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

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) {
  return (lhs - ((lhs / select(rhs, 1u, (rhs == 0u))) * select(rhs, 1u, (rhs == 0u))));
}

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;
  {
    uint2 tint_loop_idx = uint2(4294967295u);
    while(true) {
      if (all((tint_loop_idx == uint2(0u)))) {
        break;
      }
      if (!((idx < 6u))) {
        break;
      }
      uint const x_31 = idx;
      uint const x_33 = idx;
      uint const x_35 = idx;
      uint const v = tint_div_u32(x_31, 2u);
      uint const v_1 = tint_mod_u32(x_33, 2u);
      atomic_store_explicit((&(*tint_module_vars.wg)[min(v, 2u)][min(v_1, 1u)][min(tint_mod_u32(x_35, 1u), 0u)]), 0u, memory_order_relaxed);
      {
        uint const tint_low_inc = (tint_loop_idx.x - 1u);
        tint_loop_idx.x = tint_low_inc;
        uint const tint_carry = uint((tint_low_inc == 4294967295u));
        tint_loop_idx.y = (tint_loop_idx.y - tint_carry);
        idx = (idx + 1u);
      }
      continue;
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  atomic_store_explicit((&(*tint_module_vars.wg)[2u][1u][0u]), 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_2 = 0u;
    v_2 = local_invocation_index_1_param;
    while(true) {
      uint const v_3 = v_2;
      if ((v_3 >= 6u)) {
        break;
      }
      atomic_store_explicit((&(*tint_module_vars.wg)[(v_3 / 2u)][(v_3 % 2u)][0u]), 0u, memory_order_relaxed);
      {
        v_2 = (v_3 + 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_4 [[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_4).tint_symbol)};
  compute_main_inner_1(local_invocation_index_1_param, tint_module_vars);
}
