// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, 0x10(%rsp)
testl %edx, %edx
leaq 0x252e7(%rip), %rax     # 0x30e60
leaq 0x252d8(%rip), %rcx     # 0x30e58
cmoveq %rax, %rcx
movq (%rcx), %rdx
cmpl $0x2, %esi
movq %rdx, 0x8(%rsp)
ja 0xbbbb
movl %esi, %ebx
movl %esi, %eax
leaq 0x241e4(%rip), %rcx     # 0x2fd80
movq (%rcx,%rax,8), %rcx
shll $0x3, %ebx
movq (%rdx), %rdi
movslq 0x8(%rdx), %rsi
movl $0x28, %edx
callq 0x3760
movq 0x8(%rsp), %rdx
jmp 0xbbbd
xorl %ebx, %ebx
movslq 0x8(%rdx), %rdi
movl $0x28, %esi
callq 0x31b0
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq (%rdx), %r9
movdqu (%r9), %xmm0
movdqu %xmm0, (%rax)
movq 0x10(%r9), %rsi
movq %rsi, 0x10(%rax)
movl $0x0, 0x8(%rcx)
cmpl $0x0, 0x8(%rdx)
jle 0xbcde
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r9, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movslq %r15d, %rbp
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
leaq (%rax,%rcx,8), %rsi
leaq (%r9,%rbx), %rcx
movq (%r13,%rcx), %r12
movq (%rbx,%rsi), %rsi
movq %r12, %rdi
callq 0x34d0
testl %eax, %eax
je 0xbc6f
incl %r15d
movq 0x10(%rsp), %rcx
movl %r15d, 0x8(%rcx)
movslq %r15d, %rbp
leaq (,%rbp,4), %rax
addq %rbp, %rax
movq 0x18(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
movq %r12, (%rbx,%rax)
movq (%rcx), %rax
movq 0x8(%rsp), %rsi
movq (%rsi), %r9
jmp 0xbc7e
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %r9
leaq (,%rbp,4), %r8
addq %rbp, %r8
movq 0x18(%rax,%r8,8), %xmm0
movd 0x1c(%r9,%r13), %xmm1
movdqa 0x1b651(%rip), %xmm2    # 0x272f0
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddd %xmm0, %xmm2
movq %xmm2, 0x18(%rax,%r8,8)
movl 0x20(%r9,%r13), %ecx
cmpl 0x20(%rax,%r8,8), %ecx
jle 0xbcc5
leaq (%rax,%r8,8), %r8
addq $0x20, %r8
movl %ecx, (%r8)
incq %r14
movslq 0x8(%rsi), %rcx
addq $0x28, %r13
cmpq %rcx, %r14
jl 0xbc06
incl %r15d
jmp 0xbce4
movl $0x1, %r15d
movq 0x10(%rsp), %rax
movl %r15d, 0x8(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq