// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %r9, 0x8(%rsp)
movq %r8, (%rsp)
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq 0xa0(%rsp), %rax
movq 0x98(%rsp), %rcx
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
testq %rcx, %rcx
je 0x124ba1
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x28(%rsp)
jmp 0x124baa
leaq 0x1(%rdx), %rcx
movq %rcx, 0x28(%rsp)
xorl %ebx, %ebx
testq %rax, %rax
movq 0x8(%rsp), %rdi
je 0x124bca
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x10(%rsp)
jmp 0x124bd3
movq $0x0, 0x10(%rsp)
callq 0x3fd1c
leaq 0x18(%r14), %rbp
movl %ebx, 0x18(%r14)
movq %rbx, (%r14)
leaq 0x165b6e(%rip), %rcx    # 0x28a758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r10d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %eax
movl 0x1f8(%rcx), %r11d
movss 0x109d90(%rip), %xmm0   # 0x22e9a0
movq %r12, %rdi
movl %r15d, %esi
movl %r13d, %edx
movq (%rsp), %rcx
xorl %r8d, %r8d
pushq %rbx
pushq %rbx
pushq %r10
pushq 0xa8(%rsp)
pushq %r11
pushq $-0x1
pushq %rbx
pushq 0xc0(%rsp)
pushq 0xb0(%rsp)
pushq %rax
pushq 0xc8(%rsp)
movq %rbp, 0x58(%rsp)
pushq %rbp
callq 0x11dbad
addq $0x60, %rsp
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0x124c74
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 0x124cd5
movl $0x8, %ebx
xorl %ebp, %ebp
leaq 0x1c(%rsp), %r13
xorl %r12d, %r12d
movq 0x20(%r14), %rax
movq (%rax,%rbp), %rsi
movq 0x80(%rsp), %rdi
movq %r13, %rdx
callq 0x113a7b
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%rbx)
movl 0x1c(%rsp), %eax
movl %eax, -0x8(%rcx,%rbx)
movslq 0x1c(%rsp), %rax
addq %rax, (%r14)
incq %r12
movl 0x18(%r14), %eax
addq $0x10, %rbx
addq $0x18, %rbp
cmpq %rax, %r12
jb 0x124c91
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jbe 0x124cf7
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
movq %rax, (%rsp)
testq %rax, %rax
je 0x124df0
movq 0x20(%r14), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%r14), %rcx
xorl %esi, %esi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
leaq 0x2(%rax), %r8
addq %rax, %r15
xorl %r9d, %r9d
movq %rcx, 0x30(%rsp)
movq %rsi, %rax
shlq $0x4, %rax
movslq (%rcx,%rax), %r10
testq %r10, %r10
jle 0x124de3
addq %rcx, %rax
leaq (%rsi,%rsi,2), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,8), %r11
movq 0x8(%rax), %rbx
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r10d
cmovbl %r10d, %eax
movq 0x30(%r14), %rcx
shll $0x8, %eax
movzbl 0x10(%r11), %ebp
orl %eax, %ebp
negq %r10
leaq (%r9,%r9,2), %rax
leaq (%rcx,%rax,8), %r12
addq $0x10, %r12
xorl %r13d, %r13d
movq (%rbx), %rdi
movq 0x8(%r11), %rcx
xorq %rdi, %rcx
shrq $0x20, %rdi
movl %edi, -0x4(%r12)
movl 0xc(%r11), %edx
testl %ecx, %ecx
jns 0x124daa
movzbl 0x10(%r11), %eax
subq %rdx, %rax
addq %r15, %rax
movq %rax, %rdx
movl %edx, -0x8(%r12)
movl %ebp, (%r12)
movq (%rbx), %rax
shlq $0x21, %rax
addq %rcx, %rcx
andq %r8, %rcx
orq %rax, %rcx
orq %rdi, %rcx
movq %rcx, -0x10(%r12)
addq $0x10, %rbx
decq %r13
addq $0x18, %r12
cmpq %r13, %r10
jne 0x124d81
subq %r13, %r9
movq 0x30(%rsp), %rcx
incq %rsi
cmpq (%rsp), %rsi
jne 0x124d2d
movq (%r14), %rax
movq 0x30(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
movq (%r14), %rcx
testq %rcx, %rcx
movq 0x10(%rsp), %rdi
je 0x124e77
movl $0x1, %ebx
movl $0x18, %r15d
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x124e3a
movq 0x30(%r14), %rcx
movq (%rcx,%r15), %rdx
leaq (%rax,%rax,2), %rsi
cmpq (%rcx,%rsi,8), %rdx
jne 0x124e3a
movq %rax, %rdx
jmp 0x124e65
movq %rbx, %rcx
subq %rax, %rcx
movq %rbx, %rdx
cmpq $0x2, %rcx
jb 0x124e65
movq 0x30(%r14), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r15, %rsi
callq 0x1200ba
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
incq %rbx
movq (%r14), %rcx
addq $0x18, %r15
movq %rdx, %rax
cmpq %rcx, %rbx
jbe 0x124e1e
movq 0x8(%r14), %rsi
movq 0x8(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0x124ea3
movq 0x8(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, (%rbx)
movq (%r14), %rax
testq %rax, %rax
movq 0x8(%rsp), %rsi
je 0x124fe1
movq 0x28(%rsp), %rax
leaq (%rax,%rax), %rcx
movq %rcx, (%rsp)
leaq -0x1(,%rax,2), %rax
movq %rax, 0x20(%rsp)
movl $0xc, %r13d
xorl %r15d, %r15d
movl $0x80000000, %r12d      # imm = 0x80000000
xorl %ebx, %ebx
movq (%rsi), %rbp
movq 0x30(%r14), %rax
movq (%rax,%r15), %rax
shrq $0x21, %rax
movl -0xc(%rbp,%r13), %ecx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%rbp,%r13)
movq 0x30(%r14), %rcx
movq (%rcx,%r15), %rcx
shrq %rcx
andl %r12d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%rbp,%r13)
movq 0x30(%r14), %rax
movl 0xc(%rax,%r15), %ecx
movl %ecx, -0x8(%rbp,%r13)
movl 0x8(%rax,%r15), %ecx
movl %ecx, -0x4(%rbp,%r13)
movl 0x10(%rax,%r15), %ecx
shrl $0x8, %ecx
movl %ecx, %eax
cmpl %ecx, 0x28(%rsp)
seta %cl
cmpq %rax, %rdi
setb %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x124f53
movl $0x1, (%rbp,%r13)
jmp 0x124f98
cmpl %eax, %edi
jae 0x124f8f
addq 0x20(%rsp), %rax
xorl %edx, %edx
divq (%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x104bfc(%rip), %xmm1   # 0x229b70
callq 0x23a0
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
cvttsd2si %xmm0, %rax
movl %eax, (%rbp,%r13)
jmp 0x124f98
movl $0x2, (%rbp,%r13)
cmpl $0x1000000, (%rbp,%r13) # imm = 0x1000000
jb 0x124fac
movl $0xffffff, (%rbp,%r13)  # imm = 0xFFFFFF
movl (%rbp,%r13), %eax
shll $0x8, %eax
movl %eax, (%rbp,%r13)
movq 0x30(%r14), %rcx
movzbl 0x10(%rcx,%r15), %ecx
orl %eax, %ecx
movl %ecx, (%rbp,%r13)
incq %rbx
movq (%r14), %rax
addq $0x18, %r15
addq $0x10, %r13
cmpq %rax, %rbx
jb 0x124edf
movq %rax, 0x8(%rsi)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq