// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: test_het_aln(ma_ug_t*, unsigned long, u_trans_t*, unsigned long, st_mt_t*, overlap_region_alloc*, unsigned long)
uint32_t test_het_aln(ma_ug_t *ug, uint64_t rid, u_trans_t *a, uint64_t a_n, st_mt_t *sp, overlap_region_alloc* ol, uint64_t len)
{
	uint64_t k, s, e, z, l = 0; 
	for (k = sp->n = 0; k < a_n; k++) {
		if(a[k].del) continue;
		// if(rid == 56 || rid == 160) {
		// 	fprintf(stderr, "[rid::%lu]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tnw::%f\tf::%u\n", rid, 
		// 		a[k].qn+1, "lc"[ug->u.a[a[k].qn].circ], ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
		// 		a[k].tn+1, "lc"[ug->u.a[a[k].tn].circ], ug->u.a[a[k].tn].len, a[k].ts, a[k].te, a[k].nw, a[k].f);
		// }
		kv_push(uint64_t, *sp, (((uint64_t)a[k].qs)<<32)|((uint64_t)a[k].qe));
	}
	for (k = 0; k < ol->length; k++) {
		// if(b->olist.list[k].non_homopolymer_errors == 0) continue;///exact match is not trans
		s = ol->list[k].x_pos_s; e = ol->list[k].x_pos_e+1;
		kv_push(uint64_t, *sp, (((uint64_t)s)<<32)|((uint64_t)e));
	}

	if(sp->n > 1) {
		radix_sort_gfa64(sp->a, sp->a + sp->n);
		for (k = z = 0; k < sp->n; k++) {
			s = sp->a[k]>>32; e = (uint32_t)sp->a[k];
			if(z > 0 && s <= ((uint32_t)sp->a[z-1])) {
				if(e > ((uint32_t)sp->a[z-1])) {
					sp->a[z-1] >>= 32; sp->a[z-1] <<= 32; sp->a[z-1] |= e;
				}
			} else {
				sp->a[z++] = sp->a[k];
			}
		}
		sp->n = z;
	}
	for (k = l = 0; k < sp->n; k++) l += (((uint32_t)sp->a[k]) - (sp->a[k]>>32));

	// if(rid == 7) {
		// fprintf(stderr, "\n[M::%s] utg%.6lu%c, rid::%lu, l::%lu, len::%lu, sp->n::%u\n", 
		// __func__, rid+1, "lc"[ug->u.a[rid].circ], rid, l, len, (uint32_t)sp->n);
	// 	for (k = 0; k < a_n; k++) {
	// 		if(a[k].del) continue;
	// 		fprintf(stderr, "0\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tnw::%f\tf::%u\n", 
	// 				a[k].qn+1, "lc"[ug->u.a[a[k].qn].circ], ug->u.a[a[k].qn].len, 
	// 				a[k].qs, a[k].qe, "+-"[a[k].rev],
	// 				a[k].tn+1, "lc"[ug->u.a[a[k].tn].circ], ug->u.a[a[k].tn].len, 
	// 				a[k].ts, a[k].te, a[k].nw, a[k].f);
	// 	}
	// 	for (k = 0; k < ol->length; k++) {
	// 		overlap_region *z = &(ol->list[k]);
	// 		fprintf(stderr, "1\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\n", 
	// 			z->x_id+1, "lc"[ug->u.a[z->x_id].circ], ug->u.a[z->x_id].len, 
	// 			z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand],
	// 			z->y_id+1, "lc"[ug->u.a[z->y_id].circ], ug->u.a[z->y_id].len, 
	// 			z->y_pos_s, z->y_pos_e+1);
	// 	}
	// 	for (k = 0; k < sp->n; k++) {
	// 		fprintf(stderr, "in::[%lu, %u)\n", (sp->a[k]>>32), ((uint32_t)sp->a[k]));
	// 	}
	// }
	if((l > 0) && (l >= (len*0.8))) return 1;
	// fprintf(stderr, "\n[M::%s] utg%.6lu%c, rid::%lu, l::%lu, len::%lu, sp->n::%u\n", 
	// 	__func__, rid+1, "lc"[ug->u.a[rid].circ], rid, l, len, (uint32_t)sp->n);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %r14
movq %r8, %rbx
movq $0x0, (%r8)
testq %rcx, %rcx
je 0x1c50f6
movq %rcx, %r15
movq %rdx, %r12
movl $0x2, %r13d
cmpb $0x0, 0x28(%r12)
js 0x1c50ed
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1c50d2
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movl (%r12), %edx
movl 0x4(%r12), %esi
shlq $0x20, %rdx
orq %rdx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %rsi, (%rax,%rcx,8)
addq $0x30, %r12
decq %r15
jne 0x1c509b
cmpq $0x0, 0x10(%r14)
je 0x1c516b
movq 0x10(%rbx), %rax
movl $0x8, %r15d
xorl %r12d, %r12d
movq (%r14), %rcx
movl -0x4(%rcx,%r15), %ebp
movl (%rcx,%r15), %r13d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x1c5146
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
incl %r13d
shlq $0x20, %rbp
orq %r13, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %rbp, (%rax,%rcx,8)
incq %r12
addq $0xa8, %r15
cmpq 0x10(%r14), %r12
jb 0x1c510a
movq (%rbx), %rax
cmpq $0x2, %rax
jb 0x1c51e1
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
cmpq $0x0, (%rbx)
je 0x1c51dc
movq 0x10(%rbx), %rcx
xorl %eax, %eax
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
xorl %esi, %esi
movq (%rcx,%rsi,8), %r8
testq %rax, %rax
je 0x1c51b3
movq %r8, %r9
shrq $0x20, %r9
movq -0x8(%rcx,%rax,8), %rdi
cmpl %edi, %r9d
jbe 0x1c51c4
movq %r8, (%rcx,%rax,8)
incq %rax
incq %rsi
cmpq (%rbx), %rsi
jb 0x1c5199
jmp 0x1c51de
movl %r8d, %r8d
movl %edi, %r9d
cmpl %r9d, %r8d
jbe 0x1c51ba
andq %rdx, %rdi
orq %r8, %rdi
movq %rdi, -0x8(%rcx,%rax,8)
jmp 0x1c51ba
xorl %eax, %eax
movq %rax, (%rbx)
testq %rax, %rax
je 0x1c526a
movq 0x40(%rsp), %rcx
movq 0x10(%rbx), %rsi
xorl %edx, %edx
xorl %edi, %edi
movq (%rsi,%rdi,8), %r8
movl %r8d, %r9d
shrq $0x20, %r8
subq %r8, %rdx
addq %r9, %rdx
incq %rdi
cmpq %rdi, %rax
jne 0x1c51f7
testq %rdx, %rdx
je 0x1c526a
movq %rdx, %xmm0
movq 0x6c05e(%rip), %xmm1    # 0x231280
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x6c062(%rip), %xmm2    # 0x231290
subpd %xmm2, %xmm0
movq %rcx, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
subpd %xmm2, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
addsd %xmm3, %xmm2
mulsd 0x6c355(%rip), %xmm2    # 0x2315b0
addsd %xmm0, %xmm1
movl $0x1, %eax
ucomisd %xmm2, %xmm1
jae 0x1c526c
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq