// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_exact(asg_t*, int, ma_hit_t_alloc*)
int asg_arc_del_short_diploid_by_exact(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;
		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);


        ///calculate the longest edge for v and w
		for (i = 0, kv = 0; i < nv; ++i) {
			if (av[i].del) 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;
			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;
        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 $0x78, %rsp
movq %rdx, 0x58(%rsp)
movl %esi, 0x34(%rsp)
movq %rdi, %r12
callq 0x300fc
movl 0x14(%r12), %eax
addl %eax, %eax
movq %r12, 0x20(%rsp)
je 0xaea38
movl %eax, %eax
movq %rax, 0x40(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
xorl %ebp, %ebp
xorl %eax, %eax
xorl %edi, %edi
movq 0x30(%r12), %rcx
cmpb $0x0, (%rcx,%rdx)
movq %rdx, 0x18(%rsp)
je 0xae986
movq 0x18(%rsp), %rdx
incq %rdx
cmpq 0x40(%rsp), %rdx
jne 0xae962
jmp 0xaea3c
movq 0x28(%r12), %rcx
movq (%rcx,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xae972
movq %rcx, %r14
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r14
je 0xae972
movq 0x8(%r12), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r10
leaq (%rdx,%rcx,8), %rbx
addq $0xc, %rbx
xorl %r15d, %r15d
cmpq %rax, %rbp
jne 0xaea02
leaq (%rax,%rax), %r12
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %rbp, 0x48(%rsp)
movq %r10, %rbp
callq 0x2370
movq %rbp, %r10
movq 0x48(%rsp), %rbp
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r12, %rax
movq 0x20(%rsp), %r12
movl (%rbx), %ecx
andl %esi, %ecx
shlq $0x20, %rcx
movq %r10, %rdx
subq 0x8(%r12), %rdx
sarq $0x3, %rdx
imulq %r13, %rdx
addq %r15, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%rbp,8)
incq %rbp
incq %r15
addq $0x18, %rbx
cmpq %r15, %r14
jne 0xae9c0
jmp 0xae972
xorl %edi, %edi
xorl %ebp, %ebp
leaq (%rdi,%rbp,8), %rsi
movq %rdi, 0x10(%rsp)
callq 0x9d53f
testq %rbp, %rbp
je 0xaee73
movq $0x0, 0x50(%rsp)
xorl %r8d, %r8d
movq 0x10(%rsp), %rdi
movq 0x8(%r12), %rbx
movq 0x28(%r12), %rcx
movl (%rdi,%r8,8), %edx
leaq (%rdx,%rdx,2), %rax
movq (%rbx,%rax,8), %r9
movq %r9, 0x48(%rsp)
shrq $0x20, %r9
movl 0x8(%rbx,%rax,8), %r10d
movq %r10, %rdx
xorq $0x1, %rdx
movq (%rcx,%r9,8), %rsi
movq (%rcx,%rdx,8), %rdx
cmpl $0x1, %esi
jne 0xaeaa5
cmpl $0x1, %edx
je 0xaee65
movq %r8, 0x18(%rsp)
movq %rsi, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
testl %esi, %esi
movq %r10, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
je 0xaeb28
movq %r9, 0x28(%rsp)
movl %esi, %r9d
movq %rbx, %rdi
leaq (%rbx,%rcx,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %r12d, %r12d
movl %r8d, %r15d
movl (%r10), %ecx
cmpl %ecx, %r8d
movl %ecx, %r8d
cmoval %r15d, %r8d
movl %ebx, %r13d
movl %r11d, %ebx
cmovael %r13d, %ebx
xorl %r14d, %r14d
testl %ecx, %ecx
setns %r14b
cmovsl %r15d, %r8d
cmovsl %r13d, %ebx
addl %r14d, %r12d
incq %r11
addq $0x18, %r10
cmpq %r11, %r9
jne 0xaeae1
movl %ebx, %r11d
movq 0x28(%rsp), %r9
movq %rdi, %rbx
jmp 0xaeb31
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %r11d, %r11d
leaq (%rbx,%rax,8), %rdi
cmpl $0x2, %r12d
jb 0xaeb4e
movl 0xc(%rdi), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
cmpl %r8d, %ecx
je 0xaecb4
movq %rdi, 0x28(%rsp)
movq %r11, 0x70(%rsp)
movq %r9, %rdi
movq %rdx, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rax
testl %edx, %edx
movq %rbx, 0x60(%rsp)
je 0xaebb5
movl %edx, %ecx
movq %rax, %r8
leaq (%rbx,%rax,8), %r10
addq $0xc, %r10
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r11
xorl %r15d, %r15d
xorl %r9d, %r9d
xorl %ebx, %ebx
movl (%r10,%r15), %ecx
cmpl %ecx, %r9d
movl %ecx, %r14d
cmoval %r9d, %r14d
xorl %r13d, %r13d
testl %ecx, %ecx
cmovnsl %r14d, %r9d
setns %r13b
addl %r13d, %ebx
addq $0x18, %r15
cmpq %r15, %r11
jne 0xaeb8c
jmp 0xaebbd
movq %rax, %r8
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpl $0x2, %ebx
jb 0xaebf1
movq 0x28(%rsp), %rax
movl 0xc(%rax), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
cmpl %r9d, %ecx
movq 0x40(%rsp), %r13
je 0xaecb4
movq %rdi, %r15
movl %r12d, %ecx
orl %ebx, %ecx
cmpl $0x2, %ecx
jae 0xaec07
jmp 0xaecb4
movl %r12d, %ecx
orl %ebx, %ecx
cmpl $0x2, %ecx
movq %rdi, %r15
movq 0x40(%rsp), %r13
jb 0xaecb4
testl %esi, %esi
movq 0x60(%rsp), %rax
je 0xaec39
movq 0x38(%rsp), %rdi
leaq (%rax,%rdi,8), %r10
addq $0x8, %r10
xorl %r9d, %r9d
cmpl %r13d, (%r10)
je 0xaec34
incl %r9d
addq $0x18, %r10
cmpl %r9d, %esi
jne 0xaec20
movl %esi, %r9d
movl %r9d, %esi
jmp 0xaec40
xorl %esi, %esi
movq 0x38(%rsp), %rdi
testl %edx, %edx
je 0xaec6e
leaq (%rax,%r8,8), %r10
addq $0x8, %r10
xorl %r9d, %r9d
movl (%r10), %ecx
xorl %r15d, %ecx
cmpl $0x1, %ecx
je 0xaec69
incl %r9d
addq $0x18, %r10
cmpl %r9d, %edx
jne 0xaec4f
movl %edx, %r9d
movl %r9d, %r14d
jmp 0xaec71
xorl %r14d, %r14d
leaq (%rax,%rdi,8), %r9
leaq (%rax,%r8,8), %rdx
leaq (%rsi,%rsi,2), %r10
cmpl $0x0, 0xc(%r9,%r10,8)
jns 0xaec90
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rdx,%rax,8)
js 0xaecb4
movq 0x28(%rsp), %rax
movb 0x12(%rax), %r11b
xorl %esi, %esi
testb %r11b, %r11b
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r8
je 0xaecc8
movq 0x58(%rsp), %rcx
jmp 0xaed7e
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r8
jmp 0xaee65
movq 0x48(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x58(%rsp), %rcx
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xaed7e
movl %r13d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xaed7e
cmpl $0x2, %r12d
jb 0xaed09
movl $0x1, %esi
cmpl $0x1, %ebx
ja 0xaed7e
movl %r13d, %esi
cmpl $0x1, %ebx
je 0xaed22
movl $0x0, %esi
cmpl $0x1, %r12d
jne 0xaed7e
movl %r15d, %esi
xorl $0x1, %esi
movq 0x20(%rsp), %rdi
movq %r14, 0x38(%rsp)
movl 0x34(%rsp), %r14d
movq %rdx, 0x60(%rsp)
movl %r14d, %edx
movq %r9, 0x28(%rsp)
movq %r10, 0x68(%rsp)
movb %r11b, 0xf(%rsp)
callq 0xaa77e
movb 0xf(%rsp), %r11b
movq 0x68(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x60(%rsp), %rdx
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rcx
xorl %esi, %esi
cmpl %r14d, %eax
movq 0x38(%rsp), %r14
setl %sil
testb %r11b, %r11b
jne 0xaee40
movq 0x48(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xaee40
movl %r13d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x0, 0x10(%rcx,%rax,8)
jne 0xaee40
movq 0x70(%rsp), %rax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%r9,%rax,8)
jne 0xaee40
leaq (%r9,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xaee40
cmpl $0x2, %r12d
jb 0xaede8
movl $0x1, %eax
cmpl $0x1, %ebx
ja 0xaee42
cmpl $0x1, %ebx
je 0xaedfa
cmpl $0x1, %r12d
jne 0xaee40
xorl $0x1, %r15d
movl %r15d, %r13d
movq 0x20(%rsp), %rdi
movl %esi, %r12d
movl %r13d, %esi
movl 0x34(%rsp), %ebx
movq %rdx, %r15
movl %ebx, %edx
movq %r9, 0x28(%rsp)
movq %r10, %r13
callq 0xaa77e
movq %r13, %r10
movq 0x28(%rsp), %r9
movq %r15, %rdx
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %rdi
cmpl %ebx, %eax
movl $0x1, %eax
cmovll %eax, %r12d
movl %r12d, %eax
jmp 0xaee42
movl %esi, %eax
testl %eax, %eax
je 0xaee60
leaq (%r9,%r10,8), %rax
addq $0xc, %rax
leaq (%r14,%r14,2), %rcx
orb $-0x80, 0xf(%rdx,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x50(%rsp)
movq 0x20(%rsp), %r12
incq %r8
cmpq %rbp, %r8
jne 0xaea64
jmp 0xaee81
movq $0x0, 0x50(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rbx
testq %rbx, %rbx
je 0xaeeae
movq %r12, %rdi
callq 0xa02fa
movq %r12, %rdi
callq 0xa0333
movq %r12, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r12)
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq