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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
long urg_ticks(void) {
  long current_ticks = 0;
  static long first_ticks = 0;
  struct timeval tvp;
  gettimeofday(&tvp, NULL);
  long global_ticks = tvp.tv_sec * 1000 + tvp.tv_usec / 1000;
  if (first_ticks == 0) {
    first_ticks = global_ticks;
  }
  current_ticks = global_ticks - first_ticks;
  return current_ticks;
}


x86 assembly (clang, -O0):
<urg_ticks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,0x2ed9(%rip)
jne    1168 <urg_ticks+0x58>
mov    -0x18(%rbp),%rax
mov    %rax,0x2ec8(%rip)
mov    -0x18(%rbp),%rax
sub    0x2ebd(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urg_ticks>:
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq (%rsp),%rax
imul   $0x3e8,%rax,%rcx
movslq 0x4(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
cltq
add    %rcx,%rax
mov    0x2ee5(%rip),%rcx
test   %rcx,%rcx
jne    115a <urg_ticks+0x4a>
mov    %rax,0x2ed9(%rip)
mov    %rax,%rcx
sub    %rcx,%rax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@urg_ticks.first_ticks = internal global i64 0
define i64 @urg_ticks() {
%1 = alloca i64
%2 = alloca %struct.timeval
%3 = alloca i64
store i64 0, i64* %1
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%6 = load i32, i32* %5
%7 = mul nsw i32 %6, 1000
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%9 = load i32, i32* %8
%10 = sdiv i32 %9, 1000
%11 = add nsw i32 %7, %10
%12 = sext i32 %11 to i64
store i64 %12, i64* %3
%13 = load i64, i64* @urg_ticks.first_ticks
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = load i64, i64* %3
store i64 %16, i64* @urg_ticks.first_ticks
br label %17
17: ; preds = %15, %0
%18 = load i64, i64* %3
%19 = load i64, i64* @urg_ticks.first_ticks
%20 = sub nsw i64 %18, %19
store i64 %20, i64* %1
%21 = load i64, i64* %1
ret i64 %21
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@urg_ticks.first_ticks = internal unnamed_addr global i64 0
define i64 @urg_ticks() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = mul nsw i32 %5, 1000
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = sdiv i32 %8, 1000
%10 = add nsw i32 %9, %6
%11 = sext i32 %10 to i64
%12 = load i64, i64* @urg_ticks.first_ticks
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i64 %11, i64* @urg_ticks.first_ticks
br label %15
15: ; preds = %14, %0
%16 = phi i64 [ %11, %14 ], [ %12, %0 ]
%17 = sub nsw i64 %11, %16
ret i64 %17
}