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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int open_listen_socket() {
  int socket_fd, res;
  struct sockaddr_in addr;
  socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  if (socket_fd == -1) {
    perror("socket: socket_fd");
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8888);
  res = bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr));
  if (res == -1) {
    perror("bind:");
  }
  res = listen(socket_fd, 255);
  if (res == -1) {
    perror("listen:");
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<open_listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11a3 <open_listen_socket+0x43>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f6 <open_listen_socket+0x96>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%edi
mov    $0xff,%esi
call   1050 <listen@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121c <open_listen_socket+0xbc>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_listen_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e78(%rip),%r15
mov    (%r15),%edi
mov    0x2e56(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ed <open_listen_socket+0x8d>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11fb <open_listen_socket+0x9b>
mov    %ebx,%edi
mov    $0xff,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     1209 <open_listen_socket+0xa9>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
jmp    1193 <open_listen_socket+0x33>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
jmp    11d0 <open_listen_socket+0x70>
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
jmp    11e1 <open_listen_socket+0x81>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [18 x i8] c"socket: socket_fd\00"
@.str.1 = [6 x i8] c"bind:\00"
@.str.2 = [8 x i8] c"listen:\00"
define i32 @open_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 = load i32, i32* @SOCK_NONBLOCK
%7 = or i32 %5, %6
%8 = call i32 @socket(i32 %4, i32 %7, i32 0)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 8)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @htons(i32 8888)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %1
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @bind(i32 %19, %struct.sockaddr* %20, i32 8)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %13
%27 = load i32, i32* %1
%28 = call i32 @listen(i32 %27, i32 255)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [18 x i8] c"socket: socket_fd\00"
@.str.1 = [6 x i8] c"bind:\00"
@.str.2 = [8 x i8] c"listen:\00"
define i32 @open_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 = load i32, i32* @SOCK_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @socket(i32 %3, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @htons(i32 8888)
%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 i32 @bind(i32 %7, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = call i32 @listen(i32 %7, i32 255)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret i32 %7
}