// File: /workspace/github_cpp_makefile_star2_O3/renzibei[P]TSProxy/utils.cpp
// Function: tryConnectSocket(char const*, unsigned short, int)
int tryConnectSocket(const char *addrStr, uint16_t port, int isNonBlocking) {
    struct addrinfo hints, *res, *res0;
    int error;
    int s;
    // const char *cause = NULL;
    std::string cause;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    // hints.ai_protocol = IPPROTO_TCP;
    char portStr[8] = {0};
    sprintf(portStr, "%u", port); 

    error = getaddrinfo(addrStr, portStr, &hints, &res0);
    if (error) {
            // errx(1, "%s", gai_strerror(error));
        LogHelper::log(Warn, "Fail to get addr info: %s", gai_strerror(error));
        return -1;
            /*NOTREACHED*/
    }
    s = -1;
    for (res = res0; res; res = res->ai_next) {
        s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
        if (s < 0) {
            cause = "socket";
            continue;
        }

        if (isNonBlocking) {
            int status = fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
            if (status < 0) {
                LogHelper::log(Warn, "Fail to set non-blocking, %s", strerror(errno));
                cause = "cannot nonblocking";
                continue;
            }
        }
        

        if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
            if (errno != EINPROGRESS) {
                cause = "connect";
                close(s);
                s = -1;
                continue;
            }
            

        }

        static char addrS[constants::SocksDomainMaxLength];
        memset(addrS, 0, constants::SocksDomainMaxLength);
        const char* ss = inet_ntop(res->ai_family, &(((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr), addrS, sizeof(addrS));
        if (ss == NULL) {
            LogHelper::log(Error, "Fail to convrt ntop");
        }
        else {
            LogHelper::log(Debug, "Family: %d solve %s ip as %s , port %s; INET family: %d", res->ai_family, addrStr, addrS, portStr, AF_INET);
        }

        break;  /* okay we got one */
    }
    if (s < 0) {
        LogHelper::log(Warn, "Tried all the ip, non connection could be established, cause: %s", cause.c_str());
        return -2;
            // err(1, "%s", cause);
            /*NOTREACHED*/
    }
    freeaddrinfo(res0);
    
    return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, %ebp
movl %esi, %edx
movq %rdi, %r14
leaq 0x30(%rsp), %r13
movq %r13, -0x10(%r13)
xorl %eax, %eax
movq %rax, -0x8(%r13)
movb $0x0, (%r13)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbx
movaps %xmm0, (%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, 0x20(%rbx)
movl $0x1, 0x8(%rbx)
leaq 0x18(%rsp), %r15
movq %rax, (%r15)
leaq 0x2401(%rip), %rsi      # 0x7923
movq %r15, %rdi
xorl %eax, %eax
callq 0x2070
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2230
testl %eax, %eax
je 0x556a
movl %eax, %edi
callq 0x2280
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x23d0(%rip), %rsi      # 0x7926
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x273c
jmp 0x571d
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x5681
leaq 0x20(%rsp), %r12
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x2080
movl %eax, %ebx
testl %eax, %eax
js 0x5610
testl %ebp, %ebp
je 0x55c1
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x562e
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebx, %edi
callq 0x2160
testl %eax, %eax
jns 0x56a0
callq 0x2040
cmpl $0x73, (%rax)
je 0x56a0
movq 0x28(%rsp), %rdx
movl $0x7, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x2373(%rip), %rcx      # 0x7970
callq 0x22d0
movl %ebx, %edi
callq 0x2340
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x566c
movq 0x28(%rsp), %rdx
movl $0x6, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x2048(%rip), %rcx      # 0x766f
callq 0x22d0
jmp 0x566c
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movl $0x1, %edi
leaq 0x22fa(%rip), %rsi      # 0x7940
movq %rax, %rdx
xorl %eax, %eax
callq 0x273c
movq 0x28(%rsp), %rdx
movl $0x12, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x22f6(%rip), %rcx      # 0x795d
callq 0x22d0
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x557d
testl %ebx, %ebx
jns 0x5713
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movq 0x20(%rsp), %rdx
leaq 0x2332(%rip), %rsi      # 0x79c4
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
jmp 0x571d
leaq 0x76c39(%rip), %r12     # 0x7c2e0
movl $0xff, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2120
movl 0x4(%r15), %edi
movq 0x18(%r15), %rsi
addq $0x4, %rsi
movq %r12, %rdx
movl $0xff, %ecx
callq 0x21e0
testq %rax, %rax
je 0x5703
movl 0x4(%r15), %edx
movl $0x2, (%rsp)
leaq 0x22a6(%rip), %rsi      # 0x798c
leaq 0x76bf3(%rip), %r8      # 0x7c2e0
leaq 0x18(%rsp), %r9
movl $0x3, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x273c
jmp 0x5713
leaq 0x226e(%rip), %rsi      # 0x7978
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq 0x10(%rsp), %rdi
callq 0x2250
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x572c
callq 0x21a0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5743
jmp 0x5743
jmp 0x5743
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x5755
callq 0x21a0
movq %rbx, %rdi
callq 0x2310