// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: purge_ovlp_cov_adv(unsigned int, kv_u_trans_t*, asg64_v*, ug_rid_cov_t*, unsigned long)
void purge_ovlp_cov_adv(uint32_t id, kv_u_trans_t *ta, asg64_v *b64, ug_rid_cov_t *cc, uint64_t hom_cut)
{
    ma_utg_t *u = &(cc->ug->u.a[id]); uint32_t k, n, cn;
    u_trans_t *a = NULL, t; 
    kv_resize(uint64_t, *b64, u->n);
    memcpy(b64->a, cc->cov.a+cc->idx[id], sizeof((*(b64->a)))*cc->ug->u.a[id].n);
    a = u_trans_a(*ta, id); n = u_trans_n(*ta, id); 
    // if(id == 7929)  {
    //     fprintf(stderr, "\n[M::%s]\tutg%.6u%c\tn::%u\n", __func__, id+1, "lc"[cc->ug->u.a[id].circ], n);
    // }
    for (k = cn = 0; k < n; k++) {///RC_0/RC_1 to a[0, r_n)
        // if(id == 7929)  {
        //     fprintf(stderr, "-0-[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tis_reliable::%u\tw::%f\n", __func__, 
        //     a[k].qn+1, "lc"[cc->ug->u.a[a[k].qn].circ], cc->ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
        //     a[k].tn+1, "lc"[cc->ug->u.a[a[k].tn].circ], cc->ug->u.a[a[k].tn].len, a[k].ts, a[k].te, 
        //     ((a[k].f == RC_0) || (a[k].f == RC_1))?1:0, a[k].nw);
        // }
        if((a[k].f == RC_0) || (a[k].f == RC_1)) {
            if(k != cn) {
                t = a[k]; a[k] = a[cn]; a[cn] = t;
            }
            cn++;
        }
    }
    // if(id == 7929)  {
    //     fprintf(stderr, "[M::%s]\tutg%.6u%c\tn::%u\tcn::%u\n", __func__, 
    //         id+1, "lc"[cc->ug->u.a[id].circ], n, cn);
    // }
    if(cn >= n) return;
    if(n-cn > 1) qsort(a+cn, n-cn, sizeof((*a)), cmp_u_trans_weight);
    for (k = 0; k < cn; k++) {///calculate coverage for 
        append_cov_line_ug_rid_cov_t(id, b64->a, &(a[k]), cc, ((uint64_t)-1), -1);
    }

    for (k = cn; k < n; k++) {
        if(append_cov_line_ug_rid_cov_t(id, b64->a, &(a[k]), cc, hom_cut, 0.51)) {
            append_cov_line_ug_rid_cov_t(id, b64->a, &(a[k]), cc, ((uint64_t)-1), -1);
            continue;
        }
        a[k].del = 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq (%rcx), %rax
movq 0x10(%rax), %rax
movl %edi, %ebx
imulq $0x28, %rbx, %r13
movl 0x10(%rax,%r13), %edx
cmpq %rdx, 0x8(%r15)
jae 0x8b99d
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r14), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%r13), %edx
jmp 0x8b9a1
movq 0x10(%r15), %rdi
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rsi
shlq $0x3, %rsi
addq 0x28(%r14), %rsi
shlq $0x3, %rdx
callq 0x2160
movq 0x10(%r12), %rdx
movq 0x28(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r8
movq %rdx, 0x10(%rsp)
leaq (%rdx,%r8), %r12
movl %eax, %ebp
xorl %ecx, %ecx
movq %r12, %rsi
xorl %edx, %edx
movl %edx, %r13d
cmpq %rcx, %rbp
je 0x8ba5d
testb $0x3e, 0x28(%rsi)
jne 0x8ba54
cmpq %r13, %rcx
je 0x8ba52
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movaps %xmm2, 0x40(%rsp)
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
imulq $0x30, %r13, %rdi
movups (%r12,%rdi), %xmm0
movups 0x10(%r12,%rdi), %xmm1
movups 0x20(%r12,%rdi), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movups %xmm2, 0x20(%r12,%rdi)
movups %xmm1, 0x10(%r12,%rdi)
movups %xmm0, (%r12,%rdi)
incl %edx
incq %rcx
addq $0x30, %rsi
jmp 0x8b9e5
subl %edx, %eax
jbe 0x8bb15
movq %r8, 0x8(%rsp)
cmpl $0x2, %eax
jb 0x8ba87
imulq $0x30, %r13, %rdi
addq %r12, %rdi
movl %eax, %esi
leaq -0x41cd(%rip), %rcx     # 0x878b2
pushq $0x30
popq %rdx
callq 0x23f0
movq %r13, %rbx
subq $0x1, %rbx
jb 0x8bab1
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
pushq $-0x1
popq %r8
movsd 0x157ada(%rip), %xmm0   # 0x1e3580
callq 0x8af8f
addq $0x30, %r12
jmp 0x8ba8a
movq 0x10(%rsp), %rax
addq 0x8(%rsp), %rax
imulq $0x30, %r13, %r12
addq %rax, %r12
pushq $-0x1
popq %rbx
cmpq %rbp, %r13
jae 0x8bb15
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq 0x18(%rsp), %r8
movsd 0x157ab7(%rip), %xmm0   # 0x1e3598
callq 0x8af8f
testl %eax, %eax
je 0x8bb06
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %rbx, %r8
movsd 0x157a81(%rip), %xmm0   # 0x1e3580
callq 0x8af8f
jmp 0x8bb0c
orb $-0x80, 0x28(%r12)
incq %r13
addq $0x30, %r12
jmp 0x8bac5
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq