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

/* Variables and functions */
unsigned long timeval_diff_msec(const struct timeval *end,
                                const struct timeval *start) {
  if (!start || !end)
    return 0UL;
  signed long secs = end->tv_sec - start->tv_sec;
  signed long usecs = end->tv_usec - start->tv_usec;
  if (usecs < 0) {
    usecs = 1000000 - usecs;
    secs -= 1;
  }
  if (secs >= 0)
    return secs * 1000UL + usecs / 1000UL;
  else
    return 0UL;
}


x86 assembly (clang, -O0):
<timeval_diff_msec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1122 <timeval_diff_msec+0x22>
cmpq   $0x0,-0x10(%rbp)
jne    112f <timeval_diff_msec+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <timeval_diff_msec+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x18(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1179 <timeval_diff_msec+0x79>
mov    $0xf4240,%eax
sub    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     11b1 <timeval_diff_msec+0xb1>
imul   $0x3e8,-0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <timeval_diff_msec+0xb9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_diff_msec>:
xor    %eax,%eax
test   %rdi,%rdi
je     1157 <timeval_diff_msec+0x57>
test   %rsi,%rsi
je     1157 <timeval_diff_msec+0x57>
mov    (%rdi),%rax
mov    0x8(%rdi),%rcx
sub    (%rsi),%rax
sub    0x8(%rsi),%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
add    %rax,%rdx
js     1158 <timeval_diff_msec+0x58>
mov    $0xf4240,%eax
sub    %rcx,%rax
test   %rcx,%rcx
cmovns %rcx,%rax
imul   $0x3e8,%rdx,%rcx
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mul    %rdx
mov    %rdx,%rax
shr    $0x4,%rax
add    %rcx,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i64 @timeval_diff_msec(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca i64
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval*
%6 = alloca i64
%7 = alloca i64
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timeval* %1, %struct.timeval** %5
%8 = load %struct.timeval*, %struct.timeval** %5
%9 = icmp ne %struct.timeval* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load %struct.timeval*, %struct.timeval** %4
%12 = icmp ne %struct.timeval* %11, null
br i1 %12, label %14, label %13
13: ; preds = %10, %2
store i64 0, i64* %3
br label %46
14: ; preds = %10
%15 = load %struct.timeval*, %struct.timeval** %4
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load %struct.timeval*, %struct.timeval** %5
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = sub nsw i64 %17, %20
store i64 %21, i64* %6
%22 = load %struct.timeval*, %struct.timeval** %4
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = load %struct.timeval*, %struct.timeval** %5
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %25, i32 0, i32 1
%27 = load i64, i64* %26
%28 = sub nsw i64 %24, %27
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %14
%32 = load i64, i64* %7
%33 = sub nsw i64 1000000, %32
store i64 %33, i64* %7
%34 = load i64, i64* %6
%35 = sub nsw i64 %34, 1
store i64 %35, i64* %6
br label %36
36: ; preds = %31, %14
%37 = load i64, i64* %6
%38 = icmp sge i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i64, i64* %6
%41 = mul i64 %40, 1000
%42 = load i64, i64* %7
%43 = udiv i64 %42, 1000
%44 = add i64 %41, %43
store i64 %44, i64* %3
br label %46
45: ; preds = %36
store i64 0, i64* %3
br label %46
46: ; preds = %45, %39, %13
%47 = load i64, i64* %3
ret i64 %47
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i64 @timeval_diff_msec(%struct.timeval* %0, %struct.timeval* %1) {
%3 = icmp ne %struct.timeval* %1, null
%4 = icmp ne %struct.timeval* %0, null
%5 = and i1 %4, %3
br i1 %5, label %6, label %27
6: ; preds = %2
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%8 = load i64, i64* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%10 = load i64, i64* %9
%11 = sub nsw i64 %8, %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%13 = load i64, i64* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%15 = load i64, i64* %14
%16 = sub nsw i64 %13, %15
%17 = ashr i64 %16, 63
%18 = add nsw i64 %17, %11
%19 = icmp sgt i64 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %6
%21 = icmp slt i64 %16, 0
%22 = sub nsw i64 1000000, %16
%23 = select i1 %21, i64 %22, i64 %16
%24 = mul i64 %18, 1000
%25 = udiv i64 %23, 1000
%26 = add i64 %24, %25
br label %27
27: ; preds = %20, %6, %2
%28 = phi i64 [ 0, %2 ], [ %26, %20 ], [ 0, %6 ]
ret i64 %28
}