// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x18(%rsp)
andl $0x0, (%r8)
movq %rcx, 0x30(%rsp)
andl $0x0, (%rcx)
movq %rdx, %r13
movq %r9, 0x38(%rsp)
orl $-0x1, (%r9)
movq %rsi, %rbp
movq %rdi, 0x20(%rsp)
movl 0x14(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x8
popq %rsi
movq %rdi, 0x28(%rsp)
callq 0x2120
movq %rax, %r15
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
xorl %r8d, %r8d
xorl %edi, %edi
xorl %ecx, %ecx
cmpq 0x28(%rsp), %r8
je 0xae79e
imulq $0xc, %r8, %rdx
movl 0x4(%r13,%rdx), %eax
movl (%r13,%rdx), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
movl %eax, %r10d
subl %edx, %r10d
imulq $0x18, %r8, %rsi
leaq (%rsi,%rbp), %r11
movl 0xc(%rbp,%rsi), %esi
imulq $0x28, %rsi, %r14
xorl %ebx, %ebx
xorl %esi, %esi
cmpq %rbx, %r14
je 0xae780
movq (%r11), %r9
movl 0x8(%r9,%rbx), %r12d
subl (%r9,%rbx), %r12d
addq %r12, %rsi
addq $0x28, %rbx
jmp 0xae766
cmpl %edx, %eax
je 0xae799
movl %r10d, %r9d
movq %rsi, %rax
xorl %edx, %edx
divq %r9
movq %rax, (%r15,%r8,8)
addq %r9, %rdi
addq %rsi, %rcx
incq %r8
jmp 0xae730
xorl %r14d, %r14d
testq %rdi, %rdi
jle 0xae7b5
movq %rcx, %rax
cqto
idivq %rdi
movq %rax, 0x10(%rsp)
jmp 0xae7be
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rax
leaq (%r15,%rax,8), %rsi
movq %r15, %rdi
callq 0x66c25
movq 0x20(%rsp), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %rax
negq %rax
xorl %edx, %edx
movl %ecx, %edi
movl %r14d, %esi
subl %edx, %edi
jbe 0xae82e
movl %edx, %edx
leaq (%r15,%rdx,8), %r10
movq (%r15,%rdx,8), %r8
leaq (%rax,%rdx), %r11
xorl %r9d, %r9d
movq %r11, %rbx
addq %r9, %rbx
je 0xae812
cmpq (%r10,%r9,8), %r8
jne 0xae816
incq %r9
jmp 0xae7ff
movl %ecx, %edx
jmp 0xae81c
addl %r9d, %edx
movl %r9d, %edi
shlq $0x20, %r8
movl %edi, %edi
orq %r8, %rdi
movq %rdi, (%r15,%rsi,8)
incl %r14d
jmp 0xae7e5
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %edi, %edi
xorl %eax, %eax
cmpq %rcx, %rdx
je 0xae899
movq 0x20(%rsp), %r9
movq 0x20(%r9), %r9
cmpb $0x2, 0x4(%r9,%rdx,8)
jne 0xae894
imulq $0xc, %rdx, %r10
movl 0x4(%r13,%r10), %r9d
movl (%r13,%r10), %r10d
andl %r8d, %r10d
subl %r10d, %r9d
imulq $0x18, %rdx, %r11
leaq (%r11,%rbp), %r10
movl 0xc(%rbp,%r11), %r11d
imulq $0x28, %r11, %r11
xorl %ebx, %ebx
cmpq %rbx, %r11
je 0xae891
movq (%r10), %r14
movl 0x8(%r14,%rbx), %r12d
subl (%r14,%rbx), %r12d
addq %r12, %rax
addq $0x28, %rbx
jmp 0xae877
addq %r9, %rdi
incq %rdx
jmp 0xae83a
testq %rdi, %rdi
jle 0xae8a8
cqto
idivq %rdi
movq %rax, %r8
jmp 0xae8ab
xorl %r8d, %r8d
movq 0x10(%rsp), %r12
leaq 0x68(%rsp), %r14
movq %r14, (%rsp)
leaq 0x40(%rsp), %rdx
leaq 0x70(%rsp), %rbx
movq %r15, %rdi
movq %r12, %rcx
movq %rbx, %r9
callq 0xae403
movq (%rbx), %rcx
pushq $0x2
popq %rsi
movq %rcx, %rax
cqto
idivq %rsi
cmpq $0xa, %rcx
pushq $0x5
popq %rdx
cmovll %eax, %edx
movq 0x30(%rsp), %rax
movl %edx, (%rax)
movq (%r14), %rax
cmpq $-0x1, %rax
je 0xae914
addq %rcx, %rax
cqto
idivq %rsi
cmpq %rax, %r12
cmovgq %r12, %rax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
leal (%rax,%rax,2), %eax
jmp 0xae965
cvtsi2sd %rcx, %xmm0
cmpq %r12, %rcx
jle 0xae940
movsd 0x134c6a(%rip), %xmm1   # 0x1e3590
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x139362(%rip), %xmm0   # 0x1e7ca0
jmp 0xae960
movsd 0x134b78(%rip), %xmm1   # 0x1e34c0
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x139338(%rip), %xmm0   # 0x1e7c98
cvttsd2si %xmm0, %rax
movq 0x38(%rsp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220