#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 scalar_t__ time_t;
struct timespec {
  long tv_nsec;
  scalar_t__ tv_sec;
};
struct sigaction {
  scalar_t__ sa_handler;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ seconds;

/* Variables and functions */
scalar_t__ MIN(unsigned int, unsigned int const);
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
scalar_t__ __glibc_unlikely(int);
unsigned int __nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ __set_errno(int);
scalar_t__ __sigaction(int /*<<< orphan*/, struct sigaction *,
                       struct sigaction *);
int /*<<< orphan*/ __sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ __sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ __sigismember(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ __sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/ *);
int errno;
unsigned int __sleep(unsigned int seconds) {
  const unsigned int max =
      (unsigned int)(((unsigned long int)(~((time_t)0))) >> 1);
  struct timespec ts;
  sigset_t set, oset;
  unsigned int result;
  if (__glibc_unlikely(seconds == 0)) {
    return 0;
  }
  ts.tv_sec = 0;
  ts.tv_nsec = 0;
again:
  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;
  }
  __sigemptyset(&set);
  __sigaddset(&set, SIGCHLD);
  if (__sigprocmask(SIG_BLOCK, &set, &oset))
    return -1;
  if (!__sigismember(&oset, SIGCHLD)) {
    int saved_errno;
    struct sigaction oact;
    __sigemptyset(&set);
    __sigaddset(&set, SIGCHLD);
    if (__sigaction(SIGCHLD, (struct sigaction *)NULL, &oact) < 0) {
      saved_errno = errno;
      (void)__sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
      __set_errno(saved_errno);
      return -1;
    }
    if (oact.sa_handler == SIG_IGN) {
      while (1) {
        result = __nanosleep(&ts, &ts);
        if (result != 0 || seconds == 0)
          break;
        if (sizeof(ts.tv_sec) <= sizeof(seconds)) {
          ts.tv_sec = MIN(seconds, max);
          seconds -= (unsigned int)ts.tv_nsec;
        }
      }
      saved_errno = errno;
      (void)__sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
      __set_errno(saved_errno);
      goto out;
    }
    (void)__sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
  }
  result = __nanosleep(&ts, &ts);
  if (result == 0 && seconds != 0)
    goto again;
out:
  if (result != 0)
    result = seconds + (unsigned int)ts.tv_sec + (ts.tv_nsec >= 500000000L);
  return result;
}


