// File: /workspace/github_cpp_makefile_star2_O1/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 0x4a15c
movl %ecx, %ebx
movl %edx, %ebp
addq $0x8, %rdi
callq 0x4ba38
movq %rsp, %rdi
movq %rax, %rsi
callq 0x4ba8c
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 0x4a0b5
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 0x4a08b
xorl %r14d, %r14d
cmpq %rdx, %rcx
je 0x4a146
subq %rax, %rcx
movq (%rsi), %rdx
movl (%rdx,%rcx), %edi
cmpl %ebx, %edi
jg 0x4a146
sarq $0x2, %rcx
movq 0x8(%rsi), %rsi
subq %rdx, %rsi
sarq $0x2, %rsi
incl %r15d
xorl %r14d, %r14d
cmpq %rsi, %rcx
jae 0x4a146
cmpl %ebp, %edi
jge 0x4a0f9
movl (%rax,%rcx,4), %r8d
cmpl %ebx, %r8d
jle 0x4a11f
cmpl %ebp, %edi
jl 0x4a106
movl (%rax,%rcx,4), %r8d
cmpl %ebx, %r8d
jle 0x4a117
cmpl %ebp, %edi
jge 0x4a12a
cmpl %ebx, (%rax,%rcx,4)
movl $0x0, %edi
cmovgl %r15d, %edi
addl %edi, %r14d
jmp 0x4a13b
subl %edi, %r14d
addl %r8d, %r14d
jmp 0x4a13b
subl %ebp, %r14d
addl %r8d, %r14d
incl %r14d
jmp 0x4a13b
cmpl %ebx, (%rax,%rcx,4)
jle 0x4a137
addl %ebx, %r14d
subl %edi, %r14d
jmp 0x4a13b
cmpl %ebp, %edi
jl 0x4a106
movl 0x4(%rdx,%rcx,4), %edi
incq %rcx
cmpl %ebx, %edi
jle 0x4a0e3
movq %rsp, %rdi
callq 0x4b0a4
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8be9e(%rip), %rdi     # 0xd6001
leaq 0x8bea3(%rip), %rsi     # 0xd600d
leaq 0x8bebd(%rip), %rcx     # 0xd602e
movl $0x169, %edx            # imm = 0x169
callq 0x82c0
movq %rax, %rdi
cmpl $0x1, %edx
jne 0x4a192
callq 0x81c0
callq 0x8760
xorl %r14d, %r14d
jmp 0x4a14e
callq 0x8840
nop