#include <metal_stdlib>
using namespace metal;

struct S {
  int a;
  int b;
};

struct tint_module_vars_struct {
  device int* s;
  threadgroup atomic_int* g1;
};

struct tint_symbol_2 {
  atomic_int tint_symbol_1;
};

int accept_value(int val) {
  return val;
}

int accept_ptr_deref_call_func(thread int* const val) {
  int const v = (*val);
  return as_type<int>((as_type<uint>(v) + as_type<uint>(accept_value((*val)))));
}

int accept_ptr_deref_pass_through(thread int* const val) {
  int const v_1 = (*val);
  return as_type<int>((as_type<uint>(v_1) + as_type<uint>(accept_ptr_deref_call_func(val))));
}

int accept_ptr_to_struct_and_access(thread S* const val) {
  return as_type<int>((as_type<uint>((*val).a) + as_type<uint>((*val).b)));
}

int accept_ptr_to_struct_access_pass_ptr(thread S* const val) {
  thread int* const b = (&(*val).a);
  (*b) = 2;
  return (*b);
}

int tint_f32_to_i32(float value) {
  return select(2147483647, select((-2147483647 - 1), int(value), (value >= -2147483648.0f)), (value <= 2147483520.0f));
}

int accept_ptr_vec_access_elements(thread float3* const v1) {
  (*v1)[0u] = cross((*v1), (*v1))[0u];
  return tint_f32_to_i32((*v1)[0u]);
}

int call_builtin_with_mod_scope_ptr(tint_module_vars_struct tint_module_vars) {
  return atomic_load_explicit(tint_module_vars.g1, memory_order_relaxed);
}

void tint_symbol_inner(uint tint_local_index, tint_module_vars_struct tint_module_vars) {
  if ((tint_local_index == 0u)) {
    atomic_store_explicit(tint_module_vars.g1, 0, memory_order_relaxed);
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  int v1 = 0;
  S v2 = S{};
  thread S* const v3 = (&v2);
  float3 v4 = float3(0.0f);
  int const t1 = atomic_load_explicit(tint_module_vars.g1, memory_order_relaxed);
  int const v_2 = accept_ptr_deref_pass_through((&v1));
  int const v_3 = as_type<int>((as_type<uint>(v_2) + as_type<uint>(accept_ptr_to_struct_and_access((&v2)))));
  int const v_4 = as_type<int>((as_type<uint>(v_3) + as_type<uint>(accept_ptr_to_struct_and_access(v3))));
  int const v_5 = as_type<int>((as_type<uint>(v_4) + as_type<uint>(accept_ptr_vec_access_elements((&v4)))));
  int const v_6 = as_type<int>((as_type<uint>(v_5) + as_type<uint>(accept_ptr_to_struct_access_pass_ptr((&v2)))));
  (*tint_module_vars.s) = as_type<int>((as_type<uint>(as_type<int>((as_type<uint>(v_6) + as_type<uint>(call_builtin_with_mod_scope_ptr(tint_module_vars))))) + as_type<uint>(t1)));
}

kernel void tint_symbol(uint tint_local_index [[thread_index_in_threadgroup]], device int* s [[buffer(0)]], threadgroup tint_symbol_2* v_7 [[threadgroup(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.s=s, .g1=(&(*v_7).tint_symbol_1)};
  tint_symbol_inner(tint_local_index, tint_module_vars);
}
