#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_module_vars_struct {
  thread uint* local_invocation_index_1;
  threadgroup tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3>* wg;
};

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

struct tint_symbol_1 {
  tint_array<tint_array<tint_array<atomic_uint, 1>, 2>, 3> tint_symbol;
};

uint tint_mod_u32(uint lhs, uint rhs) {
  uint const v = select(rhs, 1u, (rhs == 0u));
  return (lhs - ((lhs / v) * v));
}

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

void compute_main_inner(uint local_invocation_index_2, tint_module_vars_struct tint_module_vars) {
  uint idx = 0u;
  idx = local_invocation_index_2;
  {
    while(true) {
      TINT_ISOLATE_UB(tint_volatile_false)
      if (!((idx < 6u))) {
        break;
      }
      uint const x_31 = idx;
      uint const x_33 = idx;
      uint const x_35 = idx;
      uint const v_1 = tint_div_u32(x_31, 2u);
      uint const v_2 = tint_mod_u32(x_33, 2u);
      atomic_store_explicit((&(*tint_module_vars.wg)[v_1][v_2][tint_mod_u32(x_35, 1u)]), 0u, memory_order_relaxed);
      {
        idx = (idx + 1u);
      }
      continue;
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  atomic_store_explicit((&(*tint_module_vars.wg)[2][1][0]), 1u, memory_order_relaxed);
}

void compute_main_1(tint_module_vars_struct tint_module_vars) {
  uint const x_57 = (*tint_module_vars.local_invocation_index_1);
  compute_main_inner(x_57, tint_module_vars);
}

void compute_main_inner_1(uint local_invocation_index_1_param, tint_module_vars_struct tint_module_vars) {
  {
    uint v_3 = 0u;
    v_3 = local_invocation_index_1_param;
    while(true) {
      TINT_ISOLATE_UB(tint_volatile_false_1)
      uint const v_4 = v_3;
      if ((v_4 >= 6u)) {
        break;
      }
      atomic_store_explicit((&(*tint_module_vars.wg)[(v_4 / 2u)][(v_4 % 2u)][0u]), 0u, memory_order_relaxed);
      {
        v_3 = (v_4 + 1u);
      }
      continue;
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  (*tint_module_vars.local_invocation_index_1) = local_invocation_index_1_param;
  compute_main_1(tint_module_vars);
}

kernel void compute_main(uint local_invocation_index_1_param [[thread_index_in_threadgroup]], threadgroup tint_symbol_1* v_5 [[threadgroup(0)]]) {
  thread uint local_invocation_index_1 = 0u;
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.local_invocation_index_1=(&local_invocation_index_1), .wg=(&(*v_5).tint_symbol)};
  compute_main_inner_1(local_invocation_index_1_param, tint_module_vars);
}
