#include <metal_stdlib>

using namespace metal;
int tint_first_trailing_bit(int v) {
  uint x = uint(v);
  uint const b16 = select(16u, 0u, bool((x & 65535u)));
  x = (x >> b16);
  uint const b8 = select(8u, 0u, bool((x & 255u)));
  x = (x >> b8);
  uint const b4 = select(4u, 0u, bool((x & 15u)));
  x = (x >> b4);
  uint const b2 = select(2u, 0u, bool((x & 3u)));
  x = (x >> b2);
  uint const b1 = select(1u, 0u, bool((x & 1u)));
  uint const is_zero = select(0u, 4294967295u, (x == 0u));
  return int((((((b16 | b8) | b4) | b2) | b1) | is_zero));
}

int firstTrailingBit_3a2acc() {
  int arg_0 = 1;
  int res = tint_first_trailing_bit(arg_0);
  return res;
}

fragment void fragment_main(device int* tint_symbol_1 [[buffer(0)]]) {
  *(tint_symbol_1) = firstTrailingBit_3a2acc();
  return;
}

kernel void compute_main(device int* tint_symbol_2 [[buffer(0)]]) {
  *(tint_symbol_2) = firstTrailingBit_3a2acc();
  return;
}

struct VertexOutput {
  float4 pos;
  int prevent_dce;
};

struct tint_symbol {
  int prevent_dce [[user(locn0)]] [[flat]];
  float4 pos [[position]];
};

VertexOutput vertex_main_inner() {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = firstTrailingBit_3a2acc();
  return out;
}

vertex tint_symbol vertex_main() {
  VertexOutput const inner_result = vertex_main_inner();
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

