// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movl %ecx, %r15d
movq %rsi, 0x88(%rsp)
movq %rdi, 0x20(%rsp)
leal (%r14,%r14), %ecx
movq $-0x1, %rax
shlq %cl, %rax
movq %rax, 0x58(%rsp)
testl %edx, %edx
jle 0xf04e
leal -0x1(%r15), %eax
movl %eax, 0x4c(%rsp)
cmpl $0xfe, %eax
ja 0xf04e
leal -0x1(%r14), %eax
cmpl $0x1c, %eax
jae 0xf04e
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 0xe9a1
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 0x20(%rsp), %rdi
callq 0x31ed
movq %rax, 0x10(%rbp)
notq 0x58(%rsp)
movl %ebx, %eax
movq %rax, 0x78(%rsp)
shlq $0x20, 0x68(%rsp)
leal (%r14,%r15), %eax
movl %eax, 0x64(%rsp)
movq %r15, 0x10(%rsp)
leal (%r14,%r15), %eax
decl %eax
movl %eax, 0x34(%rsp)
movl %r12d, %eax
movq %rax, 0x80(%rsp)
movq $-0x1, %r12
xorl %r15d, %r15d
movq $0x0, 0x50(%rsp)
movq $-0x1, %r13
movq $0x0, 0x8(%rsp)
xorl %r8d, %r8d
xorl %eax, %eax
xorl %r9d, %r9d
movq %r14, 0x28(%rsp)
movq 0x88(%rsp), %rcx
movzbl (%rcx,%r15), %ecx
leaq 0x238e7(%rip), %rdx     # 0x32300
movzbl (%rcx,%rdx), %esi
xorl %r10d, %r10d
cmpq $0x3, %rsi
ja 0xea7d
leal 0x1(%rax), %r10d
cmpl %r14d, %r10d
movl %r14d, %edx
cmovll %r10d, %edx
leaq (%rsi,%r9,4), %r9
andq 0x58(%rsp), %r9
movq %r9, 0x90(%rsp)
movq 0x50(%rsp), %rdi
shrq $0x2, %rdi
xorq $0x3, %rsi
movq 0x78(%rsp), %rcx
shlq %cl, %rsi
orq %rdi, %rsi
movq %rsi, 0x98(%rsp)
movq %rsi, 0x50(%rsp)
cmpq %rsi, %r9
jne 0xed4e
movq %r9, 0x50(%rsp)
jmp 0xefd2
movq %r9, 0x70(%rsp)
movq %r15, 0x18(%rsp)
movq $-0x1, %rcx
movq $-0x1, %rbx
movslq %r8d, %rax
shlq $0x4, %rax
movq %rbx, 0xa0(%rsp,%rax)
movq %rcx, 0x38(%rsp)
movq %rcx, 0xa8(%rsp,%rax)
cmpl 0x34(%rsp), %r10d
movq %r8, 0x40(%rsp)
movl %r10d, 0x4(%rsp)
jne 0xebf7
cmpq $-0x1, %r12
je 0xebf7
leal 0x1(%r8), %eax
cmpl 0x10(%rsp), %eax
jge 0xeb64
cltq
shlq $0x4, %rax
leaq (%rsp,%rax), %r15
addq $0xa0, %r15
movl 0x4c(%rsp), %r14d
subl %r8d, %r14d
cmpq (%r15), %r12
jne 0xeb5b
cmpq %r13, 0x8(%r15)
je 0xeb5b
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xeb47
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rcx
movups (%r15), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r15
decl %r14d
jne 0xeaf7
testl %r8d, %r8d
jle 0xebf7
movl %r8d, %r15d
shlq $0x4, %r15
xorl %r14d, %r14d
cmpq 0xa0(%rsp,%r14), %r12
jne 0xebee
cmpq %r13, 0xa8(%rsp,%r14)
je 0xebee
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xebd0
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq (%rsp,%r14), %rdx
addq $0xa0, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
shlq $0x4, %rcx
movups (%rdx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r14
cmpq %r14, %r15
jne 0xeb77
cmpq %r12, %rbx
jbe 0xecc7
movq 0x8(%rsp), %r14
cmpl %r14d, %r8d
jne 0xedf1
cmpl 0x34(%rsp), %r10d
jl 0xec75
cmpq $-0x1, %r12
je 0xec75
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xec60
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rcx
movq %r12, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
leal 0x1(%r14), %r9d
movq $-0x1, %rbx
movq 0x10(%rsp), %rdi
cmpl %edi, %r9d
jge 0xedf9
movslq %r9d, %rax
shlq $0x4, %rax
leaq 0xa8(%rsp), %rcx
addq %rcx, %rax
movl %r9d, %ecx
movl %r14d, %r15d
movq -0x8(%rax), %rdx
cmpq %rdx, %rbx
jb 0xecb8
movq (%rax), %r13
movl %ecx, %r15d
movq %rdx, %rbx
incl %ecx
addq $0x10, %rax
cmpl %ecx, %edi
jne 0xeca6
jmp 0xedfc
cmpl 0x64(%rsp), %r10d
jl 0xef9c
cmpq $-0x1, %r12
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r15
je 0xed3c
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xed27
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rcx
movq %r12, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
movl %r8d, %eax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
jmp 0xefb3
cmpl %r14d, %r10d
setl %al
cmpl $0x100, %edx            # imm = 0x100
setge %cl
orb %al, %cl
movq %r15, 0x18(%rsp)
movq %r9, 0x70(%rsp)
jne 0xea87
xorl %eax, %eax
cmpq 0x50(%rsp), %r9
setae %al
movq 0x90(%rsp,%rax,8), %rcx
movq %rcx, %rsi
notq %rsi
shlq $0x15, %rcx
addq %rsi, %rcx
movq 0x58(%rsp), %rdi
andq %rdi, %rcx
movq %rcx, %rsi
shrq $0x18, %rsi
xorq %rcx, %rsi
imulq $0x109, %rsi, %rcx      # imm = 0x109
andq %rdi, %rcx
movq %rcx, %rsi
shrq $0xe, %rsi
xorq %rcx, %rsi
leaq (%rsi,%rsi,4), %rcx
leaq (%rsi,%rcx,4), %rcx
andq %rdi, %rcx
movq %rcx, %rsi
shrq $0x1c, %rsi
xorq %rcx, %rsi
movq %rsi, %rcx
shlq $0x1f, %rcx
addq %rsi, %rcx
andq %rdi, %rcx
shlq $0x8, %rcx
movslq %edx, %rbx
orq %rcx, %rbx
movq 0x68(%rsp), %rcx
leaq (%rcx,%r15,2), %rcx
orq %rax, %rcx
jmp 0xea95
movq %r12, %rbx
jmp 0xef7c
movl %r14d, %r15d
testl %r14d, %r14d
js 0xee2c
movl %r9d, %eax
leaq 0xa8(%rsp), %rcx
xorl %edx, %edx
movq -0x8(%rcx), %rsi
cmpq %rsi, %rbx
jb 0xee20
movq (%rcx), %r13
movl %edx, %r15d
movq %rsi, %rbx
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %rax
jne 0xee0e
cmpl 0x34(%rsp), %r10d
jl 0xef88
cmpq $-0x1, %rbx
je 0xef88
movl %r9d, 0x38(%rsp)
cmpl %edi, %r9d
jge 0xeedd
movslq 0x38(%rsp), %rax
shlq $0x4, %rax
leaq (%rsp,%rax), %r12
addq $0xa0, %r12
movl 0x4c(%rsp), %r14d
subl 0x8(%rsp), %r14d
cmpq (%r12), %rbx
jne 0xeed4
cmpq 0x8(%r12), %r13
je 0xeed4
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xeebf
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rcx
movups (%r12), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r12
decl %r14d
jne 0xee6d
cmpl $0x0, 0x8(%rsp)
js 0xef74
movl 0x38(%rsp), %r14d
shlq $0x4, %r14
xorl %r12d, %r12d
cmpq 0xa0(%rsp,%r12), %rbx
jne 0xef6b
cmpq 0xa8(%rsp,%r12), %r13
je 0xef6b
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xef4d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl 0x4(%rsp), %r10d
movq 0x40(%rsp), %r8
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq (%rsp,%r12), %rdx
addq $0xa0, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
shlq $0x4, %rcx
movups (%rdx), %xmm0
movups %xmm0, (%rax,%rcx)
addq $0x10, %r12
cmpq %r12, %r14
jne 0xeef4
movl %r15d, %eax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %rdi
jmp 0xef95
movl %r15d, %eax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r15
jmp 0xefb8
movl %r8d, %eax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r15
movq 0x38(%rsp), %r13
incl %r8d
cmpl %edi, %r8d
movl $0x0, %eax
cmovel %eax, %r8d
movq %rbx, %r12
movl %r10d, %eax
movq 0x70(%rsp), %r9
incq %r15
cmpq 0x80(%rsp), %r15
jne 0xea05
cmpq $-0x1, %r12
je 0xf03c
movq (%rbp), %rcx
cmpq 0x8(%rbp), %rcx
jne 0xf023
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %edx
cmovneq %rax, %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%rbp), %rsi
shlq $0x4, %rdx
movq 0x20(%rsp), %rdi
callq 0x31ed
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
jmp 0xf027
movq 0x10(%rbp), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x4, %rcx
movq %r12, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
addq $0x10a8, %rsp           # imm = 0x10A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1989e(%rip), %rdi     # 0x288f3
leaq 0x198cb(%rip), %rsi     # 0x28927
leaq 0x198cd(%rip), %rcx     # 0x28930
movl $0x3e, %edx
callq 0x2160
nopl (%rax)