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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ __set_errno(int);
scalar_t__ __sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ __sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ __sigismember(int /*<<< orphan*/ *, int /*<<< orphan*/);
int errno;
unsigned int nanosleep(struct timespec *, struct timespec *);
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     struct sigaction *);
scalar_t__ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                       int /*<<< orphan*/ *);
unsigned int sleep(unsigned int seconds) {
  struct timespec ts = {.tv_sec = (long int)seconds, .tv_nsec = 0};
  sigset_t set, oset;
  unsigned int result;
  if (seconds == 0)
    return 0;
  if (__sigemptyset(&set) < 0 || __sigaddset(&set, SIGCHLD) < 0 ||
      sigprocmask(SIG_BLOCK, &set, &oset))
    return -1;
  if (!__sigismember(&oset, SIGCHLD)) {
    int saved_errno;
    struct sigaction oact;
    if (__sigemptyset(&set) < 0 || __sigaddset(&set, SIGCHLD) < 0)
      return -1;
    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) {
      result = nanosleep(&ts, &ts);
      saved_errno = errno;
      (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
      __set_errno(saved_errno);
    } else {
      (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
      result = nanosleep(&ts, &ts);
    }
  } else
    result = nanosleep(&ts, &ts);
  if (result != 0)
    result = (unsigned int)ts.tv_sec + (ts.tv_nsec >= 500000000L);
  return result;
}


x86 assembly (clang, -O0):
<sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a0 <sleep+0x30>
movl   $0x0,-0x4(%rbp)
jmp    135c <sleep+0x1ec>
lea    -0x1c(%rbp),%rdi
call   1070 <__sigemptyset@plt>
cmp    $0x0,%rax
jl     11ef <sleep+0x7f>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdi
call   1090 <__sigaddset@plt>
cmp    $0x0,%rax
jl     11ef <sleep+0x7f>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <sigprocmask@plt>
cmp    $0x0,%rax
je     11fb <sleep+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <sleep+0x1ec>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1080 <__sigismember@plt>
cmp    $0x0,%eax
jne    1323 <sleep+0x1b3>
lea    -0x1c(%rbp),%rdi
call   1070 <__sigemptyset@plt>
cmp    $0x0,%rax
jl     1245 <sleep+0xd5>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdi
call   1090 <__sigaddset@plt>
cmp    $0x0,%rax
jge    1251 <sleep+0xe1>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <sleep+0x1ec>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1050 <sigaction@plt>
cmp    $0x0,%rax
jge    12a7 <sleep+0x137>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
mov    -0x28(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <sleep+0x1ec>
mov    -0x30(%rbp),%rax
mov    0x2d26(%rip),%rcx
cmp    (%rcx),%rax
jne    12f9 <sleep+0x189>
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1060 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
mov    -0x28(%rbp),%edi
call   1030 <__set_errno@plt>
jmp    131e <sleep+0x1ae>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1060 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
jmp    1332 <sleep+0x1c2>
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1060 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1356 <sleep+0x1e6>
mov    -0x18(%rbp),%rax
cmpq   $0x1dcd6500,-0x10(%rbp)
1d
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%eax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
xor    %ebx,%ebx
test   %edi,%edi
je     11d6 <sleep+0x66>
lea    0x4(%rsp),%rdi
call   1070 <__sigemptyset@plt>
test   %rax,%rax
js     11d1 <sleep+0x61>
mov    0x2e3c(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0x4(%rsp),%rdi
call   1090 <__sigaddset@plt>
test   %rax,%rax
js     11d1 <sleep+0x61>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <sigprocmask@plt>
test   %rax,%rax
je     11e1 <sleep+0x71>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x0(%rbp),%esi
mov    %rsp,%rdi
call   1080 <__sigismember@plt>
test   %eax,%eax
je     1217 <sleep+0xa7>
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1060 <nanosleep@plt>
mov    %eax,%ebp
test   %ebp,%ebp
je     11d6 <sleep+0x66>
xor    %ebx,%ebx
cmpq   $0x1dcd6500,0x10(%rsp)
cd 1d
setge  %bl
add    0x8(%rsp),%ebx
jmp    11d6 <sleep+0x66>
lea    0x4(%rsp),%rdi
call   1070 <__sigemptyset@plt>
test   %rax,%rax
js     11d1 <sleep+0x61>
mov    0x0(%rbp),%esi
lea    0x4(%rsp),%rdi
call   1090 <__sigaddset@plt>
test   %rax,%rax
js     11d1 <sleep+0x61>
mov    0x0(%rbp),%edi
lea    0x18(%rsp),%rdx
xor    %esi,%esi
call   1050 <sigaction@plt>
test   %rax,%rax
js     129e <sleep+0x12e>
mov    0x18(%rsp),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    12c6 <sleep+0x156>
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1060 <nanosleep@plt>
mov    %eax,%ebp
mov    0x2d55(%rip),%rax
mov    (%rax),%r14d
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
test   %ebp,%ebp
jne    1203 <sleep+0x93>
jmp    11d6 <sleep+0x66>
mov    0x2d23(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
mov    %ebx,%edi
call   1030 <__set_errno@plt>
jmp    11d1 <sleep+0x61>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
jmp    11f0 <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 %struct.timespec
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sigaction
store i32 %0, i32* %3
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%11 = load i32, i32* %3
%12 = zext i32 %11 to i64
store i64 %12, i64* %10
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %84
17: ; preds = %1
%18 = call i64 @__sigemptyset(i32* %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %17
%21 = load i32, i32* @SIGCHLD
%22 = call i64 @__sigaddset(i32* %5, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @SIG_BLOCK
%26 = call i64 @sigprocmask(i32 %25, i32* %5, i32* %6)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24, %20, %17
store i32 -1, i32* %2
br label %84
29: ; preds = %24
%30 = load i32, i32* @SIGCHLD
%31 = call i32 @__sigismember(i32* %6, i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %68, label %33
33: ; preds = %29
%34 = call i64 @__sigemptyset(i32* %5)
%35 = icmp slt i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i32, i32* @SIGCHLD
%38 = call i64 @__sigaddset(i32* %5, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36, %33
store i32 -1, i32* %2
br label %84
41: ; preds = %36
%42 = load i32, i32* @SIGCHLD
%43 = call i64 @sigaction(i32 %42, %struct.sigaction* null, %struct.sigaction* %9)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @errno
store i32 %46, i32* %8
%47 = load i32, i32* @SIG_SETMASK
%48 = call i64 @sigprocmask(i32 %47, i32* %6, i32* null)
%49 = load i32, i32* %8
%50 = call i32 @__set_errno(i32 %49)
store i32 -1, i32* %2
br label %84
51: ; preds = %41
%52 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
%53 = load i64, i64* %52
%54 = load i64, i64* @SIG_IGN
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %63
56: ; preds = %51
%57 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %57, i32* %7
%58 = load i32, i32* @errno
store i32 %58, i32* %8
%59 = load i32, i32* @SIG_SETMASK
%60 = call i64 @sigprocmask(i32 %59, i32* %6, i32* null)
%61 = load i32, i32* %8
%62 = call i32 @__set_errno(i32 %61)
br label %67
63: ; preds = %51
%64 = load i32, i32* @SIG_SETMASK
%65 = call i64 @sigprocmask(i32 %64, i32* %6, i32* null)
%66 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %66, i32* %7
br label %67
67: ; preds = %63, %56
br label %70
68: ; preds = %29
%69 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %69, i32* %7
br label %70
70: ; preds = %68, %67
%71 = load i32, i32* %7
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %82
73: ; preds = %70
%74 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%75 = load i64, i64* %74
%76 = trunc i64 %75 to i32
%77 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%78 = load i64, i64* %77
%79 = icmp sge i64 %78, 500000000
%80 = zext i1 %79 to i32
%81 = add i32 %76, %80
store i32 %81, i32* %7
br label %82
82: ; preds = %73, %70
%83 = load i32, i32* %7
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %45, %40, %28, %16
%85 = load i32, i32* %2
ret i32 %85
}

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 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%8 = zext i32 %0 to i64
store i64 %8, i64* %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %9
%10 = bitcast i32* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = icmp eq i32 %0, 0
br i1 %12, label %75, label %13
13: ; preds = %1
%14 = call i64 @__sigemptyset(i32* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %75, label %16
16: ; preds = %13
%17 = load i32, i32* @SIGCHLD
%18 = call i64 @__sigaddset(i32* %3, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %75, label %20
20: ; preds = %16
%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 %75
24: ; preds = %20
%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 %63
28: ; preds = %24
%29 = bitcast %struct.sigaction* %5 to i8*
%30 = call i64 @__sigemptyset(i32* %3)
%31 = icmp slt i64 %30, 0
br i1 %31, label %60, label %32
32: ; preds = %28
%33 = load i32, i32* @SIGCHLD
%34 = call i64 @__sigaddset(i32* %3, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %60, label %36
36: ; preds = %32
%37 = load i32, i32* @SIGCHLD
%38 = call i64 @sigaction(i32 %37, %struct.sigaction* null, %struct.sigaction* %5)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = load i32, i32* @SIG_SETMASK
%43 = call i64 @sigprocmask(i32 %42, i32* %4, i32* null)
%44 = call i32 @__set_errno(i32 %41)
br label %60
45: ; preds = %36
%46 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %5, i64 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @SIG_IGN
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%52 = load i32, i32* @errno
%53 = load i32, i32* @SIG_SETMASK
%54 = call i64 @sigprocmask(i32 %53, i32* %4, i32* null)
%55 = call i32 @__set_errno(i32 %52)
br label %61
56: ; preds = %45
%57 = load i32, i32* @SIG_SETMASK
%58 = call i64 @sigprocmask(i32 %57, i32* %4, i32* null)
%59 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
br label %61
60: ; preds = %40, %32, %28
br label %75
61: ; preds = %50, %56
%62 = phi i32 [ %51, %50 ], [ %59, %56 ]
br label %65
63: ; preds = %24
%64 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
br label %65
65: ; preds = %61, %63
%66 = phi i32 [ %64, %63 ], [ %62, %61 ]
%67 = icmp eq i32 %66, 0
br i1 %67, label %75, label %68
68: ; preds = %65
%69 = load i64, i64* %7
%70 = trunc i64 %69 to i32
%71 = load i64, i64* %9
%72 = icmp sgt i64 %71, 499999999
%73 = zext i1 %72 to i32
%74 = add i32 %73, %70
br label %75
75: ; preds = %60, %65, %68, %13, %16, %20, %1
%76 = phi i32 [ 0, %1 ], [ -1, %20 ], [ -1, %16 ], [ -1, %13 ], [ %74, %68 ], [ 0, %65 ], [ -1, %60 ]
ret i32 %76
}