#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 = true; \
  if (VOLATILE_NAME)

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;
  {
    TINT_ISOLATE_UB(tint_volatile_true) while(true) {
      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;
    TINT_ISOLATE_UB(tint_volatile_true_1) while(true) {
      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);
}
