#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 tint_private_vars_struct {
  uint local_invocation_index_1;
};

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) {
  uint idx = 0u;
  idx = local_invocation_index_2;
  while (true) {
    __asm__("");
    if (!((idx < 6u))) {
      break;
    }
    uint const x_31 = idx;
    uint const x_33 = idx;
    uint const x_35 = idx;
    atomic_store_explicit(&((*(tint_symbol))[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))[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_1) {
  uint const x_57 = (*(tint_private_vars)).local_invocation_index_1;
  compute_main_inner(x_57, tint_symbol_1);
  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_2) {
  for(uint idx_1 = local_invocation_index_1_param; (idx_1 < 6u); idx_1 = (idx_1 + 1u)) {
    __asm__("");
    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_2))[i][i_1][i_2]), 0u, memory_order_relaxed);
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  (*(tint_private_vars)).local_invocation_index_1 = local_invocation_index_1_param;
  compute_main_1(tint_private_vars, tint_symbol_2);
}

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_3;
  compute_main_inner_1(local_invocation_index_1_param, &(tint_private_vars), &(tint_symbol_3));
  return;
}

