#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*/ *);
unsigned int CP_GetTickCount() {
  static int startSeconds = -1;
  struct timeval tv;
  unsigned int ticks = 0;
  gettimeofday(&tv, NULL);
  if (startSeconds < 0)
    startSeconds = tv.tv_sec;
  ticks += ((tv.tv_sec - startSeconds) * 1000);
  ticks += tv.tv_usec / 1000;
  return ticks;
}


x86 assembly (clang, -O0):
<CP_GetTickCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cmpl   $0x0,0x2ef5(%rip)
jge    1142 <CP_GetTickCount+0x32>
mov    -0x8(%rbp),%eax
mov    %eax,0x2ee6(%rip)
mov    -0x8(%rbp),%eax
sub    0x2edd(%rip),%eax
imul   $0x3e8,%eax,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CP_GetTickCount>:
push   %rax
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2f07(%rip),%ecx
mov    (%rsp),%eax
test   %ecx,%ecx
jns    1130 <CP_GetTickCount+0x20>
mov    %eax,0x2efa(%rip)
mov    %eax,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%ecx
movslq 0x4(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
add    %ecx,%eax
pop    %rcx
ret

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

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