MiAllocateEntireImageFileExtents

INT64 __fastcall MiAllocateEntireImageFileExtents(
        _FILE_OBJECT *FileObject,
        PHYSICAL_EXTENTS_DESCRIPTOR *a2,
        _DWORD *a3,
        UINT64 **a4){
  __int64 v8; 
  ULONG i; 
  struct _PHYSICAL_EXTENTS_DESCRIPTOR *v10; 
  INT64 v11; 
  struct _PHYSICAL_EXTENTS_DESCRIPTOR *v12; 
  int v14; 
  unsigned int v15; 
  ULONG NumberOfValidRuns; 
  unsigned int v17; 
  ULONG *v18; 
  UINT64 *v19; 
  __int64 j; 
  unsigned int v21; 
  UINT64 *v22; 
  bool v23; 
  unsigned __int64 BasePage; 
  char v25; 
  _QWORD *BugCheckParameter4; 
  unsigned __int64 v27; 
  _QWORD *v28; 
  *a4 = 0i64;
  LODWORD(v8) = 1;
  for( i = ((__rdtsc() >> 4) & 7) + 8; ; i = NumberOfValidRuns )
  {
    while( 1 )
    {
      if( !i )
        return 3221225626i64;
      LODWORD(v10) = MiAllocatePool((struct _SLIST_ENTRY *)0x40);
      v12 = v10;
      if( v10 )
        break;
      if( !(_DWORD)v8 )
        return 3221225626i64;
      i >>= 1;
    }
    v10->NumberOfValidRuns = 0;
    a2 = (PHYSICAL_EXTENTS_DESCRIPTOR *)(((unsigned __int64)&a2[170].Run[0].BasePage + 7) & 0xFFFFFFFFFFFFF000ui64);
    v10->NumberOfRuns = i;
    v8 = 0i64;
    FsRtlGetFileExtents((INT64)FileObject, v11);
    v15 = v14;
    if( v14 < 0 )
    {
      ExFreePoolWithTag(v12, 0);
      return v15;
    }
    NumberOfValidRuns = v12->NumberOfValidRuns;
    if( NumberOfValidRuns <= i )
      break;
    v12->NumberOfValidRuns = i;
    ExFreePoolWithTag(v12, 0);
  }
  if( !(unsigned int)MiNewPfnsSuitable(v12) )
  {
    v17 = -1073740761;
LABEL_16:
    ExFreePoolWithTag(v12, 0);
    return v17;
  }
  if( !NumberOfValidRuns )
  {
    v17 = -1073741823;
    goto LABEL_16;
  }
  LODWORD(v18) = MiAllocatePool((struct _SLIST_ENTRY *)0x40);
  v19 = (UINT64 *)v18;
  if( !v18 )
  {
    v17 = -1073741670;
    goto LABEL_16;
  }
  *v18 = v12->NumberOfValidRuns;
  MiLockImageExtentsExclusive();
  if( a3 )
  {
    for( j = 0i64; (unsigned int)j < *a3; j = (unsigned int)(j + 1) )
      RtlAvlRemoveNode(&qword_140C4C9E0, (__int64 *)&a3[12 * j + 2]);
  }
  v21 = 0;
  v22 = v19 + 1;
  if( !*(_DWORD *)v19 )
    goto LABEL_31;
  do
  {
    v23 = 0;
    BasePage = v12->Run[v21].BasePage;
    v22[4] = BasePage;
    v22[5] = v12->Run[v21].PageCount;
    v25 = 0;
    *((_DWORD *)v22 + 6) = v8;
    BugCheckParameter4 = (_QWORD *)qword_140C4C9E0;
    if( !qword_140C4C9E0 )
      goto LABEL_29;
    while( 1 )
    {
      v27 = BugCheckParameter4[4];
      if( BasePage + v22[5] <= v27 )
      {
        v28 = (_QWORD *)*BugCheckParameter4;
        if( !*BugCheckParameter4 )
          goto LABEL_30;
        goto LABEL_27;
      }
      if( BasePage < BugCheckParameter4[5] + v27 )
        break;
      v28 = (_QWORD *)BugCheckParameter4[1];
      if( !v28 )
      {
        v23 = 1;
        goto LABEL_30;
      }
LABEL_27:
      BugCheckParameter4 = v28;
    }
    v25 = 1;
LABEL_29:
    if( v25 )
      KeBugCheckEx(0x1Au, 0x13003ui64, v22[4], v22[5], (ULONG_PTR)BugCheckParameter4);
LABEL_30:
    RtlAvlInsertNodeEx(&qword_140C4C9E0, (__int64)BugCheckParameter4, v23, v22);
    v8 += v22[5];
    ++v21;
    v22 += 6;
  }
  while( v21 < *(_DWORD *)v19 );
LABEL_31:
  MiUnlockImageExtentsExclusive();
  *a4 = v19;
  ExFreePoolWithTag(v12, 0);
  if( a3 )
    ExFreePoolWithTag(a3, 0);
  return 0i64;
}

Referenced by:

MiCreateImageFileMap