// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_purge_coverage(asg_t*, ma_hit_t_alloc*, ma_sub_t*, unsigned int*, unsigned int*, unsigned int*)
void get_purge_coverage(asg_t *read_g, ma_hit_t_alloc* sources, ma_sub_t* coverage_cut,
uint32_t* junk_cov, uint32_t* hap_cov, uint32_t* dip_cov)
{
    (*junk_cov) = (*hap_cov) = 0; (*dip_cov) = (uint32_t)-1;
    uint32_t i, j, num, m;
    long long R_bases = 0, C_bases = 0, T_R_bases = 0, T_C_bases = 0, total_coverage = 0, T_mean, Hap_mean;
    ma_hit_t *h = NULL;
    uint64_t* counts = (uint64_t*)calloc(read_g->n_seq, sizeof(uint64_t));
    kvec_t_u32_warp a;
    kv_init(a.a);

    for (i = 0; i < read_g->n_seq; ++i) 
    {
        R_bases = 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]);
            C_bases += Get_qe((*h)) - Get_qs((*h));
        }
        if(R_bases == 0) continue;

        counts[i] = C_bases/R_bases;
        T_R_bases += R_bases;
        T_C_bases += C_bases;
        total_coverage += counts[i];
    }

    T_mean = 0;
    if(T_R_bases > 0) T_mean = T_C_bases/T_R_bases;

    radix_sort_arch64(counts, counts + read_g->n_seq);


    uint64_t tmp;
    i = m = 0;
    while(i < read_g->n_seq)
    {
        num = 0;
        for (j = i; j < read_g->n_seq; j++)
        {
            if(counts[i] != counts[j]) break;
            num++;
        }

        tmp = counts[i]; tmp = tmp << 32; tmp = tmp | (uint64_t)num;
        counts[m] = tmp;
        m++;
        i = j;
    }


    R_bases = C_bases = 0;
    for (i = 0; i < read_g->n_seq; ++i) 
    {
        if(read_g->seq[i].c != HAP_LABLE) continue;
        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]);
            C_bases += Get_qe((*h)) - Get_qs((*h));
        }
    }
    Hap_mean = 0;
    if(R_bases > 0) Hap_mean = C_bases/R_bases;



    long long peak0, peak1;
    calculate_peak(counts, m, &a, T_mean, Hap_mean, &peak0, &peak1);

   
    // fprintf(stderr, "T_mean: %lld, Hap_mean: %lld, peak0: %lld, peak1: %lld\n", 
    // T_mean, Hap_mean, peak0, peak1);


    (*junk_cov) = MIN(JUNK_COV, (peak0/2));
    if(peak1 == -1)
    {
        if(peak0 <= T_mean)
        {
            (*hap_cov) = peak0*1.5;
            (*dip_cov) = peak0*4.5;
        }
        else
        {
            (*hap_cov) = peak0*1.2;
            (*dip_cov) = peak0*3.6;
        }
    }
    else
    {
        num = MAX(T_mean, (peak0+peak1)/2);
        (*hap_cov) = num;
        (*dip_cov) = num*3;
    }
    
    free(counts);
    kv_destroy(a.a);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x88(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0x98(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x1462a9
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x146249
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x80(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1461ec
cmpq $0x0, -0x48(%rbp)
jne 0x146252
jmp 0x14629b
movq -0x50(%rbp), %rax
cqto
idivq -0x48(%rbp)
movq %rax, %rdx
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x14618a
movq $0x0, -0x70(%rbp)
cmpq $0x0, -0x58(%rbp)
jle 0x1462c6
movq -0x60(%rbp), %rax
cqto
idivq -0x58(%rbp)
movq %rax, -0x70(%rbp)
movq -0x88(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cltq
shlq $0x3, %rax
addq %rax, %rsi
callq 0xc90e0
movl $0x0, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x1463cd
movl $0x0, -0x3c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x146369
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x88(%rbp), %rcx
movl -0x38(%rbp), %edx
cmpq (%rcx,%rdx,8), %rax
je 0x146355
jmp 0x146369
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x146321
movq -0x88(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movl -0x3c(%rbp), %ecx
orq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdx
movq -0x88(%rbp), %rax
movl -0x40(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x1462fc
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x1464bd
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x2, %eax
je 0x146416
jmp 0x1464af
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1464ad
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x80(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x146450
jmp 0x1464af
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1463e4
movq $0x0, -0x78(%rbp)
cmpq $0x0, -0x48(%rbp)
jle 0x1464da
movq -0x50(%rbp), %rax
cqto
idivq -0x48(%rbp)
movq %rax, -0x78(%rbp)
movq -0x88(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
leaq -0xa8(%rbp), %rdx
leaq -0xb8(%rbp), %r9
leaq -0xc0(%rbp), %rax
movq %rax, (%rsp)
callq 0x145940
movq -0xb8(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movl $0x5, %eax
cmpq %rcx, %rax
jg 0x146538
movl $0x5, %eax
movq %rax, -0xc8(%rbp)
jmp 0x146550
movq -0xb8(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $-0x1, -0xc0(%rbp)
jne 0x146606
movq -0xb8(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x1465c0
cvtsi2sdq -0xb8(%rbp), %xmm0
movsd 0x23bc75(%rip), %xmm1   # 0x382200
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cvtsi2sdq -0xb8(%rbp), %xmm0
movsd 0x2402db(%rip), %xmm1   # 0x386888
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x146604
cvtsi2sdq -0xb8(%rbp), %xmm0
movsd 0x23bcdf(%rip), %xmm1   # 0x3822b0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cvtsi2sdq -0xb8(%rbp), %xmm0
movsd 0x24028d(%rip), %xmm1   # 0x386880
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x146681
movq -0x70(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb8(%rbp), %rax
addq -0xc0(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jl 0x146645
movq -0x70(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x146664
movq -0xb8(%rbp), %rax
addq -0xc0(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
imull $0x3, -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x88(%rbp), %rdi
callq 0x2210
movq -0x98(%rbp), %rdi
callq 0x2210
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)