// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/ply.cpp
// Function: ply_read
PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names)
{
  int i,j;
  PlyFile *plyfile;
  const int max_line_size = 4096;
  char orig_line[max_line_size];
  const int max_words = 1024;
  char *words[max_words];
  char word_buffer[max_line_size];
  int nwords = 0;
  char **elist;
  PlyElement *elem;

  /* check for NULL file pointer */
  if (fp == NULL)
    return (NULL);

  if (native_binary_type == -1)
     get_native_binary_type();
  if (!types_checked)
     check_types();
  
  /* create record for this object */

  plyfile = (PlyFile *) myalloc (sizeof (PlyFile));
  plyfile->nelems = 0;
  plyfile->comments = NULL;
  plyfile->num_comments = 0;
  plyfile->obj_info = NULL;
  plyfile->num_obj_info = 0;
  plyfile->fp = fp;
  plyfile->other_elems = NULL;

  /* read and parse the file's header */

  /* read in a line */
  if (!fgets (orig_line, max_line_size, plyfile->fp)) {
    fprintf(stderr, "Unable to read header from ply file\n");
    return (NULL);
  }

  /* break line into words */
  if (!get_words(orig_line, max_line_size, max_words, word_buffer, words, &nwords)) {
    fprintf(stderr, "Unable to parse first line from ply file\n");
    return (NULL);
  }

  // Check for ply keyword
  if (!equal_strings (words[0], "ply")) {
    fprintf(stderr, "No ply keyword at beginning of ply file\n");
    return (NULL);
  }

  // Parse rest of header
  while (true) {
    /* read in a line */
    if (!fgets (orig_line, max_line_size, plyfile->fp)) {
      fprintf(stderr, "Unable to read line from header of ply file\n");
      return (NULL);
    }

    /* break line into words */
    if (!get_words(orig_line, max_line_size, max_words, word_buffer, words, &nwords)) {
      fprintf(stderr, "Unable to parse words of line in header of ply file\n");
      return (NULL);
    }

    /* parse words */
    if (equal_strings (words[0], "format")) {
      if (nwords != 3) {
	return (NULL);
      }
      if (equal_strings (words[1], "ascii"))
        plyfile->file_type = PLY_ASCII;
      else if (equal_strings (words[1], "binary_big_endian"))
        plyfile->file_type = PLY_BINARY_BE;
      else if (equal_strings (words[1], "binary_little_endian"))
        plyfile->file_type = PLY_BINARY_LE;
      else {
        fprintf(stderr, "Unrecognized format (%s) in ply file\n", words[1]);
        return (NULL);
      }
      plyfile->version = atof (words[2]);
    }
    else if (equal_strings (words[0], "element"))
      add_element (plyfile, words);
    else if (equal_strings (words[0], "property"))
      add_property (plyfile, words);
    else if (equal_strings (words[0], "comment"))
      add_comment (plyfile, orig_line);
    else if (equal_strings (words[0], "obj_info"))
      add_obj_info (plyfile, orig_line);
    else if (equal_strings (words[0], "end_header")) {
      break;
    }
  }

  /* create tags for each property of each element, to be used */
  /* later to say whether or not to store each property for the user */

  for (i = 0; i < plyfile->nelems; i++) {
    elem = plyfile->elems[i];
    elem->store_prop = (char *) myalloc (sizeof (char) * elem->nprops);
    for (j = 0; j < elem->nprops; j++)
      elem->store_prop[j] = DONT_STORE_PROP;
    elem->other_offset = NO_OTHER_PROPS; /* no "other" props by default */
  }

  /* set return values about the elements */

  elist = (char **) myalloc (sizeof (char *) * plyfile->nelems);
  for (i = 0; i < plyfile->nelems; i++)
    elist[i] = my_strdup (plyfile->elems[i]->name);

  *elem_names = elist;
  *nelems = plyfile->nelems;

  /* return a pointer to the file's information */

  return (plyfile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4028, %rsp           # imm = 0x4028
andl $0x0, 0xc(%rsp)
testq %rdi, %rdi
je 0x39f1b
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
cmpb $0x0, 0x4a1ed2(%rip)    # 0x4dbb78
jne 0x39caf
movb $0x1, 0x4a1ec9(%rip)    # 0x4dbb78
cmpb $0x0, 0x4a1ec6(%rip)    # 0x4dbb7c
jne 0x39cbd
callq 0x38ef5
leaq 0x5fbd7(%rip), %rdx     # 0x9989b
pushq $0x50
popq %rdi
movl $0x2e6, %esi            # imm = 0x2E6
callq 0x38f69
movq %rax, %rbx
andl $0x0, 0x10(%rax)
andq $0x0, 0x28(%rax)
andl $0x0, 0x20(%rax)
andq $0x0, 0x38(%rax)
andl $0x0, 0x30(%rax)
movq %r13, (%rax)
andq $0x0, 0x48(%rax)
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdx
callq 0xe760
testq %rax, %rax
je 0x39eea
leaq 0x20(%rsp), %rdi
leaq 0x3020(%rsp), %rcx
leaq 0x1020(%rsp), %r14
leaq 0xc(%rsp), %r9
movl $0x1000, %esi           # imm = 0x1000
movl $0x400, %edx            # imm = 0x400
movq %r14, %r8
callq 0x3a003
movq (%r14), %rdi
leaq 0x595da(%rip), %rsi     # 0x9331d
callq 0x3a099
testl %eax, %eax
je 0x39eff
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x20(%rsp), %r12
leaq 0x1020(%rsp), %rbp
leaq 0xc(%rsp), %r14
leaq 0x5fd67(%rip), %r15     # 0x99ada
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdx
callq 0xe760
testq %rax, %rax
je 0x39ed5
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movl $0x400, %edx            # imm = 0x400
leaq 0x3020(%rsp), %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x3a003
movq 0x1020(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3a099
testl %eax, %eax
je 0x39e3a
cmpl $0x3, 0xc(%rsp)
jne 0x39f1b
movq 0x1028(%rsp), %r13
movq %r13, %rdi
leaq 0x5fd01(%rip), %rsi     # 0x99ae1
callq 0x3a099
pushq $0x1
popq %rcx
testl %eax, %eax
jne 0x39e1c
movq %r13, %rdi
leaq 0x5fcf1(%rip), %rsi     # 0x99ae7
callq 0x3a099
pushq $0x2
popq %rcx
testl %eax, %eax
jne 0x39e1c
movq %r13, %rdi
leaq 0x5fced(%rip), %rsi     # 0x99af9
callq 0x3a099
pushq $0x3
popq %rcx
testl %eax, %eax
je 0x39fe1
movl %ecx, 0x8(%rbx)
movq 0x1030(%rsp), %rdi
callq 0xe5a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rbx)
jmp 0x39ecd
movq %r13, %rdi
leaq 0x5fcf0(%rip), %rsi     # 0x99b34
callq 0x3a099
testl %eax, %eax
je 0x39e5a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3a0bb
jmp 0x39ecd
movq %r13, %rdi
leaq 0x5fcd8(%rip), %rsi     # 0x99b3c
callq 0x3a099
testl %eax, %eax
je 0x39e7a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3a143
jmp 0x39ecd
movq %r13, %rdi
leaq 0x5fcc1(%rip), %rsi     # 0x99b45
callq 0x3a099
testl %eax, %eax
je 0x39e9a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a218
jmp 0x39ecd
movq %r13, %rdi
leaq 0x5fca9(%rip), %rsi     # 0x99b4d
callq 0x3a099
testl %eax, %eax
je 0x39eba
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a232
jmp 0x39ecd
movq %r13, %rdi
leaq 0x5fc92(%rip), %rsi     # 0x99b56
callq 0x3a099
testl %eax, %eax
jne 0x39f32
movq (%rbx), %r13
jmp 0x39d73
movq 0xa00e4(%rip), %rax     # 0xd9fc0
movq (%rax), %rcx
leaq 0x5fbc7(%rip), %rdi     # 0x99aad
pushq $0x2c
jmp 0x39f12
movq 0xa00cf(%rip), %rax     # 0xd9fc0
movq (%rax), %rcx
leaq 0x5fb64(%rip), %rdi     # 0x99a5f
pushq $0x24
jmp 0x39f12
movq 0xa00ba(%rip), %rax     # 0xd9fc0
movq (%rax), %rcx
leaq 0x5fb74(%rip), %rdi     # 0x99a84
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0xe8e0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x4028, %rsp           # imm = 0x4028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%rbx), %r15d
xorl %r13d, %r13d
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
leaq 0x5f94e(%rip), %r14     # 0x9989b
cmpq %r12, %r13
je 0x39f8f
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rbp
movl 0x10(%rbp), %edi
movl $0x334, %esi            # imm = 0x334
movq %r14, %rdx
callq 0x38f69
movq %rax, 0x20(%rbp)
xorl %eax, %eax
movslq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jge 0x39f86
movq 0x20(%rbp), %rcx
movb $0x0, (%rcx,%rax)
incq %rax
jmp 0x39f70
orl $-0x1, 0x28(%rbp)
incq %r13
jmp 0x39f4d
leal (,%r15,8), %edi
leaq 0x5f8fd(%rip), %rdx     # 0x9989b
movl $0x33c, %esi            # imm = 0x33C
callq 0x38f69
movq %rax, %r14
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x39fcc
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rdi
callq 0x38fa9
movq %rax, (%r14,%r13,8)
incq %r13
jmp 0x39fae
movq 0x10(%rsp), %rax
movq %r14, (%rax)
movq 0x18(%rsp), %rax
movl %r15d, (%rax)
jmp 0x39f1d
movq 0x9ffd8(%rip), %rax     # 0xd9fc0
movq (%rax), %rdi
leaq 0x5fb1c(%rip), %rsi     # 0x99b0e
xorl %ebx, %ebx
movq %r13, %rdx
xorl %eax, %eax
callq 0xe6a0
jmp 0x39f1d