// File: /workspace/github_cpp_makefile_star2_O1/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 0x1011f
leaq 0x30(%rbx), %rax
movq %rax, (%rsp)
leaq 0x20bc5db(%rip), %r13   # 0x20cc940
movaps 0xef74(%rip), %xmm0     # 0x1f2e0
movups %xmm0, (%rbx)
movaps 0xef7a(%rip), %xmm0     # 0x1f2f0
movups %xmm0, 0x10(%rbx)
movaps 0xef4f(%rip), %xmm0     # 0x1f2d0
movups %xmm0, 0x20(%rbx)
movq (%rsp), %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x1049b
movl $0x20, %edi
callq 0x4310
movq %rax, %r15
movq 0x10(%rbp), %rsi
movsd 0x30(%rsi), %xmm0
movq %rax, %rdi
callq 0xc9a8
movq (%rsp), %rax
movq (%rax), %r14
testq %r14, %r14
je 0x103f5
cmpq %r14, %rbp
je 0x103f0
movslq 0x8(%r14), %rax
xorq $0x1, %rax
shlq $0x6, %rax
leaq (%rax,%r13), %rsi
movsd 0x30(%rax,%r13), %xmm0
movq %r15, %rdi
movsd 0xbd29(%rip), %xmm1     # 0x1c110
callq 0xd26e
testb %al, %al
je 0x103f5
movq (%r14), %r14
jmp 0x103bb
movq %r15, %rdi
movsd 0xf118(%rip), %xmm0     # 0x1f518
callq 0xca9c
movq %r15, %rdi
callq 0xbd40
movapd %xmm0, %xmm1
movsd 0xf13f(%rip), %xmm0     # 0x1f558
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
ja 0x1045e
movq %r15, 0x18(%rbp)
movl $0xffffffff, 0x28(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x8(%r15)
je 0x10482
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%r15), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0xdeb6
incq %r12
movl 0x8(%r15), %eax
addq $0x18, %r14
cmpq %rax, %r12
jb 0x1043d
jmp 0x10482
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq (%rsp), %rcx
movq (%rcx), %rax
cmpq %rbp, %rax
je 0x1047b
movq %rax, %rcx
movq (%rax), %rax
jmp 0x1046e
movq (%rbp), %rax
movq %rax, (%rcx)
movsd 0xf0ce(%rip), %xmm0     # 0x1f558
ucomisd 0x10(%rsp), %xmm0
jbe 0x10389
jmp 0x10365
xorl %r12d, %r12d
leaq 0xf257(%rip), %r15      # 0x1f6fc
movq 0x8(%rsp), %r14
movsd 0xf0ae(%rip), %xmm0     # 0x1f560
ucomisd (%rbx,%r12,8), %xmm0
ja 0x104cb
movsd 0x18(%rbx,%r12,8), %xmm0
ucomisd 0xbc07(%rip), %xmm0     # 0x1c0d0
jbe 0x10504
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 0xa38f
incq %r12
cmpq $0x3, %r12
jne 0x104aa
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 0x4320
movq %rbx, %rdi
callq 0x4570