// File: /workspace/github_cpp_makefile_star2_O3/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 0x9fa8b(%rip), %rsi     # 0xe65c3
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x46df4
movq %rax, %r12
movq %rbx, 0x20(%rsp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x21040
movq %rax, %rbx
movq %rax, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
je 0x46de4
movq %r14, 0x10(%rsp)
leaq 0x8(%r14), %rax
movq %rax, (%rsp)
leaq 0x9a3ab(%rip), %r13     # 0xe0f36
leaq 0x9a3aa(%rip), %r14     # 0xe0f3c
leaq 0x9ad3a(%rip), %rbp     # 0xe18d3
xorl %r15d, %r15d
movq %r12, 0x28(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21170
testq %rax, %rax
jne 0x46c87
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21170
testq %rax, %rax
je 0x46c6e
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x430(%rsp), %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x46c6e
movq 0x30(%rsp), %rax
movabsq $0x7562697274746140, %rcx # imm = 0x7562697274746140
xorq %rcx, %rax
movq 0x33(%rsp), %rcx
movabsq $0x65747562697274, %rdx # imm = 0x65747562697274
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x46c6e
movq %r13, %r12
movq %r14, %rbp
movl %r15d, %r14d
movl $0x448, %edi            # imm = 0x448
callq 0x218b0
movq %rax, %r15
movq 0x10(%rsp), %r13
movq (%r13), %rsi
movq %rax, %rdi
leaq 0x430(%rsp), %rdx
xorl %ecx, %ecx
callq 0x48134
movl 0x14(%r13), %edx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x8bdb2
movl %r14d, %r15d
incl %r15d
movq %rbp, %r14
movq %r12, %r13
movq 0x28(%rsp), %r12
leaq 0x9ac65(%rip), %rbp     # 0xe18d3
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
jne 0x46ba1
testl %r15d, %r15d
je 0x46de4
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
je 0x46dcc
movl %r15d, %eax
negl %eax
movq %rax, (%rsp)
leaq 0x9a285(%rip), %rbp     # 0xe0f47
movabsq $0x40000002000001, %r14 # imm = 0x40000002000001
xorl %r13d, %r13d
movzbl (%rbx), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x46ce0
btq %rcx, %r14
jb 0x46ce4
testl %eax, %eax
jne 0x46cfe
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
jne 0x46ccf
jmp 0x46dcc
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21ed0
testl %r15d, %r15d
jle 0x46da6
xorl %r14d, %r14d
movl %r15d, 0xc(%rsp)
testq %rax, %rax
je 0x46e1d
movl %r13d, %ebp
movq (%rsp), %rcx
addl %r14d, %ecx
movq 0x10(%rsp), %r13
addl 0x14(%r13), %ecx
movq 0x8(%r13), %rdx
movslq %ecx, %rcx
movq (%rdx,%rcx,8), %r15
movq %rax, %rdi
leaq 0x9abef(%rip), %rsi     # 0xe193a
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x46e2f
movq (%r13), %rax
cmpl 0x14(%rax), %ebp
jge 0x46e47
movl %ebp, %r13d
movsd 0x18(%rsp), %xmm0
movq %r15, %rdi
movl %ebp, %esi
callq 0x49b8e
xorl %edi, %edi
leaq 0x9a1be(%rip), %rbp     # 0xe0f47
movq %rbp, %rsi
callq 0x21ed0
incl %r14d
movq (%rsp), %rcx
addl %r14d, %ecx
movl 0xc(%rsp), %r15d
jne 0x46d1a
incl %r13d
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
movabsq $0x40000002000001, %r14 # imm = 0x40000002000001
jne 0x46ccf
movq %r12, %rdi
callq 0x218a0
movl $0x1, %r15d
movq %rbx, %rdi
callq 0x21c00
jmp 0x46e08
movq %rbx, %rdi
callq 0x21c00
movl $0x1, %r15d
jmp 0x46e08
leaq 0x9a11d(%rip), %rdi     # 0xe0f18
xorl %r15d, %r15d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8e88e
movl %r15d, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9a127(%rip), %rdi     # 0xe0f4b
xorl %r15d, %r15d
movl %r14d, %esi
movl %r13d, %edx
jmp 0x46e3e
leaq 0x9a115(%rip), %rdi     # 0xe0f4b
xorl %r15d, %r15d
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x8e88e
jmp 0x46dda
leaq 0x9a12d(%rip), %rdi     # 0xe0f7b
xorl %r15d, %r15d
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x8e88e
jmp 0x46dda
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq %r15, %rdi
callq 0x218d0
movq %rbx, %rdi
callq 0x220a0