// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: minimizers_qgen(ha_abufl_s*, char*, long, unsigned long, unsigned long, Candidates_list*, kvec_t_u8_warp*, void*, ha_pt_s*, All_reads*, ul_idx_t const*, kvec_t_u64_warp*, st_mt_t*, unsigned int*, unsigned int*)
void minimizers_qgen(ha_abufl_t *ab, char* rs, int64_t rl, uint64_t mz_w, uint64_t mz_k, Candidates_list *cl, kvec_t_u8_warp* k_flag, 
void *ha_flt_tab, ha_pt_t *ha_idx, All_reads* rdb, const ul_idx_t *udb, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, 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; int n, j; ha_mzl_t *z; seedl_t *s; 
	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); ab->mz.n = 0, ab->n_a = 0;
	
	// get the list of anchors
	mz2_ha_sketch(rs, rl, mz_w, mz_k, 0, !(asm_opt.flag & HA_F_NO_HPC), &ab->mz, ha_flt_tab, asm_opt.mz_sample_dist, k_flag, dbg_ct, NULL, -1, asm_opt.dp_min_len, -1, sp, asm_opt.mz_rewin, 0, NULL);

	// minimizer of queried read
	if (ab->mz.m > ab->old_mz_m) {
		ab->old_mz_m = ab->mz.m;
		REALLOC(ab->seed, ab->old_mz_m);
	}

	for (i = 0, ab->n_a = 0; i < ab->mz.n; ++i) {
		ab->seed[i].a = ha_ptl_get(ha_idx, ab->mz.a[i].x, &n);
		ab->seed[i].n = n;
		ab->n_a += n;
	}

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

	for (i = 0, k = 0; i < ab->mz.n; ++i) {
		///z is one of the minimizer
		z = &ab->mz.a[i]; s = &ab->seed[i];
		for (j = 0; j < s->n; ++j) {
			const ha_idxposl_t *y = &s->a[j];
			anchor1_t *an = &ab->a[k++];
			uint8_t rev = z->rev == y->rev? 0 : 1;
			an->other_off = rev?((uint32_t)-1)-1-(y->pos+1-y->span):y->pos;
			an->self_off = z->pos;
			///an->cnt: cnt<<8|span
			an->cnt = s->n; if(an->cnt > ((uint32_t)(0xffffffu))) an->cnt = 0xffffffu;
			an->cnt <<= 8; an->cnt |= ((z->span <= ((uint32_t)(0xffu)))?z->span:((uint32_t)(0xffu)));
			an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | an->self_off;
		}
	}

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

	k_mer_hit *p; uint64_t tid = (uint64_t)-1, tl = (uint64_t)-1;
	radix_sort_ha_an1(ab->a, ab->a + ab->n_a);
	for (k = 1, l = 0; k <= ab->n_a; ++k) {
		if (k == ab->n_a || ab->a[k].srt != ab->a[l].srt) {
			if (k-l>1) radix_sort_ha_an3(ab->a+l, ab->a+k);
			if((ab->a[l].srt>>33)!=tid) {
				tid = ab->a[l].srt>>33;
				tl = rdb?Get_READ_LENGTH((*rdb), tid):udb->ug->u.a[tid].len;
			} 
			for (i = l; i < k; i++) {
				p = &cl->list[i];
				p->readID = ab->a[i].srt>>33;
				p->strand = (ab->a[i].srt>>32)&1;
				if(!(p->strand)) {
					p->offset = ab->a[i].other_off;
				} else {
					p->offset = ((uint32_t)-1)-ab->a[i].other_off;
					p->offset = tl-p->offset;
				}
				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));
			}
			l = k;
		}
	}
	cl->length = ab->n_a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x78(%rsp)
