// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: worker_for_trans_clean(void*, long, int)
static void worker_for_trans_clean(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;
    uint32_t n, r_n, id = i, k, l, z;

    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 = NULL; 
                for (z = l; z < k; z++) {
                    cz = &(a[z]);
                    // fprintf(stderr, ">[M::%s::] qn::%u, tn::%u, cz->nw::%f, cz->f::%u\n", 
                    //     __func__, a[l].qn, a[l].tn, cz->nw, cz->f);
                    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)))) {
                        // fprintf(stderr, "+[M::%s::] qn::%u, tn::%u, mz->nw::%f, mz->f::%u, cz->nw::%f, cz->f::%u\n", 
                        // __func__, a[l].qn, a[l].tn, mz?mz->nw:-1, mz?mz->f:255, cz->nw, cz->f);
                        mz = cz; 
                    }
                }
                for (z = 0; z < r_n; z++) {
                    cz = &(r_a[z]);
                    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)))) {
                        ///note here is (cz->te - cz->ts)
                        // fprintf(stderr, "-[M::%s::] qn::%u, tn::%u, mz->nw::%f, mz->f::%u, cz->nw::%f, cz->f::%u\n", 
                        // __func__, a[l].qn, a[l].tn, mz?mz->nw:-1, mz?mz->f:255, cz->nw, cz->f);
                        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(mz->qn != id) {
                        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->nw >= 0) && ((!trans_ovlp_connect1(cz, s->ug)) 
                                    || (!ovlp_rocc(cz, s->ug, s->rg, s->small_ov_rate, s->ov_cutoff)))) {  
                        // if(mz->qn == 43 && mz->tn == 45) {
                        //     fprintf(stderr, "-[M::%s::] utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__,
                        //     mz->qn+1, "lc"[s->ug->u.a[mz->qn].circ], s->ug->u.a[mz->qn].len, mz->qs, mz->qe, 
                        //     "+-"[mz->rev],
                        //     mz->tn+1, "lc"[s->ug->u.a[mz->tn].circ], s->ug->u.a[mz->tn].len, mz->ts, mz->te);
                        // } 
                        
                        res->n--;
                    } 
                    // else {
                    //     if(mz->qn == 43 && mz->tn == 45) {
                    //         fprintf(stderr, "+[M::%s::] utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tf::%u\n", __func__,
                    //         mz->qn+1, "lc"[s->ug->u.a[mz->qn].circ], s->ug->u.a[mz->qn].len, mz->qs, mz->qe, 
                    //         "+-"[mz->rev], mz->tn+1, "lc"[s->ug->u.a[mz->tn].circ], s->ug->u.a[mz->tn].len, mz->ts, mz->te, mz->f);
                    //     }
                    // }
                }
            }
            l = k;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x18(%rdi), %rcx
movslq %edx, %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq %rdi, 0x10(%rsp)
addq 0x10(%rdi), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq 0x28(%rcx), %rax
movq %rsi, 0x20(%rsp)
movl %esi, %ecx
movq (%rax,%rcx,8), %r14
testl %r14d, %r14d
je 0xc5300
movq %r14, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r8
shlq $0x4, %r8
movq 0x28(%rsp), %rax
addq 0x10(%rax), %r8
movl $0x1, %r15d
xorl %eax, %eax
movq %r8, (%rsp)
movl %r15d, %r13d
cmpl %r14d, %r15d
je 0xc508d
movl %eax, %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movl 0x14(%r8,%rcx), %ecx
leaq (,%r13,2), %rdx
addq %r13, %rdx
shlq $0x4, %rdx
cmpl 0x14(%r8,%rdx), %ecx
jne 0xc508d
movl %eax, %ecx
jmp 0xc52f2
movl %r15d, %ecx
cmpl %eax, %r15d
jbe 0xc52f2
movl %eax, %ebx
leaq (%rbx,%rbx,2), %r12
shlq $0x4, %r12
movl 0x8(%r8,%r12), %edx
movl 0x14(%r8,%r12), %ebp
movq 0x28(%rsp), %rdi
movl %ebp, %esi
leaq 0x30(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0xcba40
movq (%rsp), %r8
movl 0xc(%rsp), %eax
testq %rax, %rax
je 0xc50dd
movl %r15d, %ecx
cmpl 0x20(%rsp), %ebp
jb 0xc52f2
addq %r8, %r12
subq %rbx, %r13
xorl %ebp, %ebp
testq %rbp, %rbp
je 0xc5151
movzbl 0x28(%rbp), %ecx
andl $0x3f, %ecx
movb 0x28(%r12), %dl
cmpl $0x1, %ecx
je 0xc5106
testl %ecx, %ecx
jne 0xc5112
testb $0x3f, %dl
jne 0xc5154
jmp 0xc5112
movl %edx, %esi
andb $0x3e, %sil
cmpb $0x2, %sil
je 0xc5154
andb $0x3f, %dl
cmpb %cl, %dl
setb %cl
cmpb $0x2, %dl
setb %dl
testb %dl, %cl
jne 0xc5151
movsd 0x20(%rbp), %xmm0
movsd 0x20(%r12), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc5151
ucomisd %xmm1, %xmm0
jne 0xc5154
jp 0xc5154
movl 0x4(%rbp), %ecx
subl (%rbp), %ecx
movl 0x4(%r12), %edx
subl (%r12), %edx
cmpl %edx, %ecx
jae 0xc5154
movq %r12, %rbp
addq $0x30, %r12
decq %r13
jne 0xc50e5
testq %rax, %rax
je 0xc51df
movq 0x30(%rsp), %rcx
testq %rbp, %rbp
je 0xc51d3
movzbl 0x28(%rbp), %edx
andl $0x3f, %edx
movb 0x28(%rcx), %sil
cmpl $0x1, %edx
je 0xc5188
testl %edx, %edx
jne 0xc5194
testb $0x3f, %sil
jne 0xc51d6
jmp 0xc5194
movl %esi, %edi
andb $0x3e, %dil
cmpb $0x2, %dil
je 0xc51d6
andb $0x3f, %sil
cmpb %dl, %sil
setb %dl
cmpb $0x2, %sil
setb %sil
testb %sil, %dl
jne 0xc51d3
movsd 0x20(%rbp), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc51d3
ucomisd %xmm1, %xmm0
jne 0xc51d6
jp 0xc51d6
movl 0x4(%rbp), %edx
subl (%rbp), %edx
movl 0x10(%rcx), %esi
subl 0xc(%rcx), %esi
cmpl %esi, %edx
jae 0xc51d6
movq %rcx, %rbp
addq $0x30, %rcx
decq %rax
jne 0xc5167
movl %r15d, %ecx
testq %rbp, %rbp
je 0xc52f2
movl 0x8(%rbp), %eax
movl %r15d, %ecx
cmpl 0x14(%rbp), %eax
je 0xc52f2
movq 0x18(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0xc523b
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 (%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), %r13
movups (%rbp), %xmm0
movupd 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq 0x20(%rsp), %rax
cmpl %eax, 0x8(%rbp)
je 0xc5292
movl 0x14(%rbp), %eax
movl %eax, 0x8(%r13)
movq 0xc(%rbp), %rax
movq %rax, (%r13)
movl 0x8(%rbp), %eax
movl %eax, 0x14(%r13)
movq (%rbp), %rax
movq %rax, 0xc(%r13)
movsd 0x20(%r13), %xmm0
ucomisd 0x16c378(%rip), %xmm0   # 0x231618
movl %r15d, %ecx
jb 0xc52f2
movq 0x10(%rsp), %rax
movq (%rax), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xc3a9a
movq (%rsp), %r8
testl %eax, %eax
je 0xc52e7
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdx
movsd 0x30(%rax), %xmm0
movl 0x28(%rax), %ecx
movq %r13, %rdi
movq %r12, %rsi
callq 0xc3c60
movq (%rsp), %r8
movl %r15d, %ecx
testl %eax, %eax
jne 0xc52f2
movq 0x18(%rsp), %rax
decq (%rax)
movl %r15d, %ecx
incl %r15d
movl %ecx, %eax
cmpl %r14d, %r15d
jbe 0xc5059
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq