#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 */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int nanosleep(struct timespec *, struct timespec *);
void ssgl_sleep(int seconds) {
  int rc;
  struct timespec req, rem;
  req.tv_sec = seconds;
  req.tv_nsec = 0;
  rem.tv_sec = 0;
  rem.tv_nsec = 0;
  do {
    rc = nanosleep(&req, &rem);
    if (rc == 0)
      break;
    if (rc < 0 && errno == -EINTR) {
      req = rem;
      rem.tv_sec = 0;
      rem.tv_nsec = 0;
      continue;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<ssgl_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1157 <ssgl_sleep+0x47>
jmp    11b5 <ssgl_sleep+0xa5>
cmpl   $0x0,-0x8(%rbp)
jge    11a1 <ssgl_sleep+0x91>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2e65(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11a1 <ssgl_sleep+0x91>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    11a6 <ssgl_sleep+0x96>
jmp    11a6 <ssgl_sleep+0x96>
mov    $0x1,%al
test   $0x1,%al
jne    1138 <ssgl_sleep+0x28>
jmp    11b5 <ssgl_sleep+0xa5>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_sleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,0x10(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a7 <ssgl_sleep+0x97>
mov    0x2e90(%rip),%r15
mov    0x2e81(%rip),%r12
lea    0x10(%rsp),%r14
mov    %rsp,%rbx
jmp    117f <ssgl_sleep+0x6f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a7 <ssgl_sleep+0x97>
test   %eax,%eax
jns    1170 <ssgl_sleep+0x60>
mov    (%r15),%eax
add    (%r12),%eax
jne    1170 <ssgl_sleep+0x60>
movups (%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
jmp    1170 <ssgl_sleep+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i32 0
@EINTR = global i32 0
define void @ssgl_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %10
br label %11
11: ; preds = %30, %1
%12 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br label %31
16: ; preds = %11
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = load i32, i32* @EINTR
%22 = sub nsw i32 0, %21
%23 = icmp eq i32 %20, %22
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = bitcast %struct.timespec* %4 to i8*
%26 = bitcast %struct.timespec* %5 to i8*
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %27
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %28
br label %30
29: ; preds = %19, %16
br label %30
30: ; preds = %29, %24
br i1 true, label %11, label %31
31: ; preds = %30, %15
ret void
}

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