#include <metal_stdlib>

using namespace metal;
uint4 textureGather_49b07f(texture2d<uint, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  uint4 res = tint_symbol_1.gather(tint_symbol_2, float2(1.0f), int2(1), component::y);
  return res;
}

fragment void fragment_main(device uint4* tint_symbol_3 [[buffer(0)]], texture2d<uint, access::sample> tint_symbol_4 [[texture(0)]], sampler tint_symbol_5 [[sampler(0)]]) {
  *(tint_symbol_3) = textureGather_49b07f(tint_symbol_4, tint_symbol_5);
  return;
}

kernel void compute_main(device uint4* tint_symbol_6 [[buffer(0)]], texture2d<uint, access::sample> tint_symbol_7 [[texture(0)]], sampler tint_symbol_8 [[sampler(0)]]) {
  *(tint_symbol_6) = textureGather_49b07f(tint_symbol_7, tint_symbol_8);
  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::sample> tint_symbol_9, sampler tint_symbol_10) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureGather_49b07f(tint_symbol_9, tint_symbol_10);
  return out;
}

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

