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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void time_sleep(double delay) {
  struct timespec ts;
  int ret;
  ts.tv_sec = (time_t)delay;
  ts.tv_nsec = (long)((delay - ts.tv_sec) * 1e9);
  do {
    ret = nanosleep(&ts, &ts);
  } while (ret < 0 && errno == EINTR);
}


x86 assembly (clang, -O0):
<time_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
cvttsd2si %xmm0,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jge    117e <time_sleep+0x6e>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1146 <time_sleep+0x36>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,(%rsp)
subsd  %xmm0,%xmm0
mulsd  0xed6(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2ea2(%rip),%r14
mov    0x2e93(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jns    1167 <time_sleep+0x57>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1150 <time_sleep+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { double, i64 }
@errno = global i64 0
@EINTR = global i64 0
define void @time_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+09
%12 = fptosi double %11 to i64
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %12, i64* %13
br label %14
14: ; preds = %23, %1
%15 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
store i32 %15, i32* %4
br label %16
16: ; preds = %14
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %14, label %25
25: ; preds = %23
ret void
}

LLVM IR (-O2):
%struct.timespec = type { double, i64 }
@errno = global i64 0
@EINTR = global i64 0
define void @time_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+09
%7 = fptosi double %6 to i64
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %7, i64* %8
br label %9
9: ; preds = %9, %1
%10 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%11 = icmp slt i32 %10, 0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %9
ret void
}