// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movl %esi, %r14d
movq %rdi, %rbp
callq 0x2f3fc
movl 0x14(%rbp), %eax
addl %eax, %eax
je 0xb03e7
movl %r14d, 0x54(%rsp)
movq %rbx, 0x88(%rsp)
movl %eax, %eax
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %edi, %edi
movq %rbp, 0x8(%rsp)
movq 0x30(%rbp), %rax
cmpb $0x0, (%rax,%rcx)
movq %rcx, 0x28(%rsp)
je 0xafd51
movq 0x28(%rsp), %rcx
incq %rcx
cmpq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbp
jne 0xafd29
jmp 0xafdf1
movq 0x28(%rbp), %rax
movq (%rax,%rcx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xafd38
movq %rcx, %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r13
je 0xafd38
movq 0x8(%rbp), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r12
xorl %ebp, %ebp
cmpq %rbx, %r14
jne 0xafdb3
leaq (%r14,%r14), %rbx
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
callq 0x2370
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movl 0xc(%r12,%rcx,8), %ecx
andl %esi, %ecx
shlq $0x20, %rcx
movq %r12, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %rbp, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r14,8)
incq %r14
incq %rbp
cmpq %r13, %rbp
jne 0xafd80
jmp 0xafd38
leaq (%rdi,%r14,8), %rsi
movq %rdi, 0x10(%rsp)
callq 0x9dc69
testq %r14, %r14
je 0xb03f4
movq $0x0, 0x98(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r11d, %r11d
movq 0x10(%rsp), %rdi
movq 0x8(%rbp), %rdx
movq 0x28(%rbp), %rcx
movl (%rdi,%r11,8), %eax
leaq (%rax,%rax,2), %rax
movq (%rdx,%rax,8), %r15
movq %r15, 0xa0(%rsp)
shrq $0x20, %r15
movq %rdx, 0x20(%rsp)
movl 0x8(%rdx,%rax,8), %ebx
movq %rbx, %rdx
xorq $0x1, %rdx
movq (%rcx,%r15,8), %rsi
movq (%rcx,%rdx,8), %rcx
movq %rsi, 0x80(%rsp)
cmpl $0x1, %esi
jne 0xafe70
cmpl $0x1, %ecx
je 0xb03ab
movq %rcx, 0x68(%rsp)
movq 0x80(%rsp), %rdx
movq %rdx, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r12
xorl %ecx, %ecx
movq %rdx, %rsi
movl $0x0, %edx
andq %r13, %rsi
movq %rsi, 0x28(%rsp)
je 0xafed6
movq 0x20(%rsp), %rdx
leaq (%rdx,%r12,8), %rsi
addq $0xc, %rsi
movq 0x28(%rsp), %rdx
leaq (,%rdx,8), %rdx
leaq (%rdx,%rdx,2), %r10
xorl %r8d, %r8d
xorl %edx, %edx
movl (%rsi,%r8), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %edx
addq $0x18, %r8
cmpq %r8, %r10
jne 0xafebf
movq 0x68(%rsp), %r8
movq %r8, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
movq %rsi, 0x78(%rsp)
andq %r13, %r8
movq %r8, 0x60(%rsp)
je 0xaff34
movq 0x20(%rsp), %rcx
movq 0x78(%rsp), %rsi
leaq (%rcx,%rsi,8), %rsi
addq $0xc, %rsi
movq 0x60(%rsp), %rcx
leaq (,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %r10
xorl %r8d, %r8d
xorl %ecx, %ecx
movl (%rsi,%r8), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %ecx
addq $0x18, %r8
cmpq %r8, %r10
jne 0xaff1d
orl %edx, %ecx
cmpl $0x2, %ecx
jb 0xb03ab
movq %r12, 0x58(%rsp)
movq %r11, 0x40(%rsp)
movq 0x20(%rsp), %r12
leaq (%r12,%rax,8), %rax
movq %rax, 0x90(%rsp)
xorl $0x1, %r15d
movq %rbp, %rdi
movl %r15d, %esi
callq 0xac87a
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, 0x38(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x0, 0x80(%rsp)
movq %rbx, 0x48(%rsp)
movq %r15, 0x70(%rsp)
je 0xb0065
movq 0x58(%rsp), %rax
leaq (%r12,%rax,8), %r15
addq $0xc, %r15
xorl %r13d, %r13d
movl $0x0, 0x34(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rdi
movq %r12, %rax
movl (%r15), %r12d
testl %r12d, %r12d
js 0xb000b
incl 0x1c(%rsp)
movl -0x4(%r15), %esi
movq %rbp, %rdi
callq 0xac87a
movzbl %al, %eax
cmpl %eax, 0x38(%rsp)
je 0xb0001
cmpl %r12d, %ebx
cmovbel %r12d, %ebx
movl 0x34(%rsp), %eax
cmovbl %r13d, %eax
movl %eax, 0x34(%rsp)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rax
incq %r13
addq $0x18, %r15
cmpq %r13, 0x28(%rsp)
jne 0xaffcd
cmpl $0x2, 0x1c(%rsp)
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
jb 0xb0096
movq 0x90(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movb $0x1, %cl
movl %ecx, 0x30(%rsp)
cmpl %ebx, %eax
movq 0x20(%rsp), %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x70(%rsp), %r15
movq 0x48(%rsp), %rbx
movq 0x58(%rsp), %rsi
jne 0xb00b3
jmp 0xb03a6
movl $0x0, 0x30(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rdi
movq %r12, %rax
movq 0x58(%rsp), %rsi
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
jmp 0xb00b3
movl $0x0, 0x30(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x70(%rsp), %r15
movq 0x48(%rsp), %rbx
movq 0x58(%rsp), %rsi
testl %r8d, %r8d
je 0xb0162
movq 0x78(%rsp), %rcx
leaq (%rax,%rcx,8), %r15
addq $0xc, %r15
leaq (,%r9,8), %rax
leaq (%rax,%rax,2), %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
movl $0x0, 0x18(%rsp)
movl (%r15,%r12), %ebp
testl %ebp, %ebp
js 0xb0110
incl 0x18(%rsp)
movl -0x4(%r15,%r12), %esi
movq 0x8(%rsp), %rdi
callq 0xac87a
movzbl %al, %eax
cmpl %eax, 0x38(%rsp)
je 0xb010b
cmpl %ebp, %ebx
cmovbel %ebp, %ebx
movq 0x10(%rsp), %rdi
addq $0x18, %r12
cmpq %r12, %r13
jne 0xb00e2
cmpl $0x2, 0x18(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x58(%rsp), %rsi
jb 0xb0167
movq 0x90(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movb $0x1, %r10b
cmpl %ebx, %eax
movq 0x8(%rsp), %rbp
movq 0x70(%rsp), %r15
movq 0x48(%rsp), %rbx
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
jne 0xb0183
jmp 0xb03a6
xorl %r10d, %r10d
jmp 0xb0183
xorl %r10d, %r10d
movq 0x8(%rsp), %rbp
movq 0x70(%rsp), %r15
movq 0x48(%rsp), %rbx
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
movl 0x1c(%rsp), %eax
orl 0x18(%rsp), %eax
cmpl $0x2, %eax
movq 0x40(%rsp), %r11
jb 0xb03ab
cmpl $0x0, 0x80(%rsp)
je 0xb01d0
movq 0x20(%rsp), %rax
leaq (%rax,%rsi,8), %rcx
addq $0x8, %rcx
xorl %eax, %eax
cmpl %ebx, (%rcx)
je 0xb01cc
incq %rax
addq $0x18, %rcx
cmpq %rax, 0x28(%rsp)
jne 0xb01b2
movq 0x80(%rsp), %rax
movl %eax, %eax
jmp 0xb01d2
xorl %eax, %eax
testl %r8d, %r8d
je 0xb0204
movq 0x20(%rsp), %rcx
movq 0x78(%rsp), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
cmpl %r15d, (%rdx)
je 0xb01ff
incq %rcx
addq $0x18, %rdx
cmpq %rcx, %r9
jne 0xb01eb
movq %r8, %rcx
movl %ecx, %r8d
jmp 0xb0207
xorl %r8d, %r8d
movq 0x20(%rsp), %rcx
leaq (%rcx,%rsi,8), %r12
movq 0x78(%rsp), %rdx
leaq (%rcx,%rdx,8), %rdx
leaq (%rax,%rax,2), %rbp
cmpl $0x0, 0xc(%r12,%rbp,8)
jns 0xb0234
leaq (%r8,%r8,2), %rax
cmpl $0x0, 0xc(%rdx,%rax,8)
js 0xb02cf
movq 0x90(%rsp), %rax
cmpb $0x0, 0x12(%rax)
jne 0xb02cf
movq 0xa0(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x88(%rsp), %rcx
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xb02cf
movl %ebx, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xb02d9
movb $0x1, %bl
testb %r10b, 0x30(%rsp)
jne 0xb02db
movq 0x48(%rsp), %rax
movl %eax, %esi
cmpl $0x1, 0x18(%rsp)
je 0xb0295
movl %r15d, %esi
cmpl $0x1, 0x1c(%rsp)
jne 0xb02d9
movq 0x8(%rsp), %rdi
movl 0x54(%rsp), %ebx
movq %rdx, 0x38(%rsp)
movl %ebx, %edx
movl %r10d, 0x20(%rsp)
movq %r8, 0x28(%rsp)
callq 0xab75d
movq 0x38(%rsp), %rdx
movq 0x28(%rsp), %r8
movl 0x20(%rsp), %r10d
movq 0x10(%rsp), %rdi
cmpl %ebx, %eax
setl %bl
jmp 0xb02db
movq 0x8(%rsp), %rbp
jmp 0xb03ab
xorl %ebx, %ebx
movq 0x48(%rsp), %rax
shrl %eax
leaq (%rax,%rax,2), %rax
movq 0x88(%rsp), %rcx
cmpb $0x0, 0x10(%rcx,%rax,8)
jne 0xb0380
movl 0x34(%rsp), %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%r12,%rax,8)
jne 0xb0380
leaq (%r12,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
leaq (%rax,%rax,2), %rax
movq 0x88(%rsp), %rcx
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xb0380
testb %r10b, 0x30(%rsp)
jne 0xb0384
cmpl $0x1, 0x18(%rsp)
jne 0xb033f
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rsi
jmp 0xb034e
cmpl $0x1, 0x1c(%rsp)
jne 0xb0380
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl 0x54(%rsp), %r15d
movq %rdx, 0x38(%rsp)
movl %r15d, %edx
movq %r8, 0x28(%rsp)
callq 0xab75d
movq 0x38(%rsp), %rdx
movq 0x28(%rsp), %r8
movq 0x10(%rsp), %rdi
cmpl %r15d, %eax
setl %al
orb %al, %bl
jne 0xb0384
jmp 0xb03a1
testb %bl, %bl
je 0xb03a1
leaq (%r12,%rbp,8), %rax
addq $0xc, %rax
leaq (%r8,%r8,2), %rcx
orb $-0x80, 0xf(%rdx,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x98(%rsp)
movq 0x8(%rsp), %rbp
movq 0x40(%rsp), %r11
incq %r11
cmpq %r14, %r11
jne 0xafe22
callq 0x2220
movq 0x98(%rsp), %rbx
testq %rbx, %rbx
je 0xb03fe
movq %rbp, %rdi
callq 0xa0a7d
movq %rbp, %rdi
callq 0xa0ab6
movq %rbp, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbp)
jmp 0xb0400
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xb03f9
movq 0x10(%rsp), %rdi
callq 0x2220
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq