#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__ EINVAL;
scalar_t__ ENOSYS;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int /*<<< orphan*/ __syscall(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ errno;
int socketcall_cp(int (*)(int, struct sockaddr *, int /*<<< orphan*/ *, int),
                  int, struct sockaddr *, int /*<<< orphan*/ *, int,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len,
            int flg) {
  if (!flg)
    return accept(fd, addr, len);
  int ret = socketcall_cp(accept4, fd, addr, len, flg, 0, 0);
  if (ret >= 0 || (errno != ENOSYS && errno != EINVAL))
    return ret;
  ret = accept(fd, addr, len);
  if (ret < 0)
    return ret;
  if (flg & SOCK_CLOEXEC)
    __syscall(55, ret, F_SETFD, FD_CLOEXEC);
  if (flg & SOCK_NONBLOCK)
    __syscall(55, ret, F_SETFL, O_NONBLOCK);
  return ret;
}


x86 assembly (clang, -O0):
<accept4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1168 <accept4+0x38>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x4(%rbp)
jmp    126f <accept4+0x13f>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    0x2e22(%rip),%rdi
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1050 <socketcall_cp@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ce <accept4+0x9e>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
je     11d9 <accept4+0xa9>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     11d9 <accept4+0xa9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <accept4+0x13f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1201 <accept4+0xd1>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <accept4+0x13f>
mov    -0x1c(%rbp),%eax
mov    0x2de5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1235 <accept4+0x105>
mov    -0x20(%rbp),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    0x2d87(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
call   1030 <__syscall@plt>
mov    -0x1c(%rbp),%eax
mov    0x2d89(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1269 <accept4+0x139>
mov    -0x20(%rbp),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    0x2d83(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
call   1030 <__syscall@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%ebp
test   %ecx,%ecx
je     1217 <accept4+0xe7>
mov    %ecx,%r15d
movl   $0x0,(%rsp)
mov    0x2e43(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <socketcall_cp@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1207 <accept4+0xd7>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     119d <accept4+0x6d>
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    1207 <accept4+0xd7>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1207 <accept4+0xd7>
mov    0x2e38(%rip),%rax
test   %r15d,(%rax)
je     11dc <accept4+0xac>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %r14d,%esi
call   1030 <__syscall@plt>
mov    0x2de5(%rip),%rax
test   %r15d,(%rax)
je     1207 <accept4+0xd7>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %r14d,%esi
call   1030 <__syscall@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <accept@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@ENOSYS = global i64 0
@EINVAL = global i64 0
@SOCK_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @accept4(i32 %0, %struct.sockaddr* noalias %1, i32* noalias %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* %9
%12 = icmp ne i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %4
%14 = load i32, i32* %6
%15 = load %struct.sockaddr*, %struct.sockaddr** %7
%16 = load i32*, i32** %8
%17 = call i32 @accept(i32 %14, %struct.sockaddr* %15, i32* %16)
store i32 %17, i32* %5
br label %67
18: ; preds = %4
%19 = load i32, i32* %6
%20 = load %struct.sockaddr*, %struct.sockaddr** %7
%21 = load i32*, i32** %8
%22 = load i32, i32* %9
%23 = call i32 @socketcall_cp(i32 (i32, %struct.sockaddr*, i32*, i32)* @accept4, i32 %19, %struct.sockaddr* %20, i32* %21, i32 %22, i32 0, i32 0)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp sge i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %18
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENOSYS
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINVAL
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30, %18
%35 = load i32, i32* %10
store i32 %35, i32* %5
br label %67
36: ; preds = %30, %26
%37 = load i32, i32* %6
%38 = load %struct.sockaddr*, %struct.sockaddr** %7
%39 = load i32*, i32** %8
%40 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = load i32, i32* %10
store i32 %44, i32* %5
br label %67
45: ; preds = %36
%46 = load i32, i32* %9
%47 = load i32, i32* @SOCK_CLOEXEC
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* %10
%52 = load i32, i32* @F_SETFD
%53 = load i32, i32* @FD_CLOEXEC
%54 = call i32 @__syscall(i32 55, i32 %51, i32 %52, i32 %53)
br label %55
55: ; preds = %50, %45
%56 = load i32, i32* %9
%57 = load i32, i32* @SOCK_NONBLOCK
%58 = and i32 %56, %57
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* %10
%62 = load i32, i32* @F_SETFL
%63 = load i32, i32* @O_NONBLOCK
%64 = call i32 @__syscall(i32 55, i32 %61, i32 %62, i32 %63)
br label %65
65: ; preds = %60, %55
%66 = load i32, i32* %10
store i32 %66, i32* %5
br label %67
67: ; preds = %65, %43, %34, %13
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@ENOSYS = global i64 0
@EINVAL = global i64 0
@SOCK_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @accept4(i32 %0, %struct.sockaddr* noalias %1, i32* noalias %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
br label %37
8: ; preds = %4
%9 = tail call i32 @socketcall_cp(i32 (i32, %struct.sockaddr*, i32*, i32)* @accept4, i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3, i32 0, i32 0)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %37, label %11
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOSYS
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EINVAL
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %37
18: ; preds = %11
%19 = tail call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%20 = icmp slt i32 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %18
%22 = load i32, i32* @SOCK_CLOEXEC
%23 = and i32 %22, %3
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @F_SETFD
%27 = load i32, i32* @FD_CLOEXEC
%28 = tail call i32 @__syscall(i32 55, i32 %19, i32 %26, i32 %27)
br label %29
29: ; preds = %25, %21
%30 = load i32, i32* @SOCK_NONBLOCK
%31 = and i32 %30, %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* @O_NONBLOCK
%36 = tail call i32 @__syscall(i32 55, i32 %19, i32 %34, i32 %35)
br label %37
37: ; preds = %11, %8, %18, %33, %29, %6
%38 = phi i32 [ %7, %6 ], [ %9, %11 ], [ %9, %8 ], [ %19, %18 ], [ %19, %33 ], [ %19, %29 ]
ret i32 %38
}