// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
callq 0x2510
movsd %xmm0, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0xe4084
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xe4074
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x54(%rbp)
cmpl $0x2, -0x54(%rbp)
jae 0xe3f97
jmp 0xe4076
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jge 0xe4072
jmp 0xe3fb1
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0xe4000
cmpq $0x0, -0x30(%rbp)
je 0xe3fd2
movq -0x30(%rbp), %rax
shlq %rax
movq %rax, -0xc8(%rbp)
jmp 0xe3fe0
movl $0x2, %eax
movq %rax, -0xc8(%rbp)
jmp 0xe3fe0
movq -0xc8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0xd0(%rbp)
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movl $0x18, %ecx
cqto
idivq %rcx
movq -0xd0(%rbp), %rdx
addq -0x60(%rbp), %rax
orq %rax, %rdx
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x38(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xe4061
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xe3f9f
jmp 0xe4074
jmp 0xe4076
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xe3f30
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0xc90e0
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0xe4757
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x80(%rbp)
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x80(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x8c(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x84(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x90(%rbp)
movl $0x0, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
cmpl $0x1, -0x8c(%rbp)
jne 0xe415d
cmpl $0x1, -0x90(%rbp)
jne 0xe415d
jmp 0xe4746
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x80(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x84(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x94(%rbp)
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x98(%rbp)
cmpl $0x1, -0x94(%rbp)
ja 0xe41f8
cmpl $0x1, -0x98(%rbp)
ja 0xe41f8
jmp 0xe4746
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %esi
xorl $0x1, %esi
callq 0xde900
movzbl %al, %eax
movl %eax, -0xa8(%rbp)
movl $0xffffffff, -0xac(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0xa8(%rbp)
jne 0xe422d
movl $0x2, -0xac(%rbp)
cmpl $0x2, -0xa8(%rbp)
jne 0xe4240
movl $0x1, -0xac(%rbp)
movl $0x0, -0x74(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0xe4316
movq -0xb8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xe4281
jmp 0xe4308
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movq -0x8(%rbp), %rdi
movq -0xb8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
callq 0xde900
movzbl %al, %eax
cmpl -0xac(%rbp), %eax
jne 0xe42ba
jmp 0xe4308
movl -0x9c(%rbp), %eax
movq -0xb8(%rbp), %rcx
movl -0x74(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xe4306
movq -0xb8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x9c(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0xe4308
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xe4251
cmpl $0x2, -0x94(%rbp)
jb 0xe4338
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x9c(%rbp), %eax
jne 0xe4338
jmp 0xe4746
movl $0x0, -0x74(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0xe4402
movq -0xc0(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xe4376
jmp 0xe43f4
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
callq 0xde900
movzbl %al, %eax
cmpl -0xac(%rbp), %eax
jne 0xe43af
jmp 0xe43f4
movl -0xa0(%rbp), %eax
movq -0xc0(%rbp), %rcx
movl -0x74(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xe43f2
movq -0xc0(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xa0(%rbp)
jmp 0xe43f4
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xe4349
cmpl $0x2, -0x98(%rbp)
jb 0xe4424
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0xa0(%rbp), %eax
jne 0xe4424
jmp 0xe4746
cmpl $0x1, -0x94(%rbp)
ja 0xe443b
cmpl $0x1, -0x98(%rbp)
ja 0xe443b
jmp 0xe4746
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0xe447d
movq -0xb8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x84(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0xe4470
jmp 0xe447d
jmp 0xe4472
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xe4442
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0xe44bc
movq -0xc0(%rbp), %rax
movl -0x7c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x80(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0xe44af
jmp 0xe44bc
jmp 0xe44b1
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xe4484
movq -0xb8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xe44f9
movq -0xc0(%rbp), %rax
movl -0x7c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xe44f9
jmp 0xe4746
movq -0x70(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xe45c6
movq -0x18(%rbp), %rax
movl -0x80(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0xe45c6
movq -0x18(%rbp), %rax
movl -0x84(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0xe45c6
cmpl $0x1, -0x94(%rbp)
jbe 0xe4565
cmpl $0x1, -0x98(%rbp)
jbe 0xe4565
movl $0x1, -0x88(%rbp)
jmp 0xe45c4
cmpl $0x1, -0x98(%rbp)
jne 0xe4594
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
xorl $0x1, %esi
movl -0xc(%rbp), %edx
callq 0xdd220
cmpl -0xc(%rbp), %eax
jge 0xe4592
movl $0x1, -0x88(%rbp)
jmp 0xe45c2
cmpl $0x1, -0x94(%rbp)
jne 0xe45c0
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %esi
xorl $0x1, %esi
movl -0xc(%rbp), %edx
callq 0xdd220
cmpl -0xc(%rbp), %eax
jge 0xe45be
movl $0x1, -0x88(%rbp)
jmp 0xe45c0
jmp 0xe45c2
jmp 0xe45c4
jmp 0xe45c6
movq -0x70(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xe46e9
movq -0x18(%rbp), %rax
movl -0x80(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0xe46e9
movq -0x18(%rbp), %rax
movl -0x84(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0xe46e9
movq -0xb8(%rbp), %rax
movl -0xa4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xe46e7
movq -0x18(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0xa4(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0xe46e7
cmpl $0x1, -0x94(%rbp)
jbe 0xe4686
cmpl $0x1, -0x98(%rbp)
jbe 0xe4686
movl $0x1, -0x88(%rbp)
jmp 0xe46e5
cmpl $0x1, -0x98(%rbp)
jne 0xe46b5
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
xorl $0x1, %esi
movl -0xc(%rbp), %edx
callq 0xdd220
cmpl -0xc(%rbp), %eax
jge 0xe46b3
movl $0x1, -0x88(%rbp)
jmp 0xe46e3
cmpl $0x1, -0x94(%rbp)
jne 0xe46e1
movq -0x8(%rbp), %rdi
movl -0x80(%rbp), %esi
xorl $0x1, %esi
movl -0xc(%rbp), %edx
callq 0xdd220
cmpl -0xc(%rbp), %eax
jge 0xe46df
movl $0x1, -0x88(%rbp)
jmp 0xe46e1
jmp 0xe46e3
jmp 0xe46e5
jmp 0xe46e7
jmp 0xe46e9
cmpl $0x0, -0x88(%rbp)
je 0xe4744
movq -0xc0(%rbp), %rax
movl -0x7c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0xb8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xe4746
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xe40a4
movq -0x28(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0x48(%rbp)
je 0xe4779
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movq -0x48(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)