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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ perror(char *);
void safeSleep(int ns) {
  int ret;
  struct timespec req, rem;
  req.tv_sec = 0;
  req.tv_nsec = ns;
  while (1) {
    ret = nanosleep(&req, &rem);
    if (ret) {
      if (errno == EINTR) {
        req.tv_sec = rem.tv_sec;
        req.tv_nsec = rem.tv_nsec;
      } else
        perror("nanosleep");
    } else
      return;
  }
}


x86 assembly (clang, -O0):
<safeSleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1191 <safeSleep+0x71>
mov    0x2e86(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <safeSleep+0x60>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118c <safeSleep+0x6c>
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
jmp    1197 <safeSleep+0x77>
add    $0x30,%rsp
pop    %rbp
ret
jmp    1139 <safeSleep+0x19>

x86 assembly (clang, -O2):
<safeSleep>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
movq   $0x0,0x8(%rsp)
mov    %edi,(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a6 <safeSleep+0x86>
mov    0x2e8f(%rip),%r12
mov    0x2e80(%rip),%r13
mov    %rsp,%r14
lea    0x10(%rsp),%rbx
lea    0xe99(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    119c <safeSleep+0x7c>
mov    0x18(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jne    1170 <safeSleep+0x50>
jmp    11a6 <safeSleep+0x86>
mov    %r15,%rdi
call   1040 <perror@plt>
jmp    118b <safeSleep+0x6b>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"nanosleep\00"
define void @safeSleep(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 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %0, i32* %7
%8 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %1
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %13
13: ; preds = %10, %23
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i64, i64* %11
store i64 %18, i64* %6
%19 = load i32, i32* %12
store i32 %19, i32* %7
br label %23
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %23
22: ; preds = %23, %1
ret void
23: ; preds = %17, %20
%24 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %22, label %13
}