// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/toolkit/bamtools_random.cpp
// Function: BamTools::RandomTool::RandomToolPrivate::Run()
bool RandomTool::RandomToolPrivate::Run(void) {

    // set to default stdin if no input files 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 random 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 our reader
    BamMultiReader reader;
    if ( !reader.Open(m_settings->InputFiles) ) {
        cerr << "bamtools random ERROR: could not open input BAM file(s)... Aborting." << endl;
        return false;
    }

    // look up index files for all BAM files
    reader.LocateIndexes();

    // make sure index data is available
    if ( !reader.HasIndexes() ) {
        cerr << "bamtools random ERROR: could not load index data for all input BAM file(s)... Aborting." << endl;
        reader.Close();
        return false;
    }

    // get BamReader metadata
    const string headerText = reader.GetHeaderText();
    const RefVector references = reader.GetReferenceData();
    if ( references.empty() ) {
        cerr << "bamtools random ERROR: no reference data available... Aborting." << endl;
        reader.Close();
        return false;
    }

    // determine compression mode for BamWriter
    bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
                              !m_settings->IsForceCompression );
    BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
    if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;

    // open BamWriter
    BamWriter writer;
    writer.SetCompressionMode(compressionMode);
    if ( !writer.Open(m_settings->OutputFilename, headerText, references) ) {
        cerr << "bamtools random ERROR: could not open " << m_settings->OutputFilename
             << " for writing... Aborting." << endl;
        reader.Close();
        return false;
    }

    // if user specified a REGION constraint, attempt to parse REGION string
    BamRegion region;
    if ( m_settings->HasRegion && !Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
        cerr << "bamtools random 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();
        writer.Close();
        return false;
    }

    // seed our random number generator
    if ( m_settings->HasRandomNumberSeed )
        srand( m_settings->RandomNumberSeed );
    else
        srand( time(NULL) );

    // grab random alignments
    BamAlignment al;
    unsigned int i = 0;
    while ( i < m_settings->AlignmentCount ) {

        int randomRefId    = 0;
        int randomPosition = 0;

        // use REGION constraints to select random refId & position
        if ( m_settings->HasRegion ) {

            // select a random refId
            randomRefId = getRandomInt(region.LeftRefID, region.RightRefID);

            // select a random position based on randomRefId
            const int lowerBoundPosition = ( (randomRefId == region.LeftRefID)
                                             ? region.LeftPosition
                                             : 0 );
            const int upperBoundPosition = ( (randomRefId == region.RightRefID)
                                             ? region.RightPosition
                                             : (references.at(randomRefId).RefLength - 1) );
            randomPosition = getRandomInt(lowerBoundPosition, upperBoundPosition);
        }

        // otherwise select from all possible random refId & position
        else {

            // select random refId
            randomRefId = getRandomInt(0, (int)references.size() - 1);

            // select random position based on randomRefId
            const int lowerBoundPosition = 0;
            const int upperBoundPosition = references.at(randomRefId).RefLength - 1;
            randomPosition = getRandomInt(lowerBoundPosition, upperBoundPosition);
        }

        // if jump & read successful, save first alignment that overlaps random refId & position
        if ( reader.Jump(randomRefId, randomPosition) ) {
            while ( reader.GetNextAlignmentCore(al) ) {
                if ( al.RefID == randomRefId && al.Position >= randomPosition ) {
                    writer.SaveAlignment(al);
                    ++i;
                    break;
                }
            }
        }
    }

    // cleanup & exit
    reader.Close();
    writer.Close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rdi, %rbx
