// File: /workspace/github_c_makefile_star2_O2/aokad[P]sequeeze/seqtk.c
// Function: stk_trimfq
int stk_trimfq(int argc, char *argv[])
{ // FIXME: when a record with zero length will always be treated as a fasta record
	gzFile fp;
	kseq_t *seq;
	double param = 0.05, q_int2real[128];
	int i, c, min_len = 30, left = 0, right = 0, fixed_len = -1;
	while ((c = getopt(argc, argv, "l:q:b:e:L:")) >= 0) {
		switch (c) {
			case 'q': param = atof(optarg); break;
			case 'l': min_len = atoi(optarg); break;
			case 'b': left = atoi(optarg); break;
			case 'e': right = atoi(optarg); break;
			case 'L': fixed_len = atoi(optarg); break;
		}
	}
	if (optind == argc) {
		fprintf(stderr, "\n");
		fprintf(stderr, "Usage:   seqtk trimfq [options] <in.fq>\n\n");
		fprintf(stderr, "Options: -q FLOAT    error rate threshold (disabled by -b/-e) [%.2f]\n", param);
		fprintf(stderr, "         -l INT      maximally trim down to INT bp (disabled by -b/-e) [%d]\n", min_len);
		fprintf(stderr, "         -b INT      trim INT bp from left (non-zero to disable -q/-l) [0]\n");
		fprintf(stderr, "         -e INT      trim INT bp from right (non-zero to disable -q/-l) [0]\n");
		fprintf(stderr, "         -L INT      retain at most INT bp from the 5'-end (non-zero to disable -q/-l) [0]\n");
		fprintf(stderr, "         -Q          force FASTQ output\n");
		fprintf(stderr, "\n");
		return 1;
	}
	fp = 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);
	for (i = 0; i < 128; ++i)
		q_int2real[i] = pow(10., -(i - 33) / 10.);
	while (kseq_read(seq) >= 0) {
		int beg, tmp, end;
		double s, max;
		if (left || right || fixed_len > 0) {
			beg = left; end = seq->seq.l - right;
			if (beg >= end) beg = end = 0;
			if (fixed_len > 0 && end - beg > fixed_len) end = beg + fixed_len;
		} else if (seq->qual.l > min_len) {
			for (i = 0, beg = tmp = 0, end = seq->qual.l, s = max = 0.; i < seq->qual.l; ++i) {
				int q = seq->qual.s[i];
				if (q < 36) q = 36;
				if (q > 127) q = 127;
				s += param - q_int2real[q];
				if (s > max) max = s, beg = tmp, end = i + 1;
				if (s < 0) s = 0, tmp = i + 1;
			}

			/* max never set; all low qual, just give first min_len bp */
			if (max == 0.) beg = 0, end = min_len;

			if (end - beg < min_len) { // window-based 
				int is, imax;
				for (i = 0, is = 0; i < min_len; ++i)
					is += seq->qual.s[i] - 33;
				for (imax = is, beg = 0; i < seq->qual.l; ++i) {
					is += (int)seq->qual.s[i] - seq->qual.s[i - min_len];
					if (imax < is) imax = is, beg = i - min_len + 1;
				}
				end = beg + min_len;
			}
		} else beg = 0, end = seq->seq.l;
		putchar(seq->is_fastq? '@' : '>'); fputs(seq->name.s, stdout); 
		if (seq->comment.l) {
			putchar(' '); puts(seq->comment.s);
		} else putchar('\n');
		fwrite(seq->seq.s + beg, 1, end - beg, stdout); putchar('\n');
		if (seq->is_fastq) {
			puts("+");
			fwrite(seq->qual.s + beg, 1, end - beg, stdout); putchar('\n');
		}
	}
	kseq_destroy(seq);
	gzclose(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %rsi, %r15
movl %edi, %r12d
pushq $-0x1
popq %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x14(%rsp)
pushq $0x1e
popq %rax
movq %rax, 0x18(%rsp)
movsd 0x6401(%rip), %xmm0     # 0x9060
movsd %xmm0, 0x20(%rsp)
leaq 0x654f(%rip), %r13      # 0x91bb
movq 0xb365(%rip), %rbx      # 0xdfd8
movl $0x0, 0x10(%rsp)
movl %r12d, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2240
testl %eax, %eax
js 0x2cf0
cmpl $0x4c, %eax
je 0x2cc5
cmpl $0x62, %eax
je 0x2cd4
cmpl $0x65, %eax
je 0x2ce2
cmpl $0x6c, %eax
je 0x2cb6
cmpl $0x71, %eax
jne 0x2c7b
movq (%rbx), %rdi
callq 0x20a0
movsd %xmm0, 0x20(%rsp)
jmp 0x2c7b
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x18(%rsp)
jmp 0x2c7b
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x28(%rsp)
jmp 0x2c7b
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0x14(%rsp)
jmp 0x2c7b
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0x10(%rsp)
jmp 0x2c7b
movq 0xb2d9(%rip), %rax      # 0xdfd0
movslq (%rax), %rax
cmpl %r12d, %eax
je 0x3021
movq (%r15,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x2d16
cmpb $0x0, 0x1(%rdi)
je 0x2fff
leaq 0x648b(%rip), %rsi      # 0x91a8
callq 0x2220
testq %rax, %rax
je 0x30db
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x3102
movq %rax, %r13
pushq $0x21
popq %rbx
xorl %r14d, %r14d
cmpq $0x80, %r14
je 0x2d6d
cvtsi2sd %ebx, %xmm1
movsd 0x6312(%rip), %xmm0     # 0x9068
divsd %xmm0, %xmm1
callq 0x20c0
movsd %xmm0, 0x60(%rsp,%r14,8)
incq %r14
decl %ebx
jmp 0x2d41
movl 0x14(%rsp), %eax
orl 0x10(%rsp), %eax
setne %al
cmpl $0x0, 0x28(%rsp)
setg %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
movq 0x18(%rsp), %rax
movslq %eax, %r15
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x40(%rsp)
pushq $0x1
popq %rax
subl %r15d, %eax
movq %rax, 0x50(%rsp)
movq %r15, 0x58(%rsp)
negq %r15
movq %r15, 0x48(%rsp)
pushq $0xa
popq %r15
pushq $0x1
popq %r14
movq %r13, %rdi
callq 0x312e
testl %eax, %eax
js 0x2fd9
cmpb $0x0, 0xf(%rsp)
je 0x2e0e
movl 0x30(%r13), %eax
subl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %eax, %ecx
movl $0x0, %ebx
cmovll %ecx, %ebx
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %ecx
subl %ebx, %ecx
movq 0x28(%rsp), %rdx
leal (%rbx,%rdx), %ebp
cmpl %edx, %ecx
cmovlel %eax, %ebp
testl %edx, %edx
cmovlel %eax, %ebp
jmp 0x2f22
movq 0x48(%r13), %rax
cmpq 0x58(%rsp), %rax
jbe 0x2e8d
movq 0x58(%r13), %rsi
xorpd %xmm0, %xmm0
xorl %edi, %edi
xorl %edx, %edx
xorl %r8d, %r8d
movl %eax, %ecx
xorpd %xmm1, %xmm1
movsd 0x20(%rsp), %xmm3
xorpd %xmm4, %xmm4
pushq $0x24
popq %r10
cmpq %rdi, %rax
je 0x2e98
movzbl (%rsi,%rdi), %r9d
cmpb $0x25, %r9b
cmovll %r10d, %r9d
movzbl %r9b, %r9d
movapd %xmm3, %xmm2
subsd 0x60(%rsp,%r9,8), %xmm2
addsd %xmm2, %xmm0
incq %rdi
ucomisd %xmm1, %xmm0
cmoval %r8d, %edx
cmoval %edi, %ecx
ucomisd %xmm0, %xmm4
cmoval %edi, %r8d
xorpd %xmm2, %xmm2
maxsd %xmm0, %xmm2
maxsd %xmm1, %xmm0
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
jmp 0x2e3c
movl 0x30(%r13), %ebp
xorl %ebx, %ebx
jmp 0x2f22
ucomisd %xmm4, %xmm1
movl $0x0, %ebx
cmovnel %edx, %ebx
cmovpl %edx, %ebx
movq 0x18(%rsp), %rdx
movl %edx, %ebp
cmovnel %ecx, %ebp
cmovpl %ecx, %ebp
movl %ebp, %ecx
subl %ebx, %ecx
cmpl %edx, %ecx
jge 0x2f22
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x50(%rsp), %r9
movq 0x48(%rsp), %r10
movq 0x40(%rsp), %rdi
cmpq %rdx, %rdi
je 0x2ee6
movq 0x58(%r13), %rsi
movsbl (%rsi,%rdx), %esi
addl %esi, %ecx
addl $-0x21, %ecx
incq %rdx
jmp 0x2ecf
xorl %ebx, %ebx
movq %rdi, %rdx
movl %ecx, %esi
cmpq %rdx, %rax
jbe 0x2f1a
movq 0x58(%r13), %rdi
movsbl (%rdi,%rdx), %r8d
addq %rdx, %rdi
movsbl (%r10,%rdi), %edi
subl %edi, %r8d
addl %r8d, %ecx
leal (%r9,%rdx), %edi
cmpl %ecx, %esi
cmovll %edi, %ebx
cmovlel %ecx, %esi
incq %rdx
jmp 0x2eed
movq 0x18(%rsp), %rax
leal (%rbx,%rax), %ebp
movl 0x64(%r13), %ecx
xorl %eax, %eax
movl %ecx, 0x34(%rsp)
testl %ecx, %ecx
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r13), %rdi
movq 0xb078(%rip), %rax      # 0xdfc0
movq (%rax), %rsi
callq 0x2110
cmpq $0x0, 0x18(%r13)
je 0x2f6a
pushq $0x20
popq %rdi
callq 0x2060
movq 0x28(%r13), %rdi
callq 0x2080
jmp 0x2f72
movl %r15d, %edi
callq 0x2060
movslq %ebx, %rbx
movq 0x40(%r13), %rdi
addq %rbx, %rdi
subl %ebx, %ebp
movslq %ebp, %rbp
movq 0xb038(%rip), %r12      # 0xdfc0
movq (%r12), %rcx
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2260
movl %r15d, %edi
callq 0x2060
cmpl $0x0, 0x34(%rsp)
je 0x2dbe
leaq 0x642a(%rip), %rdi      # 0x93db
callq 0x2080
addq 0x58(%r13), %rbx
movq (%r12), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2260
movl %r15d, %edi
callq 0x2060
jmp 0x2dbe
movq %r13, %rdi
callq 0x3329
movq 0x38(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xafc2(%rip), %rax      # 0xdfc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x6193(%rip), %rsi      # 0x91a8
movl %eax, %edi
callq 0x20b0
jmp 0x2d22
movq 0xafd0(%rip), %rbx      # 0xdff8
movq (%rbx), %rsi
pushq $0xa
popq %rbp
movl %ebp, %edi
callq 0x2140
movq (%rbx), %rcx
leaq 0x6187(%rip), %rdi      # 0x91c6
pushq $0x29
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x2260
movq (%rbx), %rdi
leaq 0x6198(%rip), %rsi      # 0x91f0
movsd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x2190
movq (%rbx), %rdi
leaq 0x61c7(%rip), %rsi      # 0x9236
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x2190
movq (%rbx), %rcx
leaq 0x61fe(%rip), %rdi      # 0x9283
pushq $0x4b
popq %rsi
movq %r14, %rdx
callq 0x2260
movq (%rbx), %rcx
leaq 0x6235(%rip), %rdi      # 0x92cf
pushq $0x4c
popq %rsi
movq %r14, %rdx
callq 0x2260
movq (%rbx), %rcx
leaq 0x626d(%rip), %rdi      # 0x931c
pushq $0x5b
popq %rsi
movq %r14, %rdx
callq 0x2260
movq (%rbx), %rcx
leaq 0x62b4(%rip), %rdi      # 0x9378
pushq $0x28
popq %rsi
movq %r14, %rdx
callq 0x2260
movq (%rbx), %rsi
movl %ebp, %edi
callq 0x2140
jmp 0x30fa
movq 0xaf16(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x62b5(%rip), %rsi      # 0x93a1
leaq 0x62dd(%rip), %rdx      # 0x93d0
xorl %eax, %eax
callq 0x2190
pushq $0x1
popq %rax
jmp 0x2fed