#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 uint32_t;
typedef int /*<<< orphan*/ atomic_size_t;

/* Variables and functions */
scalar_t__ UINT16_MAX;
int /*<<< orphan*/ atomic_compare_exchange_strong_explicit(int /*<<< orphan*/ *,
                                                           size_t *, size_t,
                                                           int /*<<< orphan*/,
                                                           int /*<<< orphan*/);
size_t atomic_load_explicit(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ memory_order_relaxed;
void *adder_cmpxchg(void *arg) {
  size_t step;
  atomic_size_t *counter = arg;
  for (step = 0; step < ((uint32_t)UINT16_MAX * 4); ++step) {
    size_t xchg;
    size_t cmp = atomic_load_explicit(counter, memory_order_relaxed);
    do {
      xchg = (cmp + 1);
    } while (!atomic_compare_exchange_strong_explicit(
        counter, &cmp, xchg, memory_order_relaxed, memory_order_relaxed));
  }
  return NULL;
}


x86 assembly (clang, -O0):
<adder_cmpxchg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%rcx
shl    $0x2,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    11c5 <adder_cmpxchg+0xa5>
mov    -0x18(%rbp),%rdi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
call   1040 <atomic_load_explicit@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    (%rax),%r8d
lea    -0x28(%rbp),%rsi
call   1030 <atomic_compare_exchange_strong_explicit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    116f <adder_cmpxchg+0x4f>
jmp    11b4 <adder_cmpxchg+0x94>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    113c <adder_cmpxchg+0x1c>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adder_cmpxchg>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2ea4(%rip),%r15
cmpl   $0x0,(%r15)
je     11a1 <adder_cmpxchg+0x81>
mov    %rdi,%rbx
xor    %r12d,%r12d
mov    0x2ea1(%rip),%r13
lea    0x8(%rsp),%r14
jmp    1161 <adder_cmpxchg+0x41>
xchg   %ax,%ax
add    $0x1,%r12
mov    (%r15),%eax
shl    $0x2,%eax
cltq
cmp    %rax,%r12
jae    11a1 <adder_cmpxchg+0x81>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1040 <atomic_load_explicit@plt>
mov    %rax,0x8(%rsp)
lea    0x1(%rax),%rdx
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ecx,%r8d
call   1030 <atomic_compare_exchange_strong_explicit@plt>
test   %eax,%eax
jne    1150 <adder_cmpxchg+0x30>
mov    0x8(%rsp),%rdx
add    $0x1,%rdx
jmp    1180 <adder_cmpxchg+0x60>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@UINT16_MAX = global i64 0
@memory_order_relaxed = global i32 0
define i8* @adder_cmpxchg(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %4
store i64 0, i64* %3
br label %9
9: ; preds = %32, %1
%10 = load i64, i64* %3
%11 = load i64, i64* @UINT16_MAX
%12 = trunc i64 %11 to i32
%13 = mul nsw i32 %12, 4
%14 = sext i32 %13 to i64
%15 = icmp ult i64 %10, %14
br i1 %15, label %16, label %35
16: ; preds = %9
%17 = load i32*, i32** %4
%18 = load i32, i32* @memory_order_relaxed
%19 = call i64 @atomic_load_explicit(i32* %17, i32 %18)
store i64 %19, i64* %6
br label %20
20: ; preds = %23, %16
%21 = load i64, i64* %6
%22 = add i64 %21, 1
store i64 %22, i64* %5
br label %23
23: ; preds = %20
%24 = load i32*, i32** %4
%25 = load i64, i64* %5
%26 = load i32, i32* @memory_order_relaxed
%27 = load i32, i32* @memory_order_relaxed
%28 = call i32 @atomic_compare_exchange_strong_explicit(i32* %24, i64* %6, i64 %25, i32 %26, i32 %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %20, label %31
31: ; preds = %23
br label %32
32: ; preds = %31
%33 = load i64, i64* %3
%34 = add i64 %33, 1
store i64 %34, i64* %3
br label %9
35: ; preds = %9
ret i8* null
}

LLVM IR (-O2):
@UINT16_MAX = global i64 0
@memory_order_relaxed = global i32 0
define noalias i8* @adder_cmpxchg(i8* %0) {
%2 = alloca i64
%3 = bitcast i8* %0 to i32*
%4 = load i64, i64* @UINT16_MAX
%5 = trunc i64 %4 to i32
%6 = icmp eq i32 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = bitcast i64* %2 to i8*
br label %9
9: ; preds = %7, %23
%10 = phi i64 [ 0, %7 ], [ %24, %23 ]
%11 = load i32, i32* @memory_order_relaxed
%12 = call i64 @atomic_load_explicit(i32* %3, i32 %11)
store i64 %12, i64* %2
%13 = add i64 %12, 1
%14 = load i32, i32* @memory_order_relaxed
%15 = call i32 @atomic_compare_exchange_strong_explicit(i32* %3, i64* %2, i64 %13, i32 %14, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %9, %17
%18 = load i64, i64* %2
%19 = add i64 %18, 1
%20 = load i32, i32* @memory_order_relaxed
%21 = call i32 @atomic_compare_exchange_strong_explicit(i32* %3, i64* %2, i64 %19, i32 %20, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %17, label %23
23: ; preds = %17, %9
%24 = add nuw i64 %10, 1
%25 = load i64, i64* @UINT16_MAX
%26 = trunc i64 %25 to i32
%27 = shl nsw i32 %26, 2
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %24, %28
br i1 %29, label %9, label %30
30: ; preds = %23, %1
ret i8* null
}