// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, 0x8(%rsp)
movapd 0x120d34(%rip), %xmm1   # 0x22d480
movapd %xmm1, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movl 0x8(%rdi), %eax
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x10c9b0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
xorl %esi, %esi
xorl %eax, %eax
movq %rax, 0x60(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
je 0x10c99d
movq 0x38(%rsp), %rdx
leaq (%rdx,%rax,8), %rcx
movq 0x10(%rcx), %rdi
movq 0xa0(%rsp), %rcx
movq 0x18(%rcx), %r8
movq %r8, 0x40(%rsp)
movq 0x40(%rcx), %r15
leaq (%rdx,%rax,8), %rax
addq $0x10, %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
movq %rdi, 0x50(%rsp)
movq %r13, %rbp
shlq $0x6, %rbp
movl 0x24(%rdi,%rbp), %ecx
movl 0x4(%r15,%rcx,8), %ebx
testl %ebx, %ebx
je 0x10c87b
movl 0x4(%rdi,%rbp), %eax
movl 0x8(%rdi,%rbp), %edx
decl %edx
movl (%r15,%rcx,8), %edi
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq 0x40(%rsp), %rdi
addq %rcx, %rdi
leaq 0x17df4b(%rip), %rcx    # 0x28a758
movl 0x1bc(%rcx), %r11d
xorl %ecx, %ecx
xorl %r8d, %r8d
movl (%rdi), %r9d
movl 0x4(%rdi), %r14d
cmpl %r14d, %edx
cmovbl %edx, %r14d
cmpl %r9d, %eax
cmoval %eax, %r9d
subl %r9d, %r14d
leal 0x1(%r14), %r12d
cmovbl %esi, %r12d
movl %ecx, %r9d
addl %r8d, %r9d
setne %r9b
testl %r12d, %r12d
sete %r14b
testb %r14b, %r9b
jne 0x10c880
testl %r12d, %r12d
je 0x10c871
movl 0x14(%rdi), %r9d
testl %r9d, %r9d
je 0x10c869
testb $0x2, %r9b
je 0x10c86e
cmpl $0x2, %r11d
jg 0x10c86e
addl %r12d, %ecx
jmp 0x10c871
addl %r12d, %r8d
addq $0x1c, %rdi
decl %ebx
jne 0x10c819
jmp 0x10c880
xorl %r8d, %r8d
xorl %ecx, %ecx
movl %r8d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
addl %r8d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x10c951
movq 0x48(%rsp), %rax
movq (%rax), %rbx
movl 0x28(%rbx,%rbp), %eax
movl 0x4(%r15,%rax,8), %ecx
testl %ecx, %ecx
je 0x10c960
movl 0xc(%rbx,%rbp), %edx
movl 0x10(%rbx,%rbp), %r8d
decl %r8d
movl (%r15,%rax,8), %r11d
leaq (%r11,%r11,8), %rax
leaq (%rax,%rax,2), %rax
addq 0x40(%rsp), %r11
addq %rax, %r11
leaq 0x17de74(%rip), %rax    # 0x28a758
movl 0x1bc(%rax), %r12d
xorl %eax, %eax
xorl %edi, %edi
movl (%r11), %r9d
movl 0x4(%r11), %r14d
cmpl %r14d, %r8d
cmovbl %r8d, %r14d
cmpl %r9d, %edx
cmoval %edx, %r9d
subl %r9d, %r14d
leal 0x1(%r14), %r9d
cmovbl %esi, %r9d
movl %eax, %r14d
addl %edi, %r14d
setne %r14b
testl %r9d, %r9d
sete %r10b
testb %r10b, %r14b
jne 0x10c964
testl %r9d, %r9d
je 0x10c947
movl 0x14(%r11), %r14d
testl %r14d, %r14d
je 0x10c93f
testb $0x2, %r14b
je 0x10c944
cmpl $0x2, %r12d
jg 0x10c944
addl %r9d, %eax
jmp 0x10c947
addl %r9d, %edi
addq $0x1c, %r11
decl %ecx
jne 0x10c8ef
jmp 0x10c964
movq 0x48(%rsp), %rax
movq (%rax), %rbx
movq 0x50(%rsp), %rdi
jmp 0x10c987
xorl %edi, %edi
xorl %eax, %eax
movl %edi, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
addl %edi, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
movq 0x50(%rsp), %rdi
jb 0x10c98c
movb $0x2, 0x2(%rbx,%rbp)
incq %r13
movl %r13d, %eax
cmpq %rax, 0x18(%rsp)
ja 0x10c7d0
movq 0x60(%rsp), %rax
incq %rax
cmpq 0x58(%rsp), %rax
jne 0x10c779
movq 0x10(%rsp), %r10
cmpl $0x0, 0x8(%r10)
je 0x10ca50
xorl %eax, %eax
movq (%r10), %r8
leaq (%rax,%rax,2), %rcx
cmpq $0x0, (%r8,%rcx,8)
je 0x10ca37
xorl %edi, %edi
movl $0x1, %edx
xorl %esi, %esi
movq 0x10(%r8,%rcx,8), %r8
shlq $0x6, %rdi
cmpb $0x2, 0x2(%r8,%rdi)
je 0x10ca22
addq %r8, %rdi
movl %esi, %r9d
shlq $0x6, %r9
movupd (%rdi), %xmm0
movupd 0x10(%rdi), %xmm1
movupd 0x20(%rdi), %xmm2
movups 0x30(%rdi), %xmm3
movups %xmm3, 0x30(%r8,%r9)
movupd %xmm2, 0x20(%r8,%r9)
movupd %xmm1, 0x10(%r8,%r9)
movupd %xmm0, (%r8,%r9)
incl %esi
movl %edx, %edi
movq (%r10), %r8
incl %edx
cmpq %rdi, (%r8,%rcx,8)
ja 0x10c9d9
leaq (%r8,%rcx,8), %rcx
movl %esi, %edx
jmp 0x10ca3d
leaq (%r8,%rcx,8), %rcx
xorl %edx, %edx
movq %rdx, (%rcx)
incq %rax
movl 0x8(%r10), %ecx
cmpq %rcx, %rax
jb 0x10c9c2
cmpl $0x0, 0x8(%r10)
je 0x10cd12
xorl %edx, %edx
leaq 0x20(%rsp), %r14
movq (%r10), %rcx
movq %rdx, 0x18(%rsp)
leaq (%rdx,%rdx,2), %rbp
cmpq $0x0, (%rcx,%rbp,8)
je 0x10ccfd
xorl %esi, %esi
xorl %r15d, %r15d
movq 0x10(%rcx,%rbp,8), %r12
shlq $0x6, %rsi
movl 0x24(%r12,%rsi), %edx
movl 0x28(%r12,%rsi), %eax
leaq (%rax,%rax,2), %rbx
leaq (%rcx,%rbx,8), %rdi
movq (%rdi), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
testq %r8, %r8
je 0x10cad5
movq 0x10(%rdi), %r10
xorl %r13d, %r13d
xorl %r11d, %r11d
shlq $0x6, %r13
cmpl %eax, 0x24(%r10,%r13)
jne 0x10cac5
addq %r10, %r13
cmpl %edx, 0x28(%r13)
je 0x10cad2
incl %r11d
movq %r11, %r13
cmpq %r11, %r8
ja 0x10cab1
jmp 0x10cad5
movl %r11d, %r9d
addq %rsi, %r12
cmpl $-0x1, %r9d
je 0x10cbd3
movq 0x10(%rcx,%rbx,8), %rcx
movslq %r9d, %rsi
shlq $0x6, %rsi
leaq (%rcx,%rsi), %r13
movb (%r12), %dil
cmpb (%rcx,%rsi), %dil
jne 0x10cb11
movzbl 0x1(%r12), %ecx
movzbl 0x1(%r13), %esi
cmpl %esi, 0x20(%rsp,%rcx,4)
je 0x10cce5
movq 0x30(%r12), %rsi
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
cmpq 0x30(%r13), %rsi
jge 0x10cc3e
leaq (%rdx,%rdx,2), %rbx
movq 0x8(%rcx,%rbx,8), %rax
cmpq %rax, (%rcx,%rbx,8)
jne 0x10cb7b
leaq (%rcx,%rbx,8), %rdi
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r14
movq %rsi, 0x8(%rcx,%r14)
movq 0x8(%rdi), %rsi
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
leaq 0x20(%rsp), %r14
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rcx
movq 0x10(%rax,%rbx,8), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rax,%rbx,8)
shlq $0x6, %rcx
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movupd 0x20(%r12), %xmm2
movups 0x30(%r12), %xmm3
movups %xmm3, 0x30(%rdx,%rcx)
movupd %xmm2, 0x20(%rdx,%rcx)
movupd %xmm1, 0x10(%rdx,%rcx)
movupd %xmm0, (%rdx,%rcx)
movq %r12, %rdi
movq %r13, %rsi
jmp 0x10ccdd
movq 0x8(%rdi), %rdx
cmpq %rdx, %r8
jne 0x10cc1c
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 0x8(%rdi), %rsi
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
leaq 0x20(%rsp), %r14
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rdi
leaq 0x1(%rdi), %rcx
movq %rcx, (%rax,%rbx,8)
shlq $0x6, %rdi
addq 0x10(%rax,%rbx,8), %rdi
jmp 0x10ccda
movq 0x8(%rcx,%rbx,8), %rdx
cmpq %rdx, (%rcx,%rbx,8)
jne 0x10cc8d
leaq (%rcx,%rbx,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 0x8(%rdi), %rsi
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
leaq 0x20(%rsp), %r14
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rcx
movq 0x10(%rax,%rbx,8), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rax,%rbx,8)
shlq $0x6, %rcx
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movupd 0x20(%r13), %xmm2
movups 0x30(%r13), %xmm3
movups %xmm3, 0x30(%rdx,%rcx)
movupd %xmm2, 0x20(%rdx,%rcx)
movupd %xmm1, 0x10(%rdx,%rcx)
movupd %xmm0, (%rdx,%rcx)
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10c0f4
incl %r15d
movq 0x10(%rsp), %r10
movq (%r10), %rcx
movq %r15, %rsi
cmpq %r15, (%rcx,%rbp,8)
ja 0x10ca7e
movq 0x18(%rsp), %rdx
incq %rdx
movl 0x8(%r10), %eax
cmpq %rax, %rdx
jb 0x10ca62
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq