#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 {
  unsigned long tv_sec;
  unsigned long tv_usec;
};

/* Variables and functions */
int time_left(struct timeval *current, struct timeval *last, int gap) {
  unsigned long secs, diff;
  secs = current->tv_sec - last->tv_sec;
  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
  return (int)(diff < gap ? gap - diff : 0);
}


x86 assembly (clang, -O0):
<time_left>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    0x8(%rcx),%rax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jae    115f <time_left+0x5f>
movslq -0x14(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    116a <time_left+0x6a>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    116a <time_left+0x6a>
mov    -0x30(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_left>:
mov    (%rdi),%rax
sub    (%rsi),%rax
imul   $0xf4240,%rax,%rcx
add    0x8(%rdi),%rcx
sub    0x8(%rsi),%rcx
movslq %edx,%rdx
xor    %eax,%eax
sub    %rcx,%rdx
cmovae %rdx,%rax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @time_left(%struct.timeval* %0, %struct.timeval* %1, i32 %2) {
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store %struct.timeval* %0, %struct.timeval** %4
store %struct.timeval* %1, %struct.timeval** %5
store i32 %2, i32* %6
%9 = load %struct.timeval*, %struct.timeval** %4
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%11 = load i64, i64* %10
%12 = load %struct.timeval*, %struct.timeval** %5
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = sub i64 %11, %14
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = mul i64 1000000, %16
%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 = add i64 %17, %20
%22 = load %struct.timeval*, %struct.timeval** %5
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = sub i64 %21, %24
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %26, %28
br i1 %29, label %30, label %35
30: ; preds = %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = load i64, i64* %8
%34 = sub i64 %32, %33
br label %36
35: ; preds = %3
br label %36
36: ; preds = %35, %30
%37 = phi i64 [ %34, %30 ], [ 0, %35 ]
%38 = trunc i64 %37 to i32
ret i32 %38
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @time_left(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1, i32 %2) {
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%7 = load i64, i64* %6
%8 = sub i64 %5, %7
%9 = mul i64 %8, 1000000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%11 = load i64, i64* %10
%12 = add i64 %9, %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%14 = load i64, i64* %13
%15 = sub i64 %12, %14
%16 = sext i32 %2 to i64
%17 = tail call i64 @llvm.usub.sat.i64(i64 %16, i64 %15)
%18 = trunc i64 %17 to i32
ret i32 %18
}