decompiled_code:
int __fastcall print_pixels(_DWORD *a1, int *a2, char a3)
{
int v4; // eax
int v5; // er13
unsigned __int8 v7; // r13
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al

v4 = *a2;
if ( *a1 )
{
v5 = a3 & 1;
if ( !v4 )
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( v5 )
{
v10 = rgb2xterm(a1);
printf("\x1B[38;5;%dm", v10);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a1[1], (unsigned int)a1[2], (unsigned int)a1[3]);
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
if ( (a3 & 1) != 0 )
{
v7 = rgb2xterm(a1);
v8 = rgb2xterm(a2);
printf("\x1B[38;5;%dm\x1B[48;5;%dm", v7, v8);
if ( (a3 & 2) == 0 )
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
else if ( (a3 & 2) == 0 )
{
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
printf(
"\x1B[38;2;%d;%d;%dm\x1B[48;2;%d;%d;%dm",
(unsigned int)a1[1],
(unsigned int)a1[2],
(unsigned int)a1[3],
(unsigned int)a2[1],
(unsigned int)a2[2],
(unsigned int)a2[3]);
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
if ( !v4 )
return printf("\x1B[0m ");
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v9 = rgb2xterm(a2);
printf("\x1B[38;5;%dm", v9);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a2[1], (unsigned int)a2[2], (unsigned int)a2[3]);
return fwrite(&unk_4025DF, 1uLL, 3uLL, stdout);
}

canonical_decompiled_code:
int __fastcall func11(_DWORD *a1, int *a2, char a3)
{
int v4; // eax
int v5; // er13
unsigned __int8 v7; // r13
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al

v4 = *a2;
if ( *a1 )
{
v5 = a3 & 1;
if ( !v4 )
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( v5 )
{
v10 = func7(a1);
printf("\x1B[38;5;%dm", v10);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a1[1], (unsigned int)a1[2], (unsigned int)a1[3]);
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
if ( (a3 & 1) != 0 )
{
v7 = func7(a1);
v8 = func7(a2);
printf("\x1B[38;5;%dm\x1B[48;5;%dm", v7, v8);
if ( (a3 & 2) == 0 )
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
else if ( (a3 & 2) == 0 )
{
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
printf(
"\x1B[38;2;%d;%d;%dm\x1B[48;2;%d;%d;%dm",
(unsigned int)a1[1],
(unsigned int)a1[2],
(unsigned int)a1[3],
(unsigned int)a2[1],
(unsigned int)a2[2],
(unsigned int)a2[3]);
return fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
if ( !v4 )
return printf("\x1B[0m ");
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v9 = func7(a2);
printf("\x1B[38;5;%dm", v9);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a2[1], (unsigned int)a2[2], (unsigned int)a2[3]);
return fwrite(&unk_4025DF, 1uLL, 3uLL, stdout);
}

original_code:
void print_pixels(Imlib_Color* p1, Imlib_Color* p2, int mode) {
 static char* upper = "▀";
 static char* lower = "▄";
 if (p1->alpha == 0 && p2->alpha == 0) {
  printf("\x1b[0m ");
 } else if (p1->alpha == 0 && p2->alpha != 0) {
  fputs("\x1b[0m", stdout);
  if(mode & MODE_INDEXED) {
   uint8_t col2 = rgb2xterm(p2);
   printf("\x1b[38;5;%dm", col2);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm", p2->red, p2->green, p2->blue);
  }
  fputs(upper, stdout);
 } else if (p1->alpha != 0 && p2->alpha == 0) {
  fputs("\x1b[0m", stdout);
  if(mode & MODE_INDEXED) {
   uint8_t col1 = rgb2xterm(p1);
   printf("\x1b[38;5;%dm", col1);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm", p1->red, p1->green, p1->blue);
  }
  fputs(lower, stdout);
 } else {
  if(mode & MODE_INDEXED) {
   uint8_t col1 = rgb2xterm(p1);
   uint8_t col2 = rgb2xterm(p2);
   printf("\x1b[38;5;%dm\x1b[48;5;%dm", col1, col2);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm", p1->red, p1->green, p1->blue, p2->red, p2->green, p2->blue);
  }
  fputs(lower, stdout);
 }
}

canonical_original_code:
void print_pixels(struct _imlib_color *p1, struct _imlib_color *p2, int mode) {
 static char *upper = "▀";
 static char *lower = "▄";
 if (p1->alpha == 0 && p2->alpha == 0) {
  printf("\x1b[0m ");
 } else if (p1->alpha == 0 && p2->alpha != 0) {
  fputs("\x1b[0m", stdout);
  if(mode & MODE_INDEXED) {
   unsigned char col2 = rgb2xterm(p2);
   printf("\x1b[38;5;%dm", col2);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm", p2->red, p2->green, p2->blue);
  }
  fputs(upper, stdout);
 } else if (p1->alpha != 0 && p2->alpha == 0) {
  fputs("\x1b[0m", stdout);
  if(mode & MODE_INDEXED) {
   unsigned char col1 = rgb2xterm(p1);
   printf("\x1b[38;5;%dm", col1);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm", p1->red, p1->green, p1->blue);
  }
  fputs(lower, stdout);
 } else {
  if(mode & MODE_INDEXED) {
   unsigned char col1 = rgb2xterm(p1);
   unsigned char col2 = rgb2xterm(p2);
   printf("\x1b[38;5;%dm\x1b[48;5;%dm", col1, col2);
  }
  if(mode & MODE_24_BIT) {
   printf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm", p1->red, p1->green, p1->blue, p2->red, p2->green, p2->blue);
  }
  fputs(lower, stdout);
 }
}

variable_types: mode int

variable_types:
upper
char *

variable_types:
lower
char *


repo: atiselsts/osw

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

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

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

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

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

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

variable_types:
acc
unsigned short

variable_types:
byte
unsigned char

return_type: unsigned short


flashAddr

decompiled_code:
__int64 __fastcall flashAddr(unsigned __int16 a1, int a2)
{
return (a1 << 14) + a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1, int a2)
{
return (a1 << 14) + a2;
}

original_code:
static inline uint32_t flashAddr(blk_t block, fsOff_t offset)
{
    return (uint32_t)block * (0x10000 / 4) + offset;
}

canonical_original_code:
static inline unsigned int flashAddr(unsigned short block, unsigned int offset)
{
    return (unsigned int)block * (0x10000 / 4) + offset;
}

variable_types:
block
unsigned short

variable_types:
offset
unsigned int

return_type: unsigned int


chunkAddr

decompiled_code:
__int64 __fastcall chunkAddr(unsigned __int16 a1, unsigned int a2)
{
return flashAddr(a1, 255 * (a2 % 0x3F40 / 0xFD) + a2 % 0xFD);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1, unsigned int a2)
{
return func1(a1, 255 * (a2 % 0x3F40 / 0xFD) + a2 % 0xFD);
}

original_code:
static inline uint32_t chunkAddr(blk_t block, fsOff_t offset)
{
    return flashAddr(block, (offset % ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t)))) / (255 - sizeof(uint16_t))
                            * 255 + offset % (255 - sizeof(uint16_t)));
}

canonical_original_code:
static inline unsigned int chunkAddr(unsigned short block, unsigned int offset)
{
    return flashAddr(block, (offset % ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short)))) / (255 - sizeof(unsigned short))
                            * 255 + offset % (255 - sizeof(unsigned short)));
}


serviceAddr

decompiled_code:
__int64 __fastcall serviceAddr(unsigned __int16 a1, unsigned int a2)
{
return flashAddr(a1, 255 * (a2 % 0x3F40 / 0xFD) + 253);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, unsigned int a2)
{
return func1(a1, 255 * (a2 % 0x3F40 / 0xFD) + 253);
}

original_code:
static inline uint32_t serviceAddr(blk_t block, fsOff_t offset)
{
    return flashAddr(block, (offset % ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t)))) / (255 - sizeof(uint16_t))
                            * 255 + (255 - sizeof(uint16_t)));
}

canonical_original_code:
static inline unsigned int serviceAddr(unsigned short block, unsigned int offset)
{
    return flashAddr(block, (offset % ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short)))) / (255 - sizeof(unsigned short))
                            * 255 + (255 - sizeof(unsigned short)));
}


freeHandle

decompiled_code:
void __fastcall freeHandle(_QWORD *a1)
{
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func4(_QWORD *a1)
{
*a1 = 0LL;
}

original_code:
static void freeHandle(struct fsBlockHandle *handle)
{
    mutexLock(&fsBlkHandleMutex);
    handle->fcb = ((void *)0);
    mutexUnlock(&fsBlkHandleMutex);
}

variable_types: struct fsBlockHandle { struct fsFileControlBlock { struct Mutex_s {  } mutex; char id; unsigned char refcount; unsigned short first; unsigned int size; unsigned short crc; _Bool openForWrite; } *fcb; enum fsMode_t { FS_READ=expr, FS_APPEND=expr, FS_RDWR=expr, FS_NOCACHE=expr, FS_CHECKSUM=expr } mode; unsigned int pos; unsigned short curr; char buf[-1]; unsigned int readEnd; } *

return_type: void

user_defined_types: struct fsBlockHandle { struct fsFileControlBlock *fcb; enum fsMode_t mode; unsigned int pos; unsigned short curr; char buf[-1]; unsigned int readEnd; }

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


decompiled_code:
__int64 __fastcall crc16Acc(unsigned __int8 *a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
unsigned __int8 *v5; // rcx

result = a3;
if ( a2 )
{
do
result = crc16Add((unsigned __int16)result, *a1);
while ( v5 != a1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 *a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
unsigned __int8 *v5; // rcx

result = a3;
if ( a2 )
{
do
result = func0((unsigned __int16)result, *a1);
while ( v5 != a1 );
}
return result;
}

original_code:
static uint16_t crc16Acc(const void *data, size_t len, uint16_t acc)
{
    const uint8_t *d = data;
    size_t i;
    for (i = 0; i < len; i++)
        acc = crc16Add(acc, d[i]);
    return acc;
}

canonical_original_code:
static unsigned short crc16Acc(void *data, unsigned long len, unsigned short acc)
{
    unsigned char *d = data;
    unsigned long i;
    for (i = 0; i < len; i++)
        acc = crc16Add(acc, d[i]);
    return acc;
}

variable_types:
data
void *

variable_types:
len
unsigned long

variable_types:
d
unsigned char *

variable_types:
i
unsigned long


blkTell

decompiled_code:
__int64 __fastcall blkTell(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
static fsOff_t blkTell(void *id)
{
    struct fsBlockHandle *handle = id;
    return handle->pos;
}

canonical_original_code:
static unsigned int blkTell(void *id)
{
    struct fsBlockHandle *handle = id;
    return handle->pos;
}

variable_types:
id
void *

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


enableFlash

decompiled_code:
// attributes: thunk
__int64 enableFlash()
{
return extFlashWake();
}

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

original_code:
static inline void enableFlash(void)
{
    extFlashWake();
}

canonical_original_code:
static inline void enableFlash()
{
    extFlashWake();
}


disableFlash

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

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

original_code:
static inline void disableFlash(void)
{
    extFlashSleep();
}

canonical_original_code:
static inline void disableFlash()
{
    extFlashSleep();
}


blkExtFlashWrite

decompiled_code:
__int64 __fastcall blkExtFlashWrite(unsigned int a1, __int64 a2, unsigned __int16 a3)
{
enableFlash();
extFlashWrite(a1, a2, a3);
return disableFlash();
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, __int64 a2, unsigned __int16 a3)
{
func7();
extFlashWrite(a1, a2, a3);
return func8();
}

original_code:
static inline void blkExtFlashWrite(uint32_t addr, const void *buf, size_t len)
{
    enableFlash();
    extFlashWrite(addr, buf, len);
    disableFlash();
}

canonical_original_code:
static inline void blkExtFlashWrite(unsigned int addr, void *buf, unsigned long len)
{
    enableFlash();
    extFlashWrite(addr, buf, len);
    disableFlash();
}

variable_types:
addr
unsigned int

variable_types:
buf
void *


doWrite

decompiled_code:
__int64 __fastcall doWrite(__int64 a1, unsigned int a2, __int64 a3, unsigned __int16 a4)
{
unsigned int v6; // eax

if ( __ROR4__(-424403883 * a2, 6) <= 0x40C24u )
fsBlockSyncEntry(*(_QWORD *)a1);
v6 = chunkAddr(*(_WORD *)(a1 + 16), a2);
return blkExtFlashWrite(v6, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, unsigned int a2, __int64 a3, unsigned __int16 a4)
{
unsigned int v6; // eax

if ( __ROR4__(-424403883 * a2, 6) <= 0x40C24u )
fsBlockSyncEntry(*(_QWORD *)a1);
v6 = func2(*(_WORD *)(a1 + 16), a2);
return func9(v6, a3, a4);
}

original_code:
static void doWrite(struct fsBlockHandle * __restrict__ handle, fsOff_t start,
                    const void * __restrict__ buf, size_t len)
{
    if (start % ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t))) == 0)
    {
        fsBlockSyncEntry(handle->fcb);
    }
    blkExtFlashWrite(chunkAddr(handle->curr, start), buf, len);
}

canonical_original_code:
static void doWrite(struct fsBlockHandle *handle, unsigned int start, void *buf, unsigned long len)
{
    if (start % ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short))) == 0)
    {
        fsBlockSyncEntry(handle->fcb);
    }
    blkExtFlashWrite(chunkAddr(handle->curr, start), buf, len);
}

variable_types:
start
unsigned int


doFlush

decompiled_code:
__int64 __fastcall doFlush(__int64 a1)
{
__int64 result; // rax
int v2; // ecx
unsigned int v3; // esi
__int64 v4; // r9
int v5; // er10
__int16 v6; // ax
unsigned int v7; // er11
unsigned int v8; // eax
__int64 v9; // r9
__int64 v10; // r9
int v11; // er10

result = *(_QWORD *)a1;
v2 = *(_DWORD *)(a1 + 12);
v3 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
if ( v3 != v2 )
{
if ( v3 / 0xFD == (v2 - 1) / 0xFDu )
{
result = doWrite(a1, v3, a1 + 18 + v3 % 0xFD, (unsigned __int16)v2 - (unsigned __int16)v3);
v4 = *(_QWORD *)a1;
v5 = *(_DWORD *)(a1 + 12);
if ( (*(_BYTE *)(a1 + 8) & 0x10) != 0
&& (v6 = crc16Acc(
(unsigned __int8 *)(a1 + 18 + *(_DWORD *)(v4 + 4) % 0xFDu),
(unsigned int)(v5 - *(_DWORD *)(v4 + 4)),
*(unsigned __int16 *)(v4 + 8)),
*(_WORD *)(v4 + 8) = v6,
result = (unsigned int)(-424403883 * v5),
(unsigned int)result <= 0x103091B) )
{
v8 = serviceAddr(*(_WORD *)(a1 + 16), v7);
blkExtFlashWrite(v8, v9 + 8, 2u);
v10 = *(_QWORD *)a1;
v11 = *(_DWORD *)(a1 + 12);
result = 0LL;
*(_WORD *)(v10 + 8) = 0;
*(_DWORD *)(v10 + 4) = v11;
}
else
{
*(_DWORD *)(v4 + 4) = v5;
}
}
else
{
assertionFailed(
"handle->fcb->size / CHUNK_DATA_SIZE == (handle->pos - 1) / CHUNK_DATA_SIZE",
"../../../src/fs/block/block.c",
331LL);
result = blkClose();
}
}
return result;
}

original_code:
static void doFlush(struct fsBlockHandle *handle)
{
    if (handle->fcb->size == handle->pos)
        return;
    ((handle->fcb->size / (255 - sizeof(uint16_t)) == (handle->pos - 1) / (255 - sizeof(uint16_t))) ? (void)0 : assertionFailed("handle->fcb->size / CHUNK_DATA_SIZE == (handle->pos - 1) / CHUNK_DATA_SIZE", "../../../src/fs/block/block.c", 331));
    doWrite(handle, handle->fcb->size,
            handle->buf + handle->fcb->size % (255 - sizeof(uint16_t)),
            handle->pos - handle->fcb->size);
    if (handle->mode & FS_CHECKSUM)
    {
        handle->fcb->crc =
            crc16Acc(handle->buf + handle->fcb->size % (255 - sizeof(uint16_t)),
                     handle->pos - handle->fcb->size, handle->fcb->crc);
        if (handle->pos % (255 - sizeof(uint16_t)) == 0)
        {
            blkExtFlashWrite(serviceAddr(handle->curr, handle->fcb->size),
                             &handle->fcb->crc, sizeof(handle->fcb->crc));
            handle->fcb->crc = 0;
        }
    }
    handle->fcb->size = handle->pos;
}

canonical_original_code:
static void doFlush(struct fsBlockHandle *handle)
{
    if (handle->fcb->size == handle->pos)
        return;
    ((handle->fcb->size / (255 - sizeof(unsigned short)) == (handle->pos - 1) / (255 - sizeof(unsigned short))) ? (void)0 : assertionFailed("handle->fcb->size / CHUNK_DATA_SIZE == (handle->pos - 1) / CHUNK_DATA_SIZE", "../../../src/fs/block/block.c", 331));
    doWrite(handle, handle->fcb->size,
            handle->buf + handle->fcb->size % (255 - sizeof(unsigned short)),
            handle->pos - handle->fcb->size);
    if (handle->mode & FS_CHECKSUM)
    {
        handle->fcb->crc =
            crc16Acc(handle->buf + handle->fcb->size % (255 - sizeof(unsigned short)),
                     handle->pos - handle->fcb->size, handle->fcb->crc);
        if (handle->pos % (255 - sizeof(unsigned short)) == 0)
        {
            blkExtFlashWrite(serviceAddr(handle->curr, handle->fcb->size),
                             &handle->fcb->crc, sizeof(handle->fcb->crc));
            handle->fcb->crc = 0;
        }
    }
    handle->fcb->size = handle->pos;
}


blkClose

decompiled_code:
void __fastcall blkClose(_DWORD *a1)
{
int v1; // eax
__int64 v3; // rdi

v1 = a1[2];
if ( (v1 & 2) != 0 )
{
if ( (v1 & 8) == 0 )
doFlush((__int64)a1);
v3 = *(_QWORD *)a1;
*(_BYTE *)(*(_QWORD *)a1 + 10LL) = 0;
}
else
{
v3 = *(_QWORD *)a1;
}
fsBlockCloseFile(v3);
freeHandle(a1);
}

original_code:
static _Bool blkClose(void *id)
{
    struct fsBlockHandle *handle = id;
    if (handle->mode & FS_APPEND)
    {
        mutexLock(&handle->fcb->mutex);
        if (!(handle->mode & FS_NOCACHE))
            doFlush(handle);
        handle->fcb->openForWrite = 0;
        mutexUnlock(&handle->fcb->mutex);
    }
    fsBlockCloseFile(handle->fcb);
    freeHandle(handle);
    return 1;
}


blkFlush

decompiled_code:
__int64 __fastcall blkFlush(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 8);
if ( (v1 & 2) != 0 )
{
if ( (v1 & 8) == 0 )
doFlush(a1);
fsBlockSyncEntry(*(_QWORD *)a1);
result = 1LL;
}
else
{
assertionFailed("handle->mode & FS_APPEND", "../../../src/fs/block/block.c", 425LL);
result = blkWrite();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = *(_DWORD *)(a1 + 8);
if ( (v1 & 2) != 0 )
{
if ( (v1 & 8) == 0 )
func11(a1);
fsBlockSyncEntry(*(_QWORD *)a1);
result = 1LL;
}
else
{
assertionFailed("handle->mode & FS_APPEND", "../../../src/fs/block/block.c", 425LL);
result = func14();
}
return result;
}

original_code:
static _Bool blkFlush(void *id)
{
    struct fsBlockHandle *handle = id;
    ((handle->mode & FS_APPEND) ? (void)0 : assertionFailed("handle->mode & FS_APPEND", "../../../src/fs/block/block.c", 425));
    mutexLock(&handle->fcb->mutex);
    if (!(handle->mode & FS_NOCACHE))
        doFlush(handle);
    fsBlockSyncEntry(handle->fcb);
    mutexUnlock(&handle->fcb->mutex);
    return 1;
}


blkWrite

original_code:
static ssize_t blkWrite(void * __restrict__ id, const void * __restrict__ buf,
                        size_t count)
{
    struct fsBlockHandle * __restrict__ handle = id;
    ssize_t res = 0;
    ((handle->mode & FS_APPEND) ? (void)0 : assertionFailed("handle->mode & FS_APPEND", "../../../src/fs/block/block.c", 358));
    if (count == 0)
        return 0;
    mutexLock(&handle->fcb->mutex);
    count = ((count) < (((fsOff_t)-1 - (fsOff_t)-1 % (255 - sizeof(uint16_t))) - handle->pos) ? (count) : (((fsOff_t)-1 - (fsOff_t)-1 % (255 - sizeof(uint16_t))) - handle->pos));
    if (count == 0)
    {
        fsSetError(FS_ERR_NOSPC);
        res = -1;
    }
    else
    {
        if (handle->pos % ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t))) == 0)
        {
            blk_t new = fsBlockAllocate(handle->curr);
            if (new == ((blk_t)-1))
                res = -1;
            else
            {
                handle->curr = new;
                if (handle->fcb->first == ((blk_t)-1))
                    handle->fcb->first = handle->curr;
            }
        }
        if (res != -1)
        {
            res = count = ((count) < ((255 - sizeof(uint16_t)) - handle->pos % (255 - sizeof(uint16_t))) ? (count) : ((255 - sizeof(uint16_t)) - handle->pos % (255 - sizeof(uint16_t))));
            if (!(handle->mode & FS_NOCACHE))
            {
                memcpy(handle->buf + handle->pos % (255 - sizeof(uint16_t)), buf, count);
                handle->pos += count;
                if (handle->pos % (255 - sizeof(uint16_t)) == 0)
                    doFlush(handle);
            }
            else
            {
                doWrite(handle, handle->pos, buf, count);
                handle->pos += count;
                handle->fcb->size = handle->pos;
            }
        }
    }
    mutexUnlock(&handle->fcb->mutex);
    return res;
}

canonical_original_code:
static long blkWrite(void *id, void *buf, unsigned long count)
{
    struct fsBlockHandle *handle = id;
    long res = 0;
    ((handle->mode & FS_APPEND) ? (void)0 : assertionFailed("handle->mode & FS_APPEND", "../../../src/fs/block/block.c", 358));
    if (count == 0)
        return 0;
    mutexLock(&handle->fcb->mutex);
    count = ((count) < (((fsOff_t)-1 - (fsOff_t)-1 % (255 - sizeof(unsigned short))) - handle->pos) ? (count) : (((fsOff_t)-1 - (fsOff_t)-1 % (255 - sizeof(unsigned short))) - handle->pos));
    if (count == 0)
    {
        fsSetError(FS_ERR_NOSPC);
        res = -1;
    }
    else
    {
        if (handle->pos % ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short))) == 0)
        {
            unsigned short new = fsBlockAllocate(handle->curr);
            if (new == ((blk_t)-1))
                res = -1;
            else
            {
                handle->curr = new;
                if (handle->fcb->first == ((blk_t)-1))
                    handle->fcb->first = handle->curr;
            }
        }
        if (res != -1)
        {
            res = count = ((count) < ((255 - sizeof(unsigned short)) - handle->pos % (255 - sizeof(unsigned short))) ? (count) : ((255 - sizeof(unsigned short)) - handle->pos % (255 - sizeof(unsigned short))));
            if (!(handle->mode & FS_NOCACHE))
            {
                memcpy(handle->buf + handle->pos % (255 - sizeof(unsigned short)), buf, count);
                handle->pos += count;
                if (handle->pos % (255 - sizeof(unsigned short)) == 0)
                    doFlush(handle);
            }
            else
            {
                doWrite(handle, handle->pos, buf, count);
                handle->pos += count;
                handle->fcb->size = handle->pos;
            }
        }
    }
    mutexUnlock(&handle->fcb->mutex);
    return res;
}

variable_types:
count
unsigned long

variable_types: res long

variable_types:
new
unsigned short

return_type: long


seekBlock

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

result = *(unsigned __int16 *)(*(_QWORD *)a1 + 2LL);
a1[8] = result;
if ( a2 > 0x3F40 )
{
for ( i = a2; i > 0x3F40; i -= 16192 )
{
result = fsBlockGetNext((unsigned __int16)result);
a1[8] = result;
}
}
return result;
}

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

result = *(unsigned __int16 *)(*(_QWORD *)a1 + 2LL);
a1[8] = result;
if ( a2 > 0x3F40 )
{
for ( i = a2; i > 0x3F40; i -= 16192 )
{
result = fsBlockGetNext((unsigned __int16)result);
a1[8] = result;
}
}
return result;
}

original_code:
static void seekBlock(struct fsBlockHandle *handle, fsOff_t pos)
{
    handle->curr = handle->fcb->first;
    while (pos > ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t))))
    {
        handle->curr = fsBlockGetNext(handle->curr);
        pos -= ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t)));
    }
}

canonical_original_code:
static void seekBlock(struct fsBlockHandle *handle, unsigned int pos)
{
    handle->curr = handle->fcb->first;
    while (pos > ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short))))
    {
        handle->curr = fsBlockGetNext(handle->curr);
        pos -= ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short)));
    }
}

variable_types:
pos
unsigned int


blkExtFlashRead

decompiled_code:
__int64 __fastcall blkExtFlashRead(unsigned int a1, __int64 a2, unsigned __int16 a3)
{
enableFlash();
extFlashRead(a1, a2, a3);
return disableFlash();
}

canonical_decompiled_code:
__int64 __fastcall func17(unsigned int a1, __int64 a2, unsigned __int16 a3)
{
func7();
extFlashRead(a1, a2, a3);
return func8();
}

original_code:
static inline void blkExtFlashRead(uint32_t addr, void *buf, size_t len)
{
    enableFlash();
    extFlashRead(addr, buf, len);
    disableFlash();
}

canonical_original_code:
static inline void blkExtFlashRead(unsigned int addr, void *buf, unsigned long len)
{
    enableFlash();
    extFlashRead(addr, buf, len);
    disableFlash();
}


doRead

