// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: renew_usg_t_bub(ul_resolve_t*, usg_t*, unsigned int*, unsigned char*, unsigned int)
void renew_usg_t_bub(ul_resolve_t *uidx, usg_t *ng, uint32_t *id_map, uint8_t *ff, uint32_t rocc_cut)
{
    ma_ug_t *ug = ma_ug_hybrid_gen(ng); 
    uint32_t i, k, v, p[2]; ma_utg_t *u; p[0] = 1; p[1] = 2;
    memset(id_map, -1, sizeof((*id_map))*ng->n);
    memset(ff, 0, sizeof((*ff))*(ng->n<<1));
    kvec_asg_arc_t_warp e; kv_init(e.a); e.i = 0;
    // for (i = dn = 0; i < ng->n; i++) {
    //     if(ng->a[i].del) continue;
    //     dn++;
    // }
    // fprintf(stderr, "ng->n::%u, ug->u.n::%u, dn::%u\n", (uint32_t)ng->n, (uint32_t)ug->u.n, dn);
    // dn = 0
    for (i = 0; i < ug->u.n; i++) {
        ug->g->seq[i].c = PRIMARY_LABLE;
        u = &(ug->u.a[i]);
        if(u->m == 0) continue;
        for (k = 0; k < u->n; k++) {
            v = u->a[k]>>32; 
            id_map[v>>1] = i<<2; 
            if(k == 0) id_map[v>>1] |= p[(v^1)&1];
            if(k + 1 == u->n) id_map[v>>1] |= p[(v)&1];
            // dn++;
        }
        // fprintf(stderr, "+[M::%s] i::%u\n", __func__, i);
        merge_hybrid_utg_content(u, uidx->l1_ug, uidx->sg, ng, &e);
        // fprintf(stderr, "-[M::%s] i::%u\n", __func__, i);
        ug->g->seq[i].len = u->len;
    }
    kv_destroy(e.a);

    destory_bubbles(uidx->bub); free(uidx->bub); CALLOC(uidx->bub, 1);
    // fprintf(stderr, "[M::%s] homozygous read coverage threshold: %d\n", __func__, asm_opt.hom_global_coverage_set?
    //                         asm_opt.hom_global_coverage:(int)(((double)asm_opt.hom_global_coverage)/((double)HOM_PEAK_RATE)));
    // identify_bubbles(ug, uidx->bub, uidx->r_het, NULL);
    if(asm_opt.polyploidy <= 2) {
        identify_bubbles_recal(uidx->sg, ug, uidx->bub, uidx->r_het, uidx->uopt->sources, uidx->uopt->ruIndex, NULL);
    } else {
        identify_bubbles_recal_poy(uidx->sg, ug, uidx->bub, uidx->r_het, uidx->uopt->sources, uidx->uopt->ruIndex, NULL);
    }
    
    // fprintf(stderr, "0[M::%s::] f[51]::%u\n", __func__, ff[51]);
    for (i = 0; i < ng->n; i++) {
        if(id_map[i] != (uint32_t)-1) {
            k = id_map[i]>>2; 
            // fprintf(stderr, "k::%u, ng->n::%u, ug->u.n::%u, dn::%u\n", k, (uint32_t)ng->n, (uint32_t)ug->u.n, dn);
            // if(i == 7075 || i == 28174 || i == 77111 || i == 3826 || i == 12150 || i == 58312 || i == 59190 || i == 72134) {
            //     fprintf(stderr, "[M::%s::k->%u] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u, is_hom::%u\n", __func__, k, 
            //         i+1, "+-"[0], (uint32_t)ug->u.a[k].n, rocc_cut, IF_HOM(k, (*(uidx->bub))));
            // }
            if((ug->u.a[k].n >= rocc_cut) && 
                            ((!(IF_HOM(k, (*(uidx->bub))))) || (asm_opt.purge_level_primary == 0))) {
                if(id_map[i]&p[0]) {
                    ff[i<<1] = 2;
                    // fprintf(stderr, "[M::%s::] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u\n", __func__, 
                    // i+1, "+-"[0], (uint32_t)ug->u.a[k].n, rocc_cut);
                }
                if(id_map[i]&p[1]) {
                    ff[(i<<1)+1] = 2;
                    // fprintf(stderr, "[M::%s::] utg%.6dl(%c), ug->u.a[k].n::%u, rocc_cut::%u\n", __func__, 
                    // i+1, "+-"[1], (uint32_t)ug->u.a[k].n, rocc_cut);
                }
            }
            id_map[i] = uidx->bub->index[k];
        }
    }
    // fprintf(stderr, "1[M::%s::] f[51]::%u\n", __func__, ff[51]);
    free(uidx->bub->index); MALLOC(uidx->bub->index, ng->n); 
    memcpy(uidx->bub->index, id_map, sizeof((*id_map))*ng->n);
    if(asm_opt.purge_level_primary == 0) {///all nodes are het
        for (k = 0; k < ug->g->n_seq; k++) {
            if(IF_HOM(k, *(uidx->bub))) uidx->bub->index[k] = uidx->bub->f_bub+1;
        }
    }
    ma_ug_destroy(ug);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x359c70
movq %rax, -0x30(%rbp)
movl $0x1, -0x44(%rbp)
movl $0x2, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
shlq $0x2, %rdx
movl $0xff, %esi
callq 0x2100
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
shlq %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x3610bf
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x360fbe
jmp 0x3610b1
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x361061
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %edx
shll $0x2, %edx
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x38(%rbp)
jne 0x361025
movl -0x3c(%rbp), %eax
xorl $0x1, %eax
andl $0x1, %eax
movl %eax, %eax
movl -0x44(%rbp,%rax,4), %edx
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movq -0x50(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x361051
movl -0x3c(%rbp), %eax
andl $0x1, %eax
movl %eax, %eax
movl -0x44(%rbp,%rax,4), %edx
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x361053
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x360fc5
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x10(%rbp), %rcx
leaq -0x70(%rbp), %r8
callq 0x361380
movq -0x50(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x360f75
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x1c4a90
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movl $0x1, %edi
movl $0x110, %esi            # imm = 0x110
callq 0x2140
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x28(%rax)
leaq 0x636f5(%rip), %rax     # 0x3c47f8
cmpl $0x2, 0x1bc(%rax)
jg 0x36114f
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x1c74e0
jmp 0x361190
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x1c9190
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x361292
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
je 0x361282
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x2, %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x24(%rbp), %eax
jb 0x361267
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jbe 0x361220
leaq 0x635e1(%rip), %rax     # 0x3c47f8
cmpl $0x0, 0x130(%rax)
jne 0x361267
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl -0x44(%rbp), %eax
cmpl $0x0, %eax
je 0x361241
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
movb $0x2, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl -0x40(%rbp), %eax
cmpl $0x0, %eax
je 0x361265
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb $0x2, (%rax,%rcx)
jmp 0x361267
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x361284
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x361197
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rdi
callq 0x2210
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
shlq $0x2, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
shlq $0x2, %rdx
callq 0x2180
leaq 0x63510(%rip), %rax     # 0x3c47f8
cmpl $0x0, 0x130(%rax)
jne 0x36136e
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x30(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x36136c
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jbe 0x36135f
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x80(%rax), %rax
addq $0x1, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x361361
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x3612f8
jmp 0x36136e
movq -0x30(%rbp), %rdi
callq 0xcd6f0
addq $0x80, %rsp
popq %rbp
retq