// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movsd %xmm2, 0xa0(%rsp)
movq %xmm1, 0x98(%rsp)
movsd %xmm0, 0x90(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, %r14
movq %rcx, 0x18(%rsp)
movl %edx, %r12d
movq %rsi, %rbx
movq %rdi, %r13
movl 0x1a0(%rsp), %eax
movq %rax, 0x48(%rsp)
movl 0x1d8(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x1a8(%rsp), %r15
movq 0x8(%rdi), %rbp
movq %rsi, %rdi
callq 0x11935
leaq 0x38(%r15), %rdi
callq 0x11976
movq %rbp, %rdi
movq %r15, %r8
movq %r14, %r9
leaq 0x18(%r13), %rax
movq %rax, 0x68(%rsp)
pushq $0x1
popq %rbp
xorl %eax, %eax
xorl %r15d, %r15d
movq %r14, 0x28(%rsp)
movl %r12d, 0x14(%rsp)
movq %r8, 0x20(%rsp)
movq %rdi, 0x40(%rsp)
cmpq %rdi, %rbp
ja 0xe8c7c
movq (%r13), %rdx
jne 0xe8a67
movq %rax, %rcx
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
jmp 0xe8a81
movq %rbp, %rsi
shlq $0x4, %rsi
movq %rax, %rcx
shlq $0x4, %rcx
movl (%rdx,%rcx), %ecx
cmpl %ecx, (%rdx,%rsi)
je 0xe8c74
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %r12d, %ecx
jne 0xe8a94
movq %rbp, %rax
jmp 0xe8c74
movq %rax, %rdi
shlq $0x4, %rdi
addq %rdx, %rdi
movl %r12d, (%r8)
movl (%rdi), %ecx
shrl $0x1f, %ecx
movl %ecx, 0xc(%r8)
movl (%rdi), %ecx
andl $0x0, 0x1c(%r8)
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, 0x10(%r8)
testq %r9, %r9
je 0xe8aca
movq 0x18(%r9), %rsi
movq (%rsi,%rcx,8), %r10
jmp 0xe8ae7
movq 0x38(%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 %r9, %r12
movq %rbp, %rsi
subq %rax, %rsi
movq %r15, 0x8(%rsp)
movq %r15, %r14
shlq $0x4, %r14
addq %r14, %rdx
subq $0x8, %rsp
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r9
movsd 0x98(%rsp), %xmm0
movq 0xa0(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm2
pushq 0x90(%rsp)
pushq %r10
pushq 0x30(%rsp)
pushq 0xa0(%rsp)
pushq 0xa0(%rsp)
movq %r8, %r15
callq 0x146e6
addq $0x30, %rsp
movq %rax, %r10
testq %r12, %r12
je 0xe8b66
movq 0x18(%r12), %rax
movl 0x10(%r15), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0xe8b83
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r15), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq (%r13), %r9
addq %r14, %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %r8
pushq %r10
movq %r10, %r12
callq 0x11ea3
popq %rcx
popq %rdx
testl %eax, %eax
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0x80000000, %r11d      # imm = 0x80000000
movq 0x8(%rsp), %r15
je 0xe8c5d
imulq $0xa8, 0x10(%rbx), %rax
movq (%rbx), %rcx
leaq (%rcx,%rax), %rdi
movl %r15d, -0x78(%rcx,%rax)
movq %r12, 0xa8(%rsp)
movq %r12, %rax
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %r8
movl 0x14(%rsp), %r12d
subq $0x1, %rax
jb 0xe8c14
movq (%r13), %rcx
movl 0x10(%rbx), %edx
andl %r10d, %edx
movl (%rcx,%r14), %esi
andl %r11d, %esi
orl %edx, %esi
movl %esi, (%rcx,%r14)
addq $0x10, %r14
jmp 0xe8bf1
cmpl $0x0, 0x34(%rsp)
movq 0xa8(%rsp), %r14
je 0xe8c50
leaq -0xa8(%rdi), %rsi
addq $-0x70, %rdi
movq %r15, %rcx
shlq $0x4, %rcx
addq (%r13), %rcx
movq 0x48(%rsp), %rdx
movq %r14, %r8
callq 0x11a23
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
addq %r14, %r15
movq %rbp, %rax
movq 0x40(%rsp), %rdi
jmp 0xe8c74
movq %rbp, %rax
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x40(%rsp), %rdi
movl 0x14(%rsp), %r12d
incq %rbp
jmp 0xe8a4c
movq %r15, 0x8(%r13)
movq (%rbx), %rsi
movq 0x10(%rbx), %r15
movq 0x60(%rsp), %r13
cmpq %r13, %r15
jbe 0xe8dbd
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0x50(%rsp)
movq %r15, %rdi
callq 0xe248f
movq 0x10(%rbx), %r15
xorl %r14d, %r14d
movq %r15, 0x8(%rsp)
subq $0x1, %r15
jb 0xe8cfb
movq (%rbx), %r12
leaq (%r12,%r14), %rdi
movq 0x18(%rsp), %rsi
callq 0xe3aee
movl %eax, %eax
movslq 0xb0(%rsp,%rax,4), %rcx
incq %rcx
movl %ecx, 0xb0(%rsp,%rax,4)
cmpq %r13, %rcx
jne 0xe8cf2
movl 0x24(%r12,%r14), %ecx
movl %ecx, 0x50(%rsp,%rax,4)
addq $0xa8, %r14
jmp 0xe8cb9
movdqa 0x50(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xe8db5
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq 0x8(%rsp), %rbp
jae 0xe8daf
movq (%rbx), %r13
leaq (%r14,%r13), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0xe3aee
movl 0x24(%r13,%r14), %ecx
movl %eax, %eax
cmpl 0x50(%rsp,%rax,4), %ecx
jl 0xe8da0
cmpq %r15, %rbp
je 0xe8d9d
imulq $0xa8, %r15, %rax
addq %rax, %r13
movl $0xa8, %edx
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r14, %rdi
movl $0xa8, %edx
leaq 0xc0(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
incq %r15
incq %rbp
addq $0xa8, %r14
jmp 0xe8d1c
movq %r15, 0x10(%rbx)
jmp 0xe8dba
movq 0x8(%rsp), %r15
movq (%rbx), %rsi
movq %r15, %rdi
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe1c6f