// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x28(%rsp)
movq %rcx, %r12
movq %rdx, %rbx
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x12b4f1
movl (%rcx), %ecx
cmpl $0x3, %ecx
movl $0x2, %edx
cmovael %ecx, %edx
movq %rdx, 0x30(%rsp)
xorl %ebp, %ebp
testq %rax, %rax
movq %rdi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
je 0x12b520
movl (%rax), %eax
cmpl $0x3, %eax
movl $0x2, %ecx
cmovael %eax, %ecx
movq %rcx, 0x20(%rsp)
jmp 0x12b529
movq $0x0, 0x20(%rsp)
movq 0x28(%rsp), %rdi
callq 0x3f1bd
movq 0x8(%rsp), %r13
leaq 0x18(%r13), %rax
movl %ebp, 0x18(%r13)
movq %rbp, (%r13)
leaq 0x16a20d(%rip), %rcx    # 0x295758
movl (%rcx), %r9d
movl 0xa0(%rcx), %r11d
notl %r9d
andl $0x1, %r9d
movl 0xa8(%rcx), %r10d
movl 0x1f8(%rcx), %r14d
movss 0x10f63e(%rip), %xmm0   # 0x23abb0
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movl %r12d, %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
pushq %rbp
pushq %rbp
pushq %r11
pushq 0xd0(%rsp)
pushq %r14
pushq $-0x1
pushq %rbp
pushq 0xe8(%rsp)
pushq 0xd0(%rsp)
pushq %r10
pushq 0xe8(%rsp)
pushq %rax
callq 0x121477
movq %r13, %rdx
addq $0x60, %rsp
movl 0x1c(%r13), %esi
cmpl 0x10(%r13), %esi
jbe 0x12b5db
movl %esi, 0x10(%rdx)
movq 0x28(%rdx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, 0x28(%rdx)
movq $0x0, (%rdx)
cmpl $0x0, 0x18(%rdx)
je 0x12b789
movq 0xa0(%rsp), %r13
xorl %ebx, %ebx
leaq 0x44(%rsp), %r12
xorl %r14d, %r14d
movq 0x20(%rdx), %rax
movq (%rax,%rbx), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x1194bb
movq 0x8(%rsp), %rdx
movq 0x28(%rdx), %rcx
movq %rax, 0x8(%rcx,%rbx)
movslq 0x44(%rsp), %rax
movl %eax, (%rcx,%rbx)
addq (%rdx), %rax
movq %rax, (%rdx)
incq %r14
movl 0x18(%rdx), %ecx
addq $0x10, %rbx
cmpq %rcx, %r14
jb 0x12b5fe
cmpq 0x8(%rdx), %rax
jbe 0x12b663
movq %rax, 0x8(%rdx)
movq 0x30(%rdx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, 0x30(%rdx)
movl 0x18(%rdx), %ecx
leaq 0x8(%rdx), %rax
testl %ecx, %ecx
je 0x12b78d
movq %rax, 0x38(%rsp)
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movq 0x20(%rdx), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rdx), %rsi
xorl %edi, %edi
movl $0x7ffffff, %r9d        # imm = 0x7FFFFFF
incq %r15
xorl %r10d, %r10d
movq %rdi, %rax
shlq $0x4, %rax
movslq (%rsi,%rax), %r11
testq %r11, %r11
jle 0x12b774
leaq (%rsi,%rax), %rbx
movq 0x8(%rbx), %rbx
movq 0x30(%rdx), %r12
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r14
addq $0x8, %r14
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r11d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r11d, %ebp
shll $0x8, %ebp
leaq (%r10,%r10,2), %rax
leaq (%r12,%rax,8), %r12
addq $0x10, %r12
xorl %r13d, %r13d
movq (%rbx,%r13,8), %rcx
movq (%r14), %rax
xorq %rcx, %rax
movq %rcx, %rdx
shrq $0x1c, %rdx
andl %r9d, %edx
shrq $0x38, %rcx
btq $0x37, %rax
movl %edx, %r8d
notl %r8d
leal -0x2(%rcx,%r8), %ecx
cmovael %edx, %ecx
movl %ecx, -0x4(%r12)
movq (%r14), %rcx
shrq $0x1c, %rcx
movl %ecx, %edx
andl %r9d, %edx
movl %edx, -0x8(%r12)
movl %ebp, (%r12)
movzbl 0x7(%r14), %edx
orl %ebp, %edx
movl %edx, (%r12)
movq (%rbx,%r13,8), %rdx
andl $0xfffffff, %edx        # imm = 0xFFFFFFF
shlq $0x21, %rdx
shrq $0x17, %rax
andq %r15, %rax
andl $0x7ffffff, %ecx        # imm = 0x7FFFFFF
orq %rax, %rcx
orq %rdx, %rcx
movq %rcx, -0x10(%r12)
incq %r13
addq $0x18, %r12
cmpq %r13, %r11
jne 0x12b6e6
addq %r13, %r10
movq 0x8(%rsp), %rdx
incq %rdi
cmpq 0x10(%rsp), %rdi
jne 0x12b696
movq 0x38(%rsp), %rax
jmp 0x12b78d
leaq 0x8(%rdx), %rax
movq (%rax), %rsi
movq 0x28(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0x12b7b8
movq 0x28(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, (%rbx)
movq (%rdx), %rax
movq 0x30(%rdx), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq 0x8(%rsp), %rsi
movq (%rsi), %rcx
testq %rcx, %rcx
je 0x12ba07
movq 0x30(%rsp), %rdi
leaq (%rdi,%rdi), %rax
movq %rax, 0x18(%rsp)
leaq -0x1(,%rdi,2), %rax
movq %rax, 0x38(%rsp)
movq 0x30(%rsi), %rax
movl $0x1, %ebp
movq $-0x1, %rbx
xorl %r12d, %r12d
movq $-0x1, %r14
movq 0x20(%rsp), %r8
cmpq %rcx, %rbp
je 0x12b839
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rcx
leaq (%r12,%r12,2), %rdx
cmpq (%rax,%rdx,8), %rcx
je 0x12b9f6
movq %rbp, %rcx
subq %r12, %rcx
cmpq $0x2, %rcx
jb 0x12b874
leaq (%r12,%r12,2), %rcx
leaq (%rax,%rcx,8), %rdi
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x12621b
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsi), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rcx
shrq $0x21, %rcx
cmpq %r14, %rcx
je 0x12b898
movq 0xa8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %rbx
movq %rcx, %r14
cmpq %r12, %rbp
jbe 0x12b9f3
movq %r14, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
leal 0x1(%rbx), %r10d
leaq (,%r12,8), %rcx
leaq (%rcx,%rcx,2), %r13
movq %r12, %rbx
shlq $0x4, %rbx
orq $0xc, %rbx
movl %r10d, 0x10(%rsp)
movq 0x28(%rsp), %rcx
movq (%rcx), %r14
movq (%rax,%r13), %rax
shrq $0x21, %rax
movl -0xc(%r14,%rbx), %ecx
movl $0x80000000, %r9d       # imm = 0x80000000
andl %r9d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r14,%rbx)
movq 0x30(%rsi), %rcx
movq (%rcx,%r13), %rcx
shrq %rcx
movl %ecx, %edx
andl %r9d, %edx
orl %eax, %edx
movl %edx, -0xc(%r14,%rbx)
movq 0x30(%rsi), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%r13), %ecx
movl %ecx, -0x8(%r14,%rbx)
movl 0x8(%rax,%r13), %ecx
movl %ecx, -0x4(%r14,%rbx)
movl 0x10(%rax,%r13), %eax
shrl $0x8, %eax
cmpl %eax, %edi
ja 0x12b99e
cmpl %eax, %r8d
jae 0x12b99e
movq %r12, %r15
shlq $0x4, %r15
addq %r14, %r15
movq 0x38(%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0x18(%rsp)
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x10a407(%rip), %xmm1   # 0x235d68
callq 0x23a0
cvttsd2si %xmm0, %rcx
movl %ecx, (%r14,%rbx)
addq $0xc, %r15
cmpl $0x1000000, %ecx        # imm = 0x1000000
jb 0x12b988
movl $0xffffff, (%r14,%rbx)  # imm = 0xFFFFFF
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %r8
movl 0x10(%rsp), %r10d
jmp 0x12b9be
leaq (%r14,%rbx), %r15
cmpq %rax, %r8
setb %cl
cmpq %rax, %rdi
seta %al
andb %cl, %al
movzbl %al, %eax
movl $0x2, %ecx
subl %eax, %ecx
movl %ecx, (%r14,%rbx)
shll $0x8, %ecx
movq 0x30(%rsi), %rax
movzbl 0x10(%rax,%r13), %edx
orl %ecx, %edx
movl %edx, (%r15)
incq %r12
addq $0x18, %r13
addq $0x10, %rbx
cmpq %r12, %rbp
jne 0x12b8cb
movq %rbp, %r12
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r14
jmp 0x12b9f6
movq %rbp, %r12
incq %rbp
movq (%rsi), %rcx
cmpq %rcx, %rbp
jbe 0x12b817
jmp 0x12ba09
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
movq %rcx, 0x8(%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq