// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x130(%rbp)
addq $0x28, %rsi
movq %rsi, -0x128(%rbp)
leaq 0xb7e5(%rip), %rdx      # 0x10371
leaq -0x30(%rbp), %rdi
callq 0x6870
movq -0x128(%rbp), %rsi
leaq 0xb7d6(%rip), %rdx      # 0x10379
leaq -0x50(%rbp), %rdi
callq 0x6870
jmp 0x4bae
movq -0x130(%rbp), %rsi
addq $0x28, %rsi
leaq 0xb7bf(%rip), %rdx      # 0x1037f
leaq -0x80(%rbp), %rdi
callq 0x6870
jmp 0x4bcb
movq -0x130(%rbp), %rdi
callq 0x5660
movl %eax, -0x134(%rbp)
jmp 0x4bdf
movl -0x134(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x4c48
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4f86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4f7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3120
jmp 0x4f7d
leaq -0x30(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq -0x118(%rbp), %rsi
callq 0x31d0
cmpl $-0x1, %eax
jne 0x4c82
leaq -0x80(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq -0x118(%rbp), %rsi
callq 0x31d0
cmpl $-0x1, %eax
je 0x4cad
movq 0x12367(%rip), %rdi     # 0x16ff0
leaq 0xb6fa(%rip), %rsi      # 0x1038a
callq 0x31f0
jmp 0x4c97
movl $0xfffffff4, -0x4(%rbp) # imm = 0xFFFFFFF4
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq -0x130(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x4ccf
movq -0x130(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3200
jmp 0x4ccd
jmp 0x4ccf
leaq -0x30(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq 0xb6dc(%rip), %rsi      # 0x103be
callq 0x32e0
movq %rax, -0x140(%rbp)
jmp 0x4cf0
movq -0x140(%rbp), %rax
movq -0x130(%rbp), %rcx
movq %rax, 0x18(%rcx)
cmpq $0x0, %rax
jne 0x4d1e
movl $0xfffffff3, -0x4(%rbp) # imm = 0xFFFFFFF3
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq -0x130(%rbp), %rdi
callq 0x5350
movl %eax, -0x144(%rbp)
jmp 0x4d32
movl -0x144(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x4d5f
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq -0x130(%rbp), %rax
movl 0x4c(%rax), %ecx
movl %ecx, %eax
shrl $0x3, %eax
andl $0x7, %ecx
subl $0x1, %ecx
sbbl $-0x1, %eax
movl %eax, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x150(%rbp)
jmp 0x4d93
movq -0x130(%rbp), %rax
movq -0x150(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rdi
movl -0x11c(%rbp), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x3100
movq -0x130(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x4dda
movq -0x130(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x3200
jmp 0x4dd8
jmp 0x4dda
leaq -0x80(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq 0xb5d1(%rip), %rsi      # 0x103be
callq 0x32e0
movq %rax, -0x158(%rbp)
jmp 0x4dfb
movq -0x158(%rbp), %rax
movq -0x130(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x4e29
movl $0xfffffff3, -0x4(%rbp) # imm = 0xFFFFFFF3
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq -0x130(%rbp), %rdi
callq 0x55a0
movl %eax, -0x15c(%rbp)
jmp 0x4e3d
movl -0x15c(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x4e6a
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0x4f56
movq -0x130(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4e8c
movq -0x130(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x3200
jmp 0x4e8a
jmp 0x4e8c
leaq -0x50(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq -0x118(%rbp), %rsi
callq 0x31d0
cmpl $-0x1, %eax
je 0x4ef7
leaq -0x50(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq 0xb4cb(%rip), %rsi      # 0x10387
callq 0x32e0
movq %rax, -0x168(%rbp)
jmp 0x4eca
movq -0x168(%rbp), %rax
movq -0x130(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x4ef5
movl $0xfffffff3, -0x4(%rbp) # imm = 0xFFFFFFF3
movl $0x1, -0x88(%rbp)
jmp 0x4f56
jmp 0x4f45
leaq -0x50(%rbp), %rdi
callq 0x30a0
movq %rax, %rdi
leaq 0xb4b4(%rip), %rsi      # 0x103be
callq 0x32e0
movq %rax, -0x170(%rbp)
jmp 0x4f18
movq -0x170(%rbp), %rax
movq -0x130(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x4f43
movl $0xfffffff3, -0x4(%rbp) # imm = 0xFFFFFFF3
movl $0x1, -0x88(%rbp)
jmp 0x4f56
jmp 0x4f45
movl $0x0, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3120
leaq -0x50(%rbp), %rdi
callq 0x3120
leaq -0x30(%rbp), %rdi
callq 0x3120
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x3120
leaq -0x30(%rbp), %rdi
callq 0x3120
movq -0x58(%rbp), %rdi
callq 0x3400
nopl (%rax,%rax)