#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 */
typedef unsigned int time_t;
struct timespec {
  unsigned int tv_sec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ seconds;

/* Variables and functions */
unsigned int MIN(unsigned int, unsigned int const);
scalar_t__ __nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ __set_errno(int);
int errno;
unsigned int __sleep(unsigned int seconds) {
  int save_errno = errno;
  const unsigned int max =
      (unsigned int)(((unsigned long int)(~((time_t)0))) >> 1);
  struct timespec ts = {0, 0};
  do {
    if (sizeof(ts.tv_sec) <= sizeof(seconds)) {
      ts.tv_sec += MIN(seconds, max);
      seconds -= (unsigned int)ts.tv_sec;
    } else {
      ts.tv_sec = (time_t)seconds;
      seconds = 0;
    }
    if (__nanosleep(&ts, &ts) < 0)
      return seconds + ts.tv_sec;
  } while (seconds > 0);
  __set_errno(save_errno);
  return 0;
}


x86 assembly (clang, -O0):
<__sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x7fffffff,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
mov    $0x7fffffff,%esi
call   1040 <MIN@plt>
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1050 <__nanosleep@plt>
cmp    $0x0,%rax
jge    11b0 <__sleep+0x70>
mov    -0x8(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <__sleep+0x8e>
jmp    11b5 <__sleep+0x75>
cmpl   $0x0,-0x8(%rbp)
ja     116e <__sleep+0x2e>
mov    -0xc(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sleep>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e9f(%rip),%rax
mov    (%rax),%r14d
movl   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x7fffffff,%esi
call   1040 <MIN@plt>
add    (%rsp),%eax
mov    %eax,(%rsp)
sub    %eax,%ebx
mov    %rbp,%rdi
mov    %rbp,%rsi
call   1050 <__nanosleep@plt>
test   %rax,%rax
js     1194 <__sleep+0x64>
test   %ebx,%ebx
jne    1160 <__sleep+0x30>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
xor    %ebx,%ebx
jmp    1197 <__sleep+0x67>
add    (%rsp),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32, i32 }
@errno = global i32 0
define i32 @__sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = load i32, i32* @errno
%4 = bitcast %struct.timespec* %2 to i8*
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
br label %6
6: ; preds = %17, %1
%7 = phi i32 [ %0, %1 ], [ %11, %17 ]
%8 = call i32 @MIN(i32 %7, i32 2147483647)
%9 = load i32, i32* %5
%10 = add i32 %9, %8
store i32 %10, i32* %5
%11 = sub i32 %7, %10
%12 = call i64 @__nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = load i32, i32* %5
%16 = add i32 %15, %11
br label %21
17: ; preds = %6
%18 = icmp eq i32 %11, 0
br i1 %18, label %19, label %6
19: ; preds = %17
%20 = call i32 @__set_errno(i32 %3)
br label %21
21: ; preds = %19, %14
%22 = phi i32 [ %16, %14 ], [ 0, %19 ]
ret i32 %22
}