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

/* Variables and functions */
void subtract_timevals(struct timeval *result, struct timeval *a,
                       struct timeval *b) {
  struct timeval r;
  r.tv_sec = a->tv_sec - b->tv_sec;
  r.tv_usec = a->tv_usec - b->tv_usec;
  if (r.tv_usec < 0) {
    r.tv_usec += 1000000;
    r.tv_sec -= 1;
  }
  *result = r;
}


x86 assembly (clang, -O0):
<subtract_timevals>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x18(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    115b <subtract_timevals+0x5b>
mov    -0x28(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    %rcx,0x8(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<subtract_timevals>:
mov    (%rsi),%rax
mov    0x8(%rsi),%rcx
sub    0x8(%rdx),%rcx
sub    (%rdx),%rax
lea    0xf4240(%rax),%rdx
mov    %rax,%rsi
sar    $0x3f,%rsi
add    %rcx,%rsi
test   %rax,%rax
cmovns %rax,%rdx
mov    %rdx,(%rdi)
mov    %rsi,0x8(%rdi)
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @subtract_timevals(%struct.timeval* nocapture writeonly %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 i64 %5, %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = sub nsw i64 %10, %12
%14 = icmp slt i64 %13, 0
%15 = add nsw i64 %13, 1000000
%16 = ashr i64 %13, 63
%17 = add nsw i64 %8, %16
%18 = select i1 %14, i64 %15, i64 %13
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i64 %18, i64* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i64 %17, i64* %20
ret void
}