// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r8, (%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movq 0xc0(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x125043
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x30(%rsp)
movq 0xb0(%rsp), %rbp
xorl %r13d, %r13d
testq %rax, %rax
je 0x12506c
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x18(%rsp)
jmp 0x125075
movq $0x0, 0x18(%rsp)
movq %r9, 0x8(%rsp)
movq %r9, %rdi
callq 0x3fd1c
leaq 0x18(%r14), %rbx
movl %r13d, 0x18(%r14)
movq %r13, (%r14)
leaq 0x1656c4(%rip), %rcx    # 0x28a758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r10d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %eax
movl 0x1f8(%rcx), %r11d
movss 0x1098e6(%rip), %xmm0   # 0x22e9a0
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq (%rsp), %rcx
xorl %r8d, %r8d
pushq %r13
pushq %r13
pushq %r10
pushq %rbp
pushq %r11
pushq $-0x1
pushq %r13
pushq 0xe0(%rsp)
pushq 0xc0(%rsp)
pushq %rax
pushq 0xd8(%rsp)
movq %rbx, 0x58(%rsp)
pushq %rbx
callq 0x11dbad
addq $0x60, %rsp
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0x12511f
movl %esi, 0x10(%r14)
movq 0x28(%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
movq $0x0, (%r14)
cmpl $0x0, 0x18(%r14)
je 0x125181
movl $0x8, %r13d
xorl %ebp, %ebp
leaq 0x14(%rsp), %r12
xorl %ebx, %ebx
movq 0x20(%r14), %rax
movq (%rax,%rbp), %rsi
movq 0x90(%rsp), %rdi
movq %r12, %rdx
callq 0x113a7b
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%r13)
movl 0x14(%rsp), %eax
movl %eax, -0x8(%rcx,%r13)
movslq 0x14(%rsp), %rax
addq %rax, (%r14)
incq %rbx
movl 0x18(%r14), %eax
addq $0x10, %r13
addq $0x18, %rbp
cmpq %rax, %rbx
jb 0x12513c
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jbe 0x1251a3
movq %rax, 0x8(%r14)
movq 0x30(%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movq (%rsp), %rax
movl (%rax), %eax
testq %rax, %rax
je 0x125289
movq 0x20(%r14), %rcx
movq %rcx, (%rsp)
movq 0x28(%r14), %rdx
xorl %esi, %esi
incq %r15
xorl %r8d, %r8d
movq %rsi, %rdi
shlq $0x4, %rdi
movslq (%rdx,%rdi), %r9
testq %r9, %r9
jle 0x12527d
addq %rdx, %rdi
leaq (%rsi,%rsi,2), %r10
movq (%rsp), %rcx
leaq (%rcx,%r10,8), %r10
movq 0x8(%rdi), %r11
movl $0xffffff, %ecx         # imm = 0xFFFFFF
cmpl %ecx, %r9d
movl $0xffffff, %edi         # imm = 0xFFFFFF
cmovbl %r9d, %edi
movq 0x30(%r14), %r12
shll $0x8, %edi
movzbl 0x10(%r10), %ebx
orl %edi, %ebx
negq %r9
leaq (%r8,%r8,2), %rdi
leaq (%r12,%rdi,8), %r12
addq $0x10, %r12
xorl %r13d, %r13d
movq (%r11), %rdi
movq 0x8(%r10), %rbp
xorq %rdi, %rbp
shrq $0x20, %rdi
testl %ebp, %ebp
jns 0x12523c
movzbl 0x8(%r11), %ecx
subl %edi, %ecx
addl $-0x3, %ecx
movl %ecx, %edi
movl %edi, -0x4(%r12)
movq 0x8(%r10), %rcx
shrq $0x20, %rcx
movl %ecx, -0x8(%r12)
movl %ebx, (%r12)
movq (%r11), %rdi
shlq $0x21, %rdi
addq %rbp, %rbp
andq %r15, %rbp
orq %rdi, %rbp
orq %rcx, %rbp
movq %rbp, -0x10(%r12)
addq $0x10, %r11
decq %r13
addq $0x18, %r12
cmpq %r13, %r9
jne 0x12521e
subq %r13, %r8
incq %rsi
cmpq %rax, %rsi
jne 0x1251c6
movq 0x8(%r14), %rsi
movq 0x8(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0x1252b0
movq %rsi, 0x10(%rax)
movq (%rax), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq (%r14), %rax
movq 0x30(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
movq (%r14), %rax
testq %rax, %rax
je 0x125533
movq 0x98(%rsp), %r15
movq 0x30(%rsp), %rdi
leaq (%rdi,%rdi), %rcx
movq %rcx, (%rsp)
leaq -0x1(,%rdi,2), %rcx
movq %rcx, 0x28(%rsp)
movl $0x1, %r12d
movq $-0x1, %r13
xorl %ebx, %ebx
movl $0x80000000, %r9d       # imm = 0x80000000
movq $-0x1, %rbp
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r8
movq %r14, 0x40(%rsp)
cmpq %rax, %r12
je 0x12533c
movq 0x30(%r14), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rcx
leaq (%rbx,%rbx,2), %rdx
cmpq (%rax,%rdx,8), %rcx
je 0x125522
movq %r12, %rax
subq %rbx, %rax
cmpq $0x2, %rax
jb 0x125376
movq 0x30(%r14), %rax
leaq (%rbx,%rbx,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq (%r12,%r12,2), %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x12035b
movl $0x80000000, %r9d       # imm = 0x80000000
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%r14), %rax
leaq (%rbx,%rbx,2), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
cmpq %rbp, %rax
je 0x1253bb
testq %r15, %r15
je 0x12539a
movq 0x18(%r15), %rcx
movq (%rcx,%rax,8), %r13
jmp 0x1253b8
movq 0xa0(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rdx
movl (%rcx,%rdx,8), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %rax, %rbp
cmpq %rbx, %r12
jbe 0x12551f
movq %rbp, 0x38(%rsp)
movq %r13, 0x20(%rsp)
leal 0x1(%r13), %r10d
leaq (,%rbx,8), %rax
leaq (%rax,%rax,2), %rbp
movq %rbx, %r15
shlq $0x4, %r15
orq $0xc, %r15
movq (%rsi), %r13
movq 0x30(%r14), %rax
movq (%rax,%rbp), %rax
shrq $0x21, %rax
movl -0xc(%r13,%r15), %ecx
andl %r9d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r13,%r15)
movq 0x30(%r14), %rcx
movq (%rcx,%rbp), %rcx
shrq %rcx
movl %ecx, %edx
andl %r9d, %edx
orl %eax, %edx
movl %edx, -0xc(%r13,%r15)
movq 0x30(%r14), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%rbp), %ecx
movl %ecx, -0x8(%r13,%r15)
movq 0x30(%r14), %rax
movl 0x8(%rax,%rbp), %ecx
movl %ecx, -0x4(%r13,%r15)
movl 0x10(%rax,%rbp), %ecx
shrl $0x8, %ecx
movl %ecx, %eax
cmpl %ecx, %edi
seta %cl
cmpq %rax, %r8
setb %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x125464
movl $0x1, (%r13,%r15)
jmp 0x1254c3
cmpl %eax, %r8d
jae 0x1254ba
addq 0x28(%rsp), %rax
xorl %edx, %edx
divq (%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x1046ed(%rip), %xmm1   # 0x229b70
movq %r12, %r14
movl %r10d, %r12d
callq 0x23a0
movl %r12d, %r10d
movq %r14, %r12
movq 0x40(%rsp), %r14
movl $0x80000000, %r9d       # imm = 0x80000000
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
cvttsd2si %xmm0, %rax
movl %eax, (%r13,%r15)
jmp 0x1254c3
movl $0x2, (%r13,%r15)
cmpl $0x1000000, (%r13,%r15) # imm = 0x1000000
jb 0x1254d7
movl $0xffffff, (%r13,%r15)  # imm = 0xFFFFFF
movl (%r13,%r15), %eax
shll $0x8, %eax
movl %eax, (%r13,%r15)
movq 0x30(%r14), %rcx
movzbl 0x10(%rcx,%rbp), %ecx
orl %eax, %ecx
movl %ecx, (%r13,%r15)
incq %rbx
addq $0x18, %rbp
addq $0x10, %r15
cmpq %rbx, %r12
jne 0x1253e9
movq %r12, %rbx
movq 0x98(%rsp), %r15
movq 0x20(%rsp), %r13
movq 0x38(%rsp), %rbp
jmp 0x125522
movq %r12, %rbx
incq %r12
movq (%r14), %rax
cmpq %rax, %r12
jbe 0x12531d
jmp 0x125538
movq 0x8(%rsp), %rsi
movq %rax, 0x8(%rsi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq