// File: /workspace/github_cpp_makefile_star2_O1/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
leaq (%rax,%rax,4), %rdi
shlq $0x3, %rdi
addq 0x17a80(%rip), %rdi     # 0x27fd8
leaq 0x11599(%rip), %rsi     # 0x21af8
movq %rsp, %rdx
callq 0x83c8
cmpl $0x0, 0x14(%rbx)
jle 0x1070f
movl $0x98, %edx
addq 0x17a4b(%rip), %rdx     # 0x27fc8
xorl %ebp, %ebp
movslq 0x10(%rbx), %rax
movslq %ebp, %rcx
addq %rax, %rcx
imulq $0xd0, %rcx, %r13
addq 0x17a31(%rip), %r13     # 0x27fc8
movapd (%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
imulq $0xd0, %rax, %rax
movq %rdx, 0x20(%rsp)
addq %rdx, %rax
xorl %ecx, %ecx
movupd -0x10(%rax,%rcx), %xmm2
subpd %xmm0, %xmm2
movupd %xmm2, -0x10(%rax,%rcx)
movsd (%rax,%rcx), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%rax,%rcx)
addq $0x18, %rcx
cmpq $0x48, %rcx
jne 0x105b3
leaq 0x88(%r13), %rdi
leaq 0xa0(%r13), %rsi
leaq 0xb8(%r13), %rdx
callq 0xe328
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x10618
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
movl $0x5, %edi
leaq 0xf13b(%rip), %rsi      # 0x1f749
movl %ebp, %r8d
xorl %eax, %eax
callq 0xa38f
movq 0x90(%rbx), %r12
testq %r12, %r12
je 0x1068d
leaq 0x60(%r13), %r15
movl 0x8(%r12), %eax
cmpl %r14d, %eax
je 0x1063a
xorl %r14d, %eax
cmpl $0x1, %eax
jne 0x10684
movl (%rbx), %edx
movsd 0x88(%r13), %xmm0
movsd 0x90(%r13), %xmm1
addsd (%rsp), %xmm0
addsd 0x8(%rsp), %xmm1
movl 0x4(%rbx), %ecx
movsd 0x98(%r13), %xmm2
addsd 0x10(%rsp), %xmm2
movl $0x6, %edi
leaq 0xf105(%rip), %rsi      # 0x1f77c
movl %ebp, %r8d
movq %r15, %r9
movb $0x3, %al
callq 0xa38f
movq (%r12), %r12
testq %r12, %r12
jne 0x10628
movl $0x68, %edi
callq 0x4994
movq %rax, %r15
movl %r14d, 0x8(%rax)
movslq %r14d, %rax
shlq $0x6, %rax
leaq 0x20bc294(%rip), %rcx   # 0x20cc940
addq %rcx, %rax
movq %rax, 0x10(%r15)
movq 0x90(%rbx), %rax
movq %rax, (%r15)
movq %r15, 0x90(%rbx)
xorl %eax, %eax
leaq 0x61bcf6c(%rip), %rcx   # 0x61cd639
cmpb $0x0, (%rcx)
jne 0x106e1
movq 0x10(%r15), %rdi
movq %r13, %rsi
movq %rsp, %rdx
callq 0x1a7e7
movl %eax, 0x20(%r15)
movb $0x1, %al
cmpb $0x0, 0x20(%rbx)
jne 0x106f4
movb 0x80(%r13), %al
movb %al, 0x30(%r15)
incl %ebp
movq 0x20(%rsp), %rdx
addq $0xd0, %rdx
cmpl 0x14(%rbx), %ebp
jl 0x1057f
movb $0x1, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq