// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x28(%rdi), %rsi
movq 0x30(%rdi), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x4670
leaq 0x61e5(%rip), %rsi      # 0x9361
movq %r14, %rdi
callq 0x2390
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x28(%rbx), %rsi
movq 0x30(%rbx), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x4670
leaq 0x61bf(%rip), %rsi      # 0x9369
leaq 0x28(%rsp), %rdi
callq 0x2390
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x28(%rbx), %rsi
movq 0x30(%rbx), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x4670
leaq 0x6196(%rip), %rsi      # 0x936f
leaq 0x8(%rsp), %rdi
callq 0x2390
movq %rbx, %rdi
callq 0x38d0
movl %eax, %ebp
testl %eax, %eax
jne 0x3236
movq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
jne 0x3219
movq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
je 0x328f
movl $0xfffffff4, %ebp       # imm = 0xFFFFFFF4
movq 0xadcb(%rip), %rdi      # 0xdff0
leaq 0x614e(%rip), %rsi      # 0x937a
movl $0x33, %edx
callq 0x2230
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x324d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x3264
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x327b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21e0
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x329d
callq 0x21c0
movq 0x48(%rsp), %rdi
leaq 0x6105(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x18(%rbx)
movl $0xfffffff3, %ebp       # imm = 0xFFFFFFF3
testq %rax, %rax
je 0x3236
movq %rbx, %rdi
callq 0x3644
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 0x3303
callq 0x21c0
movq 0x8(%rsp), %rdi
leaq 0x609f(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x3236
movq %rbx, %rdi
callq 0x385a
movl %eax, %ebp
testl %eax, %eax
jne 0x3236
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x3341
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
movq 0x28(%rsp), %rdi
cmpl $-0x1, %eax
je 0x3363
leaq 0x6016(%rip), %rsi      # 0x9377
jmp 0x336a
leaq 0x6044(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x10(%rbx)
movl $0xfffffff3, %ebp       # imm = 0xFFFFFFF3
testq %rax, %rax
je 0x3236
xorl %ebp, %ebp
jmp 0x3236
jmp 0x338e
jmp 0x338e
jmp 0x338e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x33af
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0x33af
jmp 0x33ac
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x33cd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0x33cd
jmp 0x33ca
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x33e4
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2370