#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 */
struct timeval {
  unsigned int tv_sec;
  unsigned int tv_usec;
  int /*<<< orphan*/ member_0;
};
struct timespec {
  unsigned int tv_sec;
  unsigned int tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
extern unsigned int tickget(void) {
  struct timespec tp = {0};
  struct timeval tv = {0};
  if (!clock_gettime(CLOCK_MONOTONIC_RAW, &tp)) {
    return tp.tv_sec * 1000u + tp.tv_nsec / 1000000u;
  } else {
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000u + tv.tv_usec / 1000u;
  }
}


x86 assembly (clang, -O0):
<tickget>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%eax
jne    1198 <tickget+0x68>
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <tickget+0x95>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
imul   $0x3e8,-0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tickget>:
sub    $0x28,%rsp
movl   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
je     1179 <tickget+0x59>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x8(%rsp),%eax
mov    $0x3e8,%ecx
lea    0xc(%rsp),%rdx
jmp    1187 <tickget+0x67>
mov    0x18(%rsp),%eax
mov    $0xf4240,%ecx
lea    0x1c(%rsp),%rdx
imul   $0x3e8,%eax,%esi
mov    (%rdx),%eax
xor    %edx,%edx
div    %ecx
add    %esi,%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32, i32 }
%struct.timeval = type { i32, i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define i32 @tickget() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC_RAW
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = icmp ne i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%11 = load i32, i32* %10
%12 = mul i32 %11, 1000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%14 = load i32, i32* %13
%15 = udiv i32 %14, 1000000
%16 = add i32 %12, %15
store i32 %16, i32* %1
br label %26
17: ; preds = %0
%18 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = mul i32 %20, 1000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%23 = load i32, i32* %22
%24 = udiv i32 %23, 1000
%25 = add i32 %21, %24
store i32 %25, i32* %1
br label %26
26: ; preds = %17, %9
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32, i32 }
%struct.timeval = type { i32, i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define i32 @tickget() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timeval
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = load i32, i32* @CLOCK_MONOTONIC_RAW
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
br label %17
12: ; preds = %0
%13 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
br label %17
17: ; preds = %12, %8
%18 = phi i32* [ %16, %12 ], [ %11, %8 ]
%19 = phi i32 [ 1000, %12 ], [ 1000000, %8 ]
%20 = phi i32 [ %15, %12 ], [ %10, %8 ]
%21 = mul i32 %20, 1000
%22 = load i32, i32* %18
%23 = udiv i32 %22, %19
%24 = add i32 %23, %21
ret i32 %24
}