// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movq %r9, %r14
movq %r8, %r13
movl %ecx, %r12d
movl %edx, %ebp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movl 0x8(%rsi), %ebx
addl %ebx, %ebx
movl $0x1, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
movl $0x4, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, (%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movaps %xmm0, 0x40(%rsp)
leaq 0xb0(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0x90(%rsp), %r15
movq %rax, 0x10(%r15)
testq %r13, %r13
cmovneq %r13, %rcx
movaps %xmm0, (%r15)
testq %r14, %r14
cmovneq %r14, %r15
movq %rax, 0x50(%rsp)
movq %rax, (%r15)
movq %rax, (%rcx)
testl %r12d, %r12d
je 0x2198d9
movq %r13, 0x58(%rsp)
movq %r14, 0x60(%rsp)
movl %ebp, 0x2c(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x68(%rsp)
movq 0x18(%rsp), %rbx
movq %r15, 0x20(%rsp)
movl %r12d, 0x34(%rsp)
movq 0x68(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq 0x38(%rsp), %r13
movq %r13, %rcx
movl 0x2c(%rsp), %r15d
movl %r15d, %r8d
leaq 0x10(%rsp), %r9
leaq 0x40(%rsp), %r14
pushq %r14
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
callq 0x2156aa
addq $0x20, %rsp
movl %eax, %ebx
addl %ebp, %ebx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %r13, %r14
movl %r15d, %r8d
leaq 0x10(%rsp), %r13
movq %r13, %r9
movl $0x1, %eax
pushq %rax
pushq $0x30
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rbp
pushq %rbp
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
callq 0x2150bc
addq $0x30, %rsp
movl %eax, 0x30(%rsp)
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movl %r15d, %r8d
movq %r13, %r9
movl $0x1, %eax
pushq %rax
pushq $0x10
leaq 0x50(%rsp), %rax
pushq %rax
pushq %rbp
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
callq 0x2150bc
addq $0x30, %rsp
movl %eax, %r13d
addl 0x30(%rsp), %r13d
addl %ebx, %r13d
movq %r12, %rdi
movl 0x34(%rsp), %r12d
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movl %r15d, %r8d
leaq 0x10(%rsp), %r9
xorl %eax, %eax
pushq %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
pushq %rbp
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
callq 0x2150bc
addq $0x30, %rsp
movl %eax, %ebp
addl %r13d, %ebp
movq %r14, %r15
movl 0x8(%r14), %ebx
addl %ebx, %ebx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2370
movq %rax, %r13
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
leaq (,%rbx,4), %rsi
callq 0x2370
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq (%rsp), %rdi
shlq $0x3, %rbx
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
callq 0x2370
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x21354c
movq 0x20(%rsp), %r15
decl %r12d
jne 0x2196fa
testl %ebp, %ebp
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %r13
movl 0x2c(%rsp), %ebx
je 0x2198d9
leaq 0x70ead(%rip), %rax     # 0x28a758
movl 0x7c(%rax), %edi
movq 0x38(%rsp), %r12
movq 0x8(%r12), %rcx
leaq -0x115d7(%rip), %rsi    # 0x2082e8
movq %r12, %rdx
callq 0x1047b4
movq %r12, %rdi
movl %ebx, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20865f
testq %r13, %r13
jne 0x2198eb
movq 0xc0(%rsp), %rdi
callq 0x2220
testq %r14, %r14
jne 0x2198fd
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq