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

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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf4240,-0x38(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jae    118b <main+0x5b>
mov    $0x0,%al
call   1050 <ia32_getTime@plt>
mov    %rax,-0x50(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1166 <main+0x36>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
imul   $0x3b9aca00,-0x28(%rbp),%rax
3b
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    -0x38(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rsi
lea    0xe19(%rip),%rdi
call   1040 <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    $0xf4240,%ebp
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <ia32_getTime@plt>
add    $0xffffffff,%ebp
jne    1150 <main+0x20>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
sub    0x20(%rsp),%rcx
imul   $0x3b9aca00,%rcx,%rcx
sub    0x18(%rsp),%rax
add    %rcx,%rax
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mul    %rcx
shr    $0x12,%rdx
lea    0xe62(%rip),%rdi
mov    %rdx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@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 i32
%9 = alloca i32
%10 = alloca i64
%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* %9
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %6)
store i32 0, i32* %8
br label %15
15: ; preds = %21, %2
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = icmp ult i32 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = call i64 (...) @ia32_getTime()
store i64 %20, i64* %12
br label %21
21: ; preds = %19
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
br label %15
24: ; preds = %15
%25 = load i32, i32* @CLOCK_MONOTONIC
%26 = call i32 @clock_gettime(i32 %25, %struct.timespec* %7)
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %10
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%30 = load i64, i64* %29
%31 = mul i64 %30, 1000000000
%32 = load i64, i64* %10
%33 = add i64 %32, %31
store i64 %33, i64* %10
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%35 = load i64, i64* %34
store i64 %35, i64* %11
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%37 = load i64, i64* %36
%38 = mul i64 %37, 1000000000
%39 = load i64, i64* %11
%40 = add i64 %39, %38
store i64 %40, i64* %11
%41 = load i64, i64* %11
%42 = load i64, i64* %10
%43 = sub i64 %41, %42
%44 = load i32, i32* %9
%45 = zext i32 %44 to i64
%46 = udiv i64 %43, %45
%47 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %46)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@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 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
br label %9
9: ; preds = %2, %9
%10 = phi i32 [ 0, %2 ], [ %12, %9 ]
%11 = call i64 (...) @ia32_getTime()
%12 = add nuw nsw i32 %10, 1
%13 = icmp eq i32 %12, 1000000
br i1 %13, label %14, label %9
14: ; preds = %9
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %4)
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%24 = load i64, i64* %23
%25 = sub i64 %24, %20
%26 = mul i64 %25, 1000000000
%27 = sub i64 %22, %18
%28 = add i64 %27, %26
%29 = udiv i64 %28, 1000000
%30 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %29)
ret i32 0
}