// File: /workspace/github_cpp_makefile_star2_O2/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 0x725a
leaq 0x40(%rsp), %rdi
callq 0x2ac4
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x73fc
movl %eax, %r12d
testl %eax, %eax
je 0x5389
leaq 0x40(%rsp), %rdi
callq 0x2c6e
leaq 0x18(%rsp), %rdi
callq 0x7264
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 0x53e7
xorl %r13d, %r13d
leaq 0x18(%rsp), %rbx
leaq 0x40(%rsp), %rbp
movl 0x30(%rsp), %esi
cmpl %esi, %r13d
jae 0x5448
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x7622
movl %eax, %r12d
testl %eax, %eax
jne 0x5363
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2ccc
testb %al, %al
jne 0x5434
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2cf0
testb %al, %al
jne 0x5434
incl %r13d
jmp 0x53a2
cmpl $0x4, %eax
jne 0x542b
xorl %ebx, %ebx
pushq $-0x9
popq %r12
leaq 0x40(%rsp), %rbp
cmpl 0x30(%rsp), %ebx
jae 0x5363
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x7622
testl %eax, %eax
jne 0x54cd
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2cf0
testb %al, %al
jne 0x5481
incl %ebx
jmp 0x53f7
pushq $-0xf
popq %r12
jmp 0x5363
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r13d, %esi
callq 0x766e
jmp 0x545b
testl %esi, %esi
je 0x549b
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x766e
movl %eax, %r12d
testl %eax, %eax
jne 0x5363
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
movl 0x8(%rsp), %edx
movq %r15, %rcx
movq 0x10(%rsp), %r8
callq 0x5316
jmp 0x54cd
cmpl $0x3, 0x8(%rsp)
jne 0x549f
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x768a
jmp 0x54cd
pushq $-0x9
jmp 0x542d
leaq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0x775a
movl %eax, %r12d
testl %eax, %eax
jne 0x5363
movq 0x8(%r14), %rsi
movl (%r13), %edx
leaq 0x18(%rsp), %rdi
callq 0x7338
movl %eax, %r12d
jmp 0x5363
movq %rax, %rbx
jmp 0x54eb
jmp 0x54de
jmp 0x54de
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2c6e
leaq 0x18(%rsp), %rdi
callq 0x7264
movq %rbx, %rdi
callq 0x2380
nop