#include <metal_stdlib>

using namespace metal;
uint textureNumLevels_903920(texturecube_array<int, access::sample> tint_symbol_1) {
  uint res = tint_symbol_1.get_num_mip_levels();
  return res;
}

fragment void fragment_main(device uint* tint_symbol_2 [[buffer(0)]], texturecube_array<int, access::sample> tint_symbol_3 [[texture(0)]]) {
  *(tint_symbol_2) = textureNumLevels_903920(tint_symbol_3);
  return;
}

kernel void compute_main(device uint* tint_symbol_4 [[buffer(0)]], texturecube_array<int, access::sample> tint_symbol_5 [[texture(0)]]) {
  *(tint_symbol_4) = textureNumLevels_903920(tint_symbol_5);
  return;
}

struct VertexOutput {
  float4 pos;
  uint prevent_dce;
};

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

VertexOutput vertex_main_inner(texturecube_array<int, access::sample> tint_symbol_6) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureNumLevels_903920(tint_symbol_6);
  return out;
}

vertex tint_symbol vertex_main(texturecube_array<int, access::sample> 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;
}

