// File: /workspace/github_cpp_makefile_star2_O2/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), %r8
movslq %edx, %rax
imulq $0x30, %rax, %rax
movq %rdi, 0x30(%rsp)
addq 0x10(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x40(%rsp), %rax
andq $0x0, (%rax)
movq 0x28(%r8), %rax
movq %rsi, 0x38(%rsp)
movl %esi, %ecx
movq (%rax,%rcx,8), %r9
movq %r9, %rax
shrq $0x20, %rax
imulq $0x30, %rax, %rax
movq %r8, 0x50(%rsp)
addq 0x10(%r8), %rax
movq %rax, %r8
pushq $0x1
popq %r13
xorl %ecx, %ecx
movq %r9, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpl %r9d, %r13d
ja 0x89ce3
movl %ecx, %r12d
movl %r13d, %ebx
je 0x898d3
imulq $0x30, %r12, %rax
movl 0x14(%r8,%rax), %eax
imulq $0x30, %rbx, %rdx
cmpl 0x14(%r8,%rdx), %eax
je 0x89cdb
cmpl %ecx, %r13d
movl %r13d, %ecx
jbe 0x89cdb
imulq $0x30, %r12, %r14
movl 0x8(%r8,%r14), %edx
movl 0x14(%r8,%r14), %ebp
movq 0x50(%rsp), %rdi
movl %ebp, %esi
leaq 0x40(%rsp), %rcx
leaq 0x2c(%rsp), %r8
callq 0x8feef
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
movl 0x2c(%rsp), %eax
testq %rax, %rax
je 0x89923
movl %r13d, %ecx
cmpl 0x38(%rsp), %ebp
jb 0x89cdb
addq %r8, %r14
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %rbx, %r12
jae 0x899be
movb 0x28(%r14), %dl
movl %edx, %ecx
andb $0x3f, %cl
cmpb $0x3, %cl
jne 0x89958
testq %r15, %r15
je 0x89955
movsd 0x20(%r14), %xmm0
ucomisd 0x20(%r15), %xmm0
jbe 0x89958
movq %r14, %r15
testq %rbp, %rbp
je 0x899af
movzbl 0x28(%rbp), %esi
andl $0x3f, %esi
cmpl $0x1, %esi
je 0x89973
testl %esi, %esi
jne 0x8997b
testb %cl, %cl
jne 0x899b2
jmp 0x8997b
andb $0x3e, %dl
cmpb $0x2, %dl
je 0x899b2
cmpb $0x1, %cl
ja 0x89985
cmpb %sil, %cl
jb 0x899af
movsd 0x20(%rbp), %xmm0
movsd 0x20(%r14), %xmm1
ucomisd %xmm0, %xmm1
ja 0x899af
ucomisd %xmm1, %xmm0
jne 0x899b2
jp 0x899b2
movl 0x4(%rbp), %ecx
subl (%rbp), %ecx
movl 0x4(%r14), %edx
subl (%r14), %edx
cmpl %edx, %ecx
jae 0x899b2
movq %r14, %rbp
incq %r12
addq $0x30, %r14
jmp 0x8992b
movq 0x40(%rsp), %rcx
subq $0x1, %rax
jb 0x89a53
movb 0x28(%rcx), %sil
movl %esi, %edx
andb $0x3f, %dl
cmpb $0x3, %dl
jne 0x899f0
testq %r15, %r15
je 0x899ed
movsd 0x20(%rcx), %xmm0
ucomisd 0x20(%r15), %xmm0
jbe 0x899f0
movq %rcx, %r15
testq %rbp, %rbp
je 0x89a47
movzbl 0x28(%rbp), %edi
andl $0x3f, %edi
cmpl $0x1, %edi
je 0x89a0b
testl %edi, %edi
jne 0x89a15
testb %dl, %dl
jne 0x89a4a
jmp 0x89a15
andb $0x3e, %sil
cmpb $0x2, %sil
je 0x89a4a
cmpb $0x1, %dl
ja 0x89a1f
cmpb %dil, %dl
jb 0x89a47
movsd 0x20(%rbp), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0x89a47
ucomisd %xmm1, %xmm0
jne 0x89a4a
jp 0x89a4a
movl 0x4(%rbp), %edx
subl (%rbp), %edx
movl 0x10(%rcx), %esi
subl 0xc(%rcx), %esi
cmpl %esi, %edx
jae 0x89a4a
movq %rcx, %rbp
addq $0x30, %rcx
jmp 0x899c3
movl %r13d, %ecx
testq %rbp, %rbp
je 0x89cdb
movl 0x8(%rbp), %eax
movl %r13d, %ecx
cmpl 0x14(%rbp), %eax
je 0x89cdb
movq 0x18(%rsp), %rsi
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0x89aaf
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
imulq $0x30, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
imulq $0x30, %rcx, %rcx
leaq (%rax,%rcx), %rbx
movupd (%rbp), %xmm0
movupd 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
testq %r15, %r15
je 0x89c48
movb 0x28(%rbx), %al
xorb 0x28(%r15), %al
testb $0x40, %al
jne 0x89c48
movl 0x8(%rbx), %eax
cmpl 0x8(%r15), %eax
jne 0x89b06
movl 0x14(%rbx), %eax
cmpl 0x14(%r15), %eax
je 0x89b2b
movl 0x14(%rbp), %ecx
movl %ecx, 0x8(%rbx)
movq 0xc(%rbp), %rax
movq %rax, (%rbx)
movl 0x8(%rbp), %eax
movl %eax, 0x14(%rbx)
movq (%rbp), %rdx
movq %rdx, 0xc(%rbx)
cmpl 0x8(%r15), %ecx
jne 0x89c48
cmpl 0x14(%r15), %eax
jne 0x89c48
movl (%rbx), %r12d
movl 0x4(%rbx), %r14d
movl (%r15), %ecx
movl 0x4(%r15), %eax
cmpl %ecx, %r12d
movl %ecx, %r8d
cmoval %r12d, %r8d
cmpl %eax, %r14d
movl %eax, %edi
cmovbl %r14d, %edi
movl 0xc(%rbx), %edx
movl 0xc(%r15), %r9d
cmpl %r9d, %edx
movl %r9d, 0x24(%rsp)
movl %edx, 0x28(%rsp)
cmoval %edx, %r9d
movl 0x10(%rbx), %esi
movl 0x10(%r15), %edx
cmpl %edx, %esi
movq %rdx, 0x48(%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 0x89c48
movl $0x0, %edx
cmovbl %edx, %edi
cvtsi2sd %rdi, %xmm0
subl %r12d, %r14d
cvtsi2sd %r14, %xmm1
mulsd 0x159944(%rip), %xmm1   # 0x1e3500
ucomisd %xmm1, %xmm0
jbe 0x89c48
subl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x15992b(%rip), %xmm1   # 0x1e3500
ucomisd %xmm1, %xmm0
jbe 0x89c48
cmpl %r9d, %r11d
jbe 0x89c48
movl %r10d, %eax
cvtsi2sd %rax, %xmm0
subl 0x28(%rsp), %esi
cvtsi2sd %rsi, %xmm1
mulsd 0x159907(%rip), %xmm1   # 0x1e3500
ucomisd %xmm1, %xmm0
jbe 0x89c48
movq 0x48(%rsp), %rax
subl 0x24(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x1598eb(%rip), %xmm1   # 0x1e3500
ucomisd %xmm1, %xmm0
jbe 0x89c48
movupd (%r15), %xmm0
movupd 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rbx)
movupd %xmm1, 0x10(%rbx)
movupd %xmm0, (%rbx)
movb 0x28(%rbp), %al
andb $0x3f, %al
movb 0x28(%rbx), %cl
andb $-0x40, %cl
orb %al, %cl
movb %cl, 0x28(%rbx)
movl 0x8(%rbx), %eax
cmpl 0x38(%rsp), %eax
je 0x89c6b
movl 0xc(%rbx), %ecx
movsd (%rbx), %xmm0
movl %ecx, (%rbx)
movsd 0x10(%rbx), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movl %eax, 0x14(%rbx)
movupd %xmm1, 0x4(%rbx)
movsd 0x20(%rbx), %xmm0
ucomisd 0x1598f0(%rip), %xmm0   # 0x1e3568
movl %r13d, %ecx
movq 0x10(%rsp), %r9
jb 0x89cdb
movq 0x30(%rsp), %rax
movq (%rax), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x875c0
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
testl %eax, %eax
je 0x89cd0
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdx
movsd 0x30(%rax), %xmm0
movl 0x28(%rax), %ecx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8775f
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
movl %r13d, %ecx
testl %eax, %eax
jne 0x89cdb
movq 0x18(%rsp), %rax
decq (%rax)
movl %r13d, %ecx
incl %r13d
jmp 0x898aa
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq