// File: /workspace/github_cpp_makefile_star2_O2/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
movl %r8d, 0x14(%rsp)
movl %ecx, %r14d
movq %rdx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x18(%rdi), %rbx
andq $0x0, 0x58(%rsp)
movl 0xb8(%rsp), %ebp
andq $0x0, 0x68(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
movl %ecx, %esi
xorl %edx, %edx
callq 0xc2e7a
movl %r14d, %ecx
xorl $0x1, %ecx
movl %ecx, 0x10(%rsp)
movl %ebp, %ecx
movq %rcx, 0x38(%rsp)
cmpl $0x2, %eax
jl 0xafa9c
leaq 0xc(%rsp), %rcx
movl 0x10(%rsp), %eax
movl %eax, (%rcx)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movl %r14d, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r13
imulq $0x18, %rcx, %rcx
leaq (%rcx,%rax), %rbp
addq $0xc, %rbp
xorl %r14d, %r14d
pushq $0x1
popq %r12
movq $0x0, 0x18(%rsp)
cmpq %r14, %r13
je 0xafa9c
cmpl $0x0, (%rbp,%r14)
js 0xafa96
movl -0x4(%rbp,%r14), %esi
subq $0x8, %rsp
movq %rbx, %rdi
movl 0x1c(%rsp), %edx
leaq 0x14(%rsp), %rcx
movl %r12d, %r8d
movq 0x40(%rsp), %r9
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0xaf676
addq $0x20, %rsp
testl %eax, %eax
jne 0xafa96
orb $-0x80, 0x3(%rbp,%r14)
movl -0x8(%rbp,%r14), %edx
movl -0x4(%rbp,%r14), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %rbx, %rdi
movl %r12d, %ecx
callq 0x6db50
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
addq $0x18, %r14
jmp 0xafa26
movl 0x14(%rsp), %ebp
movl %ebp, %r14d
xorl $0x1, %r14d
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jle 0xafb66
leaq 0xc(%rsp), %rax
movl %ebp, (%rax)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movl %r14d, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r13
imulq $0x18, %rcx, %rcx
leaq (%rcx,%rax), %rbp
addq $0xc, %rbp
xorl %r14d, %r14d
pushq $0x1
popq %r12
cmpq %r14, %r13
je 0xafb66
cmpl $0x0, (%rbp,%r14)
js 0xafb60
movl -0x4(%rbp,%r14), %esi
subq $0x8, %rsp
movq %rbx, %rdi
movl 0x18(%rsp), %edx
leaq 0x14(%rsp), %rcx
movl %r12d, %r8d
movq 0x40(%rsp), %r9
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0xaf676
addq $0x20, %rsp
testl %eax, %eax
jne 0xafb60
orb $-0x80, 0x3(%rbp,%r14)
movl -0x8(%rbp,%r14), %edx
movl -0x4(%rbp,%r14), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %rbx, %rdi
movl %r12d, %ecx
callq 0x6db50
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
addq $0x18, %r14
jmp 0xafaf0
xorl %edx, %edx
movq %r15, 0x70(%rsp)
movq %rbx, 0x60(%rsp)
movl %edx, %edx
cmpq 0xb0(%rsp), %rdx
jae 0xafdaf
movq (%r15,%rdx,8), %r14
movl %r14d, %eax
movq 0x20(%rbx), %rcx
cmpl $0x0, (%rcx,%rax,8)
js 0xafc8b
cmpb $0x1, 0x4(%rcx,%rax,8)
je 0xafc8b
movq %rdx, 0x20(%rsp)
addl %r14d, %r14d
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0xafc54
orl %r15d, %r14d
movq %r14, 0x28(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0xc(%rsp)
subq $0x8, %rsp
movq 0x68(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rsi
movl 0x18(%rsp), %r12d
movl %r12d, %edx
leaq 0x14(%rsp), %rcx
pushq $0x1
popq %r8
movq 0x40(%rsp), %r9
movq %r9, 0x40(%rsp)
movq 0x48(%rsp), %r13
pushq %r13
movq 0x60(%rsp), %rbx
pushq %rbx
movq 0x60(%rsp), %rbp
pushq %rbp
callq 0xaf676
addq $0x20, %rsp
movl %eax, 0x68(%rsp,%r15,4)
movl %r12d, 0xc(%rsp)
subq $0x8, %rsp
movq %r14, %rdi
movq 0x30(%rsp), %r14
movl %r14d, %esi
movl 0x1c(%rsp), %edx
leaq 0x14(%rsp), %rcx
pushq $0x1
popq %r8
movq 0x40(%rsp), %r9
pushq %r13
pushq %rbx
pushq %rbp
callq 0xaf676
addq $0x20, %rsp
movl %eax, 0x58(%rsp,%r15,4)
incq %r15
jmp 0xafbad
movl 0x68(%rsp), %edx
movl 0x58(%rsp), %eax
addl %edx, %eax
jne 0xafc6c
movq 0x70(%rsp), %r15
movq 0x60(%rsp), %rbx
jmp 0xafc86
movl 0x6c(%rsp), %ecx
movl 0x5c(%rsp), %esi
addl %ecx, %esi
movq 0x70(%rsp), %r15
movl 0x14(%rsp), %edi
movq 0x60(%rsp), %rbx
jne 0xafc92
movq 0x20(%rsp), %rdx
incl %edx
jmp 0xafb72
orl $-0x1, 0x34(%rsp)
cmpl $0x1, %eax
jne 0xafca7
xorl %eax, %eax
movl %edx, %ecx
cmpl $0x2, %esi
jb 0xafc86
jmp 0xafcbb
testl %eax, %eax
setne %dl
cmpl $0x1, %esi
sete %sil
pushq $0x1
popq %rax
testb %sil, %dl
je 0xafc86
pushq $-0x1
popq %rsi
movl %esi, %edx
movl %esi, 0x28(%rsp)
cmpl $0x1, %ecx
jne 0xafcd5
movl 0x10(%rsp), %edx
movl %edx, 0x34(%rsp)
movl %edi, 0x28(%rsp)
movl %eax, %ecx
cmpl $0x1, 0x58(%rsp,%rcx,4)
pushq $0x1
popq %rbp
jne 0xafcef
movl %edi, 0x34(%rsp)
movl %edi, %edx
movl 0x10(%rsp), %ecx
movl %ecx, 0x28(%rsp)
cmpl $-0x1, 0x28(%rsp)
sete %cl
cmpl $-0x1, %edx
sete %dl
orb %cl, %dl
jne 0xafc86
movq 0x20(%rsp), %rcx
movl (%r15,%rcx,8), %ecx
leal (%rax,%rcx,2), %eax
xorl $0x1, %eax
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rdx
movl %eax, %eax
movq (%rdx,%rax,8), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rdx, %r13
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %r12
addq $0xc, %r12
xorl %r14d, %r14d
cmpq %r14, %r13
je 0xafc86
cmpl $0x0, (%r12,%r14)
js 0xafda9
movl -0x4(%r12,%r14), %esi
subq $0x8, %rsp
movq %rbx, %rdi
movl 0x30(%rsp), %edx
leaq 0x3c(%rsp), %rcx
movl %ebp, %r8d
movq 0x40(%rsp), %r9
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0xaf676
addq $0x20, %rsp
testl %eax, %eax
jne 0xafda9
orb $-0x80, 0x3(%r12,%r14)
movl -0x8(%r12,%r14), %edx
movl -0x4(%r12,%r14), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %rbx, %rdi
movl %ebp, %ecx
callq 0x6db50
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
addq $0x18, %r14
jmp 0xafd37
movq 0x18(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq