#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 */
typedef int /*<<< orphan*/ uint16_t;
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*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int prepare_server_socket(char *port) {
  int bind_conf = 0;
  int listen_conf = 0;
  int listenfd = 0;
  struct sockaddr_in serv_addr;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd < 0) {
    perror("Cannot creat socket...\n");
    return -1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons((uint16_t)atoi(port));
  bind_conf = bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (bind_conf < 0) {
    perror("Cannot bind socket...\n");
    return -1;
  }
  listen_conf = listen(listenfd, 10);
  if (listen_conf < 0) {
    perror("Cannot listen on socket...\n");
    return -1;
  }
  return listenfd;
}


x86 assembly (clang, -O0):
<prepare_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <prepare_server_socket+0x5f>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <prepare_server_socket+0x110>
lea    -0x28(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1258 <prepare_server_socket+0xd8>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <prepare_server_socket+0x110>
mov    -0x1c(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128a <prepare_server_socket+0x10a>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <prepare_server_socket+0x110>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_server_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e53(%rip),%r12
mov    (%r12),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1224 <prepare_server_socket+0xa4>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
js     122d <prepare_server_socket+0xad>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
test   %eax,%eax
js     1236 <prepare_server_socket+0xb6>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd5(%rip),%rdi
jmp    123d <prepare_server_socket+0xbd>
lea    0xde4(%rip),%rdi
jmp    123d <prepare_server_socket+0xbd>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1216 <prepare_server_socket+0x96>

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 = [24 x i8] c"Cannot creat socket...\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [23 x i8] c"Cannot bind socket...\0A\00"
@.str.2 = [28 x i8] c"Cannot listen on socket...\0A\00"
define i32 @prepare_server_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%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* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %44
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_in* %7, i32 48, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
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* %7, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i8*, i8** %3
%24 = call i64 @atoi(i8* %23)
%25 = trunc i64 %24 to i32
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %15
%34 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %44
35: ; preds = %15
%36 = load i32, i32* %6
%37 = call i32 @listen(i32 %36, i32 10)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %44
42: ; preds = %35
%43 = load i32, i32* %6
store i32 %43, i32* %2
br label %44
44: ; preds = %42, %40, %33, %13
%45 = load i32, i32* %2
ret i32 %45
}

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 = [24 x i8] c"Cannot creat socket...\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [23 x i8] c"Cannot bind socket...\0A\00"
@.str.2 = [28 x i8] c"Cannot listen on socket...\0A\00"
define i32 @prepare_server_socket(i8* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%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 slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_in* %2, i32 48, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = call i64 @atoi(i8* %0)
%18 = trunc i64 %17 to i32
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i32 @bind(i32 %6, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %10
%25 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %31
26: ; preds = %10
%27 = call i32 @listen(i32 %6, i32 10)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %26, %29, %24, %8
%32 = phi i32 [ -1, %8 ], [ -1, %24 ], [ -1, %29 ], [ %6, %26 ]
ret i32 %32
}