// File: /workspace/github_c_makefile_star2_O1/aokad[P]sequeeze/seqtk.c
// Function: stk_kfreq
int stk_kfreq(int argc, char *argv[])
{
	gzFile fp;
	kseq_t *ks;
	int kmer, i, l, mask;
	char *nei;

	if (argc < 2) {
		fprintf(stderr, "Usage: seqtk kfreq <kmer> <in.fa>\n");
		return 1;
	}

	// get the k-mer
	l = strlen(argv[1]);
	for (i = kmer = 0; i < l; ++i) {
		int c = seq_nt6_table[(int)argv[1][i]];
		assert(c >= 1 && c <= 4);
		kmer = kmer << 2 | (c - 1);
	}
	mask = (1<<2*l) - 1;

	// get the neighbors
	nei = calloc(1, 1<<2*l);
	for (i = 0; i < l; ++i) {
		int j, x;
		x = kmer & ~(3 << 2*i);
		for (j = 0; j < 4; ++j) 
			nei[x|j<<2*i] = 1;
	}

	fp = argc == 2 || strcmp(argv[2], "-") == 0? gzdopen(fileno(stdin), "r") : gzopen(argv[2], "r");
	if (fp == 0) {
		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
		return 1;
	}
	ks = kseq_init(fp);
	while (kseq_read(ks) >= 0) {
		int k, x[2], cnt[2], cnt_nei[2], which;
		x[0] = x[1] = k = cnt[0] = cnt[1] = cnt_nei[0] = cnt_nei[1] = 0;
		for (i = 0; i < ks->seq.l; ++i) {
			int c = seq_nt6_table[(int)ks->seq.s[i]];
			if (c >= 1 && c <= 4) {
				x[0] = (x[0] << 2 | (c - 1)) & mask;
				x[1] = (x[1] >> 2 | (4 - c) << 2*(l-1));
				if (k < l) ++k;
				if (k == l) {
					if (x[0] == kmer) ++cnt[0];
					else if (x[1] == kmer) ++cnt[1];
					if (nei[x[0]]) ++cnt_nei[0];
					else if (nei[x[1]]) ++cnt_nei[1];
				}
			} else k = 0;
		}
		which = cnt_nei[0] > cnt_nei[1]? 0 : 1;
		printf("%s\t%ld\t%c\t%d\t%d\n", ks->name.s, ks->seq.l, "+-"[which], cnt_nei[which], cnt[which]);
	}
	kseq_destroy(ks);
	gzclose(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x1, %edi
jle 0x8a03
movq %rsi, %r15
movl %edi, %r13d
movq 0x8(%rsi), %r14
movq %r14, %rdi
callq 0x20d0
movq %rax, %rbx
testl %ebx, %ebx
jle 0x8794
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
leaq 0x7b0d(%rip), %rdx      # 0x10270
xorl %esi, %esi
movsbq (%r14,%rcx), %rdi
movzbl (%rdi,%rdx), %r12d
leal -0x1(%r12), %edi
cmpb $0x4, %dil
jae 0x8a16
shll $0x2, %esi
decl %r12d
orl %esi, %r12d
incq %rcx
movl %r12d, %esi
cmpq %rcx, %rax
jne 0x8765
jmp 0x8797
xorl %r12d, %r12d
movq %r15, %r14
leal (%rbx,%rbx), %ecx
movl $0x1, %esi
movl %ecx, %ebp
shll %cl, %esi
movl $0x1, %edi
movq %rsi, 0x10(%rsp)
callq 0x2160
movq %rax, %r15
testl %ebx, %ebx
jle 0x87ea
xorl %eax, %eax
leal (%rax,%rax), %ecx
movl $0x3, %edx
shll %cl, %edx
notl %edx
andl %r12d, %edx
xorl %esi, %esi
movl %esi, %edi
shll %cl, %edi
orl %edx, %edi
movslq %edi, %rdi
movb $0x1, (%r15,%rdi)
incl %esi
cmpl $0x4, %esi
jne 0x87cf
incl %eax
cmpl %ebx, %eax
jne 0x87be
cmpl $0x2, %r13d
je 0x8818
movq 0x10(%r14), %r13
leaq 0x3bb3(%rip), %rsi      # 0xc3ae
movq %r13, %rdi
callq 0x2170
testl %eax, %eax
je 0x8818
leaq 0x298a(%rip), %rsi      # 0xb198
movq %r13, %rdi
callq 0x2220
jmp 0x8835
movq 0x77a9(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x296a(%rip), %rsi      # 0xb198
movl %eax, %edi
callq 0x20b0
movl %ebp, %r13d
testq %rax, %rax
je 0x8a0a
movq %rax, 0x30(%rsp)
movq %rax, %rdi
callq 0x3e57
movq %rax, %r14
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x89e0
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
addl $-0x2, %r13d
movq %r14, 0x28(%rsp)
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x30(%r14), %rdx
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, %edi
movl $0x0, %r8d
testq %rdx, %rdx
je 0x8980
movq 0x28(%rsp), %rax
movq 0x40(%rax), %r9
leaq 0x79b6(%rip), %rax      # 0x10270
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %edi, %edi
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movsbq (%r9,%r10), %rcx
movzbl (%rcx,%rax), %ecx
leal -0x1(%rcx), %esi
cmpb $0x3, %sil
ja 0x8953
shll $0x2, %r11d
leal -0x1(%rcx), %esi
orl %esi, %r11d
movl %ebp, %esi
movl $0x4, %ebp
subl %ecx, %ebp
movl %r13d, %ecx
shll %cl, %ebp
andl 0x10(%rsp), %r11d
sarl $0x2, %esi
orl %esi, %ebp
xorl %ecx, %ecx
cmpl %ebx, %r14d
setl %cl
addl %r14d, %ecx
cmpl %ebx, %ecx
jne 0x8971
movq %rbx, %rax
movl %r13d, %ebx
xorl %esi, %esi
cmpl %r12d, %r11d
setne %r14b
sete %sil
cmpl %r12d, %ebp
sete %r13b
addl %esi, %edi
andb %r14b, %r13b
movzbl %r13b, %esi
addl %esi, %r8d
movslq %r11d, %rsi
cmpb $0x0, (%r15,%rsi)
je 0x8957
incl 0xc(%rsp)
jmp 0x8964
xorl %ecx, %ecx
jmp 0x8971
movslq %ebp, %rsi
cmpb $0x1, (%r15,%rsi)
sbbl $-0x1, 0x8(%rsp)
movl %ebx, %r13d
movq %rax, %rbx
leaq 0x78ff(%rip), %rax      # 0x10270
incq %r10
movl %ecx, %r14d
cmpq %r10, %rdx
jne 0x88da
movl %r8d, 0x24(%rsp)
movl %edi, 0x20(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x8(%rsp), %ecx
movl %ecx, 0x1c(%rsp)
xorl %edi, %edi
cmpl %ecx, %eax
setle %dil
movq 0x28(%rsp), %r14
movq 0x10(%r14), %rsi
leaq 0x39fc(%rip), %rax      # 0xc3ad
movsbl (%rdi,%rax), %ecx
shll $0x2, %edi
movl 0x18(%rsp,%rdi), %r8d
movl 0x20(%rsp,%rdi), %r9d
leaq 0x39d3(%rip), %rdi      # 0xc39c
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdi
callq 0x3ea1
testl %eax, %eax
jns 0x8876
movq %r14, %rdi
callq 0x4229
movq 0x30(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2d42
jmp 0x8a0f
callq 0x2d23
movl $0x1, %eax
jmp 0x89f4
leaq 0x3940(%rip), %rdi      # 0xc35d
leaq 0x394a(%rip), %rsi      # 0xc36e
leaq 0x394b(%rip), %rcx      # 0xc376
movl $0x5f5, %edx            # imm = 0x5F5
callq 0x2100