#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 */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FUTEX_WAIT_PRIVATE;
int /*<<< orphan*/ abort();
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int futex(int *, int /*<<< orphan*/, int, int /*<<< orphan*/ *,
          int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void wait_on_futex_value(int *futex_addr, int val) {
  while (1) {
    int futex_rc = futex(futex_addr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
    if (futex_rc == -1) {
      if (errno != EAGAIN) {
        perror("futex");
        exit(1);
      }
    } else if (futex_rc == 0) {
      if (*futex_addr == val) {
        return;
      }
    } else {
      abort();
    }
  }
}


x86 assembly (clang, -O0):
<wait_on_futex_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rcx
call   1040 <futex@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11b1 <wait_on_futex_value+0x71>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
je     11ac <wait_on_futex_value+0x6c>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11e1 <wait_on_futex_value+0xa1>
cmpl   $0x0,-0x10(%rbp)
jne    11d5 <wait_on_futex_value+0x95>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
jne    11d0 <wait_on_futex_value+0x90>
add    $0x10,%rsp
pop    %rbp
ret
jmp    11dc <wait_on_futex_value+0x9c>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11e1 <wait_on_futex_value+0xa1>
jmp    114f <wait_on_futex_value+0xf>

x86 assembly (clang, -O2):
<wait_on_futex_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e8c(%rip),%r14
mov    0x2e7d(%rip),%r15
mov    0x2e66(%rip),%r12
jmp    1174 <wait_on_futex_value+0x34>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %ebp,(%rbx)
je     11b1 <wait_on_futex_value+0x71>
mov    (%r14),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <futex@plt>
test   %eax,%eax
je     1170 <wait_on_futex_value+0x30>
cmp    $0xffffffff,%eax
jne    11ba <wait_on_futex_value+0x7a>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1174 <wait_on_futex_value+0x34>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@FUTEX_WAIT_PRIVATE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [6 x i8] c"futex\00"
define void @wait_on_futex_value(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %2, %34
%7 = load i32*, i32** %3
%8 = load i32, i32* @FUTEX_WAIT_PRIVATE
%9 = load i32, i32* %4
%10 = call i32 @futex(i32* %7, i32 %8, i32 %9, i32* null, i32* null, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %21
13: ; preds = %6
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
br label %34
21: ; preds = %6
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i32*, i32** %3
%26 = load i32, i32* %25
%27 = load i32, i32* %4
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %24
ret void
30: ; preds = %24
br label %33
31: ; preds = %21
%32 = call i32 (...) @abort()
unreachable
33: ; preds = %30
br label %34
34: ; preds = %33, %20
br label %6
}

LLVM IR (-O2):
@FUTEX_WAIT_PRIVATE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [6 x i8] c"futex\00"
define void @wait_on_futex_value(i32* %0, i32 %1) {
br label %3
3: ; preds = %10, %2
%4 = load i32, i32* @FUTEX_WAIT_PRIVATE
%5 = tail call i32 @futex(i32* %0, i32 %4, i32 %1, i32* null, i32* null, i32 0)
switch i32 %5, label %17 [
i32 -1, label %6
i32 0, label %14
]
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @EAGAIN
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %6, %14
br label %3
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = load i32, i32* %0
%16 = icmp eq i32 %15, %1
br i1 %16, label %19, label %10
17: ; preds = %3
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %14
ret void
}