#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 */
typedef scalar_t__ uint64_t;
struct timespec {
  unsigned long long tv_sec;
  scalar_t__ tv_nsec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
uint64_t gettime_ns(clockid_t clock) {
  struct timespec ts;
  if (clock_gettime(clock, &ts))
    error(1, errno, "gettime");
  return ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
}


x86 assembly (clang, -O0):
<gettime_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     115b <gettime_ns+0x3b>
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xeaa(%rip),%rdx
call   1040 <error@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
add    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettime_ns>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     114d <gettime_ns+0x2d>
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0xebd(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
imul   $0x3b9aca00,0x8(%rsp),%rax
9a 3b
add    0x10(%rsp),%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i32 0
@.str = [8 x i8] c"gettime\00"
define i64 @gettime_ns(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = call i32 @error(i32 1, i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %1
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%12 = load i64, i64* %11
%13 = mul i64 %12, 1000000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%15 = load i64, i64* %14
%16 = add i64 %13, %15
ret i64 %16
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i32 0
@.str = [8 x i8] c"gettime\00"
define i64 @gettime_ns(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = call i64 @clock_gettime(i32 %0, %struct.timespec* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = call i32 @error(i32 1, i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %6, %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = mul i64 %11, 1000000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%14 = load i64, i64* %13
%15 = add i64 %12, %14
ret i64 %15
}