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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
unsigned int get_msec(void) {
  static struct timeval timeval, first_timeval;
  gettimeofday(&timeval, 0);
  if (first_timeval.tv_sec == 0) {
    first_timeval = timeval;
    return 0;
  }
  return (timeval.tv_sec - first_timeval.tv_sec) * 1000 +
         (timeval.tv_usec - first_timeval.tv_usec) / 1000;
}


x86 assembly (clang, -O0):
<get_msec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0x2f11(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmpq   $0x0,0x2f12(%rip)
jne    115c <get_msec+0x4c>
mov    0x2ef5(%rip),%rax
mov    %rax,0x2efe(%rip)
mov    0x2eef(%rip),%rax
mov    %rax,0x2ef8(%rip)
movl   $0x0,-0x4(%rbp)
jmp    1198 <get_msec+0x88>
mov    0x2ecd(%rip),%rax
sub    0x2ed6(%rip),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x10(%rbp)
mov    0x2ebd(%rip),%eax
sub    0x2ec7(%rip),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msec>:
push   %rbx
lea    0x2f18(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x2f18(%rip),%rax
test   %rax,%rax
je     1162 <get_msec+0x52>
mov    0x2efd(%rip),%ecx
mov    0x2eff(%rip),%edx
sub    0x2f09(%rip),%edx
sub    %eax,%ecx
movslq %edx,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
imul   $0x3e8,%ecx,%ebx
add    %eax,%ebx
jmp    1170 <get_msec+0x60>
movups 0x2ec7(%rip),%xmm0
movups %xmm0,0x2ed0(%rip)
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@get_msec.timeval = internal global %struct.timeval zeroinitializer
@get_msec.first_timeval = internal unnamed_addr global %struct.timeval zeroinitializer
define i32 @get_msec() {
%1 = tail call i32 @gettimeofday(%struct.timeval* @get_msec.timeval, i32 0)
%2 = load i64, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @get_msec.first_timeval, i64 0, i32 0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %5
4: ; preds = %0
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 dereferenceable(16) bitcast (%struct.timeval* @get_msec.first_timeval to i8*), i8* align 8 dereferenceable(16) bitcast (%struct.timeval* @get_msec.timeval to i8*), i64 16, i1 false)
br label %15
5: ; preds = %0
%6 = load i64, i64* getelementptr inbounds (%struct.timeval, %struct.timeval* @get_msec.timeval, i64 0, i32 0)
%7 = sub nsw i64 %6, %2
%8 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @get_msec.timeval, i64 0, i32 1)
%9 = load i32, i32* getelementptr inbounds (%struct.timeval, %struct.timeval* @get_msec.first_timeval, i64 0, i32 1)
%10 = sub nsw i32 %8, %9
%11 = sdiv i32 %10, 1000
%12 = trunc i64 %7 to i32
%13 = mul i32 %12, 1000
%14 = add i32 %11, %13
br label %15
15: ; preds = %5, %4
%16 = phi i32 [ 0, %4 ], [ %14, %5 ]
ret i32 %16
}