// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/api/internal/io/TcpSocket_p.cpp
// Function: BamTools::Internal::TcpSocket::ConnectImpl(BamTools::Internal::HostInfo const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, BamTools::IBamIODevice::OpenMode)
bool TcpSocket::ConnectImpl(const HostInfo& hostInfo,
                            const std::string& port,
                            IBamIODevice::OpenMode mode)
{
    // skip if we're already connected
    if ( m_state == TcpSocket::ConnectedState ) {
        m_error = TcpSocket::SocketResourceError;
        m_errorString = "socket already connected";
        return false;
    }

    // reset socket state
    m_hostName   = hostInfo.HostName();
    m_mode       = mode;
    m_state      = TcpSocket::UnconnectedState;
    m_error      = TcpSocket::NoError;
//    m_localPort  = 0;
    m_remotePort = 0;
//    m_localAddress.Clear();
    m_remoteAddress.Clear();
    m_readBuffer.Clear();

    // fetch candidate addresses for requested host
    vector<HostAddress> addresses = hostInfo.Addresses();
    if ( addresses.empty() ) {
        m_error = TcpSocket::HostNotFoundError;
        m_errorString = "no IP addresses found for host";
        return false;
    }

    // convert port string to integer
    stringstream ss(port);
    uint16_t portNumber(0);
    ss >> portNumber;

    // iterate through adddresses
    vector<HostAddress>::const_iterator addrIter = addresses.begin();
    vector<HostAddress>::const_iterator addrEnd  = addresses.end();
    for ( ; addrIter != addrEnd; ++addrIter) {
        const HostAddress& addr = (*addrIter);

        // try to initialize socket engine with this address
        if ( !InitializeSocketEngine(addr.GetProtocol()) ) {
            // failure to initialize is OK here
            // we'll just try the next available address
            continue;
        }

        // attempt actual connection
        if ( m_engine->Connect(addr, portNumber) ) {

            // if connection successful, update our state & return true
            m_mode = mode;
//            m_localAddress  = m_engine->GetLocalAddress();
//            m_localPort     = m_engine->GetLocalPort();
            m_remoteAddress = m_engine->GetRemoteAddress();
            m_remotePort    = m_engine->GetRemotePort();
            m_cachedSocketDescriptor = m_engine->GetSocketDescriptor();
            m_state = TcpSocket::ConnectedState;
            return true;
        }
    }

    // if we get here, no connection could be made
    m_error = TcpSocket::HostNotFoundError;
    m_errorString = "could not connect to any host addresses";
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, %rbx
cmpl $0x1, 0xfc(%rdi)
jne 0x565cc
movl $0x4, 0xf8(%rbx)
addq $0x100, %rbx            # imm = 0x100
leaq 0x6aeb(%rip), %rsi      # 0x5d0a8
movq %rbx, %rdi
callq 0x35550
xorl %ebp, %ebp
jmp 0x5674e
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r13
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x31250
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x32060
leaq 0x60(%rsp), %rdi
callq 0x33a00
movl %r14d, (%rbx)
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movq %rax, 0xf8(%rbx)
andw $0x0, 0x28(%rbx)
leaq 0x30(%rbx), %r15
movq %r15, %rdi
callq 0x353e0
leaq 0x80(%rbx), %rdi
callq 0x338f0
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x31f20
movq (%rbp), %rax
cmpq 0x8(%rbp), %rax
je 0x566cb
leaq 0x60(%rsp), %rdi
pushq $0x18
popq %rdx
movq %r12, %rsi
callq 0x34880
leaq 0x6(%rsp), %rsi
andw $0x0, (%rsi)
leaq 0x60(%rsp), %rdi
callq 0x34410
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r13
cmpq %r13, %r12
setne %bpl
je 0x566a9
movq %r12, %rdi
callq 0x35070
movq %rbx, %rdi
movl %eax, %esi
callq 0x35250
testb %al, %al
je 0x566a3
movq 0x70(%rbx), %rdi
movzwl 0x6(%rsp), %edx
movq %r12, %rsi
callq 0x34890
testb %al, %al
jne 0x566ef
addq $0x40, %r12
jmp 0x5666f
movl $0x2, 0xf8(%rbx)
addq $0x100, %rbx            # imm = 0x100
leaq 0x6a1f(%rip), %rsi      # 0x5d0e0
movq %rbx, %rdi
callq 0x35550
jmp 0x5673a
movl $0x2, 0xf8(%rbx)
addq $0x100, %rbx            # imm = 0x100
leaq 0x69de(%rip), %rsi      # 0x5d0c1
movq %rbx, %rdi
callq 0x35550
xorl %ebp, %ebp
jmp 0x56744
movl %r14d, (%rbx)
movq 0x70(%rbx), %rsi
leaq 0x20(%rsp), %rdi
callq 0x31450
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x34760
leaq 0x20(%rsp), %rdi
callq 0x31950
movq 0x70(%rbx), %rdi
callq 0x31130
movw %ax, 0x28(%rbx)
movq 0x70(%rbx), %rdi
callq 0x336e0
movl %eax, 0x78(%rbx)
movl $0x1, 0xfc(%rbx)
leaq 0x60(%rsp), %rdi
callq 0x32870
leaq 0x8(%rsp), %rdi
callq 0x342e0
movl %ebp, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x31950
jmp 0x56792
jmp 0x5678a
jmp 0x5678f
jmp 0x5678f
jmp 0x5678f
jmp 0x5678f
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x33a00
jmp 0x567a6
movq %rax, %rbx
jmp 0x5679c
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x32870
leaq 0x8(%rsp), %rdi
callq 0x342e0
movq %rbx, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f