#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 {
  long tv_nsec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int clock_getcpuclockid(int /*<<< orphan*/, int /*<<< orphan*/ *);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ getpid();
long cpu_ns() {
  clockid_t clk;
  struct timespec ts;
  int res = clock_getcpuclockid(getpid(), &clk);
  assert(!res);
  res = clock_gettime(clk, &ts);
  assert(!res);
  return ts.tv_nsec;
}


x86 assembly (clang, -O0):
<cpu_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
lea    -0x4(%rbp),%rsi
call   1050 <clock_getcpuclockid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_ns>:
sub    $0x18,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
call   1050 <clock_getcpuclockid@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
define i64 @cpu_ns() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = call i32 (...) @getpid()
%5 = call i32 @clock_getcpuclockid(i32 %4, i32* %1)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
%8 = xor i1 %7, true
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %1
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %2)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%19 = load i64, i64* %18
ret i64 %19
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
define i64 @cpu_ns() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = call i32 @clock_getcpuclockid(i32 %5, i32* %1)
%7 = icmp eq i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* %1
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %2)
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%16 = load i64, i64* %15
ret i64 %16
}