// File: /workspace/github_cpp_makefile_star2_O1/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 0x11457
leaq 0x10a68(%rip), %r13     # 0x21d4b
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 0x11441
addq %rax, %r14
movq 0x28(%r14), %r12
movq $0x0, 0x28(%r14)
testq %r12, %r12
je 0x11441
addq $0x28, %r14
movq %r12, %rbx
movq 0x10(%rbx), %r12
cmpl $-0x1, 0x8(%rbx)
je 0x11362
movq %r13, %rdi
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq (%r14), %rsi
testq %rsi, %rsi
sete %al
je 0x113da
movl (%rbx), %edx
movl (%rsi), %edi
movq %r14, %rbp
movq %rsi, %rcx
cmpl %edx, %edi
jg 0x113a4
cmpl %edx, %edi
jne 0x11387
movb 0x4(%rsi), %cl
cmpb 0x4(%rbx), %cl
jae 0x113a1
movq 0x10(%rsi), %rcx
movq %rsi, %rbp
addq $0x10, %rbp
testq %rcx, %rcx
sete %al
je 0x113dd
movl (%rcx), %edi
movq %rcx, %rsi
jmp 0x11377
movq %rsi, %rcx
testb $0x1, %al
jne 0x113dd
movl (%rcx), %eax
cmpl (%rbx), %eax
jne 0x113dd
movb 0x4(%rcx), %al
cmpb 0x4(%rbx), %al
jne 0x113dd
movl 0xc(%rcx), %eax
movl 0xc(%rbx), %edx
cmpl %edx, %eax
cmovll %eax, %edx
movl %edx, 0xc(%rcx)
movq %rbx, %rdi
callq 0x4380
movq %r12, %rbx
testq %r12, %r12
jne 0x11345
jmp 0x11441
movq %r14, %rbp
movl 0xc(%rbx), %eax
decl %eax
cmpl $0x4, %eax
ja 0x11415
leaq 0x100da(%rip), %rcx     # 0x214c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
incl 0x10(%rsp)
jmp 0x1142c
incl 0xc(%rsp)
jmp 0x1142c
incl 0x18(%rsp)
jmp 0x1142c
incl 0x1c(%rsp)
jmp 0x1142c
incl 0x14(%rsp)
jmp 0x1142c
leaq 0x10963(%rip), %rdi     # 0x21d7f
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq (%rbp), %rax
movq %rax, 0x10(%rbx)
movq %rbx, (%rbp)
testq %r12, %r12
jne 0x11342
incq %r15
movq 0x20(%rsp), %rdi
movslq (%rdi), %rax
cmpq %rax, %r15
jl 0x11313
jmp 0x1147f
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 0x10911(%rip), %rsi     # 0x21d9e
movl $0x3, %edi
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %r9d
xorl %eax, %eax
callq 0xa496
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq