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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ abort();
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
uint64_t now_ms(void) {
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
    perror("stopwatch: clock_gettime (CLOCK_MONOTONIC)");
    abort();
  }
  return ((uint64_t)ts.tv_sec) * 1000 + (ts.tv_nsec / 1000000);
}


x86 assembly (clang, -O0):
<now_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0x0,%rax
jge    1167 <now_ms+0x37>
lea    0xea5(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%rax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<now_ms>:
sub    $0x18,%rsp
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
js     1174 <now_ms+0x44>
imul   $0x3e8,0x10(%rsp),%ecx
movslq 0x8(%rsp),%rax
imul   $0x431bde83,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rax
add    %edx,%eax
add    %ecx,%eax
add    $0x18,%rsp
ret
lea    0xe85(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@.str = [43 x i8] c"stopwatch: clock_gettime (CLOCK_MONOTONIC)\00"
define i32 @now_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 slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (...) @abort()
unreachable
9: ; preds = %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = mul nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000000
%17 = add nsw i32 %13, %16
ret i32 %17
}