// File: /workspace/github_c_makefile_star2_O2/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 $0xf8, %rsp
movq %rcx, %r12
movq %rsi, %r13
movq %rdi, 0x58(%rsp)
movq %r8, 0x20(%rsp)
andq $0x0, (%r8)
movq %r9, 0x28(%rsp)
andq $0x0, (%r9)
xorl %eax, %eax
cmpq $0x6, %rax
je 0x827b
andq $0x0, 0x90(%rsp,%rax,8)
andq $0x0, 0xc0(%rsp,%rax,8)
andq $0x0, 0x60(%rsp,%rax,8)
incq %rax
jmp 0x8258
movslq %edx, %rax
leaq -0x1(%r13), %rcx
movq %rcx, 0x48(%rsp)
xorl %ebp, %ebp
shlq $0x6, %rax
addq 0x23d64(%rip), %rax     # 0x2bff8
movq %rax, 0x50(%rsp)
movq %r12, 0x18(%rsp)
movq %r13, 0x10(%rsp)
cmpq $0x6, %rbp
je 0x849e
cmpq $0x3, %rbp
movl $0x0, %r14d
adcl $-0x1, %r14d
leal -0x3(%rbp), %ebx
cmpq $0x3, %rbp
cmovbl %ebp, %ebx
movq %r13, %rdi
movl %ebx, %esi
callq 0x7fe3
movq %rax, %r15
movq %r13, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x8032
movl 0x4c(%r12), %ecx
testb $0x20, %cl
jne 0x8398
movq %rbp, 0x8(%rsp)
testb $0x8, %cl
je 0x8303
movslq 0x68(%r12), %rcx
cmpq %r13, %rcx
cmovaq %r15, %rax
movq %rax, 0x40(%rsp)
orl $0x1, %r14d
leaq 0x2(%rbx), %rax
movq %rax, 0x38(%rsp)
pushq $0x2
popq %rax
subq %r15, %rax
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %r12
subq %rbx, %r12
leaq (%rax,%rbx), %rbp
xorl %r13d, %r13d
leaq (%rbx,%r13), %rax
addq $0x2, %rax
cmpq %r15, %rax
jg 0x8479
leaq (%rbx,%r13), %rdi
cmpq $0x3, 0x8(%rsp)
cmovaeq %r12, %rdi
addq 0x58(%rsp), %rdi
movl %r14d, %esi
callq 0x7eb4
movzbl %al, %eax
movq 0x50(%rsp), %rcx
cmpb $0x2a, (%rax,%rcx)
je 0x837b
movq %rbp, %rax
addq %r13, %rax
je 0x837b
addq $0x3, %r13
addq $-0x3, %r12
jmp 0x832f
movq 0x38(%rsp), %rax
addq %r13, %rax
cmpq 0x40(%rsp), %rax
jge 0x83f8
addq %r13, %rbx
addq $0x3, %rbx
movq 0x30(%rsp), %rax
jmp 0x8320
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rbp, %rdx
callq 0x806c
movq %rbx, %rax
notq %rax
addq %r13, %rax
cmpq $0x3, %rbp
cmovbq %rbx, %rax
movq 0x90(%rsp,%rbp,8), %rcx
movq 0x60(%rsp,%rbp,8), %rbx
movq %rax, -0x8(%rcx,%rbx,8)
movq %r13, %rdi
movl %ebp, %esi
callq 0x7fe3
movq 0xc0(%rsp,%rbp,8), %rcx
movq %rax, -0x8(%rcx,%rbx,8)
testb $0x1, 0x4c(%r12)
jne 0x8496
jmp 0x849e
xorl %ebx, %ebx
cmpq $0x6, %rbx
je 0x8479
cmpq %rbx, 0x8(%rsp)
je 0x8414
movq 0x18(%rsp), %rax
movl 0x4c(%rax), %eax
andl $0x1, %eax
je 0x8474
movq %rbx, %r14
subq $0x3, %r14
cmovbl %ebx, %r14d
movl %r14d, %eax
notl %eax
cltq
movq 0x10(%rsp), %r12
addq %r12, %rax
cmpq $0x3, %rbx
cmovaeq %rax, %r14
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rbx, %rdx
callq 0x806c
movq 0x90(%rsp,%rbx,8), %rax
movq 0x60(%rsp,%rbx,8), %r15
movq %r14, -0x8(%rax,%r15,8)
movq %r12, %rdi
movl %ebx, %esi
callq 0x7fe3
movq 0xc0(%rsp,%rbx,8), %rcx
movq %rax, -0x8(%rcx,%r15,8)
incq %rbx
jmp 0x83fa
movq 0x8(%rsp), %rbp
cmpq $0x5, %rbp
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
je 0x8496
cmpq $0x0, 0x68(%rsp,%rbp,8)
jne 0x849e
incq %rbp
jmp 0x82a3
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
callq 0x813d
xorl %ebx, %ebx
cmpq $0x6, %rbx
je 0x84e1
movq 0x90(%rsp,%rbx,8), %rdi
callq 0x3390
movq 0xc0(%rsp,%rbx,8), %rdi
callq 0x3390
incq %rbx
jmp 0x84bc
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq