#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 /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SHUT_RD;
int SOCK_DGRAM;
int SOCK_STREAM;
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*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int init_server_long(int *sock, int port, char *ip, int type) {
  int ret = 0;
  struct sockaddr_in server;
  int s = -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);
  if (type == SOCK_STREAM)
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  else
    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (s == -1)
    goto err;
  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)) {
    shutdown(s, SHUT_RD);
    close(s);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<init_server_long>:
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)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1d(%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    11fa <init_server_long+0x6a>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    120c <init_server_long+0x7c>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%eax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%eax
jne    1246 <init_server_long+0xb6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
jmp    1269 <init_server_long+0xd9>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1278 <init_server_long+0xe8>
jmp    12e0 <init_server_long+0x150>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a3 <init_server_long+0x113>
lea    0xd67(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <init_server_long+0x150>
mov    -0x1c(%rbp),%eax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%eax
jne    12d0 <init_server_long+0x140>
mov    -0x34(%rbp),%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d0 <init_server_long+0x140>
jmp    12e0 <init_server_long+0x150>
mov    -0x34(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    130d <init_server_long+0x17d>
cmpl   $0xffffffff,-0x34(%rbp)
je     130d <init_server_long+0x17d>
mov    -0x34(%rbp),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_server_long>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r12d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
test   %rbx,%rbx
je     11e7 <init_server_long+0x57>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    11f3 <init_server_long+0x63>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2dbe(%rip),%rbp
cmp    %r12d,0x0(%rbp)
mov    (%r15),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
cmove  %r12d,%esi
mov    0x2d93(%rip),%rax
cmove  0x2db3(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
xor    %r15d,%r15d
cmp    $0xffffffff,%eax
je     1287 <init_server_long+0xf7>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1264 <init_server_long+0xd4>
cmp    %r12d,0x0(%rbp)
jne    1259 <init_server_long+0xc9>
mov    %ebx,%edi
mov    $0x80,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1270 <init_server_long+0xe0>
mov    %ebx,(%r14)
mov    $0x1,%r15d
jmp    1287 <init_server_long+0xf7>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15d,%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
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [5 x i8] c"bind\00"
@SHUT_RD = global i32 0
define i32 @init_server_long(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
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
%12 = bitcast %struct.sockaddr_in* %10 to i8*
%13 = call i32 @memset(i8* %12, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %6
%17 = trunc i32 %16 to i16
%18 = call i32 @htons(i16 zeroext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
br label %31
26: ; preds = %4
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i8*, i8** %7
%30 = call i32 @memcpy(i32* %28, i8* %29, i32 4)
br label %31
31: ; preds = %26, %22
%32 = load i32, i32* %8
%33 = load i32, i32* @SOCK_STREAM
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = load i32, i32* @IPPROTO_TCP
%39 = call i32 @socket(i32 %36, i32 %37, i32 %38)
store i32 %39, i32* %11
br label %45
40: ; preds = %31
%41 = load i32, i32* @AF_INET
%42 = load i32, i32* @SOCK_DGRAM
%43 = load i32, i32* @IPPROTO_UDP
%44 = call i32 @socket(i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %11
br label %45
45: ; preds = %40, %35
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %45
br label %68
49: ; preds = %45
%50 = load i32, i32* %11
%51 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%52 = call i32 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %68
56: ; preds = %49
%57 = load i32, i32* %8
%58 = load i32, i32* @SOCK_STREAM
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %65
60: ; preds = %56
%61 = load i32, i32* %11
%62 = call i32 @listen(i32 %61, i32 128)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %60
br label %68
65: ; preds = %60, %56
%66 = load i32, i32* %11
%67 = load i32*, i32** %5
store i32 %66, i32* %67
store i32 1, i32* %9
br label %68
68: ; preds = %65, %64, %54, %48
%69 = load i32, i32* %9
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %80
71: ; preds = %68
%72 = load i32, i32* %11
%73 = icmp ne i32 %72, -1
br i1 %73, label %74, label %80
74: ; preds = %71
%75 = load i32, i32* %11
%76 = load i32, i32* @SHUT_RD
%77 = call i32 @shutdown(i32 %75, i32 %76)
%78 = load i32, i32* %11
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %74, %71, %68
%81 = load i32, i32* %9
ret i32 %81
}

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