#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 */
struct timespec {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FUTEX_WAIT;
int /*<<< orphan*/ SYS_futex;
scalar_t__ errno;
int syscall(int /*<<< orphan*/, int *, int /*<<< orphan*/, int,
            struct timespec *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int futex_wait(int *futex, int val, struct timespec *ts) {
  int err = syscall(SYS_futex, futex, FUTEX_WAIT, val, ts, NULL, 0);
  if (err == -1) {
    if (errno == EWOULDBLOCK) {
      return 0;
    } else if (errno == ETIMEDOUT) {
      return -2;
    } else if (errno == EINTR) {
      return -3;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<futex_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2eb1(%rip),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
xor    %eax,%eax
movl   $0x0,(%rsp)
call   1030 <syscall@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e1 <futex_wait+0xd1>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <futex_wait+0x76>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <futex_wait+0xd7>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
jne    11ac <futex_wait+0x9c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e7 <futex_wait+0xd7>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <futex_wait+0xc2>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11e7 <futex_wait+0xd7>
jmp    11d7 <futex_wait+0xc7>
jmp    11dc <futex_wait+0xcc>
jmp    11e1 <futex_wait+0xd1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<futex_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r8
mov    %esi,%ecx
mov    %rdi,%rsi
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2ebb(%rip),%rax
mov    (%rax),%edx
movl   $0x0,(%rsp)
xor    %ebx,%ebx
xor    %r9d,%r9d
call   1030 <syscall@plt>
cmp    $0xffffffff,%eax
je     1149 <futex_wait+0x39>
mov    %eax,%ebx
jmp    1181 <futex_wait+0x71>
mov    0x2e88(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e76(%rip),%rdx
cmp    (%rdx),%rcx
je     1181 <futex_wait+0x71>
mov    0x2e62(%rip),%rdx
mov    $0xfffffffe,%ebx
cmp    (%rdx),%rcx
je     1181 <futex_wait+0x71>
mov    0x2e49(%rip),%rdx
mov    $0xfffffffd,%ebx
cmp    (%rdx),%rcx
jne    1145 <futex_wait+0x35>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@ETIMEDOUT = global i64 0
@EINTR = global i64 0
define i32 @futex_wait(i32* %0, i32 %1, %struct.timespec* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.timespec*
%8 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store %struct.timespec* %2, %struct.timespec** %7
%9 = load i32, i32* @SYS_futex
%10 = load i32*, i32** %5
%11 = load i32, i32* @FUTEX_WAIT
%12 = load i32, i32* %6
%13 = load %struct.timespec*, %struct.timespec** %7
%14 = call i32 @syscall(i32 %9, i32* %10, i32 %11, i32 %12, %struct.timespec* %13, i32* null, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %35
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 0, i32* %4
br label %37
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @ETIMEDOUT
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -2, i32* %4
br label %37
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -3, i32* %4
br label %37
32: ; preds = %27
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34, %3
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %31, %26, %21
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@ETIMEDOUT = global i64 0
@EINTR = global i64 0
define i32 @futex_wait(i32* %0, i32 %1, %struct.timespec* %2) {
%4 = load i32, i32* @SYS_futex
%5 = load i32, i32* @FUTEX_WAIT
%6 = tail call i32 @syscall(i32 %4, i32* %0, i32 %5, i32 %1, %struct.timespec* %2, i32* null, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %18
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @EWOULDBLOCK
%11 = icmp eq i64 %9, %10
br i1 %11, label %19, label %12
12: ; preds = %8
%13 = load i64, i64* @ETIMEDOUT
%14 = icmp eq i64 %9, %13
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %9, %16
br i1 %17, label %19, label %18
18: ; preds = %15, %3
br label %19
19: ; preds = %15, %12, %8, %18
%20 = phi i32 [ %6, %18 ], [ 0, %8 ], [ -2, %12 ], [ -3, %15 ]
ret i32 %20
}