#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device uint4* prevent_dce;
  texture1d<uint, access::read> arg_0;
};

struct VertexOutput {
  float4 pos;
  uint4 prevent_dce;
};

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

uint4 textureLoad_5bb7fb(tint_module_vars_struct tint_module_vars) {
  int arg_1 = 1;
  uint4 res = tint_module_vars.arg_0.read(uint(arg_1));
  return res;
}

fragment void fragment_main(device uint4* prevent_dce [[buffer(0)]], texture1d<uint, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0};
  (*tint_module_vars.prevent_dce) = textureLoad_5bb7fb(tint_module_vars);
}

kernel void compute_main(device uint4* prevent_dce [[buffer(0)]], texture1d<uint, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0};
  (*tint_module_vars.prevent_dce) = textureLoad_5bb7fb(tint_module_vars);
}

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

vertex vertex_main_outputs vertex_main(texture1d<uint, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.arg_0=arg_0};
  VertexOutput const v = vertex_main_inner(tint_module_vars);
  vertex_main_outputs tint_wrapper_result = {};
  tint_wrapper_result.VertexOutput_pos = v.pos;
  tint_wrapper_result.VertexOutput_prevent_dce = v.prevent_dce;
  return tint_wrapper_result;
}
