// File: /workspace/github_cpp_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r15d
subl %edx, %r15d
jle 0x471a5
movl %ecx, %ebx
movl %edx, %ebp
addq $0x8, %rdi
callq 0x48a2c
movq %rsp, %rdi
movq %rax, %rsi
callq 0x48a80
movq (%rsp), %rsi
movq 0x18(%rsi), %rax
movq 0x20(%rsi), %rdx
movq %rdx, %rdi
subq %rax, %rdi
sarq $0x2, %rdi
movq %rax, %rcx
testq %rdi, %rdi
jle 0x47125
movq %rax, %rcx
movq %rdi, %r8
shrq %r8
movq %r8, %r9
notq %r9
addq %rdi, %r9
leaq (%rcx,%r8,4), %rdi
addq $0x4, %rdi
cmpl %ebp, -0x4(%rdi)
cmovlq %rdi, %rcx
cmovgeq %r8, %r9
movq %r9, %rdi
testq %r9, %r9
jg 0x470fb
xorl %r14d, %r14d
cmpq %rdx, %rcx
je 0x4718f
subq %rax, %rcx
movq (%rsi), %rdx
movl (%rdx,%rcx), %edi
cmpl %ebx, %edi
jg 0x4718f
sarq $0x2, %rcx
movq 0x8(%rsi), %rsi
subq %rdx, %rsi
sarq $0x2, %rsi
incl %r15d
xorl %r14d, %r14d
cmpq %rsi, %rcx
jae 0x4718f
movl (%rax,%rcx,4), %r8d
cmpl %ebp, %edi
jge 0x47166
cmpl %ebx, %r8d
jle 0x47173
addl %r15d, %r14d
jmp 0x47184
cmpl %ebx, %r8d
jle 0x4717e
addl %ebx, %r14d
subl %edi, %r14d
jmp 0x47184
subl %ebp, %r14d
addl %r8d, %r14d
incl %r14d
jmp 0x47184
subl %edi, %r14d
addl %r8d, %r14d
movl 0x4(%rdx,%rcx,4), %edi
incq %rcx
cmpl %ebx, %edi
jle 0x4714f
movq %rsp, %rdi
callq 0x480b0
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x880c1(%rip), %rdi     # 0xcf26d
leaq 0x880c6(%rip), %rsi     # 0xcf279
leaq 0x880e0(%rip), %rcx     # 0xcf29a
movl $0x169, %edx            # imm = 0x169
callq 0x82c0
movq %rax, %rdi
cmpl $0x1, %edx
jne 0x471db
callq 0x81c0
callq 0x8760
xorl %r14d, %r14d
jmp 0x47197
callq 0x8840