// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshProperty.cpp
// Function: gaps::R3MeshProperty::ReadARFF(char const*)
int R3MeshProperty::
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 to read lines
  const int max_line_size = 64 * 1024;
  char *buffer = new char [max_line_size];
  if (!buffer) {
    RNFail("Unable to allocate buffer to read %s\n", filename);
    return 0;
  }
  
  // Read attribute names and set property name for first 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")) {
          if (property_count == 0) SetName(name);
          property_count++;
        }
      }
    }
  }

  // Check number of properties
  if (property_count == 0) {
    RNFail("No properties in %s\n", filename);
    delete [] buffer;
    return 0;
  }

  // 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 ");
    if (!bufferp) { 
      RNFail("Unable to read property value for vertex %d\n", vertex_index);
      delete [] buffer;
      return 0;
    }
    if (sscanf(bufferp, "%lf", &property_value) != (unsigned int) 1) {
      RNFail("Unable to read property value for vertex %d\n", vertex_index);
      delete [] buffer;
      return 0;
    }
    if (vertex_index >= mesh->NVertices()) {
      RNFail("Too many data lines in %s\n", filename);
      delete [] buffer;
      return 0;
    }
    SetVertexValue(vertex_index, property_value);
    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;
  // }

  // Delete buffer
  delete [] buffer;
  
  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8b32f(%rip), %rsi     # 0xd02f3
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x45182
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x21040
movq %rax, %rbx
leaq 0x85f18(%rip), %r13     # 0xcaf0e
leaq 0x85f17(%rip), %rbp     # 0xcaf14
leaq 0x420(%rsp), %r14
leaq 0x20(%rsp), %r15
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
je 0x45099
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21170
testq %rax, %rax
jne 0x45099
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21170
testq %rax, %rax
je 0x45012
movq %rbx, %rdi
leaq 0x86822(%rip), %rsi     # 0xcb873
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x45012
movq %r14, %rdi
movq %rbp, %rsi
pushq $0xb
popq %rdx
callq 0x21910
testl %eax, %eax
jne 0x45012
movl 0x4(%rsp), %eax
testl %eax, %eax
jne 0x4508e
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x44480
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
jmp 0x45012
cmpl $0x0, 0x4(%rsp)
je 0x45197
leaq 0x18(%rsp), %rbp
andq $0x0, (%rbp)
leaq 0x86825(%rip), %r15     # 0xcb8da
movabsq $0x40000002000001, %r14 # imm = 0x40000002000001
xorl %r13d, %r13d
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
je 0x4516d
movzbl (%rbx), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x450ec
btq %rcx, %r14
jb 0x450c2
testl %eax, %eax
je 0x450c2
movq %rbx, %rdi
leaq 0x85e25(%rip), %rsi     # 0xcaf1f
callq 0x21ee0
testq %rax, %rax
je 0x451c8
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x451c8
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movslq 0x14(%rax), %rax
cmpq %rax, %r13
jge 0x451dd
movsd 0x18(%rsp), %xmm0
movq 0x410(%rcx), %rax
movsd %xmm0, (%rax,%r13,8)
movaps 0x862b0(%rip), %xmm0    # 0xcb400
movups %xmm0, 0x418(%rcx)
movups %xmm0, 0x428(%rcx)
movups %xmm0, 0x438(%rcx)
incq %r13
jmp 0x450c2
movq %rbx, %rdi
callq 0x21c00
movq %r12, %rdi
callq 0x218b0
pushq $0x1
popq %rbp
jmp 0x451b4
leaq 0x85d67(%rip), %rdi     # 0xcaef0
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x82696
jmp 0x451b4
leaq 0x862a5(%rip), %rdi     # 0xcb443
xorl %ebp, %ebp
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x82696
movq %rbx, %rdi
callq 0x21c00
movl %ebp, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x86289(%rip), %rdi     # 0xcb458
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x82696
jmp 0x451ac
leaq 0x85d6f(%rip), %rdi     # 0xcaf53
jmp 0x4519e