// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rdi, 0x10(%rsp)
testl %edx, %edx
leaq 0x254fe(%rip), %rax     # 0x30e60
leaq 0x254ef(%rip), %rcx     # 0x30e58
cmoveq %rax, %rcx
movq (%rcx), %r15
cmpl $0x2, %esi
ja 0xb99d
movl %esi, %eax
leaq 0x24402(%rip), %rcx     # 0x2fd80
movq (%rcx,%rax,8), %rcx
shll $0x3, %esi
movq %rsi, 0x8(%rsp)
movq (%r15), %rdi
movslq 0x8(%r15), %rsi
movl $0x28, %edx
callq 0x3760
jmp 0xb9a6
movq $0x0, 0x8(%rsp)
movslq 0x8(%r15), %rdi
movl $0x28, %esi
callq 0x31b0
movq 0x10(%rsp), %r8
movq %rax, (%r8)
movq (%r15), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, 0x8(%r8)
cmpl $0x0, 0x8(%r15)
movq 0x8(%rsp), %rdi
jle 0xba83
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%r8), %r14
movslq 0x8(%r8), %r12
leaq (%r12,%r12,4), %rax
leaq (%r14,%rax,8), %rax
movq %r15, %rbx
movq (%r15), %rcx
addq %rdi, %rcx
movq (%r13,%rcx), %r15
movq (%rdi,%rax), %rsi
movq %r15, %rdi
callq 0x34d0
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
testl %eax, %eax
je 0xba3c
incq %r12
movl %r12d, 0x8(%r8)
leaq (%r12,%r12,4), %rax
leaq (%r14,%rax,8), %rax
movq %r15, (%rdi,%rax)
movq (%r8), %rax
movslq 0x8(%r8), %rcx
leaq (%rcx,%rcx,4), %rcx
incl 0x18(%rax,%rcx,8)
movq %rbx, %r15
movq (%rbx), %rdx
movl 0x1c(%rdx,%r13), %esi
addl %esi, 0x1c(%rax,%rcx,8)
movl 0x20(%rdx,%r13), %edx
cmpl 0x20(%rax,%rcx,8), %edx
jle 0xba6f
leaq (%rax,%rcx,8), %rax
addq $0x20, %rax
movl %edx, (%rax)
incq %rbp
movslq 0x8(%r15), %rax
addq $0x28, %r13
cmpq %rax, %rbp
jl 0xb9f2
incl 0x8(%r8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq