// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: test_single_node_bubble_directly(asg_t*, unsigned int, long long, ma_hit_t_alloc*)
int test_single_node_bubble_directly(asg_t *g, uint32_t v, long long longLen_thres, ma_hit_t_alloc* sources)
{
    uint32_t w, vEnd;
    int flag0, flag1;
    int n_reduced = 0;
    long long Len[2], longLen;
    ///long long longLen_thres = 4;

    uint32_t nv = asg_arc_n(g, v);
    asg_arc_t *av = asg_arc_a(g, v);
    if(nv != 2)
    {
        return 0;
    }

    
    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)
    {
        return 0;
    }

    if(check_small_bubble(g, v, av[0].v, av[1].v, &(Len[0]), &(Len[1]), &vEnd))
    {
        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)
            if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0 || sources[w>>1].is_abnormal == 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)
            if(asg_arc_a(g, w)[0].el == 0 || asg_arc_a(g, w^1)[0].el == 0 || sources[w>>1].is_abnormal == 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)
        {
            flag0 = sources[av[0].v>>1].is_abnormal;
            flag1 = sources[av[1].v>>1].is_abnormal;

            if(flag0 == 1 && flag1 == 0)
            {
                asg_seq_del(g, av[0].v>>1);
                n_reduced++;
            }
            else if(flag0 == 0 && flag1 == 1)
            {
                asg_seq_del(g, av[1].v>>1);
                n_reduced++;
            }
            else
            {
                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)
                    {
                        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)
                    {
                        asg_seq_del(g, w>>1);
                        n_reduced++;
                    }
                    /****************************may have bugs********************************/
                }
            }
        }
        
    }
    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %r12d
