// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: rescue_bubble_by_chain(asg_t*, ma_sub_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, long long, float, long long, R_to_U*, float, float, int, int, unsigned int, long long, bub_label_t*, long long, unsigned char*)
void rescue_bubble_by_chain(asg_t *sg, ma_sub_t *coverage_cut, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, 
long long tipsLen, float tip_drop_ratio, long long stops_threshold, R_to_U* ruIndex, float chimeric_rate, float drop_ratio, 
int max_hang, int min_ovlp, uint32_t chainLenThres, long long gap_fuzz, bub_label_t* b_mask_t, long long no_trio_recover, uint8_t *cmk)
{
    kvec_asg_arc_t_warp new_rtg_edges;
    kv_init(new_rtg_edges.a);
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);

    hap_cov_t *cov = NULL;
    asg_t *copy_sg = copy_read_graph(sg);
    ma_ug_t *copy_ug = copy_untig_graph(ug);    
    adjust_utg_by_primary(&copy_ug, copy_sg, TRIO_THRES, sources, reverse_sources, coverage_cut, 
    tipsLen, tip_drop_ratio, stops_threshold, ruIndex, chimeric_rate, drop_ratio, 
    max_hang, min_ovlp, &new_rtg_edges, &cov, b_mask_t, 0, 0);
    ma_ug_destroy(copy_ug); copy_ug = NULL;
    asg_destroy(copy_sg); copy_sg = NULL;

    uint32_t beg_idx, occ;
    bubble_type bub; 
    memset(&bub, 0, sizeof(bubble_type));
    copy_ug = copy_untig_graph(ug);
    reset_bub(&bub, ug, cov->t_ch, &new_rtg_edges);
    beg_idx = bub.f_bub; occ = bub.b_bub + bub.b_end_bub + bub.tangle_bub;
    rescue_bubbles_by_contained_reads(ug, sg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, chainLenThres, beg_idx, occ, &bub, b_mask_t);

    ma_ug_destroy(ug); ug = NULL; ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);
    reset_bub(&bub, ug, cov->t_ch, &new_rtg_edges);
    beg_idx = bub.f_bub; occ = bub.b_bub + bub.b_end_bub + bub.tangle_bub;
    rescue_bubbles_by_missing_ovlp(ug, sg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, chainLenThres, beg_idx, occ, &bub, b_mask_t);

    ma_ug_destroy(ug); ug = NULL; ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);
    reset_bub(&bub, ug, cov->t_ch, &new_rtg_edges);
    beg_idx = bub.f_bub; occ = bub.b_bub + bub.b_end_bub + bub.tangle_bub;
    rescue_bubbles_by_missing_ovlp_backward(ug, sg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, chainLenThres, beg_idx, occ, &bub, b_mask_t);

    if(cmk) {
        ma_ug_destroy(ug); ug = NULL; ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);
        reset_bub(&bub, ug, cov->t_ch, &new_rtg_edges);
        beg_idx = bub.f_bub; occ = bub.b_bub + bub.b_end_bub + bub.tangle_bub;
        rescue_bubbles_by_cmk_reads(ug, sg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, chainLenThres, beg_idx, occ, &bub, b_mask_t, cmk);
    }
    /**
    if((!no_trio_recover) && (ha_opt_triobin(&asm_opt)))
    {
        ma_ug_destroy(ug); ug = NULL; ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);
        reset_bub(&bub, ug, cov->t_ch, &new_rtg_edges);
        // rescue_missing_hap_ovlp(ug, sg, sources, coverage_cut, max_hang, min_ovlp, &bub, gap_fuzz);
        reduce_hamming_error_adv(ug, sg, sources, coverage_cut, max_hang, min_ovlp, gap_fuzz, ruIndex, &bub);
    }
    **/

    destory_bubbles(&bub);
    destory_hap_cov_t(&cov);
    ma_ug_destroy(ug);
    kv_destroy(new_rtg_edges.a);
    ma_ug_destroy(copy_ug); copy_ug = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movss %xmm2, 0x1c(%rsp)
movss %xmm1, 0x18(%rsp)
movq %r9, 0x8(%rsp)
movss %xmm0, 0x14(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rdx, 0x38(%rsp)
movq %rsi, %rbx
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %esi, %esi
callq 0x781bd
movq %rax, %r13
movq %rsp, %rax
andq $0x0, (%rax)
movq %r12, %rdi
callq 0x8c8a9
movq %rax, %r15
movq %r13, %rdi
callq 0x8c99a
leaq 0x60(%rsp), %rbp
movq %rax, (%rbp)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x28(%rsp), %rcx
movq %rbx, %r8
movq 0x30(%rsp), %r9
movss 0x1c(%rsp), %xmm1
movss 0x20(%rsp), %xmm2
movss 0x24(%rsp), %xmm3
pushq $0x0
pushq $0x0
pushq 0x1f0(%rsp)
leaq 0x20(%rsp), %rbx
pushq %rbx
leaq 0x68(%rsp), %r14
pushq %r14
movl 0x1f0(%rsp), %eax
pushq %rax
movl 0x1f0(%rsp), %eax
pushq %rax
pushq 0x1f0(%rsp)
pushq 0x50(%rsp)
callq 0x8cb4c
addq $0x50, %rsp
movq (%rbp), %rdi
callq 0x6921d
movq %r15, %rdi
callq 0x68f68
leaq 0x68(%rsp), %rbp
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r13, %rdi
callq 0x8c99a
movq %rax, 0x8(%rsp)
movq (%rbx), %rax
movq 0xa8(%rax), %rdx
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0xbb46f
movl 0x80(%rbp), %eax
movl 0x90(%rbp), %ecx
addl 0x88(%rbp), %ecx
addl 0x98(%rbp), %ecx
subq $0x30, %rsp
movq 0x208(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl 0x1f0(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq 0x68(%rsp), %r14
movq %r14, %rdx
movq 0x60(%rsp), %r15
movq %r15, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %r9d
callq 0xb8188
addq $0x30, %rsp
movq %r13, %rdi
callq 0x6921d
movq %r12, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %rbx
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %rdx
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x40(%rsp), %rcx
callq 0xbb46f
movl 0x80(%rbp), %eax
movl 0x90(%rbp), %ecx
addl 0x88(%rbp), %ecx
addl 0x98(%rbp), %ecx
subq $0x30, %rsp
movq 0x208(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl 0x1f0(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %r13
movq %r15, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %r15d
movl %r15d, %r9d
callq 0xb9cd0
addq $0x30, %rsp
movq %rbx, %rdi
movq %r13, %rbx
callq 0x6921d
movq %r12, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %r13
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %rdx
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x40(%rsp), %rcx
callq 0xbb46f
movl 0x80(%rbp), %eax
movl 0x90(%rbp), %r10d
addl 0x88(%rbp), %r10d
addl 0x98(%rbp), %r10d
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x1b0(%rsp), %r8
movl %r15d, %r9d
pushq 0x1d8(%rsp)
pushq %rbp
pushq %r10
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
movl 0x1e8(%rsp), %eax
movq %rax, %r15
pushq %rax
callq 0xba4ad
addq $0x30, %rsp
cmpq $0x0, 0x1e8(%rsp)
je 0xbc2ca
movq %r13, %rdi
callq 0x6921d
movq %r12, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %r13
movq (%rsp), %rax
movq 0xa8(%rax), %rdx
leaq 0x40(%rsp), %rcx
movq %rbp, %rdi
movq %r13, %rsi
callq 0xbb46f
movl 0xe8(%rsp), %eax
movl 0xf8(%rsp), %ecx
addl 0xf0(%rsp), %ecx
addl 0x100(%rsp), %ecx
subq $0x40, %rsp
movq 0x218(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl %r15d, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x1f0(%rsp), %r8
movl 0x1f8(%rsp), %r9d
callq 0xb8f36
addq $0x40, %rsp
leaq 0x68(%rsp), %rdi
callq 0xf6a60
movq %rsp, %rdi
callq 0x8d243
movq %r13, %rdi
callq 0x6921d
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x6921d
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq