// File: /workspace/github_c_makefile_star2_O2/aokad[P]sequeeze/seqtk.c
// Function: stk_fqchk
int stk_fqchk(int argc, char *argv[])
{
	gzFile fp;
	kseq_t *seq;
	int i, c, k, max_len = 0, min_len = 0x7fffffff, max_alloc = 0, offset = 33, n_diffQ = 0, qthres = 20;
	int64_t tot_len = 0, n = 0;
	double perr[94];
	posstat_t all, *pos = 0;

	while ((c = getopt(argc, argv, "q:")) >= 0)
		if (c == 'q') qthres = atoi(optarg);

	if (optind == argc) {
		fprintf(stderr, "Usage: seqtk fqchk [-q %d] <in.fq>\n", qthres);
		fprintf(stderr, "Note: use -q0 to get the distribution of all quality values\n");
		return 1;
	}
	fp = (strcmp(argv[optind], "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(argv[optind], "r");
	if (fp == 0) {
		fprintf(stderr, "[E::%s] failed to open the input file/stream.\n", __func__);
		return 1;
	}
	seq = kseq_init(fp);
	for (k = 0; k <= 93; ++k)
		perr[k] = pow(10., -.1 * k);
	perr[0] = perr[1] = perr[2] = perr[3] = .5;
	while (kseq_read(seq) >= 0) {
		if (seq->qual.l == 0) continue;
		++n;
		tot_len += seq->seq.l;
		min_len = min_len < seq->seq.l? min_len : seq->seq.l;
		max_len = max_len > seq->seq.l? max_len : seq->seq.l;
		if (max_len > max_alloc) {
			int old_max = max_alloc;
			max_alloc = max_len;
			kroundup32(max_alloc);
			pos = realloc(pos, max_alloc * sizeof(posstat_t));
			memset(&pos[old_max], 0, (max_alloc - old_max) * sizeof(posstat_t));
		}
		for (i = 0; i < seq->qual.l; ++i) {
			int q = seq->qual.s[i] - offset;
			int b = seq_nt6_table[(int)seq->seq.s[i]];
			b = b? b - 1 : 4;
			q = q < 93? q : 93;
			++pos[i].q[q];
			++pos[i].b[b];
		}
	}
	kseq_destroy(seq);
	gzclose(fp);

	memset(&all, 0, sizeof(posstat_t));
	for (i = 0; i < max_len; ++i) {
		for (k = 0; k <= 93; ++k)
			all.q[k] += pos[i].q[k];
		for (k = 0; k <= 4; ++k)
			all.b[k] += pos[i].b[k];
	}
	for (k = n_diffQ = 0; k <= 93; ++k)
		if (all.q[k]) ++n_diffQ;
	printf("min_len: %d; max_len: %d; avg_len: %.2f; %d distinct quality values\n", min_len, max_len, (double)tot_len/n, n_diffQ);
	printf("POS\t#bases\t%%A\t%%C\t%%G\t%%T\t%%N\tavgQ\terrQ");
	if (qthres <= 0) {
		for (k = 0; k <= 93; ++k)
			if (all.q[k] > 0) printf("\t%%Q%d", k);
	} else printf("\t%%low\t%%high");
	putchar('\n');
	fqc_aux(&all, 0, all.q, perr, qthres);
	for (i = 0; i < max_len; ++i)
		fqc_aux(&pos[i], i + 1, all.q, perr, qthres);
	free(pos);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x658, %rsp            # imm = 0x658
movq %rsi, %r14
movl %edi, %ebp
pushq $0x14
popq %rbx
leaq 0x2177(%rip), %r15      # 0x9703
movq 0x6a45(%rip), %r12      # 0xdfd8
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
js 0x75b6
cmpl $0x71, %eax
jne 0x7593
movq (%r12), %rdi
callq 0x2250
movl %eax, %ebx
jmp 0x7593
movq 0x6a13(%rip), %rax      # 0xdfd0
movslq (%rax), %rax
cmpl %ebp, %eax
je 0x7961
movq (%r14,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x75db
cmpb $0x0, 0x1(%rdi)
je 0x793f
leaq 0x1bc6(%rip), %rsi      # 0x91a8
callq 0x2220
testq %rax, %rax
je 0x7992
movq %rax, 0x40(%rsp)
movq %rax, %rdi
callq 0x3102
movq %rax, %r13
xorl %r14d, %r14d
cmpq $0x5e, %r14
je 0x7632
cvtsi2sd %r14d, %xmm1
mulsd 0x1a9a(%rip), %xmm1     # 0x90b0
movsd 0x1a4a(%rip), %xmm0     # 0x9068
callq 0x20c0
movsd %xmm0, 0x360(%rsp,%r14,8)
incq %r14
jmp 0x7603
movapd 0x1a06(%rip), %xmm0     # 0x9040
movapd %xmm0, 0x370(%rsp)
movapd %xmm0, 0x360(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x6c0e(%rip), %r14      # 0xe270
pushq $0x7e
popq %r12
pushq $0x4
popq %rbp
movq $0x0, 0x18(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
callq 0x312e
testl %eax, %eax
js 0x77c9
cmpq $0x0, 0x48(%r13)
je 0x768d
movq 0x30(%r13), %rsi
movl %r15d, %eax
cmpq %rax, %rsi
cmovbel %esi, %r15d
movq 0x18(%rsp), %rcx
movslq %ecx, %rax
cmpq %rax, %rsi
cmovael %esi, %ecx
movq 0x10(%rsp), %rdx
cmpl %edx, %ecx
movq %rcx, 0x18(%rsp)
jle 0x775e
leal -0x1(%rcx), %eax
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %r15d, 0x24(%rsp)
movl %eax, %r15d
shrl $0x10, %r15d
orl %eax, %r15d
incl %r15d
movq %rsi, 0x38(%rsp)
imulq $0x318, %r15, %rsi      # imm = 0x318
movq 0x8(%rsp), %rdi
movq %rdx, 0x10(%rsp)
callq 0x2200
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdx
movl %edx, %ecx
imulq $0x318, %rcx, %rdi      # imm = 0x318
addq %rax, %rdi
movl %r15d, %eax
subl %edx, %eax
cltq
imulq $0x318, %rax, %rdx      # imm = 0x318
xorl %esi, %esi
callq 0x2120
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %rsi
movq %r15, 0x10(%rsp)
movl 0x24(%rsp), %r15d
jmp 0x7763
movq 0x8(%rsp), %rcx
incq 0x28(%rsp)
addq %rsi, 0x30(%rsp)
movq %rcx, 0x8(%rsp)
leaq 0x2f0(%rcx), %rax
xorl %ecx, %ecx
cmpq %rcx, 0x48(%r13)
jbe 0x768d
movq 0x40(%r13), %rdx
movq 0x58(%r13), %rsi
movzbl (%rsi,%rcx), %esi
movsbq (%rdx,%rcx), %rdx
movzbl (%rdx,%r14), %edx
cmpb $0x7e, %sil
cmovgel %r12d, %esi
movsbq %sil, %rsi
incq -0x3f8(%rax,%rsi,8)
leaq -0x1(%rdx), %rsi
testq %rdx, %rdx
cmoveq %rbp, %rsi
incq (%rax,%rsi,8)
incq %rcx
addq $0x318, %rax            # imm = 0x318
jmp 0x777b
movl %r15d, %r12d
movq %r13, %rdi
callq 0x3329
movq 0x40(%rsp), %rdi
callq 0x2040
leaq 0x48(%rsp), %rdi
xorl %r15d, %r15d
movl $0x318, %edx            # imm = 0x318
xorl %esi, %esi
callq 0x2120
movq 0x18(%rsp), %rdx
testl %edx, %edx
movl $0x0, %r14d
cmovgl %edx, %r14d
pushq $0x5e
popq %rax
movq 0x8(%rsp), %rbp
movq %rbp, %rcx
cmpq %r14, %r15
je 0x784c
xorl %edi, %edi
cmpq $0x5e, %rdi
je 0x7829
movq (%rcx,%rdi,8), %rsi
addq %rsi, 0x48(%rsp,%rdi,8)
incq %rdi
jmp 0x7815
movq %rax, %rdi
cmpq $0x63, %rdi
je 0x7840
movq (%rcx,%rdi,8), %rsi
addq %rsi, 0x48(%rsp,%rdi,8)
incq %rdi
jmp 0x782c
incq %r15
addq $0x318, %rcx            # imm = 0x318
jmp 0x780e
xorl %eax, %eax
xorl %ecx, %ecx
movl %r12d, %esi
cmpq $0x5e, %rax
je 0x7867
cmpq $0x1, 0x48(%rsp,%rax,8)
sbbl $-0x1, %ecx
incq %rax
jmp 0x7853
cvtsi2sdq 0x30(%rsp), %xmm0
cvtsi2sdq 0x28(%rsp), %xmm1
divsd %xmm1, %xmm0
leaq 0x2bab(%rip), %rdi      # 0xa42b
movb $0x1, %al
callq 0x20f0
leaq 0x2be2(%rip), %rdi      # 0xa470
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
jle 0x7915
leaq 0x2c00(%rip), %rdi      # 0xa4a0
xorl %eax, %eax
callq 0x20f0
pushq $0xa
popq %rdi
callq 0x2060
xorl %r15d, %r15d
leaq 0x48(%rsp), %r12
leaq 0x360(%rsp), %r13
movq %r12, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x79b9
cmpq %r15, %r14
je 0x78f7
incq %r15
movq %rbp, %rdi
movl %r15d, %esi
movq %r12, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x79b9
addq $0x318, %rbp            # imm = 0x318
jmp 0x78d2
movq 0x8(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b7d(%rip), %r15      # 0xa499
xorl %r12d, %r12d
cmpq $0x5e, %r12
je 0x78a7
cmpq $0x0, 0x48(%rsp,%r12,8)
jle 0x793a
movq %r15, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x20f0
incq %r12
jmp 0x791f
movq 0x6682(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x1853(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x75e7
movq 0x6690(%rip), %r14      # 0xdff8
movq (%r14), %rdi
leaq 0x2a4e(%rip), %rsi      # 0xa3c0
movl %ebx, %edx
xorl %eax, %eax
callq 0x2190
movq (%r14), %rcx
leaq 0x2a5f(%rip), %rdi      # 0xa3e4
pushq $0x3c
popq %rsi
pushq $0x1
popq %rdx
callq 0x2260
jmp 0x79b1
movq 0x665f(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x19fe(%rip), %rsi      # 0x93a1
leaq 0x2a77(%rip), %rdx      # 0xa421
xorl %eax, %eax
callq 0x2190
pushq $0x1
popq %rax
jmp 0x7903