// File: /workspace/github_cpp_makefile_star2_O3/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 $0x178, %rsp            # imm = 0x178
movq %r8, %rbx
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbp
leaq 0x138789(%rip), %rax    # 0x15fec8
addq $0x10, %rax
leaq 0x70(%rsp), %rdi
movq %rax, -0x8(%rdi)
leaq 0x144e8d(%rip), %r14    # 0x16c5e0
leaq 0x8(%r14), %rsi
callq 0x713ea
movl $0x88, %esi
leaq 0x68(%rsp), %rdi
addq %rsi, %rdi
addq 0x13c870(%rip), %rsi    # 0x163fe0
callq 0x713ea
movq 0x108(%r14), %rax
leaq 0x68(%rsp), %rdi
movq %rax, 0x108(%rdi)
movq %r12, %rsi
callq 0x6f2c2
leaq 0x58(%rbp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x6f2c2
cmpl $0x0, 0x4c(%rbp)
jle 0x277d9
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
movq 0x40(%rbp), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x27716
incq %r14
movslq 0x4c(%rbp), %rax
cmpq %rax, %r14
jl 0x277ac
movl 0x3c(%rbp), %eax
testl %eax, %eax
jle 0x27924
xorl %edx, %edx
leaq 0x38(%rsp), %r13
movq %rbp, 0x10(%rsp)
movq 0x30(%rbp), %rcx
movq (%rcx,%rdx,8), %r14
cmpl $0x0, 0x1c(%r14)
jle 0x27915
movq %rdx, 0x18(%rsp)
xorl %ecx, %ecx
movq %r14, 0x20(%rsp)
movq 0x10(%r14), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rbp
movq (%rbp), %rax
movq %rbp, %rdi
callq *0xb8(%rax)
movl %eax, %r12d
callq 0x67010
cmpl %eax, %r12d
jne 0x278ee
cmpl $0x0, 0x24(%rbp)
jle 0x278ee
xorl %r14d, %r14d
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %r12
movq (%r12), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x68(%rax)
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
callq 0x81fcc
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x74800
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movq %r15, %rdi
callq 0x74202
movsd %xmm0, 0x30(%rsp)
movq (%r12), %rax
movq %r12, %rdi
callq *0x58(%rax)
leaq 0x13d79f(%rip), %rax    # 0x165058
movsd 0x30(%rsp), %xmm1
ucomisd (%rax), %xmm1
jbe 0x278d6
movq 0x8(%rsp), %rax
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
incq %r14
movslq 0x24(%rbp), %rax
cmpq %rax, %r14
jl 0x27847
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r14
movslq 0x1c(%r14), %rax
cmpq %rax, %rcx
jl 0x2780f
movq 0x10(%rsp), %rbp
movl 0x3c(%rbp), %eax
movq 0x18(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x277f0
cmpl $0x0, 0x2c(%rbp)
jle 0x27959
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
movq 0x20(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x27716
incq %r14
movslq 0x2c(%rbp), %rax
cmpq %rax, %r14
jl 0x27932
leaq 0x68(%rsp), %rdi
callq 0x6f154
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27990
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x6f7e8
jmp 0x2799d
jmp 0x27990
jmp 0x27990
jmp 0x27990
jmp 0x27990
jmp 0x27990
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x6f154
movq %rbx, %rdi
callq 0x23050
nop