#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ fnet_set_linger(int);
scalar_t__ fnet_set_nonblocking(int);
scalar_t__ fnet_set_reuse_addr(int);
void *htons(int /*<<< orphan*/);
void *inet_addr(char const *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int fnet_listen(const char *ip, in_port_t port, int max_link, int isblock) {
  int listen_fd;
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (ip)
    addr.sin_addr.s_addr = inet_addr(ip);
  else
    addr.sin_addr.s_addr = htons(INADDR_ANY);
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (0 > listen_fd)
    return -1;
  if (fnet_set_reuse_addr(listen_fd)) {
    goto cleanup;
  }
  if (fnet_set_linger(listen_fd)) {
    goto cleanup;
  }
  if (!isblock && fnet_set_nonblocking(listen_fd)) {
    goto cleanup;
  }
  if (0 > bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr))) {
    goto cleanup;
  }
  if (0 > listen(listen_fd, max_link)) {
    goto cleanup;
  }
  return listen_fd;
cleanup:
  close(listen_fd);
  return -1;
}


x86 assembly (clang, -O0):
<fnet_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11fa <fnet_listen+0x5a>
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x38(%rbp)
jmp    120c <fnet_listen+0x6c>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
jle    123f <fnet_listen+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <fnet_listen+0x14a>
mov    -0x20(%rbp),%edi
call   1060 <fnet_set_reuse_addr@plt>
cmp    $0x0,%rax
je     1256 <fnet_listen+0xb6>
jmp    12db <fnet_listen+0x13b>
mov    -0x20(%rbp),%edi
call   10b0 <fnet_set_linger@plt>
cmp    $0x0,%rax
je     126d <fnet_listen+0xcd>
jmp    12db <fnet_listen+0x13b>
cmpl   $0x0,-0x1c(%rbp)
jne    128e <fnet_listen+0xee>
mov    -0x20(%rbp),%edi
call   1030 <fnet_set_nonblocking@plt>
cmp    $0x0,%rax
je     128e <fnet_listen+0xee>
jmp    12db <fnet_listen+0x13b>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    12b2 <fnet_listen+0x112>
jmp    12db <fnet_listen+0x13b>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <listen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    12d0 <fnet_listen+0x130>
jmp    12db <fnet_listen+0x13b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <fnet_listen+0x14a>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e19(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
test   %rbx,%rbx
je     11f2 <fnet_listen+0x52>
mov    %rbx,%rdi
call   1080 <inet_addr@plt>
jmp    1200 <fnet_listen+0x60>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    (%r12),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1243 <fnet_listen+0xa3>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <fnet_set_reuse_addr@plt>
test   %rax,%rax
jne    123c <fnet_listen+0x9c>
mov    %ebx,%edi
call   10b0 <fnet_set_linger@plt>
test   %rax,%rax
je     1252 <fnet_listen+0xb2>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15d,%r15d
je     1280 <fnet_listen+0xe0>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     123c <fnet_listen+0x9c>
mov    %ebx,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     123c <fnet_listen+0x9c>
mov    %ebx,%ebp
jmp    1243 <fnet_listen+0xa3>
mov    %ebx,%edi
call   1030 <fnet_set_nonblocking@plt>
test   %rax,%rax
jne    123c <fnet_listen+0x9c>
jmp    1257 <fnet_listen+0xb7>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @fnet_listen(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %7
%16 = call i8* @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i8* %16, i8** %17
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i8* @inet_addr(i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i8* %22, i8** %24
br label %30
25: ; preds = %4
%26 = load i32, i32* @INADDR_ANY
%27 = call i8* @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i8* %27, i8** %29
br label %30
30: ; preds = %25, %20
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp sgt i32 0, %34
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 -1, i32* %5
br label %72
37: ; preds = %30
%38 = load i32, i32* %10
%39 = call i64 @fnet_set_reuse_addr(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %69
42: ; preds = %37
%43 = load i32, i32* %10
%44 = call i64 @fnet_set_linger(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
br label %69
47: ; preds = %42
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %47
%51 = load i32, i32* %10
%52 = call i64 @fnet_set_nonblocking(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
br label %69
55: ; preds = %50, %47
%56 = load i32, i32* %10
%57 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%58 = call i64 @bind(i32 %56, %struct.sockaddr* %57, i32 24)
%59 = icmp sgt i64 0, %58
br i1 %59, label %60, label %61
60: ; preds = %55
br label %69
61: ; preds = %55
%62 = load i32, i32* %10
%63 = load i32, i32* %8
%64 = call i64 @listen(i32 %62, i32 %63)
%65 = icmp sgt i64 0, %64
br i1 %65, label %66, label %67
66: ; preds = %61
br label %69
67: ; preds = %61
%68 = load i32, i32* %10
store i32 %68, i32* %5
br label %72
69: ; preds = %66, %60, %54, %46, %41
%70 = load i32, i32* %10
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %5
br label %72
72: ; preds = %69, %67, %36
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
define i32 @fnet_listen(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 24)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i8* @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i8* %10, i8** %11
%12 = icmp eq i8* %0, null
br i1 %12, label %15, label %13
13: ; preds = %4
%14 = call i8* @inet_addr(i8* %0)
br label %18
15: ; preds = %4
%16 = load i32, i32* @INADDR_ANY
%17 = call i8* @htons(i32 %16)
br label %18
18: ; preds = %15, %13
%19 = phi i8* [ %17, %15 ], [ %14, %13 ]
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i8* %19, i8** %20
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %45, label %25
25: ; preds = %18
%26 = call i64 @fnet_set_reuse_addr(i32 %23)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %25
%29 = call i64 @fnet_set_linger(i32 %23)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = icmp eq i32 %3, 0
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = call i64 @fnet_set_nonblocking(i32 %23)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33, %31
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = call i64 @bind(i32 %23, %struct.sockaddr* %37, i32 24)
%39 = icmp slt i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = call i64 @listen(i32 %23, i32 %2)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40, %36, %33, %28, %25
%44 = call i32 @close(i32 %23)
br label %45
45: ; preds = %40, %18, %43
%46 = phi i32 [ -1, %43 ], [ -1, %18 ], [ %23, %40 ]
ret i32 %46
}