// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x50, %rsp
movq %rdi, %rbx
movq 0x30(%rdi), %r12
testq %r12, %r12
je 0x123da
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebp, %ebp
leaq 0x30(%rsp), %r14
leaq 0xd93c(%rip), %r15      # 0x1fcaa
movq 0x18(%r12), %rdi
movq %r14, %rsi
callq 0xbd96
movsd 0x30(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movupd 0x38(%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 $0x3, %edi
movq %r15, %rsi
movl %ebp, %edx
movb $0x3, %al
callq 0xa676
incl %ebp
movq (%r12), %r12
testq %r12, %r12
jne 0x1236e
cvtsi2sd %ebp, %xmm3
movapd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm0
jmp 0x123e6
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
movsd 0xccfa(%rip), %xmm4     # 0x1f0e8
divsd %xmm3, %xmm4
mulsd %xmm4, %xmm0
movsd %xmm0, (%rsp)
movapd %xmm2, %xmm1
mulsd %xmm4, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
mulsd %xmm4, %xmm2
leaq 0xd8ba(%rip), %rsi      # 0x1fccc
movl $0x3, %edi
movapd %xmm1, 0x20(%rsp)
movapd %xmm2, 0x10(%rsp)
movb $0x3, %al
callq 0xa676
movq 0x30(%rbx), %rbx
testq %rbx, %rbx
xorpd %xmm0, %xmm0
je 0x124af
leaq 0x20ba4f2(%rip), %r14   # 0x20cc930
movsd %xmm0, 0x8(%rsp)
movslq 0x8(%rbx), %r15
shlq $0x6, %r15
movq 0x18(%rbx), %rdi
callq 0xbba2
movsd 0x8(%r14,%r15), %xmm1
mulsd 0x20(%rsp), %xmm1
movsd (%r14,%r15), %xmm2
mulsd (%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x10(%r14,%r15), %xmm1
mulsd 0x10(%rsp), %xmm1
addsd %xmm2, %xmm1
subsd 0x30(%r14,%r15), %xmm1
andpd 0xce5f(%rip), %xmm1     # 0x1f2f0
mulsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
divsd 0xd725(%rip), %xmm1     # 0x1fbc8
addsd %xmm1, %xmm0
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x1243e
leaq 0xd835(%rip), %rsi      # 0x1fceb
movl $0x3, %edi
movsd %xmm0, 0x8(%rsp)
movb $0x1, %al
callq 0xa676
movsd 0x8(%rsp), %xmm0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop