// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x20(%rsp)
movq %rcx, %r12
movq %rdx, %rbx
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x12af7e
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x28(%rsp)
xorl %ebp, %ebp
testq %rax, %rax
movq %rdi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
je 0x12afad
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x18(%rsp)
jmp 0x12afb6
movq $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rdi
callq 0x3f1bd
movq 0x8(%rsp), %r13
leaq 0x18(%r13), %rax
movl %ebp, 0x18(%r13)
movq %rbp, (%r13)
leaq 0x16a780(%rip), %rcx    # 0x295758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r11d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %r10d
movl 0x1f8(%rcx), %r14d
movss 0x10fbb1(%rip), %xmm0   # 0x23abb0
movq 0x30(%rsp), %rdi
movl %ebx, %esi
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
pushq %rbp
pushq %rbp
pushq %r11
pushq 0xd8(%rsp)
pushq %r14
pushq $-0x1
pushq %rbp
pushq 0xf0(%rsp)
pushq 0xd0(%rsp)
pushq %r10
pushq 0xe8(%rsp)
pushq %rax
callq 0x123ac6
movq %r13, %r8
addq $0x60, %rsp
movl 0x1c(%r13), %esi
cmpl 0x10(%r13), %esi
jbe 0x12b069
movl %esi, 0x10(%r8)
movq 0x28(%r8), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x28(%r8)
movq $0x0, (%r8)
cmpl $0x0, 0x18(%r8)
je 0x12b1e7
movq 0xa0(%rsp), %rbp
movl $0x8, %ebx
xorl %r14d, %r14d
leaq 0x3c(%rsp), %r12
xorl %r13d, %r13d
movq 0x20(%r8), %rax
movq (%rax,%r14), %rsi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x119548
movq 0x8(%rsp), %r8
movq 0x28(%r8), %rcx
movq %rax, (%rcx,%rbx)
movslq 0x3c(%rsp), %rax
movl %eax, -0x8(%rcx,%rbx)
addq (%r8), %rax
movq %rax, (%r8)
incq %r13
movl 0x18(%r8), %ecx
addq $0x10, %rbx
addq $0x18, %r14
cmpq %rcx, %r13
jb 0x12b093
cmpq 0x8(%r8), %rax
jbe 0x12b0fe
movq %rax, 0x8(%r8)
movq 0x30(%r8), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x30(%r8)
movl 0x18(%r8), %ecx
leaq 0x8(%r8), %rax
testl %ecx, %ecx
je 0x12b1eb
movq %rax, 0x10(%rsp)
movl %ecx, %ecx
movq 0x20(%r8), %rdx
movq 0x28(%r8), %rsi
xorl %edi, %edi
incq %r15
xorl %r9d, %r9d
movq %rdi, %rax
shlq $0x4, %rax
movslq (%rsi,%rax), %r10
testq %r10, %r10
jle 0x12b1d4
addq %rsi, %rax
leaq (%rdi,%rdi,2), %r11
leaq (%rdx,%r11,8), %r11
movq 0x8(%rax), %rbx
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r10d
cmovbl %r10d, %eax
movq 0x30(%r8), %r14
shll $0x8, %eax
movzbl 0x10(%r11), %ebp
orl %eax, %ebp
negq %r10
leaq (%r9,%r9,2), %rax
leaq (%r14,%rax,8), %r14
addq $0x10, %r14
xorl %r12d, %r12d
movq (%rbx), %rax
movq 0x8(%r11), %r13
xorq %rax, %r13
shrq $0x20, %rax
testl %r13d, %r13d
jns 0x12b192
movzbl 0x8(%rbx), %r8d
subl %eax, %r8d
addl $-0x3, %r8d
movl %r8d, %eax
movl %eax, -0x4(%r14)
movq 0x8(%r11), %rax
shrq $0x20, %rax
movl %eax, -0x8(%r14)
movl %ebp, (%r14)
movq (%rbx), %r8
shlq $0x21, %r8
addq %r13, %r13
andq %r15, %r13
orq %r8, %r13
orq %rax, %r13
movq %r13, -0x10(%r14)
addq $0x10, %rbx
decq %r12
addq $0x18, %r14
cmpq %r12, %r10
jne 0x12b170
subq %r12, %r9
movq 0x8(%rsp), %r8
incq %rdi
cmpq %rcx, %rdi
jne 0x12b121
movq 0x10(%rsp), %rax
jmp 0x12b1eb
leaq 0x8(%r8), %rax
movq (%rax), %rsi
movq 0x20(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0x12b216
movq 0x20(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, (%rbx)
movq (%r8), %rax
movq 0x30(%r8), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq 0x8(%rsp), %rdi
movq (%rdi), %rcx
testq %rcx, %rcx
je 0x12b491
movq 0xa8(%rsp), %rbx
movq 0x28(%rsp), %r8
leaq (%r8,%r8), %rax
movq %rax, 0x30(%rsp)
leaq -0x1(,%r8,2), %rax
movq %rax, 0x50(%rsp)
movq 0x30(%rdi), %rax
movl $0x1, %ebp
movq $-0x1, %r14
xorl %r15d, %r15d
movq $-0x1, %r12
movq 0x18(%rsp), %r9
cmpq %rcx, %rbp
je 0x12b29f
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rcx
leaq (%r15,%r15,2), %rdx
cmpq (%rax,%rdx,8), %rcx
je 0x12b480
movq %rbp, %rcx
subq %r15, %rcx
cmpq $0x2, %rcx
jb 0x12b2da
leaq (%r15,%r15,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x12621b
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x30(%rdi), %rax
leaq (%r15,%r15,2), %rcx
movq (%rax,%rcx,8), %rcx
shrq $0x21, %rcx
cmpq %r12, %rcx
je 0x12b31b
testq %rbx, %rbx
je 0x12b2fa
movq 0x18(%rbx), %rdx
movq (%rdx,%rcx,8), %r14
jmp 0x12b318
movq 0xb0(%rsp), %rdx
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rsi
movl (%rdx,%rsi,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq %rcx, %r12
cmpq %r15, %rbp
jbe 0x12b47d
movq %r12, 0x40(%rsp)
movq %r14, 0x48(%rsp)
leal 0x1(%r14), %r10d
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,2), %r13
movq %r15, %rbx
shlq $0x4, %rbx
orq $0xc, %rbx
movl %r10d, 0x10(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx), %r14
movq (%rax,%r13), %rax
shrq $0x21, %rax
movl -0xc(%r14,%rbx), %ecx
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r14,%rbx)
movq 0x30(%rdi), %rcx
movq (%rcx,%r13), %rcx
shrq %rcx
movl %ecx, %edx
andl %esi, %edx
orl %eax, %edx
movl %edx, -0xc(%r14,%rbx)
movq 0x30(%rdi), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%r13), %ecx
movl %ecx, -0x8(%r14,%rbx)
movl 0x8(%rax,%r13), %ecx
movl %ecx, -0x4(%r14,%rbx)
movl 0x10(%rax,%r13), %eax
shrl $0x8, %eax
cmpl %eax, %r8d
ja 0x12b41f
cmpl %eax, %r9d
jae 0x12b41f
movq %r15, %r12
shlq $0x4, %r12
addq %r14, %r12
movq 0x50(%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0x30(%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x10a986(%rip), %xmm1   # 0x235d68
callq 0x23a0
cvttsd2si %xmm0, %rcx
movl %ecx, (%r14,%rbx)
addq $0xc, %r12
cmpl $0x1000000, %ecx        # imm = 0x1000000
jb 0x12b409
movl $0xffffff, (%r14,%rbx)  # imm = 0xFFFFFF
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %r9
movl 0x10(%rsp), %r10d
jmp 0x12b43f
leaq (%r14,%rbx), %r12
cmpq %rax, %r9
setb %cl
cmpq %rax, %r8
seta %al
andb %cl, %al
movzbl %al, %eax
movl $0x2, %ecx
subl %eax, %ecx
movl %ecx, (%r14,%rbx)
shll $0x8, %ecx
movq 0x30(%rdi), %rax
movzbl 0x10(%rax,%r13), %edx
orl %ecx, %edx
movl %edx, (%r12)
incq %r15
addq $0x18, %r13
addq $0x10, %rbx
cmpq %r15, %rbp
jne 0x12b34e
movq %rbp, %r15
movq 0xa8(%rsp), %rbx
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %r12
jmp 0x12b480
movq %rbp, %r15
incq %rbp
movq (%rdi), %rcx
cmpq %rcx, %rbp
jbe 0x12b27d
jmp 0x12b493
xorl %ecx, %ecx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq