// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x6b72e
jmp 0x6b74d
leaq 0xce03c(%rip), %rdi     # 0x139771
leaq 0xce041(%rip), %rsi     # 0x13977d
movl $0x169, %edx            # imm = 0x169
leaq 0xce056(%rip), %rcx     # 0x13979e
callq 0xa390
movq -0x98(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x703f0
movq %rax, -0xa0(%rbp)
jmp 0x6b76a
movq -0xa0(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x70490
jmp 0x6b77c
leaq -0x38(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
movl $0x1, %esi
movq %rsi, -0xb0(%rbp)
callq 0x705d0
movq %rax, %rdi
callq 0x46a90
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq %rax, -0x58(%rbp)
callq 0x705d0
movq %rax, %rdi
callq 0x46ac0
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x1c(%rbp), %rdx
callq 0x70580
movq %rax, -0xa8(%rbp)
jmp 0x6b7dd
movq -0xa8(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
callq 0x46ac0
movq %rax, -0x70(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x705f0
testb $0x1, %al
jne 0x6b81f
jmp 0x6bb43
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
callq 0x46a90
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x70630
movq %rax, -0xc0(%rbp)
jmp 0x6b85d
movq -0xc0(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %ecx
xorl %eax, %eax
cmpl -0x20(%rbp), %ecx
movb %al, -0xc1(%rbp)
jg 0x6b8c6
movq -0x78(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
callq 0x467f0
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
setb %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x6b8d5
jmp 0x6bb41
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jge 0x6b9b0
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x20(%rbp), %eax
jg 0x6b9b0
movl -0x64(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movq %rax, %rcx
movl -0xd4(%rbp), %eax
movl (%rcx), %ecx
subl -0x1c(%rbp), %ecx
addl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6bb30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6b97f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6d0b0
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x6bb5e
movq -0x40(%rbp), %rdi
callq 0xa210
movq %rax, -0x90(%rbp)
movl $0x0, -0x4(%rbp)
callq 0xaa20
jmp 0x6bb52
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jl 0x6ba54
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x20(%rbp), %eax
jg 0x6ba54
movl -0x64(%rbp), %eax
movl %eax, -0xd8(%rbp)
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl -0xdc(%rbp), %ecx
movq %rax, %rdx
movl -0xd8(%rbp), %eax
subl (%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x64(%rbp)
jmp 0x6bb2e
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jl 0x6bad8
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x6bad8
movl -0x64(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0xe4(%rbp)
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl -0xe4(%rbp), %ecx
movq %rax, %rdx
movl -0xe0(%rbp), %eax
subl (%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x64(%rbp)
jmp 0x6bb2c
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jge 0x6bb2a
leaq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x705d0
movq %rax, %rdi
movq -0x78(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
cmpl -0x20(%rbp), %eax
jle 0x6bb2a
movl -0x64(%rbp), %eax
movl -0x20(%rbp), %ecx
subl -0x1c(%rbp), %ecx
addl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6bb2c
jmp 0x6bb2e
jmp 0x6bb30
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6b868
jmp 0x6bb43
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6d0b0
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0xab50
nopw (%rax,%rax)