// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movsd %xmm2, 0x90(%rsp)
movq %xmm1, 0x88(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %r9, 0x48(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, %r14
movl %edx, 0x14(%rsp)
movq %rsi, %rbx
movq %rdi, %r13
movl 0x1f8(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x1c8(%rsp), %r15
movl 0x1c0(%rsp), %ebp
movq 0x8(%rdi), %r12
movq %rsi, %rdi
callq 0x3d018
movq %r15, 0x8(%rsp)
leaq 0x38(%r15), %rdi
callq 0x3d054
testq %r12, %r12
je 0x12dcfa
movq %r12, %rsi
leaq 0x18(%r13), %rax
movq %rax, 0x50(%rsp)
movl %ebp, %eax
movq %rax, 0x98(%rsp)
movl $0x1, %r15d
movq $0x0, 0x20(%rsp)
movl $0x80000000, %r12d      # imm = 0x80000000
xorl %edi, %edi
movq %r13, %r10
movq 0x30(%rsp), %r13
movl 0x14(%rsp), %r8d
movq %r10, 0x18(%rsp)
movq %rsi, 0x58(%rsp)
movq (%r10), %rdx
cmpq %rsi, %r15
jne 0x12d90f
movq %rdi, %rax
shlq $0x4, %rax
movl (%rdx,%rax), %eax
jmp 0x12d929
movq %r15, %rcx
shlq $0x4, %rcx
movq %rdi, %rax
shlq $0x4, %rax
movl (%rdx,%rax), %eax
cmpl %eax, (%rdx,%rcx)
je 0x12dcec
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r8d, %eax
jne 0x12d93b
movq %r15, %rdi
jmp 0x12dcec
movq %r15, %rsi
subq %rdi, %rsi
shlq $0x4, %rdi
addq %rdx, %rdi
movq 0x8(%rsp), %rax
movl %r8d, (%rax)
movq %rax, %rcx
movl (%rdi), %eax
shrl $0x1f, %eax
movl %eax, 0xc(%rcx)
movl (%rdi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rbp
shlq $0x4, %rbp
addq %rbp, %rdx
movl %eax, 0x10(%rcx)
movl $0x0, 0x1c(%rcx)
testq %r13, %r13
je 0x12d9eb
movq 0x18(%r13), %r10
subq $0x8, %rsp
movq 0x58(%rsp), %rcx
movq 0x10(%rsp), %r8
movq 0x68(%rsp), %r9
movsd 0x88(%rsp), %xmm0
movq 0x90(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
pushq 0x80(%rsp)
pushq (%r10,%rax,8)
pushq %r14
pushq 0x90(%rsp)
pushq 0x90(%rsp)
callq 0x3f995
movq 0x38(%rsp), %rsi
addq $0x30, %rsp
movq %rax, %r10
movq 0x18(%r13), %rax
movl 0x10(%rsi), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0x12da7f
movq 0x48(%rsp), %r13
movq (%r13), %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 0x58(%rsp), %rcx
movq 0x10(%rsp), %r8
movq 0x68(%rsp), %r9
movsd 0x88(%rsp), %xmm0
movq 0x90(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
pushq 0x80(%rsp)
pushq %rax
pushq %r14
pushq 0x90(%rsp)
pushq 0x90(%rsp)
callq 0x3f995
movq 0x38(%rsp), %rsi
addq $0x30, %rsp
movq %rax, %r10
movq (%r13), %rax
movq 0x30(%rsp), %r13
movq 0x10(%rax), %rax
movl 0x10(%rsi), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rcx
movq 0x18(%rsp), %rax
movq (%rax), %r9
addq %rbp, %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r14, %rdx
movq 0xa0(%rsp), %r8
movq %r10, 0x30(%rsp)
pushq %r10
callq 0x3d339
addq $0x10, %rsp
testl %eax, %eax
je 0x12dae6
imulq $0xa8, 0x10(%rbx), %r11
movq (%rbx), %rcx
leaq (%rcx,%r11), %rdi
movq 0x20(%rsp), %rax
movq %rcx, 0xc8(%rsp)
movl %eax, -0x78(%rcx,%r11)
testq %r13, %r13
je 0x12daf3
movq 0x18(%r13), %rdx
movl -0x98(%rdi), %esi
movq (%rdx,%rsi,8), %rsi
jmp 0x12db12
movq %r15, %rdi
movq 0x18(%rsp), %r10
jmp 0x12dce2
movq 0x48(%rsp), %rax
movq (%rax), %rdx
movq 0x10(%rdx), %rdx
movl -0x98(%rdi), %esi
leaq (%rsi,%rsi,4), %rsi
movl (%rdx,%rsi,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r8
movq (%r10), %r9
leaq (%r9,%rbp), %rdx
cmpl $0x0, -0x8c(%rdi)
je 0x12db40
cmpq $0x2, %r8
jae 0x12db6f
movq $0x0, 0x40(%rsp)
jmp 0x12dc5c
testq %r8, %r8
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
je 0x12dc9f
movq %r8, %rsi
movl 0x10(%rbx), %eax
andl %r9d, %eax
movl (%rdx), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%rdx)
addq $0x10, %rdx
decq %rsi
jne 0x12db52
jmp 0x12dc9f
movq %r11, 0xc0(%rsp)
movq %r8, %r11
shrq %r11
movq %r8, %r10
shlq $0x4, %r10
addq %rbp, %r10
addq %r10, %r9
addq $-0x10, %r9
movq %rdx, %r10
movq %r11, 0x40(%rsp)
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl (%r10), %eax
movl %eax, 0xe0(%rsp)
movq 0x4(%r10), %rax
movq %rax, 0xe4(%rsp)
movl 0xc(%r10), %eax
movl %eax, 0xec(%rsp)
movups (%r9), %xmm0
movups %xmm0, (%r10)
movl 0xe0(%rsp), %eax
movl %eax, (%r9)
movq 0xe4(%rsp), %rax
movq %rax, 0x4(%r9)
movl 0xec(%rsp), %eax
movl %eax, 0xc(%r9)
movl 0x4(%r10), %eax
movl 0x8(%r10), %ecx
notl %ecx
addl %r14d, %ecx
movl %ecx, 0x8(%r10)
notl %eax
addl %esi, %eax
movl %eax, 0x4(%r10)
movl 0x4(%r9), %eax
movl 0x8(%r9), %ecx
notl %ecx
addl %r14d, %ecx
movl %ecx, 0x8(%r9)
notl %eax
addl %esi, %eax
movl %eax, 0x4(%r9)
movl 0x10(%rbx), %eax
andl %r13d, %eax
movl (%r9), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%r9)
movl (%r10), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%r10)
addq $-0x10, %r9
addq $0x10, %r10
decq %r11
jne 0x12db9c
movq 0x18(%rsp), %r10
movq 0x30(%rsp), %r13
movq 0x28(%rsp), %r8
movq 0xc0(%rsp), %r11
testb $0x1, %r8b
je 0x12dc9f
movq 0x40(%rsp), %r9
shlq $0x4, %r9
movl 0x4(%rdx,%r9), %eax
movl 0x8(%rdx,%r9), %ecx
notl %ecx
addl %r14d, %ecx
movl %ecx, 0x8(%rdx,%r9)
notl %eax
addl %eax, %esi
movl %esi, 0x4(%rdx,%r9)
movl 0x10(%rbx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl (%rdx,%r9), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%rdx,%r9)
cmpl $0x0, 0x3c(%rsp)
je 0x12dcda
movq 0xc8(%rsp), %rax
leaq (%rax,%r11), %rsi
addq $-0xa8, %rsi
addq $-0x70, %rdi
addq (%r10), %rbp
movq 0x98(%rsp), %rdx
movq %rbp, %rcx
callq 0x3d133
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %r10
addq %r8, 0x20(%rsp)
movq %r15, %rdi
movl 0x14(%rsp), %r8d
movq 0x58(%rsp), %rsi
incq %r15
cmpq %rsi, %r15
jbe 0x12d8fb
jmp 0x12dd06
movq $0x0, 0x20(%rsp)
movq %r13, %r10
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r10)
movq (%rbx), %rsi
movq 0x10(%rbx), %r15
movq 0xb8(%rsp), %r12
cmpq %r12, %r15
jbe 0x12dead
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq %r15, %rdi
callq 0x127252
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x12dea2
movq (%rbx), %rcx
leal -0x1(%r14), %edx
addq $0x4, %rcx
movq %rax, %rsi
movl (%rcx), %r8d
movl 0x4(%rcx), %r9d
testl %r8d, %r8d
je 0x12ddae
movl $0x3, %edi
cmpl %edx, %r9d
jb 0x12dd7c
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movslq 0xd0(%rsp,%rdi,4), %r8
incq %r8
movl %r8d, 0xd0(%rsp,%rdi,4)
cmpq %r12, %r8
jne 0x12dda0
movl 0x20(%rcx), %r8d
movl %r8d, 0xa0(%rsp,%rdi,4)
addq $0xa8, %rcx
decq %rsi
jne 0x12dd5d
jmp 0x12ddba
movl $0x2, %edi
cmpl %edx, %r9d
jne 0x12dd73
jmp 0x12dd7c
movdqa 0xa0(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x12dea7
decl %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq (%rbx), %rcx
movl 0x4(%rcx,%r12), %esi
movl 0x8(%rcx,%r12), %edi
testl %esi, %esi
je 0x12de89
movl $0x3, %edx
cmpl %r14d, %edi
jb 0x12de08
xorl %edx, %edx
testl %esi, %esi
setne %dl
movl 0x24(%rcx,%r12), %esi
cmpl 0xa0(%rsp,%rdx,4), %esi
jl 0x12de74
cmpq %r15, %r13
je 0x12de71
leaq (%rcx,%r12), %rax
movq %rax, 0x8(%rsp)
imulq $0xa8, %r15, %rbp
addq %rcx, %rbp
movl $0xa8, %edx
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
movl $0xa8, %edx
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0xe0(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rax
incq %r15
incq %r13
addq $0xa8, %r12
cmpq %rax, %r13
jb 0x12dde2
jmp 0x12de9c
movl $0x2, %edx
cmpl %r14d, %edi
jne 0x12de01
jmp 0x12de08
movq %r15, 0x10(%rbx)
jmp 0x12deaa
xorl %r15d, %r15d
jmp 0x12deaa
movq %rax, %r15
movq (%rbx), %rsi
movq %r15, %rdi
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x126994