// File: /workspace/github_cpp_makefile_star2_O3/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 0x9bc25(%rip), %rsi     # 0xe65c3
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x4abb7
movq %rax, %r12
movq %rbx, 0x8(%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 0x4ab98
movq %r14, (%rsp)
leaq 0x8(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x96545(%rip), %rbp     # 0xe0f36
leaq 0x96544(%rip), %r13     # 0xe0f3c
leaq 0x420(%rsp), %r15
xorl %r14d, %r14d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21170
testq %rax, %rax
jne 0x4aaa9
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21170
testq %rax, %rax
je 0x4aa90
movq %rbx, %rdi
leaq 0x96ea2(%rip), %rsi     # 0xe18d3
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x4aa90
movq 0x20(%rsp), %rax
movabsq $0x7562697274746140, %rcx # imm = 0x7562697274746140
xorq %rcx, %rax
movq 0x23(%rsp), %rcx
movabsq $0x65747562697274, %rdx # imm = 0x65747562697274
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x4aa90
testl %r14d, %r14d
jne 0x4aa8d
movl $0x400, %edx            # imm = 0x400
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x21410
movq (%rsp), %rax
movb $0x0, 0x5b(%rax)
incl %r14d
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
jne 0x4aa03
testl %r14d, %r14d
je 0x4ab98
movq $0x0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
je 0x4abcc
leaq 0x96e5f(%rip), %r15     # 0xe193a
leaq 0x10(%rsp), %rbp
movabsq $0x40000002000001, %r14 # imm = 0x40000002000001
xorl %r13d, %r13d
movzbl (%rbx), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x4aafe
btq %rcx, %r14
jb 0x4ab02
testl %eax, %eax
jne 0x4ab1c
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdx
callq 0x21f10
testq %rax, %rax
jne 0x4aaed
jmp 0x4abcc
movq %rbx, %rdi
leaq 0x96421(%rip), %rsi     # 0xe0f47
callq 0x21ed0
testq %rax, %rax
je 0x4abf5
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x4abf5
movq (%rsp), %rcx
movq (%rcx), %rax
movslq 0x14(%rax), %rax
cmpq %rax, %r13
jge 0x4ac0a
movsd 0x10(%rsp), %xmm0
movq 0x410(%rcx), %rax
movsd %xmm0, (%rax,%r13,8)
movaps 0x968a5(%rip), %xmm0    # 0xe1420
movups %xmm0, 0x418(%rcx)
movups %xmm0, 0x428(%rcx)
movups %xmm0, 0x438(%rcx)
incq %r13
jmp 0x4ab02
leaq 0x968e4(%rip), %rdi     # 0xe1483
xorl %ebp, %ebp
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x8e88e
movq %rbx, %rdi
callq 0x21c00
jmp 0x4abe1
leaq 0x9635a(%rip), %rdi     # 0xe0f18
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8e88e
jmp 0x4abe1
movq %rbx, %rdi
callq 0x21c00
movq %r12, %rdi
callq 0x218a0
movl $0x1, %ebp
movl %ebp, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9689c(%rip), %rdi     # 0xe1498
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x8e88e
jmp 0x4abad
leaq 0x9636a(%rip), %rdi     # 0xe0f7b
jmp 0x4ab9f
nop