// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/anchor.cpp
// Function: minimizers_qgen0(ha_abuf_s*, char*, long, unsigned long, unsigned long, Candidates_list*, kvec_t_u8_warp*, void*, ha_pt_s*, All_reads*, kvec_t_u64_warp*, st_mt_t*, unsigned int*, unsigned int*)
void minimizers_qgen0(ha_abuf_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, 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_mz1_t *z; seed1_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
	mz1_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_pt_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_idxpos_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 = Get_READ_LENGTH((*rdb), tid);
				// 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, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x12559b
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x30(%rsp)
movq 0xa8(%rsp), %rbp
xorl %r13d, %r13d
testq %rax, %rax
je 0x1255c4
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x28(%rsp)
jmp 0x1255cd
movq $0x0, 0x28(%rsp)
movq %r9, (%rsp)
movq %r9, %rdi
callq 0x3fd1c
leaq 0x18(%r14), %rbx
movl %r13d, 0x18(%r14)
movq %r13, (%r14)
leaq 0x16516d(%rip), %rcx    # 0x28a758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r10d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %eax
movl 0x1f8(%rcx), %r11d
movss 0x10938f(%rip), %xmm0   # 0x22e9a0
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
pushq %r13
pushq %r13
pushq %r10
pushq %rbp
pushq %r11
pushq $-0x1
pushq %r13
pushq 0xd8(%rsp)
pushq 0xc0(%rsp)
pushq %rax
pushq 0xd8(%rsp)
pushq %rbx
callq 0x11b648
addq $0x60, %rsp
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0x125672
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 0x1256cb
xorl %r13d, %r13d
leaq 0x24(%rsp), %r12
xorl %ebp, %ebp
movq 0x20(%r14), %rax
movq (%rax,%r13), %rsi
movq 0x90(%rsp), %rdi
movq %r12, %rdx
callq 0x1139ee
movq 0x28(%r14), %rcx
movq %rax, 0x8(%rcx,%r13)
movl 0x24(%rsp), %eax
movl %eax, (%rcx,%r13)
movslq 0x24(%rsp), %rax
addq %rax, (%r14)
incq %rbp
movl 0x18(%r14), %eax
addq $0x10, %r13
cmpq %rax, %rbp
jb 0x12568a
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jbe 0x1256ed
movq %rax, 0x8(%r14)
movq 0x30(%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movl (%rbx), %eax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x125800
movq 0x20(%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%r14), %rax
xorl %esi, %esi
movl $0x7ffffff, %r8d        # imm = 0x7FFFFFF
incq %r15
xorl %r9d, %r9d
movq %rax, 0x8(%rsp)
movq %rsi, %rdi
shlq $0x4, %rdi
movslq (%rax,%rdi), %r10
testq %r10, %r10
jle 0x1257f2
leaq (%rax,%rdi), %r11
movq 0x8(%r11), %r11
movq 0x30(%r14), %r12
movq 0x10(%rsp), %rax
leaq (%rax,%rdi), %rbx
addq $0x8, %rbx
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r10d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r10d, %ebp
shll $0x8, %ebp
leaq (%r9,%r9,2), %rdi
leaq (%r12,%rdi,8), %r12
addq $0x10, %r12
xorl %r13d, %r13d
movq (%r11,%r13,8), %rdx
movq (%rbx), %rdi
xorq %rdx, %rdi
movq %rdx, %rax
shrq $0x1c, %rax
andl %r8d, %eax
shrq $0x38, %rdx
btq $0x37, %rdi
movl %eax, %ecx
notl %ecx
leal -0x2(%rdx,%rcx), %ecx
cmovael %eax, %ecx
movl %ecx, -0x4(%r12)
movq (%rbx), %rax
shrq $0x1c, %rax
movl %eax, %ecx
andl %r8d, %ecx
movl %ecx, -0x8(%r12)
movl %ebp, (%r12)
movzbl 0x7(%rbx), %ecx
orl %ebp, %ecx
movl %ecx, (%r12)
movq (%r11,%r13,8), %rcx
andl $0xfffffff, %ecx        # imm = 0xFFFFFFF
shlq $0x21, %rcx
shrq $0x17, %rdi
andq %r15, %rdi
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
orq %rdi, %rax
orq %rcx, %rax
movq %rax, -0x10(%r12)
incq %r13
addq $0x18, %r12
cmpq %r13, %r10
jne 0x12576d
addq %r13, %r9
movq 0x8(%rsp), %rax
incq %rsi
cmpq 0x18(%rsp), %rsi
jne 0x12571d
movq 0x8(%r14), %rsi
movq (%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0x125825
movq %rsi, 0x10(%rax)
movq (%rax), %rdi
shlq $0x4, %rsi
callq 0x2370
movq (%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 0x125a7b
movq 0x30(%rsp), %rdi
leaq (%rdi,%rdi), %rcx
movq %rcx, 0x18(%rsp)
leaq -0x1(,%rdi,2), %rcx
movq %rcx, 0x10(%rsp)
movl $0x1, %r15d
movq $-0x1, %r12
xorl %ebx, %ebx
movl $0x80000000, %r9d       # imm = 0x80000000
movq $-0x1, %r13
movq (%rsp), %rsi
movq 0x28(%rsp), %r8
movq %r14, 0x40(%rsp)
cmpq %rax, %r15
je 0x1258a9
movq 0x30(%r14), %rax
leaq (%r15,%r15,2), %rcx
movq (%rax,%rcx,8), %rcx
leaq (%rbx,%rbx,2), %rdx
cmpq (%rax,%rdx,8), %rcx
je 0x125a6a
movq %r15, %rax
subq %rbx, %rax
cmpq $0x2, %rax
jb 0x1258e2
movq 0x30(%r14), %rax
leaq (%rbx,%rbx,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq (%r15,%r15,2), %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x12035b
movl $0x80000000, %r9d       # imm = 0x80000000
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
movq (%rsp), %rsi
movq 0x30(%r14), %rax
leaq (%rbx,%rbx,2), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
cmpq %r13, %rax
je 0x12590a
movq 0x98(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rax,8), %r12
movq %rax, %r13
cmpq %rbx, %r15
jbe 0x125a67
movq %r13, 0x38(%rsp)
movq %r12, 0x8(%rsp)
leal 0x1(%r12), %r10d
leaq (,%rbx,8), %rax
leaq (%rax,%rax,2), %rbp
movq %rbx, %r12
shlq $0x4, %r12
orq $0xc, %r12
movq (%rsi), %r13
movq 0x30(%r14), %rax
movq (%rax,%rbp), %rax
shrq $0x21, %rax
movl -0xc(%r13,%r12), %ecx
andl %r9d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r13,%r12)
movq 0x30(%r14), %rcx
movq (%rcx,%rbp), %rcx
shrq %rcx
movl %ecx, %edx
andl %r9d, %edx
orl %eax, %edx
movl %edx, -0xc(%r13,%r12)
movq 0x30(%r14), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%rbp), %ecx
movl %ecx, -0x8(%r13,%r12)
movq 0x30(%r14), %rax
movl 0x8(%rax,%rbp), %ecx
movl %ecx, -0x4(%r13,%r12)
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 0x1259b4
movl $0x1, (%r13,%r12)
jmp 0x125a13
cmpl %eax, %r8d
jae 0x125a0a
addq 0x10(%rsp), %rax
xorl %edx, %edx
divq 0x18(%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x10419c(%rip), %xmm1   # 0x229b70
movq %r15, %r14
movl %r10d, %r15d
callq 0x23a0
movl %r15d, %r10d
movq %r14, %r15
movq 0x40(%rsp), %r14
movl $0x80000000, %r9d       # imm = 0x80000000
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
movq (%rsp), %rsi
cvttsd2si %xmm0, %rax
movl %eax, (%r13,%r12)
jmp 0x125a13
movl $0x2, (%r13,%r12)
cmpl $0x1000000, (%r13,%r12) # imm = 0x1000000
jb 0x125a27
movl $0xffffff, (%r13,%r12)  # imm = 0xFFFFFF
movl (%r13,%r12), %eax
shll $0x8, %eax
movl %eax, (%r13,%r12)
movq 0x30(%r14), %rcx
movzbl 0x10(%rcx,%rbp), %ecx
orl %eax, %ecx
movl %ecx, (%r13,%r12)
incq %rbx
addq $0x18, %rbp
addq $0x10, %r12
cmpq %rbx, %r15
jne 0x125939
movq %r15, %rbx
movq 0x8(%rsp), %r12
movq 0x38(%rsp), %r13
jmp 0x125a6a
movq %r15, %rbx
incq %r15
movq (%r14), %rax
cmpq %rax, %r15
jbe 0x12588a
jmp 0x125a7f
movq (%rsp), %rsi
movq %rax, 0x8(%rsi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq