#include <metal_stdlib>
using namespace metal;

template<typename T, size_t N>
struct tint_array {
  const constant T& operator[](size_t i) const constant { return elements[i]; }
  device T& operator[](size_t i) device { return elements[i]; }
  const device T& operator[](size_t i) const device { return elements[i]; }
  thread T& operator[](size_t i) thread { return elements[i]; }
  const thread T& operator[](size_t i) const thread { return elements[i]; }
  threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
  const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
  T elements[N];
};

struct S {
  /* 0x0000 */ tint_array<int4, 4> arr;
};

struct tint_module_vars_struct {
  thread tint_array<int4, 4>* src_private;
  threadgroup tint_array<int4, 4>* src_workgroup;
  const constant S* src_uniform;
  device S* src_storage;
  threadgroup tint_array<int4, 4>* dst;
  threadgroup tint_array<tint_array<tint_array<int, 2>, 3>, 4>* dst_nested;
};

#define TINT_ISOLATE_UB(VOLATILE_NAME) \
  {volatile bool VOLATILE_NAME = false; if (VOLATILE_NAME) break;}

struct tint_symbol_4 {
  tint_array<int4, 4> tint_symbol_1;
  tint_array<int4, 4> tint_symbol_2;
  tint_array<tint_array<tint_array<int, 2>, 3>, 4> tint_symbol_3;
};

tint_array<int4, 4> ret_arr() {
  return tint_array<int4, 4>{};
}

S ret_struct_arr() {
  return S{};
}

void foo(tint_array<int4, 4> src_param, tint_module_vars_struct tint_module_vars) {
  tint_array<int4, 4> src_function = {};
  (*tint_module_vars.dst) = tint_array<int4, 4>{int4(1), int4(2), int4(3), int4(3)};
  (*tint_module_vars.dst) = src_param;
  (*tint_module_vars.dst) = ret_arr();
  tint_array<int4, 4> const src_let = tint_array<int4, 4>{};
  (*tint_module_vars.dst) = src_let;
  (*tint_module_vars.dst) = src_function;
  (*tint_module_vars.dst) = (*tint_module_vars.src_private);
  (*tint_module_vars.dst) = (*tint_module_vars.src_workgroup);
  (*tint_module_vars.dst) = ret_struct_arr().arr;
  (*tint_module_vars.dst) = (*tint_module_vars.src_uniform).arr;
  (*tint_module_vars.dst) = (*tint_module_vars.src_storage).arr;
  tint_array<tint_array<tint_array<int, 2>, 3>, 4> src_nested = {};
  (*tint_module_vars.dst_nested) = src_nested;
}

void tint_symbol_inner(uint tint_local_index, tint_module_vars_struct tint_module_vars) {
  {
    uint v = 0u;
    v = tint_local_index;
    while(true) {
      TINT_ISOLATE_UB(tint_volatile_false)
      uint const v_1 = v;
      if ((v_1 >= 4u)) {
        break;
      }
      (*tint_module_vars.src_workgroup)[v_1] = int4(0);
      (*tint_module_vars.dst)[v_1] = int4(0);
      {
        v = (v_1 + 1u);
      }
      continue;
    }
  }
  {
    uint v_2 = 0u;
    v_2 = tint_local_index;
    while(true) {
      TINT_ISOLATE_UB(tint_volatile_false_1)
      uint const v_3 = v_2;
      if ((v_3 >= 24u)) {
        break;
      }
      (*tint_module_vars.dst_nested)[(v_3 / 6u)][((v_3 / 2u) % 3u)][(v_3 % 2u)] = 0;
      {
        v_2 = (v_3 + 1u);
      }
      continue;
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  tint_array<int4, 4> const val = tint_array<int4, 4>{};
  foo(val, tint_module_vars);
}

kernel void tint_symbol(uint tint_local_index [[thread_index_in_threadgroup]], threadgroup tint_symbol_4* v_4 [[threadgroup(0)]], const constant S* src_uniform [[buffer(0)]], device S* src_storage [[buffer(1)]]) {
  thread tint_array<int4, 4> src_private = {};
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.src_private=(&src_private), .src_workgroup=(&(*v_4).tint_symbol_1), .src_uniform=src_uniform, .src_storage=src_storage, .dst=(&(*v_4).tint_symbol_2), .dst_nested=(&(*v_4).tint_symbol_3)};
  tint_symbol_inner(tint_local_index, tint_module_vars);
}
