// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %r9d, 0x14(%rsp)
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %r13
testq %rdx, %rdx
jne 0x337a6
movq %rdi, %r15
movq %rsi, %rbx
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbp
leaq 0x10fb5d(%rip), %rsi    # 0x1432a0
leaq 0xa7f31(%rip), %rdx     # 0xdb67b
movq %rax, %rdi
callq 0x43df4
movq %r15, 0x8(%rbp)
leaq 0x38(%r15), %rdi
movl 0x44(%r15), %edx
movl %edx, 0x10(%rbp)
movq %rbp, %rsi
callq 0x87622
movl $0x30, %edi
callq 0x218c0
movq %rax, %r13
leaq 0xa7efe(%rip), %rdx     # 0xdb67b
movq %rax, %rdi
movq %rbp, %rsi
callq 0x43ace
movq %r15, (%r13)
movl 0x34(%r15), %edx
addq $0x28, %r15
movl %edx, 0x8(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x87622
movq %rbx, %rsi
movq %rsi, 0x18(%rsp)
movslq 0x3c(%rsi), %rax
testq %rax, %rax
jle 0x337d1
movq 0x18(%rsp), %rcx
movq 0x30(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rbx
cmpq %r13, 0x8(%rbx)
je 0x337e9
incq %rdx
cmpq %rdx, %rax
jne 0x337bf
movl $0x60, %edi
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3d514
movq %rbx, 0x20(%rsp)
movslq 0xc(%r12), %rax
testq %rax, %rax
jle 0x33811
movq (%r12), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl $0x0, 0x60(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x337fe
leaq 0x28(%rsp), %rdi
callq 0x8758c
movq %rsp, %rdi
callq 0x8758c
cmpl $0x0, 0xc(%r14)
jle 0x338e0
xorl %r13d, %r13d
movq %rsp, %r12
movq %r14, 0x38(%rsp)
movq (%r14), %rax
movq (%rax,%r13,8), %r14
xorl %r15d, %r15d
movq 0x8(%r14,%r15,8), %rbp
movl 0x60(%rbp), %eax
testl %eax, %eax
jne 0x3387d
movl $0x68, %edi
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x694da
movl 0xc(%rsp), %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x87622
movl 0xc(%rsp), %eax
movl %eax, 0x60(%rbp)
movq (%rsp), %rcx
cltq
movq -0x8(%rcx,%rax,8), %rax
movq %rax, 0x40(%rsp,%r15,8)
incq %r15
cmpq $0x3, %r15
jne 0x33843
movl $0x88, %edi
callq 0x218c0
movq 0x38(%rsp), %r14
movq %rax, %rbp
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq %rax, %rdi
callq 0x69a40
movl 0x34(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x87622
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x33839
movl $0x70, %edi
callq 0x218c0
movq 0x18(%rsp), %rbx
movq %rax, %r14
movq %rsp, %rsi
leaq 0x28(%rsp), %rdx
movq %rax, %rdi
callq 0x67b88
cmpl $0x0, 0x14(%rsp)
je 0x33928
leaq 0xaa558(%rip), %rax     # 0xdde68
movsd (%rax), %xmm0
mulsd 0xa6bbc(%rip), %xmm0    # 0xda4d8
movq (%r14), %rax
movq %r14, %rdi
callq *0x230(%rax)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x3d7bc
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x3c9ce
movq %rsp, %rdi
callq 0x875f6
leaq 0x28(%rsp), %rdi
callq 0x875f6
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movl $0x30, %esi
movq %r13, %rdi
jmp 0x33988
movq %rax, %r12
movl $0xb8, %esi
movq %rbp, %rdi
jmp 0x33988
movq %rax, %r12
movl $0x60, %esi
movq %rbx, %rdi
callq 0x218e0
jmp 0x339d9
movq %rax, %r12
movl $0x70, %esi
movq %r14, %rdi
jmp 0x339bd
movq %rax, %r12
jmp 0x339cf
jmp 0x339c4
movq %rax, %r12
movl $0x88, %esi
movq %rbp, %rdi
jmp 0x339bd
jmp 0x339c4
movq %rax, %r12
movl $0x68, %esi
movq %rbx, %rdi
callq 0x218e0
jmp 0x339c7
movq %rax, %r12
movq %rsp, %rdi
callq 0x875f6
leaq 0x28(%rsp), %rdi
callq 0x875f6
movq %r12, %rdi
callq 0x220c0