// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: u2g_hybrid_detan(ul_resolve_t*, usg_t*, unsigned int, asg64_v*, asg64_v*)
void u2g_hybrid_detan(ul_resolve_t *uidx, usg_t *ng, uint32_t max_ext, asg64_v *in, asg64_v *ib)
{
    uint64_t k, i, x, m, *tmp, sn; asg64_v tx = {0,0,0}, tb = {0,0,0}, *ob = NULL, *ub = NULL; 
    ob = (in?(in):(&tx)); ub = (ib?(ib):(&tb)); ob->n = ub->n = 0;

    for (k = 0; k < uidx->str_b.n_thread; k++) {
        uidx->str_b.buf[k].res_dump.n = uidx->str_b.buf[k].u.n = uidx->str_b.buf[k].o.n = 0;
    }
    kt_for(uidx->str_b.n_thread, worker_integer_realign_g, uidx, uidx->uovl.i_ug->u.n);

    for (k = ob->n = ub->n = m = 0; k < uidx->str_b.n_thread; k++) {
        for (i = 0; i < uidx->str_b.buf[k].res_dump.n; i++) {
            x = uidx->str_b.buf[k].res_dump.a[i];
            kv_push(uint64_t, *ob, x);//aln details
            if(x&((uint64_t)0x8000000000000000)) {
                x -= ((uint64_t)0x8000000000000000); x >>= 32; x <<= 32;//seq_id
                x |= ob->n;//offset
                kv_push(uint64_t, *ub, x);///idx:: seq_id|offset_in_ob
            } else {
                m++;
            }
        }
    }

    radix_sort_srt64(ub->a, ub->a + ub->n);
    kv_resize(uint64_t, *ub, ub->n+m); tmp = ub->a + ub->n; m = 0;
    for (k = 0; k < ub->n; k++) {
        sn = ((uint32_t)(ob->a[((uint32_t)ub->a[k])-1]));
        memcpy(tmp + m, ob->a + ((uint32_t)ub->a[k]), sn*sizeof((*tmp))); 
        ub->a[k] = m; ub->a[k] <<= 32; ub->a[k] |= sn;//offset_in_ob|occ
        m += sn;
    }
    assert(m <= ob->n);
    memcpy(ob->a, tmp, m*sizeof((*tmp))); ob->n = m;


    // for (k = 0, p = NULL; k < ub->n; k++) {
    //     p = &(ob->a[((uint32_t)ub->a[k])-1]);
    //     x = ub->a[k]<<32;//offset
    //     x |= ((uint32_t)(*p));///occ
    //     ub->a[k] = x;
    //     (*p) >>= 32; (*p) <<= 32; (*p) |= k; 
    // }
    // for (k = m = 0; k < ob->n; k++) {
    //     if(ob->a[k]&((uint64_t)0x8000000000000000)) {
    //         x = m; x <<= 32; x |= ((uint32_t)ub->a[(uint32_t)ob->a[k]]);
    //         ub->a[(uint32_t)ob->a[k]] = x;
    //     } else {
    //         ob->a[m++] = ob->a[k];
    //     }
    // }
    // ob->n = m;

    // for (k = ob->n = ub->n = 0; k < uidx->str_b.n_thread; k++) {
    //     for (i = 0; i < uidx->str_b.buf[k].res_dump.n; i++) {
    //         if((uidx->str_b.buf[k].res_dump.a[i]>>32)==((uint32_t)-1)) {
    //             x = ob->n; x <<= 32; x |= ((uint32_t)uidx->str_b.buf[k].res_dump.a[i]);
    //             kv_push(uint64_t, *ub, x);///idx:: offset_in_ob|occ
    //         } else {
    //             kv_push(uint64_t, *ob, uidx->str_b.buf[k].res_dump.a[i]);//aln details
    //         }
    //     }        
    // }

    /*********debugging*********/
    fprintf(stderr, "[M::%s::] # iug::%u, # gchain::%u\n", __func__, (uint32_t)uidx->uovl.i_ug->u.n, (uint32_t)ub->n);
    // for (k = 0; k < ub->n; k++) {
    //     // fprintf(stderr, "[M::%s::k->%lu] # iug::%u, # chain::%u, off chain::%u\n", 
    //     // __func__, k, (uint32_t)uidx->uovl.cc.iug_a[k].n, (uint32_t)ub->a[k], (uint32_t)(ub->a[k]>>32));
    //     for (i = 0; i < (uint32_t)ub->a[k]; i++) {
    //         // fprintf(stderr, "utg%.6dl(%c) <------> utg%.6dl(%c)\n", 
    //         // ((int32_t)(uidx->uovl.cc.iug_a[k].a[i].v>>1))+1, "+-"[uidx->uovl.cc.iug_a[k].a[i].v&1],
    //         // ((int32_t)(ob->a[(ub->a[k]>>32)+i]>>1))+1, "+-"[ob->a[(ub->a[k]>>32)+i]&1]);
    //         assert(uidx->uovl.cc.iug_a[k].a[i].v == ob->a[(ub->a[k]>>32)+i]);
    //     }
    // }
    /*********debugging*********/

    ///ub->idx; ob->nodes
    // u_ug = gen_unique_g(uidx, ng, ng_occ, ub->a, ub->n, ob->a);//ma_ug_hybrid_gen(ng);
    ///debug
    debug_sysm_usg_t(ng, __func__);
    // prt_usg_t(uidx, ng, "ng4");
    if(gen_unique_g_adv(uidx, ng, ub->a, ub->n, ob->a, max_ext)) {
        // usg_arc_t *z = get_usg_arc(ng, 2, 576), *q = get_usg_arc(ng, 577, 3);
        // fprintf(stderr, "xxxx0xxx[M::%s::] p->del::%u, q->del::%u\n", 
        //             __func__, z?z->del:1, q?q->del:1);
        ///debug
        debug_sysm_usg_t(ng, __func__);
        // z = get_usg_arc(ng, 2, 576); q = get_usg_arc(ng, 577, 3);
        // fprintf(stderr, "xxxx1xxx[M::%s::] p->del::%u, q->del::%u\n", 
        //             __func__, z?z->del:1, q?q->del:1);
        // fprintf(stderr, "+[M::%s::] ng->n::%u\n", __func__, (uint32_t)ng->n);
        usg_cleanup(ng);
        ///debug
        debug_sysm_usg_t(ng, __func__);
        // fprintf(stderr, "-[M::%s::] ng->n::%u\n", __func__, (uint32_t)ng->n);
    }
    // prt_usg_t(uidx, ng, "ng_dbg");
    if(!in) free(tx.a); if(!ib) free(tb.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, 0x3c(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
testq %r8, %r8
je 0x212300
movq $0x0, (%r8)
testq %rcx, %rcx
je 0x21230c
movq $0x0, (%rcx)
movq %rcx, 0x20(%rsp)
movq %r8, (%rsp)
movq 0x1e0(%rbp), %rdi
testq %rdi, %rdi
je 0x21234a
movq 0x1d8(%rbp), %rax
addq $0x78, %rax
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rcx, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x288(%rax)
addq $0x328, %rax            # imm = 0x328
decq %rdx
jne 0x212331
movq 0x230(%rbp), %rax
movq (%rax), %rcx
leaq 0x5b3(%rip), %rsi       # 0x21290e
movq %rbp, %rdx
callq 0x1047b4
movq (%rsp), %r8
testq %r8, %r8
je 0x212373
movq $0x0, (%r8)
movq %rbx, 0x40(%rsp)
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x212389
movq $0x0, (%r9)
xorl %ebx, %ebx
cmpq $0x0, 0x1e0(%rbp)
movl $0x0, %edi
movl $0x0, %eax
movq %rax, 0x18(%rsp)
movl $0x0, %r14d
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %r12d
je 0x21260a
xorl %ecx, %ecx
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x18(%rsp)
xorl %edi, %edi
xorl %r10d, %r10d
xorl %ebx, %ebx
movq %rbp, 0x50(%rsp)
movq 0x1d8(%rbp), %rax
movq %rcx, 0x48(%rsp)
imulq $0x328, %rcx, %rsi      # imm = 0x328
cmpq $0x0, 0x300(%rax,%rsi)
je 0x2125ef
xorl %r13d, %r13d
movq %rsi, 0x30(%rsp)
movq %rbx, %rcx
testq %r9, %r9
je 0x212419
movq (%r9), %rcx
movq %r10, %rdx
testq %r9, %r9
je 0x212425
movq 0x8(%r9), %rdx
movq 0x310(%rax,%rsi), %rax
movq (%rax,%r13,8), %r15
cmpq %rdx, %rcx
jne 0x2124b3
movq %r12, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbp, %rbx
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, %r12
testq %r9, %r9
je 0x212462
movq %rsi, 0x8(%r9)
movq %r10, %r12
movq %r11, %r14
movq %rdi, %rbp
testq %r9, %r9
je 0x212471
movq 0x10(%r9), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x212494
movq %rax, 0x10(%r9)
movq %r12, %r10
movq (%rsp), %r8
movq %rbp, %rdi
jmp 0x21249e
movq %r12, %r10
movq %rax, %rdi
movq (%rsp), %r8
movq %rbx, %rbp
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %rsi
movq %r14, %r11
movq %rdi, %rax
testq %r9, %r9
je 0x2124bf
movq 0x10(%r9), %rax
movq %rbx, %rcx
testq %r9, %r9
je 0x2124ca
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
testq %r9, %r9
je 0x2124d8
movq %rdx, (%r9)
jmp 0x2124db
movq %rdx, %rbx
movq %r15, (%rax,%rcx,8)
testq %r15, %r15
js 0x2124ef
incq %r12
movq %r11, %r14
jmp 0x2125d5
movq %rbx, %rbp
testq %r9, %r9
je 0x2124fa
movq (%r9), %rbp
movq 0x18(%rsp), %rax
testq %r8, %r8
je 0x212507
movq (%r8), %rax
movq %r11, %rcx
testq %r8, %r8
je 0x212513
movq 0x8(%r8), %rcx
cmpq %rcx, %rax
jne 0x21256a
movq %r12, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, %r14
testq %r8, %r8
je 0x212541
movq %rsi, 0x8(%r8)
movq %r11, %r14
movq %r10, %r12
movq 0x8(%rsp), %rdi
testq %r8, %r8
je 0x212552
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %r8
testq %r8, %r8
je 0x21256f
movq %rax, 0x10(%r8)
jmp 0x212574
movq %r11, %r14
jmp 0x21258b
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r9
movq %r12, %r10
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rax
testq %r8, %r8
je 0x212599
movq 0x10(%r8), %rax
movq 0x18(%rsp), %rcx
testq %r8, %r8
je 0x2125a6
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
testq %r8, %r8
je 0x2125b4
movq %rdx, (%r8)
jmp 0x2125b9
movq %rdx, 0x18(%rsp)
movabsq $0x7fffffff00000000, %rdx # imm = 0x7FFFFFFF00000000
andq %rdx, %r15
orq %r15, %rbp
movq %rbp, (%rax,%rcx,8)
movq %r14, %r11
movq 0x50(%rsp), %rbp
incq %r13
movq 0x1d8(%rbp), %rax
cmpq 0x300(%rax,%rsi), %r13
jb 0x21240e
jmp 0x2125f2
movq %r11, %r14
movq 0x48(%rsp), %rcx
incq %rcx
movq %r14, %r11
cmpq 0x1e0(%rbp), %rcx
jb 0x2123e4
movq %rdi, 0x10(%rsp)
movq 0x8(%rsp), %rdi
testq %r8, %r8
je 0x21261d
movq 0x10(%r8), %rdi
movq 0x18(%rsp), %rax
testq %r8, %r8
je 0x21262a
movq (%r8), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq (%rsp), %rdx
testq %rdx, %rdx
je 0x212640
movq 0x8(%rdx), %r14
movq 0x18(%rsp), %rax
testq %rdx, %rdx
movq 0x10(%rsp), %rdi
je 0x212652
movq (%rdx), %rax
addq %r12, %rax
cmpq %rax, %r14
movq 0x20(%rsp), %r8
jae 0x2126cf
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq 0x8(%rsp), %rdi
testq %rdx, %rdx
je 0x2126a8
movq %rsi, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %rdx
testq %rdx, %rdx
jne 0x2126c1
movq %rax, 0x8(%rsp)
jmp 0x2126c5
movq %rax, 0x10(%rdx)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %rax
testq %rdx, %rdx
je 0x2126dd
movq 0x10(%rdx), %rax
movq %rbx, %r12
movq 0x18(%rsp), %rcx
testq %rdx, %rdx
je 0x2126ed
movq (%rdx), %rcx
leaq (%rax,%rcx,8), %r14
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
testq %rdx, %rdx
je 0x212703
movq (%rdx), %rax
cmpq %rax, %rbx
jae 0x212776
movq %rdi, %rax
testq %r8, %r8
je 0x212714
movq 0x10(%r8), %rax
movq 0x8(%rsp), %rcx
testq %rdx, %rdx
je 0x212722
movq 0x10(%rdx), %rcx
movq (%rcx,%rbx,8), %rcx
leal -0x1(%rcx), %edx
movl (%rax,%rdx,8), %r15d
leaq (%r14,%r13,8), %rdi
movl %ecx, %ecx
leaq (%rax,%rcx,8), %rsi
leaq (,%r15,8), %rdx
callq 0x2160
movq (%rsp), %rdx
movq 0x8(%rsp), %rax
testq %rdx, %rdx
je 0x212756
movq 0x10(%rdx), %rax
movq %r13, %rcx
shlq $0x20, %rcx
orq %r15, %rcx
movq %rcx, (%rax,%rbx,8)
addq %r15, %r13
incq %rbx
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r8
jmp 0x2126f6
testq %r8, %r8
je 0x21277e
movq (%r8), %r12
cmpq %r12, %r13
movq 0x40(%rsp), %rbx
ja 0x2128ef
testq %r8, %r8
jne 0x2127a2
shlq $0x3, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x2160
jmp 0x2127bc
movq 0x10(%r8), %rdi
leaq (,%r13,8), %rdx
movq %r14, %rsi
movq %r8, %r14
callq 0x2160
movq %r13, (%r14)
movq 0x7781d(%rip), %rax     # 0x289fe0
movq (%rax), %rdi
movq 0x230(%rbp), %rax
movl (%rax), %ecx
cmpq $0x0, (%rsp)
jne 0x212809
leaq 0x2a702(%rip), %rsi     # 0x23cedf
leaq 0x2a71e(%rip), %r13     # 0x23cf02
movq %r13, %rdx
movq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20e650
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
jmp 0x21283b
movq (%rsp), %rax
movl (%rax), %r8d
leaq 0x2a6c8(%rip), %rsi     # 0x23cedf
leaq 0x2a6e4(%rip), %r13     # 0x23cf02
movq %r13, %rdx
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20e650
movq (%rsp), %rax
movq 0x10(%rax), %rdx
testq %rax, %rax
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rcx
je 0x212852
movq (%rax), %rax
movq %rax, 0x18(%rsp)
testq %rcx, %rcx
je 0x21285b
movq 0x10(%rcx), %r8
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rcx
movl 0x3c(%rsp), %r9d
callq 0x211906
testq %rax, %rax
je 0x2128af
leaq 0x2a686(%rip), %r15     # 0x23cf02
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20e650
leaq 0x77eca(%rip), %rax     # 0x28a758
movl 0x7c(%rax), %edi
movq 0x8(%rbx), %rcx
leaq -0xa5b4(%rip), %rsi     # 0x2082e8
movq %rbx, %rdx
callq 0x1047b4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20e650
cmpq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
jne 0x2128c1
callq 0x2220
cmpq $0x0, (%rsp)
je 0x2128d7
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0x2a58f(%rip), %rdi     # 0x23ce85
leaq 0x26a28(%rip), %rsi     # 0x239325
leaq 0x2a58c(%rip), %rcx     # 0x23ce90
movl $0x3574, %edx           # imm = 0x3574
callq 0x2110