#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 */
unsigned int irq_disable();
int /*<<< orphan*/ irq_restore(unsigned int);
scalar_t__ memcmp(void *, void *, size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
bool __atomic_compare_exchange_c(size_t len, void *ptr, void *expected,
                                 void *desired, bool weak, int success_memorder,
                                 int failure_memorder) {
  (void)weak;
  (void)success_memorder;
  (void)failure_memorder;
  unsigned int mask = irq_disable();
  bool ret;
  if (memcmp(ptr, expected, len) == 0) {
    memcpy(ptr, desired, len);
    ret = true;
  } else {
    memcpy(expected, ptr, len);
    ret = false;
  }
  irq_restore(mask);
  return ret;
}


x86 assembly (clang, -O0):
<__atomic_compare_exchange_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
mov    $0x0,%al
call   1040 <irq_disable@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    11a5 <__atomic_compare_exchange_c+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1050 <memcpy@plt>
movl   $0x1,-0x30(%rbp)
jmp    11bd <__atomic_compare_exchange_c+0x7d>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <irq_restore@plt>
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__atomic_compare_exchange_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <irq_disable@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1187 <__atomic_compare_exchange_c+0x47>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
jmp    119b <__atomic_compare_exchange_c+0x5b>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
mov    $0x1,%r15d
mov    %r14d,%edi
call   1060 <irq_restore@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @__atomic_compare_exchange_c(i64 %0, i8* %1, i8* %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i64 %0, i64* %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 %6, i32* %14
%17 = load i32, i32* %12
%18 = load i32, i32* %13
%19 = load i32, i32* %14
%20 = call i32 (...) @irq_disable()
store i32 %20, i32* %15
%21 = load i8*, i8** %9
%22 = load i8*, i8** %10
%23 = load i64, i64* %8
%24 = call i64 @memcmp(i8* %21, i8* %22, i64 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %7
%27 = load i8*, i8** %9
%28 = load i8*, i8** %11
%29 = load i64, i64* %8
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
store i32 1, i32* %16
br label %36
31: ; preds = %7
%32 = load i8*, i8** %10
%33 = load i8*, i8** %9
%34 = load i64, i64* %8
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
store i32 0, i32* %16
br label %36
36: ; preds = %31, %26
%37 = load i32, i32* %15
%38 = call i32 @irq_restore(i32 %37)
%39 = load i32, i32* %16
ret i32 %39
}

LLVM IR (-O2):
define i32 @__atomic_compare_exchange_c(i64 %0, i8* %1, i8* %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = tail call i32 (...) @irq_disable()
%9 = tail call i64 @memcmp(i8* %1, i8* %2, i64 %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @memcpy(i8* %1, i8* %3, i64 %0)
br label %15
13: ; preds = %7
%14 = tail call i32 @memcpy(i8* %2, i8* %1, i64 %0)
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ 1, %11 ], [ 0, %13 ]
%17 = tail call i32 @irq_restore(i32 %8)
ret i32 %16
}