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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setnonblock(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int server_init(char *ip, int port) {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock <= 0) {
    printf("failt to init server sock,err[%s]\n", strerror(errno));
    return -1;
  }
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htonl(port);
  addr.sin_addr.s_addr = ip == NULL ? INADDR_ANY : inet_addr(ip);
  if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) != 0) {
    printf("bind fail.err[%s]\n", strerror(errno));
    return -1;
  }
  if (listen(sock, 5) != 0) {
    printf("listen fail.err[%s]\n", strerror(errno));
    return -1;
  }
  setnonblock(sock);
  return sock;
}


x86 assembly (clang, -O0):
<server_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11de <server_init+0x5e>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <server_init+0x135>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1211 <server_init+0x91>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    121d <server_init+0x9d>
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1267 <server_init+0xe7>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdcd(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <server_init+0x135>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     12a7 <server_init+0x127>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xda0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <server_init+0x135>
mov    -0x18(%rbp),%edi
call   1060 <setnonblock@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e52(%rip),%r15
mov    (%r15),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
jle    11d0 <server_init+0x50>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
test   %r14,%r14
je     11e7 <server_init+0x67>
mov    %r14,%rdi
call   1050 <inet_addr@plt>
jmp    11f0 <server_init+0x70>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe1b(%rip),%rdi
jmp    1247 <server_init+0xc7>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <bind@plt>
test   %rax,%rax
je     1221 <server_init+0xa1>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe04(%rip),%rdi
jmp    1247 <server_init+0xc7>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
je     1261 <server_init+0xe1>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <setnonblock@plt>
jmp    1254 <server_init+0xd4>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"failt to init server sock,err[%s]\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [19 x i8] c"bind fail.err[%s]\0A\00"
@.str.2 = [21 x i8] c"listen fail.err[%s]\0A\00"
define i32 @server_init(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%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 sle i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %54
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = load i32, i32* @INADDR_ANY
br label %30
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = call i32 @inet_addr(i8* %28)
br label %30
30: ; preds = %27, %25
%31 = phi i32 [ %26, %25 ], [ %29, %27 ]
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i32 -1, i32* %3
br label %54
42: ; preds = %30
%43 = load i32, i32* %6
%44 = call i64 @listen(i32 %43, i32 5)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %48)
store i32 -1, i32* %3
br label %54
50: ; preds = %42
%51 = load i32, i32* %6
%52 = call i32 @setnonblock(i32 %51)
%53 = load i32, i32* %6
store i32 %53, i32* %3
br label %54
54: ; preds = %50, %46, %38, %13
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"failt to init server sock,err[%s]\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [19 x i8] c"bind fail.err[%s]\0A\00"
@.str.2 = [21 x i8] c"listen fail.err[%s]\0A\00"
define i32 @server_init(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%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, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %44
12: ; preds = %2
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htonl(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = icmp eq i8* %0, null
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = load i32, i32* @INADDR_ANY
br label %23
21: ; preds = %12
%22 = tail call i32 @inet_addr(i8* %0)
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ %20, %19 ], [ %22, %21 ]
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %6, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %42
33: ; preds = %23
%34 = call i64 @listen(i32 %6, i32 5)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %42
40: ; preds = %33
%41 = call i32 @setnonblock(i32 %6)
br label %42
42: ; preds = %40, %36, %29
%43 = phi i32 [ -1, %29 ], [ -1, %36 ], [ %6, %40 ]
br label %44
44: ; preds = %42, %8
%45 = phi i32 [ -1, %8 ], [ %43, %42 ]
ret i32 %45
}