#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;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
unsigned sleep4(unsigned seconds) {
  struct timespec to_sleep;
  struct timespec unslept;
  to_sleep.tv_sec = seconds;
  to_sleep.tv_nsec = 0;
  if (nanosleep(&to_sleep, &unslept) < 0 && errno != EINTR) {
    return seconds;
  }
  return unslept.tv_sec;
}


x86 assembly (clang, -O0):
<sleep4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
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    $0x0,%rax
jge    1165 <sleep4+0x55>
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
je     1165 <sleep4+0x55>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    116b <sleep4+0x5b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep4>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %edi,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    114b <sleep4+0x3b>
mov    0x2ea4(%rip),%rax
mov    (%rax),%rax
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%rax
jne    114f <sleep4+0x3f>
mov    0x10(%rsp),%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sleep4(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 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %8
%9 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* %3
store i32 %16, i32* %2
br label %20
17: ; preds = %11, %1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %2
br label %20
20: ; preds = %17, %15
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sleep4(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 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %0, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %7
%8 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10, %1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %10, %14
%18 = phi i32 [ %16, %14 ], [ %0, %10 ]
ret i32 %18
}