// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movsd %xmm3, 0x58(%rsp)
movsd %xmm2, 0x78(%rsp)
movsd %xmm1, 0x50(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x8(%rsp)
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8(%rdi), %r12
movq %rsi, %rdi
callq 0x3db37
movq $0x0, 0x18(%rsp)
testq %r12, %r12
je 0x1298c6
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, %r13d
xorl %r14d, %r14d
xorl %eax, %eax
cmpq %r12, %r13
je 0x1296f4
movq (%r15), %rcx
movq %r13, %rdx
shlq $0x4, %rdx
movq %rax, %rsi
shlq $0x4, %rsi
movl (%rcx,%rsi), %esi
xorl (%rcx,%rdx), %esi
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x1298b8
movq (%r15), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ebp, %ecx
je 0x1298b5
movq 0x1b0(%rsp), %rdx
movq (%rdx), %rsi
movq 0x18(%rsp), %rdx
cmpq %rsi, %rdx
setb %r8b
jae 0x12980e
movq 0x1b0(%rsp), %rdi
movq 0x10(%rdi), %rdi
cmpq %rax, (%rdi,%rdx,8)
jae 0x129769
incq %rdx
movq %rdx, 0x18(%rsp)
cmpq %rdx, %rsi
je 0x12980e
leaq 0x1(%rdx), %r9
cmpq %rax, (%rdi,%rdx,8)
movq %r9, %rdx
jb 0x129741
decq %r9
cmpq %rsi, %r9
setb %r8b
movq %r9, %rdx
testb %r8b, %r8b
je 0x12980e
movq 0x1b0(%rsp), %rsi
movq 0x10(%rsi), %rsi
cmpq %r13, (%rsi,%rdx,8)
jae 0x12980e
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %r9
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
pushq %r13
pushq %rax
pushq %r14
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 0x12ea2c
addq $0x60, %rsp
movq %rax, %r14
jmp 0x1298b5
movq 0x40(%rsp), %rdx
testq %rdx, %rdx
je 0x129822
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %r10
jmp 0x12983d
movq 0x38(%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 %r13, %rdx
subq %rax, %rdx
movq %r15, %rdi
movq %rax, %rsi
movq %r14, %rcx
movq 0x70(%rsp), %r8
movq %rbx, %r9
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movsd 0x10(%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 0x414f8
addq $0x60, %rsp
addq %rax, %r14
movq %r13, %rax
incq %r13
cmpq %r12, %r13
jbe 0x1296cc
jmp 0x1298c9
xorl %r14d, %r14d
movq %r14, 0x8(%r15)
movq 0x10(%rbx), %rdi
movq 0x178(%rsp), %r14
cmpq %r14, %rdi
jbe 0x129a6c
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x20(%rsp)
movq (%rbx), %rsi
callq 0x121388
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x129965
movq (%rbx), %rcx
movq 0x8(%rsp), %rdx
decl %edx
addq $0x4, %rcx
movl (%rcx), %edi
movl 0x4(%rcx), %r8d
testl %edi, %edi
je 0x129959
movl $0x3, %esi
cmpl %edx, %r8d
jb 0x12992d
xorl %esi, %esi
testl %edi, %edi
setne %sil
movslq 0x80(%rsp,%rsi,4), %rdi
incq %rdi
movl %edi, 0x80(%rsp,%rsi,4)
cmpq %r14, %rdi
jne 0x12994b
movl 0x20(%rcx), %edi
movl %edi, 0x20(%rsp,%rsi,4)
addq $0xa8, %rcx
decq %rax
jne 0x129911
jmp 0x129965
movl $0x2, %esi
cmpl %edx, %r8d
jne 0x129925
jmp 0x12992d
cmpl $0x0, 0x20(%rsp)
jg 0x129985
cmpl $0x0, 0x24(%rsp)
jg 0x129985
cmpl $0x0, 0x28(%rsp)
jg 0x129985
cmpl $0x0, 0x2c(%rsp)
jle 0x129a6c
cmpq $0x0, 0x10(%rbx)
je 0x129a66
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %rax
movl 0x4(%rax,%r13), %edx
movl 0x8(%rax,%r13), %esi
testl %edx, %edx
je 0x129a52
movl $0x3, %ecx
cmpl 0x8(%rsp), %esi
jb 0x1299cb
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
movl 0x24(%rax,%r13), %edx
cmpl 0x20(%rsp,%rcx,4), %edx
jge 0x1299de
cmpl $0x1, 0xc(%rax,%r13)
jne 0x129a3c
cmpq %rbp, %r14
je 0x129a39
leaq (%rax,%r13), %rcx
movq %rcx, 0x10(%rsp)
imulq $0xa8, %rbp, %r12
addq %rax, %r12
movl $0xa8, %edx
movq %rbx, %r15
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x2160
movq (%r15), %rdi
addq %r13, %rdi
movl $0xa8, %edx
movq %rbx, %rsi
movq %r15, %rbx
callq 0x2160
incq %rbp
incq %r14
addq $0xa8, %r13
cmpq 0x10(%rbx), %r14
jb 0x1299a4
jmp 0x129a68
movl $0x2, %ecx
cmpl 0x8(%rsp), %esi
jne 0x1299c4
jmp 0x1299cb
xorl %ebp, %ebp
movq %rbp, 0x10(%rbx)
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
callq 0x120acf
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq