#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 *);
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 * 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    $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),%eax
cltq
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
imul   $0x3b9aca00,-0x28(%rbp),%eax
cltq
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    0xe17(%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>
movslq 0x20(%rsp),%rax
imul   $0x3b9aca00,%rax,%rcx
mov    0x8(%rsp),%rax
movslq 0x10(%rsp),%rdx
imul   $0x3b9aca00,%rdx,%rdx
add    0x18(%rsp),%rcx
sub    %rcx,%rax
add    %rdx,%rax
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mul    %rcx
shr    $0x12,%rdx
lea    0xe58(%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, 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 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 i32, i32* %29
%31 = mul nsw i32 %30, 1000000000
%32 = sext i32 %31 to i64
%33 = load i64, i64* %10
%34 = add i64 %33, %32
store i64 %34, i64* %10
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%36 = load i64, i64* %35
store i64 %36, i64* %11
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%38 = load i32, i32* %37
%39 = mul nsw i32 %38, 1000000000
%40 = sext i32 %39 to i64
%41 = load i64, i64* %11
%42 = add i64 %41, %40
store i64 %42, i64* %11
%43 = load i64, i64* %11
%44 = load i64, i64* %10
%45 = sub i64 %43, %44
%46 = load i32, i32* %9
%47 = zext i32 %46 to i64
%48 = udiv i64 %45, %47
%49 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %48)
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 = 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 i32, i32* %19
%21 = mul nsw i32 %20, 1000000000
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%26 = load i32, i32* %25
%27 = mul nsw i32 %26, 1000000000
%28 = sext i32 %27 to i64
%29 = add i64 %18, %22
%30 = sub i64 %24, %29
%31 = add i64 %30, %28
%32 = udiv i64 %31, 1000000
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i64 %32)
ret i32 0
}