// File: /workspace/github_cpp_makefile_star2_O2/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 0x88a31
leaq 0x1c7a96(%rip), %rax    # 0x253758
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)
pushq $0x30
popq %rsi
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 -0x24d3(%rip), %rsi     # 0x89844
movl %r14d, %edi
movq %rbp, %rdx
callq 0xc67c0
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %edi, %edi
xorl %eax, %eax
xorl %r13d, %r13d
cmpq 0x60(%rsp), %r12
jae 0x8be23
movq 0x50(%rsp), %r11
imulq $0x30, %r12, %rdx
movq (%r11,%rdx), %rcx
testq %rcx, %rcx
je 0x8be1b
addq %rdx, %r11
pushq $0x38
popq %rbx
pushq $0x1
popq %rbp
xorl %r14d, %r14d
movq %r11, 0x10(%rsp)
cmpq %rcx, %rbp
ja 0x8be1b
je 0x8bd82
movq 0x10(%r11), %rdx
movl (%rdx,%rbx), %esi
imulq $0x30, %r14, %r8
cmpl 0x8(%rdx,%r8), %esi
je 0x8be0f
cmpq %r14, %rbp
jbe 0x8be0c
cmpl %eax, %r13d
jne 0x8bdbb
movq %r15, 0x8(%rsp)
leal (%rax,%rax), %r15d
testl %eax, %eax
pushq $0x2
popq %rax
cmovel %eax, %r15d
imulq $0x18, %r15, %rsi
callq 0x2370
movq 0x10(%rsp), %r11
movq %rax, %rdi
movl %r15d, %eax
movq 0x8(%rsp), %r15
movl %r13d, %ecx
incl %r13d
imulq $0x18, %rcx, %rcx
movq 0x10(%r11), %rdx
imulq $0x30, %r14, %rsi
movl 0x8(%rdx,%rsi), %edx
shlq $0x20, %rdx
orq %r12, %rdx
movq %rdx, (%rdi,%rcx)
movq %r14, %rdx
shrq $0x20, %rdx
movq 0x8(%rdi,%rcx), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %esi       # imm = 0x80000000
orq %rdx, %rsi
subq %r14, %r15
shlq $0x20, %r14
orq %rsi, %r14
movq %r14, 0x8(%rdi,%rcx)
addq %rbp, %r15
movq (%r11), %rcx
movq %rbp, %r14
incq %rbp
addq $0x30, %rbx
jmp 0x8bd61
incq %r12
jmp 0x8bd2f
movl %r13d, %ebx
movq 0x40(%rsp), %rax
cmpq %rbx, (%rax)
movq 0x38(%rsp), %rbp
jb 0x8c1fd
imulq $0x18, %rbx, %rsi
addq %rdi, %rsi
movq %rdi, 0x8(%rsp)
callq 0x67d37
andq $0x0, (%rbp)
addq %r15, %r15
cmpq %r15, 0x8(%rbp)
jae 0x8bea4
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
imulq $0x30, %rax, %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
xorl %eax, %eax
movq 0x8(%rsp), %r8
movq %rbx, 0x10(%rsp)
cmpq %rbx, %rax
je 0x8c0a1
movq %rax, %rbx
imulq $0x18, %rax, %r12
movq 0x50(%rsp), %r14
movq (%r8,%r12), %rsi
movq 0x8(%r8,%r12), %rax
movl %esi, %ecx
imulq $0x30, %rcx, %rdx
movq %rax, %r13
rolq $0x20, %r13
btrq $0x3f, %r13
movq 0x10(%r14,%rdx), %rcx
imulq $0x30, %r13, %rdi
shrq $0x20, %rsi
cmpl 0x8(%rcx,%rdi), %esi
jne 0x8c1de
addq %r8, %r12
addq %rdx, %r14
movq %rax, %rdx
shrq $0x20, %rdx
movabsq $0x3000000000, %rsi     # imm = 0x3000000000
imulq %rsi, %rax
imulq $0x30, %rdx, %r15
addq %rax, %r15
cmpq (%r14), %r13
jae 0x8c091
movl 0x8(%rcx,%r15), %eax
cmpl %eax, 0x4(%r12)
jne 0x8c091
cmpl 0x14(%rcx,%r15), %eax
je 0x8c085
movl 0x4(%rcx,%r15), %eax
cmpl (%rcx,%r15), %eax
je 0x8c085
movl 0x10(%rcx,%r15), %eax
cmpl 0xc(%rcx,%r15), %eax
je 0x8c085
movq (%rbp), %rdx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rdx
jne 0x8bf94
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbp)
imulq $0x30, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rdx
movq 0x10(%r14), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbp)
imulq $0x30, %rdx, %rdx
movups (%rcx,%r15), %xmm0
movups 0x10(%rcx,%r15), %xmm1
movups 0x20(%rcx,%r15), %xmm2
movups %xmm1, 0x10(%rax,%rdx)
movups %xmm2, 0x20(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
andl $0x0, 0x18(%rax,%rdx)
movb 0x28(%rax,%rdx), %cl
movl %ecx, %esi
andb $0x3f, %sil
cmpb $0x3, %sil
jne 0x8bfdd
addq %rdx, %rax
andb $-0x3e, %cl
movb %cl, 0x28(%rax)
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0x8c011
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
imulq $0x30, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movq 0x8(%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
imulq $0x30, %rcx, %rcx
leaq (%rax,%rcx), %rdx
movq 0x10(%r14), %rsi
movups (%rsi,%r15), %xmm0
movups 0x10(%rsi,%r15), %xmm1
movups 0x20(%rsi,%r15), %xmm2
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm2, 0x20(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
andl $0x0, 0x18(%rax,%rcx)
movb 0x28(%rax,%rcx), %al
movl %eax, %ecx
andb $0x3f, %cl
cmpb $0x3, %cl
jne 0x8c061
andb $-0x3e, %al
movb %al, 0x28(%rdx)
movq 0x10(%r14), %rcx
movl 0x14(%rcx,%r15), %eax
movl %eax, 0x8(%rdx)
movq 0xc(%rcx,%r15), %rax
movq %rax, (%rdx)
movl 0x8(%rcx,%r15), %eax
movl %eax, 0x14(%rdx)
movq (%rcx,%r15), %rax
movq %rax, 0xc(%rdx)
incq %r13
addq $0x30, %r15
jmp 0x8bf19
movq %rbx, %rax
incq %rax
movq 0x10(%rsp), %rbx
jmp 0x8beb0
pushq $0x10
popq %rbx
xorl %r14d, %r14d
movq 0x50(%rsp), %rdi
cmpq 0x60(%rsp), %r14
jae 0x8c0c5
movq (%rdi,%rbx), %rdi
callq 0x2220
incq %r14
addq $0x30, %rbx
jmp 0x8c0a7
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 0x88a31
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x8bb24
leaq 0x40(%rsp), %rdx
movl 0x20(%rdx), %edi
movq 0x18(%rdx), %rax
movq 0x18(%rax), %rcx
leaq 0xfe(%rip), %rsi        # 0x8c21c
callq 0xc67c0
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
cmpq (%rbp), %rdx
jae 0x8c174
movq 0x10(%rbp), %rsi
cmpl $-0x1, 0x18(%rsi,%rcx)
je 0x8c16b
leaq (%rsi,%rcx), %rdi
imulq $0x30, %rax, %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
jmp 0x8c129
movq %rax, (%rbp)
pushq $0x1
popq %rcx
pushq $0x38
popq %rdx
xorl %esi, %esi
cmpq %rax, %rcx
ja 0x8c1cc
movq 0x10(%rbp), %r8
jne 0x8c196
imulq $0x30, %rsi, %rax
movl 0x8(%r8,%rax), %edi
jmp 0x8c1a5
imulq $0x30, %rsi, %rdi
movl 0x8(%r8,%rdi), %edi
cmpl %edi, (%r8,%rdx)
je 0x8c1c3
movq %rcx, %rax
subq %rsi, %rax
shlq $0x20, %rsi
orq %rax, %rsi
movq 0x28(%rbp), %rax
movl %edi, %edi
movq %rsi, (%rax,%rdi,8)
movq (%rbp), %rax
movq %rcx, %rsi
incq %rcx
addq $0x30, %rdx
jmp 0x8c180
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15ca81(%rip), %rdi    # 0x1e8c66
leaq 0x15bba5(%rip), %rsi    # 0x1e7d91
leaq 0x15cbaf(%rip), %rcx    # 0x1e8da2
movl $0x3f31, %edx           # imm = 0x3F31
callq 0x2110
leaq 0x15ca0b(%rip), %rdi    # 0x1e8c0f
leaq 0x15bb86(%rip), %rsi    # 0x1e7d91
leaq 0x15cb90(%rip), %rcx    # 0x1e8da2
movl $0x3f2a, %edx           # imm = 0x3F2A
callq 0x2110