// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rcx, 0x48(%rsp)
movq %rdx, %rbx
movq %rsi, 0x60(%rsp)
movq %rdi, 0x30(%rsp)
callq 0x2f3fc
movq %rbx, 0x68(%rsp)
testq %rbx, %rbx
je 0xf3e9e
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
movq $0x0, 0x58(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x50(%rsp)
movq $0x0, 0x28(%rsp)
xorl %eax, %eax
movl %ebp, 0x14(%rsp)
movq 0x30(%rsp), %r8
movl %ebp, %r10d
movq %rax, 0x40(%rsp)
leaq (%rax,%rax,2), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x70(%rsp)
leaq (%rax,%rcx,8), %r14
movl 0xc(%r14), %eax
testl %eax, %eax
movq %r14, 0x38(%rsp)
je 0xf3be1
xorl %r9d, %r9d
movq $0x0, 0x20(%rsp)
xorl %r13d, %r13d
movq (%r14), %rdx
testl %r10d, %r10d
je 0xf3aba
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpq $0x0, 0x18(%rdx,%rcx,8)
js 0xf3bd1
leaq (,%r13,4), %rsi
addq %r13, %rsi
movl 0xc(%rdx,%rsi,8), %ebp
leaq (,%rbp,2), %rdi
addq %rbp, %rdi
movl 0xc(%r8,%rdi,8), %ecx
testq %rcx, %rcx
je 0xf3bd1
movl 0x4(%rdx,%rsi,8), %esi
leaq (%r8,%rdi,8), %rdx
movq (%rdx), %rdx
xorl %r12d, %r12d
cmpl %esi, 0xc(%rdx)
jne 0xf3afa
cmpl 0x4(%rdx), %ebp
je 0xf3b0b
incq %r12
addq $0x28, %rdx
cmpq %r12, %rcx
jne 0xf3af0
jmp 0xf3bd1
testl %r10d, %r10d
je 0xf3b1b
cmpq $0x0, 0x18(%rdx)
js 0xf3bd1
movq 0x20(%rsp), %r15
cmpq 0x50(%rsp), %r15
jne 0xf3b65
leaq (%r15,%r15), %rcx
testq %r15, %r15
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x50(%rsp)
leaq (,%rcx,8), %rsi
movq 0x28(%rsp), %rdi
movq %r11, 0x18(%rsp)
movq %r9, %r14
callq 0x2370
movq %r14, %r9
movq 0x18(%rsp), %r11
movq 0x38(%rsp), %r14
jmp 0xf3b6a
movq 0x28(%rsp), %rax
shlq $0x20, %rbp
orq %rbp, %r12
movq %rax, 0x28(%rsp)
movq %r12, (%rax,%r15,8)
cmpq 0x58(%rsp), %r9
jne 0xf3bb4
leaq (%r9,%r9), %rcx
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x58(%rsp)
leaq (,%rcx,8), %rsi
movq %r11, %rdi
movq %r9, %r12
callq 0x2370
movq %r12, %r9
movq %rax, %r11
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %r8
movl 0x14(%rsp), %r10d
incq %r15
movq %r15, 0x20(%rsp)
movq %r13, (%r11,%r9,8)
incq %r9
movl 0xc(%r14), %eax
incq %r13
movl %eax, %ecx
cmpq %rcx, %r13
jb 0xf3a9b
jmp 0xf3be4
xorl %r9d, %r9d
cmpq 0x48(%rsp), %r9
jb 0xf3df4
cmpq %r9, %rbx
movq %r11, 0x18(%rsp)
movq %r9, 0x20(%rsp)
jae 0xf3c59
leaq -0x1(%r9), %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 0x20(%rsp), %r9
movq 0x18(%rsp), %r11
movq %rax, 0x8(%rsp)
jmp 0xf3c62
testq %r9, %r9
je 0xf3dc9
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r12, %r13
movq (%r14), %rax
movq (%r11,%rbp,8), %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %r14d
movl 0x8(%rax,%rcx,8), %r12d
cmpq %rbx, %r13
jne 0xf3cc8
leaq (,%r13,2), %rbx
testq %r13, %r13
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r11
addl %r14d, %r14d
movq %r13, %rdx
orq $0x1, %rdx
movq %rax, 0x8(%rsp)
movl %r14d, (%rax,%r13,4)
jmp 0xf3d0b
addl %r14d, %r14d
movq %r13, %rdx
orq $0x1, %rdx
movq 0x8(%rsp), %rax
movl %r14d, (%rax,%r13,4)
cmpq %rbx, %rdx
jne 0xf3d0b
leaq (%rbx,%rbx), %r14
leaq (,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r11
movq %rax, 0x8(%rsp)
movq %rbx, %rdx
movq %r14, %rbx
movq 0x38(%rsp), %r14
leal 0x1(,%r12,2), %ecx
leaq 0x2(%r13), %r12
movq 0x8(%rsp), %rax
movl %ecx, (%rax,%rdx,4)
incq %rbp
cmpq %r9, %rbp
jne 0xf3c67
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x9f085
testq %r12, %r12
je 0xf3db4
xorl %eax, %eax
movq $-0x2, %rdx
xorl %ecx, %ecx
xorl %edi, %edi
xorl %esi, %esi
xorl %r12d, %r12d
movl 0x14(%rsp), %r10d
movq 0x18(%rsp), %r11
movl %r10d, %ebp
movq %rdi, %r9
movq 0x8(%rsp), %rdi
movl 0x8(%rdi,%rdx,4), %r8d
leaq -0x1(%r9), %rdi
leaq 0x1(%r9), %r10
testb $0x1, %r8b
cmoveq %r10, %rdi
jne 0xf3d90
shrl %r8d
cmpq %r12, %r10
cmovgq %r10, %r12
cmovgel %r8d, %esi
jmp 0xf3da0
cmpq %r12, %r9
jne 0xf3da0
shrl %r8d
movq %r9, %r12
movl %r8d, %ecx
movl %esi, %eax
movl %ebp, %r10d
incq %rdx
cmpq %rdx, %r13
jne 0xf3d5e
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
jmp 0xf3de8
xorl %edx, %edx
xorl %r12d, %r12d
xorl %ecx, %ecx
xorl %eax, %eax
movl 0x14(%rsp), %r10d
movq 0x18(%rsp), %r11
jmp 0xf3de8
xorl %r12d, %r12d
xorl %edi, %edi
movq 0x8(%rsp), %rsi
callq 0x9f085
movl 0x14(%rsp), %r10d
movq 0x18(%rsp), %r11
movb $0x1, %dl
xorl %ecx, %ecx
xorl %eax, %eax
cmpq 0x48(%rsp), %r12
movq 0x30(%rsp), %r8
jae 0xf3dfe
movq 0x40(%rsp), %rax
jmp 0xf3e8e
movl %r10d, %ebp
testb %dl, %dl
je 0xf3e0a
xorl %r12d, %r12d
jmp 0xf3e4b
movq (%r14), %rdx
xorl %esi, %esi
xorl %r12d, %r12d
movq 0x20(%rsp), %r9
movq (%r11,%rsi,8), %rdi
leaq (%rdi,%rdi,4), %r8
cmpl (%rdx,%r8,8), %eax
jb 0xf3e43
leaq (%rdx,%r8,8), %r8
cmpl %ecx, 0x8(%r8)
jb 0xf3e43
movq %rdi, (%r11,%r12,8)
movq 0x28(%rsp), %r8
movq (%r8,%rsi,8), %rdi
movq %rdi, (%r8,%r12,8)
incq %r12
incq %rsi
cmpq %rsi, %r9
jne 0xf3e17
movq %r14, %rdi
movq %r11, %rsi
movq %r12, %rdx
callq 0xa0611
movq 0x30(%rsp), %r13
movq 0x70(%rsp), %rax
leaq (,%rax,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
callq 0xa068a
movq 0x18(%rsp), %r11
movq %r13, %r8
movq 0x40(%rsp), %rax
movl %ebp, %r10d
incq %rax
cmpq 0x68(%rsp), %rax
jne 0xf3a64
jmp 0xf3eb3
movq $0x0, 0x28(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movq %r11, %rbx
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220