#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;
};
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ diff_timespec(struct timespec *, struct timespec *);
int /*<<< orphan*/ printf(char *, char *, int /*<<< orphan*/);
void measure_clock_gettime(void) {
  struct timespec ta;
  struct timespec tb;
  int32_t granularity;
  int cnt = 0;
  clock_gettime(CLOCK_REALTIME, &ta);
  tb = ta;
  while (tb.tv_nsec == ta.tv_nsec) {
    clock_gettime(CLOCK_REALTIME, &ta);
    cnt++;
  }
  granularity = diff_timespec(&tb, &ta);
  printf("\tclock_gettime: %s%d ns\r\n", cnt == 1 ? "<=" : "", granularity);
}


x86 assembly (clang, -O0):
<measure_clock_gettime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x18(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1187 <measure_clock_gettime+0x57>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1159 <measure_clock_gettime+0x29>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <diff_timespec@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
lea    0xe78(%rip),%rsi
lea    0xe72(%rip),%rax
cmp    $0x1,%ecx
cmove  %rax,%rsi
mov    -0x14(%rbp),%edx
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_clock_gettime>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e9f(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,0x10(%rsp)
mov    $0x1,%ebp
mov    (%r14),%edi
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
add    $0xffffffff,%ebp
cmp    0x8(%rsp),%rbx
je     1160 <measure_clock_gettime+0x30>
test   %ebp,%ebp
lea    0xe9c(%rip),%rax
lea    0xe94(%rip),%rbx
cmove  %rax,%rbx
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <diff_timespec@plt>
lea    0xe61(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str = [26 x i8] c"\09clock_gettime: %s%d ns\0D\0A\00"
@.str.1 = [3 x i8] c"<=\00"
@.str.2 = [1 x i8] zeroinitializer
define void @measure_clock_gettime() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = bitcast %struct.timespec* %2 to i8*
%8 = bitcast %struct.timespec* %1 to i8*
br label %9
9: ; preds = %15, %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%11 = load i64, i64* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%13 = load i64, i64* %12
%14 = icmp eq i64 %11, %13
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i32, i32* @CLOCK_REALTIME
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %1)
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %9
20: ; preds = %9
%21 = call i32 @diff_timespec(%struct.timespec* %2, %struct.timespec* %1)
store i32 %21, i32* %3
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 1
%24 = zext i1 %23 to i64
%25 = select i1 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%26 = load i32, i32* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str = [26 x i8] c"\09clock_gettime: %s%d ns\0D\0A\00"
@.str.1 = [3 x i8] c"<=\00"
@.str.2 = [1 x i8] zeroinitializer
define void @measure_clock_gettime() {
%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_REALTIME
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%9 = load i64, i64* %7
store i64 %9, i64* %8
br label %10
10: ; preds = %0, %10
%11 = phi i32 [ %14, %10 ], [ 0, %0 ]
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %1)
%14 = add nuw nsw i32 %11, 1
%15 = load i64, i64* %7
%16 = icmp eq i64 %9, %15
br i1 %16, label %10, label %17
17: ; preds = %10
%18 = icmp eq i32 %11, 0
%19 = select i1 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%20 = call i32 @diff_timespec(%struct.timespec* %2, %struct.timespec* %1)
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %20)
ret void
}