// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %edi, %edi
movq $0x0, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
cmpq %r12, %r15
je 0x76a3c
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%r12)
jne 0x769f7
movq 0x28(%rbx), %rcx
movq (%rcx,%r12,8), %rax
cmpl $0x1, %eax
jne 0x769f7
movq %r12, %r14
xorq $0x1, %r14
cmpl $0x1, (%rcx,%r14,8)
jne 0x769f7
movq %rdi, 0x8(%rsp)
movq 0x8(%rbx), %rcx
shrq $0x20, %rax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %esi
movl %r12d, %r13d
shrl %r13d
movq %rbx, %rdi
movl %r13d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jl 0x769f2
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movl 0x4(%rcx,%r14,8), %ecx
imulq $0x18, %rcx, %rcx
movl 0x8(%rax,%rcx), %esi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jl 0x769f2
movq %rbp, %r14
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rbp
cmpb $0x1, 0x12(%rcx,%rbp)
je 0x769ef
movq %rcx, 0x20(%rsp)
movl %r13d, %eax
imulq $0x18, %rax, %rdi
addq 0x30(%rsp), %rdi
movq 0x20(%rbx), %rcx
movl (%rcx,%rax,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq 0x4c(%rsp), %rdx
leaq 0x40(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x766d4
testl %eax, %eax
je 0x769ef
movq 0x8(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
jne 0x769ff
leaq (%rdi,%rdi), %rbx
testq %rdi, %rdi
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %rax
jmp 0x76a07
movq %r14, %rbp
movq 0x8(%rsp), %rdi
incq %r12
jmp 0x768c4
movq 0x20(%rsp), %rax
movq %rax, %rsi
addq %rbp, %rax
movl 0xc(%rax), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
shlq $0x20, %rcx
subq %rsi, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
orq %rcx, %rax
movq 0x18(%rsp), %rcx
movq %rax, (%r14,%rcx,8)
incq %rcx
movq %rcx, 0x18(%rsp)
movq %r14, %rbp
jmp 0x769f7
movq 0x18(%rsp), %rax
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x66c25
xorl %edx, %edx
movq $0x0, 0x38(%rsp)
movq %rbp, 0x28(%rsp)
cmpq 0x18(%rsp), %rdx
je 0x76c13
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rax
movq %rdx, 0x8(%rsp)
movl (%rbp,%rdx,8), %edx
imulq $0x18, %rdx, %rdx
movq (%rcx,%rdx), %rsi
shrq $0x20, %rsi
movl 0x8(%rcx,%rdx), %r11d
movq %r11, %rdx
xorq $0x1, %rdx
movq (%rax,%rsi,8), %r8
movq (%rax,%rdx,8), %rdi
movq %r8, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rbp
leaq (%rcx,%rbp), %rax
movq %rax, 0x30(%rsp)
movq %rdi, %rdx
shrq $0x20, %rdx
movl %r8d, %r13d
imulq $0x18, %r13, %r9
leaq (%rcx,%rbp), %r10
addq $0xc, %r10
xorl %eax, %eax
xorl %r14d, %r14d
cmpq %rax, %r9
je 0x76ae6
movl (%r10,%rax), %ebx
notl %ebx
shrl $0x1f, %ebx
addl %ebx, %r14d
addq $0x18, %rax
jmp 0x76acf
imulq $0x18, %rdx, %r9
leaq (%rcx,%r9), %rax
movq %rax, 0x20(%rsp)
movl %edi, %r15d
imulq $0x18, %r15, %rdx
leaq (%rcx,%r9), %r10
addq $0xc, %r10
xorl %eax, %eax
xorl %ebx, %ebx
cmpq %rax, %rdx
je 0x76b1f
movl (%r10,%rax), %r12d
notl %r12d
shrl $0x1f, %r12d
addl %r12d, %ebx
addq $0x18, %rax
jmp 0x76b06
testl %r14d, %r14d
je 0x76b51
cmpl $0x2, %ebx
jb 0x76b51
leaq (%rcx,%rbp), %rax
addq $0x8, %rax
xorl %r10d, %r10d
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rbp
cmpq %r10, %r13
je 0x76b63
cmpl %r11d, (%rax)
je 0x76b60
incq %r10
addq $0x18, %rax
jmp 0x76b3e
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rbp
jmp 0x76c06
movq %r10, %r8
leaq (%rcx,%r9), %rax
addq $0x8, %rax
xorl %r10d, %r10d
cmpq %r10, %r15
je 0x76b8b
movl (%rax), %r11d
xorl %esi, %r11d
cmpl $0x1, %r11d
je 0x76b88
incq %r10
addq $0x18, %rax
jmp 0x76b6e
movq %r10, %rdi
movl %r8d, %eax
imulq $0x18, %rax, %rax
movq 0x30(%rsp), %r10
cmpl $0x0, 0xc(%r10,%rax)
movl %edi, %edi
jns 0x76bb2
imulq $0x18, %rdi, %r8
movq 0x20(%rsp), %r11
cmpl $0x0, 0xc(%r11,%r8)
js 0x76c06
addq %rax, %r10
imulq $0x18, %rdi, %rdi
addq %r9, %rcx
addq $0x12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rdx
je 0x76beb
cmpq %r8, %rdi
je 0x76be5
cmpl $0x0, -0x6(%rcx,%r8)
js 0x76be5
xorl %eax, %eax
cmpb $0x1, (%rcx,%r8)
sete %al
addl %eax, %r9d
addq $0x18, %r8
jmp 0x76bc6
testl %r9d, %r9d
je 0x76c06
cmpb $0x0, 0x12(%r10)
jne 0x76c06
shrl %esi
movq %rbx, %rdi
callq 0x6f8da
incq 0x38(%rsp)
movq 0x8(%rsp), %rdx
incq %rdx
jmp 0x76a64
movq %rbp, %rdi
callq 0x2220
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x76c35
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq