// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: h_ec_lchain_re_chn(ha_abuf_s*, unsigned long, unsigned long, unsigned int, char*, unsigned long, unsigned long, char*, unsigned long, unsigned long, unsigned long, unsigned long, overlap_region_alloc*, Candidates_list*, double, int, unsigned long, unsigned long, unsigned int, long, double, unsigned int, long, long, long, long, double, double, tiny_queue_t*, asg16_v*, long*, long*)
uint64_t h_ec_lchain_re_chn(ha_abuf_t *ab, uint64_t si, uint64_t ei, uint32_t rid, char* rs, uint64_t rl, uint64_t tid, char* ts, uint64_t tl, uint64_t trev, uint64_t mz_w, uint64_t mz_k, overlap_region_alloc *olst, Candidates_list *cl, double bw_thres, 
								 int apend_be, uint64_t max_cnt, uint64_t min_cnt, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, tiny_queue_t *tq, asg16_v *scc, int64_t *n, int64_t *zn)
{
	// char dbg[768]; uint64_t rpos, rspan, thash;

	int64_t ok, nk, ck, n0; uint64_t i, m, tspan, on0; k_mer_hit *p;
	for (i = si, m = ok = nk = ck = 0; i < ei; i++) {
		if(!recalu_minimizer_non_retrieve(rid, &(ab->a[i]), &(scc[tid]), &ok, &nk, &ck, mz_k, ab->mz.a[ab->a[i].self_off].x, tq, ts, tl, trev)) continue;
		///debug
		// thash = ab->mz.a[ab->a[i].self_off].x;
		
		ab->a[m] = ab->a[i]; tspan = ab->a[m].other_off;
		ab->a[m].other_off = (uint32_t)ab->a[m].srt;
		if(trev) ab->a[m].other_off = tl - (ab->a[m].other_off+1-tspan) - 1;///looks like a bug
		ab->a[m].self_off = ab->mz.a[ab->a[m].self_off].pos;
		ab->a[m].srt = ab->a[m].self_off; ab->a[m].srt <<= 32; ab->a[m].srt |= ab->a[m].other_off;


		///debug
		// recover_UC_Read_sub_region(dbg, ab->a[m].other_off + 1 - tspan, tspan, trev, &R_INF, tid);
		// if(recalu_minimizer0(dbg, tspan, !(asm_opt.flag & HA_F_NO_HPC), mz_k, thash, tq, &rpos, &rspan) && (rpos + 1 == tspan) && (tspan == rspan)) {
		//  	// fprintf(stderr, "-0-[M::%s]\n", __func__);
		// } else {
		//  	fprintf(stderr, "-1-[M::%s]\ttrev::%lu\n", __func__, trev);
		// }
		///debug
		// hpc_ext_check(&R_INF, tid, ab->a[m].other_off + 1 - tspan, ab->a[m].other_off + 1, tl, trev, dbg);

		m++;
	}

	if(m > 1) radix_sort_ha_an1(ab->a, ab->a + m);

	for (i = 0, n0 = (*n); i < m; i++) {
		p = &cl->list[(*n)++];
		p->readID = tid;
		p->strand = trev;
		p->offset = ab->a[i].other_off;
		p->self_offset = ab->a[i].self_off;
		if(((ab->a[i].cnt>>8) < max_cnt) && ((ab->a[i].cnt>>8) > min_cnt)){
			p->cnt = 1;
		} else if((ab->a[i].cnt>>8) <= min_cnt) {
			p->cnt = 2;
		} else{
			p->cnt = 1 + (((ab->a[i].cnt>>8) + (max_cnt<<1) - 1)/(max_cnt<<1));
			p->cnt = pow(p->cnt, 1.1);
		}
		if(p->cnt > ((uint32_t)(0xffffffu))) p->cnt = 0xffffffu;
		p->cnt <<= 8; p->cnt |= (((uint32_t)(0xffu))&(ab->a[i].cnt));
	}

	// if(rid == 3196 && tid == 3199) fprintf(stderr, "[M::%s]\ttid::%lu\ttrev::%lu\told::%lu\tnew::%lu\n", __func__, tid, trev, k - l, m);
	// fprintf(stderr, "[M::%s]\ttid::%lu\ttrev::%lu\told::%lu\tnew::%lu\n", __func__, tid, trev, ei - si, m);

	if(m > 0 && tid != rid) {
		on0 = olst->length;
		*zn += lchain_qdp_mcopy_fast(cl, n0, (*n)-n0, *zn, &(cl->chainDP), olst, max_skip, max_iter, max_dis, chn_pen_gap, chn_pen_skip, bw_thres, 
																	rid, rl, tl, quick_check, apend_be, gen_off, enable_mcopy, mcopy_rate, mcopy_khit_cut, 1);
		cl->length = *zn;
		if(olst->length > on0) return 1;
	}

	cl->length = *zn;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm3, 0x28(%rsp)
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x30(%rsp)
movq %xmm0, 0x20(%rsp)
movq %r9, 0x38(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r12
movq %rdi, %r13
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
leaq 0x58(%rsp), %rax
andq $0x0, (%rax)
leaq 0x60(%rsp), %rax
andq $0x0, (%rax)
imulq $0x18, 0xa0(%rsp), %r14
addq 0x138(%rsp), %r14
movq 0xf0(%rsp), %r8
movq 0xe8(%rsp), %r9
movq 0xb8(%rsp), %rbx
movq 0xb0(%rsp), %r15
movq %rcx, 0x40(%rsp)
movl %ecx, %eax
movq %rax, 0x48(%rsp)
imulq $0x18, %rsi, %rbp
movq $0x0, (%rsp)
cmpq 0x8(%rsp), %r12
jae 0xedae2
movq 0x20(%r13), %rax
movq 0x30(%r13), %rsi
movl 0x8(%rsi,%rbp), %edi
addq %rbp, %rsi
shlq $0x4, %rdi
movq %r14, %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x50(%rsp), %r9
pushq %rbx
pushq %r15
pushq 0xb8(%rsp)
pushq 0x148(%rsp)
pushq (%rax,%rdi)
pushq 0xf0(%rsp)
callq 0xec0f2
addq $0x30, %rsp
testq %rax, %rax
je 0xedac6
movq 0x30(%r13), %rcx
imulq $0x18, (%rsp), %rax
movq 0x10(%rcx,%rbp), %rdx
movq %rdx, 0x10(%rcx,%rax)
movdqu (%rcx,%rbp), %xmm0
movdqu %xmm0, (%rcx,%rax)
movq 0x30(%r13), %rdx
movl (%rdx,%rax), %ecx
movl 0xc(%rdx,%rax), %esi
movl %ecx, 0xc(%rdx,%rax)
testq %rbx, %rbx
je 0xeda76
notl %ecx
addl %r15d, %esi
addl %esi, %ecx
decl %ecx
movl %ecx, 0xc(%rdx,%rax)
movq 0xe8(%rsp), %r9
leaq (%rdx,%rax), %rsi
movq 0x20(%r13), %rdi
movl 0x8(%rdx,%rax), %r8d
shlq $0x4, %r8
movq 0x8(%rdi,%r8), %rdi
movq %rdi, %r8
shrq $0x1c, %r8
andl $0x7ffffff, %r8d        # imm = 0x7FFFFFF
movl %r8d, 0x8(%rdx,%rax)
shlq $0x4, %rdi
movabsq $0x7ffffff00000000, %rax # imm = 0x7FFFFFF00000000
andq %rax, %rdi
movl %ecx, %eax
orq %rdi, %rax
movq %rax, (%rsi)
incq (%rsp)
jmp 0xedace
movq 0xe8(%rsp), %r9
incq %r12
addq $0x18, %rbp
movq 0xf0(%rsp), %r8
jmp 0xed9db
movq (%rsp), %r15
cmpq $0x2, %r15
jb 0xedb0c
movq 0x30(%r13), %rdi
imulq $0x18, %r15, %rsi
addq %rdi, %rsi
callq 0xe1068
movq 0xe8(%rsp), %r9
movq 0xf0(%rsp), %r8
movq 0x140(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll $0x1f, %ebx
orl %eax, %ebx
leaq (%r9,%r9), %rax
movq %rax, 0x8(%rsp)
leaq -0x1(,%r9,2), %rbp
pushq $0x10
popq %r14
subq $0x1, %r15
jb 0xedc37
movq 0xd8(%rsp), %rax
movq (%rax), %rcx
movq 0x140(%rsp), %rsi
movq (%rsi), %rdx
leaq 0x1(%rdx), %rax
movq %rax, (%rsi)
shlq $0x4, %rdx
leaq (%rcx,%rdx), %r12
movl %ebx, (%rcx,%rdx)
movq 0x30(%r13), %rax
movq -0x8(%rax,%r14), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%rcx,%rdx)
movl (%rax,%r14), %eax
shrl $0x8, %eax
cmpq %r9, %rax
setae %sil
cmpq %r8, %rax
seta %dil
testb %dil, %sil
jne 0xedbcc
cmpq %r8, %rax
seta %sil
cmpq %r9, %rax
setb %al
andb %sil, %al
movzbl %al, %esi
pushq $0x2
popq %rax
subl %esi, %eax
movl %eax, 0xc(%r12)
leaq (%rcx,%rdx), %r12
addq $0xc, %r12
jmp 0xedc1c
addq %rbp, %rax
xorl %edx, %edx
divq 0x8(%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0xfa083(%rip), %xmm1    # 0x1e7c68
callq 0x23a0
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r12)
addq $0xc, %r12
cmpl $0x1000000, %eax        # imm = 0x1000000
jb 0xedc0c
movl $0xffffff, (%r12)       # imm = 0xFFFFFF
movl $0xffffff, %eax         # imm = 0xFFFFFF
movq 0xf0(%rsp), %r8
movq 0xe8(%rsp), %r9
shll $0x8, %eax
movq 0x30(%r13), %rcx
movzbl (%rcx,%r14), %ecx
orl %eax, %ecx
movl %ecx, (%r12)
addq $0x18, %r14
jmp 0xedb43
cmpq $0x0, (%rsp)
sete %al
movq 0x48(%rsp), %rcx
cmpq 0xa0(%rsp), %rcx
sete %cl
orb %al, %cl
movq 0x148(%rsp), %r14
movq (%r14), %rax
je 0xedc6b
movq 0xd8(%rsp), %r15
jmp 0xedd2a
movq 0xd0(%rsp), %r12
movq 0x10(%r12), %rbx
movq 0x140(%rsp), %rcx
movq (%rcx), %rdx
movq 0x10(%rsp), %rsi
subq %rsi, %rdx
movq 0xd8(%rsp), %r15
leaq 0x18(%r15), %r8
movl 0xf8(%rsp), %r10d
movl 0x108(%rsp), %r11d
movq %r15, %rdi
movq %rax, %rcx
movq %r12, %r9
movq 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movsd 0x30(%rsp), %xmm3
pushq $0x1
pushq %r11
pushq 0x110(%rsp)
pushq %r10
movl 0x100(%rsp), %eax
pushq %rax
pushq 0x150(%rsp)
pushq 0xe0(%rsp)
pushq 0x70(%rsp)
pushq 0x80(%rsp)
pushq 0x168(%rsp)
pushq 0x168(%rsp)
pushq 0x168(%rsp)
callq 0x1609d
addq $0x60, %rsp
addq (%r14), %rax
movq %rax, (%r14)
movq %rax, 0x8(%r15)
cmpq %rbx, 0x10(%r12)
jbe 0xedd2a
pushq $0x1
popq %rax
jmp 0xedd30
movq %rax, 0x8(%r15)
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq