// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::InsertSceneElement(gaps::R3Scene*, gaps::R3SceneNode*, gaps::R3Material*, gaps::RNArray<gaps::R3TriangleVertex*> const&, gaps::RNArray<gaps::R3Triangle*> const&, int)
static int
InsertSceneElement(R3Scene *scene, R3SceneNode *node, R3Material *material, 
  const RNArray<R3TriangleVertex *>& verts, const RNArray<R3Triangle *>& tris,
  RNBoolean smooth_normals = FALSE)
{
  // Create material if none
  if (!material) {
    R3Brdf *brdf = new R3Brdf(R3default_brdf, "Default");
    scene->InsertBrdf(brdf);
    material = new R3Material(brdf, "Default");
    scene->InsertMaterial(material);
  }
  
  // Find element
  R3SceneElement *element = NULL;
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *g = node->Element(i);
    if (g->Material() == material) { element = g; break; }
  }

  // Create element
  if (!element) {
    element = new R3SceneElement(material);
  }

  // Set vertex marks
  for (int i = 0; i < verts.NEntries(); i++) {
    R3TriangleVertex *vertex = verts.Kth(i);
    vertex->SetMark(0);
  }

  // Create copies of verts and tris for this element
  RNArray<R3Triangle *> element_tris;
  RNArray<R3TriangleVertex *> element_verts;
  for (int i = 0; i < tris.NEntries(); i++) {
    R3Triangle *triangle = tris.Kth(i);
    R3TriangleVertex *element_vertex[3];
    for (int j = 0; j < 3; j++) {
      R3TriangleVertex *vertex = triangle->Vertex(j);
      if (vertex->Mark() == 0) {
        element_verts.Insert(new R3TriangleVertex(*vertex));
        vertex->SetMark(element_verts.NEntries());
      }
      element_vertex[j] = element_verts.Kth(vertex->Mark()-1); 
    }
    R3Triangle *element_triangle = new R3Triangle(element_vertex[0], element_vertex[1], element_vertex[2]);
    element_tris.Insert(element_triangle);
  }

  // Create shape (triangle array)
  R3TriangleArray *shape = new R3TriangleArray(element_verts, element_tris);

  // Create smooth normals
  if (smooth_normals) shape->CreateVertexNormals();

  // Insert shape
  element->InsertShape(shape);
        
  // Insert element
  node->InsertElement(element);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %rbx
testq %rdx, %rdx
jne 0x31465
movq %rdi, %r15
movl $0xb8, %edi
callq 0x218d0
movq %rax, %rbp
leaq 0x102e9d(%rip), %rsi    # 0x1342c0
leaq 0x95221(%rip), %rdx     # 0xc664b
movq %rax, %rdi
callq 0x3f100
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2788a
pushq $0x30
popq %rdi
callq 0x218d0
movq %rax, %r13
leaq 0x951fc(%rip), %rdx     # 0xc664b
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3edf2
movq %r15, %rdi
movq %r13, %rsi
callq 0x27846
movl 0x3c(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rcx, %rax
je 0x31487
movq 0x30(%rbx), %rdx
movq (%rdx,%rcx,8), %r15
incq %rcx
cmpq %r13, 0x8(%r15)
jne 0x3146f
jmp 0x3149d
pushq $0x60
popq %rdi
callq 0x218d0
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
callq 0x394c0
movl 0xc(%r12), %eax
movq (%r12), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rdx, %rax
je 0x314bf
movq (%rcx,%rdx,8), %rsi
andl $0x0, 0x60(%rsi)
incq %rdx
jmp 0x314ad
movq %r15, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x7c1c6
leaq 0x10(%rsp), %rdi
callq 0x7c1c6
xorl %r15d, %r15d
pushq $0x68
popq %r13
leaq 0x10(%rsp), %rbp
movq %r14, 0x40(%rsp)
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jge 0x3159d
movq (%r14), %rax
movq %r15, 0x48(%rsp)
movq (%rax,%r15,8), %r15
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0x3155a
movq 0x8(%r15,%r14,8), %rbx
movl 0x60(%rbx), %eax
testl %eax, %eax
jne 0x31544
movq %r13, %rdi
callq 0x218d0
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x60942
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2737a
movl 0x1c(%rsp), %eax
movl %eax, 0x60(%rbx)
movq 0x10(%rsp), %rcx
cltq
movq -0x8(%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp,%r14,8)
incq %r14
jmp 0x3150a
movl $0x88, %edi
callq 0x218d0
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %r15
movq %rax, %rbx
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq %rax, %rdi
callq 0x60de0
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x2737a
incq %r15
jmp 0x314ee
pushq $0x70
popq %rdi
callq 0x218d0
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %r15
movq %rax, %r14
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %rax, %rdi
callq 0x5f2ae
cmpl $0x0, 0xc(%rsp)
je 0x315ea
leaq 0x97826(%rip), %rax     # 0xc8df8
movsd (%rax), %xmm0
mulsd 0x93efa(%rip), %xmm0    # 0xc54d8
movq (%r14), %rax
movq %r14, %rdi
callq *0x230(%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3971a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x389c8
leaq 0x10(%rsp), %rdi
callq 0x7c234
leaq 0x20(%rsp), %rdi
callq 0x7c234
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rsi
movq %r13, %rdi
movq %rax, %r13
jmp 0x31644
movq %rax, %r13
movl $0xb8, %esi
movq %rbp, %rdi
jmp 0x31644
movq %rax, %r13
pushq $0x60
popq %rsi
movq %r15, %rdi
callq 0x218f0
jmp 0x31693
movq %rax, %r13
pushq $0x70
popq %rsi
movq %r14, %rdi
jmp 0x31675
movq %rax, %r13
jmp 0x31689
jmp 0x3167c
movq %rax, %r13
movl $0x88, %esi
movq %rbx, %rdi
jmp 0x31675
jmp 0x3167c
movq %rax, %r13
pushq $0x68
popq %rsi
movq %r12, %rdi
callq 0x218f0
jmp 0x3167f
movq %rax, %r13
leaq 0x10(%rsp), %rdi
callq 0x7c234
leaq 0x20(%rsp), %rdi
callq 0x7c234
movq %r13, %rdi
callq 0x220e0