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

/* Variables and functions */
long timeval_diff(struct timeval *t1, struct timeval *t2) {
  long long res;
  if (t1 && t2) {
    res = t1->tv_sec;
    res = ((res - t2->tv_sec) * 1000000 + t1->tv_usec - t2->tv_usec) / 1000;
    return (long)res;
  }
  return -1;
}


x86 assembly (clang, -O0):
<timeval_diff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1170 <timeval_diff+0x70>
cmpq   $0x0,-0x18(%rbp)
je     1170 <timeval_diff+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
imul   $0xf4240,%rax,%rax
mov    -0x10(%rbp),%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
movslq 0x8(%rcx),%rcx
sub    %rcx,%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1178 <timeval_diff+0x78>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_diff>:
mov    $0xffffffffffffffff,%rax
test   %rdi,%rdi
je     114a <timeval_diff+0x4a>
test   %rsi,%rsi
je     114a <timeval_diff+0x4a>
mov    (%rdi),%rax
sub    (%rsi),%rax
imul   $0xf4240,%rax,%rcx
movslq 0x8(%rdi),%rax
add    %rcx,%rax
movslq 0x8(%rsi),%rcx
sub    %rcx,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
imul   %rcx
mov    %rdx,%rax
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x7,%rax
add    %rcx,%rax
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define i64 @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 %22
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 = mul nsw i64 %11, 1000000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%14 = load i32, i32* %13
%15 = sext i32 %14 to i64
%16 = add nsw i64 %12, %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%18 = load i32, i32* %17
%19 = sext i32 %18 to i64
%20 = sub i64 %16, %19
%21 = sdiv i64 %20, 1000
br label %22
22: ; preds = %2, %6
%23 = phi i64 [ %21, %6 ], [ -1, %2 ]
ret i64 %23
}