// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: SortPartitions(bbrinkinfo_s*)
void SortPartitions (bbrinkinfo_t *info) // to merge same partition planes and compress clipnodes better if using HLBSP_MERGECLIPNODE
{
	int countfloorblocking = 0;
	int countfloor = 0;
	int countwallblocking = 0;
	int countwall = 0;
	int countany = 0;
	for (int i = 0; i < info->numclipnodes; i++)
	{
		bclipnode_t *clipnode = &info->clipnodes[i];
		if (!clipnode->isleaf)
		{
			continue;
		}
		bpartition_t *current, **pp, *partitions;
		partitions = clipnode->partitions;
		clipnode->partitions = NULL;
		while ((current = partitions) != NULL)
		{
			partitions = current->next;
			if (current->content != CONTENTS_EMPTY)
			{
				PrintOnce ("SortPartitions: content of partition was not empty.");
				hlassume (false, assume_first);
			}
			for (pp = &clipnode->partitions; *pp; pp = &(*pp)->next)
			{
				if ((*pp)->planenum > current->planenum ||
					(*pp)->planenum == current->planenum && (*pp)->planeside >= current->planeside) // normally the planeside should be identical
				{
					break;
				}
			}
			if (*pp && (*pp)->planenum == current->planenum && (*pp)->planeside == current->planeside)
			{
				(*pp)->type = qmin ((*pp)->type, current->type); // pick the lowest (most important) level from the existing partition and the current partition
				free (current);
				continue;
			}
			switch (current->type)
			{
			case BrinkFloorBlocking:
				countfloorblocking++;
				break;
			case BrinkFloor:
				countfloor++;
				break;
			case BrinkWallBlocking:
				countwallblocking++;
				break;
			case BrinkWall:
				countwall++;
				break;
			case BrinkAny:
				countany++;
				break;
			default:
				PrintOnce ("SortPartitions: internal error");
				hlassume (false, assume_first);
				break;
			}
			current->next = *pp;
			*pp = current;
		}
	}
	Developer (DEVELOPER_LEVEL_MESSAGE, "partitions: floorblocking = %d floor = %d wallblocking = %d wall = %d any = %d\n", countfloorblocking, countfloor, countwallblocking, countwall, countany);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0xf0d0(%rip), %rbx      # 0x1e420
leaq 0xf94c(%rip), %r13      # 0x1eca3
xorl %edx, %edx
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movq %rdi, 0x28(%rsp)
movslq (%rdi), %rax
cmpq %rax, %rdx
jge 0xf49f
movq 0x8(%rdi), %rax
imulq $0x38, %rdx, %rcx
cmpb $0x1, (%rax,%rcx)
jne 0xf497
movq %rdx, 0x30(%rsp)
addq %rcx, %rax
leaq 0x28(%rax), %r14
movq 0x28(%rax), %rbp
andq $0x0, 0x28(%rax)
movq %rbp, %r15
testq %rbp, %rbp
je 0xf48d
movq 0x10(%r15), %rbp
cmpl $-0x1, 0x8(%r15)
je 0xf3e3
movq %r13, %rdi
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
movq %r14, %r12
movq (%r12), %rax
testq %rax, %rax
je 0xf433
movl (%rax), %ecx
movl (%r15), %edx
cmpl %edx, %ecx
jg 0xf40d
jne 0xf404
movb 0x4(%rax), %sil
cmpb 0x4(%r15), %sil
jae 0xf40d
addq $0x10, %rax
movq %rax, %r12
jmp 0xf3e6
cmpl %edx, %ecx
jne 0xf433
movb 0x4(%rax), %cl
cmpb 0x4(%r15), %cl
jne 0xf433
movl 0xc(%rax), %ecx
movl 0xc(%r15), %edx
cmpl %edx, %ecx
cmovll %ecx, %edx
movl %edx, 0xc(%rax)
movq %r15, %rdi
callq 0x4390
jmp 0xf3b9
movl 0xc(%r15), %ecx
decl %ecx
cmpl $0x4, %ecx
ja 0xf465
movslq (%rbx,%rcx,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
incl 0x18(%rsp)
jmp 0xf480
incl 0x14(%rsp)
jmp 0xf480
incl 0x24(%rsp)
jmp 0xf480
incl 0x20(%rsp)
jmp 0xf480
incl 0x1c(%rsp)
jmp 0xf480
leaq 0xf86b(%rip), %rdi      # 0x1ecd7
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
movq (%r12), %rax
movq %rax, 0x10(%r15)
movq %r15, (%r12)
jmp 0xf3b9
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rdx
incq %rdx
jmp 0xf386
movl 0x14(%rsp), %eax
movl %eax, (%rsp)
leaq 0xf849(%rip), %rsi      # 0x1ecf6
pushq $0x3
popq %rdi
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r9d
xorl %eax, %eax
callq 0x9035
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq