// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl %eax, -0x5c(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x5c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x5c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x54(%rbp)
movl $0x1, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x54(%rbp), %eax
ja 0x104c7c
movl -0x60(%rbp), %eax
cmpl -0x54(%rbp), %eax
je 0x1047f7
movq -0x38(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x38(%rbp), %rcx
movl -0x60(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
je 0x104c6c
movl -0x60(%rbp), %eax
cmpl -0x64(%rbp), %eax
jbe 0x104c66
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %esi
movq -0x38(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x40(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x110ee0
cmpl $0x0, -0x58(%rbp)
jbe 0x10485d
movl -0x5c(%rbp), %eax
movq -0x38(%rbp), %rcx
movl -0x64(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
jbe 0x10485d
movl -0x60(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x104c6e
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x1049a7
movq -0x38(%rbp), %rax
movl -0x68(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x1048f9
movq -0x48(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1048bf
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1048bf
jmp 0x104999
movq -0x48(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x1048f7
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x2, %eax
je 0x1048f2
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x3, %eax
jne 0x1048f7
jmp 0x104999
jmp 0x1048f9
cmpq $0x0, -0x48(%rbp)
je 0x10498f
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
movq -0x48(%rbp), %rcx
movb 0x28(%rcx), %cl
andb $0x3f, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jge 0x104943
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x10498f
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x10498f
movq -0x48(%rbp), %rax
movsd 0x20(%rax), %xmm1
movq -0x50(%rbp), %rax
movsd 0x20(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x10498f
movq -0x48(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x50(%rbp), %rax
ucomisd 0x20(%rax), %xmm0
jne 0x104997
jp 0x104997
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x48(%rbp), %rcx
subl (%rcx), %eax
movq -0x50(%rbp), %rcx
movl 0x4(%rcx), %ecx
movq -0x50(%rbp), %rdx
subl (%rdx), %ecx
cmpl %ecx, %eax
jae 0x104997
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x104999
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x104873
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x104ae3
movq -0x40(%rbp), %rax
movl -0x68(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x104a34
movq -0x48(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1049fa
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1049fa
jmp 0x104ad5
movq -0x48(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x104a32
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x2, %eax
je 0x104a2d
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x3, %eax
jne 0x104a32
jmp 0x104ad5
jmp 0x104a34
cmpq $0x0, -0x48(%rbp)
je 0x104acb
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
movq -0x48(%rbp), %rcx
movb 0x28(%rcx), %cl
andb $0x3f, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jge 0x104a7e
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x104acb
movq -0x50(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x104acb
movq -0x48(%rbp), %rax
movsd 0x20(%rax), %xmm1
movq -0x50(%rbp), %rax
movsd 0x20(%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x104acb
movq -0x48(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x50(%rbp), %rax
ucomisd 0x20(%rax), %xmm0
jne 0x104ad3
jp 0x104ad3
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x48(%rbp), %rcx
subl (%rcx), %eax
movq -0x50(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq -0x50(%rbp), %rdx
subl 0xc(%rdx), %ecx
cmpl %ecx, %eax
jae 0x104ad3
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x104ad5
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x1049ae
cmpq $0x0, -0x48(%rbp)
je 0x104c64
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x104c64
jmp 0x104b04
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x104b69
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x104b31
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x104b3c
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x104b3c
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
imulq $0x30, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdi
movl $0x30, %edx
callq 0x2180
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x5c(%rbp), %eax
je 0x104bfd
movq -0x48(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x48(%rbp), %rax
movl (%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rax
movsd 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x104c62
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x101b70
cmpl $0x0, %eax
je 0x104c54
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x20(%rbp), %rax
movsd 0x30(%rax), %xmm0
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %ecx
callq 0x101f70
cmpl $0x0, %eax
jne 0x104c62
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
jmp 0x104c64
jmp 0x104c66
movl -0x60(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x104c6e
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1047bb
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)