// File: /workspace/github_cpp_makefile_star2_O2/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, %r14d
movl %esi, %edx
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, %rbx
movb $0x0, (%rax)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x20(%r12)
movl $0x1, 0x8(%r12)
leaq 0x10(%rsp), %r13
andq $0x0, (%r13)
leaq 0x22d1(%rip), %rsi      # 0x7923
movq %r13, %rdi
xorl %eax, %eax
callq 0x2070
leaq 0x18(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2240
testl %eax, %eax
je 0x5699
movl %eax, %edi
callq 0x2290
leaq 0x22a2(%rip), %rsi      # 0x7926
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x262c
pushq $-0x1
popq %rbp
jmp 0x5822
movq %rbx, 0x8(%rsp)
pushq $-0x1
popq %rbx
leaq 0x20(%rsp), %r13
leaq 0x22c3(%rip), %r12      # 0x7970
movl %ebx, %ebp
movq (%r15), %r15
testq %r15, %r15
je 0x5780
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x2080
movl %eax, %ebp
testl %eax, %eax
js 0x5737
testl %r14d, %r14d
je 0x56fc
movl %ebp, %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2140
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x5748
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebp, %edi
callq 0x2170
testl %eax, %eax
jns 0x57a5
callq 0x2040
cmpl $0x73, (%rax)
je 0x57a5
movq %r13, %rdi
movq %r12, %rsi
callq 0x2330
movl %ebp, %edi
callq 0x2350
movl %ebx, %ebp
jmp 0x5777
movq %r13, %rdi
leaq 0x1f2e(%rip), %rsi      # 0x766f
callq 0x2330
jmp 0x5777
callq 0x2040
movl (%rax), %edi
callq 0x20d0
pushq $0x1
popq %rdi
leaq 0x21e2(%rip), %rsi      # 0x7940
movq %rax, %rdx
xorl %eax, %eax
callq 0x262c
movq %r13, %rdi
leaq 0x21eb(%rip), %rsi      # 0x795d
callq 0x2330
addq $0x28, %r15
jmp 0x56af
testl %ebp, %ebp
jns 0x5818
movq 0x20(%rsp), %rdx
leaq 0x2230(%rip), %rsi      # 0x79c4
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
pushq $-0x2
jmp 0x5693
leaq 0x76b44(%rip), %r14     # 0x7c2f0
movl $0xff, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movl 0x4(%r15), %edi
movq 0x18(%r15), %rsi
addq $0x4, %rsi
movq %r14, %rdx
movl $0xff, %ecx
callq 0x21f0
testq %rax, %rax
je 0x5808
movl 0x4(%r15), %edx
movl $0x2, (%rsp)
leaq 0x21a1(%rip), %rsi      # 0x798c
leaq 0x76afe(%rip), %r8      # 0x7c2f0
pushq $0x3
popq %rdi
leaq 0x10(%rsp), %r9
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x262c
jmp 0x5818
leaq 0x2169(%rip), %rsi      # 0x7978
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
movq 0x18(%rsp), %rdi
callq 0x2260
leaq 0x20(%rsp), %rdi
callq 0x2368
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5843
jmp 0x5843
jmp 0x5843
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2368
movq %rbx, %rdi
callq 0x2310