#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 /*<<< orphan*/ FUTEX_WAIT;
int cmpxchg(unsigned long *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sys_futex(unsigned long *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
int xchg(unsigned long *, int);
int mutex_lock(unsigned long *m) {
  int c;
  c = cmpxchg(m, 0, 1);
  if (!c) {
    return 0;
  }
  if (c == 1) {
    c = xchg(m, 2);
  }
  while (c) {
    sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0);
    c = xchg(m, 2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<mutex_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <cmpxchg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1165 <mutex_lock+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11ce <mutex_lock+0x9e>
cmpl   $0x1,-0x14(%rbp)
jne    1180 <mutex_lock+0x50>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1040 <xchg@plt>
mov    %eax,-0x14(%rbp)
jmp    1185 <mutex_lock+0x55>
cmpl   $0x0,-0x14(%rbp)
je     11c7 <mutex_lock+0x97>
mov    -0x10(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1050 <sys_futex@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1040 <xchg@plt>
mov    %eax,-0x14(%rbp)
jmp    1185 <mutex_lock+0x55>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutex_lock>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <cmpxchg@plt>
test   %eax,%eax
je     1199 <mutex_lock+0x69>
cmp    $0x1,%eax
jne    115d <mutex_lock+0x2d>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <xchg@plt>
test   %eax,%eax
je     1199 <mutex_lock+0x69>
mov    0x2e84(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%esi
mov    %rbx,%rdi
mov    $0x2,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <sys_futex@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <xchg@plt>
test   %eax,%eax
jne    1170 <mutex_lock+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@FUTEX_WAIT = global i32 0
define i32 @mutex_lock(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca i32
store i64* %0, i64** %3
%5 = load i64*, i64** %3
%6 = call i32 @cmpxchg(i64* %5, i32 0, i32 1)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %27
10: ; preds = %1
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i64*, i64** %3
%15 = call i32 @xchg(i64* %14, i32 2)
store i32 %15, i32* %4
br label %16
16: ; preds = %13, %10
br label %17
17: ; preds = %20, %16
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i64*, i64** %3
%22 = load i32, i32* @FUTEX_WAIT
%23 = call i32 @sys_futex(i64* %21, i32 %22, i32 2, i32* null, i32* null, i32 0)
%24 = load i64*, i64** %3
%25 = call i32 @xchg(i64* %24, i32 2)
store i32 %25, i32* %4
br label %17
26: ; preds = %17
store i32 0, i32* %2
br label %27
27: ; preds = %26, %9
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@FUTEX_WAIT = global i32 0
define i32 @mutex_lock(i64* %0) {
%2 = tail call i32 @cmpxchg(i64* %0, i32 0, i32 1)
switch i32 %2, label %6 [
i32 0, label %12
i32 1, label %3
]
3: ; preds = %1
%4 = tail call i32 @xchg(i64* %0, i32 2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1, %3
br label %7
7: ; preds = %6, %7
%8 = load i32, i32* @FUTEX_WAIT
%9 = tail call i32 @sys_futex(i64* %0, i32 %8, i32 2, i32* null, i32* null, i32 0)
%10 = tail call i32 @xchg(i64* %0, i32 2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %7
12: ; preds = %7, %3, %1
ret i32 0
}