// File: 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:
subq $0xd8, %rsp
movw %si, %ax
movq %rdi, 0xc8(%rsp)
movw %ax, 0xc6(%rsp)
movl %edx, 0xc0(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x22e0
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x1, 0x98(%rsp)
movq $0x0, 0x50(%rsp)
movzwl 0xc6(%rsp), %edx
leaq 0x43d4(%rip), %rsi      # 0x10928
xorl %eax, %eax
leaq 0x50(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x2080
movq 0x20(%rsp), %rsi
movq 0xc8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
leaq 0x80(%rsp), %rcx
callq 0x2260
movl %eax, 0x2c(%rsp)
jmp 0xc58d
movl 0x2c(%rsp), %eax
movl %eax, 0x7c(%rsp)
cmpl $0x0, 0x7c(%rsp)
je 0xc5f2
movl 0x7c(%rsp), %edi
callq 0x22f0
movq %rax, %rdx
leaq 0x437c(%rip), %rsi      # 0x1092b
xorl %eax, %eax
movl $0x1, %edi
callq 0x28f0
jmp 0xc5bd
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x40(%rsp)
jmp 0xc870
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x23b8
jmp 0xc889
movl $0xffffffff, 0x78(%rsp) # imm = 0xFFFFFFFF
movq 0x80(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0xc812
movq 0x88(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x88(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x88(%rsp), %rax
movl 0xc(%rax), %edx
callq 0x2090
movl %eax, 0x78(%rsp)
cmpl $0x0, 0x78(%rsp)
jge 0xc662
leaq 0x4023(%rip), %rsi      # 0x10674
leaq 0x58(%rsp), %rdi
callq 0x22c0
jmp 0xc65d
jmp 0xc7f9
cmpl $0x0, 0xc0(%rsp)
je 0xc6f9
movl 0x78(%rsp), %edi
movl %edi, 0x18(%rsp)
xorl %edx, %edx
movb %dl, %al
movl $0x3, %esi
callq 0x2150
movl %eax, 0x1c(%rsp)
jmp 0xc68c
movl 0x18(%rsp), %edi
movl 0x1c(%rsp), %edx
orl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
movl $0x4, %esi
callq 0x2150
movl %eax, 0x14(%rsp)
jmp 0xc6ac
movl 0x14(%rsp), %eax
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jge 0xc6f7
callq 0x2040
movl (%rax), %edi
callq 0x20e0
movq %rax, %rdx
leaq 0x4274(%rip), %rsi      # 0x10945
xorl %eax, %eax
movl $0x1, %edi
callq 0x28f0
jmp 0xc6df
leaq 0x427c(%rip), %rsi      # 0x10962
leaq 0x58(%rsp), %rdi
callq 0x22c0
jmp 0xc6f2
jmp 0xc7f9
jmp 0xc6f9
movl 0x78(%rsp), %edi
movq 0x88(%rsp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x2190
movl %eax, 0x10(%rsp)
jmp 0xc717
movl 0x10(%rsp), %eax
cmpl $0x0, %eax
jge 0xc757
callq 0x2040
cmpl $0x73, (%rax)
je 0xc755
leaq 0x4244(%rip), %rsi      # 0x10975
leaq 0x58(%rsp), %rdi
callq 0x22c0
jmp 0xc73d
movl 0x78(%rsp), %edi
callq 0x23a0
jmp 0xc748
movl $0xffffffff, 0x78(%rsp) # imm = 0xFFFFFFFF
jmp 0xc7f9
jmp 0xc757
leaq 0x7abb2(%rip), %rdi     # 0x87310
xorl %esi, %esi
movl $0xff, %edx
callq 0x2140
movq 0x88(%rsp), %rax
movl 0x4(%rax), %edi
movq 0x88(%rsp), %rax
movq 0x18(%rax), %rsi
addq $0x4, %rsi
leaq 0x7ab84(%rip), %rdx     # 0x87310
movl $0xff, %ecx
callq 0x2210
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0xc7b8
leaq 0x41d3(%rip), %rsi      # 0x1097d
xorl %edi, %edi
movb %dil, %al
callq 0x28f0
jmp 0xc7b6
jmp 0xc7f7
movq 0x88(%rsp), %rax
movl 0x4(%rax), %edx
movq 0xc8(%rsp), %rcx
movq %rsp, %rax
movl $0x2, (%rax)
leaq 0x41b6(%rip), %rsi      # 0x10991
leaq 0x7ab2e(%rip), %r8      # 0x87310
xorl %eax, %eax
movl $0x3, %edi
leaq 0x50(%rsp), %r9
callq 0x28f0
jmp 0xc7f5
jmp 0xc7f7
jmp 0xc812
movq 0x88(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x88(%rsp)
jmp 0xc60a
cmpl $0x0, 0x78(%rsp)
jge 0xc850
leaq 0x58(%rsp), %rdi
callq 0x2110
movq %rax, %rdx
leaq 0x419c(%rip), %rsi      # 0x109c9
xorl %eax, %eax
movl $0x1, %edi
callq 0x28f0
jmp 0xc83b
movl $0xfffffffe, 0xd4(%rsp) # imm = 0xFFFFFFFE
movl $0x1, 0x40(%rsp)
jmp 0xc870
movq 0x80(%rsp), %rdi
callq 0x2290
movl 0x78(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl $0x1, 0x40(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x23b8
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
movq 0x48(%rsp), %rdi
callq 0x2360
nopw %cs:(%rax,%rax)