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

/* Variables and functions */
int AF_INET6;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int anetListen(char *, int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int anetSetReuseAddr(char *, int);
int anetV6Only(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
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, *servinfo, *p;
  snprintf(_port, 6, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = af;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(bindaddr, _port, &hints, &servinfo)) != 0) {
    anetSetError(err, "%s", gai_strerror(rv));
    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;
    if (af == AF_INET6 && anetV6Only(err, s) == -1)
      goto error;
    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, errno: %d", errno);
    goto error;
  }
error:
  s = -1;
end:
  freeaddrinfo(servinfo);
  return s;
}


x86 assembly (clang, -O0):
<_anetTcpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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    0xe51(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x36(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1252 <_anetTcpServer+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xdbf(%rip),%rsi
call   1080 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <_anetTcpServer+0x1bb>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1317 <_anetTcpServer+0x177>
mov    -0x70(%rbp),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x70(%rbp),%rax
mov    0xc(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    128f <_anetTcpServer+0xef>
jmp    1306 <_anetTcpServer+0x166>
mov    -0x24(%rbp),%eax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%eax
jne    12bb <_anetTcpServer+0x11b>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <anetV6Only@plt>
cmp    $0xffffffff,%eax
jne    12bb <_anetTcpServer+0x11b>
jmp    1345 <_anetTcpServer+0x1a5>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    12d5 <_anetTcpServer+0x135>
jmp    1345 <_anetTcpServer+0x1a5>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x70(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%r8d
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    1301 <_anetTcpServer+0x161>
jmp    1345 <_anetTcpServer+0x1a5>
jmp    134c <_anetTcpServer+0x1ac>
mov    -0x70(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    125a <_anetTcpServer+0xba>
cmpq   $0x0,-0x70(%rbp)
jne    1340 <_anetTcpServer+0x1a0>
mov    -0x10(%rbp),%rdi
mov    0x2c9b(%rip),%rax
mov    (%rax),%edx
lea    0xccd(%rip),%rsi
call   1080 <anetSetError@plt>
jmp    1345 <_anetTcpServer+0x1a5>
jmp    1345 <_anetTcpServer+0x1a5>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x68(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_anetTcpServer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%r14d
mov    %ecx,%r12d
mov    %rdx,%rbx
mov    %esi,%ecx
mov    %rdi,%r15
lea    0xe5e(%rip),%rdx
lea    0xa(%rsp),%r13
mov    %r13,%rdi
mov    $0x6,%esi
call   1040 <snprintf@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    %r12d,0x10(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     123c <_anetTcpServer+0x9c>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdd5(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%edx
call   1080 <anetSetError@plt>
mov    $0xffffffff,%ebx
jmp    1292 <_anetTcpServer+0xf2>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     126c <_anetTcpServer+0xcc>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbp),%esi
mov    0x0(%rbp),%edi
mov    0xc(%rbp),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
jne    12a3 <_anetTcpServer+0x103>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
jne    1250 <_anetTcpServer+0xb0>
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
lea    0xd87(%rip),%rsi
mov    %r15,%rdi
call   1080 <anetSetError@plt>
mov    $0xffffffff,%ebx
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    0x2d34(%rip),%rax
cmp    %r12d,(%rax)
jne    12c0 <_anetTcpServer+0x120>
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <anetV6Only@plt>
cmp    $0xffffffff,%eax
je     1284 <_anetTcpServer+0xe4>
mov    %r15,%rdi
mov    %ebx,%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     1284 <_anetTcpServer+0xe4>
mov    0x4(%rbp),%ecx
mov    0x8(%rbp),%edx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%r8d
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    1289 <_anetTcpServer+0xe9>
jmp    1284 <_anetTcpServer+0xe4>

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

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