#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*/ getuid();
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++) {
    getuid();
  }
  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    0x2e9b(%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    1180 <main+0x50>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115f <main+0x2f>
mov    0x2e61(%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    11cb <main+0x9b>
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    11e3 <main+0xb3>
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    0xe11(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
cvtsi2sdq -0x38(%rbp),%xmm0
cvtsi2sdq -0x40(%rbp),%xmm1
movsd  0xdee(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
lea    0xdf8(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2eab(%rip),%rbx
mov    (%rbx),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x35a4e900,%ebp
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getuid@plt>
add    $0xffffffff,%ebp
jne    1150 <main+0x20>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rcx
mov    0x10(%rsp),%rax
mov    0x18(%rsp),%rdx
mov    0x20(%rsp),%rsi
mov    %rcx,%rbx
sub    %rdx,%rbx
js     1189 <main+0x59>
sub    %rsi,%rax
jmp    119c <main+0x6c>
not    %rsi
add    %rsi,%rax
sub    %rdx,%rcx
add    $0x3b9aca00,%rcx
mov    %rcx,%rbx
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
mov    $0x35a4e900,%edx
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rbx,%xmm0
movsd  (%rsp),%xmm1
mulsd  0xe2f(%rip),%xmm1
addsd  %xmm0,%xmm1
divsd  0xe2b(%rip),%xmm1
movapd %xmm1,%xmm0
lea    0xe4c(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
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 = %16, %0
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = call i32 (...) @getuid()
br label %16
16: ; preds = %14
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
br label %10
19: ; preds = %10
%20 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %5)
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%25 = load i64, i64* %24
%26 = sub nsw i64 %23, %25
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %19
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%32 = load i64, i64* %31
%33 = sub nsw i64 %30, %32
%34 = sub nsw i64 %33, 1
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 %34, i64* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%37 = load i64, i64* %36
%38 = add nsw i64 1000000000, %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%40 = load i64, i64* %39
%41 = sub nsw i64 %38, %40
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i64 %41, i64* %42
br label %56
43: ; preds = %19
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%47 = load i64, i64* %46
%48 = sub nsw i64 %45, %47
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 %48, i64* %49
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%53 = load i64, i64* %52
%54 = sub nsw i64 %51, %53
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i64 %54, i64* %55
br label %56
56: ; preds = %43, %28
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%58 = load i64, i64* %57
%59 = sitofp i64 %58 to double
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i32, i32* %3
%63 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %59, i64 %61, i32 %62)
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%65 = load i64, i64* %64
%66 = sitofp i64 %65 to double
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%68 = load i64, i64* %67
%69 = sitofp i64 %68 to double
%70 = call double @llvm.fmuladd.f64(double %66, double 1.000000e+09, double %69)
%71 = load i32, i32* %3
%72 = sitofp i32 %71 to double
%73 = fdiv double %70, %72
store double %73, double* %7
%74 = load double, double* %7
%75 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %74)
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)
br label %7
7: ; preds = %0, %7
%8 = phi i32 [ 0, %0 ], [ %10, %7 ]
%9 = call i32 (...) @getuid()
%10 = add nuw nsw i32 %8, 1
%11 = icmp eq i32 %10, 900000000
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %2)
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%16 = load i64, i64* %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%18 = load i64, i64* %17
%19 = sub nsw i64 %16, %18
%20 = icmp slt i64 %19, 0
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%24 = load i64, i64* %23
br i1 %20, label %25, label %30
25: ; preds = %12
%26 = xor i64 %24, -1
%27 = add i64 %22, %26
%28 = add nsw i64 %16, 1000000000
%29 = sub i64 %28, %18
br label %32
30: ; preds = %12
%31 = sub nsw i64 %22, %24
br label %32
32: ; preds = %30, %25
%33 = phi i64 [ %27, %25 ], [ %31, %30 ]
%34 = phi i64 [ %29, %25 ], [ %19, %30 ]
%35 = sitofp i64 %33 to double
%36 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %35, i64 %34, i32 900000000)
%37 = sitofp i64 %34 to double
%38 = call double @llvm.fmuladd.f64(double %35, double 1.000000e+09, double %37)
%39 = fdiv double %38, 9.000000e+08
%40 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %39)
ret i32 0
}