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

/* Variables and functions */
int diff_ms(const struct timespec *t1, const struct timespec *t2) {
  struct timespec diff;
  diff.tv_sec = t1->tv_sec - t2->tv_sec;
  diff.tv_nsec = t1->tv_nsec - t2->tv_nsec;
  if (diff.tv_nsec < 0) {
    diff.tv_sec--;
    diff.tv_nsec += 1000000000;
  }
  return (diff.tv_sec * 1000 + diff.tv_nsec / 1000000);
}


x86 assembly (clang, -O0):
<diff_ms>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    114a <diff_ms+0x4a>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x14(%rbp)
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<diff_ms>:
mov    (%rdi),%eax
sub    (%rsi),%eax
mov    0x4(%rdi),%ecx
sub    0x4(%rsi),%ecx
lea    0x3b9aca00(%rcx),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x1f,%ecx
add    %eax,%ecx
imul   $0x3e8,%ecx,%ecx
movslq %edx,%rax
imul   $0x431bde83,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rax
add    %edx,%eax
add    %ecx,%eax
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @diff_ms(%struct.timespec* nocapture %0, %struct.timespec* nocapture %1) {
%3 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = sub i32 %4, %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sub nsw i32 %9, %11
%13 = icmp slt i32 %12, 0
%14 = add nsw i32 %12, 1000000000
%15 = select i1 %13, i32 %14, i32 %12
%16 = ashr i32 %12, 31
%17 = add nsw i32 %7, %16
%18 = mul nsw i32 %17, 1000
%19 = sdiv i32 %15, 1000000
%20 = add nsw i32 %18, %19
ret i32 %20
}