#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 int uint64_t;
struct timespec {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
typedef int int64_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
uint64_t time_monotonic_us(void) {
  struct timespec t;
  if (clock_gettime(CLOCK_MONOTONIC, &t)) {
    perror("time_monotonic_us clock_gettime");
    return -1;
  }
  return ((int64_t)t.tv_sec + (uint64_t)604800) * (uint64_t)1000000 +
         (uint64_t)t.tv_nsec / (uint64_t)1000;
}


x86 assembly (clang, -O0):
<time_monotonic_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     115c <time_monotonic_us+0x3c>
lea    0xeb5(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1184 <time_monotonic_us+0x64>
mov    -0x10(%rbp),%rax
add    $0x93a80,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_monotonic_us>:
sub    $0x18,%rsp
mov    0x2ebd(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    1169 <time_monotonic_us+0x49>
imul   $0xf4240,0x10(%rsp),%eax
movslq 0x8(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %ecx,%eax
add    $0xd0e3a000,%eax
add    $0x18,%rsp
ret
lea    0xe90(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [32 x i8] c"time_monotonic_us clock_gettime\00"
define i32 @time_monotonic_us() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = load i32, i32* @CLOCK_MONOTONIC
%4 = call i64 @clock_gettime(i32 %3, %struct.timespec* %2)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %19
8: ; preds = %0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = add nsw i32 %11, 604800
%13 = mul nsw i32 %12, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%15 = load i64, i64* %14
%16 = trunc i64 %15 to i32
%17 = sdiv i32 %16, 1000
%18 = add nsw i32 %13, %17
store i32 %18, i32* %1
br label %19
19: ; preds = %8, %6
%20 = load i32, i32* %1
ret i32 %20
}

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