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

/* Variables and functions */
long long util_timeval_diff(struct timeval *tv1, struct timeval *tv2) {
  long 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_sec * 1000000) + diff_usec;
}


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    118a <util_timeval_diff+0x8a>
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)
imul   $0xf4240,-0x20(%rbp),%rax
add    -0x28(%rbp),%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     113c <util_timeval_diff+0x3c>
test   %rsi,%rsi
je     113c <util_timeval_diff+0x3c>
mov    (%rsi),%rax
mov    0x8(%rsi),%rcx
sub    (%rdi),%rax
sub    0x8(%rdi),%rcx
lea    0xf4240(%rcx),%rdx
mov    %rcx,%rsi
sar    $0x3f,%rsi
add    %rax,%rsi
test   %rcx,%rcx
cmovns %rcx,%rdx
imul   $0xf4240,%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 %41
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* %6
%38 = mul nsw i64 %37, 1000000
%39 = load i64, i64* %7
%40 = add nsw i64 %38, %39
store i64 %40, i64* %3
br label %41
41: ; preds = %36, %13
%42 = load i64, i64* %3
ret i64 %42
}

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 %24
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 = mul nsw i64 %20, 1000000
%23 = add nsw i64 %22, %21
br label %24
24: ; preds = %2, %6
%25 = phi i64 [ %23, %6 ], [ 0, %2 ]
ret i64 %25
}