// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movl %r8d, %r14d
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x2c(%rsp)
movaps %xmm0, 0x20(%rsp)
movq $0x0, (%r9)
leal (%r14,%r14), %eax
cmpq %rax, 0x8(%r9)
movq %r9, 0x18(%rsp)
jae 0x7624b
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(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %r13
movq %rax, 0x10(%rbp)
jmp 0x7624f
movq 0x10(%rbp), %r13
movq %r12, 0x8(%rsp)
movq %r15, (%rsp)
movl %r14d, %eax
leaq (,%rax,8), %rbp
addq %r13, %rbp
testl %r14d, %r14d
movq %rax, 0x40(%rsp)
je 0x762d1
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq 0x88(%rsp), %rdx
movq 0x90(%rsp), %rcx
callq 0x74ca1
testq %rax, %rax
je 0x762c6
movl 0x24(%rsp), %eax
movl 0x28(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%r13,%r12,8)
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
movq %rcx, (%rbp,%r12,8)
incq %r12
addq $0x1c, %rbx
decq %r14
jne 0x7627b
jmp 0x762d4
xorl %r12d, %r12d
leaq (,%r12,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x45373
testq %r12, %r12
je 0x76362
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x1c(%rcx), %ecx
movq 0x10(%rsp), %r14
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x7633b
movq -0x8(%r13,%rbx,8), %rdi
movq (%r13,%rdx,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
cmpl %r8d, %edi
jb 0x76340
cmpl %esi, %edi
jae 0x76348
subl %edi, %esi
addq %rdi, %rsi
movq %rsi, -0x8(%r13,%rbx,8)
jmp 0x76348
movq (%r13,%rdx,8), %rsi
movq %rsi, (%r13,%rbx,8)
incq %rbx
incq %rdx
cmpq %rdx, %r12
jne 0x76310
testl %ecx, %ecx
je 0x76401
cmpq $0x2, %rbx
jae 0x7636e
xorl %ecx, %ecx
jmp 0x763e0
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
jmp 0x76401
movq %rbx, %rcx
shrq %rcx
movq %rcx, %rdx
negq %rdx
leaq -0x8(,%rbx,8), %rsi
addq %r13, %rsi
xorl %edi, %edi
movq %r13, %r8
movq (%r8), %r9
movq (%rsi,%rdi,8), %r10
movq %r10, (%r8)
movq %r9, (%rsi,%rdi,8)
movq (%r8), %r9
movl %eax, %r10d
subl %r9d, %r10d
shrq $0x20, %r9
movq %rax, %r11
subq %r9, %r11
shlq $0x20, %r10
orq %r11, %r10
movq %r10, (%r8)
movq (%rsi,%rdi,8), %r9
movl %eax, %r10d
subl %r9d, %r10d
shrq $0x20, %r9
movq %rax, %r11
subq %r9, %r11
shlq $0x20, %r10
orq %r11, %r10
movq %r10, (%rsi,%rdi,8)
decq %rdi
addq $0x8, %r8
cmpq %rdi, %rdx
jne 0x7638a
testb $0x1, %bl
je 0x76401
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)
movq 0x80(%rsp), %r15
leaq (,%r12,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x45373
testq %r12, %r12
je 0x7649a
movq (%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x1c(%rcx), %ecx
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
xorl %r14d, %r14d
testq %r14, %r14
je 0x7646b
movq -0x8(%rbp,%r14,8), %rdi
movq (%rbp,%rdx,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
cmpl %r8d, %edi
jb 0x76470
cmpl %esi, %edi
jae 0x76478
subl %edi, %esi
addq %rdi, %rsi
movq %rsi, -0x8(%rbp,%r14,8)
jmp 0x76478
movq (%rbp,%rdx,8), %rsi
movq %rsi, (%rbp,%r14,8)
incq %r14
incq %rdx
cmpq %rdx, %r12
jne 0x76440
testl %ecx, %ecx
movq 0x8(%rsp), %r12
je 0x76552
cmpq $0x2, %r14
jae 0x764b1
xorl %ecx, %ecx
jmp 0x76530
xorl %eax, %eax
movq %rax, (%r15)
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %r12
jmp 0x7659f
movq %r14, %rcx
shrq %rcx
movq %rcx, %rdx
negq %rdx
movq 0x40(%rsp), %rsi
leaq (,%rsi,8), %rsi
addq %r13, %rsi
leaq (%rsi,%r14,8), %rsi
addq $-0x8, %rsi
xorl %edi, %edi
movq %rbp, %r8
movq (%r8), %r9
movq (%rsi,%rdi,8), %r10
movq %r10, (%r8)
movq %r9, (%rsi,%rdi,8)
movq (%r8), %r9
movl %eax, %r10d
subl %r9d, %r10d
shrq $0x20, %r9
movq %rax, %r11
subq %r9, %r11
shlq $0x20, %r10
orq %r11, %r10
movq %r10, (%r8)
movq (%rsi,%rdi,8), %r9
movl %eax, %r10d
subl %r9d, %r10d
shrq $0x20, %r9
movq %rax, %r11
subq %r9, %r11
shlq $0x20, %r10
orq %r11, %r10
movq %r10, (%rsi,%rdi,8)
decq %rdi
addq $0x8, %r8
cmpq %rdi, %rdx
jne 0x764da
testb $0x1, %r14b
je 0x76552
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)
xorl %eax, %eax
movq %rax, (%r15)
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
testq %rbx, %rbx
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x7659f
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x753b5
movq %rax, %rbx
movq (%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x753b5
testq %rbx, %rbx
je 0x765ee
testq %rax, %rax
je 0x765ee
cmpq $0x0, (%r15)
je 0x765f5
movq %r15, %rdi
movq %r12, %rsi
movq (%rsp), %r14
movq %r14, %rdx
movq 0x90(%rsp), %rcx
callq 0x759dd
movq (%r15), %rdx
testq %rdx, %rdx
je 0x765ea
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x75721
movq %rax, %rbx
movq (%r15), %rdx
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x75721
subq %rax, %rbx
jmp 0x765fc
xorl %ebx, %ebx
jmp 0x765fc
movq $0x0, (%r15)
movq $-0x80000000, %rbx      # imm = 0x80000000
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq