// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/ply.cpp
// Function: binary_get_element(PlyFile*, char*)
void binary_get_element(PlyFile *plyfile, char *elem_ptr)
{
  int j,k;
  PlyElement *elem;
  PlyProperty *prop;
  FILE *fp = plyfile->fp;
  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 a number of elements */

  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_binary_item (fp, plyfile->file_type, 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_binary_item (fp, plyfile->file_type, 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_binary_item (fp, plyfile->file_type, 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 $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x80(%rsp), %rax
movq 0x40(%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
je 0x7430b
movl $0x1, 0xc(%rsp)
movq 0x68(%rsp), %rax
movl 0x2c(%rax), %edi
movl $0x699, %esi            # imm = 0x699
leaq 0xc1011(%rip), %rdx     # 0x1352ef
callq 0x71630
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rax
movq 0x68(%rsp), %rcx
movslq 0x28(%rcx), %rcx
addq %rcx, %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
jmp 0x74313
movl $0x0, 0xc(%rsp)
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movq 0x68(%rsp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x745a5
movq 0x68(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x74(%rsp), %rcx
movsbl (%rax,%rcx), %eax
orl 0xc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x68(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x74(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x7437e
movq 0x78(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x74388
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0x74528
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x60(%rsp), %rax
movl 0x18(%rax), %edx
leaq 0x38(%rsp), %rcx
leaq 0x34(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x751c0
cmpl $0x0, 0x20(%rsp)
je 0x74400
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq 0x20(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq 0x60(%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 0x60(%rsp), %rax
movslq 0xc(%rax), %rcx
leaq 0x103ec8(%rip), %rax    # 0x1782e0
movl (%rax,%rcx,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0x74454
cmpl $0x0, 0x20(%rsp)
je 0x7444f
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
jmp 0x74526
cmpl $0x0, 0x20(%rsp)
je 0x7449c
movslq 0x3c(%rsp), %rax
shlq $0x0, %rax
movslq 0x24(%rsp), %rcx
imulq %rcx, %rax
movl %eax, %edi
movl $0x6c2, %esi            # imm = 0x6C2
leaq 0xc0e74(%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, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x24(%rsp), %eax
jge 0x74524
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x60(%rsp), %rax
movl 0x8(%rax), %edx
leaq 0x38(%rsp), %rcx
leaq 0x34(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x751c0
cmpl $0x0, 0x20(%rsp)
je 0x74515
movq 0x48(%rsp), %rdi
movq 0x60(%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 0x74517
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x744a4
jmp 0x74526
jmp 0x74593
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x60(%rsp), %rax
movl 0x8(%rax), %edx
leaq 0x38(%rsp), %rcx
leaq 0x34(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x751c0
cmpl $0x0, 0x20(%rsp)
je 0x74591
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq 0x60(%rsp), %rax
movl 0xc(%rax), %esi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
movsd 0x28(%rsp), %xmm0
callq 0x75070
jmp 0x74593
jmp 0x74595
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x7431b
addq $0x88, %rsp
retq
nopl (%rax)