// File: theuni[P]libbtcnet/src/connection.cpp
// Function: CConnectionBase::CConnectionBase(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned short)
CConnectionBase::CConnectionBase(std::string hostIn, unsigned short portIn)
    : host(hostIn), port(portIn), isDns(true), isOnion(false), isSet(true), connection_string(std::move(hostIn))
{
    sockaddr_storage saddr;
    int addrsize = sizeof(saddr);

    if (host.empty()) {
        *this = CConnectionBase();
        return;
    }

    if (evutil_parse_sockaddr_port(host.c_str(), reinterpret_cast<sockaddr*>(&saddr), &addrsize) == 0) {
        unsigned short* pport = nullptr;
        if (saddr.ss_family == AF_INET) {
            pport = &(reinterpret_cast<sockaddr_in*>(&saddr)->sin_port);

        } else if (saddr.ss_family == AF_INET6) {
            pport = &(reinterpret_cast<sockaddr_in6*>(&saddr)->sin6_port);
        }
        if ((pport != nullptr) && *pport == 0)
            *pport = ntohs(portIn);
        host.clear();
        port = 0;
        isDns = false;
        addr.assign(reinterpret_cast<unsigned char*>(&saddr), reinterpret_cast<unsigned char*>(&saddr) + addrsize);
    } else {
        size_t colon = host.find_last_of(':');
        bool fHaveColon = colon != host.npos;
        bool fMultiColon = fHaveColon && (host.find_last_of(':', colon - 1) != host.npos);
        if (fHaveColon && (colon == 0 || !fMultiColon)) {
            std::string portstr = host.substr(colon + 1);
            if (!portstr.empty()) {
                port = 0;
                try {
                    unsigned long longport = std::stoul(portstr);
                    if (longport <= 65535)
                        port = longport;
                } catch (...) {
                }
            }
            host = host.substr(0, colon);
        }

        if (host.size() > 6 && host.substr(host.size() - 6, 6) == ".onion") {
            bool invalid = false;
            DecodeBase32(host.substr(0, host.size() - 6).c_str(), &invalid);
            if (!invalid) {
                isOnion = true;
                isDns = false;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rsi, -0x1e8(%rbp)
movw %dx, %ax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x51b0
movq -0x1d8(%rbp), %rdi
movw -0x12(%rbp), %ax
movw %ax, 0x20(%rdi)
movb $0x1, 0x22(%rdi)
movb $0x0, 0x23(%rdi)
movb $0x1, 0x24(%rdi)
addq $0x28, %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x71a0
movq -0x1e8(%rbp), %rsi
movq -0x1d8(%rbp), %rdi
addq $0x40, %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x5250
movq -0x1d8(%rbp), %rdi
movl $0x80, -0x9c(%rbp)
callq 0x56b0
testb $0x1, %al
jne 0x96a6
jmp 0x96ef
leaq -0x100(%rbp), %rdi
callq 0x9be0
jmp 0x96b4
movq -0x1d8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xae20
leaq -0x100(%rbp), %rdi
callq 0x6980
jmp 0x9b3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
jmp 0x9b44
movq -0x1d8(%rbp), %rdi
callq 0x5190
movq %rax, %rdi
leaq -0x98(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
callq 0x5170
movl %eax, -0x1f4(%rbp)
jmp 0x9719
movl -0x1f4(%rbp), %eax
cmpl $0x0, %eax
jne 0x97e5
movq $0x0, -0x118(%rbp)
movzwl -0x98(%rbp), %eax
cmpl $0x2, %eax
jne 0x9753
leaq -0x98(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x118(%rbp)
jmp 0x9773
movzwl -0x98(%rbp), %eax
cmpl $0xa, %eax
jne 0x9771
leaq -0x98(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x118(%rbp)
jmp 0x9773
cmpq $0x0, -0x118(%rbp)
je 0x97a2
movq -0x118(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0x97a2
movzwl -0x12(%rbp), %edi
callq 0x5740
movw %ax, %cx
movq -0x118(%rbp), %rax
movw %cx, (%rax)
movq -0x1d8(%rbp), %rdi
callq 0x5510
movq -0x1d8(%rbp), %rdi
movw $0x0, 0x20(%rdi)
movb $0x0, 0x22(%rdi)
addq $0x28, %rdi
movslq -0x9c(%rbp), %rax
leaq -0x98(%rbp,%rax), %rdx
leaq -0x98(%rbp), %rsi
callq 0xae90
jmp 0x97e0
jmp 0x9b3b
movq -0x1d8(%rbp), %rdi
movl $0x3a, %esi
movq $-0x1, %rdx
callq 0x57c0
movq %rax, -0x120(%rbp)
cmpq $-0x1, -0x120(%rbp)
setne %al
andb $0x1, %al
movb %al, -0x121(%rbp)
xorl %eax, %eax
testb $0x1, -0x121(%rbp)
movb %al, -0x1f5(%rbp)
je 0x9851
movq -0x1d8(%rbp), %rdi
movq -0x120(%rbp), %rdx
subq $0x1, %rdx
movl $0x3a, %esi
callq 0x57c0
cmpq $-0x1, %rax
setne %al
movb %al, -0x1f5(%rbp)
movb -0x1f5(%rbp), %al
andb $0x1, %al
movb %al, -0x122(%rbp)
testb $0x1, -0x121(%rbp)
je 0x99b0
cmpq $0x0, -0x120(%rbp)
je 0x9883
testb $0x1, -0x122(%rbp)
jne 0x99b0
movq -0x1d8(%rbp), %rsi
movq -0x120(%rbp), %rdx
incq %rdx
leaq -0x148(%rbp), %rdi
movq $-0x1, %rcx
callq 0x5420
jmp 0x98a9
leaq -0x148(%rbp), %rdi
callq 0x56b0
testb $0x1, %al
jne 0x9965
movq -0x1d8(%rbp), %rax
movw $0x0, 0x20(%rax)
xorl %eax, %eax
movl %eax, %esi
leaq -0x148(%rbp), %rdi
movl $0xa, %edx
callq 0xaec0
movq %rax, -0x200(%rbp)
jmp 0x98e8
movq -0x200(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0xffff, -0x150(%rbp) # imm = 0xFFFF
ja 0x9940
movq -0x1d8(%rbp), %rax
movq -0x150(%rbp), %rcx
movw %cx, 0x20(%rax)
jmp 0x9940
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
movq -0x108(%rbp), %rdi
callq 0x5180
callq 0x5680
jmp 0x993c
jmp 0x993e
jmp 0x9965
jmp 0x993e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x52a0
jmp 0x9b44
movq -0x1d8(%rbp), %rsi
movq -0x120(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x170(%rbp), %rdi
callq 0x5420
jmp 0x9985
movq -0x1d8(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x54d0
leaq -0x170(%rbp), %rdi
callq 0x52a0
leaq -0x148(%rbp), %rdi
callq 0x52a0
movq -0x1d8(%rbp), %rdi
callq 0x5330
movq %rax, %rcx
movb $0x0, -0x191(%rbp)
xorl %eax, %eax
cmpq $0x6, %rcx
movb %al, -0x201(%rbp)
jbe 0x9a31
movq -0x1d8(%rbp), %rdi
callq 0x5330
movq -0x1d8(%rbp), %rsi
movq %rax, %rdx
addq $-0x6, %rdx
leaq -0x190(%rbp), %rdi
movl $0x6, %ecx
callq 0x5420
jmp 0x9a01
movb $0x1, -0x191(%rbp)
leaq 0x1b70f(%rip), %rsi     # 0x2511e
leaq -0x190(%rbp), %rdi
callq 0xaf00
movb %al, -0x202(%rbp)
jmp 0x9a23
movb -0x202(%rbp), %al
movb %al, -0x201(%rbp)
jmp 0x9a31
movb -0x201(%rbp), %al
movb %al, -0x203(%rbp)
testb $0x1, -0x191(%rbp)
jne 0x9a48
jmp 0x9a54
leaq -0x190(%rbp), %rdi
callq 0x52a0
movb -0x203(%rbp), %al
testb $0x1, %al
jne 0x9a63
jmp 0x9b39
movq -0x1d8(%rbp), %rdi
movb $0x0, -0x192(%rbp)
callq 0x5330
movq -0x1d8(%rbp), %rsi
movq %rax, %rcx
addq $-0x6, %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x1d0(%rbp), %rdi
callq 0x5420
jmp 0x9a96
leaq -0x1d0(%rbp), %rdi
callq 0x5190
movq %rax, %rsi
leaq -0x1b0(%rbp), %rdi
leaq -0x192(%rbp), %rdx
callq 0xc810
jmp 0x9aba
leaq -0x1b0(%rbp), %rdi
callq 0x5c60
leaq -0x1d0(%rbp), %rdi
callq 0x52a0
testb $0x1, -0x192(%rbp)
jne 0x9b37
movq -0x1d8(%rbp), %rax
movb $0x1, 0x23(%rax)
movb $0x0, 0x22(%rax)
jmp 0x9b37
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
testb $0x1, -0x191(%rbp)
jne 0x9b09
jmp 0x9b15
leaq -0x190(%rbp), %rdi
callq 0x52a0
jmp 0x9b44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x52a0
jmp 0x9b44
jmp 0x9b39
jmp 0x9b3b
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x1e0(%rbp), %rdi
callq 0x52a0
movq -0x1f0(%rbp), %rdi
callq 0x5c60
movq -0x1d8(%rbp), %rdi
callq 0x52a0
movq -0x108(%rbp), %rdi
callq 0x5710
nopw %cs:(%rax,%rax)