// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movq %rsi, 0x38(%rsp)
movq %rdi, %r12
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
xorl %ebx, %ebx
movq %rbx, 0x10(%rax)
xorl %esi, %esi
callq 0xb1a83
movq %rax, %r13
movq %rsp, %rax
movq %rbx, (%rax)
movq %r12, %rdi
callq 0xc802c
movq %rax, %rbx
movq %r13, %rdi
callq 0xc815c
leaq 0x60(%rsp), %rbp
movq %rax, (%rbp)
subq $0x8, %rsp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x30(%rsp), %rcx
movq %r14, %r8
movq 0x38(%rsp), %r9
movss 0x1c(%rsp), %xmm1
movss 0x20(%rsp), %xmm2
movss 0x24(%rsp), %xmm3
xorl %eax, %eax
pushq %rax
pushq %rax
xorl %r15d, %r15d
pushq 0x1f0(%rsp)
leaq 0x20(%rsp), %r14
pushq %r14
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 0xc8339
addq $0x50, %rsp
movq (%rbp), %rdi
callq 0xa0004
movq %r15, (%rbp)
movq %rbx, %rdi
callq 0x9fcdb
leaq 0x68(%rsp), %r15
movl $0x110, %edx            # imm = 0x110
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r13, %rdi
callq 0xc815c
movq %rax, 0x8(%rsp)
movq %rax, (%rbp)
movq 0x20(%rsp), %rbp
movq (%r14), %rax
movq 0xa8(%rax), %rbx
movq %r15, %rdi
callq 0x1380d4
movl $0x110, %edx            # imm = 0x110
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x40(%rsp), %rax
xorl %ecx, %ecx
movq %rcx, (%rax)
movq 0x10(%rbx), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x138ab3
movl 0x80(%r15), %eax
movl 0x90(%r15), %ecx
addl 0x88(%r15), %ecx
addl 0x98(%r15), %ecx
subq $0x30, %rsp
movq 0x208(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl 0x1f0(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq 0x68(%rsp), %r14
movq %r14, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %eax
movq %rax, %r9
callq 0xf52f8
addq $0x30, %rsp
movq %r13, %rdi
callq 0xa0004
movq %r12, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %rbx
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %r13
movq %r15, %rdi
callq 0x1380d4
movl $0x110, %edx            # imm = 0x110
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x40(%rsp), %rax
xorl %ecx, %ecx
movq %rcx, (%rax)
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x138ab3
movl 0x80(%r15), %eax
movl 0x90(%r15), %ecx
addl 0x88(%r15), %ecx
addl 0x98(%r15), %ecx
subq $0x30, %rsp
movq 0x208(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl 0x1f0(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
movq 0x1e0(%rsp), %r8
movl 0x1e8(%rsp), %r9d
callq 0xf7917
addq $0x30, %rsp
movq %rbx, %rdi
callq 0xa0004
movq %r12, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %r13
movq %rsp, %rax
movq (%rax), %rax
movq 0xa8(%rax), %rbx
movq %r15, %rdi
callq 0x1380d4
movl $0x110, %edx            # imm = 0x110
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
xorl %ecx, %ecx
leaq 0x40(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rbx), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x138ab3
movl 0x80(%r15), %eax
movl 0x90(%r15), %r10d
addl 0x88(%r15), %r10d
addl 0x98(%r15), %r10d
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rbp
movq %r14, %rcx
movq 0x1b0(%rsp), %r8
movl 0x1b8(%rsp), %r9d
pushq 0x1d8(%rsp)
pushq %r15
pushq %r10
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
callq 0xf83ab
addq $0x30, %rsp
xorl %eax, %eax
cmpq %rax, 0x1e8(%rsp)
je 0xfa5d1
movq %r13, %rdi
callq 0xa0004
movq %r12, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %r13
movq (%rsp), %rax
movq 0xa8(%rax), %rbx
movq %r15, %rdi
callq 0x1380d4
movl $0x110, %edx            # imm = 0x110
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq $0x0, 0x40(%rsp)
movq 0x10(%rbx), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x138ab3
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 %r15, 0x20(%rsp)
movl %ecx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl 0x200(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq 0x60(%rsp), %rdx
movq %rbp, %rcx
movq 0x1f0(%rsp), %r8
movl 0x1f8(%rsp), %r9d
callq 0xf6604
addq $0x40, %rsp
leaq 0x68(%rsp), %rdi
callq 0x1380d4
movq %rsp, %rdi
callq 0xc8943
movq %r13, %rdi
callq 0xa0004
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0xa0004
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq