#include <metal_stdlib>

using namespace metal;
int2 tint_first_leading_bit(int2 v) {
  uint2 x = select(uint2(v), uint2(~(v)), (v < int2(0)));
  uint2 const b16 = select(uint2(0u), uint2(16u), bool2((x & uint2(4294901760u))));
  x = (x >> b16);
  uint2 const b8 = select(uint2(0u), uint2(8u), bool2((x & uint2(65280u))));
  x = (x >> b8);
  uint2 const b4 = select(uint2(0u), uint2(4u), bool2((x & uint2(240u))));
  x = (x >> b4);
  uint2 const b2 = select(uint2(0u), uint2(2u), bool2((x & uint2(12u))));
  x = (x >> b2);
  uint2 const b1 = select(uint2(0u), uint2(1u), bool2((x & uint2(2u))));
  uint2 const is_zero = select(uint2(0u), uint2(4294967295u), (x == uint2(0u)));
  return int2((((((b16 | b8) | b4) | b2) | b1) | is_zero));
}

int2 firstLeadingBit_a622c2() {
  int2 arg_0 = int2(1);
  int2 res = tint_first_leading_bit(arg_0);
  return res;
}

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

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

struct VertexOutput {
  float4 pos;
  int2 prevent_dce;
};

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

VertexOutput vertex_main_inner() {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = firstLeadingBit_a622c2();
  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;
}

