// File: /workspace/github_cpp_makefile_star2_O3/schneebergerlab[P]GameteBinning/asPollinator_1.0.cpp
// Function: find_bed(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, bool*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>*)
bool find_bed(string poPat, string pmflag, int pollenid, bool* out, vector<string>* bedregions)
{
    /*
      PPuPPMMuMMuuuMMPuuuPuuMuM
      =>
      PPuPP:1:5
           MMuMMuuuMM:6:15
                     PuuuPuu:16:22
                            MuM:23:25
    */
    if(poPat.size()==0) return false; // should never happen
    //
    size_t posp = poPat.find("P");
    size_t posm = poPat.find("M");
    //
    if(posp==std::string::npos && posm==std::string::npos)
    {
        // only "U"
        *out = false;
    }else
    if(posp==std::string::npos && posm!=std::string::npos)
    {
        // only "M"
        std::stringstream ss;
        ss.str("");        
        ss << "M:" << "1:" << poPat.size(); // real position index now
        (*bedregions).push_back(ss.str());
        *out = true;
    }else
    if(posp!=std::string::npos && posm==std::string::npos)
    {
        // only "P"
        std::stringstream ss;
        ss.str("");               
        ss << "P:" << "1:" << poPat.size(); // real position index now
        (*bedregions).push_back(ss.str());      
        *out = true;
    }else
    {    
        // posp!=std::string::npos && posm!=std::string::npos
        // mixture of PMu
        string thisGT            = ""; // starting with P or M?
        size_t thisbegin         = 0;  // 0 based system - this needs to be increased by 1 to get real position index
        size_t thisend           = 0;  // 0 based system - this needs to be increased by 1 to get real position index
        size_t thisend_exclude_u = 0;
        if(posp < posm)
        {
            thisend            = posp;
            thisGT             = "P";
            thisend_exclude_u  = posp;
        }else
        {
            thisend            = posm;
            thisGT             = "M";
            thisend_exclude_u  = posm;
        }
        for(int jj=thisend+1; jj<poPat.size(); jj ++)
        {
            if(poPat.substr(jj, 1).compare("u")==0) 
            {
                thisend           += 1; // extending 
                // reach end of pattern
                if(thisend==poPat.size()-1)
                {
                    // collect current region
                    std::stringstream ss;
                    ss.str("");  
                    ss << thisGT << ":" << thisbegin+1 << ":" << thisend+1; // real position index now        
                    (*bedregions).push_back(ss.str());
                    ss.str().clear();                       
                }
                continue;
            }else
            if(poPat.substr(jj, 1).compare(thisGT)==0)
            {
                thisend           += 1; // extending
                thisend_exclude_u  = jj;
                // reach end of pattern
                if(thisend==poPat.size()-1)
                {
                    // collect current region
                    std::stringstream ss;
                    ss.str("");  
                    ss << thisGT << ":" << thisbegin+1 << ":" << thisend+1; // real position index now        
                    (*bedregions).push_back(ss.str());
                    ss.str().clear();
                }
            }else
            {   // find a switch from thisGT to anotherGT (P->M or M->P)
                // collect current region
                std::stringstream ss;
                ss.str("");
                size_t left_marker_pos = floor((thisend_exclude_u+jj)/2);           // select the middle-left of the ambiguity region
                ss << thisGT << ":" << thisbegin+1 << ":" << left_marker_pos+1;     // real position index now
                (*bedregions).push_back(ss.str());
                ss.str().clear();
                // next region
                if(thisGT.compare("M") == 0) 
                {
                    thisGT = "P";
                }else
                if(thisGT.compare("P") == 0) 
                {
                    thisGT = "M";
                }else ;       
                /* 0-based marker sequence index, left_marker_pos+1 =< jj
                   theoretically, thisbegin is the middle marker position where PuuM or MuuuP happens. 
                      e.g, MuMuuuPuP
                           123456789
                      thisend_exclude_u = 3
                      jj                = 7
                      left_marker_pos = (3+7)/2 = 5 
                      next begin would be left_marker_pos+1 = 6 with new thisend_exclude_u = 6+1
                */
                thisbegin         = left_marker_pos+1;      
                thisend           = jj;
                thisend_exclude_u = jj;
            }
        }
        *out = true;
    }
    //
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rcx, 0x78(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x11a7b
movq %r8, %r15
movq %rdi, %rbp
movq %rax, 0x68(%rsp)
leaq 0x8319(%rip), %rsi      # 0x19b82
movl $0x1, %ecx
xorl %edx, %edx
callq 0x3440
movq %rax, %r14
leaq 0x831c(%rip), %rsi      # 0x19b9b
movl $0x1, %ecx
movq %rbp, %rdi
xorl %edx, %edx
callq 0x3440
movq %rax, %rbx
andq %r14, %rax
cmpq $-0x1, %rax
je 0x11966
cmpq $-0x1, %r14
setne %al
cmpq $-0x1, %rbx
sete %cl
orb %al, %cl
jne 0x11973
leaq 0xa0(%rsp), %rdi
callq 0x31d0
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x85f7(%rip), %rdx      # 0x19ec9
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x16550
leaq 0xb8(%rsp), %rbx
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x3370
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x1190b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0xb0(%rsp), %r14
leaq 0x8225(%rip), %rsi      # 0x19b3f
movl $0x2, %edx
movq %r14, %rdi
callq 0x32f0
leaq 0x8214(%rip), %rsi      # 0x19b42
movl $0x2, %edx
movq %r14, %rdi
callq 0x32f0
movq 0x8(%rbp), %rsi
movq %r14, %rdi
callq 0x31a0
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x33c0
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x17526
jmp 0x11a36
movq 0x78(%rsp), %rax
movb $0x0, (%rax)
jmp 0x11a76
cmpq $-0x1, %rbx
setne %al
cmpq $-0x1, %r14
sete %cl
orb %al, %cl
jne 0x11a93
leaq 0xa0(%rsp), %rdi
callq 0x31d0
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x8522(%rip), %rdx      # 0x19ec9
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x16550
leaq 0xb8(%rsp), %rbx
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x3370
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x119e0
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0xb0(%rsp), %r14
leaq 0x8156(%rip), %rsi      # 0x19b45
movl $0x2, %edx
movq %r14, %rdi
callq 0x32f0
leaq 0x813f(%rip), %rsi      # 0x19b42
movl $0x2, %edx
movq %r14, %rdi
callq 0x32f0
movq 0x8(%rbp), %rsi
movq %r14, %rdi
callq 0x31a0
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x33c0
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x17526
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x11a4d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x78(%rsp), %rax
movb $0x1, (%rax)
movq 0xf55c(%rip), %rsi      # 0x20fb8
leaq 0xa0(%rsp), %rdi
callq 0x31f0
leaq 0x120(%rsp), %rdi
callq 0x30e0
movq 0x68(%rsp), %rax
testq %rax, %rax
setne %al
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x8426(%rip), %rdx      # 0x19ec9
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x16550
movq 0x30(%rsp), %rdx
cmpq %rbx, %r14
jae 0x11ad5
leaq 0x80c1(%rip), %rcx      # 0x19b82
leaq 0x28(%rsp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
callq 0x33a0
jmp 0x11af1
leaq 0x80bf(%rip), %rcx      # 0x19b9b
leaq 0x28(%rsp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
callq 0x33a0
movq %rbx, %r14
movq %r14, 0x50(%rsp)
leal 0x1(%r14), %eax
movslq %eax, %rdx
cmpq %rdx, 0x8(%rbp)
jbe 0x12124
leaq 0x18(%rsp), %r12
leaq 0xa0(%rsp), %rbx
leaq 0x80(%rbx), %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rcx
leal 0x2(%rcx), %eax
movl %eax, 0x64(%rsp)
movq $0x0, 0x70(%rsp)
movq %rcx, 0x80(%rsp)
movq %r12, %r14
movl $0x1, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movq %rdx, 0x58(%rsp)
callq 0x32b0
movq %rbx, %rdi
leaq 0x7f54(%rip), %rsi      # 0x19ab3
callq 0x30b0
movl %eax, %r12d
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x11b8c
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x3290
testl %r12d, %r12d
je 0x11d9c
movq %r15, %r13
movl $0x1, %ecx
movq %rbx, %rdi
movq %rbp, %rbx
movq %rbp, %rsi
movq 0x58(%rsp), %rdx
callq 0x32b0
movq 0xa0(%rsp), %r12
movq 0xa8(%rsp), %r15
movq 0x30(%rsp), %rbp
cmpq %r15, %rbp
movq %r15, %rdx
cmovbq %rbp, %rdx
testq %rdx, %rdx
je 0x11bea
movq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x32a0
testl %eax, %eax
je 0x11bea
xorl %r15d, %r15d
jmp 0x11bf1
cmpq %rbp, %r15
sete %r15b
leaq 0xb0(%rsp), %rax
cmpq %rax, %r12
movq %rbx, %rbp
je 0x11c14
movq 0xb0(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x3290
testb %r15b, %r15b
leaq 0xa0(%rsp), %rbx
je 0x11f10
movq 0x80(%rsp), %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rbp), %rax
addq $-0x2, %rax
cmpq %rax, %rcx
movq %r13, %r15
movq %r14, %r12
jne 0x11d92
movq %rbx, %rdi
callq 0x31d0
movq %r12, 0x8(%rsp)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x825d(%rip), %rdx      # 0x19ec9
movq %rdx, %rsi
callq 0x16550
leaq 0xb8(%rsp), %rdi
movq %r13, %rsi
callq 0x3370
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11c9b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x32f0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7e7f(%rip), %rsi      # 0x19b43
callq 0x32f0
movq 0x70(%rsp), %rax
leaq 0x1(%rax), %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7e57(%rip), %rsi      # 0x19b43
callq 0x32f0
movq 0x80(%rsp), %rsi
addq $0x2, %rsi
movq %r12, %rdi
callq 0x31a0
leaq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r12
callq 0x17526
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11d3f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11d79
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
movq 0xf235(%rip), %rsi      # 0x20fb8
callq 0x31f0
movq 0x48(%rsp), %rdi
callq 0x30e0
movq 0x58(%rsp), %rax
jmp 0x120f9
movq 0x80(%rsp), %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rbp), %rax
addq $-0x2, %rax
cmpq %rax, %rcx
movq %r14, %r12
jne 0x120fe
movq %rbx, %rdi
callq 0x31d0
movq %r12, 0x8(%rsp)
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x80e9(%rip), %rdx      # 0x19ec9
movq %rdx, %rsi
callq 0x16550
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x3370
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11e0f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x32f0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7d0b(%rip), %rsi      # 0x19b43
callq 0x32f0
movq 0x70(%rsp), %rax
leaq 0x1(%rax), %rsi
movq %rbx, %rdi
callq 0x31a0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7ce3(%rip), %rsi      # 0x19b43
callq 0x32f0
movq 0x80(%rsp), %rsi
addq $0x2, %rsi
movq %rbx, %rdi
callq 0x31a0
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x17526
leaq 0xa0(%rsp), %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11eb8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11ef2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
movq 0xf0bc(%rip), %rsi      # 0x20fb8
callq 0x31f0
movq 0x48(%rsp), %rdi
callq 0x30e0
jmp 0x120fe
movq %rbx, %rdi
callq 0x31d0
movq %r13, %r15
movq %r14, %r12
movq %r14, 0x8(%rsp)
leaq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0x7f97(%rip), %rdx      # 0x19ec9
movq %rdx, %rsi
callq 0x16550
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x3370
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11f61
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x50(%rsp), %rax
addq 0x58(%rsp), %rax
shrq %rax
cvtsi2sd %rax, %xmm0
movapd %xmm0, 0x90(%rsp)
subsd 0x7134(%rip), %xmm0     # 0x190b8
movapd %xmm0, 0x80(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x32f0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7b8d(%rip), %rsi      # 0x19b43
callq 0x32f0
movq 0x70(%rsp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x31a0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7b66(%rip), %rsi      # 0x19b43
callq 0x32f0
cvttsd2si 0x90(%rsp), %rax
movq %rax, %rcx
cvttsd2si 0x80(%rsp), %rsi
sarq $0x3f, %rcx
andq %rcx, %rsi
orq %rax, %rsi
incq %rsi
movq %r14, %rdi
movq %rsi, 0x70(%rsp)
callq 0x31a0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq %r15, %rdi
movq %r14, %rsi
callq 0x17526
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x1204a
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33c0
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x12084
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x28(%rsp), %rdi
leaq 0x7b0b(%rip), %rsi      # 0x19b9b
callq 0x30b0
leaq 0x7ae6(%rip), %rcx      # 0x19b82
testl %eax, %eax
je 0x120bc
leaq 0x28(%rsp), %rdi
leaq 0x7ad6(%rip), %rsi      # 0x19b82
callq 0x30b0
leaq 0x7ae3(%rip), %rcx      # 0x19b9b
testl %eax, %eax
jne 0x120d3
movq 0x30(%rsp), %rdx
movl $0x1, %r8d
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x33a0
movq %rbx, %rdi
movq 0xeedb(%rip), %rsi      # 0x20fb8
callq 0x31f0
movq 0x48(%rsp), %rdi
callq 0x30e0
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rax, 0x50(%rsp)
movslq 0x64(%rsp), %rdx
leal 0x1(%rdx), %eax
movl %eax, 0x64(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq %rdx, 0x8(%rbp)
ja 0x11b3d
movq 0x78(%rsp), %rax
movb $0x1, (%rax)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x11a76
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x11a76
jmp 0x1216a
jmp 0x121c8
jmp 0x121f4
jmp 0x1216a
movq %rax, %rbx
jmp 0x12236
jmp 0x121c8
jmp 0x1216a
jmp 0x121c8
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x121cb
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x121cb
jmp 0x121c8
jmp 0x121f9
jmp 0x121a6
jmp 0x121f9
jmp 0x121a6
jmp 0x121f9
jmp 0x121f4
jmp 0x121a6
jmp 0x121f9
jmp 0x121f4
jmp 0x121f9
jmp 0x121a6
jmp 0x121f9
jmp 0x121c8
jmp 0x121a6
jmp 0x121f9
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x121fc
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x121fc
jmp 0x121f9
jmp 0x121f4
jmp 0x121f9
movq %rax, %rbx
movq 0xede6(%rip), %rsi      # 0x20fb8
leaq 0xa0(%rsp), %rdi
callq 0x31f0
leaq 0x120(%rsp), %rdi
callq 0x30e0
jmp 0x12236
jmp 0x121f4
jmp 0x121f9
jmp 0x121f9
movq %rax, %rbx
jmp 0x1221a
movq %rax, %rbx
movq 0xedb5(%rip), %rsi      # 0x20fb8
leaq 0xa0(%rsp), %rdi
callq 0x31f0
movq 0x48(%rsp), %rdi
callq 0x30e0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x12236
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x3460