// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movsd %xmm3, 0x18(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x20(%rsp)
movq %xmm0, 0x10(%rsp)
movq %r9, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %rbp
movq 0x138(%rsp), %rcx
movq 0xc8(%rsp), %r8
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
subq %rsi, %rbp
jbe 0x134433
movq %rdi, %r13
movq 0x128(%rsp), %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r12
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rbx
movq 0xa8(%rsp), %r15
xorl %r14d, %r14d
movq 0x20(%r13), %rax
movq 0x30(%r13), %rsi
movl 0x8(%rsi,%rbx), %edi
addq %rbx, %rsi
shlq $0x4, %rdi
movq %r12, %rdx
leaq 0x50(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq %r15
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x138(%rsp)
pushq (%rax,%rdi)
pushq 0xe0(%rsp)
callq 0x1321eb
addq $0x30, %rsp
testq %rax, %rax
je 0x1341bd
movq 0x30(%r13), %rcx
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rcx,%rbx), %rdx
movq %rdx, 0x10(%rcx,%rax)
movdqu (%rcx,%rbx), %xmm0
movdqu %xmm0, (%rcx,%rax)
movq 0x30(%r13), %rdx
movl (%rdx,%rax), %ecx
movl 0xc(%rdx,%rax), %esi
movl %ecx, 0xc(%rdx,%rax)
testq %r15, %r15
je 0x134175
leaq (%r14,%r14,2), %rdi
notl %ecx
movq 0xa0(%rsp), %r8
addl %r8d, %esi
addl %esi, %ecx
decl %ecx
movl %ecx, 0xc(%rdx,%rdi,8)
addq %rdx, %rax
movq 0x20(%r13), %rsi
leaq (%r14,%r14,2), %rdi
movl 0x8(%rdx,%rdi,8), %r8d
shlq $0x4, %r8
movq 0x8(%rsi,%r8), %rsi
movq %rsi, %r8
shrq $0x1c, %r8
andl $0x7ffffff, %r8d        # imm = 0x7FFFFFF
movl %r8d, 0x8(%rdx,%rdi,8)
shlq $0x4, %rsi
movabsq $0x7ffffff00000000, %rdx # imm = 0x7FFFFFF00000000
andq %rdx, %rsi
movl %ecx, %ecx
orq %rsi, %rcx
movq %rcx, (%rax)
incq %r14
addq $0x18, %rbx
decq %rbp
jne 0x1340cc
cmpq $0x2, %r14
jb 0x1341f3
movq 0x30(%r13), %rdi
leaq (%r14,%r14,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq 0xc8(%rsp), %r8
movq 0xd8(%rsp), %rdx
jmp 0x13421a
testq %r14, %r14
movl $0x1, %r14d
movq 0xc8(%rsp), %r8
movq 0x138(%rsp), %rcx
movq 0xd8(%rsp), %rdx
je 0x134433
movq 0x130(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
movq 0x90(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll $0x1f, %r15d
orl %eax, %r15d
leaq (%rdx,%rdx), %rax
movq %rax, 0x38(%rsp)
leaq -0x1(,%rdx,2), %r12
movl $0x10, %ebp
movq 0xe0(%rsp), %rdi
movq (%r8), %rcx
movq 0x130(%rsp), %rsi
movq (%rsi), %rdx
leaq 0x1(%rdx), %rax
movq %rax, (%rsi)
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rbx
movl %r15d, (%rcx,%rdx)
movq 0x30(%r13), %rax
movq -0x8(%rax,%rbp), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%rcx,%rdx)
movl (%rax,%rbp), %eax
shrl $0x8, %eax
movq 0xd8(%rsp), %r10
cmpq %r10, %rax
setae %sil
cmpq %rdi, %rax
movq %rdi, %r9
seta %dil
testb %dil, %sil
jne 0x1342df
cmpq %r9, %rax
seta %sil
cmpq %r10, %rax
setb %al
andb %sil, %al
movzbl %al, %esi
movl $0x2, %eax
subl %esi, %eax
movl %eax, 0xc(%rbx)
leaq (%rcx,%rdx), %rbx
addq $0xc, %rbx
movq %r9, %rdi
jmp 0x13432e
addq %r12, %rax
xorl %edx, %edx
divq 0x38(%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x101a6d(%rip), %xmm1   # 0x235d68
callq 0x23a0
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%rbx)
addq $0xc, %rbx
cmpl $0x1000000, %eax        # imm = 0x1000000
jb 0x13431e
movl $0xffffff, (%rbx)       # imm = 0xFFFFFF
movl $0xffffff, %eax         # imm = 0xFFFFFF
movq 0xc8(%rsp), %r8
movq 0xe0(%rsp), %rdi
shll $0x8, %eax
movq 0x30(%r13), %rcx
movzbl (%rcx,%rbp), %ecx
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x18, %rbp
decq %r14
jne 0x13425b
movq 0x30(%rsp), %r15
movl %r15d, %ecx
movq 0x138(%rsp), %r14
movq (%r14), %rax
cmpq 0x90(%rsp), %rcx
je 0x134436
movq 0xc0(%rsp), %r12
movq 0x10(%r12), %rbx
movq 0x130(%rsp), %rcx
movq (%rcx), %rdx
movq (%rsp), %rsi
subq %rsi, %rdx
movq 0xc8(%rsp), %rcx
leaq 0x18(%rcx), %r8
movl 0xe8(%rsp), %r10d
movl 0xf8(%rsp), %r11d
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movq %r12, %r9
movq 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
pushq $0x1
pushq %r11
pushq 0x100(%rsp)
pushq %r10
movl 0xf0(%rsp), %eax
pushq %rax
pushq 0x140(%rsp)
pushq 0xd0(%rsp)
pushq 0x60(%rsp)
pushq %r15
pushq 0x158(%rsp)
pushq 0x158(%rsp)
pushq 0x158(%rsp)
callq 0x42094
movq 0x128(%rsp), %r8
addq $0x60, %rsp
addq (%r14), %rax
movq %rax, (%r14)
movq %rax, 0x8(%r8)
movl $0x1, %ecx
cmpq %rbx, 0x10(%r12)
jbe 0x134436
jmp 0x13443c
movq (%rcx), %rax
movq %rax, 0x8(%r8)
xorl %ecx, %ecx
movq %rcx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq