// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r12
movq 0x22fc2(%rip), %rdi     # 0x2ce58
movslq 0x22fcb(%rip), %rsi     # 0x2ce68
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x22fa7(%rip)     # 0x2ce58
movslq 0x22fb0(%rip), %rcx     # 0x2ce68
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x22f96(%rip), %rdi     # 0x2ce60
movslq 0x22f9b(%rip), %rsi     # 0x2ce6c
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x22f7b(%rip)     # 0x2ce60
movslq 0x22f80(%rip), %rcx     # 0x2ce6c
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
movq %r14, 0x38(%rsp)
xorl %ebp, %ebp
movq %r12, 0x8(%rsp)
cmpq 0x38(%rsp), %r15
je 0x9faa
movq 0x38(%r12), %rax
movq %r15, %r14
shlq $0x5, %r14
cmpl $0x0, 0x10(%rax,%r14)
je 0x9fa2
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq %r12, (%rax,%r14)
jbe 0x9f9b
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x11a29
cmpl $0x1, %eax
je 0x9f77
testl %eax, %eax
jne 0x9f8d
movq 0x22f00(%rip), %rax     # 0x2ce58
movslq 0x22f09(%rip), %rcx     # 0x2ce68
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
cmpl $0x1, %r13d
leal 0x1(%r13), %eax
sbbl $-0x1, %ebp
movl %eax, %r13d
jmp 0x9f8d
movq 0x22ee2(%rip), %rax     # 0x2ce60
movslq 0x22ee7(%rip), %rcx     # 0x2ce6c
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
incq %r12
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rax
jmp 0x9f32
incl %ebp
movq 0x8(%rsp), %r12
incq %r15
jmp 0x9f0d
movq 0x22ea7(%rip), %r14     # 0x2ce58
movslq 0x22eb0(%rip), %rax     # 0x2ce68
shlq $0x4, %rax
movl %ebp, %ecx
subl 0x8(%r14,%rax), %ecx
movl %ecx, 0xc(%r14,%rax)
movq 0x22e91(%rip), %rax     # 0x2ce60
movslq 0x22e96(%rip), %rcx     # 0x2ce6c
shlq $0x4, %rcx
subl 0x8(%rax,%rcx), %ebp
movl %ebp, 0xc(%rax,%rcx)
movslq 0x22e7f(%rip), %r15     # 0x2ce68
movq %r15, %r13
shlq $0x4, %r13
movslq 0x8(%r14,%r13), %rdi
pushq $0x28
popq %r12
movq %r12, %rsi
callq 0x31b0
movq %rax, (%r14,%r13)
movq 0x22e54(%rip), %r13     # 0x2ce60
movslq 0x22e59(%rip), %r14     # 0x2ce6c
movq %r14, %rbp
shlq $0x4, %rbp
movslq 0x8(%r13,%rbp), %rdi
movq %r12, %rsi
callq 0x31b0
movq %rax, (%r13,%rbp)
testl %ebx, %ebx
je 0xa334
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
cmpq 0x38(%rsp), %r14
je 0xa326
movq 0x38(%r12), %rbx
movq %r14, %rax
shlq $0x5, %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x10(%rbx,%rax)
je 0xa31e
xorl %r15d, %r15d
movq %r14, 0x40(%rsp)
movq 0x28(%rsp), %rax
cmpq %r15, (%rbx,%rax)
jbe 0xa31e
movq %r12, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x11a29
movq 0x38(%r12), %rbx
movl %eax, 0x14(%rsp)
testl %eax, %eax
js 0xa316
leaq 0x22db6(%rip), %rax     # 0x2ce60
leaq 0x22da7(%rip), %rcx     # 0x2ce58
cmoveq %rcx, %rax
leaq 0x22db0(%rip), %rdx     # 0x2ce6c
leaq 0x22da5(%rip), %rcx     # 0x2ce68
cmoveq %rcx, %rdx
movq %rdx, 0x20(%rsp)
movq 0x28(%rsp), %rcx
movq 0x18(%rbx,%rcx), %rcx
movq %r15, 0x50(%rsp)
imulq $0x60, %r15, %rdx
movq %rdx, 0x48(%rsp)
movslq 0x18(%rcx,%rdx), %rcx
movq (%r12), %rdx
movq 0x10(%r12), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rcx, %rcx
movq 0x18(%rsi,%rcx), %r15
cmpl $0x0, 0x50(%rdx)
je 0xa122
pushq $0x2
popq %rcx
testl %ecx, %ecx
jle 0xa122
movzbl (%r15), %edx
cmpl $0x3a, %edx
je 0xa11b
testl %edx, %edx
jne 0xa11d
jmp 0xa122
decl %ecx
incq %r15
jmp 0xa108
movq (%rax), %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
movslq %eax, %rcx
movzbl (%r15,%rcx), %edx
cmpl $0x7c, %edx
je 0xa144
testl %edx, %edx
je 0xa150
cmpl $0x20, %edx
jne 0xa14c
jmp 0xa150
addq %rcx, %r15
incq %r15
xorl %eax, %eax
incl %eax
jmp 0xa12c
movb $0x0, (%r15,%rcx)
movq %r15, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x20(%rsp), %r12
movslq (%r12), %rbx
shlq $0x4, %rbx
cmpl $0x0, 0x14(%rsp)
movl %ebp, 0x34(%rsp)
cmovel 0x10(%rsp), %ebp
movq 0x18(%rsp), %r13
movq (%r13,%rbx), %rcx
movslq %ebp, %rdx
imulq $0x28, %rdx, %r14
movq %rax, (%rcx,%r14)
movq %rax, %rdi
movq %r15, %rsi
callq 0x32c0
movq (%r13,%rbx), %rax
movl $0x1, 0x18(%rax,%r14)
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rbx
movq 0x28(%rsp), %rax
movq 0x18(%rbx,%rax), %rax
movq 0x48(%rsp), %rcx
movl 0x4c(%rax,%rcx), %eax
movslq (%r12), %rcx
shlq $0x4, %rcx
movq (%r13,%rcx), %rcx
movl %eax, 0x1c(%rcx,%r14)
movslq (%r12), %rcx
shlq $0x4, %rcx
movq (%r13,%rcx), %rcx
cmpl 0x20(%rcx,%r14), %eax
jle 0xa1fa
addq %r14, %rcx
addq $0x20, %rcx
movl %eax, (%rcx)
movq %r15, %rdi
callq 0x3100
movq %r15, %r12
xorl %r13d, %r13d
cmpq %r13, %rax
je 0xa21c
cmpb $0x5f, (%r12)
je 0xa279
incq %r13
incq %r12
jmp 0xa208
movq %r15, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x20(%rsp), %rcx
movslq (%rcx), %r12
shlq $0x4, %r12
movq 0x18(%rsp), %r13
movq (%r13,%r12), %rcx
movq %rax, 0x8(%rcx,%r14)
movq %rax, %rdi
movq %r15, %rsi
callq 0x32c0
pushq $0x8
popq %rdi
callq 0x3470
movq (%r13,%r12), %rcx
movq %rax, 0x10(%rcx,%r14)
movabsq $0x6e776f6e6b6e55, %rcx # imm = 0x6E776F6E6B6E55
movq %rcx, (%rax)
movq 0x8(%rsp), %r12
jmp 0xa2f1
leal 0x1(%r13), %edi
callq 0x3470
movq 0x20(%rsp), %rbx
movslq (%rbx), %rcx
shlq $0x4, %rcx
movq 0x18(%rsp), %rdx
movq (%rdx,%rcx), %rcx
movq %rax, 0x8(%rcx,%r14)
movq %rax, %rdi
leaq 0x185ef(%rip), %rsi     # 0x22895
movl %r13d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x3070
movq %r12, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movslq (%rbx), %rcx
shlq $0x4, %rcx
movq 0x18(%rsp), %rdx
movq (%rdx,%rcx), %rcx
movq %rax, 0x10(%rcx,%r14)
incq %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x32c0
movq 0x8(%rsp), %r12
movq 0x38(%r12), %rbx
incl %ebp
cmpl $0x0, 0x14(%rsp)
movl 0x10(%rsp), %eax
cmovel %ebp, %eax
movl %eax, 0x10(%rsp)
movl 0x34(%rsp), %eax
cmovnel %ebp, %eax
movl %eax, %ebp
movq 0x40(%rsp), %r14
movq 0x50(%rsp), %r15
incq %r15
jmp 0xa075
incq %r14
jmp 0xa046
movl 0x22b3b(%rip), %r15d    # 0x2ce68
movl 0x22b38(%rip), %r14d    # 0x2ce6c
leal 0x1(%r15), %eax
movl %eax, 0x22b2a(%rip)     # 0x2ce68
incl %r14d
movl %r14d, 0x22b24(%rip)    # 0x2ce6c
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq