// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r15
movq %rdx, %r12
andq $0x0, (%r8)
subq $0x1, %r15
jb 0x179cb7
cmpb $0x0, 0x28(%r12)
js 0x179cb1
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x179c96
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %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
jmp 0x179c56
pushq $0x8
popq %r15
xorl %r12d, %r12d
cmpq 0x10(%r14), %r12
jae 0x179d22
movq (%r14), %rax
movl -0x4(%rax,%r15), %r13d
movl (%rax,%r15), %ebp
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x179d02
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
incl %ebp
shlq $0x20, %r13
orq %rbp, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r13, (%rax,%rcx,8)
incq %r12
addq $0xa8, %r15
jmp 0x179cbe
movq (%rbx), %rax
cmpq $0x2, %rax
jb 0x179d90
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
xorl %ecx, %ecx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
xorl %eax, %eax
cmpq (%rbx), %rcx
jae 0x179d8d
movq 0x10(%rbx), %rsi
movq (%rsi,%rcx,8), %r8
testq %rax, %rax
je 0x179d69
movq %r8, %r9
shrq $0x20, %r9
movq -0x8(%rsi,%rax,8), %rdi
cmpl %edi, %r9d
jbe 0x179d75
movq %r8, (%rsi,%rax,8)
incq %rax
incq %rcx
jmp 0x179d46
movl %r8d, %r8d
movl %edi, %r9d
cmpl %r9d, %r8d
jbe 0x179d70
andq %rdx, %rdi
orq %r8, %rdi
movq %rdi, -0x8(%rsi,%rax,8)
jmp 0x179d70
movq %rax, (%rbx)
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
je 0x179db2
movq 0x10(%rbx), %rsi
movq (%rsi,%rdx,8), %rsi
movl %esi, %edi
shrq $0x20, %rsi
subq %rsi, %rcx
addq %rdi, %rcx
incq %rdx
jmp 0x179d94
testq %rcx, %rcx
je 0x179e08
movq %rcx, %xmm0
movq 0x694ac(%rip), %xmm1    # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x694b0(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq 0x40(%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x696fe(%rip), %xmm1    # 0x1e3500
ucomisd %xmm1, %xmm3
jae 0x179e0c
xorl %eax, %eax
jmp 0x179e0f
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq