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

/* Variables and functions */
int /*<<< orphan*/ clock_gettime(int, struct timespec *);
long need_timer_in(const struct timespec *last, long timer) {
  long to;
  struct timespec ts;
  clock_gettime(1, &ts);
  to = ts.tv_sec - last->tv_sec;
  if (to > timer / 1000)
    return 0;
  to = to * 1000 + (ts.tv_nsec - last->tv_nsec) / 1000000;
  if (to >= timer)
    return 0;
  return timer - to;
}


x86 assembly (clang, -O0):
<need_timer_in>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jle    1170 <need_timer_in+0x60>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <need_timer_in+0xb5>
imul   $0x3e8,-0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     11b9 <need_timer_in+0xa9>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <need_timer_in+0xb5>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<need_timer_in>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    $0x1,%edi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rcx
sub    (%r14),%rcx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rbx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
cmp    %rdx,%rcx
jle    115b <need_timer_in+0x4b>
xor    %eax,%eax
jmp    118f <need_timer_in+0x7f>
mov    0x10(%rsp),%eax
sub    0x8(%r14),%eax
imul   $0x3e8,%rcx,%rcx
cltq
imul   $0x431bde83,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rax
add    %edx,%eax
movslq %eax,%rdx
add    %rcx,%rdx
xor    %eax,%eax
sub    %rdx,%rbx
cmovg  %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
define i64 @need_timer_in(%struct.timespec* nocapture %0, i64 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = call i32 @clock_gettime(i32 1, %struct.timespec* %3)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%7 = load i64, i64* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%9 = load i64, i64* %8
%10 = sub nsw i64 %7, %9
%11 = sdiv i64 %1, 1000
%12 = icmp sgt i64 %10, %11
br i1 %12, label %26, label %13
13: ; preds = %2
%14 = mul nsw i64 %10, 1000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%18 = load i32, i32* %17
%19 = sub nsw i32 %16, %18
%20 = sdiv i32 %19, 1000000
%21 = sext i32 %20 to i64
%22 = add nsw i64 %14, %21
%23 = icmp slt i64 %22, %1
%24 = sub nsw i64 %1, %22
%25 = select i1 %23, i64 %24, i64 0
br label %26
26: ; preds = %13, %2
%27 = phi i64 [ 0, %2 ], [ %25, %13 ]
ret i64 %27
}