// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadXYZAscii(_IO_FILE*)
int R3SurfelBlock::
ReadXYZAscii(FILE *fp)
{
  // Get original file offset
  long int file_offset = RNFileTell(fp);

  // Count the number of surfels
  int count = 0;
  char buffer[4096];
  while (fgets(buffer, 4096, fp)) {
    // Check if blank line
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!*bufferp) continue;
    count++;
  }

  // Rewind file to original file offset
  RNFileSeek(fp, file_offset, RN_FILE_SEEK_SET);

  // Allocate surfels
  surfels = new R3Surfel [ count ];
  if (!surfels) {
    RNFail("Unable to allocate surfel block\n");
    return 0;
  }

  // Read surfels
  nsurfels = 0;
  while (fgets(buffer, 4096, fp)) {
    // Check if blank line
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!*bufferp) continue;

    // Check number of surfels
    if (nsurfels >= count) break;

    // Parse surfel data
    float x, y, z;
    unsigned int r, g, b;
    if (sscanf(buffer, "%f%f%f%u%u%u", &x, &y, &z, &r, &g, &b) != (unsigned int) 6) {
      r = 255; g = 0; b = 0;
      if (sscanf(buffer, "%f%f%f", &x, &y, &z) != (unsigned int) 3) {
        RNFail("Unable to read point %d out of %d into surfel block\n", nsurfels, count);
        delete [] surfels;
        surfels = NULL;
        nsurfels = 0;
        return 0;
      }
    }

    // Assign surfel
    surfels[nsurfels].SetPosition(x, y, z);
    surfels[nsurfels].SetColor(r, g, b);
    nsurfels++;
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movq %rsi, %rdi
callq 0x9c473
movq %rax, %rbx
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13840
testq %rax, %rax
je 0x2c57d
movq %r15, %r12
movsbl (%r12), %r13d
testl %r13d, %r13d
je 0x2c574
movl %r13d, %edi
callq 0x13080
incq %r12
testl %eax, %eax
jne 0x2c55b
cmpb $0x1, %r13b
sbbl $-0x1, %ebp
jmp 0x2c543
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x9c462
movl %ebp, %eax
imulq $0x30, %rax, %r15
movq %r15, %rdi
callq 0x13030
movq %rax, %r12
testl %ebp, %ebp
je 0x2c5b4
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x29118
addq $0x30, %r13
cmpq %r13, %r15
jne 0x2c5a2
movq 0x18(%rsp), %r15
movq %r12, (%r15)
andl $0x0, 0x8(%r15)
leaq 0x30(%rsp), %r13
leaq 0x24(%rsp), %rbx
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13840
testq %rax, %rax
je 0x2c6cd
movq %r13, %r12
movsbl (%r12), %edi
testl %edi, %edi
je 0x2c5cb
callq 0x13080
incq %r12
testl %eax, %eax
jne 0x2c5e7
cmpl %ebp, 0x8(%r15)
jge 0x2c6cd
movq %r13, %rdi
leaq 0x9c957(%rip), %rsi     # 0xc8f67
leaq 0x2c(%rsp), %r12
movq %r12, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rcx
movq %rbx, %r8
leaq 0x14(%rsp), %r9
xorl %eax, %eax
leaq 0xc(%rsp), %r10
pushq %r10
leaq 0x18(%rsp), %r10
pushq %r10
callq 0x134d0
popq %rcx
popq %rdx
cmpl $0x6, %eax
je 0x2c675
andl $0x0, 0x10(%rsp)
movl $0xff, 0x14(%rsp)
andl $0x0, 0xc(%rsp)
movq %r13, %rdi
leaq 0x9c893(%rip), %rsi     # 0xc8ef3
movq %r12, %rdx
movq %r15, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x134d0
cmpl $0x3, %eax
jne 0x2c6e6
movq 0x18(%rsp), %r15
movq (%r15), %rax
movslq 0x8(%r15), %rcx
imulq $0x30, %rcx, %rcx
movss 0x2c(%rsp), %xmm0
movss 0x28(%rsp), %xmm1
movss 0x24(%rsp), %xmm2
movss %xmm0, (%rax,%rcx)
movss %xmm1, 0x4(%rax,%rcx)
movss %xmm2, 0x8(%rax,%rcx)
movb 0x14(%rsp), %dl
movb 0x10(%rsp), %sil
movb 0xc(%rsp), %dil
movb %dl, 0x2c(%rax,%rcx)
movb %sil, 0x2d(%rax,%rcx)
movb %dil, 0x2e(%rax,%rcx)
incl 0x8(%r15)
jmp 0x2c5cb
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rbx
movl 0x8(%rbx), %esi
leaq 0x9c805(%rip), %rdi     # 0xc8efa
xorl %r14d, %r14d
movl %ebp, %edx
xorl %eax, %eax
callq 0x9ce26
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2c70e
callq 0x136a0
andq $0x0, (%rbx)
andl $0x0, 0x8(%rbx)
jmp 0x2c6d1
movq %rax, %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0x139f0
movq %rbx, %rdi
callq 0x138f0