// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
cmpl $0x0, (%rdi)
jle 0x1123a
leaq 0xfc7d(%rip), %r13      # 0x20d43
xorl %r15d, %r15d
movl $0x0, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %r14
imulq $0x38, %r15, %rax
cmpb $0x1, (%r14,%rax)
jne 0x11224
addq %rax, %r14
movq 0x28(%r14), %r12
movq $0x0, 0x28(%r14)
testq %r12, %r12
je 0x11224
addq $0x28, %r14
movq %r12, %rbx
movq 0x10(%rbx), %r12
cmpl $-0x1, 0x8(%rbx)
je 0x11145
movq %r13, %rdi
xorl %eax, %eax
callq 0xa10c
xorl %edi, %edi
xorl %esi, %esi
callq 0xa655
movq (%r14), %rsi
testq %rsi, %rsi
je 0x1121d
movl (%rbx), %ecx
movl (%rsi), %edx
movq %r14, %rbp
movq %rsi, %rax
cmpl %ecx, %edx
jg 0x11185
cmpl %ecx, %edx
jne 0x1116b
movb 0x4(%rsi), %al
cmpb 0x4(%rbx), %al
jae 0x11182
movq 0x10(%rsi), %rax
movq %rsi, %rbp
addq $0x10, %rbp
testq %rax, %rax
je 0x111b5
movl (%rax), %edx
movq %rax, %rsi
jmp 0x1115b
movq %rsi, %rax
cmpl %ecx, %edx
jne 0x111b7
movb 0x4(%rax), %cl
cmpb 0x4(%rbx), %cl
jne 0x111b7
movl 0xc(%rax), %ecx
movl 0xc(%rbx), %edx
cmpl %edx, %ecx
cmovll %ecx, %edx
movl %edx, 0xc(%rax)
movq %rbx, %rdi
callq 0x4380
movq %r12, %rbx
testq %r12, %r12
jne 0x11128
jmp 0x11224
xorl %eax, %eax
movl 0xc(%rbx), %ecx
decl %ecx
cmpl $0x4, %ecx
ja 0x111ef
leaq 0xf2f8(%rip), %rdx      # 0x204c0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
incl 0x10(%rsp)
jmp 0x1120a
incl 0xc(%rsp)
jmp 0x1120a
incl 0x18(%rsp)
jmp 0x1120a
incl 0x1c(%rsp)
jmp 0x1120a
incl 0x14(%rsp)
jmp 0x1120a
leaq 0xfb81(%rip), %rdi      # 0x20d77
xorl %eax, %eax
callq 0xa10c
xorl %edi, %edi
xorl %esi, %esi
callq 0xa655
movq (%rbp), %rax
movq %rax, 0x10(%rbx)
movq %rbx, (%rbp)
testq %r12, %r12
jne 0x11125
jmp 0x11224
xorl %eax, %eax
movq %r14, %rbp
jmp 0x111b7
incq %r15
movq 0x20(%rsp), %rdi
movslq (%rdi), %rax
cmpq %rax, %r15
jl 0x110f6
jmp 0x11262
movl $0x0, 0x18(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl 0xc(%rsp), %eax
movl %eax, (%rsp)
leaq 0xfb26(%rip), %rsi      # 0x20d96
movl $0x3, %edi
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %r9d
xorl %eax, %eax
callq 0xa3ae
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq