// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movl %r8d, 0x54(%rsp)
movq %rcx, 0x80(%rsp)
movq %rdx, 0x88(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movl 0x4(%rsi), %eax
movl %eax, 0x4c(%rsp)
movl 0x14(%rsi), %edi
addl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x58(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
callq 0x1365ef
movsd %xmm0, 0x78(%rsp)
leaq 0xb0(%rsp), %r13
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%r13)
movupd %xmm0, 0x48(%r13)
movupd %xmm0, 0x38(%r13)
movupd %xmm0, 0x28(%r13)
movupd %xmm0, 0x18(%r13)
movupd %xmm0, 0x8(%r13)
movq %rbx, %r12
movq 0x18(%rbx), %rbx
movl 0x14(%rbx), %edi
addl %edi, %edi
pushq $0x1c
popq %rsi
callq 0x2120
movq %rax, (%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc0162
movq 0x150(%rsp), %r13
xorl %ebx, %ebx
movl $0x0, %ebp
cmpq %rbx, 0x80(%r13)
je 0xeb35
xorl %ebx, %ebx
movl %r15d, 0x50(%rsp)
xorl %ebp, %ebp
movq %rax, 0x38(%rsp)
leaq 0xb0(%rsp), %rdx
movq %rdx, 0x30(%rsp)
leaq 0x60(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq 0x58(%rsp), %rdx
movq %rdx, 0x20(%rsp)
movl 0x50(%rsp), %edx
movl %edx, 0x10(%rsp)
movl 0x54(%rsp), %edx
movl %edx, 0x8(%rsp)
leaq 0x90(%rsp), %rdx
movq %rdx, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %ebx, %ecx
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %r9
movq %rax, %r15
callq 0xfcf1f
addl %eax, %ebp
movq %r15, %rax
incl %ebx
cmpq %rbx, 0x80(%r13)
ja 0xeabb
movq 0x60(%rsp), %rbx
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
testq %rbx, %rbx
je 0xec51
pushq $0x1
popq %r12
movq 0x70(%rsp), %r13
xorl %r15d, %r15d
movl 0x4(%r14), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl (%r14), %ecx
jne 0xebc9
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
pushq $0x10
popq %rcx
cmovel %ecx, %eax
movl %eax, (%r14)
movq 0x8(%r14), %rdi
imulq $0x18, %rax, %rsi
callq 0x2370
movq %rax, 0x8(%r14)
movl 0x4(%r14), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0xebcd
movq 0x8(%r14), %rax
leal 0x1(%rdx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0x4(%r14)
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
imulq $0x18, %r15, %rdx
movq 0x10(%r13,%rdx), %rsi
movq %rsi, 0x10(%rax,%rcx)
movupd (%r13,%rdx), %xmm0
movupd %xmm0, (%rax,%rcx)
movl %r12d, %r15d
incl %r12d
cmpq %r15, %rbx
ja 0xeb8b
movq 0x28(%r14), %rdi
callq 0x2220
andq $0x0, 0x28(%r14)
andb $0x7f, 0x7(%r14)
movq %r14, %rdi
callq 0xa0a7d
movq %r14, %rdi
callq 0xa0ab6
movq %r14, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%r14)
movq %r14, %rdi
movq 0x158(%rsp), %rsi
callq 0xa9d09
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl 0x4c(%rsp), %r12d
andl %ebx, %r12d
movq 0x28637b(%rip), %rax    # 0x294fe0
movq (%rax), %r15
callq 0x1365ef
subsd 0x78(%rsp), %xmm0
andl 0x4(%r14), %ebx
subl %r12d, %ebx
leaq 0x229338(%rip), %rsi    # 0x237fb9
leaq 0x22a1e0(%rip), %rdx    # 0x238e68
movq %r15, %rdi
movl %ebx, %ecx
movl %ebp, %r8d
movb $0x1, %al
callq 0x22b0
movq 0x58(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq