#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];
};

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

struct tint_private_vars_struct {
  uint local_invocation_index_1;
};

void tint_zero_workgroup_memory(uint local_idx, threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3>* const tint_symbol) {
  for(uint idx_1 = local_idx; (idx_1 < 6u); idx_1 = (idx_1 + 1u)) {
    TINT_ISOLATE_UB(tint_volatile_false);
    uint const i = (idx_1 / 2u);
    uint const i_1 = (idx_1 % 2u);
    uint const i_2 = (idx_1 % 1u);
    atomic_store_explicit(&((*(tint_symbol))[i][i_1][i_2]), 0u, memory_order_relaxed);
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
}

uint tint_div(uint lhs, uint rhs) {
  return (lhs / select(rhs, 1u, (rhs == 0u)));
}

uint tint_mod(uint lhs, uint rhs) {
  return (lhs % select(rhs, 1u, (rhs == 0u)));
}

void compute_main_inner(uint local_invocation_index_2, threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3>* const tint_symbol_1) {
  uint idx = 0u;
  idx = local_invocation_index_2;
  while(true) {
    TINT_ISOLATE_UB(tint_volatile_false_1);
    if (!((idx < 6u))) {
      break;
    }
    uint const x_31 = idx;
    uint const x_33 = idx;
    uint const x_35 = idx;
    atomic_store_explicit(&((*(tint_symbol_1))[tint_div(x_31, 2u)][tint_mod(x_33, 2u)][tint_mod(x_35, 1u)]), 0u, memory_order_relaxed);
    {
      idx = (idx + 1u);
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  atomic_store_explicit(&((*(tint_symbol_1))[2][1][0]), 1u, memory_order_relaxed);
  return;
}

void compute_main_1(thread tint_private_vars_struct* const tint_private_vars, threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3>* const tint_symbol_2) {
  uint const x_57 = (*(tint_private_vars)).local_invocation_index_1;
  compute_main_inner(x_57, tint_symbol_2);
  return;
}

void compute_main_inner_1(uint local_invocation_index_1_param, thread tint_private_vars_struct* const tint_private_vars, threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3>* const tint_symbol_3) {
  tint_zero_workgroup_memory(local_invocation_index_1_param, tint_symbol_3);
  (*(tint_private_vars)).local_invocation_index_1 = local_invocation_index_1_param;
  compute_main_1(tint_private_vars, tint_symbol_3);
}

kernel void compute_main(uint local_invocation_index_1_param [[thread_index_in_threadgroup]]) {
  thread tint_private_vars_struct tint_private_vars = {};
  threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3> tint_symbol_4;
  compute_main_inner_1(local_invocation_index_1_param, &(tint_private_vars), &(tint_symbol_4));
  return;
}

