// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::ReadFile(char const*)
int R3SurfelDatabase::
ReadFile(const char *filename)
{
  // Parse input filename extension
  const char *extension;
  if (!(extension = strrchr(filename, '.'))) {
    printf("Filename %s has no extension (e.g., .ply)\n", filename);
    return 0;
  }

  // Read file of appropriate type
  if (!strncmp(extension, ".ssb", 5)) {
    // Open file
    FILE *f = fopen(filename, "rb");
    if (!f) {
      RNFail("Unable to open file %s\n", filename);
      return 0;
    }

    // Read stream
    if (!ReadStream(f)) {
      fclose(f);
      return 0;
    }

    // Close file
    fclose(f);
  }
  else if (!strncmp(extension, ".list", 5)) {
    // Open file
    FILE *f = fopen(filename, "r");
    if (!f) {
      RNFail("Unable to open file %s\n", filename);
      return 0;
    }

    // Read file names
    char buffer[4096];
    while (fscanf(f, "%s", buffer) == (unsigned int) 1) {
      // Create block
      R3SurfelBlock *block = new R3SurfelBlock();
      if (!block) {
        RNFail("Unable to create block for %s\n", buffer);
        fclose(f);
        return 0;
      }
    
      // Read block
      if (!block->ReadFile(buffer)) { 
        delete block; 
        fclose(f);
        return 0; 
      }

      // Update properties
      block->UpdateProperties();

      // Insert block
      InsertBlock(block);

      // Release block
      ReleaseBlock(block);
    }

    // Close file
    fclose(f);
  }
  else { 
    // Create block
    R3SurfelBlock *block = new R3SurfelBlock();
    if (!block) {
      RNFail("Unable to create block\n");
      return 0;
    }
    
    // Read file
    if (!block->ReadFile(filename)) { 
      delete block; 
      return 0; 
    }
    
    // Update properties
    block->UpdateProperties();

    // Insert block
    InsertBlock(block);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rdi
movl $0x2e, %esi
callq 0x13560
testq %rax, %rax
je 0x41ac3
movq %rax, %r14
leaq 0x9de99(%rip), %rsi     # 0xdf8ec
movl $0x5, %edx
movq %rax, %rdi
callq 0x13240
testl %eax, %eax
je 0x41adb
leaq 0x9de86(%rip), %rsi     # 0xdf8f1
movl $0x5, %edx
movq %r14, %rdi
callq 0x13240
testl %eax, %eax
je 0x41b1b
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r14
movq %rax, %rdi
callq 0x2b96c
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e9ea
movl %eax, %ebp
movq %r14, %rdi
testl %eax, %eax
je 0x41c27
callq 0x2e364
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x10(%rax)
jmp 0x41c39
leaq 0x9d3d2(%rip), %rdi     # 0xdee9c
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x13070
jmp 0x41c46
leaq 0x9c6fb(%rip), %rsi     # 0xde1dd
movq %r15, %rdi
callq 0x135d0
testq %rax, %rax
je 0x41c12
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x60(%rax)
movl %eax, %ebx
movq %r14, %rdi
callq 0x134d0
testl %ebx, %ebx
jne 0x41c41
xorl %ebx, %ebx
jmp 0x41c46
leaq 0xa7d61(%rip), %rsi     # 0xe9883
movq %r15, %rdi
callq 0x135d0
testq %rax, %rax
je 0x41c12
movq %rax, %r14
movq %rsp, %r12
movq %r14, %rdi
leaq 0xa3e76(%rip), %rsi     # 0xe59b9
movq %r12, %rdx
xorl %eax, %eax
callq 0x13930
movl %eax, %ebp
cmpl $0x1, %eax
jne 0x41c5a
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
callq 0x2b96c
movq %r13, %rdi
movq %r12, %rsi
callq 0x2e9ea
movl %eax, %r15d
movq %r13, %rdi
testl %eax, %eax
je 0x41bed
callq 0x2e364
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x10(%rax)
cmpl $0x1, 0xb4(%r13)
jne 0x41bb7
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x41c07
decl 0xb4(%r13)
jne 0x41c07
testb $0x2, 0x89(%r13)
je 0x41c07
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x41c07
callq 0x2ce2e
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq %r14, %rdi
callq 0x134d0
testl %r15d, %r15d
jne 0x41b39
jmp 0x41c62
leaq 0x9c8b9(%rip), %rdi     # 0xde4d2
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xaddb2
jmp 0x41c46
callq 0x2ce2e
movl $0xc8, %esi
movq %r14, %rdi
callq 0x134f0
testl %ebp, %ebp
je 0x41b14
movl $0x1, %ebx
movl %ebx, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x134d0
cmpl $0x1, %ebp
je 0x41b14
jmp 0x41c41
movq %rax, %rbx
movl $0xc8, %esi
movq %r14, %rdi
jmp 0x41c85
movq %rax, %rbx
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq %rbx, %rdi
callq 0x138b0