#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 */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                           struct timespec *, struct timespec *);
void time_sleep(double seconds) {
  struct timespec ts, rem;
  if (seconds > 0.0) {
    ts.tv_sec = seconds;
    ts.tv_nsec = (seconds - (double)ts.tv_sec) * 1E9;
    while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem)) {
      ts.tv_sec = rem.tv_sec;
      ts.tv_nsec = rem.tv_nsec;
    }
  }
}


x86 assembly (clang, -O0):
<time_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1194 <time_sleep+0x84>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  0xeb5(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1030 <clock_nanosleep@plt>
cmp    $0x0,%rax
je     118f <time_sleep+0x7f>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1154 <time_sleep+0x44>
jmp    1194 <time_sleep+0x84>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<time_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1180 <time_sleep+0x70>
movsd  %xmm0,(%rsp)
subsd  %xmm0,%xmm0
mulsd  0xecc(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    0x2e9f(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
xor    %esi,%esi
call   1030 <clock_nanosleep@plt>
test   %rax,%rax
je     1180 <time_sleep+0x70>
mov    %rsp,%r14
lea    0x10(%rsp),%rbx
movapd 0x10(%rsp),%xmm0
movapd %xmm0,(%rsp)
mov    (%r15),%edi
xor    %esi,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <clock_nanosleep@plt>
test   %rax,%rax
jne    1160 <time_sleep+0x50>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { double, double }
@CLOCK_REALTIME = global i32 0
define void @time_sleep(double %0) {
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store double %0, double* %2
%5 = load double, double* %2
%6 = fcmp ogt double %5, 0.000000e+00
br i1 %6, label %7, label %28
7: ; preds = %1
%8 = load double, double* %2
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store double %8, double* %9
%10 = load double, double* %2
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%12 = load double, double* %11
%13 = fsub double %10, %12
%14 = fmul double %13, 1.000000e+09
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store double %14, double* %15
br label %16
16: ; preds = %20, %7
%17 = load i32, i32* @CLOCK_REALTIME
%18 = call i64 @clock_nanosleep(i32 %17, i32 0, %struct.timespec* %3, %struct.timespec* %4)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%22 = load double, double* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store double %22, double* %23
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%25 = load double, double* %24
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store double %25, double* %26
br label %16
27: ; preds = %16
br label %28
28: ; preds = %27, %1
ret void
}

LLVM IR (-O2):
%struct.timespec = type { double, double }
@CLOCK_REALTIME = global i32 0
define void @time_sleep(double %0) {
%2 = alloca <2 x double>
%3 = bitcast <2 x double>* %2 to %struct.timespec*
%4 = alloca <2 x double>
%5 = bitcast <2 x double>* %4 to %struct.timespec*
%6 = bitcast <2 x double>* %2 to i8*
%7 = bitcast <2 x double>* %4 to i8*
%8 = fcmp ogt double %0, 0.000000e+00
br i1 %8, label %9, label %22
9: ; preds = %1
%10 = getelementptr inbounds <2 x double>, <2 x double>* %2, i64 0, i64 0
store double %0, double* %10
%11 = fsub double %0, %0
%12 = fmul double %11, 1.000000e+09
%13 = getelementptr inbounds <2 x double>, <2 x double>* %2, i64 0, i64 1
store double %12, double* %13
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i64 @clock_nanosleep(i32 %14, i32 0, %struct.timespec* %3, %struct.timespec* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %9, %17
%18 = load <2 x double>, <2 x double>* %4
store <2 x double> %18, <2 x double>* %2
%19 = load i32, i32* @CLOCK_REALTIME
%20 = call i64 @clock_nanosleep(i32 %19, i32 0, %struct.timespec* %3, %struct.timespec* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %17
22: ; preds = %17, %9, %1
ret void
}