// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
callq 0x44b0
movw %bp, 0x20(%rbx)
movw $0x1, 0x22(%rbx)
movb $0x1, 0x24(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbx)
andq $0x0, 0x38(%rbx)
leaq 0x40(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x4470
movl $0x80, 0x4(%rsp)
cmpq $0x0, 0x8(%rbx)
je 0x5b69
leaq 0x28(%rbx), %r15
movq (%rbx), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x4(%rsp), %rdx
callq 0x4140
testl %eax, %eax
je 0x5bb8
pushq $0x3a
popq %rsi
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x4710
cmpq $-0x1, %rax
je 0x5ac0
movq %rax, %r12
leaq -0x1(%rax), %rdx
pushq $0x3a
popq %rsi
movq %rbx, %rdi
callq 0x4710
cmpq $0x1, %r12
jb 0x5a4f
cmpq $-0x1, %rax
jne 0x5ac0
leaq 0x1(%r12), %rdx
leaq 0x28(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %rbx, %rsi
callq 0x4390
cmpq $0x0, 0x30(%rsp)
je 0x5a8c
andw $0x0, 0x20(%rbx)
leaq 0x28(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x6639
cmpq $0xffff, %rax           # imm = 0xFFFF
ja 0x5a8c
movw %ax, 0x20(%rbx)
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x4390
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4430
movq %r12, %rdi
callq 0x41a0
leaq 0x28(%rsp), %rdi
callq 0x41a0
movq 0x8(%rbx), %rdx
cmpq $0x7, %rdx
jb 0x5c13
addq $-0x6, %rdx
leaq 0x28(%rsp), %rdi
pushq $0x6
popq %rcx
movq %rbx, %rsi
callq 0x4390
leaq 0xa687(%rip), %rsi      # 0x10170
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x6655
movl %eax, %ebp
movq %r12, %rdi
callq 0x41a0
testb %bpl, %bpl
je 0x5c13
movb $0x0, 0x8(%rsp)
movq 0x8(%rbx), %rcx
addq $-0x6, %rcx
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4390
movq 0x28(%rsp), %rsi
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x6f90
leaq 0x88(%rsp), %rdi
callq 0x4fd0
leaq 0x28(%rsp), %rdi
callq 0x41a0
cmpb $0x0, 0x8(%rsp)
jne 0x5c13
movw $0x100, 0x22(%rbx)      # imm = 0x100
jmp 0x5c13
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
andq $0x0, 0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
leaq 0x78(%rsp), %rax
movb %cl, -0x2c(%rax)
andl $0x0, -0x30(%rax)
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x28(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x65f4
movq %r14, %rdi
callq 0x4d5a
jmp 0x5c13
movzwl 0xa0(%rsp), %eax
orl $0x8, %eax
cmpl $0xa, %eax
jne 0x5bdf
cmpw $0x0, 0xa2(%rsp)
jne 0x5bdf
rolw $0x8, %bp
movw %bp, 0xa2(%rsp)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rax
xorl %ecx, %ecx
movb %cl, (%rax)
andw $0x0, 0x20(%rbx)
movb %cl, 0x22(%rbx)
movslq 0x4(%rsp), %rax
leaq (%rsp,%rax), %rdx
addq $0xa0, %rdx
leaq 0xa0(%rsp), %rsi
movq %r15, %rdi
callq 0x69b2
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x4160
callq 0x45e0
jmp 0x5a8c
jmp 0x5c3b
jmp 0x5c3b
jmp 0x5c50
movq %rax, %r12
leaq 0x28(%rsp), %rdi
callq 0x41a0
jmp 0x5c53
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
movq %rax, %r12
movq %r14, %rdi
callq 0x41a0
movq %r15, %rdi
callq 0x4fd0
movq %rbx, %rdi
callq 0x41a0
movq %r12, %rdi
callq 0x4660
nop