#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 */
typedef int /*<<< orphan*/ y;
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ aiInit;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int initListSock(const char port[]) {
  int ret, fd;
  struct addrinfo aiInit;
  struct addrinfo *aiList = 0;
  memset(&aiInit, 0, sizeof(aiInit));
  aiInit.ai_family = AF_INET;
  aiInit.ai_socktype = SOCK_STREAM;
  aiInit.ai_flags = AI_PASSIVE;
  ret = getaddrinfo(NULL, port, &aiInit, &aiList);
  if (ret != 0 || aiList == 0) {
    return -1;
  }
  fd = socket(aiList->ai_family, aiList->ai_socktype, aiList->ai_protocol);
  if (fd == -1) {
    freeaddrinfo(aiList);
    return -1;
  }
  int y = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y));
  ret = bind(fd, aiList->ai_addr, aiList->ai_addrlen);
  if (ret != 0) {
    freeaddrinfo(aiList);
    close(fd);
    return -1;
  }
  ret = listen(fd, 8);
  if (ret != 0) {
    freeaddrinfo(aiList);
    close(fd);
    return -1;
  }
  freeaddrinfo(aiList);
  return fd;
}


x86 assembly (clang, -O0):
<initListSock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11f5 <initListSock+0x75>
cmpq   $0x0,-0x38(%rbp)
jne    1201 <initListSock+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <initListSock+0x16d>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123d <initListSock+0xbd>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <initListSock+0x16d>
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12a7 <initListSock+0x127>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <initListSock+0x16d>
mov    -0x18(%rbp),%edi
mov    $0x8,%esi
call   1060 <listen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12de <initListSock+0x15e>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <initListSock+0x16d>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initListSock>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1080 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    128a <initListSock+0x10a>
mov    (%rsp),%rax
test   %rax,%rax
je     128a <initListSock+0x10a>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1256 <initListSock+0xd6>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     1261 <initListSock+0xe1>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    127c <initListSock+0xfc>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    128a <initListSock+0x10a>
mov    %ebx,%edi
mov    $0x8,%esi
call   1060 <listen@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
test   %ebp,%ebp
je     1288 <initListSock+0x108>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @initListSock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %3
store %struct.addrinfo* null, %struct.addrinfo** %7
%9 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %14, i32* %15
%16 = load i8*, i8** %3
%17 = call i32 @getaddrinfo(i32* null, i8* %16, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = load %struct.addrinfo*, %struct.addrinfo** %7
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20, %1
store i32 -1, i32* %2
br label %74
24: ; preds = %20
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 4
%27 = load i32, i32* %26
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 3
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 2
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %27, i32 %30, i32 %33)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %24
%38 = load %struct.addrinfo*, %struct.addrinfo** %7
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
store i32 -1, i32* %2
br label %74
40: ; preds = %24
store i32 1, i32* %8
%41 = load i32, i32* %5
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_REUSEADDR
%44 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i32* %8, i32 4)
%45 = load i32, i32* %5
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %7
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @bind(i32 %45, i32 %48, i32 %51)
store i32 %52, i32* %4
%53 = load i32, i32* %4
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %40
%56 = load %struct.addrinfo*, %struct.addrinfo** %7
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %2
br label %74
60: ; preds = %40
%61 = load i32, i32* %5
%62 = call i32 @listen(i32 %61, i32 8)
store i32 %62, i32* %4
%63 = load i32, i32* %4
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %60
%66 = load %struct.addrinfo*, %struct.addrinfo** %7
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
br label %74
70: ; preds = %60
%71 = load %struct.addrinfo*, %struct.addrinfo** %7
%72 = call i32 @freeaddrinfo(%struct.addrinfo* %71)
%73 = load i32, i32* %5
store i32 %73, i32* %2
br label %74
74: ; preds = %70, %65, %55, %37, %23
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @initListSock(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%7 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%15 = icmp ne i32 %14, 0
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = icmp eq %struct.addrinfo* %16, null
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %55, label %19
19: ; preds = %1
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 3
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %21, i32 %23, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load %struct.addrinfo*, %struct.addrinfo** %3
%30 = call i32 @freeaddrinfo(%struct.addrinfo* %29)
br label %55
31: ; preds = %19
%32 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %26, i32 %33, i32 %34, i32* %4, i32 4)
%36 = load %struct.addrinfo*, %struct.addrinfo** %3
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @bind(i32 %26, i32 %38, i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %31
%44 = load %struct.addrinfo*, %struct.addrinfo** %3
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %51
46: ; preds = %31
%47 = call i32 @listen(i32 %26, i32 8)
%48 = icmp eq i32 %47, 0
%49 = load %struct.addrinfo*, %struct.addrinfo** %3
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %49)
br i1 %48, label %53, label %51
51: ; preds = %46, %43
%52 = call i32 @close(i32 %26)
br label %53
53: ; preds = %51, %46
%54 = phi i32 [ %26, %46 ], [ -1, %51 ]
br label %55
55: ; preds = %1, %53, %28
%56 = phi i32 [ -1, %28 ], [ %54, %53 ], [ -1, %1 ]
ret i32 %56
}