// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x35f11b
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x35f128
leaq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x35f128
movq -0xa0(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x35f14a
movq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x35f15a
leaq -0x88(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x35f15a
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq $0x0, (%rax)
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x1e0(%rcx), %rax
jae 0x35f20b
movq -0x8(%rbp), %rax
movq 0x1d8(%rax), %rax
imulq $0x328, -0x30(%rbp), %rcx # imm = 0x328
addq %rcx, %rax
movq $0x0, 0x78(%rax)
movq -0x8(%rbp), %rax
movq 0x1d8(%rax), %rax
imulq $0x328, -0x30(%rbp), %rcx # imm = 0x328
addq %rcx, %rax
movq $0x0, 0x90(%rax)
movq -0x8(%rbp), %rax
movq 0x1d8(%rax), %rax
imulq $0x328, -0x30(%rbp), %rcx # imm = 0x328
addq %rcx, %rax
movq $0x0, 0x300(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x35f18c
movq -0x8(%rbp), %rax
movq 0x1e0(%rax), %rax
movl %eax, %edi
movq -0x8(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x230(%rax), %rax
movq (%rax), %rcx
leaq 0x62f(%rip), %rsi       # 0x35f860
callq 0x16d710
movq $0x0, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq $0x0, (%rax)
movq -0x90(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x1e0(%rcx), %rax
jae 0x35f4ba
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x1d8(%rcx), %rcx
imulq $0x328, -0x30(%rbp), %rdx # imm = 0x328
addq %rdx, %rcx
cmpq 0x300(%rcx), %rax
jae 0x35f4a7
movq -0x8(%rbp), %rax
movq 0x1d8(%rax), %rax
imulq $0x328, -0x30(%rbp), %rcx # imm = 0x328
addq %rcx, %rax
movq 0x310(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq -0x90(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x35f358
movq -0x90(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x35f30b
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x35f319
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x35f319
movq -0xb0(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rdx
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x90(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq -0x40(%rbp), %rax
cmpq $0x0, %rax
je 0x35f488
movq -0x40(%rbp), %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x20, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x40(%rbp)
movq -0x90(%rbp), %rax
movq (%rax), %rax
orq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x35f45f
movq -0x98(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x35f412
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x35f420
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x35f420
movq -0xb8(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rdx
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x98(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x35f494
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x35f496
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x35f27f
jmp 0x35f4a9
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x35f262
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x98(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x98(%rbp), %rcx
movq (%rcx), %rcx
addq -0x48(%rbp), %rcx
cmpq %rcx, %rax
jae 0x35f609
movq -0x98(%rbp), %rax
movq (%rax), %rcx
addq -0x48(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x98(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x98(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x98(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x98(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x98(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x35f60b
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x98(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x35f729
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x98(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2180
movq -0x48(%rbp), %rdx
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rdx
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x35f63b
movq -0x48(%rbp), %rax
movq -0x90(%rbp), %rcx
cmpq (%rcx), %rax
ja 0x35f73b
jmp 0x35f75a
leaq 0x3a717(%rip), %rdi     # 0x399e59
leaq 0x36a70(%rip), %rsi     # 0x3961b9
movl $0x3574, %edx           # imm = 0x3574
leaq 0x3a70f(%rip), %rcx     # 0x399e64
callq 0x2130
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2180
movq -0x48(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, (%rax)
movq 0x64855(%rip), %rax     # 0x3c3fe0
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x230(%rax), %rax
movq (%rax), %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movq (%rax), %rax
movl %eax, %r8d
leaq 0x3a701(%rip), %rsi     # 0x399eb3
leaq 0x3a71d(%rip), %rdx     # 0x399ed6
movb $0x0, %al
callq 0x22a0
movq -0x10(%rbp), %rdi
leaq 0x3a70b(%rip), %rsi     # 0x399ed6
callq 0x3586a0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x98(%rbp), %rax
movq (%rax), %rcx
movq -0x90(%rbp), %rax
movq 0x10(%rax), %r8
movl -0x14(%rbp), %r9d
callq 0x35df40
cmpq $0x0, %rax
je 0x35f830
movq -0x10(%rbp), %rdi
leaq 0x3a6c4(%rip), %rsi     # 0x399ed6
callq 0x3586a0
movq -0x10(%rbp), %rdi
callq 0x34d830
movq -0x10(%rbp), %rdi
leaq 0x3a6ab(%rip), %rsi     # 0x399ed6
callq 0x3586a0
cmpq $0x0, -0x20(%rbp)
jne 0x35f840
movq -0x60(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0x28(%rbp)
jne 0x35f850
movq -0x78(%rbp), %rdi
callq 0x2210
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)