// File: /workspace/github_c_makefile_star2_O3/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 0x8c23
movq %rsi, %rbp
movl %edi, %r13d
movq 0x8(%rsi), %r14
movq %r14, %rdi
callq 0x20d0
movq %rax, %rbx
testl %ebx, %ebx
jle 0x89ed
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
leaq 0x78b4(%rip), %rdx      # 0x10270
xorl %esi, %esi
movsbq (%r14,%rcx), %rdi
movzbl (%rdi,%rdx), %r12d
leal -0x1(%r12), %edi
cmpb $0x4, %dil
jae 0x8c36
shll $0x2, %esi
decl %r12d
orl %esi, %r12d
incq %rcx
movl %r12d, %esi
cmpq %rcx, %rax
jne 0x89be
jmp 0x89f0
xorl %r12d, %r12d
leal (%rbx,%rbx), %ecx
movl $0x1, %esi
movl %ecx, 0xc(%rsp)
shll %cl, %esi
movl $0x1, %edi
movq %rsi, 0x10(%rsp)
callq 0x2160
movq %rax, %r15
testl %ebx, %ebx
jle 0x8a42
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 0x8a27
incl %eax
cmpl %ebx, %eax
jne 0x8a16
cmpl $0x2, %r13d
jne 0x8a67
movq 0x7579(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x274a(%rip), %rsi      # 0xb1a8
movl %eax, %edi
callq 0x20b0
jmp 0x8a82
movq 0x10(%rbp), %rdi
cmpb $0x2d, (%rdi)
jne 0x8a76
cmpb $0x0, 0x1(%rdi)
je 0x8a48
leaq 0x272b(%rip), %rsi      # 0xb1a8
callq 0x2220
testq %rax, %rax
je 0x8c2a
movq %rax, 0x30(%rsp)
movq %rax, %rdi
callq 0x3edd
movq %rax, %r14
movq %rax, %rdi
callq 0x3f27
testl %eax, %eax
js 0x8c00
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
addl $-0x2, 0xc(%rsp)
leaq 0x77ad(%rip), %r13      # 0x10270
movq %r14, 0x18(%rsp)
movq 0x30(%r14), %rdx
movl $0x0, 0x8(%rsp)
movl $0x0, %eax
movl $0x0, %edi
movl $0x0, %r8d
testq %rdx, %rdx
je 0x8ba4
movq 0x18(%rsp), %rax
movq 0x40(%rax), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %edi, %edi
xorl %eax, %eax
movl $0x0, 0x8(%rsp)
movsbq (%r9,%r10), %rcx
movzbl (%rcx,%r13), %ecx
leal -0x1(%rcx), %esi
cmpb $0x3, %sil
ja 0x8b82
shll $0x2, %r11d
leal -0x1(%rcx), %esi
orl %esi, %r11d
movl %ebp, %esi
movl $0x4, %ebp
subl %ecx, %ebp
movl 0xc(%rsp), %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 0x8b95
xorl %ecx, %ecx
cmpl %r12d, %r11d
setne %sil
sete %cl
cmpl %r12d, %ebp
sete %r14b
addl %ecx, %edi
andb %sil, %r14b
movzbl %r14b, %ecx
addl %ecx, %r8d
movslq %r11d, %rcx
cmpb $0x0, (%r15,%rcx)
je 0x8b86
incl %eax
jmp 0x8b93
xorl %ecx, %ecx
jmp 0x8b95
movslq %ebp, %rcx
cmpb $0x1, (%r15,%rcx)
sbbl $-0x1, 0x8(%rsp)
movl %ebx, %ecx
incq %r10
movl %ecx, %r14d
cmpq %r10, %rdx
jne 0x8b10
movl %r8d, 0x2c(%rsp)
movl %edi, 0x28(%rsp)
movl %eax, 0x20(%rsp)
movl 0x8(%rsp), %ecx
movl %ecx, 0x24(%rsp)
xorl %edi, %edi
cmpl %ecx, %eax
setle %dil
movq 0x18(%rsp), %r14
movq 0x10(%r14), %rsi
leaq 0x37ec(%rip), %rax      # 0xc3bd
movsbl (%rdi,%rax), %ecx
shll $0x2, %edi
movl 0x20(%rsp,%rdi), %r8d
movl 0x28(%rsp,%rdi), %r9d
leaq 0x37c3(%rip), %rdi      # 0xc3ac
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdi
callq 0x3f27
testl %eax, %eax
jns 0x8ac8
movq %r14, %rdi
callq 0x428d
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 0x8c2f
callq 0x2d23
movl $0x1, %eax
jmp 0x8c14
leaq 0x3730(%rip), %rdi      # 0xc36d
leaq 0x373a(%rip), %rsi      # 0xc37e
leaq 0x373b(%rip), %rcx      # 0xc386
movl $0x5f5, %edx            # imm = 0x5F5
callq 0x2100