#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 */
unsigned timeval_minus_msec(const struct timeval *s1,
                            const struct timeval *s2) {
  if (s1->tv_sec < s2->tv_sec)
    return 0;
  if (s1->tv_sec - s2->tv_sec > 2000000)
    return 2000000000;
  if (s1->tv_sec > s2->tv_sec)
    return (unsigned)((unsigned)(s1->tv_sec - s2->tv_sec) * 1000 +
                      ((int)s1->tv_usec - s2->tv_usec) / 1000);
  if (s1->tv_usec <= s2->tv_usec)
    return 0;
  return (unsigned)(s1->tv_usec - s2->tv_usec) / 1000u;
}


x86 assembly (clang, -O0):
<timeval_minus_msec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jge    112c <timeval_minus_msec+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    11dc <timeval_minus_msec+0xdc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
cmp    $0x1e8480,%rax
jle    1152 <timeval_minus_msec+0x52>
movl   $0x77359400,-0x4(%rbp)
jmp    11dc <timeval_minus_msec+0xdc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jle    11a2 <timeval_minus_msec+0xa2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x18(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <timeval_minus_msec+0xdc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x18(%rbp),%rcx
cmp    0x8(%rcx),%eax
jg     11c2 <timeval_minus_msec+0xc2>
movl   $0x0,-0x4(%rbp)
jmp    11dc <timeval_minus_msec+0xdc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x18(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_minus_msec>:
mov    (%rdi),%r8
mov    (%rsi),%rdx
xor    %eax,%eax
mov    %r8,%rcx
sub    %rdx,%rcx
jl     1160 <timeval_minus_msec+0x60>
mov    $0x77359400,%eax
cmp    $0x1e8480,%rcx
jg     1160 <timeval_minus_msec+0x60>
cmp    %rdx,%r8
jle    1148 <timeval_minus_msec+0x48>
imul   $0x3e8,%ecx,%ecx
mov    0x8(%rdi),%eax
sub    0x8(%rsi),%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
add    %ecx,%eax
ret
mov    0x8(%rdi),%ecx
mov    $0x0,%eax
sub    0x8(%rsi),%ecx
jle    1160 <timeval_minus_msec+0x60>
imul   $0x10624dd3,%rcx,%rax
shr    $0x26,%rax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
define i32 @timeval_minus_msec(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval*
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timeval* %1, %struct.timeval** %5
%6 = load %struct.timeval*, %struct.timeval** %4
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load %struct.timeval*, %struct.timeval** %5
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = icmp slt i64 %8, %11
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %69
14: ; preds = %2
%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
%22 = icmp sgt i64 %21, 2000000
br i1 %22, label %23, label %24
23: ; preds = %14
store i32 2000000000, i32* %3
br label %69
24: ; preds = %14
%25 = load %struct.timeval*, %struct.timeval** %4
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load %struct.timeval*, %struct.timeval** %5
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = icmp sgt i64 %27, %30
br i1 %31, label %32, label %51
32: ; preds = %24
%33 = load %struct.timeval*, %struct.timeval** %4
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load %struct.timeval*, %struct.timeval** %5
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 0
%38 = load i64, i64* %37
%39 = sub nsw i64 %35, %38
%40 = trunc i64 %39 to i32
%41 = mul i32 %40, 1000
%42 = load %struct.timeval*, %struct.timeval** %4
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.timeval*, %struct.timeval** %5
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = sub nsw i32 %44, %47
%49 = sdiv i32 %48, 1000
%50 = add i32 %41, %49
store i32 %50, i32* %3
br label %69
51: ; preds = %24
%52 = load %struct.timeval*, %struct.timeval** %4
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = load %struct.timeval*, %struct.timeval** %5
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = icmp sle i32 %54, %57
br i1 %58, label %59, label %60
59: ; preds = %51
store i32 0, i32* %3
br label %69
60: ; preds = %51
%61 = load %struct.timeval*, %struct.timeval** %4
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %61, i32 0, i32 1
%63 = load i32, i32* %62
%64 = load %struct.timeval*, %struct.timeval** %5
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %64, i32 0, i32 1
%66 = load i32, i32* %65
%67 = sub nsw i32 %63, %66
%68 = udiv i32 %67, 1000
store i32 %68, i32* %3
br label %69
69: ; preds = %60, %59, %32, %23, %13
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define i32 @timeval_minus_msec(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = icmp slt i64 %4, %6
br i1 %7, label %32, label %8
8: ; preds = %2
%9 = sub nsw i64 %4, %6
%10 = icmp sgt i64 %9, 2000000
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = icmp sgt i64 %4, %6
br i1 %12, label %13, label %23
13: ; preds = %11
%14 = trunc i64 %9 to i32
%15 = mul i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%19 = load i32, i32* %18
%20 = sub nsw i32 %17, %19
%21 = sdiv i32 %20, 1000
%22 = add i32 %21, %15
br label %32
23: ; preds = %11
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%27 = load i32, i32* %26
%28 = icmp sgt i32 %25, %27
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = sub nsw i32 %25, %27
%31 = udiv i32 %30, 1000
br label %32
32: ; preds = %23, %8, %2, %29, %13
%33 = phi i32 [ %22, %13 ], [ %31, %29 ], [ 0, %2 ], [ 2000000000, %8 ], [ 0, %23 ]
ret i32 %33
}