// File: /workspace/github_c_makefile_star2_O2/andyfighting[P]hiredis_timer_cmpt/net.c
// Function: redisContextConnectTcp
static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
                                   const struct timeval *timeout,
                                   const char *source_addr) {
    int s, rv, n;
    char _port[6];  /* strlen("65535"); */
    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
    int blocking = (c->flags & REDIS_BLOCK);
    int reuseaddr = (c->flags & REDIS_REUSEADDR);
    int reuses = 0;

    c->connection_type = REDIS_CONN_TCP;
    c->tcp.port = port;

    /* We need to take possession of the passed parameters
     * to make them reusable for a reconnect.
     * We also carefully check we don't free data we already own,
     * as in the case of the reconnect method.
     *
     * This is a bit ugly, but atleast it works and doesn't leak memory.
     **/
    if (c->tcp.host != addr) {
        if (c->tcp.host)
            free(c->tcp.host);

        c->tcp.host = strdup(addr);
    }

    if (timeout) {
        if (c->timeout != timeout) {
            if (c->timeout == NULL)
                c->timeout = malloc(sizeof(struct timeval));

            memcpy(c->timeout, timeout, sizeof(struct timeval));
        }
    } else {
        if (c->timeout)
            free(c->timeout);
        c->timeout = NULL;
    }

    if (source_addr == NULL) {
        free(c->tcp.source_addr);
        c->tcp.source_addr = NULL;
    } else if (c->tcp.source_addr != source_addr) {
        free(c->tcp.source_addr);
        c->tcp.source_addr = strdup(source_addr);
    }

    snprintf(_port, 6, "%d", port);
    memset(&hints,0,sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    /* Try with IPv6 if no IPv4 address was found. We do it in this order since
     * in a Redis client you can't afford to test if you have IPv6 connectivity
     * as this would add latency to every connect. Otherwise a more sensible
     * route could be: Use IPv6 if both addresses are available and there is IPv6
     * connectivity. */
    if ((rv = getaddrinfo(c->tcp.host,_port,&hints,&servinfo)) != 0) {
         hints.ai_family = AF_INET6;
         if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
            __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));
            return REDIS_ERR;
        }
    }
    for (p = servinfo; p != NULL; p = p->ai_next) {
addrretry:
        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
            continue;

        c->fd = s;
        if (redisSetBlocking(c,0) != REDIS_OK)
            goto error;
        if (c->tcp.source_addr) {
            int bound = 0;
            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
            if ((rv = getaddrinfo(c->tcp.source_addr, NULL, &hints, &bservinfo)) != 0) {
                char buf[128];
                snprintf(buf,sizeof(buf),"Can't get addr: %s",gai_strerror(rv));
                __redisSetError(c,REDIS_ERR_OTHER,buf);
                goto error;
            }

            if (reuseaddr) {
                n = 1;
                if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &n,
                               sizeof(n)) < 0) {
                    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) {
                char buf[128];
                snprintf(buf,sizeof(buf),"Can't bind socket: %s",strerror(errno));
                __redisSetError(c,REDIS_ERR_OTHER,buf);
                goto error;
            }
        }
        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
            if (errno == EHOSTUNREACH) {
                redisContextCloseFd(c);
                continue;
            } else if (errno == EINPROGRESS && !blocking) {
                /* This is ok. */
            } else if (errno == EADDRNOTAVAIL && reuseaddr) {
                if (++reuses >= REDIS_CONNECT_RETRIES) {
                    goto error;
                } else {
                    goto addrretry;
                }
            } else {
                if (redisContextWaitReady(c,c->timeout) != REDIS_OK)
                    goto error;
            }
        }
        if (blocking && redisSetBlocking(c,1) != REDIS_OK)
            goto error;
        if (redisSetTcpNoDelay(c) != REDIS_OK)
            goto error;

        c->flags |= REDIS_CONNECTED;
        rv = REDIS_OK;
        goto end;
    }
    if (p == NULL) {
        char buf[128];
        snprintf(buf,sizeof(buf),"Can't create socket: %s",strerror(errno));
        __redisSetError(c,REDIS_ERR_OTHER,buf);
        goto error;
    }

error:
    rv = REDIS_ERR;
