// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: test_triangular_exact(asg_t*, unsigned int*, unsigned int, unsigned int, unsigned int, int, buf_t*)
int test_triangular_exact(asg_t *g, uint32_t* nodes, uint32_t length, 
uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub)
{
    
    uint32_t i, v, w;
    ///int flag0, flag1, node;
    int n_reduced = 0, todel;
    long long NodeLen_first[3];
    long long NodeLen_second[3];

    uint32_t Ns_first[3];
    uint32_t Ns_second[3];
    
    
    for (i = 0; i < length; ++i)
    {
        v = nodes[i];

        if((v>>1) == (startNode>>1) || (v>>1) == (endNode>>1))
        {
            continue;
        }

        uint32_t nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        if(nv != 2)
        {
            continue;
        }
        if(av[0].v == av[1].v)
        {
            continue;
        }
        /**********************test first node************************/
        NodeLen_first[0] = NodeLen_first[1] = NodeLen_first[2] = -1;
        if(asg_is_single_edge(g, av[0].v, v>>1) <= 2 && asg_is_single_edge(g, av[1].v, v>>1) <= 2)
        {
            NodeLen_first[asg_is_single_edge(g, av[0].v, v>>1)] = 0;
            NodeLen_first[asg_is_single_edge(g, av[1].v, v>>1)] = 1;
        }
        ///one node has one out-edge, another node has two out-edges
        if(NodeLen_first[1] == -1 || NodeLen_first[2] == -1)
        {
            continue;
        }
       /**********************test first node************************/

        ///if the potiential edge has already been removed 
        if(av[NodeLen_first[2]].del == 1)
        {
            continue;
        }

        /**********************test second node************************/
        w = av[NodeLen_first[2]].v^1;
        asg_arc_t *aw = asg_arc_a(g, w);
        uint32_t nw = asg_arc_n(g, w);
        if(nw != 2)
        {
            fprintf(stderr, "error\n");
        }
        NodeLen_second[0] = NodeLen_second[1] = NodeLen_second[2] = -1;
        if(asg_is_single_edge(g, aw[0].v, w>>1) <= 2 && asg_is_single_edge(g, aw[1].v, w>>1) <= 2)
        {
            NodeLen_second[asg_is_single_edge(g, aw[0].v, w>>1)] = 0;
            NodeLen_second[asg_is_single_edge(g, aw[1].v, w>>1)] = 1;
        }
        ///one node has one out-edge, another node has two out-edges
        if(NodeLen_second[1] == -1 || NodeLen_second[2] == -1)
        {
            continue;
        }

        /**********************test second node************************/

        if(if_node_exist(nodes, length, (w>>1)) && ((w>>1) != (endNode>>1)))
        {

               uint32_t convex1 = 0, convex2 = 0, f1, f2;
               long long l1 = 0, l2 = 0;
               todel = 0;
               f1 = detect_bubble_end_with_bubbles(g, av[0].v, av[1].v, &convex1, &l1, NULL);
               f2 = detect_bubble_end_with_bubbles(g, aw[0].v, aw[1].v, &convex2, &l2, NULL);
               if(f1 && f2)
               {
                    if(((convex1>>1) == (startNode>>1) || (convex1>>1) == (endNode>>1)) && 
                     ((convex2>>1) == (startNode>>1) || (convex2>>1) == (endNode>>1)))
                     {
                         if(l1 <= min_thres || l2 <= min_thres)
                         {
                             continue;
                         } 

                         todel = 1;
                     } 
               }
               else if(f1)
               {
                  if(((convex1>>1) == (startNode>>1) || (convex1>>1) == (endNode>>1)))
                   {
                       if(l1 <= min_thres)
                       {
                           continue;
                       }

                       todel = 1;
                   }   
               }
               else if(f2)
               {
                   if(((convex2>>1) == (startNode>>1) || (convex2>>1) == (endNode>>1)))
                   {
                       if(l2 <= min_thres)
                       {
                           continue;
                       }

                       todel = 1;
                   }   
               }

            
               if(todel == 0)
               {
                   if(!f1)
                   {
                       Ns_first[0] = av[0].v; Ns_first[1] = av[1].v;
                       f1 = asg_bub_end_finder_with_del_advance(g, Ns_first, 2, max_dist, 
                       bub, 0, (uint32_t)-1, &convex1);
                       l1 = min_thres + 10;
                   }

                   if(!f2)
                   {
                       Ns_second[0] = aw[0].v; Ns_second[1] = aw[1].v;
                       f2 = asg_bub_end_finder_with_del_advance(g, Ns_second, 2, max_dist, 
                       bub, 0, (uint32_t)-1, &convex2);
                       l2 = min_thres + 10;
                   }




                    
                    if(f1 && f2)
                    {
                        if(((convex1>>1) == (startNode>>1) || (convex1>>1) == (endNode>>1)) && 
                        ((convex2>>1) == (startNode>>1) || (convex2>>1) == (endNode>>1)))
                        {
                            if(l1 <= min_thres || l2 <= min_thres)
                            {
                                continue;
                            } 

                            todel = 1;
                        } 
                    }
                    else if(f1)
                    {
                        if(((convex1>>1) == (startNode>>1) || (convex1>>1) == (endNode>>1)))
                        {
                            if(l1 <= min_thres)
                            {
                                continue;
                            }

                            todel = 1;
                        }   
                    }
                    else if(f2)
                    {
                        if(((convex2>>1) == (startNode>>1) || (convex2>>1) == (endNode>>1)))
                        {
                            if(l2 <= min_thres)
                            {
                                continue;
                            }

                            todel = 1;
                        }   
                    }
                    
               }
               
               
               if(todel)
               {
                   av[NodeLen_first[2]].del = 1;
                    ///remove the reverse direction
                    asg_arc_del(g, av[NodeLen_first[2]].v^1, av[NodeLen_first[2]].ul>>32^1, 1);
                    n_reduced++;
               }
 
        }

    }

    return n_reduced;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0xd5d9d
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd56d8
movl -0x28(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd56dd
jmp 0xd5d8f
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x8c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
cmpl $0x2, -0x8c(%rbp)
je 0xd5729
jmp 0xd5d8f
movq -0x98(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x98(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jne 0xd5744
jmp 0xd5d8f
movq $-0x1, -0x40(%rbp)
movq $-0x1, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x28(%rbp), %edx
shrl %edx
callq 0xd4770
cmpl $0x2, %eax
jg 0xd57dc
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x28(%rbp), %edx
shrl %edx
callq 0xd4770
cmpl $0x2, %eax
jg 0xd57dc
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x28(%rbp), %edx
shrl %edx
callq 0xd4770
cltq
movq $0x0, -0x50(%rbp,%rax,8)
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x28(%rbp), %edx
shrl %edx
callq 0xd4770
cltq
movq $0x1, -0x50(%rbp,%rax,8)
cmpq $-0x1, -0x48(%rbp)
je 0xd57ea
cmpq $-0x1, -0x40(%rbp)
jne 0xd57ef
jmp 0xd5d8f
movq -0x98(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xd580e
jmp 0xd5d8f
movq -0x98(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x2c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa4(%rbp)
cmpl $0x2, -0xa4(%rbp)
je 0xd5885
movq 0x2ee76c(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x2acd67(%rip), %rsi    # 0x3825e5
movb $0x0, %al
callq 0x22a0
movq $-0x1, -0x60(%rbp)
movq $-0x1, -0x68(%rbp)
movq $-0x1, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd4770
cmpl $0x2, %eax
jg 0xd591d
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd4770
cmpl $0x2, %eax
jg 0xd591d
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd4770
cltq
movq $0x0, -0x70(%rbp,%rax,8)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd4770
cltq
movq $0x1, -0x70(%rbp,%rax,8)
cmpq $-0x1, -0x68(%rbp)
je 0xd592b
cmpq $-0x1, -0x60(%rbp)
jne 0xd5930
jmp 0xd5d8f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd45f0
cmpl $0x0, %eax
je 0xd5d8d
movl -0x2c(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5d8d
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x98(%rbp), %rax
movl 0x20(%rax), %edx
leaq -0xa8(%rbp), %rcx
leaq -0xc0(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0xd51f0
movl %eax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %esi
movq -0xa0(%rbp), %rax
movl 0x20(%rax), %edx
leaq -0xac(%rbp), %rcx
leaq -0xc8(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0xd51f0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb0(%rbp)
je 0xd5a80
cmpl $0x0, -0xb4(%rbp)
je 0xd5a80
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5a2d
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5a7b
movl -0xac(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5a4f
movl -0xac(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5a7b
movq -0xc0(%rbp), %rax
cmpq 0x2efa63(%rip), %rax    # 0x3c54c0
jle 0xd5a6f
movq -0xc8(%rbp), %rax
cmpq 0x2efa53(%rip), %rax    # 0x3c54c0
jg 0xd5a74
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5b16
cmpl $0x0, -0xb0(%rbp)
je 0xd5ac9
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5aab
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5ac7
movq -0xc0(%rbp), %rax
cmpq 0x2efa07(%rip), %rax    # 0x3c54c0
jg 0xd5ac0
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5b14
cmpl $0x0, -0xb4(%rbp)
je 0xd5b12
movl -0xac(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5af4
movl -0xac(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5b10
movq -0xc8(%rbp), %rax
cmpq 0x2ef9be(%rip), %rax    # 0x3c54c0
jg 0xd5b09
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5b12
jmp 0xd5b14
jmp 0xd5b16
cmpl $0x0, -0x34(%rbp)
jne 0xd5d1c
cmpl $0x0, -0xb0(%rbp)
jne 0xd5b8a
movq -0x98(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x98(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
movl -0x20(%rbp), %ecx
movq 0x10(%rbp), %r8
movl $0x2, %edx
xorl %r9d, %r9d
leaq -0xa8(%rbp), %rax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %rax, 0x8(%rsp)
callq 0xd5db0
movl %eax, -0xb0(%rbp)
movq 0x2ef941(%rip), %rax    # 0x3c54c0
addq $0xa, %rax
movq %rax, -0xc0(%rbp)
cmpl $0x0, -0xb4(%rbp)
jne 0xd5bfd
movq -0xa0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x88(%rbp)
movq -0xa0(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movl -0x20(%rbp), %ecx
movq 0x10(%rbp), %r8
movl $0x2, %edx
xorl %r9d, %r9d
leaq -0xac(%rbp), %rax
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %rax, 0x8(%rsp)
callq 0xd5db0
movl %eax, -0xb4(%rbp)
movq 0x2ef8ce(%rip), %rax    # 0x3c54c0
addq $0xa, %rax
movq %rax, -0xc8(%rbp)
cmpl $0x0, -0xb0(%rbp)
je 0xd5c84
cmpl $0x0, -0xb4(%rbp)
je 0xd5c84
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5c31
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5c7f
movl -0xac(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5c53
movl -0xac(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5c7f
movq -0xc0(%rbp), %rax
cmpq 0x2ef85f(%rip), %rax    # 0x3c54c0
jle 0xd5c73
movq -0xc8(%rbp), %rax
cmpq 0x2ef84f(%rip), %rax    # 0x3c54c0
jg 0xd5c78
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5d1a
cmpl $0x0, -0xb0(%rbp)
je 0xd5ccd
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5caf
movl -0xa8(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5ccb
movq -0xc0(%rbp), %rax
cmpq 0x2ef803(%rip), %rax    # 0x3c54c0
jg 0xd5cc4
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5d18
cmpl $0x0, -0xb4(%rbp)
je 0xd5d16
movl -0xac(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd5cf8
movl -0xac(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd5d14
movq -0xc8(%rbp), %rax
cmpq 0x2ef7ba(%rip), %rax    # 0x3c54c0
jg 0xd5d0d
jmp 0xd5d8f
movl $0x1, -0x34(%rbp)
jmp 0xd5d16
jmp 0xd5d18
jmp 0xd5d1a
jmp 0xd5d1c
cmpl $0x0, -0x34(%rbp)
je 0xd5d8b
movq -0x98(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x98(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x98(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
xorq $0x1, %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xd5d8d
jmp 0xd5d8f
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xd56a3
movl -0x30(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)