// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movl %esi, 0x14(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xb076b
movq %rbx, %rsi
movl %eax, %eax
movq %rax, 0x20(%rsp)
movq 0x8(%rbx), %rax
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %edi, %edi
movq %rsi, 0x18(%rsp)
movq 0x28(%rsi), %rcx
movq (%rcx,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
movq %rdx, 0x28(%rsp)
je 0xb0523
movq %rcx, %r15
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r15
je 0xb0523
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r10
leaq (%rax,%rcx,8), %r14
addq $0x10, %r14
xorl %r13d, %r13d
cmpw $0x0, (%r14)
js 0xb0517
cmpq %rbx, %r12
jne 0xb04f1
leaq (%r12,%r12), %rbx
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq %r12, 0x8(%rsp)
movq %r10, %r12
callq 0x2370
movq %r12, %r10
movq 0x8(%rsp), %r12
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rsi
movq %rax, %rdi
movq 0x8(%rsi), %rax
movl -0x4(%r14), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %r10, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
addq %r13, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r13
addq $0x18, %r14
cmpq %r13, %r15
jne 0xb04a6
movq 0x28(%rsp), %rdx
incq %rdx
cmpq 0x20(%rsp), %rdx
jne 0xb0465
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x9dc69
testq %r12, %r12
je 0xb0778
movq $0x0, 0x30(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %r9
movq 0x8(%rdi), %rcx
movq 0x28(%rdi), %rdx
movl (%r9,%r13,8), %eax
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rsi
shrq $0x20, %rsi
movl 0x8(%rcx,%rax,8), %eax
movq %rax, %r8
xorq $0x1, %r8
movq (%rdx,%rsi,8), %r10
movq (%rdx,%r8,8), %r8
cmpl $0x1, %r10d
jne 0xb059e
cmpl $0x1, %r8d
je 0xb072a
movq %r10, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
testl %r10d, %r10d
je 0xb05db
movl %r10d, %edi
leaq (%rcx,%rdx,8), %r9
addq $0xc, %r9
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %r11
xorl %ebx, %ebx
xorl %edi, %edi
movl (%r9,%rbx), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %edi
addq $0x18, %rbx
cmpq %rbx, %r11
jne 0xb05c5
jmp 0xb05dd
xorl %edi, %edi
movq %r8, %r9
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
testl %r8d, %r8d
je 0xb0637
movl %r8d, %r11d
leaq (%rcx,%r9,8), %rbx
addq $0xc, %rbx
shlq $0x3, %r11
leaq (%r11,%r11,2), %r14
xorl %r15d, %r15d
xorl %r11d, %r11d
movl (%rbx,%r15), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %r11d
addq $0x18, %r15
cmpq %r15, %r14
jne 0xb0606
cmpl $0x1, %r11d
sete %bpl
cmpl $0x1, %edi
sete %bl
jne 0xb0642
cmpl $0x1, %r11d
je 0xb06e5
jmp 0xb0642
cmpl $0x1, %edi
sete %bl
xorl %ebp, %ebp
xorl %r11d, %r11d
movb %bl, 0x20(%rsp)
testl %r10d, %r10d
je 0xb0671
movl %r10d, %r14d
leaq (%rcx,%rdx,8), %r15
addq $0x8, %r15
xorl %ebx, %ebx
cmpl %eax, (%r15)
je 0xb066c
incq %rbx
addq $0x18, %r15
cmpq %rbx, %r14
jne 0xb0658
movq %r10, %rbx
movl %ebx, %r10d
jmp 0xb0674
xorl %r10d, %r10d
movl %ebp, 0x28(%rsp)
testl %r8d, %r8d
je 0xb06a8
movl %r8d, %r14d
leaq (%rcx,%r9,8), %r15
addq $0x8, %r15
xorl %ebx, %ebx
movl (%r15), %ebp
xorl %esi, %ebp
cmpl $0x1, %ebp
je 0xb06a3
incq %rbx
addq $0x18, %r15
cmpq %rbx, %r14
jne 0xb068a
movq %r8, %rbx
movl %ebx, %r14d
jmp 0xb06ab
xorl %r14d, %r14d
leaq (%rcx,%rdx,8), %rbx
leaq (%rcx,%r9,8), %r15
leaq (%r10,%r10,2), %rbp
cmpl $0x0, 0xc(%rbx,%rbp,8)
jns 0xb06ca
leaq (%r14,%r14,2), %rcx
cmpl $0x0, 0xc(%r15,%rcx,8)
js 0xb06e5
cmpl $0x2, %edi
movq 0x18(%rsp), %rdi
jb 0xb06da
cmpl $0x1, %r11d
ja 0xb070a
cmpb $0x0, 0x28(%rsp)
je 0xb06ec
movl %eax, %esi
jmp 0xb06f6
movq 0x18(%rsp), %rdi
jmp 0xb0725
cmpb $0x0, 0x20(%rsp)
je 0xb0725
xorl $0x1, %esi
movl 0x14(%rsp), %edx
callq 0xab75d
movq 0x18(%rsp), %rdi
cmpl 0x14(%rsp), %eax
jge 0xb0725
leaq (%rbx,%rbp,8), %rax
addq $0xc, %rax
leaq (%r14,%r14,2), %rcx
orb $-0x80, 0xf(%r15,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x30(%rsp)
movq 0x8(%rsp), %r9
incq %r13
cmpq %r12, %r13
jne 0xb0563
movq %r9, %rdi
callq 0x2220
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0xb0782
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xb0785
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xb077d
movq 0x8(%rsp), %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq