// File: /workspace/github_cpp_makefile_star2_O1/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, 0x54(%rsp)
movl %r8d, 0x50(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movq 0x150(%rsp), %rbp
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 0x1303c7
movsd %xmm0, 0x78(%rsp)
leaq 0xb0(%rsp), %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%r13)
movapd %xmm0, 0x40(%r13)
movapd %xmm0, 0x30(%r13)
movapd %xmm0, 0x20(%r13)
movapd %xmm0, 0x10(%r13)
andq $0x0, 0x60(%r13)
movapd %xmm0, (%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 0xbf22d
xorl %r15d, %r15d
cmpq %r15, 0x80(%rbp)
je 0xea8f
movq %rax, %r13
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r13, 0x38(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movl %ebx, %ecx
movq 0x80(%rsp), %r8
movq 0x88(%rsp), %r9
callq 0xf9919
addl %eax, %r15d
incl %ebx
cmpq %rbx, 0x80(%rbp)
ja 0xea1f
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 0x60(%rsp), %r12
testq %r12, %r12
je 0xebaa
pushq $0x1
popq %rbp
movq 0x70(%rsp), %r13
xorl %ebx, %ebx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl 0x4(%r14), %ecx
andl %edi, %ecx
movl (%r14), %eax
cmpl %eax, %ecx
jne 0xeb1e
leal (%rax,%rax), %ecx
testl %eax, %eax
pushq $0x10
popq %rax
cmovel %eax, %ecx
movl %ecx, (%r14)
movq 0x8(%r14), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %rax, 0x8(%r14)
movq 0x8(%r14), %rax
movl 0x4(%r14), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r14)
imulq $0x18, %rdx, %rcx
imulq $0x18, %rbx, %rdx
movq 0x10(%r13,%rdx), %rsi
movq %rsi, 0x10(%rax,%rcx)
movupd (%r13,%rdx), %xmm0
movupd %xmm0, (%rax,%rcx)
movl %ebp, %ebx
incl %ebp
cmpq %rbx, %r12
ja 0xeaed
movq 0x28(%r14), %rdi
callq 0x2220
andq $0x0, 0x28(%r14)
andb $0x7f, 0x7(%r14)
movq %r14, %rdi
callq 0xa02fa
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
movq %r14, %rdi
movq 0x158(%rsp), %rsi
callq 0xa8c8f
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl 0x4c(%rsp), %ebp
andl %ebx, %ebp
movq 0x27b424(%rip), %rax    # 0x289fe0
movq (%rax), %r12
callq 0x1303c7
subsd 0x78(%rsp), %xmm0
andl 0x4(%r14), %ebx
subl %ebp, %ebx
leaq 0x21d1f2(%rip), %rsi    # 0x22bdc9
leaq 0x21e09a(%rip), %rdx    # 0x22cc78
movq %r12, %rdi
movl %ebx, %ecx
movl %r15d, %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