#version 310 es

uint tint_div(uint lhs, uint rhs) {
  return (lhs / mix(rhs, 1u, (rhs == 0u)));
}

uint tint_mod(uint lhs, uint rhs) {
  return (lhs % mix(rhs, 1u, (rhs == 0u)));
}

shared uint wg[3][2][1];
void tint_zero_workgroup_memory(uint local_idx) {
  {
    for(uint idx_1 = local_idx; (idx_1 < 6u); idx_1 = (idx_1 + 1u)) {
      uint i = tint_div(idx_1, 2u);
      uint i_1 = tint_mod(idx_1, 2u);
      uint i_2 = tint_mod(idx_1, 1u);
      atomicExchange(wg[i][i_1][i_2], 0u);
    }
  }
  barrier();
}

uint local_invocation_index_1 = 0u;
void compute_main_inner(uint local_invocation_index_2) {
  uint idx = 0u;
  idx = local_invocation_index_2;
  while (true) {
    if (!((idx < 6u))) {
      break;
    }
    uint x_31 = idx;
    uint x_33 = idx;
    uint x_35 = idx;
    atomicExchange(wg[tint_div(x_31, 2u)][tint_mod(x_33, 2u)][tint_mod(x_35, 1u)], 0u);
    {
      idx = (idx + 1u);
    }
  }
  barrier();
  atomicExchange(wg[2][1][0], 1u);
  return;
}

void compute_main_1() {
  uint x_57 = local_invocation_index_1;
  compute_main_inner(x_57);
  return;
}

void compute_main(uint local_invocation_index_1_param) {
  tint_zero_workgroup_memory(local_invocation_index_1_param);
  local_invocation_index_1 = local_invocation_index_1_param;
  compute_main_1();
}

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  compute_main(gl_LocalInvocationIndex);
  return;
}
