#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_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ step_sleep(int);
void measure_sleep() {
  struct timespec start;
  struct timespec stop;
  clock_gettime(CLOCK_REALTIME, &start);
  for (int i = 0; i < 1000; i++) {
    step_sleep(1000);
  }
  clock_gettime(CLOCK_REALTIME, &stop);
  printf("Sleep measurement: %.8f\n", ((stop.tv_sec - start.tv_sec) +
                                       ((stop.tv_nsec - start.tv_nsec) / 1e9)));
}


x86 assembly (clang, -O0):
<measure_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x3e8,-0x24(%rbp)
jge    1176 <measure_sleep+0x46>
mov    $0x3e8,%edi
call   1050 <step_sleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1151 <measure_sleep+0x21>
mov    0x2e63(%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  0xe5c(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_sleep>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2ea3(%rip),%rbx
mov    (%rbx),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x3e8,%ebp
xchg   %ax,%ax
mov    $0x3e8,%edi
call   1050 <step_sleep@plt>
add    $0xffffffff,%ebp
jne    1150 <measure_sleep+0x20>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
movsd  0x8(%rsp),%xmm1
subsd  0x18(%rsp),%xmm1
mov    0x10(%rsp),%eax
sub    0x20(%rsp),%eax
cvtsi2sd %eax,%xmm0
divsd  0xe75(%rip),%xmm0
addsd  %xmm1,%xmm0
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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