// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/./process_vcf_annotation_tools.h
// Function: BedCoordinateFeatures::getNumBPinRegion(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, int)
int getNumBPinRegion(const string& scaffold, const int start, const int end) {
        assert(start < end);
        try {
            std::vector<std::vector<int> > featuresThisSc = BedFeatureMap.at(scaffold);
            /*for (std::map<std::string, std::vector<std::vector<int> > >::iterator i = accessibleGenomeMap.begin(); i != accessibleGenomeMap.end(); i++) {
             std::cerr << "There is scaffold: " << i->first << " in the map" << std::endl;
             } */
            
            
            // Binary search to find the first feature whose end coordinate is greater
            // or equal to the start of the region in question
            std::vector<int>::iterator itStart = lower_bound(featuresThisSc[1].begin(),featuresThisSc[1].end(),start);
            int numBP = 0;
            if (itStart != featuresThisSc[1].end()) {  // if (start < f[1])    ---  excludind case 1)
                std::vector<int>::size_type index = std::distance(featuresThisSc[1].begin(), itStart);
                
                // Sum the lengths
                while (featuresThisSc[0][index] <= end && index < featuresThisSc[0].size()) { // if (f[0] >= end)   ---    ecluding case 2)
                    //std::cerr << "There are " << featuresThisSc[0].size() << " intervals" << std::endl;
                    //std::cerr << "Starts: " << std::endl;
                    //print_vector(featuresThisSc[0], std::cerr);
                    //std::cerr << "Ends: " << std::endl;
                    //print_vector(featuresThisSc[1], std::cerr);
                    // Now we know the overlap is > 0
                    //std::cerr << "start, end, featuresThisSc[0][index], featuresThisSc[1][index]: " << start << ", " << end << ", " << featuresThisSc[0][index] << ", " << featuresThisSc[1][index] << std::endl;
                    
                    if (featuresThisSc[0][index] < start && featuresThisSc[1][index] <= end)
                        numBP = numBP + (featuresThisSc[1][index] - start) + 1;
                    else if (featuresThisSc[0][index] >= start && featuresThisSc[1][index] <= end)
                        numBP = numBP + (featuresThisSc[1][index] - featuresThisSc[0][index]);
                    else if (featuresThisSc[0][index] >= start && featuresThisSc[1][index] > end)
                        numBP = numBP + (end - featuresThisSc[0][index]);
                    else if (featuresThisSc[0][index] < start && featuresThisSc[1][index] > end)
                        numBP = numBP + (end - start) + 1;
                    index++;
                }
            }
            return numBP;
        } catch (const std::out_of_range& oor) {
            //std::cerr << "No features on scaffold: " << scaffold << std::endl;
            return 0;
        }
    }

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %edx, 0xc(%rsp)
cmpl %ecx, %edx
jge 0x3adb4
movl %ecx, %ebx
addq $0x8, %rdi
callq 0x3c2be
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3c304
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x20(%rax), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3c387
movq 0x10(%rsp), %rcx
cmpq 0x20(%rcx), %rax
je 0x3ad9c
movq (%rcx), %rdx
movq 0x18(%rcx), %rsi
subq %rsi, %rax
sarq $0x2, %rax
movl 0xc(%rsp), %edi
leal 0x1(%rbx), %r8d
xorl %r14d, %r14d
movl (%rdx,%rax,4), %r9d
cmpl %ebx, %r9d
jg 0x3ad9f
movq 0x8(%rcx), %r10
subq %rdx, %r10
sarq $0x2, %r10
cmpq %r10, %rax
jae 0x3ad9f
movl (%rsi,%rax,4), %r10d
cmpl %edi, %r9d
jge 0x3ad79
cmpl %ebx, %r10d
jle 0x3ad86
addl %r8d, %r14d
subl %edi, %r14d
jmp 0x3ad97
cmpl %ebx, %r10d
jle 0x3ad91
addl %ebx, %r14d
subl %r9d, %r14d
jmp 0x3ad97
subl %edi, %r14d
addl %r10d, %r14d
incl %r14d
jmp 0x3ad97
subl %r9d, %r14d
addl %r10d, %r14d
incq %rax
jmp 0x3ad4a
xorl %r14d, %r14d
leaq 0x10(%rsp), %rdi
callq 0x3b73c
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
leaq 0x633e6(%rip), %rdi     # 0x9e1a1
leaq 0x633eb(%rip), %rsi     # 0x9e1ad
leaq 0x63405(%rip), %rcx     # 0x9e1ce
movl $0x169, %edx            # imm = 0x169
callq 0x82f0
movq %rdx, %r14
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3b73c
jmp 0x3adeb
movq %rdx, %r14
movq %rax, %rbx
movq %rbx, %rdi
cmpl $0x1, %r14d
jne 0x3ae03
callq 0x81d0
callq 0x8780
xorl %r14d, %r14d
jmp 0x3ada9
callq 0x8870