// File: /workspace/github_c_makefile_star2_O3/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), %eax
movl %eax, 0x4(%rsp)
movl $0x0, 0xa0(%rdi)
movl %edx, 0xc0(%rdi)
movq 0xb0(%rdi), %rdi
cmpq %rsi, %rdi
je 0x3903
testq %rdi, %rdi
je 0x38f4
callq 0x3050
movq %r14, %rdi
callq 0x34c0
movq %rax, 0xb0(%rbx)
movq 0xa8(%rbx), %rdi
testq %r12, %r12
je 0x3937
cmpq %r12, %rdi
je 0x394c
testq %rdi, %rdi
jne 0x392d
movl $0x10, %edi
callq 0x3340
movq %rax, %rdi
movq %rax, 0xa8(%rbx)
movups (%r12), %xmm0
movups %xmm0, (%rdi)
jmp 0x394c
testq %rdi, %rdi
je 0x3941
callq 0x3050
movq $0x0, 0xa8(%rbx)
movq 0xb8(%rbx), %rdi
testq %r15, %r15
je 0x396c
cmpq %r15, %rdi
je 0x397a
callq 0x3050
movq %r15, %rdi
callq 0x34c0
jmp 0x3973
callq 0x3050
xorl %eax, %eax
movq %rax, 0xb8(%rbx)
leaq 0x56c9(%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 0x3a13
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 0x3a13
movl %eax, %edi
callq 0x3190
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x32a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3c41
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x3bad
movl 0x4(%rsp), %eax
movl %eax, %r12d
andl $0x1, %r12d
andl $0x80, %eax
movl %eax, 0x1c(%rsp)
xorl %r14d, %r14d
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 0x3ba0
movl %eax, %r13d
movl %eax, 0x84(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3e5f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x3c37
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x3b29
xorl %esi, %esi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x34a0
testl %eax, %eax
jne 0x3c55
cmpb $0x0, 0x4(%rsp)
jns 0x3ae0
movl $0x1, 0x24(%rsp)
movl %r13d, %edi
movl $0x1, %esi
movl $0x2, %edx
leaq 0x24(%rsp), %rcx
movl $0x4, %r8d
callq 0x30d0
testl %eax, %eax
js 0x3c37
movl %r12d, %ebp
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x3cc9
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %r13d, %edi
callq 0x33e0
cmpl $-0x1, %eax
jne 0x3b17
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x3af1
jmp 0x3bee
movq 0x8(%rsp), %rdi
callq 0x3530
movl %ebp, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %r13d, %edi
callq 0x3470
cmpl $-0x1, %eax
jne 0x3c78
callq 0x3080
movl (%rax), %eax
cmpl $0x71, %eax
je 0x3b87
movl %eax, %ecx
xorl $0x73, %ecx
orl %r12d, %ecx
je 0x3c90
cmpl $0x63, %eax
jne 0x3c65
cmpl $0x0, 0x1c(%rsp)
je 0x3c65
leal 0x1(%r14), %eax
cmpl 0x20(%rsp), %r14d
movl %eax, %r14d
jne 0x3a49
jmp 0x3c37
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x3ba0
callq 0x3200
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x3a38
callq 0x3080
movl (%rax), %edi
callq 0x34d0
leaq 0x54b6(%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 0x3c37
movq 0x8(%rsp), %rdi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x3530
callq 0x3080
movl (%rax), %edi
callq 0x34d0
leaq 0x5450(%rip), %rdx      # 0x9060
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
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
movl %eax, %edi
callq 0x3190
leaq 0x53ea(%rip), %rdx      # 0x904d
jmp 0x3c10
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
callq 0x3ee8
testl %eax, %eax
jne 0x3c37
testb $0x1, 0x4(%rsp)
je 0x3c90
movq %rbx, %rdi
movl $0x1, %esi
callq 0x3e5f
testl %eax, %eax
jne 0x3c37
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
je 0x3cd0
orb $0x2, 0x88(%rbx)
xorl %ebp, %ebp
jmp 0x3c37
xorl %edi, %edi
jmp 0x3bf3
leaq 0x53b7(%rip), %rsi      # 0x908e
movq %rbx, %rdi
callq 0x3794
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x3c37
callq 0x3200
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
jmp 0x3c37