// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/uniprot.c
// Function: addUniprotList
int addUniprotList(worker_t * passWorker, int passSize, int passFull) {
	int entryIdx, alnIdx, addPriIdx, addSecIdx, parseIdx;
	int refID, alignType, primaryCount, totalAlign, entryQuality;
	UniprotList * globalLists;
	int * globalCount, * currentIdx;
	char * uniprotEntry;

	// Create lists
	uniprotPriEntryLists = realloc(uniprotPriEntryLists, (uniprotPriListCount + 1) * sizeof(UniprotList));
	memset(uniprotPriEntryLists + uniprotPriListCount, 0, sizeof(UniprotList));
	uniprotSecEntryLists = realloc(uniprotSecEntryLists, (uniprotSecListCount + 1) * sizeof(UniprotList));
	memset(uniprotSecEntryLists + uniprotSecListCount, 0, sizeof(UniprotList));

	// Calculate potential total list size
	for (entryIdx = 0, totalAlign = 0 ; entryIdx < passSize ; entryIdx++) {
		// Only add active sequences
		if (!passWorker->regs[entryIdx].active) continue;

		for (alnIdx = 0, primaryCount = 0 ; alnIdx < passWorker->regs[entryIdx].n ; alnIdx++) {
			switch (alignType = getAlignmentType(passWorker, entryIdx, alnIdx)) {
			case MEM_ALIGN_PRIMARY:
				uniprotPriEntryLists[uniprotPriListCount].entryCount++;
				// Count non-linear as total alignments
				if (primaryCount++) totalAlign++;
				break;
			case MEM_ALIGN_SECONDARY:
				uniprotSecEntryLists[uniprotSecListCount].entryCount++; break;
			}
		}	

		totalAlign++;
	}

	// Set counts and allocate entries
	uniprotPriEntryLists[uniprotPriListCount].unalignedCount = totalAlign - uniprotPriEntryLists[uniprotPriListCount].entryCount;
	uniprotSecEntryLists[uniprotSecListCount].unalignedCount = totalAlign - uniprotSecEntryLists[uniprotSecListCount].entryCount;;
	uniprotPriEntryLists[uniprotPriListCount].entries = calloc(uniprotPriEntryLists[uniprotPriListCount].entryCount, sizeof(UniprotEntry));
	uniprotSecEntryLists[uniprotSecListCount].entries = calloc(uniprotSecEntryLists[uniprotSecListCount].entryCount, sizeof(UniprotEntry));

	// Populate list
	for (addPriIdx = 0, addSecIdx = 0, entryIdx = 0 ; entryIdx < passSize && passFull ; entryIdx++) {
		// Only add active sequences
		if (!passWorker->regs[entryIdx].active) continue;

		for (alnIdx = 0 ; alnIdx < passWorker->regs[entryIdx].n ; alnIdx++) {
			// Only add successful alignments
			if ((alignType = getAlignmentType(passWorker, entryIdx, alnIdx)) < MEM_ALIGN_PRIMARY) continue;

			globalLists = getGlobalLists(alignType == MEM_ALIGN_PRIMARY);
			globalCount = getGlobalCount(alignType == MEM_ALIGN_PRIMARY);
			currentIdx = (alignType == MEM_ALIGN_PRIMARY) ? &addPriIdx : &addSecIdx;

			// Extract ID and entry
			refID = passWorker->regs[entryIdx].a[alnIdx].rid;
			uniprotEntry = passWorker->bns->anns[refID].name;

			// Strip sequence and frame info for nucleotide references
			if (passWorker->opt->indexInfo.nucleotide) {
				for (parseIdx = 2 ; (parseIdx > 0) && (*uniprotEntry != 0) ; uniprotEntry++) {
					if (*uniprotEntry == ':') parseIdx--;
				}
			}

			// Strip initial IDs (if present) and description
			for (parseIdx = 0 ; (uniprotEntry[parseIdx] != 0) && (uniprotEntry[parseIdx] != ' ') ; parseIdx++) {
				if (uniprotEntry[parseIdx] == '|') {
					uniprotEntry += parseIdx + 1;
					parseIdx = 0;
				}
			}

			uniprotEntry[parseIdx] = 0;	

			// Full ID and quality
			globalLists[*globalCount].entries[*currentIdx].id = malloc(strlen(uniprotEntry) + 1);
			sprintf(globalLists[*globalCount].entries[*currentIdx].id, "%s", uniprotEntry);
			globalLists[*globalCount].entries[*currentIdx].numOccurrence = 1;

            entryQuality = passWorker->regs[entryIdx].a[alnIdx].mapq;
			globalLists[*globalCount].entries[*currentIdx].totalQuality = entryQuality;
            if (entryQuality > globalLists[*globalCount].entries[*currentIdx].maxQuality) {
                globalLists[*globalCount].entries[*currentIdx].maxQuality = entryQuality;
            }

			// Gene/organism
			for (parseIdx = 0 ; parseIdx < strlen(uniprotEntry) ; parseIdx++) {
				if (*(uniprotEntry + parseIdx) == '_') {
					globalLists[*globalCount].entries[*currentIdx].gene = malloc(parseIdx + 1);
					sprintf(globalLists[*globalCount].entries[*currentIdx].gene, "%.*s", parseIdx, uniprotEntry);
					globalLists[*globalCount].entries[*currentIdx].organism = malloc(strlen(uniprotEntry + parseIdx) + 1);
					sprintf(globalLists[*globalCount].entries[*currentIdx].organism, "%s", uniprotEntry + parseIdx + 1);
					parseIdx = -1;
					break;
				}
			}

			// If underscore missing, we may be dealing with clustered ID with deleted representative
			if (parseIdx > -1) {
				globalLists[*globalCount].entries[*currentIdx].gene = malloc(strlen(uniprotEntry) + 1);
				sprintf(globalLists[*globalCount].entries[*currentIdx].gene, "%s", uniprotEntry);
				globalLists[*globalCount].entries[*currentIdx].organism = malloc(8);
				sprintf(globalLists[*globalCount].entries[*currentIdx].organism, "Unknown");
			}

			(*currentIdx)++;
		}
	}

	uniprotPriListCount++;
	uniprotSecListCount++;

	return uniprotPriListCount - 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r13d
movl %esi, %r14d
movq %rdi, %r12
movq 0x257d4(%rip), %rdi     # 0x30e58
movslq 0x257dd(%rip), %rsi     # 0x30e68
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x257b9(%rip)     # 0x30e58
movslq 0x257c2(%rip), %rcx     # 0x30e68
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x257a8(%rip), %rdi     # 0x30e60
movslq 0x257ad(%rip), %rsi     # 0x30e6c
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x2578d(%rip)     # 0x30e60
movslq 0x25792(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movl %r14d, %ecx
movq %rcx, 0x38(%rsp)
testl %r14d, %r14d
movq %r12, 0x10(%rsp)
jle 0xb7a5
movl %r13d, 0xc(%rsp)
movq 0x38(%r12), %rax
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r12, %rdi
movq %r15, %r13
shlq $0x5, %r13
cmpl $0x0, 0x10(%rax,%r13)
je 0xb789
cmpq $0x0, (%rax,%r13)
je 0xb787
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r15d, %esi
movl %r12d, %edx
callq 0x13a45
cmpl $0x1, %eax
je 0xb75f
testl %eax, %eax
jne 0xb775
movq 0x25715(%rip), %rax     # 0x30e58
movslq 0x2571e(%rip), %rcx     # 0x30e68
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
cmpl $0x1, %ebp
leal 0x1(%rbp), %eax
sbbl $-0x1, %ebx
movl %eax, %ebp
jmp 0xb775
movq 0x256fa(%rip), %rax     # 0x30e60
movslq 0x256ff(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
movq 0x10(%rsp), %rdi
incq %r12
movq 0x38(%rdi), %rax
cmpq %r12, (%rax,%r13)
ja 0xb728
incl %ebx
incq %r15
cmpq 0x38(%rsp), %r15
jne 0xb70d
movq 0x256c2(%rip), %rax     # 0x30e60
movl 0xc(%rsp), %r13d
jmp 0xb7a7
xorl %ebx, %ebx
testl %r14d, %r14d
setle %bpl
movq 0x256a3(%rip), %r15     # 0x30e58
movslq 0x256ac(%rip), %rcx     # 0x30e68
shlq $0x4, %rcx
movl %ebx, %edx
subl 0x8(%r15,%rcx), %edx
movl %edx, 0xc(%r15,%rcx)
movslq 0x25699(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
subl 0x8(%rax,%rcx), %ebx
movl %ebx, 0xc(%rax,%rcx)
movslq 0x25682(%rip), %r14     # 0x30e68
movq %r14, %rbx
shlq $0x4, %rbx
movslq 0x8(%r15,%rbx), %rdi
movl $0x28, %esi
callq 0x31b0
movq %rax, (%r15,%rbx)
movq 0x25659(%rip), %r15     # 0x30e60
movslq 0x2565e(%rip), %rbx     # 0x30e6c
movq %rbx, %r12
shlq $0x4, %r12
movslq 0x8(%r15,%r12), %rdi
movl $0x28, %esi
callq 0x31b0
movq %rax, (%r15,%r12)
testl %r13d, %r13d
sete %al
orb %bpl, %al
jne 0xbb39
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rsi
xorl %ebx, %ebx
xorl %ebp, %ebp
movl $0x0, 0x1c(%rsp)
movq %rbx, %r14
shlq $0x5, %r14
cmpl $0x0, 0x10(%rsi,%r14)
je 0xbb1e
cmpq $0x0, (%rsi,%r14)
je 0xbb1e
xorl %r15d, %r15d
movq %rbx, 0x48(%rsp)
movq %r14, 0x40(%rsp)
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x13a45
movq 0x10(%rsp), %rdi
movq 0x38(%rdi), %rsi
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0xbb11
leaq 0x255be(%rip), %rax     # 0x30e60
leaq 0x255af(%rip), %rcx     # 0x30e58
cmoveq %rcx, %rax
leaq 0x255b8(%rip), %rdx     # 0x30e6c
leaq 0x255ad(%rip), %rcx     # 0x30e68
cmoveq %rcx, %rdx
movq %rdx, 0x20(%rsp)
movq 0x18(%rsi,%r14), %rcx
leaq (%r15,%r15,2), %rdx
shlq $0x5, %rdx
movq %rdx, 0x58(%rsp)
movslq 0x18(%rcx,%rdx), %rcx
movq (%rdi), %rdx
movq 0x10(%rdi), %rsi
movq 0x10(%rsi), %rsi
leaq (%rcx,%rcx,4), %rcx
movq 0x18(%rsi,%rcx,8), %r14
cmpl $0x0, 0x50(%rdx)
je 0xb912
movl $0x2, %ecx
movzbl (%r14), %edx
cmpl $0x3a, %edx
je 0xb909
testl %edx, %edx
jne 0xb90b
jmp 0xb912
decl %ecx
incq %r14
testl %ecx, %ecx
jg 0xb8fa
movq (%rax), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movslq %eax, %rcx
movzbl (%r14,%rcx), %edx
cmpl $0x7c, %edx
je 0xb934
testl %edx, %edx
je 0xb940
cmpl $0x20, %edx
jne 0xb93c
jmp 0xb940
addq %rcx, %r14
incq %r14
xorl %eax, %eax
incl %eax
jmp 0xb91c
movq %r15, 0x60(%rsp)
movb $0x0, (%r14,%rcx)
movq %r14, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x20(%rsp), %r15
movslq (%r15), %r13
shlq $0x4, %r13
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %r12
movq (%r12,%r13), %rcx
movl %ebp, 0x34(%rsp)
movl %ebp, %ebx
cmovel 0x1c(%rsp), %ebx
movslq %ebx, %rdx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rbp
movq %rax, (%rcx,%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x32c0
movq (%r12,%r13), %rax
movl $0x1, 0x18(%rax,%rbp)
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x50(%rsp)
movq 0x18(%rcx,%rax), %rax
movq 0x58(%rsp), %rcx
movl 0x4c(%rax,%rcx), %eax
movslq (%r15), %rcx
shlq $0x4, %rcx
movq (%r12,%rcx), %rcx
movl %eax, 0x1c(%rcx,%rbp)
movslq (%r15), %rcx
shlq $0x4, %rcx
movq (%r12,%rcx), %rcx
cmpl 0x20(%rcx,%rbp), %eax
jle 0xb9f0
addq %rbp, %rcx
addq $0x20, %rcx
movl %eax, (%rcx)
movq %r14, %rdi
callq 0x3100
testq %rax, %rax
je 0xba15
movq %r14, %r13
xorl %r15d, %r15d
cmpb $0x5f, (%r13)
je 0xba72
incq %r15
incq %r13
cmpq %r15, %rax
jne 0xba03
movq %r14, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x20(%rsp), %rcx
movslq (%rcx), %r15
shlq $0x4, %r15
movq 0x28(%rsp), %r12
movq (%r12,%r15), %rcx
movq %rax, 0x8(%rcx,%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x32c0
movl $0x8, %edi
callq 0x3470
movq (%r12,%r15), %rcx
movq %rax, 0x10(%rcx,%rbp)
movabsq $0x6e776f6e6b6e55, %rcx # imm = 0x6E776F6E6B6E55
movq %rcx, (%rax)
movq 0x50(%rsp), %rsi
jmp 0xbae9
leal 0x1(%r15), %edi
callq 0x3470
movq 0x20(%rsp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
movq 0x28(%rsp), %r12
movq (%r12,%rcx), %rcx
movq %rax, 0x8(%rcx,%rbp)
movq %rax, %rdi
leaq 0x1be46(%rip), %rsi     # 0x278e5
movl %r15d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3070
movq %r13, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x20(%rsp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
movq (%r12,%rcx), %rcx
movq %rax, 0x10(%rcx,%rbp)
incq %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x32c0
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rsi
incl %ebx
cmpl $0x0, 0xc(%rsp)
movl 0x1c(%rsp), %eax
cmovel %ebx, %eax
movl %eax, 0x1c(%rsp)
movl 0x34(%rsp), %ebp
cmovnel %ebx, %ebp
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r14
movq 0x60(%rsp), %r15
incq %r15
cmpq %r15, (%rsi,%r14)
ja 0xb877
incq %rbx
cmpq 0x38(%rsp), %rbx
jb 0xb84c
movl 0x25335(%rip), %r14d    # 0x30e68
movl 0x25333(%rip), %ebx     # 0x30e6c
leal 0x1(%r14), %eax
movl %eax, 0x25325(%rip)     # 0x30e68
incl %ebx
movl %ebx, 0x25321(%rip)     # 0x30e6c
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq