// File: /workspace/github_cpp_makefile_star2_O2/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, 0xc(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq 0xb1712(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x55401(%rip), %rsi     # 0x1f6cd9
leaq 0x55415(%rip), %rdx     # 0x1f6cf4
xorl %eax, %eax
callq 0x22b0
leaq 0xb1e6b(%rip), %r12     # 0x253758
movq 0x10(%r12), %rbp
movq %rbp, %rdi
callq 0x2090
leaq 0x32(%rax), %rdi
callq 0x2280
movq %rax, %rbx
leaq 0x49605(%rip), %rsi     # 0x1eaf12
movq %rax, %rdi
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2050
callq 0x6b0f
movl 0xd0(%r12), %esi
movq %r12, %rdi
callq 0x3430
movl (%r12), %eax
movl 0xe8(%r12), %ecx
notl %eax
andl $0x1, %eax
movsd 0x238(%r12), %xmm0
movl 0x240(%r12), %edx
leaq 0x100(%rsp), %r13
movl %eax, 0x10(%r13)
movaps 0x50078(%rip), %xmm1    # 0x1f19e0
movaps %xmm1, (%r13)
movsd 0x4ffeb(%rip), %xmm1    # 0x1f1960
movsd %xmm1, 0x50(%r13)
movaps 0x5006e(%rip), %xmm1    # 0x1f19f0
movups %xmm1, 0x14(%r13)
movaps 0x50072(%rip), %xmm1    # 0x1f1a00
movups %xmm1, 0x24(%r13)
movaps 0x50076(%rip), %xmm1    # 0x1f1a10
movups %xmm1, 0x34(%r13)
movabsq $0x600000032, %rax      # imm = 0x600000032
movq %rax, 0x44(%r13)
movl $0x5, 0x4c(%r13)
movl $0x3f4ccccd, 0x58(%r13) # imm = 0x3F4CCCCD
movl %ecx, 0x80(%r13)
movups 0x228(%r12), %xmm1
movlps %xmm1, 0x60(%r13)
movups %xmm1, 0x68(%r13)
movsd %xmm0, 0x78(%r13)
movl %edx, 0x84(%r13)
movslq 0x7c(%r12), %rbp
leaq 0x28(%rsp), %r12
movl $0xd0, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
xorps %xmm0, %xmm0
movaps %xmm0, -0x18(%r12)
movq %r13, -0x8(%r12)
movq $0x3b9aca00, 0x38(%r12) # imm = 0x3B9ACA00
movq %rbp, 0x40(%r12)
movq %r15, 0x10(%r12)
movq %r15, %rdi
movq %r14, %rsi
callq 0x7b6bf
movq %rax, %r15
leaq 0xb28e5(%rip), %r12     # 0x254318
movq %r12, %rdi
callq 0x19c9d8
leaq 0xb27ee(%rip), %rdx     # 0x254230
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x19aa47
testl %eax, %eax
je 0x1a1aa1
movq 0x28(%r12), %rdi
callq 0x2220
movq 0x40(%r12), %rdi
callq 0x2220
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r12)
movups %xmm0, 0x28(%r12)
movups %xmm0, 0x18(%r12)
cmpl $0x0, 0xc(%rsp)
je 0x1a1ace
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %r8
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x19a5de
testl %eax, %eax
jne 0x1a1abc
jmp 0x1a1ace
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %r8
pushq $0x1
popq %r9
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x19a3dc
leaq 0xb2855(%rip), %rdi     # 0x254318
movq %rbx, %rsi
movq %r15, %rdx
callq 0x19a7cf
movq %r15, %rdi
callq 0x193fe3
leaq 0xb283b(%rip), %rdi     # 0x254318
xorl %esi, %esi
movq %r15, %rdx
callq 0x193ac1
movq %r15, %rdi
callq 0x193c86
movq %rbx, %rdi
callq 0x2220
movq %r15, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq