// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq 0xb8(%rdi), %rdi
movq %rdi, 0x10(%rsp)
callq 0xb0285
movq %rax, %rdi
cmpq $0x0, (%rax)
je 0x145103
xorl %eax, %eax
xorl %edx, %edx
movq %rdi, 0x18(%rsp)
movq 0x10(%rdi), %rcx
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0x10(%rcx,%rax,8)
je 0x1450f5
movq %rdx, 0x20(%rsp)
leaq (%rcx,%rax,8), %r15
movl 0x10(%r15), %esi
testl %esi, %esi
movq 0x10(%rsp), %rdx
je 0x14507d
xorl %r12d, %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ebx
movq $0x0, (%rsp)
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %r15, %r10
movq 0x18(%r15), %rdi
movq -0x8(%rdi,%rbx,8), %r11
movq %r11, %r9
shrq $0x21, %r9
movq 0x28(%r14), %rbp
movq 0x40(%r14), %rdi
movl (%rdi,%r9,4), %edi
leal 0x1(%rdi), %r15d
btq $0x20, %r11
movl (%rbp,%rdi,4), %edi
movl (%rbp,%r15,4), %r15d
movl %edi, %ebp
cmovael %r15d, %ebp
cmovbl %r15d, %edi
movl %esi, %esi
cmpq %rsi, %rbx
jae 0x144f4e
movq 0x28(%rdx), %rsi
shrq $0x20, %r11
movl 0x4(%rsi,%r11,8), %esi
leaq (%rsi,%rsi,2), %rsi
shlq $0x3, %rsi
addq 0x8(%rdx), %rsi
jmp 0x144f50
xorl %esi, %esi
movq 0x20(%rdx), %r11
movl (%r11,%r9,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
cmpl %r13d, %eax
jge 0x144fa9
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %r9
movq 0x10(%rsp), %rdx
shrl %edi
movl (%r9,%rdi,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rcx
addq %r9, %r8
movq (%r14), %r11
movq 0x80(%r14), %r15
movl (%r11,%rdi,4), %edi
incq %r15
cmpq %rdi, %r15
movl $0x0, %edi
cmovneq %rdi, %r9
addq %r9, (%rsp)
movq %r10, %r15
testq %rsi, %rsi
je 0x145068
cmpb $0x0, 0x12(%rsi)
je 0x144ff4
movl 0xc(%rsi), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rcx
addq %rsi, %r8
movl %ebp, %edi
shrl %edi
movq (%r14), %r9
movq 0x80(%r14), %r10
movl (%r9,%rdi,4), %edi
incq %r10
cmpq %rdi, %r10
movl $0x0, %edi
cmovneq %rdi, %rsi
addq %rsi, (%rsp)
movl %r12d, %eax
jmp 0x145068
cmpl %r13d, %eax
jl 0x145055
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %rsi
movl %ebp, %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 (%rsp), %r9
subq $0x10, %rsp
movq %r14, %rdi
movq %r10, %rsi
pushq %rax
pushq %r13
movq %r10, %r15
callq 0x144af3
movq 0x30(%rsp), %rdx
addq $0x20, %rsp
movl %ebx, %r13d
movl %r12d, %eax
xorl %ecx, %ecx
xorl %r8d, %r8d
movq $0x0, (%rsp)
incq %r12
movl 0x10(%r15), %esi
incq %rbx
cmpq %rsi, %r12
jb 0x144ef1
jmp 0x145092
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %r8d, %r8d
movq $0x0, (%rsp)
cmpl %r13d, %eax
jl 0x1450eb
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %rsi
movl %ebp, %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 (%rsp), %r9
subq $0x10, %rsp
movq %r14, %rdi
movq %r15, %rsi
pushq %rax
pushq %r13
callq 0x144af3
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
incl %edx
movq %rdx, %rax
cmpq %rdx, (%rdi)
ja 0x144ea7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0004