// File: /workspace/github_cpp_makefile_star2_O3/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
movq %xmm3, 0x58(%rsp)
movq %xmm2, 0x28(%rsp)
movq %xmm1, 0x50(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x30(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r13
movq 0x8(%rdi), %r14
movq %rsi, %rdi
callq 0x3d018
testq %r14, %r14
movq %r13, 0x18(%rsp)
je 0x12e5f3
leaq 0x18(%r13), %rax
movq %rax, 0x38(%rsp)
xorl %esi, %esi
movl $0x1, %r12d
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
movq (%rdi), %rcx
cmpq %r14, %r12
jne 0x12e481
movq %rsi, %rax
shlq $0x4, %rax
movl (%rcx,%rax), %eax
jmp 0x12e49f
movq %r12, %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 0x12e4c9
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %ebp, %eax
jne 0x12e4b3
movq %r15, %rax
movq %r12, %rsi
jmp 0x12e5d4
movl %eax, %eax
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0x12e4d1
movq 0x18(%rcx), %rcx
movq (%rcx,%rax,8), %rax
jmp 0x12e4e9
movq %r15, %rax
jmp 0x12e5d4
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 %r12, %rdx
subq %rsi, %rdx
movq 0x10(%rbx), %r13
movq %r15, %rcx
movq 0x38(%rsp), %r8
movq %rbx, %r9
movsd 0x48(%rsp), %xmm0
movq 0x50(%rsp), %xmm1
movq 0x28(%rsp), %xmm2
movq 0x58(%rsp), %xmm3
movl $0x1, %r11d
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 0x48(%rsp)
pushq %rbp
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
pushq 0x1c8(%rsp)
callq 0x40ceb
addq $0x60, %rsp
addq %r15, %rax
movl 0x198(%rsp), %r8d
cmpl $0x2, %r8d
jb 0x12e5cc
cmpq $0x0, 0x8(%rsp)
jne 0x12e5cc
movq 0x10(%rbx), %rcx
cmpq %rcx, %r13
movq 0x18(%rsp), %rdi
jae 0x12e5e5
movq (%rbx), %rdx
imulq $0xa8, %r13, %rsi
incq %r13
addq %rsi, %rdx
addq $0x28, %rdx
movl (%rdx), %esi
cmpq %rcx, %r13
jae 0x12e5ba
incq %r13
addq $0xa8, %rdx
cmpl %r8d, %esi
jae 0x12e5a4
xorl %ecx, %ecx
cmpl %r8d, %esi
setb %cl
movq %rcx, 0x8(%rsp)
jmp 0x12e4ab
movq %r12, %rsi
movq 0x18(%rsp), %rdi
incq %r12
movq %rax, %r15
cmpq %r14, %r12
jbe 0x12e46d
jmp 0x12e601
movq $0x0, 0x8(%rsp)
jmp 0x12e4ab
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq %r13, %rdi
movq %rax, 0x8(%rdi)
movq (%rbx), %rsi
movq 0x10(%rbx), %r12
movq 0x168(%rsp), %r14
cmpq %r14, %r12
jbe 0x12e6b1
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq %r12, %rdi
callq 0x127252
movq 0x10(%rbx), %rax
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x12e7d9
movq (%rbx), %rcx
movq 0x10(%rsp), %rdx
decl %edx
addq $0x4, %rcx
movq %rax, %rsi
movl 0x198(%rsp), %r10d
movl (%rcx), %r8d
movl 0x4(%rcx), %r9d
testl %r8d, %r8d
je 0x12e6a5
movl $0x3, %edi
cmpl %edx, %r9d
jb 0x12e67c
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movslq 0x70(%rsp,%rdi,4), %r8
incq %r8
movl %r8d, 0x70(%rsp,%rdi,4)
cmpq %r14, %r8
jne 0x12e697
movl 0x20(%rcx), %r8d
movl %r8d, 0x60(%rsp,%rdi,4)
addq $0xa8, %rcx
decq %rsi
jne 0x12e65d
jmp 0x12e6bb
movl $0x2, %edi
cmpl %edx, %r9d
jne 0x12e673
jmp 0x12e67c
movq 0x8(%rsp), %r15
jmp 0x12e7e4
movdqa 0x60(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x12e7de
movq 0x10(%rsp), %rcx
decl %ecx
movq %rcx, 0x10(%rsp)
movq (%rbx), %rcx
xorl %r14d, %r14d
movl $0x1, %r8d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
movl 0x4(%rcx,%r14), %esi
movl 0x8(%rcx,%r14), %edi
testl %esi, %esi
je 0x12e7bf
movl $0x3, %edx
cmpl 0x10(%rsp), %edi
jb 0x12e719
xorl %edx, %edx
testl %esi, %esi
setne %dl
movl 0x24(%rcx,%r14), %esi
cmpl 0x60(%rsp,%rdx,4), %esi
jl 0x12e7aa
cmpq %r12, %r13
je 0x12e797
movq %r15, 0x8(%rsp)
leaq (%rcx,%r14), %r15
imulq $0xa8, %r12, %rbp
addq %rcx, %rbp
movl $0xa8, %edx
leaq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
movl $0xa8, %edx
movq %rbp, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %r15
callq 0x2160
movq (%rbx), %rdi
addq %r14, %rdi
movl $0xa8, %edx
leaq 0x80(%rsp), %rsi
callq 0x2160
movl $0x1, %r8d
movl 0x198(%rsp), %r10d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
imulq $0xa8, %r12, %rdx
cmpl %r10d, 0x28(%rcx,%rdx)
cmovbq %r8, %r15
incq %r12
incq %r13
addq $0xa8, %r14
cmpq %rax, %r13
jb 0x12e6f5
jmp 0x12e7d3
movl $0x2, %edx
cmpl 0x10(%rsp), %edi
jne 0x12e712
jmp 0x12e719
movq %r12, 0x10(%rbx)
jmp 0x12e7e1
xorl %r12d, %r12d
jmp 0x12e7e1
movq %rax, %r12
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x126994
movq 0x10(%rbx), %rdi
testq %r15, %r15
je 0x12ea6f
testq %rdi, %rdi
je 0x12ea69
movq %rdi, %r15
movq 0x18(%rsp), %rax
movq 0x8(%rax), %r12
xorl %ebp, %ebp
xorl %edi, %edi
movl 0x198(%rsp), %edx
movq %rbx, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movq (%rbx), %r13
imulq $0xa8, %rbp, %rcx
leaq (%rcx,%r13), %rsi
movq %rsi, 0x28(%rsp)
movq %rcx, 0x48(%rsp)
movl 0x28(%r13,%rcx), %ecx
cmpl %edx, %ecx
jae 0x12e9ef
movq 0x28(%rsp), %r9
movl 0x4(%r9), %r8d
movl 0x8(%r9), %edx
incl %edx
movq %rdx, %rsi
movq %r8, 0x58(%rsp)
subq %r8, %rsi
movq %rsi, %xmm0
punpckldq 0x102a11(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x102a19(%rip), %xmm0   # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x102d0d(%rip), %xmm1   # 0x231598
cvttsd2si %xmm1, %rsi
movq %rsi, %r8
sarq $0x3f, %r8
subsd 0x102a69(%rip), %xmm1   # 0x231308
cvttsd2si %xmm1, %r10
andq %r8, %r10
orq %rsi, %r10
cmpq $0x11, %r10
movl $0x10, %esi
cmovbq %rsi, %r10
movq %r10, 0x50(%rsp)
movl 0x24(%r9), %esi
shll $0x4, %esi
movl %esi, 0x10(%rsp)
shll $0x4, %ecx
movl 0x198(%rsp), %esi
cmpl %ecx, %esi
movl %ecx, %r9d
cmoval %esi, %r9d
xorl %r10d, %r10d
movq %rbp, 0x20(%rsp)
movq %r15, 0x8(%rsp)
imulq $0xa8, %r10, %r14
movl 0x4(%r13,%r14), %r11d
cmpl %r11d, %edx
jbe 0x12e9ef
addq %r13, %r14
cmpl %r9d, 0x28(%r14)
jb 0x12e90f
movl 0x10(%rsp), %esi
cmpl %esi, 0x24(%r14)
jge 0x12e91c
incq %r10
cmpq %r15, %r10
jne 0x12e8e7
jmp 0x12e9ef
movl 0x8(%r14), %ebx
incl %ebx
movq 0x58(%rsp), %rsi
cmpl %esi, %r11d
cmoval %r11d, %esi
cmpl %edx, %ebx
cmovael %edx, %ebx
cmpl %esi, %ebx
jbe 0x12e90f
movq %rbx, %r15
subq %rsi, %r15
cmpq 0x50(%rsp), %r15
movq 0x8(%rsp), %r15
jb 0x12e90f
movl 0x30(%r14), %r14d
movq %r12, %rbp
subq %r14, %rbp
jbe 0x12e9d3
movq %rdi, 0x38(%rsp)
movq (%rax), %r15
shlq $0x4, %r14
addq %r15, %r14
addq $0xc, %r14
movl -0xc(%r14), %r15d
xorl %r12d, %r12d
movl -0xc(%r14), %edi
xorl %r15d, %edi
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jne 0x12e9af
cmpq %rcx, %r12
jae 0x12e9af
movl -0x4(%r14), %eax
movzbl (%r14), %r8d
movq %rax, %rdi
subq %r8, %rdi
cmpq %rsi, %rdi
setae %dil
cmpl %ebx, %eax
setbe %al
andb %dil, %al
movzbl %al, %eax
addq %rax, %r12
addq $0x10, %r14
decq %rbp
jne 0x12e96f
cmpq %rcx, %r12
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %r12
movq 0x20(%rsp), %rbp
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %r15
jb 0x12e90f
jmp 0x12e9e3
testq %rcx, %rcx
movq 0x20(%rsp), %rbp
jne 0x12e90f
jmp 0x12e9e8
cmpl %r11d, %edx
jbe 0x12e9ef
movq 0x40(%rsp), %rbx
jmp 0x12ea54
cmpq %rbp, %rdi
movq 0x40(%rsp), %rbx
je 0x12ea51
imulq $0xa8, %rdi, %rax
addq %rax, %r13
movl $0xa8, %edx
movq %rdi, %r14
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq 0x28(%rsp), %rsi
callq 0x2160
movq 0x48(%rsp), %rdi
addq (%rbx), %rdi
movl $0xa8, %edx
movq %r15, %rsi
callq 0x2160
movq %r14, %rdi
movq 0x18(%rsp), %rax
movq 0x10(%rbx), %r15
incq %rdi
incq %rbp
cmpq %r15, %rbp
movl 0x198(%rsp), %edx
jb 0x12e823
jmp 0x12ea6b
xorl %edi, %edi
movq %rdi, 0x10(%rbx)
testq %rdi, %rdi
je 0x12eb7c
leaq 0x3(%rdi), %rcx
andq $-0x4, %rcx
decq %rdi
movq %rdi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movl $0x220, %eax            # imm = 0x220
addq (%rbx), %rax
movdqa 0x102823(%rip), %xmm1   # 0x2312c0
movdqa 0x10282b(%rip), %xmm2   # 0x2312d0
movdqa 0x102833(%rip), %xmm3   # 0x2312e0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x102a73(%rip), %xmm5   # 0x231530
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x12eafb
movl $0x0, -0x1f8(%rax)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x12eb13
movl $0x0, -0x150(%rax)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0x12eb54
movl $0x0, -0xa8(%rax)
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x12eb64
movl $0x0, (%rax)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x2a0, %rax            # imm = 0x2A0
addq $-0x4, %rcx
jne 0x12eabd
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq