// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::FindPrincetonMaterialAndElement(gaps::R3Scene*, gaps::R3SceneNode*, gaps::RNArray<gaps::R3Material*> const&, int, gaps::R3Material*&, gaps::R3Material*&, gaps::R3SceneElement*&)
static int
FindPrincetonMaterialAndElement(R3Scene *scene, R3SceneNode *node,
  const RNArray<R3Material *>& materials, int m, R3Material *&default_material,
  R3Material *& material, R3SceneElement *& element)
{
  // Find material from m
  material = NULL;
  if (m >= 0) {
    if (m < materials.NEntries()) material = materials[m];
    else return 0;
  }
  else {
    // Get material from 
    material = default_material;
    if (!material) {
      R3Brdf *brdf = new R3Brdf(R3default_brdf);
      scene->InsertBrdf(brdf);
      material = new R3Material(brdf);
      scene->InsertMaterial(material);
      default_material = material;
    }
  }

  // Find element with that material
  element = NULL;
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *e = node->Element(i);
    if (e->Material() == material) { element = e; break; }
  }

  // Create element if none found
  if (!element) {
    element = new R3SceneElement(material);
    node->InsertElement(element);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x88, %rsp
movq 0x90(%rsp), %rax
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movl %ecx, 0x64(%rsp)
movq %r8, 0x58(%rsp)
movq %r9, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x0, 0x64(%rsp)
jl 0x3cf2b
movl 0x64(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x68(%rsp), %rdi
callq 0x2b8b0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x3cf16
movq 0x68(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x4e650
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
jmp 0x3cf26
movl $0x0, 0x84(%rsp)
jmp 0x3d124
jmp 0x3d026
movq 0x58(%rsp), %rax
movq (%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x50(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x3d024
movl $0xb8, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
leaq 0x1bf68a(%rip), %rsi    # 0x1fc5f0
xorl %eax, %eax
movl %eax, %edx
callq 0x645c0
jmp 0x3cf71
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x78(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2ccd0
movl $0x30, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63e20
jmp 0x3cfaf
movq 0x18(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x78(%rsp), %rdi
movq 0x50(%rsp), %rax
movq (%rax), %rsi
callq 0x2cbd0
movq 0x50(%rsp), %rax
movq (%rax), %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
jmp 0x3d024
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x3d133
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x3d133
jmp 0x3d026
movq 0x90(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x70(%rsp), %rdi
callq 0x2bba0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x3d0a1
movq 0x70(%rsp), %rdi
movl 0x38(%rsp), %esi
callq 0x2bbc0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x48b50
movq 0x50(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x3d092
movq 0x30(%rsp), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
jmp 0x3d0a1
jmp 0x3d094
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x3d03d
movq 0x90(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x3d119
movl $0x60, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq (%rax), %rsi
callq 0x58f20
jmp 0x3d0d3
movq 0x8(%rsp), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rax
movq (%rax), %rsi
callq 0x57510
jmp 0x3d119
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0x60, %esi
callq 0x22a00
jmp 0x3d133
movl $0x1, 0x84(%rsp)
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x23290
nopl (%rax)