parseGSA

decompiled_code:
__int64 __fastcall parseGSA(__int64 a1, int a2)
{
char v2; // r8
__int64 result; // rax
int v4[3]; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+10h] [rbp-8h] BYREF

v4[0] = a2;
v5 = a1;
v2 = parseGPSChar(&v5, v4);
result = 0xFFFFFFFFLL;
if ( v2 )
result = parseGPSFixQual(&v5, v4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
char v2; // r8
__int64 result; // rax
int v4[3]; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+10h] [rbp-8h] BYREF

v4[0] = a2;
v5 = a1;
v2 = func7(&v5, v4);
result = 0xFFFFFFFFLL;
if ( v2 )
result = func5(&v5, v4);
return result;
}

original_code:
uint_t parseGSA(const void *buf, uint_t len, GPSFix_t *fix)
{
    const uint8_t *p = (uint8_t *) buf;
    uint_t res;
    if (parseGPSChar(&p, &len) == 0) return -1;
    if ((res = parseGPSFixQual(&p, &len, fix))) return res;
    return 0;
}

canonical_original_code:
unsigned int parseGSA(void *buf, unsigned int len, struct GPSFix_t *fix)
{
    unsigned char *p = (uint8_t *) buf;
    unsigned int res;
    if (parseGPSChar(&p, &len) == 0) return -1;
    if ((res = parseGPSFixQual(&p, &len, fix))) return res;
    return 0;
}


parseRMC

repo: atiselsts/osw

decompiled_code:
__int64 __fastcall parseRMC(__int64 a1, int a2, __int64 a3)
{
__int64 result; // rax
char v5; // r8
int v6; // [rsp+4h] [rbp-1Ch] BYREF
int v7; // [rsp+Ch] [rbp-14h] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-10h] BYREF

v6 = a2;
v8[0] = a1;
result = parseGPSTime(v8, &v6);
if ( !(_DWORD)result )
{
v5 = parseGPSChar(v8, &v6);
result = 0xFFFFFFFFLL;
if ( v5 == 65 )
{
result = parseGPSPos(v8, &v6, a3 + 6);
if ( !(_DWORD)result )
{
result = parseGPSPos(v8, &v6, a3 + 10);
if ( !(_DWORD)result )
{
result = parseGPSu32(v8, &v6, &v7, 0LL);
if ( !(_DWORD)result )
{
result = parseGPSu32(v8, &v6, &v7, 0LL);
if ( !(_DWORD)result )
result = parseGPSDate(v8, &v6);
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, __int64 a3)
{
__int64 result; // rax
char v5; // r8
int v6; // [rsp+4h] [rbp-1Ch] BYREF
int v7; // [rsp+Ch] [rbp-14h] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-10h] BYREF

v6 = a2;
v8[0] = a1;
result = func2(v8, &v6);
if ( !(_DWORD)result )
{
v5 = func7(v8, &v6);
result = 0xFFFFFFFFLL;
if ( v5 == 65 )
{
result = func8(v8, &v6, a3 + 6);
if ( !(_DWORD)result )
{
result = func8(v8, &v6, a3 + 10);
if ( !(_DWORD)result )
{
result = func1(v8, &v6, &v7, 0LL);
if ( !(_DWORD)result )
{
result = func1(v8, &v6, &v7, 0LL);
if ( !(_DWORD)result )
result = func6(v8, &v6);
}
}
}
}
}
return result;
}

original_code:
uint_t parseRMC(const void *buf, uint_t len, GPSFix_t *fix) {
    const uint8_t *p = (uint8_t *) buf;
    uint_t res;
    if ((res = parseGPSTime(&p, &len, fix))) return res;
    if (parseGPSChar(&p, &len) != 'A') return -1;
    if ((res = parseGPSPos(&p, &len, &fix->lat))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lon))) return res;
    uint32_t dummy;
    if ((res = parseGPSu32(&p, &len, &dummy, 0))) return res;
    if ((res = parseGPSu32(&p, &len, &dummy, 0))) return res;
    if ((res = parseGPSDate(&p, &len, fix))) return res;
    return 0;
}

canonical_original_code:
unsigned int parseRMC(void *buf, unsigned int len, struct GPSFix_t *fix) {
    unsigned char *p = (uint8_t *) buf;
    unsigned int res;
    if ((res = parseGPSTime(&p, &len, fix))) return res;
    if (parseGPSChar(&p, &len) != 'A') return -1;
    if ((res = parseGPSPos(&p, &len, &fix->lat))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lon))) return res;
    unsigned int dummy;
    if ((res = parseGPSu32(&p, &len, &dummy, 0))) return res;
    if ((res = parseGPSu32(&p, &len, &dummy, 0))) return res;
    if ((res = parseGPSDate(&p, &len, fix))) return res;
    return 0;
}

variable_types:
buf
void *

variable_types:
len
unsigned int

variable_types: struct GPSFix_t { struct GPSDate_t { unsigned short day; } d; unsigned char h; unsigned char m; unsigned char s; struct GPSFixQuality_t { unsigned char fix; } q; struct GPSPos_t { unsigned char deg; unsigned char min; unsigned short mindec; } lat; struct GPSPos_t lon; } *

variable_types:
p
unsigned char *

variable_types:
res
unsigned int

variable_types:
dummy
unsigned int

return_type: unsigned int

user_defined_types: struct GPSFix_t { struct GPSDate_t d; unsigned char h; unsigned char m; unsigned char s; struct GPSFixQuality_t q; struct GPSPos_t lat; struct GPSPos_t lon; }


doMsleep

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

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

return_type: void


adcInit

decompiled_code:
__int64 adcInit()
{
return hplAdcInit();
}

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

original_code:
void adcInit(void)
{
    hplAdcInit();
}

canonical_original_code:
void adcInit()
{
    hplAdcInit();
}


adcSetChannel

decompiled_code:
__int64 __fastcall adcSetChannel(unsigned __int8 a1)
{
return hplAdcSetChannel(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1)
{
return hplAdcSetChannel(a1);
}

original_code:
void adcSetChannel(uint8_t ch)
{
    _Bool wasOn = 1;
    if (wasOn) ;
    hplAdcSetChannel(ch);
    if (wasOn) ;
}

canonical_original_code:
void adcSetChannel(unsigned char ch)
{
    _Bool wasOn = 1;
    if (wasOn) ;
    hplAdcSetChannel(ch);
    if (wasOn) ;
}

variable_types:
ch
unsigned char


adcRead

decompiled_code:
__int64 __fastcall adcRead(unsigned __int8 a1)
{
if ( (unsigned __int8)hplAdcGetChannel() != a1 )
hplAdcSetChannel(a1);
return hplAdcGetVal();
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
if ( (unsigned __int8)hplAdcGetChannel() != a1 )
hplAdcSetChannel(a1);
return hplAdcGetVal();
}

original_code:
uint16_t adcRead(uint8_t ch)
{
    uint16_t retval;
    _Bool wasOn = 1;
    if (hplAdcGetChannel() != ch) {
        if (wasOn) ;
        hplAdcSetChannel(ch);
        if (wasOn) ;
    }
    if (!wasOn) {
        ;
    }
    ;
    while ((0)) {}
    retval = hplAdcGetVal();
    if (!wasOn) {
        ;
    }
    return retval;
}

canonical_original_code:
unsigned short adcRead(unsigned char ch)
{
    unsigned short retval;
    _Bool wasOn = 1;
    if (hplAdcGetChannel() != ch) {
        if (wasOn) ;
        hplAdcSetChannel(ch);
        if (wasOn) ;
    }
    if (!wasOn) {
        ;
    }
    ;
    while ((0)) {}
    retval = hplAdcGetVal();
    if (!wasOn) {
        ;
    }
    return retval;
}

variable_types:
retval
unsigned short

return_type: unsigned short


rewind

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

result = *(unsigned __int16 *)(a1 + 2);
*(_DWORD *)(a1 + 12) = 0;
*(_WORD *)(a1 + 4) = result;
return result;
}

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

result = *(unsigned __int16 *)(a1 + 2);
*(_DWORD *)(a1 + 12) = 0;
*(_WORD *)(a1 + 4) = result;
return result;
}

original_code:
static inline void rewind(FILE *fp)
{
    fp->position = 0;
    fp->currentCluster = fp->firstCluster;
}

canonical_original_code:
static inline void rewind(struct FILE_s *fp)
{
    fp->position = 0;
    fp->currentCluster = fp->firstCluster;
}

variable_types: struct FILE_s { _Bool isOpened; unsigned short firstCluster; unsigned short currentCluster; unsigned short directoryEntry; unsigned int fileSize; unsigned int position; unsigned short flags; unsigned char state; _Bool dirEntryDirty; } *


redLedOn

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

canonical_decompiled_code:
__int64 func1()
{
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 func2()
{
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 func6(unsigned __int16 a1)
{
return usleep(1000 * a1);
}


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 *


doMsleep

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


extFlashSleep

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

if ( flashFile )
{
result = fclose(flashFile);
flashFile = 0LL;
}
return result;
}

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

if ( flashFile )
{
result = fclose(flashFile);
flashFile = 0LL;
}
return result;
}

original_code:
void extFlashSleep(void)
{
    if (flashFile) {
        fclose(flashFile);
        flashFile = ((void *)0);
    }
}

canonical_original_code:
void extFlashSleep()
{
    if (flashFile) {
        fclose(flashFile);
        flashFile = ((void *)0);
    }
}


flashFileClose

decompiled_code:
// attributes: thunk
void flashFileClose(void)
{
extFlashSleep();
}

canonical_decompiled_code:
void func3(void)
{
func2();
}

original_code:
static void flashFileClose(void)
{
    if (flashFile) {
        fclose(flashFile);
        flashFile = ((void *)0);
    }
}

canonical_original_code:
static void flashFileClose()
{
    if (flashFile) {
        fclose(flashFile);
        flashFile = ((void *)0);
    }
}


extFlashWake

decompiled_code:
void extFlashWake()
{
if ( !flashFile )
flashFile = fopen("extflash.dat", "r+");
}

canonical_decompiled_code:
void func4()
{
if ( !flashFile )
flashFile = fopen("extflash.dat", "r+");
}

original_code:
void extFlashWake(void)
{
    if (!flashFile) {
        flashFile = fopen("extflash.dat", "r+");
    }
}

canonical_original_code:
void extFlashWake()
{
    if (!flashFile) {
        flashFile = fopen("extflash.dat", "r+");
    }
}


extFlashRead

