// File: /workspace/github_cpp_makefile_star2_O1/zeeev[P]DASVC/bamtools/src/utils/bamtools_fasta.cpp
// Function: BamTools::Fasta::FastaPrivate::GetSequence(int const&, int const&, int const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool Fasta::FastaPrivate::GetSequence(const int& refId, const int& start, const int& stop, string& sequence) {
 
    // make sure FASTA file is open
    if ( !IsOpen ) {
        cerr << "FASTA error : file not open for reading" << endl;
        return false;
    }
  
    // use index if available
    if ( HasIndex && !Index.empty() ) {
      
        // validate reference id 
        if ( (refId < 0) || (refId >= (int)Index.size()) ) {
            cerr << "FASTA error: invalid refId specified: " << refId << endl;
            return false;
        }
        
        // retrieve reference index data
        const FastaIndexData& referenceData = Index.at(refId);
        
        // validate stop position 
        if ( (start < 0) || (start > stop) || (stop > referenceData.Length) ) {
            cerr << "FASTA error: invalid start/stop positions specified: " << start << ", " << stop << endl;
            return false;
        }
        
        // seek to beginning of sequence data
        if ( fseeko(Stream, referenceData.Offset, SEEK_SET) != 0 ) {
            cerr << "FASTA error : could not sek in file" << endl;
            return false;
        }
      
        // retrieve full sequence
        string fullSequence = "";
        if ( !GetNextSequence(fullSequence) ) {
            cerr << "FASTA error : could not retrieve sequence from FASTA file" << endl;
            return false;
        }
        
        // set sub-sequence & return success
        const int seqLength = (stop - start) + 1;
        sequence = fullSequence.substr(start, seqLength);
        return true;
    }
    
    // else plow through sequentially
    else {
      
        // rewind FASTA file
        if ( !Rewind() ) {
            cerr << "FASTA error : could not rewind FASTA file" << endl;
            return false;
        }
     
        // iterate through fasta entries
        int currentId = 0;
        string header = "";
        string fullSequence = "";
        
        // get first entry
        GetNextHeader(header);
        GetNextSequence(fullSequence);
        
        while ( currentId != refId ) {
            GetNextHeader(header);
            GetNextSequence(fullSequence);
            ++currentId;
        }
        
        // get desired substring from sequence
        // TODO: error reporting on invalid start/stop positions
        if ( currentId == refId && (fullSequence.length() >= (size_t)stop) ) {          
            const int seqLength = (stop - start) + 1;
            sequence = fullSequence.substr(start, seqLength);
            return true;
        }
      
        // could not get sequence
        return false;
    }
  
    // return success
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
cmpb $0x0, 0x8(%rdi)
je 0x62d35
movq %rcx, %r14
movq %rsi, %r13
movq %rdi, %r12
movq %rdx, 0x8(%rsp)
cmpb $0x1, 0x18(%rdi)
jne 0x62d4a
movq 0x20(%r12), %rax
movq 0x28(%r12), %rcx
cmpq %rcx, %rax
je 0x62d4a
movslq (%r13), %rsi
testq %rsi, %rsi
js 0x62ebb
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rdx
cmpl %edx, %esi
jge 0x62ebb
cmpq %rsi, %rdx
jbe 0x62fdd
movq 0x8(%rsp), %rcx
movl (%rcx), %edx
testl %edx, %edx
js 0x62edc
movl (%r14), %ecx
cmpl %ecx, %edx
jg 0x62edc
imulq $0x38, %rsi, %rdx
addq %rdx, %rax
cmpl 0x20(%rax), %ecx
jg 0x62edc
movq %r8, %rbx
movq (%r12), %rdi
movq 0x28(%rax), %rsi
xorl %edx, %edx
callq 0x27a40
testl %eax, %eax
je 0x62f33
movq 0x2d2c9(%rip), %rbx     # 0x8ffe0
leaq 0x1409e(%rip), %rsi     # 0x76dbc
movl $0x23, %edx
movq %rbx, %rdi
callq 0x276c0
movq %rbx, %rdi
callq 0x27310
jmp 0x62da2
movq 0x2d2a4(%rip), %rbx     # 0x8ffe0
leaq 0x13fa2(%rip), %rsi     # 0x76ce5
movl $0x27, %edx
jmp 0x62d73
movq %r8, 0x50(%rsp)
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a40
testl %eax, %eax
je 0x62db5
movq 0x2d279(%rip), %rbx     # 0x8ffe0
leaq 0x13e5d(%rip), %rsi     # 0x76bcb
movl $0x29, %edx
movq %rbx, %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
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x14a56(%rip), %rdx     # 0x7781b
leaq 0x30(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1b2
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x14a39(%rip), %rdx     # 0x7781b
leaq 0x10(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1b2
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x6224c
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x62594
cmpl $0x0, (%r13)
je 0x62e3c
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x6224c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x62594
incl %r15d
cmpl (%r13), %r15d
jne 0x62e1d
movslq (%r14), %rax
cmpq %rax, 0x18(%rsp)
setae %bpl
jb 0x62e8a
movq 0x8(%rsp), %rcx
movslq (%rcx), %rdx
subl %edx, %eax
incl %eax
movslq %eax, %rcx
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x27600
leaq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x27350
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x62e8a
callq 0x27560
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x62e9e
callq 0x27560
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x62da4
callq 0x27560
jmp 0x62da4
movq 0x2d11e(%rip), %rbx     # 0x8ffe0
leaq 0x13e44(%rip), %rsi     # 0x76d0d
movl $0x26, %edx
movq %rbx, %rdi
callq 0x276c0
movl (%r13), %esi
jmp 0x62f20
movq 0x2d0fd(%rip), %rbx     # 0x8ffe0
leaq 0x13e99(%rip), %rsi     # 0x76d83
movl $0x35, %edx
movq %rbx, %rdi
callq 0x276c0
movq 0x8(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x27ac0
movq %rax, %rbx
leaq 0x13ea9(%rip), %rsi     # 0x76db9
movl $0x2, %edx
movq %rax, %rdi
callq 0x276c0
movl (%r14), %esi
movq %rbx, %rdi
callq 0x27ac0
movq %rax, %rbx
movq (%rax), %rax
jmp 0x62d7e
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x148d8(%rip), %rdx     # 0x7781b
leaq 0x30(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1b2
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x62594
movl %eax, %ebp
testb %al, %al
je 0x62fa6
movl (%r14), %eax
movq 0x8(%rsp), %rcx
movslq (%rcx), %rdx
subl %edx, %eax
incl %eax
movslq %eax, %rcx
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x27600
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x27350
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x62fca
callq 0x27560
jmp 0x62fca
movq 0x2d033(%rip), %rdi     # 0x8ffe0
leaq 0x13e2c(%rip), %rsi     # 0x76de0
movl $0x39, %edx
callq 0x276c0
movq 0x2d01b(%rip), %rdi     # 0x8ffe0
callq 0x27310
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
jne 0x62eb1
jmp 0x62da4
leaq 0xf782(%rip), %rdi      # 0x72766
xorl %eax, %eax
callq 0x278c0
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6300a
callq 0x27560
jmp 0x6300a
jmp 0x63007
jmp 0x63035
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
jne 0x63062
jmp 0x63067
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6303f
callq 0x27560
jmp 0x6303f
jmp 0x6303c
movq %rax, %rbx
jmp 0x63053
movq %rax, %rbx
jmp 0x63067
jmp 0x6303c
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x63053
callq 0x27560
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x63067
callq 0x27560
movq %rbx, %rdi
callq 0x27b20
nop