#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 {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int clock_getcpuclockid(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, long long, double);
int /*<<< orphan*/ timeit();
int main() {
  struct timespec tv_start, tv_end;
  int count = 1 * 1000 * 1000 * 50;
  clockid_t clockid;
  int rv = clock_getcpuclockid(0, &clockid);
  if (rv) {
    perror("clock_getcpuclockid");
    return 1;
  }
  clock_gettime(clockid, &tv_start);
  for (int i = 0; i < count; i++) {
    timeit();
  }
  clock_gettime(clockid, &tv_end);
  long long diff =
      (long long)(tv_end.tv_sec - tv_start.tv_sec) * (1 * 1000 * 1000 * 1000);
  diff += (tv_end.tv_nsec - tv_start.tv_nsec);
  printf("%d cycles in %lld ns = %f ns/cycle\n", count, diff,
         (double)diff / (double)count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2faf080,-0x2c(%rbp)
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_getcpuclockid@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1196 <main+0x46>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1222 <main+0xd2>
mov    -0x30(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11ca <main+0x7a>
mov    $0x0,%al
call   1070 <timeit@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11a9 <main+0x59>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
imul   $0x3b9aca00,%rax,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x40(%rbp),%rdx
cvtsi2sdq -0x40(%rbp),%xmm0
cvtsi2sdl -0x2c(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1050 <clock_getcpuclockid@plt>
test   %eax,%eax
jne    11de <main+0x8e>
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x2faf080,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <timeit@plt>
add    $0xffffffff,%ebx
jne    1180 <main+0x30>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x10(%rsp),%rdx
mov    0x18(%rsp),%rax
sub    0x28(%rsp),%rax
imul   $0x3b9aca00,%rax,%rax
sub    0x20(%rsp),%rdx
add    %rax,%rdx
cvtsi2sd %rdx,%xmm0
divsd  0xe3b(%rip),%xmm0
lea    0xe50(%rip),%rdi
mov    $0x2faf080,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11d8 <main+0x88>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [20 x i8] c"clock_getcpuclockid\00"
@.str.1 = [36 x i8] c"%d cycles in %lld ns = %f ns/cycle\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %1
store i32 50000000, i32* %4
%9 = call i32 @clock_getcpuclockid(i32 0, i32* %5)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %50
14: ; preds = %0
%15 = load i32, i32* %5
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %2)
store i32 0, i32* %7
br label %17
17: ; preds = %23, %14
%18 = load i32, i32* %7
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = call i32 (...) @timeit()
br label %23
23: ; preds = %21
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
br label %17
26: ; preds = %17
%27 = load i32, i32* %5
%28 = call i32 @clock_gettime(i32 %27, %struct.timespec* %3)
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%32 = load i64, i64* %31
%33 = sub nsw i64 %30, %32
%34 = mul nsw i64 %33, 1000000000
store i64 %34, i64* %8
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%38 = load i64, i64* %37
%39 = sub nsw i64 %36, %38
%40 = load i64, i64* %8
%41 = add nsw i64 %40, %39
store i64 %41, i64* %8
%42 = load i32, i32* %4
%43 = load i64, i64* %8
%44 = load i64, i64* %8
%45 = sitofp i64 %44 to double
%46 = load i32, i32* %4
%47 = sitofp i32 %46 to double
%48 = fdiv double %45, %47
%49 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %42, i64 %43, double %48)
store i32 0, i32* %1
br label %50
50: ; preds = %26, %12
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [20 x i8] c"clock_getcpuclockid\00"
@.str.1 = [36 x i8] c"%d cycles in %lld ns = %f ns/cycle\0A\00"
define i32 @main() {
%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 = bitcast i32* %3 to i8*
%7 = call i32 @clock_getcpuclockid(i32 0, i32* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %37
11: ; preds = %0
%12 = load i32, i32* %3
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %1)
br label %32
14: ; preds = %32
%15 = load i32, i32* %3
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %2)
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%20 = load i64, i64* %19
%21 = sub nsw i64 %18, %20
%22 = mul nsw i64 %21, 1000000000
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%26 = load i64, i64* %25
%27 = sub nsw i64 %24, %26
%28 = add nsw i64 %27, %22
%29 = sitofp i64 %28 to double
%30 = fdiv double %29, 5.000000e+07
%31 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 50000000, i64 %28, double %30)
br label %37
32: ; preds = %11, %32
%33 = phi i32 [ 0, %11 ], [ %35, %32 ]
%34 = call i32 (...) @timeit()
%35 = add nuw nsw i32 %33, 1
%36 = icmp eq i32 %35, 50000000
br i1 %36, label %14, label %32
37: ; preds = %14, %9
%38 = phi i32 [ 1, %9 ], [ 0, %14 ]
ret i32 %38
}