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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int get_msec(struct timeval *oldtv) {
  struct timeval tv;
  unsigned msec = 0;
  gettimeofday(&tv, 0);
  if (oldtv->tv_sec != 0)
    msec = (int)(tv.tv_sec - oldtv->tv_sec) * 1000 +
           (int)(tv.tv_usec - oldtv->tv_usec + 500) / 1000;
  *oldtv = tv;
  return msec;
}


x86 assembly (clang, -O0):
<get_msec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1174 <get_msec+0x64>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%rax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    0x8(%rcx),%rax
add    $0x1f4,%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msec>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%r14),%rax
test   %rax,%rax
je     1163 <get_msec+0x53>
mov    0x8(%rsp),%ecx
mov    0x10(%rsp),%edx
sub    %eax,%ecx
sub    0x8(%r14),%edx
imul   $0x3e8,%ecx,%eax
add    $0x1f4,%edx
movslq %edx,%rcx
imul   $0x10624dd3,%rcx,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
add    %eax,%ebx
movups 0x8(%rsp),%xmm0
movups %xmm0,(%r14)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @get_msec(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca %struct.timeval
%4 = alloca i32
store %struct.timeval* %0, %struct.timeval** %2
store i32 0, i32* %4
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%6 = load %struct.timeval*, %struct.timeval** %2
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load %struct.timeval*, %struct.timeval** %2
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = sub nsw i64 %12, %15
%17 = trunc i64 %16 to i32
%18 = mul nsw i32 %17, 1000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%20 = load i64, i64* %19
%21 = load %struct.timeval*, %struct.timeval** %2
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = sub nsw i64 %20, %23
%25 = add nsw i64 %24, 500
%26 = trunc i64 %25 to i32
%27 = sdiv i32 %26, 1000
%28 = add nsw i32 %18, %27
store i32 %28, i32* %4
br label %29
29: ; preds = %10, %1
%30 = load %struct.timeval*, %struct.timeval** %2
%31 = bitcast %struct.timeval* %30 to i8*
%32 = bitcast %struct.timeval* %3 to i8*
%33 = load i32, i32* %4
ret i32 %33
}

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