// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1000, %rsp           # imm = 0x1000
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x2e
popq %rsi
movq %r14, %rdi
callq 0x13580
testq %rax, %rax
je 0x3c626
movq %rax, %r15
leaq 0x8d2f0(%rip), %rsi     # 0xc98ac
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x13250
testl %eax, %eax
je 0x3c63e
leaq 0x8d2df(%rip), %rsi     # 0xc98b1
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0x13250
testl %eax, %eax
je 0x3c679
movl $0xc8, %edi
callq 0x134f0
movq %rax, %r15
movq %rax, %rdi
callq 0x294bc
movq %r15, %rdi
movq %r14, %rsi
callq 0x2bab8
movq %r15, %rdi
testl %eax, %eax
je 0x3c709
callq 0x2b540
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
callq *0x10(%rax)
jmp 0x3c725
leaq 0x8c85f(%rip), %rdi     # 0xc8e8c
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x13070
jmp 0x3c746
leaq 0x8bb90(%rip), %rsi     # 0xc81d5
movq %r14, %rdi
callq 0x135f0
testq %rax, %rax
je 0x3c6f4
movq %rax, %r15
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
callq *0x60(%rax)
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
movq %r15, %rdi
callq 0x134e0
jmp 0x3c746
leaq 0x96f33(%rip), %rsi     # 0xd35b3
movq %r14, %rdi
callq 0x135f0
testq %rax, %rax
je 0x3c6f4
movq %rax, %r15
leaq 0x932ae(%rip), %r14     # 0xcf945
movq %rsp, %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x13970
cmpl $0x1, %eax
jne 0x3c71d
movl $0xc8, %edi
callq 0x134f0
movq %rax, %r13
movq %rax, %rdi
callq 0x294bc
movq %r13, %rdi
movq %r12, %rsi
callq 0x2bab8
movq %r13, %rdi
testl %eax, %eax
je 0x3c72a
callq 0x2b540
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x10(%rax)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20aa0
jmp 0x3c69a
leaq 0x8bdd7(%rip), %rdi     # 0xc84d2
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x9ce26
jmp 0x3c746
callq 0x2a5a6
movl $0xc8, %esi
movq %r15, %rdi
callq 0x13500
jmp 0x3c744
movq %r15, %rdi
callq 0x134e0
pushq $0x1
popq %rbx
jmp 0x3c746
callq 0x2a5a6
movl $0xc8, %esi
movq %r13, %rdi
callq 0x13500
movq %r15, %rdi
callq 0x134e0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
movl $0xc8, %esi
movq %r15, %rdi
jmp 0x3c771
movq %rax, %rbx
movl $0xc8, %esi
movq %r13, %rdi
callq 0x13500
movq %rbx, %rdi
callq 0x138f0