KiIntSteerConnect

__int64 __fastcall KiIntSteerConnect(__int64 *a1, unsigned int a2, __int64 a3){
  UINT64 v3; 
  int v6; 
  char v7; 
  char *PoolWithTag; 
  UINT64 v9; 
  char *v10; 
  __int64 *v11; 
  _QWORD *v12; 
  unsigned __int8 v13; 
  unsigned __int64 v14; 
  INT64 *i; 
  INT64 v16; 
  int v17; 
  INT64 v18; 
  unsigned __int16 Group; 
  __int16 v20; 
  unsigned __int64 Mask; 
  bool v22; 
  INT64 **v23; 
  char *v24; 
  char **v25; 
  int v27; 
  int v28; 
  __int64 v29; 
  __int64 v30; 
  __int64 v31; 
  __int64 v32; 
  INT64 IsPrimaryInterrupt; 
  GROUP_AFFINITY GroupAffinity; 
  int v35; 
  INT64 a7; 
  v3 = a2;
  LODWORD(a7) = 0;
  GroupAffinity = 0i64;
  if( !KiIntSteerEtwHandle )
    EtwRegister(
      &INTSTEER_ETW_PROVIDER,
      (PETWENABLECALLBACK)KiIntSteerEventTraceControlCallback,
      0i64,
      &KiIntSteerEtwHandle);
  if( !*(_DWORD *)a3 )
  {
    v35 = *(_DWORD *)(a3 + 56);
    if( (unsigned __int8)off_140C00780[0]() )
    {
      v7 = 0;
      v28 = ((__int64(__fastcall *)(__int64, int *))off_140C00768[0])(a3, &v35);
      if( !v28 )
        goto LABEL_32;
    }
    else
    {
      v7 = 1;
      v28 = 0;
    }
    if( v28 < 0 )
      goto LABEL_35;
LABEL_32:
    v6 = v35;
    goto LABEL_6;
  }
  if( *(_DWORD *)a3 != 3 )
  {
LABEL_35:
    v29 = *(unsigned __int16 *)(a3 + 32);
    v30 = *(_QWORD *)(a3 + 24);
    if( LOWORD(KiIntSteerAffinitizedInterrupts[0]) <= (unsigned __int16)v29 )
      LOWORD(KiIntSteerAffinitizedInterrupts[0]) = v29 + 1;
    *(_QWORD *)&KiIntSteerAffinitizedInterrupts[2 * v29 + 2] |= v30;
    return 0i64;
  }
  v6 = -1;
  v7 = 1;
  v35 = -1;
LABEL_6:
  PoolWithTag = (char *)ExAllocatePoolWithTag(NonPagedPoolNx, 0x98ui64, 0x6B725449ui64);
  v10 = PoolWithTag;
  if( PoolWithTag )
  {
    *((_QWORD *)PoolWithTag + 4) = a1;
    *((_DWORD *)PoolWithTag + 6) = v3;
    *((_QWORD *)PoolWithTag + 5) = 0i64;
    *((_QWORD *)PoolWithTag + 6) = 0i64;
    PoolWithTag[56] = v7;
    *((_OWORD *)PoolWithTag + 4) = *(_OWORD *)a3;
    *((_OWORD *)PoolWithTag + 5) = *(_OWORD *)(a3 + 16);
    *((_OWORD *)PoolWithTag + 6) = *(_OWORD *)(a3 + 32);
    *((_OWORD *)PoolWithTag + 7) = *(_OWORD *)(a3 + 48);
    *((_OWORD *)PoolWithTag + 8) = *(_OWORD *)(a3 + 64);
    *((_QWORD *)PoolWithTag + 18) = *(_QWORD *)(a3 + 80);
    if( (_DWORD)v3 )
    {
      v11 = a1;
      v9 = v3;
      do
      {
        v12 = (_QWORD *)*v11++;
        v12[22] = 0i64;
        v12[24] = 0i64;
        v12[25] = 0i64;
        v12[27] = 0i64;
        v12[21] = v10;
        --v9;
      }
      while( v9 );
    }
    KeAcquireSpinLockRaiseToDpc(&KiIntTrackSpinlock, v9);
    v14 = v13;
    if( *(_DWORD *)a3 )
    {
LABEL_11:
      i = (INT64 *)ExAllocatePoolWithTag(NonPagedPoolNx, 0xD0ui64, 0x6B725449ui64);
      if( i )
      {
        LOBYTE(IsPrimaryInterrupt) = v7;
        v17 = KiIntSteerChooseInitialTargetProcessors(
                a1,
                (unsigned int)v3,
                a3,
                v16,
                IsPrimaryInterrupt,
                &GroupAffinity,
                (INT64)&a7);
        if( v17 >= 0 )
        {
          memset((INT64)i, 0i64);
          i[19] = 0i64;
          Group = GroupAffinity.Group;
          i[3] = (INT64)(i + 2);
          i[2] = (INT64)(i + 2);
          *((_DWORD *)i + 8) = v6;
          *((_BYTE *)i + 132) = 0;
          v20 = *(_WORD *)(a3 + 32);
          i[17] = 0i64;
          *((_WORD *)i + 72) = v20;
          *((_DWORD *)i + 32) = a7;
          Mask = GroupAffinity.Mask;
          i[20] = GroupAffinity.Mask;
          i[22] = Mask;
          *((_WORD *)i + 84) = Group;
          *((_WORD *)i + 92) = Group;
          v22 = *((_DWORD *)i + 32) == 2;
          *(_OWORD *)(i + 5) = *(_OWORD *)a3;
          *(_OWORD *)(i + 7) = *(_OWORD *)(a3 + 16);
          *(_OWORD *)(i + 9) = *(_OWORD *)(a3 + 32);
          *(_OWORD *)(i + 11) = *(_OWORD *)(a3 + 48);
          *(_OWORD *)(i + 13) = *(_OWORD *)(a3 + 64);
          i[15] = *(_QWORD *)(a3 + 80);
          if( v22 )
            i[25] = *(_QWORD *)(*a1 + 272);
          v23 = (INT64 **)qword_140C2AED8;
          if( *(INT64 **)qword_140C2AED8 != &KiIntTrackRootList )
            goto LABEL_47;
          *i = (INT64)&KiIntTrackRootList;
          i[1] = (INT64)v23;
          *v23 = i;
          ++KiIntTrackRootCount;
          qword_140C2AED8 = (__int64)i;
          KiIntSteerUpdateDeviceInterruptMask((INT64)(i + 20), 0i64, v18);
          if( *((_DWORD *)i + 32) )
            KiIntSteerSetDestination((INT64)i, (INT64)(i + 20));
          v17 = 0;
        }
        if( !v17 )
        {
LABEL_20:
          *((_QWORD *)v10 + 2) = i;
          v24 = (char *)(i + 2);
          v25 = (char **)*((_QWORD *)v24 + 1);
          if( *v25 == v24 )
          {
            *(_QWORD *)v10 = v24;
            *((_QWORD *)v10 + 1) = v25;
            *v25 = v10;
            *((_QWORD *)v24 + 1) = v10;
            KiIntSteerLogState((INT64)v10, (EVENT_DESCRIPTOR *)&PPM_ETW_INTERRUPT_STEERING_STATE_CONNECT);
LABEL_22:
            KxReleaseSpinLock(&KiIntTrackSpinlock);
            __writecr8(v14);
            return(unsigned int)v17;
          }
LABEL_47:
          __fastfail(3u);
        }
      }
      else
      {
        v17 = -1073741670;
      }
      ExFreePoolWithTag(v10, 0x6B725449u);
      goto LABEL_22;
    }
    for( i = (INT64 *)KiIntTrackRootList; ; i = (INT64 *)*i )
    {
      if( i == &KiIntTrackRootList )
        goto LABEL_11;
      v27 = *((_DWORD *)i + 8);
      if( v27 != -1 && v27 == v6 )
        break;
    }
    if( *((_DWORD *)i + 32) == 2 )
    {
      *(_BYTE *)(i[25] + 16) = 0;
      *((_DWORD *)i + 32) = 0;
      *((_BYTE *)i + 132) = 0;
      v31 = *(unsigned __int16 *)(a3 + 32);
      v32 = *(_QWORD *)(a3 + 24);
      if( LOWORD(KiIntSteerAffinitizedInterrupts[0]) <= (unsigned __int16)v31 )
        LOWORD(KiIntSteerAffinitizedInterrupts[0]) = v31 + 1;
      *(_QWORD *)&KiIntSteerAffinitizedInterrupts[2 * v31 + 2] |= v32;
    }
    v17 = 0;
    goto LABEL_20;
  }
  return 3221225626i64;
}

Referenced by:

KeConnectInterrupt