// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: lchain_qgen_mcopy_input(Candidates_list*, overlap_region_alloc*, unsigned int, unsigned long, All_reads*, ul_idx_t const*, unsigned int, unsigned long, long, long, long, double, double, double, double, long, unsigned int, double, unsigned int, st_mt_t*)
void lchain_qgen_mcopy_input(Candidates_list* cl, overlap_region_alloc* ol, uint32_t rid, uint64_t rl, All_reads* rdb, 
				const ul_idx_t *udb, uint32_t apend_be, uint64_t max_n_chain, int64_t max_skip, int64_t max_iter, 
				int64_t max_dis, double chn_pen_gap, double chn_pen_skip, double bw_rate, double bw_thres_sec, 
				int64_t quick_check, uint32_t gen_off, double mcopy_rate, uint32_t mcopy_khit_cut, st_mt_t *sp)
{
	// fprintf(stderr, "+[M::%s]\n", __func__);
	uint64_t i, k, l, m, cn = cl->length, yid, si; overlap_region *r; ///srt = 0
	clear_overlap_region_alloc(ol);

	// if(rid == 160) {
	// 	fprintf(stderr, "[M::%s::utg%.6ul] sp->n::%u\n", __func__, rid+1, (uint32_t)sp->n);
	// }
	for (l = 0, k = 1, m = 0, si = 0; k <= cn; k++) {
		if((k == cn) || (cl->list[k].readID != cl->list[l].readID)) {
			if(cl->list[l].readID != rid) {
				yid = cl->list[l].readID;
				// if(rid == 160) {
				// 	fprintf(stderr, "+[M::%s::utg%.6lul] [%lu, %lu), m::%lu, ol->length::%lu\n", __func__, yid+1, l, k, m, ol->length);
				// }
				for (; si < sp->n && sp->a[si] < l; si++);
				if(si >= sp->n || sp->a[si] >= k) {///might be unmatched
					m += lchain_qdp_mcopy(cl, l, k-l, m, &(cl->chainDP), ol, max_skip, max_iter, max_dis, chn_pen_gap, chn_pen_skip, bw_rate, 
						rid, rl, rdb?Get_READ_LENGTH((*rdb), yid):udb->ug->u.a[yid].len, quick_check, apend_be, gen_off, 0, mcopy_rate, mcopy_khit_cut, 0);
				} else {
					m = special_lchain(cl, ol, rid, rl, rdb, udb, apend_be, max_skip, max_iter, max_dis, chn_pen_gap, 
					chn_pen_skip, bw_thres_sec, quick_check, gen_off, mcopy_rate, mcopy_khit_cut, sp, &si, m, l, k);
				}
				// if(rid == 160) {
				// 	fprintf(stderr, "-[M::%s::utg%.6lul] [%lu, %lu), m::%lu, ol->length::%lu\n", __func__, yid+1, l, k, m, ol->length);
				// }
			}
			l = k;
		}
	}
	cl->length = m;

	// for (k = 0; k < ol->length; k++) {
	// 	fprintf(stderr, "---[M::%s::utg%.6dl] q[%d, %d), t[%d, %d), khit_off::%u\n", __func__, 
    //     (int32_t)ol->list[k].y_id+1, ol->list[k].x_pos_s, ol->list[k].x_pos_e+1,
	// 	ol->list[k].y_pos_s, ol->list[k].y_pos_e+1, ol->list[k].non_homopolymer_errors);
	// }

	k = ol->length;
	if (ol->length > max_n_chain) {
		int32_t w, n[4], s[4]; overlap_region t;
		n[0] = n[1] = n[2] = n[3] = 0, s[0] = s[1] = s[2] = s[3] = 0;
		ks_introsort_or_ss(ol->length, ol->list); ///srt = 1;
		for (i = 0; i < ol->length; ++i) {
			r = &(ol->list[i]);
			w = ha_ov_type(r, rl);
			++n[w];
			if (((uint64_t)n[w]) == max_n_chain) s[w] = r->shared_seed;
		}
		if (s[0] > 0 || s[1] > 0 || s[2] > 0 || s[3] > 0) {
			// n[0] = n[1] = n[2] = n[3] = 0;
			for (i = 0, k = 0; i < ol->length; ++i) {
				r = &(ol->list[i]);
				w = ha_ov_type(r, rl);
				// ++n[w];
				// if (((int)n[w] <= max_n_chain) || (r->shared_seed >= s[w] && s[w] >= (asm_opt.k_mer_length<<1))) {
				if ((r->shared_seed >= s[w]) || (r->x_pos_strand == 1)) {
					if (k != i) {
						t = ol->list[k];
						ol->list[k] = ol->list[i];
						ol->list[i] = t;
					}
					++k;
				}
			}
			ol->length = k;
		}
	}
	ks_introsort_or_xs(ol->length, ol->list);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movsd %xmm4, 0x38(%rsp)
movsd %xmm3, 0x58(%rsp)
movsd %xmm2, 0x68(%rsp)
movq %xmm1, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
movl %edx, %r13d
movq %rdi, %rbp
movq 0x1a0(%rsp), %r12
movq 0x8(%rdi), %r14
movq %rsi, (%rsp)
movq %rsi, %rdi
callq 0x11935
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
leaq 0x18(%rbp), %rax
movq %rax, 0x60(%rsp)
pushq $0x1
popq %rbx
xorl %eax, %eax
xorl %r15d, %r15d
cmpq %r14, %rbx
ja 0xe9fb4
movq (%rbp), %rdx
jne 0xe9e16
movq %rax, %rcx
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
jmp 0xe9e38
movq %rbx, %rsi
shlq $0x4, %rsi
movq %rax, %rcx
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
movl (%rdx,%rsi), %edx
xorl %ecx, %edx
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
je 0xe9fac
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %r13d, %ecx
je 0xe9fa9
movl %ecx, %ecx
movq (%r12), %rdx
movq 0x10(%rsp), %rsi
cmpq %rdx, %rsi
jae 0xe9efe
movq 0x10(%r12), %rdi
movq (%rdi,%rsi,8), %rdi
cmpq %rax, %rdi
jae 0xe9e73
incq %rsi
movq %rsi, 0x10(%rsp)
jmp 0xe9e52
cmpq %rbx, %rdi
jae 0xe9efe
movq %rbp, %rdi
movq (%rsp), %rsi
movl %r13d, %edx
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
pushq %rbx
pushq %rax
pushq %r15
leaq 0x28(%rsp), %rax
pushq %rax
pushq %r12
movl 0x1c0(%rsp), %eax
pushq %rax
movl 0x1c0(%rsp), %eax
pushq %rax
pushq 0x1c0(%rsp)
pushq 0x1c0(%rsp)
pushq 0x1c0(%rsp)
pushq 0x1c0(%rsp)
movl 0x1b8(%rsp), %eax
pushq %rax
callq 0xee5e0
addq $0x60, %rsp
movq %rax, %r15
jmp 0xe9fa9
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0xe9f12
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %r10
jmp 0xe9f2d
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %rdx
subq %rax, %rdx
movq %rbp, %rdi
movq %rax, %rsi
movq %r15, %rcx
movq 0x60(%rsp), %r8
movq (%rsp), %r9
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
pushq $0x0
movl 0x1a0(%rsp), %eax
pushq %rax
pushq $0x0
movl 0x1a8(%rsp), %eax
pushq %rax
movl 0x180(%rsp), %eax
pushq %rax
pushq 0x1b0(%rsp)
pushq %r10
pushq 0x40(%rsp)
pushq %r13
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
callq 0x14f8d
addq $0x60, %rsp
addq %rax, %r15
movq %rbx, %rax
incq %rbx
jmp 0xe9dfb
movq %r15, 0x8(%rbp)
movq (%rsp), %r13
movq (%r13), %rsi
movq 0x10(%r13), %r15
movq 0x168(%rsp), %rbp
cmpq %rbp, %r15
jbe 0xea0fd
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %r15, %rdi
callq 0xe248f
movq 0x10(%r13), %rbx
xorl %r14d, %r14d
movq %rbx, %r15
subq $0x1, %r15
jb 0xea031
movq (%r13), %r12
leaq (%r12,%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3aee
movl %eax, %eax
movslq 0x70(%rsp,%rax,4), %rcx
incq %rcx
movl %ecx, 0x70(%rsp,%rax,4)
cmpq %rbp, %rcx
jne 0xea028
movl 0x24(%r12,%r14), %ecx
movl %ecx, 0x40(%rsp,%rax,4)
addq $0xa8, %r14
jmp 0xe9ff4
movdqa 0x40(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xea0f6
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq %rbx, %r12
jae 0xea0f0
movq (%r13), %rbp
leaq (%r14,%rbp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0xe3aee
movl 0x24(%rbp,%r14), %ecx
movl %eax, %eax
cmpl 0x40(%rsp,%rax,4), %ecx
jge 0xea086
cmpl $0x1, 0xc(%rbp,%r14)
jne 0xea0dd
cmpq %r15, %r12
je 0xea0da
imulq $0xa8, %r15, %rax
addq %rax, %rbp
movl $0xa8, %edx
leaq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
movl $0xa8, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2160
movq (%rsp), %rbx
movq (%rbx), %rdi
addq %r14, %rdi
movl $0xa8, %edx
leaq 0x80(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rbx
incq %r15
incq %r12
addq $0xa8, %r14
movq (%rsp), %r13
jmp 0xea053
movq %r15, 0x10(%r13)
jmp 0xea0f9
movq %rbx, %r15
movq (%r13), %rsi
movq %r15, %rdi
callq 0xe1c6f
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq