// File: zeeev[P]DASVC/bamtools/src/api/internal/bam/BamMultiReader_p.cpp
// Function: BamTools::Internal::BamMultiReaderPrivate::CloseFiles(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&)
bool BamMultiReaderPrivate::CloseFiles(const vector<string>& filenames) {

    bool errorsEncountered = false;
    m_errorString.clear();

    // iterate over filenames
    vector<string>::const_iterator filesIter = filenames.begin();
    vector<string>::const_iterator filesEnd  = filenames.end();
    for ( ; filesIter != filesEnd; ++filesIter ) {
        const string& filename = (*filesIter);
        if ( filename.empty() ) continue;

        // iterate over readers
        vector<MergeItem>::iterator readerIter = m_readers.begin();
        vector<MergeItem>::iterator readerEnd  = m_readers.end();
        for ( ; readerIter != readerEnd; ++readerIter ) {
            MergeItem& item = (*readerIter);
            BamReader* reader = item.Reader;
            if ( reader == 0 ) continue;

            // if reader matches requested filename
            if ( reader->GetFilename() == filename ) {

                // remove reader's entry from alignment cache
                m_alignmentCache->Remove(reader);

                // clean up reader & its alignment
                if ( !reader->Close() ) {
                    m_errorString.append(1, '\t');
                    m_errorString.append(reader->GetErrorString());
                    m_errorString.append(1, '\n');
                    errorsEncountered = true;
                }
                delete reader;
                reader = 0;

                // delete reader's alignment entry
                BamAlignment* alignment = item.Alignment;
                delete alignment;
                alignment = 0;

                // remove reader from reader list
                m_readers.erase(readerIter);

                // on match, just go on to next filename
                // (no need to keep looking and item iterator is invalid now anyway)
                break;
            }
        }
    }

    // make sure we clean up properly if all readers were closed
    if ( m_readers.empty() ) {

        // clean up merger
        if ( m_alignmentCache ) {
            m_alignmentCache->Clear();
            delete m_alignmentCache;
            m_alignmentCache = 0;
        }

        // reset merge flags
        m_hasUserMergeOrder = false;
        m_mergeOrder = BamMultiReader::RoundRobinMerge;
    }

    // return whether all readers closed OK
    return !errorsEncountered;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movb $0x0, 0xe7(%rsp)
addq $0x28, %rdi
callq 0x78800
movq 0xe8(%rsp), %rdi
callq 0x71c40
movq %rax, 0xd8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x74ae0
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x750f0
testb $0x1, %al
jne 0x9df3d
jmp 0x9e214
leaq 0xd8(%rsp), %rdi
callq 0x71410
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x7aba0
testb $0x1, %al
jne 0x9df65
jmp 0x9df6a
jmp 0x9e202
movq 0x38(%rsp), %rdi
callq 0x73770
movq 0x38(%rsp), %rdi
movq %rax, 0xc0(%rsp)
callq 0x74890
movq %rax, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x74d10
testb $0x1, %al
jne 0x9dfac
jmp 0x9e200
leaq 0xc0(%rsp), %rdi
callq 0x762d0
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
jne 0x9dfe4
jmp 0x9e1ee
movq 0xa8(%rsp), %rsi
leaq 0x88(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x741b0
movq 0x28(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x74120
movb %al, 0x37(%rsp)
jmp 0x9e016
leaq 0x88(%rsp), %rdi
callq 0x7cce8
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x9e030
jmp 0x9e1ec
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0xa8(%rsp), %rsi
movq (%rdi), %rax
callq *0x30(%rax)
movq 0xa8(%rsp), %rdi
callq 0x71570
testb $0x1, %al
jne 0x9e114
movq 0x38(%rsp), %rdi
addq $0x28, %rdi
movq %rdi, 0x18(%rsp)
movl $0x1, %esi
movl $0x9, %edx
callq 0x76f60
movq 0xa8(%rsp), %rsi
leaq 0x58(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x74350
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x7c8b0
jmp 0x9e0a1
leaq 0x58(%rsp), %rdi
callq 0x7cce8
movq 0x38(%rsp), %rdi
addq $0x28, %rdi
movl $0x1, %esi
movl $0xa, %edx
callq 0x76f60
movb $0x1, 0xe7(%rsp)
jmp 0x9e114
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x7cce8
jmp 0x9e0ed
jmp 0x9e28c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x7cce8
jmp 0x9e10f
jmp 0x9e28c
movq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x9e13d
movq 0x10(%rsp), %rdi
callq 0x76ec0
jmp 0x9e133
movq 0x10(%rsp), %rdi
callq 0x764d0
movq $0x0, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x9e180
movq 0x8(%rsp), %rdi
callq 0x7a0d0
jmp 0x9e176
movq 0x8(%rsp), %rdi
callq 0x764d0
movq 0x38(%rsp), %rdi
movq $0x0, 0x50(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rsi
callq 0x722b0
movq %rax, 0x40(%rsp)
jmp 0x9e200
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
callq 0x764d0
jmp 0x9e28c
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
callq 0x764d0
jmp 0x9e28c
jmp 0x9e1ee
leaq 0xc0(%rsp), %rdi
callq 0x74ed0
jmp 0x9df8e
jmp 0x9e202
leaq 0xd8(%rsp), %rdi
callq 0x75880
jmp 0x9df1f
movq 0x38(%rsp), %rdi
callq 0x764e0
testb $0x1, %al
jne 0x9e224
jmp 0x9e279
movq 0x38(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x9e269
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x9e25c
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x38(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x38(%rsp), %rax
movb $0x0, 0x20(%rax)
movl $0x0, 0x24(%rax)
movb 0xe7(%rsp), %al
xorb $-0x1, %al
andb $0x1, %al
addq $0xf8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw %cs:(%rax,%rax)