// File: /workspace/github_cpp_makefile_star2_O3/schneebergerlab[P]DrLink/detect_recombsite.cpp
// Function: get_moleStat_distribution(char const*, double, int*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool get_moleStat_distribution(const char* file, 
                              double      maxPercent, 
                              int*        maxStat,
                              string      lenORreadnum)
{
    // this function finds out a cutoff for read_number/length of molecules for determining some of them as bad ones.
    // lenORreadnum is either "readNum" or "moleLen"
    cout << "   Info: checking " << lenORreadnum << " distribution ... " << endl;
    fstream fp;
    fp.open(file, ios::in);
    if(!fp.good())
    {
        cout << "   Error: cannot open read number distributio " << file << endl;
        return false;
    }
    vector<int>           xvalue;
    vector<unsigned long> yvalue;
    //
    int maxNumLen = 0; // if about length, in kb
    unsigned long maxcnt = 0;
    int xindex    = 0;
    int maxxindex = 0;
    char sepc = '\t';
    while(fp.good())
    {
        string line("");
        getline(fp, line);
        if(line.size()==0 || line[0]=='#') continue;
        
        if(sepc == '\t' && line.find("\t") == std::string::npos) 
        {
            sepc = ' ';
        }
        vector<string> lineinfo = split_string(line, sepc);
        if(line.size() < 2) continue;
        
        int           xxx = round(atof(lineinfo[0].c_str()));
        unsigned long yyy = strtoul(lineinfo[1].c_str(), NULL, 0);
        xindex ++;
        
        xvalue.push_back(xxx);
        yvalue.push_back(yyy);
        
        if(yyy > maxcnt)
        {
            maxcnt    = yyy;
            maxNumLen = xxx;
            maxxindex = xindex; // 1-based system; indeed ymax is at vector[maxxindex-1]
        }
    }
    // find a proper maximum statistic
    assert(maxxindex>=1);
    for(int i = maxxindex; i < xvalue.size(); i ++)
    {
        // caution: too stringent on molecule size
        if( (yvalue[i]*1.0)/(yvalue[maxxindex-1]*1.0)<maxPercent )
        {
            if(i == maxxindex)
            {
                // the one right next to max
                *maxStat = xvalue[i]; 
            }
            else
            {
                *maxStat = xvalue[i-1];
            }
            break;
        }
        else 
        if( lenORreadnum.compare("readNum")==0 && 
            (double)xvalue[i] > 1.5*(double)xvalue[maxxindex-1])
        {
            *maxStat = xvalue[i-1];
            break;
        }
        else 
        if( lenORreadnum.compare("moleLen")==0 && 
            (double)xvalue[i] > 1.5*(double)xvalue[maxxindex-1])
        {
            *maxStat = xvalue[i-1];
            break;
        }        
    }
    if(lenORreadnum.compare("readNum")==0)
    {
        *maxStat -= 1;
        cout << "   Info: maximum number of reads allowed for a good molecule: " 
             << *(maxStat)   << ";"    
             << endl;
        cout << "   Info: checking read number distribution done. "  
             << endl;
    }
    else
    if(lenORreadnum.compare("moleLen")==0)
    {
        *maxStat = (*maxStat) * 1000 + 1000; // into bp and complement 1kb: less stringent
        cout << "   Info: maximum length allowed for a good molecule: "          
             << *maxStat     
             << " bp;" 
             << endl;
        cout << "   Info: checking molecule length distribution done. "
             << endl;
    }   
    else
    {
        cout << "   Error: you should never reach here when checking readnum or length of molecules." 
             << endl;
        return false;
    } 
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdx, %rbp
movq %rsi, %r14
movsd %xmm0, 0x68(%rsp)
movq %rdi, %rbx
movq 0x32d22(%rip), %r15     # 0x4bfc8
leaq 0x23606(%rip), %rsi     # 0x3c8b3
movl $0x12, %edx
movq %r15, %rdi
callq 0x43a0
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movq %r15, %rdi
callq 0x43a0
movq %rax, %r15
leaq 0x235f2(%rip), %rsi     # 0x3c8c6
movl $0x12, %edx
movq %rax, %rdi
callq 0x43a0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq %r15, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4220
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
callq 0x43c0
movq %r15, %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x42d0
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rcx
cmpl $0x0, 0xd8(%rsp,%rcx)
je 0x19381
movq 0x32c83(%rip), %r14     # 0x4bfc8
leaq 0x2358d(%rip), %rsi     # 0x3c8d9
movl $0x2e, %edx
movq %r14, %rdi
callq 0x43a0
testq %rbx, %rbx
je 0x19628
movq %rbx, %rdi
callq 0x4180
movq 0x32c57(%rip), %rdi     # 0x4bfc8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x43a0
jmp 0x19640
movq %r14, 0x98(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movb $0x9, 0xf(%rsp)
movq -0x18(%rax), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
jne 0x1999e
movq $0x0, 0x10(%rsp)
leaq 0x88(%rsp), %r14
leaq 0x25736(%rip), %r12     # 0x3eb0a
leaq 0x78(%rsp), %r13
movl $0x0, 0x18(%rsp)
movq $0x0, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %r12, %rdx
callq 0x5384
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x45c0
cmpq $0x0, 0x80(%rsp)
movq 0x78(%rsp), %rdi
je 0x1955f
cmpb $0x23, (%rdi)
je 0x1955f
cmpb $0x9, 0xf(%rsp)
jne 0x19469
movl $0x1, %ecx
movq %r13, %rdi
leaq 0x256b2(%rip), %rsi     # 0x3eb09
xorl %edx, %edx
callq 0x4590
cmpq $-0x1, %rax
jne 0x19469
movb $0x20, 0xf(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
leaq 0xf(%rsp), %rdx
callq 0x367c0
cmpq $0x2, 0x80(%rsp)
jb 0x1954d
movq %rbp, %rbx
movq 0xa0(%rsp), %rbp
movq (%rbp), %rdi
callq 0x43f0
callq 0x4170
cvttsd2si %xmm0, %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, 0x60(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x194e0
movl 0x1c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
movq %rbx, %rbp
jmp 0x194f2
leaq 0x20(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
callq 0x22014
movq %rbx, %rbp
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x19511
movq 0x60(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x19525
leaq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x22150
movq 0x60(%rsp), %rax
movl 0x18(%rsp), %ecx
incl %ecx
movq 0x70(%rsp), %rdx
cmpq %rdx, %rax
cmovaq %rax, %rdx
movq %rdx, 0x70(%rsp)
movq 0x10(%rsp), %rax
movl %ecx, 0x18(%rsp)
cmoval %ecx, %eax
movq %rax, 0x10(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x6aac
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x19574
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
je 0x193ea
movq 0x10(%rsp), %rax
testl %eax, %eax
jle 0x1999e
movl %eax, %r12d
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
subq %rcx, %rax
sarq $0x2, %rax
cmpq %r12, %rax
movq 0x98(%rsp), %r13
jbe 0x1977e
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r14d
movq 0x40(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
movsd 0x21b04(%rip), %xmm2    # 0x3b0e0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x21b09(%rip), %xmm3    # 0x3b0f0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd (%rax,%r14,8), %xmm0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0x68(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19672
movq %r12, %rax
jmp 0x19765
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4560
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq 0x3296a(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
xorl %ebx, %ebx
jmp 0x1995d
leaq 0x22489(%rip), %rbx     # 0x3bb02
leaq 0x224ac(%rip), %r15     # 0x3bb2c
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x4100
testl %eax, %eax
jne 0x196b8
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r12,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%r14,4), %xmm1
mulsd 0x21a1a(%rip), %xmm1    # 0x3b0c8
ucomisd %xmm1, %xmm0
ja 0x1997e
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4100
movq 0x20(%rsp), %rcx
testl %eax, %eax
jne 0x196f0
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%r12,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%rcx,%r14,4), %xmm1
mulsd 0x219e2(%rip), %xmm1    # 0x3b0c8
ucomisd %xmm1, %xmm0
ja 0x1998e
leaq 0x1(%r12), %rax
movq 0x28(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rax, %rdx
jbe 0x1977e
movq 0x40(%rsp), %rdx
movsd 0x8(%rdx,%r12,8), %xmm0
movsd 0x219c6(%rip), %xmm2    # 0x3b0e0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x219cb(%rip), %xmm3    # 0x3b0f0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd (%rdx,%r14,8), %xmm0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0x68(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %rax, %r12
jbe 0x19680
cmpl %eax, 0x10(%rsp)
movl %eax, %edx
leaq -0x4(%rcx,%rdx,4), %rdx
leaq (%rcx,%rax,4), %rax
cmovneq %rdx, %rax
movl (%rax), %eax
movl %eax, (%r13)
leaq 0x2237d(%rip), %rsi     # 0x3bb02
movq %rbp, %rdi
callq 0x4100
testl %eax, %eax
je 0x197f5
leaq 0x22394(%rip), %rsi     # 0x3bb2c
movq %rbp, %rdi
callq 0x4100
testl %eax, %eax
je 0x19870
movq 0x32819(%rip), %rbx     # 0x4bfc8
leaq 0x23280(%rip), %rsi     # 0x3ca36
movl $0x53, %edx
movq %rbx, %rdi
callq 0x43a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq 0x327e7(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
xorl %ebx, %ebx
jmp 0x1992f
decl (%r13)
movq 0x327c8(%rip), %rdi     # 0x4bfc8
leaq 0x23152(%rip), %rsi     # 0x3c959
movl $0x3e, %edx
callq 0x43a0
movl (%r13), %esi
movq 0x327ac(%rip), %rdi     # 0x4bfc8
callq 0x4580
movq %rax, %rbx
leaq 0x22baf(%rip), %rsi     # 0x3c3da
movl $0x1, %edx
movq %rax, %rdi
callq 0x43a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4220
movl $0x31, %edx
leaq 0x2312d(%rip), %rsi     # 0x3c998
jmp 0x198f3
imull $0x3e8, (%r13), %eax    # imm = 0x3E8
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, (%r13)
movq 0x32740(%rip), %rdi     # 0x4bfc8
leaq 0x2313b(%rip), %rsi     # 0x3c9ca
movl $0x35, %edx
callq 0x43a0
movl (%r13), %esi
movq 0x32724(%rip), %rdi     # 0x4bfc8
callq 0x4580
movq %rax, %rbx
leaq 0x22b24(%rip), %rsi     # 0x3c3d7
movl $0x4, %edx
movq %rax, %rdi
callq 0x43a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4220
movl $0x35, %edx
leaq 0x2310d(%rip), %rsi     # 0x3ca00
movq 0x326ce(%rip), %rbx     # 0x4bfc8
movq %rbx, %rdi
callq 0x43a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq 0x326a8(%rip), %rdi     # 0x4bfc8
callq 0x4030
movb $0x1, %bl
movq %rax, %rdi
callq 0x4220
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x19946
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1995d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
leaq 0xb8(%rsp), %rdi
callq 0x40b0
movl %ebx, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %ecx
leaq (%rax,%rcx,4), %rax
addq $-0x4, %rax
jmp 0x19778
movl %r12d, %eax
leaq (%rcx,%rax,4), %rax
addq $-0x4, %rax
jmp 0x19778
leaq 0x22f63(%rip), %rdi     # 0x3c908
leaq 0x22575(%rip), %rsi     # 0x3bf21
leaq 0x22f62(%rip), %rcx     # 0x3c915
movl $0x61c, %edx            # imm = 0x61C
callq 0x41f0
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x6aac
jmp 0x199e0
jmp 0x199d8
jmp 0x199dd
movq %rax, %rbx
jmp 0x19a28
movq %rax, %rbx
jmp 0x199fa
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x199fa
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x19a11
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x19a28
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
leaq 0xb8(%rsp), %rdi
callq 0x40b0
movq %rbx, %rdi
callq 0x45a0