#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<atomic_uint, 4>* wg;
};

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

struct tint_symbol_1 {
  tint_array<atomic_uint, 4> tint_symbol;
};

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 < 4u))) {
        break;
      }
      uint const x_26 = idx;
      atomic_store_explicit((&(*tint_module_vars.wg)[x_26]), 0u, memory_order_relaxed);
      {
        idx = (idx + 1u);
      }
      continue;
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  atomic_store_explicit((&(*tint_module_vars.wg)[1]), 1u, memory_order_relaxed);
}

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

void compute_main_inner_1(uint local_invocation_index_1_param, tint_module_vars_struct tint_module_vars) {
  {
    uint v = 0u;
    v = local_invocation_index_1_param;
    while(true) {
      TINT_ISOLATE_UB(tint_volatile_false_1)
      uint const v_1 = v;
      if ((v_1 >= 4u)) {
        break;
      }
      atomic_store_explicit((&(*tint_module_vars.wg)[v_1]), 0u, memory_order_relaxed);
      {
        v = (v_1 + 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_2 [[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_2).tint_symbol)};
  compute_main_inner_1(local_invocation_index_1_param, tint_module_vars);
}
