// File: /workspace/github_cpp_makefile_star2_O2/cpottamus[P]btree_lab/disksystem.cc
// Function: DiskSystem::InitFromInMemoryConfig()
ERROR_T DiskSystem::InitFromInMemoryConfig()
{
  string configname = diskfilestem + ".config";
  string dataname = diskfilestem + ".data";
  string bitmapname = diskfilestem + ".bitmap";

  int rc=SanityCheckConfig();

  if (rc) { 
    return rc;
  }

  // it should be the case that none of the files exist
  // except for the data file, since we may be using a chunk of it
  // ie, think parition.

  struct stat s;
  
  if (stat(configname.c_str(),&s)!=-1 ||
      stat(bitmapname.c_str(),&s)!=-1) { 
    cerr << "Configuration or bitmap files exist for this name!\n";
    return ERROR_BADCONFIG;
  }

  
  // OK, now we'll create the files.


  // config
  if (configfilefd) { fclose(configfilefd); }
  
  if ((configfilefd = fopen(configname.c_str(),"w+"))==0) { 
    return ERROR_NOFILE;
  }

  rc = WriteConfig();
  
  if (rc) { 
    return rc;
  }


  // allocate in-memory bitmap

  SIZE_T numbitmapbytes = numblocks / 8 + (numblocks%8 != 0); 
  
  bitmap = new BYTE_T [numbitmapbytes];

  memset(bitmap,0,numbitmapbytes);

  // create the bitmap file and write out the bitmap

  if (bitmapfilefd) { fclose(bitmapfilefd); }

  if ((bitmapfilefd = fopen(bitmapname.c_str(),"w+"))==0) { 
    return ERROR_NOFILE;
  }

  rc = WriteBitMap();
  
  if (rc) { 
    return rc;
  }

  // Now we'll open the data file
  // notice that we will REUSE an existing data file if it exists
  // The idea is that we will write only from offset to offset+blocksize*numblocks

  if (datafilefd) { fclose(datafilefd);}

  if (stat(dataname.c_str(),&s)!=-1) { 
    // reuse existing datafile
    if ((datafilefd = fopen(dataname.c_str(),"r+"))==0) { 
      return ERROR_NOFILE;
    }
  } else {
    // create new data file
    if ((datafilefd = fopen(dataname.c_str(),"w+"))==0) { 
      return ERROR_NOFILE;
    }
  }

  return ERROR_NOERROR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x28(%rdi), %r14
leaq 0x5466(%rip), %rdx      # 0x8361
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x3ffb
leaq 0x545a(%rip), %rdx      # 0x8369
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x3ffb
leaq 0x544c(%rip), %rdx      # 0x836f
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x3ffb
movq %rbx, %rdi
callq 0x34e4
movl %eax, %ebp
testl %eax, %eax
jne 0x2f7c
movq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
jne 0x2f66
movq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
je 0x2faa
movq 0xa083(%rip), %rdi      # 0xcff0
leaq 0x5406(%rip), %rsi      # 0x837a
callq 0x21b0
pushq $-0xc
popq %rbp
leaq 0x8(%rsp), %rdi
callq 0x20b0
leaq 0x28(%rsp), %rdi
callq 0x20b0
leaq 0x48(%rsp), %rdi
callq 0x20b0
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x2fb8
callq 0x21c0
movq 0x48(%rsp), %rdi
leaq 0x53ea(%rip), %rsi      # 0x83ae
callq 0x2260
movq %rax, 0x18(%rbx)
pushq $-0xd
popq %r15
testq %rax, %rax
je 0x3079
movq %rbx, %rdi
callq 0x3274
movl 0x4c(%rbx), %eax
movl %eax, %r14d
shrl $0x3, %r14d
andl $0x7, %eax
cmpl $0x1, %eax
sbbl $-0x1, %r14d
movq %r14, %rdi
callq 0x2030
movq %rax, 0x8(%rbx)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20f0
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x301d
callq 0x21c0
movq 0x8(%rsp), %rdi
leaq 0x5385(%rip), %rsi      # 0x83ae
callq 0x2260
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x3079
movq %rbx, %rdi
callq 0x3476
movl %eax, %ebp
testl %eax, %eax
jne 0x2f7c
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x3057
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
movq 0x28(%rsp), %rdi
cmpl $-0x1, %eax
je 0x3081
leaq 0x5300(%rip), %rsi      # 0x8377
jmp 0x3088
movl %r15d, %ebp
jmp 0x2f7c
leaq 0x5326(%rip), %rsi      # 0x83ae
callq 0x2260
movq %rax, 0x10(%rbx)
movl %r15d, %ebp
testq %rax, %rax
je 0x2f7c
xorl %ebp, %ebp
jmp 0x2f7c
jmp 0x30a8
jmp 0x30a8
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20b0
jmp 0x30ba
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20b0
jmp 0x30c9
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2380
nop