// File: /workspace/github_cpp_makefile_star2_O3/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, 0x80(%rsp)
movq %xmm1, 0x78(%rsp)
movsd %xmm0, 0x70(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %r14
movl %edx, %r12d
movq %rsi, %rbx
movq %rdi, %r15
movl 0x1d8(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x1a8(%rsp), %r13
movl 0x1a0(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x8(%rdi), %rbp
movq %rsi, %rdi
callq 0x3d018
movq %r13, 0x28(%rsp)
leaq 0x38(%r13), %rdi
callq 0x3d054
testq %rbp, %rbp
je 0x12e236
movq %rbp, %rsi
leaq 0x18(%r15), %rax
movq %rax, 0x40(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x88(%rsp)
movl $0x1, %ebp
xorl %r9d, %r9d
xorl %edi, %edi
movq 0x20(%rsp), %r8
movq %r14, 0x18(%rsp)
movl %r12d, 0x14(%rsp)
movq %rsi, 0x48(%rsp)
movq (%r15), %rdx
cmpq %rsi, %rbp
jne 0x12dfd1
movq %rdi, %rax
shlq $0x4, %rax
movl (%rdx,%rax), %eax
jmp 0x12dfeb
movq %rbp, %rcx
shlq $0x4, %rcx
movq %rdi, %rax
shlq $0x4, %rax
movl (%rdx,%rax), %eax
cmpl %eax, (%rdx,%rcx)
je 0x12e228
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r12d, %eax
jne 0x12dffd
movq %rbp, %rdi
jmp 0x12e228
movq %rbp, %rsi
subq %rdi, %rsi
shlq $0x4, %rdi
addq %rdx, %rdi
movq 0x28(%rsp), %rax
movl %r12d, (%rax)
movq %rax, %r12
movl (%rdi), %eax
shrl $0x1f, %eax
movl %eax, 0xc(%r12)
movl (%rdi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r9, 0x8(%rsp)
movq %r9, %r13
shlq $0x4, %r13
addq %r13, %rdx
movl %eax, 0x10(%r12)
movl $0x0, 0x1c(%r12)
testq %r8, %r8
je 0x12e0b9
movq 0x18(%r8), %r10
subq $0x8, %rsp
movq 0x48(%rsp), %rcx
movq %r8, %r12
movq 0x30(%rsp), %r8
movq 0x58(%rsp), %r9
movsd 0x78(%rsp), %xmm0
movq 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
pushq 0x70(%rsp)
pushq (%r10,%rax,8)
pushq %r14
pushq 0x80(%rsp)
pushq 0x80(%rsp)
callq 0x40163
addq $0x30, %rsp
movq %r14, %rdx
movq %rax, %r14
movq 0x18(%r12), %rax
movq 0x28(%rsp), %r12
movl 0x10(%r12), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0x12e143
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl %eax, %eax
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rax
subq $0x8, %rsp
movq 0x48(%rsp), %rcx
movq %r12, %r8
movq 0x58(%rsp), %r9
movsd 0x78(%rsp), %xmm0
movq 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
pushq 0x70(%rsp)
pushq %rax
pushq %r14
pushq 0x80(%rsp)
pushq 0x80(%rsp)
callq 0x40163
addq $0x30, %rsp
movq %r14, %rdx
movq %rax, %r14
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r12), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rcx
movq (%r15), %r9
addq %r13, %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r12, %rsi
movq 0x90(%rsp), %r8
pushq %r14
callq 0x3d51f
addq $0x10, %rsp
testl %eax, %eax
je 0x12e20c
movq (%rbx), %rax
imulq $0xa8, 0x10(%rbx), %rcx
movq 0x8(%rsp), %r9
movl %r9d, -0x78(%rax,%rcx)
testq %r14, %r14
movl 0x14(%rsp), %r12d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0x80000000, %r11d      # imm = 0x80000000
je 0x12e1bc
movq %r14, %rdx
movq (%r15), %rsi
movl 0x10(%rbx), %edi
andl %r10d, %edi
movl (%rsi,%r13), %r8d
andl %r11d, %r8d
orl %edi, %r8d
movl %r8d, (%rsi,%r13)
addq $0x10, %r13
decq %rdx
jne 0x12e19c
cmpl $0x0, 0x34(%rsp)
je 0x12e1f5
leaq (%rax,%rcx), %rdi
leaq (%rax,%rcx), %rsi
addq $-0xa8, %rsi
addq $-0x70, %rdi
movq %r9, %rcx
shlq $0x4, %rcx
addq (%r15), %rcx
movq 0x88(%rsp), %rdx
movq %r14, %r8
callq 0x3d133
movq 0x8(%rsp), %r9
addq %r14, %r9
movq %rbp, %rdi
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r8
movq 0x48(%rsp), %rsi
jmp 0x12e228
movq %rbp, %rdi
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r8
movl 0x14(%rsp), %r12d
movq 0x48(%rsp), %rsi
movq 0x8(%rsp), %r9
incq %rbp
cmpq %rsi, %rbp
jbe 0x12dfbd
jmp 0x12e239
xorl %r9d, %r9d
movq %r9, 0x8(%r15)
movq (%rbx), %rsi
movq 0x10(%rbx), %r15
movq 0xa8(%rsp), %r12
cmpq %r12, %r15
jbe 0x12e3da
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %r15, %rdi
callq 0x127252
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x12e3cf
movq (%rbx), %rcx
leal -0x1(%r14), %edx
addq $0x4, %rcx
movq %rax, %rsi
movl (%rcx), %r8d
movl 0x4(%rcx), %r9d
testl %r8d, %r8d
je 0x12e2dc
movl $0x3, %edi
cmpl %edx, %r9d
jb 0x12e2aa
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movslq 0xb0(%rsp,%rdi,4), %r8
incq %r8
movl %r8d, 0xb0(%rsp,%rdi,4)
cmpq %r12, %r8
jne 0x12e2ce
movl 0x20(%rcx), %r8d
movl %r8d, 0x90(%rsp,%rdi,4)
addq $0xa8, %rcx
decq %rsi
jne 0x12e28b
jmp 0x12e2e8
movl $0x2, %edi
cmpl %edx, %r9d
jne 0x12e2a1
jmp 0x12e2aa
movdqa 0x90(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x12e3d4
decl %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r14, 0x18(%rsp)
movq (%rbx), %rcx
movl 0x4(%rcx,%rbp), %esi
movl 0x8(%rcx,%rbp), %edi
testl %esi, %esi
je 0x12e3b6
movl $0x3, %edx
cmpl %r14d, %edi
jb 0x12e338
xorl %edx, %edx
testl %esi, %esi
setne %dl
movl 0x24(%rcx,%rbp), %esi
cmpl 0x90(%rsp,%rdx,4), %esi
jl 0x12e3a1
cmpq %r15, %r12
je 0x12e39e
leaq (%rcx,%rbp), %r14
imulq $0xa8, %r15, %r13
addq %rcx, %r13
movl $0xa8, %edx
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %r14
callq 0x2160
movq (%rbx), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0xc0(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rax
incq %r15
incq %r12
addq $0xa8, %rbp
cmpq %rax, %r12
jb 0x12e314
jmp 0x12e3c9
movl $0x2, %edx
cmpl %r14d, %edi
jne 0x12e331
jmp 0x12e338
movq %r15, 0x10(%rbx)
jmp 0x12e3d7
xorl %r15d, %r15d
jmp 0x12e3d7
movq %rax, %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 0x126994