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

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int anetListen(char *, int, int /*<<< orphan*/, 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 **);
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;
  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;
    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    $0x70,%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    0xe53(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x36(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1201 <_anetTcpServer+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <_anetTcpServer+0x155>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     129a <_anetTcpServer+0x11a>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x68(%rbp),%rax
mov    0xc(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    123e <_anetTcpServer+0xbe>
jmp    1289 <_anetTcpServer+0x109>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    1258 <_anetTcpServer+0xd8>
jmp    12bf <_anetTcpServer+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x28(%rbp),%r8d
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    1284 <_anetTcpServer+0x104>
jmp    12bf <_anetTcpServer+0x13f>
jmp    12c6 <_anetTcpServer+0x146>
mov    -0x68(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    1209 <_anetTcpServer+0x89>
cmpq   $0x0,-0x68(%rbp)
jne    12ba <_anetTcpServer+0x13a>
mov    -0x10(%rbp),%rdi
lea    0xd53(%rip),%rsi
call   1060 <anetSetError@plt>
jmp    12bf <_anetTcpServer+0x13f>
jmp    12bf <_anetTcpServer+0x13f>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x60(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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,%ebp
mov    %rdx,%rbx
mov    %esi,%ecx
mov    %rdi,%r15
lea    0xe5e(%rip),%rdx
lea    0x12(%rsp),%r12
mov    %r12,%rdi
mov    $0x6,%esi
call   1040 <snprintf@plt>
lea    0x18(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %ebp,0x18(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1080 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1239 <_anetTcpServer+0xb9>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     121b <_anetTcpServer+0x9b>
nop
mov    0x10(%rbx),%esi
mov    (%rbx),%edi
mov    0xc(%rbx),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
jne    124a <_anetTcpServer+0xca>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1200 <_anetTcpServer+0x80>
lea    0xde1(%rip),%rsi
mov    %r15,%rdi
call   1060 <anetSetError@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
call   1070 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     122a <_anetTcpServer+0xaa>
mov    0x4(%rbx),%ecx
mov    0x8(%rbx),%edx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%r8d
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    122f <_anetTcpServer+0xaf>
jmp    122a <_anetTcpServer+0xaa>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@.str.1 = [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*
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 32)
%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 i8*, i8** %9
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%28 = call i32 @getaddrinfo(i8* %26, i8* %27, %struct.addrinfo* %15, %struct.addrinfo** %16)
store i32 %28, i32* %13
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %5
store i32 -1, i32* %6
br label %85
31: ; preds = %5
%32 = load %struct.addrinfo*, %struct.addrinfo** %16
store %struct.addrinfo* %32, %struct.addrinfo** %17
br label %33
33: ; preds = %69, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %17
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %73
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %17
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %17
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 4
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %17
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 3
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %12
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %36
br label %69
49: ; preds = %36
%50 = load i8*, i8** %7
%51 = load i32, i32* %12
%52 = call i32 @anetSetReuseAddr(i8* %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %49
br label %80
55: ; preds = %49
%56 = load i8*, i8** %7
%57 = load i32, i32* %12
%58 = load %struct.addrinfo*, %struct.addrinfo** %17
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load i32, i32* %59
%61 = load %struct.addrinfo*, %struct.addrinfo** %17
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 1
%63 = load i32, i32* %62
%64 = load i32, i32* %11
%65 = call i32 @anetListen(i8* %56, i32 %57, i32 %60, i32 %63, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %68
67: ; preds = %55
br label %80
68: ; preds = %55
br label %81
69: ; preds = %48
%70 = load %struct.addrinfo*, %struct.addrinfo** %17
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 5
%72 = load %struct.addrinfo*, %struct.addrinfo** %71
store %struct.addrinfo* %72, %struct.addrinfo** %17
br label %33
73: ; preds = %33
%74 = load %struct.addrinfo*, %struct.addrinfo** %17
%75 = icmp eq %struct.addrinfo* %74, null
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i8*, i8** %7
%78 = call i32 @anetSetError(i8* %77, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %80
79: ; preds = %73
br label %80
80: ; preds = %79, %76, %67, %54
store i32 -1, i32* %12
br label %81
81: ; preds = %80, %68
%82 = load %struct.addrinfo*, %struct.addrinfo** %16
%83 = call i32 @freeaddrinfo(%struct.addrinfo* %82)
%84 = load i32, i32* %12
store i32 %84, i32* %6
br label %85
85: ; preds = %81, %30
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@.str.1 = [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 = 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 32)
%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 = call i32 @getaddrinfo(i8* %2, i8* %9, %struct.addrinfo* %7, %struct.addrinfo** %8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %53
19: ; preds = %5
%20 = load %struct.addrinfo*, %struct.addrinfo** %8
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %46, label %22
22: ; preds = %19, %42
%23 = phi %struct.addrinfo* [ %44, %42 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %42, label %32
32: ; preds = %22
%33 = call i32 @anetSetReuseAddr(i8* %0, i32 %30)
%34 = icmp eq i32 %33, -1
br i1 %34, label %48, label %35
35: ; preds = %32
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @anetListen(i8* %0, i32 %30, i32 %37, i32 %39, i32 %4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %48, label %49
42: ; preds = %22
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %22
46: ; preds = %42, %19
%47 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %35, %32, %46
br label %49
49: ; preds = %35, %48
%50 = phi i32 [ -1, %48 ], [ %30, %35 ]
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %5, %49
%54 = phi i32 [ %50, %49 ], [ -1, %5 ]
ret i32 %54
}