// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq %r8, %rdi
callq 0x93324
testl %eax, %eax
sete %bpl
je 0x353ec
movq %r15, (%rsp)
movq %r14, 0x68(%rsp)
leaq 0x38(%r12), %rax
movq %rax, 0x50(%rsp)
leaq 0x48(%r12), %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x8(%rsp)
leaq 0x28(%r12), %rax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movq %rbx, 0x70(%rsp)
movl $0x400, %edx            # imm = 0x400
leaq 0x10e0(%rsp), %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
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
callq 0x21490
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x34c81
movl $0x4, %r13d
testl %eax, %eax
je 0x353c0
movq 0x78(%rsp), %r15
movq %r15, %rdi
callq 0x94a3e
cmpl $0x7, %eax
jne 0x353c0
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
leaq 0xab986(%rip), %rdx     # 0xe075d
xorl %ecx, %ecx
callq 0x34bd0
testl %eax, %eax
je 0x34e31
movq 0x18(%rsp), %rsi
leaq 0x4e0(%rsp), %rdi
callq 0x8d848
movq 0x4e0(%rsp), %r12
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0x10e0(%rsp), %rdi
movq %r12, %rsi
callq 0x21430
leaq 0x4f0(%rsp), %rax
cmpq %rax, %r12
je 0x34e31
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x218e0
movq %r13, %rdi
movq %r15, %rsi
leaq 0xa8afb(%rip), %rdx     # 0xdd939
xorl %ecx, %ecx
callq 0x34bd0
testl %eax, %eax
je 0x34e98
movq 0x18(%rsp), %rsi
leaq 0x4e0(%rsp), %rdi
callq 0x8d848
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 0x34e98
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x218e0
movq %r13, %rdi
movq %r15, %rsi
leaq 0xa8a86(%rip), %rdx     # 0xdd92b
xorl %ecx, %ecx
callq 0x34bd0
testl %eax, %eax
movq 0x68(%rsp), %r12
leaq 0x4e0(%rsp), %r13
je 0x34f07
movq 0x18(%rsp), %rsi
movq %r13, %rdi
callq 0x8d848
movq 0x4e0(%rsp), %r15
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0x21430
leaq 0x4f0(%rsp), %rax
cmpq %rax, %r15
je 0x34f07
movq 0x4f0(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x218e0
cmpl %r14d, 0xc(%r12)
jle 0x34f1b
movq (%r12), %rax
movslq %r14d, %rcx
movq (%rax,%rcx,8), %rax
jmp 0x34f1d
xorl %eax, %eax
testq %rax, %rax
je 0x34f28
movq 0x18(%rax), %r15
jmp 0x34f2b
xorl %r15d, %r15d
testq %rax, %rax
je 0x34f36
movq 0x20(%rax), %rcx
jmp 0x34f38
xorl %ecx, %ecx
movb 0xce0(%rsp), %al
orb 0xe0(%rsp), %al
je 0x34f73
movq %rcx, 0x10(%rsp)
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbx
testq %r15, %r15
leaq 0x28(%rsp), %r12
je 0x34f7b
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x443d8
jmp 0x34f94
xorl %r12d, %r12d
jmp 0x353ad
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4432c
movq %rbx, %rdi
leaq 0x10b129(%rip), %rsi    # 0x1400b8
callq 0x4493a
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rbx)
movl 0x44(%rax), %edx
movl %edx, 0x10(%rbx)
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x88752
cmpb $0x0, 0xe0(%rsp)
je 0x3505e
leaq 0xe5(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d20
movq %rax, %r15
xorl %r12d, %r12d
movb %r12b, 0xe5(%rsp)
leaq 0xe3(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d20
movq %rax, %r13
movb %r12b, 0xe3(%rsp)
leaq 0xe1(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x21d20
cvtsi2sd %rax, %xmm0
movb %r12b, 0xe1(%rsp)
leaq 0x28(%rsp), %r12
movsd 0xa74fb(%rip), %xmm3    # 0xdc528
divsd %xmm3, %xmm0
cvtsi2sd %r13, %xmm1
leaq 0x4e0(%rsp), %r13
divsd %xmm3, %xmm1
cvtsi2sd %r15, %xmm2
divsd %xmm3, %xmm2
movq %r13, %rdi
callq 0x88530
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4493a
movq %rbx, 0x58(%rsp)
cmpb $0x0, 0xce0(%rsp)
je 0x35109
movq %r13, %rdi
leaq 0xa88c6(%rip), %rsi     # 0xdd941
leaq 0xa88b1(%rip), %rdx     # 0xdd933
leaq 0xce0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
movq %r13, %rdi
callq 0x8a868
testl %eax, %eax
jne 0x350bd
movq %r13, %rdi
leaq 0xa88a4(%rip), %rsi     # 0xdd94b
leaq 0xa8885(%rip), %rdx     # 0xdd933
leaq 0xce0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x4e0, %rdx            # imm = 0x4E0
movq %r12, %rdi
movq %r13, %rsi
callq 0x395ec
movq (%rsp), %rbx
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x374be
movq %rax, %r13
movq (%rbx), %r12
addq $0x8, %r12
cmpq %r12, %rax
je 0x35157
movq 0x40(%r13), %r15
jmp 0x3515a
movq 0x10(%rsp), %rax
testq %rax, %rax
leaq 0xa0(%rsp), %r15
je 0x351b1
movq 0x18(%rax), %rbx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x372c4
movq (%rsp), %r12
movq (%r12), %rdi
movq %r15, %rsi
callq 0x374be
movq %rax, %r13
movq (%r12), %r12
addq $0x8, %r12
cmpq %r12, %rax
je 0x351b9
movq 0x40(%r13), %r15
jmp 0x351bc
xorl %r15d, %r15d
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x35176
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r12, %r13
jne 0x35354
movl $0x50, %edi
callq 0x218c0
movq %rax, %r15
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x35275
movq %r15, %rdi
xorl %edx, %edx
callq 0x44f56
leaq 0x4e0(%rsp), %r13
jmp 0x35285
xorl %r15d, %r15d
jmp 0x35354
xorl %r15d, %r15d
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x351e1
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r12, %r13
jne 0x35354
movl $0x50, %edi
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x44f56
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movl 0x54(%rax), %edx
movl %edx, 0x8(%r15)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x88752
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x4e0(%rsp), %rdx
callq 0x372c4
movq (%rsp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq 0x377ec
movq %r15, (%rax)
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x35354
movq 0x90(%rsp), %rsi
jmp 0x3534c
movq %r15, %rdi
callq 0x44f20
leaq 0x4e0(%rsp), %r13
movl $0x18, %edi
callq 0x218c0
movq %rax, %r12
movq %rax, %rdi
callq 0x8338c
movq %r12, %rdi
movq %r13, %rsi
callq 0x833b0
testl %eax, %eax
je 0x353e5
movq %r15, %rdi
movq %r12, %rsi
callq 0x45288
movq %r15, %rdi
movq %r13, %rsi
callq 0x45252
movq %r15, %rdi
leaq 0xce0(%rsp), %rsi
callq 0x4521c
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movl 0x54(%rax), %edx
movl %edx, 0x8(%r15)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x88752
leaq 0xd0(%rsp), %rbx
movq %rbx, 0xc0(%rsp)
movq %r13, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x4e0, %rdx            # imm = 0x4E0
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x395ec
movq (%rsp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq 0x377ec
movq %r15, (%rax)
movq 0xc0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x35354
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x218e0
movl $0x30, %edi
callq 0x218c0
movq %rax, %r12
movq %rax, %rdi
movq 0x58(%rsp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x44174
movq %r12, %rdi
leaq 0x10e0(%rsp), %rsi
callq 0x4427e
movq 0x8(%rsp), %rax
movq %rax, (%r12)
movl 0x34(%rax), %edx
movl %edx, 0x8(%r12)
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x88752
movb $0x1, %al
movl $0x1, %r13d
testb %al, %al
je 0x353c0
movq 0x60(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r12, %rsi
callq 0x88752
xorl %r13d, %r13d
testb $0x3, %r13b
movq 0x70(%rsp), %rbx
jne 0x353ec
incl %r14d
movq %rbx, %rdi
callq 0x93324
cmpl %eax, %r14d
setae %bpl
jb 0x34d55
jmp 0x353ec
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x353a3
movzbl %bpl, %eax
andl $0x1, %eax
addq $0x14e8, %rsp           # imm = 0x14E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x354cc
movq 0x90(%rsp), %rsi
jmp 0x354a5
jmp 0x354aa
jmp 0x35451
jmp 0x35451
movq %rax, %r14
movq 0xc0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x354cc
movq 0xd0(%rsp), %rsi
jmp 0x354a5
jmp 0x354aa
movq %rax, %r14
movl $0x50, %esi
movq %r15, %rdi
jmp 0x354c7
movq %rax, %r14
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x354cc
movq 0xb0(%rsp), %rsi
jmp 0x354a5
jmp 0x354aa
movq %rax, %r14
movl $0x18, %esi
jmp 0x354c4
jmp 0x354af
movq %rax, %r14
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x354cc
movq 0x38(%rsp), %rsi
incq %rsi
jmp 0x354c7
movq %rax, %r14
jmp 0x354cc
movq %rax, %r14
movl $0xb8, %esi
movq %rbx, %rdi
jmp 0x354c7
movq %rax, %r14
movl $0x30, %esi
movq %r12, %rdi
callq 0x218e0
movq %r14, %rdi
callq 0x220d0