#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__ __glibc_unlikely(int);
scalar_t__ __nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ __set_errno(int);
int errno;
unsigned int __sleep(unsigned int seconds) {
  if (__glibc_unlikely(seconds == 0)) {
    return 0;
  }
  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)
cmpl   $0x0,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     1182 <__sleep+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1205 <__sleep+0xb5>
mov    0x2e57(%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    11e7 <__sleep+0x97>
mov    -0x8(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1205 <__sleep+0xb5>
jmp    11ec <__sleep+0x9c>
cmpl   $0x0,-0x8(%rbp)
ja     11a5 <__sleep+0x55>
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
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1060 <__glibc_unlikely@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11b9 <__sleep+0x79>
mov    0x2e78(%rip),%rax
mov    (%rax),%r14d
movl   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rbp
xchg   %ax,%ax
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     11b4 <__sleep+0x74>
test   %ebx,%ebx
jne    1180 <__sleep+0x40>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
xor    %eax,%eax
jmp    11b9 <__sleep+0x79>
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* %3
%8 = icmp eq i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i64 @__glibc_unlikely(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %40
13: ; preds = %1
%14 = load i32, i32* @errno
store i32 %14, i32* %4
store i32 2147483647, i32* %5
%15 = bitcast %struct.timespec* %6 to i8*
br label %16
16: ; preds = %34, %13
%17 = load i32, i32* %3
%18 = call i32 @MIN(i32 %17, i32 2147483647)
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = add i32 %20, %18
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* %3
%25 = sub i32 %24, %23
store i32 %25, i32* %3
%26 = call i64 @__nanosleep(%struct.timespec* %6, %struct.timespec* %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %16
%29 = load i32, i32* %3
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add i32 %29, %31
store i32 %32, i32* %2
br label %40
33: ; preds = %16
br label %34
34: ; preds = %33
%35 = load i32, i32* %3
%36 = icmp ugt i32 %35, 0
br i1 %36, label %16, label %37
37: ; preds = %34
%38 = load i32, i32* %4
%39 = call i32 @__set_errno(i32 %38)
store i32 0, i32* %2
br label %40
40: ; preds = %37, %28, %12
%41 = load i32, i32* %2
ret i32 %41
}

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