MiGetHotPatchEntry

NTSTATUS __fastcall MiGetHotPatchEntry(_RTL_RB_TREE *Tree, UINT64 a2, INT64 a3, INT64 *a4){
  int v4; 
  _EX_PUSH_LOCK *v7; 
  UINT64 Root; 
  int v9; 
  int v10; 
  NTSTATUS v11; 
  UINT64 v12; 
  _QWORD *Pool; 
  UINT64 v14; 
  BOOL v15; 
  int v16; 
  UINT64 v17; 
  _ETHREAD *CurrentThread; 

  v4 = a3;
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  --CurrentThread->Tcb.SpecialApcDisable;
  v7 = (_EX_PUSH_LOCK *)&Tree[1];
  ExAcquirePushLockExclusiveEx((_EX_PUSH_LOCK *)&Tree[1], 0i64);
  Root = (UINT64)Tree->Root;
  v9 = 0;
  if( (Tree->_bf_0 & 1) != 0 )
  {
    if( Root )
      Root ^= (unsigned __int64)Tree;
    else
      Root = 0i64;
  }
  v10 = Tree->_bf_0 & 1;
  if( !Root )
    goto LABEL_20;
  do
  {
    v11 = MiHotPatchImageTreeCompare(a2, Root);
    if( v11 >= 0 )
    {
      if( v11 <= 0 )
        break;
      v12 = *(_QWORD *)(Root + 8);
    }
    else
    {
      v12 = *(_QWORD *)Root;
    }
    if( v10 && v12 )
      Root ^= v12;
    else
      Root = v12;
  }
  while( Root );
  if( Root )
  {
    if( (*(_DWORD *)(Root + 92) & 1) != 0 )
    {
      LODWORD(Root) = 0;
      v9 = 259;
      goto LABEL_43;
    }
    if( *(_DWORD *)(Root + 88) == v4 )
    {
      LODWORD(Root) = 0;
      v9 = 255;
      goto LABEL_43;
    }
  }
  else
  {
LABEL_20:
    if( !v4 )
    {
      LODWORD(Root) = 0;
      v9 = 1075380276;
      goto LABEL_43;
    }
    Pool = MiAllocatePool(256i64, 0x60ui64, 0x4E455048ui64);
    Root = (UINT64)Pool;
    if( !Pool )
    {
      v9 = -1073741670;
      goto LABEL_43;
    }
    Pool[3] = a2;
    *((_DWORD *)Pool + 22) = 0;
    v14 = (UINT64)Tree->Root;
    if( (Tree->_bf_0 & 1) != 0 )
    {
      if( v14 )
        v14 ^= (unsigned __int64)Tree;
      else
        v14 = 0i64;
    }
    v15 = 0;
    v16 = Tree->_bf_0 & 1;
    if( v14 )
    {
      while( 1 )
      {
        if( MiHotPatchImageTreeCompare(a2, v14) < 0 )
        {
          v17 = *(_QWORD *)v14;
          if( v16 )
          {
            if( !v17 )
              goto LABEL_40;
            v17 ^= v14;
          }
          if( !v17 )
          {
LABEL_40:
            v15 = 0;
            break;
          }
        }
        else
        {
          v17 = *(_QWORD *)(v14 + 8);
          if( v16 )
          {
            if( !v17 )
              goto LABEL_34;
            v17 ^= v14;
          }
          if( !v17 )
          {
LABEL_34:
            v15 = 1;
            break;
          }
        }
        v14 = v17;
      }
    }
    RtlRbInsertNodeEx((UINT64 *)Tree, v14, v15, Root);
  }
  *(_DWORD *)(Root + 92) |= 1u;
LABEL_43:
  if( (_InterlockedExchangeAdd64(&v7->_bf_0, 0xFFFFFFFFFFFFFFFFui64) & 6) == 2 )
    ExfTryToWakePushLock(v7);
  KeAbPostRelease(v7);
  KiLeaveGuardedRegionUnsafe((__int64)CurrentThread);
  *(_DWORD *)a4 = v9;
  return Root;
}

Referenced by:

MiHotPatchImage