#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 */
long util_timeval_diff(struct timeval *tv1, struct timeval *tv2) {
  long diff_sec, diff_usec;
  if (!tv1 || !tv2)
    return 0;
  diff_sec = tv2->tv_sec - tv1->tv_sec;
  diff_usec = tv2->tv_usec - tv1->tv_usec;
  if (diff_usec < 0) {
    diff_usec += 1000000;
    diff_sec--;
  }
  return ((diff_usec / 1000) + (diff_sec * 1000));
}


x86 assembly (clang, -O0):
<util_timeval_diff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1122 <util_timeval_diff+0x22>
cmpq   $0x0,-0x18(%rbp)
jne    112f <util_timeval_diff+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    1197 <util_timeval_diff+0x97>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    117a <util_timeval_diff+0x7a>
mov    -0x28(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,-0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

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

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i64 @util_timeval_diff(%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** %4
%9 = icmp ne %struct.timeval* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load %struct.timeval*, %struct.timeval** %5
%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 %42
14: ; preds = %10
%15 = load %struct.timeval*, %struct.timeval** %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load %struct.timeval*, %struct.timeval** %4
%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** %5
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = load %struct.timeval*, %struct.timeval** %4
%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 = add nsw i64 %32, 1000000
store i64 %33, i64* %7
%34 = load i64, i64* %6
%35 = add nsw i64 %34, -1
store i64 %35, i64* %6
br label %36
36: ; preds = %31, %14
%37 = load i64, i64* %7
%38 = sdiv i64 %37, 1000
%39 = load i64, i64* %6
%40 = mul nsw i64 %39, 1000
%41 = add nsw i64 %38, %40
store i64 %41, i64* %3
br label %42
42: ; preds = %36, %13
%43 = load i64, i64* %3
ret i64 %43
}

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