// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rax
movslq 0x8(%rax), %rax
leaq 0x20175a7(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x10(%rsp), %rdx
leaq 0x19ca4(%rip), %rsi     # 0x2fbc0
callq 0x9110
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x16230
movq 0x50(%rsp), %rax
movl 0x10(%rax), %eax
addl 0x4c(%rsp), %eax
movslq %eax, %rcx
leaq 0x3ac56ff(%rip), %rax   # 0x3adb650
imulq $0xd0, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x48(%rsp)
cmpl $0x3, 0x48(%rsp)
jge 0x1603b
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
subsd 0x10(%rsp), %xmm0
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
subsd 0x18(%rsp), %xmm0
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
subsd 0x20(%rsp), %xmm0
movq 0x38(%rsp), %rax
addq $0x88, %rax
movslq 0x48(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x10(%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x15f68
movq 0x38(%rsp), %rdi
addq $0x88, %rdi
movq 0x38(%rsp), %rsi
addq $0x88, %rsi
addq $0x18, %rsi
movq 0x38(%rsp), %rdx
addq $0x88, %rdx
addq $0x30, %rdx
callq 0x12a30
movl %eax, 0x44(%rsp)
cmpl $-0x1, 0x44(%rsp)
jne 0x1609e
movq 0x50(%rsp), %rax
movl (%rax), %edx
movq 0x50(%rsp), %rax
movl 0x4(%rax), %ecx
movl 0x4c(%rsp), %r8d
movl $0x5, %edi
leaq 0x177f1(%rip), %rsi     # 0x2d888
movb $0x0, %al
callq 0xbbb0
movq 0x50(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x16159
movq 0x30(%rsp), %rax
movl 0x8(%rax), %eax
cmpl 0x44(%rsp), %eax
je 0x160dc
movq 0x30(%rsp), %rax
movl 0x8(%rax), %eax
movl 0x44(%rsp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x16145
movq 0x50(%rsp), %rax
movl (%rax), %edx
movq 0x50(%rsp), %rax
movl 0x4(%rax), %ecx
movl 0x4c(%rsp), %r8d
movq 0x38(%rsp), %rax
movsd 0x88(%rax), %xmm0
addsd 0x10(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd 0x90(%rax), %xmm1
addsd 0x18(%rsp), %xmm1
movq 0x38(%rsp), %rax
movsd 0x98(%rax), %xmm2
addsd 0x20(%rsp), %xmm2
movq 0x38(%rsp), %r9
addq $0x60, %r9
movl $0x6, %edi
leaq 0x1777d(%rip), %rsi     # 0x2d8bb
movb $0x3, %al
callq 0xbbb0
jmp 0x16147
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x160af
movl $0x68, %edi
callq 0x4aa0
movq %rax, 0x30(%rsp)
movl 0x44(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x8(%rax)
movslq 0x44(%rsp), %rax
leaq 0x20c4820(%rip), %rcx   # 0x20da9a0
shlq $0x6, %rax
addq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x50(%rsp), %rax
movq 0x90(%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x90(%rax)
leaq 0x61c550d(%rip), %rax   # 0x61db6c9
testb $0x1, (%rax)
je 0x161c9
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x161e5
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x28330
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x50(%rsp), %rcx
movb $0x1, %al
testb $0x1, 0x20(%rcx)
movb %al, 0xb(%rsp)
jne 0x16211
movq 0x38(%rsp), %rax
movb 0x80(%rax), %al
movb %al, 0xb(%rsp)
movb 0xb(%rsp), %cl
movq 0x30(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x30(%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x15f29
movb $0x1, %al
andb $0x1, %al
addq $0x58, %rsp
retq
nopl (%rax)