decompiled_code:
_QWORD *CKI_FunctionList_New()
{
_QWORD *result; // rax

log_printf("CKI_FunctionList_New: entered\n");
result = malloc(0x220uLL);
if ( result )
{
result[1] = 0LL;
*(_WORD *)result = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
result[7] = 0LL;
result[8] = 0LL;
result[9] = 0LL;
result[10] = 0LL;
result[11] = 0LL;
result[12] = 0LL;
result[13] = 0LL;
result[14] = 0LL;
result[15] = 0LL;
result[16] = 0LL;
result[17] = 0LL;
result[18] = 0LL;
result[19] = 0LL;
result[20] = 0LL;
result[21] = 0LL;
result[22] = 0LL;
result[23] = 0LL;
result[24] = 0LL;
result[25] = 0LL;
result[26] = 0LL;
result[27] = 0LL;
result[28] = 0LL;
result[29] = 0LL;
result[30] = 0LL;
result[31] = 0LL;
result[32] = 0LL;
result[33] = 0LL;
result[34] = 0LL;
result[35] = 0LL;
result[36] = 0LL;
result[37] = 0LL;
result[38] = 0LL;
result[39] = 0LL;
result[40] = 0LL;
result[41] = 0LL;
result[42] = 0LL;
result[43] = 0LL;
result[44] = 0LL;
result[45] = 0LL;
result[46] = 0LL;
result[47] = 0LL;
result[48] = 0LL;
result[49] = 0LL;
result[50] = 0LL;
result[51] = 0LL;
result[52] = 0LL;
result[53] = 0LL;
result[54] = 0LL;
result[55] = 0LL;
result[56] = 0LL;
result[57] = 0LL;
result[58] = 0LL;
result[59] = 0LL;
result[60] = 0LL;
result[61] = 0LL;
result[62] = 0LL;
result[63] = 0LL;
result[64] = 0LL;
result[65] = 0LL;
result[66] = 0LL;
result[67] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func16()
{
_QWORD *result; // rax

log_printf("CKI_FunctionList_New: entered\n");
result = malloc(0x220uLL);
if ( result )
{
result[1] = 0LL;
*(_WORD *)result = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
result[7] = 0LL;
result[8] = 0LL;
result[9] = 0LL;
result[10] = 0LL;
result[11] = 0LL;
result[12] = 0LL;
result[13] = 0LL;
result[14] = 0LL;
result[15] = 0LL;
result[16] = 0LL;
result[17] = 0LL;
result[18] = 0LL;
result[19] = 0LL;
result[20] = 0LL;
result[21] = 0LL;
result[22] = 0LL;
result[23] = 0LL;
result[24] = 0LL;
result[25] = 0LL;
result[26] = 0LL;
result[27] = 0LL;
result[28] = 0LL;
result[29] = 0LL;
result[30] = 0LL;
result[31] = 0LL;
result[32] = 0LL;
result[33] = 0LL;
result[34] = 0LL;
result[35] = 0LL;
result[36] = 0LL;
result[37] = 0LL;
result[38] = 0LL;
result[39] = 0LL;
result[40] = 0LL;
result[41] = 0LL;
result[42] = 0LL;
result[43] = 0LL;
result[44] = 0LL;
result[45] = 0LL;
result[46] = 0LL;
result[47] = 0LL;
result[48] = 0LL;
result[49] = 0LL;
result[50] = 0LL;
result[51] = 0LL;
result[52] = 0LL;
result[53] = 0LL;
result[54] = 0LL;
result[55] = 0LL;
result[56] = 0LL;
result[57] = 0LL;
result[58] = 0LL;
result[59] = 0LL;
result[60] = 0LL;
result[61] = 0LL;
result[62] = 0LL;
result[63] = 0LL;
result[64] = 0LL;
result[65] = 0LL;
result[66] = 0LL;
result[67] = 0LL;
}
return result;
}

original_code:
CK_FUNCTION_LIST_PTR CKI_FunctionList_New() {
    CK_FUNCTION_LIST_PTR pFunctionList;
    log_printf("CKI_FunctionList_New: entered\n");
    pFunctionList=(CK_FUNCTION_LIST_PTR)malloc(sizeof(CK_FUNCTION_LIST));
    if (pFunctionList==((void *)0)) {
 return(((void *)0));
    }
    pFunctionList->version.major=0;
    pFunctionList->version.minor=0;
    pFunctionList->C_Initialize=((void *)0);
    pFunctionList->C_Finalize=((void *)0);
    pFunctionList->C_GetInfo=((void *)0);
    pFunctionList->C_GetFunctionList=((void *)0);
    pFunctionList->C_GetSlotList=((void *)0);
    pFunctionList->C_GetSlotInfo=((void *)0);
    pFunctionList->C_GetTokenInfo=((void *)0);
    pFunctionList->C_GetMechanismList=((void *)0);
    pFunctionList->C_GetMechanismInfo=((void *)0);
    pFunctionList->C_InitToken=((void *)0);
    pFunctionList->C_InitPIN=((void *)0);
    pFunctionList->C_SetPIN=((void *)0);
    pFunctionList->C_OpenSession=((void *)0);
    pFunctionList->C_CloseSession=((void *)0);
    pFunctionList->C_CloseAllSessions=((void *)0);
    pFunctionList->C_GetSessionInfo=((void *)0);
    pFunctionList->C_GetOperationState=((void *)0);
    pFunctionList->C_SetOperationState=((void *)0);
    pFunctionList->C_Login=((void *)0);
    pFunctionList->C_Logout=((void *)0);
    pFunctionList->C_CreateObject=((void *)0);
    pFunctionList->C_CopyObject=((void *)0);
    pFunctionList->C_DestroyObject=((void *)0);
    pFunctionList->C_GetObjectSize=((void *)0);
    pFunctionList->C_GetAttributeValue=((void *)0);
    pFunctionList->C_SetAttributeValue=((void *)0);
    pFunctionList->C_FindObjectsInit=((void *)0);
    pFunctionList->C_FindObjects=((void *)0);
    pFunctionList->C_FindObjectsFinal=((void *)0);
    pFunctionList->C_EncryptInit=((void *)0);
    pFunctionList->C_Encrypt=((void *)0);
    pFunctionList->C_EncryptUpdate=((void *)0);
    pFunctionList->C_EncryptFinal=((void *)0);
    pFunctionList->C_DecryptInit=((void *)0);
    pFunctionList->C_Decrypt=((void *)0);
    pFunctionList->C_DecryptUpdate=((void *)0);
    pFunctionList->C_DecryptFinal=((void *)0);
    pFunctionList->C_DigestInit=((void *)0);
    pFunctionList->C_Digest=((void *)0);
    pFunctionList->C_DigestUpdate=((void *)0);
    pFunctionList->C_DigestKey=((void *)0);
    pFunctionList->C_DigestFinal=((void *)0);
    pFunctionList->C_SignInit=((void *)0);
    pFunctionList->C_Sign=((void *)0);
    pFunctionList->C_SignUpdate=((void *)0);
    pFunctionList->C_SignFinal=((void *)0);
    pFunctionList->C_SignRecoverInit=((void *)0);
    pFunctionList->C_SignRecover=((void *)0);
    pFunctionList->C_VerifyInit=((void *)0);
    pFunctionList->C_Verify=((void *)0);
    pFunctionList->C_VerifyUpdate=((void *)0);
    pFunctionList->C_VerifyFinal=((void *)0);
    pFunctionList->C_VerifyRecoverInit=((void *)0);
    pFunctionList->C_VerifyRecover=((void *)0);
    pFunctionList->C_DigestEncryptUpdate=((void *)0);
    pFunctionList->C_DecryptDigestUpdate=((void *)0);
    pFunctionList->C_SignEncryptUpdate=((void *)0);
    pFunctionList->C_DecryptVerifyUpdate=((void *)0);
    pFunctionList->C_GenerateKey=((void *)0);
    pFunctionList->C_GenerateKeyPair=((void *)0);
    pFunctionList->C_WrapKey=((void *)0);
    pFunctionList->C_UnwrapKey=((void *)0);
    pFunctionList->C_DeriveKey=((void *)0);
    pFunctionList->C_SeedRandom=((void *)0);
    pFunctionList->C_GenerateRandom=((void *)0);
    pFunctionList->C_GetFunctionStatus=((void *)0);
    pFunctionList->C_CancelFunction=((void *)0);
    return(pFunctionList);
}

canonical_original_code:
struct CK_FUNCTION_LIST *CKI_FunctionList_New() {
    struct CK_FUNCTION_LIST *pFunctionList;
    log_printf("CKI_FunctionList_New: entered\n");
    pFunctionList=(struct CK_FUNCTION_LIST *)malloc(sizeof(CK_FUNCTION_LIST));
    if (pFunctionList==((void *)0)) {
 return(((void *)0));
    }
    pFunctionList->version.major=0;
    pFunctionList->version.minor=0;
    pFunctionList->C_Initialize=((void *)0);
    pFunctionList->C_Finalize=((void *)0);
    pFunctionList->C_GetInfo=((void *)0);
    pFunctionList->C_GetFunctionList=((void *)0);
    pFunctionList->C_GetSlotList=((void *)0);
    pFunctionList->C_GetSlotInfo=((void *)0);
    pFunctionList->C_GetTokenInfo=((void *)0);
    pFunctionList->C_GetMechanismList=((void *)0);
    pFunctionList->C_GetMechanismInfo=((void *)0);
    pFunctionList->C_InitToken=((void *)0);
    pFunctionList->C_InitPIN=((void *)0);
    pFunctionList->C_SetPIN=((void *)0);
    pFunctionList->C_OpenSession=((void *)0);
    pFunctionList->C_CloseSession=((void *)0);
    pFunctionList->C_CloseAllSessions=((void *)0);
    pFunctionList->C_GetSessionInfo=((void *)0);
    pFunctionList->C_GetOperationState=((void *)0);
    pFunctionList->C_SetOperationState=((void *)0);
    pFunctionList->C_Login=((void *)0);
    pFunctionList->C_Logout=((void *)0);
    pFunctionList->C_CreateObject=((void *)0);
    pFunctionList->C_CopyObject=((void *)0);
    pFunctionList->C_DestroyObject=((void *)0);
    pFunctionList->C_GetObjectSize=((void *)0);
    pFunctionList->C_GetAttributeValue=((void *)0);
    pFunctionList->C_SetAttributeValue=((void *)0);
    pFunctionList->C_FindObjectsInit=((void *)0);
    pFunctionList->C_FindObjects=((void *)0);
    pFunctionList->C_FindObjectsFinal=((void *)0);
    pFunctionList->C_EncryptInit=((void *)0);
    pFunctionList->C_Encrypt=((void *)0);
    pFunctionList->C_EncryptUpdate=((void *)0);
    pFunctionList->C_EncryptFinal=((void *)0);
    pFunctionList->C_DecryptInit=((void *)0);
    pFunctionList->C_Decrypt=((void *)0);
    pFunctionList->C_DecryptUpdate=((void *)0);
    pFunctionList->C_DecryptFinal=((void *)0);
    pFunctionList->C_DigestInit=((void *)0);
    pFunctionList->C_Digest=((void *)0);
    pFunctionList->C_DigestUpdate=((void *)0);
    pFunctionList->C_DigestKey=((void *)0);
    pFunctionList->C_DigestFinal=((void *)0);
    pFunctionList->C_SignInit=((void *)0);
    pFunctionList->C_Sign=((void *)0);
    pFunctionList->C_SignUpdate=((void *)0);
    pFunctionList->C_SignFinal=((void *)0);
    pFunctionList->C_SignRecoverInit=((void *)0);
    pFunctionList->C_SignRecover=((void *)0);
    pFunctionList->C_VerifyInit=((void *)0);
    pFunctionList->C_Verify=((void *)0);
    pFunctionList->C_VerifyUpdate=((void *)0);
    pFunctionList->C_VerifyFinal=((void *)0);
    pFunctionList->C_VerifyRecoverInit=((void *)0);
    pFunctionList->C_VerifyRecover=((void *)0);
    pFunctionList->C_DigestEncryptUpdate=((void *)0);
    pFunctionList->C_DecryptDigestUpdate=((void *)0);
    pFunctionList->C_SignEncryptUpdate=((void *)0);
    pFunctionList->C_DecryptVerifyUpdate=((void *)0);
    pFunctionList->C_GenerateKey=((void *)0);
    pFunctionList->C_GenerateKeyPair=((void *)0);
    pFunctionList->C_WrapKey=((void *)0);
    pFunctionList->C_UnwrapKey=((void *)0);
    pFunctionList->C_DeriveKey=((void *)0);
    pFunctionList->C_SeedRandom=((void *)0);
    pFunctionList->C_GenerateRandom=((void *)0);
    pFunctionList->C_GetFunctionStatus=((void *)0);
    pFunctionList->C_CancelFunction=((void *)0);
    return(pFunctionList);
}

return_type: struct CK_FUNCTION_LIST { struct CK_VERSION { unsigned char major; unsigned char minor; } version; unsigned long (*C_Initialize)(void *pReserved); unsigned long (*C_Finalize)(void *pReserved); unsigned long (*C_GetInfo)(struct CK_INFO { struct CK_VERSION cryptokiVersion; unsigned char manufacturerID[32]; unsigned long flags; unsigned char libraryDescription[32]; struct CK_VERSION libraryVersion; } *pInfo); unsigned long (*C_GetFunctionList)(struct CK_FUNCTION_LIST **ppFunctionList); unsigned long (*C_GetSlotList)(unsigned char tokenPresent, unsigned long *pSlotList, unsigned long *pulCount); unsigned long (*C_GetSlotInfo)(unsigned long slotID, struct CK_SLOT_INFO { unsigned char slotDescription[64]; unsigned char manufacturerID[32]; unsigned long flags; struct CK_VERSION hardwareVersion; struct CK_VERSION firmwareVersion; } *pInfo); unsigned long (*C_GetTokenInfo)(unsigned long slotID, struct CK_TOKEN_INFO { unsigned char label[32]; unsigned char manufacturerID[32]; unsigned char model[16]; unsigned char serialNumber[16]; unsigned long flags; unsigned long ulMaxSessionCount; unsigned long ulSessionCount; unsigned long ulMaxRwSessionCount; unsigned long ulRwSessionCount; unsigned long ulMaxPinLen; unsigned long ulMinPinLen; unsigned long ulTotalPublicMemory; unsigned long ulFreePublicMemory; unsigned long ulTotalPrivateMemory; unsigned long ulFreePrivateMemory; struct CK_VERSION hardwareVersion; struct CK_VERSION firmwareVersion; unsigned char utcTime[16]; } *pInfo); unsigned long (*C_GetMechanismList)(unsigned long slotID, unsigned short *pMechanismList, unsigned long *pulCount); unsigned long (*C_GetMechanismInfo)(unsigned long slotID, unsigned short type, struct CK_MECHANISM_INFO { unsigned long ulMinKeySize; unsigned long ulMaxKeySize; unsigned long flags; } *pInfo); unsigned long (*C_InitToken)(unsigned long slotID, unsigned char *pPin, unsigned long ulPinLen, unsigned char *pLabel); unsigned long (*C_InitPIN)(unsigned long hSession, unsigned char *pPin, unsigned long ulPinLen); unsigned long (*C_SetPIN)(unsigned long hSession, unsigned char *pOldPin, unsigned long ulOldLen, unsigned char *pNewPin, unsigned long ulNewLen); unsigned long (*C_OpenSession)(unsigned long slotID, unsigned long flags, void *pApplication, unsigned long *Notify, unsigned long *phSession); unsigned long (*C_CloseSession)(unsigned long hSession); unsigned long (*C_CloseAllSessions)(unsigned long slotID); unsigned long (*C_GetSessionInfo)(unsigned long hSession, struct CK_SESSION_INFO { unsigned long slotID; unsigned long state; unsigned long flags; unsigned long ulDeviceError; } *pInfo); unsigned long (*C_GetOperationState)(unsigned long hSession, unsigned char *pOperationState, unsigned long *pulOperationStateLen); unsigned long (*C_SetOperationState)(unsigned long hSession, unsigned char *pOperationState, unsigned long ulOperationStateLen, unsigned long hEncryptionKey, unsigned long hAuthenticationKey); unsigned long (*C_Login)(unsigned long hSession, unsigned long userType, unsigned char *pPin, unsigned long ulPinLen); unsigned long (*C_Logout)(unsigned long hSession); unsigned long (*C_CreateObject)(unsigned long hSession, struct CK_ATTRIBUTE { unsigned long type; void *value; unsigned long ulValueLen; } *pTemplate, unsigned long ulCount, unsigned long *pObject); unsigned long (*C_CopyObject)(unsigned long hSession, unsigned long hObject, struct CK_ATTRIBUTE *pTemplate, unsigned long ulCount, unsigned long *pNewObject); unsigned long (*C_DestroyObject)(unsigned long hSession, unsigned long hObject); unsigned long (*C_GetObjectSize)(unsigned long hSession, unsigned long hObject, unsigned long *pulSize); unsigned long (*C_GetAttributeValue)(unsigned long hSession, unsigned long hObject, struct CK_ATTRIBUTE *pTemplate, unsigned long ulCount); unsigned long (*C_SetAttributeValue)(unsigned long hSession, unsigned long hObject, struct CK_ATTRIBUTE *pTemplate, unsigned long ulCount); unsigned long (*C_FindObjectsInit)(unsigned long hSession, struct CK_ATTRIBUTE *pTemplate, unsigned long ulCount); unsigned long (*C_FindObjects)(unsigned long hSession, unsigned long *phObject, unsigned long ulMaxObjectCount, unsigned long *pulObjectCount); unsigned long (*C_FindObjectsFinal)(unsigned long hSession); unsigned long (*C_EncryptInit)(unsigned long hSession, struct CK_MECHANISM { unsigned short mechanism; void *pParameter; unsigned long ulParameterLen; } *pMechanism, unsigned long hKey); unsigned long (*C_Encrypt)(unsigned long hSession, unsigned char *pData, unsigned long ulDataLen, unsigned char *pEncryptedData, unsigned long *pulEncryptedDataLen); unsigned long (*C_EncryptUpdate)(unsigned long hSession, unsigned char *pPart, unsigned long ulPartLen, unsigned char *pEncryptedPart, unsigned long *pulEncryptedPartLen); unsigned long (*C_EncryptFinal)(unsigned long hSession, unsigned char *pLastEncryptedPart, unsigned long *pulLastEncyptedPartLen); unsigned long (*C_DecryptInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hKey); unsigned long (*C_Decrypt)(unsigned long hSession, unsigned char *pEncryptedData, unsigned long ulEncryptedDataLen, unsigned long *pulDataLen); unsigned long (*C_DecryptUpdate)(unsigned long hSession, unsigned char *pEncryptedPart, unsigned long ulEncryptedPartLen, unsigned char *pPart, unsigned long *pulPartLen); unsigned long (*C_DecryptFinal)(unsigned long hSession, unsigned char *pLastPart, unsigned long *pulLastPartLen); unsigned long (*C_DigestInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism); unsigned long (*C_Digest)(unsigned long hSession, unsigned char *pData, unsigned long ulDataLen, unsigned char *pDigest, unsigned long *pulDigestLen); unsigned long (*C_DigestUpdate)(unsigned long hSession, unsigned char *pPart, unsigned long ulPartLen); unsigned long (*C_DigestKey)(unsigned long hSession, unsigned long hKey); unsigned long (*C_DigestFinal)(unsigned long hSession, unsigned char *pDigest, unsigned long *pulDigestLen); unsigned long (*C_SignInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hKey); unsigned long (*C_Sign)(unsigned long hSession, unsigned char *pData, unsigned long ulDataLen, unsigned char *pSignature, unsigned long *pulSignatureLen); unsigned long (*C_SignUpdate)(unsigned long hSession, unsigned char *pData, unsigned long ulPartLen); unsigned long (*C_SignFinal)(unsigned long hSession, unsigned char *pSignature, unsigned long *pulSignatureLen); unsigned long (*C_SignRecoverInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hKey); unsigned long (*C_SignRecover)(unsigned long hSession, unsigned char *pData, unsigned long ulDataLen, unsigned char *pSignature, unsigned long *pulSignature); unsigned long (*C_VerifyInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hKey); unsigned long (*C_Verify)(unsigned long hSession, unsigned char *pData, unsigned long ulDataLen, unsigned char *pSignature, unsigned long ulSignatureLen); unsigned long (*C_VerifyUpdate)(unsigned long hSession, unsigned char *pPart, unsigned long ulPartLen); unsigned long (*C_VerifyFinal)(unsigned long hSession, unsigned char *pSignature, unsigned long ulSignatureLen); unsigned long (*C_VerifyRecoverInit)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hKey); unsigned long (*C_VerifyRecover)(unsigned long hSession, unsigned char *pSignature, unsigned long ulSignatureLen, unsigned char *pData, unsigned long *pulDataLen); unsigned long (*C_DigestEncryptUpdate)(unsigned long hSession, unsigned char *pPart, unsigned long ulPartLen, unsigned char *pEncryptedPart, unsigned long *pulEncryptedPartLen); unsigned long (*C_DecryptDigestUpdate)(unsigned long hSession, unsigned char *pEncryptedPart, unsigned long pEncryptedPartLen, unsigned char *pPart, unsigned long *pulPartLen); unsigned long (*C_SignEncryptUpdate)(unsigned long hSession, unsigned char *pPart, unsigned long ulPartLen, unsigned char *pEncryptedPart, unsigned long *pulEncryptedPartLen); unsigned long (*C_DecryptVerifyUpdate)(unsigned long hSession, unsigned char *pEncryptedPart, unsigned long ulEncryptedPartLen, unsigned char *pPart, unsigned long *pulPartLen); unsigned long (*C_GenerateKey)(unsigned long hSession, struct CK_MECHANISM *pMechanism, struct CK_ATTRIBUTE *pTemplate, unsigned long ulCount, unsigned long *phKey); unsigned long (*C_GenerateKeyPair)(unsigned long hSession, struct CK_MECHANISM *pMechanism, struct CK_ATTRIBUTE *pPublicKeyTemplate, unsigned long ulPublicKeyAttributeCount, struct CK_ATTRIBUTE *pPrivateKeyTemplate, unsigned long ulPrivateKeyAttributeCount, unsigned long *phPublicKey, unsigned long *phPrivateKey); unsigned long (*C_WrapKey)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hWrappingKey, unsigned long hKey, unsigned char *pWrappedKey, unsigned long *pulWrappedKeyLen); unsigned long (*C_UnwrapKey)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hUnwrappingKey, unsigned char *pWrappedKey, unsigned long ulWrappedKeyLen, struct CK_ATTRIBUTE *pTemplate, unsigned long ulAttributeCount, unsigned long *phKey); unsigned long (*C_DeriveKey)(unsigned long hSession, struct CK_MECHANISM *pMechanism, unsigned long hBaseKey, struct CK_ATTRIBUTE *pTemplate, unsigned long ulAttributeCount, unsigned long *phKey); unsigned long (*C_SeedRandom)(unsigned long hSession, unsigned char *pSeed, unsigned long ulSeedLen); unsigned long (*C_GenerateRandom)(unsigned long hSession, unsigned char *pRandomData, unsigned long ulRandomLen); unsigned long (*C_GetFunctionStatus)(unsigned long hSession); unsigned long (*C_CancelFunction)(unsigned long hSession); } *


repo: secure-endpoints/kcacred

decompiled_code:
void __fastcall CKI_AttributePtr_Free(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v3 += 3;
*(v3 - 3) = 0LL;
v2 = *v3;
}
while ( *v3 );
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func17(void *ptr)
{
void *v2; // rdi
void **v3; // rbx

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v3 += 3;
*(v3 - 3) = 0LL;
v2 = *v3;
}
while ( *v3 );
}
free(ptr);
}
}

original_code:
void CKI_AttributePtr_Free(CK_ATTRIBUTE_PTR pAttribute) {
    int i;
    if (!pAttribute)
 return;
    for (i=0; pAttribute[i].value; i++) {
 free(pAttribute[i].value);
 pAttribute[i].value = ((void *)0);
    }
    free(pAttribute);
    return;
}

canonical_original_code:
void CKI_AttributePtr_Free(struct CK_ATTRIBUTE *pAttribute) {
    int i;
    if (!pAttribute)
 return;
    for (i=0; pAttribute[i].value; i++) {
 free(pAttribute[i].value);
 pAttribute[i].value = ((void *)0);
    }
    free(pAttribute);
    return;
}

variable_types: struct CK_ATTRIBUTE { unsigned long type; void *value; unsigned long ulValueLen; } *

variable_types: i int

return_type: void


decompiled_code:
__int64 __fastcall connect_x509(const char *a1, unsigned __int16 a2)
{
struct hostent *v2; // rax
struct hostent *v3; // rbx
int v4; // er12
socklen_t v5; // edx
__int64 result; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
__int64 optval; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-28h] BYREF

optval = 0x100000001LL;
v2 = gethostbyname(a1);
if ( v2 )
{
v3 = v2;
log_printf("%s: Host official name: %s\n", "connect_x509", v2->h_name);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_printf("%s: Failed creating socket\n%s\n", "connect_x509", v8);
result = 0LL;
}
else
{
log_printf("%s: Issuing connect to port %d\n", "connect_x509", a2);
v14.sa_family = 2;
*(_WORD *)v14.sa_data = _bswap_16(a2);
*(_DWORD *)&v14.sa_data[2] = **(_DWORD **)v3->h_addr_list;
if ( connect(v4, &v14, v5) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_printf("%s: Failed connecting socket\n%s\n", "connect_x509", v10);
result = 0LL;
}
else
{
if ( setsockopt(v4, 1, 13, &optval, 8u) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_printf("%s: Unable to set linger option on socket\n%s\n", "connect_x509", v12);
}
result = (unsigned int)v4;
}
}
}
else
{
log_printf("%s: unknown host\n", "connect_x509");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, unsigned __int16 a2)
{
struct hostent *v2; // rax
struct hostent *v3; // rbx
int v4; // er12
socklen_t v5; // edx
__int64 result; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
__int64 optval; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-28h] BYREF

optval = 0x100000001LL;
v2 = gethostbyname(a1);
if ( v2 )
{
v3 = v2;
log_printf("%s: Host official name: %s\n", "connect_x509", v2->h_name);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_printf("%s: Failed creating socket\n%s\n", "connect_x509", v8);
result = 0LL;
}
else
{
log_printf("%s: Issuing connect to port %d\n", "connect_x509", a2);
v14.sa_family = 2;
*(_WORD *)v14.sa_data = func0(a2);
*(_DWORD *)&v14.sa_data[2] = **(_DWORD **)v3->h_addr_list;
if ( connect(v4, &v14, v5) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
log_printf("%s: Failed connecting socket\n%s\n", "connect_x509", v10);
result = 0LL;
}
else
{
if ( setsockopt(v4, 1, 13, &optval, 8u) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
log_printf("%s: Unable to set linger option on socket\n%s\n", "connect_x509", v12);
}
result = (unsigned int)v4;
}
}
}
else
{
log_printf("%s: unknown host\n", "connect_x509");
result = 0LL;
}
return result;
}

original_code:
SOCKET
connect_x509(char *hostname, u_short port_no)
{
    char *rn = "connect_x509";
    struct sockaddr_in peeraddr;
    struct hostent *phostent;
    int optrc;
    SOCKET s;
    struct linger linger = {1, 1};
    phostent = gethostbyname (hostname);
    if( phostent == ((void *)0))
    {
 log_printf("%s: unknown host\n", rn);
 return 0;
    }
    log_printf("%s: Host official name: %s\n", rn, phostent->h_name);
    if ((s = socket(2, SOCK_STREAM, 0)) < 0)
    {
 log_printf("%s: Failed creating socket\n%s\n",
      rn, strerror((*__errno_location ())));
 return 0;
    }
    log_printf("%s: Issuing connect to port %d\n", rn, port_no);
    peeraddr.sin_family = 2;
    peeraddr.sin_port = htons(port_no);
    peeraddr.sin_addr.s_addr = ((struct in_addr *)(phostent->h_addr_list[0]))->s_addr;
    if (connect(s, (struct sockaddr *) &peeraddr, sizeof(struct sockaddr_in)) == -1)
    {
 log_printf("%s: Failed connecting socket\n%s\n",
      rn, strerror((*__errno_location ())));
 return 0;
    }
    optrc = setsockopt (s, 1, 13,
    (char *) &linger, sizeof (struct linger));
    if (optrc == -1)
 log_printf("%s: Unable to set linger option on socket\n%s\n",
      rn, strerror((*__errno_location ())));
    return s;
}

canonical_original_code:
int connect_x509(char *hostname, unsigned short port_no)
{
    char *rn = "connect_x509";
    struct sockaddr_in peeraddr;
    struct hostent *phostent;
    int optrc;
    int s;
    struct linger linger = {1, 1};
    phostent = gethostbyname (hostname);
    if( phostent == ((void *)0))
    {
 log_printf("%s: unknown host\n", rn);
 return 0;
    }
    log_printf("%s: Host official name: %s\n", rn, phostent->h_name);
    if ((s = socket(2, SOCK_STREAM, 0)) < 0)
    {
 log_printf("%s: Failed creating socket\n%s\n",
      rn, strerror((*__errno_location ())));
 return 0;
    }
    log_printf("%s: Issuing connect to port %d\n", rn, port_no);
    peeraddr.sin_family = 2;
    peeraddr.sin_port = htons(port_no);
    peeraddr.sin_addr.s_addr = ((struct in_addr *)(phostent->h_addr_list[0]))->s_addr;
    if (connect(s, (struct sockaddr *) &peeraddr, sizeof(struct sockaddr_in)) == -1)
    {
 log_printf("%s: Failed connecting socket\n%s\n",
      rn, strerror((*__errno_location ())));
 return 0;
    }
    optrc = setsockopt (s, 1, 13,
    (char *) &linger, sizeof (struct linger));
    if (optrc == -1)
 log_printf("%s: Unable to set linger option on socket\n%s\n",
      rn, strerror((*__errno_location ())));
    return s;
}

variable_types:
hostname
char *

variable_types:
rn
char *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: optrc int

variable_types: s int

variable_types: struct linger { int l_onoff; int l_linger; }

return_type: int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


original_code:
time_t utc2unix(ASN1_UTCTIME *utctime, time_t *unixtime)
{
    char *utcchars;
    int length, temp;
    time_t utime;
    char *current;
    struct tm tms;
    memset(&tms, '\0', sizeof(tms));
    utime = -1;
    length = utctime->length;
    if (length != 13)
 goto returntime;
    utcchars = (char*) utctime->data;
    if (utcchars[12] != 'Z')
 goto returntime;
    current = utcchars;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    if (temp < 50)
 temp += 100;
    tms.tm_year = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    temp--;
    tms.tm_mon = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_mday = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_hour = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_min = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_sec = temp;
    tms.tm_isdst = -1;
    tzset();
    utime = mktime(&tms);
    if (utime != -1) {
 utime = utime - timezone + daylight*3600;
    }
  returntime:
    if (unixtime)
 *unixtime = utime;
    return utime;
}

canonical_original_code:
long utc2unix(struct asn1_string_st *utctime, long *unixtime)
{
    char *utcchars;
    int length; int temp;
    long utime;
    char *current;
    struct tm tms;
    memset(&tms, '\0', sizeof(tms));
    utime = -1;
    length = utctime->length;
    if (length != 13)
 goto returntime;
    utcchars = (char *) utctime->data;
    if (utcchars[12] != 'Z')
 goto returntime;
    current = utcchars;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    if (temp < 50)
 temp += 100;
    tms.tm_year = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    temp--;
    tms.tm_mon = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_mday = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_hour = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_min = temp;
    current+=2;
    temp = (current[0]-'0')*10 + (current[1]-'0');
    tms.tm_sec = temp;
    tms.tm_isdst = -1;
    tzset();
    utime = mktime(&tms);
    if (utime != -1) {
 utime = utime - timezone + daylight*3600;
    }
  returntime:
    if (unixtime)
 *unixtime = utime;
    return utime;
}

variable_types: struct asn1_string_st { int length; int type; unsigned char *data; long flags; } *

variable_types:
unixtime
long *

variable_types:
utcchars
char *

variable_types: length int

variable_types: temp int

variable_types: utime long

variable_types:
current
char *

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }

return_type: long


decompiled_code:
_QWORD *PKCS11_FindObjectsInfo_New()
{
_QWORD *result; // rax

log_printf("entering PKCS11_FindObjectsInfo_New\n");
result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
*((_BYTE *)result + 24) = 0;
}
return result;
}

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

log_printf("entering PKCS11_FindObjectsInfo_New\n");
result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
*((_BYTE *)result + 24) = 0;
}
return result;
}

original_code:
PKCS11_FINDOBJECTS_INFO * PKCS11_FindObjectsInfo_New() {
    PKCS11_FINDOBJECTS_INFO * FindObjectsInfoPtr;
    log_printf("entering PKCS11_FindObjectsInfo_New\n");
    FindObjectsInfoPtr=(PKCS11_FINDOBJECTS_INFO *)malloc(sizeof(PKCS11_FINDOBJECTS_INFO));
    if (FindObjectsInfoPtr==((void *)0)) {
 return(((void *)0));
    }
    FindObjectsInfoPtr->pTemplate=((void *)0);
    FindObjectsInfoPtr->ulAttrCount=0L;
    FindObjectsInfoPtr->TokenObjectsIndex=0L;
    FindObjectsInfoPtr->isactive=0;
    return(FindObjectsInfoPtr);
}

canonical_original_code:
struct PKCS11_FINDOBJECTS_INFO *PKCS11_FindObjectsInfo_New() {
    struct PKCS11_FINDOBJECTS_INFO *FindObjectsInfoPtr;
    log_printf("entering PKCS11_FindObjectsInfo_New\n");
    FindObjectsInfoPtr=(struct PKCS11_FINDOBJECTS_INFO *)malloc(sizeof(PKCS11_FINDOBJECTS_INFO));
    if (FindObjectsInfoPtr==((void *)0)) {
 return(((void *)0));
    }
    FindObjectsInfoPtr->pTemplate=((void *)0);
    FindObjectsInfoPtr->ulAttrCount=0L;
    FindObjectsInfoPtr->TokenObjectsIndex=0L;
    FindObjectsInfoPtr->isactive=0;
    return(FindObjectsInfoPtr);
}

variable_types: struct PKCS11_FINDOBJECTS_INFO { struct CK_ATTRIBUTE { unsigned long type; void *value; unsigned long ulValueLen; } *pTemplate; unsigned long ulAttrCount; long TokenObjectsIndex; unsigned char isactive; } *

user_defined_types: struct PKCS11_FINDOBJECTS_INFO { struct CK_ATTRIBUTE *pTemplate; unsigned long ulAttrCount; long TokenObjectsIndex; unsigned char isactive; }


