// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/protein.c
// Function: getSequenceORF
int getSequenceORF(char * passSequence, unsigned long passLength, int passTrans, mem_opt_t * passOptions, CDS * * retCDS, unsigned long * retCount) {
	int frameIdx, addIdx, strandDir, relFrame;
	long seqIdx, absIdx, lastStart;
	long endSeqPos, endOrfPos;
	unsigned char currentCodon;
	long * history[2][6], historySize[6];

	// Initialize values
	*retCDS = NULL;
	*retCount = 0;
	for (frameIdx = 0 ; frameIdx < 6 ; frameIdx++) {
		history[0][frameIdx] = NULL;
		history[1][frameIdx] = NULL;
		historySize[frameIdx] = 0;
	}

	for (frameIdx = 0 ; frameIdx < 6 ; frameIdx++) {
		strandDir = (frameIdx < 3 ? 1 : -1);
		relFrame = frameIdx % 3;
		lastStart = relFrame;
		endSeqPos = getLastAlignedPos(passLength, relFrame);
		endOrfPos = getLastAlignedOrfPos(passLength, relFrame, passOptions);

        // Treat entire sequence as ORF in transcript mode
        if (passOptions->proteinFlag & ALIGN_FLAG_MANUAL_TRANSCRIPT) {
            addORFHistory(history, historySize, frameIdx);
            history[0][frameIdx][historySize[frameIdx] - 1] = (strandDir == 1) ? relFrame : passLength - 1 - relFrame;
            history[1][frameIdx][historySize[frameIdx] - 1] = getLastAlignedPos(passLength, frameIdx);

            if (passOptions->proteinFlag & ALIGN_FLAG_BRUTE_ORF) {
                continue;
            }

            break;
        }

		// Adjust min ORF length if requested
		if ((passOptions->proteinFlag & ALIGN_FLAG_ADJUST_ORF) && (passLength < passOptions->min_orf_len)) {
			endOrfPos = getLastAlignedPos(passLength, relFrame);
		}

		// Iterate through all codons in this read frame
		for (seqIdx = relFrame ; seqIdx + 2 <= endSeqPos ; seqIdx += 3) {
			// Calculate absolute index from relative
			absIdx = seqIdx;
			if (strandDir == -1) absIdx = absIdx * -1 + passLength - 1;

			// Encode codon
			currentCodon = encodeCodon(passSequence + absIdx, strandDir);

			// Check for stop codon or EOS
			if ((codon_aa_hash[passTrans][currentCodon] == '*') || (seqIdx + 2 == endSeqPos)) {
				if (seqIdx + relFrame + 2 - lastStart >= endOrfPos) {
					// Translate current (non-brute) or all frames (brute)
					for (addIdx = 0 ; addIdx < 6 ; addIdx++) {
						if ((passOptions->proteinFlag & ALIGN_FLAG_BRUTE_ORF) || (addIdx == frameIdx)) {
							strandDir = (addIdx < 3 ? 1 : -1);
							relFrame = addIdx % 3;

							addORFHistory(history, historySize, addIdx);
							history[0][addIdx][historySize[addIdx] - 1] = (strandDir == 1) ? relFrame : passLength - 1 - relFrame;
							history[1][addIdx][historySize[addIdx] - 1] = getLastAlignedPos(passLength, addIdx);
						}
					}

					break;
				}

				lastStart = seqIdx + 3;
			}
		}

		// If we have brute-force match, end processing
		if (frameIdx < 5) {
			if (historySize[frameIdx + 1]) break;
		}
	}

	// Convert history array into CDS entries
	compileORFHistory(history, historySize, retCDS, retCount);

	for (frameIdx = 0 ; frameIdx < 6 ; frameIdx++) {
		free(history[0][frameIdx]);
		free(history[1][frameIdx]);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, 0x8(%rsp)
movl %edx, %ecx
movq %rsi, %r12
xorl %ebp, %ebp
movq %r8, 0x20(%rsp)
movq %rbp, (%r8)
movq %r9, 0x28(%rsp)
movq %rbp, (%r9)
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rax
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movabsq $0x5555555555555556, %rdx # imm = 0x5555555555555556
movq %rsi, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
addq %rdx, %rax
leaq (%rax,%rax,2), %rax
movq %rsi, %rdx
movq %rax, 0x40(%rsp)
subq %rax, %rdx
movq %rdx, 0x10(%rsp)
notq %rdx
addq %rsi, %rdx
movq %rdx, 0x18(%rsp)
movslq %ecx, %r11
leaq (%rdi,%rsi), %rax
movq %rax, 0x48(%rsp)
leaq 0x2734b(%rip), %rbx     # 0x30b40
shlq $0x6, %r11
addq 0x267f8(%rip), %r11     # 0x2fff8
movq %rdi, 0x38(%rsp)
movq %r11, 0x30(%rsp)
leal -0x3(%rbp), %r14d
cmpq $0x3, %rbp
cmovbl %ebp, %r14d
movslq %r14d, %rsi
movq 0x18(%rsp), %rcx
leaq (%rcx,%rsi), %rax
cmpq %r12, %rax
leaq -0x3(%rcx,%rsi), %r15
cmovlq %rax, %r15
movq %r12, %rdi
movq %r12, %r13
movq 0x8(%rsp), %r12
movq %r12, %rdx
callq 0x93c2
movl 0x4c(%r12), %ecx
testb $0x20, %cl
jne 0x992f
testb $0x8, %cl
movq %r13, %r12
je 0x9867
movq 0x8(%rsp), %rcx
movslq 0x68(%rcx), %rcx
cmpq %r12, %rcx
cmovaq %r15, %rax
leaq 0x2(%r14), %rcx
cmpq %r15, %rcx
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %r11
jg 0x9a5e
movq %rcx, %rsi
movq %r14, %rdx
cmpq $0x2, %rbp
jbe 0x98bc
movq 0x48(%rsp), %r8
subq %rdx, %r8
movl $0x3, %r9d
xorl %edi, %edi
movsbq -0x4(%r8,%r9), %r10
movb (%rbx,%r10), %r10b
cmpb $0x3, %r10b
ja 0x98e7
shlb $0x2, %dil
orb %r10b, %dil
decq %r9
jne 0x989a
xorb $0x3f, %dil
jmp 0x98ea
leaq (%rdx,%r13), %r8
movq $-0x3, %r9
xorl %edi, %edi
movsbq 0x3(%r8,%r9), %r10
movb (%rbx,%r10), %r10b
cmpb $0x3, %r10b
ja 0x98e7
shlb $0x2, %dil
orb %r10b, %dil
incq %r9
jne 0x98c9
jmp 0x98ea
movb $-0x1, %dil
movzbl %dil, %edi
cmpb $0x2a, (%rdi,%r11)
je 0x9900
cmpq %r15, %rsi
je 0x9900
leaq 0x3(%rdx), %rdi
jmp 0x9917
leaq (%rcx,%rdx), %rsi
subq %r14, %rsi
cmpq %rax, %rsi
jge 0x99b7
leaq 0x3(%rdx), %rdi
movq %rdi, %r14
addq $0x5, %rdx
movq %rdx, %rsi
cmpq %r15, %rdx
movq %rdi, %rdx
jle 0x9884
jmp 0x9a5e
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rbp, %rdx
callq 0x9563
movq %r14, %rcx
notq %rcx
addq %r13, %rcx
cmpq $0x3, %rbp
cmovbq %r14, %rcx
movq 0x80(%rsp,%rbp,8), %rdx
movq 0x50(%rsp,%rbp,8), %rax
movq %rcx, -0x8(%rdx,%rax,8)
movq %r13, %r12
jae 0x9983
movq 0x18(%rsp), %rcx
leaq (%rcx,%rbp), %rdx
cmpq %r12, %rdx
leaq -0x3(%rcx,%rbp), %rcx
cmovlq %rdx, %rcx
jmp 0x9996
movq 0x10(%rsp), %rdx
subq %r14, %rdx
leaq 0x3(%rdx), %rcx
testq %rdx, %rdx
cmovnsq %rdx, %rcx
movq 0xb0(%rsp,%rbp,8), %rdx
movq %rcx, -0x8(%rdx,%rax,8)
movq 0x8(%rsp), %rax
testb $0x1, 0x4c(%rax)
jne 0x9a6c
jmp 0x9a79
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x99d0
movq 0x8(%rsp), %rax
movl 0x4c(%rax), %eax
andl $0x1, %eax
je 0x9a51
movq %r14, %r15
subq $0x3, %r15
cmovbl %r14d, %r15d
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x9563
movl %r15d, %eax
notl %eax
movslq %eax, %rcx
addq %r12, %rcx
cmpq $0x3, %r14
cmovbq %r15, %rcx
movq 0x80(%rsp,%r14,8), %rdx
movq 0x50(%rsp,%r14,8), %rax
movq %rcx, -0x8(%rdx,%rax,8)
jae 0x9a31
movq 0x40(%rsp), %rcx
leaq (%rcx,%r14), %rdx
decq %rdx
cmpq %r12, %rdx
leaq -0x4(%rcx,%r14), %rcx
cmovlq %rdx, %rcx
jmp 0x9a44
movq 0x10(%rsp), %rdx
subq %r15, %rdx
leaq 0x3(%rdx), %rcx
testq %rdx, %rdx
cmovnsq %rdx, %rcx
movq 0xb0(%rsp,%r14,8), %rdx
movq %rcx, -0x8(%rdx,%rax,8)
incq %r14
cmpq $0x6, %r14
jne 0x99ba
cmpq $0x5, %rbp
je 0x9a79
cmpq $0x0, 0x58(%rsp,%rbp,8)
jne 0x9a79
incq %rbp
cmpq $0x6, %rbp
jne 0x980a
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0x963e
xorl %ebx, %ebx
movq 0x80(%rsp,%rbx,8), %rdi
callq 0x3390
movq 0xb0(%rsp,%rbx,8), %rdi
callq 0x3390
incq %rbx
cmpq $0x6, %rbx
jne 0x9a97
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq