#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 */
int timeval_sub(struct timeval *x, struct timeval *y) {
  struct timeval out;
  if (!x || !y) {
    return (-1);
  }
  out.tv_sec = x->tv_sec - y->tv_sec;
  out.tv_usec = x->tv_usec - y->tv_usec;
  if (out.tv_usec < 0) {
    --(out.tv_usec);
    out.tv_usec += 1000000;
  }
  x->tv_sec = out.tv_sec;
  x->tv_usec = out.tv_usec;
  return (0);
}


x86 assembly (clang, -O0):
<timeval_sub>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1122 <timeval_sub+0x22>
cmpq   $0x0,-0x18(%rbp)
jne    112e <timeval_sub+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <timeval_sub+0x97>
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    1179 <timeval_sub+0x79>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_sub>:
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1130 <timeval_sub+0x30>
test   %rsi,%rsi
je     1130 <timeval_sub+0x30>
mov    (%rdi),%rax
sub    (%rsi),%rax
lea    0xf423f(%rax),%rcx
test   %rax,%rax
cmovns %rax,%rcx
mov    0x8(%rsi),%rax
sub    %rax,0x8(%rdi)
mov    %rcx,(%rdi)
xor    %eax,%eax
ret

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

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