// File: /workspace/github_c_makefile_star2_O1/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 $0x458, %rsp            # imm = 0x458
movq %rsi, %r12
movl %edi, %r13d
leaq 0x77d9(%rip), %rdx      # 0xb1ab
callq 0x2240
testl %eax, %eax
js 0x3aa3
movsd 0x7669(%rip), %xmm0     # 0xb050
movl $0x1e, %ecx
movq %rcx, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x18(%rsp)
movq 0xc5ce(%rip), %rbx      # 0xffd8
leaq 0x779a(%rip), %rbp      # 0xb1ab
movl $0x0, 0x8(%rsp)
cmpl $0x64, %eax
movsd %xmm0, 0x20(%rsp)
jle 0x3a43
cmpl $0x65, %eax
je 0x3a5b
cmpl $0x6c, %eax
je 0x3a69
cmpl $0x71, %eax
jne 0x3a85
movq (%rbx), %rdi
callq 0x20a0
movsd %xmm0, 0x20(%rsp)
jmp 0x3a85
cmpl $0x4c, %eax
je 0x3a78
cmpl $0x62, %eax
jne 0x3a85
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0x8(%rsp)
jmp 0x3a85
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0xc(%rsp)
jmp 0x3a85
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x10(%rsp)
jmp 0x3a85
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x18(%rsp)
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2240
testl %eax, %eax
movsd 0x20(%rsp), %xmm0
jns 0x3a19
jmp 0x3acf
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1e, %eax
movq %rax, 0x10(%rsp)
movsd 0x7589(%rip), %xmm0     # 0xb050
movl $0x0, 0xc(%rsp)
movq 0xc4fa(%rip), %rax      # 0xffd0
movslq (%rax), %rax
cmpl %r13d, %eax
je 0x3e3f
movsd %xmm0, 0x20(%rsp)
movq (%r12,%rax,8), %r12
leaq 0x88bb(%rip), %rsi      # 0xc3ae
movq %r12, %rdi
callq 0x2170
testl %eax, %eax
je 0x3b10
leaq 0x7692(%rip), %rsi      # 0xb198
movq %r12, %rdi
callq 0x2220
jmp 0x3b2d
movq 0xc4b1(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x7672(%rip), %rsi      # 0xb198
movl %eax, %edi
callq 0x20b0
testq %rax, %rax
je 0x3e4b
movq %rax, 0x40(%rsp)
movq %rax, %rdi
callq 0x3e57
movq %rax, %r13
movl $0x21, %ebx
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movsd 0x74fb(%rip), %xmm0     # 0xb058
divsd %xmm0, %xmm1
callq 0x20c0
movsd %xmm0, 0x50(%rsp,%r14,8)
incq %r14
decl %ebx
cmpq $0x80, %r14
jne 0x3b4e
movq %r13, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x3e19
movl 0x8(%rsp), %eax
orl 0xc(%rsp), %eax
setne %al
cmpl $0x0, 0x18(%rsp)
setg %cl
orb %al, %cl
movb %cl, 0x7(%rsp)
movq 0x10(%rsp), %rax
movslq %eax, %r15
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %eax, %edx
movl %r15d, %r11d
movl %edx, %eax
subl %r15d, %eax
incl %eax
movl %eax, 0x2c(%rsp)
movq %r15, 0x48(%rsp)
negq %r15
movq %rdx, 0x38(%rsp)
negq %rdx
movq %rdx, 0x30(%rsp)
movl $0x24, %ebx
xorpd %xmm4, %xmm4
cmpb $0x0, 0x7(%rsp)
je 0x3c29
movl 0x30(%r13), %eax
subl 0xc(%rsp), %eax
movl 0x8(%rsp), %ecx
cmpl %eax, %ecx
movl $0x0, %r12d
cmovll %ecx, %r12d
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %ecx
subl %r12d, %ecx
movq 0x18(%rsp), %rdx
leal (%r12,%rdx), %r14d
cmpl %edx, %ecx
cmovlel %eax, %r14d
testl %edx, %edx
cmovlel %eax, %r14d
jmp 0x3d46
movq 0x48(%r13), %rax
cmpq 0x48(%rsp), %rax
jbe 0x3cf2
movq 0x58(%r13), %rdx
xorl %esi, %esi
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movl %eax, %ecx
xorl %r8d, %r8d
xorl %edi, %edi
movsd 0x20(%rsp), %xmm3
movapd %xmm1, %xmm2
movzbl (%rdx,%rsi), %r9d
cmpb $0x25, %r9b
cmovll %ebx, %r9d
movzbl %r9b, %r9d
movapd %xmm3, %xmm1
subsd 0x50(%rsp,%r9,8), %xmm1
addsd %xmm0, %xmm1
incq %rsi
ucomisd %xmm2, %xmm1
cmoval %r8d, %edi
cmoval %esi, %ecx
ucomisd %xmm1, %xmm4
xorpd %xmm0, %xmm0
maxsd %xmm1, %xmm0
maxsd %xmm2, %xmm1
cmoval %esi, %r8d
cmpq %rsi, %rax
jne 0x3c53
ucomisd %xmm4, %xmm1
movl $0x0, %r12d
cmovnel %edi, %r12d
cmovpl %edi, %r12d
movq 0x10(%rsp), %rdx
movl %edx, %r14d
cmovnel %ecx, %r14d
cmovpl %ecx, %r14d
movl %r14d, %ecx
subl %r12d, %ecx
cmpl %edx, %ecx
jge 0x3d46
xorl %r12d, %r12d
cmpl $0x0, 0x10(%rsp)
jle 0x3cfb
movq 0x58(%r13), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
movsbl (%rdx,%rsi), %edi
addl %edi, %ecx
addl $-0x21, %ecx
incq %rsi
cmpq %rsi, %r11
jne 0x3cdc
movq %r11, %rdx
jmp 0x3cff
movl 0x30(%r13), %r14d
xorl %r12d, %r12d
jmp 0x3d46
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %rax
jbe 0x3d3d
movq 0x58(%r13), %rdx
addq 0x38(%rsp), %rdx
addq 0x30(%rsp), %rax
xorl %r12d, %r12d
movl 0x2c(%rsp), %esi
movl %ecx, %edi
movsbl (%rdx), %r8d
movsbl (%rdx,%r15), %r9d
subl %r9d, %r8d
addl %r8d, %edi
cmpl %edi, %ecx
cmovll %esi, %r12d
cmovlel %edi, %ecx
incq %rdx
incl %esi
decq %rax
jne 0x3d1b
movq 0x10(%rsp), %rax
leal (%r12,%rax), %r14d
movq %r11, %rbx
xorl %eax, %eax
cmpl $0x0, 0x64(%r13)
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r13), %rdi
movq 0xc256(%rip), %rax      # 0xffc0
movq (%rax), %rsi
callq 0x2110
cmpq $0x0, 0x18(%r13)
je 0x3d8e
movl $0x20, %edi
callq 0x2060
movq 0x28(%r13), %rdi
callq 0x2080
jmp 0x3d98
movl $0xa, %edi
callq 0x2060
movslq %r12d, %rbp
movq 0x40(%r13), %rdi
addq %rbp, %rdi
subl %ebp, %r14d
movslq %r14d, %r12
movq 0xc211(%rip), %r14      # 0xffc0
movq (%r14), %rcx
movl $0x1, %esi
movq %r12, %rdx
callq 0x2260
movl $0xa, %edi
callq 0x2060
cmpl $0x0, 0x64(%r13)
je 0x3dfd
leaq 0x75f4(%rip), %rdi      # 0xb3cb
callq 0x2080
addq 0x58(%r13), %rbp
movq (%r14), %rcx
movl $0x1, %esi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x2260
movl $0xa, %edi
callq 0x2060
movq %r13, %rdi
callq 0x3ea1
testl %eax, %eax
movq %rbx, %r11
movl $0x24, %ebx
xorpd %xmm4, %xmm4
jns 0x3be3
movq %r13, %rdi
callq 0x4229
movq 0x40(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x22df
jmp 0x3e50
callq 0x22c0
movl $0x1, %eax
jmp 0x3e2d