devOpen

decompiled_code:
char *__fastcall devOpen(const char *a1, int a2)
{
char **v2; // rax
char **v3; // rbx
char *result; // rax

if ( a2 == 4 )
{
v2 = fsFindDevEntry(a1);
v3 = v2;
if ( v2 && (*(unsigned __int8 (**)(void))v2[1])() )
result = v3[1];
else
result = 0LL;
}
else
{
fsSetError(7LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func9(const char *a1, int a2)
{
char **v2; // rax
char **v3; // rbx
char *result; // rax

if ( a2 == 4 )
{
v2 = func8(a1);
v3 = v2;
if ( v2 && (*(unsigned __int8 (**)(void))v2[1])() )
result = v3[1];
else
result = 0LL;
}
else
{
fsSetError(7LL);
result = 0LL;
}
return result;
}

original_code:
static void *devOpen(const char *path, fsMode_t mode)
{
    const struct fsDevEntry *entry;
    if (mode != FS_RDWR)
    {
        fsSetError(FS_ERR_INVAL);
        return ((void *)0);
    }
    entry = fsFindDevEntry(path);
    if (entry)
        return entry->ops->open() ? (void *)entry->ops : ((void *)0);
    else
        return ((void *)0);
}

canonical_original_code:
static void *devOpen(char *path, enum fsMode_t mode)
{
    struct fsDevEntry *entry;
    if (mode != FS_RDWR)
    {
        fsSetError(FS_ERR_INVAL);
        return ((void *)0);
    }
    entry = fsFindDevEntry(path);
    if (entry)
        return entry->ops->open() ? (void *)entry->ops : ((void *)0);
    else
        return ((void *)0);
}

variable_types: enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr }

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


devStat

repo: atiselsts/osw

decompiled_code:
__int64 __fastcall devStat(const char *a1, _DWORD *a2)
{
char **v2; // r8
__int64 result; // rax

v2 = fsFindDevEntry(a1);
result = 0LL;
if ( v2 )
{
*a2 = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1, _DWORD *a2)
{
char **v2; // r8
__int64 result; // rax

v2 = func8(a1);
result = 0LL;
if ( v2 )
{
*a2 = 0;
result = 1LL;
}
return result;
}

original_code:
static _Bool devStat(const char * __restrict__ path, struct fsStat * __restrict__ buf)
{
    if (!fsFindDevEntry(path))
        return 0;
    else
    {
        buf->size = 0;
        return 1;
    }
}

canonical_original_code:
static _Bool devStat(char *path, struct fsStat *buf)
{
    if (!fsFindDevEntry(path))
        return 0;
    else
    {
        buf->size = 0;
        return 1;
    }
}

variable_types:
path
char *

variable_types:
buf
struct fsStat { unsigned int size; } *


fsDevInit

decompiled_code:
void fsDevInit()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void fsDevInit(void)
{
}

canonical_original_code:
void fsDevInit()
{
}

return_type: void


fsFindPrefixEntry

decompiled_code:
char **__fastcall fsFindPrefixEntry(const char *a1)
{
if ( strlen(a1) <= 5 )
goto LABEL_4;
if ( !memcmp(a1, "/blk/", 5uLL) )
return &prefixEntries;
if ( memcmp(a1, "/dev/", 5uLL) )
{
LABEL_4:
fsSetError(4LL);
return 0LL;
}
return &off_F8;
}

canonical_decompiled_code:
char **__fastcall func0(const char *a1)
{
if ( strlen(a1) <= 5 )
goto LABEL_4;
if ( !memcmp(a1, "/blk/", 5uLL) )
return &prefixEntries;
if ( memcmp(a1, "/dev/", 5uLL) )
{
LABEL_4:
fsSetError(4LL);
return 0LL;
}
return &off_F8;
}

original_code:
static const struct fsPrefixEntry *fsFindPrefixEntry(const char *path)
{
    size_t i;
    for (i = 0; i < sizeof(prefixEntries) / sizeof(struct fsPrefixEntry); i++)
    {
        size_t prefixlen = strlen(prefixEntries[i].prefix);
        if (strlen(path) > prefixlen && !strncmp(path, prefixEntries[i].prefix,
            prefixlen))
        {
            return prefixEntries + i;
        }
    }
    fsSetError(FS_ERR_NOENT);
    return ((void *)0);
}

canonical_original_code:
static struct fsPrefixEntry *fsFindPrefixEntry(char *path)
{
    unsigned long i;
    for (i = 0; i < sizeof(prefixEntries) / sizeof(struct fsPrefixEntry); i++)
    {
        unsigned long prefixlen = strlen(prefixEntries[i].prefix);
        if (strlen(path) > prefixlen && !strncmp(path, prefixEntries[i].prefix,
            prefixlen))
        {
            return prefixEntries + i;
        }
    }
    fsSetError(FS_ERR_NOENT);
    return ((void *)0);
}

variable_types:
i
unsigned long

variable_types:
prefixlen
unsigned long

return_type: struct fsPrefixEntry { char *prefix; struct fsOperations { _Bool (*stat)(char *path, struct fsStat { unsigned int size; } *buf); void *(*open)(char *path, enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr } mode); long (*read)(void *id, void *buf, unsigned long count); unsigned int (*tell)(void *id); void (*seek)(void *id, unsigned int pos); long (*write)(void *id, void *buf, unsigned long count); _Bool (*flush)(void *id); _Bool (*close)(void *id); _Bool (*remove)(char *path); _Bool (*rename)(char *old, char *new); } *ops; void (*init)(); } *

user_defined_types: struct fsPrefixEntry { char *prefix; struct fsOperations *ops; void (*init)(); }

user_defined_types: struct fsOperations { _Bool (*stat)(char *path, struct fsStat *buf); void *(*open)(char *path, enum fsMode_t mode); long (*read)(void *id, void *buf, unsigned long count); unsigned int (*tell)(void *id); void (*seek)(void *id, unsigned int pos); long (*write)(void *id, void *buf, unsigned long count); _Bool (*flush)(void *id); _Bool (*close)(void *id); _Bool (*remove)(char *path); _Bool (*rename)(char *old, char *new); }


fsInitSubsystems

decompiled_code:
__int64 fsInitSubsystems()
{
fsBlockInit();
return fsDevInit();
}

canonical_decompiled_code:
__int64 func1()
{
fsBlockInit();
return fsDevInit();
}

original_code:
void fsInitSubsystems(void)
{
    size_t i;
    for (i = 0; i < sizeof(prefixEntries) / sizeof(struct fsPrefixEntry); i++)
        prefixEntries[i].init();
}

canonical_original_code:
void fsInitSubsystems()
{
    unsigned long i;
    for (i = 0; i < sizeof(prefixEntries) / sizeof(struct fsPrefixEntry); i++)
        prefixEntries[i].init();
}

user_defined_types: enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr }


fsPrefixLookup

decompiled_code:
char **__fastcall fsPrefixLookup(const char **a1)
{
char **v1; // rax
char **v2; // r12

v1 = fsFindPrefixEntry(*a1);
v2 = v1;
if ( v1 )
{
*a1 += strlen(*v1);
v2 = (char **)v2[1];
}
return v2;
}

canonical_decompiled_code:
char **__fastcall func2(const char **a1)
{
char **v1; // rax
char **v2; // r12

v1 = func0(*a1);
v2 = v1;
if ( v1 )
{
*a1 += strlen(*v1);
v2 = (char **)v2[1];
}
return v2;
}

original_code:
const struct fsOperations *fsPrefixLookup(const char * __restrict__ *path)
{
    const struct fsPrefixEntry *entry = fsFindPrefixEntry(*path);
    if (!entry)
        return ((void *)0);
    else
    {
        *path += strlen(entry->prefix);
        return entry->ops;
    }
}

canonical_original_code:
struct fsOperations *fsPrefixLookup(char **path)
{
    struct fsPrefixEntry *entry = fsFindPrefixEntry(*path);
    if (!entry)
        return ((void *)0);
    else
    {
        *path += strlen(entry->prefix);
        return entry->ops;
    }
}

variable_types:
path
char **

variable_types: struct fsPrefixEntry { char *prefix; struct fsOperations { _Bool (*stat)(char *path, struct fsStat { unsigned int size; } *buf); void *(*open)(char *path, enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr } mode); long (*read)(void *id, void *buf, unsigned long count); unsigned int (*tell)(void *id); void (*seek)(void *id, unsigned int pos); long (*write)(void *id, void *buf, unsigned long count); _Bool (*flush)(void *id); _Bool (*close)(void *id); _Bool (*remove)(char *path); _Bool (*rename)(char *old, char *new); } *ops; void (*init)(); } *


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

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


redLedToggle

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

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

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

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

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

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


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

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

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

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


blueLedToggle

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

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

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

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

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

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


onTimer

decompiled_code:
__int64 onTimer()
{
unsigned int v0; // eax

if ( print_buf )
{
v0 = getTimeMs();
debugPrintfFormat("[%u] onTimer\n", v0);
serialPrint(print_buf);
}
blueLedToggle();
return alarmSchedule(&timer, 1000LL);
}

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

if ( print_buf )
{
v0 = func1();
debugPrintfFormat("[%u] onTimer\n", v0);
serialPrint(print_buf);
}
func5();
return alarmSchedule(&timer, 1000LL);
}

original_code:
void onTimer(void *x)
{
    if (_print_buf) { debugPrintfFormat("[%u] " "onTimer\n", getTimeMs()); serialPrint(_print_buf); };
    blueLedToggle();
    alarmSchedule(&timer, 1000);
}

variable_types:
x
void *


radioOff

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

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

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

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


radioOn

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

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

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

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


radioRecvCb

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

redLedToggle();
result = radioRecv_constprop_0();
if ( (__int16)result <= 0 )
{
if ( print_buf )
{
debugPrintfFormat("radio recv failed, len=%d\n", (unsigned int)(__int16)result);
result = serialPrint(print_buf);
}
}
else
{
result = (__int16)result;
v1 = print_buf == 0LL;
buffer_0[(__int16)result] = 0;
if ( !v1 )
{
debugPrintfFormat("recv: %s\n", buffer_0);
result = serialPrint(print_buf);
}
}
return result;
}

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

func3();
result = func10();
if ( (__int16)result <= 0 )
{
if ( print_buf )
{
debugPrintfFormat("radio recv failed, len=%d\n", (unsigned int)(__int16)result);
result = serialPrint(print_buf);
}
}
else
{
result = (__int16)result;
v1 = print_buf == 0LL;
buffer_0[(__int16)result] = 0;
if ( !v1 )
{
debugPrintfFormat("recv: %s\n", buffer_0);
result = serialPrint(print_buf);
}
}
return result;
}

original_code:
void radioRecvCb() {
    static uint8_t buffer[128];
    int16_t len;
    redLedToggle();
    len = radioRecv(buffer, sizeof(buffer) - 1);
    if (len <= 0) {
        if (2 == 1) { serialPrintV("radio recv failed, len=%d\n", len); } else { if (_print_buf) { debugPrintfFormat("radio recv failed, len=%d\n", len); serialPrint(_print_buf); }; };
        return;
    }
    if (len > 0) {
        buffer[len] = 0;
        if (2 == 1) { serialPrintV("recv: %s\n", (char *) buffer); } else { if (_print_buf) { debugPrintfFormat("recv: %s\n", (char *) buffer); serialPrint(_print_buf); }; };
    }
}

canonical_original_code:
void radioRecvCb() {
    static unsigned char buffer[128];
    short len;
    redLedToggle();
    len = radioRecv(buffer, sizeof(buffer) - 1);
    if (len <= 0) {
        if (2 == 1) { serialPrintV("radio recv failed, len=%d\n", len); } else { if (_print_buf) { debugPrintfFormat("radio recv failed, len=%d\n", len); serialPrint(_print_buf); }; };
        return;
    }
    if (len > 0) {
        buffer[len] = 0;
        if (2 == 1) { serialPrintV("recv: %s\n", (char *) buffer); } else { if (_print_buf) { debugPrintfFormat("recv: %s\n", (char *) buffer); serialPrint(_print_buf); }; };
    }
}

variable_types:
buffer
unsigned char [128]

variable_types: len short


doMsleep

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

variable_types:
milliseconds
unsigned short


flashErase

decompiled_code:
// attributes: thunk
__int64 flashErase()
{
return extFlashEraseSector();
}

canonical_decompiled_code:
__int64 func17()
{
return extFlashEraseSector();
}

original_code:
void flashErase(uint32_t address)
{
    extFlashEraseSector(address);
}

canonical_original_code:
void flashErase(unsigned int address)
{
    extFlashEraseSector(address);
}

variable_types:
address
unsigned int


flashWrite

decompiled_code:
__int64 __fastcall flashWrite(__int64 a1)
{
return extFlashWrite(a1, &buffer, 64LL);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
return extFlashWrite(a1, &buffer, 64LL);
}

original_code:
void flashWrite(uint32_t address)
{
    extFlashWrite(address, buffer, 64);
}

canonical_original_code:
void flashWrite(unsigned int address)
{
    extFlashWrite(address, buffer, 64);
}


flashRead

decompiled_code:
__int64 __fastcall flashRead(__int64 a1)
{
__int64 result; // rax
char v2[72]; // [rsp+0h] [rbp-48h] BYREF

extFlashRead(a1, v2, 64LL);
for ( result = 0LL; result != 64; ++result )
{
if ( v2[result] != buffer[result] )
{
assertionFailed("readBuffer[i] == buffer[i]", "main.c", 106LL);
appMain();
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
__int64 result; // rax
char v2[72]; // [rsp+0h] [rbp-48h] BYREF

extFlashRead(a1, v2, 64LL);
for ( result = 0LL; result != 64; ++result )
{
if ( v2[result] != buffer[result] )
{
assertionFailed("readBuffer[i] == buffer[i]", "main.c", 106LL);
func20();
}
}
return result;
}

original_code:
void flashRead(uint32_t address)
{
    uint16_t i;
    uint8_t readBuffer[64];
    extFlashRead(address, readBuffer, 64);
    for (i = 0; i < 64; ++i) {
        ((readBuffer[i] == buffer[i]) ? (void)0 : assertionFailed("readBuffer[i] == buffer[i]", "main.c", 106));
    }
}

canonical_original_code:
void flashRead(unsigned int address)
{
    unsigned short i;
    unsigned char readBuffer[64];
    extFlashRead(address, readBuffer, 64);
    for (i = 0; i < 64; ++i) {
        ((readBuffer[i] == buffer[i]) ? (void)0 : assertionFailed("readBuffer[i] == buffer[i]", "main.c", 106));
    }
}

variable_types:
i
unsigned short

variable_types:
readBuffer
unsigned char [64]


doMsleep

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


serialInit

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

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

original_code:
uint_t serialInit(uint8_t id, uint32_t speed, uint8_t conf)
{
    return 0;
}

canonical_original_code:
unsigned int serialInit(unsigned char id, unsigned int speed, unsigned char conf)
{
    return 0;
}

variable_types:
id
unsigned char

variable_types:
speed
unsigned int

variable_types:
conf
unsigned char


serialSendByte

decompiled_code:
ssize_t __fastcall serialSendByte(char a1, char a2)
{
ssize_t result; // rax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
if ( !a1 )
{
if ( txEnabled )
result = write(1, buf, 1uLL);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(char a1, char a2)
{
ssize_t result; // rax
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
if ( !a1 )
{
if ( txEnabled )
result = write(1, buf, 1uLL);
}
return result;
}

original_code:
void serialSendByte(uint8_t id, uint8_t data)
{
    if (id >= 1) return;
    if (txEnabled[id]) {
        int ret = write(1, &data, 1);
        (void) ret;
    }
}

canonical_original_code:
void serialSendByte(unsigned char id, unsigned char data)
{
    if (id >= 1) return;
    if (txEnabled[id]) {
        int ret = write(1, &data, 1);
        (void) ret;
    }
}

variable_types:
data
unsigned char

variable_types: ret int


serialEnableTX

decompiled_code:
void __fastcall serialEnableTX(char a1)
{
if ( !a1 )
txEnabled = 1;
}

canonical_decompiled_code:
void __fastcall func4(char a1)
{
if ( !a1 )
txEnabled = 1;
}

original_code:
void serialEnableTX( uint8_t id )
{
    if (id >= 1) return;
    txEnabled[id] = 1;
}

canonical_original_code:
void serialEnableTX(unsigned char id)
{
    if (id >= 1) return;
    txEnabled[id] = 1;
}


serialDisableTX

decompiled_code:
void __fastcall serialDisableTX(unsigned __int8 a1)
{
if ( a1 <= 1u )
txEnabled[a1] = 0;
}

canonical_decompiled_code:
void __fastcall func5(unsigned __int8 a1)
{
if ( a1 <= 1u )
txEnabled[a1] = 0;
}

original_code:
void serialDisableTX( uint8_t id )
{
    if (id > 1) return;
    txEnabled[id] = 0;
}

canonical_original_code:
void serialDisableTX(unsigned char id)
{
    if (id > 1) return;
    txEnabled[id] = 0;
}


serialEnableRX

decompiled_code:
int __fastcall serialEnableRX(char a1)
{
int result; // eax

if ( !a1 && !rxEnabled[0] )
{
rxEnabled[0] = 1;
result = pthread_create(&rxThread, 0LL, (void *(*)(void *))rxHandler, 0LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(char a1)
{
int result; // eax

if ( !a1 && !rxEnabled[0] )
{
rxEnabled[0] = 1;
result = pthread_create(&rxThread, 0LL, (void *(*)(void *))rxHandler, 0LL);
}
return result;
}

original_code:
void serialEnableRX( uint8_t id )
{
    if (id >= 1) return;
    if (!rxEnabled[id]) {
        rxEnabled[id] = 1;
        pthread_create(&rxThread, ((void *)0), rxHandler, (void *) (uint32_t) id);
    }
}

canonical_original_code:
void serialEnableRX(unsigned char id)
{
    if (id >= 1) return;
    if (!rxEnabled[id]) {
        rxEnabled[id] = 1;
        pthread_create(&rxThread, ((void *)0), rxHandler, (void *) (unsigned int) id);
    }
}


serialDisableRX

decompiled_code:
int __fastcall serialDisableRX(unsigned __int8 a1)
{
int result; // eax

if ( a1 <= 1u )
{
if ( rxEnabled[a1] )
{
rxEnabled[a1] = 0;
result = pthread_join(rxThread, 0LL);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(unsigned __int8 a1)
{
int result; // eax

if ( a1 <= 1u )
{
if ( rxEnabled[a1] )
{
rxEnabled[a1] = 0;
result = pthread_join(rxThread, 0LL);
}
}
return result;
}

original_code:
void serialDisableRX( uint8_t id )
{
    if (id > 1) return;
    if (rxEnabled[id]) {
        rxEnabled[id] = 0;
        pthread_join(rxThread, ((void *)0));
    }
}

canonical_original_code:
void serialDisableRX(unsigned char id)
{
    if (id > 1) return;
    if (rxEnabled[id]) {
        rxEnabled[id] = 0;
        pthread_join(rxThread, ((void *)0));
    }
}


fsFreeHandle

decompiled_code:
void __fastcall fsFreeHandle(char a1)
{
openFiles[2 * a1] = 0LL;
}

canonical_decompiled_code:
void __fastcall func0(char a1)
{
openFiles[2 * a1] = 0LL;
}

original_code:
static inline void fsFreeHandle(int8_t fd)
{
    mutexLock(&openFilesMutex);
    openFiles[fd].ops = ((void *)0);
    mutexUnlock(&openFilesMutex);
}

canonical_original_code:
static inline void fsFreeHandle(char fd)
{
    mutexLock(&openFilesMutex);
    openFiles[fd].ops = ((void *)0);
    mutexUnlock(&openFilesMutex);
}

variable_types: fd char


fsInit

decompiled_code:
// attributes: thunk
__int64 fsInit()
{
return fsInitSubsystems();
}

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

original_code:
void fsInit(void)
{
    mutexInit(&openFilesMutex);
    fsInitSubsystems();
}

canonical_original_code:
void fsInit()
{
    mutexInit(&openFilesMutex);
    fsInitSubsystems();
}


fsSetError

decompiled_code:
void __fastcall fsSetError(char a1)
{
lastErr = a1;
}

canonical_decompiled_code:
void __fastcall func2(char a1)
{
lastErr = a1;
}

original_code:
void fsSetError(fsError_t err)
{
    lastErr = err;
}

canonical_original_code:
void fsSetError(enum fsError_t err)
{
    lastErr = err;
}

variable_types: enum fsError_t { FS_ERR_OK=0, FS_ERR_NOMEM=1, FS_ERR_IO=2, FS_ERR_NOSPC=3, FS_ERR_NOENT=4, FS_ERR_EXIST=5, FS_ERR_NOSYS=6, FS_ERR_INVAL=7, FS_ERR_OPEN=8 }


fsAllocHandle

decompiled_code:
void __fastcall fsAllocHandle(__int64 a1)
{
if ( openFiles[0] )
{
if ( qword_350 )
fsSetError(1);
else
qword_350 = a1;
}
else
{
openFiles[0] = a1;
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
if ( openFiles[0] )
{
if ( qword_350 )
func2(1);
else
qword_350 = a1;
}
else
{
openFiles[0] = a1;
}
}

original_code:
static int8_t fsAllocHandle(const struct fsOperations *ops)
{
    int8_t i;
    mutexLock(&openFilesMutex);
    for (i = 0; i < 2; i++)
    {
        if (!openFiles[i].ops)
        {
            openFiles[i].ops = ops;
            break;
        }
    }
    mutexUnlock(&openFilesMutex);
    if (i == 2)
    {
        fsSetError(FS_ERR_NOMEM);
        return -1;
    }
    return i;
}

canonical_original_code:
static char fsAllocHandle(struct fsOperations *ops)
{
    char i;
    mutexLock(&openFilesMutex);
    for (i = 0; i < 2; i++)
    {
        if (!openFiles[i].ops)
        {
            openFiles[i].ops = ops;
            break;
        }
    }
    mutexUnlock(&openFilesMutex);
    if (i == 2)
    {
        fsSetError(FS_ERR_NOMEM);
        return -1;
    }
    return i;
}

variable_types: struct fsOperations { _Bool (*stat)(char *path, struct fsStat { unsigned int size; } *buf); void *(*open)(char *path, enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr } mode); long (*read)(void *id, void *buf, unsigned long count); unsigned int (*tell)(void *id); void (*seek)(void *id, unsigned int pos); long (*write)(void *id, void *buf, unsigned long count); _Bool (*flush)(void *id); _Bool (*close)(void *id); _Bool (*remove)(char *path); _Bool (*rename)(char *old, char *new); } *

variable_types: i char

return_type: char


fsLastError

decompiled_code:
__int64 fsLastError()
{
return (unsigned __int8)lastErr;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned __int8)lastErr;
}

original_code:
fsError_t fsLastError(void)
{
    return lastErr;
}

canonical_original_code:
enum fsError_t fsLastError()
{
    return lastErr;
}

return_type: enum fsError_t { FS_ERR_OK=0, FS_ERR_NOMEM=1, FS_ERR_IO=2, FS_ERR_NOSPC=3, FS_ERR_NOENT=4, FS_ERR_EXIST=5, FS_ERR_NOSYS=6, FS_ERR_INVAL=7, FS_ERR_OPEN=8 }


fsStat

original_code:
_Bool fsStat(const char * __restrict__ path, struct fsStat * __restrict__ buf)
{
    const struct fsOperations *ops = fsPrefixLookup(&path);
    return !ops ? 0 : ops->stat(path, buf);
}

canonical_original_code:
_Bool fsStat(char *path, struct fsStat *buf)
{
    struct fsOperations *ops = fsPrefixLookup(&path);
    return !ops ? 0 : ops->stat(path, buf);
}

variable_types:
path
char *

variable_types:
buf
struct fsStat { unsigned int size; } *


fsOpen

decompiled_code:
__int64 __fastcall fsOpen(__int64 a1, unsigned int a2)
{
__int64 v2; // rdi
unsigned int v3; // eax
unsigned int v4; // er12
char v5; // r13
__int64 *v6; // rbx
__int64 v7; // rax
__int64 v9[6]; // [rsp+8h] [rbp-30h] BYREF

v9[0] = a1;
if ( (a2 & 0x18) == 24 )
{
v4 = -1;
fsSetError(7);
return v4;
}
v2 = fsPrefixLookup(v9);
if ( !v2 )
goto LABEL_7;
fsAllocHandle(v2);
v4 = v3;
if ( (_BYTE)v3 == 0xFF )
goto LABEL_7;
v5 = v3;
v6 = &openFiles[2 * (char)v3];
v7 = (*(__int64 (__fastcall **)(__int64, _QWORD))(*v6 + 8))(v9[0], a2);
v6[1] = v7;
if ( !v7 )
{
fsFreeHandle(v5);
LABEL_7:
v4 = -1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2)
{
__int64 v2; // rdi
unsigned int v3; // eax
unsigned int v4; // er12
char v5; // r13
__int64 *v6; // rbx
__int64 v7; // rax
__int64 v9[6]; // [rsp+8h] [rbp-30h] BYREF

v9[0] = a1;
if ( (a2 & 0x18) == 24 )
{
v4 = -1;
func2(7);
return v4;
}
v2 = fsPrefixLookup(v9);
if ( !v2 )
goto LABEL_7;
func3(v2);
v4 = v3;
if ( (_BYTE)v3 == 0xFF )
goto LABEL_7;
v5 = v3;
v6 = &openFiles[2 * (char)v3];
v7 = (*(__int64 (__fastcall **)(__int64, _QWORD))(*v6 + 8))(v9[0], a2);
v6[1] = v7;
if ( !v7 )
{
func0(v5);
LABEL_7:
v4 = -1;
}
return v4;
}

original_code:
int8_t fsOpen(const char *path, fsMode_t mode)
{
    int8_t fd;
    const struct fsOperations *ops;
    if (mode & FS_NOCACHE && mode & FS_CHECKSUM)
    {
        fsSetError(FS_ERR_INVAL);
        return -1;
    }
    if (!(ops = fsPrefixLookup(&path)))
        return -1;
    if ((fd = fsAllocHandle(ops)) == -1)
        return -1;
    if (!(openFiles[fd].data = openFiles[fd].ops->open(path, mode)))
    {
        fsFreeHandle(fd);
        return -1;
    }
    return fd;
}

canonical_original_code:
char fsOpen(char *path, enum fsMode_t mode)
{
    char fd;
    struct fsOperations *ops;
    if (mode & FS_NOCACHE && mode & FS_CHECKSUM)
    {
        fsSetError(FS_ERR_INVAL);
        return -1;
    }
    if (!(ops = fsPrefixLookup(&path)))
        return -1;
    if ((fd = fsAllocHandle(ops)) == -1)
        return -1;
    if (!(openFiles[fd].data = openFiles[fd].ops->open(path, mode)))
    {
        fsFreeHandle(fd);
        return -1;
    }
    return fd;
}

variable_types: enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr }


fsRemove

original_code:
_Bool fsRemove(const char *path)
{
    const struct fsOperations *ops = fsPrefixLookup(&path);
    return !ops ? 0 : ops->remove(path);
}

canonical_original_code:
_Bool fsRemove(char *path)
{
    struct fsOperations *ops = fsPrefixLookup(&path);
    return !ops ? 0 : ops->remove(path);
}


fsRename

decompiled_code:
void __fastcall fsRename(char *a1, char *a2)
{
__int64 v2; // rbx
char *v3; // r12
char *v4; // rbp
char *s2; // [rsp+0h] [rbp-28h] BYREF
char *s1; // [rsp+8h] [rbp-20h] BYREF

s1 = a1;
s2 = a2;
v2 = fsPrefixLookup(&s1);
if ( v2 == fsPrefixLookup(&s2) )
{
if ( v2 )
{
v3 = s2;
v4 = s1;
if ( strcmp(s1, s2) )
(*(void (__fastcall **)(char *, char *))(v2 + 72))(v4, v3);
}
}
else
{
fsSetError(6);
}
}

canonical_decompiled_code:
void __fastcall func8(char *a1, char *a2)
{
__int64 v2; // rbx
char *v3; // r12
char *v4; // rbp
char *s2; // [rsp+0h] [rbp-28h] BYREF
char *s1; // [rsp+8h] [rbp-20h] BYREF

s1 = a1;
s2 = a2;
v2 = fsPrefixLookup(&s1);
if ( v2 == fsPrefixLookup(&s2) )
{
if ( v2 )
{
v3 = s2;
v4 = s1;
if ( strcmp(s1, s2) )
(*(void (__fastcall **)(char *, char *))(v2 + 72))(v4, v3);
}
}
else
{
func2(6);
}
}

original_code:
_Bool fsRename(const char *old, const char *new)
{
    const struct fsOperations *a = fsPrefixLookup(&old),
                              *b = fsPrefixLookup(&new);
    if (a != b)
    {
        fsSetError(FS_ERR_NOSYS);
        return 0;
    }
    else if (!a)
        return 0;
    return !strcmp(old, new) ? 1 : a->rename(old, new);
}

canonical_original_code:
_Bool fsRename(char *old, char *new)
{
    struct fsOperations *a = fsPrefixLookup(&old); struct fsOperations *b = fsPrefixLookup(&new);
    if (a != b)
    {
        fsSetError(FS_ERR_NOSYS);
        return 0;
    }
    else if (!a)
        return 0;
    return !strcmp(old, new) ? 1 : a->rename(old, new);
}

variable_types:
old
char *

variable_types:
new
char *


fsRead

decompiled_code:
__int64 __fastcall fsRead(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 16))(qword_348[2 * a1]);
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 16))(qword_348[2 * a1]);
}

original_code:
ssize_t fsRead(int8_t fd, void *buf, size_t count)
{
    return openFiles[fd].ops->read(openFiles[fd].data, buf, count);
}

canonical_original_code:
long fsRead(char fd, void *buf, unsigned long count)
{
    return openFiles[fd].ops->read(openFiles[fd].data, buf, count);
}

variable_types:
buf
void *

variable_types:
count
unsigned long

return_type: long


fsTell

decompiled_code:
__int64 __fastcall fsTell(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 24))(qword_348[2 * a1]);
}

canonical_decompiled_code:
__int64 __fastcall func10(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 24))(qword_348[2 * a1]);
}

original_code:
fsOff_t fsTell(int8_t fd)
{
    return openFiles[fd].ops->tell(openFiles[fd].data);
}

canonical_original_code:
unsigned int fsTell(char fd)
{
    return openFiles[fd].ops->tell(openFiles[fd].data);
}


fsSeek

decompiled_code:
__int64 __fastcall fsSeek(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 32))(qword_348[2 * a1]);
}

canonical_decompiled_code:
__int64 __fastcall func11(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 32))(qword_348[2 * a1]);
}

original_code:
void fsSeek(int8_t fd, fsOff_t pos)
{
    return openFiles[fd].ops->seek(openFiles[fd].data, pos);
}

canonical_original_code:
void fsSeek(char fd, unsigned int pos)
{
    return openFiles[fd].ops->seek(openFiles[fd].data, pos);
}

variable_types:
pos
unsigned int


fsWrite

decompiled_code:
__int64 __fastcall fsWrite(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 40))(qword_348[2 * a1]);
}

canonical_decompiled_code:
__int64 __fastcall func12(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 40))(qword_348[2 * a1]);
}

original_code:
ssize_t fsWrite(int8_t fd, const void *buf, size_t count)
{
    return openFiles[fd].ops->write(openFiles[fd].data, buf, count);
}

canonical_original_code:
long fsWrite(char fd, void *buf, unsigned long count)
{
    return openFiles[fd].ops->write(openFiles[fd].data, buf, count);
}


fsFlush

decompiled_code:
__int64 __fastcall fsFlush(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 48))(qword_348[2 * a1]);
}

canonical_decompiled_code:
__int64 __fastcall func13(char a1)
{
return (*(__int64 (__fastcall **)(__int64))(openFiles[2 * a1] + 48))(qword_348[2 * a1]);
}

original_code:
_Bool fsFlush(int8_t fd)
{
    return openFiles[fd].ops->flush(openFiles[fd].data);
}

canonical_original_code:
_Bool fsFlush(char fd)
{
    return openFiles[fd].ops->flush(openFiles[fd].data);
}


fsClose

decompiled_code:
void __fastcall fsClose(char a1)
{
(*(void (__fastcall **)(__int64))(openFiles[2 * a1] + 56))(qword_348[2 * a1]);
fsFreeHandle(a1);
}

canonical_decompiled_code:
void __fastcall func14(char a1)
{
(*(void (__fastcall **)(__int64))(openFiles[2 * a1] + 56))(qword_348[2 * a1]);
func0(a1);
}

original_code:
_Bool fsClose(int8_t fd)
{
    _Bool res = openFiles[fd].ops->close(openFiles[fd].data);
    fsFreeHandle(fd);
    return res;
}

canonical_original_code:
_Bool fsClose(char fd)
{
    _Bool res = openFiles[fd].ops->close(openFiles[fd].data);
    fsFreeHandle(fd);
    return res;
}


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


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

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

variable_types:
milliseconds
unsigned short


msleep

decompiled_code:
int __fastcall msleep(__int64 a1)
{
unsigned int v1; // ebx
int v2; // eax
signed __int16 v3; // dx
int v4; // edi
__int64 v5; // rdi
__int16 v6; // dx
int v8; // [rsp+0h] [rbp-18h]

a1 = (unsigned __int16)a1;
v1 = (unsigned __int16)a1 + (unsigned int)getJiffies((unsigned __int16)a1);
while ( 1 )
{
v2 = getJiffies(a1);
v3 = v1 - v2;
if ( (__int16)(v1 - v2) <= 0 )
break;
while ( 1 )
{
if ( !alarmListHead )
return doMsleep(v3);
v4 = *(_DWORD *)(alarmListHead + 24LL);
if ( (int)(v4 - v1) >= 0 )
return doMsleep(v3);
a1 = (unsigned int)(v4 - v2);
if ( (__int16)a1 <= 0 )
break;
v5 = (unsigned int)(__int16)a1;
doMsleep(v5);
v2 = getJiffies(v5);
v3 = v6 - v2;
if ( v3 <= 0 )
return v8;
}
alarmsProcess();
}
return v8;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
unsigned int v1; // ebx
int v2; // eax
signed __int16 v3; // dx
int v4; // edi
__int64 v5; // rdi
__int16 v6; // dx
int v8; // [rsp+0h] [rbp-18h]

a1 = (unsigned __int16)a1;
v1 = (unsigned __int16)a1 + (unsigned int)func0((unsigned __int16)a1);
while ( 1 )
{
v2 = func0(a1);
v3 = v1 - v2;
if ( (__int16)(v1 - v2) <= 0 )
break;
while ( 1 )
{
if ( !alarmListHead )
return func1(v3);
v4 = *(_DWORD *)(alarmListHead + 24LL);
if ( (int)(v4 - v1) >= 0 )
return func1(v3);
a1 = (unsigned int)(v4 - v2);
if ( (__int16)a1 <= 0 )
break;
v5 = (unsigned int)(__int16)a1;
func1(v5);
v2 = func0(v5);
v3 = v6 - v2;
if ( v3 <= 0 )
return v8;
}
alarmsProcess();
}
return v8;
}

