// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/anchor.cpp
// Function: lchain_qgen_mcopy_fast_re0(ha_abuf_s*, ha_pt_s*, ha_mz1_t*, unsigned long, ha_mz1_t*, unsigned long, unsigned long, Candidates_list*, unsigned int*, unsigned int*)
uint64_t lchain_qgen_mcopy_fast_re0(ha_abuf_t *ab, ha_pt_t *ha_idx, ha_mz1_t *ra, uint64_t rn, ha_mz1_t *qa, uint64_t qn, uint64_t qid, Candidates_list *cl, uint32_t *high_occ, uint32_t *low_occ)
{
	// fprintf(stderr, "+[M::%s]\n", __func__);
	uint64_t i, k, l, max_cnt = UINT32_MAX, min_cnt = 0, ri, qi, sn; anchor1_t *an; k_mer_hit *p;
	if(high_occ) {
		max_cnt = (*high_occ);
		if(max_cnt < 2) max_cnt = 2;
	}
	if(low_occ) {
		min_cnt = (*low_occ);
		if(min_cnt < 2) min_cnt = 2;
	}
	// clear_Candidates_list(cl); 

	///first try
	for (k = 1, l = i = ab->n_a = 0; k <= rn; ++k) {
		if (k == rn || ra[k].x != ra[l].x) {
			for (; i < qn && qa[i].x < ra[l].x; i++);
			if(i < qn && qa[i].x == ra[l].x) {
				sn = 0;
				if(ab->n_a < ab->m_a) sn = ab->seed[l].n;///ha_pt_cnt(ha_idx, ra[l].x);
							
				for (qi = i; qi < qn && qa[qi].x == ra[l].x; qi++) {
					for (ri = l; ri < k; ri++) {
						if(qa[qi].rev != ra[ri].rev) continue;
						if(ab->n_a < ab->m_a) {
							an = &(ab->a[ab->n_a++]); 

							an->other_off = qa[qi].pos;
							an->self_off = ra[ri].pos;
							///an->cnt: cnt<<8|span
							an->cnt = sn; if(an->cnt > ((uint32_t)(0xffffffu))) an->cnt = 0xffffffu;
							an->cnt <<= 8; an->cnt |= ((ra[ri].span <= ((uint32_t)(0xffu)))?ra[ri].span:((uint32_t)(0xffu)));
							an->srt = (((uint64_t)(an->self_off))<<32)|((uint64_t)(an->other_off));
						} else {
							ab->n_a++;
						}
					}
				}
			}
			l = k;
		}
	}

	
	if (ab->n_a > ab->m_a) {
		ab->m_a = ab->n_a; REALLOC(ab->a, ab->m_a);

		for (k = 1, l = i = ab->n_a = 0; k <= rn; ++k) {
			if (k == rn || ra[k].x != ra[l].x) {
				for (; i < qn && qa[i].x < ra[l].x; i++);
				if(i < qn && qa[i].x == ra[l].x) {
					sn = ab->seed[l].n;///ha_pt_cnt(ha_idx, ra[l].x);
					
					for (qi = i; qi < qn && qa[qi].x == ra[l].x; qi++) {
						for (ri = l; ri < k; ri++) {
							if(qa[qi].rev != ra[ri].rev) continue;

							an = &(ab->a[ab->n_a++]); 

							an->other_off = qa[qi].pos;
							an->self_off = ra[ri].pos;
							///an->cnt: cnt<<8|span
							an->cnt = sn; if(an->cnt > ((uint32_t)(0xffffffu))) an->cnt = 0xffffffu;
							an->cnt <<= 8; an->cnt |= ((ra[ri].span <= ((uint32_t)(0xffu)))?ra[ri].span:((uint32_t)(0xffu)));
							an->srt = (((uint64_t)(an->self_off))<<32)|((uint64_t)(an->other_off));
						}
					}
				}
				l = k;
			}
		}
	}

	// copy over to _cl_
	sn = ab->n_a + cl->length;
	if (sn > (uint64_t)cl->size) {
		cl->size = sn;
		REALLOC(cl->list, cl->size);
	}

	radix_sort_ha_an1(ab->a, ab->a + ab->n_a);
	for (i = 0; i < ab->n_a; i++) {
		p = &cl->list[cl->length++];
		p->readID = qid;
		p->strand = 0;
		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));
	}
	// cl->length = ab->n_a;
	return ab->n_a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, (%rsp)
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rcx
testq %rcx, %rcx
je 0x12bfdd
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x40(%rsp)
jmp 0x12bfe7
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x40(%rsp)
testq %rax, %rax
je 0x12c000
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x58(%rsp)
jmp 0x12c009
movq $0x0, 0x58(%rsp)
movq (%rsp), %rax
movq $0x0, (%rax)
testq %r14, %r14
je 0x12c443
leaq 0x1(%r14), %rax
cmpq $0x3, %rax
movl $0x2, %edi
movq %rax, 0x20(%rsp)
cmovaeq %rax, %rdi
movabsq $0x80000000000000, %r12 # imm = 0x80000000000000
leaq 0x8(%r15), %rax
movq %rax, 0x60(%rsp)
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r10d, %r10d
movq %r9, 0x50(%rsp)
movq %r14, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq %rdi, 0x38(%rsp)
cmpq %r14, %rdx
je 0x12c088
movq %rdx, %rax
shlq $0x4, %rax
movq (%r15,%rax), %rax
movq %r10, %rsi
shlq $0x4, %rsi
cmpq (%r15,%rsi), %rax
je 0x12c229
cmpq %r9, %r8
jae 0x12c226
movq %r10, %rax
shlq $0x4, %rax
movq %rax, 0x10(%rsp)
movq (%r15,%rax), %rsi
movq %r8, %rax
shlq $0x4, %rax
addq 0x8(%rsp), %rax
cmpq %rsi, (%rax)
jae 0x12c0c9
incq %r8
addq $0x10, %rax
cmpq %r8, %r9
jne 0x12c0ad
movq %rdx, %r10
movq %r9, %r8
jmp 0x12c229
jne 0x12c226
movq %rsi, 0x18(%rsp)
movq (%rsp), %rax
movq 0x8(%rax), %r11
xorl %r13d, %r13d
cmpq %r11, %rcx
jae 0x12c0f5
movq (%rsp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rsp), %rsi
movl (%rax,%rsi), %r13d
cmpq %r9, %r8
jae 0x12c226
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r13d
cmovael %eax, %r13d
shll $0x8, %r13d
movq 0x10(%rsp), %rax
addq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r8, %r15
movq %r10, 0x48(%rsp)
movq %r15, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
cmpq %rsi, (%rdi,%rax)
jne 0x12c212
cmpq %rdx, %r10
jae 0x12c1fc
movq %r10, %r9
addq 0x8(%rsp), %rax
movq 0x10(%rsp), %r10
movq (%r10), %rdi
xorq 0x8(%rax), %rdi
testq %r12, %rdi
jne 0x12c1ec
cmpq %r11, %rcx
jae 0x12c1e2
movq (%rsp), %rbx
movq 0x30(%rbx), %rbp
leaq (%rcx,%rcx,2), %rdi
incq %rcx
movq %rcx, (%rbx)
movq 0x8(%rax), %r14
shrq $0x1c, %r14
movl %r14d, %r12d
movl $0x7ffffff, %esi        # imm = 0x7FFFFFF
andl %esi, %r12d
movl %r12d, 0xc(%rbp,%rdi,8)
movq (%r10), %r12
shrq $0x1c, %r12
movl %r12d, %ebx
andl %esi, %ebx
movl %ebx, 0x8(%rbp,%rdi,8)
movl %r13d, 0x10(%rbp,%rdi,8)
movzbl 0x7(%r10), %ebx
orl %r13d, %ebx
movl %ebx, 0x10(%rbp,%rdi,8)
shlq $0x20, %r12
movabsq $0x7ffffff00000000, %rbx # imm = 0x7FFFFFF00000000
andq %rbx, %r12
andl $0x7ffffff, %r14d       # imm = 0x7FFFFFF
orq %r12, %r14
movabsq $0x80000000000000, %r12 # imm = 0x80000000000000
movq %r14, (%rbp,%rdi,8)
jmp 0x12c1ec
incq %rcx
movq (%rsp), %rdi
movq %rcx, (%rdi)
incq %r9
addq $0x10, %r10
cmpq %r9, %rdx
jne 0x12c156
incq %r15
movq 0x50(%rsp), %r9
cmpq %r9, %r15
movq 0x48(%rsp), %r10
jb 0x12c125
movq %rdx, %r10
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %rdi
jmp 0x12c229
movq %rdx, %r10
incq %rdx
cmpq %rdi, %rdx
jne 0x12c067
movq (%rsp), %rax
cmpq 0x8(%rax), %rcx
jbe 0x12c445
movq (%rsp), %rbx
movq %rcx, 0x8(%rbx)
movq 0x30(%rbx), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
movq $0x0, (%rbx)
testq %r14, %r14
je 0x12c443
movq 0x20(%rsp), %rcx
cmpq $0x3, %rcx
movl $0x2, %r10d
cmovaeq %rcx, %r10
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r9d, %r9d
xorl %edx, %edx
movq 0x50(%rsp), %r8
movq %r10, 0x20(%rsp)
movl $0x7ffffff, %r13d       # imm = 0x7FFFFFF
cmpq %r14, %rsi
movq %rdx, 0x18(%rsp)
je 0x12c2cc
movq %rsi, %rdx
shlq $0x4, %rdx
movq (%r15,%rdx), %rdx
movq 0x18(%rsp), %rdi
shlq $0x4, %rdi
cmpq (%r15,%rdi), %rdx
movq 0x18(%rsp), %rdx
je 0x12c435
cmpq %r8, %r9
jae 0x12c432
shlq $0x4, %rdx
movq %rdx, %r11
movq (%r15,%rdx), %rbx
movq %r9, %rdx
shlq $0x4, %rdx
addq 0x8(%rsp), %rdx
cmpq %rbx, (%rdx)
jae 0x12c308
incq %r9
addq $0x10, %rdx
cmpq %r9, %r8
jne 0x12c2ec
movq %rsi, %rdx
movq %r8, %r9
jmp 0x12c435
setne %dl
cmpq %r8, %r9
setae %dil
orb %dl, %dil
jne 0x12c432
movq (%rsp), %rdx
movq 0x28(%rdx), %rdx
movl (%rdx,%r11), %edi
movl $0xffffff, %edx         # imm = 0xFFFFFF
cmpl %edx, %edi
cmovael %edx, %edi
shll $0x8, %edi
movl %edi, 0x10(%rsp)
addq 0x60(%rsp), %r11
movq %r11, 0x38(%rsp)
movq %r9, %rdx
movq %rbx, 0x48(%rsp)
movq %rdx, %r15
shlq $0x4, %r15
movq 0x8(%rsp), %rdi
cmpq %rbx, (%rdi,%r15)
jne 0x12c41e
movq 0x18(%rsp), %r10
cmpq %rsi, %r10
jae 0x12c408
addq 0x8(%rsp), %r15
movq 0x8(%r15), %r11
movq 0x38(%rsp), %rbp
movq (%rbp), %rdi
xorq %r11, %rdi
testq %r12, %rdi
jne 0x12c3f8
leaq (%rcx,%rcx,2), %rdi
incq %rcx
movq (%rsp), %r8
movq %rcx, (%r8)
movq 0x8(%r15), %r11
movq %r11, %rbx
shrq $0x1c, %rbx
movl %ebx, %r14d
andl %r13d, %r14d
movl %r14d, 0xc(%rax,%rdi,8)
movq (%rbp), %r14
movq %r14, %r12
shrq $0x1c, %r12
movl %r12d, %r8d
andl %r13d, %r8d
movl %r8d, 0x8(%rax,%rdi,8)
shrq $0x38, %r14
orl 0x10(%rsp), %r14d
movl %r14d, 0x10(%rax,%rdi,8)
shlq $0x20, %r12
movabsq $0x7ffffff00000000, %r8 # imm = 0x7FFFFFF00000000
andq %r8, %r12
andl $0x7ffffff, %ebx        # imm = 0x7FFFFFF
orq %r12, %rbx
movabsq $0x80000000000000, %r12 # imm = 0x80000000000000
movq %rbx, (%rax,%rdi,8)
incq %r10
addq $0x10, %rbp
cmpq %r10, %rsi
jne 0x12c37c
incq %rdx
movq 0x50(%rsp), %r8
cmpq %r8, %rdx
movq 0x48(%rsp), %rbx
jb 0x12c34a
movq %rsi, %rdx
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r10
jmp 0x12c435
movq %rsi, %rdx
incq %rsi
cmpq %r10, %rsi
jne 0x12c29f
jmp 0x12c445
xorl %ecx, %ecx
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rsi
addq %rcx, %rsi
cmpq 0x10(%rax), %rsi
jbe 0x12c47c
movq 0xa8(%rsp), %r14
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r14)
movq (%rsp), %rax
movq (%rax), %rcx
movq (%rsp), %rbx
movq 0x30(%rbx), %rdi
leaq (%rcx,%rcx,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
cmpq $0x0, (%rbx)
je 0x12c5aa
movq 0xa0(%rsp), %r14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rax
leaq (%rax,%rax), %rbx
leaq -0x1(,%rax,2), %r12
movl $0x10, %r13d
xorl %ebp, %ebp
movq 0xa8(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsi)
shlq $0x4, %rcx
leaq (%rax,%rcx), %r15
movl %r14d, (%rax,%rcx)
movq (%rsp), %rdx
movq 0x30(%rdx), %rdx
movq -0x8(%rdx,%r13), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%rax,%rcx)
movl (%rdx,%r13), %edx
shrl $0x8, %edx
cmpl %edx, 0x40(%rsp)
ja 0x12c556
cmpl %edx, 0x58(%rsp)
jae 0x12c556
leaq (%r12,%rdx), %rax
xorl %edx, %edx
divq %rbx
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x10983a(%rip), %xmm1   # 0x235d68
callq 0x23a0
cvttsd2si %xmm0, %rdx
movl %edx, 0xc(%r15)
addq $0xc, %r15
cmpl $0x1000000, %edx        # imm = 0x1000000
jb 0x12c580
movl $0xffffff, (%r15)       # imm = 0xFFFFFF
movl $0xffffff, %edx         # imm = 0xFFFFFF
jmp 0x12c580
cmpq %rdx, 0x58(%rsp)
setb %sil
cmpq %rdx, 0x40(%rsp)
seta %dl
andb %sil, %dl
movzbl %dl, %esi
movl $0x2, %edx
subl %esi, %edx
movl %edx, 0xc(%r15)
leaq (%rax,%rcx), %r15
addq $0xc, %r15
shll $0x8, %edx
movq (%rsp), %rcx
movq 0x30(%rcx), %rax
movzbl (%rax,%r13), %eax
orl %edx, %eax
movl %eax, (%r15)
incq %rbp
movq (%rcx), %rax
addq $0x18, %r13
cmpq %rax, %rbp
jb 0x12c4c3
jmp 0x12c5ac
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq