// File: /workspace/github_cpp_makefile_star2_O3/cpottamus[P]btree_lab/btree.cc
// Function: BTreeIndex::LookupOrUpdateInternal(unsigned int const&, BTreeOp, Block const&, Block&)
ERROR_T BTreeIndex::LookupOrUpdateInternal(const SIZE_T &node,
  const BTreeOp op,
  const KEY_T &key,
  VALUE_T &value)
{
  BTreeNode b;
  ERROR_T rc;
  SIZE_T offset;
  KEY_T testkey;
  SIZE_T ptr;

  rc= b.Unserialize(buffercache,node);

  if (rc!=ERROR_NOERROR) { 
    return rc;
  }

  switch (b.info.nodetype) { 
    case BTREE_ROOT_NODE:
    case BTREE_INTERIOR_NODE:
    // Scan through key/ptr pairs
    //and recurse if possible
    for (offset=0;offset<b.info.numkeys;offset++) { 
      rc=b.GetKey(offset,testkey);
      if (rc) {  return rc; }
      if (key<testkey || key==testkey) { //STRUCTURED SO EQUIVALENT KEY VALUES ARE TO THE LEFT
	// OK, so we now have the first key that's larger
	// so we ned to recurse on the ptr immediately previous to 
	// this one, if it exists
       rc=b.GetPtr(offset,ptr);
       if (rc) { return rc; }
       return LookupOrUpdateInternal(ptr,op,key,value);
     }
   }
    // if we got here, we need to go to the next pointer, if it exists
   if (b.info.numkeys>0) { 
    rc=b.GetPtr(b.info.numkeys,ptr);
    if (rc) { return rc; }
    return LookupOrUpdateInternal(ptr,op,key,value);
  } else {
      // There are no keys at all on this node, so nowhere to go
    return ERROR_NONEXISTENT;
  }
  break;
  case BTREE_LEAF_NODE:
    // Scan through keys looking for matching value
  for (offset=0;offset<b.info.numkeys;offset++) { 
    rc=b.GetKey(offset,testkey);
    if (rc) {  return rc; }
    if (testkey==key) { 
     if (op==BTREE_OP_LOOKUP) { 
       return b.GetVal(offset,value);
     } else { 
      rc =  b.SetVal(offset, value);
      if(rc) {return rc;}
      rc = b.Serialize(buffercache, node);
      return rc;
    }
  }
}
return ERROR_NONEXISTENT;
break;
default:
    // We can't be looking at anything other than a root, internal, or leaf
return ERROR_INSANE;
break;
}  

return ERROR_INSANE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %r15
movl %edx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %r14
leaq 0x18(%rsp), %rdi
callq 0x7c4a
leaq 0x40(%rsp), %rdi
callq 0x2c58
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x7e00
movl %eax, %r12d
testl %eax, %eax
je 0x5a1f
leaq 0x40(%rsp), %rdi
callq 0x2e26
leaq 0x18(%rsp), %rdi
callq 0x7c5a
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rsp), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0x5aa2
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
cmpl $0x0, 0x30(%rsp)
je 0x59f9
xorl %r13d, %r13d
leaq 0x18(%rsp), %rbx
leaq 0x40(%rsp), %rbp
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x802a
testl %eax, %eax
jne 0x5b7c
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2e9e
testb %al, %al
jne 0x5afc
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2ec2
testb %al, %al
jne 0x5afc
incl %r13d
movl 0x30(%rsp), %esi
cmpl %esi, %r13d
jb 0x5a45
testl %esi, %esi
je 0x59f9
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x8078
jmp 0x5b0e
movl $0xfffffff1, %r12d      # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x59f9
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
cmpl $0x0, 0x30(%rsp)
je 0x59f9
xorl %ebx, %ebx
leaq 0x40(%rsp), %rbp
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x802a
testl %eax, %eax
jne 0x5b7c
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2ec2
testb %al, %al
jne 0x5b34
incl %ebx
cmpl 0x30(%rsp), %ebx
jb 0x5ac9
jmp 0x59f9
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r13d, %esi
callq 0x8078
movl %eax, %r12d
testl %eax, %eax
jne 0x59f9
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
movl 0x8(%rsp), %edx
movq %r15, %rcx
movq 0x10(%rsp), %r8
callq 0x59ac
jmp 0x5b7c
cmpl $0x3, 0x8(%rsp)
jne 0x5b4e
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x8096
jmp 0x5b7c
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x816c
movl %eax, %r12d
testl %eax, %eax
jne 0x59f9
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x7d3a
movl %eax, %r12d
jmp 0x59f9
movq %rax, %rbx
jmp 0x5b9a
jmp 0x5b8d
jmp 0x5b8d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2e26
leaq 0x18(%rsp), %rdi
callq 0x7c5a
movq %rbx, %rdi
callq 0x2370