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

#define TINT_ISOLATE_UB(VOLATILE_NAME) \
  volatile bool VOLATILE_NAME = true; \
  if (VOLATILE_NAME)

struct tint_private_vars_struct {
  uint local_invocation_index_1;
};

void tint_zero_workgroup_memory(uint local_idx, threadgroup tint_array<atomic_uint, 4>* const tint_symbol) {
  TINT_ISOLATE_UB(tint_volatile_true) for(uint idx_1 = local_idx; (idx_1 < 4u); idx_1 = (idx_1 + 1u)) {
    uint const i = idx_1;
    atomic_store_explicit(&((*(tint_symbol))[i]), 0u, memory_order_relaxed);
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
}

void compute_main_inner(uint local_invocation_index_2, threadgroup tint_array<atomic_uint, 4>* const tint_symbol_1) {
  uint idx = 0u;
  idx = local_invocation_index_2;
  TINT_ISOLATE_UB(tint_volatile_true_1) while(true) {
    if (!((idx < 4u))) {
      break;
    }
    uint const x_26 = idx;
    atomic_store_explicit(&((*(tint_symbol_1))[x_26]), 0u, memory_order_relaxed);
    {
      idx = (idx + 1u);
    }
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  atomic_store_explicit(&((*(tint_symbol_1))[1]), 1u, memory_order_relaxed);
  return;
}

void compute_main_1(thread tint_private_vars_struct* const tint_private_vars, threadgroup tint_array<atomic_uint, 4>* const tint_symbol_2) {
  uint const x_47 = (*(tint_private_vars)).local_invocation_index_1;
  compute_main_inner(x_47, tint_symbol_2);
  return;
}

void compute_main_inner_1(uint local_invocation_index_1_param, thread tint_private_vars_struct* const tint_private_vars, threadgroup tint_array<atomic_uint, 4>* const tint_symbol_3) {
  tint_zero_workgroup_memory(local_invocation_index_1_param, tint_symbol_3);
  (*(tint_private_vars)).local_invocation_index_1 = local_invocation_index_1_param;
  compute_main_1(tint_private_vars, tint_symbol_3);
}

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<atomic_uint, 4> tint_symbol_4;
  compute_main_inner_1(local_invocation_index_1_param, &(tint_private_vars), &(tint_symbol_4));
  return;
}

