#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 */
typedef scalar_t__ time_t;
struct timeval {
  scalar_t__ tv_sec;
  int tv_usec;
};

/* Variables and functions */
time_t delta_time_in_milliseconds(struct timeval *t2, struct timeval *t1) {
  time_t delta_seconds = t2->tv_sec - t1->tv_sec;
  time_t delta_milliseconds = (t2->tv_usec - t1->tv_usec) / 1000;
  if (delta_milliseconds < 0) {
    delta_milliseconds += 1000;
    --delta_seconds;
  }
  return (delta_seconds * 1000) + delta_milliseconds;
}


x86 assembly (clang, -O0):
<delta_time_in_milliseconds>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    115f <delta_time_in_milliseconds+0x5f>
mov    -0x20(%rbp),%rax
add    $0x3e8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
imul   $0x3e8,-0x18(%rbp),%rax
add    -0x20(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<delta_time_in_milliseconds>:
mov    (%rdi),%rax
sub    (%rsi),%rax
mov    0x8(%rdi),%ecx
sub    0x8(%rsi),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rdx
add    %esi,%edx
movslq %edx,%rdx
lea    0x3e8(%rdx),%rsi
xor    %edi,%edi
cmp    $0xfffffc19,%ecx
cmovge %rdx,%rsi
setl   %dil
sub    %rdi,%rax
imul   $0x3e8,%rax,%rax
add    %rsi,%rax
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define i64 @delta_time_in_milliseconds(%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 = sub i64 %4, %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sub nsw i32 %9, %11
%13 = sdiv i32 %12, 1000
%14 = sext i32 %13 to i64
%15 = icmp slt i32 %12, -999
%16 = add nsw i64 %14, 1000
%17 = sext i1 %15 to i64
%18 = add nsw i64 %7, %17
%19 = select i1 %15, i64 %16, i64 %14
%20 = mul nsw i64 %18, 1000
%21 = add nsw i64 %20, %19
ret i64 %21
}