MiFindFreePageFileSpace

__int64 __fastcall MiFindFreePageFileSpace(
        _MI_PARTITION *Partition,
        _MMPTE *PageFileInfo,
        unsigned __int64 NumberOfPages,
        unsigned int Flags,
        unsigned int a5){
  char v5; 
  UINT64 v8; 
  __int64 v9; 
  unsigned int v10; 
  unsigned int v11; 
  unsigned int v12; 
  __int64 *v13; 
  __int64 i; 
  __int16 v15; 
  __int64 v16; 
  int v17; 
  int v18; 
  UINT64 v19; 
  KIRQL v20; 
  KIRQL v21; 
  unsigned __int64 v22; 
  bool v23; 
  UINT64 v24; 
  UINT64 v25; 
  UINT64 v26; 
  UINT64 v27; 
  unsigned int v28; 
  char v29; 
  int v30; 
  volatile LONG *v31; 
  UINT64 v33; 
  UINT64 v34; 
  __int64 v35; 
  int v36; 
  unsigned int v37; 
  unsigned int v38; 
  volatile LONG *v39; 
  unsigned int FreePageFileSpaceForward; 
  unsigned __int64 v41; 
  unsigned int v42; 
  unsigned __int64 v43; 
  UINT64 v44; 
  __int64 j; 
  unsigned int v46; 
  int v47; 
  int v48; 
  __int128 v49; 
  __int128 v50; 
  __int128 v51; 
  __int64 v52; 
  char v55; 
  v55 = Flags;
  v52 = 0i64;
  v5 = Flags;
  v49 = 0i64;
  v50 = 0i64;
  v51 = 0i64;
  if( NumberOfPages < 0xFFFFFFFF )
  {
    LODWORD(v8) = -1;
    if( (Flags & 0x10) != 0 )
      goto LABEL_3;
    v9 = *((_QWORD *)Partition + ((unsigned __int8)HIBYTE(*(_WORD *)PageFileInfo) >> 4) + 868);
    if( (Flags & 0x65) == 1
      && (unsigned int)MiPageFileLargestBitmapsRun(*((_QWORD *)Partition
                                                   + ((unsigned __int8)HIBYTE(*(_WORD *)PageFileInfo) >> 4)
                                                   + 868)) < NumberOfPages )
    {
      v5 |= 0x10u;
      v55 = v5;
    }
    else
    {
      v34 = *(_QWORD *)PageFileInfo;
      if( qword_140C4DC80 && (v34 & 0x10) == 0 )
        v34 &= ~qword_140C4DC80;
      v8 = HIDWORD(v34);
    }
    if( (v5 & 0x10) != 0 )
    {
LABEL_3:
      v9 = *((_QWORD *)Partition + 868);
      v10 = MiPageFileLargestBitmapsRun(v9);
      v11 = *((_DWORD *)Partition + 1734);
      v12 = v10;
      if( v11 > 1 )
      {
        v13 = (__int64 *)((char *)Partition + 6952);
        for( i = v11 - 1; i; --i )
        {
          v15 = *(_WORD *)(v9 + 204);
          v16 = *v13;
          if( (v15 & 0x10) != 0 && (*(_BYTE *)(v16 + 204) & 0x10) == 0
            || (v15 & 0x20) != 0 && (*(_BYTE *)(v16 + 204) & 0x20) == 0 )
          {
            v9 = *v13;
            v42 = MiPageFileLargestBitmapsRun(*v13);
          }
          else
          {
            if( (*(_WORD *)(v16 + 204) & 0x30) != 0 )
              goto LABEL_8;
            v42 = MiPageFileLargestBitmapsRun(*v13);
            if( v42 <= v12 )
              goto LABEL_8;
            v9 = v16;
          }
          v12 = v42;
LABEL_8:
          ++v13;
        }
      }
      v5 = v55;
    }
    v17 = 0;
    v46 = 0;
    v18 = v5 & 4;
    v48 = v18;
    while( 1 )
    {
      v19 = 0i64;
      v47 = 0;
      if( v18 )
      {
        v21 = 0;
      }
      else
      {
        if( (v5 & 1) != 0 || v17 )
        {
          v20 = ExAcquireSpinLockExclusive((PEX_SPIN_LOCK)(v9 + 232));
        }
        else
        {
          v47 = 1;
          v20 = ExAcquireSpinLockShared((PEX_SPIN_LOCK)(v9 + 232));
        }
        v21 = v20;
      }
      if( (*(_BYTE *)(v9 + 207) & 1) != 0 )
      {
LABEL_87:
        if( !v48 )
        {
          v39 = (volatile LONG *)(v9 + 232);
          if( v47 )
            ExReleaseSpinLockSharedFromDpcLevel(v39);
          else
            ExReleaseSpinLockExclusiveFromDpcLevel((INT64 *)v39);
          __writecr8(v21);
        }
        return 0i64;
      }
      if( (_DWORD)v8 == -1 )
        goto LABEL_17;
      v35 = *(_QWORD *)(v9 + 112);
      if( (unsigned int)v8 >= *(_DWORD *)(v35 + 24) )
      {
        if( (v5 & 0x48) == 0 )
        {
LABEL_17:
          v22 = NumberOfPages;
          goto LABEL_18;
        }
        goto LABEL_87;
      }
      if( _bittest64(*(const signed __int64 **)(v35 + 32), (unsigned int)v8)
        || _bittest64(*(const signed __int64 **)(v35 + 16), (unsigned int)v8) )
      {
        v22 = NumberOfPages;
        v28 = 0;
        v36 = v5 & 0x40;
      }
      else
      {
        v37 = RtlLengthCurrentClearRunForward((RTL_BITMAP *)(v35 + 24), (unsigned int)v8, (unsigned int)NumberOfPages);
        v22 = NumberOfPages;
        v36 = v5 & 0x40;
        if( v37 != (_DWORD)NumberOfPages && !v36
          || (v38 = RtlLengthCurrentClearRunForward((RTL_BITMAP *)(*(_QWORD *)(v9 + 112) + 8i64), (unsigned int)v8, v37),
              v22 = NumberOfPages,
              v28 = v38,
              v38 != (_DWORD)NumberOfPages)
          && !v36 )
        {
          v28 = 0;
        }
      }
      if( v28 == v22 || v36 )
      {
        v46 = v8;
        goto LABEL_42;
      }
      v5 = v55;
LABEL_18:
      while( 2 )
      {
        if( (v5 & 8) != 0 )
        {
          v46 = v8;
          FreePageFileSpaceForward = MiFindFreePageFileSpaceForward(v9, &v46, (unsigned int)v22, v5);
          LODWORD(v8) = v46;
          v28 = FreePageFileSpaceForward;
          goto LABEL_42;
        }
        LODWORD(v52) = 0;
        v23 = (*(_BYTE *)(v9 + 152) & 1) == 0;
        v24 = *(_QWORD *)(v9 + 144);
        HIDWORD(v52) = v22;
        if( !v23 && v24 )
          v24 ^= v9 + 144;
        v19 = 0i64;
        while( 1 )
        {
          if( !v24 )
            goto LABEL_40;
          if( (unsigned int)v22 < *(_DWORD *)(v24 + 52) )
            goto LABEL_54;
          if( (unsigned int)v22 <= *(_DWORD *)(v24 + 52) )
            break;
          v33 = *(_QWORD *)(v24 + 8);
LABEL_55:
          if( (*(_BYTE *)(v9 + 152) & 1) != 0 && v33 )
            v24 ^= v33;
          else
            v24 = v33;
        }
        if( *(_DWORD *)(v24 + 48) )
        {
LABEL_54:
          v33 = *(_QWORD *)v24;
          v19 = v24;
          goto LABEL_55;
        }
        v25 = *(_QWORD *)v24;
        v19 = v24;
        if( (*(_BYTE *)(v9 + 152) & 1) != 0 && v25 )
          v25 ^= v24;
        v26 = 0i64;
        if( v25 )
        {
          while( 1 )
          {
            if( (unsigned int)v22 < *(_DWORD *)(v25 + 52) )
              goto LABEL_34;
            if( (unsigned int)v22 <= *(_DWORD *)(v25 + 52) )
              break;
            v27 = *(_QWORD *)(v25 + 8);
LABEL_35:
            if( (*(_BYTE *)(v9 + 152) & 1) != 0 && v27 )
              v25 ^= v27;
            else
              v25 = v27;
            if( !v25 )
            {
              if( v26 )
                v19 = v26;
              goto LABEL_40;
            }
          }
          if( !*(_DWORD *)(v25 + 48) )
            v26 = v25;
LABEL_34:
          v27 = *(_QWORD *)v25;
          goto LABEL_35;
        }
LABEL_40:
        if( *(_DWORD *)(v19 + 52) != -1 )
          goto LABEL_41;
        v41 = *(unsigned int *)(v9 + 140);
        if( v41 < v22 || (unsigned int)v41 < 0x20 )
        {
          if( v5 < 0 )
            goto LABEL_87;
          v43 = *(_QWORD *)v19;
          v44 = v19;
          if( *(_QWORD *)v19 )
          {
            for( ; *(_QWORD *)(v43 + 8); v43 = *(_QWORD *)(v43 + 8) )
              ;
          }
          else
          {
            for( j = *(_QWORD *)(v19 + 16); ; j = *(_QWORD *)(v43 + 16) )
            {
              v43 = j & 0xFFFFFFFFFFFFFFFCui64;
              if( !v43 || *(_QWORD *)(v43 + 8) == v44 )
                break;
              v44 = v43;
            }
          }
          if( !v43 )
          {
            if( (unsigned int)v41 < 0x20 )
              goto LABEL_87;
            goto LABEL_99;
          }
          v19 = v43;
          if( (unsigned int)v41 > *(_DWORD *)(v43 + 52) && (unsigned int)v41 >= 0x20 )
            goto LABEL_99;
          LODWORD(v22) = *(_DWORD *)(v43 + 52);
LABEL_41:
          LODWORD(v8) = *(_DWORD *)(v19 + 48);
          v28 = v22;
          v46 = v8;
LABEL_42:
          if( v28 )
          {
            v29 = v55;
            v30 = 0;
            if( (v55 & 1) != 0 )
            {
              v30 = 2;
              RtlSetBits((RTL_BITMAP *)(*(_QWORD *)(v9 + 112) + 24i64), (unsigned int)v8, v28);
              *(_QWORD *)(v9 + 48) -= v28;
              v29 = v55;
            }
            if( (v29 & 2) != 0 )
            {
              v30 |= 1u;
              MiSetPageFileAllocationBits(v9, (unsigned int)v8, v28);
            }
            if( v30 )
              MiInvalidatePageFileBitmapsCache(v9, (unsigned int)v8, v28, v19);
            if( !v48 )
            {
              v31 = (volatile LONG *)(v9 + 232);
              if( v47 )
                ExReleaseSpinLockSharedFromDpcLevel(v31);
              else
                ExReleaseSpinLockExclusiveFromDpcLevel((INT64 *)v31);
              __writecr8(v21);
            }
            *(_QWORD *)PageFileInfo = MiTransferSoftwarePte(*(_QWORD *)PageFileInfo, v9, (unsigned int)v8);
            return v28;
          }
          goto LABEL_87;
        }
LABEL_99:
        if( !v47 )
        {
          MiRescanPagefileBitmaps(v9);
          v22 = NumberOfPages;
          continue;
        }
        break;
      }
      ExReleaseSpinLockSharedFromDpcLevel((PEX_SPIN_LOCK)(v9 + 232));
      __writecr8(v21);
      v18 = v48;
      v17 = 1;
    }
  }
  return 0i64;
}

Referenced by:

MiFindPageFileWriteCluster
MiGetKernelStackSwapSupport
MiReservePageFileSpace
MiReserveWorkingSetSwapSpaceRuns
MiTrimUnusedPageFileRegionsApc