// File: /workspace/github_cpp_makefile_star2_O3/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 0x4fc22
movl $0x4, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
leaq 0x746c(%rip), %rcx      # 0x57076
xorl %r14d, %r14d
movl $0x18, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285a0
jmp 0x4fde9
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r12
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x261e0
leaq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x26b80
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4fc56
callq 0x27560
movl %ebp, (%rbx)
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movq %rax, 0xf8(%rbx)
movw $0x0, 0x28(%rbx)
leaq 0x30(%rbx), %r14
movq %r14, %rdi
callq 0x28fc0
leaq 0x80(%rbx), %rdi
callq 0x27be0
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x26a50
movq (%r13), %rax
cmpq 0x8(%r13), %rax
je 0x4fd3d
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
movl $0x18, %edx
callq 0x28740
leaq 0x6(%rsp), %rsi
movw $0x0, (%rsi)
leaq 0x60(%rsp), %rdi
callq 0x283e0
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r12
cmpq %r12, %r15
je 0x4fd06
movq %r15, %rdi
callq 0x28d60
movq %rbx, %rdi
movl %eax, %esi
callq 0x28e90
testb %al, %al
je 0x4fd00
movq 0x70(%rbx), %rdi
movzwl 0x6(%rsp), %edx
movq %r15, %rsi
callq 0x28750
testb %al, %al
jne 0x4fd71
addq $0x40, %r15
jmp 0x4fcd0
movl $0x2, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
xorl %r14d, %r14d
leaq 0x7386(%rip), %rcx      # 0x570ae
movl $0x27, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285a0
jmp 0x4fdd5
movl $0x2, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
xorl %r14d, %r14d
leaq 0x7330(%rip), %rcx      # 0x5708f
movl $0x1e, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285a0
jmp 0x4fddf
movl %ebp, (%rbx)
movq 0x70(%rbx), %rsi
leaq 0x20(%rsp), %rdi
callq 0x262f0
leaq 0x38(%rsp), %rsi
movq -0x8(%rsi), %rax
movq %rax, 0x10(%r14)
movups -0x18(%rsi), %xmm0
movups %xmm0, (%r14)
leaq 0x48(%rbx), %rdi
callq 0x26b80
leaq 0x20(%rsp), %rdi
movb 0x38(%rdi), %al
movb %al, 0x68(%rbx)
callq 0x26640
movq 0x70(%rbx), %rdi
callq 0x260f0
movw %ax, 0x28(%rbx)
movq 0x70(%rbx), %rdi
callq 0x27ac0
movl %eax, 0x78(%rbx)
movl $0x1, 0xfc(%rbx)
movb $0x1, %r14b
leaq 0x60(%rsp), %rdi
callq 0x270d0
leaq 0x8(%rsp), %rdi
callq 0x282f0
movl %r14d, %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 0x26640
jmp 0x4fe37
jmp 0x4fe34
jmp 0x4fe34
jmp 0x4fe2f
jmp 0x4fe34
jmp 0x4fe34
movq %rax, %rbx
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4fe4b
callq 0x27560
jmp 0x4fe4b
movq %rax, %rbx
jmp 0x4fe41
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x270d0
leaq 0x8(%rsp), %rdi
callq 0x282f0
movq %rbx, %rdi
callq 0x28c10
movq %rax, %rdi
callq 0x2f047
nop