#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;
unsigned int INT_MAX;
int _nanosleep(struct timespec *, struct timespec *);
scalar_t__ errno;
unsigned int __sleep(unsigned int seconds) {
  struct timespec time_to_sleep;
  struct timespec time_remaining;
  if (seconds > INT_MAX)
    return (seconds - INT_MAX + __sleep(INT_MAX));
  time_to_sleep.tv_sec = seconds;
  time_to_sleep.tv_nsec = 0;
  if (_nanosleep(&time_to_sleep, &time_remaining) != -1)
    return (0);
  if (errno != EINTR)
    return (seconds);
  return (time_remaining.tv_sec + (time_remaining.tv_nsec != 0));
}


x86 assembly (clang, -O0):
<__sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2eb3(%rip),%rcx
cmp    (%rcx),%eax
jbe    1169 <__sleep+0x49>
mov    -0x8(%rbp),%eax
mov    0x2ea1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
call   1040 <__sleep@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <__sleep+0xb4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <_nanosleep@plt>
cmp    $0xffffffff,%eax
je     1199 <__sleep+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <__sleep+0xb4>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
je     11be <__sleep+0x9e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <__sleep+0xb4>
mov    -0x28(%rbp),%eax
cmpq   $0x0,-0x20(%rbp)
setne  %cl
and    $0x1,%cl
movzbl %cl,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sleep>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
cmp    %ebx,%edi
jae    113f <__sleep+0x1f>
sub    %edi,%ebx
call   1040 <__sleep@plt>
add    %ebx,%eax
jmp    1186 <__sleep+0x66>
mov    %ebx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <_nanosleep@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1186 <__sleep+0x66>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e5d(%rip),%rdx
mov    %ebx,%eax
cmp    (%rdx),%rcx
jne    1186 <__sleep+0x66>
mov    (%rsp),%eax
cmpq   $0x1,0x8(%rsp)
sbb    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@INT_MAX = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @__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 = load i32, i32* @INT_MAX
%7 = icmp ult i32 %6, %0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = sub i32 %0, %6
%10 = tail call i32 @__sleep(i32 %6)
%11 = add i32 %9, %10
br label %29
12: ; preds = %1
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %0, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %14
%15 = call i32 @_nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %29
17: ; preds = %12
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
%28 = add i32 %23, %27
br label %29
29: ; preds = %17, %12, %21, %8
%30 = phi i32 [ %11, %8 ], [ %28, %21 ], [ 0, %12 ], [ %0, %17 ]
ret i32 %30
}