#include <metal_stdlib>

using namespace metal;

struct atomic_compare_exchange_result_i32 {
  int old_value;
  bool exchanged;
};
atomic_compare_exchange_result_i32 atomicCompareExchangeWeak_1(device atomic_int* atomic, int compare, int value) {
  int old_value = compare;
  bool exchanged = atomic_compare_exchange_weak_explicit(atomic, &old_value, value, memory_order_relaxed, memory_order_relaxed);
  return {old_value, exchanged};
}

struct tint_private_vars_struct {
  bool tint_discarded;
};

struct tint_symbol_2 {
  float4 value [[color(0)]];
};

float4 tint_symbol_inner(thread tint_private_vars_struct* const tint_private_vars, device atomic_int* const tint_symbol_4) {
  if (false) {
    (*(tint_private_vars)).tint_discarded = true;
  }
  atomic_compare_exchange_result_i32 tint_symbol_3 = {};
  if (!((*(tint_private_vars)).tint_discarded)) {
    tint_symbol_3 = atomicCompareExchangeWeak_1(tint_symbol_4, 0, 1);
  }
  atomic_compare_exchange_result_i32 const tint_symbol_1 = tint_symbol_3;
  int const old_value = tint_symbol_1.old_value;
  return float4(float(old_value));
}

fragment tint_symbol_2 tint_symbol(device atomic_int* tint_symbol_5 [[buffer(0)]]) {
  thread tint_private_vars_struct tint_private_vars = {};
  tint_private_vars.tint_discarded = false;
  float4 const inner_result = tint_symbol_inner(&(tint_private_vars), tint_symbol_5);
  tint_symbol_2 wrapper_result = {};
  wrapper_result.value = inner_result;
  if (tint_private_vars.tint_discarded) {
    discard_fragment();
  }
  return wrapper_result;
}

