// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlcsg/brush.cpp
// Function: MakeHullFaces(brush_s const*, brushhull_t*)
void            MakeHullFaces(const brush_t* const b, brushhull_t *h)
{
    bface_t*        f;
    bface_t*        f2;
#ifdef HLCSG_PRECISIONCLIP //#ifdef HLCSG_PRECISECLIP //vluzacn
#ifndef HLCSG_CUSTOMHULL
	bool warned = false;
#endif
#endif
#ifdef HLCSG_SORTSIDES
	// this will decrease AllocBlock amount
	SortSides (h);
#endif

restart:
    h->bounds.reset();

    // for each face in this brushes hull
    for (f = h->faces; f; f = f->next)
    {
        Winding* w = new Winding(f->plane->normal, f->plane->dist);
        for (f2 = h->faces; f2; f2 = f2->next)
        {
            if (f == f2)
            {
                continue;
            }
            const plane_t* p = &g_mapplanes[f2->planenum ^ 1];
            if (!w->Chop(p->normal, p->dist
#ifdef HLCSG_MakeHullFaces_PRECISE
				, NORMAL_EPSILON  // fix "invalid brush" in ExpandBrush
#endif
				))   // Nothing left to chop (getArea will return 0 for us in this case for below)
            {
                break;
            }
        }
#ifdef HLCSG_MakeHullFaces_PRECISE
		w->RemoveColinearPoints (ON_EPSILON);
#endif
        if (w->getArea() < 0.1)
        {
#ifdef HLCSG_PRECISIONCLIP //#ifdef HLCSG_PRECISECLIP //vluzacn
#ifndef HLCSG_CUSTOMHULL // this occurs when there are BEVEL faces.
			if(w->getArea() == 0 && !warned) //warn user when there's a bad brush (face not contributing)
			{
				Warning("Illegal Brush (plane doesn't contribute to final shape): Entity %i, Brush %i\n",
#ifdef HLCSG_COUNT_NEW
					b->originalentitynum, b->originalbrushnum
#else
					b->entitynum, b->brushnum
#endif
					);
				warned = true;
			}
#endif
#endif
            delete w;
            if (h->faces == f)
            {
                h->faces = f->next;
            }
            else
            {
                for (f2 = h->faces; f2->next != f; f2 = f2->next)
                {
                    ;
                }
                f2->next = f->next;
            }
            goto restart;
        }
        else
        {
            f->w = w;
            f->contents = CONTENTS_EMPTY;
            unsigned int    i;
            for (i = 0; i < w->m_NumPoints; i++)
            {
                h->bounds.add(w->m_Points[i]);
            }
        }
    }

    unsigned int    i;
    for (i = 0; i < 3; i++)
    {
        if (h->bounds.m_Mins[i] < -BOGUS_RANGE / 2 || h->bounds.m_Maxs[i] > BOGUS_RANGE / 2)
        {
            Fatal(assume_BRUSH_OUTSIDE_WORLD, "Entity %i, Brush %i: outside world(+/-%d): (%.0f,%.0f,%.0f)-(%.0f,%.0f,%.0f)",
#ifdef HLCSG_COUNT_NEW
				b->originalentitynum, b->originalbrushnum,
#else
                  b->entitynum, b->brushnum,
#endif
                  BOGUS_RANGE / 2,
                  h->bounds.m_Mins[0], h->bounds.m_Mins[1], h->bounds.m_Mins[2],
                  h->bounds.m_Maxs[0], h->bounds.m_Maxs[1], h->bounds.m_Maxs[2]);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
movq %rsi, %rdi
callq 0xdce1
leaq 0x30(%rbx), %rax
movq %rax, (%rsp)
leaq 0x20bca21(%rip), %rbp   # 0x20ca940
movaps 0xe2ea(%rip), %xmm0     # 0x1c210
movups %xmm0, (%rbx)
movaps 0xe2f0(%rip), %xmm0     # 0x1c220
movups %xmm0, 0x10(%rbx)
movaps 0xe2f5(%rip), %xmm0     # 0x1c230
movups %xmm0, 0x20(%rbx)
movq (%rsp), %r13
movq (%r13), %r13
testq %r13, %r13
je 0xe05a
pushq $0x20
popq %rdi
callq 0x4310
movq %rax, %r12
movq 0x10(%r13), %rsi
movsd 0x30(%rsi), %xmm0
movq %rax, %rdi
callq 0xb02c
movq (%rsp), %r15
movq (%r15), %r15
testq %r15, %r15
je 0xdfa7
cmpq %r15, %r13
je 0xdf70
movslq 0x8(%r15), %rax
xorq $0x1, %rax
shlq $0x6, %rax
leaq (%rax,%rbp), %rsi
movsd 0x30(%rax,%rbp), %xmm0
movq %r12, %rdi
movsd 0xb0e2(%rip), %xmm1     # 0x19080
callq 0xb7b2
testb %al, %al
jne 0xdf70
movq %r12, %rdi
movsd 0xe4b6(%rip), %xmm0     # 0x1c468
callq 0xb10a
movq %r12, %rdi
callq 0xa5d6
movapd %xmm0, %xmm1
movsd 0xe4e5(%rip), %xmm0     # 0x1c4b0
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
ja 0xe009
movq %r12, 0x18(%r13)
orl $-0x1, 0x28(%r13)
xorl %r15d, %r15d
xorl %r14d, %r14d
movl 0x8(%r12), %eax
cmpq %rax, %r14
jae 0xe031
movq 0x10(%r12), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0xc35e
incq %r14
addq $0x18, %r15
jmp 0xdfe6
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
movq (%rsp), %rax
movq (%rax), %rax
cmpq %r13, %rax
je 0xe04a
movq %rax, %rcx
movq (%rax), %rax
cmpq %r13, %rax
jne 0xe01f
movq (%r13), %rax
movq %rax, (%rcx)
movsd 0xe477(%rip), %xmm0     # 0x1c4b0
ucomisd 0x10(%rsp), %xmm0
jbe 0xdf43
jmp 0xdf1f
movq (%r13), %rax
movq (%rsp), %rcx
movq %rax, (%rcx)
jmp 0xdf1f
xorl %r12d, %r12d
leaq 0xe5f8(%rip), %r15      # 0x1c65c
pushq $0x7
popq %rbp
movq 0x8(%rsp), %r14
cmpq $0x3, %r12
je 0xe0ce
movsd 0xe43e(%rip), %xmm0     # 0x1c4b8
ucomisd (%rbx,%r12,8), %xmm0
ja 0xe093
movsd 0x18(%rbx,%r12,8), %xmm0
ucomisd 0xe42f(%rip), %xmm0     # 0x1c4c0
jbe 0xe0c9
movl (%r14), %edx
movl 0x4(%r14), %ecx
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
movsd 0x18(%rbx), %xmm3
movsd 0x20(%rbx), %xmm4
movsd 0x28(%rbx), %xmm5
movl %ebp, %edi
movq %r15, %rsi
movl $0x7fff, %r8d           # imm = 0x7FFF
movb $0x6, %al
callq 0x8e70
incq %r12
jmp 0xe06c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x4320
movq %rbx, %rdi
callq 0x4590