MiAddRangeToPartitionTree

VOID __fastcall MiAddRangeToPartitionTree(
        _RTL_AVL_TREE *PageRoot,
        UINT64 BasePage,
        UINT64 PageCount,
        UINT64 EntireRangeIsZeroed){
  unsigned __int64 v5; 
  int v6; 
  UINT64 v7; 
  UINT64 v8; 
  UINT64 v9; 
  UINT64 v10; 
  UINT64 v11; 
  _RTL_BALANCED_NODE *Root; 
  unsigned __int64 v13; 
  __int64 v14; 
  unsigned __int64 v15; 
  _RTL_BALANCED_NODE *Pool; 
  VOID *v17; 
  _RTL_BITMAP_EX *p_Right; 
  __int64 v19; 
  BOOL v20; 
  _RTL_BALANCED_NODE *v21; 
  unsigned __int64 v22; 
  _RTL_BALANCED_NODE *v23; 
  __int64 v24; 
  __int64 v25; 
  UINT64 v26; 
  UINT64 v27; 
  _RTL_AVL_TREE *Tree; 
  UINT64 v29; 
  UINT64 v30; 
  int v31; 

  v31 = EntireRangeIsZeroed;
  v29 = BasePage;
  Tree = PageRoot;
  v5 = 0i64;
  v6 = 0;
  v7 = 0i64;
  v8 = BasePage & 0x7FFFFFFFFFFC0000i64;
  v9 = BasePage + 0x3FFFF;
  v27 = BasePage + 0x3FFFF;
  v30 = BasePage & 0x7FFFFFFFFFFC0000i64;
LABEL_2:
  v5 = v8 | v5 & 0x8000000000000000ui64;
  v10 = BasePage;
  v11 = (PageCount + v9 - (v5 & 0x7FFFFFFFFFFFFFFFi64)) >> 18;
LABEL_3:
  if( v11 )
  {
    Root = PageRoot->Root;
    if( !PageRoot->Root )
      goto LABEL_11;
    v13 = v5 & 0x7FFFFFFFFFFFFFFFi64;
    while( 1 )
    {
      v14 = (__int64)Root[1].Children[0];
      v15 = v14 & 0x7FFFFFFFFFFFFFFFi64;
      if( v13 < (v14 & 0x7FFFFFFFFFFFFFFFui64) )
      {
        Root = Root->Children[0];
      }
      else
      {
        if( v13 <= v15 )
        {
          if( Root )
          {
            p_Right = (_RTL_BITMAP_EX *)&Root[1].Right;
            if( v14 >= 0 || (_DWORD)EntireRangeIsZeroed != 1 )
            {
              Root[1].Children[0] = (_RTL_BALANCED_NODE *)v15;
              v14 &= ~0x8000000000000000ui64;
            }
            goto LABEL_21;
          }
LABEL_11:
          Pool = (_RTL_BALANCED_NODE *)MiAllocatePool(64i64, 0x30ui64, 0x7070694Dui64);
          if( Pool )
          {
            v17 = MiAllocatePool(64i64, 0x8000ui64, 0x6270694Dui64);
            if( v17 )
            {
              p_Right = (_RTL_BITMAP_EX *)&Pool[1].Right;
              Pool[1].Children[1] = (_RTL_BALANCED_NODE *)0x40000;
              Pool[1].ParentValue = (unsigned __int64)v17;
              v19 = (unsigned __int64)Pool[1].Children[0] ^ (v5 ^ (unsigned __int64)Pool[1].Children[0]) & 0x7FFFFFFFFFFFFFFFi64;
              Pool[1].Children[0] = (_RTL_BALANCED_NODE *)v19;
              if( v31 == 1 )
              {
                v19 |= 0x8000000000000000ui64;
                Pool[1].Children[0] = (_RTL_BALANCED_NODE *)v19;
              }
              v20 = 0;
              v21 = Tree->Root;
              if( Tree->Root )
              {
                v22 = v19 & 0x7FFFFFFFFFFFFFFFi64;
                while( 1 )
                {
                  if( v22 < ((unsigned __int64)v21[1].Children[0] & 0x7FFFFFFFFFFFFFFFi64) )
                  {
                    v23 = v21->Children[0];
                    if( !v21->Children[0] )
                      break;
                  }
                  else
                  {
                    v23 = v21->Children[1];
                    if( !v23 )
                    {
                      v20 = 1;
                      break;
                    }
                  }
                  v21 = v23;
                }
              }
              RtlAvlInsertNodeEx(&Tree->Root, (UINT64)v21, v20, Pool);
              v14 = (__int64)Pool[1].Children[0];
LABEL_21:
              v24 = v14 & 0x7FFFFFFFFFFFFFFFi64;
              v25 = PageCount;
              if( v24 - v10 + 0x40000 <= PageCount )
                v25 = v24 - v10 + 0x40000;
              v26 = v10 - v24;
              if( v6 )
              {
                RtlClearBitsEx(p_Right, v26, v25);
              }
              else
              {
                RtlSetBitsEx(p_Right, v26, v25);
                v7 += v25;
              }
              PageRoot = Tree;
              LODWORD(EntireRangeIsZeroed) = v31;
              v10 += v25;
              PageCount -= v25;
              --v11;
              v5 ^= (v5 ^ (v5 + 0x40000)) & 0x7FFFFFFFFFFFFFFFi64;
              goto LABEL_3;
            }
            ExFreePoolWithTag(Pool, 0);
          }
          v6 = 1;
          if( !v7 )
            return;
          PageRoot = Tree;
          PageCount = v7;
          BasePage = v29;
          v8 = v30;
          LODWORD(EntireRangeIsZeroed) = v31;
          v9 = v27;
          goto LABEL_2;
        }
        Root = Root->Children[1];
      }
      if( !Root )
        goto LABEL_11;
    }
  }
}

Referenced by:

MiAddMdlToPartitionTree
MiAllocatePartitionPhysicalPages
MiHotAddPartitionMemory