// File: /workspace/github_cpp_makefile_star2_O3/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 0x44a3e
cmpb $0x0, 0x2(%r14)
jne 0x44a3e
addq $0x10, %r14
callq 0x641a2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e190
movq (%rbx), %r14
cmpb $0x1, 0x2(%r14)
jne 0x44b40
movq 0x28(%r14), %rsi
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27b90
leaq 0xe8(%rsp), %rdi
callq 0x27130
testb %al, %al
je 0x44ad0
leaq 0x60(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x50(%rsp), %r15
movq 0x70(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x27ba0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x44b27
movq (%rbx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x2e190
jmp 0x44a8d
movq 0x4a509(%rip), %rbx     # 0x8efe0
leaq 0x2e162(%rip), %rsi     # 0x72c40
movl $0x46, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4a4d7(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x70(%rsp), %rdi
callq 0x27170
xorl %ebx, %ebx
jmp 0x44d13
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x44b36
callq 0x27560
leaq 0x70(%rsp), %rdi
callq 0x27170
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x27b30
movq (%rbx), %rsi
addq $0x10, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x44c6f
leaq 0x70(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x27760
leaq 0x8(%rsp), %rdi
callq 0x273e0
testb %al, %al
je 0x44cb7
leaq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x274d0
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x27270
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x44d27
movq (%rbx), %r14
callq 0x641aa
movq 0x50(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x44bec
testq %rdx, %rdx
je 0x44be2
movq (%rax), %rsi
movq 0x48(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x44bec
movq (%rbx), %rax
movzbl 0x6(%rax), %ebp
xorl $0x1, %ebp
movl %ebp, 0x4c(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x27360
leaq 0x18(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
callq 0x27250
movq (%rbx), %rsi
addq $0x48, %rsi
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x279a0
testb %al, %al
je 0x44d7e
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movq (%rbx), %rdi
cmpb $0x1, 0x5(%rdi)
jne 0x44c5d
addq $0x70, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x6643c
testb %al, %al
je 0x45045
movq (%rbx), %rdi
cmpb $0x1, 0x4(%rdi)
jne 0x44dfc
movl 0x68(%rdi), %edi
jmp 0x44e06
movq 0x4a36a(%rip), %rbx     # 0x8efe0
leaq 0x2e00a(%rip), %rsi     # 0x72c87
movl $0x44, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4a338(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x44d07
movq 0x4a322(%rip), %rbx     # 0x8efe0
leaq 0x2e007(%rip), %rsi     # 0x72ccc
movl $0x57, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4a2f0(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x27110
movl %ebx, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4a2b2(%rip), %rbx     # 0x8efe0
leaq 0x2dfef(%rip), %rsi     # 0x72d24
movl $0x3f, %edx
movq %rbx, %rdi
callq 0x276b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4a280(%rip), %rdi     # 0x8efe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
jmp 0x4501f
movq 0x4a25b(%rip), %rdi     # 0x8efe0
leaq 0x2dfd8(%rip), %rsi     # 0x72d64
movl $0x26, %edx
callq 0x276b0
movq (%rbx), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq 0x4a238(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rbx
leaq 0x2dfd4(%rip), %rsi     # 0x72d8b
movl $0x19, %edx
movq %rax, %rdi
callq 0x276b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq %rbx, %rdi
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
jmp 0x45015
xorl %edi, %edi
callq 0x27530
movq %rax, %rdi
callq 0x27580
leaq 0x70(%rsp), %rdi
callq 0x27220
movq (%rbx), %rax
cmpl $0x0, 0x8(%rax)
je 0x44ff5
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x70(%rsp), %r15
cmpb $0x1, 0x5(%rax)
jne 0x44e99
movl %r12d, 0x48(%rsp)
movl 0x20(%rsp), %r12d
movl 0x28(%rsp), %eax
subl %r12d, %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x271a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x2ddc9(%rip), %xmm0    # 0x72c38
cvttsd2si %xmm0, %ebp
addl %r12d, %ebp
xorl %r12d, %r12d
cmpl 0x20(%rsp), %ebp
jne 0x44e84
movl 0x24(%rsp), %r12d
cmpl 0x28(%rsp), %ebp
jne 0x44f3b
movl 0x2c(%rsp), %eax
incl %eax
jmp 0x44f6e
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x271a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0x2dd64(%rip), %xmm1    # 0x72c38
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movslq %ebp, %rsi
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x450b9
leaq (%rsi,%rsi,4), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl 0x20(%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x271a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x2dd04(%rip), %xmm0    # 0x72c38
cvttsd2si %xmm0, %r13d
jmp 0x44fa5
movslq %ebp, %rsi
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x450c7
leaq (%rsi,%rsi,4), %rcx
movl 0x20(%rax,%rcx,8), %eax
subl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x271a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x2dca0(%rip), %xmm0    # 0x72c38
cvttsd2si %xmm0, %r13d
addl %r12d, %r13d
movl 0x48(%rsp), %r12d
movq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0x27a60
testb %al, %al
je 0x44fe8
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x44fe8
cmpl %ebp, 0x118(%rsp)
jne 0x44fb6
cmpl %r13d, 0x11c(%rsp)
jl 0x44fb6
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x27bd0
incl %r12d
movq (%rbx), %rax
cmpl 0x8(%rax), %r12d
jb 0x44e2f
leaq 0x8(%rsp), %rdi
callq 0x27190
leaq 0x18(%rsp), %rdi
callq 0x27370
leaq 0x70(%rsp), %rdi
callq 0x27920
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x27430
leaq 0x30(%rsp), %rdi
callq 0x2e37a
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x44d09
callq 0x27560
jmp 0x44d09
movq 0x49f94(%rip), %rdi     # 0x8efe0
leaq 0x2dd52(%rip), %rsi     # 0x72da5
movl $0x2f, %edx
callq 0x276b0
movq (%rbx), %rax
movq 0x70(%rax), %rsi
movq 0x78(%rax), %rdx
movq 0x49f71(%rip), %rdi     # 0x8efe0
callq 0x276b0
movq %rax, %rdi
callq 0x27310
movq 0x49f5d(%rip), %rdi     # 0x8efe0
leaq 0x2c170(%rip), %rsi     # 0x711fa
movl $0x5b, %edx
callq 0x276b0
movq 0x49f45(%rip), %rdi     # 0x8efe0
callq 0x27310
leaq 0x8(%rsp), %rdi
callq 0x27190
leaq 0x18(%rsp), %rdi
callq 0x27370
jmp 0x44df5
leaq 0x2c6a6(%rip), %rdi     # 0x71766
xorl %eax, %eax
callq 0x278b0
leaq 0x2c698(%rip), %rdi     # 0x71766
xorl %eax, %eax
callq 0x278b0
jmp 0x45102
jmp 0x450f1
jmp 0x450e3
jmp 0x450f1
jmp 0x450fd
jmp 0x45102
jmp 0x450e3
movq %rax, %rbx
jmp 0x45119
movq %rax, %rbx
jmp 0x45123
jmp 0x450fd
jmp 0x45102
movq %rax, %rbx
jmp 0x4510f
movq %rax, %rbx
jmp 0x45154
jmp 0x45102
movq %rax, %rbx
jmp 0x45136
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x27920
leaq 0x18(%rsp), %rdi
callq 0x27430
leaq 0x30(%rsp), %rdi
callq 0x2e37a
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x45136
callq 0x27560
leaq 0x8(%rsp), %rdi
callq 0x27110
jmp 0x4515e
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x45154
callq 0x27560
leaq 0x70(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b10
movq %rax, %rdi
callq 0x2e1cd