// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0x9d547(%rip), %rsi     # 0xe85a3
movq %r14, %rdi
callq 0x21aa0
testq %rax, %rax
je 0x4b290
movq %rax, %r15
movl $0x10000, %edi          # imm = 0x10000
callq 0x21040
movq %rax, %r12
movq %rax, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdx
callq 0x21f20
testq %rax, %rax
je 0x4b273
movq %r14, 0x8(%rsp)
movq %rbx, (%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x97e97(%rip), %rbp     # 0xe2f46
leaq 0x97e96(%rip), %r13     # 0xe2f4c
leaq 0x420(%rsp), %r14
xorl %ebx, %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x21170
testq %rax, %rax
jne 0x4b164
movq %r12, %rdi
movq %r13, %rsi
callq 0x21170
testq %rax, %rax
je 0x4b14b
movq %r12, %rdi
leaq 0x987f8(%rip), %rsi     # 0xe38e6
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x4b14b
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 0x4b14b
testl %ebx, %ebx
jne 0x4b149
movl $0x400, %edx            # imm = 0x400
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x21410
movq (%rsp), %rax
movb $0x0, 0x5b(%rax)
incl %ebx
movq %r12, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdx
callq 0x21f20
testq %rax, %rax
jne 0x4b0c0
testl %ebx, %ebx
movq 0x8(%rsp), %r14
je 0x4b273
leaq 0x18(%rsp), %r13
movq $0x0, (%r13)
leaq 0x97dd2(%rip), %r14     # 0xe2f57
movabsq $0x40000002000001, %rbx # imm = 0x40000002000001
xorl %ebp, %ebp
movq %r12, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdx
callq 0x21f20
testq %rax, %rax
je 0x4b2a5
movzbl (%r12), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x4b1bd
btq %rcx, %rbx
jb 0x4b191
testl %eax, %eax
je 0x4b191
movq %r12, %rdi
movq %r14, %rsi
callq 0x21ee0
testq %rax, %rax
je 0x4b22e
movq %rax, %rdi
leaq 0x98772(%rip), %rsi     # 0xe394d
movq %r13, %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x4b22e
movq (%rsp), %rdx
movq (%rdx), %rax
cmpl 0x14(%rax), %ebp
jge 0x4b25b
movsd 0x18(%rsp), %xmm0
movq 0x410(%rdx), %rax
movslq %ebp, %rcx
movsd %xmm0, (%rax,%rcx,8)
movaps 0x9821e(%rip), %xmm0    # 0xe3430
movups %xmm0, 0x418(%rdx)
movups %xmm0, 0x428(%rdx)
movups %xmm0, 0x438(%rdx)
incl %ebp
movb $0x1, %r14b
jmp 0x4b249
xorl %r14d, %r14d
leaq 0x98270(%rip), %rdi     # 0xe34a8
movl %ebp, %esi
xorl %eax, %eax
callq 0x8fd7a
movq %r12, %rdi
callq 0x21c10
testb %r14b, %r14b
leaq 0x97d04(%rip), %r14     # 0xe2f57
jne 0x4b191
jmp 0x4b2ce
xorl %r14d, %r14d
leaq 0x97d26(%rip), %rdi     # 0xe2f8b
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x8fd7a
jmp 0x4b241
leaq 0x98219(%rip), %rdi     # 0xe3493
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8fd7a
movq %r12, %rdi
callq 0x21c10
jmp 0x4b2ba
leaq 0x97c91(%rip), %rdi     # 0xe2f28
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8fd7a
jmp 0x4b2ba
movq %r12, %rdi
callq 0x21c10
movq %r15, %rdi
callq 0x218a0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x4b2ba