// File: /workspace/github_c_makefile_star2_O1/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, 0x70(%rsp)
movl %r8d, %r14d
movl %ecx, %r15d
movq %rsi, 0x98(%rsp)
leal (%r14,%r14), %ecx
movq $-0x1, %rax
shlq %cl, %rax
movq %rax, 0x48(%rsp)
movq %rdi, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
testl %edx, %edx
jle 0xec95
leal -0x1(%r15), %eax
movl %eax, 0x3c(%rsp)
cmpl $0xfe, %eax
ja 0xec95
leal -0x1(%r14), %eax
cmpl $0x1c, %eax
jae 0xec95
movl %edx, %r12d
movq 0x10e0(%rsp), %rbp
leal -0x2(,%r14,2), %ebx
movl %r15d, %edx
shll $0x4, %edx
leaq 0xa0(%rsp), %rdi
movl $0xff, %esi
callq 0x2180
movq 0x8(%rbp), %rcx
movl %r12d, %eax
xorl %edx, %edx
divl %r15d
addq (%rbp), %rax
cmpq %rax, %rcx
jae 0xe5f1
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(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movq %rax, 0x10(%rbp)
notq 0x48(%rsp)
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl %ebx, %ecx
movq %rcx, 0x80(%rsp)
movq %rax, %rbx
shlq $0x20, 0x70(%rsp)
leal (%r14,%r15), %eax
movl %eax, 0x5c(%rsp)
movq %r14, 0x88(%rsp)
leal (%r14,%r15), %eax
decl %eax
movl %eax, 0x2c(%rsp)
cmpl $0x2, %r12d
movl $0x1, %eax
cmovgel %r12d, %eax
movq %rax, 0x90(%rsp)
movq $-0x1, %r12
xorl %r9d, %r9d
movq $-0x1, %r13
movl $0x0, 0x8(%rsp)
xorl %r10d, %r10d
xorl %edx, %edx
movq %r15, 0x20(%rsp)
movq 0x98(%rsp), %rax
movzbl (%rax,%r9), %eax
leaq 0x23c82(%rip), %rcx     # 0x32300
movzbl (%rax,%rcx), %esi
xorl %r11d, %r11d
cmpq $0x3, %rsi
movq %r9, 0x78(%rsp)
ja 0xe6ed
leal 0x1(%rdx), %r11d
movq 0x88(%rsp), %r8
cmpl %r8d, %r11d
movl %r8d, %eax
cmovll %r11d, %eax
leaq (%rsi,%rbx,4), %rbx
andq 0x48(%rsp), %rbx
movq %rbx, 0x60(%rsp)
movq 0x40(%rsp), %rdi
shrq $0x2, %rdi
xorq $0x3, %rsi
movq 0x80(%rsp), %rcx
shlq %cl, %rsi
orq %rdi, %rsi
movq %rsi, 0x68(%rsp)
movq $-0x1, %rdi
movq %rsi, 0x40(%rsp)
cmpq %rsi, %rbx
jne 0xe700
movl %edx, %r11d
jmp 0xe797
movq $-0x1, %rdi
movq $-0x1, %r14
jmp 0xe7a9
cmpl %r8d, %r11d
setl %cl
cmpl $0x100, %eax            # imm = 0x100
setge %dl
orb %cl, %dl
jne 0xe797
xorl %ecx, %ecx
cmpq 0x40(%rsp), %rbx
setae %cl
movq 0x60(%rsp,%rcx,8), %rdx
movq %rdx, %rsi
notq %rsi
shlq $0x15, %rdx
addq %rsi, %rdx
movq 0x48(%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
leaq (%rsi,%rsi,4), %rdx
leaq (%rsi,%rdx,4), %rdx
andq %rdi, %rdx
movq %rdx, %rsi
shrq $0x1c, %rsi
xorq %rdx, %rsi
movq %rsi, %rdx
shlq $0x1f, %rdx
addq %rsi, %rdx
andq %rdi, %rdx
shlq $0x8, %rdx
movslq %eax, %r14
orq %rdx, %r14
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rdx
leaq (%rax,%rdx,2), %rdi
orq %rcx, %rdi
jmp 0xe79e
movq $-0x1, %r14
cmpq 0x40(%rsp), %rbx
je 0xec13
movq %rbx, 0x10(%rsp)
movslq %r10d, %rax
shlq $0x4, %rax
movq %r14, 0xa0(%rsp,%rax)
movq %rdi, 0x50(%rsp)
movq %rdi, 0xa8(%rsp,%rax)
cmpl 0x2c(%rsp), %r11d
movq %r10, 0x30(%rsp)
movl %r11d, 0xc(%rsp)
jne 0xe910
cmpq $-0x1, %r12
je 0xe910
leal 0x1(%r10), %eax
cmpl 0x20(%rsp), %eax
jge 0xe87d
cltq
shlq $0x4, %rax
leaq (%rsp,%rax), %rbx
addq $0xa0, %rbx
movl 0x3c(%rsp), %r15d
subl %r10d, %r15d
cmpq (%rbx), %r12
jne 0xe874
cmpq %r13, 0x8(%rbx)
je 0xe874
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xe859
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rax
movups (%rbx), %xmm0
movups %xmm0, (%rcx,%rax)
addq $0x10, %rbx
decl %r15d
jne 0xe810
testl %r10d, %r10d
jle 0xe910
movl %r10d, %ebx
shlq $0x4, %rbx
xorl %r15d, %r15d
cmpq 0xa0(%rsp,%r15), %r12
jne 0xe907
cmpq %r13, 0xa8(%rsp,%r15)
je 0xe907
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xe8e2
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
leaq (%rsp,%r15), %rax
addq $0xa0, %rax
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
shlq $0x4, %rcx
movups (%rax), %xmm0
movups %xmm0, (%rdx,%rcx)
addq $0x10, %r15
cmpq %r15, %rbx
jne 0xe890
cmpq %r12, %r14
jbe 0xe9de
cmpl 0x8(%rsp), %r10d
jne 0xea5e
cmpl 0x2c(%rsp), %r11d
movq 0x20(%rsp), %r15
jl 0xe991
cmpq $-0x1, %r12
je 0xe991
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xe974
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rax
movq %r12, (%rcx,%rax)
movq %r13, 0x8(%rcx,%rax)
leal 0x1(%r10), %ebx
movq $-0x1, %r14
cmpl %r15d, %ebx
jge 0xea70
movslq %ebx, %rax
shlq $0x4, %rax
leaq 0xa8(%rsp), %rcx
addq %rcx, %rax
movl %ebx, %ecx
movl 0x8(%rsp), %edi
movq -0x8(%rax), %rdx
cmpq %rdx, %r14
jb 0xe9ce
movq (%rax), %r13
movl %ecx, %edi
movq %rdx, %r14
incl %ecx
addq $0x10, %rax
cmpl %ecx, %r15d
jne 0xe9bd
jmp 0xea74
cmpl 0x5c(%rsp), %r11d
jl 0xebed
cmpq $-0x1, %r12
movq 0x20(%rsp), %r15
je 0xea66
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
movq 0x10(%rsp), %rbx
jne 0xea37
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rax
movq %r12, (%rcx,%rax)
movq %r13, 0x8(%rcx,%rax)
movl %r10d, 0x8(%rsp)
jmp 0xebfc
movq %r12, %r14
jmp 0xebe1
movl %r10d, 0x8(%rsp)
jmp 0xebf7
movl 0x8(%rsp), %edi
testl %r10d, %r10d
js 0xeaa2
movl %ebx, %eax
leaq 0xa8(%rsp), %rcx
xorl %edx, %edx
movq -0x8(%rcx), %rsi
cmpq %rsi, %r14
jb 0xea96
movq (%rcx), %r13
movl %edx, %edi
movq %rsi, %r14
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %rax
jne 0xea85
movl %edi, 0x8(%rsp)
cmpl 0x2c(%rsp), %r11d
jl 0xebe6
cmpq $-0x1, %r14
je 0xebe6
cmpl %r15d, %ebx
jge 0xeb4f
movslq %ebx, %rax
shlq $0x4, %rax
leaq (%rsp,%rax), %r12
addq $0xa0, %r12
movl 0x3c(%rsp), %r15d
subl %r10d, %r15d
cmpq (%r12), %r14
jne 0xeb46
cmpq 0x8(%r12), %r13
je 0xeb46
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xeb29
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rax
movups (%r12), %xmm0
movups %xmm0, (%rcx,%rax)
addq $0x10, %r12
decl %r15d
jne 0xeade
testl %r10d, %r10d
js 0xebe1
movl %ebx, %ebx
shlq $0x4, %rbx
xorl %r15d, %r15d
cmpq 0xa0(%rsp,%r15), %r14
jne 0xebd8
cmpq 0xa8(%rsp,%r15), %r13
je 0xebd8
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xebb3
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movl 0xc(%rsp), %r11d
movq 0x30(%rsp), %r10
movq %rax, 0x10(%rbp)
leaq (%rsp,%r15), %rax
addq $0xa0, %rax
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
shlq $0x4, %rcx
movups (%rax), %xmm0
movups %xmm0, (%rdx,%rcx)
addq $0x10, %r15
cmpq %r15, %rbx
jne 0xeb61
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
jmp 0xec01
movl %r10d, 0x8(%rsp)
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x50(%rsp), %r13
incl %r10d
cmpl %r15d, %r10d
movl $0x0, %eax
cmovel %eax, %r10d
movq %r14, %r12
movq 0x78(%rsp), %r9
incq %r9
movl %r11d, %edx
cmpq 0x90(%rsp), %r9
jne 0xe66a
cmpq $-0x1, %r12
je 0xec83
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0xec66
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %edx
cmovneq %rcx, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rax
movq %r12, (%rcx,%rax)
movq %r13, 0x8(%rcx,%rax)
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19c3f(%rip), %rdi     # 0x288db
leaq 0x19c6c(%rip), %rsi     # 0x2890f
leaq 0x19c6e(%rip), %rcx     # 0x28918
movl $0x3e, %edx
callq 0x2160