#include <metal_stdlib>

using namespace metal;
int4 textureGather_5ba85f(texturecube<int, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  int4 res = tint_symbol_1.gather(tint_symbol_2, float3(1.0f), component::y);
  return res;
}

fragment void fragment_main(device int4* tint_symbol_3 [[buffer(0)]], texturecube<int, access::sample> tint_symbol_4 [[texture(0)]], sampler tint_symbol_5 [[sampler(0)]]) {
  *(tint_symbol_3) = textureGather_5ba85f(tint_symbol_4, tint_symbol_5);
  return;
}

kernel void compute_main(device int4* tint_symbol_6 [[buffer(0)]], texturecube<int, access::sample> tint_symbol_7 [[texture(0)]], sampler tint_symbol_8 [[sampler(0)]]) {
  *(tint_symbol_6) = textureGather_5ba85f(tint_symbol_7, tint_symbol_8);
  return;
}

struct VertexOutput {
  float4 pos;
  int4 prevent_dce;
};

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

VertexOutput vertex_main_inner(texturecube<int, access::sample> tint_symbol_9, sampler tint_symbol_10) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureGather_5ba85f(tint_symbol_9, tint_symbol_10);
  return out;
}

vertex tint_symbol vertex_main(texturecube<int, 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;
}

