// File: /workspace/github_cpp_makefile_star2_O1/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 0x28(%rsp), %r12
movq %r12, -0x10(%r12)
xorl %eax, %eax
movq %rax, -0x8(%r12)
movb $0x0, (%r12)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbx
movaps %xmm0, (%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, 0x20(%rbx)
movl $0x1, 0x8(%rbx)
leaq 0x38(%rsp), %r15
movq %rax, (%r15)
leaq 0x3293(%rip), %rsi      # 0x8923
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 0x56d8
movl %eax, %edi
callq 0x2280
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x3262(%rip), %rsi      # 0x8926
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x273c
jmp 0x589c
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x57eb
leaq 0x18(%rsp), %r12
movl 0x4(%r13), %edi
movl 0x8(%r13), %esi
movl 0xc(%r13), %edx
callq 0x2080
movl %eax, %ebx
testl %eax, %eax
js 0x57c0
testl %ebp, %ebp
je 0x5779
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
movl %eax, %r15d
testl %eax, %eax
jns 0x5779
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movl $0x1, %edi
leaq 0x31f2(%rip), %rsi      # 0x8940
movq %rax, %rdx
xorl %eax, %eax
callq 0x273c
movq 0x20(%rsp), %rdx
movl $0x12, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x31ee(%rip), %rcx      # 0x895d
callq 0x22d0
testl %r15d, %r15d
js 0x57dc
movq 0x18(%r13), %rsi
movl 0x10(%r13), %edx
movl %ebx, %edi
callq 0x2160
testl %eax, %eax
jns 0x57f2
callq 0x2040
cmpl $0x73, (%rax)
je 0x57f2
movq 0x20(%rsp), %rdx
movl $0x7, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x31c3(%rip), %rcx      # 0x8970
callq 0x22d0
movl %ebx, %edi
callq 0x2340
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x57dc
movq 0x20(%rsp), %rdx
movl $0x6, %r8d
movq %r12, %rdi
xorl %esi, %esi
leaq 0x2e98(%rip), %rcx      # 0x866f
callq 0x22d0
movq 0x28(%r13), %r13
testq %r13, %r13
jne 0x56eb
jmp 0x5865
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x5865
leaq 0x77ae7(%rip), %r15     # 0x7d2e0
movl $0xff, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl 0x4(%r13), %edi
movq 0x18(%r13), %rsi
addq $0x4, %rsi
movq %r15, %rdx
movl $0xff, %ecx
callq 0x21e0
testq %rax, %rax
je 0x5855
movl 0x4(%r13), %edx
movl $0x2, (%rsp)
leaq 0x3154(%rip), %rsi      # 0x898c
leaq 0x77aa1(%rip), %r8      # 0x7d2e0
leaq 0x38(%rsp), %r9
movl $0x3, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x273c
jmp 0x5865
leaq 0x311c(%rip), %rsi      # 0x8978
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
testl %ebx, %ebx
js 0x587a
movq 0x10(%rsp), %rdi
callq 0x2250
leaq 0x28(%rsp), %r12
jmp 0x589c
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rdx
leaq 0x3139(%rip), %rsi      # 0x89c4
movl $0x1, %edi
xorl %eax, %eax
leaq 0x28(%rsp), %r12
callq 0x273c
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x58ab
callq 0x21a0
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x58c2
jmp 0x58c2
jmp 0x58c2
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x58d9
callq 0x21a0
movq %rbx, %rdi
callq 0x2310