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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
unsigned long long timestamp_now_ns() {
  struct timespec ts;
  int res = clock_gettime(CLOCK_REALTIME, &ts);
  if (res != 0) {
    fprintf(stderr, "clock_gettime() returned %d\n", res);
    perror("clock_gettime()");
    return res;
  }
  return (ts.tv_sec * (1000 * 1000 * 1000UL) + ts.tv_nsec);
}


x86 assembly (clang, -O0):
<timestamp_now_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1188 <timestamp_now_ns+0x58>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe96(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119b <timestamp_now_ns+0x6b>
movslq -0x18(%rbp),%rax
imul   $0x3b9aca00,%rax,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timestamp_now_ns>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
jne    1160 <timestamp_now_ns+0x30>
movslq (%rsp),%rax
imul   $0x3b9aca00,%rax,%rax
add    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    %eax,%ebx
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
lea    0xe9d(%rip),%rdi
call   1050 <perror@plt>
movslq %ebx,%rax
jmp    115a <timestamp_now_ns+0x2a>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"clock_gettime() returned %d\0A\00"
@.str.1 = [16 x i8] c"clock_gettime()\00"
define i64 @timestamp_now_ns() {
%1 = alloca i64
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = load i32, i32* @CLOCK_REALTIME
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* %3
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
store i64 %14, i64* %1
br label %23
15: ; preds = %0
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 1000000000
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%21 = load i64, i64* %20
%22 = add i64 %19, %21
store i64 %22, i64* %1
br label %23
23: ; preds = %15, %8
%24 = load i64, i64* %1
ret i64 %24
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"clock_gettime() returned %d\0A\00"
@.str.1 = [16 x i8] c"clock_gettime()\00"
define i64 @timestamp_now_ns() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_REALTIME
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %4)
%9 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%10 = sext i32 %4 to i64
br label %19
11: ; preds = %0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
%15 = mul nsw i64 %14, 1000000000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%17 = load i64, i64* %16
%18 = add i64 %15, %17
br label %19
19: ; preds = %11, %6
%20 = phi i64 [ %10, %6 ], [ %18, %11 ]
ret i64 %20
}