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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int EINTR;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    int /*<<< orphan*/ *);
int nsleep(uint64_t ns) {
  struct timespec ts;
  int ds;
  int dns;
  int r;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  ds = ns / (1000000000);
  dns = ns - ds * (1000000000);
  ts.tv_sec += ds;
  ts.tv_nsec += dns;
  if (ts.tv_nsec >= (1000000000)) {
    ++ts.tv_sec;
    ts.tv_nsec -= (1000000000);
  }
  do {
    r = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
  } while (r == EINTR);
  return r;
}


x86 assembly (clang, -O0):
<nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x3b9aca00,-0x1c(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3b9aca00,-0x18(%rbp)
jl     1193 <nsleep+0x73>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x18(%rbp)
jmp    1198 <nsleep+0x78>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <clock_nanosleep@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%eax
je     1198 <nsleep+0x78>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2eac(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
movslq %ebx,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
imul   $0xc4653600,%ecx,%edx
add    %edx,%eax
movslq %ecx,%rcx
add    0x10(%rsp),%rcx
mov    %rcx,0x10(%rsp)
add    0x8(%rsp),%eax
mov    %eax,0x8(%rsp)
cmp    $0x3b9aca00,%eax
jl     118e <nsleep+0x6e>
add    $0x1,%rcx
mov    %rcx,0x10(%rsp)
add    $0xc4653600,%eax
mov    %eax,0x8(%rsp)
mov    0x2e53(%rip),%r15
lea    0x8(%rsp),%r12
mov    0x2e2f(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    (%r15),%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1040 <clock_nanosleep@plt>
cmp    (%rbx),%eax
je     11b0 <nsleep+0x90>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@EINTR = global i32 0
define i32 @nsleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = load i32, i32* %2
%10 = sdiv i32 %9, 1000000000
store i32 %10, i32* %4
%11 = load i32, i32* %2
%12 = load i32, i32* %4
%13 = mul nsw i32 %12, 1000000000
%14 = sub nsw i32 %11, %13
store i32 %14, i32* %5
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%18 = load i64, i64* %17
%19 = add nsw i64 %18, %16
store i64 %19, i64* %17
%20 = load i32, i32* %5
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, %20
store i32 %23, i32* %21
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sge i32 %25, 1000000000
br i1 %26, label %27, label %34
27: ; preds = %1
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%29 = load i64, i64* %28
%30 = add nsw i64 %29, 1
store i64 %30, i64* %28
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sub nsw i32 %32, 1000000000
store i32 %33, i32* %31
br label %34
34: ; preds = %27, %1
br label %35
35: ; preds = %39, %34
%36 = load i32, i32* @CLOCK_MONOTONIC
%37 = load i32, i32* @TIMER_ABSTIME
%38 = call i32 @clock_nanosleep(i32 %36, i32 %37, %struct.timespec* %3, i32* null)
store i32 %38, i32* %6
br label %39
39: ; preds = %35
%40 = load i32, i32* %6
%41 = load i32, i32* @EINTR
%42 = icmp eq i32 %40, %41
br i1 %42, label %35, label %43
43: ; preds = %39
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@EINTR = global i32 0
define i32 @nsleep(i32 %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 = sdiv i32 %0, 1000000000
%7 = mul nsw i32 %6, -1000000000
%8 = add i32 %7, %0
%9 = sext i32 %6 to i64
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%11 = load i64, i64* %10
%12 = add nsw i64 %11, %9
store i64 %12, i64* %10
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %8, %14
store i32 %15, i32* %13
%16 = icmp sgt i32 %15, 999999999
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = add nsw i64 %12, 1
store i64 %18, i64* %10
%19 = add nsw i32 %15, -1000000000
store i32 %19, i32* %13
br label %20
20: ; preds = %17, %1
br label %21
21: ; preds = %20, %21
%22 = load i32, i32* @CLOCK_MONOTONIC
%23 = load i32, i32* @TIMER_ABSTIME
%24 = call i32 @clock_nanosleep(i32 %22, i32 %23, %struct.timespec* %2, i32* null)
%25 = load i32, i32* @EINTR
%26 = icmp eq i32 %24, %25
br i1 %26, label %21, label %27
27: ; preds = %21
ret i32 %24
}