#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 /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int accept4(int sockfd, struct sockaddr *addr, socklen_t *len, int flags) {
  int fl, fd = accept(sockfd, addr, len);
  if (fd < 0)
    return fd;
  fl = fcntl(fd, F_GETFD);
  if (fl == -1)
    goto err;
  fl |= flags;
  if (fcntl(fd, F_SETFD, fl) == -1)
    goto err;
  return fd;
err:
  close(fd);
  return -1;
}


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)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    116e <accept4+0x3e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <accept4+0xaa>
mov    -0x24(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1193 <accept4+0x63>
jmp    11cb <accept4+0x9b>
mov    -0x1c(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11c0 <accept4+0x90>
jmp    11cb <accept4+0x9b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <accept4+0xaa>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept4>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
call   1050 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     117e <accept4+0x4e>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1172 <accept4+0x42>
or     %ebp,%eax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    117e <accept4+0x4e>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@F_GETFD = global i32 0
@F_SETFD = 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 slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = load i32, i32* @F_GETFD
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = or i32 %9, %3
%13 = load i32, i32* @F_SETFD
%14 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %13, i32 %12)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11, %7
%17 = tail call i32 @close(i32 %5)
br label %18
18: ; preds = %11, %4, %16
%19 = phi i32 [ -1, %16 ], [ %5, %4 ], [ %5, %11 ]
ret i32 %19
}