// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, %r14
movq %rcx, %r15
movq %rdx, 0x28(%rsp)
movq %rdi, %rsi
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
pushq $0x2
popq %rdx
testq %rcx, %rcx
je 0xe6e88
movl (%rcx), %ecx
cmpl $0x3, %ecx
cmovbl %edx, %ecx
jmp 0xe6e8d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x48(%rsp)
testq %rax, %rax
je 0xe6ea6
movl (%rax), %eax
cmpl $0x3, %eax
cmovbl %edx, %eax
movq %rax, 0x58(%rsp)
jmp 0xe6eaf
movq $0x0, 0x58(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x50(%rsp)
andq $0x0, (%rsi)
movabsq $0x80000000000000, %r12 # imm = 0x80000000000000
movq 0x28(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x70(%rsp)
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
movq %rsi, 0x10(%rsp)
movq %r14, 0x30(%rsp)
movq %r8, 0x38(%rsp)
movq %r15, 0x40(%rsp)
cmpq %r15, %rax
ja 0xe70cb
je 0xe6f2a
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x28(%rsp), %r9
movq (%r9,%rdx), %rdx
movq 0x8(%rsp), %r8
shlq $0x4, %r8
cmpq (%r9,%r8), %rdx
je 0xe70c3
movq 0x8(%rsp), %r9
shlq $0x4, %r9
cmpq %r14, %rdi
movq %r14, %rdx
cmovaq %rdi, %rdx
movq 0x28(%rsp), %r8
movq %r9, 0x20(%rsp)
addq %r9, %r8
movq %rdi, %r9
shlq $0x4, %r9
addq 0x38(%rsp), %r9
cmpq %r14, %rdi
jae 0xe6f6c
movq (%r9), %r10
cmpq (%r8), %r10
jae 0xe6f79
incq %rdi
addq $0x10, %r9
jmp 0xe6f56
movq %rax, 0x8(%rsp)
movq %rdx, %rdi
jmp 0xe70c3
movq 0x38(%rsp), %r8
jne 0xe70a1
movq %rdi, 0x18(%rsp)
movq 0x8(%rsi), %r11
xorl %edx, %edx
cmpq %r11, %rcx
jae 0xe6fa0
movq 0x28(%rsi), %rdx
movq 0x20(%rsp), %rdi
movl (%rdx,%rdi), %edx
movl $0xffffff, %r9d         # imm = 0xFFFFFF
cmpl %r9d, %edx
cmovael %r9d, %edx
shll $0x8, %edx
movq 0x20(%rsp), %rdi
addq 0x70(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
jae 0xe70a8
movq %rdi, %r13
shlq $0x4, %r13
cmpq %r10, (%r8,%r13)
jne 0xe70af
movq %rdi, 0x60(%rsp)
addq %r8, %r13
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
cmpq %rax, %r8
jae 0xe708a
movq (%r9), %r14
xorq 0x8(%r13), %r14
testq %r12, %r14
jne 0xe707e
cmpq %r11, %rcx
jae 0xe7078
movq 0x30(%rsi), %rbp
imulq $0x18, %rcx, %r14
incq %rcx
movq %rcx, (%rsi)
movq 0x8(%r13), %rsi
shrq $0x1c, %rsi
movl %esi, %r15d
movl $0x7ffffff, %edi        # imm = 0x7FFFFFF
andl %edi, %r15d
movl %r15d, 0xc(%rbp,%r14)
movq (%r9), %r15
shrq $0x1c, %r15
movl %r15d, %ebx
andl %edi, %ebx
movl %ebx, 0x8(%rbp,%r14)
movl %edx, 0x10(%rbp,%r14)
movzbl 0x7(%r9), %ebx
orl %edx, %ebx
movl %ebx, 0x10(%rbp,%r14)
shlq $0x20, %r15
movabsq $0x7ffffff00000000, %rbx # imm = 0x7FFFFFF00000000
andq %rbx, %r15
andl $0x7ffffff, %esi        # imm = 0x7FFFFFF
orq %r15, %rsi
movq %rsi, (%rbp,%r14)
movq 0x10(%rsp), %rsi
jmp 0xe707e
incq %rcx
movq %rcx, (%rsi)
incq %r8
addq $0x10, %r9
jmp 0xe6ff0
movq 0x60(%rsp), %rdi
incq %rdi
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r8
jmp 0xe6fc4
movq %rax, 0x8(%rsp)
jmp 0xe70c3
movq %rax, 0x8(%rsp)
jmp 0xe70b9
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x18(%rsp), %rdi
incq %rax
jmp 0xe6efc
cmpq 0x8(%rsi), %rcx
jbe 0xe72a7
movq %rcx, 0x8(%rsi)
movq 0x30(%rsi), %rdi
movq %rsi, %rbx
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
andq $0x0, (%rbx)
leaq 0x1(%r15), %rbx
pushq $0x1
popq %rsi
xorl %ecx, %ecx
movl $0x7ffffff, %r8d        # imm = 0x7FFFFFF
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movq %rbx, 0x68(%rsp)
cmpq %rbx, %rsi
je 0xe72a7
cmpq %r15, %rsi
je 0xe7142
movq %rsi, %rdx
shlq $0x4, %rdx
movq 0x28(%rsp), %r9
movq (%r9,%rdx), %rdx
movq 0x8(%rsp), %rdi
shlq $0x4, %rdi
cmpq (%r9,%rdi), %rdx
je 0xe729f
movq 0x8(%rsp), %r9
shlq $0x4, %r9
cmpq %r14, %r13
movq %r14, %rdx
cmovaq %r13, %rdx
movq 0x28(%rsp), %rdi
movq %r9, %rbp
leaq (%rdi,%r9), %r10
movq %r13, %r11
shlq $0x4, %r11
addq 0x38(%rsp), %r11
cmpq %r14, %r13
jae 0xe7183
movq (%r11), %rdi
cmpq (%r10), %rdi
jae 0xe7190
incq %r13
addq $0x10, %r11
jmp 0xe716d
movq %rsi, 0x8(%rsp)
movq %rdx, %r13
jmp 0xe729f
jne 0xe727f
movq 0x10(%rsp), %rdx
movq 0x28(%rdx), %rdx
movq %r13, %r9
movl (%rdx,%rbp), %r13d
movl $0xffffff, %edx         # imm = 0xFFFFFF
cmpl %edx, %r13d
cmovael %edx, %r13d
shll $0x8, %r13d
addq 0x70(%rsp), %rbp
movq %rbp, 0x20(%rsp)
movq %r9, 0x18(%rsp)
cmpq 0x30(%rsp), %r9
jae 0xe7286
movq %r9, %r15
shlq $0x4, %r15
movq 0x38(%rsp), %rdx
cmpq %rdi, (%rdx,%r15)
jne 0xe7286
movq %r9, 0x60(%rsp)
addq %rdx, %r15
movq 0x20(%rsp), %r11
movq 0x8(%rsp), %r10
cmpq %rsi, %r10
jae 0xe7272
movq (%r11), %rbx
xorq 0x8(%r15), %rbx
testq %r12, %rbx
jne 0xe7269
imulq $0x18, %rcx, %rbp
incq %rcx
movq 0x10(%rsp), %rdx
movq %rcx, (%rdx)
movq 0x8(%r15), %rbx
shrq $0x1c, %rbx
movl %ebx, %r14d
andl %r8d, %r14d
movl %r14d, 0xc(%rax,%rbp)
movq (%r11), %rdx
movq %rdx, %r14
shrq $0x1c, %r14
movl %r14d, %r9d
andl %r8d, %r9d
movl %r9d, 0x8(%rax,%rbp)
shrq $0x38, %rdx
orl %r13d, %edx
movl %edx, 0x10(%rax,%rbp)
shlq $0x20, %r14
movabsq $0x7ffffff00000000, %rdx # imm = 0x7FFFFFF00000000
andq %rdx, %r14
andl $0x7ffffff, %ebx        # imm = 0x7FFFFFF
orq %r14, %rbx
movq %rbx, (%rax,%rbp)
incq %r10
addq $0x10, %r11
jmp 0xe71f8
movq 0x60(%rsp), %r9
incq %r9
jmp 0xe71c5
movq %rsi, 0x8(%rsp)
jmp 0xe729f
movq %rsi, 0x8(%rsp)
movq 0x30(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x68(%rsp), %rbx
movq 0x18(%rsp), %r13
incq %rsi
jmp 0xe7111
movq 0xb8(%rsp), %rax
movq 0x8(%rax), %rsi
addq %rcx, %rsi
cmpq 0x10(%rax), %rsi
jbe 0xe72df
movq 0xb8(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rsp), %rbx
movq 0x30(%rbx), %rdi
imulq $0x18, %rcx, %rsi
addq %rdi, %rsi
callq 0xe1068
movq %rbx, %rsi
movq 0x50(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x50(%rsp)
movq 0x48(%rsp), %rdi
leaq (%rdi,%rdi), %r14
leaq -0x1(,%rdi,2), %r15
pushq $0x10
popq %r12
xorl %r13d, %r13d
movq (%rsi), %rax
cmpq %rax, %r13
jae 0xe7408
movq 0xb8(%rsp), %r8
movq (%r8), %rax
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rcx
leaq (%rax,%rcx), %rbp
movq 0x50(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0x30(%rsi), %rdx
movq -0x8(%rdx,%r12), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%rax,%rcx)
movl (%rdx,%r12), %edx
shrl $0x8, %edx
cmpl %edx, %edi
ja 0xe73c1
cmpl %edx, 0x58(%rsp)
jae 0xe73c1
leaq (%r15,%rdx), %rax
xorl %edx, %edx
divq %r14
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x1008d8(%rip), %xmm1   # 0x1e7c68
callq 0x23a0
cvttsd2si %xmm0, %rdx
movl %edx, 0xc(%rbp)
addq $0xc, %rbp
cmpl $0x1000000, %edx        # imm = 0x1000000
jb 0xe73b5
movl $0xffffff, (%rbp)       # imm = 0xFFFFFF
movl $0xffffff, %edx         # imm = 0xFFFFFF
movq 0x10(%rsp), %rsi
movq 0x48(%rsp), %rdi
jmp 0xe73eb
cmpq %rdx, 0x58(%rsp)
setb %sil
cmpq %rdx, %rdi
seta %dl
andb %sil, %dl
movzbl %dl, %esi
pushq $0x2
popq %rdx
subl %esi, %edx
movl %edx, 0xc(%rbp)
leaq (%rax,%rcx), %rbp
addq $0xc, %rbp
movq 0x10(%rsp), %rsi
shll $0x8, %edx
movq 0x30(%rsi), %rax
movzbl (%rax,%r12), %eax
orl %edx, %eax
movl %eax, (%rbp)
incq %r13
addq $0x18, %r12
jmp 0xe731e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq