// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
movq $0x0, 0x38(%rsp)
xorl %r14d, %r14d
cmpq $0x0, 0x18(%rdi)
movq %rdi, (%rsp)
je 0xcd0ac
movl %ecx, %ebp
movq $0x0, 0x20(%rsp)
xorl %edi, %edi
movq 0x28(%r15), %rax
movq (%rax,%r14,8), %rbx
testl %ebx, %ebx
je 0xcd09d
movq 0x10(%r15), %r15
movl %ebx, %r12d
cmpq %r12, 0x20(%rsp)
jae 0xcceaf
leaq -0x1(%r12), %rax
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, 0x20(%rsp)
callq 0x2370
movq %rax, %rdi
movq %rbx, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
addq %r15, %rax
movl (%rax), %edx
movl 0x4(%rax), %esi
movl %ebx, %eax
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x28(%rsp)
addq %r15, %rcx
addq $0x4, %rcx
movq %rcx, 0x58(%rsp)
xorl %r8d, %r8d
movq %rdx, %r9
movq %rsi, %r10
movl $0x0, (%rdi,%r8,4)
movl -0x4(%rcx), %edx
movl (%rcx), %esi
cmpq %rdx, %r9
cmovbq %r9, %rdx
cmpq %rsi, %r10
cmovaq %r10, %rsi
incq %r8
addq $0x30, %rcx
cmpq %r8, %rax
jne 0xccee4
movq %r15, 0x30(%rsp)
movq 0x18(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %r14, 0x8(%rsp)
leaq (%r14,%r14,4), %r8
movl 0x10(%rcx,%r8,8), %r9d
testl %r9d, %r9d
je 0xcd028
leaq (%rcx,%r8,8), %r8
movq 0x40(%rsp), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, 0x68(%rsp)
movq %r8, 0x48(%rsp)
movq 0x18(%r8), %rcx
movq %rcx, 0x60(%rsp)
xorl %ebx, %ebx
xorl %ecx, %ecx
movq %r12, 0x50(%rsp)
movq 0x60(%rsp), %r8
movq (%r8,%rbx,8), %r11
movq %r11, %r8
shrq $0x21, %r8
movq 0x68(%rsp), %r10
movl (%r10,%r8,8), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%rcx,%r15), %r8
cmpq %r8, %rdx
movq %r11, 0x70(%rsp)
jae 0xcd00c
cmpq %rcx, %rsi
jbe 0xcd028
movq 0x58(%rsp), %r12
xorl %r9d, %r9d
xorl %r13d, %r13d
movl (%rdi,%r9,4), %r10d
cmpl %ebp, %r10d
jae 0xccfe8
movl (%r12), %r11d
cmpq %r11, %rcx
jae 0xccfe8
cmpq %r11, %r8
cmovbq %r8, %r11
movl -0x4(%r12), %r14d
cmpq %r14, %rcx
cmovaq %rcx, %r14
subq %r14, %r11
movl $0x0, %r14d
cmovaeq %r11, %r14
cmpl %r15d, %r14d
jne 0xccfeb
incl %r10d
movl %r10d, (%rdi,%r9,4)
cmpl %ebp, %r10d
sbbq $-0x1, %r13
jmp 0xccfeb
incq %r13
incq %r9
addq $0x30, %r12
cmpq %r9, %rax
jne 0xccfa1
movq 0x50(%rsp), %r12
cmpq %r12, %r13
je 0xcd06d
movq 0x48(%rsp), %r8
movl 0x10(%r8), %r9d
jmp 0xcd00f
xorl %r13d, %r13d
movl 0x70(%rsp), %r8d
addq %r8, %rcx
incq %rbx
movl %r9d, %r8d
cmpq %r8, %rbx
jb 0xccf5f
jmp 0xcd02b
xorl %r13d, %r13d
cmpq %r12, %r13
jae 0xcd062
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorl %edx, %edx
movq (%rsp), %r15
movq 0x8(%rsp), %r14
cmpl %ebp, (%rdi,%rdx,4)
jae 0xcd054
orb $-0x80, (%rcx)
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %rax
jne 0xcd04c
jmp 0xcd09d
movq (%rsp), %r15
movq 0x8(%rsp), %r14
jmp 0xcd09d
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorl %edx, %edx
movq (%rsp), %r15
movq 0x8(%rsp), %r14
cmpl %ebp, (%rdi,%rdx,4)
jae 0xcd091
orb $-0x80, (%rcx)
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %rax
jne 0xcd089
incl %r14d
cmpq %r14, 0x18(%r15)
ja 0xcce43
jmp 0xcd0ae
xorl %edi, %edi
callq 0x2220
movq 0x18(%r15), %rax
testq %rax, %rax
je 0xcd227
xorl %r14d, %r14d
movq 0x28(%r15), %rcx
movq (%rcx,%r14,8), %r13
testl %r13d, %r13d
je 0xcd21b
movq %r13, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbp
shlq $0x4, %rbp
addq 0x10(%r15), %rbp
xorl %r12d, %r12d
movl $0x1, %ebx
movl %r12d, %r15d
cmpl %r13d, %ebx
jne 0xcd105
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movl 0x14(%rbp,%rax), %esi
jmp 0xcd125
movl %ebx, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movl 0x14(%rbp,%rcx), %esi
cmpl %esi, 0x14(%rbp,%rax)
je 0xcd208
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movl 0x8(%rbp,%rax), %edx
movq (%rsp), %rdi
leaq 0x38(%rsp), %rcx
leaq 0x14(%rsp), %r8
callq 0xcba40
cmpl %ebx, %r12d
movl %ebx, %r12d
jae 0xcd208
movl %ebx, %eax
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movb 0x28(%rbp,%rcx), %dl
testb %dl, %dl
jns 0xcd1ec
movl 0x14(%rsp), %r8d
testq %r8, %r8
je 0xcd1ec
addq %rbp, %rcx
movq 0x38(%rsp), %rsi
movl 0x8(%rcx), %edi
shlq $0x4, %r8
leaq (%r8,%r8,2), %r9
xorl %r8d, %r8d
cmpl %edi, 0x14(%rsi,%r8)
jne 0xcd1e3
movl 0x8(%rsi,%r8), %r10d
cmpl 0x14(%rcx), %r10d
jne 0xcd1e3
movl 0xc(%rsi,%r8), %r10d
cmpl (%rcx), %r10d
jne 0xcd1e3
movl 0x10(%rsi,%r8), %r10d
cmpl 0x4(%rcx), %r10d
jne 0xcd1e3
movl (%rsi,%r8), %r10d
cmpl 0xc(%rcx), %r10d
jne 0xcd1e3
movl 0x4(%rsi,%r8), %r10d
cmpl 0x10(%rcx), %r10d
jne 0xcd1e3
movsd 0x20(%rsi,%r8), %xmm0
ucomisd 0x20(%rcx), %xmm0
jne 0xcd1e3
jp 0xcd1e3
movb 0x28(%rsi,%r8), %r10b
movl %r10d, %r11d
xorb %dl, %r11b
testb $0x40, %r11b
je 0xcd1fa
addq $0x30, %r8
cmpq %r8, %r9
jne 0xcd186
incq %r15
cmpq %rax, %r15
jne 0xcd152
jmp 0xcd205
orb $-0x80, %r10b
movb %r10b, 0x28(%rsi,%r8)
jmp 0xcd1ec
movl %ebx, %r12d
incl %ebx
cmpl %r13d, %ebx
jbe 0xcd0ef
movq (%rsp), %r15
movq 0x18(%r15), %rax
incl %r14d
cmpq %r14, %rax
ja 0xcd0c3
movq (%r15), %rdx
xorl %eax, %eax
testq %rdx, %rdx
je 0xcd283
movl $0x1, %ecx
xorl %esi, %esi
movq 0x10(%r15), %rdi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
cmpb $0x0, 0x28(%rdi,%rsi)
js 0xcd278
addq %rdi, %rsi
movl %eax, %edx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movupd (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, 0x20(%rdi,%rdx)
movups %xmm1, 0x10(%rdi,%rdx)
movupd %xmm0, (%rdi,%rdx)
incl %eax
movq (%r15), %rdx
movl %ecx, %esi
incl %ecx
cmpq %rsi, %rdx
ja 0xcd238
movl %eax, %eax
movq %rax, (%r15)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %r15, %rdi
callq 0xc4edd
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq