// File: /workspace/github_c_makefile_star2_O3/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 0x1c335(%rip), %rsi     # 0x27521
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3070
leaq 0x1c328(%rip), %rdi     # 0x27528
leaq 0x1bc63(%rip), %rdx     # 0x26e6a
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
callq 0xe1c4
movq %rax, %r15
testl %ebp, %ebp
movq %r14, 0x10(%rsp)
leaq 0x1c303(%rip), %rdi     # 0x27528
leaq 0x1e333(%rip), %rdx     # 0x2955f
movq %r14, %rsi
je 0xb23d
callq 0xe023
movq %rax, 0x8(%rsp)
jmp 0xb246
callq 0xe1c4
movq %rax, (%rsp)
leaq 0x20(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3670
testq %rax, %rax
je 0xb3a0
leaq 0x1020(%rsp), %rbx
leaq 0x1c2a7(%rip), %r13     # 0x2751a
xorl %r12d, %r12d
testl %ebp, %ebp
jne 0xb281
cmpb $0x3e, 0x20(%rsp)
je 0xb296
cmpl $0x1, %ebp
jne 0xb32f
movl %r12d, %eax
andl $0x1, %eax
je 0xb32f
movq %r14, %rdi
callq 0x3100
decl %eax
js 0xb2c0
movl %eax, %eax
incq %rax
movzbl 0x1f(%rsp,%rax), %ecx
cmpl $0xd, %ecx
je 0xb2b6
cmpl $0xa, %ecx
jne 0xb2c0
movb $0x0, 0x1f(%rsp,%rax)
decq %rax
jg 0xb2a7
movq %r14, %rdi
callq 0x3100
addq $-0x6, %rax
je 0xb2f5
movq %r14, %rcx
xorl %edx, %edx
xorl %esi, %esi
movb (%rcx,%rsi), %dil
cmpb (%rsi,%r13), %dil
jne 0xb2ea
incq %rsi
cmpq $0x6, %rsi
jne 0xb2d5
jmp 0xb2fb
incq %rdx
incq %rcx
cmpq %rax, %rdx
jne 0xb2d3
testl %ebp, %ebp
jne 0xb31d
jmp 0xb365
leaq 0x26(%rsp), %rax
addq %rax, %rdx
movq %rbx, %rdi
testl %ebp, %ebp
je 0xb352
leaq 0x1c23b(%rip), %rsi     # 0x2754c
leaq 0x22(%rsp), %rcx
xorl %eax, %eax
callq 0x3070
movq %rbx, %rdi
callq 0x3100
movl $0x1, %esi
movq %rbx, %rdi
jmp 0xb343
movq %r14, %rdi
callq 0x3100
testl %ebp, %ebp
je 0xb376
movl $0x1, %esi
movq %r14, %rdi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
callq 0x3790
jmp 0xb384
leaq 0x1c1eb(%rip), %rsi     # 0x27544
leaq 0x21(%rsp), %rcx
xorl %eax, %eax
callq 0x3070
movq %rbx, %rdi
callq 0x3100
movq (%rsp), %rdi
movq %rbx, %rsi
jmp 0xb37d
movq (%rsp), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x3530
incl %r12d
movq %r15, %rdi
movq %r14, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3670
testq %rax, %rax
jne 0xb276
movq %r15, %rdi
callq 0xe747
testl %ebp, %ebp
je 0xb3b8
movq 0x8(%rsp), %rdi
callq 0x32f0
jmp 0xb3c1
movq (%rsp), %rdi
callq 0xe747
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