// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movl %esi, 0x24(%rsp)
movq %rdi, %rbx
callq 0x251c
movq %rbx, %r8
movl 0x14(%rbx), %eax
addl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
pushq $0x18
popq %r11
xorl %edi, %edi
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rbx, 0x8(%rsp)
cmpq 0x28(%rsp), %rdx
je 0x764b1
movq 0x28(%r8), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x764a9
movq %rdx, 0x30(%rsp)
movq 0x8(%r8), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %rbx
movl %eax, %r14d
leaq (%rsi,%rdx), %r15
addq $0x10, %r15
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
cmpq %rbp, %r14
je 0x764a4
cmpw $0x0, (%r15)
js 0x76498
cmpq %rcx, %r12
jne 0x76475
leaq (%rcx,%rcx), %r14
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r12, %r13
movq %rbx, %r12
movq %r11, %rbx
callq 0x2370
movq %rbx, %r11
movq %r12, %rbx
movq %r13, %r12
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r8
movq %rax, %rdi
movq 0x8(%r8), %rsi
movq %r14, %rcx
movq 0x10(%rsp), %r14
movl -0x4(%r15), %r9d
andl %r10d, %r9d
shlq $0x20, %r9
movq %rbx, %rax
subq %rsi, %rax
cqto
idivq %r11
addq %rbp, %rax
orq %r9, %rax
movq %rax, (%rdi,%r12,8)
incq %r12
incq %rbp
addq $0x18, %r15
jmp 0x76419
movq 0x30(%rsp), %rdx
incq %rdx
jmp 0x763d1
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x18(%rsp)
callq 0x66c25
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
xorl %r11d, %r11d
movq $0x0, 0x38(%rsp)
cmpq %r12, %r11
je 0x7669b
movq 0x8(%rdi), %r8
movq 0x28(%rdi), %rcx
movl (%r10,%r11,8), %eax
imulq $0x18, %rax, %rax
movq (%r8,%rax), %rsi
shrq $0x20, %rsi
movl 0x8(%r8,%rax), %eax
movq %rax, %r9
xorq $0x1, %r9
movq (%rcx,%rsi,8), %rdx
movq (%rcx,%r9,8), %rcx
cmpl $0x1, %edx
jne 0x76518
cmpl $0x1, %ecx
je 0x76693
movq %r11, 0x10(%rsp)
movq %rdx, %rdi
shrq $0x20, %rdi
imulq $0x18, %rdi, %rdi
leaq (%r8,%rdi), %r14
movq %rcx, %r9
shrq $0x20, %r9
movl %edx, %r11d
imulq $0x18, %r11, %r10
movq %rdi, 0x28(%rsp)
leaq (%r8,%rdi), %r15
addq $0xc, %r15
xorl %ebx, %ebx
xorl %edi, %edi
cmpq %rbx, %r10
je 0x76561
movl (%r15,%rbx), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %edi
addq $0x18, %rbx
jmp 0x7654b
movq %r14, 0x30(%rsp)
imulq $0x18, %r9, %r15
leaq (%r8,%r15), %r9
movq %r9, 0x40(%rsp)
movl %ecx, %r10d
imulq $0x18, %r10, %rbp
leaq (%r8,%r15), %r13
addq $0xc, %r13
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpq %rbx, %rbp
je 0x765a1
movl (%r13,%rbx), %r14d
notl %r14d
shrl $0x1f, %r14d
addl %r14d, %r9d
addq $0x18, %rbx
jmp 0x76587
cmpl $0x1, %edi
jne 0x765bb
cmpl $0x1, %r9d
jne 0x765bb
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
jmp 0x7668e
movq 0x28(%rsp), %rbx
addq %r8, %rbx
addq $0x8, %rbx
xorl %r13d, %r13d
movq 0x30(%rsp), %r14
cmpq %r13, %r11
je 0x765e4
cmpl %eax, (%rbx)
je 0x765e1
incq %r13
addq $0x18, %rbx
jmp 0x765cf
movq %r13, %rdx
addq %r15, %r8
addq $0x8, %r8
xorl %r11d, %r11d
cmpq %r11, %r10
je 0x76609
movl (%r8), %ebx
xorl %esi, %ebx
cmpl $0x1, %ebx
je 0x76606
incq %r11
addq $0x18, %r8
jmp 0x765ee
movl %r11d, %ecx
movl %edx, %edx
imulq $0x18, %rdx, %rbx
cmpl $0x0, 0xc(%r14,%rbx)
movl %ecx, %r15d
movq 0x18(%rsp), %r10
jns 0x7662f
imulq $0x18, %r15, %rcx
movq 0x40(%rsp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx)
js 0x76689
cmpl $0x2, %edi
jb 0x7663a
cmpl $0x1, %r9d
ja 0x7666a
cmpl $0x1, %r9d
jne 0x76649
movq 0x8(%rsp), %rdi
movl %eax, %esi
jmp 0x76656
cmpl $0x1, %edi
movq 0x8(%rsp), %rdi
jne 0x7668e
xorl $0x1, %esi
movl 0x24(%rsp), %ebp
movl %ebp, %edx
callq 0x71a0e
movq 0x18(%rsp), %r10
cmpl %ebp, %eax
jge 0x76689
leaq (%r14,%rbx), %rax
addq $0xc, %rax
imulq $0x18, %r15, %rcx
movq 0x40(%rsp), %rdx
orb $-0x80, 0xf(%rdx,%rcx)
orb $-0x80, 0x3(%rax)
incq 0x38(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r11
incq %r11
jmp 0x764d5
movq %r10, %rdi
callq 0x2220
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x766c2
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq