// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %r9, 0x68(%rsp)
movq %r8, %rbp
movq %rcx, %r13
movq %rdx, 0x78(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rcx
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
pushq $0x2
popq %rdx
testq %rcx, %rcx
je 0xe5938
movl (%rcx), %ecx
cmpl $0x3, %ecx
cmovbl %edx, %ecx
jmp 0xe593c
leaq 0x1(%rbx), %rcx
movq %rcx, 0x80(%rsp)
testq %rax, %rax
je 0xe5958
movl (%rax), %eax
cmpl $0x3, %eax
cmovbl %edx, %eax
movq %rax, 0x70(%rsp)
jmp 0xe5961
movq $0x0, 0x70(%rsp)
movq 0x68(%rsp), %rdi
callq 0x13d24
andl $0x0, 0x18(%r14)
andq $0x0, (%r14)
leaq 0x18(%r14), %r15
leaq 0x16ddd9(%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 0x110(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x38(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 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 %r15, (%rsp)
andq $0x0, 0x28(%rsp)
movd 0x1070a3(%rip), %xmm0   # 0x1ecaa0
movq %r12, %rdi
movq 0x78(%rsp), %rsi
movl %r13d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xdf468
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0xe5a31
movl %esi, 0x10(%r14)
movq 0x28(%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
andq $0x0, (%r14)
pushq $0x8
popq %r12
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %ebp, %ebp
movl (%r15), %eax
cmpq %rax, %rbp
jae 0xe5a8d
movq 0x20(%r14), %rax
movq (%rax,%r13), %rsi
movq 0x100(%rsp), %rdi
leaq 0x94(%rsp), %rdx
callq 0xd4e67
movq 0x28(%r14), %rdx
movq %rax, (%rdx,%r12)
movslq 0x94(%rsp), %rcx
movl %ecx, -0x8(%rdx,%r12)
addq (%r14), %rcx
movq %rcx, (%r14)
incq %rbp
addq $0x10, %r12
addq $0x18, %r13
jmp 0xe5a40
cmpq 0x8(%r14), %rcx
jbe 0xe5aac
movq %rcx, 0x8(%r14)
movq 0x30(%r14), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movl 0x18(%r14), %eax
leaq 0x2(%rbx), %rcx
movq %rcx, 0x88(%rsp)
xorl %esi, %esi
xorl %edx, %edx
movq %r14, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
cmpq %rax, %rdx
je 0xe5bdc
imulq $0x18, %rdx, %r9
addq 0x20(%r14), %r9
movq 0x28(%r14), %rcx
movq %rdx, 0xa8(%rsp)
shlq $0x4, %rdx
leaq (%rcx,%rdx), %r10
movl (%rcx,%rdx), %r11d
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r11d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r11d, %ebp
shll $0x8, %ebp
testl %r11d, %r11d
movl $0x0, %eax
cmovlel %eax, %r11d
imulq $0x18, %rsi, %r12
addq %r11, %rsi
movq %rsi, 0xb0(%rsp)
shlq $0x4, %r11
xorl %r13d, %r13d
cmpq %r13, %r11
je 0xe5bb4
movq 0x8(%r10), %rax
movq 0x98(%rsp), %rcx
movq 0x30(%rcx), %rcx
movq (%rax,%r13), %rdx
movq 0x8(%r9), %rdi
xorq %rdx, %rdi
shrq $0x20, %rdx
movl %edx, 0xc(%rcx,%r12)
movl 0xc(%r9), %r8d
movzbl 0x10(%r9), %r14d
movl %r14d, %ebx
subl %r8d, %ebx
movq 0x78(%rsp), %r15
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
addl %esi, %r15d
addl %r15d, %ebx
testl %edi, %edi
cmovnsl %r8d, %ebx
movl %ebx, 0x8(%rcx,%r12)
orl %ebp, %r14d
movl %r14d, 0x10(%rcx,%r12)
movq (%rax,%r13), %rax
shlq $0x21, %rax
addq %rdi, %rdi
andq 0x88(%rsp), %rdi
orq %rax, %rdi
orq %rdx, %rdi
movq %rdi, (%rcx,%r12)
addq $0x10, %r13
addq $0x18, %r12
jmp 0xe5b2b
movq 0xa8(%rsp), %rdx
incq %rdx
movq 0x98(%rsp), %r14
movq 0xa0(%rsp), %rax
movq 0xb0(%rsp), %rsi
jmp 0xe5acc
movq 0x30(%r14), %rdi
imulq $0x18, (%r14), %rsi
addq %rdi, %rsi
callq 0xe1068
pushq $0x1
popq %rbx
pushq $0x18
popq %r15
xorl %eax, %eax
cmpq (%r14), %rbx
ja 0xe5c44
je 0xe5c13
movq 0x30(%r14), %rcx
movq (%rcx,%r15), %rdx
imulq $0x18, %rax, %rsi
cmpq (%rcx,%rsi), %rdx
jne 0xe5c13
movq %rax, %rcx
jmp 0xe5c38
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jb 0xe5c38
movq 0x30(%r14), %rsi
imulq $0x18, %rax, %rdi
addq %rsi, %rdi
addq %r15, %rsi
callq 0xe1300
movq %rbx, %rcx
incq %rbx
addq $0x18, %r15
movq %rcx, %rax
jmp 0xe5bf5
movq 0x8(%r14), %rsi
movq 0x68(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0xe5c6b
movq 0x68(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x80(%rsp), %rdx
leaq (%rdx,%rdx), %rax
movq %rax, 0x78(%rsp)
leaq -0x1(,%rdx,2), %rax
movq %rax, 0x88(%rsp)
pushq $0xc
popq %r12
xorl %r13d, %r13d
movl $0x80000000, %edi       # imm = 0x80000000
xorl %ebp, %ebp
movq 0x70(%rsp), %rsi
movq (%r14), %rax
cmpq %rax, %rbp
jae 0xe5dac
movq 0x68(%rsp), %rax
movq (%rax), %r15
movq 0x30(%r14), %rax
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, %edx
ja 0xe5d6d
cmpl %eax, %esi
jae 0xe5d6d
movq %rbp, %rbx
shlq $0x4, %rbx
addq %r15, %rbx
movq 0x88(%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0x78(%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x101f35(%rip), %xmm1   # 0x1e7c68
callq 0x23a0
cvttsd2si %xmm0, %rax
movl %eax, (%r15,%r12)
addq $0xc, %rbx
cmpl $0x1000000, %eax        # imm = 0x1000000
jb 0xe5d59
movl $0xffffff, (%r15,%r12)  # imm = 0xFFFFFF
movl $0xffffff, %eax         # imm = 0xFFFFFF
movq 0x80(%rsp), %rdx
movq 0x70(%rsp), %rsi
movl $0x80000000, %edi       # imm = 0x80000000
jmp 0xe5d8b
leaq (%r15,%r12), %rbx
cmpq %rax, %rsi
setb %cl
cmpq %rax, %rdx
seta %al
andb %cl, %al
movzbl %al, %ecx
pushq $0x2
popq %rax
subl %ecx, %eax
movl %eax, (%r15,%r12)
shll $0x8, %eax
movq 0x30(%r14), %rcx
movzbl 0x10(%rcx,%r13), %ecx
orl %eax, %ecx
movl %ecx, (%rbx)
incq %rbp
addq $0x18, %r13
addq $0x10, %r12
jmp 0xe5c9f
movq 0x68(%rsp), %rcx
movq %rax, 0x8(%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq