#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;
};
struct addrinfo {
  int ai_family;
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int anetListen(char *, int, scalar_t__, int /*<<< orphan*/, int);
int /*<<< orphan*/ anetSetError(char *, char *);
int anetSetReuseAddr(char *, int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
char *inet_ntop(int, void const *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int _anetTcpServer(char *err, int port, char *bindaddr, int af, int backlog) {
  int s, rv;
  char _port[6];
  struct addrinfo hints;
  struct addrinfo *servinfo, *p;
  snprintf(_port, 6, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = af;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(bindaddr, _port, &hints, &servinfo)) != 0) {
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
      continue;
    struct sockaddr_in *sock_addr = (struct sockaddr_in *)p->ai_addr;
    char buf[0x100] = {0};
    char *info =
        inet_ntop(p->ai_family, (const void *)(&sock_addr->sin_addr.s_addr),
                  buf, sizeof(buf));
    printf("%s\n", info);
    if (anetSetReuseAddr(err, s) == -1)
      goto error;
    if (anetListen(err, s, p->ai_addr, p->ai_addrlen, backlog) == -1)
      goto error;
    goto end;
  }
  if (p == NULL) {
    anetSetError(err, "unable to bind socket");
    goto error;
  }
error:
  s = -1;
end:
  freeaddrinfo(servinfo);
  return s;
}


x86 assembly (clang, -O0):
<_anetTcpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
lea    -0x36(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xe30(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x36(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1224 <_anetTcpServer+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <_anetTcpServer+0x1ad>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     1312 <_anetTcpServer+0x172>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x68(%rbp),%rax
mov    0x10(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1261 <_anetTcpServer+0xc1>
jmp    1301 <_anetTcpServer+0x161>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x70(%rbp)
lea    -0x170(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rsi
lea    -0x170(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x178(%rbp)
mov    -0x178(%rbp),%rsi
lea    0xd53(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    12cf <_anetTcpServer+0x12f>
jmp    1337 <_anetTcpServer+0x197>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%r8d
call   1040 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    12fc <_anetTcpServer+0x15c>
jmp    1337 <_anetTcpServer+0x197>
jmp    133e <_anetTcpServer+0x19e>
mov    -0x68(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    122c <_anetTcpServer+0x8c>
cmpq   $0x0,-0x68(%rbp)
jne    1332 <_anetTcpServer+0x192>
mov    -0x10(%rbp),%rdi
lea    0xcdf(%rip),%rsi
call   1080 <anetSetError@plt>
jmp    1337 <_anetTcpServer+0x197>
jmp    1337 <_anetTcpServer+0x197>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x60(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_anetTcpServer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %r8d,%r14d
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%ecx
mov    %rdi,%r15
lea    0xe3b(%rip),%rdx
lea    0xa(%rsp),%r12
mov    %r12,%rdi
mov    $0x6,%esi
call   1060 <snprintf@plt>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    %ebp,0x10(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10a0 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1258 <_anetTcpServer+0xb8>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     123b <_anetTcpServer+0x9b>
nop
mov    0x14(%rbx),%esi
mov    (%rbx),%edi
mov    0x10(%rbx),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
jne    126c <_anetTcpServer+0xcc>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1220 <_anetTcpServer+0x80>
lea    0xdc5(%rip),%rsi
mov    %r15,%rdi
call   1080 <anetSetError@plt>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    (%rbx),%edi
lea    0x30(%rsp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xd05(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     124a <_anetTcpServer+0xaa>
mov    0x8(%rbx),%rdx
mov    0x4(%rbx),%ecx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%r8d
call   1040 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    124f <_anetTcpServer+0xaf>
jmp    124a <_anetTcpServer+0xaa>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo* }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [22 x i8] c"unable to bind socket\00"
define i32 @_anetTcpServer(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [6 x i8]
%15 = alloca %struct.addrinfo
%16 = alloca %struct.addrinfo*
%17 = alloca %struct.addrinfo*
%18 = alloca %struct.sockaddr_in*
%19 = alloca [256 x i8]
%20 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%21 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%22 = load i32, i32* %8
%23 = call i32 @snprintf(i8* %21, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = call i32 @memset(%struct.addrinfo* %15, i32 0, i32 32)
%25 = load i32, i32* %10
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 4
store i32 %27, i32* %28
%29 = load i8*, i8** %9
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%31 = call i32 @getaddrinfo(i8* %29, i8* %30, %struct.addrinfo* %15, %struct.addrinfo** %16)
store i32 %31, i32* %13
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %5
store i32 -1, i32* %6
br label %104
34: ; preds = %5
%35 = load %struct.addrinfo*, %struct.addrinfo** %16
store %struct.addrinfo* %35, %struct.addrinfo** %17
br label %36
36: ; preds = %88, %34
%37 = load %struct.addrinfo*, %struct.addrinfo** %17
%38 = icmp ne %struct.addrinfo* %37, null
br i1 %38, label %39, label %92
39: ; preds = %36
%40 = load %struct.addrinfo*, %struct.addrinfo** %17
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %17
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 4
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %17
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 3
%48 = load i32, i32* %47
%49 = call i32 @socket(i32 %42, i32 %45, i32 %48)
store i32 %49, i32* %12
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %39
br label %88
52: ; preds = %39
%53 = load %struct.addrinfo*, %struct.addrinfo** %17
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 2
%55 = load i64, i64* %54
%56 = inttoptr i64 %55 to %struct.sockaddr_in*
store %struct.sockaddr_in* %56, %struct.sockaddr_in** %18
%57 = bitcast [256 x i8]* %19 to i8*
%58 = load %struct.addrinfo*, %struct.addrinfo** %17
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = load %struct.sockaddr_in*, %struct.sockaddr_in** %18
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %61, i32 0, i32 0
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = bitcast i32* %63 to i8*
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %19, i64 0, i64 0
%66 = call i8* @inet_ntop(i32 %60, i8* %64, i8* %65, i32 256)
store i8* %66, i8** %20
%67 = load i8*, i8** %20
%68 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %7
%70 = load i32, i32* %12
%71 = call i32 @anetSetReuseAddr(i8* %69, i32 %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %52
br label %99
74: ; preds = %52
%75 = load i8*, i8** %7
%76 = load i32, i32* %12
%77 = load %struct.addrinfo*, %struct.addrinfo** %17
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 2
%79 = load i64, i64* %78
%80 = load %struct.addrinfo*, %struct.addrinfo** %17
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 1
%82 = load i32, i32* %81
%83 = load i32, i32* %11
%84 = call i32 @anetListen(i8* %75, i32 %76, i64 %79, i32 %82, i32 %83)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %87
86: ; preds = %74
br label %99
87: ; preds = %74
br label %100
88: ; preds = %51
%89 = load %struct.addrinfo*, %struct.addrinfo** %17
%90 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %89, i32 0, i32 5
%91 = load %struct.addrinfo*, %struct.addrinfo** %90
store %struct.addrinfo* %91, %struct.addrinfo** %17
br label %36
92: ; preds = %36
%93 = load %struct.addrinfo*, %struct.addrinfo** %17
%94 = icmp eq %struct.addrinfo* %93, null
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i8*, i8** %7
%97 = call i32 @anetSetError(i8* %96, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %99
98: ; preds = %92
br label %99
99: ; preds = %98, %95, %86, %73
store i32 -1, i32* %12
br label %100
100: ; preds = %99, %87
%101 = load %struct.addrinfo*, %struct.addrinfo** %16
%102 = call i32 @freeaddrinfo(%struct.addrinfo* %101)
%103 = load i32, i32* %12
store i32 %103, i32* %6
br label %104
104: ; preds = %100, %33
%105 = load i32, i32* %6
ret i32 %105
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo* }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [22 x i8] c"unable to bind socket\00"
define i32 @_anetTcpServer(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca [6 x i8]
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca [256 x i8]
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%11 = bitcast %struct.addrinfo* %7 to i8*
%12 = bitcast %struct.addrinfo** %8 to i8*
%13 = call i32 @snprintf(i8* %10, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%14 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 0
store i32 %3, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* %2, i8* %10, %struct.addrinfo* %7, %struct.addrinfo** %8)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %62
20: ; preds = %5
%21 = load %struct.addrinfo*, %struct.addrinfo** %8
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %56, label %23
23: ; preds = %20, %52
%24 = phi %struct.addrinfo* [ %54, %52 ], [ %21, %20 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %52, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%36 = load i64, i64* %35
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* %34
%39 = inttoptr i64 %36 to i8*
%40 = call i8* @inet_ntop(i32 %38, i8* %39, i8* %37, i32 256)
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @anetSetReuseAddr(i8* %0, i32 %31)
%43 = icmp eq i32 %42, -1
br i1 %43, label %50, label %44
44: ; preds = %33
%45 = load i64, i64* %35
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @anetListen(i8* %0, i32 %31, i64 %45, i32 %47, i32 %4)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %33, %44
br label %58
51: ; preds = %44
br label %58
52: ; preds = %23
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %23
56: ; preds = %52, %20
%57 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %50, %51
%59 = phi i32 [ %31, %51 ], [ -1, %50 ], [ -1, %56 ]
%60 = load %struct.addrinfo*, %struct.addrinfo** %8
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
br label %62
62: ; preds = %5, %58
%63 = phi i32 [ %59, %58 ], [ -1, %5 ]
ret i32 %63
}