// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadParseFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadParseFile(const char *filename, R3SceneNode *parent_node)
{
  // Get/set parent node
  if (!parent_node) parent_node = root;

  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "r"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Read header
  char buffer[4096];
  if (!fgets(buffer, 4096, fp)) {
    RNFail("Unable to read object parse file %s\n", filename);
    return 0;
  }

  // Check header
  if (strncmp(buffer, "OBJECT PARSE 1.0", 16)) {
    RNFail("Error in header of oject parse file %s\n", filename);
    return 0;
  }

  // Read file
  int line_number = 0;
  int assignment_index = 0;
  RNArray<R3Shape *> shapes;
  char mesh_directory[1024] = { '\0' };
  while (fgets(buffer, 2048, fp)) {
    // Check line
    line_number++;
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!bufferp) continue;
    if (*bufferp == '#') continue;

    // Parse line
    char keyword[1024];
    if (sscanf(bufferp, "%s", keyword) == (unsigned int) 1) {
      if (!strcmp(keyword, "A")) {
        // Parse assignment
        double score, m[16];
        int segmentation_index, model_index, dummy;
        if (sscanf(bufferp, "%s%d%d%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%d%d%d%d", keyword, 
          &segmentation_index, &model_index, 
          &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7], 
          &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15], 
          &score, &dummy, &dummy, &dummy, &dummy) != (unsigned int) 24) {
          RNFail("Error parsing assignment at line %d of %s\n", line_number, filename);
          return 0;
        }

        // Create node
        R3SceneNode *node = new R3SceneNode(this);
        parent_node->InsertChild(node);

        // Create shape element
        R3Shape *shape = shapes.Kth(model_index);
        R3Brdf *brdf = new R3Brdf(RNRgb(0.1 + score, 0.25 + score, 0.5 + score), 0.0, 0.5 + score);
        InsertBrdf(brdf);
        R3Material *material = new R3Material(brdf);
        InsertMaterial(material);
        R3SceneElement *element = new R3SceneElement();
        element->SetMaterial(material);
        element->InsertShape(shape);
        node->InsertElement(element);

        // Set node name
        char node_name[1024];
        sprintf(node_name, "A%d_M%d_S%03d", assignment_index++, model_index, (int) (1000*score));
        node->SetName(node_name);

        // Set node transformation
        R4Matrix matrix(m); 
        R3Affine affine(matrix, 0);
        node->SetTransformation(affine);
      }
      else if (!strcmp(keyword, "M")) {
        // Parse model
        int dummy;
        double cx, cy, cz, r, h;
        char model_name[1024], mesh_name[1024];
        if (sscanf(bufferp, "%s%d%lf%lf%lf%lf%lf%s%s%d%d%d%d%d", keyword, 
          &dummy, &cx, &cy, &cz, &r, &h, model_name, mesh_name, &dummy, &dummy, &dummy, &dummy, &dummy) != (unsigned int) 14) {
          RNFail("Error parsing model at line %d of %s\n", line_number, filename);
          return 0;
        }

        // Read mesh
        char mesh_filename[2048];
        R3Shape *shape = NULL;
        if (strcmp(mesh_name, "None")) {
          if (mesh_directory[0]) sprintf(mesh_filename, "%s/%s", mesh_directory, mesh_name);
          else sprintf(mesh_filename, "%s", mesh_name);
          shape = ReadMesh(mesh_filename);
          if (!shape) return 0;
        }
        else {
          shape = new R3Sphere(R3Point(0,0,0), 0);
        }

        // Insert shape into list
        shapes.Insert(shape);
      }
      else if (!strcmp(keyword, "MD")) {
        // Parse model directory
        if (sscanf(bufferp, "%s%s", keyword, mesh_directory) != (unsigned int) 2) {
          RNFail("Error parsing model directory at line %d of %s\n", line_number, filename);
          return 0;
        }
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28e8, %rsp           # imm = 0x28E8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
testq %rdx, %rdx
jne 0x2a546
movq (%r15), %rbx
leaq 0xa1856(%rip), %rsi     # 0xcbda3
movq %r14, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2a59c
movq %rax, %r12
leaq 0x18e0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x21f50
testq %rax, %rax
je 0x2a5a5
movdqa 0x18e0(%rsp), %xmm0
pcmpeqb 0x9afb8(%rip), %xmm0    # 0xc5540
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x2a5cc
leaq 0x9b9b3(%rip), %rdi     # 0xc5f4d
jmp 0x2a5ac
leaq 0x9b083(%rip), %rdi     # 0xc5626
jmp 0x2a5ac
leaq 0x9b96b(%rip), %rdi     # 0xc5f17
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x7e83e
movl %ebx, %eax
addq $0x28e8, %rsp           # imm = 0x28E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x18(%rsp)
movq %rbx, 0x38(%rsp)
movq %r14, 0x10(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x7c1c6
leaq 0x8e0(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x21490
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
leaq 0x18e0(%rsp), %rbx
movq %rbx, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x21f50
testq %rax, %rax
je 0x2aa68
incl %ebp
movsbl (%rbx), %r14d
testl %r14d, %r14d
je 0x2a646
movl %r14d, %edi
callq 0x210b0
testl %eax, %eax
je 0x2a640
incq %rbx
jmp 0x2a626
cmpb $0x23, %r14b
je 0x2a603
movq %rbx, %rdi
leaq 0xba60b(%rip), %rsi     # 0xe4c5b
leaq 0x4e0(%rsp), %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2a603
movzwl 0x4e0(%rsp), %eax
cmpl $0x4d, %eax
je 0x2a8f0
cmpl $0x41, %eax
jne 0x2a9ab
movq %rbx, %rdi
leaq 0x9b8ed(%rip), %rsi     # 0xc5f75
leaq 0x4e0(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x60(%rsp), %r9
xorl %eax, %eax
leaq 0x40(%rsp), %r10
pushq %r10
pushq %r10
pushq %r10
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
callq 0x21890
addq $0xa0, %rsp
cmpl $0x18, %eax
jne 0x2aa82
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218d0
movq %rax, %rbx
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x37a1e
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x3898c
movslq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %r15
movl $0xb8, %edi
callq 0x218d0
movq %rax, %r14
movsd 0x8(%rsp), %xmm2
movapd %xmm2, %xmm0
addsd 0x9a87c(%rip), %xmm0    # 0xc5030
movapd %xmm2, %xmm1
addsd 0x9ad18(%rip), %xmm1    # 0xc54d8
movsd 0x9a878(%rip), %xmm3    # 0xc5040
addsd %xmm3, %xmm2
leaq 0x10e0(%rsp), %r13
movq %r13, %rdi
callq 0x7c03e
movsd 0x8(%rsp), %xmm1
addsd 0x9a856(%rip), %xmm1    # 0xc5040
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r13, %rsi
movsd 0x9a81c(%rip), %xmm2    # 0xc5018
xorl %edx, %edx
callq 0x3f1a6
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x2788a
pushq $0x30
popq %rdi
callq 0x218d0
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3edf2
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x27846
pushq $0x60
popq %rdi
callq 0x218d0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x394c0
movq %r14, %rdi
movq %r13, %rsi
callq 0x39714
movq %r14, %rdi
movq %r15, %rsi
callq 0x3971a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x389c8
movl 0x20(%rsp), %ecx
movsd 0x8(%rsp), %xmm0
mulsd 0x9ac63(%rip), %xmm0    # 0xc54e0
cvttsd2si %xmm0, %r8d
leaq 0x10e0(%rsp), %r14
movq %r14, %rdi
leaq 0x9b74e(%rip), %rsi     # 0xc5fe2
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x21140
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38b2e
leaq 0x460(%rsp), %r15
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
callq 0x6886c
leaq 0xce0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x66808
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38b02
incl 0x4(%rsp)
movq %r14, %rdi
callq 0x66856
jmp 0x2a603
movq %rbx, %rdi
leaq 0x9b6f6(%rip), %rsi     # 0xc5ff0
leaq 0x4e0(%rsp), %rdx
leaq 0x54(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x48(%rsp), %r9
xorl %eax, %eax
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
leaq 0x88(%rsp), %rbx
pushq %rbx
leaq 0xd10(%rsp), %r10
pushq %r10
leaq 0x90(%rsp), %r10
pushq %r10
leaq 0x80(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x50, %rsp
cmpl $0xe, %eax
jne 0x2aa9b
movq %rbx, %rdi
leaq 0x9b6d6(%rip), %rsi     # 0xc6038
pushq $0x5
popq %rdx
callq 0x21920
testl %eax, %eax
je 0x2a9f9
cmpb $0x0, 0x8e0(%rsp)
je 0x2aa31
leaq 0x10e0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x9ac8e(%rip), %rsi     # 0xc5620
leaq 0x8e0(%rsp), %rdx
leaq 0x60(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
jmp 0x2aa46
leaq 0x4e0(%rsp), %rdi
leaq 0x9b683(%rip), %rsi     # 0xc603d
pushq $0x3
popq %rdx
callq 0x21920
testl %eax, %eax
jne 0x2a603
movq %rbx, %rdi
leaq 0x9c8ea(%rip), %rsi     # 0xc72be
leaq 0x4e0(%rsp), %rdx
leaq 0x8e0(%rsp), %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
je 0x2a603
jmp 0x2aab2
pushq $0x28
popq %rdi
callq 0x218d0
movq %rax, %rbx
pxor %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x460(%rsp), %r14
movq %r14, %rdi
callq 0x6b874
pxor %xmm0, %xmm0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x619a0
jmp 0x2aa56
leaq 0x10e0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x21810
movq %rbx, %rdi
callq 0x3169b
movq %rax, %rbx
testq %rax, %rax
je 0x2aac7
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x2737a
jmp 0x2a603
movq %r12, %rdi
callq 0x218c0
pushq $0x1
popq %rbx
leaq 0x28(%rsp), %rdi
callq 0x7c234
jmp 0x2a5b8
leaq 0x9b52e(%rip), %rdi     # 0xc5fb7
xorl %ebx, %ebx
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2aa73
leaq 0x9b570(%rip), %rdi     # 0xc6012
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2aac7
leaq 0x9b587(%rip), %rdi     # 0xc6040
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
xorl %ebx, %ebx
jmp 0x2aa73
jmp 0x2ab2b
jmp 0x2ab2b
jmp 0x2ab2b
movq %rax, %r15
pushq $0x28
popq %rsi
jmp 0x2ab0c
movq %rax, %r15
leaq 0xce0(%rsp), %rdi
callq 0x66856
jmp 0x2ab2e
jmp 0x2ab2b
jmp 0x2ab2b
jmp 0x2ab2b
movq %rax, %r15
pushq $0x60
popq %rsi
jmp 0x2ab1f
movq %rax, %r15
pushq $0x30
popq %rsi
movq %r13, %rdi
jmp 0x2ab22
movq %rax, %r15
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
jmp 0x2ab22
jmp 0x2ab2b
jmp 0x2ab2b
jmp 0x2ab2b
movq %rax, %r15
movl $0xb8, %esi
movq %r14, %rdi
callq 0x218f0
jmp 0x2ab2e
jmp 0x2ab2b
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x7c234
movq %r15, %rdi
callq 0x220e0