// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, 0x34(%rsp)
movl %r8d, %ebp
movl %ecx, %r14d
movq %rsi, %r8
movq %rdi, %r13
shrl %r14d
shrl %ebp
movl %edx, 0x3c(%rsp)
movl %edx, %edi
xorl %r12d, %r12d
movl $0x0, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, (%rsp)
cmpq %r12, %rdi
je 0x6d6e9
movl (%r8,%r12,4), %eax
movl %eax, %r15d
shrl %r15d
cmpl %r14d, %r15d
sete %cl
cmpl %ebp, %r15d
sete %dl
orb %cl, %dl
jne 0x6d47b
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rax
cmpl $0x2, %eax
jne 0x6d47b
shrq $0x20, %rax
imulq $0x18, %rax, %rbx
addq 0x8(%r13), %rbx
movl 0x8(%rbx), %esi
cmpl 0x20(%rbx), %esi
je 0x6d47b
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x60(%rsp)
orq $-0x1, 0x70(%rsp)
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jg 0x6d472
movl 0x20(%rbx), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jg 0x6d472
movl 0x8(%rbx), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cltq
andq $0x0, 0x60(%rsp,%rax,8)
movl 0x20(%rbx), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cltq
movq $0x1, 0x60(%rsp,%rax,8)
movq 0x70(%rsp), %rax
cmpq $-0x1, 0x68(%rsp)
sete %cl
cmpq $-0x1, %rax
sete %dl
orb %cl, %dl
jne 0x6d472
imulq $0x18, %rax, %rax
cmpl $0x0, 0xc(%rbx,%rax)
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
js 0x6d47b
movq %rax, 0x88(%rsp)
addq %rbx, %rax
movq %rax, 0x80(%rsp)
movl 0x8(%rax), %r15d
movq %r15, %rax
xorq $0x1, %rax
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq 0x8(%r13), %rcx
cmpl $0x2, %eax
movq %rcx, 0x18(%rsp)
jne 0x6d483
orq $-0x1, 0x50(%rsp)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movl 0x8(%rcx), %esi
shrl %r15d
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jg 0x6d472
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cmpl $0x2, %eax
jg 0x6d472
movq 0x18(%rsp), %rax
movl 0x8(%rax), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cltq
andq $0x0, 0x40(%rsp,%rax,8)
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
movq %r13, %rdi
movl %r15d, %edx
callq 0x6c8a1
cltq
movq $0x1, 0x40(%rsp,%rax,8)
cmpq $-0x1, 0x48(%rsp)
je 0x6d472
cmpq $-0x1, 0x50(%rsp)
movq 0x8(%rsp), %r8
je 0x6d477
movq %r8, %rdi
movl 0x3c(%rsp), %esi
movl %r15d, %edx
callq 0x6c7f0
cmpl %ebp, %r15d
je 0x6d472
testl %eax, %eax
je 0x6d472
andl $0x0, 0x14(%rsp)
andl $0x0, 0x10(%rsp)
andq $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
movl 0x8(%rbx), %esi
movl 0x20(%rbx), %edx
movq %r13, %rdi
leaq 0x14(%rsp), %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
callq 0x6ce93
movl %eax, %r15d
movq 0x18(%rsp), %rax
movl 0x8(%rax), %esi
movl 0x20(%rax), %edx
movq %r13, %rdi
leaq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %r9d, %r9d
callq 0x6ce93
movl %eax, %edx
testl %r15d, %r15d
je 0x6d4a9
testl %edx, %edx
je 0x6d4a9
movl 0x14(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %dl
testb %dl, %cl
jne 0x6d4e6
movl 0x10(%rsp), %ecx
shrl %ecx
cmpl %r14d, %ecx
setne %dl
cmpl %ebp, %ecx
setne %cl
testb %cl, %dl
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
jne 0x6d614
jmp 0x6d640
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
incq %r12
jmp 0x6d1e5
movq 0x1e5b56(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x1764d9(%rip), %rdi    # 0x1e396d
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x18(%rsp), %rcx
jmp 0x6d317
testl %r15d, %r15d
je 0x6d4f4
movl 0x14(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
movb $0x1, %r15b
testb %al, %cl
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
jne 0x6d594
movq 0x28(%rsp), %rax
cmpq 0x1e6f41(%rip), %rax    # 0x254420
jle 0x6d47b
jmp 0x6d65d
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
jmp 0x6d614
testl %edx, %edx
je 0x6d52d
movl 0x10(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
testb %al, %cl
jne 0x6d52d
movq 0x20(%rsp), %rax
cmpq 0x1e6f07(%rip), %rax    # 0x254420
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
jle 0x6d47b
jmp 0x6d65d
movl 0x8(%rbx), %eax
movl %eax, 0x9c(%rsp)
movl 0x20(%rbx), %eax
movl %eax, 0xa0(%rsp)
movq %r13, %rdi
leaq 0x9c(%rsp), %rsi
movl %edx, %r15d
pushq $0x2
popq %rdx
movl 0x34(%rsp), %ecx
movq 0xe0(%rsp), %r8
xorl %r9d, %r9d
leaq 0x14(%rsp), %rax
pushq %rax
pushq $-0x1
callq 0x6d6ff
movl %r15d, %edx
addq $0x10, %rsp
movq 0x1e6ea4(%rip), %rcx    # 0x254420
addq $0xa, %rcx
movq %rcx, 0x28(%rsp)
testl %eax, %eax
setne %r15b
movq 0x8(%rsp), %r8
movq (%rsp), %rdi
movb $0x1, %al
testl %edx, %edx
jne 0x6d600
movq 0x18(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x90(%rsp)
movl 0x20(%rcx), %eax
movl %eax, 0x94(%rsp)
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
pushq $0x2
popq %rdx
movl 0x34(%rsp), %ecx
movq 0xe0(%rsp), %r8
xorl %r9d, %r9d
leaq 0x10(%rsp), %rax
pushq %rax
pushq $-0x1
callq 0x6d6ff
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r8
addq $0x10, %rsp
movq 0x1e6e2e(%rip), %rcx    # 0x254420
addq $0xa, %rcx
movq %rcx, 0x20(%rsp)
testl %eax, %eax
setne %al
movl %r15d, %ecx
andb %al, %cl
cmpb $0x1, %cl
jne 0x6d69b
movl 0x14(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
testb %al, %cl
jne 0x6d47b
movl 0x10(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
testb %al, %cl
jne 0x6d47b
movq 0x1e6dd9(%rip), %rax    # 0x254420
cmpq %rax, 0x28(%rsp)
jle 0x6d47b
cmpq %rax, 0x20(%rsp)
jle 0x6d47b
movq 0x88(%rsp), %rax
orb $-0x80, 0xf(%rbx,%rax)
movq 0x80(%rsp), %rax
movl 0x4(%rax), %edx
movl 0x8(%rax), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %r13, %rdi
pushq $0x1
popq %rcx
callq 0x6db50
movq (%rsp), %rdi
movq 0x8(%rsp), %r8
incl 0x38(%rsp)
jmp 0x6d47b
testb %r15b, %r15b
je 0x6d6be
movl 0x14(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
testb %al, %cl
jne 0x6d47b
jmp 0x6d4d3
testb %al, %al
je 0x6d47b
movl 0x10(%rsp), %eax
shrl %eax
cmpl %r14d, %eax
setne %cl
cmpl %ebp, %eax
setne %al
testb %al, %cl
jne 0x6d47b
movq 0x20(%rsp), %rax
jmp 0x6d4d8
movl 0x38(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq