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

/* Variables and functions */
void timevalsub(struct timeval *res, struct timeval *tv1, struct timeval *tv2) {
  res->tv_sec = tv1->tv_sec - tv2->tv_sec;
  res->tv_usec = tv1->tv_usec - tv2->tv_usec;
  if (res->tv_usec >= 1000000) {
    res->tv_sec++;
    res->tv_usec -= 1000000;
  }
  if (res->tv_usec < 0) {
    res->tv_sec--;
    res->tv_usec += 1000000;
  }
}


x86 assembly (clang, -O0):
<timevalsub>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
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    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
sub    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xf4240,(%rax)
jl     1168 <timevalsub+0x68>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1193 <timevalsub+0x93>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0xf4240,%ecx
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<timevalsub>:
mov    0x8(%rsi),%rax
sub    0x8(%rdx),%rax
mov    %rax,0x8(%rdi)
mov    (%rsi),%ecx
sub    (%rdx),%ecx
mov    %ecx,(%rdi)
cmp    $0xf423f,%ecx
jle    1126 <timevalsub+0x26>
mov    $0xfff0bdc0,%edx
mov    $0x1,%esi
jmp    1137 <timevalsub+0x37>
test   %ecx,%ecx
js     112b <timevalsub+0x2b>
ret
mov    $0xf4240,%edx
mov    $0xffffffffffffffff,%rsi
add    %rsi,%rax
mov    %rax,0x8(%rdi)
add    %edx,%ecx
mov    %ecx,(%rdi)
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define void @timevalsub(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1, %struct.timeval* nocapture %2) {
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%5 = load i64, i64* %4
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%7 = load i64, i64* %6
%8 = sub nsw i64 %5, %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i64 %8, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sub nsw i32 %11, %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i32 %14, i32* %15
%16 = icmp sgt i32 %14, 999999
br i1 %16, label %19, label %17
17: ; preds = %3
%18 = icmp slt i32 %14, 0
br i1 %18, label %19, label %24
19: ; preds = %17, %3
%20 = phi i64 [ 1, %3 ], [ -1, %17 ]
%21 = phi i32 [ -1000000, %3 ], [ 1000000, %17 ]
%22 = add nsw i64 %8, %20
store i64 %22, i64* %9
%23 = add nsw i32 %14, %21
store i32 %23, i32* %15
br label %24
24: ; preds = %19, %17
ret void
}