// File: /workspace/github_cpp_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movsd %xmm4, 0x38(%rsp)
movsd %xmm3, 0x58(%rsp)
movsd %xmm2, 0x78(%rsp)
movq %xmm1, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, %r13
movq %rcx, 0x8(%rsp)
movl %edx, %ebp
movq %rdi, %r15
movq 0x8(%rdi), %r14
movq %rsi, 0x10(%rsp)
movq %rsi, %rdi
callq 0x3d018
movq $0x0, 0x18(%rsp)
testq %r14, %r14
je 0x12f983
movl 0x1a8(%rsp), %eax
movl 0x1a0(%rsp), %ecx
leaq 0x18(%r15), %rdx
movq %rdx, 0x70(%rsp)
movl %ecx, %ecx
movq %rcx, 0x68(%rsp)
movl %eax, %eax
movq %rax, 0x60(%rsp)
movl $0x1, %ebx
xorl %r12d, %r12d
xorl %eax, %eax
movq (%r15), %rdx
cmpq %r14, %rbx
jne 0x12f7bf
movq %rax, %rcx
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
jmp 0x12f7e1
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 0x12f975
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ebp, %ecx
je 0x12f972
movq 0x1b0(%rsp), %rdx
movq (%rdx), %rdx
movq 0x18(%rsp), %rsi
cmpq %rdx, %rsi
jae 0x12f8cc
movq 0x1b0(%rsp), %rdi
movq 0x10(%rdi), %rdi
incq %rsi
negq %rdx
movq -0x8(%rdi,%rsi,8), %r8
cmpq %rax, %r8
jae 0x12f83e
movq %rsi, 0x18(%rsp)
leaq (%rdx,%rsi), %r8
incq %r8
incq %rsi
cmpq $0x1, %r8
jne 0x12f81a
jmp 0x12f8cc
cmpq %rbx, %r8
jae 0x12f8cc
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
movq %r13, %r8
movq 0x20(%rsp), %r9
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
pushq %rbx
pushq %rax
pushq %r12
leaq 0x30(%rsp), %rax
pushq %rax
pushq 0x1d0(%rsp)
movl 0x1d0(%rsp), %eax
pushq %rax
movl 0x1d0(%rsp), %eax
pushq %rax
pushq 0x1d0(%rsp)
pushq 0x1d0(%rsp)
pushq 0x1d0(%rsp)
pushq 0x1d0(%rsp)
movl 0x1c8(%rsp), %eax
pushq %rax
callq 0x134c3b
addq $0x60, %rsp
movq %rax, %r12
jmp 0x12f972
movl %ecx, %ecx
testq %r13, %r13
je 0x12f8dd
movq 0x18(%r13), %rdx
movq (%rdx,%rcx,8), %r10
jmp 0x12f8f8
movq 0x20(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rcx
movl (%rdx,%rcx,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rbx, %rdx
subq %rax, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rcx
movq 0x70(%rsp), %r8
movq 0x10(%rsp), %r9
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
movl $0x0, %eax
pushq %rax
pushq 0x68(%rsp)
pushq %rax
pushq 0x80(%rsp)
movl 0x190(%rsp), %eax
pushq %rax
pushq 0x1c0(%rsp)
pushq %r10
pushq 0x40(%rsp)
pushq %rbp
pushq 0x1d8(%rsp)
pushq 0x1d8(%rsp)
pushq 0x1d8(%rsp)
callq 0x40ceb
addq $0x60, %rsp
addq %rax, %r12
movq %rbx, %rax
incq %rbx
cmpq %r14, %rbx
jbe 0x12f7ab
jmp 0x12f986
xorl %r12d, %r12d
movq %r12, 0x8(%r15)
movq 0x10(%rsp), %r13
movq (%r13), %rsi
movq 0x10(%r13), %r15
movq 0x178(%rsp), %rbx
cmpq %rbx, %r15
jbe 0x12fb33
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %r15, %rdi
callq 0x127252
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x12fb27
movq (%r13), %rcx
movq 0x8(%rsp), %rdx
decl %edx
addq $0x4, %rcx
movq %rax, %rsi
movl (%rcx), %r8d
movl 0x4(%rcx), %r9d
testl %r8d, %r8d
je 0x12fa2d
movl $0x3, %edi
cmpl %edx, %r9d
jb 0x12f9fe
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movslq 0x80(%rsp,%rdi,4), %r8
incq %r8
movl %r8d, 0x80(%rsp,%rdi,4)
cmpq %rbx, %r8
jne 0x12fa1f
movl 0x20(%rcx), %r8d
movl %r8d, 0x40(%rsp,%rdi,4)
addq $0xa8, %rcx
decq %rsi
jne 0x12f9df
jmp 0x12fa39
movl $0x2, %edi
cmpl %edx, %r9d
jne 0x12f9f5
jmp 0x12f9fe
movdqa 0x40(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x12fb2c
movq 0x8(%rsp), %rcx
decl %ecx
movq %rcx, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x90(%rsp), %r12
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%r13), %rcx
movl 0x4(%rcx,%rbx), %esi
movl 0x8(%rcx,%rbx), %edi
testl %esi, %esi
je 0x12fb0d
movl $0x3, %edx
cmpl 0x8(%rsp), %edi
jb 0x12fa94
xorl %edx, %edx
testl %esi, %esi
setne %dl
movl 0x24(%rcx,%rbx), %esi
cmpl 0x40(%rsp,%rdx,4), %esi
jge 0x12faa5
cmpl $0x1, 0xc(%rcx,%rbx)
jne 0x12faf8
cmpq %r15, %r14
je 0x12faf5
leaq (%rcx,%rbx), %r13
imulq $0xa8, %r15, %rbp
addq %rcx, %rbp
movl $0xa8, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2160
movl $0xa8, %edx
movq %rbp, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %r13
callq 0x2160
movq (%r13), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
movq %r12, %rsi
callq 0x2160
movq 0x10(%r13), %rax
incq %r15
incq %r14
addq $0xa8, %rbx
cmpq %rax, %r14
jb 0x12fa6e
jmp 0x12fb21
movl $0x2, %edx
cmpl 0x8(%rsp), %edi
jne 0x12fa8d
jmp 0x12fa94
movq %r15, 0x10(%r13)
jmp 0x12fb2f
xorl %r15d, %r15d
jmp 0x12fb2f
movq %rax, %r15
movq (%r13), %rsi
movq %r15, %rdi
callq 0x126994
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq