// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movb %dl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
andb $0x1, %al
movb %al, -0x15(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, 0x10(%rax)
cmpl $0x0, 0x10(%rax)
jne 0xa0b2
jmp 0xa0d1
leaq 0x66a6(%rip), %rdi      # 0x1075f
leaq 0x660c(%rip), %rsi      # 0x106cc
movl $0x7b, %edx
leaq 0x66a7(%rip), %rcx      # 0x10773
callq 0x3130
testb $0x1, -0x15(%rbp)
je 0xa42e
movq -0xb8(%rbp), %rax
movl 0x1c(%rax), %ecx
movl %ecx, -0xc0(%rbp)
movl 0x20(%rax), %ecx
movl %ecx, -0xbc(%rbp)
movq 0x8(%rax), %rdi
callq 0x79c0
movl -0xc0(%rbp), %edx
movl -0xbc(%rbp), %ecx
movl %eax, %r8d
leaq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0xeef0
movq -0xb8(%rbp), %rax
movl 0x10(%rax), %ecx
incl %ecx
movl %ecx, -0x38(%rbp)
movl 0x10(%rax), %ecx
addl $0x2, %ecx
movl %ecx, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movq 0x8(%rax), %rdi
movl 0x10(%rax), %esi
callq 0x7a20
jmp 0xa147
movq -0xb8(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x10(%rax), %edx
leaq -0x48(%rbp), %rdi
callq 0xf080
movl %eax, -0xc4(%rbp)
jmp 0xa166
movl -0xc4(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xa198
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0xa404
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa423
movq -0xb8(%rbp), %rax
movl 0x1c(%rax), %ecx
movl %ecx, -0xd0(%rbp)
movl 0x20(%rax), %ecx
movl %ecx, -0xcc(%rbp)
movq 0x8(%rax), %rdi
callq 0x79c0
movl %eax, -0xc8(%rbp)
jmp 0xa1c2
movl -0xc8(%rbp), %r8d
movl -0xcc(%rbp), %ecx
movl -0xd0(%rbp), %edx
leaq -0x80(%rbp), %rdi
movl $0x2, %esi
callq 0xeef0
jmp 0xa1e5
movq -0xb8(%rbp), %rax
movl 0x10(%rax), %ecx
incl %ecx
movl %ecx, -0x70(%rbp)
movl 0x10(%rax), %ecx
addl $0x2, %ecx
movl %ecx, -0x6c(%rbp)
movl $0x0, -0x68(%rbp)
movq 0x8(%rax), %rdi
movl 0x10(%rax), %esi
incl %esi
callq 0x7a20
jmp 0xa214
movq -0xb8(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x10(%rax), %edx
incl %edx
leaq -0x80(%rbp), %rdi
callq 0xf080
movl %eax, -0xd4(%rbp)
jmp 0xa235
movl -0xd4(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xa267
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0xa3fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa41a
movq -0xb8(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x2, %eax
movl %eax, -0x84(%rbp)
movq -0xb8(%rbp), %rax
movl -0x84(%rbp), %ecx
movl %ecx, -0xdc(%rbp)
movq 0x8(%rax), %rdi
callq 0x79e0
movl %eax, -0xd8(%rbp)
jmp 0xa29e
movl -0xdc(%rbp), %eax
movl -0xd8(%rbp), %ecx
cmpl %ecx, %eax
jae 0xa3f4
movq -0xb8(%rbp), %rax
movl 0x1c(%rax), %ecx
movl %ecx, -0xe8(%rbp)
movl 0x20(%rax), %ecx
movl %ecx, -0xe4(%rbp)
movq 0x8(%rax), %rdi
callq 0x79c0
movl %eax, -0xe0(%rbp)
jmp 0xa2dc
movl -0xe0(%rbp), %r8d
movl -0xe4(%rbp), %ecx
movl -0xe8(%rbp), %edx
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0xeef0
jmp 0xa2ff
movq -0xb8(%rbp), %rax
movl 0x10(%rax), %ecx
incl %ecx
movl %ecx, -0xa0(%rbp)
movl -0x84(%rbp), %ecx
incl %ecx
movl %ecx, -0xf0(%rbp)
movq 0x8(%rax), %rdi
callq 0x79e0
movl %eax, -0xec(%rbp)
jmp 0xa330
movl -0xf0(%rbp), %eax
movl -0xec(%rbp), %ecx
cmpl %ecx, %eax
jne 0xa34a
xorl %eax, %eax
movl %eax, -0xf4(%rbp)
jmp 0xa359
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
movq -0xb8(%rbp), %rax
movl -0xf4(%rbp), %ecx
movl %ecx, -0x9c(%rbp)
movq 0x8(%rax), %rsi
movl -0x84(%rbp), %edx
leaq -0xb0(%rbp), %rdi
callq 0xf080
movl %eax, -0xf8(%rbp)
jmp 0xa38a
movl -0xf8(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0xa3c2
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0xa3c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xeea0
jmp 0xa41a
movl $0x0, -0x58(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xeea0
movl -0x58(%rbp), %eax
testl %eax, %eax
jne 0xa3fb
jmp 0xa3de
jmp 0xa3e0
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xa27a
movl $0x0, -0x58(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xeea0
leaq -0x48(%rbp), %rdi
callq 0xeea0
movl -0x58(%rbp), %eax
testl %eax, %eax
je 0xa418
jmp 0xa416
jmp 0xa44b
jmp 0xa42e
leaq -0x80(%rbp), %rdi
callq 0xeea0
leaq -0x48(%rbp), %rdi
callq 0xeea0
jmp 0xa457
movq -0xb8(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movq 0x8(%rax), %rsi
movl -0x14(%rbp), %edx
callq 0xf1a0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x3400