#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int knot_map_errno();
int net_accept(int sock, struct sockaddr_storage *addr) {
  socklen_t len = sizeof(*addr);
  socklen_t *addr_len = (addr != NULL) ? &len : NULL;
  int remote = -1;
  remote = accept(sock, (struct sockaddr *)addr, addr_len);
  if (fcntl(remote, F_SETFL, O_NONBLOCK) != 0) {
    int error = knot_map_errno();
    close(remote);
    return error;
  }
  return remote;
}


x86 assembly (clang, -O0):
<net_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116e <net_accept+0x2e>
lea    -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1179 <net_accept+0x39>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1179 <net_accept+0x39>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     11dc <net_accept+0x9c>
mov    $0x0,%al
call   1050 <knot_map_errno@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e2 <net_accept+0xa2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_accept>:
push   %rbp
push   %rbx
push   %rax
movl   $0x4,0x4(%rsp)
test   %rsi,%rsi
lea    0x4(%rsp),%rdx
cmove  %rsi,%rdx
call   1060 <accept@plt>
mov    %eax,%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
je     118e <net_accept+0x4e>
xor    %eax,%eax
call   1050 <knot_map_errno@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @net_accept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
store i32 4, i32* %6
%10 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%11 = icmp ne %struct.sockaddr_storage* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %14
13: ; preds = %2
br label %14
14: ; preds = %13, %12
%15 = phi i32* [ %6, %12 ], [ null, %13 ]
store i32* %15, i32** %7
store i32 -1, i32* %8
%16 = load i32, i32* %4
%17 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%18 = bitcast %struct.sockaddr_storage* %17 to %struct.sockaddr*
%19 = load i32*, i32** %7
%20 = call i32 @accept(i32 %16, %struct.sockaddr* %18, i32* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* @O_NONBLOCK
%24 = call i64 @fcntl(i32 %21, i32 %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %14
%27 = call i32 (...) @knot_map_errno()
store i32 %27, i32* %9
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %9
store i32 %30, i32* %3
br label %33
31: ; preds = %14
%32 = load i32, i32* %8
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @net_accept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%5 = icmp eq %struct.sockaddr_storage* %1, null
%6 = select i1 %5, i32* null, i32* %3
%7 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %6)
%9 = load i32, i32* @F_SETFL
%10 = load i32, i32* @O_NONBLOCK
%11 = call i64 @fcntl(i32 %8, i32 %9, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = call i32 (...) @knot_map_errno()
%15 = call i32 @close(i32 %8)
br label %16
16: ; preds = %2, %13
%17 = phi i32 [ %14, %13 ], [ %8, %2 ]
ret i32 %17
}