#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 scalar_t__ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ __CLREX();
scalar_t__ __LDREXH(scalar_t__ volatile *);
int /*<<< orphan*/ __STREXH(scalar_t__, scalar_t__ volatile *);
bool atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue,
                    uint16_t desiredValue) {
  uint16_t currentValue = __LDREXH((volatile uint16_t *)ptr);
  if (currentValue != *expectedCurrentValue) {
    *expectedCurrentValue = currentValue;
    __CLREX();
    return false;
  }
  return !__STREXH(desiredValue, (volatile uint16_t *)ptr);
}


x86 assembly (clang, -O0):
<atomic_cas_u16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <__LDREXH@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     1180 <atomic_cas_u16+0x50>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x0,%al
call   1030 <__CLREX@plt>
movl   $0x0,-0x4(%rbp)
jmp    119d <atomic_cas_u16+0x6d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <__STREXH@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomic_cas_u16>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <__LDREXH@plt>
cmp    (%r15),%rax
jne    115c <atomic_cas_u16+0x2c>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <__STREXH@plt>
xor    %ebx,%ebx
test   %eax,%eax
sete   %bl
jmp    1168 <atomic_cas_u16+0x38>
mov    %rax,(%r15)
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <__CLREX@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @atomic_cas_u16(i64* %0, i64* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
store i64* %0, i64** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%9 = load i64*, i64** %5
%10 = call i64 @__LDREXH(i64* %9)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = load i64*, i64** %6
%13 = load i64, i64* %12
%14 = icmp ne i64 %11, %13
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i64*, i64** %6
store i64 %16, i64* %17
%18 = call i32 (...) @__CLREX()
store i32 0, i32* %4
br label %26
19: ; preds = %3
%20 = load i64, i64* %7
%21 = load i64*, i64** %5
%22 = call i32 @__STREXH(i64 %20, i64* %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
store i32 %25, i32* %4
br label %26
26: ; preds = %19, %15
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
define i32 @atomic_cas_u16(i64* %0, i64* nocapture %1, i64 %2) {
%4 = tail call i64 @__LDREXH(i64* %0)
%5 = load i64, i64* %1
%6 = icmp eq i64 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
store i64 %4, i64* %1
%8 = tail call i32 (...) @__CLREX()
br label %13
9: ; preds = %3
%10 = tail call i32 @__STREXH(i64 %2, i64* %0)
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
br label %13
13: ; preds = %9, %7
%14 = phi i32 [ 0, %7 ], [ %12, %9 ]
ret i32 %14
}