#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 {
  unsigned long long tv_nsec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ printf(char *, unsigned long long);
int main(int argc, char *argv[]) {
  struct timespec start, stop, useless;
  int i;
  unsigned n = 1000000;
  unsigned long long t1, t2;
  clock_gettime(CLOCK_MONOTONIC, &start);
  for (i = 0; i < n; i++)
    clock_gettime(CLOCK_MONOTONIC, &useless);
  clock_gettime(CLOCK_MONOTONIC, &stop);
  t1 = start.tv_nsec;
  t1 += start.tv_sec * 1000000000;
  t2 = stop.tv_nsec;
  t2 += stop.tv_sec * 1000000000;
  printf("RESULT : clock_gettime() %Lu ns\n", (t2 - t1) / n);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf4240,-0x48(%rbp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jae    1182 <main+0x62>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1156 <main+0x36>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
imul   $0x3b9aca00,-0x18(%rbp),%eax
cltq
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
imul   $0x3b9aca00,-0x28(%rbp),%eax
cltq
add    -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x50(%rbp),%rax
mov    -0x48(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rsi
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2eb9(%rip),%r14
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0xf4240,%ebp
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
add    $0xffffffff,%ebp
jne    1150 <main+0x30>
mov    (%r14),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
movslq 0x18(%rsp),%rax
imul   $0x3b9aca00,%rax,%rcx
mov    (%rsp),%rax
movslq 0x8(%rsp),%rdx
imul   $0x3b9aca00,%rdx,%rdx
add    0x10(%rsp),%rcx
sub    %rcx,%rax
add    %rdx,%rax
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mul    %rcx
shr    $0x12,%rdx
lea    0xe56(%rip),%rdi
mov    %rdx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [33 x i8] c"RESULT : clock_gettime() %Lu ns\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %3)
br label %11
11: ; preds = %2, %11
%12 = phi i32 [ 0, %2 ], [ %15, %11 ]
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %5)
%15 = add nuw nsw i32 %12, 1
%16 = icmp eq i32 %15, 1000000
br i1 %16, label %17, label %11
17: ; preds = %11
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i32 @clock_gettime(i32 %18, %struct.timespec* %4)
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%23 = load i32, i32* %22
%24 = mul nsw i32 %23, 1000000000
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%27 = load i64, i64* %26
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%29 = load i32, i32* %28
%30 = mul nsw i32 %29, 1000000000
%31 = sext i32 %30 to i64
%32 = add i64 %21, %25
%33 = sub i64 %27, %32
%34 = add i64 %33, %31
%35 = udiv i64 %34, 1000000
%36 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %35)
ret i32 0
}