// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: rescue_missing_hap_ovlp(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, int, int, bubble_type*, long long)
void rescue_missing_hap_ovlp(ma_ug_t *u_g, asg_t *r_g, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp, bubble_type* bub, long long gap_fuzz)
{
    uint32_t i, n_arc = r_g->n_arc, fix_bub = 0;
    uint8_t* vis_flag = NULL; CALLOC(vis_flag, r_g->n_seq*2);
    kvec_t_u32_warp stack; kv_init(stack.a);
    kv_asg_arc_t e; kv_init(e);
    double index_time = yak_realtime();

    buf_t b; memset(&b, 0, sizeof(buf_t)); 
    b.a = (binfo_t*)calloc(u_g->g->n_seq * 2, sizeof(binfo_t));
    uint64_t tLen = get_bub_pop_max_dist_advance(u_g->g, &b);
    for (i = 0; i < bub->f_bub; i++)
    {
        fix_bub += bub_complex(r_g, u_g, bub, i, sources, coverage_cut, &stack, max_hang, min_ovlp, R_INF.trio_flag, vis_flag, &e, &b, tLen);
    }
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);

    asg_arc_t* p = NULL;
    for (i = 0; i < e.n; i++)
    {
        p = asg_arc_pushp(r_g);
        *p = e.a[i];
    }
    if(e.n != 0)
    {
        free(r_g->idx);
        r_g->idx = 0;
        r_g->is_srt = 0;
        asg_cleanup(r_g);
        asg_symm(r_g);
        asg_arc_del_trans(r_g, gap_fuzz);
        // for (i = 0; i < bub->f_bub; i++)
        // {
        //     debug_bubble_chain(r_g, u_g, bub, i, &stack, vis_flag);
        // }
    }

    fprintf(stderr, "[M::%s::%.3f] # inserted edges: %u, # fixed bubbles: %u\n", 
                        __func__, yak_realtime() - index_time, r_g->n_arc - n_arc, fix_bub);

    free(vis_flag);
    kv_destroy(stack.a);
    kv_destroy(e);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r9d, 0x4c(%rsp)
movl %r8d, 0x48(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq 0x150(%rsp), %r13
movl 0x4(%rsi), %eax
movl %eax, 0x44(%rsp)
movl 0x14(%rsi), %edi
addl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x50(%rsp)
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x70(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
callq 0xefea7
movsd %xmm0, 0x58(%rsp)
leaq 0xb0(%rsp), %r15
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%r15)
movupd %xmm0, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
movupd %xmm0, 0x28(%r15)
movupd %xmm0, 0x18(%r15)
movupd %xmm0, 0x8(%r15)
movq 0x18(%r12), %rbx
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1c
popq %rsi
callq 0x2120
movq %rax, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x84975
movq %rax, %r15
xorl %ebp, %ebp
xorl %ebx, %ebx
movl %ebx, %eax
cmpq %rax, 0x80(%r13)
jbe 0xbbe2c
movq %r15, 0x38(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %ebx, %ecx
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %r9
callq 0xbb4bd
addl %eax, %ebp
incl %ebx
jmp 0xbbdbf
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
movq 0x110(%rsp), %rdi
callq 0x2220
movq 0x70(%rsp), %r15
movq 0x80(%rsp), %rbx
xorl %eax, %eax
movl %eax, %r12d
cmpq %r12, %r15
jbe 0xbbea9
movq %r14, %rdi
callq 0x6c45d
imulq $0x18, %r12, %rcx
movupd (%rbx,%rcx), %xmm0
movupd %xmm0, (%rax)
movq 0x10(%rbx,%rcx), %rcx
movq %rcx, 0x10(%rax)
leal 0x1(%r12), %eax
jmp 0xbbe7c
testq %r15, %r15
je 0xbbee1
movq 0x28(%r14), %rdi
callq 0x2220
andq $0x0, 0x28(%r14)
andb $0x7f, 0x7(%r14)
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
movq %r14, %rdi
movq 0x158(%rsp), %rsi
callq 0x701c4
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl 0x44(%rsp), %r13d
andl %r15d, %r13d
movq 0x1970ea(%rip), %rax    # 0x252fe0
movq (%rax), %r12
callq 0xefea7
subsd 0x58(%rsp), %xmm0
andl 0x4(%r14), %r15d
subl %r13d, %r15d
leaq 0x12dfa7(%rip), %rsi    # 0x1e9eb9
leaq 0x12ee4f(%rip), %rdx    # 0x1ead68
movq %r12, %rdi
movl %r15d, %ecx
movl %ebp, %r8d
movb $0x1, %al
callq 0x22b0
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq