// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x34(%rbp)
cmpl $0x6, -0x34(%rbp)
jge 0xabf0
movslq -0x34(%rbp), %rax
movq $0x0, -0xd0(%rbp,%rax,8)
movslq -0x34(%rbp), %rax
movq $0x0, -0xa0(%rbp,%rax,8)
movslq -0x34(%rbp), %rax
movq $0x0, -0x100(%rbp,%rax,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xabaf
movl $0x0, -0x34(%rbp)
cmpl $0x6, -0x34(%rbp)
jge 0xaf21
movl -0x34(%rbp), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl $0x3, %edx
cmovll %ecx, %eax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, -0x40(%rbp)
movslq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0xa680
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x20(%rbp), %rdx
callq 0xa730
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movl 0x4c(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xad13
leaq -0xd0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movslq -0x34(%rbp), %rdx
callq 0xa7d0
cmpl $0x1, -0x3c(%rbp)
jne 0xac8e
movslq -0x40(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0xaca4
movq -0x10(%rbp), %rax
subq $0x1, %rax
movslq -0x40(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rdx
movslq -0x34(%rbp), %rax
movq -0xd0(%rbp,%rax,8), %rax
movslq -0x34(%rbp), %rcx
movq -0x100(%rbp,%rcx,8), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0xa680
movq %rax, %rdx
movslq -0x34(%rbp), %rax
movq -0xa0(%rbp,%rax,8), %rax
movslq -0x34(%rbp), %rcx
movq -0x100(%rbp,%rcx,8), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movl 0x4c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xad0e
jmp 0xaf13
jmp 0xaf21
movq -0x20(%rbp), %rax
movl 0x4c(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xad43
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq 0x68(%rcx), %rcx
cmpq %rcx, %rax
jae 0xad43
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0xa680
movq %rax, -0x68(%rbp)
movslq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
addq $0x2, %rax
cmpq -0x60(%rbp), %rax
jg 0xaef4
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0xad7c
imulq $-0x1, -0x50(%rbp), %rax
addq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
addq -0x50(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0xa450
movb %al, -0x69(%rbp)
movslq -0x14(%rbp), %rcx
leaq 0x35646(%rip), %rax     # 0x403e0
shlq $0x6, %rcx
addq %rcx, %rax
movzbl -0x69(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
je 0xadc0
movq -0x48(%rbp), %rax
addq $0x2, %rax
cmpq -0x60(%rbp), %rax
jne 0xaee1
movq -0x48(%rbp), %rax
movslq -0x40(%rbp), %rcx
addq %rcx, %rax
addq $0x2, %rax
subq -0x58(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0xaed5
movl $0x0, -0x38(%rbp)
cmpl $0x6, -0x38(%rbp)
jge 0xaed3
movq -0x20(%rbp), %rax
movl 0x4c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xae09
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0xaec3
movl -0x38(%rbp), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl $0x3, %edx
cmovll %ecx, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, -0x40(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movslq -0x38(%rbp), %rdx
callq 0xa7d0
cmpl $0x1, -0x3c(%rbp)
jne 0xae57
movslq -0x40(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0xae6d
movq -0x10(%rbp), %rax
subq $0x1, %rax
movslq -0x40(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdx
movslq -0x38(%rbp), %rax
movq -0xd0(%rbp,%rax,8), %rax
movslq -0x38(%rbp), %rcx
movq -0x100(%rbp,%rcx,8), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0xa680
movq %rax, %rdx
movslq -0x38(%rbp), %rax
movq -0xa0(%rbp,%rax,8), %rax
movslq -0x38(%rbp), %rcx
movq -0x100(%rbp,%rcx,8), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xaec5
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xade4
jmp 0xaef4
movq -0x48(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x58(%rbp)
jmp 0xaee3
movq -0x48(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x48(%rbp)
jmp 0xad4b
cmpl $0x5, -0x34(%rbp)
jge 0xaf11
movl -0x34(%rbp), %eax
addl $0x1, %eax
cltq
cmpq $0x0, -0x100(%rbp,%rax,8)
je 0xaf0f
jmp 0xaf21
jmp 0xaf11
jmp 0xaf13
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xabf7
leaq -0xd0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0xa930
movl $0x0, -0x34(%rbp)
cmpl $0x6, -0x34(%rbp)
jge 0xaf76
movslq -0x34(%rbp), %rax
movq -0xd0(%rbp,%rax,8), %rdi
callq 0x3380
movslq -0x34(%rbp), %rax
movq -0xa0(%rbp,%rax,8), %rdi
callq 0x3380
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xaf43
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)