// File: zeeev[P]DASVC/bamtools/src/toolkit/bamtools_sort.cpp
// Function: BamTools::SortTool::SortToolPrivate::GenerateSortedRuns()
bool SortTool::SortToolPrivate::GenerateSortedRuns(void) {
    
    // open input BAM file
    BamReader reader;
    if ( !reader.Open(m_settings->InputBamFilename) ) {
        cerr << "bamtools sort ERROR: could not open " << m_settings->InputBamFilename
             << " for reading... Aborting." << endl;
        return false;
    }
    
    // get basic data that will be shared by all temp/output files 
    SamHeader header = reader.GetHeader();
    if ( !header.HasVersion() )
        header.Version = Constants::SAM_CURRENT_VERSION;
    header.SortOrder = ( m_settings->IsSortingByName
                       ? Constants::SAM_HD_SORTORDER_QUERYNAME
                       : Constants::SAM_HD_SORTORDER_COORDINATE );
    m_headerText = header.ToString();
    m_references = reader.GetReferenceData();
    
    // set up alignments buffer
    BamAlignment al;
    vector<BamAlignment> buffer;
    buffer.reserve( (size_t)(m_settings->MaxBufferCount*1.1) );
    bool bufferFull = false;

    // if sorting by name, we need to generate full char data
    // so can't use GetNextAlignmentCore()
    if ( m_settings->IsSortingByName ) {

        // iterate through file
        while ( reader.GetNextAlignment(al)) {

            // check buffer's usage
            bufferFull = ( buffer.size() >= m_settings->MaxBufferCount );

            // store alignments until buffer is "full"
            if ( !bufferFull )
                buffer.push_back(al);

            // if buffer is "full"
            else {
                // so create a sorted temp file with current buffer contents
                // then push "al" into fresh buffer
                CreateSortedTempFile(buffer);
                buffer.push_back(al);
            }
        }
    }

    // sorting by position, can take advantage of GNACore() speedup
    else {

        // iterate through file
        while ( reader.GetNextAlignmentCore(al) ) {

            // check buffer's usage
            bufferFull = ( buffer.size() >= m_settings->MaxBufferCount );

            // store alignments until buffer is "full"
            if ( !bufferFull )
                buffer.push_back(al);

            // if buffer is "full"
            else {
                // create a sorted temp file with current buffer contents
                // then push "al" into fresh buffer
                CreateSortedTempFile(buffer);
                buffer.push_back(al);
            }
        }
    }

    // handle any leftover buffer contents
    if ( !buffer.empty() )
        CreateSortedTempFile(buffer);
    
    // close reader & return success
    reader.Close();
    return true;
}

// Assembly:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x378(%rsp)
movq 0x378(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x370(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x864d0
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdi
movq (%rax), %rsi
addq $0x8, %rsi
callq 0x86980
movb %al, 0x76(%rsp)
jmp 0xdafda
movb 0x76(%rsp), %al
testb $0x1, %al
jne 0xdb07b
jmp 0xdafe8
movq 0x89ff1(%rip), %rdi     # 0x164fe0
leaq 0x5733a(%rip), %rsi     # 0x132330
callq 0x86700
movq %rax, 0x58(%rsp)
jmp 0xdb002
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rax
movq (%rax), %rsi
addq $0x8, %rsi
callq 0x86640
movq %rax, 0x50(%rsp)
jmp 0xdb01f
movq 0x50(%rsp), %rdi
leaq 0x5500b(%rip), %rsi     # 0x130036
callq 0x86700
movq %rax, 0x48(%rsp)
jmp 0xdb037
movq 0x48(%rsp), %rdi
movq 0x89f2d(%rip), %rsi     # 0x164f70
callq 0x86770
jmp 0xdb04a
movb $0x0, 0x387(%rsp)
movl $0x1, 0x360(%rsp)
jmp 0xdb44f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0xdb46d
leaq 0x208(%rsp), %rdi
leaq 0x370(%rsp), %rsi
callq 0x869b0
jmp 0xdb092
leaq 0x208(%rsp), %rdi
callq 0x868f0
movb %al, 0x47(%rsp)
jmp 0xdb0a5
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0xdb0e0
jmp 0xdb0af
leaq 0x8adb2(%rip), %rsi     # 0x165e68
leaq 0x208(%rsp), %rdi
callq 0x86070
jmp 0xdb0c5
jmp 0xdb0e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0xdb43e
movq 0x60(%rsp), %rax
movq (%rax), %rax
testb $0x1, 0x4(%rax)
je 0xdb0fc
leaq 0x8b193(%rip), %rax     # 0x166288
movq %rax, 0x38(%rsp)
jmp 0xdb10a
leaq 0x8b165(%rip), %rax     # 0x166268
movq %rax, 0x38(%rsp)
jmp 0xdb10a
movq 0x38(%rsp), %rsi
leaq 0x228(%rsp), %rdi
callq 0x86070
jmp 0xdb11e
leaq 0x1e8(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x86b20
jmp 0xdb135
movq 0x60(%rsp), %rdi
addq $0x30, %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x86070
jmp 0xdb14d
leaq 0x1e8(%rsp), %rdi
callq 0x86f68
jmp 0xdb15c
leaq 0x370(%rsp), %rdi
callq 0x860a0
movq %rax, 0x30(%rsp)
jmp 0xdb170
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rdi
addq $0x50, %rdi
callq 0x912a0
jmp 0xdb185
leaq 0x90(%rsp), %rdi
callq 0x86260
jmp 0xdb194
leaq 0x78(%rsp), %rdi
callq 0xdcf70
jmp 0xdb1a0
movq 0x60(%rsp), %rax
movq (%rax), %rax
movl 0x48(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x57088(%rip), %xmm1    # 0x132240
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
movq %rsi, %rcx
sarq $0x3f, %rcx
movsd 0x57075(%rip), %xmm1    # 0x132248
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rcx, %rax
orq %rax, %rsi
leaq 0x78(%rsp), %rdi
callq 0xdcf80
jmp 0xdb1ee
movq 0x60(%rsp), %rax
movb $0x0, 0x77(%rsp)
movq (%rax), %rax
testb $0x1, 0x4(%rax)
je 0xdb311
jmp 0xdb207
leaq 0x370(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x86bb0
movb %al, 0x2f(%rsp)
jmp 0xdb222
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0xdb22f
jmp 0xdb30c
leaq 0x78(%rsp), %rdi
callq 0xdd090
movq %rax, 0x20(%rsp)
jmp 0xdb240
movq 0x20(%rsp), %rax
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
setae %al
andb $0x1, %al
movb %al, 0x77(%rsp)
testb $0x1, 0x77(%rsp)
jne 0xdb2e0
leaq 0x78(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdd0b0
jmp 0xdb277
jmp 0xdb307
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x86f68
jmp 0xdb29f
jmp 0xdb43e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
jmp 0xdb42b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x368(%rsp)
movl %eax, 0x364(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xdd180
jmp 0xdb41a
movq 0x60(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xdb4a0
jmp 0xdb2f1
leaq 0x78(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdd0b0
jmp 0xdb305
jmp 0xdb307
jmp 0xdb207
jmp 0xdb3b0
jmp 0xdb313
leaq 0x370(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x86c70
movb %al, 0x1f(%rsp)
jmp 0xdb32e
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0xdb338
jmp 0xdb3ae
leaq 0x78(%rsp), %rdi
callq 0xdd090
movq %rax, 0x10(%rsp)
jmp 0xdb349
movq 0x10(%rsp), %rax
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movl 0x48(%rcx), %ecx
cmpq %rcx, %rax
setae %al
andb $0x1, %al
movb %al, 0x77(%rsp)
testb $0x1, 0x77(%rsp)
jne 0xdb382
leaq 0x78(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdd0b0
jmp 0xdb380
jmp 0xdb3a9
movq 0x60(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xdb4a0
jmp 0xdb393
leaq 0x78(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdd0b0
jmp 0xdb3a7
jmp 0xdb3a9
jmp 0xdb313
jmp 0xdb3b0
leaq 0x78(%rsp), %rdi
callq 0xdd130
movb %al, 0xf(%rsp)
jmp 0xdb3c0
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xdb3dd
jmp 0xdb3ca
movq 0x60(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xdb4a0
jmp 0xdb3db
jmp 0xdb3dd
leaq 0x370(%rsp), %rdi
callq 0x860c0
jmp 0xdb3ec
movb $0x1, 0x387(%rsp)
movl $0x1, 0x360(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xdd180
jmp 0xdb40b
leaq 0x90(%rsp), %rdi
callq 0x86b30
jmp 0xdb41c
jmp 0xdb42b
leaq 0x208(%rsp), %rdi
callq 0x866e0
jmp 0xdb43c
leaq 0x90(%rsp), %rdi
callq 0x86b30
jmp 0xdb43a
jmp 0xdb43e
jmp 0xdb44f
leaq 0x208(%rsp), %rdi
callq 0x866e0
jmp 0xdb44d
jmp 0xdb46d
leaq 0x370(%rsp), %rdi
callq 0x86790
movb 0x387(%rsp), %al
andb $0x1, %al
addq $0x388, %rsp            # imm = 0x388
retq
leaq 0x370(%rsp), %rdi
callq 0x86790
jmp 0xdb47c
jmp 0xdb47e
movq 0x368(%rsp), %rdi
callq 0x86d80
movq %rax, %rdi
callq 0x91290
nopw %cs:(%rax,%rax)