#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 sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ errno;
int signalSafeAccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
  bool interrupted;
  int retVal;
  do {
    retVal = accept(sockfd, addr, addrlen);
    interrupted = ((retVal < 0) && (errno == EINTR));
  } while (interrupted);
  return retVal;
}


x86 assembly (clang, -O0):
<signalSafeAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <accept@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jge    115f <signalSafeAccept+0x4f>
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e82(%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)
cmpl   $0x0,-0x1c(%rbp)
jne    1123 <signalSafeAccept+0x13>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalSafeAccept>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <accept@plt>
test   %eax,%eax
jns    114a <signalSafeAccept+0x3a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1130 <signalSafeAccept+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @signalSafeAccept(i32 %0, %struct.sockaddr* %1, i32* %2) {
br label %4
4: ; preds = %4, %3
%5 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%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 %4, label %11
11: ; preds = %4
ret i32 %5
}