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

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


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

x86 assembly (clang, -O2):
<time_monotonic_ms>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
jne    1167 <time_monotonic_ms+0x47>
mov    $0x93a80,%eax
add    (%rsp),%eax
and    $0x1fffff,%eax
imul   $0x3e8,%eax,%ecx
movslq 0x4(%rsp),%rax
imul   $0x431bde83,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rax
add    %edx,%eax
add    %ecx,%eax
pop    %rcx
ret
lea    0xe92(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [32 x i8] c"time_monotonic_ms clock_gettime\00"
define i32 @time_monotonic_ms() {
%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 %18
8: ; preds = %0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = add nsw i32 %10, 604800
%12 = and i32 %11, 2097151
%13 = mul nuw nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000000
%17 = add nsw i32 %13, %16
br label %18
18: ; preds = %8, %6
%19 = phi i32 [ -1, %6 ], [ %17, %8 ]
ret i32 %19
}