// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d
movq 0x28(%rdi), %rax
movl %esi, %esi
movq (%rax,%rsi,8), %rsi
xorl %ebp, %ebp
cmpl $0x2, %esi
jne 0xa84a6
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %r13
movl 0x8(%r13), %esi
movl %r14d, %r12d
shrl %r12d
xorq $0x1, %rsi
movq (%rax,%rsi,8), %rax
testl %eax, %eax
movq %rdx, 0x18(%rsp)
jle 0xa80fd
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %rsi
addq $0x8, %rsi
xorl %edx, %edx
movl $0x1, %edi
movl (%rsi), %r8d
shrl %r8d
cmpl %r12d, %r8d
cmovel %edi, %edx
addq $0x18, %rsi
decq %r9
jne 0xa80da
testl %edx, %edx
sete %dl
cmpl $0x1, %eax
setne %r15b
jmp 0xa8102
movb $0x1, %r15b
movb $0x1, %dl
testb %dl, %dl
jne 0xa8436
movl 0x20(%r13), %eax
movq 0x28(%rbx), %rdx
xorq $0x1, %rax
movq (%rdx,%rax,8), %rax
testl %eax, %eax
jle 0xa8169
movq 0x8(%rbx), %rdx
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %rsi
addq $0x8, %rsi
xorl %edx, %edx
movl $0x1, %edi
movl (%rsi), %r8d
shrl %r8d
cmpl %r12d, %r8d
cmovel %edi, %edx
addq $0x18, %rsi
decq %r9
jne 0xa8146
testl %edx, %edx
sete %dl
cmpl $0x1, %eax
setne %r12b
jmp 0xa816e
movb $0x1, %r12b
movb $0x1, %dl
testb %dl, %dl
jne 0xa844a
orb %r12b, %r15b
jne 0xa84a6
movq %rcx, %r15
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 %r14d, %esi
callq 0xa7139
testl %eax, %eax
je 0xa84a6
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 0xa82d5
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 0xa821b
movq %rax, %rsi
xorq $0x1, %rsi
movl 0x4(%rdi,%rsi,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rcx,%rsi,8)
je 0xa821b
movl %eax, %esi
shrl %esi
leaq (%rsi,%rsi,2), %r8
cmpb $0x1, 0x11(%r15,%r8,8)
jne 0xa845e
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq (%rcx,%rax), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %rdx, %rdi
je 0xa82c0
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 %rdx, %r10
je 0xa82b8
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 %esi, %ebp
cmpl $0x1, %ebp
jne 0xa82af
orb $-0x80, 0x3(%r11,%r14)
addq $0x18, %r14
cmpq %r14, %r10
jne 0xa829d
incq %r9
cmpq %rdi, %r9
jne 0xa825b
leaq 0x1(%rcx), %rsi
testq %rcx, %rcx
movq %rsi, %rcx
je 0xa8234
jmp 0xa84a1
cmpq $0x1, %rcx
setne %al
cmpq $0x1, %rdx
sete %sil
andb %al, %sil
cmpb $0x1, %sil
movq %r15, %r9
jne 0xa83f3
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 0xa8339
movq %rax, %rsi
xorq $0x1, %rsi
movl 0x4(%rdi,%rsi,8), %esi
leaq (%rsi,%rsi,2), %rsi
cmpb $0x0, 0x12(%rdx,%rsi,8)
je 0xa8339
movl %eax, %esi
shrl %esi
leaq (%rsi,%rsi,2), %r8
cmpb $0x1, 0x11(%r9,%r8,8)
jne 0xa84b7
movl %eax, %ecx
shrl %ecx
movq 0x20(%rbx), %rdx
orl $0x80000000, (%rdx,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq (%rcx,%rax), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %rdx, %rdi
je 0xa83de
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 %rdx, %r10
je 0xa83d6
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 %esi, %ebp
cmpl $0x1, %ebp
jne 0xa83cd
orb $-0x80, 0x3(%r11,%r14)
addq $0x18, %r14
cmpq %r14, %r10
jne 0xa83bb
incq %r9
cmpq %rdi, %r9
jne 0xa8379
leaq 0x1(%rcx), %rsi
testq %rcx, %rcx
movq %rsi, %rcx
je 0xa8352
jmp 0xa84a1
xorq $0x1, %rdx
xorq $0x1, %rcx
orq %rdx, %rcx
jne 0xa84a6
movl 0x8(%r13), %edx
movl 0x20(%r13), %ecx
movl %edx, %esi
shrl %esi
leaq (%rsi,%rsi,2), %rax
movb 0x11(%r9,%rax,8), %dil
movl %ecx, %eax
shrl %eax
leaq (%rax,%rax,2), %r8
movb 0x11(%r9,%r8,8), %r8b
cmpb $0x1, %dil
jne 0xa848c
testb %r8b, %r8b
jne 0xa848c
movq %rbx, %rdi
jmp 0xa849c
movq %rcx, 0x10(%rsp)
callq 0x101d0
movq 0x10(%rsp), %rcx
jmp 0xa810a
movq %rcx, 0x10(%rsp)
callq 0x101ec
movq 0x10(%rsp), %rcx
jmp 0xa8176
cmpq 0x18(%rsp), %rdx
jl 0xa84a6
cmpb $0x1, 0x12(%r13)
jne 0xa84a6
cmpb $0x1, 0x2a(%r13)
jne 0xa84a6
movl 0xc(%rsp), %eax
movl 0x4(%rdi,%rax,8), %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rcx,%rax,8)
jne 0xa84a6
leaq (%rcx,%rax,8), %rax
jmp 0xa84e3
testb %dil, %dil
jne 0xa84ef
cmpb $0x1, %r8b
jne 0xa84ef
movq %rbx, %rdi
movl %eax, %esi
callq 0xa7fa4
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 0xa84a6
cmpb $0x1, 0x12(%r13)
jne 0xa84a6
cmpb $0x1, 0x2a(%r13)
jne 0xa84a6
movl 0xc(%rsp), %eax
movl 0x4(%rdi,%rax,8), %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rdx,%rax,8)
jne 0xa84a6
leaq (%rdx,%rax,8), %rax
cmpb $0x1, 0x2a(%rax)
je 0xa8431
jmp 0xa84a6
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r8
movl 0x4(%r8,%rdx,8), %eax
leaq (%rax,%rax,2), %rax
movzbl 0x12(%rdi,%rax,8), %eax
xorl $0x1, %edx
movl 0x4(%r8,%rdx,8), %edx
leaq (%rdx,%rdx,2), %rdx
movzbl 0x12(%rdi,%rdx,8), %edx
leal (%rdx,%rax), %r14d
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), %r15d
addl %r9d, %r15d
xorl %ebp, %ebp
cmpl $0x1, %r14d
ja 0xa8561
cmpl $0x2, %r15d
jb 0xa8561
testb %al, %al
je 0xa8554
testb %dl, %dl
jne 0xa8561
movq %rbx, %rdi
callq 0xa7fa4
movl $0x1, %ebp
cmpl $0x2, %r14d
jb 0xa84a6
cmpl $0x1, %r15d
ja 0xa84a6
movl 0x20(%r13), %esi
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movl 0x4(%rcx,%rsi,8), %edx
leaq (%rdx,%rdx,2), %rdx
cmpb $0x0, 0x12(%rax,%rdx,8)
je 0xa85aa
movq %rsi, %rdx
xorq $0x1, %rdx
movl 0x4(%rcx,%rdx,8), %ecx
leaq (%rcx,%rcx,2), %rcx
cmpb $0x0, 0x12(%rax,%rcx,8)
jne 0xa84a6
shrl %esi
movq %rbx, %rdi
callq 0xa7fa4
incl %ebp
jmp 0xa84a6