#include <metal_stdlib>

using namespace metal;
uint4 textureLoad_aa8a0d(texture2d<uint, access::read> tint_symbol_1) {
  uint4 res = tint_symbol_1.read(uint2(uint2(1u)));
  return res;
}

fragment void fragment_main(device uint4* tint_symbol_2 [[buffer(0)]], texture2d<uint, access::read> tint_symbol_3 [[texture(0)]]) {
  *(tint_symbol_2) = textureLoad_aa8a0d(tint_symbol_3);
  return;
}

kernel void compute_main(device uint4* tint_symbol_4 [[buffer(0)]], texture2d<uint, access::read> tint_symbol_5 [[texture(0)]]) {
  *(tint_symbol_4) = textureLoad_aa8a0d(tint_symbol_5);
  return;
}

struct VertexOutput {
  float4 pos;
  uint4 prevent_dce;
};

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

VertexOutput vertex_main_inner(texture2d<uint, access::read> tint_symbol_6) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureLoad_aa8a0d(tint_symbol_6);
  return out;
}

vertex tint_symbol vertex_main(texture2d<uint, access::read> tint_symbol_7 [[texture(0)]]) {
  VertexOutput const inner_result = vertex_main_inner(tint_symbol_7);
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

