// File: /workspace/github_cpp_makefile_star2_O3/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 0x627eb
movq %rcx, %r14
movq %rsi, %r13
movq %rdi, %r12
movq %rdx, 0x8(%rsp)
cmpb $0x1, 0x18(%rdi)
jne 0x62800
movq 0x20(%r12), %rax
movq 0x28(%r12), %rcx
cmpq %rcx, %rax
je 0x62800
movslq (%r13), %rsi
testq %rsi, %rsi
js 0x62971
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $0x6db6db6db6db6db7, %rdx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rdx
cmpl %edx, %esi
jge 0x62971
cmpq %rsi, %rdx
jbe 0x62a93
movq 0x8(%rsp), %rcx
movl (%rcx), %edx
testl %edx, %edx
js 0x62992
movl (%r14), %ecx
cmpl %ecx, %edx
jg 0x62992
imulq $0x38, %rsi, %rdx
addq %rdx, %rax
cmpl 0x20(%rax), %ecx
jg 0x62992
movq %r8, %rbx
movq (%r12), %rdi
movq 0x28(%rax), %rsi
xorl %edx, %edx
callq 0x27a30
testl %eax, %eax
je 0x629e9
movq 0x2c813(%rip), %rbx     # 0x8efe0
leaq 0x135e8(%rip), %rsi     # 0x75dbc
movl $0x23, %edx
movq %rbx, %rdi
callq 0x276b0
movq %rbx, %rdi
callq 0x27310
jmp 0x62858
movq 0x2c7ee(%rip), %rbx     # 0x8efe0
leaq 0x134ec(%rip), %rsi     # 0x75ce5
movl $0x27, %edx
jmp 0x62829
movq %r8, 0x50(%rsp)
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x27a30
testl %eax, %eax
je 0x6286b
movq 0x2c7c3(%rip), %rbx     # 0x8efe0
leaq 0x133a7(%rip), %rsi     # 0x75bcb
movl $0x29, %edx
movq %rbx, %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
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 0x13f9c(%rip), %rdx     # 0x76817
leaq 0x30(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1a0
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x13f7f(%rip), %rdx     # 0x76817
leaq 0x10(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1a0
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x61d2c
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x62068
cmpl $0x0, (%r13)
je 0x628f2
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x61d2c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x62068
incl %r15d
cmpl (%r13), %r15d
jne 0x628d3
movslq (%r14), %rax
cmpq %rax, 0x18(%rsp)
setae %bpl
jb 0x62940
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 0x62940
callq 0x27560
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x62954
callq 0x27560
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6285a
callq 0x27560
jmp 0x6285a
movq 0x2c668(%rip), %rbx     # 0x8efe0
leaq 0x1338e(%rip), %rsi     # 0x75d0d
movl $0x26, %edx
movq %rbx, %rdi
callq 0x276b0
movl (%r13), %esi
jmp 0x629d6
movq 0x2c647(%rip), %rbx     # 0x8efe0
leaq 0x133e3(%rip), %rsi     # 0x75d83
movl $0x35, %edx
movq %rbx, %rdi
callq 0x276b0
movq 0x8(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x27ab0
movq %rax, %rbx
leaq 0x133f3(%rip), %rsi     # 0x75db9
movl $0x2, %edx
movq %rax, %rdi
callq 0x276b0
movl (%r14), %esi
movq %rbx, %rdi
callq 0x27ab0
movq %rax, %rbx
movq (%rax), %rax
jmp 0x62834
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x13e1e(%rip), %rdx     # 0x76817
leaq 0x30(%rsp), %rdi
movq %rdx, %rsi
callq 0x2f1a0
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x62068
movl %eax, %ebp
testb %al, %al
je 0x62a5c
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 0x62a80
callq 0x27560
jmp 0x62a80
movq 0x2c57d(%rip), %rdi     # 0x8efe0
leaq 0x13376(%rip), %rsi     # 0x75de0
movl $0x39, %edx
callq 0x276b0
movq 0x2c565(%rip), %rdi     # 0x8efe0
callq 0x27310
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
jne 0x62967
jmp 0x6285a
leaq 0xeccc(%rip), %rdi      # 0x71766
xorl %eax, %eax
callq 0x278b0
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x62ac0
callq 0x27560
jmp 0x62ac0
jmp 0x62abd
jmp 0x62aeb
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
jne 0x62b18
jmp 0x62b1d
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x62af5
callq 0x27560
jmp 0x62af5
jmp 0x62af2
movq %rax, %rbx
jmp 0x62b09
movq %rax, %rbx
jmp 0x62b1d
jmp 0x62af2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x62b09
callq 0x27560
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x62b1d
callq 0x27560
movq %rbx, %rdi
callq 0x27b10
nop