decompiled_code:
__int64 __fastcall tpm_quote(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // er15
unsigned int v3; // eax
int v4; // er12
unsigned int v5; // eax
unsigned int v6; // ebx
int v7; // er13
_WORD *v8; // rax
_WORD *v9; // r14
_WORD *v10; // rbp
size_t v11; // rdx
int v12; // eax
char v13; // cl
unsigned int v14; // eax
const char *v15; // rax
const char *v17; // rax
const char *v18; // rax
unsigned int v19; // eax
unsigned int v20; // ebp
__int64 v21; // rbx
void *v22; // rdi
unsigned int v23; // eax
const char *v24; // rax
unsigned int v25; // ebp
int v26; // ebx
unsigned int v27; // er12
char *v28; // rax
const char *v29; // rax
unsigned int v30; // [rsp+Ch] [rbp-BCh]
unsigned int v31; // [rsp+14h] [rbp-B4h] BYREF
unsigned int v32; // [rsp+18h] [rbp-B0h] BYREF
unsigned int v33; // [rsp+1Ch] [rbp-ACh] BYREF
unsigned int v34; // [rsp+20h] [rbp-A8h] BYREF
unsigned int v35; // [rsp+24h] [rbp-A4h] BYREF
unsigned int v36; // [rsp+28h] [rbp-A0h] BYREF
unsigned int v37; // [rsp+2Ch] [rbp-9Ch] BYREF
int v38; // [rsp+30h] [rbp-98h] BYREF
_DWORD n[3]; // [rsp+34h] [rbp-94h] BYREF
char v40[32]; // [rsp+40h] [rbp-88h] BYREF
char v41[4]; // [rsp+60h] [rbp-68h] BYREF
int v42; // [rsp+64h] [rbp-64h]
char *v43; // [rsp+68h] [rbp-60h]
size_t v44; // [rsp+80h] [rbp-48h]
void *src; // [rsp+88h] [rbp-40h]

syslog(3, "Request for TPM quote generation for nonce %s \n", a1);
v1 = take_ownership();
if ( v1 )
{
v2 = v1;
syslog(3, "tpm_quote Error 0x%X taking ownership of TPM.\n", v1);
}
else
{
v2 = tpm_create_context(&v31, &v32, &v33, &v36, &v35);
if ( !v2 )
{
v2 = get_nonce_sha1((__int64)a1, (__int64)v40, v31);
if ( v2 )
{
syslog(3, "Unable to b64 decode nonce \n");
}
else
{
v2 = load_aik_tpm(v31, v33, &v34);
if ( v2 )
{
syslog(3, "xenquote Unable to load citrix aik");
}
else
{
v38 = 16;
v3 = Tspi_TPM_GetCapability(v32, 19LL, 4LL, &v38, n, &n[1]);
v2 = v3;
if ( v3 )
{
v17 = (const char *)Trspi_Error_String(v3);
syslog(3, "Tspi_TPM_GetCapability failed with 0x%X %s", v2, v17);
}
else
{
v4 = **(_DWORD **)&n[1];
Tspi_Context_FreeMemory(v31);
v5 = Tspi_Context_CreateObject(v31, 4LL, 1LL, &v37);
v2 = v5;
if ( v5 )
{
v18 = (const char *)Trspi_Error_String(v5);
syslog(3, "Tspi_Context_CreateObject(PCR) failed with 0x%X %s", v2, v18);
}
else
{
v6 = (unsigned int)(v4 + 7) >> 3;
v7 = v6 + 20 * v4 + 6;
v8 = malloc(v7);
v9 = v8;
if ( v8 )
{
v10 = v8 + 1;
*v8 = _bswap_16(v6);
memset(v9 + 1, 0, v11);
if ( v4 )
{
while ( 1 )
{
v14 = Tspi_PcrComposite_SelectPcrIndex(v37, v2);
if ( v14 )
break;
v12 = v2;
v13 = v2++;
*((_BYTE *)v10 + (v12 >> 3)) |= 1 << (v13 & 7);
if ( v4 == v2 )
goto LABEL_22;
}
v30 = v14;
v15 = (const char *)Trspi_Error_String(v14);
syslog(3, "Tspi_PcrComposite_SelectPcrIndex failed with 0x%X %s", v30, v15);
v2 = v30;
}
else
{
LABEL_22:
v42 = 20;
v43 = v40;
v19 = Tspi_TPM_Quote(v32, v34, v37, v41);
v20 = v19;
if ( v19 )
{
v2 = v19;
v29 = (const char *)Trspi_Error_String(v19);
syslog(3, "Tspi_TPM_Quote failed with 0x%X %s", v20, v29);
}
else
{
v21 = (__int64)v9 + v6 + 6;
*(_DWORD *)(v21 - 4) = _bswap_32(20 * v4);
if ( v4 )
{
while ( 1 )
{
v23 = Tspi_PcrComposite_GetPcrValue(v37, v20, n, &n[1]);
v2 = v23;
if ( v23 )
break;
v22 = (void *)v21;
++v20;
v21 += n[0];
memcpy(v22, *(const void **)&n[1], n[0]);
if ( v4 == v20 )
goto LABEL_28;
}
v24 = (const char *)Trspi_Error_String(v23);
syslog(3, "Tspi_PcrComposite_GetPcrValue failed with 0x%X %s", v2, v24);
}
else
{
LABEL_28:
v25 = v44;
v26 = v21 - (_DWORD)v9;
v27 = v44 + v26;
v28 = (char *)realloc(v9, (int)v44 + v26);
v9 = v28;
if ( !v28 )
{
v2 = -3;
syslog(3, "Unable to realloc memory for size %d at %s and %d \n", v27, "xenquote.c", 286LL);
goto LABEL_18;
}
memcpy(&v28[v26], src, v25);
v2 = print_base64(v9, v27);
if ( v2 )
{
v2 = -3;
syslog(3, "Error in converting B64 %s and %d ", "xenquote.c", 296LL);
}
else
{
syslog(6, "Generate TPM Quote Success!\n");
}
}
}
}
}
else
{
v2 = -3;
syslog(3, "Unable to allocate memory %d , %s and %d \n", (unsigned int)v7, "xenquote.c", 197LL);
}
free(v9);
}
}
}
}
LABEL_18:
Tspi_Context_CloseObject(v31, v35);
tpm_free_context(v31, v36);
return v2;
}
syslog(3, "Error in aik context for generating aik_pem");
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // er15
unsigned int v3; // eax
int v4; // er12
unsigned int v5; // eax
unsigned int v6; // ebx
int v7; // er13
_WORD *v8; // rax
_WORD *v9; // r14
_WORD *v10; // rbp
size_t v11; // rdx
int v12; // eax
char v13; // cl
unsigned int v14; // eax
const char *v15; // rax
const char *v17; // rax
const char *v18; // rax
unsigned int v19; // eax
unsigned int v20; // ebp
__int64 v21; // rbx
void *v22; // rdi
unsigned int v23; // eax
const char *v24; // rax
unsigned int v25; // ebp
int v26; // ebx
unsigned int v27; // er12
char *v28; // rax
const char *v29; // rax
unsigned int v30; // [rsp+Ch] [rbp-BCh]
unsigned int v31; // [rsp+14h] [rbp-B4h] BYREF
unsigned int v32; // [rsp+18h] [rbp-B0h] BYREF
unsigned int v33; // [rsp+1Ch] [rbp-ACh] BYREF
unsigned int v34; // [rsp+20h] [rbp-A8h] BYREF
unsigned int v35; // [rsp+24h] [rbp-A4h] BYREF
unsigned int v36; // [rsp+28h] [rbp-A0h] BYREF
unsigned int v37; // [rsp+2Ch] [rbp-9Ch] BYREF
int v38; // [rsp+30h] [rbp-98h] BYREF
_DWORD n[3]; // [rsp+34h] [rbp-94h] BYREF
char v40[32]; // [rsp+40h] [rbp-88h] BYREF
char v41[4]; // [rsp+60h] [rbp-68h] BYREF
int v42; // [rsp+64h] [rbp-64h]
char *v43; // [rsp+68h] [rbp-60h]
size_t v44; // [rsp+80h] [rbp-48h]
void *src; // [rsp+88h] [rbp-40h]

syslog(3, "Request for TPM quote generation for nonce %s \n", a1);
v1 = take_ownership();
if ( v1 )
{
v2 = v1;
syslog(3, "tpm_quote Error 0x%X taking ownership of TPM.\n", v1);
}
else
{
v2 = tpm_create_context(&v31, &v32, &v33, &v36, &v35);
if ( !v2 )
{
v2 = func0((__int64)a1, (__int64)v40, v31);
if ( v2 )
{
syslog(3, "Unable to b64 decode nonce \n");
}
else
{
v2 = load_aik_tpm(v31, v33, &v34);
if ( v2 )
{
syslog(3, "xenquote Unable to load citrix aik");
}
else
{
v38 = 16;
v3 = Tspi_TPM_GetCapability(v32, 19LL, 4LL, &v38, n, &n[1]);
v2 = v3;
if ( v3 )
{
v17 = (const char *)Trspi_Error_String(v3);
syslog(3, "Tspi_TPM_GetCapability failed with 0x%X %s", v2, v17);
}
else
{
v4 = **(_DWORD **)&n[1];
Tspi_Context_FreeMemory(v31);
v5 = Tspi_Context_CreateObject(v31, 4LL, 1LL, &v37);
v2 = v5;
if ( v5 )
{
v18 = (const char *)Trspi_Error_String(v5);
syslog(3, "Tspi_Context_CreateObject(PCR) failed with 0x%X %s", v2, v18);
}
else
{
v6 = (unsigned int)(v4 + 7) >> 3;
v7 = v6 + 20 * v4 + 6;
v8 = malloc(v7);
v9 = v8;
if ( v8 )
{
v10 = v8 + 1;
*v8 = func1(v6);
memset(v9 + 1, 0, v11);
if ( v4 )
{
while ( 1 )
{
v14 = Tspi_PcrComposite_SelectPcrIndex(v37, v2);
if ( v14 )
break;
v12 = v2;
v13 = v2++;
*((_BYTE *)v10 + (v12 >> 3)) |= 1 << (v13 & 7);
if ( v4 == v2 )
goto LABEL_22;
}
v30 = v14;
v15 = (const char *)Trspi_Error_String(v14);
syslog(3, "Tspi_PcrComposite_SelectPcrIndex failed with 0x%X %s", v30, v15);
v2 = v30;
}
else
{
LABEL_22:
v42 = 20;
v43 = v40;
v19 = Tspi_TPM_Quote(v32, v34, v37, v41);
v20 = v19;
if ( v19 )
{
v2 = v19;
v29 = (const char *)Trspi_Error_String(v19);
syslog(3, "Tspi_TPM_Quote failed with 0x%X %s", v20, v29);
}
else
{
v21 = (__int64)v9 + v6 + 6;
*(_DWORD *)(v21 - 4) = func2(20 * v4);
if ( v4 )
{
while ( 1 )
{
v23 = Tspi_PcrComposite_GetPcrValue(v37, v20, n, &n[1]);
v2 = v23;
if ( v23 )
break;
v22 = (void *)v21;
++v20;
v21 += n[0];
memcpy(v22, *(const void **)&n[1], n[0]);
if ( v4 == v20 )
goto LABEL_28;
}
v24 = (const char *)Trspi_Error_String(v23);
syslog(3, "Tspi_PcrComposite_GetPcrValue failed with 0x%X %s", v2, v24);
}
else
{
LABEL_28:
v25 = v44;
v26 = v21 - (_DWORD)v9;
v27 = v44 + v26;
v28 = (char *)realloc(v9, (int)v44 + v26);
v9 = v28;
if ( !v28 )
{
v2 = -3;
syslog(3, "Unable to realloc memory for size %d at %s and %d \n", v27, "xenquote.c", 286LL);
goto LABEL_18;
}
memcpy(&v28[v26], src, v25);
v2 = print_base64(v9, v27);
if ( v2 )
{
v2 = -3;
syslog(3, "Error in converting B64 %s and %d ", "xenquote.c", 296LL);
}
else
{
syslog(6, "Generate TPM Quote Success!\n");
}
}
}
}
}
else
{
v2 = -3;
syslog(3, "Unable to allocate memory %d , %s and %d \n", (unsigned int)v7, "xenquote.c", 197LL);
}
free(v9);
}
}
}
}
LABEL_18:
Tspi_Context_CloseObject(v31, v35);
tpm_free_context(v31, v36);
return v2;
}
syslog(3, "Error in aik context for generating aik_pem");
}
return v2;
}

original_code:
int
tpm_quote(char * b64_nonce)
{
    TSS_HCONTEXT tpm_context;
    TSS_HTPM tpm_handle;
    TSS_HKEY srk_handle;
    TSS_HKEY aik_handle;
    TSS_HPOLICY srk_policy;
    TSS_HPOLICY tpm_policy;
    TSS_HPCRS pcr_handle;
    TSS_VALIDATION valid;
    UINT32 pcr_property;
    UINT32 max_pcr;
    BYTE* pcr_mask ;
    UINT32 mask_size;
    BYTE* quote_buf;
    UINT32 quote_buf_len;
    BYTE* marker;
    BYTE* api_buf;
    UINT32 api_buf_len;
    BYTE nonce_hash[20];
    int i;
    int result;
    int alloc_size;
    syslog(3, "Request for TPM quote generation for nonce %s \n", b64_nonce);
    result = take_ownership();
    if (result) {
        syslog(3, "tpm_quote Error 0x%X taking ownership of TPM.\n", result);
        goto out;
    }
    if ((result = tpm_create_context(&tpm_context, &tpm_handle, &srk_handle,
                &tpm_policy, &srk_policy)) != (UINT32)(0x00000000L)) {
        syslog(3, "Error in aik context for generating aik_pem");
        goto out;
    }
    if ((result = get_nonce_sha1(b64_nonce, nonce_hash,
                tpm_context)) != (UINT32)(0x00000000L)) {
        syslog(3, "Unable to b64 decode nonce \n");
        goto free_context;
    }
    if ((result = load_aik_tpm(tpm_context,
                srk_handle, &aik_handle)) != (UINT32)(0x00000000L)) {
        syslog(3, "xenquote Unable to load citrix aik");
        goto free_context;
    }
    pcr_property = (0x10);
    if ((result = Tspi_TPM_GetCapability(tpm_handle, (0x13),
          sizeof(pcr_property), (BYTE *)&pcr_property, &api_buf_len,
                &api_buf)) != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_GetCapability failed with 0x%X %s", result,
            Trspi_Error_String(result));
        goto free_context;
    }
    max_pcr = *(UINT32 *)api_buf;
    Tspi_Context_FreeMemory(tpm_context, api_buf);
    mask_size = ((max_pcr + 8 - 1 ) / 8);
    if ((result = Tspi_Context_CreateObject(tpm_context, (0x04),
          (0x00000001), &pcr_handle)) != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_Context_CreateObject(PCR) failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_context;
    }
    alloc_size = sizeof(UINT16) + mask_size + sizeof(UINT32) +
                    (0x14) * max_pcr;
    quote_buf = (BYTE*)malloc(alloc_size);
    if (!quote_buf) {
        syslog(3, "Unable to allocate memory %d , %s and %d \n",
        alloc_size, "xenquote.c", 197);
        result = -3;
        goto free_quote;
    }
    *(UINT16 *)quote_buf = htons(mask_size);
    pcr_mask = quote_buf + sizeof(UINT16);
    memset(pcr_mask, 0, mask_size);
    for (i = 0;i < max_pcr; i++) {
        result = Tspi_PcrComposite_SelectPcrIndex(pcr_handle, i);
        if (result != (UINT32)(0x00000000L)) {
            syslog(3, "Tspi_PcrComposite_SelectPcrIndex failed with 0x%X %s",
                 result, Trspi_Error_String(result));
            goto free_quote;
        }
        ((((BYTE*)pcr_mask)[i/8] |= 1 << (i%8)));
    }
    valid.ulExternalDataLength = sizeof(nonce_hash);
    valid.rgbExternalData = nonce_hash;
    result = Tspi_TPM_Quote(tpm_handle, aik_handle, pcr_handle, &valid);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_Quote failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_quote;
    }
    marker = quote_buf + sizeof(UINT16) + mask_size;
    *(UINT32 *)marker = htonl((0x14)*max_pcr);
    marker += sizeof(UINT32);
    for (i = 0;i < max_pcr; i++) {
        result = Tspi_PcrComposite_GetPcrValue(pcr_handle, i, &api_buf_len,
                &api_buf);
        if (result != (UINT32)(0x00000000L)) {
            syslog(3, "Tspi_PcrComposite_GetPcrValue failed with 0x%X %s",
                    result, Trspi_Error_String(result));
            goto free_quote;
        }
        memcpy (marker, api_buf, api_buf_len);
        marker += api_buf_len;
    }
    quote_buf_len = marker - quote_buf;
    alloc_size = quote_buf_len + valid.ulValidationDataLength;
    quote_buf = realloc(quote_buf, alloc_size);
    if (!quote_buf) {
        syslog(3, "Unable to realloc memory for size %d at %s and %d \n",
            alloc_size, "xenquote.c", 286);
        result = -3;
        goto free_context;
    }
    memcpy(&quote_buf[quote_buf_len], valid.rgbValidationData,
            valid.ulValidationDataLength);
    quote_buf_len += valid.ulValidationDataLength;
    if ((result = print_base64(quote_buf, quote_buf_len)) != 0) {
        syslog(3, "Error in converting B64 %s and %d ", "xenquote.c", 296);
        result = -3;
        goto free_quote;
    }
    syslog(6, "Generate TPM Quote Success!\n");
free_quote:
    free(quote_buf);
free_context:
    Tspi_Context_CloseObject(tpm_context, srk_policy);
    tpm_free_context(tpm_context, tpm_policy);
out:
    return result;
}

canonical_original_code:
int tpm_quote(char *b64_nonce)
{
    unsigned int tpm_context;
    unsigned int tpm_handle;
    unsigned int srk_handle;
    unsigned int aik_handle;
    unsigned int srk_policy;
    unsigned int tpm_policy;
    unsigned int pcr_handle;
    struct tdTSS_VALIDATION valid;
    unsigned int pcr_property;
    unsigned int max_pcr;
    unsigned char *pcr_mask;
    unsigned int mask_size;
    unsigned char *quote_buf;
    unsigned int quote_buf_len;
    unsigned char *marker;
    unsigned char *api_buf;
    unsigned int api_buf_len;
    unsigned char nonce_hash[20];
    int i;
    int result;
    int alloc_size;
    syslog(3, "Request for TPM quote generation for nonce %s \n", b64_nonce);
    result = take_ownership();
    if (result) {
        syslog(3, "tpm_quote Error 0x%X taking ownership of TPM.\n", result);
        goto out;
    }
    if ((result = tpm_create_context(&tpm_context, &tpm_handle, &srk_handle,
                &tpm_policy, &srk_policy)) != (UINT32)(0x00000000L)) {
        syslog(3, "Error in aik context for generating aik_pem");
        goto out;
    }
    if ((result = get_nonce_sha1(b64_nonce, nonce_hash,
                tpm_context)) != (UINT32)(0x00000000L)) {
        syslog(3, "Unable to b64 decode nonce \n");
        goto free_context;
    }
    if ((result = load_aik_tpm(tpm_context,
                srk_handle, &aik_handle)) != (UINT32)(0x00000000L)) {
        syslog(3, "xenquote Unable to load citrix aik");
        goto free_context;
    }
    pcr_property = (0x10);
    if ((result = Tspi_TPM_GetCapability(tpm_handle, (0x13),
          sizeof(pcr_property), (unsigned char *)&pcr_property, &api_buf_len,
                &api_buf)) != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_GetCapability failed with 0x%X %s", result,
            Trspi_Error_String(result));
        goto free_context;
    }
    max_pcr = *(unsigned int *)api_buf;
    Tspi_Context_FreeMemory(tpm_context, api_buf);
    mask_size = ((max_pcr + 8 - 1 ) / 8);
    if ((result = Tspi_Context_CreateObject(tpm_context, (0x04),
          (0x00000001), &pcr_handle)) != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_Context_CreateObject(PCR) failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_context;
    }
    alloc_size = sizeof(UINT16) + mask_size + sizeof(UINT32) +
                    (0x14) * max_pcr;
    quote_buf = (unsigned char *)malloc(alloc_size);
    if (!quote_buf) {
        syslog(3, "Unable to allocate memory %d , %s and %d \n",
        alloc_size, "xenquote.c", 197);
        result = -3;
        goto free_quote;
    }
    *(unsigned short *)quote_buf = htons(mask_size);
    pcr_mask = quote_buf + sizeof(UINT16);
    memset(pcr_mask, 0, mask_size);
    for (i = 0;i < max_pcr; i++) {
        result = Tspi_PcrComposite_SelectPcrIndex(pcr_handle, i);
        if (result != (UINT32)(0x00000000L)) {
            syslog(3, "Tspi_PcrComposite_SelectPcrIndex failed with 0x%X %s",
                 result, Trspi_Error_String(result));
            goto free_quote;
        }
        ((((unsigned char *)pcr_mask)[i/8] |= 1 << (i%8)));
    }
    valid.ulExternalDataLength = sizeof(nonce_hash);
    valid.rgbExternalData = nonce_hash;
    result = Tspi_TPM_Quote(tpm_handle, aik_handle, pcr_handle, &valid);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_Quote failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_quote;
    }
    marker = quote_buf + sizeof(UINT16) + mask_size;
    *(unsigned int *)marker = htonl((0x14)*max_pcr);
    marker += sizeof(UINT32);
    for (i = 0;i < max_pcr; i++) {
        result = Tspi_PcrComposite_GetPcrValue(pcr_handle, i, &api_buf_len,
                &api_buf);
        if (result != (UINT32)(0x00000000L)) {
            syslog(3, "Tspi_PcrComposite_GetPcrValue failed with 0x%X %s",
                    result, Trspi_Error_String(result));
            goto free_quote;
        }
        memcpy (marker, api_buf, api_buf_len);
        marker += api_buf_len;
    }
    quote_buf_len = marker - quote_buf;
    alloc_size = quote_buf_len + valid.ulValidationDataLength;
    quote_buf = realloc(quote_buf, alloc_size);
    if (!quote_buf) {
        syslog(3, "Unable to realloc memory for size %d at %s and %d \n",
            alloc_size, "xenquote.c", 286);
        result = -3;
        goto free_context;
    }
    memcpy(&quote_buf[quote_buf_len], valid.rgbValidationData,
            valid.ulValidationDataLength);
    quote_buf_len += valid.ulValidationDataLength;
    if ((result = print_base64(quote_buf, quote_buf_len)) != 0) {
        syslog(3, "Error in converting B64 %s and %d ", "xenquote.c", 296);
        result = -3;
        goto free_quote;
    }
    syslog(6, "Generate TPM Quote Success!\n");
free_quote:
    free(quote_buf);
free_context:
    Tspi_Context_CloseObject(tpm_context, srk_policy);
    tpm_free_context(tpm_context, tpm_policy);
out:
    return result;
}

variable_types: struct tdTSS_VALIDATION { struct tdTSS_VERSION { unsigned char bMajor; unsigned char bMinor; unsigned char bRevMajor; unsigned char bRevMinor; } versionInfo; unsigned int ulExternalDataLength; unsigned char *rgbExternalData; unsigned int ulDataLength; unsigned char *rgbData; unsigned int ulValidationDataLength; unsigned char *rgbValidationData; }

variable_types:
marker
unsigned char *

variable_types: i int

variable_types: result int

user_defined_types: struct tdTSS_VALIDATION { struct tdTSS_VERSION versionInfo; unsigned int ulExternalDataLength; unsigned char *rgbExternalData; unsigned int ulDataLength; unsigned char *rgbData; unsigned int ulValidationDataLength; unsigned char *rgbValidationData; }


repo: xenserver/xentpm

decompiled_code:
__int64 __fastcall tpm_challenge(__int64 a1)
{
__int64 v1; // rax
unsigned int *v2; // rbx
unsigned int v3; // er12
unsigned int v4; // er12
__int64 v5; // r10
unsigned int *v6; // rsi
unsigned __int64 v7; // rdx
unsigned int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
unsigned int v11; // er10
unsigned int *v12; // r9
__int64 v13; // rsi
const char *v15; // rax
__int64 v16; // [rsp-8h] [rbp-50h]
unsigned int v17; // [rsp+8h] [rbp-40h] BYREF
unsigned int v18; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v19; // [rsp+10h] [rbp-38h] BYREF
unsigned int v20; // [rsp+14h] [rbp-34h] BYREF
unsigned int v21; // [rsp+18h] [rbp-30h] BYREF
unsigned int v22; // [rsp+1Ch] [rbp-2Ch] BYREF
unsigned int v23; // [rsp+20h] [rbp-28h] BYREF
unsigned int v24; // [rsp+24h] [rbp-24h] BYREF
__int64 v25[4]; // [rsp+28h] [rbp-20h] BYREF

syslog(6, "Recieved a Challange");
v1 = base64_decode(a1, &v24);
if ( !v1 )
{
v4 = 3;
syslog(3, "Unable to b64 decode challange \n");
LABEL_10:
Tspi_Context_CloseObject(v17, v22);
tpm_free_context(v17, v21);
return v4;
}
v2 = (unsigned int *)v1;
v3 = tpm_create_context(&v17, &v18, &v19, &v21, &v22);
if ( !v3 )
{
v4 = load_aik_tpm(v17, v19, &v20);
if ( v4 )
{
syslog(3, "challange Unable to load citrix aik");
}
else if ( v24 <= 7 )
{
v4 = 3;
syslog(3, "Challenge Length too small\n");
}
else
{
v5 = (unsigned int)_bswap_32(*v2);
v6 = (unsigned int *)((char *)v2 + v5 + 4);
if ( v7 < v5 + 8 )
{
v4 = 3;
syslog(3, "Challenge incomplete\n");
}
else
{
v8 = _bswap_32(*v6);
if ( v9 == v11 + v8 + 8LL )
{
v12 = v6 + 1;
v13 = v20;
v4 = Tspi_TPM_ActivateIdentity(v18, v20, v11, v2 + 1, v8, v12, &v23, v25);
if ( v4 )
{
v15 = (const char *)Trspi_Error_String(v4, v13, v16);
syslog(3, "Tspi_TPM_ActivateIdentity failed with 0x%X %s", v4, v15);
}
else
{
v4 = print_base64(v25[0], v23, v16);
if ( v4 )
syslog(3, "Error in converting B64 %s and %d ", "challenge.c", 160LL);
else
syslog(6, "XenTPM challange success!\n");
}
}
else
{
v4 = 3;
syslog(3, "Challenge does not have SYM and ASYM keys\n", v9, v10, v8);
}
}
}
goto LABEL_10;
}
syslog(3, "Error in aik context for generating aik");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rax
unsigned int *v2; // rbx
unsigned int v3; // er12
unsigned int v4; // er12
__int64 v5; // r10
unsigned int *v6; // rsi
unsigned __int64 v7; // rdx
unsigned int v8; // eax
__int64 v9; // rdx
__int64 v10; // rcx
unsigned int v11; // er10
unsigned int *v12; // r9
__int64 v13; // rsi
const char *v15; // rax
__int64 v16; // [rsp-8h] [rbp-50h]
unsigned int v17; // [rsp+8h] [rbp-40h] BYREF
unsigned int v18; // [rsp+Ch] [rbp-3Ch] BYREF
unsigned int v19; // [rsp+10h] [rbp-38h] BYREF
unsigned int v20; // [rsp+14h] [rbp-34h] BYREF
unsigned int v21; // [rsp+18h] [rbp-30h] BYREF
unsigned int v22; // [rsp+1Ch] [rbp-2Ch] BYREF
unsigned int v23; // [rsp+20h] [rbp-28h] BYREF
unsigned int v24; // [rsp+24h] [rbp-24h] BYREF
__int64 v25[4]; // [rsp+28h] [rbp-20h] BYREF

syslog(6, "Recieved a Challange");
v1 = base64_decode(a1, &v24);
if ( !v1 )
{
v4 = 3;
syslog(3, "Unable to b64 decode challange \n");
LABEL_10:
Tspi_Context_CloseObject(v17, v22);
tpm_free_context(v17, v21);
return v4;
}
v2 = (unsigned int *)v1;
v3 = tpm_create_context(&v17, &v18, &v19, &v21, &v22);
if ( !v3 )
{
v4 = load_aik_tpm(v17, v19, &v20);
if ( v4 )
{
syslog(3, "challange Unable to load citrix aik");
}
else if ( v24 <= 7 )
{
v4 = 3;
syslog(3, "Challenge Length too small\n");
}
else
{
v5 = (unsigned int)func0(*v2);
v6 = (unsigned int *)((char *)v2 + v5 + 4);
if ( v7 < v5 + 8 )
{
v4 = 3;
syslog(3, "Challenge incomplete\n");
}
else
{
v8 = func0(*v6);
if ( v9 == v11 + v8 + 8LL )
{
v12 = v6 + 1;
v13 = v20;
v4 = Tspi_TPM_ActivateIdentity(v18, v20, v11, v2 + 1, v8, v12, &v23, v25);
if ( v4 )
{
v15 = (const char *)Trspi_Error_String(v4, v13, v16);
syslog(3, "Tspi_TPM_ActivateIdentity failed with 0x%X %s", v4, v15);
}
else
{
v4 = print_base64(v25[0], v23, v16);
if ( v4 )
syslog(3, "Error in converting B64 %s and %d ", "challenge.c", 160LL);
else
syslog(6, "XenTPM challange success!\n");
}
}
else
{
v4 = 3;
syslog(3, "Challenge does not have SYM and ASYM keys\n", v9, v10, v8);
}
}
}
goto LABEL_10;
}
syslog(3, "Error in aik context for generating aik");
return v3;
}

original_code:
int tpm_challenge(char *b64_challenge)
{
    TSS_HCONTEXT context;
    TSS_HTPM tpm_handle;
    TSS_HKEY srk_handle;
    TSS_HKEY aik_handle;
    TSS_HPOLICY tpm_policy;
    TSS_HPOLICY srk_policy;
    BYTE *response;
    UINT32 responseLen;
    BYTE *asymCA_data;
    UINT32 asymCA_data_len;
    BYTE *symCA_data;
    UINT32 symCA_data_len;
    BYTE* challenge = ((void *)0);
    int challenge_len;
    int result;
    syslog(6, "Recieved a Challange");
    challenge = base64_decode(b64_challenge, &challenge_len);
    if (!challenge) {
        syslog(3, "Unable to b64 decode challange \n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    result = tpm_create_context(&context, &tpm_handle, &srk_handle,
            &tpm_policy, &srk_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error in aik context for generating aik");
        goto out;
    }
    if ((result = load_aik_tpm(context,
            srk_handle, &aik_handle)) != 0) {
        syslog(3, "challange Unable to load citrix aik");
        goto free_context;
    }
    if (challenge_len < 2*sizeof(UINT32)){
        result = (UINT32)(0x00000000L + 0x003L);
        syslog(3, "Challenge Length too small\n");
        goto free_context;
    }
    asymCA_data_len = ntohl(*(UINT32*)challenge);
    asymCA_data = challenge + sizeof(UINT32);
    challenge += (asymCA_data_len + sizeof(UINT32));
    if (challenge_len < asymCA_data_len + 2*sizeof(UINT32)) {
        syslog(3, "Challenge incomplete\n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    symCA_data_len = ntohl(*(UINT32*)challenge);
    if (challenge_len != asymCA_data_len + symCA_data_len + 2*sizeof(UINT32)) {
        syslog(3, "Challenge does not have SYM and ASYM keys\n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    symCA_data = challenge + sizeof(UINT32);
    result = Tspi_TPM_ActivateIdentity(tpm_handle, aik_handle, asymCA_data_len,
                asymCA_data, symCA_data_len, symCA_data, &responseLen,
                &response);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_ActivateIdentity failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_context;
    }
    if ((result = print_base64(response, responseLen)) != 0) {
        syslog(3, "Error in converting B64 %s and %d ",
            "challenge.c",160);
        goto free_context;
    }
    syslog(6, "XenTPM challange success!\n");
free_context:
    Tspi_Context_CloseObject(context, srk_policy);
    tpm_free_context(context, tpm_policy);
out:
    return result;
}

canonical_original_code:
int tpm_challenge(char *b64_challenge)
{
    unsigned int context;
    unsigned int tpm_handle;
    unsigned int srk_handle;
    unsigned int aik_handle;
    unsigned int tpm_policy;
    unsigned int srk_policy;
    unsigned char *response;
    unsigned int responseLen;
    unsigned char *asymCA_data;
    unsigned int asymCA_data_len;
    unsigned char *symCA_data;
    unsigned int symCA_data_len;
    unsigned char *challenge = ((void *)0);
    int challenge_len;
    int result;
    syslog(6, "Recieved a Challange");
    challenge = base64_decode(b64_challenge, &challenge_len);
    if (!challenge) {
        syslog(3, "Unable to b64 decode challange \n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    result = tpm_create_context(&context, &tpm_handle, &srk_handle,
            &tpm_policy, &srk_policy);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Error in aik context for generating aik");
        goto out;
    }
    if ((result = load_aik_tpm(context,
            srk_handle, &aik_handle)) != 0) {
        syslog(3, "challange Unable to load citrix aik");
        goto free_context;
    }
    if (challenge_len < 2*sizeof(UINT32)){
        result = (UINT32)(0x00000000L + 0x003L);
        syslog(3, "Challenge Length too small\n");
        goto free_context;
    }
    asymCA_data_len = ntohl(*(unsigned int *)challenge);
    asymCA_data = challenge + sizeof(UINT32);
    challenge += (asymCA_data_len + sizeof(UINT32));
    if (challenge_len < asymCA_data_len + 2*sizeof(UINT32)) {
        syslog(3, "Challenge incomplete\n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    symCA_data_len = ntohl(*(unsigned int *)challenge);
    if (challenge_len != asymCA_data_len + symCA_data_len + 2*sizeof(UINT32)) {
        syslog(3, "Challenge does not have SYM and ASYM keys\n");
        result = (UINT32)(0x00000000L + 0x003L);
        goto free_context;
    }
    symCA_data = challenge + sizeof(UINT32);
    result = Tspi_TPM_ActivateIdentity(tpm_handle, aik_handle, asymCA_data_len,
                asymCA_data, symCA_data_len, symCA_data, &responseLen,
                &response);
    if (result != (UINT32)(0x00000000L)) {
        syslog(3, "Tspi_TPM_ActivateIdentity failed with 0x%X %s",
            result, Trspi_Error_String(result));
        goto free_context;
    }
    if ((result = print_base64(response, responseLen)) != 0) {
        syslog(3, "Error in converting B64 %s and %d ",
            "challenge.c",160);
        goto free_context;
    }
    syslog(6, "XenTPM challange success!\n");
free_context:
    Tspi_Context_CloseObject(context, srk_policy);
    tpm_free_context(context, tpm_policy);
out:
    return result;
}

variable_types:
context
unsigned int

variable_types:
response
unsigned char *

variable_types:
responseLen
unsigned int

variable_types:
challenge
unsigned char *

return_type: int


mstots

repo: xenserver/xha

decompiled_code:
__int64 __fastcall mstots(__int64 a1)
{
return a1 / 1000;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1 / 1000;
}

original_code:
static __inline struct timespec
mstots(MTC_CLOCK ms)
{
    struct timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000 * 1000;
    return ts;
}

canonical_original_code:
static __inline struct timespec mstots(long long ms)
{
    struct timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000 * 1000;
    return ts;
}

variable_types:
ms
long long

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


mstotv

decompiled_code:
unsigned __int64 __fastcall mstotv(unsigned __int64 a1)
{
return a1 / 0x3E8;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(unsigned __int64 a1)
{
return a1 / 0x3E8;
}

original_code:
static __inline struct timeval
mstotv(MTC_CLOCK ms)
{
    struct timeval tv;
    tv.tv_sec = ms / 1000;
    tv.tv_usec = (ms % 1000) * 1000;
    return tv;
}

canonical_original_code:
static __inline struct timeval mstotv(long long ms)
{
    struct timeval tv;
    tv.tv_sec = ms / 1000;
    tv.tv_usec = (ms % 1000) * 1000;
    return tv;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int __fastcall xm_sleep(__int64 a1)
{
struct timespec v1; // rax
int result; // eax
__m128i i; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = mstots(a1);
remaining = v1;
for ( i = (__m128i)v1; ; i = _mm_loadu_si128((const __m128i *)&remaining) )
{
result = nanosleep((const struct timespec *)&i, &remaining);
if ( !result )
break;
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
struct timespec v1; // rax
int result; // eax
__m128i i; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = func0(a1);
remaining = v1;
for ( i = (__m128i)v1; ; i = _mm_loadu_si128((const __m128i *)&remaining) )
{
result = nanosleep((const struct timespec *)&i, &remaining);
if ( !result )
break;
}
return result;
}

original_code:
void
xm_sleep(
    MTC_CLOCK msec)
{
    struct timespec ts, ts_rem;
    ts = ts_rem = mstots(msec);
    while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
}

canonical_original_code:
void xm_sleep(long long msec)
{
    struct timespec ts; struct timespec ts_rem;
    ts = ts_rem = mstots(msec);
    while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
}

variable_types:
msec
long long

return_type: void

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


decompiled_code:
int xm_close_descriptors()
{
rlim_t v0; // rax
__int64 v1; // rbx
__int64 v2; // rax
struct rlimit v4; // [rsp+0h] [rbp-18h] BYREF

LODWORD(v0) = getrlimit(RLIMIT_NOFILE, &v4);
if ( v4.rlim_cur > 2 )
{
v1 = 0LL;
do
{
close(v1 + 2);
v2 = v1++;
v0 = v2 + 3;
}
while ( v4.rlim_cur > v0 );
}
return v0;
}

canonical_decompiled_code:
int func5()
{
rlim_t v0; // rax
__int64 v1; // rbx
__int64 v2; // rax
struct rlimit v4; // [rsp+0h] [rbp-18h] BYREF

LODWORD(v0) = getrlimit(RLIMIT_NOFILE, &v4);
if ( v4.rlim_cur > 2 )
{
v1 = 0LL;
do
{
close(v1 + 2);
v2 = v1++;
v0 = v2 + 3;
}
while ( v4.rlim_cur > v0 );
}
return v0;
}

original_code:
void
xm_close_descriptors()
{
    struct rlimit rlimit;
    int fd;
    getrlimit(RLIMIT_NOFILE, &rlimit);
    for (fd = 2; fd < rlimit.rlim_cur; fd++)
    {
        close(fd);
    }
}

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

variable_types: fd int


decompiled_code:
__int64 __fastcall xapi_healthcheck(__pid_t *a1, _DWORD *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int *v4; // rax
int *v5; // rax
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) < 0 )
{
v5 = _errno_location();
log_message(131075LL, "Xapimon: cannot create pipe. (%d)\n", (unsigned int)*v5);
result = 2002LL;
}
else
{
v2 = fork();
*a1 = v2;
if ( !v2 )
{
dup2(pipedes[1], 1);
close(pipedes[0]);
close(pipedes[1]);
xm_close_descriptors();
main_reset_scheduler();
execl("/opt/xensource/libexec/xapi-health-check", "xapi-health-check", 0LL);
exit(-1);
}
if ( v2 < 0 )
{
v4 = _errno_location();
log_message(131075LL, "Xapimon: cannot start xapi healthchecker. (%d)\n", (unsigned int)*v4);
close(pipedes[0]);
close(pipedes[1]);
result = 2003LL;
}
else
{
close(pipedes[1]);
*a2 = pipedes[0];
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__pid_t *a1, _DWORD *a2)
{
__pid_t v2; // eax
__int64 result; // rax
int *v4; // rax
int *v5; // rax
int pipedes[2]; // [rsp+8h] [rbp-20h] BYREF

if ( pipe(pipedes) < 0 )
{
v5 = _errno_location();
log_message(131075LL, "Xapimon: cannot create pipe. (%d)\n", (unsigned int)*v5);
result = 2002LL;
}
else
{
v2 = fork();
*a1 = v2;
if ( !v2 )
{
dup2(pipedes[1], 1);
close(pipedes[0]);
close(pipedes[1]);
func5();
main_reset_scheduler();
execl("/opt/xensource/libexec/xapi-health-check", "xapi-health-check", 0LL);
exit(-1);
}
if ( v2 < 0 )
{
v4 = _errno_location();
log_message(131075LL, "Xapimon: cannot start xapi healthchecker. (%d)\n", (unsigned int)*v4);
close(pipedes[0]);
close(pipedes[1]);
result = 2003LL;
}
else
{
close(pipedes[1]);
*a2 = pipedes[0];
result = 0LL;
}
}
return result;
}

original_code:
MTC_STATUS
xapi_healthcheck(
    pid_t *ppid,
    PMTC_S32 ppip)
{
    MTC_S32 pip[2];
    if (pipe(pip) < 0)
    {
        log_message(((((3 | (1<<16) | (1<<17))) & ~(1<<16)) | (1<<17)), "Xapimon: cannot create pipe. (%d)\n", (*__errno_location ()));
        return (1000 + 1000 + 2);
    }
    if ((*ppid = fork()) == 0)
    {
        dup2(pip[1], 1);
        close(pip[0]);
        close(pip[1]);
        xm_close_descriptors();
        main_reset_scheduler();
        execl("/opt/xensource/libexec/" "xapi-health-check",
              "xapi-health-check", (char *) ((void *)0));
        _exit(-1);
    }
    if (*ppid < 0)
    {
        log_message(((((3 | (1<<16) | (1<<17))) & ~(1<<16)) | (1<<17)), "Xapimon: cannot start xapi healthchecker. (%d)\n", (*__errno_location ()));
        close(pip[0]);
        close(pip[1]);
        return (1000 + 1000 + 3);
    }
    else
    {
        close(pip[1]);
        *ppip = pip[0];
        return 0;
    }
}

canonical_original_code:
int xapi_healthcheck(int *ppid, int *ppip)
{
    int pip[2];
    if (pipe(pip) < 0)
    {
        log_message(((((3 | (1<<16) | (1<<17))) & ~(1<<16)) | (1<<17)), "Xapimon: cannot create pipe. (%d)\n", (*__errno_location ()));
        return (1000 + 1000 + 2);
    }
    if ((*ppid = fork()) == 0)
    {
        dup2(pip[1], 1);
        close(pip[0]);
        close(pip[1]);
        xm_close_descriptors();
        main_reset_scheduler();
        execl("/opt/xensource/libexec/" "xapi-health-check",
              "xapi-health-check", (char *) ((void *)0));
        _exit(-1);
    }
    if (*ppid < 0)
    {
        log_message(((((3 | (1<<16) | (1<<17))) & ~(1<<16)) | (1<<17)), "Xapimon: cannot start xapi healthchecker. (%d)\n", (*__errno_location ()));
        close(pip[0]);
        close(pip[1]);
        return (1000 + 1000 + 3);
    }
    else
    {
        close(pip[1]);
        *ppip = pip[0];
        return 0;
    }
}

variable_types:
ppid
int *

variable_types:
ppip
int *

variable_types:
pip
int [2]


decompiled_code:
__int64 restart_xapi()
{
__pid_t v0; // eax
unsigned int v1; // er12
int *v3; // rax

v0 = fork();
if ( !v0 )
{
xm_close_descriptors();
main_reset_scheduler();
if ( execlp("service", "service", "xapi", "restart", 0LL) < 0 && *_errno_location() == 2 )
execl("/sbin/service", "service", "xapi", "restart", 0LL);
exit(-1);
}
v1 = v0;
if ( v0 < 0 )
{
v3 = _errno_location();
log_message(131076LL, "Xapimon: cannot fork xapi restart process. (%d)\n", (unsigned int)*v3);
}
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
__pid_t v0; // eax
unsigned int v1; // er12
int *v3; // rax

v0 = fork();
if ( !v0 )
{
func5();
main_reset_scheduler();
if ( execlp("service", "service", "xapi", "restart", 0LL) < 0 && *_errno_location() == 2 )
execl("/sbin/service", "service", "xapi", "restart", 0LL);
exit(-1);
}
v1 = v0;
if ( v0 < 0 )
{
v3 = _errno_location();
log_message(131076LL, "Xapimon: cannot fork xapi restart process. (%d)\n", (unsigned int)*v3);
}
return v1;
}

original_code:
pid_t
restart_xapi()
{
    pid_t pid;
    if ((pid = fork()) == 0)
    {
        xm_close_descriptors();
        main_reset_scheduler();
        if (execlp("service",
                   "service", "xapi", "restart",
                   (char *) ((void *)0)) < 0)
        {
            switch ((*__errno_location ()))
            {
            case 2:
                execl("/sbin/" "service",
                      "service", "xapi", "restart",
                      (char *) ((void *)0));
                break;
            default:
                break;
            }
        }
        _exit(-1);
    }
    if (pid < 0)
    {
        log_message((4 | (1<<17)),
            "Xapimon: cannot fork xapi restart process. (%d)\n", (*__errno_location ()));
    }
    return pid;
}

canonical_original_code:
int restart_xapi()
{
    int pid;
    if ((pid = fork()) == 0)
    {
        xm_close_descriptors();
        main_reset_scheduler();
        if (execlp("service",
                   "service", "xapi", "restart",
                   (char *) ((void *)0)) < 0)
        {
            switch ((*__errno_location ()))
            {
            case 2:
                execl("/sbin/" "service",
                      "service", "xapi", "restart",
                      (char *) ((void *)0));
                break;
            default:
                break;
            }
        }
        _exit(-1);
    }
    if (pid < 0)
    {
        log_message((4 | (1<<17)),
            "Xapimon: cannot fork xapi restart process. (%d)\n", (*__errno_location ()));
    }
    return pid;
}

variable_types: pid int


decompiled_code:
__int64 Xapi_license_check()
{
__pid_t v0; // eax
unsigned int v1; // ebp
__int64 result; // rax
int *v3; // rax
struct timespec v4; // [rsp+0h] [rbp-18h] BYREF

v0 = fork();
if ( !v0 )
{
xm_close_descriptors();
main_reset_scheduler();
execl("/opt/xensource/libexec/xha-lc", "xha-lc", 0LL);
exit(-1);
}
if ( v0 < 0 )
{
v3 = _errno_location();
log_message(131076LL, "Xapimon: cannot fork xapi license checker. (%d)\n", (unsigned int)*v3);
result = 0xFFFFFFFFLL;
}
else
{
v1 = v0;
clock_gettime(1, &v4);
if ( v1 == wait_pid_timeout(
v1,
(int *)&v4,
1000 * v4.tv_sec + v4.tv_nsec / 1000000,
(unsigned int)(1000 * ha_config[984])) )
{
result = LODWORD(v4.tv_sec);
if ( LODWORD(v4.tv_sec) )
{
log_message(131076LL, "Xapimon: Xapi license checker failed. (%d)\n", LODWORD(v4.tv_sec));
result = 0xFFFFFFFFLL;
}
}
else
{
log_message(131076LL, "Xapimon: Xapi license checker (pid=%d) is fung.\n", v1);
kill(v1, 9);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
__pid_t v0; // eax
unsigned int v1; // ebp
__int64 result; // rax
int *v3; // rax
struct timespec v4; // [rsp+0h] [rbp-18h] BYREF

v0 = fork();
if ( !v0 )
{
func5();
main_reset_scheduler();
execl("/opt/xensource/libexec/xha-lc", "xha-lc", 0LL);
exit(-1);
}
if ( v0 < 0 )
{
v3 = _errno_location();
log_message(131076LL, "Xapimon: cannot fork xapi license checker. (%d)\n", (unsigned int)*v3);
result = 0xFFFFFFFFLL;
}
else
{
v1 = v0;
clock_gettime(1, &v4);
if ( v1 == func3(
v1,
(int *)&v4,
1000 * v4.tv_sec + v4.tv_nsec / 1000000,
(unsigned int)(1000 * ha_config[984])) )
{
result = LODWORD(v4.tv_sec);
if ( LODWORD(v4.tv_sec) )
{
log_message(131076LL, "Xapimon: Xapi license checker failed. (%d)\n", LODWORD(v4.tv_sec));
result = 0xFFFFFFFFLL;
}
}
else
{
log_message(131076LL, "Xapimon: Xapi license checker (pid=%d) is fung.\n", v1);
kill(v1, 9);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
MTC_S32
Xapi_license_check()
{
    pid_t pid, ret_pid;
    MTC_CLOCK time_start_license_check;
    MTC_S32 status, ret;
    if ((pid = fork()) == 0)
    {
        xm_close_descriptors();
        main_reset_scheduler();
        execl("/opt/xensource/libexec/" "xha-lc",
              "xha-lc", (char *) ((void *)0));
        _exit(-1);
    }
    if (pid < 0)
    {
        log_message((4 | (1<<17)),
            "Xapimon: cannot fork xapi license checker. (%d)\n", (*__errno_location ()));
        ret = -1;
    }
    else
    {
        time_start_license_check = ({ struct timespec ts; clock_gettime(1, &ts); ((MTC_CLOCK) ((ts).tv_sec) * 1000 + (ts).tv_nsec / 1000 / 1000); });
        ret_pid = wait_pid_timeout(pid, &status,
                                   time_start_license_check, (ha_config.common.xapi_licensecheck_timeout) * (1000));
        if (ret_pid == pid)
        {
            switch (status)
            {
            case 0:
                ret = 0;
                break;
            default:
                log_message((4 | (1<<17)),
                            "Xapimon: Xapi license checker failed. (%d)\n", status);
                ret = -1;
                break;
            }
        }
        else
        {
            log_message((4 | (1<<17)),
                        "Xapimon: Xapi license checker (pid=%d) is fung.\n", pid);
            kill(pid, 9);
            ret = -1;
        }
    }
    return ret;
}

canonical_original_code:
int Xapi_license_check()
{
    int pid; int ret_pid;
    long long time_start_license_check;
    int status; int ret;
    if ((pid = fork()) == 0)
    {
        xm_close_descriptors();
        main_reset_scheduler();
        execl("/opt/xensource/libexec/" "xha-lc",
              "xha-lc", (char *) ((void *)0));
        _exit(-1);
    }
    if (pid < 0)
    {
        log_message((4 | (1<<17)),
            "Xapimon: cannot fork xapi license checker. (%d)\n", (*__errno_location ()));
        ret = -1;
    }
    else
    {
        time_start_license_check = ({ struct timespec ts; clock_gettime(1, &ts); ((MTC_CLOCK) ((ts).tv_sec) * 1000 + (ts).tv_nsec / 1000 / 1000); });
        ret_pid = wait_pid_timeout(pid, &status,
                                   time_start_license_check, (ha_config.common.xapi_licensecheck_timeout) * (1000));
        if (ret_pid == pid)
        {
            switch (status)
            {
            case 0:
                ret = 0;
                break;
            default:
                log_message((4 | (1<<17)),
                            "Xapimon: Xapi license checker failed. (%d)\n", status);
                ret = -1;
                break;
            }
        }
        else
        {
            log_message((4 | (1<<17)),
                        "Xapimon: Xapi license checker (pid=%d) is fung.\n", pid);
            kill(pid, 9);
            ret = -1;
        }
    }
    return ret;
}

variable_types: status int

variable_types: ret int


mstots

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


sigcatch

decompiled_code:
void __fastcall sigcatch(int a1)
{
sigset(a1, sigcatch);
if ( a1 == 15 )
{
dword_2598 = 1;
}
else if ( a1 != 17 )
{
if ( a1 == 1 )
dword_259C = 1;
else
dword_25A0 = a1;
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
sigset(a1, sigcatch);
if ( a1 == 15 )
{
dword_2598 = 1;
}
else if ( a1 != 17 )
{
if ( a1 == 1 )
dword_259C = 1;
else
dword_25A0 = a1;
}
}

original_code:
void
sigcatch(
    int signo)
{
    sigset(signo, sigcatch);
    switch (signo)
    {
    case 15:
        daemon_vars.terminate_requested = 1;
        break;
    case 17:
        break;
    case 1:
        daemon_vars.catch_SIGHUP = 1;
        break;
    default:
        daemon_vars.catch_unexpected_signo = signo;
        break;
    }
}

canonical_original_code:
void sigcatch(int signo)
{
    sigset(signo, sigcatch);
    switch (signo)
    {
    case 15:
        daemon_vars.terminate_requested = 1;
        break;
    case 17:
        break;
    case 1:
        daemon_vars.catch_SIGHUP = 1;
        break;
    default:
        daemon_vars.catch_unexpected_signo = signo;
        break;
    }
}

variable_types: signo int


decompiled_code:
void __fastcall post_phase_init(int a1)
{
unsigned int v1; // eax

if ( a1 == 1 )
{
v1 = pthread_create(&newthread, xhad_pthread_attr, main_sighandler, 0LL);
if ( v1 )
{
log_message(131076LL, "HA daemon cannot create signal handler thread (sys %d)\n", v1);
newthread = 0LL;
exit_status = 1100;
dword_2598 = 1;
}
else
{
sigset(15, sigcatch);
sigset(17, sigcatch);
sigset(1, sigcatch);
}
}
}

canonical_decompiled_code:
void __fastcall func3(int a1)
{
unsigned int v1; // eax

if ( a1 == 1 )
{
v1 = pthread_create(&newthread, xhad_pthread_attr, main_sighandler, 0LL);
if ( v1 )
{
log_message(131076LL, "HA daemon cannot create signal handler thread (sys %d)\n", v1);
newthread = 0LL;
exit_status = 1100;
dword_2598 = 1;
}
else
{
sigset(15, sigcatch);
sigset(17, sigcatch);
sigset(1, sigcatch);
}
}
}

original_code:
void
post_phase_init(
    int phase)
{
    MTC_S32 ret;
    if (phase == 1)
    {
        ret = pthread_create(&daemon_vars.sighandler_thread, xhad_pthread_attr, main_sighandler, ((void *)0));
        if (ret)
        {
            log_message((4 | (1<<17)), "HA daemon cannot create signal handler thread (sys %d)\n",
                        ret);
            daemon_vars.sighandler_thread = 0;
            exit_status = (1000 + 100 + 0);
            daemon_vars.terminate_requested = 1;
            return;
        }
        sigset(15, sigcatch);
        sigset(17, sigcatch);
        sigset(1, sigcatch);
    }
}

canonical_original_code:
void post_phase_init(int phase)
{
    int ret;
    if (phase == 1)
    {
        ret = pthread_create(&daemon_vars.sighandler_thread, xhad_pthread_attr, main_sighandler, ((void *)0));
        if (ret)
        {
            log_message((4 | (1<<17)), "HA daemon cannot create signal handler thread (sys %d)\n",
                        ret);
            daemon_vars.sighandler_thread = 0;
            exit_status = (1000 + 100 + 0);
            daemon_vars.terminate_requested = 1;
            return;
        }
        sigset(15, sigcatch);
        sigset(17, sigcatch);
        sigset(1, sigcatch);
    }
}

variable_types: phase int


decompiled_code:
void __fastcall __noreturn main_terminate(int a1)
{
dword_2598 = 1;
if ( !exit_status )
exit_status = a1;
while ( 1 )
sleep(0x3Cu);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(int a1)
{
dword_2598 = 1;
if ( !exit_status )
exit_status = a1;
while ( 1 )
sleep(0x3Cu);
}

original_code:
void
main_terminate(
    MTC_STATUS status)
{
    daemon_vars.terminate_requested = 1;
    if (exit_status == 0)
    {
        exit_status = status;
    }
    for (;;)
    {
        sleep(60);
    }
}

canonical_original_code:
void main_terminate(int status)
{
    daemon_vars.terminate_requested = 1;
    if (exit_status == 0)
    {
        exit_status = status;
    }
    for (;;)
    {
        sleep(60);
    }
}


decompiled_code:
__int64 main_save_scheduler()
{
__pid_t v0; // eax
int v1; // eax
int v2; // ebx
__pid_t v3; // eax
__int64 result; // rax
int *v5; // rax
int *v6; // rax
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = 0;
v0 = getpid();
v1 = sched_getscheduler(v0);
if ( v1 < 0 )
{
v5 = _errno_location();
result = log_message(131076LL, "HA daemon get scheduler policy failed (sys %d)\n", (unsigned int)*v5);
}
else
{
v2 = v1;
v3 = getpid();
if ( sched_getparam(v3, &param) )
{
v6 = _errno_location();
result = log_message(131076LL, "HA daemon get scheduler param failed (sys %d)\n", (unsigned int)*v6);
}
else
{
result = (unsigned int)param.__sched_priority;
sched_policy_save = v2;
sparam_save.__sched_priority = param.__sched_priority;
}
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__pid_t v0; // eax
int v1; // eax
int v2; // ebx
__pid_t v3; // eax
__int64 result; // rax
int *v5; // rax
int *v6; // rax
struct sched_param param; // [rsp+Ch] [rbp-Ch] BYREF

param.__sched_priority = 0;
v0 = getpid();
v1 = sched_getscheduler(v0);
if ( v1 < 0 )
{
v5 = _errno_location();
result = log_message(131076LL, "HA daemon get scheduler policy failed (sys %d)\n", (unsigned int)*v5);
}
else
{
v2 = v1;
v3 = getpid();
if ( sched_getparam(v3, &param) )
{
v6 = _errno_location();
result = log_message(131076LL, "HA daemon get scheduler param failed (sys %d)\n", (unsigned int)*v6);
}
else
{
result = (unsigned int)param.__sched_priority;
sched_policy_save = v2;
sparam_save.__sched_priority = param.__sched_priority;
}
}
return result;
}

original_code:
void
main_save_scheduler(void)
{
    struct sched_param sparam = {0};
    int sched_policy;
    int status;
    sched_policy = sched_getscheduler(getpid());
    if (sched_policy < 0)
    {
        log_message((4 | (1<<17)), "HA daemon get scheduler policy failed (sys %d)\n",
                    (*__errno_location ()));
        return;
    }
    status = sched_getparam(getpid(), &sparam);
    if (status != 0)
    {
        log_message((4 | (1<<17)), "HA daemon get scheduler param failed (sys %d)\n",
                    (*__errno_location ()));
        return;
    }
    sched_policy_save = sched_policy;
    sparam_save = sparam;
}

canonical_original_code:
void main_save_scheduler()
{
    struct sched_param sparam = {0};
    int sched_policy;
    int status;
    sched_policy = sched_getscheduler(getpid());
    if (sched_policy < 0)
    {
        log_message((4 | (1<<17)), "HA daemon get scheduler policy failed (sys %d)\n",
                    (*__errno_location ()));
        return;
    }
    status = sched_getparam(getpid(), &sparam);
    if (status != 0)
    {
        log_message((4 | (1<<17)), "HA daemon get scheduler param failed (sys %d)\n",
                    (*__errno_location ()));
        return;
    }
    sched_policy_save = sched_policy;
    sparam_save = sparam;
}

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
int main_reset_scheduler()
{
int v0; // ebp
__pid_t v1; // eax

v0 = sched_policy_save;
v1 = getpid();
return sched_setscheduler(v1, v0, &sparam_save);
}

canonical_decompiled_code:
int func7()
{
int v0; // ebp
__pid_t v1; // eax

v0 = sched_policy_save;
v1 = getpid();
return sched_setscheduler(v1, v0, &sparam_save);
}

original_code:
void
main_reset_scheduler(void)
{
    int status;
    status = sched_setscheduler(getpid() , sched_policy_save, &sparam_save);
}

canonical_original_code:
void main_reset_scheduler()
{
    int status;
    status = sched_setscheduler(getpid() , sched_policy_save, &sparam_save);
}


decompiled_code:
__int64 main_log_timeouts()
{
return log_message(
131078LL,
"CONF: my_index=%d num_host=%d t1=%d T1=%d t2=%d T2=%d Wh=%d Ws=%d Tboot=%d Tenable=%d tXapi=%d TXapi=%d Resta"
"rtXapi=%d TRestartXapi=%d Tlicence=%d\n",
ha_config[985],
ha_config[11],
ha_config[972],
ha_config[973],
ha_config[974],
ha_config[975],
ha_config[976],
ha_config[977],
ha_config[978],
ha_config[979],
ha_config[980],
ha_config[981],
ha_config[982],
ha_config[983],
ha_config[984]);
}

canonical_decompiled_code:
__int64 func8()
{
return log_message(
131078LL,
"CONF: my_index=%d num_host=%d t1=%d T1=%d t2=%d T2=%d Wh=%d Ws=%d Tboot=%d Tenable=%d tXapi=%d TXapi=%d Resta"
"rtXapi=%d TRestartXapi=%d Tlicence=%d\n",
ha_config[985],
ha_config[11],
ha_config[972],
ha_config[973],
ha_config[974],
ha_config[975],
ha_config[976],
ha_config[977],
ha_config[978],
ha_config[979],
ha_config[980],
ha_config[981],
ha_config[982],
ha_config[983],
ha_config[984]);
}

original_code:
void
main_log_timeouts(void)
{
    log_message((6 | (1<<17)), "CONF: my_index=%d num_host=%d t1=%d T1=%d t2=%d T2=%d Wh=%d Ws=%d Tboot=%d Tenable=%d tXapi=%d TXapi=%d RestartXapi=%d TRestartXapi=%d Tlicence=%d\n",
               (ha_config.local.localhost_index), (ha_config.common.hostnum),
               (ha_config.common.heartbeat_interval), (ha_config.common.heartbeat_timeout),
               (ha_config.common.statefile_interval), (ha_config.common.statefile_timeout),
               (ha_config.common.heartbeat_watchdog_timeout), (ha_config.common.statefile_watchdog_timeout),
               (ha_config.common.boot_join_timeout), (ha_config.common.enable_join_timeout), (ha_config.common.xapi_healthcheck_interval), (ha_config.common.xapi_healthcheck_timeout), (ha_config.common.xapi_restart_attempts), (ha_config.common.xapi_restart_timeout), (ha_config.common.xapi_licensecheck_timeout));
}

canonical_original_code:
void main_log_timeouts()
{
    log_message((6 | (1<<17)), "CONF: my_index=%d num_host=%d t1=%d T1=%d t2=%d T2=%d Wh=%d Ws=%d Tboot=%d Tenable=%d tXapi=%d TXapi=%d RestartXapi=%d TRestartXapi=%d Tlicence=%d\n",
               (ha_config.local.localhost_index), (ha_config.common.hostnum),
               (ha_config.common.heartbeat_interval), (ha_config.common.heartbeat_timeout),
               (ha_config.common.statefile_interval), (ha_config.common.statefile_timeout),
               (ha_config.common.heartbeat_watchdog_timeout), (ha_config.common.statefile_watchdog_timeout),
               (ha_config.common.boot_join_timeout), (ha_config.common.enable_join_timeout), (ha_config.common.xapi_healthcheck_interval), (ha_config.common.xapi_healthcheck_timeout), (ha_config.common.xapi_restart_attempts), (ha_config.common.xapi_restart_timeout), (ha_config.common.xapi_licensecheck_timeout));
}


decompiled_code:
int __fastcall main_sleep(__int64 a1)
{
struct timespec v1; // rax
int result; // eax
__m128i i; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = mstots(a1);
remaining = v1;
for ( i = (__m128i)v1; ; i = _mm_loadu_si128((const __m128i *)&remaining) )
{
result = nanosleep((const struct timespec *)&i, &remaining);
if ( !result )
break;
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1)
{
struct timespec v1; // rax
int result; // eax
__m128i i; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = func0(a1);
remaining = v1;
for ( i = (__m128i)v1; ; i = _mm_loadu_si128((const __m128i *)&remaining) )
{
result = nanosleep((const struct timespec *)&i, &remaining);
if ( !result )
break;
}
return result;
}

original_code:
void
main_sleep(
    MTC_CLOCK msec)
{
    struct timespec ts, ts_rem;
    ts = ts_rem = mstots(msec);
    while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
}

canonical_original_code:
void main_sleep(long long msec)
{
    struct timespec ts; struct timespec ts_rem;
    ts = ts_rem = mstots(msec);
    while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
}


xhadlog

decompiled_code:
int xhadlog(int a1, const char *a2, ...)
{
int v2; // eax
unsigned __int64 v3; // rdx
int v4; // edi
int v5; // ebx
char *v6; // r12
size_t v7; // rax
FILE *v8; // rdi
time_t v10; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v2 = *((_DWORD *)&prioritynames + 2);
if ( v2 == -1 )
{
v5 = 0;
}
else
{
v3 = (unsigned int)((_DWORD)&prioritynames + 24);
v4 = a1 & 7;
v5 = 0;
do
{
if ( v4 == v2 )
break;
v2 = *(_DWORD *)v3;
v3 += 16LL;
++v5;
}
while ( v2 != -1 );
}
v10 = time(0LL);
v6 = ctime(&v10);
v7 = strlen(v6);
v8 = fpLogfile;
v6[v7 - 1] = 0;
fprintf(v8, "%s [%s] ", v6, (&prioritynames)[2 * v5]);
va_start(arg, a2);
vfprintf(fpLogfile, a2, arg);
fflush(fpLogfile);
}
return pthread_spin_unlock(&lock);
}

canonical_decompiled_code:
int func3(int a1, const char *a2, ...)
{
int v2; // eax
unsigned __int64 v3; // rdx
int v4; // edi
int v5; // ebx
char *v6; // r12
size_t v7; // rax
FILE *v8; // rdi
time_t v10; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v2 = *((_DWORD *)&prioritynames + 2);
if ( v2 == -1 )
{
v5 = 0;
}
else
{
v3 = (unsigned int)((_DWORD)&prioritynames + 24);
v4 = a1 & 7;
v5 = 0;
do
{
if ( v4 == v2 )
break;
v2 = *(_DWORD *)v3;
v3 += 16LL;
++v5;
}
while ( v2 != -1 );
}
v10 = time(0LL);
v6 = ctime(&v10);
v7 = strlen(v6);
v8 = fpLogfile;
v6[v7 - 1] = 0;
fprintf(v8, "%s [%s] ", v6, (&prioritynames)[2 * v5]);
va_start(arg, a2);
vfprintf(fpLogfile, a2, arg);
fflush(fpLogfile);
}
return pthread_spin_unlock(&lock);
}

original_code:
void
xhadlog(MTC_S32 priority, PMTC_S8 fmt, ...)
{
    time_t now;
    MTC_S8 *pTString;
    va_list ap;
    pthread_spin_lock(&lock);
    if (priority & (1<<17))
    {
        MTC_S32 i = 0;
        while (prioritynames[i].c_val != -1 &&
               prioritynames[i].c_val != ((priority) & 0x07))
        {
     i++;
        }
        now = time(((void *)0));
        pTString = ctime(&now);
        pTString[strlen(pTString) - 1] = '\0';
        fprintf(fpLogfile,
         "%s [%s] ", pTString, prioritynames[i].c_name);
        __builtin_va_start(ap,fmt);
        vfprintf(fpLogfile, fmt, ap);
        __builtin_va_end(ap);
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}

canonical_original_code:
void xhadlog(int priority, char *fmt, ...)
{
    long now;
    char *pTString;
    __builtin_va_list ap;
    pthread_spin_lock(&lock);
    if (priority & (1<<17))
    {
        int i = 0;
        while (prioritynames[i].c_val != -1 &&
               prioritynames[i].c_val != ((priority) & 0x07))
        {
     i++;
        }
        now = time(((void *)0));
        pTString = ctime(&now);
        pTString[strlen(pTString) - 1] = '\0';
        fprintf(fpLogfile,
         "%s [%s] ", pTString, prioritynames[i].c_name);
        __builtin_va_start(ap,fmt);
        vfprintf(fpLogfile, fmt, ap);
        __builtin_va_end(ap);
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}

variable_types: priority int

variable_types:
fmt
char *

variable_types: now long

variable_types:
pTString
char *

variable_types: i int


decompiled_code:
int __fastcall xhadlog_bin8(int a1, __int64 a2, int a3)
{
int v3; // er14
int v4; // er12
__int64 v5; // r13
__int64 v6; // rbx
int v7; // er15
char v8; // r12
bool v9; // dl
bool v10; // al
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
char v14[88]; // [rsp+10h] [rbp-58h] BYREF

v3 = a3;
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
if ( v3 > 0 )
{
v13 = 0;
v4 = 0;
do
{
fprintf(fpLogfile, "\t%04x: ", v13);
if ( a3 <= v4 )
{
v14[0] = 0;
v7 = 0;
}
else
{
v5 = 1LL;
v6 = v4 + 1;
do
{
v7 = v5;
fprintf(fpLogfile, "%02x ", (unsigned int)*(char *)(a2 + v6 - 1));
v8 = *(_BYTE *)(a2 + v6 - 1);
if ( !isprint(v8) )
v8 = 46;
v9 = (int)v5 <= 15;
v10 = a3 > (int)v6;
*((_BYTE *)&v13 + v5 + 3) = v8;
v4 = v6;
++v5;
++v6;
}
while ( v10 && v9 );
v14[v7] = 0;
if ( v7 == 16 )
goto LABEL_11;
}
do
{
++v7;
fwrite(" ", 1uLL, 3uLL, fpLogfile);
}
while ( v7 != 16 );
LABEL_11:
fprintf(fpLogfile, ": %s\n", v14);
++v13;
}
while ( a3 > v4 );
}
fflush(fpLogfile);
}
return pthread_spin_unlock(&lock);
}

canonical_decompiled_code:
int __fastcall func4(int a1, __int64 a2, int a3)
{
int v3; // er14
int v4; // er12
__int64 v5; // r13
__int64 v6; // rbx
int v7; // er15
char v8; // r12
bool v9; // dl
bool v10; // al
unsigned int v13; // [rsp+Ch] [rbp-5Ch]
char v14[88]; // [rsp+10h] [rbp-58h] BYREF

v3 = a3;
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
if ( v3 > 0 )
{
v13 = 0;
v4 = 0;
do
{
fprintf(fpLogfile, "\t%04x: ", v13);
if ( a3 <= v4 )
{
v14[0] = 0;
v7 = 0;
}
else
{
v5 = 1LL;
v6 = v4 + 1;
do
{
v7 = v5;
fprintf(fpLogfile, "%02x ", (unsigned int)*(char *)(a2 + v6 - 1));
v8 = *(_BYTE *)(a2 + v6 - 1);
if ( !isprint(v8) )
v8 = 46;
v9 = (int)v5 <= 15;
v10 = a3 > (int)v6;
*((_BYTE *)&v13 + v5 + 3) = v8;
v4 = v6;
++v5;
++v6;
}
while ( v10 && v9 );
v14[v7] = 0;
if ( v7 == 16 )
goto LABEL_11;
}
do
{
++v7;
fwrite(" ", 1uLL, 3uLL, fpLogfile);
}
while ( v7 != 16 );
LABEL_11:
fprintf(fpLogfile, ": %s\n", v14);
++v13;
}
while ( a3 > v4 );
}
fflush(fpLogfile);
}
return pthread_spin_unlock(&lock);
}

original_code:
void
xhadlog_bin8(MTC_S32 priority, PMTC_S8 data, MTC_S32 size)
{
    MTC_S32 line, pos, col;
    MTC_S8 asc_dmp[16 + 1];
    pthread_spin_lock(&lock);
    if (priority & (1<<17))
    {
        for (line = 0, pos = 0; pos < size; line++)
        {
            fprintf(fpLogfile, "\t%04x: ", line);
            for (col = 0; col < 16 && pos < size; col++, pos++)
            {
                fprintf(fpLogfile, "%02x ", data[pos]);
                asc_dmp[col] = (isprint(data[pos]))? data[pos]: '.';
            }
            asc_dmp[col] = '\0';
            for (; col < 16; col++)
            {
                fprintf(fpLogfile, "   ");
            }
            fprintf(fpLogfile, ": %s\n", asc_dmp);
        }
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}

canonical_original_code:
void xhadlog_bin8(int priority, char *data, int size)
{
    int line; int pos; int col;
    char asc_dmp[-1];
    pthread_spin_lock(&lock);
    if (priority & (1<<17))
    {
        for (line = 0, pos = 0; pos < size; line++)
        {
            fprintf(fpLogfile, "\t%04x: ", line);
            for (col = 0; col < 16 && pos < size; col++, pos++)
            {
                fprintf(fpLogfile, "%02x ", data[pos]);
                asc_dmp[col] = (isprint(data[pos]))? data[pos]: '.';
            }
            asc_dmp[col] = '\0';
            for (; col < 16; col++)
            {
                fprintf(fpLogfile, "   ");
            }
            fprintf(fpLogfile, ": %s\n", asc_dmp);
        }
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}

variable_types:
data
char *

variable_types: size int

variable_types: line int

variable_types: pos int

variable_types: col int


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

fpLogfile = stderr;
result = pthread_spin_init(&lock, 0);
if ( result )
{
syslog(3, "can't initialize spin lock");
result = -*_errno_location();
}
return result;
}

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

fpLogfile = stderr;
result = pthread_spin_init(&lock, 0);
if ( result )
{
syslog(3, "can't initialize spin lock");
result = -*_errno_location();
}
return result;
}

original_code:
MTC_S32
log_initialize()
{
    MTC_S32 ret;
    fpLogfile = stderr;
    ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
    if (ret)
    {
        syslog(3, "can't initialize spin lock");
        return -(*__errno_location ());
    }
    return 0;
}

canonical_original_code:
int log_initialize()
{
    int ret;
    fpLogfile = stderr;
    ret = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
    if (ret)
    {
        syslog(3, "can't initialize spin lock");
        return -(*__errno_location ());
    }
    return 0;
}


decompiled_code:
void log_reopen()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void
log_reopen()
{
}

canonical_original_code:
void log_reopen()
{
}


decompiled_code:
int log_terminate()
{
return pthread_spin_destroy(&lock);
}

canonical_decompiled_code:
int func2()
{
return pthread_spin_destroy(&lock);
}

original_code:
void
log_terminate()
{
    pthread_spin_destroy(&lock);
}


decompiled_code:
void log_message(int a1, const char *a2, ...)
{
FILE *v2; // r13
int v3; // eax
unsigned __int64 v4; // rdx
int v5; // er12
struct tm *v6; // rax
time_t v7; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

if ( initialized )
{
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v2 = fpLogfile;
if ( fpLogfile )
{
if ( privatelogflag )
{
v3 = *((_DWORD *)&prioritynames + 2);
if ( v3 == -1 )
{
v5 = 0;
}
else
{
v4 = (unsigned int)((_DWORD)&prioritynames + 24);
v5 = 0;
do
{
if ( (a1 & 7) == v3 )
break;
v3 = *(_DWORD *)v4;
v4 += 16LL;
++v5;
}
while ( v3 != -1 );
}
v7 = time(0LL);
v6 = localtime(&v7);
fprintf(
v2,
"%s %02d %02d:%02d:%02d %s %04d [%s] ",
monthnames[v6->tm_mon],
(unsigned int)v6->tm_mday,
(unsigned int)v6->tm_hour,
(unsigned int)v6->tm_min,
(unsigned int)v6->tm_sec,
v6->tm_zone,
(unsigned int)(v6->tm_year + 1900),
(&prioritynames)[2 * v5]);
va_start(arg, a2);
vfprintf(fpLogfile, a2, arg);
fflush(fpLogfile);
}
}
}
if ( (a1 & 0x10000) != 0 )
{
va_end(arg);
va_start(arg, a2);
vsyslog(a1 & 7, a2, arg);
}
pthread_spin_unlock(&lock);
}
}

canonical_decompiled_code:
void func3(int a1, const char *a2, ...)
{
FILE *v2; // r13
int v3; // eax
unsigned __int64 v4; // rdx
int v5; // er12
struct tm *v6; // rax
time_t v7; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

if ( initialized )
{
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v2 = fpLogfile;
if ( fpLogfile )
{
if ( privatelogflag )
{
v3 = *((_DWORD *)&prioritynames + 2);
if ( v3 == -1 )
{
v5 = 0;
}
else
{
v4 = (unsigned int)((_DWORD)&prioritynames + 24);
v5 = 0;
do
{
if ( (a1 & 7) == v3 )
break;
v3 = *(_DWORD *)v4;
v4 += 16LL;
++v5;
}
while ( v3 != -1 );
}
v7 = time(0LL);
v6 = localtime(&v7);
fprintf(
v2,
"%s %02d %02d:%02d:%02d %s %04d [%s] ",
monthnames[v6->tm_mon],
(unsigned int)v6->tm_mday,
(unsigned int)v6->tm_hour,
(unsigned int)v6->tm_min,
(unsigned int)v6->tm_sec,
v6->tm_zone,
(unsigned int)(v6->tm_year + 1900),
(&prioritynames)[2 * v5]);
va_start(arg, a2);
vfprintf(fpLogfile, a2, arg);
fflush(fpLogfile);
}
}
}
if ( (a1 & 0x10000) != 0 )
{
va_end(arg);
va_start(arg, a2);
vsyslog(a1 & 7, a2, arg);
}
pthread_spin_unlock(&lock);
}
}

original_code:
void log_message(MTC_S32 priority, PMTC_S8 fmt, ...)
{
    time_t now;
    struct tm *ptm;
    va_list ap;
    if (!initialized)
    {
        return;
    }
    pthread_spin_lock(&lock);
    if (priority & (1<<17) && fpLogfile != ((void *)0) && privatelogflag)
    {
        MTC_S32 i = 0;
        while (prioritynames[i].c_val != -1 &&
               prioritynames[i].c_val != ((priority) & 0x07))
        {
            i++;
        }
        now = time(((void *)0));
        ptm = localtime(&now);
        fprintf(fpLogfile, "%s %02d %02d:%02d:%02d %s %04d [%s] ",
                monthnames[ptm->tm_mon], ptm->tm_mday,
                ptm->tm_hour, ptm->tm_min, ptm->tm_sec,
                ptm->tm_zone, 1900 + ptm->tm_year, prioritynames[i].c_name);
        __builtin_va_start(ap,fmt);
        vfprintf(fpLogfile, fmt, ap);
        __builtin_va_end(ap);
        fflush(fpLogfile);
    }
    if (priority & (1<<16))
    {
        __builtin_va_start(ap,fmt);
        vsyslog(((priority) & 0x07), fmt, ap);
        __builtin_va_end(ap);
    }
    pthread_spin_unlock(&lock);
}

canonical_original_code:
void log_message(int priority, char *fmt, ...)
{
    long now;
    struct tm *ptm;
    __builtin_va_list ap;
    if (!initialized)
    {
        return;
    }
    pthread_spin_lock(&lock);
    if (priority & (1<<17) && fpLogfile != ((void *)0) && privatelogflag)
    {
        int i = 0;
        while (prioritynames[i].c_val != -1 &&
               prioritynames[i].c_val != ((priority) & 0x07))
        {
            i++;
        }
        now = time(((void *)0));
        ptm = localtime(&now);
        fprintf(fpLogfile, "%s %02d %02d:%02d:%02d %s %04d [%s] ",
                monthnames[ptm->tm_mon], ptm->tm_mday,
                ptm->tm_hour, ptm->tm_min, ptm->tm_sec,
                ptm->tm_zone, 1900 + ptm->tm_year, prioritynames[i].c_name);
        __builtin_va_start(ap,fmt);
        vfprintf(fpLogfile, fmt, ap);
        __builtin_va_end(ap);
        fflush(fpLogfile);
    }
    if (priority & (1<<16))
    {
        __builtin_va_start(ap,fmt);
        vsyslog(((priority) & 0x07), fmt, ap);
        __builtin_va_end(ap);
    }
    pthread_spin_unlock(&lock);
}

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; } *


decompiled_code:
void __fastcall log_bin(int a1, __int64 a2, int a3)
{
FILE *v4; // rdi
int v5; // er13
__int64 v6; // r14
const unsigned __int16 **v7; // r12
__int64 v8; // rbx
int v9; // er15
char v10; // al
bool v11; // dl
bool v12; // al
int v13; // [rsp+8h] [rbp-60h]
unsigned int v14; // [rsp+Ch] [rbp-5Ch]
char v15[88]; // [rsp+10h] [rbp-58h] BYREF

v13 = a3;
if ( initialized )
{
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v4 = fpLogfile;
if ( fpLogfile )
{
if ( privatelogflag )
{
if ( a3 > 0 )
{
v14 = 0;
v5 = 0;
do
{
fprintf(v4, "\t%04x: ", v14);
if ( v13 <= v5 )
{
v15[0] = 0;
v9 = 0;
}
else
{
v6 = 1LL;
v7 = _ctype_b_loc();
v8 = v5 + 1;
do
{
v9 = v6;
v5 = v8;
fprintf(fpLogfile, "%02x ", *(unsigned __int8 *)(a2 + v8 - 1));
v10 = *(_BYTE *)(a2 + v8 - 1);
if ( ((*v7)[v10] & 0x4000) == 0 )
v10 = 46;
v11 = (int)v6 <= 15;
*((_BYTE *)&v14 + v6 + 3) = v10;
v12 = v13 > (int)v8;
++v6;
++v8;
}
while ( v12 && v11 );
v15[v9] = 0;
if ( v9 == 16 )
goto LABEL_14;
}
do
{
++v9;
fwrite(" ", 1uLL, 3uLL, fpLogfile);
}
while ( v9 != 16 );
LABEL_14:
fprintf(fpLogfile, ": %s\n", v15);
++v14;
v4 = fpLogfile;
}
while ( v13 > v5 );
}
fflush(v4);
}
}
}
pthread_spin_unlock(&lock);
}
}

canonical_decompiled_code:
void __fastcall func4(int a1, __int64 a2, int a3)
{
FILE *v4; // rdi
int v5; // er13
__int64 v6; // r14
const unsigned __int16 **v7; // r12
__int64 v8; // rbx
int v9; // er15
char v10; // al
bool v11; // dl
bool v12; // al
int v13; // [rsp+8h] [rbp-60h]
unsigned int v14; // [rsp+Ch] [rbp-5Ch]
char v15[88]; // [rsp+10h] [rbp-58h] BYREF

v13 = a3;
if ( initialized )
{
pthread_spin_lock(&lock);
if ( (a1 & 0x20000) != 0 )
{
v4 = fpLogfile;
if ( fpLogfile )
{
if ( privatelogflag )
{
if ( a3 > 0 )
{
v14 = 0;
v5 = 0;
do
{
fprintf(v4, "\t%04x: ", v14);
if ( v13 <= v5 )
{
v15[0] = 0;
v9 = 0;
}
else
{
v6 = 1LL;
v7 = _ctype_b_loc();
v8 = v5 + 1;
do
{
v9 = v6;
v5 = v8;
fprintf(fpLogfile, "%02x ", *(unsigned __int8 *)(a2 + v8 - 1));
v10 = *(_BYTE *)(a2 + v8 - 1);
if ( ((*v7)[v10] & 0x4000) == 0 )
v10 = 46;
v11 = (int)v6 <= 15;
*((_BYTE *)&v14 + v6 + 3) = v10;
v12 = v13 > (int)v8;
++v6;
++v8;
}
while ( v12 && v11 );
v15[v9] = 0;
if ( v9 == 16 )
goto LABEL_14;
}
do
{
++v9;
fwrite(" ", 1uLL, 3uLL, fpLogfile);
}
while ( v9 != 16 );
LABEL_14:
fprintf(fpLogfile, ": %s\n", v15);
++v14;
v4 = fpLogfile;
}
while ( v13 > v5 );
}
fflush(v4);
}
}
}
pthread_spin_unlock(&lock);
}
}

original_code:
void log_bin(MTC_S32 priority, PMTC_S8 data, MTC_S32 size)
{
    MTC_S32 line, pos, col;
    MTC_S8 asc_dmp[16 + 1];
    if (!initialized)
    {
        return;
    }
    pthread_spin_lock(&lock);
    if (priority & (1<<17) && fpLogfile != ((void *)0) && privatelogflag)
    {
        for (line = 0, pos = 0; pos < size; line++)
        {
            fprintf(fpLogfile, "\t%04x: ", line);
            for (col = 0; col < 16 && pos < size; col++, pos++)
            {
                fprintf(fpLogfile, "%02x ", (MTC_U8) data[pos]);
                asc_dmp[col] = (((*__ctype_b_loc ())[(int) ((data[pos]))] & (unsigned short int) _ISprint))? data[pos]: '.';
            }
            asc_dmp[col] = '\0';
            for (; col < 16; col++)
            {
                fprintf(fpLogfile, "   ");
            }
            fprintf(fpLogfile, ": %s\n", asc_dmp);
        }
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}

canonical_original_code:
void log_bin(int priority, char *data, int size)
{
    int line; int pos; int col;
    char asc_dmp[-1];
    if (!initialized)
    {
        return;
    }
    pthread_spin_lock(&lock);
    if (priority & (1<<17) && fpLogfile != ((void *)0) && privatelogflag)
    {
        for (line = 0, pos = 0; pos < size; line++)
        {
            fprintf(fpLogfile, "\t%04x: ", line);
            for (col = 0; col < 16 && pos < size; col++, pos++)
            {
                fprintf(fpLogfile, "%02x ", (unsigned char) data[pos]);
                asc_dmp[col] = (((*__ctype_b_loc ())[(int) ((data[pos]))] & (unsigned short) _ISprint))? data[pos]: '.';
            }
            asc_dmp[col] = '\0';
            for (; col < 16; col++)
            {
                fprintf(fpLogfile, "   ");
            }
            fprintf(fpLogfile, ": %s\n", asc_dmp);
        }
        fflush(fpLogfile);
    }
    pthread_spin_unlock(&lock);
}


original_code:
void
log_logmask(void)
{
    MTC_U32 logmask_index;
    char *logmask_name[(32 - 18)] = {"DUMPPACKET", "TRACE", "FH_TRACE", "LM_TRACE", "SCRIPT", "SC_WARNING", ((void *)0)};
    log_message((6 | (1<<17)), "LOG: logmask = %x\n", logmask);
    for (logmask_index = 0; logmask_index < (32 - 18); logmask_index++)
    {
        MTC_S32 logmask_on;
        logmask_on = (1 << (logmask_index + 18)) & logmask;
        if (logmask_name[logmask_index] == ((void *)0) && logmask_on == 0)
        {
            continue;
        }
        log_message((6 | (1<<17)), "LOG:  %s:%2.2d(%s)\n",
                    (logmask_on == 0)?"OFF":"ON ",
                    logmask_index + 18,
                    (logmask_name[logmask_index] != ((void *)0))?logmask_name[logmask_index]:"UNKNOWN_MASK");
    }
    return;
}

canonical_original_code:
void log_logmask()
{
    unsigned int logmask_index;
    char *logmask_name[-1] = {"DUMPPACKET", "TRACE", "FH_TRACE", "LM_TRACE", "SCRIPT", "SC_WARNING", ((void *)0)};
    log_message((6 | (1<<17)), "LOG: logmask = %x\n", logmask);
    for (logmask_index = 0; logmask_index < (32 - 18); logmask_index++)
    {
        int logmask_on;
        logmask_on = (1 << (logmask_index + 18)) & logmask;
        if (logmask_name[logmask_index] == ((void *)0) && logmask_on == 0)
        {
            continue;
        }
        log_message((6 | (1<<17)), "LOG:  %s:%2.2d(%s)\n",
                    (logmask_on == 0)?"OFF":"ON ",
                    logmask_index + 18,
                    (logmask_name[logmask_index] != ((void *)0))?logmask_name[logmask_index]:"UNKNOWN_MASK");
    }
    return;
}


original_code:
void
log_backtrace(MTC_S32 priority)
{
    int level;
    Dl_info dli;
    void *return_address[10] = {((void *)0)};
    level = 0;
    if (__builtin_frame_address(0) == 0) { return_address[0] = ((void *)0); goto end_fill; } return_address[0] = __builtin_return_address(0);;
    if (__builtin_frame_address(1) == 0) { return_address[1] = ((void *)0); goto end_fill; } return_address[1] = __builtin_return_address(1);;
    if (__builtin_frame_address(2) == 0) { return_address[2] = ((void *)0); goto end_fill; } return_address[2] = __builtin_return_address(2);;
    if (__builtin_frame_address(3) == 0) { return_address[3] = ((void *)0); goto end_fill; } return_address[3] = __builtin_return_address(3);;
    if (__builtin_frame_address(4) == 0) { return_address[4] = ((void *)0); goto end_fill; } return_address[4] = __builtin_return_address(4);;
    if (__builtin_frame_address(5) == 0) { return_address[5] = ((void *)0); goto end_fill; } return_address[5] = __builtin_return_address(5);;
    if (__builtin_frame_address(6) == 0) { return_address[6] = ((void *)0); goto end_fill; } return_address[6] = __builtin_return_address(6);;
    if (__builtin_frame_address(7) == 0) { return_address[7] = ((void *)0); goto end_fill; } return_address[7] = __builtin_return_address(7);;
    if (__builtin_frame_address(8) == 0) { return_address[8] = ((void *)0); goto end_fill; } return_address[8] = __builtin_return_address(8);;
    if (__builtin_frame_address(9) == 0) { return_address[9] = ((void *)0); goto end_fill; } return_address[9] = __builtin_return_address(9);;
 end_fill:
    log_message(priority, "backtrace -------------\n");
    for (level = 0;
         level < 10;
         level ++)
    {
        int err;
        if (return_address[level] == ((void *)0))
        {
            break;
        }
        err = dladdr(return_address[level], &dli);
        if (err == 0 || dli.dli_sname == ((void *)0))
        {
            log_message(priority, "  %2d: (%p) --\n", level, return_address[level]);
        }
        else
        {
            log_message(priority, "  %2d: (%p) %s\n", level, return_address[level], dli.dli_sname);
        }
    }
    log_message(priority, "backtrace -------------\n");
    return;
}

canonical_original_code:
void log_backtrace(int priority)
{
    int level;
    struct Dl_info dli;
    void *return_address[10] = {((void *)0)};
    level = 0;
    if (__builtin_frame_address(0) == 0) { return_address[0] = ((void *)0); goto end_fill; } return_address[0] = __builtin_return_address(0);;
    if (__builtin_frame_address(1) == 0) { return_address[1] = ((void *)0); goto end_fill; } return_address[1] = __builtin_return_address(1);;
    if (__builtin_frame_address(2) == 0) { return_address[2] = ((void *)0); goto end_fill; } return_address[2] = __builtin_return_address(2);;
    if (__builtin_frame_address(3) == 0) { return_address[3] = ((void *)0); goto end_fill; } return_address[3] = __builtin_return_address(3);;
    if (__builtin_frame_address(4) == 0) { return_address[4] = ((void *)0); goto end_fill; } return_address[4] = __builtin_return_address(4);;
    if (__builtin_frame_address(5) == 0) { return_address[5] = ((void *)0); goto end_fill; } return_address[5] = __builtin_return_address(5);;
    if (__builtin_frame_address(6) == 0) { return_address[6] = ((void *)0); goto end_fill; } return_address[6] = __builtin_return_address(6);;
    if (__builtin_frame_address(7) == 0) { return_address[7] = ((void *)0); goto end_fill; } return_address[7] = __builtin_return_address(7);;
    if (__builtin_frame_address(8) == 0) { return_address[8] = ((void *)0); goto end_fill; } return_address[8] = __builtin_return_address(8);;
    if (__builtin_frame_address(9) == 0) { return_address[9] = ((void *)0); goto end_fill; } return_address[9] = __builtin_return_address(9);;
 end_fill:
    log_message(priority, "backtrace -------------\n");
    for (level = 0;
         level < 10;
         level ++)
    {
        int err;
        if (return_address[level] == ((void *)0))
        {
            break;
        }
        err = dladdr(return_address[level], &dli);
        if (err == 0 || dli.dli_sname == ((void *)0))
        {
            log_message(priority, "  %2d: (%p) --\n", level, return_address[level]);
        }
        else
        {
            log_message(priority, "  %2d: (%p) %s\n", level, return_address[level], dli.dli_sname);
        }
    }
    log_message(priority, "backtrace -------------\n");
    return;
}

variable_types: level int

variable_types: struct Dl_info { char *dli_fname; void *dli_fbase; char *dli_sname; void *dli_saddr; }

variable_types: err int


decompiled_code:
void __fastcall log_status(unsigned int a1, const char *a2)
{
const char *v2; // rdx

v2 = (const char *)status_to_message();
if ( a2 && *a2 )
log_message(196611, "%s (%d) %s.\n", v2, a1, a2);
else
log_message(196611, "%s (%d).\n", v2, a1);
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1, const char *a2)
{
const char *v2; // rdx

v2 = (const char *)status_to_message();
if ( a2 && *a2 )
func3(196611, "%s (%d) %s.\n", v2, a1, a2);
else
func3(196611, "%s (%d).\n", v2, a1);
}

original_code:
void
log_status(
    MTC_STATUS status,
    char *suffix)
{
    char *message;
    message = status_to_message(status);
    if (suffix == ((void *)0) || strlen(suffix) == 0)
    {
        log_message((3 | (1<<16) | (1<<17)), "%s (%d).\n", message, status);
    }
    else
    {
        log_message((3 | (1<<16) | (1<<17)), "%s (%d) %s.\n", message, status, suffix);
    }
}

canonical_original_code:
void log_status(int status, char *suffix)
{
    char *message;
    message = status_to_message(status);
    if (suffix == ((void *)0) || strlen(suffix) == 0)
    {
        log_message((3 | (1<<16) | (1<<17)), "%s (%d).\n", message, status);
    }
    else
    {
        log_message((3 | (1<<16) | (1<<17)), "%s (%d) %s.\n", message, status, suffix);
    }
}

variable_types: status int

variable_types:
suffix
char *

variable_types:
message
char *


decompiled_code:
void log_fsync()
{
int v0; // eax

if ( initialized )
{
pthread_spin_lock(&lock);
if ( fpLogfile )
{
v0 = fileno(fpLogfile);
fsync(v0);
}
pthread_spin_unlock(&lock);
}
}

canonical_decompiled_code:
void func8()
{
int v0; // eax

if ( initialized )
{
pthread_spin_lock(&lock);
if ( fpLogfile )
{
v0 = fileno(fpLogfile);
fsync(v0);
}
pthread_spin_unlock(&lock);
}
}

original_code:
void
log_fsync()
{
    if (!initialized)
    {
        return;
    }
    pthread_spin_lock(&lock);
    if (fpLogfile)
    {
        fsync(fileno(fpLogfile));
    }
    pthread_spin_unlock(&lock);
}


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

result = 0LL;
memset(weight_table, 0, sizeof(weight_table));
return result;
}

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

result = 0LL;
memset(weight_table, 0, sizeof(weight_table));
return result;
}

original_code:
MTC_STATUS
hostweight_initialize0(void)
{
    memset(weight_table, 0, sizeof(weight_table));
    return 0;
}

canonical_original_code:
int hostweight_initialize0()
{
    memset(weight_table, 0, sizeof(weight_table));
    return 0;
}


decompiled_code:
__int64 log_hostweight()
{
char *v0; // rbx
__int64 result; // rax

v0 = weight_table;
result = log_message(131078LL, "SC: builtinclass=%s weight=%d\n", "native", 1LL);
do
{
if ( *v0 )
result = log_message(131078LL, "SC: class=%s weight=%d\n", v0, *((unsigned int *)v0 + 17));
v0 += 72;
}
while ( v0 != &error_reported );
return result;
}

canonical_decompiled_code:
__int64 func1()
{
char *v0; // rbx
__int64 result; // rax

v0 = weight_table;
result = log_message(131078LL, "SC: builtinclass=%s weight=%d\n", "native", 1LL);
do
{
if ( *v0 )
result = log_message(131078LL, "SC: class=%s weight=%d\n", v0, *((unsigned int *)v0 + 17));
v0 += 72;
}
while ( v0 != &error_reported );
return result;
}

original_code:
void
log_hostweight(void)
{
    int index;
    log_message((6 | (1<<17)), "SC:     builtinclass=%s weight=%d\n",
                "native",
                1);
    for (index = 0; index < 16; index++)
    {
        if (!strcmp(weight_table[index].classname, "")) continue;
        log_message((6 | (1<<17)), "SC:     class=%s weight=%d\n",
                    weight_table[index].classname,
                    weight_table[index].weight);
    }
    return;
}

canonical_original_code:
void log_hostweight()
{
    int index;
    log_message((6 | (1<<17)), "SC:     builtinclass=%s weight=%d\n",
                "native",
                1);
    for (index = 0; index < 16; index++)
    {
        if (!strcmp(weight_table[index].classname, "")) continue;
        log_message((6 | (1<<17)), "SC:     class=%s weight=%d\n",
                    weight_table[index].classname,
                    weight_table[index].weight);
    }
    return;
}

variable_types: index int


decompiled_code:
__int64 calc_hostweight()
{
char *v0; // rax
unsigned int v1; // er8

v0 = weight_table;
v1 = 1;
do
{
if ( *v0 )
v1 += *((_DWORD *)v0 + 17);
v0 += 72;
}
while ( v0 != &error_reported );
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
char *v0; // rax
unsigned int v1; // er8

v0 = weight_table;
v1 = 1;
do
{
if ( *v0 )
v1 += *((_DWORD *)v0 + 17);
v0 += 72;
}
while ( v0 != &error_reported );
return v1;
}

original_code:
MTC_U32
calc_hostweight(void)
{
    int index;
    MTC_U32 weight;
    weight = 1;
    for (index = 0; index < 16; index++)
    {
        if (!strcmp(weight_table[index].classname, "")) continue;
        weight += weight_table[index].weight;
    }
    return weight;
}

canonical_original_code:
unsigned int calc_hostweight()
{
    int index;
    unsigned int weight;
    weight = 1;
    for (index = 0; index < 16; index++)
    {
        if (!strcmp(weight_table[index].classname, "")) continue;
        weight += weight_table[index].weight;
    }
    return weight;
}

variable_types:
weight
unsigned int

return_type: unsigned int


decompiled_code:
__int64 hostweight_reload()
{
unsigned int v0; // eax
unsigned int v1; // er12
unsigned int v3; // eax
unsigned int v4; // er12
int fd; // [rsp+8h] [rbp-490h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-48Ch] BYREF
char s1[1160]; // [rsp+10h] [rbp-488h] BYREF

v0 = open_hostweight_file(&fd, &v6);
if ( !v0 )
{
v3 = read_hostweight_file((unsigned int)fd, &v6, s1, 16LL);
v4 = v3;
if ( v3 )
{
if ( !error_reported )
{
log_message(196611LL, "open hostweight file error. %d (sys %d)\n", v3, v6);
error_reported = 1;
}
}
else
{
error_reported = 0;
v4 = hostweight_update(weight_table, s1, 16);
if ( !v4 )
{
hostweight_set_sm();
close(fd);
return v4;
}
}
close(fd);
return v4;
}
v1 = v0;
if ( !error_reported )
{
log_message(196611LL, "open hostweight file error. %d (sys %d)\n", v0, v6);
error_reported = 1;
}
return v1;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int v0; // eax
unsigned int v1; // er12
unsigned int v3; // eax
unsigned int v4; // er12
int fd; // [rsp+8h] [rbp-490h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-48Ch] BYREF
char s1[1160]; // [rsp+10h] [rbp-488h] BYREF

v0 = open_hostweight_file(&fd, &v6);
if ( !v0 )
{
v3 = read_hostweight_file((unsigned int)fd, &v6, s1, 16LL);
v4 = v3;
if ( v3 )
{
if ( !error_reported )
{
log_message(196611LL, "open hostweight file error. %d (sys %d)\n", v3, v6);
error_reported = 1;
}
}
else
{
error_reported = 0;
v4 = func4(weight_table, s1, 16);
if ( !v4 )
{
func5();
close(fd);
return v4;
}
}
close(fd);
return v4;
}
v1 = v0;
if ( !error_reported )
{
log_message(196611LL, "open hostweight file error. %d (sys %d)\n", v0, v6);
error_reported = 1;
}
return v1;
}

original_code:
MTC_STATUS
hostweight_reload(void)
{
    HOST_WEIGHT_TABLE newwtable[16];
    int fd;
    int err_no;
    MTC_STATUS rs = 0;
    if ((rs = open_hostweight_file(&fd, &err_no)) != 0)
    {
        if (!error_reported)
        {
            log_message((3 | (1<<16) | (1<<17)), "open hostweight file error. %d (sys %d)\n",
                        rs, err_no);
            error_reported = 1;
        }
        return rs;
    }
    rs = read_hostweight_file(fd, &err_no, newwtable, 16);
    if (rs != 0)
    {
        if (!error_reported)
        {
            log_message((3 | (1<<16) | (1<<17)), "open hostweight file error. %d (sys %d)\n",
                        rs, err_no);
            error_reported = 1;
        }
        goto error_return;
    }
    error_reported = 0;
    rs = hostweight_update(weight_table, newwtable,
                           16);
    if (rs != 0)
    {
        goto error_return;
    }
    hostweight_set_sm();
 error_return:
    close(fd);
    return rs;
}

canonical_original_code:
int hostweight_reload()
{
    struct host_weight_table newwtable[16];
    int fd;
    int err_no;
    int rs = 0;
    if ((rs = open_hostweight_file(&fd, &err_no)) != 0)
    {
        if (!error_reported)
        {
            log_message((3 | (1<<16) | (1<<17)), "open hostweight file error. %d (sys %d)\n",
                        rs, err_no);
            error_reported = 1;
        }
        return rs;
    }
    rs = read_hostweight_file(fd, &err_no, newwtable, 16);
    if (rs != 0)
    {
        if (!error_reported)
        {
            log_message((3 | (1<<16) | (1<<17)), "open hostweight file error. %d (sys %d)\n",
                        rs, err_no);
            error_reported = 1;
        }
        goto error_return;
    }
    error_reported = 0;
    rs = hostweight_update(weight_table, newwtable,
                           16);
    if (rs != 0)
    {
        goto error_return;
    }
    hostweight_set_sm();
 error_return:
    close(fd);
    return rs;
}

variable_types: struct host_weight_table { char classname[-1]; unsigned int weight; } [16]

variable_types: fd int

variable_types: rs int


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

if ( a1 )
{
if ( a1 == 1 )
{
log_message(131078LL, "SC: hostweight_initialize(1).\n");
result = hostweight_reload();
}
else
{
log_message(131078LL, "SC: hostweight_initialize(-1).\n");
result = 0LL;
}
}
else
{
log_message(131078LL, "SC: hostweight_initialize(0).\n");
result = hostweight_initialize0();
}
return result;
}

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

if ( a1 )
{
if ( a1 == 1 )
{
log_message(131078LL, "SC: hostweight_initialize(1).\n");
result = func6();
}
else
{
log_message(131078LL, "SC: hostweight_initialize(-1).\n");
result = 0LL;
}
}
else
{
log_message(131078LL, "SC: hostweight_initialize(0).\n");
result = func0();
}
return result;
}

original_code:
MTC_S32
hostweight_initialize(
    MTC_S32 phase)
{
    MTC_STATUS ret;
    switch (phase)
    {
    case 0:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(0).\n");
        ret = hostweight_initialize0();
        break;
    case 1:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(1).\n");
        ret = hostweight_reload();
        break;
    case -1:
    default:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(-1).\n");
        ret = 0;
        break;
    }
    return ret;
}

canonical_original_code:
int hostweight_initialize(int phase)
{
    int ret;
    switch (phase)
    {
    case 0:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(0).\n");
        ret = hostweight_initialize0();
        break;
    case 1:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(1).\n");
        ret = hostweight_reload();
        break;
    case -1:
    default:
        log_message((6 | (1<<17)), "SC: hostweight_initialize(-1).\n");
        ret = 0;
        break;
    }
    return ret;
}

variable_types: phase int

variable_types: ret int


decompiled_code:
__int64 __fastcall uuid_strcpy(__int64 a1, char *a2)
{
char v2; // al
__int64 v3; // rdx
__int64 v4; // rcx
unsigned __int64 v6; // rsi
unsigned int v7; // eax
unsigned int v8; // edx
__int64 v9; // rcx
__int64 v10; // rcx
_QWORD *v11; // rdx

v2 = *a2;
v3 = 0LL;
if ( *a2 )
{
do
{
if ( v2 != 45 )
{
if ( (unsigned int)v3 > 0x1F )
return 0xFFFFFFFFLL;
v4 = (unsigned int)v3;
v3 = (unsigned int)(v3 + 1);
*(_BYTE *)(a1 + v4) = v2;
}
v2 = *++a2;
}
while ( v2 );
if ( (unsigned int)v3 > 0x1F )
return 0LL;
}
v10 = (unsigned int)(32 - v3);
if ( (unsigned int)(v3 + 1) > 0x20 )
v10 = 1LL;
v11 = (_QWORD *)(a1 + v3);
if ( (unsigned int)v10 >= 8 )
{
*v11 = 0LL;
*(_QWORD *)((char *)v11 + v10 - 8) = 0LL;
v6 = (unsigned __int64)(v11 + 1) & 0xFFFFFFFFFFFFFFF8LL;
if ( (((_DWORD)v10 + (_DWORD)v11 - (_DWORD)v6) & 0xFFFFFFF8) >= 8 )
{
v7 = (v10 + (_DWORD)v11 - v6) & 0xFFFFFFF8;
v8 = 0;
do
{
v9 = v8;
v8 += 8;
*(_QWORD *)(v6 + v9) = 0LL;
}
while ( v8 < v7 );
}
}
else if ( (v10 & 4) != 0 )
{
*(_DWORD *)v11 = 0;
*(_DWORD *)((char *)v11 + (unsigned int)v10 - 4) = 0;
}
else if ( (_DWORD)v10 )
{
*(_BYTE *)v11 = 0;
if ( (v10 & 2) != 0 )
*(_WORD *)((char *)v11 + (unsigned int)v10 - 2) = 0;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char *a2)
{
char v2; // al
__int64 v3; // rdx
__int64 v4; // rcx
unsigned __int64 v6; // rsi
unsigned int v7; // eax
unsigned int v8; // edx
__int64 v9; // rcx
__int64 v10; // rcx
_QWORD *v11; // rdx

v2 = *a2;
v3 = 0LL;
if ( *a2 )
{
do
{
if ( v2 != 45 )
{
if ( (unsigned int)v3 > 0x1F )
return 0xFFFFFFFFLL;
v4 = (unsigned int)v3;
v3 = (unsigned int)(v3 + 1);
*(_BYTE *)(a1 + v4) = v2;
}
v2 = *++a2;
}
while ( v2 );
if ( (unsigned int)v3 > 0x1F )
return 0LL;
}
v10 = (unsigned int)(32 - v3);
if ( (unsigned int)(v3 + 1) > 0x20 )
v10 = 1LL;
v11 = (_QWORD *)(a1 + v3);
if ( (unsigned int)v10 >= 8 )
{
*v11 = 0LL;
*(_QWORD *)((char *)v11 + v10 - 8) = 0LL;
v6 = (unsigned __int64)(v11 + 1) & 0xFFFFFFFFFFFFFFF8LL;
if ( (((_DWORD)v10 + (_DWORD)v11 - (_DWORD)v6) & 0xFFFFFFF8) >= 8 )
{
v7 = (v10 + (_DWORD)v11 - v6) & 0xFFFFFFF8;
v8 = 0;
do
{
v9 = v8;
v8 += 8;
*(_QWORD *)(v6 + v9) = 0LL;
}
while ( v8 < v7 );
}
}
else if ( (v10 & 4) != 0 )
{
*(_DWORD *)v11 = 0;
*(_DWORD *)((char *)v11 + (unsigned int)v10 - 4) = 0;
}
else if ( (_DWORD)v10 )
{
*(_BYTE *)v11 = 0;
if ( (v10 & 2) != 0 )
*(_WORD *)((char *)v11 + (unsigned int)v10 - 2) = 0;
}
return 0xFFFFFFFFLL;
}

original_code:
static int
uuid_strcpy(
    char *packed_uuid,
    char *uuid)
{
    char *src;
    MTC_U32 count;
    count = 0;
    for (src = uuid; *src; src++)
    {
        if (*src == '-') continue;
        if (count < 32) {
            packed_uuid[count] = *src;
            count++;
        }
        else {
            return -1;
        }
    }
    if (count < 32)
    {
        for (; count < 32; count++)
        {
            packed_uuid[count] = '\0';
        }
        return -1;
    }
    return 0;
}

canonical_original_code:
static int uuid_strcpy(char *packed_uuid, char *uuid)
{
    char *src;
    unsigned int count;
    count = 0;
    for (src = uuid; *src; src++)
    {
        if (*src == '-') continue;
        if (count < 32) {
            packed_uuid[count] = *src;
            count++;
        }
        else {
            return -1;
        }
    }
    if (count < 32)
    {
        for (; count < 32; count++)
        {
            packed_uuid[count] = '\0';
        }
        return -1;
    }
    return 0;
}

variable_types:
uuid
char *

variable_types:
src
char *

variable_types:
count
unsigned int


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

if ( *(_BYTE *)(a1 + 8) )
{
if ( *(_DWORD *)(a1 + 40) )
{
if ( *(_DWORD *)(a1 + 44) )
{
if ( *(_BYTE *)(a1 + 2408) )
{
if ( *(_BYTE *)(a1 + 2472) )
{
result = 1LL;
if ( *(_DWORD *)(a1 + 2404) > 0x3Fu )
{
xhadlog(131075LL, "%s: localhost.HostID is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: StateFile is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: HeartbeatInterface is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: host num is 0\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: UDPport is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: GenerationUUID is not set\n", "valid_config");
result = 0LL;
}
return result;
}

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

if ( *(_BYTE *)(a1 + 8) )
{
if ( *(_DWORD *)(a1 + 40) )
{
if ( *(_DWORD *)(a1 + 44) )
{
if ( *(_BYTE *)(a1 + 2408) )
{
if ( *(_BYTE *)(a1 + 2472) )
{
result = 1LL;
if ( *(_DWORD *)(a1 + 2404) > 0x3Fu )
{
xhadlog(131075LL, "%s: localhost.HostID is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: StateFile is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: HeartbeatInterface is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: host num is 0\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: UDPport is not set\n", "valid_config");
result = 0LL;
}
}
else
{
xhadlog(131075LL, "%s: GenerationUUID is not set\n", "valid_config");
result = 0LL;
}
return result;
}

variable_types: struct ha_config { unsigned char version[8]; struct ha_config_common { unsigned char generation_uuid[32]; unsigned int udp_port; unsigned int hostnum; struct ha_config_host_info { unsigned char host_id[32]; unsigned int ip_address; } host[64]; unsigned int heartbeat_interval; unsigned int heartbeat_timeout; unsigned int statefile_interval; unsigned int statefile_timeout; unsigned int heartbeat_watchdog_timeout; unsigned int statefile_watchdog_timeout; unsigned int boot_join_timeout; unsigned int enable_join_timeout; unsigned int xapi_healthcheck_interval; unsigned int xapi_healthcheck_timeout; unsigned int xapi_restart_retry; unsigned int xapi_restart_timeout; unsigned int xapi_licensecheck_timeout; } common; struct ha_config_local { unsigned int localhost_index; unsigned char heartbeat_interface[64]; unsigned char statefile_path[256]; } local; } *

user_defined_types: struct ha_config { unsigned char version[8]; struct ha_config_common common; struct ha_config_local local; }

user_defined_types: struct ha_config_common { unsigned char generation_uuid[32]; unsigned int udp_port; unsigned int hostnum; struct ha_config_host_info host[64]; unsigned int heartbeat_interval; unsigned int heartbeat_timeout; unsigned int statefile_interval; unsigned int statefile_timeout; unsigned int heartbeat_watchdog_timeout; unsigned int statefile_watchdog_timeout; unsigned int boot_join_timeout; unsigned int enable_join_timeout; unsigned int xapi_healthcheck_interval; unsigned int xapi_healthcheck_timeout; unsigned int xapi_restart_retry; unsigned int xapi_restart_timeout; unsigned int xapi_licensecheck_timeout; }


decompiled_code:
_BOOL8 __fastcall valid_version(const char *a1)
{
size_t v1; // r8
_BOOL8 result; // rax

v1 = strlen(a1);
result = 0LL;
if ( v1 <= 7 )
result = strcmp(a1, "1.0") == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1)
{
size_t v1; // r8
_BOOL8 result; // rax

v1 = strlen(a1);
result = 0LL;
if ( v1 <= 7 )
result = strcmp(a1, "1.0") == 0;
return result;
}

original_code:
static MTC_S32
valid_version(
    char *version)
{
    if (strlen(version) >= sizeof(((HA_CONFIG *)0)->version)) {
        return 0;
    }
    if (strcmp(version, "1.0") != 0) {
        return 0;
    }
    return 1;
}

canonical_original_code:
static int valid_version(char *version)
{
    if (strlen(version) >= sizeof(((struct ha_config *)0)->version)) {
        return 0;
    }
    if (strcmp(version, "1.0") != 0) {
        return 0;
    }
    return 1;
}

variable_types:
version
char *

user_defined_types: struct ha_config_local { unsigned int localhost_index; unsigned char heartbeat_interface[64]; unsigned char statefile_path[256]; }

user_defined_types: struct ha_config_host_info { unsigned char host_id[32]; unsigned int ip_address; }


decompiled_code:
__int64 fist_initialize()
{
char **v0; // rdx
__int64 result; // rax

v0 = &fisttab;
do
{
result = fist_genhas(*v0);
*((_DWORD *)v0 - 1) = result;
}
while ( v0 != &fisttab + 76 );
return result;
}

canonical_decompiled_code:
__int64 func1()
{
char **v0; // rdx
__int64 result; // rax

v0 = &fisttab;
do
{
result = func0(*v0);
*((_DWORD *)v0 - 1) = result;
}
while ( v0 != &fisttab + 76 );
return result;
}

original_code:
void
fist_initialize()
{
    int index;
    for (index = 0; index < sizeof(fisttab) / sizeof(struct _fisttab); index++)
    {
        fisttab[index].hash = _fist_genhas(fisttab[index].name);
    }
}

user_defined_types: struct _fisttab { char *name; unsigned char sticky; unsigned char enabled; int hash; }


chop

decompiled_code:
size_t __fastcall chop(const char *a1)
{
size_t result; // rax
const char *v2; // rbx

result = strlen(a1);
if ( (_DWORD)result )
{
v2 = &a1[(unsigned int)(result - 1)];
if ( *v2 == 10 )
*v2 = 0;
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(const char *a1)
{
size_t result; // rax
const char *v2; // rbx

result = strlen(a1);
if ( (_DWORD)result )
{
v2 = &a1[(unsigned int)(result - 1)];
if ( *v2 == 10 )
*v2 = 0;
}
return result;
}

original_code:
void chop(MTC_S8 *line)
{
    MTC_U32 len = strlen(line);
    if (len > 0 && line[len - 1] == '\n')
    {
        line[len - 1] = '\0';
    }
}

canonical_original_code:
void chop(char *line)
{
    unsigned int len = strlen(line);
    if (len > 0 && line[len - 1] == '\n')
    {
        line[len - 1] = '\0';
    }
}

variable_types:
line
char *

variable_types:
len
unsigned int