#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 {
  unsigned long tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
void nsleep(unsigned long delay) {
  struct timespec deadline;
  clock_gettime(CLOCK_MONOTONIC, &deadline);
  deadline.tv_nsec += delay;
  if (deadline.tv_nsec >= 1E9) {
    deadline.tv_nsec -= 1E9;
    deadline.tv_sec++;
  }
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL);
}


x86 assembly (clang, -O0):
<nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movaps 0xecc(%rip),%xmm1
movq   -0x18(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xecb(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe84(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11ec <nsleep+0xcc>
movaps 0xe93(%rip),%xmm1
movq   -0x18(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe92(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe53(%rip),%xmm1
addsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe40(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <clock_nanosleep@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsleep>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2eaf(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
add    0x8(%rsp),%rbx
mov    %rbx,0x8(%rsp)
cmp    $0x3b9aca00,%rbx
jb     11a3 <nsleep+0x83>
movq   %rbx,%xmm0
punpckldq 0xea2(%rip),%xmm0
subpd  0xeaa(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
addsd  0xea6(%rip),%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xe9a(%rip),%xmm1
cvttsd2si %xmm1,%rdx
and    %rcx,%rdx
or     %rax,%rdx
mov    %rdx,0x8(%rsp)
addl   $0x1,0x10(%rsp)
mov    (%r14),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %ecx,%ecx
call   1040 <clock_nanosleep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define void @nsleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
store i64 %0, i64* %2
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %3)
%6 = load i64, i64* %2
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%8 = load i64, i64* %7
%9 = add i64 %8, %6
store i64 %9, i64* %7
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%11 = load i64, i64* %10
%12 = uitofp i64 %11 to double
%13 = fcmp oge double %12, 1.000000e+09
br i1 %13, label %14, label %23
14: ; preds = %1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%16 = load i64, i64* %15
%17 = uitofp i64 %16 to double
%18 = fsub double %17, 1.000000e+09
%19 = fptoui double %18 to i64
store i64 %19, i64* %15
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
store i32 %22, i32* %20
br label %23
23: ; preds = %14, %1
%24 = load i32, i32* @CLOCK_MONOTONIC
%25 = load i32, i32* @TIMER_ABSTIME
%26 = call i32 @clock_nanosleep(i32 %24, i32 %25, %struct.timespec* %3, i32* null)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
define void @nsleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = add i64 %7, %0
store i64 %8, i64* %6
%9 = icmp ugt i64 %8, 999999999
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = uitofp i64 %8 to double
%12 = fadd double %11, -1.000000e+09
%13 = fptoui double %12 to i64
store i64 %13, i64* %6
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
store i32 %16, i32* %14
br label %17
17: ; preds = %10, %1
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = load i32, i32* @TIMER_ABSTIME
%20 = call i32 @clock_nanosleep(i32 %18, i32 %19, %struct.timespec* %2, i32* null)
ret void
}