HalQueryRealTimeClock

NTSTATUS __fastcall HalQueryRealTimeClock(_TIME_FIELDS *TimeFields){
  INT64 v2; 
  INT64 v3; 
  unsigned int v4; 
  __int64 v5; 
  unsigned int v6; 
  __int64 QuadPart; 
  INT64 v8; 
  _KSPIN_LOCK *v9; 
  _LARGE_INTEGER v10; 
  INT64 v11; 
  INT64 *v12; 
  int v13; 
  int v14; 
  int v15; 
  unsigned __int8 CurrentIrql; 
  int v17; 
  int v18; 
  int v19; 
  _LARGE_INTEGER v20; 
  int v22[8]; 
  char RtcValid[8]; 
  _LARGE_INTEGER UtcTime; 
  __int64 v25; 
  _WORD v26[8]; 
  __int128 v27; 
  _ACPI_REAL_TIME AcpiRealTime; 

  UtcTime.QuadPart = 0i64;
  RtcValid[0] = 0;
  AcpiRealTime = 0i64;
  *(_OWORD *)v26 = 0i64;
  HalpSetVirtualRtc(0i64);
  v4 = ((unsigned int)HalpPlatformFlags >> 2) & 1;
  if( v4 )
    HalpReadCmosTime(v26, v2, v3);
  v25 = 0i64;
  v5 = *(&ExBootDevicesRemovedEvent + 103);
  v27 = 0i64;
  if( !*(&ExBootDevicesRemovedEvent + 103) || !**(&ExBootDevicesRemovedEvent + 103) )
  {
    LOBYTE(QuadPart) = RtlpTimeFieldsToTimeNoLeapSeconds(v26, &UtcTime);
    goto LABEL_22;
  }
  v6 = *(_DWORD *)(*(&ExBootDevicesRemovedEvent + 103) + 4i64);
  _InterlockedOr(v22, 0);
  if( !RtlpTimeFieldsToTimeNoLeapSeconds(v26, &v25) )
    goto LABEL_6;
  QuadPart = v25;
  v10.QuadPart = 0i64;
  if( !v6 )
    goto LABEL_20;
  v12 = (INT64 *)(v5 + 8);
  while( 1 )
  {
    v8 = *v12;
    if( *v12 >= 0 )
    {
      v9 = (_KSPIN_LOCK *)(v8 + 10000000);
      if( QuadPart < v8 + 10000000 )
      {
        if( QuadPart < v8 )
          goto LABEL_20;
        QuadPart = 2 * QuadPart - v8;
      }
      else
      {
        QuadPart += 10000000i64;
      }
      goto LABEL_16;
    }
    v8 &= ~0x8000000000000000ui64;
    v9 = (_KSPIN_LOCK *)(v8 + 10000000);
    if( QuadPart < v8 + 10000000 )
      break;
    QuadPart -= 10000000i64;
LABEL_16:
    v10.QuadPart = v10.anonymous_0.LowPart + 1;
    v25 = QuadPart;
    ++v12;
    if( v10.anonymous_0.LowPart >= v6 )
      goto LABEL_20;
  }
  if( QuadPart < v8 || QuadPart >= (__int64)v9 )
  {
LABEL_20:
    UtcTime.QuadPart = QuadPart;
    LOBYTE(QuadPart) = 1;
    goto LABEL_22;
  }
LABEL_6:
  LOBYTE(QuadPart) = 0;
LABEL_22:
  if( (_BYTE)v4 && (_BYTE)QuadPart )
  {
    if( !*(&stru_140CF2E80 + 1829) )
    {
      QuadPart = PsGetCurrentServerSiloGlobals(v9)->ExTimeZoneState->TimeZoneBias.QuadPart;
      UtcTime.QuadPart += QuadPart;
    }
    goto LABEL_44;
  }
  v13 = dword_140C49D0C;
  if( KeGetCurrentIrql() < 2u && !dword_140C49D0C )
  {
    HalpQueryAcpiRealTimeClock(&AcpiRealTime);
    if( v14 < 0 )
    {
      if( v14 != -1073741822 )
        _InterlockedOr(&HalpTimerRtcErrorCode, 2u);
    }
    else
    {
      HalpAcpiRealTimeToUtcTime(&AcpiRealTime, &UtcTime);
      if( v15 >= 0 )
        goto LABEL_30;
    }
  }
  if( byte_140C49D08 && v13 && (HalpPlatformFlags & 8) == 0 )
  {
    if( KeGetCurrentIrql() >= 2u )
    {
      HalEfiGetTime((BOOL *)&UtcTime, v8, (INT64 *)v10.QuadPart, v11);
      v18 = v19;
    }
    else
    {
      CurrentIrql = KeGetCurrentIrql();
      __writecr8(2ui64);
      HalEfiGetTime((BOOL *)&UtcTime, v8, (INT64 *)v10.QuadPart, v11);
      v18 = v17;
      __writecr8(CurrentIrql);
    }
    if( v18 < 0 )
    {
      if( v18 != -1073741822 )
        _InterlockedOr(&HalpTimerRtcErrorCode, 4u);
      goto LABEL_42;
    }
LABEL_30:
    LOBYTE(v4) = 1;
LABEL_45:
    v20 = UtcTime;
    if( *(__int64 *)&KUSER_SHARED_DATA.SystemTime.LowPart > UtcTime.QuadPart + 864000000000i64 )
    {
      UtcTime = *(_LARGE_INTEGER *)&KUSER_SHARED_DATA.SystemTime.LowPart;
      _InterlockedOr(&HalpTimerRtcErrorCode, 1u);
      v20 = *(_LARGE_INTEGER *)&KUSER_SHARED_DATA.SystemTime.LowPart;
    }
    if( !*(&stru_140CF2E80 + 1829) )
      UtcTime.QuadPart = v20.QuadPart
                       - PsGetCurrentServerSiloGlobals(*(_KSPIN_LOCK **)&KUSER_SHARED_DATA.SystemTime.LowPart)->ExTimeZoneState->TimeZoneBias.QuadPart;
    LOWORD(QuadPart) = RtlpTimeToTimeFields(&UtcTime, TimeFields, v10);
  }
  else
  {
LABEL_42:
    LODWORD(QuadPart) = HalpQueryVirtualRtc(&UtcTime, (UINT8 *)RtcValid);
    LOBYTE(v4) = QuadPart;
    if( !RtcValid[0] )
      _InterlockedOr(&HalpTimerRtcErrorCode, 8u);
LABEL_44:
    if( (_BYTE)v4 )
      goto LABEL_45;
    _InterlockedOr(&HalpTimerRtcErrorCode, 0x10u);
  }
  LOBYTE(QuadPart) = v4;
  return QuadPart;
}

Referenced by:

ExUpdateSystemTimeFromCmos
ExpRefreshSystemTime
GetBootSystemTime
HaliSetWakeAlarm
HalpCheckWakeupTimeAndAdjust
NtSetSystemTime