// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc8(%rsp)
movsd %xmm1, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x68(%rsp)
movq %rcx, %r14
movl %edx, 0x14(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
movl 0x1f8(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x1c8(%rsp), %r13
movl 0x1c0(%rsp), %r12d
movq 0x8(%rdi), %r15
movq %rsi, %rdi
callq 0x3db37
movq %r13, 0x30(%rsp)
leaq 0x38(%r13), %rdi
callq 0x3db73
testq %r15, %r15
je 0x127dfe
movq %r15, %r8
leaq 0x18(%rbp), %rax
movq %rax, 0x90(%rsp)
movl %r12d, %eax
movq %rax, 0x60(%rsp)
movl $0x1, %r15d
movq $0x0, 0x18(%rsp)
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %eax, %eax
movq %rbp, %r9
movq %rbp, 0x8(%rsp)
movq 0x30(%rsp), %r13
movq %r8, 0x58(%rsp)
movl 0x14(%rsp), %r10d
cmpq %r8, %r15
je 0x127a77
movq (%r9), %rcx
movq %r15, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %edx
movq %rax, %rsi
shlq $0x4, %rsi
cmpl (%rcx,%rsi), %edx
je 0x127df0
movq (%r9), %rdx
movq %rax, %rdi
shlq $0x4, %rdi
movl (%rdx,%rdi), %ecx
andl %r12d, %ecx
cmpl %r10d, %ecx
jne 0x127a94
movq %r15, %rax
jmp 0x127df0
addq %rdx, %rdi
movl %r10d, (%r13)
movl (%rdi), %ecx
shrl $0x1f, %ecx
movl %ecx, 0xc(%r13)
movl (%rdi), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, 0x10(%r13)
movl $0x0, 0x1c(%r13)
movq 0x68(%rsp), %r12
testq %r12, %r12
je 0x127acd
movq 0x18(%r12), %rsi
movq (%rsi,%rcx,8), %r10
jmp 0x127aea
movq 0x28(%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 %r15, %rsi
subq %rax, %rsi
movq 0x18(%rsp), %rbp
shlq $0x4, %rbp
addq %rbp, %rdx
subq $0x8, %rsp
movq 0x98(%rsp), %rcx
movq %r13, %r8
movq 0xa0(%rsp), %r9
movsd 0xc0(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm2
pushq 0xb8(%rsp)
pushq %r10
pushq %r14
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
callq 0x40239
addq $0x30, %rsp
movq %rax, %r10
testq %r12, %r12
je 0x127b67
movq 0x18(%r12), %rax
movl 0x10(%r13), %ecx
movq (%rax,%rcx,8), %rcx
jmp 0x127b84
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r13), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rax
movl $0x80000000, %r12d      # imm = 0x80000000
movq (%rax), %r9
addq %rbp, %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq 0x68(%rsp), %r8
movq %r10, 0x28(%rsp)
pushq %r10
callq 0x3de56
addq $0x10, %rsp
testl %eax, %eax
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
je 0x127bf4
imulq $0xa8, 0x10(%rbx), %r10
movq (%rbx), %r11
leaq (%r11,%r10), %rdi
movq 0x18(%rsp), %rax
movl %eax, -0x78(%r11,%r10)
movq 0x68(%rsp), %rax
testq %rax, %rax
je 0x127c07
movq 0x18(%rax), %rdx
movl -0x98(%rdi), %esi
movq (%rdx,%rsi,8), %rsi
jmp 0x127c26
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r15, %rax
movq 0x8(%rsp), %r9
jmp 0x127de1
movq 0x28(%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 0x8(%rsp), %r9
movq 0x20(%rsp), %r8
movq (%r9), %rax
leaq (%rax,%rbp), %rdx
cmpl $0x0, -0x8c(%rdi)
movq %rbp, 0x88(%rsp)
je 0x127c62
movq %r11, 0x78(%rsp)
movq %r10, 0x80(%rsp)
cmpq $0x2, %r8
jae 0x127c8b
xorl %ebp, %ebp
jmp 0x127d53
testq %r8, %r8
je 0x127d9e
movq %r8, %rsi
movl 0x10(%rbx), %eax
andl %r13d, %eax
movl (%rdx), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%rdx)
addq $0x10, %rdx
decq %rsi
jne 0x127c6e
jmp 0x127d9e
movq %r8, %r9
shrq %r9
movq %r8, %r10
shlq $0x4, %r10
addq %rbp, %r10
addq %rax, %r10
addq $-0x10, %r10
movq %rdx, %r11
xorl %ebp, %ebp
movl (%r11), %eax
movl %eax, 0xe0(%rsp)
movq 0x4(%r11), %rax
movq %rax, 0xe4(%rsp)
movl 0xc(%r11), %eax
movl %eax, 0xec(%rsp)
movups (%r10), %xmm0
movups %xmm0, (%r11)
movl 0xe0(%rsp), %eax
movl %eax, (%r10)
movq 0xe4(%rsp), %rax
movq %rax, 0x4(%r10)
movl 0xec(%rsp), %eax
movl %eax, 0xc(%r10)
movl 0x4(%r11), %eax
movl 0x8(%r11), %ecx
notl %ecx
addl %r14d, %ecx
movl %ecx, 0x8(%r11)
notl %eax
addl %esi, %eax
movl %eax, 0x4(%r11)
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 0x10(%rbx), %eax
andl %r13d, %eax
movl (%r10), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%r10)
movl (%r11), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%r11)
incq %rbp
addq $-0x10, %r10
addq $0x10, %r11
cmpq %rbp, %r9
jne 0x127ca7
movq 0x20(%rsp), %r8
testb $0x1, %r8b
movq 0x8(%rsp), %r9
movq 0x80(%rsp), %r10
movq 0x78(%rsp), %r11
je 0x127d9e
shlq $0x4, %rbp
movl 0x4(%rdx,%rbp), %eax
movl 0x8(%rdx,%rbp), %ecx
notl %ecx
addl %r14d, %ecx
movl %ecx, 0x8(%rdx,%rbp)
notl %eax
addl %eax, %esi
movl %esi, 0x4(%rdx,%rbp)
movl 0x10(%rbx), %eax
andl %r13d, %eax
movl (%rdx,%rbp), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, (%rdx,%rbp)
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpl $0x0, 0x3c(%rsp)
je 0x127dd9
leaq (%r11,%r10), %rsi
addq $-0xa8, %rsi
addq $-0x70, %rdi
movq 0x88(%rsp), %rcx
addq (%r9), %rcx
movq 0x60(%rsp), %rdx
callq 0x3dc52
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %r9
addq %r8, 0x18(%rsp)
movq %r15, %rax
movq 0x30(%rsp), %r13
movq 0x58(%rsp), %r8
movl 0x14(%rsp), %r10d
incq %r15
cmpq %r8, %r15
jbe 0x127a55
jmp 0x127e0a
movq $0x0, 0x18(%rsp)
movq %rbp, %r9
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r9)
movq 0x10(%rbx), %rdi
movq 0x70(%rsp), %r15
cmpq %r15, %rdi
jbe 0x127f93
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0x40(%rsp)
movq (%rbx), %rsi
callq 0x121388
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x127ea5
movq (%rbx), %rcx
leal -0x1(%r14), %edx
addq $0x4, %rcx
movl (%rcx), %edi
movl 0x4(%rcx), %r8d
testl %edi, %edi
je 0x127e99
movl $0x3, %esi
cmpl %edx, %r8d
jb 0x127e6d
xorl %esi, %esi
testl %edi, %edi
setne %sil
movslq 0xd0(%rsp,%rsi,4), %rdi
incq %rdi
movl %edi, 0xd0(%rsp,%rsi,4)
cmpq %r15, %rdi
jne 0x127e8b
movl 0x20(%rcx), %edi
movl %edi, 0x40(%rsp,%rsi,4)
addq $0xa8, %rcx
decq %rax
jne 0x127e51
jmp 0x127ea5
movl $0x2, %esi
cmpl %edx, %r8d
jne 0x127e65
jmp 0x127e6d
cmpl $0x0, 0x40(%rsp)
jg 0x127ec5
cmpl $0x0, 0x44(%rsp)
jg 0x127ec5
cmpl $0x0, 0x48(%rsp)
jg 0x127ec5
cmpl $0x0, 0x4c(%rsp)
jle 0x127f93
cmpq $0x0, 0x10(%rbx)
je 0x127f8c
decl %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq (%rbx), %rax
movl 0x4(%rax,%rbp), %edx
movl 0x8(%rax,%rbp), %esi
testl %edx, %edx
je 0x127f79
movl $0x3, %ecx
cmpl %r14d, %esi
jb 0x127eff
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
movl 0x24(%rax,%rbp), %edx
cmpl 0x40(%rsp,%rcx,4), %edx
jl 0x127f63
cmpq %r15, %r12
je 0x127f60
leaq (%rax,%rbp), %rcx
movq %rcx, 0x8(%rsp)
imulq $0xa8, %r15, %r13
addq %rax, %r13
movl $0xa8, %edx
leaq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x2160
movq (%rbx), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0xe0(%rsp), %rsi
callq 0x2160
incq %r15
incq %r12
addq $0xa8, %rbp
cmpq 0x10(%rbx), %r12
jb 0x127edb
jmp 0x127f8f
movl $0x2, %ecx
cmpl %r14d, %esi
jne 0x127ef8
jmp 0x127eff
xorl %r15d, %r15d
movq %r15, 0x10(%rbx)
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x120acf