// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movl %r8d, %r15d
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, (%rsp)
movq %rdi, 0x38(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, (%r9)
leal (%r15,%r15), %eax
cmpq %rax, 0x8(%r9)
jae 0x76855
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(%r12)
movq 0x10(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r12)
movq %r14, 0x8(%rsp)
movq %r12, 0x40(%rsp)
movq 0x10(%r12), %rbp
movl %r15d, %eax
leaq (,%rax,8), %r13
addq %rbp, %r13
testl %r15d, %r15d
movq %rax, 0x30(%rsp)
je 0x768dd
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x75243
testq %rax, %rax
je 0x768d2
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%rbp,%r12,8)
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%r13,%r12,8)
incq %r12
addq $0x1c, %rbx
decq %r15
jne 0x76887
jmp 0x768e0
xorl %r12d, %r12d
leaq (,%r12,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x45d47
movq (%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x1c(%rcx), %ecx
movq 0x38(%rsp), %r14
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
testq %r12, %r12
je 0x76958
xorl %edx, %edx
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x76931
movq -0x8(%rbp,%rbx,8), %rsi
movq (%rbp,%rdx,8), %rdi
movq %rdi, %r8
shrq $0x20, %r8
cmpl %r8d, %esi
jae 0x76948
movq (%rbp,%rdx,8), %rsi
movq %rsi, (%rbp,%rbx,8)
incq %rbx
incq %rdx
cmpq %rdx, %r12
jne 0x76916
jmp 0x7695a
cmpl %edi, %esi
jae 0x7693e
subl %esi, %edi
addq %rsi, %rdi
movq %rdi, -0x8(%rbp,%rbx,8)
jmp 0x7693e
xorl %ebx, %ebx
testl %ecx, %ecx
je 0x76a04
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq $0x2, %rbx
jae 0x76971
xorl %ecx, %ecx
jmp 0x769e3
movq %rbx, %rdx
shrq %rdx
negq %rdx
leaq -0x8(,%rbx,8), %rsi
addq %rbp, %rsi
xorl %ecx, %ecx
movq %rbp, %rdi
movq (%rdi), %r8
movq (%rsi,%rcx,8), %r9
movq %r9, (%rdi)
movq %r8, (%rsi,%rcx,8)
movq (%rdi), %r8
movl %eax, %r9d
subl %r8d, %r9d
shrq $0x20, %r8
movq %rax, %r10
subq %r8, %r10
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%rdi)
movq (%rsi,%rcx,8), %r8
movl %eax, %r9d
subl %r8d, %r9d
shrq $0x20, %r8
movq %rax, %r10
subq %r8, %r10
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%rsi,%rcx,8)
decq %rcx
addq $0x8, %rdi
cmpq %rcx, %rdx
jne 0x7698a
negq %rcx
testb $0x1, %bl
je 0x76a04
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,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x45d47
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x1c(%rcx), %ecx
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
testq %r12, %r12
je 0x76a79
xorl %edx, %edx
xorl %r14d, %r14d
testq %r14, %r14
je 0x76a52
movq -0x8(%r13,%r14,8), %rsi
movq (%r13,%rdx,8), %rdi
movq %rdi, %r8
shrq $0x20, %r8
cmpl %r8d, %esi
jae 0x76a69
movq (%r13,%rdx,8), %rsi
movq %rsi, (%r13,%r14,8)
incq %r14
incq %rdx
cmpq %rdx, %r12
jne 0x76a37
jmp 0x76a7c
cmpl %edi, %esi
jae 0x76a5f
subl %esi, %edi
addq %rsi, %rdi
movq %rdi, -0x8(%r13,%r14,8)
jmp 0x76a5f
xorl %r14d, %r14d
movq 0x80(%rsp), %r15
testl %ecx, %ecx
movq 0x8(%rsp), %r12
je 0x76b3e
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq $0x2, %r14
jae 0x76aa0
xorl %ecx, %ecx
jmp 0x76b1c
movq %r14, %rdx
shrq %rdx
negq %rdx
movq 0x30(%rsp), %rcx
shlq $0x3, %rcx
leaq (%rcx,%r14,8), %rcx
leaq (%rcx,%rbp), %rsi
addq $-0x8, %rsi
xorl %ecx, %ecx
movq %r13, %rdi
movq (%rdi), %r8
movq (%rsi,%rcx,8), %r9
movq %r9, (%rdi)
movq %r8, (%rsi,%rcx,8)
movq (%rdi), %r8
movl %eax, %r9d
subl %r8d, %r9d
shrq $0x20, %r8
movq %rax, %r10
subq %r8, %r10
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%rdi)
movq (%rsi,%rcx,8), %r8
movl %eax, %r9d
subl %r8d, %r9d
shrq $0x20, %r8
movq %rax, %r10
subq %r8, %r10
shlq $0x20, %r9
orq %r10, %r9
movq %r9, (%rsi,%rcx,8)
decq %rcx
addq $0x8, %rdi
cmpq %rcx, %rdx
jne 0x76ac3
negq %rcx
testb $0x1, %r14b
je 0x76b3e
movq (%r13,%rcx,8), %rdx
movl %eax, %esi
subl %edx, %esi
shrq $0x20, %rdx
subq %rdx, %rax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rsi, (%r13,%rcx,8)
xorl %eax, %eax
movq %rax, (%r15)
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
testq %rbx, %rbx
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x76b92
movq (%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x75997
movq %rax, %rbx
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x75997
testq %rbx, %rbx
je 0x76b8b
testq %rax, %rax
jne 0x76b92
movq $0x0, (%r15)
cmpq $0x0, (%r15)
je 0x76bdd
movq %r15, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
movq 0x90(%rsp), %rcx
callq 0x76009
movq (%r15), %rdx
testq %rdx, %rdx
je 0x76be6
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x75d3a
movq %rax, %rbx
movq (%r15), %rdx
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x75d3a
subq %rax, %rbx
jmp 0x76be8
movq $-0x80000000, %rbx      # imm = 0x80000000
jmp 0x76be8
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq