// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rcx, %r13
movl %edx, 0x4(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movq 0x26c18e(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x21bfb5(%rip), %rsi    # 0x244e11
leaq 0x21bfc9(%rip), %rdx    # 0x244e2c
xorl %eax, %eax
callq 0x22b0
leaq 0x26c8e7(%rip), %r14    # 0x295758
movq 0x10(%r14), %rbp
movq %rbp, %rdi
callq 0x2090
leaq 0x32(%rax), %rdi
callq 0x2280
movq %rax, %rbx
leaq 0x210182(%rip), %rsi    # 0x239012
movq %rax, %rdi
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2050
callq 0x32d70
movl 0xd0(%r14), %esi
movq %r14, %rdi
callq 0x2f59
movl (%r14), %eax
movl 0xe8(%r14), %ecx
notl %eax
andl $0x1, %eax
movsd 0x238(%r14), %xmm0
movl 0x240(%r14), %edx
leaq 0x100(%rsp), %rbp
movl %eax, 0x10(%rbp)
movaps 0x216c2b(%rip), %xmm1   # 0x23fb10
movaps %xmm1, (%rbp)
movsd 0x216baf(%rip), %xmm1   # 0x23faa0
movsd %xmm1, 0x50(%rbp)
movaps 0x216c23(%rip), %xmm1   # 0x23fb20
movups %xmm1, 0x14(%rbp)
movaps 0x216c28(%rip), %xmm1   # 0x23fb30
movups %xmm1, 0x24(%rbp)
movaps 0x216c2d(%rip), %xmm1   # 0x23fb40
movups %xmm1, 0x34(%rbp)
movabsq $0x600000032, %rax      # imm = 0x600000032
movq %rax, 0x44(%rbp)
movl $0x5, 0x4c(%rbp)
movl $0x3f4ccccd, 0x58(%rbp) # imm = 0x3F4CCCCD
movl %ecx, 0x80(%rbp)
movups 0x228(%r14), %xmm1
movlps %xmm1, 0x60(%rbp)
movups %xmm1, 0x68(%rbp)
movsd %xmm0, 0x78(%rbp)
movl %edx, 0x84(%rbp)
movslq 0x7c(%r14), %r15
leaq 0x28(%rsp), %r13
movl $0xd0, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movaps %xmm0, -0x18(%r13)
movq %rbp, -0x8(%r13)
movq $0x3b9aca00, 0x38(%r13) # imm = 0x3B9ACA00
movq %r15, 0x40(%r13)
movq 0x8(%rsp), %r15
movq %r12, 0x10(%r13)
movq %r12, %rdi
movq %r15, %rsi
callq 0xb65bf
movq %rax, %r12
leaq 0x26d376(%rip), %r13    # 0x296318
movq %r13, %rdi
callq 0x1e7efd
leaq 0x26d27f(%rip), %rdx    # 0x296230
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1e67fb
testl %eax, %eax
je 0x29081
movq 0x28(%r13), %rdi
callq 0x2220
movq 0x40(%r13), %rdi
callq 0x2220
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r13)
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
cmpl $0x0, 0x4(%rsp)
je 0x2901f
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %r8
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1e6565
testl %eax, %eax
je 0x2901f
leaq 0x26d304(%rip), %rdi    # 0x296318
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1e6781
movl 0x7c(%r14), %edi
movq 0x50(%r13), %rcx
leaq 0x1b7ef9(%rip), %rsi    # 0x1e0f27
movq %r12, %rdx
callq 0x108f2c
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1e0ae1
movl 0x7c(%r14), %edi
movq 0x18(%r12), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x1b7c93(%rip), %rsi    # 0x1e0cef
movq %r12, %rdx
callq 0x108f2c
movq %rbx, %rdi
callq 0x2220
movq %r12, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %r8
pushq $0x1
popq %r9
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x27456
jmp 0x2900d