end:
    freeaddrinfo(servinfo);
    return rv;  // Need to return REDIS_OK if alright
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r8, %r14
movq %rcx, %r12
movl %edx, %ebp
movq %rdi, %rbx
movl 0x88(%rdi), %eax
movl %eax, 0xc(%rsp)
andl $0x0, 0xa0(%rdi)
movq %rsi, %r15
movl %edx, 0xc0(%rdi)
movq 0xb0(%rdi), %rdi
cmpq %rsi, %rdi
je 0x38d3
callq 0x3050
movq %r15, %rdi
callq 0x34d0
movq %rax, 0xb0(%rbx)
movq 0xa8(%rbx), %rdi
testq %r12, %r12
je 0x3905
cmpq %r12, %rdi
je 0x3912
testq %rdi, %rdi
jne 0x38fb
pushq $0x10
popq %rdi
callq 0x3350
movq %rax, %rdi
movq %rax, 0xa8(%rbx)
movups (%r12), %xmm0
movups %xmm0, (%rdi)
jmp 0x3912
callq 0x3050
andq $0x0, 0xa8(%rbx)
movq 0xb8(%rbx), %rdi
testq %r14, %r14
je 0x3932
cmpq %r14, %rdi
je 0x3940
callq 0x3050
movq %r14, %rdi
callq 0x34d0
jmp 0x3939
callq 0x3050
xorl %eax, %eax
movq %rax, 0xb8(%rbx)
leaq 0x5703(%rip), %rdx      # 0x904a
xorl %r13d, %r13d
leaq 0x2a(%rsp), %r12
pushq $0x6
popq %rsi
movq %r12, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3190
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%rdx)
movq 0xb0(%rbx), %rdi
leaq 0x20(%rsp), %r14
movq %r12, %rsi
movq %r14, %rcx
callq 0x34b0
testl %eax, %eax
je 0x39d9
leaq 0x30(%rsp), %rdx
movl $0xa, 0x4(%rdx)
leaq 0x2a(%rsp), %rsi
leaq 0x20(%rsp), %rcx
movq %r15, %rdi
callq 0x34b0
testl %eax, %eax
je 0x39d9
movl %eax, %edi
callq 0x31a0
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x32b0
pushq $-0x1
popq %rbx
jmp 0x3b89
movl 0xc(%rsp), %ebp
movl %ebp, %r15d
andl $0x1, %r15d
movl %ebp, %r12d
andl $0x80, %r12d
movl %r15d, 0x18(%rsp)
movq (%r14), %r14
testq %r14, %r14
je 0x3c09
movq %r13, %rbp
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x3560
cmpl $-0x1, %eax
je 0x3b2e
movl %eax, %r13d
movl %eax, 0x84(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3d82
testl %eax, %eax
jne 0x3b7c
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x3ad0
xorl %esi, %esi
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x34b0
testl %eax, %eax
jne 0x3b9d
cmpb $0x0, 0xc(%rsp)
jns 0x3a8e
movl $0x1, 0x1c(%rsp)
movl %r13d, %edi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0x1c(%rsp), %rcx
pushq $0x4
popq %r8
callq 0x30e0
testl %eax, %eax
js 0x3b7c
movl %r12d, %r15d
leaq 0x10(%rsp), %r12
movq (%r12), %r12
testq %r12, %r12
je 0x3b3a
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r13d, %edi
callq 0x33f0
addq $0x28, %r12
cmpl $-0x1, %eax
je 0x3a96
movq 0x10(%rsp), %rdi
callq 0x3550
movl %r15d, %r12d
movl 0x18(%rsp), %r15d
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %r13d, %edi
callq 0x3480
cmpl $-0x1, %eax
jne 0x3bc0
callq 0x3080
movl (%rax), %eax
cmpl $0x71, %eax
je 0x3b26
movl %eax, %ecx
xorl $0x73, %ecx
orl %r15d, %ecx
je 0x3bd6
cmpl $0x63, %eax
jne 0x3bad
testl %r12d, %r12d
je 0x3bad
leal 0x1(%rbp), %eax
cmpl $0x8, %ebp
movl %eax, %r13d
jle 0x39ff
jmp 0x3b7c
movq %rbx, %rdi
callq 0x3ecd
addq $0x28, %r14
movq %rbp, %r13
jmp 0x39f3
movq 0x10(%rsp), %rdi
callq 0x3550
callq 0x3080
movl (%rax), %edi
callq 0x34e0
leaq 0x5509(%rip), %rdx      # 0x9060
leaq 0x60(%rsp), %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3190
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x32b0
pushq $-0x1
popq %rbx
movq 0x20(%rsp), %rdi
callq 0x3550
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x31a0
leaq 0x54a2(%rip), %rdx      # 0x904d
jmp 0x3b57
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
callq 0x3df4
testl %eax, %eax
jne 0x3b7c
testb $0x1, 0xc(%rsp)
je 0x3bd6
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3d82
testl %eax, %eax
jne 0x3b7c
leaq 0x60(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x84(%rbx), %edi
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
pushq $0x4
popq %r8
callq 0x30e0
cmpl $-0x1, %eax
je 0x3c21
orb $0x2, 0x88(%rbx)
xorl %ebx, %ebx
jmp 0x3b7f
callq 0x3080
movl (%rax), %edi
callq 0x34e0
leaq 0x545a(%rip), %rdx      # 0x9076
jmp 0x3b57
leaq 0x5466(%rip), %rsi      # 0x908e
movq %rbx, %rdi
callq 0x377c
movq %rbx, %rdi
callq 0x3ecd
jmp 0x3b7c