#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;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void hardsleep(int secs) {
  struct timespec ts, rem;
  ts.tv_sec = secs;
  ts.tv_nsec = 0;
  while (nanosleep(&ts, &rem) < 0 && errno == EINTR)
    ts = rem;
}


x86 assembly (clang, -O0):
<hardsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jge    1162 <hardsleep+0x52>
mov    0x2e91(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1172 <hardsleep+0x62>
jmp    1187 <hardsleep+0x77>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1129 <hardsleep+0x19>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hardsleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    1182 <hardsleep+0x72>
mov    0x2ea0(%rip),%r15
mov    0x2e91(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%r15)
jne    1182 <hardsleep+0x72>
mov    %rsp,%r14
lea    0x18(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    1182 <hardsleep+0x72>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1160 <hardsleep+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define void @hardsleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %7
br label %8
8: ; preds = %17, %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 %20
17: ; preds = %15
%18 = bitcast %struct.timespec* %3 to i8*
%19 = bitcast %struct.timespec* %4 to i8*
br label %8
20: ; preds = %15
ret void
}

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