original_code:
void msleep(uint16_t ms)
{
    const uint32_t sleepEnd = (uint32_t) getJiffies() + ms;
    _Bool allTimeSpent;
    for (;;) {
        uint32_t now = (uint32_t) getJiffies();
        int16_t msToSleep = sleepEnd - now;
        if (msToSleep <= 0) break;
        Handle_t handle;
        (handle) = 0;
        Alarm_t *first = ((&alarmListHead)->slh_first);
        if (first && ((int32_t) ((first->jiffies) - (sleepEnd)) < 0)) {
            msToSleep = first->jiffies - now;
            ((void) handle);
            if (msToSleep <= 0) {
                alarmsProcess();
                continue;
            }
            allTimeSpent = 0;
        } else {
            ((void) handle);
            allTimeSpent = 1;
        }
        doMsleep(msToSleep);
        if (allTimeSpent) break;
    }
}

canonical_original_code:
void msleep(unsigned short ms)
{
    unsigned int sleepEnd = (uint32_t) getJiffies() + ms;
    _Bool allTimeSpent;
    for (;;) {
        unsigned int now = (uint32_t) getJiffies();
        short msToSleep = sleepEnd - now;
        if (msToSleep <= 0) break;
        unsigned short handle;
        (handle) = 0;
        struct Alarm_s *first = ((&alarmListHead)->slh_first);
        if (first && ((int) ((first->jiffies) - (sleepEnd)) < 0)) {
            msToSleep = first->jiffies - now;
            ((void) handle);
            if (msToSleep <= 0) {
                alarmsProcess();
                continue;
            }
            allTimeSpent = 0;
        } else {
            ((void) handle);
            allTimeSpent = 1;
        }
        doMsleep(msToSleep);
        if (allTimeSpent) break;
    }
}

variable_types:
ms
unsigned short

variable_types:
sleepEnd
unsigned int

variable_types:
now
unsigned int

variable_types: msToSleep short

variable_types: struct Alarm_s { struct { struct Alarm_s *sle_next; } chain; void (*callback)(void *); void *data; unsigned int jiffies; } *


doMsleep

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


assertionFailed

decompiled_code:
void __fastcall __noreturn assertionFailed(const char *a1, const char *a2, __int64 a3)
{
if ( print_buf )
{
debugPrintfFormat("ASSERTION FAILED:\r\n%s:%d: %s\r\n", a2, a3, a1);
serialPrint(print_buf);
}
assertionFailed_cold();
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1, const char *a2, __int64 a3)
{
if ( print_buf )
{
debugPrintfFormat("ASSERTION FAILED:\r\n%s:%d: %s\r\n", a2, a3, a1);
serialPrint(print_buf);
}
func4();
}

original_code:
void assertionFailed(const char * __restrict__ msg, const char * __restrict__ file,
                     int line)
{
    if (4 == 1) { serialPrintV("ASSERTION FAILED:\r\n%s:%d: %s\r\n", file, line, msg); } else { if (_print_buf) { debugPrintfFormat("ASSERTION FAILED:\r\n%s:%d: %s\r\n", file, line, msg); serialPrint(_print_buf); }; };
    panic();
}

canonical_original_code:
void assertionFailed(char *msg, char *file, int line)
{
    if (4 == 1) { serialPrintV("ASSERTION FAILED:\r\n%s:%d: %s\r\n", file, line, msg); } else { if (_print_buf) { debugPrintfFormat("ASSERTION FAILED:\r\n%s:%d: %s\r\n", file, line, msg); serialPrint(_print_buf); }; };
    panic();
}

variable_types:
msg
char *

variable_types:
file
char *

variable_types: line int


printWarning

decompiled_code:
__int64 __fastcall printWarning(const char *a1, const char *a2, __int64 a3)
{
__int64 result; // rax

if ( print_buf )
{
debugPrintfFormat("WARNING:\r\n%s:%d: %s\r\n", a2, a3, a1);
result = serialPrint(print_buf);
}
return result;
}

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

if ( print_buf )
{
debugPrintfFormat("WARNING:\r\n%s:%d: %s\r\n", a2, a3, a1);
result = serialPrint(print_buf);
}
return result;
}

original_code:
void printWarning(const char * __restrict__ msg, const char * __restrict__ file,
                  int line)
{
    if (4 == 1) { serialPrintV("WARNING:\r\n%s:%d: %s\r\n", file, line, msg); } else { if (_print_buf) { debugPrintfFormat("WARNING:\r\n%s:%d: %s\r\n", file, line, msg); serialPrint(_print_buf); }; };
}

canonical_original_code:
void printWarning(char *msg, char *file, int line)
{
    if (4 == 1) { serialPrintV("WARNING:\r\n%s:%d: %s\r\n", file, line, msg); } else { if (_print_buf) { debugPrintfFormat("WARNING:\r\n%s:%d: %s\r\n", file, line, msg); serialPrint(_print_buf); }; };
}


panic

decompiled_code:
void __noreturn panic()
{
abort();
}

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

original_code:
void panic(void)
{
    abort();
}

canonical_original_code:
void panic()
{
    abort();
}


fflush

decompiled_code:
__int64 fflush()
{
fatFsFileFlush();
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
fatFsFileFlush();
return 0LL;
}

original_code:
int fflush(FILE *fp)
{
    fatFsFileFlush(fp);
    return 0;
}

canonical_original_code:
int fflush(struct FILE_s *fp)
{
    fatFsFileFlush(fp);
    return 0;
}

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

return_type: int


fopenEx

original_code:
FILE *fopenEx(const char *__restrict__ filename,
              const char *__restrict__ modes,
              FILE *__restrict__ result)
{
    if (!result || result->isOpened) return ((void *)0);
    result->flags = 0;
    while (*modes) {
        switch (*modes) {
        case 'r':
            result->flags |= 00000000;
            break;
        case 'w':
            result->flags |= 00000001 | 00001000 | 00000100;
            break;
        case 'a':
            result->flags |= 00000001 | 00000100 | 00002000;
            break;
        case '+':
            result->flags &= ~(00000001 | 00000000);
            result->flags |= 00000002;
            break;
        case 't':
        case 'b':
            break;
        }
        modes++;
    }
    DirectoryEntry_t *de = fatFsFileSearch(filename, &result->directoryEntry);
    if (de == ((void *)0)) {
        if (result->flags & 00000100) {
            de = fatFsFileCreate(filename, &result->directoryEntry);
        }
    }
    if (de == ((void *)0)) {
        return ((void *)0);
    }
    result->firstCluster = 0;
    result->firstCluster |= de->startClusterLoword;
    result->currentCluster = result->firstCluster;
    result->position = 0;
    if (result->flags & 00001000) {
        result->fileSize = 0;
        if (de->fileSize != 0) {
            result->dirEntryDirty = 1;
            fatFsFileFlush(result);
        }
    } else {
        result->dirEntryDirty = 0;
        result->fileSize = de->fileSize;
    }
    result->isOpened = 1;
    return result;
}

canonical_original_code:
struct FILE_s *fopenEx(char *filename, char *modes, struct FILE_s *result)
{
    if (!result || result->isOpened) return ((void *)0);
    result->flags = 0;
    while (*modes) {
        switch (*modes) {
        case 'r':
            result->flags |= 00000000;
            break;
        case 'w':
            result->flags |= 00000001 | 00001000 | 00000100;
            break;
        case 'a':
            result->flags |= 00000001 | 00000100 | 00002000;
            break;
        case '+':
            result->flags &= ~(00000001 | 00000000);
            result->flags |= 00000002;
            break;
        case 't':
        case 'b':
            break;
        }
        modes++;
    }
    struct DirectoryEntry_s *de = fatFsFileSearch(filename, &result->directoryEntry);
    if (de == ((void *)0)) {
        if (result->flags & 00000100) {
            de = fatFsFileCreate(filename, &result->directoryEntry);
        }
    }
    if (de == ((void *)0)) {
        return ((void *)0);
    }
    result->firstCluster = 0;
    result->firstCluster |= de->startClusterLoword;
    result->currentCluster = result->firstCluster;
    result->position = 0;
    if (result->flags & 00001000) {
        result->fileSize = 0;
        if (de->fileSize != 0) {
            result->dirEntryDirty = 1;
            fatFsFileFlush(result);
        }
    } else {
        result->dirEntryDirty = 0;
        result->fileSize = de->fileSize;
    }
    result->isOpened = 1;
    return result;
}

variable_types:
filename
char *

variable_types:
modes
char *

variable_types:
result
struct FILE_s *

variable_types: struct DirectoryEntry_s { unsigned char filename[8]; unsigned char extension[3]; unsigned char attributes; unsigned char reserved; unsigned char timeCreatedSplitSeconds; unsigned short timeCreated; unsigned short dateCreated; unsigned short dateAccessed; unsigned short startClusterHiword; unsigned short timeWritten; unsigned short dateWritten; unsigned short startClusterLoword; unsigned int fileSize; } *

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


fclose

decompiled_code:
__int64 __fastcall fclose(_BYTE *a1)
{
fatFsFileClose();
*a1 = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
fatFsFileClose();
*a1 = 0;
return 0LL;
}

original_code:
int fclose(FILE *fp)
{
    fatFsFileClose(fp);
    fp->isOpened = 0;
    return 0;
}

canonical_original_code:
int fclose(struct FILE_s *fp)
{
    fatFsFileClose(fp);
    fp->isOpened = 0;
    return 0;
}

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


fgets

original_code:
char *fgets(char *__restrict__ s, int size, FILE *__restrict__ fp)
{
    uint16_t i;
    if (size == 0) return s;
    for (i = 0; i < size - 1; ++i) {
        uint8_t b = 0;
        fread(&b, 1, 1, fp);
        if (b == (-1)) {
            if (i == 0) return ((void *)0);
            break;
        }
        s[i] = b;
        if (b == '\n') break;
    }
    i++;
    s[i] = '\0';
    return s;
}

canonical_original_code:
char *fgets(char *s, int size, struct FILE_s *fp)
{
    unsigned short i;
    if (size == 0) return s;
    for (i = 0; i < size - 1; ++i) {
        unsigned char b = 0;
        fread(&b, 1, 1, fp);
        if (b == (-1)) {
            if (i == 0) return ((void *)0);
            break;
        }
        s[i] = b;
        if (b == '\n') break;
    }
    i++;
    s[i] = '\0';
    return s;
}

variable_types:
s
char *

variable_types: size int

variable_types:
i
unsigned short

variable_types:
b
unsigned char

return_type: char *


remove

decompiled_code:
__int64 remove()
{
fatFsFileRemove();
return 0LL;
}

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

original_code:
int remove(const char *filename)
{
    fatFsFileRemove(filename);
    return 0;
}

canonical_original_code:
int remove(char *filename)
{
    fatFsFileRemove(filename);
    return 0;
}


fseek

decompiled_code:
__int64 __fastcall fseek(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // esi

if ( !a3 )
{
if ( a2 < 0 )
return 0xFFFFFFFFLL;
v4 = 0;
goto LABEL_9;
}
v4 = *(_DWORD *)(a1 + 12);
if ( a3 != 1 )
v4 = *(_DWORD *)(a1 + 8);
if ( a2 >= 0 )
{
LABEL_9:
if ( !a2 || a2 + v4 <= *(unsigned int *)(a1 + 8) )
goto LABEL_6;
return 0xFFFFFFFFLL;
}
if ( v4 >= -a2 )
{
LABEL_6:
fatfsGoToPosition(a1, (unsigned int)a2 + v4);
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3)
{
unsigned int v4; // esi

if ( !a3 )
{
if ( a2 < 0 )
return 0xFFFFFFFFLL;
v4 = 0;
goto LABEL_9;
}
v4 = *(_DWORD *)(a1 + 12);
if ( a3 != 1 )
v4 = *(_DWORD *)(a1 + 8);
if ( a2 >= 0 )
{
LABEL_9:
if ( !a2 || a2 + v4 <= *(unsigned int *)(a1 + 8) )
goto LABEL_6;
return 0xFFFFFFFFLL;
}
if ( v4 >= -a2 )
{
LABEL_6:
fatfsGoToPosition(a1, (unsigned int)a2 + v4);
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int fseek(FILE *fp, long offset, int whence)
{
    uint32_t newPos;
    switch (whence) {
    case 0:
        newPos = 0;
        break;
    case 1:
        newPos = fp->position;
        break;
    case 2:
    default:
        newPos = fp->fileSize;
        break;
    }
    if (offset < 0 && newPos < -offset) {
        return -1;
    }
    if (offset > 0 && newPos + offset > fp->fileSize) {
        return -1;
    }
    newPos += offset;
    fatfsGoToPosition(fp, newPos);
    return 0;
}

canonical_original_code:
int fseek(struct FILE_s *fp, long offset, int whence)
{
    unsigned int newPos;
    switch (whence) {
    case 0:
        newPos = 0;
        break;
    case 1:
        newPos = fp->position;
        break;
    case 2:
    default:
        newPos = fp->fileSize;
        break;
    }
    if (offset < 0 && newPos < -offset) {
        return -1;
    }
    if (offset > 0 && newPos + offset > fp->fileSize) {
        return -1;
    }
    newPos += offset;
    fatfsGoToPosition(fp, newPos);
    return 0;
}

variable_types: offset long

variable_types: whence int

variable_types:
newPos
unsigned int


doMsleep

variable_types:
milliseconds
unsigned short


networkingInit

decompiled_code:
// attributes: thunk
__int64 networkingInit()
{
return networkingInitArch();
}

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

original_code:
void networkingInit(void) {
    networkingInitArch();
}

canonical_original_code:
void networkingInit() {
    networkingInitArch();
}


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 *


pcRadioInit

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

if ( !pcRadioInitialized )
{
pcRadioInitialized = 1;
pcRadioIsOn = 0;
result = pthread_mutex_init(&pcRadioSendMutex, 0LL);
}
return result;
}

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

if ( !pcRadioInitialized )
{
pcRadioInitialized = 1;
pcRadioIsOn = 0;
result = pthread_mutex_init(&pcRadioSendMutex, 0LL);
}
return result;
}

original_code:
void pcRadioInit(void) {
    if (pcRadioInitialized) return;
    pcRadioInitialized = 1;
    pcRadioIsOn = 0;
    pthread_mutex_init(&pcRadioSendMutex, ((void *)0));
}

canonical_original_code:
void pcRadioInit() {
    if (pcRadioInitialized) return;
    pcRadioInitialized = 1;
    pcRadioIsOn = 0;
    pthread_mutex_init(&pcRadioSendMutex, ((void *)0));
}


pcRadioReinit

decompiled_code:
void pcRadioReinit()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void pcRadioReinit(void) {
}

canonical_original_code:
void pcRadioReinit() {
}


pcRadioRecv

decompiled_code:
__int64 __fastcall pcRadioRecv(void *a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // ebx

if ( pcRadioIsOn )
{
result = 0LL;
if ( (unsigned __int16)pcRadioBufLen > 2u )
{
v3 = (unsigned __int16)pcRadioBufLen - 2;
if ( (unsigned __int16)v3 > (unsigned __int16)a2 )
v3 = a2;
memcpy(a1, (const void *)(unsigned int)((_DWORD)&pcRadioBuf + 2), (unsigned __int16)v3);
pcRadioBufLen = 0;
result = v3;
}
}
else
{
serialPrintV("radioRecv: pcRadio is off\n");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *a1, unsigned int a2)
{
__int64 result; // rax
unsigned int v3; // ebx

if ( pcRadioIsOn )
{
result = 0LL;
if ( (unsigned __int16)pcRadioBufLen > 2u )
{
v3 = (unsigned __int16)pcRadioBufLen - 2;
if ( (unsigned __int16)v3 > (unsigned __int16)a2 )
v3 = a2;
memcpy(a1, (const void *)(unsigned int)((_DWORD)&pcRadioBuf + 2), (unsigned __int16)v3);
pcRadioBufLen = 0;
result = v3;
}
}
else
{
func0("radioRecv: pcRadio is off\n");
result = 0LL;
}
return result;
}

original_code:
int16_t pcRadioRecv(void *buffer, uint16_t buffLen) {
    if (!pcRadioIsOn) {
        if (1 == 1) { serialPrintV("radioRecv: pcRadio is off\n"); } else { if (_print_buf) { debugPrintfFormat("radioRecv: pcRadio is off\n"); serialPrint(_print_buf); }; };
        return 0;
    }
    if (pcRadioBufLen <= sizeof(PcRadioPackSize_t)) return 0;
    uint16_t len = pcRadioBufLen - sizeof(PcRadioPackSize_t);
    if (len > buffLen) len = buffLen;
    memcpy(buffer, pcRadioBuf + sizeof(PcRadioPackSize_t), len);
    pcRadioBufLen = 0;
    return len;
}

canonical_original_code:
short pcRadioRecv(void *buffer, unsigned short buffLen) {
    if (!pcRadioIsOn) {
        if (1 == 1) { serialPrintV("radioRecv: pcRadio is off\n"); } else { if (_print_buf) { debugPrintfFormat("radioRecv: pcRadio is off\n"); serialPrint(_print_buf); }; };
        return 0;
    }
    if (pcRadioBufLen <= sizeof(PcRadioPackSize_t)) return 0;
    unsigned short len = pcRadioBufLen - sizeof(PcRadioPackSize_t);
    if (len > buffLen) len = buffLen;
    memcpy(buffer, pcRadioBuf + sizeof(PcRadioPackSize_t), len);
    pcRadioBufLen = 0;
    return len;
}

variable_types:
buffer
void *

variable_types:
buffLen
unsigned short

variable_types:
len
unsigned short

return_type: short


pcRadioDiscard

decompiled_code:
void pcRadioDiscard()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void pcRadioDiscard(void) {
}

canonical_original_code:
void pcRadioDiscard() {
}


pcRadioSetReceiveHandle

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

result = pcRadioCallback;
pcRadioCallback = a1;
return result;
}

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

result = pcRadioCallback;
pcRadioCallback = a1;
return result;
}

original_code:
RadioRecvFunction pcRadioSetReceiveHandle(RadioRecvFunction functionHandle) {
    RadioRecvFunction old = pcRadioCallback;
    pcRadioCallback = functionHandle;
    return old;
}

canonical_original_code:
void (*pcRadioSetReceiveHandle(void (*functionHandle)()))() {
    void (*old)() = pcRadioCallback;
    pcRadioCallback = functionHandle;
    return old;
}

variable_types:
functionHandle
void (*)()

variable_types:
old
void (*)()


pcRadioOn

decompiled_code:
void pcRadioOn()
{
pcRadioIsOn = 1;
}

canonical_decompiled_code:
void func8()
{
pcRadioIsOn = 1;
}

original_code:
void pcRadioOn(void) {
    pcRadioIsOn = 1;
}

canonical_original_code:
void pcRadioOn() {
    pcRadioIsOn = 1;
}


pcRadioOff

decompiled_code:
void pcRadioOff()
{
pcRadioIsOn = 0;
}

canonical_decompiled_code:
void func9()
{
pcRadioIsOn = 0;
}

original_code:
void pcRadioOff(void) {
    pcRadioIsOn = 0;
}

canonical_original_code:
void pcRadioOff() {
    pcRadioIsOn = 0;
}


pcRadioGetRSSI

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

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

original_code:
int pcRadioGetRSSI(void) {
    return 0;
}

canonical_original_code:
int pcRadioGetRSSI() {
    return 0;
}


pcRadioGetLastRSSI

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

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

original_code:
int8_t pcRadioGetLastRSSI(void) {
    return 0;
}

canonical_original_code:
char pcRadioGetLastRSSI() {
    return 0;
}


pcRadioGetLastLQI

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

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

original_code:
uint8_t pcRadioGetLastLQI(void) {
    return 0;
}

canonical_original_code:
unsigned char pcRadioGetLastLQI() {
    return 0;
}

return_type: unsigned char


pcRadioSetChannel

decompiled_code:
void pcRadioSetChannel()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void pcRadioSetChannel(int channel) {
    return;
}

variable_types: channel int


pcRadioSetTxPower

decompiled_code:
void pcRadioSetTxPower()
{
;
}

canonical_decompiled_code:
void func14()
{
;
}

original_code:
void pcRadioSetTxPower(uint8_t power) {
    return;
}

canonical_original_code:
void pcRadioSetTxPower(unsigned char power) {
    return;
}

variable_types:
power
unsigned char


pcRadioIsChannelClear

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

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

original_code:
_Bool pcRadioIsChannelClear(void) {
    return 1;
}

canonical_original_code:
_Bool pcRadioIsChannelClear() {
    return 1;
}


ledsOpen

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

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

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

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


ledsClose

decompiled_code:
// attributes: thunk
__int64 ledsClose()
{
return ledsOpen();
}

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

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

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


ledsRead

original_code:
static ssize_t ledsRead(void *buf, size_t count)
{
    uint_t leds = ledsGet();
    size_t i;
    char *b = buf;
    count = ((4 + 1) < (count) ? (4 + 1) : (count));
    for (i = 0; i < count; i++)
    {
        if (i < 4)
            b[i] = leds & (1U << (4 - 1 - i)) ? '1' : '0';
        else
            b[i] = '\n';
    }
    return count;
}

canonical_original_code:
static long ledsRead(void *buf, unsigned long count)
{
    unsigned int leds = ledsGet();
    unsigned long i;
    char *b = buf;
    count = ((4 + 1) < (count) ? (4 + 1) : (count));
    for (i = 0; i < count; i++)
    {
        if (i < 4)
            b[i] = leds & (1U << (4 - 1 - i)) ? '1' : '0';
        else
            b[i] = '\n';
    }
    return count;
}

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types:
leds
unsigned int

variable_types:
i
unsigned long

variable_types:
b
char *


ledsWrite

