#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 */
typedef int time_t;
typedef int suseconds_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
duration(struct timeval *start, struct timeval *stop, struct timeval *delta) {
  suseconds_t microstart, microstop, microdelta;
  microstart = (suseconds_t)(100000 * (start->tv_sec)) + start->tv_usec;
  microstop = (suseconds_t)(100000 * (stop->tv_sec)) + stop->tv_usec;
  microdelta = microstop - microstart;
  delta->tv_usec = microdelta % 100000;
  delta->tv_sec = (time_t)(microdelta / 100000);
  if ((*delta).tv_sec < 0 || (*delta).tv_usec < 0)
    return -1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<duration>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
imul   $0x186a0,(%rax),%eax
mov    -0x10(%rbp),%rcx
add    0x4(%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
imul   $0x186a0,(%rax),%eax
mov    -0x18(%rbp),%rcx
add    0x4(%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rax
mov    %edx,0x4(%rax)
mov    -0x2c(%rbp),%eax
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jl     1181 <duration+0x81>
mov    -0x20(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jge    118d <duration+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <duration+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<duration>:
mov    (%rsi),%eax
mov    0x4(%rsi),%ecx
sub    (%rdi),%eax
sub    0x4(%rdi),%ecx
imul   $0x186a0,%eax,%eax
add    %eax,%ecx
movslq %ecx,%rax
imul   $0x14f8b589,%rax,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
sar    $0x2d,%rcx
add    %esi,%ecx
imul   $0x186a0,%ecx,%esi
mov    %eax,%edi
sub    %esi,%edi
mov    %edi,0x4(%rdx)
mov    %ecx,(%rdx)
sar    $0x1f,%edi
cmp    $0xfffe7961,%eax
mov    $0xffffffff,%eax
cmovge %edi,%eax
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @duration(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1, %struct.timeval* nocapture %2) {
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sub i32 %9, %5
%13 = mul i32 %12, 100000
%14 = sub i32 %11, %7
%15 = add i32 %14, %13
%16 = srem i32 %15, 100000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = sdiv i32 %15, 100000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 %18, i32* %19
%20 = icmp slt i32 %15, -99999
%21 = ashr i32 %16, 31
%22 = select i1 %20, i32 -1, i32 %21
ret i32 %22
}