CmpCreateHardwareProfiles

INT64 __fastcall CmpCreateHardwareProfiles(INT64 a1){
  char v2; 
  int v3; 
  INT64 v4; 
  void *v5; 
  int v6; 
  NTSTATUS v7; 
  INT64 v8; 
  INT64 v9; 
  INT64 v10; 
  __int64 v11; 
  UINT16 v12; 
  _WORD *v13; 
  __int64 v14; 
  __int64 v15; 
  INT64 Length; 
  UINT64 Lengtha; 
  INT64 Lengthb; 
  INT64 Lengthc; 
  INT64 Lengthd; 
  INT64 Lengthe; 
  UINT64 *ResultLength; 
  UINT64 *ResultLengtha; 
  UINT64 *ResultLengthb; 
  UINT64 *ResultLengthc; 
  UINT64 *ResultLengthd; 
  UINT64 *ResultLengthe; 
  UINT64 *ResultLengthf; 
  UINT64 *ResultLengthg; 
  UINT64 *ResultLengthh; 
  UINT64 *ResultLengthi; 
  UINT64 *ResultLengthj; 
  UINT64 *ResultLengthk; 
  UINT64 *ResultLengthl; 
  UINT64 *ResultLengthm; 
  UINT64 *Disposition; 
  UINT64 *Dispositiona; 
  UINT64 *Dispositionb; 
  UINT64 *Dispositionc; 
  INT64 v41; 
  INT64 v42; 
  INT64 v43; 
  INT64 v44; 
  UINT64 NewProfileNumber; 
  __int64 v46; 
  VOID *DestinationString; 
  _UNICODE_STRING DestinationString_8; 
  VOID *KeyHandle; 
  _OBJECT_ATTRIBUTES KeyHandle_8; 
  PVOID NewProfile; 
  UINT64 v52; 
  PVOID Parent; 
  UINT64 v54; 
  VOID *Handle; 
  _UNICODE_STRING String1; 
  UINT64 v57; 
  int KeyValueInformation[32]; 
  char v59[256]; 
  int v60[64]; 

  v52 = 0i64;
  LODWORD(v54) = 0;
  NewProfileNumber = 0i64;
  memset(&KeyHandle_8, 0, sizeof(KeyHandle_8));
  v57 = 0i64;
  KeyHandle = 0i64;
  NewProfile = 0i64;
  String1 = 0i64;
  Parent = 0i64;
  DestinationString_8 = 0i64;
  DestinationString = 0i64;
  v2 = 0;
  v3 = CmpOpenDevicesControlSet(a1, (VOID **)&v57, &String1);
  v5 = (void *)v57;
  v6 = v3;
  if( v3 < 0 )
    goto LABEL_25;
  KeyHandle_8.RootDirectory = (void *)v57;
  KeyHandle_8.Attributes = 576;
  KeyHandle_8.ObjectName = (_UNICODE_STRING *)L"$&";
  KeyHandle_8.Length = 48;
  *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
  v7 = ZwOpenKey(&KeyHandle, 0x20019ui64, &KeyHandle_8, v4, Length, (INT64)ResultLength, (INT64)Disposition, v41);
  v6 = v7;
  if( v7 == -1073741772 )
  {
    if( !*(&stru_140CF2E80 + 1656) )
      goto LABEL_25;
    KeyHandle_8.Length = 48;
    KeyHandle_8.ObjectName = (_UNICODE_STRING *)&CmpControlString;
    KeyHandle_8.RootDirectory = v5;
    LODWORD(ResultLengtha) = 0;
    KeyHandle_8.Attributes = 576;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    v6 = ZwCreateKey((VOID **)&v52, 0x20019ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengtha, &NewProfileNumber);
    if( v6 < 0 )
      goto LABEL_25;
    ZwClose(v52);
    v52 = 0i64;
    KeyHandle_8.ObjectName = (_UNICODE_STRING *)L"$&";
    KeyHandle_8.Length = 48;
    LODWORD(ResultLengthh) = 0;
    KeyHandle_8.RootDirectory = v5;
    KeyHandle_8.Attributes = 576;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    v6 = ZwCreateKey(&KeyHandle, 0x20019ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthh, &NewProfileNumber);
    if( v6 < 0 )
      goto LABEL_25;
    KeyHandle_8.RootDirectory = KeyHandle;
    KeyHandle_8.Length = 48;
    KeyHandle_8.ObjectName = (_UNICODE_STRING *)L"\"$";
    KeyHandle_8.Attributes = 576;
    LODWORD(ResultLengthi) = 0;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    v6 = ZwCreateKey((VOID **)&v52, 0x20019ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthi, &NewProfileNumber);
    if( v6 < 0 )
      goto LABEL_25;
    DestinationString_8.MaximumLength = 256;
    DestinationString_8.Buffer = (wchar_t *)v59;
    RtlUnicodeStringPrintf(&DestinationString_8, (WCHAR *)L"%04d");
    KeyHandle_8.RootDirectory = (void *)v52;
    KeyHandle_8.Length = 48;
    KeyHandle_8.ObjectName = &DestinationString_8;
    KeyHandle_8.Attributes = 576;
    LODWORD(ResultLengthj) = 0;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    v6 = ZwCreateKey(&NewProfile, 0x20019ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthj, &NewProfileNumber);
    ZwClose(v52);
    v52 = 0i64;
    if( v6 < 0 )
      goto LABEL_25;
    ZwClose((_HANDLE)NewProfile);
    NewProfile = 0i64;
    LODWORD(ResultLengthk) = 4;
    v6 = ZwSetValueKey(
           KeyHandle,
           (_UNICODE_STRING *)&CmpCurrentConfigString,
           0i64,
           4ui64,
           (char *)&NewProfileNumber + 4,
           (UINT64)ResultLengthk);
    if( v6 < 0 )
      goto LABEL_25;
  }
  else
  {
    if( v7 < 0 )
      goto LABEL_25;
    LODWORD(Lengtha) = 128;
    v6 = ZwQueryValueKey(
           KeyHandle,
           (_UNICODE_STRING *)&CmpCurrentConfigString,
           KeyValueFullInformation,
           KeyValueInformation,
           Lengtha,
           &v54);
    if( v6 < 0 || KeyValueInformation[1] != 4 )
      goto LABEL_25;
    HIDWORD(NewProfileNumber) = *(int *)((char *)KeyValueInformation + (unsigned int)KeyValueInformation[2]);
  }
  KeyHandle_8.Length = 48;
  KeyHandle_8.ObjectName = (_UNICODE_STRING *)L"\"$";
  KeyHandle_8.RootDirectory = v5;
  KeyHandle_8.Attributes = 576;
  *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
  v6 = ZwOpenKey(&Parent, 0x20019ui64, &KeyHandle_8, v8, Lengthb, (INT64)ResultLengthb, (INT64)Dispositiona, v42);
  if( v6 == -1073741772 )
  {
    if( !*(&stru_140CF2E80 + 1656) )
      goto LABEL_25;
    LODWORD(ResultLengthc) = 0;
    v6 = ZwCreateKey(&Parent, 0x20019ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthc, &NewProfileNumber);
  }
  if( v6 < 0 )
    goto LABEL_25;
  *(_QWORD *)&DestinationString_8.Length = 0x1000000i64;
  DestinationString_8.Buffer = (wchar_t *)v59;
  RtlUnicodeStringPrintf(&DestinationString_8, (WCHAR *)L"%04d");
  KeyHandle_8.RootDirectory = Parent;
  KeyHandle_8.Length = 48;
  KeyHandle_8.ObjectName = &DestinationString_8;
  KeyHandle_8.Attributes = 576;
  *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
  v6 = ZwOpenKey(&NewProfile, 0x2001Fui64, &KeyHandle_8, v9, Lengthc, (INT64)ResultLengthc, (INT64)Dispositionb, v43);
  if( v6 == -1073741772 )
  {
    if( !*(&stru_140CF2E80 + 1656) )
      goto LABEL_25;
    LODWORD(ResultLengthd) = 0;
    v6 = ZwCreateKey(&NewProfile, 0x2001Fui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthd, &NewProfileNumber);
  }
  if( v6 < 0 )
    goto LABEL_25;
  v11 = *(_QWORD *)(a1 + 240);
  v12 = *(_WORD *)(v11 + 8);
  v13 = (_WORD *)(v11 + 4);
  if( v12 == 3 )
  {
    v12 = 1;
    *(_WORD *)(v11 + 8) = 1;
  }
  if( *v13 == 1 )
    goto LABEL_51;
  if( *v13 == 2 )
  {
LABEL_15:
    KeyHandle_8.RootDirectory = KeyHandle;
    KeyHandle_8.Length = 48;
    KeyHandle_8.ObjectName = (_UNICODE_STRING *)&CmpStrCurrentDockInfoString;
    KeyHandle_8.Attributes = 576;
    LODWORD(ResultLengthd) = 1;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    if( ZwCreateKey(
           &DestinationString,
           0x2001Fui64,
           &KeyHandle_8,
           0i64,
           0i64,
           (UINT64)ResultLengthd,
           &NewProfileNumber) >= 0 )
    {
      CmpAddDockingInfo(DestinationString, v11 + 4);
      ZwClose((_HANDLE)DestinationString);
      DestinationString = 0i64;
    }
    if( *(_WORD *)(v11 + 8) == 1 )
      v2 = 1;
    goto LABEL_19;
  }
  if( *v13 != 3 )
  {
LABEL_19:
    KeyHandle_8.Length = 48;
    KeyHandle_8.RootDirectory = v5;
    KeyHandle_8.ObjectName = (_UNICODE_STRING *)L"24";
    KeyHandle_8.Attributes = 576;
    *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
    if( ZwOpenKey(
           &DestinationString,
           0xF003Fui64,
           &KeyHandle_8,
           v10,
           Lengthd,
           (INT64)ResultLengthd,
           (INT64)Dispositionc,
           v44) >= 0 )
    {
      CmDeleteKeyRecursive(DestinationString, 0i64, (__int64)v60, v14, Lengthe);
      ZwClose((_HANDLE)DestinationString);
      DestinationString = 0i64;
    }
    LODWORD(ResultLengthe) = 3;
    v6 = ZwCreateKey(&DestinationString, 0x20ui64, &KeyHandle_8, 0i64, 0i64, (UINT64)ResultLengthe, &NewProfileNumber);
    if( v6 >= 0 )
    {
      *(_QWORD *)&DestinationString_8.Length = 0x1000000i64;
      DestinationString_8.Buffer = (wchar_t *)v59;
      RtlUnicodeStringPrintf(
        &DestinationString_8,
        (WCHAR *)L"\\Registry\\Machine\\%wZ\\CurrentControlSet\\Hardware Profiles\\%04d");
      LODWORD(ResultLengthf) = DestinationString_8.Length;
      v6 = ZwSetValueKey(
             DestinationString,
             &CmSymbolicLinkValueName,
             0i64,
             6ui64,
             DestinationString_8.Buffer,
             (UINT64)ResultLengthf);
      if( !(unsigned __int8)RtlEqualUnicodeString(&String1, &CmpSystemHiveNameString, 1u) )
      {
        ZwClose((_HANDLE)DestinationString);
        DestinationString = 0i64;
        *(_QWORD *)&DestinationString_8.Length = 0x1000000i64;
        DestinationString_8.Buffer = (wchar_t *)v59;
        RtlUnicodeStringPrintf(&DestinationString_8, (WCHAR *)L"\\Registry\\Machine\\%wZ\\CurrentControlSet\\%wZ");
        KeyHandle_8.Length = 48;
        KeyHandle_8.ObjectName = &DestinationString_8;
        KeyHandle_8.RootDirectory = 0i64;
        LODWORD(ResultLengthg) = 3;
        KeyHandle_8.Attributes = 576;
        *(_OWORD *)&KeyHandle_8.SecurityDescriptor = 0i64;
        v6 = ZwCreateKey(
               &DestinationString,
               0x20ui64,
               &KeyHandle_8,
               0i64,
               0i64,
               (UINT64)ResultLengthg,
               &NewProfileNumber);
        if( v6 >= 0 )
        {
          *(_QWORD *)&DestinationString_8.Length = 0x1000000i64;
          DestinationString_8.Buffer = (wchar_t *)v59;
          RtlUnicodeStringPrintf(&DestinationString_8, (WCHAR *)L"\\Registry\\Machine\\%wZ\\CurrentControlSet\\%wZ");
          LODWORD(ResultLengthm) = DestinationString_8.Length;
          v6 = ZwSetValueKey(
                 DestinationString,
                 &CmSymbolicLinkValueName,
                 0i64,
                 6ui64,
                 DestinationString_8.Buffer,
                 (UINT64)ResultLengthm);
        }
      }
    }
    if( v2 )
    {
      Handle = 0i64;
      LOBYTE(v46) = 0;
      LODWORD(v57) = 131073;
      WORD2(v57) = 0;
      v6 = CmSetAcpiHwProfile(
             (unsigned __int16 *)&v57,
             (__int64(__fastcall *)(INT64, char *, _QWORD))CmpHwprofileDefaultSelect,
             v15,
             (__int64)&Handle,
             &v46);
      ZwClose((_HANDLE)Handle);
    }
    goto LABEL_25;
  }
  v6 = CmpCloneHwProfile(
         KeyHandle,
         Parent,
         NewProfile,
         HIDWORD(NewProfileNumber),
         v12,
         &NewProfile,
         (UINT64 *)((char *)&NewProfileNumber + 4));
  if( v6 < 0 )
  {
    NewProfile = 0i64;
    goto LABEL_25;
  }
  LODWORD(ResultLengthl) = 4;
  v6 = ZwSetValueKey(
         KeyHandle,
         (_UNICODE_STRING *)&CmpCurrentConfigString,
         0i64,
         4ui64,
         (char *)&NewProfileNumber + 4,
         (UINT64)ResultLengthl);
  if( v6 >= 0 )
  {
LABEL_51:
    CmpAddAliasEntry(KeyHandle, v11 + 4, HIDWORD(NewProfileNumber));
    goto LABEL_15;
  }
LABEL_25:
  if( v5 )
    ZwClose((_HANDLE)v5);
  if( KeyHandle )
    ZwClose((_HANDLE)KeyHandle);
  if( NewProfile )
    ZwClose((_HANDLE)NewProfile);
  if( Parent )
    ZwClose((_HANDLE)Parent);
  if( DestinationString )
    ZwClose((_HANDLE)DestinationString);
  return(unsigned int)v6;
}

Referenced by:

CmInitSystem1