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

/* Variables and functions */
unsigned int tv_diff(struct timeval *tv1, struct timeval *tv2) {
  unsigned int udiff;
  unsigned int diff;
  udiff = (unsigned int)(tv1->tv_usec - tv2->tv_usec);
  diff = (unsigned int)(tv1->tv_sec - tv2->tv_sec);
  return (diff * 1000000 + udiff) / 1000;
}


x86 assembly (clang, -O0):
<tv_diff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %eax,-0x18(%rbp)
imul   $0xf4240,-0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
pop    %rbp
ret

x86 assembly (clang, -O2):
<tv_diff>:
mov    (%rdi),%eax
mov    0x8(%rdi),%ecx
sub    0x8(%rsi),%ecx
sub    (%rsi),%eax
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @tv_diff(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval*
%5 = alloca i32
%6 = alloca i32
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 1
%9 = load i64, i64* %8
%10 = load %struct.timeval*, %struct.timeval** %4
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%12 = load i64, i64* %11
%13 = sub nsw i64 %9, %12
%14 = trunc i64 %13 to i32
store i32 %14, i32* %5
%15 = load %struct.timeval*, %struct.timeval** %3
%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
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = mul i32 %23, 1000000
%25 = load i32, i32* %5
%26 = add i32 %24, %25
%27 = udiv i32 %26, 1000
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @tv_diff(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%6 = load i64, i64* %5
%7 = sub nsw i64 %4, %6
%8 = trunc i64 %7 to i32
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = sub nsw i64 %10, %12
%14 = trunc i64 %13 to i32
%15 = mul i32 %14, 1000000
%16 = add i32 %15, %8
%17 = udiv i32 %16, 1000
ret i32 %17
}