movq 0x28(%rdi), %r10
movl %esi, %eax
movq (%r10,%rax,8), %rsi
xorl %ebp, %ebp
cmpl $0x2, %esi
jne 0xa94dc
movq %rdi, %rbx
movq %rdx, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x8(%rdi), %rax
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rdx
leaq (%rax,%rdx,8), %r13
movl 0x8(%r13), %edx
movl %r12d, %ebp
shrl %ebp
xorq $0x1, %rdx
movq (%r10,%rdx,8), %r15
testl %r15d, %r15d
jle 0xa943e
movq %r15, %rsi
shrq $0x20, %rsi
movl %r15d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq (%rsi,%rsi,2), %rsi
leaq (%rax,%rsi,8), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x1, %r8d
movl (%rdi), %r9d
shrl %r9d
cmpl %ebp, %r9d
cmovel %r8d, %esi
addq $0x18, %rdi
decq %rdx
jne 0xa90e3
testl %esi, %esi
je 0xa9441
movl 0x20(%r13), %edx
xorq $0x1, %rdx
movq (%r10,%rdx,8), %r14
testl %r14d, %r14d
jle 0xa946e
movq %r14, %rdx
shrq $0x20, %rdx
movl %r14d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0x8, %rdx
xorl %eax, %eax
movl $0x1, %esi
movl (%rdx), %edi
shrl %edi
cmpl %ebp, %edi
cmovel %esi, %eax
addq $0x18, %rdx
decq %rcx
jne 0xa9139
testl %eax, %eax
je 0xa9471
xorl %ebp, %ebp
cmpl $0x1, %r15d
jne 0xa94dc
cmpl $0x1, %r14d
jne 0xa94dc
movl 0x8(%r13), %edx
movl 0x20(%r13), %ecx
leaq 0x28(%rsp), %r9
leaq 0xc(%rsp), %rax
movq %rax, (%rsp)
leaq 0x20(%rsp), %r8
movq %rbx, %rdi
movl %r12d, %esi
callq 0xa8187
testl %eax, %eax
je 0xa94dc
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
cmpq $0x1, %rcx
sete %al
cmpq $0x1, %rdx
setne %sil
andb %al, %sil
cmpb $0x1, %sil
jne 0xa92c5
movl 0x8(%r13), %eax
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rdi
movl 0x4(%rdi,%rax,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rcx,%rsi,8)
je 0xa9207
movq %rax, %rsi
xorq $0x1, %rsi
movl 0x4(%rdi,%rsi,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rcx,%rsi,8)
je 0xa9207
movl %eax, %esi
shrl %esi
leaq (%rsi,%rsi,2), %r8
movq 0x10(%rsp), %r9
cmpb $0x1, 0x11(%r9,%r8,8)
jne 0xa9496
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
movb $0x1, %cl
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rax, %rdx
movq 0x28(%rbx), %rdi
movq (%rdi,%rdx,8), %r8
movq %r8, %rdi
andq %rsi, %rdi
je 0xa92ad
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rbx), %r8
xorl %r9d, %r9d
leaq (%r9,%r9,2), %r10
orb $-0x80, 0xf(%r8,%r10,8)
movl 0x8(%r8,%r10,8), %r10d
xorq $0x1, %r10
movq 0x28(%rbx), %r11
movq (%r11,%r10,8), %r11
movq %r11, %r10
andq %rsi, %r10
je 0xa92a5
movq 0x8(%rbx), %r14
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r14,%r11,8), %r11
addq $0xc, %r11
xorl %r14d, %r14d
movl -0x4(%r11,%r14), %ebp
xorl %edx, %ebp
cmpl $0x1, %ebp
jne 0xa929c
orb $-0x80, 0x3(%r11,%r14)
addq $0x18, %r14
cmpq %r14, %r10
jne 0xa928a
incq %r9
cmpq %rdi, %r9
jne 0xa9248
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xa9222
jmp 0xa94d7
cmpq $0x1, %rcx
setne %al
cmpq $0x1, %rdx
sete %sil
andb %al, %sil
cmpb $0x1, %sil
jne 0xa93e9
movl 0x20(%r13), %eax
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rdi
movl 0x4(%rdi,%rax,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rdx,%rsi,8)
je 0xa932b
movq %rax, %rsi
xorq $0x1, %rsi
movl 0x4(%rdi,%rsi,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rdx,%rsi,8)
je 0xa932b
movl %eax, %esi
shrl %esi
leaq (%rsi,%rsi,2), %r8
movq 0x10(%rsp), %r9
cmpb $0x1, 0x11(%r9,%r8,8)
jne 0xa94ed
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
movb $0x1, %cl
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rax, %rdx
movq 0x28(%rbx), %rdi
movq (%rdi,%rdx,8), %r8
movq %r8, %rdi
andq %rsi, %rdi
je 0xa93d1
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rbx), %r8
xorl %r9d, %r9d
leaq (%r9,%r9,2), %r10
orb $-0x80, 0xf(%r8,%r10,8)
movl 0x8(%r8,%r10,8), %r10d
xorq $0x1, %r10
movq 0x28(%rbx), %r11
movq (%r11,%r10,8), %r11
movq %r11, %r10
andq %rsi, %r10
je 0xa93c9
movq 0x8(%rbx), %r14
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%r14,%r11,8), %r11
addq $0xc, %r11
xorl %r14d, %r14d
movl -0x4(%r11,%r14), %ebp
xorl %edx, %ebp
cmpl $0x1, %ebp
jne 0xa93c0
orb $-0x80, 0x3(%r11,%r14)
addq $0x18, %r14
cmpq %r14, %r10
jne 0xa93ae
incq %r9
cmpq %rdi, %r9
jne 0xa936c
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xa9346
jmp 0xa94d7
xorq $0x1, %rdx
xorq $0x1, %rcx
orq %rdx, %rcx
jne 0xa94dc
movl 0x8(%r13), %edx
movl 0x20(%r13), %ecx
movl %edx, %eax
shrl %eax
leaq (%rax,%rax,2), %rsi
movq 0x10(%rsp), %r9
movb 0x11(%r9,%rsi,8), %dil
movl %ecx, %esi
shrl %esi
leaq (%rsi,%rsi,2), %r8
movb 0x11(%r9,%r8,8), %r8b
cmpb $0x1, %dil
jne 0xa94c4
testb %r8b, %r8b
jne 0xa94c4
movq %rbx, %rdi
movl %eax, %esi
jmp 0xa94d2
xorl %r15d, %r15d
movq 0x1ebb98(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0x18fcc8(%rip), %rdi    # 0x23911a
movl $0xa, %esi
movl $0x1, %edx
callq 0x2410
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %r10
jmp 0xa9101
xorl %r14d, %r14d
movq 0x1ebb68(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0x18fc98(%rip), %rdi    # 0x23911a
movl $0xa, %esi
movl $0x1, %edx
callq 0x2410
jmp 0xa9153
cmpq 0x18(%rsp), %rdx
jl 0xa94dc
cmpb $0x1, 0x12(%r13)
jne 0xa94dc
cmpb $0x1, 0x2a(%r13)
jne 0xa94dc
movl 0xc(%rsp), %eax
movl 0x4(%rdi,%rax,8), %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rcx,%rax,8)
jne 0xa94dc
leaq (%rcx,%rax,8), %rax
jmp 0xa9519
testb %dil, %dil
jne 0xa9521
cmpb $0x1, %r8b
jne 0xa9521
movq %rbx, %rdi
callq 0xa8fa1
movl $0x1, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq 0x18(%rsp), %rcx
jl 0xa94dc
cmpb $0x1, 0x12(%r13)
jne 0xa94dc
cmpb $0x1, 0x2a(%r13)
jne 0xa94dc
movl 0xc(%rsp), %eax
movl 0x4(%rdi,%rax,8), %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rdx,%rax,8)
jne 0xa94dc
leaq (%rdx,%rax,8), %rax
cmpb $0x1, 0x2a(%rax)
je 0xa94cf
jmp 0xa94dc
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r8
movl 0x4(%r8,%rdx,8), %r9d
leaq (%r9,%r9,2), %r9
movzbl 0x12(%rdi,%r9,8), %r9d
xorl $0x1, %edx
movl 0x4(%r8,%rdx,8), %edx
leaq (%rdx,%rdx,2), %rdx
movzbl 0x12(%rdi,%rdx,8), %edx
addl %r9d, %edx
movl 0x4(%r8,%rcx,8), %r9d
leaq (%r9,%r9,2), %r9
movzbl 0x12(%rdi,%r9,8), %r9d
xorl $0x1, %ecx
movl 0x4(%r8,%rcx,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movzbl 0x12(%rdi,%rcx,8), %ecx
addl %r9d, %ecx
cmpl $0x1, %edx
ja 0xa957d
cmpl $0x2, %ecx
jae 0xa9434
cmpl $0x2, %edx
jb 0xa94dc
cmpl $0x1, %ecx
jbe 0xa94cf
jmp 0xa94dc