// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/api/internal/io/HostInfo_p.cpp
// Function: BamTools::Internal::HostInfo::Lookup(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
HostInfo HostInfo::Lookup(const string& hostname, const string& port) {

    HostInfo result;
    result.SetHostName(hostname);
    set<HostAddress> uniqueAddresses;

#ifdef _WIN32
    WindowsSockInit init;
#endif

    HostAddress address;
    address.SetAddress(hostname);

    // if hostname is an IP string ('0.0.0.0' or IPv6 format)
    // do reverse lookup for host domain name
    //
    // TODO: might just remove this... not sure if proper 'hostname' from IP string is needed
    //
    //       so far, haven't been able to successfully fetch a domain name with reverse DNS
    //       getnameinfo() on test sites just returns original IP string. BUT this is likely a rare
    //       case that client code tries to use an IP string and the connection should work fine
    //       anyway. GetHostName() just won't quite show what I was hoping for. :(
    if ( address.HasIPAddress() ) {

        const uint16_t portNum = static_cast<uint16_t>( atoi(port.c_str()) );

        sockaddr_in  sa4;
        sockaddr_in6 sa6;
        sockaddr* sa = 0;
        BT_SOCKLEN_T saSize = 0;

        // IPv4
        if ( address.GetProtocol() == HostAddress::IPv4Protocol ) {
            sa = (sockaddr*)&sa4;
            saSize = sizeof(sa4);
            memset(&sa4, 0, sizeof(sa4));
            sa4.sin_family = AF_INET;
            sa4.sin_addr.s_addr = htonl(address.GetIPv4Address());
            sa4.sin_port = htons(portNum);
        }

        // IPv6
        else if ( address.GetProtocol() == HostAddress::IPv4Protocol ){
            sa = (sockaddr*)&sa6;
            saSize = sizeof(sa6);
            memset(&sa6, 0, sizeof(sa6));
            sa6.sin6_family = AF_INET6;
            memcpy(sa6.sin6_addr.s6_addr, address.GetIPv6Address().data, sizeof(sa6.sin6_addr.s6_addr));
            sa6.sin6_port = htons(portNum);
        }

        // unknown (should be unreachable)
        else BT_ASSERT_X(false, "HostInfo::Lookup: unknown network protocol");

        // lookup name for IP
        char hbuf[NI_MAXHOST];
        char serv[NI_MAXSERV];
        if ( sa && (getnameinfo(sa, saSize, hbuf, sizeof(hbuf), serv, sizeof(serv), 0) == 0) )
            result.SetHostName(string(hbuf));

        // if no domain name found, just use the original address's IP string
        if ( result.HostName().empty() )
            result.SetHostName(address.GetIPString());

        // store address in HostInfo
        uniqueAddresses.insert(address);
    }

    // otherwise, hostname is a domain name ('www.foo.bar')
    // do 'normal' lookup
    else {

        // setup address lookup 'hints'
        addrinfo hints;
        memset(&hints, 0, sizeof(hints));
        hints.ai_family   = AF_UNSPEC;   // allow either IPv4 or IPv6
        hints.ai_socktype = SOCK_STREAM; // for TCP
        hints.ai_protocol = IPPROTO_TCP;

        // fetch addresses for requested hostname/port
        addrinfo* res;
        int status = getaddrinfo(hostname.c_str(), port.c_str(), &hints, &res );

        // if everything OK
        if ( status == 0 ) {

            // iterate over all IP addresses found
            addrinfo* p = res;
            for ( ; p != NULL; p = p->ai_next ) {

                // IPv4
                if ( p->ai_family == AF_INET ) {
                    sockaddr_in* ipv4 = (sockaddr_in*)p->ai_addr;
                    HostAddress a( ntohl(ipv4->sin_addr.s_addr) );
                    uniqueAddresses.insert(a);
                }

                // IPv6
                else if ( p->ai_family == AF_INET6 ) {
                    sockaddr_in6* ipv6 = (sockaddr_in6*)p->ai_addr;
                    HostAddress a(ipv6->sin6_addr.s6_addr);
                    uniqueAddresses.insert(a);
                }
            }

            // if we iterated, but no addresses were stored
            if ( uniqueAddresses.empty() && (p == NULL) ) {
                result.SetError(HostInfo::UnknownError);
                result.SetErrorString("HostInfo: unknown address types found");
            }
        }

        // handle error cases
        else if (
#ifndef _WIN32
                     status == EAI_NONAME
                  || status == EAI_FAIL
#  ifdef EAI_NODATA
                  || status == EAI_NODATA  // officially deprecated, but just in case we happen to hit it
#  endif // EAI_NODATA

#else  // _WIN32
                     WSAGetLastError() == WSAHOST_NOT_FOUND
                  || WSAGetLastError() == WSANO_DATA
                  || WSAGetLastError() == WSANO_RECOVERY
#endif // _WIN32
                )
        {
            result.SetError(HostInfo::HostNotFound);
            result.SetErrorString("HostInfo: host not found");
        }
        else {
            result.SetError(HostInfo::UnknownError);
            result.SetErrorString("HostInfo: unknown error encountered");
        }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
callq 0x31e10
movq %rbx, %rdi
movq %r12, %rsi
callq 0x32060
leaq 0xa8(%rsp), %r14
andl $0x0, (%r14)
andq $0x0, 0x8(%r14)
movq %r14, 0x10(%r14)
movq %r14, 0x18(%r14)
andq $0x0, 0x20(%r14)
leaq 0x60(%rsp), %rdi
callq 0x34a80
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x34c60
leaq 0x60(%rsp), %rdi
callq 0x31b80
testb %al, %al
je 0x5292b
movq (%r15), %rdi
callq 0x34710
movl %eax, %ebp
leaq 0x60(%rsp), %rdi
callq 0x35070
testl %eax, %eax
je 0x52a3c
leaq 0x60(%rsp), %rdi
callq 0x35070
testl %eax, %eax
jne 0x52abe
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movups %xmm0, 0x3c(%rsp)
movw $0xa, 0x30(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x34cb0
leaq 0x30(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
pushq $0x1c
jmp 0x52a61
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movabsq $0x600000001, %rax      # imm = 0x600000001
movq %rax, 0x8(%rdx)
movq (%r12), %rdi
movq (%r15), %rsi
leaq 0x10(%rsp), %r15
movq %r15, %rcx
callq 0x33b00
leal 0x5(%rax), %ecx
cmpl $0x2, %ecx
jb 0x529f9
cmpl $-0x2, %eax
je 0x529f9
testl %eax, %eax
jne 0x52b1b
leaq 0x100(%rsp), %r12
leaq 0xa0(%rsp), %r13
movq (%r15), %r15
testq %r15, %r15
je 0x52b5b
movl 0x4(%r15), %eax
cmpl $0xa, %eax
je 0x529d0
cmpl $0x2, %eax
jne 0x529f3
movq 0x18(%r15), %rax
movl 0x4(%rax), %esi
bswapl %esi
movq %r12, %rdi
callq 0x329e0
movq %r13, %rdi
movq %r12, %rsi
callq 0x336a0
movq %r12, %rdi
callq 0x31950
jmp 0x529f3
movq 0x18(%r15), %rsi
addq $0x8, %rsi
movq %r12, %rdi
callq 0x31f30
movq %r13, %rdi
movq %r12, %rsi
callq 0x336a0
movq %r12, %rdi
callq 0x31950
addq $0x28, %r15
jmp 0x52990
movl $0x1, 0x38(%rbx)
leaq 0xa57f(%rip), %rsi      # 0x5cf86
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x31b30
leaq 0x40(%rbx), %rdi
leaq 0x100(%rsp), %rsi
callq 0x32060
leaq 0x100(%rsp), %rdi
callq 0x33a00
jmp 0x52ba4
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movw $0x2, 0x50(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x34c00
bswapl %eax
leaq 0x50(%rsp), %rdi
movl %eax, 0x4(%rdi)
pushq $0x10
popq %rsi
rolw $0x8, %bp
movw %bp, 0x2(%rdi)
andl $0x0, (%rsp)
leaq 0x100(%rsp), %rdx
leaq 0xd0(%rsp), %r8
pushq $0x20
popq %r9
movl $0x401, %ecx            # imm = 0x401
callq 0x32450
testl %eax, %eax
jne 0x52abe
leaq 0x10(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0xf(%rsp), %rdx
callq 0x31b30
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x32060
leaq 0x10(%rsp), %rdi
callq 0x33a00
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x33ff0
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %r15
callq 0x33a00
testq %r15, %r15
jne 0x52b04
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x344a0
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x32060
leaq 0x10(%rsp), %rdi
callq 0x33a00
leaq 0xa0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x336a0
jmp 0x52bae
movl $0x2, 0x38(%rbx)
leaq 0xa476(%rip), %rsi      # 0x5cf9f
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x31b30
leaq 0x40(%rbx), %rdi
leaq 0x100(%rsp), %rsi
callq 0x32060
leaq 0x100(%rsp), %rdi
callq 0x33a00
jmp 0x52ba4
cmpq $0x0, 0xc8(%rsp)
jne 0x52ba4
movl $0x2, 0x38(%rbx)
leaq 0xa3ec(%rip), %rsi      # 0x5cf60
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x31b30
leaq 0x40(%rbx), %rdi
leaq 0x100(%rsp), %rsi
callq 0x32060
leaq 0x100(%rsp), %rdi
callq 0x33a00
movq 0x10(%rsp), %rdi
callq 0x33cb0
movq 0xb8(%rsp), %rsi
leaq 0x100(%rsp), %rdi
leaq 0xd0(%rsp), %rcx
movq %r14, %rdx
callq 0x31d00
leaq 0x20(%rbx), %rdi
leaq 0x100(%rsp), %rsi
callq 0x31230
leaq 0x100(%rsp), %rdi
callq 0x342e0
leaq 0x60(%rsp), %rdi
callq 0x31950
leaq 0xa0(%rsp), %rdi
callq 0x33730
movq %rbx, %rax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x33a00
jmp 0x52cd4
jmp 0x52cd1
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x33a00
jmp 0x52cd4
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x33a00
jmp 0x52cd4
jmp 0x52cd1
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x33a00
jmp 0x52cd4
jmp 0x52cd1
jmp 0x52cd1
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x33a00
jmp 0x52cd4
jmp 0x52cd1
jmp 0x52cd1
jmp 0x52cd1
jmp 0x52cd1
jmp 0x52c9d
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x342e0
jmp 0x52cd4
jmp 0x52cd1
jmp 0x52cd1
movq %rax, %r14
jmp 0x52ceb
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x31950
jmp 0x52cd4
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x31950
jmp 0x52cd4
jmp 0x52cd1
movq %rax, %r14
jmp 0x52cde
jmp 0x52cd1
jmp 0x52cd1
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x31950
leaq 0xa0(%rsp), %rdi
callq 0x33730
movq %rbx, %rdi
callq 0x34d80
movq %r14, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f
nop