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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_socket(int port) {
  int socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd == -1) {
    perror("socket error");
    exit(1);
  }
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port);
  memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));
  int opt = 1;
  int err = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
  if (err == -1) {
    perror("setsockopt error");
    exit(1);
  }
  int ret =
      bind(socketfd, (struct sockaddr *)(&server_addr), sizeof(server_addr));
  if (ret == -1) {
    perror("bind error");
    exit(1);
  }
  ret = listen(socketfd, 20);
  if (ret == -1) {
    perror("listen error");
    exit(1);
  }
  printf("My Web Server is Ready!\n");
  return socketfd;
}


x86 assembly (clang, -O0):
<init_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e7 <init_socket+0x47>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    126c <init_socket+0xcc>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12a0 <init_socket+0x100>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1080 <listen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12d0 <init_socket+0x130>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd5f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    0x2e2f(%rip),%r14
mov    (%r14),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126e <init_socket+0xce>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1277 <init_socket+0xd7>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1280 <init_socket+0xe0>
mov    %ebx,%edi
mov    $0x14,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1289 <init_socket+0xe9>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
jmp    1290 <init_socket+0xf0>
lea    0xd8f(%rip),%rdi
jmp    1290 <init_socket+0xf0>
lea    0xd97(%rip),%rdi
jmp    1290 <init_socket+0xf0>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, 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 = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [17 x i8] c"setsockopt error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [25 x i8] c"My Web Server is Ready!\0A\00"
define i32 @init_socket(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* %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memset(i32 %27, i32 0, i32 4)
store i32 1, i32* %5
%29 = load i32, i32* %3
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_REUSEADDR
%32 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, i32* %5, i32 4)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %16
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %16
%39 = load i32, i32* %3
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 16)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = load i32, i32* %3
%49 = call i32 @listen(i32 %48, i32 20)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %47
%56 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, 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 = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [17 x i8] c"setsockopt error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@str = [24 x i8] c"My Web Server is Ready!\00"
define i32 @init_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = bitcast %struct.sockaddr_in* %2 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = tail call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = tail call i32 @memset(i32 undef, i32 0, i32 4)
%21 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %6, i32 %22, i32 %23, i32* %3, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %11
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %11
%30 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%31 = call i32 @bind(i32 %6, %struct.sockaddr* %30, i32 16)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = call i32 @listen(i32 %6, i32 20)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
ret i32 %6
}