#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 /*<<< orphan*/ assert(struct timeval *);
scalar_t__ timeval_gt(struct timeval *, struct timeval *);
void timeval_sub(struct timeval *a, struct timeval *b, struct timeval *result) {
  assert(a);
  assert(b);
  assert(result);
  if (timeval_gt(a, b)) {
    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;
    }
  } else {
    result->tv_sec = 0;
    result->tv_usec = 0;
  }
}


x86 assembly (clang, -O0):
<timeval_sub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <timeval_gt@plt>
cmp    $0x0,%rax
je     11c7 <timeval_sub+0xa7>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
sub    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jge    11c2 <timeval_sub+0xa2>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
jmp    11de <timeval_sub+0xbe>
mov    -0x18(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_sub>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <assert@plt>
mov    %r15,%rdi
call   1030 <assert@plt>
mov    %r14,%rdi
call   1030 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <timeval_gt@plt>
test   %rax,%rax
je     1179 <timeval_sub+0x59>
movdqu (%rbx),%xmm0
movdqu (%r15),%xmm1
psubq  %xmm1,%xmm0
movdqu %xmm0,(%r14)
movq   %xmm0,%rax
test   %rax,%rax
jns    1182 <timeval_sub+0x62>
paddq  0xe89(%rip),%xmm0
jmp    117d <timeval_sub+0x5d>
pxor   %xmm0,%xmm0
movdqu %xmm0,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @timeval_sub(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = tail call i32 @assert(%struct.timeval* %0)
%5 = tail call i32 @assert(%struct.timeval* %1)
%6 = tail call i32 @assert(%struct.timeval* %2)
%7 = tail call i64 @timeval_gt(%struct.timeval* %0, %struct.timeval* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %3
%10 = bitcast %struct.timeval* %0 to <2 x i64>*
%11 = load <2 x i64>, <2 x i64>* %10
%12 = bitcast %struct.timeval* %1 to <2 x i64>*
%13 = load <2 x i64>, <2 x i64>* %12
%14 = sub nsw <2 x i64> %11, %13
%15 = bitcast %struct.timeval* %2 to <2 x i64>*
store <2 x i64> %14, <2 x i64>* %15
%16 = extractelement <2 x i64> %14, i64 0
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %9
%19 = add nsw <2 x i64> %14, <i64 1000000, i64 -1>
%20 = bitcast %struct.timeval* %2 to <2 x i64>*
store <2 x i64> %19, <2 x i64>* %20
br label %23
21: ; preds = %3
%22 = bitcast %struct.timeval* %2 to i8*
br label %23
23: ; preds = %9, %18, %21
ret void
}