#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 timeval {
  long long tv_sec;
  scalar_t__ tv_usec;
  int /*<<< orphan*/ member_0;
};
struct timespec {
  int tv_sec;
  int /*<<< orphan*/ tv_nsec;
};

/* Variables and functions */
int ETIMEDOUT;
int /*<<< orphan*/ FUTEX_WAIT;
int /*<<< orphan*/ SYS_futex;
int errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
char *strerror(int);
int syscall(int /*<<< orphan*/, int *, int /*<<< orphan*/, int /*<<< orphan*/,
            struct timespec *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, const char **argv) {
  int myfutex = 0;
  int ret;
  int futex_errno = 0;
  struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
  struct timeval tv1 = {0};
  struct timeval tv2 = {0};
  printf("invoke futex syscall with a %d second timeout\n", 1);
  if (gettimeofday(&tv1, NULL)) {
    printf("Cannot get time 1: %m\n");
    return 1;
  }
  ret = syscall(SYS_futex, &myfutex, FUTEX_WAIT, 0, &t, NULL, 0);
  futex_errno = errno;
  if (gettimeofday(&tv2, NULL)) {
    printf("Cannot get time 2: %m\n");
    return 1;
  }
  if (ret != -1 || futex_errno != ETIMEDOUT) {
    printf("futex syscall returned: %d with errno: %d (%s)\n", ret, futex_errno,
           strerror(futex_errno));
    return 1;
  }
  long long diff = (tv2.tv_sec - tv1.tv_sec) * 1000000ll;
  diff += tv2.tv_usec - tv1.tv_usec;
  if (diff < 0) {
    printf("Just moved back in time (%lld), better call Ghostbusters!\n", diff);
    return 1;
  }
  if (diff < 1000000ll * 1) {
    printf("Slept for %lld microseconds, which is less than %d seconds\n", diff,
           1);
    return 1;
  }
  puts("futex correctly timed out");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xe75(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
lea    0xe52(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     11f3 <main+0x93>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1e3>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
movl   $0x0,(%rsp)
call   1070 <syscall@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x58(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     1260 <main+0x100>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1e3>
cmpl   $0xffffffff,-0x18(%rbp)
jne    127c <main+0x11c>
mov    -0x1c(%rbp),%eax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
je     12b3 <main+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x68(%rbp),%esi
mov    -0x64(%rbp),%edx
mov    %rax,%rcx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1e3>
mov    -0x58(%rbp),%rax
sub    -0x40(%rbp),%rax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
sub    -0x38(%rbp),%rax
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jge    12ff <main+0x19f>
mov    -0x60(%rbp),%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1e3>
cmpq   $0xf4240,-0x60(%rbp)
jge    1330 <main+0x1d0>
mov    -0x60(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1343 <main+0x1e3>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
movl   $0x0,0x14(%rsp)
movq   $0x1,0x18(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movq   $0x0,0x50(%rsp)
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
lea    0xe71(%rip),%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x40(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     11ba <main+0x6a>
lea    0xe77(%rip),%rdi
jmp    120b <main+0xbb>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
movl   $0x0,(%rsp)
lea    0x14(%rsp),%rsi
lea    0x18(%rsp),%r8
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1060 <syscall@plt>
mov    %eax,%r14d
mov    0x2de7(%rip),%rax
mov    (%rax),%ebp
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     121d <main+0xcd>
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0xffffffff,%r14d
jne    1271 <main+0x121>
mov    0x2da6(%rip),%rax
cmp    (%rax),%ebp
jne    1271 <main+0x121>
mov    0x20(%rsp),%rax
mov    0x28(%rsp),%rsi
sub    0x40(%rsp),%rax
imul   $0xf4240,%rax,%rax
sub    0x48(%rsp),%rsi
add    %rax,%rsi
js     1290 <main+0x140>
cmp    $0xf423f,%rsi
ja     12a3 <main+0x153>
lea    0xe6a(%rip),%rdi
mov    $0x1,%ebx
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1212 <main+0xc2>
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xdde(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1212 <main+0xc2>
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1212 <main+0xc2>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1212 <main+0xc2>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i64, i64, i32 }
@__const.main.t = %struct.timespec { i32 1, i32 0 }
@.str = [47 x i8] c"invoke futex syscall with a %d second timeout\0A\00"
@.str.1 = [23 x i8] c"Cannot get time 1: %m\0A\00"
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@errno = global i32 0
@.str.2 = [23 x i8] c"Cannot get time 2: %m\0A\00"
@ETIMEDOUT = global i32 0
@.str.3 = [48 x i8] c"futex syscall returned: %d with errno: %d (%s)\0A\00"
@.str.4 = [59 x i8] c"Just moved back in time (%lld), better call Ghostbusters!\0A\00"
@.str.5 = [60 x i8] c"Slept for %lld microseconds, which is less than %d seconds\0A\00"
@.str.6 = [26 x i8] c"futex correctly timed out\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timespec
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %8
%13 = bitcast %struct.timespec* %9 to i8*
%14 = bitcast %struct.timeval* %10 to i8*
%15 = bitcast %struct.timeval* %11 to i8*
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 1)
%17 = call i64 @gettimeofday(%struct.timeval* %10, i32* null)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %70
21: ; preds = %2
%22 = load i32, i32* @SYS_futex
%23 = load i32, i32* @FUTEX_WAIT
%24 = call i32 @syscall(i32 %22, i32* %6, i32 %23, i32 0, %struct.timespec* %9, i32* null, i32 0)
store i32 %24, i32* %7
%25 = load i32, i32* @errno
store i32 %25, i32* %8
%26 = call i64 @gettimeofday(%struct.timeval* %11, i32* null)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %70
30: ; preds = %21
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, -1
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* %8
%35 = load i32, i32* @ETIMEDOUT
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %33, %30
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = load i32, i32* %8
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %39, i8* %41)
store i32 1, i32* %3
br label %70
43: ; preds = %33
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%47 = load i64, i64* %46
%48 = sub nsw i64 %45, %47
%49 = mul nsw i64 %48, 1000000
store i64 %49, i64* %12
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%53 = load i64, i64* %52
%54 = sub nsw i64 %51, %53
%55 = load i64, i64* %12
%56 = add nsw i64 %55, %54
store i64 %56, i64* %12
%57 = load i64, i64* %12
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %43
%60 = load i64, i64* %12
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i64 %60)
store i32 1, i32* %3
br label %70
62: ; preds = %43
%63 = load i64, i64* %12
%64 = icmp slt i64 %63, 1000000
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i64, i64* %12
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), i64 %66, i32 1)
store i32 1, i32* %3
br label %70
68: ; preds = %62
%69 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %70
70: ; preds = %68, %65, %59, %37, %28, %19
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64, i32 }
%struct.timespec = type { i32, i32 }
@.str = [47 x i8] c"invoke futex syscall with a %d second timeout\0A\00"
@.str.1 = [23 x i8] c"Cannot get time 1: %m\0A\00"
@SYS_futex = global i32 0
@FUTEX_WAIT = global i32 0
@errno = global i32 0
@.str.2 = [23 x i8] c"Cannot get time 2: %m\0A\00"
@ETIMEDOUT = global i32 0
@.str.3 = [48 x i8] c"futex syscall returned: %d with errno: %d (%s)\0A\00"
@.str.4 = [59 x i8] c"Just moved back in time (%lld), better call Ghostbusters!\0A\00"
@.str.5 = [60 x i8] c"Slept for %lld microseconds, which is less than %d seconds\0A\00"
@.str.6 = [26 x i8] c"futex correctly timed out\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i64* %4 to i8*
store i64 1, i64* %4
%9 = bitcast %struct.timeval* %5 to i8*
%10 = bitcast %struct.timeval* %6 to i8*
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 1)
%12 = call i64 @gettimeofday(%struct.timeval* %5, i32* null)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %56
16: ; preds = %2
%17 = bitcast i64* %4 to %struct.timespec*
%18 = load i32, i32* @SYS_futex
%19 = load i32, i32* @FUTEX_WAIT
%20 = call i32 @syscall(i32 %18, i32* %3, i32 %19, i32 0, %struct.timespec* %17, i32* null, i32 0)
%21 = load i32, i32* @errno
%22 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %56
26: ; preds = %16
%27 = icmp eq i32 %20, -1
%28 = load i32, i32* @ETIMEDOUT
%29 = icmp eq i32 %21, %28
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = call i8* @strerror(i32 %21)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %20, i32 %21, i8* %32)
br label %56
34: ; preds = %26
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%38 = load i64, i64* %37
%39 = sub nsw i64 %36, %38
%40 = mul nsw i64 %39, 1000000
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%44 = load i64, i64* %43
%45 = sub nsw i64 %42, %44
%46 = add nsw i64 %45, %40
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %34
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i64 %46)
br label %56
50: ; preds = %34
%51 = icmp ult i64 %46, 1000000
br i1 %51, label %52, label %54
52: ; preds = %50
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), i64 %46, i32 1)
br label %56
54: ; preds = %50
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %48, %52, %54, %31, %24, %14
%57 = phi i32 [ 1, %14 ], [ 1, %24 ], [ 1, %31 ], [ 1, %48 ], [ 1, %52 ], [ 0, %54 ]
ret i32 %57
}