// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: lchain_qgen_mcopy(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, long, unsigned int, double, unsigned int, unsigned int, st_mt_t*)
void lchain_qgen_mcopy(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, int64_t quick_check, 
				uint32_t gen_off, double mcopy_rate, uint32_t chain_cutoff, 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, ol0, lch; overlap_region *r, t; ///srt = 0
	clear_overlap_region_alloc(ol);

	for (l = 0, k = 1, m = 0, lch = 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; ol0 = ol->length;
				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, 1, mcopy_rate, mcopy_khit_cut, 1);
				if((chain_cutoff >= 2) && (!lch)) {
					for (i = ol0; (i<ol->length) && (!lch); i++) {
						if(ol->list[i].align_length < chain_cutoff) lch = 1;
					}
				}
			}
			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]; 
		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); 
		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, lch = 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]) {
					if (k != i) {
						t = ol->list[k];
						ol->list[k] = ol->list[i];
						ol->list[i] = t;
					}
					if(ol->list[k].align_length < chain_cutoff) lch = 1;
					++k;
				}
			}
			ol->length = k;
		}
	} 
	
	ks_introsort_or_xs(ol->length, ol->list);
	if(lch) {
		//@brief r485
		uint64_t zs, ze, rs, re, ob, os, oe, ocn, pp, kn, ms, me; int64_t osc;
		for (i = l = 0, cn = cl->length; i < ol->length; ++i) {
			if(ol->list[i].align_length < chain_cutoff) {
				zs = ol->list[i].x_pos_s; ze = ol->list[i].x_pos_e + 1;
				ob = (ze - zs)*OFL; if(ob < 16) ob = 16;
				osc = ol->list[i].shared_seed*CH_SC;
				ocn = ol->list[i].align_length<<CH_OCC;
				for (k = 0; (k < ol->length) && (ze > ol->list[k].x_pos_s); k++) {
					if(ol->list[k].align_length < chain_cutoff) continue;
					if(ol->list[k].align_length < ocn) continue;
					if(ol->list[k].shared_seed < osc) continue;
					rs = ol->list[k].x_pos_s; re = ol->list[k].x_pos_e + 1;
					os = ((rs>=zs)?rs:zs); oe = ((re<=ze)?re:ze);
					if((oe > os) && (oe - os) >= ob) {
						m = ol->list[k].non_homopolymer_errors;
						pp = cl->list[m].readID; kn = 0;
						for (; (m < cn) && (cl->list[m].readID == pp) && (kn < ocn); m++) {
							me = cl->list[m].self_offset; ms = me - (cl->list[m].cnt&(0xffu)); 
							if((ms >= os) && (me <= oe)) kn++;
						}
						if(kn >= ocn) break;
					}
				}
				if((k < ol->length) && (ze > ol->list[k].x_pos_s)) continue;
			}
			if (l != i) {
				t = ol->list[l];
				ol->list[l] = ol->list[i];
				ol->list[i] = t;
			}
			l++;
		}
		// fprintf(stderr, "+[M::%s] rid::%u, ol->length0::%lu, ol->length1::%lu\n", __func__, rid, ol->length, l);
		ol->length = l;


		/**
		//@brief r484
		for (i = sp->n = 0; i < ol->length; ++i) {
			if(ol->list[i].align_length < chain_cutoff) continue;
			os = ol->list[i].x_pos_s; oe = ol->list[i].x_pos_e + 1;
			if((sp->n) && (((uint32_t)sp->a[sp->n-1]) >= os)) {
				if(oe > ((uint32_t)sp->a[sp->n-1])) {
					oe = oe - ((uint32_t)sp->a[sp->n-1]);
					sp->a[sp->n-1] += oe; 
				}
			} else {
				os = (os<<32)|oe; kv_push(uint64_t, *sp, os);
			}
		}

		for (i = k = 0; i < ol->length; ++i) {
			if(ol->list[i].align_length < chain_cutoff) {///ol has been sorted by x_pos_s
				r = &(ol->list[i]); rs = r->x_pos_s; re = r->x_pos_e + 1;
				rl = re - rs; ovl = 0;
				for (m = 0; (m < sp->n) && (re > (sp->a[m]>>32)); m++) {
					os = ((rs>=(sp->a[m]>>32))? rs:(sp->a[m]>>32));
            		oe = ((re<=((uint32_t)sp->a[m]))? re:((uint32_t)sp->a[m]));
					if(oe > os) {
						ovl += (oe - os); if(ovl >= (rl*0.95)) break;
					}
				}
				if(ovl >= (rl*0.95)) continue;
			}
			if (k != i) {
				t = ol->list[k];
				ol->list[k] = ol->list[i];
				ol->list[i] = t;
			}
			ol->list[k++].align_length = 0;
		}
		// fprintf(stderr, "+[M::%s] ol->length0::%lu, ol->length1::%lu\n", __func__, ol->length, k);
		ol->length = k;
		**/
	} 
	/**else {
		for (i = 0; i < ol->length; ++i) ol->list[i].align_length = 0;
	}
	**/
	for (i = 0; i < ol->length; ++i) ol->list[i].align_length = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movsd %xmm3, 0x10(%rsp)
movsd %xmm2, (%rsp)
movq %xmm1, 0x48(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, %r15
movq %rcx, 0x28(%rsp)
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %r12
movq %rsi, %rdi
callq 0x11935
movq %r12, %rdi
movq %r14, 0x20(%rsp)
leaq 0x18(%r14), %rax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %r13
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %esi, %esi
movq %r15, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
cmpq %rdi, %r13
ja 0xe8fb0
movq 0x20(%rsp), %rax
movq (%rax), %rcx
jne 0xe8e65
movq %rsi, %rax
shlq $0x4, %rax
movl (%rcx,%rax), %eax
jmp 0xe8e87
movq %r13, %rdx
shlq $0x4, %rdx
movq %rsi, %rax
shlq $0x4, %rax
movl (%rcx,%rax), %eax
movl (%rcx,%rdx), %ecx
xorl %eax, %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0xe8fa8
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %ebp, %eax
jne 0xe8e98
movq %r13, %rsi
jmp 0xe8fa8
movl %eax, %eax
testq %r15, %r15
je 0xe8ea9
movq 0x18(%r15), %rcx
movq (%rcx,%rax,8), %rax
jmp 0xe8ec1
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
movl (%rcx,%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r13, %rdx
subq %rsi, %rdx
movq 0x10(%rbx), %r15
movq 0x20(%rsp), %rdi
movq %r12, %rcx
movq 0x40(%rsp), %r8
movq %rbx, %r9
movsd 0x30(%rsp), %xmm0
movq 0x48(%rsp), %xmm1
movsd (%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
pushq $0x1
popq %r11
pushq %r11
movl 0x1a8(%rsp), %r10d
pushq %r10
pushq %r11
movl 0x1a8(%rsp), %r10d
pushq %r10
movl 0x180(%rsp), %r10d
pushq %r10
pushq 0x1b0(%rsp)
pushq %rax
pushq 0x60(%rsp)
pushq %rbp
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
callq 0x14f8d
addq $0x60, %rsp
addq %rax, %r12
movl 0x198(%rsp), %esi
cmpl $0x2, %esi
jb 0xe8f9b
testq %r14, %r14
movq 0x8(%rsp), %rdi
jne 0xe8f91
movq 0x10(%rbx), %rax
imulq $0xa8, %r15, %rcx
addq $0x28, %rcx
xorl %r14d, %r14d
cmpq %rax, %r15
jae 0xe8f91
testq %r14, %r14
jne 0xe8f91
movq (%rbx), %rdx
xorl %r14d, %r14d
cmpl %esi, (%rdx,%rcx)
setb %r14b
incq %r15
addq $0xa8, %rcx
jmp 0xe8f6e
movq %r13, %rsi
movq 0x18(%rsp), %r15
jmp 0xe8fa8
movq %r13, %rsi
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rdi
incq %r13
jmp 0xe8e46
movq 0x20(%rsp), %rax
movq %r12, 0x8(%rax)
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
movq 0x168(%rsp), %r13
cmpq %r13, %rdi
movq %rbx, 0x8(%rsp)
jbe 0xe913f
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x50(%rsp)
callq 0xe248f
movq 0x10(%rbx), %r15
xorl %ebx, %ebx
movq %r15, (%rsp)
subq $0x1, %r15
jb 0xe9033
movq 0x8(%rsp), %rax
movq (%rax), %r12
leaq (%r12,%rbx), %rdi
movq 0x28(%rsp), %rsi
callq 0xe3aee
movl %eax, %eax
movslq 0x70(%rsp,%rax,4), %rcx
incq %rcx
movl %ecx, 0x70(%rsp,%rax,4)
cmpq %r13, %rcx
jne 0xe902a
movl 0x24(%r12,%rbx), %ecx
movl %ecx, 0x50(%rsp,%rax,4)
addq $0xa8, %rbx
jmp 0xe8ff2
movdqa 0x50(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xe9133
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %edi, %edi
xorl %edx, %edx
movq 0x8(%rsp), %rbx
cmpq (%rsp), %rdx
jae 0xe912d
movq %rdx, 0x10(%rsp)
movq %rdi, %r12
movq (%rbx), %rbp
leaq (%r15,%rbp), %r13
movq %r13, %rdi
movq 0x28(%rsp), %rsi
callq 0xe3aee
movl 0x24(%rbp,%r15), %ecx
movl %eax, %eax
cmpl 0x50(%rsp,%rax,4), %ecx
jge 0xe909b
movq %r12, %rdi
movq 0x10(%rsp), %rdx
jmp 0xe911e
movq %r12, %rdi
movq 0x10(%rsp), %rdx
cmpq %r12, %rdx
je 0xe9102
imulq $0xa8, %rdi, %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 (%rbx), %rdi
addq %r15, %rdi
movl $0xa8, %edx
leaq 0x80(%rsp), %rsi
callq 0x2160
movq 0x10(%rsp), %rdx
movq %r12, %rdi
movq (%rbx), %rbp
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movl 0x198(%rsp), %ecx
imulq $0xa8, %rdi, %rax
cmpl %ecx, 0x28(%rbp,%rax)
pushq $0x1
popq %rax
cmovbq %rax, %r14
incq %rdi
incq %rdx
addq $0xa8, %r15
jmp 0xe905b
movq %rdi, 0x10(%rbx)
jmp 0xe913c
movq (%rsp), %rdi
movq 0x8(%rsp), %rbx
movq (%rbx), %rsi
callq 0xe1c6f
testq %r14, %r14
je 0xe9397
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movl 0x198(%rsp), %eax
movq 0x10(%rbx), %rcx
movq %rcx, (%rsp)
cmpq %rcx, %r12
jae 0xe9391
movq (%rbx), %r13
imulq $0xa8, %r12, %rcx
leaq (%rcx,%r13), %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movl 0x28(%r13,%rcx), %ecx
cmpl %eax, %ecx
jae 0xe9338
movq 0x30(%rsp), %r8
movl 0x4(%r8), %eax
movl 0x8(%r8), %edx
incl %edx
movq %rdx, %rsi
movq %rax, 0x48(%rsp)
subq %rax, %rsi
movq %rsi, %xmm0
punpckldq 0xfa0ab(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xfa0b3(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xfa307(%rip), %xmm1    # 0x1e34e8
cvttsd2si %xmm1, %rsi
movq %rsi, %rdi
sarq $0x3f, %rdi
subsd 0xfa0a3(%rip), %xmm1    # 0x1e3298
cvttsd2si %xmm1, %r9
andq %rdi, %r9
orq %rsi, %r9
cmpq $0x11, %r9
pushq $0x10
popq %rax
cmovbq %rax, %r9
movq %r9, 0x18(%rsp)
movl 0x24(%r8), %eax
shll $0x4, %eax
movl %eax, 0x10(%rsp)
shll $0x4, %ecx
movl 0x198(%rsp), %eax
cmpl %ecx, %eax
movl %ecx, %r9d
cmoval %eax, %r9d
movq (%rsp), %rax
xorl %r10d, %r10d
movq %r14, 0x38(%rsp)
movq %r12, 0x68(%rsp)
cmpq %rax, %r10
je 0xe9338
imulq $0xa8, %r10, %rdi
movl 0x4(%r13,%rdi), %r11d
cmpl %r11d, %edx
jbe 0xe9338
addq %r13, %rdi
cmpl %r9d, 0x28(%rdi)
jb 0xe932b
movl 0x10(%rsp), %esi
cmpl %esi, 0x24(%rdi)
jl 0xe932b
movl 0x8(%rdi), %ebp
incl %ebp
movq 0x48(%rsp), %rsi
cmpl %esi, %r11d
cmoval %r11d, %esi
cmpl %edx, %ebp
cmovael %edx, %ebp
cmpl %esi, %ebp
jbe 0xe932b
movq %rbp, %r8
subq %rsi, %r8
cmpq 0x18(%rsp), %r8
jb 0xe932b
movl 0x30(%rdi), %ebx
movq 0x20(%rsp), %rax
movq (%rax), %rdi
movq %rbx, %r8
shlq $0x4, %r8
leaq (%rdi,%r8), %r12
addq $0xc, %r12
movl -0xc(%r12), %edi
xorl %r8d, %r8d
cmpq 0x28(%rsp), %rbx
jae 0xe9313
movl -0xc(%r12), %r14d
xorl %edi, %r14d
testl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
jne 0xe9313
cmpq %rcx, %r8
jae 0xe9313
movl -0x4(%r12), %eax
movzbl (%r12), %r15d
movq %rax, %r14
subq %r15, %r14
cmpq %rsi, %r14
setae %r14b
cmpl %ebp, %eax
setbe %al
andb %r14b, %al
movzbl %al, %eax
addq %rax, %r8
incq %rbx
addq $0x10, %r12
jmp 0xe92c8
cmpq %rcx, %r8
movq 0x8(%rsp), %rbx
movq 0x38(%rsp), %r14
movq 0x68(%rsp), %r12
movq (%rsp), %rax
jae 0xe9333
incq %r10
jmp 0xe923f
cmpl %r11d, %edx
ja 0xe9389
cmpq %r12, %r14
je 0xe9386
imulq $0xa8, %r14, %rax
addq %rax, %r13
movl $0xa8, %edx
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq 0x30(%rsp), %rsi
callq 0x2160
movq 0x40(%rsp), %rdi
addq (%rbx), %rdi
movl $0xa8, %edx
movq %r15, %rsi
callq 0x2160
incq %r14
incq %r12
jmp 0xe9161
movq %r14, 0x10(%rbx)
jmp 0xe939b
movq 0x10(%rbx), %r14
pushq $0x28
popq %rax
subq $0x1, %r14
jb 0xe93b3
movq (%rbx), %rcx
andl $0x0, (%rcx,%rax)
addq $0xa8, %rax
jmp 0xe939e
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq