// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/uniprot.c
// Function: aggregateUniprotList
void aggregateUniprotList(UniprotList * retList, int passListType, int passPrimary) {
	UniprotList * globalLists;
	int entryIdx;
	int memberOffset;
	char * srcBase, * dstBase;

	globalLists = getGlobalLists(passPrimary);

	// First sort full list
	switch (passListType) {
	case UNIPROT_LIST_FULL:
		qsort(globalLists[0].entries, globalLists[0].entryCount, sizeof(UniprotEntry), uniprotEntryCompareID);
		memberOffset = offsetof(UniprotEntry, id);
		break;
	case UNIPROT_LIST_GENES:
		qsort(globalLists[0].entries, globalLists[0].entryCount, sizeof(UniprotEntry), uniprotEntryCompareGene);
		memberOffset = offsetof(UniprotEntry, gene);
		break;
	case UNIPROT_LIST_ORGANISM:
		qsort(globalLists[0].entries, globalLists[0].entryCount, sizeof(UniprotEntry), uniprotEntryCompareOrganism);
		memberOffset = offsetof(UniprotEntry, organism);
		break;
	default: memberOffset = 0; break;
	}

	// Count entry occurrences and aggregate into return list
	retList->entries = calloc(globalLists[0].entryCount, sizeof(UniprotEntry));
	retList->entries[0].id = globalLists[0].entries[0].id;
	retList->entries[0].gene = globalLists[0].entries[0].gene;
	retList->entries[0].organism = globalLists[0].entries[0].organism;
	retList->entryCount = 0;

	for (entryIdx = 0 ; entryIdx < globalLists[0].entryCount ; entryIdx++) {
		srcBase = (char *) (globalLists[0].entries + entryIdx);
		dstBase = (char *) (retList->entries + retList->entryCount);

		// If current global entry doesn't match local entry, create new local entry and copy pointer to relevant string info
		if (strcmp(*((char * *) (srcBase + memberOffset)), *((char * *) (dstBase + memberOffset))) != 0) {
			dstBase = (char *) (retList->entries + ++retList->entryCount);
			*((char * *) (dstBase + memberOffset)) = *((char * *) (srcBase + memberOffset));
		}

		retList->entries[retList->entryCount].numOccurrence++;
		retList->entries[retList->entryCount].totalQuality += (globalLists[0].entries + entryIdx)->totalQuality;
        if ((globalLists[0].entries + entryIdx)->maxQuality > retList->entries[retList->entryCount].maxQuality) {
            retList->entries[retList->entryCount].maxQuality = (globalLists[0].entries + entryIdx)->maxQuality;
        }
	}

	retList->entryCount++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testl %edx, %edx
leaq 0x22aef(%rip), %rax     # 0x2ce60
leaq 0x22ae0(%rip), %rcx     # 0x2ce58
cmoveq %rax, %rcx
movq %rdi, 0x10(%rsp)
movq (%rcx), %rdx
cmpl $0x2, %esi
movq %rdx, 0x8(%rsp)
ja 0xa3b6
movl %esi, %ebx
movl %esi, %eax
leaq 0x219e7(%rip), %rcx     # 0x2bd80
movq (%rcx,%rax,8), %rcx
shll $0x3, %ebx
movq (%rdx), %rdi
movslq 0x8(%rdx), %rsi
pushq $0x28
popq %rdx
callq 0x3760
movq 0x8(%rsp), %rdx
jmp 0xa3b8
xorl %ebx, %ebx
movslq 0x8(%rdx), %rdi
pushq $0x28
popq %rsi
callq 0x31b0
movq 0x8(%rsp), %rdi
movq %rax, %r15
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq (%rdi), %r8
movdqu (%r8), %xmm0
movdqu %xmm0, (%rax)
movq 0x10(%r8), %rax
movq %rax, 0x10(%r15)
andl $0x0, 0x8(%rcx)
xorl %r14d, %r14d
xorl %ecx, %ecx
xorl %ebp, %ebp
movslq 0x8(%rdi), %rax
cmpq %rax, %rcx
jge 0xa4aa
movq %rcx, 0x20(%rsp)
movslq %ebp, %r13
imulq $0x28, %r13, %rax
addq %r15, %rax
leaq (%r8,%rbx), %rcx
movq (%r14,%rcx), %r12
movq (%rbx,%rax), %rsi
movq %r12, %rdi
movq %r8, 0x18(%rsp)
callq 0x34d0
testl %eax, %eax
je 0xa451
incl %ebp
movq 0x10(%rsp), %rcx
movl %ebp, 0x8(%rcx)
movslq %ebp, %r13
imulq $0x28, %r13, %rax
addq %rax, %r15
movq %r12, (%rbx,%r15)
movq (%rcx), %r15
movq 0x8(%rsp), %rdi
movq (%rdi), %r8
jmp 0xa45b
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
imulq $0x28, %r13, %rax
movq 0x18(%r15,%rax), %xmm0
movd 0x1c(%r8,%r14), %xmm1
movdqa 0x17e2b(%rip), %xmm2    # 0x222a0
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddd %xmm0, %xmm2
movq %xmm2, 0x18(%r15,%rax)
movl 0x20(%r8,%r14), %ecx
cmpl 0x20(%r15,%rax), %ecx
jle 0xa499
addq %r15, %rax
addq $0x20, %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rcx
incq %rcx
addq $0x28, %r14
jmp 0xa3f3
incl %ebp
movq 0x10(%rsp), %rax
movl %ebp, 0x8(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq