// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2908, %rsp           # imm = 0x2908
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
testq %rdx, %rdx
jne 0x2b8dc
movq (%r15), %r14
leaq 0xb7760(%rip), %rsi     # 0xe3043
movq %rbx, %rdi
callq 0x21ac0
testq %rax, %rax
je 0x2b932
movq %rax, %r12
leaq 0x1900(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x21f40
testq %rax, %rax
je 0x2b93b
movdqa 0x1900(%rsp), %xmm0
pcmpeqb 0xb0c22(%rip), %xmm0    # 0xdc540
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x2b962
leaq 0xb163d(%rip), %rdi     # 0xdcf6d
jmp 0x2b942
leaq 0xb0d0d(%rip), %rdi     # 0xdc646
jmp 0x2b942
leaq 0xb15f5(%rip), %rdi     # 0xdcf37
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b53a
movl %ebp, %eax
addq $0x2908, %rsp           # imm = 0x2908
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x18(%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x58(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x886bc
leaq 0x900(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x21490
leaq 0x1900(%rsp), %r14
movq %r14, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x21f40
testq %rax, %rax
je 0x2bf06
movq 0x8(%rsp), %rax
leaq 0x38(%rax), %rcx
movq %rcx, 0x48(%rsp)
addq $0x28, %rax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
incl %ebp
movb 0x1900(%rsp), %r13b
movq %r14, %rbx
testb %r13b, %r13b
je 0x2ba11
movq %rbx, %r14
movsbl %r13b, %edi
callq 0x210b0
testl %eax, %eax
je 0x2ba01
movb 0x1(%r14), %r13b
incq %r14
testb %r13b, %r13b
jne 0x2b9e6
jmp 0x2ba11
movl $0x2, %r15d
cmpb $0x23, %r13b
je 0x2bee4
xorl %r15d, %r15d
movq %r14, %rdi
leaq 0xd053d(%rip), %rsi     # 0xfbf5b
leaq 0x500(%rsp), %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2bee4
movzwl 0x500(%rsp), %eax
cmpl $0x4d, %eax
je 0x2bb52
cmpl $0x41, %eax
jne 0x2bc15
movq %r14, %rdi
leaq 0xb153b(%rip), %rsi     # 0xdcf95
leaq 0x500(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x80(%rsp), %r9
xorl %eax, %eax
leaq 0x60(%rsp), %r10
pushq %r10
pushq %r10
pushq %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
leaq 0x120(%rsp), %r10
pushq %r10
callq 0x21890
addq $0xa0, %rsp
movl %eax, %r14d
cmpl $0x18, %eax
je 0x2bc81
leaq 0xb1498(%rip), %rdi     # 0xdcfd7
movl %ebp, %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2be44
movq %r14, %rdi
leaq 0xb14b4(%rip), %rsi     # 0xdd010
leaq 0x500(%rsp), %rdx
leaq 0x74(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x68(%rsp), %r9
xorl %eax, %eax
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
leaq 0xa8(%rsp), %r10
pushq %r10
leaq 0xd30(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x50, %rsp
cmpl $0xe, %eax
jne 0x2be54
movl 0x80(%rsp), %eax
movl $0x656e6f4e, %ecx       # imm = 0x656E6F4E
xorl %ecx, %eax
movzbl 0x84(%rsp), %ecx
orl %eax, %ecx
je 0x2be71
cmpb $0x0, 0x900(%rsp)
je 0x2beab
leaq 0x1100(%rsp), %r14
movq %r14, %rdi
leaq 0xb0a47(%rip), %rsi     # 0xdc640
leaq 0x900(%rsp), %rdx
leaq 0x80(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
jmp 0x2bec3
movl 0x500(%rsp), %eax
movl $0x444d, %ecx           # imm = 0x444D
xorl %ecx, %eax
movzbl 0x502(%rsp), %ecx
orw %ax, %cx
jne 0x2bee4
xorl %r15d, %r15d
movq %r14, %rdi
leaq 0xb2700(%rip), %rsi     # 0xde341
leaq 0x500(%rsp), %rdx
leaq 0x900(%rsp), %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
je 0x2bee4
movl $0x1, %r15d
leaq 0xb13f2(%rip), %rdi     # 0xdd060
movl %ebp, %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2bee4
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x3bab8
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x3ce8e
movl %r14d, 0x2c(%rsp)
movslq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp)
movl $0xb8, %edi
callq 0x218c0
movq %rax, %r14
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
addsd 0xb0359(%rip), %xmm0    # 0xdc038
movapd %xmm2, %xmm1
addsd 0xb07ed(%rip), %xmm1    # 0xdc4d8
movsd 0xb032d(%rip), %xmm3    # 0xdc020
addsd %xmm3, %xmm2
leaq 0x1100(%rsp), %r13
movq %r13, %rdi
callq 0x88530
movsd 0x10(%rsp), %xmm1
addsd 0xb030b(%rip), %xmm1    # 0xdc020
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r13, %rsi
movsd 0xb02f1(%rip), %xmm2    # 0xdc018
xorl %edx, %edx
callq 0x44488
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r14)
movl 0x44(%rax), %edx
movl %edx, 0x10(%r14)
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x88752
movl $0x30, %edi
callq 0x218c0
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x440f8
movq 0x8(%rsp), %rax
movq %rax, (%r13)
movl 0x34(%rax), %edx
movl %edx, 0x8(%r13)
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x88752
movl $0x60, %edi
callq 0x218c0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x3d9c4
movq %r14, %rdi
movq %r13, %rsi
callq 0x3dc66
movq %r14, %rdi
movq 0x50(%rsp), %rsi
callq 0x3dc6c
movq %r15, %rdi
movq %r14, %rsi
callq 0x3ceca
movl 0x20(%rsp), %ecx
movsd 0x10(%rsp), %xmm0
mulsd 0xb0712(%rip), %xmm0    # 0xdc4e0
cvttsd2si %xmm0, %r8d
leaq 0x1100(%rsp), %r14
movq %r14, %rdi
leaq 0xb121d(%rip), %rsi     # 0xdd002
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x21140
movq %r15, %rdi
movq %r14, %rsi
callq 0x3d0ca
leaq 0x480(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
callq 0x72caa
leaq 0xd00(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x7082a
movq %r15, %rdi
movq %r14, %rsi
callq 0x3d06c
incl 0x4(%rsp)
movq %r14, %rdi
callq 0x70878
movl 0x2c(%rsp), %r14d
xorl %r15d, %r15d
cmpl $0x18, %r14d
setne %r15b
jmp 0x2bee4
leaq 0xb11d7(%rip), %rdi     # 0xdd032
movl %ebp, %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
movl $0x1, %r15d
jmp 0x2bee4
movl $0x28, %edi
callq 0x218c0
movq %rax, %r14
pxor %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x480(%rsp), %r13
movq %r13, %rdi
callq 0x762ba
pxor %xmm0, %xmm0
movq %r14, %rdi
movq %r13, %rsi
callq 0x6b536
jmp 0x2bed3
leaq 0x1100(%rsp), %r14
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
callq 0x21800
movq %r14, %rdi
callq 0x33d50
movq %rax, %r14
testq %rax, %rax
je 0x2be69
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x88752
testb $0x1, %r15b
jne 0x2bf15
movq %rbx, %r14
movq %rbx, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x21f40
testq %rax, %rax
jne 0x2b9d1
movq %r12, %rdi
callq 0x218b0
movl $0x1, %ebp
jmp 0x2bf17
xorl %ebp, %ebp
leaq 0x30(%rsp), %rdi
callq 0x88726
jmp 0x2b94e
jmp 0x2bf8c
movq %rax, %rbx
movl $0x28, %esi
jmp 0x2bf80
jmp 0x2bf8c
jmp 0x2bf8c
movq %rax, %rbx
leaq 0xd00(%rsp), %rdi
callq 0x70878
jmp 0x2bf8f
jmp 0x2bf8c
jmp 0x2bf8c
jmp 0x2bf8c
movq %rax, %rbx
movl $0x60, %esi
jmp 0x2bf80
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
jmp 0x2bf83
movq %rax, %rbx
movl $0x1b0, %esi            # imm = 0x1B0
movq %r15, %rdi
jmp 0x2bf83
jmp 0x2bf8c
jmp 0x2bf8c
jmp 0x2bf8c
movq %rax, %rbx
movl $0xb8, %esi
movq %r14, %rdi
callq 0x218e0
jmp 0x2bf8f
jmp 0x2bf8c
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x88726
movq %rbx, %rdi
callq 0x220d0
nop