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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ server_log(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void server_init(int *listenfd, int listen_max, struct sockaddr_in *server_addr,
                 const char *server_ip, int port) {
  socklen_t addr_len = sizeof(*server_addr);
  if ((*listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socke create");
    exit(-1);
  }
  if (inet_pton(AF_INET, server_ip, &(server_addr->sin_addr)) <= 0) {
    fprintf(stderr, "inet_pton error for %s\n", server_ip);
    exit(1);
  }
  bzero(server_addr, sizeof(*server_addr));
  server_addr->sin_family = AF_INET;
  server_addr->sin_port = htons(port);
  if (bind(*listenfd, (struct sockaddr *)server_addr, addr_len) < 0) {
    perror("socket bind");
    exit(1);
  }
  if (listen(*listenfd, listen_max) < 0) {
    perror("socket listen");
    exit(1);
  }
  server_log("Server init successful, waiting for client to connect...");
}


x86 assembly (clang, -O0):
<server_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0xc,-0x28(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    -0x8(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    1200 <server_init+0x60>
lea    0xe0f(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x8,%rdx
call   1060 <inet_pton@plt>
cmp    $0x0,%rax
jg     1247 <server_init+0xa7>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12a7 <server_init+0x107>
lea    0xd8d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12d5 <server_init+0x135>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd63(%rip),%rdi
call   1040 <server_log@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e27(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     124c <server_init+0xac>
mov    0x0(%r13),%edi
lea    0x8(%rbp),%rdx
mov    %r12,%rsi
call   1060 <inet_pton@plt>
test   %rax,%rax
jle    1262 <server_init+0xc2>
mov    %rbp,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rbp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x0(%rbp)
mov    (%rbx),%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1284 <server_init+0xe4>
mov    (%rbx),%edi
mov    %r14d,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     128d <server_init+0xed>
lea    0xe06(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <server_log@plt>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
mov    %r12,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd9a(%rip),%rdi
jmp    1294 <server_init+0xf4>
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socke create\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.2 = [12 x i8] c"socket bind\00"
@.str.3 = [14 x i8] c"socket listen\00"
@.str.4 = [57 x i8] c"Server init successful, waiting for client to connect...\00"
define void @server_init(i32* %0, i32 %1, %struct.sockaddr_in* %2, i8* %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 12, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32*, i32** %6
store i32 %14, i32* %15
%16 = icmp slt i32 %14, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %5
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %9
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 2
%25 = call i64 @inet_pton(i32 %21, i8* %22, i32* %24)
%26 = icmp sle i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %9
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%34 = call i32 @bzero(%struct.sockaddr_in* %33, i32 12)
%35 = load i32, i32* @AF_INET
%36 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 1
store i32 %35, i32* %37
%38 = load i32, i32* %10
%39 = call i32 @htons(i32 %38)
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32*, i32** %6
%43 = load i32, i32* %42
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%45 = bitcast %struct.sockaddr_in* %44 to %struct.sockaddr*
%46 = load i32, i32* %11
%47 = call i64 @bind(i32 %43, %struct.sockaddr* %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %32
%50 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %32
%53 = load i32*, i32** %6
%54 = load i32, i32* %53
%55 = load i32, i32* %7
%56 = call i64 @listen(i32 %54, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %52
%62 = call i32 @server_log(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socke create\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.2 = [12 x i8] c"socket bind\00"
@.str.3 = [14 x i8] c"socket listen\00"
@.str.4 = [57 x i8] c"Server init successful, waiting for client to connect...\00"
define void @server_init(i32* nocapture %0, i32 %1, %struct.sockaddr_in* %2, i8* %3, i32 %4) {
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %0
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %5
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%16 = tail call i64 @inet_pton(i32 %14, i8* %3, i32* %15)
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = tail call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 %4)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %0
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = tail call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %22
%36 = load i32, i32* %0
%37 = tail call i64 @listen(i32 %36, i32 %1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = tail call i32 @server_log(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.4, i64 0, i64 0))
ret void
}