#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 */
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_CLOEXEC;
int O_NONBLOCK;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, ...);
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
  int fd = accept(sockfd, addr, addrlen);
  if (fd == -1) {
    ;
  }
  if (flags & O_CLOEXEC)
    fcntl(fd, F_SETFD, FD_CLOEXEC);
  if (flags & O_NONBLOCK) {
    int i = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, i | O_NONBLOCK);
  }
  return fd;
}


x86 assembly (clang, -O0):
<xaccept4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1158 <xaccept4+0x38>
jmp    1158 <xaccept4+0x38>
mov    -0x1c(%rbp),%eax
mov    0x2e4e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1189 <xaccept4+0x69>
mov    -0x20(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    0x2e55(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d3 <xaccept4+0xb3>
mov    -0x20(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xaccept4>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebp
call   1040 <accept@plt>
mov    %eax,%ebx
mov    0x2e7c(%rip),%rax
test   %ebp,(%rax)
je     1153 <xaccept4+0x33>
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e8e(%rip),%r14
test   %ebp,(%r14)
je     1188 <xaccept4+0x68>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%r14),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define i32 @xaccept4(i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = load i32, i32* %5
%12 = load %struct.sockaddr*, %struct.sockaddr** %6
%13 = load i32*, i32** %7
%14 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %4
br label %18
18: ; preds = %17, %4
%19 = load i32, i32* %8
%20 = load i32, i32* @O_CLOEXEC
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %9
%25 = load i32, i32* @F_SETFD
%26 = load i32, i32* @FD_CLOEXEC
%27 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %26)
br label %28
28: ; preds = %23, %18
%29 = load i32, i32* %8
%30 = load i32, i32* @O_NONBLOCK
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %28
%34 = load i32, i32* %9
%35 = load i32, i32* @F_GETFL
%36 = call i32 (i32, i32, ...) @fcntl(i32 %34, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %9
%38 = load i32, i32* @F_SETFL
%39 = load i32, i32* %10
%40 = load i32, i32* @O_NONBLOCK
%41 = or i32 %39, %40
%42 = call i32 (i32, i32, ...) @fcntl(i32 %37, i32 %38, i32 %41)
br label %43
43: ; preds = %33, %28
%44 = load i32, i32* %9
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define i32 @xaccept4(i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3) {
%5 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%6 = load i32, i32* @O_CLOEXEC
%7 = and i32 %6, %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = load i32, i32* @F_SETFD
%11 = load i32, i32* @FD_CLOEXEC
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %10, i32 %11)
br label %13
13: ; preds = %9, %4
%14 = load i32, i32* @O_NONBLOCK
%15 = and i32 %14, %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %13
%18 = load i32, i32* @F_GETFL
%19 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %18)
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %19
%23 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %20, i32 %22)
br label %24
24: ; preds = %17, %13
ret i32 %5
}