// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: clean_u_trans_t_idx_filter_mmhap_adv(kv_u_trans_t*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, ug_rid_cov_t*)
void clean_u_trans_t_idx_filter_mmhap_adv(kv_u_trans_t *ta, ma_ug_t *ug, asg_t *read_g, ma_hit_t_alloc* src, ug_rid_cov_t *in)
{
    u_trans_clean_t sl; uint64_t k, i, l, st, occ; ha_mzl_t *tz;
    ha_mzl_v srt_a; kv_u_trans_t *bl; u_trans_t *z;
    memset(&sl, 0, sizeof(sl)); kv_init(srt_a);
    
    kt_u_trans_t_idx(ta, ug->g->n_seq);
    sl.n_thread = asm_opt.thread_num; sl.ug = ug; sl.rg = read_g; sl.ta = ta;
    CALLOC(sl.res, sl.n_thread); 
    sl.ov_cutoff = 3; sl.small_ov_rate = 0.8;
    // dbg_prt_utg_trans(ta, ug, "pre");
    kt_for(sl.n_thread, worker_for_trans_clean_re, &sl, sl.ta->idx.n);

    for (i = srt_a.n = occ = 0; i < sl.n_thread; i++) {
		bl = &(sl.res[i]); 
		if(!(bl->n)) continue;
		for (k = 1, l = 0; k <= bl->n; k++) {
			if(k == bl->n || bl->a[k].qn != bl->a[l].qn) {
				if(k > l) {
					kv_pushp(ha_mzl_t, srt_a, &tz);
					tz->x = bl->a[l].qn; tz->x <<= 32; tz->x |= i;
					tz->rid = l>>32; tz->pos = (uint32_t)l;
					occ += (k - l);
				}
				l = k;
			}
		}
	}
    // fprintf(stderr, "[M::%s::] occ::%lu \n", __func__, occ);
    assert(srt_a.n <= sl.ug->u.n); 
	radix_sort_ha_mzl_t_srt1(srt_a.a, srt_a.a + srt_a.n);
    occ <<= 1; ta->n = 0; kv_resize(u_trans_t, *ta, occ); 
    for (i = 0; i < srt_a.n; i++) {
		tz = &(srt_a.a[i]);
		bl = &(sl.res[(uint32_t)(tz->x)]);
		k = tz->rid; k <<= 32; k += tz->pos;
		assert(bl->a[k].qn == (tz->x>>32));
		for (; (k < bl->n) && (bl->a[k].qn == (tz->x>>32)); k++) {
			if(bl->a[k].qn == bl->a[k].tn) continue;
            if(bl->a[k].qe-bl->a[k].qs <= 0) continue;
            if(bl->a[k].te-bl->a[k].ts <= 0) continue;
			kv_pushp(u_trans_t, *ta, &z); 
            (*z) = bl->a[k]; z->occ = 0; if(z->f == RC_3) z->f = RC_2;
            // if(z->ts >= z->te || z->qs >= z->qe)  {
            //     fprintf(stderr, "+[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
            //                     z->qn+1, "lc"[ug->u.a[z->qn].circ], ug->u.a[z->qn].len, z->qs, z->qe, "+-"[z->rev],
            //                     z->tn+1, "lc"[ug->u.a[z->tn].circ], ug->u.a[z->tn].len, z->ts, z->te);
            // }

            kv_pushp(u_trans_t, *ta, &z); 
            (*z) = bl->a[k]; z->occ = 0; if(z->f == RC_3) z->f = RC_2;
            z->qn = bl->a[k].tn; z->qs = bl->a[k].ts; z->qe = bl->a[k].te;
            z->tn = bl->a[k].qn; z->ts = bl->a[k].qs; z->te = bl->a[k].qe;

            // if(z->ts >= z->te || z->qs >= z->qe)  {
            //     fprintf(stderr, "-[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", __func__, 
            //                     z->qn+1, "lc"[ug->u.a[z->qn].circ], ug->u.a[z->qn].len, z->qs, z->qe, "+-"[z->rev],
            //                     z->tn+1, "lc"[ug->u.a[z->tn].circ], ug->u.a[z->tn].len, z->ts, z->te);
            // }
		}
	}

    for (k = 0; k < sl.n_thread; k++) free(sl.res[k].a); 
    free(sl.res); kv_destroy(srt_a);
    kt_u_trans_t_idx(ta, ug->g->n_seq);
    // dbg_prt_utg_trans(ta, ug, "after");
    trans_sec_cut_filter_mmhap_adv(ta, ug, read_g, src, in);
    kt_for(sl.n_thread, worker_for_trans_sec_simple_cut, &sl, sl.ta->idx.n);

    // CALLOC(sl.srt, sl.n_thread); sl.sec_rate = 0.5;
    // kt_for(sl.n_thread, worker_for_trans_sec_cut, &sl, sl.ta->idx.n);
    // sl.cu = gen_u_trans_cluster(ta);
    // kt_for(sl.n_thread, worker_for_trans_sec_cut, &sl, sl.cu->idx.n);
    // free(sl.cu->idx.a); free(sl.cu->z.a); free(sl.cu);
    // for (k = 0; k < sl.n_thread; k++) free(sl.srt[k].a); free(sl.srt);
    for (k = st = 0; k < ta->n; k++) {
        if(ta->a[k].occ == ((uint32_t)-1)) continue;
        ta->a[st] = ta->a[k]; ta->a[st].del = 0; st++;
    }
    ta->n = st;
    for (st = 0, i = 1; i <= ta->n; ++i) {
        if (i == ta->n || ta->a[i].qn != ta->a[st].qn) {
            ta->idx.a[ta->a[st].qn] = (((uint64_t)st)<<32)|(i-st);
            st = i;
        }
    }

    // dbg_prt_utg_extra_trans(ta, ug, asm_opt.output_file_name);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbp
movaps %xmm0, 0x40(%rbp)
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq $0x0, 0x50(%rbp)
movq 0x18(%rsi), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
callq 0xc405a
leaq 0x1c33ff(%rip), %rax    # 0x28a758
movl 0x7c(%rax), %eax
movslq %eax, %r14
movq %r14, 0x20(%rbp)
movq %r12, 0x18(%rsp)
movq %r12, (%rbp)
movq %r15, 0x20(%rsp)
movq %r15, 0x8(%rbp)
movq %rbx, 0x18(%rbp)
movl $0x30, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x10(%rbp)
movq $0x3, 0x28(%rbp)
movabsq $0x3fe999999999999a, %rax # imm = 0x3FE999999999999A
movq %rax, 0x30(%rbp)
movq %rbx, 0x38(%rsp)
movq 0x18(%rbx), %rcx
leaq -0x2560(%rip), %rsi     # 0xc4e50
movl %r14d, %edi
movq %rbp, %rdx
callq 0x1047b4
xorl %r15d, %r15d
cmpq $0x0, 0x20(%rbp)
movl $0x0, %edi
movl $0x0, %r14d
je 0xc7502
xorl %r14d, %r14d
xorl %eax, %eax
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x50(%rsp), %r10
leaq (%r12,%r12,2), %rcx
shlq $0x4, %rcx
cmpq $0x0, (%r10,%rcx)
je 0xc74f1
addq %rcx, %r10
movq (%r10), %rcx
testq %rcx, %rcx
je 0xc74f1
movl $0x1, %ebx
movl $0x38, %ebp
xorl %r13d, %r13d
movq %r10, 0x10(%rsp)
cmpq %rcx, %rbx
je 0xc743f
movq 0x10(%r10), %rcx
movl (%rcx,%rbp), %edx
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x4, %rsi
cmpl 0x8(%rcx,%rsi), %edx
je 0xc74de
cmpq %r13, %rbx
jbe 0xc74db
cmpl %eax, %r14d
jne 0xc7482
movq %r15, 0x8(%rsp)
leal (%rax,%rax), %r15d
testl %eax, %eax
movl $0x2, %eax
cmovel %eax, %r15d
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq %rax, %rdi
movl %r15d, %eax
movq 0x8(%rsp), %r15
movl %r14d, %ecx
incl %r14d
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%r10), %rdx
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x4, %rsi
movl 0x8(%rdx,%rsi), %edx
shlq $0x20, %rdx
orq %r12, %rdx
movq %rdx, (%rdi,%rcx,8)
movq %r13, %rdx
shrq $0x20, %rdx
movq 0x8(%rdi,%rcx,8), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %esi       # imm = 0x80000000
orq %rdx, %rsi
subq %r13, %r15
shlq $0x20, %r13
orq %rsi, %r13
movq %r13, 0x8(%rdi,%rcx,8)
addq %rbx, %r15
movq %rbx, %r13
incq %rbx
movq (%r10), %rcx
addq $0x30, %rbp
cmpq %rcx, %rbx
jbe 0xc741a
incq %r12
cmpq 0x60(%rsp), %r12
jb 0xc73e1
addq %r15, %r15
movl %r14d, %ecx
movq 0x40(%rsp), %rax
cmpq %rcx, (%rax)
jb 0xc7946
movq %rcx, 0x10(%rsp)
leaq (%rcx,%rcx,2), %rax
leaq (%rdi,%rax,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x9e737
movq 0x38(%rsp), %rbp
movq $0x0, (%rbp)
cmpq %r15, 0x8(%rbp)
jae 0xc758d
decq %r15
movq %r15, %rax
shrq %rax
orq %r15, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
incq %rax
movq %rax, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq 0x10(%rsp), %r10
testq %r10, %r10
movq 0x8(%rsp), %r9
je 0xc77ba
xorl %r15d, %r15d
leaq (%r15,%r15,2), %rcx
movq 0x50(%rsp), %r13
movq (%r9,%rcx,8), %rsi
movq 0x8(%r9,%rcx,8), %rax
movl %esi, %edx
leaq (%rdx,%rdx,2), %rdx
movq %rax, %r12
rolq $0x20, %r12
shlq $0x4, %rdx
btrq $0x3f, %r12
movq 0x10(%r13,%rdx), %rdi
leaq (%r12,%r12,2), %r8
shlq $0x4, %r8
shrq $0x20, %rsi
cmpl 0x8(%rdi,%r8), %esi
jne 0xc7927
addq %rdx, %r13
cmpq (%r13), %r12
jae 0xc77ae
leaq (%r9,%rcx,8), %r14
leaq (%rax,%rax,2), %rcx
shrq $0x20, %rax
shlq $0x24, %rcx
leaq (%rax,%rax,2), %rbx
shlq $0x4, %rbx
addq %rcx, %rbx
movq 0x10(%r13), %rax
movl 0x8(%rax,%rbx), %ecx
cmpl %ecx, 0x4(%r14)
jne 0xc77ae
cmpl 0x14(%rax,%rbx), %ecx
je 0xc779d
movl 0x4(%rax,%rbx), %ecx
cmpl (%rax,%rbx), %ecx
je 0xc779d
movl 0x10(%rax,%rbx), %ecx
cmpl 0xc(%rax,%rbx), %ecx
je 0xc779d
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xc7683
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r9
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq 0x10(%r13), %rdx
movups (%rdx,%rbx), %xmm0
movups 0x10(%rdx,%rbx), %xmm1
movups 0x20(%rdx,%rbx), %xmm2
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm2, 0x20(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movl $0x0, 0x18(%rax,%rcx)
movb 0x28(%rax,%rcx), %dl
movl %edx, %esi
andb $0x3f, %sil
cmpb $0x3, %sil
jne 0xc76dc
addq %rcx, %rax
andb $-0x3e, %dl
movb %dl, 0x28(%rax)
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xc7719
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r9
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rax
movq 0x10(%r13), %rsi
movups (%rsi,%rbx), %xmm0
movups 0x10(%rsi,%rbx), %xmm1
movups 0x20(%rsi,%rbx), %xmm2
movups %xmm1, 0x10(%rcx,%rdx)
movups %xmm2, 0x20(%rcx,%rdx)
movups %xmm0, (%rcx,%rdx)
movl $0x0, 0x18(%rcx,%rdx)
movb 0x28(%rcx,%rdx), %cl
movl %ecx, %edx
andb $0x3f, %dl
cmpb $0x3, %dl
jne 0xc7771
andb $-0x3e, %cl
movb %cl, 0x28(%rax)
movq 0x10(%r13), %rcx
movl 0x14(%rcx,%rbx), %edx
movl %edx, 0x8(%rax)
movl 0xc(%rcx,%rbx), %edx
movl %edx, (%rax)
movl 0x10(%rcx,%rbx), %edx
movl %edx, 0x4(%rax)
movl 0x8(%rcx,%rbx), %edx
movl %edx, 0x14(%rax)
movl (%rcx,%rbx), %edx
movl %edx, 0xc(%rax)
movl 0x4(%rcx,%rbx), %ecx
movl %ecx, 0x10(%rax)
incq %r12
addq $0x30, %rbx
cmpq (%r13), %r12
jb 0xc760f
incq %r15
cmpq %r10, %r15
jne 0xc75a3
cmpq $0x0, 0x60(%rsp)
je 0xc77e6
movl $0x10, %ebx
xorl %r14d, %r14d
movq 0x50(%rsp), %rax
movq (%rax,%rbx), %rdi
callq 0x2220
incq %r14
addq $0x30, %rbx
cmpq 0x60(%rsp), %r14
jb 0xc77ca
leaq 0x40(%rsp), %r14
movq 0x10(%r14), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rbx
movq 0x18(%rbx), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbp, %rdi
callq 0xc405a
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x87e3
movl 0x20(%r14), %edi
movq 0x18(%r14), %rax
movq 0x18(%rax), %rcx
leaq 0x121(%rip), %rsi       # 0xc7965
movq %r14, %rdx
callq 0x1047b4
cmpq $0x0, (%rbp)
je 0xc78ab
xorl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x10(%rbp), %rsi
cmpl $-0x1, 0x18(%rsi,%rcx)
je 0xc789c
leaq (%rsi,%rcx), %rdi
movq %rax, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %r8
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups 0x20(%rdi), %xmm2
movups %xmm2, 0x20(%rsi,%r8)
movups %xmm1, 0x10(%rsi,%r8)
movups %xmm0, (%rsi,%r8)
movq 0x10(%rbp), %rsi
andb $0x7f, 0x28(%rsi,%r8)
incq %rax
incq %rdx
addq $0x30, %rcx
cmpq (%rbp), %rdx
jb 0xc7859
jmp 0xc78ad
xorl %eax, %eax
movq %rax, (%rbp)
testq %rax, %rax
je 0xc7915
movl $0x1, %ecx
movl $0x38, %edx
xorl %esi, %esi
cmpq %rax, %rcx
je 0xc78dd
movq 0x10(%rbp), %rax
movl (%rax,%rdx), %edi
leaq (%rsi,%rsi,2), %r8
shlq $0x4, %r8
cmpl 0x8(%rax,%r8), %edi
je 0xc7905
movq %rcx, %rax
subq %rsi, %rax
leaq (%rsi,%rsi,2), %rdi
shlq $0x20, %rsi
orq %rsi, %rax
movq 0x10(%rbp), %rsi
movq 0x28(%rbp), %r8
shlq $0x4, %rdi
movl 0x8(%rsi,%rdi), %esi
movq %rax, (%r8,%rsi,8)
movq %rcx, %rsi
incq %rcx
movq (%rbp), %rax
addq $0x30, %rdx
cmpq %rax, %rcx
jbe 0xc78c2
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x163248(%rip), %rdi    # 0x22ab76
leaq 0x16236c(%rip), %rsi    # 0x229ca1
leaq 0x163376(%rip), %rcx    # 0x22acb2
movl $0x3f31, %edx           # imm = 0x3F31
callq 0x2110
leaq 0x1631d2(%rip), %rdi    # 0x22ab1f
leaq 0x16234d(%rip), %rsi    # 0x229ca1
leaq 0x163357(%rip), %rcx    # 0x22acb2
movl $0x3f2a, %edx           # imm = 0x3F2A
callq 0x2110