// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlcsg/brush.cpp
// Function: MakeBrushPlanes(brush_s*)
bool            MakeBrushPlanes(brush_t* b)
{
    int             i;
    int             j;
    int             planenum;
    side_t*         s;
    bface_t*        f;
    vec3_t          origin;

    //
    // if the origin key is set (by an origin brush), offset all of the values
    //
    GetVectorForKey(&g_entities[b->entitynum], "origin", origin);

    //
    // convert to mapplanes
    //
    // for each side in this brush
    for (i = 0; i < b->numsides; i++)
    {
        s = &g_brushsides[b->firstside + i];
        for (j = 0; j < 3; j++)
        {
            VectorSubtract(s->planepts[j], origin, s->planepts[j]);
        }
        planenum = PlaneFromPoints(s->planepts[0], s->planepts[1], s->planepts[2]);
        if (planenum == -1)
        {
            Fatal(assume_PLANE_WITH_NO_NORMAL, "Entity %i, Brush %i, Side %i: plane with no normal", 
#ifdef HLCSG_COUNT_NEW
				b->originalentitynum, b->originalbrushnum
#else
				b->entitynum, b->brushnum
#endif
				, i);
        }

        //
        // see if the plane has been used already
        //
        for (f = b->hulls[0].faces; f; f = f->next)
        {
            if (f->planenum == planenum || f->planenum == (planenum ^ 1))
            {
                Fatal(assume_BRUSH_WITH_COPLANAR_FACES, "Entity %i, Brush %i, Side %i: has a coplanar plane at (%.0f, %.0f, %.0f), texture %s",
#ifdef HLCSG_COUNT_NEW
					b->originalentitynum, b->originalbrushnum
#else
                      b->entitynum, b->brushnum
#endif
					  , i, s->planepts[0][0] + origin[0], s->planepts[0][1] + origin[1],
                      s->planepts[0][2] + origin[2], s->td.name);
            }
        }

        f = (bface_t*)Alloc(sizeof(*f));                             // TODO: This leaks

        f->planenum = planenum;
        f->plane = &g_mapplanes[planenum];
        f->next = b->hulls[0].faces;
        b->hulls[0].faces = f;
        f->texinfo = g_onlyents ? 0 : TexinfoForBrushTexture(f->plane, &s->td, origin
#ifdef ZHLT_HIDDENSOUNDTEXTURE
						, s->shouldhide
#endif
						);
#ifdef HLCSG_CUSTOMHULL
		f->bevel = b->bevel || s->bevel;
#endif
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movslq 0x8(%rdi), %rax
imulq $0x28, %rax, %rdi
addq 0x17ec5(%rip), %rdi     # 0x25fd8
leaq 0x108f3(%rip), %rsi     # 0x1ea0d
movq %rsp, %rdx
callq 0x73ec
leaq 0x90(%rbx), %r15
xorl %ebp, %ebp
movq %r15, 0x20(%rsp)
cmpl 0x14(%rbx), %ebp
jge 0xe29d
movl 0x10(%rbx), %eax
addl %ebp, %eax
cltq
imulq $0xd0, %rax, %rax
leaq 0x3abd4a2(%rip), %rcx   # 0x3acb5f0
leaq (%rcx,%rax), %r14
leaq (%rcx,%rax), %rdi
addq $0x88, %rdi
movapd (%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
xorl %eax, %eax
cmpq $0x48, %rax
je 0xe194
movupd (%rdi,%rax), %xmm2
subpd %xmm0, %xmm2
movupd %xmm2, (%rdi,%rax)
movsd 0x10(%rdi,%rax), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rdi,%rax)
addq $0x18, %rax
jmp 0xe16a
leaq 0xa0(%r14), %rsi
leaq 0xb8(%r14), %rdx
callq 0xc6b0
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0xe1c8
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
pushq $0x5
popq %rdi
leaq 0xe4eb(%rip), %rsi      # 0x1c6a9
movl %ebp, %r8d
xorl %eax, %eax
callq 0x8e70
leaq 0x60(%r14), %r13
movq (%r15), %r15
testq %r15, %r15
je 0xe22f
movl 0x8(%r15), %eax
cmpl %r12d, %eax
je 0xe1e5
xorl %r12d, %eax
cmpl $0x1, %eax
jne 0xe1cc
movl (%rbx), %edx
movsd 0x88(%r14), %xmm0
movsd 0x90(%r14), %xmm1
addsd (%rsp), %xmm0
addsd 0x8(%rsp), %xmm1
movl 0x4(%rbx), %ecx
movsd 0x98(%r14), %xmm2
addsd 0x10(%rsp), %xmm2
pushq $0x6
popq %rdi
leaq 0xe4bc(%rip), %rsi      # 0x1c6dc
movl %ebp, %r8d
movq %r13, %r9
movb $0x3, %al
callq 0x8e70
jmp 0xe1cc
pushq $0x68
popq %rdi
callq 0x4842
movq %rax, %r13
movl %r12d, 0x8(%rax)
movslq %r12d, %rax
shlq $0x6, %rax
leaq 0x20bc6f4(%rip), %rcx   # 0x20ca940
addq %rcx, %rax
movq %rax, 0x10(%r13)
movq 0x20(%rsp), %r15
movq (%r15), %rax
movq %rax, (%r13)
movq %r13, (%r15)
xorl %eax, %eax
leaq 0x61bd3ce(%rip), %rcx   # 0x61cb639
cmpb $0x0, (%rcx)
jne 0xe27f
movq 0x10(%r13), %rdi
movq %r14, %rsi
movq %rsp, %rdx
callq 0x17aff
movl %eax, 0x20(%r13)
movb $0x1, %al
cmpb $0x0, 0x20(%rbx)
jne 0xe292
movb 0x80(%r14), %al
movb %al, 0x30(%r13)
incl %ebp
jmp 0xe130
movb $0x1, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq