// File: /workspace/github_cpp_makefile_star2_O3/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
movups %xmm0, 0x48(%rbp)
movups %xmm0, 0x38(%rbp)
movq 0x18(%rsi), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
callq 0xc4edd
leaq 0x1cd661(%rip), %rax    # 0x295758
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 -0x24e3(%rip), %rsi     # 0xc5c6b
movl %r14d, %edi
movq %rbp, %rdx
callq 0x108f2c
movq 0x20(%rbp), %rcx
xorl %r15d, %r15d
movl $0x0, %edi
movl $0x0, %ebx
testq %rcx, %rcx
je 0xc829a
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x50(%rsp), %r11
leaq (%r14,%r14,2), %rdx
shlq $0x4, %rdx
movq (%r11,%rdx), %rax
testq %rax, %rax
je 0xc828b
addq %rdx, %r11
movl $0x1, %ebp
movl $0x38, %r12d
xorl %r13d, %r13d
movq %r11, 0x10(%rsp)
cmpq %rax, %rbp
je 0xc81d6
movq 0x10(%r11), %rcx
movl (%rcx,%r12), %edx
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x4, %rsi
cmpl 0x8(%rcx,%rsi), %edx
je 0xc8276
cmpq %r13, %rbp
jbe 0xc8273
cmpl %r8d, %ebx
jne 0xc8219
movq %r15, 0x8(%rsp)
leal (%rbx,%rbx), %r15d
testl %ebx, %ebx
movl $0x2, %eax
cmovel %eax, %r15d
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r11
movq %r15, %r8
movq 0x8(%rsp), %r15
movq %rax, %rdi
movl %ebx, %eax
incl %ebx
leaq (%rax,%rax,2), %rax
movq 0x10(%r11), %rcx
leaq (,%r13,2), %rdx
addq %r13, %rdx
shlq $0x4, %rdx
movl 0x8(%rcx,%rdx), %ecx
shlq $0x20, %rcx
orq %r14, %rcx
movq %rcx, (%rdi,%rax,8)
movq %r13, %rcx
shrq $0x20, %rcx
movq 0x8(%rdi,%rax,8), %rdx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orq %rcx, %rdx
subq %r13, %r15
shlq $0x20, %r13
orq %rdx, %r13
movq %r13, 0x8(%rdi,%rax,8)
addq %rbp, %r15
movq (%r11), %rax
movq %rbp, %r13
incq %rbp
addq $0x30, %r12
cmpq %rax, %rbp
jbe 0xc81b0
movq 0x60(%rsp), %rcx
incq %r14
cmpq %rcx, %r14
jb 0xc8180
addq %r15, %r15
movl %ebx, %ecx
movq 0x40(%rsp), %rax
cmpq %rcx, (%rax)
jb 0xc86c6
movq %rcx, 0x10(%rsp)
leaq (%rcx,%rcx,2), %rax
leaq (%rdi,%rax,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x9ee5d
movq 0x38(%rsp), %rbp
movq $0x0, (%rbp)
cmpq %r15, 0x8(%rbp)
jae 0xc8324
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 0xc853c
xorl %r15d, %r15d
leaq (%r15,%r15,2), %rsi
movq 0x50(%rsp), %r13
movq (%r9,%rsi,8), %rdi
movq 0x8(%r9,%rsi,8), %rdx
movl %edi, %eax
leaq (%rax,%rax,2), %rax
movq %rdx, %r12
rolq $0x20, %r12
shlq $0x4, %rax
btrq $0x3f, %r12
movq 0x10(%r13,%rax), %rcx
leaq (%r12,%r12,2), %r8
shlq $0x4, %r8
shrq $0x20, %rdi
cmpl 0x8(%rcx,%r8), %edi
jne 0xc86a7
addq %rax, %r13
movq (%r13), %rax
cmpq %rax, %r12
jae 0xc8530
leaq (%r9,%rsi,8), %r14
leaq (%rdx,%rdx,2), %rsi
shrq $0x20, %rdx
shlq $0x24, %rsi
leaq (%rdx,%rdx,2), %rbx
shlq $0x4, %rbx
addq %rsi, %rbx
movl 0x8(%rcx,%rbx), %edx
cmpl %edx, 0x4(%r14)
jne 0xc8530
cmpl 0x14(%rcx,%rbx), %edx
je 0xc8520
movl 0x4(%rcx,%rbx), %edx
cmpl (%rcx,%rbx), %edx
je 0xc8520
movl 0x10(%rcx,%rbx), %edx
cmpl 0xc(%rcx,%rbx), %edx
je 0xc8520
movq (%rbp), %rdx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rdx
jne 0xc841a
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbp)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rdx
movq 0x10(%r13), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbp)
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movups (%rcx,%rbx), %xmm0
movups 0x10(%rcx,%rbx), %xmm1
movups 0x20(%rcx,%rbx), %xmm2
movups %xmm1, 0x10(%rax,%rdx)
movups %xmm2, 0x20(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movl $0x0, 0x18(%rax,%rdx)
movb 0x28(%rax,%rdx), %cl
movl %ecx, %esi
andb $0x3f, %sil
cmpb $0x3, %sil
jne 0xc8467
addq %rdx, %rax
andb $-0x3e, %cl
movb %cl, 0x28(%rax)
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xc84a1
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
leaq (%rax,%rcx), %rdx
movq 0x10(%r13), %rsi
movups (%rsi,%rbx), %xmm0
movups 0x10(%rsi,%rbx), %xmm1
movups 0x20(%rsi,%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), %al
movl %eax, %ecx
andb $0x3f, %cl
cmpb $0x3, %cl
jne 0xc84fa
andb $-0x3e, %al
movb %al, 0x28(%rdx)
movq 0x10(%r13), %rcx
movl 0x14(%rcx,%rbx), %eax
movl %eax, 0x8(%rdx)
movq 0xc(%rcx,%rbx), %rax
movq %rax, (%rdx)
movl 0x8(%rcx,%rbx), %eax
movl %eax, 0x14(%rdx)
movq (%rcx,%rbx), %rax
movq %rax, 0xc(%rdx)
movq (%r13), %rax
incq %r12
addq $0x30, %rbx
cmpq %rax, %r12
jb 0xc83a9
incq %r15
cmpq %r10, %r15
jne 0xc833a
cmpq $0x0, 0x60(%rsp)
je 0xc8568
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 0xc854c
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 0xc4edd
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x875d
movl 0x20(%r14), %edi
movq 0x18(%r14), %rax
movq 0x18(%rax), %rcx
leaq 0x11f(%rip), %rsi       # 0xc86e5
movq %r14, %rdx
callq 0x108f2c
movq (%rbp), %rsi
testq %rsi, %rsi
je 0xc8695
xorl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x10(%rbp), %rdi
cmpl $-0x1, 0x18(%rdi,%rcx)
je 0xc8628
leaq (%rdi,%rcx), %rsi
movq %rax, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %r8
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, 0x20(%rdi,%r8)
movups %xmm1, 0x10(%rdi,%r8)
movups %xmm0, (%rdi,%r8)
movq 0x10(%rbp), %rsi
andb $0x7f, 0x28(%rsi,%r8)
incq %rax
movq (%rbp), %rsi
incq %rdx
addq $0x30, %rcx
cmpq %rsi, %rdx
jb 0xc85e1
movq %rax, (%rbp)
testq %rax, %rax
je 0xc8695
movq 0x10(%rbp), %rcx
leaq 0x38(%rcx), %rdx
movl $0x1, %esi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r8
shlq $0x4, %r8
cmpq %rax, %rsi
jne 0xc8660
movl 0x8(%rcx,%r8), %r8d
jmp 0xc866a
movl 0x8(%rcx,%r8), %r8d
cmpl %r8d, (%rdx)
je 0xc8689
movq %rsi, %rax
subq %rdi, %rax
shlq $0x20, %rdi
orq %rax, %rdi
movq 0x28(%rbp), %rax
movl %r8d, %r8d
movq %rdi, (%rax,%r8,8)
movq (%rbp), %rax
movq %rsi, %rdi
incq %rsi
addq $0x30, %rdx
cmpq %rax, %rsi
jbe 0xc864c
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16e6b8(%rip), %rdi    # 0x236d66
leaq 0x16d7dc(%rip), %rsi    # 0x235e91
leaq 0x16e7e6(%rip), %rcx    # 0x236ea2
movl $0x3f31, %edx           # imm = 0x3F31
callq 0x2110
leaq 0x16e642(%rip), %rdi    # 0x236d0f
leaq 0x16d7bd(%rip), %rsi    # 0x235e91
leaq 0x16e7c7(%rip), %rcx    # 0x236ea2
movl $0x3f2a, %edx           # imm = 0x3F2A
callq 0x2110