#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 dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec const *, struct timespec *);
int nsleep(const struct timespec *req, struct timespec *rem) {
  struct timespec temp_rem;
  if (nanosleep(req, rem) == -1) {
    if (errno == EINTR) {
      nsleep(rem, &temp_rem);
    } else {
      return errno;
    }
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1189 <nsleep+0x69>
mov    0x2e93(%rip),%rax
mov    (%rax),%rax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
jne    1172 <nsleep+0x52>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <nsleep@plt>
jmp    1184 <nsleep+0x64>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    1190 <nsleep+0x70>
jmp    1190 <nsleep+0x70>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsleep>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
call   1040 <nanosleep@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1159 <nsleep+0x39>
mov    0x2ea3(%rip),%rax
mov    (%rax),%rax
mov    0x2e91(%rip),%rcx
cmp    (%rcx),%rax
jne    1159 <nsleep+0x39>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <nsleep@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @nsleep(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca %struct.timespec*
%6 = alloca %struct.timespec
store %struct.timespec* %0, %struct.timespec** %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load %struct.timespec*, %struct.timespec** %4
%8 = load %struct.timespec*, %struct.timespec** %5
%9 = call i32 @nanosleep(%struct.timespec* %7, %struct.timespec* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %22
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load %struct.timespec*, %struct.timespec** %5
%17 = call i32 @nsleep(%struct.timespec* %16, %struct.timespec* %6)
br label %21
18: ; preds = %11
%19 = load i64, i64* @errno
%20 = trunc i64 %19 to i32
store i32 %20, i32* %3
br label %23
21: ; preds = %15
br label %23
22: ; preds = %2
store i32 0, i32* %3
br label %23
23: ; preds = %18, %22, %21
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @nsleep(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = tail call i32 @nanosleep(%struct.timespec* %0, %struct.timespec* %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @nsleep(%struct.timespec* %1, %struct.timespec* %3)
br label %15
13: ; preds = %7
%14 = trunc i64 %8 to i32
br label %15
15: ; preds = %2, %11, %13
%16 = phi i32 [ undef, %11 ], [ %14, %13 ], [ 0, %2 ]
ret i32 %16
}