#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 {
  int tv_usec;
  int tv_sec;
};
typedef int LONG_MAX;

/* Variables and functions */
long tv_to_msec(const struct timeval *tv) {
  if (tv->tv_usec > 1000000 || tv->tv_sec > (((LONG_MAX)-999) / 1000))
    return -1;
  return (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
}


x86 assembly (clang, -O0):
<tv_to_msec>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xf4240,(%rax)
jg     1126 <tv_to_msec+0x26>
mov    -0x10(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jle    1133 <tv_to_msec+0x33>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1161 <tv_to_msec+0x61>
mov    -0x10(%rbp),%rax
imul   $0x3e8,0x4(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x3e7,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<tv_to_msec>:
mov    (%rdi),%ecx
mov    $0xffffffffffffffff,%rax
cmp    $0xf4240,%ecx
jg     1118 <tv_to_msec+0x18>
mov    0x4(%rdi),%edx
test   %edx,%edx
jle    1119 <tv_to_msec+0x19>
ret
imul   $0x3e8,%edx,%eax
add    $0x3e7,%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
movslq %ecx,%rax
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i64 @tv_to_msec(%struct.timeval* %0) {
%2 = alloca i64
%3 = alloca %struct.timeval*
store %struct.timeval* %0, %struct.timeval** %3
%4 = load %struct.timeval*, %struct.timeval** %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%6 = load i32, i32* %5
%7 = icmp sgt i32 %6, 1000000
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = load %struct.timeval*, %struct.timeval** %3
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %8, %1
store i64 -1, i64* %2
br label %26
14: ; preds = %8
%15 = load %struct.timeval*, %struct.timeval** %3
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 1000
%19 = load %struct.timeval*, %struct.timeval** %3
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 999
%23 = sdiv i32 %22, 1000
%24 = add nsw i32 %18, %23
%25 = sext i32 %24 to i64
store i64 %25, i64* %2
br label %26
26: ; preds = %14, %13
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i64 @tv_to_msec(%struct.timeval* nocapture %0) {
%2 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = icmp sgt i32 %3, 1000000
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = icmp sgt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = mul nsw i32 %7, 1000
%11 = add nsw i32 %3, 999
%12 = sdiv i32 %11, 1000
%13 = add nsw i32 %10, %12
%14 = sext i32 %13 to i64
br label %15
15: ; preds = %1, %5, %9
%16 = phi i64 [ %14, %9 ], [ -1, %5 ], [ -1, %1 ]
ret i64 %16
}