movq (%rdi), %r14
cmpb $0x0, 0x1(%r14)
jne 0x4d29e
cmpb $0x0, 0x2(%r14)
jne 0x4d29e
addq $0x10, %r14
callq 0x65ebe
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a16c
movq (%rbx), %r14
cmpb $0x1, 0x2(%r14)
jne 0x4d34c
movq 0x28(%r14), %rsi
leaq 0x70(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x35bd0
leaq 0xe8(%rsp), %rdi
callq 0x35130
movl %eax, %ebp
testb %al, %al
je 0x4d312
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x70(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4d32f
movq (%rbx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x3a16c
jmp 0x4d2e8
movq 0x47cc7(%rip), %rdi     # 0x94fe0
leaq 0x25898(%rip), %rsi     # 0x72bb8
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x4d339
leaq 0x50(%rsp), %rdi
callq 0x35730
leaq 0x70(%rsp), %rdi
callq 0x35170
testb %bpl, %bpl
je 0x4d48c
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x35b70
movq (%rbx), %rsi
addq $0x10, %rsi
movq %r14, %rdi
callq 0x353c0
testb %al, %al
je 0x4d46f
leaq 0x70(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x35780
leaq 0x8(%rsp), %rdi
callq 0x353e0
testb %al, %al
je 0x4d493
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x354d0
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x35250
movq 0x38(%rsp), %rax
cmpq 0x40(%rsp), %rax
je 0x4d4bf
movq (%rbx), %r14
callq 0x65ec6
addq $0x48, %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x3a2a9
xorl %ecx, %ecx
testb %al, %al
je 0x4d3ec
movq (%rbx), %rax
movzbl 0x6(%rax), %ecx
xorl $0x1, %ecx
movl %ecx, 0x34(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x35350
leaq 0x18(%rsp), %rdi
leaq 0x34(%rsp), %rsi
callq 0x35230
movq (%rbx), %rsi
addq $0x48, %rsi
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x359d0
testb %al, %al
je 0x4d4eb
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movq (%rbx), %rdi
cmpb $0x1, 0x5(%rdi)
jne 0x4d45d
addq $0x70, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x67976
testb %al, %al
je 0x4d6eb
movq (%rbx), %rdi
cmpb $0x1, 0x4(%rdi)
jne 0x4d535
movl 0x68(%rdi), %edi
jmp 0x4d53f
movq 0x47b6a(%rip), %rdi     # 0x94fe0
leaq 0x25782(%rip), %rsi     # 0x72bff
callq 0x355b0
movq %rax, %rdi
callq 0x35300
jmp 0x4d4b8
xorl %ebx, %ebx
jmp 0x4d6d7
movq 0x47b46(%rip), %rdi     # 0x94fe0
leaq 0x257a3(%rip), %rsi     # 0x72c44
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x4d6cd
movq 0x47b1a(%rip), %rdi     # 0x94fe0
leaq 0x257cf(%rip), %rsi     # 0x72c9c
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x4d6b9
movq 0x47aee(%rip), %rdi     # 0x94fe0
leaq 0x257e3(%rip), %rsi     # 0x72cdc
callq 0x355b0
movq (%rbx), %rsi
addq $0x48, %rsi
movq %rax, %rdi
callq 0x35510
leaq 0x257ef(%rip), %rsi     # 0x72d03
movq %rax, %rdi
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
xorl %ebx, %ebx
jmp 0x4d6af
xorl %edi, %edi
callq 0x35540
movq %rax, %rdi
callq 0x35590
leaq 0x70(%rsp), %rdi
callq 0x35200
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
leaq 0x70(%rsp), %r15
movq (%rbx), %rax
cmpl 0x8(%rax), %r13d
jae 0x4d68f
cmpb $0x1, 0x5(%rax)
jne 0x4d5a3
leaq 0x20(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4d228
movl %eax, %ebp
xorl %eax, %eax
cmpl 0x20(%rsp), %ebp
leaq 0x14(%rsp), %r12
jne 0x4d590
movl 0x24(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl 0x28(%rsp), %ebp
jne 0x4d619
movl 0x2c(%rsp), %eax
jmp 0x4d62b
movq %r15, %r14
movq %rbx, %r15
movl %r13d, %ebx
andl $0x0, 0x14(%rsp)
movq 0x40(%rsp), %rax
subq 0x38(%rsp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
decl %eax
movl %eax, 0x10(%rsp)
leaq 0x14(%rsp), %r12
movq %r12, %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x4d228
movl %eax, %ebp
andl $0x0, 0x14(%rsp)
movslq %eax, %rsi
leaq 0x38(%rsp), %rdi
callq 0x4e05e
movl 0x20(%rax), %eax
decl %eax
movl %eax, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4d228
movl %eax, %r12d
movl %ebx, %r13d
movq %r15, %rbx
movq %r14, %r15
leaq 0x8(%rsp), %r14
jmp 0x4d63f
movslq %ebp, %rsi
leaq 0x38(%rsp), %rdi
callq 0x4e05e
movl 0x20(%rax), %eax
decl %eax
movl %eax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4d228
movl %eax, %r12d
movq %r14, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x35ab0
testb %al, %al
je 0x4d55b
movq %r14, %rdi
movq %r15, %rsi
callq 0x35140
testb %al, %al
je 0x4d55b
cmpl %ebp, 0x118(%rsp)
jne 0x4d654
cmpl %r12d, 0x11c(%rsp)
jl 0x4d654
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x35c00
incl %r13d
jmp 0x4d55b
leaq 0x8(%rsp), %rdi
callq 0x35190
leaq 0x18(%rsp), %rdi
callq 0x35360
leaq 0x70(%rsp), %rdi
callq 0x35940
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x35420
leaq 0x38(%rsp), %rdi
callq 0x3a26c
leaq 0x50(%rsp), %rdi
callq 0x35730
leaq 0x8(%rsp), %rdi
callq 0x35110
movl %ebx, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x478ee(%rip), %rdi     # 0x94fe0
leaq 0x25624(%rip), %rsi     # 0x72d1d
callq 0x355b0
movq (%rbx), %rsi
addq $0x70, %rsi
movq %rax, %rdi
callq 0x35510
movq %rax, %rdi
callq 0x35300
movq 0x478c4(%rip), %rdi     # 0x94fe0
leaq 0x23ae1(%rip), %rsi     # 0x71204
callq 0x355b0
movq %rax, %rdi
callq 0x35300
leaq 0x8(%rsp), %rdi
callq 0x35190
leaq 0x18(%rsp), %rdi
callq 0x35360
jmp 0x4d52e
jmp 0x4d77a
jmp 0x4d76c
jmp 0x4d76c
jmp 0x4d753
jmp 0x4d753
movq %rax, %rbx
jmp 0x4d791
jmp 0x4d767
jmp 0x4d77a
jmp 0x4d77a
jmp 0x4d775
movq %rax, %rbx
jmp 0x4d79b
jmp 0x4d775
movq %rax, %rbx
jmp 0x4d7be
movq %rax, %rbx
jmp 0x4d787
jmp 0x4d77a
jmp 0x4d77a
movq %rax, %rbx
jmp 0x4d7a5
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x35940
leaq 0x18(%rsp), %rdi
callq 0x35420
leaq 0x38(%rsp), %rdi
callq 0x3a26c
leaq 0x50(%rsp), %rdi
callq 0x35730
leaq 0x8(%rsp), %rdi
callq 0x35110
jmp 0x4d7c8
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x35730
leaq 0x70(%rsp), %rdi
callq 0x35170
movq %rbx, %rdi
callq 0x35b50
movq %rax, %rdi
callq 0x3a198