// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: print_bubbles(ma_ug_t*, bubble_type*, kvec_pe_hit*, hc_links*, ha_ug_index*)
void print_bubbles(ma_ug_t* ug, bubble_type* bub, kvec_pe_hit* hits, hc_links* link, ha_ug_index* idx)
{
    uint64_t tLen, t_utg, i, k;
    uint32_t beg, sink, n, *a;
    for (i = 0, tLen = 0; i < bub->ug->u.n; i++) tLen += bub->ug->u.a[i].len;
    fprintf(stderr, "[M::%s] # unitigs: %lu, # bases: %lu\n",  __func__, bub->ug->u.n, tLen);
    for (i = 0, tLen = 0, t_utg = 0; i < bub->f_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, NULL);
        t_utg += n;
        for (k = 0; k < n; k++)
        {
            tLen +=bub->ug->u.a[(a[k]>>1)].len;
        }
    }
    fprintf(stderr, "[M::%s] # bubbles: %lu, # unitigs: %lu, # bases: %lu\n",  __func__, 
    Get_bub_num(*bub), t_utg, tLen);


    for (i = 0, tLen = 0, t_utg = 0; i < ug->g->n_seq; i++)
    {
        if(IF_BUB(i, *bub))
        {
            t_utg++;
            tLen +=bub->ug->u.a[i].len;
        }
    }
    fprintf(stderr, "[M::%s] # bubbles: %lu, # unitigs: %lu, # bases: %lu\n",  __func__, 
    Get_bub_num(*bub), t_utg, tLen);

    for (i = 0, tLen = 0, t_utg = 0; i < ug->g->n_seq; i++)
    {
        if(IF_HET(i, *bub))
        {
            t_utg++;
            tLen +=bub->ug->u.a[i].len;
        }
    }
    fprintf(stderr, "[M::%s] # het unitigs: %lu, # het bases: %lu\n",  __func__, t_utg, tLen);

    uint8_t* flag; CALLOC(flag, ug->g->n_seq);
    uint64_t s_uid, e_uid, shif = 64 - idx->uID_bits;
    if(hits)
    {
        for (k = 0; k < hits->a.n; ++k) 
        {
            s_uid = ((hits->a.a[k].s<<1)>>shif);
            e_uid = ((hits->a.a[k].e<<1)>>shif);
            if(bub->index[s_uid] == (uint32_t)-1 || bub->index[e_uid] == (uint32_t)-1) continue;
            if(IF_BUB(s_uid, *bub) && IF_BUB(e_uid, *bub))
            {
                flag[s_uid] |= 1;
                flag[e_uid] |= 1;
                continue;
            }
            if(IF_HET(s_uid, *bub) && IF_HET(e_uid, *bub))
            {
                flag[s_uid] |= 4;
                flag[e_uid] |= 4;
                continue;
            }
            if(IF_BUB(s_uid, *bub)) flag[s_uid] |= 2, flag[e_uid] |= 2;
            if(IF_BUB(e_uid, *bub)) flag[e_uid] |= 2, flag[s_uid] |= 2;
        }
    }
    else if(link)
    {
        for (i = 0; i < link->a.n; i++)
        {
            for (k = 0; k < link->a.a[i].e.n; ++k) 
            {
                if(link->a.a[i].e.a[k].del) continue;
                s_uid = i;
                e_uid = link->a.a[i].e.a[k].uID;
                if(bub->index[s_uid] == (uint32_t)-1 || bub->index[e_uid] == (uint32_t)-1) continue;
                if(IF_BUB(s_uid, *bub) && IF_BUB(e_uid, *bub))
                {
                    flag[s_uid] |= 1;
                    flag[e_uid] |= 1;
                    continue;
                }
                if(IF_HET(s_uid, *bub) && IF_HET(e_uid, *bub))
                {
                    flag[s_uid] |= 4;
                    flag[e_uid] |= 4;
                    continue;
                }
                if(IF_BUB(s_uid, *bub)) flag[s_uid] |= 2, flag[e_uid] |= 2;
                if(IF_BUB(e_uid, *bub)) flag[e_uid] |= 2, flag[s_uid] |= 2;
            }
        }
    }
    



    for (i = 0, tLen = 0, t_utg = 0; i < ug->g->n_seq; i++)
    {
        if(flag[i] & (uint32_t)1)
        {
            t_utg++;
            tLen +=bub->ug->u.a[i].len;
        } 
    }
    fprintf(stderr, "[M::%s] # bubble-chained unitigs: %lu, # bubble-chained bases: %lu\n",  
                    __func__, t_utg, tLen);

    for (i = 0, tLen = 0, t_utg = 0; i < ug->g->n_seq; i++)
    {
        if((flag[i] & (uint32_t)1) || (flag[i] & (uint32_t)2))
        {
            t_utg++;
            tLen +=bub->ug->u.a[i].len;
        } 
    }
    fprintf(stderr, "[M::%s] # (bubble && het)-chained unitigs: %lu, # (bubble && het)-chained bases: %lu\n",  
                    __func__, t_utg, tLen);


    for (i = 0, tLen = 0, t_utg = 0; i < ug->g->n_seq; i++)
    {
        if((flag[i] & (uint32_t)1) || (flag[i] & (uint32_t)2) || (flag[i] & (uint32_t)4))
        {
            t_utg++;
            tLen +=bub->ug->u.a[i].len;
        } 
    }
    fprintf(stderr, "[M::%s] # (bubble || het)-chained unitigs: %lu, # (bubble || het)-chained bases: %lu\n",  
                    __func__, t_utg, tLen);
    free(flag);


    fprintf(stderr, "************bubble utgs************\n");
    uint64_t pathLen;
    for (i = 0, tLen = 0, t_utg = 0; i < bub->f_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, &pathLen);
        t_utg += n;
        fprintf(stderr, "(full-%lu)\tbeg:utg%.6u\tsink:utg%.6u\tpathLen:%lu\t%s\n", 
        i, (beg>>1)+1, (sink>>1)+1, pathLen, i < bub->s_bub? "s-bub":(i<bub->f_bub?"f-bub":"b-bub"));
        for (k = 0; k < n; k++)
        {
            tLen +=bub->ug->u.a[(a[k]>>1)].len;
            fprintf(stderr, "utg%.6u,", (a[k]>>1)+1);
        }
        fprintf(stderr, "\n");
        ///if(i < bub->s_bub && (n != 4 && n != 2 && n != 1)) fprintf(stderr, "weird\n");
    }

    for (i = bub->f_bub, tLen = 0, t_utg = 0; i < bub->f_bub + bub->b_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, &pathLen);
        t_utg += n;
        fprintf(stderr, "(broken-%lu)\tbeg:utg%.6u\tsink:utg%.6u\tpathLen:%lu\t%s\n", 
        i, (beg>>1)+1, (sink>>1)+1, pathLen, i < bub->s_bub? "s-bub":(i<bub->f_bub?"f-bub":"b-bub"));
        for (k = 0; k < n; k++)
        {
            tLen +=bub->ug->u.a[(a[k]>>1)].len;
            fprintf(stderr, "utg%.6u,", (a[k]>>1)+1);
        }
        fprintf(stderr, "\n");
    }

    // fprintf(stderr, "************het utgs************\n");
    // for (i = 0; i < ug->g->n_seq; i++)
    // {
    //     if(IF_HET(i, *bub)) fprintf(stderr, "utg%.6lu\n", i+1);
    // }
    // fprintf(stderr, "************het utgs************\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x38(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, %rbx
