// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movsd %xmm2, 0x98(%rsp)
movsd %xmm1, 0x90(%rsp)
movsd %xmm0, 0x88(%rsp)
movq %r9, 0x40(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, (%rsp)
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
movl 0x1c8(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x198(%rsp), %r12
movl 0x190(%rsp), %ebp
movq 0x8(%rdi), %r13
movq %rsi, %rdi
callq 0x3db37
movq %r12, 0x10(%rsp)
leaq 0x38(%r12), %rdi
movq %r13, %r12
callq 0x3db73
testq %r13, %r13
je 0x1282d3
leaq 0x18(%r15), %rax
movq %rax, 0x60(%rsp)
movl %ebp, %eax
movq %rax, 0x50(%rsp)
movl $0x1, %r13d
xorl %r9d, %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq 0x18(%rsp), %rbp
movl %r14d, 0xc(%rsp)
movq 0x10(%rsp), %r8
movq %r12, 0x48(%rsp)
cmpq %r12, %r13
je 0x1280cf
movq (%r15), %rcx
movq %r13, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %edx
movq %rax, %rsi
shlq $0x4, %rsi
cmpl (%rcx,%rsi), %edx
je 0x1282c5
movq (%r15), %rdx
movq %rax, %rdi
shlq $0x4, %rdi
movl (%rdx,%rdi), %ecx
andl %r10d, %ecx
cmpl %r14d, %ecx
jne 0x1280ec
movq %r13, %rax
jmp 0x1282c5
addq %rdx, %rdi
movl %r14d, (%r8)
movl (%rdi), %ecx
shrl $0x1f, %ecx
movl %ecx, 0xc(%r8)
movl (%rdi), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, 0x10(%r8)
movl $0x0, 0x1c(%r8)
testq %rbp, %rbp
je 0x12811e
movq 0x18(%rbp), %rsi
movq (%rsi,%rcx,8), %r10
jmp 0x12813b
movq 0x40(%rsp), %rsi
movq (%rsi), %rsi
movq 0x10(%rsi), %rsi
movl %ecx, %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rsi,%rcx,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %r13, %rsi
subq %rax, %rsi
movq %r9, 0x20(%rsp)
movq %r9, %r12
shlq $0x4, %r12
addq %r12, %rdx
subq $0x8, %rsp
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r9
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
pushq 0x88(%rsp)
pushq %r10
pushq 0x18(%rsp)
pushq 0x98(%rsp)
pushq 0x98(%rsp)
movq %r8, %r14
callq 0x40991
addq $0x30, %rsp
movq %rax, %r10
testq %rbp, %rbp
je 0x1281b6
movq 0x18(%rbp), %rax
movl 0x10(%r14), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0x1281d3
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r14), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x80000000, %ebp       # imm = 0x80000000
movq (%r15), %r9
addq %r12, %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
movq 0x58(%rsp), %r8
movq %r10, %r14
pushq %r10
callq 0x3e053
addq $0x10, %rsp
testl %eax, %eax
je 0x1282a3
movq (%rbx), %rax
imulq $0xa8, 0x10(%rbx), %rcx
movq 0x20(%rsp), %r9
movl %r9d, -0x78(%rax,%rcx)
testq %r14, %r14
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
je 0x12824b
movq %r14, %rdx
movq (%r15), %rsi
movl 0x10(%rbx), %edi
andl %r10d, %edi
movl (%rsi,%r12), %r8d
andl %ebp, %r8d
orl %edi, %r8d
movl %r8d, (%rsi,%r12)
addq $0x10, %r12
decq %rdx
jne 0x12822b
cmpl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %r12
je 0x12828c
leaq (%rax,%rcx), %rdi
leaq (%rax,%rcx), %rsi
addq $-0xa8, %rsi
addq $-0x70, %rdi
movq %r9, %rcx
shlq $0x4, %rcx
addq (%r15), %rcx
movq 0x50(%rsp), %rdx
movq %r14, %r8
callq 0x3dc52
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r9
addq %r14, %r9
movq %r13, %rax
movq 0x18(%rsp), %rbp
movl 0xc(%rsp), %r14d
movq 0x10(%rsp), %r8
jmp 0x1282c5
movq %r13, %rax
movq 0x18(%rsp), %rbp
movl 0xc(%rsp), %r14d
movq 0x10(%rsp), %r8
movq 0x48(%rsp), %r12
movq 0x20(%rsp), %r9
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
incq %r13
cmpq %r12, %r13
jbe 0x1280ad
jmp 0x1282d6
xorl %r9d, %r9d
movq %r9, 0x8(%r15)
movq 0x10(%rbx), %rdi
movq 0x58(%rsp), %r14
cmpq %r14, %rdi
jbe 0x12845e
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x30(%rsp)
movq (%rbx), %rsi
callq 0x121388
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x12836e
movq (%rbx), %rcx
movq (%rsp), %rdx
decl %edx
addq $0x4, %rcx
movl (%rcx), %edi
movl 0x4(%rcx), %r8d
testl %edi, %edi
je 0x128362
movl $0x3, %esi
cmpl %edx, %r8d
jb 0x128336
xorl %esi, %esi
testl %edi, %edi
setne %sil
movslq 0xa0(%rsp,%rsi,4), %rdi
incq %rdi
movl %edi, 0xa0(%rsp,%rsi,4)
cmpq %r14, %rdi
jne 0x128354
movl 0x20(%rcx), %edi
movl %edi, 0x30(%rsp,%rsi,4)
addq $0xa8, %rcx
decq %rax
jne 0x12831a
jmp 0x12836e
movl $0x2, %esi
cmpl %edx, %r8d
jne 0x12832e
jmp 0x128336
cmpl $0x0, 0x30(%rsp)
jg 0x12838e
cmpl $0x0, 0x34(%rsp)
jg 0x12838e
cmpl $0x0, 0x38(%rsp)
jg 0x12838e
cmpl $0x0, 0x3c(%rsp)
jle 0x12845e
cmpq $0x0, 0x10(%rbx)
je 0x128458
movq (%rsp), %rax
decl %eax
movq %rax, (%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %rax
movl 0x4(%rax,%r13), %edx
movl 0x8(%rax,%r13), %esi
testl %edx, %edx
je 0x128445
movl $0x3, %ecx
cmpl (%rsp), %esi
jb 0x1283d1
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
movl 0x24(%rax,%r13), %edx
cmpl 0x30(%rsp,%rcx,4), %edx
jl 0x12842f
cmpq %rbp, %r14
je 0x12842c
leaq (%rax,%r13), %r15
imulq $0xa8, %rbp, %r12
addq %rax, %r12
movl $0xa8, %edx
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r13, %rdi
movl $0xa8, %edx
leaq 0xb0(%rsp), %rsi
callq 0x2160
incq %rbp
incq %r14
addq $0xa8, %r13
cmpq 0x10(%rbx), %r14
jb 0x1283ab
jmp 0x12845a
movl $0x2, %ecx
cmpl (%rsp), %esi
jne 0x1283ca
jmp 0x1283d1
xorl %ebp, %ebp
movq %rbp, 0x10(%rbx)
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x120acf