decompiled_code:
_QWORD *PKCS11_SignInfo_New()
{
_QWORD *result; // rax

log_printf("entering PKCS11_SignInfo_New\n");
result = malloc(0x28uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
*((_BYTE *)result + 24) = 0;
result[4] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax

log_printf("entering PKCS11_SignInfo_New\n");
result = malloc(0x28uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
*((_BYTE *)result + 24) = 0;
result[4] = 0LL;
}
return result;
}

original_code:
PKCS11_SIGN_INFO * PKCS11_SignInfo_New() {
    PKCS11_SIGN_INFO * SignInfoPtr;
    log_printf("entering PKCS11_SignInfo_New\n");
    SignInfoPtr=(PKCS11_SIGN_INFO *)malloc(sizeof(PKCS11_SIGN_INFO));
    if (SignInfoPtr==((void *)0)) {
 return(((void *)0));
    }
    SignInfoPtr->pMechanism=((void *)0);
    SignInfoPtr->hKey=0L;
    SignInfoPtr->pSignature=((void *)0);
    SignInfoPtr->isactive=0;
    SignInfoPtr->pulSignatureLen=0L;
    return(SignInfoPtr);
}

canonical_original_code:
struct PKCS11_SIGN_INFO *PKCS11_SignInfo_New() {
    struct PKCS11_SIGN_INFO *SignInfoPtr;
    log_printf("entering PKCS11_SignInfo_New\n");
    SignInfoPtr=(struct PKCS11_SIGN_INFO *)malloc(sizeof(PKCS11_SIGN_INFO));
    if (SignInfoPtr==((void *)0)) {
 return(((void *)0));
    }
    SignInfoPtr->pMechanism=((void *)0);
    SignInfoPtr->hKey=0L;
    SignInfoPtr->pSignature=((void *)0);
    SignInfoPtr->isactive=0;
    SignInfoPtr->pulSignatureLen=0L;
    return(SignInfoPtr);
}

variable_types: struct PKCS11_SIGN_INFO { struct CK_MECHANISM { unsigned short mechanism; void *pParameter; unsigned long ulParameterLen; } *pMechanism; unsigned long hKey; unsigned char *pSignature; unsigned char isactive; unsigned long *pulSignatureLen; } *

user_defined_types: struct PKCS11_SIGN_INFO { struct CK_MECHANISM *pMechanism; unsigned long hKey; unsigned char *pSignature; unsigned char isactive; unsigned long *pulSignatureLen; }


decompiled_code:
_QWORD *PKCS11_Object_New()
{
_QWORD *result; // rax

log_printf("entering PKCS11_Object_New\n");
result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[3] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *result; // rax

log_printf("entering PKCS11_Object_New\n");
result = malloc(0x20uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[3] = 0LL;
}
return result;
}

original_code:
PKCS11_OBJECT *PKCS11_Object_New() {
    PKCS11_OBJECT *pObject;
    log_printf("entering PKCS11_Object_New\n");
    pObject=(PKCS11_OBJECT *)malloc(sizeof(PKCS11_OBJECT));
    if (pObject==((void *)0)) {
 return(((void *)0));
    }
    pObject->ulObjectHandle=0L;
    pObject->ulObjectClass=0L;
    pObject->pAttribute=((void *)0);
    return(pObject);
}

canonical_original_code:
struct PKCS11_OBJECT *PKCS11_Object_New() {
    struct PKCS11_OBJECT *pObject;
    log_printf("entering PKCS11_Object_New\n");
    pObject=(struct PKCS11_OBJECT *)malloc(sizeof(PKCS11_OBJECT));
    if (pObject==((void *)0)) {
 return(((void *)0));
    }
    pObject->ulObjectHandle=0L;
    pObject->ulObjectClass=0L;
    pObject->pAttribute=((void *)0);
    return(pObject);
}

variable_types: struct PKCS11_OBJECT { unsigned long ulObjectHandle; unsigned long ulObjectClass; unsigned long ulSessionHandle; struct CK_ATTRIBUTE { unsigned long type; void *value; unsigned long ulValueLen; } *pAttribute; } *

user_defined_types: struct PKCS11_OBJECT { unsigned long ulObjectHandle; unsigned long ulObjectClass; unsigned long ulSessionHandle; struct CK_ATTRIBUTE *pAttribute; }


decompiled_code:
void __fastcall PKCS11_FindObjectsInfo_Free(void *ptr)
{
log_printf("entering PKCS11_FindObjectsInfo_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_Attribute_Free();
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
log_printf("entering PKCS11_FindObjectsInfo_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_Attribute_Free();
free(ptr);
}
}

original_code:
void PKCS11_FindObjectsInfo_Free(PKCS11_FINDOBJECTS_INFO *FindObjectsInfoPtr) {
    log_printf("entering PKCS11_FindObjectsInfo_Free\n");
    if (FindObjectsInfoPtr==((void *)0))
 return;
    if (FindObjectsInfoPtr->pTemplate!=((void *)0))
 CKI_Attribute_Free(FindObjectsInfoPtr->pTemplate);
    free(FindObjectsInfoPtr);
    return;
}

canonical_original_code:
void PKCS11_FindObjectsInfo_Free(struct PKCS11_FINDOBJECTS_INFO *FindObjectsInfoPtr) {
    log_printf("entering PKCS11_FindObjectsInfo_Free\n");
    if (FindObjectsInfoPtr==((void *)0))
 return;
    if (FindObjectsInfoPtr->pTemplate!=((void *)0))
 CKI_Attribute_Free(FindObjectsInfoPtr->pTemplate);
    free(FindObjectsInfoPtr);
    return;
}

user_defined_types: struct CK_ATTRIBUTE { unsigned long type; void *value; unsigned long ulValueLen; }


decompiled_code:
void __fastcall PKCS11_SignInfo_Free(void *ptr)
{
void *v2; // rdi

log_printf("entering PKCS11_SignInfo_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_Mechanism_Free();
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
void *v2; // rdi

log_printf("entering PKCS11_SignInfo_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_Mechanism_Free();
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
free(ptr);
}
}

original_code:
void PKCS11_SignInfo_Free(PKCS11_SIGN_INFO *SignInfoPtr) {
    log_printf("entering PKCS11_SignInfo_Free\n");
    if (SignInfoPtr==((void *)0))
 return;
    if (SignInfoPtr->pMechanism!=((void *)0))
 CKI_Mechanism_Free(SignInfoPtr->pMechanism);
    if (SignInfoPtr->pSignature!=((void *)0))
 free(SignInfoPtr->pSignature);
    free(SignInfoPtr);
    return;
}

canonical_original_code:
void PKCS11_SignInfo_Free(struct PKCS11_SIGN_INFO *SignInfoPtr) {
    log_printf("entering PKCS11_SignInfo_Free\n");
    if (SignInfoPtr==((void *)0))
 return;
    if (SignInfoPtr->pMechanism!=((void *)0))
 CKI_Mechanism_Free(SignInfoPtr->pMechanism);
    if (SignInfoPtr->pSignature!=((void *)0))
 free(SignInfoPtr->pSignature);
    free(SignInfoPtr);
    return;
}

user_defined_types: struct CK_MECHANISM { unsigned short mechanism; void *pParameter; unsigned long ulParameterLen; }


decompiled_code:
void __fastcall PKCS11_Object_Free(void *ptr)
{
log_printf("entering PKCS11_Object_Free\n");
if ( ptr )
{
if ( *((_QWORD *)ptr + 3) )
CKI_AttributePtr_Free();
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func9(void *ptr)
{
log_printf("entering PKCS11_Object_Free\n");
if ( ptr )
{
if ( *((_QWORD *)ptr + 3) )
CKI_AttributePtr_Free();
free(ptr);
}
}

original_code:
void PKCS11_Object_Free(PKCS11_OBJECT *pObject) {
    log_printf("entering PKCS11_Object_Free\n");
    if (pObject==((void *)0))
 return;
    if (pObject->pAttribute!=((void *)0)) {
 CKI_AttributePtr_Free(pObject->pAttribute);
    }
    free(pObject);
    return;
}

canonical_original_code:
void PKCS11_Object_Free(struct PKCS11_OBJECT *pObject) {
    log_printf("entering PKCS11_Object_Free\n");
    if (pObject==((void *)0))
 return;
    if (pObject->pAttribute!=((void *)0)) {
 CKI_AttributePtr_Free(pObject->pAttribute);
    }
    free(pObject);
    return;
}


decompiled_code:
void __fastcall PKCS11_Mechanism_Free(void *ptr)
{
log_printf("entering PKCS11_Mechanism_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_MechanismInfo_Free();
if ( *((_QWORD *)ptr + 1) )
CKI_Mechanism_Free();
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func12(void *ptr)
{
log_printf("entering PKCS11_Mechanism_Free\n");
if ( ptr )
{
if ( *(_QWORD *)ptr )
CKI_MechanismInfo_Free();
if ( *((_QWORD *)ptr + 1) )
CKI_Mechanism_Free();
free(ptr);
}
}

original_code:
void PKCS11_Mechanism_Free(PKCS11_MECHANISM *pMechanism) {
    log_printf("entering PKCS11_Mechanism_Free\n");
    if (pMechanism==((void *)0)) return;
    if (pMechanism->pInfo!=((void *)0))
 CKI_MechanismInfo_Free(pMechanism->pInfo);
    if (pMechanism->pMechanism!=((void *)0))
 CKI_Mechanism_Free(pMechanism->pMechanism);
    free(pMechanism);
    return;
}

canonical_original_code:
void PKCS11_Mechanism_Free(struct PKCS11_MECHANISM *pMechanism) {
    log_printf("entering PKCS11_Mechanism_Free\n");
    if (pMechanism==((void *)0)) return;
    if (pMechanism->pInfo!=((void *)0))
 CKI_MechanismInfo_Free(pMechanism->pInfo);
    if (pMechanism->pMechanism!=((void *)0))
 CKI_Mechanism_Free(pMechanism->pMechanism);
    free(pMechanism);
    return;
}

variable_types: struct PKCS11_MECHANISM { struct CK_MECHANISM_INFO { unsigned long ulMinKeySize; unsigned long ulMaxKeySize; unsigned long flags; } *pInfo; struct CK_MECHANISM { unsigned short mechanism; void *pParameter; unsigned long ulParameterLen; } *pMechanism; } *

user_defined_types: struct PKCS11_MECHANISM { struct CK_MECHANISM_INFO *pInfo; struct CK_MECHANISM *pMechanism; }


decompiled_code:
__int64 *PKCS11_Mechanism_New()
{
__int64 *v0; // rax
__int64 *v1; // r12
__int64 v2; // rax
__int64 v3; // rax

log_printf("entering PKCS11_Mechanism_New\n");
v0 = (__int64 *)malloc(0x10uLL);
v1 = v0;
if ( !v0 )
return v1;
*v0 = 0LL;
v0[1] = 0LL;
v2 = CKI_MechanismInfo_New();
*v1 = v2;
if ( v2 )
{
v3 = CKI_Mechanism_New();
v1[1] = v3;
if ( v3 )
return v1;
}
PKCS11_Mechanism_Free(v1);
return 0LL;
}

canonical_decompiled_code:
__int64 *func13()
{
__int64 *v0; // rax
__int64 *v1; // r12
__int64 v2; // rax
__int64 v3; // rax

log_printf("entering PKCS11_Mechanism_New\n");
v0 = (__int64 *)malloc(0x10uLL);
v1 = v0;
if ( !v0 )
return v1;
*v0 = 0LL;
v0[1] = 0LL;
v2 = CKI_MechanismInfo_New();
*v1 = v2;
if ( v2 )
{
v3 = CKI_Mechanism_New();
v1[1] = v3;
if ( v3 )
return v1;
}
func12(v1);
return 0LL;
}

original_code:
PKCS11_MECHANISM *PKCS11_Mechanism_New() {
    PKCS11_MECHANISM *pMechanism;
    log_printf("entering PKCS11_Mechanism_New\n");
    pMechanism=(PKCS11_MECHANISM *)malloc(sizeof(PKCS11_MECHANISM));
    if (pMechanism==((void *)0)) {
 return(((void *)0));
    }
    pMechanism->pInfo=((void *)0);
    pMechanism->pMechanism=((void *)0);
    pMechanism->pInfo=CKI_MechanismInfo_New();
    if (pMechanism->pInfo==((void *)0)) {
 PKCS11_Mechanism_Free(pMechanism);
 return(((void *)0));
    }
    pMechanism->pMechanism=CKI_Mechanism_New();
    if (pMechanism->pMechanism==((void *)0)) {
 PKCS11_Mechanism_Free(pMechanism);
 return(((void *)0));
    }
    return(pMechanism);
}

canonical_original_code:
struct PKCS11_MECHANISM *PKCS11_Mechanism_New() {
    struct PKCS11_MECHANISM *pMechanism;
    log_printf("entering PKCS11_Mechanism_New\n");
    pMechanism=(struct PKCS11_MECHANISM *)malloc(sizeof(PKCS11_MECHANISM));
    if (pMechanism==((void *)0)) {
 return(((void *)0));
    }
    pMechanism->pInfo=((void *)0);
    pMechanism->pMechanism=((void *)0);
    pMechanism->pInfo=CKI_MechanismInfo_New();
    if (pMechanism->pInfo==((void *)0)) {
 PKCS11_Mechanism_Free(pMechanism);
 return(((void *)0));
    }
    pMechanism->pMechanism=CKI_Mechanism_New();
    if (pMechanism->pMechanism==((void *)0)) {
 PKCS11_Mechanism_Free(pMechanism);
 return(((void *)0));
    }
    return(pMechanism);
}

return_type: struct PKCS11_MECHANISM { struct CK_MECHANISM_INFO { unsigned long ulMinKeySize; unsigned long ulMaxKeySize; unsigned long flags; } *pInfo; struct CK_MECHANISM { unsigned short mechanism; void *pParameter; unsigned long ulParameterLen; } *pMechanism; } *


getelt

decompiled_code:
__int64 __fastcall getelt(__int64 *a1, const char *a2)
{
__int64 v2; // r12
__int64 *v3; // rbx

if ( !a1 )
return 0LL;
v2 = *a1;
if ( *a1 )
{
v3 = a1 + 1;
while ( strcmp(*(const char **)v2, a2) )
{
v2 = *v3++;
if ( !v2 )
return v2;
}
v2 = *(_QWORD *)(v2 + 8);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, const char *a2)
{
__int64 v2; // r12
__int64 *v3; // rbx

if ( !a1 )
return 0LL;
v2 = *a1;
if ( *a1 )
{
v3 = a1 + 1;
while ( strcmp(*(const char **)v2, a2) )
{
v2 = *v3++;
if ( !v2 )
return v2;
}
v2 = *(_QWORD *)(v2 + 8);
}
return v2;
}

original_code:
char *getelt(struct a_t **alist, char *name) {
  int i;
  if (!alist) return(((void *)0));
  i=0;
  while (alist[i]) {
   if (!strcmp(alist[i]->name,name)) return(alist[i]->value);
   i++;
  }
  return(((void *)0));
}

variable_types: struct a_t { char *name; char *value; } **

variable_types:
name
char *

variable_types: i int

return_type: char *


getCertAndKey

decompiled_code:
char *__fastcall getCertAndKey(__int64 a1, char *a2, int a3)
{
unsigned int v4; // ebp
char *v5; // rax
char *v6; // r13
unsigned int v7; // er12
char *v8; // rax
char *v9; // rbp
char *v10; // r12
signed int v11; // eax
_QWORD *v12; // rdx
signed int v13; // eax
_QWORD *v14; // rdx
char *result; // rax
unsigned int v16; // [rsp+8h] [rbp-30h] BYREF
unsigned int v17; // [rsp+Ch] [rbp-2Ch] BYREF
__int64 v18; // [rsp+10h] [rbp-28h] BYREF
__int64 v19[4]; // [rsp+18h] [rbp-20h] BYREF

v16 = 0;
v18 = 0LL;
v17 = 0;
v19[0] = 0LL;
log_printf("getCertAndKey: entered\n");
if ( (unsigned int)os_getCertAndKey(&v18, (int *)&v16, v19, &v17, a2, a3) )
{
v4 = v16;
v5 = (char *)malloc(2LL * (int)(v16 + 1));
v6 = v5;
if ( !v5 )
{
v10 = 0LL;
log_printf("getCertAndKey: out of memory\n");
return v10;
}
b64_encode(v18, v4, v5);
log_printf("getCertAndKey: cert_length=%0d\n", v16);
log_printf("getCertAndKey: cert_enc='%s'\n", v6);
log_printf("getCertAndKey: 4\n");
v7 = v17;
v8 = (char *)malloc(2LL * (int)(v17 + 1));
v9 = v8;
if ( v8 )
{
b64_encode(v19[0], v7, v8);
log_printf("getCertAndKey: key_length=%0d\n", v17);
log_printf("getCertAndKey: key_enc='%s'\n", v9);
log_printf("getCertAndKey: 5\n");
v10 = (char *)malloc(0x18uLL);
log_printf("getCertAndKey: 6\n");
if ( v10 )
{
*(_OWORD *)(v10 + 8) = 0LL;
*(_QWORD *)v10 = malloc(0x10uLL);
log_printf("getCertAndKey: 7\n");
if ( *(_QWORD *)v10 )
{
*((_QWORD *)v10 + 1) = malloc(0x10uLL);
log_printf("getCertAndKey: 8\n");
if ( *((_QWORD *)v10 + 1) )
{
log_printf("getCertAndKey: 9\n");
v11 = (unsigned int)strdup("cert");
v12 = *(_QWORD **)v10;
*v12 = v11;
v12[1] = v6;
v13 = (unsigned int)strdup("key");
v14 = (_QWORD *)*((_QWORD *)v10 + 1);
*v14 = v13;
v14[1] = v9;
log_printf("getCertAndKey: leaving\n");
return v10;
}
if ( *(_QWORD *)v10 )
free(*(void **)v10);
}
free(v10);
}
free(v9);
}
else
{
log_printf("getCertAndKey: out of memory\n");
}
free(v6);
result = 0LL;
}
else
{
log_printf("getCertAndKey: os_getCertAndKey failed\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1, char *a2, int a3)
{
unsigned int v4; // ebp
char *v5; // rax
char *v6; // r13
unsigned int v7; // er12
char *v8; // rax
char *v9; // rbp
char *v10; // r12
signed int v11; // eax
_QWORD *v12; // rdx
signed int v13; // eax
_QWORD *v14; // rdx
char *result; // rax
unsigned int v16; // [rsp+8h] [rbp-30h] BYREF
unsigned int v17; // [rsp+Ch] [rbp-2Ch] BYREF
__int64 v18; // [rsp+10h] [rbp-28h] BYREF
__int64 v19[4]; // [rsp+18h] [rbp-20h] BYREF

v16 = 0;
v18 = 0LL;
v17 = 0;
v19[0] = 0LL;
log_printf("getCertAndKey: entered\n");
if ( (unsigned int)func1(&v18, (int *)&v16, v19, &v17, a2, a3) )
{
v4 = v16;
v5 = (char *)malloc(2LL * (int)(v16 + 1));
v6 = v5;
if ( !v5 )
{
v10 = 0LL;
log_printf("getCertAndKey: out of memory\n");
return v10;
}
b64_encode(v18, v4, v5);
log_printf("getCertAndKey: cert_length=%0d\n", v16);
log_printf("getCertAndKey: cert_enc='%s'\n", v6);
log_printf("getCertAndKey: 4\n");
v7 = v17;
v8 = (char *)malloc(2LL * (int)(v17 + 1));
v9 = v8;
if ( v8 )
{
b64_encode(v19[0], v7, v8);
log_printf("getCertAndKey: key_length=%0d\n", v17);
log_printf("getCertAndKey: key_enc='%s'\n", v9);
log_printf("getCertAndKey: 5\n");
v10 = (char *)malloc(0x18uLL);
log_printf("getCertAndKey: 6\n");
if ( v10 )
{
*(_OWORD *)(v10 + 8) = 0LL;
*(_QWORD *)v10 = malloc(0x10uLL);
log_printf("getCertAndKey: 7\n");
if ( *(_QWORD *)v10 )
{
*((_QWORD *)v10 + 1) = malloc(0x10uLL);
log_printf("getCertAndKey: 8\n");
if ( *((_QWORD *)v10 + 1) )
{
log_printf("getCertAndKey: 9\n");
v11 = (unsigned int)strdup("cert");
v12 = *(_QWORD **)v10;
*v12 = v11;
v12[1] = v6;
v13 = (unsigned int)strdup("key");
v14 = (_QWORD *)*((_QWORD *)v10 + 1);
*v14 = v13;
v14[1] = v9;
log_printf("getCertAndKey: leaving\n");
return v10;
}
if ( *(_QWORD *)v10 )
free(*(void **)v10);
}
free(v10);
}
free(v9);
}
else
{
log_printf("getCertAndKey: out of memory\n");
}
free(v6);
result = 0LL;
}
else
{
log_printf("getCertAndKey: os_getCertAndKey failed\n");
result = 0LL;
}
return result;
}

original_code:
struct a_t **getCertAndKey(struct a_t **tattrl, char *name, int namelen)
{
    struct a_t **attrl=((void *)0);
    int cert_length = 0;
    char *cert_der = ((void *)0);
    char *cert_enc = ((void *)0);
    int key_length =0;
    char *key_der = ((void *)0);
    char *key_enc = ((void *)0);
    int i;
    log_printf("getCertAndKey: entered\n");
    if (!os_getCertAndKey(&cert_der, &cert_length,
      &key_der, &key_length,
      name, namelen))
    {
 log_printf("getCertAndKey: os_getCertAndKey failed\n");
 goto error;
    }
    cert_enc=(char *)malloc(sizeof(char)*(cert_length+1)*2);
    if (!cert_enc) {
 log_printf("getCertAndKey: out of memory\n");
 goto error;
    }
    b64_encode(cert_der,cert_length,cert_enc);
    log_printf("getCertAndKey: cert_length=%0d\n", cert_length);
    log_printf("getCertAndKey: cert_enc='%s'\n", cert_enc);
    log_printf("getCertAndKey: 4\n");
    key_enc=(char *)malloc(sizeof(char)*(key_length+1)*2);
    if (!key_enc) {
 log_printf("getCertAndKey: out of memory\n");
 goto error;
    }
    b64_encode(key_der,key_length,key_enc);
    log_printf("getCertAndKey: key_length=%0d\n", key_length);
    log_printf("getCertAndKey: key_enc='%s'\n", key_enc);
    log_printf("getCertAndKey: 5\n");
    attrl=(struct a_t **)malloc(sizeof(struct a_t *)*3);
    log_printf("getCertAndKey: 6\n");
    if (!attrl)
     goto error;
    memset(attrl, 0, sizeof(struct a_t *)*3);
    (attrl)[0]=(struct a_t *)malloc(sizeof(struct a_t));
    log_printf("getCertAndKey: 7\n");
    if (! (attrl)[0]) {
     goto error;
    }
    (attrl)[1]=(struct a_t *)malloc(sizeof(struct a_t));
    log_printf("getCertAndKey: 8\n");
    if (! (attrl)[1]) {
 goto error;
    }
    log_printf("getCertAndKey: 9\n");
    (attrl)[0]->name=_strdup("cert");
    (attrl)[0]->value=cert_enc;
    (attrl)[1]->name=_strdup("key");
    (attrl)[1]->value=key_enc;
    log_printf("getCertAndKey: leaving\n");
    return(attrl);
  error:
    if (attrl) {
 for (i=0;attrl[i];i++) {
     if (attrl[i])
  free(attrl[i]);
 }
 free(attrl);
    }
    if (key_enc)
 free(key_enc);
    if (cert_enc)
 free(cert_enc);
    return ((void *)0);
}

canonical_original_code:
struct a_t **getCertAndKey(struct a_t **tattrl, char *name, int namelen)
{
    struct a_t **attrl = ((void *)0);
    int cert_length = 0;
    char *cert_der = ((void *)0);
    char *cert_enc = ((void *)0);
    int key_length = 0;
    char *key_der = ((void *)0);
    char *key_enc = ((void *)0);
    int i;
    log_printf("getCertAndKey: entered\n");
    if (!os_getCertAndKey(&cert_der, &cert_length,
      &key_der, &key_length,
      name, namelen))
    {
 log_printf("getCertAndKey: os_getCertAndKey failed\n");
 goto error;
    }
    cert_enc=(char *)malloc(sizeof(char)*(cert_length+1)*2);
    if (!cert_enc) {
 log_printf("getCertAndKey: out of memory\n");
 goto error;
    }
    b64_encode(cert_der,cert_length,cert_enc);
    log_printf("getCertAndKey: cert_length=%0d\n", cert_length);
    log_printf("getCertAndKey: cert_enc='%s'\n", cert_enc);
    log_printf("getCertAndKey: 4\n");
    key_enc=(char *)malloc(sizeof(char)*(key_length+1)*2);
    if (!key_enc) {
 log_printf("getCertAndKey: out of memory\n");
 goto error;
    }
    b64_encode(key_der,key_length,key_enc);
    log_printf("getCertAndKey: key_length=%0d\n", key_length);
    log_printf("getCertAndKey: key_enc='%s'\n", key_enc);
    log_printf("getCertAndKey: 5\n");
    attrl=(struct a_t **)malloc(sizeof(struct a_t *)*3);
    log_printf("getCertAndKey: 6\n");
    if (!attrl)
     goto error;
    memset(attrl, 0, sizeof(struct a_t *)*3);
    (attrl)[0]=(struct a_t *)malloc(sizeof(struct a_t));
    log_printf("getCertAndKey: 7\n");
    if (! (attrl)[0]) {
     goto error;
    }
    (attrl)[1]=(struct a_t *)malloc(sizeof(struct a_t));
    log_printf("getCertAndKey: 8\n");
    if (! (attrl)[1]) {
 goto error;
    }
    log_printf("getCertAndKey: 9\n");
    (attrl)[0]->name=_strdup("cert");
    (attrl)[0]->value=cert_enc;
    (attrl)[1]->name=_strdup("key");
    (attrl)[1]->value=key_enc;
    log_printf("getCertAndKey: leaving\n");
    return(attrl);
  error:
    if (attrl) {
 for (i=0;attrl[i];i++) {
     if (attrl[i])
  free(attrl[i]);
 }
 free(attrl);
    }
    if (key_enc)
 free(key_enc);
    if (cert_enc)
 free(cert_enc);
    return ((void *)0);
}

variable_types:
tattrl
struct a_t **

variable_types: namelen int

return_type: struct a_t { char *name; char *value; } **


doauth

decompiled_code:
__int64 __fastcall doauth(char **a1, void ***a2)
{
void **v3; // rax
char *v4; // rax
void **v5; // r13
void *v6; // rax
void *v7; // rax
void ***v9; // rdi
void **v10; // rax
__int64 v11; // r12
void **v12; // rdi
void *v13; // rdi
void **v14; // rdi
__int128 v15; // [rsp+0h] [rbp-228h] BYREF
char v16[496]; // [rsp+10h] [rbp-218h] BYREF

v15 = 0LL;
memset(v16, 0, sizeof(v16));
log_printf("doauth: entered\n");
*a1 = 0LL;
v3 = (void **)calloc(0x18uLL, 1uLL);
*a2 = v3;
if ( v3 )
{
v4 = getCertAndKey((__int64)v3, (char *)&v15, 512);
*a1 = v4;
if ( v4 )
{
log_printf("doauth: user name is '%s'\n", (const char *)&v15);
v5 = *a2;
v6 = malloc(0x10uLL);
*v5 = v6;
if ( v6 )
{
v7 = malloc(0x10uLL);
v5[1] = v7;
if ( v7 )
{
*(_QWORD *)**a2 = (int)strdup("user");
*((_QWORD *)**a2 + 1) = (int)strdup((const char *)&v15);
*(_QWORD *)(*a2)[1] = (int)strdup("password");
*((_QWORD *)(*a2)[1] + 1) = (int)strdup(&LC0);
log_printf("doauth: success\n");
return 0LL;
}
v9 = (void ***)*a1;
if ( !*a1 )
goto LABEL_18;
}
else
{
v9 = (void ***)*a1;
if ( !*a1 )
{
LABEL_16:
v13 = v5[1];
if ( v13 )
free(v13);
LABEL_18:
v14 = *a2;
if ( **a2 )
{
free(**a2);
v14 = *a2;
}
free(v14);
*a2 = 0LL;
return 0xFFFFFFFFLL;
}
}
}
else
{
log_printf("doauth: doauth 9, couldn't do cert/key\n");
v9 = (void ***)*a1;
if ( !*a1 )
goto LABEL_15;
}
}
else
{
v9 = (void ***)*a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
}
v10 = *v9;
if ( *v9 )
{
v11 = 0LL;
do
{
if ( *v10 )
{
free(*v10);
v9 = (void ***)*a1;
}
v12 = v9[v11];
if ( v12[1] )
{
free(v12[1]);
v12 = *(void ***)&(*a1)[v11 * 8];
}
free(v12);
v9 = (void ***)*a1;
v10 = *(void ***)&(*a1)[++v11 * 8];
}
while ( v10 );
}
free(v9);
*a1 = 0LL;
LABEL_15:
v5 = *a2;
if ( *a2 )
goto LABEL_16;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char **a1, void ***a2)
{
void **v3; // rax
char *v4; // rax
void **v5; // r13
void *v6; // rax
void *v7; // rax
void ***v9; // rdi
void **v10; // rax
__int64 v11; // r12
void **v12; // rdi
void *v13; // rdi
void **v14; // rdi
__int128 v15; // [rsp+0h] [rbp-228h] BYREF
char v16[496]; // [rsp+10h] [rbp-218h] BYREF

v15 = 0LL;
memset(v16, 0, sizeof(v16));
log_printf("doauth: entered\n");
*a1 = 0LL;
v3 = (void **)calloc(0x18uLL, 1uLL);
*a2 = v3;
if ( v3 )
{
v4 = func2((__int64)v3, (char *)&v15, 512);
*a1 = v4;
if ( v4 )
{
log_printf("doauth: user name is '%s'\n", (const char *)&v15);
v5 = *a2;
v6 = malloc(0x10uLL);
*v5 = v6;
if ( v6 )
{
v7 = malloc(0x10uLL);
v5[1] = v7;
if ( v7 )
{
*(_QWORD *)**a2 = (int)strdup("user");
*((_QWORD *)**a2 + 1) = (int)strdup((const char *)&v15);
*(_QWORD *)(*a2)[1] = (int)strdup("password");
*((_QWORD *)(*a2)[1] + 1) = (int)strdup(&LC0);
log_printf("doauth: success\n");
return 0LL;
}
v9 = (void ***)*a1;
if ( !*a1 )
goto LABEL_18;
}
else
{
v9 = (void ***)*a1;
if ( !*a1 )
{
LABEL_16:
v13 = v5[1];
if ( v13 )
free(v13);
LABEL_18:
v14 = *a2;
if ( **a2 )
{
free(**a2);
v14 = *a2;
}
free(v14);
*a2 = 0LL;
return 0xFFFFFFFFLL;
}
}
}
else
{
log_printf("doauth: doauth 9, couldn't do cert/key\n");
v9 = (void ***)*a1;
if ( !*a1 )
goto LABEL_15;
}
}
else
{
v9 = (void ***)*a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
}
v10 = *v9;
if ( *v9 )
{
v11 = 0LL;
do
{
if ( *v10 )
{
free(*v10);
v9 = (void ***)*a1;
}
v12 = v9[v11];
if ( v12[1] )
{
free(v12[1]);
v12 = *(void ***)&(*a1)[v11 * 8];
}
free(v12);
v9 = (void ***)*a1;
v10 = *(void ***)&(*a1)[++v11 * 8];
}
while ( v10 );
}
free(v9);
*a1 = 0LL;
LABEL_15:
v5 = *a2;
if ( *a2 )
goto LABEL_16;
return 0xFFFFFFFFLL;
}

original_code:
int doauth(struct a_t ***attrl, struct a_t ***tattrl)
{
    char user[512]="";
    int i;
    log_printf("doauth: entered\n");
    *attrl=((void *)0);
    *tattrl=((void *)0);
    *tattrl=(struct a_t **)malloc(sizeof(struct a_t *)*3);
    if (!*tattrl) goto error;
    memset(*tattrl, 0, sizeof(struct a_t *)*3);
    *attrl=getCertAndKey(*tattrl, user, sizeof(user));
    if (!*attrl) {
 log_printf("doauth: doauth 9, couldn't do cert/key\n");
 goto error;
    }
    log_printf("doauth: user name is '%s'\n", user);
    (*tattrl)[0]=(struct a_t *)malloc(sizeof(struct a_t));
    if (! (*tattrl)[0]) goto error;
    (*tattrl)[1]=(struct a_t *)malloc(sizeof(struct a_t));
    if (! (*tattrl)[1]) goto error;
    (*tattrl)[0]->name=_strdup("user");
    (*tattrl)[0]->value=_strdup(user);
    (*tattrl)[1]->name=_strdup("password");
    (*tattrl)[1]->value=_strdup("");
    log_printf("doauth: success\n");
    return(0);
  error:
    if (*attrl) {
 for (i=0;(*attrl)[i];i++) {
     if ((*attrl)[i]->name)
  free((*attrl)[i]->name);
     if ((*attrl)[i]->value)
  free((*attrl)[i]->value);
     free((*attrl)[i]);
 }
 free(*attrl);
 *attrl = ((void *)0);
    }
    if (*tattrl) {
 if ((*tattrl)[1])
     free((*tattrl)[1]);
 if ((*tattrl)[0])
     free((*tattrl)[0]);
 free(*tattrl);
 *tattrl = ((void *)0);
    }
    return(-1);
}

canonical_original_code:
int doauth(struct a_t ***attrl, struct a_t ***tattrl)
{
    char user[512] = "";
    int i;
    log_printf("doauth: entered\n");
    *attrl=((void *)0);
    *tattrl=((void *)0);
    *tattrl=(struct a_t **)malloc(sizeof(struct a_t *)*3);
    if (!*tattrl) goto error;
    memset(*tattrl, 0, sizeof(struct a_t *)*3);
    *attrl=getCertAndKey(*tattrl, user, sizeof(user));
    if (!*attrl) {
 log_printf("doauth: doauth 9, couldn't do cert/key\n");
 goto error;
    }
    log_printf("doauth: user name is '%s'\n", user);
    (*tattrl)[0]=(struct a_t *)malloc(sizeof(struct a_t));
    if (! (*tattrl)[0]) goto error;
    (*tattrl)[1]=(struct a_t *)malloc(sizeof(struct a_t));
    if (! (*tattrl)[1]) goto error;
    (*tattrl)[0]->name=_strdup("user");
    (*tattrl)[0]->value=_strdup(user);
    (*tattrl)[1]->name=_strdup("password");
    (*tattrl)[1]->value=_strdup("");
    log_printf("doauth: success\n");
    return(0);
  error:
    if (*attrl) {
 for (i=0;(*attrl)[i];i++) {
     if ((*attrl)[i]->name)
  free((*attrl)[i]->name);
     if ((*attrl)[i]->value)
  free((*attrl)[i]->value);
     free((*attrl)[i]);
 }
 free(*attrl);
 *attrl = ((void *)0);
    }
    if (*tattrl) {
 if ((*tattrl)[1])
     free((*tattrl)[1]);
 if ((*tattrl)[0])
     free((*tattrl)[0]);
 free(*tattrl);
 *tattrl = ((void *)0);
    }
    return(-1);
}

variable_types: struct a_t { char *name; char *value; } ***

variable_types:
tattrl
struct a_t ***

variable_types:
user
char [512]


checkTokenValidity

decompiled_code:
__int64 checkTokenValidity()
{
return checkTokenValidity_KRB5();
}

canonical_decompiled_code:
__int64 func5()
{
return func4();
}

original_code:
int checkTokenValidity()
{
 return checkTokenValidity_KRB5();
}


memcpyfast

repo: sehugg/Starthinker

decompiled_code:
void *__fastcall memcpyfast(_BYTE *a1, unsigned int *a2, int a3)
{
void *result; // rax

if ( a3 == 3 )
{
*(_WORD *)a1 = *(_WORD *)a2;
result = (void *)*((unsigned __int8 *)a2 + 2);
a1[2] = (_BYTE)result;
return result;
}
if ( a3 > 3 )
{
if ( a3 == 4 )
{
result = (void *)*a2;
*(_DWORD *)a1 = (_DWORD)result;
return result;
}
return memcpy(a1, a2, a3);
}
if ( a3 != 1 )
{
if ( a3 == 2 )
{
result = (void *)*(unsigned __int16 *)a2;
*(_WORD *)a1 = (_WORD)result;
return result;
}
return memcpy(a1, a2, a3);
}
result = (void *)*(unsigned __int8 *)a2;
*a1 = (_BYTE)result;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(_BYTE *a1, unsigned int *a2, int a3)
{
void *result; // rax

if ( a3 == 3 )
{
*(_WORD *)a1 = *(_WORD *)a2;
result = (void *)*((unsigned __int8 *)a2 + 2);
a1[2] = (_BYTE)result;
return result;
}
if ( a3 > 3 )
{
if ( a3 == 4 )
{
result = (void *)*a2;
*(_DWORD *)a1 = (_DWORD)result;
return result;
}
return memcpy(a1, a2, a3);
}
if ( a3 != 1 )
{
if ( a3 == 2 )
{
result = (void *)*(unsigned __int16 *)a2;
*(_WORD *)a1 = (_WORD)result;
return result;
}
return memcpy(a1, a2, a3);
}
result = (void *)*(unsigned __int8 *)a2;
*a1 = (_BYTE)result;
return result;
}

original_code:
static void memcpyfast(void* dst, const void* src, int size)
{
  switch (size)
  {
    case 1: *((char*)dst) = *((char*)src); break;
    case 2: *((short*)dst) = *((short*)src); break;
    case 3: memcpy(dst,src,size); break;
    case 4: *((int*)dst) = *((int*)src); break;
    default: memcpy(dst,src,size); break;
  }
}

canonical_original_code:
static void memcpyfast(void *dst, void *src, int size)
{
  switch (size)
  {
    case 1: *((char *)dst) = *((char *)src); break;
    case 2: *((short *)dst) = *((short *)src); break;
    case 3: memcpy(dst,src,size); break;
    case 4: *((int *)dst) = *((int *)src); break;
    default: memcpy(dst,src,size); break;
  }
}

variable_types:
dst
void *

variable_types:
src
void *

variable_types: size int


decompiled_code:
__int64 __fastcall rollback_entry(__int64 a1)
{
size_t v1; // rdx
_BYTE *v3; // rdi
__int64 result; // rax

v1 = *(unsigned int *)(a1 + 16);
if ( !(_DWORD)v1 )
_assert_fail("j->size>0", "journal.c", 0x40u, "rollback_entry");
v3 = *(_BYTE **)a1;
if ( (unsigned int)v1 > 8 )
memcpy(v3, *(const void **)(a1 + 8), v1);
else
memcpyfast(v3, (unsigned int *)(a1 + 8), v1);
result = *(unsigned int *)(a1 + 20);
current_hash = *(_DWORD *)(a1 + 20);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
size_t v1; // rdx
_BYTE *v3; // rdi
__int64 result; // rax

v1 = *(unsigned int *)(a1 + 16);
if ( !(_DWORD)v1 )
_assert_fail("j->size>0", "journal.c", 0x40u, "rollback_entry");
v3 = *(_BYTE **)a1;
if ( (unsigned int)v1 > 8 )
memcpy(v3, *(const void **)(a1 + 8), v1);
else
func0(v3, (unsigned int *)(a1 + 8), v1);
result = *(unsigned int *)(a1 + 20);
current_hash = *(_DWORD *)(a1 + 20);
return result;
}

original_code:
static void rollback_entry(Journal* j)
{
  ;
  ((void) sizeof ((j->size>0) ? 1 : 0), __extension__ ({ if (j->size>0) ; else __assert_fail ("j->size>0", "journal.c", 64, __extension__ __PRETTY_FUNCTION__); }));
  if (j->size > sizeof(j->mem))
    memcpy(j->dest, j->mem, j->size);
  else
    memcpyfast(j->dest, &j->mem, j->size);
  current_hash = j->hash;
}

canonical_original_code:
static void rollback_entry(struct Journal *j)
{
  ;
  ((void) sizeof ((j->size>0) ? 1 : 0), __extension__ ({ if (j->size>0) ; else __assert_fail ("j->size>0", "journal.c", 64, __extension__ __PRETTY_FUNCTION__); }));
  if (j->size > sizeof(j->mem))
    memcpy(j->dest, j->mem, j->size);
  else
    memcpyfast(j->dest, &j->mem, j->size);
  current_hash = j->hash;
}

variable_types: struct Journal { void *dest; void *mem; unsigned int size; unsigned int hash; } *


decompiled_code:
__int64 __fastcall ai_journal_save(void *src, size_t size)
{
__int64 v2; // rax
char *v4; // rdi
char *v5; // rbx
__int64 result; // rax
void *v7; // rax

if ( !(_DWORD)size )
_assert_fail("size>0", "journal.c", 0x1Fu, "ai_journal_save");
v2 = jbuffer_top;
v4 = (char *)jbuffer;
if ( jbuffer_top == jbuffer_size )
{
jbuffer_size = jbuffer_inc + jbuffer_top;
jbuffer = realloc(jbuffer, 24LL * (jbuffer_inc + jbuffer_top));
v4 = (char *)jbuffer;
v2 = jbuffer_top;
}
v5 = &v4[24 * v2];
jbuffer_top = v2 + 1;
*((_DWORD *)v5 + 4) = size;
*(_QWORD *)v5 = src;
if ( (unsigned int)size > 8 )
{
v7 = malloc((unsigned int)size);
*((_QWORD *)v5 + 1) = v7;
memcpy(v7, src, (unsigned int)size);
}
else
{
memcpyfast(v5 + 8, (unsigned int *)src, size);
}
result = (unsigned int)current_hash;
*((_DWORD *)v5 + 5) = current_hash;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *src, size_t size)
{
__int64 v2; // rax
char *v4; // rdi
char *v5; // rbx
__int64 result; // rax
void *v7; // rax

if ( !(_DWORD)size )
_assert_fail("size>0", "journal.c", 0x1Fu, "ai_journal_save");
v2 = jbuffer_top;
v4 = (char *)jbuffer;
if ( jbuffer_top == jbuffer_size )
{
jbuffer_size = jbuffer_inc + jbuffer_top;
jbuffer = realloc(jbuffer, 24LL * (jbuffer_inc + jbuffer_top));
v4 = (char *)jbuffer;
v2 = jbuffer_top;
}
v5 = &v4[24 * v2];
jbuffer_top = v2 + 1;
*((_DWORD *)v5 + 4) = size;
*(_QWORD *)v5 = src;
if ( (unsigned int)size > 8 )
{
v7 = malloc((unsigned int)size);
*((_QWORD *)v5 + 1) = v7;
memcpy(v7, src, (unsigned int)size);
}
else
{
func0(v5 + 8, (unsigned int *)src, size);
}
result = (unsigned int)current_hash;
*((_DWORD *)v5 + 5) = current_hash;
return result;
}

original_code:
void ai_journal_save(const void* dst, unsigned int size)
{
  ((void) sizeof ((size>0) ? 1 : 0), __extension__ ({ if (size>0) ; else __assert_fail ("size>0", "journal.c", 31, __extension__ __PRETTY_FUNCTION__); }));
  if (jbuffer_top == jbuffer_size)
  {
    jbuffer_size += jbuffer_inc;
    jbuffer = (Journal*) realloc(jbuffer, sizeof(Journal) * jbuffer_size);
  }
  Journal* j = &jbuffer[jbuffer_top++];
  ;
  j->dest = (void*)dst;
  j->size = size;
  if (size > sizeof(j->mem))
  {
    j->mem = malloc(size);
    memcpy(j->mem, dst, size);
  } else {
    memcpyfast(&j->mem, dst, size);
  }
  j->hash = current_hash;
}

canonical_original_code:
void ai_journal_save(void *dst, unsigned int size)
{
  ((void) sizeof ((size>0) ? 1 : 0), __extension__ ({ if (size>0) ; else __assert_fail ("size>0", "journal.c", 31, __extension__ __PRETTY_FUNCTION__); }));
  if (jbuffer_top == jbuffer_size)
  {
    jbuffer_size += jbuffer_inc;
    jbuffer = (struct Journal *) realloc(jbuffer, sizeof(Journal) * jbuffer_size);
  }
  struct Journal *j = &jbuffer[jbuffer_top++];
  ;
  j->dest = (void *)dst;
  j->size = size;
  if (size > sizeof(j->mem))
  {
    j->mem = malloc(size);
    memcpy(j->mem, dst, size);
  } else {
    memcpyfast(&j->mem, dst, size);
  }
  j->hash = current_hash;
}

variable_types:
size
unsigned int

user_defined_types: struct Journal { void *dest; void *mem; unsigned int size; unsigned int hash; }


decompiled_code:
void *__fastcall ai_journal(int a1, _BYTE *a2, unsigned int *a3, unsigned int a4)
{
int v6; // ebx

ai_journal_save(a2, a4);
v6 = compute_hash_murmur2(a3, a4, (unsigned int)((_DWORD)a2 - a1));
current_hash ^= compute_hash_murmur2(a2, a4, (unsigned int)((_DWORD)a2 - a1)) ^ v6;
return memcpyfast(a2, a3, a4);
}

canonical_decompiled_code:
void *__fastcall func4(int a1, _BYTE *a2, unsigned int *a3, unsigned int a4)
{
int v6; // ebx

func3(a2, a4);
v6 = compute_hash_murmur2(a3, a4, (unsigned int)((_DWORD)a2 - a1));
current_hash ^= compute_hash_murmur2(a2, a4, (unsigned int)((_DWORD)a2 - a1)) ^ v6;
return func0(a2, a3, a4);
}

original_code:
void ai_journal(const void* base, const void* dst, const void* src, unsigned int size)
{
  ai_journal_save(dst, size);
  int index0 = (intptr_t)dst - (intptr_t)base;
  current_hash ^= compute_hash_murmur2(src, size, index0) ^ compute_hash_murmur2(dst, size, index0);
  ;
  memcpyfast((void*)dst, src, size);
}

canonical_original_code:
void ai_journal(void *base, void *dst, void *src, unsigned int size)
{
  ai_journal_save(dst, size);
  int index0 = (intptr_t)dst - (intptr_t)base;
  current_hash ^= compute_hash_murmur2(src, size, index0) ^ compute_hash_murmur2(dst, size, index0);
  ;
  memcpyfast((void *)dst, src, size);
}

variable_types:
base
void *


decompiled_code:
__int64 __fastcall rollback_journal(int a1)
{
__int64 result; // rax
char *v2; // rbx

for ( result = (unsigned int)jbuffer_top; jbuffer_top > a1; result = (unsigned int)jbuffer_top )
{
jbuffer_top = result - 1;
v2 = (char *)jbuffer + 24 * (int)result - 24;
rollback_entry((__int64)v2);
dealloc_entry_isra_0(*((void **)v2 + 1), *((_DWORD *)v2 + 4));
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
__int64 result; // rax
char *v2; // rbx

for ( result = (unsigned int)jbuffer_top; jbuffer_top > a1; result = (unsigned int)jbuffer_top )
{
jbuffer_top = result - 1;
v2 = (char *)jbuffer + 24 * (int)result - 24;
func1((__int64)v2);
func2(*((void **)v2 + 1), *((_DWORD *)v2 + 4));
}
return result;
}

original_code:
void rollback_journal(int top)
{
  while (jbuffer_top > top)
  {
    Journal* j = &jbuffer[--jbuffer_top];
    rollback_entry(j);
    dealloc_entry(j);
  }
}

canonical_original_code:
void rollback_journal(int top)
{
  while (jbuffer_top > top)
  {
    struct Journal *j = &jbuffer[--jbuffer_top];
    rollback_entry(j);
    dealloc_entry(j);
  }
}

variable_types: top int


decompiled_code:
void commit_journal()
{
jbuffer_top = 0;
}

canonical_decompiled_code:
void func6()
{
jbuffer_top = 0;
}


repo: seiferteric/life

decompiled_code:
__int64 __fastcall get_point(int a1, int a2)
{
return (unsigned int)life[800 * a2 + a1];
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
return (unsigned int)life[800 * a2 + a1];
}

original_code:
uint32_t get_point(int x, int y) {
  return life[y][x];
}

canonical_original_code:
unsigned int get_point(int x, int y) {
  return life[y][x];
}

variable_types: x int

variable_types: y int

return_type: unsigned int


decompiled_code:
__int64 __fastcall set_point(int a1, int a2, int a3)
{
__int64 result; // rax

result = *(_QWORD *)(srf + 32);
*(_DWORD *)(result + 4LL * (a1 + 800 * a2)) = a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3)
{
__int64 result; // rax

result = *(_QWORD *)(srf + 32);
*(_DWORD *)(result + 4LL * (a1 + 800 * a2)) = a3;
return result;
}

original_code:
void set_point(int x, int y, uint32_t color) {
  *((uint32_t*)srf->pixels + (800*y + x)) = color;
}

canonical_original_code:
void set_point(int x, int y, unsigned int color) {
  *((unsigned int *)srf->pixels + (800*y + x)) = color;
}

variable_types:
color
unsigned int


init

decompiled_code:
__int64 __fastcall init(time_t *a1)
{
int v1; // ebx
unsigned int v2; // eax
int i; // ebp
int v4; // edi

v1 = 0;
v2 = time(a1);
srand(v2);
SDL_FillRect();
do
{
for ( i = 0; i != 800; ++i )
{
while ( rand() % 10 != 1 )
{
if ( ++i == 800 )
goto LABEL_6;
}
v4 = i;
set_point(v4, v1, 65280);
}
LABEL_6:
++v1;
}
while ( v1 != 800 );
return SDL_UpdateRect();
}

canonical_decompiled_code:
__int64 __fastcall func9(time_t *a1)
{
int v1; // ebx
unsigned int v2; // eax
int i; // ebp
int v4; // edi

v1 = 0;
v2 = time(a1);
srand(v2);
SDL_FillRect();
do
{
for ( i = 0; i != 800; ++i )
{
while ( rand() % 10 != 1 )
{
if ( ++i == 800 )
goto LABEL_6;
}
v4 = i;
func8(v4, v1, 65280);
}
LABEL_6:
++v1;
}
while ( v1 != 800 );
return SDL_UpdateRect();
}


north

decompiled_code:
__int64 __fastcall north(int a1, int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
result = get_point(a1, 799);
else
result = get_point(a1, a2 - 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
result = func7(a1, 799);
else
result = func7(a1, a2 - 1);
return result;
}

original_code:
uint32_t north(int x, int y) {
  if(y > 0)
    return get_point(x, y-1);
  else
    return get_point(x, (800 - 1));
}

canonical_original_code:
unsigned int north(int x, int y) {
  if(y > 0)
    return get_point(x, y-1);
  else
    return get_point(x, (800 - 1));
}


south

decompiled_code:
__int64 __fastcall south(int a1, int a2)
{
__int64 result; // rax

if ( a2 > 798 )
result = get_point(a1, 0);
else
result = get_point(a1, a2 + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1, int a2)
{
__int64 result; // rax

if ( a2 > 798 )
result = func7(a1, 0);
else
result = func7(a1, a2 + 1);
return result;
}

original_code:
uint32_t south(int x, int y) {
  if(y < (800 - 1))
    return get_point(x, y+1);
  else
    return get_point(x, 0);
}

canonical_original_code:
unsigned int south(int x, int y) {
  if(y < (800 - 1))
    return get_point(x, y+1);
  else
    return get_point(x, 0);
}


east

decompiled_code:
__int64 __fastcall east(int a1, int a2)
{
__int64 result; // rax

if ( a1 > 798 )
result = get_point(0, a2);
else
result = get_point(a1 + 1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, int a2)
{
__int64 result; // rax

if ( a1 > 798 )
result = func7(0, a2);
else
result = func7(a1 + 1, a2);
return result;
}

original_code:
uint32_t east(int x, int y) {
  if(x < (800 - 1))
    return get_point(x+1, y);
  else
    return get_point(0, y);
}

canonical_original_code:
unsigned int east(int x, int y) {
  if(x < (800 - 1))
    return get_point(x+1, y);
  else
    return get_point(0, y);
}


west

decompiled_code:
__int64 __fastcall west(int a1, int a2)
{
__int64 result; // rax

if ( a1 <= 0 )
result = get_point(799, a2);
else
result = get_point(a1 - 1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1, int a2)
{
__int64 result; // rax

if ( a1 <= 0 )
result = func7(799, a2);
else
result = func7(a1 - 1, a2);
return result;
}

original_code:
uint32_t west(int x, int y) {
  if(x > 0)
    return get_point(x-1, y);
  else
    return get_point((800 - 1), y);
}

canonical_original_code:
unsigned int west(int x, int y) {
  if(x > 0)
    return get_point(x-1, y);
  else
    return get_point((800 - 1), y);
}


decompiled_code:
__int64 __fastcall north_west(int a1, int a2)
{
int v2; // edx
int v3; // eax

v2 = 799;
v3 = a2 - 1;
if ( a2 <= 0 )
v3 = 799;
if ( a1 > 0 )
v2 = a1 - 1;
return get_point(v2, v3);
}

original_code:
uint32_t north_west(int x, int y) {
  int nx, ny;
  if(y > 0)
    ny = y -1;
  else
    ny = (800 - 1);
  if(x > 0)
    nx = x-1;
  else
    nx = (800 - 1);
  return get_point(nx, ny);
}

canonical_original_code:
unsigned int north_west(int x, int y) {
  int nx; int ny;
  if(y > 0)
    ny = y -1;
  else
    ny = (800 - 1);
  if(x > 0)
    nx = x-1;
  else
    nx = (800 - 1);
  return get_point(nx, ny);
}

variable_types: nx int

variable_types: ny int


decompiled_code:
__int64 __fastcall north_east(int a1, int a2)
{
int v2; // eax
bool v3; // cc
int v4; // esi
int v5; // eax
int v6; // edi

v2 = a2 - 1;
v3 = a2 <= 0;
v4 = 799;
if ( !v3 )
v4 = v2;
v5 = a1 + 1;
v3 = a1 < 799;
v6 = 0;
if ( v3 )
v6 = v5;
return get_point(v6, v4);
}

original_code:
uint32_t north_east(int x, int y) {
  int nx, ny;
  if(y > 0)
    ny = y -1;
  else
    ny = (800 - 1);
  if(x < (800 - 1))
    nx = x+1;
  else
    nx = 0;
  return get_point(nx, ny);
}

canonical_original_code:
unsigned int north_east(int x, int y) {
  int nx; int ny;
  if(y > 0)
    ny = y -1;
  else
    ny = (800 - 1);
  if(x < (800 - 1))
    nx = x+1;
  else
    nx = 0;
  return get_point(nx, ny);
}


decompiled_code:
__int64 __fastcall south_west(int a1, int a2)
{
int v2; // eax
bool v3; // cc
int v4; // esi
int v5; // eax
int v6; // edi

v2 = a2 + 1;
v3 = a2 < 799;
v4 = 0;
if ( v3 )
v4 = v2;
v5 = a1 - 1;
v3 = a1 <= 0;
v6 = 799;
if ( !v3 )
v6 = v5;
return get_point(v6, v4);
}

original_code:
uint32_t south_west(int x, int y) {
  int nx, ny;
  if(y < (800 - 1))
    ny = y + 1;
  else
    ny = 0;
  if(x > 0)
    nx = x-1;
  else
    nx = (800 - 1);
  return get_point(nx, ny);
}

canonical_original_code:
unsigned int south_west(int x, int y) {
  int nx; int ny;
  if(y < (800 - 1))
    ny = y + 1;
  else
    ny = 0;
  if(x > 0)
    nx = x-1;
  else
    nx = (800 - 1);
  return get_point(nx, ny);
}


decompiled_code:
__int64 __fastcall south_east(int a1, int a2)
{
int v2; // edx
int v3; // eax

v2 = 0;
v3 = a2 + 1;
if ( a2 >= 799 )
v3 = 0;
if ( a1 < 799 )
v2 = a1 + 1;
return get_point(v2, v3);
}

original_code:
uint32_t south_east(int x, int y) {
  int nx, ny;
  if(y < (800 - 1))
    ny = y + 1;
  else
    ny = 0;
  if(x < (800 - 1))
    nx = x+1;
  else
    nx = 0;
  return get_point(nx, ny);
}

canonical_original_code:
unsigned int south_east(int x, int y) {
  int nx; int ny;
  if(y < (800 - 1))
    ny = y + 1;
  else
    ny = 0;
  if(x < (800 - 1))
    nx = x+1;
  else
    nx = 0;
  return get_point(nx, ny);
}


decompiled_code:
__int64 __fastcall living_neighbours(int a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // er9
int v5; // er10
int v6; // er9
int v7; // er10
int v8; // er9
int v9; // er10
int v10; // er9
int v11; // er10
int v12; // er9
int v13; // er10
int v14; // er9
int v15; // er10
int v16; // eax
int v17; // er8

north_west(a1, a2);
north(v2, v3);
north_east(v4, v5);
west(v6, v7);
east(v8, v9);
south_west(v10, v11);
south(v12, v13);
v16 = south_east(v14, v15);
return v17 - ((unsigned int)(v16 == 0) - 1);
}

original_code:
uint32_t living_neighbours(int x, int y) {
  int count = 0;
  if(north_west(x, y))
    count++;
  if(north(x, y))
    count++;
  if(north_east(x, y))
    count++;
  if(west(x, y))
    count++;
  if(east(x, y))
    count++;
  if(south_west(x, y))
    count++;
  if(south(x, y))
    count++;
  if(south_east(x, y))
    count++;
  return count;
}

canonical_original_code:
unsigned int living_neighbours(int x, int y) {
  int count = 0;
  if(north_west(x, y))
    count++;
  if(north(x, y))
    count++;
  if(north_east(x, y))
    count++;
  if(west(x, y))
    count++;
  if(east(x, y))
    count++;
  if(south_west(x, y))
    count++;
  if(south(x, y))
    count++;
  if(south_east(x, y))
    count++;
  return count;
}

variable_types: count int


step

decompiled_code:
__int64 step()
{
int v0; // er12
int i; // ebx
int v2; // ebp
int v3; // edi

v0 = 0;
memcpy(life, *(const void **)(srf + 32), 0x271000uLL);
do
{
for ( i = 0; i != 800; ++i )
{
while ( 1 )
{
v2 = living_neighbours(i, v0);
if ( !(unsigned int)get_point(i, v0) && __ROR4__(652835029 * rand() + 17179864, 3) <= (unsigned int)&unk_418936 )
break;
if ( v2 <= 1 || v2 > 3 )
{
set_point(i, v0, 0);
}
else if ( v2 == 3 )
{
break;
}
if ( ++i == 800 )
goto LABEL_10;
}
v3 = i;
set_point(v3, v0, 65280);
}
LABEL_10:
++v0;
}
while ( v0 != 800 );
return SDL_UpdateRect();
}

original_code:
void step() {
  memcpy(life, srf->pixels, 800*800*4);
  int x, y;
  for(y=0;y<800;y++) {
    for(x=0;x<800;x++) {
      int n = living_neighbours(x, y);
      if(!get_point(x,y) && !(rand() % 1000)) {
        set_point(x, y, 0x0000ff00);
        continue;
      }
      if(n < 2) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n > 3) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n == 3) {
        set_point(x, y, 0x0000ff00);
      }
    }
  }
  SDL_UpdateRect(srf, 0, 0, 0, 0);
}

canonical_original_code:
void step() {
  memcpy(life, srf->pixels, 800*800*4);
  int x; int y;
  for(y=0;y<800;y++) {
    for(x=0;x<800;x++) {
      int n = living_neighbours(x, y);
      if(!get_point(x,y) && !(rand() % 1000)) {
        set_point(x, y, 0x0000ff00);
        continue;
      }
      if(n < 2) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n > 3) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n == 3) {
        set_point(x, y, 0x0000ff00);
      }
    }
  }
  SDL_UpdateRect(srf, 0, 0, 0, 0);
}

variable_types: n int


repo: selavy/anothersim

decompiled_code:
__int64 __fastcall check_stall(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // er8
_DWORD *v3; // rax
__int64 result; // rax

if ( MEM1 != -1 && (v1 = *(&Instructions + MEM1), *v1 == 2) && (v2 = 1, v1[69] == a1)
|| (v2 = 0, MEM2 == -1)
|| (v3 = *(&Instructions + MEM2), *v3 != 2) )
{
result = v2;
}
else
{
result = v3[69] == a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // er8
_DWORD *v3; // rax
__int64 result; // rax

if ( MEM1 != -1 && (v1 = *(&Instructions + MEM1), *v1 == 2) && (v2 = 1, v1[69] == a1)
|| (v2 = 0, MEM2 == -1)
|| (v3 = *(&Instructions + MEM2), *v3 != 2) )
{
result = v2;
}
else
{
result = v3[69] == a1;
}
return result;
}

original_code:
int check_stall( int reg )
{
  if( MEM1 != -1 )
    if( ( (*Instructions[ MEM1 ]).itype == LD ) && ( (*Instructions[ MEM1 ]).rt == reg ) ) return 1;
  if( MEM2 != -1 )
    if( ( (*Instructions[ MEM2 ]).itype == LD ) && ( (*Instructions[ MEM2 ]).rt == reg ) ) return 1;
  return 0;
}

canonical_original_code:
int check_stall(int reg)
{
  if( MEM1 != -1 )
    if( ( (*Instructions[ MEM1 ]).itype == LD ) && ( (*Instructions[ MEM1 ]).rt == reg ) ) return 1;
  if( MEM2 != -1 )
    if( ( (*Instructions[ MEM2 ]).itype == LD ) && ( (*Instructions[ MEM2 ]).rt == reg ) ) return 1;
  return 0;
}

variable_types: reg int


decompiled_code:
void if1()
{
if ( !flush && (!IF1_stall || IF1 == -1) )
{
if ( inst_counter >= instcount )
{
IF1 = -1;
}
else
{
IF1 = inst_counter;
IF1_count = inst_cycle;
if ( inst_counter != -1 )
{
fprintf(fout, "I%d-IF1 ", (unsigned int)inst_cycle);
++inst_counter;
return;
}
}
++inst_counter;
}
}

canonical_decompiled_code:
void func7()
{
if ( !flush && (!IF1_stall || IF1 == -1) )
{
if ( inst_counter >= instcount )
{
IF1 = -1;
}
else
{
IF1 = inst_counter;
IF1_count = inst_cycle;
if ( inst_counter != -1 )
{
fprintf(fout, "I%d-IF1 ", (unsigned int)inst_cycle);
++inst_counter;
return;
}
}
++inst_counter;
}
}

original_code:
void if1()
{
  if( flush ) return;
  if( IF1_stall && (IF1 != -1) ) return;
  if( inst_counter < instcount )
    {
      IF1 = inst_counter;
      IF1_count = inst_cycle;
    }
  else
    IF1 = -1;
  if( IF1 != -1 )
    fprintf( fout, "I%d-IF1 ", IF1_count );
  inst_counter++;
}


decompiled_code:
int if2()
{
int result; // eax

result = IF2;
if ( IF2_stall )
{
if ( IF2 != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)IF2_count);
}
else if ( IF2 != -1 )
{
result = fprintf(fout, "I%d-IF2 ", (unsigned int)IF2_count);
}
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax

result = IF2;
if ( IF2_stall )
{
if ( IF2 != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)IF2_count);
}
else if ( IF2 != -1 )
{
result = fprintf(fout, "I%d-IF2 ", (unsigned int)IF2_count);
}
return result;
}

original_code:
void if2()
{
  if( IF2_stall && (IF2 != -1) )
    {
      fprintf( fout, "I%d-stall ", IF2_count );
      return;
    }
  if( IF2 != -1 )
    fprintf( fout, "I%d-IF2 ", IF2_count );
}


id

decompiled_code:
int id()
{
int result; // eax

result = ID;
if ( ID_stall )
{
if ( ID != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)ID_count);
}
else if ( ID != -1 )
{
result = fprintf(fout, "I%d-ID ", (unsigned int)ID_count);
}
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax

result = ID;
if ( ID_stall )
{
if ( ID != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)ID_count);
}
else if ( ID != -1 )
{
result = fprintf(fout, "I%d-ID ", (unsigned int)ID_count);
}
return result;
}

original_code:
void id()
{
  if( ID_stall && (ID != -1) )
    {
      fprintf( fout, "I%d-stall ", ID_count );
      return;
    }
  if( ID != -1 )
    fprintf( fout, "I%d-ID ", ID_count );
}


ex

decompiled_code:
int ex()
{
int result; // eax
_DWORD *v1; // rbx
int v2; // eax
__int64 v3; // rsi
__int64 v4; // rcx
int v5; // ecx
int v6; // ecx
__int64 v7; // rsi
__int64 v8; // rcx
__int64 v9; // rax
int v10; // ecx
__int64 v11; // rcx
__int64 v12; // r12
const char *v13; // rbx
__int64 v14; // rbp

result = EX;
if ( EX == -1 )
return result;
v1 = *(&Instructions + EX);
if ( EX_stall )
{
if ( (unsigned int)check_stall(v1[68]) || (unsigned int)check_stall(v1[69]) )
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
EX_stall = 0;
ID_stall = 0;
IF2_stall = 0;
IF1_stall = 0;
}
v2 = *v1;
if ( !*v1 )
{
if ( !(unsigned int)check_stall(v1[68]) )
{
v3 = (int)v1[69];
if ( !(unsigned int)check_stall(v1[69]) )
{
v5 = Registers[v4];
if ( (_DWORD)v3 == -1 )
v6 = v1[138] + v5;
else
v6 = Registers[v3] + v5;
Registers[v1[70]] = v6;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
goto LABEL_15;
}
if ( v2 == 1 )
{
if ( !(unsigned int)check_stall(v1[68]) )
{
v7 = (int)v1[69];
if ( !(unsigned int)check_stall(v1[69]) )
{
v9 = (int)v1[70];
v10 = Registers[v8];
if ( (_DWORD)v7 == -1 )
Registers[v9] = v10 - v1[138];
else
Registers[v9] = v10 - Registers[v7];
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
goto LABEL_15;
}
if ( v2 != 4 )
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
if ( (unsigned int)check_stall(v1[68]) )
{
LABEL_15:
EX_stall = 1;
ID_stall = 1;
IF2_stall = 1;
IF1_stall = 1;
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
}
result = Registers[v11];
if ( !result )
{
EX = -1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
v12 = instcount;
if ( instcount > 0 )
{
v13 = (const char *)(v1 + 71);
v14 = 0LL;
while ( 1 )
{
result = strcmp((const char *)*(&Instructions + v14) + 4, v13);
if ( !result )
break;
if ( ++v14 == v12 )
return result;
}
inst_counter = v14;
flush = 1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax
_DWORD *v1; // rbx
int v2; // eax
__int64 v3; // rsi
__int64 v4; // rcx
int v5; // ecx
int v6; // ecx
__int64 v7; // rsi
__int64 v8; // rcx
__int64 v9; // rax
int v10; // ecx
__int64 v11; // rcx
__int64 v12; // r12
const char *v13; // rbx
__int64 v14; // rbp

result = EX;
if ( EX == -1 )
return result;
v1 = *(&Instructions + EX);
if ( EX_stall )
{
if ( (unsigned int)func6(v1[68]) || (unsigned int)func6(v1[69]) )
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
EX_stall = 0;
ID_stall = 0;
IF2_stall = 0;
IF1_stall = 0;
}
v2 = *v1;
if ( !*v1 )
{
if ( !(unsigned int)func6(v1[68]) )
{
v3 = (int)v1[69];
if ( !(unsigned int)func6(v1[69]) )
{
v5 = Registers[v4];
if ( (_DWORD)v3 == -1 )
v6 = v1[138] + v5;
else
v6 = Registers[v3] + v5;
Registers[v1[70]] = v6;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
goto LABEL_15;
}
if ( v2 == 1 )
{
if ( !(unsigned int)func6(v1[68]) )
{
v7 = (int)v1[69];
if ( !(unsigned int)func6(v1[69]) )
{
v9 = (int)v1[70];
v10 = Registers[v8];
if ( (_DWORD)v7 == -1 )
Registers[v9] = v10 - v1[138];
else
Registers[v9] = v10 - Registers[v7];
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
goto LABEL_15;
}
if ( v2 != 4 )
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
if ( (unsigned int)func6(v1[68]) )
{
LABEL_15:
EX_stall = 1;
ID_stall = 1;
IF2_stall = 1;
IF1_stall = 1;
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
}
result = Registers[v11];
if ( !result )
{
EX = -1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
v12 = instcount;
if ( instcount > 0 )
{
v13 = (const char *)(v1 + 71);
v14 = 0LL;
while ( 1 )
{
result = strcmp((const char *)*(&Instructions + v14) + 4, v13);
if ( !result )
break;
if ( ++v14 == v12 )
return result;
}
inst_counter = v14;
flush = 1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
return result;
}

original_code:
void ex()
{
  struct inst * curr;
  int i;
  if( EX == -1 )
    return;
  curr = Instructions[EX];
  if( EX_stall )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      else
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 0;
 }
    }
  if( (*curr).itype == DADD )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == SUB )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == BNEZ )
    {
      if( check_stall( (*curr).rs ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( Registers[ ( ((*curr).rs) ) ] != 0 )
 {
   for( i = 0; i < instcount; ++i )
     {
       if( strcmp( (*Instructions[i]).label, (*curr).btarget ) == 0 )
  break;
     }
   if( i >= instcount )
     return;
   inst_counter = i;
   flush = 1;
 }
      else
 EX = -1;
    }
  fprintf( fout, "I%d-EX ", EX_count );
}

canonical_original_code:
void ex()
{
  struct inst *curr;
  int i;
  if( EX == -1 )
    return;
  curr = Instructions[EX];
  if( EX_stall )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      else
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 0;
 }
    }
  if( (*curr).itype == DADD )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == SUB )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == BNEZ )
    {
      if( check_stall( (*curr).rs ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( Registers[ ( ((*curr).rs) ) ] != 0 )
 {
   for( i = 0; i < instcount; ++i )
     {
       if( strcmp( (*Instructions[i]).label, (*curr).btarget ) == 0 )
  break;
     }
   if( i >= instcount )
     return;
   inst_counter = i;
   flush = 1;
 }
      else
 EX = -1;
    }
  fprintf( fout, "I%d-EX ", EX_count );
}

variable_types: struct inst { enum inst_t { DADD=0, SUB=1, LD=2, SD=3, BNEZ=4 } itype; char label[256]; unsigned long labelsz; int rs; int rt; int rd; char btarget[256]; unsigned long btargetsz; int value; } *

variable_types: i int

user_defined_types: struct inst { enum inst_t itype; char label[256]; unsigned long labelsz; int rs; int rt; int rd; char btarget[256]; unsigned long btargetsz; int value; }


decompiled_code:
int mem1()
{
int result; // eax

if ( MEM1 != -1 )
{
if ( MEM1_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM1_count);
else
result = fprintf(fout, "I%d-MEM1 ", (unsigned int)MEM1_count);
}
return result;
}

canonical_decompiled_code:
int func11()
{
int result; // eax

if ( MEM1 != -1 )
{
if ( MEM1_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM1_count);
else
result = fprintf(fout, "I%d-MEM1 ", (unsigned int)MEM1_count);
}
return result;
}

original_code:
void mem1()
{
  if( MEM1 == -1 ) return;
  else if( MEM1_stall )
    {
      fprintf( fout, "I%d-stall ", MEM1_count );
      return;
    }
  else
    fprintf( fout, "I%d-MEM1 ", MEM1_count );
}


decompiled_code:
int mem2()
{
int result; // eax
unsigned int v1; // er8
FILE *v2; // rdi
_DWORD *v3; // rax
__int64 v4; // rcx
int v5; // edx
bool v6; // sf
int v7; // ecx
int v8; // edx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rcx
int v12; // eax
int v13; // edx
int v14; // eax

result = MEM2;
if ( MEM2 == -1 )
return result;
v1 = MEM2_count;
v2 = fout;
if ( MEM2_stall )
return fprintf(fout, "I%d-stall ", (unsigned int)MEM2_count);
v3 = *(&Instructions + MEM2);
if ( *v3 == 2 )
{
v10 = (int)v3[68];
v11 = (int)v3[69];
v12 = v3[138];
v6 = Registers[v10] + v12 < 0;
v13 = Registers[v10] + v12;
v14 = v13 + 7;
if ( !v6 )
v14 = v13;
Registers[v11] = Memory[v14 >> 3];
return fprintf(v2, "I%d-MEM2 ", v1);
}
if ( *v3 != 3 )
return fprintf(v2, "I%d-MEM2 ", v1);
v4 = (int)v3[68];
v5 = v3[138];
v6 = Registers[v4] + v5 < 0;
v7 = Registers[v4] + v5;
v8 = v7 + 7;
v9 = Registers[v3[69]];
if ( !v6 )
v8 = v7;
Memory[v8 >> 3] = v9;
return fprintf(v2, "I%d-MEM2 ", v1);
}

canonical_decompiled_code:
int func12()
{
int result; // eax
unsigned int v1; // er8
FILE *v2; // rdi
_DWORD *v3; // rax
__int64 v4; // rcx
int v5; // edx
bool v6; // sf
int v7; // ecx
int v8; // edx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rcx
int v12; // eax
int v13; // edx
int v14; // eax

result = MEM2;
if ( MEM2 == -1 )
return result;
v1 = MEM2_count;
v2 = fout;
if ( MEM2_stall )
return fprintf(fout, "I%d-stall ", (unsigned int)MEM2_count);
v3 = *(&Instructions + MEM2);
if ( *v3 == 2 )
{
v10 = (int)v3[68];
v11 = (int)v3[69];
v12 = v3[138];
v6 = Registers[v10] + v12 < 0;
v13 = Registers[v10] + v12;
v14 = v13 + 7;
if ( !v6 )
v14 = v13;
Registers[v11] = Memory[v14 >> 3];
return fprintf(v2, "I%d-MEM2 ", v1);
}
if ( *v3 != 3 )
return fprintf(v2, "I%d-MEM2 ", v1);
v4 = (int)v3[68];
v5 = v3[138];
v6 = Registers[v4] + v5 < 0;
v7 = Registers[v4] + v5;
v8 = v7 + 7;
v9 = Registers[v3[69]];
if ( !v6 )
v8 = v7;
Memory[v8 >> 3] = v9;
return fprintf(v2, "I%d-MEM2 ", v1);
}

original_code:
void mem2()
{
  struct inst * curr;
  if( MEM2 == -1 ) return;
  else if( MEM2_stall )
    {
      fprintf( fout, "I%d-stall ", MEM2_count );
      return;
    }
  else
    {
      curr = Instructions[MEM2];
      if( (*curr).itype == LD )
 Registers[ ( ((*curr).rt) ) ] = Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ];
      else if( (*curr).itype == SD )
 Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ] = Registers[ ( ((*curr).rt) ) ];
      fprintf( fout, "I%d-MEM2 ", MEM2_count );
    }
}

canonical_original_code:
void mem2()
{
  struct inst *curr;
  if( MEM2 == -1 ) return;
  else if( MEM2_stall )
    {
      fprintf( fout, "I%d-stall ", MEM2_count );
      return;
    }
  else
    {
      curr = Instructions[MEM2];
      if( (*curr).itype == LD )
 Registers[ ( ((*curr).rt) ) ] = Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ];
      else if( (*curr).itype == SD )
 Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ] = Registers[ ( ((*curr).rt) ) ];
      fprintf( fout, "I%d-MEM2 ", MEM2_count );
    }
}

user_defined_types: enum inst_t { DADD=0, SUB=1, LD=2, SD=3, BNEZ=4 }


decompiled_code:
int mem3()
{
int result; // eax

if ( MEM3 != -1 )
{
if ( MEM3_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM3_count);
else
result = fprintf(fout, "I%d-MEM3 ", (unsigned int)MEM3_count);
}
return result;
}

canonical_decompiled_code:
int func13()
{
int result; // eax

if ( MEM3 != -1 )
{
if ( MEM3_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM3_count);
else
result = fprintf(fout, "I%d-MEM3 ", (unsigned int)MEM3_count);
}
return result;
}

original_code:
void mem3()
{
  if( MEM3 == -1 ) return;
  else if( MEM3_stall )
    {
      fprintf( fout, "I%d-stall ", MEM3_count );
      return;
    }
  else
    fprintf( fout, "I%d-MEM3 ", MEM3_count );
}


wb

decompiled_code:
int wb()
{
int result; // eax

if ( WB != -1 )
{
if ( WB_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)WB_count);
else
result = fprintf(fout, "I%d-WB ", (unsigned int)WB_count);
}
return result;
}

canonical_decompiled_code:
int func14()
{
int result; // eax

if ( WB != -1 )
{
if ( WB_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)WB_count);
else
result = fprintf(fout, "I%d-WB ", (unsigned int)WB_count);
}
return result;
}

original_code:
void wb()
{
  if( WB == -1 ) return;
  else if( WB_stall )
    {
      fprintf( fout, "I%d-stall ", WB_count );
      return;
    }
  else
    fprintf( fout, "I%d-WB ", WB_count );
}


main

canonical_name: main

repo: selavy/simulator

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
infile = dftinfile;
outfile = (__int64)dftoutfile;
}
else
{
infile = (char *)argv[1];
if ( argc == 3 )
outfile = (__int64)dftoutfile;
else
outfile = (__int64)argv[2];
}
init_regs(argc, argv, envp);
init_memfile();
read_input(infile);
print_code_listing();
WB();
MEM3();
MEM2();
MEM1();
EX();
ID();
IF2();
IF1();
close_input();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
infile = dftinfile;
outfile = (__int64)dftoutfile;
}
else
{
infile = (char *)argv[1];
if ( argc == 3 )
outfile = (__int64)dftoutfile;
else
outfile = (__int64)argv[2];
}
func7(argc, argv, envp);
func13();
func29(infile);
func31();
func39();
func38();
func37();
func36();
func35();
func34();
func33();
func32();
func21();
return 0;
}

original_code:
int main( int argc, char ** argv )
{
  if( argc < 2 )
    {
      infile = dftinfile;
      outfile = dftoutfile;
    }
  else if( argc == 3 )
    {
      infile = argv[1];
      outfile = dftoutfile;
    }
  else
    {
      infile = argv[1];
      outfile = argv[2];
    }
  init_regs();
  init_memfile();
  read_input( infile );
  print_code_listing();
  do
    {
      WB();
      MEM3();
      MEM2();
      MEM1();
      EX();
      ID();
      IF2();
      IF1();
    } while( 0 );
  close_input();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  if( argc < 2 )
    {
      infile = dftinfile;
      outfile = dftoutfile;
    }
  else if( argc == 3 )
    {
      infile = argv[1];
      outfile = dftoutfile;
    }
  else
    {
      infile = argv[1];
      outfile = argv[2];
    }
  init_regs();
  init_memfile();
  read_input( infile );
  print_code_listing();
  do
    {
      WB();
      MEM3();
      MEM2();
      MEM1();
      EX();
      ID();
      IF2();
      IF1();
    } while( 0 );
  close_input();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 init_regs()
{
__int64 result; // rax

puts("init_regs()");
result = 0LL;
memset(&theRegisterFile, 0, 0x80uLL);
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax

puts("init_regs()");
result = 0LL;
memset(&theRegisterFile, 0, 0x80uLL);
return result;
}

original_code:
void
init_regs()
{
  short i;
  printf("init_regs()\n");
  for( i = 0; i < 32; ++i )
    theRegisterFile[i] = 0;
}

variable_types: i short


decompiled_code:
int __fastcall set_reg(char *s, int a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

puts("set_reg()");
result = sscanf(s, "R%d", &v3);
if ( result != 1 )
return 1;
if ( v3 <= 0x1F )
{
theRegisterFile[v3] = a2;
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char *s, int a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

puts("set_reg()");
result = sscanf(s, "R%d", &v3);
if ( result != 1 )
return 1;
if ( v3 <= 0x1F )
{
theRegisterFile[v3] = a2;
result = 0;
}
return result;
}

original_code:
int
set_reg( char * reg, Register data )
{
  int n;
  printf("set_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    return 1;
  if( ( n >= 32 ) || ( n < 0 ) )
    return 1;
  theRegisterFile[n] = data;
  return 0;
}

canonical_original_code:
int set_reg(char *reg, int data)
{
  int n;
  printf("set_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    return 1;
  if( ( n >= 32 ) || ( n < 0 ) )
    return 1;
  theRegisterFile[n] = data;
  return 0;
}

variable_types:
reg
char *

variable_types: data int