// File: /workspace/github_c_makefile_star2_O1/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 $0x648, %rsp            # imm = 0x648
movq %rsi, %r14
movl %edi, %ebp
leaq 0x2ca1(%rip), %rdx      # 0xb6f3
callq 0x2240
movl $0x14, %ebx
testl %eax, %eax
js 0x8a8f
movq 0x7571(%rip), %r12      # 0xffd8
leaq 0x2c85(%rip), %r15      # 0xb6f3
cmpl $0x71, %eax
jne 0x8a7e
movq (%r12), %rdi
callq 0x2250
movl %eax, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
jns 0x8a6e
movq 0x753a(%rip), %rax      # 0xffd0
movslq (%rax), %rax
cmpl %ebp, %eax
je 0x8e59
movq (%r14,%rax,8), %r14
leaq 0x3902(%rip), %rsi      # 0xc3ae
movq %r14, %rdi
callq 0x2170
testl %eax, %eax
je 0x8ac9
leaq 0x26d9(%rip), %rsi      # 0xb198
movq %r14, %rdi
callq 0x2220
jmp 0x8ae6
movq 0x74f8(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x26b9(%rip), %rsi      # 0xb198
movl %eax, %edi
callq 0x20b0
movq %rax, %r15
testq %rax, %rax
je 0x8e62
movq %r15, %rdi
callq 0x3e57
movq %rax, %r13
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x2588(%rip), %xmm1     # 0xb098
movsd 0x2540(%rip), %xmm0     # 0xb058
callq 0x20c0
movsd %xmm0, 0x40(%rsp,%r14,8)
incq %r14
cmpq $0x5e, %r14
jne 0x8b00
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, 0x58(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl $0x7e, %r12d
movl $0x4, %r14d
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x8cc8
cmpq $0x0, 0x48(%r13)
je 0x8b89
movq %r15, 0x38(%rsp)
movq 0x30(%r13), %r15
movl %ebp, %eax
cmpq %rax, %r15
cmovbel %r15d, %ebp
movq 0x20(%rsp), %rcx
movslq %ecx, %rax
cmpq %rax, %r15
cmovael %r15d, %ecx
movq 0x10(%rsp), %rdx
cmpl %edx, %ecx
movq %rcx, 0x20(%rsp)
jle 0x8c43
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 %ebp, 0x2c(%rsp)
movl %eax, %ebp
shrl $0x10, %ebp
orl %eax, %ebp
incl %ebp
imulq $0x318, %rbp, %rsi      # imm = 0x318
movq 0x8(%rsp), %rdi
movq %rdx, 0x10(%rsp)
callq 0x2200
movq 0x10(%rsp), %rdx
movl %edx, %ecx
imulq $0x318, %rcx, %rdi      # imm = 0x318
movq %rax, 0x8(%rsp)
addq %rax, %rdi
movl %ebp, %eax
subl %edx, %eax
cltq
imulq $0x318, %rax, %rdx      # imm = 0x318
xorl %esi, %esi
callq 0x2120
movq %rbp, 0x10(%rsp)
movl 0x2c(%rsp), %ebp
movq 0x18(%rsp), %rax
leaq 0x7621(%rip), %r9       # 0x10270
incq 0x30(%rsp)
addq %r15, %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x48(%r13)
movq 0x38(%rsp), %r15
je 0x8b89
movq 0x40(%r13), %rax
movq 0x58(%r13), %rcx
movq 0x8(%rsp), %rdx
addq $0x2f0, %rdx            # imm = 0x2F0
xorl %esi, %esi
movzbl (%rcx,%rsi), %edi
movsbq (%rax,%rsi), %r8
movzbl (%r8,%r9), %r8d
cmpb $0x7e, %dil
cmovgel %r12d, %edi
movsbq %dil, %rdi
incq -0x3f8(%rdx,%rdi,8)
leaq -0x1(%r8), %rdi
testq %r8, %r8
cmoveq %r14, %rdi
incq (%rdx,%rdi,8)
incq %rsi
addq $0x318, %rdx            # imm = 0x318
cmpq %rsi, 0x48(%r13)
ja 0x8c82
jmp 0x8b89
movl %ebp, %r14d
movq %r13, %rdi
callq 0x4229
movq %r15, %rdi
callq 0x2040
leaq 0x330(%rsp), %rdi
xorl %r12d, %r12d
movl $0x318, %edx            # imm = 0x318
xorl %esi, %esi
callq 0x2120
movq 0x20(%rsp), %r15
movl %r15d, %ebp
testl %r15d, %r15d
jle 0x8d46
xorl %eax, %eax
movq 0x8(%rsp), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
addq %rsi, 0x330(%rsp,%rdx,8)
incq %rdx
cmpq $0x5e, %rdx
jne 0x8d08
movl $0x5e, %edx
movq (%rcx,%rdx,8), %rsi
addq %rsi, 0x330(%rsp,%rdx,8)
incq %rdx
cmpq $0x63, %rdx
jne 0x8d22
incq %rax
addq $0x318, %rcx            # imm = 0x318
cmpq %rbp, %rax
jne 0x8d06
xorl %eax, %eax
cmpq $0x1, 0x330(%rsp,%rax,8)
sbbl $-0x1, %r12d
incq %rax
cmpq $0x5e, %rax
jne 0x8d48
xorps %xmm0, %xmm0
cvtsi2sdq 0x18(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x30(%rsp), %xmm1
divsd %xmm1, %xmm0
leaq 0x369e(%rip), %rdi      # 0xc41b
movl %r14d, %esi
movl %r15d, %edx
movl %r12d, %ecx
movb $0x1, %al
callq 0x20f0
leaq 0x36cc(%rip), %rdi      # 0xc460
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
jle 0x8daf
leaq 0x36ea(%rip), %rdi      # 0xc490
xorl %eax, %eax
callq 0x20f0
jmp 0x8dda
leaq 0x36d3(%rip), %r12      # 0xc489
xorl %r14d, %r14d
cmpq $0x0, 0x330(%rsp,%r14,8)
jle 0x8dd1
movq %r12, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
incq %r14
cmpq $0x5e, %r14
jne 0x8db9
movl $0xa, %edi
callq 0x2060
leaq 0x330(%rsp), %rdi
leaq 0x40(%rsp), %rcx
xorl %esi, %esi
movq %rdi, %rdx
movl %ebx, %r8d
callq 0x8e6e
testl %r15d, %r15d
jle 0x8e3b
xorl %r15d, %r15d
leaq 0x330(%rsp), %r12
leaq 0x40(%rsp), %r13
movq 0x8(%rsp), %r14
incq %r15
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x8e6e
addq $0x318, %r14            # imm = 0x318
cmpq %r15, %rbp
jne 0x8e18
movq 0x8(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x2d7d
jmp 0x8e67
callq 0x2d5e
movl $0x1, %eax
jmp 0x8e47