// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %rsi, %rbx
movq 0x18(%rdi), %r14
movl 0x14(%r14), %eax
addl %eax, %eax
movslq %eax, %rsi
leaq 0x70(%rsp), %rdi
callq 0x13bb82
movq %r14, 0x20(%rsp)
movl 0x14(%r14), %eax
addl %eax, %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,4), %rdi
callq 0x2280
movq %rax, 0x30(%rsp)
callq 0x9fccc
movq %rax, %r14
movq 0x80(%rbx), %rax
testq %rax, %rax
je 0x1451eb
xorl %r15d, %r15d
cmpl $0x0, 0xb0(%rbx)
movl $0x0, %ecx
je 0x1451bf
movq 0x28(%rbx), %rsi
movq 0x40(%rbx), %rcx
movl (%rcx,%r15,4), %edi
movl (%rsi,%rdi,4), %ecx
movq (%rbx), %rdx
shrl %ecx
movl (%rdx,%rcx,4), %r8d
incq %rax
movl $0x0, %ecx
cmpq %r8, %rax
jne 0x1451bf
incl %edi
movl (%rsi,%rdi,4), %ecx
shrl %ecx
movl (%rdx,%rcx,4), %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
sete %cl
movq 0x58(%rbx), %rax
movl (%rax,%r15,8), %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0xa0115
movq 0x20(%r14), %rax
movb $0x0, 0x4(%rax,%r15,8)
incl %r15d
movq 0x80(%rbx), %rax
cmpq %r15, %rax
ja 0x14517a
movq %r14, 0x38(%rsp)
cmpl $0x0, 0x58(%rsp)
je 0x145545
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movl %r13d, %ecx
shrl %ecx
movq %rcx, 0x28(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x145537
movq 0x70(%rbx), %rax
movq 0x28(%rsp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x145537
movl 0xb0(%rbx), %r15d
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl %r15d, %r8d
callq 0x1445e7
xorl %eax, %eax
cmpq $-0x1, 0x18(%rsp)
sete %al
xorl %ecx, %ecx
cmpq $-0x1, 0x10(%rsp)
sete %cl
addl %eax, %ecx
movl $0x2, %eax
subl %ecx, %eax
je 0x145537
cmpl $0x2, %eax
jne 0x145346
movl %r13d, %ecx
xorl $0x1, %ecx
movl %r15d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
xorl %r8d, %r8d
leaq 0x40(%rsp), %r9
callq 0x144774
testl %eax, %eax
je 0x145537
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rsp), %rcx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movl 0x4c(%rsp), %ecx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %ecx
orl %eax, %ecx
movl %ecx, 0x4c(%rsp)
movq 0x38(%rsp), %r14
movl 0x4(%r14), %ecx
andl %edx, %ecx
movl (%r14), %eax
cmpl %eax, %ecx
jne 0x145305
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%r14)
movq 0x8(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x8(%r14)
movq 0x8(%r14), %rax
movl 0x4(%r14), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r14)
leaq (%rdx,%rdx,2), %rcx
movq 0x50(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
jmp 0x145537
movl %r15d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
leaq 0x40(%rsp), %r9
callq 0x144774
testl %eax, %eax
je 0x145537
movl %r13d, %edi
leaq 0x70(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %rcx
callq 0x13be4a
movq 0xa0(%rsp), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0x145537
xorl %r15d, %r15d
movq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq $-0x1, (%rax,%r15,8)
je 0x145529
movq 0x70(%rbx), %rax
movl %r15d, %r14d
shrl %r14d
cmpq $-0x1, (%rax,%r14,8)
je 0x145529
movl 0xb0(%rbx), %r12d
movq %rbx, %rdi
movl %r14d, %esi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl %r12d, %r8d
callq 0x1445e7
movq 0x10(%rsp), %rax
andq 0x18(%rsp), %rax
cmpq $-0x1, %rax
sete %al
cmpl 0x28(%rsp), %r14d
je 0x145529
testb %al, %al
jne 0x145529
movq 0x30(%rsp), %rax
movl (%rax,%r15,4), %ebp
cmpl %r13d, %ebp
je 0x14545c
movl %ebp, %esi
shrl %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl %r12d, %r8d
callq 0x1445e7
cmpq $-0x1, 0x18(%rsp)
jne 0x145529
cmpq $-0x1, 0x10(%rsp)
jne 0x145529
movl %ebp, %eax
movq 0x30(%rsp), %rcx
movl (%rcx,%rax,4), %ebp
jmp 0x14541a
movl %r15d, %ecx
xorl $0x1, %ecx
movl %r12d, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
xorl %r8d, %r8d
leaq 0x40(%rsp), %r9
callq 0x144774
testl %eax, %eax
je 0x145529
movb $0x0, 0x52(%rsp)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r14,8), %eax
movq 0x60(%rsp), %rcx
addl (%rcx,%r15,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movl 0x4c(%rsp), %ecx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %ecx
orl %eax, %ecx
movl %ecx, 0x4c(%rsp)
movq 0x38(%rsp), %r14
movl 0x4(%r14), %ecx
andl %edx, %ecx
movl (%r14), %eax
cmpl %eax, %ecx
jne 0x1454ed
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%r14)
movq 0x8(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x8(%r14)
movq 0x8(%r14), %rax
movl 0x4(%r14), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r14)
leaq (%rdx,%rdx,2), %rcx
movq 0x50(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
incl %r15d
cmpq %r15, 0x68(%rsp)
ja 0x1453a9
incl %r13d
cmpl 0x58(%rsp), %r13d
jne 0x1451fe
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0xa02fa
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%r14), %eax
movl %eax, 0x18(%r14)
movq %r14, 0xb8(%rbx)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq