// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: lchain_gen(Candidates_list*, overlap_region_alloc*, unsigned int, unsigned long, All_reads*, ul_idx_t const*, unsigned int, overlap_region*, unsigned long, long, long, long, double, double, double, long, unsigned int)
void lchain_gen(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, overlap_region* tf, 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)
{
	// fprintf(stderr, "+[M::%s]\n", __func__);
	uint64_t i, k, l, m, sm, cn = cl->length; overlap_region *r; ///srt = 0
	clear_overlap_region_alloc(ol);
	clear_fake_cigar(&(tf->f_cigar));

	for (l = 0, k = 1, m = 0; k <= cn; k++) {
		if((k == cn) || (cl->list[k].readID != cl->list[l].readID) 
													|| (cl->list[k].strand != cl->list[l].strand)) {
			if(cl->list[l].readID != rid) {
				tf->x_id = rid; 
				tf->x_pos_strand = cl->list[l].strand;
				tf->y_id = cl->list[l].readID; 
				tf->y_pos_strand = 0;///always 0
				// fprintf(stderr, "+[M::%s] l::%lu, k::%lu\n", __func__, l, k);
				sm = lchain_dp(cl->list+l, k-l, cl->list+m, &(cl->chainDP), tf, max_skip, max_iter, max_dis, chn_pen_gap, chn_pen_skip, bw_rate, 
              	rl, rdb?Get_READ_LENGTH((*rdb), (*tf).y_id):udb->ug->u.a[(*tf).y_id].len, quick_check);
				// assert(sm > 0);
				if(ovlp_chain_gen(ol, tf, rl, rdb?Get_READ_LENGTH((*rdb), (*tf).y_id):udb->ug->u.a[(*tf).y_id].len, apend_be, cl->list+m, sm)) {
					r = &(ol->list[ol->length-1]); r->non_homopolymer_errors = m; 
					// if(r->y_pos_strand) {
					// 	reverse_k_mer_hit(cl->list+m, sm, rl, rdb?Get_READ_LENGTH((*rdb), r->y_id):udb->ug->u.a[r->y_id].len);
					// }
					reset_k_mer_hit(cl->list+m, sm, rl, rdb?Get_READ_LENGTH((*rdb), r->y_id):udb->ug->u.a[r->y_id].len, r->y_pos_strand, &(ol->length));
					if(gen_off) gen_fake_cigar(&(r->f_cigar), r, apend_be, cl->list+m, sm);
					m += sm;
				}
			}
			l = k;
		}
	}
	cl->length = m;


	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]) {
					if (k != i) {
						t = ol->list[k];
						ol->list[k] = ol->list[i];
						ol->list[i] = t;
					}
					++k;
				}
			}
			ol->length = k;
		}
	}
	/**
	if(!gen_off) {
		if(srt) ks_introsort_or_id(ol->length, ol->list);
		uint64_t cln = cl->length;
		for (i = k = m = 0; i < ol->length; ++i) {
			r = &(ol->list[i]);
			for (;(k<cln)&&((cl->list[k].readID!=r->y_id)||(cl->list[k].strand!=r->y_pos_strand)); k++);
			// assert(k<cln);
			for (cn = m; k < cln; k++, m++) {
				if((cl->list[k].readID!=r->y_id)||(cl->list[k].strand!=r->y_pos_strand)) break;
				if(m != k) cl->list[m] = cl->list[k];
			}
			// assert(m - cn > 0);
			if(r->y_pos_strand) {
				reverse_k_mer_hit(cl->list+cn, m-cn, rl, rdb?Get_READ_LENGTH((*rdb), r->y_id):udb->ug->u.a[r->y_id].len);
			}
			// gen_fake_cigar(&(r->f_cigar), r, apend_be, cl->list+cn, m-cn);
		}
		cl->length = m;
	}
	**/

	ks_introsort_or_xs(ol->length, ol->list);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movsd %xmm2, 0xc8(%rsp)
movq %xmm1, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, %rbx
movq %rcx, 0x8(%rsp)
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r14
movl 0x1c0(%rsp), %eax
movq %rax, 0x48(%rsp)
movl 0x1f8(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x1c8(%rsp), %r13
movq 0x8(%rdi), %rbp
movq %rsi, %rdi
callq 0x11935
movq %r13, 0x90(%rsp)
leaq 0x38(%r13), %rdi
callq 0x11976
leaq 0x18(%r14), %rax
movq %rax, 0x88(%rsp)
leaq 0x10(%r12), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r13
pushq $0x10
popq %rsi
xorl %eax, %eax
movq $0x0, 0x10(%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, 0x78(%rsp)
movq %r14, 0x30(%rsp)
movq %rbp, 0x40(%rsp)
movl %r15d, 0x3c(%rsp)
cmpq %rbp, %r13
ja 0xe87fa
movq (%r14), %rdx
movq %rax, %rcx
jne 0xe85be
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
jmp 0xe85ce
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
cmpl %ecx, (%rdx,%rsi)
je 0xe87ee
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %r15d, %ecx
jne 0xe85e1
movq %r13, %rax
jmp 0xe87ee
movq %rsi, 0x58(%rsp)
movq %rax, %rdi
shlq $0x4, %rdi
addq %rdx, %rdi
movq 0x90(%rsp), %r14
movl %r15d, (%r14)
movl (%rdi), %ecx
shrl $0x1f, %ecx
movl %ecx, 0xc(%r14)
movl (%rdi), %ecx
andl $0x0, 0x1c(%r14)
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, 0x10(%r14)
testq %rbx, %rbx
je 0xe8624
movq 0x18(%rbx), %rsi
movq (%rsi,%rcx,8), %r10
jmp 0xe8641
movq 0x20(%rsp), %rsi
movq (%rsi), %rsi
movq 0x10(%rsi), %rsi
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
movl (%rsi,%rcx), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %r13, %rsi
subq %rax, %rsi
movq 0x10(%rsp), %rbp
shlq $0x4, %rbp
addq %rbp, %rdx
subq $0x8, %rsp
movq 0x90(%rsp), %rcx
movq %r14, %r8
movq 0xa0(%rsp), %r9
movsd 0xc0(%rsp), %xmm0
movq 0xc8(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm2
pushq 0xb8(%rsp)
pushq %r10
pushq 0x20(%rsp)
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
callq 0x14230
addq $0x30, %rsp
movq %rax, %r10
testq %rbx, %rbx
je 0xe86bf
movq 0x18(%rbx), %rax
movl 0x10(%r14), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0xe86dc
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r14), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x30(%rsp), %rax
movq (%rax), %r9
addq %rbp, %r9
subq $0x8, %rsp
movq %r12, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movq 0x50(%rsp), %r8
movq %r10, %r14
pushq %r10
callq 0x11cba
popq %rcx
popq %rdx
testl %eax, %eax
je 0xe8746
imulq $0xa8, 0x10(%r12), %rdx
movq (%r12), %r12
movq %rbx, %rax
leaq (%r12,%rdx), %rbx
movq 0x10(%rsp), %rcx
movq %rdx, 0x80(%rsp)
movl %ecx, -0x78(%r12,%rdx)
testq %rax, %rax
je 0xe8753
movq 0x18(%rax), %rax
movl -0x98(%rbx), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0xe8772
movq %r13, %rax
movq 0x30(%rsp), %r14
jmp 0xe87e4
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl -0x98(%rbx), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r14, %r15
movq 0x30(%rsp), %r14
movq (%r14), %rdi
addq %rbp, %rdi
movl -0x8c(%rbx), %r8d
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %r9
callq 0xee4b9
cmpl $0x0, 0x38(%rsp)
je 0xe87cd
movq 0x80(%rsp), %rax
leaq (%r12,%rax), %rsi
addq $-0xa8, %rsi
addq $-0x70, %rbx
addq (%r14), %rbp
movq %rbx, %rdi
movq 0x48(%rsp), %rdx
movq %rbp, %rcx
movq %r15, %r8
callq 0x11a23
addq %r15, 0x10(%rsp)
movq %r13, %rax
movq 0x28(%rsp), %r12
movq 0x78(%rsp), %rbx
movl 0x3c(%rsp), %r15d
movq 0x40(%rsp), %rbp
movq 0x58(%rsp), %rsi
incq %r13
addq $0x10, %rsi
jmp 0xe85a4
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r14)
movq (%r12), %rsi
movq 0x10(%r12), %r15
cmpq 0x50(%rsp), %r15
jbe 0xe8948
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0x60(%rsp)
movq %r15, %rdi
callq 0xe248f
movq 0x18(%rsp), %rax
movq (%rax), %r13
xorl %ebx, %ebx
movq %r13, %r14
subq $0x1, %r14
jb 0xe8881
movq (%r12), %r15
leaq (%r15,%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3aee
movl %eax, %eax
movslq 0xd0(%rsp,%rax,4), %rcx
incq %rcx
movl %ecx, 0xd0(%rsp,%rax,4)
cmpq 0x50(%rsp), %rcx
jne 0xe8878
movl 0x24(%r15,%rbx), %ecx
movl %ecx, 0x60(%rsp,%rax,4)
addq $0xa8, %rbx
jmp 0xe883c
movdqa 0x60(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xe8941
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %r13, %r14
jae 0xe8937
movq (%r12), %r12
leaq (%r12,%rbp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xe3aee
movl 0x24(%r12,%rbp), %ecx
movl %eax, %eax
cmpl 0x60(%rsp,%rax,4), %ecx
jl 0xe8923
cmpq %r15, %r14
je 0xe8920
imulq $0xa8, %r15, %rax
addq %rax, %r12
movl $0xa8, %edx
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x28(%rsp), %rbx
movq (%rbx), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
movq %r13, %rsi
callq 0x2160
movq 0x10(%rbx), %r13
incq %r15
incq %r14
addq $0xa8, %rbp
movq 0x28(%rsp), %r12
jmp 0xe88a2
movq 0x18(%rsp), %rax
movq %r15, (%rax)
jmp 0xe8944
movq %r13, %r15
movq (%r12), %rsi
movq %r15, %rdi
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe1c6f