// File: /workspace/github_c_makefile_star2_O2/aokad[P]sequeeze/seqtk.c
// Function: stk_comp
int stk_comp(int argc, char *argv[])
{
	gzFile fp;
	kseq_t *seq;
	int l, c, upper_only = 0;
	reghash_t *h = 0;
	reglist_t dummy;

	while ((c = getopt(argc, argv, "ur:")) >= 0) {
		switch (c) {
			case 'u': upper_only = 1; break;
			case 'r': h = stk_reg_read(optarg); break;
		}
	}
	if (argc == optind && isatty(fileno(stdin))) {
		fprintf(stderr, "Usage:  seqtk comp [-u] [-r in.bed] <in.fa>\n\n");
		fprintf(stderr, "Output format: chr, length, #A, #C, #G, #T, #2, #3, #4, #CpG, #tv, #ts, #CpG-ts\n");
		return 1;
	}
	fp = optind < argc && strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
	if (fp == 0) {
		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
		return 1;
	}
	seq = kseq_init(fp);
	dummy.n= dummy.m = 1; dummy.a = calloc(1, 8);
	while ((l = kseq_read(seq)) >= 0) {
		int i, k;
		reglist_t *p = 0;
		if (h) {
			khint_t k = kh_get(reg, h, seq->name.s);
			if (k != kh_end(h)) p = &kh_val(h, k);
		} else {
			p = &dummy;
			dummy.a[0] = l;
		}
		for (k = 0; p && k < p->n; ++k) {
			int beg = p->a[k]>>32, end = p->a[k]&0xffffffff;
			int la, lb, lc, na, nb, nc, cnt[11];
			if (beg > 0) la = seq->seq.s[beg-1], lb = seq_nt16_table[la], lc = bitcnt_table[lb];
			else la = 'a', lb = -1, lc = 0;
			na = seq->seq.s[beg]; nb = seq_nt16_table[na]; nc = bitcnt_table[nb];
			memset(cnt, 0, 11 * sizeof(int));
			for (i = beg; i < end; ++i) {
				int is_CpG = 0, a, b, c;
				a = na; b = nb; c = nc;
				na = seq->seq.s[i+1]; nb = seq_nt16_table[na]; nc = bitcnt_table[nb];
				if (b == 2 || b == 10) { // C or Y
					if (nb == 4 || nb == 5) is_CpG = 1;
				} else if (b == 4 || b == 5) { // G or R
					if (lb == 2 || lb == 10) is_CpG = 1;
				}
				if (upper_only == 0 || isupper(a)) {
					if (c > 1) ++cnt[c+2];
					if (c == 1) ++cnt[seq_nt16to4_table[b]];
					if (b == 10 || b == 5) ++cnt[9];
					else if (c == 2) {
						++cnt[8];
					}
					if (is_CpG) {
						++cnt[7];
						if (b == 10 || b == 5) ++cnt[10];
					}
				}
				la = a; lb = b; lc = c;
			}
			if (h) printf("%s\t%d\t%d", seq->name.s, beg, end);
			else printf("%s\t%d", seq->name.s, l);
			for (i = 0; i < 11; ++i) printf("\t%d", cnt[i]);
			putchar('\n');
		}
		fflush(stdout);
	}
	free(dummy.a);
	kseq_destroy(seq);
	gzclose(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r14
movl %edi, %ebp
movq $0x0, 0x18(%rsp)
leaq 0x604e(%rip), %r15      # 0x93dd
movq 0xac42(%rip), %rbx      # 0xdfd8
pushq $0x1
popq %r12
movl $0x0, 0x14(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
js 0x33d3
cmpl $0x72, %eax
je 0x33c4
cmpl $0x75, %eax
jne 0x33a2
movl %r12d, 0x14(%rsp)
jmp 0x33a2
movq (%rbx), %rdi
callq 0x23ac
movq %rax, 0x18(%rsp)
jmp 0x33a2
movq 0xabf6(%rip), %rbx      # 0xdfd0
movl (%rbx), %eax
cmpl %ebp, %eax
jne 0x3400
movq 0xabe1(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
movl %eax, %edi
callq 0x2090
testl %eax, %eax
jne 0x3775
movl (%rbx), %eax
cmpl %ebp, %eax
jge 0x3423
cltq
movq (%r14,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x3415
cmpb $0x0, 0x1(%rdi)
je 0x3423
leaq 0x5d8c(%rip), %rsi      # 0x91a8
callq 0x2220
jmp 0x3440
movq 0xab9e(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x5d6f(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
testq %rax, %rax
movq 0x18(%rsp), %r14
je 0x3754
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x3102
movq %rax, %r15
movabsq $0x100000001, %rax      # imm = 0x100000001
leaq 0xa8(%rsp), %rbx
movq %rax, (%rbx)
pushq $0x1
popq %rdi
pushq $0x8
popq %rsi
callq 0x2160
movq %rax, 0x28(%rsp)
movq %rax, 0x8(%rbx)
movq %r15, 0x30(%rsp)
movq %r15, %rdi
callq 0x312e
movl %eax, 0x10(%rsp)
testl %eax, %eax
js 0x3721
testq %r14, %r14
je 0x34d9
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x266f
cmpl (%r14), %eax
leaq 0xacb5(%rip), %rdx      # 0xe170
je 0x370d
movq 0x20(%r14), %rcx
testq %rcx, %rcx
je 0x370d
movl %eax, %eax
shlq $0x4, %rax
addq %rax, %rcx
jmp 0x34f4
movl 0x10(%rsp), %eax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xa8(%rsp), %rcx
leaq 0xac7c(%rip), %rdx      # 0xe170
xorl %esi, %esi
movq %rcx, 0x38(%rsp)
movslq (%rcx), %rax
cmpq %rax, %rsi
jge 0x370d
movq 0x8(%rcx), %rax
movq (%rax,%rsi,8), %rdi
movq %rdi, %r8
shrq $0x20, %r8
movq 0x40(%r15), %r9
pushq $-0x1
popq %r11
testl %r8d, %r8d
jle 0x3532
movl %r8d, %eax
decl %eax
movsbq (%r9,%rax), %rax
movzbl (%rax,%rdx), %r11d
movq %r8, 0x40(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, %r12
sarq $0x20, %r12
movsbq (%r9,%r12), %r14
movzbl (%r14,%rdx), %r8d
leaq 0xae4c(%rip), %rax      # 0xe3a0
movl (%rax,%r8,4), %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movups %xmm0, 0x8c(%rsp)
movq %rdi, 0x48(%rsp)
movslq %edi, %rax
movq %rax, 0x58(%rsp)
cmpq 0x58(%rsp), %r12
jge 0x368b
movl %r13d, %ebx
movl %r11d, %eax
movzbl %r8b, %r11d
movsbq 0x1(%r9,%r12), %r15
movzbl (%r15,%rdx), %r8d
movl %r11d, %ecx
orl $0x8, %ecx
cmpl $0xa, %ecx
jne 0x35b1
movl %r8d, %eax
andb $-0x2, %al
cmpb $0x4, %al
jmp 0x35c2
movl %r11d, %ecx
andl $-0x2, %ecx
xorl $0x4, %ecx
andl $-0x9, %eax
xorl $0x2, %eax
orl %ecx, %eax
setne %bpl
leaq 0xadd3(%rip), %rax      # 0xe3a0
movl (%rax,%r8,4), %r13d
cmpl $0x0, 0x14(%rsp)
je 0x361f
movb %bpl, 0xf(%rsp)
movq %r9, %rbp
movq %r14, 0x60(%rsp)
movq %r8, %r14
movq %rbx, 0x68(%rsp)
movl %r11d, %ebx
callq 0x2290
movl %ebx, %r11d
movq 0x68(%rsp), %rbx
movq %r14, %r8
movq %rbp, %r9
movb 0xf(%rsp), %bpl
leaq 0xab61(%rip), %rdx      # 0xe170
movq (%rax), %rax
movsbq 0x60(%rsp), %rcx
testb $0x1, 0x1(%rax,%rcx,2)
je 0x3672
cmpl $0x2, %ebx
jl 0x3629
leal 0x2(%rbx), %eax
jmp 0x363c
cmpl $0x1, %ebx
jne 0x3640
movl %r11d, %eax
leaq 0xad48(%rip), %rcx      # 0xe380
movzbl (%rax,%rcx), %eax
incl 0x70(%rsp,%rax,4)
cmpl $0xa, %r11d
je 0x364c
cmpl $0x5, %r11d
jne 0x367d
incl 0x94(%rsp)
testb %bpl, %bpl
jne 0x3672
incl 0x8c(%rsp)
cmpl $0x5, %r11d
je 0x366b
cmpl $0xa, %r11d
jne 0x3672
incl 0x98(%rsp)
incq %r12
movl %r15d, %r14d
jmp 0x357d
cmpl $0x2, %ebx
jne 0x3653
incl 0x90(%rsp)
jmp 0x3653
movq 0x30(%rsp), %r15
movq 0x10(%r15), %rsi
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0x36b8
leaq 0x5dc4(%rip), %rdi      # 0x9469
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x20f0
jmp 0x36ca
leaq 0x5db3(%rip), %rdi      # 0x9472
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x20f0
leaq 0x5d9d(%rip), %r12      # 0x946e
xorl %ebx, %ebx
cmpq $0xb, %rbx
je 0x36ec
movl 0x70(%rsp,%rbx,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20f0
incq %rbx
jmp 0x36d3
pushq $0xa
popq %rdi
callq 0x2060
movq 0x50(%rsp), %rsi
incq %rsi
leaq 0xaa6d(%rip), %rdx      # 0xe170
movq 0x38(%rsp), %rcx
jmp 0x34fb
movq 0xa8ac(%rip), %rax      # 0xdfc0
movq (%rax), %rdi
callq 0x21e0
jmp 0x348c
movq 0xb0(%rsp), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x3329
movq 0x20(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa89d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x5c3c(%rip), %rsi      # 0x93a1
leaq 0x5cf4(%rip), %rdx      # 0x9460
xorl %eax, %eax
callq 0x2190
jmp 0x37a9
movq 0xa87c(%rip), %r14      # 0xdff8
movq (%r14), %rcx
leaq 0x5c5b(%rip), %rdi      # 0x93e1
pushq $0x2d
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2260
movq (%r14), %rcx
leaq 0x5c71(%rip), %rdi      # 0x940f
pushq $0x50
popq %rsi
movq %rbx, %rdx
callq 0x2260
pushq $0x1
popq %rax
jmp 0x3742