// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::ParseSUNCGMaterials(gaps::R3Scene*, gaps::RNSymbolTable<gaps::R2Texture*>&, gaps::RNArray<gaps::R3Material*> const&, gaps::RNArray<gaps::R3Material*>&, Json::Value*)
static int
ParseSUNCGMaterials(R3Scene *scene,
  RNSymbolTable<R2Texture *>& texture_symbol_table,
  const RNArray<R3Material *>& input_materials,
  RNArray<R3Material *>& output_materials,
  Json::Value *json_materials)
{
  // Parse JSON array of materials
  Json::Value *json_material, *json_value;
  for (Json::ArrayIndex index = 0; index < json_materials->size(); index++) {
    char material_name[1024] = { '\0' };
    char texture_name[1024] = { '\0' };
    char diffuse_string[1024] = { '\0' };
    if (!GetJsonArrayEntry(json_material, json_materials, index)) continue; 
    if (json_material->type() != Json::objectValue) continue;
    if (GetJsonObjectMember(json_value, json_material, "name"))
      strncpy(material_name, json_value->asString().c_str(), 1023);
    if (GetJsonObjectMember(json_value, json_material, "texture")) 
      strncpy(texture_name, json_value->asString().c_str(), 1023);
    if (GetJsonObjectMember(json_value, json_material, "diffuse")) 
      strncpy(diffuse_string, json_value->asString().c_str(), 1023);

    // Get input material
    R3Material *input_material = (input_materials.NEntries() > (int) index) ? input_materials.Kth(index) : NULL;
    const R3Brdf *input_brdf = (input_material) ? input_material->Brdf() : NULL;
    const R2Texture *input_texture = (input_material) ? input_material->Texture() : NULL;
    
    // Get/create output material
    R3Material *output_material = NULL;
    if (*diffuse_string || *texture_name) {
      // Create output brdf
      R3Brdf *output_brdf = NULL;
      if (input_brdf) output_brdf = new R3Brdf(*input_brdf);
      else { output_brdf = new R3Brdf(); output_brdf->SetDiffuse(RNwhite_rgb); }
      scene->InsertBrdf(output_brdf);
      if (*diffuse_string) {
        long int b = strtol(&diffuse_string[5], NULL, 16); diffuse_string[5] = '\0';
        long int g = strtol(&diffuse_string[3], NULL, 16); diffuse_string[3] = '\0';
        long int r = strtol(&diffuse_string[1], NULL, 16); diffuse_string[1] = '\0';
        RNRgb diffuse_rgb(r / 255.0, g / 255.0, b / 255.0);
        output_brdf->SetDiffuse(diffuse_rgb);
      }

      // Create output texture
      R2Texture *output_texture = NULL;
      if (*texture_name) {
        char texture_filename[2048];
        const char *texture_directory = "../../texture";
        sprintf(texture_filename, "%s/%s.png", texture_directory, texture_name);
        if (!RNFileExists(texture_filename)) sprintf(texture_filename, "%s/%s.jpg", texture_directory, texture_name);
        if (!texture_symbol_table.Find(texture_filename, &output_texture)) {
          if (input_texture) output_texture = new R2Texture(*input_texture);
          else output_texture = new R2Texture();
          R2Image *image = new R2Image();
          if (!image->Read(texture_filename)) return 0;
          output_texture->SetImage(image);
          output_texture->SetFilename(texture_filename);
          output_texture->SetName(texture_name);
          scene->InsertTexture(output_texture);
          texture_symbol_table.Insert(texture_filename, output_texture);
        }
      }
      else if (input_texture) {
        const char *texture_filename = input_texture->Filename();
        if (!texture_symbol_table.Find(texture_filename, &output_texture)) {
          output_texture = new R2Texture(*input_texture);
          scene->InsertTexture(output_texture);
          texture_symbol_table.Insert(texture_filename, output_texture);
        }
      }

      // Create output material
      output_material = new R3Material(output_brdf, output_texture);
      output_material->SetName(material_name);
      scene->InsertMaterial(output_material);
    }

    // Insert material into array of results
    output_materials.Insert(output_material);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x14e8, %rsp           # imm = 0x14E8
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq %r8, %rdi
callq 0x91428
movl $0x1, %ecx
testl %eax, %eax
je 0x35092
movq %r14, 0x8(%rsp)
leaq 0x38(%r15), %rax
movq %rax, 0x90(%rsp)
leaq 0x48(%r15), %rax
movq %rax, 0x28(%rsp)
movq %r15, 0x20(%rsp)
leaq 0x28(%r15), %rax
movq %rax, 0x80(%rsp)
leaq 0x10e0(%rsp), %r13
leaq 0xe0(%rsp), %r12
xorl %r14d, %r14d
movq %rbp, 0x88(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x21490
movl $0x400, %edx            # imm = 0x400
leaq 0xce0(%rsp), %rdi
xorl %esi, %esi
callq 0x21490
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x21490
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x348d9
testl %eax, %eax
je 0x35079
movq 0x98(%rsp), %r15
movq %r15, %rdi
callq 0x92988
cmpl $0x7, %eax
jne 0x35079
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
leaq 0xa9d48(%rip), %rdx     # 0xde77d
xorl %ecx, %ecx
callq 0x34828
testl %eax, %eax
je 0x34a92
movq 0x10(%rsp), %rsi
leaq 0x4e0(%rsp), %rdi
callq 0x8c0b8
movq 0x4e0(%rsp), %r12
movl $0x3ff, %edx            # imm = 0x3FF
movq %r13, %rdi
movq %r12, %rsi
callq 0x21430
leaq 0x4f0(%rsp), %rax
cmpq %rax, %r12
je 0x34a8a
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x218e0
leaq 0xe0(%rsp), %r12
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
leaq 0xa6ea8(%rip), %rdx     # 0xdb949
xorl %ecx, %ecx
callq 0x34828
testl %eax, %eax
leaq 0x4e0(%rsp), %r13
je 0x34b06
movq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x8c0b8
movq 0x4e0(%rsp), %r12
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0xce0(%rsp), %rdi
movq %r12, %rsi
callq 0x21430
leaq 0x4f0(%rsp), %rax
cmpq %rax, %r12
je 0x34afe
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x218e0
leaq 0xe0(%rsp), %r12
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
leaq 0xa6e26(%rip), %rdx     # 0xdb93b
xorl %ecx, %ecx
callq 0x34828
testl %eax, %eax
je 0x34b65
movq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x8c0b8
movq 0x4e0(%rsp), %r15
movl $0x3ff, %edx            # imm = 0x3FF
movq %r12, %rdi
movq %r15, %rsi
callq 0x21430
leaq 0x4f0(%rsp), %rax
cmpq %rax, %r15
je 0x34b65
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x218e0
movq 0x30(%rsp), %rax
cmpl %r14d, 0xc(%rax)
jle 0x34bd0
movq 0x30(%rsp), %rax
movq (%rax), %rax
movslq %r14d, %rcx
movq (%rax,%rcx,8), %rax
testq %rax, %rax
je 0x34bd0
movb 0xce0(%rsp), %cl
orb 0xe0(%rsp), %cl
je 0x34def
movq 0x18(%rax), %r15
movq 0x20(%rax), %rax
testq %r15, %r15
leaq 0x38(%rsp), %r12
je 0x34bef
movq %rax, 0x18(%rsp)
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x43df4
movq 0x8(%rsp), %r15
jmp 0x34c2c
movb 0xce0(%rsp), %al
orb 0xe0(%rsp), %al
movl $0x0, %eax
movl $0x0, %r13d
je 0x35063
movq %rax, 0x18(%rsp)
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
callq 0x43d48
movq %rbp, %rdi
leaq 0x1094a3(%rip), %rsi    # 0x13e0b8
callq 0x44356
movq 0x8(%rsp), %r15
leaq 0x4e0(%rsp), %r13
leaq 0x38(%rsp), %r12
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rbp)
movl 0x44(%rax), %edx
movl %edx, 0x10(%rbp)
movq 0x90(%rsp), %rdi
movq %rbp, %rsi
callq 0x87622
cmpb $0x0, 0xe0(%rsp)
je 0x34cfe
leaq 0xe5(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d10
movq %rax, %r15
xorl %r13d, %r13d
movb %r13b, 0xe5(%rsp)
leaq 0xe3(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d10
movq %rax, %r12
movb %r13b, 0xe3(%rsp)
leaq 0xe1(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d10
cvtsi2sd %rax, %xmm0
movb %r13b, 0xe1(%rsp)
leaq 0x4e0(%rsp), %r13
movsd 0xa5865(%rip), %xmm3    # 0xda530
divsd %xmm3, %xmm0
cvtsi2sd %r12, %xmm1
leaq 0x38(%rsp), %r12
divsd %xmm3, %xmm1
cvtsi2sd %r15, %xmm2
movq 0x8(%rsp), %r15
divsd %xmm3, %xmm2
movq %r13, %rdi
callq 0x87400
movq %rbp, %rdi
movq %r13, %rsi
callq 0x44356
cmpb $0x0, 0xce0(%rsp)
je 0x34da0
movq %r13, %rdi
leaq 0xa6c3b(%rip), %rsi     # 0xdb951
leaq 0xa6c26(%rip), %rdx     # 0xdb943
leaq 0xce0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
movq %r13, %rdi
callq 0x894d0
testl %eax, %eax
jne 0x34d58
movq %r13, %rdi
leaq 0xa6c19(%rip), %rsi     # 0xdb95b
leaq 0xa6bfa(%rip), %rdx     # 0xdb943
leaq 0xce0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r13, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x4e0, %rdx            # imm = 0x4E0
movq %r12, %rdi
movq %r13, %rsi
callq 0x39150
movq (%r15), %rdi
movq %r12, %rsi
callq 0x370a2
movq %rax, %r13
movq (%r15), %r12
addq $0x8, %r12
cmpq %r12, %rax
je 0x34df7
movq 0x40(%r13), %r15
jmp 0x34dfa
movq 0x18(%rsp), %rax
testq %rax, %rax
leaq 0xc0(%rsp), %r12
je 0x34e59
movq 0x18(%rax), %rsi
movq %r12, %rdi
movq %rsi, 0x78(%rsp)
movq %r13, %rdx
callq 0x36eb0
movq (%r15), %rdi
movq %r12, %rsi
callq 0x370a2
movq %rax, %r12
movq (%r15), %r13
addq $0x8, %r13
cmpq %r13, %rax
je 0x34e69
movq 0x40(%r12), %r15
jmp 0x34e6c
xorl %r13d, %r13d
jmp 0x35063
xorl %r15d, %r15d
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x34e16
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r12, %r13
leaq 0xe0(%rsp), %r12
jne 0x3500e
movl $0x50, %edi
callq 0x218c0
movq %rax, %r15
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x34f30
movq %r15, %rdi
xorl %edx, %edx
callq 0x4497a
leaq 0x4e0(%rsp), %r13
jmp 0x34f40
xorl %r15d, %r15d
leaq 0xe0(%rsp), %r12
jmp 0x3500e
xorl %r15d, %r15d
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x34e91
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r13, %r12
leaq 0xe0(%rsp), %r12
jne 0x3500e
movl $0x50, %edi
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x4497a
movq 0x20(%rsp), %rax
movq %rax, (%r15)
movl 0x54(%rax), %edx
movl %edx, 0x8(%r15)
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq 0x78(%rsp), %rsi
leaq 0x4e0(%rsp), %rdx
callq 0x36eb0
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r13, %rsi
callq 0x373c0
movq %r15, (%rax)
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x3500e
movq 0xb0(%rsp), %rsi
jmp 0x35006
movq %r15, %rdi
callq 0x44944
leaq 0x4e0(%rsp), %r13
movl $0x18, %edi
callq 0x218c0
movq %rax, %r12
movq %rax, %rdi
callq 0x82250
movq %r12, %rdi
movq %r13, %rsi
callq 0x82274
testl %eax, %eax
je 0x350a6
movq %r15, %rdi
movq %r12, %rsi
callq 0x44ca2
movq %r15, %rdi
movq %r13, %rsi
callq 0x44c6c
movq %r15, %rdi
leaq 0xce0(%rsp), %rsi
callq 0x44c36
movq 0x20(%rsp), %rax
movq %rax, (%r15)
movl 0x54(%rax), %edx
movl %edx, 0x8(%r15)
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r13, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x4e0, %rdx            # imm = 0x4E0
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x39150
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq 0x373c0
leaq 0xe0(%rsp), %r12
movq %r15, (%rax)
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x3500e
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x218e0
movl $0x30, %edi
callq 0x218c0
movq %rax, %r13
movq %rax, %rdi
movq %rbp, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x43b4a
movq %r13, %rdi
leaq 0x10e0(%rsp), %rsi
callq 0x43c9c
movq 0x20(%rsp), %rax
movq %rax, (%r13)
movl 0x34(%rax), %edx
movl %edx, 0x8(%r13)
movq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x87622
movq 0x88(%rsp), %rbp
movl 0xc(%rbp), %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x87622
leaq 0x10e0(%rsp), %r13
incl %r14d
movq %rbx, %rdi
callq 0x91428
cmpl %eax, %r14d
jb 0x349c0
movl $0x1, %ecx
movl %ecx, %eax
addq $0x14e8, %rsp           # imm = 0x14E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
jmp 0x35092
jmp 0x350d6
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x35172
movq 0xb0(%rsp), %rsi
jmp 0x3514b
jmp 0x35150
jmp 0x350d6
movq %rax, %rbx
movl $0x50, %esi
movq %r15, %rdi
jmp 0x3516d
jmp 0x35155
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x35172
movq 0x68(%rsp), %rsi
jmp 0x3514b
jmp 0x35150
movq %rax, %rbx
movl $0x18, %esi
movq %r12, %rdi
jmp 0x3516d
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x35172
movq 0xd0(%rsp), %rsi
jmp 0x3514b
jmp 0x35150
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x35172
movq 0x48(%rsp), %rsi
incq %rsi
jmp 0x3516d
movq %rax, %rbx
jmp 0x35172
movq %rax, %rbx
movl $0xb8, %esi
movq %rbp, %rdi
jmp 0x3516d
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
callq 0x218e0
movq %rbx, %rdi
callq 0x220c0