// MeSetupConfiguration - decompiled from Platform.efi
__int64 __fastcall MeSetupConfiguration(unsigned __int8 *SourceBuffer, __int64 a2)
{
  __int64 v3; // rax
  __int64 v4; // rax
  __int64 v5; // rax
  __int64 v6; // rbx
  _DWORD *v7; // rsi
  __int64 v8; // rax
  unsigned __int8 *v9; // rdx
  __int64 v10; // rcx
  unsigned int n7; // eax
  int n164; // edx
  _BYTE DestinationBuffer[16]; // [rsp+20h] [rbp-59h] BYREF
  _BYTE v15[160]; // [rsp+30h] [rbp-49h] BYREF
  _DWORD *v16; // [rsp+E0h] [rbp+67h] BYREF
  __int64 v17; // [rsp+E8h] [rbp+6Fh] BYREF
  __int64 v18; // [rsp+F0h] [rbp+77h] BYREF

  v17 = a2; /*0x4970*/
  v18 = 0; /*0x4998*/
  v16 = 0; /*0x499c*/
  LOBYTE(v17) = 0; /*0x49a0*/
  DebugPrint(0x40u, "[ME] MeRouteSpsAdvancedConfig called...\n"); /*0x49ab*/
  if ( !SourceBuffer ) /*0x49b3*/
  {
    DebugPrint(0x80000000, "[ME] Invalid parameters (MeRouteSpsAdvancedConfig)\n"); /*0x4bc4*/
    return 0x8000000000000002uLL; /*0x4bc9*/
  }
  v3 = (*(__int64 (__fastcall **)(void *, _QWORD, __int64 *))(BootServices + 320))(&unk_F9A0, 0, &v18); /*0x49cd*/
  if ( v3 >= 0 )
  {
    v4 = (*(__int64 (__fastcall **)(__int64, unsigned __int8 *, __int64 *))(v18 + 40))(v18, SourceBuffer, &v17); /*0x4a16*/
    if ( v4 >= 0 ) /*0x4a1c*/
      goto LABEL_7; /*0x4a1c*/
    DebugPrint(0x80000000, "[ME] ERROR: Cannot update SPS policy (%r)\n", v4);
  }
  else
  {
    DebugPrint(0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", v3); /*0x49e9*/
    Assert((__int64)"e:\\hs\\PurleyRpPkg\\Platform\\Dxe\\Setup\\MeSpsSetup.c", 454, (__int64)"!EFI_ERROR (Status)"); /*0x4a01*/
  }
  LOBYTE(v17) = 1; /*0x4a30*/
LABEL_7:
  v5 = (*(__int64 (__fastcall **)(void *, _QWORD, _DWORD **))(BootServices + 320))(&unk_FBC0, 0, &v16); /*0x4a34*/
  v6 = v5; /*0x4a4e*/
  if ( v5 < 0 ) /*0x4a54*/
  {
    DebugPrint(0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", v5); /*0x4a63*/
    Assert((__int64)"e:\\hs\\PurleyRpPkg\\Platform\\Dxe\\Setup\\MeSpsSetup.c", 471, (__int64)"!EFI_ERROR (Status)"); /*0x4a7b*/
LABEL_19:
    LOBYTE(v17) = 1; /*0x4b3e*/
    goto LABEL_21; /*0x4b42*/
  }
  CopyMem(DestinationBuffer, v16 + 1, 0x10u); /*0x4a97*/
  v7 = v16; /*0x4a9c*/
  DebugPrint(0x40u, "UpdateDxeMePolicy start\n"); /*0x4aaa*/
  if ( !v7 )
  {
    v6 = 0x8000000000000002uLL; /*0x4ab4*/
LABEL_16:
    DebugPrint(0x80000000, "[ME] ERROR: Cannot update ME policy (%r)\n", v6);
    goto LABEL_21; /*0x4b11*/
  }
  v8 = WriteProtocolVar((__int64)&unk_F950, (__int64)v15); /*0x4acb*/
  v9 = v15; /*0x4ad3*/
  v6 = v8; /*0x4ad7*/
  if ( v8 < 0 ) /*0x4ada*/
    v9 = 0; /*0x4ada*/
  if ( v9 ) /*0x4ae1*/
    SetupIfrStringLoader(v7, v9); /*0x4ae6*/
  DebugPrint(0x40u, "UpdateDxeMePolicy exit\n"); /*0x4af5*/
  if ( v6 < 0 ) /*0x4afd*/
    goto LABEL_16; /*0x4afd*/
  if ( StrCmpS(DestinationBuffer, (UINTN)(v16 + 1), 16) )
  {
    DebugPrint(0x40u, "[ME] INFO: ME setup options have changed. Requesting Global reset..\n");
    goto LABEL_19; /*0x4b39*/
  }
  LOBYTE(v17) = 0; /*0x4b44*/
LABEL_21:
  MeFirmwareUpdateHandler(SourceBuffer); /*0x4b48*/
  n7 = *(_DWORD *)(ReadCpuPciCfg(v10, 22, 0) + 64) & 0xF; /*0x4b60*/
  if ( n7 > 7 || (n164 = 164, !_bittest(&n164, n7)) ) /*0x4b70*/
  {
    DebugPrint(0x40u, "[ME] Do not send any HECI requests, no chance ME responds\n"); /*0x4b7c*/
    LOBYTE(v17) = 1; /*0x4b81*/
  }
  if ( SetupConfigRouting() || (byte_17BFC = 0, v6 < 0) || (_BYTE)v17 || v18 && *(_BYTE *)(v18 + 24) ) /*0x4ba9*/
    byte_17BFC = 1; /*0x4baf*/
  return v6; /*0x4bd6*/
}
