// File: /workspace/github_cpp_makefile_star2_O1/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 0x44b2b
cmpb $0x0, 0x2(%r14)
jne 0x44b2b
addq $0x10, %r14
callq 0x646e2
movq %r14, %rdi
movq %rax, %rsi
callq 0x2e1b0
movq (%rbx), %rax
cmpb $0x1, 0x2(%rax)
jne 0x44c2b
movq 0x28(%rax), %rsi
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x27ba0
leaq 0xe8(%rsp), %rdi
callq 0x27130
movl %eax, %ebp
testb %al, %al
je 0x44bc1
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 0x27bb0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x44c09
movq (%rbx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x2e1b0
jmp 0x44b7e
movq 0x4b418(%rip), %r14     # 0x8ffe0
leaq 0x2f059(%rip), %rsi     # 0x73c28
movl $0x46, %edx
movq %r14, %rdi
callq 0x276c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b3e6(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x44c18
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x44c18
callq 0x27560
leaq 0x70(%rsp), %rdi
callq 0x27170
testb %bpl, %bpl
je 0x44da2
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x27b40
movq (%rbx), %rsi
addq $0x10, %rsi
movq %r14, %rdi
callq 0x273c0
testb %al, %al
je 0x44d5a
leaq 0x70(%rsp), %rsi
movl $0x1, (%rsi)
leaq 0x8(%rsp), %rdi
callq 0x27770
leaq 0x8(%rsp), %rdi
callq 0x273e0
testb %al, %al
je 0x44da6
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 0x44e16
movq (%rbx), %r14
callq 0x646ea
movq 0x50(%r14), %rdx
xorl %ebp, %ebp
cmpq 0x8(%rax), %rdx
jne 0x44cd7
testq %rdx, %rdx
je 0x44ccd
movq (%rax), %rsi
movq 0x48(%r14), %rdi
callq 0x275f0
testl %eax, %eax
jne 0x44cd7
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 0x279b0
testb %al, %al
je 0x44e6d
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movq (%rbx), %rdi
cmpb $0x1, 0x5(%rdi)
jne 0x44d45
addq $0x70, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x669d4
testb %al, %al
je 0x45134
movq (%rbx), %rax
cmpb $0x1, 0x4(%rax)
jne 0x44eeb
movl 0x68(%rax), %edi
jmp 0x44ef5
movq 0x4b27f(%rip), %rbx     # 0x8ffe0
leaq 0x2ef07(%rip), %rsi     # 0x73c6f
movl $0x44, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b24d(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
jmp 0x44df6
xorl %ebx, %ebx
jmp 0x44e02
movq 0x4b233(%rip), %rbx     # 0x8ffe0
leaq 0x2ef00(%rip), %rsi     # 0x73cb4
movl $0x57, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b201(%rip), %rdi     # 0x8ffe0
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 0x4b1c3(%rip), %rbx     # 0x8ffe0
leaq 0x2eee8(%rip), %rsi     # 0x73d0c
movl $0x3f, %edx
movq %rbx, %rdi
callq 0x276c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x27590
movsbl %al, %esi
movq 0x4b191(%rip), %rdi     # 0x8ffe0
callq 0x27040
movq %rax, %rdi
callq 0x27410
leaq 0x8(%rsp), %rdi
callq 0x27190
xorl %ebx, %ebx
jmp 0x4510e
movq 0x4b16c(%rip), %rdi     # 0x8ffe0
leaq 0x2eed1(%rip), %rsi     # 0x73d4c
movl $0x26, %edx
callq 0x276c0
movq (%rbx), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq 0x4b149(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rbx
leaq 0x2eecd(%rip), %rsi     # 0x73d73
movl $0x19, %edx
movq %rax, %rdi
callq 0x276c0
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 0x45104
xorl %edi, %edi
callq 0x27530
movq %rax, %rdi
callq 0x27580
leaq 0x70(%rsp), %rdi
callq 0x27220
movq (%rbx), %rax
cmpl $0x0, 0x8(%rax)
je 0x450e4
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x70(%rsp), %r15
cmpb $0x1, 0x5(%rax)
jne 0x44f88
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 0x2ecc2(%rip), %xmm0    # 0x73c20
cvttsd2si %xmm0, %ebp
addl %r12d, %ebp
xorl %r12d, %r12d
cmpl 0x20(%rsp), %ebp
jne 0x44f73
movl 0x24(%rsp), %r12d
cmpl 0x28(%rsp), %ebp
jne 0x4502a
movl 0x2c(%rsp), %eax
incl %eax
jmp 0x4505d
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 0x2ec5d(%rip), %xmm1    # 0x73c20
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 0x451a8
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 0x2ebfd(%rip), %xmm0    # 0x73c20
cvttsd2si %xmm0, %r13d
jmp 0x45094
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 0x451b6
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 0x2eb99(%rip), %xmm0    # 0x73c20
cvttsd2si %xmm0, %r13d
addl %r12d, %r13d
movl 0x48(%rsp), %r12d
movq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0x27a70
testb %al, %al
je 0x450d7
movq %r14, %rdi
movq %r15, %rsi
callq 0x27140
testb %al, %al
je 0x450d7
cmpl %ebp, 0x118(%rsp)
jne 0x450a5
cmpl %r13d, 0x11c(%rsp)
jl 0x450a5
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x27be0
incl %r12d
movq (%rbx), %rax
cmpl 0x8(%rax), %r12d
jb 0x44f1e
leaq 0x8(%rsp), %rdi
callq 0x27190
leaq 0x18(%rsp), %rdi
callq 0x27370
leaq 0x70(%rsp), %rdi
callq 0x27930
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x27430
leaq 0x30(%rsp), %rdi
callq 0x2e378
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x44df8
callq 0x27560
jmp 0x44df8
movq 0x4aea5(%rip), %rdi     # 0x8ffe0
leaq 0x2ec4b(%rip), %rsi     # 0x73d8d
movl $0x2f, %edx
callq 0x276c0
movq (%rbx), %rax
movq 0x70(%rax), %rsi
movq 0x78(%rax), %rdx
movq 0x4ae82(%rip), %rdi     # 0x8ffe0
callq 0x276c0
movq %rax, %rdi
callq 0x27310
movq 0x4ae6e(%rip), %rdi     # 0x8ffe0
leaq 0x2d081(%rip), %rsi     # 0x721fa
movl $0x5b, %edx
callq 0x276c0
movq 0x4ae56(%rip), %rdi     # 0x8ffe0
callq 0x27310
leaq 0x8(%rsp), %rdi
callq 0x27190
leaq 0x18(%rsp), %rdi
callq 0x27370
jmp 0x44ee4
leaq 0x2d5b7(%rip), %rdi     # 0x72766
xorl %eax, %eax
callq 0x278c0
leaq 0x2d5a9(%rip), %rdi     # 0x72766
xorl %eax, %eax
callq 0x278c0
jmp 0x451f1
jmp 0x451e0
jmp 0x451d2
jmp 0x451e0
jmp 0x451ec
jmp 0x451f1
jmp 0x451d2
movq %rax, %rbx
jmp 0x45208
movq %rax, %rbx
jmp 0x45212
jmp 0x451ec
jmp 0x451f1
movq %rax, %rbx
jmp 0x451fe
movq %rax, %rbx
jmp 0x45243
jmp 0x451f1
movq %rax, %rbx
jmp 0x45225
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x27930
leaq 0x18(%rsp), %rdi
callq 0x27430
leaq 0x30(%rsp), %rdi
callq 0x2e378
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x45225
callq 0x27560
leaq 0x8(%rsp), %rdi
callq 0x27110
jmp 0x4524d
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x45243
callq 0x27560
leaq 0x70(%rsp), %rdi
callq 0x27170
movq %rbx, %rdi
callq 0x27b20
movq %rax, %rdi
callq 0x2e1ed
nop