// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movl %r9d, -0x50(%rbp)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0xa8(%rbp)
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rdi
movl $0x1c, %esi
callq 0x2140
movq %rax, -0x110(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x110(%rbp), %rsi
callq 0xfde50
movq %rax, -0x118(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq 0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x15b953
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq 0x10(%rbp), %rdx
movl -0x54(%rbp), %ecx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movl -0x4c(%rbp), %r12d
movl -0x50(%rbp), %r15d
leaq 0x2699e1(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %r14
movq -0x68(%rbp), %rbx
movq -0x118(%rbp), %rax
leaq -0x88(%rbp), %r13
leaq -0xa0(%rbp), %r11
leaq -0x110(%rbp), %r10
movq %r13, (%rsp)
movl %r12d, 0x8(%rsp)
movl %r15d, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x15aa00
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x15b8b5
movq -0x110(%rbp), %rdi
callq 0x2210
movq -0xf8(%rbp), %rdi
callq 0x2210
movq -0xe0(%rbp), %rdi
callq 0x2210
movq -0xc8(%rbp), %rdi
callq 0x2210
movq -0xb0(%rbp), %rdi
callq 0x2210
movq $0x0, -0x120(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpq -0xa0(%rbp), %rax
jae 0x15b9f6
movq -0x38(%rbp), %rdi
callq 0xd4050
movq %rax, -0x120(%rbp)
movq -0x90(%rbp), %rcx
movl -0x54(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq -0x120(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x15b9a1
cmpq $0x0, -0xa0(%rbp)
je 0x15ba4d
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x4(%rax)
movq -0x38(%rbp), %rdi
callq 0xcdd10
movq -0x38(%rbp), %rdi
callq 0xce020
movq -0x38(%rbp), %rdi
movq 0x18(%rbp), %rax
movl %eax, %esi
callq 0xda630
movq 0x26858c(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x128(%rbp)
callq 0x1b7ca0
movq -0x128(%rbp), %rdi
subsd -0xa8(%rbp), %xmm0
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl -0x58(%rbp), %ecx
movl -0x5c(%rbp), %r8d
leaq 0x22d157(%rip), %rsi    # 0x388be4
leaq 0x22dfff(%rip), %rdx    # 0x389a93
movb $0x1, %al
callq 0x22a0
movq -0x68(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0x90(%rbp), %rdi
callq 0x2210
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)