// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rdi, %r12
movq 0x2606c4(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x20f21b(%rip), %rsi    # 0x238b41
leaq 0x20f22f(%rip), %rdx    # 0x238b5c
xorl %eax, %eax
callq 0x22b0
leaq 0x260e1d(%rip), %r14    # 0x28a758
movq 0x10(%r14), %r15
movq %r15, %rdi
callq 0x2090
leaq 0x32(%rax), %rdi
callq 0x2280
movq %rax, %rbx
leaq 0x2034c8(%rip), %rsi    # 0x22ce22
movq %rax, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2050
callq 0x3366f
movl 0xd0(%r14), %esi
movq %r14, %rdi
callq 0x2f59
movl (%r14), %eax
movl 0xe8(%r14), %ecx
notl %eax
andl $0x1, %eax
movsd 0x228(%r14), %xmm0
movups 0x230(%r14), %xmm1
movl 0x240(%r14), %edx
movabsq $0x130000000a, %rsi     # imm = 0x130000000A
leaq 0x100(%rsp), %r13
movq %rsi, (%r13)
movl $0x20, 0x14(%r13)
movl %eax, 0x10(%r13)
movabsq $0x7a12000002710, %rax  # imm = 0x7A12000002710
movq %rax, 0x8(%r13)
movabsq $0x271000000014, %rax   # imm = 0x271000000014
movq %rax, 0x18(%r13)
movabsq $0x3f0000003e428f5c, %rax # imm = 0x3F0000003E428F5C
movq %rax, 0x50(%r13)
movabsq $0x271000000019, %rax   # imm = 0x271000000019
movq %rax, 0x28(%r13)
movaps 0x209e3a(%rip), %xmm2   # 0x233840
movups %xmm2, 0x30(%r13)
movabsq $0xb00000005, %rax      # imm = 0xB00000005
movq %rax, 0x20(%r13)
movaps 0x209e30(%rip), %xmm2   # 0x233850
movups %xmm2, 0x40(%r13)
movl $0x3f4ccccd, 0x58(%r13) # imm = 0x3F4CCCCD
movl %ecx, 0x80(%r13)
movsd %xmm0, 0x60(%r13)
movsd %xmm0, 0x68(%r13)
movups %xmm1, 0x70(%r13)
movl %edx, 0x84(%r13)
movslq 0x7c(%r14), %rbp
leaq 0x18(%rsp), %r15
movl $0xe8, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r13, 0x10(%r15)
movq $0x3b9aca00, 0x50(%r15) # imm = 0x3B9ACA00
movq %rbp, 0x58(%r15)
andq $0x0, 0x30(%r15)
movq %r12, 0x28(%r15)
movq 0x10(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0xb556d
movq %rax, %r12
leaq 0x261881(%rip), %r13    # 0x28b318
movq %r13, %rdi
callq 0x1dd4ab
leaq 0x26178a(%rip), %rdx    # 0x28b230
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1dc0e1
testl %eax, %eax
je 0x29b49
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, 0xc(%rsp)
jne 0x29b66
movl 0x7c(%r14), %edi
movq 0x50(%r13), %rcx
leaq 0x1ad51c(%rip), %rsi    # 0x1d7012
movq %r12, %rdx
callq 0x1047b4
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1d6bbe
movl 0x7c(%r14), %edi
movq 0x18(%r12), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x1ad2b8(%rip), %rsi    # 0x1d6ddc
movq %r12, %rdx
callq 0x1047b4
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 0x18(%rsp), %rdi
pushq $0x20
popq %r8
pushq $0x1
popq %r9
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x27cd5
jmp 0x29b85
leaq 0x18(%rsp), %rdi
pushq $0x20
popq %r8
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x27e4d
testl %eax, %eax
je 0x29ae7
leaq 0x26178c(%rip), %rdi    # 0x28b318
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1dc067
jmp 0x29ae7