#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;
};

/* Variables and functions */
unsigned int nanosleep(struct timespec *, struct timespec *);
unsigned int sleep(unsigned int seconds) {
  unsigned int res;
  struct timespec ts = {.tv_sec = (long int)seconds, .tv_nsec = 0};
  res = nanosleep(&ts, &ts);
  if (res)
    res = (unsigned int)ts.tv_sec + (ts.tv_nsec >= 500000000L);
  return res;
}


x86 assembly (clang, -O0):
<sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     115d <sleep+0x4d>
mov    -0x18(%rbp),%rax
cmpq   $0x1dcd6500,-0x10(%rbp)
1d
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep>:
sub    $0x18,%rsp
mov    %edi,%eax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     114c <sleep+0x3c>
xor    %eax,%eax
cmpq   $0x1dcd6500,0x10(%rsp)
cd 1d
setge  %al
add    0x8(%rsp),%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define i32 @sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%6 = load i32, i32* %2
%7 = zext i32 %6 to i64
store i64 %7, i64* %5
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %8
%9 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %1
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i32
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%17 = load i64, i64* %16
%18 = icmp sge i64 %17, 500000000
%19 = zext i1 %18 to i32
%20 = add i32 %15, %19
store i32 %20, i32* %3
br label %21
21: ; preds = %12, %1
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
define i32 @sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%5 = zext i32 %0 to i64
store i64 %5, i64* %4
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %6
%7 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %1
%10 = load i64, i64* %4
%11 = trunc i64 %10 to i32
%12 = load i64, i64* %6
%13 = icmp sgt i64 %12, 499999999
%14 = zext i1 %13 to i32
%15 = add i32 %14, %11
br label %16
16: ; preds = %9, %1
%17 = phi i32 [ %15, %9 ], [ 0, %1 ]
ret i32 %17
}