#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 /*<<< orphan*/ printf(char *, ...);
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 retval = nanosleep(&tv, &tv);
    if (retval == 0) {
      printf("Sleep accomplished!\n");
      return 0;
    } else if (errno == EINTR) {
      printf("Sleep interrupted!\n");
      continue;
    } else {
      printf("Other codes returned: %d.\n", retval);
      return retval;
    }
  }
  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  0xeb7(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1040 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1189 <better_sleep+0x69>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <better_sleep+0xad>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <better_sleep+0x96>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1156 <better_sleep+0x36>
mov    -0x24(%rbp),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
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   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,(%rsp)
subsd  %xmm0,%xmm0
mulsd  0xeb2(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
mov    %rdi,%rsi
call   1050 <nanosleep@plt>
test   %eax,%eax
je     11c5 <better_sleep+0x95>
mov    %eax,%ebx
mov    0x2e70(%rip),%r12
mov    (%r12),%rax
mov    0x2e5d(%rip),%r13
cmp    0x0(%r13),%rax
jne    11b3 <better_sleep+0x83>
lea    0xe9b(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r15,%rsi
call   1050 <nanosleep@plt>
test   %eax,%eax
je     11c5 <better_sleep+0x95>
mov    %eax,%ebx
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1190 <better_sleep+0x60>
lea    0xe4e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d3 <better_sleep+0xa3>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { double, i64 }
@.str = [21 x i8] c"Sleep accomplished!\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [20 x i8] c"Sleep interrupted!\0A\00"
@.str.2 = [27 x i8] c"Other codes returned: %d.\0A\00"
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, %25
%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 %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %31
21: ; preds = %15
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %15
27: ; preds = %21
%28 = load i32, i32* %5
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %27, %19
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.timespec = type { double, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [27 x i8] c"Other codes returned: %d.\0A\00"
@str = [19 x i8] c"Sleep interrupted!\00"
@str.3 = [20 x i8] c"Sleep accomplished!\00"
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
%9 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %24, label %21
15: ; preds = %24, %1
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.3, i64 0, i64 0))
br label %28
17: ; preds = %24
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17, %11
%22 = phi i32 [ %9, %11 ], [ %26, %17 ]
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %28
24: ; preds = %11, %17
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%26 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %15, label %17
28: ; preds = %15, %21
%29 = phi i32 [ 0, %15 ], [ %22, %21 ]
ret i32 %29
}