// File: /workspace/github_c_makefile_star2_O2/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
jb 0x1aef6
movl %ecx, %r10d
xorl %eax, %eax
testl %edi, %edi
movl $0x0, %ecx
cmovgl %edi, %ecx
imulq $0x18, %rcx, %rcx
xorl %r11d, %r11d
cmpq %rax, %rcx
je 0x1ad25
cmpl (%rsi,%rax), %r10d
adcl $0x0, %r11d
addq $0x18, %rax
jmp 0x1ad12
testl %r11d, %r11d
je 0x1aef6
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl $0x80000000, %r14d      # imm = 0x80000000
cvtsi2sd %r9d, %xmm0
movsd %xmm0, 0x38(%rsp)
movslq %edi, %r9
movl %edi, %r11d
leaq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
pushq $-0x1
popq %rax
xorl %ebp, %ebp
movl %r8d, 0x14(%rsp)
movl %r10d, 0x10(%rsp)
movl %edx, 0xc(%rsp)
movq %rsi, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %r11, 0x18(%rsp)
cmpq %r9, %rbp
jg 0x1aee5
cmpq %r11, %rbp
je 0x1ad9d
imulq $0x18, %rbp, %rcx
cmpl %r10d, (%rsi,%rcx)
ja 0x1aedd
movl %ebp, %ecx
subl %eax, %ecx
cmpl $0x2, %ecx
jl 0x1aedb
testl %eax, %eax
js 0x1adbc
movl %eax, %ecx
imulq $0x18, %rcx, %rcx
movl 0x4(%rsi,%rcx), %ecx
shrl %ecx
jmp 0x1adbe
xorl %ecx, %ecx
movl %edx, %edi
cmpq %r11, %rbp
je 0x1adcf
imulq $0x18, %rbp, %rdi
movl 0x4(%rsi,%rdi), %edi
shrl %edi
subl %ecx, %edi
incl %eax
cvtsi2sd %edi, %xmm0
divsd 0x38(%rsp), %xmm0
addsd 0xb233(%rip), %xmm0     # 0x26018
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
jle 0x1aea3
movl $0x80, %edi
cmpl %edi, %ecx
cmovael %edi, %ecx
movslq %eax, %r12
imulq $0x18, %r12, %r15
addq %rsi, %r15
xorl %r13d, %r13d
leaq (%r12,%r13), %rbx
cmpq %rbp, %rbx
jge 0x1ae2e
cmpq %rcx, %r13
jae 0x1ae2e
movl (%r15), %eax
shlq $0x20, %rax
orq %rbx, %rax
movq %rax, 0x40(%rsp,%r13,8)
incq %r13
addq $0x18, %r15
jmp 0x1ae08
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
callq 0x4b7f
cmpq %rbp, %rbx
jge 0x1ae6d
movl (%r15), %eax
cmpl 0x44(%rsp), %eax
jae 0x1ae64
shlq $0x20, %rax
orq %rbx, %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
movq %r13, %rsi
leaq 0x40(%rsp), %rdx
callq 0x4b38
incq %rbx
addq $0x18, %r15
jmp 0x1ae3b
xorl %eax, %eax
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r10d
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r11
cmpq %r13, %rax
jae 0x1aea6
movl 0x40(%rsp,%rax,8), %ecx
imulq $0x18, %rcx, %rcx
orq %r14, 0x8(%rsi,%rcx)
incq %rax
jmp 0x1ae8c
movslq %eax, %r12
imulq $0x18, %r12, %rax
addq 0x30(%rsp), %rax
movq %rax, %rcx
movq %r12, %rdi
cmpq %rbp, %rdi
jge 0x1aed6
xorq %r14, (%rcx)
incq %rdi
addq $0x18, %rcx
jmp 0x1aeb5
cmpl %r8d, -0x8(%rax)
jbe 0x1aecf
orq %r14, (%rax)
incq %r12
addq $0x18, %rax
cmpq %rbp, %r12
jl 0x1aec6
movl %ebp, %eax
incq %rbp
jmp 0x1ad81
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq