#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 */
void tv_sub(struct timeval *tv2, struct timeval *tv1) {
  tv2->tv_sec -= tv1->tv_sec;
  tv2->tv_usec -= tv1->tv_usec;
  while (tv2->tv_usec < 0) {
    tv2->tv_usec += 1000000;
    tv2->tv_sec -= 1;
  }
}


x86 assembly (clang, -O0):
<tv_sub>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jge    1165 <tv_sub+0x65>
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    1133 <tv_sub+0x33>
pop    %rbp
ret

x86 assembly (clang, -O2):
<tv_sub>:
mov    (%rdi),%ecx
sub    (%rsi),%ecx
mov    %ecx,(%rdi)
mov    0x8(%rsi),%r8
mov    0x8(%rdi),%rsi
mov    %rsi,%rdx
sub    %r8,%rdx
mov    %rdx,0x8(%rdi)
js     111b <tv_sub+0x1b>
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    %edx
add    %edx,%ecx
mov    %rax,0x8(%rdi)
mov    %ecx,(%rdi)
ret

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

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