// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploi_by_suspect_edge(asg_t*, int)
int asg_arc_del_short_diploi_by_suspect_edge(asg_t *g, int max_ext)
{
    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)
            {
                ///means there is a large indel at this edge
                if(av[i].no_l_indel == 0)
                {
                    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;
		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;
			++kv;
		}

		for (i = 0, kw = 0; i < nw; ++i) 
        {
			if (aw[i].del) continue;
			++kw;
		}

		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 (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 suspect 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 $0x28, %rsp
movl %esi, 0x24(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
addl %eax, %eax
movq %rbx, 0x8(%rsp)
je 0xaf74f
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r9 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %eax, %eax
xorl %edi, %edi
movq 0x28(%rbx), %rcx
movq %rdx, 0x10(%rsp)
movq (%rcx,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaf73a
movq %rcx, %r15
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r15
je 0xaf73a
movq 0x8(%rbx), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r10
leaq (%rdx,%rcx,8), %rbp
addq $0x10, %rbp
xorl %r14d, %r14d
cmpw $0x0, (%rbp)
js 0xaf72e
cmpq %rax, %r12
jne 0xaf708
leaq (%rax,%rax), %r13
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r13
leaq (,%r13,8), %rsi
movq %r12, (%rsp)
movq %r9, %r12
movq %r10, %rbx
callq 0x2370
movq %rbx, %r10
movq %r12, %r9
movq (%rsp), %r12
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rbx
movq %rax, %rdi
movq %r13, %rax
movl -0x4(%rbp), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %r10, %rdx
subq 0x8(%rbx), %rdx
sarq $0x3, %rdx
imulq %r9, %rdx
addq %r14, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r14
addq $0x18, %rbp
cmpq %r14, %r15
jne 0xaf6ba
movq 0x10(%rsp), %rdx
incq %rdx
cmpq 0x18(%rsp), %rdx
jne 0xaf675
jmp 0xaf754
xorl %edi, %edi
xorl %r12d, %r12d
leaq (%rdi,%r12,8), %rsi
movq %rdi, (%rsp)
callq 0x9d53f
testq %r12, %r12
je 0xaf94a
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
movq 0x8(%rdi), %rcx
movq 0x28(%rdi), %rdx
movl (%r10,%r13,8), %eax
leaq (%rax,%rax,2), %rsi
movq (%rcx,%rsi,8), %rax
shrq $0x20, %rax
movl 0x8(%rcx,%rsi,8), %esi
movq %rsi, %r8
xorq $0x1, %r8
movq (%rdx,%rax,8), %r11
movq (%rdx,%r8,8), %r9
cmpl $0x1, %r11d
jne 0xaf7ba
cmpl $0x1, %r9d
je 0xaf93c
movq %r11, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdi
xorl %r8d, %r8d
movl $0x0, %edx
testl %r11d, %r11d
je 0xaf7fe
movl %r11d, %edx
leaq (%rcx,%rdi,8), %r10
addq $0xc, %r10
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rbx
xorl %r14d, %r14d
xorl %edx, %edx
movl (%r10,%r14), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %edx
addq $0x18, %r14
cmpq %r14, %rbx
jne 0xaf7ea
movq %r9, %r10
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
testl %r9d, %r9d
je 0xaf842
movq %rdi, %r8
movl %r9d, %edi
leaq (%rcx,%r10,8), %rbx
addq $0xc, %rbx
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %r14
movq %r8, %rdi
xorl %r15d, %r15d
xorl %r8d, %r8d
movl (%rbx,%r15), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %r8d
addq $0x18, %r15
cmpq %r15, %r14
jne 0xaf82d
cmpl $0x1, %edx
jne 0xaf85b
cmpl $0x1, %r8d
jne 0xaf85b
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
jmp 0xaf93c
testl %r11d, %r11d
je 0xaf882
leaq (%rcx,%rdi,8), %r14
addq $0x8, %r14
xorl %ebx, %ebx
cmpl %esi, (%r14)
je 0xaf87d
incl %ebx
addq $0x18, %r14
cmpl %ebx, %r11d
jne 0xaf86a
movl %r11d, %ebx
movl %ebx, %r11d
jmp 0xaf885
xorl %r11d, %r11d
testl %r9d, %r9d
je 0xaf8b1
leaq (%rcx,%r10,8), %r14
addq $0x8, %r14
xorl %ebx, %ebx
movl (%r14), %ebp
xorl %eax, %ebp
cmpl $0x1, %ebp
je 0xaf8ac
incl %ebx
addq $0x18, %r14
cmpl %ebx, %r9d
jne 0xaf894
movl %r9d, %ebx
movl %ebx, %r9d
jmp 0xaf8b4
xorl %r9d, %r9d
leaq (%rcx,%rdi,8), %rbp
leaq (%rcx,%r10,8), %r14
leaq (%r11,%r11,2), %r15
cmpl $0x0, 0xc(%rbp,%r15,8)
jns 0xaf8d8
leaq (%r9,%r9,2), %rcx
cmpl $0x0, 0xc(%r14,%rcx,8)
js 0xaf84d
cmpl $0x2, %edx
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
jb 0xaf8ec
cmpl $0x1, %r8d
ja 0xaf91e
cmpl $0x1, %r8d
je 0xaf8fc
cmpl $0x1, %edx
jne 0xaf93c
xorl $0x1, %eax
movl %eax, %esi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
movq %r9, 0x18(%rsp)
callq 0xaa77e
movq 0x18(%rsp), %r9
movq (%rsp), %r10
movq 0x8(%rsp), %rdi
cmpl %ebx, %eax
jge 0xaf93c
leaq 0xc(,%r15,8), %rax
addq %rbp, %rax
leaq (%r9,%r9,2), %rcx
orb $-0x80, 0xf(%r14,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x10(%rsp)
incq %r13
cmpq %r12, %r13
jne 0xaf77f
jmp 0xaf957
movq $0x0, 0x10(%rsp)
movq (%rsp), %r10
movq %r10, %rdi
callq 0x2220
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0xaf98a
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq