// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap/sketch.c
// Function: mm_sketch
void mm_sketch(const char *str, int len, int w, int k, uint32_t rid, 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;
	mm128_t *buf, min = { UINT64_MAX, UINT64_MAX };

	assert(len > 0 && w > 0 && k > 0);
	buf = (mm128_t*)alloca(w * 16);
	memset(buf, 0xff, w * 16);

	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;
			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
			if (++l >= k)
				info.x = hash64(kmer[z], mask), info.y = (uint64_t)rid<<32 | (uint32_t)i<<1 | z;
		} else l = 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) { // 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, *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, *p, buf[j]);
		}
		if (info.x <= min.x) { // a new minimum; then write the old min
			if (l >= w + k) kv_push(mm128_t, *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) kv_push(mm128_t, *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) { // 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, *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, *p, buf[j]);
			}
		}
		if (++buf_pos == w) buf_pos = 0;
	}
	if (min.x != UINT64_MAX)
		kv_push(mm128_t, *p, min);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, -0xa0(%rbp)
movq %rdx, -0x90(%rbp)
movq %rdi, -0xc8(%rbp)
movq %rcx, -0x78(%rbp)
addl %ecx, %ecx
pushq $-0x1
popq %rax
movq %rcx, -0x80(%rbp)
shlq %cl, %rax
movq %rax, -0x88(%rbp)
testl %esi, %esi
jle 0x6b2f
cmpl $0x0, -0x90(%rbp)
jle 0x6b2f
cmpl $0x0, -0x78(%rbp)
jle 0x6b2f
movq %r9, %rbx
movl %esi, %r12d
notq -0x88(%rbp)
movq -0x80(%rbp), %rax
addl $-0x2, %eax
movq %rax, -0x80(%rbp)
movq -0x90(%rbp), %r14
movl %r14d, %edx
shll $0x4, %edx
movq %rsp, %r15
subq %rdx, %r15
movq %r15, %rsp
movq %r15, %rdi
movl $0xff, %esi
callq 0x2130
movq %r15, %rdi
shlq $0x20, -0xa0(%rbp)
movq -0x78(%rbp), %rax
leal (%rax,%r14), %ecx
movl %ecx, -0x94(%rbp)
addl %r14d, %eax
decl %eax
movl %eax, -0x3c(%rbp)
movl %r14d, %r8d
movl %r12d, %eax
movq %rax, -0xd0(%rbp)
leaq 0x10(%r15), %rax
movq %rax, -0xc0(%rbp)
leaq 0x8(%r15), %rax
movq %rax, -0xa8(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x70(%rbp)
xorl %esi, %esi
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
pushq $-0x1
popq %r13
movq %r13, %r9
movq %rdi, -0x50(%rbp)
movq %r8, -0x48(%rbp)
cmpq -0xd0(%rbp), %r12
movq %r9, -0x38(%rbp)
je 0x6ad0
movq -0xc8(%rbp), %rax
movzbl (%rax,%r12), %eax
leaq 0x4b9d(%rip), %rcx      # 0xb1a0
movzbl (%rax,%rcx), %eax
cmpq $0x3, %rax
ja 0x664e
leaq (%rax,%rsi,4), %rsi
andq -0x88(%rbp), %rsi
movq %rsi, -0xe0(%rbp)
movq -0x70(%rbp), %rdx
shrq $0x2, %rdx
xorq $0x3, %rax
movq -0x80(%rbp), %rcx
shlq %cl, %rax
orq %rdx, %rax
movq %rax, -0xd8(%rbp)
cmpq %rax, %rsi
jne 0x6875
movq %rsi, -0x70(%rbp)
jmp 0x6ac8
movq %rsi, -0xb8(%rbp)
movq %r12, -0x58(%rbp)
xorl %r10d, %r10d
pushq $-0x1
popq %r14
movq %r14, %rcx
movq %r15, -0x60(%rbp)
movslq %r15d, %r11
movq %r11, %r15
shlq $0x4, %r15
movq %r14, (%rdi,%r15)
movq %rcx, -0xb0(%rbp)
movq %rcx, 0x8(%rdi,%r15)
movl %r10d, -0x30(%rbp)
cmpl -0x3c(%rbp), %r10d
movq %r11, -0x68(%rbp)
jne 0x6793
leaq 0x1(%r11), %r12
addq -0xc0(%rbp), %r15
cmpq %r8, %r12
jge 0x670c
cmpq (%r15), %r13
jne 0x6703
cmpq %r9, 0x8(%r15)
je 0x6703
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x66f0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x68(%rbp), %r11
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movups (%r15), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r12
addq $0x10, %r15
jmp 0x669e
movq -0x60(%rbp), %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
shlq $0x4, %r15
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x6793
cmpq (%rdi,%r12), %r13
jne 0x678d
cmpq %r9, 0x8(%rdi,%r12)
je 0x678d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x6777
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x68(%rbp), %r11
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq (%rdi,%r12), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
shlq $0x4, %rcx
movups (%rdx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r12
jmp 0x6723
cmpq %r13, %r14
jbe 0x6855
movq -0x60(%rbp), %r15
cmpl -0x2c(%rbp), %r15d
jne 0x690b
movl -0x30(%rbp), %eax
cmpl -0x3c(%rbp), %eax
movq -0x58(%rbp), %r12
jl 0x680c
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x67f8
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x68(%rbp), %r11
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movq %r13, (%rax,%rcx)
movq %r9, 0x8(%rax,%rcx)
leal 0x1(%r15), %eax
movq %r15, %rdx
movslq %eax, %r15
movq %r15, %r13
shlq $0x4, %r13
movq -0xa8(%rbp), %rax
addq %r13, %rax
movq %r15, %rcx
movl %edx, -0x2c(%rbp)
pushq $-0x1
popq %r14
cmpq %r8, %rcx
jge 0x6917
movq -0x8(%rax), %rdx
cmpq %rdx, %r14
jb 0x684c
movq (%rax), %r9
movl %ecx, -0x2c(%rbp)
movq %rdx, %r14
incq %rcx
addq $0x10, %rax
jmp 0x6831
movl -0x30(%rbp), %eax
cmpl -0x94(%rbp), %eax
movq -0x60(%rbp), %r15
jge 0x6944
movl %r15d, -0x2c(%rbp)
movq -0x58(%rbp), %r12
jmp 0x699e
movq %rax, -0x70(%rbp)
movl -0x30(%rbp), %eax
incl %eax
movl %eax, %r10d
cmpl -0x78(%rbp), %eax
movq %r12, -0x58(%rbp)
movq %rsi, -0xb8(%rbp)
jl 0x665c
xorl %eax, %eax
cmpq -0x70(%rbp), %rsi
setae %al
movq -0xe0(%rbp,%rax,8), %rcx
movq %rcx, %rdx
notq %rdx
shlq $0x15, %rcx
addq %rdx, %rcx
movq -0x88(%rbp), %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, %r14
shrq $0x1c, %r14
xorq %rcx, %r14
movl $0x80000001, %ecx       # imm = 0x80000001
imulq %rcx, %r14
andq %rsi, %r14
movq -0xa0(%rbp), %rcx
leaq (%rcx,%r12,2), %rcx
orq %rax, %rcx
jmp 0x6663
movq %r13, %r14
movq -0x58(%rbp), %r12
jmp 0x6aab
movq -0xa8(%rbp), %rax
xorl %ecx, %ecx
cmpq %r11, %rcx
jg 0x69aa
movq -0x8(%rax), %rdx
cmpq %rdx, %r14
jb 0x693b
movq (%rax), %r9
movl %ecx, -0x2c(%rbp)
movq %rdx, %r14
incq %rcx
addq $0x10, %rax
jmp 0x6920
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
movq -0x58(%rbp), %r12
jne 0x6986
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movq %r13, (%rax,%rcx)
movq %r9, 0x8(%rax,%rcx)
movl %r15d, -0x2c(%rbp)
movq -0xb0(%rbp), %r9
jmp 0x6aab
movl -0x30(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x6aa7
addq %rdi, %r13
movq %r9, -0x38(%rbp)
cmpq %r8, %r15
jge 0x6a2d
cmpq (%r13), %r14
jne 0x6a24
cmpq 0x8(%r13), %r9
je 0x6a24
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x6a10
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x68(%rbp), %r11
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movups (%r13), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r15
addq $0x10, %r13
jmp 0x69bd
movq %r12, %r13
movq %rdi, %r15
xorl %r12d, %r12d
cmpq %r11, %r12
jg 0x6aa4
cmpq (%r15), %r14
jne 0x6a9b
cmpq 0x8(%r15), %r9
je 0x6a9b
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x6a88
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x68(%rbp), %r11
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movups (%r15), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r12
addq $0x10, %r15
jmp 0x6a36
movq %r13, %r12
movq -0x60(%rbp), %r15
incl %r15d
cmpl -0x90(%rbp), %r15d
movl $0x0, %eax
cmovel %eax, %r15d
movq %r14, %r13
movq -0xb8(%rbp), %rsi
incq %r12
jmp 0x65df
cmpq $-0x1, %r13
je 0x6b20
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x6b0c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2200
movq -0x38(%rbp), %r9
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
shlq $0x4, %rcx
movq %r13, (%rax,%rcx)
movq %r9, 0x8(%rax,%rcx)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1df5(%rip), %rdi      # 0x892b
leaq 0x1e08(%rip), %rsi      # 0x8945
leaq 0x1e0a(%rip), %rcx      # 0x894e
pushq $0x3b
popq %rdx
callq 0x2110