decompiled_code:
__int64 __fastcall ledsWrite(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v3; // rbx
unsigned int v4; // eax
__int64 v5; // rcx
__int64 v6; // rdi
__int64 v7; // rdx
char v8; // al
__int64 result; // rax

v3 = 5LL;
v4 = ledsGet();
v5 = 3LL;
if ( a2 <= 5 )
v3 = a2;
v6 = v4;
v7 = 0LL;
if ( a2 )
{
do
{
v8 = *(_BYTE *)(a1 + v7);
if ( v8 == 49 )
{
v6 = (1 << v5) | (unsigned int)v6;
}
else if ( v8 > 49 )
{
if ( (v8 & 0xDF) != 88 )
goto LABEL_14;
LODWORD(v6) = v6 ^ (1 << v5);
}
else if ( v8 != 46 )
{
if ( v8 != 48 )
goto LABEL_14;
v6 = ~(1 << v5) & (unsigned int)v6;
}
if ( v3 <= ++v7 )
goto LABEL_12;
--v5;
}
while ( v7 != 4 );
if ( *(_BYTE *)(a1 + 4) == 10 )
goto LABEL_12;
LABEL_14:
fsSetError(2LL, 1LL, v7, v5);
result = -1LL;
}
else
{
LABEL_12:
ledsSet(v6, 1LL, v7, v5);
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v3; // rbx
unsigned int v4; // eax
__int64 v5; // rcx
__int64 v6; // rdi
__int64 v7; // rdx
char v8; // al
__int64 result; // rax

v3 = 5LL;
v4 = ledsGet();
v5 = 3LL;
if ( a2 <= 5 )
v3 = a2;
v6 = v4;
v7 = 0LL;
if ( a2 )
{
do
{
v8 = *(_BYTE *)(a1 + v7);
if ( v8 == 49 )
{
v6 = (1 << v5) | (unsigned int)v6;
}
else if ( v8 > 49 )
{
if ( (v8 & 0xDF) != 88 )
goto LABEL_14;
LODWORD(v6) = v6 ^ (1 << v5);
}
else if ( v8 != 46 )
{
if ( v8 != 48 )
goto LABEL_14;
v6 = ~(1 << v5) & (unsigned int)v6;
}
if ( v3 <= ++v7 )
goto LABEL_12;
--v5;
}
while ( v7 != 4 );
if ( *(_BYTE *)(a1 + 4) == 10 )
goto LABEL_12;
LABEL_14:
fsSetError(2LL, 1LL, v7, v5);
result = -1LL;
}
else
{
LABEL_12:
ledsSet(v6, 1LL, v7, v5);
result = v3;
}
return result;
}

original_code:
static ssize_t ledsWrite(const void *buf, size_t count)
{
    uint_t leds = ledsGet();
    size_t i;
    const char *b = buf;
    count = ((4 + 1) < (count) ? (4 + 1) : (count));
    for (i = 0; i < count; i++)
    {
        if (i == 4)
        {
            if (b[i] == '\n')
                continue;
            else
            {
                fsSetError(FS_ERR_IO);
                return -1;
            }
        }
        switch (b[i])
        {
            case '0':
                leds &= ~(1U << (4 - 1 - i));
                break;
            case '1':
                leds |= 1U << (4 - 1 - i);
                break;
            case 'x':
            case 'X':
                leds ^= 1U << (4 - 1 - i);
            case '.':
                break;
            default:
                fsSetError(FS_ERR_IO);
                return -1;
        }
    }
    ledsSet(leds);
    return count;
}

canonical_original_code:
static long ledsWrite(void *buf, unsigned long count)
{
    unsigned int leds = ledsGet();
    unsigned long i;
    char *b = buf;
    count = ((4 + 1) < (count) ? (4 + 1) : (count));
    for (i = 0; i < count; i++)
    {
        if (i == 4)
        {
            if (b[i] == '\n')
                continue;
            else
            {
                fsSetError(FS_ERR_IO);
                return -1;
            }
        }
        switch (b[i])
        {
            case '0':
                leds &= ~(1U << (4 - 1 - i));
                break;
            case '1':
                leds |= 1U << (4 - 1 - i);
                break;
            case 'x':
            case 'X':
                leds ^= 1U << (4 - 1 - i);
            case '.':
                break;
            default:
                fsSetError(FS_ERR_IO);
                return -1;
        }
    }
    ledsSet(leds);
    return count;
}


doMsleep

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

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


fsBlockAlarmCallback

decompiled_code:
// attributes: thunk
__int64 fsBlockAlarmCallback()
{
return extFlashSleep();
}

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

original_code:
void fsBlockAlarmCallback(void *data)
{
    (void)data;
    extFlashSleep();
}

variable_types:
data
void *


fsBlockDelEntry

decompiled_code:
__int64 __fastcall fsBlockDelEntry(char a1)
{
return fsBlockRenameEntry(a1, &s1);
}

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

original_code:
static inline void fsBlockDelEntry(int8_t id)
{
    fsBlockRenameEntry(id, "");
}

canonical_original_code:
static inline void fsBlockDelEntry(char id)
{
    fsBlockRenameEntry(id, "");
}

variable_types: id char


findAndLockFcb

decompiled_code:
char *__fastcall findAndLockFcb(char a1)
{
if ( a1 == fcbs )
return &fcbs;
if ( a1 == byte_6AC )
return &byte_6AC;
if ( a1 == -1 )
fsSetError(1LL);
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func3(char a1)
{
if ( a1 == fcbs )
return &fcbs;
if ( a1 == byte_6AC )
return &byte_6AC;
if ( a1 == -1 )
fsSetError(1LL);
return 0LL;
}

original_code:
static struct fsFileControlBlock *findAndLockFcb(int8_t id)
{
    size_t i;
    for (i = 0; i < 2; i++)
    {
        mutexLock(&fcbs[i].mutex);
        if (fcbs[i].id == id)
            return fcbs + i;
        mutexUnlock(&fcbs[i].mutex);
    }
    if (id == -1)
        fsSetError(FS_ERR_NOMEM);
    return ((void *)0);
}

canonical_original_code:
static struct fsFileControlBlock *findAndLockFcb(char id)
{
    unsigned long i;
    for (i = 0; i < 2; i++)
    {
        mutexLock(&fcbs[i].mutex);
        if (fcbs[i].id == id)
            return fcbs + i;
        mutexUnlock(&fcbs[i].mutex);
    }
    if (id == -1)
        fsSetError(FS_ERR_NOMEM);
    return ((void *)0);
}

return_type: struct fsFileControlBlock { struct Mutex_s {  } mutex; char id; unsigned char refcount; unsigned short first; unsigned int size; unsigned short crc; _Bool openForWrite; } *

user_defined_types: struct fsFileControlBlock { struct Mutex_s mutex; char id; unsigned char refcount; unsigned short first; unsigned int size; unsigned short crc; _Bool openForWrite; }


allocFcb

decompiled_code:
char *allocFcb()
{
return findAndLockFcb(-1);
}

canonical_decompiled_code:
char *func4()
{
return func3(-1);
}

original_code:
static inline struct fsFileControlBlock *allocFcb(void)
{
    return findAndLockFcb(-1);
}

canonical_original_code:
static inline struct fsFileControlBlock *allocFcb()
{
    return findAndLockFcb(-1);
}


fsBlockInit

decompiled_code:
void fsBlockInit()
{
__int64 i; // rbx
char v1; // di
__int16 v2[5]; // [rsp+Eh] [rbp-Ah] BYREF

eepromRead(0LL, v2, 2LL);
if ( v2[0] != -31408 )
{
fsBlockFreeAll();
for ( i = 0LL; i != 13; ++i )
{
v1 = i;
fsBlockDelEntry(v1);
}
v2[0] = -31408;
eepromWrite(0LL, v2, 2LL);
}
alarmInit_constprop_0();
fcbs = -1;
byte_6AC = -1;
}

canonical_decompiled_code:
void func7()
{
__int64 i; // rbx
char v1; // di
__int16 v2[5]; // [rsp+Eh] [rbp-Ah] BYREF

eepromRead(0LL, v2, 2LL);
if ( v2[0] != -31408 )
{
fsBlockFreeAll();
for ( i = 0LL; i != 13; ++i )
{
v1 = i;
func2(v1);
}
v2[0] = -31408;
eepromWrite(0LL, v2, 2LL);
}
func6();
fcbs = -1;
byte_6AC = -1;
}

original_code:
void fsBlockInit(void)
{
    size_t i;
    uint16_t check;
    eepromRead(0, &check, sizeof(check));
    if (check != 0x8550)
    {
        fsBlockFreeAll();
        for (i = 0; i < ((256 - (sizeof(uint16_t) + (32 * 4 * 2 / 8))) / sizeof(struct fsFileEntry)); i++)
            fsBlockDelEntry(i);
        check = 0x8550;
        eepromWrite(0, &check, sizeof(check));
    }
    mutexInit(&rootMutex);
    mutexInit(&fsBlkTableMutex);
    mutexInit(&fsBlkHandleMutex);
    alarmInit(&fsBlockFlashAlarm, fsBlockAlarmCallback, ((void *)0));
    for (i = 0; i < 2; i++)
    {
        mutexInit(&fcbs[i].mutex);
        fcbs[i].id = -1;
    }
}

canonical_original_code:
void fsBlockInit()
{
    unsigned long i;
    unsigned short check;
    eepromRead(0, &check, sizeof(check));
    if (check != 0x8550)
    {
        fsBlockFreeAll();
        for (i = 0; i < ((256 - (sizeof(unsigned short) + (32 * 4 * 2 / 8))) / sizeof(struct fsFileEntry)); i++)
            fsBlockDelEntry(i);
        check = 0x8550;
        eepromWrite(0, &check, sizeof(check));
    }
    mutexInit(&rootMutex);
    mutexInit(&fsBlkTableMutex);
    mutexInit(&fsBlkHandleMutex);
    alarmInit(&fsBlockFlashAlarm, fsBlockAlarmCallback, ((void *)0));
    for (i = 0; i < 2; i++)
    {
        mutexInit(&fcbs[i].mutex);
        fcbs[i].id = -1;
    }
}

variable_types:
check
unsigned short

user_defined_types: struct fsFileEntry { char name[7]; unsigned short first; unsigned int size; unsigned char attr; unsigned short lastCrc; }


fsBlockCloseFile

decompiled_code:
__int64 __fastcall fsBlockCloseFile(char *a1)
{
__int64 result; // rax

if ( a1[1]-- == 1 )
{
result = fsBlockSyncEntry(a1);
*a1 = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1)
{
__int64 result; // rax

if ( a1[1]-- == 1 )
{
result = func9(a1);
*a1 = -1;
}
return result;
}

original_code:
void fsBlockCloseFile(struct fsFileControlBlock *file)
{
    mutexLock(&file->mutex);
    if (--file->refcount == 0)
    {
        fsBlockSyncEntry(file);
        file->id = -1;
    }
    mutexUnlock(&file->mutex);
}

variable_types: struct fsFileControlBlock { struct Mutex_s {  } mutex; char id; unsigned char refcount; unsigned short first; unsigned int size; unsigned short crc; _Bool openForWrite; } *


fsBlockRename

decompiled_code:
__int64 __fastcall fsBlockRename(char *s1, char *a2)
{
unsigned int v2; // er13
char v3; // bp
__int64 result; // rax

if ( strlen(s1) > 7 )
{
fsSetError(4LL);
result = 0LL;
}
else if ( strlen(a2) > 7 )
{
fsSetError(7LL);
result = 0LL;
}
else
{
v2 = 0;
v3 = fsBlockFindEntry(s1);
if ( v3 == -1 )
return v2;
if ( (unsigned __int8)fsBlockFindEntry(a2) == 0xFF )
{
v2 = 1;
fsBlockRenameEntry(v3, a2);
return v2;
}
fsSetError(5LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *s1, char *a2)
{
unsigned int v2; // er13
char v3; // bp
__int64 result; // rax

if ( strlen(s1) > 7 )
{
fsSetError(4LL);
result = 0LL;
}
else if ( strlen(a2) > 7 )
{
fsSetError(7LL);
result = 0LL;
}
else
{
v2 = 0;
v3 = func5(s1);
if ( v3 == -1 )
return v2;
if ( (unsigned __int8)func5(a2) == 0xFF )
{
v2 = 1;
func1(v3, a2);
return v2;
}
fsSetError(5LL);
result = 0LL;
}
return result;
}

original_code:
_Bool fsBlockRename(const char *old, const char *new)
{
    int8_t entry;
    if (strlen(old) > 7)
    {
        fsSetError(FS_ERR_NOENT);
        return 0;
    }
    else if (strlen(new) > 7)
    {
        fsSetError(FS_ERR_INVAL);
        return 0;
    }
    mutexLock(&rootMutex);
    entry = fsBlockFindEntry(old);
    if (entry != -1)
    {
        if (fsBlockFindEntry(new) != -1)
        {
            fsSetError(FS_ERR_EXIST);
            entry = -1;
        }
        else
            fsBlockRenameEntry(entry, new);
    }
    mutexUnlock(&rootMutex);
    return entry != -1;
}

canonical_original_code:
_Bool fsBlockRename(char *old, char *new)
{
    char entry;
    if (strlen(old) > 7)
    {
        fsSetError(FS_ERR_NOENT);
        return 0;
    }
    else if (strlen(new) > 7)
    {
        fsSetError(FS_ERR_INVAL);
        return 0;
    }
    mutexLock(&rootMutex);
    entry = fsBlockFindEntry(old);
    if (entry != -1)
    {
        if (fsBlockFindEntry(new) != -1)
        {
            fsSetError(FS_ERR_EXIST);
            entry = -1;
        }
        else
            fsBlockRenameEntry(entry, new);
    }
    mutexUnlock(&rootMutex);
    return entry != -1;
}

variable_types:
old
char *

variable_types:
new
char *

variable_types: entry char


writeFile

decompiled_code:
__int64 __fastcall writeFile(char a1, unsigned int a2)
{
unsigned int v2; // er13
__int64 v4; // rdx
__int64 v5; // rsi
__int64 result; // rax

v2 = 0;
while ( 1 )
{
v4 = a2 - v2;
v5 = v2 % 0x2A;
if ( (unsigned int)v4 > 42 - (int)v5 )
v4 = (unsigned int)(42 - v5);
result = fsWrite((unsigned int)a1, &data[v5], v4);
if ( result <= 0 )
break;
v2 += result;
if ( a2 <= v2 )
return result;
}
assertionFailed("r > 0", "main.c", 69LL);
return readFile();
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1, unsigned int a2)
{
unsigned int v2; // er13
__int64 v4; // rdx
__int64 v5; // rsi
__int64 result; // rax

v2 = 0;
while ( 1 )
{
v4 = a2 - v2;
v5 = v2 % 0x2A;
if ( (unsigned int)v4 > 42 - (int)v5 )
v4 = (unsigned int)(42 - v5);
result = fsWrite((unsigned int)a1, &data[v5], v4);
if ( result <= 0 )
break;
v2 += result;
if ( a2 <= v2 )
return result;
}
assertionFailed("r > 0", "main.c", 69LL);
return func1();
}

original_code:
static void writeFile(int8_t f, fsOff_t size)
{
    fsOff_t written = 0;
    while (written < size)
    {
        ssize_t r = fsWrite(f, data + written % 42,
                            ((42 - written % 42) < (size - written) ? (42 - written % 42) : (size - written)));
        ((r > 0) ? (void)0 : assertionFailed("r > 0", "main.c", 69));
        written += r;
    }
}

canonical_original_code:
static void writeFile(char f, unsigned int size)
{
    unsigned int written = 0;
    while (written < size)
    {
        long r = fsWrite(f, data + written % 42,
                            ((42 - written % 42) < (size - written) ? (42 - written % 42) : (size - written)));
        ((r > 0) ? (void)0 : assertionFailed("r > 0", "main.c", 69));
        written += r;
    }
}

variable_types: f char

variable_types:
size
unsigned int

variable_types:
written
unsigned int

variable_types: r long


readFile

decompiled_code:
__int64 __fastcall readFile(char a1, unsigned int a2)
{
unsigned int v2; // er14
__int64 *v3; // rax
signed __int64 v4; // rax
int v5; // er15
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // rax
__int64 v9; // rcx
__int64 result; // rax

v2 = 0;
do
{
v6 = v2 % 0x2A;
v3 = &buf_1;
v7 = 42 - v2 % 0x2A;
if ( v7 >= 8 )
{
LODWORD(v8) = 0;
do
{
v9 = (unsigned int)v8;
v8 = (unsigned int)(v8 + 8);
*(__int64 *)((char *)&buf_1 + v9) = 0LL;
}
while ( (unsigned int)v8 < (v7 & 0xFFFFFFF8) );
v3 = (__int64 *)((char *)&buf_1 + v8);
}
if ( (v7 & 4) != 0 )
{
*(_DWORD *)v3 = 0;
v3 = (__int64 *)((char *)v3 + 4);
}
if ( (v7 & 2) != 0 )
{
v3 = (__int64 *)((char *)v3 + 2);
*((_WORD *)v3 - 1) = 0;
}
if ( (v7 & 1) != 0 )
*(_BYTE *)v3 = 0;
v4 = fsRead((unsigned int)a1, &buf_1, 42 - v6);
v5 = v4;
if ( v4 <= 0 )
{
assertionFailed("r > 0", "main.c", 88LL);
LABEL_18:
assertionFailed("!memcmp(buf, data + read % BUFSIZE, r)", "main.c", 89LL);
goto LABEL_19;
}
if ( memcmp(&buf_1, &data[v6], v4) )
goto LABEL_18;
v2 += v5;
}
while ( a2 > v2 );
result = fsRead((unsigned int)a1, &buf_1, 42LL);
if ( !result )
return result;
LABEL_19:
assertionFailed("fsRead(f, buf, BUFSIZE) == 0", "main.c", 93LL);
return mark();
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, unsigned int a2)
{
unsigned int v2; // er14
__int64 *v3; // rax
signed __int64 v4; // rax
int v5; // er15
unsigned int v6; // ebx
unsigned int v7; // edx
__int64 v8; // rax
__int64 v9; // rcx
__int64 result; // rax

v2 = 0;
do
{
v6 = v2 % 0x2A;
v3 = &buf_1;
v7 = 42 - v2 % 0x2A;
if ( v7 >= 8 )
{
LODWORD(v8) = 0;
do
{
v9 = (unsigned int)v8;
v8 = (unsigned int)(v8 + 8);
*(__int64 *)((char *)&buf_1 + v9) = 0LL;
}
while ( (unsigned int)v8 < (v7 & 0xFFFFFFF8) );
v3 = (__int64 *)((char *)&buf_1 + v8);
}
if ( (v7 & 4) != 0 )
{
*(_DWORD *)v3 = 0;
v3 = (__int64 *)((char *)v3 + 4);
}
if ( (v7 & 2) != 0 )
{
v3 = (__int64 *)((char *)v3 + 2);
*((_WORD *)v3 - 1) = 0;
}
if ( (v7 & 1) != 0 )
*(_BYTE *)v3 = 0;
v4 = fsRead((unsigned int)a1, &buf_1, 42 - v6);
v5 = v4;
if ( v4 <= 0 )
{
assertionFailed("r > 0", "main.c", 88LL);
LABEL_18:
assertionFailed("!memcmp(buf, data + read % BUFSIZE, r)", "main.c", 89LL);
goto LABEL_19;
}
if ( memcmp(&buf_1, &data[v6], v4) )
goto LABEL_18;
v2 += v5;
}
while ( a2 > v2 );
result = fsRead((unsigned int)a1, &buf_1, 42LL);
if ( !result )
return result;
LABEL_19:
assertionFailed("fsRead(f, buf, BUFSIZE) == 0", "main.c", 93LL);
return func2();
}

original_code:
static void readFile(int8_t f, fsOff_t size)
{
    static char buf[42];
    fsOff_t read = 0;
    while (read < size)
    {
        ssize_t r;
        memset(buf, 0, 42 - read % 42);
        r = fsRead(f, buf, 42 - read % 42);
        ((r > 0) ? (void)0 : assertionFailed("r > 0", "main.c", 88));
        ((!memcmp(buf, data + read % 42, r)) ? (void)0 : assertionFailed("!memcmp(buf, data + read % BUFSIZE, r)", "main.c", 89));
        read += r;
    }
    ((fsRead(f, buf, 42) == 0) ? (void)0 : assertionFailed("fsRead(f, buf, BUFSIZE) == 0", "main.c", 93));
}

canonical_original_code:
static void readFile(char f, unsigned int size)
{
    static char buf[42];
    unsigned int read = 0;
    while (read < size)
    {
        long r;
        memset(buf, 0, 42 - read % 42);
        r = fsRead(f, buf, 42 - read % 42);
        ((r > 0) ? (void)0 : assertionFailed("r > 0", "main.c", 88));
        ((!memcmp(buf, data + read % 42, r)) ? (void)0 : assertionFailed("!memcmp(buf, data + read % BUFSIZE, r)", "main.c", 89));
        read += r;
    }
    ((fsRead(f, buf, 42) == 0) ? (void)0 : assertionFailed("fsRead(f, buf, BUFSIZE) == 0", "main.c", 93));
}

variable_types:
buf
char [42]

variable_types:
read
unsigned int


mark

decompiled_code:
__int64 mark()
{
__int64 result; // rax
__int64 v1; // rsi

result = (unsigned int)counter_0;
v1 = (unsigned int)++counter_0;
if ( print_buf )
{
debugPrintfFormat("Test %2u passed\n", v1);
result = serialPrint(print_buf);
}
return result;
}

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

result = (unsigned int)counter_0;
v1 = (unsigned int)++counter_0;
if ( print_buf )
{
debugPrintfFormat("Test %2u passed\n", v1);
result = serialPrint(print_buf);
}
return result;
}

original_code:
static void mark(void)
{
    static unsigned counter = 0;
    counter++;
    if (2 == 1) { serialPrintV("Test %2u passed\n", counter); } else { if (_print_buf) { debugPrintfFormat("Test %2u passed\n", counter); serialPrint(_print_buf); }; };
}

canonical_original_code:
static void mark()
{
    static unsigned int counter = 0;
    counter++;
    if (2 == 1) { serialPrintV("Test %2u passed\n", counter); } else { if (_print_buf) { debugPrintfFormat("Test %2u passed\n", counter); serialPrint(_print_buf); }; };
}

variable_types:
counter
unsigned int


doMsleep

variable_types:
milliseconds
unsigned short


gpsCharRecv

decompiled_code:
__int64 __fastcall gpsCharRecv(unsigned __int8 a1)
{
return serialSendByte(1LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1)
{
return serialSendByte(1LL, a1);
}

original_code:
void gpsCharRecv(uint8_t b)
{
    serialSendByte(1, b);
}

canonical_original_code:
void gpsCharRecv(unsigned char b)
{
    serialSendByte(1, b);
}

variable_types:
b
unsigned char


usbCharRecv

decompiled_code:
__int64 __fastcall usbCharRecv(unsigned __int8 a1)
{
return serialSendByte(0LL, a1);
}

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

original_code:
void usbCharRecv(uint8_t b)
{
    serialSendByte(0, b);
}

canonical_original_code:
void usbCharRecv(unsigned char b)
{
    serialSendByte(0, b);
}


redLedOn

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

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

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

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


doMsleep

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


getJiffies

decompiled_code:
__int64 getJiffies()
{
return jiffies;
}

canonical_decompiled_code:
__int64 func0()
{
return jiffies;
}

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

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

variable_types:
handle
unsigned short

variable_types:
result
unsigned int


platformFixRssi

decompiled_code:
__int64 __fastcall platformFixRssi(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
return a1;
}

original_code:
static inline int16_t platformFixRssi(uint16_t rssi)
{
    return (int16_t) rssi;
}

canonical_original_code:
static inline short platformFixRssi(unsigned short rssi)
{
    return (short) rssi;
}

variable_types:
rssi
unsigned short


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 *


redLedToggle

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

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

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

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

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

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


radioGetLastRSSI

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

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

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

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


radioGetLastLQI

decompiled_code:
// attributes: thunk
__int64 radioGetLastLQI()
{
return pcRadioGetLastLQI();
}

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

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

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


radioOn

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

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

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

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


doMsleep

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

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

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

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


addAvgStatistics

decompiled_code:
__int64 __fastcall addAvgStatistics(int a1, __int16 a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // er8
__int16 v5; // r9
__int16 v6; // bp
unsigned int v7; // edx
int v8; // eax
int v9; // eax
int v10; // eax
bool v11; // zf
__int16 v12; // dx
unsigned __int16 v13; // bx
int v14; // ecx
int v15; // edi
unsigned __int16 v16; // r14
unsigned __int16 v17; // r12
__int16 v18; // ax
__int16 v19; // r13
__int16 v20; // si
unsigned __int64 v21; // r15
__int64 result; // rax
__int16 v23; // si
int v24; // ecx
__int16 v25; // cx
__int16 v26; // di
__int64 v27; // rcx
int v28; // ecx
int v29; // ebx
int v30; // eax
int v31; // edx
int v32; // er13
int v33; // er14
__int16 v34; // ax
__int64 v35; // rdx
__int64 v36; // rcx
__int16 v37[44]; // [rsp+0h] [rbp-58h] BYREF

v4 = (unsigned __int16)a1;
v5 = a3;
v6 = a1;
v7 = (18351 * (unsigned int)(unsigned __int16)a1) >> 16;
v8 = a1 - v7;
LOWORD(v8) = (unsigned __int16)(a1 - v7) >> 1;
v9 = v7 + v8;
LOWORD(v9) = (unsigned __int16)v9 >> 4;
v10 = a1 - 25 * v9;
v11 = calledBefore_3 == 0;
v12 = v10;
*((_WORD *)&avgPDR + (unsigned __int16)v10) = a2;
*((_WORD *)&avgRssi + (unsigned __int16)v10) = v5;
*((_WORD *)&avgLQI + (unsigned __int16)v10) = a4;
if ( v11 || prevPrevTestNumber_2 > (int)(unsigned __int16)a1 )
{
calledBefore_3 = 1;
prevPrevTestNumber_2 = a1;
*((_WORD *)&avgPDR + 24) = -1;
*((_WORD *)&avgRssi + 24) = -1;
*((_WORD *)&avgLQI + 24) = -1;
avgPDR = -1LL;
*(&avgPDR + 1) = -1LL;
*(&avgPDR + 2) = -1LL;
avgRssi = -1LL;
*(&avgRssi + 1) = -1LL;
*(&avgRssi + 2) = -1LL;
avgLQI = -1LL;
*(&avgLQI + 1) = -1LL;
*(&avgLQI + 2) = -1LL;
}
else if ( prevPrevTestNumber_2 < (int)(unsigned __int16)a1 )
{
v23 = prevPrevTestNumber_2 + 1;
v24 = v23;
++prevPrevTestNumber_2;
if ( (unsigned __int16)a1 > v23 )
{
do
{
v25 = ((__int16)((unsigned int)(5243 * v24) >> 16) >> 1) - (v23 >> 15);
v26 = v23++;
v27 = (__int16)(v26 - 25 * v25);
*((_WORD *)&avgPDR + v27) = 0;
*((_WORD *)&avgRssi + v27) = 0;
*((_WORD *)&avgLQI + v27) = 0;
v24 = v23;
}
while ( v4 > v23 );
prevPrevTestNumber_2 = v23;
}
}
v13 = 10;
v14 = 0;
do
{
v15 = v14 + *((unsigned __int16 *)&avgPDR + (__int16)v10);
v11 = (_WORD)v10-- == 0;
if ( v11 )
v10 = 24;
v14 = v15;
--v13;
}
while ( v13 );
v16 = 0;
v17 = 0;
v18 = 25;
v19 = 0;
do
{
v20 = *((_WORD *)&avgPDR + v12);
if ( v20 )
{
v19 += *((_WORD *)&avgRssi + v12);
++v17;
v13 += v20;
v16 += *((_WORD *)&avgLQI + v12);
if ( v17 > 9u )
{
LODWORD(v21) = ((unsigned int)(unsigned __int16)v15 + 5) / 0xA;
sprintf(
strBuf,
"avg (%d runs): %d\n",
10LL,
(3435973837u * ((unsigned __int64)(unsigned __int16)v15 + 5)) >> 35);
serialPrintV(strBuf);
goto LABEL_21;
}
}
v11 = v12-- == 0;
if ( v11 )
v12 = 24;
--v18;
}
while ( v18 );
v21 = (3435973837u * ((unsigned __int64)(unsigned __int16)v15 + 5)) >> 35;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, ((unsigned int)(unsigned __int16)v15 + 5) / 0xA);
result = serialPrintV(strBuf);
if ( !v17 )
return result;
LABEL_21:
v28 = v17 >> 1;
v29 = (v28 + v13) / v17;
v31 = (v28 + v19) >> 31;
v30 = v28 + v19;
v32 = v30 / v17;
v33 = (v28 + v16) / v17;
v34 = platformFixRssi((unsigned __int16)(__SPAIR64__(v31, v30) / v17));
sprintf(strBuf, "nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n", v35, v36, (unsigned int)v34, (unsigned __int16)v33);
serialPrintV(strBuf);
v37[2] = v6;
v37[1] = testId;
v37[4] = v21;
v37[0] = localAddress;
v37[5] = v17;
v37[6] = v29;
v37[7] = v32;
v37[8] = v33;
v37[3] = 10;
return crc16(v37, 18LL);
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int16 a2, unsigned __int8 a3, unsigned __int8 a4)
{
int v4; // er8
__int16 v5; // r9
__int16 v6; // bp
unsigned int v7; // edx
int v8; // eax
int v9; // eax
int v10; // eax
bool v11; // zf
__int16 v12; // dx
unsigned __int16 v13; // bx
int v14; // ecx
int v15; // edi
unsigned __int16 v16; // r14
unsigned __int16 v17; // r12
__int16 v18; // ax
__int16 v19; // r13
__int16 v20; // si
unsigned __int64 v21; // r15
__int64 result; // rax
__int16 v23; // si
int v24; // ecx
__int16 v25; // cx
__int16 v26; // di
__int64 v27; // rcx
int v28; // ecx
int v29; // ebx
int v30; // eax
int v31; // edx
int v32; // er13
int v33; // er14
__int16 v34; // ax
__int64 v35; // rdx
__int64 v36; // rcx
__int16 v37[44]; // [rsp+0h] [rbp-58h] BYREF

v4 = (unsigned __int16)a1;
v5 = a3;
v6 = a1;
v7 = (18351 * (unsigned int)(unsigned __int16)a1) >> 16;
v8 = a1 - v7;
LOWORD(v8) = (unsigned __int16)(a1 - v7) >> 1;
v9 = v7 + v8;
LOWORD(v9) = (unsigned __int16)v9 >> 4;
v10 = a1 - 25 * v9;
v11 = calledBefore_3 == 0;
v12 = v10;
*((_WORD *)&avgPDR + (unsigned __int16)v10) = a2;
*((_WORD *)&avgRssi + (unsigned __int16)v10) = v5;
*((_WORD *)&avgLQI + (unsigned __int16)v10) = a4;
if ( v11 || prevPrevTestNumber_2 > (int)(unsigned __int16)a1 )
{
calledBefore_3 = 1;
prevPrevTestNumber_2 = a1;
*((_WORD *)&avgPDR + 24) = -1;
*((_WORD *)&avgRssi + 24) = -1;
*((_WORD *)&avgLQI + 24) = -1;
avgPDR = -1LL;
*(&avgPDR + 1) = -1LL;
*(&avgPDR + 2) = -1LL;
avgRssi = -1LL;
*(&avgRssi + 1) = -1LL;
*(&avgRssi + 2) = -1LL;
avgLQI = -1LL;
*(&avgLQI + 1) = -1LL;
*(&avgLQI + 2) = -1LL;
}
else if ( prevPrevTestNumber_2 < (int)(unsigned __int16)a1 )
{
v23 = prevPrevTestNumber_2 + 1;
v24 = v23;
++prevPrevTestNumber_2;
if ( (unsigned __int16)a1 > v23 )
{
do
{
v25 = ((__int16)((unsigned int)(5243 * v24) >> 16) >> 1) - (v23 >> 15);
v26 = v23++;
v27 = (__int16)(v26 - 25 * v25);
*((_WORD *)&avgPDR + v27) = 0;
*((_WORD *)&avgRssi + v27) = 0;
*((_WORD *)&avgLQI + v27) = 0;
v24 = v23;
}
while ( v4 > v23 );
prevPrevTestNumber_2 = v23;
}
}
v13 = 10;
v14 = 0;
do
{
v15 = v14 + *((unsigned __int16 *)&avgPDR + (__int16)v10);
v11 = (_WORD)v10-- == 0;
if ( v11 )
v10 = 24;
v14 = v15;
--v13;
}
while ( v13 );
v16 = 0;
v17 = 0;
v18 = 25;
v19 = 0;
do
{
v20 = *((_WORD *)&avgPDR + v12);
if ( v20 )
{
v19 += *((_WORD *)&avgRssi + v12);
++v17;
v13 += v20;
v16 += *((_WORD *)&avgLQI + v12);
if ( v17 > 9u )
{
LODWORD(v21) = ((unsigned int)(unsigned __int16)v15 + 5) / 0xA;
sprintf(
strBuf,
"avg (%d runs): %d\n",
10LL,
(3435973837u * ((unsigned __int64)(unsigned __int16)v15 + 5)) >> 35);
func2(strBuf);
goto LABEL_21;
}
}
v11 = v12-- == 0;
if ( v11 )
v12 = 24;
--v18;
}
while ( v18 );
v21 = (3435973837u * ((unsigned __int64)(unsigned __int16)v15 + 5)) >> 35;
sprintf(strBuf, "avg (%d runs): %d\n", 10LL, ((unsigned int)(unsigned __int16)v15 + 5) / 0xA);
result = func2(strBuf);
if ( !v17 )
return result;
LABEL_21:
v28 = v17 >> 1;
v29 = (v28 + v13) / v17;
v31 = (v28 + v19) >> 31;
v30 = v28 + v19;
v32 = v30 / v17;
v33 = (v28 + v16) / v17;
v34 = func1((unsigned __int16)(__SPAIR64__(v31, v30) / v17));
sprintf(strBuf, "nonempty avg (%d runs): %d, rssi: %d, lqi: %d\n", v35, v36, (unsigned int)v34, (unsigned __int16)v33);
func2(strBuf);
v37[2] = v6;
v37[1] = testId;
v37[4] = v21;
v37[0] = localAddress;
v37[5] = v17;
v37[6] = v29;
v37[7] = v32;
v37[8] = v33;
v37[3] = 10;
return crc16(v37, 18LL);
}

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

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

variable_types:
prevTestNumber
unsigned short

variable_types:
prevTestPacketsRx
unsigned short

variable_types:
rssi
unsigned char

variable_types:
lqi
unsigned char

variable_types: i short

variable_types: prevPrevTestNumber short

variable_types: idx short

variable_types: startIdx short

variable_types:
numSamples
unsigned short

variable_types:
sumSamples
unsigned short

variable_types: sumRssi short

variable_types:
sumLqi
unsigned short

variable_types:
numSamplesNe
unsigned short

variable_types:
sumSamplesNe
unsigned short

variable_types: sumRssiNe short

variable_types:
sumLqiNe
unsigned short

variable_types:
samplesAvg
unsigned short

variable_types:
samplesAvgNe
unsigned short

variable_types: rssiAvg short

variable_types:
lqiAvg
unsigned short

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


endTest

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

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

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

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


startTest

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

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

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

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

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

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

variable_types:
newTestNumber
unsigned short