decompiled_code:
void __fastcall extFlashRead(__int64 off, void *ptr, unsigned __int16 a3)
{
if ( flashFile )
{
if ( (unsigned int)off + a3 > 0x200000 )
{
serialPrintV("extFlashRead: address out of bounds!\n", ptr);
}
else if ( fseek(flashFile, (unsigned int)off, 0) )
{
perror("fseek");
}
else if ( a3 != fread(ptr, 1uLL, a3, flashFile) )
{
perror("fread");
}
}
else
{
serialPrintV("extFlashRead: flash not opened\n", ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 off, void *ptr, unsigned __int16 a3)
{
if ( flashFile )
{
if ( (unsigned int)off + a3 > 0x200000 )
{
func0("extFlashRead: address out of bounds!\n", ptr);
}
else if ( fseek(flashFile, (unsigned int)off, 0) )
{
perror("fseek");
}
else if ( a3 != fread(ptr, 1uLL, a3, flashFile) )
{
perror("fread");
}
}
else
{
func0("extFlashRead: flash not opened\n", ptr);
}
}

original_code:
void extFlashRead(uint32_t addr, uint8_t *buf, uint16_t len)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashRead: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashRead: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (addr + len > ((uint32_t)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashRead: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashRead: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fread(buf, 1, len, flashFile) != len) {
        perror("fread");
    }
}

canonical_original_code:
void extFlashRead(unsigned int addr, unsigned char *buf, unsigned short len)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashRead: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashRead: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (addr + len > ((unsigned int)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashRead: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashRead: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fread(buf, 1, len, flashFile) != len) {
        perror("fread");
    }
}

variable_types:
addr
unsigned int

variable_types:
buf
unsigned char *

variable_types:
len
unsigned short


extFlashWrite

decompiled_code:
void __fastcall extFlashWrite(__int64 off, void *ptr, unsigned __int16 a3)
{
FILE *v3; // r15
int v4; // er14
size_t v5; // rbx
void *v6; // r12
char *v7; // r13
_BYTE *v8; // rax
const char *v9; // rdi

v3 = flashFile;
if ( !flashFile )
{
v9 = "extFlashWrite: flash not opened\n";
goto LABEL_15;
}
v4 = a3;
if ( (unsigned int)off + a3 > 0x200000 )
{
v9 = "extFlashWrite: address out of bounds!\n";
LABEL_15:
serialPrintV(v9, ptr);
return;
}
v5 = a3;
v6 = ptr;
v7 = (char *)malloc(a3);
if ( fseek(v3, (unsigned int)off, 0) )
goto LABEL_12;
ptr = serialPrintV + 1;
if ( v5 != fread(v7, 1uLL, v5, flashFile) )
perror("fread");
if ( v4 )
{
v8 = v7;
while ( *v8 == 0xFF )
{
if ( ++v8 == &v7[v5] )
goto LABEL_11;
}
v9 = "extFlashWrite: attempt to write in a sector that was not erased!\n";
goto LABEL_15;
}
LABEL_11:
free(v7);
if ( fseek(flashFile, (unsigned int)off, 0) )
{
LABEL_12:
perror("fseek");
}
else if ( v5 != fwrite(v6, 1uLL, v5, flashFile) )
{
perror("fwrite");
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 off, void *ptr, unsigned __int16 a3)
{
FILE *v3; // r15
int v4; // er14
size_t v5; // rbx
void *v6; // r12
char *v7; // r13
_BYTE *v8; // rax
const char *v9; // rdi

v3 = flashFile;
if ( !flashFile )
{
v9 = "extFlashWrite: flash not opened\n";
goto LABEL_15;
}
v4 = a3;
if ( (unsigned int)off + a3 > 0x200000 )
{
v9 = "extFlashWrite: address out of bounds!\n";
LABEL_15:
func0(v9, ptr);
return;
}
v5 = a3;
v6 = ptr;
v7 = (char *)malloc(a3);
if ( fseek(v3, (unsigned int)off, 0) )
goto LABEL_12;
ptr = serialPrintV + 1;
if ( v5 != fread(v7, 1uLL, v5, flashFile) )
perror("fread");
if ( v4 )
{
v8 = v7;
while ( *v8 == 0xFF )
{
if ( ++v8 == &v7[v5] )
goto LABEL_11;
}
v9 = "extFlashWrite: attempt to write in a sector that was not erased!\n";
goto LABEL_15;
}
LABEL_11:
free(v7);
if ( fseek(flashFile, (unsigned int)off, 0) )
{
LABEL_12:
perror("fseek");
}
else if ( v5 != fwrite(v6, 1uLL, v5, flashFile) )
{
perror("fwrite");
}
}

original_code:
void extFlashWrite(uint32_t addr, const uint8_t *buf, uint16_t len)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashWrite: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (addr + len > ((uint32_t)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashWrite: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    uint8_t *oldContents = malloc(len);
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fread(oldContents, 1, len, flashFile) != len) {
        perror("fread");
    }
    uint32_t i;
    for (i = 0; i < len; ++i) {
        if (oldContents[i] != 0xff) {
            if (1 == 1) { serialPrintV("extFlashWrite: attempt to write in a sector that was not erased!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: attempt to write in a sector that was not erased!\n"); serialPrint(_print_buf); }; };
            return;
        }
    }
    free(oldContents);
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fwrite(buf, 1, len, flashFile) != len) {
        perror("fwrite");
    }
}

canonical_original_code:
void extFlashWrite(unsigned int addr, unsigned char *buf, unsigned short len)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashWrite: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (addr + len > ((unsigned int)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashWrite: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    unsigned char *oldContents = malloc(len);
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fread(oldContents, 1, len, flashFile) != len) {
        perror("fread");
    }
    unsigned int i;
    for (i = 0; i < len; ++i) {
        if (oldContents[i] != 0xff) {
            if (1 == 1) { serialPrintV("extFlashWrite: attempt to write in a sector that was not erased!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashWrite: attempt to write in a sector that was not erased!\n"); serialPrint(_print_buf); }; };
            return;
        }
    }
    free(oldContents);
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    if (fwrite(buf, 1, len, flashFile) != len) {
        perror("fwrite");
    }
}

variable_types:
oldContents
unsigned char *

variable_types:
i
unsigned int


extFlashBulkErase

decompiled_code:
size_t __fastcall extFlashBulkErase(__int64 a1, __int64 a2)
{
int v2; // ebx
size_t result; // rax
_BYTE v4[1048]; // [rsp-418h] [rbp-418h] BYREF

if ( !flashFile )
return serialPrintV("extFlashBulkErase: flash not opened\n", a2);
v2 = 2048;
memset(v4, 0xFFu, 0x400uLL);
rewind(flashFile);
do
{
result = fwrite(v4, 1uLL, 0x400uLL, flashFile);
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(__int64 a1, __int64 a2)
{
int v2; // ebx
size_t result; // rax
_BYTE v4[1048]; // [rsp-418h] [rbp-418h] BYREF

if ( !flashFile )
return func0("extFlashBulkErase: flash not opened\n", a2);
v2 = 2048;
memset(v4, 0xFFu, 0x400uLL);
rewind(flashFile);
do
{
result = fwrite(v4, 1uLL, 0x400uLL, flashFile);
--v2;
}
while ( v2 );
return result;
}

original_code:
void extFlashBulkErase(void)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashBulkErase: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashBulkErase: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    uint8_t block[1024];
    memset(block, 0xff, sizeof(block));
    rewind(flashFile);
    uint32_t i;
    for (i = 0; i < ((uint32_t)0x10000 * 32) / sizeof(block); i++) {
        fwrite(block, 1, sizeof(block), flashFile);
    }
}

canonical_original_code:
void extFlashBulkErase()
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashBulkErase: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashBulkErase: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    unsigned char block[1024];
    memset(block, 0xff, sizeof(block));
    rewind(flashFile);
    unsigned int i;
    for (i = 0; i < ((unsigned int)0x10000 * 32) / sizeof(block); i++) {
        fwrite(block, 1, sizeof(block), flashFile);
    }
}

variable_types:
block
unsigned char [1024]


extFlashInit

decompiled_code:
int extFlashInit()
{
int result; // eax
struct stat v1; // [rsp+0h] [rbp-98h] BYREF

atexit(flashFileClose);
result = stat("extflash.dat", &v1);
if ( result || v1.st_size != 0x200000 )
{
flashFile = fopen("extflash.dat", "w+");
extFlashBulkErase((__int64)"extflash.dat", (__int64)"w+");
result = fclose(flashFile);
flashFile = 0LL;
}
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax
struct stat v1; // [rsp+0h] [rbp-98h] BYREF

atexit(flashFileClose);
result = stat("extflash.dat", &v1);
if ( result || v1.st_size != 0x200000 )
{
flashFile = fopen("extflash.dat", "w+");
func7((__int64)"extflash.dat", (__int64)"w+");
result = fclose(flashFile);
flashFile = 0LL;
}
return result;
}

original_code:
void extFlashInit(void)
{
    static _Bool initCalled;
    if (initCalled) {
        if (1 == 1) { serialPrintV("extFlashInit: already called\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashInit: already called\n"); serialPrint(_print_buf); }; };
        return;
    }
    atexit(flashFileClose);
    struct stat st;
    if (stat("extflash.dat", &st) == 0) {
        if (st.st_size == ((uint32_t)0x10000 * 32)) return;
    }
    flashFile = fopen("extflash.dat", "w+");
    extFlashBulkErase();
    fclose(flashFile);
    flashFile = ((void *)0);
}

canonical_original_code:
void extFlashInit()
{
    static _Bool initCalled;
    if (initCalled) {
        if (1 == 1) { serialPrintV("extFlashInit: already called\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashInit: already called\n"); serialPrint(_print_buf); }; };
        return;
    }
    atexit(flashFileClose);
    struct stat st;
    if (stat("extflash.dat", &st) == 0) {
        if (st.st_size == ((unsigned int)0x10000 * 32)) return;
    }
    flashFile = fopen("extflash.dat", "w+");
    extFlashBulkErase();
    fclose(flashFile);
    flashFile = ((void *)0);
}

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

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


extFlashEraseSector

decompiled_code:
void __fastcall extFlashEraseSector(unsigned int a1, __int64 a2)
{
int v2; // ebx
_BYTE v3[1048]; // [rsp-418h] [rbp-418h] BYREF

if ( flashFile )
{
LOWORD(a1) = 0;
if ( a1 > 0x1FFFFF )
{
serialPrintV("extFlashEraseSector: address out of bounds!\n", a2);
}
else if ( fseek(flashFile, a1, 0) )
{
perror("fseek");
}
else
{
v2 = 64;
memset(v3, 0xFFu, 0x400uLL);
do
{
fwrite(v3, 1uLL, 0x400uLL, flashFile);
--v2;
}
while ( v2 );
}
}
else
{
serialPrintV("extFlashEraseSector: flash not opened\n", a2);
}
}

canonical_decompiled_code:
void __fastcall func9(unsigned int a1, __int64 a2)
{
int v2; // ebx
_BYTE v3[1048]; // [rsp-418h] [rbp-418h] BYREF

if ( flashFile )
{
LOWORD(a1) = 0;
if ( a1 > 0x1FFFFF )
{
func0("extFlashEraseSector: address out of bounds!\n", a2);
}
else if ( fseek(flashFile, a1, 0) )
{
perror("fseek");
}
else
{
v2 = 64;
memset(v3, 0xFFu, 0x400uLL);
do
{
fwrite(v3, 1uLL, 0x400uLL, flashFile);
--v2;
}
while ( v2 );
}
}
else
{
func0("extFlashEraseSector: flash not opened\n", a2);
}
}

original_code:
void extFlashEraseSector(uint32_t addr)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashEraseSector: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashEraseSector: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    addr = ((uint32_t)(addr) & ~(0x10000 - 1));
    if (addr >= ((uint32_t)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashEraseSector: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashEraseSector: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    uint8_t block[1024];
    memset(block, 0xff, sizeof(block));
    uint32_t i;
    for (i = 0; i < 0x10000 / sizeof(block); i++) {
        fwrite(block, 1, sizeof(block), flashFile);
    }
}

canonical_original_code:
void extFlashEraseSector(unsigned int addr)
{
    if (!flashFile) {
        if (1 == 1) { serialPrintV("extFlashEraseSector: flash not opened\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashEraseSector: flash not opened\n"); serialPrint(_print_buf); }; };
        return;
    }
    addr = ((unsigned int)(addr) & ~(0x10000 - 1));
    if (addr >= ((unsigned int)0x10000 * 32)) {
        if (1 == 1) { serialPrintV("extFlashEraseSector: address out of bounds!\n"); } else { if (_print_buf) { debugPrintfFormat("extFlashEraseSector: address out of bounds!\n"); serialPrint(_print_buf); }; };
        return;
    }
    if (fseek(flashFile, addr, 0) != 0) {
        perror("fseek");
        return;
    }
    unsigned char block[1024];
    memset(block, 0xff, sizeof(block));
    unsigned int i;
    for (i = 0; i < 0x10000 / sizeof(block); i++) {
        fwrite(block, 1, sizeof(block), flashFile);
    }
}


doMsleep

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


serialSendString

