MmFlushVirtualMemory

__int64 __fastcall MmFlushVirtualMemory(ULONG_PTR a1, unsigned __int64 *a2, _QWORD *a3, _DWORD *a4){
  unsigned __int64 v4; 
  unsigned int v5; 
  __int64 v7; 
  unsigned __int64 v8; 
  unsigned __int64 v9; 
  unsigned int v10; 
  INT64 v11; 
  _MMVAD *v12; 
  unsigned __int64 v13; 
  int v14; 
  UINT64 v15; 
  INT64 v16; 
  int v17; 
  _MMPTE *ProtoPteAddress; 
  _SUBSECTION *LastSubsection; 
  __int64 *v20; 
  _FILE_OBJECT *v21; 
  __int64 v22; 
  int v23; 
  _DWORD *v25; 
  BOOLEAN v26; 
  unsigned int v27; 
  _SUBSECTION *ContainingSubsection; 
  INT64 v29; 
  _MMPTE *CurrentThread; 
  _SUBSECTION *Subsection; 
  _DWORD *v32; 
  _QWORD *v33; 
  _SUBSECTION *v34; 
  _KAPC_STATE ApcState; 
  v4 = *a2;
  v5 = 0;
  v32 = a4;
  ApcState.ApcListHead[0] = 0i64;
  v33 = a3;
  v7 = *a3 + v4 - 1;
  v8 = v4 & 0xFFFFFFFFFFFFF000ui64;
  Subsection = 0i64;
  *a2 = v8;
  v9 = v7 | 0xFFF;
  memset(&ApcState.ApcListHead[1], 0, 32);
  ContainingSubsection = 0i64;
  v10 = 0;
  LODWORD(v29) = 0;
  CurrentThread = (_MMPTE *)KeGetCurrentThread();
  if( *((_QWORD *)CurrentThread + 23) != a1 )
  {
    v10 = 2;
    KiStackAttachProcess((_KPROCESS *)a1, 0i64, &ApcState);
  }
  v27 = 0;
  v26 = KeAreAllApcsDisabled();
  v11 = MiObtainReferencedVadEx(v8, 2, &v29);
  v12 = (_MMVAD *)v11;
  if( !v11 )
  {
    v5 = v29;
    if( (_DWORD)v29 == -1073741664 )
      v5 = -1073741799;
    goto LABEL_25;
  }
  v13 = *(unsigned int *)(v11 + 28) | ((unsigned __int64)*(unsigned __int8 *)(v11 + 33) << 32);
  if( !*a3 )
  {
    v9 = (v13 << 12) | 0xFFF;
    v10 |= 1u;
  }
  v14 = *(_DWORD *)(v11 + 48);
  if( (v14 & 0x100000) != 0 || (v15 = v9 >> 12, v9 >> 12 > v13) )
  {
LABEL_31:
    v5 = -1073741799;
    goto LABEL_24;
  }
  v16 = **(_QWORD **)(v11 + 72);
  v29 = v16;
  if( *(_QWORD *)(v16 + 64) && (v14 & 0x70) != 32 )
  {
    *v33 = v9 - v8 + 1;
    if( !v26 )
    {
      v17 = 0;
      if( (*(_DWORD *)(v16 + 56) & 0x20000) != 0 )
        v17 = 4;
      v27 = v17;
    }
    MiFlushDirtyBitsToPfn(v8, v9, (__int64)CurrentThread);
    MiGetProtoPteAddress(v12, v9 >> 12, 0i64, &ContainingSubsection);
    ProtoPteAddress = MiGetProtoPteAddress(v12, v8 >> 12, 0i64, &Subsection);
    LastSubsection = ContainingSubsection;
    v20 = (__int64 *)Subsection;
    CurrentThread = ProtoPteAddress;
    if( ContainingSubsection )
    {
      ContainingSubsection = (_SUBSECTION *)MiGetProtoPteAddress(v12, v15, 0i64, &v34);
LABEL_16:
      if( (unsigned int)MiFlushAcquire((_CONTROL_AREA *)v16, (_SUBSECTION *)v20, LastSubsection) )
      {
        MiUnlockAndDereferenceVadShared(v12);
        if( v10 >= 2 )
          KiUnstackDetachProcess(&ApcState, 0i64);
        v21 = MiReferenceControlAreaFile((_CONTROL_AREA *)v16);
        v22 = (__int64)v32;
        while( 1 )
        {
          v23 = FsRtlAcquireFileForCcFlushEx(v21);
          if( v23 < 0 )
            break;
          v23 = MiFlushSectionInternal(
                  (ULONG_PTR)CurrentThread,
                  (__int64)ContainingSubsection,
                  v20,
                  (__int64)LastSubsection,
                  0i64,
                  v27,
                  v22);
          FsRtlReleaseFileForCcFlush(v21);
          if( v23 != -1073741740 )
            break;
          KeDelayExecutionThread(0, 0, (PLARGE_INTEGER)&MiShortTime);
        }
        MiDereferenceControlAreaFile(v29, (unsigned __int64)v21);
        MiFlushRelease((_CONTROL_AREA *)v29, (_SUBSECTION *)v20, LastSubsection);
        return(unsigned int)v23;
      }
      v5 = -1073741670;
      goto LABEL_24;
    }
    if( Subsection )
    {
      if( (v10 & 1) != 0 )
      {
        LastSubsection = MiFindLastSubsection((_CONTROL_AREA *)v16, 0i64);
        ContainingSubsection = (_SUBSECTION *)(*((_QWORD *)LastSubsection + 1)
                                             + 8
                                             * (*((unsigned int *)LastSubsection + 11)
                                              - (unsigned __int64)(*((_DWORD *)LastSubsection + 13) & 0x3FFFFFFF)
                                              - 1));
        goto LABEL_16;
      }
    }
    else if( (v10 & 1) != 0 )
    {
      v25 = v32;
      *v32 = 0;
      *((_QWORD *)v25 + 1) = 0i64;
      goto LABEL_24;
    }
    goto LABEL_31;
  }
  v5 = -1073741688;
LABEL_24:
  MiUnlockAndDereferenceVadShared(v12);
LABEL_25:
  if( v10 >= 2 )
    KiUnstackDetachProcess(&ApcState, 0i64);
  return v5;
}

Referenced by:

CMFFlushHitsFile
NtFlushVirtualMemory