// File: FreeSlave[P]vhlt/hlcsg/qcsg.cpp
// Function: ProcessModels()
static void     ProcessModels()
{
    int             i, j, type;
    int             placed;
    int             first, contents;
    brush_t         temp;

    for (i = 0; i < g_numentities; i++)
    {
        if (!g_entities[i].numbrushes) // only models
            continue;

        // sort the contents down so stone bites water, etc
        first = g_entities[i].firstbrush;
#ifdef HLCSG_SORTBRUSH_KEEPORDER
		brush_t *temps = (brush_t *)malloc (g_entities[i].numbrushes * sizeof (brush_t));
		hlassume (temps, assume_NoMemory);
		for (j = 0; j < g_entities[i].numbrushes; j++)
		{
			temps[j] = g_mapbrushes[first + j];
		}
		int placedcontents;
		bool b_placedcontents = false;
		for (placed = 0; placed < g_entities[i].numbrushes; )
		{
			bool b_contents = false;
			for (j = 0; j < g_entities[i].numbrushes; j++)
			{
				brush_t *brush = &temps[j];
				if (b_placedcontents && brush->contents <= placedcontents)
					continue;
				if (b_contents && brush->contents >= contents)
					continue;
				b_contents = true;
				contents = brush->contents;
			}
			for (j = 0; j < g_entities[i].numbrushes; j++)
			{
				brush_t *brush = &temps[j];
				if (brush->contents == contents)
				{
					g_mapbrushes[first + placed] = *brush;
					placed++;
				}
			}
			b_placedcontents = true;
			placedcontents = contents;
		}
		free (temps);
#else
#ifdef HLCSG_SORTBRUSH_FIX
        placed = 0;
		while (placed < g_entities[i].numbrushes)
		{
			for (j = placed; j < g_entities[i].numbrushes; j++)
			{
				if (j == placed)
				{
					contents = g_mapbrushes[first + j].contents;
				}
				else
				{
					contents = qmin(g_mapbrushes[first + j].contents, contents);
				}
			}
			for (j = placed; j < g_entities[i].numbrushes; j++)
			{
				if (g_mapbrushes[first + j].contents == contents)
				{
					temp = g_mapbrushes[first + placed];
					g_mapbrushes[first + placed] = g_mapbrushes[first + j];
					g_mapbrushes[first + j] = temp;
					placed++;
				}
			}
		}
#else
        placed = 0;
        for (type = 0; type < NUM_TYPECONTENTS; type++)                 // for each of the contents types
        {
            contents = typecontents[type];
            for (j = placed + 1; j < g_entities[i].numbrushes; j++)     // for each of the model's brushes
            {
                // if this brush is of the contents type in this for iteration
                if (g_mapbrushes[first + j].contents == contents)       
                {
                    temp = g_mapbrushes[first + placed];
                    g_mapbrushes[first + placed] = g_mapbrushes[j];
                    g_mapbrushes[j] = temp;
                    placed++;
                }
            }
        }
#endif
#endif

        // csg them in order
        if (i == 0) // if its worldspawn....
        {
            NamedRunThreadsOnIndividual(g_entities[i].numbrushes, g_estimate, CSGBrush);
            CheckFatal();
        }
        else
        {
            for (j = 0; j < g_entities[i].numbrushes; j++)
            {
                CSGBrush(first + j);
            }
        }

        // write end of model marker
        for (j = 0; j < NUM_HULLS; j++)
        {
#ifdef ZHLT_DETAILBRUSH
			fprintf (out[j], "-1 -1 -1 -1 -1\n");
			fprintf (out_detailbrush[j], "-1\n");
#else
            fprintf(out[j], "-1 -1 -1 -1\n");
#endif
        }
    }
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
leaq 0x40(%rsp), %rdi
callq 0x1e180
movl $0x0, 0x194(%rsp)
movl 0x194(%rsp), %eax
leaq 0x2009d5a(%rip), %rcx   # 0x202d4a4
cmpl (%rcx), %eax
jge 0x23b69
movslq 0x194(%rsp), %rcx
leaq 0x2009d4f(%rip), %rax   # 0x202d4b0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x23773
jmp 0x23b53
movslq 0x194(%rsp), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x2009d2a(%rip), %rax   # 0x202d4b0
movl 0x18(%rax,%rcx,8), %ecx
movl %ecx, 0x184(%rsp)
movslq 0x194(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x1c(%rax,%rcx,8), %rax
shlq $0x6, %rax
leaq (%rax,%rax,4), %rdi
callq 0x4500
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
movl $0x1, %esi
callq 0xc400
jmp 0x237ce
movl $0x0, 0x190(%rsp)
movl 0x190(%rsp), %eax
movslq 0x194(%rsp), %rdx
leaq 0x2009cc1(%rip), %rcx   # 0x202d4b0
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0x2386d
movl 0x184(%rsp), %eax
addl 0x190(%rsp), %eax
cltq
leaq 0x30b7e2e(%rip), %rsi   # 0x30db640
imulq $0x140, %rax, %rax      # imm = 0x140
addq %rax, %rsi
movq 0x38(%rsp), %rdi
movslq 0x190(%rsp), %rax
imulq $0x140, %rax, %rax      # imm = 0x140
addq %rax, %rdi
movl $0x140, %edx            # imm = 0x140
callq 0x4310
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x237d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x1e230
jmp 0x23b7b
movb $0x0, 0x27(%rsp)
movl $0x0, 0x188(%rsp)
movl 0x188(%rsp), %eax
movslq 0x194(%rsp), %rdx
leaq 0x2009c1d(%rip), %rcx   # 0x202d4b0
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0x23a18
movb $0x0, 0x26(%rsp)
movl $0x0, 0x190(%rsp)
movl 0x190(%rsp), %eax
movslq 0x194(%rsp), %rdx
leaq 0x2009be7(%rip), %rcx   # 0x202d4b0
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0x2394c
movq 0x38(%rsp), %rax
movslq 0x190(%rsp), %rcx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movq %rax, 0x18(%rsp)
testb $0x1, 0x27(%rsp)
je 0x23908
movq 0x18(%rsp), %rax
movl 0x58(%rax), %eax
cmpl 0x28(%rsp), %eax
jg 0x23908
jmp 0x23936
testb $0x1, 0x26(%rsp)
je 0x23922
movq 0x18(%rsp), %rax
movl 0x58(%rax), %eax
cmpl 0x180(%rsp), %eax
jl 0x23922
jmp 0x23936
movb $0x1, 0x26(%rsp)
movq 0x18(%rsp), %rax
movl 0x58(%rax), %eax
movl %eax, 0x180(%rsp)
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x238b3
movl $0x0, 0x190(%rsp)
movl 0x190(%rsp), %eax
movslq 0x194(%rsp), %rdx
leaq 0x2009b43(%rip), %rcx   # 0x202d4b0
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0x23a03
movq 0x38(%rsp), %rax
movslq 0x190(%rsp), %rcx
imulq $0x140, %rcx, %rcx      # imm = 0x140
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl 0x58(%rax), %eax
cmpl 0x180(%rsp), %eax
jne 0x239eb
movq 0x10(%rsp), %rsi
movl 0x184(%rsp), %eax
addl 0x188(%rsp), %eax
cltq
leaq 0x30b7c7a(%rip), %rdi   # 0x30db640
imulq $0x140, %rax, %rax      # imm = 0x140
addq %rax, %rdi
movl $0x140, %edx            # imm = 0x140
callq 0x4310
movl 0x188(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x188(%rsp)
jmp 0x239ed
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x23957
movb $0x1, 0x27(%rsp)
movl 0x180(%rsp), %eax
movl %eax, 0x28(%rsp)
jmp 0x2387d
movq 0x38(%rsp), %rdi
callq 0x4470
cmpl $0x0, 0x194(%rsp)
jne 0x23a8c
leaq 0xc1a1(%rip), %rdi      # 0x2fbd4
callq 0xb7f0
movq %rax, 0x8(%rsp)
jmp 0x23a3f
movq 0x8(%rsp), %rsi
leaq 0xc541(%rip), %rdi      # 0x2ff8c
xorl %eax, %eax
callq 0xb5d0
jmp 0x23a54
movslq 0x194(%rsp), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x2009a49(%rip), %rax   # 0x202d4b0
movl 0x1c(%rax,%rcx,8), %edi
movzbl 0x13a40(%rip), %esi     # 0x374b2
andl $0x1, %esi
leaq 0x354(%rip), %rdx       # 0x23dd0
callq 0xd760
jmp 0x23a83
callq 0xba60
jmp 0x23a8a
jmp 0x23ae7
movl $0x0, 0x190(%rsp)
movl 0x190(%rsp), %eax
movslq 0x194(%rsp), %rdx
leaq 0x2009a03(%rip), %rcx   # 0x202d4b0
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0x23ae5
movl 0x184(%rsp), %edi
movl 0x190(%rsp), %eax
addl %eax, %edi
callq 0x23dd0
jmp 0x23ad0
jmp 0x23ad2
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x23a97
jmp 0x23ae7
movl $0x0, 0x190(%rsp)
cmpl $0x4, 0x190(%rsp)
jge 0x23b51
movslq 0x190(%rsp), %rcx
leaq 0x61b7bf5(%rip), %rax   # 0x61db700
movq (%rax,%rcx,8), %rdi
leaq 0xc0c8(%rip), %rsi      # 0x2fbde
movb $0x0, %al
callq 0x4550
movslq 0x190(%rsp), %rcx
leaq 0x61b7c24(%rip), %rax   # 0x61db750
movq (%rax,%rcx,8), %rdi
leaq 0xc0b3(%rip), %rsi      # 0x2fbea
movb $0x0, %al
callq 0x4550
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x23af2
jmp 0x23b53
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
jmp 0x2373c
leaq 0x40(%rsp), %rdi
callq 0x1e230
addq $0x198, %rsp            # imm = 0x198
retq
movq 0x30(%rsp), %rdi
callq 0x4650
nopw %cs:(%rax,%rax)