// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x1cb9c0
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xa4(%rbp)
movq $0x0, -0xb8(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2270
movq %rax, -0xb8(%rbp)
callq 0xcd150
movq %rax, -0xd0(%rbp)
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x1dd9de
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %eax
movl %eax, %esi
leaq -0xdc(%rbp), %rdx
leaq -0xe0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
leaq -0xd8(%rbp), %rax
movq %r9, %r8
movq %rax, (%rsp)
callq 0x1c4b20
movq -0xd0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0xec(%rbp)
movq -0xd8(%rbp), %rax
movl %eax, -0xe8(%rbp)
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0xb0(%rcx)
movb %al, -0xe1(%rbp)
je 0x1dd987
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0xdc(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdx
addq $0x1, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0xe1(%rbp)
jne 0x1dd987
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0xe0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
sete %al
movb %al, -0xe1(%rbp)
movl -0xe8(%rbp), %edx
movl -0xec(%rbp), %esi
movq -0xf8(%rbp), %rdi
movb -0xe1(%rbp), %r8b
xorl %ecx, %ecx
movl $0x1, %eax
testb $0x1, %r8b
cmovnel %eax, %ecx
callq 0xcd8f0
movq -0xd0(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa8(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x1dd8a1
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jae 0x1ddd3f
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1dda1c
jmp 0x1ddd2b
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0xa8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x1dda3a
jmp 0x1ddd2b
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %esi
shrl %esi
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %edx
callq 0x1dc5c0
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jne 0x1dda69
jmp 0x1ddd2b
cmpl $0x2, -0xc4(%rbp)
jne 0x1ddb0a
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %esi
movl -0xa8(%rbp), %ecx
xorl $0x1, %ecx
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %eax
xorl %edx, %edx
movl %edx, %r8d
leaq -0x30(%rbp), %r9
movq %r8, %rdx
movl %eax, (%rsp)
callq 0x1dc630
cmpl $0x0, %eax
je 0x1ddb05
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa8(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl -0x24(%rbp), %eax
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1cef50
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x1ddd2b
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %eax
xorl %ecx, %ecx
movl %ecx, %r8d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x30(%rbp), %r9
movq %r8, %rdx
movl %eax, (%rsp)
callq 0x1dc630
cmpl $0x0, %eax
jne 0x1ddb41
jmp 0x1ddd2b
movl -0xa8(%rbp), %edi
movq -0x18(%rbp), %rdx
movq -0xb8(%rbp), %rcx
leaq -0xa0(%rbp), %rsi
callq 0x1cc080
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpq -0x70(%rbp), %rax
jae 0x1ddd29
movq -0x60(%rbp), %rax
movl -0xac(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x1ddb8e
jmp 0x1ddd15
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl -0xac(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x1ddbac
jmp 0x1ddd15
movq -0x10(%rbp), %rdi
movl -0xac(%rbp), %esi
shrl %esi
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %edx
callq 0x1dc5c0
cmpl $0x0, %eax
jne 0x1ddbd1
jmp 0x1ddd15
movl -0xac(%rbp), %eax
shrl %eax
movl -0xa8(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x1ddbea
jmp 0x1ddd15
movq -0xb8(%rbp), %rax
movl -0xac(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xbc(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xbc(%rbp), %eax
cmpl -0xa8(%rbp), %eax
je 0x1ddc5c
movq -0x10(%rbp), %rdi
movl -0xbc(%rbp), %esi
shrl %esi
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %edx
callq 0x1dc5c0
cmpl $0x0, %eax
jbe 0x1ddc44
movl $0x1, -0xc0(%rbp)
jmp 0x1ddc5c
movq -0xb8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xbc(%rbp)
jmp 0x1ddc0a
cmpl $0x0, -0xc0(%rbp)
jne 0x1ddd13
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %esi
movl -0xac(%rbp), %ecx
xorl $0x1, %ecx
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %eax
xorl %edx, %edx
movl %edx, %r8d
leaq -0x30(%rbp), %r9
movq %r8, %rdx
movl %eax, (%rsp)
callq 0x1dc630
cmpl $0x0, %eax
je 0x1ddd11
movb $0x0, -0x1e(%rbp)
movq -0x60(%rbp), %rax
movl -0xac(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0xac(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
movl %eax, %ecx
movl -0x24(%rbp), %eax
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1cef50
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x1ddd13
jmp 0x1ddd15
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x1ddb68
jmp 0x1ddd2b
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x1dd9e8
movq -0xb8(%rbp), %rdi
callq 0x2210
leaq -0xa0(%rbp), %rdi
callq 0x1cbb60
movq -0xd0(%rbp), %rdi
callq 0xcdd10
movq -0xd0(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0xd0(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0xd0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb8(%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax,%rax)