// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_false_node(asg_t*, ma_hit_t_alloc*, int)
int asg_arc_del_false_node(asg_t *g, ma_hit_t_alloc* sources, int max_ext)
{
    double startTime = Get_T();
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));
    ma_sub_t max_left, max_right;

	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)
        {
            if(asg_arc_n(g, v)!=1 || asg_arc_n(g, v^1)!=1)
            {
                continue;
            }


            if(asg_is_single_edge(g, asg_arc_a(g, v)[0].v, v>>1) < 2)
            {
                continue;
            }

            if(asg_is_single_edge(g, asg_arc_a(g, v^1)[0].v, (v^1)>>1) < 2)
            {
                continue;
            }

            if(asg_arc_a(g, v)[0].el == 1)
            {
                continue;
            }

            if(if_potential_false_node(&(sources[v>>1]), g->seq[v>>1].len, &max_left, &max_right, 1)==0)
            {
                continue;
            }

            asg_arc_t *av = asg_arc_a(g, v);
            kv_push(uint64_t, b, (uint64_t)((uint64_t)av[0].ol << 32 | (av - g->arc)));
        }
	}

    radix_sort_arch64(b.a, b.a + b.n);

    uint64_t k;
    ///here all edges are inexact matches 
    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;
		uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw;
		asg_arc_t *av, *aw;
        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 < 2) 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;

        

        uint32_t el_edges = 0;
        ///there should be at least two available edges in aw
        for (i = 0; i < nw; i++)
        {
            if (aw[i].del) continue;

            if(i != iw && aw[i].el == 1)
            {
                el_edges++;
            }
        }

        if(el_edges > 0 && av[iv].el == 0)
        {
            asg_seq_del(g, v>>1);
            ++n_cut;
        }
    }
    
    free(b.a);
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %lld single nodes\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
movq %rsi, %r14
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xafe0e
movq %r14, 0x28(%rsp)
movl %eax, %r9d
xorl %r13d, %r13d
movl $0x1, %r15d
leaq 0x3c(%rsp), %r10
leaq 0x30(%rsp), %r11
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
xorl %edi, %edi
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%r13)
jne 0xafddc
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rcx
cmpl $0x1, %ecx
jne 0xafddc
movl %r13d, %r14d
xorq $0x1, %r14
cmpl $0x1, (%rax,%r14,8)
jne 0xafddc
movq 0x8(%rbx), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rdx,%rcx,8), %ecx
movl %r13d, %ebp
shrl %ebp
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
jle 0xafc2d
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %rsi
addq $0x8, %rsi
xorl %edx, %edx
movl (%rsi), %r8d
shrl %r8d
cmpl %ebp, %r8d
cmovel %r15d, %edx
addq $0x18, %rsi
decq %rcx
jne 0xafc09
testl %edx, %edx
sete %cl
cmpl $0x1, %eax
sete %r12b
jmp 0xafc32
movb $0x1, %r12b
movb $0x1, %cl
testb %cl, %cl
jne 0xafda4
testb %r12b, %r12b
jne 0xafddc
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rax
movl 0x4(%rax,%r14,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rdx,%rcx,8), %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
jle 0xafca5
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %rsi
addq $0x8, %rsi
xorl %edx, %edx
movl (%rsi), %r8d
shrl %r8d
cmpl %ebp, %r8d
cmovel %r15d, %edx
addq $0x18, %rsi
decq %rcx
jne 0xafc81
testl %edx, %edx
sete %cl
cmpl $0x1, %eax
sete %r14b
jmp 0xafcaa
movb $0x1, %r14b
movb $0x1, %cl
testb %cl, %cl
jne 0xafdea
testb %r14b, %r14b
jne 0xafddc
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rax
movq (%rax,%r13,8), %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r14
cmpb $0x1, 0x12(%rcx,%r14,8)
je 0xafddc
movq %rcx, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movq %rdi, %r12
movl %ebp, %eax
leaq (%rax,%rax,2), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdi
movq 0x20(%rbx), %rcx
movl (%rcx,%rax,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r10, %rbp
movq %r10, %rdx
movq %r11, %rcx
movl $0x1, %r8d
callq 0xaf99c
testl %eax, %eax
je 0xafdcc
movq 0x20(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq %r12, %rdi
jne 0xafd54
leaq (%rax,%rax), %r12
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r12, 0x20(%rsp)
movq 0x8(%rsp), %r9
movq %rbp, %r10
leaq 0x30(%rsp), %r11
movq 0x10(%rsp), %rax
leaq (%rax,%r14,8), %rax
movl 0xc(%rax), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
shlq $0x20, %rcx
subq 0x8(%rbx), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rax
orq %rcx, %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rdi,%rcx,8)
incq %rcx
movq %rcx, 0x18(%rsp)
jmp 0xafddc
movq %rdi, 0x10(%rsp)
movq %r9, 0x8(%rsp)
callq 0x10288
leaq 0x30(%rsp), %r11
leaq 0x3c(%rsp), %r10
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rdi
jmp 0xafc3a
movq %r12, %rdi
movq 0x8(%rsp), %r9
movq %rbp, %r10
leaq 0x30(%rsp), %r11
incq %r13
cmpq %r9, %r13
jne 0xafb99
jmp 0xafe19
movq %rdi, 0x10(%rsp)
movq %r9, %r12
callq 0x102a4
leaq 0x30(%rsp), %r11
leaq 0x3c(%rsp), %r10
movq %r12, %r9
movq 0x10(%rsp), %rdi
jmp 0xafcb2
xorl %edi, %edi
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %r14
leaq (%rdi,%r14,8), %rsi
movq %rdi, 0x10(%rsp)
callq 0x9d53f
testq %r14, %r14
je 0xb00ca
movq $0x0, 0x8(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r9
movq 0x10(%rsp), %rdx
movl (%rdx,%rcx,8), %edx
leaq (%rdx,%rdx,2), %r10
movq (%rdi,%r10,8), %rdx
movq %rdx, 0x20(%rsp)
shrq $0x20, %rdx
movq (%r9,%rdx,8), %r11
movq %r11, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r8
movb $0x1, %bpl
movb $0x1, %r12b
testl %r11d, %r11d
je 0xafeb5
movl %r11d, %esi
leaq (%rdi,%r8,8), %r14
addq $0xc, %r14
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
movl (%r14,%r13), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %r15d
addq $0x18, %r13
cmpq %r13, %r12
jne 0xafe99
testl %r15d, %r15d
sete %r12b
movl 0x8(%rdi,%r10,8), %r14d
movq %r14, %rsi
xorq $0x1, %rsi
movq (%r9,%rsi,8), %r9
movq %r9, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r13
testl %r9d, %r9d
je 0xaff22
movq %rcx, 0x28(%rsp)
movq %r8, %r10
movl %r9d, %esi
movq %r13, %rcx
leaq (%rdi,%r13,8), %r13
addq $0xc, %r13
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rbp
xorl %r15d, %r15d
xorl %esi, %esi
movl (%r13,%r15), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %r15
cmpq %r15, %rbp
jne 0xafef8
cmpl $0x2, %esi
setb %bpl
movq %r10, %r8
movq %rcx, %r13
movq 0x28(%rsp), %rcx
orb %bpl, %r12b
testb $0x1, %r12b
jne 0xb00ba
testl %r11d, %r11d
je 0xaff56
leaq (%rdi,%r8,8), %rsi
addq $0x8, %rsi
xorl %ebp, %ebp
cmpl %r14d, (%rsi)
je 0xaff51
incl %ebp
addq $0x18, %rsi
cmpl %ebp, %r11d
jne 0xaff3e
movl %r11d, %ebp
movl %ebp, %r14d
jmp 0xaff59
xorl %r14d, %r14d
testl %r9d, %r9d
je 0xaff87
leaq (%rdi,%r13,8), %r15
addq $0x8, %r15
xorl %r11d, %r11d
movl (%r15), %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0xaff82
incl %r11d
addq $0x18, %r15
cmpl %r11d, %r9d
jne 0xaff69
movl %r9d, %r11d
movl %r11d, %r11d
jmp 0xaff8a
xorl %r11d, %r11d
leaq (%rdi,%r8,8), %rsi
leaq (%r14,%r14,2), %r8
cmpl $0x0, 0xc(%rsi,%r8,8)
jns 0xaffae
leaq (%rdi,%r13,8), %r14
leaq (%r11,%r11,2), %r15
cmpl $0x0, 0xc(%r14,%r15,8)
js 0xb00ba
testl %r9d, %r9d
je 0xb00ba
leaq (%rsi,%r8,8), %r8
movl %r9d, %r9d
leaq (%rdi,%r13,8), %rdi
addq $0x12, %rdi
xorl %r10d, %r10d
xorl %ebp, %ebp
cmpq %r10, %r11
je 0xaffe1
cmpl $0x0, -0x6(%rdi)
js 0xaffe1
xorl %esi, %esi
cmpb $0x1, (%rdi)
sete %sil
addl %esi, %ebp
incq %r10
addq $0x18, %rdi
cmpq %r10, %r9
jne 0xaffcb
testl %ebp, %ebp
je 0xb00ba
cmpb $0x0, 0x12(%r8)
jne 0xb00ba
movq 0x20(%rbx), %rsi
movq 0x20(%rsp), %rdi
shrq $0x21, %rdi
orl $0x80000000, (%rsi,%rdi,8) # imm = 0x80000000
andl $-0x2, %edx
xorl %esi, %esi
leaq (%rsi,%rdx), %rdi
movq 0x28(%rbx), %r8
movq (%r8,%rdi,8), %r9
movq %r9, %r8
andq %rax, %r8
je 0xb00a5
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
shlq $0x3, %r9
addq 0x8(%rbx), %r9
xorl %r10d, %r10d
leaq (%r10,%r10,2), %r11
orb $-0x80, 0xf(%r9,%r11,8)
movl 0x8(%r9,%r11,8), %r11d
xorq $0x1, %r11
movq 0x28(%rbx), %r14
movq (%r14,%r11,8), %r14
movq %r14, %r11
andq %rax, %r11
je 0xb009d
movq 0x8(%rbx), %r15
shrq $0x20, %r14
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%r14,%r14,2), %r14
leaq (%r15,%r14,8), %r14
addq $0xc, %r14
xorl %r15d, %r15d
movl -0x4(%r14,%r15), %ebp
xorl %edi, %ebp
cmpl $0x1, %ebp
jne 0xb0094
orb $-0x80, 0x3(%r14,%r15)
addq $0x18, %r15
cmpq %r15, %r11
jne 0xb0082
incq %r10
cmpq %r8, %r10
jne 0xb0040
leaq 0x1(%rsi), %rdi
testq %rsi, %rsi
movq %rdi, %rsi
je 0xb0019
incq 0x8(%rsp)
incq %rcx
cmpq 0x18(%rsp), %rcx
jne 0xafe45
jmp 0xb00d3
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x8(%rsp)
je 0xb0101
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq