// File: /workspace/github_cpp_makefile_star2_O1/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 0x7d2a
leaq 0x40(%rsp), %rdi
callq 0x2c54
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x7eee
movl %eax, %r12d
testl %eax, %eax
je 0x5a3d
leaq 0x40(%rsp), %rdi
callq 0x2e22
leaq 0x18(%rsp), %rdi
callq 0x7d3a
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 0x5aba
movl 0x30(%rsp), %esi
testl %esi, %esi
je 0x5aa1
xorl %r13d, %r13d
leaq 0x18(%rsp), %rbx
leaq 0x40(%rsp), %rbp
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x8114
movl %eax, %r12d
testl %eax, %eax
jne 0x5a17
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2ebc
testb %al, %al
jne 0x5b10
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2ee0
testb %al, %al
jne 0x5b10
incl %r13d
movl 0x30(%rsp), %esi
cmpl %esi, %r13d
jb 0x5a5e
testl %esi, %esi
je 0x5b4e
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x8162
jmp 0x5b22
movl $0xfffffff1, %r12d      # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x5a17
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
cmpl $0x0, 0x30(%rsp)
je 0x5a17
xorl %ebx, %ebx
leaq 0x40(%rsp), %rbp
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x8114
testl %eax, %eax
jne 0x5b46
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2ee0
testb %al, %al
jne 0x5b59
incl %ebx
cmpl 0x30(%rsp), %ebx
jb 0x5ae1
jmp 0x5a17
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r13d, %esi
callq 0x8162
movl %eax, %r12d
testl %eax, %eax
jne 0x5a17
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
movl 0x8(%rsp), %edx
movq %r15, %rcx
movq 0x10(%rsp), %r8
callq 0x59ca
movl %eax, %r12d
jmp 0x5a17
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
jmp 0x5a17
cmpl $0x3, 0x8(%rsp)
jne 0x5b73
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x8180
jmp 0x5b46
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x8256
movl %eax, %r12d
testl %eax, %eax
jne 0x5a17
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x7e28
jmp 0x5b46
movq %rax, %rbx
jmp 0x5bb9
jmp 0x5bac
jmp 0x5bac
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2e22
leaq 0x18(%rsp), %rdi
callq 0x7d3a
movq %rbx, %rdi
callq 0x2370
nop