// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rsi, %r12
movsd %xmm0, 0x70(%rsp)
movq %rdi, %rbx
movq 0x32cbc(%rip), %r15     # 0x4bfc8
leaq 0x235a0(%rip), %rsi     # 0x3c8b3
movl $0x12, %edx
movq %r15, %rdi
callq 0x43a0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x43a0
movq %rax, %r15
leaq 0x2358d(%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), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
je 0x193e6
movq 0x32c1e(%rip), %r14     # 0x4bfc8
leaq 0x23528(%rip), %rsi     # 0x3c8d9
movl $0x2e, %edx
movq %r14, %rdi
callq 0x43a0
testq %rbx, %rbx
je 0x19681
movq %rbx, %rdi
callq 0x4180
movq 0x32bf2(%rip), %rdi     # 0x4bfc8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x43a0
jmp 0x19699
movq %r14, 0x68(%rsp)
movq %r12, 0x98(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
movq %r14, 0x30(%rsp)
movq %r14, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movb $0x9, 0xf(%rsp)
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
jne 0x195e8
xorl %r14d, %r14d
leaq 0x88(%rsp), %rbp
leaq 0x256e1(%rip), %r12     # 0x3eb22
leaq 0x78(%rsp), %r13
movl $0x0, 0x10(%rsp)
movq $0x0, 0x60(%rsp)
movq %rbp, 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)
je 0x195b4
movq 0x78(%rsp), %rax
cmpb $0x23, (%rax)
je 0x195b4
cmpb $0x9, 0xf(%rsp)
jne 0x194d6
movl $0x1, %ecx
movq %r13, %rdi
leaq 0x2565d(%rip), %rsi     # 0x3eb21
xorl %edx, %edx
callq 0x4590
cmpq $-0x1, %rax
jne 0x194d6
movb $0x20, 0xf(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
leaq 0xf(%rsp), %rdx
callq 0x36b7c
cmpq $0x2, 0x80(%rsp)
jb 0x195a7
movq 0xa0(%rsp), %rbx
movq (%rbx), %rdi
callq 0x43f0
callq 0x4170
cvttsd2si %xmm0, %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x19546
movl 0x1c(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x19555
leaq 0x20(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
callq 0x22154
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x19574
movq 0x58(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x19583
leaq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x22290
movl 0x10(%rsp), %ecx
incl %ecx
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rdx
cmpq %rdx, %rax
cmovaq %rax, %rdx
movq %rdx, 0x60(%rsp)
movl %ecx, 0x10(%rsp)
cmoval %ecx, %r14d
leaq 0xa0(%rsp), %rdi
callq 0x6aa2
movq 0x78(%rsp), %rdi
cmpq %rbp, %rdi
je 0x195ce
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
je 0x19457
testl %r14d, %r14d
jle 0x199ec
movq %r14, 0x10(%rsp)
movl %r14d, %r13d
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r13, %rcx
movq 0x98(%rsp), %rbp
jbe 0x197dc
movq 0x10(%rsp), %rcx
leal -0x1(%rcx), %ebx
movq 0x40(%rsp), %rcx
movsd (%rcx,%r13,8), %xmm0
movsd 0x21aaa(%rip), %xmm2    # 0x3b0e0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x21aaf(%rip), %xmm3    # 0x3b0f0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd (%rcx,%rbx,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 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x196cb
movq %r13, %rcx
jmp 0x197c4
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 0x32911(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
xorl %ebx, %ebx
jmp 0x199bb
leaq 0x22430(%rip), %r12     # 0x3bb02
leaq 0x22453(%rip), %r15     # 0x3bb2c
movq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x4100
testl %eax, %eax
jne 0x19715
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r13,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%rbx,4), %xmm1
mulsd 0x219bd(%rip), %xmm1    # 0x3b0c8
ucomisd %xmm1, %xmm0
ja 0x199dc
movq %r14, %rdi
movq %r15, %rsi
callq 0x4100
testl %eax, %eax
jne 0x1974c
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r13,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%rax,%rbx,4), %xmm1
mulsd 0x21986(%rip), %xmm1    # 0x3b0c8
ucomisd %xmm1, %xmm0
ja 0x199dc
leaq 0x1(%r13), %rcx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rcx, %rdx
jbe 0x197dc
movq 0x40(%rsp), %rdx
movsd 0x8(%rdx,%r13,8), %xmm0
movsd 0x21966(%rip), %xmm2    # 0x3b0e0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x2196b(%rip), %xmm3    # 0x3b0f0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd (%rdx,%rbx,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 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %rcx, %r13
jbe 0x196de
cmpl %ecx, 0x10(%rsp)
movl %ecx, %edx
leaq -0x4(%rax,%rdx,4), %rdx
leaq (%rax,%rcx,4), %rax
cmovneq %rdx, %rax
movl (%rax), %eax
movl %eax, (%rbp)
leaq 0x2231f(%rip), %rsi     # 0x3bb02
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x4100
testl %eax, %eax
je 0x19858
leaq 0x22331(%rip), %rsi     # 0x3bb2c
movq %rbx, %rdi
callq 0x4100
testl %eax, %eax
je 0x198d1
movq 0x327b6(%rip), %rbx     # 0x4bfc8
leaq 0x2321d(%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 0x32784(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
xorl %ebx, %ebx
jmp 0x1998d
decl (%rbp)
movq 0x32766(%rip), %rdi     # 0x4bfc8
leaq 0x230f0(%rip), %rsi     # 0x3c959
movl $0x3e, %edx
callq 0x43a0
movl (%rbp), %esi
movq 0x3274b(%rip), %rdi     # 0x4bfc8
callq 0x4580
movq %rax, %rbx
leaq 0x22b4e(%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 0x230cc(%rip), %rsi     # 0x3c998
jmp 0x19951
imull $0x3e8, (%rbp), %eax    # imm = 0x3E8
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, (%rbp)
movq 0x326e1(%rip), %rdi     # 0x4bfc8
leaq 0x230dc(%rip), %rsi     # 0x3c9ca
movl $0x35, %edx
callq 0x43a0
movl (%rbp), %esi
movq 0x326c6(%rip), %rdi     # 0x4bfc8
callq 0x4580
movq %rax, %rbx
leaq 0x22ac6(%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 0x230af(%rip), %rsi     # 0x3ca00
movq 0x32670(%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 0x3264a(%rip), %rdi     # 0x4bfc8
callq 0x4030
movb $0x1, %bl
movq %rax, %rdi
callq 0x4220
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x199a4
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x199bb
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 %r13d, %ecx
leaq (%rax,%rcx,4), %rax
addq $-0x4, %rax
jmp 0x197d7
leaq 0x22f15(%rip), %rdi     # 0x3c908
leaq 0x22527(%rip), %rsi     # 0x3bf21
leaq 0x22f14(%rip), %rcx     # 0x3c915
movl $0x61c, %edx            # imm = 0x61C
callq 0x41f0
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x6aa2
jmp 0x19a2e
jmp 0x19a2b
jmp 0x19a26
movq %rax, %rbx
jmp 0x19a76
movq %rax, %rbx
jmp 0x19a48
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %rbp, %rdi
je 0x19a48
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x19a5f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x19a76
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x4340
leaq 0xb8(%rsp), %rdi
callq 0x40b0
movq %rbx, %rdi
callq 0x45a0