#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;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned short);
int listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int init_server(int *sock, int port, char *ip, int type) {
  int ret = 0;
  struct sockaddr_in server;
  int s = -1;
  int j = 1;
  memset((char *)&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons((unsigned short)port);
  if (ip == NULL)
    server.sin_addr.s_addr = INADDR_ANY;
  else
    memcpy(&server.sin_addr.s_addr, ip, 4);
  s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s == (-1))
    goto err;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
  if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("bind");
    goto err;
  }
  if (type == SOCK_STREAM && listen(s, 128) == -1)
    goto err;
  *sock = s;
  ret = 1;
err:
  if ((ret == 0) && (s != -1)) {
    close(s);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<init_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <init_server+0x71>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    1213 <init_server+0x83>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1245 <init_server+0xb5>
jmp    12d1 <init_server+0x141>
mov    -0x34(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1294 <init_server+0x104>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d1 <init_server+0x141>
mov    -0x1c(%rbp),%eax
mov    0x2d22(%rip),%rcx
cmp    (%rcx),%eax
jne    12c1 <init_server+0x131>
mov    -0x34(%rbp),%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c1 <init_server+0x131>
jmp    12d1 <init_server+0x141>
mov    -0x34(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    12ed <init_server+0x15d>
cmpl   $0xffffffff,-0x34(%rbp)
je     12ed <init_server+0x15d>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1b(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
test   %rbx,%rbx
je     11f0 <init_server+0x60>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    11fc <init_server+0x6c>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%r12),%edi
mov    0x2db9(%rip),%r12
mov    (%r12),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     128e <init_server+0xfe>
mov    %eax,%ebp
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     127b <init_server+0xeb>
cmp    %r15d,(%r12)
jne    1271 <init_server+0xe1>
mov    %ebp,%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1287 <init_server+0xf7>
mov    %ebp,(%r14)
mov    $0x1,%ebx
jmp    128e <init_server+0xfe>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [5 x i8] c"bind\00"
define i32 @init_server(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 -1, i32* %11
store i32 1, i32* %12
%13 = bitcast %struct.sockaddr_in* %10 to i8*
%14 = call i32 @memset(i8* %13, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %6
%18 = trunc i32 %17 to i16
%19 = call i32 @htons(i16 zeroext %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %4
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
br label %32
27: ; preds = %4
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = load i8*, i8** %7
%31 = call i32 @memcpy(i32* %29, i8* %30, i32 4)
br label %32
32: ; preds = %27, %23
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = load i32, i32* @IPPROTO_TCP
%36 = call i32 @socket(i32 %33, i32 %34, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %32
br label %64
40: ; preds = %32
%41 = load i32, i32* %11
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_REUSEADDR
%44 = bitcast i32* %12 to i8*
%45 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i8* %44, i32 4)
%46 = load i32, i32* %11
%47 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%48 = call i32 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %64
52: ; preds = %40
%53 = load i32, i32* %8
%54 = load i32, i32* @SOCK_STREAM
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = load i32, i32* %11
%58 = call i32 @listen(i32 %57, i32 128)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %61
60: ; preds = %56
br label %64
61: ; preds = %56, %52
%62 = load i32, i32* %11
%63 = load i32*, i32** %5
store i32 %62, i32* %63
store i32 1, i32* %9
br label %64
64: ; preds = %61, %60, %50, %39
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %64
%68 = load i32, i32* %11
%69 = icmp ne i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* %11
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %67, %64
%74 = load i32, i32* %9
ret i32 %74
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [5 x i8] c"bind\00"
define i32 @init_server(i32* nocapture writeonly %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%9 = call i32 @memset(i8* %7, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %10, i32* %11
%12 = trunc i32 %1 to i16
%13 = call i32 @htons(i16 zeroext %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %13, i32* %14
%15 = icmp eq i8* %2, null
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %17, i32* %18
br label %22
19: ; preds = %4
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%21 = call i32 @memcpy(i32* %20, i8* %2, i32 4)
br label %22
22: ; preds = %19, %16
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %46, label %28
28: ; preds = %22
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i32 @setsockopt(i32 %26, i32 %29, i32 %30, i8* %8, i32 4)
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @bind(i32 %26, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %44
37: ; preds = %28
%38 = load i32, i32* @SOCK_STREAM
%39 = icmp eq i32 %38, %3
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @listen(i32 %26, i32 128)
%42 = icmp eq i32 %41, -1
br i1 %42, label %44, label %43
43: ; preds = %40, %37
store i32 %26, i32* %0
br label %46
44: ; preds = %35, %40
%45 = call i32 @close(i32 %26)
br label %46
46: ; preds = %22, %43, %44
%47 = phi i32 [ 0, %44 ], [ 1, %43 ], [ 0, %22 ]
ret i32 %47
}