#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 /*<<< orphan*/ mtx_t;
typedef int /*<<< orphan*/ atomic_int;

/* Variables and functions */
scalar_t__ atomic_compare_exchange_strong(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *, int);
int /*<<< orphan*/ mtx_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ mtx_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, int);
int cas(void *arg) {
  atomic_int *a;
  mtx_t *m;
  int i, x, y;
  void **l;
  l = (void **)arg;
  a = l[0];
  m = l[1];
  x = y = 0;
  for (i = 0; i < 10000000; i++) {
    if (atomic_compare_exchange_strong(a, a, i)) {
      x++;
    } else {
      y++;
    }
  }
  mtx_lock(m);
  printf("%d %d\n", x, y);
  mtx_unlock(m);
  return 0;
}


x86 assembly (clang, -O0):
<cas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x989680,-0x1c(%rbp)
jge    11d1 <cas+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <atomic_compare_exchange_strong@plt>
cmp    $0x0,%rax
je     11b5 <cas+0x75>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11be <cas+0x7e>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c3 <cas+0x83>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1180 <cas+0x40>
mov    -0x18(%rbp),%rdi
call   1030 <mtx_lock@plt>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <mtx_unlock@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cas>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    (%rdi),%r15
mov    0x8(%rdi),%r14
xor    %ebp,%ebp
xor    %r12d,%r12d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <atomic_compare_exchange_strong@plt>
xor    %ecx,%ecx
xor    %edx,%edx
test   %rax,%rax
sete   %cl
setne  %dl
add    %edx,%ebx
add    %ecx,%r12d
add    $0x1,%ebp
cmp    $0x989680,%ebp
jne    1160 <cas+0x20>
mov    %r14,%rdi
call   1030 <mtx_lock@plt>
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
mov    %r12d,%edx
call   1040 <printf@plt>
mov    %r14,%rdi
call   1050 <mtx_unlock@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%d %d\0A\00"
define i32 @cas(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i8**
store i8** %10, i8*** %8
%11 = load i8**, i8*** %8
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %3
%15 = load i8**, i8*** %8
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = bitcast i8* %17 to i32*
store i32* %18, i32** %4
store i32 0, i32* %7
store i32 0, i32* %6
store i32 0, i32* %5
br label %19
19: ; preds = %35, %1
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 10000000
br i1 %21, label %22, label %38
22: ; preds = %19
%23 = load i32*, i32** %3
%24 = load i32*, i32** %3
%25 = load i32, i32* %5
%26 = call i64 @atomic_compare_exchange_strong(i32* %23, i32* %24, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %34
31: ; preds = %22
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %34
34: ; preds = %31, %28
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %19
38: ; preds = %19
%39 = load i32*, i32** %4
%40 = call i32 @mtx_lock(i32* %39)
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %42)
%44 = load i32*, i32** %4
%45 = call i32 @mtx_unlock(i32* %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d %d\0A\00"
define i32 @cas(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32**
%3 = load i32*, i32** %2
%4 = getelementptr inbounds i8, i8* %0, i64 8
%5 = bitcast i8* %4 to i32**
%6 = load i32*, i32** %5
br label %7
7: ; preds = %1, %7
%8 = phi i32 [ 0, %1 ], [ %18, %7 ]
%9 = phi i32 [ 0, %1 ], [ %17, %7 ]
%10 = phi i32 [ 0, %1 ], [ %15, %7 ]
%11 = tail call i64 @atomic_compare_exchange_strong(i32* %3, i32* %3, i32 %8)
%12 = icmp eq i64 %11, 0
%13 = xor i1 %12, true
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %10, %14
%16 = zext i1 %12 to i32
%17 = add nuw nsw i32 %9, %16
%18 = add nuw nsw i32 %8, 1
%19 = icmp eq i32 %18, 10000000
br i1 %19, label %20, label %7
20: ; preds = %7
%21 = tail call i32 @mtx_lock(i32* %6)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17)
%23 = tail call i32 @mtx_unlock(i32* %6)
ret i32 0
}