#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  long tv_sec;
  long tv_usec;
};
struct itimerval {
  TYPE_1__ it_value;
};

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int setitimer(int /*<<< orphan*/, struct itimerval *, struct itimerval *);
double time_now();
int itimer_set(double expiry) {
  struct itimerval val = {};
  struct itimerval old = {};
  double now, delay;
  int err = 0;
  if (expiry == 0.0) {
    val.it_value.tv_sec = 0;
    val.it_value.tv_usec = 0;
  } else {
    now = time_now();
    delay = expiry - now;
    if (delay < 0.01)
      delay = 0.01;
    val.it_value.tv_sec = (long)delay;
    val.it_value.tv_usec = (long)((delay - (double)(long)delay) * 1.0e6);
  }
  err = setitimer(ITIMER_REAL, &val, &old);
  return err;
}


x86 assembly (clang, -O0):
<itimer_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movl   $0x0,-0x3c(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1191 <itimer_set+0x61>
jp     1191 <itimer_set+0x61>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
jmp    11ff <itimer_set+0xcf>
mov    $0x0,%al
call   1050 <time_now@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe4c(%rip),%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    11cc <itimer_set+0x9c>
movsd  0xe39(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
cvttsd2si -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
movsd  -0x38(%rbp),%xmm0
cvttsd2si -0x38(%rbp),%rax
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
movsd  0xe16(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x10(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <setitimer@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itimer_set>:
sub    $0x38,%rsp
xorpd  %xmm1,%xmm1
movapd %xmm1,0x20(%rsp)
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1140 <itimer_set+0x20>
jp     1140 <itimer_set+0x20>
xor    %eax,%eax
xor    %ecx,%ecx
jmp    1181 <itimer_set+0x61>
xor    %eax,%eax
movsd  %xmm0,0x8(%rsp)
call   1040 <time_now@plt>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  0xea1(%rip),%xmm0
maxsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
mulsd  0xe8c(%rip),%xmm0
cvttsd2si %xmm0,%rcx
mov    %rax,0x10(%rsp)
mov    %rcx,0x18(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
lea    0x20(%rsp),%rdx
call   1030 <setitimer@plt>
add    $0x38,%rsp
ret

LLVM IR (-O0):
%struct.itimerval = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64, i64 }
@ITIMER_REAL = global i32 0
define i32 @itimer_set(double %0) {
%2 = alloca double
%3 = alloca %struct.itimerval
%4 = alloca %struct.itimerval
%5 = alloca double
%6 = alloca double
%7 = alloca i32
store double %0, double* %2
%8 = bitcast %struct.itimerval* %3 to i8*
%9 = bitcast %struct.itimerval* %4 to i8*
store i32 0, i32* %7
%10 = load double, double* %2
%11 = fcmp oeq double %10, 0.000000e+00
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %3, i32 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %3, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 1
store i64 0, i64* %16
br label %39
17: ; preds = %1
%18 = call double (...) @time_now()
store double %18, double* %5
%19 = load double, double* %2
%20 = load double, double* %5
%21 = fsub double %19, %20
store double %21, double* %6
%22 = load double, double* %6
%23 = fcmp olt double %22, 1.000000e-02
br i1 %23, label %24, label %25
24: ; preds = %17
store double 1.000000e-02, double* %6
br label %25
25: ; preds = %24, %17
%26 = load double, double* %6
%27 = fptosi double %26 to i64
%28 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %3, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i64 %27, i64* %29
%30 = load double, double* %6
%31 = load double, double* %6
%32 = fptosi double %31 to i64
%33 = sitofp i64 %32 to double
%34 = fsub double %30, %33
%35 = fmul double %34, 1.000000e+06
%36 = fptosi double %35 to i64
%37 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %3, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 1
store i64 %36, i64* %38
br label %39
39: ; preds = %25, %12
%40 = load i32, i32* @ITIMER_REAL
%41 = call i32 @setitimer(i32 %40, %struct.itimerval* %3, %struct.itimerval* %4)
store i32 %41, i32* %7
%42 = load i32, i32* %7
ret i32 %42
}

LLVM IR (-O2):
%struct.itimerval = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64, i64 }
@ITIMER_REAL = global i32 0
define i32 @itimer_set(double %0) {
%2 = alloca %struct.itimerval
%3 = alloca %struct.itimerval
%4 = bitcast %struct.itimerval* %2 to i8*
%5 = bitcast %struct.itimerval* %3 to i8*
%6 = fcmp oeq double %0, 0.000000e+00
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = tail call double (...) @time_now()
%9 = fsub double %0, %8
%10 = fcmp olt double %9, 1.000000e-02
%11 = select i1 %10, double 1.000000e-02, double %9
%12 = fptosi double %11 to i64
%13 = sitofp i64 %12 to double
%14 = fsub double %11, %13
%15 = fmul double %14, 1.000000e+06
%16 = fptosi double %15 to i64
br label %17
17: ; preds = %1, %7
%18 = phi i64 [ %12, %7 ], [ 0, %1 ]
%19 = phi i64 [ %16, %7 ], [ 0, %1 ]
%20 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 0
store i64 %18, i64* %20
%21 = getelementptr inbounds %struct.itimerval, %struct.itimerval* %2, i64 0, i32 0, i32 1
store i64 %19, i64* %21
%22 = load i32, i32* @ITIMER_REAL
%23 = call i32 @setitimer(i32 %22, %struct.itimerval* %2, %struct.itimerval* %3)
ret i32 %23
}