// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movapd 0x127e7e(%rip), %xmm1   # 0x239690
movapd %xmm1, 0x30(%rsp)
movq %rdi, 0x8(%rsp)
movl 0x8(%rdi), %r8d
testq %r8, %r8
je 0x111dae
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
xorl %esi, %esi
xorl %eax, %eax
movq %r8, 0x40(%rsp)
movq %rax, 0x48(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, 0x50(%rsp)
testq %rcx, %rcx
je 0x111a65
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq 0x10(%rax), %r10
movq 0x90(%rsp), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x40(%rax), %rdx
xorl %r15d, %r15d
movq %rdx, 0x10(%rsp)
movq %r15, %r12
shlq $0x6, %r12
movl 0x24(%r10,%r12), %ecx
movl 0x4(%rdx,%rcx,8), %eax
testq %rax, %rax
je 0x111947
movl 0x4(%r10,%r12), %ebp
movl 0x8(%r10,%r12), %r13d
decl %r13d
movl (%rdx,%rcx,8), %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq 0x28(%rsp), %rcx
addq %rdx, %rcx
leaq 0x183e8e(%rip), %rdx    # 0x295758
movl 0x1bc(%rdx), %r14d
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %r8
addq %rax, %r8
xorl %edi, %edi
xorl %edx, %edx
xorl %eax, %eax
movl (%rcx,%rdi), %r9d
movl 0x4(%rcx,%rdi), %r11d
cmpl %r11d, %r13d
cmovbl %r13d, %r11d
cmpl %r9d, %ebp
cmoval %ebp, %r9d
subl %r9d, %r11d
leal 0x1(%r11), %r11d
cmovbl %esi, %r11d
movl %edx, %r9d
addl %eax, %r9d
setne %r9b
testl %r11d, %r11d
sete %bl
testb %bl, %r9b
jne 0x11194b
testl %r11d, %r11d
je 0x11193c
movl 0x14(%rcx,%rdi), %r9d
testl %r9d, %r9d
je 0x111934
testb $0x2, %r9b
je 0x111939
cmpl $0x2, %r14d
jg 0x111939
addl %r11d, %edx
jmp 0x11193c
addl %r11d, %eax
addq $0x1c, %rdi
cmpq %rdi, %r8
jne 0x1118e2
jmp 0x11194b
xorl %eax, %eax
xorl %edx, %edx
movl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
addl %eax, %edx
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x111a49
leaq (%r10,%r12), %rdx
movl 0x28(%rdx), %ecx
movq 0x10(%rsp), %rdi
movl 0x4(%rdi,%rcx,8), %eax
testq %rax, %rax
je 0x111a27
movl 0xc(%rdx), %ebp
movl 0x10(%rdx), %r13d
decl %r13d
movl (%rdi,%rcx,8), %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq 0x28(%rsp), %rcx
addq %rdx, %rcx
leaq 0x183dae(%rip), %rdx    # 0x295758
movl 0x1bc(%rdx), %r14d
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %r8
addq %rax, %r8
xorl %edi, %edi
xorl %edx, %edx
xorl %eax, %eax
movl (%rcx,%rdi), %r9d
movl 0x4(%rcx,%rdi), %r11d
cmpl %r11d, %r13d
cmovbl %r13d, %r11d
cmpl %r9d, %ebp
cmoval %ebp, %r9d
subl %r9d, %r11d
leal 0x1(%r11), %r11d
cmovbl %esi, %r11d
movl %edx, %r9d
addl %eax, %r9d
setne %r9b
testl %r11d, %r11d
sete %bl
testb %bl, %r9b
jne 0x111a2b
testl %r11d, %r11d
je 0x111a1c
movl 0x14(%rcx,%rdi), %r9d
testl %r9d, %r9d
je 0x111a14
testb $0x2, %r9b
je 0x111a19
cmpl $0x2, %r14d
jg 0x111a19
addl %r11d, %edx
jmp 0x111a1c
addl %r11d, %eax
addq $0x1c, %rdi
cmpq %rdi, %r8
jne 0x1119c2
jmp 0x111a2b
xorl %eax, %eax
xorl %edx, %edx
movl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
addl %eax, %edx
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x111a4f
movb $0x2, 0x2(%r10,%r12)
incq %r15
movl %r15d, %eax
cmpq %rax, 0x50(%rsp)
movq 0x10(%rsp), %rdx
ja 0x11188a
movq 0x48(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r8
cmpq %r8, %rax
jne 0x111840
testl %r8d, %r8d
je 0x111dae
movq 0x8(%rsp), %rax
movq (%rax), %rdx
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rax
cmpq $0x0, (%rdx,%rax,8)
je 0x111b13
xorl %r8d, %r8d
movl $0x1, %esi
xorl %edi, %edi
movq 0x10(%rdx,%rax,8), %r9
shlq $0x6, %r8
cmpb $0x2, 0x2(%r9,%r8)
je 0x111af7
addq %r9, %r8
movl %edi, %edx
shlq $0x6, %rdx
movupd (%r8), %xmm0
movupd 0x10(%r8), %xmm1
movupd 0x20(%r8), %xmm2
movups 0x30(%r8), %xmm3
movups %xmm3, 0x30(%r9,%rdx)
movupd %xmm2, 0x20(%r9,%rdx)
movupd %xmm1, 0x10(%r9,%rdx)
movupd %xmm0, (%r9,%rdx)
incl %edi
movq 0x8(%rsp), %rdx
movq (%rdx), %rdx
movl %esi, %r8d
incl %esi
cmpq %r8, (%rdx,%rax,8)
ja 0x111aa3
leaq (%rdx,%rax,8), %rax
movq 0x8(%rsp), %rsi
movl 0x8(%rsi), %r8d
movl %edi, %esi
jmp 0x111b19
leaq (%rdx,%rax,8), %rax
xorl %esi, %esi
movq %rsi, (%rax)
incq %rcx
movl %r8d, %eax
cmpq %rax, %rcx
jb 0x111a8e
testl %r8d, %r8d
je 0x111dae
movq 0x8(%rsp), %rax
movq (%rax), %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rbx
cmpq $0x0, (%rcx,%rbx,8)
je 0x111d9f
movq %rdx, 0x10(%rsp)
xorl %edx, %edx
xorl %ebp, %ebp
movq 0x10(%rcx,%rbx,8), %rsi
shlq $0x6, %rdx
leaq (%rsi,%rdx), %r12
movl 0x28(%rsi,%rdx), %eax
leaq (%rax,%rax,2), %r15
leaq (%rcx,%r15,8), %r8
movq (%r8), %rdi
testq %rdi, %rdi
je 0x111baf
movl 0x24(%rsi,%rdx), %edx
movq 0x10(%r8), %rsi
movl $0x1, %r9d
xorl %r10d, %r10d
shlq $0x6, %r10
cmpl %eax, 0x24(%rsi,%r10)
jne 0x111b9c
addq %rsi, %r10
cmpl %edx, 0x28(%r10)
je 0x111ba9
movl %r9d, %r10d
incl %r9d
cmpq %r10, %rdi
ja 0x111b88
jmp 0x111baf
subl $0x1, %r9d
jae 0x111c2a
cmpq 0x8(%r8), %rdi
jne 0x111bf0
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %edx
cmoveq %rdx, %rsi
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq %rsi, 0x8(%rcx,%r14)
movq 0x10(%r8), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
movq (%rcx,%r14), %rdi
leaq 0x1(%rdi), %rax
movq %rax, (%rcx,%r15,8)
shlq $0x6, %rdi
addq 0x10(%rcx,%r15,8), %rdi
movq %r12, %rsi
leaq 0x30(%rsp), %rdx
callq 0x11122f
incl %ebp
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq %rbp, %rdx
cmpq %rbp, (%rcx,%rbx,8)
ja 0x111b56
jmp 0x111d91
movslq %r9d, %rcx
shlq $0x6, %rcx
leaq (%rsi,%rcx), %r13
movb (%r12), %dil
cmpb (%rsi,%rcx), %dil
jne 0x111c50
movzbl 0x1(%r12), %ecx
movzbl 0x1(%r13), %esi
cmpl %esi, 0x30(%rsp,%rcx,4)
je 0x111c0e
movq 0x30(%r12), %rsi
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
cmpq 0x30(%r13), %rsi
jge 0x111d01
leaq (%rdx,%rdx,2), %r14
movq (%rcx,%r14,8), %rax
cmpq 0x8(%rcx,%r14,8), %rax
jne 0x111cb5
leaq (%rcx,%r14,8), %rdi
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r15
movq %rsi, 0x8(%rcx,%r15)
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r15)
movq (%rcx,%r15), %rax
movq 0x10(%rcx,%r14,8), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rcx,%r14,8)
shlq $0x6, %rax
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movupd 0x20(%r12), %xmm2
movups 0x30(%r12), %xmm3
movups %xmm3, 0x30(%rdx,%rax)
movupd %xmm2, 0x20(%rdx,%rax)
movupd %xmm1, 0x10(%rdx,%rax)
movupd %xmm0, (%rdx,%rax)
movq %r12, %rdi
movq %r13, %rsi
jmp 0x111c04
movq (%rcx,%r15,8), %rdx
cmpq 0x8(%rcx,%r15,8), %rdx
jne 0x111d4b
leaq (%rcx,%r15,8), %rdi
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rsi
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq %rsi, 0x8(%rcx,%r14)
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
movq (%rcx,%r14), %rdx
movq 0x10(%rcx,%r15,8), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, (%rcx,%r15,8)
shlq $0x6, %rdx
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movupd 0x20(%r13), %xmm2
movups 0x30(%r13), %xmm3
movups %xmm3, 0x30(%rax,%rdx)
movupd %xmm2, 0x20(%rax,%rdx)
movupd %xmm1, 0x10(%rax,%rdx)
movupd %xmm0, (%rax,%rdx)
movq %r13, %rdi
jmp 0x111c01
movq 0x8(%rsp), %rax
movl 0x8(%rax), %r8d
movq 0x10(%rsp), %rdx
incq %rdx
movl %r8d, %eax
cmpq %rax, %rdx
jb 0x111b3e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq