// File: /workspace/github_c_makefile_star2_O2/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 0x750b
movq %rsi, %r15
movl %edi, %ebp
movq 0x8(%rsi), %r14
movq %r14, %rdi
callq 0x20d0
movq %rax, %rbx
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %r13d
cmovgl %ebx, %r13d
xorl %r12d, %r12d
cmpq %rax, %r13
je 0x72fc
movsbq (%r14,%rax), %rcx
leaq 0x6f95(%rip), %rdx      # 0xe270
movzbl (%rcx,%rdx), %ecx
leal -0x1(%rcx), %edx
cmpb $0x4, %dl
jae 0x754d
shll $0x2, %r12d
decl %ecx
orl %r12d, %ecx
incq %rax
movl %ecx, %r12d
jmp 0x72ca
movl %ebp, %r14d
movq %r15, 0x10(%rsp)
leal (%rbx,%rbx), %ecx
pushq $0x1
popq %rbp
movl %ebp, %esi
movl %ecx, 0xc(%rsp)
shll %cl, %esi
pushq $0x1
popq %rdi
movq %rsi, 0x18(%rsp)
callq 0x2160
movq %rax, %r15
xorl %eax, %eax
pushq $0x3
popq %rdx
cmpl %r13d, %eax
je 0x7358
leal (%rax,%rax), %ecx
movl %edx, %esi
shll %cl, %esi
notl %esi
andl %r12d, %esi
xorl %edi, %edi
cmpl $0x4, %edi
je 0x7354
movl %edi, %r8d
shll %cl, %r8d
orl %esi, %r8d
movslq %r8d, %r8
movb $0x1, (%r15,%r8)
incl %edi
jmp 0x733a
incl %eax
jmp 0x7327
cmpl $0x2, %r14d
jne 0x737d
movq 0x6c63(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x1e34(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x739d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
cmpb $0x2d, (%rdi)
jne 0x7391
cmpb $0x0, 0x1(%rdi)
je 0x735e
leaq 0x1e10(%rip), %rsi      # 0x91a8
callq 0x2220
movq %rax, %rcx
testq %rax, %rax
je 0x752c
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
movq %rcx, %rdi
callq 0x3102
movq %rax, %r13
addl $-0x2, 0xc(%rsp)
movq %r13, %rdi
callq 0x312e
testl %eax, %eax
js 0x74e6
movq 0x30(%r13), %rdx
xorl %edi, %edi
movl $0x0, 0x10(%rsp)
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %ebp, %ebp
xorl %r11d, %r11d
xorl %r10d, %r10d
cmpq %rdi, %rdx
je 0x749a
movq 0x40(%r13), %rcx
movsbq (%rcx,%rdi), %rcx
leaq 0x6e5f(%rip), %rax      # 0xe270
movzbl (%rcx,%rax), %r14d
leal -0x1(%r14), %eax
xorl %ecx, %ecx
cmpb $0x3, %al
ja 0x7490
shll $0x2, %r10d
leal -0x1(%r14), %eax
orl %eax, %r10d
movl %r11d, %eax
pushq $0x4
popq %r11
subl %r14d, %r11d
movl 0xc(%rsp), %ecx
shll %cl, %r11d
andl 0x18(%rsp), %r10d
sarl $0x2, %eax
orl %eax, %r11d
xorl %ecx, %ecx
cmpl %ebx, %ebp
setl %cl
addl %ebp, %ecx
cmpl %ebx, %ecx
jne 0x7490
xorl %eax, %eax
cmpl %r12d, %r10d
setne %cl
sete %al
cmpl %r12d, %r11d
sete %bpl
addl %eax, %r8d
andb %cl, %bpl
movzbl %bpl, %eax
addl %eax, %r9d
movslq %r10d, %rax
cmpb $0x0, (%r15,%rax)
je 0x7481
incl %esi
jmp 0x748e
movslq %r11d, %rax
cmpb $0x1, (%r15,%rax)
sbbl $-0x1, 0x10(%rsp)
movl %ebx, %ecx
incq %rdi
movl %ecx, %ebp
jmp 0x73f8
movl %r9d, 0x2c(%rsp)
movl %r8d, 0x28(%rsp)
movl %esi, 0x20(%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, 0x24(%rsp)
xorl %eax, %eax
cmpl %ecx, %esi
setle %al
movq 0x10(%r13), %rsi
leaq 0x2efb(%rip), %rcx      # 0xa3bd
movsbl (%rax,%rcx), %ecx
shll $0x2, %eax
movl 0x20(%rsp,%rax), %r8d
movl 0x28(%rsp,%rax), %r9d
leaq 0x2ed2(%rip), %rdi      # 0xa3ac
xorl %eax, %eax
callq 0x20f0
jmp 0x73ca
movq %r13, %rdi
callq 0x3329
movq 0x30(%rsp), %rdi
callq 0x2040
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6ae6(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x2e2e(%rip), %rdi      # 0xa34a
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x2260
pushq $0x1
popq %rbp
jmp 0x74fa
movq 0x6ac5(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1e64(%rip), %rsi      # 0x93a1
leaq 0x2e5e(%rip), %rdx      # 0xa3a2
xorl %eax, %eax
callq 0x2190
jmp 0x74fa
leaq 0x2e19(%rip), %rdi      # 0xa36d
leaq 0x2e23(%rip), %rsi      # 0xa37e
leaq 0x2e24(%rip), %rcx      # 0xa386
movl $0x5f5, %edx            # imm = 0x5F5
callq 0x2100