#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_GETFD;
int /*<<< orphan*/ F_SETFD;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, ...);
int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags) {
  int rets = accept(s, addr, addrlen);
  if (rets == -1)
    return s;
  if (flags & 0x8000) {
    flags = fcntl(s, F_GETFD);
    fcntl(rets, F_SETFD, flags | FD_CLOEXEC);
  }
  return rets;
}


x86 assembly (clang, -O0):
<accept4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    115e <accept4+0x3e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <accept4+0x8a>
mov    -0x1c(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     11a4 <accept4+0x84>
mov    -0x8(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    0x2e3d(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept4>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edi,%ebp
call   1040 <accept@plt>
cmp    $0xffffffff,%eax
je     116f <accept4+0x4f>
mov    %eax,%ebx
test   $0x8000,%r14d
je     1171 <accept4+0x51>
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e78(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
jmp    1171 <accept4+0x51>
mov    %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @accept4(i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3) {
%5 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = and i32 %3, 32768
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = load i32, i32* @F_GETFD
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %11)
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = or i32 %14, %12
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %13, i32 %15)
br label %17
17: ; preds = %7, %10, %4
%18 = phi i32 [ %0, %4 ], [ %5, %10 ], [ %5, %7 ]
ret i32 %18
}