#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ compare_and_swap(int *, int, int);
int /*<<< orphan*/ fetch_and_add(int *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ test_and_set(int *);
int main(void) {
  int i = 0;
  printf("1: test_and_set, i = 0\n");
  if (!test_and_set(&i) || i != 1) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  printf("2: test_and_set, i != 0\n");
  if (test_and_set(&i) || i != 1) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  printf("3: fetch_and_add, i = 1 add 41\n");
  fetch_and_add(&i, 41);
  if (i != 42) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  printf("4: fetch_and_add, i = 42 add -41\n");
  fetch_and_add(&i, -41);
  if (i != 1) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  printf("5: compare_and_swap, i = 1 old = 1 new = 42\n");
  if (!compare_and_swap(&i, 1, 42) || i != 42) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  printf("6: compare_and_swap, i = 42 old = 1 new = 5\n");
  if (compare_and_swap(&i, 1, 5) || i != 42) {
    printf("\tfailed, i = %d\n", i);
    return EXIT_FAILURE;
  }
  printf("\tsuccess i = %d\n\n", i);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1040 <test_and_set@plt>
cmp    $0x0,%rax
je     1181 <main+0x41>
cmpl   $0x1,-0x8(%rbp)
je     11a3 <main+0x63>
mov    -0x8(%rbp),%esi
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1040 <test_and_set@plt>
cmp    $0x0,%rax
jne    11df <main+0x9f>
cmpl   $0x1,-0x8(%rbp)
je     1201 <main+0xc1>
mov    -0x8(%rbp),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    $0x29,%esi
call   1030 <fetch_and_add@plt>
cmpl   $0x2a,-0x8(%rbp)
je     125a <main+0x11a>
mov    -0x8(%rbp),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    $0xffffffd7,%esi
call   1030 <fetch_and_add@plt>
cmpl   $0x1,-0x8(%rbp)
je     12b3 <main+0x173>
mov    -0x8(%rbp),%esi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2a,%edx
call   1060 <compare_and_swap@plt>
cmp    $0x0,%rax
je     12f9 <main+0x1b9>
cmpl   $0x2a,-0x8(%rbp)
je     131b <main+0x1db>
mov    -0x8(%rbp),%esi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   1060 <compare_and_swap@plt>
cmp    $0x0,%rax
jne    1361 <main+0x221>
cmpl   $0x2a,-0x8(%rbp)
je     1383 <main+0x243>
mov    -0x8(%rbp),%esi
lea    0xcad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a0 <main+0x260>
mov    -0x8(%rbp),%esi
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
movl   $0x0,0x4(%rsp)
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1050 <test_and_set@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
je     12d8 <main+0x188>
cmp    $0x1,%esi
jne    12d8 <main+0x188>
lea    0xe85(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1050 <test_and_set@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
jne    12d8 <main+0x188>
cmp    $0x1,%esi
jne    12d8 <main+0x188>
lea    0xe46(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0x29,%esi
call   1040 <fetch_and_add@plt>
mov    0x4(%rsp),%esi
cmp    $0x2a,%esi
jne    12d8 <main+0x188>
lea    0xe0b(%rip),%rdi
mov    $0x2a,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0xffffffd7,%esi
call   1040 <fetch_and_add@plt>
mov    0x4(%rsp),%esi
cmp    $0x1,%esi
jne    12d8 <main+0x188>
lea    0xdd0(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2a,%edx
call   1070 <compare_and_swap@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
je     12d8 <main+0x188>
cmp    $0x2a,%esi
jne    12d8 <main+0x188>
lea    0xd8f(%rip),%rdi
mov    $0x2a,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   1070 <compare_and_swap@plt>
mov    0x4(%rsp),%esi
test   %rax,%rax
jne    12d8 <main+0x188>
cmp    $0x2a,%esi
jne    12d8 <main+0x188>
lea    0xd4e(%rip),%rdi
mov    $0x2a,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d02(%rip),%rax
jmp    12ed <main+0x19d>
lea    0xd21(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"1: test_and_set, i = 0\0A\00"
@.str.1 = [17 x i8] c"\09failed, i = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"\09success i = %d\0A\0A\00"
@.str.3 = [25 x i8] c"2: test_and_set, i != 0\0A\00"
@.str.4 = [32 x i8] c"3: fetch_and_add, i = 1 add 41\0A\00"
@.str.5 = [34 x i8] c"4: fetch_and_add, i = 42 add -41\0A\00"
@.str.6 = [45 x i8] c"5: compare_and_swap, i = 1 old = 1 new = 42\0A\00"
@.str.7 = [45 x i8] c"6: compare_and_swap, i = 42 old = 1 new = 5\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%4 = call i64 @test_and_set(i32* %2)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 1
br i1 %8, label %9, label %13
9: ; preds = %6, %0
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %1
br label %78
13: ; preds = %6
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%17 = call i64 @test_and_set(i32* %2)
%18 = icmp ne i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %19, %13
%23 = load i32, i32* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %1
br label %78
26: ; preds = %19
%27 = load i32, i32* %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @fetch_and_add(i32* %2, i32 41)
%31 = load i32, i32* %2
%32 = icmp ne i32 %31, 42
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = load i32, i32* %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %1
br label %78
37: ; preds = %26
%38 = load i32, i32* %2
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @fetch_and_add(i32* %2, i32 -41)
%42 = load i32, i32* %2
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load i32, i32* %2
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = load i32, i32* @EXIT_FAILURE
store i32 %47, i32* %1
br label %78
48: ; preds = %37
%49 = load i32, i32* %2
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%52 = call i64 @compare_and_swap(i32* %2, i32 1, i32 42)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* %2
%56 = icmp ne i32 %55, 42
br i1 %56, label %57, label %61
57: ; preds = %54, %48
%58 = load i32, i32* %2
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = load i32, i32* @EXIT_FAILURE
store i32 %60, i32* %1
br label %78
61: ; preds = %54
%62 = load i32, i32* %2
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.7, i64 0, i64 0))
%65 = call i64 @compare_and_swap(i32* %2, i32 1, i32 5)
%66 = icmp ne i64 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %61
%68 = load i32, i32* %2
%69 = icmp ne i32 %68, 42
br i1 %69, label %70, label %74
70: ; preds = %67, %61
%71 = load i32, i32* %2
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %71)
%73 = load i32, i32* @EXIT_FAILURE
store i32 %73, i32* %1
br label %78
74: ; preds = %67
%75 = load i32, i32* %2
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %75)
%77 = load i32, i32* @EXIT_SUCCESS
store i32 %77, i32* %1
br label %78
78: ; preds = %74, %70, %57, %44, %33, %22, %9
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"\09failed, i = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"\09success i = %d\0A\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"1: test_and_set, i = 0\00"
@str.8 = [24 x i8] c"2: test_and_set, i != 0\00"
@str.9 = [31 x i8] c"3: fetch_and_add, i = 1 add 41\00"
@str.10 = [33 x i8] c"4: fetch_and_add, i = 42 add -41\00"
@str.11 = [44 x i8] c"5: compare_and_swap, i = 1 old = 1 new = 42\00"
@str.12 = [44 x i8] c"6: compare_and_swap, i = 42 old = 1 new = 5\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%4 = call i64 @test_and_set(i32* %1)
%5 = icmp eq i64 %4, 0
%6 = load i32, i32* %1
%7 = icmp ne i32 %6, 1
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %59
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 1)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%14 = call i64 @test_and_set(i32* %1)
%15 = icmp ne i64 %14, 0
%16 = load i32, i32* %1
%17 = icmp ne i32 %16, 1
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %59
21: ; preds = %11
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 1)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0))
%24 = call i32 @fetch_and_add(i32* %1, i32 41)
%25 = load i32, i32* %1
%26 = icmp eq i32 %25, 42
br i1 %26, label %29, label %27
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %59
29: ; preds = %21
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 42)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.10, i64 0, i64 0))
%32 = call i32 @fetch_and_add(i32* %1, i32 -41)
%33 = load i32, i32* %1
%34 = icmp eq i32 %33, 1
br i1 %34, label %37, label %35
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %33)
br label %59
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 1)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.11, i64 0, i64 0))
%40 = call i64 @compare_and_swap(i32* %1, i32 1, i32 42)
%41 = icmp eq i64 %40, 0
%42 = load i32, i32* %1
%43 = icmp ne i32 %42, 42
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %59
47: ; preds = %37
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 42)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.12, i64 0, i64 0))
%50 = call i64 @compare_and_swap(i32* %1, i32 1, i32 5)
%51 = icmp ne i64 %50, 0
%52 = load i32, i32* %1
%53 = icmp ne i32 %52, 42
%54 = select i1 %51, i1 true, i1 %53
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %52)
br label %59
57: ; preds = %47
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 42)
br label %59
59: ; preds = %57, %55, %45, %35, %27, %19, %9
%60 = phi i32* [ @EXIT_FAILURE, %9 ], [ @EXIT_FAILURE, %19 ], [ @EXIT_FAILURE, %27 ], [ @EXIT_FAILURE, %35 ], [ @EXIT_FAILURE, %45 ], [ @EXIT_FAILURE, %55 ], [ @EXIT_SUCCESS, %57 ]
%61 = load i32, i32* %60
ret i32 %61
}