// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, 0x80(%rsp)
movl %esi, 0x3c(%rsp)
movq %rdi, %rbp
callq 0x251c
movl 0x14(%rbp), %edx
addl %edx, %edx
xorl %esi, %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
pushq $0x18
popq %r13
xorl %r12d, %r12d
xorl %ecx, %ecx
movq $0x0, 0x30(%rsp)
movq %rbp, 0x20(%rsp)
movq %rdx, 0x40(%rsp)
cmpq %rdx, %rsi
je 0x75ee9
movq 0x30(%rbp), %rax
cmpb $0x0, (%rax,%rsi)
je 0x75dfb
movq 0x30(%rsp), %rax
jmp 0x75edc
movq 0x28(%rbp), %rax
movq (%rax,%rsi,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x75df1
movq %rsi, 0x18(%rsp)
movq 0x8(%rbp), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %r10
movl %eax, %r15d
leaq (%rsi,%rdx), %r14
addq $0xc, %r14
movq 0x30(%rsp), %rax
leaq (,%rax,8), %rbp
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
addq %rbx, %rax
cmpq %rbx, %r15
je 0x75ecd
cmpq %rcx, %rax
jne 0x75e9e
movq %r12, %rdi
leaq (%rcx,%rcx), %r12
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %r14, 0x28(%rsp)
movq %r15, %r14
movq %r10, %r15
callq 0x2370
movq %r15, %r10
movq %r14, %r15
movq 0x28(%rsp), %r14
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rax, %rdx
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rsi
movq %r12, %rcx
movq %rdx, %r12
movl (%r14), %edi
andl %r8d, %edi
shlq $0x20, %rdi
movq %r10, %rax
subq %rsi, %rax
cqto
idivq %r13
addq %rbx, %rax
orq %rdi, %rax
leaq (%r12,%rbp), %rdx
movq %rax, (%rdx,%rbx,8)
incq %rbx
addq $0x18, %r14
jmp 0x75e3c
movq 0x20(%rsp), %rbp
movq 0x40(%rsp), %rdx
movq 0x18(%rsp), %rsi
incq %rsi
movq %rax, 0x30(%rsp)
jmp 0x75dde
movq 0x30(%rsp), %rax
leaq (%r12,%rax,8), %rsi
movq %r12, %rdi
callq 0x66c25
xorl %ebx, %ebx
movq $0x0, 0x78(%rsp)
movq %r12, 0x8(%rsp)
cmpq 0x30(%rsp), %rbx
je 0x7635c
movq 0x8(%rbp), %rdx
movq 0x28(%rbp), %rax
movl (%r12,%rbx,8), %ecx
imulq $0x18, %rcx, %rcx
movq (%rdx,%rcx), %r13
movq %r13, %r14
shrq $0x20, %r14
movq %rcx, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
movl 0x8(%rdx,%rcx), %r15d
movq %r15, %rbp
xorq $0x1, %rbp
movq (%rax,%r14,8), %rcx
movq (%rax,%rbp,8), %rax
movq %rcx, 0x70(%rsp)
cmpl $0x1, %ecx
jne 0x75f67
cmpl $0x1, %eax
jne 0x75f67
movq 0x20(%rsp), %rbp
jmp 0x76354
movq %rax, 0x68(%rsp)
movq 0x20(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
movl %eax, 0x28(%rsp)
movq %r12, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
orl 0x28(%rsp), %eax
cmpl $0x2, %eax
jae 0x75fa6
movq 0x20(%rsp), %rbp
movq 0x8(%rsp), %r12
jmp 0x76354
movq %r13, 0xa0(%rsp)
movq %r15, 0x60(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x58(%rsp), %r15
addq %r15, 0x50(%rsp)
movq 0x70(%rsp), %rbx
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %r12
leaq (%r15,%r12), %rax
movq %rax, 0x48(%rsp)
movq 0x68(%rsp), %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
movq %rax, 0x98(%rsp)
addq %r15, %rax
movq %rax, 0x88(%rsp)
xorl $0x1, %r14d
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r14, 0x90(%rsp)
movl %r14d, %esi
callq 0x728c0
xorl %ecx, %ecx
cmpb $0x1, %al
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpb $0x2, %al
pushq $0x1
popq %rax
cmovel %eax, %ecx
movl %ecx, 0x28(%rsp)
movl %ebx, %r14d
movq %r12, 0xb0(%rsp)
leaq (%r15,%r12), %rbx
addq $0xc, %rbx
xorl %r13d, %r13d
movl $0x0, 0x18(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x40(%rsp)
cmpq %r13, %r14
je 0x7609f
cmpl $0x0, (%rbx)
js 0x76096
incl 0x18(%rsp)
movl -0x4(%rbx), %esi
movq %rbp, %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, 0x28(%rsp)
je 0x76096
movl (%rbx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %eax, %r15d
cmovbel %eax, %r15d
movl 0x40(%rsp), %eax
cmovbl %r13d, %eax
movl %eax, 0x40(%rsp)
incq %r13
addq $0x18, %rbx
jmp 0x76058
cmpl $0x2, 0x18(%rsp)
jb 0x760be
movq 0x50(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %r15d, %eax
je 0x7634a
movl 0x68(%rsp), %eax
movq %rax, 0xa8(%rsp)
imulq $0x18, %rax, %r15
movq 0x58(%rsp), %rax
movq 0x98(%rsp), %rcx
leaq (%rax,%rcx), %r13
addq $0xc, %r13
xorl %ebp, %ebp
xorl %edi, %edi
xorl %ebx, %ebx
cmpq %rbp, %r15
je 0x7612d
cmpl $0x0, (%r13,%rbp)
js 0x76127
incl %edi
movl %edi, %r12d
movl -0x4(%r13,%rbp), %esi
movq 0x20(%rsp), %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, 0x28(%rsp)
je 0x76124
movl (%r13,%rbp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %eax, %ebx
cmovbel %eax, %ebx
movl %r12d, %edi
addq $0x18, %rbp
jmp 0x760e9
cmpl $0x2, %edi
jb 0x76164
movq 0x50(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cmpl %ebx, %eax
movq 0x20(%rsp), %rbp
movq 0x80(%rsp), %rbx
movq 0x90(%rsp), %r13
movq 0x60(%rsp), %rdx
jne 0x76189
jmp 0x7634a
cmpl $0x2, 0x18(%rsp)
movq 0x20(%rsp), %rbp
movq 0x80(%rsp), %rbx
movq 0x90(%rsp), %r13
movq 0x60(%rsp), %rdx
jb 0x7634a
movq 0x58(%rsp), %rax
movq 0xb0(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x761b6
cmpl %edx, (%rax)
je 0x761b1
incq %rcx
addq $0x18, %rax
jmp 0x7619f
movq %rcx, 0x70(%rsp)
movq 0x58(%rsp), %rax
movq 0x98(%rsp), %rcx
addq %rcx, %rax
addq $0x8, %rax
xorl %ecx, %ecx
movq 0x68(%rsp), %rsi
movq 0xa8(%rsp), %r8
cmpq %rcx, %r8
je 0x761ee
cmpl %r13d, (%rax)
je 0x761ec
incq %rcx
addq $0x18, %rax
jmp 0x761d9
movl %ecx, %esi
movl 0x70(%rsp), %eax
imulq $0x18, %rax, %r14
movq 0x48(%rsp), %rax
cmpl $0x0, 0xc(%rax,%r14)
movl %esi, %r12d
jns 0x7621d
imulq $0x18, %r12, %rax
movq 0x88(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax)
js 0x7634a
movq 0x50(%rsp), %rax
cmpb $0x0, 0x12(%rax)
jne 0x7634a
movq 0xa0(%rsp), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x10(%rbx,%rax)
jne 0x7634a
movl %edx, %eax
shrl %eax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x10(%rbx,%rax)
jne 0x762a2
cmpl $0x2, %edi
setae %al
cmpl $0x2, 0x18(%rsp)
setae %cl
movb $0x1, %r15b
testb %al, %cl
jne 0x762a5
movl %edx, %esi
cmpl $0x1, %edi
je 0x7627c
movl %r13d, %esi
cmpl $0x1, 0x18(%rsp)
jne 0x762a2
movl %edi, 0x28(%rsp)
movq %rbp, %rdi
movl 0x3c(%rsp), %r15d
movl %r15d, %edx
callq 0x71a0e
movl 0x28(%rsp), %edi
cmpl %r15d, %eax
movq 0x60(%rsp), %rdx
setl %r15b
jmp 0x762a5
xorl %r15d, %r15d
movl %edx, %eax
shrl %eax
imulq $0x18, %rax, %rax
cmpb $0x0, 0x10(%rbx,%rax)
jne 0x7631f
movl 0x40(%rsp), %eax
imulq $0x18, %rax, %rax
movq 0x48(%rsp), %rcx
cmpb $0x1, 0x12(%rcx,%rax)
jne 0x7631f
addq 0x48(%rsp), %rax
movl 0x8(%rax), %eax
shrl %eax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x10(%rbx,%rax)
jne 0x7631f
cmpl $0x2, %edi
setae %al
cmpl $0x2, 0x18(%rsp)
setae %cl
testb %al, %cl
jne 0x76324
cmpl $0x1, %edi
jne 0x762fb
movq %rbp, %rdi
movl %edx, %esi
jmp 0x76308
cmpl $0x1, 0x18(%rsp)
jne 0x7631f
movq %rbp, %rdi
movl %r13d, %esi
movl 0x3c(%rsp), %ebx
movl %ebx, %edx
callq 0x71a0e
cmpl %ebx, %eax
setl %al
orb %al, %r15b
jne 0x76324
jmp 0x7634a
testb %r15b, %r15b
je 0x7634a
movq 0x48(%rsp), %rax
addq %r14, %rax
addq $0xc, %rax
imulq $0x18, %r12, %rcx
movq 0x88(%rsp), %rdx
orb $-0x80, 0xf(%rdx,%rcx)
orb $-0x80, 0x3(%rax)
incq 0x78(%rsp)
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %rbx
incq %rbx
jmp 0x75f0a
movq %r12, %rdi
callq 0x2220
movq 0x78(%rsp), %rbx
testq %rbx, %rbx
je 0x7637e
movq %rbp, %rdi
callq 0x694eb
movq %rbp, %rdi
callq 0x69638
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq