// File: chhylp123[P]hifiasm/inter.cpp
// Function: ul_realignment(ug_opt_t const*, asg_t*, unsigned int, char const*)
ma_ug_t *ul_realignment(const ug_opt_t *uopt, asg_t *sg, uint32_t double_check_cache, const char *bin_file)
{
	fprintf(stderr, "[M::%s::] ==> starting UL\n", __func__);
	mg_idxopt_t opt; uldat_t sl;
	int32_t cutoff;
	char* gfa_name = NULL; MALLOC(gfa_name, strlen(asm_opt.output_file_name)+50);
	sprintf(gfa_name, "%s.%s", asm_opt.output_file_name, bin_file);

	init_aux_table(); ha_opt_update_cov(&asm_opt, asm_opt.hom_cov);
	cutoff = REA_ALIGN_CUTOFF;
	init_mg_opt(&opt, !(asm_opt.flag&HA_F_NO_HPC), 19, 10, cutoff, asm_opt.max_n_chain, asm_opt.ul_error_rate, asm_opt.ul_error_rate, asm_opt.ul_error_rate_low, asm_opt.ul_error_rate_hpc, asm_opt.ul_ec_round);
	init_uldat_t(&sl, NULL, NULL, &opt, CHUNK_SIZE, asm_opt.thread_num, uopt, NULL);
	ma_ug_t *ug = gen_polished_ug(uopt, sg);
	// dd_ug(sg, ug, uopt->coverage_cut, uopt->sources, uopt->ruIndex, "UL.sa");
	// debug_sl_compress_base_disk_0(&sl, asm_opt.ar);
	// detect_outlier_len("ul_realignment");
	clear_all_ul_t(&UL_INF);

	///for debug interval
	if(!load_all_ul_t(&UL_INF, gfa_name, &R_INF, ug)) {
		gen_UL_reovlps(&sl, ug, sg, gfa_name, cutoff, 1);
		// exit(1);
		write_all_ul_t(&UL_INF, gfa_name, ug);
	} else{
		free(UL_INF.ridx.idx.a); free(UL_INF.ridx.occ.a); 
		memset(&(UL_INF.ridx), 0, sizeof((UL_INF.ridx)));
		if(double_check_cache){
			if(drenew_UL_reovlps(&sl, ug, sg, gfa_name, cutoff)) {
				write_all_ul_t(&UL_INF, gfa_name, ug);
			}
		}
	}

	// print_ul_alignment(ug, &UL_INF, 147, "init-0");
	filter_ul_ug(ug);
	// print_ul_alignment(ug, &UL_INF, 147, "init-1");
	gen_ul_vec_rid_t(&UL_INF, NULL, ug);
	// print_ul_alignment(ug, &UL_INF, 147, "init-2");
	update_ug_arch_ul_mul(ug);

	// exit(1);
	// print_ul_alignment(ug, &UL_INF, 41927, "init-3");
	// kt_for(asm_opt.thread_num, update_ug_arch_ul, ug, ug->g->n_arc);
	// print_all_ul_t_stat(&UL_INF);
	// kt_for(sl.n_thread, update_ovlp_src, &sl, R_INF.total_reads);
	// kt_for(sl.n_thread, update_ovlp_src_bl, &sl, R_INF.total_reads);
	
	// print_ovlp_src_bl_stat(&UL_INF, sl.uopt);
	// print_ul_ovlps(&UL_INF, 0); print_ul_ovlps(&UL_INF, 1);

	// destory_all_ul_t(&UL_INF); 
	free(gfa_name);
	return ug;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq 0xb9f1f(%rip), %rax     # 0x3c3fe0
movq (%rax), %rdi
leaq 0x8b91a(%rip), %rsi     # 0x3959e5
leaq 0x8b92e(%rip), %rdx     # 0x395a00
movb $0x0, %al
callq 0x22a0
movq $0x0, -0x1a0(%rbp)
leaq 0xba70d(%rip), %rax     # 0x3c47f8
movq 0x10(%rax), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0x32, %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rdi
leaq 0xba6df(%rip), %rax     # 0x3c47f8
movq 0x10(%rax), %rdx
movq -0x20(%rbp), %rcx
leaq 0x7fb15(%rip), %rsi     # 0x389c3d
movb $0x0, %al
callq 0x2050
callq 0x9690
leaq 0xba6bd(%rip), %rax     # 0x3c47f8
movl 0xd0(%rax), %esi
leaq 0xba6b0(%rip), %rdi     # 0x3c47f8
callq 0x3b60
movl $0x20, -0x194(%rbp)
leaq 0xba69a(%rip), %rax     # 0x3c47f8
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %esi
movl -0x194(%rbp), %r8d
leaq 0xba67a(%rip), %rax     # 0x3c47f8
movl 0xe8(%rax), %r9d
leaq 0xba66c(%rip), %rax     # 0x3c47f8
movsd 0x228(%rax), %xmm0
leaq 0xba65d(%rip), %rax     # 0x3c47f8
movsd 0x228(%rax), %xmm1
leaq 0xba64e(%rip), %rax     # 0x3c47f8
movsd 0x230(%rax), %xmm2
leaq 0xba63f(%rip), %rax     # 0x3c47f8
movsd 0x238(%rax), %xmm3
leaq 0xba630(%rip), %rax     # 0x3c47f8
movl 0x240(%rax), %eax
leaq -0xa8(%rbp), %rdi
movl $0x13, %edx
movl $0xa, %ecx
movl %eax, (%rsp)
callq 0x282cf0
leaq 0xba60a(%rip), %rax     # 0x3c47f8
movslq 0x7c(%rax), %r9
movq -0x8(%rbp), %rax
leaq -0x190(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %edx
leaq -0xa8(%rbp), %rcx
movl $0x3b9aca00, %r8d       # imm = 0x3B9ACA00
movq %rdx, %rsi
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x2fd4f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xecda0
movq %rax, -0x1a8(%rbp)
leaq 0xbb17a(%rip), %rdi     # 0x3c53b8
callq 0x300d10
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rcx
leaq 0xbb160(%rip), %rdi     # 0x3c53b8
leaq 0xbb071(%rip), %rdx     # 0x3c52d0
callq 0x2fd8d0
cmpl $0x0, %eax
jne 0x30a2b3
movq -0x1a8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x1a0(%rbp), %rcx
movl -0x194(%rbp), %r8d
leaq -0x190(%rbp), %rdi
movl $0x1, %r9d
callq 0x2fcfa0
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
leaq 0xbb10f(%rip), %rdi     # 0x3c53b8
callq 0x2fd590
jmp 0x30a338
leaq 0xbb0fe(%rip), %rax     # 0x3c53b8
movq 0x28(%rax), %rdi
callq 0x2210
leaq 0xbb0ee(%rip), %rax     # 0x3c53b8
movq 0x40(%rax), %rdi
callq 0x2210
leaq 0xbb0de(%rip), %rdi     # 0x3c53b8
addq $0x18, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2100
cmpl $0x0, -0x14(%rbp)
je 0x30a336
movq -0x1a8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x1a0(%rbp), %rcx
movl -0x194(%rbp), %r8d
leaq -0x190(%rbp), %rdi
callq 0x2fd280
cmpl $0x0, %eax
je 0x30a334
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
leaq 0xbb089(%rip), %rdi     # 0x3c53b8
callq 0x2fd590
jmp 0x30a336
jmp 0x30a338
movq -0x1a8(%rbp), %rdi
callq 0x2f1800
movq -0x1a8(%rbp), %rdx
leaq 0xbb066(%rip), %rdi     # 0x3c53b8
xorl %eax, %eax
movl %eax, %esi
callq 0x2f0d10
movq -0x1a8(%rbp), %rdi
callq 0x2f11d0
movq -0x1a0(%rbp), %rdi
callq 0x2210
movq -0x1a8(%rbp), %rax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)