// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/deps/hiredis/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;
    char _port[6];  /* strlen("65535"); */
    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
    int blocking = (c->flags & REDIS_BLOCK);

    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(addr,_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) {
        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 (source_addr) {
            int bound = 0;
            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
            if ((rv = getaddrinfo(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;
            }
            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 (redisContextWaitReady(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, %r13
movl %edx, %ecx
movq %rsi, %r14
movq %rdi, %rbx
movl 0x88(%rdi), %ebp
leaq 0x1e9f9(%rip), %rdx     # 0x3917a
leaq 0x22(%rsp), %r12
movl $0x6, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0xb1d0
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)
leaq 0x18(%rsp), %rcx
movq %r14, %rdi
movq %r12, %rsi
callq 0xb540
testl %eax, %eax
je 0x1a80d
leaq 0x30(%rsp), %rdx
movl $0xa, 0x4(%rdx)
leaq 0x22(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq %r14, %rdi
callq 0xb540
testl %eax, %eax
je 0x1a80d
movl %eax, %edi
callq 0xb1f0
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x17424
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1a9f8
movq %r13, 0x28(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x18(%rsp), %r13
testq %r13, %r13
je 0x1a9af
leaq 0x30(%rsp), %r14
movl 0x4(%r13), %edi
movl 0x8(%r13), %esi
movl 0xc(%r13), %edx
callq 0xb600
cmpl $-0x1, %eax
je 0x1a9a2
movl %eax, %r12d
movl %eax, 0x84(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1abaf
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1a9ee
testq %r15, %r15
je 0x1a95b
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
leaq 0x10(%rsp), %rcx
callq 0xb540
testl %eax, %eax
je 0x1a8d2
movl %eax, %edi
callq 0xb1f0
movl $0x80, %esi
movq %r14, %rbp
movq %r15, %r14
leaq 0x60(%rsp), %r15
movq %r15, %rdi
leaq 0x204a7(%rip), %rdx     # 0x3ad4a
movq %rax, %rcx
xorl %eax, %eax
callq 0xb1d0
movq %rbx, %rdi
movl $0x2, %esi
movq %r15, %rdx
movq %r14, %r15
movq %rbp, %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
callq 0x17424
movl $0x5, %eax
jmp 0x1a953
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x1a8f7
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %r12d, %edi
callq 0xb420
cmpl $-0x1, %eax
jne 0x1a942
movq 0x28(%r14), %r14
jmp 0x1a8d7
movq 0x10(%rsp), %rdi
callq 0xb5f0
callq 0xb080
movl (%rax), %edi
callq 0xb560
movl $0x80, %esi
leaq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x2043c(%rip), %rdx     # 0x3ad5d
movq %rax, %rcx
xorl %eax, %eax
callq 0xb1d0
movq %rbx, %rdi
movl $0x2, %esi
movq %r14, %rdx
callq 0x17424
movl $0x5, %eax
jmp 0x1a94e
movq 0x10(%rsp), %rdi
callq 0xb5f0
xorl %eax, %eax
leaq 0x30(%rsp), %r14
testl %eax, %eax
jne 0x1aa0c
movq 0x18(%r13), %rsi
movl 0x10(%r13), %edx
movl %r12d, %edi
callq 0xb510
cmpl $-0x1, %eax
jne 0x1aa32
callq 0xb080
movl (%rax), %eax
cmpl $0x71, %eax
jne 0x1aa13
testq %rbx, %rbx
je 0x1a9a2
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x1a9a2
callq 0xb240
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
movq 0x28(%r13), %r13
testq %r13, %r13
jne 0x1a829
callq 0xb080
movl (%rax), %edi
callq 0xb560
leaq 0x203b1(%rip), %rdx     # 0x3ad73
leaq 0x60(%rsp), %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0xb1d0
movq %rbx, %rdi
movl $0x2, %esi
movq %r14, %rdx
callq 0x17424
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rdi
callq 0xb5f0
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x5, %eax
je 0x1a9ee
jmp 0x1a9f8
xorl $0x73, %eax
movl 0xc(%rsp), %ecx
andl $0x1, %ecx
orl %eax, %ecx
je 0x1aa32
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x1ac42
testl %eax, %eax
jne 0x1a9ee
testb $0x1, 0xc(%rsp)
je 0x1aa4a
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1abaf
testl %eax, %eax
jne 0x1a9ee
leaq 0x60(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x84(%rbx), %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0xb100
cmpl $-0x1, %eax
jne 0x1aaaf
movl %eax, %r14d
leaq 0x2030c(%rip), %rsi     # 0x3ad8b
movq %rbx, %rdi
callq 0x1a644
testq %rbx, %rbx
je 0x1aaa5
movl 0x84(%rbx), %edi
testl %edi, %edi
js 0x1aaa5
callq 0xb240
movl $0xffffffff, 0x84(%rbx) # imm = 0xFFFFFFFF
cmpl $-0x1, %r14d
je 0x1a9ee
orb $0x2, 0x88(%rbx)
xorl %ebp, %ebp
jmp 0x1a9ee