// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x976c
movq 0x78a9(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x4cc9(%rip), %rsi      # 0xe422
movb $0x0, %al
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x9afb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x20d0
movl %eax, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x97fc
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x7ab4(%rip), %rax      # 0x11260
movzbl (%rax,%rcx), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
jl 0x97c1
cmpl $0x4, -0x3c(%rbp)
jg 0x97c1
jmp 0x97e0
leaq 0x4c7d(%rip), %rdi      # 0xe445
leaq 0x4c87(%rip), %rsi      # 0xe456
movl $0x5f5, %edx            # imm = 0x5F5
leaq 0x4c83(%rip), %rcx      # 0xe45e
callq 0x2100
movl -0x24(%rbp), %eax
shll $0x2, %eax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x978a
movl -0x2c(%rbp), %ecx
shll %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %ecx
shll %ecx
movl $0x1, %eax
shll %cl, %eax
movslq %eax, %rsi
movl $0x1, %edi
callq 0x2160
movq %rax, -0x38(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x9898
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
shll %ecx
movl $0x3, %edx
shll %cl, %edx
movl %edx, %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0x4, -0x40(%rbp)
jge 0x988b
movq -0x38(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %ecx, -0x6c(%rbp)
movl -0x40(%rbp), %edx
movl -0x28(%rbp), %ecx
shll %ecx
shll %cl, %edx
movl -0x6c(%rbp), %ecx
orl %edx, %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x985a
jmp 0x988d
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x9832
cmpl $0x2, -0x8(%rbp)
je 0x98b7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x4be9(%rip), %rsi      # 0xe496
callq 0x2170
cmpl $0x0, %eax
jne 0x98da
movq 0x770a(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x21b0
movl %eax, %edi
leaq 0x39b1(%rip), %rsi      # 0xd280
callq 0x20b0
movq %rax, -0x78(%rbp)
jmp 0x98f2
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x3997(%rip), %rsi      # 0xd280
callq 0x2220
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x992c
movq 0x76f0(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x3b67(%rip), %rsi      # 0xd479
leaq 0x4b61(%rip), %rdx      # 0xe47a
movb $0x0, %al
callq 0x2190
movl $0x1, -0x4(%rbp)
jmp 0x9afb
movq -0x18(%rbp), %rdi
callq 0x38f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3930
cmpl $0x0, %eax
jl 0x9ae2
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x28(%rbp)
movslq -0x28(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x9a89
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x78b1(%rip), %rax      # 0x11260
movzbl (%rax,%rcx), %eax
movl %eax, -0x68(%rbp)
cmpl $0x1, -0x68(%rbp)
jl 0x9a72
cmpl $0x4, -0x68(%rbp)
jg 0x9a72
movl -0x50(%rbp), %eax
shll $0x2, %eax
movl -0x68(%rbp), %ecx
subl $0x1, %ecx
orl %ecx, %eax
andl -0x30(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
sarl $0x2, %eax
movl $0x4, %edx
subl -0x68(%rbp), %edx
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
shll %ecx
shll %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x9a0e
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x9a70
movl -0x50(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x9a29
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x9a3c
movl -0x4c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x9a3a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x9a3c
movq -0x38(%rbp), %rax
movslq -0x50(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x9a55
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x9a6e
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x9a6c
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x9a6e
jmp 0x9a70
jmp 0x9a79
movl $0x0, -0x48(%rbp)
jmp 0x9a7b
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x9983
movl -0x60(%rbp), %edx
movl -0x5c(%rbp), %esi
movl $0x1, %eax
xorl %ecx, %ecx
cmpl %esi, %edx
cmovgl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdx
movslq -0x64(%rbp), %rcx
leaq 0x49dc(%rip), %rax      # 0xe495
movsbl (%rax,%rcx), %ecx
movslq -0x64(%rbp), %rax
movl -0x60(%rbp,%rax,4), %r8d
movslq -0x64(%rbp), %rax
movl -0x58(%rbp,%rax,4), %r9d
leaq 0x49ae(%rip), %rdi      # 0xe484
movb $0x0, %al
callq 0x20f0
jmp 0x9939
movq -0x20(%rbp), %rdi
callq 0x3d40
movq -0x18(%rbp), %rdi
callq 0x2030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)