// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_paired_distance(ma_ug_t*, overlap_region*, overlap_region*, ul_ov_t*, unsigned int, asg64_v*, asg64_v*, idx_emask_t*, long)
int64_t cal_paired_distance(ma_ug_t *ug, overlap_region *q, overlap_region *t, ul_ov_t *a, uint32_t a_n, 
asg64_v *srt, asg64_v* buf1, idx_emask_t *mm, int64_t bd)
{
    uint64_t k, cn, *qa, *ta, *ca, m, qn, tn, rev, rev_n, tl, mt, qocc, tocc, s, e; 
    ul_ov_t ou; memset((&ou), 0, sizeof(ou)); int64_t eq, et;
    srt->n = 0; kv_resize(uint64_t, *srt, (a_n<<1)); qa = srt->a; ta = qa + a_n;

    // if(q->y_id == 4 && t->y_id == 6) {
    //     fprintf(stderr, "\n\n\n[M::%s]\tutg%.6u%c\tutg%.6u%c\n", __func__, q->y_id+1, "lc"[ug->u.a[q->y_id].circ], 
    //     t->y_id+1, "lc"[ug->u.a[t->y_id].circ]);
    // }

    for (k = cn = 0; k < a_n; k++) {
        // if(q->y_id == 4 && t->y_id == 6) {
        //     fprintf(stderr, "[M::%s]\tk::%lu\n", __func__, k); 
        //     fprintf(stderr, "au\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\tfull::%u\tis_cal::%u\n", 
        //     a[k].qn + 1, "lc"[ug->u.a[a[k].qn].circ], ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
        //     a[k].tn + 1, "lc"[ug->u.a[a[k].tn].circ], ug->u.a[a[k].tn].len, a[k].ts, a[k].te,
        //     a[k].el, (a[k].sec!=((uint32_t)(0x3fffffff)))?1:0);
        // }
        if(!cal_no_bd_coor(&(a[k]), &ou, mm, bd)) continue;
        qa[cn] = (((uint64_t)ou.qs)<<32)|((uint64_t)ou.qe);
        ta[cn] = (((uint64_t)ou.ts)<<32)|((uint64_t)ou.te);
        cn++;
        // if(q->y_id == 4 && t->y_id == 6) {
        //     fprintf(stderr, "ou\tutg%.6u%c\txl::%u\tx::[%u,\t%u)\t%c\tutg%.6u%c\tyl::%u\ty::[%u,\t%u)\n", 
        //     ou.qn + 1, "lc"[ug->u.a[ou.qn].circ], ug->u.a[ou.qn].len, ou.qs, ou.qe, "+-"[ou.rev],
        //     ou.tn + 1, "lc"[ug->u.a[ou.tn].circ], ug->u.a[ou.tn].len, ou.ts, ou.te);
        // }
    }
    radix_sort_bc64(qa, qa+cn); ca = qa; rev = q->y_pos_strand; tl = ug->g->seq[q->y_id].len;
    for (k = m = 0; k < cn; k++) {
        if(m <= 0 || (((uint32_t)ca[m-1])) < (ca[k]>>32)) {
            ca[m++] = ca[k];
        } else if(((uint32_t)ca[m-1]) < ((uint32_t)ca[k])) {
            ca[m-1] += ((uint32_t)ca[k])-((uint32_t)ca[m-1]);
        }
    }
    if(rev) {
        rev_n = m>>1; ///tl = (tl<<32) + tl;
        for (k = 0; k < rev_n; k++) {
            mt = ca[k]; ca[k] = ca[m-k-1]; ca[m-k-1] = mt;

            s = tl-((uint32_t)ca[k]); e = tl-(ca[k]>>32);
            ca[k] = (s<<32)|e;
            s = tl-((uint32_t)ca[m-k-1]); e = tl-(ca[m-k-1]>>32);
            ca[m-k-1] = (s<<32)|e;
        }
        if(m&1) {
            s = tl-((uint32_t)ca[k]); e = tl-(ca[k]>>32);
            ca[k] = (s<<32)|e;
        }
    }
    qn = m;
    // if(q->y_id == 4 && t->y_id == 6) {
    // fprintf(stderr, "[M::%s]\tqn::%lu\trev::%lu\n", __func__, qn, rev); 
    // for (k = 0; k < qn; k++) {
    //     s = qa[k]>>32; e = ((uint32_t)qa[k]);
    //     fprintf(stderr, "[M::%s]\tqstr::[%lu,\t%lu)\n", __func__, s, e); 
    // }
    // }

    radix_sort_bc64(ta, ta+cn); ca = ta; rev = t->y_pos_strand; tl = ug->g->seq[t->y_id].len;
    for (k = m = 0; k < cn; k++) {
        if(m <= 0 || (((uint32_t)ca[m-1])) < (ca[k]>>32)) {
            ca[m++] = ca[k];
        } else if(((uint32_t)ca[m-1]) < ((uint32_t)ca[k])) {
            ca[m-1] += ((uint32_t)ca[k])-((uint32_t)ca[m-1]);
        }
    }
    if(rev) {
        rev_n = m>>1; ///tl = (tl<<32) + tl;
        for (k = 0; k < rev_n; k++) {
            mt = ca[k]; ca[k] = ca[m-k-1]; ca[m-k-1] = mt;

            s = tl-((uint32_t)ca[k]); e = tl-(ca[k]>>32);
            ca[k] = (s<<32)|e;
            s = tl-((uint32_t)ca[m-k-1]); e = tl-(ca[m-k-1]>>32);
            ca[m-k-1] = (s<<32)|e;
        }
        if(m&1) {
            s = tl-((uint32_t)ca[k]); e = tl-(ca[k]>>32);
            ca[k] = (s<<32)|e;
        }
    }
    tn = m;
    // if(q->y_id == 4 && t->y_id == 6) {
    // fprintf(stderr, "[M::%s]\ttn::%lu\trev::%lu\n", __func__, tn, rev); 
    // for (k = 0; k < tn; k++) {
    //     s = ta[k]>>32; e = ((uint32_t)ta[k]);
    //     fprintf(stderr, "[M::%s]\ttstr::[%lu,\t%lu)\n", __func__, s, e); 
    // }
    // }

    buf1->n = 0; qocc = tocc = 0; srt->n = 0;
    if(qn > 0 && tn > 0) {
        qocc = extract_xcoordates(q, qa, qn, buf1);
        tocc = extract_xcoordates(t, ta, tn, buf1);
        // if(q->y_id == 4 && t->y_id == 6) {
        //     fprintf(stderr, "[M::%s]\tqocc::%lu\ttocc::%lu\n", __func__, qocc, tocc);
        // }
        if((!qocc) || (!tocc)) qocc = tocc = buf1->n = 0;
    }
    if(!(buf1->n)) return INT32_MIN;
    update_masks(buf1, q, t, bd);   

    eq = et = 0;
    if(buf1->n) {
        eq = cal_xerr(q, buf1->a, buf1->n);
        et = cal_xerr(t, buf1->a, buf1->n);
    }   
    // if(q->y_id == 4 && t->y_id == 6) {
    //     s = MAX(q->x_pos_s, t->x_pos_s);
    //     e = MIN(q->x_pos_e, t->x_pos_e) + 1;
    //     buf1->n = 0;
    //     kv_push(uint64_t, *buf1, ((s<<32)|e));
    //     fprintf(stderr, "[M::%s]\teq::%ld\tet::%ld\tbuf1->n::%u\n", __func__, eq, et, (uint32_t)buf1->n);
    //     eq = cal_xerr(q, buf1->a, buf1->n); et = cal_xerr(t, buf1->a, buf1->n);
    //     fprintf(stderr, "[M::%s]\teq1::%ld\tet1::%ld\tbuf1->n::%u\ts::%lu\te::%lu\n", 
    //     __func__, eq, et, (uint32_t)buf1->n, s, e);
    // }

    return eq - et;    
    // return get_pe_diff(q, qa, qn, t, ta, tn, bd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r14
movl %r8d, %r12d
movq %rcx, %rbx
movq %rsi, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, (%r9)
leal (%r12,%r12), %eax
cmpq %rax, 0x8(%r9)
movq %rdx, (%rsp)
movq %r9, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
jae 0x43fe2
decq %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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %rbp
movq %rax, 0x10(%r14)
jmp 0x43fe6
movq 0x10(%r14), %rbp
movl %r12d, %r14d
leaq (,%r14,8), %r12
addq %rbp, %r12
xorl %r13d, %r13d
leaq 0x10(%rsp), %r15
subq $0x1, %r14
jb 0x44052
movq %rbx, %rdi
movq %r15, %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x42aae
testq %rax, %rax
je 0x4404c
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%rbp,%r13,8)
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%r12,%r13,8)
incq %r13
addq $0x1c, %rbx
jmp 0x43ffc
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x1905d
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x1c(%rcx), %ecx
movq 0x38(%rsp), %r15
movq 0x18(%r15), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
xorl %edx, %edx
movq (%rsp), %r14
cmpq %rdx, %r13
je 0x440cf
testq %rbx, %rbx
je 0x440bd
movq -0x8(%rbp,%rbx,8), %rdi
movq (%rbp,%rdx,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
cmpl %r8d, %edi
jb 0x440c2
cmpl %esi, %edi
jae 0x440ca
subl %edi, %esi
addq %rdi, %rsi
movq %rsi, -0x8(%rbp,%rbx,8)
jmp 0x440ca
movq (%rbp,%rdx,8), %rsi
movq %rsi, (%rbp,%rbx,8)
incq %rbx
incq %rdx
jmp 0x4408d
testl %ecx, %ecx
je 0x44167
movq %rbx, %rcx
shrq %rcx
leaq -0x8(,%rbx,8), %rdx
addq %rbp, %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x44146
movq (%rbp,%rsi,8), %rdi
movq (%rdx), %r8
movq %r8, (%rbp,%rsi,8)
movq %rdi, (%rdx)
movq (%rbp,%rsi,8), %rdi
movl %eax, %r8d
subl %edi, %r8d
shrq $0x20, %rdi
movq %rax, %r9
subq %rdi, %r9
shlq $0x20, %r8
orq %r9, %r8
movq %r8, (%rbp,%rsi,8)
movq (%rdx), %rdi
movl %eax, %r8d
subl %edi, %r8d
shrq $0x20, %rdi
movq %rax, %r9
subq %rdi, %r9
shlq $0x20, %r8
orq %r9, %r8
movq %r8, (%rdx)
incq %rsi
addq $-0x8, %rdx
jmp 0x440ea
testb $0x1, %bl
je 0x44167
movq (%rbp,%rcx,8), %rdx
movl %eax, %esi
subl %edx, %esi
shrq $0x20, %rdx
subq %rdx, %rax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rsi, (%rbp,%rcx,8)
leaq (%r12,%r13,8), %rsi
movq %r12, %rdi
callq 0x1905d
movl 0x10(%r14), %eax
movl 0x1c(%r14), %ecx
movq 0x18(%r15), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
xorl %edx, %edx
cmpq %rdx, %r13
je 0x441cf
testq %r14, %r14
je 0x441bf
movq -0x8(%r12,%r14,8), %rdi
movq (%r12,%rdx,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
cmpl %r8d, %edi
jb 0x441c3
cmpl %esi, %edi
jae 0x441ca
subl %edi, %esi
addq %rdi, %rsi
movq %rsi, -0x8(%r12,%r14,8)
jmp 0x441ca
movq (%r12,%rdx,8), %rsi
movq %rsi, (%r12,%r14,8)
incq %r14
incq %rdx
jmp 0x44190
testl %ecx, %ecx
movq 0x8(%rsp), %r13
je 0x44264
movq %r14, %rcx
shrq %rcx
leaq (%r12,%r14,8), %rdx
addq $-0x8, %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x44244
movq (%r12,%rsi,8), %rdi
movq (%rdx), %r8
movq %r8, (%r12,%rsi,8)
movq %rdi, (%rdx)
movq (%r12,%rsi,8), %rdi
movl %eax, %r8d
subl %edi, %r8d
shrq $0x20, %rdi
movq %rax, %r9
subq %rdi, %r9
shlq $0x20, %r8
orq %r9, %r8
movq %r8, (%r12,%rsi,8)
movq (%rdx), %rdi
movl %eax, %r8d
subl %edi, %r8d
shrq $0x20, %rdi
movq %rax, %r9
subq %rdi, %r9
shlq $0x20, %r8
orq %r9, %r8
movq %r8, (%rdx)
incq %rsi
addq $-0x8, %rdx
jmp 0x441ec
testb $0x1, %r14b
je 0x44264
movq (%r12,%rcx,8), %rdx
movl %eax, %esi
subl %edx, %esi
shrq $0x20, %rdx
subq %rdx, %rax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rsi, (%r12,%rcx,8)
movq 0x80(%rsp), %r15
andq $0x0, (%r15)
movq 0x40(%rsp), %rax
andq $0x0, (%rax)
testq %rbx, %rbx
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x442bc
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x4319d
movq %rax, %rbx
movq (%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x4319d
testq %rbx, %rbx
je 0x44307
testq %rax, %rax
je 0x44307
cmpq $0x0, (%r15)
je 0x4430b
movq %r15, %rdi
movq %r13, %rsi
movq (%rsp), %r14
movq %r14, %rdx
movq 0x90(%rsp), %rcx
callq 0x437b6
movq (%r15), %rdx
testq %rdx, %rdx
je 0x44314
movq 0x10(%r15), %rsi
movq %r13, %rdi
callq 0x4350b
movq %rax, %rbx
movq (%r15), %rdx
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x4350b
subq %rax, %rbx
jmp 0x44316
andq $0x0, (%r15)
movq $-0x80000000, %rbx      # imm = 0x80000000
jmp 0x44316
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq