// File: /workspace/github_cpp_makefile_star2_O1/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), %rdi
testl %edi, %edi
je 0xc44e3
movq %rdi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbx
shlq $0x4, %rbx
movq 0x28(%rsp), %rax
addq 0x10(%rax), %rbx
movl $0x1, %r15d
xorl %r12d, %r12d
movq %rdi, (%rsp)
movl %r15d, %ebp
cmpl %edi, %r15d
je 0xc4204
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 0xc4204
movl %r12d, %ecx
jmp 0xc44d4
movl %r15d, %ecx
cmpl %r12d, %r15d
jbe 0xc44d4
movl %r12d, %r13d
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x4, %r14
movl 0x8(%rbx,%r14), %edx
movl 0x14(%rbx,%r14), %esi
movq 0x28(%rsp), %rdi
leaq 0x30(%rsp), %rcx
leaq 0xc(%rsp), %r8
callq 0xca79c
movq (%rsp), %rdi
movl 0xc(%rsp), %eax
testq %rax, %rax
je 0xc4262
addq %rbx, %r14
movl %r15d, %ecx
movq 0x20(%rsp), %rdx
cmpl %edx, 0x14(%r14)
jb 0xc44d4
cmpl %r15d, %r12d
jae 0xc430c
leaq (,%r13,2), %rcx
addq %r13, %rcx
shlq $0x4, %rcx
addq %rbx, %rcx
subq %r13, %rbp
xorl %r12d, %r12d
testq %r12, %r12
je 0xc42fa
movzbl 0x28(%r12), %edx
andl $0x3f, %edx
cmpl $0x1, %edx
je 0xc42a5
testl %edx, %edx
jne 0xc42b3
testb $0x3f, 0x28(%rcx)
jne 0xc42fd
cmpb $0x1, %dl
jne 0xc42b3
movb 0x28(%rcx), %sil
andb $0x3e, %sil
cmpb $0x2, %sil
je 0xc42fd
movb 0x28(%rcx), %sil
andb $0x3f, %sil
cmpb %dl, %sil
setb %dl
cmpb $0x2, %sil
setb %sil
testb %sil, %dl
jne 0xc42fa
movsd 0x20(%r12), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc42fa
ucomisd %xmm1, %xmm0
jne 0xc42fd
jp 0xc42fd
movl 0x4(%r12), %edx
subl (%r12), %edx
movl 0x4(%rcx), %esi
subl (%rcx), %esi
cmpl %esi, %edx
jae 0xc42fd
movq %rcx, %r12
addq $0x30, %rcx
decq %rbp
jne 0xc4283
jmp 0xc430f
xorl %r12d, %r12d
testq %rax, %rax
je 0xc43a5
movq 0x30(%rsp), %rcx
testq %r12, %r12
je 0xc4395
movzbl 0x28(%r12), %edx
andl $0x3f, %edx
cmpl $0x1, %edx
je 0xc433f
testl %edx, %edx
jne 0xc434d
testb $0x3f, 0x28(%rcx)
jne 0xc4398
cmpb $0x1, %dl
jne 0xc434d
movb 0x28(%rcx), %sil
andb $0x3e, %sil
cmpb $0x2, %sil
je 0xc4398
movb 0x28(%rcx), %sil
andb $0x3f, %sil
cmpb %dl, %sil
setb %dl
cmpb $0x2, %sil
setb %sil
testb %sil, %dl
jne 0xc4395
movsd 0x20(%r12), %xmm0
movsd 0x20(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0xc4395
ucomisd %xmm1, %xmm0
jne 0xc4398
jp 0xc4398
movl 0x4(%r12), %edx
subl (%r12), %edx
movl 0x10(%rcx), %esi
subl 0xc(%rcx), %esi
cmpl %esi, %edx
jae 0xc4398
movq %rcx, %r12
addq $0x30, %rcx
decq %rax
jne 0xc431d
movl %r15d, %ecx
testq %r12, %r12
je 0xc44d4
movl 0x8(%r12), %eax
movl %r15d, %ecx
cmpl 0x14(%r12), %eax
je 0xc44d4
movq 0x18(%rsp), %r14
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xc43ff
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 (%rsp), %rdi
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
movups (%r12), %xmm0
movupd 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm2, 0x20(%rcx,%rax)
movupd %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movq 0x20(%rsp), %rax
cmpl %eax, 0x8(%r12)
je 0xc4475
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)
movsd 0x20(%rbp), %xmm0
ucomisd 0x161096(%rip), %xmm0   # 0x225518
movl %r15d, %ecx
jb 0xc44d4
movq 0x10(%rsp), %rax
movq (%rax), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc2b66
movq (%rsp), %rdi
testl %eax, %eax
je 0xc44c9
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdx
movsd 0x30(%rax), %xmm0
movl 0x28(%rax), %ecx
movq %rbp, %rdi
movq %r12, %rsi
callq 0xc2d60
movq (%rsp), %rdi
movl %r15d, %ecx
testl %eax, %eax
jne 0xc44d4
movq 0x18(%rsp), %rax
decq (%rax)
movl %r15d, %ecx
incl %r15d
movl %ecx, %r12d
cmpl %edi, %r15d
jbe 0xc41d0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq