#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device packed_int3* prevent_dce;
};

struct VertexOutput {
  float4 pos;
  int3 prevent_dce;
};

struct vertex_main_outputs {
  float4 VertexOutput_pos [[position]];
  int3 VertexOutput_prevent_dce [[user(locn0)]] [[flat]];
};

int3 firstLeadingBit_35053e() {
  int3 arg_0 = int3(1);
  uint3 const v = as_type<uint3>(arg_0);
  uint3 const v_1 = select(~(v), v, (v < uint3(2147483648u)));
  uint3 const v_2 = select(uint3(16u), uint3(0u), ((v_1 & uint3(4294901760u)) == uint3(0u)));
  uint3 const v_3 = select(uint3(8u), uint3(0u), (((v_1 >> v_2) & uint3(65280u)) == uint3(0u)));
  uint3 const v_4 = select(uint3(4u), uint3(0u), ((((v_1 >> v_2) >> v_3) & uint3(240u)) == uint3(0u)));
  uint3 const v_5 = select(uint3(2u), uint3(0u), (((((v_1 >> v_2) >> v_3) >> v_4) & uint3(12u)) == uint3(0u)));
  int3 res = as_type<int3>(select((v_2 | (v_3 | (v_4 | (v_5 | select(uint3(1u), uint3(0u), ((((((v_1 >> v_2) >> v_3) >> v_4) >> v_5) & uint3(2u)) == uint3(0u))))))), uint3(4294967295u), (((((v_1 >> v_2) >> v_3) >> v_4) >> v_5) == uint3(0u))));
  return res;
}

fragment void fragment_main(device packed_int3* prevent_dce [[buffer(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce};
  (*tint_module_vars.prevent_dce) = packed_int3(firstLeadingBit_35053e());
}

kernel void compute_main(device packed_int3* prevent_dce [[buffer(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce};
  (*tint_module_vars.prevent_dce) = packed_int3(firstLeadingBit_35053e());
}

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

vertex vertex_main_outputs vertex_main() {
  VertexOutput const v_6 = vertex_main_inner();
  vertex_main_outputs tint_wrapper_result = {};
  tint_wrapper_result.VertexOutput_pos = v_6.pos;
  tint_wrapper_result.VertexOutput_prevent_dce = v_6.prevent_dce;
  return tint_wrapper_result;
}
