// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: test_single_node_bubble(asg_t*, unsigned int*, unsigned int, unsigned int, unsigned int)
int test_single_node_bubble(asg_t *g, uint32_t* nodes, uint32_t length, 
uint32_t startNode, uint32_t endNode)
{
    
    uint32_t i, v, w;
    uint32_t vEnd;
    int flag0, flag1;
    int n_reduced = 0;
    long long Len[2], longLen;
    long long longLen_thres = 4;
    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;
        }

        
        flag0 = asg_is_single_edge(g, av[0].v, v>>1);
        flag1 = asg_is_single_edge(g, av[1].v, v>>1);
        if(flag0 != 1 || flag1 != 1)
        {
            continue;
        }

        if(check_small_bubble(g, v, av[0].v, av[1].v, &(Len[0]), &(Len[1]), &vEnd))
        {

            if(if_node_exist(nodes, length, vEnd>>1) && ((vEnd>>1) != (endNode>>1)))
           {
               
               if(Len[0] == 1 && Len[1] != 1)
               {
                   w = av[0].v;
                   longLen = Len[1];
                    
                  /****************************may have bugs********************************/
                  if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0)
                   {
                    //    fprintf(stderr, "w>>1: %u, beg: %u, end: %u\n", 
                    //    w>>1, startNode>>1, endNode>>1);
                       asg_seq_del(g, w>>1);
                       n_reduced++;
                   }///up to here w is exactly overlapped in both directions
                   else if(longLen >= longLen_thres)
                   {
                       if(av[0].el == 1 && av[1].el == 1
                          &&
                          asg_arc_a(g, vEnd)[0].el == 1 && asg_arc_a(g, vEnd)[1].el == 1)
                       {
                           asg_seq_del(g, w>>1);
                           n_reduced++;
                       }
                   }
                   
                  /****************************may have bugs********************************/
                   
               }
               else if(Len[0] != 1 && Len[1] == 1)
               {
                   w = av[1].v;
                   longLen = Len[0];
                   
                  /****************************may have bugs********************************/
                  if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0)
                   {
                    //    fprintf(stderr, "w>>1: %u, beg: %u, end: %u\n", 
                    //    w>>1, startNode>>1, endNode>>1);
                       asg_seq_del(g, w>>1);
                       n_reduced++;
                   }///up to here w is exactly overlapped in both directions
                   else if(longLen >= longLen_thres)
                   {
                       if(av[0].el == 1 && av[1].el == 1
                          &&
                          asg_arc_a(g, vEnd)[0].el == 1 && asg_arc_a(g, vEnd)[1].el == 1)
                       {
                           asg_seq_del(g, w>>1);
                           n_reduced++;
                       }
                   }
                  /****************************may have bugs********************************/
               }
               else if(Len[0] == 1 && Len[1] == 1)
               {
                   w = av[0].v;
                   flag0 = asg_arc_a(g, w)[0].el + asg_arc_a(g, w^1)[0].el;
                   w = av[1].v;
                   flag1 = asg_arc_a(g, w)[0].el + asg_arc_a(g, w^1)[0].el;
                    ///>=2 means this is an exact overlap
                   if(flag0 < 2 && flag1 >= 2)
                   {
                       w = av[0].v;
                       
                       /****************************may have bugs********************************/
                        if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0)
                        {
                            // fprintf(stderr, "w>>1: %u, beg: %u, end: %u\n", 
                            // w>>1, startNode>>1, endNode>>1);
                            asg_seq_del(g, w>>1);
                            n_reduced++;
                        }
                        /****************************may have bugs********************************/
                   }

                   if(flag0 >= 2 && flag1 < 2)
                   {
                       w = av[1].v;
                      
                       /****************************may have bugs********************************/
                        if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0)
                        {
                            // fprintf(stderr, "w>>1: %u, beg: %u, end: %u\n", 
                            // w>>1, startNode>>1, endNode>>1);
                            asg_seq_del(g, w>>1);
                            n_reduced++;
                        }
                        /****************************may have bugs********************************/
                   }
               }
               else
               {
                   fprintf(stderr, "error\n");
               }
               
           }
        }

    }

    return n_reduced;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x4, -0x60(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0xd9502
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd8f78
movl -0x24(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xd8f7d
jmp 0xd94f4
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x24(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x24(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
cmpl $0x2, -0x64(%rbp)
je 0xd8fc0
jmp 0xd94f4
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x24(%rbp), %edx
shrl %edx
callq 0xd4770
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x24(%rbp), %edx
shrl %edx
callq 0xd4770
movl %eax, -0x34(%rbp)
cmpl $0x1, -0x30(%rbp)
jne 0xd8ffc
cmpl $0x1, -0x34(%rbp)
je 0xd9001
jmp 0xd94f4
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movq -0x70(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x70(%rbp), %rax
movl 0x20(%rax), %ecx
leaq -0x50(%rbp), %r8
leaq -0x50(%rbp), %r9
addq $0x8, %r9
leaq -0x2c(%rbp), %rax
movq %rax, (%rsp)
callq 0xd8a50
cmpl $0x0, %eax
je 0xd94f2
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
shrl %edx
callq 0xd45f0
cmpl $0x0, %eax
je 0xd94f0
movl -0x2c(%rbp), %eax
shrl %eax
movl -0x1c(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xd94f0
cmpq $0x1, -0x50(%rbp)
jne 0xd91a1
cmpq $0x1, -0x48(%rbp)
je 0xd91a1
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%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
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
je 0xd90e7
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xd9103
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd919c
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0xd919a
movq -0x70(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xd9198
movq -0x70(%rbp), %rax
movzbl 0x2a(%rax), %eax
cmpl $0x1, %eax
jne 0xd9198
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
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xd9198
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
movzbl 0x2a(%rax), %eax
cmpl $0x1, %eax
jne 0xd9198
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd919a
jmp 0xd919c
jmp 0xd94ee
cmpq $0x1, -0x50(%rbp)
je 0xd92de
cmpq $0x1, -0x48(%rbp)
jne 0xd92de
movq -0x70(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%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
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
je 0xd9224
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xd9240
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd92d9
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0xd92d7
movq -0x70(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xd92d5
movq -0x70(%rbp), %rax
movzbl 0x2a(%rax), %eax
cmpl $0x1, %eax
jne 0xd92d5
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
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xd92d5
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
movzbl 0x2a(%rax), %eax
cmpl $0x1, %eax
jne 0xd92d5
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd92d7
jmp 0xd92d9
jmp 0xd94ec
cmpq $0x1, -0x50(%rbp)
jne 0xd94d2
cmpq $0x1, -0x48(%rbp)
jne 0xd94d2
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x28(%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
movzbl 0x12(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x8(%rbp), %rdx
movq 0x28(%rdx), %rdx
movl -0x28(%rbp), %esi
xorl $0x1, %esi
movl %esi, %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x12(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x30(%rbp)
movq -0x70(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x28(%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
movzbl 0x12(%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x8(%rbp), %rdx
movq 0x28(%rdx), %rdx
movl -0x28(%rbp), %esi
xorl $0x1, %esi
movl %esi, %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x12(%rcx), %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x30(%rbp)
jge 0xd9442
cmpl $0x2, -0x34(%rbp)
jl 0xd9442
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x28(%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
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
je 0xd9429
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xd9440
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd9442
cmpl $0x2, -0x30(%rbp)
jl 0xd94d0
cmpl $0x2, -0x34(%rbp)
jge 0xd94d0
movq -0x70(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x28(%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
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
je 0xd94b7
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x28(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0xd94ce
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
shrl %esi
callq 0xd9510
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xd94d0
jmp 0xd94ea
movq 0x2eab07(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x2a9102(%rip), %rsi    # 0x3825e5
movb $0x0, %al
callq 0x22a0
jmp 0xd94ec
jmp 0xd94ee
jmp 0xd94f0
jmp 0xd94f2
jmp 0xd94f4
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xd8f43
movl -0x38(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop