// File: FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: SortPatches()
static void     SortPatches()
{
#ifdef HLRAD_MORE_PATCHES
	// SortPatches is the ideal place to do this, because the address of the patches are going to be invalidated.
	patch_t *old_patches = g_patches;
	g_patches = (patch_t *)AllocBlock ((g_num_patches + 1) * sizeof (patch_t)); // allocate one extra slot considering how terribly the code were written
	memcpy (g_patches, old_patches, g_num_patches * sizeof (patch_t));
	FreeBlock (old_patches);
#endif
    qsort((void*)g_patches, (size_t) g_num_patches, sizeof(patch_t), patch_sorter);

    // Fixup g_face_patches & Fixup patch->next
    memset(g_face_patches, 0, sizeof(g_face_patches));
    {
        unsigned        x;
        patch_t*        patch = g_patches + 1;
        patch_t*        prev = g_patches;

#ifdef HLRAD_SortPatches_FIX
		g_face_patches[prev->faceNumber] = prev;
#else
        g_face_patches[0] = g_patches;
#endif

        for (x = 1; x < g_num_patches; x++, patch++)
        {
            if (patch->faceNumber != prev->faceNumber)
            {
                prev->next = NULL;
                g_face_patches[patch->faceNumber] = patch;
            }
            else
            {
                prev->next = patch;
            }
            prev = patch;
        }
    }
#ifdef HLRAD_ENTITYBOUNCE_FIX
	for (unsigned x = 0; x < g_num_patches; x++)
	{
		patch_t *patch = &g_patches[x];
		patch->leafnum = PointInLeaf (patch->origin) - g_dleafs;
	}
#endif
}

// Assembly:
subq $0x38, %rsp
movq 0x5398c65(%rip), %rax   # 0x53eda00
movq %rax, 0x30(%rsp)
movl 0x5418c62(%rip), %eax   # 0x546da08
addl $0x1, %eax
movl %eax, %eax
imulq $0x138, %rax, %rdi      # imm = 0x138
callq 0x47a0
movq %rax, 0x5398c42(%rip)   # 0x53eda00
movq 0x5398c3b(%rip), %rdi   # 0x53eda00
movq 0x30(%rsp), %rsi
movl 0x5418c38(%rip), %eax   # 0x546da08
imulq $0x138, %rax, %rdx      # imm = 0x138
callq 0x4280
movq 0x30(%rsp), %rdi
callq 0x47d0
movq 0x5398c13(%rip), %rdi   # 0x53eda00
movl 0x5418c15(%rip), %eax   # 0x546da08
movl %eax, %esi
movl $0x138, %edx            # imm = 0x138
leaq 0x451f(%rip), %rcx      # 0x59320
callq 0x4630
leaq 0x5258bf3(%rip), %rdi   # 0x52ada00
xorl %esi, %esi
movl $0x7fff8, %edx          # imm = 0x7FFF8
callq 0x41b0
movq 0x5398be0(%rip), %rax   # 0x53eda00
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x20(%rsp)
movq 0x5398bce(%rip), %rax   # 0x53eda00
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdx
movq 0x18(%rsp), %rax
movslq 0x58(%rax), %rcx
leaq 0x5258bb4(%rip), %rax   # 0x52ada00
movq %rdx, (%rax,%rcx,8)
movl $0x1, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x5418ba6(%rip), %eax   # 0x546da08
jae 0x54ed1
movq 0x20(%rsp), %rax
movl 0x58(%rax), %eax
movq 0x18(%rsp), %rcx
cmpl 0x58(%rcx), %eax
je 0x54e9d
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq 0x20(%rsp), %rdx
movq 0x20(%rsp), %rax
movslq 0x58(%rax), %rcx
leaq 0x5258b69(%rip), %rax   # 0x52ada00
movq %rdx, (%rax,%rcx,8)
jmp 0x54eaa
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x20(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x20(%rsp)
jmp 0x54e58
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x5418b25(%rip), %eax   # 0x546da08
jae 0x54f3b
movq 0x5398b14(%rip), %rax   # 0x53eda00
movl 0x14(%rsp), %ecx
imulq $0x138, %rcx, %rcx      # imm = 0x138
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x5cc60
leaq 0xa315bc(%rip), %rcx    # 0xa864d0
subq %rcx, %rax
movl $0x1c, %ecx
cqto
idivq %rcx
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x130(%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x54ed9
addq $0x38, %rsp
retq