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

/* Variables and functions */
int time_delta(struct timeval *later, struct timeval *past) {
  if (later->tv_usec < past->tv_usec) {
    later->tv_sec--;
    later->tv_usec += 1000000;
  }
  return (later->tv_sec - past->tv_sec) * 1000 +
         (later->tv_usec - past->tv_usec) / 1000;
}


x86 assembly (clang, -O0):
<time_delta>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jge    113e <time_delta+0x3e>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%eax
imul   $0x3e8,%eax,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
add    %rcx,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_delta>:
mov    (%rdi),%rax
mov    (%rsi),%rdx
mov    0x8(%rdi),%ecx
cmp    %rdx,%rax
jge    1120 <time_delta+0x20>
add    $0xffffffff,%ecx
mov    %ecx,0x8(%rdi)
add    $0xf4240,%rax
mov    %rax,(%rdi)
mov    (%rsi),%rdx
sub    %rdx,%rax
sub    0x8(%rsi),%ecx
imul   $0x3e8,%ecx,%ecx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x7,%rdx
add    %edx,%eax
add    %ecx,%eax
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
define i32 @time_delta(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = icmp slt i64 %4, %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%9 = load i32, i32* %8
br i1 %7, label %10, label %14
10: ; preds = %2
%11 = add nsw i32 %9, -1
store i32 %11, i32* %8
%12 = add nsw i64 %4, 1000000
store i64 %12, i64* %3
%13 = load i64, i64* %5
br label %14
14: ; preds = %2, %10
%15 = phi i64 [ %13, %10 ], [ %6, %2 ]
%16 = phi i64 [ %12, %10 ], [ %4, %2 ]
%17 = phi i32 [ %11, %10 ], [ %9, %2 ]
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%19 = load i32, i32* %18
%20 = sub nsw i32 %17, %19
%21 = mul nsw i32 %20, 1000
%22 = sub nsw i64 %16, %15
%23 = sdiv i64 %22, 1000
%24 = trunc i64 %23 to i32
%25 = add i32 %21, %24
ret i32 %25
}