// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x1, 0x28(%rax)
jne 0x36e9a9
movq -0x10(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0xd0(%rbp)
jmp 0x36e9d8
movq -0x10(%rbp), %rax
movsd 0x38(%rax), %xmm0
movq -0x10(%rbp), %rax
subsd 0x30(%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
shlq %rdi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
shlq %rdi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rcx
xorl %edx, %edx
callq 0x34e170
movq $0x1, -0x38(%rbp)
cmpq $0x1, -0x38(%rbp)
jg 0x36eda2
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
imulq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jge 0x36ec3d
movsd -0x50(%rbp), %xmm0
movq -0x10(%rbp), %rax
ucomisd 0x38(%rax), %xmm0
jbe 0x36ead6
movq -0x10(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
sarq %rax
movl %eax, %r8d
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0xc0(%rbp), %r10
movq -0xc8(%rbp), %rax
leaq -0xb8(%rbp), %rsi
movl $0x1, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x36e4a0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rax
sarq %rax
movl %eax, %ecx
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movl 0x68(%rax), %r8d
movq -0xc0(%rbp), %rax
movl $0x1, %r9d
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x3507e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rcx
xorl %edx, %edx
callq 0x34e170
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
movl %eax, %r8d
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0xc0(%rbp), %r10
movq -0xc8(%rbp), %rax
leaq -0xb8(%rbp), %rsi
movl $0x1, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x36e4a0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rax
movl %eax, %ecx
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movl 0x68(%rax), %r8d
movq -0xc0(%rbp), %rax
movl $0x1, %r9d
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x3507e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x34e170
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movsd -0x48(%rbp), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0x36eaa6
movsd 0x133bb(%rip), %xmm0    # 0x382000
movsd %xmm0, -0x50(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
sarq %rax
movl %eax, %r8d
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0xc0(%rbp), %r10
movq -0xc8(%rbp), %rax
leaq -0xb8(%rbp), %rsi
movl $0x1, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x36e4a0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rax
sarq %rax
movl %eax, %ecx
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movl 0x68(%rax), %r8d
movq -0xc0(%rbp), %rax
movl $0x1, %r9d
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x3507e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rcx
xorl %edx, %edx
callq 0x34e170
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
movl %eax, %r8d
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0xc0(%rbp), %r10
movq -0xc8(%rbp), %rax
leaq -0xb8(%rbp), %rsi
movl $0x1, %r9d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x36e4a0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x40(%rbp), %rax
movl %eax, %ecx
movsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movl 0x68(%rax), %r8d
movq -0xc0(%rbp), %rax
movl $0x1, %r9d
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x3507e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x34e170
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x36ea74
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0xa0(%rbp), %rdi
callq 0x2210
movq -0x88(%rbp), %rdi
callq 0x2210
movq -0x70(%rbp), %rdi
callq 0x2210
movq -0x58(%rbp), %rdi
callq 0x2210
movq -0xc0(%rbp), %rdi
callq 0x2210
movq -0xc8(%rbp), %rdi
callq 0x2210
movq -0x18(%rbp), %rdi
leaq 0x2be70(%rip), %rsi     # 0x39ac6b
callq 0x3586a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %ecx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
callq 0x36b810
addq $0xe0, %rsp
popq %rbp
retq
nop