// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_read_peak(asg_t*, long long*, long long, long long*, long long*, long long*, long long*, long long*, long long)
void get_read_peak(asg_t *read_g, long long* cov_buf, long long cov_buf_length, long long* topo_peak_cov, 
long long* hom_peak, long long* het_peak, long long* k_mer_only, long long* coverage_only, long long g_size)
{
    long long i, start, err_i, max_i, max2_i, max3_i, topo_peak_i, max, max2, max3, topo_peak, min;

    i = start = err_i = max_i = max2_i = max3_i = topo_peak_i = -1;
    max = max2 = max3 = topo_peak = min = -1;

    ///cov_buf[0] is usually very large
    for (i = 1; i < cov_buf_length; ++i)
    {
        if(cov_buf[i] > cov_buf[i-1]) break;
    }
    err_i = i - 1;
    // find the global highest peak
	max_i = err_i + 1, max = cov_buf[max_i];
    for (i = max_i; i < cov_buf_length; ++i)
    {
        if (cov_buf[i] > max)
        {
            max = cov_buf[i];
            max_i = i;
        }
    }

    ///print_peak(cov_buf, cov_buf_length, max_i);

    // look for smaller peak on the low end
	max2 = -1; max2_i = -1;
	for (i = max_i - 1; i > err_i; --i) 
    {
		///at first, it should be a peak
		if (cov_buf[i] >= cov_buf[i-1] && cov_buf[i] >= cov_buf[i+1]) 
        {
			if (cov_buf[i] > max2)
            {
                max2 = cov_buf[i];
                max2_i = i;
            } 
		}
	}

    ///fprintf(stderr, "***max2: %lld, max2_i: %lld\n", max2, max2_i);

    if (max2_i != -1 && max2_i > err_i && max2_i < max_i) 
    {
		for (i = max2_i + 1, min = max; i < max_i; ++i)
        {
            if (cov_buf[i] < min) min = cov_buf[i];
        }
			
		///if the second peak is not significant
		if(max2 < max * 0.05 || min > max2 * 0.95) max2 = max2_i = -1;
	}
    if(max2 < max*0.0075) max2 = max2_i = -1;
		

    // look for smaller peak on the high end
	max3 = -1; max3_i = -1;
    // we'd better use i < cov_buf_length - 1, since cov_buf[cov_buf_length-1] may have problem
	for (i = max_i + 1; i < cov_buf_length - 1; ++i) 
    {
		//at first, it should be a peak
		if (cov_buf[i] >= cov_buf[i-1] && cov_buf[i] >= cov_buf[i+1]) 
        {
			if (cov_buf[i] > max3)
            {
                max3 = cov_buf[i], max3_i = i;
            } 
		}
	}

    ///fprintf(stderr, "***max3: %lld, max3_i: %lld\n", max3, max3_i);
    
    //if found a peak
	if (max3 != -1 && max3_i > max_i) 
    {
		for (i = max_i + 1, min = max; i < max3_i; ++i)
        {
            if (cov_buf[i] < min) min = cov_buf[i];
        }
			
		if (max3 < max * 0.05 || min > max3 * 0.95 || max3_i > max_i * 3) max3 = max3_i = -1;
	}
    if (max3 < max*0.0075) max3 = max3_i = -1;

    

    (*hom_peak) = (*het_peak) = -1;
    if (topo_peak_cov && (*topo_peak_cov) < cov_buf_length)
    {
        topo_peak_i = (*topo_peak_cov);
        topo_peak = cov_buf[topo_peak_i];
        if (topo_peak <= max * 0.05) topo_peak_i = topo_peak = -1;
    }

    if(asm_opt.purge_level_primary == 0) 
    {
        (*hom_peak) = max_i;
        return;
    }
    
    
    long long k_mer_het, k_mer_hom, coverage_het, coverage_hom, alter_peak;
    k_mer_het = k_mer_hom = coverage_het = coverage_hom = alter_peak = -1;

    alter_peak = topo_peak_i;
    k_mer_het = asm_opt.het_cov;
    k_mer_hom = asm_opt.hom_cov;
    if(max3_i > 0)
    {
        coverage_het = max_i;
        coverage_hom = max3_i;
    }
    else
    {
        coverage_het = max2_i;
        coverage_hom = max_i;
    }

    if(g_size > 0) {
        long long n_bs, m_peak_hom = -1;
        int p_ht = -1;
        for (i = n_bs = 0; i < read_g->n_seq; i++) n_bs += read_g->seq[i].len;
        m_peak_hom = n_bs/g_size;
        if(m_peak_hom > 0) {
            p_ht = -1;
            coverage_hom = adj_m_peak_hom(m_peak_hom, max_i, max2_i, max3_i, &p_ht);
            coverage_het = p_ht;
        }   
    }

    if(k_mer_het != -1)
    {
        (*het_peak) = k_mer_het;
        (*hom_peak) = k_mer_hom;
        return;
    }
    else if(coverage_het != -1)
    {
        (*het_peak) = coverage_het;
        (*hom_peak) = coverage_hom;
        return;
    }
    else if(k_mer_hom > coverage_hom*1.5)
    {
        (*het_peak) = coverage_hom;
        (*hom_peak) = k_mer_hom;
        return;
    }
    else if(alter_peak != -1)
    {
        ///if peak is het, coverage peak is more reliable 
        if(coverage_hom >= alter_peak*0.8 && coverage_hom <= alter_peak*1.2) 
        {
            (*het_peak) = coverage_hom;
            return;
        }///if peak is homo, k-mer peak is more reliable 
        else if(k_mer_hom >= alter_peak*0.8*2 && k_mer_hom <= alter_peak*1.2*2)
        {
            (*hom_peak) = k_mer_hom;
            return;
        }
    }

    (*k_mer_only) = k_mer_hom;
    (*coverage_only) = coverage_hom;
    

    // fprintf(stderr, "max: %lld, max_i: %lld\n", max, max_i);
    // fprintf(stderr, "max2: %lld, max2_i: %lld\n", max2, max2_i);
    // fprintf(stderr, "max3: %lld, max3_i: %lld\n", max3, max3_i);
    // fprintf(stderr, "[M::%s] Heterozygous k-mer peak: %d\n", __func__, asm_opt.het_cov);
    // fprintf(stderr, "[M::%s] Homozygous k-mer peak: %d\n", __func__, asm_opt.hom_cov);
    // fprintf(stderr, "[M::%s] Heterozygous coverage peak: %lld\n", __func__, (*het_peak));
    // fprintf(stderr, "[M::%s] Homozygous coverage peak: %lld\n", __func__, (*hom_peak));
    // fprintf(stderr, "[M::%s] Alter coverage peak: %lld\n", __func__, topo_peak_i);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rsi, %rax
cmpq $0x2, %rdx
pushq $0x1
popq %rcx
movq %rcx, %r15
cmovgeq %rdx, %r15
cmpq %rdx, %rcx
jge 0xc7146
movq (%rax,%rcx,8), %r11
cmpq -0x8(%rax,%rcx,8), %r11
jg 0xc714c
incq %rcx
jmp 0xc7131
movq (%rax,%r15,8), %r11
jmp 0xc714f
movq %rcx, %r15
movq %r15, %rcx
movq %r15, %rsi
cmpq %rdx, %rcx
jge 0xc716e
movq (%rax,%rcx,8), %r8
cmpq %r11, %r8
cmovgq %rcx, %rsi
cmovgq %r8, %r11
incq %rcx
jmp 0xc7155
pushq $-0x1
popq %r10
movq %r10, %r12
movq %r10, %r14
movq %rsi, %rcx
cmpq %r15, %rcx
jle 0xc71bb
leaq -0x1(%rcx), %r13
movq -0x8(%rax,%rcx,8), %rbp
cmpq -0x10(%rax,%rcx,8), %rbp
movq %r13, %rcx
jl 0xc717b
cmpq 0x8(%rax,%r13,8), %rbp
setge %r8b
cmpq %r12, %rbp
setg %bl
movq %r13, %rcx
testb %bl, %r8b
jne 0xc71b0
movq %r14, %rcx
movq %r12, %rbp
movq %rbp, %r12
movq %rcx, %r14
movq %r13, %rcx
jmp 0xc717b
cmpq $-0x1, %r14
je 0xc71e7
cmpq %r15, %r14
jl 0xc721e
cmpq %rsi, %r14
jge 0xc721e
leaq 0x1(%r14), %r13
movq %r11, %r15
cmpq %rsi, %r13
jge 0xc71ec
movq (%rax,%r13,8), %rcx
cmpq %r15, %rcx
cmovlq %rcx, %r15
incq %r13
jmp 0xc71d2
movq %r10, %r14
jmp 0xc721e
cvtsi2sd %r12, %xmm0
cvtsi2sd %r11, %xmm1
mulsd 0x11c31a(%rip), %xmm1   # 0x1e3518
ucomisd %xmm0, %xmm1
ja 0xc7217
cvtsi2sd %r15, %xmm1
mulsd 0x11c2d7(%rip), %xmm0   # 0x1e34e8
ucomisd %xmm0, %xmm1
jbe 0xc721e
pushq $-0x1
popq %r14
movq %r14, %r12
cvtsi2sd %r12, %xmm2
cvtsi2sd %r11, %xmm0
movsd 0x124368(%rip), %xmm1   # 0x1eb598
mulsd %xmm0, %xmm1
leaq 0x1(%rsi), %r12
leaq -0x1(%rdx), %r13
movq %r12, %rbp
movq %r10, %rcx
cmpq %r13, %rbp
jge 0xc727a
movq (%rax,%rbp,8), %r15
cmpq -0x8(%rax,%rbp,8), %r15
jl 0xc7275
cmpq 0x8(%rax,%rbp,8), %r15
setge %bl
cmpq %rcx, %r15
setg %r9b
movq %rbp, %r8
testb %r9b, %bl
jne 0xc726f
movq %r10, %r8
movq %rcx, %r15
movq %r8, %r10
movq %r15, %rcx
incq %rbp
jmp 0xc7242
ucomisd %xmm2, %xmm1
pushq $-0x1
popq %r13
cmovaq %r13, %r14
cmpq $-0x1, %rcx
je 0xc72a6
cmpq %rsi, %r10
jle 0xc72e7
cmpq %r10, %r12
jge 0xc72ab
movq (%rax,%r12,8), %r8
cmpq %r11, %r8
cmovlq %r8, %r11
incq %r12
jmp 0xc7291
movq %r13, %rcx
jmp 0xc72e7
cvtsi2sd %rcx, %xmm2
movsd 0x11c260(%rip), %xmm3   # 0x1e3518
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
jbe 0xc72cb
pushq $-0x1
popq %r10
movq %r10, %rcx
jmp 0xc72e7
cvtsi2sd %r11, %xmm3
mulsd 0x11c210(%rip), %xmm2   # 0x1e34e8
ucomisd %xmm2, %xmm3
ja 0xc72c2
leaq (%rsi,%rsi,2), %r8
cmpq %r8, %r10
jg 0xc72c2
cvtsi2sd %rcx, %xmm2
ucomisd %xmm2, %xmm1
cmovaq %r13, %r10
movq 0x8(%rsp), %rcx
orq $-0x1, (%rcx)
movq 0x10(%rsp), %rcx
orq $-0x1, (%rcx)
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xc732c
movq (%r8), %r12
cmpq %rdx, %r12
jge 0xc732c
cvtsi2sdq (%rax,%r12,8), %xmm1
mulsd 0x11c1f2(%rip), %xmm0   # 0x1e3518
ucomisd %xmm1, %xmm0
jb 0xc732f
movq %r13, %r12
leaq 0x18c422(%rip), %rax    # 0x253758
cmpl $0x0, 0x130(%rax)
je 0xc73de
movslq 0xd4(%rax), %r13
movslq 0xd0(%rax), %rbp
testq %r10, %r10
movq %r14, %r9
cmovgq %rsi, %r9
movq %rsi, %rdx
cmovgq %r10, %rdx
cmpq $0x0, 0x70(%rsp)
jle 0xc73c8
movl 0x14(%rdi), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
xorl %eax, %eax
cmpq %r15, %r11
je 0xc7395
movq 0x20(%rdi), %rcx
movl (%rcx,%r15,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %rax
incq %r15
jmp 0xc737a
movq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jb 0xc73c3
xorl %edx, %edx
divq %rcx
leaq 0x24(%rsp), %r15
orl $-0x1, (%r15)
movl %eax, %edi
movl %r14d, %edx
movl %r10d, %ecx
movq %r15, %r8
callq 0xdb00e
movslq %eax, %rdx
movslq (%r15), %r9
movq 0x10(%rsp), %rcx
cmpl $-0x1, %r13d
je 0xc73e6
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movq %rbp, (%rcx)
jmp 0xc7478
movq %rsi, (%rcx)
jmp 0xc7478
cmpq $-0x1, %r9
je 0xc73f9
movq 0x8(%rsp), %rax
movq %r9, (%rax)
movq %rdx, (%rcx)
jmp 0xc7478
cvtsi2sd %ebp, %xmm0
cvtsi2sd %rdx, %xmm1
movsd 0x11c0b6(%rip), %xmm2   # 0x1e34c0
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0xc741e
movq 0x8(%rsp), %rax
movq %rdx, (%rax)
jmp 0xc73d6
cmpq $-0x1, %r12
je 0xc7468
cvtsi2sd %r12, %xmm2
movsd 0x11c0cf(%rip), %xmm3   # 0x1e3500
mulsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
mulsd 0x11c14f(%rip), %xmm2   # 0x1e3590
jb 0xc7450
ucomisd %xmm1, %xmm2
jb 0xc7450
movq 0x8(%rsp), %rax
jmp 0xc7475
addsd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0xc7468
addsd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jae 0xc73d6
movq 0x60(%rsp), %rax
movq %rbp, (%rax)
movq 0x68(%rsp), %rax
movq %rdx, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq