BgpFwLibraryInitialize

NTSTATUS __stdcall BgpFwLibraryInitialize(PVOID Context, UINT64 Phase){
  int v2; 
  int v4; 
  NTSTATUS v5; 
  unsigned int v6; 
  int v7; 
  unsigned int v8; 
  int v9; 
  int v10; 
  unsigned int v11; 
  int v12; 
  unsigned int v13; 
  unsigned int v14; 
  _OWORD *v15; 
  _OWORD *v16; 
  __int64 v17; 
  _OWORD *v18; 
  __int128 v19; 
  char *v20; 
  UINT8 *v21; 
  int v22; 
  __int64 v23; 
  UINT8 *v24; 
  UINT8 *v25; 
  __int64 v26; 
  __int64 v27; 
  UINT8 *v28; 
  UINT8 *v29; 
  __int64 v30; 
  __int64 v31; 
  int v32; 
  __int64 v33; 
  __int64 v34; 
  __int64 v35; 
  __int64 v36; 
  int v37; 
  NTSTATUS result; 
  unsigned int *v39; 
  int v40; 
  void *v41; 
  __int64 v42; 
  __int64 v43; 
  __int64 v44; 
  WCHAR *v45; 
  WCHAR *v46; 
  __int64 v47; 
  unsigned int v48; 
  int v49; 
  unsigned int v50; 
  unsigned int v51; 
  v49 = Phase;
  v2 = Phase;
  if( !Context )
    return -1073741811;
  if( *((_DWORD *)Context + 29) < 4u )
    return -1073741735;
  v4 = *((_DWORD *)Context + 30) & 0x16FF0000;
  if( (dword_140C13310 & 1) != 0 && (dword_140C13310 & 4) == 0 && (*((_DWORD *)Context + 30) & 0x100000) == 0 )
    return -1073741811;
  if( (*((_DWORD *)Context + 30) & 0x100000) != 0 )
  {
    BgpFwAcquireLock(0x100000i64);
    if( v2 == -1 )
    {
      qword_140C132C0 = 0i64;
      dword_140C13310 = dword_140C13310 & 0xFFEFE7FD | 0x101800;
      *(_OWORD *)BgInternal = 0i64;
      xmmword_140C132B0 = 0i64;
      result = BgpFwLibraryEnable((INT64)Context + 32);
      if( result < 0 )
        return result;
      dword_140C13310 &= ~0x2000u;
      v39 = (unsigned int *)*((_QWORD *)Context + 2);
      if( v39 )
      {
        BgpFwReservePoolSwap(0, *((_QWORD *)Context + 30), *((_DWORD *)Context + 62), v39);
        RegionHandle = (INT64)ResFwConfigureDisplayStringResources(*((_DWORD **)Context + 32));
        qword_140C133D0 = (INT64)AnFwConfigureProgressResources(*((_DWORD **)Context + 33));
      }
      else
      {
        RegionHandle = 0i64;
        qword_140C133D0 = 0i64;
      }
      LogFwInitialize();
    }
    else
    {
      dword_140C13310 &= ~0x1000u;
      AnFwProgressIndicatorTransition();
    }
    BgpFwReleaseLock();
    goto LABEL_49;
  }
  if( (_DWORD)Phase == -1 )
  {
    BgpFwInitializeLock();
    dword_140C13310 = v40 | 0xC04;
    qword_140C133E8 = (__int64)&qword_140C133E0;
    qword_140C133E0 = (__int64)&qword_140C133E0;
    v41 = (void *)*((_QWORD *)Context + 30);
    if( !v41 || !*((_DWORD *)Context + 62) )
      return -1073741670;
    BgpFwInitializeReservePool(v41, 0x4000ui64, qword_140CF2580);
    v5 = BgpFwLibraryEnable((INT64)Context + 32);
    if( v5 < 0 )
      goto LABEL_59;
    xmmword_140C13348 = *(_OWORD *)((char *)Context + 100);
    if( *((_QWORD *)Context + 11) )
    {
      LODWORD(v42) = BgpFwAllocateMemory(0x18ui64);
      if( !v42 )
        goto LABEL_72;
      *(_QWORD *)v42 = *((_QWORD *)Context + 11);
      *(_DWORD *)(v42 + 8) = *((_DWORD *)Context + 24);
      *(_QWORD *)(v42 + 16) = *((_QWORD *)Context + 11);
      *(_DWORD *)(v42 + 12) = 1;
      qword_140C133B8 = v42;
      v5 = BgpFoInitialize(v42, 0);
      if( v5 < 0 )
        goto LABEL_59;
    }
    if( *((_DWORD *)Context + 29) >= 3u )
    {
      unk_140C13490 = *(_QWORD *)((char *)Context + 404);
      HIDWORD(Color) = *((_DWORD *)Context + 103);
    }
    LODWORD(dword_140C13478) = *((_DWORD *)Context + 100);
    v5 = BgpBcInitializeCriticalMode((__int64)Context, -1);
    if( v5 < 0 )
      goto LABEL_59;
    goto LABEL_49;
  }
  BgpFwAcquireLock(0x100000i64);
  v5 = BgpFwLibraryEnable((INT64)Context + 32);
  if( v5 >= 0 )
  {
    *(PHYSICAL_ADDRESS *)&xmmword_140C132F0 = MmGetPhysicalAddress(qword_140C132C0);
    DWORD2(xmmword_140C132F0) = *((_DWORD *)BgInternal + 2);
    HIDWORD(xmmword_140C132F0) = *((_DWORD *)BgInternal + 1);
    LODWORD(xmmword_140C13300) = *((_DWORD *)BgInternal + 3);
    if( (_DWORD)xmmword_140C132B0 == 4 )
    {
      DWORD2(xmmword_140C13300) = 3;
    }
    else if( (_DWORD)xmmword_140C132B0 == 5 )
    {
      DWORD2(xmmword_140C13300) = 4;
    }
    else
    {
      DWORD2(xmmword_140C13300) = 0;
    }
    v6 = 0;
    v7 = 416;
    v8 = 0;
    xmmword_140C133F0 = *((_OWORD *)Context + 17);
    v9 = 0;
    xmmword_140C13400 = *((_OWORD *)Context + 18);
    xmmword_140C13410 = *((_OWORD *)Context + 19);
    xmmword_140C13420 = *((_OWORD *)Context + 20);
    xmmword_140C13430 = *((_OWORD *)Context + 21);
    xmmword_140C13440 = *((_OWORD *)Context + 22);
    xmmword_140C13450 = *((_OWORD *)Context + 23);
    xmmword_140C13460 = *((_OWORD *)Context + 24);
    if( *((_QWORD *)Context + 9) )
    {
      v8 = 32;
      v9 = *((_DWORD *)Context + 21);
      v6 = (*((_DWORD *)Context + 20) + 15) & 0xFFFFFFF0;
      v7 = v6 + 448;
    }
    v10 = 0;
    v11 = 0;
    v48 = 0;
    if( *((_QWORD *)Context + 16) )
    {
      v10 = 32;
      v48 = 32;
      v11 = (*((_DWORD *)Context + 34) + 15) & 0xFFFFFFF0;
      v7 += v11 + 32;
    }
    v12 = v9 + 16480 + v7;
    v13 = v12 - v6 - v10 - v11 - v8 - 416;
    v14 = ((v13 >> 3) + 15) & 0xFFFFFFF0;
    v50 = v14 + v12;
    LODWORD(v15) = BgpFwAllocateMemory(v14 + v12);
    qword_140C13340 = (__int64)v15;
    v16 = v15;
    if( v15 )
    {
      v17 = 3i64;
      v18 = Context;
      do
      {
        *v15 = *v18;
        v15[1] = v18[1];
        v15[2] = v18[2];
        v15[3] = v18[3];
        v15[4] = v18[4];
        v15[5] = v18[5];
        v15[6] = v18[6];
        v15 += 8;
        v19 = v18[7];
        v18 += 8;
        *(v15 - 1) = v19;
        --v17;
      }
      while( v17 );
      *v15 = *v18;
      v15[1] = v18[1];
      *(_DWORD *)v16 = v50;
      *((_DWORD *)v16 + 30) |= 0x100000u;
      *((_QWORD *)v16 + 30) = 0i64;
      *((_DWORD *)v16 + 62) = 0;
      *((_QWORD *)v16 + 33) = 0i64;
      *((_QWORD *)v16 + 32) = 0i64;
      v51 = v6 + v8;
      v20 = (char *)v16 + v11 + v6 + v8 + v48 + 416;
      BgpFwInitializeReservePool(&v20[v14], v13, v20);
      qword_140C133C0 = 0i64;
      if( !v6 )
        goto LABEL_76;
      v21 = (UINT8 *)&v16[v8 / 0x10 + 26];
      memmove(v21, *((UINT8 **)Context + 9), *((unsigned int *)Context + 20));
      *((_DWORD *)v16 + 107) = 1;
      *((_QWORD *)v16 + 52) = v21;
      *((_DWORD *)v16 + 106) = *((_DWORD *)Context + 20);
      *((_QWORD *)v16 + 54) = v21;
      dword_140C13310 |= 0x800u;
      v22 = BgpFoInitialize((__int64)(v16 + 26), 1);
      dword_140C13310 &= ~0x800u;
      if( v22 >= 0 )
        qword_140C133C0 = (__int64)(v16 + 26);
      if( !qword_140C133C0 )
LABEL_76:
        DbgPrintEx(0x65u, 0, "BGFX system font initialization failure!\n");
      qword_140C133C8 = 0i64;
      if( v11 )
      {
        v23 = (__int64)v16 + v51 + 416;
        v24 = (UINT8 *)(v23 + v48);
        memmove(v24, *((UINT8 **)Context + 16), *((unsigned int *)Context + 34));
        *(_DWORD *)(v23 + 12) = 1;
        *(_QWORD *)v23 = v24;
        *(_DWORD *)(v23 + 8) = *((_DWORD *)Context + 34);
        *(_QWORD *)(v23 + 16) = v24;
        qword_140C133C8 = v23;
      }
      memset((INT64)&qword_140C13358, 0i64);
      if( *((_QWORD *)Context + 18) )
      {
        LODWORD(v25) = BgpFwAllocateMemory(*((unsigned int *)Context + 40));
        qword_140C13358 = (INT64)v25;
        if( v25 )
        {
          memmove(v25, *((UINT8 **)Context + 18), *((unsigned int *)Context + 40));
          *(_DWORD *)Size = *((_DWORD *)Context + 40);
          qword_140C1336C = *(_QWORD *)((char *)Context + 164);
        }
      }
      v26 = *((_QWORD *)Context + 23);
      if( v26 && (int)BgpGxProcessQrCodeBitmap(v26, &qword_140C13398) < 0 )
        qword_140C13398 = 0i64;
      v27 = *((_QWORD *)Context + 27);
      if( v27 )
      {
        v43 = -1i64;
        v44 = -1i64;
        do
          ++v44;
        while( *(_WORD *)(v27 + 2 * v44) );
        LODWORD(v45) = BgpFwAllocateMemory(2 * v44 + 2);
        Dst = v45;
        if( v45 )
        {
          v46 = (WCHAR *)*((_QWORD *)Context + 27);
          do
            ++v43;
          while( v46[v43] );
          wcscpy_s(Dst, v43 + 1, v46);
        }
      }
      qword_140C133B8 = 0i64;
      if( !*((_QWORD *)Context + 11) )
        goto LABEL_35;
      LODWORD(v28) = BgpFwAllocateMemory(*((unsigned int *)Context + 24));
      v29 = v28;
      if( !v28 )
        goto LABEL_35;
      memmove(v28, *((UINT8 **)Context + 11), *((unsigned int *)Context + 24));
      LODWORD(v30) = BgpFwAllocateMemory(0x18ui64);
      v31 = v30;
      if( v30 )
      {
        *(_DWORD *)(v30 + 12) = 0;
        *(_QWORD *)v30 = v29;
        *(_DWORD *)(v30 + 8) = *((_DWORD *)Context + 24);
        *(_QWORD *)(v30 + 16) = v29;
        if( (int)BgpFoInitialize(v30, 0) >= 0 )
        {
          qword_140C133B8 = v31;
LABEL_35:
          if( *((_DWORD *)Context + 29) >= 3u )
          {
            unk_140C13490 = *(_QWORD *)((char *)Context + 404);
            HIDWORD(Color) = *((_DWORD *)Context + 103);
          }
          v32 = *((_DWORD *)Context + 100);
          dword_140C13310 |= 0x8000u;
          LODWORD(dword_140C13478) = v32;
          BgpBcInitializeCriticalMode((__int64)Context, v49);
          dword_140C13310 = dword_140C13310 & 0xFFFF77FF | 0x800;
          LODWORD(v33) = BgpFwAllocateMemory(0x38ui64);
          dword_140C13310 &= ~0x800u;
          v34 = v33;
          if( v33 )
          {
            v35 = *((_QWORD *)Context + 32);
            if( v35 )
            {
              *(_OWORD *)v34 = *(_OWORD *)v35;
              *(_QWORD *)(v34 + 16) = *(_QWORD *)(v35 + 16);
              *(_DWORD *)(v34 + 24) = *(_DWORD *)(v35 + 24);
              *((_QWORD *)v16 + 32) = v34;
              v34 += 28i64;
            }
            v36 = *((_QWORD *)Context + 33);
            if( v36 )
            {
              *(_OWORD *)v34 = *(_OWORD *)v36;
              *(_QWORD *)(v34 + 16) = *(_QWORD *)(v36 + 16);
              *(_DWORD *)(v34 + 24) = *(_DWORD *)(v36 + 24);
              *((_QWORD *)v16 + 33) = v34;
            }
          }
          RegionHandle = (INT64)ResFwConfigureDisplayStringResources(*((_DWORD **)Context + 32));
          qword_140C133D0 = (INT64)AnFwConfigureProgressResources(*((_DWORD **)Context + 33));
          v37 = *((_DWORD *)Context + 31);
          dword_140C13310 |= v4 | 0x80;
          dword_140C13314 = v37;
          BgpFwReleaseLock();
          qword_140C133B0 = *((_QWORD *)Context + 29);
LABEL_49:
          dword_140C13310 |= 1u;
          v5 = 0;
          goto LABEL_50;
        }
        BgpFwFreeMemory(v29);
        v47 = v31;
      }
      else
      {
        v47 = (__int64)v29;
      }
      BgpFwFreeMemory(v47);
      goto LABEL_35;
    }
    BgpFwReleaseLock();
LABEL_72:
    v5 = -1073741801;
    goto LABEL_59;
  }
  BgpFwReleaseLock();
LABEL_50:
  if( v5 < 0 )
LABEL_59:
    BgpFwLibraryDestroy();
  return v5;
}

Referenced by:

BgLibraryInitialize