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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int sys_getMilli() {
  int curtime;
  struct timespec tp;
  static time_t secbase = 0;
  if (clock_gettime(CLOCK_REALTIME, &tp) != 0) {
    return 0;
  }
  if (secbase == 0) {
    secbase = tp.tv_sec;
  }
  curtime = (tp.tv_sec - secbase) * 1000 + tp.tv_nsec / 1000000;
  return curtime;
}


x86 assembly (clang, -O0):
<sys_getMilli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ec1(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     1140 <sys_getMilli+0x30>
movl   $0x0,-0x4(%rbp)
jmp    118f <sys_getMilli+0x7f>
cmpq   $0x0,0x2ee8(%rip)
jne    1159 <sys_getMilli+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,0x2ed7(%rip)
mov    -0x18(%rbp),%rax
sub    0x2ecc(%rip),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_getMilli>:
sub    $0x18,%rsp
mov    0x2ec5(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1136 <sys_getMilli+0x26>
add    $0x18,%rsp
ret
mov    0x2ef3(%rip),%rcx
mov    0x8(%rsp),%rax
test   %rcx,%rcx
jne    1151 <sys_getMilli+0x41>
mov    %rax,0x2ee2(%rip)
mov    %rax,%rcx
sub    %ecx,%eax
movslq 0x10(%rsp),%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0x3e8,%eax,%eax
add    %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@sys_getMilli.secbase = internal global i64 0
@CLOCK_REALTIME = global i32 0
define i32 @sys_getMilli() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = load i32, i32* @CLOCK_REALTIME
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 0, i32* %1
br label %27
8: ; preds = %0
%9 = load i64, i64* @sys_getMilli.secbase
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* @sys_getMilli.secbase
br label %14
14: ; preds = %11, %8
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @sys_getMilli.secbase
%18 = sub nsw i64 %16, %17
%19 = mul nsw i64 %18, 1000
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%21 = load i32, i32* %20
%22 = sdiv i32 %21, 1000000
%23 = sext i32 %22 to i64
%24 = add nsw i64 %19, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %2
%26 = load i32, i32* %2
store i32 %26, i32* %1
br label %27
27: ; preds = %14, %7
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@sys_getMilli.secbase = internal unnamed_addr global i64 0
@CLOCK_REALTIME = global i32 0
define i32 @sys_getMilli() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @CLOCK_REALTIME
%4 = call i64 @clock_gettime(i32 %3, %struct.timespec* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %21
6: ; preds = %0
%7 = load i64, i64* @sys_getMilli.secbase
%8 = icmp eq i64 %7, 0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%10 = load i64, i64* %9
br i1 %8, label %11, label %12
11: ; preds = %6
store i64 %10, i64* @sys_getMilli.secbase
br label %12
12: ; preds = %6, %11
%13 = phi i64 [ %10, %11 ], [ %7, %6 ]
%14 = sub nsw i64 %10, %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sdiv i32 %16, 1000000
%18 = trunc i64 %14 to i32
%19 = mul i32 %18, 1000
%20 = add i32 %19, %17
br label %21
21: ; preds = %0, %12
%22 = phi i32 [ %20, %12 ], [ 0, %0 ]
ret i32 %22
}