#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 {
  unsigned int tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
unsigned int sleep(unsigned int seconds) {
  struct timespec t;
  if ((int)seconds < 0)
    seconds = 0x7fffffff;
  t.tv_sec = seconds;
  t.tv_nsec = 0;
  if (!nanosleep(&t, &t))
    return 0;
  if (errno == EINTR)
    return t.tv_sec;
  return -1;
}


x86 assembly (clang, -O0):
<sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    112c <sleep+0x1c>
movl   $0x7fffffff,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0x0,%eax
jne    115b <sleep+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    1187 <sleep+0x77>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <sleep+0x70>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1187 <sleep+0x77>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep>:
sub    $0x18,%rsp
cmp    $0x7fffffff,%edi
mov    $0x7fffffff,%eax
cmovb  %edi,%eax
mov    %eax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     1164 <sleep+0x54>
mov    0x2e99(%rip),%rax
mov    (%rax),%rcx
mov    0x2e87(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    115f <sleep+0x4f>
mov    0x8(%rsp),%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
ret

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

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