// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %edi, %r13d
leaq 0x775e(%rip), %rdx      # 0xb1bb
callq 0x2240
testl %eax, %eax
js 0x3b2e
movsd 0x75ee(%rip), %xmm0     # 0xb060
movl $0x1e, %ecx
movq %rcx, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x20(%rsp)
movq 0xc543(%rip), %rbx      # 0xffd8
leaq 0x771f(%rip), %rbp      # 0xb1bb
movl $0x0, 0x10(%rsp)
cmpl $0x64, %eax
movsd %xmm0, 0x28(%rsp)
jle 0x3ace
cmpl $0x65, %eax
je 0x3ae6
cmpl $0x6c, %eax
je 0x3af4
cmpl $0x71, %eax
jne 0x3b10
movq (%rbx), %rdi
callq 0x20a0
movsd %xmm0, 0x28(%rsp)
jmp 0x3b10
cmpl $0x4c, %eax
je 0x3b03
cmpl $0x62, %eax
jne 0x3b10
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0x10(%rsp)
jmp 0x3b10
movq (%rbx), %rdi
callq 0x2250
movl %eax, 0x14(%rsp)
jmp 0x3b10
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x18(%rsp)
jmp 0x3b10
movq (%rbx), %rdi
callq 0x2250
movq %rax, 0x20(%rsp)
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2240
testl %eax, %eax
movsd 0x28(%rsp), %xmm0
jns 0x3aa4
jmp 0x3b5a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1e, %eax
movq %rax, 0x18(%rsp)
movsd 0x750e(%rip), %xmm0     # 0xb060
movl $0x0, 0x14(%rsp)
movq 0xc46f(%rip), %rax      # 0xffd0
movslq (%rax), %rax
cmpl %r13d, %eax
je 0x3ec5
movsd %xmm0, 0x28(%rsp)
movq (%r12,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x3b86
cmpb $0x0, 0x1(%rdi)
je 0x3ea3
leaq 0x761b(%rip), %rsi      # 0xb1a8
callq 0x2220
testq %rax, %rax
je 0x3ed1
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x3edd
movq %rax, %r13
movl $0x21, %ebx
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
movsd 0x74a6(%rip), %xmm0     # 0xb068
divsd %xmm0, %xmm1
callq 0x20c0
movsd %xmm0, 0x60(%rsp,%r14,8)
incq %r14
decl %ebx
cmpq $0x80, %r14
jne 0x3bb3
movq %r13, %rdi
callq 0x3f27
testl %eax, %eax
js 0x3e7d
movl 0x10(%rsp), %eax
orl 0x14(%rsp), %eax
setne %al
cmpl $0x0, 0x20(%rsp)
setg %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
movq 0x18(%rsp), %rax
movslq %eax, %rcx
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %eax, %edx
movl %ecx, %r10d
movl %edx, %eax
subl %ecx, %eax
incl %eax
movl %eax, 0x34(%rsp)
movq %rcx, 0x50(%rsp)
movq %rcx, %r11
negq %r11
movq %rdx, 0x48(%rsp)
negq %rdx
movq %rdx, 0x40(%rsp)
movl $0x24, %ebx
xorpd %xmm4, %xmm4
movq %r11, 0x58(%rsp)
cmpb $0x0, 0xf(%rsp)
je 0x3c94
movl 0x30(%r13), %eax
subl 0x14(%rsp), %eax
movl 0x10(%rsp), %ecx
cmpl %eax, %ecx
movl $0x0, %r12d
cmovll %ecx, %r12d
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %ecx
subl %r12d, %ecx
movq 0x20(%rsp), %rdx
leal (%r12,%rdx), %r15d
cmpl %edx, %ecx
cmovlel %eax, %r15d
testl %edx, %edx
cmovlel %eax, %r15d
jmp 0x3da5
movq 0x48(%r13), %rax
cmpq 0x50(%rsp), %rax
jbe 0x3d9e
movq 0x58(%r13), %rcx
xorl %esi, %esi
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movl %eax, %edx
xorl %r8d, %r8d
xorl %edi, %edi
movsd 0x28(%rsp), %xmm3
movapd %xmm1, %xmm2
movzbl (%rcx,%rsi), %r9d
cmpb $0x25, %r9b
cmovll %ebx, %r9d
movzbl %r9b, %r9d
movapd %xmm3, %xmm1
subsd 0x60(%rsp,%r9,8), %xmm1
addsd %xmm0, %xmm1
incq %rsi
ucomisd %xmm2, %xmm1
cmoval %r8d, %edi
cmoval %esi, %edx
ucomisd %xmm1, %xmm4
xorpd %xmm0, %xmm0
maxsd %xmm1, %xmm0
maxsd %xmm2, %xmm1
cmoval %esi, %r8d
cmpq %rsi, %rax
jne 0x3cbe
ucomisd %xmm4, %xmm1
movl $0x0, %r12d
cmovnel %edi, %r12d
cmovpl %edi, %r12d
movq 0x18(%rsp), %rsi
movl %esi, %r15d
cmovnel %edx, %r15d
cmovpl %edx, %r15d
movl %r15d, %edx
subl %r12d, %edx
cmpl %esi, %edx
jge 0x3da5
movl $0x0, %edx
cmpl $0x0, 0x18(%rsp)
jle 0x3d5e
xorl %esi, %esi
xorl %edx, %edx
movsbl (%rcx,%rsi), %edi
addl %edi, %edx
addl $-0x21, %edx
incq %rsi
cmpq %rsi, %r10
jne 0x3d45
xorl %r12d, %r12d
cmpq %r10, %rax
jbe 0x3d93
addq 0x48(%rsp), %rcx
addq 0x40(%rsp), %rax
movl 0x34(%rsp), %esi
movl %edx, %edi
xorl %r12d, %r12d
movsbl (%rcx), %r8d
movsbl (%rcx,%r11), %r9d
subl %r9d, %r8d
addl %r8d, %edi
cmpl %edi, %edx
cmovll %esi, %r12d
cmovlel %edi, %edx
incq %rcx
incl %esi
decq %rax
jne 0x3d71
movq 0x18(%rsp), %rax
leal (%r12,%rax), %r15d
jmp 0x3da5
movl 0x30(%r13), %r15d
xorl %r12d, %r12d
movq %r10, %rbx
movl 0x64(%r13), %r14d
xorl %eax, %eax
testl %r14d, %r14d
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r13), %rdi
movq 0xc1f5(%rip), %rax      # 0xffc0
movq (%rax), %rsi
callq 0x2110
cmpq $0x0, 0x18(%r13)
je 0x3def
movl $0x20, %edi
callq 0x2060
movq 0x28(%r13), %rdi
callq 0x2080
jmp 0x3df9
movl $0xa, %edi
callq 0x2060
movslq %r12d, %rbp
movq 0x40(%r13), %rdi
addq %rbp, %rdi
subl %ebp, %r15d
movslq %r15d, %r12
movq 0xc1b0(%rip), %r15      # 0xffc0
movq (%r15), %rcx
movl $0x1, %esi
movq %r12, %rdx
callq 0x2260
movl $0xa, %edi
callq 0x2060
testl %r14d, %r14d
je 0x3e5c
leaq 0x75a5(%rip), %rdi      # 0xb3db
callq 0x2080
addq 0x58(%r13), %rbp
movq (%r15), %rcx
movl $0x1, %esi
movq %rbp, %rdi
movq %r12, %rdx
callq 0x2260
movl $0xa, %edi
callq 0x2060
movq %r13, %rdi
callq 0x3f27
testl %eax, %eax
movq %rbx, %r10
movq 0x58(%rsp), %r11
movl $0x24, %ebx
xorpd %xmm4, %xmm4
jns 0x3c4e
movq %r13, %rdi
callq 0x428d
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 0xc11e(%rip), %rax      # 0xffc8
movq (%rax), %rdi
callq 0x21b0
leaq 0x72ef(%rip), %rsi      # 0xb1a8
movl %eax, %edi
callq 0x20b0
jmp 0x3b92
movq 0x18(%rsp), %rdi
callq 0x22df
jmp 0x3ed6
callq 0x22c0
movl $0x1, %eax
jmp 0x3e91