decompiled_code:
__int64 create_cfg()
{
const char *v0; // rbx
char *v1; // rax
int (*v2)(PANEL *); // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
int (*v6)(PANEL *); // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
void *ptr; // [rsp+8h] [rbp-10h] BYREF

ptr = (void *)xgetcwd(cfg_directory);
v0 = (const char *)xconcat_path_file(ptr, ".eView");
cfg_directory = (__int64)v0;
free(ptr);
if ( mkdir(v0, 0x1C0u) == -1 )
{
printf("%s:%d %s() UNABLE TO mkdir %s! IT'S BAD!\n", "src/cfg.c", 193LL, "create_cfg", v0);
fflush(stdout);
}
v1 = get_current_dir_name();
crop = 1;
ptr = v1;
write_config_int((__int64)"crop", 1u);
split_spreads = 0;
write_config_int((__int64)"split_spreads", 0);
rotate = 0;
write_config_int((__int64)"rotate", 0);
web_manga_mode = 0;
write_config_int((__int64)"web_manga_mode", 0);
frame = 0;
write_config_int((__int64)"frame", 0);
overlap = 10;
write_config_int((__int64)"overlap", 0xAu);
manga = 0;
write_config_int((__int64)"manga", 0);
keepaspect = 1;
write_config_int((__int64)"keepaspect", 1u);
fm_toggle = 0;
write_config_int((__int64)"fm_toggle", 0);
move_toggle = 1;
write_config_int((__int64)"move_toggle", 1u);
speed_toggle = 0;
write_config_int((__int64)"speed_toggle", 0);
show_clock = 1;
write_config_int((__int64)"show_clock", 1u);
top_panel_active = 1;
write_config_int((__int64)"top_panel_active", 1u);
loop_dir = 0;
write_config_int((__int64)"loop_dir", 0);
double_refresh = 0;
write_config_int((__int64)"double_refresh", 0);
viewed_pages = 0;
write_config_int((__int64)"viewed_pages", 0);
preload_enable = 1;
write_config_int((__int64)"preload_enable", 1u);
caching_enable = 1;
write_config_int((__int64)"caching_enable", 1u);
suppress_panel = 0;
write_config_int((__int64)"suppress_panel", 0);
show_hidden_files = 0;
write_config_int((__int64)"show_hidden_files", 0);
LED_notify = 1;
write_config_int((__int64)"LED_notify", 1u);
backlight = 0;
write_config_int((__int64)"backlight", 0);
sleep_timeout = 180;
write_config_int((__int64)"sleep_timeout", 0xB4u);
HD_scaling = 0;
write_config_int((__int64)"HD_scaling", 0);
boost_contrast = 0;
write_config_int((__int64)"boost_contrast", 0);
refresh_type = detect_refresh_type();
write_config_int((__int64)"refresh_type", refresh_type);
v2 = (int (*)(PANEL *))xgetcwd(0LL);
*(&top_panel + 3) = v2;
write_config_string((__int64)"top_panel.path", (const char *)v2);
v3 = strdup("../");
*(&top_panel + 4) = (int (*)(PANEL *))v3;
write_config_string((__int64)"top_panel.selected_name", v3);
v4 = strdup(&byte_103A);
*(&top_panel + 8) = (int (*)(PANEL *))v4;
write_config_string((__int64)"top_panel.archive_cwd", v4);
v5 = strdup(&byte_103A);
*(&top_panel + 7) = (int (*)(PANEL *))v5;
write_config_string((__int64)"top_panel.last_name", v5);
strcpy((char *)&top_panel + 80, "filesystem");
*((_BYTE *)&top_panel + 337) = 0;
write_archive_stack((__int64)"top_panel.archive_stack", (__int64)&top_panel);
v6 = (int (*)(PANEL *))xgetcwd(0LL);
*(&bottom_panel + 3) = v6;
write_config_string((__int64)"bottom_panel.path", (const char *)v6);
v7 = strdup("../");
*(&bottom_panel + 4) = (int (*)(PANEL *))v7;
write_config_string((__int64)"bottom_panel.selected_name", v7);
v8 = strdup(&byte_103A);
*(&bottom_panel + 8) = (int (*)(PANEL *))v8;
write_config_string((__int64)"bottom_panel.archive_cwd", v8);
v9 = strdup(&byte_103A);
*(&bottom_panel + 7) = (int (*)(PANEL *))v9;
write_config_string((__int64)"bottom_panel.last_name", v9);
strcpy((char *)&bottom_panel + 80, "filesystem");
*((_BYTE *)&bottom_panel + 337) = 0;
write_archive_stack((__int64)"bottom_panel.archive_stack", (__int64)&bottom_panel);
return xfree(&ptr);
}

canonical_decompiled_code:
__int64 func6()
{
const char *v0; // rbx
char *v1; // rax
int (*v2)(PANEL *); // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
int (*v6)(PANEL *); // rax
char *v7; // rax
char *v8; // rax
char *v9; // rax
void *ptr; // [rsp+8h] [rbp-10h] BYREF

ptr = (void *)xgetcwd(cfg_directory);
v0 = (const char *)xconcat_path_file(ptr, ".eView");
cfg_directory = (__int64)v0;
free(ptr);
if ( mkdir(v0, 0x1C0u) == -1 )
{
printf("%s:%d %s() UNABLE TO mkdir %s! IT'S BAD!\n", "src/cfg.c", 193LL, "create_cfg", v0);
fflush(stdout);
}
v1 = get_current_dir_name();
crop = 1;
ptr = v1;
func0((__int64)"crop", 1u);
split_spreads = 0;
func0((__int64)"split_spreads", 0);
rotate = 0;
func0((__int64)"rotate", 0);
web_manga_mode = 0;
func0((__int64)"web_manga_mode", 0);
frame = 0;
func0((__int64)"frame", 0);
overlap = 10;
func0((__int64)"overlap", 0xAu);
manga = 0;
func0((__int64)"manga", 0);
keepaspect = 1;
func0((__int64)"keepaspect", 1u);
fm_toggle = 0;
func0((__int64)"fm_toggle", 0);
move_toggle = 1;
func0((__int64)"move_toggle", 1u);
speed_toggle = 0;
func0((__int64)"speed_toggle", 0);
show_clock = 1;
func0((__int64)"show_clock", 1u);
top_panel_active = 1;
func0((__int64)"top_panel_active", 1u);
loop_dir = 0;
func0((__int64)"loop_dir", 0);
double_refresh = 0;
func0((__int64)"double_refresh", 0);
viewed_pages = 0;
func0((__int64)"viewed_pages", 0);
preload_enable = 1;
func0((__int64)"preload_enable", 1u);
caching_enable = 1;
func0((__int64)"caching_enable", 1u);
suppress_panel = 0;
func0((__int64)"suppress_panel", 0);
show_hidden_files = 0;
func0((__int64)"show_hidden_files", 0);
LED_notify = 1;
func0((__int64)"LED_notify", 1u);
backlight = 0;
func0((__int64)"backlight", 0);
sleep_timeout = 180;
func0((__int64)"sleep_timeout", 0xB4u);
HD_scaling = 0;
func0((__int64)"HD_scaling", 0);
boost_contrast = 0;
func0((__int64)"boost_contrast", 0);
refresh_type = detect_refresh_type();
func0((__int64)"refresh_type", refresh_type);
v2 = (int (*)(PANEL *))xgetcwd(0LL);
*(&top_panel + 3) = v2;
func2((__int64)"top_panel.path", (const char *)v2);
v3 = strdup("../");
*(&top_panel + 4) = (int (*)(PANEL *))v3;
func2((__int64)"top_panel.selected_name", v3);
v4 = strdup(&byte_103A);
*(&top_panel + 8) = (int (*)(PANEL *))v4;
func2((__int64)"top_panel.archive_cwd", v4);
v5 = strdup(&byte_103A);
*(&top_panel + 7) = (int (*)(PANEL *))v5;
func2((__int64)"top_panel.last_name", v5);
strcpy((char *)&top_panel + 80, "filesystem");
*((_BYTE *)&top_panel + 337) = 0;
func5((__int64)"top_panel.archive_stack", (__int64)&top_panel);
v6 = (int (*)(PANEL *))xgetcwd(0LL);
*(&bottom_panel + 3) = v6;
func2((__int64)"bottom_panel.path", (const char *)v6);
v7 = strdup("../");
*(&bottom_panel + 4) = (int (*)(PANEL *))v7;
func2((__int64)"bottom_panel.selected_name", v7);
v8 = strdup(&byte_103A);
*(&bottom_panel + 8) = (int (*)(PANEL *))v8;
func2((__int64)"bottom_panel.archive_cwd", v8);
v9 = strdup(&byte_103A);
*(&bottom_panel + 7) = (int (*)(PANEL *))v9;
func2((__int64)"bottom_panel.last_name", v9);
strcpy((char *)&bottom_panel + 80, "filesystem");
*((_BYTE *)&bottom_panel + 337) = 0;
func5((__int64)"bottom_panel.archive_stack", (__int64)&bottom_panel);
return xfree(&ptr);
}

original_code:
void create_cfg (void)
{
  char *current_dir=xgetcwd (cfg_directory);
  cfg_directory = xconcat_path_file(current_dir, ".eView");
  free(current_dir);
  if ((mkdir (cfg_directory, (0400|0200|0100))) == -1)
  {
    do { printf("%s:%d %s() " "UNABLE TO mkdir %s! IT'S BAD!\n" , "src/cfg.c", 193, __func__, cfg_directory); fflush(stdout); } while (0);
  }
  current_dir = get_current_dir_name();
  write_config_int("crop", crop=(!(0)));
  write_config_int("split_spreads", split_spreads=(0));
  write_config_int("rotate", rotate=(0));
  write_config_int("web_manga_mode", web_manga_mode=(0));
  write_config_int("frame", frame=(0));
  write_config_int("overlap", overlap=10);
  write_config_int("manga", manga=(0));
  write_config_int("keepaspect", keepaspect=(!(0)));
  write_config_int("fm_toggle", fm_toggle=(0));
  write_config_int("move_toggle", move_toggle=(!(0)));
  write_config_int("speed_toggle", speed_toggle=(0));
  write_config_int("show_clock", show_clock=(!(0)));
  write_config_int("top_panel_active", top_panel_active=(!(0)));
  write_config_int("loop_dir", loop_dir=LOOP_NONE);
  write_config_int("double_refresh", double_refresh=(0));
  write_config_int("viewed_pages", viewed_pages=0);
  write_config_int("preload_enable", preload_enable=(!(0)));
  write_config_int("caching_enable", caching_enable=(!(0)));
  write_config_int("suppress_panel", suppress_panel=(0));
  write_config_int("show_hidden_files", show_hidden_files=(0));
  write_config_int("LED_notify", LED_notify=(!(0)));
  write_config_int("backlight", backlight=(0));
  write_config_int("sleep_timeout", sleep_timeout=180);
  write_config_int("HD_scaling", HD_scaling=(0));
  write_config_int("boost_contrast", boost_contrast=(0));
  write_config_int("refresh_type", refresh_type=detect_refresh_type());
  write_config_string("top_panel.path", top_panel.path=xgetcwd(((void *)0)));
  write_config_string("top_panel.selected_name", top_panel.selected_name=strdup("../"));
  write_config_string("top_panel.archive_cwd", top_panel.archive_cwd=strdup(""));
  write_config_string("top_panel.last_name", top_panel.last_name=strdup(""));
  strcpy(top_panel.archive_stack[0],"filesystem");
  strcpy(top_panel.archive_stack[1],"");
  write_archive_stack("top_panel.archive_stack", &top_panel);
  write_config_string("bottom_panel.path", bottom_panel.path=xgetcwd(((void *)0)));
  write_config_string("bottom_panel.selected_name", bottom_panel.selected_name=strdup("../"));
  write_config_string("bottom_panel.archive_cwd", bottom_panel.archive_cwd=strdup(""));
  write_config_string("bottom_panel.last_name", bottom_panel.last_name=strdup(""));
  strcpy(bottom_panel.archive_stack[0],"filesystem");
  strcpy(bottom_panel.archive_stack[1],"");
  write_archive_stack("bottom_panel.archive_stack", &bottom_panel);
  xfree(&current_dir);
}

canonical_original_code:
void create_cfg()
{
  char *current_dir = xgetcwd (cfg_directory);
  cfg_directory = xconcat_path_file(current_dir, ".eView");
  free(current_dir);
  if ((mkdir (cfg_directory, (0400|0200|0100))) == -1)
  {
    do { printf("%s:%d %s() " "UNABLE TO mkdir %s! IT'S BAD!\n" , "src/cfg.c", 193, __func__, cfg_directory); fflush(stdout); } while (0);
  }
  current_dir = get_current_dir_name();
  write_config_int("crop", crop=(!(0)));
  write_config_int("split_spreads", split_spreads=(0));
  write_config_int("rotate", rotate=(0));
  write_config_int("web_manga_mode", web_manga_mode=(0));
  write_config_int("frame", frame=(0));
  write_config_int("overlap", overlap=10);
  write_config_int("manga", manga=(0));
  write_config_int("keepaspect", keepaspect=(!(0)));
  write_config_int("fm_toggle", fm_toggle=(0));
  write_config_int("move_toggle", move_toggle=(!(0)));
  write_config_int("speed_toggle", speed_toggle=(0));
  write_config_int("show_clock", show_clock=(!(0)));
  write_config_int("top_panel_active", top_panel_active=(!(0)));
  write_config_int("loop_dir", loop_dir=LOOP_NONE);
  write_config_int("double_refresh", double_refresh=(0));
  write_config_int("viewed_pages", viewed_pages=0);
  write_config_int("preload_enable", preload_enable=(!(0)));
  write_config_int("caching_enable", caching_enable=(!(0)));
  write_config_int("suppress_panel", suppress_panel=(0));
  write_config_int("show_hidden_files", show_hidden_files=(0));
  write_config_int("LED_notify", LED_notify=(!(0)));
  write_config_int("backlight", backlight=(0));
  write_config_int("sleep_timeout", sleep_timeout=180);
  write_config_int("HD_scaling", HD_scaling=(0));
  write_config_int("boost_contrast", boost_contrast=(0));
  write_config_int("refresh_type", refresh_type=detect_refresh_type());
  write_config_string("top_panel.path", top_panel.path=xgetcwd(((void *)0)));
  write_config_string("top_panel.selected_name", top_panel.selected_name=strdup("../"));
  write_config_string("top_panel.archive_cwd", top_panel.archive_cwd=strdup(""));
  write_config_string("top_panel.last_name", top_panel.last_name=strdup(""));
  strcpy(top_panel.archive_stack[0],"filesystem");
  strcpy(top_panel.archive_stack[1],"");
  write_archive_stack("top_panel.archive_stack", &top_panel);
  write_config_string("bottom_panel.path", bottom_panel.path=xgetcwd(((void *)0)));
  write_config_string("bottom_panel.selected_name", bottom_panel.selected_name=strdup("../"));
  write_config_string("bottom_panel.archive_cwd", bottom_panel.archive_cwd=strdup(""));
  write_config_string("bottom_panel.last_name", bottom_panel.last_name=strdup(""));
  strcpy(bottom_panel.archive_stack[0],"filesystem");
  strcpy(bottom_panel.archive_stack[1],"");
  write_archive_stack("bottom_panel.archive_stack", &bottom_panel);
  xfree(&current_dir);
}


repo: S-trace/eView

decompiled_code:
void read_configuration()
{
void *v0; // rbp

v0 = (void *)xgetcwd(cfg_directory);
cfg_directory = xconcat_path_file(v0, ".eView");
crop = read_config_int("crop");
split_spreads = read_config_int("split_spreads");
rotate = read_config_int("rotate");
frame = read_config_int("frame");
keepaspect = read_config_int("keepaspect");
manga = read_config_int("manga");
web_manga_mode = read_config_int("web_manga_mode");
overlap = read_config_int("overlap");
fm_toggle = read_config_int("fm_toggle");
move_toggle = read_config_int("move_toggle");
speed_toggle = read_config_int("speed_toggle");
show_clock = read_config_int("show_clock");
top_panel_active = read_config_int("top_panel_active");
loop_dir = read_config_int("loop_dir");
double_refresh = read_config_int("double_refresh");
viewed_pages = read_config_int("viewed_pages");
preload_enable = read_config_int("preload_enable");
caching_enable = read_config_int("caching_enable");
suppress_panel = read_config_int("suppress_panel");
show_hidden_files = read_config_int("show_hidden_files");
LED_notify = read_config_int("LED_notify");
backlight = read_config_int("backlight");
sleep_timeout = read_config_int("sleep_timeout");
HD_scaling = read_config_int("HD_scaling");
boost_contrast = read_config_int("boost_contrast");
refresh_type = read_config_int("refresh_type");
if ( !refresh_type )
{
refresh_type = detect_refresh_type();
write_config_int((__int64)"refresh_type", refresh_type);
}
read_panel_configuration((__int64)&top_panel);
read_panel_configuration((__int64)&bottom_panel);
free(v0);
}

canonical_decompiled_code:
void func8()
{
void *v0; // rbp

v0 = (void *)xgetcwd(cfg_directory);
cfg_directory = xconcat_path_file(v0, ".eView");
crop = func1("crop");
split_spreads = func1("split_spreads");
rotate = func1("rotate");
frame = func1("frame");
keepaspect = func1("keepaspect");
manga = func1("manga");
web_manga_mode = func1("web_manga_mode");
overlap = func1("overlap");
fm_toggle = func1("fm_toggle");
move_toggle = func1("move_toggle");
speed_toggle = func1("speed_toggle");
show_clock = func1("show_clock");
top_panel_active = func1("top_panel_active");
loop_dir = func1("loop_dir");
double_refresh = func1("double_refresh");
viewed_pages = func1("viewed_pages");
preload_enable = func1("preload_enable");
caching_enable = func1("caching_enable");
suppress_panel = func1("suppress_panel");
show_hidden_files = func1("show_hidden_files");
LED_notify = func1("LED_notify");
backlight = func1("backlight");
sleep_timeout = func1("sleep_timeout");
HD_scaling = func1("HD_scaling");
boost_contrast = func1("boost_contrast");
refresh_type = func1("refresh_type");
if ( !refresh_type )
{
refresh_type = detect_refresh_type();
func0((__int64)"refresh_type", refresh_type);
}
func7((__int64)&top_panel);
func7((__int64)&bottom_panel);
free(v0);
}

original_code:
void read_configuration (void)
{
  char *current_dir=xgetcwd (cfg_directory);
  cfg_directory = xconcat_path_file(current_dir, ".eView");
  crop=read_config_int("crop");
  split_spreads=read_config_int("split_spreads");
  rotate=read_config_int("rotate");
  frame=read_config_int("frame");
  keepaspect=read_config_int("keepaspect");
  manga=read_config_int("manga");
  web_manga_mode=read_config_int("web_manga_mode");
  overlap=read_config_int("overlap");
  fm_toggle=read_config_int("fm_toggle");
  move_toggle=read_config_int("move_toggle");
  speed_toggle=read_config_int("speed_toggle");
  show_clock=read_config_int("show_clock");
  top_panel_active=read_config_int("top_panel_active");
  loop_dir=read_config_int("loop_dir");
  double_refresh=read_config_int("double_refresh");
  viewed_pages=read_config_int("viewed_pages");
  preload_enable=read_config_int("preload_enable");
  caching_enable=read_config_int("caching_enable");
  suppress_panel=read_config_int("suppress_panel");
  show_hidden_files=read_config_int("show_hidden_files");
  LED_notify=read_config_int("LED_notify");
  backlight=read_config_int("backlight");
  sleep_timeout=read_config_int("sleep_timeout");
  HD_scaling=read_config_int("HD_scaling");
  boost_contrast=read_config_int("boost_contrast");
  refresh_type=read_config_int("refresh_type");
  if (refresh_type == REFRESH_UNKNOWN)
    write_config_int("refresh_type", refresh_type=detect_refresh_type());
  read_panel_configuration(&top_panel);
  read_panel_configuration(&bottom_panel);
  free (current_dir);
}

canonical_original_code:
void read_configuration()
{
  char *current_dir = xgetcwd (cfg_directory);
  cfg_directory = xconcat_path_file(current_dir, ".eView");
  crop=read_config_int("crop");
  split_spreads=read_config_int("split_spreads");
  rotate=read_config_int("rotate");
  frame=read_config_int("frame");
  keepaspect=read_config_int("keepaspect");
  manga=read_config_int("manga");
  web_manga_mode=read_config_int("web_manga_mode");
  overlap=read_config_int("overlap");
  fm_toggle=read_config_int("fm_toggle");
  move_toggle=read_config_int("move_toggle");
  speed_toggle=read_config_int("speed_toggle");
  show_clock=read_config_int("show_clock");
  top_panel_active=read_config_int("top_panel_active");
  loop_dir=read_config_int("loop_dir");
  double_refresh=read_config_int("double_refresh");
  viewed_pages=read_config_int("viewed_pages");
  preload_enable=read_config_int("preload_enable");
  caching_enable=read_config_int("caching_enable");
  suppress_panel=read_config_int("suppress_panel");
  show_hidden_files=read_config_int("show_hidden_files");
  LED_notify=read_config_int("LED_notify");
  backlight=read_config_int("backlight");
  sleep_timeout=read_config_int("sleep_timeout");
  HD_scaling=read_config_int("HD_scaling");
  boost_contrast=read_config_int("boost_contrast");
  refresh_type=read_config_int("refresh_type");
  if (refresh_type == REFRESH_UNKNOWN)
    write_config_int("refresh_type", refresh_type=detect_refresh_type());
  read_panel_configuration(&top_panel);
  read_panel_configuration(&bottom_panel);
  free (current_dir);
}

return_type: void


decompiled_code:
__int64 reset_config()
{
char *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

ptr[0] = 0LL;
printf("%s:%d %s() Removing '%s'\n", "src/cfg.c", 312LL, "reset_config", (const char *)cfg_directory);
fflush(stdout);
if ( asprintf(ptr, "rm -rf \"%s\"", (const char *)cfg_directory) < 0 )
{
printf("%s:%d %s() asprintf() call failed (out of memory?)", "src/cfg.c", 313LL, "reset_config");
fflush(stdout);
shutdown(1, (int)"src/cfg.c");
}
xsystem(ptr[0]);
return xfree(ptr);
}

canonical_decompiled_code:
__int64 func9()
{
char *ptr[2]; // [rsp+8h] [rbp-10h] BYREF

ptr[0] = 0LL;
printf("%s:%d %s() Removing '%s'\n", "src/cfg.c", 312LL, "reset_config", (const char *)cfg_directory);
fflush(stdout);
if ( asprintf(ptr, "rm -rf \"%s\"", (const char *)cfg_directory) < 0 )
{
printf("%s:%d %s() asprintf() call failed (out of memory?)", "src/cfg.c", 313LL, "reset_config");
fflush(stdout);
shutdown(1, (int)"src/cfg.c");
}
xsystem(ptr[0]);
return xfree(ptr);
}

original_code:
void reset_config(void)
{
  char *command=((void *)0);
  do { printf("%s:%d %s() " "Removing '%s'\n" , "src/cfg.c", 312, __func__, cfg_directory); fflush(stdout); } while (0);
  if (asprintf(&command, "rm -rf \"%s\"", cfg_directory) < 0) { do { printf("%s:%d %s() " "asprintf() call failed (out of memory?)" , "src/cfg.c", 313, __func__); fflush(stdout); shutdown(1); } while (0); };
  xsystem(command);
  xfree(&command);
}

canonical_original_code:
void reset_config()
{
  char *command = ((void *)0);
  do { printf("%s:%d %s() " "Removing '%s'\n" , "src/cfg.c", 312, __func__, cfg_directory); fflush(stdout); } while (0);
  if (asprintf(&command, "rm -rf \"%s\"", cfg_directory) < 0) { do { printf("%s:%d %s() " "asprintf() call failed (out of memory?)" , "src/cfg.c", 313, __func__); fflush(stdout); shutdown(1); } while (0); };
  xsystem(command);
  xfree(&command);
}

variable_types:
command
char *


decompiled_code:
__int64 __fastcall right_way(_QWORD *a1)
{
__int64 result; // rax

if ( height - 32 > vertical_position )
{
++vertical_position;
result = 0LL;
}
else if ( f_count <= 0 )
{
frame_map_clear(a1);
result = 1LL;
}
else
{
++f_count;
result = 1LL;
*((_DWORD *)a1 + f_num + 128) = height;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
__int64 result; // rax

if ( height - 32 > vertical_position )
{
++vertical_position;
result = 0LL;
}
else if ( f_count <= 0 )
{
func1(a1);
result = 1LL;
}
else
{
++f_count;
result = 1LL;
*((_DWORD *)a1 + f_num + 128) = height;
}
return result;
}

original_code:
int right_way (int frame_map[][128])
{
  if (height-32 > vertical_position) {
    vertical_position++;
    return 0;
  }
  if (f_count>=1) {
    frame_map[1][f_num] = height;
    f_count++;
    return 1;
  }
  frame_map_clear(frame_map);
  return 1;
}

canonical_original_code:
int right_way(int frame_map[][128])
{
  if (height-32 > vertical_position) {
    vertical_position++;
    return 0;
  }
  if (f_count>=1) {
    frame_map[1][f_num] = height;
    f_count++;
    return 1;
  }
  frame_map_clear(frame_map);
  return 1;
}

return_type: int


decompiled_code:
__int64 __fastcall left_way_fc(_QWORD *a1)
{
__int64 result; // rax

if ( f_count <= 1 )
{
frame_map_clear(a1);
result = 1LL;
}
else
{
++f_count;
result = 1LL;
*((_DWORD *)a1 + f_num + 128) = height;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1)
{
__int64 result; // rax

if ( f_count <= 1 )
{
func1(a1);
result = 1LL;
}
else
{
++f_count;
result = 1LL;
*((_DWORD *)a1 + f_num + 128) = height;
}
return result;
}

original_code:
int left_way_fc (int frame_map[][128])
{
  if (f_count>1){
    frame_map[1][f_num] = height;
    f_count++;
    return 1;
  }
  frame_map_clear(frame_map);
  return 1;
}

canonical_original_code:
int left_way_fc(int frame_map[][128])
{
  if (f_count>1){
    frame_map[1][f_num] = height;
    f_count++;
    return 1;
  }
  frame_map_clear(frame_map);
  return 1;
}


decompiled_code:
__int64 __fastcall left_way_sc(_QWORD *a1)
{
int v1; // er8
__int64 result; // rax

v1 = vertical_position;
if ( height - 31 <= vertical_position && (unsigned int)left_way_fc(a1) )
return 1LL;
if ( s_count > 2048 && (unsigned int)left_way_fc(a1) )
return 1LL;
result = 0LL;
vertical_position = v1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1)
{
int v1; // er8
__int64 result; // rax

v1 = vertical_position;
if ( height - 31 <= vertical_position && (unsigned int)func3(a1) )
return 1LL;
if ( s_count > 2048 && (unsigned int)func3(a1) )
return 1LL;
result = 0LL;
vertical_position = v1 + 1;
return result;
}

original_code:
int left_way_sc(int frame_map[][128])
{
  int f = 0;
  for(;;){
    if (height-32 < vertical_position) f = left_way_fc(frame_map);
    if (f) break;
    if (s_count > 2048) {
      f = left_way_fc(frame_map);
      if (f) break;
    }
    vertical_position++;
    return 0;
  }
  return 1;
}

variable_types: f int


decompiled_code:
__int64 line_separator()
{
int v0; // eax
int v1; // edx
int v2; // edi
int v3; // er11
int v4; // er9
unsigned __int8 *v5; // rcx
int v6; // er14
unsigned __int8 v7; // si
unsigned __int8 v8; // r8
unsigned __int8 v9; // r10

if ( width - 1 <= 0 )
return 1LL;
v0 = 0;
v1 = 1;
v2 = 0;
v3 = 0;
v4 = 0;
v5 = (unsigned __int8 *)(pixels + rowstride * vertical_position);
while ( 1 )
{
v7 = *v5;
v8 = v5[1];
v9 = v5[2];
if ( *v5 == v8 && v7 == v9 )
{
if ( v7 > 0x3Bu )
goto LABEL_16;
}
else
{
++v0;
if ( v7 > 0x3Bu )
goto LABEL_16;
}
if ( v8 <= 0x3Bu )
{
if ( v9 <= 0x3Bu )
{
v6 = v1;
v3 = 1;
}
else
{
v6 = v1 & v3;
}
goto LABEL_7;
}
LABEL_16:
v6 = v1 & v3;
if ( v8 > 0xA0u && v7 > 0xA0u && v9 > 0xA0u )
v2 = 1;
LABEL_7:
v0 = v0 - ((v6 == 0) - 1) - (((v2 & (v1 ^ 1)) == 0) - 1);
if ( v0 > 80 )
return 0LL;
if ( v2 )
v1 = v2;
if ( v3 )
v1 = 0;
++v4;
v5 += n_channels;
if ( v4 == width - 1 )
return 1LL;
}
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // eax
int v1; // edx
int v2; // edi
int v3; // er11
int v4; // er9
unsigned __int8 *v5; // rcx
int v6; // er14
unsigned __int8 v7; // si
unsigned __int8 v8; // r8
unsigned __int8 v9; // r10

if ( width - 1 <= 0 )
return 1LL;
v0 = 0;
v1 = 1;
v2 = 0;
v3 = 0;
v4 = 0;
v5 = (unsigned __int8 *)(pixels + rowstride * vertical_position);
while ( 1 )
{
v7 = *v5;
v8 = v5[1];
v9 = v5[2];
if ( *v5 == v8 && v7 == v9 )
{
if ( v7 > 0x3Bu )
goto LABEL_16;
}
else
{
++v0;
if ( v7 > 0x3Bu )
goto LABEL_16;
}
if ( v8 <= 0x3Bu )
{
if ( v9 <= 0x3Bu )
{
v6 = v1;
v3 = 1;
}
else
{
v6 = v1 & v3;
}
goto LABEL_7;
}
LABEL_16:
v6 = v1 & v3;
if ( v8 > 0xA0u && v7 > 0xA0u && v9 > 0xA0u )
v2 = 1;
LABEL_7:
v0 = v0 - ((v6 == 0) - 1) - (((v2 & (v1 ^ 1)) == 0) - 1);
if ( v0 > 80 )
return 0LL;
if ( v2 )
v1 = v2;
if ( v3 )
v1 = 0;
++v4;
v5 += n_channels;
if ( v4 == width - 1 )
return 1LL;
}
}

original_code:
int line_separator (void)
{
  int x, b_color, w_color, prev, line, rp;
  rp = 0;
  prev = 1;
  line = 1;
  b_color = 0;
  w_color = 0;
  for (x = 0; x < width-1; x++) {
    guchar red, green, blue;
    current_pixel = pixels + vertical_position * rowstride + x * n_channels;
    red = current_pixel[0];
    green = current_pixel[1];
    blue = current_pixel[2];
    if (red != green || red != blue || green != blue)
      rp++;
    if (red < (guchar)60 && green<(guchar)60 && blue < (guchar)60) b_color = 1;
    if (red > (guchar)160 && green>(guchar)160 && blue >(guchar)160) w_color =1;
    if (prev == 1 && b_color == 1) rp++;
    if (prev == 0 && w_color == 1) rp++;
    if (rp > 80){
      line = 0;
      break;
    }
    if (w_color) prev = 1;
    if (b_color) prev = 0;
  }
  return line;
}

canonical_original_code:
int line_separator()
{
  int x; int b_color; int w_color; int prev; int line; int rp;
  rp = 0;
  prev = 1;
  line = 1;
  b_color = 0;
  w_color = 0;
  for (x = 0; x < width-1; x++) {
    unsigned char red; unsigned char green; unsigned char blue;
    current_pixel = pixels + vertical_position * rowstride + x * n_channels;
    red = current_pixel[0];
    green = current_pixel[1];
    blue = current_pixel[2];
    if (red != green || red != blue || green != blue)
      rp++;
    if (red < (unsigned char)60 && green<(unsigned char)60 && blue < (unsigned char)60) b_color = 1;
    if (red > (unsigned char)160 && green>(unsigned char)160 && blue >(unsigned char)160) w_color =1;
    if (prev == 1 && b_color == 1) rp++;
    if (prev == 0 && w_color == 1) rp++;
    if (rp > 80){
      line = 0;
      break;
    }
    if (w_color) prev = 1;
    if (b_color) prev = 0;
  }
  return line;
}

variable_types: x int

variable_types: prev int

variable_types: line int

variable_types: rp int

variable_types:
red
unsigned char

variable_types:
green
unsigned char

variable_types:
blue
unsigned char


decompiled_code:
__int64 __fastcall left_way(_QWORD *a1)
{
int v1; // edx
int v2; // eax
_DWORD *v3; // rbp
__int64 result; // rax
int v5; // edx
int v6; // ecx

v1 = vertical_position;
v2 = f_num;
v3 = (_DWORD *)a1 + f_num;
++s_count;
v3[128] = vertical_position;
++f_count;
vertical_position = v1 + 1;
f_num = v2 + 1;
while ( 1 )
{
result = line_separator();
if ( !(_DWORD)result )
break;
if ( (unsigned int)left_way_sc(a1) )
return 1LL;
}
v5 = vertical_position;
v6 = height;
s_count = 0;
v3[1] = vertical_position;
vertical_position = v5 + 32;
if ( v5 + 32 >= v6 - 32 )
{
++f_count;
result = 1LL;
v3[129] = v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
int v1; // edx
int v2; // eax
_DWORD *v3; // rbp
__int64 result; // rax
int v5; // edx
int v6; // ecx

v1 = vertical_position;
v2 = f_num;
v3 = (_DWORD *)a1 + f_num;
++s_count;
v3[128] = vertical_position;
++f_count;
vertical_position = v1 + 1;
f_num = v2 + 1;
while ( 1 )
{
result = func5();
if ( !(_DWORD)result )
break;
if ( (unsigned int)func4(a1) )
return 1LL;
}
v5 = vertical_position;
v6 = height;
s_count = 0;
v3[1] = vertical_position;
vertical_position = v5 + 32;
if ( v5 + 32 >= v6 - 32 )
{
++f_count;
result = 1LL;
v3[129] = v6;
}
return result;
}

original_code:
int left_way(int frame_map[][128])
{
  int f = 0;
  frame_map[1][f_num] = vertical_position;
  vertical_position++;
  s_count++;
  f_count++;
  f_num++;
  for (;;){
    if (line_separator()) f=left_way_sc(frame_map); else break;
    if (f) return 1;
  }
  s_count = 0;
  frame_map[0][f_num] = vertical_position;
  vertical_position = vertical_position + 32;
  if (height - 32 > vertical_position) return 0;
  frame_map[1][f_num] = height;
  f_count++;
  return 1;
}


strregerror

decompiled_code:
const char *__fastcall strregerror(int a1)
{
int v1; // edx
char v2; // cl
char *v3; // rax
const char *v4; // r8

v1 = 1;
v2 = 0;
v3 = (char *)&tab_0;
p_1 = (__int64)&tab_0;
v4 = "regexec() failed to match";
while ( a1 != v1 )
{
v4 = (const char *)*((_QWORD *)v3 + 3);
v3 += 16;
if ( !v4 )
{
p_1 = (__int64)v3;
return "unknown error";
}
v1 = *(_DWORD *)v3;
v2 = 1;
}
if ( v2 )
p_1 = (__int64)v3;
return v4;
}

canonical_decompiled_code:
const char *__fastcall func7(int a1)
{
int v1; // edx
char v2; // cl
char *v3; // rax
const char *v4; // r8

v1 = 1;
v2 = 0;
v3 = (char *)&tab_0;
p_1 = (__int64)&tab_0;
v4 = "regexec() failed to match";
while ( a1 != v1 )
{
v4 = (const char *)*((_QWORD *)v3 + 3);
v3 += 16;
if ( !v4 )
{
p_1 = (__int64)v3;
return "unknown error";
}
v1 = *(_DWORD *)v3;
v2 = 1;
}
if ( v2 )
p_1 = (__int64)v3;
return v4;
}

original_code:
static const char *
strregerror(int code)
{
 static const struct {
  int code;
  const char *descr;
 } tab[] = {
  { _REG_NOMATCH, "regexec() failed to match"},
  { _REG_BADPAT, "invalid regular expression"},
  { _REG_ECOLLATE, "invalid collating element"},
  { _REG_ECTYPE, "invalid character class"},
  { _REG_EESCAPE, "\\ applied to unescapable character"},
  { _REG_ESUBREG, "invalid backreference number"},
  { _REG_EBRACK, "brackets [ ] not balanced"},
  { _REG_EPAREN, "parentheses ( ) not balanced"},
  { _REG_EBRACE, "braces { } not balanced"},
  { _REG_BADBR, "invalid repetition count(s) in { }"},
  { _REG_ERANGE, "invalid character range in [ ]"},
  { _REG_ESPACE, "ran out of memory"},
  { _REG_BADRPT, "?, *, or + operand invalid"},
  { 0, ((void *)0)}
 }, *p;
 for (p = tab; p->descr != ((void *)0); ++p)
  if (code == p->code)
   return p->descr;
 return "unknown error";
}

canonical_original_code:
static char *strregerror(int code)
{
 static struct { int code; char *descr; } tab[] = {
  { _REG_NOMATCH, "regexec() failed to match"},
  { _REG_BADPAT, "invalid regular expression"},
  { _REG_ECOLLATE, "invalid collating element"},
  { _REG_ECTYPE, "invalid character class"},
  { _REG_EESCAPE, "\\ applied to unescapable character"},
  { _REG_ESUBREG, "invalid backreference number"},
  { _REG_EBRACK, "brackets [ ] not balanced"},
  { _REG_EPAREN, "parentheses ( ) not balanced"},
  { _REG_EBRACE, "braces { } not balanced"},
  { _REG_BADBR, "invalid repetition count(s) in { }"},
  { _REG_ERANGE, "invalid character range in [ ]"},
  { _REG_ESPACE, "ran out of memory"},
  { _REG_BADRPT, "?, *, or + operand invalid"},
  { 0, ((void *)0)}
 }; static struct { int code; char *descr; } *p;
 for (p = tab; p->descr != ((void *)0); ++p)
  if (code == p->code)
   return p->descr;
 return "unknown error";
}

variable_types: code int

variable_types:
tab
struct { int code; char *descr; } []

variable_types:
p
struct { int code; char *descr; } *

return_type: char *


coloridx

decompiled_code:
__int64 __fastcall coloridx(char *s1)
{
const char *v1; // rsi
__int64 v2; // rbx
unsigned int v3; // er12
char *s2[13]; // [rsp+0h] [rbp-68h]

v1 = "black";
v2 = 0LL;
s2[1] = "red";
s2[2] = "green";
s2[3] = "yellow";
s2[4] = "blue";
s2[5] = "magenta";
s2[6] = "cyan";
s2[7] = "white";
s2[8] = 0LL;
while ( 1 )
{
v3 = v2;
if ( !strcmp(s1, v1) )
break;
v1 = s2[++v2];
if ( !v1 )
return (unsigned int)-1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s1)
{
const char *v1; // rsi
__int64 v2; // rbx
unsigned int v3; // er12
char *s2[13]; // [rsp+0h] [rbp-68h]

v1 = "black";
v2 = 0LL;
s2[1] = "red";
s2[2] = "green";
s2[3] = "yellow";
s2[4] = "blue";
s2[5] = "magenta";
s2[6] = "cyan";
s2[7] = "white";
s2[8] = 0LL;
while ( 1 )
{
v3 = v2;
if ( !strcmp(s1, v1) )
break;
v1 = s2[++v2];
if ( !v1 )
return (unsigned int)-1;
}
return v3;
}

original_code:
static int
coloridx(const char *color)
{
 const char *tab[] = {
  "black",
  "red",
  "green",
  "yellow",
  "blue",
  "magenta",
  "cyan",
  "white",
  ((void *)0)
 };
 int i;
 for (i = 0; tab[i] != ((void *)0); ++i)
  if (!strcmp(color, tab[i]))
   return i;
 return -1;
}

canonical_original_code:
static int coloridx(char *color)
{
 char *tab[] = {
  "black",
  "red",
  "green",
  "yellow",
  "blue",
  "magenta",
  "cyan",
  "white",
  ((void *)0)
 };
 int i;
 for (i = 0; tab[i] != ((void *)0); ++i)
  if (!strcmp(color, tab[i]))
   return i;
 return -1;
}

variable_types:
color
char *

variable_types:
tab
char *[]

variable_types: i int


parsecolor

decompiled_code:
__int64 __fastcall parsecolor(char *s1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rax
char *v5; // rbx
__int64 result; // rax

v4 = strchr(s1, 58);
if ( v4 )
{
*v4 = 0;
v5 = v4;
*a2 = coloridx(s1);
result = coloridx(v5 + 1);
*a3 = result;
}
else
{
result = coloridx(s1);
*a2 = result;
*a3 = -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *s1, _DWORD *a2, _DWORD *a3)
{
char *v4; // rax
char *v5; // rbx
__int64 result; // rax

v4 = strchr(s1, 58);
if ( v4 )
{
*v4 = 0;
v5 = v4;
*a2 = func9(s1);
result = func9(v5 + 1);
*a3 = result;
}
else
{
result = func9(s1);
*a2 = result;
*a3 = -1;
}
return result;
}

original_code:
static void
parsecolor(char *col, int *fg, int *bg)
{
 char *p;
 p = strchr(col, ':');
 if (p == ((void *)0)) {
  *fg = coloridx(col);
  *bg = -1;
 } else {
  *p++ = '\0';
  *fg = coloridx(col);
  *bg = coloridx(p);
 }
}

variable_types:
col
char *

variable_types:
fg
int *

variable_types:
bg
int *

variable_types:
p
char *


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
err(1, 0LL);
return result;
}

canonical_decompiled_code:
void *__fastcall func11(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
err(1, 0LL);
return result;
}

original_code:
static void *
xrealloc(void *p, size_t size)
{
 void *q;
 q = realloc(p, size);
 if (q == ((void *)0))
  err(1, ((void *)0));
 return q;
}

canonical_original_code:
static void *xrealloc(void *p, unsigned long size)
{
 void *q;
 q = realloc(p, size);
 if (q == ((void *)0))
  err(1, ((void *)0));
 return q;
}

variable_types:
p
void *

variable_types:
size
unsigned long

variable_types:
q
void *

return_type: void *


ins

decompiled_code:
size_t __fastcall ins(void **a1, __int64 *a2, const char *a3, __int64 a4)
{
size_t v6; // r12
char *v7; // rax
__int64 v8; // rdx

v6 = strlen(a3);
v7 = (char *)xrealloc(*a1, v6 + *a2);
v8 = *a2;
*a1 = v7;
memmove(&v7[a4 + v6], &v7[a4], v8 - a4);
memcpy((char *)*a1 + a4, a3, v6);
*a2 += v6;
return v6;
}

canonical_decompiled_code:
size_t __fastcall func12(void **a1, __int64 *a2, const char *a3, __int64 a4)
{
size_t v6; // r12
char *v7; // rax
__int64 v8; // rdx

v6 = strlen(a3);
v7 = (char *)func11(*a1, v6 + *a2);
v8 = *a2;
*a1 = v7;
memmove(&v7[a4 + v6], &v7[a4], v8 - a4);
memcpy((char *)*a1 + a4, a3, v6);
*a2 += v6;
return v6;
}

original_code:
static size_t
ins(char **s, size_t *sz, const char *txt, off_t pos)
{
 size_t txtlen;
 txtlen = strlen(txt);
 *s = xrealloc(*s, *sz + txtlen);
 memmove(*s + pos + txtlen, *s + pos, *sz - pos);
 memcpy(*s + pos, txt, txtlen);
 *sz += txtlen;
 return txtlen;
}

canonical_original_code:
static unsigned long ins(char **s, unsigned long *sz, char *txt, long pos)
{
 unsigned long txtlen;
 txtlen = strlen(txt);
 *s = xrealloc(*s, *sz + txtlen);
 memmove(*s + pos + txtlen, *s + pos, *sz - pos);
 memcpy(*s + pos, txt, txtlen);
 *sz += txtlen;
 return txtlen;
}

variable_types:
s
char **

variable_types:
sz
unsigned long *

variable_types:
txt
char *

variable_types: pos long

variable_types:
txtlen
unsigned long

return_type: unsigned long


passthru

decompiled_code:
__int64 passthru()
{
signed __int64 v0; // rax
_BYTE v2[8200]; // [rsp+0h] [rbp-2008h] BYREF

while ( 1 )
{
v0 = read(0, v2, 0x2000uLL);
if ( v0 <= 0 )
break;
if ( v0 != write(1, v2, v0) )
err(1, "short write");
}
if ( v0 )
err(1, "read");
return 0LL;
}

canonical_decompiled_code:
__int64 func14()
{
signed __int64 v0; // rax
_BYTE v2[8200]; // [rsp+0h] [rbp-2008h] BYREF

while ( 1 )
{
v0 = read(0, v2, 0x2000uLL);
if ( v0 <= 0 )
break;
if ( v0 != write(1, v2, v0) )
err(1, "short write");
}
if ( v0 )
err(1, "read");
return 0LL;
}

original_code:
static int
passthru(void)
{
 char buf[8192];
 const size_t bufsize = sizeof buf;
 ssize_t nread;
 ssize_t nwrote;
 while ((nread = read(0, buf, bufsize)) > 0) {
  nwrote = write(1, buf, nread);
  if (nwrote != nread)
   err(1, "short write");
 }
 if (nread < 0)
  err(1, "read");
 return 0;
}

canonical_original_code:
static int passthru()
{
 char buf[8192];
 unsigned long bufsize = sizeof buf;
 long nread;
 long nwrote;
 while ((nread = read(0, buf, bufsize)) > 0) {
  nwrote = write(1, buf, nread);
  if (nwrote != nread)
   err(1, "short write");
 }
 if (nread < 0)
  err(1, "read");
 return 0;
}

variable_types:
buf
char [8192]

variable_types:
bufsize
unsigned long

variable_types: nread long

variable_types: nwrote long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // r12
int result; // eax

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "bfhi");
if ( v3 == -1 )
break;
if ( v3 == 102 )
{
++fflag;
}
else if ( v3 > 102 )
{
if ( v3 == 104 )
usage(0);
if ( v3 != 105 )
goto LABEL_16;
++iflag;
}
else
{
if ( v3 != 98 )
goto LABEL_16;
++bflag;
}
}
v4 = optind;
if ( argc - optind <= 1 )
LABEL_16:
usage(1);
if ( (unsigned __int8)istty_constprop_0() || fflag )
result = color(argv[v4], argv[v4 + 1]);
else
result = passthru();
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // r12
int result; // eax

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "bfhi");
if ( v3 == -1 )
break;
if ( v3 == 102 )
{
++fflag;
}
else if ( v3 > 102 )
{
if ( v3 == 104 )
func8(0);
if ( v3 != 105 )
goto LABEL_16;
++iflag;
}
else
{
if ( v3 != 98 )
goto LABEL_16;
++bflag;
}
}
v4 = optind;
if ( argc - optind <= 1 )
LABEL_16:
func8(1);
if ( (unsigned __int8)func15() || fflag )
result = func13(argv[v4], argv[v4 + 1]);
else
result = func14();
return result;
}

original_code:
int
main(int argc, char **argv)
{
 int ch;
 extern int optind;
 while ((ch = getopt(argc, argv, "bfhi")) != -1) {
  switch (ch) {
  case 'b':
   ++bflag;
   break;
  case 'f':
   ++fflag;
   break;
  case 'i':
   ++iflag;
   break;
  case 'h':
   usage(0);
  case '?':
  default:
   usage(1);
  }
 }
 argc -= optind;
 argv += optind;
 if (argc < 2)
  usage(1);
 if (istty(1) || fflag)
  return color(argv[0], argv[1]);
 else
  return passthru();
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ch int

variable_types: optind int


usage

decompiled_code:
void __fastcall __noreturn usage(int status)
{
printf("usage: %s [-b] [-i] [<fg>]:[<bg>] <pattern>\n", (const char *)program_invocation_short_name);
puts("Read from stdin, colorize text matching <pattern>, output to stdout.");
puts("\t-b -- bold");
puts("\t-f -- force color output even when stdout is not terminal");
puts("\t-i -- ignore case");
puts("\t<fg>, <bg> -- foreground and background colors");
puts("\t<pattern> -- extended regular expression");
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(int status)
{
printf("usage: %s [-b] [-i] [<fg>]:[<bg>] <pattern>\n", (const char *)program_invocation_short_name);
puts("Read from stdin, colorize text matching <pattern>, output to stdout.");
puts("\t-b -- bold");
puts("\t-f -- force color output even when stdout is not terminal");
puts("\t-i -- ignore case");
puts("\t<fg>, <bg> -- foreground and background colors");
puts("\t<pattern> -- extended regular expression");
exit(status);
}

original_code:
static void
usage(int exitcode)
{
 extern char *__progname;
 printf("usage: %s [-b] [-i] [<fg>]:[<bg>] <pattern>\n", __progname);
 printf("Read from stdin, colorize text matching <pattern>, output to stdout.\n");
 printf("	-b -- bold\n");
 printf("	-f -- force color output even when stdout is not terminal\n");
 printf("	-i -- ignore case\n");
 printf("	<fg>, <bg> -- foreground and background colors\n");
 printf("	<pattern> -- extended regular expression\n");
 exit(exitcode);
}

variable_types: exitcode int


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

if ( ((*__ctype_b_loc())[a1] & 0x8000u) != 0 )
result = printf("'%c'\n", (unsigned int)a1);
else
result = puts(".");
return result;
}

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

if ( ((*__ctype_b_loc())[a1] & 0x8000u) != 0 )
result = printf("'%c'\n", (unsigned int)a1);
else
result = puts(".");
return result;
}

original_code:
static void
print_chr(unsigned long long n)
{
 if (n > ~(unsigned char)0)
  printf("U+%04x\n", (unsigned int) n);
 else if (((*__ctype_b_loc ())[(int) (((char)n))] & (unsigned short int) _ISgraph))
  printf("'%c'\n", (char)n);
 else
  printf(".\n");
}

canonical_original_code:
static void print_chr(unsigned long long n)
{
 if (n > ~(unsigned char)0)
  printf("U+%04x\n", (unsigned int) n);
 else if (((*__ctype_b_loc ())[(int) (((char)n))] & (unsigned short) _ISgraph))
  printf("'%c'\n", (char)n);
 else
  printf(".\n");
}

variable_types:
n
unsigned long long


decompiled_code:
__int64 __fastcall int2digit(unsigned int a1)
{
__int64 result; // rax

result = 63LL;
if ( a1 <= 0xF )
result = LOBYTE((*__ctype_toupper_loc())[digits[a1]]);
return result;
}

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

result = 63LL;
if ( a1 <= 0xF )
result = LOBYTE((*__ctype_toupper_loc())[digits[a1]]);
return result;
}

original_code:
static char
int2digit(int i)
{
 if (i < 0 || i >= (sizeof(digits)/sizeof((digits)[0])))
  return '?';
 return toupper(digits[i]);
}

variable_types: i int

return_type: char


decompiled_code:
int __fastcall print_num(unsigned __int64 a1, int a2, const char *a3)
{
unsigned __int64 v4; // rbx
char *v5; // r14
char v6; // al
unsigned __int64 v7; // rdx
char v9; // [rsp+40h] [rbp-38h] BYREF

v9 = 0;
if ( !a1 )
goto LABEL_6;
v4 = a1;
v5 = &v9;
do
{
--v5;
v6 = int2digit(v4 % a2);
v7 = v4;
v4 /= (unsigned __int64)a2;
*v5 = v6;
}
while ( a2 <= v7 );
if ( !v6 )
LABEL_6:
v5 = "0";
return printf(a3, v5);
}

canonical_decompiled_code:
int __fastcall func10(unsigned __int64 a1, int a2, const char *a3)
{
unsigned __int64 v4; // rbx
char *v5; // r14
char v6; // al
unsigned __int64 v7; // rdx
char v9; // [rsp+40h] [rbp-38h] BYREF

v9 = 0;
if ( !a1 )
goto LABEL_6;
v4 = a1;
v5 = &v9;
do
{
--v5;
v6 = func9(v4 % a2);
v7 = v4;
v4 /= (unsigned __int64)a2;
*v5 = v6;
}
while ( a2 <= v7 );
if ( !v6 )
LABEL_6:
v5 = "0";
return printf(a3, v5);
}

original_code:
static void
print_num(unsigned long long n, int base, const char *fmt)
{
 const size_t bufsize = sizeof(n) * 8 + 1;
 char buf[bufsize], *p;
 int rem;
 p = buf + bufsize - 1;
 *p = '\0';
 while (n) {
  rem = n % base;
  *--p = int2digit(rem);
  n /= base;
 }
 printf(fmt, *p == '\0' ? "0" : p);
}

canonical_original_code:
static void print_num(unsigned long long n, int base, char *fmt)
{
 unsigned long bufsize = sizeof(n) * 8 + 1;
 char buf[-1]; char *p;
 int rem;
 p = buf + bufsize - 1;
 *p = '\0';
 while (n) {
  rem = n % base;
  *--p = int2digit(rem);
  n /= base;
 }
 printf(fmt, *p == '\0' ? "0" : p);
}

variable_types: base int

variable_types:
fmt
char *

variable_types:
buf
char [-1]

variable_types:
p
char *

variable_types: rem int


decompiled_code:
int __fastcall print_bin(unsigned __int64 a1)
{
return print_num(a1, 2, "b%s\n");
}

canonical_decompiled_code:
int __fastcall func11(unsigned __int64 a1)
{
return func10(a1, 2, "b%s\n");
}

original_code:
static void
print_bin(unsigned long long n)
{
 print_num(n, 2, "b%s\n");
}


decompiled_code:
int __fastcall print_dec(unsigned __int64 a1)
{
return print_num(a1, 10, "%s\n");
}

canonical_decompiled_code:
int __fastcall func12(unsigned __int64 a1)
{
return func10(a1, 10, "%s\n");
}

original_code:
static void
print_dec(unsigned long long n)
{
 print_num(n, 10, "%s\n");
}


decompiled_code:
int __fastcall print_hex(unsigned __int64 a1)
{
return print_num(a1, 16, "0x%s\n");
}

canonical_decompiled_code:
int __fastcall func13(unsigned __int64 a1)
{
return func10(a1, 16, "0x%s\n");
}

original_code:
static void
print_hex(unsigned long long n)
{
 print_num(n, 16, "0x%s\n");
}


decompiled_code:
int __fastcall print_oct(unsigned __int64 a1)
{
return print_num(a1, 8, "0%s\n");
}

canonical_decompiled_code:
int __fastcall func14(unsigned __int64 a1)
{
return func10(a1, 8, "0%s\n");
}

original_code:
static void
print_oct(unsigned long long n)
{
 print_num(n, 8, "0%s\n");
}


decompiled_code:
__int64 __fastcall digit2int(char a1)
{
const __int32_t **v1; // rax
char v2; // dl
char v3; // si
__int64 v4; // rax

v1 = __ctype_tolower_loc();
v2 = 48;
v3 = (*v1)[a1];
v4 = 0LL;
while ( v3 != v2 )
{
if ( ++v4 == 16 )
return 0xFFFFFFFFLL;
v2 = digits[v4];
}
return (unsigned int)digit_values[(int)v4];
}

canonical_decompiled_code:
__int64 __fastcall func15(char a1)
{
const __int32_t **v1; // rax
char v2; // dl
char v3; // si
__int64 v4; // rax

v1 = __ctype_tolower_loc();
v2 = 48;
v3 = (*v1)[a1];
v4 = 0LL;
while ( v3 != v2 )
{
if ( ++v4 == 16 )
return 0xFFFFFFFFLL;
v2 = digits[v4];
}
return (unsigned int)digit_values[(int)v4];
}

original_code:
static int
digit2int(char c)
{
 int i;
 c = tolower(c);
 for (i = 0; i < (sizeof(digits)/sizeof((digits)[0])); ++i)
  if (digits[i] == c)
   return digit_values[i];
 return -1;
}

variable_types: c char


decompiled_code:
__int64 __fastcall read_num(const char *a1, int a2, const char *a3)
{
char v3; // bl
__int64 v4; // rbp
char *v5; // r15
const unsigned __int16 *v6; // r13
unsigned int v7; // eax

v3 = *a1;
if ( !*a1 )
return 0LL;
v4 = 0LL;
v5 = (char *)a1;
v6 = *__ctype_b_loc();
do
{
if ( (v6[v3] & 0x1000) == 0 )
errx(1, "%s: not a %s number", a1, a3);
++v5;
v7 = digit2int(v3);
v3 = *v5;
v4 = v7 + a2 * v4;
}
while ( *v5 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func16(const char *a1, int a2, const char *a3)
{
char v3; // bl
__int64 v4; // rbp
char *v5; // r15
const unsigned __int16 *v6; // r13
unsigned int v7; // eax

v3 = *a1;
if ( !*a1 )
return 0LL;
v4 = 0LL;
v5 = (char *)a1;
v6 = *__ctype_b_loc();
do
{
if ( (v6[v3] & 0x1000) == 0 )
errx(1, "%s: not a %s number", a1, a3);
++v5;
v7 = func15(v3);
v3 = *v5;
v4 = v7 + a2 * v4;
}
while ( *v5 );
return v4;
}

original_code:
static unsigned long long
read_num(const char *s, int base, const char *name)
{
 const char *p = s;
 unsigned long long i = 0;
 while (*p != '\0' && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISxdigit)) {
  i *= base;
  i += (unsigned) digit2int(*p);
  ++p;
 }
 if (*p != '\0')
  errx(1, "%s: not a %s number", s, name ? name : "valid");
 return i;
}

canonical_original_code:
static unsigned long long read_num(char *s, int base, char *name)
{
 char *p = s;
 unsigned long long i = 0;
 while (*p != '\0' && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISxdigit)) {
  i *= base;
  i += (unsigned int) digit2int(*p);
  ++p;
 }
 if (*p != '\0')
  errx(1, "%s: not a %s number", s, name ? name : "valid");
 return i;
}

variable_types:
s
char *

variable_types:
name
char *

variable_types:
i
unsigned long long

return_type: unsigned long long


decompiled_code:
__int64 __fastcall read_bin(const char *a1)
{
return read_num(a1, 2, "binary");
}

canonical_decompiled_code:
__int64 __fastcall func17(const char *a1)
{
return func16(a1, 2, "binary");
}

original_code:
unsigned long long
read_bin(const char *s)
{
 return read_num(s, 2, "binary");
}

canonical_original_code:
unsigned long long read_bin(char *s)
{
 return read_num(s, 2, "binary");
}


decompiled_code:
__int64 __fastcall read_dec(const char *a1)
{
return read_num(a1, 10, "decimal");
}

canonical_decompiled_code:
__int64 __fastcall func18(const char *a1)
{
return func16(a1, 10, "decimal");
}

original_code:
unsigned long long
read_dec(const char *s)
{
 return read_num(s, 10, "decimal");
}

canonical_original_code:
unsigned long long read_dec(char *s)
{
 return read_num(s, 10, "decimal");
}


decompiled_code:
__int64 __fastcall read_hex(const char *a1)
{
return read_num(a1, 16, "hexadecimal");
}

canonical_decompiled_code:
__int64 __fastcall func19(const char *a1)
{
return func16(a1, 16, "hexadecimal");
}

original_code:
unsigned long long
read_hex(const char *s)
{
 return read_num(s, 16, "hexadecimal");
}

canonical_original_code:
unsigned long long read_hex(char *s)
{
 return read_num(s, 16, "hexadecimal");
}


decompiled_code:
__int64 __fastcall read_oct(const char *a1)
{
return read_num(a1, 8, "octal");
}

canonical_decompiled_code:
__int64 __fastcall func20(const char *a1)
{
return func16(a1, 8, "octal");
}

original_code:
unsigned long long
read_oct(const char *s)
{
 return read_num(s, 8, "octal");
}

canonical_original_code:
unsigned long long read_oct(char *s)
{
 return read_num(s, 8, "octal");
}


hex

decompiled_code:
int __fastcall hex(const char *a1)
{
__int64 v1; // rbp
char v2; // al
__int64 v3; // rbp

v1 = *a1;
if ( (_BYTE)v1 == 88 )
goto LABEL_10;
if ( (char)v1 > 88 )
{
if ( (_BYTE)v1 == 98 )
{
LABEL_12:
v3 = read_bin(a1 + 1);
goto LABEL_7;
}
if ( (_BYTE)v1 != 120 )
{
LABEL_13:
if ( ((*__ctype_b_loc())[v1] & 0x800) == 0 )
usage(a1);
goto LABEL_14;
}
LABEL_10:
v3 = read_hex(a1 + 1);
goto LABEL_7;
}
if ( (_BYTE)v1 != 48 )
{
if ( (_BYTE)v1 == 66 )
goto LABEL_12;
goto LABEL_13;
}
v2 = a1[1];
if ( !v2 )
{
LABEL_14:
v3 = read_dec(a1);
goto LABEL_7;
}
if ( (v2 & 0xDF) == 88 )
v3 = read_hex(a1 + 2);
else
v3 = read_oct(a1 + 1);
LABEL_7:
print_bin(v3);
print_chr(v3);
print_dec(v3);
print_hex(v3);
return print_oct(v3);
}

original_code:
static void
hex(const char *s)
{
 unsigned long long i = 0;
 switch (s[0]) {
 case 'b':
 case 'B':
  i = read_bin(s + 1);
  break;
 case 'x':
 case 'X':
  i = read_hex(s + 1);
  break;
 case '0':
  if (s[1] == '\0')
   i = read_dec(s);
  else if (s[1] == 'x' || s[1] == 'X')
   i = read_hex(s + 2);
  else
   i = read_oct(s + 1);
  break;
 default:
  if (((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short int) _ISdigit))
   i = read_dec(s);
  else
   usage();
  break;
 }
 print_bin(i);
 print_chr(i);
 print_dec(i);
 print_hex(i);
 print_oct(i);
}

canonical_original_code:
static void hex(char *s)
{
 unsigned long long i = 0;
 switch (s[0]) {
 case 'b':
 case 'B':
  i = read_bin(s + 1);
  break;
 case 'x':
 case 'X':
  i = read_hex(s + 1);
  break;
 case '0':
  if (s[1] == '\0')
   i = read_dec(s);
  else if (s[1] == 'x' || s[1] == 'X')
   i = read_hex(s + 2);
  else
   i = read_oct(s + 1);
  break;
 default:
  if (((*__ctype_b_loc ())[(int) ((*s))] & (unsigned short) _ISdigit))
   i = read_dec(s);
  else
   usage();
  break;
 }
 print_bin(i);
 print_chr(i);
 print_dec(i);
 print_hex(i);
 print_oct(i);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
const char **v4; // rbx
__int64 v5; // rdi

if ( argc == 1 )
return 0;
v3 = &argv[(unsigned int)(argc - 2) + 2];
v4 = argv + 1;
do
{
v5 = (__int64)*v4++;
hex(v5, argv, envp);
}
while ( v4 != v3 );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbp
const char **v4; // rbx
__int64 v5; // rdi

if ( argc == 1 )
return 0;
v3 = &argv[(unsigned int)(argc - 2) + 2];
v4 = argv + 1;
do
{
v5 = (__int64)*v4++;
func21(v5, argv, envp);
}
while ( v4 != v3 );
return 0;
}

original_code:
int
main(int argc, char **argv)
{
 while (++argv, --argc)
  hex(*argv);
 return 0;
}


usage

decompiled_code:
void __noreturn usage()
{
puts("usage: hex <number>");
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("usage: hex <number>");
exit(1);
}

original_code:
static void
usage(void)
{
 printf("usage: hex <number>\n");
 exit(1);
}

canonical_original_code:
static void usage()
{
 printf("usage: hex <number>\n");
 exit(1);
}


decompiled_code:
__int64 start_dlist()
{
__int64 i; // rax
__int64 result; // rax

for ( i = 0LL; i != 1296; ++i )
dlist[i] = i;
result = 0LL;
dlist_length = 1296;
memset(&order, 0, 0x1440uLL);
return result;
}

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

for ( i = 0LL; i != 1296; ++i )
dlist[i] = i;
result = 0LL;
dlist_length = 1296;
memset(&order, 0, 0x1440uLL);
return result;
}

original_code:
void start_dlist()
{
  for (int i = 0; i < 1296; i++)
  {
    dlist[i] = i;
    order[i] = 0;
  }
  dlist_length = 1296;
}


decompiled_code:
__int64 shuffle_dlist()
{
unsigned int v0; // ebp
int *v1; // rbx
unsigned int v2; // eax
int v3; // ecx
__int64 v4; // rdx
__int64 result; // rax

v0 = 1296;
v1 = (int *)(unsigned int)((_DWORD)dlist + 5180);
do
{
v2 = rand();
v3 = *v1--;
v4 = v2 % v0--;
result = (unsigned int)dlist[v4];
v1[1] = result;
dlist[(unsigned int)v4] = v3;
}
while ( v1 != dlist );
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebp
int *v1; // rbx
unsigned int v2; // eax
int v3; // ecx
__int64 v4; // rdx
__int64 result; // rax

v0 = 1296;
v1 = (int *)(unsigned int)((_DWORD)dlist + 5180);
do
{
v2 = rand();
v3 = *v1--;
v4 = v2 % v0--;
result = (unsigned int)dlist[v4];
v1[1] = result;
dlist[(unsigned int)v4] = v3;
}
while ( v1 != dlist );
return result;
}

original_code:
void shuffle_dlist()
{
  for (unsigned int i = 1296 - 1; i > 0; i--)
  {
    unsigned int j = rand() % (i+1);
    dex tmp = dlist[i];
    dlist[i] = dlist[j];
    dlist[j] = tmp;
  }
}

canonical_original_code:
void shuffle_dlist()
{
  for (unsigned int i = 1296 - 1; i > 0; i--)
  {
    unsigned int j = rand() % (i+1);
    unsigned int tmp = dlist[i];
    dlist[i] = dlist[j];
    dlist[j] = tmp;
  }
}

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
tmp
unsigned int


decompiled_code:
void filter_dlist()
{
unsigned int v0; // edx
__int64 v1; // rcx
__int64 v2; // rax
__int64 v3; // rsi

v0 = dlist_length;
if ( dlist_length )
{
v1 = (unsigned int)dlist_length;
v2 = 0LL;
v0 = 0;
do
{
if ( order[v2] )
{
v3 = v0++;
dlist[v3] = dlist[v2];
}
++v2;
}
while ( v2 != v1 );
}
dlist_length = v0;
}

canonical_decompiled_code:
void func2()
{
unsigned int v0; // edx
__int64 v1; // rcx
__int64 v2; // rax
__int64 v3; // rsi

v0 = dlist_length;
if ( dlist_length )
{
v1 = (unsigned int)dlist_length;
v2 = 0LL;
v0 = 0;
do
{
if ( order[v2] )
{
v3 = v0++;
dlist[v3] = dlist[v2];
}
++v2;
}
while ( v2 != v1 );
}
dlist_length = v0;
}

original_code:
void filter_dlist()
{
  unsigned int j = 0;
  for (unsigned int i = 0; i < dlist_length; i++)
  {
    if (order[i])
    {
      dlist[j] = dlist[i];
      j++;
    }
  }
  dlist_length = j;
}


decompiled_code:
void sort_dlist()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void sort_dlist()
{
}

canonical_original_code:
void sort_dlist()
{
}


run

decompiled_code:
__int64 __fastcall run(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // er14
char v9[21]; // [rsp+Bh] [rbp-4Dh] BYREF
char v10[56]; // [rsp+20h] [rbp-38h] BYREF

if ( !ai_name[a1] )
{
printf("AI %d is not initialised.\n", a1);
puts("\n< aborted >");
exit(-1);
}
v2 = a2;
v3 = 8;
strcpy(v9, "1234");
v4 = rand();
codex(v10, (unsigned int)(v4 % 1296));
((void (*)(void))init[a1])();
while ( 1 )
{
while ( 1 )
{
((void (__fastcall *)(char *))guess[a1])(&v9[5]);
if ( (_BYTE)a2 )
break;
v5 = evaluate(&v9[5], v10);
if ( v5 == 16 )
{
v2 = 1;
goto LABEL_9;
}
((void (__fastcall *)(_QWORD))reward[a1])(v5);
if ( !--v3 )
goto LABEL_8;
}
stringify(&v9[5], v9);
printf("%s: %s\n", (const char *)ai_name[a1], v9);
v6 = evaluate(&v9[5], v10);
v7 = v6;
if ( v6 == 16 )
break;
printf("->\t\t[ %d exact, %d about ]\n", v6 >> 2, v6 & 3);
((void (__fastcall *)(_QWORD))reward[a1])(v7);
putchar(10);
if ( !--v3 )
{
LABEL_8:
v2 = 0;
goto LABEL_9;
}
}
puts("->\t\tCorrect!");
LABEL_9:
((void (*)(void))end[a1])();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // ebx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // er14
char v9[21]; // [rsp+Bh] [rbp-4Dh] BYREF
char v10[56]; // [rsp+20h] [rbp-38h] BYREF

if ( !ai_name[a1] )
{
printf("AI %d is not initialised.\n", a1);
puts("\n< aborted >");
exit(-1);
}
v2 = a2;
v3 = 8;
strcpy(v9, "1234");
v4 = rand();
codex(v10, (unsigned int)(v4 % 1296));
((void (*)(void))init[a1])();
while ( 1 )
{
while ( 1 )
{
((void (__fastcall *)(char *))guess[a1])(&v9[5]);
if ( (_BYTE)a2 )
break;
v5 = evaluate(&v9[5], v10);
if ( v5 == 16 )
{
v2 = 1;
goto LABEL_9;
}
((void (__fastcall *)(_QWORD))reward[a1])(v5);
if ( !--v3 )
goto LABEL_8;
}
stringify(&v9[5], v9);
printf("%s: %s\n", (const char *)ai_name[a1], v9);
v6 = evaluate(&v9[5], v10);
v7 = v6;
if ( v6 == 16 )
break;
printf("->\t\t[ %d exact, %d about ]\n", v6 >> 2, v6 & 3);
((void (__fastcall *)(_QWORD))reward[a1])(v7);
putchar(10);
if ( !--v3 )
{
LABEL_8:
v2 = 0;
goto LABEL_9;
}
}
puts("->\t\tCorrect!");
LABEL_9:
((void (*)(void))end[a1])();
return v2;
}

original_code:
_Bool run(const AI ai, const _Bool verbose)
{
  if (! ai_name[ai])
  {
    printf("AI %d is not initialised.\n", ai);
    printf("\n< aborted >\n");
    exit(-1);
  }
  code attempt, master;
  int turn;
  score S = 0;
  char str[5] = "1234";
  codex(master, rand() % 1296);
  init[ai]();
  for (turn = 1; turn <= 8; turn++)
  {
    guess[ai](attempt);
    if (verbose)
    {
      stringify(attempt, str);
      printf("%s: %s\n", ai_name[ai], str);
    }
    S = evaluate(attempt, master);
    if (S == 16)
    {
      if (verbose)
      {
        printf("->\t\tCorrect!\n");
      }
      break;
    }
    else
    {
      if (verbose)
      {
        printf("->\t\t[ %d exact, %d about ]\n", ((S) / 4), ((S) % 4));
      }
      reward[ai](S);
    }
    if (verbose)
    {
      printf("\n");
    }
  }
  end[ai]();
  return (S == 16);
}

canonical_original_code:
_Bool run(enum AI ai, _Bool verbose)
{
  if (! ai_name[ai])
  {
    printf("AI %d is not initialised.\n", ai);
    printf("\n< aborted >\n");
    exit(-1);
  }
  enum digit attempt[4]; enum digit master[4];
  int turn;
  unsigned int S = 0;
  char str[5] = "1234";
  codex(master, rand() % 1296);
  init[ai]();
  for (turn = 1; turn <= 8; turn++)
  {
    guess[ai](attempt);
    if (verbose)
    {
      stringify(attempt, str);
      printf("%s: %s\n", ai_name[ai], str);
    }
    S = evaluate(attempt, master);
    if (S == 16)
    {
      if (verbose)
      {
        printf("->\t\tCorrect!\n");
      }
      break;
    }
    else
    {
      if (verbose)
      {
        printf("->\t\t[ %d exact, %d about ]\n", ((S) / 4), ((S) % 4));
      }
      reward[ai](S);
    }
    if (verbose)
    {
      printf("\n");
    }
  }
  end[ai]();
  return (S == 16);
}

variable_types: enum AI { RANDY=0, JIMMY=1, PHILIP=2, N_AI=3 }

variable_types: turn int

variable_types:
S
unsigned int

variable_types:
str
char [5]


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
time_t v4; // rdi
unsigned int v5; // er12
int v6; // ebx

v3 = 0LL;
puts("[ Mastermind ]\n\n");
v4 = time(0LL);
srand(v4);
ai_setup(v4, argv);
while ( 1 )
{
v5 = 0;
v6 = 1000000;
printf("It's now %s's turn.\n", (const char *)ai_name[v3]);
do
{
v5 -= ((unsigned __int8)run(v3, 0) == 0) - 1;
--v6;
}
while ( v6 );
printf("%s's success rate: %d / %d.\n", (const char *)ai_name[v3], v5, 1000000LL);
if ( v3 == 2 )
break;
do
++v3;
while ( v3 == 1 );
}
puts("\n\n[ done ]");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r13
time_t v4; // rdi
unsigned int v5; // er12
int v6; // ebx

v3 = 0LL;
puts("[ Mastermind ]\n\n");
v4 = time(0LL);
srand(v4);
ai_setup(v4, argv);
while ( 1 )
{
v5 = 0;
v6 = 1000000;
printf("It's now %s's turn.\n", (const char *)ai_name[v3]);
do
{
v5 -= ((unsigned __int8)func0(v3, 0) == 0) - 1;
--v6;
}
while ( v6 );
printf("%s's success rate: %d / %d.\n", (const char *)ai_name[v3], v5, 1000000LL);
if ( v3 == 2 )
break;
do
++v3;
while ( v3 == 1 );
}
puts("\n\n[ done ]");
return 0;
}

original_code:
int main(int argc, char** argv)
{
    printf("[ Mastermind ]\n\n\n");
    srand(time(((void *)0)));
    ai_setup();
    unsigned int tries = 1000000;
    unsigned int successes;
    for (int ai = 0; ai < N_AI; ai++)
    {
      if (ai == JIMMY)
        continue;
      successes = 0;
      printf("It's now %s's turn.\n", ai_name[ai]);
      for (int t = 0; t < tries; t++)
      {
        if (run(ai, 0))
          successes++;
      }
      printf("%s's success rate: %d / %d.\n", ai_name[ai], successes, tries);
    }
    printf("\n\n[ done ]\n");
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    printf("[ Mastermind ]\n\n\n");
    srand(time(((void *)0)));
    ai_setup();
    unsigned int tries = 1000000;
    unsigned int successes;
    for (int ai = 0; ai < N_AI; ai++)
    {
      if (ai == JIMMY)
        continue;
      successes = 0;
      printf("It's now %s's turn.\n", ai_name[ai]);
      for (int t = 0; t < tries; t++)
      {
        if (run(ai, 0))
          successes++;
      }
      printf("%s's success rate: %d / %d.\n", ai_name[ai], successes, tries);
    }
    printf("\n\n[ done ]\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
tries
unsigned int

variable_types:
successes
unsigned int

variable_types: ai int

variable_types: t int


evaluate

original_code:
score evaluate(const code attempt, const code master)
{
  score S = 0;
  code att, mas;
  for (int i = 0; i < 4; i++)
  {
    att[i] = attempt[i];
    mas[i] = master[i];
  }
  for (int i = 0; i < 4; i++)
  {
    if (att[i] == mas[i])
    {
      S += 4;
      att[i] = X;
      mas[i] = X;
    }
  }
  for (int i = 0; i < 4; i++)
  {
    if (att[i] != X)
    {
      for (int j = 0; j < 4; j++)
      {
        if (mas[j] == att[i])
        {
          S += 1;
          mas[j] = X;
          att[i] = X;
          break;
        }
      }
    }
  }
  return S;
}

canonical_original_code:
unsigned int evaluate(enum digit attempt[4], enum digit master[4])
{
  unsigned int S = 0;
  enum digit att[4]; enum digit mas[4];
  for (int i = 0; i < 4; i++)
  {
    att[i] = attempt[i];
    mas[i] = master[i];
  }
  for (int i = 0; i < 4; i++)
  {
    if (att[i] == mas[i])
    {
      S += 4;
      att[i] = X;
      mas[i] = X;
    }
  }
  for (int i = 0; i < 4; i++)
  {
    if (att[i] != X)
    {
      for (int j = 0; j < 4; j++)
      {
        if (mas[j] == att[i])
        {
          S += 1;
          mas[j] = X;
          att[i] = X;
          break;
        }
      }
    }
  }
  return S;
}

variable_types: i int

variable_types: j int

return_type: unsigned int


codex

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

result = 0LL;
if ( a2 <= 0x50F )
{
v3 = a1 + 16;
do
{
a1 += 4LL;
*(_DWORD *)(a1 - 4) = a2 % 6;
a2 /= 6u;
}
while ( a1 != v3 );
result = 1LL;
}
return result;
}

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

result = 0LL;
if ( a2 <= 0x50F )
{
v3 = a1 + 16;
do
{
a1 += 4LL;
*(_DWORD *)(a1 - 4) = a2 % 6;
a2 /= 6u;
}
while ( a1 != v3 );
result = 1LL;
}
return result;
}

original_code:
_Bool codex(code a, dex j)
{
  if (j >= 1296)
    return 0;
  for (int i = 0; i < 4; i++)
  {
    a[i] = j % 6;
    j /= 6;
  }
  return 1;
}

canonical_original_code:
_Bool codex(enum digit a[4], unsigned int j)
{
  if (j >= 1296)
    return 0;
  for (int i = 0; i < 4; i++)
  {
    a[i] = j % 6;
    j /= 6;
  }
  return 1;
}


fill

original_code:
_Bool fill(code a, const char* str)
{
  char c;
  for (int i = 0; i < 4; i++)
  {
    c = str[i];
    if (c >= 'a' && c <= 'f')
    {
      a[i] = (digit) (c - 'a');
    }
    else if (c >= 'A' && c <= 'F')
    {
      a[i] = (digit) (c - 'A');
    }
    else
    {
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
_Bool fill(enum digit a[4], char *str)
{
  char c;
  for (int i = 0; i < 4; i++)
  {
    c = str[i];
    if (c >= 'a' && c <= 'f')
    {
      a[i] = (digit) (c - 'a');
    }
    else if (c >= 'A' && c <= 'F')
    {
      a[i] = (digit) (c - 'A');
    }
    else
    {
      return 0;
    }
  }
  return 1;
}

variable_types:
str
char *

variable_types: c char


stringify

decompiled_code:
__int64 __fastcall stringify(__int64 a1, const char *a2)
{
size_t v2; // r8
__int64 result; // rax
__int64 i; // rax
unsigned int v5; // edx
bool v6; // zf
char v7; // dl

v2 = strlen(a2);
result = 0LL;
if ( v2 > 3 )
{
for ( i = 0LL; i != 4; ++i )
{
v5 = *(_DWORD *)(a1 + 4 * i);
if ( v5 > 5 )
{
v6 = v5 == 6;
v7 = 88;
if ( !v6 )
v7 = 36;
a2[i] = v7;
}
else
{
a2[i] = v5 + 65;
}
}
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2)
{
size_t v2; // r8
__int64 result; // rax
__int64 i; // rax
unsigned int v5; // edx
bool v6; // zf
char v7; // dl

v2 = strlen(a2);
result = 0LL;
if ( v2 > 3 )
{
for ( i = 0LL; i != 4; ++i )
{
v5 = *(_DWORD *)(a1 + 4 * i);
if ( v5 > 5 )
{
v6 = v5 == 6;
v7 = 88;
if ( !v6 )
v7 = 36;
a2[i] = v7;
}
else
{
a2[i] = v5 + 65;
}
}
result = 1LL;
}
return result;
}

original_code:
_Bool stringify(const code a, char* str)
{
  if (strlen(str) < 4)
  {
    return 0;
  }
  for (int i = 0; i < 4; i++)
  {
    if (a[i] >= A && a[i] <= F)
    {
      str[i] = ((char) (a[i] - A)) + 'A';
    }
    else if (a[i] == X)
    {
      str[i] = 'X';
    }
    else
    {
      str[i] = '$';
    }
  }
  return 1;
}

canonical_original_code:
_Bool stringify(enum digit a[4], char *str)
{
  if (strlen(str) < 4)
  {
    return 0;
  }
  for (int i = 0; i < 4; i++)
  {
    if (a[i] >= A && a[i] <= F)
    {
      str[i] = ((char) (a[i] - A)) + 'A';
    }
    else if (a[i] == X)
    {
      str[i] = 'X';
    }
    else
    {
      str[i] = '$';
    }
  }
  return 1;
}


decompiled_code:
__int64 __fastcall random_guess(__int64 a1)
{
int v1; // eax

v1 = rand();
return codex(a1, (unsigned int)(v1 % 1296));
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax

v1 = rand();
return codex(a1, (unsigned int)(v1 % 1296));
}

original_code:
void random_guess(code attempt)
{
  codex(attempt, rand() % 1296);
}

canonical_original_code:
void random_guess(enum digit attempt[4])
{
  codex(attempt, rand() % 1296);
}


decompiled_code:
__int64 next_guess()
{
__int64 v0; // rax
__int64 result; // rax

v0 = (unsigned int)i_0;
if ( i_0 >= (unsigned int)dlist_length )
{
i_0 = 0;
v0 = 0LL;
}
philips_dex = dlist[v0];
result = codex();
++i_0;
return result;
}

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

v0 = (unsigned int)i_0;
if ( i_0 >= (unsigned int)dlist_length )
{
i_0 = 0;
v0 = 0LL;
}
philips_dex = dlist[v0];
result = codex();
++i_0;
return result;
}

original_code:
void next_guess(code attempt)
{
  static unsigned int i = 0;
  if (i >= dlist_length)
  {
    i = 0;
  }
  philips_dex = dlist[i];
  codex(attempt, philips_dex);
  i++;
}

canonical_original_code:
void next_guess(enum digit attempt[4])
{
  static unsigned int i = 0;
  if (i >= dlist_length)
  {
    i = 0;
  }
  philips_dex = dlist[i];
  codex(attempt, philips_dex);
  i++;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall filter_evalscore(int a1)
{
__int64 v1; // rbx
_BYTE v3[16]; // [rsp+0h] [rbp-38h] BYREF
char v4[40]; // [rsp+10h] [rbp-28h] BYREF

codex(v3, (unsigned int)philips_dex);
if ( dlist_length )
{
v1 = 0LL;
do
{
codex(v4, (unsigned int)dlist[v1]);
order[v1++] = evaluate(v3, v4) == a1;
}
while ( dlist_length > (unsigned int)v1 );
}
return filter_dlist();
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 v1; // rbx
_BYTE v3[16]; // [rsp+0h] [rbp-38h] BYREF
char v4[40]; // [rsp+10h] [rbp-28h] BYREF

codex(v3, (unsigned int)philips_dex);
if ( dlist_length )
{
v1 = 0LL;
do
{
codex(v4, (unsigned int)dlist[v1]);
order[v1++] = evaluate(v3, v4) == a1;
}
while ( dlist_length > (unsigned int)v1 );
}
return filter_dlist();
}

original_code:
void filter_evalscore(score S)
{
  code philips_guess, supposed_master;
  codex(philips_guess, philips_dex);
  for (int i = 0; i < dlist_length; i++)
  {
    codex(supposed_master, dlist[i]);
    order[i] = (evaluate(philips_guess, supposed_master) == S);
  }
  filter_dlist();
}

canonical_original_code:
void filter_evalscore(unsigned int S)
{
  enum digit philips_guess[4]; enum digit supposed_master[4];
  codex(philips_guess, philips_dex);
  for (int i = 0; i < dlist_length; i++)
  {
    codex(supposed_master, dlist[i]);
    order[i] = (evaluate(philips_guess, supposed_master) == S);
  }
  filter_dlist();
}


decompiled_code:
void empty_init()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void empty_init()
{
}

canonical_original_code:
void empty_init()
{
}


decompiled_code:
void empty_reward()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void empty_reward(score S)
{
}

canonical_original_code:
void empty_reward(unsigned int S)
{
}


decompiled_code:
void empty_end()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void empty_end()
{
}

canonical_original_code:
void empty_end()
{
}


decompiled_code:
void empty_guess()
{
;
}

original_code:
void empty_guess(code attempt)
{
}

canonical_original_code:
void empty_guess(enum digit attempt[4])
{
}


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

start_dlist();
result = shuffle_dlist();
jimmy = 0;
return result;
}

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

start_dlist();
result = shuffle_dlist();
jimmy = 0;
return result;
}

original_code:
void init_random_dlist()
{
  start_dlist();
  shuffle_dlist();
  jimmy = 0;
}


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

result = codex(a1, (unsigned int)dlist[jimmy]);
++jimmy;
return result;
}

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

result = codex(a1, (unsigned int)dlist[jimmy]);
++jimmy;
return result;
}

original_code:
void random_listed_guess(code attempt)
{
  codex(attempt, dlist[jimmy]);
  jimmy++;
}

canonical_original_code:
void random_listed_guess(enum digit attempt[4])
{
  codex(attempt, dlist[jimmy]);
  jimmy++;
}


start

decompiled_code:
// positive sp value has been detected, the output may be wrong!
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void))
{
__int64 v3; // rax
int v4; // esi
__int64 v5; // [rsp-8h] [rbp-8h] BYREF
char *retaddr; // [rsp+0h] [rbp+0h] BYREF

v4 = v5;
v5 = v3;
_libc_start_main(
(int (__fastcall *)(int, char **, char **))main,
v4,
&retaddr,
_libc_csu_init,
_libc_csu_fini,
a3,
&v5);
__halt();
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1, __int64 a2, void (*a3)(void))
{
__int64 v3; // rax
int v4; // esi
__int64 v5; // [rsp-8h] [rbp-8h] BYREF
char *retaddr; // [rsp+0h] [rbp+0h] BYREF

v4 = v5;
v5 = v3;
_libc_start_main(
(int (__fastcall *)(int, char **, char **))main,
v4,
&retaddr,
_libc_csu_init,
_libc_csu_fini,
a3,
&v5);
__halt();
}

original_code:
void start()
{
  if (State == 1)
  {
    State = 2;
  }
  else
  {
    printf("{ warning } Sieve not yet ready or already started.\n");
  }
}


decompiled_code:
__int64 get_flag_in_block()
{
return 3LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 3LL;
}

original_code:
char get_flag_in_block(block *B, unsigned int d)
{
  return 3;
}

canonical_original_code:
char get_flag_in_block(struct block *B, unsigned int d)
{
  return 3;
}

variable_types: struct block { unsigned int part[16]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *blocklock; } *

variable_types:
d
unsigned int

user_defined_types: struct block { unsigned int part[16]; union pthread_mutex_t *blocklock; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
// attributes: thunk
__int64 set_flag_in_block()
{
return get_flag_in_block();
}

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

original_code:
char set_flag_in_block(block* B, unsigned int d)
{
  return 3;
}

canonical_original_code:
char set_flag_in_block(struct block *B, unsigned int d)
{
  return 3;
}

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 get_flag()
{
return 3LL;
}

canonical_decompiled_code:
__int64 func9()
{
return 3LL;
}

original_code:
char get_flag(unsigned int d)
{
  if ((d / (sizeof(int) * 8 * 16 / 2)) >= nBlocks)
  {
  return 3;
  }
  return 3;
}


decompiled_code:
__int64 set_flag()
{
return 3LL;
}

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

original_code:
char set_flag(unsigned int d, char f)
{
  if ((d / (sizeof(int) * 8 * 16 / 2)) >= nBlocks)
  {
  return 3;
  }
  return 3;
}

variable_types: f char


canonical_original_code:
void start()
{
  if (State == 1)
  {
    State = 2;
  }
  else
  {
    printf("{ warning } Sieve not yet ready or already started.\n");
  }
}


filter

decompiled_code:
__int64 __fastcall filter(__int64 a1)
{
unsigned int v1; // ebx
int v2; // ebp
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + ((unsigned __int64)(a1 - 3) >> 1);
if ( v1 <= 0xF423E )
{
v2 = a1;
do
{
v3 = v1;
v1 += v2;
result = setbit(v3, 1LL);
}
while ( v1 <= 0xF423E );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
unsigned int v1; // ebx
int v2; // ebp
__int64 v3; // rdi
__int64 result; // rax

v1 = a1 + ((unsigned __int64)(a1 - 3) >> 1);
if ( v1 <= 0xF423E )
{
v2 = a1;
do
{
v3 = v1;
v1 += v2;
result = func11(v3, 1LL);
}
while ( v1 <= 0xF423E );
}
return result;
}

original_code:
void filter(natural p)
{
  for (unsigned int u = ((p-3) / 2) + p; u <= ((1999999 -3) / 2); u += p)
  {
    setbit(u, 1);
  }
}

canonical_original_code:
void filter(unsigned long p)
{
  for (unsigned int u = ((p-3) / 2) + p; u <= ((1999999 -3) / 2); u += p)
  {
    setbit(u, 1);
  }
}

variable_types:
p
unsigned long

variable_types:
u
unsigned int


decompiled_code:
void *clear_bitmap()
{
return memset(&segm, 0, 0x1E850uLL);
}

canonical_decompiled_code:
void *func8()
{
return memset(&segm, 0, 0x1E850uLL);
}

original_code:
void clear_bitmap()
{
  for (int i = 0; i < (1000000 / (sizeof(long) * 8) + 1); i++)
  {
    segm[i] = 0;
  }
}


decompiled_code:
int print_bitmap()
{
__int64 *v0; // r12
unsigned __int64 v1; // rbp
int v2; // ebx
char v3; // si

v0 = &segm;
puts("[[");
do
{
v1 = *v0;
v2 = 64;
printf("\t :");
do
{
v3 = v1;
v1 >>= 1;
printf("%d", v3 & 1);
--v2;
}
while ( v2 );
++v0;
puts(": ");
}
while ( &end != (_UNKNOWN *)v0 );
return puts("]]");
}

original_code:
void print_bitmap()
{
  segment printer = 0;
  printf("[[\n");
  for (int i = 0; i < (1000000 / (sizeof(long) * 8) + 1); i++)
  {
    printer = segm[i];
    printf("\t :");
    for (int z = 0; z < (sizeof(long) * 8); z++)
    {
      printf("%d", (unsigned int) (printer & 1));
      printer = printer >> 1;
    }
    printf(": \n");
  }
  printf("]]\n");
}

canonical_original_code:
void print_bitmap()
{
  unsigned long printer = 0;
  printf("[[\n");
  for (int i = 0; i < (1000000 / (sizeof(long) * 8) + 1); i++)
  {
    printer = segm[i];
    printf("\t :");
    for (int z = 0; z < (sizeof(long) * 8); z++)
    {
      printf("%d", (unsigned int) (printer & 1));
      printer = printer >> 1;
    }
    printf(": \n");
  }
  printf("]]\n");
}

variable_types:
printer
unsigned long

variable_types: z int


getbit

decompiled_code:
unsigned __int64 __fastcall getbit(unsigned int a1)
{
if ( a1 <= 0xF427F )
return ((unsigned __int64)segm[a1 >> 6] >> a1) & 1;
printf("{ bitmap fault } Index %d out of bounds.\n", a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(unsigned int a1)
{
if ( a1 <= 0xF427F )
return ((unsigned __int64)segm[a1 >> 6] >> a1) & 1;
printf("{ bitmap fault } Index %d out of bounds.\n", a1);
return 0xFFFFFFFFLL;
}

original_code:
bit getbit(unsigned int d)
{
  unsigned int k = d / (sizeof(long) * 8);
  if (k >= (1000000 / (sizeof(long) * 8) + 1))
  {
    printf("{ bitmap fault } Index %d out of bounds.\n", d);
    return -1;
  }
  return ((segm[k] >> (d % (sizeof(long) * 8))) & 1);
}

canonical_original_code:
char getbit(unsigned int d)
{
  unsigned int k = d / (sizeof(long) * 8);
  if (k >= (1000000 / (sizeof(long) * 8) + 1))
  {
    printf("{ bitmap fault } Index %d out of bounds.\n", d);
    return -1;
  }
  return ((segm[k] >> (d % (sizeof(long) * 8))) & 1);
}

variable_types:
k
unsigned int


setbit

decompiled_code:
int __fastcall setbit(unsigned int a1, char a2)
{
__int64 v3; // rdi
__int64 v4; // rax

v3 = a1 >> 6;
if ( a1 > 0xF427F )
{
LODWORD(v4) = printf("{ bitmap fault } Index %d out of bounds.\n", a1);
}
else
{
v4 = (__int64)a2 << a1;
segm[v3] = v4 | ((unsigned __int64)segm[v3] >> a1 << a1) | segm[v3] & ~(-1LL << a1);
}
return v4;
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1, char a2)
{
__int64 v3; // rdi
__int64 v4; // rax

v3 = a1 >> 6;
if ( a1 > 0xF427F )
{
LODWORD(v4) = printf("{ bitmap fault } Index %d out of bounds.\n", a1);
}
else
{
v4 = (__int64)a2 << a1;
segm[v3] = v4 | ((unsigned __int64)segm[v3] >> a1 << a1) | segm[v3] & ~(-1LL << a1);
}
return v4;
}

original_code:
void setbit(unsigned int d, bit b)
{
  unsigned int k = d / (sizeof(long) * 8);
  if (k >= (1000000 / (sizeof(long) * 8) + 1))
  {
    printf("{ bitmap fault } Index %d out of bounds.\n", d);
    return;
  }
  (segm[k] = (((segment) b << (d % (sizeof(long) * 8))) | ((segm[k] >> (d % (sizeof(long) * 8))) << (d % (sizeof(long) * 8))) | (segm[k] % ((segment) 1 << (d % (sizeof(long) * 8))))));
}

canonical_original_code:
void setbit(unsigned int d, char b)
{
  unsigned int k = d / (sizeof(long) * 8);
  if (k >= (1000000 / (sizeof(long) * 8) + 1))
  {
    printf("{ bitmap fault } Index %d out of bounds.\n", d);
    return;
  }
  (segm[k] = (((unsigned long) b << (d % (sizeof(long) * 8))) | ((segm[k] >> (d % (sizeof(long) * 8))) << (d % (sizeof(long) * 8))) | (segm[k] % ((unsigned long) 1 << (d % (sizeof(long) * 8))))));
}

variable_types: b char


find

decompiled_code:
__int64 __fastcall find(unsigned int a1, char a2)
{
unsigned int i; // ebx

if ( a1 > 0xF423F )
return 0xFFFFFFFFLL;
for ( i = a1; i != 1000000; ++i )
{
if ( (unsigned __int8)getbit(i) == a2 )
return i;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, char a2)
{
unsigned int i; // ebx

if ( a1 > 0xF423F )
return 0xFFFFFFFFLL;
for ( i = a1; i != 1000000; ++i )
{
if ( (unsigned __int8)func10(i) == a2 )
return i;
}
return 0xFFFFFFFFLL;
}

original_code:
unsigned int find(unsigned int d, bit b)
{
  for (unsigned int u = d; u < 1000000; u++)
  {
    if (getbit(u) == b)
    {
      return u;
    }
  }
  return ((unsigned int) -1);
}

canonical_original_code:
unsigned int find(unsigned int d, char b)
{
  for (unsigned int u = d; u < 1000000; u++)
  {
    if (getbit(u) == b)
    {
      return u;
    }
  }
  return ((unsigned int) -1);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
__int64 i; // rdi
unsigned int v5; // eax
__int64 v6; // rbx

v3 = 2LL;
puts("[ Project Euler: Problem 10 ]\n\n");
clear_bitmap("[ Project Euler: Problem 10 ]\n\n", argv);
for ( i = 0LL; ; i = v6 + 1 )
{
v5 = find(i, 0LL);
v6 = v5;
if ( v5 > 0xF423E )
break;
v3 += 2LL * v5 + 3;
filter();
}
printf("Total: %lu.\n", v3);
puts("\n\n[ done ]");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
__int64 i; // rdi
unsigned int v5; // eax
__int64 v6; // rbx

v3 = 2LL;
puts("[ Project Euler: Problem 10 ]\n\n");
func8("[ Project Euler: Problem 10 ]\n\n", argv);
for ( i = 0LL; ; i = v6 + 1 )
{
v5 = func12(i, 0LL);
v6 = v5;
if ( v5 > 0xF423E )
break;
v3 += 2LL * v5 + 3;
func7();
}
printf("Total: %lu.\n", v3);
puts("\n\n[ done ]");
return 0;
}

original_code:
int main(int argc, char** argv)
{
    printf("[ Project Euler: Problem 10 ]\n\n\n");
    natural total = 2;
    clear_bitmap();
    natural next_prime;
    natural prime_index = 0;
    while ((prime_index = find(prime_index, 0)) <= ((1999999 -3) / 2))
    {
      next_prime = (2*prime_index + 3);
      total += next_prime;
      filter(next_prime);
      prime_index++;
    }
    printf("Total: %lu.\n", total);
    printf("\n\n[ done ]\n");
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    printf("[ Project Euler: Problem 10 ]\n\n\n");
    unsigned long total = 2;
    clear_bitmap();
    unsigned long next_prime;
    unsigned long prime_index = 0;
    while ((prime_index = find(prime_index, 0)) <= ((1999999 -3) / 2))
    {
      next_prime = (2*prime_index + 3);
      total += next_prime;
      filter(next_prime);
      prime_index++;
    }
    printf("Total: %lu.\n", total);
    printf("\n\n[ done ]\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
total
unsigned long


segment

decompiled_code:
void *__fastcall segment(void *a1)
{
char v1; // bl
unsigned int v2; // er12
__int64 v3; // rbx
char v4; // bp
unsigned __int64 v5; // rax
__int64 v6; // rsi

v1 = State;
if ( State <= 1 )
{
while ( 1 )
sched_yield();
}
v2 = *(_DWORD *)a1;
while ( v1 == 2 )
{
while ( 1 )
{
if ( v2 != 8 )
{
if ( v2 < listlength )
{
v3 = (int)v2;
v4 = 0;
goto LABEL_12;
}
goto LABEL_23;
}
v4 = add(buff[0], transfer[0]);
if ( !v4 )
{
v3 = 8LL;
if ( (unsigned int)listlength > 8 )
goto LABEL_12;
goto LABEL_22;
}
transfer[0] += 2LL;
v3 = 8LL;
v4 = 1;
if ( (unsigned int)listlength > 8 )
break;
LABEL_18:
v1 = State;
if ( State != 2 )
return 0LL;
}
do
{
LABEL_12:
while ( 1 )
{
v6 = transfer[v3];
if ( !v6 )
break;
if ( (unsigned __int8)add(buff[v3], v6) )
{
transfer[v3] = 0LL;
v4 = 1;
break;
}
if ( !transfer[v3] )
break;
v3 += 8LL;
if ( listlength <= (unsigned int)v3 )
goto LABEL_17;
}
v5 = get(buff[(int)v3 - 1]);
if ( v5 && v5 % prime[v3] )
{
transfer[v3] = v5;
v4 = 1;
}
v3 += 8LL;
}
while ( listlength > (unsigned int)v3 );
LABEL_17:
if ( v4 )
goto LABEL_18;
LABEL_22:
v1 = State;
LABEL_23:
sched_yield();
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
char v1; // bl
unsigned int v2; // er12
__int64 v3; // rbx
char v4; // bp
unsigned __int64 v5; // rax
__int64 v6; // rsi

v1 = State;
if ( State <= 1 )
{
while ( 1 )
sched_yield();
}
v2 = *(_DWORD *)a1;
while ( v1 == 2 )
{
while ( 1 )
{
if ( v2 != 8 )
{
if ( v2 < listlength )
{
v3 = (int)v2;
v4 = 0;
goto LABEL_12;
}
goto LABEL_23;
}
v4 = add(buff[0], transfer[0]);
if ( !v4 )
{
v3 = 8LL;
if ( (unsigned int)listlength > 8 )
goto LABEL_12;
goto LABEL_22;
}
transfer[0] += 2LL;
v3 = 8LL;
v4 = 1;
if ( (unsigned int)listlength > 8 )
break;
LABEL_18:
v1 = State;
if ( State != 2 )
return 0LL;
}
do
{
LABEL_12:
while ( 1 )
{
v6 = transfer[v3];
if ( !v6 )
break;
if ( (unsigned __int8)add(buff[v3], v6) )
{
transfer[v3] = 0LL;
v4 = 1;
break;
}
if ( !transfer[v3] )
break;
v3 += 8LL;
if ( listlength <= (unsigned int)v3 )
goto LABEL_17;
}
v5 = get(buff[(int)v3 - 1]);
if ( v5 && v5 % prime[v3] )
{
transfer[v3] = v5;
v4 = 1;
}
v3 += 8LL;
}
while ( listlength > (unsigned int)v3 );
LABEL_17:
if ( v4 )
goto LABEL_18;
LABEL_22:
v1 = State;
LABEL_23:
sched_yield();
}
return 0LL;
}

original_code:
void* segment(void* arg)
{
  int id = *((int*) arg);
  char progress = 0;
  while (State < 2)
  {
    sched_yield();
  }
  while (State == 2)
  {
    progress = 0;
    if (id == 8)
    {
      if (add(buff[0], transfer[0]))
      {
        transfer[0] += 2;
        progress = 1;
      }
    }
    unsigned long x;
    for (int pos = id; pos < listlength; pos += 8)
    {
      if ((x = transfer[pos]))
      {
        if (add(buff[pos], x))
        {
          transfer[pos] = 0;
          progress = 1;
        }
      }
      if (transfer[pos] == 0)
      {
        if ((x = get(buff[pos - 1])))
        {
          if (x % prime[pos] != 0)
          {
            transfer[pos] = x;
            progress = 1;
          }
        }
      }
    }
    if (!progress)
    {
      sched_yield();
    }
  }
  return ((void *)0);
}

canonical_original_code:
void *segment(void *arg)
{
  int id = *((int*) arg);
  char progress = 0;
  while (State < 2)
  {
    sched_yield();
  }
  while (State == 2)
  {
    progress = 0;
    if (id == 8)
    {
      if (add(buff[0], transfer[0]))
      {
        transfer[0] += 2;
        progress = 1;
      }
    }
    unsigned long x;
    for (int pos = id; pos < listlength; pos += 8)
    {
      if ((x = transfer[pos]))
      {
        if (add(buff[pos], x))
        {
          transfer[pos] = 0;
          progress = 1;
        }
      }
      if (transfer[pos] == 0)
      {
        if ((x = get(buff[pos - 1])))
        {
          if (x % prime[pos] != 0)
          {
            transfer[pos] = x;
            progress = 1;
          }
        }
      }
    }
    if (!progress)
    {
      sched_yield();
    }
  }
  return ((void *)0);
}

variable_types:
arg
void *

variable_types: id int

variable_types: progress char

variable_types:
x
unsigned long

variable_types: pos int


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

if ( listlength > (unsigned int)&unk_1000000 )
{
printf("{ error } All %lu primes calculated.\n", &unk_1000000);
result = 1LL;
}
else
{
result = get(buff[listlength - 1]);
prime[listlength] = result;
}
return result;
}

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

if ( listlength > (unsigned int)&unk_1000000 )
{
printf("{ error } All %lu primes calculated.\n", &unk_1000000);
result = 1LL;
}
else
{
result = get(buff[listlength - 1]);
prime[listlength] = result;
}
return result;
}

original_code:
unsigned long get_prime()
{
  if (listlength > 16777216)
  {
    printf("{ error } All %lu primes calculated.\n", (unsigned long) 16777216);
    return 1;
  }
  unsigned long p = get(buff[listlength - 1]);
  prime[listlength] = p;
  return p;
}


advance

decompiled_code:
int advance()
{
__int64 v0; // rbx
__int64 v1; // rax

v0 = (unsigned int)listlength;
if ( prime[listlength] )
{
if ( listlength > (unsigned int)&unk_FFFFFF )
{
LODWORD(v1) = puts("{ warning } Cannot advance; already full.");
}
else
{
v1 = init_buffer();
++listlength;
buff[v0] = v1;
}
}
else
{
LODWORD(v1) = puts("{ warning } Prime not yet calculated.");
}
return v1;
}

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

v0 = (unsigned int)listlength;
if ( prime[listlength] )
{
if ( listlength > (unsigned int)&unk_FFFFFF )
{
LODWORD(v1) = puts("{ warning } Cannot advance; already full.");
}
else
{
v1 = init_buffer();
++listlength;
buff[v0] = v1;
}
}
else
{
LODWORD(v1) = puts("{ warning } Prime not yet calculated.");
}
return v1;
}

original_code:
void advance()
{
  if (!prime[listlength])
  {
    printf("{ warning } Prime not yet calculated.\n");
    return;
  }
  if (listlength >= 16777216)
  {
    printf("{ warning } Cannot advance; already full.\n");
    return;
  }
  buff[listlength] = init_buffer();
  listlength++;
}


decompiled_code:
int init_sieve()
{
_DWORD *v0; // rbp
__int64 v1; // rbx
int result; // eax

if ( State )
return puts("{ warning } Sieve already initialised.");
v0 = &PTID;
v1 = 0LL;
prime[0] = 2LL;
transfer[0] = 3LL;
buff[0] = init_buffer();
memset((void *)(unsigned int)((_DWORD)prime + 8), 0, (size_t)&unk_7FFFFF8);
memset(&unk_10000508, 0, (size_t)&unk_7FFFFF8);
memset(&unk_8000508, 0, (size_t)&unk_7FFFFF8);
listlength = 1;
pthread_attr_init(&ptattr);
pthread_attr_setscope(&ptattr, 0);
do
{
*v0 = v1 + 1;
result = pthread_create(&PT + v1, &ptattr, segment, v0);
if ( result )
{
printf("{ fatal error } Could not create pthread %d.\n", (unsigned int)v1);
exit(-1);
}
++v1;
++v0;
}
while ( v1 != 8 );
State = 1;
return result;
}

canonical_decompiled_code:
int func3()
{
_DWORD *v0; // rbp
__int64 v1; // rbx
int result; // eax

if ( State )
return puts("{ warning } Sieve already initialised.");
v0 = &PTID;
v1 = 0LL;
prime[0] = 2LL;
transfer[0] = 3LL;
buff[0] = init_buffer();
memset((void *)(unsigned int)((_DWORD)prime + 8), 0, (size_t)&unk_7FFFFF8);
memset(&unk_10000508, 0, (size_t)&unk_7FFFFF8);
memset(&unk_8000508, 0, (size_t)&unk_7FFFFF8);
listlength = 1;
pthread_attr_init(&ptattr);
pthread_attr_setscope(&ptattr, 0);
do
{
*v0 = v1 + 1;
result = pthread_create(&PT + v1, &ptattr, segment, v0);
if ( result )
{
printf("{ fatal error } Could not create pthread %d.\n", (unsigned int)v1);
exit(-1);
}
++v1;
++v0;
}
while ( v1 != 8 );
State = 1;
return result;
}

original_code:
void init_sieve()
{
  if (State != 0)
  {
    printf("{ warning } Sieve already initialised.\n");
    return;
  }
  prime[0] = 2;
  transfer[0] = 3;
  buff[0] = init_buffer();
  for (int i = 1; i < 16777216; i++)
  {
    prime[i] = 0;
    transfer[i] = 0;
    buff[i] = ((void *)0);
  }
  listlength = 1;
  pthread_attr_init(&ptattr);
  pthread_attr_setscope(&ptattr, PTHREAD_SCOPE_SYSTEM);
  for (int i = 0; i < 8; i++)
  {
    PTID[i] = i + 1;
    if (pthread_create(&PT[i], &ptattr, &segment, &PTID[i]))
    {
      printf("{ fatal error } Could not create pthread %d.\n", i);
      exit(-1);
    }
  }
  State = 1;
}

variable_types: i int


decompiled_code:
int free_sieve()
{
__int64 i; // rbx
__int64 v1; // rsi
__int64 *v2; // rbx
int result; // eax

if ( State != 3 )
return puts("{ warning } Sieve has not finished or already freed.");
for ( i = 0LL; i != 8; ++i )
{
while ( !pthread_join(*(&PT + i), 0LL) )
{
if ( ++i == 8 )
goto LABEL_6;
}
v1 = (unsigned int)i;
printf("{ error } Could not join pthread %d.\n", v1);
}
LABEL_6:
v2 = buff;
do
{
if ( *v2 )
free_buffer();
++v2;
}
while ( v2 != (__int64 *)((char *)&unk_8000000 + (_QWORD)buff) );
result = pthread_attr_destroy(&ptattr);
State = 4;
return result;
}

canonical_decompiled_code:
int func4()
{
__int64 i; // rbx
__int64 v1; // rsi
__int64 *v2; // rbx
int result; // eax

if ( State != 3 )
return puts("{ warning } Sieve has not finished or already freed.");
for ( i = 0LL; i != 8; ++i )
{
while ( !pthread_join(*(&PT + i), 0LL) )
{
if ( ++i == 8 )
goto LABEL_6;
}
v1 = (unsigned int)i;
printf("{ error } Could not join pthread %d.\n", v1);
}
LABEL_6:
v2 = buff;
do
{
if ( *v2 )
free_buffer();
++v2;
}
while ( v2 != (__int64 *)((char *)&unk_8000000 + (_QWORD)buff) );
result = pthread_attr_destroy(&ptattr);
State = 4;
return result;
}

original_code:
void free_sieve()
{
  if (State != 3)
  {
    printf("{ warning } Sieve has not finished or already freed.\n");
    return;
  }
  for (int i = 0; i < 8; i++)
  {
    if (pthread_join(PT[i], ((void *)0)))
    {
      printf("{ error } Could not join pthread %d.\n", i);
    }
  }
  for (int i = 0; i < 16777216; i++)
  {
    if (buff[i])
    {
      free_buffer(buff[i]);
    }
  }
  pthread_attr_destroy(&ptattr);
  State = 4;
}


start

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

if ( State != 1 )
return puts("{ warning } Sieve not yet ready or already started.");
State = 2;
return result;
}

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

if ( State != 1 )
return puts("{ warning } Sieve not yet ready or already started.");
State = 2;
return result;
}

original_code:
void start()
{
  if (State == 1)
  {
    State = 2;
  }
  else
  {
    printf("{ warning } Sieve not yet ready or already started.\n");
  }
}


end

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

if ( State != 2 )
return puts("{ warning } Sieve not running.");
State = 3;
return result;
}

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

if ( State != 2 )
return puts("{ warning } Sieve not running.");
State = 3;
return result;
}

original_code:
void end()
{
  if (State == 2)
  {
    State = 3;
  }
  else
  {
    printf("{ warning } Sieve not running.\n");
  }
}


start

decompiled_code:
// positive sp value has been detected, the output may be wrong!
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void))
{
__int64 v3; // rax
int v4; // esi
__int64 v5; // [rsp-8h] [rbp-8h] BYREF
char *retaddr; // [rsp+0h] [rbp+0h] BYREF

v4 = v5;
v5 = v3;
_libc_start_main(
(int (__fastcall *)(int, char **, char **))main,
v4,
&retaddr,
_libc_csu_init,
_libc_csu_fini,
a3,
&v5);
__halt();
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1, __int64 a2, void (*a3)(void))
{
__int64 v3; // rax
int v4; // esi
__int64 v5; // [rsp-8h] [rbp-8h] BYREF
char *retaddr; // [rsp+0h] [rbp+0h] BYREF

v4 = v5;
v5 = v3;
_libc_start_main(
(int (__fastcall *)(int, char **, char **))main,
v4,
&retaddr,
_libc_csu_init,
_libc_csu_fini,
a3,
&v5);
__halt();
}


decompiled_code:
int print_names()
{
const char *v0; // rbp
unsigned int v1; // ebx

printf("Names:\t\t");
if ( nNames )
{
v0 = name;
v1 = 0;
do
{
++v1;
printf("%12s,\t", v0);
v0 += 12;
}
while ( nNames > v1 );
}
return puts("\tend.");
}

canonical_decompiled_code:
int func7()
{
const char *v0; // rbp
unsigned int v1; // ebx

printf("Names:\t\t");
if ( nNames )
{
v0 = name;
v1 = 0;
do
{
++v1;
printf("%12s,\t", v0);
v0 += 12;
}
while ( nNames > v1 );
}
return puts("\tend.");
}

original_code:
void print_names()
{
  printf("Names:\t\t");
  for (int i = 0; i < nNames; i++)
  {
    printf("%12s,\t", name[i]);
  }
  printf("\tend.\n");
}


decompiled_code:
_QWORD *init_buffer()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
pthread_mutex_t *v2; // rax

v0 = malloc(0x810uLL);
if ( !v0 )
{
puts("{ fatal error } Could not allocate buffer.");
exit(-1);
}
v0[256] = 0LL;
v1 = v0;
v2 = (pthread_mutex_t *)malloc(0x28uLL);
v1[257] = v2;
pthread_mutex_init(v2, 0LL);
return v1;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rax
_QWORD *v1; // r12
pthread_mutex_t *v2; // rax

v0 = malloc(0x810uLL);
if ( !v0 )
{
puts("{ fatal error } Could not allocate buffer.");
exit(-1);
}
v0[256] = 0LL;
v1 = v0;
v2 = (pthread_mutex_t *)malloc(0x28uLL);
v1[257] = v2;
pthread_mutex_init(v2, 0LL);
return v1;
}

original_code:
buffer* init_buffer()
{
  buffer* B = malloc(sizeof(buffer));
  if (!B)
  {
    printf("{ fatal error } Could not allocate buffer.\n");
    exit(-1);
  }
  B->index = 0;
  B->size = 0;
  B->bufferlock = malloc(sizeof(pthread_mutex_t));
  pthread_mutex_init(B->bufferlock, ((void *)0));
  return B;
}

canonical_original_code:
struct buffer *init_buffer()
{
  struct buffer *B = malloc(sizeof(buffer));
  if (!B)
  {
    printf("{ fatal error } Could not allocate buffer.\n");
    exit(-1);
  }
  B->index = 0;
  B->size = 0;
  B->bufferlock = malloc(sizeof(pthread_mutex_t));
  pthread_mutex_init(B->bufferlock, ((void *)0));
  return B;
}

variable_types: struct buffer { unsigned long data[-1]; unsigned int index; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *bufferlock; } *

user_defined_types: struct buffer { unsigned long data[-1]; unsigned int index; union pthread_mutex_t *bufferlock; }


decompiled_code:
void __fastcall free_buffer(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy(*((pthread_mutex_t **)ptr + 257));
free(*((void **)ptr + 257));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy(*((pthread_mutex_t **)ptr + 257));
free(*((void **)ptr + 257));
free(ptr);
}
}

original_code:
void free_buffer(buffer* B)
{
  if (B)
  {
    pthread_mutex_destroy(B->bufferlock);
    free(B->bufferlock);
    free(B);
  }
}

canonical_original_code:
void free_buffer(struct buffer *B)
{
  if (B)
  {
    pthread_mutex_destroy(B->bufferlock);
    free(B->bufferlock);
    free(B);
  }
}


add

decompiled_code:
__int64 __fastcall add(__int64 a1, __int64 a2)
{
unsigned int v3; // eax
__int64 result; // rax
pthread_mutex_t *v5; // rdi

if ( a1 )
{
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 2056));
v3 = *(_DWORD *)(a1 + 2052);
if ( v3 <= 0xFF )
{
v5 = *(pthread_mutex_t **)(a1 + 2056);
*(_QWORD *)(a1 + 8LL * (unsigned __int8)(v3 + *(_DWORD *)(a1 + 2048))) = a2;
*(_DWORD *)(a1 + 2052) = v3 + 1;
pthread_mutex_unlock(v5);
result = 1LL;
}
else
{
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 2056));
result = 0LL;
}
}
else
{
puts("{ error } This is not a buffer.");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
unsigned int v3; // eax
__int64 result; // rax
pthread_mutex_t *v5; // rdi

if ( a1 )
{
pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 2056));
v3 = *(_DWORD *)(a1 + 2052);
if ( v3 <= 0xFF )
{
v5 = *(pthread_mutex_t **)(a1 + 2056);
*(_QWORD *)(a1 + 8LL * (unsigned __int8)(v3 + *(_DWORD *)(a1 + 2048))) = a2;
*(_DWORD *)(a1 + 2052) = v3 + 1;
pthread_mutex_unlock(v5);
result = 1LL;
}
else
{
pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 2056));
result = 0LL;
}
}
else
{
puts("{ error } This is not a buffer.");
result = 0LL;
}
return result;
}

original_code:
char add(buffer* B, unsigned long x)
{
  if (!B)
  {
    printf("{ error } This is not a buffer.\n");
    return 0;
  }
  pthread_mutex_lock(B->bufferlock);
  if (B->size < (1<<8))
  {
    B->data[(B->index + B->size) % (1<<8)] = x;
    B->size += 1;
    pthread_mutex_unlock(B->bufferlock);
    return 1;
  }
  pthread_mutex_unlock(B->bufferlock);
  return 0;
}

canonical_original_code:
char add(struct buffer *B, unsigned long x)
{
  if (!B)
  {
    printf("{ error } This is not a buffer.\n");
    return 0;
  }
  pthread_mutex_lock(B->bufferlock);
  if (B->size < (1<<8))
  {
    B->data[(B->index + B->size) % (1<<8)] = x;
    B->size += 1;
    pthread_mutex_unlock(B->bufferlock);
    return 1;
  }
  pthread_mutex_unlock(B->bufferlock);
  return 0;
}