#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 */
typedef unsigned int time_t;
typedef int suseconds_t;
struct timeval {
  unsigned int tv_sec;
  int tv_usec;
};

/* Variables and functions */
unsigned int msdiff(struct timeval *tva, struct timeval *tvb) {
  time_t sdiff = tva->tv_sec - tvb->tv_sec;
  suseconds_t udiff = tva->tv_usec - tvb->tv_usec;
  if (sdiff < 0) {
    return (0);
  }
  if (udiff < 0) {
    udiff += 1000000;
    sdiff--;
  }
  if (sdiff < 0) {
    return (0);
  }
  if (sdiff >= (4294967295U / 1000)) {
    return ((unsigned int)4294967295U);
  }
  return ((unsigned int)(sdiff * 1000 + udiff / 1000));
}


x86 assembly (clang, -O0):
<msdiff>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x18(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jae    1142 <msdiff+0x42>
movl   $0x0,-0x4(%rbp)
jmp    11ae <msdiff+0xae>
cmpl   $0x0,-0x20(%rbp)
jge    1160 <msdiff+0x60>
mov    -0x20(%rbp),%eax
add    $0xf4240,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jae    1176 <msdiff+0x76>
movl   $0x0,-0x4(%rbp)
jmp    11ae <msdiff+0xae>
cmpl   $0x418937,-0x1c(%rbp)
jb     118f <msdiff+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <msdiff+0xae>
imul   $0x3e8,-0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<msdiff>:
mov    (%rdi),%eax
mov    0x4(%rdi),%ecx
sub    (%rsi),%eax
sub    0x4(%rsi),%ecx
mov    %ecx,%edx
sar    $0x1f,%edx
add    %eax,%edx
mov    $0xffffffff,%eax
cmp    $0x418936,%edx
ja     1147 <msdiff+0x47>
lea    0xf4240(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
imul   $0x3e8,%edx,%ecx
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
add    %ecx,%eax
ret

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

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