recvCallback

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

if ( a2 == 30 )
{
if ( (unsigned __int16)crc16(a1, 28LL) == a1[14] )
{
rssiSum += (unsigned __int8)radioGetLastRSSI();
lqiSum += (unsigned __int8)radioGetLastLQI();
v2 = *a1;
testId = a1[1];
if ( __PAIR32__(currentTestId, currentTestNumber) != __PAIR32__(testId, v2) )
{
currentTestId = testId;
if ( testInProgress )
endTest();
}
if ( !testInProgress )
startTest(v2);
++currentTestPacketsRx;
}
else
{
v3 = getJiffies();
if ( nextCrcErrorReportTime_1 - v3 < 0 )
{
serialPrintV("wrong CRC\n");
nextCrcErrorReportTime_1 = v3 + 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)
{
__int16 v2; // bp
int v3; // ebx

if ( a2 == 30 )
{
if ( (unsigned __int16)crc16(a1, 28LL) == a1[14] )
{
rssiSum += (unsigned __int8)func4();
lqiSum += (unsigned __int8)func5();
v2 = *a1;
testId = a1[1];
if ( __PAIR32__(currentTestId, currentTestNumber) != __PAIR32__(testId, v2) )
{
currentTestId = testId;
if ( testInProgress )
func11();
}
if ( !testInProgress )
func12(v2);
++currentTestPacketsRx;
}
else
{
v3 = func0();
if ( nextCrcErrorReportTime_1 - v3 < 0 )
{
func2("wrong CRC\n");
nextCrcErrorReportTime_1 = v3 + 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


repo: atiselsts/osw

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

v0 = radioRecv_constprop_0();
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 = func7();
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]

return_type: void


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

decompiled_code:
// attributes: thunk
__int64 radioOn()
{
return pcRadioOn();
}

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

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

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


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(); }


ledOn

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

canonical_decompiled_code:
__int64 func3()
{
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 func4()
{
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 func5()
{
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 func6()
{
__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

decompiled_code:
// attributes: thunk
__int64 radioGetLastRSSI()
{
return pcRadioGetLastRSSI();
}

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

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

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

return_type: char


doMsleep

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

canonical_decompiled_code:
int __fastcall func13(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


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_constprop_0() )
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);
}
func3();
if ( (unsigned __int8)func11() )
func0("radio send failed\n");
usleep(0x186A0u);
func5();
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)
{
int v2; // ebp
unsigned int v3; // er13
__int64 v4; // rdi
int v5; // ebx
__int64 result; // rax

v2 = 0;
v3 = ledold;
do
{
v4 = a1;
v5 = 0;
while ( 1 )
{
result = ledsSet(v4);
if ( ++v5 == 32 )
break;
v4 = v3;
if ( v5 <= v2 )
v4 = a1;
}
++v2;
}
while ( v2 != 32 );
ledold = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
int v2; // ebp
unsigned int v3; // er13
__int64 v4; // rdi
int v5; // ebx
__int64 result; // rax

v2 = 0;
v3 = ledold;
do
{
v4 = a1;
v5 = 0;
while ( 1 )
{
result = ledsSet(v4);
if ( ++v5 == 32 )
break;
v4 = v3;
if ( v5 <= v2 )
v4 = a1;
}
++v2;
}
while ( v2 != 32 );
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 v3; // r12
int v4; // er15
__int64 result; // rax
_BYTE *v6; // rax

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

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, unsigned __int16 a2, char a3)
{
char v3; // r12
int v4; // er15
__int64 result; // rax
_BYTE *v6; // rax

sdCardAddress = 0x40000;
v3 = 0;
while ( 1 )
{
sdcardRead((unsigned int)sdCardAddress, a1, a2);
if ( !a3 || (v4 = *a1, (unsigned __int16)crc16(a1 + 1, (unsigned __int16)(a2 - 4)) == HIWORD(v4)) )
{
v6 = a1;
if ( a2 )
{
while ( !*v6 )
{
if ( ++v6 == (char *)a1 + (unsigned __int16)(a2 - 1) + 1 )
goto LABEL_3;
}
v3 = 0;
goto LABEL_5;
}
}
LABEL_3:
if ( v3 )
break;
v3 = 1;
LABEL_5:
sdCardAddress += a2;
result = (unsigned int)sdCardAddress;
if ( (unsigned int)sdCardAddress > 0xFFFFFF )
return result;
}
result = sdCardAddress - (unsigned int)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, unsigned __int16 a2, char a3)
{
__int64 result; // rax

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

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

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

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
{
result = 1;
sdCardAddress += a2;
}
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
{
result = 1;
sdCardAddress += a2;
}
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(char a1)
{
return spiExchByte_isra_0(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
return func1(a1);
}

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

return_type: unsigned char


doMsleep

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


spiWrite

decompiled_code:
void spiWrite()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

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(char a1, unsigned __int64 a2, unsigned __int16 a3)
{
void *result; // rax
unsigned __int8 v4; // al
size_t v5; // rdx
void *v6; // r8

result = (void *)(a2 + a3);
if ( a2 < (unsigned __int64)result )
{
v4 = spiExchByte_isra_0(a1);
result = memset(v6, v4, v5);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(char a1, unsigned __int64 a2, unsigned __int16 a3)
{
void *result; // rax
unsigned __int8 v4; // al
size_t v5; // rdx
void *v6; // r8

result = (void *)(a2 + a3);
if ( a2 < (unsigned __int64)result )
{
v4 = func1(a1);
result = memset(v6, v4, v5);
}
return result;
}

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)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
return a2;
}

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:
// attributes: thunk
__int64 nullClose()
{
return nullOpen();
}

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

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
__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 + (unsigned __int8)v4) = 0;
packetHandler((unsigned __int8)v4);
bytesReceived = 0;
}
}
}

canonical_decompiled_code:
void __fastcall func0(char a1)
{
__int64 v1; // rdx
__int16 v3; // di
__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 + (unsigned __int8)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

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

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

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;
packetHandler = a2;
bufferSize = a4 - 1;
bytesReceived = 0;
serialSetReceiveHandle(0LL, recvByte);
result = 0LL;
}
else
{
serialDisableRX(0LL);
result = 0xFFFFFFFFLL;
packetBuffer = 0LL;
}
}
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;
packetHandler = a2;
bufferSize = a4 - 1;
bytesReceived = 0;
serialSetReceiveHandle(0LL, recvByte);
result = 0LL;
}
else
{
serialDisableRX(0LL);
result = 0xFFFFFFFFLL;
packetBuffer = 0LL;
}
}
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; // er8
__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; // er8
__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; }


