RtlInsertElementGenericTableFullAvl

VOID *__stdcall RtlInsertElementGenericTableFullAvl(
        _RTL_AVL_TABLE *Table,
        VOID *Buffer,
        UINT64 BufferSize,
        UINT8 *NewElement,
        VOID *NodeOrParent,
        _TABLE_SEARCH_RESULT SearchResult){
  size_t v7; 
  __int64 v10; 
  char *v11; 
  _RTL_BALANCED_LINKS *v12; 
  __int64 v13; 
  _RTL_BALANCED_LINKS *i; 
  bool v15; 
  char v16; 
  char Balance; 

  v7 = BufferSize;
  if( SearchResult == TableFoundNode )
  {
    v11 = (char *)NodeOrParent;
LABEL_16:
    if( NewElement )
      *NewElement = SearchResult != TableFoundNode;
    Table->WhichOrderedElement = 0;
    Table->OrderedPointer = 0i64;
    return v11 + 32;
  }
  if( (int)BufferSize + 32 >= (unsigned int)BufferSize )
  {
    v10 = ((__int64(*)(void))Table->AllocateRoutine)();
    v11 = (char *)v10;
    if( v10 )
    {
      *(_OWORD *)v10 = 0i64;
      *(_OWORD *)(v10 + 16) = 0i64;
      ++Table->NumberGenericTableElements;
      if( SearchResult )
      {
        v12 = (_RTL_BALANCED_LINKS *)NodeOrParent;
        v13 = v10;
        if( SearchResult == TableInsertAsLeft )
          *((_QWORD *)NodeOrParent + 1) = v10;
        else
          *((_QWORD *)NodeOrParent + 2) = v10;
        *(_QWORD *)v10 = NodeOrParent;
        Table->BalancedRoot.Balance = -1;
        for( i = *(_RTL_BALANCED_LINKS **)v10; ; v12 = i )
        {
          v15 = i->LeftChild == (_RTL_BALANCED_LINKS *)v13;
          v16 = -1;
          Balance = v12->Balance;
          if( !v15 )
            v16 = 1;
          if( Balance )
            break;
          i = v12->Parent;
          v13 = (__int64)v12;
          v12->Balance = v16;
        }
        if( Balance == v16 )
        {
          RebalanceNode(v12);
        }
        else
        {
          v12->Balance = 0;
          if( !Table->BalancedRoot.Balance )
            ++Table->DepthOfTree;
        }
      }
      else
      {
        Table->BalancedRoot.RightChild = (_RTL_BALANCED_LINKS *)v10;
        *(_QWORD *)v10 = Table;
        Table->DepthOfTree = 1;
      }
      memmove(v11 + 32, Buffer, v7);
      goto LABEL_16;
    }
  }
  if( NewElement )
    *NewElement = 0;
  return 0i64;
}

Referenced by:

PiDmAddCacheReferenceForObject
PiPnpRtlObjectEventCreate
RtlInsertElementGenericTableAvl