// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: detect_bub_graph(bubble_type*, asg_t*)
void detect_bub_graph(bubble_type* bub, asg_t *untig_sg)
{
    asg_t *bg = bub->b_g;
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen(bub->b_g);
    ///debug_bub_utg(bub, ug, bg, bub->check_het);
    uint32_t i, k, rId, ori, root, r_root;
    int beg_idx, end_idx;
    uint64_t pLen, rLEN, r_hetLen;
    ma_utg_t *u = NULL;
    asg_arc_t *t = NULL;
    for (i = 0; i < ug->u.n; i++)///bubble chain graph; bubbles within the same chain have been merged
    {
        u = &(ug->u.a[i]);
        if(u->n == 0) continue;
        ///u is a bubble chain
        for (k = pLen = rLEN = r_hetLen = beg_idx = 0, end_idx = -1; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            ori = u->a[k]>>32&1;
            get_bubbles(bub, rId, ori == 1?&root:&r_root, ori == 0?&root:&r_root, NULL, NULL, NULL);
            
            t = NULL;
            if(k+1 < u->n) t = &(arc_first(bg, u->a[k]>>32));//edge between two bubbles within the same chain

            pLen += bg->seq[rId].len;///path length in bubble
            if(end_idx < beg_idx) ///first bubble
            {
                pLen += untig_sg->seq[r_root>>1].len;
                rLEN += untig_sg->seq[r_root>>1].len;
                if(IF_HET(r_root>>1, *bub)) r_hetLen += untig_sg->seq[r_root>>1].len;
            }

            if(t)
            {
                if(t->el == 0)///there is tangle between two bubbles
                {
                    if(end_idx >= beg_idx)
                    {
                        pLen += untig_sg->seq[root>>1].len;
                        rLEN += untig_sg->seq[root>>1].len;
                        if(IF_HET(root>>1, *bub)) r_hetLen += untig_sg->seq[root>>1].len;
                        determine_bub_idx(bub, u, untig_sg, pLen, rLEN, r_hetLen, beg_idx, end_idx, bub->check_het);
                    } 
                    
                    pLen = rLEN = r_hetLen = 0;
                    beg_idx = k + 1; end_idx = k;
                }
                else///two bubbles directly connected with each other
                {
                    pLen += t->ol;
                    rLEN += t->ol;
                    if(IF_HET(root>>1, *bub)) r_hetLen += t->ol;
                    end_idx = k;
                }
            }
        }
        
        if(end_idx >= beg_idx)
        {
            pLen += untig_sg->seq[root>>1].len;
            rLEN += untig_sg->seq[root>>1].len;
            if(IF_HET(root>>1, *bub)) r_hetLen += untig_sg->seq[root>>1].len;
            determine_bub_idx(bub, u, untig_sg, pLen, rLEN, r_hetLen, beg_idx, end_idx, bub->check_het);
        }
    }
    
    ma_ug_destroy(ug);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
movq 0xb8(%rdi), %r15
movq %r15, %rdi
callq 0xb10cd
movq %rax, %r12
cmpq $0x0, (%rax)
je 0x14b73a
xorl %eax, %eax
xorl %r13d, %r13d
movq %r12, 0x28(%rsp)
movq 0x10(%r12), %rcx
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %edx
movl %edx, 0x14(%rsp)
testl %edx, %edx
je 0x14b72a
movq %r13, 0x30(%rsp)
leaq (%rcx,%rax,8), %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0x1, %ebp
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rdx
movq 0x18(%rdx), %rdi
movq -0x8(%rdi,%rbp,8), %r9
movq %r9, %r13
shrq $0x21, %r13
movq 0x28(%r14), %rdi
movq 0x40(%r14), %r11
movl (%r11,%r13,4), %r11d
leal 0x1(%r11), %ebx
btq $0x20, %r9
movl (%rdi,%r11,4), %r11d
movl (%rdi,%rbx,4), %edi
movl %r11d, %ebx
cmovael %edi, %ebx
cmovbl %edi, %r11d
movl 0x14(%rsp), %edi
cmpq %rdi, %rbp
jae 0x14b58c
movq 0x28(%r15), %rsi
shrq $0x20, %r9
movl 0x4(%rsi,%r9,8), %esi
leaq (%rsi,%rsi,2), %r9
shlq $0x3, %r9
addq 0x8(%r15), %r9
jmp 0x14b58f
xorl %r9d, %r9d
movq 0x20(%r15), %rsi
movl (%rsi,%r13,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rcx
cmpl 0x8(%rsp), %r10d
jge 0x14b5ea
movq 0x18(%rsp), %rdx
movq 0x20(%rdx), %rsi
shrl %r11d
movl (%rsi,%r11,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rcx
addq %rsi, %r8
movq (%r14), %r13
movq %r15, %rdx
movq 0x80(%r14), %r15
movl (%r13,%r11,4), %r11d
incq %r15
cmpq %r11, %r15
movq %rdx, %r15
movl $0x0, %edx
cmovneq %rdx, %rsi
addq %rsi, %rax
testq %r9, %r9
je 0x14b6b3
cmpb $0x0, 0x12(%r9)
je 0x14b635
movl 0xc(%r9), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rcx
addq %rsi, %r8
movl %ebx, %r9d
shrl %r9d
movq (%r14), %r10
movq 0x80(%r14), %r11
movl (%r10,%r9,4), %r9d
incq %r11
cmpq %r9, %r11
movl $0x0, %edx
cmovneq %rdx, %rsi
addq %rsi, %rax
movl %r12d, %r10d
jmp 0x14b6b3
movq %rax, %r13
movq 0x8(%rsp), %rax
cmpl %eax, %r10d
jl 0x14b6a2
movq 0x18(%rsp), %rdx
movq 0x20(%rdx), %rsi
movl %ebx, %edi
shrl %edi
movl (%rsi,%rdi,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
addq %r9, %r8
movq (%r14), %rsi
movq 0x80(%r14), %r11
movl (%rsi,%rdi,4), %esi
incq %r11
cmpq %rsi, %r11
movl $0x0, %esi
cmovneq %rsi, %r9
addq %r13, %r9
subq $0x10, %rsp
movq %r14, %rdi
movq 0x30(%rsp), %r13
movq %r13, %rsi
pushq %r10
pushq %rax
callq 0x14b13e
addq $0x20, %rsp
movl 0x10(%r13), %edi
movl %edi, 0x14(%rsp)
movl %ebp, %eax
movq %rax, 0x8(%rsp)
movl %r12d, %r10d
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
incq %r12
incq %rbp
cmpq %rdi, %r12
jb 0x14b52d
movq 0x8(%rsp), %r11
cmpl %r11d, %r10d
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r13
jl 0x14b72a
movq 0x18(%rsp), %rdx
movq 0x20(%rdx), %rsi
shrl %ebx
movl (%rsi,%rbx,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
addq %r9, %r8
movq (%r14), %rsi
movq 0x80(%r14), %rdi
movl (%rsi,%rbx,4), %esi
incq %rdi
cmpq %rsi, %rdi
movl $0x0, %esi
cmovneq %rsi, %r9
addq %rax, %r9
subq $0x10, %rsp
movq %r14, %rdi
movq 0x30(%rsp), %rsi
pushq %r10
pushq %r11
callq 0x14b13e
addq $0x20, %rsp
incl %r13d
movq %r13, %rax
cmpq %r13, (%r12)
ja 0x14b4e8
movq %r12, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0761