// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movsd %xmm3, 0x58(%rsp)
movsd %xmm2, 0x50(%rsp)
movsd %xmm1, 0x48(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r9, 0x30(%rsp)
movq %r8, %r14
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, %rbp
movq 0x8(%rdi), %rbx
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x3db37
testq %rbx, %rbx
je 0x128699
movq %rbx, %rdi
leaq 0x18(%rbp), %rax
movq %rax, 0x38(%rsp)
movl $0x1, %r13d
xorl %esi, %esi
movl $0x1, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rsp), %r9
movq %rbp, %r8
movq %r14, 0x28(%rsp)
movq 0x10(%rsp), %r10
movq %rdi, 0x20(%rsp)
cmpq %rdi, %rbx
je 0x12852d
movq (%r8), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
movq %rsi, %rdx
shlq $0x4, %rdx
movl (%rax,%rdx), %edx
xorl (%rax,%rcx), %edx
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
je 0x12855e
movq (%r8), %rax
movq %rsi, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r10d, %eax
jne 0x12854f
movq %r15, %rax
movq %rbx, %rsi
jmp 0x128688
testq %r14, %r14
je 0x128566
movq 0x18(%r14), %rcx
movq (%rcx,%rax,8), %rax
jmp 0x12857e
movq %r15, %rax
jmp 0x128688
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdx
subq %rsi, %rdx
movq 0x10(%r9), %rbp
movq %r8, %r14
movq %r8, %rdi
movq %r15, %rcx
movq 0x38(%rsp), %r8
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movsd 0x58(%rsp), %xmm3
pushq %r13
movl 0x1b8(%rsp), %r11d
pushq %r11
pushq %r13
movl 0x1b8(%rsp), %r11d
pushq %r11
movl 0x190(%rsp), %r11d
pushq %r11
pushq 0x1c0(%rsp)
pushq %rax
pushq 0x50(%rsp)
pushq %r10
pushq 0x1d8(%rsp)
pushq 0x1d8(%rsp)
pushq 0x1d8(%rsp)
callq 0x414f8
addq $0x60, %rsp
addq %r15, %rax
movl 0x1a8(%rsp), %edi
cmpl $0x2, %edi
jb 0x128661
testq %r12, %r12
jne 0x128661
movq 0x8(%rsp), %r9
movq 0x10(%r9), %rcx
cmpq %rcx, %rbp
movq %r14, %r8
movq 0x10(%rsp), %r10
jae 0x128678
movq (%r9), %rdx
imulq $0xa8, %rbp, %rsi
incq %rbp
addq %rsi, %rdx
addq $0x28, %rdx
xorl %r12d, %r12d
movq 0x28(%rsp), %r14
cmpl %edi, (%rdx)
cmovbq %r13, %r12
cmpq %rcx, %rbp
jae 0x12865c
incq %rbp
addq $0xa8, %rdx
testq %r12, %r12
je 0x128642
movq %rbx, %rsi
jmp 0x128683
movq %rbx, %rsi
movq 0x8(%rsp), %r9
movq %r14, %r8
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %r10
jmp 0x128683
xorl %r12d, %r12d
movq %rbx, %rsi
movq 0x28(%rsp), %r14
movq 0x20(%rsp), %rdi
incq %rbx
movq %rax, %r15
cmpq %rdi, %rbx
jbe 0x128509
jmp 0x1286a6
xorl %r12d, %r12d
xorl %eax, %eax
movq 0x8(%rsp), %r9
movq %rbp, %r8
movq %rax, 0x8(%r8)
movq 0x10(%r9), %rdi
movq 0x178(%rsp), %rbx
cmpq %rbx, %rdi
movq %r8, 0x78(%rsp)
jbe 0x12887c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x60(%rsp)
movq (%r9), %rsi
callq 0x121388
movq 0x8(%rsp), %r9
movq 0x10(%r9), %rax
testq %rax, %rax
movl 0x1a8(%rsp), %r15d
je 0x128757
movq (%r9), %rcx
movq 0x18(%rsp), %rdx
decl %edx
addq $0x4, %rcx
movl (%rcx), %edi
movl 0x4(%rcx), %r8d
testl %edi, %edi
je 0x12874b
movl $0x3, %esi
cmpl %edx, %r8d
jb 0x12871f
xorl %esi, %esi
testl %edi, %edi
setne %sil
movslq 0x80(%rsp,%rsi,4), %rdi
incq %rdi
movl %edi, 0x80(%rsp,%rsi,4)
cmpq %rbx, %rdi
jne 0x12873d
movl 0x20(%rcx), %edi
movl %edi, 0x60(%rsp,%rsi,4)
addq $0xa8, %rcx
decq %rax
jne 0x128703
jmp 0x128757
movl $0x2, %esi
cmpl %edx, %r8d
jne 0x128717
jmp 0x12871f
cmpl $0x0, 0x60(%rsp)
jg 0x128777
cmpl $0x0, 0x64(%rsp)
jg 0x128777
cmpl $0x0, 0x68(%rsp)
jg 0x128777
cmpl $0x0, 0x6c(%rsp)
jle 0x128884
cmpq $0x0, 0x10(%r9)
je 0x128872
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movl $0x1, %edi
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq (%r9), %rax
movl 0x4(%rax,%rbx), %edx
movl 0x8(%rax,%rbx), %esi
testl %edx, %edx
je 0x12885e
movl $0x3, %ecx
cmpl 0x18(%rsp), %esi
jb 0x1287c3
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
movl 0x24(%rax,%rbx), %edx
cmpl 0x60(%rsp,%rcx,4), %edx
jl 0x128848
cmpq %r14, %r15
je 0x12882c
leaq (%rax,%rbx), %rbp
imulq $0xa8, %r14, %r13
addq %rax, %r13
movl $0xa8, %edx
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movl $0xa8, %edx
leaq 0x90(%rsp), %rsi
callq 0x2160
movl $0x1, %edi
movq 0x8(%rsp), %r9
movq (%r9), %rax
imulq $0xa8, %r14, %rcx
movl 0x1a8(%rsp), %edx
cmpl %edx, 0x28(%rax,%rcx)
cmovbq %rdi, %r12
incq %r14
incq %r15
addq $0xa8, %rbx
cmpq 0x10(%r9), %r15
jb 0x12879e
jmp 0x128878
movl $0x2, %ecx
cmpl 0x18(%rsp), %esi
jne 0x1287bc
jmp 0x1287c3
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r14, 0x10(%r9)
movl 0x1a8(%rsp), %r15d
movq (%r9), %rsi
movq 0x10(%r9), %rdi
callq 0x120acf
movq 0x8(%rsp), %r8
testq %r12, %r12
je 0x128b4b
movq 0x10(%r8), %rax
testq %rax, %rax
je 0x128b44
movq 0x78(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x30(%rsp)
xorl %r12d, %r12d
xorl %r9d, %r9d
movq (%r8), %r13
imulq $0xa8, %r12, %rcx
leaq (%rcx,%r13), %r11
movq %rcx, 0x48(%rsp)
movl 0x28(%r13,%rcx), %edx
cmpl %r15d, %edx
jae 0x128ad0
movl 0x4(%r11), %edi
movl 0x8(%r11), %ecx
incl %ecx
movq %rcx, %rsi
movq %rdi, 0x50(%rsp)
subq %rdi, %rsi
movq %rsi, %xmm0
punpckldq 0xfc93e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xfc946(%rip), %xmm0    # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xfcb7a(%rip), %xmm1    # 0x225498
cvttsd2si %xmm1, %rsi
movq %rsi, %rdi
subsd 0xfc93a(%rip), %xmm1    # 0x225268
cvttsd2si %xmm1, %r10
sarq $0x3f, %rdi
andq %rdi, %r10
orq %rsi, %r10
cmpq $0x11, %r10
movl $0x10, %esi
cmovbq %rsi, %r10
movq %r10, 0x40(%rsp)
shll $0x4, %edx
cmpl %edx, %r15d
movl %edx, %edi
cmoval %r15d, %edi
cmpq $0x1, %rax
movq %rax, %rsi
adcq $0x0, %rsi
movq %rsi, 0x18(%rsp)
movl 0x4(%r13), %ebp
cmpl %ebp, %ecx
jbe 0x128ab8
movl 0x24(%r11), %esi
shll $0x4, %esi
movl %esi, 0x58(%rsp)
movb $0x1, %bl
xorl %r10d, %r10d
movq %r12, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %r11, 0x38(%rsp)
movl %edi, 0x10(%rsp)
imulq $0xa8, %r10, %rsi
cmpl %edi, 0x28(%r13,%rsi)
jb 0x128a92
addq %r13, %rsi
movl 0x58(%rsp), %r14d
cmpl %r14d, 0x24(%rsi)
jl 0x128a92
movl 0x8(%rsi), %r14d
incl %r14d
movq 0x50(%rsp), %rdi
cmpl %edi, %ebp
cmovbel %edi, %ebp
movl 0x10(%rsp), %edi
cmpl %ecx, %r14d
cmovael %ecx, %r14d
cmpl %ebp, %r14d
jbe 0x128a92
movq %r14, %rdi
subq %rbp, %rdi
cmpq 0x40(%rsp), %rdi
movl 0x10(%rsp), %edi
jb 0x128a92
movl 0x30(%rsi), %edi
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
jbe 0x128a6a
movq 0x78(%rsp), %r8
movq (%r8), %r11
shlq $0x4, %rdi
leaq (%r11,%rdi), %r12
addq $0xc, %r12
movl -0xc(%r12), %edi
xorl %r11d, %r11d
movl -0xc(%r12), %r15d
xorl %edi, %r15d
testl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
jne 0x128a6d
cmpq %rdx, %r11
jae 0x128a6d
movl -0x4(%r12), %r9d
movzbl (%r12), %r8d
movq %r9, %r15
subq %r8, %r15
cmpq %rbp, %r15
setae %r8b
cmpl %r14d, %r9d
setbe %r9b
andb %r8b, %r9b
movzbl %r9b, %r8d
addq %r8, %r11
addq $0x10, %r12
decq %rsi
jne 0x128a21
jmp 0x128a6d
xorl %r11d, %r11d
cmpq %rdx, %r11
movq 0x8(%rsp), %r8
movl 0x1a8(%rsp), %r15d
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r9
movq 0x38(%rsp), %r11
movl 0x10(%rsp), %edi
jae 0x128abd
incq %r10
cmpq 0x18(%rsp), %r10
je 0x128ad0
cmpq %rax, %r10
setb %bl
imulq $0xa8, %r10, %rsi
movl 0x4(%r13,%rsi), %ebp
cmpl %ebp, %ecx
ja 0x12899a
jmp 0x128abd
movb $0x1, %bl
xorl %r10d, %r10d
testb $0x1, %bl
je 0x128ad0
imulq $0xa8, %r10, %rax
cmpl 0x4(%r13,%rax), %ecx
ja 0x128b32
cmpq %r12, %r9
je 0x128b2f
imulq $0xa8, %r9, %rax
addq %rax, %r13
movl $0xa8, %edx
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r9, %rbx
movq %r11, %rbp
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq 0x48(%rsp), %rdi
addq (%rax), %rdi
movl $0xa8, %edx
movq %r14, %rsi
callq 0x2160
movq %rbx, %r9
movq 0x8(%rsp), %r8
incq %r9
incq %r12
movq 0x10(%r8), %rax
cmpq %rax, %r12
jb 0x1288bf
jmp 0x128b47
xorl %r9d, %r9d
movq %r9, 0x10(%r8)
movq 0x10(%r8), %rax
testq %rax, %rax
je 0x128b6d
movq (%r8), %rcx
addq $0x28, %rcx
movl $0x0, (%rcx)
addq $0xa8, %rcx
decq %rax
jne 0x128b5b
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq