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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void intr_sleep(int sec) {
  struct timespec ts;
  ts.tv_sec = sec;
  ts.tv_nsec = 0;
  while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
    ;
}


x86 assembly (clang, -O0):
<intr_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x19(%rbp)
jne    115f <intr_sleep+0x4f>
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116f <intr_sleep+0x5f>
jmp    1174 <intr_sleep+0x64>
jmp    1129 <intr_sleep+0x19>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intr_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2eb1(%rip),%r14
mov    0x2ea2(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1158 <intr_sleep+0x48>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1140 <intr_sleep+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define void @intr_sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %5
br label %6
6: ; preds = %6, %1
%7 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%8 = icmp eq i32 %7, -1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %6, label %13
13: ; preds = %6
ret void
}