#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;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void my_nanosleep(const struct timespec *ts) {
  struct timespec rqt, rmt;
  for (rqt = *ts; nanosleep(&rqt, &rmt) < 0 && errno == EINTR; rqt = rmt)
    ;
}


x86 assembly (clang, -O0):
<my_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
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)
jge    115e <my_nanosleep+0x4e>
mov    0x2e95(%rip),%rax
mov    (%rax),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116e <my_nanosleep+0x5e>
jmp    117e <my_nanosleep+0x6e>
jmp    1173 <my_nanosleep+0x63>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1125 <my_nanosleep+0x15>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_nanosleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    (%rdi),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    1181 <my_nanosleep+0x71>
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%r15)
jne    1181 <my_nanosleep+0x71>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    1181 <my_nanosleep+0x71>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1160 <my_nanosleep+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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