x86 assembly (clang, -O0):
<__sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     11b9 <__sleep+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1391 <__sleep+0x211>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <__sigemptyset@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   10a0 <__sigaddset@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <__sigprocmask@plt>
cmp    $0x0,%rax
je     121e <__sleep+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <__sleep+0x211>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1070 <__sigismember@plt>
cmp    $0x0,%eax
jne    1335 <__sleep+0x1b5>
lea    -0x24(%rbp),%rdi
call   1060 <__sigemptyset@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   10a0 <__sigaddset@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
call   1080 <__sigaction@plt>
cmp    $0x0,%rax
jge    12aa <__sleep+0x12a>
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <__sigprocmask@plt>
mov    -0x30(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <__sleep+0x211>
mov    -0x38(%rbp),%rax
mov    0x2d23(%rip),%rcx
cmp    (%rcx),%rax
jne    131f <__sleep+0x19f>
jmp    12c3 <__sleep+0x143>
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1040 <__nanosleep@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12e6 <__sleep+0x166>
cmpl   $0x0,-0x8(%rbp)
jne    12eb <__sleep+0x16b>
jmp    12f0 <__sleep+0x170>
jmp    12c3 <__sleep+0x143>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <__sigprocmask@plt>
mov    -0x30(%rbp),%edi
call   1030 <__set_errno@plt>
jmp    1362 <__sleep+0x1e2>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <__sigprocmask@plt>
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1040 <__nanosleep@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    135d <__sleep+0x1dd>
cmpl   $0x0,-0x8(%rbp)
je     135d <__sleep+0x1dd>
jmp    11c9 <__sleep+0x49>
jmp    1362 <__sleep+0x1e2>
cmpl   $0x0,-0x2c(%rbp)
je     138b <__sleep+0x20b>
mov    -0x8(%rbp),%eax
mov    -0x18(%rbp),%rcx
add    %ecx,%eax
cmpq   $0x1dcd6500,-0x20(%rbp)
1d
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__sleep>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
xor    %edi,%edi
test   %ebp,%ebp
sete   %dil
call   1050 <__glibc_unlikely@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1225 <__sleep+0xa5>
mov    %ebp,%eax
movq   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <__sigemptyset@plt>
mov    0x2e1a(%rip),%r14
mov    (%r14),%esi
mov    %rbp,%rdi
call   10a0 <__sigaddset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    %rbp,%rsi
call   1090 <__sigprocmask@plt>
test   %rax,%rax
je     11f1 <__sleep+0x71>
mov    $0xffffffff,%ebx
jmp    1225 <__sleep+0xa5>
mov    (%r14),%esi
mov    %rsp,%rdi
call   1070 <__sigismember@plt>
test   %eax,%eax
je     1230 <__sleep+0xb0>
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1040 <__nanosleep@plt>
mov    %eax,%ebp
test   %ebp,%ebp
je     1225 <__sleep+0xa5>
xor    %ebx,%ebx
cmpq   $0x1dcd6500,0x8(%rsp)
cd 1d
setge  %bl
add    0x10(%rsp),%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <__sigemptyset@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   10a0 <__sigaddset@plt>
mov    (%r14),%edi
lea    0x18(%rsp),%rdx
xor    %esi,%esi
call   1080 <__sigaction@plt>
test   %rax,%rax
js     12ae <__sleep+0x12e>
mov    0x18(%rsp),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
jne    12d6 <__sleep+0x156>
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1040 <__nanosleep@plt>
mov    %eax,%ebp
mov    0x2d45(%rip),%rax
mov    (%rax),%r14d
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1090 <__sigprocmask@plt>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
test   %ebp,%ebp
jne    1213 <__sleep+0x93>
jmp    1225 <__sleep+0xa5>
mov    0x2d13(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1090 <__sigprocmask@plt>
mov    %ebx,%edi
call   1030 <__set_errno@plt>
jmp    11ea <__sleep+0x6a>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1090 <__sigprocmask@plt>
jmp    1200 <__sleep+0x80>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
%struct.sigaction = type { i64 }
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@errno = global i32 0
@SIG_SETMASK = global i32 0
@SIG_IGN = global i64 0
define i32 @__sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sigaction
store i32 %0, i32* %3
store i32 -1, i32* %4
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i64 @__glibc_unlikely(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %97
17: ; preds = %1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 0, i64* %19
br label %20
20: ; preds = %79, %17
%21 = load i32, i32* %3
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 %22, i64* %23
store i32 0, i32* %3
%24 = call i32 @__sigemptyset(i32* %6)
%25 = load i32, i32* @SIGCHLD
%26 = call i32 @__sigaddset(i32* %6, i32 %25)
%27 = load i32, i32* @SIG_BLOCK
%28 = call i64 @__sigprocmask(i32 %27, i32* %6, i32* %7)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 -1, i32* %2
br label %97
31: ; preds = %20
%32 = load i32, i32* @SIGCHLD
%33 = call i32 @__sigismember(i32* %7, i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %72, label %35
35: ; preds = %31
%36 = call i32 @__sigemptyset(i32* %6)
%37 = load i32, i32* @SIGCHLD
%38 = call i32 @__sigaddset(i32* %6, i32 %37)
%39 = load i32, i32* @SIGCHLD
%40 = call i64 @__sigaction(i32 %39, %struct.sigaction* null, %struct.sigaction* %10)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %35
%43 = load i32, i32* @errno
store i32 %43, i32* %9
%44 = load i32, i32* @SIG_SETMASK
%45 = call i64 @__sigprocmask(i32 %44, i32* %7, i32* null)
%46 = load i32, i32* %9
%47 = call i32 @__set_errno(i32 %46)
store i32 -1, i32* %2
br label %97
48: ; preds = %35
%49 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 0
%50 = load i64, i64* %49
%51 = load i64, i64* @SIG_IGN
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %69
53: ; preds = %48
br label %54
54: ; preds = %53, %62
%55 = call i32 @__nanosleep(%struct.timespec* %5, %struct.timespec* %5)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp ne i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = load i32, i32* %3
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %58, %54
br label %63
62: ; preds = %58
br label %54
63: ; preds = %61
%64 = load i32, i32* @errno
store i32 %64, i32* %9
%65 = load i32, i32* @SIG_SETMASK
%66 = call i64 @__sigprocmask(i32 %65, i32* %7, i32* null)
%67 = load i32, i32* %9
%68 = call i32 @__set_errno(i32 %67)
br label %81
69: ; preds = %48
%70 = load i32, i32* @SIG_SETMASK
%71 = call i64 @__sigprocmask(i32 %70, i32* %7, i32* null)
br label %72
72: ; preds = %69, %31
%73 = call i32 @__nanosleep(%struct.timespec* %5, %struct.timespec* %5)
store i32 %73, i32* %8
%74 = load i32, i32* %8
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = load i32, i32* %3
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %76
br label %20
80: ; preds = %76, %72
br label %81
81: ; preds = %80, %63
%82 = load i32, i32* %8
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %95
84: ; preds = %81
%85 = load i32, i32* %3
%86 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%87 = load i64, i64* %86
%88 = trunc i64 %87 to i32
%89 = add i32 %85, %88
%90 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%91 = load i64, i64* %90
%92 = icmp sge i64 %91, 500000000
%93 = zext i1 %92 to i32
%94 = add i32 %89, %93
store i32 %94, i32* %8
br label %95
95: ; preds = %84, %81
%96 = load i32, i32* %8
store i32 %96, i32* %2
br label %97
97: ; preds = %95, %42, %30, %16
%98 = load i32, i32* %2
ret i32 %98
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
%struct.sigaction = type { i64 }
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@errno = global i32 0
@SIG_SETMASK = global i32 0
@SIG_IGN = global i64 0
define i32 @__sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sigaction
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = icmp eq i32 %0, 0
%10 = zext i1 %9 to i32
%11 = tail call i64 @__glibc_unlikely(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %67
13: ; preds = %1
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%16 = zext i32 %0 to i64
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 0, i64* %17
store i64 %16, i64* %14
%18 = call i32 @__sigemptyset(i32* %3)
%19 = load i32, i32* @SIGCHLD
%20 = call i32 @__sigaddset(i32* %3, i32 %19)
%21 = load i32, i32* @SIG_BLOCK
%22 = call i64 @__sigprocmask(i32 %21, i32* %3, i32* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %67
24: ; preds = %13
%25 = load i32, i32* @SIGCHLD
%26 = call i32 @__sigismember(i32* %4, i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %55
28: ; preds = %24
%29 = bitcast %struct.sigaction* %5 to i8*
%30 = call i32 @__sigemptyset(i32* %3)
%31 = load i32, i32* @SIGCHLD
%32 = call i32 @__sigaddset(i32* %3, i32 %31)
%33 = load i32, i32* @SIGCHLD
%34 = call i64 @__sigaction(i32 %33, %struct.sigaction* null, %struct.sigaction* %5)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @errno
%38 = load i32, i32* @SIG_SETMASK
%39 = call i64 @__sigprocmask(i32 %38, i32* %4, i32* null)
%40 = call i32 @__set_errno(i32 %37)
br label %67
41: ; preds = %28
%42 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @SIG_IGN
%45 = icmp eq i64 %43, %44
br i1 %45, label %49, label %46
46: ; preds = %41
%47 = load i32, i32* @SIG_SETMASK
%48 = call i64 @__sigprocmask(i32 %47, i32* %4, i32* null)
br label %55
49: ; preds = %41
%50 = call i32 @__nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%51 = load i32, i32* @errno
%52 = load i32, i32* @SIG_SETMASK
%53 = call i64 @__sigprocmask(i32 %52, i32* %4, i32* null)
%54 = call i32 @__set_errno(i32 %51)
br label %57
55: ; preds = %46, %24
%56 = call i32 @__nanosleep(%struct.timespec* %2, %struct.timespec* %2)
br label %57
57: ; preds = %49, %55
%58 = phi i32 [ %56, %55 ], [ %50, %49 ]
%59 = icmp eq i32 %58, 0
br i1 %59, label %67, label %60
60: ; preds = %57
%61 = load i64, i64* %14
%62 = trunc i64 %61 to i32
%63 = load i64, i64* %15
%64 = icmp sgt i64 %63, 499999999
%65 = zext i1 %64 to i32
%66 = add i32 %65, %62
br label %67
67: ; preds = %36, %57, %60, %13, %1
%68 = phi i32 [ 0, %1 ], [ -1, %13 ], [ %66, %60 ], [ 0, %57 ], [ -1, %36 ]
ret i32 %68
}