// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movl %r8d, %ebp
movq %rcx, %r14
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2b3e7(%rip), %rdx     # 0x40174
leaq 0x2a(%rsp), %r13
movl $0x6, %esi
movq %r13, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10200
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, 0x8(%rdx)
leaq 0x20(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x10670
testl %eax, %eax
je 0x14df8
movl %eax, %edi
callq 0x10220
leaq 0x2b252(%rip), %rsi     # 0x40032
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14915
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x14fa6
movq %r14, 0x10(%rsp)
movq 0x20(%rsp), %r14
testq %r14, %r14
je 0x14f0c
movl %ebp, 0xc(%rsp)
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x10740
cmpl $-0x1, %eax
je 0x14eff
movl %eax, %r13d
movq %rbx, %rdi
movl %eax, %esi
callq 0x15842
cmpl $-0x1, %eax
je 0x14f62
testb $0x1, %bpl
je 0x14e5d
movq %rbx, %rdi
movl %r13d, %esi
movl $0x1, %edx
callq 0x1488c
testl %eax, %eax
jne 0x14f62
cmpq $0x0, 0x10(%rsp)
je 0x14ece
movq 0x10(%rsp), %rdi
xorl %esi, %esi
leaq 0x30(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x10670
testl %eax, %eax
jne 0x14fb8
movq %r15, %rbp
movl %r12d, %r15d
movq 0x18(%rsp), %r12
testq %r12, %r12
je 0x14fd5
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r13d, %edi
callq 0x10500
cmpl $-0x1, %eax
jne 0x14eba
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x14e97
jmp 0x14f2e
movq 0x18(%rsp), %rdi
callq 0x10730
movl %r15d, %r12d
movq %rbp, %r15
movl 0xc(%rsp), %ebp
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %r13d, %edi
callq 0x10640
cmpl $-0x1, %eax
jne 0x14f70
callq 0x100a0
testb $0x1, %bpl
je 0x14ef7
cmpl $0x73, (%rax)
je 0x14f70
movl %r13d, %edi
callq 0x102a0
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x14e0f
callq 0x100a0
movl (%rax), %edi
callq 0x10690
leaq 0x2b207(%rip), %rsi     # 0x40126
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14915
jmp 0x14f6a
movq 0x18(%rsp), %rdi
movl %r15d, %r12d
callq 0x10730
callq 0x100a0
movl (%rax), %edi
callq 0x10690
leaq 0x2b1cf(%rip), %rsi     # 0x4011d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14915
movq %rbp, %r15
movl 0xc(%rsp), %ebp
movl %r13d, %edi
callq 0x102a0
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rdi
callq 0x10730
testb $0x2, %bpl
movq 0x10(%rsp), %rax
je 0x14fa6
testq %rax, %rax
je 0x14fa6
cmpl $-0x1, %r13d
jne 0x14fa6
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x14d69
movl %eax, %r13d
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x10220
leaq 0x2b06c(%rip), %rsi     # 0x40032
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14915
jmp 0x14f62
movl %r15d, %r12d
xorl %edi, %edi
jmp 0x14f36