// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_convert.cpp
// Function: BamTools::ConvertTool::ConvertToolPrivate::Run()
bool ConvertTool::ConvertToolPrivate::Run(void) {
 
    // ------------------------------------
    // initialize conversion input/output
        
    // set to default input if none provided
    if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
        m_settings->InputFiles.push_back(Options::StandardIn());
    
    // add files in the filelist to the input file list
    if ( m_settings->HasInputFilelist ) {

        ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
        if ( !filelist.is_open() ) {
            cerr << "bamtools convert ERROR: could not open input BAM file list... Aborting." << endl;
            return false;
        }

        string line;
        while ( getline(filelist, line) )
            m_settings->InputFiles.push_back(line);
    }

    // open input files
    BamMultiReader reader;
    if ( !reader.Open(m_settings->InputFiles) ) {
        cerr << "bamtools convert ERROR: could not open input BAM file(s)... Aborting." << endl;
        return false;
    }

    // if input is not stdin & a region is provided, look for index files
    if ( m_settings->HasInput && m_settings->HasRegion ) {
        if ( !reader.LocateIndexes() ) {
            cerr << "bamtools convert ERROR: could not locate index file(s)... Aborting." << endl;
            return false;
        }
    }

    // retrieve reference data
    m_references = reader.GetReferenceData();

    // set region if specified
    BamRegion region;
    if ( m_settings->HasRegion ) {
        if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {

            if ( reader.HasIndexes() ) {
                if ( !reader.SetRegion(region) ) {
                    cerr << "bamtools convert ERROR: set region failed. Check that REGION describes a valid range" << endl;
                    reader.Close();
                    return false;
                }
            }

        } else {
            cerr << "bamtools convert ERROR: could not parse REGION: " << m_settings->Region << endl;
            cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
                 << endl;
            reader.Close();
            return false;
        }
    }
        
    // if output file given
    ofstream outFile;
    if ( m_settings->HasOutput ) {
      
        // open output file stream
        outFile.open(m_settings->OutputFilename.c_str());
        if ( !outFile ) {
            cerr << "bamtools convert ERROR: could not open " << m_settings->OutputFilename
                 << " for output" << endl;
            return false; 
        }
        
        // set m_out to file's streambuf
        m_out.rdbuf(outFile.rdbuf()); 
    }
    
    // -------------------------------------
    // do conversion based on format
    
     bool convertedOk = true;
    
    // pileup is special case
    // conversion not done per alignment, like the other formats
    if ( m_settings->Format == FORMAT_PILEUP )
        convertedOk = RunPileupConversion(&reader);
    
    // all other formats
    else {
    
        bool formatError = false;
        
        // set function pointer to proper conversion method
        void (BamTools::ConvertTool::ConvertToolPrivate::*pFunction)(const BamAlignment&) = 0;
        if      ( m_settings->Format == FORMAT_BED )   pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintBed;
        else if ( m_settings->Format == FORMAT_FASTA ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFasta;
        else if ( m_settings->Format == FORMAT_FASTQ ) pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFastq;
        else if ( m_settings->Format == FORMAT_JSON )  pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintJson;
        else if ( m_settings->Format == FORMAT_SAM )   pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintSam;
        else if ( m_settings->Format == FORMAT_YAML )  pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintYaml;
        else { 
            cerr << "bamtools convert ERROR: unrecognized format: " << m_settings->Format << endl;
            cerr << "Please see documentation for list of supported formats " << endl;
            formatError = true;
            convertedOk = false;
        }
        
        // if format selected ok
        if ( !formatError ) {
        
            // if SAM format & not omitting header, print SAM header first
            if ( (m_settings->Format == FORMAT_SAM) && !m_settings->IsOmittingSamHeader ) 
                m_out << reader.GetHeaderText();
            
            // iterate through file, doing conversion
            BamAlignment a;
            while ( reader.GetNextAlignment(a) )
                (this->*pFunction)(a);
            
            // set flag for successful conversion
            convertedOk = true;
        }
    }
    
    // ------------------------
    // clean up & exit
    reader.Close();
    if ( m_settings->HasOutput )
        outFile.close();
    return convertedOk;   
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x380, %rsp            # imm = 0x380
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, (%r14)
jne 0x376a5
cmpb $0x0, 0x1(%r14)
jne 0x376a5
addq $0x8, %r14
callq 0x65ebe
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a16c
movq (%rbx), %r14
cmpb $0x1, 0x1(%r14)
jne 0x3775c
movq 0x20(%r14), %rsi
leaq 0x178(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x35bd0
leaq 0x1f0(%rsp), %rdi
callq 0x35130
movl %eax, %ebp
testb %al, %al
je 0x3771f
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x178(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3773c
movq (%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x3a16c
jmp 0x376f5
movq 0x5d8ba(%rip), %rdi     # 0x94fe0
leaq 0x3997f(%rip), %rsi     # 0x710ac
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x37746
leaq 0x20(%rsp), %rdi
callq 0x35730
leaq 0x178(%rsp), %rdi
callq 0x35170
testb %bpl, %bpl
je 0x378db
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x35b70
movq (%rbx), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x353c0
testb %al, %al
je 0x378b9
movq (%rbx), %rax
cmpb $0x1, (%rax)
jne 0x377ae
cmpb $0x1, 0x4(%rax)
jne 0x377ae
leaq 0x178(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x35780
testb %al, %al
je 0x37918
leaq 0x178(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35250
leaq 0x8(%rbx), %rdi
leaq 0x178(%rsp), %rsi
callq 0x3a1a4
leaq 0x178(%rsp), %rdi
callq 0x3a26c
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq (%rbx), %rdi
cmpb $0x1, 0x4(%rdi)
jne 0x37831
subq $-0x80, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x67976
testb %al, %al
je 0x378e2
leaq 0x8(%rsp), %rdi
callq 0x353e0
testb %al, %al
je 0x37831
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x35b20
testb %al, %al
je 0x379ab
leaq 0x178(%rsp), %rdi
callq 0x35ae0
movq (%rbx), %rdi
cmpb $0x1, 0x2(%rdi)
jne 0x3793d
movq 0x40(%rdi), %rsi
leaq 0x178(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x35070
movq 0x178(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x198(%rsp,%rax)
je 0x37921
movq 0x5d760(%rip), %rdi     # 0x94fe0
leaq 0x399d9(%rip), %rsi     # 0x71260
callq 0x355b0
movq (%rbx), %rsi
addq $0x40, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x399e6(%rip), %rsi     # 0x71288
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebp, %ebp
jmp 0x37af9
leaq 0x39834(%rip), %rsi     # 0x710f4
movq 0x5d719(%rip), %rdi     # 0x94fe0
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebp, %ebp
jmp 0x37b06
xorl %ebp, %ebp
jmp 0x37b10
movq 0x5d6f7(%rip), %rdi     # 0x94fe0
leaq 0x398e3(%rip), %rsi     # 0x711d3
callq 0x355b0
movq (%rbx), %rsi
subq $-0x80, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
leaq 0x398f1(%rip), %rsi     # 0x71204
jmp 0x379b2
leaq 0x3981b(%rip), %rsi     # 0x7113a
jmp 0x378c0
leaq 0x20(%rbx), %rdi
movq 0x20(%rbx), %rax
addq -0x18(%rax), %rdi
leaq 0x180(%rsp), %rsi
callq 0x35c10
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5ddf0(%rip), %rsi     # 0x95738
callq 0x3a2a9
testb %al, %al
je 0x37965
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x37c00
movl %eax, %ebp
jmp 0x37ad9
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dd25(%rip), %rsi     # 0x95698
callq 0x3a2a9
testb %al, %al
je 0x37988
leaq 0x387(%rip), %r12       # 0x37d0a
jmp 0x37a57
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dd22(%rip), %rsi     # 0x956b8
callq 0x3a2a9
testb %al, %al
je 0x379d5
leaq 0x43e(%rip), %r12       # 0x37de4
jmp 0x37a57
leaq 0x397cc(%rip), %rsi     # 0x7117e
movq 0x5d627(%rip), %rdi     # 0x94fe0
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
jmp 0x378d4
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dcf5(%rip), %rsi     # 0x956d8
callq 0x3a2a9
testb %al, %al
je 0x379f5
leaq 0x54d(%rip), %r12       # 0x37f40
jmp 0x37a57
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dcf5(%rip), %rsi     # 0x956f8
callq 0x3a2a9
testb %al, %al
je 0x37a15
leaq 0x67f(%rip), %r12       # 0x38092
jmp 0x37a57
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dcf5(%rip), %rsi     # 0x95718
callq 0x3a2a9
testb %al, %al
je 0x37a35
leaq 0xc01(%rip), %r12       # 0x38634
jmp 0x37a57
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dd15(%rip), %rsi     # 0x95758
callq 0x3a2a9
testb %al, %al
je 0x37b22
leaq 0x1077(%rip), %r12      # 0x38ace
movq (%rbx), %rdi
addq $0x60, %rdi
leaq 0x5dcb3(%rip), %rsi     # 0x95718
callq 0x3a2a9
testb %al, %al
je 0x37a9e
movq (%rbx), %rax
cmpb $0x0, 0x6(%rax)
jne 0x37a9e
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x354d0
leaq 0x20(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0x35510
leaq 0x20(%rsp), %rdi
callq 0x35730
leaq 0x20(%rsp), %rdi
callq 0x35200
leaq 0x8(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x352b0
testb %al, %al
je 0x37acc
movq %rbx, %rdi
movq %r15, %rsi
callq *%r12
jmp 0x37ab2
leaq 0x20(%rsp), %rdi
callq 0x35940
movb $0x1, %bpl
leaq 0x8(%rsp), %rdi
callq 0x35190
movq (%rbx), %rax
cmpb $0x1, 0x2(%rax)
jne 0x37af9
leaq 0x178(%rsp), %rdi
callq 0x35870
leaq 0x178(%rsp), %rdi
callq 0x358b0
leaq 0x8(%rsp), %rdi
callq 0x35110
movl %ebp, %eax
addq $0x380, %rsp            # imm = 0x380
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x5d4b7(%rip), %rdi     # 0x94fe0
leaq 0x39764(%rip), %rsi     # 0x71294
callq 0x355b0
movq (%rbx), %rsi
addq $0x60, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
movq 0x5d48d(%rip), %rdi     # 0x94fe0
leaq 0x39768(%rip), %rsi     # 0x712c2
callq 0x355b0
movq %rax, %rdi
callq 0x35300
xorl %ebp, %ebp
jmp 0x37ad9
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35730
jmp 0x37bbd
jmp 0x37b9d
jmp 0x37bab
jmp 0x37b9d
jmp 0x37b9d
jmp 0x37ba2
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x3a26c
jmp 0x37bca
jmp 0x37b9d
jmp 0x37bab
movq %rax, %rbx
jmp 0x37bbd
movq %rax, %rbx
jmp 0x37be3
jmp 0x37bab
jmp 0x37bab
movq %rax, %rbx
jmp 0x37bca
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35940
leaq 0x178(%rsp), %rdi
callq 0x358b0
leaq 0x8(%rsp), %rdi
callq 0x35110
jmp 0x37bf0
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35730
leaq 0x178(%rsp), %rdi
callq 0x35170
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198