#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 {
  int tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
struct timeval calculate_duration(struct timeval start, struct timeval end) {
  struct timeval duration;
  duration.tv_sec = end.tv_sec - start.tv_sec;
  if (start.tv_usec < end.tv_usec) {
    duration.tv_usec = end.tv_usec - start.tv_usec;
  } else {
    duration.tv_sec--;
    duration.tv_usec = end.tv_usec + (1000000 - start.tv_usec);
  }
  return duration;
}


x86 assembly (clang, -O0):
<calculate_duration>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x20(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x30(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1138 <calculate_duration+0x38>
mov    -0x30(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1154 <calculate_duration+0x54>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x30(%rbp),%eax
mov    $0xf4240,%ecx
sub    -0x20(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rdx
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_duration>:
mov    %edx,%eax
sub    %edi,%eax
add    $0xf4240,%eax
xor    %r8d,%r8d
sub    %edi,%edx
setle  %r8b
cmovg  %edx,%eax
sub    %rsi,%rcx
sub    %r8,%rcx
mov    %rcx,%rdx
ret

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

LLVM IR (-O2):
define { i32, i64 } @calculate_duration(i32 %0, i64 %1, i32 %2, i64 %3) {
%5 = sub i64 %3, %1
%6 = icmp sgt i32 %2, %0
%7 = select i1 %6, i32 0, i32 1000000
%8 = sub i32 %2, %0
%9 = add i32 %8, %7
%10 = xor i1 %6, true
%11 = sext i1 %10 to i64
%12 = add nsw i64 %5, %11
%13 = insertvalue { i32, i64 } poison, i32 %9, 0
%14 = insertvalue { i32, i64 } %13, i64 %12, 1
ret { i32, i64 } %14
}