#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 *);
int better_sleep(double sleep_time) {
  struct timespec tv;
  tv.tv_sec = (time_t)sleep_time;
  tv.tv_nsec = (long)((sleep_time - tv.tv_sec) * 1e+9);
  while (1) {
    int rval = nanosleep(&tv, &tv);
    if (rval == 0) {
      return 0;
    } else if (errno == EINTR) {
      continue;
    } else {
      return rval;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<better_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xec7(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    116b <better_sleep+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    1190 <better_sleep+0x80>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <better_sleep+0x7a>
jmp    1146 <better_sleep+0x36>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<better_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
je     1167 <better_sleep+0x57>
mov    (%r14),%rcx
cmp    (%r15),%rcx
je     1150 <better_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 i32 @better_sleep(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca %struct.timespec
%5 = alloca i32
store double %0, double* %3
%6 = load double, double* %3
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store double %6, double* %7
%8 = load double, double* %3
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%10 = load double, double* %9
%11 = fsub double %8, %10
%12 = fmul double %11, 1.000000e+09
%13 = fptosi double %12 to i64
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %13, i64* %14
br label %15
15: ; preds = %1, %24
%16 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %2
br label %27
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %15
25: ; preds = %20
%26 = load i32, i32* %5
store i32 %26, i32* %2
br label %27
27: ; preds = %25, %19
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.timespec = type { double, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @better_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 = %12, %1
%10 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %9, %12
ret i32 %10
}