// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: normalize_hap_overlaps_advance_by_p_g_t(hap_overlaps_list*, hap_overlaps_list*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, R_to_U*, p_g_t*, hap_cov_t*, double)
void normalize_hap_overlaps_advance_by_p_g_t(hap_overlaps_list* all_ovlp, hap_overlaps_list* back_all_ovlp,
ma_ug_t *ug, asg_t *read_g, ma_hit_t_alloc* reverse_sources, R_to_U* ruIndex, p_g_t *pg, hap_cov_t *cov, 
double filter_rate)
{
    hap_overlaps *x = NULL, *y = NULL;
    uint32_t v, i, uId, qn, tn;
    uint32_t types[4];
    

    types[X2Y] = Y2X; types[Y2X] = X2Y; types[XCY] = YCX; types[YCX] = XCY;
    int index;
    uint32_t k, qs, qe, ts, te, occ, as, ae, ovlp, hetLen, homLen;
    p_node_t *a = NULL;


    for (v = 0; v < all_ovlp->num; v++)
    {
        uId = v;
        for (i = 0; i < all_ovlp->x[uId].a.n; i++)
        {
            /*****************qn*****************/
            qn = all_ovlp->x[uId].a.a[i].xUid;
            qs = all_ovlp->x[uId].a.a[i].x_beg_pos;
            qe = all_ovlp->x[uId].a.a[i].x_end_pos - 1;
            get_p_nodes(pg, &a, &occ, qn);
            for (k = 0, hetLen = 0, homLen = 0; k < occ; k++)
            {
                as = a[k].baseBeg;
                ae = a[k].baseEnd;
                ovlp = ((MIN(qe, ae) >= MAX(qs, as))? MIN(qe, ae) - MAX(qs, as) + 1 : 0);
                if(homLen + hetLen > 0 && ovlp == 0) break;
                if(ovlp == 0) continue;
                if(a[k].h_status == N_HET)
                {
                    homLen += ovlp;
                } 
                else if(asm_opt.polyploidy <= 2 && (a[k].h_status&P_HET))///if(asm_opt.polyploidy <= 2 && (a[k].h_status&S_HET))
                {
                    homLen += ovlp;
                }
                else
                {
                    hetLen += ovlp;
                }                
            }

            if(hetLen <= ((hetLen + homLen) * filter_rate))
            {
                all_ovlp->x[uId].a.a[i].status = DELETE;
                continue;
            }
            /*****************qn*****************/


            /*****************tn*****************/
            tn = all_ovlp->x[uId].a.a[i].yUid;
            ts = all_ovlp->x[uId].a.a[i].y_beg_pos;
            te = all_ovlp->x[uId].a.a[i].y_end_pos - 1;
            get_p_nodes(pg, &a, &occ, tn);
            for (k = 0, hetLen = 0, homLen = 0; k < occ; k++)
            {
                as = a[k].baseBeg;
                ae = a[k].baseEnd;
                ovlp = ((MIN(te, ae) >= MAX(ts, as))? MIN(te, ae) - MAX(ts, as) + 1 : 0);
                if(homLen + hetLen > 0 && ovlp == 0) break;
                if(ovlp == 0) continue;
                if(a[k].h_status == N_HET)
                {
                    homLen += ovlp;
                } 
                else if(asm_opt.polyploidy <= 2 && (a[k].h_status&P_HET))///if(asm_opt.polyploidy <= 2 && (a[k].h_status&S_HET))
                {
                    homLen += ovlp;
                }
                else
                {
                    hetLen += ovlp;
                }                 
            }

            if(hetLen <= ((hetLen + homLen) * filter_rate))
            {
                all_ovlp->x[uId].a.a[i].status = DELETE;
                continue;
            }
            /*****************tn*****************/
        }
    }

    for (v = 0; v < all_ovlp->num; v++)
    {
        uId = v;
        k = 0;
        for (i = 0; i < all_ovlp->x[uId].a.n; i++)
        {
            if(all_ovlp->x[uId].a.a[i].status == DELETE) continue;
            all_ovlp->x[uId].a.a[k] = all_ovlp->x[uId].a.a[i];
            k++;
        }
        all_ovlp->x[uId].a.n = k;
    }


    for (v = 0; v < all_ovlp->num; v++)
    {
        uId = v;
        for (i = 0; i < all_ovlp->x[uId].a.n; i++)
        {
            qn = all_ovlp->x[uId].a.a[i].xUid;
            tn = all_ovlp->x[uId].a.a[i].yUid;
            x = &(all_ovlp->x[uId].a.a[i]);
            index = get_specific_hap_overlap(&(all_ovlp->x[tn]), tn, qn);
            if(index != -1)
            {
                y = &(all_ovlp->x[tn].a.a[index]);
                if(x->rev == y->rev && types[x->type]==y->type) continue;
                if(x->score >= y->score)
                {
                    kv_push(hap_overlaps, back_all_ovlp->x[tn].a, (*y));
                    set_reverse_hap_overlap(y, x, types);
                }
                else
                {
                    kv_push(hap_overlaps, back_all_ovlp->x[qn].a, (*x));
                    set_reverse_hap_overlap(x, y, types);
                }
            }
            else
            {
                kv_pushp(hap_overlaps, all_ovlp->x[tn].a, &y);
                set_reverse_hap_overlap(y, x, types);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x8(%rsp)
movapd 0x11d97c(%rip), %xmm1   # 0x1eb570
movapd %xmm1, 0x20(%rsp)
movq %rdi, (%rsp)
movl 0x8(%rdi), %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rsi, 0x30(%rsp)
cmpq %rsi, %rcx
je 0xcde16
movq (%rsp), %rax
movq (%rax), %rax
movq %rcx, 0x38(%rsp)
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rsi
movq %rsi, 0x40(%rsp)
movq (%rax,%rcx), %rax
movq %rax, 0x48(%rsp)
xorl %r10d, %r10d
movl %r10d, %r10d
cmpq %r10, 0x48(%rsp)
jbe 0xcde04
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rcx
movq %r10, %r15
shlq $0x6, %r15
leaq (%rcx,%r15), %rax
movq %rax, 0x10(%rsp)
movl 0x24(%rcx,%r15), %eax
movl 0x4(%rcx,%r15), %r14d
movq %rcx, 0x18(%rsp)
movl 0x8(%rcx,%r15), %edi
decl %edi
movq 0x90(%rsp), %rcx
movq 0x18(%rcx), %rbx
movq 0x40(%rcx), %rsi
movl (%rsi,%rax,8), %ecx
movq %rsi, 0x50(%rsp)
movl 0x4(%rsi,%rax,8), %esi
imulq $0x1c, %rcx, %rax
addq %rbx, %rax
leaq 0x185ab9(%rip), %rcx    # 0x253758
movl 0x1bc(%rcx), %ebp
imulq $0x1c, %rsi, %r8
xorl %r9d, %r9d
xorl %ecx, %ecx
xorl %esi, %esi
cmpq %r9, %r8
je 0xcdd1f
movl (%rax,%r9), %r11d
movl 0x4(%rax,%r9), %r12d
cmpl %r12d, %edi
cmovbl %edi, %r12d
cmpl %r11d, %r14d
cmoval %r14d, %r11d
subl %r11d, %r12d
leal 0x1(%r12), %r12d
cmovbl %edx, %r12d
movl %esi, %r11d
addl %ecx, %r11d
setne %r11b
testl %r12d, %r12d
sete %r13b
testb %r13b, %r11b
jne 0xcdd1f
testl %r12d, %r12d
je 0xcdd19
movl 0x14(%rax,%r9), %r11d
testl %r11d, %r11d
je 0xcdd16
cmpl $0x3, %ebp
setge %r13b
testb $0x2, %r11b
sete %r11b
orb %r13b, %r11b
je 0xcdd16
addl %r12d, %ecx
jmp 0xcdd19
addl %r12d, %esi
addq $0x1c, %r9
jmp 0xcdcb0
movl %ecx, %eax
cvtsi2sd %rax, %xmm1
addl %ecx, %esi
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0xcddf1
movq 0x10(%rsp), %rcx
movl 0x28(%rcx), %eax
movl 0xc(%rcx), %edi
movl 0x10(%rcx), %r14d
decl %r14d
movq 0x50(%rsp), %rsi
movl (%rsi,%rax,8), %ecx
movl 0x4(%rsi,%rax,8), %eax
imulq $0x1c, %rcx, %rcx
addq %rcx, %rbx
imulq $0x1c, %rax, %rsi
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %r8, %rsi
je 0xcddd9
movl (%rbx,%r8), %r9d
movl 0x4(%rbx,%r8), %r11d
cmpl %r11d, %r14d
cmovbl %r14d, %r11d
cmpl %r9d, %edi
cmoval %edi, %r9d
subl %r9d, %r11d
leal 0x1(%r11), %r9d
cmovbl %edx, %r9d
movl %eax, %r11d
addl %ecx, %r11d
setne %r11b
testl %r9d, %r9d
sete %r12b
testb %r12b, %r11b
jne 0xcddd9
testl %r9d, %r9d
je 0xcddd3
movl 0x14(%rbx,%r8), %r11d
testl %r11d, %r11d
je 0xcddd0
cmpl $0x3, %ebp
setge %r12b
testb $0x2, %r11b
sete %r11b
orb %r12b, %r11b
je 0xcddd0
addl %r9d, %ecx
jmp 0xcddd3
addl %r9d, %eax
addq $0x1c, %r8
jmp 0xcdd6b
movl %ecx, %esi
cvtsi2sd %rsi, %xmm1
addl %ecx, %eax
cvtsi2sd %rax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0xcddfc
movq 0x18(%rsp), %rax
movb $0x2, 0x2(%rax,%r15)
incl %r10d
jmp 0xcdc38
movq 0x38(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %rsi
jmp 0xcdc0a
xorl %eax, %eax
movl %esi, %ecx
cmpq %rcx, %rax
jae 0xcde97
xorl %ecx, %ecx
imulq $0x18, %rax, %rdx
xorl %esi, %esi
movl %esi, %esi
movq (%rsp), %rdi
movq (%rdi), %r9
leaq (%r9,%rdx), %r8
movl %ecx, %edi
cmpq %rsi, (%r9,%rdx)
jbe 0xcde88
movq 0x10(%r8), %r8
movq %rsi, %r9
shlq $0x6, %r9
cmpb $0x2, 0x2(%r8,%r9)
je 0xcde84
addq %r8, %r9
shlq $0x6, %rdi
movups (%r9), %xmm0
movupd 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups 0x30(%r9), %xmm3
movups %xmm3, 0x30(%r8,%rdi)
movups %xmm2, 0x20(%r8,%rdi)
movupd %xmm1, 0x10(%r8,%rdi)
movups %xmm0, (%r8,%rdi)
incl %ecx
incl %esi
jmp 0xcde27
movq %rdi, (%r8)
incq %rax
movq (%rsp), %rcx
movl 0x8(%rcx), %esi
jmp 0xcde18
xorl %ecx, %ecx
movl %esi, %eax
cmpq %rax, %rcx
jae 0xce0c4
xorl %ebx, %ebx
movq %rcx, 0x10(%rsp)
imulq $0x18, %rcx, %rcx
movq %rcx, 0x18(%rsp)
movl %ebx, %ebx
movq (%rsp), %rax
movq (%rax), %r15
cmpq %rbx, (%r15,%rcx)
jbe 0xce0b0
leaq (%r15,%rcx), %rax
movq 0x10(%rax), %rax
movq %rbx, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %r12
movl 0x24(%rax,%rcx), %ebp
movl 0x28(%rax,%rcx), %esi
imulq $0x18, %rsi, %r14
leaq (%r15,%r14), %r13
movq %r13, %rdi
movl %ebp, %edx
callq 0xcd675
cmpl $-0x1, %eax
je 0xcdfcd
movq 0x10(%r15,%r14), %rcx
cltq
shlq $0x6, %rax
leaq (%rcx,%rax), %r13
movb (%r12), %dl
cmpb (%rcx,%rax), %dl
jne 0xcdf2a
movzbl 0x1(%r12), %eax
movzbl 0x1(%r13), %ecx
cmpl %ecx, 0x20(%rsp,%rax,4)
je 0xce0a4
movq 0x30(%r12), %rax
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
cmpq 0x30(%r13), %rax
jge 0xce01a
imulq $0x18, %rbp, %r14
movq (%rcx,%r14), %rax
cmpq 0x8(%rcx,%r14), %rax
jne 0xcdf85
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rcx,%r14), %rax
movq %rsi, 0x8(%rcx,%r14)
movq 0x10(%rax), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
movq (%rcx,%r14), %rax
movq 0x10(%rcx,%r14), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%r14)
shlq $0x6, %rax
movups (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups 0x30(%r12), %xmm3
movups %xmm3, 0x30(%rdx,%rax)
movups %xmm2, 0x20(%rdx,%rax)
movupd %xmm1, 0x10(%rdx,%rax)
movups %xmm0, (%rdx,%rax)
movq %r12, %rdi
movq %r13, %rsi
jmp 0xce09a
movq (%r13), %rdi
cmpq 0x8(%r13), %rdi
jne 0xce007
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15,%r14)
movq 0x10(%r13), %rdi
shlq $0x6, %rsi
callq 0x2370
movq (%rsp), %rcx
movq (%rcx), %r15
movq %rax, 0x10(%r15,%r14)
movq (%r15,%r14), %rdi
leaq 0x1(%rdi), %rax
movq %rax, (%r15,%r14)
shlq $0x6, %rdi
addq 0x10(%r15,%r14), %rdi
jmp 0xce097
movq (%rcx,%r14), %rax
cmpq 0x8(%rcx,%r14), %rax
jne 0xce05a
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
leaq (%rcx,%r14), %rax
movq %rsi, 0x8(%rcx,%r14)
movq 0x10(%rax), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
movq (%rcx,%r14), %rax
movq 0x10(%rcx,%r14), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%r14)
shlq $0x6, %rax
movups (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups 0x30(%r13), %xmm3
movups %xmm3, 0x30(%rdx,%rax)
movups %xmm2, 0x20(%rdx,%rax)
movupd %xmm1, 0x10(%rdx,%rax)
movups %xmm0, (%rdx,%rax)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
callq 0xcd6a4
incl %ebx
movq 0x18(%rsp), %rcx
jmp 0xcdeb4
movq 0x10(%rsp), %rcx
incq %rcx
movq (%rsp), %rax
movl 0x8(%rax), %esi
jmp 0xcde99
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq