// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movl %edx, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl 0x8(%rsi), %ebx
addl %ebx, %ebx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x2120
movq %rax, %r12
leaq 0x18(%rsp), %rax
movq %r12, (%rax)
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x2120
movq %rax, %rbp
leaq 0x10(%rsp), %rax
movq %rbp, (%rax)
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x2120
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rcx
movaps %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
leaq 0x40(%rsp), %rcx
andq $0x0, 0x10(%rcx)
leaq 0xb0(%rsp), %rdx
andq $0x0, 0x10(%rdx)
leaq 0x90(%rsp), %rsi
andq $0x0, 0x10(%rsi)
movaps %xmm0, (%rdx)
testq %r13, %r13
movq %r13, 0x78(%rsp)
cmovneq %r13, %rdx
movq %rax, %r13
movaps %xmm0, (%rsi)
testq %r14, %r14
movq %r14, 0x80(%rsp)
cmovneq %r14, %rsi
movaps %xmm0, (%rcx)
movq %rsi, 0x28(%rsp)
andq $0x0, (%rsi)
movq %rdx, 0x88(%rsp)
andq $0x0, (%rdx)
movl $0x0, 0x4(%rsp)
subl $0x1, %r15d
jb 0x1d5d74
movq 0x88(%rsp), %r12
movq %r12, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
movl %r15d, 0x24(%rsp)
movq 0x38(%rsp), %r15
movq %r15, %rdx
movq 0x30(%rsp), %r13
movq %r13, %rcx
movl 0x20(%rsp), %ebx
movl %ebx, %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 0x1d1b0b
addq $0x20, %rsp
movl %eax, %ebp
addl 0x4(%rsp), %ebp
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
movl %ebx, %r8d
leaq 0x18(%rsp), %r9
pushq $0x1
popq %rax
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 0x1d15b5
addq $0x30, %rsp
movl %eax, 0x4(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %r15
movq %r13, %rcx
movl %ebx, %r8d
leaq 0x18(%rsp), %r9
pushq $0x1
popq %rax
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 0x1d15b5
addq $0x30, %rsp
movl %eax, %r13d
addl 0x4(%rsp), %r13d
addl %ebp, %r13d
movq %r12, %rdi
movq %r14, %rsi
movq 0x38(%rsp), %r14
movq %r14, %rdx
movq %r15, %rcx
movl %ebx, %r8d
leaq 0x18(%rsp), %r9
pushq $0x0
pushq $0x0
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 0x1d15b5
addq $0x30, %rsp
addl %r13d, %eax
movl %eax, 0x4(%rsp)
movq %r15, %rbx
movl 0x8(%r15), %r13d
addl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x2370
movq %rax, %r12
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rdi
leaq (,%r13,4), %rsi
callq 0x2370
movq %rax, %rbp
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
shlq $0x3, %r13
movq %r13, %rsi
callq 0x2370
movq %rax, %r13
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movl 0x24(%rsp), %r15d
movq %rbx, %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x1cfb2b
jmp 0x1d5bd2
cmpl $0x0, 0x4(%rsp)
je 0x1d5d9c
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c5ce9
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl 0x20(%rsp), %esi
movq 0x28(%rsp), %rcx
callq 0x1c600f
cmpq $0x0, 0x78(%rsp)
jne 0x1d5db1
movq 0xc0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x80(%rsp)
jne 0x1d5dc9
movq 0xa0(%rsp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
movq 0x70(%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