// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: u2g_hybrid_detan_iter(ul_resolve_t*, usg_t*, unsigned int, unsigned int, asg64_v*, asg64_v*)
void u2g_hybrid_detan_iter(ul_resolve_t *uidx, usg_t *ng, uint32_t max_ext, uint32_t clean_round, asg64_v *in, asg64_v *ib)
{
    uint32_t n_vtx = ng->n<<1, ncut = 0, k;
    uint8_t *ff; CALLOC(ff, n_vtx);
    uint32_t *ng_occ; CALLOC(ng_occ, n_vtx); 
    uint64_t *i_idx; CALLOC(i_idx, n_vtx);
    asg64_v b64, ub64; kv_init(b64); kv_init(ub64); 
    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;
    // fprintf(stderr, "\n[M::%s::] asm_opt.is_low_het_ul::%u, max_ext::%u\n", 
    //     __func__, asm_opt.is_low_het_ul, max_ext);
    // prt_usg_t(uidx, ng, "ng0");
    for (k = 0; k < clean_round; k++) {
        ncut += ug_ext_strict(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64);
        // if(asm_opt.is_low_het_ul) break;
        ncut += ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 48, 1);
        // prt_usg_t(uidx, ng, "ng_python");
        ncut += ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 16, 1);

        ncut += ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 0, 0);
        ///renew bubble for ug_ext_strict
        n_vtx = ng->n<<1; 
        REALLOC(ff, n_vtx); REALLOC(ng_occ, n_vtx); REALLOC(i_idx, n_vtx);
        renew_usg_t_bub(uidx, ng, ng_occ, ff, 0);
    }
    // // ug_ext_strict(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64);
    // ncut += ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 50);
    // prt_usg_t(uidx, ng, "ng.db");
    // ncut += ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 10);
    // ncut += ug_ext_strict(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64);
    // prt_usg_t(uidx, ng, "ng.db");
    if(ncut) {
        usg_cleanup(ng); 
        // if((max_ext>>1) > 0) usg_arc_cut_tips(ng, (max_ext>>1), 1, ub);
        usg_arc_cut_tips(ng, max_ext, 1, ub);

        // if(ng->n > 139131) {
        //     fprintf(stderr, ">[M::%s::] ng->a[139131].del::%u\n", __func__, 
        //     ng->a[139131].del);
        // }
    }
    // prt_usg_t(uidx, ng, "ng2");
    // u2g_hybrid_aln(uidx, ng, ob, ub);
    // if(ug_ext(uidx, ng, ub->a, ub->n, ob->a, max_ext, ff, ng_occ, i_idx, &b64, &ub64)) {
    //     // debug_sysm_usg_t(ng, __func__);
    //     usg_cleanup(ng);
    //     // debug_sysm_usg_t(ng, __func__);
    // }
    
    // prt_usg_t(uidx, ng, "ng0");
    // if(ug_ext_free(ob, ub, uidx, ng, max_ext, &ff, &ng_occ, &i_idx, &b64, &ub64, 50)) {
    //     usg_cleanup(ng);
    // }
    // prt_usg_t(uidx, ng, "ng1");
    
    if(!in) free(tx.a); if(!ib) free(tb.a);
    free(ng_occ); free(i_idx); free(ff); kv_destroy(b64); kv_destroy(ub64); 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %r12
movq %r8, %rbp
movl %ecx, %r13d
movl %edx, 0x24(%rsp)
movq %rdi, 0x98(%rsp)
movq %rsi, 0x28(%rsp)
movl 0x8(%rsi), %r14d
addl %r14d, %r14d
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r15
movq %rax, 0x18(%rsp)
movl $0x4, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movl $0x8, %esi
movq %r14, %rdi
callq 0x2120
movl %r13d, %ecx
movq %rax, %r13
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
xorl %edx, %edx
movq %rdx, 0x70(%rsp)
movaps %xmm0, 0x40(%rsp)
leaq 0xc0(%rsp), %rsi
movq %rdx, 0x10(%rsi)
movaps %xmm0, (%rsi)
leaq 0xa0(%rsp), %rax
movq %rdx, 0x10(%rax)
testq %rbp, %rbp
movq %rbp, 0x80(%rsp)
cmovneq %rbp, %rsi
movaps %xmm0, (%rax)
testq %r12, %r12
movq %r12, 0x88(%rsp)
cmovneq %r12, %rax
movq %rdx, 0x50(%rsp)
movq %rdx, (%rax)
movq %rdx, (%rsi)
testl %ecx, %ecx
je 0x224dff
movq %rsi, 0x90(%rsp)
xorl %r14d, %r14d
movq %rax, 0x38(%rsp)
movl %ecx, 0x34(%rsp)
movq 0x90(%rsp), %r15
movq %r15, %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
movq 0x98(%rsp), %r13
movq %r13, %rdx
movq 0x28(%rsp), %rcx
movl 0x24(%rsp), %r12d
movl %r12d, %r8d
leaq 0x18(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
callq 0x220a81
addq $0x20, %rsp
movl %eax, %ebx
addl %r14d, %ebx
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r13, %r14
movq 0x28(%rsp), %r13
movq %r13, %rcx
movl %r12d, %r8d
leaq 0x18(%rsp), %r9
movl $0x1, %eax
pushq %rax
pushq $0x30
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x220504
addq $0x30, %rsp
movl %eax, 0x30(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r13, %rcx
movl %r12d, %r8d
leaq 0x18(%rsp), %r9
movl $0x1, %eax
pushq %rax
pushq $0x10
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x220504
addq $0x30, %rsp
movl %eax, %r13d
addl 0x30(%rsp), %r13d
addl %ebx, %r13d
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rbp
movq %r14, %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rcx
movl %r12d, %r8d
leaq 0x18(%rsp), %r9
xorl %eax, %eax
pushq %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x220504
addq $0x30, %rsp
movl %eax, %r14d
addl %r13d, %r14d
movq %rbx, %r12
movl 0x8(%rbx), %r13d
addl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x2370
movq %rax, %r15
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rdi
leaq (,%r13,4), %rsi
callq 0x2370
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
shlq $0x3, %r13
movq %r13, %rsi
callq 0x2370
movq %rax, %r13
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x21e98e
movl 0x34(%rsp), %ecx
decl %ecx
jne 0x224c1b
movq 0x38(%rsp), %rbp
testl %r14d, %r14d
movq 0x28(%rsp), %r14
je 0x224dff
leaq 0x70983(%rip), %rax     # 0x295758
movl 0x7c(%rax), %edi
movq 0x8(%r14), %rcx
leaq -0x11600(%rip), %rsi    # 0x2137e3
movq %r14, %rdx
callq 0x108f2c
movq %r14, %rdi
movl 0x24(%rsp), %esi
movl $0x1, %edx
movq %rbp, %rcx
callq 0x213b68
cmpq $0x0, 0x80(%rsp)
jne 0x224e17
movq 0xd0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x88(%rsp)
jne 0x224e2f
movq 0xb0(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq