// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movsd %xmm3, 0x20(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r9, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %r12
movq %rdi, %r15
movq 0xc8(%rsp), %rbp
xorl %r13d, %r13d
movq %r13, 0x60(%rsp)
movq %r13, 0x68(%rsp)
movq %r13, 0x70(%rsp)
subq %rsi, %r12
jbe 0x12dfd8
movq 0x148(%rsp), %rax
movq 0xb0(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r14
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rbx
xorl %r13d, %r13d
movq 0x20(%r15), %rax
movq 0x30(%r15), %rsi
movl 0x8(%rsi,%rbx), %edi
addq %rbx, %rsi
shlq $0x4, %rdi
movq %r14, %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
pushq %rbp
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0x158(%rsp)
pushq (%rax,%rdi)
pushq 0x100(%rsp)
callq 0x12c19e
addq $0x30, %rsp
testq %rax, %rax
je 0x12dfcb
movq 0x30(%r15), %rcx
leaq (,%r13,8), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rcx,%rbx), %rdx
movq %rdx, 0x10(%rcx,%rax)
movupd (%rcx,%rbx), %xmm0
movupd %xmm0, (%rcx,%rax)
movq 0x30(%r15), %rcx
movl (%rcx,%rax), %esi
movl 0xc(%rcx,%rax), %edx
movl %esi, 0xc(%rcx,%rax)
testq %rbp, %rbp
je 0x12df7e
leaq (,%r13,2), %rsi
addq %r13, %rsi
movl 0xc(%rcx,%rsi,8), %edi
notl %edi
movq 0xc0(%rsp), %r8
addl %r8d, %edx
addl %edi, %edx
decl %edx
movl %edx, 0xc(%rcx,%rsi,8)
addq %rcx, %rax
movq 0x20(%r15), %rdx
leaq (,%r13,2), %rsi
addq %r13, %rsi
movl 0x8(%rcx,%rsi,8), %edi
shlq $0x4, %rdi
movq 0x8(%rdx,%rdi), %rdx
movq %rdx, %rdi
shrq $0x1c, %rdi
andl $0x7ffffff, %edi        # imm = 0x7FFFFFF
movl %edi, 0x8(%rcx,%rsi,8)
shlq $0x4, %rdx
movabsq $0x7ffffff00000000, %rcx # imm = 0x7FFFFFF00000000
andq %rcx, %rdx
movl 0xc(%rax), %ecx
orq %rdx, %rcx
movq %rcx, (%rax)
incq %r13
addq $0x18, %rbx
decq %r12
jne 0x12decb
movq 0x150(%rsp), %r8
cmpq $0x2, %r13
jb 0x12e006
movq 0x30(%r15), %rdi
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
movq 0x150(%rsp), %r8
movq 0xe8(%rsp), %rdi
movq (%r8), %rax
movq %rax, 0x8(%rsp)
testq %r13, %r13
je 0x12e151
movq 0x100(%rsp), %r9
movq 0xf8(%rsp), %r10
movq 0xb0(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll $0x1f, %ebp
orl %eax, %ebp
leaq (%r10,%r10), %rax
movq %rax, 0x48(%rsp)
leaq -0x1(,%r10,2), %rax
movq %rax, 0x40(%rsp)
movl $0x10, %r12d
movq %r13, %r14
movq %r13, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq (%rdi), %rax
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
leaq (%rax,%rcx), %rbx
movl %ebp, (%rax,%rcx)
movq 0x30(%r15), %rdx
movl -0x4(%rdx,%r12), %esi
movl %esi, 0x4(%rax,%rcx)
movl -0x8(%rdx,%r12), %esi
movl %esi, 0x8(%rax,%rcx)
movl (%rdx,%r12), %eax
shrl $0x8, %eax
cmpq %r10, %rax
setae %cl
cmpq %r9, %rax
setbe %dl
orb %cl, %dl
jne 0x12e0b8
movl $0x1, 0xc(%rbx)
jmp 0x12e11d
cmpq %r9, %rax
jbe 0x12e116
addq 0x40(%rsp), %rax
xorl %edx, %edx
divq 0x48(%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0xfba95(%rip), %xmm1    # 0x229b70
movq %rbp, %r13
movq %r9, %rbp
movq %r10, %r15
callq 0x23a0
movq %r15, %r10
movq 0x50(%rsp), %r15
movq %rbp, %r9
movq %r13, %rbp
movq 0x58(%rsp), %r13
movq 0x150(%rsp), %r8
movq 0xe8(%rsp), %rdi
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%rbx)
jmp 0x12e11d
movl $0x2, 0xc(%rbx)
cmpl $0x1000000, 0xc(%rbx)   # imm = 0x1000000
jb 0x12e12d
movl $0xffffff, 0xc(%rbx)    # imm = 0xFFFFFF
movl 0xc(%rbx), %eax
shll $0x8, %eax
movl %eax, 0xc(%rbx)
movq 0x30(%r15), %rcx
movzbl (%rcx,%r12), %ecx
orl %eax, %ecx
movl %ecx, 0xc(%rbx)
addq $0x18, %r12
decq %r14
jne 0x12e06a
movq 0x158(%rsp), %r15
testq %r13, %r13
sete %al
movq 0x38(%rsp), %r12
movl %r12d, %ecx
cmpq 0xb0(%rsp), %rcx
sete %cl
orb %al, %cl
jne 0x12e22f
movl 0xf0(%rsp), %eax
movq 0xe0(%rsp), %r14
movq 0x10(%r14), %rbx
movq (%r8), %rdx
movq 0x8(%rsp), %rsi
subq %rsi, %rdx
movq (%r15), %rcx
leaq 0x18(%rdi), %r8
movl 0x108(%rsp), %r10d
movl 0x118(%rsp), %r11d
movq 0xe8(%rsp), %rdi
movq %r14, %r9
movsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x28(%rsp), %xmm3
pushq $0x1
pushq %r11
pushq 0x120(%rsp)
pushq %r10
pushq %rax
pushq 0x160(%rsp)
pushq 0xf0(%rsp)
pushq 0x68(%rsp)
pushq %r12
pushq 0x178(%rsp)
pushq 0x178(%rsp)
pushq 0x178(%rsp)
callq 0x4287f
movq 0x148(%rsp), %rdi
addq $0x60, %rsp
addq (%r15), %rax
movq %rax, (%r15)
movq %rax, 0x8(%rdi)
movl $0x1, %eax
cmpq %rbx, 0x10(%r14)
ja 0x12e238
movq (%r15), %rax
movq %rax, 0x8(%rdi)
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq