#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*/ ps_clrex();
unsigned long ps_ldrexw(unsigned long *);
unsigned long ps_strexw(unsigned long, unsigned long *);
int ps_cas(unsigned long *target, unsigned long old, unsigned long updated) {
  unsigned long oldval, res;
  do {
    oldval = ps_ldrexw(target);
    if (oldval == old) {
      res = ps_strexw(updated, target);
    } else {
      ps_clrex();
      return 0;
    }
  } while (res);
  return 1;
}


x86 assembly (clang, -O0):
<ps_cas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ps_ldrexw@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1175 <ps_cas+0x45>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <ps_strexw@plt>
mov    %rax,-0x30(%rbp)
jmp    1188 <ps_cas+0x58>
mov    $0x0,%al
call   1040 <ps_clrex@plt>
movl   $0x0,-0x4(%rbp)
jmp    119f <ps_cas+0x6f>
jmp    118d <ps_cas+0x5d>
cmpq   $0x0,-0x30(%rbp)
jne    1144 <ps_cas+0x14>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ps_cas>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <ps_ldrexw@plt>
cmp    %r15,%rax
jne    1164 <ps_cas+0x34>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <ps_strexw@plt>
test   %rax,%rax
jne    1140 <ps_cas+0x10>
mov    $0x1,%ebx
jmp    116d <ps_cas+0x3d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <ps_clrex@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @ps_cas(i64* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i64* %0, i64** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
br label %10
10: ; preds = %23, %3
%11 = load i64*, i64** %5
%12 = call i64 @ps_ldrexw(i64* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* %6
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i64, i64* %7
%18 = load i64*, i64** %5
%19 = call i64 @ps_strexw(i64 %17, i64* %18)
store i64 %19, i64* %9
br label %22
20: ; preds = %10
%21 = call i32 (...) @ps_clrex()
store i32 0, i32* %4
br label %27
22: ; preds = %16
br label %23
23: ; preds = %22
%24 = load i64, i64* %9
%25 = icmp ne i64 %24, 0
br i1 %25, label %10, label %26
26: ; preds = %23
store i32 1, i32* %4
br label %27
27: ; preds = %26, %20
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @ps_cas(i64* %0, i64 %1, i64 %2) {
br label %4
4: ; preds = %7, %3
%5 = tail call i64 @ps_ldrexw(i64* %0)
%6 = icmp eq i64 %5, %1
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i64 @ps_strexw(i64 %2, i64* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %4
10: ; preds = %4
%11 = tail call i32 (...) @ps_clrex()
br label %12
12: ; preds = %7, %10
%13 = phi i32 [ 0, %10 ], [ 1, %7 ]
ret i32 %13
}