// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_bub_graph(ma_ug_t*, bubble_type*)
void get_bub_graph(ma_ug_t* ug, bubble_type* bub)
{
    asg_t *sg = ug->g;
    asg_arc_t t, *p = NULL;
    pdq pq;
    init_pdq(&pq, sg->n_seq<<1);
    uint32_t n_vtx = sg->n_seq<<1, v, k;
    uint32_t *pre = NULL; MALLOC(pre, n_vtx);
    uint32_t pre_id, adjecent, bub_occ;
    asg_t *bub_g = asg_init();
    for (v = 0; v < bub->f_bub; v++)///all bubbles
    {
        uint64_t pathbase;
        uint32_t beg, sink;
        get_bubbles(bub, v, &beg, &sink, NULL, NULL, &pathbase);
        asg_seq_set(bub_g, v, pathbase, (bub->check_het && IF_HET(beg>>1, *bub) && IF_HET(sink>>1, *bub))?1:0);
        bub_g->seq[v].c = PRIMARY_LABLE;
    }

    //check all unitigs, instead of bubble nodes
    for (v = 0; v < n_vtx; ++v)
    {
        if(sg->seq[v>>1].del) continue;
        if(bub->b_s_idx.a[v>>1] == (uint64_t)-1) continue; ///if (v>>1) is not a beg or sink of bubbles
        ///one node might be the beg/sink node of at most two bubbles
        bub_occ = connect_bub_occ(bub, v>>1, bub->check_het);
        if(bub_occ == 0) continue;
        if(bub_occ == 2)
        {
            if(ma_2_bub_arc(bub, v, NULL, v^1, NULL, &t, bub->check_het))
            {
                t.ol = sg->seq[v>>1].len;
                p = asg_arc_pushp(bub_g);
                *p = t;
            }
            continue;
        }
        if(ma_2_bub_arc(bub, v, NULL, (uint32_t)-1, NULL, &t, bub->check_het) == 0) continue;

        ///v is the beg/sink node of only one bubble
        get_shortest_path(v, &pq, sg, pre);
        for (k = 0; k < pq.dis.n; k++)
        {
            if(pq.dis.a[k] == (uint64_t)-1) continue;
            if(bub->b_s_idx.a[k>>1] == (uint64_t)-1) continue;
            if(connect_bub_occ(bub, k>>1, bub->check_het) == 0) continue;
            if((k>>1) == (v>>1)) continue;
            pre_id = pre[k];
            adjecent = 0;
            while (pre_id != v)
            {
                if(connect_bub_occ(bub, pre_id>>1, bub->check_het) > 0)///some bubbles between v and k
                {
                    adjecent = 1;
                    break;
                } 
                pre_id = pre[pre_id];
            }

            if(adjecent == 0)
            {
                if(ma_2_bub_arc(bub, v, NULL, k^1, NULL, &t, bub->check_het))///edges spanning tangles
                {
                    t.el = 0; t.ol = pq.dis.a[k] + sg->seq[k>>1].len;
                    p = asg_arc_pushp(bub_g);
                    *p = t;
                }
            }
        }
    }

    free(pre);
    destory_pdq(&pq);

    asg_cleanup(bub_g);
    bub_g->r_seq = bub_g->n_seq;
    bub->b_g = bub_g;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
movq 0x18(%rdi), %r14
movl 0x14(%r14), %eax
addl %eax, %eax
movslq %eax, %rsi
leaq 0x60(%rsp), %rdi
callq 0xfa585
movq %r14, 0x10(%rsp)
movl 0x14(%r14), %eax
addl %eax, %eax
movq %rax, 0x50(%rsp)
leaq (,%rax,4), %rdi
callq 0x2280
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
callq 0x68f5d
movq %rax, %r15
movl %ebp, %r14d
movq 0x80(%rbx), %rdx
cmpq %r14, %rdx
jbe 0x103abb
xorl %ecx, %ecx
cmpl $0x0, 0xb0(%rbx)
je 0x103a98
movq 0x28(%rbx), %rsi
movq 0x40(%rbx), %rax
movl (%rax,%r14,4), %r8d
movl (%rsi,%r8,4), %edi
movq (%rbx), %rax
shrl %edi
movl (%rax,%rdi,4), %edi
incq %rdx
cmpq %rdi, %rdx
jne 0x103a98
incl %r8d
movl (%rsi,%r8,4), %edx
shrl %edx
xorl %ecx, %ecx
cmpl (%rax,%rdx,4), %edi
sete %cl
movq 0x58(%rbx), %rax
movl (%rax,%r14,8), %edx
movq %r15, %rdi
movl %r14d, %esi
callq 0x69316
movq 0x20(%r15), %rax
movb $0x0, 0x4(%rax,%r14,8)
leal 0x1(%r14), %ebp
jmp 0x103a4d
movq %r15, 0x20(%rsp)
xorl %r13d, %r13d
cmpl 0x50(%rsp), %r13d
je 0x103ce8
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl %r13d, %ecx
shrl %ecx
movq %rcx, 0x18(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x103ce0
movq 0x70(%rbx), %rax
movq 0x18(%rsp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x103ce0
movl 0xb0(%rbx), %r14d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl %r14d, %edx
callq 0x102ecf
testl %eax, %eax
je 0x103ce0
cmpl $0x2, %eax
jne 0x103b93
movl %r13d, %ecx
xorl $0x1, %ecx
movl %r14d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
xorl %r8d, %r8d
leaq 0x30(%rsp), %r9
callq 0x102f0f
testl %eax, %eax
je 0x103ce0
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rsp), %rcx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl 0x3c(%rsp), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
callq 0xfbf83
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x103ce0
movl %r14d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
leaq 0x30(%rsp), %r9
callq 0x102f0f
testl %eax, %eax
je 0x103ce0
movl %r13d, %edi
leaq 0x60(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0xfa824
movq 0x90(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
movl %r12d, %r12d
cmpq %r12, 0x58(%rsp)
jbe 0x103ce0
movq 0x48(%rsp), %rax
cmpq $-0x1, (%rax,%r12,8)
je 0x103cd8
movq 0x70(%rbx), %rax
movl %r12d, %ebp
shrl %ebp
cmpq $-0x1, (%rax,%rbp,8)
je 0x103cd8
movl 0xb0(%rbx), %r15d
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x102ecf
cmpl 0x18(%rsp), %ebp
je 0x103cd8
testl %eax, %eax
je 0x103cd8
movq %r12, %r14
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %r14d
cmpl %r13d, %r14d
je 0x103c6a
movl %r14d, %esi
shrl %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x102ecf
testl %eax, %eax
je 0x103c46
jmp 0x103cd8
movl %r12d, %ecx
xorl $0x1, %ecx
movl %r15d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
xorl %r8d, %r8d
leaq 0x30(%rsp), %r9
callq 0x102f0f
testl %eax, %eax
je 0x103cd8
movb $0x0, 0x42(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%rbp,8), %eax
movq 0x48(%rsp), %rcx
addl (%rcx,%r12,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x3c(%rsp), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
callq 0xfbf83
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x10(%rax)
incl %r12d
jmp 0x103beb
incl %r13d
jmp 0x103ac3
movq 0x28(%rsp), %rdi
callq 0x2220
leaq 0x60(%rsp), %rdi
callq 0xfa60b
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x694eb
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%r14), %eax
movl %eax, 0x18(%r14)
movq %r14, 0xb8(%rbx)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq