// File: /workspace/github_cpp_makefile_star2_O3/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 %r8, 0x48(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq 0x60(%rsi), %r12
movq 0x28(%rsi), %rax
movsd 0x38(%rsi), %xmm1
cmpq $0x1, %rax
movq %rdi, 0x50(%rsp)
je 0x226861
subsd 0x30(%rbx), %xmm1
decq %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0xb8(%rsp)
movupd %xmm0, 0xa8(%rsp)
movupd %xmm0, 0x98(%rsp)
movupd %xmm0, 0x88(%rsp)
movupd %xmm0, 0x78(%rsp)
movupd %xmm0, 0x68(%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, %rbp
movl $0x1, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, %r13
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
movq %r14, 0x28(%rsp)
movq %r14, %rcx
callq 0x213b68
movq 0x60(%rbx), %rax
cmpq $0x0, 0x28(%rbx)
movq %rbx, 0x18(%rsp)
movq %rax, 0x40(%rsp)
movq %r13, 0x10(%rsp)
jle 0x226a52
movsd 0x30(%rbx), %xmm0
movq %rax, %r14
shrq %r14
movq %rbp, 0x38(%rsp)
xorl %ebx, %ebx
movq %r15, %rbp
movq 0x28(%rsp), %r15
movq %r14, 0x20(%rsp)
movq 0x38(%rsp), %r12
movq 0x40(%rsp), %r13
movq 0x18(%rsp), %rax
movsd 0x38(%rax), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x58(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp)
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
movq 0x48(%rsp), %r14
movq %r14, %rcx
movq 0x20(%rsp), %r8
movl $0x1, %r9d
pushq 0x10(%rsp)
pushq %r12
callq 0x22653e
addq $0x10, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
callq 0x2150f4
addq $0x10, %rsp
movq %rbp, %rdi
movl %r13d, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x213b68
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movl %r13d, %r8d
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq 0x10(%rsp)
pushq %r12
callq 0x22653e
addq $0x10, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %r13d, %ecx
movss 0xc(%rsp), %xmm0
movl $0x1, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
callq 0x2150f4
addq $0x10, %rsp
movq %rbp, %rdi
movl %r13d, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x213b68
movq 0x20(%rsp), %r14
incq %rbx
movsd 0x58(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movq 0x18(%rsp), %rax
cmpq 0x28(%rax), %rbx
jl 0x226935
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r13
movq %rbp, %r15
jmp 0x226a5b
movq %rax, %r14
shrq %r14
movq %rbp, %r12
leaq 0x60(%rsp), %rsi
movss 0x7668(%rip), %xmm0     # 0x22e0d0
movq %r15, %rdi
movq 0x28(%rsp), %rbp
movq %rbp, %rdx
movq 0x48(%rsp), %rbx
movq %rbx, %rcx
movl %r14d, %r8d
movl $0x1, %r9d
pushq %r13
pushq %r12
callq 0x22653e
addq $0x10, %rsp
xorl %eax, %eax
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r14d, %ecx
movss 0x7629(%rip), %xmm0     # 0x22e0d0
movl $0x1, %r9d
pushq %r12
pushq %rax
callq 0x2150f4
addq $0x10, %rsp
movq %r15, %rdi
movq 0x40(%rsp), %r14
movl %r14d, %esi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x213b68
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movl %r14d, %r8d
movss 0x75e9(%rip), %xmm0     # 0x22e0d0
movl $0x1, %r9d
pushq %r13
pushq %r12
callq 0x22653e
addq $0x10, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl %r14d, %ecx
movss 0x75c2(%rip), %xmm0     # 0x22e0d0
movl $0x1, %r9d
pushq %r12
xorl %eax, %eax
pushq %rax
callq 0x2150f4
addq $0x10, %rsp
movq %r15, %rdi
movl %r14d, %esi
movl $0x1, %edx
movq %rbp, %rcx
callq 0x213b68
leaq 0x60(%rsp), %r13
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 %r12, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
leaq 0x233fb(%rip), %rsi     # 0x249f7b
movq %r15, %rdi
callq 0x219965
movq 0x18(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x50(%rsp), %rdi
movq %r15, %rsi
movl %r14d, %edx
movq %rbp, %r8
movq %rbx, %r9
callq 0x224b34
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq