#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device uint* prevent_dce;
};

struct VertexOutput {
  float4 pos;
  uint prevent_dce;
};

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

uint tint_dot(uint4 lhs, uint4 rhs) {
  return ((((lhs * rhs)[0u] + (lhs * rhs)[1u]) + (lhs * rhs)[2u]) + (lhs * rhs)[3u]);
}

uint dot4U8Packed_fbed7b() {
  uint arg_0 = 1u;
  uint arg_1 = 1u;
  uint const v = arg_0;
  uint const v_1 = arg_1;
  uint4 const v_2 = uint4(0u, 8u, 16u, 24u);
  uint4 const v_3 = (uint4(v) >> v_2);
  uint4 const v_4 = (v_3 & uint4(255u));
  uint4 const v_5 = uint4(0u, 8u, 16u, 24u);
  uint4 const v_6 = (uint4(v_1) >> v_5);
  uint res = tint_dot(v_4, (v_6 & uint4(255u)));
  return res;
}

fragment void fragment_main(device uint* 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) = dot4U8Packed_fbed7b();
}

kernel void compute_main(device uint* 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) = dot4U8Packed_fbed7b();
}

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

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