// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: filter_u_trans_t(kv_u_trans_t*, ma_ug_t*, asg_t*, unsigned int)
void filter_u_trans_t(kv_u_trans_t *ta, ma_ug_t *ug, asg_t *read_g, uint32_t thres)
{
    u_trans_t *a = NULL, *r_a = NULL;
    ma_utg_t *u = NULL;
    uint32_t i, k, m, j, st, n, r_n;
    uint64_t offset, r_beg, r_end, ovlp, min, max, pass;
    kvec_t(uint32_t) cnt; kv_init(cnt);
    for (k = 0; k < ta->idx.n; k++)
    {
        a = u_trans_a(*ta, k);
        n = u_trans_n(*ta, k);
        if(n == 0) continue;
        kv_resize(uint32_t, cnt, n); cnt.n = n;
        min = a[0].qs; max = a[0].qe; 
        for (i = 0; i < n; i++)
        {
            cnt.a[i] = 0;
            min = MIN(min, a[i].qs);
            max = MAX(max, a[i].qe);
        } 
        

        u = &(ug->u.a[k]); pass = 0;
        for (j = 0, offset = 0; j < u->n; j++)
        {
            pass = 0;
            r_beg = offset; r_end = offset + read_g->seq[u->a[j]>>33].len;
            offset += (uint32_t)u->a[j];
            if(min >= r_end) continue;
            if(max <= r_beg) break;
            for (i = 0; i < n; i++)
            {
                if(cnt.a[i] >= thres || r_beg >= a[i].qe)
                {
                    pass++;
                    continue;
                } 
                ovlp = ((MIN(r_end, a[i].qe) > MAX(r_beg, a[i].qs))?
                                        MIN(r_end, a[i].qe) - MAX(r_beg, a[i].qs):0);
                if(ovlp == (r_end - r_beg))
                {   
                    cnt.a[i]++;
                    if(cnt.a[i] >= thres) pass++;
                }
            }

            if(pass == n) 
            {
                for (i = 0; i < n; i++)
                {
                    if(cnt.a[i] < thres) a[i].del = 1;
                }
                break;
            }
        }

        if(pass < n)
        {
            for (i = 0; i < n; i++)
            {
                if(cnt.a[i] < thres) a[i].del = 1;
            }
        } 
    }
    kv_destroy(cnt);

    for (k = 0; k < ta->idx.n; k++)
    {
        a = u_trans_a(*ta, k);
        n = u_trans_n(*ta, k);
        for (st = 0, i = 1; i <= n; ++i)
        {
            if (i == n || a[i].tn != a[st].tn)///same qn && tn
            {
                get_u_trans_spec(ta, a[st].tn, a[st].qn, &r_a, &r_n);
                
                for (m = st; m < i; m++)
                {
                    if(!a[m].del) continue;
                    
                    for (j = 0; j < r_n; j++)
                    {
                        if(r_a[j].tn == a[m].qn && r_a[j].qn == a[m].tn && 
                           r_a[j].ts == a[m].qs && r_a[j].te == a[m].qe && 
                           r_a[j].qs == a[m].ts && r_a[j].qe == a[m].te &&
                           r_a[j].nw == a[m].nw && r_a[j].rev == a[m].rev)
                        {
                            r_a[j].del = 1;
                            break;
                        }
                    }                    
                }
                st = i;
            }
        }
    }
    /*******************************for debug************************************/
    // for (i = 0; i < ta->n; ++i)
    // {
    //     uint32_t c_q = 0, c_t = 0, s, e;
    //     u = &(ug->u.a[ta->a[i].qn]); s = ta->a[i].qs; e = ta->a[i].qe; 
    //     for (j = 0, offset = 0; j < u->n; j++)
    //     {
    //         r_beg = offset; r_end = offset + read_g->seq[u->a[j]>>33].len;
    //         offset += (uint32_t)u->a[j];

    //         ovlp = ((MIN(r_end, e) > MAX(r_beg, s))? MIN(r_end, e) - MAX(r_beg, s):0);
    //         if(ovlp == (r_end - r_beg))
    //         {   
    //             c_q++;
    //             if(c_q >= thres) break;
    //         }
    //     }


    //     u = &(ug->u.a[ta->a[i].tn]); s = ta->a[i].ts; e = ta->a[i].te; 
    //     for (j = 0, offset = 0; j < u->n; j++)
    //     {
    //         r_beg = offset; r_end = offset + read_g->seq[u->a[j]>>33].len;
    //         offset += (uint32_t)u->a[j];

    //         ovlp = ((MIN(r_end, e) > MAX(r_beg, s))? MIN(r_end, e) - MAX(r_beg, s):0);
    //         if(ovlp == (r_end - r_beg))
    //         {   
    //             c_t++;
    //             if(c_t >= thres) break;
    //         }
    //     }

    //     if(ta->a[i].del && (c_q >= thres && c_t >= thres)) fprintf(stderr, "ERROR\n");
    //     if(!ta->a[i].del && (c_q < thres || c_t < thres)) fprintf(stderr, "ERROR\n");
    // }
    /*******************************for debug************************************/

    for (i = m = 0; i < ta->n; ++i)
    {
        if(ta->a[i].del) continue;
        ta->a[m] = ta->a[i];
        m++;
    }
    ta->n = m;
    kt_u_trans_t_idx(ta, ug->g->n_seq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, 0x20(%rsp)
andq $0x0, 0x48(%rsp)
movq %rdi, %rsi
xorl %r11d, %r11d
xorl %edi, %edi
movq $0x0, 0x28(%rsp)
movq %rsi, (%rsp)
movq %rdx, 0x50(%rsp)
movl %r11d, %r11d
cmpq %r11, 0x18(%rsi)
jbe 0x912b9
movq 0x28(%rsi), %rax
movq (%rax,%r11,8), %rbx
testl %ebx, %ebx
je 0x9127f
movq 0x10(%rsi), %rcx
movl %ebx, %r12d
cmpq %r12, 0x28(%rsp)
movq %r12, 0x40(%rsp)
jae 0x910ff
leaq -0x1(%r12), %rax
movq %rcx, %r12
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x4(,%rcx,4), %rsi
incq %rcx
movq %rcx, 0x28(%rsp)
movq %r11, %r14
callq 0x2370
movq %r12, %rcx
movq 0x40(%rsp), %r12
movq %r14, %r11
movq %rax, %rdi
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x30, %rax, %r8
leaq (%rcx,%r8), %rax
movl (%rax), %edx
movl 0x4(%rax), %esi
movl %ebx, %eax
movq %rcx, 0x38(%rsp)
movq %r8, 0x30(%rsp)
addq %r8, %rcx
addq $0x4, %rcx
movq %rcx, 0x58(%rsp)
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x91156
andl $0x0, (%rdi,%r8,4)
movl -0x4(%rcx), %r9d
movl (%rcx), %r10d
cmpq %r9, %rdx
cmovaeq %r9, %rdx
cmpq %r10, %rsi
cmovbeq %r10, %rsi
incq %r8
addq $0x30, %rcx
jmp 0x9112e
movq %r11, 0x8(%rsp)
imulq $0x28, %r11, %r8
movq 0x20(%rsp), %rcx
addq 0x10(%rcx), %r8
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
movq %r8, 0x60(%rsp)
movl 0x10(%r8), %ecx
cmpq %rcx, %r10
jae 0x9123f
movq 0x20(%r15), %rcx
movq 0x18(%r8), %r8
movq (%r8,%r10,8), %r9
movq %r9, %r8
shrq $0x21, %r8
movl (%rcx,%r8,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r11,%rcx), %r14
cmpq %r14, %rdx
movq %r9, 0x10(%rsp)
jae 0x91216
cmpq %r11, %rsi
jbe 0x9123d
movq 0x58(%rsp), %r8
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %r12, %rax
je 0x9121a
movl (%rdi,%r12,4), %r15d
cmpl %ebp, %r15d
jae 0x9120a
movl (%r8), %r9d
cmpq %r9, %r11
jae 0x9120a
cmpq %r9, %r14
cmovbq %r14, %r9
movl -0x4(%r8), %r13d
cmpq %r13, %r11
cmovaq %r11, %r13
subq %r13, %r9
movl $0x0, %r13d
cmovaeq %r9, %r13
cmpl %ecx, %r13d
jne 0x9120d
incl %r15d
movl %r15d, (%rdi,%r12,4)
cmpl %ebp, %r15d
sbbq $-0x1, %rbx
jmp 0x9120d
incq %rbx
incq %r12
addq $0x30, %r8
jmp 0x911c0
xorl %ebx, %ebx
jmp 0x91229
movq 0x40(%rsp), %r12
cmpq %r12, %rbx
movq 0x50(%rsp), %r15
je 0x91287
movl 0x10(%rsp), %ecx
addq %rcx, %r11
incq %r10
movq 0x60(%rsp), %r8
jmp 0x91175
xorl %ebx, %ebx
cmpq %r12, %rbx
jae 0x91276
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorl %edx, %edx
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
cmpq %rdx, %rax
je 0x9127f
cmpl %ebp, (%rdi,%rdx,4)
jae 0x9126d
orb $-0x80, (%rcx)
incq %rdx
addq $0x30, %rcx
jmp 0x91260
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
incl %r11d
jmp 0x91070
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorl %edx, %edx
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
cmpq %rdx, %rax
je 0x9127f
cmpl %ebp, (%rdi,%rdx,4)
jae 0x912b0
orb $-0x80, (%rcx)
incq %rdx
addq $0x30, %rcx
jmp 0x912a3
callq 0x2220
movq (%rsp), %rdi
xorl %ecx, %ecx
leaq 0x48(%rsp), %r15
leaq 0x1c(%rsp), %r12
movl %ecx, %ecx
cmpq %rcx, 0x18(%rdi)
jbe 0x91401
movq 0x28(%rdi), %rax
movq %rcx, 0x10(%rsp)
movq (%rax,%rcx,8), %r13
movq %r13, %rax
shrq $0x20, %rax
imulq $0x30, %rax, %rbp
addq 0x10(%rdi), %rbp
xorl %r14d, %r14d
pushq $0x1
popq %rbx
cmpl %r13d, %ebx
ja 0x913f5
movl %r14d, %eax
jne 0x91314
imulq $0x30, %rax, %rcx
movl 0x14(%rbp,%rcx), %esi
jmp 0x9132c
movl %ebx, %ecx
imulq $0x30, %rcx, %rcx
imulq $0x30, %rax, %rdx
movl 0x14(%rbp,%rdx), %esi
cmpl %esi, 0x14(%rbp,%rcx)
je 0x913ee
imulq $0x30, %rax, %rax
movl 0x8(%rbp,%rax), %edx
movq %r15, %rcx
movq %r12, %r8
callq 0x8feef
cmpl %ebx, %r14d
jae 0x913e7
movl %r14d, %eax
imulq $0x30, %rax, %rax
movb 0x28(%rbp,%rax), %cl
testb %cl, %cl
js 0x9135c
incl %r14d
jmp 0x9133f
addq %rbp, %rax
movl 0x1c(%rsp), %esi
movq 0x48(%rsp), %rdx
imulq $0x30, %rsi, %rdi
xorl %esi, %esi
cmpq %rsi, %rdi
je 0x91357
movl 0x14(%rdx,%rsi), %r8d
cmpl 0x8(%rax), %r8d
jne 0x913d3
movl 0x8(%rdx,%rsi), %r8d
cmpl 0x14(%rax), %r8d
jne 0x913d3
movl 0xc(%rdx,%rsi), %r8d
cmpl (%rax), %r8d
jne 0x913d3
movl 0x10(%rdx,%rsi), %r8d
cmpl 0x4(%rax), %r8d
jne 0x913d3
movl (%rdx,%rsi), %r8d
cmpl 0xc(%rax), %r8d
jne 0x913d3
movl 0x4(%rdx,%rsi), %r8d
cmpl 0x10(%rax), %r8d
jne 0x913d3
movsd 0x20(%rdx,%rsi), %xmm0
ucomisd 0x20(%rax), %xmm0
jne 0x913d3
jp 0x913d3
movb 0x28(%rdx,%rsi), %r8b
movl %r8d, %r9d
xorb %cl, %r9b
testb $0x40, %r9b
je 0x913d9
addq $0x30, %rsi
jmp 0x9136e
orb $-0x80, %r8b
movb %r8b, 0x28(%rdx,%rsi)
jmp 0x91357
movl %ebx, %r14d
movq (%rsp), %rdi
incl %ebx
jmp 0x912fc
movq 0x10(%rsp), %rcx
incl %ecx
jmp 0x912ce
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %ecx
movl %eax, %edx
cmpq %rcx, (%rdi)
jbe 0x91447
movq 0x10(%rdi), %rsi
imulq $0x30, %rcx, %r8
cmpb $0x0, 0x28(%rsi,%r8)
js 0x91443
addq %rsi, %r8
imulq $0x30, %rdx, %rdx
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups 0x20(%r8), %xmm2
movups %xmm2, 0x20(%rsi,%rdx)
movups %xmm1, 0x10(%rsi,%rdx)
movups %xmm0, (%rsi,%rdx)
incl %eax
incl %ecx
jmp 0x91405
movq %rdx, (%rdi)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
callq 0x88a31
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq