// File: /workspace/github_cpp_makefile_star2_O2/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 0x52ad
movq %rdi, %rbx
testb %dl, %dl
je 0x5288
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x44c4
leaq 0x50(%rsp), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7280
movl 0x10(%rbx), %esi
movd %esi, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
paddd 0x353e(%rip), %xmm0     # 0x8690
movq %xmm0, 0x10(%r15)
andl $0x0, 0x18(%r15)
movq 0x8(%rbx), %rdi
callq 0x44da
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
leaq 0x50(%rsp), %rdi
callq 0x7338
movl %eax, %ebp
testl %eax, %eax
je 0x5185
xorl %r13d, %r13d
jmp 0x5279
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r14d
movq 0x8(%rbx), %rdi
callq 0x44c4
movq %rsp, %rdi
pushq $0x2
popq %rsi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x7280
movl 0x10(%rbx), %eax
leal 0x1(%rax), %esi
movl %esi, 0x10(%rsp)
addl $0x2, %eax
movl %eax, 0x14(%rsp)
andl $0x0, 0x18(%rsp)
movq 0x8(%rbx), %rdi
callq 0x44da
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
incl %edx
movq %rsp, %rdi
callq 0x7338
movl %eax, %ebp
testl %eax, %eax
je 0x51e6
xorl %r13d, %r13d
jmp 0x5271
movl 0x10(%rbx), %r15d
addl $0x2, %r15d
leaq 0x28(%rsp), %r14
movq 0x8(%rbx), %rdi
callq 0x44cc
cmpl %eax, %r15d
setae %r13b
jae 0x5271
movl 0x1c(%rbx), %ebp
movl 0x20(%rbx), %r12d
movq 0x8(%rbx), %rdi
callq 0x44c4
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
movl %r12d, %ecx
movl %eax, %r8d
callq 0x7280
movl 0x10(%rbx), %eax
incl %eax
movl %eax, 0x38(%rsp)
movq 0x8(%rbx), %rdi
callq 0x44cc
leal 0x1(%r15), %r12d
cmpl %eax, %r12d
movl %r12d, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, 0x3c(%rsp)
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x7338
movl %eax, %ebp
movq %r14, %rdi
callq 0x7264
movl %r12d, %r15d
testl %ebp, %ebp
je 0x51f3
jmp 0x5271
movq %rsp, %rdi
callq 0x7264
leaq 0x50(%rsp), %rdi
callq 0x7264
testb %r13b, %r13b
je 0x529c
movq 0x8(%rbx), %rsi
addq $0x18, %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x73fc
movl %eax, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34b3(%rip), %rdi      # 0x8767
leaq 0x3419(%rip), %rsi      # 0x86d4
leaq 0x34b9(%rip), %rcx      # 0x877b
pushq $0x7b
popq %rdx
callq 0x2110
jmp 0x52d5
jmp 0x52ce
movq %rax, %rbx
jmp 0x52ef
jmp 0x52d5
movq %rax, %rbx
jmp 0x52e7
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7264
movq %rsp, %rdi
callq 0x7264
leaq 0x50(%rsp), %rdi
callq 0x7264
movq %rbx, %rdi
callq 0x2380
nop