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

	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);
	kv_resize(mg128_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]];
		mg128_t info = { UINT64_MAX, UINT64_MAX };
		if (c < 4) { // not an ambiguous base
			int z;
			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, 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(mg128_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(mg128_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(mg128_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(mg128_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(mg128_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(mg128_t, km, *p, buf[j]);
			}
		}
		if (++buf_pos == w) buf_pos = 0;
	}
	if (min.x != UINT64_MAX)
		kv_push(mg128_t, km, *p, min);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10a8, %rsp           # imm = 0x10A8
movq %r9, 0x68(%rsp)
movl %r8d, %r12d
movq %rcx, 0x38(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdi, 0x18(%rsp)
leal (%r12,%r12), %ecx
pushq $-0x1
popq %rax
shlq %cl, %rax
movq %rax, 0x50(%rsp)
testl %edx, %edx
jle 0xc939
movq 0x38(%rsp), %rax
decl %eax
cmpl $0xfe, %eax
ja 0xc939
leal -0x1(%r12), %eax
cmpl $0x1c, %eax
jae 0xc939
movl %edx, %ebp
movq 0x10e0(%rsp), %r14
leal -0x2(,%r12,2), %ebx
movq 0x38(%rsp), %r15
movl %r15d, %edx
shll $0x4, %edx
leaq 0xa0(%rsp), %rdi
movl $0xff, %esi
callq 0x2180
movq %r14, %r8
movq 0x8(%r14), %rcx
movl %ebp, %eax
xorl %edx, %edx
divl %r15d
addq (%r14), %rax
cmpq %rax, %rcx
jae 0xc2e8
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(%r8)
movq 0x10(%r8), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x2774
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
notq 0x50(%rsp)
movl %ebx, %eax
movq %rax, 0x70(%rsp)
shlq $0x20, 0x68(%rsp)
movq 0x38(%rsp), %rax
leal (%r12,%rax), %ecx
movl %ecx, 0x64(%rsp)
leal (%r12,%rax), %ecx
decl %ecx
movl %ecx, 0x24(%rsp)
movl %eax, %r14d
movl %ebp, %eax
movq %rax, 0x88(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x40(%rsp)
xorl %r13d, %r13d
xorl %edx, %edx
movq $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
pushq $-0x1
popq %r15
movq %r15, %rbx
movq %r12, 0x78(%rsp)
cmpq 0x88(%rsp), %rbp
je 0xc8ce
movq 0x80(%rsp), %rax
movzbl (%rax,%rbp), %eax
leaq 0x21f94(%rip), %rcx     # 0x2e300
movzbl (%rax,%rcx), %esi
movl $0x0, 0x8(%rsp)
cmpq $0x3, %rsi
ja 0xc3d9
leal 0x1(%rdx), %r9d
cmpl %r12d, %r9d
movl %r12d, %eax
cmovll %r9d, %eax
leaq (%rsi,%r13,4), %r13
andq 0x50(%rsp), %r13
movq %r13, 0x90(%rsp)
movq 0x40(%rsp), %rdi
shrq $0x2, %rdi
xorq $0x3, %rsi
movq 0x70(%rsp), %rcx
shlq %cl, %rsi
orq %rdi, %rsi
movq %rsi, 0x98(%rsp)
movq %rsi, 0x40(%rsp)
cmpq %rsi, %r13
jne 0xc695
movq %r13, 0x40(%rsp)
movl %edx, 0x8(%rsp)
jmp 0xc8c0
movq %r13, 0x48(%rsp)
movq %rbp, 0x28(%rsp)
pushq $-0x1
popq %r12
movq %r12, %rax
movslq 0x10(%rsp), %r9
movq %r9, %r13
shlq $0x4, %r13
movq %r12, 0xa0(%rsp,%r13)
movq %rax, 0x58(%rsp)
movq %rax, 0xa8(%rsp,%r13)
movl 0x8(%rsp), %eax
cmpl 0x24(%rsp), %eax
movq %r9, 0x30(%rsp)
jne 0xc540
cmpq $-0x1, %r15
je 0xc540
leaq 0x1(%r9), %rbp
leaq 0xb0(%rsp), %rax
addq %rax, %r13
cmpq %r14, %rbp
jge 0xc4a9
cmpq (%r13), %r15
jne 0xc4a0
cmpq %rbx, 0x8(%r13)
je 0xc4a0
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc48c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x30(%rsp), %r9
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movups (%r13), %xmm0
movups %xmm0, (%rax,%rcx)
incq %rbp
addq $0x10, %r13
jmp 0xc437
movq 0x10(%rsp), %rax
testl %eax, %eax
movl $0x0, %r13d
cmovgl %eax, %r13d
shlq $0x4, %r13
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0xc540
cmpq 0xa0(%rsp,%rbp), %r15
jne 0xc53a
cmpq %rbx, 0xa8(%rsp,%rbp)
je 0xc53a
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc51d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x30(%rsp), %r9
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq (%rsp,%rbp), %rdx
addq $0xa0, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r8)
shlq $0x4, %rcx
movups (%rdx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %rbp
jmp 0xc4c0
cmpq %r15, %r12
jbe 0xc616
movl 0xc(%rsp), %eax
cmpl %eax, 0x10(%rsp)
movq 0x48(%rsp), %r13
jne 0xc737
movl 0x8(%rsp), %eax
cmpl 0x24(%rsp), %eax
movq 0x28(%rsp), %rbp
jl 0xc5c9
cmpq $-0x1, %r15
je 0xc5c9
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc5b5
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x30(%rsp), %r9
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movq %r15, (%rax,%rcx)
movq %rbx, 0x8(%rax,%rcx)
movq 0x10(%rsp), %rsi
leal 0x1(%rsi), %eax
movslq %eax, %r15
movq %r15, %rax
shlq $0x4, %rax
leaq 0xa8(%rsp), %rcx
addq %rax, %rcx
movq %r15, %rdx
movl %esi, 0xc(%rsp)
pushq $-0x1
popq %r12
cmpq %r14, %rdx
jge 0xc744
movq -0x8(%rcx), %rsi
cmpq %rsi, %r12
jb 0xc60d
movq (%rcx), %rbx
movl %edx, 0xc(%rsp)
movq %rsi, %r12
incq %rdx
addq $0x10, %rcx
jmp 0xc5f1
movl 0x8(%rsp), %eax
cmpl 0x64(%rsp), %eax
movq 0x48(%rsp), %r13
jl 0xc808
cmpq $-0x1, %r15
movq 0x28(%rsp), %rbp
je 0xc687
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc673
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movq %r15, (%rax,%rcx)
movq %rbx, 0x8(%rax,%rcx)
movq 0x10(%rsp), %rcx
movl %ecx, 0xc(%rsp)
jmp 0xc816
movl %r9d, 0x8(%rsp)
cmpl %r12d, %r9d
setl %cl
cmpl $0x100, %eax            # imm = 0x100
setge %dl
orb %cl, %dl
movq %rbp, 0x28(%rsp)
movq %r13, 0x48(%rsp)
jne 0xc3e3
xorl %ecx, %ecx
cmpq 0x40(%rsp), %r13
setae %cl
movq 0x90(%rsp,%rcx,8), %rdx
movq %rdx, %rsi
notq %rsi
shlq $0x15, %rdx
addq %rsi, %rdx
movq 0x50(%rsp), %rdi
andq %rdi, %rdx
movq %rdx, %rsi
shrq $0x18, %rsi
xorq %rdx, %rsi
imulq $0x109, %rsi, %rdx      # imm = 0x109
andq %rdi, %rdx
movq %rdx, %rsi
shrq $0xe, %rsi
xorq %rdx, %rsi
imulq $0x15, %rsi, %rdx
andq %rdi, %rdx
movq %rdx, %rsi
shrq $0x1c, %rsi
xorq %rdx, %rsi
movl $0x80000001, %edx       # imm = 0x80000001
imulq %rdx, %rsi
andq %rdi, %rsi
shlq $0x8, %rsi
movslq %eax, %r12
orq %rsi, %r12
movq 0x68(%rsp), %rax
leaq (%rax,%rbp,2), %rax
orq %rcx, %rax
jmp 0xc3ea
movq %r15, %r12
movq 0x28(%rsp), %rbp
jmp 0xc8a0
leaq 0xa8(%rsp), %rcx
xorl %edx, %edx
cmpq %r9, %rdx
jg 0xc76f
movq -0x8(%rcx), %rsi
cmpq %rsi, %r12
jb 0xc766
movq (%rcx), %rbx
movl %edx, 0xc(%rsp)
movq %rsi, %r12
incq %rdx
addq $0x10, %rcx
jmp 0xc74e
movl 0x8(%rsp), %ecx
cmpl 0x24(%rsp), %ecx
jl 0xc8a0
cmpq $-0x1, %r12
je 0xc8a0
leaq (%rsp,%rax), %r13
addq $0xa0, %r13
cmpq %r14, %r15
jge 0xc820
cmpq (%r13), %r12
jne 0xc7ff
cmpq 0x8(%r13), %rbx
je 0xc7ff
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc7eb
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x30(%rsp), %r9
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movups (%r13), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r15
addq $0x10, %r13
jmp 0xc792
movq 0x10(%rsp), %rcx
movl %ecx, 0xc(%rsp)
movq 0x28(%rsp), %rbp
movq 0x58(%rsp), %rbx
jmp 0xc8a5
leaq 0xa0(%rsp), %r15
xorl %r13d, %r13d
cmpq %r9, %r13
jg 0xc89b
cmpq (%r15), %r12
jne 0xc892
cmpq 0x8(%r15), %rbx
je 0xc892
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc87f
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x30(%rsp), %r9
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movups (%r15), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r13
addq $0x10, %r15
jmp 0xc82b
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rcx
incl %ecx
cmpl 0x38(%rsp), %ecx
movl $0x0, %eax
cmovel %eax, %ecx
movq %rcx, 0x10(%rsp)
movq %r12, %r15
movq 0x78(%rsp), %r12
incq %rbp
movl 0x8(%rsp), %eax
movl %eax, %edx
jmp 0xc34b
cmpq $-0x1, %r15
je 0xc927
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0xc913
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r8)
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2774
movq 0x10e0(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
shlq $0x4, %rcx
movq %r15, (%rax,%rcx)
movq %rbx, 0x8(%rax,%rcx)
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16fc3(%rip), %rdi     # 0x23903
leaq 0x16ff0(%rip), %rsi     # 0x23937
leaq 0x16ff2(%rip), %rcx     # 0x23940
pushq $0x3e
popq %rdx
callq 0x2160
nop