// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %rbx
cmpl $0x1, 0xfc(%rdi)
jne 0x4fcaa
movl $0x4, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
leaq 0x73e4(%rip), %rcx      # 0x57076
xorl %r14d, %r14d
movl $0x18, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285b0
jmp 0x4fe9e
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r12
leaq 0x70(%rsp), %r15
movq %r15, %rdi
callq 0x261e0
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x26b80
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4fce1
callq 0x27560
movl %ebp, (%rbx)
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movq %rax, 0xf8(%rbx)
movw $0x0, 0x28(%rbx)
leaq 0x30(%rbx), %r15
movq %r15, %rdi
callq 0x28fd0
leaq 0x80(%rbx), %rdi
callq 0x27bf0
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x26a50
movq (%r13), %rax
cmpq 0x8(%r13), %rax
je 0x4fe62
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
movl $0x18, %edx
callq 0x28750
leaq 0xe(%rsp), %rsi
movw $0x0, (%rsi)
leaq 0x70(%rsp), %rdi
callq 0x283f0
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %r13
cmpq %r13, %r12
setne %r14b
je 0x4fe27
leaq 0x48(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x28d70
movq %rbx, %rdi
movl %eax, %esi
callq 0x28ea0
movl $0x4, %ecx
testb %al, %al
je 0x4fe11
movq 0x70(%rbx), %rdi
movzwl 0xe(%rsp), %edx
movq %r12, %rsi
callq 0x28760
xorl %ecx, %ecx
testb %al, %al
je 0x4fe11
movl %ebp, (%rbx)
movq 0x70(%rbx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x262f0
movq 0x40(%rsp), %rax
movq %rax, 0x10(%r15)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%r15)
movq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x26b80
movb 0x68(%rsp), %al
movb %al, 0x68(%rbx)
leaq 0x30(%rsp), %rdi
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)
movl $0x1, %ecx
testb $0x3, %cl
jne 0x4fe56
addq $0x40, %r12
cmpq %r13, %r12
setne %r14b
jne 0x4fd71
movl $0x2, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
leaq 0x7268(%rip), %rcx      # 0x570ae
movl $0x27, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285b0
leaq 0x70(%rsp), %rdi
callq 0x270d0
jmp 0x4fe94
movl $0x2, 0xf8(%rbx)
movq 0x108(%rbx), %rdx
addq $0x100, %rbx            # imm = 0x100
xorl %r14d, %r14d
leaq 0x720b(%rip), %rcx      # 0x5708f
movl $0x1e, %r8d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x285b0
leaq 0x18(%rsp), %rdi
callq 0x28300
andb $0x1, %r14b
movl %r14d, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4fed8
jmp 0x4feee
jmp 0x4feee
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4ff05
callq 0x27560
jmp 0x4ff05
movq %rax, %rbx
jmp 0x4fefb
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x26640
jmp 0x4fef1
jmp 0x4feee
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x270d0
leaq 0x18(%rsp), %rdi
callq 0x28300
movq %rbx, %rdi
callq 0x28c20
movq %rax, %rdi
callq 0x2f0bd
nop