// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x0, 0xc(%rsp)
testq %rdi, %rdi
je 0x3f5f3
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
cmpb $0x0, 0x4a7a51(%rip)    # 0x4e6b68
jne 0x3f120
movb $0x1, 0x4a7a48(%rip)    # 0x4e6b68
cmpb $0x0, 0x4a7a45(%rip)    # 0x4e6b6c
jne 0x3f12e
callq 0x3e406
leaq 0x697b6(%rip), %rdx     # 0xa88eb
movl $0x50, %edi
movl $0x2e6, %esi            # imm = 0x2E6
callq 0x3e440
movq %rax, %rbx
xorl %eax, %eax
movl %eax, 0x10(%rbx)
movq %rax, 0x28(%rbx)
movl %eax, 0x20(%rbx)
movq %rax, 0x38(%rbx)
movl %eax, 0x30(%rbx)
movq %r14, (%rbx)
movq %rax, 0x48(%rbx)
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0xe730
testq %rax, %rax
je 0x3f5ee
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 0x3f60a
xorl %ecx, %ecx
movq (%r14), %rsi
leaq 0x631bc(%rip), %rax     # 0xa2370
leaq 0x631b2(%rip), %rdx     # 0xa236d
movb (%rsi,%rcx), %dil
testb %dil, %dil
je 0x3f1dc
cmpq $0x3, %rcx
je 0x3f1e2
leaq 0x1(%rcx), %r8
cmpb (%rcx,%rdx), %dil
movq %r8, %rcx
je 0x3f1bb
jmp 0x3f5e7
addq %rcx, %rdx
movq %rdx, %rax
cmpb (%rax), %dil
jne 0x3f5e7
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq (%rbx), %rdx
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xe730
testq %rax, %rax
je 0x3f512
leaq 0x20(%rsp), %rdi
leaq 0x6990e(%rip), %r15     # 0xa8b2a
leaq 0x6990e(%rip), %r13     # 0xa8b31
leaq 0x6995a(%rip), %r12     # 0xa8b84
leaq 0x6995b(%rip), %rbp     # 0xa8b8c
movq %rdi, %r14
movl $0x1000, %esi           # imm = 0x1000
movl $0x400, %edx            # imm = 0x400
leaq 0x3020(%rsp), %rcx
leaq 0x1020(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x3f60a
movq 0x1020(%rsp), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f27e
cmpq $0x6, %rcx
je 0x3f283
leaq 0x1(%rcx), %rsi
cmpb (%rcx,%r15), %dl
movq %rsi, %rcx
je 0x3f262
jmp 0x3f2d1
addq %r15, %rcx
jmp 0x3f28a
leaq 0x698a6(%rip), %rcx     # 0xa8b30
cmpb (%rcx), %dl
jne 0x3f2d1
cmpl $0x3, 0xc(%rsp)
jne 0x3f5f3
movq 0x1028(%rsp), %rdx
xorl %eax, %eax
leaq 0x6988d(%rip), %rdi     # 0xa8b37
movb (%rdx,%rax), %cl
testb %cl, %cl
je 0x3f381
cmpq $0x5, %rax
je 0x3f386
leaq 0x1(%rax), %rsi
cmpb (%rax,%r13), %cl
movq %rsi, %rax
je 0x3f2aa
jmp 0x3f39d
xorl %ecx, %ecx
leaq 0x698bb(%rip), %rdi     # 0xa8b95
leaq 0x698bc(%rip), %r8      # 0xa8b9d
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f2fd
cmpq $0x7, %rcx
je 0x3f302
leaq 0x1(%rcx), %rsi
cmpb (%rcx,%r12), %dl
movq %rsi, %rcx
je 0x3f2e1
jmp 0x3f322
addq %r12, %rcx
jmp 0x3f309
leaq 0x69882(%rip), %rcx     # 0xa8b8b
cmpb (%rcx), %dl
jne 0x3f322
movq %rbx, %rdi
leaq 0x1020(%rsp), %rsi
callq 0x3f6ce
jmp 0x3f4f6
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f33f
cmpq $0x8, %rcx
je 0x3f344
leaq 0x1(%rcx), %rsi
cmpb (%rcx,%rbp), %dl
movq %rsi, %rcx
je 0x3f324
jmp 0x3f364
addq %rbp, %rcx
jmp 0x3f34b
leaq 0x69849(%rip), %rcx     # 0xa8b94
cmpb (%rcx), %dl
jne 0x3f364
movq %rbx, %rdi
leaq 0x1020(%rsp), %rsi
callq 0x3f761
jmp 0x3f4f6
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f3ba
cmpq $0x7, %rcx
je 0x3f3bf
leaq 0x1(%rcx), %rsi
cmpb (%rcx,%rdi), %dl
movq %rsi, %rcx
je 0x3f366
jmp 0x3f3e1
addq %r13, %rax
jmp 0x3f38d
leaq 0x697a9(%rip), %rax     # 0xa8b36
cmpb (%rax), %cl
jne 0x3f39d
movl $0x1, 0x8(%rbx)
jmp 0x3f472
xorl %eax, %eax
movb (%rdx,%rax), %cl
testb %cl, %cl
je 0x3f417
cmpq $0x11, %rax
je 0x3f41c
leaq 0x1(%rax), %rsi
cmpb (%rax,%rdi), %cl
movq %rsi, %rax
je 0x3f39f
jmp 0x3f430
addq %rdi, %rcx
jmp 0x3f3c6
leaq 0x697d6(%rip), %rcx     # 0xa8b9c
leaq 0x27(%rsp), %rsi
cmpb (%rcx), %dl
jne 0x3f3e1
movzbl (%rsi), %eax
cmpl $0x20, %eax
je 0x3f3dc
cmpl $0x9, %eax
jne 0x3f40a
incq %rsi
jmp 0x3f3cf
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f48a
cmpq $0x8, %rcx
je 0x3f48f
leaq 0x1(%rcx), %rsi
cmpb (%rcx,%r8), %dl
movq %rsi, %rcx
je 0x3f3e3
jmp 0x3f4b1
movq %rbx, %rdi
callq 0x3f021
jmp 0x3f4f6
addq %rdi, %rax
jmp 0x3f423
leaq 0x69725(%rip), %rax     # 0xa8b48
cmpb (%rax), %cl
jne 0x3f430
movl $0x2, 0x8(%rbx)
jmp 0x3f472
xorl %eax, %eax
leaq 0x69710(%rip), %rdi     # 0xa8b49
movb (%rdx,%rax), %cl
testb %cl, %cl
je 0x3f457
cmpq $0x14, %rax
je 0x3f45c
leaq 0x1(%rax), %rsi
cmpb (%rax,%rdi), %cl
movq %rsi, %rax
je 0x3f439
jmp 0x3f51c
addq %rdi, %rax
jmp 0x3f463
leaq 0x696fa(%rip), %rax     # 0xa8b5d
cmpb (%rax), %cl
jne 0x3f51c
movl $0x3, 0x8(%rbx)
movq 0x1030(%rsp), %rdi
callq 0xe580
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rbx)
jmp 0x3f4f6
addq %r8, %rcx
jmp 0x3f496
leaq 0x6970f(%rip), %rcx     # 0xa8ba5
leaq 0x28(%rsp), %rsi
cmpb (%rcx), %dl
jne 0x3f4b1
movzbl (%rsi), %eax
cmpl $0x20, %eax
je 0x3f4ac
cmpl $0x9, %eax
jne 0x3f4d5
incq %rsi
jmp 0x3f49f
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
testb %dl, %dl
je 0x3f4df
cmpq $0xa, %rcx
je 0x3f4eb
leaq 0x1(%rcx), %rsi
leaq 0x696db(%rip), %rdi     # 0xa8ba6
cmpb (%rcx,%rdi), %dl
movq %rsi, %rcx
je 0x3f4b3
jmp 0x3f4f6
movq %rbx, %rdi
callq 0x3f083
jmp 0x3f4f6
leaq 0x696c0(%rip), %rax     # 0xa8ba6
addq %rax, %rcx
jmp 0x3f4f2
leaq 0x696be(%rip), %rcx     # 0xa8bb0
cmpb (%rcx), %dl
je 0x3f53b
movq (%rbx), %rdx
movq %r14, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xe730
testq %rax, %rax
movq %r14, %rdi
jne 0x3f231
callq 0xec20
jmp 0x3f5f3
movq 0xa5a95(%rip), %rax     # 0xe4fb8
movq (%rax), %rdi
leaq 0x69631(%rip), %rsi     # 0xa8b5e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xe670
jmp 0x3f5f5
movslq 0x10(%rbx), %r15
testq %r15, %r15
jle 0x3f595
movq 0x18(%rbx), %r12
xorl %r13d, %r13d
leaq 0x69399(%rip), %r14     # 0xa88eb
movq (%r12,%r13,8), %rbp
movl 0x10(%rbp), %edi
movl $0x334, %esi            # imm = 0x334
movq %r14, %rdx
callq 0x3e440
movq %rax, 0x20(%rbp)
cmpl $0x0, 0x10(%rbp)
jle 0x3f586
xorl %eax, %eax
movq 0x20(%rbp), %rcx
movb $0x0, (%rcx,%rax)
incq %rax
movslq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jl 0x3f572
movl $0xffffffff, 0x28(%rbp) # imm = 0xFFFFFFFF
incq %r13
cmpq %r15, %r13
jne 0x3f552
leal (,%r15,8), %edi
leaq 0x69347(%rip), %rdx     # 0xa88eb
movl $0x33c, %esi            # imm = 0x33C
callq 0x3e440
movq %rax, %r14
testl %r15d, %r15d
jle 0x3f5d5
movq 0x18(%rbx), %r12
xorl %r13d, %r13d
movq (%r12,%r13,8), %rax
movq (%rax), %rdi
callq 0x3e480
movq %rax, (%r14,%r13,8)
incq %r13
cmpq %r13, %r15
jne 0x3f5bd
movq 0x10(%rsp), %rax
movq %r14, (%rax)
movq 0x18(%rsp), %rax
movl %r15d, (%rax)
jmp 0x3f5f5
callq 0xebfe
jmp 0x3f5f3
callq 0xec42
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x4028, %rsp           # imm = 0x4028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq