// File: /workspace/github_c_makefile_star2_O3/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, %r12d
leaq 0x70d9(%rip), %rdx      # 0xb3dd
callq 0x2240
xorl %ebx, %ebx
testl %eax, %eax
js 0x435a
movq 0xbcc2(%rip), %r13      # 0xffd8
leaq 0x70c0(%rip), %r15      # 0xb3dd
xorl %ebp, %ebp
cmpl $0x72, %eax
je 0x4330
cmpl $0x75, %eax
jne 0x433c
movl $0x1, %ebx
jmp 0x433c
movq (%r13), %rdi
callq 0x314c
movq %rax, %rbp
movl %r12d, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
jns 0x431f
testl %ebx, %ebx
sete 0xf(%rsp)
movq %rbp, %r15
jmp 0x4362
movb $0x1, 0xf(%rsp)
xorl %r15d, %r15d
movq 0xbc67(%rip), %rbx      # 0xffd0
movl (%rbx), %eax
cmpl %r12d, %eax
jne 0x4390
movq 0xbc51(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
movl %eax, %edi
callq 0x2090
testl %eax, %eax
jne 0x47f9
movl (%rbx), %eax
cmpl %r12d, %eax
jge 0x43b4
cltq
movq (%r14,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x43a6
cmpb $0x0, 0x1(%rdi)
je 0x43b4
leaq 0x6dfb(%rip), %rsi      # 0xb1a8
callq 0x2220
jmp 0x43d1
movq 0xbc0d(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x6dde(%rip), %rsi      # 0xb1a8
movl %eax, %edi
callq 0x20b0
testq %rax, %rax
je 0x47f2
movq %rax, 0x50(%rsp)
movq %rax, %rdi
callq 0x3edd
movq %rax, %rbx
movl $0x1, 0x40(%rsp)
movl $0x1, %edi
movl $0x8, %esi
callq 0x2160
movq %rax, 0x48(%rsp)
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x3f27
movl %eax, 0x10(%rsp)
testl %eax, %eax
js 0x47c0
leaq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0xbd3c(%rip), %rsi      # 0x10170
movq %r15, 0x20(%rsp)
testq %r15, %r15
je 0x4479
movl (%r15), %edi
testl %edi, %edi
je 0x448f
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r14
movsbl (%r14), %ebp
testl %ebp, %ebp
je 0x4497
movb 0x1(%r14), %al
testb %al, %al
je 0x4499
leaq 0x2(%r14), %rcx
movl %ebp, %edx
shll $0x5, %edx
subl %ebp, %edx
movsbl %al, %ebp
addl %edx, %ebp
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x4462
jmp 0x4499
movl 0x10(%rsp), %eax
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x40(%rsp), %rcx
jmp 0x455a
xorl %r15d, %r15d
jmp 0x4535
xorl %ebp, %ebp
movq %rdi, 0x28(%rsp)
leal -0x1(%rdi), %ebx
movl %ebp, %edi
andl %ebx, %edi
movl %ebp, %eax
shrl $0x3, %eax
shll $0x3, %ebp
xorl %eax, %ebp
orl $0x1, %ebp
andl %ebx, %ebp
movq 0x10(%r15), %r9
movl %edi, %r15d
movl %edi, 0x14(%rsp)
movq %r9, 0x30(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%r9,%rax,4), %r13d
leal (%r15,%r15), %r12d
andl $0x1e, %r12d
movl %r13d, %eax
movl %r12d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4522
testb $0x1, %al
jne 0x4512
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r14, %rsi
callq 0x2170
movq 0x30(%rsp), %r9
movl 0x14(%rsp), %edi
leaq 0xbc62(%rip), %rsi      # 0x10170
testl %eax, %eax
je 0x4522
addl %ebp, %r15d
andl %ebx, %r15d
cmpl %edi, %r15d
jne 0x44c4
jmp 0x478f
movl %r12d, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x28(%rsp), %rdi
cmovnel %edi, %r15d
cmpl %edi, %r15d
je 0x478f
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x478f
movl %r15d, %eax
shlq $0x4, %rax
addq %rax, %rcx
cmpl $0x0, (%rcx)
jle 0x478f
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
movq 0x8(%rcx), %rax
movq (%rax,%rdx,8), %rcx
movq %rcx, %rdi
shrq $0x20, %rdi
movq 0x18(%rsp), %rax
movq 0x40(%rax), %r8
testl %edi, %edi
movq %rdx, 0x70(%rsp)
jle 0x459b
movl %edi, %eax
decl %eax
movsbq (%r8,%rax), %rax
movzbl (%rax,%rsi), %r13d
jmp 0x45a1
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rcx, %rbp
sarq $0x20, %rbp
movsbq (%r8,%rbp), %r14
movzbl (%r14,%rsi), %r9d
leaq 0xbde7(%rip), %rax      # 0x103a0
movl (%rax,%r9,4), %r11d
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movups %xmm0, 0x9c(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdi, 0x68(%rsp)
cmpl %ecx, %edi
jge 0x4705
movslq 0x38(%rsp), %r12
movl %r13d, %eax
movl %r11d, %ebx
movzbl %r9b, %r13d
movsbq 0x1(%r8,%rbp), %rdi
movzbl (%rdi,%rsi), %r9d
movl %r13d, %ecx
orl $0x8, %ecx
cmpl $0xa, %ecx
jne 0x4618
movl %r9d, %eax
andb $-0x2, %al
cmpb $0x4, %al
jmp 0x4629
movl %r13d, %ecx
andl $-0x2, %ecx
xorl $0x4, %ecx
andl $-0x9, %eax
xorl $0x2, %eax
orl %ecx, %eax
setne %r15b
leaq 0xbd6c(%rip), %rax      # 0x103a0
movl (%rax,%r9,4), %r11d
cmpb $0x0, 0xf(%rsp)
jne 0x4690
movq %rbx, 0x30(%rsp)
movq %r8, %rbx
movb %r15b, 0x28(%rsp)
movq %r9, %r15
movq %r14, 0x78(%rsp)
movq %rdi, %r14
movl %r11d, 0x14(%rsp)
callq 0x2290
movl 0x14(%rsp), %r11d
movq %r14, %rdi
movq %r15, %r9
movb 0x28(%rsp), %r15b
movq %rbx, %r8
movq 0x30(%rsp), %rbx
leaq 0xbaf0(%rip), %rsi      # 0x10170
movq (%rax), %rax
movsbq 0x78(%rsp), %rcx
testb $0x1, 0x1(%rax,%rcx,2)
je 0x46e6
cmpl $0x2, %ebx
jl 0x469a
leal 0x2(%rbx), %eax
jmp 0x46ad
cmpl $0x1, %ebx
jne 0x46b4
movl %r13d, %eax
leaq 0xbcd7(%rip), %rcx      # 0x10380
movzbl (%rax,%rcx), %eax
incl 0x80(%rsp,%rax,4)
cmpl $0xa, %r13d
je 0x46c0
cmpl $0x5, %r13d
jne 0x46f7
incl 0xa4(%rsp)
testb %r15b, %r15b
jne 0x46e6
incl 0x9c(%rsp)
cmpl $0xa, %r13d
je 0x46df
cmpl $0x5, %r13d
jne 0x46e6
incl 0xa8(%rsp)
incq %rbp
movl %edi, %r14d
cmpq %rbp, %r12
jne 0x45ef
jmp 0x4705
cmpl $0x2, %ebx
jne 0x46c7
incl 0xa0(%rsp)
jmp 0x46c7
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rsi
cmpq $0x0, 0x20(%rsp)
je 0x4730
leaq 0x6d4c(%rip), %rdi      # 0xb469
movq 0x68(%rsp), %rdx
movq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0x20f0
jmp 0x4742
leaq 0x6d3b(%rip), %rdi      # 0xb472
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x20f0
leaq 0x6d25(%rip), %r14      # 0xb46e
xorl %ebx, %ebx
movl 0x80(%rsp,%rbx,4), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x20f0
incq %rbx
cmpq $0xb, %rbx
jne 0x474b
movl $0xa, %edi
callq 0x2060
movq 0x70(%rsp), %rdx
incq %rdx
movq 0x60(%rsp), %rcx
movslq (%rcx), %rax
cmpq %rax, %rdx
leaq 0xb9e7(%rip), %rsi      # 0x10170
jl 0x456a
movq 0xb82a(%rip), %rax      # 0xffc0
movq (%rax), %rdi
callq 0x21e0
movq 0x18(%rsp), %rdi
callq 0x3f27
leaq 0xb9c1(%rip), %rsi      # 0x10170
movl %eax, 0x10(%rsp)
testl %eax, %eax
movq 0x20(%rsp), %r15
jns 0x4439
movq 0x48(%rsp), %rdi
callq 0x2050
movq 0x18(%rsp), %rdi
callq 0x428d
movq 0x50(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x23ea
jmp 0x47fe
callq 0x23ab
movl $0x1, %eax
jmp 0x47e0