#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;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, double, ...);
int main() {
  int n, iterations = 900000000;
  struct timespec tStart, tEnd, diff;
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tStart);
  for (n = 0; n < iterations; n++) {
  }
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tEnd);
  if ((tEnd.tv_nsec - tStart.tv_nsec) < 0) {
    diff.tv_sec = tEnd.tv_sec - tStart.tv_sec - 1;
    diff.tv_nsec = 1000000000 + tEnd.tv_nsec - tStart.tv_nsec;
  } else {
    diff.tv_sec = tEnd.tv_sec - tStart.tv_sec;
    diff.tv_nsec = tEnd.tv_nsec - tStart.tv_nsec;
  }
  printf("%ld.%ld seconds for %i interations\n", diff.tv_sec, diff.tv_nsec,
         iterations);
  double pi =
      ((double)diff.tv_sec * ((double)1000000000) + (double)diff.tv_nsec) /
      ((double)iterations);
  printf("%f nanoseconds per interation\n", pi);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x35a4e900,-0xc(%rbp)
mov    0x2eab(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    116e <main+0x4e>
jmp    1160 <main+0x40>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114f <main+0x2f>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0x0,%rax
jge    11b9 <main+0x99>
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x3b9aca00,%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11d1 <main+0xb1>
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
mov    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe23(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
cvtsi2sdq -0x38(%rbp),%xmm0
cvtsi2sdq -0x40(%rbp),%xmm1
movsd  0xe00(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
lea    0xe0a(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    0x2ebc(%rip),%rbx
mov    (%rbx),%edi
lea    0x20(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x10(%rsp),%rcx
mov    0x18(%rsp),%rax
mov    0x20(%rsp),%rdx
mov    0x28(%rsp),%rsi
mov    %rcx,%rbx
sub    %rdx,%rbx
js     1165 <main+0x45>
sub    %rsi,%rax
jmp    1178 <main+0x58>
not    %rsi
add    %rsi,%rax
sub    %rdx,%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,%rbx
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0xe86(%rip),%rdi
mov    %rbx,%rsi
mov    $0x35a4e900,%edx
mov    $0x1,%al
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rbx,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  0xe51(%rip),%xmm1
addsd  %xmm0,%xmm1
divsd  0xe4d(%rip),%xmm1
movapd %xmm1,%xmm0
lea    0xe6e(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@.str = [36 x i8] c"%ld.%ld seconds for %i interations\0A\00"
@.str.1 = [31 x i8] c"%f nanoseconds per interation\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca double
store i32 0, i32* %1
store i32 900000000, i32* %3
%8 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %4)
store i32 0, i32* %2
br label %10
10: ; preds = %15, %0
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %10
br label %15
15: ; preds = %14
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %10
18: ; preds = %10
%19 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%20 = call i32 @clock_gettime(i32 %19, %struct.timespec* %5)
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%24 = load i64, i64* %23
%25 = sub nsw i64 %22, %24
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %42
27: ; preds = %18
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%31 = load i64, i64* %30
%32 = sub nsw i64 %29, %31
%33 = sub nsw i64 %32, 1
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 %33, i64* %34
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%36 = load i64, i64* %35
%37 = add nsw i64 1000000000, %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%39 = load i64, i64* %38
%40 = sub nsw i64 %37, %39
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i64 %40, i64* %41
br label %55
42: ; preds = %18
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%46 = load i64, i64* %45
%47 = sub nsw i64 %44, %46
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 %47, i64* %48
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%52 = load i64, i64* %51
%53 = sub nsw i64 %50, %52
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i64 %53, i64* %54
br label %55
55: ; preds = %42, %27
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%57 = load i64, i64* %56
%58 = sitofp i64 %57 to double
%59 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i32, i32* %3
%62 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %58, i64 %60, i32 %61)
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%64 = load i64, i64* %63
%65 = sitofp i64 %64 to double
%66 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%67 = load i64, i64* %66
%68 = sitofp i64 %67 to double
%69 = call double @llvm.fmuladd.f64(double %65, double 1.000000e+09, double %68)
%70 = load i32, i32* %3
%71 = sitofp i32 %70 to double
%72 = fdiv double %69, %71
store double %72, double* %7
%73 = load double, double* %7
%74 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %73)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
@.str = [36 x i8] c"%ld.%ld seconds for %i interations\0A\00"
@.str.1 = [31 x i8] c"%f nanoseconds per interation\0A\00"
define i32 @main() {
%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_PROCESS_CPUTIME_ID
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %2)
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = sub nsw i64 %10, %12
%14 = icmp slt i64 %13, 0
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%18 = load i64, i64* %17
br i1 %14, label %19, label %24
19: ; preds = %0
%20 = xor i64 %18, -1
%21 = add i64 %16, %20
%22 = add nsw i64 %10, 1000000000
%23 = sub i64 %22, %12
br label %26
24: ; preds = %0
%25 = sub nsw i64 %16, %18
br label %26
26: ; preds = %24, %19
%27 = phi i64 [ %21, %19 ], [ %25, %24 ]
%28 = phi i64 [ %23, %19 ], [ %13, %24 ]
%29 = sitofp i64 %27 to double
%30 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %29, i64 %28, i32 900000000)
%31 = sitofp i64 %28 to double
%32 = call double @llvm.fmuladd.f64(double %29, double 1.000000e+09, double %31)
%33 = fdiv double %32, 9.000000e+08
%34 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %33)
ret i32 0
}