#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_sec;
  int tv_nsec;
};

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void nsleep(uint64_t period) {
  struct timespec req;
  struct timespec rem;
  int ret;
  if (period < 1000000000) {
    req.tv_sec = 0;
    req.tv_nsec = period;
  } else {
    req.tv_sec = period / 1000000000;
    req.tv_nsec = period % 1000000000;
  }
do_sleep:
  ret = nanosleep(&req, &rem);
  if (ret) {
    req.tv_sec = rem.tv_sec;
    req.tv_nsec = rem.tv_nsec;
    goto do_sleep;
  }
}


x86 assembly (clang, -O0):
<nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3b9aca00,-0x4(%rbp)
jge    113a <nsleep+0x2a>
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1156 <nsleep+0x46>
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
jmp    115b <nsleep+0x4b>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1186 <nsleep+0x76>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    115b <nsleep+0x4b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsleep>:
xor    %eax,%eax
cmp    $0x3b9aca00,%edi
jl     1132 <nsleep+0x22>
mov    %edi,%eax
shr    $0x9,%eax
imul   $0x44b83,%rax,%rax
shr    $0x27,%rax
imul   $0x3b9aca00,%eax,%ecx
sub    %ecx,%edi
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %eax,0x8(%rsp)
mov    %edi,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     1179 <nsleep+0x69>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jne    1160 <nsleep+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @nsleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 1000000000
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 0, i32* %9
%10 = load i32, i32* %2
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %10, i32* %11
br label %19
12: ; preds = %1
%13 = load i32, i32* %2
%14 = sdiv i32 %13, 1000000000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = srem i32 %16, 1000000000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %17, i32* %18
br label %19
19: ; preds = %12, %8
br label %20
20: ; preds = %24, %19
%21 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %29, i32* %30
br label %20
31: ; preds = %20
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @nsleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = icmp slt i32 %0, 1000000000
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = udiv i32 %0, 1000000000
%9 = urem i32 %0, 1000000000
br label %10
10: ; preds = %1, %7
%11 = phi i32 [ %8, %7 ], [ 0, %1 ]
%12 = phi i32 [ %9, %7 ], [ %0, %1 ]
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %11, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %12, i32* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %10
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %20
20: ; preds = %17, %20
%21 = load i32, i32* %18
store i32 %21, i32* %13
%22 = load i32, i32* %19
store i32 %22, i32* %14
%23 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %20
25: ; preds = %20, %10
ret void
}