// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/uniprot.c
// Function: cleanUniprotReferenceUniref
void cleanUniprotReferenceUniref(const char * passName, int passANN) {
	gzFile srcHandle, dstHandle;
	FILE * dstANNHandle;
	int searchIdx, searchIdx2, lineIdx;
	char * newName;
	char srcBuffer[4096], dstBuffer[16384];
	char target[] = "RepID=";

	// Prepare and open reference and temporary destination
	newName = malloc(strlen(passName) + 5);
	sprintf(newName, "%s.tmp", passName);

	srcHandle = xzopen(passName, "r");
	if (!passANN) dstHandle = xzopen(newName, "w");
	else dstANNHandle = err_xopen_core(__func__, newName, "w");
	lineIdx = 0;

	while (gzgets(srcHandle, srcBuffer, 4096)) {
		// For FASTA files, must start with ">".  For ANN, must be an odd line
		if (((passANN == 0) && (srcBuffer[0] == '>')) ||
				((passANN == 1) && (lineIdx % 2 == 1))) {
			// Alter header - strip CRLF
			for (searchIdx = strlen(srcBuffer) - 1 ; searchIdx >= 0 ; searchIdx--) {
				if ((srcBuffer[searchIdx] == 0x0A) || (srcBuffer[searchIdx] == 0x0D)) {
					srcBuffer[searchIdx] = 0x00;
				}
				else break;
			}

			// Find RepID
			for (searchIdx = 0 ; searchIdx < strlen(srcBuffer) - strlen(target) ; searchIdx++) {
				for (searchIdx2 = 0 ; searchIdx2 < strlen(target) ; searchIdx2++ ) {
					if (srcBuffer[searchIdx + searchIdx2] != target[searchIdx2]) break;
				}

				if (searchIdx2 == strlen(target)) {
					// RepID found, write new header
					if (!passANN) sprintf(dstBuffer, ">%s %s\n", srcBuffer + searchIdx + strlen(target), srcBuffer + 1);
					else sprintf(dstBuffer, "0 %s %s\n", srcBuffer + searchIdx + strlen(target), srcBuffer + 2);
					break;
				}
			}

			if (!passANN) gzwrite(dstHandle, dstBuffer, strlen(dstBuffer));
			else fwrite(dstBuffer, 1, strlen(dstBuffer), dstANNHandle);
		}
		else {
			// Copy sequence
			if (!passANN) gzwrite(dstHandle, srcBuffer, strlen(srcBuffer));
			else fwrite(srcBuffer, 1, strlen(srcBuffer), dstANNHandle);
		}

		lineIdx++;
	}

	err_gzclose(srcHandle);
	if (!passANN) err_gzclose(dstHandle);
	else fclose(dstANNHandle);

	// Delete original file and replace with new one
	unlink(passName);
	rename(newName, passName);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5028, %rsp           # imm = 0x5028
movl %esi, %ebp
movq %rdi, %rbx
callq 0x3100
leaq 0x5(%rax), %rdi
callq 0x3470
movq %rax, %r14
leaq 0x18aad(%rip), %rsi     # 0x224d1
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3070
leaq 0x18aa0(%rip), %rdi     # 0x224d8
leaq 0x183df(%rip), %rdx     # 0x21e1e
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
callq 0xcacc
movq %rax, %r15
testl %ebp, %ebp
movq %r14, 0x10(%rsp)
leaq 0x18a7b(%rip), %rdi     # 0x224d8
leaq 0x1aa7b(%rip), %rdx     # 0x244df
movq %r14, %rsi
je 0x9a75
callq 0xc931
movq %rax, 0x8(%rsp)
jmp 0x9a7e
callq 0xcacc
movq %rax, (%rsp)
leaq 0x20(%rsp), %rbx
leaq 0x1020(%rsp), %r14
leaq 0x18a38(%rip), %r13     # 0x224ca
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3670
testq %rax, %rax
je 0x9bc8
testl %ebp, %ebp
jne 0x9ab9
cmpb $0x3e, 0x20(%rsp)
je 0x9ace
cmpl $0x1, %ebp
jne 0x9b8b
movl %r12d, %eax
andl $0x1, %eax
je 0x9b8b
movq %rbx, %rdi
callq 0x3100
movl %eax, %eax
decq %rax
testl %eax, %eax
js 0x9afd
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movzbl 0x20(%rsp,%rcx), %edx
cmpl $0xd, %edx
je 0x9af6
cmpl $0xa, %edx
jne 0x9afd
movb $0x0, 0x20(%rsp,%rcx)
jmp 0x9ad8
movq %rbx, %rdi
callq 0x3100
addq $-0x6, %rax
movq %rbx, %rcx
xorl %edx, %edx
cmpq %rax, %rdx
je 0x9bc2
xorl %esi, %esi
cmpq $0x6, %rsi
je 0x9b38
movb (%rcx,%rsi), %dil
leaq 0x1(%rsi), %r8
cmpb (%rsi,%r13), %dil
movq %r8, %rsi
je 0x9b19
incq %rdx
incq %rcx
jmp 0x9b0e
leaq 0x26(%rsp), %rax
addq %rax, %rdx
movq %r14, %rdi
testl %ebp, %ebp
je 0x9b67
leaq 0x189ae(%rip), %rsi     # 0x224fc
leaq 0x22(%rsp), %rcx
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
callq 0x3100
movq %r14, %rdi
jmp 0x9b9a
leaq 0x18986(%rip), %rsi     # 0x224f4
leaq 0x21(%rsp), %rcx
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
callq 0x3100
movq (%rsp), %rdi
movq %r14, %rsi
jmp 0x9bb3
movq %rbx, %rdi
callq 0x3100
testl %ebp, %ebp
je 0x9bac
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
callq 0x3790
jmp 0x9bba
movq (%rsp), %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0x3530
incl %r12d
jmp 0x9a95
testl %ebp, %ebp
jne 0x9b5a
jmp 0x9b7a
movq %r15, %rdi
callq 0xd044
testl %ebp, %ebp
je 0x9be0
movq 0x8(%rsp), %rdi
callq 0x32f0
jmp 0x9be9
movq (%rsp), %rdi
callq 0xd044
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x3110
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3180
addq $0x5028, %rsp           # imm = 0x5028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq