#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 */
typedef int uint64_t;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
void calibrate() {
  uint64_t i, cnt, nsecs;
  struct timespec ts_start, ts_end;
  printf("Calibrating...\n");
  printf("NSEC=%u\n", (1000000000));
  printf("CAL_LOOP=%u\n", (100000000));
  sleep(5);
  clock_gettime(CLOCK_MONOTONIC, &ts_start);
  for (i = 0; i < (100000000); i++)
    ;
  clock_gettime(CLOCK_MONOTONIC, &ts_end);
  nsecs = (ts_end.tv_sec - ts_start.tv_sec) * (1000000000) +
          (ts_end.tv_nsec - ts_start.tv_nsec);
  printf("nsec = %u\n", nsecs);
  cnt = i * (10 * (1000000)) / nsecs;
  printf("cnt: %u\n", cnt);
}


x86 assembly (clang, -O0):
<calibrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xec3(%rip),%rdi
mov    $0x3b9aca00,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb9(%rip),%rdi
mov    $0x5f5e100,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1050 <sleep@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x5f5e100,-0x4(%rbp)
jge    11af <calibrate+0x7f>
jmp    11a1 <calibrate+0x71>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118f <calibrate+0x5f>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
imul   $0x3b9aca00,%eax,%eax
mov    -0x1c(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
imul   $0x989680,-0x4(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate>:
push   %rbx
sub    $0x10,%rsp
lea    0xede(%rip),%rdi
call   1030 <puts@plt>
lea    0xea8(%rip),%rdi
mov    $0x3b9aca00,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x5f5e100,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
mov    0x2e60(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ebx
sub    0x8(%rsp),%eax
imul   $0x3b9aca00,%eax,%eax
sub    0xc(%rsp),%ebx
add    %eax,%ebx
lea    0xe5a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa4c68000,%eax
cltd
idiv   %ebx
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [16 x i8] c"Calibrating...\0A\00"
@.str.1 = [9 x i8] c"NSEC=%u\0A\00"
@.str.2 = [13 x i8] c"CAL_LOOP=%u\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.3 = [11 x i8] c"nsec = %u\0A\00"
@.str.4 = [9 x i8] c"cnt: %u\0A\00"
define void @calibrate() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 1000000000)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 100000000)
%9 = call i32 @sleep(i32 5)
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %4)
store i32 0, i32* %1
br label %12
12: ; preds = %16, %0
%13 = load i32, i32* %1
%14 = icmp slt i32 %13, 100000000
br i1 %14, label %15, label %19
15: ; preds = %12
br label %16
16: ; preds = %15
%17 = load i32, i32* %1
%18 = add nsw i32 %17, 1
store i32 %18, i32* %1
br label %12
19: ; preds = %12
%20 = load i32, i32* @CLOCK_MONOTONIC
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %5)
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sub nsw i32 %23, %25
%27 = mul nsw i32 %26, 1000000000
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = sub nsw i32 %29, %31
%33 = add nsw i32 %27, %32
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %1
%37 = mul nsw i32 %36, 10000000
%38 = load i32, i32* %3
%39 = sdiv i32 %37, %38
store i32 %39, i32* %2
%40 = load i32, i32* %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %40)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str.1 = [9 x i8] c"NSEC=%u\0A\00"
@.str.2 = [13 x i8] c"CAL_LOOP=%u\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.3 = [11 x i8] c"nsec = %u\0A\00"
@.str.4 = [9 x i8] c"cnt: %u\0A\00"
@str = [15 x i8] c"Calibrating...\00"
define void @calibrate() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 1000000000)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 100000000)
%8 = tail call i32 @sleep(i32 5)
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %1)
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %2)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%16 = load i32, i32* %15
%17 = sub nsw i32 %14, %16
%18 = mul nsw i32 %17, 1000000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%22 = load i32, i32* %21
%23 = sub nsw i32 %20, %22
%24 = add nsw i32 %23, %18
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = sdiv i32 -1530494976, %24
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %26)
ret void
}