#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 epoll_event {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
int epoll_wait(int, struct epoll_event *, int, int);
scalar_t__ errno;
int signalSafeEpollWait(int epfd, struct epoll_event *events, int maxevents,
                        int timeout) {
  bool interrupted;
  int retVal;
  do {
    retVal = epoll_wait(epfd, events, maxevents, timeout);
    interrupted = ((retVal < 0) && (errno == EINTR));
    timeout = 0;
  } while (interrupted);
  return retVal;
}


x86 assembly (clang, -O0):
<signalSafeEpollWait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <epoll_wait@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jge    1163 <signalSafeEpollWait+0x53>
mov    0x2e90(%rip),%rax
mov    (%rax),%rax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1125 <signalSafeEpollWait+0x15>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalSafeEpollWait>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <epoll_wait@plt>
test   %eax,%eax
jns    115c <signalSafeEpollWait+0x4c>
mov    0x2eb0(%rip),%r15
mov    0x2ea1(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rcx
cmp    %rcx,(%r15)
jne    115c <signalSafeEpollWait+0x4c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <epoll_wait@plt>
test   %eax,%eax
js     1140 <signalSafeEpollWait+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @signalSafeEpollWait(i32 %0, %struct.epoll_event* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.epoll_event*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store %struct.epoll_event* %1, %struct.epoll_event** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
br label %11
11: ; preds = %26, %4
%12 = load i32, i32* %5
%13 = load %struct.epoll_event*, %struct.epoll_event** %6
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = call i32 @epoll_wait(i32 %12, %struct.epoll_event* %13, i32 %14, i32 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %11
%24 = phi i1 [ false, %11 ], [ %22, %19 ]
%25 = zext i1 %24 to i32
store i32 %25, i32* %9
store i32 0, i32* %8
br label %26
26: ; preds = %23
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, 0
br i1 %28, label %11, label %29
29: ; preds = %26
%30 = load i32, i32* %10
ret i32 %30
}

LLVM IR (-O2):
%struct.epoll_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @signalSafeEpollWait(i32 %0, %struct.epoll_event* %1, i32 %2, i32 %3) {
%5 = tail call i32 @epoll_wait(i32 %0, %struct.epoll_event* %1, i32 %2, i32 %3)
%6 = icmp slt i32 %5, 0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %11, label %18
11: ; preds = %4, %11
%12 = tail call i32 @epoll_wait(i32 %0, %struct.epoll_event* %1, i32 %2, i32 0)
%13 = icmp slt i32 %12, 0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11, %4
%19 = phi i32 [ %5, %4 ], [ %12, %11 ]
ret i32 %19
}