// File: /workspace/github_cpp_makefile_star2_O2/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 $0x14d8, %rsp           # imm = 0x14D8
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
xorl %r13d, %r13d
movq %rcx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x83dd2
movl %eax, %r14d
cmpl %eax, %r13d
jae 0x328b9
movl $0x400, %edx            # imm = 0x400
leaq 0x10d0(%rsp), %rdi
xorl %esi, %esi
callq 0x21490
movl $0x400, %edx            # imm = 0x400
leaq 0x4d0(%rsp), %rdi
xorl %esi, %esi
callq 0x21490
movl $0x400, %edx            # imm = 0x400
leaq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0x21490
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x322e6
testl %eax, %eax
je 0x328b1
movq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x84e4c
cmpl $0x7, %eax
jne 0x328b1
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x97302(%rip), %rdx     # 0xc970d
xorl %ecx, %ecx
callq 0x32249
testl %eax, %eax
je 0x3244d
movq 0x10(%rsp), %rsi
leaq 0x8d0(%rsp), %r15
movq %r15, %rdi
callq 0x80236
movq 0x8d0(%rsp), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0x10d0(%rsp), %rdi
callq 0x21440
movq %r15, %rdi
callq 0x213a0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x944bd(%rip), %rdx     # 0xc6919
xorl %ecx, %ecx
callq 0x32249
testl %eax, %eax
je 0x3249e
movq 0x10(%rsp), %rsi
leaq 0x8d0(%rsp), %r15
movq %r15, %rdi
callq 0x80236
movq 0x8d0(%rsp), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0x4d0(%rsp), %rdi
callq 0x21440
movq %r15, %rdi
callq 0x213a0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x9445e(%rip), %rdx     # 0xc690b
xorl %ecx, %ecx
callq 0x32249
testl %eax, %eax
je 0x324ef
movq 0x10(%rsp), %rsi
leaq 0x8d0(%rsp), %r15
movq %r15, %rdi
callq 0x80236
movq 0x8d0(%rsp), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0xd0(%rsp), %rdi
callq 0x21440
movq %r15, %rdi
callq 0x213a0
movq 0x40(%rsp), %rax
cmpl %r13d, 0xc(%rax)
jle 0x32513
movq (%rax), %rax
movslq %r13d, %rcx
movq (%rax,%rcx,8), %rax
testq %rax, %rax
je 0x32513
movq 0x18(%rax), %r15
movq 0x20(%rax), %rcx
jmp 0x32518
xorl %r15d, %r15d
xorl %ecx, %ecx
movb 0x4d0(%rsp), %al
orb 0xd0(%rsp), %al
je 0x32553
movq %rcx, 0x30(%rsp)
movl $0xb8, %edi
callq 0x218d0
movq %rax, %r12
testq %r15, %r15
je 0x3255b
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3f100
leaq 0x50(%rsp), %rbp
jmp 0x32579
xorl %r14d, %r14d
jmp 0x328a6
movq %r12, %rdi
xorl %esi, %esi
callq 0x3f056
leaq 0x50(%rsp), %rbp
movq %r12, %rdi
leaq 0xfcb64(%rip), %rsi     # 0x12f0d8
callq 0x3f616
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2788a
cmpb $0x0, 0xd0(%rsp)
je 0x3263e
leaq 0xd5(%rsp), %rdi
xorl %esi, %esi
movq %rbx, %rbp
pushq $0x10
popq %rbx
movl %ebx, %edx
callq 0x21d40
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movb %al, 0xd5(%rsp)
leaq 0xd3(%rsp), %rdi
xorl %esi, %esi
movl %ebx, %edx
callq 0x21d40
movq %r12, %r15
movq %rax, %r12
xorl %eax, %eax
movb %al, 0xd3(%rsp)
leaq 0xd1(%rsp), %rdi
xorl %esi, %esi
movl %ebx, %edx
movq %rbp, %rbx
callq 0x21d40
cvtsi2sd %rax, %xmm0
xorl %eax, %eax
movb %al, 0xd1(%rsp)
leaq 0x50(%rsp), %rbp
movsd 0x92f18(%rip), %xmm3    # 0xc5520
divsd %xmm3, %xmm0
cvtsi2sd %r12, %xmm1
movq %r15, %r12
divsd %xmm3, %xmm1
cvtsi2sdq 0x28(%rsp), %xmm2
divsd %xmm3, %xmm2
leaq 0x8d0(%rsp), %r15
movq %r15, %rdi
callq 0x7c03e
movq %r12, %rdi
movq %r15, %rsi
callq 0x3f616
andq $0x0, 0x8(%rsp)
cmpb $0x0, 0x4d0(%rsp)
je 0x32711
movq %r12, 0x28(%rsp)
leaq 0x8d0(%rsp), %r12
movq %r12, %rdi
leaq 0x942b8(%rip), %rsi     # 0xc6921
leaq 0x942a3(%rip), %rdx     # 0xc6913
leaq 0x4d0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
movq %r12, %rdi
callq 0x7de10
testl %eax, %eax
jne 0x326ab
movq %r12, %rdi
leaq 0x94296(%rip), %rsi     # 0xc692b
leaq 0x94277(%rip), %rdx     # 0xc6913
leaq 0x4d0(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x7(%rsp), %rdx
callq 0x3438a
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x3482e
movl %eax, %r15d
movq %rbp, %rdi
callq 0x213a0
testl %r15d, %r15d
jne 0x32862
pushq $0x50
popq %rdi
callq 0x218d0
movq %rax, %r15
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
je 0x327c2
movq %r15, %rdi
xorl %edx, %edx
callq 0x3fb22
jmp 0x327ca
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x32867
movq 0x18(%rax), %r14
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x8d0(%rsp), %rdx
callq 0x3438a
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x3482e
movl %eax, %r15d
movq %rbp, %rdi
callq 0x213a0
testl %r15d, %r15d
movq 0x30(%rsp), %rbp
jne 0x32867
pushq $0x50
popq %rdi
callq 0x218d0
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x3fb22
movq %r15, 0x8(%rsp)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x278d2
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
leaq 0x8d0(%rsp), %rdx
callq 0x3438a
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x3486a
movq %r15, %rdi
callq 0x213a0
jmp 0x32867
movq %r15, %rdi
callq 0x3faf2
movq %r15, 0x8(%rsp)
pushq $0x18
popq %rdi
callq 0x218d0
movq %rax, %r15
movq %rax, %rdi
callq 0x7761c
movq %r15, %rdi
movq %r12, %rsi
callq 0x77638
testl %eax, %eax
je 0x328b9
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x3fda8
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x3fd78
movq 0x8(%rsp), %rdi
leaq 0x4d0(%rsp), %rsi
callq 0x3fd48
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x278d2
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
leaq 0x7(%rsp), %rdx
callq 0x3438a
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3486a
movq %r14, %rdi
callq 0x213a0
movq 0x28(%rsp), %r12
pushq $0x30
popq %rdi
callq 0x218d0
movq %rax, %r14
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x3ee68
movq %r14, %rdi
leaq 0x10d0(%rsp), %rsi
callq 0x3efb0
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x27846
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2737a
incl %r13d
jmp 0x3237e
xorl %eax, %eax
cmpl %r14d, %r13d
setae %al
addq $0x14d8, %rsp           # imm = 0x14D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x3291d
jmp 0x32924
jmp 0x328e3
jmp 0x328e3
movq %rax, %rbx
pushq $0x50
jmp 0x3290a
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x3291d
jmp 0x32924
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x3291d
jmp 0x32924
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r15, %rdi
jmp 0x3293f
jmp 0x32929
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x213a0
jmp 0x32944
movq %rax, %rbx
jmp 0x32944
movq %rax, %rbx
movl $0xb8, %esi
movq %r12, %rdi
jmp 0x3293f
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x218f0
movq %rbx, %rdi
callq 0x220e0