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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void nano_sleep(double val) {
  struct timespec tv;
  tv.tv_sec = val;
  tv.tv_nsec = (val - tv.tv_sec) * 1000 * 1000 * 1000;
  int ret;
  do {
    ret = nanosleep(&tv, &tv);
  } while (ret == -1 && errno == EINTR);
}


x86 assembly (clang, -O0):
<nano_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  0xec7(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xebb(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xeaf(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    1192 <nano_sleep+0x82>
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    115a <nano_sleep+0x4a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nano_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,(%rsp)
subsd  %xmm0,%xmm0
movsd  0xed6(%rip),%xmm1
mulsd  %xmm1,%xmm0
mulsd  %xmm1,%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2e9a(%rip),%r14
mov    0x2e8b(%rip),%r15
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1168 <nano_sleep+0x58>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1150 <nano_sleep+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { double, double }
@errno = global i64 0
@EINTR = global i64 0
define void @nano_sleep(double %0) {
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca i32
store double %0, double* %2
%5 = load double, double* %2
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store double %5, double* %6
%7 = load double, double* %2
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%9 = load double, double* %8
%10 = fsub double %7, %9
%11 = fmul double %10, 1.000000e+03
%12 = fmul double %11, 1.000000e+03
%13 = fmul double %12, 1.000000e+03
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store double %13, double* %14
br label %15
15: ; preds = %24, %1
%16 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
store i32 %16, i32* %4
br label %17
17: ; preds = %15
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %15, label %26
26: ; preds = %24
ret void
}

LLVM IR (-O2):
%struct.timespec = type { double, double }
@errno = global i64 0
@EINTR = global i64 0
define void @nano_sleep(double %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store double %0, double* %4
%5 = fsub double %0, %0
%6 = fmul double %5, 1.000000e+03
%7 = fmul double %6, 1.000000e+03
%8 = fmul double %7, 1.000000e+03
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store double %8, double* %9
br label %10
10: ; preds = %10, %1
%11 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%12 = icmp eq i32 %11, -1
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %10, label %17
17: ; preds = %10
ret void
}