// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %r9, 0x88(%rsp)
movq %r8, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdi, %r14
movq 0x128(%rsp), %rax
movq 0x120(%rsp), %rcx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
pushq $0x2
popq %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0xe63a4
movl (%rcx), %esi
cmpl $0x3, %esi
cmovbl %edx, %esi
movq %rsi, 0x90(%rsp)
movq 0x118(%rsp), %r15
movq 0x110(%rsp), %r13
movq 0xf0(%rsp), %rbp
testq %rax, %rax
je 0xe63db
movl (%rax), %eax
cmpl $0x3, %eax
cmovbl %edx, %eax
movq %rax, 0x80(%rsp)
jmp 0xe63e7
movq $0x0, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0x13d24
andl $0x0, 0x18(%r14)
andq $0x0, (%r14)
leaq 0x18(%r14), %rbx
leaq 0x16d350(%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 %r15, 0x40(%rsp)
movl %eax, 0x38(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 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 %rbx, (%rsp)
andq $0x0, 0x28(%rsp)
movss 0x106632(%rip), %xmm0   # 0x1ecaa0
movq 0x98(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rcx
xorl %r8d, %r8d
callq 0xdd581
movl 0x1c(%r14), %esi
cmpl 0x10(%r14), %esi
jbe 0xe64ac
movl %esi, 0x10(%r14)
movq 0x28(%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
movq 0x100(%rsp), %rbp
andq $0x0, (%r14)
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %r13d, %r13d
movl (%rbx), %eax
cmpq %rax, %r13
jae 0xe6503
movq 0x20(%r14), %rax
movq (%rax,%r15), %rsi
movq %rbp, %rdi
leaq 0xa4(%rsp), %rdx
callq 0xd4de2
movq 0x28(%r14), %rdx
movq %rax, 0x8(%rdx,%r15)
movslq 0xa4(%rsp), %rcx
movl %ecx, (%rdx,%r15)
addq (%r14), %rcx
movq %rcx, (%r14)
incq %r13
addq $0x10, %r15
jmp 0xe64c0
cmpq 0x8(%r14), %rcx
jbe 0xe6522
movq %rcx, 0x8(%r14)
movq 0x30(%r14), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movl 0x18(%r14), %eax
movl $0x7ffffff, %esi        # imm = 0x7FFFFFF
incq %r12
xorl %edi, %edi
xorl %edx, %edx
movq %rax, 0x78(%rsp)
cmpq %rax, %rdx
je 0xe6634
movq 0x20(%r14), %rcx
movq 0x28(%r14), %r9
movq %rdx, 0x70(%rsp)
shlq $0x4, %rdx
movl (%r9,%rdx), %r10d
addq %rdx, %r9
leaq (%rcx,%rdx), %r11
addq $0x8, %r11
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r10d
movl $0xffffff, %ebx         # imm = 0xFFFFFF
cmovbl %r10d, %ebx
shll $0x8, %ebx
testl %r10d, %r10d
movl $0x0, %eax
cmovlel %eax, %r10d
movq %rdi, 0x68(%rsp)
imulq $0x18, %rdi, %r13
xorl %r15d, %r15d
cmpq %r15, %r10
je 0xe661a
movq 0x8(%r9), %rdx
movq 0x30(%r14), %rbp
movq (%rdx,%r15,8), %rax
movq (%r11), %rcx
xorq %rax, %rcx
movq %rax, %r8
shrq $0x1c, %r8
andl %esi, %r8d
shrq $0x38, %rax
btq $0x37, %rcx
movl %r8d, %edi
notl %edi
leal -0x2(%rax,%rdi), %eax
cmovael %r8d, %eax
movl %eax, 0xc(%rbp,%r13)
movq (%r11), %rax
shrq $0x1c, %rax
movl %eax, %edi
andl %esi, %edi
movl %edi, 0x8(%rbp,%r13)
movl %ebx, 0x10(%rbp,%r13)
movzbl 0x7(%r11), %edi
orl %ebx, %edi
movl %edi, 0x10(%rbp,%r13)
movq (%rdx,%r15,8), %rdx
andl $0xfffffff, %edx        # imm = 0xFFFFFFF
shlq $0x21, %rdx
shrq $0x17, %rcx
andq %r12, %rcx
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
orq %rcx, %rax
orq %rdx, %rax
movq %rax, (%rbp,%r13)
incq %r15
addq $0x18, %r13
jmp 0xe6588
movq 0x70(%rsp), %rdx
incq %rdx
movq 0x68(%rsp), %rdi
addq %r15, %rdi
movq 0x78(%rsp), %rax
jmp 0xe6533
movq 0x8(%r14), %rsi
movq 0x88(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0xe6661
movq 0x88(%rsp), %rbx
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0x30(%r14), %rdi
imulq $0x18, (%r14), %rsi
addq %rdi, %rsi
callq 0xe1068
movq 0x90(%rsp), %rsi
leaq (%rsi,%rsi), %rax
movq %rax, 0x78(%rsp)
leaq -0x1(,%rsi,2), %rax
movq %rax, 0x98(%rsp)
pushq $-0x1
popq %rax
pushq $0x1
popq %r8
xorl %r11d, %r11d
movq %rax, 0xb0(%rsp)
movq %rax, 0xa8(%rsp)
movq 0x80(%rsp), %rdi
movq (%r14), %rax
cmpq %rax, %r8
ja 0xe68aa
movq 0x30(%r14), %rax
je 0xe66dc
imulq $0x18, %r8, %rcx
movq (%rax,%rcx), %rcx
imulq $0x18, %r11, %rdx
cmpq (%rax,%rdx), %rcx
je 0xe68a2
movq %r8, %rcx
subq %r11, %rcx
cmpq $0x2, %rcx
movq %r8, 0x68(%rsp)
jb 0xe671f
imulq $0x18, %r11, %rdi
addq %rax, %rdi
imulq $0x18, %r8, %rsi
addq %rax, %rsi
movq %r11, %rbx
callq 0xe1595
movq %rbx, %r11
movq 0x68(%rsp), %r8
movq 0x80(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x30(%r14), %rax
imulq $0x18, %r11, %r15
movq (%rax,%r15), %rcx
shrq $0x21, %rcx
cmpq 0xb0(%rsp), %rcx
je 0xe6755
movq 0x108(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xb0(%rsp)
movq 0xa8(%rsp), %rcx
leal 0x1(%rcx), %r10d
movq %r11, %rbx
shlq $0x4, %rbx
orq $0xc, %rbx
movl %r10d, 0x70(%rsp)
cmpq %r8, %r11
jae 0xe689f
movq 0x88(%rsp), %rcx
movq (%rcx), %r13
movq (%rax,%r15), %rax
shrq $0x21, %rax
movl -0xc(%r13,%rbx), %ecx
movl $0x80000000, %r9d       # imm = 0x80000000
andl %r9d, %ecx
orl %eax, %ecx
movl %ecx, -0xc(%r13,%rbx)
movq 0x30(%r14), %rcx
movq (%rcx,%r15), %rcx
shrq %rcx
movl %ecx, %edx
andl %r9d, %edx
orl %eax, %edx
movl %edx, -0xc(%r13,%rbx)
movq 0x30(%r14), %rax
sarl $0x1f, %ecx
andl %r10d, %ecx
addl 0xc(%rax,%r15), %ecx
movl %ecx, -0x8(%r13,%rbx)
movl 0x8(%rax,%r15), %ecx
movl %ecx, -0x4(%r13,%rbx)
movl 0x10(%rax,%r15), %eax
shrl $0x8, %eax
cmpl %eax, %esi
ja 0xe685d
cmpl %eax, %edi
jae 0xe685d
movq %r11, %rbp
movq %r11, %r12
shlq $0x4, %r12
addq %r13, %r12
movq 0x98(%rsp), %rcx
addq %rcx, %rax
xorl %edx, %edx
divq 0x78(%rsp)
incl %eax
cvtsi2sd %rax, %xmm0
movsd 0x101453(%rip), %xmm1   # 0x1e7c68
callq 0x23a0
cvttsd2si %xmm0, %rcx
movl %ecx, (%r13,%rbx)
addq $0xc, %r12
cmpl $0x1000000, %ecx        # imm = 0x1000000
jb 0xe683e
movl $0xffffff, (%r13,%rbx)  # imm = 0xFFFFFF
movl $0xffffff, %ecx         # imm = 0xFFFFFF
movq 0x90(%rsp), %rsi
movq 0x80(%rsp), %rdi
movq 0x68(%rsp), %r8
movl 0x70(%rsp), %r10d
movq %rbp, %r11
jmp 0xe687c
leaq (%rbx,%r13), %r12
cmpq %rax, %rdi
setb %cl
cmpq %rax, %rsi
seta %al
andb %cl, %al
movzbl %al, %eax
pushq $0x2
popq %rcx
subl %eax, %ecx
movl %ecx, (%r13,%rbx)
shll $0x8, %ecx
movq 0x30(%r14), %rax
movzbl 0x10(%rax,%r15), %edx
orl %ecx, %edx
movl %edx, (%r12)
incq %r11
addq $0x18, %r15
addq $0x10, %rbx
jmp 0xe6771
movq %r8, %r11
incq %r8
jmp 0xe66b4
movq 0x88(%rsp), %rcx
movq %rax, 0x8(%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq