// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_exact_trio(asg_t*, int, ma_hit_t_alloc*)
int asg_arc_del_short_diploid_by_exact_trio(asg_t *g, int max_ext, ma_hit_t_alloc* sources)
{
    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(g->seq_vis[v] == 0)
        {
            asg_arc_t *av = asg_arc_a(g, v);
            uint32_t nv = asg_arc_n(g, v);
            if (nv < 2) continue;
            long long 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, 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) 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;
            }
			///++kw;
		}
		if (kw >= 2 && a->ol == ow_max) 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;


        ///if this edge is an inexact edge
        if(a->el == 0 && 
        sources[v>>1].is_fully_corrected == 1 && 
        sources[w>>1].is_fully_corrected == 1)
        {
            if (kv > 1 && kw > 1) {
				to_del = 1;
            } else if (kw == 1) {
                if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
            } else if (kv == 1) {
                if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
            }
        }

        if(a->el == 0 && 
        sources[v>>1].is_fully_corrected == 1 && 
        sources[w>>1].is_fully_corrected == 0)
        {
            /****************************may have bugs********************************/
            ///if(av[ov_max_i].el == 1 && sources[av[ov_max_i].v>>1].is_fully_corrected)
            /****************************may have bugs********************************/
            if(av[ov_max_i].el == 1 && sources[av[ov_max_i].v>>1].is_fully_corrected == 1)
            {
                if (kv > 1 && kw > 1) {
                    to_del = 1;
                } else if (kw == 1) {
                    if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
                } else if (kv == 1) {
                    if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
                }
            }
        }


        
		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 inexact 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 $0xa8, %rsp
movq %rdx, 0xa0(%rsp)
movl %esi, 0x4c(%rsp)
movq %rdi, %rbx
callq 0x300fc
movq %rbx, 0x20(%rsp)
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xaeff7
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r9 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %eax, %eax
xorl %edi, %edi
movq 0x20(%rsp), %rcx
movq 0x30(%rcx), %rcx
cmpb $0x0, (%rcx,%rdx)
movq %rdx, 0x8(%rsp)
je 0xaef3c
movq 0x8(%rsp), %rdx
incq %rdx
cmpq 0x30(%rsp), %rdx
jne 0xaef14
jmp 0xaeffc
movq 0x20(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaef28
movq %rcx, %r13
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r13
je 0xaef28
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r15
leaq (%rdx,%rcx,8), %rbp
addq $0xc, %rbp
xorl %ebx, %ebx
cmpq %rax, %r12
jne 0xaefbb
movq %r12, 0x38(%rsp)
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r9, %r12
callq 0x2370
movq %r12, %r9
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r14, %rax
movq 0x38(%rsp), %r12
movl (%rbp), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %r15, %rdx
movq 0x20(%rsp), %rsi
subq 0x8(%rsi), %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
addq %rbx, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %rbx
addq $0x18, %rbp
cmpq %rbx, %r13
jne 0xaef7d
jmp 0xaef28
xorl %edi, %edi
xorl %r12d, %r12d
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x9d53f
testq %r12, %r12
je 0xaf5d8
movq $0x0, 0x58(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movq %r12, 0x38(%rsp)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x28(%rax), %rax
movl (%rdi,%r13,8), %ecx
leaq (%rcx,%rcx,2), %r14
movq (%rsi,%r14,8), %rdx
movq %rdx, %rbp
shrq $0x20, %rbp
movq %rsi, 0x28(%rsp)
movl 0x8(%rsi,%r14,8), %ebx
movq %rbx, %rcx
xorq $0x1, %rcx
movq (%rax,%rbp,8), %rsi
movq (%rax,%rcx,8), %r10
movq %rsi, 0x90(%rsp)
cmpl $0x1, %esi
jne 0xaf07f
cmpl $0x1, %r10d
je 0xaf5ca
movq %rdx, 0x80(%rsp)
movq (%rax,%rbp,8), %rdx
xorl %eax, %eax
movq %rdx, %rsi
movl $0x0, %ecx
andq %r15, %rsi
je 0xaf0d3
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r9d, %r9d
xorl %ecx, %ecx
movl (%rdx,%r9), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %ecx
addq $0x18, %r9
cmpq %r9, %rsi
jne 0xaf0bc
movq %r10, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r11
movq %r10, %rdx
andq %r15, %rdx
movq %rdx, 0x88(%rsp)
je 0xaf12b
movq 0x28(%rsp), %rax
leaq (%rax,%r11,8), %rdx
addq $0xc, %rdx
movq 0x88(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
xorl %r9d, %r9d
xorl %eax, %eax
movl (%rdx,%r9), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %r9
cmpq %r9, %rsi
jne 0xaf114
orl %ecx, %eax
cmpl $0x2, %eax
jb 0xaf5ca
movq %r14, 0x40(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x68(%rsp)
movq %r13, 0x10(%rsp)
movq 0x90(%rsp), %r14
movq %r14, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r13
xorl $0x1, %ebp
movq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0xab815
xorl %ecx, %ecx
cmpb $0x1, %al
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpb $0x2, %al
movl $0x1, %eax
cmovel %eax, %ecx
movl %ecx, 0x30(%rsp)
testl %r14d, %r14d
movq %rbx, 0x60(%rsp)
movq %rbp, 0x78(%rsp)
movq %r13, 0x70(%rsp)
je 0xaf20f
movl %r14d, %r15d
movq 0x28(%rsp), %rax
leaq (%rax,%r13,8), %rbp
addq $0xc, %rbp
xorl %ebx, %ebx
movl $0x0, 0x18(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x8(%rsp), %rdi
movl (%rbp), %r12d
testl %r12d, %r12d
js 0xaf1f8
incl %r14d
movl -0x4(%rbp), %esi
movq 0x20(%rsp), %rdi
callq 0xab815
movzbl %al, %eax
cmpl %eax, 0x30(%rsp)
je 0xaf1f3
cmpl %r12d, %r13d
cmovbel %r12d, %r13d
movl 0x18(%rsp), %eax
cmovbl %ebx, %eax
movl %eax, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %r12
incq %rbx
addq $0x18, %rbp
cmpq %rbx, %r15
jne 0xaf1bf
movl 0x18(%rsp), %edx
jmp 0xaf21c
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %edx, %edx
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
cmpl $0x2, %r14d
jb 0xaf243
movl 0xc(%rsi), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %r13d, %eax
je 0xaf5bf
movq %rsi, 0x18(%rsp)
movq %rdx, 0x98(%rsp)
movq %r14, 0x40(%rsp)
cmpl $0x0, 0x68(%rsp)
je 0xaf2c6
movq 0x28(%rsp), %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx,8), %r15
addq $0xc, %r15
movq 0x88(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r10d, %r10d
movl (%r15,%rbp), %r13d
testl %r13d, %r13d
js 0xaf2bb
incl %r10d
movl %r10d, %r14d
movl -0x4(%r15,%rbp), %esi
movq 0x20(%rsp), %rdi
callq 0xab815
movzbl %al, %eax
cmpl %eax, 0x30(%rsp)
je 0xaf2b3
cmpl %r13d, %ebx
cmovbel %r13d, %ebx
movq 0x8(%rsp), %rdi
movl %r14d, %r10d
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0xaf285
jmp 0xaf2cb
xorl %r10d, %r10d
xorl %ebx, %ebx
cmpl $0x2, %r10d
movq 0xa0(%rsp), %r13
movq 0x38(%rsp), %r12
jb 0xaf310
movq 0x18(%rsp), %r11
movl 0xc(%r11), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %ebx, %eax
movq 0x78(%rsp), %rbp
je 0xaf5bf
movq 0x40(%rsp), %r9
movl %r9d, %eax
orl %r10d, %eax
cmpl $0x2, %eax
jae 0xaf32e
jmp 0xaf5bf
movq 0x40(%rsp), %r9
movl %r9d, %eax
orl %r10d, %eax
cmpl $0x2, %eax
movq 0x78(%rsp), %rbp
movq 0x18(%rsp), %r11
jb 0xaf5bf
movq 0x90(%rsp), %rdx
testl %edx, %edx
je 0xaf36d
movq 0x28(%rsp), %rax
movq 0x70(%rsp), %rcx
leaq (%rax,%rcx,8), %rcx
addq $0x8, %rcx
xorl %eax, %eax
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %rsi
cmpl %r8d, (%rcx)
je 0xaf369
incl %eax
addq $0x18, %rcx
cmpl %eax, %edx
jne 0xaf358
movl %edx, %eax
movl %eax, %eax
jmp 0xaf379
xorl %eax, %eax
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %rsi
testl %esi, %esi
je 0xaf3a6
movq 0x28(%rsp), %rcx
movq 0x50(%rsp), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
cmpl %ebp, (%rdx)
je 0xaf3a1
incl %ecx
addq $0x18, %rdx
cmpl %ecx, %esi
jne 0xaf391
movl %esi, %ecx
movl %ecx, %r14d
jmp 0xaf3a9
xorl %r14d, %r14d
movq 0x50(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x70(%rsp), %rsi
leaq (%rcx,%rsi,8), %rsi
leaq (%rcx,%rdx,8), %rbx
movq %rsi, %rdx
leaq (%rax,%rax,2), %rcx
cmpl $0x0, 0xc(%rsi,%rcx,8)
jns 0xaf3dd
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rbx,%rax,8)
js 0xaf5ba
movb 0x12(%r11), %r15b
xorl %r12d, %r12d
testb %r15b, %r15b
jne 0xaf4a4
movq 0x80(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%r13,%rax,8)
jne 0xaf4a4
movl %r8d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%r13,%rax,8)
jne 0xaf4a4
cmpl $0x2, %r10d
setae %al
cmpl $0x2, %r9d
movq %rcx, 0x30(%rsp)
setae %cl
movl $0x1, %r12d
testb %al, %cl
movq 0x30(%rsp), %rcx
jne 0xaf4a4
movl %r8d, %esi
cmpl $0x1, %r10d
je 0xaf457
movl %ebp, %esi
movl $0x0, %r12d
cmpl $0x1, %r9d
jne 0xaf4a4
movq 0x20(%rsp), %rdi
movq %rbx, 0x28(%rsp)
movl 0x4c(%rsp), %ebx
movq %rdx, 0x18(%rsp)
movl %ebx, %edx
movq %r14, %r12
movl %r10d, %r14d
callq 0xaa77e
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %r14d, %r10d
movq %r12, %r14
movq 0x8(%rsp), %rdi
xorl %r12d, %r12d
cmpl %ebx, %eax
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r9
movq 0x60(%rsp), %r8
setl %r12b
testb %r15b, %r15b
jne 0xaf587
movq 0x80(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%r13,%rax,8)
jne 0xaf587
movl %r8d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x0, 0x10(%r13,%rax,8)
jne 0xaf587
movq 0x98(%rsp), %rax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rdx,%rax,8)
jne 0xaf587
leaq (%rdx,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%r13,%rax,8)
jne 0xaf587
cmpl $0x2, %r10d
movq %rcx, 0x30(%rsp)
setae %cl
cmpl $0x2, %r9d
movq %rdx, 0x18(%rsp)
setae %dl
movl $0x1, %eax
testb %cl, %dl
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
jne 0xaf595
cmpl $0x1, %r10d
je 0xaf54f
movl %ebp, %r8d
cmpl $0x1, %r9d
jne 0xaf582
movq 0x20(%rsp), %rdi
movl %r8d, %esi
movq %rbx, %rbp
movl 0x4c(%rsp), %ebx
movl %ebx, %edx
callq 0xaa77e
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %rdi
cmpl %ebx, %eax
movq %rbp, %rbx
movl $0x1, %eax
cmovll %eax, %r12d
movl %r12d, %eax
jmp 0xaf595
movl %r12d, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
testl %eax, %eax
je 0xaf5b3
leaq (%rdx,%rcx,8), %rax
addq $0xc, %rax
leaq (%r14,%r14,2), %rcx
orb $-0x80, 0xf(%rbx,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x58(%rsp)
movq 0x38(%rsp), %r12
jmp 0xaf5ca
movq 0x38(%rsp), %r12
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
incq %r13
cmpq %r12, %r13
jne 0xaf02f
jmp 0xaf5e6
movq $0x0, 0x58(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x58(%rsp), %rbx
testq %rbx, %rbx
je 0xaf617
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xa02fa
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq