#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 */
typedef int uint64_t;

/* Variables and functions */
int cmpxchg_reg2reg(int, int, int *);
int /*<<< orphan*/ printf(char *, int, int, int, ...);
int xadd_reg2reg(int, int *);
int main() {
  uint64_t test, new, old_loc, loc;
  int res;
  test = 0;
  loc = 0;
  new = 1;
  old_loc = loc;
  res = cmpxchg_reg2reg(test, new, &loc);
  printf("CMPXCHG (%li == %li) new = %li -> %i loc = %li\n", old_loc, test, new,
         res, loc);
  test = 6;
  loc = 5;
  new = 10;
  old_loc = loc;
  res = cmpxchg_reg2reg(test, new, &loc);
  printf("CMPXCHG (%li == %li) new = %li -> %i loc = %li\n", old_loc, test, new,
         res, loc);
  old_loc = xadd_reg2reg(new, &loc);
  printf("XADD old_loc = %li new = %li loc = %li\n", old_loc, new, loc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x14(%rbp),%rdx
call   1050 <cmpxchg_reg2reg@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x6,-0x8(%rbp)
movl   $0x5,-0x14(%rbp)
movl   $0xa,-0xc(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x14(%rbp),%rdx
call   1050 <cmpxchg_reg2reg@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <xadd_reg2reg@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%r14
xor    %edi,%edi
mov    $0x1,%esi
mov    %r14,%rdx
call   1050 <cmpxchg_reg2reg@plt>
mov    0x4(%rsp),%r9d
lea    0xea4(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x1,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x5,0x4(%rsp)
mov    $0x6,%edi
mov    $0xa,%esi
mov    %r14,%rdx
call   1050 <cmpxchg_reg2reg@plt>
mov    0x4(%rsp),%r9d
mov    %rbx,%rdi
mov    $0x5,%esi
mov    $0x6,%edx
mov    $0xa,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
mov    %r14,%rsi
call   1030 <xadd_reg2reg@plt>
mov    0x4(%rsp),%ecx
lea    0xe6b(%rip),%rdi
mov    %eax,%esi
mov    $0xa,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [48 x i8] c"CMPXCHG (%li == %li) new = %li -> %i loc = %li\0A\00"
@.str.1 = [40 x i8] c"XADD old_loc = %li new = %li loc = %li\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %5
store i32 1, i32* %3
%7 = load i32, i32* %5
store i32 %7, i32* %4
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = call i32 @cmpxchg_reg2reg(i32 %8, i32 %9, i32* %5)
store i32 %10, i32* %6
%11 = load i32, i32* %4
%12 = load i32, i32* %2
%13 = load i32, i32* %3
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12, i32 %13, i32 %14, i32 %15)
store i32 6, i32* %2
store i32 5, i32* %5
store i32 10, i32* %3
%17 = load i32, i32* %5
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = call i32 @cmpxchg_reg2reg(i32 %18, i32 %19, i32* %5)
store i32 %20, i32* %6
%21 = load i32, i32* %4
%22 = load i32, i32* %2
%23 = load i32, i32* %3
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %22, i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %3
%28 = call i32 @xadd_reg2reg(i32 %27, i32* %5)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = load i32, i32* %3
%31 = load i32, i32* %5
%32 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30, i32 %31)
ret i32 0
}

LLVM IR (-O2):
@.str = [48 x i8] c"CMPXCHG (%li == %li) new = %li -> %i loc = %li\0A\00"
@.str.1 = [40 x i8] c"XADD old_loc = %li new = %li loc = %li\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = call i32 @cmpxchg_reg2reg(i32 0, i32 1, i32* %1)
%4 = load i32, i32* %1
%5 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 0, i32 0, i32 1, i32 %3, i32 %4)
store i32 5, i32* %1
%6 = call i32 @cmpxchg_reg2reg(i32 6, i32 10, i32* %1)
%7 = load i32, i32* %1
%8 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 5, i32 6, i32 10, i32 %6, i32 %7)
%9 = call i32 @xadd_reg2reg(i32 10, i32* %1)
%10 = load i32, i32* %1
%11 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 10, i32 %10)
ret i32 0
}