// File: /workspace/github_cpp_makefile_star2_O3/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 0xfc5e
leaq 0x30(%rbx), %rax
movq %rax, (%rsp)
leaq 0x20bca8c(%rip), %r13   # 0x20cc930
movaps 0xf415(%rip), %xmm0     # 0x1f2c0
movups %xmm0, (%rbx)
movaps 0xf41b(%rip), %xmm0     # 0x1f2d0
movups %xmm0, 0x10(%rbx)
movaps 0xf3f0(%rip), %xmm0     # 0x1f2b0
movups %xmm0, 0x20(%rbx)
movq (%rsp), %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0xffed
movl $0x20, %edi
callq 0x4300
movq %rax, %r15
movq 0x10(%rbp), %rsi
movsd 0x30(%rsi), %xmm0
movq %rax, %rdi
callq 0xc786
movq (%rsp), %rax
movq (%rax), %r14
testq %r14, %r14
je 0xff34
cmpq %r14, %rbp
je 0xff2f
movslq 0x8(%r14), %rax
xorq $0x1, %rax
shlq $0x6, %rax
leaq (%rax,%r13), %rsi
movsd 0x30(%rax,%r13), %xmm0
movq %r15, %rdi
movsd 0xc1f2(%rip), %xmm1     # 0x1c118
callq 0xd058
testb %al, %al
je 0xff34
movq (%r14), %r14
jmp 0xfefa
movq %r15, %rdi
movsd 0xf5b9(%rip), %xmm0     # 0x1f4f8
callq 0xc87a
movq %r15, %rdi
callq 0xbba2
movapd %xmm0, %xmm1
movsd 0xf5e8(%rip), %xmm0     # 0x1f540
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
ja 0xff9d
movq %r15, 0x18(%rbp)
movl $0xffffffff, 0x28(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x8(%r15)
je 0xffc4
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r15), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0xdcae
incq %r12
movl 0x8(%r15), %eax
addq $0x18, %r14
cmpq %rax, %r12
jb 0xff7c
jmp 0xffc4
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq (%rsp), %rax
movq (%rax), %rax
cmpq %rbp, %rax
je 0xffdd
movq %rax, %rcx
movq (%rax), %rax
cmpq %rbp, %rax
jne 0xffb2
movq (%rbp), %rax
movq %rax, (%rcx)
movsd 0xf574(%rip), %xmm0     # 0x1f540
ucomisd 0x10(%rsp), %xmm0
jbe 0xfec8
jmp 0xfea4
movq (%rbp), %rax
movq (%rsp), %rcx
movq %rax, (%rcx)
jmp 0xfea4
xorl %r12d, %r12d
leaq 0xf6f5(%rip), %r15      # 0x1f6ec
movq 0x8(%rsp), %r14
movsd 0xf544(%rip), %xmm0     # 0x1f548
ucomisd (%rbx,%r12,8), %xmm0
ja 0x1001d
movsd 0x18(%rbx,%r12,8), %xmm0
ucomisd 0xc0bd(%rip), %xmm0     # 0x1c0d8
jbe 0x10056
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 $0x7, %edi
movq %r15, %rsi
movl $0x7fff, %r8d           # imm = 0x7FFF
movb $0x6, %al
callq 0xa253
incq %r12
cmpq $0x3, %r12
jne 0xfffc
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x4310
movq %rbx, %rdi
callq 0x4560