#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 */
struct timeval os_timeval_diff(const struct timeval *lhs,
                               const struct timeval *rhs) {
  struct timeval diff;
  diff.tv_sec = lhs->tv_sec - rhs->tv_sec;
  diff.tv_usec = lhs->tv_usec;
  while (rhs->tv_usec > diff.tv_usec) {
    --diff.tv_sec;
    diff.tv_usec += 1000000;
  }
  diff.tv_usec -= rhs->tv_usec;
  return diff;
}


x86 assembly (clang, -O0):
<os_timeval_diff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x20(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jle    115b <os_timeval_diff+0x5b>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0xf4240,%rax
mov    %rax,-0x10(%rbp)
jmp    112b <os_timeval_diff+0x2b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdx
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_timeval_diff>:
mov    (%rdi),%rcx
mov    0x8(%rdi),%rdi
mov    (%rsi),%r8
mov    0x8(%rsi),%r9
cmp    %r8,%rcx
jge    1163 <os_timeval_diff+0x63>
lea    0xf4240(%rcx),%rax
cmp    %rax,%r8
cmovg  %r8,%rax
sub    %rcx,%rax
xor    %esi,%esi
add    $0xfffffffffff0bdc0,%rax
setne  %sil
sub    %rsi,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
add    %rsi,%rdx
imul   $0xf4240,%rdx,%rax
not    %r9
add    %r9,%rdi
sub    %rdx,%rdi
add    %rax,%rcx
add    $0xf4240,%rcx
jmp    1166 <os_timeval_diff+0x66>
sub    %r9,%rdi
sub    %r8,%rcx
mov    %rcx,%rax
mov    %rdi,%rdx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define { i64, i64 } @os_timeval_diff(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%6 = load i64, i64* %5
%7 = sub nsw i64 %4, %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%9 = load i64, i64* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%11 = load i64, i64* %10
%12 = icmp slt i64 %9, %11
br i1 %12, label %13, label %29
13: ; preds = %2
%14 = add i64 %9, 1000000
%15 = call i64 @llvm.smax.i64(i64 %11, i64 %14)
%16 = add i64 %15, -1000000
%17 = sub i64 %16, %9
%18 = icmp ne i64 %17, 0
%19 = zext i1 %18 to i64
%20 = sub i64 %17, %19
%21 = udiv i64 %20, 1000000
%22 = add nuw nsw i64 %21, %19
%23 = mul i64 %22, 1000000
%24 = add i64 %9, %23
%25 = xor i64 %6, -1
%26 = add i64 %4, %25
%27 = sub i64 %26, %22
%28 = add i64 %24, 1000000
br label %29
29: ; preds = %13, %2
%30 = phi i64 [ %9, %2 ], [ %28, %13 ]
%31 = phi i64 [ %7, %2 ], [ %27, %13 ]
%32 = sub nsw i64 %30, %11
%33 = insertvalue { i64, i64 } poison, i64 %32, 0
%34 = insertvalue { i64, i64 } %33, i64 %31, 1
ret { i64, i64 } %34
}