// File: /workspace/github_cpp_makefile_star2_O1/cpottamus[P]btree_lab/btree.cc
// Function: BTreeIndex::Attach(unsigned int, bool)
ERROR_T BTreeIndex::Attach(const SIZE_T initblock, const bool create)
{
  ERROR_T rc;

  superblock_index=initblock;
  assert(superblock_index==0);

  if (create) {
    // build a super block, root node, and a free space list
    //
    // Superblock at superblock_index
    // root node at superblock_index+1
    // free space list for rest
    BTreeNode newsuperblock(BTREE_SUPERBLOCK,
     superblock.info.keysize,
     superblock.info.valuesize,
     buffercache->GetBlockSize());
    newsuperblock.info.rootnode=superblock_index+1;
    newsuperblock.info.freelist=superblock_index+2;
    newsuperblock.info.numkeys=0;

    buffercache->NotifyAllocateBlock(superblock_index);

    rc=newsuperblock.Serialize(buffercache,superblock_index);

    if (rc) { 
      return rc;
    }
    
    BTreeNode newrootnode(BTREE_ROOT_NODE,
     superblock.info.keysize,
     superblock.info.valuesize,
     buffercache->GetBlockSize());
    newrootnode.info.rootnode=superblock_index+1;
    newrootnode.info.freelist=superblock_index+2;
    newrootnode.info.numkeys=0;

    buffercache->NotifyAllocateBlock(superblock_index+1);

    rc=newrootnode.Serialize(buffercache,superblock_index+1);

    if (rc) { 
      return rc;
    }

    for (SIZE_T i=superblock_index+2; i<buffercache->GetNumBlocks();i++) { 
      BTreeNode newfreenode(BTREE_UNALLOCATED_BLOCK,
       superblock.info.keysize,
       superblock.info.valuesize,
       buffercache->GetBlockSize());
      newfreenode.info.rootnode=superblock_index+1;
      newfreenode.info.freelist= ((i+1)==buffercache->GetNumBlocks()) ? 0: i+1;
      
      rc = newfreenode.Serialize(buffercache,i);

      if (rc) {
       return rc;
     }

   }
 }

  // OK, now, mounting the btree is simply a matter of reading the superblock 

 return superblock.Unserialize(buffercache,initblock);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, 0x10(%rdi)
testl %esi, %esi
jne 0x595f
movq %rdi, %rbx
testb %dl, %dl
je 0x593a
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x4b3a
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7d5c
movl 0x10(%rbx), %esi
leal 0x1(%rsi), %eax
movl %eax, 0x10(%r15)
leal 0x2(%rsi), %eax
movl %eax, 0x14(%r15)
movl $0x0, 0x18(%r15)
movq 0x8(%rbx), %rdi
callq 0x4b50
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
leaq 0x50(%rsp), %rdi
callq 0x7e28
movl %eax, %ebp
testl %eax, %eax
je 0x5829
xorl %r12d, %r12d
jmp 0x592b
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x4b3a
movq %rsp, %rdi
movl $0x2, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7d5c
movl 0x10(%rbx), %eax
leal 0x1(%rax), %esi
movl %esi, 0x10(%rsp)
addl $0x2, %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x8(%rbx), %rdi
callq 0x4b50
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
incl %edx
movq %rsp, %rdi
callq 0x7e28
movl %eax, %ebp
testl %eax, %eax
jne 0x5920
movl 0x10(%rbx), %r15d
addl $0x2, %r15d
leaq 0x28(%rsp), %r14
movq 0x8(%rbx), %rdi
callq 0x4b42
cmpl %eax, %r15d
setae %r12b
jae 0x5923
movl 0x1c(%rbx), %r12d
movl 0x20(%rbx), %r13d
movq 0x8(%rbx), %rdi
callq 0x4b3a
movq %r14, %rdi
xorl %esi, %esi
movl %r12d, %edx
movl %r13d, %ecx
movl %eax, %r8d
callq 0x7d5c
movl 0x10(%rbx), %eax
incl %eax
movl %eax, 0x38(%rsp)
movq 0x8(%rbx), %rdi
callq 0x4b42
leal 0x1(%r15), %r13d
cmpl %eax, %r13d
movl %r13d, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, 0x3c(%rsp)
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x7e28
movl %eax, %r12d
movq %r14, %rdi
callq 0x7d3a
movl %r13d, %r15d
testl %r12d, %r12d
cmovnel %r12d, %ebp
je 0x5898
xorl %r12d, %r12d
movq %rsp, %rdi
callq 0x7d3a
leaq 0x50(%rsp), %rdi
callq 0x7d3a
testb %r12b, %r12b
je 0x594e
movq 0x8(%rbx), %rsi
addq $0x18, %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7eee
movl %eax, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3de9(%rip), %rdi      # 0x974f
leaq 0x3d4f(%rip), %rsi      # 0x96bc
leaq 0x3def(%rip), %rcx      # 0x9763
movl $0x7b, %edx
callq 0x2110
jmp 0x5989
jmp 0x5982
movq %rax, %rbx
jmp 0x59a3
jmp 0x5989
movq %rax, %rbx
jmp 0x599b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7d3a
movq %rsp, %rdi
callq 0x7d3a
leaq 0x50(%rsp), %rdi
callq 0x7d3a
movq %rbx, %rdi
callq 0x2370
nop