// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/scn2scn/scn2scn.cpp
// Function: CountOverlapAreas(gaps::R3SceneNode*, gaps::R3Grid*, gaps::R3Affine const&, double&, double&)
static void
CountOverlapAreas(R3SceneNode *node, R3Grid *grid, const R3Affine& parent_transformation, RNArea& overlap_area, RNArea& total_area)
{
  // Update transformation
  R3Affine transformation = R3identity_affine;
  transformation.Transform(parent_transformation);
  transformation.Transform(node->Transformation());

  // Count overlapping triangle areas in references
  for (int i = 0; i < node->NReferences(); i++) {
    R3SceneReference *reference = node->Reference(i);
    R3Scene *referenced_scene = reference->ReferencedScene();
    CountOverlapAreas(referenced_scene->Root(), grid, transformation, overlap_area, total_area);
  }

  // Count overlapping triangle areas in elements
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *element = node->Element(i);
    for (int j = 0; j < element->NShapes(); j++) {
      R3Shape *shape = element->Shape(j);
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        for (int k = 0; k < triangles->NTriangles(); k++) {
          R3Triangle *triangle = triangles->Triangle(k);
          R3Point centroid = triangle->Centroid();
          centroid.Transform(transformation);
          RNScalar grid_value = grid->WorldValue(centroid);
          RNArea area = triangle->Area();
          if (grid_value > RN_EPSILON) overlap_area += area;
          total_area += area;
        }
      }
    }
  }
  
  // Count overlapping triangle areas in children
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);
    CountOverlapAreas(child, grid, transformation, overlap_area, total_area);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, %rbx
movq %rcx, (%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
leaq 0x134d8d(%rip), %rsi    # 0x15b600
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x26a6c
movq %r13, %rdi
movq %r14, %r13
movq %r12, %rsi
callq 0x6615a
leaq 0x58(%r13), %rsi
leaq 0x48(%rsp), %rdi
callq 0x6615a
xorl %r14d, %r14d
leaq 0x48(%rsp), %r12
movslq 0x4c(%r13), %rax
cmpq %rax, %r14
jge 0x268d2
movq 0x40(%r13), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq (%rsp), %rcx
movq %rbx, %r8
callq 0x2684b
incq %r14
jmp 0x268a4
movq %r13, 0x8(%rsp)
xorl %edx, %edx
leaq 0x30(%rsp), %r13
movq 0x8(%rsp), %rcx
movslq 0x3c(%rcx), %rax
cmpq %rax, %rdx
jge 0x269d0
movq 0x30(%rcx), %rax
movq %rdx, 0x10(%rsp)
movq (%rax,%rdx,8), %r14
xorl %ecx, %ecx
movq %r14, 0x18(%rsp)
movslq 0x1c(%r14), %rax
cmpq %rax, %rcx
jge 0x269c3
movq 0x10(%r14), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %r12
movq (%r12), %rax
movq %r12, %rdi
callq *0xb8(%rax)
movl %eax, %ebp
callq 0x5e7e0
cmpl %eax, %ebp
jne 0x269b1
xorl %r14d, %r14d
movslq 0x24(%r12), %rax
cmpq %rax, %r14
jge 0x269b1
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %rbp
movq (%rbp), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x68(%rax)
movq %r13, %rdi
leaq 0x48(%rsp), %rsi
callq 0x76b0e
movq %r15, %rdi
movq %r13, %rsi
callq 0x26c40
movsd %xmm0, 0x28(%rsp)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x58(%rax)
leaq 0x12d6f0(%rip), %rax    # 0x154078
movsd 0x28(%rsp), %xmm1
ucomisd (%rax), %xmm1
jbe 0x269a4
movq (%rsp), %rax
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
incq %r14
jmp 0x26939
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %r14
jmp 0x26904
movq 0x10(%rsp), %rdx
incq %rdx
jmp 0x268de
xorl %r14d, %r14d
leaq 0x48(%rsp), %r12
movq %rcx, %r13
movslq 0x2c(%r13), %rax
cmpq %rax, %r14
jge 0x26a03
movq 0x20(%r13), %rax
movq (%rax,%r14,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq (%rsp), %rcx
movq %rbx, %r8
callq 0x2684b
incq %r14
jmp 0x269db
leaq 0x48(%rsp), %rdi
callq 0x65fee
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x26a2b
jmp 0x26a2b
jmp 0x26a2b
jmp 0x26a2b
jmp 0x26a2b
jmp 0x26a2b
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x65fee
movq %rbx, %rdi
callq 0x23070