// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movl 0x88(%rdi), %r13d
movl $0x0, 0xa0(%rdi)
movl %edx, 0xc0(%rdi)
movq 0xb0(%rdi), %rdi
cmpq %rsi, %rdi
je 0x3900
testq %rdi, %rdi
je 0x38f1
callq 0x3050
movq %r14, %rdi
callq 0x34c0
movq %rax, 0xb0(%rbx)
movq 0xa8(%rbx), %rdi
testq %r12, %r12
je 0x3938
cmpq %r12, %rdi
je 0x394d
testq %rdi, %rdi
jne 0x3927
movl $0x10, %edi
callq 0x3340
movq %rax, 0xa8(%rbx)
movq 0xa8(%rbx), %rax
movups (%r12), %xmm0
movups %xmm0, (%rax)
jmp 0x394d
testq %rdi, %rdi
je 0x3942
callq 0x3050
movq $0x0, 0xa8(%rbx)
movq 0xb8(%rbx), %rdi
testq %r15, %r15
je 0x396d
cmpq %r15, %rdi
je 0x397b
callq 0x3050
movq %r15, %rdi
callq 0x34c0
jmp 0x3974
callq 0x3050
xorl %eax, %eax
movq %rax, 0xb8(%rbx)
leaq 0x56c8(%rip), %rdx      # 0x904a
leaq 0x2a(%rsp), %r15
movl $0x6, %esi
movq %r15, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3180
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 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x34a0
testl %eax, %eax
je 0x3a14
leaq 0x30(%rsp), %rdx
movl $0xa, 0x4(%rdx)
leaq 0x2a(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
callq 0x34a0
testl %eax, %eax
je 0x3a14
movl %eax, %edi
callq 0x3190
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x32a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3d2f
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x3c3b
movl %r13d, %eax
andl $0x1, %eax
movl %eax, 0x1c(%rsp)
movl %r13d, %eax
andl $0x80, %eax
movl %eax, 0x18(%rsp)
xorl %r14d, %r14d
movl %r13d, 0x4(%rsp)
cmpl $0xa, %r14d
movl $0x9, %eax
cmovgel %r14d, %eax
movl %eax, 0x20(%rsp)
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x3540
cmpl $-0x1, %eax
je 0x3c2e
movl %eax, %r12d
movl %eax, 0x84(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3eab
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x3d25
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x3bb1
xorl %esi, %esi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x34a0
testl %eax, %eax
je 0x3afa
movl %eax, %edi
callq 0x3190
movl $0x80, %esi
movl %r13d, %ebp
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x557f(%rip), %rdx      # 0x904d
movq %rax, %rcx
xorl %eax, %eax
callq 0x3180
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
movl %ebp, %r13d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x32a0
movl $0x6, %eax
jmp 0x3ba9
testb %r13b, %r13b
jns 0x3b28
movl $0x1, 0x24(%rsp)
movl %r12d, %edi
movl $0x1, %esi
movl $0x2, %edx
leaq 0x24(%rsp), %rcx
movl $0x4, %r8d
callq 0x30d0
testl %eax, %eax
js 0x3af0
movq 0x8(%rsp), %r13
testq %r13, %r13
je 0x3b4d
movq 0x18(%r13), %rsi
movl 0x10(%r13), %edx
movl %r12d, %edi
callq 0x33e0
cmpl $-0x1, %eax
jne 0x3b98
movq 0x28(%r13), %r13
jmp 0x3b2d
movq 0x8(%rsp), %rdi
callq 0x3530
callq 0x3080
movl (%rax), %edi
callq 0x34d0
movl $0x80, %esi
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x54e9(%rip), %rdx      # 0x9060
movq %rax, %rcx
xorl %eax, %eax
callq 0x3180
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
callq 0x32a0
movl $0x6, %eax
jmp 0x3ba4
movq 0x8(%rsp), %rdi
callq 0x3530
xorl %eax, %eax
movl 0x4(%rsp), %r13d
testl %eax, %eax
jne 0x3c7f
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %r12d, %edi
callq 0x3470
cmpl $-0x1, %eax
jne 0x3ca4
callq 0x3080
movl (%rax), %eax
cmpl $0x71, %eax
je 0x3c10
movl %eax, %ecx
xorl $0x73, %ecx
orl 0x1c(%rsp), %ecx
je 0x3ca4
cmpl $0x63, %eax
jne 0x3c8d
cmpl $0x0, 0x18(%rsp)
je 0x3c8d
leal 0x1(%r14), %eax
cmpl 0x20(%rsp), %r14d
movl %eax, %r14d
jne 0x3a51
jmp 0x3d25
testq %rbx, %rbx
je 0x3c2e
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x3c2e
callq 0x3200
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x3a40
callq 0x3080
movl (%rax), %edi
callq 0x34d0
leaq 0x5428(%rip), %rdx      # 0x9076
leaq 0x60(%rsp), %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x3180
movq %rbx, %rdi
movl $0x2, %esi
movq %r14, %rdx
callq 0x32a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3d25
cmpl $0x6, %eax
je 0x3d25
jmp 0x3d2f
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
callq 0x3f3e
testl %eax, %eax
jne 0x3d25
testb $0x1, %r13b
je 0x3cbb
movq %rbx, %rdi
movl $0x1, %esi
callq 0x3eab
testl %eax, %eax
jne 0x3d25
leaq 0x60(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x84(%rbx), %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x30d0
cmpl $-0x1, %eax
jne 0x3d1c
movl %eax, %r14d
leaq 0x539e(%rip), %rsi      # 0x908e
movq %rbx, %rdi
callq 0x3794
testq %rbx, %rbx
je 0x3d16
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x3d16
callq 0x3200
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
cmpl $-0x1, %r14d
je 0x3d25
orb $0x2, 0x88(%rbx)
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
callq 0x3530
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq