// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdx, 0x48(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq $0x0, 0x40(%rsp)
xorl %r14d, %r14d
cmpq $0x0, 0x18(%rdi)
movq %rdi, 0x8(%rsp)
je 0xcbdc7
movl %ecx, %ebp
movq $0x0, 0x20(%rsp)
xorl %edi, %edi
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %r15
testl %r15d, %r15d
je 0xcbdb8
movq 0x10(%rbx), %rbx
movl %r15d, %r12d
cmpq %r12, 0x20(%rsp)
jae 0xcbbc1
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 %r15, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
addq %rbx, %rcx
movl (%rcx), %edx
movl 0x4(%rcx), %esi
cmpl $0x1, %r15d
movl %r15d, %ecx
adcl $0x0, %ecx
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x28(%rsp)
addq %rbx, %rax
addq $0x4, %rax
movq %rax, 0x70(%rsp)
xorl %r8d, %r8d
movq %rdx, %r9
movq %rsi, %r10
movl $0x0, (%rdi,%r8,4)
movl -0x4(%rax), %edx
movl (%rax), %esi
cmpq %rdx, %r9
cmovbq %r9, %rdx
cmpq %rsi, %r10
cmovaq %r10, %rsi
incq %r8
addq $0x30, %rax
cmpq %r8, %rcx
jne 0xcbbfe
movq %rbx, 0x30(%rsp)
movq %r15, 0x50(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq %r14, 0x38(%rsp)
leaq (%r14,%r14,4), %r8
cmpl $0x0, 0x10(%rax,%r8,8)
je 0xcbd46
leaq (%rax,%r8,8), %r10
movq 0x48(%rsp), %rax
movq 0x20(%rax), %r11
movq 0x18(%r10), %rax
movq %rax, 0x78(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
movq %r12, 0x68(%rsp)
movq %r10, 0x60(%rsp)
movq %r11, 0x58(%rsp)
movq 0x78(%rsp), %r8
movq (%r8,%rax,8), %r9
movq %r9, %r8
shrq $0x21, %r8
movl (%r11,%r8,8), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%r15,%r13), %r8
cmpq %r8, %rdx
movq %r9, 0x80(%rsp)
jae 0xcbd26
cmpq %r13, %rsi
jbe 0xcbd46
movq 0x70(%rsp), %r12
xorl %r11d, %r11d
xorl %r9d, %r9d
movl (%rdi,%r11,4), %ebx
cmpl %ebp, %ebx
jae 0xcbd01
movl (%r12), %r10d
cmpq %r10, %r13
jae 0xcbd01
cmpq %r10, %r8
cmovbq %r8, %r10
movl -0x4(%r12), %r14d
cmpq %r14, %r13
cmovaq %r13, %r14
subq %r14, %r10
movl $0x0, %r14d
cmovaeq %r10, %r14
cmpl %r15d, %r14d
jne 0xcbd04
incl %ebx
movl %ebx, (%rdi,%r11,4)
cmpl %ebp, %ebx
sbbq $-0x1, %r9
jmp 0xcbd04
incq %r9
incq %r11
addq $0x30, %r12
cmpq %r11, %rcx
jne 0xcbcbd
movq 0x68(%rsp), %r12
cmpq %r12, %r9
je 0xcbd4b
movq 0x60(%rsp), %r10
movq 0x58(%rsp), %r11
jmp 0xcbd29
xorl %r9d, %r9d
movl 0x80(%rsp), %r8d
addq %r8, %r13
incq %rax
movl 0x10(%r10), %r8d
cmpq %r8, %rax
jb 0xcbc7d
jmp 0xcbd72
xorl %r9d, %r9d
jmp 0xcbd72
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rdx
addq %rdx, %rax
addq $0x28, %rax
xorl %edx, %edx
cmpl %ebp, (%rdi,%rdx,4)
jae 0xcbd66
orb $-0x80, (%rax)
incq %rdx
addq $0x30, %rax
cmpq %rdx, %rcx
jne 0xcbd5e
cmpq %r12, %r9
jae 0xcbdae
movl 0x50(%rsp), %eax
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorl %edx, %edx
movq 0x8(%rsp), %rbx
movq 0x38(%rsp), %r14
cmpl %ebp, (%rdi,%rdx,4)
jae 0xcbda0
orb $-0x80, (%rcx)
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %rax
jne 0xcbd98
jmp 0xcbdb8
movq 0x8(%rsp), %rbx
movq 0x38(%rsp), %r14
incl %r14d
cmpq %r14, 0x18(%rbx)
ja 0xcbb54
jmp 0xcbdc9
xorl %edi, %edi
callq 0x2220
cmpq $0x0, 0x18(%rbx)
je 0xcbf3a
xorl %r14d, %r14d
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %r13
testl %r13d, %r13d
je 0xcbf28
movq %r13, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbp
shlq $0x4, %rbp
movq 0x8(%rsp), %rax
addq 0x10(%rax), %rbp
xorl %r12d, %r12d
movl $0x1, %ebx
movl %r12d, %r15d
cmpl %r13d, %ebx
je 0xcbe35
movl %ebx, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl 0x14(%rbp,%rax), %eax
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
cmpl 0x14(%rbp,%rcx), %eax
je 0xcbf1d
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movl 0x8(%rbp,%rax), %edx
movl 0x14(%rbp,%rax), %esi
movq 0x8(%rsp), %rdi
leaq 0x40(%rsp), %rcx
leaq 0x14(%rsp), %r8
callq 0xca79c
cmpl %ebx, %r12d
movl %ebx, %r12d
jae 0xcbf1d
movl %ebx, %eax
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movb 0x28(%rbp,%rcx), %dl
testb %dl, %dl
jns 0xcbf01
movl 0x14(%rsp), %r8d
testq %r8, %r8
je 0xcbf01
addq %rbp, %rcx
movq 0x40(%rsp), %rsi
movl 0x8(%rcx), %edi
shlq $0x4, %r8
leaq (%r8,%r8,2), %r9
xorl %r8d, %r8d
cmpl %edi, 0x14(%rsi,%r8)
jne 0xcbef8
movl 0x8(%rsi,%r8), %r10d
cmpl 0x14(%rcx), %r10d
jne 0xcbef8
movl 0xc(%rsi,%r8), %r10d
cmpl (%rcx), %r10d
jne 0xcbef8
movl 0x10(%rsi,%r8), %r10d
cmpl 0x4(%rcx), %r10d
jne 0xcbef8
movl (%rsi,%r8), %r10d
cmpl 0xc(%rcx), %r10d
jne 0xcbef8
movl 0x4(%rsi,%r8), %r10d
cmpl 0x10(%rcx), %r10d
jne 0xcbef8
movsd 0x20(%rsi,%r8), %xmm0
ucomisd 0x20(%rcx), %xmm0
jne 0xcbef8
jp 0xcbef8
movb 0x28(%rsi,%r8), %r10b
movl %r10d, %r11d
xorb %dl, %r11b
testb $0x40, %r11b
je 0xcbf0f
addq $0x30, %r8
cmpq %r8, %r9
jne 0xcbe9b
incq %r15
cmpq %rax, %r15
jne 0xcbe67
jmp 0xcbf1a
orb $-0x80, %r10b
movb %r10b, 0x28(%rsi,%r8)
jmp 0xcbf01
movl %ebx, %r12d
incl %ebx
cmpl %r13d, %ebx
jbe 0xcbe0d
incl %r14d
movq 0x8(%rsp), %rbx
cmpq %r14, 0x18(%rbx)
ja 0xcbddc
xorl %eax, %eax
cmpq $0x0, (%rbx)
je 0xcbf91
movl $0x1, %ecx
xorl %esi, %esi
movq 0x10(%rbx), %rdx
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
cmpb $0x0, 0x28(%rdx,%rsi)
js 0xcbf86
addq %rdx, %rsi
movl %eax, %edi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
movupd (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, 0x20(%rdx,%rdi)
movups %xmm1, 0x10(%rdx,%rdi)
movupd %xmm0, (%rdx,%rdi)
incl %eax
movl %ecx, %esi
incl %ecx
cmpq %rsi, (%rbx)
ja 0xcbf49
movl %eax, %eax
movq %rax, (%rbx)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbx, %rdi
callq 0xc405a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq