// File: /workspace/github_cpp_makefile_star2_O2/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), %r15
movq %r15, %rdi
callq 0x76d91
movq %rax, %rdi
xorl %edx, %edx
movq %rax, 0x10(%rsp)
movl %edx, %edx
cmpq %rdx, (%rdi)
jbe 0x1039e3
movq 0x10(%rdi), %r13
imulq $0x28, %rdx, %rcx
movl 0x10(%r13,%rcx), %eax
testl %eax, %eax
je 0x1039dc
movq %rdx, 0x18(%rsp)
addq %rcx, %r13
xorl %ebx, %ebx
pushq $-0x1
popq %rdi
movl %eax, %esi
movl %ebx, %edx
movl %edi, %r10d
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
movq %rdx, 0x20(%rsp)
cmpq %rsi, %rbx
jae 0x103977
movq 0x18(%r13), %rdi
movq (%rdi,%rbx,8), %rdi
movq %rdi, %r11
shrq $0x21, %r11
movq 0x28(%r14), %rbp
movq 0x40(%r14), %r9
movl (%r9,%r11,4), %r9d
leal 0x1(%r9), %r12d
btq $0x20, %rdi
movl (%rbp,%r9,4), %r9d
movl (%rbp,%r12,4), %r12d
movl %r9d, %ebp
cmovael %r12d, %ebp
cmovbl %r12d, %r9d
incq %rbx
cmpq %rsi, %rbx
jae 0x103855
movq 0x28(%r15), %r12
shrq $0x20, %rdi
movl 0x4(%r12,%rdi,8), %edi
imulq $0x18, %rdi, %rdi
addq 0x8(%r15), %rdi
jmp 0x103857
xorl %edi, %edi
movq 0x20(%r15), %r12
movl (%r12,%r11,8), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
addq %r11, %rcx
cmpl %edx, %r10d
jge 0x1038b6
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %r11
shrl %r9d
movl (%r11,%r9,8), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
addq %r11, %rcx
addq %r11, %r8
movq (%r14), %r12
movq %r13, %rdx
movq 0x80(%r14), %r13
movl (%r12,%r9,4), %r9d
incq %r13
cmpq %r9, %r13
movq %rdx, %r13
movl $0x0, %edx
cmovneq %rdx, %r11
movq 0x20(%rsp), %rdx
addq %r11, %rax
testq %rdi, %rdi
je 0x1037f4
cmpb $0x0, 0x12(%rdi)
je 0x103908
leal -0x1(%rbx), %r10d
movl 0xc(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq %rdi, %rcx
addq %rdi, %r8
movl %ebp, %r9d
shrl %r9d
movq (%r14), %r11
movq 0x80(%r14), %r12
movl (%r11,%r9,4), %r9d
incq %r12
cmpq %r9, %r12
movl $0x0, %edx
cmovneq %rdx, %rdi
movq 0x20(%rsp), %rdx
addq %rdi, %rax
jmp 0x1037f4
leal -0x1(%rbx), %edi
cmpl %edx, %r10d
jl 0x1037e3
movq %rdx, %r12
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 %rax, %r9
subq $0x10, %rsp
movq %r14, %rdi
movq %r13, %rsi
pushq %r10
pushq %r12
callq 0x103616
addq $0x20, %rsp
movl 0x10(%r13), %eax
leal -0x1(%rbx), %edi
jmp 0x1037e1
cmpl %edx, %r10d
jl 0x1039d2
movq %rdx, %rbx
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 %rax, %r9
subq $0x10, %rsp
movq %r14, %rdi
movq %r13, %rsi
pushq %r10
pushq %rbx
callq 0x103616
addq $0x20, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
incl %edx
jmp 0x1037b4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6921d