doMsleep

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


alarmIntHandler

decompiled_code:
void __noreturn alarmIntHandler()
{
int v0; // ebp
int v1; // ebx
int v2; // eax

v0 = getPcTime();
while ( 1 )
{
usleep(0x2710u);
v1 = getPcTime();
v2 = jiffies - v0;
v0 = v1;
jiffies = v1 + v2;
alarmsProcess();
}
}

canonical_decompiled_code:
void __noreturn func2()
{
int v0; // ebp
int v1; // ebx
int v2; // eax

v0 = func0();
while ( 1 )
{
usleep(0x2710u);
v1 = func0();
v2 = jiffies - v0;
v0 = v1;
jiffies = v1 + v2;
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; // ebp
int v2; // eax

serialPrintV_constprop_0();
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; // ebp
int v2; // eax

func0();
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; // ebp
int v6; // ebx
ssize_t v7; // rax

result = open("eeprom", 0);
if ( result >= 0 )
{
v5 = result;
if ( a1 + a3 <= 0x100 )
{
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);
}
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63LL);
result = eepromWrite();
}
return result;
}

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

result = open("eeprom", 0);
if ( result >= 0 )
{
v5 = result;
if ( a1 + a3 <= 0x100 )
{
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);
}
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 63LL);
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 ( a1 + a3 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73LL);
JUMPOUT(0x1E8LL);
}
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 ( a1 + a3 > 0x100 )
{
assertionFailed("addr + len <= EEPROM_SIZE", "../../../src/arch/pc/eeprom_hal.c", 73LL);
JUMPOUT(0x1E8LL);
}
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 func0()
{
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_constprop_0();
return alarmSchedule(&recalibrationAlarm, 60000LL);
}

