// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::ReadARFF(char const*)
int R3MeshPropertySet::
ReadARFF(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open ARFF file: %s\n", filename);
    return 0;
  }

  // Allocate large buffer for reading ARFF lines
  const int max_line_size = 64 * 1024;
  char *buffer = new char [ max_line_size ];
  if (!buffer) {
    RNFail("Unable to allocate buffer for reading ARFF file\n");
    return 0;
  }
  
  // Read attribute names and allocate property for each one
  int property_count = 0;
  char token[1024], name[1024];
  while (fgets(buffer, max_line_size, fp)) {
    if (strstr(buffer, "@data")) break;
    else if (strstr(buffer, "@attribute")) {
      if (sscanf(buffer, "%s%s", token, name) == (unsigned int) 2) {
        if (!strcmp(token, "@attribute")) {
          R3MeshProperty *property = new R3MeshProperty(mesh, name);
          Insert(property);
          property_count++;
        }
      }
    }
  }

  // Check number of properties
  if (property_count == 0) { delete [] buffer; return 1; }

  // Read data and assign property values
  int vertex_index = 0;
  RNScalar property_value = 0;
  while (fgets(buffer, max_line_size, fp)) {
    // Check for header line
    if (buffer[0] == '#') continue;
    if (buffer[0] == '@') continue;
    if (buffer[0] == '\0') continue;
    if (buffer[0] == '\n') continue;
    char *bufferp = strtok(buffer, "\n\t ");
    for (int i = 0; i < property_count; i++) {
      R3MeshProperty *property = Property(NProperties()-property_count+i);
      if (!bufferp) { 
        RNFail("Unable to read property value %d for vertex %d\n", i, vertex_index);
        delete [] buffer;
        return 0;
      }
      if (sscanf(bufferp, "%lf", &property_value) != (unsigned int) 1) {
        RNFail("Unable to read property value %d for vertex %d\n", i, vertex_index);
        delete [] buffer;
        return 0;
      }
      if (vertex_index >= mesh->NVertices()) {
        RNFail("Too many data lines in %s\n", filename);
        delete [] buffer;
        return 0;
      }
      property->SetVertexValue(vertex_index, property_value);
      bufferp = strtok(NULL, "\n\t ");
    }
    vertex_index++;
  }

  // Check if read value for every vertex
  // if (vertex_index != mesh->NVertices()) {
  //   RNFail("Mismatching number of data lines (%d) and vertices (%d) in %s\n", vertex_index, mesh->NVertices(), filename);
  //   delete [] buffer;
  //   return 0;
  // }

  // Close file
  fclose(fp);

  // Delete buffer
  delete [] buffer;
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x838, %rsp            # imm = 0x838
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8ecd5(%rip), %rsi     # 0xd02f3
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x4181d
movq %rax, %r12
movq %rbx, 0x28(%rsp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x21040
movq %rax, %rbx
movq %r14, 0x18(%rsp)
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
leaq 0x898b5(%rip), %rbp     # 0xcaf0e
leaq 0x898b4(%rip), %r13     # 0xcaf14
leaq 0x430(%rsp), %r15
leaq 0x30(%rsp), %r14
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
je 0x41722
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21170
testq %rax, %rax
jne 0x41722
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21170
testq %rax, %rax
je 0x4167c
movq %rbx, %rdi
leaq 0x8a1b4(%rip), %rsi     # 0xcb873
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x4167c
movq %r15, %rdi
movq %r13, %rsi
pushq $0xb
popq %rdx
callq 0x21910
testl %eax, %eax
jne 0x4167c
movl $0x448, %edi            # imm = 0x448
callq 0x218c0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq %rdi, 0x10(%rsp)
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x42ae4
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x26aa0
incl (%rsp)
decl 0x4(%rsp)
jmp 0x4167c
cmpl $0x0, (%rsp)
je 0x41849
leaq 0x20(%rsp), %rax
andq $0x0, (%rax)
movabsq $0x40000002000001, %r15 # imm = 0x40000002000001
xorl %ebp, %ebp
movq 0x18(%rsp), %r14
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
je 0x41833
movzbl (%rbx), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x41770
btq %rcx, %r15
jb 0x41746
testl %eax, %eax
je 0x41746
movq %rbx, %rdi
leaq 0x897a1(%rip), %rsi     # 0xcaf1f
callq 0x21ee0
xorl %esi, %esi
movl 0x4(%rsp), %edx
cmpl %esi, (%rsp)
je 0x41816
testq %rax, %rax
je 0x4186a
movl %esi, 0x8(%rsp)
movl 0x14(%r14), %ecx
movl %edx, %r13d
addl %edx, %ecx
movq 0x8(%r14), %rdx
movslq %ecx, %rcx
movq (%rdx,%rcx,8), %r15
movq %rax, %rdi
leaq 0x8a11d(%rip), %rsi     # 0xcb8da
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x41876
movq (%r14), %rax
cmpl 0x14(%rax), %ebp
jge 0x4188f
movsd 0x20(%rsp), %xmm0
movq %r15, %rdi
movl %ebp, %esi
callq 0x44320
xorl %edi, %edi
leaq 0x89728(%rip), %rsi     # 0xcaf1f
callq 0x21ee0
movl 0x8(%rsp), %esi
incl %esi
movl %r13d, %edx
incl %edx
movabsq $0x40000002000001, %r15 # imm = 0x40000002000001
jmp 0x41789
incl %ebp
jmp 0x41746
leaq 0x896cc(%rip), %rdi     # 0xcaef0
xorl %r15d, %r15d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x82696
jmp 0x41855
movq %r12, %rdi
callq 0x218b0
pushq $0x1
popq %r15
movq %rbx, %rdi
callq 0x21c00
jmp 0x41855
movq %rbx, %rdi
callq 0x21c00
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x896b2(%rip), %rdi     # 0xcaf23
xorl %r15d, %r15d
jmp 0x41884
leaq 0x896a6(%rip), %rdi     # 0xcaf23
xorl %r15d, %r15d
movl 0x8(%rsp), %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x82696
jmp 0x4183f
leaq 0x896bd(%rip), %rdi     # 0xcaf53
xorl %r15d, %r15d
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x82696
jmp 0x4183f
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq 0x10(%rsp), %rdi
callq 0x218e0
movq %rbx, %rdi
callq 0x220c0
nop