#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 firstTrailingBit_7496d6() {
  int3 arg_0 = int3(1);
  uint3 const v = as_type<uint3>(arg_0);
  uint3 const v_1 = select(uint3(0u), uint3(16u), ((v & uint3(65535u)) == uint3(0u)));
  uint3 const v_2 = select(uint3(0u), uint3(8u), (((v >> v_1) & uint3(255u)) == uint3(0u)));
  uint3 const v_3 = select(uint3(0u), uint3(4u), ((((v >> v_1) >> v_2) & uint3(15u)) == uint3(0u)));
  uint3 const v_4 = select(uint3(0u), uint3(2u), (((((v >> v_1) >> v_2) >> v_3) & uint3(3u)) == uint3(0u)));
  int3 res = as_type<int3>(select((v_1 | (v_2 | (v_3 | (v_4 | select(uint3(0u), uint3(1u), ((((((v >> v_1) >> v_2) >> v_3) >> v_4) & uint3(1u)) == uint3(0u))))))), uint3(4294967295u), (((((v >> v_1) >> v_2) >> v_3) >> v_4) == 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(firstTrailingBit_7496d6());
}

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(firstTrailingBit_7496d6());
}

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

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