#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 */
scalar_t__ _nas_timer_cmp(struct timeval const *, struct timeval const *);
int _nas_timer_sub(const struct timeval *a, const struct timeval *b,
                   struct timeval *result) {
  if (_nas_timer_cmp(a, b) > 0) {
    result->tv_sec = a->tv_sec - b->tv_sec;
    result->tv_usec = a->tv_usec - b->tv_usec;
    if (result->tv_usec < 0) {
      result->tv_sec--;
      result->tv_usec += 1000000;
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<_nas_timer_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <_nas_timer_cmp@plt>
cmp    $0x0,%rax
jle    11a3 <_nas_timer_sub+0x93>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
sub    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jge    1197 <_nas_timer_sub+0x87>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11aa <_nas_timer_sub+0x9a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nas_timer_sub>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <_nas_timer_cmp@plt>
test   %rax,%rax
jle    1155 <_nas_timer_sub+0x45>
movdqu (%rbx),%xmm0
movdqu (%r15),%xmm1
psubq  %xmm1,%xmm0
movdqu %xmm0,(%r14)
movq   %xmm0,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    115a <_nas_timer_sub+0x4a>
paddq  0xeb2(%rip),%xmm0
movdqu %xmm0,(%r14)
jmp    115a <_nas_timer_sub+0x4a>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @_nas_timer_sub(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* nocapture %2) {
%4 = tail call i64 @_nas_timer_cmp(%struct.timeval* %0, %struct.timeval* %1)
%5 = icmp sgt i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = bitcast %struct.timeval* %0 to <2 x i64>*
%8 = load <2 x i64>, <2 x i64>* %7
%9 = bitcast %struct.timeval* %1 to <2 x i64>*
%10 = load <2 x i64>, <2 x i64>* %9
%11 = sub nsw <2 x i64> %8, %10
%12 = bitcast %struct.timeval* %2 to <2 x i64>*
store <2 x i64> %11, <2 x i64>* %12
%13 = extractelement <2 x i64> %11, i64 0
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = add nsw <2 x i64> %11, <i64 1000000, i64 -1>
%17 = bitcast %struct.timeval* %2 to <2 x i64>*
store <2 x i64> %16, <2 x i64>* %17
br label %18
18: ; preds = %3, %6, %15
%19 = phi i32 [ 0, %15 ], [ 0, %6 ], [ -1, %3 ]
ret i32 %19
}