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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ anetNonBlock(char *, int);
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int anetSetReuseAddr(char *, int);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ 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 /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int anetTcpGenericConnect(char *err, char *addr, int port, char *source_addr,
                          int flags) {
  int s = -1, rv;
  char portstr[6];
  struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
  snprintf(portstr, sizeof(portstr), "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(addr, portstr, &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 (anetSetReuseAddr(err, s) == -1)
      goto error;
    if (flags & 1 && anetNonBlock(err, s) != 0)
      goto error;
    if (source_addr) {
      int bound = 0;
      if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0) {
        anetSetError(err, "%s", gai_strerror(rv));
        goto end;
      }
      for (b = bservinfo; b != NULL; b = b->ai_next) {
        if (bind(s, b->ai_addr, b->ai_addrlen) != -1) {
          bound = 1;
          break;
        }
      }
      if (!bound) {
        anetSetError(err, "bind: %s", strerror(errno));
        goto end;
      }
    }
    if (connect(s, p->ai_addr, p->ai_addrlen) == -1) {
      if (errno == EINPROGRESS && flags & 1)
        goto end;
      close(s);
      s = -1;
      continue;
    }
    goto end;
  }
  if (p == NULL)
    anetSetError(err, "creating socket: %s", strerror(errno));
error:
  if (s != -1) {
    close(s);
    s = -1;
  }
end:
  freeaddrinfo(servinfo);
  return s;
}


x86 assembly (clang, -O0):
<anetTcpGenericConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
lea    -0x3a(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x6,%esi
lea    0xdf8(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x3a(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     128a <anetTcpGenericConnect+0xba>
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x90(%rbp),%rdi
mov    %eax,%edx
lea    0xd90(%rip),%rsi
call   1080 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14ed <anetTcpGenericConnect+0x31d>
mov    -0x68(%rbp),%rax
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
je     1481 <anetTcpGenericConnect+0x2b1>
mov    -0x78(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x78(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    12c8 <anetTcpGenericConnect+0xf8>
jmp    1470 <anetTcpGenericConnect+0x2a0>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
jne    12e2 <anetTcpGenericConnect+0x112>
jmp    14c0 <anetTcpGenericConnect+0x2f0>
mov    -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     130c <anetTcpGenericConnect+0x13c>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1030 <anetNonBlock@plt>
cmp    $0x0,%rax
je     130c <anetTcpGenericConnect+0x13c>
jmp    14c0 <anetTcpGenericConnect+0x2f0>
cmpq   $0x0,-0x28(%rbp)
je     140b <anetTcpGenericConnect+0x23b>
movl   $0x0,-0x84(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x60(%rbp),%rdx
lea    -0x70(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     136f <anetTcpGenericConnect+0x19f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%edx
lea    0xca4(%rip),%rsi
call   1080 <anetSetError@plt>
jmp    14de <anetTcpGenericConnect+0x30e>
mov    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
je     13c5 <anetTcpGenericConnect+0x1f5>
mov    -0x30(%rbp),%edi
mov    -0x80(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x80(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     13af <anetTcpGenericConnect+0x1df>
movl   $0x1,-0x84(%rbp)
jmp    13c5 <anetTcpGenericConnect+0x1f5>
jmp    13b4 <anetTcpGenericConnect+0x1e4>
mov    -0x80(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
jmp    1377 <anetTcpGenericConnect+0x1a7>
cmpl   $0x0,-0x84(%rbp)
jne    1406 <anetTcpGenericConnect+0x236>
mov    -0x10(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    0x2be4(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%edx
lea    0xc07(%rip),%rsi
call   1080 <anetSetError@plt>
jmp    14de <anetTcpGenericConnect+0x30e>
jmp    140b <anetTcpGenericConnect+0x23b>
mov    -0x30(%rbp),%edi
mov    -0x78(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x78(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    146b <anetTcpGenericConnect+0x29b>
mov    0x2b98(%rip),%rax
mov    (%rax),%rax
mov    0x2bae(%rip),%rcx
cmp    (%rcx),%rax
jne    1457 <anetTcpGenericConnect+0x287>
mov    -0x2c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1457 <anetTcpGenericConnect+0x287>
jmp    14de <anetTcpGenericConnect+0x30e>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    1470 <anetTcpGenericConnect+0x2a0>
jmp    14de <anetTcpGenericConnect+0x30e>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x78(%rbp)
jmp    1292 <anetTcpGenericConnect+0xc2>
cmpq   $0x0,-0x78(%rbp)
jne    14bb <anetTcpGenericConnect+0x2eb>
mov    -0x10(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    0x2b2a(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%edx
lea    0xb56(%rip),%rsi
call   1080 <anetSetError@plt>
jmp    14c0 <anetTcpGenericConnect+0x2f0>
cmpl   $0xffffffff,-0x30(%rbp)
je     14d9 <anetTcpGenericConnect+0x309>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    14de <anetTcpGenericConnect+0x30e>
mov    -0x68(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpGenericConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %edx,%ecx
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xe0d(%rip),%rdx
lea    0x12(%rsp),%r13
mov    %r13,%rdi
mov    $0x6,%esi
call   1040 <snprintf@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
je     126a <anetTcpGenericConnect+0x9a>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdb3(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1080 <anetSetError@plt>
mov    $0xffffffff,%ebp
jmp    13a1 <anetTcpGenericConnect+0x1d1>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1351 <anetTcpGenericConnect+0x181>
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1344 <anetTcpGenericConnect+0x174>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %eax,%esi
call   1090 <anetSetReuseAddr@plt>
cmp    $0xffffffff,%eax
je     13b2 <anetTcpGenericConnect+0x1e2>
test   $0x1,%r15b
je     12bc <anetTcpGenericConnect+0xec>
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <anetNonBlock@plt>
test   %rax,%rax
jne    13b2 <anetTcpGenericConnect+0x1e2>
test   %r14,%r14
je     1310 <anetTcpGenericConnect+0x140>
mov    %r14,%rdi
xor    %esi,%esi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
jne    13bb <anetTcpGenericConnect+0x1eb>
mov    0x8(%rsp),%r13
test   %r13,%r13
je     1378 <anetTcpGenericConnect+0x1a8>
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    0x4(%r13),%esi
mov    %ebp,%edi
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1310 <anetTcpGenericConnect+0x140>
mov    0x8(%r13),%r13
test   %r13,%r13
jne    12f0 <anetTcpGenericConnect+0x120>
jmp    1378 <anetTcpGenericConnect+0x1a8>
nop
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1398 <anetTcpGenericConnect+0x1c8>
test   $0x1,%r15b
je     133d <anetTcpGenericConnect+0x16d>
mov    0x2cba(%rip),%rax
mov    (%rax),%rax
mov    0x2c90(%rip),%rcx
cmp    %rax,(%rcx)
je     1398 <anetTcpGenericConnect+0x1c8>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    1277 <anetTcpGenericConnect+0xa7>
mov    0x2c70(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xca5(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1080 <anetSetError@plt>
mov    $0xffffffff,%ebp
jmp    1398 <anetTcpGenericConnect+0x1c8>
mov    0x2c49(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xc75(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1080 <anetSetError@plt>
mov    (%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1371 <anetTcpGenericConnect+0x1a1>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xc40(%rip),%rsi
jmp    138e <anetTcpGenericConnect+0x1be>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
@.str = [3 x i8] c"%d\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [9 x i8] c"bind: %s\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.3 = [20 x i8] c"creating socket: %s\00"
define i32 @anetTcpGenericConnect(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%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.addrinfo*
%19 = alloca %struct.addrinfo*
%20 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 -1, i32* %12
%21 = getelementptr inbounds [6 x i8], [6 x i8]* %14, i64 0, i64 0
%22 = load i32, i32* %9
%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* @AF_UNSPEC
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 5
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** %8
%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 %38
33: ; preds = %5
%34 = load i8*, i8** %7
%35 = load i32, i32* %13
%36 = call i32 @gai_strerror(i32 %35)
%37 = call i32 @anetSetError(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %36)
store i32 -1, i32* %6
br label %161
38: ; preds = %5
%39 = load %struct.addrinfo*, %struct.addrinfo** %16
store %struct.addrinfo* %39, %struct.addrinfo** %18
br label %40
40: ; preds = %137, %38
%41 = load %struct.addrinfo*, %struct.addrinfo** %18
%42 = icmp ne %struct.addrinfo* %41, null
br i1 %42, label %43, label %141
43: ; preds = %40
%44 = load %struct.addrinfo*, %struct.addrinfo** %18
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 5
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %18
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 4
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %18
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 3
%52 = load i32, i32* %51
%53 = call i32 @socket(i32 %46, i32 %49, i32 %52)
store i32 %53, i32* %12
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %43
br label %137
56: ; preds = %43
%57 = load i8*, i8** %7
%58 = load i32, i32* %12
%59 = call i32 @anetSetReuseAddr(i8* %57, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %56
br label %150
62: ; preds = %56
%63 = load i32, i32* %11
%64 = and i32 %63, 1
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %62
%67 = load i8*, i8** %7
%68 = load i32, i32* %12
%69 = call i64 @anetNonBlock(i8* %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %66
br label %150
72: ; preds = %66, %62
%73 = load i8*, i8** %10
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %114
75: ; preds = %72
store i32 0, i32* %20
%76 = load i8*, i8** %10
%77 = call i32 @getaddrinfo(i8* %76, i8* null, %struct.addrinfo* %15, %struct.addrinfo** %17)
store i32 %77, i32* %13
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %84
79: ; preds = %75
%80 = load i8*, i8** %7
%81 = load i32, i32* %13
%82 = call i32 @gai_strerror(i32 %81)
%83 = call i32 @anetSetError(i8* %80, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %82)
br label %157
84: ; preds = %75
%85 = load %struct.addrinfo*, %struct.addrinfo** %17
store %struct.addrinfo* %85, %struct.addrinfo** %19
br label %86
86: ; preds = %101, %84
%87 = load %struct.addrinfo*, %struct.addrinfo** %19
%88 = icmp ne %struct.addrinfo* %87, null
br i1 %88, label %89, label %105
89: ; preds = %86
%90 = load i32, i32* %12
%91 = load %struct.addrinfo*, %struct.addrinfo** %19
%92 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %91, i32 0, i32 1
%93 = load i32, i32* %92
%94 = load %struct.addrinfo*, %struct.addrinfo** %19
%95 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %94, i32 0, i32 0
%96 = load i32, i32* %95
%97 = call i32 @bind(i32 %90, i32 %93, i32 %96)
%98 = icmp ne i32 %97, -1
br i1 %98, label %99, label %100
99: ; preds = %89
store i32 1, i32* %20
br label %105
100: ; preds = %89
br label %101
101: ; preds = %100
%102 = load %struct.addrinfo*, %struct.addrinfo** %19
%103 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %102, i32 0, i32 2
%104 = load %struct.addrinfo*, %struct.addrinfo** %103
store %struct.addrinfo* %104, %struct.addrinfo** %19
br label %86
105: ; preds = %99, %86
%106 = load i32, i32* %20
%107 = icmp ne i32 %106, 0
br i1 %107, label %113, label %108
108: ; preds = %105
%109 = load i8*, i8** %7
%110 = load i64, i64* @errno
%111 = call i32 @strerror(i64 %110)
%112 = call i32 @anetSetError(i8* %109, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %111)
br label %157
113: ; preds = %105
br label %114
114: ; preds = %113, %72
%115 = load i32, i32* %12
%116 = load %struct.addrinfo*, %struct.addrinfo** %18
%117 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %116, i32 0, i32 1
%118 = load i32, i32* %117
%119 = load %struct.addrinfo*, %struct.addrinfo** %18
%120 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %119, i32 0, i32 0
%121 = load i32, i32* %120
%122 = call i32 @connect(i32 %115, i32 %118, i32 %121)
%123 = icmp eq i32 %122, -1
br i1 %123, label %124, label %136
124: ; preds = %114
%125 = load i64, i64* @errno
%126 = load i64, i64* @EINPROGRESS
%127 = icmp eq i64 %125, %126
br i1 %127, label %128, label %133
128: ; preds = %124
%129 = load i32, i32* %11
%130 = and i32 %129, 1
%131 = icmp ne i32 %130, 0
br i1 %131, label %132, label %133
132: ; preds = %128
br label %157
133: ; preds = %128, %124
%134 = load i32, i32* %12
%135 = call i32 @close(i32 %134)
store i32 -1, i32* %12
br label %137
136: ; preds = %114
br label %157
137: ; preds = %133, %55
%138 = load %struct.addrinfo*, %struct.addrinfo** %18
%139 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %138, i32 0, i32 2
%140 = load %struct.addrinfo*, %struct.addrinfo** %139
store %struct.addrinfo* %140, %struct.addrinfo** %18
br label %40
141: ; preds = %40
%142 = load %struct.addrinfo*, %struct.addrinfo** %18
%143 = icmp eq %struct.addrinfo* %142, null
br i1 %143, label %144, label %149
144: ; preds = %141
%145 = load i8*, i8** %7
%146 = load i64, i64* @errno
%147 = call i32 @strerror(i64 %146)
%148 = call i32 @anetSetError(i8* %145, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %147)
br label %149
149: ; preds = %144, %141
br label %150
150: ; preds = %149, %71, %61
%151 = load i32, i32* %12
%152 = icmp ne i32 %151, -1
br i1 %152, label %153, label %156
153: ; preds = %150
%154 = load i32, i32* %12
%155 = call i32 @close(i32 %154)
store i32 -1, i32* %12
br label %156
156: ; preds = %153, %150
br label %157
157: ; preds = %156, %136, %132, %108, %79
%158 = load %struct.addrinfo*, %struct.addrinfo** %16
%159 = call i32 @freeaddrinfo(%struct.addrinfo* %158)
%160 = load i32, i32* %12
store i32 %160, i32* %6
br label %161
161: ; preds = %157, %33
%162 = load i32, i32* %6
ret i32 %162
}

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