RtlpHpSegPageRangeCommit

NTSTATUS __fastcall RtlpHpSegPageRangeCommit(UINT16 *a1, UINT64 a2, unsigned int a3, int a4, int a5, __int64 a6){
  unsigned int v6; 
  unsigned int v8; 
  int v9; 
  unsigned int v10; 
  int v12; 
  char v13; 
  unsigned int v14; 
  unsigned int v15; 
  int v16; 
  unsigned __int64 v17; 
  UINT64 v18; 
  int v19; 
  unsigned int v20; 
  UINT64 v21; 
  int v22; 
  int v23; 
  int v24; 
  unsigned int v25; 
  int v26; 
  unsigned int v27; 
  NTSTATUS result; 
  unsigned int v29; 
  int v30; 
  unsigned int v31; 
  int v32; 
  int v33; 
  int v34; 
  unsigned int v35; 
  int v36; 
  int v37; 
  unsigned int v38; 
  unsigned int v39; 
  unsigned int v40; 
  unsigned int v41; 
  INT64 v42; 
  INT64 v43; 
  INT64 v44; 
  int v45; 
  unsigned int v46; 
  int v47; 
  unsigned int v48; 
  UINT64 v49; 
  int v50; 
  int v51; 
  unsigned int v52; 

  v49 = a2;
  v6 = a3;
  v8 = a3 + a4;
  v9 = 511;
  if( (*((_BYTE *)a1 + 13) & 7) == 0 )
    v9 = 0x7FFF;
  v47 = v9;
  v10 = (unsigned int)((__int64)(a2 - (a2 & *(_QWORD *)a1)) >> 5) << *((_BYTE *)a1 + 9);
  if( a4 <= 0 )
    v8 = v6 - a4;
  v12 = 0;
  v45 = 0;
  if( v6 >= v8 )
    goto LABEL_17;
  while( 2 )
  {
    v13 = *((_BYTE *)a1 + 9);
    v14 = v9 - (v9 & (v6 + v10)) + 1;
    if( v14 >= v8 - v6 )
      v14 = v8 - v6;
    v15 = 1 << v13;
    v46 = v14;
    v16 = 0;
    v17 = (unsigned __int64)v6 >> v13 << v13;
    v18 = a2 + 32 * ((unsigned __int64)v6 >> v13);
    v51 = v6 & ((1 << v13) - 1);
    v19 = (v14 - 1 + v51) & ((1 << v13) - 1);
    v20 = v51;
    v21 = v18 + 32 * ((unsigned __int64)(v14 - 1 + v51) >> v13);
    v48 = v19 + 1;
    v22 = -1;
    v50 = -1;
    v23 = -1;
    if( !v51 )
      goto LABEL_9;
    v38 = v15;
    if( v18 == v21 )
      v38 = v48;
    v52 = v38;
    v39 = *(unsigned __int8 *)(v18 + 25);
    if( a4 <= 0 )
    {
      if( v39 > v20 )
      {
        v41 = v20;
        v40 = *(unsigned __int8 *)(v18 + 25);
        goto LABEL_60;
      }
    }
    else if( v39 < v52 )
    {
      v40 = v52;
      v20 = *(unsigned __int8 *)(v18 + 25);
      v41 = v52;
LABEL_60:
      v22 = v17 + v40;
      v16 = v41 - v39;
      v50 = v22;
      v23 = v17 + v20;
      if( v16 )
      {
        v50 = v22;
        if( a4 <= 0 )
        {
          v50 = v22;
          *(_BYTE *)(v18 + 25) = v16 + v39;
        }
      }
    }
    v18 += 32i64;
    LODWORD(v17) = v15 + v17;
LABEL_9:
    if( v18 >= v21 )
      goto LABEL_10;
    do
    {
      v31 = *(unsigned __int8 *)(v18 + 25);
      v32 = 0;
      v33 = v23;
      if( a4 <= 0 )
      {
        if( !*(_BYTE *)(v18 + 25) )
          goto LABEL_30;
        v34 = v17;
        v32 = -v31;
        if( v23 != -1 )
          v34 = v23;
        v23 = v34;
        v35 = *(unsigned __int8 *)(v18 + 25);
      }
      else
      {
        if( v31 >= v15 )
          goto LABEL_30;
        v23 = v31 + v17;
        v32 = v15 - v31;
        if( v33 != -1 )
          v23 = v33;
        v35 = v15;
      }
      v36 = v17 + v35;
      v50 = v36;
      if( v32 )
      {
        v50 = v36;
        if( a4 <= 0 )
        {
          v50 = v36;
          *(_BYTE *)(v18 + 25) = v32 + v31;
        }
      }
LABEL_30:
      v16 += v32;
      v18 += 32i64;
      LODWORD(v17) = v15 + v17;
    }
    while( v18 < v21 );
    v22 = v50;
LABEL_10:
    v24 = v23;
    if( v18 == v21 )
    {
      v25 = *(unsigned __int8 *)(v18 + 25);
      v26 = 0;
      if( a4 <= 0 )
      {
        if( *(_BYTE *)(v18 + 25) )
        {
          v37 = v17;
          v26 = -v25;
          v27 = *(unsigned __int8 *)(v18 + 25);
          if( v23 != -1 )
            v37 = v23;
          v23 = v37;
          goto LABEL_44;
        }
      }
      else
      {
        v27 = v48;
        if( v25 >= v48 )
          goto LABEL_13;
        v23 = v17 + v25;
        v26 = v48 - v25;
        if( v24 != -1 )
          v23 = v24;
LABEL_44:
        v22 = v17 + v27;
        if( v26 && a4 <= 0 )
          *(_BYTE *)(v18 + 25) = v26 + v25;
      }
LABEL_13:
      v16 += v26;
    }
    if( v16 )
    {
      v29 = v22 - v23;
      if( v16 <= 0 )
      {
        v30 = 0x4000;
      }
      else
      {
        v30 = 4096;
        if( (a5 & 2) != 0 )
          v30 = 1073745920;
      }
      LODWORD(v44) = ((unsigned int)a5 >> 22) & 2;
      LODWORD(v43) = v30;
      LODWORD(v42) = v16;
      result = RtlpHpSegMgrCommit((INT64)a1, v49 & *(_QWORD *)a1, v23 + v10, v29, v42, v43, v44);
      if( result >= 0 )
      {
        if( v16 > 0 )
          RtlpHpSegPageRangeHandleCommit(a1, v49);
        _InterlockedExchangeAdd64((volatile signed __int64 *)((char *)a1 + (__int16)a1[11] + 8), v16);
        a2 = v49;
        *(_WORD *)(v49 + 28) = ~(v16 + ~*(_WORD *)(v49 + 28));
        goto LABEL_16;
      }
    }
    else
    {
      a2 = v49;
LABEL_16:
      v6 += v46;
      v12 = v16 + v45;
      v45 += v16;
      if( v6 < v8 )
      {
        v9 = v47;
        continue;
      }
LABEL_17:
      if( a6 )
        *(_DWORD *)a6 = v12;
      return 0;
    }
    return result;
  }
}

Referenced by:

RtlpHpMetadataCommit
RtlpHpSegAlloc
RtlpHpSegLfhVsCommit
RtlpHpSegLfhVsDecommit
RtlpHpSegPageRangeCoalesce