redLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 0LL) )
result = pinClearPc(255LL, 0LL);
else
result = pinSetPc(255LL, 0LL);
return result;
}

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

if ( (unsigned int)pinReadPc(255LL, 0LL) )
result = pinClearPc(255LL, 0LL);
else
result = pinSetPc(255LL, 0LL);
return result;
}

original_code: static inline void redLedToggle(void) { led0Toggle(); }

canonical_original_code: static inline void redLedToggle() { led0Toggle(); }


radioGetLastRSSI

repo: atiselsts/osw

decompiled_code:
__int64 radioGetLastRSSI()
{
return pcRadioGetLastRSSI();
}

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

original_code:
static inline int8_t radioGetLastRSSI(void) {
    return pcRadioGetLastRSSI();
}

canonical_original_code:
static inline char radioGetLastRSSI() {
    return pcRadioGetLastRSSI();
}

return_type: char


radioGetLastLQI

decompiled_code:
__int64 radioGetLastLQI()
{
return pcRadioGetLastLQI();
}

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

original_code:
static inline uint8_t radioGetLastLQI(void) {
    return pcRadioGetLastLQI();
}

canonical_original_code:
static inline unsigned char radioGetLastLQI() {
    return pcRadioGetLastLQI();
}

return_type: unsigned char


radioOn

decompiled_code:
__int64 radioOn()
{
return pcRadioOn();
}

canonical_decompiled_code:
__int64 func8()
{
return pcRadioOn();
}

original_code:
static inline void radioOn(void) {
    pcRadioOn();
}

canonical_original_code:
static inline void radioOn() {
    pcRadioOn();
}

return_type: void


doMsleep

decompiled_code:
int __fastcall doMsleep(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

canonical_decompiled_code:
int __fastcall func9(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

original_code:
extern inline void doMsleep(uint16_t milliseconds) {
    usleep(milliseconds * 1000);
}

canonical_original_code:
extern inline void doMsleep(unsigned short milliseconds) {
    usleep(milliseconds * 1000);
}

variable_types:
milliseconds
unsigned short


addAvgStatistics

decompiled_code:
__int64 __fastcall addAvgStatistics(int a1, __int16 a2, unsigned __int8 a3, unsigned __int8 a4)
{
unsigned int v6; // edx
int v7; // eax
int v8; // eax
int v9; // eax
int v10; // edx
unsigned __int16 v11; // bx
unsigned __int16 v12; // di
bool v13; // zf
unsigned __int16 v14; // bp
unsigned __int16 v15; // r13
__int16 v16; // dx
__int16 v17; // r12
int v18; // ecx
int v19; // esi
int v20; // esi
__int16 v21; // si
int v22; // ecx
int v23; // er13
int v24; // er12
int v25; // ebp
__int16 v26; // ax
__int64 result; // rax
int v28; // [rsp+Ch] [rbp-5Ch]
__int16 v29[44]; // [rsp+10h] [rbp-58h] BYREF

v6 = (18351 * (unsigned int)(unsigned __int16)a1) >> 16;
v7 = a1 - v6;
LOWORD(v7) = (unsigned __int16)(a1 - v6) >> 1;
v8 = v6 + v7;
LOWORD(v8) = (unsigned __int16)v8 >> 4;
v9 = a1 - 25 * v8;
*((_WORD *)&avgPDR + (__int16)v9) = a2;
*((_WORD *)&avgRssi + (__int16)v9) = a3;
*((_WORD *)&avgLQI + (__int16)v9) = a4;
if ( calledBefore_3 && prevPrevTestNumber_2 <= (int)(unsigned __int16)a1 )
{
if ( prevPrevTestNumber_2 < (int)(unsigned __int16)a1 )
{
v18 = (unsigned __int16)prevPrevTestNumber_2 + 1;
prevPrevTestNumber_2 = v18;
if ( (unsigned __int16)a1 > (__int16)v18 )
{
do
{
HIWORD(v19) = HIWORD(v18);
LOWORD(v19) = (__int16)v18 >> 15;
v20 = v18 - 25 * (((5243 * (__int16)v18) >> 17) - v19);
*((_WORD *)&avgPDR + (__int16)v20) = 0;
*((_WORD *)&avgRssi + (__int16)v20) = 0;
*((_WORD *)&avgLQI + (__int16)v20) = 0;
++v18;
}
while ( (unsigned __int16)a1 > (__int16)v18 );
prevPrevTestNumber_2 = v18;
}
}
}
else
{
calledBefore_3 = 1;
prevPrevTestNumber_2 = a1;
avgPDR = -1LL;
*(&avgPDR + 1) = -1LL;
*(&avgPDR + 2) = -1LL;
*(&avgPDR + 3) = -1LL;
*(&avgPDR + 4) = -1LL;
*(&avgPDR + 5) = -1LL;
*((_WORD *)&avgPDR + 24) = -1;
avgRssi = -1LL;
*(&avgRssi + 1) = -1LL;
*(&avgRssi + 2) = -1LL;
*(&avgRssi + 3) = -1LL;
*(&avgRssi + 4) = -1LL;
*(&avgRssi + 5) = -1LL;
*((_WORD *)&avgRssi + 24) = -1;
avgLQI = -1LL;
*(&avgLQI + 1) = -1LL;
*(&avgLQI + 2) = -1LL;
*(&avgLQI + 3) = -1LL;
*(&avgLQI + 4) = -1LL;
*(&avgLQI + 5) = -1LL;
*((_WORD *)&avgLQI + 24) = -1;
}
v10 = v9;
v11 = 10;
v12 = 0;
do
{
v12 += *((_WORD *)&avgPDR + (__int16)v10);
v13 = (_WORD)v10-- == 0;
if ( v13 )
v10 = 24;
--v11;
}
while ( v11 );
v14 = 0;
v15 = 0;
v16 = 25;
v17 = 0;
do
{
v21 = *((_WORD *)&avgPDR + (__int16)v9);
if ( v21 )
{
v15 += v21;
v17 += *((_WORD *)&avgRssi + (__int16)v9);
v14 += *((_WORD *)&avgLQI + (__int16)v9);
if ( ++v11 > 9u )
{
v28 = (int)((1717986919 * ((unsigned __int64)v12 + 5)) >> 32) >> 2;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, (1717986919 * (v12 + 5LL)) >> 34);
serialPrintV(strBuf);
LABEL_19:
v22 = v11 >> 1;
v23 = (v22 + v15) / v11;
v24 = (v22 + v17) / v11;
v25 = (v22 + v14) / v11;
v26 = platformFixRssi((unsigned __int16)v24);
sprintf(
strBuf,
"nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n",
v11,
(unsigned __int16)v23,
(unsigned int)v26,
(unsigned __int16)v25);
serialPrintV(strBuf);
v29[1] = testId;
v29[0] = localAddress;
v29[2] = a1;
v29[3] = 10;
v29[4] = v28;
v29[5] = v11;
v29[6] = v23;
v29[7] = v24;
v29[8] = v25;
return crc16(v29, 18LL);
}
}
v13 = (_WORD)v9 == 0;
LOWORD(v9) = v9 - 1;
if ( v13 )
LOWORD(v9) = 24;
--v16;
}
while ( v16 );
v28 = (int)((1717986919 * ((unsigned __int64)v12 + 5)) >> 32) >> 2;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, (1717986919 * (v12 + 5LL)) >> 34);
result = serialPrintV(strBuf);
if ( v11 )
goto LABEL_19;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int16 a2, unsigned __int8 a3, unsigned __int8 a4)
{
unsigned int v6; // edx
int v7; // eax
int v8; // eax
int v9; // eax
int v10; // edx
unsigned __int16 v11; // bx
unsigned __int16 v12; // di
bool v13; // zf
unsigned __int16 v14; // bp
unsigned __int16 v15; // r13
__int16 v16; // dx
__int16 v17; // r12
int v18; // ecx
int v19; // esi
int v20; // esi
__int16 v21; // si
int v22; // ecx
int v23; // er13
int v24; // er12
int v25; // ebp
__int16 v26; // ax
__int64 result; // rax
int v28; // [rsp+Ch] [rbp-5Ch]
__int16 v29[44]; // [rsp+10h] [rbp-58h] BYREF

v6 = (18351 * (unsigned int)(unsigned __int16)a1) >> 16;
v7 = a1 - v6;
LOWORD(v7) = (unsigned __int16)(a1 - v6) >> 1;
v8 = v6 + v7;
LOWORD(v8) = (unsigned __int16)v8 >> 4;
v9 = a1 - 25 * v8;
*((_WORD *)&avgPDR + (__int16)v9) = a2;
*((_WORD *)&avgRssi + (__int16)v9) = a3;
*((_WORD *)&avgLQI + (__int16)v9) = a4;
if ( calledBefore_3 && prevPrevTestNumber_2 <= (int)(unsigned __int16)a1 )
{
if ( prevPrevTestNumber_2 < (int)(unsigned __int16)a1 )
{
v18 = (unsigned __int16)prevPrevTestNumber_2 + 1;
prevPrevTestNumber_2 = v18;
if ( (unsigned __int16)a1 > (__int16)v18 )
{
do
{
HIWORD(v19) = HIWORD(v18);
LOWORD(v19) = (__int16)v18 >> 15;
v20 = v18 - 25 * (((5243 * (__int16)v18) >> 17) - v19);
*((_WORD *)&avgPDR + (__int16)v20) = 0;
*((_WORD *)&avgRssi + (__int16)v20) = 0;
*((_WORD *)&avgLQI + (__int16)v20) = 0;
++v18;
}
while ( (unsigned __int16)a1 > (__int16)v18 );
prevPrevTestNumber_2 = v18;
}
}
}
else
{
calledBefore_3 = 1;
prevPrevTestNumber_2 = a1;
avgPDR = -1LL;
*(&avgPDR + 1) = -1LL;
*(&avgPDR + 2) = -1LL;
*(&avgPDR + 3) = -1LL;
*(&avgPDR + 4) = -1LL;
*(&avgPDR + 5) = -1LL;
*((_WORD *)&avgPDR + 24) = -1;
avgRssi = -1LL;
*(&avgRssi + 1) = -1LL;
*(&avgRssi + 2) = -1LL;
*(&avgRssi + 3) = -1LL;
*(&avgRssi + 4) = -1LL;
*(&avgRssi + 5) = -1LL;
*((_WORD *)&avgRssi + 24) = -1;
avgLQI = -1LL;
*(&avgLQI + 1) = -1LL;
*(&avgLQI + 2) = -1LL;
*(&avgLQI + 3) = -1LL;
*(&avgLQI + 4) = -1LL;
*(&avgLQI + 5) = -1LL;
*((_WORD *)&avgLQI + 24) = -1;
}
v10 = v9;
v11 = 10;
v12 = 0;
do
{
v12 += *((_WORD *)&avgPDR + (__int16)v10);
v13 = (_WORD)v10-- == 0;
if ( v13 )
v10 = 24;
--v11;
}
while ( v11 );
v14 = 0;
v15 = 0;
v16 = 25;
v17 = 0;
do
{
v21 = *((_WORD *)&avgPDR + (__int16)v9);
if ( v21 )
{
v15 += v21;
v17 += *((_WORD *)&avgRssi + (__int16)v9);
v14 += *((_WORD *)&avgLQI + (__int16)v9);
if ( ++v11 > 9u )
{
v28 = (int)((1717986919 * ((unsigned __int64)v12 + 5)) >> 32) >> 2;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, (1717986919 * (v12 + 5LL)) >> 34);
func2(strBuf);
LABEL_19:
v22 = v11 >> 1;
v23 = (v22 + v15) / v11;
v24 = (v22 + v17) / v11;
v25 = (v22 + v14) / v11;
v26 = func1((unsigned __int16)v24);
sprintf(
strBuf,
"nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n",
v11,
(unsigned __int16)v23,
(unsigned int)v26,
(unsigned __int16)v25);
func2(strBuf);
v29[1] = testId;
v29[0] = localAddress;
v29[2] = a1;
v29[3] = 10;
v29[4] = v28;
v29[5] = v11;
v29[6] = v23;
v29[7] = v24;
v29[8] = v25;
return crc16(v29, 18LL);
}
}
v13 = (_WORD)v9 == 0;
LOWORD(v9) = v9 - 1;
if ( v13 )
LOWORD(v9) = 24;
--v16;
}
while ( v16 );
v28 = (int)((1717986919 * ((unsigned __int64)v12 + 5)) >> 32) >> 2;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, (1717986919 * (v12 + 5LL)) >> 34);
result = func2(strBuf);
if ( v11 )
goto LABEL_19;
return result;
}

original_code:
void addAvgStatistics(uint16_t prevTestNumber, uint16_t prevTestPacketsRx,
                      uint8_t rssi, uint8_t lqi)
{
    int16_t i;
    static _Bool calledBefore;
    static int16_t prevPrevTestNumber;
    int16_t idx = prevTestNumber % 25;
    int16_t startIdx = idx;
    avgPDR[idx] = prevTestPacketsRx;
    avgRssi[idx] = rssi;
    avgLQI[idx] = lqi;
    if (!calledBefore || prevPrevTestNumber > prevTestNumber) {
        calledBefore = 1;
        prevPrevTestNumber = prevTestNumber;
        memset(avgPDR, 0xff, sizeof(avgPDR));
        memset(avgRssi, 0xff, sizeof(avgRssi));
        memset(avgLQI, 0xff, sizeof(avgLQI));
    } else if (prevPrevTestNumber < prevTestNumber) {
        prevPrevTestNumber++;
        while (prevPrevTestNumber < prevTestNumber) {
            avgPDR[prevPrevTestNumber % 25] = 0;
            avgRssi[prevPrevTestNumber % 25] = 0;
            avgLQI[prevPrevTestNumber % 25] = 0;
            prevPrevTestNumber++;
        }
    }
    uint16_t numSamples = 0;
    uint16_t sumSamples = 0;
    int16_t sumRssi = 0;
    uint16_t sumLqi = 0;
    uint16_t numSamplesNe = 0;
    uint16_t sumSamplesNe = 0;
    int16_t sumRssiNe = 0;
    uint16_t sumLqiNe = 0;
    for (i = 0; i < 10; ++i) {
        if (avgPDR[idx] == ~0u) break;
        sumSamples += avgPDR[idx];
        sumRssi += avgRssi[idx];
        sumLqi += avgLQI[idx];
        numSamples++;
        if (idx == 0) idx = 25 - 1;
        else idx--;
    }
    if (numSamples == 0) return;
    idx = startIdx;
    for (i = 0; i < 25; ++i) {
        if (avgPDR[idx] == ~0u) break;
        if (avgPDR[idx] != 0) {
            sumSamplesNe += avgPDR[idx];
            sumRssiNe += avgRssi[idx];
            sumLqiNe += avgLQI[idx];
            numSamplesNe++;
            if (numSamplesNe >= 10) break;
        }
        if (idx == 0) idx = 25 - 1;
        else idx--;
    }
    uint16_t samplesAvg, samplesAvgNe;
    int16_t rssiAvg;
    uint16_t lqiAvg;
    samplesAvg = (sumSamples + (numSamples / 2)) / numSamples;
    sprintf(strBuf, "avg (%d runs): %d\n", numSamples, samplesAvg);
    if (1 == 1) { serialPrintV(strBuf); } else { if (_print_buf) { debugPrintfFormat(strBuf); serialPrint(_print_buf); }; };
    if (numSamplesNe == 0) return;
    samplesAvgNe = (sumSamplesNe + (numSamplesNe / 2)) / numSamplesNe;
    rssiAvg = (sumRssiNe + (numSamplesNe / 2)) / numSamplesNe;
    lqiAvg = (sumLqiNe + (numSamplesNe / 2)) / numSamplesNe;
    sprintf(strBuf, "nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n",
            numSamplesNe, samplesAvgNe, platformFixRssi(rssiAvg), lqiAvg);
    if (1 == 1) { serialPrintV(strBuf); } else { if (_print_buf) { debugPrintfFormat(strBuf); serialPrint(_print_buf); }; };
    RadioInfoPacket_t packet;
    packet.testId = testId;
    packet.address = localAddress;
    packet.lastTestNo = prevTestNumber;
    packet.numTests = numSamples;
    packet.avgPdr = samplesAvg;
    packet.numTestsNe = numSamplesNe;
    packet.avgPdrNe = samplesAvgNe;
    packet.avgRssiNe = rssiAvg;
    packet.avgLqiNe = lqiAvg;
    packet.crc = crc16((uint8_t *)&packet, sizeof(packet) - 2);
}

canonical_original_code:
void addAvgStatistics(unsigned short prevTestNumber, unsigned short prevTestPacketsRx, unsigned char rssi, unsigned char lqi)
{
    short i;
    static _Bool calledBefore;
    static short prevPrevTestNumber;
    short idx = prevTestNumber % 25;
    short startIdx = idx;
    avgPDR[idx] = prevTestPacketsRx;
    avgRssi[idx] = rssi;
    avgLQI[idx] = lqi;
    if (!calledBefore || prevPrevTestNumber > prevTestNumber) {
        calledBefore = 1;
        prevPrevTestNumber = prevTestNumber;
        memset(avgPDR, 0xff, sizeof(avgPDR));
        memset(avgRssi, 0xff, sizeof(avgRssi));
        memset(avgLQI, 0xff, sizeof(avgLQI));
    } else if (prevPrevTestNumber < prevTestNumber) {
        prevPrevTestNumber++;
        while (prevPrevTestNumber < prevTestNumber) {
            avgPDR[prevPrevTestNumber % 25] = 0;
            avgRssi[prevPrevTestNumber % 25] = 0;
            avgLQI[prevPrevTestNumber % 25] = 0;
            prevPrevTestNumber++;
        }
    }
    unsigned short numSamples = 0;
    unsigned short sumSamples = 0;
    short sumRssi = 0;
    unsigned short sumLqi = 0;
    unsigned short numSamplesNe = 0;
    unsigned short sumSamplesNe = 0;
    short sumRssiNe = 0;
    unsigned short sumLqiNe = 0;
    for (i = 0; i < 10; ++i) {
        if (avgPDR[idx] == ~0u) break;
        sumSamples += avgPDR[idx];
        sumRssi += avgRssi[idx];
        sumLqi += avgLQI[idx];
        numSamples++;
        if (idx == 0) idx = 25 - 1;
        else idx--;
    }
    if (numSamples == 0) return;
    idx = startIdx;
    for (i = 0; i < 25; ++i) {
        if (avgPDR[idx] == ~0u) break;
        if (avgPDR[idx] != 0) {
            sumSamplesNe += avgPDR[idx];
            sumRssiNe += avgRssi[idx];
            sumLqiNe += avgLQI[idx];
            numSamplesNe++;
            if (numSamplesNe >= 10) break;
        }
        if (idx == 0) idx = 25 - 1;
        else idx--;
    }
    unsigned short samplesAvg; unsigned short samplesAvgNe;
    short rssiAvg;
    unsigned short lqiAvg;
    samplesAvg = (sumSamples + (numSamples / 2)) / numSamples;
    sprintf(strBuf, "avg (%d runs): %d\n", numSamples, samplesAvg);
    if (1 == 1) { serialPrintV(strBuf); } else { if (_print_buf) { debugPrintfFormat(strBuf); serialPrint(_print_buf); }; };
    if (numSamplesNe == 0) return;
    samplesAvgNe = (sumSamplesNe + (numSamplesNe / 2)) / numSamplesNe;
    rssiAvg = (sumRssiNe + (numSamplesNe / 2)) / numSamplesNe;
    lqiAvg = (sumLqiNe + (numSamplesNe / 2)) / numSamplesNe;
    sprintf(strBuf, "nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n",
            numSamplesNe, samplesAvgNe, platformFixRssi(rssiAvg), lqiAvg);
    if (1 == 1) { serialPrintV(strBuf); } else { if (_print_buf) { debugPrintfFormat(strBuf); serialPrint(_print_buf); }; };
    struct RadioInfoPacket_s packet;
    packet.testId = testId;
    packet.address = localAddress;
    packet.lastTestNo = prevTestNumber;
    packet.numTests = numSamples;
    packet.avgPdr = samplesAvg;
    packet.numTestsNe = numSamplesNe;
    packet.avgPdrNe = samplesAvgNe;
    packet.avgRssiNe = rssiAvg;
    packet.avgLqiNe = lqiAvg;
    packet.crc = crc16((unsigned char *)&packet, sizeof(packet) - 2);
}

variable_types:
prevTestNumber
unsigned short

variable_types:
prevTestPacketsRx
unsigned short

variable_types:
rssi
unsigned char

variable_types:
lqi
unsigned char

variable_types: i short

variable_types: prevPrevTestNumber short

variable_types: idx short

variable_types: startIdx short

variable_types:
numSamples
unsigned short

variable_types:
sumSamples
unsigned short

variable_types: sumRssi short

variable_types:
sumLqi
unsigned short

variable_types:
numSamplesNe
unsigned short

variable_types:
sumSamplesNe
unsigned short

variable_types: sumRssiNe short

variable_types:
sumLqiNe
unsigned short

variable_types:
samplesAvg
unsigned short

variable_types:
samplesAvgNe
unsigned short

variable_types: rssiAvg short

variable_types:
lqiAvg
unsigned short

variable_types: struct RadioInfoPacket_s { unsigned short address; unsigned short testId; unsigned short lastTestNo; unsigned short numTests; unsigned short avgPdr; unsigned short numTestsNe; unsigned short avgPdrNe; unsigned short avgRssiNe; unsigned short avgLqiNe; unsigned short crc; }


endTest

decompiled_code:
__int64 endTest()
{
testInProgress = 0;
prevTestPacketsRx = currentTestPacketsRx;
prevTestRssiSum = rssiSum;
prevTestLqiSum = lqiSum;
currentTestPacketsRx = 0;
return redLedToggle();
}

canonical_decompiled_code:
__int64 func11()
{
testInProgress = 0;
prevTestPacketsRx = currentTestPacketsRx;
prevTestRssiSum = rssiSum;
prevTestLqiSum = lqiSum;
currentTestPacketsRx = 0;
return func3();
}

original_code:
void endTest(void) {
    testInProgress = 0;
    prevTestPacketsRx = currentTestPacketsRx;
    prevTestRssiSum = rssiSum;
    prevTestLqiSum = lqiSum;
    currentTestPacketsRx = 0;
    redLedToggle();
}

canonical_original_code:
void endTest() {
    testInProgress = 0;
    prevTestPacketsRx = currentTestPacketsRx;
    prevTestRssiSum = rssiSum;
    prevTestLqiSum = lqiSum;
    currentTestPacketsRx = 0;
    redLedToggle();
}


startTest

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

testInProgress = 1;
currentTestNumber = a1;
testStartTime = getJiffies();
result = redLedToggle();
rssiSum = 0;
lqiSum = 0;
return result;
}

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

testInProgress = 1;
currentTestNumber = a1;
testStartTime = func0();
result = func3();
rssiSum = 0;
lqiSum = 0;
return result;
}

original_code:
void startTest(uint16_t newTestNumber) {
    testInProgress = 1;
    currentTestNumber = newTestNumber;
    testStartTime = getJiffies();
    redLedToggle();
    rssiSum = 0;
    lqiSum = 0;
}

canonical_original_code:
void startTest(unsigned short newTestNumber) {
    testInProgress = 1;
    currentTestNumber = newTestNumber;
    testStartTime = getJiffies();
    redLedToggle();
    rssiSum = 0;
    lqiSum = 0;
}

variable_types:
newTestNumber
unsigned short


recvCallback

decompiled_code:
void __fastcall recvCallback(__int16 *a1, __int16 a2)
{
int v2; // ebx
__int16 v3; // bp

if ( a2 == 30 )
{
if ( (unsigned __int16)crc16(a1, 28LL) == a1[14] )
{
rssiSum += (unsigned __int8)radioGetLastRSSI();
lqiSum += (unsigned __int8)radioGetLastLQI();
v3 = *a1;
testId = a1[1];
if ( __PAIR32__(currentTestId, currentTestNumber) != __PAIR32__(testId, v3) )
{
currentTestId = testId;
if ( testInProgress )
endTest();
}
if ( !testInProgress )
startTest(v3);
++currentTestPacketsRx;
}
else
{
v2 = getJiffies();
if ( nextCrcErrorReportTime_1 - v2 < 0 )
{
serialPrintV("wrong CRC\n");
nextCrcErrorReportTime_1 = v2 + 2500;
}
}
}
else if ( print_buf )
{
debugPrintfFormat("rcvd length=%d\n", (unsigned int)a2);
serialPrint(print_buf);
}
}

canonical_decompiled_code:
void __fastcall func13(__int16 *a1, __int16 a2)
{
int v2; // ebx
__int16 v3; // bp

if ( a2 == 30 )
{
if ( (unsigned __int16)crc16(a1, 28LL) == a1[14] )
{
rssiSum += (unsigned __int8)func4();
lqiSum += (unsigned __int8)func5();
v3 = *a1;
testId = a1[1];
if ( __PAIR32__(currentTestId, currentTestNumber) != __PAIR32__(testId, v3) )
{
currentTestId = testId;
if ( testInProgress )
func11();
}
if ( !testInProgress )
func12(v3);
++currentTestPacketsRx;
}
else
{
v2 = func0();
if ( nextCrcErrorReportTime_1 - v2 < 0 )
{
func2("wrong CRC\n");
nextCrcErrorReportTime_1 = v2 + 2500;
}
}
}
else if ( print_buf )
{
debugPrintfFormat("rcvd length=%d\n", (unsigned int)a2);
serialPrint(print_buf);
}
}

original_code:
void recvCallback(uint8_t *data, int16_t len)
{
    if (len != 30) {
        if (2 == 1) { serialPrintV("rcvd length=%d\n", len); } else { if (_print_buf) { debugPrintfFormat("rcvd length=%d\n", len); serialPrint(_print_buf); }; };
        return;
    }
    uint16_t calcCrc, recvCrc;
    calcCrc = crc16(data, 30 - 2);
    memcpy(&recvCrc, data + 30 - 2, sizeof(uint16_t));
    if (calcCrc != recvCrc) {
        static uint32_t nextCrcErrorReportTime;
        uint32_t now = getJiffies();
        if (((int32_t) ((nextCrcErrorReportTime) - (now)) < 0)) {
            if (1 == 1) { serialPrintV("wrong CRC\n"); } else { if (_print_buf) { debugPrintfFormat("wrong CRC\n"); serialPrint(_print_buf); }; };
            nextCrcErrorReportTime = now + ((5 + 20) * 100u);
        }
        return;
    }
    uint8_t rssi;
    rssi = radioGetLastRSSI();
    rssiSum += rssi;
    lqiSum += radioGetLastLQI();
    uint16_t testNum;
    memcpy(&testNum, data, sizeof(uint16_t));
    memcpy(&testId, data + 2, sizeof(uint16_t));
    if (testNum != currentTestNumber || testId != currentTestId) {
        currentTestId = testId;
        if (testInProgress) endTest();
    }
    if (!testInProgress) {
        startTest(testNum);
    }
    ++currentTestPacketsRx;
}

canonical_original_code:
void recvCallback(unsigned char *data, short len)
{
    if (len != 30) {
        if (2 == 1) { serialPrintV("rcvd length=%d\n", len); } else { if (_print_buf) { debugPrintfFormat("rcvd length=%d\n", len); serialPrint(_print_buf); }; };
        return;
    }
    unsigned short calcCrc; unsigned short recvCrc;
    calcCrc = crc16(data, 30 - 2);
    memcpy(&recvCrc, data + 30 - 2, sizeof(unsigned short));
    if (calcCrc != recvCrc) {
        static unsigned int nextCrcErrorReportTime;
        unsigned int now = getJiffies();
        if (((int) ((nextCrcErrorReportTime) - (now)) < 0)) {
            if (1 == 1) { serialPrintV("wrong CRC\n"); } else { if (_print_buf) { debugPrintfFormat("wrong CRC\n"); serialPrint(_print_buf); }; };
            nextCrcErrorReportTime = now + ((5 + 20) * 100u);
        }
        return;
    }
    unsigned char rssi;
    rssi = radioGetLastRSSI();
    rssiSum += rssi;
    lqiSum += radioGetLastLQI();
    unsigned short testNum;
    memcpy(&testNum, data, sizeof(unsigned short));
    memcpy(&testId, data + 2, sizeof(unsigned short));
    if (testNum != currentTestNumber || testId != currentTestId) {
        currentTestId = testId;
        if (testInProgress) endTest();
    }
    if (!testInProgress) {
        startTest(testNum);
    }
    ++currentTestPacketsRx;
}

variable_types:
data
unsigned char *

variable_types: len short

variable_types:
calcCrc
unsigned short

variable_types:
recvCrc
unsigned short

variable_types:
nextCrcErrorReportTime
unsigned int

variable_types:
now
unsigned int

variable_types:
rssi
unsigned char

variable_types:
testNum
unsigned short


decompiled_code:
void recvCallback1()
{
__int16 v0; // ax
char *v1; // rax

v0 = radioRecv((__int64)buffer_0, 0x80u);
if ( v0 > 0 )
{
recvCallback(buffer_0, v0);
}
else if ( print_buf )
{
v1 = strerror(-v0);
debugPrintfFormat("radio rx error: %s\n", v1);
serialPrint(print_buf);
}
}

canonical_decompiled_code:
void func14()
{
__int16 v0; // ax
char *v1; // rax

v0 = func6((__int64)buffer_0, 0x80u);
if ( v0 > 0 )
{
func13(buffer_0, v0);
}
else if ( print_buf )
{
v1 = strerror(-v0);
debugPrintfFormat("radio rx error: %s\n", v1);
serialPrint(print_buf);
}
}

original_code:
void recvCallback1(void)
{
    static uint8_t buffer[128];
    int16_t len;
    len = radioRecv(buffer, sizeof(buffer));
    if (len > 0) {
        recvCallback(buffer, len);
    } else {
        if (2 == 1) { serialPrintV("radio rx error: %s\n", strerror(-len)); } else { if (_print_buf) { debugPrintfFormat("radio rx error: %s\n", strerror(-len)); serialPrint(_print_buf); }; };
    }
}

canonical_original_code:
void recvCallback1()
{
    static unsigned char buffer[128];
    short len;
    len = radioRecv(buffer, sizeof(buffer));
    if (len > 0) {
        recvCallback(buffer, len);
    } else {
        if (2 == 1) { serialPrintV("radio rx error: %s\n", strerror(-len)); } else { if (_print_buf) { debugPrintfFormat("radio rx error: %s\n", strerror(-len)); serialPrint(_print_buf); }; };
    }
}

variable_types:
buffer
unsigned char [128]


serialPrintV

decompiled_code:
__int64 serialPrintV()
{
return serialPrint();
}

canonical_decompiled_code:
__int64 func0()
{
return serialPrint();
}

original_code:
static inline void serialPrintV(const char* str, ...) {
    serialPrint(str);
}

canonical_original_code:
static inline void serialPrintV(char *str, ...) {
    serialPrint(str);
}

variable_types:
str
char *


radioOn

canonical_decompiled_code:
__int64 func2()
{
return pcRadioOn();
}


blueLedOn

decompiled_code:
__int64 blueLedOn()
{
return pinSetPc(255LL, 2LL);
}

canonical_decompiled_code:
__int64 func3()
{
return pinSetPc(255LL, 2LL);
}

original_code: static inline void blueLedOn(void) { led2On(); }

canonical_original_code: static inline void blueLedOn() { led2On(); }


ledOn

decompiled_code:
__int64 ledOn()
{
return pinSetPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
return pinSetPc(255LL, 0LL);
}

original_code: static inline void ledOn(void) { led0On(); }

canonical_original_code: static inline void ledOn() { led0On(); }


blueLedOff

decompiled_code:
__int64 blueLedOff()
{
return pinClearPc(255LL, 2LL);
}

canonical_decompiled_code:
__int64 func5()
{
return pinClearPc(255LL, 2LL);
}

original_code: static inline void blueLedOff(void) { led2Off(); }

canonical_original_code: static inline void blueLedOff() { led2Off(); }


ledOff

decompiled_code:
__int64 ledOff()
{
return pinClearPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func6()
{
return pinClearPc(255LL, 0LL);
}

original_code: static inline void ledOff(void) { led0Off(); }

canonical_original_code: static inline void ledOff() { led0Off(); }


greenLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 1LL) )
result = pinClearPc(255LL, 1LL);
else
result = pinSetPc(255LL, 1LL);
return result;
}

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

if ( (unsigned int)pinReadPc(255LL, 1LL) )
result = pinClearPc(255LL, 1LL);
else
result = pinSetPc(255LL, 1LL);
return result;
}

original_code: static inline void greenLedToggle(void) { led1Toggle(); }

canonical_original_code: static inline void greenLedToggle() { led1Toggle(); }


radioGetLastRSSI

canonical_decompiled_code:
__int64 func9()
{
return pcRadioGetLastRSSI();
}


doMsleep

canonical_decompiled_code:
int __fastcall func13(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

variable_types:
milliseconds
unsigned short


setLocalAddress

decompiled_code:
void setLocalAddress()
{
;
}

canonical_decompiled_code:
void func14()
{
;
}

original_code:
void setLocalAddress(void) {
}

canonical_original_code:
void setLocalAddress() {
}


sendCounter

decompiled_code:
void __noreturn sendCounter()
{
while ( 1 )
{
if ( print_buf )
{
debugPrintfFormat("0x%04x: sending counter %i\n", localAddress, (unsigned __int8)sendBuffer_0);
serialPrint(print_buf);
}
ledOn();
if ( (unsigned __int8)radioSend((__int64)&sendBuffer_0, 0x14u) )
serialPrintV("radio send failed\n");
usleep(0x186A0u);
ledOff();
usleep(0xF4240u);
++sendBuffer_0;
}
}

canonical_decompiled_code:
void __noreturn func16()
{
while ( 1 )
{
if ( print_buf )
{
debugPrintfFormat("0x%04x: sending counter %i\n", localAddress, (unsigned __int8)sendBuffer_0);
serialPrint(print_buf);
}
func4();
if ( (unsigned __int8)func12((__int64)&sendBuffer_0, 0x14u) )
func0("radio send failed\n");
usleep(0x186A0u);
func6();
usleep(0xF4240u);
++sendBuffer_0;
}
}

original_code:
void sendCounter(void) {
    static uint8_t sendBuffer[20] = {
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        10, 11, 12, 13, 14, 15, 16, 17, 18, 19
    };
    uint8_t *counter = &sendBuffer[0];
    while (1) {
        if (3 == 1) { serialPrintV("0x%04x: sending counter %i\n", localAddress, *counter); } else { if (_print_buf) { debugPrintfFormat("0x%04x: sending counter %i\n", localAddress, *counter); serialPrint(_print_buf); }; };
        ledOn();
        int8_t result = radioSend(sendBuffer, sizeof(sendBuffer));
        if (result != 0) {
            if (1 == 1) { serialPrintV("radio send failed\n"); } else { if (_print_buf) { debugPrintfFormat("radio send failed\n"); serialPrint(_print_buf); }; };
        }
        usleep((100) * 1000);
        ledOff();
        usleep((1000) * 1000);
        ++(*counter);
    }
}

canonical_original_code:
void sendCounter() {
    static unsigned char sendBuffer[20] = {
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        10, 11, 12, 13, 14, 15, 16, 17, 18, 19
    };
    unsigned char *counter = &sendBuffer[0];
    while (1) {
        if (3 == 1) { serialPrintV("0x%04x: sending counter %i\n", localAddress, *counter); } else { if (_print_buf) { debugPrintfFormat("0x%04x: sending counter %i\n", localAddress, *counter); serialPrint(_print_buf); }; };
        ledOn();
        char result = radioSend(sendBuffer, sizeof(sendBuffer));
        if (result != 0) {
            if (1 == 1) { serialPrintV("radio send failed\n"); } else { if (_print_buf) { debugPrintfFormat("radio send failed\n"); serialPrint(_print_buf); }; };
        }
        usleep((100) * 1000);
        ledOff();
        usleep((1000) * 1000);
        ++(*counter);
    }
}

variable_types:
sendBuffer
unsigned char [20]

variable_types:
counter
unsigned char *

variable_types: result char


doMsleep

canonical_decompiled_code:
int __fastcall func0(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


ledsGlow

decompiled_code:
__int64 __fastcall ledsGlow(unsigned int a1)
{
unsigned int v2; // er12
int v3; // er14
int i; // ebx
__int64 v5; // rdi
__int64 result; // rax

v2 = ledold;
v3 = 32;
do
{
for ( i = 0; i != 32; ++i )
{
v5 = v2;
if ( 32 - v3 >= i )
v5 = a1;
ledsSet(v5);
LODWORD(result) = 100;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
}
--v3;
}
while ( v3 );
ledold = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
unsigned int v2; // er12
int v3; // er14
int i; // ebx
__int64 v5; // rdi
__int64 result; // rax

v2 = ledold;
v3 = 32;
do
{
for ( i = 0; i != 32; ++i )
{
v5 = v2;
if ( 32 - v3 >= i )
v5 = a1;
ledsSet(v5);
LODWORD(result) = 100;
do
result = (unsigned int)(result - 1);
while ( (_DWORD)result );
}
--v3;
}
while ( v3 );
ledold = a1;
return result;
}

original_code:
void ledsGlow(int lednew)
{
    int ledon, ledup, leddn;
    int i0, i1, ii, i3;
    ledup = lednew | ~ledold;
    leddn = ~lednew | ledold;
    ledon = lednew | ledold;
    for(i0=0; i0<32; i0++){
        for( i1=0; i1<32; i1++ ){
            ii = ledon;
            if(i1 <= i0) ii &= ledup;
            else ii &= leddn;
            ledsSet(ii);
            for(i3=0; i3<100; i3++);
        }
    }
    ledold = lednew;
}

canonical_original_code:
void ledsGlow(int lednew)
{
    int ledon; int ledup; int leddn;
    int i0; int i1; int ii; int i3;
    ledup = lednew | ~ledold;
    leddn = ~lednew | ledold;
    ledon = lednew | ledold;
    for(i0=0; i0<32; i0++){
        for( i1=0; i1<32; i1++ ){
            ii = ledon;
            if(i1 <= i0) ii &= ledup;
            else ii &= leddn;
            ledsSet(ii);
            for(i3=0; i3<100; i3++);
        }
    }
    ledold = lednew;
}

variable_types: lednew int

variable_types: ledon int

variable_types: ledup int

variable_types: leddn int

variable_types: ii int


sdStreamFindStart

decompiled_code:
__int64 __fastcall sdStreamFindStart(_DWORD *a1, unsigned __int16 a2, char a3)
{
char v4; // r14
_BYTE *v5; // rax
__int64 result; // rax
int v7; // er13

sdCardAddress = 0x40000;
v4 = 0;
while ( 1 )
{
sdcardRead((unsigned int)sdCardAddress, a1, a2);
if ( !a3 || (v7 = *a1, (unsigned __int16)crc16(a1 + 1, (unsigned __int16)(a2 - 4)) == HIWORD(v7)) )
{
v5 = a1;
if ( a2 )
{
while ( !*v5 )
{
if ( ++v5 == (char *)a1 + (unsigned __int16)(a2 - 1) + 1 )
goto LABEL_5;
}
v4 = 0;
goto LABEL_7;
}
}
LABEL_5:
if ( v4 )
break;
v4 = 1;
LABEL_7:
sdCardAddress += a2;
result = (unsigned int)sdCardAddress;
if ( (unsigned int)sdCardAddress > 0xFFFFFF )
return result;
}
result = a2;
sdCardAddress -= a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, unsigned __int16 a2, char a3)
{
char v4; // r14
_BYTE *v5; // rax
__int64 result; // rax
int v7; // er13

sdCardAddress = 0x40000;
v4 = 0;
while ( 1 )
{
sdcardRead((unsigned int)sdCardAddress, a1, a2);
if ( !a3 || (v7 = *a1, (unsigned __int16)crc16(a1 + 1, (unsigned __int16)(a2 - 4)) == HIWORD(v7)) )
{
v5 = a1;
if ( a2 )
{
while ( !*v5 )
{
if ( ++v5 == (char *)a1 + (unsigned __int16)(a2 - 1) + 1 )
goto LABEL_5;
}
v4 = 0;
goto LABEL_7;
}
}
LABEL_5:
if ( v4 )
break;
v4 = 1;
LABEL_7:
sdCardAddress += a2;
result = (unsigned int)sdCardAddress;
if ( (unsigned int)sdCardAddress > 0xFFFFFF )
return result;
}
result = a2;
sdCardAddress -= a2;
return result;
}

original_code:
static void sdStreamFindStart(void *buffer, uint16_t length, _Bool crc)
{
    _Bool oneInvalid = 0;
    sdCardAddress = (256 * 1024ul);
    while (sdCardAddress < (512 * (16ul * 1024 * 1024 / 512))) {
        sdcardRead(sdCardAddress, buffer, length);
        _Bool valid = 1;
        if (crc) {
            HeaderWithCrc_t headerWithCrc;
            memcpy(&headerWithCrc, buffer, sizeof(headerWithCrc));
            uint16_t calcCrc = crc16((uint8_t *)buffer + sizeof(headerWithCrc), length - sizeof(headerWithCrc));
            valid = headerWithCrc.crc == calcCrc;
        }
        if (valid) {
            uint16_t i;
            valid = 0;
            for (i = 0; i < length; ++i) {
                if (((uint8_t *)buffer)[i] != 0) {
                    valid = 1;
                    break;
                }
            }
        }
        if (valid) {
            oneInvalid = 0;
        } else {
            if (!oneInvalid) {
                oneInvalid = 1;
            } else {
                sdCardAddress -= length;
                break;
            }
        }
        sdCardAddress += length;
    }
}

canonical_original_code:
static void sdStreamFindStart(void *buffer, unsigned short length, _Bool crc)
{
    _Bool oneInvalid = 0;
    sdCardAddress = (256 * 1024ul);
    while (sdCardAddress < (512 * (16ul * 1024 * 1024 / 512))) {
        sdcardRead(sdCardAddress, buffer, length);
        _Bool valid = 1;
        if (crc) {
            struct HeaderWithCrc_s headerWithCrc;
            memcpy(&headerWithCrc, buffer, sizeof(headerWithCrc));
            unsigned short calcCrc = crc16((uint8_t *)buffer + sizeof(headerWithCrc), length - sizeof(headerWithCrc));
            valid = headerWithCrc.crc == calcCrc;
        }
        if (valid) {
            unsigned short i;
            valid = 0;
            for (i = 0; i < length; ++i) {
                if (((unsigned char *)buffer)[i] != 0) {
                    valid = 1;
                    break;
                }
            }
        }
        if (valid) {
            oneInvalid = 0;
        } else {
            if (!oneInvalid) {
                oneInvalid = 1;
            } else {
                sdCardAddress -= length;
                break;
            }
        }
        sdCardAddress += length;
    }
}

variable_types:
buffer
void *

variable_types:
length
unsigned short

variable_types: struct HeaderWithCrc_s { unsigned short __unused; unsigned short crc; unsigned char data[]; }

variable_types:
calcCrc
unsigned short

variable_types:
i
unsigned short


doMsleep

canonical_decompiled_code:
int __fastcall func1(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


sdStreamReset

decompiled_code:
void sdStreamReset()
{
sdCardAddress = 0;
}

canonical_decompiled_code:
void func2()
{
sdCardAddress = 0;
}

original_code:
void sdStreamReset(void)
{
    sdCardAddress = 0;
}


sdStreamWriteRecord

decompiled_code:
__int64 __fastcall sdStreamWriteRecord(__int64 a1, const char *a2, char a3)
{
unsigned __int16 v3; // bx
__int64 result; // rax

if ( (unsigned __int16)a2 > 0x28u )
{
a2 = "../../../src/hil/sdstream.c";
assertionFailed("length <= sizeof(tmpBuffer)", "../../../src/hil/sdstream.c", 110LL);
LABEL_9:
sdStreamFindStart(tmpBuffer, (unsigned __int16)a2, a3);
goto LABEL_4;
}
v3 = (unsigned __int16)a2;
result = 0LL;
if ( (serial & 1) != 0 )
return result;
if ( !sdCardAddress )
goto LABEL_9;
LABEL_4:
if ( a3 )
*(_WORD *)(a1 + 2) = crc16(a1 + 4, (unsigned __int16)(v3 - 4));
sdcardWrite((unsigned int)sdCardAddress, a1, v3);
sdcardFlush();
sdCardAddress += v3;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, char a3)
{
unsigned __int16 v3; // bx
__int64 result; // rax

if ( (unsigned __int16)a2 > 0x28u )
{
a2 = "../../../src/hil/sdstream.c";
assertionFailed("length <= sizeof(tmpBuffer)", "../../../src/hil/sdstream.c", 110LL);
LABEL_9:
func0(tmpBuffer, (unsigned __int16)a2, a3);
goto LABEL_4;
}
v3 = (unsigned __int16)a2;
result = 0LL;
if ( (serial & 1) != 0 )
return result;
if ( !sdCardAddress )
goto LABEL_9;
LABEL_4:
if ( a3 )
*(_WORD *)(a1 + 2) = crc16(a1 + 4, (unsigned __int16)(v3 - 4));
sdcardWrite((unsigned int)sdCardAddress, a1, v3);
sdcardFlush();
sdCardAddress += v3;
return 1LL;
}

original_code:
_Bool sdStreamWriteRecord(void *data, uint16_t length, _Bool crc)
{
    ((length <= sizeof(tmpBuffer)) ? (void)0 : assertionFailed("length <= sizeof(tmpBuffer)", "../../../src/hil/sdstream.c", 110));
    if (serial[0].busy) return 0;
    if (sdCardAddress == 0) {
        sdStreamFindStart(tmpBuffer, length, crc);
    }
    if (crc) {
        uint16_t calcCrc = crc16((uint8_t *)data + sizeof(HeaderWithCrc_t), length - sizeof(HeaderWithCrc_t));
        memcpy((uint8_t *)data + 2, &calcCrc, sizeof(calcCrc));
    }
    sdcardWrite(sdCardAddress, data, length);
    sdcardFlush();
    sdCardAddress += length;
    return 1;
}

canonical_original_code:
_Bool sdStreamWriteRecord(void *data, unsigned short length, _Bool crc)
{
    ((length <= sizeof(tmpBuffer)) ? (void)0 : assertionFailed("length <= sizeof(tmpBuffer)", "../../../src/hil/sdstream.c", 110));
    if (serial[0].busy) return 0;
    if (sdCardAddress == 0) {
        sdStreamFindStart(tmpBuffer, length, crc);
    }
    if (crc) {
        unsigned short calcCrc = crc16((uint8_t *)data + sizeof(HeaderWithCrc_t), length - sizeof(HeaderWithCrc_t));
        memcpy((unsigned char *)data + 2, &calcCrc, sizeof(calcCrc));
    }
    sdcardWrite(sdCardAddress, data, length);
    sdcardFlush();
    sdCardAddress += length;
    return 1;
}

variable_types:
data
void *


sdStreamReadRecord

decompiled_code:
bool __fastcall sdStreamReadRecord(int *a1, unsigned __int16 a2, char a3)
{
bool result; // al
int v5; // er12

if ( !sdCardAddress )
sdStreamFindStart(a1, a2, a3);
sdcardRead((unsigned int)sdCardAddress, a1, a2);
if ( a3 )
{
v5 = *a1;
result = (unsigned __int16)crc16(a1 + 1, (unsigned __int16)(a2 - 4)) == HIWORD(v5);
}
else
{
sdCardAddress += a2;
result = 1;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func4(int *a1, unsigned __int16 a2, char a3)
{
bool result; // al
int v5; // er12

if ( !sdCardAddress )
func0(a1, a2, a3);
sdcardRead((unsigned int)sdCardAddress, a1, a2);
if ( a3 )
{
v5 = *a1;
result = (unsigned __int16)crc16(a1 + 1, (unsigned __int16)(a2 - 4)) == HIWORD(v5);
}
else
{
sdCardAddress += a2;
result = 1;
}
return result;
}

original_code:
_Bool sdStreamReadRecord(void *data, uint16_t length, _Bool crc)
{
    if (sdCardAddress == 0) sdStreamFindStart(data, length, crc);
    sdcardRead(sdCardAddress, data, length);
    if (crc) {
        HeaderWithCrc_t headerWithCrc;
        memcpy(&headerWithCrc, data, sizeof(headerWithCrc));
        uint16_t calcCrc = crc16((uint8_t *)data + sizeof(headerWithCrc), length - sizeof(headerWithCrc));
        return headerWithCrc.crc == calcCrc;
    }
    sdCardAddress += length;
    return 1;
}

canonical_original_code:
_Bool sdStreamReadRecord(void *data, unsigned short length, _Bool crc)
{
    if (sdCardAddress == 0) sdStreamFindStart(data, length, crc);
    sdcardRead(sdCardAddress, data, length);
    if (crc) {
        struct HeaderWithCrc_s headerWithCrc;
        memcpy(&headerWithCrc, data, sizeof(headerWithCrc));
        unsigned short calcCrc = crc16((uint8_t *)data + sizeof(headerWithCrc), length - sizeof(headerWithCrc));
        return headerWithCrc.crc == calcCrc;
    }
    sdCardAddress += length;
    return 1;
}

user_defined_types: struct HeaderWithCrc_s { unsigned short __unused; unsigned short crc; unsigned char data[]; }


spiReadByte

decompiled_code:
__int64 __fastcall spiReadByte(unsigned __int8 a1)
{
return spiExchByte(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
return func1(a1, 0);
}

original_code:
static inline uint8_t spiReadByte(uint8_t busId) {
    return spiExchByte(busId, 0);
}

canonical_original_code:
static inline unsigned char spiReadByte(unsigned char busId) {
    return spiExchByte(busId, 0);
}

variable_types:
busId
unsigned char


doMsleep

decompiled_code:
int __fastcall doMsleep(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

original_code:
extern inline void doMsleep(uint16_t milliseconds) {
    usleep(milliseconds * 1000);
}

canonical_original_code:
extern inline void doMsleep(unsigned short milliseconds) {
    usleep(milliseconds * 1000);
}


spiWrite

decompiled_code:
void __fastcall spiWrite(__int64 a1, unsigned __int64 a2, unsigned __int16 a3)
{
unsigned __int64 v3; // rdx

v3 = a2 + a3;
if ( a2 < v3 )
{
do
++a2;
while ( v3 != a2 );
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, unsigned __int64 a2, unsigned __int16 a3)
{
unsigned __int64 v3; // rdx

v3 = a2 + a3;
if ( a2 < v3 )
{
do
++a2;
while ( v3 != a2 );
}
}

original_code:
void spiWrite(uint8_t busId, const void *buf_, uint16_t len) {
     uint8_t *buf = (uint8_t *) buf_;
     uint8_t *end = buf + len;
     while (buf < end) spiWriteByte(busId, *buf++);
}

canonical_original_code:
void spiWrite(unsigned char busId, void *buf_, unsigned short len) {
     unsigned char *buf = (uint8_t *) buf_;
     unsigned char *end = buf + len;
     while (buf < end) spiWriteByte(busId, *buf++);
}

variable_types:
len
unsigned short

variable_types:
buf
unsigned char *

variable_types:
end
unsigned char *


spiExchange

decompiled_code:
void __fastcall spiExchange(unsigned __int8 a1, unsigned __int8 *a2, unsigned __int16 a3)
{
unsigned __int8 *v3; // rbp
unsigned __int8 *v4; // rbx

v3 = &a2[a3];
if ( a2 < v3 )
{
v4 = a2;
do
{
*v4 = spiExchByte(a1, *v4);
++v4;
}
while ( v3 != v4 );
}
}

canonical_decompiled_code:
void __fastcall func6(unsigned __int8 a1, unsigned __int8 *a2, unsigned __int16 a3)
{
unsigned __int8 *v3; // rbp
unsigned __int8 *v4; // rbx

v3 = &a2[a3];
if ( a2 < v3 )
{
v4 = a2;
do
{
*v4 = func1(a1, *v4);
++v4;
}
while ( v3 != v4 );
}
}

original_code:
void spiExchange(uint8_t busId, void *buf_, uint16_t len) {
    uint8_t *buf = (uint8_t *) buf_;
    uint8_t *end = buf + len;
    while (buf < end) {
        *buf = spiExchByte(busId, *buf);
        buf++;
    }
}

canonical_original_code:
void spiExchange(unsigned char busId, void *buf_, unsigned short len) {
    unsigned char *buf = (uint8_t *) buf_;
    unsigned char *end = buf + len;
    while (buf < end) {
        *buf = spiExchByte(busId, *buf);
        buf++;
    }
}


spiReadAndDiscard

decompiled_code:
__int64 __fastcall spiReadAndDiscard(__int64 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // edx

result = a2;
if ( (_WORD)a2 )
{
v3 = 0;
do
++v3;
while ( (_WORD)a2 != (_WORD)v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // edx

result = a2;
if ( (_WORD)a2 )
{
v3 = 0;
do
++v3;
while ( (_WORD)a2 != (_WORD)v3 );
}
return result;
}

original_code:
uint8_t spiReadAndDiscard(uint8_t busId, uint16_t len) {
    uint16_t i;
    for (i = 0; i < len; ++i) {
        spiReadByte(busId);
    }
    return len;
}

canonical_original_code:
unsigned char spiReadAndDiscard(unsigned char busId, unsigned short len) {
    unsigned short i;
    for (i = 0; i < len; ++i) {
        spiReadByte(busId);
    }
    return len;
}


nullOpen

decompiled_code:
__int64 nullOpen()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
static _Bool nullOpen(void)
{
    return 1;
}

canonical_original_code:
static _Bool nullOpen()
{
    return 1;
}


nullWrite

decompiled_code:
__int64 __fastcall nullWrite(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
return a2;
}

original_code:
static ssize_t nullWrite(const void *buf, size_t count)
{
    (void)buf;
    return count;
}

canonical_original_code:
static long nullWrite(void *buf, unsigned long count)
{
    (void)buf;
    return count;
}

variable_types:
buf
void *

variable_types:
count
unsigned long

return_type: long


nullClose

decompiled_code:
__int64 nullClose()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 1LL;
}

original_code:
static _Bool nullClose(void)
{
    return 1;
}

canonical_original_code:
static _Bool nullClose()
{
    return 1;
}


nullRead

decompiled_code:
size_t __fastcall nullRead(void *a1, size_t a2)
{
memset(a1, 0, a2);
return a2;
}

canonical_decompiled_code:
size_t __fastcall func3(void *a1, size_t a2)
{
memset(a1, 0, a2);
return a2;
}

original_code:
static ssize_t nullRead(void *buf, size_t count)
{
    memset(buf, 0, count);
    return count;
}

canonical_original_code:
static long nullRead(void *buf, unsigned long count)
{
    memset(buf, 0, count);
    return count;
}


recvByte

decompiled_code:
void __fastcall recvByte(char a1)
{
__int64 v1; // rdx
__int16 v3; // di
unsigned __int16 v4; // di

v1 = packetBuffer;
if ( packetBuffer )
{
v3 = bytesReceived;
*(_BYTE *)(packetBuffer + (unsigned __int16)bytesReceived) = a1;
v4 = v3 + 1;
bytesReceived = v4;
if ( a1 == 10 || a1 == 13 || v4 == bufferSize )
{
*(_BYTE *)(v1 + v4) = 0;
packetHandler((unsigned __int8)v4);
bytesReceived = 0;
}
}
}

canonical_decompiled_code:
void __fastcall func0(char a1)
{
__int64 v1; // rdx
__int16 v3; // di
unsigned __int16 v4; // di

v1 = packetBuffer;
if ( packetBuffer )
{
v3 = bytesReceived;
*(_BYTE *)(packetBuffer + (unsigned __int16)bytesReceived) = a1;
v4 = v3 + 1;
bytesReceived = v4;
if ( a1 == 10 || a1 == 13 || v4 == bufferSize )
{
*(_BYTE *)(v1 + v4) = 0;
packetHandler((unsigned __int8)v4);
bytesReceived = 0;
}
}
}

original_code:
static void recvByte(uint8_t byte)
{
    if (packetBuffer) {
        packetBuffer[bytesReceived] = byte;
        ++bytesReceived;
        if (byte == '\n' || byte == '\r' || bytesReceived == bufferSize) {
            packetBuffer[bytesReceived] = 0;
            packetHandler(bytesReceived);
            bytesReceived = 0;
        }
    }
}

canonical_original_code:
static void recvByte(unsigned char byte)
{
    if (packetBuffer) {
        packetBuffer[bytesReceived] = byte;
        ++bytesReceived;
        if (byte == '\n' || byte == '\r' || bytesReceived == bufferSize) {
            packetBuffer[bytesReceived] = 0;
            packetHandler(bytesReceived);
            bytesReceived = 0;
        }
    }
}

variable_types:
byte
unsigned char


doMsleep

variable_types:
milliseconds
unsigned short


serialSetPacketReceiveHandle

decompiled_code:
__int64 __fastcall serialSetPacketReceiveHandle(char a1, __int64 (__fastcall *a2)(_QWORD), __int64 a3, __int16 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( !a1 )
{
if ( a2 && a3 )
{
serialEnableRX(0LL);
packetBuffer = a3;
bufferSize = a4 - 1;
packetHandler = a2;
bytesReceived = 0;
serialSetReceiveHandle(0LL, recvByte);
result = 0LL;
}
else
{
serialDisableRX(0LL);
packetBuffer = 0LL;
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, __int64 (__fastcall *a2)(_QWORD), __int64 a3, __int16 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( !a1 )
{
if ( a2 && a3 )
{
serialEnableRX(0LL);
packetBuffer = a3;
bufferSize = a4 - 1;
packetHandler = a2;
bytesReceived = 0;
serialSetReceiveHandle(0LL, recvByte);
result = 0LL;
}
else
{
serialDisableRX(0LL);
packetBuffer = 0LL;
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
uint_t serialSetPacketReceiveHandle(uint8_t id, SerialCallback_t cb,
                                    void *buffer, uint16_t len)
{
    if (id >= 1) return -1;
    if (!cb || !buffer) {
        serialDisableRX(id);
        packetBuffer = ((void *)0);
        return -1;
    }
    serialEnableRX(id);
    packetBuffer = (uint8_t *) buffer;
    bufferSize = len - 1;
    packetHandler = cb;
    bytesReceived = 0;
    serialSetReceiveHandle(id, recvByte);
    return 0;
}

canonical_original_code:
unsigned int serialSetPacketReceiveHandle(unsigned char id, void (*cb)(unsigned char), void *buffer, unsigned short len)
{
    if (id >= 1) return -1;
    if (!cb || !buffer) {
        serialDisableRX(id);
        packetBuffer = ((void *)0);
        return -1;
    }
    serialEnableRX(id);
    packetBuffer = (unsigned char *) buffer;
    bufferSize = len - 1;
    packetHandler = cb;
    bytesReceived = 0;
    serialSetReceiveHandle(id, recvByte);
    return 0;
}

variable_types:
id
unsigned char

variable_types:
cb
void (*)(unsigned char)

return_type: unsigned int


getPcTime

decompiled_code:
__int64 getPcTime()
{
int v0; // edx
__int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v0 = gettimeofday(&v2, 0LL);
result = 0LL;
if ( !v0 )
result = 1000 * (v2.tv_sec & 0xFFFFF) + (unsigned int)(v2.tv_usec / 1000);
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // edx
__int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v0 = gettimeofday(&v2, 0LL);
result = 0LL;
if ( !v0 )
result = 1000 * (v2.tv_sec & 0xFFFFF) + (unsigned int)(v2.tv_usec / 1000);
return result;
}

original_code:
static uint32_t getPcTime(void)
{
    struct timeval tv;
    if (gettimeofday(&tv, ((void *)0))) {
        return 0;
    } else {
        return (tv.tv_sec & 0x000fffff) * 1000 + tv.tv_usec / 1000;
    }
}

canonical_original_code:
static unsigned int getPcTime()
{
    struct timeval tv;
    if (gettimeofday(&tv, ((void *)0))) {
        return 0;
    } else {
        return (tv.tv_sec & 0x000fffff) * 1000 + tv.tv_usec / 1000;
    }
}

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


alarmIntHandler

decompiled_code:
void __noreturn alarmIntHandler()
{
int i; // ebp
int v1; // ebx

for ( i = getPcTime(); ; i = v1 )
{
usleep(0x2710u);
v1 = getPcTime();
jiffies = v1 + jiffies - i;
alarmsProcess();
}
}

canonical_decompiled_code:
void __noreturn func2()
{
int i; // ebp
int v1; // ebx

for ( i = func0(); ; i = v1 )
{
usleep(0x2710u);
v1 = func0();
jiffies = v1 + jiffies - i;
alarmsProcess();
}
}

original_code:
void *alarmIntHandler(void *dummy) {
    uint32_t lastTime = getPcTime();
    while (1) {
        usleep(10000);
        uint32_t now = getPcTime();
        jiffies += now - lastTime;
        lastTime = now;
        alarmsProcess();
    }
    return 0;
}

canonical_original_code:
void *alarmIntHandler(void *dummy) {
    unsigned int lastTime = getPcTime();
    while (1) {
        usleep(10000);
        unsigned int now = getPcTime();
        jiffies += now - lastTime;
        lastTime = now;
        alarmsProcess();
    }
    return 0;
}

variable_types:
dummy
void *

variable_types:
lastTime
unsigned int

variable_types:
now
unsigned int

return_type: void *


doMsleep

canonical_decompiled_code:
int __fastcall func1(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


eepromInit

decompiled_code:
int eepromInit()
{
int v0; // ebx
int v2; // eax

serialPrintV("Opening EEPROM image `eeprom'...\n");
v0 = open("eeprom", 0);
if ( v0 >= 0 )
return close(v0);
v2 = creat("eeprom", 0x1A4u);
v0 = v2;
if ( v2 > 0 )
{
if ( !ftruncate(v2, 256LL) )
return close(v0);
assertionFailed("ret == 0", "../../../src/arch/pc/eeprom_hal.c", 53LL);
}
assertionFailed("data > 0", "../../../src/arch/pc/eeprom_hal.c", 50LL);
return eepromRead();
}

canonical_decompiled_code:
int func2()
{
int v0; // ebx
int v2; // eax

func0("Opening EEPROM image `eeprom'...\n");
v0 = open("eeprom", 0);
if ( v0 >= 0 )
return close(v0);
v2 = creat("eeprom", 0x1A4u);
v0 = v2;
if ( v2 > 0 )
{
if ( !ftruncate(v2, 256LL) )
return close(v0);
assertionFailed("ret == 0", "../../../src/arch/pc/eeprom_hal.c", 53LL);
}
assertionFailed("data > 0", "../../../src/arch/pc/eeprom_hal.c", 50LL);
return func3();
}

original_code:
void eepromInit(void)
{
    if (1 == 1) { serialPrintV("Opening EEPROM image `" "eeprom" "'...\n"); } else { if (_print_buf) { debugPrintfFormat("Opening EEPROM image `" "eeprom" "'...\n"); serialPrint(_print_buf); }; };
    int data = open("eeprom", 00);
    if (data < 0) {
        data = creat("eeprom", 0644);
        ((data > 0) ? (void)0 : assertionFailed("data > 0", "../../../src/arch/pc/eeprom_hal.c", 50));
        int ret = ftruncate(data, 256);
        ((ret == 0) ? (void)0 : assertionFailed("ret == 0", "../../../src/arch/pc/eeprom_hal.c", 53));
    }
    close(data);
}

canonical_original_code:
void eepromInit()
{
    if (1 == 1) { serialPrintV("Opening EEPROM image `" "eeprom" "'...\n"); } else { if (_print_buf) { debugPrintfFormat("Opening EEPROM image `" "eeprom" "'...\n"); serialPrint(_print_buf); }; };
    int data = open("eeprom", 00);
    if (data < 0) {
        data = creat("eeprom", 0644);
        ((data > 0) ? (void)0 : assertionFailed("data > 0", "../../../src/arch/pc/eeprom_hal.c", 50));
        int ret = ftruncate(data, 256);
        ((ret == 0) ? (void)0 : assertionFailed("ret == 0", "../../../src/arch/pc/eeprom_hal.c", 53));
    }
    close(data);
}

variable_types: data int

variable_types: ret int


eepromRead

decompiled_code:
int __fastcall eepromRead(unsigned __int16 a1, void *a2, size_t a3)
{
int result; // eax
int v5; // ebx
int v6; // ebp
ssize_t v7; // rax

result = open("eeprom", 0);
if ( result >= 0 )
{
v5 = result;
if ( a3 + a1 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63LL);
}
else
{
v6 = lseek(result, a1, 0);
v7 = read(v5, a2, a3);
if ( !v6 && v7 == a3 )
return close(v5);
}
assertionFailed("ret == 0 && ret2 == len", "../../../src/arch/pc/eeprom_hal.c", 67LL);
result = eepromWrite();
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int16 a1, void *a2, size_t a3)
{
int result; // eax
int v5; // ebx
int v6; // ebp
ssize_t v7; // rax

result = open("eeprom", 0);
if ( result >= 0 )
{
v5 = result;
if ( a3 + a1 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63LL);
}
else
{
v6 = lseek(result, a1, 0);
v7 = read(v5, a2, a3);
if ( !v6 && v7 == a3 )
return close(v5);
}
assertionFailed("ret == 0 && ret2 == len", "../../../src/arch/pc/eeprom_hal.c", 67LL);
result = func4();
}
return result;
}

original_code:
void eepromRead(uint16_t addr, void *buf, size_t len)
{
    int data = open("eeprom", 00);
    if (data < 0) return;
    ((addr + len <= 256) ? (void)0 : assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63));
    int ret = lseek(data, addr, 0);
    size_t ret2 = read(data, buf, len);
    ((ret == 0 && ret2 == len) ? (void)0 : assertionFailed("ret == 0 && ret2 == len", "../../../src/arch/pc/eeprom_hal.c", 67));
    close(data);
}

canonical_original_code:
void eepromRead(unsigned short addr, void *buf, unsigned long len)
{
    int data = open("eeprom", 00);
    if (data < 0) return;
    ((addr + len <= 256) ? (void)0 : assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63));
    int ret = lseek(data, addr, 0);
    unsigned long ret2 = read(data, buf, len);
    ((ret == 0 && ret2 == len) ? (void)0 : assertionFailed("ret == 0 && ret2 == len", "../../../src/arch/pc/eeprom_hal.c", 67));
    close(data);
}

variable_types:
addr
unsigned short

variable_types:
len
unsigned long


eepromWrite

decompiled_code:
int __fastcall eepromWrite(unsigned __int16 a1, const void *a2, size_t a3)
{
int result; // eax
int v5; // ebp

if ( a3 + a1 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73LL);
JUMPOUT(0x1BDLL);
}
result = open("eeprom", 1);
v5 = result;
if ( result >= 0 )
{
lseek(result, a1, 0);
write(v5, a2, a3);
result = close(v5);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 a1, const void *a2, size_t a3)
{
int result; // eax
int v5; // ebp

if ( a3 + a1 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73LL);
JUMPOUT(0x1BDLL);
}
result = open("eeprom", 1);
v5 = result;
if ( result >= 0 )
{
lseek(result, a1, 0);
write(v5, a2, a3);
result = close(v5);
}
return result;
}

original_code:
void eepromWrite(uint16_t addr, const void *buf, size_t len)
{
    ((addr + len <= 256) ? (void)0 : assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73));
    int data = open("eeprom", 01);
    if (data < 0) return;
    lseek(data, addr, 0);
    int r = write(data, buf, len);
    (void) r;
    close(data);
}

canonical_original_code:
void eepromWrite(unsigned short addr, void *buf, unsigned long len)
{
    ((addr + len <= 256) ? (void)0 : assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73));
    int data = open("eeprom", 01);
    if (data < 0) return;
    lseek(data, addr, 0);
    int r = write(data, buf, len);
    (void) r;
    close(data);
}

variable_types: r int


onDcoAlarm

decompiled_code:
__int64 onDcoAlarm()
{
return alarmSchedule(&recalibrationAlarm, 60000LL);
}

canonical_decompiled_code:
__int64 func1()
{
return alarmSchedule(&recalibrationAlarm, 60000LL);
}

original_code:
static void onDcoAlarm(void *x)
{
    Handle_t handle;
    (handle) = 0;
    ;
    ((void) handle);
    ;
    alarmSchedule(&recalibrationAlarm, 60ul * 1000);
}

canonical_original_code:
static void onDcoAlarm(void *x)
{
    unsigned short handle;
    (handle) = 0;
    ;
    ((void) handle);
    ;
    alarmSchedule(&recalibrationAlarm, 60ul * 1000);
}

variable_types:
x
void *

variable_types:
handle
unsigned short


doMsleep

canonical_decompiled_code:
int __fastcall func2(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


dcoRecalibrationInit

decompiled_code:
__int64 dcoRecalibrationInit()
{
alarmInit((__int64)&recalibrationAlarm, (__int64)onDcoAlarm, 0LL);
return alarmSchedule(&recalibrationAlarm, 60000LL);
}

canonical_decompiled_code:
__int64 func3()
{
func0((__int64)&recalibrationAlarm, (__int64)onDcoAlarm, 0LL);
return alarmSchedule(&recalibrationAlarm, 60000LL);
}

original_code:
void dcoRecalibrationInit(void)
{
    alarmInit(&recalibrationAlarm, onDcoAlarm, ((void *)0));
    alarmSchedule(&recalibrationAlarm, 60ul * 1000);
}

canonical_original_code:
void dcoRecalibrationInit()
{
    alarmInit(&recalibrationAlarm, onDcoAlarm, ((void *)0));
    alarmSchedule(&recalibrationAlarm, 60ul * 1000);
}


decompiled_code:
__int64 __fastcall crc16Add(int a1, unsigned __int8 a2)
{
int v2; // edi
int v3; // eax
int v4; // edi
int v5; // edi

v2 = a1 ^ a2;
LOWORD(v2) = __ROL2__(v2, 8);
v3 = 16 * v2;
LOWORD(v3) = (16 * v2) & 0xF000;
v4 = v3 ^ v2;
HIWORD(v3) = HIWORD(v4);
LOWORD(v3) = (unsigned __int16)v4 >> 12;
v5 = v3 ^ v4;
HIWORD(v3) = HIWORD(v5);
LOWORD(v3) = ((unsigned __int16)v5 >> 5) & 0x7F8;
return v5 ^ (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned __int8 a2)
{
int v2; // edi
int v3; // eax
int v4; // edi
int v5; // edi

v2 = a1 ^ a2;
LOWORD(v2) = __ROL2__(v2, 8);
v3 = 16 * v2;
LOWORD(v3) = (16 * v2) & 0xF000;
v4 = v3 ^ v2;
HIWORD(v3) = HIWORD(v4);
LOWORD(v3) = (unsigned __int16)v4 >> 12;
v5 = v3 ^ v4;
HIWORD(v3) = HIWORD(v5);
LOWORD(v3) = ((unsigned __int16)v5 >> 5) & 0x7F8;
return v5 ^ (unsigned int)v3;
}

original_code:
static inline uint16_t crc16Add(uint16_t acc, uint8_t byte) {
    acc ^= byte;
    acc = (acc >> 8) | (acc << 8);
    acc ^= (acc & 0xff00) << 4;
    acc ^= (acc >> 8) >> 4;
    acc ^= (acc & 0xff00) >> 5;
    return acc;
}

canonical_original_code:
static inline unsigned short crc16Add(unsigned short acc, unsigned char byte) {
    acc ^= byte;
    acc = (acc >> 8) | (acc << 8);
    acc ^= (acc & 0xff00) << 4;
    acc ^= (acc >> 8) >> 4;
    acc ^= (acc & 0xff00) >> 5;
    return acc;
}

variable_types:
acc
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall crc8Add(int a1, int a2)
{
__int64 result; // rax
int v3; // ecx
int v4; // edx
unsigned int v5; // esi
bool v6; // zf

LODWORD(result) = a1 ^ a2;
v3 = 8;
do
{
v4 = result;
LOBYTE(v4) = (unsigned __int8)result >> 1;
v5 = result;
LOBYTE(v5) = (unsigned __int8)result >> 1;
v6 = (result & 1) == 0;
result = v4 ^ 0xFFFFFF8C;
if ( v6 )
result = v5;
--v3;
}
while ( v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 result; // rax
int v3; // ecx
int v4; // edx
unsigned int v5; // esi
bool v6; // zf

LODWORD(result) = a1 ^ a2;
v3 = 8;
do
{
v4 = result;
LOBYTE(v4) = (unsigned __int8)result >> 1;
v5 = result;
LOBYTE(v5) = (unsigned __int8)result >> 1;
v6 = (result & 1) == 0;
result = v4 ^ 0xFFFFFF8C;
if ( v6 )
result = v5;
--v3;
}
while ( v3 );
return result;
}

original_code:
static inline uint8_t crc8Add(uint8_t acc, uint8_t byte)
{
    int i;
    acc ^= byte;
    for (i = 0; i < 8; i++) {
        if (acc & 1) {
            acc = (acc >> 1) ^ 0x8c;
        } else {
            acc >>= 1;
        }
    }
    return acc;
}

canonical_original_code:
static inline unsigned char crc8Add(unsigned char acc, unsigned char byte)
{
    int i;
    acc ^= byte;
    for (i = 0; i < 8; i++) {
        if (acc & 1) {
            acc = (acc >> 1) ^ 0x8c;
        } else {
            acc >>= 1;
        }
    }
    return acc;
}

variable_types:
acc
unsigned char

variable_types: i int


decompiled_code:
__int64 __fastcall crc16(unsigned __int8 *a1, unsigned int a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !(_WORD)a2 )
return a2;
v2 = a1;
LOWORD(result) = 0;
do
result = crc16Add((unsigned __int16)result, *v2++);
while ( &a1[(unsigned __int16)a2] != v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1, unsigned int a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !(_WORD)a2 )
return a2;
v2 = a1;
LOWORD(result) = 0;
do
result = func0((unsigned __int16)result, *v2++);
while ( &a1[(unsigned __int16)a2] != v2 );
return result;
}

original_code:
uint16_t crc16(const uint8_t *data, uint16_t len) {
    uint16_t i, crc = 0;
    for (i = 0; i < len; ++i) {
        crc = crc16Add(crc, *data++);
    }
    return crc;
}

canonical_original_code:
unsigned short crc16(unsigned char *data, unsigned short len) {
    unsigned short i; unsigned short crc = 0;
    for (i = 0; i < len; ++i) {
        crc = crc16Add(crc, *data++);
    }
    return crc;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned short

variable_types:
i
unsigned short

variable_types:
crc
unsigned short


decompiled_code:
__int64 __fastcall crc8(unsigned __int8 *a1, unsigned __int16 a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !a2 )
return 0LL;
v2 = a1;
LOBYTE(result) = 0;
do
result = crc8Add((unsigned __int8)result, *v2++);
while ( &a1[a2] != v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, unsigned __int16 a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !a2 )
return 0LL;
v2 = a1;
LOBYTE(result) = 0;
do
result = func1((unsigned __int8)result, *v2++);
while ( &a1[a2] != v2 );
return result;
}

original_code:
uint8_t crc8(const uint8_t *data, uint16_t len) {
    uint16_t i;
    uint8_t crc = 0;
    for (i = 0; i < len; ++i) {
        crc = crc8Add(crc, *data++);
    }
    return crc;
}

canonical_original_code:
unsigned char crc8(unsigned char *data, unsigned short len) {
    unsigned short i;
    unsigned char crc = 0;
    for (i = 0; i < len; ++i) {
        crc = crc8Add(crc, *data++);
    }
    return crc;
}

variable_types:
crc
unsigned char


nmeaCharRecv

decompiled_code:
void __fastcall nmeaCharRecv(unsigned __int8 a1)
{
int v1; // edx
int v2; // eax
int v3; // er10
__int64 v4; // rax
char v5; // r9
int v6; // er8
int v7; // ecx
__int64 v8; // rdi
char v9; // al

v1 = readState;
switch ( readState )
{
case 2:
atLeastOneValid_1 = 0;
v3 = goodChars_2;
v4 = 0LL;
v5 = 0;
v6 = 0;
v7 = a1;
v8 = (unsigned int)goodChars_2;
do
{
if ( seemsValid[v4] )
{
if ( v7 == NMEACMD[3 * v4 + v8] )
{
v5 = 1;
v6 = 1;
}
else
{
seemsValid[v4] = 0;
}
}
++v4;
}
while ( v4 != 3 );
i = 3;
if ( v5 && (atLeastOneValid_1 = v6) != 0 )
{
if ( v3 != 2 )
{
goodChars_2 = v3 + 1;
return;
}
goodChars_2 = 0;
i = 0;
if ( seemsValid[0] )
{
v1 = 0;
}
else
{
if ( dword_384 )
{
v1 = 1;
}
else if ( !dword_388 )
{
i = 3;
return;
}
i = v1;
}
if ( nmeaBufState[v1] )
{
readState = 1;
}
else
{
readState = 3;
actualCmd_0 = v1;
bufPos = 0;
}
}
else
{
readState = 1;
goodChars_2 = 0;
}
break;
case 3:
if ( a1 == 42 )
{
nmeaBufState[actualCmd_0] = 1;
readState = 1;
goodChars_2 = 0;
}
else
{
v9 = bufPos;
if ( (unsigned __int8)bufPos > 0x4Eu )
{
if ( (unsigned __int8)bufPos <= 0x4Fu )
nmeaBuf[80 * actualCmd_0 + 79] = 0;
}
else
{
nmeaBuf[80 * actualCmd_0 + (unsigned __int8)bufPos] = a1;
}
bufPos = v9 + 1;
}
break;
case 1:
v2 = goodChars_2;
if ( a1 == NMEAHEAD[goodChars_2] )
{
++goodChars_2;
if ( v2 == 2 )
{
readState = 2;
goodChars_2 = 0;
seemsValid[0] = 1;
dword_384 = 1;
dword_388 = 1;
i = 3;
}
}
else
{
goodChars_2 = 0;
}
break;
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned __int8 a1)
{
int v1; // edx
int v2; // eax
int v3; // er10
__int64 v4; // rax
char v5; // r9
int v6; // er8
int v7; // ecx
__int64 v8; // rdi
char v9; // al

v1 = readState;
switch ( readState )
{
case 2:
atLeastOneValid_1 = 0;
v3 = goodChars_2;
v4 = 0LL;
v5 = 0;
v6 = 0;
v7 = a1;
v8 = (unsigned int)goodChars_2;
do
{
if ( seemsValid[v4] )
{
if ( v7 == NMEACMD[3 * v4 + v8] )
{
v5 = 1;
v6 = 1;
}
else
{
seemsValid[v4] = 0;
}
}
++v4;
}
while ( v4 != 3 );
i = 3;
if ( v5 && (atLeastOneValid_1 = v6) != 0 )
{
if ( v3 != 2 )
{
goodChars_2 = v3 + 1;
return;
}
goodChars_2 = 0;
i = 0;
if ( seemsValid[0] )
{
v1 = 0;
}
else
{
if ( dword_384 )
{
v1 = 1;
}
else if ( !dword_388 )
{
i = 3;
return;
}
i = v1;
}
if ( nmeaBufState[v1] )
{
readState = 1;
}
else
{
readState = 3;
actualCmd_0 = v1;
bufPos = 0;
}
}
else
{
readState = 1;
goodChars_2 = 0;
}
break;
case 3:
if ( a1 == 42 )
{
nmeaBufState[actualCmd_0] = 1;
readState = 1;
goodChars_2 = 0;
}
else
{
v9 = bufPos;
if ( (unsigned __int8)bufPos > 0x4Eu )
{
if ( (unsigned __int8)bufPos <= 0x4Fu )
nmeaBuf[80 * actualCmd_0 + 79] = 0;
}
else
{
nmeaBuf[80 * actualCmd_0 + (unsigned __int8)bufPos] = a1;
}
bufPos = v9 + 1;
}
break;
case 1:
v2 = goodChars_2;
if ( a1 == NMEAHEAD[goodChars_2] )
{
++goodChars_2;
if ( v2 == 2 )
{
readState = 2;
goodChars_2 = 0;
seemsValid[0] = 1;
dword_384 = 1;
dword_388 = 1;
i = 3;
}
}
else
{
goodChars_2 = 0;
}
break;
}
}

original_code:
void nmeaCharRecv(uint8_t b)
{
    static uint_t goodChars = 0;
    static uint_t atLeastOneValid = 0;
    static uint_t actualCmd = -1u;
    switch (readState) {
    case RS_WAIT_HEADER:
        if (b == NMEAHEAD[goodChars]) {
            if (++goodChars == sizeof(NMEAHEAD) - 1) {
                readState = RS_PARSE_CMD;
                goodChars = 0;
                for (i = 0; i < NMEA_CMD_COUNT; ++i) {
                    seemsValid[i] = 1;
                }
            } else {
            }
        } else {
            goodChars = 0;
        }
        break;
    case RS_PARSE_CMD:
        atLeastOneValid = 0;
        for (i = 0; i < NMEA_CMD_COUNT; ++i) {
            if (seemsValid[i]) {
                if (b == NMEACMD[i][goodChars]) {
                    atLeastOneValid = 1;
                } else {
                    seemsValid[i] = 0;
                }
            }
        }
        if (atLeastOneValid) {
            ++goodChars;
            if (goodChars == 3) {
                goodChars = 0;
                for (i = 0; i < NMEA_CMD_COUNT; ++i) {
                    if (seemsValid[i]) {
                        if (nmeaBufState[i] == BS_EMPTY) {
                            readState = RS_READ_CMD;
                            actualCmd = i;
                            bufPos = 0;
                        } else {
                            readState = RS_WAIT_HEADER;
                        }
                        break;
                    }
                }
            }
        } else {
            readState = RS_WAIT_HEADER;
            goodChars = 0;
        }
        break;
    case RS_READ_CMD:
        if (b == '*') {
            readState = RS_DONE;
            nmeaBufState[actualCmd] = BS_READY;
            readState = RS_WAIT_HEADER;
            goodChars = 0;
        } else {
            if (bufPos < MAX_NMEA_CMD_SIZE - 1) {
                nmeaBuf[actualCmd][bufPos] = b;
            } else if (bufPos < MAX_NMEA_CMD_SIZE) {
                nmeaBuf[actualCmd][bufPos] = 0;
            }
            ++bufPos;
        }
        break;
    default:
        break;
    }
}

canonical_original_code:
void nmeaCharRecv(unsigned char b)
{
    static unsigned int goodChars = 0;
    static unsigned int atLeastOneValid = 0;
    static unsigned int actualCmd = -1u;
    switch (readState) {
    case RS_WAIT_HEADER:
        if (b == NMEAHEAD[goodChars]) {
            if (++goodChars == sizeof(NMEAHEAD) - 1) {
                readState = RS_PARSE_CMD;
                goodChars = 0;
                for (i = 0; i < NMEA_CMD_COUNT; ++i) {
                    seemsValid[i] = 1;
                }
            } else {
            }
        } else {
            goodChars = 0;
        }
        break;
    case RS_PARSE_CMD:
        atLeastOneValid = 0;
        for (i = 0; i < NMEA_CMD_COUNT; ++i) {
            if (seemsValid[i]) {
                if (b == NMEACMD[i][goodChars]) {
                    atLeastOneValid = 1;
                } else {
                    seemsValid[i] = 0;
                }
            }
        }
        if (atLeastOneValid) {
            ++goodChars;
            if (goodChars == 3) {
                goodChars = 0;
                for (i = 0; i < NMEA_CMD_COUNT; ++i) {
                    if (seemsValid[i]) {
                        if (nmeaBufState[i] == BS_EMPTY) {
                            readState = RS_READ_CMD;
                            actualCmd = i;
                            bufPos = 0;
                        } else {
                            readState = RS_WAIT_HEADER;
                        }
                        break;
                    }
                }
            }
        } else {
            readState = RS_WAIT_HEADER;
            goodChars = 0;
        }
        break;
    case RS_READ_CMD:
        if (b == '*') {
            readState = RS_DONE;
            nmeaBufState[actualCmd] = BS_READY;
            readState = RS_WAIT_HEADER;
            goodChars = 0;
        } else {
            if (bufPos < MAX_NMEA_CMD_SIZE - 1) {
                nmeaBuf[actualCmd][bufPos] = b;
            } else if (bufPos < MAX_NMEA_CMD_SIZE) {
                nmeaBuf[actualCmd][bufPos] = 0;
            }
            ++bufPos;
        }
        break;
    default:
        break;
    }
}

variable_types:
b
unsigned char

variable_types:
goodChars
unsigned int

variable_types:
atLeastOneValid
unsigned int

variable_types:
actualCmd
unsigned int


redLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 0LL) )
result = pinClearPc(255LL, 0LL);
else
result = pinSetPc(255LL, 0LL);
return result;
}

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

if ( (unsigned int)pinReadPc(255LL, 0LL) )
result = pinClearPc(255LL, 0LL);
else
result = pinSetPc(255LL, 0LL);
return result;
}

original_code: static inline void redLedToggle(void) { led0Toggle(); }

canonical_original_code: static inline void redLedToggle() { led0Toggle(); }


blueLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 2LL) )
result = pinClearPc(255LL, 2LL);
else
result = pinSetPc(255LL, 2LL);
return result;
}

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

if ( (unsigned int)pinReadPc(255LL, 2LL) )
result = pinClearPc(255LL, 2LL);
else
result = pinSetPc(255LL, 2LL);
return result;
}

original_code: static inline void blueLedToggle(void) { led2Toggle(); }

canonical_original_code: static inline void blueLedToggle() { led2Toggle(); }


serialPrintV

decompiled_code:
__int64 serialPrintV()
{
return serialPrint();
}

canonical_decompiled_code:
__int64 func3()
{
return serialPrint();
}

original_code:
static inline void serialPrintV(const char* str, ...) {
    serialPrint(str);
}

canonical_original_code:
static inline void serialPrintV(char *str, ...) {
    serialPrint(str);
}

variable_types:
str
char *


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

serialPrintV("onTimer1\n");
redLedToggle();
alarmSchedule(&timer1, 700LL);
result = (unsigned int)(unsigned __int8)counter_0 + 1;
counter_0 = result;
if ( (_BYTE)result == 3 )
{
serialPrintV("reschedule alarm\n");
result = alarmSchedule(&timer2, 200LL);
}
return result;
}

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

func3("onTimer1\n");
func1();
alarmSchedule(&timer1, 700LL);
result = (unsigned int)(unsigned __int8)counter_0 + 1;
counter_0 = result;
if ( (_BYTE)result == 3 )
{
func3("reschedule alarm\n");
result = alarmSchedule(&timer2, 200LL);
}
return result;
}

original_code:
void onTimer1(void *x)
{
    static uint8_t counter;
    if (1 == 1) { serialPrintV("onTimer1\n"); } else { if (_print_buf) { debugPrintfFormat("onTimer1\n"); serialPrint(_print_buf); }; };
    redLedToggle();
    alarmSchedule(&timer1, 700);
    if (++counter == 3) {
        if (1 == 1) { serialPrintV("reschedule alarm\n"); } else { if (_print_buf) { debugPrintfFormat("reschedule alarm\n"); serialPrint(_print_buf); }; };
        alarmSchedule(&timer2, 200);
    }
}

canonical_original_code:
void onTimer1(void *x)
{
    static unsigned char counter;
    if (1 == 1) { serialPrintV("onTimer1\n"); } else { if (_print_buf) { debugPrintfFormat("onTimer1\n"); serialPrint(_print_buf); }; };
    redLedToggle();
    alarmSchedule(&timer1, 700);
    if (++counter == 3) {
        if (1 == 1) { serialPrintV("reschedule alarm\n"); } else { if (_print_buf) { debugPrintfFormat("reschedule alarm\n"); serialPrint(_print_buf); }; };
        alarmSchedule(&timer2, 200);
    }
}

variable_types:
counter
unsigned char


decompiled_code:
__int64 onTimer2()
{
serialPrintV("onTimer2\n");
return blueLedToggle();
}

canonical_decompiled_code:
__int64 func5()
{
func3("onTimer2\n");
return func2();
}

original_code:
void onTimer2(void *x)
{
    if (1 == 1) { serialPrintV("onTimer2\n"); } else { if (_print_buf) { debugPrintfFormat("onTimer2\n"); serialPrint(_print_buf); }; };
    blueLedToggle();
}


doMsleep

decompiled_code:
int __fastcall doMsleep(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

canonical_decompiled_code:
int __fastcall func6(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

original_code:
extern inline void doMsleep(uint16_t milliseconds) {
    usleep(milliseconds * 1000);
}

canonical_original_code:
extern inline void doMsleep(unsigned short milliseconds) {
    usleep(milliseconds * 1000);
}

variable_types:
milliseconds
unsigned short


redLedOn

decompiled_code:
__int64 redLedOn()
{
return pinSetPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func0()
{
return pinSetPc(255LL, 0LL);
}

original_code: static inline void redLedOn(void) { led0On(); }

canonical_original_code: static inline void redLedOn() { led0On(); }


greenLedOn

decompiled_code:
__int64 greenLedOn()
{
return pinSetPc(255LL, 1LL);
}

canonical_decompiled_code:
__int64 func1()
{
return pinSetPc(255LL, 1LL);
}

original_code: static inline void greenLedOn(void) { led1On(); }

canonical_original_code: static inline void greenLedOn() { led1On(); }


blueLedOn

decompiled_code:
__int64 blueLedOn()
{
return pinSetPc(255LL, 2LL);
}

canonical_decompiled_code:
__int64 func2()
{
return pinSetPc(255LL, 2LL);
}

original_code: static inline void blueLedOn(void) { led2On(); }

canonical_original_code: static inline void blueLedOn() { led2On(); }


redLedOff

decompiled_code:
__int64 redLedOff()
{
return pinClearPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func3()
{
return pinClearPc(255LL, 0LL);
}

original_code: static inline void redLedOff(void) { led0Off(); }

canonical_original_code: static inline void redLedOff() { led0Off(); }


greenLedOff

decompiled_code:
__int64 greenLedOff()
{
return pinClearPc(255LL, 1LL);
}

canonical_decompiled_code:
__int64 func4()
{
return pinClearPc(255LL, 1LL);
}

original_code: static inline void greenLedOff(void) { led1Off(); }

canonical_original_code: static inline void greenLedOff() { led1Off(); }


blueLedOff

decompiled_code:
__int64 blueLedOff()
{
return pinClearPc(255LL, 2LL);
}

canonical_decompiled_code:
__int64 func5()
{
return pinClearPc(255LL, 2LL);
}

original_code: static inline void blueLedOff(void) { led2Off(); }

canonical_original_code: static inline void blueLedOff() { led2Off(); }


redLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 0LL) )
result = pinClearPc(255LL, 0LL);
else
result = pinSetPc(255LL, 0LL);
return result;
}


greenLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 1LL) )
result = pinClearPc(255LL, 1LL);
else
result = pinSetPc(255LL, 1LL);
return result;
}

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

if ( (unsigned int)pinReadPc(255LL, 1LL) )
result = pinClearPc(255LL, 1LL);
else
result = pinSetPc(255LL, 1LL);
return result;
}

original_code: static inline void greenLedToggle(void) { led1Toggle(); }

canonical_original_code: static inline void greenLedToggle() { led1Toggle(); }


blueLedToggle

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

if ( (unsigned int)pinReadPc(255LL, 2LL) )
result = pinClearPc(255LL, 2LL);
else
result = pinSetPc(255LL, 2LL);
return result;
}


decompiled_code:
__int64 led0Get()
{
return pinReadPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func9()
{
return pinReadPc(255LL, 0LL);
}

original_code: static inline uint_t led0Get(void) { return (pinReadPc(255, 0) ^ (1-1)); }

canonical_original_code: static inline unsigned int led0Get() { return (pinReadPc(255, 0) ^ (1-1)); }


redLedGet

decompiled_code:
__int64 redLedGet()
{
return led0Get();
}

canonical_decompiled_code:
__int64 func10()
{
return func9();
}

original_code: static inline uint_t redLedGet(void) { return led0Get(); }

canonical_original_code: static inline unsigned int redLedGet() { return led0Get(); }


decompiled_code:
__int64 led1Get()
{
return pinReadPc(255LL, 1LL);
}

canonical_decompiled_code:
__int64 func11()
{
return pinReadPc(255LL, 1LL);
}

original_code: static inline uint_t led1Get(void) { return (pinReadPc(255, 1) ^ (1-1)); }

canonical_original_code: static inline unsigned int led1Get() { return (pinReadPc(255, 1) ^ (1-1)); }


greenLedGet

decompiled_code:
__int64 greenLedGet()
{
return led1Get();
}

canonical_decompiled_code:
__int64 func12()
{
return func11();
}

original_code: static inline uint_t greenLedGet(void) { return led1Get(); }

canonical_original_code: static inline unsigned int greenLedGet() { return led1Get(); }


decompiled_code:
__int64 led2Get()
{
return pinReadPc(255LL, 2LL);
}

canonical_decompiled_code:
__int64 func13()
{
return pinReadPc(255LL, 2LL);
}

original_code: static inline uint_t led2Get(void) { return (pinReadPc(255, 2) ^ (1-1)); }

canonical_original_code: static inline unsigned int led2Get() { return (pinReadPc(255, 2) ^ (1-1)); }


blueLedGet

decompiled_code:
__int64 blueLedGet()
{
return led2Get();
}

canonical_decompiled_code:
__int64 func14()
{
return func13();
}

original_code: static inline uint_t blueLedGet(void) { return led2Get(); }

canonical_original_code: static inline unsigned int blueLedGet() { return led2Get(); }


doMsleep

canonical_decompiled_code:
int __fastcall func15(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


ledOff

decompiled_code:
__int64 ledOff()
{
return pinClearPc("ing #%i\n", 0LL);
}

canonical_decompiled_code:
__int64 func0()
{
return pinClearPc("ing #%i\n", 0LL);
}

original_code: static inline void ledOff(void) { led0Off(); }

canonical_original_code: static inline void ledOff() { led0Off(); }


serialPacketReceived

decompiled_code:
__int64 __fastcall serialPacketReceived(unsigned __int8 a1)
{
unsigned __int8 v1; // bl
__int64 result; // rax

v1 = buf - 48;
result = ledsSet((unsigned __int8)(buf - 48));
if ( print_buf )
{
debugPrintfFormat("Pong %i %i\n", v1, a1);
result = serialPrint(print_buf);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1)
{
unsigned __int8 v1; // bl
__int64 result; // rax

v1 = buf - 48;
result = ledsSet((unsigned __int8)(buf - 48));
if ( print_buf )
{
debugPrintfFormat("Pong %i %i\n", v1, a1);
result = serialPrint(print_buf);
}
return result;
}

original_code:
void serialPacketReceived(uint8_t bytes) {
    uint8_t c = buf[0] - '0';
    ledsSet(c);
    if (3 == 1) { serialPrintV("Pong %i %i\n", c, bytes); } else { if (_print_buf) { debugPrintfFormat("Pong %i %i\n", c, bytes); serialPrint(_print_buf); }; };
}

canonical_original_code:
void serialPacketReceived(unsigned char bytes) {
    unsigned char c = buf[0] - '0';
    ledsSet(c);
    if (3 == 1) { serialPrintV("Pong %i %i\n", c, bytes); } else { if (_print_buf) { debugPrintfFormat("Pong %i %i\n", c, bytes); serialPrint(_print_buf); }; };
}

variable_types:
bytes
unsigned char

variable_types:
c
unsigned char


doMsleep

canonical_decompiled_code:
int __fastcall func2(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


getJiffies

decompiled_code:
__int64 getJiffies()
{
return jiffies;
}

canonical_decompiled_code:
__int64 func0()
{
return jiffies;
}

original_code:
static inline ticks_t getJiffies(void)
{
    Handle_t handle;
    (handle) = 0;
    ticks_t result = jiffies;
    ((void) handle);
    return result;
}

canonical_original_code:
static inline unsigned int getJiffies()
{
    unsigned short handle;
    (handle) = 0;
    unsigned int result = jiffies;
    ((void) handle);
    return result;
}

variable_types:
handle
unsigned short

variable_types:
result
unsigned int


getTimeMs

decompiled_code:
// attributes: thunk
__int64 getTimeMs()
{
return getJiffies();
}

canonical_decompiled_code:
__int64 func1()
{
return func0();
}

original_code:
static inline uint32_t getTimeMs(void) {
    return (uint32_t) getJiffies();
}

canonical_original_code:
static inline unsigned int getTimeMs() {
    return (unsigned int) getJiffies();
}


doMsleep

decompiled_code:
int __fastcall doMsleep(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

original_code:
extern inline void doMsleep(uint16_t milliseconds) {
    usleep(milliseconds * 1000);
}

canonical_original_code:
extern inline void doMsleep(unsigned short milliseconds) {
    usleep(milliseconds * 1000);
}


appMainx

decompiled_code:
void __noreturn appMainx()
{
unsigned int v0; // eax

while ( 1 )
{
v0 = getTimeMs();
if ( print_buf )
{
debugPrintfFormat("real time = %lu\n", v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

canonical_decompiled_code:
void __noreturn func3()
{
unsigned int v0; // eax

while ( 1 )
{
v0 = func1();
if ( print_buf )
{
debugPrintfFormat("real time = %lu\n", v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

original_code:
void appMainx(void)
{
    for (;;) {
        uint32_t t = getTimeMs();
        if (2 == 1) { serialPrintV("real time = %lu\n", t); } else { if (_print_buf) { debugPrintfFormat("real time = %lu\n", t); serialPrint(_print_buf); }; };
        usleep((1000) * 1000);
    }
}

canonical_original_code:
void appMainx()
{
    for (;;) {
        unsigned int t = getTimeMs();
        if (2 == 1) { serialPrintV("real time = %lu\n", t); } else { if (_print_buf) { debugPrintfFormat("real time = %lu\n", t); serialPrint(_print_buf); }; };
        usleep((1000) * 1000);
    }
}

variable_types:
t
unsigned int


appMainLong

decompiled_code:
void __noreturn appMainLong()
{
unsigned int v0; // eax

jiffies = -2001LL;
while ( 1 )
{
v0 = getTimeMs();
if ( print_buf )
{
debugPrintfFormat(
"real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
0LL,
0LL,
0LL,
0LL,
(unsigned __int64)v0 >> 24,
BYTE2(v0),
BYTE1(v0),
(unsigned __int8)v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

canonical_decompiled_code:
void __noreturn func5()
{
unsigned int v0; // eax

jiffies = -2001LL;
while ( 1 )
{
v0 = func1();
if ( print_buf )
{
debugPrintfFormat(
"real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
0LL,
0LL,
0LL,
0LL,
(unsigned __int64)v0 >> 24,
BYTE2(v0),
BYTE1(v0),
(unsigned __int8)v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

original_code:
void appMainLong(void)
{
    extern volatile ticks_t jiffies;
    jiffies = (0x7fffffffffffffffL * 2UL + 1UL) - 2000;
    for (;;) {
        ticks_t time = getTimeMs();
        uint8_t *t = (uint8_t *)&time;
        if (9 == 1) { serialPrintV("real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", t[7], t[6], t[5], t[4], t[3], t[2], t[1], t[0]); } else { if (_print_buf) { debugPrintfFormat("real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", t[7], t[6], t[5], t[4], t[3], t[2], t[1], t[0]); serialPrint(_print_buf); }; };
        usleep((1000) * 1000);
    }
}

canonical_original_code:
void appMainLong()
{
    extern unsigned long jiffies;
    jiffies = (0x7fffffffffffffffL * 2UL + 1UL) - 2000;
    for (;;) {
        unsigned long time = getTimeMs();
        unsigned char *t = (uint8_t *)&time;
        if (9 == 1) { serialPrintV("real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", t[7], t[6], t[5], t[4], t[3], t[2], t[1], t[0]); } else { if (_print_buf) { debugPrintfFormat("real time = 0x%02x%02x%02x%02x%02x%02x%02x%02x\n", t[7], t[6], t[5], t[4], t[3], t[2], t[1], t[0]); serialPrint(_print_buf); }; };
        usleep((1000) * 1000);
    }
}

variable_types:
jiffies
unsigned long

variable_types:
time
unsigned long

variable_types:
t
unsigned char *


appMainTimesave

decompiled_code:
void __noreturn appMainTimesave()
{
unsigned int v0; // eax

while ( 1 )
{
v0 = getTimeMs();
if ( print_buf )
{
debugPrintfFormat("real time = %lu\n", v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

canonical_decompiled_code:
void __noreturn func6()
{
unsigned int v0; // eax

while ( 1 )
{
v0 = func1();
if ( print_buf )
{
debugPrintfFormat("real time = %lu\n", v0);
serialPrint(print_buf);
}
usleep(0xF4240u);
}
}

original_code:
void appMainTimesave(void)
{
    for (;;) {
        uint32_t t = getTimeMs();
        if (2 == 1) { serialPrintV("real time = %lu\n", t); } else { if (_print_buf) { debugPrintfFormat("real time = %lu\n", t); serialPrint(_print_buf); }; };
        Handle_t handle;
        { const uint16_t _start_time = 0; (handle) = 0;
        usleep((1000) * 1000);
        if (handle != 0) { const uint16_t _time_diff = 0 - _start_time; if (_time_diff > PLATFORM_ALARM_TIMER_PERIOD) { jiffies += convertAlarmTimerToMs( _time_diff - PLATFORM_ALARM_TIMER_PERIOD); } } ((void) handle); };
    }
}

canonical_original_code:
void appMainTimesave()
{
    for (;;) {
        unsigned int t = getTimeMs();
        if (2 == 1) { serialPrintV("real time = %lu\n", t); } else { if (_print_buf) { debugPrintfFormat("real time = %lu\n", t); serialPrint(_print_buf); }; };
        unsigned short handle;
        { unsigned short _start_time = 0; (handle) = 0;
        usleep((1000) * 1000);
        if (handle != 0) { unsigned short _time_diff = 0 - _start_time; if (_time_diff > PLATFORM_ALARM_TIMER_PERIOD) { jiffies += convertAlarmTimerToMs( _time_diff - PLATFORM_ALARM_TIMER_PERIOD); } } ((void) handle); };
    }
}


tosSerialMsgEnc

decompiled_code:
__int64 __fastcall tosSerialMsgEnc(void *src, unsigned int a2, unsigned __int16 a3, char a4, _BYTE *a5, int a6, __int64 a7)
{
unsigned int v8; // ebx

v8 = 28;
if ( (unsigned __int16)a2 <= 0x1Cu )
v8 = a2;
if ( (unsigned __int16)a6 - 7 <= (unsigned __int16)v8 )
v8 = a6 - 8;
if ( (unsigned __int16)a6 > 8u && (_WORD)v8 )
{
*a5 = 0;
be16Write(a5 + 1, a3);
a5[3] = 0;
a5[4] = 0;
a5[5] = v8;
a5[6] = 0;
a5[7] = a4;
memcpy(a5 + 8, src, (unsigned __int16)v8);
if ( a7 )
*(_WORD *)a7 = v8 + 8;
}
else
{
v8 = 0;
if ( a7 )
*(_WORD *)a7 = 0;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *src, unsigned int a2, unsigned __int16 a3, char a4, _BYTE *a5, int a6, __int64 a7)
{
unsigned int v8; // ebx

v8 = 28;
if ( (unsigned __int16)a2 <= 0x1Cu )
v8 = a2;
if ( (unsigned __int16)a6 - 7 <= (unsigned __int16)v8 )
v8 = a6 - 8;
if ( (unsigned __int16)a6 > 8u && (_WORD)v8 )
{
*a5 = 0;
func0(a5 + 1, a3);
a5[3] = 0;
a5[4] = 0;
a5[5] = v8;
a5[6] = 0;
a5[7] = a4;
memcpy(a5 + 8, src, (unsigned __int16)v8);
if ( a7 )
*(_WORD *)a7 = v8 + 8;
}
else
{
v8 = 0;
if ( a7 )
*(_WORD *)a7 = 0;
}
return v8;
}

original_code:
uint16_t tosSerialMsgEnc(void *dataBuf, uint16_t dataSize,
        uint16_t dstAddr, uint8_t amType, void *resultBuf,
        uint16_t resultSize, uint16_t *bytesWritten) {
    uint16_t n = dataSize;
    if (n > MAX_TOS_MSG_PAYLOAD) n = MAX_TOS_MSG_PAYLOAD;
    if (n > resultSize - TOS_SERMSG_META_SIZE) n = resultSize - TOS_SERMSG_META_SIZE;
    if (resultSize < TOS_SERMSG_META_SIZE + 1) n = 0;
    if (n > 0) {
        TosSerialMsg_t *msg = (TosSerialMsg_t *) resultBuf;
        msg->zero = 0;
        be16Write(&msg->dstAddr, dstAddr);
        msg->srcAddr[0] = 0;
        msg->srcAddr[1] = 0;
        msg->msgLen = n;
        msg->group = 0;
        msg->amType = amType;
        memcpy(&msg->payload, dataBuf, n);
        if (bytesWritten) *bytesWritten = n + TOS_SERMSG_META_SIZE;
    } else {
        if (bytesWritten) *bytesWritten = 0;
    }
    return n;
}

canonical_original_code:
unsigned short tosSerialMsgEnc(void *dataBuf, unsigned short dataSize, unsigned short dstAddr, unsigned char amType, void *resultBuf, unsigned short resultSize, unsigned short *bytesWritten) {
    unsigned short n = dataSize;
    if (n > MAX_TOS_MSG_PAYLOAD) n = MAX_TOS_MSG_PAYLOAD;
    if (n > resultSize - TOS_SERMSG_META_SIZE) n = resultSize - TOS_SERMSG_META_SIZE;
    if (resultSize < TOS_SERMSG_META_SIZE + 1) n = 0;
    if (n > 0) {
        struct TosSerialMsg_t *msg = (TosSerialMsg_t *) resultBuf;
        msg->zero = 0;
        be16Write(&msg->dstAddr, dstAddr);
        msg->srcAddr[0] = 0;
        msg->srcAddr[1] = 0;
        msg->msgLen = n;
        msg->group = 0;
        msg->amType = amType;
        memcpy(&msg->payload, dataBuf, n);
        if (bytesWritten) *bytesWritten = n + TOS_SERMSG_META_SIZE;
    } else {
        if (bytesWritten) *bytesWritten = 0;
    }
    return n;
}

variable_types:
dataBuf
void *

variable_types:
dataSize
unsigned short

variable_types:
dstAddr
unsigned short

variable_types:
amType
unsigned char

variable_types:
resultBuf
void *

variable_types:
resultSize
unsigned short

variable_types:
bytesWritten
unsigned short *

variable_types:
n
unsigned short

variable_types: struct TosSerialMsg_t { unsigned char zero; unsigned char dstAddr[2]; unsigned char srcAddr[2]; unsigned char msgLen; unsigned char group; unsigned char amType; unsigned char payload; } *


serialPrintV

decompiled_code:
__int64 serialPrintV()
{
return serialPrint();
}

canonical_decompiled_code:
__int64 func2()
{
return serialPrint();
}

original_code:
static inline void serialPrintV(const char* str, ...) {
    serialPrint(str);
}

canonical_original_code:
static inline void serialPrintV(char *str, ...) {
    serialPrint(str);
}

variable_types:
str
char *


redLedOn

decompiled_code:
__int64 redLedOn()
{
return pinSetPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func3()
{
return pinSetPc(255LL, 0LL);
}

original_code: static inline void redLedOn(void) { led0On(); }

canonical_original_code: static inline void redLedOn() { led0On(); }


redLedOff

decompiled_code:
__int64 redLedOff()
{
return pinClearPc(255LL, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
return pinClearPc(255LL, 0LL);
}

original_code: static inline void redLedOff(void) { led0Off(); }

canonical_original_code: static inline void redLedOff() { led0Off(); }


doMsleep

canonical_decompiled_code:
int __fastcall func5(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

variable_types:
milliseconds
unsigned short