#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 S {
  tint_array<int4, 4> a;
};

struct tint_module_vars_struct {
  device tint_array<S, 1>* tint_symbol;
  thread uint* v;
};

int idx1(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 1;
}

int idx2(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 2;
}

int idx3(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 3;
}

int idx4(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 4;
}

int idx5(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 0;
}

int idx6(tint_module_vars_struct tint_module_vars) {
  (*tint_module_vars.v) = ((*tint_module_vars.v) - 1u);
  return 2;
}

void tint_symbol_1(tint_module_vars_struct tint_module_vars) {
  {
    int const v_1 = idx1(tint_module_vars);
    device int4* const v_2 = (&(*tint_module_vars.tint_symbol)[v_1].a[idx2(tint_module_vars)]);
    int const v_3 = idx3(tint_module_vars);
    (*v_2)[v_3] = ((*v_2)[v_3] - 1);
    while(true) {
      if (((*tint_module_vars.v) < 10u)) {
      } else {
        break;
      }
      {
        int const v_4 = idx4(tint_module_vars);
        device int4* const v_5 = (&(*tint_module_vars.tint_symbol)[v_4].a[idx5(tint_module_vars)]);
        int const v_6 = idx6(tint_module_vars);
        (*v_5)[v_6] = ((*v_5)[v_6] - 1);
      }
      continue;
    }
  }
}
