// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: worker_for_trans_clean_re(void*, long, int)
static void worker_for_trans_clean_re(void *data, long i, int tid) // callback for kt_for()
{
    u_trans_clean_t *s = (u_trans_clean_t*)data;
    kv_u_trans_t *ta = s->ta;
    kv_u_trans_t *res = &(s->res[tid]);
    u_trans_t *a = NULL, *r_a = NULL, *mz, *cz, *sz;
    uint32_t n, r_n, id = i, k, l, z;
    uint32_t ovq, ovt, os, oe;

    a = u_trans_a(*ta, id); n = u_trans_n(*ta, id);
    for (k = 1, l = 0; k <= n; k++) {
        if(k == n || a[l].tn != a[k].tn) {
            if(k > l) {
                get_u_trans_spec(ta, a[l].tn, a[l].qn, &r_a, &r_n);
                if(r_n > 0 && id > a[l].tn) {
                    l = k; continue;
                }

                mz = cz = sz = NULL; 
                for (z = l; z < k; z++) {
                    cz = &(a[z]);
                    if(cz->f == RC_3) {
                        if((!sz) || (sz->nw < cz->nw)) sz = cz;
                    }
                    if(mz) {
                        if((mz->f == RC_0) && (cz->f != RC_0)) continue;
                        if((mz->f == RC_1) && ((cz->f == RC_2) || (cz->f == RC_3))) continue;
                    }
                    if((!mz) || ((cz->f < mz->f) && ((cz->f == RC_0) || (cz->f == RC_1))) || 
                        (mz->nw < cz->nw) || ((mz->nw == cz->nw) && ((mz->qe - mz->qs) < (cz->qe - cz->qs)))) {
                        mz = cz; 
                    }
                }
                for (z = 0; z < r_n; z++) {
                    cz = &(r_a[z]);
                    if(cz->f == RC_3) {
                        if((!sz) || (sz->nw < cz->nw)) sz = cz;
                    }
                    if(mz) {
                        if((mz->f == RC_0) && (cz->f != RC_0)) continue;
                        if((mz->f == RC_1) && ((cz->f == RC_2) || (cz->f == RC_3))) continue;
                    }
                    if((!mz) || ((cz->f < mz->f) && ((cz->f == RC_0) || (cz->f == RC_1))) || 
                        (mz->nw < cz->nw) || ((mz->nw == cz->nw) && ((mz->qe - mz->qs) < (cz->te - cz->ts)))) {
                        mz = cz;
                    }
                }


                
                // fprintf(stderr, "-[M::%s::] is_mz::%u, qn::%u, tn::%u\n", __func__, (uint32_t)(!!mz), mz->qn, mz->tn);
                if((mz) && (mz->qn != mz->tn)) {
                    ///need to check it cover enough reads
                    kv_pushp(u_trans_t, *res, &cz); (*cz) = (*mz);
                    if((sz) && (sz->rev == cz->rev)) {
                        if((cz->qn != sz->qn) || (cz->tn != sz->tn)) {
                            cz->qn = mz->tn; cz->qs = mz->ts; cz->qe = mz->te;
                            cz->tn = mz->qn; cz->ts = mz->qs; cz->te = mz->qe;
                        }
                        if((cz->qn == sz->qn) && (cz->tn == sz->tn)) {
                            os = MAX(cz->qs, sz->qs); oe = MIN(cz->qe, sz->qe);
                            ovq = ((oe > os)? (oe - os):0);

                            os = MAX(cz->ts, sz->ts); oe = MIN(cz->te, sz->te);
                            ovt = ((oe > os)? (oe - os):0);

                            if(((ovq) && (ovq > ((cz->qe-cz->qs)*0.8)) && (ovq > ((sz->qe-sz->qs)*0.8))) && 
                                (((ovt) && (ovt > ((cz->te-cz->ts)*0.8)) && (ovt > ((sz->te-sz->ts)*0.8))))) {///sounds like base-level alignment coordinates are more reliable
                                (*cz) = (*sz); cz->f = mz->f; 
                            }
                        }
                    }
                    if(cz->qn != id) {
                        z = cz->qn; cz->qn = cz->tn; cz->tn = z;
                        z = cz->qs; cz->qs = cz->ts; cz->ts = z;
                        z = cz->qe; cz->qe = cz->te; cz->te = z;
                    } 

                    // if(id == 394 || id == 1698 || id == 84)  {
                    //     fprintf(stderr, "+[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
                    //         cz->qn+1, "lc"[s->ug->u.a[cz->qn].circ], s->ug->u.a[cz->qn].len, cz->qs, cz->qe, "+-"[cz->rev],
                    //         cz->tn+1, "lc"[s->ug->u.a[cz->tn].circ], s->ug->u.a[cz->tn].len, cz->ts, cz->te);
                    // }

                    if((cz->nw >= 0) && ((!trans_ovlp_connect1(cz, s->ug)) 
                                    || (!ovlp_rocc(cz, s->ug, s->rg, s->small_ov_rate, s->ov_cutoff)))) { 
                        // if(id == 394 || id == 1698 || id == 84)  {
                        //     fprintf(stderr, "-[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
                        //         cz->qn+1, "lc"[s->ug->u.a[cz->qn].circ], s->ug->u.a[cz->qn].len, cz->qs, cz->qe, "+-"[cz->rev],
                        //         cz->tn+1, "lc"[s->ug->u.a[cz->tn].circ], s->ug->u.a[cz->tn].len, cz->ts, cz->te);
                        // } 
                        res->n--;
                    }
                }
            }
            l = k;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x18(%rdi), %rcx
movslq %edx, %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq %rdi, 0x28(%rsp)
addq 0x10(%rdi), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq 0x28(%rcx), %rax
movq %rsi, 0x38(%rsp)
movl %esi, %ecx
movq (%rax,%rcx,8), %r8
testl %r8d, %r8d
je 0xc614a
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r9
shlq $0x4, %r9
movq 0x40(%rsp), %rax
addq 0x10(%rax), %r9
movl $0x1, %r15d
xorl %eax, %eax
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movl %r15d, %ebx
cmpl %r8d, %r15d
je 0xc5d13
movl %eax, %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movl 0x14(%r9,%rcx), %ecx
leaq (%rbx,%rbx,2), %rdx
shlq $0x4, %rdx
cmpl 0x14(%r9,%rdx), %ecx
jne 0xc5d13
movl %eax, %ecx
jmp 0xc613c
movl %r15d, %ecx
cmpl %eax, %r15d
jbe 0xc613c
movl %eax, %r14d
leaq (%r14,%r14,2), %r12
shlq $0x4, %r12
movl 0x8(%r9,%r12), %edx
movl 0x14(%r9,%r12), %ebp
movq 0x40(%rsp), %rdi
movl %ebp, %esi
leaq 0x48(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0xcba40
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movl 0x24(%rsp), %eax
testq %rax, %rax
je 0xc5d6a
movl %r15d, %ecx
cmpl 0x38(%rsp), %ebp
jb 0xc613c
addq %r9, %r12
subq %r14, %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
movb 0x28(%r12), %dl
movl %edx, %ecx
andb $0x3f, %cl
cmpb $0x3, %cl
jne 0xc5d9c
testq %r13, %r13
je 0xc5d99
movsd 0x20(%r12), %xmm0
ucomisd 0x20(%r13), %xmm0
jbe 0xc5d9c
movq %r12, %r13
testq %r14, %r14
je 0xc5df9
movzbl 0x28(%r14), %esi
andl $0x3f, %esi
cmpl $0x1, %esi
je 0xc5db8
testl %esi, %esi
jne 0xc5dc0
testb %cl, %cl
jne 0xc5dfc
jmp 0xc5dc0
andb $0x3e, %dl
cmpb $0x2, %dl
je 0xc5dfc
cmpb $0x1, %cl
ja 0xc5dca
cmpb %sil, %cl
jb 0xc5df9
movsd 0x20(%r14), %xmm0
movsd 0x20(%r12), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc5df9
ucomisd %xmm1, %xmm0
jne 0xc5dfc
jp 0xc5dfc
movl 0x4(%r14), %ecx
subl (%r14), %ecx
movl 0x4(%r12), %edx
subl (%r12), %edx
cmpl %edx, %ecx
jae 0xc5dfc
movq %r12, %r14
addq $0x30, %r12
decq %rbx
jne 0xc5d76
testq %rax, %rax
je 0xc5e9f
movq 0x48(%rsp), %rcx
movb 0x28(%rcx), %sil
movl %esi, %edx
andb $0x3f, %dl
cmpb $0x3, %dl
jne 0xc5e3a
testq %r13, %r13
je 0xc5e37
movsd 0x20(%rcx), %xmm0
ucomisd 0x20(%r13), %xmm0
jbe 0xc5e3a
movq %rcx, %r13
movzbl 0x28(%r14), %edi
andl $0x3f, %edi
cmpl $0x1, %edi
je 0xc5e51
testl %edi, %edi
jne 0xc5e5b
testb %dl, %dl
jne 0xc5e92
jmp 0xc5e5b
andb $0x3e, %sil
cmpb $0x2, %sil
je 0xc5e92
cmpb $0x1, %dl
ja 0xc5e65
cmpb %dil, %dl
jb 0xc5e8f
movsd 0x20(%r14), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc5e8f
ucomisd %xmm1, %xmm0
jne 0xc5e92
jp 0xc5e92
movl 0x4(%r14), %edx
subl (%r14), %edx
movl 0x10(%rcx), %esi
subl 0xc(%rcx), %esi
cmpl %esi, %edx
jae 0xc5e92
movq %rcx, %r14
addq $0x30, %rcx
decq %rax
jne 0xc5e17
movl 0x8(%r14), %eax
movl %r15d, %ecx
cmpl 0x14(%r14), %eax
je 0xc613c
movq 0x30(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0xc5ef2
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq (%rax,%rcx), %rbp
movupd (%r14), %xmm0
movupd 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
testq %r13, %r13
je 0xc60a7
movb 0x28(%rbp), %al
xorb 0x28(%r13), %al
testb $0x40, %al
jne 0xc60a7
movl 0x8(%rbp), %eax
cmpl 0x8(%r13), %eax
jne 0xc5f4f
movl 0x14(%rbp), %eax
cmpl 0x14(%r13), %eax
je 0xc5f76
movl 0x14(%r14), %ecx
movl %ecx, 0x8(%rbp)
movq 0xc(%r14), %rax
movq %rax, (%rbp)
movl 0x8(%r14), %eax
movl %eax, 0x14(%rbp)
movq (%r14), %rdx
movq %rdx, 0xc(%rbp)
cmpl 0x8(%r13), %ecx
jne 0xc60a7
cmpl 0x14(%r13), %eax
jne 0xc60a7
movl (%rbp), %r12d
movl 0x4(%rbp), %ebx
movl (%r13), %ecx
movl 0x4(%r13), %eax
cmpl %ecx, %r12d
movl %ecx, %r8d
cmoval %r12d, %r8d
cmpl %eax, %ebx
movl %eax, %edi
cmovbl %ebx, %edi
movl 0xc(%rbp), %edx
movl 0xc(%r13), %r9d
cmpl %r9d, %edx
movl %r9d, 0x1c(%rsp)
movl %edx, 0x20(%rsp)
cmoval %edx, %r9d
movl 0x10(%rbp), %esi
movl 0x10(%r13), %edx
cmpl %edx, %esi
movq %rdx, 0x50(%rsp)
movl %edx, %r11d
cmovbl %esi, %r11d
movl %r11d, %r10d
subl %r9d, %r10d
movl $0x0, %edx
cmovbl %edx, %r10d
subl %r8d, %edi
movq 0x8(%rsp), %r8
jbe 0xc60a7
movl $0x0, %edx
cmovbl %edx, %edi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
subl %r12d, %ebx
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
mulsd 0x16b5a4(%rip), %xmm1   # 0x2315b0
ucomisd %xmm1, %xmm0
jbe 0xc60a7
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x16b588(%rip), %xmm1   # 0x2315b0
ucomisd %xmm1, %xmm0
jbe 0xc60a7
cmpl %r9d, %r11d
jbe 0xc60a7
movl %r10d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subl 0x20(%rsp), %esi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd 0x16b55e(%rip), %xmm1   # 0x2315b0
ucomisd %xmm1, %xmm0
jbe 0xc60a7
movq 0x50(%rsp), %rax
subl 0x1c(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x16b53f(%rip), %xmm1   # 0x2315b0
ucomisd %xmm1, %xmm0
jbe 0xc60a7
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rbp)
movupd %xmm1, 0x10(%rbp)
movupd %xmm0, (%rbp)
movb 0x28(%r14), %al
andb $0x3f, %al
movb 0x28(%rbp), %cl
andb $-0x40, %cl
orb %al, %cl
movb %cl, 0x28(%rbp)
movl 0x8(%rbp), %eax
cmpl 0x38(%rsp), %eax
je 0xc60cc
movl 0xc(%rbp), %ecx
movsd (%rbp), %xmm0
movl %ecx, (%rbp)
movsd 0x10(%rbp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movl %eax, 0x14(%rbp)
movupd %xmm1, 0x4(%rbp)
movsd 0x20(%rbp), %xmm0
ucomisd 0x16b53f(%rip), %xmm0   # 0x231618
movl %r15d, %ecx
movq 0x10(%rsp), %r9
jb 0xc613c
movq 0x28(%rsp), %rax
movq (%rax), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc3a9a
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
testl %eax, %eax
je 0xc6131
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdx
movsd 0x30(%rax), %xmm0
movl 0x28(%rax), %ecx
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc3c60
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movl %r15d, %ecx
testl %eax, %eax
jne 0xc613c
movq 0x30(%rsp), %rax
decq (%rax)
movl %r15d, %ecx
incl %r15d
movl %ecx, %eax
cmpl %r8d, %r15d
jbe 0xc5ce6
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq