// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
addq 0x10(%rdi), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x28(%rcx), %rax
movq %rsi, 0x30(%rsp)
movl %esi, %ecx
movq (%rax,%rcx,8), %r8
testl %r8d, %r8d
je 0xc5380
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbx
shlq $0x4, %rbx
movq 0x38(%rsp), %rax
addq 0x10(%rax), %rbx
movl $0x1, %r15d
xorl %r12d, %r12d
movq %r8, 0x8(%rsp)
movl %r15d, %ebp
cmpl %r8d, %r15d
je 0xc4efb
movl %r12d, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl 0x14(%rbx,%rax), %eax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
shlq $0x4, %rcx
cmpl 0x14(%rbx,%rcx), %eax
jne 0xc4efb
movl %r12d, %ecx
jmp 0xc5371
movl %r15d, %ecx
cmpl %r12d, %r15d
jbe 0xc5371
movl %r12d, %r13d
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x4, %r14
movl 0x8(%rbx,%r14), %edx
movl 0x14(%rbx,%r14), %esi
movq 0x38(%rsp), %rdi
leaq 0x40(%rsp), %rcx
leaq 0x1c(%rsp), %r8
callq 0xca79c
movq 0x8(%rsp), %r8
movl 0x1c(%rsp), %eax
testq %rax, %rax
je 0xc4f5a
addq %rbx, %r14
movl %r15d, %ecx
movq 0x30(%rsp), %rdx
cmpl %edx, 0x14(%r14)
jb 0xc5371
cmpl %r15d, %r12d
jae 0xc5010
leaq (,%r13,2), %rcx
addq %r13, %rcx
shlq $0x4, %rcx
addq %rbx, %rcx
subq %r13, %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
movb 0x28(%rcx), %sil
movl %esi, %edx
andb $0x3f, %dl
cmpb $0x3, %dl
jne 0xc4fa1
testq %r13, %r13
je 0xc4f9e
movsd 0x20(%rcx), %xmm0
ucomisd 0x20(%r13), %xmm0
jbe 0xc4fa1
movq %rcx, %r13
testq %r12, %r12
je 0xc4ffe
movb 0x28(%r12), %dil
andb $0x3f, %dil
testb %dl, %dl
je 0xc4fb8
testb %dil, %dil
je 0xc5001
andb $0x3e, %sil
cmpb $0x2, %sil
jne 0xc4fc8
cmpb $0x1, %dil
je 0xc5001
cmpb $0x1, %dl
ja 0xc4fd2
cmpb %dil, %dl
jb 0xc4ffe
movsd 0x20(%r12), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc4ffe
ucomisd %xmm1, %xmm0
jne 0xc5001
jp 0xc5001
movl 0x4(%r12), %edx
subl (%r12), %edx
movl 0x4(%rcx), %esi
subl (%rcx), %esi
cmpl %esi, %edx
jae 0xc5001
movq %rcx, %r12
addq $0x30, %rcx
decq %rbp
jne 0xc4f7e
jmp 0xc5016
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %rax, %rax
je 0xc50b5
movq 0x40(%rsp), %rcx
movb 0x28(%rcx), %sil
movl %esi, %edx
andb $0x3f, %dl
cmpb $0x3, %dl
jne 0xc5047
testq %r13, %r13
je 0xc5044
movsd 0x20(%rcx), %xmm0
ucomisd 0x20(%r13), %xmm0
jbe 0xc5047
movq %rcx, %r13
testq %r12, %r12
je 0xc50a5
movb 0x28(%r12), %dil
andb $0x3f, %dil
testb %dl, %dl
je 0xc505e
testb %dil, %dil
je 0xc50a8
andb $0x3e, %sil
cmpb $0x2, %sil
jne 0xc506e
cmpb $0x1, %dil
je 0xc50a8
cmpb $0x1, %dl
ja 0xc5078
cmpb %dil, %dl
jb 0xc50a5
movsd 0x20(%r12), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc50a5
ucomisd %xmm1, %xmm0
jne 0xc50a8
jp 0xc50a8
movl 0x4(%r12), %edx
subl (%r12), %edx
movl 0x10(%rcx), %esi
subl 0xc(%rcx), %esi
cmpl %esi, %edx
jae 0xc50a8
movq %rcx, %r12
addq $0x30, %rcx
decq %rax
jne 0xc5024
movl %r15d, %ecx
testq %r12, %r12
je 0xc5371
movl 0x8(%r12), %eax
movl %r15d, %ecx
cmpl 0x14(%r12), %eax
je 0xc5371
movq 0x28(%rsp), %r14
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xc510b
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %rbp
movupd (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rcx,%rax)
movupd %xmm1, 0x10(%rcx,%rax)
movupd %xmm0, (%rcx,%rax)
testq %r13, %r13
je 0xc52e9
movb 0x28(%rbp), %al
xorb 0x28(%r13), %al
testb $0x40, %al
jne 0xc52e9
movl 0x8(%rbp), %eax
cmpl 0x8(%r13), %eax
jne 0xc5172
movl 0x14(%rbp), %eax
cmpl 0x14(%r13), %eax
je 0xc51a1
movl 0x14(%r12), %eax
movl %eax, 0x8(%rbp)
movl 0xc(%r12), %eax
movl %eax, (%rbp)
movl 0x10(%r12), %eax
movl %eax, 0x4(%rbp)
movl 0x8(%r12), %eax
movl %eax, 0x14(%rbp)
movl (%r12), %eax
movl %eax, 0xc(%rbp)
movl 0x4(%r12), %eax
movl %eax, 0x10(%rbp)
movl 0x8(%rbp), %eax
cmpl 0x8(%r13), %eax
jne 0xc52e9
movl 0x14(%rbp), %eax
cmpl 0x14(%r13), %eax
jne 0xc52e9
movl (%rbp), %ecx
movl 0x4(%rbp), %eax
movl (%r13), %r8d
movl 0x4(%r13), %edx
cmpl %r8d, %ecx
movl %r8d, %r14d
cmoval %ecx, %r14d
cmpl %edx, %eax
movl %edx, %edi
cmovbl %eax, %edi
movl 0xc(%rbp), %esi
movl 0xc(%r13), %r9d
cmpl %r9d, %esi
movl %r9d, 0x14(%rsp)
movl %esi, 0x18(%rsp)
cmoval %esi, %r9d
movl 0x10(%rbp), %r10d
movl 0x10(%r13), %esi
cmpl %esi, %r10d
movq %rsi, 0x48(%rsp)
movl %esi, %r11d
movq %r10, 0x50(%rsp)
cmovbl %r10d, %r11d
movl %r11d, %r10d
subl %r9d, %r10d
movl $0x0, %esi
cmovbl %esi, %r10d
subl %r14d, %edi
jbe 0xc52e9
movl $0x0, %esi
cmovbl %esi, %edi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x160269(%rip), %xmm1   # 0x2254b0
ucomisd %xmm1, %xmm0
jbe 0xc52e9
subl %r8d, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x16024c(%rip), %xmm1   # 0x2254b0
ucomisd %xmm1, %xmm0
jbe 0xc52e9
cmpl %r9d, %r11d
jbe 0xc52e9
movl %r10d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movq 0x50(%rsp), %rax
subl 0x18(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x16021d(%rip), %xmm1   # 0x2254b0
ucomisd %xmm1, %xmm0
jbe 0xc52e9
movq 0x48(%rsp), %rax
subl 0x14(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x1601fe(%rip), %xmm1   # 0x2254b0
ucomisd %xmm1, %xmm0
jbe 0xc52e9
movupd (%r13), %xmm0
movupd 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rbp)
movupd %xmm1, 0x10(%rbp)
movupd %xmm0, (%rbp)
movb 0x28(%r12), %al
andb $0x3f, %al
movb 0x28(%rbp), %cl
andb $-0x40, %cl
orb %al, %cl
movb %cl, 0x28(%rbp)
movl 0x8(%rbp), %eax
cmpl 0x30(%rsp), %eax
je 0xc530b
movq 0x10(%rbp), %rcx
movl %eax, 0x14(%rbp)
movq (%rbp), %rax
movl 0xc(%rbp), %edx
movl %edx, (%rbp)
movq %rax, 0xc(%rbp)
movq %rcx, 0x4(%rbp)
movsd 0x20(%rbp), %xmm0
ucomisd 0x160200(%rip), %xmm0   # 0x225518
movl %r15d, %ecx
movq 0x8(%rsp), %r8
jb 0xc5371
movq 0x20(%rsp), %rax
movq (%rax), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc2b66
movq 0x8(%rsp), %r8
testl %eax, %eax
je 0xc5366
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
movsd 0x30(%rax), %xmm0
movl 0x28(%rax), %ecx
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc2d60
movq 0x8(%rsp), %r8
movl %r15d, %ecx
testl %eax, %eax
jne 0xc5371
movq 0x28(%rsp), %rax
decq (%rax)
movl %r15d, %ecx
incl %r15d
movl %ecx, %r12d
cmpl %r8d, %r15d
jbe 0xc4ec7
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq