// File: chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_read_coverage_thres(ma_ug_t*, asg_t*, R_to_U*, unsigned long*, ma_hit_t_alloc*, ma_sub_t*, unsigned long, long long, long long*, long long*)
long long get_read_coverage_thres(ma_ug_t *ug, asg_t *read_g, R_to_U* ruIndex, uint64_t* position_index, 
ma_hit_t_alloc* sources, ma_sub_t* coverage_cut, uint64_t n_read, long long cov_buf_length,
long long* k_mer_only, long long* coverage_only)
{
    uint64_t i, j;
    long long* cov_buf = NULL;
    ma_hit_t *h;
    cov_buf = (long long*)calloc(cov_buf_length, sizeof(long long));
    long long R_bases = 0, C_bases = 0;
    for (i = 0; i < n_read; ++i) 
    {
        C_bases = 0;
        R_bases = coverage_cut[i].e - coverage_cut[i].s;
        for (j = 0; j < (uint64_t)(sources[i].length); j++)
        {
            h = &(sources[i].buffer[j]);
            if(h->el != 1) continue;
            C_bases += Get_qe((*h)) - Get_qs((*h));
        }
        C_bases = C_bases/R_bases;
        if(C_bases < 0 || C_bases >= cov_buf_length) continue;
        cov_buf[C_bases]++;
    }

    long long alter_peak = -1, hom_peak = -1, het_peak = -1;
    if(position_index)
    {
        alter_peak = get_alter_peak(ug, read_g, ruIndex, position_index, sources, coverage_cut, 
        cov_buf_length);
    }
    
    get_read_peak(read_g, cov_buf, cov_buf_length, alter_peak == -1? NULL: &alter_peak, &hom_peak, &het_peak,
    k_mer_only, coverage_only, asm_opt.hg_size);

    free(cov_buf);

    if(hom_peak != -1) return hom_peak*HOM_PEAK_RATE;
    if(het_peak != -1) return het_peak*HET_PEAK_RATE;
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq 0x18(%rbp), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x10(%rbp), %rax
jae 0x16f7fe
movq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rax
imulq $0xc, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x38(%rbp), %rcx
imulq $0xc, -0x40(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x16f7b8
movq -0x30(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0x16f790
jmp 0x16f7aa
movq -0x58(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x58(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x16f74e
movq -0x68(%rbp), %rax
cqto
idivq -0x60(%rbp)
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jl 0x16f7d7
movq -0x68(%rbp), %rax
cmpq 0x18(%rbp), %rax
jl 0x16f7d9
jmp 0x16f7ed
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16f705
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x78(%rbp)
movq $-0x1, -0x80(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x16f846
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movq 0x18(%rbp), %rax
movq %rax, (%rsp)
callq 0x16f140
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $-0x1, -0x70(%rbp)
jne 0x16f879
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x16f886
leaq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x16f886
movq -0x88(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rcx
movq 0x20(%rbp), %r11
movq 0x28(%rbp), %r10
leaq 0x254f47(%rip), %rax    # 0x3c47f8
movq 0x200(%rax), %rax
leaq -0x78(%rbp), %r8
leaq -0x80(%rbp), %r9
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x16e8f0
movq -0x50(%rbp), %rdi
callq 0x2210
cmpq $-0x1, -0x78(%rbp)
je 0x16f900
cvtsi2sdq -0x78(%rbp), %xmm0
movsd 0x216f4f(%rip), %xmm1   # 0x386840
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x8(%rbp)
jmp 0x16f92c
cmpq $-0x1, -0x80(%rbp)
je 0x16f924
cvtsi2sdq -0x80(%rbp), %xmm0
movsd 0x216f5b(%rip), %xmm1   # 0x386870
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x8(%rbp)
jmp 0x16f92c
movq $-0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)