// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/sketch.c
// Function: mm_sketch
void mm_sketch(void *km, const char *str, int len, int w, int k, uint32_t rid, int is_hpc, mm128_v *p)
{
	uint64_t shift1 = 2 * (k - 1), mask = (1ULL<<2*k) - 1, kmer[2] = {0,0};
	int i, j, l, buf_pos, min_pos, kmer_span = 0;
	mm128_t buf[256], min = { UINT64_MAX, UINT64_MAX };
	tiny_queue_t tq;

	assert(len > 0 && (w > 0 && w < 256) && (k > 0 && k <= 28)); // 56 bits for k-mer; could use long k-mers, but 28 enough in practice
	memset(buf, 0xff, w * 16);
	memset(&tq, 0, sizeof(tiny_queue_t));
	kv_resize(mm128_t, km, *p, p->n + len/w);

	for (i = l = buf_pos = min_pos = 0; i < len; ++i) {
		int c = seq_nt4_table[(uint8_t)str[i]];
		mm128_t info = { UINT64_MAX, UINT64_MAX };
		if (c < 4) { // not an ambiguous base
			int z;
			if (is_hpc) {
				int skip_len = 1;
				if (i + 1 < len && seq_nt4_table[(uint8_t)str[i + 1]] == c) {
					for (skip_len = 2; i + skip_len < len; ++skip_len)
						if (seq_nt4_table[(uint8_t)str[i + skip_len]] != c)
							break;
					i += skip_len - 1; // put $i at the end of the current homopolymer run
				}
				tq_push(&tq, skip_len);
				kmer_span += skip_len;
				if (tq.count > k) kmer_span -= tq_shift(&tq);
			} else kmer_span = l + 1 < k? l + 1 : k;
			kmer[0] = (kmer[0] << 2 | c) & mask;           // forward k-mer
			kmer[1] = (kmer[1] >> 2) | (3ULL^c) << shift1; // reverse k-mer
			if (kmer[0] == kmer[1]) continue; // skip "symmetric k-mers" as we don't know it strand
			z = kmer[0] < kmer[1]? 0 : 1; // strand
			++l;
			if (l >= k && kmer_span < 256) {
				info.x = hash64(kmer[z], mask) << 8 | kmer_span;
				info.y = (uint64_t)rid<<32 | (uint32_t)i<<1 | z;
			}
		} else l = 0, tq.count = tq.front = 0, kmer_span = 0;
		buf[buf_pos] = info; // need to do this here as appropriate buf_pos and buf[buf_pos] are needed below
		if (l == w + k - 1 && min.x != UINT64_MAX) { // special case for the first window - because identical k-mers are not stored yet
			for (j = buf_pos + 1; j < w; ++j)
				if (min.x == buf[j].x && buf[j].y != min.y) kv_push(mm128_t, km, *p, buf[j]);
			for (j = 0; j < buf_pos; ++j)
				if (min.x == buf[j].x && buf[j].y != min.y) kv_push(mm128_t, km, *p, buf[j]);
		}
		if (info.x <= min.x) { // a new minimum; then write the old min
			if (l >= w + k && min.x != UINT64_MAX) kv_push(mm128_t, km, *p, min);
			min = info, min_pos = buf_pos;
		} else if (buf_pos == min_pos) { // old min has moved outside the window
			if (l >= w + k - 1 && min.x != UINT64_MAX) kv_push(mm128_t, km, *p, min);
			for (j = buf_pos + 1, min.x = UINT64_MAX; j < w; ++j) // the two loops are necessary when there are identical k-mers
				if (min.x >= buf[j].x) min = buf[j], min_pos = j; // >= is important s.t. min is always the closest k-mer
			for (j = 0; j <= buf_pos; ++j)
				if (min.x >= buf[j].x) min = buf[j], min_pos = j;
			if (l >= w + k - 1 && min.x != UINT64_MAX) { // write identical k-mers
				for (j = buf_pos + 1; j < w; ++j) // these two loops make sure the output is sorted
					if (min.x == buf[j].x && min.y != buf[j].y) kv_push(mm128_t, km, *p, buf[j]);
				for (j = 0; j <= buf_pos; ++j)
					if (min.x == buf[j].x && min.y != buf[j].y) kv_push(mm128_t, km, *p, buf[j]);
			}
		}
		if (++buf_pos == w) buf_pos = 0;
	}
	if (min.x != UINT64_MAX)
		kv_push(mm128_t, km, *p, min);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1158, %rsp           # imm = 0x1158
movq %r9, 0x90(%rsp)
movq %rcx, 0x58(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdi, 0x18(%rsp)
movq %r8, 0x30(%rsp)
leal (%r8,%r8), %ecx
pushq $-0x1
popq %rax
shlq %cl, %rax
movq %rax, 0x70(%rsp)
movl %edx, 0x14(%rsp)
testl %edx, %edx
jle 0x12305
movq 0x58(%rsp), %rax
decl %eax
cmpl $0xfe, %eax
ja 0x12305
movq 0x30(%rsp), %rax
decl %eax
cmpl $0x1c, %eax
jae 0x12305
movq 0x1198(%rsp), %r14
movq 0x30(%rsp), %rax
leal -0x2(,%rax,2), %ebx
movq 0x58(%rsp), %r15
movl %r15d, %edx
shll $0x4, %edx
leaq 0x150(%rsp), %rdi
movl $0xff, %esi
callq 0x21b0
movq %r14, %r9
xorps %xmm0, %xmm0
movups %xmm0, 0xd0(%rsp)
movups %xmm0, 0xe0(%rsp)
movups %xmm0, 0xf0(%rsp)
movups %xmm0, 0x100(%rsp)
movups %xmm0, 0x110(%rsp)
movups %xmm0, 0x120(%rsp)
movups %xmm0, 0x130(%rsp)
movups %xmm0, 0x140(%rsp)
movq 0x8(%r14), %rcx
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl %r15d
addq (%r14), %rax
cmpq %rax, %rcx
jae 0x11b4a
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rdx
shrq $0x10, %rdx
orq %rax, %rdx
incq %rdx
movq %rdx, 0x8(%r9)
movq 0x10(%r9), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x10c04
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
notq 0x70(%rsp)
movl %ebx, %eax
movq %rax, 0xa0(%rsp)
shlq $0x20, 0x90(%rsp)
movq 0x30(%rsp), %rax
movq 0x58(%rsp), %rcx
leal (%rax,%rcx), %edx
movl %edx, 0x8c(%rsp)
leal -0x1(%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
movl 0x14(%rsp), %eax
movq %rax, 0xa8(%rsp)
movl %ecx, %r11d
leaq 0x20c7c(%rip), %rbx     # 0x32810
movq $0x0, 0x78(%rsp)
movq $0x0, 0x50(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x60(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
pushq $-0x1
popq %rbp
movq %rbp, %r15
xorl %r13d, %r13d
movq 0x98(%rsp), %r10
movq %r11, 0x48(%rsp)
movl 0x14(%rsp), %eax
movl %eax, %ecx
subl %r13d, %ecx
jle 0x1229a
movslq %r13d, %rdx
movzbl (%r10,%rdx), %eax
movzbl (%rax,%rbx), %eax
cmpq $0x3, %rax
ja 0x11c5c
cmpl $0x0, 0x1190(%rsp)
je 0x11f4d
leaq 0x1(%rdx), %rsi
cmpl 0x14(%rsp), %esi
jge 0x11f45
movzbl (%r10,%rsi), %esi
cmpb %al, (%rsi,%rbx)
jne 0x11f45
leaq (%r10,%rdx), %rdi
pushq $0x2
popq %rsi
leaq (%rdx,%rsi), %r8
cmpq 0xa8(%rsp), %r8
jge 0x12118
movzbl (%rdi,%rsi), %r8d
cmpb %al, (%r8,%rbx)
jne 0x12116
incq %rsi
jmp 0x11c36
movq %r13, 0x38(%rsp)
movq %r14, 0x20(%rsp)
andq $0x0, 0xc8(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
xorl %edi, %edi
movl $0x0, 0x10(%rsp)
pushq $-0x1
popq %rbx
movq %rbx, %rcx
movq %r12, 0x68(%rsp)
movslq %r12d, %r14
movq %r14, %r12
shlq $0x4, %r12
movq %rbx, 0x150(%rsp,%r12)
movq %rcx, 0x80(%rsp)
movq %rcx, 0x158(%rsp,%r12)
movq %rdi, 0x40(%rsp)
cmpl 0x2c(%rsp), %edi
jne 0x11deb
cmpq $-0x1, %rbp
je 0x11deb
leaq 0x1(%r14), %r13
leaq 0x160(%rsp), %rax
addq %rax, %r12
cmpq %r11, %r13
jge 0x11d53
cmpq (%r12), %rbp
jne 0x11d4a
cmpq %r15, 0x8(%r12)
je 0x11d4a
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x11d36
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movups (%r12), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r13
addq $0x10, %r12
jmp 0x11ce0
movq 0x68(%rsp), %rax
testl %eax, %eax
movl $0x0, %r12d
cmovgl %eax, %r12d
shlq $0x4, %r12
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x11deb
cmpq 0x150(%rsp,%r13), %rbp
jne 0x11de5
cmpq %r15, 0x158(%rsp,%r13)
je 0x11de5
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x11dc8
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq (%rsp,%r13), %rdx
addq $0x150, %rdx            # imm = 0x150
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
shlq $0x4, %rcx
movups (%rdx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r13
jmp 0x11d6b
cmpq %rbp, %rbx
jbe 0x11ebc
movq 0x68(%rsp), %r12
cmpl 0xc(%rsp), %r12d
jne 0x11f68
movq 0x40(%rsp), %rax
cmpl 0x2c(%rsp), %eax
movq 0x38(%rsp), %r13
jl 0x11e72
cmpq $-0x1, %rbp
je 0x11e72
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x11e5e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movq %rbp, (%rax,%rcx)
movq %r15, 0x8(%rax,%rcx)
leal 0x1(%r12), %eax
movslq %eax, %rbp
movq %rbp, %rax
shlq $0x4, %rax
leaq 0x158(%rsp), %rcx
addq %rax, %rcx
movq %rbp, %rdx
movl %r12d, 0xc(%rsp)
pushq $-0x1
popq %rbx
cmpq %r11, %rdx
jge 0x11f75
movq -0x8(%rcx), %rsi
cmpq %rsi, %rbx
jb 0x11eb3
movq (%rcx), %r15
movl %edx, 0xc(%rsp)
movq %rsi, %rbx
incq %rdx
addq $0x10, %rcx
jmp 0x11e97
movq 0x40(%rsp), %rax
cmpl 0x8c(%rsp), %eax
movq 0x68(%rsp), %r12
jl 0x12045
cmpq $-0x1, %rbp
movq 0x20(%rsp), %r14
movq 0x38(%rsp), %r13
je 0x11f3b
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x11f27
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movq %rbp, (%rax,%rcx)
movq %r15, 0x8(%rax,%rcx)
movl %r12d, 0xc(%rsp)
jmp 0x12054
pushq $0x1
popq %rcx
jmp 0x1211e
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %edx
movq 0x30(%rsp), %rcx
cmpl %ecx, %edx
cmovgel %ecx, %edx
movl %edx, 0x10(%rsp)
jmp 0x1219a
movq %rbp, %rbx
movq 0x20(%rsp), %r14
jmp 0x120e9
leaq 0x158(%rsp), %rcx
xorl %edx, %edx
cmpq %r14, %rdx
jg 0x11fa0
movq -0x8(%rcx), %rsi
cmpq %rsi, %rbx
jb 0x11f97
movq (%rcx), %r15
movl %edx, 0xc(%rsp)
movq %rsi, %rbx
incq %rdx
addq $0x10, %rcx
jmp 0x11f7f
movq 0x40(%rsp), %rcx
cmpl 0x2c(%rsp), %ecx
jl 0x1203b
cmpq $-0x1, %rbx
je 0x1203b
leaq (%rsp,%rax), %r12
addq $0x150, %r12            # imm = 0x150
cmpq %r11, %rbp
jge 0x12061
cmpq (%r12), %rbx
jne 0x12032
cmpq 0x8(%r12), %r15
je 0x12032
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x1201e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movups (%r12), %xmm0
movups %xmm0, (%rax,%rcx)
incq %rbp
addq $0x10, %r12
jmp 0x11fc4
movq 0x20(%rsp), %r14
jmp 0x120ee
movl %r12d, 0xc(%rsp)
movq 0x20(%rsp), %r14
movq 0x38(%rsp), %r13
movq 0x80(%rsp), %r15
jmp 0x120ee
leaq 0x150(%rsp), %r12
xorl %r13d, %r13d
cmpq %r14, %r13
jg 0x120df
cmpq (%r12), %rbx
jne 0x120d6
cmpq 0x8(%r12), %r15
je 0x120d6
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x120c2
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x48(%rsp), %r11
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movups (%r12), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r13
addq $0x10, %r12
jmp 0x1206c
movq 0x20(%rsp), %r14
movq 0x68(%rsp), %r12
movq 0x38(%rsp), %r13
incl %r12d
cmpl 0x58(%rsp), %r12d
movl $0x0, %eax
cmovel %eax, %r12d
movq %rbx, %rbp
movq 0x98(%rsp), %r10
leaq 0x206ff(%rip), %rbx     # 0x32810
jmp 0x121d8
movl %esi, %ecx
addl %ecx, %r13d
decl %r13d
movq 0x50(%rsp), %rdi
leal 0x1(%rdi), %edx
movl %edx, 0xcc(%rsp)
movq 0x60(%rsp), %rsi
addl %edi, %esi
andl $0x1f, %esi
movl %ecx, 0xd0(%rsp,%rsi,4)
addl %ecx, 0x10(%rsp)
cmpl 0x30(%rsp), %edi
jge 0x12151
movl %edx, %ecx
movq %rcx, 0x50(%rsp)
jmp 0x1219a
testl %edx, %edx
je 0x1218a
movq 0x60(%rsp), %rdx
movslq %edx, %rcx
incl %edx
movl %edx, 0xc8(%rsp)
movl 0xd0(%rsp,%rcx,4), %ecx
andl $0x1f, %edx
movl %edx, 0xc8(%rsp)
movq 0x50(%rsp), %rsi
movl %esi, 0xcc(%rsp)
movq %rdx, 0x60(%rsp)
jmp 0x12196
movq $0x0, 0x50(%rsp)
pushq $-0x1
popq %rcx
subl %ecx, 0x10(%rsp)
leaq (%rax,%r14,4), %r14
andq 0x70(%rsp), %r14
movq %r14, 0xb0(%rsp)
movq 0x78(%rsp), %rdx
shrq $0x2, %rdx
xorq $0x3, %rax
movq 0xa0(%rsp), %rcx
shlq %cl, %rax
orq %rdx, %rax
movq %rax, 0xb8(%rsp)
cmpq %rax, %r14
jne 0x121e0
movq %r14, 0x78(%rsp)
incl %r13d
jmp 0x11be4
movq 0x40(%rsp), %rcx
incl %ecx
movq %rcx, %rdi
cmpl 0x30(%rsp), %ecx
movq %r14, 0x20(%rsp)
movq %rax, 0x78(%rsp)
movq %r13, 0x38(%rsp)
jl 0x11c8b
movl 0x10(%rsp), %r8d
cmpl $0xff, %r8d
jg 0x11c8b
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %r14
setae %al
movq 0xb0(%rsp,%rax,8), %rcx
movq %rcx, %rdx
notq %rdx
shlq $0x15, %rcx
addq %rdx, %rcx
movq 0x70(%rsp), %rsi
andq %rsi, %rcx
movq %rcx, %rdx
shrq $0x18, %rdx
xorq %rcx, %rdx
imulq $0x109, %rdx, %rcx      # imm = 0x109
andq %rsi, %rcx
movq %rcx, %rdx
shrq $0xe, %rdx
xorq %rcx, %rdx
imulq $0x15, %rdx, %rcx
andq %rsi, %rcx
movq %rcx, %rdx
shrq $0x1c, %rdx
xorq %rcx, %rdx
movl $0x80000001, %ecx       # imm = 0x80000001
imulq %rcx, %rdx
andq %rsi, %rdx
shlq $0x8, %rdx
movslq %r8d, %rbx
orq %rdx, %rbx
leal (,%r13,2), %ecx
orq 0x90(%rsp), %rcx
orq %rax, %rcx
jmp 0x11c91
cmpq $-0x1, %rbp
je 0x122f3
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x122df
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rdx
cmovneq %rsi, %rdx
movq %rdx, 0x8(%r9)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x10c04
movq 0x1198(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
shlq $0x4, %rcx
movq %rbp, (%rax,%rcx)
movq %r15, 0x8(%rax,%rcx)
addq $0x1158, %rsp           # imm = 0x1158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1703f(%rip), %rdi     # 0x2934b
leaq 0x1706c(%rip), %rsi     # 0x2937f
leaq 0x1706e(%rip), %rcx     # 0x29388
pushq $0x54
popq %rdx
callq 0x21a0
nop