#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 */
void diff_timeval(struct timeval *dest, struct timeval *left,
                  struct timeval *right) {
  if ((left->tv_sec < right->tv_sec) ||
      ((left->tv_sec == right->tv_sec) && (left->tv_usec < right->tv_usec))) {
    dest->tv_sec = 0;
    dest->tv_usec = 0;
    return;
  }
  dest->tv_sec = left->tv_sec - right->tv_sec;
  dest->tv_usec = left->tv_usec - right->tv_usec;
  while (dest->tv_usec < 0) {
    dest->tv_usec += 1000000;
    dest->tv_sec--;
  }
}


x86 assembly (clang, -O0):
<diff_timeval>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jl     114e <diff_timeval+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    116a <diff_timeval+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    0x8(%rcx),%rax
jge    116a <diff_timeval+0x6a>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x8(%rax)
jmp    11cc <diff_timeval+0xcc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
sub    (%rax),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%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    11cc <diff_timeval+0xcc>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,(%rax)
jmp    1197 <diff_timeval+0x97>
pop    %rbp
ret

x86 assembly (clang, -O2):
<diff_timeval>:
mov    (%rsi),%rcx
sub    (%rdx),%rcx
jl     1117 <diff_timeval+0x17>
mov    0x8(%rsi),%rsi
mov    0x8(%rdx),%r8
jne    111e <diff_timeval+0x1e>
cmp    %r8,%rsi
jge    111e <diff_timeval+0x1e>
xorps  %xmm0,%xmm0
movups %xmm0,(%rdi)
ret
mov    %rcx,(%rdi)
mov    %rsi,%rdx
sub    %r8,%rdx
mov    %rdx,0x8(%rdi)
js     112e <diff_timeval+0x2e>
ret
cmp    $0xfffffffffff0bdc1,%rdx
mov    $0xfffffffffff0bdc0,%rax
cmovge %rdx,%rax
add    %r8,%rax
xor    %r9d,%r9d
sub    %rsi,%rax
setne  %r9b
sub    %r9,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
add    %r9,%rdx
imul   $0xf4240,%rdx,%rax
add    %rsi,%rax
sub    %r8,%rax
add    $0xf4240,%rax
not    %rdx
add    %rdx,%rcx
mov    %rax,0x8(%rdi)
mov    %rcx,(%rdi)
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define void @diff_timeval(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval*
%6 = alloca %struct.timeval*
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timeval* %1, %struct.timeval** %5
store %struct.timeval* %2, %struct.timeval** %6
%7 = load %struct.timeval*, %struct.timeval** %5
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%9 = load i64, i64* %8
%10 = load %struct.timeval*, %struct.timeval** %6
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = icmp slt i64 %9, %12
br i1 %13, label %30, label %14
14: ; preds = %3
%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** %6
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = icmp eq i64 %17, %20
br i1 %21, label %22, label %35
22: ; preds = %14
%23 = load %struct.timeval*, %struct.timeval** %5
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 1
%25 = load i64, i64* %24
%26 = load %struct.timeval*, %struct.timeval** %6
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %26, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp slt i64 %25, %28
br i1 %29, label %30, label %35
30: ; preds = %22, %3
%31 = load %struct.timeval*, %struct.timeval** %4
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %31, i32 0, i32 0
store i64 0, i64* %32
%33 = load %struct.timeval*, %struct.timeval** %4
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %33, i32 0, i32 1
store i64 0, i64* %34
br label %68
35: ; preds = %22, %14
%36 = load %struct.timeval*, %struct.timeval** %5
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load %struct.timeval*, %struct.timeval** %6
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = sub nsw i64 %38, %41
%43 = load %struct.timeval*, %struct.timeval** %4
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %43, i32 0, i32 0
store i64 %42, i64* %44
%45 = load %struct.timeval*, %struct.timeval** %5
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = load %struct.timeval*, %struct.timeval** %6
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %48, i32 0, i32 1
%50 = load i64, i64* %49
%51 = sub nsw i64 %47, %50
%52 = load %struct.timeval*, %struct.timeval** %4
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %52, i32 0, i32 1
store i64 %51, i64* %53
br label %54
54: ; preds = %59, %35
%55 = load %struct.timeval*, %struct.timeval** %4
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 1
%57 = load i64, i64* %56
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %54
%60 = load %struct.timeval*, %struct.timeval** %4
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %60, i32 0, i32 1
%62 = load i64, i64* %61
%63 = add nsw i64 %62, 1000000
store i64 %63, i64* %61
%64 = load %struct.timeval*, %struct.timeval** %4
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %64, i32 0, i32 0
%66 = load i64, i64* %65
%67 = add nsw i64 %66, -1
store i64 %67, i64* %65
br label %54
68: ; preds = %30, %54
ret void
}

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