// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: gen_trans_adaptive_mmhap_aln(ug_trans_t*, unsigned long, ha_ovec_buf_t*, kv_ul_ov_t*, char*, unsigned long, kv_u_trans_t*, double, double, double, double)
uint64_t gen_trans_adaptive_mmhap_aln(ug_trans_t *s, uint64_t rid, ha_ovec_buf_t *b, kv_ul_ov_t *bl, char *seq, uint64_t len, kv_u_trans_t *fi, double err_low, double err_high, double bw_low, double bw_high)
{
	uint64_t cnt = ((s->idx_n.a[rid+1]-s->idx_n.a[rid])), ol_h = 0, pass_aln = 0; 
	uint32_t high_occ = asm_opt.polyploidy + 1; overlap_region *aux_o = NULL; 
	///note: high_occ is different
	ug_map_lchain(b->abl, rid, seq, len, s->w, s->k, &(s->udb), &b->olist, &b->clist, bw_low, bw_high,
			s->max_n_chain, 1, NULL, &(b->tmp_region), NULL, &(b->sp), &high_occ, NULL, 0, 1, 0.2, 3, 
			s->is_HPC, s->idx_a.a + s->idx_n.a[rid], cnt, s->srt_a.a, s->srt_a.n, s->mini_cut, s->chain_cut, fi);
	// if(rid == 57) {
	// 	fprintf(stderr, "-1-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }
	///remove candidate chains that have been calculated
	if(!fi) backward_dedup_ol(rid, bl, &(b->sp), &b->olist);///it is ok
	// if(rid == 57) {
	// 	fprintf(stderr, "-2-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }
	filter_by_reliable_ovlp_mmhap_adv(rid, s->filter, &(b->sp), &b->olist, &(s->udb), s->sec_cutoff, 1, 1, s->ccov, &ol_h);
	clear_Cigar_record(&b->cigar1); clear_Round2_alignment(&b->round2); 
	if(!fi) ol_h = 0;

	// if(rid == 57) {
	// 	fprintf(stderr, "-3-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }
	
	ol_h = split_ug_lalign(ol_h, &b->olist, err_high, err_low, 
		&b->clist, &(s->udb), s->uopt, seq, len, &b->self_read, &b->ovlp_read, 
		&b->correct, &b->exz, aux_o, rid, s->k, s->chain_cut, NULL);

	// if(rid == 57) {
	// 	fprintf(stderr, "-4-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }
	
	aux_o = gen_aux_ovlp(&b->olist);///must be here

	// if(rid == 57) {
	// 	fprintf(stderr, "-5-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }

	ol_h = split_ug_lalign(ol_h, &b->olist, err_high, err_low, 
		&b->clist, &(s->udb), s->uopt, seq, len, &b->self_read, &b->ovlp_read, 
		&b->correct, &b->exz, aux_o, rid, s->k, s->chain_cut, NULL);
	
	// if(rid == 57) {
	// 	fprintf(stderr, "-6-[M::%s] utg%.6lu%c, rid::%ld, b->olist->length::%lu\n", 
	// 			__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, b->olist.length);
	// }
	
	if(fi) {///first round
		pass_aln = test_het_aln_mmhap(rid, s->ccov, u_trans_a((*(s->filter)), rid), u_trans_n((*(s->filter)), rid), &b->olist, &(b->sp));
		push_ul_ov_t(&(s->udb), u_trans_a((*(s->filter)), rid), u_trans_n((*(s->filter)), rid), rid, &(b->sp), &b->olist, len, pass_aln, err_high, bl);
		// fprintf(stderr, "-1-[M::%s] utg%.6lu%c, rid::%lu, pass_aln::%lu\n", 
		// 	__func__, rid+1, "lc"[s->ug->u.a[rid].circ], rid, pass_aln);
	} else {///second round
		push_ul_ov_t(&(s->udb), NULL, 0, rid, &(b->sp), &b->olist, len, 0, err_high, bl);
		remove_trans_ovlp_connect(s->udb.ug, rid, bl);
	}
	return pass_aln;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movaps %xmm2, %xmm4
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x58(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r14
movq %rdi, %rbp
movq 0x88(%rdi), %rax
movq (%rax,%rsi,8), %rcx
movq 0x8(%rax,%rsi,8), %r11
subq %rcx, %r11
xorl %eax, %eax
movq %rax, (%rsp)
leaq 0xcd8c0(%rip), %rdx     # 0x28a758
movl 0x1bc(%rdx), %edx
incl %edx
leaq 0x4c(%rsp), %rax
movl %edx, (%rax)
movq 0xe8(%r14), %rdi
movslq 0x30(%rbp), %rax
movq %rax, 0x18(%rsp)
movslq 0x34(%rbp), %r13
leaq 0xc8(%rbp), %rax
movq %rax, 0x50(%rsp)
leaq (%rcx,%rcx,2), %r10
shlq $0x3, %r10
addq 0x70(%rbp), %r10
leaq 0xa0(%r14), %rbx
leaq 0x48(%r14), %rax
movq %rax, 0x40(%rsp)
leaq 0x121a0(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x12250(%r14), %rax
movq %rax, 0x38(%rsp)
movl 0xa0(%rbp), %eax
movq %rax, 0x20(%rsp)
movl 0x60(%rbp), %eax
movq %rax, 0x8(%rsp)
movl 0x90(%rbp), %eax
movslq 0xbc(%rbp), %r15
movslq 0xc0(%rbp), %r12
movsd 0x68556(%rip), %xmm2    # 0x225480
movq %r8, 0x60(%rsp)
movq %r8, %rdx
movq %r9, 0x68(%rsp)
movq %r9, %rcx
movq 0x18(%rsp), %r8
movq %r13, %r9
movaps %xmm4, %xmm0
movaps %xmm3, %xmm1
pushq 0xb0(%rsp)
pushq %r12
movq %rbx, %r12
pushq %r15
movq %rsi, %r15
pushq %rax
pushq 0x98(%rbp)
pushq %r11
pushq %r10
pushq 0x58(%rsp)
movq 0x78(%rsp), %r13
pushq $0x3
movl $0x1, %r11d
pushq %r11
pushq $0x0
movl $0x0, %eax
pushq %rax
leaq 0xac(%rsp), %r10
pushq %r10
pushq %r13
pushq %rax
movl $0x0, %eax
pushq 0xa0(%rsp)
pushq %rax
pushq %r11
pushq 0x98(%rsp)
pushq 0xd8(%rsp)
pushq %rbx
pushq 0xf8(%rsp)
callq 0x12e4fe
addq $0xb0, %rsp
cmpq $0x0, 0xb0(%rsp)
jne 0x1bcfdb
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x1bbbcf
movq 0xf8(%rbp), %rsi
movsd 0x48(%rbp), %xmm0
subq $0x8, %rsp
leaq 0x8(%rsp), %rax
movl $0x1, %r9d
movl %r15d, %edi
movq %r13, %rdx
movq %r12, %rcx
pushq %rax
pushq 0x108(%rbp)
pushq $0x1
callq 0x1bc8f6
addq $0x20, %rsp
leaq 0x108(%r14), %rdi
callq 0x48007
leaq 0x9330(%r14), %rdi
callq 0x47f99
cmpq $0x0, 0xb0(%rsp)
movq %r15, 0x28(%rsp)
jne 0x1bd041
movq $0x0, (%rsp)
movq (%rsp), %rdi
movq (%rbp), %r8
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
leaq 0x28(%r14), %r11
movq %r11, 0x20(%rsp)
leaq 0x2c0(%r14), %r15
movq %r15, 0x18(%rsp)
addq $0x12268, %r14          # imm = 0x12268
movslq 0x34(%rbp), %rax
movslq 0xc0(%rbp), %r10
movq %r12, 0x70(%rsp)
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movq 0x40(%rsp), %rdx
movq 0x50(%rsp), %rbx
movq %rbx, %rcx
movq 0x60(%rsp), %r9
movl $0x0, %r12d
pushq %r12
pushq %r10
pushq %rax
movq 0x40(%rsp), %r13
pushq %r13
movl $0x0, %eax
pushq %rax
pushq %r14
pushq %r15
pushq %r11
pushq 0x48(%rsp)
pushq 0xb0(%rsp)
callq 0x1bade9
addq $0x50, %rsp
movq 0x70(%rsp), %r12
movq %rax, %r15
movq %rax, (%rsp)
movq %r12, %rdi
callq 0x1b3684
movq (%rbp), %r8
movslq 0x34(%rbp), %r10
movslq 0xc0(%rbp), %r11
movq %r15, %rdi
movq %rbx, %r15
movq %r13, %rbx
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movq 0x40(%rsp), %rdx
movq %r15, %rcx
movq 0x60(%rsp), %r9
movl $0x0, %r13d
pushq %r13
pushq %r11
pushq %r10
pushq %rbx
pushq %rax
pushq %r14
pushq 0x48(%rsp)
pushq 0x58(%rsp)
pushq 0x48(%rsp)
pushq 0xb0(%rsp)
callq 0x1bade9
addq $0x50, %rsp
movq %rax, (%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x1bd1e7
movq 0xf8(%rbp), %rax
movq 0x108(%rbp), %rsi
movq 0x28(%rax), %rcx
movq (%rcx,%rbx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
addq 0x10(%rax), %rdx
movq %rbx, %rdi
movq %r12, %r8
movq 0x38(%rsp), %r13
movq %r13, %r9
callq 0x1bb4f0
movl %eax, %r14d
movq 0xf8(%rbp), %rax
movq 0x28(%rax), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
shlq $0x4, %rsi
addq 0x10(%rax), %rsi
subq $0x20, %rsp
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r15, %rdi
movq %rbx, %rcx
movq %r13, %r8
movq %r12, %r9
movsd 0x30(%rsp), %xmm0
callq 0x1bb70b
addq $0x20, %rsp
jmp 0x1bd234
subq $0x20, %rsp
movq %r12, %r9
movq %r15, %r12
movq 0x50(%rsp), %r15
movq %r15, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq 0x58(%rsp), %r8
movsd 0x30(%rsp), %xmm0
callq 0x1bb70b
addq $0x20, %rsp
movq (%r12), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1bbf20
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq