// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x230, %rsp            # imm = 0x230
movl 0x48(%rbp), %eax
movq 0x40(%rbp), %rax
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, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movsd %xmm0, -0x48(%rbp)
movsd %xmm1, -0x50(%rbp)
movsd %xmm2, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1e120
movq 0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x1e1d0
movq $0x0, -0x70(%rbp)
movq $0x1, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x88(%rbp), %rax
ja 0x1a94bf
movq -0x68(%rbp), %rax
cmpq -0x88(%rbp), %rax
je 0x1a90ca
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jne 0x1a90ca
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
shrl $0x1f, %ecx
cmpl %ecx, %eax
je 0x1a94ac
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x24(%rbp), %eax
je 0x1a94a4
movl -0x24(%rbp), %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x18(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x1c8(%rbp)
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x1b8(%rbp)
movq -0x18(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x1b0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq 0x30(%rbp), %rax
movq %rax, -0x198(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x190(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x188(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1a9210
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1d0(%rbp)
jmp 0x1a9239
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x1d0(%rbp)
movq -0x170(%rbp), %r11
movsd -0x178(%rbp), %xmm2
movsd -0x180(%rbp), %xmm1
movsd -0x188(%rbp), %xmm0
movq -0x190(%rbp), %rbx
movq -0x198(%rbp), %r14
movq -0x1a0(%rbp), %r9
movq -0x1a8(%rbp), %r8
movq -0x1b0(%rbp), %rcx
movq -0x1b8(%rbp), %rdx
movq -0x1c0(%rbp), %rsi
movq -0x1c8(%rbp), %rdi
movq -0x1d0(%rbp), %r10
movq 0x40(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x23c60
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x1d8(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1a9300
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1f0(%rbp)
jmp 0x1a9329
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x1f0(%rbp)
movq -0x1d8(%rbp), %rdx
movq -0x1e0(%rbp), %rsi
movq -0x1e8(%rbp), %rdi
movq -0x1f0(%rbp), %rcx
movl 0x10(%rbp), %eax
movl %eax, %r8d
movq -0x18(%rbp), %rax
movq (%rax), %r9
movq -0x78(%rbp), %rax
shlq $0x4, %rax
addq %rax, %r9
movq -0x80(%rbp), %rax
movq %rax, (%rsp)
callq 0x1e870
cmpl $0x0, %eax
je 0x1a94a2
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
movl %eax, %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x208(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x1f8(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1a93fc
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x90(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x210(%rbp)
jmp 0x1a9428
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x90(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x210(%rbp)
movq -0x1f8(%rbp), %rdx
movq -0x200(%rbp), %rsi
movq -0x208(%rbp), %rdi
movq -0x210(%rbp), %rcx
movq -0x90(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %r8d
movq -0x20(%rbp), %r9
addq $0x10, %r9
callq 0x1b46c0
cmpl $0x0, 0x48(%rbp)
je 0x1a9496
movq -0x90(%rbp), %rdi
addq $0x38, %rdi
movq -0x90(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x78(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x80(%rbp), %r8
callq 0x1e3a0
movq -0x80(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x1a94a4
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x1a94ae
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1a903f
movq -0x78(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
cmpq 0x20(%rbp), %rax
jbe 0x1a9734
movl $0x0, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl $0x0, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x19e350
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x1a95f0
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %esi
callq 0x1a08b0
movl %eax, -0x94(%rbp)
movslq -0x94(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0xb0(%rbp,%rax,4)
movslq -0x94(%rbp), %rax
movslq -0xb0(%rbp,%rax,4), %rax
cmpq 0x20(%rbp), %rax
jne 0x1a95dd
movq -0x90(%rbp), %rax
movl 0x24(%rax), %ecx
movslq -0x94(%rbp), %rax
movl %ecx, -0xc0(%rbp,%rax,4)
jmp 0x1a95df
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1a9555
cmpl $0x0, -0xc0(%rbp)
jg 0x1a9618
cmpl $0x0, -0xbc(%rbp)
jg 0x1a9618
cmpl $0x0, -0xb8(%rbp)
jg 0x1a9618
cmpl $0x0, -0xb4(%rbp)
jle 0x1a9732
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x1a9726
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %esi
callq 0x1a08b0
movl %eax, -0x94(%rbp)
movq -0x90(%rbp), %rax
movl 0x24(%rax), %eax
movslq -0x94(%rbp), %rcx
cmpl -0xc0(%rbp,%rcx,4), %eax
jl 0x1a9713
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x1a9707
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x68(%rbp), %rax
addq %rax, %rsi
leaq -0x168(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x68(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x20(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rdi
leaq -0x168(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1a9715
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1a9628
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1a9734
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x19d6c0
addq $0x230, %rsp            # imm = 0x230
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)