// File: chhylp123[P]hifiasm/anchor.cpp
// Function: lchain_qgen(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_qgen(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)
{
	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_qdp(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_qgen(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(tf->y_id == 66 || tf->y_id == 66) {
					// 	fprintf(stderr, "\n[M::%s::] utg%.6dl(%c), i::%lu\n", 
        			// 			__func__, (int32_t)tf->y_id+1, "+-"[tf->x_pos_strand], m);
					// }
					// 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));
					for (i = 0; i < sm; i++) {
						cl->list[m+i].readID = ol->length;
						// if(tf->y_id == 126) fprintf(stderr, "[M::%s::qoff->%u::toff->%u]\n", __func__, cl->list[m+i].self_offset, cl->list[m+i].offset);
					}
					if(gen_off) gen_fake_cigar(&(r->f_cigar), r, apend_be, cl->list+m, sm);
					m += sm;
				}
			}
			l = k;
		}
	}
	cl->length = m;
	// fprintf(stderr, "+[M::%s] cn::%lu, m::%lu, ol->length::%lu\n", __func__, cn, m, ol->length);
	// 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]; 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;
		}
	}
	ks_introsort_or_xs(ol->length, ol->list);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
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 0x1a9c0a
movq -0x68(%rbp), %rax
cmpq -0x88(%rbp), %rax
je 0x1a987a
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 0x1a987a
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 0x1a9bf7
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 0x1a9bef
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 0x1a99c0
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 0x1a99e9
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 0x24510
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 0x1a9ab0
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 0x1a9ad9
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 0x1ecc0
cmpl $0x0, %eax
je 0x1a9bed
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 $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x1a9ba9
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
addq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1a9b5f
cmpl $0x0, 0x48(%rbp)
je 0x1a9be1
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 0x1a9bef
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x1a9bf9
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1a97ef
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 0x1a9e7f
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 0x1a9d3b
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 0x1a9d28
movq -0x90(%rbp), %rax
movl 0x24(%rax), %ecx
movslq -0x94(%rbp), %rax
movl %ecx, -0xc0(%rbp,%rax,4)
jmp 0x1a9d2a
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1a9ca0
cmpl $0x0, -0xc0(%rbp)
jg 0x1a9d63
cmpl $0x0, -0xbc(%rbp)
jg 0x1a9d63
cmpl $0x0, -0xb8(%rbp)
jg 0x1a9d63
cmpl $0x0, -0xb4(%rbp)
jle 0x1a9e7d
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x1a9e71
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 0x1a9e5e
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x1a9e52
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 0x1a9e60
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1a9d73
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1a9e7f
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0x19d6c0
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r14
popq %rbp
retq
nop