// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jle 0x14833a
movl -0x3c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x148338
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x148277
jmp 0x14832a
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movl -0x50(%rbp), %edx
movl 0x18(%rbp), %eax
movl %eax, %r9d
movq -0x10(%rbp), %r11
movq -0x18(%rbp), %r10
movq -0x8(%rbp), %rax
leaq -0x40(%rbp), %rcx
movl $0x1, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x147c90
cmpl $0x0, %eax
jne 0x148328
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
xorl $0x1, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x14832a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x14824d
jmp 0x14833a
movl -0x20(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jle 0x14849a
movl -0x3c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x148498
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1483d7
jmp 0x14848a
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movl -0x50(%rbp), %edx
movl 0x18(%rbp), %eax
movl %eax, %r9d
movq -0x10(%rbp), %r11
movq -0x18(%rbp), %r10
movq -0x8(%rbp), %rax
leaq -0x40(%rbp), %rcx
movl $0x1, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x147c90
cmpl $0x0, %eax
jne 0x148488
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
xorl $0x1, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x14848a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1483ad
jmp 0x14849a
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpq 0x10(%rbp), %rax
jae 0x1487cf
movq -0x28(%rbp), %rax
movl -0x48(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1484d7
jmp 0x1487c1
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x1484f1
jmp 0x1487c1
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x2, -0x4c(%rbp)
jae 0x1485b7
movl -0x3c(%rbp), %eax
orl -0x4c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x1c(%rbp), %edx
xorl $0x1, %edx
movl 0x18(%rbp), %eax
movl %eax, %r9d
movq -0x10(%rbp), %r11
movq -0x18(%rbp), %r10
movq -0x8(%rbp), %rax
leaq -0x40(%rbp), %rcx
movl $0x1, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x147c90
movl %eax, %ecx
movl -0x4c(%rbp), %eax
movl %ecx, -0x5c(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x20(%rbp), %edx
movl 0x18(%rbp), %eax
movl %eax, %r9d
movq -0x10(%rbp), %r11
movq -0x18(%rbp), %r10
movq -0x8(%rbp), %rax
leaq -0x40(%rbp), %rcx
movl $0x1, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x147c90
movl %eax, %ecx
movl -0x4c(%rbp), %eax
movl %ecx, -0x64(%rbp,%rax,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x148500
movl -0x5c(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl $0x0, %eax
je 0x1485cd
movl -0x58(%rbp), %eax
addl -0x60(%rbp), %eax
cmpl $0x0, %eax
jne 0x1485d2
jmp 0x1487c1
movl $0xffffffff, -0x6c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl -0x5c(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl $0x1, %eax
jne 0x148604
movl -0x58(%rbp), %eax
addl -0x60(%rbp), %eax
cmpl $0x1, %eax
jbe 0x148604
movl $0x0, -0x4c(%rbp)
movl -0x5c(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl $0x1, %eax
jbe 0x148621
movl -0x58(%rbp), %eax
addl -0x60(%rbp), %eax
cmpl $0x1, %eax
jne 0x148621
movl $0x1, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x14862c
jmp 0x1487c1
movq -0x28(%rbp), %rax
movl -0x48(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
shll %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl $0x1, %ecx
subl -0x4c(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x70(%rbp)
movl -0x4c(%rbp), %eax
cmpl $0x1, -0x5c(%rbp,%rax,4)
jne 0x14866b
movl -0x20(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x4c(%rbp), %eax
cmpl $0x1, -0x64(%rbp,%rax,4)
jne 0x148684
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x6c(%rbp)
cmpl $-0x1, -0x68(%rbp)
je 0x148690
cmpl $-0x1, -0x6c(%rbp)
jne 0x148695
jmp 0x1487c1
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x70(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x1487bf
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1486fe
jmp 0x1487b1
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movl -0x68(%rbp), %edx
movl 0x18(%rbp), %eax
movl %eax, %r9d
movq -0x10(%rbp), %r11
movq -0x18(%rbp), %r10
movq -0x8(%rbp), %rax
leaq -0x6c(%rbp), %rcx
movl $0x1, %r8d
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x147c90
cmpl $0x0, %eax
jne 0x1487af
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
xorl $0x1, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1487b1
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1486d4
jmp 0x1487c1
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1484a1
movl -0x54(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)