// File: /workspace/github_cpp_makefile_star2_O1/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 %ebx, %ebx
movq %r8, 0x10(%rsp)
movl %ebx, (%r8)
movq %rcx, 0x28(%rsp)
movl %ebx, (%rcx)
movq %r9, 0x18(%rsp)
movl $0xffffffff, (%r9)      # imm = 0xFFFFFFFF
movq %rdi, 0x20(%rsp)
movl 0x14(%rdi), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl $0x8, %esi
movq %r12, %rdi
callq 0x2120
movq %rax, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
testq %r12, %r12
je 0xec06d
movl %r12d, %edi
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
xorl %ebx, %ebx
leaq (%r8,%r8,2), %rax
movl 0xc(%rbp,%rax,8), %esi
testq %rsi, %rsi
je 0xec03a
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 0xec023
jmp 0xec03c
xorl %esi, %esi
movl 0x4(%r13,%rax,4), %r10d
movl (%r13,%rax,4), %eax
andl %r9d, %eax
cmpl %eax, %r10d
je 0xec063
subl %eax, %r10d
movq %rsi, %rax
xorl %edx, %edx
divq %r10
movq %rax, (%r14,%r8,8)
addq %r10, %rbx
addq %rsi, %rcx
incq %r8
cmpq %rdi, %r8
jne 0xebffb
jmp 0xec06f
xorl %ecx, %ecx
testq %rbx, %rbx
jle 0xec081
movq %rcx, %rax
cqto
idivq %rbx
movq %rax, %rbx
jmp 0xec083
xorl %ebx, %ebx
leaq (%r14,%r12,8), %rsi
movq %r14, %rdi
callq 0x9d53f
movq 0x20(%rsp), %r10
movl 0x14(%r10), %eax
xorl %esi, %esi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xec0e5
xorl %ecx, %ecx
movl %esi, %edx
incl %esi
cmpl %eax, %esi
cmovbel %eax, %esi
movq (%r14,%rdx,8), %r9
xorl %edi, %edi
movq %rdx, %r8
cmpq (%r14,%r8,8), %r9
jne 0xec0c7
incl %edi
incq %r8
cmpq %rax, %r8
jb 0xec0b5
jmp 0xec0ca
movl %r8d, %esi
movq (%r14,%rdx,8), %rdx
shlq $0x20, %rdx
movl %edi, %edi
orq %rdx, %rdi
movl %ecx, %edx
movq %rdi, (%r14,%rdx,8)
incl %ecx
cmpl %eax, %esi
jb 0xec0a3
movl %ecx, %esi
testl %eax, %eax
je 0xec15d
movq 0x20(%r10), %rdx
movl %eax, %edi
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %ecx, %ecx
cmpb $0x2, 0x4(%rdx,%r8,8)
jne 0xec153
leaq (%r8,%r8,2), %r10
movl 0x4(%r13,%r10,4), %r11d
movl (%r13,%r10,4), %r15d
andl %r9d, %r15d
subl %r15d, %r11d
addq %r11, %rcx
movl 0xc(%rbp,%r10,8), %r11d
testq %r11, %r11
je 0xec153
leaq (,%r10,8), %r10
addq %rbp, %r10
movq (%r10), %r10
shlq $0x3, %r11
leaq (%r11,%r11,4), %r11
xorl %r15d, %r15d
movl 0x8(%r10,%r15), %r12d
subl (%r10,%r15), %r12d
addq %r12, %rax
addq $0x28, %r15
cmpq %r15, %r11
jne 0xec13e
incq %r8
cmpq %rdi, %r8
jne 0xec0fc
jmp 0xec161
xorl %ecx, %ecx
xorl %eax, %eax
testq %rcx, %rcx
jle 0xec170
cqto
idivq %rcx
movq %rax, %r8
jmp 0xec173
xorl %r8d, %r8d
leaq 0x58(%rsp), %r12
movq %r12, (%rsp)
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rcx
movq %r15, %r9
callq 0xebc6d
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 (%r12), %rcx
cmpq $-0x1, %rcx
je 0xec1e4
addq %rax, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
addq %rcx, %rax
sarq %rax
cmpq %rax, %rbx
cmovgq %rbx, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
leal (%rax,%rax,2), %eax
jmp 0xec238
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpq %rbx, %rax
jle 0xec213
movsd 0x139347(%rip), %xmm1   # 0x225540
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x13d997(%rip), %xmm0   # 0x229ba8
jmp 0xec233
movsd 0x139255(%rip), %xmm1   # 0x225470
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
mulsd 0x13d96d(%rip), %xmm0   # 0x229ba0
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