// File: /workspace/github_cpp_makefile_star2_O1/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, 0x4(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbx
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
callq 0x300fc
movq %rbx, 0x60(%rsp)
testq %rbx, %rbx
je 0xf1ac6
movq $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r9d, %r9d
xorl %r14d, %r14d
movq $0x0, 0x48(%rsp)
xorl %r11d, %r11d
xorl %eax, %eax
movl 0x4(%rsp), %r8d
movq %rax, 0x70(%rsp)
leaq (%rax,%rax,2), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x68(%rsp)
leaq (%rax,%rcx,8), %r12
cmpl $0x0, 0xc(%r12)
movq %r12, 0x20(%rsp)
je 0xf186c
xorl %r15d, %r15d
xorl %r10d, %r10d
xorl %ebp, %ebp
testl %r8d, %r8d
je 0xf1734
movq (%r12), %rax
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
cmpq $0x0, 0x18(%rax,%rcx,8)
js 0xf1859
movq %r15, %r13
movq (%r12), %rdx
leaq (,%rbp,4), %rsi
addq %rbp, %rsi
movl 0xc(%rdx,%rsi,8), %ebx
leaq (%rbx,%rbx,2), %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movl 0xc(%rax), %ecx
movq $-0x1, %r15
testq %rcx, %rcx
je 0xf178a
movl 0x4(%rdx,%rsi,8), %edx
movq (%rax), %rsi
xorl %edi, %edi
cmpl %edx, 0xc(%rsi)
jne 0xf1779
cmpl 0x4(%rsi), %ebx
je 0xf1787
incq %rdi
addq $0x28, %rsi
cmpq %rdi, %rcx
jne 0xf176f
jmp 0xf178a
movq %rdi, %r15
cmpq $-0x1, %r15
je 0xf1856
testl %r8d, %r8d
je 0xf17ac
movq (%rax), %rax
leaq (%r15,%r15,4), %rcx
cmpq $0x0, 0x18(%rax,%rcx,8)
js 0xf1856
movq 0x48(%rsp), %rax
cmpq %rax, %r10
jne 0xf17fb
movq %r9, 0x18(%rsp)
movq %r14, 0x28(%rsp)
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r11, %rdi
movq %r10, %r12
callq 0x2370
movq %r12, %r10
movq %rax, %r11
movq %r14, %rax
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %r12
movq %rax, 0x48(%rsp)
shlq $0x20, %rbx
orq %rbx, %r15
movq %r15, (%r11,%r10,8)
movq %r13, %r15
cmpq %r9, %r13
jne 0xf1845
leaq (%r9,%r9), %rbx
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq %r14, %rdi
movq %r11, %r14
movq %r10, %r13
callq 0x2370
movq %r13, %r10
movq %r14, %r11
movq %rax, %r14
movq %rbx, %r9
movl 0x4(%rsp), %r8d
incq %r10
movq %rbp, (%r14,%r15,8)
incq %r15
jmp 0xf1859
movq %r13, %r15
incq %rbp
movl 0xc(%r12), %eax
cmpq %rax, %rbp
jb 0xf1714
jmp 0xf186f
xorl %r15d, %r15d
cmpq 0x40(%rsp), %r15
jb 0xf1ab1
movq %r9, 0x18(%rsp)
movq 0x30(%rsp), %rsi
cmpq %r15, %rsi
movq %r11, 0x10(%rsp)
jae 0xf18e0
leaq -0x1(%r15), %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 0x8(%rsp), %rdi
callq 0x2370
movq %rbx, %rsi
movq %rax, %rdx
jmp 0xf18e5
movq 0x8(%rsp), %rdx
testq %r15, %r15
movq %r15, 0x38(%rsp)
je 0xf1992
movq %r15, %rbx
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movq %rbp, %r12
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%r14,%rbp,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ebp
movl 0x8(%rax,%rcx,8), %r15d
cmpq %rsi, %r12
jne 0xf1943
leaq (%rsi,%rsi), %r14
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
movq %rdx, %rdi
callq 0x2370
movq %rax, %rdx
movq %r14, %rsi
addl %ebp, %ebp
movl %ebp, (%rdx,%r12,4)
leaq 0x1(%r12), %rbp
cmpq %rsi, %rbp
jne 0xf1969
leaq (%rsi,%rsi), %r14
shlq $0x3, %rsi
movq %rdx, %rdi
callq 0x2370
movq %rax, %rdx
movq %r14, %rsi
movq 0x28(%rsp), %r14
leal 0x1(,%r15,2), %eax
movl %eax, 0x4(%rdx,%r12,4)
incq %rbp
decq %rbx
jne 0xf18fd
movq %rsi, 0x30(%rsp)
addq $0x2, %r12
jmp 0xf199a
movq %rsi, 0x30(%rsp)
xorl %r12d, %r12d
movq %r12, %rdi
movq %rdx, 0x8(%rsp)
movq %rdx, %rsi
callq 0x9e958
testq %r12, %r12
je 0xf1a15
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
xorl %edx, %edx
movq 0x10(%rsp), %r11
movq 0x38(%rsp), %rbx
movq %r8, %r10
movq 0x8(%rsp), %r8
movl (%r8,%rsi,4), %r9d
movl %r9d, %r8d
andl $0x1, %r8d
negq %r8
orq $0x1, %r8
addq %r10, %r8
testb $0x1, %r9b
jne 0xf19f9
cmpq %rdx, %r8
jl 0xf1a06
shrl %r9d
movq %r8, %rdx
movq %r9, %rdi
jmp 0xf1a06
cmpq %rdx, %r10
jne 0xf1a06
shrl %r9d
movl %r9d, %ecx
movl %edi, %eax
incq %rsi
cmpq %rsi, %r12
jne 0xf19c6
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0xf1a25
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x10(%rsp), %r11
movq 0x38(%rsp), %rbx
cmpq 0x40(%rsp), %rdx
movl 0x4(%rsp), %r8d
movq 0x20(%rsp), %rdx
jb 0xf1aac
testq %rbx, %rbx
je 0xf1a74
movq (%rdx), %rdx
xorl %esi, %esi
xorl %r12d, %r12d
movq (%r14,%rsi,8), %rdi
leaq (%rdi,%rdi,4), %r8
cmpl (%rdx,%r8,8), %eax
jb 0xf1a6a
leaq (%rdx,%r8,8), %r8
cmpl %ecx, 0x8(%r8)
jb 0xf1a6a
movq %rdi, (%r14,%r12,8)
movq (%r11,%rsi,8), %rdi
movq %rdi, (%r11,%r12,8)
incq %r12
incq %rsi
cmpq %rsi, %rbx
jne 0xf1a43
jmp 0xf1a77
xorl %r12d, %r12d
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x9fe5c
movq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rax
leaq (%rdi,%rax,8), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
callq 0x9fed3
movq 0x10(%rsp), %r11
movl 0x4(%rsp), %r8d
movq 0x18(%rsp), %r9
movq 0x70(%rsp), %rax
incq %rax
cmpq 0x60(%rsp), %rax
jne 0xf16e4
jmp 0xf1ad5
xorl %r11d, %r11d
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
movq %r11, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220