// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, %r13
movq %rsi, %rbp
xorl %r12d, %r12d
movq %r8, 0x10(%rsp)
movl %r12d, (%r8)
movq %rcx, 0x28(%rsp)
movl %r12d, (%rcx)
movq %r9, 0x18(%rsp)
movl $0xffffffff, (%r9)      # imm = 0xFFFFFFFF
movq %rdi, 0x20(%rsp)
movl 0x14(%rdi), %ebx
movq %rbx, %rdi
andq $0x7fffffff, %rdi       # imm = 0x7FFFFFFF
movl $0x8, %esi
callq 0x2120
movq %rax, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
andq $0x7fffffff, %rbx       # imm = 0x7FFFFFFF
je 0xed986
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
xorl %edi, %edi
leaq (%r8,%r8,2), %rax
movl 0xc(%rbp,%rax,8), %esi
testq %rsi, %rsi
je 0xed941
leaq (,%rax,8), %rdx
addq %rbp, %rdx
movq (%rdx), %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %r10
xorl %r11d, %r11d
xorl %esi, %esi
movl 0x8(%rdx,%r11), %r15d
subl (%rdx,%r11), %r15d
addq %r15, %rsi
addq $0x28, %r11
cmpq %r11, %r10
jne 0xed92a
jmp 0xed943
xorl %esi, %esi
movl 0x4(%r13,%rax,4), %r10d
movl (%r13,%rax,4), %eax
andl %r9d, %eax
cmpl %eax, %r10d
je 0xed96a
subl %eax, %r10d
movq %rsi, %rax
xorl %edx, %edx
divq %r10
movq %rax, (%r14,%r8,8)
addq %r10, %rdi
addq %rsi, %rcx
incq %r8
cmpq %rbx, %r8
jne 0xed902
testq %rdi, %rdi
jle 0xed986
movq %rcx, %rax
cqto
idivq %rdi
movq %rax, 0x8(%rsp)
jmp 0xed98f
movq $0x0, 0x8(%rsp)
leaq (%r14,%rbx,8), %rsi
movq %r14, %rdi
callq 0x9dc69
movq 0x20(%rsp), %rdi
movl 0x14(%rdi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xeda70
xorl %ecx, %ecx
xorl %r12d, %r12d
movl %ecx, %ecx
movq (%r14,%rcx,8), %rsi
movq %rsi, %rdx
shlq $0x20, %rdx
cmpq (%r14,%rcx,8), %rsi
jne 0xed9d3
incq %rcx
incq %rdx
cmpq %rax, %rcx
jb 0xed9c0
jmp 0xed9e3
movl %r12d, %esi
movq %rdx, (%r14,%rsi,8)
incl %r12d
cmpl %ecx, %eax
ja 0xed9b3
jmp 0xed9ed
movl %r12d, %ecx
movq %rdx, (%r14,%rcx,8)
incl %r12d
testl %eax, %eax
je 0xeda70
movq 0x20(%rdi), %rcx
movl %eax, %edx
xorl %edi, %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %esi, %esi
cmpb $0x2, 0x4(%rcx,%rdi,8)
jne 0xeda59
leaq (%rdi,%rdi,2), %r9
movl 0x4(%r13,%r9,4), %r10d
movl (%r13,%r9,4), %r11d
andl %r8d, %r11d
subl %r11d, %r10d
addq %r10, %rsi
movl 0xc(%rbp,%r9,8), %r10d
testq %r10, %r10
je 0xeda59
leaq (,%r9,8), %r9
addq %rbp, %r9
movq (%r9), %r9
shlq $0x3, %r10
leaq (%r10,%r10,4), %r10
xorl %r11d, %r11d
movl 0x8(%r9,%r11), %ebx
subl (%r9,%r11), %ebx
addq %rbx, %rax
addq $0x28, %r11
cmpq %r11, %r10
jne 0xeda44
incq %rdi
cmpq %rdx, %rdi
jne 0xeda03
testq %rsi, %rsi
jle 0xeda70
cqto
idivq %rsi
movq %rax, %r8
jmp 0xeda73
xorl %r8d, %r8d
movl %r12d, %esi
leaq 0x58(%rsp), %rbx
movq %rbx, (%rsp)
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq 0x8(%rsp), %r12
movq %r12, %rcx
movq %r15, %r9
callq 0xed55c
movq (%r15), %rax
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
shrq %rcx
cmpq $0xa, %rax
movl $0x5, %edx
cmovll %ecx, %edx
movq 0x28(%rsp), %rcx
movl %edx, (%rcx)
movq (%rbx), %rcx
cmpq $-0x1, %rcx
je 0xedaeb
addq %rax, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
addq %rcx, %rax
sarq %rax
cmpq %rax, %r12
cmovgq %r12, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
leal (%rax,%rax,2), %eax
jmp 0xedb3f
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpq %r12, %rax
jle 0xedb1a
movsd 0x143b40(%rip), %xmm1   # 0x231640
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x148288(%rip), %xmm0   # 0x235da0
jmp 0xedb3a
movsd 0x143a4e(%rip), %xmm1   # 0x231570
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x14825e(%rip), %xmm0   # 0x235d98
cvttsd2si %xmm0, %rax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
movq %r14, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220