// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movss %xmm0, -0x54(%rbp)
movq %r9, -0x60(%rbp)
movss %xmm1, -0x64(%rbp)
movss %xmm2, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x30(%rbp), %rdi
callq 0x10b830
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rdi
callq 0x10ba70
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x50(%rbp), %r9
movss -0x54(%rbp), %xmm1
movq -0x60(%rbp), %r12
movq 0x10(%rbp), %r15
movss -0x64(%rbp), %xmm2
movss -0x68(%rbp), %xmm3
movl 0x18(%rbp), %r14d
movl 0x20(%rbp), %ebx
movq 0x38(%rbp), %rax
leaq -0xa8(%rbp), %rdi
movss 0x22acc6(%rip), %xmm0   # 0x38689c
leaq -0x88(%rbp), %r11
leaq -0x98(%rbp), %r10
xorl %r13d, %r13d
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movl %r14d, 0x10(%rsp)
movl %ebx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x0, 0x40(%rsp)
callq 0x10bde0
movq -0xa8(%rbp), %rdi
callq 0xcd6f0
movq $0x0, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xcd170
movq $0x0, -0xa0(%rbp)
leaq -0x1c0(%rbp), %rdi
xorl %esi, %esi
movl $0x110, %edx            # imm = 0x110
callq 0x2100
movq -0x90(%rbp), %rdi
callq 0x10ba70
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rcx
callq 0x15a990
movq -0x140(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x138(%rbp), %rax
addq -0x130(%rbp), %rax
addq -0x128(%rbp), %rax
movl %eax, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %r8
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %r15d
movl 0x28(%rbp), %r14d
movl -0xac(%rbp), %ebx
movl -0xb0(%rbp), %r11d
movq 0x38(%rbp), %rax
leaq -0x1c0(%rbp), %r10
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x155bd0
movq -0x90(%rbp), %rdi
callq 0xcd6f0
movq $0x0, -0x90(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rcx
callq 0x15a990
movq -0x140(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x138(%rbp), %rax
addq -0x130(%rbp), %rax
addq -0x128(%rbp), %rax
movl %eax, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %r8
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %r15d
movl 0x28(%rbp), %r14d
movl -0xac(%rbp), %ebx
movl -0xb0(%rbp), %r11d
movq 0x38(%rbp), %rax
leaq -0x1c0(%rbp), %r10
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x158520
movq -0x90(%rbp), %rdi
callq 0xcd6f0
movq $0x0, -0x90(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rcx
callq 0x15a990
movq -0x140(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x138(%rbp), %rax
addq -0x130(%rbp), %rax
addq -0x128(%rbp), %rax
movl %eax, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %r8
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %r15d
movl 0x28(%rbp), %r14d
movl -0xac(%rbp), %ebx
movl -0xb0(%rbp), %r11d
movq 0x38(%rbp), %rax
leaq -0x1c0(%rbp), %r10
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movl %r11d, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x158ff0
cmpq $0x0, 0x48(%rbp)
je 0x15bfb0
movq -0x90(%rbp), %rdi
callq 0xcd6f0
movq $0x0, -0x90(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rcx
callq 0x15a990
movq -0x140(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x138(%rbp), %rax
addq -0x130(%rbp), %rax
addq -0x128(%rbp), %rax
movl %eax, -0xb0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %r8
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %r12d
movl 0x28(%rbp), %r15d
movl -0xac(%rbp), %r14d
movl -0xb0(%rbp), %ebx
movq 0x38(%rbp), %r10
movq 0x48(%rbp), %rax
leaq -0x1c0(%rbp), %r11
movl %r12d, (%rsp)
movl %r15d, 0x8(%rsp)
movl %r14d, 0x10(%rsp)
movl %ebx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x157050
leaq -0x1c0(%rbp), %rdi
callq 0x1c4a90
leaq -0x98(%rbp), %rdi
callq 0x10c960
movq -0x90(%rbp), %rdi
callq 0xcd6f0
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0xa8(%rbp), %rdi
callq 0xcd6f0
movq $0x0, -0xa8(%rbp)
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)