#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setnonblocking(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int ev_listen(int port) {
  signal(SIGPIPE, SIG_IGN);
  struct sockaddr_in server_addr;
  int listen_sock;
  listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == listen_sock) {
    fprintf(stderr, "create sock err: %s\n", strerror(errno));
    return -1;
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_port = htons(port);
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int ret;
  ret = bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (-1 == ret) {
    fprintf(stderr, "bind err: %s\n", strerror(errno));
    return -1;
  }
  setnonblocking(listen_sock);
  int reuse = 1;
  setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  if (-1 == listen(listen_sock, SOMAXCONN)) {
    fprintf(stderr, "listen err: %s\n", strerror(errno));
    return -1;
  }
  return listen_sock;
}


x86 assembly (clang, -O0):
<ev_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    1234 <ev_listen+0x84>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xddd(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1357 <ev_listen+0x1a7>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    12c6 <ev_listen+0x116>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd60(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1357 <ev_listen+0x1a7>
mov    -0x1c(%rbp),%edi
call   10b0 <setnonblocking@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    0x2cb0(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cb5(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1351 <ev_listen+0x1a1>
mov    0x2ca0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xce3(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1357 <ev_listen+0x1a7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ev_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e06(%rip),%r15
mov    (%r15),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12b0 <ev_listen+0x100>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12d0 <ev_listen+0x120>
mov    %ebx,%edi
call   10b0 <setnonblocking@plt>
movl   $0x1,0x4(%rsp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12fd <ev_listen+0x14d>
mov    0x2d29(%rip),%rax
mov    (%rax),%ebx
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd75(%rip),%rsi
jmp    12ee <ev_listen+0x13e>
mov    0x2d09(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd32(%rip),%rsi
jmp    12ee <ev_listen+0x13e>
mov    0x2ce9(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd27(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"create sock err: %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind err: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOMAXCONN = global i32 0
@.str.2 = [16 x i8] c"listen err: %s\0A\00"
define i32 @ev_listen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @SIGPIPE
%9 = load i32, i32* @SIG_IGN
%10 = call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %2
br label %60
21: ; preds = %1
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%23 = load i32, i32* %3
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %5
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp eq i32 -1, %35
br i1 %36, label %37, label %42
37: ; preds = %21
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i32 -1, i32* %2
br label %60
42: ; preds = %21
%43 = load i32, i32* %5
%44 = call i32 @setnonblocking(i32 %43)
store i32 1, i32* %7
%45 = load i32, i32* %5
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_REUSEADDR
%48 = call i32 @setsockopt(i32 %45, i32 %46, i32 %47, i32* %7, i32 4)
%49 = load i32, i32* %5
%50 = load i32, i32* @SOMAXCONN
%51 = call i32 @listen(i32 %49, i32 %50)
%52 = icmp eq i32 -1, %51
br i1 %52, label %53, label %58
53: ; preds = %42
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %56)
store i32 -1, i32* %2
br label %60
58: ; preds = %42
%59 = load i32, i32* %5
store i32 %59, i32* %2
br label %60
60: ; preds = %58, %53, %37, %16
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"create sock err: %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind err: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOMAXCONN = global i32 0
@.str.2 = [16 x i8] c"listen err: %s\0A\00"
define i32 @ev_listen(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @SIGPIPE
%5 = load i32, i32* @SIG_IGN
%6 = tail call i32 @signal(i32 %4, i32 %5)
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %50
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%19 = call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @bind(i32 %10, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %50
34: ; preds = %17
%35 = call i32 @setnonblocking(i32 %10)
%36 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = call i32 @setsockopt(i32 %10, i32 %37, i32 %38, i32* %3, i32 4)
%40 = load i32, i32* @SOMAXCONN
%41 = call i32 @listen(i32 %10, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %34, %43
%49 = phi i32 [ -1, %43 ], [ %10, %34 ]
br label %50
50: ; preds = %29, %48, %12
%51 = phi i32 [ -1, %12 ], [ -1, %29 ], [ %49, %48 ]
ret i32 %51
}