// File: /workspace/github_c_makefile_star2_O1/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 0x1c4ce(%rip), %rsi     # 0x274a1
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3070
leaq 0x1c4c1(%rip), %rdi     # 0x274a8
leaq 0x1be30(%rip), %rdx     # 0x26e1e
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
callq 0xdf35
movq %rax, %r15
testl %ebp, %ebp
movq %r14, 0x10(%rsp)
leaq 0x1c49c(%rip), %rdi     # 0x274a8
leaq 0x1e41c(%rip), %rdx     # 0x2942f
movq %r14, %rsi
je 0xb024
callq 0xdd8e
movq %rax, 0x8(%rsp)
jmp 0xb02d
callq 0xdf35
movq %rax, (%rsp)
leaq 0x20(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3670
testq %rax, %rax
je 0xb17e
leaq 0x1020(%rsp), %rbx
leaq 0x1c440(%rip), %r13     # 0x2749a
xorl %r12d, %r12d
testl %ebp, %ebp
jne 0xb068
cmpb $0x3e, 0x20(%rsp)
je 0xb075
cmpl $0x1, %ebp
jne 0xb0e2
movl %r12d, %eax
andl $0x1, %eax
je 0xb0e2
movq %r14, %rdi
callq 0x3100
decl %eax
js 0xb09f
movl %eax, %eax
incq %rax
movzbl 0x1f(%rsp,%rax), %ecx
cmpl $0xd, %ecx
je 0xb095
cmpl $0xa, %ecx
jne 0xb09f
movb $0x0, 0x1f(%rsp,%rax)
decq %rax
jg 0xb086
movq %r14, %rdi
callq 0x3100
addq $-0x6, %rax
je 0xb131
movq %r14, %rcx
xorl %edx, %edx
xorl %esi, %esi
movb (%rcx,%rsi), %dil
cmpb (%rsi,%r13), %dil
jne 0xb0d0
incq %rsi
cmpq $0x6, %rsi
jne 0xb0b8
movl $0x6, %esi
cmpl $0x6, %esi
je 0xb0f8
incq %rdx
incq %rcx
cmpq %rax, %rdx
jne 0xb0b6
jmp 0xb131
movq %r14, %rdi
callq 0x3100
testl %ebp, %ebp
je 0xb115
movl $0x1, %esi
movq %r14, %rdi
jmp 0xb145
leaq 0x26(%rsp), %rax
addq %rax, %rdx
movq %rbx, %rdi
testl %ebp, %ebp
je 0xb11e
leaq 0x1c3be(%rip), %rsi     # 0x274cc
leaq 0x22(%rsp), %rcx
jmp 0xb12a
movq (%rsp), %rdi
movq %r14, %rsi
jmp 0xb15b
leaq 0x1c39f(%rip), %rsi     # 0x274c4
leaq 0x21(%rsp), %rcx
xorl %eax, %eax
callq 0x3070
movq %rbx, %rdi
callq 0x3100
testl %ebp, %ebp
je 0xb154
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
callq 0x3790
jmp 0xb162
movq (%rsp), %rdi
movq %rbx, %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 0xb05d
movq %r15, %rdi
callq 0xe4b9
testl %ebp, %ebp
je 0xb196
movq 0x8(%rsp), %rdi
callq 0x32f0
jmp 0xb19f
movq (%rsp), %rdi
callq 0xe4b9
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