// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x18fc6
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x38(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
testb $0x1, (%rax)
jne 0x18da3
jmp 0x18fb6
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq $0x0, 0x28(%rax)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x18fb4
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
cmpl $-0x1, 0x8(%rax)
je 0x18e02
leaq 0x210f5(%rip), %rdi     # 0x39ee7
movb $0x0, %al
callq 0xb840
xorl %esi, %esi
movl %esi, %edi
callq 0xbe90
movq 0x30(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
je 0x18e7a
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq 0x28(%rsp), %rcx
cmpl (%rcx), %eax
jg 0x18e63
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq 0x28(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x18e65
movq 0x20(%rsp), %rax
movq (%rax), %rax
movb 0x4(%rax), %al
andb $0x1, %al
movzbl %al, %eax
movq 0x28(%rsp), %rcx
movb 0x4(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jl 0x18e65
jmp 0x18e7a
jmp 0x18e67
movq 0x20(%rsp), %rax
movq (%rax), %rax
addq $0x10, %rax
movq %rax, 0x20(%rsp)
jmp 0x18e10
movq 0x20(%rsp), %rax
cmpq $0x0, (%rax)
je 0x18f0e
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq 0x28(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x18f0e
movq 0x20(%rsp), %rax
movq (%rax), %rax
movb 0x4(%rax), %al
andb $0x1, %al
movzbl %al, %eax
movq 0x28(%rsp), %rcx
movb 0x4(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x18f0e
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x18ee4
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
jmp 0x18ef0
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %ecx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0xc(%rax)
movq 0x28(%rsp), %rdi
callq 0x4370
jmp 0x18dbe
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0x4, %eax
ja 0x18f7a
movq 0x8(%rsp), %rax
leaq 0x20710(%rip), %rcx     # 0x39640
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x18f91
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x18f91
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x18f91
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x18f91
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x18f91
leaq 0x20f9a(%rip), %rdi     # 0x39f1b
movb $0x0, %al
callq 0xb840
xorl %esi, %esi
movl %esi, %edi
callq 0xbe90
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, (%rax)
jmp 0x18dbe
jmp 0x18fb6
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x18d69
movl 0x4c(%rsp), %edx
movl 0x48(%rsp), %ecx
movl 0x44(%rsp), %r8d
movl 0x40(%rsp), %r9d
movl 0x3c(%rsp), %eax
movl $0x3, %edi
leaq 0x20f52(%rip), %rsi     # 0x39f3a
movl %eax, (%rsp)
movb $0x0, %al
callq 0xbb80
addq $0x58, %rsp
retq
nopw (%rax,%rax)