#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 /*<<< orphan*/ uint64_t;
struct timespec {
  int dummy;
};
struct htrace_log {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int errno;
int /*<<< orphan*/ htrace_log(struct htrace_log *, char *, int,
                              int /*<<< orphan*/);
int /*<<< orphan*/ terror(int);
int /*<<< orphan*/ timespec_to_ms(struct timespec *);
uint64_t monotonic_now_ms(struct htrace_log *lg) {
  struct timespec ts;
  int err;
  if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
    err = errno;
    if (lg) {
      htrace_log(lg, "clock_gettime(CLOCK_MONOTONIC) error: %d (%s)\n", err,
                 terror(err));
    }
    return 0;
  }
  return timespec_to_ms(&ts);
}


x86 assembly (clang, -O0):
<monotonic_now_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     11b6 <monotonic_now_ms+0x76>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <monotonic_now_ms+0x6a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <terror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xe5b(%rip),%rsi
call   1060 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <monotonic_now_ms+0x82>
lea    -0x18(%rbp),%rdi
call   1050 <timespec_to_ms@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monotonic_now_ms>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     1192 <monotonic_now_ms+0x52>
xor    %ebp,%ebp
test   %rbx,%rbx
je     119e <monotonic_now_ms+0x5e>
mov    0x2e67(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   1040 <terror@plt>
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
mov    %eax,%ecx
call   1060 <htrace_log@plt>
jmp    119e <monotonic_now_ms+0x5e>
lea    0x8(%rsp),%rdi
call   1050 <timespec_to_ms@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.htrace_log = type { i32 }
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i32 0
@.str = [47 x i8] c"clock_gettime(CLOCK_MONOTONIC) error: %d (%s)\0A\00"
define i32 @monotonic_now_ms(%struct.htrace_log* %0) {
%2 = alloca i32
%3 = alloca %struct.htrace_log*
%4 = alloca %struct.timespec
%5 = alloca i32
store %struct.htrace_log* %0, %struct.htrace_log** %3
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i64 @clock_gettime(i32 %6, %struct.timespec* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %1
%10 = load i32, i32* @errno
store i32 %10, i32* %5
%11 = load %struct.htrace_log*, %struct.htrace_log** %3
%12 = icmp ne %struct.htrace_log* %11, null
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load %struct.htrace_log*, %struct.htrace_log** %3
%15 = load i32, i32* %5
%16 = load i32, i32* %5
%17 = call i32 @terror(i32 %16)
%18 = call i32 @htrace_log(%struct.htrace_log* %14, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17)
br label %19
19: ; preds = %13, %9
store i32 0, i32* %2
br label %22
20: ; preds = %1
%21 = call i32 @timespec_to_ms(%struct.timespec* %4)
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %19
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.htrace_log = type { i32 }
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i32 0
@.str = [47 x i8] c"clock_gettime(CLOCK_MONOTONIC) error: %d (%s)\0A\00"
define i32 @monotonic_now_ms(%struct.htrace_log* %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = icmp eq %struct.htrace_log* %0, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = call i32 @terror(i32 %8)
%12 = call i32 @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11)
br label %15
13: ; preds = %1
%14 = call i32 @timespec_to_ms(%struct.timespec* %2)
br label %15
15: ; preds = %7, %10, %13
%16 = phi i32 [ %14, %13 ], [ 0, %10 ], [ 0, %7 ]
ret i32 %16
}