spnegoIsMechTypeAvailable

decompiled_code:
__int64 __fastcall spnegoIsMechTypeAvailable(__int64 a1, unsigned int a2, __int64 a3)
{
int v4; // eax

v4 = IsValidSpnegoToken();
if ( !a3 || !v4 )
return 4294967288LL;
if ( !(unsigned int)IsValidMechOid(a2) )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 56) == 1 )
return FindMechOIDInMechList(a1 + 48, a2, a3);
return 4294967291LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, __int64 a3)
{
int v4; // eax

v4 = IsValidSpnegoToken();
if ( !a3 || !v4 )
return 4294967288LL;
if ( !(unsigned int)IsValidMechOid(a2) )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 56) == 1 )
return FindMechOIDInMechList(a1 + 48, a2, a3);
return 4294967291LL;
}

original_code:
int spnegoIsMechTypeAvailable( SPNEGO_TOKEN_HANDLE hSpnegoToken, SPNEGO_MECH_OID MechOID, int * piMechTypeIndex )
{
   int nReturn = -8;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != piMechTypeIndex &&
         IsValidMechOid( MechOID ) &&
         0 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[0].iElementPresent
            == 1 )
      {
         nReturn = FindMechOIDInMechList(
                     &pSpnegoToken->aElementArray[0],
                     MechOID, piMechTypeIndex );
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoIsMechTypeAvailable(void *hSpnegoToken, enum spnego_mech_oid MechOID, int *piMechTypeIndex)
{
   int nReturn = -8;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != piMechTypeIndex &&
         IsValidMechOid( MechOID ) &&
         0 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[0].iElementPresent
            == 1 )
      {
         nReturn = FindMechOIDInMechList(
                     &pSpnegoToken->aElementArray[0],
                     MechOID, piMechTypeIndex );
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types:
piMechTypeIndex
int *


spnegoGetContextFlags

repo: turnkeylinux/tklbam-squid

decompiled_code:
__int64 __fastcall spnegoGetContextFlags(__int64 a1, _BYTE *a2)
{
int v2; // eax
unsigned int v3; // er12
_BYTE *v4; // rax

v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return (unsigned int)-8;
v3 = *(_DWORD *)(a1 + 16);
if ( v3 )
return (unsigned int)-8;
if ( *(_DWORD *)(a1 + 96) != 1 )
return (unsigned int)-5;
if ( *(_QWORD *)(a1 + 120) != 2LL )
return (unsigned int)-13;
v4 = *(_BYTE **)(a1 + 112);
if ( *v4 != 1 )
return (unsigned int)-13;
if ( !(unsigned int)IsValidContextFlags((unsigned __int8)v4[1]) )
return (unsigned int)-13;
*a2 = *(_BYTE *)(*(_QWORD *)(a1 + 112) + 1LL);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, _BYTE *a2)
{
int v2; // eax
unsigned int v3; // er12
_BYTE *v4; // rax

v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return (unsigned int)-8;
v3 = *(_DWORD *)(a1 + 16);
if ( v3 )
return (unsigned int)-8;
if ( *(_DWORD *)(a1 + 96) != 1 )
return (unsigned int)-5;
if ( *(_QWORD *)(a1 + 120) != 2LL )
return (unsigned int)-13;
v4 = *(_BYTE **)(a1 + 112);
if ( *v4 != 1 )
return (unsigned int)-13;
if ( !(unsigned int)IsValidContextFlags((unsigned __int8)v4[1]) )
return (unsigned int)-13;
*a2 = *(_BYTE *)(*(_QWORD *)(a1 + 112) + 1LL);
return v3;
}

original_code:
int spnegoGetContextFlags( SPNEGO_TOKEN_HANDLE hSpnegoToken, unsigned char* pucContextFlags )
{
   int nReturn = -8;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pucContextFlags &&
         0 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[1].iElementPresent
            == 1 )
      {
         if ( pSpnegoToken->aElementArray[1].nDatalength == 2 &&
               pSpnegoToken->aElementArray[1].pbData[0] == 1 &&
               IsValidContextFlags( pSpnegoToken->aElementArray[1].pbData[1] ) )
         {
            *pucContextFlags = pSpnegoToken->aElementArray[1].pbData[1];
            nReturn = 0;
         }
         else
         {
            nReturn = -13;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoGetContextFlags(void *hSpnegoToken, unsigned char *pucContextFlags)
{
   int nReturn = -8;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pucContextFlags &&
         0 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[1].iElementPresent
            == 1 )
      {
         if ( pSpnegoToken->aElementArray[1].nDatalength == 2 &&
               pSpnegoToken->aElementArray[1].pbData[0] == 1 &&
               IsValidContextFlags( pSpnegoToken->aElementArray[1].pbData[1] ) )
         {
            *pucContextFlags = pSpnegoToken->aElementArray[1].pbData[1];
            nReturn = 0;
         }
         else
         {
            nReturn = -13;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types:
hSpnegoToken
void *

variable_types:
pucContextFlags
unsigned char *

variable_types: nReturn int

variable_types: struct SpnegoToken { unsigned long nStructSize; unsigned long ulFlags; int ucTokenType; unsigned char *pbBinaryData; unsigned long ulBinaryDataLen; int nNumElements; struct SpnegoElement { unsigned long nStructSize; int iElementPresent; enum spnego_element_type { spnego_element_min=0, spnego_init_mechtypes=1, spnego_init_reqFlags=2, spnego_init_mechToken=3, spnego_init_mechListMIC=4, spnego_targ_negResult=5, spnego_targ_supportedMech=6, spnego_targ_responseToken=7, spnego_targ_mechListMIC=8, spnego_element_max=9 } eElementType; unsigned char type; unsigned char *pbData; unsigned long nDatalength; } aElementArray[4]; } *

return_type: int

user_defined_types: struct SpnegoToken { unsigned long nStructSize; unsigned long ulFlags; int ucTokenType; unsigned char *pbBinaryData; unsigned long ulBinaryDataLen; int nNumElements; struct SpnegoElement aElementArray[4]; }

user_defined_types: struct SpnegoElement { unsigned long nStructSize; int iElementPresent; enum spnego_element_type eElementType; unsigned char type; unsigned char *pbData; unsigned long nDatalength; }


spnegoGetNegotiationResult

decompiled_code:
__int64 __fastcall spnegoGetNegotiationResult(__int64 a1, _DWORD *a2)
{
int v2; // eax

v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 56) != 1 )
return 4294967291LL;
if ( *(_QWORD *)(a1 + 80) != 1LL )
return 4294967283LL;
if ( !(unsigned int)IsValidNegResult(**(unsigned __int8 **)(a1 + 72)) )
return 4294967283LL;
*a2 = **(unsigned __int8 **)(a1 + 72);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, _DWORD *a2)
{
int v2; // eax

v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 56) != 1 )
return 4294967291LL;
if ( *(_QWORD *)(a1 + 80) != 1LL )
return 4294967283LL;
if ( !(unsigned int)IsValidNegResult(**(unsigned __int8 **)(a1 + 72)) )
return 4294967283LL;
*a2 = **(unsigned __int8 **)(a1 + 72);
return 0LL;
}

original_code:
int spnegoGetNegotiationResult( SPNEGO_TOKEN_HANDLE hSpnegoToken, SPNEGO_NEGRESULT* pnegResult )
{
   int nReturn = -8;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pnegResult &&
         1 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[0].iElementPresent
            == 1 )
      {
         if ( pSpnegoToken->aElementArray[0].nDatalength == 1 &&
               IsValidNegResult( *pSpnegoToken->aElementArray[0].pbData ) )
         {
            *pnegResult = *pSpnegoToken->aElementArray[0].pbData;
            nReturn = 0;
         }
         else
         {
            nReturn = -13;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoGetNegotiationResult(void *hSpnegoToken, enum spnego_negResult *pnegResult)
{
   int nReturn = -8;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pnegResult &&
         1 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[0].iElementPresent
            == 1 )
      {
         if ( pSpnegoToken->aElementArray[0].nDatalength == 1 &&
               IsValidNegResult( *pSpnegoToken->aElementArray[0].pbData ) )
         {
            *pnegResult = *pSpnegoToken->aElementArray[0].pbData;
            nReturn = 0;
         }
         else
         {
            nReturn = -13;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types: enum spnego_negResult { spnego_negresult_success=0, spnego_negresult_incomplete=1, spnego_negresult_rejected=2, spnego_negresult_NotUsed=-1 } *

user_defined_types: enum spnego_element_type { spnego_element_min=0, spnego_init_mechtypes=1, spnego_init_reqFlags=2, spnego_init_mechToken=3, spnego_init_mechListMIC=4, spnego_targ_negResult=5, spnego_targ_supportedMech=6, spnego_targ_responseToken=7, spnego_targ_mechListMIC=8, spnego_element_max=9 }


spnegoGetSupportedMechType

decompiled_code:
__int64 __fastcall spnegoGetSupportedMechType(__int64 a1, unsigned int *a2)
{
int v2; // eax
_DWORD *v3; // rbx
unsigned int v4; // er12
__int64 result; // rax
__int64 v6[6]; // [rsp+8h] [rbp-30h] BYREF

v6[0] = 0LL;
v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 96) != 1 )
return 4294967291LL;
v3 = (_DWORD *)((char *)&g_stcMechOIDList + 16);
v4 = 0;
result = 4294967288LL;
while ( *v3 != -1 )
{
result = ASNDerCheckOID(*(_QWORD *)(a1 + 112), v4, *(_QWORD *)(a1 + 120), v6);
v3 += 6;
if ( !(_DWORD)result )
{
*a2 = v4;
return result;
}
++v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned int *a2)
{
int v2; // eax
_DWORD *v3; // rbx
unsigned int v4; // er12
__int64 result; // rax
__int64 v6[6]; // [rsp+8h] [rbp-30h] BYREF

v6[0] = 0LL;
v2 = IsValidSpnegoToken();
if ( !a2 || !v2 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 16) != 1 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 96) != 1 )
return 4294967291LL;
v3 = (_DWORD *)((char *)&g_stcMechOIDList + 16);
v4 = 0;
result = 4294967288LL;
while ( *v3 != -1 )
{
result = ASNDerCheckOID(*(_QWORD *)(a1 + 112), v4, *(_QWORD *)(a1 + 120), v6);
v3 += 6;
if ( !(_DWORD)result )
{
*a2 = v4;
return result;
}
++v4;
}
return result;
}

original_code:
int spnegoGetSupportedMechType( SPNEGO_TOKEN_HANDLE hSpnegoToken, SPNEGO_MECH_OID* pMechOID )
{
   int nReturn = -8;
   int nCtr = 0L;
   long nLength = 0L;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pMechOID &&
         1 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[1].iElementPresent
            == 1 )
      {
         for ( nCtr = 0;
               nReturn != 0 &&
               g_stcMechOIDList[nCtr].eMechanismOID != spnego_mech_oid_NotUsed;
               nCtr++ )
         {
            if ( ( nReturn = ASNDerCheckOID(
                        pSpnegoToken->aElementArray[1].pbData,
                        nCtr,
                        pSpnegoToken->aElementArray[1].nDatalength,
                        &nLength ) ) == 0 )
            {
               *pMechOID = nCtr;
            }
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoGetSupportedMechType(void *hSpnegoToken, enum spnego_mech_oid *pMechOID)
{
   int nReturn = -8;
   int nCtr = 0L;
   long nLength = 0L;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pMechOID &&
         1 == pSpnegoToken->ucTokenType )
   {
      if ( pSpnegoToken->aElementArray[1].iElementPresent
            == 1 )
      {
         for ( nCtr = 0;
               nReturn != 0 &&
               g_stcMechOIDList[nCtr].eMechanismOID != spnego_mech_oid_NotUsed;
               nCtr++ )
         {
            if ( ( nReturn = ASNDerCheckOID(
                        pSpnegoToken->aElementArray[1].pbData,
                        nCtr,
                        pSpnegoToken->aElementArray[1].nDatalength,
                        &nLength ) ) == 0 )
            {
               *pMechOID = nCtr;
            }
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types: enum spnego_mech_oid { spnego_mech_oid_Kerberos_V5_Legacy=0, spnego_mech_oid_Kerberos_V5=1, spnego_mech_oid_Spnego=2, spnego_mech_oid_NotUsed=-1 } *

variable_types: nCtr int

variable_types: nLength long


spnegoGetMechToken

decompiled_code:
__int64 __fastcall spnegoGetMechToken(__int64 a1, void *a2, size_t *a3)
{
int v4; // eax
size_t v5; // rdx
__int64 result; // rax

v4 = IsValidSpnegoToken();
if ( !a3 || !v4 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 136) != 1 )
return 4294967291LL;
if ( a2 && (v5 = *(_QWORD *)(a1 + 160), v5 <= *a3) )
{
memcpy(a2, *(const void **)(a1 + 152), v5);
*a3 = *(_QWORD *)(a1 + 160);
result = 0LL;
}
else
{
*a3 = *(_QWORD *)(a1 + 160);
result = 4294967284LL;
}
return result;
}

original_code:
int spnegoGetMechToken( SPNEGO_TOKEN_HANDLE hSpnegoToken, unsigned char* pbTokenData, unsigned long* pulDataLen )
{
   int nReturn = -8;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   SPNEGO_ELEMENT* pSpnegoElement = ((void *)0);
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pulDataLen )
   {
      if ( 0 == pSpnegoToken->ucTokenType )
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[2];
      }
      else
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[2];
      }
      if ( 1 == pSpnegoElement->iElementPresent )
      {
         if ( ((void *)0) == pbTokenData ||
               pSpnegoElement->nDatalength > *pulDataLen )
         {
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = -12;
         }
         else
         {
            memcpy( pbTokenData, pSpnegoElement->pbData, pSpnegoElement->nDatalength );
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = 0;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoGetMechToken(void *hSpnegoToken, unsigned char *pbTokenData, unsigned long *pulDataLen)
{
   int nReturn = -8;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   struct SpnegoElement *pSpnegoElement = ((void *)0);
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pulDataLen )
   {
      if ( 0 == pSpnegoToken->ucTokenType )
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[2];
      }
      else
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[2];
      }
      if ( 1 == pSpnegoElement->iElementPresent )
      {
         if ( ((void *)0) == pbTokenData ||
               pSpnegoElement->nDatalength > *pulDataLen )
         {
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = -12;
         }
         else
         {
            memcpy( pbTokenData, pSpnegoElement->pbData, pSpnegoElement->nDatalength );
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = 0;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types:
pbTokenData
unsigned char *

variable_types:
pulDataLen
unsigned long *

variable_types: struct SpnegoElement { unsigned long nStructSize; int iElementPresent; enum spnego_element_type { spnego_element_min=0, spnego_init_mechtypes=1, spnego_init_reqFlags=2, spnego_init_mechToken=3, spnego_init_mechListMIC=4, spnego_targ_negResult=5, spnego_targ_supportedMech=6, spnego_targ_responseToken=7, spnego_targ_mechListMIC=8, spnego_element_max=9 } eElementType; unsigned char type; unsigned char *pbData; unsigned long nDatalength; } *


spnegoGetMechListMIC

decompiled_code:
__int64 __fastcall spnegoGetMechListMIC(__int64 a1, void *a2, size_t *a3)
{
int v4; // eax
size_t v5; // rdx
__int64 result; // rax

v4 = IsValidSpnegoToken();
if ( !a3 || !v4 )
return 4294967288LL;
if ( *(_DWORD *)(a1 + 176) != 1 )
return 4294967291LL;
if ( a2 && (v5 = *(_QWORD *)(a1 + 200), v5 <= *a3) )
{
memcpy(a2, *(const void **)(a1 + 192), v5);
*a3 = *(_QWORD *)(a1 + 200);
result = 0LL;
}
else
{
*a3 = *(_QWORD *)(a1 + 200);
result = 4294967284LL;
}
return result;
}

original_code:
int spnegoGetMechListMIC( SPNEGO_TOKEN_HANDLE hSpnegoToken, unsigned char* pbMICData, unsigned long* pulDataLen )
{
   int nReturn = -8;
   SPNEGO_TOKEN* pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   SPNEGO_ELEMENT* pSpnegoElement = ((void *)0);
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pulDataLen )
   {
      if ( 0 == pSpnegoToken->ucTokenType )
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[3];
      }
      else
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[3];
      }
      if ( 1 == pSpnegoElement->iElementPresent )
      {
         if ( ((void *)0) == pbMICData ||
               pSpnegoElement->nDatalength > *pulDataLen )
         {
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = -12;
         }
         else
         {
            memcpy( pbMICData, pSpnegoElement->pbData, pSpnegoElement->nDatalength );
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = 0;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

canonical_original_code:
int spnegoGetMechListMIC(void *hSpnegoToken, unsigned char *pbMICData, unsigned long *pulDataLen)
{
   int nReturn = -8;
   struct SpnegoToken *pSpnegoToken = (SPNEGO_TOKEN*) hSpnegoToken;
   struct SpnegoElement *pSpnegoElement = ((void *)0);
   if ( IsValidSpnegoToken( pSpnegoToken ) &&
         ((void *)0) != pulDataLen )
   {
      if ( 0 == pSpnegoToken->ucTokenType )
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[3];
      }
      else
      {
         pSpnegoElement = &pSpnegoToken->aElementArray[3];
      }
      if ( 1 == pSpnegoElement->iElementPresent )
      {
         if ( ((void *)0) == pbMICData ||
               pSpnegoElement->nDatalength > *pulDataLen )
         {
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = -12;
         }
         else
         {
            memcpy( pbMICData, pSpnegoElement->pbData, pSpnegoElement->nDatalength );
            *pulDataLen = pSpnegoElement->nDatalength;
            nReturn = 0;
         }
      }
      else
      {
         nReturn = -5;
      }
   }
   ;
   return nReturn;;
}

variable_types:
pbMICData
unsigned char *


ASNDerGetLength

original_code:
int ASNDerGetLength( unsigned char* pbLengthData, long nBoundaryLength, long* pnLength,
                     long* pnNumLengthBytes )
{
   int nReturn = -2;
   int nNumLengthBytes = 0;
   if ( *pbLengthData & 0x80 )
   {
      nNumLengthBytes = *pbLengthData & 0x7f;
      if ( nNumLengthBytes <= nBoundaryLength - 1 )
      {
         if ( nNumLengthBytes >= 1 && nNumLengthBytes <= 4 )
         {
            *pnLength = 0L;
            pbLengthData++;
            switch ( nNumLengthBytes )
            {
               case 1:
               {
                  *( ( (unsigned char*) pnLength ) ) = *pbLengthData;
                  break;
               }
               case 2:
               {
                  *( ( (unsigned char*) pnLength ) ) = *(pbLengthData + 1);
                  *( ( (unsigned char*) pnLength ) + 1 ) = *(pbLengthData);
                  break;
               }
               case 3:
               {
                  *( ( (unsigned char*) pnLength ) ) = *(pbLengthData + 2);
                  *( ( (unsigned char*) pnLength ) + 2 ) = *(pbLengthData + 1);
                  *( ( (unsigned char*) pnLength ) + 3 ) = *(pbLengthData);
                  break;
               }
               case 4:
               {
                  *( ( (unsigned char*) pnLength ) ) = *(pbLengthData + 3);
                  *( ( (unsigned char*) pnLength ) + 1 ) = *(pbLengthData + 2);
                  *( ( (unsigned char*) pnLength ) + 2 ) = *(pbLengthData + 1);
                  *( ( (unsigned char*) pnLength ) + 3 ) = *(pbLengthData);
                  break;
               }
            }
            *pnNumLengthBytes = nNumLengthBytes + 1;
            nReturn = 0;
         }
      }
   }
   else
   {
      *pnLength = *pbLengthData & 0x7f;
      *pnNumLengthBytes = 1;
      nReturn = 0;
   }
   ;
   return nReturn;
}

canonical_original_code:
int ASNDerGetLength(unsigned char *pbLengthData, long nBoundaryLength, long *pnLength, long *pnNumLengthBytes)
{
   int nReturn = -2;
   int nNumLengthBytes = 0;
   if ( *pbLengthData & 0x80 )
   {
      nNumLengthBytes = *pbLengthData & 0x7f;
      if ( nNumLengthBytes <= nBoundaryLength - 1 )
      {
         if ( nNumLengthBytes >= 1 && nNumLengthBytes <= 4 )
         {
            *pnLength = 0L;
            pbLengthData++;
            switch ( nNumLengthBytes )
            {
               case 1:
               {
                  *( ( (unsigned char *) pnLength ) ) = *pbLengthData;
                  break;
               }
               case 2:
               {
                  *( ( (unsigned char *) pnLength ) ) = *(pbLengthData + 1);
                  *( ( (unsigned char *) pnLength ) + 1 ) = *(pbLengthData);
                  break;
               }
               case 3:
               {
                  *( ( (unsigned char *) pnLength ) ) = *(pbLengthData + 2);
                  *( ( (unsigned char *) pnLength ) + 2 ) = *(pbLengthData + 1);
                  *( ( (unsigned char *) pnLength ) + 3 ) = *(pbLengthData);
                  break;
               }
               case 4:
               {
                  *( ( (unsigned char *) pnLength ) ) = *(pbLengthData + 3);
                  *( ( (unsigned char *) pnLength ) + 1 ) = *(pbLengthData + 2);
                  *( ( (unsigned char *) pnLength ) + 2 ) = *(pbLengthData + 1);
                  *( ( (unsigned char *) pnLength ) + 3 ) = *(pbLengthData);
                  break;
               }
            }
            *pnNumLengthBytes = nNumLengthBytes + 1;
            nReturn = 0;
         }
      }
   }
   else
   {
      *pnLength = *pbLengthData & 0x7f;
      *pnNumLengthBytes = 1;
      nReturn = 0;
   }
   ;
   return nReturn;
}

variable_types:
pbLengthData
unsigned char *

variable_types: nBoundaryLength long

variable_types:
pnLength
long *

variable_types:
pnNumLengthBytes
long *

variable_types: nNumLengthBytes int


ASNDerCheckToken

original_code:
int ASNDerCheckToken( unsigned char* pbTokenData, unsigned char nToken,
                        long nLengthWithToken, long nBoundaryLength,
                        long* pnLength, long* pnTokenLength )
{
   int nReturn = -2;
   long nNumLengthBytes = 0L;
   if ( nBoundaryLength >= 2 )
   {
      if ( *pbTokenData == nToken )
      {
         pbTokenData++;
         if ( ( nReturn = ASNDerGetLength( pbTokenData, nBoundaryLength, pnLength,
                                             &nNumLengthBytes ) ) == 0 )
         {
            if ( ( nBoundaryLength - ( nNumLengthBytes + 1 ) < *pnLength ) )
            {
               nReturn = -2;
            }
            if ( nLengthWithToken > 0L )
            {
               if ( ( nLengthWithToken - ( nNumLengthBytes + 1 ) ) != *pnLength )
               {
                  nReturn = -2;
               }
            }
            if ( 0 == nReturn )
            {
               *pnTokenLength = nNumLengthBytes + 1;
            }
         }
      }
      else
      {
         nReturn = -10;
      }
   }
   ;
   return nReturn;
}

canonical_original_code:
int ASNDerCheckToken(unsigned char *pbTokenData, unsigned char nToken, long nLengthWithToken, long nBoundaryLength, long *pnLength, long *pnTokenLength)
{
   int nReturn = -2;
   long nNumLengthBytes = 0L;
   if ( nBoundaryLength >= 2 )
   {
      if ( *pbTokenData == nToken )
      {
         pbTokenData++;
         if ( ( nReturn = ASNDerGetLength( pbTokenData, nBoundaryLength, pnLength,
                                             &nNumLengthBytes ) ) == 0 )
         {
            if ( ( nBoundaryLength - ( nNumLengthBytes + 1 ) < *pnLength ) )
            {
               nReturn = -2;
            }
            if ( nLengthWithToken > 0L )
            {
               if ( ( nLengthWithToken - ( nNumLengthBytes + 1 ) ) != *pnLength )
               {
                  nReturn = -2;
               }
            }
            if ( 0 == nReturn )
            {
               *pnTokenLength = nNumLengthBytes + 1;
            }
         }
      }
      else
      {
         nReturn = -10;
      }
   }
   ;
   return nReturn;
}

variable_types:
nToken
unsigned char

variable_types: nLengthWithToken long

variable_types:
pnTokenLength
long *

variable_types: nNumLengthBytes long


ASNDerCheckOID

decompiled_code:
int __fastcall ASNDerCheckOID(void *s1, int a2, __int64 a3, __int64 *a4)
{
int result; // eax
size_t v6; // rdx
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v7[0] = 0LL;
result = ASNDerCheckToken(s1, 6, 0LL, a3, v7, a4);
if ( !result )
{
v6 = *a4 + v7[0];
*a4 = v6;
if ( v6 == *((_DWORD *)&g_stcMechOIDList + 6 * a2 + 2) )
{
result = memcmp(s1, *(&g_stcMechOIDList + 3 * a2), v6);
if ( result )
result = -9;
}
else
{
result = -9;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *s1, int a2, __int64 a3, __int64 *a4)
{
int result; // eax
size_t v6; // rdx
__int64 v7[4]; // [rsp+8h] [rbp-20h] BYREF

v7[0] = 0LL;
result = func1(s1, 6, 0LL, a3, v7, a4);
if ( !result )
{
v6 = *a4 + v7[0];
*a4 = v6;
if ( v6 == *((_DWORD *)&g_stcMechOIDList + 6 * a2 + 2) )
{
result = memcmp(s1, *(&g_stcMechOIDList + 3 * a2), v6);
if ( result )
result = -9;
}
else
{
result = -9;
}
}
return result;
}

original_code:
int ASNDerCheckOID( unsigned char* pbTokenData, SPNEGO_MECH_OID nMechOID, long nBoundaryLength,
                     long* pnTokenLength )
{
   int nReturn = 0L;
   long nLength = 0L;
   if ( ( nReturn = ASNDerCheckToken( pbTokenData, 0x06, 0L, nBoundaryLength,
                                       &nLength, pnTokenLength ) ) == 0 )
   {
      *pnTokenLength += nLength;
      if ( *pnTokenLength == g_stcMechOIDList[nMechOID].iLen )
      {
         if ( memcmp( pbTokenData, g_stcMechOIDList[nMechOID].ucOid, *pnTokenLength ) != 0 )
         {
        ;
            nReturn = -9;
         }
      }
      else
      {
         ;
         nReturn = -9;
      }
   }
   ;
   return nReturn;
}

canonical_original_code:
int ASNDerCheckOID(unsigned char *pbTokenData, enum spnego_mech_oid nMechOID, long nBoundaryLength, long *pnTokenLength)
{
   int nReturn = 0L;
   long nLength = 0L;
   if ( ( nReturn = ASNDerCheckToken( pbTokenData, 0x06, 0L, nBoundaryLength,
                                       &nLength, pnTokenLength ) ) == 0 )
   {
      *pnTokenLength += nLength;
      if ( *pnTokenLength == g_stcMechOIDList[nMechOID].iLen )
      {
         if ( memcmp( pbTokenData, g_stcMechOIDList[nMechOID].ucOid, *pnTokenLength ) != 0 )
         {
        ;
            nReturn = -9;
         }
      }
      else
      {
         ;
         nReturn = -9;
      }
   }
   ;
   return nReturn;
}

variable_types: enum spnego_mech_oid { spnego_mech_oid_Kerberos_V5_Legacy=0, spnego_mech_oid_Kerberos_V5=1, spnego_mech_oid_Spnego=2, spnego_mech_oid_NotUsed=-1 }

variable_types: nReturn int


ASNDerCalcNumLengthBytes

decompiled_code:
__int64 __fastcall ASNDerCalcNumLengthBytes(__int64 a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 255 )
{
result = 3LL;
if ( a1 > 0xFFFF )
result = (unsigned int)(a1 > 0xFFFFFF) + 4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax

result = 1LL;
if ( a1 > 127 )
{
result = 2LL;
if ( a1 > 255 )
{
result = 3LL;
if ( a1 > 0xFFFF )
result = (unsigned int)(a1 > 0xFFFFFF) + 4;
}
}
return result;
}

original_code:
int ASNDerCalcNumLengthBytes( long nLength )
{
      if ( nLength <= 0x7F )
      {
         return 1;
      }
      else if ( nLength <= 0xFF )
      {
         return 2;
      }
      else if ( nLength <= 0xFFFF )
      {
         return 3;
      }
      else if ( nLength <= 0xFFFFFF )
      {
         return 4;
      }
      else
      {
         return 5;
      }
}

canonical_original_code:
int ASNDerCalcNumLengthBytes(long nLength)
{
      if ( nLength <= 0x7F )
      {
         return 1;
      }
      else if ( nLength <= 0xFF )
      {
         return 2;
      }
      else if ( nLength <= 0xFFFF )
      {
         return 3;
      }
      else if ( nLength <= 0xFFFFFF )
      {
         return 4;
      }
      else
      {
         return 5;
      }
}


ASNDerCalcTokenLength

decompiled_code:
__int64 __fastcall ASNDerCalcTokenLength(__int64 a1, __int64 a2)
{
return a2 + (int)(ASNDerCalcNumLengthBytes(a1) + 1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
return a2 + (int)(func3(a1) + 1);
}

original_code:
long ASNDerCalcTokenLength( long nLength, long nDataLength )
{
   long nTotalLength = ASNDerCalcNumLengthBytes( nLength ) + 1;
   return nTotalLength + nDataLength;
}

canonical_original_code:
long ASNDerCalcTokenLength(long nLength, long nDataLength)
{
   long nTotalLength = ASNDerCalcNumLengthBytes( nLength ) + 1;
   return nTotalLength + nDataLength;
}

variable_types: nDataLength long

variable_types: nTotalLength long

return_type: long


ASNDerCalcElementLength

decompiled_code:
__int64 __fastcall ASNDerCalcElementLength(__int64 a1, __int64 *a2)
{
__int64 v2; // rax

v2 = ASNDerCalcTokenLength(a1, a1);
if ( a2 )
*a2 = v2;
return v2 + ASNDerCalcTokenLength(v2, 0LL);
}

original_code:
long ASNDerCalcElementLength( long nDataLength, long* pnInternalLength )
{
   long nTotalLength = ASNDerCalcTokenLength( nDataLength, nDataLength );
   if ( ((void *)0) != pnInternalLength )
   {
      *pnInternalLength = nTotalLength;
   }
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   return nTotalLength;
}

canonical_original_code:
long ASNDerCalcElementLength(long nDataLength, long *pnInternalLength)
{
   long nTotalLength = ASNDerCalcTokenLength( nDataLength, nDataLength );
   if ( ((void *)0) != pnInternalLength )
   {
      *pnInternalLength = nTotalLength;
   }
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   return nTotalLength;
}

variable_types:
pnInternalLength
long *


ASNDerCalcMechListLength

decompiled_code:
__int64 __fastcall ASNDerCalcMechListLength(int a1, __int64 *a2)
{
__int64 v2; // rbx
__int64 v3; // rbx

v2 = *((int *)&g_stcMechOIDList + 6 * a1 + 2);
v3 = ASNDerCalcTokenLength(v2, 0LL) + v2;
if ( a2 )
*a2 = v3;
return v3 + ASNDerCalcTokenLength(v3, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 *a2)
{
__int64 v2; // rbx
__int64 v3; // rbx

v2 = *((int *)&g_stcMechOIDList + 6 * a1 + 2);
v3 = func4(v2, 0LL) + v2;
if ( a2 )
*a2 = v3;
return v3 + func4(v3, 0LL);
}

original_code:
long ASNDerCalcMechListLength( SPNEGO_MECH_OID mechoid, long* pnInternalLength )
{
   long nTotalLength = g_stcMechOIDList[mechoid].iLen;
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   if ( ((void *)0) != pnInternalLength )
   {
      *pnInternalLength = nTotalLength;
   }
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   return nTotalLength;
}

canonical_original_code:
long ASNDerCalcMechListLength(enum spnego_mech_oid mechoid, long *pnInternalLength)
{
   long nTotalLength = g_stcMechOIDList[mechoid].iLen;
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   if ( ((void *)0) != pnInternalLength )
   {
      *pnInternalLength = nTotalLength;
   }
   nTotalLength += ASNDerCalcTokenLength( nTotalLength, 0L );
   return nTotalLength;
}

user_defined_types: enum spnego_mech_oid { spnego_mech_oid_Kerberos_V5_Legacy=0, spnego_mech_oid_Kerberos_V5=1, spnego_mech_oid_Spnego=2, spnego_mech_oid_NotUsed=-1 }


ASNDerWriteLength

decompiled_code:
__int64 __fastcall ASNDerWriteLength(_BYTE *a1, __int64 a2)
{
__int64 result; // rax

result = ASNDerCalcNumLengthBytes(a2);
if ( (int)result <= 1 )
{
*a1 = a2;
}
else
{
*a1 = (result - 1) | 0x80;
if ( (_DWORD)result == 4 )
{
a1[1] = BYTE3(a2);
a1[2] = BYTE2(a2);
a1[3] = a2;
}
else if ( (int)result - 1 > 3 )
{
if ( (_DWORD)result == 5 )
{
a1[1] = BYTE3(a2);
a1[2] = BYTE2(a2);
a1[3] = BYTE1(a2);
a1[4] = a2;
}
}
else if ( (_DWORD)result == 2 )
{
a1[1] = a2;
}
else if ( (_DWORD)result == 3 )
{
a1[1] = BYTE1(a2);
a1[2] = a2;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_BYTE *a1, __int64 a2)
{
__int64 result; // rax

result = func3(a2);
if ( (int)result <= 1 )
{
*a1 = a2;
}
else
{
*a1 = (result - 1) | 0x80;
if ( (_DWORD)result == 4 )
{
a1[1] = BYTE3(a2);
a1[2] = BYTE2(a2);
a1[3] = a2;
}
else if ( (int)result - 1 > 3 )
{
if ( (_DWORD)result == 5 )
{
a1[1] = BYTE3(a2);
a1[2] = BYTE2(a2);
a1[3] = BYTE1(a2);
a1[4] = a2;
}
}
else if ( (_DWORD)result == 2 )
{
a1[1] = a2;
}
else if ( (_DWORD)result == 3 )
{
a1[1] = BYTE1(a2);
a1[2] = a2;
}
}
return result;
}

original_code:
int ASNDerWriteLength( unsigned char* pbData, long nLength )
{
   int nNumBytesRequired = ASNDerCalcNumLengthBytes( nLength );
   int nNumLengthBytes = nNumBytesRequired - 1;
   if ( nNumBytesRequired > 1 )
   {
      *pbData = (unsigned char ) ( 0x80 | nNumLengthBytes );
      pbData++;
      switch ( nNumLengthBytes )
      {
         case 1:
         {
            *pbData = (unsigned char) nLength;
            break;
         }
         case 2:
         {
            *pbData = *( ( (unsigned char*) &nLength ) + 1 );
            *( pbData + 1) = *( ( (unsigned char*) &nLength ) );
            break;
         }
         case 3:
         {
            *pbData = *( ( (unsigned char*) &nLength ) + 3 );
            *( pbData + 1) = *( ( (unsigned char*) &nLength ) + 2 );
            *( pbData + 2) = *( ( (unsigned char*) &nLength ) );
            break;
         }
         case 4:
         {
            *pbData = *( ( (unsigned char*) &nLength ) + 3 );
            *( pbData + 1) = *( ( (unsigned char*) &nLength ) + 2 );
            *( pbData + 2) = *( ( (unsigned char*) &nLength ) + 1 );
            *( pbData + 3) = *( ( (unsigned char*) &nLength ) );
            break;
         }
      }
   }
   else
   {
      *pbData = (unsigned char) nLength;
   }
   return nNumBytesRequired;
}

canonical_original_code:
int ASNDerWriteLength(unsigned char *pbData, long nLength)
{
   int nNumBytesRequired = ASNDerCalcNumLengthBytes( nLength );
   int nNumLengthBytes = nNumBytesRequired - 1;
   if ( nNumBytesRequired > 1 )
   {
      *pbData = (unsigned char ) ( 0x80 | nNumLengthBytes );
      pbData++;
      switch ( nNumLengthBytes )
      {
         case 1:
         {
            *pbData = (unsigned char) nLength;
            break;
         }
         case 2:
         {
            *pbData = *( ( (unsigned char *) &nLength ) + 1 );
            *( pbData + 1) = *( ( (unsigned char *) &nLength ) );
            break;
         }
         case 3:
         {
            *pbData = *( ( (unsigned char *) &nLength ) + 3 );
            *( pbData + 1) = *( ( (unsigned char *) &nLength ) + 2 );
            *( pbData + 2) = *( ( (unsigned char *) &nLength ) );
            break;
         }
         case 4:
         {
            *pbData = *( ( (unsigned char *) &nLength ) + 3 );
            *( pbData + 1) = *( ( (unsigned char *) &nLength ) + 2 );
            *( pbData + 2) = *( ( (unsigned char *) &nLength ) + 1 );
            *( pbData + 3) = *( ( (unsigned char *) &nLength ) );
            break;
         }
      }
   }
   else
   {
      *pbData = (unsigned char) nLength;
   }
   return nNumBytesRequired;
}

variable_types:
pbData
unsigned char *

variable_types: nLength long

variable_types: nNumBytesRequired int


ASNDerWriteToken

decompiled_code:
__int64 __fastcall ASNDerWriteToken(_BYTE *a1, char a2, const void *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // er13

*a1 = a2;
v6 = ASNDerWriteLength(a1 + 1, a4);
v7 = v6 + 1;
if ( a3 )
{
memcpy(&a1[v6 + 1], a3, a4);
v7 += a4;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func8(_BYTE *a1, char a2, const void *a3, size_t a4)
{
int v6; // eax
unsigned int v7; // er13

*a1 = a2;
v6 = func7(a1 + 1, a4);
v7 = v6 + 1;
if ( a3 )
{
memcpy(&a1[v6 + 1], a3, a4);
v7 += a4;
}
return v7;
}

original_code:
int ASNDerWriteToken( unsigned char* pbData, unsigned char ucType,
                     unsigned char* pbTokenValue, long nLength )
{
   int nTotalBytesWrittenOut = 0L;
   int nNumLengthBytesWritten = 0L;
   *pbData = ucType;
   nTotalBytesWrittenOut++;
   pbData++;
   nNumLengthBytesWritten = ASNDerWriteLength( pbData, nLength );
   nTotalBytesWrittenOut += nNumLengthBytesWritten;
   pbData += nNumLengthBytesWritten;
   if ( ((void *)0) != pbTokenValue )
   {
      memcpy( pbData, pbTokenValue, nLength );
      nTotalBytesWrittenOut += nLength;
   }
   return nTotalBytesWrittenOut;
}

canonical_original_code:
int ASNDerWriteToken(unsigned char *pbData, unsigned char ucType, unsigned char *pbTokenValue, long nLength)
{
   int nTotalBytesWrittenOut = 0L;
   int nNumLengthBytesWritten = 0L;
   *pbData = ucType;
   nTotalBytesWrittenOut++;
   pbData++;
   nNumLengthBytesWritten = ASNDerWriteLength( pbData, nLength );
   nTotalBytesWrittenOut += nNumLengthBytesWritten;
   pbData += nNumLengthBytesWritten;
   if ( ((void *)0) != pbTokenValue )
   {
      memcpy( pbData, pbTokenValue, nLength );
      nTotalBytesWrittenOut += nLength;
   }
   return nTotalBytesWrittenOut;
}

variable_types:
ucType
unsigned char

variable_types:
pbTokenValue
unsigned char *

variable_types: nTotalBytesWrittenOut int

variable_types: nNumLengthBytesWritten int


ASNDerWriteOID

decompiled_code:
__int64 __fastcall ASNDerWriteOID(void *a1, int a2)
{
memcpy(a1, *(&g_stcMechOIDList + 3 * a2), *((int *)&g_stcMechOIDList + 6 * a2 + 2));
return *((unsigned int *)&g_stcMechOIDList + 6 * a2 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func9(void *a1, int a2)
{
memcpy(a1, *(&g_stcMechOIDList + 3 * a2), *((int *)&g_stcMechOIDList + 6 * a2 + 2));
return *((unsigned int *)&g_stcMechOIDList + 6 * a2 + 2);
}

original_code:
int ASNDerWriteOID( unsigned char* pbData, SPNEGO_MECH_OID eMechOID )
{
   memcpy( pbData, g_stcMechOIDList[eMechOID].ucOid, g_stcMechOIDList[eMechOID].iLen );
   return g_stcMechOIDList[eMechOID].iLen;
}

canonical_original_code:
int ASNDerWriteOID(unsigned char *pbData, enum spnego_mech_oid eMechOID)
{
   memcpy( pbData, g_stcMechOIDList[eMechOID].ucOid, g_stcMechOIDList[eMechOID].iLen );
   return g_stcMechOIDList[eMechOID].iLen;
}


ASNDerWriteMechList

decompiled_code:
__int64 __fastcall ASNDerWriteMechList(_BYTE *a1, int a2)
{
__int64 v2; // r12
int v3; // eax
size_t v5[4]; // [rsp+8h] [rbp-20h] BYREF

v5[0] = 0LL;
v2 = ASNDerCalcMechListLength(a2, (__int64 *)v5);
v3 = ASNDerWriteToken(a1, 160, 0LL, v5[0]);
ASNDerWriteToken(&a1[v3], 48, *(&g_stcMechOIDList + 3 * a2), *((int *)&g_stcMechOIDList + 6 * a2 + 2));
return v2;
}

original_code:
long ASNDerWriteMechList( unsigned char* pbData, SPNEGO_MECH_OID mechoid )
{
   long nInternalLength = 0L;
   long nMechListLength = ASNDerCalcMechListLength( mechoid, &nInternalLength );
   long nTempLength = 0L;
   nTempLength = ASNDerWriteToken( pbData, ( 0x80 | 0x20 | 0x0 ),
                                    ((void *)0), nInternalLength );
   pbData += nTempLength;
   nTempLength = ASNDerWriteToken( pbData, ( 0x10 | 0x20 ),
                                    g_stcMechOIDList[mechoid].ucOid,
                                    g_stcMechOIDList[mechoid].iLen );
   return nMechListLength;
}

canonical_original_code:
long ASNDerWriteMechList(unsigned char *pbData, enum spnego_mech_oid mechoid)
{
   long nInternalLength = 0L;
   long nMechListLength = ASNDerCalcMechListLength( mechoid, &nInternalLength );
   long nTempLength = 0L;
   nTempLength = ASNDerWriteToken( pbData, ( 0x80 | 0x20 | 0x0 ),
                                    ((void *)0), nInternalLength );
   pbData += nTempLength;
   nTempLength = ASNDerWriteToken( pbData, ( 0x10 | 0x20 ),
                                    g_stcMechOIDList[mechoid].ucOid,
                                    g_stcMechOIDList[mechoid].iLen );
   return nMechListLength;
}

variable_types: nInternalLength long

variable_types: nMechListLength long

variable_types: nTempLength long


ASNDerWriteElement

decompiled_code:
__int64 __fastcall ASNDerWriteElement(_BYTE *a1, char a2, char a3, const void *a4, size_t a5)
{
unsigned int v8; // er14
int v9; // eax
size_t v11[8]; // [rsp+8h] [rbp-40h] BYREF

v11[0] = 0LL;
v8 = ASNDerCalcElementLength(a5, (__int64 *)v11);
v9 = ASNDerWriteToken(a1, a2, 0LL, v11[0]);
ASNDerWriteToken(&a1[v9], a3, a4, a5);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1, char a2, char a3, const void *a4, size_t a5)
{
unsigned int v8; // er14
int v9; // eax
size_t v11[8]; // [rsp+8h] [rbp-40h] BYREF

v11[0] = 0LL;
v8 = func5(a5, (__int64 *)v11);
v9 = func8(a1, a2, 0LL, v11[0]);
func8(&a1[v9], a3, a4, a5);
return v8;
}

original_code:
int ASNDerWriteElement( unsigned char* pbData, unsigned char ucElementSequence,
                        unsigned char ucType, unsigned char* pbTokenValue, long nLength )
{
   long nInternalLength = 0L;
   long nElementLength = ASNDerCalcElementLength( nLength, &nInternalLength );
   long nTempLength = 0L;
   nTempLength = ASNDerWriteToken( pbData, ucElementSequence, ((void *)0), nInternalLength );
   pbData += nTempLength;
   nTempLength = ASNDerWriteToken( pbData, ucType, pbTokenValue, nLength );
   return nElementLength;
}

canonical_original_code:
int ASNDerWriteElement(unsigned char *pbData, unsigned char ucElementSequence, unsigned char ucType, unsigned char *pbTokenValue, long nLength)
{
   long nInternalLength = 0L;
   long nElementLength = ASNDerCalcElementLength( nLength, &nInternalLength );
   long nTempLength = 0L;
   nTempLength = ASNDerWriteToken( pbData, ucElementSequence, ((void *)0), nInternalLength );
   pbData += nTempLength;
   nTempLength = ASNDerWriteToken( pbData, ucType, pbTokenValue, nLength );
   return nElementLength;
}

variable_types:
ucElementSequence
unsigned char

variable_types: nElementLength long


makeNegTokenTarg

decompiled_code:
__int64 __fastcall makeNegTokenTarg(__int64 a1, __int64 a2, void **a3, size_t *a4)
{
int v6; // eax
unsigned int v7; // ebx
int v8; // eax
void *v9; // rsi
int v10; // eax
__int64 v12[4]; // [rsp+8h] [rbp-20h] BYREF

v12[0] = 0LL;
if ( a4 == 0LL || a3 == 0LL || !a1 )
return 10;
v6 = spnegoCreateNegTokenTarg(0LL, 0LL, a1, a2, 0LL, 0LL, v12);
if ( v6 )
{
v7 = abs32(v6) + 100;
}
else
{
v8 = spnegoTokenGetBinary(v12[0], 0LL, a4);
if ( v8 == -12 )
{
v9 = malloc(*a4);
*a3 = v9;
if ( v9 )
{
v10 = spnegoTokenGetBinary(v12[0], v9, a4);
v7 = v10;
if ( v10 )
{
v7 = abs32(v10) + 400;
if ( *a3 )
{
free(*a3);
*a3 = 0LL;
*a4 = 0LL;
}
}
}
else
{
v7 = 312;
}
}
else
{
v7 = abs32(v8) + 200;
}
}
if ( v12[0] )
spnegoFreeData();
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, void **a3, size_t *a4)
{
int v6; // eax
unsigned int v7; // ebx
int v8; // eax
void *v9; // rsi
int v10; // eax
__int64 v12[4]; // [rsp+8h] [rbp-20h] BYREF

v12[0] = 0LL;
if ( a4 == 0LL || a3 == 0LL || !a1 )
return 10;
v6 = spnegoCreateNegTokenTarg(0LL, 0LL, a1, a2, 0LL, 0LL, v12);
if ( v6 )
{
v7 = abs32(v6) + 100;
}
else
{
v8 = spnegoTokenGetBinary(v12[0], 0LL, a4);
if ( v8 == -12 )
{
v9 = malloc(*a4);
*a3 = v9;
if ( v9 )
{
v10 = spnegoTokenGetBinary(v12[0], v9, a4);
v7 = v10;
if ( v10 )
{
v7 = abs32(v10) + 400;
if ( *a3 )
{
free(*a3);
*a3 = 0LL;
*a4 = 0LL;
}
}
}
else
{
v7 = 312;
}
}
else
{
v7 = abs32(v8) + 200;
}
}
if ( v12[0] )
spnegoFreeData();
return v7;
}

original_code:
int makeNegTokenTarg (const unsigned char * kerberosToken,
                      size_t kerberosTokenLength,
                      const unsigned char ** negTokenTarg,
                      size_t * negTokenTargLength)
{
    SPNEGO_TOKEN_HANDLE hSpnegoToken = ((void *)0);
    int rc1 = 1;
    int rc2 = 0;
    if (!kerberosToken ||
        !negTokenTarg ||
        !negTokenTargLength)
        return 10;
    rc2 = spnegoCreateNegTokenTarg (spnego_mech_oid_Kerberos_V5_Legacy,
                                    spnego_negresult_success,
                                    (unsigned char *) kerberosToken,
                                    kerberosTokenLength,
                                    ((void *)0),
                                    0,
                                    &hSpnegoToken);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+100;
        goto cleanup;
    }
    rc2 = spnegoTokenGetBinary (hSpnegoToken,
                                ((void *)0),
                                (unsigned long*) negTokenTargLength);
    if (rc2 != -12)
    {
        rc1 = abs(rc2)+200;
        goto cleanup;
    }
    *negTokenTarg = malloc (*negTokenTargLength);
    if (!*negTokenTarg)
    {
        rc1 = abs(rc2)+300;
        goto cleanup;
    }
    rc2 = spnegoTokenGetBinary (hSpnegoToken,
                              (unsigned char *) *negTokenTarg,
                              (unsigned long*) negTokenTargLength);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+400;
        goto error;
    }
    rc1 = 0;
    goto cleanup;
error:
    if (*negTokenTarg)
    {
        free ((unsigned char *) *negTokenTarg);
        *negTokenTarg = ((void *)0);
        *negTokenTargLength = 0;
    }
cleanup:
    if (hSpnegoToken)
        spnegoFreeData (hSpnegoToken);
    ;
    return rc1;
}

canonical_original_code:
int makeNegTokenTarg(unsigned char *kerberosToken, unsigned long kerberosTokenLength, unsigned char **negTokenTarg, unsigned long *negTokenTargLength)
{
    void *hSpnegoToken = ((void *)0);
    int rc1 = 1;
    int rc2 = 0;
    if (!kerberosToken ||
        !negTokenTarg ||
        !negTokenTargLength)
        return 10;
    rc2 = spnegoCreateNegTokenTarg (spnego_mech_oid_Kerberos_V5_Legacy,
                                    spnego_negresult_success,
                                    (unsigned char *) kerberosToken,
                                    kerberosTokenLength,
                                    ((void *)0),
                                    0,
                                    &hSpnegoToken);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+100;
        goto cleanup;
    }
    rc2 = spnegoTokenGetBinary (hSpnegoToken,
                                ((void *)0),
                                (unsigned long *) negTokenTargLength);
    if (rc2 != -12)
    {
        rc1 = abs(rc2)+200;
        goto cleanup;
    }
    *negTokenTarg = malloc (*negTokenTargLength);
    if (!*negTokenTarg)
    {
        rc1 = abs(rc2)+300;
        goto cleanup;
    }
    rc2 = spnegoTokenGetBinary (hSpnegoToken,
                              (unsigned char *) *negTokenTarg,
                              (unsigned long *) negTokenTargLength);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+400;
        goto error;
    }
    rc1 = 0;
    goto cleanup;
error:
    if (*negTokenTarg)
    {
        free ((unsigned char *) *negTokenTarg);
        *negTokenTarg = ((void *)0);
        *negTokenTargLength = 0;
    }
cleanup:
    if (hSpnegoToken)
        spnegoFreeData (hSpnegoToken);
    ;
    return rc1;
}

variable_types:
kerberosToken
unsigned char *

variable_types:
kerberosTokenLength
unsigned long

variable_types:
negTokenTarg
unsigned char **

variable_types:
negTokenTargLength
unsigned long *

variable_types:
hSpnegoToken
void *


parseNegTokenInit

decompiled_code:
__int64 __fastcall parseNegTokenInit(__int64 a1, __int64 a2, void **a3, size_t *a4)
{
int v6; // eax
unsigned int v7; // ebx
int v8; // eax
int v9; // eax
int v10; // eax
int v11; // eax
void *v12; // rsi
int v13; // eax
int v15; // [rsp+Ch] [rbp-2Ch] BYREF
char v16; // [rsp+13h] [rbp-25h] BYREF
int v17; // [rsp+14h] [rbp-24h] BYREF
__int64 v18[4]; // [rsp+18h] [rbp-20h] BYREF

v18[0] = 0LL;
v17 = -1;
v16 = 0;
v15 = 0;
if ( a4 == 0LL || a3 == 0LL || !a1 )
return 10;
v6 = spnegoInitFromBinary(a1, a2, v18);
if ( v6 )
{
v7 = abs32(v6) + 100;
}
else
{
v8 = spnegoGetTokenType(v18[0], &v15);
if ( v8 )
{
v7 = abs32(v8) + 200;
}
else
{
v7 = 300;
if ( !v15 )
{
v9 = spnegoIsMechTypeAvailable(v18[0], 0LL, &v17);
if ( v17 | v9 && (v10 = spnegoIsMechTypeAvailable(v18[0], 1LL, &v17), v17 | v10) )
{
v7 = abs32(v10) + 400;
}
else
{
v7 = 500;
if ( (unsigned int)spnegoGetContextFlags(v18[0], &v16) )
{
v11 = spnegoGetMechToken(v18[0], 0LL, a4);
if ( v11 == -12 )
{
v12 = malloc(*a4);
*a3 = v12;
if ( v12 )
{
v13 = spnegoGetMechToken(v18[0], v12, a4);
v7 = v13;
if ( v13 )
{
v7 = abs32(v13) + 800;
if ( *a3 )
{
free(*a3);
*a3 = 0LL;
*a4 = 0LL;
}
}
}
else
{
v7 = 712;
}
}
else
{
v7 = abs32(v11) + 600;
}
}
}
}
}
}
if ( v18[0] )
spnegoFreeData();
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, void **a3, size_t *a4)
{
int v6; // eax
unsigned int v7; // ebx
int v8; // eax
int v9; // eax
int v10; // eax
int v11; // eax
void *v12; // rsi
int v13; // eax
int v15; // [rsp+Ch] [rbp-2Ch] BYREF
char v16; // [rsp+13h] [rbp-25h] BYREF
int v17; // [rsp+14h] [rbp-24h] BYREF
__int64 v18[4]; // [rsp+18h] [rbp-20h] BYREF

v18[0] = 0LL;
v17 = -1;
v16 = 0;
v15 = 0;
if ( a4 == 0LL || a3 == 0LL || !a1 )
return 10;
v6 = spnegoInitFromBinary(a1, a2, v18);
if ( v6 )
{
v7 = abs32(v6) + 100;
}
else
{
v8 = spnegoGetTokenType(v18[0], &v15);
if ( v8 )
{
v7 = abs32(v8) + 200;
}
else
{
v7 = 300;
if ( !v15 )
{
v9 = spnegoIsMechTypeAvailable(v18[0], 0LL, &v17);
if ( v17 | v9 && (v10 = spnegoIsMechTypeAvailable(v18[0], 1LL, &v17), v17 | v10) )
{
v7 = abs32(v10) + 400;
}
else
{
v7 = 500;
if ( (unsigned int)spnegoGetContextFlags(v18[0], &v16) )
{
v11 = spnegoGetMechToken(v18[0], 0LL, a4);
if ( v11 == -12 )
{
v12 = malloc(*a4);
*a3 = v12;
if ( v12 )
{
v13 = spnegoGetMechToken(v18[0], v12, a4);
v7 = v13;
if ( v13 )
{
v7 = abs32(v13) + 800;
if ( *a3 )
{
free(*a3);
*a3 = 0LL;
*a4 = 0LL;
}
}
}
else
{
v7 = 712;
}
}
else
{
v7 = abs32(v11) + 600;
}
}
}
}
}
}
if ( v18[0] )
spnegoFreeData();
return v7;
}

original_code:
int parseNegTokenInit (const unsigned char * negTokenInit,
                       size_t negTokenInitLength,
                       const unsigned char ** kerberosToken,
                       size_t * kerberosTokenLength)
{
    SPNEGO_TOKEN_HANDLE hSpnegoToken = ((void *)0);
    int pindex = -1;
    int rc1 = 1;
    int rc2 = 0;
    unsigned char reqFlags = 0;
    int tokenType = 0;
    if (!negTokenInit ||
        !kerberosToken ||
        !kerberosTokenLength)
        return 10;
    rc2 = spnegoInitFromBinary ((unsigned char *) negTokenInit,
                                negTokenInitLength,
                                &hSpnegoToken);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+100;
        goto cleanup;
    }
    rc2 = spnegoGetTokenType (hSpnegoToken,
                              &tokenType);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+200;
        goto cleanup;
    }
    if (tokenType != 0)
    {
        rc1 = abs(rc2)+300;
        goto cleanup;
    }
    rc2 = spnegoIsMechTypeAvailable (hSpnegoToken,
                                     spnego_mech_oid_Kerberos_V5_Legacy,
                                     &pindex);
    if (rc2 != 0 ||
        pindex != 0)
    {
        rc2 = spnegoIsMechTypeAvailable (hSpnegoToken,
                                         spnego_mech_oid_Kerberos_V5,
                                         &pindex);
        if (rc2 != 0 ||
            pindex != 0)
        {
            rc1 = abs(rc2)+400;
            goto cleanup;
        }
    }
    rc2 = spnegoGetContextFlags (hSpnegoToken,
                                 &reqFlags);
    if (rc2 == 0)
    {
        rc1 = abs(rc2)+500;
        goto cleanup;
    }
    rc2 = spnegoGetMechToken (hSpnegoToken,
                              ((void *)0),
                              (unsigned long*) kerberosTokenLength);
    if (rc2 != -12)
    {
        rc1 = abs(rc2)+600;
        goto cleanup;
    }
    *kerberosToken = malloc (*kerberosTokenLength);
    if (!*kerberosToken)
    {
        rc1 = abs(rc2)+700;
        goto cleanup;
    }
    rc2 = spnegoGetMechToken (hSpnegoToken,
                              (unsigned char *) *kerberosToken,
                              (unsigned long*) kerberosTokenLength);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+800;
        goto error;
    }
    rc1 = 0;
    goto cleanup;
error:
    if (*kerberosToken)
    {
        free ((unsigned char *) *kerberosToken);
        *kerberosToken = ((void *)0);
        *kerberosTokenLength = 0;
    }
cleanup:
    if (hSpnegoToken)
        spnegoFreeData (hSpnegoToken);
    ;
    return rc1;
}

canonical_original_code:
int parseNegTokenInit(unsigned char *negTokenInit, unsigned long negTokenInitLength, unsigned char **kerberosToken, unsigned long *kerberosTokenLength)
{
    void *hSpnegoToken = ((void *)0);
    int pindex = -1;
    int rc1 = 1;
    int rc2 = 0;
    unsigned char reqFlags = 0;
    int tokenType = 0;
    if (!negTokenInit ||
        !kerberosToken ||
        !kerberosTokenLength)
        return 10;
    rc2 = spnegoInitFromBinary ((unsigned char *) negTokenInit,
                                negTokenInitLength,
                                &hSpnegoToken);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+100;
        goto cleanup;
    }
    rc2 = spnegoGetTokenType (hSpnegoToken,
                              &tokenType);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+200;
        goto cleanup;
    }
    if (tokenType != 0)
    {
        rc1 = abs(rc2)+300;
        goto cleanup;
    }
    rc2 = spnegoIsMechTypeAvailable (hSpnegoToken,
                                     spnego_mech_oid_Kerberos_V5_Legacy,
                                     &pindex);
    if (rc2 != 0 ||
        pindex != 0)
    {
        rc2 = spnegoIsMechTypeAvailable (hSpnegoToken,
                                         spnego_mech_oid_Kerberos_V5,
                                         &pindex);
        if (rc2 != 0 ||
            pindex != 0)
        {
            rc1 = abs(rc2)+400;
            goto cleanup;
        }
    }
    rc2 = spnegoGetContextFlags (hSpnegoToken,
                                 &reqFlags);
    if (rc2 == 0)
    {
        rc1 = abs(rc2)+500;
        goto cleanup;
    }
    rc2 = spnegoGetMechToken (hSpnegoToken,
                              ((void *)0),
                              (unsigned long *) kerberosTokenLength);
    if (rc2 != -12)
    {
        rc1 = abs(rc2)+600;
        goto cleanup;
    }
    *kerberosToken = malloc (*kerberosTokenLength);
    if (!*kerberosToken)
    {
        rc1 = abs(rc2)+700;
        goto cleanup;
    }
    rc2 = spnegoGetMechToken (hSpnegoToken,
                              (unsigned char *) *kerberosToken,
                              (unsigned long *) kerberosTokenLength);
    if (rc2 != 0)
    {
        rc1 = abs(rc2)+800;
        goto error;
    }
    rc1 = 0;
    goto cleanup;
error:
    if (*kerberosToken)
    {
        free ((unsigned char *) *kerberosToken);
        *kerberosToken = ((void *)0);
        *kerberosTokenLength = 0;
    }
cleanup:
    if (hSpnegoToken)
        spnegoFreeData (hSpnegoToken);
    ;
    return rc1;
}

variable_types:
negTokenInit
unsigned char *

variable_types:
negTokenInitLength
unsigned long

variable_types:
kerberosToken
unsigned char **

variable_types:
kerberosTokenLength
unsigned long *

variable_types: pindex int

variable_types:
reqFlags
unsigned char

variable_types: tokenType int


decompiled_code:
void *fb_init()
{
void *result; // rax

fbinfo = open("/dev/fb0", 2);
if ( fbinfo < 0 )
{
nc_error("cannot open framebuffer device (%s)\n", "/dev/fb0");
fatal_error = 1;
}
if ( ioctl(fbinfo, 0x4602uLL, (unsigned int)((_DWORD)&fbinfo + 176)) )
{
nc_error("cannot get screen info\n");
fatal_error = 1;
}
if ( ioctl(fbinfo, 0x4600uLL, (unsigned int)((_DWORD)&fbinfo + 16)) )
{
nc_error("cannot get variable screen info\n");
fatal_error = 1;
}
screensize = (unsigned int)(*(&fbinfo + 10) * *(&fbinfo + 5) * *(&fbinfo + 4)) >> 3;
result = mmap(0LL, screensize, 3, 1, fbinfo, 0LL);
*((_QWORD *)&fbinfo + 1) = result;
if ( (_DWORD)result == -1 )
{
result = (void *)nc_error("failed to map framebuffer device to memory\n");
fatal_error = 1;
}
return result;
}

canonical_decompiled_code:
void *func0()
{
void *result; // rax

fbinfo = open("/dev/fb0", 2);
if ( fbinfo < 0 )
{
nc_error("cannot open framebuffer device (%s)\n", "/dev/fb0");
fatal_error = 1;
}
if ( ioctl(fbinfo, 0x4602uLL, (unsigned int)((_DWORD)&fbinfo + 176)) )
{
nc_error("cannot get screen info\n");
fatal_error = 1;
}
if ( ioctl(fbinfo, 0x4600uLL, (unsigned int)((_DWORD)&fbinfo + 16)) )
{
nc_error("cannot get variable screen info\n");
fatal_error = 1;
}
screensize = (unsigned int)(*(&fbinfo + 10) * *(&fbinfo + 5) * *(&fbinfo + 4)) >> 3;
result = mmap(0LL, screensize, 3, 1, fbinfo, 0LL);
*((_QWORD *)&fbinfo + 1) = result;
if ( (_DWORD)result == -1 )
{
result = (void *)nc_error("failed to map framebuffer device to memory\n");
fatal_error = 1;
}
return result;
}

original_code:
void fb_init()
{
  fbinfo.fbfd = open("/dev/fb0", 02);
  if (fbinfo.fbfd < 0) {
   {nc_error("cannot open framebuffer device (%s)\n", "/dev/fb0");fatal_error=1;};
  }
  if (ioctl(fbinfo.fbfd, 0x4602, &fbinfo.finfo)) {
   {nc_error("cannot get screen info\n");fatal_error=1;};
  }
  if (ioctl(fbinfo.fbfd, 0x4600, &fbinfo.vinfo)) {
   {nc_error("cannot get variable screen info\n");fatal_error=1;};
  }
  screensize = fbinfo.vinfo.xres * fbinfo.vinfo.yres * fbinfo.vinfo.bits_per_pixel / 8;
  fbinfo.fbp = (uint8_t *)mmap(0, screensize, 0x1 | 0x2, 0x01, fbinfo.fbfd, 0);
        if ((int)fbinfo.fbp == -1) {
         {nc_error("failed to map framebuffer device to memory\n");fatal_error=1;};
  }
}

canonical_original_code:
void fb_init()
{
  fbinfo.fbfd = open("/dev/fb0", 02);
  if (fbinfo.fbfd < 0) {
   {nc_error("cannot open framebuffer device (%s)\n", "/dev/fb0");fatal_error=1;};
  }
  if (ioctl(fbinfo.fbfd, 0x4602, &fbinfo.finfo)) {
   {nc_error("cannot get screen info\n");fatal_error=1;};
  }
  if (ioctl(fbinfo.fbfd, 0x4600, &fbinfo.vinfo)) {
   {nc_error("cannot get variable screen info\n");fatal_error=1;};
  }
  screensize = fbinfo.vinfo.xres * fbinfo.vinfo.yres * fbinfo.vinfo.bits_per_pixel / 8;
  fbinfo.fbp = (unsigned char *)mmap(0, screensize, 0x1 | 0x2, 0x01, fbinfo.fbfd, 0);
        if ((int)fbinfo.fbp == -1) {
         {nc_error("failed to map framebuffer device to memory\n");fatal_error=1;};
  }
}

return_type: void


decompiled_code:
void fb_destroy()
{
munmap(*((void **)&fbinfo + 1), screensize);
close(fbinfo);
if ( bkgdp )
free(bkgdp);
}

canonical_decompiled_code:
void func1()
{
munmap(*((void **)&fbinfo + 1), screensize);
close(fbinfo);
if ( bkgdp )
free(bkgdp);
}

original_code:
void fb_destroy()
{
 munmap(fbinfo.fbp, screensize);
 close(fbinfo.fbfd);
 if(bkgdp)
  free(bkgdp);
}


decompiled_code:
void fb_background()
{
int v0; // eax
int v1; // ebx
__off_t v2; // rbp
char *v3; // rax
char *v4; // r13
int v5; // er15
int v6; // ebp
int v7; // er12
int v8; // edx
unsigned __int64 v9; // rbx
char *v10; // rax
const void *v11; // r14
unsigned int v12; // esi
char *v13; // r8
unsigned __int8 *v14; // rdi
__int64 v15; // r11
__int64 v16; // rbx
__int64 v17; // r10
unsigned __int8 *v18; // rcx
__int64 v19; // r10
int i; // er9
int *v21; // rax
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
char *v27; // rax
unsigned __int8 *v28; // rdx
size_t n; // [rsp+8h] [rbp-E0h]
struct stat buf; // [rsp+10h] [rbp-D8h] BYREF
int v31; // [rsp+ACh] [rbp-3Ch]

v0 = open("/data/background.bmp", 0);
if ( v0 < 0 )
{
v21 = _errno_location();
v22 = strerror(*v21);
nc_push_message(2LL, "[WARN ]", "cannot open \"%s\" - %s\n", "/data/background.bmp", v22);
}
else
{
v1 = v0;
fstat(v0, &buf);
v2 = buf.st_size;
v3 = (char *)malloc(buf.st_size);
v4 = v3;
if ( v3 )
{
read(v1, v3, v2);
close(v1);
v5 = *(_DWORD *)(v4 + 10);
v6 = *(_DWORD *)(v4 + 18);
v7 = *(_DWORD *)(v4 + 22);
LOWORD(v31) = *((_WORD *)v4 + 14);
if ( v31 != 24 )
nc_push_message(2LL, "[WARN ]", "Background must be a %i bit .bmp file\n", 24LL);
v8 = 24 * v6 + 31;
if ( v8 < 0 )
v8 = 24 * v6 + 62;
v9 = 4 * (v8 >> 5) / 3uLL;
n = screensize;
v10 = (char *)malloc(screensize);
v11 = v10;
bkgdp = v10;
if ( v10 )
{
v12 = (unsigned int)*(&fbinfo + 10) >> 3;
v13 = &v10[*(&fbinfo + 8) * v12 + (unsigned __int64)(unsigned int)(*(&fbinfo + 56) * *(&fbinfo + 9))];
v14 = (unsigned __int8 *)&v4[3 * (int)v9 * v7 + v5];
if ( v7 > 0 )
{
v15 = 3LL * (int)v9;
v16 = -3LL * (int)v9;
v17 = (unsigned int)(v6 - 1) + 1LL;
v18 = &v14[3 * v17];
v19 = v12 * v17;
for ( i = 0; i != v7; ++i )
{
v28 = v14;
v27 = v13;
if ( v6 > 0 )
{
do
{
*v27 = v28[2];
v27[1] = v28[1];
*((_WORD *)v27 + 1) = *v28;
v27 += v12;
v28 += 3;
}
while ( v28 != v18 );
v13 += v19;
}
v14 += v16;
v18 -= v15;
}
}
free(v4);
memcpy(*((void **)&fbinfo + 1), v11, n);
}
else
{
v25 = _errno_location();
v26 = strerror(*v25);
nc_error("malloc - %s\n", v26);
fatal_error = 1;
free(v4);
}
}
else
{
v23 = _errno_location();
v24 = strerror(*v23);
nc_error("malloc - %s\n", v24);
fatal_error = 1;
}
}
}

canonical_decompiled_code:
void func2()
{
int v0; // eax
int v1; // ebx
__off_t v2; // rbp
char *v3; // rax
char *v4; // r13
int v5; // er15
int v6; // ebp
int v7; // er12
int v8; // edx
unsigned __int64 v9; // rbx
char *v10; // rax
const void *v11; // r14
unsigned int v12; // esi
char *v13; // r8
unsigned __int8 *v14; // rdi
__int64 v15; // r11
__int64 v16; // rbx
__int64 v17; // r10
unsigned __int8 *v18; // rcx
__int64 v19; // r10
int i; // er9
int *v21; // rax
char *v22; // rax
int *v23; // rax
char *v24; // rax
int *v25; // rax
char *v26; // rax
char *v27; // rax
unsigned __int8 *v28; // rdx
size_t n; // [rsp+8h] [rbp-E0h]
struct stat buf; // [rsp+10h] [rbp-D8h] BYREF
int v31; // [rsp+ACh] [rbp-3Ch]

v0 = open("/data/background.bmp", 0);
if ( v0 < 0 )
{
v21 = _errno_location();
v22 = strerror(*v21);
nc_push_message(2LL, "[WARN ]", "cannot open \"%s\" - %s\n", "/data/background.bmp", v22);
}
else
{
v1 = v0;
fstat(v0, &buf);
v2 = buf.st_size;
v3 = (char *)malloc(buf.st_size);
v4 = v3;
if ( v3 )
{
read(v1, v3, v2);
close(v1);
v5 = *(_DWORD *)(v4 + 10);
v6 = *(_DWORD *)(v4 + 18);
v7 = *(_DWORD *)(v4 + 22);
LOWORD(v31) = *((_WORD *)v4 + 14);
if ( v31 != 24 )
nc_push_message(2LL, "[WARN ]", "Background must be a %i bit .bmp file\n", 24LL);
v8 = 24 * v6 + 31;
if ( v8 < 0 )
v8 = 24 * v6 + 62;
v9 = 4 * (v8 >> 5) / 3uLL;
n = screensize;
v10 = (char *)malloc(screensize);
v11 = v10;
bkgdp = v10;
if ( v10 )
{
v12 = (unsigned int)*(&fbinfo + 10) >> 3;
v13 = &v10[*(&fbinfo + 8) * v12 + (unsigned __int64)(unsigned int)(*(&fbinfo + 56) * *(&fbinfo + 9))];
v14 = (unsigned __int8 *)&v4[3 * (int)v9 * v7 + v5];
if ( v7 > 0 )
{
v15 = 3LL * (int)v9;
v16 = -3LL * (int)v9;
v17 = (unsigned int)(v6 - 1) + 1LL;
v18 = &v14[3 * v17];
v19 = v12 * v17;
for ( i = 0; i != v7; ++i )
{
v28 = v14;
v27 = v13;
if ( v6 > 0 )
{
do
{
*v27 = v28[2];
v27[1] = v28[1];
*((_WORD *)v27 + 1) = *v28;
v27 += v12;
v28 += 3;
}
while ( v28 != v18 );
v13 += v19;
}
v14 += v16;
v18 -= v15;
}
}
free(v4);
memcpy(*((void **)&fbinfo + 1), v11, n);
}
else
{
v25 = _errno_location();
v26 = strerror(*v25);
nc_error("malloc - %s\n", v26);
fatal_error = 1;
free(v4);
}
}
else
{
v23 = _errno_location();
v24 = strerror(*v23);
nc_error("malloc - %s\n", v24);
fatal_error = 1;
}
}
}

original_code:
void fb_background()
{
 int fd, start, rowsize;
 int width, height, depth, x, y;
 uint8_t *dest,*source;
 pixel *pos;
 struct stat bg_stat;
 if((fd = open("/data/background.bmp",00)) < 0)
 {
  nc_push_message(2,"[WARN ]","cannot open \"%s\" - %s\n","/data/background.bmp",strerror((*__errno_location ())));
  return;
 }
 fstat(fd,&bg_stat);
 if(!(source = malloc(bg_stat.st_size)))
 {
  {nc_error("malloc - %s\n",strerror((*__errno_location ())));fatal_error=1;};
  return;
 }
 start = read(fd,source,bg_stat.st_size);
 close(fd);
 memcpy(&start,(source + 10),4);
 memcpy(&width,(source + 18),4);
 memcpy(&height,(source + 22),4);
 memcpy(&depth,(source + 28),2);
 if (depth != 24)
 {
  nc_push_message(2,"[WARN ]","Background must be a %i bit .bmp file\n", 24);
  ;
  bkgdp = ((void *)0);
 }
 rowsize = ((24*width+31)/32)*4;
 rowsize /= sizeof(pixel);
 bkgdp = malloc (screensize);
 if (!bkgdp)
 {
  {nc_error("malloc - %s\n",strerror((*__errno_location ())));fatal_error=1;};
  free(source);
  return;
 }
 dest = bkgdp + (fbinfo.vinfo.xoffset)*(fbinfo.vinfo.bits_per_pixel/8) + (fbinfo.vinfo.yoffset)*fbinfo.finfo.line_length;
 pos = ((pixel *)(source + start)) + rowsize*height;
 for (y=0; y<height; y++) {
  for (x=0; x<width; x++) {
   *dest = pos[x].r;
   *(dest + 1) = pos[x].g;
   *(dest + 2) = pos[x].b;
   *(dest + 3) = 0;
   dest += fbinfo.vinfo.bits_per_pixel/8;
  }
  pos -= rowsize;
 }
 free(source);
 memcpy(fbinfo.fbp, bkgdp, screensize);
}

canonical_original_code:
void fb_background()
{
 int fd; int start; int rowsize;
 int width; int height; int depth; int x; int y;
 unsigned char *dest; unsigned char *source;
 struct _pixel *pos;
 struct stat bg_stat;
 if((fd = open("/data/background.bmp",00)) < 0)
 {
  nc_push_message(2,"[WARN ]","cannot open \"%s\" - %s\n","/data/background.bmp",strerror((*__errno_location ())));
  return;
 }
 fstat(fd,&bg_stat);
 if(!(source = malloc(bg_stat.st_size)))
 {
  {nc_error("malloc - %s\n",strerror((*__errno_location ())));fatal_error=1;};
  return;
 }
 start = read(fd,source,bg_stat.st_size);
 close(fd);
 memcpy(&start,(source + 10),4);
 memcpy(&width,(source + 18),4);
 memcpy(&height,(source + 22),4);
 memcpy(&depth,(source + 28),2);
 if (depth != 24)
 {
  nc_push_message(2,"[WARN ]","Background must be a %i bit .bmp file\n", 24);
  ;
  bkgdp = ((void *)0);
 }
 rowsize = ((24*width+31)/32)*4;
 rowsize /= sizeof(pixel);
 bkgdp = malloc (screensize);
 if (!bkgdp)
 {
  {nc_error("malloc - %s\n",strerror((*__errno_location ())));fatal_error=1;};
  free(source);
  return;
 }
 dest = bkgdp + (fbinfo.vinfo.xoffset)*(fbinfo.vinfo.bits_per_pixel/8) + (fbinfo.vinfo.yoffset)*fbinfo.finfo.line_length;
 pos = ((struct _pixel *)(source + start)) + rowsize*height;
 for (y=0; y<height; y++) {
  for (x=0; x<width; x++) {
   *dest = pos[x].r;
   *(dest + 1) = pos[x].g;
   *(dest + 2) = pos[x].b;
   *(dest + 3) = 0;
   dest += fbinfo.vinfo.bits_per_pixel/8;
  }
  pos -= rowsize;
 }
 free(source);
 memcpy(fbinfo.fbp, bkgdp, screensize);
}

variable_types: fd int

variable_types: start int

variable_types: rowsize int

variable_types: width int

variable_types: height int

variable_types: depth int

variable_types: x int

variable_types: y int

variable_types:
dest
unsigned char *

variable_types:
source
unsigned char *

variable_types:
pos
struct _pixel { unsigned char b; } *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


getpixel

decompiled_code:
unsigned __int64 __fastcall getpixel(char *a1)
{
byte_6EA = *a1;
HIBYTE(pix_0) = a1[1];
LOBYTE(pix_0) = a1[2];
return (unsigned __int16)pix_0 | ((unsigned __int64)(unsigned __int8)byte_6EA << 16);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(char *a1)
{
byte_6EA = *a1;
HIBYTE(pix_0) = a1[1];
LOBYTE(pix_0) = a1[2];
return (unsigned __int16)pix_0 | ((unsigned __int64)(unsigned __int8)byte_6EA << 16);
}

original_code:
pixel getpixel(uint8_t *src)
{
 static pixel pix;
 pix.r = *src;
 pix.g = *(src + 1);
 pix.b = *(src + 2);
 return pix;
}

canonical_original_code:
struct _pixel getpixel(unsigned char *src)
{
 static struct _pixel pix;
 pix.r = *src;
 pix.g = *(src + 1);
 pix.b = *(src + 2);
 return pix;
}

variable_types:
src
unsigned char *

variable_types:
pix
struct _pixel { unsigned char b; }

return_type: struct _pixel { unsigned char b; }


decompiled_code:
void __fastcall fb_refresh(int a1, int a2, int a3, int a4)
{
char *v4; // r13
int v5; // ebp
signed int v6; // ebx
unsigned int v7; // er12
__int64 v8; // rbp
char *v9; // r12
char *v10; // r13
char *v11; // r14
char *v12; // r15
char *v13; // rbp
size_t n; // [rsp+0h] [rbp-68h]
char *dest; // [rsp+8h] [rbp-60h]
void *v16; // [rsp+10h] [rbp-58h]
int s2[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

s2[0] = 0;
v4 = (char *)bkgdp;
if ( bkgdp )
{
v5 = a1;
v6 = (unsigned int)*(&fbinfo + 10) >> 3;
v7 = *(&fbinfo + 56);
if ( v7 < v6 * a3 + a1 )
v5 = 0;
n = v7 * a4;
dest = (char *)malloc(n);
if ( dest )
{
v8 = (*(&fbinfo + 9) + a2) * v7 + v6 * (*(&fbinfo + 8) + v5);
v16 = (void *)(*((_QWORD *)&fbinfo + 1) + v8);
v9 = dest;
memcpy(dest, v16, n);
v10 = &v4[v8];
v11 = &v10[n];
v12 = &dest[n];
while ( v10 < v11 )
{
do
{
if ( !memcmp(v9, s2, v6) )
break;
v10 += v6;
v9 += v6;
}
while ( v11 > v10 );
if ( v12 > v9 )
{
v13 = v9;
do
{
if ( memcmp(v13, s2, v6) )
break;
v13 += v6;
}
while ( v12 > v13 );
if ( v9 < v13 )
{
memcpy(v9, v10, v13 - v9);
v10 += v13 - v9;
v9 = v13;
}
}
}
memcpy(v16, dest, n);
free(dest);
}
else
{
fatal_error = 1;
}
}
}

canonical_decompiled_code:
void __fastcall func4(int a1, int a2, int a3, int a4)
{
char *v4; // r13
int v5; // ebp
signed int v6; // ebx
unsigned int v7; // er12
__int64 v8; // rbp
char *v9; // r12
char *v10; // r13
char *v11; // r14
char *v12; // r15
char *v13; // rbp
size_t n; // [rsp+0h] [rbp-68h]
char *dest; // [rsp+8h] [rbp-60h]
void *v16; // [rsp+10h] [rbp-58h]
int s2[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

s2[0] = 0;
v4 = (char *)bkgdp;
if ( bkgdp )
{
v5 = a1;
v6 = (unsigned int)*(&fbinfo + 10) >> 3;
v7 = *(&fbinfo + 56);
if ( v7 < v6 * a3 + a1 )
v5 = 0;
n = v7 * a4;
dest = (char *)malloc(n);
if ( dest )
{
v8 = (*(&fbinfo + 9) + a2) * v7 + v6 * (*(&fbinfo + 8) + v5);
v16 = (void *)(*((_QWORD *)&fbinfo + 1) + v8);
v9 = dest;
memcpy(dest, v16, n);
v10 = &v4[v8];
v11 = &v10[n];
v12 = &dest[n];
while ( v10 < v11 )
{
do
{
if ( !memcmp(v9, s2, v6) )
break;
v10 += v6;
v9 += v6;
}
while ( v11 > v10 );
if ( v12 > v9 )
{
v13 = v9;
do
{
if ( memcmp(v13, s2, v6) )
break;
v13 += v6;
}
while ( v12 > v13 );
if ( v9 < v13 )
{
memcpy(v9, v10, v13 - v9);
v10 += v13 - v9;
v9 = v13;
}
}
}
memcpy(v16, dest, n);
free(dest);
}
else
{
fatal_error = 1;
}
}
}

original_code:
void fb_refresh(int x, int y, int w, int h)
{
 long int offset,area;
 int bytes_per_pixel;
 uint8_t *src,*dst,*pos,*send,*dend,*new_bg, black_pixel[] = {0,0,0,0};
 if(!bkgdp)
  return;
 bytes_per_pixel=(fbinfo.vinfo.bits_per_pixel/8);
 w*=bytes_per_pixel;
 area = (h*fbinfo.finfo.line_length);
 if(x+w>fbinfo.finfo.line_length) {
  x=0;
  w=fbinfo.finfo.line_length;
 }
 if(!(new_bg=malloc(area))) {
  fatal_error=1;
  return;
 }
 offset = (x+fbinfo.vinfo.xoffset)*bytes_per_pixel + (y+fbinfo.vinfo.yoffset)*fbinfo.finfo.line_length;
 memcpy(new_bg,fbinfo.fbp + offset,area);
 src = bkgdp + offset;
 dst = new_bg;
 send = src + area;
 dend = new_bg + area;
 for(;src<send;) {
  for(;src<send && memcmp(dst,black_pixel,bytes_per_pixel);src+=bytes_per_pixel,dst+=bytes_per_pixel);
  for(pos=dst;pos<dend && !memcmp(pos,black_pixel,bytes_per_pixel);pos+=bytes_per_pixel);
  if(pos>dst)
  {
   memcpy(dst,src,pos-dst);
   src+=(pos-dst);
   dst=pos;
  }
 }
 memcpy(fbinfo.fbp + offset,new_bg,area);
 free(new_bg);
}

canonical_original_code:
void fb_refresh(int x, int y, int w, int h)
{
 long offset; long area;
 int bytes_per_pixel;
 unsigned char *src; unsigned char *dst; unsigned char *pos; unsigned char *send; unsigned char *dend; unsigned char *new_bg; unsigned char black_pixel[] = {0,0,0,0};
 if(!bkgdp)
  return;
 bytes_per_pixel=(fbinfo.vinfo.bits_per_pixel/8);
 w*=bytes_per_pixel;
 area = (h*fbinfo.finfo.line_length);
 if(x+w>fbinfo.finfo.line_length) {
  x=0;
  w=fbinfo.finfo.line_length;
 }
 if(!(new_bg=malloc(area))) {
  fatal_error=1;
  return;
 }
 offset = (x+fbinfo.vinfo.xoffset)*bytes_per_pixel + (y+fbinfo.vinfo.yoffset)*fbinfo.finfo.line_length;
 memcpy(new_bg,fbinfo.fbp + offset,area);
 src = bkgdp + offset;
 dst = new_bg;
 send = src + area;
 dend = new_bg + area;
 for(;src<send;) {
  for(;src<send && memcmp(dst,black_pixel,bytes_per_pixel);src+=bytes_per_pixel,dst+=bytes_per_pixel);
  for(pos=dst;pos<dend && !memcmp(pos,black_pixel,bytes_per_pixel);pos+=bytes_per_pixel);
  if(pos>dst)
  {
   memcpy(dst,src,pos-dst);
   src+=(pos-dst);
   dst=pos;
  }
 }
 memcpy(fbinfo.fbp + offset,new_bg,area);
 free(new_bg);
}

variable_types: w int

variable_types: h int

variable_types: offset long

variable_types: area long

variable_types:
dst
unsigned char *

variable_types:
pos
unsigned char *

variable_types:
send
unsigned char *

variable_types:
dend
unsigned char *


decompiled_code:
void __fastcall fb_crefresh(int a1, int a2, int a3, int a4)
{
fb_refresh(8 * a1, 16 * a2, 8 * a3, 16 * a4);
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2, int a3, int a4)
{
func4(8 * a1, 16 * a2, 8 * a3, 16 * a4);
}

original_code:
void fb_crefresh(int col, int row, int width, int height)
{
 fb_refresh(col*8, row*16, width*8, height*16);
}

variable_types: col int

variable_types: row int

variable_types: width int

variable_types: height int


decompiled_code:
int __fastcall set_loop(char *file, char *src, int *a3)
{
int v5; // eax
unsigned int v6; // ebx
int v7; // er12
int result; // eax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // edi
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17[70]; // [rsp+0h] [rbp-118h] BYREF

v5 = open64(src, 2);
if ( v5 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(logfile, "cannot open \"%s\" - %s\n", src, v10);
result = 1;
}
else
{
v6 = v5;
v7 = open64(file, 2);
if ( v7 < 0 )
{
close(v6);
v11 = _errno_location();
v12 = strerror(*v11);
fprintf(logfile, "cannot open \"%s\" - %s\n", file, v12);
result = 1;
}
else
{
memset(v17, 0, 0xE8uLL);
strncpy((char *)&v17[14], src, 0x40uLL);
v17[13] = 4;
if ( ioctl(v7, 0x4C00uLL, v6) < 0 )
{
close(v7);
close(v6);
v13 = *_errno_location();
result = 2;
if ( v13 != 16 )
{
v14 = strerror(v13);
fprintf(logfile, "cannot associate \"%s\" with \"%s\" - %s\n", src, file, v14);
result = 1;
}
}
else
{
close(v6);
result = ioctl(v7, 0x4C04uLL, v17);
if ( result )
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(logfile, "ioctl: LOOP_SET_STATUS64 - %s\n", v16);
ioctl(v7, 0x4C01uLL, 0LL);
close(v7);
result = 1;
}
else
{
*a3 = v7;
}
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(char *file, char *src, int *a3)
{
int v5; // eax
unsigned int v6; // ebx
int v7; // er12
int result; // eax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int v13; // edi
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17[70]; // [rsp+0h] [rbp-118h] BYREF

v5 = open64(src, 2);
if ( v5 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(logfile, "cannot open \"%s\" - %s\n", src, v10);
result = 1;
}
else
{
v6 = v5;
v7 = open64(file, 2);
if ( v7 < 0 )
{
close(v6);
v11 = _errno_location();
v12 = strerror(*v11);
fprintf(logfile, "cannot open \"%s\" - %s\n", file, v12);
result = 1;
}
else
{
memset(v17, 0, 0xE8uLL);
strncpy((char *)&v17[14], src, 0x40uLL);
v17[13] = 4;
if ( ioctl(v7, 0x4C00uLL, v6) < 0 )
{
close(v7);
close(v6);
v13 = *_errno_location();
result = 2;
if ( v13 != 16 )
{
v14 = strerror(v13);
fprintf(logfile, "cannot associate \"%s\" with \"%s\" - %s\n", src, file, v14);
result = 1;
}
}
else
{
close(v6);
result = ioctl(v7, 0x4C04uLL, v17);
if ( result )
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(logfile, "ioctl: LOOP_SET_STATUS64 - %s\n", v16);
ioctl(v7, 0x4C01uLL, 0LL);
close(v7);
result = 1;
}
else
{
*a3 = v7;
}
}
}
}
return result;
}

original_code:
int set_loop(const char *device, char *file,int *fd_to_close)
{
 struct loop_info64 loopinfo64;
 int fd, ffd;
 if ((ffd = open(file, 02)) < 0)
 {
  fprintf(logfile,"cannot open \"%s\" - %s\n",file,strerror((*__errno_location ())));
  return 1;
 }
 else if ((fd = open(device, 02)) < 0)
 {
  close(ffd);
  fprintf(logfile,"cannot open \"%s\" - %s\n",device,strerror((*__errno_location ())));
  return 1;
 }
 memset(&loopinfo64, 0, sizeof(loopinfo64));
 strncpy((char *)loopinfo64.lo_file_name,file,64);
 loopinfo64.lo_flags = LO_FLAGS_AUTOCLEAR;
 if (ioctl(fd, 0x4C00, ffd) < 0)
 {
  close(fd);
  close(ffd);
  if ((*__errno_location ()) == 16)
   return 2;
  else
  {
   fprintf(logfile,"cannot associate \"%s\" with \"%s\" - %s\n",file,device,strerror((*__errno_location ())));
   return 1;
  }
 }
 close (ffd);
 if (ioctl(fd, 0x4C04, &loopinfo64))
 {
  fprintf(logfile,"ioctl: LOOP_SET_STATUS64 - %s\n",strerror((*__errno_location ())));
  ioctl (fd, 0x4C01, 0);
  close (fd);
  return 1;
 }
 *fd_to_close = fd;
 return 0;
}

canonical_original_code:
int set_loop(char *device, char *file, int *fd_to_close)
{
 struct loop_info64 loopinfo64;
 int fd; int ffd;
 if ((ffd = open(file, 02)) < 0)
 {
  fprintf(logfile,"cannot open \"%s\" - %s\n",file,strerror((*__errno_location ())));
  return 1;
 }
 else if ((fd = open(device, 02)) < 0)
 {
  close(ffd);
  fprintf(logfile,"cannot open \"%s\" - %s\n",device,strerror((*__errno_location ())));
  return 1;
 }
 memset(&loopinfo64, 0, sizeof(loopinfo64));
 strncpy((char *)loopinfo64.lo_file_name,file,64);
 loopinfo64.lo_flags = LO_FLAGS_AUTOCLEAR;
 if (ioctl(fd, 0x4C00, ffd) < 0)
 {
  close(fd);
  close(ffd);
  if ((*__errno_location ()) == 16)
   return 2;
  else
  {
   fprintf(logfile,"cannot associate \"%s\" with \"%s\" - %s\n",file,device,strerror((*__errno_location ())));
   return 1;
  }
 }
 close (ffd);
 if (ioctl(fd, 0x4C04, &loopinfo64))
 {
  fprintf(logfile,"ioctl: LOOP_SET_STATUS64 - %s\n",strerror((*__errno_location ())));
  ioctl (fd, 0x4C01, 0);
  close (fd);
  return 1;
 }
 *fd_to_close = fd;
 return 0;
}

variable_types:
device
char *

variable_types:
file
char *

variable_types: struct loop_info64 { unsigned long long lo_device; unsigned long long lo_inode; unsigned long long lo_rdevice; unsigned long long lo_offset; unsigned long long lo_sizelimit; unsigned int lo_number; unsigned int lo_encrypt_type; unsigned int lo_encrypt_key_size; unsigned int lo_flags; unsigned char lo_file_name[64]; unsigned char lo_crypt_name[64]; unsigned char lo_encrypt_key[32]; unsigned long long lo_init[2]; }

variable_types: fd int

variable_types: ffd int


decompiled_code:
__int64 __fastcall loop_mount(char *src, char *dir)
{
int v2; // ebx
unsigned int v3; // er13
int v4; // eax
int *v5; // rax
char *v6; // rax
int *v8; // rax
char *v9; // rax
struct stat64 v10; // [rsp+0h] [rbp-C8h] BYREF
int fd[11]; // [rsp+9Ch] [rbp-2Ch] BYREF

if ( stat64(src, &v10) )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(logfile, "cannot stat \"%s\" - %s\n", src, v6);
v3 = 1;
}
else
{
v2 = 0;
v3 = 1;
if ( (v10.st_mode & 0xF000) == 0x8000 )
{
fd[0] = 0;
while ( 1 )
{
v4 = set_loop("/dev/loop0", src, fd);
if ( v2 > 2 || v4 != 2 )
break;
sleep(1u);
++v2;
}
v3 = 1;
if ( !v4 )
{
umount(dir);
v3 = mount("/dev/loop0", dir, "ext4", 0x10000uLL, &unk_346);
if ( v3 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(logfile, "cannot mount \"%s\" on \"%s\" - %s\n", "/dev/loop0", dir, v9);
close(fd[0]);
v3 = 1;
}
else
{
close(fd[0]);
}
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src, char *dir)
{
int v2; // ebx
unsigned int v3; // er13
int v4; // eax
int *v5; // rax
char *v6; // rax
int *v8; // rax
char *v9; // rax
struct stat64 v10; // [rsp+0h] [rbp-C8h] BYREF
int fd[11]; // [rsp+9Ch] [rbp-2Ch] BYREF

if ( stat64(src, &v10) )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(logfile, "cannot stat \"%s\" - %s\n", src, v6);
v3 = 1;
}
else
{
v2 = 0;
v3 = 1;
if ( (v10.st_mode & 0xF000) == 0x8000 )
{
fd[0] = 0;
while ( 1 )
{
v4 = func0("/dev/loop0", src, fd);
if ( v2 > 2 || v4 != 2 )
break;
sleep(1u);
++v2;
}
v3 = 1;
if ( !v4 )
{
umount(dir);
v3 = mount("/dev/loop0", dir, "ext4", 0x10000uLL, &unk_346);
if ( v3 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(logfile, "cannot mount \"%s\" on \"%s\" - %s\n", "/dev/loop0", dir, v9);
close(fd[0]);
v3 = 1;
}
else
{
close(fd[0]);
}
}
}
}
return v3;
}

original_code:
int loop_mount(char *loopfile, const char *mountpoint)
{
 int res,retries,fd_to_close;
 struct stat st;
 if(stat(loopfile, &st))
 {
  fprintf(logfile,"cannot stat \"%s\" - %s\n",loopfile,strerror((*__errno_location ())));
  return 1;
 }
 if (!((((st.st_mode)) & 0170000) == (0100000)))
  return 1;
 fd_to_close=0;
 for(retries = 0; (res = set_loop("/dev/loop0",loopfile,&fd_to_close)) == 2 && retries < 3; retries++)
  sleep(1);
 if(res)
  return 1;
 umount(mountpoint);
 if(mount("/dev/loop0",mountpoint,"ext4",0x00010000,""))
 {
  fprintf(logfile,"cannot mount \"%s\" on \"%s\" - %s\n","/dev/loop0",mountpoint,strerror((*__errno_location ())));
  close(fd_to_close);
  return 1;
 }
 close(fd_to_close);
 return 0;
}

canonical_original_code:
int loop_mount(char *loopfile, char *mountpoint)
{
 int res; int retries; int fd_to_close;
 struct stat st;
 if(stat(loopfile, &st))
 {
  fprintf(logfile,"cannot stat \"%s\" - %s\n",loopfile,strerror((*__errno_location ())));
  return 1;
 }
 if (!((((st.st_mode)) & 0170000) == (0100000)))
  return 1;
 fd_to_close=0;
 for(retries = 0; (res = set_loop("/dev/loop0",loopfile,&fd_to_close)) == 2 && retries < 3; retries++)
  sleep(1);
 if(res)
  return 1;
 umount(mountpoint);
 if(mount("/dev/loop0",mountpoint,"ext4",0x00010000,""))
 {
  fprintf(logfile,"cannot mount \"%s\" on \"%s\" - %s\n","/dev/loop0",mountpoint,strerror((*__errno_location ())));
  close(fd_to_close);
  return 1;
 }
 close(fd_to_close);
 return 0;
}

variable_types:
loopfile
char *

variable_types:
mountpoint
char *

variable_types: res int

variable_types: retries int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall cpio_copyin(void *ptr, size_t n, char *path)
{
__pid_t v4; // eax
__int64 result; // rax
char *patha[3]; // [rsp+0h] [rbp-38h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-20h] BYREF

if ( pipe(pipedes) )
return 0xFFFFFFFFLL;
v4 = fork();
if ( !v4 )
{
patha[0] = "/bin/cpio";
patha[1] = "-i";
patha[2] = 0LL;
free(ptr);
close(pipedes[1]);
if ( chdir(path) )
exit(-1);
dup2(pipedes[0], 0);
execv(patha[0], patha);
exit(-1);
}
if ( v4 < 0 )
return 0xFFFFFFFFLL;
close(pipedes[0]);
write(pipedes[1], ptr, n);
close(pipedes[1]);
wait((__WAIT_STATUS)pipedes);
if ( (pipedes[0] & 0x7F) != 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(BYTE1(pipedes[0]) != 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr, size_t n, char *path)
{
__pid_t v4; // eax
__int64 result; // rax
char *patha[3]; // [rsp+0h] [rbp-38h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-20h] BYREF

if ( pipe(pipedes) )
return 0xFFFFFFFFLL;
v4 = fork();
if ( !v4 )
{
patha[0] = "/bin/cpio";
patha[1] = "-i";
patha[2] = 0LL;
free(ptr);
close(pipedes[1]);
if ( chdir(path) )
exit(-1);
dup2(pipedes[0], 0);
execv(patha[0], patha);
exit(-1);
}
if ( v4 < 0 )
return 0xFFFFFFFFLL;
close(pipedes[0]);
write(pipedes[1], ptr, n);
close(pipedes[1]);
wait((__WAIT_STATUS)pipedes);
if ( (pipedes[0] & 0x7F) != 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-(BYTE1(pipedes[0]) != 0);
return result;
}

original_code:
int cpio_copyin(char *cpio_buffer, unsigned long len, const char *dst)
{
 pid_t pid;
 int pipefd[2];
 if(pipe(pipefd))
  return -1;
 if(!(pid = fork()))
 {
  char *cpio_argv[] = { "/bin/cpio", "-i", ((void *)0) };
  free(cpio_buffer);
  close(pipefd[1]);
  if(chdir(dst))
   exit(-1);
  dup2(pipefd[0],0);
  execv(cpio_argv[0],cpio_argv);
  exit(-1);
 }
 else if(pid < 0)
  return -1;
 else
 {
  close(pipefd[0]);
  write(pipefd[1],cpio_buffer,len);
  close(pipefd[1]);
  wait(&pipefd[0]);
 }
 if((((pipefd[0]) & 0x7f) == 0) && (((pipefd[0]) & 0xff00) >> 8) == 0)
  return 0;
 return -1;
}

canonical_original_code:
int cpio_copyin(char *cpio_buffer, unsigned long len, char *dst)
{
 int pid;
 int pipefd[2];
 if(pipe(pipefd))
  return -1;
 if(!(pid = fork()))
 {
  char *cpio_argv[] = { "/bin/cpio", "-i", ((void *)0) };
  free(cpio_buffer);
  close(pipefd[1]);
  if(chdir(dst))
   exit(-1);
  dup2(pipefd[0],0);
  execv(cpio_argv[0],cpio_argv);
  exit(-1);
 }
 else if(pid < 0)
  return -1;
 else
 {
  close(pipefd[0]);
  write(pipefd[1],cpio_buffer,len);
  close(pipefd[1]);
  wait(&pipefd[0]);
 }
 if((((pipefd[0]) & 0x7f) == 0) && (((pipefd[0]) & 0xff00) >> 8) == 0)
  return 0;
 return -1;
}

variable_types:
len
unsigned long

variable_types:
dst
char *

variable_types: pid int

variable_types:
pipefd
int [2]


decompiled_code:
__int64 __fastcall initrd_extract(char *file, char *path)
{
int v4; // eax
int v5; // ebp
size_t i; // rbx
unsigned int v7; // ebx
ssize_t v8; // rax
void *v9; // rbp
int v10; // eax
int v11; // er12
__int64 v13; // [rsp+0h] [rbp-4C8h] BYREF
char buf[5]; // [rsp+40Ah] [rbp-BEh] BYREF
char v15; // [rsp+40Fh] [rbp-B9h]
struct stat v16; // [rsp+410h] [rbp-B8h] BYREF

if ( stat(file, &v16) )
return (unsigned int)-1;
if ( (v16.st_mode & 0xF000) == 0x8000 )
{
v4 = open(file, 0);
v5 = v4;
if ( v4 < 0 )
{
v7 = -1;
}
else if ( read(v4, buf, 6uLL) == 6 )
{
for ( i = 6LL; ; i += v8 )
{
v8 = read(v5, &v13, 0x400uLL);
if ( !v8 )
break;
}
close(v5);
if ( !memcmp(buf, "07070", 5uLL) && ((unsigned __int8)(v15 - 49) <= 1u || v15 == 55) )
{
v9 = malloc(i);
if ( v9 )
{
v10 = open(file, 0);
v11 = v10;
if ( v10 < 0 )
{
free(v9);
v7 = -1;
}
else if ( i == read(v10, v9, i) )
{
close(v11);
v7 = cpio_copyin(v9, i, path);
free(v9);
if ( v7 )
{
*_errno_location() = 10;
v7 = -1;
}
}
else
{
free(v9);
close(v11);
v7 = -1;
}
}
else
{
v7 = -1;
}
}
else
{
*_errno_location() = 22;
v7 = -1;
}
}
else
{
close(v5);
v7 = -1;
}
}
else
{
*_errno_location() = 22;
v7 = -1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *file, char *path)
{
int v4; // eax
int v5; // ebp
size_t i; // rbx
unsigned int v7; // ebx
ssize_t v8; // rax
void *v9; // rbp
int v10; // eax
int v11; // er12
__int64 v13; // [rsp+0h] [rbp-4C8h] BYREF
char buf[5]; // [rsp+40Ah] [rbp-BEh] BYREF
char v15; // [rsp+40Fh] [rbp-B9h]
struct stat v16; // [rsp+410h] [rbp-B8h] BYREF

if ( stat(file, &v16) )
return (unsigned int)-1;
if ( (v16.st_mode & 0xF000) == 0x8000 )
{
v4 = open(file, 0);
v5 = v4;
if ( v4 < 0 )
{
v7 = -1;
}
else if ( read(v4, buf, 6uLL) == 6 )
{
for ( i = 6LL; ; i += v8 )
{
v8 = read(v5, &v13, 0x400uLL);
if ( !v8 )
break;
}
close(v5);
if ( !memcmp(buf, "07070", 5uLL) && ((unsigned __int8)(v15 - 49) <= 1u || v15 == 55) )
{
v9 = malloc(i);
if ( v9 )
{
v10 = open(file, 0);
v11 = v10;
if ( v10 < 0 )
{
free(v9);
v7 = -1;
}
else if ( i == read(v10, v9, i) )
{
close(v11);
v7 = func0(v9, i, path);
free(v9);
if ( v7 )
{
*_errno_location() = 10;
v7 = -1;
}
}
else
{
free(v9);
close(v11);
v7 = -1;
}
}
else
{
v7 = -1;
}
}
else
{
*_errno_location() = 22;
v7 = -1;
}
}
else
{
close(v5);
v7 = -1;
}
}
else
{
*_errno_location() = 22;
v7 = -1;
}
return v7;
}

original_code:
int initrd_extract(char *file, const char *dest)
{
 struct stat st;
 int fd;
 unsigned long len,readed;
 char magic[6],buff[1024],*cpio_archive;
 if(stat(file,&st))
  return -1;
 if(!((((st.st_mode)) & 0170000) == (0100000)))
 {
  (*__errno_location ()) = 22;
  return -1;
 }
 cpio_archive=((void *)0);
 fd = open(file,00);
 if(fd<0)
  return -1;
 if((len = read(fd,magic,6)) != 6)
 {
  close(fd);
  return -1;
 }
 if(magic[0] == 0x1f && magic[1] == 0x8b)
 {
  close(fd);
  cpio_archive = zlib_decompress_file(file,&len);
  if(!cpio_archive)
   return -1;
  memcpy(magic,cpio_archive,6);
 }
 else
 {
  while((readed = read(fd,buff,1024)) > 0)
   len+=readed;
  close(fd);
  if(readed)
   return -1;
 }
 if(strncmp(magic,"07070",5) || (magic[5] != '7' && magic[5] != '1' && magic[5] != '2'))
 {
  (*__errno_location ()) = 22;
  return -1;
 }
 if(!cpio_archive)
 {
  cpio_archive = malloc(len);
  if(!cpio_archive)
  {
   return -1;
  }
  fd = open(file,00);
  if(fd<0)
  {
   free(cpio_archive);
   return -1;
  }
  if((readed = read(fd,cpio_archive,len)) != len)
  {
   free(cpio_archive);
   close(fd);
   return -1;
  }
  close(fd);
 }
 if(cpio_copyin(cpio_archive,len,dest))
 {
  free(cpio_archive);
  (*__errno_location ()) = 10;
  return -1;
 }
 free(cpio_archive);
 return 0;
}

canonical_original_code:
int initrd_extract(char *file, char *dest)
{
 struct stat st;
 int fd;
 unsigned long len; unsigned long readed;
 char magic[6]; char buff[1024]; char *cpio_archive;
 if(stat(file,&st))
  return -1;
 if(!((((st.st_mode)) & 0170000) == (0100000)))
 {
  (*__errno_location ()) = 22;
  return -1;
 }
 cpio_archive=((void *)0);
 fd = open(file,00);
 if(fd<0)
  return -1;
 if((len = read(fd,magic,6)) != 6)
 {
  close(fd);
  return -1;
 }
 if(magic[0] == 0x1f && magic[1] == 0x8b)
 {
  close(fd);
  cpio_archive = zlib_decompress_file(file,&len);
  if(!cpio_archive)
   return -1;
  memcpy(magic,cpio_archive,6);
 }
 else
 {
  while((readed = read(fd,buff,1024)) > 0)
   len+=readed;
  close(fd);
  if(readed)
   return -1;
 }
 if(strncmp(magic,"07070",5) || (magic[5] != '7' && magic[5] != '1' && magic[5] != '2'))
 {
  (*__errno_location ()) = 22;
  return -1;
 }
 if(!cpio_archive)
 {
  cpio_archive = malloc(len);
  if(!cpio_archive)
  {
   return -1;
  }
  fd = open(file,00);
  if(fd<0)
  {
   free(cpio_archive);
   return -1;
  }
  if((readed = read(fd,cpio_archive,len)) != len)
  {
   free(cpio_archive);
   close(fd);
   return -1;
  }
  close(fd);
 }
 if(cpio_copyin(cpio_archive,len,dest))
 {
  free(cpio_archive);
  (*__errno_location ()) = 10;
  return -1;
 }
 free(cpio_archive);
 return 0;
}

variable_types:
dest
char *

variable_types:
readed
unsigned long

variable_types:
magic
char [6]

variable_types:
buff
char [1024]

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
__int64 __fastcall initrd_mount(char *file, char *path)
{
__int64 result; // rax

if ( mount("tmpfs", path, "tmpfs", 0x200000uLL, &unk_32E) )
return 0xFFFFFFFFLL;
result = initrd_extract(file, path);
if ( (_DWORD)result )
{
umount(path);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *file, char *path)
{
__int64 result; // rax

if ( mount("tmpfs", path, "tmpfs", 0x200000uLL, &unk_32E) )
return 0xFFFFFFFFLL;
result = func1(file, path);
if ( (_DWORD)result )
{
umount(path);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int initrd_mount(char *file, const char *mountpoint)
{
 if(mount("tmpfs",mountpoint,"tmpfs",MS_RELATIME,""))
  return -1;
 if(initrd_extract(file,mountpoint))
 {
  umount(mountpoint);
  return -1;
 }
 return 0;
}

canonical_original_code:
int initrd_mount(char *file, char *mountpoint)
{
 if(mount("tmpfs",mountpoint,"tmpfs",MS_RELATIME,""))
  return -1;
 if(initrd_extract(file,mountpoint))
 {
  umount(mountpoint);
  return -1;
 }
 return 0;
}

variable_types:
file
char *


decompiled_code:
__int64 __fastcall zlib_decompress_file(__int64 a1, _QWORD *a2)
{
__int64 v2; // r12
char *v3; // r13
__int64 v4; // rbx
size_t v5; // rbp
int v6; // eax
char *v7; // rax
int v8; // eax

if ( a1 )
{
v2 = gzopen(a1, &unk_181);
if ( v2 )
{
v3 = (char *)malloc(0x10000uLL);
if ( v3 )
{
v4 = 0LL;
v5 = 0x10000LL;
while ( 1 )
{
v6 = gzread(v2, &v3[v4], (unsigned int)(v5 - v4));
if ( v6 < 0LL )
break;
v4 += v6;
if ( v6 <= 0LL )
goto LABEL_15;
if ( v5 == v4 )
{
v5 *= 2LL;
v7 = (char *)realloc(v3, v5);
if ( !v7 )
goto LABEL_17;
v3 = v7;
}
}
v8 = *_errno_location();
if ( v8 != 4 && v8 != 11 )
{
LABEL_17:
free(v3);
gzclose(v2);
return 0LL;
}
LABEL_15:
if ( (unsigned int)gzclose(v2) )
{
free(v3);
v2 = 0LL;
}
else
{
*a2 = v4;
v2 = (__int64)v3;
}
}
else
{
gzclose(v2);
v2 = 0LL;
}
}
}
else
{
*a2 = 0LL;
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
__int64 v2; // r12
char *v3; // r13
__int64 v4; // rbx
size_t v5; // rbp
int v6; // eax
char *v7; // rax
int v8; // eax

if ( a1 )
{
v2 = gzopen(a1, &unk_181);
if ( v2 )
{
v3 = (char *)malloc(0x10000uLL);
if ( v3 )
{
v4 = 0LL;
v5 = 0x10000LL;
while ( 1 )
{
v6 = gzread(v2, &v3[v4], (unsigned int)(v5 - v4));
if ( v6 < 0LL )
break;
v4 += v6;
if ( v6 <= 0LL )
goto LABEL_15;
if ( v5 == v4 )
{
v5 *= 2LL;
v7 = (char *)realloc(v3, v5);
if ( !v7 )
goto LABEL_17;
v3 = v7;
}
}
v8 = *_errno_location();
if ( v8 != 4 && v8 != 11 )
{
LABEL_17:
free(v3);
gzclose(v2);
return 0LL;
}
LABEL_15:
if ( (unsigned int)gzclose(v2) )
{
free(v3);
v2 = 0LL;
}
else
{
*a2 = v4;
v2 = (__int64)v3;
}
}
else
{
gzclose(v2);
v2 = 0LL;
}
}
}
else
{
*a2 = 0LL;
v2 = 0LL;
}
return v2;
}

original_code:
char *zlib_decompress_file(const char *filename, off_t *r_size)
{
 gzFile fp;
 char *buf,*tmp;
 off_t size, allocated;
 ssize_t result;
 if (!filename) {
  *r_size = 0;
  return 0;
 }
 fp = gzopen(filename, "rb");
 if (fp == 0) {
  return ((void *)0);
 }
 size = 0;
 allocated = 65536;
 buf = malloc(allocated);
 if(!buf)
 {
  gzclose(fp);
  return ((void *)0);
 }
 do {
  if (size == allocated) {
   allocated <<= 1;
   tmp = realloc(buf, allocated);
   if(!tmp)
   {
    free(buf);
    gzclose(fp);
    return ((void *)0);
   }
   buf = tmp;
  }
  result = gzread(fp, buf + size, allocated - size);
  if (result < 0) {
   if (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11))
    continue;
   free(buf);
   gzclose(fp);
   return ((void *)0);
  }
  size += result;
 } while(result > 0);
 result = gzclose(fp);
 if (result != 0) {
  free(buf);
  return ((void *)0);
 }
 *r_size = size;
 return buf;
}

canonical_original_code:
char *zlib_decompress_file(char *filename, long *r_size)
{
 struct gzFile_s *fp;
 char *buf; char *tmp;
 long size; long allocated;
 long result;
 if (!filename) {
  *r_size = 0;
  return 0;
 }
 fp = gzopen(filename, "rb");
 if (fp == 0) {
  return ((void *)0);
 }
 size = 0;
 allocated = 65536;
 buf = malloc(allocated);
 if(!buf)
 {
  gzclose(fp);
  return ((void *)0);
 }
 do {
  if (size == allocated) {
   allocated <<= 1;
   tmp = realloc(buf, allocated);
   if(!tmp)
   {
    free(buf);
    gzclose(fp);
    return ((void *)0);
   }
   buf = tmp;
  }
  result = gzread(fp, buf + size, allocated - size);
  if (result < 0) {
   if (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11))
    continue;
   free(buf);
   gzclose(fp);
   return ((void *)0);
  }
  size += result;
 } while(result > 0);
 result = gzclose(fp);
 if (result != 0) {
  free(buf);
  return ((void *)0);
 }
 *r_size = size;
 return buf;
}

variable_types:
filename
char *

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *

variable_types:
buf
char *

variable_types:
tmp
char *

variable_types: size long

variable_types: allocated long

variable_types: result long

return_type: char *


decompiled_code:
__int64 __fastcall read_first_bytes_of_archive(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rbp
int v5; // ebx
int v6; // eax

v4 = gzopen(a1, &unk_181);
if ( !v4 )
return 0xFFFFFFFFLL;
do
{
v5 = gzread(v4, a2, a3);
if ( v5 >= 0 )
break;
v6 = *_errno_location();
}
while ( v6 == 4 || v6 == 11 );
gzclose(v4);
return (unsigned int)-(a3 != v5);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rbp
int v5; // ebx
int v6; // eax

v4 = gzopen(a1, &unk_181);
if ( !v4 )
return 0xFFFFFFFFLL;
do
{
v5 = gzread(v4, a2, a3);
if ( v5 >= 0 )
break;
v6 = *_errno_location();
}
while ( v6 == 4 || v6 == 11 );
gzclose(v4);
return (unsigned int)-(a3 != v5);
}

original_code:
int read_first_bytes_of_archive(char *file, char *dest, int len)
{
   gzFile fp;
   int ret;
   fp = gzopen(file, "rb");
   if(!fp)
     return -1;
   do
   {
     ret = gzread(fp,dest,len);
   } while( ret < 0 && (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11)));
   gzclose(fp);
   if ( ret == len )
     return 0;
   return -1;
}

canonical_original_code:
int read_first_bytes_of_archive(char *file, char *dest, int len)
{
   struct gzFile_s *fp;
   int ret;
   fp = gzopen(file, "rb");
   if(!fp)
     return -1;
   do
   {
     ret = gzread(fp,dest,len);
   } while( ret < 0 && (((*__errno_location ()) == 4) || ((*__errno_location ()) == 11)));
   gzclose(fp);
   if ( ret == len )
     return 0;
   return -1;
}

variable_types: len int

variable_types: ret int

user_defined_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; }


decompiled_code:
void __fastcall free_entry(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
v6 = (void *)*((_QWORD *)ptr + 5);
if ( v6 )
free(v6);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi
void *v6; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
v6 = (void *)*((_QWORD *)ptr + 5);
if ( v6 )
free(v6);
free(ptr);
}

original_code:
void free_entry(menu_entry *item)
{
 if(item->name)
  free(item->name);
 if(item->blkdev)
  free(item->blkdev);
 if(item->kernel)
  free(item->kernel);
 if(item->cmdline)
  free(item->cmdline);
 if(item->initrd)
  free(item->initrd);
 free(item);
}

canonical_original_code:
void free_entry(struct _menu_entry *item)
{
 if(item->name)
  free(item->name);
 if(item->blkdev)
  free(item->blkdev);
 if(item->kernel)
  free(item->kernel);
 if(item->cmdline)
  free(item->cmdline);
 if(item->initrd)
  free(item->initrd);
 free(item);
}

variable_types: struct _menu_entry { unsigned int id; char *name; struct _menu_entry *next; } *