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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
struct timespec double2timespec(double timeout) {
  long long int sec = (long long int)timeout;
  long long int nsec = (long long int)((timeout - (double)sec) * 1000000000);
  struct timespec ts;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  ts.tv_sec += sec;
  ts.tv_nsec += nsec;
  if (ts.tv_nsec >= 1000000000) {
    ts.tv_nsec %= 1000000000;
    ts.tv_sec++;
  }
  return ts;
}


x86 assembly (clang, -O0):
<double2timespec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x18(%rbp)
cvttsd2si -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsi2sdq -0x20(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  0xec2(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x28(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x3b9aca00,-0x10(%rbp)
3b
jl     11a1 <double2timespec+0x91>
mov    -0x10(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdx
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double2timespec>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cvttsd2si %xmm0,%r14
cvtsi2sd %r14,%xmm1
subsd  %xmm1,%xmm0
mulsd  0xed3(%rip),%xmm0
cvttsd2si %xmm0,%rbx
mov    0x2eaf(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
add    0x10(%rsp),%r14
add    0x8(%rsp),%rbx
cmp    $0x3b9aca00,%rbx
jl     1188 <double2timespec+0x78>
mov    %rbx,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rcx
mul    %rcx
shr    $0xb,%rdx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rbx
mov    %rbx,0x8(%rsp)
add    $0x1,%r14
mov    %r14,0x10(%rsp)
mov    %rbx,%rax
mov    %r14,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define { i64, i64 } @double2timespec(double %0) {
%2 = alloca %struct.timespec
%3 = alloca double
%4 = alloca i64
%5 = alloca i64
store double %0, double* %3
%6 = load double, double* %3
%7 = fptosi double %6 to i64
store i64 %7, i64* %4
%8 = load double, double* %3
%9 = load i64, i64* %4
%10 = sitofp i64 %9 to double
%11 = fsub double %8, %10
%12 = fmul double %11, 1.000000e+09
%13 = fptosi double %12 to i64
store i64 %13, i64* %5
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %2)
%16 = load i64, i64* %4
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%18 = load i64, i64* %17
%19 = add nsw i64 %18, %16
store i64 %19, i64* %17
%20 = load i64, i64* %5
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%22 = load i64, i64* %21
%23 = add nsw i64 %22, %20
store i64 %23, i64* %21
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp sge i64 %25, 1000000000
br i1 %26, label %27, label %34
27: ; preds = %1
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%29 = load i64, i64* %28
%30 = srem i64 %29, 1000000000
store i64 %30, i64* %28
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %31
br label %34
34: ; preds = %27, %1
%35 = bitcast %struct.timespec* %2 to { i64, i64 }*
%36 = load { i64, i64 }, { i64, i64 }* %35
ret { i64, i64 } %36
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define { i64, i64 } @double2timespec(double %0) {
%2 = alloca %struct.timespec
%3 = fptosi double %0 to i64
%4 = sitofp i64 %3 to double
%5 = fsub double %0, %4
%6 = fmul double %5, 1.000000e+09
%7 = fptosi double %6 to i64
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%11 = load i64, i64* %10
%12 = add nsw i64 %11, %3
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = add nsw i64 %14, %7
%16 = icmp sgt i64 %15, 999999999
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = urem i64 %15, 1000000000
store i64 %18, i64* %13
%19 = add nsw i64 %12, 1
store i64 %19, i64* %10
br label %20
20: ; preds = %17, %1
%21 = phi i64 [ %19, %17 ], [ %12, %1 ]
%22 = phi i64 [ %18, %17 ], [ %15, %1 ]
%23 = insertvalue { i64, i64 } poison, i64 %22, 0
%24 = insertvalue { i64, i64 } %23, i64 %21, 1
ret { i64, i64 } %24
}