// File: /workspace/github_cpp_makefile_star2_O1/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 0x33b03
movq %rdi, %r15
movq %rsi, %rbx
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbp
leaq 0x111800(%rip), %rsi    # 0x1452a0
leaq 0xa9bc4(%rip), %rdx     # 0xdd66b
movq %rax, %rdi
callq 0x443d8
movq %r15, 0x8(%rbp)
leaq 0x38(%r15), %rdi
movl 0x44(%r15), %edx
movl %edx, 0x10(%rbp)
movq %rbp, %rsi
callq 0x88752
movl $0x30, %edi
callq 0x218c0
movq %rax, %r13
leaq 0xa9b91(%rip), %rdx     # 0xdd66b
movq %rax, %rdi
movq %rbp, %rsi
callq 0x440f8
movq %r15, (%r13)
movl 0x34(%r15), %edx
addq $0x28, %r15
movl %edx, 0x8(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x88752
movq %rbx, %rsi
movslq 0x3c(%rsi), %rax
testq %rax, %rax
movq %rsi, 0x30(%rsp)
jle 0x33b38
movq 0x30(%rsi), %rcx
decq %rax
xorl %edx, %edx
xorl %ebx, %ebx
movq (%rcx,%rdx,8), %rsi
cmpq %r13, 0x8(%rsi)
cmoveq %rsi, %rbx
je 0x33b3a
leaq 0x1(%rdx), %rsi
cmpq %rdx, %rax
movq %rsi, %rdx
jne 0x33b1c
jmp 0x33b3a
xorl %ebx, %ebx
testq %rbx, %rbx
jne 0x33b57
movl $0x60, %edi
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3d9c4
movq %rbx, 0x18(%rsp)
movslq 0xc(%r12), %rax
testq %rax, %rax
jle 0x33b7f
movq (%r12), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl $0x0, 0x60(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x33b6c
leaq 0x20(%rsp), %rdi
callq 0x886bc
movq %rsp, %rdi
callq 0x886bc
cmpl $0x0, 0xc(%r14)
jle 0x33c4f
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
cmpl $0x0, 0x60(%rbp)
jne 0x33bea
movl $0x68, %edi
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x6a29a
movl 0xc(%rsp), %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x88752
movl 0xc(%rsp), %eax
movl %eax, 0x60(%rbp)
movslq 0x60(%rbp), %rax
movq (%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rax
movq %rax, 0x40(%rsp,%r15,8)
incq %r15
cmpq $0x3, %r15
jne 0x33bb1
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 0x6a800
movl 0x2c(%rsp), %edx
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x88752
incq %r13
movslq 0xc(%r14), %rax
cmpq %rax, %r13
jl 0x33ba7
movl $0x70, %edi
callq 0x218c0
movq 0x30(%rsp), %rbx
movq %rax, %r14
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
movq %rax, %rdi
callq 0x68918
cmpl $0x0, 0x14(%rsp)
je 0x33c97
leaq 0xac1c9(%rip), %rax     # 0xdfe48
movsd (%rax), %xmm0
mulsd 0xa884d(%rip), %xmm0    # 0xdc4d8
movq (%r14), %rax
movq %r14, %rdi
callq *0x230(%rax)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3dc6c
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x3ceca
movq %rsp, %rdi
callq 0x88726
leaq 0x20(%rsp), %rdi
callq 0x88726
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 0x33cf7
movq %rax, %r12
movl $0x60, %esi
movq %rbx, %rdi
jmp 0x33cf7
movq %rax, %r12
movl $0xb8, %esi
movq %rbp, %rdi
callq 0x218e0
jmp 0x33d48
movq %rax, %r12
movl $0x70, %esi
movq %r14, %rdi
jmp 0x33d2c
movq %rax, %r12
jmp 0x33d3e
jmp 0x33d33
movq %rax, %r12
movl $0x88, %esi
movq %rbp, %rdi
jmp 0x33d2c
jmp 0x33d33
movq %rax, %r12
movl $0x68, %esi
movq %rbx, %rdi
callq 0x218e0
jmp 0x33d36
movq %rax, %r12
movq %rsp, %rdi
callq 0x88726
leaq 0x20(%rsp), %rdi
callq 0x88726
movq %r12, %rdi
callq 0x220d0