// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %rbx
movq %rsi, %rdi
movl $0x2e, %esi
callq 0x13560
testq %rax, %rax
je 0x41726
movq %rax, %r14
leaq 0x9d234(%rip), %rsi     # 0xde8ec
movl $0x5, %edx
movq %rax, %rdi
callq 0x13240
testl %eax, %eax
je 0x4173e
leaq 0x9d221(%rip), %rsi     # 0xde8f1
movl $0x5, %edx
movq %r14, %rdi
callq 0x13240
testl %eax, %eax
je 0x41779
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r14
movq %rax, %rdi
callq 0x2b544
movq %r14, %rdi
movq %r15, %rsi
callq 0x2e666
movq %r14, %rdi
testl %eax, %eax
je 0x41897
callq 0x2dfb8
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x10(%rax)
jmp 0x4187b
leaq 0x9c76f(%rip), %rdi     # 0xdde9c
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x13070
jmp 0x418c7
leaq 0x9ba90(%rip), %rsi     # 0xdd1d5
movq %r15, %rdi
callq 0x135d0
testq %rax, %rax
je 0x41882
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x60(%rax)
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
movq %r14, %rdi
callq 0x134d0
jmp 0x418c7
leaq 0xa7113(%rip), %rsi     # 0xe8893
movq %r15, %rdi
callq 0x135d0
testq %rax, %rax
je 0x41882
movq %rax, %r14
leaq 0xa320a(%rip), %rsi     # 0xe49a5
movq %rsp, %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x13930
cmpl $0x1, %eax
jne 0x41873
movq %rsp, %r15
leaq 0xa31ea(%rip), %r12     # 0xe49a5
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
callq 0x2b544
movq %r13, %rdi
movq %r15, %rsi
callq 0x2e666
movq %r13, %rdi
testl %eax, %eax
je 0x418ab
callq 0x2dfb8
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x10(%rax)
movl 0xb4(%r13), %eax
cmpl $0x1, %eax
jne 0x41824
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x4185a
movl 0xb4(%r13), %eax
decl %eax
movl %eax, 0xb4(%r13)
jne 0x4185a
testb $0x2, 0x89(%r13)
je 0x4185a
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x13930
cmpl $0x1, %eax
je 0x417bb
movq %r14, %rdi
callq 0x134d0
movl $0x1, %ebx
jmp 0x418c7
leaq 0x9bc49(%rip), %rdi     # 0xdd4d2
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xac81a
jmp 0x418c7
callq 0x2ca06
movl $0xc8, %esi
movq %r14, %rdi
callq 0x134f0
jmp 0x418c5
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq %r14, %rdi
callq 0x134d0
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 %r14, %rdi
jmp 0x418f2
movq %rax, %rbx
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq %rbx, %rdi
callq 0x138b0
nop