// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: try_rescue_overlaps(ma_hit_t_alloc*, ma_hit_t_alloc*, unsigned long, unsigned long, unsigned int)
void try_rescue_overlaps(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev, uint64_t rn, uint64_t rescue_threshold, uint32_t is_del)
{
    double startTime = Get_T();
    int64_t revises = 0, dd, dp, old_dp, start, max_dp, m; uint32_t qn, tn, qs, qe; uint64_t ff, i, j; ma_sub_t max_interval; 
    kvec_t(uint64_t) ev; kv_init(ev);
    kvec_t(uint64_t) evi; kv_init(evi);
    kvec_t(uint32_t) b; kv_init(b);
    
    for (i = 0; i < rn; i++) {
        ev.n = evi.n = 0;
        for (j = 0; j < rev[i].length; j++) {
            if(is_del && rev[i].buffer[j].del) continue;
            qn = Get_qn(rev[i].buffer[j]); tn = Get_tn(rev[i].buffer[j]);
            dd = get_specific_overlap(&(paf[tn]), tn, qn);
            if((dd != -1) && ((!is_del) || (paf[tn].buffer[dd].del == 0))) {
                ff = tn; ff <<= 32; ff |= (uint64_t)(dd);
                kv_push(uint64_t, ev, ff); kv_push(uint64_t, evi, j);
            }
        }

        ///based on qn, all edges at edge_vector/edge_vector_index come from different haplotype
        ///but at another direction, all these edges come from the same haplotype
        //here we want to recover these edges
        if(evi.n >= rescue_threshold) {
            kv_resize(uint32_t, b, evi.n); b.n = 0;
            for (j = 0; j < evi.n; j++) {
                qs = Get_qs(rev[i].buffer[evi.a[j]]);
                qe = Get_qe(rev[i].buffer[evi.a[j]]);
                kv_push(uint32_t, b, qs<<1);
                kv_push(uint32_t, b, qe<<1|1);
            }

            ks_introsort_uint32_t(b.n, b.a);
            dp = 0, start = 0, max_dp = 0;
            max_interval.s = max_interval.e = 0;
            for (j = 0, dp = 0; j < b.n; ++j)  {
                old_dp = dp;
                ///if a[j] is qe
                if (b.a[j]&1) --dp;
                else ++dp;
                
                if(old_dp < dp) {///b.a[j] is qs
                    ///case 2, a[j] is qs
                    //here should use dp >= max_dp, instead of dp > max_dp
                    if(dp >= max_dp) {
                        start = b.a[j]>>1;
                        max_dp = dp;
                    }
                } else if (old_dp > dp) {///old_dp > min_dp, b.a[j] is qe
                    if(old_dp == max_dp) {
                        max_interval.s = start;
                        max_interval.e = b.a[j]>>1;
                    }
                }
            }

            if(((uint64_t)max_dp) >= rescue_threshold) {
                m = 0;
                for (j = 0; j < evi.n; j++) {
                    qs = Get_qs(rev[i].buffer[evi.a[j]]);
                    qe = Get_qe(rev[i].buffer[evi.a[j]]);
                    if(qs <= max_interval.s && qe >= max_interval.e) {
                        evi.a[m] = evi.a[j]; ev.a[m] = ev.a[j]; m++;
                    }
                }
                evi.n = ev.n = m;

                ///the read itself do not have these overlaps, but all related reads have
                ///we need to remove all overlaps from rev_paf[i], and then add all overlaps to paf[i]
                remove_overlaps(&(rev[i]), evi.a, evi.n);
                add_overlaps_from_different_sources(paf, &(paf[i]), ev.a, ev.n);
                revises = revises + ev.n;
            }
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r8d, 0x24(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
callq 0x251c
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
movq $0x0, 0x50(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x48(%rsp)
xorl %r15d, %r15d
xorl %esi, %esi
cmpq 0x68(%rsp), %rax
je 0xb4ba2
movq %r15, 0x40(%rsp)
movq %r13, 0x18(%rsp)
movq %rax, 0x30(%rsp)
imulq $0x18, %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x70(%rsp)
addq %rax, %rcx
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movq %rcx, (%rsp)
movq %rsi, 0x28(%rsp)
movl 0xc(%rcx), %eax
cmpq %rax, %r13
jae 0xb49b2
movq (%rcx), %rax
cmpl $0x0, 0x24(%rsp)
je 0xb48c0
cmpq $0x0, 0x18(%rax,%r15)
js 0xb49a6
movl 0x4(%rax,%r15), %edx
movl 0xc(%rax,%r15), %r12d
imulq $0x18, %r12, %rbp
addq 0x58(%rsp), %rbp
movq %rbp, %rdi
movl %r12d, %esi
callq 0x696ab
cmpq $-0x1, %rax
je 0xb4931
movq %rax, %rbx
cmpl $0x0, 0x24(%rsp)
je 0xb48fe
movq (%rbp), %rax
imulq $0x28, %rbx, %rcx
cmpq $0x0, 0x18(%rax,%rcx)
js 0xb4931
movq 0x50(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0xb493c
leaq (%rax,%rax), %rbp
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %rbp, 0x50(%rsp)
jmp 0xb4941
movq 0x28(%rsp), %rsi
movq (%rsp), %rcx
jmp 0xb49a6
movq 0x8(%rsp), %rax
shlq $0x20, %r12
orq %rbx, %r12
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rcx
movq %r12, (%rax,%rcx,8)
movq 0x48(%rsp), %rax
cmpq %rax, %r14
jne 0xb4987
leaq (%rax,%rax), %rbx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x2370
movq %rbx, 0x48(%rsp)
jmp 0xb498c
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rsi
movq (%rsp), %rcx
incq 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %r13, (%rax,%r14,8)
incq %r14
incq %r13
addq $0x28, %r15
jmp 0xb489e
cmpq 0x38(%rsp), %r14
jae 0xb49cd
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %r13
movq 0x40(%rsp), %r15
jmp 0xb4b9a
cmpq %r14, %rsi
movq 0x18(%rsp), %r13
movq 0x40(%rsp), %r15
jae 0xb4a2e
leaq -0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rbx
shrq $0x10, %rbx
orq %rax, %rbx
leaq 0x4(,%rbx,4), %rsi
incq %rbx
movq %r15, %rdi
callq 0x2370
movq (%rsp), %rcx
movq %rbx, %rsi
movq %rax, %r15
movq %r14, 0x10(%rsp)
movq %r14, %rbx
xorl %r12d, %r12d
subq $0x1, %rbx
jb 0xb4ab6
imulq $0x28, (%r13,%r12,4), %rax
movq (%rcx), %rcx
movl (%rcx,%rax), %ebp
movl 0x8(%rcx,%rax), %r14d
cmpq %rsi, %r12
jne 0xb4a79
movq %r15, %rdi
leaq (%rsi,%rsi), %r15
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
callq 0x2370
movq %r15, %rsi
movq %rax, %r15
addl %ebp, %ebp
leaq 0x1(%r12), %rax
movl %ebp, (%r15,%r12,4)
cmpq %rsi, %rax
jne 0xb4a9f
movq %r15, %rdi
leaq (%rsi,%rsi), %r15
shlq $0x3, %rsi
callq 0x2370
movq %r15, %rsi
movq %rax, %r15
movq (%rsp), %rcx
leal 0x1(,%r14,2), %eax
movl %eax, 0x4(%r15,%r12,4)
addq $0x2, %r12
jmp 0xb4a39
movq %rsi, %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0x67f2f
movq 0x10(%rsp), %rax
leaq (%rax,%rax), %rsi
xorl %edx, %edx
xorl %edi, %edi
xorl %r10d, %r10d
xorl %r8d, %r8d
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %r8, %rsi
je 0xb4b1b
movl (%r15,%r8,4), %ebp
leaq -0x1(%r10), %r9
leaq 0x1(%r10), %rbx
movl %ebp, %r11d
shrl %r11d
testb $0x1, %bpl
cmoveq %rbx, %r9
jne 0xb4b09
cmpq %rdx, %rbx
cmovgq %rbx, %rdx
cmovgel %r11d, %edi
jmp 0xb4b13
cmpq %rdx, %r10
cmovel %r11d, %eax
cmovel %edi, %ecx
incq %r8
movq %r9, %r10
jmp 0xb4adb
cmpq 0x38(%rsp), %rdx
jb 0xb4b92
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
xorl %edx, %edx
movq (%rsp), %rdi
cmpq %rdx, 0x10(%rsp)
je 0xb4b6d
movq (%rdi), %r9
movq (%r13,%rdx,8), %rsi
imulq $0x28, %rsi, %r8
cmpl (%r9,%r8), %ecx
jb 0xb4b68
addq %r8, %r9
cmpl %eax, 0x8(%r9)
jb 0xb4b68
movq %rsi, (%r13,%r12,8)
movq 0x8(%rsp), %r8
movq (%r8,%rdx,8), %rsi
movq %rsi, (%r8,%r12,8)
incq %r12
incq %rdx
jmp 0xb4b31
movq %r13, %rsi
movq %r12, %rdx
callq 0x690d3
movq 0x58(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq %rdi, %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
callq 0x69143
movq 0x30(%rsp), %rax
movq %r14, %rsi
incq %rax
jmp 0xb4858
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220