// File: /workspace/github_c_makefile_star2_O3/lh3[P]minimap2/seed.c
// Function: mm_seed_select
void mm_seed_select(int32_t n, mm_seed_t *a, int len, int max_occ, int max_max_occ, int dist)
{ // for high-occ minimizers, choose up to max_high_occ in each high-occ streak
	extern void ks_heapdown_uint64_t(size_t i, size_t n, uint64_t*);
	extern void ks_heapmake_uint64_t(size_t n, uint64_t*);
	int32_t i, last0, m;
	uint64_t b[MAX_MAX_HIGH_OCC]; // this is to avoid a heap allocation

	if (n == 0 || n == 1) return;
	for (i = m = 0; i < n; ++i)
		if (a[i].n > max_occ) ++m;
	if (m == 0) return; // no high-frequency k-mers; do nothing
	for (i = 0, last0 = -1; i <= n; ++i) {
		if (i == n || a[i].n <= max_occ) {
			if (i - last0 > 1) {
				int32_t ps = last0 < 0? 0 : (uint32_t)a[last0].q_pos>>1;
				int32_t pe = i == n? len : (uint32_t)a[i].q_pos>>1;
				int32_t j, k, st = last0 + 1, en = i;
				int32_t max_high_occ = (int32_t)((double)(pe - ps) / dist + .499);
				if (max_high_occ > 0) {
					if (max_high_occ > MAX_MAX_HIGH_OCC)
						max_high_occ = MAX_MAX_HIGH_OCC;
					for (j = st, k = 0; j < en && k < max_high_occ; ++j, ++k)
						b[k] = (uint64_t)a[j].n<<32 | j;
					ks_heapmake_uint64_t(k, b); // initialize the binomial heap
					for (; j < en; ++j) { // if there are more, choose top max_high_occ
						if (a[j].n < (int32_t)(b[0]>>32)) { // then update the heap
							b[0] = (uint64_t)a[j].n<<32 | j;
							ks_heapdown_uint64_t(0, k, b);
						}
					}
					for (j = 0; j < k; ++j) a[(uint32_t)b[j]].flt = 1;
				}
				for (j = st; j < en; ++j) a[j].flt ^= 1;
				for (j = st; j < en; ++j)
					if (a[j].n > max_max_occ)
						a[j].flt = 1;
			}
			last0 = i;
		}
	}
}

// Assembly:
cmpl $0x2, %edi
jl 0x1db41
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl %ecx, %r10d
movq %rsi, %r15
movl %edi, %ebx
leaq (,%rbx,8), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %esi, %esi
cmpl (%r15,%rcx), %r10d
adcl $0x0, %esi
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x1d910
testl %esi, %esi
je 0x1db30
movl $0x80000000, %r12d      # imm = 0x80000000
cvtsi2sd %r9d, %xmm0
movsd %xmm0, 0x38(%rsp)
incl %edi
leaq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movl %r8d, 0x14(%rsp)
movl %r10d, 0x10(%rsp)
movl %edx, 0xc(%rsp)
movq %rdi, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
cmpq %rbx, %r13
je 0x1d97e
leaq (,%r13,2), %rcx
addq %r13, %rcx
cmpl %r10d, (%r15,%rcx,8)
ja 0x1db24
movl %r13d, %ecx
subl %eax, %ecx
cmpl $0x2, %ecx
jl 0x1db21
testl %eax, %eax
js 0x1d99f
movl %eax, %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x4(%r15,%rcx,8), %ecx
shrl %ecx
jmp 0x1d9a1
xorl %ecx, %ecx
movl %edx, %esi
cmpq %rbx, %r13
je 0x1d9ba
leaq (,%r13,2), %rsi
addq %r13, %rsi
movl 0x4(%r15,%rsi,8), %esi
shrl %esi
subl %ecx, %esi
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
divsd 0x38(%rsp), %xmm0
addsd 0xd64d(%rip), %xmm0     # 0x2b020
cvttsd2si %xmm0, %ecx
movslq %eax, %rbp
testl %ecx, %ecx
jle 0x1dad0
xorl %eax, %eax
movq %rbp, 0x18(%rsp)
cmpq %rbp, %r13
jle 0x1da42
movl $0x80, %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rsi,2), %rax
leaq (%r15,%rax,8), %r9
movq %rsi, %rbp
shlq $0x20, %rbp
xorl %eax, %eax
movabsq $0x100000000, %r14      # imm = 0x100000000
addq %r14, %rbp
movl (%r9), %r11d
shlq $0x20, %r11
orq %rsi, %r11
movq %r11, 0x40(%rsp,%rax,8)
incq %rsi
incq %rax
cmpq %r13, %rsi
jge 0x1da3e
addq $0x18, %r9
cmpq %rcx, %rax
jb 0x1da18
sarq $0x20, %rbp
movl %eax, %r14d
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x5ec3
cmpq %r13, %rbp
jge 0x1da97
leaq (,%rbp,2), %rax
addq %rbp, %rax
leaq (%r15,%rax,8), %rbx
movl (%rbx), %eax
cmpl 0x44(%rsp), %eax
jae 0x1da89
shlq $0x20, %rax
orq %rbp, %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x5e65
incq %rbp
movl %ebp, %eax
addq $0x18, %rbx
cmpq %r13, %rax
jne 0x1da66
testq %r14, %r14
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r10d
movl 0xc(%rsp), %edx
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rbx
movq 0x18(%rsp), %rbp
je 0x1dad0
xorl %eax, %eax
movl 0x40(%rsp,%rax,8), %ecx
leaq (%rcx,%rcx,2), %rcx
orq %r12, 0x8(%r15,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x1dabb
cmpq %rbp, %r13
jle 0x1db21
leaq (,%rbp,2), %rax
addq %rbp, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
leaq 0x1(%rbp), %rcx
movq %rcx, %r9
movq %rax, %rsi
xorq %r12, (%rsi)
movl %r9d, %r11d
addq $0x18, %rsi
incq %r9
cmpq %r13, %r11
jne 0x1daf3
cmpq %rbp, %r13
jle 0x1db21
cmpl %r8d, -0x8(%rax)
jbe 0x1db13
orq %r12, (%rax)
movl %ecx, %esi
addq $0x18, %rax
incq %rcx
cmpq %r13, %rsi
jne 0x1db0a
movl %r13d, %eax
incq %r13
cmpq %rdi, %r13
jne 0x1d964
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq