#include <metal_stdlib>

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

int4 firstLeadingBit_c1f940() {
  int4 arg_0 = int4(1);
  int4 res = tint_first_leading_bit(arg_0);
  return res;
}

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

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

struct VertexOutput {
  float4 pos;
  int4 prevent_dce;
};

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

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