movq %r8, 0x68(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdi, %r14
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rcx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
pushq $0x2
popq %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0xe5e28
movl (%rcx), %esi
cmpl $0x3, %esi
cmovbl %edx, %esi
movq %rsi, 0x80(%rsp)
movq 0x120(%rsp), %r15
movq 0x118(%rsp), %r13
movq 0xf0(%rsp), %rbp
testq %rax, %rax
je 0xe5e5c
movl (%rax), %eax
cmpl $0x3, %eax
cmovbl %edx, %eax
movq %rax, 0x70(%rsp)
jmp 0xe5e65
movq $0x0, 0x70(%rsp)
movq 0x78(%rsp), %rdi
callq 0x13d24
andl $0x0, 0x18(%r14)
andq $0x0, (%r14)
leaq 0x18(%r14), %rbx
leaq 0x16d8d5(%rip), %rax    # 0x253758
movl (%rax), %r9d
movl 0xa0(%rax), %ecx
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rax), %edx
movl 0x1f8(%rax), %eax
movl %ecx, 0x48(%rsp)
movq %r15, 0x40(%rsp)
movl %eax, 0x38(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
andq $0x0, 0x58(%rsp)
movl %edx, 0x10(%rsp)
andl $0x0, 0x50(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x8(%rsp)
orl $-0x1, 0x30(%rsp)
movq %rbx, (%rsp)
andq $0x0, 0x28(%rsp)
movss 0x106bb7(%rip), %xmm0   # 0x1ecaa0
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq 0x68(%rsp), %rcx
xorl %r8d, %r8d
callq 0xdf468
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0xe5f2d
movl %esi, 0x10(%r14)
movq 0x28(%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
andq $0x0, (%r14)
pushq $0x8
popq %r15
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %ebp, %ebp
movl (%rbx), %eax
cmpq %rax, %rbp
jae 0xe5f88
movq 0x20(%r14), %rax
movq (%rax,%r13), %rsi
movq 0x100(%rsp), %rdi
leaq 0xb4(%rsp), %rdx
callq 0xd4e67
movq 0x28(%r14), %rdx
movq %rax, (%rdx,%r15)
movslq 0xb4(%rsp), %rcx
movl %ecx, -0x8(%rdx,%r15)
addq (%r14), %rcx
movq %rcx, (%r14)
incq %rbp
addq $0x10, %r15
addq $0x18, %r13
jmp 0xe5f3c
cmpq 0x8(%r14), %rcx
jbe 0xe5fa7
movq %rcx, 0x8(%r14)
movq 0x30(%r14), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movl 0x18(%r14), %eax
incq %r12
xorl %edx, %edx
xorl %edi, %edi
movq 0x108(%rsp), %rbx
cmpq %rax, %rdi
je 0xe6092
imulq $0x18, %rdi, %r8
addq 0x20(%r14), %r8
movq 0x28(%r14), %rcx
movq %rdi, %r10
shlq $0x4, %r10
leaq (%rcx,%r10), %r9
movl (%rcx,%r10), %r10d
movl $0xffffff, %ecx         # imm = 0xFFFFFF
cmpl %ecx, %r10d
movl $0xffffff, %r11d        # imm = 0xFFFFFF
cmovbl %r10d, %r11d
shll $0x8, %r11d
testl %r10d, %r10d
movl $0x0, %ecx
cmovlel %ecx, %r10d
imulq $0x18, %rdx, %rbx
addq %r10, %rdx
movq %rdx, 0x68(%rsp)
shlq $0x4, %r10
xorl %r15d, %r15d
cmpq %r15, %r10
je 0xe607d
movq 0x8(%r9), %rbp
movq (%rbp,%r15), %rcx
movq 0x8(%r8), %r13
xorq %rcx, %r13
shrq $0x20, %rcx
testl %r13d, %r13d
jns 0xe603a
movzbl 0x8(%rbp,%r15), %edx
subl %ecx, %edx
addl $-0x3, %edx
movl %edx, %ecx
movq 0x30(%r14), %rdx
movl %ecx, 0xc(%rdx,%rbx)
movq 0x8(%r8), %rcx
shrq $0x20, %rcx
movl %ecx, 0x8(%rdx,%rbx)
movzbl 0x10(%r8), %esi
orl %r11d, %esi
movl %esi, 0x10(%rdx,%rbx)
movq (%rbp,%r15), %rsi
shlq $0x21, %rsi
addq %r13, %r13
andq %r12, %r13
orq %rsi, %r13
orq %rcx, %r13
movq %r13, (%rdx,%rbx)
addq $0x10, %r15
addq $0x18, %rbx
jmp 0xe600f
incq %rdi
movq 0x108(%rsp), %rbx
movq 0x68(%rsp), %rdx
jmp 0xe5fb6
movq 0x8(%r14), %rsi
movq 0x78(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0xe60c1
movq 0x78(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x108(%rsp), %rbx
movq 0x30(%r14), %rdi
imulq $0x18, (%r14), %rsi
addq %rdi, %rsi
callq 0xe1068
movq 0x80(%rsp), %rdi
leaq (%rdi,%rdi), %rax
movq %rax, 0xa8(%rsp)
leaq -0x1(,%rdi,2), %rax
movq %rax, 0xa0(%rsp)
pushq $-0x1
popq %rax
pushq $0x1
popq %r9
xorl %r11d, %r11d
movq %rax, 0x98(%rsp)
movq %rax, 0x90(%rsp)
movq 0x70(%rsp), %r8
movq (%r14), %rax
cmpq %rax, %r9
ja 0xe632b
movq 0x30(%r14), %rax
je 0xe613c
imulq $0x18, %r9, %rcx
movq (%rax,%rcx), %rcx
imulq $0x18, %r11, %rdx
cmpq (%rax,%rdx), %rcx
je 0xe6323
movq %r9, %rcx
subq %r11, %rcx
cmpq $0x2, %rcx
movq %r9, 0x68(%rsp)
jb 0xe617c
imulq $0x18, %r11, %rdi
addq %rax, %rdi
imulq $0x18, %r9, %rsi
addq %rax, %rsi
movq %r11, %r15
callq 0xe1595
movq %r15, %r11
movq 0x68(%rsp), %r9
movq 0x70(%rsp), %r8
movq 0x80(%rsp), %rdi
movq 0x30(%r14), %rax
imulq $0x18, %r11, %r15
movq (%rax,%r15), %rcx
shrq $0x21, %rcx
cmpq 0x98(%rsp), %rcx
je 0xe61cd
testq %rbx, %rbx
je 0xe61a1
movq 0x18(%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
jmp 0xe61bd
movq 0x110(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rsi
movl (%rdx,%rsi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movq 0x90(%rsp), %rcx
leal 0x1(%rcx), %r10d
movq %r11, %rbx
shlq $0x4, %rbx
orq $0xc, %rbx
movl %r10d, 0x88(%rsp)
cmpq %r9, %r11
jae 0xe6318
movq 0x78(%rsp), %rcx
movq (%rcx), %r13
movq (%rax,%r15), %rax
shrq $0x21, %rax
movl -0xc(%r13,%rbx), %ecx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r13,%rbx)
movq 0x30(%r14), %rcx
movq (%rcx,%r15), %rcx
shrq %rcx
movl %ecx, %edx
andl %esi, %edx
orl %eax, %edx
movl %edx, -0xc(%r13,%rbx)
movq 0x30(%r14), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%r15), %ecx
movl %ecx, -0x8(%r13,%rbx)
movl 0x8(%rax,%r15), %ecx
movl %ecx, -0x4(%r13,%rbx)
movl 0x10(%rax,%r15), %eax
shrl $0x8, %eax
cmpl %eax, %edi
ja 0xe62d6
cmpl %eax, %r8d
jae 0xe62d6
movq %r11, %rbp
movq %r11, %r12
shlq $0x4, %r12
addq %r13, %r12
movq 0xa0(%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0xa8(%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x1019da(%rip), %xmm1   # 0x1e7c68
callq 0x23a0
cvttsd2si %xmm0, %rcx
movl %ecx, (%r13,%rbx)
addq $0xc, %r12
cmpl $0x1000000, %ecx        # imm = 0x1000000
jb 0xe62b7
movl $0xffffff, (%r13,%rbx)  # imm = 0xFFFFFF
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq 0x80(%rsp), %rdi
movq 0x70(%rsp), %r8
movq 0x68(%rsp), %r9
movl 0x88(%rsp), %r10d
movq %rbp, %r11
jmp 0xe62f5
leaq (%rbx,%r13), %r12
cmpq %rax, %r8
setb %cl
cmpq %rax, %rdi
seta %al
andb %cl, %al
movzbl %al, %eax
pushq $0x2
popq %rcx
subl %eax, %ecx
movl %ecx, (%r13,%rbx)
shll $0x8, %ecx
movq 0x30(%r14), %rax
movzbl 0x10(%rax,%r15), %edx
orl %ecx, %edx
movl %edx, (%r12)
incq %r11
addq $0x18, %r15
addq $0x10, %rbx
jmp 0xe61ec
movq %r9, %r11
movq 0x108(%rsp), %rbx
incq %r9
jmp 0xe6114
movq 0x78(%rsp), %rcx
movq %rax, 0x8(%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq