// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movl %esi, %ebx
movq %rdi, 0x8(%rsp)
movq 0x259f6(%rip), %rdi     # 0x30e58
movslq 0x259ff(%rip), %rsi     # 0x30e68
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x259db(%rip)     # 0x30e58
movslq 0x259e4(%rip), %rcx     # 0x30e68
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x259ca(%rip), %rdi     # 0x30e60
movslq 0x259cf(%rip), %rsi     # 0x30e6c
shlq $0x4, %rsi
addq $0x10, %rsi
callq 0x3610
movq %rax, 0x259af(%rip)     # 0x30e60
movslq 0x259b4(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movl %ebx, %eax
movq %rax, 0x40(%rsp)
testl %ebx, %ebx
jle 0xb579
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x8(%rsp), %rcx
movq 0x38(%rcx), %rax
movq %r15, %r14
shlq $0x5, %r14
cmpl $0x0, 0x10(%rax,%r14)
je 0xb569
movq 0x38(%rcx), %rax
cmpq $0x0, (%rax,%r14)
je 0xb567
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x136de
cmpl $0x1, %eax
je 0xb53f
testl %eax, %eax
jne 0xb555
movq 0x25938(%rip), %rax     # 0x30e58
movslq 0x25941(%rip), %rcx     # 0x30e68
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
cmpl $0x1, %r13d
leal 0x1(%r13), %eax
sbbl $-0x1, %ebp
movl %eax, %r13d
jmp 0xb555
movq 0x2591a(%rip), %rax     # 0x30e60
movslq 0x2591f(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
incl 0x8(%rax,%rcx)
incq %r12
movq 0x8(%rsp), %rdi
movq 0x38(%rdi), %rax
cmpq %r12, (%rax,%r14)
ja 0xb505
incl %ebp
incq %r15
cmpq 0x40(%rsp), %r15
jne 0xb4d7
jmp 0xb57b
xorl %ebp, %ebp
movq 0x258d6(%rip), %r14     # 0x30e58
movslq 0x258df(%rip), %rax     # 0x30e68
shlq $0x4, %rax
movl %ebp, %ecx
subl 0x8(%r14,%rax), %ecx
movl %ecx, 0xc(%r14,%rax)
movq 0x258c0(%rip), %rax     # 0x30e60
movslq 0x258c5(%rip), %rcx     # 0x30e6c
shlq $0x4, %rcx
subl 0x8(%rax,%rcx), %ebp
movl %ebp, 0xc(%rax,%rcx)
movslq 0x258ae(%rip), %r15     # 0x30e68
shlq $0x4, %r15
movslq 0x8(%r14,%r15), %rdi
movl $0x28, %esi
callq 0x31b0
movq %rax, (%r14,%r15)
movq 0x25888(%rip), %r14     # 0x30e60
movslq 0x2588d(%rip), %r15     # 0x30e6c
shlq $0x4, %r15
movslq 0x8(%r14,%r15), %rdi
movl $0x28, %esi
callq 0x31b0
movq %rax, (%r14,%r15)
cmpl $0x0, 0x10(%rsp)
sete %al
testl %ebx, %ebx
setle %cl
orb %al, %cl
movq 0x8(%rsp), %rbp
jne 0xb922
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x38(%rbp), %rax
movq %rbx, %r15
shlq $0x5, %r15
cmpl $0x0, 0x10(%rax,%r15)
je 0xb90d
movq 0x38(%rbp), %rax
cmpq $0x0, (%rax,%r15)
je 0xb90d
xorl %r13d, %r13d
movq %rbx, 0x50(%rsp)
movq %r15, 0x48(%rsp)
movq %rbp, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x136de
movl %eax, 0x14(%rsp)
testl %eax, %eax
js 0xb8fc
leaq 0x257f5(%rip), %rax     # 0x30e60
leaq 0x257e6(%rip), %rcx     # 0x30e58
cmoveq %rcx, %rax
leaq 0x257ef(%rip), %rdx     # 0x30e6c
leaq 0x257e4(%rip), %rcx     # 0x30e68
cmoveq %rcx, %rdx
movq %rdx, 0x18(%rsp)
movq 0x38(%rbp), %rcx
movq 0x18(%rcx,%r15), %rcx
leaq (,%r13,2), %rdx
addq %r13, %rdx
shlq $0x5, %rdx
movq %rdx, 0x20(%rsp)
movslq 0x18(%rcx,%rdx), %rcx
movq (%rbp), %rdx
movq 0x10(%rbp), %rsi
movq 0x10(%rsi), %rsi
leaq (%rcx,%rcx,4), %rcx
movq 0x18(%rsi,%rcx,8), %rbx
cmpl $0x0, 0x50(%rdx)
je 0xb6e6
movl $0x2, %ecx
movzbl (%rbx), %edx
cmpl $0x3a, %edx
je 0xb6dd
testl %edx, %edx
jne 0xb6df
jmp 0xb6e6
decl %ecx
incq %rbx
testl %ecx, %ecx
jg 0xb6cf
movq (%rax), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movslq %eax, %rcx
movzbl (%rbx,%rcx), %edx
cmpl $0x7c, %edx
je 0xb707
testl %edx, %edx
je 0xb713
cmpl $0x20, %edx
jne 0xb70f
jmp 0xb713
addq %rcx, %rbx
incq %rbx
xorl %eax, %eax
incl %eax
jmp 0xb6f0
movq %r13, 0x28(%rsp)
movb $0x0, (%rbx,%rcx)
movq %rbx, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x18(%rsp), %r15
movslq (%r15), %rbp
shlq $0x4, %rbp
cmpl $0x0, 0x14(%rsp)
movq 0x30(%rsp), %r13
movq (%r13,%rbp), %rcx
movl %r14d, 0x3c(%rsp)
movl %r12d, 0x38(%rsp)
cmovel %r12d, %r14d
movslq %r14d, %rdx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %r12
movq %rax, (%rcx,%r12)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x32c0
movq (%r13,%rbp), %rax
movq 0x8(%rsp), %rbp
movl $0x1, 0x18(%rax,%r12)
movq 0x38(%rbp), %rax
movq 0x48(%rsp), %rcx
movq 0x18(%rax,%rcx), %rax
movq 0x20(%rsp), %rcx
movl 0x4c(%rax,%rcx), %eax
movslq (%r15), %rcx
shlq $0x4, %rcx
movq (%r13,%rcx), %rcx
movl %eax, 0x1c(%rcx,%r12)
movslq (%r15), %rcx
shlq $0x4, %rcx
movq (%r13,%rcx), %rcx
cmpl 0x20(%rcx,%r12), %eax
jle 0xb7c7
addq %r12, %rcx
addq $0x20, %rcx
movl %eax, (%rcx)
movq %rbx, %rdi
callq 0x3100
testq %rax, %rax
movq 0x28(%rsp), %r13
je 0xb7f3
xorl %r15d, %r15d
cmpb $0x5f, (%rbx,%r15)
je 0xb855
incq %r15
cmpq %r15, %rax
jne 0xb7dc
testl %eax, %eax
js 0xb8d6
movq %rbx, %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x18(%rsp), %rcx
movslq (%rcx), %r15
shlq $0x4, %r15
movq 0x30(%rsp), %r13
movq (%r13,%r15), %rcx
movq %rax, 0x8(%rcx,%r12)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x32c0
movl $0x8, %edi
callq 0x3470
movq (%r13,%r15), %rcx
movq 0x28(%rsp), %r13
movq %rax, 0x10(%rcx,%r12)
movabsq $0x6e776f6e6b6e55, %rcx # imm = 0x6E776F6E6B6E55
movq %rcx, (%rax)
jmp 0xb8d6
leaq (%rbx,%r15), %rax
movq %rax, 0x20(%rsp)
leal 0x1(%r15), %edi
callq 0x3470
movq 0x18(%rsp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
movq 0x30(%rsp), %r13
movq (%r13,%rcx), %rcx
movq %rax, 0x8(%rcx,%r12)
movq %rax, %rdi
leaq 0x1bfd9(%rip), %rsi     # 0x27865
movl %r15d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3070
movq 0x20(%rsp), %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
callq 0x3470
movq 0x18(%rsp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
movq (%r13,%rcx), %rcx
movq 0x28(%rsp), %r13
movq %rax, 0x10(%rcx,%r12)
leaq (%rbx,%r15), %rsi
incq %rsi
movq %rax, %rdi
callq 0x32c0
incl %r14d
cmpl $0x0, 0x14(%rsp)
movl 0x38(%rsp), %r12d
cmovel %r14d, %r12d
movl 0x3c(%rsp), %eax
cmovnel %r14d, %eax
movl %eax, %r14d
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r15
incq %r13
movq 0x38(%rbp), %rax
cmpq %r13, (%rax,%r15)
ja 0xb64b
cmpl $0x0, 0x10(%rsp)
je 0xb922
incq %rbx
cmpq 0x40(%rsp), %rbx
jb 0xb618
movl 0x25540(%rip), %eax     # 0x30e68
leal 0x1(%rax), %ecx
movl %ecx, 0x25537(%rip)     # 0x30e68
incl 0x25535(%rip)           # 0x30e6c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq