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

/* Variables and functions */
int CHAR_BIT;
scalar_t__ EINTR;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int xnanosleep(double seconds) {
  enum { BILLION = 1000000000 };
  bool overflow = false;
  double ns;
  struct timespec ts_sleep;
  assert(0 <= seconds);
  ts_sleep.tv_sec = seconds;
  ns = BILLION * (seconds - ts_sleep.tv_sec);
  overflow |= !(ts_sleep.tv_sec <= seconds && 0 <= ns && ns <= BILLION);
  ts_sleep.tv_nsec = ns;
  ts_sleep.tv_nsec += (ts_sleep.tv_nsec < ns);
  if (BILLION <= ts_sleep.tv_nsec) {
    time_t t = ts_sleep.tv_sec + 1;
    overflow |= (t < ts_sleep.tv_sec);
    ts_sleep.tv_sec = t;
    ts_sleep.tv_nsec -= BILLION;
  }
  for (;;) {
    if (overflow) {
      ts_sleep.tv_sec =
          ((time_t)(!(!((time_t)0 < (time_t)-1))
                        ? (time_t)-1
                        : ~(~(time_t)0 << (sizeof(time_t) * CHAR_BIT - 1))));
      ts_sleep.tv_nsec = BILLION - 1;
    }
    errno = 0;
    if (nanosleep(&ts_sleep, NULL) == 0)
      break;
    if (errno != EINTR && errno != 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<xnanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm1
subsd  -0x30(%rbp),%xmm1
movsd  0xe97(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jb     11b5 <xnanosleep+0x95>
movsd  -0x20(%rbp),%xmm0
xor    %eax,%eax
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jb     11b5 <xnanosleep+0x95>
movsd  0xe56(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
setae  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe00(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     125e <xnanosleep+0x13e>
movsd  0xdf6(%rip),%xmm0
addsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xdab(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1263 <xnanosleep+0x143>
cmpl   $0x0,-0x14(%rbp)
je     12a4 <xnanosleep+0x184>
mov    0x2d6c(%rip),%rax
movslq (%rax),%rcx
shl    $0x3,%rcx
sub    $0x1,%rcx
mov    $0xffffffffffffffff,%rax
shl    %cl,%rax
xor    $0xffffffffffffffff,%rax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xd71(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2d2d(%rip),%rax
movq   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jne    12ce <xnanosleep+0x1ae>
jmp    130a <xnanosleep+0x1ea>
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%rax
je     1305 <xnanosleep+0x1e5>
mov    0x2ce9(%rip),%rax
cmpq   $0x0,(%rax)
je     1305 <xnanosleep+0x1e5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <xnanosleep+0x1f1>
jmp    1263 <xnanosleep+0x143>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xnanosleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm1,%xmm1
xor    %edi,%edi
ucomisd %xmm1,%xmm0
setae  %dil
call   1040 <assert@plt>
movsd  0x10(%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  %xmm2,%xmm0
movsd  0xea3(%rip),%xmm1
ucomisd %xmm2,%xmm2
mulsd  %xmm1,%xmm0
setp   %al
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm0
setb   %r13b
ucomisd %xmm0,%xmm1
movsd  %xmm2,(%rsp)
setb   %cl
or     %al,%r13b
or     %cl,%r13b
addsd  %xmm3,%xmm0
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm1,%xmm0
jb     11cc <xnanosleep+0xac>
movsd  0xe6a(%rip),%xmm1
addsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
ucomisd %xmm1,%xmm2
seta   %al
or     %al,%r13b
movsd  %xmm1,(%rsp)
addsd  0xe4a(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movabs $0x41cdcd64ff800000,%r12
cd cd 41
mov    0x2dfb(%rip),%rbp
mov    %rsp,%r14
mov    0x2de9(%rip),%r15
nopw   0x0(%rax,%rax,1)
test   %r13b,%r13b
je     1224 <xnanosleep+0x104>
mov    0x2de4(%rip),%rax
movzbl (%rax),%ecx
shl    $0x3,%cl
add    $0xff,%cl
mov    $0xffffffffffffffff,%rax
shl    %cl,%rax
not    %rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
mov    %r12,0x8(%rsp)
movq   $0x0,0x0(%rbp)
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     1250 <xnanosleep+0x130>
mov    0x0(%rbp),%rax
test   %rax,%rax
je     11f0 <xnanosleep+0xd0>
cmp    (%r15),%rax
je     11f0 <xnanosleep+0xd0>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { double, double }
@CHAR_BIT = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @xnanosleep(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca %struct.timespec
%7 = alloca i64
store double %0, double* %3
store i32 0, i32* %4
%8 = load double, double* %3
%9 = fcmp ole double 0.000000e+00, %8
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load double, double* %3
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store double %12, double* %13
%14 = load double, double* %3
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%16 = load double, double* %15
%17 = fsub double %14, %16
%18 = fmul double 1.000000e+09, %17
store double %18, double* %5
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%20 = load double, double* %19
%21 = load double, double* %3
%22 = fcmp ole double %20, %21
br i1 %22, label %23, label %29
23: ; preds = %1
%24 = load double, double* %5
%25 = fcmp ole double 0.000000e+00, %24
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load double, double* %5
%28 = fcmp ole double %27, 1.000000e+09
br label %29
29: ; preds = %26, %23, %1
%30 = phi i1 [ false, %23 ], [ false, %1 ], [ %28, %26 ]
%31 = xor i1 %30, true
%32 = zext i1 %31 to i32
%33 = load i32, i32* %4
%34 = or i32 %33, %32
store i32 %34, i32* %4
%35 = load double, double* %5
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store double %35, double* %36
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%38 = load double, double* %37
%39 = load double, double* %5
%40 = fcmp olt double %38, %39
%41 = zext i1 %40 to i32
%42 = sitofp i32 %41 to double
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%44 = load double, double* %43
%45 = fadd double %44, %42
store double %45, double* %43
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%47 = load double, double* %46
%48 = fcmp ole double 1.000000e+09, %47
br i1 %48, label %49, label %68
49: ; preds = %29
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%51 = load double, double* %50
%52 = fadd double %51, 1.000000e+00
%53 = fptosi double %52 to i64
store i64 %53, i64* %7
%54 = load i64, i64* %7
%55 = sitofp i64 %54 to double
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%57 = load double, double* %56
%58 = fcmp olt double %55, %57
%59 = zext i1 %58 to i32
%60 = load i32, i32* %4
%61 = or i32 %60, %59
store i32 %61, i32* %4
%62 = load i64, i64* %7
%63 = sitofp i64 %62 to double
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store double %63, double* %64
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%66 = load double, double* %65
%67 = fsub double %66, 1.000000e+09
store double %67, double* %65
br label %68
68: ; preds = %49, %29
br label %69
69: ; preds = %94, %68
%70 = load i32, i32* %4
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %82
72: ; preds = %69
%73 = load i32, i32* @CHAR_BIT
%74 = sext i32 %73 to i64
%75 = mul i64 8, %74
%76 = sub i64 %75, 1
%77 = shl i64 -1, %76
%78 = xor i64 %77, -1
%79 = sitofp i64 %78 to double
%80 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store double %79, double* %80
%81 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store double 0x41CDCD64FF800000, double* %81
br label %82
82: ; preds = %72, %69
store i64 0, i64* @errno
%83 = call i64 @nanosleep(%struct.timespec* %6, i32* null)
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %82
br label %95
86: ; preds = %82
%87 = load i64, i64* @errno
%88 = load i64, i64* @EINTR
%89 = icmp ne i64 %87, %88
br i1 %89, label %90, label %94
90: ; preds = %86
%91 = load i64, i64* @errno
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %90
store i32 -1, i32* %2
br label %96
94: ; preds = %90, %86
br label %69
95: ; preds = %85
store i32 0, i32* %2
br label %96
96: ; preds = %95, %93
%97 = load i32, i32* %2
ret i32 %97
}

LLVM IR (-O2):
%struct.timespec = type { double, double }
@CHAR_BIT = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @xnanosleep(double %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = fcmp oge double %0, 0.000000e+00
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store double %0, double* %7
%8 = fsub double %0, %0
%9 = fmul double %8, 1.000000e+09
%10 = fcmp uno double %0, 0.000000e+00
%11 = fcmp ult double %9, 0.000000e+00
%12 = fcmp ugt double %9, 1.000000e+09
%13 = or i1 %10, %11
%14 = or i1 %12, %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%16 = fadd double %9, 0.000000e+00
store double %16, double* %15
%17 = fcmp ult double %16, 1.000000e+09
br i1 %17, label %25, label %18
18: ; preds = %1
%19 = fadd double %0, 1.000000e+00
%20 = fptosi double %19 to i64
%21 = sitofp i64 %20 to double
%22 = fcmp olt double %21, %0
%23 = or i1 %22, %14
store double %21, double* %7
%24 = fadd double %16, -1.000000e+09
store double %24, double* %15
br label %25
25: ; preds = %18, %1
%26 = phi i1 [ %23, %18 ], [ %14, %1 ]
br label %27
27: ; preds = %39, %25
br i1 %26, label %28, label %36
28: ; preds = %27
%29 = load i32, i32* @CHAR_BIT
%30 = sext i32 %29 to i64
%31 = shl nsw i64 %30, 3
%32 = add nsw i64 %31, -1
%33 = shl nsw i64 -1, %32
%34 = xor i64 %33, -1
%35 = sitofp i64 %34 to double
store double %35, double* %7
store double 0x41CDCD64FF800000, double* %15
br label %36
36: ; preds = %28, %27
store i64 0, i64* @errno
%37 = call i64 @nanosleep(%struct.timespec* %2, i32* null)
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp ne i64 %40, %41
%43 = icmp ne i64 %40, 0
%44 = and i1 %43, %42
br i1 %44, label %45, label %27
45: ; preds = %36, %39
%46 = phi i32 [ -1, %39 ], [ 0, %36 ]
ret i32 %46
}