// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_length_trio(asg_t*, float, int, ma_hit_t_alloc*, long long, unsigned int, int, int, int, R_to_U*)
int asg_arc_del_short_diploid_by_length_trio(asg_t *g, float drop_ratio, int max_ext, 
ma_hit_t_alloc* reverse_sources, long long miniedgeLen, uint32_t stops_threshold,
int if_skip_bubble, int if_drop, int if_check_hap, R_to_U* ruIndex)
{
    double startTime = Get_T();
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));

	uint32_t v, n_vtx = g->n_seq * 2;
    long long n_cut = 0;
    
    for (v = 0; v < n_vtx; ++v) 
    {
        if(if_skip_bubble && g->seq_vis[v] != 0) continue;
        if(if_drop && g->seq[v>>1].c == ALTER_LABLE) continue;
        asg_arc_t *av = asg_arc_a(g, v);
        uint32_t nv = asg_arc_n(g, v);
        if (nv < 2) continue;
        uint64_t i;
        for (i = 0; i < nv; ++i)
        {
            kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i)));   
        }
	}

    radix_sort_arch64(b.a, b.a + b.n);

    uint64_t k;
    for (k = 0; k < b.n; k++)
    {
        asg_arc_t *a = &g->arc[(uint32_t)b.a[k]];
		///v is self id, w is the id of another end
		uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0;
		uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw;
		uint32_t ov_max = 0, ow_max = 0, ov_max_i = 0, ow_max_i = 0, trio_flag, non_trio_flag;
		asg_arc_t *av, *aw;
		///nv must be >= 2
		if (nv == 1 && nw == 1) continue;
        av = asg_arc_a(g, v);
		aw = asg_arc_a(g, w);
        kv = get_real_length(g, v, NULL);
        kw = get_real_length(g, w, NULL);
        if (kv <= 1 && kw <= 1) continue;
        trio_flag = get_tip_trio_infor(g, v^1);
        non_trio_flag = (uint32_t)-1;
        if(trio_flag == FATHER) non_trio_flag = MOTHER;
        if(trio_flag == MOTHER) non_trio_flag = FATHER;
        
        ///calculate the longest edge for v and w
		for (i = 0, kv = 0; i < nv; ++i) {
			if (av[i].del) continue;
            kv++; 
            if(get_tip_trio_infor(g, av[i].v) == non_trio_flag) continue;
			if (ov_max < av[i].ol) ov_max = av[i].ol, ov_max_i = i;
            ///kv++; 
		}
		if (kv >= 2 && a->ol > ov_max * drop_ratio) continue;

		for (i = 0, kw = 0; i < nw; ++i) {
			if (aw[i].del) continue;
            kw++;
            if (get_tip_trio_infor(g, aw[i].v) == non_trio_flag) continue;
			if (ow_max < aw[i].ol) ow_max = aw[i].ol, ow_max_i = i;
            ///kw++;
		}
		if (kw >= 2 && a->ol > ow_max * drop_ratio) continue;

        if (kv <= 1 && kw <= 1) continue;

        ///to see which one is the current edge (from v and w)
		for (iv = 0; iv < nv; ++iv)
			if (av[iv].v == (w^1)) break;
		for (iw = 0; iw < nw; ++iw)
			if (aw[iw].v == (v^1)) break;
        ///if one edge has been deleted, it should be deleted in both direction
        if (av[iv].del && aw[iw].del) continue;

        ///kv and kw is the avialiable 
		if (kv > 1 && kw > 1) {
			if (a->ol < ov_max * drop_ratio && a->ol < ow_max * drop_ratio)
				to_del = 1;
            if(if_check_hap == 1 && to_del == 1)
            {
                
                if(check_if_diploid_primary_complex(av[ov_max_i].v, w^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1 
                   || 
                   check_if_diploid_primary_complex(aw[ow_max_i].v, v^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
        
		} else if (kw == 1) {
			if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
            ///kv > 1
            if(if_check_hap == 1 && to_del == 1)
            {
                if(check_if_diploid_primary_complex(av[ov_max_i].v, w^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
		} else if (kv == 1) {
			if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
            ///kw > 1
            if(if_check_hap == 1 && to_del == 1)
            {
                if(check_if_diploid_primary_complex(aw[ow_max_i].v, v^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
		}
		if (to_del)
			av[iv].del = aw[iw].del = 1, ++n_cut;
        
    }
    
    free(b.a);
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %lld short overlaps\n", __func__, n_cut);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
	return n_cut;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %r9d, %ebp
movl %r8d, 0x30(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdx, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movss %xmm0, 0x24(%rsp)
movq %rdi, %rbx
movl 0x100(%rsp), %r14d
callq 0x251c
movl 0x14(%rbx), %esi
addl %esi, %esi
xorl %edi, %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
pushq $0x18
popq %r9
xorl %r12d, %r12d
xorl %ecx, %ecx
movq $0x0, 0x28(%rsp)
movq %rbx, 0x40(%rsp)
movl %ebp, 0x20(%rsp)
movq %rsi, 0x80(%rsp)
cmpq %rsi, %rdi
je 0x72b6c
testl %ebp, %ebp
je 0x72a46
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%rdi)
jne 0x72b3a
testl %r14d, %r14d
je 0x72a5e
movq 0x20(%rbx), %rax
movl %edi, %edx
shrl %edx
cmpb $0x1, 0x4(%rax,%rdx,8)
je 0x72b3a
movq 0x28(%rbx), %rax
movq (%rax,%rdi,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x72b3a
movq %rdi, 0x38(%rsp)
movq 0x8(%rbx), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %r15
movl %eax, %ebx
leaq (%rsi,%rdx), %rbp
addq $0xc, %rbp
movq 0x28(%rsp), %rax
leaq (,%rax,8), %r11
xorl %r13d, %r13d
movq %rbx, 0x88(%rsp)
movq 0x28(%rsp), %rax
addq %r13, %rax
cmpq %r13, %rbx
je 0x72b41
cmpq %rcx, %rax
jne 0x72b0b
movq %r12, %rdi
leaq (%rcx,%rcx), %r12
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %r9, %rbx
movq %r11, %r14
callq 0x2370
movq %r14, %r11
movq %rbx, %r9
movq 0x88(%rsp), %rbx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rax, %rdx
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rsi
movq %r12, %rcx
movq %rdx, %r12
movl (%rbp), %edi
andl %r8d, %edi
shlq $0x20, %rdi
movq %r15, %rax
subq %rsi, %rax
cqto
idivq %r9
addq %r13, %rax
orq %rdi, %rax
leaq (%r12,%r11), %rdx
movq %rax, (%rdx,%r13,8)
incq %r13
addq $0x18, %rbp
jmp 0x72aab
movq 0x28(%rsp), %rax
jmp 0x72b5f
movq 0x40(%rsp), %rbx
movl 0x20(%rsp), %ebp
movl 0x100(%rsp), %r14d
movq 0x80(%rsp), %rsi
movq 0x38(%rsp), %rdi
incq %rdi
movq %rax, 0x28(%rsp)
jmp 0x72a2b
movq 0x28(%rsp), %rax
leaq (%r12,%rax,8), %rsi
movq %r12, %rdi
callq 0x66c25
movq %rbx, %r8
xorl %ebx, %ebx
movq $0x0, 0xa8(%rsp)
movq %r12, 0x10(%rsp)
cmpq 0x28(%rsp), %rbx
je 0x730d4
movq 0x8(%r8), %rsi
movq 0x28(%r8), %rax
movl (%r12,%rbx,8), %ecx
imulq $0x18, %rcx, %rdx
movq (%rsi,%rdx), %r14
shrq $0x20, %r14
movq %rsi, 0x78(%rsp)
movl 0x8(%rsi,%rdx), %ecx
movq %rcx, %r15
xorq $0x1, %r15
movq (%rax,%r14,8), %rsi
movq (%rax,%r15,8), %rax
cmpl $0x1, %esi
jne 0x72bdc
cmpl $0x1, %eax
je 0x73094
movq %rsi, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, %rdi
movl %r14d, %esi
xorl %edx, %edx
movq %r8, %r13
callq 0xc2e7a
movl %eax, %ebp
movq %r13, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
orl %ebp, %eax
cmpl $0x2, %eax
jae 0x72c27
movq %r13, %r8
jmp 0x73094
movq %rbx, 0x18(%rsp)
movq 0x78(%rsp), %r12
addq %r12, 0x70(%rsp)
movq 0x98(%rsp), %rbx
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rbp
leaq (%r12,%rbp), %rax
movq %rax, 0x68(%rsp)
movq 0xa0(%rsp), %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
movq %rax, 0xb0(%rsp)
addq %r12, %rax
movq %rax, 0x58(%rsp)
xorl $0x1, %r14d
movq %r13, %rdi
movq %r14, 0x60(%rsp)
movl %r14d, %esi
callq 0x728c0
xorl %ecx, %ecx
cmpb $0x1, %al
sete %cl
leal (%rcx,%rcx,2), %r15d
decl %r15d
cmpb $0x2, %al
pushq $0x1
popq %rax
cmovel %eax, %r15d
movl %ebx, %eax
movq %rax, 0x88(%rsp)
movq %rbp, 0xc0(%rsp)
leaq (%r12,%rbp), %rbx
addq $0xc, %rbx
xorl %r12d, %r12d
xorl %eax, %eax
xorl %ebp, %ebp
movl $0x0, 0x38(%rsp)
movss 0x24(%rsp), %xmm3
movq %r13, %r8
cmpq %r12, 0x88(%rsp)
je 0x72d28
cmpl $0x0, (%rbx)
js 0x72d1f
incl %eax
movl %eax, %r13d
movl -0x4(%rbx), %esi
movq %r8, %r14
movq %r8, %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, %r15d
je 0x72d13
movl (%rbx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %eax, %ebp
cmovbel %eax, %ebp
movl 0x38(%rsp), %eax
cmovbl %r12d, %eax
movl %eax, 0x38(%rsp)
movss 0x24(%rsp), %xmm3
movl %r13d, %eax
movq %r14, %r8
incq %r12
addq $0x18, %rbx
jmp 0x72ccf
cvtsi2ss %ebp, %xmm4
movl %eax, 0x34(%rsp)
cmpl $0x2, %eax
movq 0xb0(%rsp), %rdx
jb 0x72d60
movq 0x70(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm0
movaps %xmm4, %xmm1
mulss %xmm3, %xmm1
ucomiss %xmm1, %xmm0
ja 0x7308a
movl 0xa0(%rsp), %ebx
movq 0x78(%rsp), %rax
leaq (%rax,%rdx), %r13
addq $0xc, %r13
xorl %ebp, %ebp
xorl %esi, %esi
xorl %r12d, %r12d
movl $0x0, 0x20(%rsp)
movss %xmm4, 0x80(%rsp)
cmpq %rbp, %rbx
je 0x72deb
cmpl $0x0, (%r13)
js 0x72de2
incl %esi
movl %esi, %r14d
movl -0x4(%r13), %esi
movq 0x40(%rsp), %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, %r15d
je 0x72dd0
movl (%r13), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %eax, %r12d
cmovbel %eax, %r12d
movl 0x20(%rsp), %eax
cmovbl %ebp, %eax
movl %eax, 0x20(%rsp)
movss 0x24(%rsp), %xmm3
movss 0x80(%rsp), %xmm4
movl %r14d, %esi
incq %rbp
addq $0x18, %r13
jmp 0x72d8c
cvtsi2ss %r12d, %xmm0
cmpl $0x2, %esi
jb 0x72e2b
movq 0x70(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm1
movaps %xmm0, %xmm2
mulss %xmm3, %xmm2
ucomiss %xmm2, %xmm1
movq 0x40(%rsp), %r8
movq 0x10(%rsp), %r12
movq 0xb8(%rsp), %rbp
jbe 0x72e48
jmp 0x7308f
cmpl $0x2, 0x34(%rsp)
movq 0x40(%rsp), %r8
movq 0x10(%rsp), %r12
movq 0xb8(%rsp), %rbp
jb 0x7308f
movq 0x78(%rsp), %rax
movq 0xc0(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
cmpq %rcx, 0x88(%rsp)
je 0x72e7d
cmpl %ebp, (%rax)
je 0x72e75
incq %rcx
addq $0x18, %rax
jmp 0x72e5e
movq %rcx, 0x98(%rsp)
movq 0x78(%rsp), %rax
movq 0xb0(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
movq 0x60(%rsp), %rdx
cmpq %rcx, %rbx
je 0x72eaa
cmpl %edx, (%rax)
je 0x72eb4
incq %rcx
addq $0x18, %rax
jmp 0x72e98
movq 0xa0(%rsp), %rcx
jmp 0x72eb4
movl 0x98(%rsp), %eax
imulq $0x18, %rax, %r14
movq 0x68(%rsp), %rax
cmpl $0x0, 0xc(%rax,%r14)
movl %ecx, %r15d
jns 0x72ee3
imulq $0x18, %r15, %rax
movq 0x58(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax)
js 0x7308a
cmpl $0x2, %esi
setb %al
cmpl $0x2, 0x34(%rsp)
setb %cl
orb %al, %cl
movl 0x38(%rsp), %r12d
movl 0x20(%rsp), %ebx
jne 0x72f94
movq 0x70(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm1
mulss %xmm3, %xmm4
ucomiss %xmm1, %xmm4
jbe 0x7308a
mulss %xmm3, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x7308a
cmpl $0x1, 0x108(%rsp)
jne 0x73064
imulq $0x18, %r12, %rax
movq 0x68(%rsp), %rcx
movl 0x8(%rcx,%rax), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %ebp, %esi
movq %r8, %rdx
movq 0x50(%rsp), %rcx
movq 0x90(%rsp), %r12
movq %r8, %r13
movq %r12, %r8
movl 0x30(%rsp), %ebp
movl %ebp, %r9d
callq 0x71242
cmpq $0x1, %rax
jne 0x7309c
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbx
jmp 0x72c1f
cmpl $0x1, %esi
jne 0x72fe5
movq %r8, %r13
movq %r8, %rdi
movl %ebp, %esi
movl 0x4c(%rsp), %ebx
movl %ebx, %edx
callq 0x71a0e
cmpl %ebx, %eax
setl %cl
cmpl $0x1, 0x108(%rsp)
jne 0x7305d
cmpl %ebx, %eax
jge 0x7305d
imulq $0x18, %r12, %rax
movq 0x68(%rsp), %rcx
movl 0x8(%rcx,%rax), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %ebp, %esi
jmp 0x73038
cmpl $0x1, 0x34(%rsp)
jne 0x7308a
movq %r8, %r13
movq %r8, %rdi
movq 0x60(%rsp), %rsi
movl 0x4c(%rsp), %ebp
movl %ebp, %edx
callq 0x71a0e
cmpl %ebp, %eax
setl %cl
cmpl $0x1, 0x108(%rsp)
jne 0x7305d
cmpl %ebp, %eax
jge 0x7305d
imulq $0x18, %rbx, %rax
movq 0x58(%rsp), %rcx
movl 0x8(%rcx,%rax), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rsi
movq %r13, %rdx
movq 0x50(%rsp), %rcx
movq 0x90(%rsp), %r8
movl 0x30(%rsp), %r9d
callq 0x71242
movq %r13, %r8
cmpq $0x1, %rax
jne 0x73064
jmp 0x7308a
testb %cl, %cl
movq %r13, %r8
je 0x7308a
movq 0x68(%rsp), %rax
addq %r14, %rax
addq $0xc, %rax
imulq $0x18, %r15, %rcx
movq 0x58(%rsp), %rdx
orb $-0x80, 0xf(%rdx,%rcx)
orb $-0x80, 0x3(%rax)
incq 0xa8(%rsp)
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbx
incq %rbx
jmp 0x72b93
imulq $0x18, %rbx, %rax
movq 0x58(%rsp), %rcx
movl 0x8(%rcx,%rax), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rsi
movq %r13, %rdx
movq 0x50(%rsp), %rcx
movq %r12, %r8
movl %ebp, %r9d
callq 0x71242
cmpq $0x1, %rax
jmp 0x7305f
movq %r8, %r14
movq %r12, %rdi
callq 0x2220
movq 0xa8(%rsp), %rbx
testq %rbx, %rbx
je 0x730fc
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq