wadInit

decompiled_code:
__int64 __fastcall wadInit(char *filename)
{
void *v1; // rax
void *v2; // rbx
FILE *v3; // rcx
__int64 v5; // [rsp+8h] [rbp-30h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h] BYREF
char dest[29]; // [rsp+1Bh] [rbp-1Dh] BYREF

strcpy(dest, "xWAD");
v1 = calloc(1uLL, 5uLL);
if ( !v1 )
{
perror("wadInit");
exit(1);
}
v2 = v1;
v3 = fopen(filename, "r");
wadFile = v3;
if ( !v3 )
{
perror("wadInit");
exit(1);
}
if ( (unsigned int)fread(v2, 4uLL, 1uLL, v3) != 1 )
_assert_fail("result == 1", "wadread.c", 0xCEu, "wadInit");
strncpy(dest, (const char *)v2, 4uLL);
if ( strcmp(dest, "IWAD") )
{
fprintf(stderr, "Error: %s is no valid WAD file\n", filename);
exit(1);
}
if ( wadReadLong(&v6) )
_assert_fail("result == 0", "wadread.c", 0xD7u, "wadInit");
if ( wadReadLong(&v5) )
_assert_fail("result == 0", "wadread.c", 0xD9u, "wadInit");
wadReadDir(v6, v5);
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *filename)
{
void *v1; // rax
void *v2; // rbx
FILE *v3; // rcx
__int64 v5; // [rsp+8h] [rbp-30h] BYREF
__int64 v6; // [rsp+10h] [rbp-28h] BYREF
char dest[29]; // [rsp+1Bh] [rbp-1Dh] BYREF

strcpy(dest, "xWAD");
v1 = calloc(1uLL, 5uLL);
if ( !v1 )
{
perror("wadInit");
exit(1);
}
v2 = v1;
v3 = fopen(filename, "r");
wadFile = v3;
if ( !v3 )
{
perror("wadInit");
exit(1);
}
if ( (unsigned int)fread(v2, 4uLL, 1uLL, v3) != 1 )
_assert_fail("result == 1", "wadread.c", 0xCEu, "wadInit");
strncpy(dest, (const char *)v2, 4uLL);
if ( strcmp(dest, "IWAD") )
{
fprintf(stderr, "Error: %s is no valid WAD file\n", filename);
exit(1);
}
if ( func2(&v6) )
_assert_fail("result == 0", "wadread.c", 0xD7u, "wadInit");
if ( func2(&v5) )
_assert_fail("result == 0", "wadread.c", 0xD9u, "wadInit");
func4(v6, v5);
free(v2);
return 0LL;
}

original_code:
int wadInit(char *wadFileName) {
 char wadHeaderType[5] = "xWAD";
 long wadHeaderNumLumps;
 long wadHeaderDirOffset;
 int result;
 void *buf;
 buf = calloc(1, 5);
 if (buf == ((void *)0)) {
  perror("wadInit");
  exit(1);
 }
 wadFile = fopen(wadFileName, "r");
 if (wadFile == ((void *)0)) {
  perror("wadInit");
  exit(1);
 }
 ((wadFile != ((void *)0)) ? (void) (0) : __assert_fail ("wadFile != NULL", "wadread.c", 202, __extension__ __PRETTY_FUNCTION__));
 result = fread(buf, 4, 1, wadFile);
 ((result == 1) ? (void) (0) : __assert_fail ("result == 1", "wadread.c", 206, __extension__ __PRETTY_FUNCTION__));
 strncpy(wadHeaderType, buf, 4);
 if (strcmp(wadHeaderType, "IWAD") != 0) {
  fprintf(stderr, "Error: %s is no valid WAD file\n", wadFileName);
  exit(1);
 }
 result = wadReadLong(&wadHeaderNumLumps);
 ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 215, __extension__ __PRETTY_FUNCTION__));
 result = wadReadLong(&wadHeaderDirOffset);
 ((result == 0) ? (void) (0) : __assert_fail ("result == 0", "wadread.c", 217, __extension__ __PRETTY_FUNCTION__));
 wadReadDir(wadHeaderNumLumps, wadHeaderDirOffset);
 free(buf);
 return 0;
}

variable_types:
wadFileName
char *

variable_types:
wadHeaderType
char [5]

variable_types: wadHeaderNumLumps long

variable_types: wadHeaderDirOffset long

variable_types:
buf
void *


timeLeft

repo: atextor/demon-defense

decompiled_code:
__int64 timeLeft()
{
unsigned int v0; // eax
unsigned int v1; // ecx

v0 = SDL_GetTicks();
v1 = nextTime_0 - v0;
if ( nextTime_0 <= v0 )
{
nextTime_0 = v0 + 50;
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax
unsigned int v1; // ecx

v0 = SDL_GetTicks();
v1 = nextTime_0 - v0;
if ( nextTime_0 <= v0 )
{
nextTime_0 = v0 + 50;
v1 = 0;
}
return v1;
}

original_code:
static Uint32 timeLeft() {
 static Uint32 nextTime = 0;
 Uint32 now;
 now = SDL_GetTicks();
 if (nextTime <= now) {
  nextTime = now + 50;
  return 0;
 }
 return (nextTime - now);
}

canonical_original_code:
static unsigned int timeLeft() {
 static unsigned int nextTime = 0;
 unsigned int now;
 now = SDL_GetTicks();
 if (nextTime <= now) {
  nextTime = now + 50;
  return 0;
 }
 return (nextTime - now);
}

variable_types:
nextTime
unsigned int

variable_types:
now
unsigned int

return_type: unsigned int


loopDelay

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

v0 = timeLeft();
return SDL_Delay(v0);
}

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

v0 = func0();
return SDL_Delay(v0);
}

original_code:
void loopDelay() {
 SDL_Delay(timeLeft());
}

return_type: void


repo: atextor/icat

decompiled_code:
__int64 __fastcall rgb2xterm(_DWORD *a1)
{
int v1; // er10
int v2; // er9
int v3; // er8
__int64 v4; // rsi
unsigned int v5; // edi
unsigned int v6; // er11
int v7; // eax
unsigned int v8; // eax

v1 = a1[1];
v2 = a1[2];
v3 = a1[3];
v4 = 0LL;
v5 = 1000000000;
v6 = 0;
do
{
v7 = (unsigned __int8)BYTE2(colors[v4]) - v1;
v8 = v7 * v7
+ ((unsigned __int8)BYTE1(colors[v4]) - v2) * ((unsigned __int8)BYTE1(colors[v4]) - v2)
+ ((unsigned __int8)colors[v4] - v3) * ((unsigned __int8)colors[v4] - v3);
if ( v8 < v5 )
{
v5 = v8;
v6 = v4;
}
++v4;
}
while ( v4 != 254 );
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
int v1; // er10
int v2; // er9
int v3; // er8
__int64 v4; // rsi
unsigned int v5; // edi
unsigned int v6; // er11
int v7; // eax
unsigned int v8; // eax

v1 = a1[1];
v2 = a1[2];
v3 = a1[3];
v4 = 0LL;
v5 = 1000000000;
v6 = 0;
do
{
v7 = (unsigned __int8)BYTE2(colors[v4]) - v1;
v8 = v7 * v7
+ ((unsigned __int8)BYTE1(colors[v4]) - v2) * ((unsigned __int8)BYTE1(colors[v4]) - v2)
+ ((unsigned __int8)colors[v4] - v3) * ((unsigned __int8)colors[v4] - v3);
if ( v8 < v5 )
{
v5 = v8;
v6 = v4;
}
++v4;
}
while ( v4 != 254 );
return v6;
}

original_code:
uint8_t rgb2xterm(Imlib_Color* pixel) {
 uint8_t c, match = 0;
 uint32_t r, g, b, d, distance;
 distance = 1000000000;
 for(c = 0; c <= 253; c++) {
  r = ((0xff0000 & colors[c]) >> 16) - pixel->red;
  g = ((0x00ff00 & colors[c]) >> 8) - pixel->green;
  b = (0x0000ff & colors[c]) - pixel->blue;
  d = r * r + g * g + b * b;
  if (d < distance) {
   distance = d;
   match = c;
  }
 }
 return match;
}

canonical_original_code:
unsigned char rgb2xterm(struct _imlib_color *pixel) {
 unsigned char c; unsigned char match = 0;
 unsigned int r; unsigned int g; unsigned int b; unsigned int d; unsigned int distance;
 distance = 1000000000;
 for(c = 0; c <= 253; c++) {
  r = ((0xff0000 & colors[c]) >> 16) - pixel->red;
  g = ((0x00ff00 & colors[c]) >> 8) - pixel->green;
  b = (0x0000ff & colors[c]) - pixel->blue;
  d = r * r + g * g + b * b;
  if (d < distance) {
   distance = d;
   match = c;
  }
 }
 return match;
}

variable_types:
pixel
struct _imlib_color { int alpha; } *

variable_types:
c
unsigned char

variable_types:
match
unsigned char

variable_types:
r
unsigned int

variable_types:
g
unsigned int

variable_types:
b
unsigned int

variable_types:
d
unsigned int

variable_types:
distance
unsigned int

return_type: unsigned char


decompiled_code:
int print_usage()
{
return puts(
"icat (0.5) outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
"Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
" [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
"\t-h | --help -- Display this message\n"
"\t-x value -- Specify the column to print the image in (min. 1)\n"
"\t-y value -- Specify the row to print the image in (min. 1)\n"
"\t This is ignored when more than one image is printed.\n"
"\t-w | --width value\n"
"\t Instead of resizing the image to fit the terminal width,\n"
"\t use the provided value as the desired width.\n"
"\t-k | --keep -- Keep image size, i.e. do not automatically resize image to fit\n"
"\t the terminal width.\n"
"\t-m | --mode indexed|24bit|both\n"
"\t Use indexed (256-color, the default), 24-bit color, or both.\n"
"\timagefile -- The image to print. If the file name is \"-\", the file is\n"
"\t read from stdin.\n"
"Big images are automatically resized to your terminal width, unless with the -k option.\n"
"You can achieve the same effect with convert (from the ImageMagick package):\n"
"\tconvert -resize $((COLUMNS - 2))x image.png - | icat -\n"
"Or read images from another source:\n"
"\tcurl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -");
}

canonical_decompiled_code:
int func8()
{
return puts(
"icat (0.5) outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
"Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
" [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
"\t-h | --help -- Display this message\n"
"\t-x value -- Specify the column to print the image in (min. 1)\n"
"\t-y value -- Specify the row to print the image in (min. 1)\n"
"\t This is ignored when more than one image is printed.\n"
"\t-w | --width value\n"
"\t Instead of resizing the image to fit the terminal width,\n"
"\t use the provided value as the desired width.\n"
"\t-k | --keep -- Keep image size, i.e. do not automatically resize image to fit\n"
"\t the terminal width.\n"
"\t-m | --mode indexed|24bit|both\n"
"\t Use indexed (256-color, the default), 24-bit color, or both.\n"
"\timagefile -- The image to print. If the file name is \"-\", the file is\n"
"\t read from stdin.\n"
"Big images are automatically resized to your terminal width, unless with the -k option.\n"
"You can achieve the same effect with convert (from the ImageMagick package):\n"
"\tconvert -resize $((COLUMNS - 2))x image.png - | icat -\n"
"Or read images from another source:\n"
"\tcurl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -");
}

original_code:
void print_usage() {
 printf("icat (" "0.5" ") outputs an image on a 256-color or 24-bit color enabled terminal with UTF-8 locale.\n"
   "Usage: icat [-h|--help] [-x value] [-y value] [-w|--width value] [-k|--keep]\n"
   "            [-m|--mode indexed|24bit|both] imagefile [imagefile...]\n"
   "	-h | --help  -- Display this message\n"
   "	-x value     -- Specify the column to print the image in (min. 1)\n"
   "	-y value     -- Specify the row to print the image in (min. 1)\n"
   "	                This is ignored when more than one image is printed.\n"
   "	-w | --width value\n"
   "	                Instead of resizing the image to fit the terminal width,\n"
   "	                use the provided value as the desired width.\n"
   "	-k | --keep  -- Keep image size, i.e. do not automatically resize image to fit\n"
   "	                the terminal width.\n"
   "	-m | --mode indexed|24bit|both\n"
   "	                Use indexed (256-color, the default), 24-bit color, or both.\n"
   "	imagefile    -- The image to print. If the file name is \"-\", the file is\n"
   "	                read from stdin.\n"
   "Big images are automatically resized to your terminal width, unless with the -k option.\n"
   "You can achieve the same effect with convert (from the ImageMagick package):\n"
   "	convert -resize $((COLUMNS - 2))x image.png - | icat -\n"
   "Or read images from another source:\n"
   "	curl -sL http://example.com/image.png | convert -resize $((COLUMNS - 2))x - - | icat -\n");
}


decompiled_code:
__int64 terminal_width()
{
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

ioctl(0, 0x5413uLL, v1);
return v2;
}

canonical_decompiled_code:
__int64 func9()
{
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

ioctl(0, 0x5413uLL, v1);
return v2;
}

original_code:
int terminal_width() {
 int cols = 80;
 struct winsize ts;
 ioctl(0, 0x5413, &ts);
 cols = ts.ws_col;
 return cols;
}

variable_types: cols int

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

return_type: int


decompiled_code:
__int64 __fastcall resize_image_if_necessary(int *a1, int *a2, int a3)
{
int v3; // ebx
__int64 result; // rax
int v5; // er14

v3 = a3;
if ( a3 || (result = terminal_width(), v3 = result, *a1 >= (int)result) )
{
v5 = (int)(float)((float)((float)v3 / (float)*a1) * (float)*a2);
imlib_create_cropped_scaled_image();
imlib_free_image_and_decache();
result = imlib_context_set_image();
*a1 = v3;
*a2 = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int *a1, int *a2, int a3)
{
int v3; // ebx
__int64 result; // rax
int v5; // er14

v3 = a3;
if ( a3 || (result = func9(), v3 = result, *a1 >= (int)result) )
{
v5 = (int)(float)((float)((float)v3 / (float)*a1) * (float)*a2);
imlib_create_cropped_scaled_image();
imlib_free_image_and_decache();
result = imlib_context_set_image();
*a1 = v3;
*a2 = v5;
}
return result;
}

original_code:
void resize_image_if_necessary(int *width, int *height, const int user_width) {
 int resized_width;
 if (!user_width) {
  int cols = terminal_width();
  if(cols > *width) return;
  resized_width = cols;
 } else {
  resized_width = user_width;
 }
 float ratio = ((float)resized_width) / *width;
 int resized_height = *height * ratio;
 Imlib_Image resized_image = imlib_create_cropped_scaled_image(0, 0,
   *width, *height, resized_width, resized_height);
 imlib_free_image_and_decache();
 imlib_context_set_image(resized_image);
 *width = resized_width;
 *height = resized_height;
}

canonical_original_code:
void resize_image_if_necessary(int *width, int *height, int user_width) {
 int resized_width;
 if (!user_width) {
  int cols = terminal_width();
  if(cols > *width) return;
  resized_width = cols;
 } else {
  resized_width = user_width;
 }
 float ratio = ((float)resized_width) / *width;
 int resized_height = *height * ratio;
 void *resized_image = imlib_create_cropped_scaled_image(0, 0,
   *width, *height, resized_width, resized_height);
 imlib_free_image_and_decache();
 imlib_context_set_image(resized_image);
 *width = resized_width;
 *height = resized_height;
}

variable_types:
width
int *

variable_types:
height
int *

variable_types: ratio float


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

if ( *a1 )
{
if ( *a2 )
{
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 )
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]);
}
else
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v6 = rgb2xterm(a1);
printf("\x1B[38;5;%dm", v6);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a1[1], (unsigned int)a1[2], (unsigned int)a1[3]);
}
result = fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
else if ( *a2 )
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v5 = rgb2xterm(a2);
printf("\x1B[38;5;%dm", v5);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a2[1], (unsigned int)a2[2], (unsigned int)a2[3]);
result = fwrite(&unk_4025DF, 1uLL, 3uLL, stdout);
}
else
{
result = printf("\x1B[0m ");
}
return result;
}

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

if ( *a1 )
{
if ( *a2 )
{
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 )
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]);
}
else
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v6 = func7(a1);
printf("\x1B[38;5;%dm", v6);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a1[1], (unsigned int)a1[2], (unsigned int)a1[3]);
}
result = fwrite(&unk_4025E3, 1uLL, 3uLL, stdout);
}
else if ( *a2 )
{
fwrite("\x1B[0m", 1uLL, 4uLL, stdout);
if ( (a3 & 1) != 0 )
{
v5 = func7(a2);
printf("\x1B[38;5;%dm", v5);
}
if ( (a3 & 2) != 0 )
printf("\x1B[38;2;%d;%d;%dm", (unsigned int)a2[1], (unsigned int)a2[2], (unsigned int)a2[3]);
result = fwrite(&unk_4025DF, 1uLL, 3uLL, stdout);
}
else
{
result = printf("\x1B[0m ");
}
return result;
}

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 *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // r15
int v5; // ebx
char *v6; // rbx
int j; // ebx
int i; // ebp
const char **v10; // [rsp+10h] [rbp-88h]
int v11; // [rsp+28h] [rbp-70h] BYREF
int v12; // [rsp+2Ch] [rbp-6Ch] BYREF
char templatea[16]; // [rsp+30h] [rbp-68h] BYREF
int v14[4]; // [rsp+40h] [rbp-58h] BYREF
_DWORD buf[18]; // [rsp+50h] [rbp-48h] BYREF

while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "hx:y:w:km:", &long_options_1, 0LL);
if ( v3 == -1 )
break;
if ( v3 != 63 )
display_help_0 = 1;
}
if ( display_help_0 == 1 || _bss_start == argc )
{
print_usage();
exit(0);
}
if ( _bss_start < argc )
{
v4 = &argv[_bss_start];
v10 = &(&argv[_bss_start])[(unsigned __int64)(unsigned int)(argc + ~_bss_start) + 1];
do
{
strcpy(templatea, "/tmp/icatXXXXXX");
v6 = (char *)*v4;
if ( !strcmp(*v4, "-") )
{
v5 = mkstemp(templatea);
if ( v5 < 0 )
{
perror("mkstemp");
exit(1);
}
while ( read(0, buf, 1uLL) > 0 )
write(v5, buf, 1uLL);
v6 = templatea;
}
if ( !imlib_load_image_immediately() )
{
fprintf(stderr, "Could not load image: %s\n", v6);
exit(1);
}
imlib_context_set_image();
v11 = imlib_image_get_width();
v12 = imlib_image_get_height();
resize_image_if_necessary(&v11, &v12, 0);
if ( v12 > 0 )
{
for ( i = 1; ; i += 2 )
{
if ( v11 > 0 )
{
for ( j = 0; j < v11; ++j )
{
imlib_image_query_pixel();
imlib_image_query_pixel();
print_pixels(v14, buf, 1);
}
}
puts("\x1B[0m");
if ( v12 <= i + 1 )
break;
}
}
imlib_free_image_and_decache();
++v4;
}
while ( v4 != v10 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char **v4; // r15
int v5; // ebx
char *v6; // rbx
int j; // ebx
int i; // ebp
const char **v10; // [rsp+10h] [rbp-88h]
int v11; // [rsp+28h] [rbp-70h] BYREF
int v12; // [rsp+2Ch] [rbp-6Ch] BYREF
char templatea[16]; // [rsp+30h] [rbp-68h] BYREF
int v14[4]; // [rsp+40h] [rbp-58h] BYREF
_DWORD buf[18]; // [rsp+50h] [rbp-48h] BYREF

while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "hx:y:w:km:", &long_options_1, 0LL);
if ( v3 == -1 )
break;
if ( v3 != 63 )
display_help_0 = 1;
}
if ( display_help_0 == 1 || _bss_start == argc )
{
func8();
exit(0);
}
if ( _bss_start < argc )
{
v4 = &argv[_bss_start];
v10 = &(&argv[_bss_start])[(unsigned __int64)(unsigned int)(argc + ~_bss_start) + 1];
do
{
strcpy(templatea, "/tmp/icatXXXXXX");
v6 = (char *)*v4;
if ( !strcmp(*v4, "-") )
{
v5 = mkstemp(templatea);
if ( v5 < 0 )
{
perror("mkstemp");
exit(1);
}
while ( read(0, buf, 1uLL) > 0 )
write(v5, buf, 1uLL);
v6 = templatea;
}
if ( !imlib_load_image_immediately() )
{
fprintf(stderr, "Could not load image: %s\n", v6);
exit(1);
}
imlib_context_set_image();
v11 = imlib_image_get_width();
v12 = imlib_image_get_height();
func10(&v11, &v12, 0);
if ( v12 > 0 )
{
for ( i = 1; ; i += 2 )
{
if ( v11 > 0 )
{
for ( j = 0; j < v11; ++j )
{
imlib_image_query_pixel();
imlib_image_query_pixel();
func11(v14, buf, 1);
}
}
puts("\x1B[0m");
if ( v12 <= i + 1 )
break;
}
}
imlib_free_image_and_decache();
++v4;
}
while ( v4 != v10 );
}
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 char *filename;
 static int display_help = 0;
 Imlib_Image image = ((void *)0);
 unsigned int x = 0;
 unsigned int y = 0;
 unsigned int user_w = 0;
 int c;
 _Bool keep_size = 0;
 int mode = MODE_INDEXED;
 for(;;) {
  static struct option long_options[] = {
   {"help", 0, &display_help, 1},
   {"x", 1, 0, 'x'},
   {"y", 1, 0, 'y'},
   {"width",1, 0, 'w'},
   {"keep", 0, 0, 'k'},
   {"mode", 1, 0, 'm'},
   {0, 0, 0, 0}
  };
  c = getopt_long(argc, argv, "hx:y:w:km:", long_options, ((void *)0));
  if (c == -1)
   break;
  switch (c) {
   case 'x':
    x = atoi(optarg);
    if (x < 0) {
     x = 0;
    }
    break;
   case 'y':
    y = atoi(optarg);
    if (y < 0) {
     y = 0;
    }
    break;
   case 'w':
    user_w = atoi(optarg);
    if(user_w < 1) {
     printf("Image width must be larger than 0\n");
     exit(1);
    }
    break;
   case 'h':
    display_help = 1;
    break;
   case 'k':
    keep_size = 1;
    break;
   case 'm':
    if(strcmp(optarg, "indexed") == 0) {
     mode = MODE_INDEXED;
    } else if(strcmp(optarg, "24bit") == 0) {
     mode = MODE_24_BIT;
    } else if(strcmp(optarg, "both") == 0) {
     mode = MODE_BOTH;
    } else {
     printf("Mode must be one of 'indexed', '24bit', or 'both'.\n");
     exit(1);
    }
    break;
   case '?':
    break;
   default:
    display_help = 1;
    break;
  }
 }
 if (display_help == 1 || optind == argc) {
  print_usage();
  exit(0);
 }
 if (argc - optind > 1) {
  y = 0;
 }
 for (int i = optind; i < argc; i++) {
  char tempfile_name[] = "/tmp/icatXXXXXX";
  if (strcmp(argv[i], "-") == 0) {
   int tempfile;
   if ((tempfile = mkstemp(tempfile_name)) < 0) {
    perror("mkstemp");
    exit(1);
   }
   filename = tempfile_name;
   char buf;
   while (read(0, &buf, 1) > 0) {
    write(tempfile, &buf, 1);
   }
  } else {
   filename = argv[i];
  }
  image = imlib_load_image_immediately(filename);
  if (!image) {
   fprintf(stderr, "Could not load image: %s\n", filename);
   exit(1);
  }
  imlib_context_set_image(image);
  int width = imlib_image_get_width();
  int height = imlib_image_get_height();
  if (!keep_size) {
   resize_image_if_necessary(&width, &height, user_w);
  }
  if (y > 0) {
   printf("\x1b[%d;%dH", y, x);
  }
  Imlib_Color pixel1;
  Imlib_Color pixel2;
  for (int h = 0; h < height; h += 2) {
   if (x > 0) {
    printf("\x1b[%dG", x);
   }
   for (int w = 0; w < width; w++) {
    imlib_image_query_pixel(w, h + 1, &pixel1);
    imlib_image_query_pixel(w, h, &pixel2);
    print_pixels(&pixel1, &pixel2, mode);
   }
   printf("\x1b[0m\n");
  }
  imlib_free_image_and_decache();
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 char *filename;
 static int display_help = 0;
 void *image = ((void *)0);
 unsigned int x = 0;
 unsigned int y = 0;
 unsigned int user_w = 0;
 int c;
 _Bool keep_size = 0;
 int mode = MODE_INDEXED;
 for(;;) {
  static struct option long_options[] = {
   {"help", 0, &display_help, 1},
   {"x", 1, 0, 'x'},
   {"y", 1, 0, 'y'},
   {"width",1, 0, 'w'},
   {"keep", 0, 0, 'k'},
   {"mode", 1, 0, 'm'},
   {0, 0, 0, 0}
  };
  c = getopt_long(argc, argv, "hx:y:w:km:", long_options, ((void *)0));
  if (c == -1)
   break;
  switch (c) {
   case 'x':
    x = atoi(optarg);
    if (x < 0) {
     x = 0;
    }
    break;
   case 'y':
    y = atoi(optarg);
    if (y < 0) {
     y = 0;
    }
    break;
   case 'w':
    user_w = atoi(optarg);
    if(user_w < 1) {
     printf("Image width must be larger than 0\n");
     exit(1);
    }
    break;
   case 'h':
    display_help = 1;
    break;
   case 'k':
    keep_size = 1;
    break;
   case 'm':
    if(strcmp(optarg, "indexed") == 0) {
     mode = MODE_INDEXED;
    } else if(strcmp(optarg, "24bit") == 0) {
     mode = MODE_24_BIT;
    } else if(strcmp(optarg, "both") == 0) {
     mode = MODE_BOTH;
    } else {
     printf("Mode must be one of 'indexed', '24bit', or 'both'.\n");
     exit(1);
    }
    break;
   case '?':
    break;
   default:
    display_help = 1;
    break;
  }
 }
 if (display_help == 1 || optind == argc) {
  print_usage();
  exit(0);
 }
 if (argc - optind > 1) {
  y = 0;
 }
 for (int i = optind; i < argc; i++) {
  char tempfile_name[] = "/tmp/icatXXXXXX";
  if (strcmp(argv[i], "-") == 0) {
   int tempfile;
   if ((tempfile = mkstemp(tempfile_name)) < 0) {
    perror("mkstemp");
    exit(1);
   }
   filename = tempfile_name;
   char buf;
   while (read(0, &buf, 1) > 0) {
    write(tempfile, &buf, 1);
   }
  } else {
   filename = argv[i];
  }
  image = imlib_load_image_immediately(filename);
  if (!image) {
   fprintf(stderr, "Could not load image: %s\n", filename);
   exit(1);
  }
  imlib_context_set_image(image);
  int width = imlib_image_get_width();
  int height = imlib_image_get_height();
  if (!keep_size) {
   resize_image_if_necessary(&width, &height, user_w);
  }
  if (y > 0) {
   printf("\x1b[%d;%dH", y, x);
  }
  struct _imlib_color pixel1;
  struct _imlib_color pixel2;
  for (int h = 0; h < height; h += 2) {
   if (x > 0) {
    printf("\x1b[%dG", x);
   }
   for (int w = 0; w < width; w++) {
    imlib_image_query_pixel(w, h + 1, &pixel1);
    imlib_image_query_pixel(w, h, &pixel2);
    print_pixels(&pixel1, &pixel2, mode);
   }
   printf("\x1b[0m\n");
  }
  imlib_free_image_and_decache();
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
filename
char *

variable_types:
image
void *

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: i int

variable_types: tempfile int

variable_types: buf char

variable_types: width int

variable_types: height int

variable_types: h int

variable_types: w int


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


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

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 *v4; // rbx

result = a3;
if ( a2 )
{
v4 = a1;
do
result = crc16Add((unsigned __int16)result, *v4++);
while ( v4 != &a1[a2] );
}
return result;
}

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

result = a3;
if ( a2 )
{
v4 = a1;
do
result = func0((unsigned __int16)result, *v4++);
while ( v4 != &a1[a2] );
}
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:
__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:
__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 ( a2 == 16192 * (a2 / 0x3F40) )
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 ( a2 == 16192 * (a2 / 0x3F40) )
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(_DWORD *a1)
{
__int64 result; // rax
unsigned int v2; // esi
int v3; // ecx
__int64 v4; // r13
__int64 v5; // rbp
unsigned int v6; // eax

result = *(_QWORD *)a1;
v2 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
v3 = a1[3];
if ( v2 != v3 )
{
if ( v2 / 0xFD == (v3 - 1) / 0xFDu )
{
doWrite((__int64)a1, v2, (__int64)a1 + v2 % 0xFD + 18, v3 - v2);
if ( (a1[2] & 0x10) == 0 )
goto LABEL_5;
v4 = *(_QWORD *)a1;
*(_WORD *)(v4 + 8) = crc16Acc(
(unsigned __int8 *)a1 + *(_DWORD *)(*(_QWORD *)a1 + 4LL) % 0xFDu + 18,
(unsigned int)(a1[3] - *(_DWORD *)(*(_QWORD *)a1 + 4LL)),
*(unsigned __int16 *)(*(_QWORD *)a1 + 8LL));
if ( a1[3] != 253 * (a1[3] / 0xFDu) )
goto LABEL_5;
}
else
{
assertionFailed(
"handle->fcb->size / CHUNK_DATA_SIZE == (handle->pos - 1) / CHUNK_DATA_SIZE",
"../../../src/fs/block/block.c",
331LL);
}
v5 = *(_QWORD *)a1;
v6 = serviceAddr(*((_WORD *)a1 + 8), *(_DWORD *)(*(_QWORD *)a1 + 4LL));
blkExtFlashWrite(v6, v5 + 8, 2u);
*(_WORD *)(*(_QWORD *)a1 + 8LL) = 0;
LABEL_5:
result = *(_QWORD *)a1;
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = a1[3];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_DWORD *a1)
{
__int64 result; // rax
unsigned int v2; // esi
int v3; // ecx
__int64 v4; // r13
__int64 v5; // rbp
unsigned int v6; // eax

result = *(_QWORD *)a1;
v2 = *(_DWORD *)(*(_QWORD *)a1 + 4LL);
v3 = a1[3];
if ( v2 != v3 )
{
if ( v2 / 0xFD == (v3 - 1) / 0xFDu )
{
func10((__int64)a1, v2, (__int64)a1 + v2 % 0xFD + 18, v3 - v2);
if ( (a1[2] & 0x10) == 0 )
goto LABEL_5;
v4 = *(_QWORD *)a1;
*(_WORD *)(v4 + 8) = func5(
(unsigned __int8 *)a1 + *(_DWORD *)(*(_QWORD *)a1 + 4LL) % 0xFDu + 18,
(unsigned int)(a1[3] - *(_DWORD *)(*(_QWORD *)a1 + 4LL)),
*(unsigned __int16 *)(*(_QWORD *)a1 + 8LL));
if ( a1[3] != 253 * (a1[3] / 0xFDu) )
goto LABEL_5;
}
else
{
assertionFailed(
"handle->fcb->size / CHUNK_DATA_SIZE == (handle->pos - 1) / CHUNK_DATA_SIZE",
"../../../src/fs/block/block.c",
331LL);
}
v5 = *(_QWORD *)a1;
v6 = func3(*((_WORD *)a1 + 8), *(_DWORD *)(*(_QWORD *)a1 + 4LL));
func9(v6, v5 + 8, 2u);
*(_WORD *)(*(_QWORD *)a1 + 8LL) = 0;
LABEL_5:
result = *(_QWORD *)a1;
*(_DWORD *)(*(_QWORD *)a1 + 4LL) = a1[3];
}
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:
__int64 __fastcall blkClose(_DWORD *a1)
{
int v1; // eax

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

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(char *a1)
{
char *v1; // rbx
int v2; // eax

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

canonical_decompiled_code:
__int64 __fastcall func13(char *a1)
{
char *v1; // rbx
int v2; // eax

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

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)
{
unsigned int v2; // ebx
__int64 result; // rax

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

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

v2 = a2;
result = *(unsigned __int16 *)(*(_QWORD *)a1 + 2LL);
for ( a1[8] = result; v2 > 0x3F40; v2 -= 16192 )
{
result = fsBlockGetNext((unsigned __int16)a1[8]);
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 int v6; // eax

if ( a2 && a2 == 16192 * (a2 / 0x3F40) )
*(_WORD *)(a1 + 16) = fsBlockGetNext(*(unsigned __int16 *)(a1 + 16));
v6 = chunkAddr(*(_WORD *)(a1 + 16), a2);
return blkExtFlashRead(v6, a3, a4);
}

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

if ( a2 && a2 == 16192 * (a2 / 0x3F40) )
*(_WORD *)(a1 + 16) = fsBlockGetNext(*(unsigned __int16 *)(a1 + 16));
v6 = func2(*(_WORD *)(a1 + 16), a2);
return func17(v6, 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)
{
unsigned __int64 v3; // rbx
unsigned __int64 v5; // r13
int v7; // edi
unsigned int v8; // esi
unsigned int v9; // edx
char *v10; // rsi

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

canonical_decompiled_code:
__int64 __fastcall func20(_DWORD *a1, _QWORD *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rbx
unsigned __int64 v5; // r13
int v7; // edi
unsigned int v8; // esi
unsigned int v9; // edx
char *v10; // rsi

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

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 func22(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
__int64 result; // rax

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

canonical_decompiled_code:
__int64 func3()
{
char *v0; // rbx
__int64 result; // rax

if ( !printingSupressed )
{
func0("LEDs:");
v0 = (char *)&unk_1C4;
do
{
if ( *v0 )
{
if ( print_buf )
{
debugPrintfFormat(" %s%s%s", "\x1B[0;33m+", *(const char **)(v0 + 12), "+\x1B[0;37;00m");
serialPrint(print_buf);
}
}
else if ( print_buf )
{
debugPrintfFormat(" %s%s%s", "\x1B[0;34m-", *(const char **)(v0 + 4), "-\x1B[0;37;00m");
serialPrint(print_buf);
}
v0 += 24;
}
while ( v0 != "s:" );
result = func0("\n");
}
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; // edx

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; // edx

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


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; // ebx
int v7; // edx
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

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

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

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

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; // ebx
char v2; // r12
unsigned __int8 v3; // bp
__int16 v4; // ax
unsigned __int16 v5; // dx
char v6; // [rsp+Eh] [rbp-1Ah] BYREF
char v7[25]; // [rsp+Fh] [rbp-19h] BYREF

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

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

memset(values, 0, sizeof(values));
valueCount = 0LL;
qword_268 = 0LL;
qword_270 = 0LL;
qword_278 = 0LL;
currPos = 0LL;
qword_248 = 0LL;
qword_250 = 0LL;
qword_258 = 0LL;
result = open("adcValues.txt", 0);
if ( result >= 0 )
{
v1 = result;
v7[0] = 0;
v2 = 0;
v3 = -1;
do
{
v4 = func0(v1, &v6, v7);
if ( v4 != -1 )
{
if ( v3 == 0xFF || v2 )
{
v3 = v4;
}
else if ( v3 <= 0xFu )
{
v5 = *((_WORD *)&valueCount + v3);
values[256 * (unsigned __int64)v3 + v5] = v4;
*((_WORD *)&valueCount + v3) = v5 + 1;
}
v2 = v6;
}
}
while ( !v7[0] );
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()
{
int v0; // eax
__int64 v1; // rcx
unsigned __int16 v2; // dx

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

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
__int64 v1; // rcx
unsigned __int16 v2; // dx

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

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


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)
{
int v1; // ebx

v1 = *(_DWORD *)(a1 + 24);
return v1 - (unsigned int)getJiffies();
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // ebx

v1 = *(_DWORD *)(a1 + 24);
return v1 - (unsigned int)func0();
}

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


recvRadio

decompiled_code:
__int64 recvRadio()
{
__int64 result; // rax
__int16 v1; // bx

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

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
__int16 v1; // bx

func2();
result = func3((__int64)&radioBuffer, 0xFFFFu);
if ( (result & 0x8000u) != 0LL )
return func0("radio receive failed\n");
v1 = result;
if ( (__int16)result > 0 )
{
if ( print_buf )
{
debugPrintfFormat("radio receive %d bytes\n", (unsigned int)(__int16)result);
serialPrint(print_buf);
}
result = debugHexdump(&radioBuffer, (unsigned int)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((__int64)serialBuffer, a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int8 a1)
{
func0(serialBuffer);
if ( serialBuffer[a1 - 1] != 10 )
func0("\n");
return func6((__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


radioOn

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

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

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

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


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 )
result = fsClose((unsigned int)files);
if ( byte_3B1 != -1 )
result = fsClose((unsigned int)byte_3B1);
return result;
}

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

if ( files != -1 )
result = fsClose((unsigned int)files);
if ( byte_3B1 != -1 )
result = fsClose((unsigned int)byte_3B1);
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 streamWrite();
}

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 i; // rbx
char *v2; // rax
char *v3; // rax
__int64 result; // rax

current = 0;
fileCount = 0;
for ( i = 0LL; i != 2; ++i )
{
if ( (a1 & 2) != 0 )
{
v2 = name(i);
fsRemove(v2);
}
v3 = name(i);
result = fsOpen(v3, a1);
files[i] = result;
if ( (_BYTE)result != 0xFF )
++fileCount;
}
if ( (a1 & 2) != 0 )
{
if ( fileCount == 2 )
return result;
LABEL_12:
error("failed to open stream");
return streamRead();
}
if ( (a1 & 1) != 0 && fileCount <= 0 )
goto LABEL_12;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 i; // rbx
char *v2; // rax
char *v3; // rax
__int64 result; // rax

current = 0;
fileCount = 0;
for ( i = 0LL; i != 2; ++i )
{
if ( (a1 & 2) != 0 )
{
v2 = func1(i);
fsRemove(v2);
}
v3 = func1(i);
result = fsOpen(v3, a1);
files[i] = result;
if ( (_BYTE)result != 0xFF )
++fileCount;
}
if ( (a1 & 2) != 0 )
{
if ( fileCount == 2 )
return result;
LABEL_12:
func2("failed to open stream");
return func5();
}
if ( (a1 & 1) != 0 && 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:
__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:
__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
unsigned __int8 v2; // dl
unsigned __int16 v3; // ax

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

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

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

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