decompiled_code:
__int64 __fastcall serialSendString(unsigned __int8 a1, _BYTE *a2)
{
_BYTE *i; // rbx
__int64 result; // rax

for ( i = a2; *i; result = serialSendByte(a1) )
{
while ( 1 )
{
result = serialSendByte(a1);
if ( *i == 10 )
break;
if ( !*++i )
return result;
}
++i;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, _BYTE *a2)
{
_BYTE *i; // rbx
__int64 result; // rax

for ( i = a2; *i; result = serialSendByte(a1) )
{
while ( 1 )
{
result = serialSendByte(a1);
if ( *i == 10 )
break;
if ( !*++i )
return result;
}
++i;
}
return result;
}

original_code:
void serialSendString(uint8_t id, const char *s) {
    for (; *s; ++s) {
        serialSendByte(id, *s);
        if (*s == '\n') {
            serialSendByte(id, '\r');
        }
    }
}

canonical_original_code:
void serialSendString(unsigned char id, char *s) {
    for (; *s; ++s) {
        serialSendByte(id, *s);
        if (*s == '\n') {
            serialSendByte(id, '\r');
        }
    }
}

variable_types:
id
unsigned char

variable_types:
s
char *


serialSendData

decompiled_code:
__int64 __fastcall serialSendData(unsigned __int8 a1, unsigned __int64 a2, unsigned __int16 a3)
{
unsigned __int64 v3; // rbp
unsigned __int64 v4; // rbx
__int64 result; // rax

v3 = a2 + a3;
if ( a2 < v3 )
{
v4 = a2;
do
{
++v4;
result = serialSendByte(a1);
}
while ( v4 != v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 a1, unsigned __int64 a2, unsigned __int16 a3)
{
unsigned __int64 v3; // rbp
unsigned __int64 v4; // rbx
__int64 result; // rax

v3 = a2 + a3;
if ( a2 < v3 )
{
v4 = a2;
do
{
++v4;
result = serialSendByte(a1);
}
while ( v4 != v3 );
}
return result;
}

original_code:
void serialSendData(uint8_t id, const uint8_t *data, uint16_t len) {
    const uint8_t *p;
    for (p = data; p < data + len; ++p) {
        serialSendByte(id, *p);
    }
}

canonical_original_code:
void serialSendData(unsigned char id, unsigned char *data, unsigned short len) {
    unsigned char *p;
    for (p = data; p < data + len; ++p) {
        serialSendByte(id, *p);
    }
}

variable_types:
data
unsigned char *

variable_types:
p
unsigned char *


serialSetReceiveHandle

decompiled_code:
__int64 __fastcall serialSetReceiveHandle(char a1, __int64 a2)
{
unsigned int v2; // er12

v2 = -1;
if ( !a1 )
{
serialRecvCb = a2;
v2 = 0;
if ( a2 )
serialEnableRX(0LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1, __int64 a2)
{
unsigned int v2; // er12

v2 = -1;
if ( !a1 )
{
serialRecvCb = a2;
v2 = 0;
if ( a2 )
serialEnableRX(0LL);
}
return v2;
}

original_code:
uint_t serialSetReceiveHandle(uint8_t id, SerialCallback_t functionHandle) {
    if (id >= 1) return -1;
    serialRecvCb[id] = functionHandle;
    if (functionHandle) {
        serialEnableRX(id);
    }
    return 0;
}

canonical_original_code:
unsigned int serialSetReceiveHandle(unsigned char id, void (*functionHandle)(unsigned char)) {
    if (id >= 1) return -1;
    serialRecvCb[id] = functionHandle;
    if (functionHandle) {
        serialEnableRX(id);
    }
    return 0;
}

variable_types:
functionHandle
void (*)(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);
}

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

variable_types:
milliseconds
unsigned short


sdcardInit

decompiled_code:
__int64 sdcardInit()
{
int v0; // eax
int v1; // ebp

serialPrintV_constprop_0();
v0 = open("sdcard.dat", 0);
v1 = v0;
if ( v0 < 0 )
{
sdcardBulkErase();
close(v1);
}
else
{
close(v0);
}
return 1LL;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // eax
int v1; // ebp

func0();
v0 = open("sdcard.dat", 0);
v1 = v0;
if ( v0 < 0 )
{
func4();
close(v1);
}
else
{
close(v0);
}
return 1LL;
}

original_code:
_Bool sdcardInit(void)
{
    if (1 == 1) { serialPrintV("Opening SDCARD image `" "sdcard.dat" "'...\n"); } else { if (_print_buf) { debugPrintfFormat("Opening SDCARD image `" "sdcard.dat" "'...\n"); serialPrint(_print_buf); }; };
    int data = open("sdcard.dat", 00);
    if (data < 0) {
        sdcardBulkErase();
    }
    close(data);
    return 1;
}

canonical_original_code:
_Bool sdcardInit()
{
    if (1 == 1) { serialPrintV("Opening SDCARD image `" "sdcard.dat" "'...\n"); } else { if (_print_buf) { debugPrintfFormat("Opening SDCARD image `" "sdcard.dat" "'...\n"); serialPrint(_print_buf); }; };
    int data = open("sdcard.dat", 00);
    if (data < 0) {
        sdcardBulkErase();
    }
    close(data);
    return 1;
}

variable_types: data int


sdcardEraseSector

decompiled_code:
int __fastcall sdcardEraseSector(__off_t offset)
{
int result; // eax
int v2; // ebp

result = open("sdcard.dat", 1);
if ( result >= 0 )
{
v2 = result;
lseek(result, (unsigned int)offset, 0);
result = write(v2, &zeroSector, 0x200uLL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(__off_t offset)
{
int result; // eax
int v2; // ebp

result = open("sdcard.dat", 1);
if ( result >= 0 )
{
v2 = result;
lseek(result, (unsigned int)offset, 0);
result = write(v2, &zeroSector, 0x200uLL);
}
return result;
}

original_code:
void sdcardEraseSector(uint32_t address)
{
    int data = open("sdcard.dat", 01);
    if (data < 0) return;
    lseek(data, address, 0);
    int r = write(data, zeroSector, sizeof(zeroSector));
    (void) r;
}

canonical_original_code:
void sdcardEraseSector(unsigned int address)
{
    int data = open("sdcard.dat", 01);
    if (data < 0) return;
    lseek(data, address, 0);
    int r = write(data, zeroSector, sizeof(zeroSector));
    (void) r;
}

variable_types:
address
unsigned int

variable_types: r int


sdcardFlush

decompiled_code:
void sdcardFlush()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

original_code:
void sdcardFlush(void)
{
}

canonical_original_code:
void sdcardFlush()
{
}


sdcardGetSize

decompiled_code:
__int64 sdcardGetSize()
{
return 0x8000LL;
}

canonical_decompiled_code:
__int64 func10()
{
return 0x8000LL;
}

original_code:
uint32_t sdcardGetSize(void)
{
    return 32768;
}

canonical_original_code:
unsigned int sdcardGetSize()
{
    return 32768;
}


radioOff

decompiled_code:
// attributes: thunk
__int64 radioOff()
{
return pcRadioOff();
}

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

original_code:
static inline void radioOff(void) {
    pcRadioOff();
}

canonical_original_code:
static inline void radioOff() {
    pcRadioOff();
}


doMsleep

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


readExactly

decompiled_code:
__int64 __fastcall readExactly(unsigned int a1, void *a2, unsigned __int16 a3)
{
unsigned int v3; // er12
unsigned int v5; // eax
int *v6; // rax
char *v7; // rax

v3 = 0;
if ( !a3 )
return v3;
v5 = read(a1, a2, a3);
v3 = v5;
if ( !(_WORD)v5 )
{
v3 = -1;
if ( print_buf )
{
debugPrintfFormat("socket %i disconnected\n", a1);
serialPrint(print_buf);
}
return v3;
}
if ( (v5 & 0x8000u) == 0 || !print_buf )
return v3;
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("socket %i reading error: %s\n", a1, v7);
serialPrint(print_buf);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, void *a2, unsigned __int16 a3)
{
unsigned int v3; // er12
unsigned int v5; // eax
int *v6; // rax
char *v7; // rax

v3 = 0;
if ( !a3 )
return v3;
v5 = read(a1, a2, a3);
v3 = v5;
if ( !(_WORD)v5 )
{
v3 = -1;
if ( print_buf )
{
debugPrintfFormat("socket %i disconnected\n", a1);
serialPrint(print_buf);
}
return v3;
}
if ( (v5 & 0x8000u) == 0 || !print_buf )
return v3;
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("socket %i reading error: %s\n", a1, v7);
serialPrint(print_buf);
return v3;
}

original_code:
int16_t readExactly(int sock, void *buf, uint16_t len) {
    if (len == 0) return 0;
    int16_t l = read(sock, buf, len);
    if (l == 0) {
        if (2 == 1) { serialPrintV("socket %i disconnected\n", sock); } else { if (_print_buf) { debugPrintfFormat("socket %i disconnected\n", sock); serialPrint(_print_buf); }; };
        return -1;
    }
    if (l < 0) {
        if (3 == 1) { serialPrintV("socket %i reading error: %s\n", sock, strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("socket %i reading error: %s\n", sock, strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
    }
    return l;
}

canonical_original_code:
short readExactly(int sock, void *buf, unsigned short len) {
    if (len == 0) return 0;
    short l = read(sock, buf, len);
    if (l == 0) {
        if (2 == 1) { serialPrintV("socket %i disconnected\n", sock); } else { if (_print_buf) { debugPrintfFormat("socket %i disconnected\n", sock); serialPrint(_print_buf); }; };
        return -1;
    }
    if (l < 0) {
        if (3 == 1) { serialPrintV("socket %i reading error: %s\n", sock, strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("socket %i reading error: %s\n", sock, strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
    }
    return l;
}

variable_types: sock int

variable_types:
buf
void *

variable_types: l short

return_type: short


pcRadioRecvPack

decompiled_code:
__int64 __fastcall pcRadioRecvPack(unsigned int a1, _WORD *a2)
{
__int64 result; // rax

if ( (unsigned __int16)readExactly(a1, a2, 2u) != 2 )
return 0xFFFFFFFFLL;
result = readExactly(a1, a2 + 1, *a2);
if ( (__int16)result > 0 )
result = (unsigned int)(result + 2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, _WORD *a2)
{
__int64 result; // rax

if ( (unsigned __int16)func5(a1, a2, 2u) != 2 )
return 0xFFFFFFFFLL;
result = func5(a1, a2 + 1, *a2);
if ( (__int16)result > 0 )
result = (unsigned int)(result + 2);
return result;
}

original_code:
int16_t pcRadioRecvPack(int sock, void *buf, uint16_t bufLen) {
    if (readExactly(sock, buf, sizeof(PcRadioPackSize_t))
            != sizeof(PcRadioPackSize_t)) return -1;
    PcRadioPackSize_t len = *((PcRadioPackSize_t *) buf);
    int16_t r = readExactly(sock, buf + sizeof(PcRadioPackSize_t), len);
    return r > 0 ? r + sizeof(PcRadioPackSize_t) : r;
}

canonical_original_code:
short pcRadioRecvPack(int sock, void *buf, unsigned short bufLen) {
    if (readExactly(sock, buf, sizeof(PcRadioPackSize_t))
            != sizeof(PcRadioPackSize_t)) return -1;
    unsigned short len = *((PcRadioPackSize_t *) buf);
    short r = readExactly(sock, buf + sizeof(PcRadioPackSize_t), len);
    return r > 0 ? r + sizeof(PcRadioPackSize_t) : r;
}

variable_types:
bufLen
unsigned short

variable_types:
len
unsigned short

variable_types: r short


pcRadioSend

decompiled_code:
__int64 __fastcall pcRadioSend(unsigned int a1, const void *a2, unsigned __int16 a3)
{
unsigned int v3; // er12
unsigned int v5; // eax
int *v6; // rax
char *v7; // rax

v3 = 0;
if ( !a3 )
return v3;
v5 = write(a1, a2, a3);
v3 = v5;
if ( !(_WORD)v5 )
{
v3 = -1;
if ( print_buf )
{
debugPrintfFormat("socket %i disconnected\n", a1);
serialPrint(print_buf);
}
return v3;
}
if ( (v5 & 0x8000u) == 0 || !print_buf )
return v3;
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("socket %i writing error: %s\n", a1, v7);
serialPrint(print_buf);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, const void *a2, unsigned __int16 a3)
{
unsigned int v3; // er12
unsigned int v5; // eax
int *v6; // rax
char *v7; // rax

v3 = 0;
if ( !a3 )
return v3;
v5 = write(a1, a2, a3);
v3 = v5;
if ( !(_WORD)v5 )
{
v3 = -1;
if ( print_buf )
{
debugPrintfFormat("socket %i disconnected\n", a1);
serialPrint(print_buf);
}
return v3;
}
if ( (v5 & 0x8000u) == 0 || !print_buf )
return v3;
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("socket %i writing error: %s\n", a1, v7);
serialPrint(print_buf);
return v3;
}

original_code:
int16_t pcRadioSend(int sock, const void *buf, uint16_t bufLen) {
    if (bufLen == 0) return 0;
    int16_t l = write(sock, buf, bufLen);
    if (l == 0) {
        if (2 == 1) { serialPrintV("socket %i disconnected\n", sock); } else { if (_print_buf) { debugPrintfFormat("socket %i disconnected\n", sock); serialPrint(_print_buf); }; };
        return -1;
    }
    if (l < 0) {
        if (3 == 1) { serialPrintV("socket %i writing error: %s\n", sock, strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("socket %i writing error: %s\n", sock, strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
    }
    return l;
}

canonical_original_code:
short pcRadioSend(int sock, void *buf, unsigned short bufLen) {
    if (bufLen == 0) return 0;
    short l = write(sock, buf, bufLen);
    if (l == 0) {
        if (2 == 1) { serialPrintV("socket %i disconnected\n", sock); } else { if (_print_buf) { debugPrintfFormat("socket %i disconnected\n", sock); serialPrint(_print_buf); }; };
        return -1;
    }
    if (l < 0) {
        if (3 == 1) { serialPrintV("socket %i writing error: %s\n", sock, strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("socket %i writing error: %s\n", sock, strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
    }
    return l;
}


connectSock

decompiled_code:
__int64 __fastcall connectSock(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int v9; // edi
int *v10; // rax
char *v11; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v13[2]; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 >= 0 )
{
optval = 1;
if ( setsockopt(v1, 1, 2, &optval, 4u) )
{
if ( print_buf )
{
v7 = _errno_location();
v8 = strerror(*v7);
debugPrintfFormat("cannot set socket to reuse addr: %s\n", v8);
serialPrint(print_buf);
}
}
else
{
*(_QWORD *)&v13[0].sa_data[6] = 0LL;
v13[0].sa_family = 2;
*(_QWORD *)&v13[0].sa_data[2] = (unsigned int)_bswap_32_constprop_0(a1, v13, 16LL);
*(_WORD *)v13[0].sa_data = _bswap_16(a1);
if ( connect(v2, v13, v3) >= 0 )
return v2;
if ( print_buf )
{
v10 = _errno_location();
v11 = strerror(*v10);
debugPrintfFormat("can not connect to cloud: %s\n", v11);
serialPrint(print_buf);
}
radioOff();
}
v9 = v2;
v2 = -1;
close(v9);
return v2;
}
if ( print_buf )
{
v5 = _errno_location();
v6 = strerror(*v5);
debugPrintfFormat("error = %s\n", v6);
serialPrint(print_buf);
}
radioOff();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int v9; // edi
int *v10; // rax
char *v11; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v13[2]; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 >= 0 )
{
optval = 1;
if ( setsockopt(v1, 1, 2, &optval, 4u) )
{
if ( print_buf )
{
v7 = _errno_location();
v8 = strerror(*v7);
debugPrintfFormat("cannot set socket to reuse addr: %s\n", v8);
serialPrint(print_buf);
}
}
else
{
*(_QWORD *)&v13[0].sa_data[6] = 0LL;
v13[0].sa_family = 2;
*(_QWORD *)&v13[0].sa_data[2] = (unsigned int)func3(a1, v13, 16LL);
*(_WORD *)v13[0].sa_data = func0(a1);
if ( connect(v2, v13, v3) >= 0 )
return v2;
if ( print_buf )
{
v10 = _errno_location();
v11 = strerror(*v10);
debugPrintfFormat("can not connect to cloud: %s\n", v11);
serialPrint(print_buf);
}
func1();
}
v9 = v2;
v2 = -1;
close(v9);
return v2;
}
if ( print_buf )
{
v5 = _errno_location();
v6 = strerror(*v5);
debugPrintfFormat("error = %s\n", v6);
serialPrint(print_buf);
}
func1();
return v2;
}

original_code:
int connectSock(unsigned port) {
    int sock = socket(2, SOCK_STREAM, 0);
    if (sock < 0)
    {
        if (2 == 1) { serialPrintV("error = %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("error = %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        radioOff();
        return sock;
    }
    int on = 1;
    if (setsockopt(sock, 1, 2, &on, sizeof(on)))
    {
        if (2 == 1) { serialPrintV("cannot set socket to reuse addr: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("cannot set socket to reuse addr: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        close(sock);
        return -1;
    }
    struct sockaddr_in proxyAddr;
    memset((char *) &proxyAddr, 0, sizeof(proxyAddr));
    proxyAddr.sin_family = 2;
    proxyAddr.sin_addr.s_addr = htonl(LOOPBACK_ADDR);
    proxyAddr.sin_port = htons(port);
    if (connect(sock, (struct sockaddr *) &proxyAddr, sizeof(proxyAddr)) < 0)
    {
        if (2 == 1) { serialPrintV("can not connect to cloud: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("can not connect to cloud: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        radioOff();
        close(sock);
        return -1;
    }
    return sock;
}

canonical_original_code:
int connectSock(unsigned int port) {
    int sock = socket(2, SOCK_STREAM, 0);
    if (sock < 0)
    {
        if (2 == 1) { serialPrintV("error = %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("error = %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        radioOff();
        return sock;
    }
    int on = 1;
    if (setsockopt(sock, 1, 2, &on, sizeof(on)))
    {
        if (2 == 1) { serialPrintV("cannot set socket to reuse addr: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("cannot set socket to reuse addr: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        close(sock);
        return -1;
    }
    struct sockaddr_in proxyAddr;
    memset((char *) &proxyAddr, 0, sizeof(proxyAddr));
    proxyAddr.sin_family = 2;
    proxyAddr.sin_addr.s_addr = htonl(LOOPBACK_ADDR);
    proxyAddr.sin_port = htons(port);
    if (connect(sock, (struct sockaddr *) &proxyAddr, sizeof(proxyAddr)) < 0)
    {
        if (2 == 1) { serialPrintV("can not connect to cloud: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("can not connect to cloud: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        radioOff();
        close(sock);
        return -1;
    }
    return sock;
}

variable_types:
port
unsigned int

variable_types: on int

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

return_type: int

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

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


makeSocketPair

decompiled_code:
__int64 makeSocketPair()
{
if ( socketpair(1, 1, 0, sockets) )
return 0xFFFFFFFFLL;
mosSendSock = sockets[0];
mosSendPairSock = dword_67C;
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
if ( socketpair(1, 1, 0, sockets) )
return 0xFFFFFFFFLL;
mosSendSock = sockets[0];
mosSendPairSock = dword_67C;
return 0LL;
}

original_code:
int8_t makeSocketPair() {
    if (socketpair(1, SOCK_STREAM, 0, sockets) != 0) return -1;
    mosSendSock = sockets[0];
    mosSendPairSock = sockets[1];
    return 0;
}

canonical_original_code:
char makeSocketPair() {
    if (socketpair(1, SOCK_STREAM, 0, sockets) != 0) return -1;
    mosSendSock = sockets[0];
    mosSendPairSock = sockets[1];
    return 0;
}

return_type: char


intHandler

decompiled_code:
void *__fastcall intHandler(void *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
int v4; // eax
__int16 v5; // ax
int *v6; // rax
char *v7; // rax
__int16 v8; // ax
__int16 v9; // ax
unsigned __int16 v11; // ax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( !(unsigned __int8)makeSocketPair(a1) )
{
v1 = connectSock(0x1895u);
cloudSock = v1;
v2 = v1;
if ( v1 != -1 )
{
len = 16;
if ( !getsockname(v1, &addr, &len) )
{
v11 = _bswap_16(*(__int16 *)addr.sa_data);
localAddress = v11;
if ( print_buf )
{
debugPrintfFormat("set local address to port number: 0x%04x\n", v11);
serialPrint(print_buf);
v2 = cloudSock;
}
}
}
v3 = mosSendPairSock;
pollFd.fd = v2;
pollFd.events = 1;
*((_DWORD *)&pollFd + 2) = v3;
*((_WORD *)&pollFd + 6) = 1;
while ( 1 )
{
while ( 1 )
{
v4 = poll(&pollFd, 2uLL, -1);
if ( v4 > 0 )
break;
if ( v4 && print_buf )
{
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("poll error: %s\n", v7);
serialPrint(print_buf);
}
}
if ( (pollFd.revents & 1) == 0 )
goto LABEL_6;
v8 = pcRadioRecvPack(cloudSock, &pcRadioBuf);
if ( v8 < 0 )
return 0LL;
pcRadioBufLen = v8;
if ( pcRadioIsOn && pcRadioCallback )
{
pcRadioCallback();
if ( (*((_BYTE *)&pollFd + 14) & 1) != 0 )
{
LABEL_18:
v9 = pcRadioRecvPack(mosSendPairSock, mosOutBuf);
if ( v9 <= 0 )
{
if ( v9 )
return 0LL;
}
else
{
mosOutBufOffset = 0;
pollFd.events = 4;
mosOutBufLen = v9;
}
goto LABEL_7;
}
}
else
{
LABEL_6:
if ( (*((_BYTE *)&pollFd + 14) & 1) != 0 )
goto LABEL_18;
}
LABEL_7:
if ( (pollFd.revents & 4) != 0 )
{
v5 = pcRadioSend(cloudSock, (char *)mosOutBuf + (unsigned int)mosOutBufOffset, mosOutBufLen);
if ( v5 <= 0 )
{
if ( v5 )
return 0LL;
}
else
{
mosOutBufLen -= v5;
if ( mosOutBufLen )
mosOutBufOffset += v5;
else
pollFd.events = 1;
}
}
}
}
serialPrintV_constprop_0();
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func10(void *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
int v4; // eax
__int16 v5; // ax
int *v6; // rax
char *v7; // rax
__int16 v8; // ax
__int16 v9; // ax
unsigned __int16 v11; // ax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( !(unsigned __int8)func9(a1) )
{
v1 = func8(0x1895u);
cloudSock = v1;
v2 = v1;
if ( v1 != -1 )
{
len = 16;
if ( !getsockname(v1, &addr, &len) )
{
v11 = func0(*(__int16 *)addr.sa_data);
localAddress = v11;
if ( print_buf )
{
debugPrintfFormat("set local address to port number: 0x%04x\n", v11);
serialPrint(print_buf);
v2 = cloudSock;
}
}
}
v3 = mosSendPairSock;
pollFd.fd = v2;
pollFd.events = 1;
*((_DWORD *)&pollFd + 2) = v3;
*((_WORD *)&pollFd + 6) = 1;
while ( 1 )
{
while ( 1 )
{
v4 = poll(&pollFd, 2uLL, -1);
if ( v4 > 0 )
break;
if ( v4 && print_buf )
{
v6 = _errno_location();
v7 = strerror(*v6);
debugPrintfFormat("poll error: %s\n", v7);
serialPrint(print_buf);
}
}
if ( (pollFd.revents & 1) == 0 )
goto LABEL_6;
v8 = func6(cloudSock, &pcRadioBuf);
if ( v8 < 0 )
return 0LL;
pcRadioBufLen = v8;
if ( pcRadioIsOn && pcRadioCallback )
{
pcRadioCallback();
if ( (*((_BYTE *)&pollFd + 14) & 1) != 0 )
{
LABEL_18:
v9 = func6(mosSendPairSock, mosOutBuf);
if ( v9 <= 0 )
{
if ( v9 )
return 0LL;
}
else
{
mosOutBufOffset = 0;
pollFd.events = 4;
mosOutBufLen = v9;
}
goto LABEL_7;
}
}
else
{
LABEL_6:
if ( (*((_BYTE *)&pollFd + 14) & 1) != 0 )
goto LABEL_18;
}
LABEL_7:
if ( (pollFd.revents & 4) != 0 )
{
v5 = func7(cloudSock, (char *)mosOutBuf + (unsigned int)mosOutBufOffset, mosOutBufLen);
if ( v5 <= 0 )
{
if ( v5 )
return 0LL;
}
else
{
mosOutBufLen -= v5;
if ( mosOutBufLen )
mosOutBufOffset += v5;
else
pollFd.events = 1;
}
}
}
}
func2();
return 0LL;
}

original_code:
void *intHandler(void *dummy) {
    if (makeSocketPair() != 0) {
        if (1 == 1) { serialPrintV("cannot create outgoing socket pair!\n"); } else { if (_print_buf) { debugPrintfFormat("cannot create outgoing socket pair!\n"); serialPrint(_print_buf); }; };
        return ((void *)0);
    }
    cloudSock = connectSock(PROXY_SERVER_PORT);
    if (cloudSock != -1) {
        struct sockaddr_in saddr;
        socklen_t slen = sizeof(saddr);
        if (getsockname(cloudSock, (struct sockaddr *) &saddr, &slen) == 0) {
            localAddress = htons(saddr.sin_port);
            if (2 == 1) { serialPrintV("set local address to port number: 0x%04x\n", localAddress); } else { if (_print_buf) { debugPrintfFormat("set local address to port number: 0x%04x\n", localAddress); serialPrint(_print_buf); }; };
        }
    }
    pollFd[0].fd = cloudSock;
    pollFd[0].events = 0x001;
    pollFd[1].fd = mosSendPairSock;
    pollFd[1].events = 0x001;
    while (1) {
        int p = poll(pollFd, 2, -1);
        if (p > 0) {
            if (pollFd[0].revents & 0x001) {
                int16_t l = pcRadioRecvPack(cloudSock, pcRadioBuf,
                        sizeof(pcRadioBuf));
                if (l < 0) {
                    return ((void *)0);
                }
                pcRadioBufLen = l;
                if (pcRadioIsOn && pcRadioCallback) {
                    pcRadioCallback();
                }
            }
            if (pollFd[1].revents & 0x001) {
                int16_t l = pcRadioRecvPack(mosSendPairSock, mosOutBuf,
                        sizeof(mosOutBuf));
                if (l > 0) {
                    pollFd[0].events = 0x004;
                    mosOutBufOffset = 0;
                    mosOutBufLen = l;
                } else if (l < 0) {
                    return ((void *)0);
                }
            }
            if (pollFd[0].revents & 0x004) {
                int16_t l = pcRadioSend(cloudSock, mosOutBuf + mosOutBufOffset,
                        mosOutBufLen);
                if (l > 0) {
                    mosOutBufLen -= l;
                    if (mosOutBufLen == 0) {
                        pollFd[0].events = 0x001;
                    } else {
                        mosOutBufOffset += l;
                    }
                } else if (l < 0) {
                    return ((void *)0);
                } else {
                }
            }
        } else if (p == 0) {
        } else {
            if (2 == 1) { serialPrintV("poll error: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("poll error: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        }
    }
    return ((void *)0);
}

canonical_original_code:
void *intHandler(void *dummy) {
    if (makeSocketPair() != 0) {
        if (1 == 1) { serialPrintV("cannot create outgoing socket pair!\n"); } else { if (_print_buf) { debugPrintfFormat("cannot create outgoing socket pair!\n"); serialPrint(_print_buf); }; };
        return ((void *)0);
    }
    cloudSock = connectSock(PROXY_SERVER_PORT);
    if (cloudSock != -1) {
        struct sockaddr_in saddr;
        unsigned int slen = sizeof(saddr);
        if (getsockname(cloudSock, (struct sockaddr *) &saddr, &slen) == 0) {
            localAddress = htons(saddr.sin_port);
            if (2 == 1) { serialPrintV("set local address to port number: 0x%04x\n", localAddress); } else { if (_print_buf) { debugPrintfFormat("set local address to port number: 0x%04x\n", localAddress); serialPrint(_print_buf); }; };
        }
    }
    pollFd[0].fd = cloudSock;
    pollFd[0].events = 0x001;
    pollFd[1].fd = mosSendPairSock;
    pollFd[1].events = 0x001;
    while (1) {
        int p = poll(pollFd, 2, -1);
        if (p > 0) {
            if (pollFd[0].revents & 0x001) {
                short l = pcRadioRecvPack(cloudSock, pcRadioBuf,
                        sizeof(pcRadioBuf));
                if (l < 0) {
                    return ((void *)0);
                }
                pcRadioBufLen = l;
                if (pcRadioIsOn && pcRadioCallback) {
                    pcRadioCallback();
                }
            }
            if (pollFd[1].revents & 0x001) {
                short l = pcRadioRecvPack(mosSendPairSock, mosOutBuf,
                        sizeof(mosOutBuf));
                if (l > 0) {
                    pollFd[0].events = 0x004;
                    mosOutBufOffset = 0;
                    mosOutBufLen = l;
                } else if (l < 0) {
                    return ((void *)0);
                }
            }
            if (pollFd[0].revents & 0x004) {
                short l = pcRadioSend(cloudSock, mosOutBuf + mosOutBufOffset,
                        mosOutBufLen);
                if (l > 0) {
                    mosOutBufLen -= l;
                    if (mosOutBufLen == 0) {
                        pollFd[0].events = 0x001;
                    } else {
                        mosOutBufOffset += l;
                    }
                } else if (l < 0) {
                    return ((void *)0);
                } else {
                }
            }
        } else if (p == 0) {
        } else {
            if (2 == 1) { serialPrintV("poll error: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("poll error: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
        }
    }
    return ((void *)0);
}

variable_types:
dummy
void *

variable_types:
slen
unsigned int

variable_types: p int

return_type: void *


closeSocket

decompiled_code:
int __fastcall closeSocket(int a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax

result = close(a1);
if ( result < 0 )
{
if ( print_buf )
{
v2 = _errno_location();
v3 = strerror(*v2);
debugPrintfFormat("error closing socket: %s\n", v3);
result = serialPrint(print_buf);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int result; // eax
int *v2; // rax
char *v3; // rax

result = close(a1);
if ( result < 0 )
{
if ( print_buf )
{
v2 = _errno_location();
v3 = strerror(*v2);
debugPrintfFormat("error closing socket: %s\n", v3);
result = serialPrint(print_buf);
}
}
return result;
}

original_code:
void closeSocket(int sock) {
    if (close(sock) < 0) {
        if (2 == 1) { serialPrintV("error closing socket: %s\n", strerror((*__errno_location ()))); } else { if (_print_buf) { debugPrintfFormat("error closing socket: %s\n", strerror((*__errno_location ()))); serialPrint(_print_buf); }; };
    } else {
    }
}


networkingInitArch

decompiled_code:
int networkingInitArch()
{
mos_sem_init(&alarmMutex, 0LL);
return pthread_create(&intThread, 0LL, intHandler, 0LL);
}

canonical_decompiled_code:
int func12()
{
mos_sem_init(&alarmMutex, 0LL);
return pthread_create(&intThread, 0LL, intHandler, 0LL);
}

original_code:
void networkingInitArch(void)
{
    mos_sem_init(&alarmMutex, 0);
    pthread_create(&intThread, ((void *)0), intHandler, ((void *)0));
}

canonical_original_code:
void networkingInitArch()
{
    mos_sem_init(&alarmMutex, 0);
    pthread_create(&intThread, ((void *)0), intHandler, ((void *)0));
}


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


redLedToggle

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

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

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

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

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

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


doMsleep

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


printBuf

decompiled_code:
__int64 printBuf()
{
__int64 result; // rax
__int64 v1; // [rsp-18h] [rbp-18h]

if ( print_buf )
{
debugPrintfFormat(
"%i-%i-%i %i:%i:%i\t",
(unsigned __int16)fix >> 9,
(*(_QWORD *)&fix >> 5) & 0xFLL,
fix & 0x1F,
(unsigned __int8)byte_2F2,
(unsigned __int8)byte_2F3,
(unsigned __int8)byte_2F4);
serialPrint(print_buf);
result = v1;
if ( print_buf )
{
debugPrintfFormat("%i %i.%i\t", (unsigned __int8)byte_2F6, (unsigned __int8)byte_2F7, (unsigned __int16)word_2F8);
result = serialPrint(print_buf);
if ( print_buf )
{
debugPrintfFormat(
"%i %i.%i\t",
(unsigned __int8)byte_2FA,
(unsigned __int8)byte_2FB,
(unsigned __int16)word_2FC);
result = serialPrint(print_buf);
if ( print_buf )
{
debugPrintfFormat(
"%i %i %i\n",
(*(_QWORD *)&fix >> 45) & 7LL,
(*(_QWORD *)&fix >> 40) & 3LL,
(*(_QWORD *)&fix >> 42) & 7LL);
result = serialPrint(print_buf);
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
__int64 v1; // [rsp-18h] [rbp-18h]

if ( print_buf )
{
debugPrintfFormat(
"%i-%i-%i %i:%i:%i\t",
(unsigned __int16)fix >> 9,
(*(_QWORD *)&fix >> 5) & 0xFLL,
fix & 0x1F,
(unsigned __int8)byte_2F2,
(unsigned __int8)byte_2F3,
(unsigned __int8)byte_2F4);
serialPrint(print_buf);
result = v1;
if ( print_buf )
{
debugPrintfFormat("%i %i.%i\t", (unsigned __int8)byte_2F6, (unsigned __int8)byte_2F7, (unsigned __int16)word_2F8);
result = serialPrint(print_buf);
if ( print_buf )
{
debugPrintfFormat(
"%i %i.%i\t",
(unsigned __int8)byte_2FA,
(unsigned __int8)byte_2FB,
(unsigned __int16)word_2FC);
result = serialPrint(print_buf);
if ( print_buf )
{
debugPrintfFormat(
"%i %i %i\n",
(*(_QWORD *)&fix >> 45) & 7LL,
(*(_QWORD *)&fix >> 40) & 3LL,
(*(_QWORD *)&fix >> 42) & 7LL);
result = serialPrint(print_buf);
}
}
}
}
return result;
}

original_code:
void printBuf(void *buf)
{
    if (7 == 1) { serialPrintV("%i-%i-%i %i:%i:%i\t", fix.d.year, fix.d.mon, fix.d.day, fix.h, fix.m, fix.s); } else { if (_print_buf) { debugPrintfFormat("%i-%i-%i %i:%i:%i\t", fix.d.year, fix.d.mon, fix.d.day, fix.h, fix.m, fix.s); serialPrint(_print_buf); }; };
    if (4 == 1) { serialPrintV("%i %i.%i\t", fix.lat.deg, fix.lat.min, fix.lat.mindec); } else { if (_print_buf) { debugPrintfFormat("%i %i.%i\t", fix.lat.deg, fix.lat.min, fix.lat.mindec); serialPrint(_print_buf); }; };
    if (4 == 1) { serialPrintV("%i %i.%i\t", fix.lon.deg, fix.lon.min, fix.lon.mindec); } else { if (_print_buf) { debugPrintfFormat("%i %i.%i\t", fix.lon.deg, fix.lon.min, fix.lon.mindec); serialPrint(_print_buf); }; };
    if (4 == 1) { serialPrintV("%i %i %i\n", fix.q.satcnt, fix.q.fix, fix.q.dop); } else { if (_print_buf) { debugPrintfFormat("%i %i %i\n", fix.q.satcnt, fix.q.fix, fix.q.dop); serialPrint(_print_buf); }; };
}


charRecv

decompiled_code:
// attributes: thunk
__int64 charRecv()
{
return redLedToggle();
}

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

original_code:
void charRecv(uint8_t b) {
    redLedToggle();
}

canonical_original_code:
void charRecv(unsigned char b) {
    redLedToggle();
}

variable_types:
b
unsigned char


doMsleep

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

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

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

variable_types:
milliseconds
unsigned short


decompiled_code:
int __fastcall mos_sem_init(sem_t *a1, char a2)
{
return sem_init(a1, 0, a2);
}

canonical_decompiled_code:
int __fastcall func1(sem_t *a1, char a2)
{
return sem_init(a1, 0, a2);
}

original_code:
void mos_sem_init(sem_t *s, int8_t value)
{
    sem_init(s,0,value);
}

canonical_original_code:
void mos_sem_init(union sem_t *s, char value)
{
    sem_init(s,0,value);
}

variable_types: union sem_t { char __size[32]; long __align; } *

variable_types: value char


decompiled_code:
// attributes: thunk
int mos_sem_destroy(sem_t *sem)
{
return sem_destroy(sem);
}

canonical_decompiled_code:
int func2(sem_t *sem)
{
return sem_destroy(sem);
}

original_code:
void mos_sem_destroy(sem_t *s)
{
    sem_destroy(s);
}

canonical_original_code:
void mos_sem_destroy(union sem_t *s)
{
    sem_destroy(s);
}

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
bool __fastcall sem_try_wait(sem_t *a1)
{
return sem_trywait(a1) != 0;
}

canonical_decompiled_code:
bool __fastcall func3(sem_t *a1)
{
return sem_trywait(a1) != 0;
}

original_code:
uint8_t sem_try_wait(sem_t *s)
{
    if (sem_trywait(s)) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
unsigned char sem_try_wait(union sem_t *s)
{
    if (sem_trywait(s)) {
        return 1;
    } else {
        return 0;
    }
}

return_type: unsigned char


decompiled_code:
// attributes: thunk
int mos_sem_wait(sem_t *sem)
{
return sem_wait(sem);
}

canonical_decompiled_code:
int func4(sem_t *sem)
{
return sem_wait(sem);
}

original_code:
void mos_sem_wait(sem_t *s)
{
    sem_wait(s);
}

canonical_original_code:
void mos_sem_wait(union sem_t *s)
{
    sem_wait(s);
}


decompiled_code:
// attributes: thunk
int mos_sem_post(sem_t *sem)
{
return sem_post(sem);
}

canonical_decompiled_code:
int func5(sem_t *sem)
{
return sem_post(sem);
}

original_code:
void mos_sem_post(sem_t *s)
{
    sem_post(s);
}

canonical_original_code:
void mos_sem_post(union sem_t *s)
{
    sem_post(s);
}


decompiled_code:
__int64 __fastcall mos_sem_get_val(sem_t *a1)
{
int v1; // er8
__int64 result; // rax
int sval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = sem_getvalue(a1, sval);
result = LOBYTE(sval[0]);
if ( v1 )
{
perror("sem_getvalue");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(sem_t *a1)
{
int v1; // er8
__int64 result; // rax
int sval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = sem_getvalue(a1, sval);
result = LOBYTE(sval[0]);
if ( v1 )
{
perror("sem_getvalue");
result = 0LL;
}
return result;
}

original_code:
int8_t mos_sem_get_val(sem_t *s)
{
    int val;
    if (sem_getvalue(s, &val)) {
        perror("sem_getvalue");
        return 0;
    }
    return val;
}

canonical_original_code:
char mos_sem_get_val(union sem_t *s)
{
    int val;
    if (sem_getvalue(s, &val)) {
        perror("sem_getvalue");
        return 0;
    }
    return val;
}

variable_types: val int


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


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

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


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 func6(unsigned __int16 a1)
{
return usleep(1000 * a1);
}

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


eraseData

decompiled_code:
__int64 eraseData()
{
serialPrintV("erasing flash...");
extFlashEraseSector(4294901779LL);
serialPrintV("+");
extFlashEraseSector(19LL);
serialPrintV("+");
extFlashEraseSector(65555LL);
return serialPrint("+ DONE\n");
}

canonical_decompiled_code:
__int64 func7()
{
func2("erasing flash...");
extFlashEraseSector(4294901779LL);
func2("+");
extFlashEraseSector(19LL);
func2("+");
extFlashEraseSector(65555LL);
return serialPrint("+ DONE\n");
}

original_code:
void eraseData() {
    if (1 == 1) { serialPrintV("erasing flash..."); } else { if (_print_buf) { debugPrintfFormat("erasing flash..."); serialPrint(_print_buf); }; };
    extFlashEraseSector(START_ADDR - 0x10000);
    if (1 == 1) { serialPrintV("+"); } else { if (_print_buf) { debugPrintfFormat("+"); serialPrint(_print_buf); }; };
    extFlashEraseSector(START_ADDR);
    if (1 == 1) { serialPrintV("+"); } else { if (_print_buf) { debugPrintfFormat("+"); serialPrint(_print_buf); }; };
    extFlashEraseSector(START_ADDR + 0x10000);
    serialPrint("+ DONE" "\n");
}


eraseAllBySector

decompiled_code:
__int64 eraseAllBySector()
{
__int64 i; // rax
char v1; // dl
unsigned int v2; // ebx

serialPrint("erasing all by sector\n\n");
for ( i = 0LL; i != 320; byte_51F[i] = v1 )
v1 = i++ + 12;
c = 76;
v2 = 13;
::i = 320;
do
{
extFlashEraseSector(v2);
extFlashWrite(v2, &buf, 10LL);
*((_WORD *)&data + 4) = 0;
data = 0LL;
extFlashRead(v2, &data, 10LL);
if ( buf == data )
goto LABEL_6;
do
{
assertionFailed("!memcmp(buf, data, 10)", "main.c", 78LL);
LABEL_6:
;
}
while ( word_528 != *((_WORD *)&data + 4) );
v2 += 11177;
}
while ( v2 != 1005943 );
return serialPrint("..done\n\n");
}

canonical_decompiled_code:
__int64 func8()
{
__int64 i; // rax
char v1; // dl
unsigned int v2; // ebx

serialPrint("erasing all by sector\n\n");
for ( i = 0LL; i != 320; byte_51F[i] = v1 )
v1 = i++ + 12;
c = 76;
v2 = 13;
::i = 320;
do
{
extFlashEraseSector(v2);
extFlashWrite(v2, &buf, 10LL);
*((_WORD *)&data + 4) = 0;
data = 0LL;
extFlashRead(v2, &data, 10LL);
if ( buf == data )
goto LABEL_6;
do
{
assertionFailed("!memcmp(buf, data, 10)", "main.c", 78LL);
LABEL_6:
;
}
while ( word_528 != *((_WORD *)&data + 4) );
v2 += 11177;
}
while ( v2 != 1005943 );
return serialPrint("..done\n\n");
}

original_code:
void eraseAllBySector() {
    serialPrint("erasing all by sector\n" "\n");
    for (i = 0, c = START_BYTE; i < BYTE_COUNT; ++i, ++c) {
        buf[i] = c;
    }
    uint32_t ii;
    for (ii = 13; ii < 1000000; ii += 11177) {
        extFlashEraseSector(ii);
        extFlashWrite(ii, buf, 10);
        memset(data, 0, 10);
        extFlashRead(ii, data, 10);
        ((!memcmp(buf, data, 10)) ? (void)0 : assertionFailed("!memcmp(buf, data, 10)", "main.c", 78));
    }
    serialPrint("..done\n" "\n");
}

canonical_original_code:
void eraseAllBySector() {
    serialPrint("erasing all by sector\n" "\n");
    for (i = 0, c = START_BYTE; i < BYTE_COUNT; ++i, ++c) {
        buf[i] = c;
    }
    unsigned int ii;
    for (ii = 13; ii < 1000000; ii += 11177) {
        extFlashEraseSector(ii);
        extFlashWrite(ii, buf, 10);
        memset(data, 0, 10);
        extFlashRead(ii, data, 10);
        ((!memcmp(buf, data, 10)) ? (void)0 : assertionFailed("!memcmp(buf, data, 10)", "main.c", 78));
    }
    serialPrint("..done\n" "\n");
}

variable_types:
ii
unsigned int


writeData

decompiled_code:
__int64 writeData()
{
__int64 i; // rax
char v1; // dl

for ( i = 0LL; i != 320; byte_51F[i] = v1 )
v1 = i++ + 12;
c = 76;
::i = 320;
if ( print_buf )
{
debugPrintfFormat("writing flash from %lu to %lu\n", 19LL, 337LL);
serialPrint(print_buf);
}
extFlashWrite(19LL, &buf, 318LL);
return serialPrint("done\n");
}

canonical_decompiled_code:
__int64 func9()
{
__int64 i; // rax
char v1; // dl

for ( i = 0LL; i != 320; byte_51F[i] = v1 )
v1 = i++ + 12;
c = 76;
::i = 320;
if ( print_buf )
{
debugPrintfFormat("writing flash from %lu to %lu\n", 19LL, 337LL);
serialPrint(print_buf);
}
extFlashWrite(19LL, &buf, 318LL);
return serialPrint("done\n");
}

original_code:
void writeData() {
    for (i = 0, c = START_BYTE; i < BYTE_COUNT; ++i, ++c) {
        buf[i] = c;
    }
    if (3 == 1) { serialPrintV("writing flash from %lu to %lu\n", (uint32_t)START_ADDR, (uint32_t)START_ADDR + BYTE_COUNT - 2); } else { if (_print_buf) { debugPrintfFormat("writing flash from %lu to %lu\n", (uint32_t)START_ADDR, (uint32_t)START_ADDR + BYTE_COUNT - 2); serialPrint(_print_buf); }; };
    extFlashWrite(START_ADDR, buf, BYTE_COUNT - 2);
    serialPrint("done" "\n");
}

canonical_original_code:
void writeData() {
    for (i = 0, c = START_BYTE; i < BYTE_COUNT; ++i, ++c) {
        buf[i] = c;
    }
    if (3 == 1) { serialPrintV("writing flash from %lu to %lu\n", (unsigned int)START_ADDR, (unsigned int)START_ADDR + BYTE_COUNT - 2); } else { if (_print_buf) { debugPrintfFormat("writing flash from %lu to %lu\n", (unsigned int)START_ADDR, (unsigned int)START_ADDR + BYTE_COUNT - 2); serialPrint(_print_buf); }; };
    extFlashWrite(START_ADDR, buf, BYTE_COUNT - 2);
    serialPrint("done" "\n");
}


writeDataSpeedTest

decompiled_code:
__int64 writeDataSpeedTest()
{
unsigned int v0; // ebx
int v1; // ebp
__int64 v2; // rdi
__int64 result; // rax
int v4; // eax

v0 = 19;
v1 = getTimeMs();
do
{
v2 = v0;
v0 += 100;
result = extFlashWrite(v2, &buf, 100LL);
}
while ( v0 != 65619 );
if ( print_buf )
{
v4 = getTimeMs();
debugPrintfFormat("to write 64 kb: %lu ms \n", (unsigned int)(v4 - v1));
result = serialPrint(print_buf);
}
return result;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // ebx
int v1; // ebp
__int64 v2; // rdi
__int64 result; // rax
int v4; // eax

v0 = 19;
v1 = func1();
do
{
v2 = v0;
v0 += 100;
result = extFlashWrite(v2, &buf, 100LL);
}
while ( v0 != 65619 );
if ( print_buf )
{
v4 = func1();
debugPrintfFormat("to write 64 kb: %lu ms \n", (unsigned int)(v4 - v1));
result = serialPrint(print_buf);
}
return result;
}

original_code:
void writeDataSpeedTest() {
    uint32_t startTime = getTimeMs();
    uint32_t i;
    for (i = 0; i < 0x10000; i += 100) {
        extFlashWrite(START_ADDR + i, buf, 100);
    }
    if (2 == 1) { serialPrintV("to write 64 kb: %lu ms \n", getTimeMs() - startTime); } else { if (_print_buf) { debugPrintfFormat("to write 64 kb: %lu ms \n", getTimeMs() - startTime); serialPrint(_print_buf); }; };
}

canonical_original_code:
void writeDataSpeedTest() {
    unsigned int startTime = getTimeMs();
    unsigned int i;
    for (i = 0; i < 0x10000; i += 100) {
        extFlashWrite(START_ADDR + i, buf, 100);
    }
    if (2 == 1) { serialPrintV("to write 64 kb: %lu ms \n", getTimeMs() - startTime); } else { if (_print_buf) { debugPrintfFormat("to write 64 kb: %lu ms \n", getTimeMs() - startTime); serialPrint(_print_buf); }; };
}

variable_types:
startTime
unsigned int

variable_types:
i
unsigned int


printResults

decompiled_code:
__int64 printResults()
{
__int64 result; // rax
int v1; // eax

memset(&buf, 0, 0x140uLL);
serialPrintV("reading flash...");
extFlashRead(19LL, &buf, 320LL);
serialPrint("done\n");
i32 = 19;
i = 0;
LOWORD(result) = 0;
do
{
if ( print_buf )
{
debugPrintfFormat("[0x%lx] = %u\t", (unsigned int)i32, *((unsigned __int8 *)&buf + (unsigned __int16)result));
serialPrint(print_buf);
}
v1 = (unsigned __int16)i;
if ( (i & 3) == 3 )
{
serialPrint("\n");
v1 = (unsigned __int16)i;
}
result = (unsigned int)(v1 + 1);
++i32;
i = result;
}
while ( (unsigned __int16)result <= 0x13Fu );
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 result; // rax
int v1; // eax

memset(&buf, 0, 0x140uLL);
func2("reading flash...");
extFlashRead(19LL, &buf, 320LL);
serialPrint("done\n");
i32 = 19;
i = 0;
LOWORD(result) = 0;
do
{
if ( print_buf )
{
debugPrintfFormat("[0x%lx] = %u\t", (unsigned int)i32, *((unsigned __int8 *)&buf + (unsigned __int16)result));
serialPrint(print_buf);
}
v1 = (unsigned __int16)i;
if ( (i & 3) == 3 )
{
serialPrint("\n");
v1 = (unsigned __int16)i;
}
result = (unsigned int)(v1 + 1);
++i32;
i = result;
}
while ( (unsigned __int16)result <= 0x13Fu );
return result;
}

original_code:
void printResults() {
    memset(buf, 0, BYTE_COUNT);
    if (1 == 1) { serialPrintV("reading flash..."); } else { if (_print_buf) { debugPrintfFormat("reading flash..."); serialPrint(_print_buf); }; };
    extFlashRead(START_ADDR, buf, BYTE_COUNT);
    serialPrint("done" "\n");
    for (i = 0, i32 = START_ADDR; i < BYTE_COUNT; ++i, ++i32) {
        if (3 == 1) { serialPrintV("[0x%lx] = %u\t", i32, buf[i]); } else { if (_print_buf) { debugPrintfFormat("[0x%lx] = %u\t", i32, buf[i]); serialPrint(_print_buf); }; };
        if (i % 4 == 3) serialPrint("" "\n");
    }
}


writeSimpleData

decompiled_code:
__int64 writeSimpleData()
{
LODWORD(buf) = 67305985;
i = 4;
serialPrintV("writing flash...");
extFlashWrite(19LL, &buf, 4LL);
return serialPrint("done\n");
}

canonical_decompiled_code:
__int64 func12()
{
LODWORD(buf) = 67305985;
i = 4;
func2("writing flash...");
extFlashWrite(19LL, &buf, 4LL);
return serialPrint("done\n");
}

original_code:
void writeSimpleData() {
    for (i = 0; i < 4; ++i) {
        buf[i] = i + 1;
    }
    if (1 == 1) { serialPrintV("writing flash..."); } else { if (_print_buf) { debugPrintfFormat("writing flash..."); serialPrint(_print_buf); }; };
    extFlashWrite(START_ADDR, buf, 4);
    serialPrint("done" "\n");
}


readData

decompiled_code:
__int64 readData()
{
__int64 v0; // rsi
unsigned __int16 i; // ax

memset(&buf, 0, 0x140uLL);
serialPrintV("reading flash...");
extFlashRead(19LL, &buf, 4LL);
serialPrint("done\n");
v0 = 19LL;
::i = 0;
for ( i = 0; i <= 3u; ::i = i )
{
if ( print_buf )
{
debugPrintfFormat("[0x%lx] = %u\t", v0, *((unsigned __int8 *)&buf + i));
serialPrint(print_buf);
v0 = (unsigned int)(v0 + 1);
}
i = ::i + 1;
}
return serialPrint("\n");
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rsi
unsigned __int16 i; // ax

memset(&buf, 0, 0x140uLL);
func2("reading flash...");
extFlashRead(19LL, &buf, 4LL);
serialPrint("done\n");
v0 = 19LL;
::i = 0;
for ( i = 0; i <= 3u; ::i = i )
{
if ( print_buf )
{
debugPrintfFormat("[0x%lx] = %u\t", v0, *((unsigned __int8 *)&buf + i));
serialPrint(print_buf);
v0 = (unsigned int)(v0 + 1);
}
i = ::i + 1;
}
return serialPrint("\n");
}

original_code:
void readData() {
    memset(buf, 0, BYTE_COUNT);
    if (1 == 1) { serialPrintV("reading flash..."); } else { if (_print_buf) { debugPrintfFormat("reading flash..."); serialPrint(_print_buf); }; };
    extFlashRead(START_ADDR, buf, 4);
    serialPrint("done" "\n");
    uint32_t addr = START_ADDR;
    for (i = 0; i < 4; ++i) {
        if (3 == 1) { serialPrintV("[0x%lx] = %u\t", addr++, buf[i]); } else { if (_print_buf) { debugPrintfFormat("[0x%lx] = %u\t", addr++, buf[i]); serialPrint(_print_buf); }; };
    }
    serialPrint("" "\n");
}

canonical_original_code:
void readData() {
    memset(buf, 0, BYTE_COUNT);
    if (1 == 1) { serialPrintV("reading flash..."); } else { if (_print_buf) { debugPrintfFormat("reading flash..."); serialPrint(_print_buf); }; };
    extFlashRead(START_ADDR, buf, 4);
    serialPrint("done" "\n");
    unsigned int addr = START_ADDR;
    for (i = 0; i < 4; ++i) {
        if (3 == 1) { serialPrintV("[0x%lx] = %u\t", addr++, buf[i]); } else { if (_print_buf) { debugPrintfFormat("[0x%lx] = %u\t", addr++, buf[i]); serialPrint(_print_buf); }; };
    }
    serialPrint("" "\n");
}

variable_types:
addr
unsigned int


doMsleep

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

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

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


decompiled_code:
void __noreturn alarmTimerInterrupt0()
{
++jiffies;
if ( hasAnyReadyAlarms(jiffies) )
alarmsProcess();
while ( 1 )
++jiffies;
}

canonical_decompiled_code:
void __noreturn func2()
{
++jiffies;
if ( func0(jiffies) )
alarmsProcess();
while ( 1 )
++jiffies;
}

original_code:
void alarmTimerInterrupt0(void)
{
    jiffies += 1;
    if (hasAnyReadyAlarms(jiffies)) {
        alarmsProcess();
    }
    ;
    uint16_t tar = 0 + 1;
    while (!((int16_t) ((tar) - (0)) < 0)) {
        jiffies += 1;
        ;
    }
}

canonical_original_code:
void alarmTimerInterrupt0()
{
    jiffies += 1;
    if (hasAnyReadyAlarms(jiffies)) {
        alarmsProcess();
    }
    ;
    unsigned short tar = 0 + 1;
    while (!((short) ((tar) - (0)) < 0)) {
        jiffies += 1;
        ;
    }
}

variable_types:
tar
unsigned short


decompiled_code:
void alarmTimerInterrupt1()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void alarmTimerInterrupt1(void)
{
    if (0) {
        while (!((int16_t) ((0) - (0)) < 0)) {
            ;
            jiffies--;
        }
    }
}

canonical_original_code:
void alarmTimerInterrupt1()
{
    if (0) {
        while (!((short) ((0) - (0)) < 0)) {
            ;
            jiffies--;
        }
    }
}


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

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

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

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


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

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

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

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


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

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

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

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


decompiled_code:
__int64 led3Init()
{
return pinClearPc(255LL, 3LL);
}

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

original_code: static inline void led3Init(void) { ; led3Off(); }

canonical_original_code: static inline void led3Init() { ; led3Off(); }


decompiled_code:
__int64 led4Init()
{
return pinClearPc(255LL, 4LL);
}

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

original_code: static inline void led4Init(void) { ; led4Off(); }

canonical_original_code: static inline void led4Init() { ; led4Off(); }


decompiled_code:
__int64 led5Init()
{
return pinClearPc(255LL, 5LL);
}

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

original_code: static inline void led5Init(void) { ; led5Off(); }

canonical_original_code: static inline void led5Init() { ; led5Off(); }


decompiled_code:
__int64 led6Init()
{
return pinClearPc(255LL, 6LL);
}

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

original_code: static inline void led6Init(void) { ; led6Off(); }

canonical_original_code: static inline void led6Init() { ; led6Off(); }


decompiled_code:
__int64 led7Init()
{
return pinClearPc(255LL, 7LL);
}

canonical_decompiled_code:
__int64 func7()
{
return pinClearPc(255LL, 7LL);
}

original_code: static inline void led7Init(void) { ; led7Off(); }

canonical_original_code: static inline void led7Init() { ; led7Off(); }


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

canonical_decompiled_code:
__int64 func8()
{
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)); }


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

canonical_decompiled_code:
__int64 func9()
{
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)); }


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

canonical_decompiled_code:
__int64 func10()
{
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)); }


decompiled_code:
__int64 led3Get()
{
return pinReadPc(255LL, 3LL);
}

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

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

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


decompiled_code:
__int64 led4Get()
{
return pinReadPc(255LL, 4LL);
}

canonical_decompiled_code:
__int64 func12()
{
return pinReadPc(255LL, 4LL);
}

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

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


decompiled_code:
__int64 led5Get()
{
return pinReadPc(255LL, 5LL);
}

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

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

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


decompiled_code:
__int64 led6Get()
{
return pinReadPc(255LL, 6LL);
}

canonical_decompiled_code:
__int64 func14()
{
return pinReadPc(255LL, 6LL);
}

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

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


decompiled_code:
__int64 led7Get()
{
return pinReadPc(255LL, 7LL);
}

canonical_decompiled_code:
__int64 func15()
{
return pinReadPc(255LL, 7LL);
}

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

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


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

canonical_decompiled_code:
int __fastcall func16(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);
}


ledsInit

decompiled_code:
__int64 ledsInit()
{
led0Init();
led1Init();
led2Init();
led3Init();
led4Init();
led5Init();
led6Init();
return led7Init();
}

canonical_decompiled_code:
__int64 func17()
{
func0();
func1();
func2();
func3();
func4();
func5();
func6();
return func7();
}

original_code:
void ledsInit(void)
{
led0Init();
led1Init();
led2Init();
led3Init();
led4Init();
led5Init();
led6Init();
led7Init();
}

canonical_original_code:
void ledsInit()
{
led0Init();
led1Init();
led2Init();
led3Init();
led4Init();
led5Init();
led6Init();
led7Init();
}


ledsSet

decompiled_code:
__int64 __fastcall ledsSet(char a1)
{
suppressLedOutput(1LL);
if ( (a1 & 1) != 0 )
{
pinSetPc(255LL, 0LL);
if ( (a1 & 2) != 0 )
goto LABEL_3;
}
else
{
pinClearPc(255LL, 0LL);
if ( (a1 & 2) != 0 )
{
LABEL_3:
pinSetPc(255LL, 1LL);
if ( (a1 & 4) != 0 )
goto LABEL_4;
goto LABEL_12;
}
}
pinClearPc(255LL, 1LL);
if ( (a1 & 4) != 0 )
{
LABEL_4:
pinSetPc(255LL, 2LL);
if ( (a1 & 8) != 0 )
goto LABEL_5;
goto LABEL_13;
}
LABEL_12:
pinClearPc(255LL, 2LL);
if ( (a1 & 8) != 0 )
{
LABEL_5:
pinSetPc(255LL, 3LL);
if ( (a1 & 0x10) != 0 )
goto LABEL_6;
goto LABEL_14;
}
LABEL_13:
pinClearPc(255LL, 3LL);
if ( (a1 & 0x10) != 0 )
{
LABEL_6:
pinSetPc(255LL, 4LL);
if ( (a1 & 0x20) != 0 )
goto LABEL_7;
goto LABEL_15;
}
LABEL_14:
pinClearPc(255LL, 4LL);
if ( (a1 & 0x20) != 0 )
{
LABEL_7:
pinSetPc(255LL, 5LL);
if ( (a1 & 0x40) != 0 )
goto LABEL_8;
LABEL_16:
pinClearPc(255LL, 6LL);
if ( (a1 & 0x80) != 0 )
goto LABEL_9;
goto LABEL_17;
}
LABEL_15:
pinClearPc(255LL, 5LL);
if ( (a1 & 0x40) == 0 )
goto LABEL_16;
LABEL_8:
pinSetPc(255LL, 6LL);
if ( (a1 & 0x80) != 0 )
{
LABEL_9:
pinSetPc(255LL, 7LL);
return suppressLedOutput(0LL);
}
LABEL_17:
pinClearPc(255LL, 7LL);
return suppressLedOutput(0LL);
}

canonical_decompiled_code:
__int64 __fastcall func18(char a1)
{
suppressLedOutput(1LL);
if ( (a1 & 1) != 0 )
{
pinSetPc(255LL, 0LL);
if ( (a1 & 2) != 0 )
goto LABEL_3;
}
else
{
pinClearPc(255LL, 0LL);
if ( (a1 & 2) != 0 )
{
LABEL_3:
pinSetPc(255LL, 1LL);
if ( (a1 & 4) != 0 )
goto LABEL_4;
goto LABEL_12;
}
}
pinClearPc(255LL, 1LL);
if ( (a1 & 4) != 0 )
{
LABEL_4:
pinSetPc(255LL, 2LL);
if ( (a1 & 8) != 0 )
goto LABEL_5;
goto LABEL_13;
}
LABEL_12:
pinClearPc(255LL, 2LL);
if ( (a1 & 8) != 0 )
{
LABEL_5:
pinSetPc(255LL, 3LL);
if ( (a1 & 0x10) != 0 )
goto LABEL_6;
goto LABEL_14;
}
LABEL_13:
pinClearPc(255LL, 3LL);
if ( (a1 & 0x10) != 0 )
{
LABEL_6:
pinSetPc(255LL, 4LL);
if ( (a1 & 0x20) != 0 )
goto LABEL_7;
goto LABEL_15;
}
LABEL_14:
pinClearPc(255LL, 4LL);
if ( (a1 & 0x20) != 0 )
{
LABEL_7:
pinSetPc(255LL, 5LL);
if ( (a1 & 0x40) != 0 )
goto LABEL_8;
LABEL_16:
pinClearPc(255LL, 6LL);
if ( (a1 & 0x80) != 0 )
goto LABEL_9;
goto LABEL_17;
}
LABEL_15:
pinClearPc(255LL, 5LL);
if ( (a1 & 0x40) == 0 )
goto LABEL_16;
LABEL_8:
pinSetPc(255LL, 6LL);
if ( (a1 & 0x80) != 0 )
{
LABEL_9:
pinSetPc(255LL, 7LL);
return suppressLedOutput(0LL);
}
LABEL_17:
pinClearPc(255LL, 7LL);
return suppressLedOutput(0LL);
}

original_code:
void ledsSet(uint_t led_bitmask)
{
    suppressLedOutput(1);
if(led0_mask & led_bitmask) led0On(); else led0Off();
if(led1_mask & led_bitmask) led1On(); else led1Off();
if(led2_mask & led_bitmask) led2On(); else led2Off();
if(led3_mask & led_bitmask) led3On(); else led3Off();
if(led4_mask & led_bitmask) led4On(); else led4Off();
if(led5_mask & led_bitmask) led5On(); else led5Off();
if(led6_mask & led_bitmask) led6On(); else led6Off();
if(led7_mask & led_bitmask) led7On(); else led7Off();
    suppressLedOutput(0);
}

canonical_original_code:
void ledsSet(unsigned int led_bitmask)
{
    suppressLedOutput(1);
if(led0_mask & led_bitmask) led0On(); else led0Off();
if(led1_mask & led_bitmask) led1On(); else led1Off();
if(led2_mask & led_bitmask) led2On(); else led2Off();
if(led3_mask & led_bitmask) led3On(); else led3Off();
if(led4_mask & led_bitmask) led4On(); else led4Off();
if(led5_mask & led_bitmask) led5On(); else led5Off();
if(led6_mask & led_bitmask) led6On(); else led6Off();
if(led7_mask & led_bitmask) led7On(); else led7Off();
    suppressLedOutput(0);
}


ledsGet

decompiled_code:
__int64 ledsGet()
{
__int64 v0; // r12
int v1; // er8
int v2; // eax

v0 = (unsigned int)led0Get() != 0;
if ( (unsigned int)led1Get() )
LODWORD(v0) = v0 | 2;
if ( (unsigned int)led2Get() )
LODWORD(v0) = v0 | 4;
if ( (unsigned int)led3Get() )
LODWORD(v0) = v0 | 8;
if ( (unsigned int)led4Get() )
LODWORD(v0) = v0 | 0x10;
if ( (unsigned int)led5Get() )
LODWORD(v0) = v0 | 0x20;
if ( (unsigned int)led6Get() )
LODWORD(v0) = v0 | 0x40;
v1 = led7Get();
v2 = v0;
if ( v1 )
{
LOBYTE(v2) = v0 | 0x80;
LODWORD(v0) = v2;
}
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func19()
{
__int64 v0; // r12
int v1; // er8
int v2; // eax

v0 = (unsigned int)func8() != 0;
if ( (unsigned int)func9() )
LODWORD(v0) = v0 | 2;
if ( (unsigned int)func10() )
LODWORD(v0) = v0 | 4;
if ( (unsigned int)func11() )
LODWORD(v0) = v0 | 8;
if ( (unsigned int)func12() )
LODWORD(v0) = v0 | 0x10;
if ( (unsigned int)func13() )
LODWORD(v0) = v0 | 0x20;
if ( (unsigned int)func14() )
LODWORD(v0) = v0 | 0x40;
v1 = func15();
v2 = v0;
if ( v1 )
{
LOBYTE(v2) = v0 | 0x80;
LODWORD(v0) = v2;
}
return (unsigned int)v0;
}

original_code:
uint_t ledsGet(void)
{
    uint_t led_bitmask = 0;
if( led0Get() ) led_bitmask |= led0_mask;
if( led1Get() ) led_bitmask |= led1_mask;
if( led2Get() ) led_bitmask |= led2_mask;
if( led3Get() ) led_bitmask |= led3_mask;
if( led4Get() ) led_bitmask |= led4_mask;
if( led5Get() ) led_bitmask |= led5_mask;
if( led6Get() ) led_bitmask |= led6_mask;
if( led7Get() ) led_bitmask |= led7_mask;
    return led_bitmask;
}

canonical_original_code:
unsigned int ledsGet()
{
    unsigned int led_bitmask = 0;
if( led0Get() ) led_bitmask |= led0_mask;
if( led1Get() ) led_bitmask |= led1_mask;
if( led2Get() ) led_bitmask |= led2_mask;
if( led3Get() ) led_bitmask |= led3_mask;
if( led4Get() ) led_bitmask |= led4_mask;
if( led5Get() ) led_bitmask |= led5_mask;
if( led6Get() ) led_bitmask |= led6_mask;
if( led7Get() ) led_bitmask |= led7_mask;
    return led_bitmask;
}


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


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


radioOn

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

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

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

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


radioGetLastRSSI

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

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

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

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


redLedOn

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

canonical_decompiled_code:
__int64 func4()
{
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 func5()
{
return pinClearPc(255LL, 0LL);
}

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

canonical_original_code: static inline void redLedOff() { 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(); }


rcvRadio

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

radioRecv_constprop_0_isra_0();
result = (unsigned int)(unsigned __int8)rxOk + 1;
if ( (unsigned __int8)(rxOk + 1) > 3u )
result = 3LL;
rxOk = result;
return result;
}

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

func10();
result = (unsigned int)(unsigned __int8)rxOk + 1;
if ( (unsigned __int8)(rxOk + 1) > 3u )
result = 3LL;
rxOk = result;
return result;
}

original_code:
void rcvRadio(void)
{
    static uint8_t buffer[0xffff];
    uint16_t len;
    len = radioRecv(buffer, sizeof(buffer));
    ++rxOk;
    if (rxOk > 3) rxOk = 3;
}

canonical_original_code:
void rcvRadio()
{
    static unsigned char buffer[65535];
    unsigned short len;
    len = radioRecv(buffer, sizeof(buffer));
    ++rxOk;
    if (rxOk > 3) rxOk = 3;
}

variable_types:
buffer
unsigned char [65535]

variable_types:
len
unsigned short


doMsleep

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


getWaitInterval

decompiled_code:
__int64 __fastcall getWaitInterval(char a1)
{
__int64 result; // rax
__int16 v2; // dx
__int16 v3; // di

result = 600000LL;
if ( rxOk )
{
v2 = 100;
v3 = a1 + 70;
if ( v3 < 0 )
v3 = 0;
if ( v3 <= 100 )
v2 = v3;
result = 401000 - (unsigned int)(400000LL * v2 / 0x64uLL);
}
return result;
}

original_code:
uint32_t getWaitInterval(int8_t rssi) {
    if (!rxOk) return 600000ul;
    int16_t normRssi = (int16_t) rssi - -70;
    if (normRssi > (30 - -70)) normRssi = (30 - -70);
    if (normRssi < 0) normRssi = 0;
    return 1000 + 400000ul - normRssi * 400000ul / (30 - -70);
}

canonical_original_code:
unsigned int getWaitInterval(char rssi) {
    if (!rxOk) return 600000ul;
    short normRssi = (int16_t) rssi - -70;
    if (normRssi > (30 - -70)) normRssi = (30 - -70);
    if (normRssi < 0) normRssi = 0;
    return 1000 + 400000ul - normRssi * 400000ul / (30 - -70);
}

variable_types: rssi char

variable_types: normRssi short


devRead

original_code:
static ssize_t devRead(void * __restrict__ id, void * __restrict__ buf, size_t count)
{
    const struct fsDevOperations *ops = id;
    return ops->read(buf, count);
}

canonical_original_code:
static long devRead(void *id, void *buf, unsigned long count)
{
    struct fsDevOperations *ops = id;
    return ops->read(buf, count);
}

variable_types:
id
void *

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: struct fsDevOperations { _Bool (*open)(); long (*read)(void *buf, unsigned long count); long (*write)(void *buf, unsigned long count); _Bool (*close)(); } *

return_type: long


devWrite

original_code:
static ssize_t devWrite(void * __restrict__ id, const void * __restrict__ buf,
                          size_t count)
{
    const struct fsDevOperations *ops = id;
    return ops->write(buf, count);
}

canonical_original_code:
static long devWrite(void *id, void *buf, unsigned long count)
{
    struct fsDevOperations *ops = id;
    return ops->write(buf, count);
}

user_defined_types: struct fsDevOperations { _Bool (*open)(); long (*read)(void *buf, unsigned long count); long (*write)(void *buf, unsigned long count); _Bool (*close)(); }


devClose

original_code:
static _Bool devClose(void *id)
{
    const struct fsDevOperations *ops = id;
    return ops->close();
}

canonical_original_code:
static _Bool devClose(void *id)
{
    struct fsDevOperations *ops = id;
    return ops->close();
}


devTell

decompiled_code:
__int64 devTell()
{
return 0LL;
}

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

original_code:
static fsOff_t devTell(void *id)
{
    (void)id;
    return 0;
}

canonical_original_code:
static unsigned int devTell(void *id)
{
    (void)id;
    return 0;
}


devSeek

decompiled_code:
void devSeek()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
static void devSeek(void *id, fsOff_t pos)
{
    (void)id; (void)pos;
}

canonical_original_code:
static void devSeek(void *id, unsigned int pos)
{
    (void)id; (void)pos;
}

variable_types:
pos
unsigned int


devFlush

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

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

original_code:
static _Bool devFlush(void *id)
{
    (void)id;
    return 1;
}


devRename

decompiled_code:
__int64 devRename()
{
fsSetError(6LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
fsSetError(6LL);
return 0LL;
}

original_code:
static _Bool devRename(const char *old, const char *new)
{
    (void)old; (void)new;
    fsSetError(FS_ERR_NOSYS);
    return 0;
}

canonical_original_code:
static _Bool devRename(char *old, char *new)
{
    (void)old; (void)new;
    fsSetError(FS_ERR_NOSYS);
    return 0;
}

variable_types:
old
char *

variable_types:
new
char *


devRemove

decompiled_code:
__int64 devRemove()
{
fsSetError(6LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
fsSetError(6LL);
return 0LL;
}

original_code:
static _Bool devRemove(const char *path)
{
    (void)path;
    fsSetError(FS_ERR_NOSYS);
    return 0;
}

canonical_original_code:
static _Bool devRemove(char *path)
{
    (void)path;
    fsSetError(FS_ERR_NOSYS);
    return 0;
}

variable_types:
path
char *


fsFindDevEntry

decompiled_code:
char **__fastcall fsFindDevEntry(const char *a1)
{
if ( !strcmp("leds", a1) )
return &fsDevEntries;
if ( !strcmp("null", a1) )
return &off_1F0;
fsSetError(4LL);
return 0LL;
}

canonical_decompiled_code:
char **__fastcall func8(const char *a1)
{
if ( !strcmp("leds", a1) )
return &fsDevEntries;
if ( !strcmp("null", a1) )
return &off_1F0;
fsSetError(4LL);
return 0LL;
}

original_code:
static const struct fsDevEntry *fsFindDevEntry(const char *name)
{
    size_t i;
    for (i = 0; i < sizeof(fsDevEntries) / sizeof(struct fsDevEntry); i++)
    {
        if (!strcmp(fsDevEntries[i].name, name))
            return fsDevEntries + i;
    }
    fsSetError(FS_ERR_NOENT);
    return ((void *)0);
}

canonical_original_code:
static struct fsDevEntry *fsFindDevEntry(char *name)
{
    unsigned long i;
    for (i = 0; i < sizeof(fsDevEntries) / sizeof(struct fsDevEntry); i++)
    {
        if (!strcmp(fsDevEntries[i].name, name))
            return fsDevEntries + i;
    }
    fsSetError(FS_ERR_NOENT);
    return ((void *)0);
}

variable_types:
name
char *

variable_types:
i
unsigned long

return_type: struct fsDevEntry { char *name; struct fsDevOperations { _Bool (*open)(); long (*read)(void *buf, unsigned long count); long (*write)(void *buf, unsigned long count); _Bool (*close)(); } *ops; } *

user_defined_types: struct fsDevEntry { char *name; struct fsDevOperations *ops; }