#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
long cSunMiscSignal_atomicCompareAndSwap_long(long volatile *const, long, long);
long cSunMiscSignal_atomicDecrementToZero(volatile long *const address) {
  long result = 0;
  long sample;
  do {
    sample = *address;
    if (sample <= 0) {
      break;
    }
    result =
        cSunMiscSignal_atomicCompareAndSwap_long(address, sample, sample - 1);
  } while (result != sample);
  return result;
}


x86 assembly (clang, -O0):
<cSunMiscSignal_atomicDecrementToZero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jg     113f <cSunMiscSignal_atomicDecrementToZero+0x2f>
jmp    1166 <cSunMiscSignal_atomicDecrementToZero+0x56>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <cSunMiscSignal_atomicCompareAndSwap_long@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1124 <cSunMiscSignal_atomicDecrementToZero+0x14>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cSunMiscSignal_atomicDecrementToZero>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%rbx
test   %rbx,%rbx
jle    1141 <cSunMiscSignal_atomicDecrementToZero+0x31>
mov    %rdi,%r14
nop
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <cSunMiscSignal_atomicCompareAndSwap_long@plt>
cmp    %rbx,%rax
je     1143 <cSunMiscSignal_atomicDecrementToZero+0x33>
mov    (%r14),%rbx
test   %rbx,%rbx
jg     1120 <cSunMiscSignal_atomicDecrementToZero+0x10>
mov    %rax,%rbx
jmp    1143 <cSunMiscSignal_atomicDecrementToZero+0x33>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @cSunMiscSignal_atomicDecrementToZero(i64* %0) {
%2 = alloca i64*
%3 = alloca i64
%4 = alloca i64
store i64* %0, i64** %2
store i64 0, i64* %3
br label %5
5: ; preds = %17, %1
%6 = load i64*, i64** %2
%7 = load volatile i64, i64* %6
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp sle i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
br label %21
11: ; preds = %5
%12 = load i64*, i64** %2
%13 = load i64, i64* %4
%14 = load i64, i64* %4
%15 = sub nsw i64 %14, 1
%16 = call i64 @cSunMiscSignal_atomicCompareAndSwap_long(i64* %12, i64 %13, i64 %15)
store i64 %16, i64* %3
br label %17
17: ; preds = %11
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = icmp ne i64 %18, %19
br i1 %20, label %5, label %21
21: ; preds = %17, %10
%22 = load i64, i64* %3
ret i64 %22
}

LLVM IR (-O2):
define i64 @cSunMiscSignal_atomicDecrementToZero(i64* %0) {
%2 = load volatile i64, i64* %0
%3 = icmp slt i64 %2, 1
br i1 %3, label %12, label %7
4: ; preds = %7
%5 = load volatile i64, i64* %0
%6 = icmp slt i64 %5, 1
br i1 %6, label %12, label %7
7: ; preds = %1, %4
%8 = phi i64 [ %5, %4 ], [ %2, %1 ]
%9 = add nsw i64 %8, -1
%10 = tail call i64 @cSunMiscSignal_atomicCompareAndSwap_long(i64* %0, i64 %8, i64 %9)
%11 = icmp eq i64 %10, %8
br i1 %11, label %12, label %4
12: ; preds = %7, %4, %1
%13 = phi i64 [ 0, %1 ], [ %8, %7 ], [ %10, %4 ]
ret i64 %13
}