decompiled_code:
__int64 __fastcall doRead(__int64 a1, unsigned int a2, __int64 a3, unsigned __int16 a4)
{
unsigned __int16 v6; // ax
unsigned int v7; // eax

v6 = *(_WORD *)(a1 + 16);
if ( a2 && __ROR4__(-424403883 * a2, 6) <= 0x40C24u )
{
v6 = fsBlockGetNext(v6);
*(_WORD *)(a1 + 16) = v6;
}
v7 = chunkAddr(v6, a2);
return blkExtFlashRead(v7, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, unsigned int a2, __int64 a3, unsigned __int16 a4)
{
unsigned __int16 v6; // ax
unsigned int v7; // eax

v6 = *(_WORD *)(a1 + 16);
if ( a2 && __ROR4__(-424403883 * a2, 6) <= 0x40C24u )
{
v6 = fsBlockGetNext(v6);
*(_WORD *)(a1 + 16) = v6;
}
v7 = func2(v6, a2);
return func17(v7, a3, a4);
}

original_code:
static void doRead(struct fsBlockHandle * __restrict__ handle, fsOff_t start,
                   void * __restrict__ buf, size_t len)
{
    if (start != 0 && start % ((0x10000 / 4) / 255 * (255 - sizeof(uint16_t))) == 0)
        handle->curr = fsBlockGetNext(handle->curr);
    blkExtFlashRead(chunkAddr(handle->curr, start), buf, len);
}

canonical_original_code:
static void doRead(struct fsBlockHandle *handle, unsigned int start, void *buf, unsigned long len)
{
    if (start != 0 && start % ((0x10000 / 4) / 255 * (255 - sizeof(unsigned short))) == 0)
        handle->curr = fsBlockGetNext(handle->curr);
    blkExtFlashRead(chunkAddr(handle->curr, start), buf, len);
}


fillBuffer

original_code:
static _Bool fillBuffer(struct fsBlockHandle *handle)
{
    blk_t backup = handle->curr;
    fsOff_t end = handle->readEnd - handle->readEnd % (255 - sizeof(uint16_t))
                  + (255 - sizeof(uint16_t));
    if (end > handle->fcb->size)
        end = handle->fcb->size;
    doRead(handle, handle->readEnd,
           handle->buf + handle->readEnd % (255 - sizeof(uint16_t)),
           end - handle->readEnd);
    if (handle->mode & FS_CHECKSUM)
    {
        uint16_t crc;
        if (handle->fcb->size / (255 - sizeof(uint16_t))
            == handle->readEnd / (255 - sizeof(uint16_t)))
        {
            crc = handle->fcb->crc;
        }
        else
        {
            blkExtFlashRead(serviceAddr(handle->curr, handle->readEnd),
                            &crc, sizeof(crc));
        }
        if (crc16Acc(handle->buf, end - handle->readEnd, 0) != crc)
        {
            fsSetError(FS_ERR_IO);
            handle->curr = backup;
            return 0;
        }
    }
    handle->readEnd = end;
    return 1;
}

canonical_original_code:
static _Bool fillBuffer(struct fsBlockHandle *handle)
{
    unsigned short backup = handle->curr;
    unsigned int end = handle->readEnd - handle->readEnd % (255 - sizeof(uint16_t))
                  + (255 - sizeof(uint16_t));
    if (end > handle->fcb->size)
        end = handle->fcb->size;
    doRead(handle, handle->readEnd,
           handle->buf + handle->readEnd % (255 - sizeof(unsigned short)),
           end - handle->readEnd);
    if (handle->mode & FS_CHECKSUM)
    {
        unsigned short crc;
        if (handle->fcb->size / (255 - sizeof(unsigned short))
            == handle->readEnd / (255 - sizeof(unsigned short)))
        {
            crc = handle->fcb->crc;
        }
        else
        {
            blkExtFlashRead(serviceAddr(handle->curr, handle->readEnd),
                            &crc, sizeof(crc));
        }
        if (crc16Acc(handle->buf, end - handle->readEnd, 0) != crc)
        {
            fsSetError(FS_ERR_IO);
            handle->curr = backup;
            return 0;
        }
    }
    handle->readEnd = end;
    return 1;
}

variable_types:
backup
unsigned short

variable_types:
end
unsigned int

variable_types:
crc
unsigned short


blkRead

decompiled_code:
__int64 __fastcall blkRead(_DWORD *a1, _QWORD *a2, unsigned __int64 a3)
{
int v3; // ecx
unsigned int v5; // er8
__int64 v6; // rsi
unsigned __int64 v7; // r12
char *v8; // rsi
__int64 result; // rax

v3 = a1[2];
if ( (v3 & 1) != 0 )
{
v5 = a1[3];
v6 = v5 % 0xFD;
v7 = 253 - v6;
if ( *(_DWORD *)(*(_QWORD *)a1 + 4LL) - v5 <= (unsigned __int64)(253 - v6) )
v7 = *(_DWORD *)(*(_QWORD *)a1 + 4LL) - v5;
if ( v7 > a3 )
v7 = a3;
if ( !v7 )
return v7;
if ( (v3 & 8) == 0 )
{
if ( v5 == a1[68] )
{
if ( !(unsigned __int8)fillBuffer((__int64)a1) )
return -1LL;
v5 = a1[3];
v6 = v5 % 0xFD;
}
v8 = (char *)a1 + v6 + 18;
if ( (unsigned int)v7 >= 8 )
{
*a2 = *(_QWORD *)v8;
*(_QWORD *)((char *)a2 + (unsigned int)v7 - 8) = *(_QWORD *)&v8[(unsigned int)v7 - 8];
qmemcpy(
(void *)((unsigned __int64)(a2 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v8 - ((char *)a2 - ((unsigned __int64)(a2 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v7 + (_DWORD)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v7 & 4) != 0 )
{
*(_DWORD *)a2 = *(_DWORD *)v8;
*(_DWORD *)((char *)a2 + (unsigned int)v7 - 4) = *(_DWORD *)&v8[(unsigned int)v7 - 4];
}
else if ( (_DWORD)v7 )
{
*(_BYTE *)a2 = *v8;
if ( (v7 & 2) != 0 )
*(_WORD *)((char *)a2 + (unsigned int)v7 - 2) = *(_WORD *)&v8[(unsigned int)v7 - 2];
}
a1[3] = v7 + v5;
return v7;
}
doRead((__int64)a1, v5, (__int64)a2, v7);
a1[3] += v7;
result = v7;
}
else
{
assertionFailed("handle->mode & FS_READ", "../../../src/fs/block/block.c", 247LL);
result = blkOpen();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(_DWORD *a1, _QWORD *a2, unsigned __int64 a3)
{
int v3; // ecx
unsigned int v5; // er8
__int64 v6; // rsi
unsigned __int64 v7; // r12
char *v8; // rsi
__int64 result; // rax

v3 = a1[2];
if ( (v3 & 1) != 0 )
{
v5 = a1[3];
v6 = v5 % 0xFD;
v7 = 253 - v6;
if ( *(_DWORD *)(*(_QWORD *)a1 + 4LL) - v5 <= (unsigned __int64)(253 - v6) )
v7 = *(_DWORD *)(*(_QWORD *)a1 + 4LL) - v5;
if ( v7 > a3 )
v7 = a3;
if ( !v7 )
return v7;
if ( (v3 & 8) == 0 )
{
if ( v5 == a1[68] )
{
if ( !(unsigned __int8)func19((__int64)a1) )
return -1LL;
v5 = a1[3];
v6 = v5 % 0xFD;
}
v8 = (char *)a1 + v6 + 18;
if ( (unsigned int)v7 >= 8 )
{
*a2 = *(_QWORD *)v8;
*(_QWORD *)((char *)a2 + (unsigned int)v7 - 8) = *(_QWORD *)&v8[(unsigned int)v7 - 8];
qmemcpy(
(void *)((unsigned __int64)(a2 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v8 - ((char *)a2 - ((unsigned __int64)(a2 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v7 + (_DWORD)a2 - (((_DWORD)a2 + 8) & 0xFFFFFFF8)) >> 3));
}
else if ( (v7 & 4) != 0 )
{
*(_DWORD *)a2 = *(_DWORD *)v8;
*(_DWORD *)((char *)a2 + (unsigned int)v7 - 4) = *(_DWORD *)&v8[(unsigned int)v7 - 4];
}
else if ( (_DWORD)v7 )
{
*(_BYTE *)a2 = *v8;
if ( (v7 & 2) != 0 )
*(_WORD *)((char *)a2 + (unsigned int)v7 - 2) = *(_WORD *)&v8[(unsigned int)v7 - 2];
}
a1[3] = v7 + v5;
return v7;
}
func18((__int64)a1, v5, (__int64)a2, v7);
a1[3] += v7;
result = v7;
}
else
{
assertionFailed("handle->mode & FS_READ", "../../../src/fs/block/block.c", 247LL);
result = blkOpen();
}
return result;
}

original_code:
static ssize_t blkRead(void * __restrict__ id, void * __restrict__ buf, size_t count)
{
    struct fsBlockHandle * __restrict__ handle = id;
    ssize_t res;
    ((handle->mode & FS_READ) ? (void)0 : assertionFailed("handle->mode & FS_READ", "../../../src/fs/block/block.c", 247));
    mutexLock(&handle->fcb->mutex);
    count = ((count) < (handle->fcb->size - handle->pos) ? (count) : (handle->fcb->size - handle->pos));
    res = count = ((count) < ((255 - sizeof(uint16_t)) - handle->pos % (255 - sizeof(uint16_t))) ? (count) : ((255 - sizeof(uint16_t)) - handle->pos % (255 - sizeof(uint16_t))));
    if (count != 0)
    {
        if (!(handle->mode & FS_NOCACHE))
        {
            if (handle->pos == handle->readEnd)
            {
                if (!fillBuffer(handle))
                    res = -1;
            }
            if (res != -1)
            {
                memcpy(buf, handle->buf + handle->pos % (255 - sizeof(uint16_t)), count);
                handle->pos += count;
            }
        }
        else
        {
            doRead(handle, handle->pos, buf, count);
            handle->pos += count;
        }
    }
    mutexUnlock(&handle->fcb->mutex);
    return res;
}

canonical_original_code:
static long blkRead(void *id, void *buf, unsigned long count)
{
    struct fsBlockHandle *handle = id;
    long res;
    ((handle->mode & FS_READ) ? (void)0 : assertionFailed("handle->mode & FS_READ", "../../../src/fs/block/block.c", 247));
    mutexLock(&handle->fcb->mutex);
    count = ((count) < (handle->fcb->size - handle->pos) ? (count) : (handle->fcb->size - handle->pos));
    res = count = ((count) < ((255 - sizeof(unsigned short)) - handle->pos % (255 - sizeof(unsigned short))) ? (count) : ((255 - sizeof(unsigned short)) - handle->pos % (255 - sizeof(unsigned short))));
    if (count != 0)
    {
        if (!(handle->mode & FS_NOCACHE))
        {
            if (handle->pos == handle->readEnd)
            {
                if (!fillBuffer(handle))
                    res = -1;
            }
            if (res != -1)
            {
                memcpy(buf, handle->buf + handle->pos % (255 - sizeof(unsigned short)), count);
                handle->pos += count;
            }
        }
        else
        {
            doRead(handle, handle->pos, buf, count);
            handle->pos += count;
        }
    }
    mutexUnlock(&handle->fcb->mutex);
    return res;
}


doMsleep

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

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


serialPrintV

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

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

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

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

variable_types:
str
char *


doMsleep

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


getLedStruct

decompiled_code:
char *__fastcall getLedStruct(unsigned int a1)
{
char *result; // rax

result = 0LL;
if ( a1 <= 7 )
result = (char *)&ledData + 24 * a1;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(unsigned int a1)
{
char *result; // rax

result = 0LL;
if ( a1 <= 7 )
result = (char *)&ledData + 24 * a1;
return result;
}

original_code:
PCLed_t *getLedStruct(uint_t id)
{
    if (id >= 0 && id < LEDS_COUNT) {
        return &ledData[id];
    }
    return ((void *)0);
}

canonical_original_code:
struct PCLed_t *getLedStruct(unsigned int id)
{
    if (id >= 0 && id < LEDS_COUNT) {
        return &ledData[id];
    }
    return ((void *)0);
}

variable_types:
id
unsigned int

return_type: struct PCLed_t { unsigned int id; _Bool isOn; char *name; char *upcaseName; } *


printLeds

decompiled_code:
__int64 printLeds()
{
char *v0; // rbx
const char *v1; // rdx
const char *v2; // rcx
const char *v3; // rsi
__int64 result; // rax

if ( !printingSupressed )
{
v0 = (char *)&unk_1E4;
serialPrintV("LEDs:");
while ( *v0 )
{
if ( print_buf )
{
v1 = *(const char **)(v0 + 12);
v2 = "+\x1B[0;37;00m";
v3 = "\x1B[0;33m+";
LABEL_5:
debugPrintfFormat(" %s%s%s", v3, v1, v2);
serialPrint(print_buf);
}
LABEL_6:
v0 += 24;
if ( v0 == "s:" )
return serialPrintV("\n");
}
if ( !print_buf )
goto LABEL_6;
v1 = *(const char **)(v0 + 4);
v2 = "-\x1B[0;37;00m";
v3 = "\x1B[0;34m-";
goto LABEL_5;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
char *v0; // rbx
const char *v1; // rdx
const char *v2; // rcx
const char *v3; // rsi
__int64 result; // rax

if ( !printingSupressed )
{
v0 = (char *)&unk_1E4;
func0("LEDs:");
while ( *v0 )
{
if ( print_buf )
{
v1 = *(const char **)(v0 + 12);
v2 = "+\x1B[0;37;00m";
v3 = "\x1B[0;33m+";
LABEL_5:
debugPrintfFormat(" %s%s%s", v3, v1, v2);
serialPrint(print_buf);
}
LABEL_6:
v0 += 24;
if ( v0 == "s:" )
return func0("\n");
}
if ( !print_buf )
goto LABEL_6;
v1 = *(const char **)(v0 + 4);
v2 = "-\x1B[0;37;00m";
v3 = "\x1B[0;34m-";
goto LABEL_5;
}
return result;
}

canonical_original_code:
void printLeds()
{
    if (printingSupressed) return;
    if (1 == 1) { serialPrintV("LEDs:"); } else { if (_print_buf) { debugPrintfFormat("LEDs:"); serialPrint(_print_buf); }; };
    unsigned int i;
    for (i = 0; i < LEDS_COUNT; i++) {
        if (ledData[i].isOn)
        {
            if (4 == 1) { serialPrintV(" %s%s%s", "\033" "\[" "0" ";" "33" "m" "+", ledData[i].upcaseName, "+" "\033" "[" "0" ";" "37" ";" "00" "m"); } else { if (_print_buf) { debugPrintfFormat(" %s%s%s", "\033" "\[" "0" ";" "33" "m" "+", ledData[i].upcaseName, "+" "\033" "[" "0" ";" "37" ";" "00" "m"); serialPrint(_print_buf); }; };
        } else
        {
            if (4 == 1) { serialPrintV(" %s%s%s", "\033" "\[" "0" ";" "34" "m" "-", ledData[i].name, "-" "\033" "[" "0" ";" "37" ";" "00" "m"); } else { if (_print_buf) { debugPrintfFormat(" %s%s%s", "\033" "\[" "0" ";" "34" "m" "-", ledData[i].name, "-" "\033" "[" "0" ";" "37" ";" "00" "m"); serialPrint(_print_buf); }; };
        }
    }
    if (1 == 1) { serialPrintV("\n"); } else { if (_print_buf) { debugPrintfFormat("\n"); serialPrint(_print_buf); }; };
}

variable_types:
i
unsigned int


pcLedOn

decompiled_code:
char *__fastcall pcLedOn(unsigned int a1)
{
char *result; // rax

result = getLedStruct(a1);
if ( result )
{
result[4] = 1;
result = (char *)printLeds();
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(unsigned int a1)
{
char *result; // rax

result = func2(a1);
if ( result )
{
result[4] = 1;
result = (char *)func3();
}
return result;
}

original_code:
void pcLedOn(uint_t id) {
    PCLed_t *led = getLedStruct(id);
    if (led) {
        led->isOn = 1;
        printLeds();
    }
}

canonical_original_code:
void pcLedOn(unsigned int id) {
    struct PCLed_t *led = getLedStruct(id);
    if (led) {
        led->isOn = 1;
        printLeds();
    }
}

variable_types: struct PCLed_t { unsigned int id; _Bool isOn; char *name; char *upcaseName; } *


pcLedOff

decompiled_code:
char *__fastcall pcLedOff(unsigned int a1)
{
char *result; // rax

result = getLedStruct(a1);
if ( result )
{
result[4] = 0;
result = (char *)printLeds();
}
return result;
}

canonical_decompiled_code:
char *__fastcall func5(unsigned int a1)
{
char *result; // rax

result = func2(a1);
if ( result )
{
result[4] = 0;
result = (char *)func3();
}
return result;
}

original_code:
void pcLedOff(uint_t id) {
    PCLed_t *led = getLedStruct(id);
    if (led) {
        led->isOn = 0;
        printLeds();
    }
}

canonical_original_code:
void pcLedOff(unsigned int id) {
    struct PCLed_t *led = getLedStruct(id);
    if (led) {
        led->isOn = 0;
        printLeds();
    }
}

user_defined_types: struct PCLed_t { unsigned int id; _Bool isOn; char *name; char *upcaseName; }


pcIsLedOn

decompiled_code:
__int64 __fastcall pcIsLedOn(unsigned int a1)
{
char *v1; // rax
unsigned int v2; // er8

v1 = getLedStruct(a1);
v2 = 0;
if ( v1 )
v2 = (unsigned __int8)v1[4];
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
char *v1; // rax
unsigned int v2; // er8

v1 = func2(a1);
v2 = 0;
if ( v1 )
v2 = (unsigned __int8)v1[4];
return v2;
}

original_code:
_Bool pcIsLedOn(uint_t id) {
    PCLed_t *led = getLedStruct(id);
    return led ? led->isOn : 0;
}

canonical_original_code:
_Bool pcIsLedOn(unsigned int id) {
    struct PCLed_t *led = getLedStruct(id);
    return led ? led->isOn : 0;
}


pinSetPc

decompiled_code:
char *__fastcall pinSetPc(int a1, unsigned int a2)
{
char *result; // rax

if ( a1 == 255 )
result = pcLedOn(a2);
return result;
}

canonical_decompiled_code:
char *__fastcall func7(int a1, unsigned int a2)
{
char *result; // rax

if ( a1 == 255 )
result = func4(a2);
return result;
}

original_code:
void pinSetPc(int port, int pin)
{
    ;
    if (port != 255) return;
    pcLedOn(pin);
}

variable_types: port int

variable_types: pin int


pinClearPc

decompiled_code:
char *__fastcall pinClearPc(int a1, unsigned int a2)
{
char *result; // rax

if ( a1 == 255 )
result = pcLedOff(a2);
return result;
}

canonical_decompiled_code:
char *__fastcall func8(int a1, unsigned int a2)
{
char *result; // rax

if ( a1 == 255 )
result = func5(a2);
return result;
}

original_code:
void pinClearPc(int port, int pin)
{
    ;
    if (port != 255) return;
    pcLedOff(pin);
}


pinReadPc

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

result = 0LL;
if ( a1 == 255 )
result = (unsigned __int8)pcIsLedOn(a2);
return result;
}

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

result = 0LL;
if ( a1 == 255 )
result = (unsigned __int8)func6(a2);
return result;
}

original_code:
int pinReadPc(int port, int pin)
{
    ;
    if (port != 255) return 0;
    return pcIsLedOn(pin);
}

return_type: int


suppressLedOutput

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

if ( printingSupressed != a1 )
result = printLeds();
printingSupressed = a1;
return result;
}

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

if ( printingSupressed != a1 )
result = func3();
printingSupressed = a1;
return result;
}

original_code:
void suppressLedOutput(_Bool yes)
{
    if( yes != printingSupressed ){
            printLeds();
    }
    printingSupressed = yes;
}


readNum

decompiled_code:
__int64 __fastcall readNum(int fd, _BYTE *a2, char *a3)
{
char v3; // r14
unsigned int v5; // er12
int v6; // edx
__int64 result; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

*a2 = 0;
v3 = *a3;
if ( *a3 )
{
v5 = -1;
LABEL_12:
result = v5;
}
else
{
v5 = -1;
while ( (unsigned int)read(fd, buf, 1uLL) == 1 )
{
if ( (unsigned __int8)(buf[0] - 48) <= 9u )
{
v6 = 0;
if ( (_WORD)v5 != 0xFFFF )
v6 = 10 * v5;
v5 = v6 + (unsigned __int8)buf[0] - 48;
v3 = 1;
}
else
{
if ( buf[0] == 10 || buf[0] == 13 )
*a2 = 1;
if ( v3 )
goto LABEL_12;
}
if ( *a3 )
goto LABEL_12;
}
*a3 = 1;
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, _BYTE *a2, char *a3)
{
char v3; // r14
unsigned int v5; // er12
int v6; // edx
__int64 result; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

*a2 = 0;
v3 = *a3;
if ( *a3 )
{
v5 = -1;
LABEL_12:
result = v5;
}
else
{
v5 = -1;
while ( (unsigned int)read(fd, buf, 1uLL) == 1 )
{
if ( (unsigned __int8)(buf[0] - 48) <= 9u )
{
v6 = 0;
if ( (_WORD)v5 != 0xFFFF )
v6 = 10 * v5;
v5 = v6 + (unsigned __int8)buf[0] - 48;
v3 = 1;
}
else
{
if ( buf[0] == 10 || buf[0] == 13 )
*a2 = 1;
if ( v3 )
goto LABEL_12;
}
if ( *a3 )
goto LABEL_12;
}
*a3 = 1;
result = v5;
}
return result;
}

original_code:
static uint16_t readNum(int fd, _Bool *isNewLine, _Bool *isEOF) {
    *isNewLine = 0;
    uint16_t n = 0xffff;
    _Bool numStarted = 0;
    while (!*isEOF)
    {
        uint8_t c;
        int ret = read(fd, &c, 1);
        if (ret != 1) {
            *isEOF = 1;
            break;
        }
        if (c >= '0' && c <= '9') {
            if (n != 0xffff) {
                n = n * 10;
            } else {
                n = 0;
            }
            n += (c - '0');
            numStarted = 1;
        } else {
            if (c == '\n' || c == '\r') {
                *isNewLine = 1;
            }
            if (numStarted) break;
        }
    }
    return n;
}

canonical_original_code:
static unsigned short readNum(int fd, _Bool *isNewLine, _Bool *isEOF) {
    *isNewLine = 0;
    unsigned short n = 0xffff;
    _Bool numStarted = 0;
    while (!*isEOF)
    {
        unsigned char c;
        int ret = read(fd, &c, 1);
        if (ret != 1) {
            *isEOF = 1;
            break;
        }
        if (c >= '0' && c <= '9') {
            if (n != 0xffff) {
                n = n * 10;
            } else {
                n = 0;
            }
            n += (c - '0');
            numStarted = 1;
        } else {
            if (c == '\n' || c == '\r') {
                *isNewLine = 1;
            }
            if (numStarted) break;
        }
    }
    return n;
}

variable_types: fd int

variable_types:
isNewLine
_Bool *

variable_types:
isEOF
_Bool *

variable_types:
n
unsigned short

variable_types:
c
unsigned char

variable_types: ret int


hplAdcInit

decompiled_code:
int hplAdcInit()
{
int result; // eax
int v1; // ebp
char v2; // r12
unsigned __int8 v3; // bl
unsigned __int16 v4; // dx
__int16 v5; // ax
char v6; // [rsp+0h] [rbp-1Ah] BYREF
char v7[25]; // [rsp+1h] [rbp-19h] BYREF

valueCount = 0LL;
memset(values, 0, sizeof(values));
xmmword_250 = 0LL;
currPos = 0LL;
xmmword_230 = 0LL;
result = open("adcValues.txt", 0);
if ( result >= 0 )
{
v6 = 0;
v1 = result;
v2 = 0;
v3 = -1;
do
{
v5 = readNum(v1, v7, &v6);
if ( v5 != -1 )
{
if ( v3 == 0xFF || v2 )
{
v3 = v5;
}
else if ( v3 <= 0xFu )
{
v4 = *((_WORD *)&valueCount + v3);
values[256 * (unsigned __int64)v3 + v4] = v5;
*((_WORD *)&valueCount + v3) = v4 + 1;
}
v2 = v7[0];
}
}
while ( !v6 );
result = close(v1);
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
int v1; // ebp
char v2; // r12
unsigned __int8 v3; // bl
unsigned __int16 v4; // dx
__int16 v5; // ax
char v6; // [rsp+0h] [rbp-1Ah] BYREF
char v7[25]; // [rsp+1h] [rbp-19h] BYREF

valueCount = 0LL;
memset(values, 0, sizeof(values));
xmmword_250 = 0LL;
currPos = 0LL;
xmmword_230 = 0LL;
result = open("adcValues.txt", 0);
if ( result >= 0 )
{
v6 = 0;
v1 = result;
v2 = 0;
v3 = -1;
do
{
v5 = func0(v1, v7, &v6);
if ( v5 != -1 )
{
if ( v3 == 0xFF || v2 )
{
v3 = v5;
}
else if ( v3 <= 0xFu )
{
v4 = *((_WORD *)&valueCount + v3);
values[256 * (unsigned __int64)v3 + v4] = v5;
*((_WORD *)&valueCount + v3) = v4 + 1;
}
v2 = v7[0];
}
}
while ( !v6 );
result = close(v1);
}
return result;
}

original_code:
void hplAdcInit() {
    memset(values, 0, sizeof(values));
    memset(valueCount, 0, sizeof(valueCount));
    memset(currPos, 0, sizeof(currPos));
    int fd = open("adcValues.txt", 00);
    if (fd < 0) return;
    uint8_t chnum = 255;
    _Bool wasNewLine = 0;
    _Bool isEOF = 0;
    while (!isEOF)
    {
        _Bool isNewLine;
        uint16_t num = readNum(fd, &isNewLine, &isEOF);
        if (num != 0xffff) {
            if (wasNewLine || chnum == 255) {
                chnum = num;
                wasNewLine = 0;
            } else {
                if (chnum < 16) {
                    values[chnum][valueCount[chnum]] = num;
                    ++valueCount[chnum];
                }
            }
            if (isNewLine) {
                wasNewLine = 1;
            }
        }
    }
    close(fd);
    adcOk = 1;
}

canonical_original_code:
void hplAdcInit() {
    memset(values, 0, sizeof(values));
    memset(valueCount, 0, sizeof(valueCount));
    memset(currPos, 0, sizeof(currPos));
    int fd = open("adcValues.txt", 00);
    if (fd < 0) return;
    unsigned char chnum = 255;
    _Bool wasNewLine = 0;
    _Bool isEOF = 0;
    while (!isEOF)
    {
        _Bool isNewLine;
        unsigned short num = readNum(fd, &isNewLine, &isEOF);
        if (num != 0xffff) {
            if (wasNewLine || chnum == 255) {
                chnum = num;
                wasNewLine = 0;
            } else {
                if (chnum < 16) {
                    values[chnum][valueCount[chnum]] = num;
                    ++valueCount[chnum];
                }
            }
            if (isNewLine) {
                wasNewLine = 1;
            }
        }
    }
    close(fd);
    adcOk = 1;
}

variable_types:
chnum
unsigned char

variable_types:
num
unsigned short


hplAdcGetVal

decompiled_code:
__int64 hplAdcGetVal()
{
__int64 v0; // rax
__int64 v1; // rdx

v0 = (unsigned __int8)currChannel;
v1 = *((unsigned __int16 *)&currPos + (unsigned __int8)currChannel);
*((_WORD *)&currPos + (unsigned __int8)currChannel) = v1 + 1;
if ( (unsigned __int16)(v1 + 1) >= *((_WORD *)&valueCount + v0) )
*((_WORD *)&currPos + v0) = 0;
return (unsigned __int16)values[256 * v0 + v1];
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // rdx

v0 = (unsigned __int8)currChannel;
v1 = *((unsigned __int16 *)&currPos + (unsigned __int8)currChannel);
*((_WORD *)&currPos + (unsigned __int8)currChannel) = v1 + 1;
if ( (unsigned __int16)(v1 + 1) >= *((_WORD *)&valueCount + v0) )
*((_WORD *)&currPos + v0) = 0;
return (unsigned __int16)values[256 * v0 + v1];
}

original_code:
uint16_t hplAdcGetVal() {
    uint16_t pos = currPos[currChannel]++;
    if (currPos[currChannel] >= valueCount[currChannel]) {
        currPos[currChannel] = 0;
    }
    return values[currChannel][pos];
}

canonical_original_code:
unsigned short hplAdcGetVal() {
    unsigned short pos = currPos[currChannel]++;
    if (currPos[currChannel] >= valueCount[currChannel]) {
        currPos[currChannel] = 0;
    }
    return values[currChannel][pos];
}

variable_types:
pos
unsigned short


hplAdcSetChannel

decompiled_code:
void __fastcall hplAdcSetChannel(char a1)
{
currChannel = a1;
}

canonical_decompiled_code:
void __fastcall func3(char a1)
{
currChannel = a1;
}

original_code:
void hplAdcSetChannel(uint8_t ch) {
    currChannel = ch;
}

canonical_original_code:
void hplAdcSetChannel(unsigned char ch) {
    currChannel = ch;
}

variable_types:
ch
unsigned char


hplAdcGetChannel

decompiled_code:
__int64 hplAdcGetChannel()
{
return (unsigned __int8)currChannel;
}

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

original_code:
uint8_t hplAdcGetChannel(void) {
    return currChannel;
}

canonical_original_code:
unsigned char hplAdcGetChannel() {
    return currChannel;
}

return_type: unsigned char


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


isStackAddress

decompiled_code:
bool __fastcall isStackAddress(int a1)
{
return (unsigned int)(a1 - 14336) <= 0xFF;
}

canonical_decompiled_code:
bool __fastcall func1(int a1)
{
return (unsigned int)(a1 - 14336) <= 0xFF;
}

original_code:
static inline _Bool isStackAddress(void *x)
{
    MemoryAddress_t ix = (MemoryAddress_t) x;
    if (ix >= ((MemoryAddress_t)0x3800) && ix < ((MemoryAddress_t)0x3900)) {
        return 1;
    }
    return 0;
}

canonical_original_code:
static inline _Bool isStackAddress(void *x)
{
    unsigned int ix = (MemoryAddress_t) x;
    if (ix >= ((unsigned int)0x3800) && ix < ((unsigned int)0x3900)) {
        return 1;
    }
    return 0;
}

variable_types:
x
void *

variable_types:
ix
unsigned int


doMsleep

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

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

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

variable_types:
milliseconds
unsigned short


initAlarms

decompiled_code:
void initAlarms()
{
alarmListHead = 0LL;
}

canonical_decompiled_code:
void func3()
{
alarmListHead = 0LL;
}

original_code:
void initAlarms(void)
{
    do { (&alarmListHead)->slh_first = ((void *)0); } while ( 0);
    ;
}

canonical_original_code:
void initAlarms()
{
    do { (&alarmListHead)->slh_first = ((void *)0); } while ( 0);
    ;
}


alarmsProcess

decompiled_code:
__int64 *alarmsProcess()
{
__int64 *result; // rax
__int64 v1; // rbx
int v2; // er12
__int64 *v3; // rbp

result = (__int64 *)getJiffies();
v1 = alarmListHead;
if ( alarmListHead )
{
v2 = (int)result;
for ( result = &alarmListHead; v2 - *(_DWORD *)(v1 + 24) >= 0; result = v3 )
{
v3 = (__int64 *)v1;
*result = *(_QWORD *)v1;
result = (__int64 *)(*(__int64 (__fastcall **)(_QWORD))(v1 + 8))(*(_QWORD *)(v1 + 16));
v1 = *(_QWORD *)v1;
if ( !v1 )
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 *func4()
{
__int64 *result; // rax
__int64 v1; // rbx
int v2; // er12
__int64 *v3; // rbp

result = (__int64 *)func0();
v1 = alarmListHead;
if ( alarmListHead )
{
v2 = (int)result;
for ( result = &alarmListHead; v2 - *(_DWORD *)(v1 + 24) >= 0; result = v3 )
{
v3 = (__int64 *)v1;
*result = *(_QWORD *)v1;
result = (__int64 *)(*(__int64 (__fastcall **)(_QWORD))(v1 + 8))(*(_QWORD *)(v1 + 16));
v1 = *(_QWORD *)v1;
if ( !v1 )
break;
}
}
return result;
}

original_code:
void alarmsProcess(void)
{
    uint32_t now = (uint32_t) getJiffies();
    Alarm_t **a = &((&alarmListHead)->slh_first);
    while (*a) {
        Alarm_t *ap = *a;
        if (((int32_t) ((now) - (ap->jiffies)) < 0)) {
            break;
        }
        Alarm_t **next = &((ap)->chain.sle_next);
        *a = *next;
        ap->callback(ap->data);
        a = next;
    }
}

canonical_original_code:
void alarmsProcess()
{
    unsigned int now = (uint32_t) getJiffies();
    struct Alarm_s **a = &((&alarmListHead)->slh_first);
    while (*a) {
        struct Alarm_s *ap = *a;
        if (((int) ((now) - (ap->jiffies)) < 0)) {
            break;
        }
        struct Alarm_s **next = &((ap)->chain.sle_next);
        *a = *next;
        ap->callback(ap->data);
        a = next;
    }
}

variable_types:
now
unsigned int

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


getAlarmTime

decompiled_code:
__int64 __fastcall getAlarmTime(__int64 a1, __int64 a2)
{
int v2; // eax
int v3; // edx

v2 = getJiffies(a1, a2, *(unsigned int *)(a1 + 24));
return (unsigned int)(v3 - v2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
int v2; // eax
int v3; // edx

v2 = func0(a1, a2, *(unsigned int *)(a1 + 24));
return (unsigned int)(v3 - v2);
}

original_code:
uint32_t getAlarmTime(Alarm_t *alarm)
{
    return (uint32_t)(alarm->jiffies - getJiffies());
}

canonical_original_code:
unsigned int getAlarmTime(struct Alarm_s *alarm)
{
    return (unsigned int)(alarm->jiffies - getJiffies());
}

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


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

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

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

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

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


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


radioOn

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

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

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

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


recvRadio

decompiled_code:
__int64 recvRadio()
{
__int64 result; // rax
unsigned int v1; // ebp

greenLedToggle();
result = radioRecv_constprop_0();
if ( (result & 0x8000u) != 0LL )
return serialPrintV("radio receive failed\n");
if ( (_WORD)result )
{
v1 = (__int16)result;
if ( print_buf )
{
debugPrintfFormat("radio receive %d bytes\n", (unsigned int)(__int16)result);
serialPrint(print_buf);
}
result = debugHexdump(&radioBuffer, v1);
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 result; // rax
unsigned int v1; // ebp

func2();
result = func4();
if ( (result & 0x8000u) != 0LL )
return func0("radio receive failed\n");
if ( (_WORD)result )
{
v1 = (__int16)result;
if ( print_buf )
{
debugPrintfFormat("radio receive %d bytes\n", (unsigned int)(__int16)result);
serialPrint(print_buf);
}
result = debugHexdump(&radioBuffer, v1);
}
return result;
}

original_code:
void recvRadio(void)
{
    int16_t len;
    greenLedToggle();
    len = radioRecv(radioBuffer, sizeof(radioBuffer));
    if (len < 0) {
        if (1 == 1) { serialPrintV("radio receive failed\n"); } else { if (_print_buf) { debugPrintfFormat("radio receive failed\n"); serialPrint(_print_buf); }; };
    }
    else if (len > 0 ) {
        if (2 == 1) { serialPrintV("radio receive %d bytes\n", len); } else { if (_print_buf) { debugPrintfFormat("radio receive %d bytes\n", len); serialPrint(_print_buf); }; };
        debugHexdump((uint8_t *) radioBuffer, len);
    }
}

canonical_original_code:
void recvRadio()
{
    short len;
    greenLedToggle();
    len = radioRecv(radioBuffer, sizeof(radioBuffer));
    if (len < 0) {
        if (1 == 1) { serialPrintV("radio receive failed\n"); } else { if (_print_buf) { debugPrintfFormat("radio receive failed\n"); serialPrint(_print_buf); }; };
    }
    else if (len > 0 ) {
        if (2 == 1) { serialPrintV("radio receive %d bytes\n", len); } else { if (_print_buf) { debugPrintfFormat("radio receive %d bytes\n", len); serialPrint(_print_buf); }; };
        debugHexdump((unsigned char *) radioBuffer, len);
    }
}

variable_types: len short


recvSerial

decompiled_code:
__int64 __fastcall recvSerial(unsigned __int8 a1)
{
serialPrintV(serialBuffer);
if ( serialBuffer[a1 - 1] != 10 )
serialPrintV("\n");
return radioSend_isra_0((__int64)serialBuffer, a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1)
{
func0(serialBuffer);
if ( serialBuffer[a1 - 1] != 10 )
func0("\n");
return func8((__int64)serialBuffer, a1);
}

original_code:
void recvSerial(uint8_t length) {
    if (1 == 1) { serialPrintV(serialBuffer); } else { if (_print_buf) { debugPrintfFormat(serialBuffer); serialPrint(_print_buf); }; };
    if (serialBuffer[length - 1] != '\n') {
        if (1 == 1) { serialPrintV("\n"); } else { if (_print_buf) { debugPrintfFormat("\n"); serialPrint(_print_buf); }; };
    }
    radioSend(serialBuffer, length);
}

canonical_original_code:
void recvSerial(unsigned char length) {
    if (1 == 1) { serialPrintV(serialBuffer); } else { if (_print_buf) { debugPrintfFormat(serialBuffer); serialPrint(_print_buf); }; };
    if (serialBuffer[length - 1] != '\n') {
        if (1 == 1) { serialPrintV("\n"); } else { if (_print_buf) { debugPrintfFormat("\n"); serialPrint(_print_buf); }; };
    }
    radioSend(serialBuffer, length);
}

variable_types:
length
unsigned char


doMsleep

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

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


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


randomInit

decompiled_code:
__int64 randomInit()
{
__int64 result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
result = (unsigned int)(LODWORD(v1.tv_usec) + LODWORD(v1.tv_sec));
randomKey = LODWORD(v1.tv_usec) + LODWORD(v1.tv_sec);
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
result = (unsigned int)(LODWORD(v1.tv_usec) + LODWORD(v1.tv_sec));
randomKey = LODWORD(v1.tv_usec) + LODWORD(v1.tv_sec);
return result;
}

original_code:
void randomInit() {
    struct timeval tv;
    gettimeofday(&tv, ((void *)0));
    randomKey = tv.tv_usec + tv.tv_sec;
}

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


randomSeed

decompiled_code:
void __fastcall randomSeed(unsigned __int16 a1)
{
randomKey = a1;
}

canonical_decompiled_code:
void __fastcall func2(unsigned __int16 a1)
{
randomKey = a1;
}

original_code:
void randomSeed(uint16_t seed) {
    randomKey = seed;
}

canonical_original_code:
void randomSeed(unsigned short seed) {
    randomKey = seed;
}

variable_types:
seed
unsigned short


randomNumber

decompiled_code:
__int64 randomNumber()
{
randomKey = 1664525 * randomKey + 1013904223;
return HIWORD(randomKey);
}

canonical_decompiled_code:
__int64 func3()
{
randomKey = 1664525 * randomKey + 1013904223;
return HIWORD(randomKey);
}

original_code:
uint16_t randomNumber(void) {
    randomKey = randomKey * 1664525ul + 1013904223ul;
    return (uint16_t) (randomKey >> 16);
}

canonical_original_code:
unsigned short randomNumber() {
    randomKey = randomKey * 1664525ul + 1013904223ul;
    return (unsigned short) (randomKey >> 16);
}


streamClose

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

if ( files == -1 )
{
if ( byte_3F1 == -1 )
return result;
return fsClose();
}
result = fsClose();
if ( byte_3F1 != -1 )
return fsClose();
return result;
}

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

if ( files == -1 )
{
if ( byte_3F1 == -1 )
return result;
return fsClose();
}
result = fsClose();
if ( byte_3F1 != -1 )
return fsClose();
return result;
}

original_code:
static void streamClose(void)
{
    int i;
    for (i = 0; i < 2; i++)
    {
        if (files[i] != -1)
            fsClose(files[i]);
    }
}

canonical_original_code:
static void streamClose()
{
    int i;
    for (i = 0; i < 2; i++)
    {
        if (files[i] != -1)
            fsClose(files[i]);
    }
}

variable_types: i int


name

decompiled_code:
char *__fastcall name(unsigned int a1)
{
snprintf(s_0, 0xCuLL, "/blk/file%02d", a1);
return s_0;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned int a1)
{
snprintf(s_0, 0xCuLL, "/blk/file%02d", a1);
return s_0;
}

original_code:
static const char *name(int i)
{
    static char s[12];
    snprintf(s, 12, "/blk/file%02d", i);
    return s;
}

canonical_original_code:
static char *name(int i)
{
    static char s[12];
    snprintf(s, 12, "/blk/file%02d", i);
    return s;
}

variable_types:
s
char [12]

return_type: char *


error

decompiled_code:
__int64 __fastcall error(const char *a1)
{
if ( print_buf )
{
debugPrintfFormat("error: %s\n", a1);
serialPrint(print_buf);
}
assertionFailed("false", "main.c", 41LL);
return streamOpen();
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
if ( print_buf )
{
debugPrintfFormat("error: %s\n", a1);
serialPrint(print_buf);
}
assertionFailed("false", "main.c", 41LL);
return func3();
}

original_code:
static void error(const char *msg)
{
    if (2 == 1) { serialPrintV("error: %s\n", msg); } else { if (_print_buf) { debugPrintfFormat("error: %s\n", msg); serialPrint(_print_buf); }; };
    ((0) ? (void)0 : assertionFailed("false", "main.c", 41));
}

canonical_original_code:
static void error(char *msg)
{
    if (2 == 1) { serialPrintV("error: %s\n", msg); } else { if (_print_buf) { debugPrintfFormat("error: %s\n", msg); serialPrint(_print_buf); }; };
    ((0) ? (void)0 : assertionFailed("false", "main.c", 41));
}

variable_types:
msg
char *


streamOpen

decompiled_code:
__int64 __fastcall streamOpen(unsigned int a1)
{
__int64 v1; // rbp
unsigned int v2; // er13
int v3; // ebx
char *v4; // rax
__int64 result; // rax
char *v6; // rax

v1 = 0LL;
v2 = 0;
current = 0;
fileCount = 0;
v3 = 2 * (a1 != 1);
if ( v3 )
goto LABEL_9;
while ( 1 )
{
v4 = name(v2);
result = fsOpen(v4, a1);
files[v1] = result;
if ( (_BYTE)result != 0xFF )
++fileCount;
if ( v1 == 1 )
break;
v1 = 1LL;
v2 = 1;
v3 = 2 * (a1 != 1);
if ( v3 )
{
LABEL_9:
v6 = name(v1);
fsRemove(v6);
}
}
if ( v3 )
{
if ( fileCount == 2 )
return result;
LABEL_12:
error("failed to open stream");
return streamRead_constprop_0();
}
if ( a1 == 1 )
{
result = (unsigned int)fileCount;
if ( fileCount <= 0 )
goto LABEL_12;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
__int64 v1; // rbp
unsigned int v2; // er13
int v3; // ebx
char *v4; // rax
__int64 result; // rax
char *v6; // rax

v1 = 0LL;
v2 = 0;
current = 0;
fileCount = 0;
v3 = 2 * (a1 != 1);
if ( v3 )
goto LABEL_9;
while ( 1 )
{
v4 = func1(v2);
result = fsOpen(v4, a1);
files[v1] = result;
if ( (_BYTE)result != 0xFF )
++fileCount;
if ( v1 == 1 )
break;
v1 = 1LL;
v2 = 1;
v3 = 2 * (a1 != 1);
if ( v3 )
{
LABEL_9:
v6 = func1(v1);
fsRemove(v6);
}
}
if ( v3 )
{
if ( fileCount == 2 )
return result;
LABEL_12:
func2("failed to open stream");
return func4();
}
if ( a1 == 1 )
{
result = (unsigned int)fileCount;
if ( fileCount <= 0 )
goto LABEL_12;
}
return result;
}

original_code:
static void streamOpen(fsMode_t mode)
{
    int i;
    fileCount = current = 0;
    for (i = 0; i < 2; i++)
    {
        if (mode & FS_APPEND)
        {
            fsRemove(name(i));
        }
        files[i] = fsOpen(name(i), mode);
        if (files[i] != -1)
            fileCount++;
    }
    if ((mode & FS_APPEND && fileCount != 2)
        || (mode & FS_READ && fileCount < 1))
    {
        error("failed to open stream");
    }
}

canonical_original_code:
static void streamOpen(enum fsMode_t mode)
{
    int i;
    fileCount = current = 0;
    for (i = 0; i < 2; i++)
    {
        if (mode & FS_APPEND)
        {
            fsRemove(name(i));
        }
        files[i] = fsOpen(name(i), mode);
        if (files[i] != -1)
            fileCount++;
    }
    if ((mode & FS_APPEND && fileCount != 2)
        || (mode & FS_READ && fileCount < 1))
    {
        error("failed to open stream");
    }
}

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


doMsleep

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

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

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


flashAddr

decompiled_code:
__int64 __fastcall flashAddr(unsigned __int16 a1, int a2)
{
return (a1 << 14) + a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1, int a2)
{
return (a1 << 14) + a2;
}

original_code:
static inline uint32_t flashAddr(blk_t block, fsOff_t offset)
{
    return (uint32_t)block * (0x10000 / 4) + offset;
}

canonical_original_code:
static inline unsigned int flashAddr(unsigned short block, unsigned int offset)
{
    return (unsigned int)block * (0x10000 / 4) + offset;
}

variable_types:
block
unsigned short

variable_types:
offset
unsigned int


segNum

decompiled_code:
__int16 __fastcall segNum(unsigned __int16 a1)
{
return a1 >> 2;
}

canonical_decompiled_code:
__int16 __fastcall func1(unsigned __int16 a1)
{
return a1 >> 2;
}

original_code:
static inline segnum_t segNum(blk_t block)
{
    return block / 4;
}

canonical_original_code:
static inline unsigned short segNum(unsigned short block)
{
    return block / 4;
}


segOffset

decompiled_code:
__int64 __fastcall segOffset(char a1)
{
return a1 & 3;
}

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

original_code:
static inline blk_t segOffset(blk_t block)
{
    return block % 4;
}

canonical_original_code:
static inline unsigned short segOffset(unsigned short block)
{
    return block % 4;
}


makeBlock

decompiled_code:
__int64 __fastcall makeBlock(int a1, int a2)
{
return (unsigned int)(a2 + 4 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
return (unsigned int)(a2 + 4 * a1);
}

original_code:
static inline blk_t makeBlock(segnum_t seg, blk_t off)
{
    return seg * 4 + off;
}

canonical_original_code:
static inline unsigned short makeBlock(unsigned short seg, unsigned short off)
{
    return seg * 4 + off;
}

variable_types:
seg
unsigned short

variable_types:
off
unsigned short


enableFlash

decompiled_code:
// attributes: thunk
__int64 enableFlash()
{
return extFlashWake();
}

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

original_code:
static inline void enableFlash(void)
{
    extFlashWake();
}

canonical_original_code:
static inline void enableFlash()
{
    extFlashWake();
}


disableFlash

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

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

original_code:
static inline void disableFlash(void)
{
    extFlashSleep();
}

canonical_original_code:
static inline void disableFlash()
{
    extFlashSleep();
}


blkExtFlashEraseSector

decompiled_code:
__int64 __fastcall blkExtFlashEraseSector(unsigned int a1)
{
enableFlash();
extFlashEraseSector(a1);
return disableFlash();
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
func4();
extFlashEraseSector(a1);
return func5();
}

original_code:
static inline void blkExtFlashEraseSector(uint32_t addr)
{
    enableFlash();
    extFlashEraseSector(addr);
    disableFlash();
}

canonical_original_code:
static inline void blkExtFlashEraseSector(unsigned int addr)
{
    enableFlash();
    extFlashEraseSector(addr);
    disableFlash();
}

variable_types:
addr
unsigned int


readSeg

decompiled_code:
__int64 __fastcall readSeg(__int16 a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

eepromRead((unsigned __int16)(a1 + 2), v2, 1LL);
return (unsigned __int8)v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1)
{
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

eepromRead((unsigned __int16)(a1 + 2), v2, 1LL);
return (unsigned __int8)v2[0];
}

original_code:
static inline segment_t readSeg(segnum_t i)
{
    segment_t s;
    eepromRead(sizeof(uint16_t) + i * sizeof(segment_t), &s, sizeof(s));
    return s;
}

canonical_original_code:
static inline unsigned char readSeg(unsigned short i)
{
    unsigned char s;
    eepromRead(sizeof(unsigned short) + i * sizeof(segment_t), &s, sizeof(s));
    return s;
}

variable_types:
i
unsigned short

variable_types:
s
unsigned char


findFreeBlock

decompiled_code:
__int64 __fastcall findFreeBlock(unsigned __int16 a1)
{
unsigned __int16 v1; // bx
int v2; // edx
unsigned __int16 v3; // ax

v1 = randomNumber() & 3;
v2 = (unsigned __int8)readSeg(a1);
v3 = v1;
do
{
if ( ((v2 >> (2 * v3)) & 3) == 1 )
return makeBlock(a1, v3);
v3 = ((_BYTE)v3 + 1) & 3;
}
while ( v1 != v3 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int16 a1)
{
unsigned __int16 v1; // bx
int v2; // edx
unsigned __int16 v3; // ax

v1 = randomNumber() & 3;
v2 = (unsigned __int8)func7(a1);
v3 = v1;
do
{
if ( ((v2 >> (2 * v3)) & 3) == 1 )
return func3(a1, v3);
v3 = ((_BYTE)v3 + 1) & 3;
}
while ( v1 != v3 );
return 0xFFFFFFFFLL;
}

original_code:
static blk_t findFreeBlock(segnum_t seg)
{
    blk_t start = randomNumber() % 4,
              i = start;
    segment_t s = readSeg(seg);
    do
    {
        if (((s) >> 2 * (i) & 0x3) == BLOCK_FREE)
            return makeBlock(seg, i);
        i = (i + 1) % 4;
    }
    while (i != start);
    return ((blk_t)-1);
}

canonical_original_code:
static unsigned short findFreeBlock(unsigned short seg)
{
    unsigned short start = randomNumber() % 4; unsigned short i = start;
    unsigned char s = readSeg(seg);
    do
    {
        if (((s) >> 2 * (i) & 0x3) == BLOCK_FREE)
            return makeBlock(seg, i);
        i = (i + 1) % 4;
    }
    while (i != start);
    return ((blk_t)-1);
}

variable_types:
start
unsigned short


writeSeg

decompiled_code:
__int64 __fastcall writeSeg(__int16 a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return eepromWrite((unsigned __int16)(a1 + 2), v3, 1LL);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return eepromWrite((unsigned __int16)(a1 + 2), v3, 1LL);
}

original_code:
static inline void writeSeg(segnum_t i, segment_t s)
{
    eepromWrite(sizeof(uint16_t) + i * sizeof(segment_t), &s, sizeof(s));
}

canonical_original_code:
static inline void writeSeg(unsigned short i, unsigned char s)
{
    eepromWrite(sizeof(unsigned short) + i * sizeof(segment_t), &s, sizeof(s));
}


fsBlockAllocate

decompiled_code:
__int64 __fastcall fsBlockAllocate(unsigned __int16 a1)
{
unsigned __int16 v1; // r13
int v2; // er14
__int64 v3; // r12
unsigned __int16 v4; // r15
char v5; // al
__int16 v6; // ax
unsigned __int16 v7; // ax
__int16 v8; // ax
char v9; // al
int v10; // edx
int v11; // esi
__int16 v12; // ax
unsigned int v14; // eax
char v15; // al
unsigned __int16 v16; // ax
unsigned int v17; // eax
char v18; // al
unsigned __int16 v19[29]; // [rsp+Eh] [rbp-3Ah] BYREF

v19[0] = -1;
if ( a1 == 0xFFFF || (v6 = segNum(a1), v7 = findFreeBlock(v6), v19[0] = v7, v7 == 0xFFFF) )
{
v1 = -1;
v2 = 0xFFFF;
v3 = randomNumber() & 0x1F;
v4 = v3;
do
{
v5 = readSeg(v4);
if ( v5 == 85 )
{
v15 = randomNumber();
v7 = makeBlock(v4, v15 & 3);
v19[0] = v7;
goto LABEL_14;
}
if ( (v5 & 0xAA) != 0 )
{
if ( (v5 & 0x55) != 0 )
v1 = v4;
}
else
{
v2 = v4;
}
v4 = ((_BYTE)v4 + 1) & 0x1F;
}
while ( (_WORD)v3 != v4 );
v7 = v19[0];
LABEL_14:
if ( v7 == 0xFFFF )
{
if ( (_WORD)v2 == 0xFFFF )
{
if ( v1 == 0xFFFF )
{
LABEL_19:
fsSetError(3LL);
return v19[0];
}
v7 = findFreeBlock(v1);
v19[0] = v7;
}
else
{
writeSeg(v2, 85);
v16 = makeBlock(v2, 0);
v17 = flashAddr(v16, 0);
blkExtFlashEraseSector(v17);
v18 = randomNumber();
v7 = makeBlock(v2, v18 & 3);
v19[0] = v7;
}
if ( v7 == 0xFFFF )
goto LABEL_19;
}
}
v8 = segNum(v7);
readSeg(v8);
v9 = segOffset(v19[0]);
v11 = (2 << (2 * v9)) | v10 & ~(3 << (2 * v9));
v12 = segNum(v19[0]);
writeSeg(v12, v11);
if ( a1 != 0xFFFF )
{
v14 = flashAddr(a1, 16382);
blkExtFlashWrite_constprop_0(v14, (__int64)v19);
}
return v19[0];
}

original_code:
blk_t fsBlockAllocate(blk_t old)
{
    blk_t res = ((blk_t)-1);
    mutexLock(&fsBlkTableMutex);
    if (old != ((blk_t)-1))
    {
        res = findFreeBlock(segNum(old));
    }
    if (res == ((blk_t)-1))
    {
        segnum_t start = randomNumber() % 32,
                 i = start,
                 avail = ((blk_t)-1), partial = ((blk_t)-1);
        do
        {
            segment_t s = readSeg(i);
            if (s == 0x55)
            {
                res = makeBlock(i, randomNumber() % 4);
                break;
            }
            else if (!(s & 0xAA))
            {
                avail = i;
            }
            else if (s & 0x55)
            {
                partial = i;
            }
            i = (i + 1) % 32;
        }
        while (i != start);
        if (res == ((blk_t)-1))
        {
            if (avail != ((blk_t)-1))
            {
                writeSeg(avail, 0x55);
                blkExtFlashEraseSector(flashAddr(makeBlock(avail, 0), 0));
                res = makeBlock(avail, randomNumber() % 4);
            }
            else if (partial != ((blk_t)-1))
            {
                res = findFreeBlock(partial);
            }
        }
    }
    if (res == ((blk_t)-1))
        fsSetError(FS_ERR_NOSPC);
    else
    {
        segment_t s = readSeg(segNum(res));
        ((s) = ((s) & ~((segment_t)0x3 << 2 * (segOffset(res)))) | (segment_t)(BLOCK_USED) << 2 * (segOffset(res)));
        writeSeg(segNum(res), s);
        if (old != ((blk_t)-1))
        {
            blkExtFlashWrite(flashAddr(old, (0x10000 / 4) - sizeof(blk_t)),
                             &res, sizeof(blk_t));
        }
    }
    mutexUnlock(&fsBlkTableMutex);
    return res;
}

canonical_original_code:
unsigned short fsBlockAllocate(unsigned short old)
{
    unsigned short res = ((blk_t)-1);
    mutexLock(&fsBlkTableMutex);
    if (old != ((blk_t)-1))
    {
        res = findFreeBlock(segNum(old));
    }
    if (res == ((blk_t)-1))
    {
        unsigned short start = randomNumber() % 32; unsigned short i = start; unsigned short avail = ((blk_t)-1); unsigned short partial = ((blk_t)-1);
        do
        {
            unsigned char s = readSeg(i);
            if (s == 0x55)
            {
                res = makeBlock(i, randomNumber() % 4);
                break;
            }
            else if (!(s & 0xAA))
            {
                avail = i;
            }
            else if (s & 0x55)
            {
                partial = i;
            }
            i = (i + 1) % 32;
        }
        while (i != start);
        if (res == ((blk_t)-1))
        {
            if (avail != ((blk_t)-1))
            {
                writeSeg(avail, 0x55);
                blkExtFlashEraseSector(flashAddr(makeBlock(avail, 0), 0));
                res = makeBlock(avail, randomNumber() % 4);
            }
            else if (partial != ((blk_t)-1))
            {
                res = findFreeBlock(partial);
            }
        }
    }
    if (res == ((blk_t)-1))
        fsSetError(FS_ERR_NOSPC);
    else
    {
        unsigned char s = readSeg(segNum(res));
        ((s) = ((s) & ~((unsigned char)0x3 << 2 * (segOffset(res)))) | (segment_t)(BLOCK_USED) << 2 * (segOffset(res)));
        writeSeg(segNum(res), s);
        if (old != ((blk_t)-1))
        {
            blkExtFlashWrite(flashAddr(old, (0x10000 / 4) - sizeof(blk_t)),
                             &res, sizeof(blk_t));
        }
    }
    mutexUnlock(&fsBlkTableMutex);
    return res;
}

variable_types:
old
unsigned short

variable_types:
res
unsigned short

variable_types:
avail
unsigned short

variable_types:
partial
unsigned short


fsBlockGetNext

decompiled_code:
__int64 __fastcall fsBlockGetNext(unsigned __int16 a1)
{
unsigned int v1; // eax
unsigned __int16 v3[5]; // [rsp+Eh] [rbp-Ah] BYREF

v1 = flashAddr(a1, 16382);
blkExtFlashRead_constprop_0(v1, (__int64)v3);
return v3[0];
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int16 a1)
{
unsigned int v1; // eax
unsigned __int16 v3[5]; // [rsp+Eh] [rbp-Ah] BYREF

v1 = func0(a1, 16382);
func11(v1, (__int64)v3);
return v3[0];
}

original_code:
blk_t fsBlockGetNext(blk_t prev)
{
    blk_t res;
    blkExtFlashRead(flashAddr(prev, (0x10000 / 4) - sizeof(blk_t)),
                    &res, sizeof(blk_t));
    return res;
}

canonical_original_code:
unsigned short fsBlockGetNext(unsigned short prev)
{
    unsigned short res;
    blkExtFlashRead(flashAddr(prev, (0x10000 / 4) - sizeof(blk_t)),
                    &res, sizeof(blk_t));
    return res;
}

variable_types:
prev
unsigned short


fsBlockFree

decompiled_code:
__int64 __fastcall fsBlockFree(__int16 a1)
{
__int16 v1; // bp
char v2; // al
char v3; // dl

v1 = segNum(a1);
readSeg(v1);
v2 = segOffset(a1);
return writeSeg(v1, v3 & (unsigned __int8)~(3 << (2 * v2)));
}

canonical_decompiled_code:
__int64 __fastcall func14(__int16 a1)
{
__int16 v1; // bp
char v2; // al
char v3; // dl

v1 = func1(a1);
func7(v1);
v2 = func2(a1);
return func9(v1, v3 & (unsigned __int8)~(3 << (2 * v2)));
}

original_code:
void fsBlockFree(blk_t block)
{
    segment_t s;
    mutexLock(&fsBlkTableMutex);
    s = readSeg(segNum(block));
    ((s) = ((s) & ~((segment_t)0x3 << 2 * (segOffset(block)))) | (segment_t)(BLOCK_AVAIL) << 2 * (segOffset(block)));
    writeSeg(segNum(block), s);
    mutexUnlock(&fsBlkTableMutex);
}

canonical_original_code:
void fsBlockFree(unsigned short block)
{
    unsigned char s;
    mutexLock(&fsBlkTableMutex);
    s = readSeg(segNum(block));
    ((s) = ((s) & ~((unsigned char)0x3 << 2 * (segOffset(block)))) | (segment_t)(BLOCK_AVAIL) << 2 * (segOffset(block)));
    writeSeg(segNum(block), s);
    mutexUnlock(&fsBlkTableMutex);
}


fsBlockFreeAll

decompiled_code:
__int64 fsBlockFreeAll()
{
int i; // ebx
__int16 v1; // di
__int64 result; // rax

for ( i = 0; i != 32; ++i )
{
v1 = i;
result = writeSeg(v1, 0);
}
return result;
}

canonical_decompiled_code:
__int64 func15()
{
int i; // ebx
__int16 v1; // di
__int64 result; // rax

for ( i = 0; i != 32; ++i )
{
v1 = i;
result = func9(v1, 0);
}
return result;
}

original_code:
void fsBlockFreeAll(void)
{
    segnum_t i;
    for (i = 0; i < 32; i++)
        writeSeg(i, 0x0);
}

canonical_original_code:
void fsBlockFreeAll()
{
    unsigned short i;
    for (i = 0; i < 32; i++)
        writeSeg(i, 0x0);
}


parseGPSGetChar

decompiled_code:
__int64 __fastcall parseGPSGetChar(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er8

v2 = 0;
if ( *a2 )
{
--*a2;
v2 = *(unsigned __int8 *)(*a1)++;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er8

v2 = 0;
if ( *a2 )
{
--*a2;
v2 = *(unsigned __int8 *)(*a1)++;
}
return v2;
}

original_code:
uint8_t parseGPSGetChar(const uint8_t **buf, uint_t *len)
{
    if (*len == 0) return 0;
    --(*len);
    const uint8_t ch = **buf;
    ++(*buf);
    return ch;
}

canonical_original_code:
unsigned char parseGPSGetChar(unsigned char **buf, unsigned int *len)
{
    if (*len == 0) return 0;
    --(*len);
    unsigned char ch = **buf;
    ++(*buf);
    return ch;
}

variable_types:
buf
unsigned char **

variable_types:
len
unsigned int *

variable_types:
ch
unsigned char


decompiled_code:
__int64 __fastcall parseGPSu32(_QWORD *a1, _DWORD *a2, _DWORD *a3, _WORD *a4)
{
unsigned __int8 v4; // al
int v5; // ecx
_DWORD *v6; // r9
_WORD *v7; // r10
__int64 result; // rax

*a3 = 0;
if ( a4 )
*a4 = 0;
LABEL_3:
while ( 2 )
{
v4 = parseGPSGetChar(a1, a2);
if ( !v4 )
return 0LL;
while ( (unsigned __int8)(v4 - 48) <= 9u )
{
if ( !v5 )
{
if ( v7 )
*v7 = 10 * *v7 - 48 + v4;
goto LABEL_3;
}
*v6 = 10 * *v6 - 48 + v4;
v4 = parseGPSGetChar(a1, a2);
if ( !v4 )
return 0LL;
}
if ( v4 == 46 )
{
if ( v5 )
continue;
*v6 = -1;
result = 0xFFFFFFFFLL;
}
else
{
if ( v4 != 44 )
continue;
result = 0LL;
}
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, _DWORD *a2, _DWORD *a3, _WORD *a4)
{
unsigned __int8 v4; // al
int v5; // ecx
_DWORD *v6; // r9
_WORD *v7; // r10
__int64 result; // rax

*a3 = 0;
if ( a4 )
*a4 = 0;
LABEL_3:
while ( 2 )
{
v4 = func0(a1, a2);
if ( !v4 )
return 0LL;
while ( (unsigned __int8)(v4 - 48) <= 9u )
{
if ( !v5 )
{
if ( v7 )
*v7 = 10 * *v7 - 48 + v4;
goto LABEL_3;
}
*v6 = 10 * *v6 - 48 + v4;
v4 = func0(a1, a2);
if ( !v4 )
return 0LL;
}
if ( v4 == 46 )
{
if ( v5 )
continue;
*v6 = -1;
result = 0xFFFFFFFFLL;
}
else
{
if ( v4 != 44 )
continue;
result = 0LL;
}
return result;
}
}

original_code:
uint_t parseGPSu32(const uint8_t **buf, uint_t *len, uint32_t *u32, uint16_t *dec)
{
    uint_t mainPart = 1;
    *u32 = 0;
    if (dec) *dec = 0;
    uint8_t ch = 0;
    while ((ch = parseGPSGetChar(buf, len)) != 0) {
        if (ch >= '0' && ch <= '9') {
            if (mainPart) {
                *u32 = ((*u32 << 3) + (*u32 << 1)) + (ch - '0');
            } else {
                if (dec) *dec = ((*dec << 3) + (*dec << 1)) + (ch - '0');
            }
        } else if (ch == '.') {
            if (mainPart) {
                mainPart = 0;
            } else {
                *u32 = -1u;
                return -1;
            }
        } else if (ch == ',') {
            break;
        }
    }
    return 0;
}

canonical_original_code:
unsigned int parseGPSu32(unsigned char **buf, unsigned int *len, unsigned int *u32, unsigned short *dec)
{
    unsigned int mainPart = 1;
    *u32 = 0;
    if (dec) *dec = 0;
    unsigned char ch = 0;
    while ((ch = parseGPSGetChar(buf, len)) != 0) {
        if (ch >= '0' && ch <= '9') {
            if (mainPart) {
                *u32 = ((*u32 << 3) + (*u32 << 1)) + (ch - '0');
            } else {
                if (dec) *dec = ((*dec << 3) + (*dec << 1)) + (ch - '0');
            }
        } else if (ch == '.') {
            if (mainPart) {
                mainPart = 0;
            } else {
                *u32 = -1u;
                return -1;
            }
        } else if (ch == ',') {
            break;
        }
    }
    return 0;
}

variable_types:
dec
unsigned short *

variable_types:
mainPart
unsigned int


parseGPSTime

decompiled_code:
__int64 __fastcall parseGPSTime(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er8
_BYTE *v3; // r11
unsigned int v4; // eax
__int16 v6; // [rsp+Ah] [rbp-6h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-4h] BYREF

v2 = parseGPSu32(a1, a2, &v7, &v6);
if ( !v2 )
{
v4 = v7 / 0x64;
v3[4] = v7 % 0x64;
v3[2] = v4 / 0x64;
v3[3] = v4 % 0x64;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er8
_BYTE *v3; // r11
unsigned int v4; // eax
__int16 v6; // [rsp+Ah] [rbp-6h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-4h] BYREF

v2 = func1(a1, a2, &v7, &v6);
if ( !v2 )
{
v4 = v7 / 0x64;
v3[4] = v7 % 0x64;
v3[2] = v4 / 0x64;
v3[3] = v4 % 0x64;
}
return v2;
}

original_code:
uint_t parseGPSTime(const uint8_t **buf, uint_t *len, GPSFix_t *fix)
{
    uint32_t t;
    uint16_t dec;
    uint_t res = parseGPSu32(buf, len, &t, &dec);
    if (res) return res;
    fix->s = t % 100;
    t /= 100;
    fix->m = t % 100;
    fix->h = t / 100;
    return 0;
}

canonical_original_code:
unsigned int parseGPSTime(unsigned char **buf, unsigned int *len, struct GPSFix_t *fix)
{
    unsigned int t;
    unsigned short dec;
    unsigned int res = parseGPSu32(buf, len, &t, &dec);
    if (res) return res;
    fix->s = t % 100;
    t /= 100;
    fix->m = t % 100;
    fix->h = t / 100;
    return 0;
}

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

variable_types:
t
unsigned int

variable_types:
dec
unsigned short

variable_types:
res
unsigned int

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


parseGPSSatCnt

decompiled_code:
__int64 __fastcall parseGPSSatCnt(_QWORD *a1, _DWORD *a2)
{
__int64 v2; // r11
__int64 result; // rax
unsigned int v4; // [rsp+Ch] [rbp-4h] BYREF

parseGPSu32(a1, a2, &v4, 0LL);
result = v4;
if ( v4 != -1 )
{
if ( v4 <= 8 )
{
if ( v4 > 6 )
{
*(_BYTE *)(v2 + 5) = *(_BYTE *)(v2 + 5) & 0x1F | 0xC0;
result = 0LL;
}
else
{
if ( v4 <= 4 )
*(_BYTE *)(v2 + 5) = *(_BYTE *)(v2 + 5) & 0x1F | (32 * v4);
else
*(_BYTE *)(v2 + 5) = *(_BYTE *)(v2 + 5) & 0x1F | 0xA0;
result = 0LL;
}
}
else
{
*(_BYTE *)(v2 + 5) |= 0xE0u;
result = 0LL;
}
}
return result;
}

original_code:
uint_t parseGPSSatCnt(const uint8_t **buf, uint_t *len, GPSFix_t *fix)
{
    uint32_t satcnt;
    parseGPSu32(buf, len, &satcnt, 0);
    if (satcnt != -1u) {
        if (satcnt > 8) {
            fix->q.satcnt = SAT_COUNT_OVER_8;
        } else if (satcnt > 6) {
            fix->q.satcnt = SAT_COUNT_7_8;
        } else if (satcnt > 4) {
            fix->q.satcnt = SAT_COUNT_5_6;
        } else {
            fix->q.satcnt = satcnt;
        }
        return 0;
    } else {
        return -1;
    }
}

canonical_original_code:
unsigned int parseGPSSatCnt(unsigned char **buf, unsigned int *len, struct GPSFix_t *fix)
{
    unsigned int satcnt;
    parseGPSu32(buf, len, &satcnt, 0);
    if (satcnt != -1u) {
        if (satcnt > 8) {
            fix->q.satcnt = SAT_COUNT_OVER_8;
        } else if (satcnt > 6) {
            fix->q.satcnt = SAT_COUNT_7_8;
        } else if (satcnt > 4) {
            fix->q.satcnt = SAT_COUNT_5_6;
        } else {
            fix->q.satcnt = satcnt;
        }
        return 0;
    } else {
        return -1;
    }
}

variable_types:
satcnt
unsigned int

user_defined_types: struct GPSDate_t { unsigned short day; }

user_defined_types: struct GPSFixQuality_t { unsigned char fix; }

user_defined_types: struct GPSPos_t { unsigned char deg; unsigned char min; unsigned short mindec; }


parseGPSDOP

original_code:
uint_t parseGPSDOP(const uint8_t **buf, uint_t *len, GPSFix_t *fix)
{
    uint32_t dop;
    uint16_t dopdec;
    parseGPSu32(buf, len, &dop, &dopdec);
    if (dop != -1u) {
        if (dop == 1) {
            if (dopdec == 0) {
                fix->q.dop = DOP_IDEAL;
            } else {
                fix->q.dop = DOP_EXCELLENT;
            }
        } else if (dop == 0) {
            fix->q.dop = DOP_POOR;
            fix->q.fix = FT_NO_FIX;
        } else if (dop < 5) {
            fix->q.dop = DOP_GOOD;
        } else if (dop < 10) {
            fix->q.dop = DOP_MODERATE;
        } else if (dop < 20) {
            fix->q.dop = DOP_FAIR;
        } else {
            fix->q.dop = DOP_POOR;
        }
        return 0;
    } else {
        fix->q.dop = -1;
        return -1;
    }
}

canonical_original_code:
unsigned int parseGPSDOP(unsigned char **buf, unsigned int *len, struct GPSFix_t *fix)
{
    unsigned int dop;
    unsigned short dopdec;
    parseGPSu32(buf, len, &dop, &dopdec);
    if (dop != -1u) {
        if (dop == 1) {
            if (dopdec == 0) {
                fix->q.dop = DOP_IDEAL;
            } else {
                fix->q.dop = DOP_EXCELLENT;
            }
        } else if (dop == 0) {
            fix->q.dop = DOP_POOR;
            fix->q.fix = FT_NO_FIX;
        } else if (dop < 5) {
            fix->q.dop = DOP_GOOD;
        } else if (dop < 10) {
            fix->q.dop = DOP_MODERATE;
        } else if (dop < 20) {
            fix->q.dop = DOP_FAIR;
        } else {
            fix->q.dop = DOP_POOR;
        }
        return 0;
    } else {
        fix->q.dop = -1;
        return -1;
    }
}

variable_types:
dop
unsigned int

variable_types:
dopdec
unsigned short


parseGPSFixQual

decompiled_code:
__int64 __fastcall parseGPSFixQual(_QWORD *a1, _DWORD *a2)
{
__int64 v2; // r11
__int64 result; // rax
char v4; // al
unsigned int v5; // [rsp+Ch] [rbp-4h] BYREF

parseGPSu32(a1, a2, &v5, 0LL);
result = v5;
if ( v5 != -1 )
{
if ( v5 == 3 )
{
*(_BYTE *)(v2 + 5) = *(_BYTE *)(v2 + 5) & 0xFC | 2;
result = 0LL;
}
else
{
v4 = *(_BYTE *)(v2 + 5);
if ( v5 == 2 )
{
*(_BYTE *)(v2 + 5) = v4 & 0xFC | 1;
result = 0LL;
}
else
{
*(_BYTE *)(v2 + 5) = v4 & 0xFC;
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, _DWORD *a2)
{
__int64 v2; // r11
__int64 result; // rax
char v4; // al
unsigned int v5; // [rsp+Ch] [rbp-4h] BYREF

func1(a1, a2, &v5, 0LL);
result = v5;
if ( v5 != -1 )
{
if ( v5 == 3 )
{
*(_BYTE *)(v2 + 5) = *(_BYTE *)(v2 + 5) & 0xFC | 2;
result = 0LL;
}
else
{
v4 = *(_BYTE *)(v2 + 5);
if ( v5 == 2 )
{
*(_BYTE *)(v2 + 5) = v4 & 0xFC | 1;
result = 0LL;
}
else
{
*(_BYTE *)(v2 + 5) = v4 & 0xFC;
result = 0LL;
}
}
}
return result;
}

original_code:
uint_t parseGPSFixQual(const uint8_t **buf, uint_t *len, GPSFix_t *fix)
{
    uint32_t fixq;
    parseGPSu32(buf, len, &fixq, 0);
    if (fixq != -1u) {
        if (fixq == 3) {
            fix->q.fix = FT_3D_FIX;
        } else if (fixq == 2) {
            fix->q.fix = FT_2D_FIX;
        } else {
            fix->q.fix = FT_NO_FIX;
        }
        return 0;
    } else {
        return -1;
    }
}

canonical_original_code:
unsigned int parseGPSFixQual(unsigned char **buf, unsigned int *len, struct GPSFix_t *fix)
{
    unsigned int fixq;
    parseGPSu32(buf, len, &fixq, 0);
    if (fixq != -1u) {
        if (fixq == 3) {
            fix->q.fix = FT_3D_FIX;
        } else if (fixq == 2) {
            fix->q.fix = FT_2D_FIX;
        } else {
            fix->q.fix = FT_NO_FIX;
        }
        return 0;
    } else {
        return -1;
    }
}

variable_types:
fixq
unsigned int


parseGPSDate

original_code:
uint_t parseGPSDate(const uint8_t **buf, uint_t *len, GPSFix_t *fix)
{
    uint32_t d;
    uint_t res = parseGPSu32(buf, len, &d, 0);
    if (res) return res;
    fix->d.year = d % 100;
    d /= 100;
    fix->d.mon = d % 100;
    fix->d.day = d / 100;
    return 0;
}

canonical_original_code:
unsigned int parseGPSDate(unsigned char **buf, unsigned int *len, struct GPSFix_t *fix)
{
    unsigned int d;
    unsigned int res = parseGPSu32(buf, len, &d, 0);
    if (res) return res;
    fix->d.year = d % 100;
    d /= 100;
    fix->d.mon = d % 100;
    fix->d.day = d / 100;
    return 0;
}

variable_types:
d
unsigned int


parseGPSChar

decompiled_code:
__int64 __fastcall parseGPSChar(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er9

parseGPSGetChar(a1, a2);
parseGPSGetChar(a1, a2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1, _DWORD *a2)
{
unsigned int v2; // er9

func0(a1, a2);
func0(a1, a2);
return v2;
}

original_code:
uint8_t parseGPSChar(const uint8_t **buf, uint_t *len)
{
    uint8_t ch = parseGPSGetChar(buf, len);
    parseGPSGetChar(buf, len);
    return ch;
}

canonical_original_code:
unsigned char parseGPSChar(unsigned char **buf, unsigned int *len)
{
    unsigned char ch = parseGPSGetChar(buf, len);
    parseGPSGetChar(buf, len);
    return ch;
}


parseGPSPos

decompiled_code:
__int64 __fastcall parseGPSPos(_QWORD *a1, _DWORD *a2, __int64 a3)
{
unsigned int v3; // er10
_BYTE *v4; // r11
unsigned int v5; // eax
__int64 v7; // r11
unsigned int v8; // [rsp+Ch] [rbp-4h] BYREF

v3 = parseGPSu32(a1, a2, &v8, (_WORD *)(a3 + 2));
if ( v3 )
return v3;
v5 = v8;
if ( v8 == -1 )
return v3;
*v4 = v8 / 0x64;
v4[1] = v5 % 0x64;
if ( (parseGPSChar(a1, a2) & 0xFB) != 83 )
return v3;
*(_WORD *)(v7 + 2) |= 0x8000u;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, _DWORD *a2, __int64 a3)
{
unsigned int v3; // er10
_BYTE *v4; // r11
unsigned int v5; // eax
__int64 v7; // r11
unsigned int v8; // [rsp+Ch] [rbp-4h] BYREF

v3 = func1(a1, a2, &v8, (_WORD *)(a3 + 2));
if ( v3 )
return v3;
v5 = v8;
if ( v8 == -1 )
return v3;
*v4 = v8 / 0x64;
v4[1] = v5 % 0x64;
if ( (func7(a1, a2) & 0xFB) != 83 )
return v3;
*(_WORD *)(v7 + 2) |= 0x8000u;
return v3;
}

original_code:
uint_t parseGPSPos(const uint8_t **buf, uint_t *len, GPSPos_t *pos)
{
    uint32_t p;
    uint_t res = parseGPSu32(buf, len, &p, &pos->mindec);
    if (res) return res;
    if (p != -1u) {
        pos->min = p % 100;
        pos->deg = p / 100;
        uint8_t ch = parseGPSChar(buf, len);
        if (ch == 'S' || ch == 'W') pos->mindec |= SOUTH_WEST;
    }
    return 0;
}

canonical_original_code:
unsigned int parseGPSPos(unsigned char **buf, unsigned int *len, struct GPSPos_t *pos)
{
    unsigned int p;
    unsigned int res = parseGPSu32(buf, len, &p, &pos->mindec);
    if (res) return res;
    if (p != -1u) {
        pos->min = p % 100;
        pos->deg = p / 100;
        unsigned char ch = parseGPSChar(buf, len);
        if (ch == 'S' || ch == 'W') pos->mindec |= SOUTH_WEST;
    }
    return 0;
}

variable_types: struct GPSPos_t { unsigned char deg; unsigned char min; unsigned short mindec; } *

variable_types:
p
unsigned int


parseGGA

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

v6[0] = a2;
v7[0] = a1;
result = parseGPSTime(v7, v6);
if ( !(_DWORD)result )
{
result = parseGPSPos(v7, v6, a3 + 6);
if ( !(_DWORD)result )
{
result = parseGPSPos(v7, v6, a3 + 10);
if ( !(_DWORD)result )
{
v5 = parseGPSChar(v7, v6);
result = 0xFFFFFFFFLL;
if ( v5 )
{
result = parseGPSSatCnt(v7, v6);
if ( !(_DWORD)result )
result = parseGPSDOP(v7, v6);
}
}
}
}
return result;
}

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

v6[0] = a2;
v7[0] = a1;
result = func2(v7, v6);
if ( !(_DWORD)result )
{
result = func8(v7, v6, a3 + 6);
if ( !(_DWORD)result )
{
result = func8(v7, v6, a3 + 10);
if ( !(_DWORD)result )
{
v5 = func7(v7, v6);
result = 0xFFFFFFFFLL;
if ( v5 )
{
result = func3(v7, v6);
if ( !(_DWORD)result )
result = func4(v7, v6);
}
}
}
}
return result;
}

original_code:
uint_t parseGGA(const void *buf, uint_t len, GPSFix_t *fix)
{
    const uint8_t *p = (uint8_t *) buf;
    uint_t res;
    if ((res = parseGPSTime(&p, &len, fix))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lat))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lon))) return res;
    if (parseGPSChar(&p, &len) == 0) return -1;
    if ((res = parseGPSSatCnt(&p, &len, fix))) return res;
    if ((res = parseGPSDOP(&p, &len, fix))) return res;
    return 0;
}

canonical_original_code:
unsigned int parseGGA(void *buf, unsigned int len, struct GPSFix_t *fix)
{
    unsigned char *p = (uint8_t *) buf;
    unsigned int res;
    if ((res = parseGPSTime(&p, &len, fix))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lat))) return res;
    if ((res = parseGPSPos(&p, &len, &fix->lon))) return res;
    if (parseGPSChar(&p, &len) == 0) return -1;
    if ((res = parseGPSSatCnt(&p, &len, fix))) return res;
    if ((res = parseGPSDOP(&p, &len, fix))) return res;
    return 0;
}

variable_types:
buf
void *

variable_types:
len
unsigned int

variable_types:
p
unsigned char *