// File: /workspace/github_cpp_makefile_star2_O3/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 0x4658
leaq 0x6201(%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 0x4658
leaq 0x61db(%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 0x4658
leaq 0x61b2(%rip), %rsi      # 0x936f
leaq 0x8(%rsp), %rdi
callq 0x2390
movq %rbx, %rdi
callq 0x38b4
movl %eax, %ebp
testl %eax, %eax
jne 0x321a
movq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
jne 0x31fd
movq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
cmpl $-0x1, %eax
je 0x3273
movl $0xfffffff4, %ebp       # imm = 0xFFFFFFF4
movq 0xade7(%rip), %rdi      # 0xdff0
leaq 0x616a(%rip), %rsi      # 0x937a
movl $0x33, %edx
callq 0x2230
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x3231
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x3248
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x325f
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 0x3281
callq 0x21c0
movq 0x48(%rsp), %rdi
leaq 0x6121(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x18(%rbx)
movl $0xfffffff3, %ebp       # imm = 0xFFFFFFF3
testq %rax, %rax
je 0x321a
movq %rbx, %rdi
callq 0x3628
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 0x32e7
callq 0x21c0
movq 0x8(%rsp), %rdi
leaq 0x60bb(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x321a
movq %rbx, %rdi
callq 0x383e
movl %eax, %ebp
testl %eax, %eax
jne 0x321a
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x3325
callq 0x21c0
movq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2190
movq 0x28(%rsp), %rdi
cmpl $-0x1, %eax
je 0x3347
leaq 0x6032(%rip), %rsi      # 0x9377
jmp 0x334e
leaq 0x6060(%rip), %rsi      # 0x93ae
callq 0x2260
movq %rax, 0x10(%rbx)
movl $0xfffffff3, %ebp       # imm = 0xFFFFFFF3
testq %rax, %rax
je 0x321a
xorl %ebp, %ebp
jmp 0x321a
jmp 0x3372
jmp 0x3372
jmp 0x3372
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x3393
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0x3393
jmp 0x3390
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x33b1
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0x33b1
jmp 0x33ae
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x33c8
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2370