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

/* Variables and functions */
long long timeval_diff(struct timeval *difference, struct timeval *end_time,
                       struct timeval *start_time) {
  struct timeval temp_diff;
  if (difference == NULL) {
    difference = &temp_diff;
  }
  difference->tv_sec = end_time->tv_sec - start_time->tv_sec;
  difference->tv_usec = end_time->tv_usec - start_time->tv_usec;
  while (difference->tv_usec < 0) {
    difference->tv_usec += 1000000;
    difference->tv_sec -= 1;
  }
  return 1000000LL * difference->tv_sec + difference->tv_usec;
}


x86 assembly (clang, -O0):
<timeval_diff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1123 <timeval_diff+0x23>
lea    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
sub    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jge    117f <timeval_diff+0x7f>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
jmp    114d <timeval_diff+0x4d>
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
imul   $0xf4240,%rax,%rax
mov    -0x8(%rbp),%rcx
add    0x8(%rcx),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_diff>:
mov    (%rsi),%ecx
sub    (%rdx),%ecx
lea    0x8(%rdi),%rax
test   %rdi,%rdi
lea    -0x10(%rsp),%r8
cmovne %rdi,%r8
mov    %ecx,(%r8)
mov    0x8(%rsi),%r10
mov    0x8(%rdx),%r9
lea    -0x8(%rsp),%rsi
cmovne %rax,%rsi
mov    %r10,%rdi
sub    %r9,%rdi
mov    %rdi,(%rsi)
jns    1186 <timeval_diff+0x86>
cmp    $0xfffffffffff0bdc1,%rdi
mov    $0xfffffffffff0bdc0,%rax
cmovge %rdi,%rax
add    %r9,%rax
xor    %edi,%edi
sub    %r10,%rax
setne  %dil
sub    %rdi,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
add    %rdi,%rdx
imul   $0xf4240,%rdx,%rdi
add    %r10,%rdi
sub    %r9,%rdi
add    $0xf4240,%rdi
not    %edx
add    %edx,%ecx
mov    %rdi,(%rsi)
mov    %ecx,(%r8)
movslq %ecx,%rax
imul   $0xf4240,%rax,%rax
add    %rdi,%rax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i64 @timeval_diff(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval*
%6 = alloca %struct.timeval*
%7 = alloca %struct.timeval
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timeval* %1, %struct.timeval** %5
store %struct.timeval* %2, %struct.timeval** %6
%8 = load %struct.timeval*, %struct.timeval** %4
%9 = icmp eq %struct.timeval* %8, null
br i1 %9, label %10, label %11
10: ; preds = %3
store %struct.timeval* %7, %struct.timeval** %4
br label %11
11: ; preds = %10, %3
%12 = load %struct.timeval*, %struct.timeval** %5
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load %struct.timeval*, %struct.timeval** %6
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sub nsw i32 %14, %17
%19 = load %struct.timeval*, %struct.timeval** %4
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load %struct.timeval*, %struct.timeval** %5
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = load %struct.timeval*, %struct.timeval** %6
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = sub nsw i64 %23, %26
%28 = load %struct.timeval*, %struct.timeval** %4
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %28, i32 0, i32 1
store i64 %27, i64* %29
br label %30
30: ; preds = %35, %11
%31 = load %struct.timeval*, %struct.timeval** %4
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = load %struct.timeval*, %struct.timeval** %4
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = add nsw i64 %38, 1000000
store i64 %39, i64* %37
%40 = load %struct.timeval*, %struct.timeval** %4
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %41
br label %30
44: ; preds = %30
%45 = load %struct.timeval*, %struct.timeval** %4
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = mul nsw i64 1000000, %48
%50 = load %struct.timeval*, %struct.timeval** %4
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = add nsw i64 %49, %52
ret i64 %53
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i64 @timeval_diff(%struct.timeval* %0, %struct.timeval* nocapture %1, %struct.timeval* nocapture %2) {
%4 = alloca i32
%5 = alloca i64
%6 = bitcast i32* %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = icmp eq %struct.timeval* %0, null
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = sub nsw i32 %10, %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%15 = select i1 %8, i32* %4, i32* %14
store i32 %13, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%19 = load i64, i64* %18
%20 = sub i64 %17, %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%22 = select i1 %8, i64* %5, i64* %21
store i64 %20, i64* %22
%23 = icmp slt i64 %20, 0
br i1 %23, label %24, label %40
24: ; preds = %3
%25 = call i64 @llvm.smax.i64(i64 %20, i64 -1000000)
%26 = add i64 %19, %25
%27 = sub i64 %26, %17
%28 = icmp ne i64 %27, 0
%29 = zext i1 %28 to i64
%30 = sub i64 %27, %29
%31 = udiv i64 %30, 1000000
%32 = add nuw nsw i64 %31, %29
%33 = mul i64 %32, 1000000
%34 = add i64 %17, %33
%35 = add i64 %34, 1000000
%36 = sub i64 %35, %19
%37 = trunc i64 %32 to i32
%38 = xor i32 %37, -1
%39 = add i32 %13, %38
store i64 %36, i64* %22
store i32 %39, i32* %15
br label %40
40: ; preds = %24, %3
%41 = phi i32 [ %39, %24 ], [ %13, %3 ]
%42 = phi i64 [ %36, %24 ], [ %20, %3 ]
%43 = sext i32 %41 to i64
%44 = mul nsw i64 %43, 1000000
%45 = add nsw i64 %44, %42
ret i64 %45
}