// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
movl %r8d, 0x14(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, %rbx
movq %rdi, 0x38(%rsp)
movq 0x18(%rdi), %r13
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movq %rax, 0x50(%rsp)
movq 0x28(%r13), %rax
movl %ecx, 0x10(%rsp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rax, %rdx
andq %rcx, %rdx
movq %r13, 0x20(%rsp)
je 0xed407
movq 0x8(%r13), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rbp
leaq (%rcx,%rbp,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xed2df
movq $0x0, 0x18(%rsp)
cmpl $0x2, %edi
jl 0xed407
movl 0x10(%rsp), %ecx
xorl $0x1, %ecx
movl %ecx, 0xc(%rsp)
testl %eax, %eax
je 0xed407
shlq $0x3, %rbp
addq 0x8(%r13), %rbp
movl %eax, %r15d
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rbp,%rax,8)
js 0xed3fb
leaq (,%rax,8), %r12
addq %rbp, %r12
movl 0x8(%r12), %esi
subq $0x8, %rsp
movq %r13, %rdi
movl 0x1c(%rsp), %edx
leaq 0x14(%rsp), %rcx
movl $0x1, %r8d
movl 0xc0(%rsp), %r9d
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq %rbx
callq 0xece7c
addq $0x20, %rsp
testl %eax, %eax
jne 0xed3fb
orb $-0x80, 0xf(%r12)
movl 0x8(%r12), %eax
xorq $0x1, %rax
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0xed3ea
movl 0x4(%r12), %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 0xed3e1
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xed3d1
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r13
incq %r14
cmpq %r15, %r14
jne 0xed332
movl 0x14(%rsp), %r9d
movl %r9d, %eax
xorl $0x1, %eax
xorl $0x1, 0x10(%rsp)
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rax, %rsi
je 0xed560
movq 0x8(%r13), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rbp
leaq (%rcx,%rbp,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xed450
cmpl $0x2, %edi
jl 0xed560
movl %r9d, 0xc(%rsp)
testl %eax, %eax
je 0xed560
shlq $0x3, %rbp
addq 0x8(%r13), %rbp
movl %eax, %r15d
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rbp,%rax,8)
js 0xed554
leaq (,%rax,8), %r12
addq %rbp, %r12
movl 0x8(%r12), %esi
subq $0x8, %rsp
movq %r13, %rdi
movl 0x18(%rsp), %edx
leaq 0x14(%rsp), %rcx
movl $0x1, %r8d
movl 0xc0(%rsp), %r9d
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq %rbx
callq 0xece7c
addq $0x20, %rsp
testl %eax, %eax
jne 0xed554
orb $-0x80, 0xf(%r12)
movl 0x8(%r12), %eax
xorq $0x1, %rax
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0xed543
movl 0x4(%r12), %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 0xed53a
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xed52a
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r13
incq %r14
cmpq %r15, %r14
jne 0xed48b
cmpq $0x0, 0xb0(%rsp)
je 0xed85a
xorl %edx, %edx
movq %rbx, 0x68(%rsp)
movq 0x60(%rsp), %rax
movq (%rax,%rdx,8), %r15
movl %r15d, %eax
movq 0x20(%r13), %rcx
cmpl $0x0, (%rcx,%rax,8)
js 0xed6a5
cmpb $0x1, 0x4(%rcx,%rax,8)
je 0xed6a5
movq %rdx, 0x30(%rsp)
addl %r15d, %r15d
xorl %r12d, %r12d
orl %r12d, %r15d
movl 0x14(%rsp), %eax
movl %eax, 0xc(%rsp)
subq $0x8, %rsp
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movl %r15d, %esi
movl 0x18(%rsp), %r13d
movl %r13d, %edx
leaq 0x14(%rsp), %rcx
movl $0x1, %r8d
movl 0xc0(%rsp), %eax
movq %rax, %r9
pushq 0x40(%rsp)
movq 0x50(%rsp), %r14
pushq %r14
movq 0x80(%rsp), %rbx
pushq %rbx
callq 0xece7c
addq $0x20, %rsp
movl %eax, 0x50(%rsp,%r12,4)
movl %r13d, 0xc(%rsp)
subq $0x8, %rsp
movq %rbp, %rdi
movl %r15d, %esi
movl 0x1c(%rsp), %edx
leaq 0x14(%rsp), %rcx
movl $0x1, %r8d
movl 0xc0(%rsp), %r9d
pushq 0x40(%rsp)
pushq %r14
pushq %rbx
callq 0xece7c
addq $0x20, %rsp
movl %eax, 0x48(%rsp,%r12,4)
incq %r12
cmpq $0x1, %r12
je 0xed5a6
movq 0x68(%rsp), %rbx
movl 0x48(%rsp), %eax
addl 0x50(%rsp), %eax
jne 0xed65d
movq 0x20(%rsp), %r13
jmp 0xed6a0
movl 0x4c(%rsp), %ecx
addl 0x54(%rsp), %ecx
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %rbp
movq 0x20(%rsp), %r13
je 0xed6a0
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, %eax
setne %sil
cmpl $0x2, %ecx
setb %dl
orb %sil, %dl
cmpl $0x2, %eax
setb %al
cmpl $0x1, %ecx
setne %cl
orb %al, %cl
testb %dl, %cl
je 0xed6ba
movq 0x30(%rsp), %rdx
incl %edx
cmpq 0xb0(%rsp), %rdx
jb 0xed576
jmp 0xed85a
movzbl %dl, %eax
negl %eax
testb %cl, %cl
movl $0x1, %ecx
cmovel %ecx, %eax
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x50(%rsp,%rax,4)
jne 0xed6e8
movl 0x10(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
movl 0x14(%rsp), %ecx
movl %ecx, 0x28(%rsp)
cmpl $0x1, 0x48(%rsp,%rax,4)
jne 0xed6ff
movl 0x14(%rsp), %ecx
movl %ecx, 0x2c(%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, 0x28(%rsp)
cmpl $-0x1, 0x28(%rsp)
movl 0xb8(%rsp), %r9d
movq 0x30(%rsp), %rdx
je 0xed6a5
cmpl $-0x1, 0x2c(%rsp)
je 0xed6a5
movq 0x60(%rsp), %rcx
movl (%rcx,%rdx,8), %ecx
addl %ecx, %ecx
subl %eax, %ecx
incl %ecx
movq 0x28(%r13), %rax
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xed6a5
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x3, %rcx
addq 0x8(%r13), %rcx
movl %eax, %eax
movq %rax, 0x70(%rsp)
xorl %r15d, %r15d
movq %rcx, 0x58(%rsp)
leaq (%r15,%r15,2), %rax
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xed847
leaq (%rcx,%rax,8), %r12
movl 0x8(%r12), %esi
subq $0x8, %rsp
movq %r13, %rdi
movl 0x30(%rsp), %edx
leaq 0x34(%rsp), %rcx
movl $0x1, %r8d
movq %rbp, %r14
pushq %rbp
movq %r10, %rbp
pushq %r10
pushq %rbx
callq 0xece7c
addq $0x20, %rsp
testl %eax, %eax
je 0xed7b9
movl 0xb8(%rsp), %r9d
movq 0x30(%rsp), %rdx
movq %rbp, %r10
movq %r14, %rbp
jmp 0xed842
orb $-0x80, 0xf(%r12)
movl 0x8(%r12), %eax
xorq $0x1, %rax
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
movq %rbp, %r10
movq %r14, %rbp
je 0xed824
movl 0x4(%r12), %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 0xed81b
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xed80b
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r13
movl 0xb8(%rsp), %r9d
movq 0x30(%rsp), %rdx
movq 0x58(%rsp), %rcx
incq %r15
cmpq 0x70(%rsp), %r15
jne 0xed75c
jmp 0xed6a5
movq 0x18(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq