// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: drop_useless_edges(ma_ug_t*, buf_t*, unsigned char*, unsigned int, unsigned int, unsigned long*, unsigned long, unsigned int)
int drop_useless_edges(ma_ug_t *ug, buf_t* bb, uint8_t* visit, uint32_t beg, uint32_t end, 
uint64_t* nodes, uint64_t nodes_n, uint32_t NodesThres)
{
    asg_t *nsg = ug->g;
    asg_arc_t *av = NULL;
    uint32_t v, w, nv, i, k, dest, n_reduce = 0;
    uint32_t reach_beg[2];
    uint32_t reach_end[2];
    reach_beg[0] = reach_beg[1] = reach_end[0] = reach_end[1] = 0;

    v = beg; dest = end; 
    if(get_real_length(nsg, v, NULL) > 1)
    {
        w = v^1;
        av = asg_arc_a(nsg, v);
        nv = asg_arc_n(nsg, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(find_spec_node(nsg, av[k].v, dest, &w, 1, NodesThres, bb, visit, ug) == 0)
            {
                // if(get_real_length(nsg, v, NULL)<=1)
                // {
                //     fprintf(stderr, "false edge: beg: %u, end: %u\n", (uint32_t)(av[k].ul>>33), av[k].v>>1);
                // }
                
                av[k].del = 1;
                asg_arc_del(nsg, av[k].v^1, ((uint32_t)(av[k].ul>>32))^1, 1);
                n_reduce++;
            }
        }
    }
    


    v = end^1; dest = beg^1; 
    if(get_real_length(nsg, v, NULL) > 1)
    {
        w = v^1;
        av = asg_arc_a(nsg, v);
        nv = asg_arc_n(nsg, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(find_spec_node(nsg, av[k].v, dest, &w, 1, NodesThres, bb, visit, ug) == 0)
            {
                // if(get_real_length(nsg, v, NULL)<=1)
                // {
                //     fprintf(stderr, "false edge: beg: %u, end: %u\n", (uint32_t)(av[k].ul>>33), av[k].v>>1);
                // }

                av[k].del = 1;
                asg_arc_del(nsg, av[k].v^1, ((uint32_t)(av[k].ul>>32))^1, 1);
                n_reduce++;
            }
        }
    }

    uint32_t drop_dest, forbid_dest, drop_node;
    for (i = 0; i < nodes_n; i++)
    {
        v = (uint32_t)nodes[i];
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;
        v = v<<1;

        for (k = 0; k < 2; k++)
        {
            v = v|k;

            w = end;
            reach_beg[k] = find_spec_node(nsg, v, beg^1, &w, 1, NodesThres, bb, visit, ug);
            w = beg^1;
            reach_end[k] = find_spec_node(nsg, v, end, &w, 1, NodesThres, bb, visit, ug);
        }

        if((reach_beg[0]+reach_end[0])==0 || (reach_beg[1]+reach_end[1])==0) continue;

        k = drop_dest = forbid_dest = (uint32_t)-1;
        if((reach_beg[0]+reach_end[0])==1 && (reach_beg[1]+reach_end[1])>1) k = 0;
        if((reach_beg[0]+reach_end[0])>1 && (reach_beg[1]+reach_end[1])==1) k = 1;
        if(k == (uint32_t)-1) continue;

        drop_node = (uint32_t)nodes[i]; 
        drop_node = drop_node <<1; 
        drop_node = drop_node | (uint32_t)(1-k);
        if(reach_beg[k]==1)
        {
            //drop_dest = beg^1; forbid_dest = end;
            drop_dest = end; forbid_dest = beg^1;
        }

        if(reach_end[k]==1)
        {
            //drop_dest = end; forbid_dest = beg^1;
            drop_dest = beg^1; forbid_dest = end;
        }

        if(drop_dest == (uint32_t)-1 || forbid_dest == (uint32_t)-1) continue;

        av = asg_arc_a(nsg, drop_node);
        nv = asg_arc_n(nsg, drop_node);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(find_spec_node(nsg, av[k].v, drop_dest, &forbid_dest, 1, NodesThres, bb, visit, ug) == 0)
            {
                av[k].del = 1;
                asg_arc_del(nsg, av[k].v^1, ((uint32_t)(av[k].ul>>32))^1, 1);

                // fprintf(stderr, "false inner edge: beg: %u, end: %u\n", 
                // (uint32_t)(av[k].ul>>33), av[k].v>>1);

                n_reduce++;
            }
        }

    }

    return n_reduce;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r15
movq %rdx, %r13
movq %rsi, 0x58(%rsp)
movq %rdi, 0x40(%rsp)
movl 0xb8(%rsp), %r11d
movq 0x18(%rdi), %r12
movq $0x0, 0x48(%rsp)
xorl %eax, %eax
movl %eax, 0x50(%rsp)
movl %eax, 0x54(%rsp)
movq 0x28(%r12), %rax
movl %ecx, 0x10(%rsp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rcx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rcx, %rdx
andq %rbx, %rdx
movl %r8d, 0x14(%rsp)
movq %r12, 0x20(%rsp)
je 0xeeeb8
movq 0x8(%r12), %rsi
movq %rcx, %rdi
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%rsi,%rdi,8), %r14
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl 0xc(%r14,%rsi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xeed83
movl $0x0, 0xc(%rsp)
cmpl $0x2, %edi
jl 0xeeec0
movl 0x10(%rsp), %edx
xorl $0x1, %edx
movl %edx, 0x8(%rsp)
testl %ecx, %ecx
je 0xeeec0
movl %r11d, %eax
movq %rax, 0x18(%rsp)
movl %ecx, %eax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%r14,%rax,8)
js 0xeee91
leaq (%r14,%rax,8), %rbx
movl 0x8(%rbx), %esi
subq $0x8, %rsp
movq %r12, %rdi
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
movq 0x20(%rsp), %r9
pushq 0x48(%rsp)
pushq %r13
pushq 0x70(%rsp)
callq 0xee7a2
addq $0x20, %rsp
testl %eax, %eax
jne 0xeee91
orb $-0x80, 0xf(%rbx)
movl 0x8(%rbx), %eax
xorq $0x1, %rax
movq 0x28(%r12), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0xeee88
movl 0x4(%rbx), %eax
movq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rsi
shrq $0x20, %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %edi
xorl %eax, %edi
cmpl $0x1, %edi
jne 0xeee7f
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xeee6f
incl 0xc(%rsp)
movq 0x20(%rsp), %r12
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0xeedd8
movq 0x28(%r12), %rax
movl 0x14(%rsp), %r8d
movl 0xb8(%rsp), %r11d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xeeec0
movl $0x0, 0xc(%rsp)
movl %r8d, %ecx
xorl $0x1, %ecx
xorl $0x1, 0x10(%rsp)
movq (%rax,%rcx,8), %rax
andq %rax, %rbx
je 0xef008
movq 0x8(%r12), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r14
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%r14,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xeeef8
cmpl $0x2, %esi
jl 0xef008
movl %r8d, 0x8(%rsp)
testl %eax, %eax
je 0xef008
movl %r11d, %ecx
movq %rcx, 0x18(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq %r15, %r8
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%r14,%rax,8)
js 0xeeff8
leaq (%r14,%rax,8), %rbx
movl 0x8(%rbx), %esi
subq $0x8, %rsp
movq %r12, %rdi
movl 0x18(%rsp), %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
movq 0x20(%rsp), %r9
pushq 0x48(%rsp)
pushq %r13
pushq 0x70(%rsp)
callq 0xee7a2
addq $0x20, %rsp
testl %eax, %eax
je 0xeef8c
movq %r15, %r8
jmp 0xeeff8
orb $-0x80, 0xf(%rbx)
movl 0x8(%rbx), %eax
xorq $0x1, %rax
movq 0x28(%r12), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
movq %r15, %r8
je 0xeefef
movl 0x4(%rbx), %eax
movq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rsi
shrq $0x20, %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %edi
xorl %eax, %edi
cmpl $0x1, %edi
jne 0xeefe6
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xeefd6
incl 0xc(%rsp)
movq 0x20(%rsp), %r12
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0xeef37
jmp 0xef00b
movq %r15, %r8
cmpq $0x0, 0xb0(%rsp)
je 0xef317
xorl %edx, %edx
movq %r8, 0x60(%rsp)
movq %r13, 0x68(%rsp)
movq (%r8,%rdx,8), %r14
movl %r14d, %eax
movq 0x20(%r12), %rcx
cmpl $0x0, (%rcx,%rax,8)
js 0xef151
cmpb $0x1, 0x4(%rcx,%rax,8)
je 0xef151
movq %rdx, 0x28(%rsp)
addl %r14d, %r14d
xorl %ecx, %ecx
movb $0x1, %al
leaq 0x48(%rsp), %r13
leaq 0x54(%rsp), %rbx
movq %rbx, 0x70(%rsp)
movl %eax, 0x18(%rsp)
movq %r13, 0x30(%rsp)
orl %ecx, %r14d
movl 0x14(%rsp), %eax
movl %eax, 0x8(%rsp)
subq $0x8, %rsp
movq 0x28(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl 0x18(%rsp), %ebp
movl %ebp, %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
movl 0xc0(%rsp), %ebx
movq %rbx, %r9
pushq 0x48(%rsp)
movq 0x78(%rsp), %r15
pushq %r15
movq 0x70(%rsp), %r13
pushq %r13
callq 0xee7a2
addq $0x20, %rsp
movq 0x70(%rsp), %rcx
movl %eax, (%rcx)
movl %ebp, 0x8(%rsp)
subq $0x8, %rsp
movq %r12, %rdi
movl %r14d, %esi
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
movq %rbx, %r9
pushq 0x48(%rsp)
pushq %r15
pushq %r13
callq 0xee7a2
leaq 0x70(%rsp), %rbx
leaq 0x6c(%rsp), %r13
addq $0x20, %rsp
movq 0x30(%rsp), %rdx
movl %eax, (%rdx)
movl $0x1, %ecx
testb $0x1, 0x18(%rsp)
movl $0x0, %eax
jne 0xef05d
movq 0x68(%rsp), %r13
movl 0x54(%rsp), %edx
movl 0x48(%rsp), %eax
addl %edx, %eax
jne 0xef136
movq 0x60(%rsp), %r8
movq 0x20(%rsp), %r12
jmp 0xef14c
movl 0x50(%rsp), %ecx
movl 0x4c(%rsp), %esi
addl %ecx, %esi
movq 0x60(%rsp), %r8
movq 0x20(%rsp), %r12
jne 0xef166
movq 0x28(%rsp), %rdx
incl %edx
cmpq 0xb0(%rsp), %rdx
jb 0xef026
jmp 0xef317
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, %eax
jne 0xef17e
xorl %eax, %eax
movl %edx, %ecx
cmpl $0x2, %esi
jb 0xef14c
jmp 0xef194
testl %eax, %eax
setne %dl
cmpl $0x1, %esi
sete %sil
movl $0x1, %eax
testb %sil, %dl
je 0xef14c
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, %ecx
jne 0xef1b6
movl 0x10(%rsp), %edx
movl %edx, 0x3c(%rsp)
movl 0x14(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, %ecx
cmpl $0x1, 0x48(%rsp,%rcx,4)
jne 0xef1cf
movl 0x14(%rsp), %edx
movl %edx, 0x3c(%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, 0x18(%rsp)
cmpl $-0x1, 0x18(%rsp)
sete %cl
cmpl $-0x1, %edx
sete %dl
orb %cl, %dl
jne 0xef14c
movq 0x28(%rsp), %rdx
movl (%r8,%rdx,8), %ecx
leal (%rax,%rcx,2), %eax
xorl $0x1, %eax
movq 0x28(%r12), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
testl %eax, %eax
movl 0xb8(%rsp), %r9d
je 0xef151
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%r12), %rcx
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xef304
movq %r8, %r15
movq %rcx, %rbp
leaq (%rcx,%rax,8), %rbx
movl 0x8(%rbx), %esi
subq $0x8, %rsp
movq %r12, %rdi
movl 0x20(%rsp), %edx
leaq 0x44(%rsp), %rcx
movl $0x1, %r8d
pushq 0x48(%rsp)
pushq %r13
pushq 0x70(%rsp)
callq 0xee7a2
addq $0x20, %rsp
testl %eax, %eax
je 0xef288
movl 0xb8(%rsp), %r9d
movq 0x28(%rsp), %rdx
movq %r15, %r8
jmp 0xef301
orb $-0x80, 0xf(%rbx)
movl 0x8(%rbx), %eax
xorq $0x1, %rax
movq 0x28(%r12), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
movq %r15, %r8
je 0xef2eb
movl 0x4(%rbx), %eax
movq 0x20(%rsp), %rsi
movq 0x8(%rsi), %rsi
shrq $0x20, %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %edi
xorl %eax, %edi
cmpl $0x1, %edi
jne 0xef2e2
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xef2d2
incl 0xc(%rsp)
movq 0x20(%rsp), %r12
movl 0xb8(%rsp), %r9d
movq 0x28(%rsp), %rdx
movq %rbp, %rcx
incq %r14
cmpq 0x30(%rsp), %r14
jne 0xef22d
jmp 0xef151
movl 0xc(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq