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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_socket(void) {
  int listenfd;
  struct sockaddr_in servaddr;
  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return -1;
  }
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(9877);
  if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("bind");
    goto close_fd;
  }
  if (listen(listenfd, 1) < 0) {
    perror("listen");
    goto close_fd;
  };
  fcntl(listenfd, F_SETFL, O_NONBLOCK);
  return listenfd;
close_fd:
  close(listenfd);
  return -1;
}


x86 assembly (clang, -O0):
<listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c5 <listen_socket+0x45>
lea    0xe4c(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <listen_socket+0xf7>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x2695,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    121b <listen_socket+0x9b>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
jmp    1268 <listen_socket+0xe8>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1243 <listen_socket+0xc3>
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
jmp    1268 <listen_socket+0xe8>
mov    -0x8(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <listen_socket+0xf7>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e5a(%rip),%r14
mov    (%r14),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1218 <listen_socket+0x98>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x2695,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1226 <listen_socket+0xa6>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     122f <listen_socket+0xaf>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde1(%rip),%rdi
call   1090 <perror@plt>
jmp    1242 <listen_socket+0xc2>
lea    0xdda(%rip),%rdi
jmp    1236 <listen_socket+0xb6>
lea    0xdd6(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    120e <listen_socket+0x8e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @listen_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
10: ; preds = %0
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = call i32 @htons(i32 9877)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
25: ; preds = %10
%26 = load i32, i32* %2
%27 = call i64 @listen(i32 %26, i32 1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %37
31: ; preds = %25
%32 = load i32, i32* %2
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_NONBLOCK
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
%36 = load i32, i32* %2
store i32 %36, i32* %1
br label %40
37: ; preds = %29, %23
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %1
br label %40
40: ; preds = %37, %31, %8
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @listen_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %0
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = tail call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 9877)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 %5, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %9
%21 = call i64 @listen(i32 %5, i32 1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* @O_NONBLOCK
%26 = call i32 @fcntl(i32 %5, i32 %24, i32 %25)
br label %31
27: ; preds = %20, %9
%28 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %20 ]
%29 = call i32 @perror(i8* %28)
%30 = call i32 @close(i32 %5)
br label %31
31: ; preds = %27, %23, %7
%32 = phi i32 [ -1, %7 ], [ -1, %27 ], [ %5, %23 ]
ret i32 %32
}