// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdx, 0x38(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
xorl %r13d, %r13d
movq %r13, 0x10(%rax)
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r15
movq %rsp, %rax
movq %r13, (%rax)
movq %r14, %rdi
callq 0xc8dad
movq %rax, %r13
movq %r15, %rdi
callq 0xc8eab
leaq 0x60(%rsp), %rbp
movq %rax, (%rbp)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq %rbx, 0x38(%rsp)
movq %rbx, %r8
movq 0x30(%rsp), %r9
movss 0x1c(%rsp), %xmm1
movss 0x20(%rsp), %xmm2
movss 0x24(%rsp), %xmm3
movl $0x0, %eax
pushq %rax
pushq %rax
pushq 0x1f0(%rsp)
leaq 0x20(%rsp), %rbx
pushq %rbx
leaq 0x68(%rsp), %rax
pushq %rax
movl 0x1f0(%rsp), %eax
pushq %rax
movl 0x1f0(%rsp), %eax
pushq %rax
pushq 0x1f0(%rsp)
pushq 0x50(%rsp)
callq 0xc906a
addq $0x50, %rsp
movq (%rbp), %rdi
callq 0xa0761
movq %r13, %rdi
callq 0xa0494
leaq 0x68(%rsp), %rbp
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r15, %rdi
callq 0xc8eab
movq %rax, 0x8(%rsp)
movq (%rbx), %rax
movq 0xa8(%rax), %rbx
movq %rbp, %rdi
callq 0x13dea7
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorl %ecx, %ecx
leaq 0x40(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rbx), %rdx
movq %r15, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x13e8a1
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 %r15, %rdi
movq %r14, %rsi
movq 0x68(%rsp), %r13
movq %r13, %rdx
movq 0x60(%rsp), %r12
movq %r12, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %eax
movq %rax, %r9
callq 0xf8078
addq $0x30, %rsp
movq %r15, %rdi
callq 0xa0761
movq %r14, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r15
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %rbx
movq %rbp, %rdi
callq 0x13dea7
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorl %eax, %eax
leaq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rbx), %rdx
movq %r15, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x13e8a1
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 %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %r9d
callq 0xfab24
addq $0x30, %rsp
movq %r15, %rdi
callq 0xa0761
movq %r14, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r15
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %rbx
movq %rbp, %rdi
callq 0x13dea7
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorl %eax, %eax
leaq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rbx), %rdx
movq %r15, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x13e8a1
movl 0x80(%rbp), %eax
movl 0x90(%rbp), %r10d
addl 0x88(%rbp), %r10d
addl 0x98(%rbp), %r10d
movq %r15, %rdi
movq %r14, %rbx
movq %r14, %rsi
movq %r13, %r14
movq %r13, %rdx
movq %r12, %r13
movq %r12, %rcx
movq 0x1b0(%rsp), %r8
movl 0x1b8(%rsp), %r9d
pushq 0x1d8(%rsp)
pushq %rbp
pushq %r10
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
callq 0xfb816
addq $0x30, %rsp
xorl %eax, %eax
cmpq %rax, 0x1e8(%rsp)
je 0xfddb2
movq %r15, %rdi
callq 0xa0761
movq %rbx, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %r15
movq (%rsp), %rax
movq 0xa8(%rax), %rbx
movq %rbp, %rdi
callq 0x13dea7
movl $0x110, %edx            # imm = 0x110
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq $0x0, 0x40(%rsp)
movq 0x10(%rbx), %rdx
movq %r15, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x13e8a1
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 0x200(%rsp), %eax
movl %eax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r13, %rcx
movq 0x1f0(%rsp), %r8
movl 0x1f8(%rsp), %r9d
callq 0xf95da
addq $0x40, %rsp
leaq 0x68(%rsp), %rdi
callq 0x13dea7
movq %rsp, %rdi
callq 0xc9bdb
movq %r15, %rdi
callq 0xa0761
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0xa0761
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq