#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*/ timeval_align(struct timeval *);
int timeval_comp(struct timeval *, struct timeval *);
int timeval_subtract(struct timeval *result, struct timeval *x,
                     struct timeval *y) {
  int ret = timeval_comp(x, y);
  if (ret <= 0) {
    result->tv_sec = 0;
    result->tv_usec = 0;
    return 1;
  }
  if (x->tv_usec < y->tv_usec) {
    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
    y->tv_usec -= 1000000 * nsec;
    y->tv_sec += nsec;
  }
  result->tv_sec = x->tv_sec - y->tv_sec;
  result->tv_usec = x->tv_usec - y->tv_usec;
  timeval_align(result);
  return ret <= 0;
}


x86 assembly (clang, -O0):
<timeval_subtract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <timeval_comp@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1171 <timeval_subtract+0x51>
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1210 <timeval_subtract+0xf0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jge    11cb <timeval_subtract+0xab>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
add    $0x1,%rax
mov    %eax,-0x28(%rbp)
imul   $0xf4240,-0x28(%rbp),%eax
movslq %eax,%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
movslq -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x20(%rbp),%rax
sub    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
sub    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <timeval_align@plt>
cmpl   $0x0,-0x24(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_subtract>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1040 <timeval_comp@plt>
test   %eax,%eax
jle    1186 <timeval_subtract+0x66>
mov    (%r15),%rdx
mov    (%rbx),%rcx
mov    %rcx,%rax
sub    %rdx,%rax
jle    1194 <timeval_subtract+0x74>
movabs $0x431bde82d7b634db,%rdx
de 1b 43
imul   %rdx
mov    %rdx,%rax
shr    $0x12,%rax
shr    $0x3f,%rdx
add    %edx,%eax
add    $0x1,%eax
imul   $0xf4240,%eax,%edx
movslq %edx,%rdx
sub    %rdx,%rcx
mov    %rcx,(%rbx)
cltq
add    0x8(%rbx),%rax
mov    %rax,0x8(%rbx)
mov    (%r15),%rdx
jmp    1198 <timeval_subtract+0x78>
xorps  %xmm0,%xmm0
movups %xmm0,(%r14)
mov    $0x1,%eax
jmp    11b3 <timeval_subtract+0x93>
mov    0x8(%rbx),%rax
sub    %rcx,%rdx
mov    0x8(%r15),%rcx
sub    %rax,%rcx
mov    %rcx,0x8(%r14)
mov    %rdx,(%r14)
mov    %r14,%rdi
call   1030 <timeval_align@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @timeval_subtract(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = alloca i32
%5 = alloca %struct.timeval*
%6 = alloca %struct.timeval*
%7 = alloca %struct.timeval*
%8 = alloca i32
%9 = alloca i32
store %struct.timeval* %0, %struct.timeval** %5
store %struct.timeval* %1, %struct.timeval** %6
store %struct.timeval* %2, %struct.timeval** %7
%10 = load %struct.timeval*, %struct.timeval** %6
%11 = load %struct.timeval*, %struct.timeval** %7
%12 = call i32 @timeval_comp(%struct.timeval* %10, %struct.timeval* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load %struct.timeval*, %struct.timeval** %5
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %17
%18 = load %struct.timeval*, %struct.timeval** %5
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
store i64 0, i64* %19
store i32 1, i32* %4
br label %76
20: ; preds = %3
%21 = load %struct.timeval*, %struct.timeval** %6
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load %struct.timeval*, %struct.timeval** %7
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp slt i64 %23, %26
br i1 %27, label %28, label %52
28: ; preds = %20
%29 = load %struct.timeval*, %struct.timeval** %7
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load %struct.timeval*, %struct.timeval** %6
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %32, i32 0, i32 0
%34 = load i64, i64* %33
%35 = sub nsw i64 %31, %34
%36 = sdiv i64 %35, 1000000
%37 = add nsw i64 %36, 1
%38 = trunc i64 %37 to i32
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = mul nsw i32 1000000, %39
%41 = sext i32 %40 to i64
%42 = load %struct.timeval*, %struct.timeval** %7
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = sub nsw i64 %44, %41
store i64 %45, i64* %43
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = load %struct.timeval*, %struct.timeval** %7
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %48, i32 0, i32 1
%50 = load i64, i64* %49
%51 = add nsw i64 %50, %47
store i64 %51, i64* %49
br label %52
52: ; preds = %28, %20
%53 = load %struct.timeval*, %struct.timeval** %6
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %53, i32 0, i32 1
%55 = load i64, i64* %54
%56 = load %struct.timeval*, %struct.timeval** %7
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %56, i32 0, i32 1
%58 = load i64, i64* %57
%59 = sub nsw i64 %55, %58
%60 = load %struct.timeval*, %struct.timeval** %5
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %60, i32 0, i32 1
store i64 %59, i64* %61
%62 = load %struct.timeval*, %struct.timeval** %6
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = load %struct.timeval*, %struct.timeval** %7
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %65, i32 0, i32 0
%67 = load i64, i64* %66
%68 = sub nsw i64 %64, %67
%69 = load %struct.timeval*, %struct.timeval** %5
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %69, i32 0, i32 0
store i64 %68, i64* %70
%71 = load %struct.timeval*, %struct.timeval** %5
%72 = call i32 @timeval_align(%struct.timeval* %71)
%73 = load i32, i32* %8
%74 = icmp sle i32 %73, 0
%75 = zext i1 %74 to i32
store i32 %75, i32* %4
br label %76
76: ; preds = %52, %15
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @timeval_subtract(%struct.timeval* %0, %struct.timeval* %1, %struct.timeval* %2) {
%4 = tail call i32 @timeval_comp(%struct.timeval* %1, %struct.timeval* %2)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = bitcast %struct.timeval* %0 to i8*
br label %41
8: ; preds = %3
%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 = icmp sgt i64 %12, %10
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%16 = load i64, i64* %15
br label %30
17: ; preds = %8
%18 = sub nsw i64 %12, %10
%19 = sdiv i64 %18, 1000000
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1
%22 = mul nsw i32 %21, 1000000
%23 = sext i32 %22 to i64
%24 = sub nsw i64 %12, %23
store i64 %24, i64* %11
%25 = sext i32 %21 to i64
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%27 = load i64, i64* %26
%28 = add nsw i64 %27, %25
store i64 %28, i64* %26
%29 = load i64, i64* %9
br label %30
30: ; preds = %14, %17
%31 = phi i64 [ %12, %14 ], [ %24, %17 ]
%32 = phi i64 [ %10, %14 ], [ %29, %17 ]
%33 = phi i64 [ %16, %14 ], [ %28, %17 ]
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%35 = load i64, i64* %34
%36 = sub nsw i64 %35, %33
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i64 %36, i64* %37
%38 = sub nsw i64 %32, %31
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i64 %38, i64* %39
%40 = tail call i32 @timeval_align(%struct.timeval* %0)
br label %41
41: ; preds = %30, %6
%42 = phi i32 [ 1, %6 ], [ 0, %30 ]
ret i32 %42
}