movq 0x10(%rsi), %rax
movq (%rax), %rcx
xorl %r10d, %r10d
movq %rcx, %rsi
xorl %r8d, %r8d
subq $0x1, %rsi
jb 0xf9c69
movq 0x10(%rax), %r9
movl (%r9,%r10), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %r8
addq $0x28, %r10
jmp 0xf9c4b
movq %rdx, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x159366(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xf3741(%rip), %rsi     # 0x1ed3c5
leaq 0xf3760(%rip), %rdx     # 0x1ed3eb
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x22b0
movq 0x80(%rbx), %r12
leaq 0xc(%rsp), %r13
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq %r12, %r15
je 0xf9d07
andq $0x0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x18(%rsp), %r8
movq %r13, %r9
callq 0xf6ab5
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0xf9cff
movq 0x10(%rbx), %rsi
movq 0x10(%rsi), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
imulq $0x28, %rdi, %rdi
movl (%rsi,%rdi), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %r14
incq %rdx
jmp 0xf9cd8
addq %rax, %rbp
incq %r15
jmp 0xf9ca6
movq 0x1592d2(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq 0x30(%rbx), %rcx
decq %rcx
leaq 0xf36da(%rip), %rsi     # 0x1ed3f9
leaq 0xf36c5(%rip), %rdx     # 0x1ed3eb
xorl %r15d, %r15d
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x22b0
movq 0x30(%rsp), %r12
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %rcx, %rax
je 0xf9d8b
movq (%rbx), %rdx
movq 0x80(%rbx), %rsi
movl (%rdx,%rcx,4), %edx
incq %rsi
cmpq %rdx, %rsi
jbe 0xf9d82
incq %r8
movq 0x10(%rbx), %rdx
movq 0x10(%rdx), %rdx
movl (%rdx,%r15), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %r9
incq %rcx
addq $0x28, %r15
jmp 0xf9d50
movq 0x15924e(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq 0x30(%rbx), %rcx
decq %rcx
leaq 0xf3656(%rip), %rsi     # 0x1ed3f9
leaq 0xf3641(%rip), %rdx     # 0x1ed3eb
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22b0
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
cmpq %rdx, %rax
je 0xf9e03
movq (%rbx), %rsi
movq 0x80(%rbx), %rdi
movl (%rsi,%rdx,4), %esi
incq %rdi
cmpq %rsi, %rdi
jne 0xf9dfa
incq %rcx
movq 0x10(%rbx), %rsi
movq 0x10(%rsi), %rsi
movl (%rsi,%r14), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %r8
incq %rdx
addq $0x28, %r14
jmp 0xf9dc8
movq 0x1591d6(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xf361b(%rip), %rsi     # 0x1ed42f
leaq 0xf35d0(%rip), %rdx     # 0x1ed3eb
xorl %eax, %eax
callq 0x22b0
movq 0x18(%r12), %rax
movl 0x14(%rax), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r15
movq 0x40(%rsp), %r12
testq %r12, %r12
jne 0xfa188
cmpq $0x0, 0x20(%rsp)
jne 0xfa22d
imulq $0x28, %rbp, %rax
xorl %edx, %edx
movq %r15, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x30(%rsp), %r12
cmpq %rdx, %rax
je 0xf9e95
testb $0x1, (%rsi)
je 0xf9e8c
incq %rcx
movq 0x10(%rbx), %rdi
movq 0x10(%rdi), %rdi
movl (%rdi,%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq %rdi, %r8
addq $0x28, %rdx
incq %rsi
jmp 0xf9e6b
movq 0x159144(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xf35b7(%rip), %rsi     # 0x1ed45d
leaq 0xf353e(%rip), %rdx     # 0x1ed3eb
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22b0
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
imulq $0x28, %rax, %rax
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
cmpq %r14, %rax
je 0xf9efb
testb $0x3, (%rdx)
je 0xf9ef2
incq %rcx
movq 0x10(%rbx), %rsi
movq 0x10(%rsi), %rsi
movl (%rsi,%r14), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %r8
addq $0x28, %r14
incq %rdx
jmp 0xf9ed0
movq 0x1590de(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xf3595(%rip), %rsi     # 0x1ed4a1
leaq 0xf34d8(%rip), %rdx     # 0x1ed3eb
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22b0
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
imulq $0x28, %rax, %rax
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
cmpq %r14, %rax
je 0xf9f61
testb $0x7, (%rdx)
je 0xf9f58
incq %rcx
movq 0x10(%rbx), %rsi
movq 0x10(%rsi), %rsi
movl (%rsi,%r14), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %r8
addq $0x28, %r14
incq %rdx
jmp 0xf9f36
movq 0x159078(%rip), %r14    # 0x252fe0
movq (%r14), %rdi
leaq 0xf3585(%rip), %rsi     # 0x1ed4f7
leaq 0xf3472(%rip), %rdx     # 0x1ed3eb
xorl %eax, %eax
callq 0x22b0
movq %r15, %rdi
callq 0x2220
movq (%r14), %rcx
leaq 0xf35bb(%rip), %rdi     # 0x1ed54d
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
leaq 0xf35ce(%rip), %rbp     # 0x1ed572
leaq 0xf360d(%rip), %r14     # 0x1ed5b8
pushq $0xa
popq %r15
xorl %r13d, %r13d
movq 0x80(%rbx), %r12
cmpq %r12, %r13
jae 0xfa07f
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0xf6ab5
movq 0x158fef(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movl 0x14(%rsp), %ecx
shrl %ecx
incl %ecx
movl 0x10(%rsp), %r8d
shrl %r8d
incl %r8d
cmpq 0x78(%rbx), %r13
movq 0x28(%rsp), %r9
leaq 0xf3595(%rip), %rax     # 0x1ed5ac
leaq 0xf3588(%rip), %rdx     # 0x1ed5a6
cmovbq %rdx, %rax
movq %rax, (%rsp)
movq %rbp, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x22b0
xorl %r12d, %r12d
movl 0xc(%rsp), %eax
cmpq %rax, %r12
jae 0xfa065
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %edx
shrl %edx
movq 0x158f8f(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
incl %edx
movq %r14, %rsi
xorl %eax, %eax
callq 0x22b0
incq %r12
jmp 0xfa036
movq 0x158f74(%rip), %rax    # 0x252fe0
movq (%rax), %rsi
movl %r15d, %edi
callq 0x2270
incq %r13
jmp 0xf9fb2
leaq 0xf353b(%rip), %rbp     # 0x1ed5c1
leaq 0xf352b(%rip), %r14     # 0x1ed5b8
pushq $0xa
popq %r15
movq %r12, %r13
movq 0x88(%rbx), %rax
addq %r12, %rax
cmpq %rax, %r13
jae 0xfa179
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0xf6ab5
movq 0x158f0a(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movl 0x14(%rsp), %ecx
shrl %ecx
incl %ecx
movl 0x10(%rsp), %r8d
shrl %r8d
incl %r8d
movq 0x28(%rsp), %r9
cmpq %r12, %r13
leaq 0xf34b7(%rip), %rax     # 0x1ed5b2
leaq 0xf34aa(%rip), %rdx     # 0x1ed5ac
cmovbq %rdx, %rax
cmpq 0x78(%rbx), %r13
leaq 0xf3495(%rip), %rdx     # 0x1ed5a6
cmovbq %rdx, %rax
movq %rax, (%rsp)
movq %rbp, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x22b0
xorl %r12d, %r12d
movl 0xc(%rsp), %eax
cmpq %rax, %r12
jae 0xfa158
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %edx
shrl %edx
movq 0x158e9c(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
incl %edx
movq %r14, %rsi
xorl %eax, %eax
callq 0x22b0
incq %r12
jmp 0xfa129
movq 0x158e81(%rip), %rax    # 0x252fe0
movq (%rax), %rsi
movl %r15d, %edi
callq 0x2270
incq %r13
movq 0x80(%rbx), %r12
jmp 0xfa094
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x40
popq %rcx
movq 0x38(%rsp), %rax
subq 0x18(%rax), %rcx
movq (%r12), %rax
pushq $0x8
popq %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
subq $0x1, %rax
jb 0xf9e58
movq 0x10(%r12), %r8
movq -0x8(%r8,%rdx), %rdi
movq (%r8,%rdx), %r8
addq %rdi, %rdi
shrq %cl, %rdi
addq %r8, %r8
shrq %cl, %r8
movq (%rbx), %r9
movl (%r9,%rdi,4), %r10d
cmpq %rsi, %r10
je 0xfa20b
movl (%r9,%r8,4), %r9d
cmpq %rsi, %r9
je 0xfa20b
movq 0x80(%rbx), %r11
incq %r11
cmpq %r10, %r11
jbe 0xfa1f0
movb $0x1, %r14b
cmpq %r9, %r11
ja 0xfa1fd
cmpq %r10, %r11
jne 0xfa211
movb $0x4, %r14b
cmpq %r9, %r11
jne 0xfa211
movq %rdi, %r10
movq %r8, %rdi
orb %r14b, (%r15,%r10)
orb %r14b, (%r15,%rdi)
addq $0x20, %rdx
jmp 0xfa1a0
cmpq %r10, %r11
jbe 0xfa220
orb $0x2, (%r15,%rdi)
orb $0x2, (%r15,%r8)
movb $0x2, %r14b
movq %r8, %r10
cmpq %r9, %r11
ja 0xfa203
jmp 0xfa20b
movq 0x20(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
pushq $0x18
popq %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0xf9e58
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %r8
imulq $0x30, %rcx, %r9
leaq (%r8,%r9), %rdi
movq (%r8,%r9), %r8
leaq (%r15,%rcx), %r9
movq %rdx, %r10
subq $0x1, %r8
jb 0xfa2f3
movq 0x10(%rdi), %r11
cmpb $0x0, (%r11,%r10)
js 0xfa2ea
movq (%rbx), %r12
movl (%r12,%rcx,4), %r14d
cmpq %rsi, %r14
je 0xfa2ea
movl -0x10(%r11,%r10), %r11d
movl (%r12,%r11,4), %r12d
cmpq %rsi, %r12
je 0xfa2ea
movq 0x80(%rbx), %r13
incq %r13
cmpq %r14, %r13
jbe 0xfa2b3
cmpq %r12, %r13
jbe 0xfa2b3
orb $0x1, (%r9)
addq %r15, %r11
movb $0x1, %r14b
jmp 0xfa2e7
cmpq %r14, %r13
jne 0xfa2c9
cmpq %r12, %r13
jne 0xfa2c9
orb $0x4, (%r9)
addq %r15, %r11
movb $0x4, %r14b
jmp 0xfa2e7
cmpq %r14, %r13
jbe 0xfa2d7
orb $0x2, (%r9)
orb $0x2, (%r15,%r11)
cmpq %r12, %r13
jbe 0xfa2ea
orb $0x2, (%r15,%r11)
movb $0x2, %r14b
movq %r9, %r11
orb %r14b, (%r11)
addq $0x28, %r10
jmp 0xfa264
incq %rcx
jmp 0xfa23f