#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 cmpxchg8_base(int *);
int cmpxchg8_esp(int *);
int cmpxchg8_plus8(int *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
main() {
  int arr[4];
  int res;
  arr[0] = 1;
  arr[1] = 1;
  printf("calling cmpxchg with base array\n");
  res = cmpxchg8_base(arr);
  if (res != 1 || arr[0] != 2 || arr[1] != 2) {
    printf("cmpxchg function failed: expected (2,2) got (%d,%d)\n", arr[0],
           arr[1]);
    exit(1);
  }
  printf("cmpxchg function ok (%d,%d)\n", arr[0], arr[1]);
  arr[2] = 1;
  arr[3] = 1;
  printf("calling cmpxchg with base plus 8\n");
  res = cmpxchg8_plus8(arr);
  if (res != 1 || arr[2] != 2 || arr[3] != 2) {
    printf("cmpxchg function failed: expected (2,2) got (%d,%d)\n", arr[2],
           arr[3]);
    exit(1);
  }
  printf("cmpxchg function ok (%d,%d)\n", arr[2], arr[3]);
  arr[2] = 1;
  arr[3] = 1;
  printf("calling cmpxchg with base esp\n");
  res = cmpxchg8_esp(arr);
  if (res != 1) {
    printf("cmpxchg function failed: expected (2,2) got (%d,%d)\n", arr[2],
           arr[3]);
    exit(1);
  }
  printf("cmpxchg function ok\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1050 <cmpxchg8_base@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    11a5 <main+0x55>
cmpl   $0x2,-0x20(%rbp)
jne    11a5 <main+0x55>
cmpl   $0x2,-0x1c(%rbp)
je     11c3 <main+0x73>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1040 <cmpxchg8_plus8@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    121d <main+0xcd>
cmpl   $0x2,-0x18(%rbp)
jne    121d <main+0xcd>
cmpl   $0x2,-0x14(%rbp)
je     123b <main+0xeb>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1060 <cmpxchg8_esp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     129f <main+0x14f>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x100000001,%rbx
mov    %rbx,(%rsp)
lea    0xed8(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1060 <cmpxchg8_base@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
cmp    $0x1,%eax
jne    1242 <main+0xe2>
cmp    $0x2,%esi
jne    1242 <main+0xe2>
cmp    $0x2,%edx
jne    1242 <main+0xe2>
lea    0xe85(%rip),%rdi
mov    $0x2,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,0x8(%rsp)
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1050 <cmpxchg8_plus8@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
cmp    $0x1,%eax
jne    1242 <main+0xe2>
cmp    $0x2,%esi
jne    1242 <main+0xe2>
cmp    $0x2,%edx
jne    1242 <main+0xe2>
lea    0xe3d(%rip),%rdi
mov    $0x2,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,0x8(%rsp)
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1070 <cmpxchg8_esp@plt>
cmp    $0x1,%eax
jne    123a <main+0xda>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"calling cmpxchg with base array\0A\00"
@.str.1 = [53 x i8] c"cmpxchg function failed: expected (2,2) got (%d,%d)\0A\00"
@.str.2 = [29 x i8] c"cmpxchg function ok (%d,%d)\0A\00"
@.str.3 = [34 x i8] c"calling cmpxchg with base plus 8\0A\00"
@.str.4 = [31 x i8] c"calling cmpxchg with base esp\0A\00"
@.str.5 = [21 x i8] c"cmpxchg function ok\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4 x i32]
%3 = alloca i32
store i32 0, i32* %1
%4 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
store i32 1, i32* %4
%5 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
store i32 1, i32* %5
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%8 = call i32 @cmpxchg8_base(i32* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 1
br i1 %10, label %19, label %11
11: ; preds = %0
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 2
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %26
19: ; preds = %15, %11, %0
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %30)
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
store i32 1, i32* %32
%33 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
store i32 1, i32* %33
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%36 = call i32 @cmpxchg8_plus8(i32* %35)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp ne i32 %37, 1
br i1 %38, label %47, label %39
39: ; preds = %26
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%41 = load i32, i32* %40
%42 = icmp ne i32 %41, 2
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%45 = load i32, i32* %44
%46 = icmp ne i32 %45, 2
br i1 %46, label %47, label %54
47: ; preds = %43, %39, %26
%48 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%49 = load i32, i32* %48
%50 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %49, i32 %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %43
%55 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%56 = load i32, i32* %55
%57 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %58)
%60 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
store i32 1, i32* %60
%61 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
store i32 1, i32* %61
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%63 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%64 = call i32 @cmpxchg8_esp(i32* %63)
store i32 %64, i32* %3
%65 = load i32, i32* %3
%66 = icmp ne i32 %65, 1
br i1 %66, label %67, label %74
67: ; preds = %54
%68 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%69 = load i32, i32* %68
%70 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%71 = load i32, i32* %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %69, i32 %71)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %54
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%76 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c"cmpxchg function failed: expected (2,2) got (%d,%d)\0A\00"
@.str.2 = [29 x i8] c"cmpxchg function ok (%d,%d)\0A\00"
@str = [32 x i8] c"calling cmpxchg with base array\00"
@str.6 = [33 x i8] c"calling cmpxchg with base plus 8\00"
@str.7 = [30 x i8] c"calling cmpxchg with base esp\00"
@str.8 = [20 x i8] c"cmpxchg function ok\00"
define i32 @main() {
%1 = alloca [4 x i32]
%2 = bitcast [4 x i32]* %1 to i8*
%3 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
store i32 1, i32* %3
%4 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 1
store i32 1, i32* %4
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%6 = call i32 @cmpxchg8_base(i32* %3)
%7 = icmp ne i32 %6, 1
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 2
%10 = select i1 %7, i1 true, i1 %9
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %11)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 2)
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 2
store i32 1, i32* %19
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 3
store i32 1, i32* %20
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%22 = call i32 @cmpxchg8_plus8(i32* %3)
%23 = icmp ne i32 %22, 1
%24 = load i32, i32* %19
%25 = icmp ne i32 %24, 2
%26 = select i1 %23, i1 true, i1 %25
%27 = load i32, i32* %20
%28 = icmp ne i32 %27, 2
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %27)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %17
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 2)
store i32 1, i32* %19
store i32 1, i32* %20
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%36 = call i32 @cmpxchg8_esp(i32* %3)
%37 = icmp eq i32 %36, 1
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i32, i32* %19
%40 = load i32, i32* %20
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %39, i32 %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
}