#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__ uint32_t;
struct timespec {
  scalar_t__ tv_nsec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
uint32_t plt_getMonoTimeUS() {
  extern struct timespec plt_monoRef;
  extern uint32_t plt_monoTimeUS;
  struct timespec tsNow, tsDiff;
  clock_gettime(CLOCK_MONOTONIC, &tsNow);
  if (tsNow.tv_nsec < plt_monoRef.tv_nsec) {
    tsDiff.tv_sec = (tsNow.tv_sec - plt_monoRef.tv_sec) - 1;
    tsDiff.tv_nsec = (1000000000 + tsNow.tv_nsec) - plt_monoRef.tv_nsec;
  } else {
    tsDiff.tv_sec = tsNow.tv_sec - plt_monoRef.tv_sec;
    tsDiff.tv_nsec = tsNow.tv_nsec - plt_monoRef.tv_nsec;
  }
  plt_monoTimeUS +=
      (uint32_t)((tsDiff.tv_sec * 1000000) + (tsDiff.tv_nsec / 1000));
  plt_monoRef = tsNow;
  return plt_monoTimeUS;
}


x86 assembly (clang, -O0):
<plt_getMonoTimeUS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ec9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%rax
mov    0x2e9b(%rip),%rcx
cmp    (%rcx),%rax
jge    116e <plt_getMonoTimeUS+0x5e>
mov    -0x8(%rbp),%eax
mov    0x2e88(%rip),%rcx
sub    0x8(%rcx),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3b9aca00,%rax
mov    0x2e6e(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
jmp    1190 <plt_getMonoTimeUS+0x80>
mov    -0x8(%rbp),%eax
mov    0x2e58(%rip),%rcx
sub    0x8(%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e47(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x18(%rbp),%eax
movslq %eax,%rcx
mov    -0x20(%rbp),%rax
mov    $0x3e8,%esi
cqto
idiv   %rsi
add    %rax,%rcx
mov    0x2e2e(%rip),%rax
add    (%rax),%rcx
mov    0x2e24(%rip),%rax
mov    %rcx,(%rax)
mov    0x2e0a(%rip),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plt_getMonoTimeUS>:
sub    $0x18,%rsp
mov    0x2ecd(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rcx
mov    0x2e9d(%rip),%r8
mov    (%r8),%rdx
mov    0x8(%r8),%edi
lea    0x3b9aca00(%rcx),%rax
mov    %edi,%esi
neg    %esi
cmp    %rdx,%rcx
not    %edi
cmovge %esi,%edi
cmovge %rcx,%rax
add    0x10(%rsp),%edi
sub    %rdx,%rax
imul   $0xf4240,%edi,%ecx
movslq %ecx,%rcx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rdx,%rax
mov    0x2e5d(%rip),%rdx
add    (%rdx),%rcx
add    %rax,%rcx
mov    %rcx,(%rdx)
movups 0x8(%rsp),%xmm0
movups %xmm0,(%r8)
mov    %rcx,%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@plt_monoRef = external global %struct.timespec
@plt_monoTimeUS = external global i64
define i64 @plt_getMonoTimeUS() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i32 0, i32 0)
%8 = icmp slt i64 %6, %7
br i1 %8, label %9, label %22
9: ; preds = %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i32 0, i32 1)
%13 = sub nsw i32 %11, %12
%14 = sub nsw i32 %13, 1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%17 = load i64, i64* %16
%18 = add nsw i64 1000000000, %17
%19 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i32 0, i32 0)
%20 = sub nsw i64 %18, %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i64 %20, i64* %21
br label %33
22: ; preds = %0
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i32 0, i32 1)
%26 = sub nsw i32 %24, %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i32 0, i32 0)
%31 = sub nsw i64 %29, %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i64 %31, i64* %32
br label %33
33: ; preds = %22, %9
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%35 = load i32, i32* %34
%36 = mul nsw i32 %35, 1000000
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%39 = load i64, i64* %38
%40 = sdiv i64 %39, 1000
%41 = add nsw i64 %37, %40
%42 = load i64, i64* @plt_monoTimeUS
%43 = add nsw i64 %42, %41
store i64 %43, i64* @plt_monoTimeUS
%44 = bitcast %struct.timespec* %1 to i8*
%45 = load i64, i64* @plt_monoTimeUS
ret i64 %45
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@plt_monoRef = external global %struct.timespec
@plt_monoTimeUS = external global i64
define i64 @plt_getMonoTimeUS() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i32 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i64 0, i32 0)
%8 = icmp slt i64 %6, %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = load i32, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @plt_monoRef, i64 0, i32 1)
%12 = xor i32 %11, -1
%13 = add nsw i64 %6, 1000000000
%14 = sub i32 0, %11
%15 = select i1 %8, i32 %12, i32 %14
%16 = add i32 %10, %15
%17 = select i1 %8, i64 %13, i64 %6
%18 = sub nsw i64 %17, %7
%19 = mul nsw i32 %16, 1000000
%20 = sext i32 %19 to i64
%21 = sdiv i64 %18, 1000
%22 = load i64, i64* @plt_monoTimeUS
%23 = add i64 %22, %20
%24 = add i64 %23, %21
store i64 %24, i64* @plt_monoTimeUS
ret i64 %24
}