// File: /workspace/github_cpp_makefile_star2_O3/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 0x5942
movq %rdi, %rbx
testb %dl, %dl
je 0x591d
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x4b3c
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7c7c
movl 0x10(%rbx), %esi
movd %esi, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
paddd 0x3eb8(%rip), %xmm0     # 0x9690
movq %xmm0, 0x10(%r15)
movl $0x0, 0x18(%r15)
movq 0x8(%rbx), %rdi
callq 0x4b52
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
leaq 0x28(%rsp), %rdi
callq 0x7d3a
movl %eax, %ebp
testl %eax, %eax
je 0x5815
leaq 0x28(%rsp), %rdi
callq 0x7c5a
jmp 0x5931
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x4b3c
movq %rsp, %rdi
movl $0x2, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7c7c
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 0x4b52
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
incl %edx
movq %rsp, %rdi
callq 0x7d3a
movl %eax, %ebp
testl %eax, %eax
je 0x587b
xorl %r13d, %r13d
jmp 0x5906
movl 0x10(%rbx), %r15d
addl $0x2, %r15d
leaq 0x50(%rsp), %r14
movq 0x8(%rbx), %rdi
callq 0x4b44
cmpl %eax, %r15d
setae %r13b
jae 0x5906
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r12d
movq 0x8(%rbx), %rdi
callq 0x4b3c
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
movl %r12d, %ecx
movl %eax, %r8d
callq 0x7c7c
movl 0x10(%rbx), %eax
incl %eax
movl %eax, 0x60(%rsp)
movq 0x8(%rbx), %rdi
callq 0x4b44
leal 0x1(%r15), %r12d
cmpl %eax, %r12d
movl %r12d, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, 0x64(%rsp)
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x7d3a
movl %eax, %ebp
movq %r14, %rdi
callq 0x7c5a
movl %r12d, %r15d
testl %ebp, %ebp
je 0x5888
jmp 0x5906
movq %rsp, %rdi
callq 0x7c5a
leaq 0x28(%rsp), %rdi
callq 0x7c5a
testb %r13b, %r13b
je 0x5931
movq 0x8(%rbx), %rsi
addq $0x18, %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7e00
movl %eax, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e1e(%rip), %rdi      # 0x9767
leaq 0x3d84(%rip), %rsi      # 0x96d4
leaq 0x3e24(%rip), %rcx      # 0x977b
movl $0x7b, %edx
callq 0x2110
jmp 0x596c
jmp 0x5965
movq %rax, %rbx
jmp 0x5986
jmp 0x596c
movq %rax, %rbx
jmp 0x597e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x7c5a
movq %rsp, %rdi
callq 0x7c5a
leaq 0x28(%rsp), %rdi
callq 0x7c5a
movq %rbx, %rdi
callq 0x2370