canonical_decompiled_code:
__int64 func3()
{
func1();
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:
unsigned __int8 __fastcall crc8Add(char a1, char a2)
{
int v2; // edx
unsigned __int8 result; // al
unsigned __int8 v4; // si

v2 = 8;
result = a1 ^ a2;
do
{
v4 = result;
result >>= 1;
if ( (v4 & 1) != 0 )
result ^= 0x8Cu;
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
unsigned __int8 __fastcall func1(char a1, char a2)
{
int v2; // edx
unsigned __int8 result; // al
unsigned __int8 v4; // si

v2 = 8;
result = a1 ^ a2;
do
{
v4 = result;
result >>= 1;
if ( (v4 & 1) != 0 )
result ^= 0x8Cu;
--v2;
}
while ( v2 );
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, __int16 a2)
{
__int64 result; // rax
unsigned __int8 *v4; // rcx

if ( !a2 )
return 0LL;
LOWORD(result) = 0;
do
result = crc16Add((unsigned __int16)result, *a1);
while ( v4 != a1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1, __int16 a2)
{
__int64 result; // rax
unsigned __int8 *v4; // rcx

if ( !a2 )
return 0LL;
LOWORD(result) = 0;
do
result = func0((unsigned __int16)result, *a1);
while ( v4 != a1 );
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:
unsigned __int8 __fastcall crc8(char *a1, __int16 a2)
{
unsigned __int8 result; // al
char *v4; // r9

if ( !a2 )
return 0;
result = 0;
do
result = crc8Add(result, *a1);
while ( v4 != a1 );
return result;
}

canonical_decompiled_code:
unsigned __int8 __fastcall func3(char *a1, __int16 a2)
{
unsigned __int8 result; // al
char *v4; // r9

if ( !a2 )
return 0;
result = 0;
do
result = func1(result, *a1);
while ( v4 != a1 );
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:
__int64 __fastcall nmeaCharRecv(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // edx
int v3; // eax
__int64 v4; // rsi
char v5; // r8
int v6; // edi
int v7; // er9

result = (unsigned int)readState;
v2 = a1;
if ( readState != 2 )
{
if ( readState == 3 )
{
if ( a1 == 42 )
{
readState = 1;
result = (unsigned int)actualCmd_0;
goodChars_2 = 0;
nmeaBufState[actualCmd_0] = 1;
}
else
{
v3 = (unsigned __int8)bufPos;
if ( (unsigned __int8)bufPos > 0x4Eu )
{
if ( bufPos == 79 )
nmeaBuf[80 * actualCmd_0 + 79] = 0;
}
else
{
nmeaBuf[80 * actualCmd_0 + (unsigned __int8)bufPos] = a1;
}
result = (unsigned int)(v3 + 1);
bufPos = result;
}
}
else if ( readState == 1 )
{
result = (unsigned int)goodChars_2;
if ( a1 == NMEAHEAD[goodChars_2] )
{
result = (unsigned int)(goodChars_2 + 1);
goodChars_2 = result;
if ( (_DWORD)result == 3 )
{
result = 0x100000001LL;
readState = 2;
goodChars_2 = 0;
seemsValid = 0x100000001LL;
dword_3C8 = 1;
i = 3;
}
}
else
{
goodChars_2 = 0;
}
}
return result;
}
v4 = (unsigned int)goodChars_2;
result = 0LL;
v5 = 0;
v6 = 0;
atLeastOneValid_1 = 0;
v7 = goodChars_2;
do
{
while ( 1 )
{
if ( !*((_DWORD *)&seemsValid + result) )
goto LABEL_13;
if ( v2 == NMEACMD[3 * result + v4] )
break;
*((_DWORD *)&seemsValid + result) = 0;
LABEL_13:
if ( ++result == 3 )
goto LABEL_17;
}
++result;
v5 = 1;
v6 = 1;
}
while ( result != 3 );
LABEL_17:
i = 3;
if ( !v5 || (atLeastOneValid_1 = v6) == 0 )
{
readState = 1;
goodChars_2 = 0;
return result;
}
result = (unsigned int)(v7 + 1);
if ( v7 != 2 )
{
goodChars_2 = v7 + 1;
return result;
}
goodChars_2 = 0;
i = 0;
if ( (_DWORD)seemsValid )
{
v6 = 0;
}
else
{
if ( !HIDWORD(seemsValid) )
{
if ( !dword_3C8 )
{
i = 3;
return result;
}
v6 = 2;
}
i = v6;
}
result = (unsigned int)nmeaBufState[v6];
if ( (_DWORD)result )
{
readState = 1;
}
else
{
readState = 3;
actualCmd_0 = v6;
bufPos = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
__int64 result; // rax
int v2; // edx
int v3; // eax
__int64 v4; // rsi
char v5; // r8
int v6; // edi
int v7; // er9

result = (unsigned int)readState;
v2 = a1;
if ( readState != 2 )
{
if ( readState == 3 )
{
if ( a1 == 42 )
{
readState = 1;
result = (unsigned int)actualCmd_0;
goodChars_2 = 0;
nmeaBufState[actualCmd_0] = 1;
}
else
{
v3 = (unsigned __int8)bufPos;
if ( (unsigned __int8)bufPos > 0x4Eu )
{
if ( bufPos == 79 )
nmeaBuf[80 * actualCmd_0 + 79] = 0;
}
else
{
nmeaBuf[80 * actualCmd_0 + (unsigned __int8)bufPos] = a1;
}
result = (unsigned int)(v3 + 1);
bufPos = result;
}
}
else if ( readState == 1 )
{
result = (unsigned int)goodChars_2;
if ( a1 == NMEAHEAD[goodChars_2] )
{
result = (unsigned int)(goodChars_2 + 1);
goodChars_2 = result;
if ( (_DWORD)result == 3 )
{
result = 0x100000001LL;
readState = 2;
goodChars_2 = 0;
seemsValid = 0x100000001LL;
dword_3C8 = 1;
i = 3;
}
}
else
{
goodChars_2 = 0;
}
}
return result;
}
v4 = (unsigned int)goodChars_2;
result = 0LL;
v5 = 0;
v6 = 0;
atLeastOneValid_1 = 0;
v7 = goodChars_2;
do
{
while ( 1 )
{
if ( !*((_DWORD *)&seemsValid + result) )
goto LABEL_13;
if ( v2 == NMEACMD[3 * result + v4] )
break;
*((_DWORD *)&seemsValid + result) = 0;
LABEL_13:
if ( ++result == 3 )
goto LABEL_17;
}
++result;
v5 = 1;
v6 = 1;
}
while ( result != 3 );
LABEL_17:
i = 3;
if ( !v5 || (atLeastOneValid_1 = v6) == 0 )
{
readState = 1;
goodChars_2 = 0;
return result;
}
result = (unsigned int)(v7 + 1);
if ( v7 != 2 )
{
goodChars_2 = v7 + 1;
return result;
}
goodChars_2 = 0;
i = 0;
if ( (_DWORD)seemsValid )
{
v6 = 0;
}
else
{
if ( !HIDWORD(seemsValid) )
{
if ( !dword_3C8 )
{
i = 3;
return result;
}
v6 = 2;
}
i = v6;
}
result = (unsigned int)nmeaBufState[v6];
if ( (_DWORD)result )
{
readState = 1;
}
else
{
readState = 3;
actualCmd_0 = v6;
bufPos = 0;
}
return result;
}

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 func0()
{
__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 func1()
{
__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 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 *


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 func3()
{
__int64 result; // rax

func2("onTimer1\n");
func0();
alarmSchedule(&timer1, 700LL);
result = (unsigned int)(unsigned __int8)counter_0 + 1;
counter_0 = result;
if ( (_BYTE)result == 3 )
{
func2("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 func4()
{
func2("onTimer2\n");
return func1();
}

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

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:
// attributes: thunk
__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:
// attributes: thunk
__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:
// attributes: thunk
__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);
}

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


ledOff

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

canonical_decompiled_code:
__int64 func0()
{
return pinClearPc(255LL, 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 int v1; // ebp
__int64 result; // rax

v1 = (unsigned __int8)(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 int v1; // ebp
__int64 result; // rax

v1 = (unsigned __int8)(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);
}


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()
{
appMainx();
}

canonical_decompiled_code:
void __noreturn func6()
{
func3();
}

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

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


fatFsFormat

decompiled_code:
__int64 fatFsFormat()
{
unsigned int v0; // eax
unsigned int v1; // er12
int v2; // ebx
unsigned __int16 v3; // ax
unsigned int v4; // er12

redLedOn();
v0 = sdcardGetSize();
v1 = v0 - 2048;
if ( print_buf )
{
debugPrintfFormat("cardSizeInBlocks = %lu\n", v0);
serialPrint(print_buf);
}
sdcardBulkErase();
if ( (unsigned __int8)sdcardReadBlock(0LL, &buffer) )
{
qword_51E = 0x8300000000LL;
dword_526 = 2048;
dword_52A = v1;
word_55E = -21931;
if ( (unsigned __int8)sdcardWriteBlock(0LL, &buffer) )
{
if ( !(unsigned __int8)sdcardReadBlock(0x100000LL, &buffer) )
goto LABEL_15;
BYTE2(buffer) = -112;
LOWORD(buffer) = 15595;
*(_QWORD *)((char *)&buffer + 3) = 7828335LL;
le16Write(&word_36B, 512);
byte_36D = 64;
le16Write(&word_36E, 64);
byte_370 = 2;
le16Write(&word_371, 1024);
if ( v1 <= 0xFFFF )
le16Write(&word_373, v1);
else
le16Write(&word_373, 0);
byte_375 = -8;
le16Write(&word_376, 256);
le16Write(&word_378, 0);
le16Write(&word_37A, 0);
le32Write(&dword_37C, 0);
le32Write(&dword_380, v1);
le16Write(&word_384, 0);
byte_386 = 41;
v2 = randomNumber();
v3 = randomNumber();
le32Write(&dword_387, (v2 << 16) + v3);
strcpy((char *)&qword_38B, " ");
byte_395 = 109;
qword_396 = 0x2020203631544146LL;
word_55E = -21931;
if ( !(unsigned __int8)sdcardWriteBlock(0x100000LL, &buffer) )
goto LABEL_15;
if ( (unsigned __int8)sdcardReadBlock(1081344LL, &buffer)
&& (buffer = -8, (unsigned __int8)sdcardWriteBlock(1081344LL, &buffer))
&& (unsigned __int8)sdcardReadBlock(1212416LL, &buffer) )
{
buffer = -8;
v4 = -((unsigned __int8)sdcardWriteBlock(1212416LL, &buffer) ^ 1);
}
else
{
LABEL_15:
v4 = -1;
}
}
else
{
v4 = -1;
serialPrintV("write block failed!\n");
}
}
else
{
v4 = -1;
serialPrintV("read block failed!\n");
}
redLedOff();
return v4;
}

canonical_decompiled_code:
__int64 func6()
{
unsigned int v0; // eax
unsigned int v1; // er12
int v2; // ebx
unsigned __int16 v3; // ax
unsigned int v4; // er12

func3();
v0 = sdcardGetSize();
v1 = v0 - 2048;
if ( print_buf )
{
debugPrintfFormat("cardSizeInBlocks = %lu\n", v0);
serialPrint(print_buf);
}
sdcardBulkErase();
if ( (unsigned __int8)sdcardReadBlock(0LL, &buffer) )
{
qword_51E = 0x8300000000LL;
dword_526 = 2048;
dword_52A = v1;
word_55E = -21931;
if ( (unsigned __int8)sdcardWriteBlock(0LL, &buffer) )
{
if ( !(unsigned __int8)sdcardReadBlock(0x100000LL, &buffer) )
goto LABEL_15;
BYTE2(buffer) = -112;
LOWORD(buffer) = 15595;
*(_QWORD *)((char *)&buffer + 3) = 7828335LL;
func0(&word_36B, 512);
byte_36D = 64;
func0(&word_36E, 64);
byte_370 = 2;
func0(&word_371, 1024);
if ( v1 <= 0xFFFF )
func0(&word_373, v1);
else
func0(&word_373, 0);
byte_375 = -8;
func0(&word_376, 256);
func0(&word_378, 0);
func0(&word_37A, 0);
func1(&dword_37C, 0);
func1(&dword_380, v1);
func0(&word_384, 0);
byte_386 = 41;
v2 = randomNumber();
v3 = randomNumber();
func1(&dword_387, (v2 << 16) + v3);
strcpy((char *)&qword_38B, " ");
byte_395 = 109;
qword_396 = 0x2020203631544146LL;
word_55E = -21931;
if ( !(unsigned __int8)sdcardWriteBlock(0x100000LL, &buffer) )
goto LABEL_15;
if ( (unsigned __int8)sdcardReadBlock(1081344LL, &buffer)
&& (buffer = -8, (unsigned __int8)sdcardWriteBlock(1081344LL, &buffer))
&& (unsigned __int8)sdcardReadBlock(1212416LL, &buffer) )
{
buffer = -8;
v4 = -((unsigned __int8)sdcardWriteBlock(1212416LL, &buffer) ^ 1);
}
else
{
LABEL_15:
v4 = -1;
}
}
else
{
v4 = -1;
func2("write block failed!\n");
}
}
else
{
v4 = -1;
func2("read block failed!\n");
}
func4();
return v4;
}

original_code:
int fatFsFormat(void)
{
    MasterBootRecord_t *mbr;
    FatBootBlock_t *fbs;
    int ret = -1;
    redLedOn();
    const uint32_t cardSizeInBlocks = sdcardGetSize();
    const uint32_t partitionSizeInBlocks = cardSizeInBlocks - 2048ul;
    if (2 == 1) { serialPrintV("cardSizeInBlocks = %lu\n", cardSizeInBlocks); } else { if (_print_buf) { debugPrintfFormat("cardSizeInBlocks = %lu\n", cardSizeInBlocks); serialPrint(_print_buf); }; };
    sdcardBulkErase();
    if (!sdcardReadBlock(0, buffer)) {
        if (1 == 1) { serialPrintV("read block failed!\n"); } else { if (_print_buf) { debugPrintfFormat("read block failed!\n"); serialPrint(_print_buf); }; };
        goto fail;
    }
    mbr = (MasterBootRecord_t *) buffer;
    mbr->part[0].boot = 0;
    mbr->part[0].beginHead = 0;
    mbr->part[0].beginSector = 0;
    mbr->part[0].beginCylinderHigh = 0;
    mbr->part[0].beginCylinderLow = 0;
    mbr->part[0].type = 0x83;
    mbr->part[0].endHead = 0;
    mbr->part[0].endSector = 0;
    mbr->part[0].endCylinderHigh = 0;
    mbr->part[0].endCylinderLow = 0;
    mbr->part[0].firstSector = 2048ul;
    mbr->part[0].totalSectors = partitionSizeInBlocks;
    mbr->mbrSig0 = 0x55;
    mbr->mbrSig1 = 0xAA;
    if (!sdcardWriteBlock(0, buffer)) {
        if (1 == 1) { serialPrintV("write block failed!\n"); } else { if (_print_buf) { debugPrintfFormat("write block failed!\n"); serialPrint(_print_buf); }; };
        goto fail;
    }
    if (!sdcardReadBlock((2048ul * 512), buffer)) goto fail;
    fbs = (FatBootBlock_t *) buffer;
    fbs->bootstrapProg[0] = 0xEB;
    fbs->bootstrapProg[1] = 0x3C;
    fbs->bootstrapProg[2] = 0x90;
    memcpy(fbs->oemDescription, "osw\0\0\0\0\0", 8);
    le16Write(fbs->bytesPerSector, 512);
    fbs->sectorsPerCluster = 64;
    le16Write(fbs->numReservedSectors, 64);
    fbs->numFATs = 2;
    le16Write(fbs->numRootEntries, 1024);
    if (partitionSizeInBlocks < 0x10000) {
        le16Write(fbs->totalSectors16, partitionSizeInBlocks);
    } else {
        le16Write(fbs->totalSectors16, 0);
    }
    fbs->mediaDescriptor = 0xf8;
    le16Write(fbs->sectorsPerFAT16, 256);
    le16Write(fbs->sectorsPerTrack, 0x0);
    le16Write(fbs->numHeads, 0x0);
    le32Write(fbs->numHiddenBlocks, 0ul);
    le32Write(fbs->totalSectors32, partitionSizeInBlocks);
    le16Write(fbs->physicalDriveNumber, 0);
    fbs->bootRecordSignature = 0x29;
    le32Write(fbs->volumeSerialNumber, ((uint32_t)randomNumber() << 16) + randomNumber());
    memcpy(fbs->volumeLabel, "         ", 11);
    memcpy(fbs->fileSystemIdentifier, "FAT16   ", 8);
    fbs->bootSectorSig0 = 0x55;
    fbs->bootSectorSig1 = 0xAA;
    if (!sdcardWriteBlock((2048ul * 512), buffer)) goto fail;
    if (!sdcardReadBlock(((2048ul + 64) * 512), buffer)) goto fail;
    buffer[0] = 0xf8;
    buffer[1] = 0xff;
    buffer[2] = 0xff;
    buffer[3] = 0xff;
    if (!sdcardWriteBlock(((2048ul + 64) * 512), buffer)) goto fail;
    if (!sdcardReadBlock(((2048ul + 64 + 256) * 512), buffer)) goto fail;
    buffer[0] = 0xf8;
    buffer[1] = 0xff;
    buffer[2] = 0xff;
    buffer[3] = 0xff;
    if (!sdcardWriteBlock(((2048ul + 64 + 256) * 512), buffer)) goto fail;
    ret = 0;
  fail:
    redLedOff();
    return ret;
}

canonical_original_code:
int fatFsFormat()
{
    struct MasterBootRecord_s *mbr;
    struct FatBootBlock_s *fbs;
    int ret = -1;
    redLedOn();
    unsigned int cardSizeInBlocks = sdcardGetSize();
    unsigned int partitionSizeInBlocks = cardSizeInBlocks - 2048ul;
    if (2 == 1) { serialPrintV("cardSizeInBlocks = %lu\n", cardSizeInBlocks); } else { if (_print_buf) { debugPrintfFormat("cardSizeInBlocks = %lu\n", cardSizeInBlocks); serialPrint(_print_buf); }; };
    sdcardBulkErase();
    if (!sdcardReadBlock(0, buffer)) {
        if (1 == 1) { serialPrintV("read block failed!\n"); } else { if (_print_buf) { debugPrintfFormat("read block failed!\n"); serialPrint(_print_buf); }; };
        goto fail;
    }
    mbr = (struct MasterBootRecord_s *) buffer;
    mbr->part[0].boot = 0;
    mbr->part[0].beginHead = 0;
    mbr->part[0].beginSector = 0;
    mbr->part[0].beginCylinderHigh = 0;
    mbr->part[0].beginCylinderLow = 0;
    mbr->part[0].type = 0x83;
    mbr->part[0].endHead = 0;
    mbr->part[0].endSector = 0;
    mbr->part[0].endCylinderHigh = 0;
    mbr->part[0].endCylinderLow = 0;
    mbr->part[0].firstSector = 2048ul;
    mbr->part[0].totalSectors = partitionSizeInBlocks;
    mbr->mbrSig0 = 0x55;
    mbr->mbrSig1 = 0xAA;
    if (!sdcardWriteBlock(0, buffer)) {
        if (1 == 1) { serialPrintV("write block failed!\n"); } else { if (_print_buf) { debugPrintfFormat("write block failed!\n"); serialPrint(_print_buf); }; };
        goto fail;
    }
    if (!sdcardReadBlock((2048ul * 512), buffer)) goto fail;
    fbs = (struct FatBootBlock_s *) buffer;
    fbs->bootstrapProg[0] = 0xEB;
    fbs->bootstrapProg[1] = 0x3C;
    fbs->bootstrapProg[2] = 0x90;
    memcpy(fbs->oemDescription, "osw\0\0\0\0\0", 8);
    le16Write(fbs->bytesPerSector, 512);
    fbs->sectorsPerCluster = 64;
    le16Write(fbs->numReservedSectors, 64);
    fbs->numFATs = 2;
    le16Write(fbs->numRootEntries, 1024);
    if (partitionSizeInBlocks < 0x10000) {
        le16Write(fbs->totalSectors16, partitionSizeInBlocks);
    } else {
        le16Write(fbs->totalSectors16, 0);
    }
    fbs->mediaDescriptor = 0xf8;
    le16Write(fbs->sectorsPerFAT16, 256);
    le16Write(fbs->sectorsPerTrack, 0x0);
    le16Write(fbs->numHeads, 0x0);
    le32Write(fbs->numHiddenBlocks, 0ul);
    le32Write(fbs->totalSectors32, partitionSizeInBlocks);
    le16Write(fbs->physicalDriveNumber, 0);
    fbs->bootRecordSignature = 0x29;
    le32Write(fbs->volumeSerialNumber, ((unsigned int)randomNumber() << 16) + randomNumber());
    memcpy(fbs->volumeLabel, "         ", 11);
    memcpy(fbs->fileSystemIdentifier, "FAT16   ", 8);
    fbs->bootSectorSig0 = 0x55;
    fbs->bootSectorSig1 = 0xAA;
    if (!sdcardWriteBlock((2048ul * 512), buffer)) goto fail;
    if (!sdcardReadBlock(((2048ul + 64) * 512), buffer)) goto fail;
    buffer[0] = 0xf8;
    buffer[1] = 0xff;
    buffer[2] = 0xff;
    buffer[3] = 0xff;
    if (!sdcardWriteBlock(((2048ul + 64) * 512), buffer)) goto fail;
    if (!sdcardReadBlock(((2048ul + 64 + 256) * 512), buffer)) goto fail;
    buffer[0] = 0xf8;
    buffer[1] = 0xff;
    buffer[2] = 0xff;
    buffer[3] = 0xff;
    if (!sdcardWriteBlock(((2048ul + 64 + 256) * 512), buffer)) goto fail;
    ret = 0;
  fail:
    redLedOff();
    return ret;
}

variable_types: struct MasterBootRecord_s { unsigned char codeArea[440]; unsigned int diskSignature; unsigned short usuallyZero; struct PartitionTable_s { unsigned char boot; unsigned char beginHead; unsigned int beginSector; unsigned int beginCylinderHigh; unsigned char beginCylinderLow; unsigned char type; unsigned char endHead; unsigned int endSector; unsigned int endCylinderHigh; unsigned char endCylinderLow; unsigned int firstSector; unsigned int totalSectors; } part[4]; unsigned char mbrSig0; unsigned char mbrSig1; } *

variable_types: struct FatBootBlock_s { unsigned char bootstrapProg[3]; unsigned char oemDescription[8]; unsigned char bytesPerSector[2]; unsigned char sectorsPerCluster; unsigned char numReservedSectors[2]; unsigned char numFATs; unsigned char numRootEntries[2]; unsigned char totalSectors16[2]; unsigned char mediaDescriptor; unsigned char sectorsPerFAT16[2]; unsigned char sectorsPerTrack[2]; unsigned char numHeads[2]; unsigned char numHiddenBlocks[4]; unsigned char totalSectors32[4]; unsigned char physicalDriveNumber[2]; unsigned char bootRecordSignature; unsigned char volumeSerialNumber[4]; unsigned char volumeLabel[11]; unsigned char fileSystemIdentifier[8]; unsigned char bootCode[448]; unsigned char bootSectorSig0; unsigned char bootSectorSig1; } *

variable_types: ret int

variable_types:
cardSizeInBlocks
unsigned int

variable_types:
partitionSizeInBlocks
unsigned int

return_type: int

user_defined_types: struct MasterBootRecord_s { unsigned char codeArea[440]; unsigned int diskSignature; unsigned short usuallyZero; struct PartitionTable_s part[4]; unsigned char mbrSig0; unsigned char mbrSig1; }


getJiffies

variable_types:
handle
unsigned short

variable_types:
result
unsigned int


ledToggle

decompiled_code:
__int64 ledToggle()
{
__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 ledToggle(void) { led0Toggle(); }

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


redLedOn

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


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

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


blueLedOff

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

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

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

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


doMsleep

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


test

decompiled_code:
__int64 test()
{
redLedOn();
redLedOff();
blueLedOn();
debugHexdump(&buffer, 0LL);
serialPrint(&unk_C8);
serialPrint(&unk_C8);
blueLedOff();
return greenLedOn();
}

canonical_decompiled_code:
__int64 func6()
{
func0();
func3();
func2();
debugHexdump(&buffer, 0LL);
serialPrint(&unk_C8);
serialPrint(&unk_C8);
func4();
return func1();
}

original_code:
void test(uint8_t value)
{
    FlashAddress_t address;
    int i;
    redLedOn();
    for (i = 0; i < 0; ++i) {
        buffer[i] = (uint8_t) (i + value);
    }
    ;
    address = 0x6000;
    for (i = 0; i < 1; ++i) {
        ;
        address += sizeof(buffer);
    }
    redLedOff();
    blueLedOn();
    address = 0x6000;
    for (i = 0; i < 1; ++i) {
        ;
        debugHexdump(buffer, sizeof(buffer));
        serialPrint( "\n");
        serialPrint( "\n");
        address += sizeof(buffer);
    }
    blueLedOff();
    greenLedOn();
}

canonical_original_code:
void test(unsigned char value)
{
    unsigned short address;
    int i;
    redLedOn();
    for (i = 0; i < 0; ++i) {
        buffer[i] = (unsigned char) (i + value);
    }
    ;
    address = 0x6000;
    for (i = 0; i < 1; ++i) {
        ;
        address += sizeof(buffer);
    }
    redLedOff();
    blueLedOn();
    address = 0x6000;
    for (i = 0; i < 1; ++i) {
        ;
        debugHexdump(buffer, sizeof(buffer));
        serialPrint( "\n");
        serialPrint( "\n");
        address += sizeof(buffer);
    }
    blueLedOff();
    greenLedOn();
}

variable_types:
value
unsigned char

variable_types:
address
unsigned short

variable_types: i int


doMsleep

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


printInit

decompiled_code:
// attributes: thunk
__int64 printInit()
{
return printInitReal();
}

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

original_code:
void printInit(void)
{
    extern void printInitReal(void);
    printInitReal();
}

canonical_original_code:
void printInit()
{
    extern void printInitReal();
    printInitReal();
}

variable_types:
printInitReal
void ()


radioPrint

decompiled_code:
__int64 __fastcall radioPrint(const char *a1)
{
__int16 v1; // ax

v1 = strlen(a1);
return radioSend_isra_0((__int64)a1, v1 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
__int16 v1; // ax

v1 = strlen(a1);
return func1((__int64)a1, v1 + 1);
}

original_code:
void radioPrint(const char* str)
{
   radioSend((uint8_t *) str, strlen(str) + 1);
}

canonical_original_code:
void radioPrint(char *str)
{
   radioSend((unsigned char *) str, strlen(str) + 1);
}


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 func1()
{
__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

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(); }


decompiled_code:
__int64 __fastcall usart0Recv(char a1)
{
char v1; // al

greenLedToggle();
v1 = bufPos;
if ( (unsigned __int8)bufPos <= 0x7Eu )
{
buf[(unsigned __int8)bufPos] = a1;
bufPos = v1 + 1;
blueLedToggle();
}
return alarmSchedule(&printAlarm, 200LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
char v1; // al

func1();
v1 = bufPos;
if ( (unsigned __int8)bufPos <= 0x7Eu )
{
buf[(unsigned __int8)bufPos] = a1;
bufPos = v1 + 1;
func2();
}
return alarmSchedule(&printAlarm, 200LL);
}

original_code:
void usart0Recv(uint8_t b) {
    greenLedToggle();
    if (bufPos < 128 - 1) {
        buf[bufPos++] = b;
        blueLedToggle();
    }
    alarmSchedule(&printAlarm, 200);
}

canonical_original_code:
void usart0Recv(unsigned char b) {
    greenLedToggle();
    if (bufPos < 128 - 1) {
        buf[bufPos++] = b;
        blueLedToggle();
    }
    alarmSchedule(&printAlarm, 200);
}

variable_types:
b
unsigned char


doPrintBuffer

decompiled_code:
__int64 doPrintBuffer()
{
__int64 result; // rax
bool v1; // zf

result = (unsigned __int8)bufPos;
if ( bufPos )
{
v1 = print_buf == 0LL;
buf[(unsigned __int8)bufPos] = 0;
bufPos = result + 1;
if ( !v1 )
{
debugPrintfFormat(&unk_2A0, buf);
result = serialPrint(print_buf);
}
bufPos = 0;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
bool v1; // zf

result = (unsigned __int8)bufPos;
if ( bufPos )
{
v1 = print_buf == 0LL;
buf[(unsigned __int8)bufPos] = 0;
bufPos = result + 1;
if ( !v1 )
{
debugPrintfFormat(&unk_2A0, buf);
result = serialPrint(print_buf);
}
bufPos = 0;
}
return result;
}

original_code:
void doPrintBuffer(void *userData) {
    if (bufPos > 0) {
        Handle_t h;
        (h) = 0;
        buf[bufPos++] = 0;
        if (2 == 1) { serialPrintV("%s", buf); } else { if (_print_buf) { debugPrintfFormat("%s", buf); serialPrint(_print_buf); }; };
        bufPos = 0;
        ((void) h);
    }
}

canonical_original_code:
void doPrintBuffer(void *userData) {
    if (bufPos > 0) {
        unsigned short h;
        (h) = 0;
        buf[bufPos++] = 0;
        if (2 == 1) { serialPrintV("%s", buf); } else { if (_print_buf) { debugPrintfFormat("%s", buf); serialPrint(_print_buf); }; };
        bufPos = 0;
        ((void) h);
    }
}

variable_types:
userData
void *

variable_types:
h
unsigned short


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_decompiled_code:
int __fastcall func0(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


printInit

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


serialPrint

decompiled_code:
__int64 __fastcall serialPrint(__int64 a1)
{
return serialSendString(0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return serialSendString(0LL, a1);
}

original_code:
void serialPrint(const char* str)
{
    serialSendString(0, str);
}

canonical_original_code:
void serialPrint(char *str)
{
    serialSendString(0, str);
}


doMsleep

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