// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, 0x20(%rsp)
movq %rsi, %rax
movl $0x1, %r9d
cmpq $0x2, %rdx
jl 0x105002
movq (%rax,%r9,8), %rcx
cmpq -0x8(%rax,%r9,8), %rcx
jg 0x105002
incq %r9
cmpq %r9, %rdx
jne 0x104fec
movq %rdx, %r9
movq (%rax,%r9,8), %r10
cmpq %rdx, %r9
movq %r8, 0x18(%rsp)
movq %r9, %rsi
jge 0x10502d
movq %r9, %r8
movq (%rax,%r8,8), %rcx
cmpq %r10, %rcx
cmovgq %r8, %rsi
cmovgq %rcx, %r10
incq %r8
cmpq %r8, %rdx
jne 0x105016
movq $-0x1, %r8
movq $-0x1, %r11
cmpq %r9, %rsi
jle 0x10507d
movq %rsi, %r13
leaq -0x1(%r13), %r15
movq -0x8(%rax,%r13,8), %r12
cmpq -0x10(%rax,%r13,8), %r12
jl 0x105075
cmpq (%rax,%r13,8), %r12
setge %cl
cmpq %r11, %r12
setg %bpl
movq %r15, %r13
testb %bpl, %cl
jne 0x10506f
movq %r8, %r13
movq %r11, %r12
movq %r13, %r8
movq %r12, %r11
movq %r15, %r13
cmpq %r9, %r15
jg 0x105043
cmpq $-0x1, %r8
je 0x1050ed
cmpq %r9, %r8
jl 0x1050f4
cmpq %rsi, %r8
jge 0x1050f4
leaq 0x1(%r8), %r15
movq %r10, %r9
cmpq %rsi, %r15
jge 0x1050af
movq %r10, %r9
movq (%rax,%r15,8), %rcx
cmpq %r9, %rcx
cmovlq %rcx, %r9
incq %r15
cmpq %r15, %rsi
jne 0x10509c
cvtsi2sd %r11, %xmm0
cvtsi2sd %r10, %xmm1
mulsd 0x120407(%rip), %xmm1   # 0x2254c8
ucomisd %xmm0, %xmm1
ja 0x1050dd
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
mulsd 0x1203c1(%rip), %xmm0   # 0x225498
ucomisd %xmm0, %xmm1
jbe 0x1050f4
movq $-0x1, %r8
movq $-0x1, %r11
jmp 0x1050f4
movq $-0x1, %r8
cvtsi2sd %r11, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
movsd 0x12839f(%rip), %xmm1   # 0x22d4a8
mulsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
movq $-0x1, %rcx
cmovaq %rcx, %r8
leaq 0x1(%rsi), %r15
leaq -0x1(%rdx), %r9
movq $-0x1, %r11
cmpq %r9, %r15
jge 0x10517a
movl $0x2, %r12d
subq %rdx, %r12
movq %r15, %r13
movq (%rax,%r13,8), %rbp
cmpq -0x8(%rax,%r13,8), %rbp
jl 0x10516a
cmpq 0x8(%rax,%r13,8), %rbp
setge %bl
cmpq %r11, %rbp
setg %r14b
movq %r13, %r9
testb %r14b, %bl
jne 0x105164
movq %rcx, %r9
movq %r11, %rbp
movq %r9, %rcx
movq %rbp, %r11
leaq (%r12,%r13), %r9
incq %r9
incq %r13
cmpq $0x1, %r9
jne 0x10513c
cmpq $-0x1, %r11
je 0x1051c2
cmpq %rsi, %rcx
jle 0x1051c9
cmpq %rcx, %r15
jge 0x10519d
movq (%rax,%r15,8), %r9
cmpq %r10, %r9
cmovlq %r9, %r10
incq %r15
cmpq %r15, %rcx
jne 0x10518a
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
movsd 0x12031b(%rip), %xmm3   # 0x2254c8
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x10529a
movq $-0x1, %rcx
movq $-0x1, %r11
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
ucomisd %xmm2, %xmm1
movq $-0x1, %r9
cmovaq %r9, %rcx
movq 0x8(%rsp), %r10
movq %r9, (%r10)
movq 0x18(%rsp), %r11
movq %r9, (%r11)
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x105219
movq (%r9), %r13
cmpq %rdx, %r13
jge 0x105219
xorps %xmm1, %xmm1
cvtsi2sdq (%rax,%r13,8), %xmm1
mulsd 0x1202b5(%rip), %xmm0   # 0x2254c8
ucomisd %xmm1, %xmm0
jb 0x105220
movq $-0x1, %r13
leaq 0x185531(%rip), %rax    # 0x28a758
cmpl $0x0, 0x130(%rax)
je 0x105292
movq 0x70(%rsp), %r10
movslq 0xd4(%rax), %rbp
movslq 0xd0(%rax), %r12
testq %rcx, %rcx
movq %r8, %rdx
cmovgq %rsi, %rdx
movq %rsi, %r9
cmovgq %rcx, %r9
testq %r10, %r10
jle 0x1052f6
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movl 0x14(%rdi), %r11d
andq $0x7fffffff, %r11       # imm = 0x7FFFFFFF
je 0x1052c6
movq 0x20(%rdi), %rdi
xorl %eax, %eax
xorl %r15d, %r15d
movl (%rdi,%r15,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %rax
incq %r15
cmpq %r15, %r11
jne 0x10527b
jmp 0x1052c8
movq %rsi, (%r11)
jmp 0x1053aa
xorps %xmm3, %xmm3
cvtsi2sd %r10, %xmm3
mulsd 0x1201ee(%rip), %xmm2   # 0x225498
ucomisd %xmm2, %xmm3
ja 0x1051bb
leaq (%rsi,%rsi,2), %r9
cmpq %r9, %rcx
jle 0x1051c9
jmp 0x1051bb
xorl %eax, %eax
cmpq %r10, %rax
jb 0x1052f1
xorl %edx, %edx
divq %r10
leaq 0x14(%rsp), %r15
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
movl %eax, %edi
movl %r8d, %edx
movq %r15, %r8
callq 0x118ee2
movslq %eax, %r9
movslq (%r15), %rdx
movq 0x18(%rsp), %r11
cmpl $-0x1, %ebp
je 0x10530b
movq 0x8(%rsp), %rax
movq %rbp, (%rax)
movq %r12, (%r11)
jmp 0x1053aa
cmpq $-0x1, %rdx
je 0x105321
movq 0x8(%rsp), %rax
movq %rdx, (%rax)
movq %r9, (%r11)
jmp 0x1053aa
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
movsd 0x120137(%rip), %xmm2   # 0x225470
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x10534d
movq 0x8(%rsp), %rax
movq %r9, (%rax)
jmp 0x105303
cmpq $-0x1, %r13
je 0x10539a
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
movsd 0x12014d(%rip), %xmm3   # 0x2254b0
mulsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
mulsd 0x1201cd(%rip), %xmm2   # 0x225540
jb 0x105382
ucomisd %xmm1, %xmm2
jb 0x105382
movq 0x8(%rsp), %rax
jmp 0x1053a7
addsd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0x10539a
addsd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jae 0x105303
movq 0x68(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %r12, (%rcx)
movq %r9, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq