// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/anet.c
// Function: anetTcpGenericConnect
static int anetTcpGenericConnect(char *err, char *addr, int port,
                                 char *source_addr, int flags)
{
    int s = ANET_ERR, rv;
    char portstr[6];  /* strlen("65535") + 1; */
    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 ANET_ERR;
    }
    for (p = servinfo; p != NULL; p = p->ai_next) {
        /* Try to create the socket and to connect it.
         * If we fail in the socket() call, or on connect(), we retry with
         * the next entry in servinfo. */
        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
            continue;
        if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
        if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)
            goto error;
        if (source_addr) {
            int bound = 0;
            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
            if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0)
            {
                anetSetError(err, "%s", gai_strerror(rv));
                goto error;
            }
            for (b = bservinfo; b != NULL; b = b->ai_next) {
                if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
                    bound = 1;
                    break;
                }
            }
            freeaddrinfo(bservinfo);
            if (!bound) {
                anetSetError(err, "bind: %s", strerror(errno));
                goto error;
            }
        }
        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
            /* If the socket is non-blocking, it is ok for connect() to
             * return an EINPROGRESS error here. */
            if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)
                goto end;
            close(s);
            s = ANET_ERR;
            continue;
        }

        /* If we ended an iteration of the for loop without errors, we
         * have a connected socket. Let's return to the caller. */
        goto end;
    }
    if (p == NULL)
        anetSetError(err, "creating socket: %s", strerror(errno));

error:
    if (s != ANET_ERR) {
        close(s);
        s = ANET_ERR;
    }

end:
    freeaddrinfo(servinfo);

    /* Handle best effort binding: if a binding address was used, but it is
     * not possible to create a socket, try again without a binding address. */
    if (s == ANET_ERR && source_addr && (flags & ANET_CONNECT_BE_BINDING)) {
        return anetTcpGenericConnect(err,addr,port,NULL,flags);
    } else {
        return s;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, %rbp
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x2a497(%rip), %rdx     # 0x3b174
leaq 0x3a(%rsp), %r13
pushq $0x6
popq %rsi
movq %r13, %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10200
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, 0x8(%rdx)
leaq 0x30(%rsp), %r14
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x10670
testl %eax, %eax
je 0x10d47
movl %eax, %edi
callq 0x10220
leaq 0x2a301(%rip), %rsi     # 0x3b032
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x108d0
pushq $-0x1
popq %r13
jmp 0x10ecb
movq %rbp, 0x18(%rsp)
movq %r12, 0x28(%rsp)
movl 0xc(%rsp), %r12d
movq %rbx, 0x10(%rsp)
movq (%r14), %r14
testq %r14, %r14
je 0x10edd
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x10740
cmpl $-0x1, %eax
je 0x10e3e
movl %eax, %r13d
movq %rbx, %rdi
movl %eax, %esi
callq 0x116d9
cmpl $-0x1, %eax
je 0x10e7e
testb $0x1, %r12b
je 0x10db0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x10968
testl %eax, %eax
jne 0x10e7e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x10e11
movl %r15d, %ebp
xorl %esi, %esi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %r15
movq %r15, %rcx
callq 0x10670
testl %eax, %eax
jne 0x10eff
movq (%r15), %r15
testq %r15, %r15
je 0x10e47
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %r13d, %edi
callq 0x10500
addq $0x28, %r15
cmpl $-0x1, %eax
je 0x10dd9
movq 0x20(%rsp), %rdi
callq 0x10730
movl %ebp, %r15d
movl 0xc(%rsp), %r12d
movq 0x10(%rsp), %rbx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %r13d, %edi
callq 0x10640
cmpl $-0x1, %eax
jne 0x10e8a
callq 0x100a0
testb $0x1, %r12b
je 0x10e36
cmpl $0x73, (%rax)
je 0x10e8a
movl %r13d, %edi
callq 0x102a0
addq $0x28, %r14
jmp 0x10d5b
movq 0x20(%rsp), %rdi
callq 0x10730
callq 0x100a0
movl (%rax), %edi
callq 0x10690
leaq 0x2a2b9(%rip), %rsi     # 0x3b11d
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x108d0
movl %ebp, %r15d
movl 0xc(%rsp), %r12d
movl %r13d, %edi
callq 0x102a0
pushq $-0x1
popq %r13
cmpq $0x0, 0x18(%rsp)
sete %r14b
movq 0x30(%rsp), %rdi
callq 0x10730
cmpl $-0x1, %r13d
setne %al
testb $0x2, %r12b
sete %cl
orb %r14b, %cl
orb %al, %cl
jne 0x10ecb
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movl %r15d, %edx
xorl %ecx, %ecx
movl %r12d, %r8d
callq 0x10cb7
movl %eax, %r13d
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x100a0
movl (%rax), %edi
callq 0x10690
leaq 0x2a236(%rip), %rsi     # 0x3b126
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x108d0
jmp 0x10e86
movl %eax, %edi
callq 0x10220
leaq 0x2a125(%rip), %rsi     # 0x3b032
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x108d0
movl %ebp, %r15d
jmp 0x10e7e