// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %r8, 0x38(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdi, 0x58(%rsp)
movq 0x60(%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsi), %rax
movsd 0x38(%rsi), %xmm1
cmpq $0x1, %rax
movq %rsi, 0x48(%rsp)
je 0x1d74a1
subsd 0x30(%rsi), %xmm1
decq %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %r14
movupd %xmm0, 0x58(%r14)
movupd %xmm0, 0x48(%r14)
movupd %xmm0, 0x38(%r14)
movupd %xmm0, 0x28(%r14)
movupd %xmm0, 0x18(%r14)
movupd %xmm0, 0x8(%r14)
movq 0x20(%rsp), %rbx
movq 0x8(%rbx), %r13
leaq (%r13,%r13), %r12
pushq $0x1c
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, (%r14)
pushq $0x1
popq %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, 0x30(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0x1c600f
movb $0x1, %al
testb $0x1, %al
movq 0x48(%rsp), %r13
je 0x1d7740
movq 0x60(%r13), %rax
movsd 0x30(%r13), %xmm0
movq %rax, 0x10(%rsp)
shrq %rax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
cmpq 0x28(%r13), %rax
jge 0x1d7662
movsd 0x38(%r13), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x60(%rsp)
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rsi
movq 0x28(%rsp), %r15
movq %r15, %rdx
movq 0x38(%rsp), %rbp
movq %rbp, %rcx
movq 0x50(%rsp), %r13
movl %r13d, %r8d
pushq $0x1
popq %r12
movl %r12d, %r9d
pushq 0x18(%rsp)
movq 0x38(%rsp), %r14
pushq %r14
callq 0x1d71c0
popq %rcx
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movss 0xc(%rsp), %xmm0
movl %r12d, %r9d
pushq %r14
pushq $0x0
callq 0x1c739d
popq %rax
popq %rcx
movq %rbx, %rdi
movq 0x10(%rsp), %r13
movl %r13d, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x1c600f
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %rcx
movl %r13d, %r8d
movss 0xc(%rsp), %xmm0
movl %r12d, %r9d
pushq 0x18(%rsp)
pushq %r14
callq 0x1d71c0
popq %rcx
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movss 0xc(%rsp), %xmm0
movl %r12d, %r9d
pushq %r14
pushq $0x0
callq 0x1c739d
popq %rax
popq %rcx
movq %rbx, %rdi
movl %r13d, %esi
movq 0x48(%rsp), %r13
movl %r12d, %edx
movq %r15, %rcx
callq 0x1c600f
movq 0x68(%rsp), %rax
incq %rax
movsd 0x60(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
jmp 0x1d7550
movq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdx
movq 0x38(%rsp), %rbx
movq %rbx, %rcx
movq 0x50(%rsp), %r13
movl %r13d, %r8d
movss 0x8a41(%rip), %xmm0     # 0x1e00d0
pushq $0x1
popq %rbp
movl %ebp, %r9d
pushq 0x18(%rsp)
movq 0x38(%rsp), %r12
pushq %r12
callq 0x1d71c0
popq %rcx
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %r13d, %ecx
movss 0x8a15(%rip), %xmm0     # 0x1e00d0
movl %ebp, %r9d
pushq %r12
pushq $0x0
callq 0x1c739d
popq %rax
popq %rcx
movq %r15, %rdi
movq 0x10(%rsp), %r13
movl %r13d, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x1c600f
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movl %r13d, %r8d
movss 0x89d9(%rip), %xmm0     # 0x1e00d0
movl %ebp, %r9d
pushq 0x18(%rsp)
pushq %r12
callq 0x1d71c0
popq %rcx
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %r13d, %ecx
movss 0x89b5(%rip), %xmm0     # 0x1e00d0
movl %ebp, %r9d
pushq %r12
pushq $0x0
callq 0x1c739d
popq %rax
popq %rcx
movq %r15, %rdi
movl %r13d, %esi
movl %ebp, %edx
movq %r14, %rcx
callq 0x1c600f
xorl %eax, %eax
jmp 0x1d752a
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0xb8(%rsp), %rdi
callq 0x2220
movq 0xd0(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rdi
callq 0x2220
leaq 0x247a1(%rip), %rsi     # 0x1fbf3a
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x1cb1f3
movl 0x28(%r13), %ecx
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %r8
movq 0x38(%rsp), %r9
callq 0x1d5aee
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq