#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 long time_t;
struct timespec {
  long tv_sec;
  long tv_nsec;
};

/* Variables and functions */
long LONG_MAX;
long LONG_MIN;
long sancus_time_ts_to_ms(const struct timespec *ts) {
  long ms;
  if (ts->tv_sec <= (time_t)(LONG_MIN / (1000L * (1))))
    ms = LONG_MIN;
  else if (ts->tv_sec >= (time_t)(LONG_MAX / (1000L * (1))))
    ms = LONG_MAX;
  else if (ts->tv_sec < 0)
    ms = (1000L * (ts->tv_sec)) -
         (((ts->tv_nsec) + ((1000L * (1000L * (1))) / 2)) /
          (1000L * (1000L * (1))));
  else
    ms = (1000L * (ts->tv_sec)) +
         (((ts->tv_nsec) + ((1000L * (1000L * (1))) / 2)) /
          (1000L * (1000L * (1))));
  return ms;
}


x86 assembly (clang, -O0):
<sancus_time_ts_to_ms>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ece(%rip),%rax
mov    (%rax),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jg     114a <sancus_time_ts_to_ms+0x4a>
mov    0x2eaa(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    1213 <sancus_time_ts_to_ms+0x113>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jl     118c <sancus_time_ts_to_ms+0x8c>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    120e <sancus_time_ts_to_ms+0x10e>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jge    11d4 <sancus_time_ts_to_ms+0xd4>
mov    -0x8(%rbp),%rax
imul   $0x3e8,(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x7a120,%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1209 <sancus_time_ts_to_ms+0x109>
mov    -0x8(%rbp),%rax
imul   $0x3e8,(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
add    $0x7a120,%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    120e <sancus_time_ts_to_ms+0x10e>
jmp    1213 <sancus_time_ts_to_ms+0x113>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<sancus_time_ts_to_ms>:
mov    (%rdi),%rsi
mov    0x2ede(%rip),%rax
mov    (%rax),%rcx
movabs $0x20c49ba5e353f7cf,%r8
9b c4 20
mov    %rcx,%rax
imul   %r8
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
cmp    %rdx,%rsi
jle    1195 <sancus_time_ts_to_ms+0x95>
mov    0x2ea1(%rip),%rax
mov    (%rax),%rcx
mov    %rcx,%rax
imul   %r8
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
cmp    %rdx,%rsi
jge    1195 <sancus_time_ts_to_ms+0x95>
imul   $0x3e8,%rsi,%r8
mov    $0x7a120,%eax
add    0x8(%rdi),%rax
test   %rsi,%rsi
js     1174 <sancus_time_ts_to_ms+0x74>
movabs $0x431bde82d7b634db,%rcx
de 1b 43
jmp    117e <sancus_time_ts_to_ms+0x7e>
movabs $0xbce4217d2849cb25,%rcx
21 e4 bc
imul   %rcx
mov    %rdx,%rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rcx
add    %rax,%rcx
add    %r8,%rcx
mov    %rcx,%rax
ret

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

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