// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlcsg/brushunion.cpp
// Function: CalculateSolidVolume(brushhull_t const*)
static vec_t    CalculateSolidVolume(const brushhull_t* const hull)
{
    // calculate polyhedron origin
    // subdivide face winding into triangles

    // for each face
    // calculate volume of triangle of face to origin
    // add subidivided volume chunk to total

    int             x = 0;
    vec_t           volume = 0.0;
    vec_t           inverse;
    vec3_t          midpoint = { 0.0, 0.0, 0.0 };

    bface_t*        face;

    for (face = hull->faces; face; face = face->next, x++)
    {
        vec3_t          facemid;

        face->w->getCenter(facemid);
        VectorAdd(midpoint, facemid, midpoint);
        Developer(DEVELOPER_LEVEL_MESSAGE, "Midpoint for face %d is %f %f %f\n", x, facemid[0], facemid[1], facemid[2]);
    }

    inverse = 1.0 / x;

    VectorScale(midpoint, inverse, midpoint);

    Developer(DEVELOPER_LEVEL_MESSAGE, "Midpoint for hull is %f %f %f\n", midpoint[0], midpoint[1], midpoint[2]);

    for (face = hull->faces; face; face = face->next, x++)
    {
        plane_t*        plane = &g_mapplanes[face->planenum];
        vec_t           area = face->w->getArea();
        vec_t           dist = DotProduct(plane->normal, midpoint);

        dist -= plane->dist;
        dist = fabs(dist);

        volume += area * dist / 3.0;
    }

    Developer(DEVELOPER_LEVEL_MESSAGE, "Volume for brush is %f\n", volume);

    return volume;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
addq $0x30, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x40(%rsp), %r14
leaq 0xcaf0(%rip), %r15      # 0x1cc1a
pushq $0x3
popq %r12
movq %rbx, %r13
movq (%r13), %r13
testq %r13, %r13
je 0x1018d
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xa79e
movsd 0x40(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movupd 0x48(%rsp), %xmm1
movapd 0x10(%rsp), %xmm2
addpd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
movl %r12d, %edi
movq %r15, %rsi
movl %ebp, %edx
movb $0x3, %al
callq 0x9291
incl %ebp
jmp 0x10131
cvtsi2sd %ebp, %xmm0
movsd 0xbebf(%rip), %xmm3     # 0x1c058
divsd %xmm0, %xmm3
movsd 0x8(%rsp), %xmm0
mulsd %xmm3, %xmm0
movapd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm3, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
mulsd %xmm3, %xmm2
leaq 0xca78(%rip), %rsi      # 0x1cc3c
pushq $0x3
popq %rdi
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, 0x30(%rsp)
movapd %xmm2, 0x10(%rsp)
movb $0x3, %al
callq 0x9291
xorpd %xmm0, %xmm0
leaq 0x20ba755(%rip), %r14   # 0x20ca940
movq (%rbx), %rbx
testq %rbx, %rbx
movsd %xmm0, 0x28(%rsp)
je 0x1025f
movslq 0x8(%rbx), %r15
shlq $0x6, %r15
movq 0x18(%rbx), %rdi
callq 0xa5d6
movsd 0x8(%r14,%r15), %xmm1
mulsd 0x30(%rsp), %xmm1
movsd (%r14,%r15), %xmm2
mulsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x10(%r14,%r15), %xmm1
mulsd 0x10(%rsp), %xmm1
addsd %xmm2, %xmm1
subsd 0x30(%r14,%r15), %xmm1
andpd 0xc009(%rip), %xmm1     # 0x1c250
mulsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
divsd 0xc8df(%rip), %xmm1     # 0x1cb38
addsd %xmm1, %xmm0
jmp 0x101eb
leaq 0xc9f5(%rip), %rsi      # 0x1cc5b
pushq $0x3
popq %rdi
movb $0x1, %al
callq 0x9291
movsd 0x28(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)