#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ compare_and_swap(intptr_t *, intptr_t, intptr_t);
int decrement_and_test_and_set(intptr_t *p) {
  intptr_t old, new;
  do {
    old = *p;
    new = old - 2;
    if (new == 0)
      new = 1;
  } while (!compare_and_swap(p, old, new));
  assert(*p != (intptr_t)-1);
  assert(*p < (intptr_t)10000);
  return (old - new) & 0x1;
}


x86 assembly (clang, -O0):
<decrement_and_test_and_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1156 <decrement_and_test_and_set+0x36>
movq   $0x1,-0x18(%rbp)
jmp    115b <decrement_and_test_and_set+0x3b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <compare_and_swap@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    112c <decrement_and_test_and_set+0xc>
mov    -0x8(%rbp),%rax
cmpq   $0xffffffffffffffff,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x2710,(%rax)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
and    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrement_and_test_and_set>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x1,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%r14
mov    %r14,%rbx
add    $0xfffffffffffffffe,%rbx
cmove  %r12,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <compare_and_swap@plt>
test   %eax,%eax
je     1140 <decrement_and_test_and_set+0x20>
xor    %edi,%edi
cmpq   $0xffffffffffffffff,(%r15)
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
cmpq   $0x2710,(%r15)
setl   %dil
call   1030 <assert@plt>
sub    %ebx,%r14d
and    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @decrement_and_test_and_set(i64* %0) {
%2 = alloca i64*
%3 = alloca i64
%4 = alloca i64
store i64* %0, i64** %2
br label %5
5: ; preds = %14, %1
%6 = load i64*, i64** %2
%7 = load i64, i64* %6
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = sub nsw i64 %8, 2
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %5
store i64 1, i64* %4
br label %13
13: ; preds = %12, %5
br label %14
14: ; preds = %13
%15 = load i64*, i64** %2
%16 = load i64, i64* %3
%17 = load i64, i64* %4
%18 = call i32 @compare_and_swap(i64* %15, i64 %16, i64 %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %5, label %21
21: ; preds = %14
%22 = load i64*, i64** %2
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, -1
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i64*, i64** %2
%28 = load i64, i64* %27
%29 = icmp slt i64 %28, 10000
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i64, i64* %3
%33 = load i64, i64* %4
%34 = sub nsw i64 %32, %33
%35 = and i64 %34, 1
%36 = trunc i64 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
define i32 @decrement_and_test_and_set(i64* %0) {
br label %2
2: ; preds = %2, %1
%3 = load i64, i64* %0
%4 = add nsw i64 %3, -2
%5 = icmp eq i64 %4, 0
%6 = select i1 %5, i64 1, i64 %4
%7 = tail call i32 @compare_and_swap(i64* %0, i64 %3, i64 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %2, label %9
9: ; preds = %2
%10 = load i64, i64* %0
%11 = icmp ne i64 %10, -1
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = load i64, i64* %0
%15 = icmp slt i64 %14, 10000
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = sub nsw i64 %3, %6
%19 = trunc i64 %18 to i32
%20 = and i32 %19, 1
ret i32 %20
}