#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 */

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int net_accept(int fd) {
  int con_fd = accept(fd, NULL, NULL);
  if (con_fd == -1)
    return -1;
  if (-1 == fcntl(con_fd, F_SETFL, O_NONBLOCK)) {
    close(con_fd);
    return -1;
  }
  return con_fd;
}


x86 assembly (clang, -O0):
<net_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1050 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1163 <net_accept+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <net_accept+0x76>
mov    -0xc(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11a0 <net_accept+0x70>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <net_accept+0x76>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_accept>:
push   %rbp
push   %rbx
push   %rax
xor    %esi,%esi
xor    %edx,%edx
call   1050 <accept@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1171 <net_accept+0x41>
mov    %eax,%ebp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     116a <net_accept+0x3a>
mov    %ebp,%ebx
jmp    1171 <net_accept+0x41>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @net_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @accept(i32 %5, i32* null, i32* null)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %21
10: ; preds = %1
%11 = load i32, i32* %4
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = call i32 @fcntl(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %2
br label %21
19: ; preds = %10
%20 = load i32, i32* %4
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %16, %9
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @net_accept(i32 %0) {
%2 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%3 = icmp eq i32 %2, -1
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = load i32, i32* @F_SETFL
%6 = load i32, i32* @O_NONBLOCK
%7 = tail call i32 @fcntl(i32 %2, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @close(i32 %2)
br label %11
11: ; preds = %4, %1, %9
%12 = phi i32 [ -1, %9 ], [ -1, %1 ], [ %2, %4 ]
ret i32 %12
}