// File: /workspace/github_c_makefile_star2_O1/aokad[P]sequeeze/seqtk.c
// Function: binning
int binning(gzFile fp, char *bin_quality)
{
	kseq_t *seq, last;
	uint64_t n = 1;

	char base_quality[] = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJ";
	//char bin_quality[] = "!!''''''''000000000077777<<<<<BBBBBFFFFFII";

	seq = kseq_init(fp);

	memset(&last, 0, sizeof(kseq_t));
	while (kseq_read(seq) >= 0) {
		int i;
		for (i = 0; i < seq->qual.l; ++i) {
			char *p = strchr(base_quality, seq->qual.s[i]);
			int pos = 42;
			if (p != NULL) {
				pos = p - base_quality;
			}
			seq->qual.s[i] = bin_quality[pos];
		}
		if (last.name.l) {
			kstring_t *p = &last.name, *q = &seq->name;
			int is_diff;
			if (p->l == q->l) {
				int l = (p->l > 2 && p->s[p->l-2] == '/' && q->s[q->l-2] == '/' && isdigit(p->s[p->l-1]) && isdigit(q->s[q->l-1]))? p->l - 2 : p->l;
				is_diff = strncmp(p->s, q->s, l);
			} else is_diff = 1;
			if (!is_diff) {
				stk_printseq(&last, 0);
				stk_printseq(seq,   0);
				last.name.l = 0;
				++n;
			} else {
				stk_printseq(&last, 0);
				++n;
				cpy_kseq(&last, seq);
			}
		} else cpy_kseq(&last, seq);
	}
	if (last.name.l) stk_printseq(&last, 0);

	kseq_destroy(seq);
	gzclose(fp);
	// free last!
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r15
movups 0x34c2(%rip), %xmm0     # 0xc97b
movups %xmm0, 0xab(%rsp)
movaps 0x34a8(%rip), %xmm0     # 0xc970
movaps %xmm0, 0xa0(%rsp)
movaps 0x3489(%rip), %xmm0     # 0xc960
movaps %xmm0, 0x90(%rsp)
movq %rdi, 0x18(%rsp)
callq 0x3e57
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movq %rax, %rdi
callq 0x3ea1
testl %eax, %eax
js 0x974f
leaq 0x90(%rsp), %r13
movl $0x2a, %ebx
cmpq $0x0, 0x48(%r14)
je 0x956f
xorl %r12d, %r12d
movq 0x58(%r14), %rbp
movsbl (%rbp,%r12), %esi
movq %r13, %rdi
callq 0x20e0
movl %eax, %ecx
subl %r13d, %ecx
testq %rax, %rax
movslq %ecx, %rax
cmoveq %rbx, %rax
movb (%r15,%rax), %al
movb %al, (%rbp,%r12)
incq %r12
cmpq %r12, 0x48(%r14)
ja 0x953c
movq 0x20(%rsp), %r12
testq %r12, %r12
je 0x965d
movq (%r14), %rbp
cmpq %rbp, %r12
jne 0x95f9
cmpq $0x3, %r12
jb 0x95dd
movq 0x30(%rsp), %rax
cmpb $0x2f, -0x2(%rax,%r12)
jne 0x95dd
movq 0x10(%r14), %rcx
cmpb $0x2f, -0x2(%rcx,%rbp)
jne 0x95dd
movq %rax, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
callq 0x2290
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movsbq -0x1(%rcx,%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
movq 0x10(%rsp), %rcx
je 0x95dd
movsbq -0x1(%rcx,%rbp), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
je 0x95dd
addq $-0x2, %r12
movq 0x30(%rsp), %rbp
movq 0x10(%r14), %rsi
movslq %r12d, %rdx
movq %rbp, %rdi
callq 0x2070
testl %eax, %eax
je 0x967f
movq 0x68(%rsp), %r12
xorl %eax, %eax
testq %r12, %r12
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x30(%rsp), %rdi
movq 0x69a2(%rip), %rax      # 0xffc0
movq (%rax), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2080
testq %r12, %r12
je 0x965d
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x78(%rsp), %rdi
callq 0x2080
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x6ee7
movq %r14, %rdi
callq 0x3ea1
testl %eax, %eax
jns 0x9532
jmp 0x974f
movq 0x68(%rsp), %r12
xorl %eax, %eax
testq %r12, %r12
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x6921(%rip), %rax      # 0xffc0
movq (%rax), %rsi
movq %rbp, %rdi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2080
testq %r12, %r12
je 0x96e1
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x78(%rsp), %rdi
callq 0x2080
xorl %eax, %eax
cmpq $0x0, 0x48(%r14)
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x10(%r14), %rdi
movq 0x68be(%rip), %rax      # 0xffc0
movq (%rax), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x40(%r14), %rdi
callq 0x2080
cmpq $0x0, 0x48(%r14)
je 0x9741
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x58(%r14), %rdi
callq 0x2080
movq $0x0, 0x20(%rsp)
jmp 0x966a
cmpq $0x0, 0x20(%rsp)
je 0x97bb
movq 0x68(%rsp), %rbx
xorl %eax, %eax
testq %rbx, %rbx
setne %al
leal 0x3e(,%rax,2), %edi
callq 0x2060
movq 0x30(%rsp), %rdi
movq 0x6844(%rip), %rax      # 0xffc0
movq (%rax), %rsi
callq 0x2110
movl $0xa, %edi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2080
testq %rbx, %rbx
je 0x97bb
movl $0x2b, %edi
callq 0x2060
movl $0xa, %edi
callq 0x2060
movq 0x78(%rsp), %rdi
callq 0x2080
movq %r14, %rdi
callq 0x4229
movq 0x18(%rsp), %rdi
callq 0x2040
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq