#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_WAKE;
int /*<<< orphan*/ sys_futex(unsigned long *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
int xchg(unsigned long *, int /*<<< orphan*/);
int mutex_unlock(unsigned long *m) {
  if (*m == 2) {
    *m = 0;
  } else if (xchg(m, 0) == 1) {
    return 0;
  }
  sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0);
  return 0;
}


x86 assembly (clang, -O0):
<mutex_unlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x2,(%rax)
jne    114a <mutex_unlock+0x2a>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    116f <mutex_unlock+0x4f>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <xchg@plt>
cmp    $0x1,%eax
jne    116a <mutex_unlock+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    1198 <mutex_unlock+0x78>
jmp    116f <mutex_unlock+0x4f>
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1040 <sys_futex@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutex_unlock>:
push   %rbx
mov    %rdi,%rbx
cmpq   $0x2,(%rdi)
jne    1133 <mutex_unlock+0x13>
movq   $0x0,(%rbx)
jmp    1142 <mutex_unlock+0x22>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <xchg@plt>
cmp    $0x1,%eax
je     1160 <mutex_unlock+0x40>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <sys_futex@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@FUTEX_WAKE = global i32 0
define i32 @mutex_unlock(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
store i64* %0, i64** %3
%4 = load i64*, i64** %3
%5 = load i64, i64* %4
%6 = icmp eq i64 %5, 2
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i64*, i64** %3
store i64 0, i64* %8
br label %15
9: ; preds = %1
%10 = load i64*, i64** %3
%11 = call i32 @xchg(i64* %10, i32 0)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %19
14: ; preds = %9
br label %15
15: ; preds = %14, %7
%16 = load i64*, i64** %3
%17 = load i32, i32* @FUTEX_WAKE
%18 = call i32 @sys_futex(i64* %16, i32 %17, i32 1, i32* null, i32* null, i32 0)
store i32 0, i32* %2
br label %19
19: ; preds = %15, %13
%20 = load i32, i32* %2
ret i32 %20
}

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