// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: u2g_hybrid_clean(ul_resolve_t*, ulg_opt_t*, usg_t*, asg64_v*, asg64_v*)
void u2g_hybrid_clean(ul_resolve_t *uidx, ulg_opt_t *ulopt, usg_t *ng, asg64_v *b, asg64_v *ub)
{
    int64_t i, ss, mm_tip = ulopt->max_tip_hifi;///ulopt->max_tip; 
    double step = (ulopt->clean_round==1?ulopt->max_ovlp_drop_ratio:
                        ((ulopt->max_ovlp_drop_ratio-ulopt->min_ovlp_drop_ratio)/(ulopt->clean_round-1)));
    double drop = ulopt->min_ovlp_drop_ratio; ///CALLOC(iug->g->seq_vis, iug->g->n_seq*2);
    buf_t bb; memset(&bb, 0, sizeof(buf_t)); CALLOC(bb.a, ng->n<<1);
    uint8_t *bs, *f; CALLOC(bs, ng->n<<1); CALLOC(f, ng->n);
    // fprintf(stderr, "\n[M::%s::] Starting hybrid clean, mm_tip::%ld\n", __func__, mm_tip);
    // prt_usg_t(uidx, ng, "ng_h0");
    usg_arc_cut_tips(ng, mm_tip, 0, b);///p_telo
    // prt_usg_t(uidx, ng, "ng_h1");
    // char sb[1000]; 

    for (ss = 1; ss <= 1/**6**/; ss++) {
        mm_tip = ulopt->max_tip_hifi*ss;
        // fprintf(stderr, "\n[M::%s::] ss::%ld, mm_tip::%ld, ulopt->clean_round::%ld\n", 
        // __func__, ss, mm_tip, ulopt->clean_round);
        for (i = 0, drop = ulopt->min_ovlp_drop_ratio; i < ulopt->clean_round; i++, drop += step) {
            if(drop > ulopt->max_ovlp_drop_ratio) drop = ulopt->max_ovlp_drop_ratio;
            // fprintf(stderr, "-0-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
            // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_a", ss, i, drop);
            // prt_usg_t(uidx, ng, sb);
            // usg_arc_cut_length(ng, b, ub, mm_tip>>1, drop, ulopt->is_trio, 1, NULL);
            usg_bub_clean(ng, &bb, b, ub, mm_tip>>1, drop, 1, bs, f);
            usg_arc_cut_srt_length(ng, b, ub, mm_tip>>1, drop, ulopt->is_trio, 1, NULL, bs);
            // fprintf(stderr, "-1-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
            // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_b", ss, i, drop);
            // prt_usg_t(uidx, ng, sb);
            usg_arc_cut_tips(ng, mm_tip, 0, b);
            // fprintf(stderr, "-2-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
            // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_c", ss, i, drop);
            // prt_usg_t(uidx, ng, sb);
            // usg_arc_cut_length(ng, b, ub, mm_tip, drop, ulopt->is_trio, 1, NULL);
            usg_bub_clean(ng, &bb, b, ub, mm_tip, drop, 1, bs, f);
            usg_arc_cut_srt_length(ng, b, ub, mm_tip, drop, ulopt->is_trio, 1, NULL, bs);
            // fprintf(stderr, "-3-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
            // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_d", ss, i, drop);
            // prt_usg_t(uidx, ng, sb);
            usg_arc_cut_tips(ng, mm_tip, 1, b);
            // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_e", ss, i, drop);
            // prt_usg_t(uidx, ng, sb);
            // fprintf(stderr, "-4-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        }

        drop = 1;
        // fprintf(stderr, "-0-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_a", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        // usg_arc_cut_length(ng, b, ub, mm_tip>>1, drop, ulopt->is_trio, 1, NULL);
        usg_bub_clean(ng, &bb, b, ub, mm_tip>>1, drop, 1, bs, f);
        // fprintf(stderr, "-1bub-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_b::bub", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        usg_arc_cut_srt_length(ng, b, ub, mm_tip>>1, drop, ulopt->is_trio, 1, NULL, bs);
        // fprintf(stderr, "-1-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_b", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        usg_arc_cut_tips(ng, mm_tip, 0, b);
        // fprintf(stderr, "-2-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_c", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        // usg_arc_cut_length(ng, b, ub, mm_tip, drop, ulopt->is_trio, 1, NULL);
        usg_bub_clean(ng, &bb, b, ub, mm_tip, drop, 1, bs, f);
        usg_arc_cut_srt_length(ng, b, ub, mm_tip, drop, ulopt->is_trio, 1, NULL, bs);
        // fprintf(stderr, "-3-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_d", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        usg_arc_cut_tips(ng, mm_tip, 1, b);
        // sprintf(sb, "ng_ss::%ld_i::%ld_drop::%f_e", ss, i, drop);
        // prt_usg_t(uidx, ng, sb);
        // fprintf(stderr, "-4-[M::%s::] i::%ld, drop::%f\n", __func__, i, drop);
    }
    free(bb.a); free(bb.S.a); free(bb.T.a); free(bb.b.a); free(bb.e.a); free(bs); free(f);
    // prt_usg_t(uidx, ng, "ng_h2");
    ///debug
    debug_sysm_usg_t(ng, __func__);

    /******for debug******/
    // prt_usg_t(uidx, ng, "ng_dbg");
    /******for debug******/

    // u2g_hybrid_extend(ng, NULL, b, ub);
    // u2g_hybrid_detan(uidx, ng, mm_tip, b, ub);
    u2g_hybrid_detan_iter(uidx, ng, mm_tip, ulopt->clean_round, b, ub);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq 0x60(%rsi), %r12
movq 0x28(%rsi), %rax
movsd 0x38(%rsi), %xmm1
cmpq $0x1, %rax
movq %rdi, 0x40(%rsp)
movq %r8, 0x38(%rsp)
je 0x21b369
subsd 0x30(%rsi), %xmm1
decq %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %rsi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movq $0x0, 0xc0(%rsp)
movq 0x8(%r15), %r13
leaq (,%r13,2), %rbp
movl $0x1c, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x60(%rsp)
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x30(%rsp)
movl $0x1, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x18(%rsp)
movq %r15, %rdi
movl %r12d, %esi
movq %r15, %r12
xorl %edx, %edx
movq %r14, %rcx
callq 0x20865f
movq 0x60(%rbx), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x28(%rbx)
movq 0x38(%rsp), %rbp
movsd 0x10(%rsp), %xmm1
movq %rbx, 0x28(%rsp)
jle 0x21b564
movsd 0x30(%rbx), %xmm0
movq 0x20(%rsp), %rcx
shrq %rcx
movq %rcx, 0x48(%rsp)
xorl %ecx, %ecx
movsd %xmm1, 0x10(%rsp)
movq %rcx, 0x58(%rsp)
movsd 0x38(%rbx), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp)
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %r13
movq %rbp, %rcx
movq 0x48(%rsp), %r15
movl %r15d, %r8d
movl $0x1, %r9d
movq 0x18(%rsp), %rbp
pushq %rbp
movq 0x38(%rsp), %rbx
pushq %rbx
callq 0x21b03c
addq $0x10, %rsp
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r15d, %ecx
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq %rbx
xorl %eax, %eax
pushq %rax
callq 0x209db9
addq $0x10, %rsp
movq %r12, %rdi
movq %r14, %r15
movq 0x20(%rsp), %r14
movl %r14d, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x20865f
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
movl %r14d, %r8d
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq %rbp
pushq %rbx
callq 0x21b03c
addq $0x10, %rsp
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl %r14d, %ecx
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq %rbx
xorl %eax, %eax
pushq %rax
callq 0x209db9
addq $0x10, %rsp
movq %r12, %rdi
movl %r14d, %esi
movq %r15, %r14
movl $0x1, %edx
movq %r15, %rcx
callq 0x20865f
movq 0x58(%rsp), %rcx
movsd 0x10(%rsp), %xmm1
movq %r13, %rbp
movq 0x28(%rsp), %rbx
incq %rcx
movsd 0x50(%rsp), %xmm0
addsd %xmm1, %xmm0
cmpq 0x28(%rbx), %rcx
jl 0x21b445
movq %r12, %r15
movq 0x20(%rsp), %r12
movq %r12, %r13
shrq %r13
leaq 0x60(%rsp), %rsi
movss 0x6b41(%rip), %xmm0     # 0x2220c0
movq %r15, %rdi
movq %r14, %rdx
movq %rbp, %rcx
movl %r13d, %r8d
movl $0x1, %r9d
movq 0x18(%rsp), %rax
pushq %rax
movq 0x38(%rsp), %rbx
pushq %rbx
callq 0x21b03c
addq $0x10, %rsp
xorl %eax, %eax
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movss 0x6b04(%rip), %xmm0     # 0x2220c0
movl $0x1, %r9d
pushq %rbx
pushq %rax
callq 0x209db9
addq $0x10, %rsp
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x20865f
movq %r15, %rdi
leaq 0x60(%rsp), %r13
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movl %r12d, %r8d
movss 0x6ac7(%rip), %xmm0     # 0x2220c0
movl $0x1, %r9d
pushq 0x18(%rsp)
pushq %rbx
callq 0x21b03c
addq $0x10, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl %r12d, %ecx
movss 0x6a9f(%rip), %xmm0     # 0x2220c0
movl $0x1, %r9d
pushq %rbx
xorl %eax, %eax
pushq %rax
callq 0x209db9
addq $0x10, %rsp
movq %r15, %rdi
movl %r12d, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x20865f
movq (%r13), %rdi
callq 0x2220
movq 0x18(%r13), %rdi
callq 0x2220
movq 0x30(%r13), %rdi
callq 0x2220
movq 0x48(%r13), %rdi
callq 0x2220
movq 0x60(%r13), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x18(%rsp), %rdi
callq 0x2220
leaq 0x2260a(%rip), %rsi     # 0x23dc97
movq %r15, %rdi
callq 0x20e650
movq 0x28(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movl %r12d, %edx
movq %r14, %r8
movq %rbp, %r9
callq 0x219623
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq