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

/* Variables and functions */
int timer_distance(struct timeval start_time, struct timeval end_time,
                   long int *res_s, long int *res_ms) {
  *res_ms = end_time.tv_usec - start_time.tv_usec;
  if (*res_ms < 0) {
    end_time.tv_sec--;
    *res_ms += 1000000;
  }
  *res_ms /= 1000;
  *res_s = 0;
  *res_s = end_time.tv_sec - start_time.tv_sec;
  return (0);
}


x86 assembly (clang, -O0):
<timer_distance>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x8(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x20(%rbp),%rcx
sub    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jge    1156 <timer_distance+0x56>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
add    $0xf4240,%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    (%rax),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rcx
sub    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_distance>:
mov    %rdx,%r10
sub    %rdi,%r10
lea    0xf4240(%r10),%rax
test   %r10,%r10
cmovns %r10,%rax
sar    $0x3f,%r10
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,(%r9)
sub    %rsi,%rcx
add    %r10,%rcx
mov    %rcx,(%r8)
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i32 @timer_distance(i64 %0, i64 %1, i64 %2, i64 %3, i64* nocapture writeonly %4, i64* nocapture writeonly %5) {
%7 = sub nsw i64 %2, %0
%8 = icmp slt i64 %7, 0
%9 = add nsw i64 %7, 1000000
%10 = select i1 %8, i64 %9, i64 %7
%11 = ashr i64 %7, 63
%12 = sdiv i64 %10, 1000
store i64 %12, i64* %5
%13 = sub i64 %3, %1
%14 = add i64 %13, %11
store i64 %14, i64* %4
ret i32 0
}