// File: /workspace/github_cpp_makefile_star2_O1/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 $0x848, %rsp            # imm = 0x848
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xa142b(%rip), %rsi     # 0xe85a3
movq %r14, %rdi
callq 0x21aa0
testq %rax, %rax
je 0x472be
movq %rax, %r15
movq %r14, 0x38(%rsp)
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
movq %rbx, 0x20(%rsp)
je 0x472d6
leaq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x9bd7a(%rip), %r14     # 0xe2f46
leaq 0x9bd79(%rip), %rbx     # 0xe2f4c
leaq 0x9c70c(%rip), %rbp     # 0xe38e6
xorl %r13d, %r13d
movq %r12, %rdi
movq %r14, %rsi
callq 0x21170
testq %rax, %rax
jne 0x472d9
movq %r12, %rdi
movq %rbx, %rsi
callq 0x21170
testq %rax, %rax
je 0x472a3
movq %r12, %rdi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
leaq 0x440(%rsp), %rcx
xorl %eax, %eax
callq 0x21880
cmpl $0x2, %eax
jne 0x472a3
movq 0x40(%rsp), %rax
movabsq $0x7562697274746140, %rcx # imm = 0x7562697274746140
xorq %rcx, %rax
movq 0x43(%rsp), %rcx
movabsq $0x65747562697274, %rdx # imm = 0x65747562697274
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x472a3
movq %rbx, %rbp
movl %r13d, %ebx
movl $0x448, %edi            # imm = 0x448
callq 0x218b0
movq %rax, %r14
movq 0x20(%rsp), %r13
movq (%r13), %rsi
movq %rax, %rdi
leaq 0x440(%rsp), %rdx
xorl %ecx, %ecx
callq 0x487c8
movl 0x14(%r13), %edx
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x8cf92
movl %ebx, %r13d
incl %r13d
movq %rbp, %rbx
leaq 0x9bcaa(%rip), %r14     # 0xe2f46
leaq 0x9c643(%rip), %rbp     # 0xe38e6
movq %r12, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdx
callq 0x21f20
testq %rax, %rax
jne 0x471dd
jmp 0x472d9
leaq 0x9bc63(%rip), %rdi     # 0xe2f28
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8fd7a
jmp 0x4749e
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x47491
leaq 0x28(%rsp), %rax
movq $0x0, (%rax)
setle %r14b
movl %r13d, %eax
negl %eax
movq %rax, 0x10(%rsp)
leaq 0x9bc54(%rip), %rbx     # 0xe2f57
movabsq $0x40000002000001, %rbp # imm = 0x40000002000001
xorl %edx, %edx
movl %edx, 0x8(%rsp)
movq %r12, %rdi
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdx
callq 0x21f20
testq %rax, %rax
je 0x47489
movzbl (%r12), %eax
leal -0xa(%rax), %ecx
cmpl $0x36, %ecx
ja 0x4733f
btq %rcx, %rbp
jb 0x47313
testl %eax, %eax
je 0x47313
movq %r12, %rdi
movq %rbx, %rsi
callq 0x21ee0
movb %r14b, 0x7(%rsp)
movl %r13d, 0xc(%rsp)
testl %r13d, %r13d
jle 0x4745d
movq %rax, %rdx
xorl %r13d, %r13d
movb 0x7(%rsp), %r14b
testq %rdx, %rdx
je 0x473e1
movq 0x10(%rsp), %rax
addl %r13d, %eax
movq 0x20(%rsp), %rbx
addl 0x14(%rbx), %eax
movq 0x8(%rbx), %rcx
cltq
movq (%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdx, %rdi
leaq 0x9c5ae(%rip), %rsi     # 0xe394d
leaq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x21880
cmpl $0x1, %eax
jne 0x47404
movq (%rbx), %rax
movl 0x8(%rsp), %esi
cmpl 0x14(%rax), %esi
jge 0x4741d
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %rdi
callq 0x4a20c
xorl %edi, %edi
leaq 0x9bb82(%rip), %rsi     # 0xe2f57
callq 0x21ee0
movq %rax, %rdx
movb $0x1, %bl
jmp 0x4743f
xorl %ebx, %ebx
leaq 0x9bb71(%rip), %rdi     # 0xe2f5b
movl %r13d, %esi
movl 0x8(%rsp), %edx
xorl %eax, %eax
callq 0x8fd7a
movq %r12, %rdi
callq 0x21c10
xorl %edx, %edx
jmp 0x4743f
xorl %ebx, %ebx
leaq 0x9bb4e(%rip), %rdi     # 0xe2f5b
movl %r13d, %esi
movl 0x8(%rsp), %edx
xorl %eax, %eax
callq 0x8fd7a
jmp 0x47432
xorl %ebx, %ebx
leaq 0x9bb65(%rip), %rdi     # 0xe2f8b
movq 0x38(%rsp), %rsi
xorl %eax, %eax
callq 0x8fd7a
movq %r12, %rdi
callq 0x21c10
movq 0x18(%rsp), %rdx
testb %bl, %bl
je 0x4745d
incl %r13d
cmpl 0xc(%rsp), %r13d
setge %r14b
movq 0x10(%rsp), %rax
addl %r13d, %eax
jne 0x4736c
movzbl %r14b, %eax
movl %eax, %ecx
andl $0x1, %ecx
movl 0x8(%rsp), %edx
addl %ecx, %edx
testb $0x1, %al
movl 0xc(%rsp), %r13d
movb 0x7(%rsp), %r14b
leaq 0x9bad8(%rip), %rbx     # 0xe2f57
jne 0x4730f
xorl %ebx, %ebx
jmp 0x4749e
movq %r15, %rdi
callq 0x218a0
movq %r12, %rdi
callq 0x21c10
movl $0x1, %ebx
movl %ebx, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x448, %esi            # imm = 0x448
movq %r14, %rdi
callq 0x218d0
movq %rbx, %rdi
callq 0x220b0