// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/ply.cpp
// Function: ascii_get_element(PlyFile*, char*)
void ascii_get_element(PlyFile *plyfile, char *elem_ptr)
{
  int j,k;
  PlyElement *elem;
  PlyProperty *prop;
  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;
  int which_word;
  char *elem_data,*item=NULL;
  char *item_ptr;
  int item_size;
  int int_val;
  unsigned int uint_val;
  double double_val;
  int list_count;
  int store_it;
  char **store_array;
  char *other_data=NULL;
  int other_flag;

  /* the kind of element we're reading currently */
  elem = plyfile->which_elem;

  /* do we need to setup for other_props? */

  if (elem->other_offset != NO_OTHER_PROPS) {
    char **ptr;
    other_flag = 1;
    /* make room for other_props */
    other_data = (char *) myalloc (elem->other_size);
    /* store pointer in user's structure to the other_props */
    ptr = (char **) (elem_ptr + elem->other_offset);
    *ptr = other_data;
  }
  else {
    other_flag = 0;
  }

  /* read in the element */

  /* read in the elemnt */
  if (!fgets (orig_line, max_line_size, plyfile->fp)) {
    fprintf(stderr, "ply_get_element: unexpected end of file\n");
    exit (-1);
  }

  /* break line into words */
  if (!get_words(orig_line, max_line_size, max_words, word_buffer, words, &nwords)) {
    fprintf(stderr, "ply_get_element: unexpected format\n");
    exit (-1);
  }

  which_word = 0;

  for (j = 0; j < elem->nprops; j++) {

    prop = elem->props[j];
    store_it = (elem->store_prop[j] | other_flag);

    /* store either in the user's structure or in other_props */
    if (elem->store_prop[j])
      elem_data = elem_ptr;
    else
      elem_data = other_data;

    if (prop->is_list) {       /* a list */

      /* get and store the number of items in the list */
      get_ascii_item (words[which_word++], prop->count_external,
                      &int_val, &uint_val, &double_val);
      if (store_it) {
        item = elem_data + prop->count_offset;
        store_item(item, prop->count_internal, int_val, uint_val, double_val);
      }

      /* allocate space for an array of items and store a ptr to the array */
      list_count = int_val;
      item_size = ply_type_size[prop->internal_type];
      store_array = (char **) (elem_data + prop->offset);

      if (list_count == 0) {
        if (store_it)
          *store_array = NULL;
      }
      else {
        if (store_it) {
          item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count);
          item = item_ptr;
          *store_array = item_ptr;
        }

        /* read items and store them into the array */
        for (k = 0; k < list_count; k++) {
          get_ascii_item (words[which_word++], prop->external_type,
                          &int_val, &uint_val, &double_val);
          if (store_it) {
            store_item (item, prop->internal_type,
                        int_val, uint_val, double_val);
            item += item_size;
          }
        }
      }

    }
    else {                     /* not a list */
      get_ascii_item (words[which_word++], prop->external_type,
                      &int_val, &uint_val, &double_val);
      if (store_it) {
        item = elem_data + prop->offset;
        store_item (item, prop->internal_type, int_val, uint_val, double_val);
      }
    }

  }
}

// Assembly:
subq $0x40a8, %rsp           # imm = 0x40A8
movq %rdi, 0x40a0(%rsp)
movq %rsi, 0x4098(%rsp)
movl $0x1000, 0x407c(%rsp) # imm = 0x1000
movl $0x400, 0x306c(%rsp) # imm = 0x400
movl $0x0, 0x5c(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x40a0(%rsp), %rax
movq 0x40(%rax), %rax
movq %rax, 0x4088(%rsp)
movq 0x4088(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
je 0x73ebb
movl $0x1, 0xc(%rsp)
movq 0x4088(%rsp), %rax
movl 0x2c(%rax), %edi
movl $0x61e, %esi            # imm = 0x61E
leaq 0xc1467(%rip), %rdx     # 0x1352ef
callq 0x71630
movq %rax, 0x10(%rsp)
movq 0x4098(%rsp), %rax
movq 0x4088(%rsp), %rcx
movslq 0x28(%rcx), %rcx
addq %rcx, %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
jmp 0x73ec3
movl $0x0, 0xc(%rsp)
leaq 0x3070(%rsp), %rdi
movq 0x40a0(%rsp), %rax
movq (%rax), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0xf7e0
cmpq $0x0, %rax
jne 0x73f08
movq 0xfb0db(%rip), %rax     # 0x16efc8
movq (%rax), %rdi
leaq 0xc182a(%rip), %rsi     # 0x135721
movb $0x0, %al
callq 0xf730
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
leaq 0x3070(%rsp), %rdi
leaq 0x60(%rsp), %rcx
leaq 0x1060(%rsp), %r8
movl $0x1000, %esi           # imm = 0x1000
movl $0x400, %edx            # imm = 0x400
leaq 0x5c(%rsp), %r9
callq 0x732f0
cmpl $0x0, %eax
jne 0x73f58
movq 0xfb08b(%rip), %rax     # 0x16efc8
movq (%rax), %rdi
leaq 0xc1803(%rip), %rsi     # 0x13574a
movb $0x0, %al
callq 0xf730
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
movl $0x0, 0x58(%rsp)
movl $0x0, 0x4094(%rsp)
movl 0x4094(%rsp), %eax
movq 0x4088(%rsp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x74262
movq 0x4088(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x4094(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x4080(%rsp)
movq 0x4088(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x4094(%rsp), %rcx
movsbl (%rax,%rcx), %eax
orl 0xc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x4088(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x4094(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x73fec
movq 0x4098(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x73ff6
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x4080(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0x741cf
movl 0x58(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rsp)
cltq
movq 0x1060(%rsp,%rax,8), %rdi
movq 0x4080(%rsp), %rax
movl 0x18(%rax), %esi
leaq 0x38(%rsp), %rdx
leaq 0x34(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x74f00
cmpl $0x0, 0x20(%rsp)
je 0x74081
movq 0x50(%rsp), %rax
movq 0x4080(%rsp), %rcx
movslq 0x20(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq 0x4080(%rsp), %rax
movl 0x1c(%rax), %esi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
movsd 0x28(%rsp), %xmm0
callq 0x75070
movl 0x38(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x4080(%rsp), %rax
movslq 0xc(%rax), %rcx
leaq 0x104244(%rip), %rax    # 0x1782e0
movl (%rax,%rcx,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x50(%rsp), %rax
movq 0x4080(%rsp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0x740db
cmpl $0x0, 0x20(%rsp)
je 0x740d6
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
jmp 0x741cd
cmpl $0x0, 0x20(%rsp)
je 0x74123
movslq 0x3c(%rsp), %rax
shlq $0x0, %rax
movslq 0x24(%rsp), %rcx
imulq %rcx, %rax
movl %eax, %edi
movl $0x657, %esi            # imm = 0x657
leaq 0xc11ed(%rip), %rdx     # 0x1352ef
callq 0x71630
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x4090(%rsp)
movl 0x4090(%rsp), %eax
cmpl 0x24(%rsp), %eax
jge 0x741cb
movl 0x58(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rsp)
cltq
movq 0x1060(%rsp,%rax,8), %rdi
movq 0x4080(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x38(%rsp), %rdx
leaq 0x34(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x74f00
cmpl $0x0, 0x20(%rsp)
je 0x741b3
movq 0x48(%rsp), %rdi
movq 0x4080(%rsp), %rax
movl 0xc(%rax), %esi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
movsd 0x28(%rsp), %xmm0
callq 0x75070
movl 0x3c(%rsp), %ecx
movq 0x48(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
jmp 0x741b5
movl 0x4090(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4090(%rsp)
jmp 0x7412e
jmp 0x741cd
jmp 0x7424a
movl 0x58(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x58(%rsp)
cltq
movq 0x1060(%rsp,%rax,8), %rdi
movq 0x4080(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x38(%rsp), %rdx
leaq 0x34(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x74f00
cmpl $0x0, 0x20(%rsp)
je 0x74248
movq 0x50(%rsp), %rax
movq 0x4080(%rsp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq 0x4080(%rsp), %rax
movl 0xc(%rax), %esi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
movsd 0x28(%rsp), %xmm0
callq 0x75070
jmp 0x7424a
jmp 0x7424c
movl 0x4094(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4094(%rsp)
jmp 0x73f6b
addq $0x40a8, %rsp           # imm = 0x40A8
retq
nopw (%rax,%rax)