#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 */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void rc_nanosleep(uint64_t ns) {
  struct timespec req, rem;
  req.tv_sec = ns / 1000000000;
  req.tv_nsec = ns % 1000000000;
  errno = 0;
  while (nanosleep(&req, &rem) && errno == EINTR) {
    req.tv_sec = rem.tv_sec;
    req.tv_nsec = rem.tv_nsec;
  }
  return;
}


x86 assembly (clang, -O0):
<rc_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
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)
mov    0x2ea2(%rip),%rax
movq   $0x0,(%rax)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     117e <rc_nanosleep+0x6e>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118e <rc_nanosleep+0x7e>
jmp    119f <rc_nanosleep+0x8f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1145 <rc_nanosleep+0x35>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rc_nanosleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movslq %edi,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3b9aca00,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0xc(%rsp)
mov    0x2e97(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     11a3 <rc_nanosleep+0x93>
mov    0x2e6d(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%r15)
jne    11a3 <rc_nanosleep+0x93>
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   %rax,%rax
je     11a3 <rc_nanosleep+0x93>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1180 <rc_nanosleep+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @rc_nanosleep(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 = sdiv i32 %0, 1000000000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %6, i32* %7
%8 = srem i32 %0, 1000000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %8, i32* %9
store i64 0, i64* @errno
%10 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%11 = icmp ne i64 %10, 0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %16, label %28
16: ; preds = %1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %19
19: ; preds = %16, %19
%20 = load i32, i32* %17
store i32 %20, i32* %7
%21 = load i32, i32* %18
store i32 %21, i32* %9
%22 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%23 = icmp ne i64 %22, 0
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %19, label %28
28: ; preds = %19, %1
ret void
}