// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq 0x50(%rbp), %rax
movl 0x48(%rbp), %eax
movl 0x40(%rbp), %eax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movl %edx, -0x3c(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x68(%rbp)
movsd %xmm2, -0x70(%rbp)
movsd %xmm3, -0x78(%rbp)
movsd %xmm4, -0x80(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1e120
movq $0x0, -0x98(%rbp)
movq $0x1, -0x90(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0x90(%rbp), %rax
cmpq -0xa8(%rbp), %rax
ja 0x1ac78c
movq -0x90(%rbp), %rax
cmpq -0xa8(%rbp), %rax
je 0x1ac3ce
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x98(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
je 0x1ac773
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x3c(%rbp), %eax
je 0x1ac765
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xb0(%rbp)
movq -0xb8(%rbp), %rcx
movq 0x50(%rbp), %rdx
xorl %eax, %eax
cmpq (%rdx), %rcx
movb %al, -0x199(%rbp)
jae 0x1ac453
movq 0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xb8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x98(%rbp), %rax
setb %al
movb %al, -0x199(%rbp)
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x1ac45f
jmp 0x1ac475
jmp 0x1ac461
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x1ac418
movq -0xb8(%rbp), %rax
movq 0x50(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1ac4a5
movq 0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xb8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x90(%rbp), %rax
jb 0x1ac67f
movq -0x30(%rbp), %rax
movq %rax, -0x210(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x208(%rbp)
movq -0x90(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x30(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x1f0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq 0x30(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x1c8(%rbp)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x1b8(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x1ac(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x1ac57a
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x218(%rbp)
jmp 0x1ac5a0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0xb0(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x218(%rbp)
movl -0x1ac(%rbp), %r12d
movsd -0x1b8(%rbp), %xmm2
movsd -0x1c0(%rbp), %xmm1
movsd -0x1c8(%rbp), %xmm0
movq -0x1d0(%rbp), %r13
movq -0x1e0(%rbp), %r15
movq -0x1e8(%rbp), %r9
movq -0x1f0(%rbp), %r8
movq -0x200(%rbp), %rdx
movq -0x208(%rbp), %rsi
movq -0x210(%rbp), %rdi
movq -0x218(%rbp), %r14
movq 0x38(%rbp), %rbx
movl 0x10(%rbp), %r11d
movl 0x40(%rbp), %eax
movl %eax, %r10d
movsd -0x80(%rbp), %xmm3
movl 0x48(%rbp), %eax
xorl %ecx, %ecx
movq -0x1f8(%rbp), %rcx
movq %r15, (%rsp)
movq -0x1d8(%rbp), %r15
movq %r15, 0x8(%rsp)
movq -0x1a8(%rbp), %r15
movq %r13, 0x10(%rsp)
movl %r12d, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movl %r11d, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
callq 0x25550
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x1ac763
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x3c(%rbp), %edx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movl 0x10(%rbp), %eax
movq 0x20(%rbp), %r10
movq %r10, -0x238(%rbp)
movq 0x28(%rbp), %r10
movq %r10, -0x230(%rbp)
movq 0x30(%rbp), %r10
movq %r10, -0x228(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
movsd -0x78(%rbp), %xmm2
movq 0x38(%rbp), %r13
movl 0x40(%rbp), %r12d
movsd -0x80(%rbp), %xmm3
movl 0x48(%rbp), %r15d
movq 0x50(%rbp), %r14
movq -0xa0(%rbp), %r11
movq -0x98(%rbp), %r10
movq -0x90(%rbp), %rbx
movq %rbx, -0x220(%rbp)
leaq -0xb8(%rbp), %rbx
movl %eax, (%rsp)
movq -0x238(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x230(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x228(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0x220(%rbp), %rax
movq %r13, 0x20(%rsp)
movl %r12d, 0x28(%rsp)
movl %r15d, 0x30(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
movq %r11, 0x48(%rsp)
movq %r10, 0x50(%rsp)
movq %rax, 0x58(%rsp)
callq 0x1b4930
movq %rax, -0xa0(%rbp)
jmp 0x1ac765
movq -0x90(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x1ac775
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x1ac369
movq -0xa0(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
cmpq 0x18(%rbp), %rax
jbe 0x1aca54
movl $0x0, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
movl $0x0, -0xdc(%rbp)
movl $0x0, -0xe0(%rbp)
movl $0x0, -0xe4(%rbp)
movl $0x0, -0xe8(%rbp)
movl $0x0, -0xec(%rbp)
movl $0x0, -0xf0(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
callq 0x19e350
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x1ac8d2
movq -0x38(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x88(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %esi
callq 0x1a08b0
movl %eax, -0xc4(%rbp)
movslq -0xc4(%rbp), %rax
movl -0xe0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0xe0(%rbp,%rax,4)
movslq -0xc4(%rbp), %rax
movslq -0xe0(%rbp,%rax,4), %rax
cmpq 0x18(%rbp), %rax
jne 0x1ac8b9
movq -0xc0(%rbp), %rax
movl 0x24(%rax), %ecx
movslq -0xc4(%rbp), %rax
movl %ecx, -0xf0(%rbp,%rax,4)
jmp 0x1ac8bb
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x1ac82b
cmpl $0x0, -0xf0(%rbp)
jg 0x1ac8fa
cmpl $0x0, -0xec(%rbp)
jg 0x1ac8fa
cmpl $0x0, -0xe8(%rbp)
jg 0x1ac8fa
cmpl $0x0, -0xe4(%rbp)
jle 0x1aca52
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x88(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x1aca43
movq -0x38(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x88(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %esi
callq 0x1a08b0
movl %eax, -0xc4(%rbp)
movq -0xc0(%rbp), %rax
movl 0x24(%rax), %eax
movslq -0xc4(%rbp), %rcx
cmpl -0xf0(%rbp,%rcx,4), %eax
jge 0x1ac984
movq -0xc0(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x1aca2a
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
je 0x1aca18
movq -0x38(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x90(%rbp), %rax
addq %rax, %rsi
leaq -0x198(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x38(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x88(%rbp), %rax
addq %rax, %rsi
movq -0x38(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x90(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x38(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x88(%rbp), %rax
addq %rax, %rdi
leaq -0x198(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x1aca2c
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x1ac910
movq -0x90(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1aca54
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
callq 0x19d6c0
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)