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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int errno;
int /*<<< orphan*/ printf(char *, int);
int getTimeUs(uint64_t *ns) {
  struct timespec ts;
  int ret;
  ret = clock_gettime(CLOCK_MONOTONIC, &ts);
  if (ret < 0) {
    ret = -errno;
    printf("clock_gettime() failed : %d(%m)", errno);
    return ret;
  }
  *ns = ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL;
  return 0;
}


x86 assembly (clang, -O0):
<getTimeUs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1179 <getTimeUs+0x59>
xor    %eax,%eax
mov    0x2e84(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <getTimeUs+0x80>
imul   $0xf4240,-0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    $0x3e8,%esi
xor    %edx,%edx
div    %rsi
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTimeUs>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
js     1169 <getTimeUs+0x49>
imul   $0xf4240,(%rsp),%rcx
mov    0x8(%rsp),%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mul    %rdx
shr    $0x4,%rdx
add    %rcx,%rdx
mov    %rdx,(%rbx)
xor    %ebx,%ebx
jmp    1182 <getTimeUs+0x62>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %esi,%ebx
neg    %ebx
lea    0xe83(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i32 0
@.str = [32 x i8] c"clock_gettime() failed : %d(%m)\00"
define i32 @getTimeUs(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca %struct.timespec
%5 = alloca i32
store i64* %0, i64** %3
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
store i32 %12, i32* %5
%13 = load i32, i32* @errno
%14 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %5
store i32 %15, i32* %2
br label %25
16: ; preds = %1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = mul i64 %18, 1000000
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%21 = load i64, i64* %20
%22 = udiv i64 %21, 1000
%23 = add i64 %19, %22
%24 = load i64*, i64** %3
store i64 %23, i64* %24
store i32 0, i32* %2
br label %25
25: ; preds = %16, %10
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i32 0
@.str = [32 x i8] c"clock_gettime() failed : %d(%m)\00"
define i32 @getTimeUs(i64* nocapture writeonly %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
%10 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %19
11: ; preds = %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = mul i64 %13, 1000000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%16 = load i64, i64* %15
%17 = udiv i64 %16, 1000
%18 = add i64 %17, %14
store i64 %18, i64* %0
br label %19
19: ; preds = %11, %7
%20 = phi i32 [ %9, %7 ], [ 0, %11 ]
ret i32 %20
}