#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 {
  double tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ FUTEX_WAKE;
int /*<<< orphan*/ __NR_futex;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, unsigned int *,
                           int /*<<< orphan*/, int, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *, int /*<<< orphan*/);
int test_futex(void) {
  struct timespec ts_start, ts_end;
  unsigned long i = 100000000;
  clock_gettime(CLOCK_MONOTONIC, &ts_start);
  while (i--) {
    unsigned int addr = 0;
    syscall(__NR_futex, &addr, FUTEX_WAKE, 1, NULL, NULL, 0);
  }
  clock_gettime(CLOCK_MONOTONIC, &ts_end);
  printf("time = %.6f\n", ts_end.tv_sec - ts_start.tv_sec +
                              (ts_end.tv_nsec - ts_start.tv_nsec) / 1e9);
  return 0;
}


x86 assembly (clang, -O0):
<test_futex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x5f5e100,-0x28(%rbp)
05
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
cmp    $0x0,%rax
je     11a8 <test_futex+0x78>
movl   $0x0,-0x2c(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rsi
mov    $0x1,%ecx
xor    %eax,%eax
mov    %eax,%r9d
xor    %eax,%eax
mov    %r9,%r8
movl   $0x0,(%rsp)
call   1050 <syscall@plt>
jmp    1152 <test_futex+0x22>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
movsd  -0x20(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
mov    -0x18(%rbp),%eax
sub    -0x8(%rbp),%eax
cvtsi2sd %eax,%xmm1
movsd  0xe2a(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_futex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e9c(%rip),%r14
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0xfffffffffa0a1f00,%rbx
mov    0x2e89(%rip),%r12
mov    0x2e62(%rip),%r13
lea    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movl   $0x0,0x10(%rsp)
mov    (%r12),%edi
mov    0x0(%r13),%edx
movl   $0x0,(%rsp)
mov    %r15,%rsi
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <syscall@plt>
inc    %rbx
jne    1170 <test_futex+0x40>
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
movsd  0x10(%rsp),%xmm1
subsd  0x20(%rsp),%xmm1
mov    0x18(%rsp),%eax
sub    0x28(%rsp),%eax
cvtsi2sd %eax,%xmm0
divsd  0xe34(%rip),%xmm0
addsd  %xmm1,%xmm0
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { double, i32 }
@CLOCK_MONOTONIC = global i32 0
@__NR_futex = global i32 0
@FUTEX_WAKE = global i32 0
@.str = [13 x i8] c"time = %.6f\0A\00"
define i32 @test_futex() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca i64
%4 = alloca i32
store i64 100000000, i64* %3
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %1)
br label %7
7: ; preds = %11, %0
%8 = load i64, i64* %3
%9 = add i64 %8, -1
store i64 %9, i64* %3
%10 = icmp ne i64 %8, 0
br i1 %10, label %11, label %15
11: ; preds = %7
store i32 0, i32* %4
%12 = load i32, i32* @__NR_futex
%13 = load i32, i32* @FUTEX_WAKE
%14 = call i32 @syscall(i32 %12, i32* %4, i32 %13, i32 1, i32* null, i32* null, i32 0)
br label %7
15: ; preds = %7
%16 = load i32, i32* @CLOCK_MONOTONIC
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %2)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%19 = load double, double* %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%21 = load double, double* %20
%22 = fsub double %19, %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%26 = load i32, i32* %25
%27 = sub nsw i32 %24, %26
%28 = sitofp i32 %27 to double
%29 = fdiv double %28, 1.000000e+09
%30 = fadd double %22, %29
%31 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), double %30)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { double, i32 }
@CLOCK_MONOTONIC = global i32 0
@__NR_futex = global i32 0
@FUTEX_WAKE = global i32 0
@.str = [13 x i8] c"time = %.6f\0A\00"
define i32 @test_futex() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %1)
%8 = bitcast i32* %3 to i8*
br label %9
9: ; preds = %0, %9
%10 = phi i64 [ 100000000, %0 ], [ %11, %9 ]
%11 = add nsw i64 %10, -1
store i32 0, i32* %3
%12 = load i32, i32* @__NR_futex
%13 = load i32, i32* @FUTEX_WAKE
%14 = call i32 @syscall(i32 %12, i32* %3, i32 %13, i32 1, i32* null, i32* null, i32 0)
%15 = icmp eq i64 %11, 0
br i1 %15, label %16, label %9
16: ; preds = %9
%17 = load i32, i32* @CLOCK_MONOTONIC
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %2)
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%20 = load double, double* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%22 = load double, double* %21
%23 = fsub double %20, %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%27 = load i32, i32* %26
%28 = sub nsw i32 %25, %27
%29 = sitofp i32 %28 to double
%30 = fdiv double %29, 1.000000e+09
%31 = fadd double %23, %30
%32 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), double %31)
ret i32 0
}