// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/anchor.cpp
// Function: minimizers_gen(ha_abufl_s*, char*, long, unsigned long, unsigned long, Candidates_list*, kvec_t_u8_warp*, void*, ha_pt_s*, kvec_t_u64_warp*, st_mt_t*, unsigned int*, unsigned int*)
void minimizers_gen(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, 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 = y->pos;
			an->self_off = rev? rl - 1 - (z->pos + 1 - z->span) : 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->other_off;
		}
	}

	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_an2(ab->a + l, ab->a + k);
			l = k;
		}
	}

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

	for (k = 0; k < ab->n_a; ++k) {
		k_mer_hit *p = &cl->list[k];
		p->readID = ab->a[k].srt >> 33;
		p->strand = ab->a[k].srt >> 32 & 1;
		p->offset = ab->a[k].other_off;
		p->self_offset = ab->a[k].self_off;
		if(((ab->a[k].cnt>>8) < max_cnt) && ((ab->a[k].cnt>>8) > min_cnt)){
			p->cnt = 1;
		} else if((ab->a[k].cnt>>8) <= min_cnt) {
			p->cnt = 2;
		} else{
			p->cnt = 1 + (((ab->a[k].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[k].cnt));
	}
	cl->length = ab->n_a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x10(%rsp)
movq %rcx, %r12
movq %rdx, %r15
movq %rdi, %r14
movq 0xb0(%rsp), %rax
movq 0xa8(%rsp), %rcx
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
testq %rcx, %rcx
je 0x12aac6
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
jmp 0x12aaca
leaq 0x1(%rbx), %rdx
xorl %ebp, %ebp
testq %rax, %rax
movq %rdx, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq %rsi, (%rsp)
je 0x12aaf3
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x20(%rsp)
jmp 0x12aafc
movq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rdi
callq 0x3f1bd
leaq 0x18(%r14), %rax
movl %ebp, 0x18(%r14)
movq %rbp, (%r14)
leaq 0x16ac40(%rip), %rcx    # 0x295758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r11d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %r10d
movl 0x1f8(%rcx), %r13d
movd 0x110071(%rip), %xmm0   # 0x23abb0
movq (%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
movq 0x8(%rsp), %rcx
xorl %r8d, %r8d
pushq %rbp
pushq %rbp
pushq %r11
pushq 0xb8(%rsp)
pushq %r13
pushq $-0x1
pushq %rbp
pushq 0xd0(%rsp)
pushq 0xc0(%rsp)
pushq %r10
pushq 0xd8(%rsp)
pushq %rax
callq 0x123ac6
addq $0x60, %rsp
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0x12aba1
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 0x12ad2b
movl $0x8, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%r14), %rax
movq (%rax,%r12), %rsi
movq 0x90(%rsp), %rdi
leaq 0x2c(%rsp), %rdx
callq 0x119548
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%rbp)
movslq 0x2c(%rsp), %rax
movl %eax, -0x8(%rcx,%rbp)
addq (%r14), %rax
movq %rax, (%r14)
incq %r13
movl 0x18(%r14), %ecx
addq $0x10, %rbp
addq $0x18, %r12
cmpq %rcx, %r13
jb 0x12abbe
cmpq 0x8(%r14), %rax
jbe 0x12ac26
movq %rax, 0x8(%r14)
movq 0x30(%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movl 0x18(%r14), %ecx
leaq 0x8(%r14), %rax
movq %rax, 0x30(%rsp)
testl %ecx, %ecx
je 0x12ad34
movl %ecx, %eax
movq %rax, (%rsp)
movq 0x20(%r14), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%r14), %rax
xorl %esi, %esi
addl %ebx, %r15d
addq $0x2, %rbx
xorl %ecx, %ecx
movq %rax, 0x38(%rsp)
movq %rsi, %rdi
shlq $0x4, %rdi
movslq (%rax,%rdi), %r9
testq %r9, %r9
jle 0x12ad1c
addq %rax, %rdi
leaq (%rsi,%rsi,2), %r10
movq 0x40(%rsp), %rax
leaq (%rax,%r10,8), %r10
movq 0x8(%rdi), %r11
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r9d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r9d, %ebp
movq 0x30(%r14), %rdi
shll $0x8, %ebp
movzbl 0x10(%r10), %r13d
orl %r13d, %ebp
negq %r9
movq %rcx, 0x8(%rsp)
leaq (%rcx,%rcx,2), %r12
leaq (%rdi,%r12,8), %r12
addq $0x10, %r12
xorl %edi, %edi
movq (%r11), %rcx
movq 0x8(%r10), %rax
xorq %rcx, %rax
shrq $0x20, %rcx
movl %ecx, -0x4(%r12)
movl 0xc(%r10), %edx
movl %r13d, %r8d
subl %edx, %r8d
addl %r15d, %r8d
testl %eax, %eax
cmovnsl %edx, %r8d
movl %r8d, -0x8(%r12)
movl %ebp, (%r12)
movq (%r11), %rdx
shlq $0x21, %rdx
addq %rax, %rax
andq %rbx, %rax
orq %rdx, %rax
orq %rcx, %rax
movq %rax, -0x10(%r12)
addq $0x10, %r11
decq %rdi
addq $0x18, %r12
cmpq %rdi, %r9
jne 0x12acb8
movq 0x8(%rsp), %rcx
subq %rdi, %rcx
movq 0x38(%rsp), %rax
incq %rsi
cmpq (%rsp), %rsi
jne 0x12ac5a
jmp 0x12ad34
leaq 0x8(%r14), %rax
movq %rax, 0x30(%rsp)
movq (%r14), %rax
movq 0x30(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq (%r14), %rax
testq %rax, %rax
je 0x12adb3
movl $0x1, %ebx
movl $0x18, %r15d
xorl %ecx, %ecx
cmpq %rax, %rbx
je 0x12ad79
movq 0x30(%r14), %rdx
movq (%rdx,%r15), %rsi
leaq (%rcx,%rcx,2), %rdi
cmpq (%rdx,%rdi,8), %rsi
jne 0x12ad79
movq %rcx, %rdx
jmp 0x12ada2
movq %rbx, %rsi
subq %rcx, %rsi
movq %rbx, %rdx
cmpq $0x2, %rsi
jb 0x12ada2
movq 0x30(%r14), %rsi
leaq (%rcx,%rcx,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r15, %rsi
callq 0x125f7f
movq (%r14), %rax
movq %rbx, %rdx
incq %rbx
addq $0x18, %r15
movq %rdx, %rcx
cmpq %rax, %rbx
jbe 0x12ad5d
jmp 0x12adb5
xorl %eax, %eax
movq 0x30(%rsp), %rcx
movq (%rcx), %rsi
movq 0x10(%rsp), %rcx
cmpq 0x10(%rcx), %rsi
jb 0x12ade3
movq 0x10(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%r14), %rax
testq %rax, %rax
je 0x12af1e
movq 0x18(%rsp), %rsi
leaq (%rsi,%rsi), %rax
movq %rax, 0x8(%rsp)
leaq -0x1(,%rsi,2), %rax
movq %rax, (%rsp)
movq 0x30(%r14), %rax
movl $0xc, %r12d
xorl %r13d, %r13d
movl $0x80000000, %edi       # imm = 0x80000000
xorl %ebp, %ebp
movq 0x10(%rsp), %rcx
movq (%rcx), %r15
movq (%rax,%r13), %rax
shrq $0x21, %rax
movl -0xc(%r15,%r12), %ecx
andl %edi, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r15,%r12)
movq 0x30(%r14), %rcx
movq (%rcx,%r13), %rcx
shrq %rcx
andl %edi, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r15,%r12)
movq 0x30(%r14), %rax
movq 0x8(%rax,%r13), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, -0x8(%r15,%r12)
movl 0x10(%rax,%r13), %eax
shrl $0x8, %eax
cmpl %eax, %esi
ja 0x12aed2
cmpl %eax, 0x20(%rsp)
jae 0x12aed2
movq %rbp, %rbx
shlq $0x4, %rbx
addq %r15, %rbx
movq (%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0x8(%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x10aec9(%rip), %xmm1   # 0x235d68
callq 0x23a0
cvttsd2si %xmm0, %rcx
movl %ecx, (%r15,%r12)
addq $0xc, %rbx
cmpl $0x1000000, %ecx        # imm = 0x1000000
jb 0x12aec6
movl $0xffffff, (%r15,%r12)  # imm = 0xFFFFFF
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq 0x18(%rsp), %rsi
movl $0x80000000, %edi       # imm = 0x80000000
jmp 0x12aef4
leaq (%r15,%r12), %rbx
cmpq %rax, 0x20(%rsp)
setb %cl
cmpq %rax, %rsi
seta %al
andb %cl, %al
movzbl %al, %eax
movl $0x2, %ecx
subl %eax, %ecx
movl %ecx, (%r15,%r12)
shll $0x8, %ecx
movq 0x30(%r14), %rax
movzbl 0x10(%rax,%r13), %edx
orl %ecx, %edx
movl %edx, (%rbx)
incq %rbp
movq (%r14), %rcx
addq $0x18, %r13
addq $0x10, %r12
cmpq %rcx, %rbp
jb 0x12ae1a
jmp 0x12af20
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq %rcx, 0x8(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq