decompiled_code:
__int64 __fastcall lvl_parents(__int64 a1, _QWORD *a2)
{
int v2; // ebx
int v3; // er12
_QWORD *v4; // rax
__int64 v5; // r15
bool v6; // al
unsigned __int8 v8; // [rsp+6h] [rbp-42h]
bool v9; // [rsp+7h] [rbp-41h]

if ( !**(_QWORD **)a1 )
return 1;
v2 = my_strcmp(&unk_332);
if ( !v2 )
{
v4 = *(_QWORD **)a1;
v3 = 1;
v8 = *(_BYTE *)(*(_QWORD *)a1 + 8LL);
if ( !v8 )
goto LABEL_6;
return 1;
}
if ( !**(_QWORD **)a1 )
return 1;
v2 = 1;
v3 = my_strcmp(&unk_334);
if ( v3 )
return 1;
v4 = *(_QWORD **)a1;
v8 = *(_BYTE *)(*(_QWORD *)a1 + 8LL);
if ( v8 )
return 1;
LABEL_6:
v5 = v4[2];
if ( v5 )
{
while ( v3 | v2 )
{
if ( *(_QWORD *)v5 )
{
v9 = *(_BYTE *)(v5 + 8) == 0;
v6 = (unsigned int)my_strcmp(&unk_332) == 0;
if ( *(_QWORD *)v5 )
{
v3 += ((__PAIR64__(v6, my_strcmp(&unk_336)) - 1) >> 32) * v9;
if ( *(_QWORD *)v5 && !(unsigned int)my_strcmp(&unk_334) )
v2 ^= 1u;
}
else
{
v3 += v9 && v6;
}
}
*(_QWORD *)a1 = v5;
v5 = *(_QWORD *)(v5 + 16);
if ( !v5 )
goto LABEL_19;
}
return 1;
}
LABEL_19:
if ( v3 )
*a2 = my_strdup("(sh): unexpected EOS while looking for matching `)'");
if ( !v2 )
return v3 == 0;
*a2 = my_strdup("(sh): syntax error near unexpected token ``'");
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
int v2; // ebx
int v3; // er12
_QWORD *v4; // rax
__int64 v5; // r15
bool v6; // al
unsigned __int8 v8; // [rsp+6h] [rbp-42h]
bool v9; // [rsp+7h] [rbp-41h]

if ( !**(_QWORD **)a1 )
return 1;
v2 = my_strcmp(&unk_332);
if ( !v2 )
{
v4 = *(_QWORD **)a1;
v3 = 1;
v8 = *(_BYTE *)(*(_QWORD *)a1 + 8LL);
if ( !v8 )
goto LABEL_6;
return 1;
}
if ( !**(_QWORD **)a1 )
return 1;
v2 = 1;
v3 = my_strcmp(&unk_334);
if ( v3 )
return 1;
v4 = *(_QWORD **)a1;
v8 = *(_BYTE *)(*(_QWORD *)a1 + 8LL);
if ( v8 )
return 1;
LABEL_6:
v5 = v4[2];
if ( v5 )
{
while ( v3 | v2 )
{
if ( *(_QWORD *)v5 )
{
v9 = *(_BYTE *)(v5 + 8) == 0;
v6 = (unsigned int)my_strcmp(&unk_332) == 0;
if ( *(_QWORD *)v5 )
{
v3 += ((__PAIR64__(v6, my_strcmp(&unk_336)) - 1) >> 32) * v9;
if ( *(_QWORD *)v5 && !(unsigned int)my_strcmp(&unk_334) )
v2 ^= 1u;
}
else
{
v3 += v9 && v6;
}
}
*(_QWORD *)a1 = v5;
v5 = *(_QWORD *)(v5 + 16);
if ( !v5 )
goto LABEL_19;
}
return 1;
}
LABEL_19:
if ( v3 )
*a2 = my_strdup("(sh): unexpected EOS while looking for matching `)'");
if ( !v2 )
return v3 == 0;
*a2 = my_strdup("(sh): syntax error near unexpected token ``'");
return v8;
}

original_code:
unsigned char lvl_parents(t_get **words, char **bad_sintax)
{
  t_get *link;
  int plevel;
  int blevel;
  plevel = 0;
  if ((!(blevel = (("(" && (*words)->word) ? (!my_strcmp("(", (*words)->word)) : (0)))
       && !(plevel = (("`" && (*words)->word) ? (!my_strcmp("`", (*words)->word)) : (0)))) || (*words)->inter)
    return (1);
  link = (*words)->next;
  while (link && (plevel || blevel))
    {
      blevel = blevel + !(link->inter) * ((("(" && (link)->word) ? (!my_strcmp("(", (link)->word)) : (0))
       - ((")" && (link)->word) ? (!my_strcmp(")", (link)->word)) : (0)));
      if ((("`" && (link)->word) ? (!my_strcmp("`", (link)->word)) : (0)))
 plevel = !plevel;
      *words = link;
      link = link->next;
    }
  if (blevel)
    *bad_sintax = my_strdup("(sh): unexpected EOS while looking for matching `)'");
  if (plevel)
    *bad_sintax = my_strdup("(sh): syntax error near unexpected token ``'");
  if (plevel || blevel)
    return (0);
  return (1);
}

canonical_original_code:
unsigned char lvl_parents(struct s_get **words, char **bad_sintax)
{
  struct s_get *link;
  int plevel;
  int blevel;
  plevel = 0;
  if ((!(blevel = (("(" && (*words)->word) ? (!my_strcmp("(", (*words)->word)) : (0)))
       && !(plevel = (("`" && (*words)->word) ? (!my_strcmp("`", (*words)->word)) : (0)))) || (*words)->inter)
    return (1);
  link = (*words)->next;
  while (link && (plevel || blevel))
    {
      blevel = blevel + !(link->inter) * ((("(" && (link)->word) ? (!my_strcmp("(", (link)->word)) : (0))
       - ((")" && (link)->word) ? (!my_strcmp(")", (link)->word)) : (0)));
      if ((("`" && (link)->word) ? (!my_strcmp("`", (link)->word)) : (0)))
 plevel = !plevel;
      *words = link;
      link = link->next;
    }
  if (blevel)
    *bad_sintax = my_strdup("(sh): unexpected EOS while looking for matching `)'");
  if (plevel)
    *bad_sintax = my_strdup("(sh): syntax error near unexpected token ``'");
  if (plevel || blevel)
    return (0);
  return (1);
}

variable_types: struct s_get { char *word; unsigned char inter; struct s_get *next; struct s_get *prev; } **

variable_types: plevel int

variable_types: blevel int


decompiled_code:
__int64 __fastcall match_parents(_QWORD **a1, _QWORD *a2)
{
int v2; // er12
int v3; // ebp
_QWORD *v4; // rax
unsigned int v5; // er14
_QWORD *v6; // rbx
bool v8; // zf

if ( !**a1 )
return 1;
v2 = my_strcmp(&unk_332);
if ( v2 )
{
if ( !**a1 )
return 1;
v3 = my_strcmp(&unk_334);
if ( v3 )
return 1;
v4 = *a1;
v2 = 1;
}
else
{
v4 = *a1;
if ( **a1 )
{
v8 = (unsigned int)my_strcmp(&unk_334) == 0;
v4 = *a1;
v2 = v8;
}
v3 = 1;
}
v5 = *((unsigned __int8 *)v4 + 8);
if ( (_BYTE)v5 )
return 1;
v6 = (_QWORD *)v4[2];
if ( v6 )
{
if ( !(v2 | v3) )
{
LABEL_20:
*a1 = v6;
return 1;
}
while ( 1 )
{
if ( *v6 )
{
v3 += my_strcmp(&unk_332) == 0;
if ( *v6 )
{
v3 = (__PAIR64__(v3, my_strcmp(&unk_336)) - 1) >> 32;
if ( *v6 )
{
if ( !(unsigned int)my_strcmp(&unk_334) )
v2 ^= 1u;
}
}
}
*a1 = v6;
v6 = (_QWORD *)v6[2];
if ( !v6 )
break;
if ( !(v3 | v2) )
goto LABEL_20;
}
}
if ( !v3 )
{
if ( !v2 )
return 1;
goto LABEL_19;
}
*a2 = my_strdup("(sh): unexpected EOS while looking for matching `)'");
if ( v2 )
LABEL_19:
*a2 = my_strdup("(sh): syntax error near unexpected token ``'");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD **a1, _QWORD *a2)
{
int v2; // er12
int v3; // ebp
_QWORD *v4; // rax
unsigned int v5; // er14
_QWORD *v6; // rbx
bool v8; // zf

if ( !**a1 )
return 1;
v2 = my_strcmp(&unk_332);
if ( v2 )
{
if ( !**a1 )
return 1;
v3 = my_strcmp(&unk_334);
if ( v3 )
return 1;
v4 = *a1;
v2 = 1;
}
else
{
v4 = *a1;
if ( **a1 )
{
v8 = (unsigned int)my_strcmp(&unk_334) == 0;
v4 = *a1;
v2 = v8;
}
v3 = 1;
}
v5 = *((unsigned __int8 *)v4 + 8);
if ( (_BYTE)v5 )
return 1;
v6 = (_QWORD *)v4[2];
if ( v6 )
{
if ( !(v2 | v3) )
{
LABEL_20:
*a1 = v6;
return 1;
}
while ( 1 )
{
if ( *v6 )
{
v3 += my_strcmp(&unk_332) == 0;
if ( *v6 )
{
v3 = (__PAIR64__(v3, my_strcmp(&unk_336)) - 1) >> 32;
if ( *v6 )
{
if ( !(unsigned int)my_strcmp(&unk_334) )
v2 ^= 1u;
}
}
}
*a1 = v6;
v6 = (_QWORD *)v6[2];
if ( !v6 )
break;
if ( !(v3 | v2) )
goto LABEL_20;
}
}
if ( !v3 )
{
if ( !v2 )
return 1;
goto LABEL_19;
}
*a2 = my_strdup("(sh): unexpected EOS while looking for matching `)'");
if ( v2 )
LABEL_19:
*a2 = my_strdup("(sh): syntax error near unexpected token ``'");
return v5;
}

original_code:
unsigned char match_parents(t_get **words, char **bad_sintax)
{
  t_get *link;
  int plevel;
  int blevel;
  if ((!(blevel = (("(" && (*words)->word) ? (!my_strcmp("(", (*words)->word)) : (0)))
       + !(plevel = (("`" && (*words)->word) ? (!my_strcmp("`", (*words)->word)) : (0))) == 2) || (*words)->inter)
    return (1);
  link = (*words)->next;
  while (link && (plevel || blevel))
    {
      blevel = blevel + (("(" && (link)->word) ? (!my_strcmp("(", (link)->word)) : (0)) - ((")" && (link)->word) ? (!my_strcmp(")", (link)->word)) : (0));
      if ((("`" && (link)->word) ? (!my_strcmp("`", (link)->word)) : (0)))
 plevel = !plevel;
      *words = link;
      link = link->next;
    }
  if (blevel)
    *bad_sintax = my_strdup("(sh): unexpected EOS while looking for matching `)'");
  if (plevel)
    *bad_sintax = my_strdup("(sh): syntax error near unexpected token ``'");
  if (plevel || blevel)
    return (0);
  if (link)
    *words = link;
  return (1);
}

canonical_original_code:
unsigned char match_parents(struct s_get **words, char **bad_sintax)
{
  struct s_get *link;
  int plevel;
  int blevel;
  if ((!(blevel = (("(" && (*words)->word) ? (!my_strcmp("(", (*words)->word)) : (0)))
       + !(plevel = (("`" && (*words)->word) ? (!my_strcmp("`", (*words)->word)) : (0))) == 2) || (*words)->inter)
    return (1);
  link = (*words)->next;
  while (link && (plevel || blevel))
    {
      blevel = blevel + (("(" && (link)->word) ? (!my_strcmp("(", (link)->word)) : (0)) - ((")" && (link)->word) ? (!my_strcmp(")", (link)->word)) : (0));
      if ((("`" && (link)->word) ? (!my_strcmp("`", (link)->word)) : (0)))
 plevel = !plevel;
      *words = link;
      link = link->next;
    }
  if (blevel)
    *bad_sintax = my_strdup("(sh): unexpected EOS while looking for matching `)'");
  if (plevel)
    *bad_sintax = my_strdup("(sh): syntax error near unexpected token ``'");
  if (plevel || blevel)
    return (0);
  if (link)
    *words = link;
  return (1);
}

variable_types: struct s_get { char *word; unsigned char inter; struct s_get *next; struct s_get *prev; } *

return_type: unsigned char


decompiled_code:
__int64 __fastcall word_nullify(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

if ( !ptr )
return 0LL;
v1 = (void **)ptr;
do
{
v2 = v1;
v1 = (void **)v1[2];
free(*v2);
free(v2);
}
while ( v1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

if ( !ptr )
return 0LL;
v1 = (void **)ptr;
do
{
v2 = v1;
v1 = (void **)v1[2];
free(*v2);
free(v2);
}
while ( v1 );
return 0LL;
}

original_code:
static void *word_nullify(t_get *word)
{
  t_get *next;
  next = word;
  while (next)
    {
      next = word->next;
      free(word->word);
      free(word);
      word = next;
    }
  return (((void *)0));
}

canonical_original_code:
static void *word_nullify(struct s_get *word)
{
  struct s_get *next;
  next = word;
  while (next)
    {
      next = word->next;
      free(word->word);
      free(word);
      word = next;
    }
  return (((void *)0));
}

return_type: void *

user_defined_types: struct s_get { char *word; unsigned char inter; struct s_get *next; struct s_get *prev; }


decompiled_code:
__int64 __fastcall get_pipe(__int64 a1, _QWORD *a2)
{
__int64 v2; // rbx
__int64 v3; // rax

if ( !a1 )
return 1LL;
v2 = a1;
do
{
v3 = assoc_pipes(*(void **)(v2 + 24), 0LL, a2);
*(_QWORD *)(v2 + 16) = v3;
if ( !v3 )
return 0LL;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
return 1LL;
}

original_code:
unsigned char get_pipe(t_exec *execs, char **bad_sintax)
{
  if (!execs)
    return (1);
  if ((execs->pipes = assoc_pipes(execs->tmp, ((void *)0), bad_sintax)) == ((void *)0))
    return (0);
  return (get_pipe(execs->next, bad_sintax));
}

canonical_original_code:
unsigned char get_pipe(struct s_exec *execs, char **bad_sintax)
{
  if (!execs)
    return (1);
  if ((execs->pipes = assoc_pipes(execs->tmp, ((void *)0), bad_sintax)) == ((void *)0))
    return (0);
  return (get_pipe(execs->next, bad_sintax));
}

variable_types: struct s_exec { unsigned int type; struct s_exec *next; struct s_pipes { struct s_pipes *next; struct s_get { char *word; unsigned char inter; struct s_get *next; struct s_get *prev; } *tmp; struct s_cmd { struct s_redir { unsigned int redir; unsigned int type; int in; int out; char *file; struct s_redir *next; } *redir; unsigned int type; struct s_words { char *word; struct s_words *next; } *params; struct s_jobs { unsigned int type; struct s_jobs *next; struct s_exec *exec; struct s_get *tmp; } *parents; } *cmd; } *pipes; struct s_get *tmp; } *

user_defined_types: struct s_exec { unsigned int type; struct s_exec *next; struct s_pipes *pipes; struct s_get *tmp; }

user_defined_types: struct s_pipes { struct s_pipes *next; struct s_get *tmp; struct s_cmd *cmd; }

user_defined_types: struct s_cmd { struct s_redir *redir; unsigned int type; struct s_words *params; struct s_jobs *parents; }


add

decompiled_code:
__int64 __fastcall add(__int64 a1, __int64 a2, _BYTE *a3)
{
__int64 *v3; // rbx
__int64 result; // rax
__int64 v5; // rax

if ( !a1 )
return 0LL;
*a3 = 0;
v3 = (__int64 *)xmalloc(16LL);
result = 0LL;
if ( v3 )
{
v5 = my_strdup(a1);
v3[1] = 0LL;
*v3 = v5;
*(_QWORD *)(*(_QWORD *)a2 + 8LL) = v3;
result = 1LL;
*(_QWORD *)a2 = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, _BYTE *a3)
{
__int64 *v3; // rbx
__int64 result; // rax
__int64 v5; // rax

if ( !a1 )
return 0LL;
*a3 = 0;
v3 = (__int64 *)xmalloc(16LL);
result = 0LL;
if ( v3 )
{
v5 = my_strdup(a1);
v3[1] = 0LL;
*v3 = v5;
*(_QWORD *)(*(_QWORD *)a2 + 8LL) = v3;
result = 1LL;
*(_QWORD *)a2 = v3;
}
return result;
}

original_code:
static unsigned char add(char *name, t_words **last, unsigned char *no_match)
{
  t_words *link;
  if (!name)
    return (0);
  *no_match = 0;
  if ((link = xmalloc(sizeof(*link))) == ((void *)0))
    return (0);
  link->word = my_strdup(name);
  link->next = ((void *)0);
  (*last)->next = link;
  *last = link;
  return (1);
}

canonical_original_code:
static unsigned char add(char *name, struct s_words **last, unsigned char *no_match)
{
  struct s_words *link;
  if (!name)
    return (0);
  *no_match = 0;
  if ((link = xmalloc(sizeof(*link))) == ((void *)0))
    return (0);
  link->word = my_strdup(name);
  link->next = ((void *)0);
  (*last)->next = link;
  *last = link;
  return (1);
}

variable_types:
name
char *

variable_types: struct s_words { char *word; struct s_words *next; } **


decompiled_code:
__int64 __fastcall add_and_last(_BYTE *a1, void *a2, __int64 a3, _BYTE *a4)
{
void *v6; // r14
__int64 result; // rax

if ( !*a1 )
{
if ( a2 )
{
v6 = a2;
goto LABEL_6;
}
LABEL_12:
v6 = (void *)my_strdup(a1);
if ( !(unsigned __int8)add((__int64)v6, a3, a4) )
return 0LL;
goto LABEL_10;
}
if ( !a2 )
goto LABEL_12;
v6 = (void *)my_stricat(a2, a1, 47LL);
LABEL_6:
if ( !(unsigned __int8)add((__int64)v6, a3, a4) )
return 0LL;
free(a2);
LABEL_10:
result = 1LL;
if ( *a1 )
{
free(v6);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, void *a2, __int64 a3, _BYTE *a4)
{
void *v6; // r14
__int64 result; // rax

if ( !*a1 )
{
if ( a2 )
{
v6 = a2;
goto LABEL_6;
}
LABEL_12:
v6 = (void *)my_strdup(a1);
if ( !(unsigned __int8)func0((__int64)v6, a3, a4) )
return 0LL;
goto LABEL_10;
}
if ( !a2 )
goto LABEL_12;
v6 = (void *)my_stricat(a2, a1, 47LL);
LABEL_6:
if ( !(unsigned __int8)func0((__int64)v6, a3, a4) )
return 0LL;
free(a2);
LABEL_10:
result = 1LL;
if ( *a1 )
{
free(v6);
result = 1LL;
}
return result;
}

original_code:
unsigned char add_and_last(char *m, char *dp, t_words **last, unsigned char *no_match)
{
  char *s;
  if (m[0] && (dp) != ((void *)0))
    s = my_stricat(dp, m, '/');
  else if (dp != ((void *)0))
    s = dp;
  else
    s = my_strdup(m);
  if (add(s, last, no_match) == 0)
    return (0);
  if (dp)
    free(dp);
  if (m[0])
    free(s);
  return (1);
}

canonical_original_code:
unsigned char add_and_last(char *m, char *dp, struct s_words **last, unsigned char *no_match)
{
  char *s;
  if (m[0] && (dp) != ((void *)0))
    s = my_stricat(dp, m, '/');
  else if (dp != ((void *)0))
    s = dp;
  else
    s = my_strdup(m);
  if (add(s, last, no_match) == 0)
    return (0);
  if (dp)
    free(dp);
  if (m[0])
    free(s);
  return (1);
}

variable_types:
m
char *

variable_types:
dp
char *

variable_types:
s
char *

user_defined_types: struct s_words { char *word; struct s_words *next; }


original_code:
void auto_completion(char value, int *rev_c, char **cmd)
{
  char *dir;
  char *s;
  (void)value;
  s = my_strndup(*cmd + lastoccur_bf(*cmd, ' ', *rev_c) +
   ((*cmd)[lastoccur_bf(*cmd, ' ', *rev_c)] == ' '), *rev_c -
   lastoccur_bf(*cmd, ' ', *rev_c));
  if (!s || empty(s))
    dir = (s = ((void *)0));
  else
    dir = my_strndup(s, lastoccur_bf(s, '/', my_strlen(s)) +
       ((s)[my_strilen(s, '/')] != 0));
  go_down_of_cmd_high(*cmd);
  printf("\n");
  if (dir)
    print_complo(dir, s + ((s)[my_strilen(s, '/')] != 0) * (lastoccur_bf(s, '/',
           my_strlen(s)) + 1));
  else
    print_complo(".", s);
  if (dir)
    free(dir);
  prompt(1);
}

variable_types: value char

variable_types:
cmd
char **

variable_types:
dir
char *

return_type: void


decompiled_code:
__int64 __fastcall my_realloc(void *ptr, unsigned __int64 a2, __int64 a3)
{
__int64 v4; // r12
unsigned __int64 v5; // rax

v4 = xmalloc(a3);
if ( v4 )
{
v5 = 0LL;
if ( a3 )
{
do
{
while ( a2 > v5 )
{
*(_BYTE *)(v4 + v5) = *((_BYTE *)ptr + v5);
if ( a3 == ++v5 )
goto LABEL_7;
}
*(_BYTE *)(v4 + v5++) = 0;
}
while ( a3 != v5 );
}
LABEL_7:
free(ptr);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr, unsigned __int64 a2, __int64 a3)
{
__int64 v4; // r12
unsigned __int64 v5; // rax

v4 = xmalloc(a3);
if ( v4 )
{
v5 = 0LL;
if ( a3 )
{
do
{
while ( a2 > v5 )
{
*(_BYTE *)(v4 + v5) = *((_BYTE *)ptr + v5);
if ( a3 == ++v5 )
goto LABEL_7;
}
*(_BYTE *)(v4 + v5++) = 0;
}
while ( a3 != v5 );
}
LABEL_7:
free(ptr);
}
return v4;
}

original_code:
void *my_realloc(void *ptr, size_t pre, size_t new)
{
  size_t i;
  void *r;
  i = 0;
  if ((r = xmalloc(new)) == ((void *)0))
    return (((void *)0));
  while (i < new)
    {
      if (i < pre)
 *(unsigned char*)(r + i) = *(unsigned char*)(ptr + i);
      else
 *(unsigned char*)(r + i) = 0;
      i++;
    }
  free(ptr);
  return (r);
}

canonical_original_code:
void *my_realloc(void *ptr, unsigned long pre, unsigned long new)
{
  unsigned long i;
  void *r;
  i = 0;
  if ((r = xmalloc(new)) == ((void *)0))
    return (((void *)0));
  while (i < new)
    {
      if (i < pre)
 *(unsigned char *)(r + i) = *(unsigned char *)(ptr + i);
      else
 *(unsigned char *)(r + i) = 0;
      i++;
    }
  free(ptr);
  return (r);
}

variable_types:
ptr
void *

variable_types:
pre
unsigned long

variable_types:
new
unsigned long

variable_types:
i
unsigned long

variable_types:
r
void *


decompiled_code:
__int64 __fastcall built_setenv(_QWORD *a1)
{
__int64 v1; // rbx
_QWORD *v2; // rbp
__int64 v3; // rax
char **v4; // rax
__int64 v5; // rdi
__int64 v6; // rbx
__int64 v7; // r13
__int64 v8; // rdx
__int64 v9; // r12
__int64 v10; // rax
int v11; // er8
char *v12; // r14
__int64 v13; // r8
char **v14; // rdi
__int64 *v15; // rax
__int64 v16; // rdx

if ( !a1[1] )
return built_env();
v1 = *a1;
if ( *(_BYTE *)(v1 + my_strilen(*a1, 61LL)) )
return 0LL;
v2 = (_QWORD *)a1[1];
if ( v2 )
{
v3 = v2[1];
if ( v3 )
{
if ( *(_QWORD *)(v3 + 8) )
return my_putstr("(sh): setenv : Too many arguments.\n", 2LL) != 0;
}
}
v4 = environ;
v5 = 16LL;
if ( !environ || !*environ )
goto LABEL_17;
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v9 = v6;
if ( *v2 )
{
v10 = my_strlen(*v2);
v11 = my_strncmp(*v2, environ[v6], v10);
v4 = environ;
if ( !v11 )
break;
}
v8 = v6 + 1;
if ( !v4[v9 + 1] )
goto LABEL_16;
LABEL_10:
v6 = v8;
}
v12 = environ[v6];
v13 = my_strlen(*v2);
v4 = environ;
if ( v12[v13] == 61 )
v7 = 1LL;
v8 = v6 + 1;
if ( environ[v9 + 1] )
goto LABEL_10;
LABEL_16:
v5 = 8 * (v6 + (v7 ^ 1) + 2);
LABEL_17:
v14 = (char **)xmalloc(v5);
if ( !v14 )
return 1LL;
v15 = (__int64 *)v2[1];
v16 = 0LL;
if ( v15 )
v16 = *v15;
return fill_env(v14, v2, v16);
}

original_code:
int built_setenv(t_words *cmd, void *bool)
{
  size_t n;
  size_t i;
  char **tab;
  (void)bool;
  if (cmd->next == ((void*)(n = (i = 0))))
    return (built_env(cmd, bool));
  if (((cmd->word)[my_strilen(cmd->word, '=')] != 0))
    return (0);
  if ((cmd = cmd->next) && cmd->next)
    if (cmd->next->next)
      return (my_putstr("(sh): setenv : Too many arguments.\n", 2) != 0);
  if (environ)
    while (environ[i])
      {
 if ((((cmd->word) && (environ[i])) ? (!my_strncmp((cmd->word), (environ[i]), my_strlen(cmd->word))) : (0))
     && environ[i][my_strlen(cmd->word)] == '=')
   n = 1;
 i++;
      }
  if ((tab = xmalloc(sizeof(char *) * (i + !n + 1))) == ((void *)0))
    return (1);
  if (cmd->next)
    return (fill_env(tab, cmd, cmd->next->word));
  return (fill_env(tab, cmd, ((void *)0)));
}

canonical_original_code:
int built_setenv(struct s_words *cmd, void *bool)
{
  unsigned long n;
  unsigned long i;
  char **tab;
  (void)bool;
  if (cmd->next == ((void *)(n = (i = 0))))
    return (built_env(cmd, bool));
  if (((cmd->word)[my_strilen(cmd->word, '=')] != 0))
    return (0);
  if ((cmd = cmd->next) && cmd->next)
    if (cmd->next->next)
      return (my_putstr("(sh): setenv : Too many arguments.\n", 2) != 0);
  if (environ)
    while (environ[i])
      {
 if ((((cmd->word) && (environ[i])) ? (!my_strncmp((cmd->word), (environ[i]), my_strlen(cmd->word))) : (0))
     && environ[i][my_strlen(cmd->word)] == '=')
   n = 1;
 i++;
      }
  if ((tab = xmalloc(sizeof(char *) * (i + !n + 1))) == ((void *)0))
    return (1);
  if (cmd->next)
    return (fill_env(tab, cmd, cmd->next->word));
  return (fill_env(tab, cmd, ((void *)0)));
}

variable_types: struct s_words { char *word; struct s_words *next; } *

variable_types:
bool
void *

variable_types:
n
unsigned long

variable_types:
tab
char **

return_type: int


decompiled_code:
__int64 __fastcall fill_env(char **a1, _QWORD *a2, __int64 a3)
{
char **v5; // rax
unsigned __int64 i; // rbx
char **v7; // rdi
unsigned __int64 v8; // rcx
unsigned __int64 v9; // rdx
unsigned __int64 v10; // rbp
__int64 v11; // rax
int v12; // er8
char *v13; // r15
__int64 v14; // r8
__int64 v15; // rax
_QWORD *v17; // rbp
__int64 v18; // rcx
__int64 v19; // rax
char v20; // [rsp+8h] [rbp-40h]
__int64 v21; // [rsp+8h] [rbp-40h]

v5 = environ;
if ( !*environ )
{
v17 = a1;
v18 = 8LL;
goto LABEL_12;
}
v20 = 0;
for ( i = 0LL; ; i = v9 )
{
v10 = i;
if ( *a2 )
{
v11 = ((__int64 (*)(void))my_strlen)();
v12 = my_strncmp(*a2, environ[i], v11);
v5 = environ;
if ( !v12 )
{
v13 = environ[i];
v14 = my_strlen(*a2);
v5 = environ;
if ( v13[v14] == 61 )
break;
}
}
v7 = v5;
v8 = v10 * 8 + 8;
a1[i] = v5[i];
v9 = i + 1;
if ( !v5[v10 + 1] )
goto LABEL_9;
LABEL_4:
;
}
free(environ[i]);
v15 = my_stricat(*a2, a3, 61LL);
v20 = 1;
v9 = i + 1;
v8 = v10 * 8 + 8;
a1[i] = (char *)v15;
v5 = environ;
v7 = environ;
if ( environ[v10 + 1] )
goto LABEL_4;
LABEL_9:
if ( !v20 )
{
v17 = (char **)((char *)a1 + v8);
v18 = 8 * i + 16;
LABEL_12:
v21 = v18;
v19 = my_stricat(*a2, a3, 61LL);
v7 = environ;
v8 = v21;
*v17 = v19;
}
*(char **)((char *)a1 + v8) = 0LL;
free(v7);
environ = a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char **a1, _QWORD *a2, __int64 a3)
{
char **v5; // rax
unsigned __int64 i; // rbx
char **v7; // rdi
unsigned __int64 v8; // rcx
unsigned __int64 v9; // rdx
unsigned __int64 v10; // rbp
__int64 v11; // rax
int v12; // er8
char *v13; // r15
__int64 v14; // r8
__int64 v15; // rax
_QWORD *v17; // rbp
__int64 v18; // rcx
__int64 v19; // rax
char v20; // [rsp+8h] [rbp-40h]
__int64 v21; // [rsp+8h] [rbp-40h]

v5 = environ;
if ( !*environ )
{
v17 = a1;
v18 = 8LL;
goto LABEL_12;
}
v20 = 0;
for ( i = 0LL; ; i = v9 )
{
v10 = i;
if ( *a2 )
{
v11 = ((__int64 (*)(void))my_strlen)();
v12 = my_strncmp(*a2, environ[i], v11);
v5 = environ;
if ( !v12 )
{
v13 = environ[i];
v14 = my_strlen(*a2);
v5 = environ;
if ( v13[v14] == 61 )
break;
}
}
v7 = v5;
v8 = v10 * 8 + 8;
a1[i] = v5[i];
v9 = i + 1;
if ( !v5[v10 + 1] )
goto LABEL_9;
LABEL_4:
;
}
free(environ[i]);
v15 = my_stricat(*a2, a3, 61LL);
v20 = 1;
v9 = i + 1;
v8 = v10 * 8 + 8;
a1[i] = (char *)v15;
v5 = environ;
v7 = environ;
if ( environ[v10 + 1] )
goto LABEL_4;
LABEL_9:
if ( !v20 )
{
v17 = (char **)((char *)a1 + v8);
v18 = 8 * i + 16;
LABEL_12:
v21 = v18;
v19 = my_stricat(*a2, a3, 61LL);
v7 = environ;
v8 = v21;
*v17 = v19;
}
*(char **)((char *)a1 + v8) = 0LL;
free(v7);
environ = a1;
return 0LL;
}

original_code:
static int fill_env(char **tab, t_words *cmd, char *s)
{
  size_t i;
  unsigned char n;
  i = (n = 0);
  while (environ[i])
    {
      if (!((((cmd->word) && (environ[i])) ? (!my_strncmp((cmd->word), (environ[i]), my_strlen(cmd->word))) : (0))
     && environ[i][my_strlen(cmd->word)] == '='))
 tab[i] = environ[i];
      else
 {
   n = 1;
   free(environ[i]);
   tab[i] = my_stricat(cmd->word, s, '=');
 }
      i++;
    }
  if (n == 0)
    tab[i++] = my_stricat(cmd->word, s, '=');
  tab[i] = ((void *)0);
  free(environ);
  environ = tab;
  return (0);
}

canonical_original_code:
static int fill_env(char **tab, struct s_words *cmd, char *s)
{
  unsigned long i;
  unsigned char n;
  i = (n = 0);
  while (environ[i])
    {
      if (!((((cmd->word) && (environ[i])) ? (!my_strncmp((cmd->word), (environ[i]), my_strlen(cmd->word))) : (0))
     && environ[i][my_strlen(cmd->word)] == '='))
 tab[i] = environ[i];
      else
 {
   n = 1;
   free(environ[i]);
   tab[i] = my_stricat(cmd->word, s, '=');
 }
      i++;
    }
  if (n == 0)
    tab[i++] = my_stricat(cmd->word, s, '=');
  tab[i] = ((void *)0);
  free(environ);
  environ = tab;
  return (0);
}

variable_types:
n
unsigned char


canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

if ( !ptr )
return 0LL;
v1 = (void **)ptr;
do
{
v2 = v1;
v1 = (void **)v1[2];
free(*v2);
free(v2);
}
while ( v1 );
return 0LL;
}

variable_types: struct s_get { char *word; unsigned char inter; struct s_get *next; struct s_get *prev; } *


original_code:
int PBI_BB_Initialise(int *argc, char *argv[])
{
 int i, j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-bb") == 0) {
   init_bb();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-bb              Emulate the CSS Black Box");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

canonical_original_code:
int PBI_BB_Initialise(int *argc, char *argv[])
{
 int i; int j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-bb") == 0) {
   init_bb();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-bb              Emulate the CSS Black Box");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: i int

variable_types: j int


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

if ( !strcmp(a1, "BLACK_BOX_ROM") )
{
Util_strlcpy(bb_rom_filename, a2, 4096LL);
result = 1LL;
}
else if ( !strcmp(a1, "BB_SCSI_DISK") )
{
Util_strlcpy(&bb_scsi_disk_filename, a2, 4096LL);
result = 1LL;
}
else
{
result = 0LL;
}
return result;
}

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

if ( !strcmp(a1, "BLACK_BOX_ROM") )
{
Util_strlcpy(bb_rom_filename, a2, 4096LL);
result = 1LL;
}
else if ( !strcmp(a1, "BB_SCSI_DISK") )
{
Util_strlcpy(&bb_scsi_disk_filename, a2, 4096LL);
result = 1LL;
}
else
{
result = 0LL;
}
return result;
}

original_code:
int PBI_BB_ReadConfig(char *string, char *ptr)
{
 if (strcmp(string, "BLACK_BOX_ROM") == 0)
  Util_strlcpy(bb_rom_filename, ptr, sizeof(bb_rom_filename));
 else if (strcmp(string, "BB_SCSI_DISK") == 0)
  Util_strlcpy(bb_scsi_disk_filename, ptr, sizeof(bb_scsi_disk_filename));
 else return 0;
 return 1;
}

variable_types:
string
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall PBI_BB_D1GetByte(__int16 a1, __int64 a2, __int64 a3, int a4)
{
__int64 result; // rax
_DWORD *v5; // rsi
_DWORD *v6; // r8
_DWORD *v7; // rax
unsigned __int8 v8; // [rsp+1h] [rbp-9h]

result = 0xFFFFFFFFLL;
if ( a1 != -11842 )
{
switch ( a1 )
{
case -11920:
v5 = &PBI_SCSI_REQ;
v6 = &PBI_SCSI_BSY;
v7 = &PBI_SCSI_CD;
LOBYTE(v5) = PBI_SCSI_BSY == 0;
LOBYTE(a4) = PBI_SCSI_SEL == 0;
LOBYTE(v6) = PBI_SCSI_CD == 0;
LOBYTE(v7) = PBI_SCSI_IO == 0;
result = (2 * (_DWORD)v6) | (4 * a4) | ((_DWORD)v5 << 6) | (PBI_SCSI_REQ == 0 ? 0xFFFFFF80 : 0) | (unsigned int)v7;
break;
case -11919:
result = 0LL;
if ( bb_scsi_enabled )
{
result = PBI_SCSI_GetByte();
if ( (((int)(unsigned __int8)bb_PCR >> 1) & 7) == 4 )
{
v8 = result;
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
result = v8;
}
}
break;
case -11844:
result = (unsigned __int8)byte_2B39;
break;
default:
result = 0LL;
if ( a1 == -11777 )
result = PBI_IRQ != 0 ? 0xA : 0;
break;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1, __int64 a2, __int64 a3, int a4)
{
__int64 result; // rax
_DWORD *v5; // rsi
_DWORD *v6; // r8
_DWORD *v7; // rax
unsigned __int8 v8; // [rsp+1h] [rbp-9h]

result = 0xFFFFFFFFLL;
if ( a1 != -11842 )
{
switch ( a1 )
{
case -11920:
v5 = &PBI_SCSI_REQ;
v6 = &PBI_SCSI_BSY;
v7 = &PBI_SCSI_CD;
LOBYTE(v5) = PBI_SCSI_BSY == 0;
LOBYTE(a4) = PBI_SCSI_SEL == 0;
LOBYTE(v6) = PBI_SCSI_CD == 0;
LOBYTE(v7) = PBI_SCSI_IO == 0;
result = (2 * (_DWORD)v6) | (4 * a4) | ((_DWORD)v5 << 6) | (PBI_SCSI_REQ == 0 ? 0xFFFFFF80 : 0) | (unsigned int)v7;
break;
case -11919:
result = 0LL;
if ( bb_scsi_enabled )
{
result = PBI_SCSI_GetByte();
if ( (((int)(unsigned __int8)bb_PCR >> 1) & 7) == 4 )
{
v8 = result;
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
result = v8;
}
}
break;
case -11844:
result = (unsigned __int8)byte_2B39;
break;
default:
result = 0LL;
if ( a1 == -11777 )
result = PBI_IRQ != 0 ? 0xA : 0;
break;
}
}
return result;
}

original_code:
unsigned char PBI_BB_D1GetByte(unsigned short addr)
{
 unsigned char result = 0x00;
 if (addr == 0xd1be) result = 0xff;
 else if (addr == 0xd170) {
  result = ((!(PBI_SCSI_REQ))<<7)|((!PBI_SCSI_BSY)<<6)|((!PBI_SCSI_SEL)<<2)|((!PBI_SCSI_CD)<<1)|(!PBI_SCSI_IO);
 }
 else if (addr == 0xd171) {
  if (bb_scsi_enabled) {
   result = PBI_SCSI_GetByte();
   if (((bb_PCR & 0x0e)>>1) == 0x04) {
    PBI_SCSI_PutACK(1);
    PBI_SCSI_PutACK(0);
   }
  }
 }
 else if (addr == 0xd1bc) result = (bb_ram_bank_offset >> 8);
 else if (addr == 0xd1be) {
  result = 0x02;
 }
 else if (addr == 0xd1ff) result = PBI_IRQ ? (0x08 | 0x02) : 0 ;
 do{}while(0);
 return result;
}

variable_types:
addr
unsigned short

variable_types:
result
unsigned char


decompiled_code:
__int64 __fastcall PBI_BB_D1PutByte(__int16 a1, unsigned int a2)
{
__int64 result; // rax
int v3; // esi
char *v4; // rsi
char *v5; // r11
__int64 v6; // rdx
int v7; // er9
char *v8; // rsi
__int64 v9; // r9
const __m128i *v10; // rsi
__m128i v11; // xmm15

result = a2;
switch ( a1 )
{
case -11920:
if ( bb_scsi_enabled )
result = PBI_SCSI_PutSEL((((unsigned __int8)a2 >> 2) ^ 1) & 1);
break;
case -11919:
if ( bb_scsi_enabled )
{
result = PBI_SCSI_PutByte((unsigned __int8)a2);
if ( (((int)(unsigned __int8)bb_PCR >> 1) & 7) == 4 )
{
PBI_SCSI_PutACK(1LL);
result = PBI_SCSI_PutACK(0LL);
}
}
break;
case -11908:
bb_PCR = a2;
result = (a2 >> 1) & 7;
if ( (_DWORD)result == 6 )
{
if ( bb_scsi_enabled )
result = PBI_SCSI_PutACK(1LL);
}
else if ( (_DWORD)result == 7 && bb_scsi_enabled )
{
result = PBI_SCSI_PutACK(0LL);
}
break;
case -11844:
v9 = bb_ram + bb_ram_bank_offset;
result = (unsigned __int8)a2 << 8;
*(__m128i *)v9 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3424);
*(__m128i *)(v9 + 16) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3425);
*(__m128i *)(v9 + 32) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3426);
*(__m128i *)(v9 + 48) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3427);
*(__m128i *)(v9 + 64) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3428);
*(__m128i *)(v9 + 80) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3429);
*(__m128i *)(v9 + 96) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3430);
*(__m128i *)(v9 + 112) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3431);
*(__m128i *)(v9 + 128) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3432);
*(__m128i *)(v9 + 144) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3433);
*(__m128i *)(v9 + 160) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3434);
*(__m128i *)(v9 + 176) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3435);
*(__m128i *)(v9 + 192) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3436);
*(__m128i *)(v9 + 208) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3437);
*(__m128i *)(v9 + 224) = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3438);
v10 = (const __m128i *)(bb_ram + (int)result);
v11 = _mm_loadu_si128((const __m128i *)&MEMORY_mem + 3439);
bb_ram_bank_offset = result;
*(__m128i *)(v9 + 240) = v11;
*((__m128i *)&MEMORY_mem + 3424) = _mm_loadu_si128(v10);
*((__m128i *)&MEMORY_mem + 3425) = _mm_loadu_si128(v10 + 1);
*((__m128i *)&MEMORY_mem + 3426) = _mm_loadu_si128(v10 + 2);
*((__m128i *)&MEMORY_mem + 3427) = _mm_loadu_si128(v10 + 3);
*((__m128i *)&MEMORY_mem + 3428) = _mm_loadu_si128(v10 + 4);
*((__m128i *)&MEMORY_mem + 3429) = _mm_loadu_si128(v10 + 5);
*((__m128i *)&MEMORY_mem + 3430) = _mm_loadu_si128(v10 + 6);
*((__m128i *)&MEMORY_mem + 3431) = _mm_loadu_si128(v10 + 7);
*((__m128i *)&MEMORY_mem + 3432) = _mm_loadu_si128(v10 + 8);
*((__m128i *)&MEMORY_mem + 3433) = _mm_loadu_si128(v10 + 9);
*((__m128i *)&MEMORY_mem + 3434) = _mm_loadu_si128(v10 + 10);
*((__m128i *)&MEMORY_mem + 3435) = _mm_loadu_si128(v10 + 11);
*((__m128i *)&MEMORY_mem + 3436) = _mm_loadu_si128(v10 + 12);
*((__m128i *)&MEMORY_mem + 3437) = _mm_loadu_si128(v10 + 13);
*((__m128i *)&MEMORY_mem + 3438) = _mm_loadu_si128(v10 + 14);
*((__m128i *)&MEMORY_mem + 3439) = _mm_loadu_si128(v10 + 15);
break;
case -11842:
v7 = (4 * (_BYTE)a2) & 0x10;
if ( v7 != bb_rom_high_bit && bb_rom_size == 0x10000 )
{
result = (unsigned __int8)bb_rom_bank;
bb_rom_high_bit = (4 * (_BYTE)a2) & 0x10;
if ( (unsigned __int8)(bb_rom_bank - 1) <= 6u )
{
v8 = (char *)bb_rom + 2048 * ((unsigned __int8)bb_rom_bank + v7);
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v8;
*((_QWORD *)&MEMORY_mem + 7167) = *((_QWORD *)v8 + 255);
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v8
- ((char *)&MEMORY_mem
- (((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)
+ 55296)),
8LL * (((unsigned int)&MEMORY_mem + 55296 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
}
}
break;
default:
if ( (a1 & 0xFFC0) == 0xD1C0 && bb_rom_bank != (_BYTE)a2 )
{
if ( bb_rom_size == 0x4000 )
{
if ( (unsigned __int8)(a2 - 8) <= 7u )
{
v3 = ((unsigned __int8)a2 - 8) << 11;
goto LABEL_11;
}
if ( (unsigned __int8)(a2 - 1) <= 6u )
{
v3 = (unsigned __int8)a2 << 11;
goto LABEL_11;
}
}
else if ( (unsigned __int8)(a2 - 1) <= 0xEu )
{
v3 = (bb_rom_high_bit + (unsigned __int8)a2) << 11;
LABEL_11:
v4 = (char *)bb_rom + v3;
v5 = (char *)&MEMORY_mem + 55296;
*((_QWORD *)&MEMORY_mem + 6912) = *(_QWORD *)v4;
v6 = *((_QWORD *)v4 + 255);
LABEL_12:
*((_QWORD *)&MEMORY_mem + 7167) = v6;
qmemcpy(
(void *)(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(v4 - &v5[-(((unsigned __int64)&MEMORY_mem + 55304) & 0xFFFFFFFFFFFFFFF8LL)]),
8LL * (((unsigned int)v5 - (((unsigned int)&MEMORY_mem + 55304) & 0xFFFFFFF8) + 2048) >> 3));
bb_rom_bank = result;
return result;
}
v5 = (char *)&MEMORY_mem + 55296;
v4 = (char *)&MEMORY_os[768];
*((_QWORD *)&MEMORY_mem + 6912) = MEMORY_os[768];
v6 = MEMORY_os[1023];
goto LABEL_12;
}
break;
}
return result;
}

original_code:
void PBI_BB_D1PutByte(unsigned short addr, unsigned char byte)
{
 do{}while(0);
 if (addr == 0xd170) {
  if (bb_scsi_enabled) PBI_SCSI_PutSEL(!(byte&0x04));
 }
 else if (addr == 0xd171) {
  if (bb_scsi_enabled) {
   PBI_SCSI_PutByte(byte);
   if (((bb_PCR & 0x0e)>>1) == 0x04) {
    PBI_SCSI_PutACK(1);
    PBI_SCSI_PutACK(0);
   }
  }
 }
 else if (addr == 0xd17c) {
  bb_PCR = byte;
  if (((bb_PCR & 0x0e)>>1) == 0x06) {
   if (bb_scsi_enabled) PBI_SCSI_PutACK(1);
  }
  else if (((bb_PCR & 0x0e)>>1) == 0x07) {
   if (bb_scsi_enabled) PBI_SCSI_PutACK(0);
  }
 }
 else if (addr == 0xd1bc) {
  memcpy(bb_ram+bb_ram_bank_offset,MEMORY_mem + 0xd600,0x100);
  bb_ram_bank_offset = (byte << 8);
  memcpy(MEMORY_mem + 0xd600, bb_ram+bb_ram_bank_offset, 0x100);
 }
 else if (addr == 0xd1be) {
  if (bb_rom_high_bit != ((byte & 0x04) << 2) && bb_rom_size == 0x10000) {
   bb_rom_high_bit = ((byte & 0x04) << 2);
   if (bb_rom_bank > 0 && bb_rom_bank < 8) {
     memcpy(MEMORY_mem + 0xd800, bb_rom + (bb_rom_bank + bb_rom_high_bit)*0x800, 0x800);
     do{}while(0);
   }
  }
 }
 else if ((addr & 0xffc0) == 0xd1c0) {
  if (bb_rom_bank != byte) {
   int offset = -1;
   if (bb_rom_size == 0x4000) {
    if (byte >= 8 && byte <= 0x0f) offset = (byte - 8)*0x800;
    else if (byte > 0 && byte < 0x08) offset = byte*0x800;
   }
   else {
    if (byte > 0 && byte < 0x10) offset = (byte + bb_rom_high_bit)*0x800;
   }
   if (offset != -1) {
     memcpy(MEMORY_mem + 0xd800, bb_rom + offset, 0x800);
     do{}while(0);
   }
   else {
     memcpy(MEMORY_mem + 0xd800, MEMORY_os + 0x1800, 0x800);
     if (byte != 0) do{}while(0);
     do{}while(0);
   }
   bb_rom_bank = byte;
  }
 }
}

variable_types:
byte
unsigned char

variable_types: offset int


decompiled_code:
__int64 __fastcall PBI_BB_D6GetByte(unsigned __int16 a1)
{
return *((unsigned __int8 *)&MEMORY_mem + a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int16 a1)
{
return *((unsigned __int8 *)&MEMORY_mem + a1);
}

original_code:
unsigned char PBI_BB_D6GetByte(unsigned short addr)
{
 return MEMORY_mem[addr];
}


decompiled_code:
void *__fastcall PBI_BB_D6PutByte(unsigned __int16 a1, char a2)
{
void *result; // rax

result = &MEMORY_mem;
*((_BYTE *)&MEMORY_mem + a1) = a2;
return result;
}

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

result = &MEMORY_mem;
*((_BYTE *)&MEMORY_mem + a1) = a2;
return result;
}

original_code:
void PBI_BB_D6PutByte(unsigned short addr, unsigned char byte)
{
 MEMORY_mem[addr]=byte;
}


decompiled_code:
_DWORD *PBI_BB_Menu()
{
_DWORD *result; // rax

result = &PBI_BB_enabled;
if ( PBI_BB_enabled )
{
if ( !buttondown )
{
buttondown = 1;
PBI_IRQ |= 1u;
CPU_IRQ = 1;
}
}
return result;
}

canonical_decompiled_code:
_DWORD *func8()
{
_DWORD *result; // rax

result = &PBI_BB_enabled;
if ( PBI_BB_enabled )
{
if ( !buttondown )
{
buttondown = 1;
PBI_IRQ |= 1u;
CPU_IRQ = 1;
}
}
return result;
}

original_code:
void PBI_BB_Menu(void)
{
 if (!PBI_BB_enabled) return;
 if (buttondown == 0) {
  do{}while(0);
  (CPU_IRQ = 1);
  PBI_IRQ |= 1;
  buttondown = 1;
 }
}

canonical_original_code:
void PBI_BB_Menu()
{
 if (!PBI_BB_enabled) return;
 if (buttondown == 0) {
  do{}while(0);
  (CPU_IRQ = 1);
  PBI_IRQ |= 1;
  buttondown = 1;
 }
}


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

result = (unsigned int)buttondown;
if ( buttondown )
{
if ( count_0 > 0 )
{
PBI_IRQ &= 0xFFFFFFFE;
result = POKEY_PutByte(14LL, POKEY_IRQEN);
buttondown = 0;
count_0 = 0;
}
else
{
++count_0;
}
}
return result;
}

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

result = (unsigned int)buttondown;
if ( buttondown )
{
if ( count_0 > 0 )
{
PBI_IRQ &= 0xFFFFFFFE;
result = POKEY_PutByte(14LL, POKEY_IRQEN);
buttondown = 0;
count_0 = 0;
}
else
{
++count_0;
}
}
return result;
}

original_code:
void PBI_BB_Frame(void)
{
 static int count = 0;
 if (buttondown) {
   if (count < 1) count++;
  else {
   do{}while(0);
   PBI_IRQ &= ~1;
   POKEY_PutByte(0x0e, POKEY_IRQEN);
   buttondown = 0;
   count = 0;
  }
 }
}

canonical_original_code:
void PBI_BB_Frame()
{
 static int count = 0;
 if (buttondown) {
   if (count < 1) count++;
  else {
   do{}while(0);
   PBI_IRQ &= ~1;
   POKEY_PutByte(0x0e, POKEY_IRQEN);
   buttondown = 0;
   count = 0;
  }
 }
}

variable_types: count int


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

StateSav_SaveINT(&PBI_BB_enabled, 1LL);
result = (unsigned int)PBI_BB_enabled;
if ( PBI_BB_enabled )
{
StateSav_SaveFNAME(&bb_scsi_disk_filename);
StateSav_SaveFNAME(bb_rom_filename);
StateSav_SaveINT(&bb_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(bb_ram, 0x10000LL);
StateSav_SaveUBYTE(&bb_rom_bank, 1LL);
StateSav_SaveINT(&bb_rom_high_bit, 1LL);
result = StateSav_SaveUBYTE(&bb_PCR, 1LL);
}
return result;
}

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

StateSav_SaveINT(&PBI_BB_enabled, 1LL);
result = (unsigned int)PBI_BB_enabled;
if ( PBI_BB_enabled )
{
StateSav_SaveFNAME(&bb_scsi_disk_filename);
StateSav_SaveFNAME(bb_rom_filename);
StateSav_SaveINT(&bb_ram_bank_offset, 1LL);
StateSav_SaveUBYTE(bb_ram, 0x10000LL);
StateSav_SaveUBYTE(&bb_rom_bank, 1LL);
StateSav_SaveINT(&bb_rom_high_bit, 1LL);
result = StateSav_SaveUBYTE(&bb_PCR, 1LL);
}
return result;
}

original_code:
void PBI_BB_StateSave(void)
{
 StateSav_SaveINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_SaveFNAME(bb_scsi_disk_filename);
  StateSav_SaveFNAME(bb_rom_filename);
  StateSav_SaveINT(&bb_ram_bank_offset, 1);
  StateSav_SaveUBYTE(bb_ram, 0x10000);
  StateSav_SaveUBYTE(&bb_rom_bank, 1);
  StateSav_SaveINT(&bb_rom_high_bit, 1);
  StateSav_SaveUBYTE(&bb_PCR, 1);
 }
}

canonical_original_code:
void PBI_BB_StateSave()
{
 StateSav_SaveINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_SaveFNAME(bb_scsi_disk_filename);
  StateSav_SaveFNAME(bb_rom_filename);
  StateSav_SaveINT(&bb_ram_bank_offset, 1);
  StateSav_SaveUBYTE(bb_ram, 0x10000);
  StateSav_SaveUBYTE(&bb_rom_bank, 1);
  StateSav_SaveINT(&bb_rom_high_bit, 1);
  StateSav_SaveUBYTE(&bb_PCR, 1);
 }
}


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

StateSav_ReadINT(&PBI_BB_enabled, 1LL);
result = (unsigned int)PBI_BB_enabled;
if ( PBI_BB_enabled )
{
StateSav_ReadFNAME(&bb_scsi_disk_filename);
StateSav_ReadFNAME(bb_rom_filename);
init_bb();
StateSav_ReadINT(&bb_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(bb_ram, 0x10000LL);
StateSav_ReadUBYTE(&bb_rom_bank, 1LL);
StateSav_ReadINT(&bb_rom_high_bit, 1LL);
result = StateSav_ReadUBYTE(&bb_PCR, 1LL);
}
return result;
}

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

StateSav_ReadINT(&PBI_BB_enabled, 1LL);
result = (unsigned int)PBI_BB_enabled;
if ( PBI_BB_enabled )
{
StateSav_ReadFNAME(&bb_scsi_disk_filename);
StateSav_ReadFNAME(bb_rom_filename);
func0();
StateSav_ReadINT(&bb_ram_bank_offset, 1LL);
StateSav_ReadUBYTE(bb_ram, 0x10000LL);
StateSav_ReadUBYTE(&bb_rom_bank, 1LL);
StateSav_ReadINT(&bb_rom_high_bit, 1LL);
result = StateSav_ReadUBYTE(&bb_PCR, 1LL);
}
return result;
}

original_code:
void PBI_BB_StateRead(void)
{
 StateSav_ReadINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_ReadFNAME(bb_scsi_disk_filename);
  StateSav_ReadFNAME(bb_rom_filename);
  init_bb();
  StateSav_ReadINT(&bb_ram_bank_offset, 1);
  StateSav_ReadUBYTE(bb_ram, 0x10000);
  StateSav_ReadUBYTE(&bb_rom_bank, 1);
  StateSav_ReadINT(&bb_rom_high_bit, 1);
  StateSav_ReadUBYTE(&bb_PCR, 1);
 }
}

canonical_original_code:
void PBI_BB_StateRead()
{
 StateSav_ReadINT(&PBI_BB_enabled, 1);
 if (PBI_BB_enabled) {
  StateSav_ReadFNAME(bb_scsi_disk_filename);
  StateSav_ReadFNAME(bb_rom_filename);
  init_bb();
  StateSav_ReadINT(&bb_ram_bank_offset, 1);
  StateSav_ReadUBYTE(bb_ram, 0x10000);
  StateSav_ReadUBYTE(&bb_rom_bank, 1);
  StateSav_ReadINT(&bb_rom_high_bit, 1);
  StateSav_ReadUBYTE(&bb_PCR, 1);
 }
}


decompiled_code:
_BOOL8 __fastcall Util_chrieq(unsigned __int8 a1, unsigned __int8 a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 != a2 )
{
result = 0LL;
if ( (a1 ^ a2) == 32 )
result = (unsigned __int8)((a1 & 0xDF) - 65) <= 0x19u;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(unsigned __int8 a1, unsigned __int8 a2)
{
_BOOL8 result; // rax

result = 1LL;
if ( a1 != a2 )
{
result = 0LL;
if ( (a1 ^ a2) == 32 )
result = (unsigned __int8)((a1 & 0xDF) - 65) <= 0x19u;
}
return result;
}

original_code:
int Util_chrieq(char c1, char c2)
{
 switch (c1 ^ c2) {
 case 0x00:
  return 1;
 case 0x20:
  return (c1 >= 'A' && c1 <= 'Z') || (c1 >= 'a' && c1 <= 'z');
 default:
  return 0;
 }
}


decompiled_code:
__int64 __fastcall Util_stpcpy(void *dest, void *src)
{
size_t v2; // rbp

v2 = strlen((const char *)src);
memcpy(dest, src, v2 + 1);
return (__int64)dest + v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *dest, void *src)
{
size_t v2; // rbp

v2 = strlen((const char *)src);
memcpy(dest, src, v2 + 1);
return (__int64)dest + v2;
}

original_code:
char *Util_stpcpy(char *dest, const char *src)
{
 size_t len = strlen(src);
 memcpy(dest, src, len + 1);
 return dest + len;
}

canonical_original_code:
char *Util_stpcpy(char *dest, char *src)
{
 unsigned long len = strlen(src);
 memcpy(dest, src, len + 1);
 return dest + len;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
len
unsigned long

return_type: char *


decompiled_code:
char *__fastcall safe_strncpy(char *a1, const char *a2, int a3)
{
char *v3; // r8
char *v5; // rax

v3 = a1;
if ( !a1 )
return 0LL;
if ( a3 > 0 )
{
if ( !a2 )
a2 = &LC0;
v5 = strncpy(a1, a2, a3);
v5[a3 - 1] = 0;
v3 = v5;
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1, const char *a2, int a3)
{
char *v3; // r8
char *v5; // rax

v3 = a1;
if ( !a1 )
return 0LL;
if ( a3 > 0 )
{
if ( !a2 )
a2 = &LC0;
v5 = strncpy(a1, a2, a3);
v5[a3 - 1] = 0;
v3 = v5;
}
return v3;
}

original_code:
char *safe_strncpy(char *buffer, const char *source, int bufsize)
{
 if (buffer == ((void *)0)) return ((void *)0);
 if (bufsize > 0) {
  strncpy(buffer, source != ((void *)0) ? source : "", bufsize);
  buffer[bufsize-1] = '\0';
 }
 return buffer;
}

canonical_original_code:
char *safe_strncpy(char *buffer, char *source, int bufsize)
{
 if (buffer == ((void *)0)) return ((void *)0);
 if (bufsize > 0) {
  strncpy(buffer, source != ((void *)0) ? source : "", bufsize);
  buffer[bufsize-1] = '\0';
 }
 return buffer;
}

variable_types:
buffer
char *

variable_types:
source
char *

variable_types: bufsize int


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

result = strncpy(a1, a2, a3);
result[a3 - 1] = 0;
return result;
}

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

result = strncpy(a1, a2, a3);
result[a3 - 1] = 0;
return result;
}

original_code:
char *Util_strlcpy(char *dest, const char *src, size_t size)
{
 strncpy(dest, src, size);
 dest[size - 1] = '\0';
 return dest;
}

canonical_original_code:
char *Util_strlcpy(char *dest, char *src, unsigned long size)
{
 strncpy(dest, src, size);
 dest[size - 1] = '\0';
 return dest;
}

variable_types:
size
unsigned long


decompiled_code:
const char *__fastcall Util_chomp(const char *a1)
{
const char *result; // rax
char *v2; // rdx
char v3; // cl

result = (const char *)strlen(a1);
if ( (int)result > 1 )
{
v2 = (char *)&a1[(int)result - 1];
v3 = *v2;
if ( *v2 == 10 )
{
result = &a1[(int)result - 2];
if ( *result == 13 )
{
*result = 0;
return result;
}
goto LABEL_6;
}
LABEL_3:
if ( v3 != 13 )
return result;
LABEL_6:
*v2 = 0;
return result;
}
if ( (_DWORD)result == 1 )
{
v3 = *a1;
v2 = (char *)a1;
if ( *a1 == 10 )
goto LABEL_6;
goto LABEL_3;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(const char *a1)
{
const char *result; // rax
char *v2; // rdx
char v3; // cl

result = (const char *)strlen(a1);
if ( (int)result > 1 )
{
v2 = (char *)&a1[(int)result - 1];
v3 = *v2;
if ( *v2 == 10 )
{
result = &a1[(int)result - 2];
if ( *result == 13 )
{
*result = 0;
return result;
}
goto LABEL_6;
}
LABEL_3:
if ( v3 != 13 )
return result;
LABEL_6:
*v2 = 0;
return result;
}
if ( (_DWORD)result == 1 )
{
v3 = *a1;
v2 = (char *)a1;
if ( *a1 == 10 )
goto LABEL_6;
goto LABEL_3;
}
return result;
}

variable_types:
s
char *

variable_types: len int


decompiled_code:
unsigned __int64 __fastcall Util_trim(void *dest)
{
void *v1; // r12
unsigned __int64 result; // rax
__int64 v3; // rdx
unsigned __int8 *v4; // rbp
__int64 v5; // rsi
_BYTE *i; // rax
unsigned __int64 v7; // rdi

v1 = dest;
result = *(unsigned __int8 *)dest;
if ( (unsigned __int8)result > 0x20u )
{
v4 = (unsigned __int8 *)dest;
}
else
{
v3 = 0x100002600LL;
v4 = (unsigned __int8 *)dest;
while ( _bittest64(&v3, result) )
{
result = *++v4;
if ( (unsigned __int8)result > 0x20u )
goto LABEL_5;
}
if ( !(_BYTE)result )
{
*(_BYTE *)dest = 0;
return result;
}
}
LABEL_5:
v5 = 0x100002600LL;
for ( i = (char *)dest + strlen((const char *)dest); ; --i )
{
v7 = (unsigned __int8)*(i - 1);
if ( (unsigned __int8)v7 > 0x20u || !_bittest64(&v5, v7) )
break;
}
*i = 0;
return (unsigned __int64)memmove(v1, v4, i + 1 - v4);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(void *dest)
{
void *v1; // r12
unsigned __int64 result; // rax
__int64 v3; // rdx
unsigned __int8 *v4; // rbp
__int64 v5; // rsi
_BYTE *i; // rax
unsigned __int64 v7; // rdi

v1 = dest;
result = *(unsigned __int8 *)dest;
if ( (unsigned __int8)result > 0x20u )
{
v4 = (unsigned __int8 *)dest;
}
else
{
v3 = 0x100002600LL;
v4 = (unsigned __int8 *)dest;
while ( _bittest64(&v3, result) )
{
result = *++v4;
if ( (unsigned __int8)result > 0x20u )
goto LABEL_5;
}
if ( !(_BYTE)result )
{
*(_BYTE *)dest = 0;
return result;
}
}
LABEL_5:
v5 = 0x100002600LL;
for ( i = (char *)dest + strlen((const char *)dest); ; --i )
{
v7 = (unsigned __int8)*(i - 1);
if ( (unsigned __int8)v7 > 0x20u || !_bittest64(&v5, v7) )
break;
}
*i = 0;
return (unsigned __int64)memmove(v1, v4, i + 1 - v4);
}

variable_types:
p
char *

variable_types:
q
char *


decompiled_code:
__int64 __fastcall Util_sscandec(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // esi
bool v3; // zf
__int64 result; // rax

v1 = (char)*a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
++a1;
v2 = v1 + 10 * v2 - 48;
v1 = (char)*a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
v3 = (_BYTE)v1 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // esi
bool v3; // zf
__int64 result; // rax

v1 = (char)*a1;
if ( (unsigned __int8)(*a1 - 48) > 9u )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
++a1;
v2 = v1 + 10 * v2 - 48;
v1 = (char)*a1;
}
while ( (unsigned __int8)(*a1 - 48) <= 9u );
v3 = (_BYTE)v1 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

original_code:
int Util_sscandec(const char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 10 * result + *s - '0';
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

canonical_original_code:
int Util_sscandec(char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 10 * result + *s - '0';
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

variable_types: result int


decompiled_code:
_BOOL8 __fastcall Util_sscansdec(_BYTE *a1, _DWORD *a2)
{
int v2; // eax
_BOOL8 result; // rax
int v4; // eax

if ( *a1 == 43 )
{
++a1;
goto LABEL_6;
}
if ( *a1 != 45 )
{
LABEL_6:
v4 = Util_sscandec(a1);
*a2 = v4;
return v4 != -1;
}
v2 = Util_sscandec(a1 + 1);
if ( v2 == -1 )
{
*a2 = -1;
result = 0LL;
}
else
{
*a2 = -v2;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(_BYTE *a1, _DWORD *a2)
{
int v2; // eax
_BOOL8 result; // rax
int v4; // eax

if ( *a1 == 43 )
{
++a1;
goto LABEL_6;
}
if ( *a1 != 45 )
{
LABEL_6:
v4 = func6(a1);
*a2 = v4;
return v4 != -1;
}
v2 = func6(a1 + 1);
if ( v2 == -1 )
{
*a2 = -1;
result = 0LL;
}
else
{
*a2 = -v2;
result = 1LL;
}
return result;
}

original_code:
int Util_sscansdec(char const *s, int *dest)
{
 int minus = 0;
 switch(*s) {
 case '-':
  minus = 1;
 case '+':
  ++s;
 }
 *dest = Util_sscandec(s);
 if (*dest == -1)
  return 0;
 if (minus)
  *dest = -*dest;
 return 1;
}

canonical_original_code:
int Util_sscansdec(char *s, int *dest)
{
 int minus = 0;
 switch(*s) {
 case '-':
  minus = 1;
 case '+':
  ++s;
 }
 *dest = Util_sscandec(s);
 if (*dest == -1)
  return 0;
 if (minus)
  *dest = -*dest;
 return 1;
}

variable_types:
dest
int *

variable_types: minus int


decompiled_code:
__int64 __fastcall Util_sscandouble(const char *a1, double *a2)
{
unsigned int v2; // er12
double v4; // [rsp+8h] [rbp-30h]
char *endptr; // [rsp+18h] [rbp-20h] BYREF

v2 = 0;
v4 = strtod(a1, &endptr);
if ( !*endptr && *_errno_location() != 34 )
{
v2 = 1;
*a2 = v4;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1, double *a2)
{
unsigned int v2; // er12
double v4; // [rsp+8h] [rbp-30h]
char *endptr; // [rsp+18h] [rbp-20h] BYREF

v2 = 0;
v4 = strtod(a1, &endptr);
if ( !*endptr && *_errno_location() != 34 )
{
v2 = 1;
*a2 = v4;
}
return v2;
}

original_code:
int Util_sscandouble(char const *s, double *dest)
{
 char *endptr;
 double result;
 result = strtod(s, &endptr);
 if (endptr[0] != '\0' || (*__errno_location ()) == 34)
  return 0;
 *dest = result;
 return 1;
}

canonical_original_code:
int Util_sscandouble(char *s, double *dest)
{
 char *endptr;
 double result;
 result = strtod(s, &endptr);
 if (endptr[0] != '\0' || (*__errno_location ()) == 34)
  return 0;
 *dest = result;
 return 1;
}

variable_types:
dest
double *

variable_types:
endptr
char *

variable_types: result double


decompiled_code:
__int64 __fastcall Util_sscanhex(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // edx
bool v3; // zf
__int64 result; // rax

v1 = (char)*a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = 0;
while ( 1 )
{
if ( (unsigned __int8)(v1 - 48) <= 9u )
{
v2 = 16 * v2 + v1 - 48;
goto LABEL_4;
}
if ( (unsigned __int8)(v1 - 65) <= 5u )
{
v2 = 16 * v2 + v1 - 55;
goto LABEL_4;
}
if ( (unsigned __int8)(v1 - 97) > 5u )
break;
v2 = 16 * v2 + v1 - 87;
LABEL_4:
v1 = (char)*++a1;
}
v3 = (_BYTE)v1 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // edx
bool v3; // zf
__int64 result; // rax

v1 = (char)*a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = 0;
while ( 1 )
{
if ( (unsigned __int8)(v1 - 48) <= 9u )
{
v2 = 16 * v2 + v1 - 48;
goto LABEL_4;
}
if ( (unsigned __int8)(v1 - 65) <= 5u )
{
v2 = 16 * v2 + v1 - 55;
goto LABEL_4;
}
if ( (unsigned __int8)(v1 - 97) > 5u )
break;
v2 = 16 * v2 + v1 - 87;
LABEL_4:
v1 = (char)*++a1;
}
v3 = (_BYTE)v1 == 0;
result = 0xFFFFFFFFLL;
if ( v3 )
result = v2;
return result;
}

original_code:
int Util_sscanhex(const char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 16 * result + *s - '0';
  else if (*s >= 'A' && *s <= 'F')
   result = 16 * result + *s - 'A' + 10;
  else if (*s >= 'a' && *s <= 'f')
   result = 16 * result + *s - 'a' + 10;
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}

canonical_original_code:
int Util_sscanhex(char *s)
{
 int result;
 if (*s == '\0')
  return -1;
 result = 0;
 for (;;) {
  if (*s >= '0' && *s <= '9')
   result = 16 * result + *s - '0';
  else if (*s >= 'A' && *s <= 'F')
   result = 16 * result + *s - 'A' + 10;
  else if (*s >= 'a' && *s <= 'f')
   result = 16 * result + *s - 'a' + 10;
  else if (*s == '\0')
   return result;
  else
   return -1;
  s++;
 }
}


decompiled_code:
__int64 __fastcall Util_sscanbool(_BYTE *a1)
{
__int64 result; // rax

if ( *a1 == 48 )
return (unsigned int)-(a1[1] != 0);
result = 0xFFFFFFFFLL;
if ( *a1 == 49 )
result = a1[1] == 0 ? 1 : -1;
return result;
}

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

if ( *a1 == 48 )
return (unsigned int)-(a1[1] != 0);
result = 0xFFFFFFFFLL;
if ( *a1 == 49 )
result = a1[1] == 0 ? 1 : -1;
return result;
}

original_code:
int Util_sscanbool(const char *s)
{
 if (*s == '0' && s[1] == '\0')
  return 0;
 if (*s == '1' && s[1] == '\0')
  return 1;
 return -1;
}


original_code:
double Util_round(double x)
{
 return floor(x + 0.5);
}

variable_types: x double

return_type: double


decompiled_code:
void *__fastcall Util_malloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func12(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

original_code:
void *Util_malloc(size_t size)
{
 void *ptr = malloc(size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

canonical_original_code:
void *Util_malloc(unsigned long size)
{
 void *ptr = malloc(size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

variable_types:
ptr
void *


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

result = realloc(a1, a2);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

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

result = realloc(a1, a2);
if ( !result )
{
Atari800_Exit(0LL);
puts("Fatal error: out of memory");
exit(1);
}
return result;
}

original_code:
void *Util_realloc(void *ptr, size_t size)
{
 ptr = realloc(ptr, size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}

canonical_original_code:
void *Util_realloc(void *ptr, unsigned long size)
{
 ptr = realloc(ptr, size);
 if (ptr == ((void *)0)) {
  Atari800_Exit(0);
  printf("Fatal error: out of memory\n");
  exit(1);
 }
 return ptr;
}


decompiled_code:
void *__fastcall Util_strdup(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = Util_malloc(v1);
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func14(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = func12(v1);
return memcpy(v2, src, v1);
}

original_code:
char *Util_strdup(const char *s)
{
 size_t size = strlen(s) + 1;
 char *ptr = (char *) Util_malloc(size);
 memcpy(ptr, s, size);
 return ptr;
}

canonical_original_code:
char *Util_strdup(char *s)
{
 unsigned long size = strlen(s) + 1;
 char *ptr = (char *) Util_malloc(size);
 memcpy(ptr, s, size);
 return ptr;
}

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall Util_splitpath(char *src, void *dest, char *a3)
{
__int64 result; // rax
char *v5; // rbx
char *v6; // rdx
char *v8; // rsi
int v9; // er14

result = strlen(src);
v5 = &src[result - 2];
if ( src > v5 )
goto LABEL_34;
result = ((_BYTE)result - 2) & 7;
if ( *v5 == 47 )
goto LABEL_39;
if ( src > --v5 )
goto LABEL_34;
if ( !result )
{
LABEL_25:
while ( *v5 != 47 )
{
v6 = v5 - 1;
if ( *--v5 == 47 )
break;
if ( *--v5 == 47 )
break;
v5 = v6 - 2;
if ( *(v6 - 2) == 47 )
break;
v5 = v6 - 3;
if ( *(v6 - 3) == 47 )
break;
v5 = v6 - 4;
if ( *(v6 - 4) == 47 )
break;
v5 = v6 - 5;
if ( *(v6 - 5) == 47 )
break;
v5 = v6 - 6;
if ( *(v6 - 6) == 47 )
break;
v5 = v6 - 7;
if ( src > v6 - 7 )
goto LABEL_34;
}
goto LABEL_39;
}
if ( result == 1 )
goto LABEL_23;
if ( result != 2 )
{
switch ( result )
{
case 3LL:
LABEL_19:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_21;
case 4LL:
LABEL_17:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_19;
case 5LL:
LABEL_15:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_17;
}
if ( result != 6 )
{
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 != 47 )
{
--v5;
goto LABEL_15;
}
LABEL_39:
if ( dest )
{
v9 = (_DWORD)v5 - (_DWORD)src;
if ( src == v5 || v5 == src + 2 && src[1] == 58 )
v9 = (_DWORD)v5 - (_DWORD)src + 1;
result = (__int64)memcpy(dest, src, v9);
*((_BYTE *)dest + v9) = 0;
}
v8 = v5 + 1;
if ( a3 )
goto LABEL_38;
return result;
}
LABEL_21:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
LABEL_23:
if ( *v5 == 47 )
goto LABEL_39;
if ( src <= --v5 )
goto LABEL_25;
LABEL_34:
if ( dest )
*(_BYTE *)dest = 0;
if ( a3 )
{
v8 = src;
LABEL_38:
result = (__int64)strcpy(a3, v8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(char *src, void *dest, char *a3)
{
__int64 result; // rax
char *v5; // rbx
char *v6; // rdx
char *v8; // rsi
int v9; // er14

result = strlen(src);
v5 = &src[result - 2];
if ( src > v5 )
goto LABEL_34;
result = ((_BYTE)result - 2) & 7;
if ( *v5 == 47 )
goto LABEL_39;
if ( src > --v5 )
goto LABEL_34;
if ( !result )
{
LABEL_25:
while ( *v5 != 47 )
{
v6 = v5 - 1;
if ( *--v5 == 47 )
break;
if ( *--v5 == 47 )
break;
v5 = v6 - 2;
if ( *(v6 - 2) == 47 )
break;
v5 = v6 - 3;
if ( *(v6 - 3) == 47 )
break;
v5 = v6 - 4;
if ( *(v6 - 4) == 47 )
break;
v5 = v6 - 5;
if ( *(v6 - 5) == 47 )
break;
v5 = v6 - 6;
if ( *(v6 - 6) == 47 )
break;
v5 = v6 - 7;
if ( src > v6 - 7 )
goto LABEL_34;
}
goto LABEL_39;
}
if ( result == 1 )
goto LABEL_23;
if ( result != 2 )
{
switch ( result )
{
case 3LL:
LABEL_19:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_21;
case 4LL:
LABEL_17:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_19;
case 5LL:
LABEL_15:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
goto LABEL_17;
}
if ( result != 6 )
{
if ( *v5 == 47 )
goto LABEL_39;
--v5;
}
if ( *v5 != 47 )
{
--v5;
goto LABEL_15;
}
LABEL_39:
if ( dest )
{
v9 = (_DWORD)v5 - (_DWORD)src;
if ( src == v5 || v5 == src + 2 && src[1] == 58 )
v9 = (_DWORD)v5 - (_DWORD)src + 1;
result = (__int64)memcpy(dest, src, v9);
*((_BYTE *)dest + v9) = 0;
}
v8 = v5 + 1;
if ( a3 )
goto LABEL_38;
return result;
}
LABEL_21:
if ( *v5 == 47 )
goto LABEL_39;
--v5;
LABEL_23:
if ( *v5 == 47 )
goto LABEL_39;
if ( src <= --v5 )
goto LABEL_25;
LABEL_34:
if ( dest )
*(_BYTE *)dest = 0;
if ( a3 )
{
v8 = src;
LABEL_38:
result = (__int64)strcpy(a3, v8);
}
return result;
}

original_code:
void Util_splitpath(const char *path, char *dir_part, char *file_part)
{
 const char *p;
 for (p = path + strlen(path) - 2; p >= path; p--) {
  if (*p == '/'
     ) {
   if (dir_part != ((void *)0)) {
    int len = p - path;
    if (p == path || (p == path + 2 && path[1] == ':'))
     len++;
    memcpy(dir_part, path, len);
    dir_part[len] = '\0';
   }
   if (file_part != ((void *)0))
    strcpy(file_part, p + 1);
   return;
  }
 }
 if (dir_part != ((void *)0))
  dir_part[0] = '\0';
 if (file_part != ((void *)0))
  strcpy(file_part, path);
}

canonical_original_code:
void Util_splitpath(char *path, char *dir_part, char *file_part)
{
 char *p;
 for (p = path + strlen(path) - 2; p >= path; p--) {
  if (*p == '/'
     ) {
   if (dir_part != ((void *)0)) {
    int len = p - path;
    if (p == path || (p == path + 2 && path[1] == ':'))
     len++;
    memcpy(dir_part, path, len);
    dir_part[len] = '\0';
   }
   if (file_part != ((void *)0))
    strcpy(file_part, p + 1);
   return;
  }
 }
 if (dir_part != ((void *)0))
  dir_part[0] = '\0';
 if (file_part != ((void *)0))
  strcpy(file_part, path);
}

variable_types:
path
char *


decompiled_code:
int __fastcall Util_catpath(char *s, char *a2, _BYTE *a3)
{
_BYTE *v3; // r8
const char *v4; // r13
size_t v5; // rax

v3 = a3;
v4 = "%s%s";
if ( *a2 )
{
if ( *a3 != 47 )
{
v5 = strlen(a2);
v3 = a3;
if ( a2[v5 - 1] != 47 )
v4 = "%s/%s";
}
}
return snprintf(s, 0x1000uLL, v4, a2, v3);
}

original_code:
void Util_catpath(char *result, const char *path1, const char *path2)
{
 snprintf(result, 4096,
  path1[0] == '\0' || path2[0] == '/' || path1[strlen(path1) - 1] == '/'
   ? "%s%s" : "%s" "/" "%s", path1, path2);
}

canonical_original_code:
void Util_catpath(char *result, char *path1, char *path2)
{
 snprintf(result, 4096,
  path1[0] == '\0' || path2[0] == '/' || path1[strlen(path1) - 1] == '/'
   ? "%s%s" : "%s" "/" "%s", path1, path2);
}

variable_types:
result
char *


decompiled_code:
__int64 __fastcall Util_direxists(const char *a1)
{
int v1; // er8
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(const char *a1)
{
int v1; // er8
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3);
result = 0LL;
if ( !v1 )
result = (v3.st_mode >> 14) & 1;
return result;
}

original_code:
int Util_direxists(const char *filename)
{
 struct stat filestatus;
 return stat(filename, &filestatus) == 0 && (filestatus.st_mode & 0040000);
}

canonical_original_code:
int Util_direxists(char *filename)
{
 struct stat filestatus;
 return stat(filename, &filestatus) == 0 && (filestatus.st_mode & 0040000);
}

variable_types:
filename
char *

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

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


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

result = 0LL;
memset(&esc_function, 0, 0x800uLL);
return result;
}

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

result = 0LL;
memset(&esc_function, 0, 0x800uLL);
return result;
}

original_code:
void ESC_ClearAll(void)
{
 int i;
 for (i = 0; i < 256; i++)
  esc_function[i] = ((void *)0);
}

canonical_original_code:
void ESC_ClearAll()
{
 int i;
 for (i = 0; i < 256; i++)
  esc_function[i] = ((void *)0);
}

variable_types: i int


decompiled_code:
__int64 __fastcall ESC_Add(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -14;
MEMORY_mem[a1 + 1] = a2;
return result;
}

original_code:
void ESC_Add(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
}

canonical_original_code:
void ESC_Add(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
}

variable_types:
address
unsigned short

variable_types:
function
void (*)()


decompiled_code:
__int64 __fastcall ESC_AddEscRts(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

esc_function[a2] = a3;
esc_address[a2] = a1;
MEMORY_mem[a1] = -14;
result = a1 + 2;
MEMORY_mem[a1 + 1] = a2;
MEMORY_mem[result] = 96;
return result;
}

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

esc_function[a2] = a3;
esc_address[a2] = a1;
MEMORY_mem[a1] = -14;
result = a1 + 2;
MEMORY_mem[a1 + 1] = a2;
MEMORY_mem[result] = 96;
return result;
}

original_code:
void ESC_AddEscRts(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
 (MEMORY_mem[address + 2] = 0x60);
}

canonical_original_code:
void ESC_AddEscRts(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xf2);
 (MEMORY_mem[address + 1] = esc_code);
 (MEMORY_mem[address + 2] = 0x60);
}


decompiled_code:
__int64 __fastcall ESC_AddEscRts2(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -46;
MEMORY_mem[a1 + 1] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, unsigned __int8 a2, __int64 a3)
{
__int64 result; // rax

result = a2;
esc_address[a2] = a1;
esc_function[a2] = a3;
MEMORY_mem[a1] = -46;
MEMORY_mem[a1 + 1] = a2;
return result;
}

original_code:
void ESC_AddEscRts2(unsigned short address, unsigned char esc_code, ESC_FunctionType function)
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xd2);
 (MEMORY_mem[address + 1] = esc_code);
}

canonical_original_code:
void ESC_AddEscRts2(unsigned short address, unsigned char esc_code, void (*function)())
{
 esc_address[esc_code] = address;
 esc_function[esc_code] = function;
 (MEMORY_mem[address] = 0xd2);
 (MEMORY_mem[address + 1] = esc_code);
}


decompiled_code:
__int64 __fastcall ESC_Run(unsigned __int8 a1)
{
__int64 (__fastcall *v1)(_QWORD, _QWORD); // r10
__int64 result; // rax

if ( (unsigned __int16)esc_address[a1] == CPU_regPC - 2 )
{
v1 = (__int64 (__fastcall *)(_QWORD, _QWORD))esc_function[a1];
if ( v1 )
return v1(a1, a1);
}
CPU_cim_encountered = 1;
Log_print("Invalid ESC code %02x at address %04x", a1);
result = Atari800_Exit(1LL);
if ( !(_DWORD)result )
exit(0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int8 a1)
{
__int64 (__fastcall *v1)(_QWORD, _QWORD); // r10
__int64 result; // rax

if ( (unsigned __int16)esc_address[a1] == CPU_regPC - 2 )
{
v1 = (__int64 (__fastcall *)(_QWORD, _QWORD))esc_function[a1];
if ( v1 )
return v1(a1, a1);
}
CPU_cim_encountered = 1;
Log_print("Invalid ESC code %02x at address %04x", a1);
result = Atari800_Exit(1LL);
if ( !(_DWORD)result )
exit(0);
return result;
}

original_code:
void ESC_Run(unsigned char esc_code)
{
 if (esc_address[esc_code] == CPU_regPC - 2 && esc_function[esc_code] != ((void *)0)) {
  esc_function[esc_code]();
  return;
 }
 CPU_cim_encountered = 1;
 Log_print("Invalid ESC code %02x at address %04x", esc_code, CPU_regPC - 2);
 if (!Atari800_Exit(1))
  exit(0);
}


decompiled_code:
_DWORD *ESC_PatchOS()
{
int v0; // ebx
_DWORD *result; // rax
int v2; // edi
char v3; // r9
__int16 v4; // bp
char v5; // r8
unsigned __int16 v6; // r10
unsigned __int16 v7; // bp

v0 = Devices_PatchOS();
result = &ESC_enable_sio_patch;
if ( ESC_enable_sio_patch )
{
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
v2 = 61300;
v3 = 0x80;
v4 = -4236;
v5 = -96;
v6 = -4164;
}
else
{
if ( Atari800_machine_type != 2 )
return result;
v2 = 64787;
v3 = 3;
v4 = -749;
v5 = -87;
v6 = -672;
}
if ( MEMORY_mem[v2] == 0xA9
&& MEMORY_mem[(unsigned __int16)(v4 + 1)] == 3
&& MEMORY_mem[(unsigned __int16)(v4 + 2)] == 0x8D
&& MEMORY_mem[(unsigned __int16)(v4 + 3)] == 42
&& MEMORY_mem[(unsigned __int16)(v4 + 4)] == 2 )
{
v7 = v6;
if ( MEMORY_mem[v6] == v5
&& MEMORY_mem[(unsigned __int16)(v6 + 1)] == v3
&& MEMORY_mem[(unsigned __int16)(v6 + 2)] == 32
&& MEMORY_mem[(unsigned __int16)(v6 + 3)] == 92
&& MEMORY_mem[(unsigned __int16)(v6 + 4)] == 0xE4 )
{
ESC_Add(v2, 0xA8u, (__int64)&CASSETTE_LeaderLoad);
ESC_Add(v7, 0xA9u, (__int64)&CASSETTE_LeaderSave);
}
}
result = (_DWORD *)ESC_AddEscRts(0xE459u, 0, (__int64)&SIO_Handler);
}
else
{
ESC_Remove(0xA8u);
ESC_Remove(0xA9u);
result = ESC_Remove(0);
if ( !v0 )
return result;
}
if ( Atari800_machine_type == 2 )
{
*(_WORD *)&MEMORY_mem[49940] = -114;
*(_WORD *)&MEMORY_mem[49945] = -114;
}
return result;
}

canonical_decompiled_code:
_DWORD *func5()
{
int v0; // ebx
_DWORD *result; // rax
int v2; // edi
char v3; // r9
__int16 v4; // bp
char v5; // r8
unsigned __int16 v6; // r10
unsigned __int16 v7; // bp

v0 = Devices_PatchOS();
result = &ESC_enable_sio_patch;
if ( ESC_enable_sio_patch )
{
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
v2 = 61300;
v3 = 0x80;
v4 = -4236;
v5 = -96;
v6 = -4164;
}
else
{
if ( Atari800_machine_type != 2 )
return result;
v2 = 64787;
v3 = 3;
v4 = -749;
v5 = -87;
v6 = -672;
}
if ( MEMORY_mem[v2] == 0xA9
&& MEMORY_mem[(unsigned __int16)(v4 + 1)] == 3
&& MEMORY_mem[(unsigned __int16)(v4 + 2)] == 0x8D
&& MEMORY_mem[(unsigned __int16)(v4 + 3)] == 42
&& MEMORY_mem[(unsigned __int16)(v4 + 4)] == 2 )
{
v7 = v6;
if ( MEMORY_mem[v6] == v5
&& MEMORY_mem[(unsigned __int16)(v6 + 1)] == v3
&& MEMORY_mem[(unsigned __int16)(v6 + 2)] == 32
&& MEMORY_mem[(unsigned __int16)(v6 + 3)] == 92
&& MEMORY_mem[(unsigned __int16)(v6 + 4)] == 0xE4 )
{
func1(v2, 0xA8u, (__int64)&CASSETTE_LeaderLoad);
func1(v7, 0xA9u, (__int64)&CASSETTE_LeaderSave);
}
}
result = (_DWORD *)func2(0xE459u, 0, (__int64)&SIO_Handler);
}
else
{
ESC_Remove(0xA8u);
ESC_Remove(0xA9u);
result = ESC_Remove(0);
if ( !v0 )
return result;
}
if ( Atari800_machine_type == 2 )
{
*(_WORD *)&MEMORY_mem[49940] = -114;
*(_WORD *)&MEMORY_mem[49945] = -114;
}
return result;
}

original_code:
void ESC_PatchOS(void)
{
 int patched = Devices_PatchOS();
 if (ESC_enable_sio_patch) {
  unsigned short addr_l;
  unsigned short addr_s;
  unsigned char check_s_0;
  unsigned char check_s_1;
  switch (Atari800_machine_type) {
  case 0:
  case 1:
   addr_l = 0xef74;
   addr_s = 0xefbc;
   check_s_0 = 0xa0;
   check_s_1 = 0x80;
   break;
  case 2:
   addr_l = 0xfd13;
   addr_s = 0xfd60;
   check_s_0 = 0xa9;
   check_s_1 = 0x03;
   break;
  default:
   return;
  }
  if ((MEMORY_mem[addr_l]) == 0xa9 && (MEMORY_mem[addr_l + 1]) == 0x03
   && (MEMORY_mem[addr_l + 2]) == 0x8d && (MEMORY_mem[addr_l + 3]) == 0x2a
   && (MEMORY_mem[addr_l + 4]) == 0x02
   && (MEMORY_mem[addr_s]) == check_s_0
   && (MEMORY_mem[addr_s + 1]) == check_s_1
   && (MEMORY_mem[addr_s + 2]) == 0x20 && (MEMORY_mem[addr_s + 3]) == 0x5c
   && (MEMORY_mem[addr_s + 4]) == 0xe4) {
   ESC_Add(addr_l, ESC_COPENLOAD, CASSETTE_LeaderLoad);
   ESC_Add(addr_s, ESC_COPENSAVE, CASSETTE_LeaderSave);
  }
  ESC_AddEscRts(0xe459, ESC_SIOV, SIO_Handler);
  patched = 1;
 }
 else {
  ESC_Remove(ESC_COPENLOAD);
  ESC_Remove(ESC_COPENSAVE);
  ESC_Remove(ESC_SIOV);
 };
 if (patched && Atari800_machine_type == 2) {
  (MEMORY_mem[0xc314] = 0x8e);
  (MEMORY_mem[0xc315] = 0xff);
  (MEMORY_mem[0xc319] = 0x8e);
  (MEMORY_mem[0xc31a] = 0xff);
 }
}

canonical_original_code:
void ESC_PatchOS()
{
 int patched = Devices_PatchOS();
 if (ESC_enable_sio_patch) {
  unsigned short addr_l;
  unsigned short addr_s;
  unsigned char check_s_0;
  unsigned char check_s_1;
  switch (Atari800_machine_type) {
  case 0:
  case 1:
   addr_l = 0xef74;
   addr_s = 0xefbc;
   check_s_0 = 0xa0;
   check_s_1 = 0x80;
   break;
  case 2:
   addr_l = 0xfd13;
   addr_s = 0xfd60;
   check_s_0 = 0xa9;
   check_s_1 = 0x03;
   break;
  default:
   return;
  }
  if ((MEMORY_mem[addr_l]) == 0xa9 && (MEMORY_mem[addr_l + 1]) == 0x03
   && (MEMORY_mem[addr_l + 2]) == 0x8d && (MEMORY_mem[addr_l + 3]) == 0x2a
   && (MEMORY_mem[addr_l + 4]) == 0x02
   && (MEMORY_mem[addr_s]) == check_s_0
   && (MEMORY_mem[addr_s + 1]) == check_s_1
   && (MEMORY_mem[addr_s + 2]) == 0x20 && (MEMORY_mem[addr_s + 3]) == 0x5c
   && (MEMORY_mem[addr_s + 4]) == 0xe4) {
   ESC_Add(addr_l, ESC_COPENLOAD, CASSETTE_LeaderLoad);
   ESC_Add(addr_s, ESC_COPENSAVE, CASSETTE_LeaderSave);
  }
  ESC_AddEscRts(0xe459, ESC_SIOV, SIO_Handler);
  patched = 1;
 }
 else {
  ESC_Remove(ESC_COPENLOAD);
  ESC_Remove(ESC_COPENSAVE);
  ESC_Remove(ESC_SIOV);
 };
 if (patched && Atari800_machine_type == 2) {
  (MEMORY_mem[0xc314] = 0x8e);
  (MEMORY_mem[0xc315] = 0xff);
  (MEMORY_mem[0xc319] = 0x8e);
  (MEMORY_mem[0xc31a] = 0xff);
 }
}

variable_types: patched int


decompiled_code:
int *ESC_UpdatePatches()
{
int *result; // rax

result = &Atari800_machine_type;
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
memcpy(&MEMORY_mem[6912], MEMORY_os, 0x2800uLL);
ESC_PatchOS();
return (int *)Devices_UpdatePatches();
}
if ( Atari800_machine_type == 2 && (PIA_PORTB & 1) != 0 )
{
MEMORY_mem[6144] = MEMORY_os[0];
MEMORY_mem[6655] = MEMORY_os[511];
qmemcpy(
(void *)((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)MEMORY_os
- ((char *)&MEMORY_mem[6144]
- ((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)&MEMORY_mem[6144] - ((unsigned int)&MEMORY_mem[6145] & 0xFFFFFFF8) + 4096) >> 3));
memcpy(&MEMORY_mem[6912], &MEMORY_os[768], 0x2800uLL);
ESC_PatchOS();
return (int *)Devices_UpdatePatches();
}
return result;
}

canonical_decompiled_code:
int *func6()
{
int *result; // rax

result = &Atari800_machine_type;
if ( Atari800_machine_type <= 1 )
{
if ( Atari800_machine_type < 0 )
return result;
memcpy(&MEMORY_mem[6912], MEMORY_os, 0x2800uLL);
func5();
return (int *)Devices_UpdatePatches();
}
if ( Atari800_machine_type == 2 && (PIA_PORTB & 1) != 0 )
{
MEMORY_mem[6144] = MEMORY_os[0];
MEMORY_mem[6655] = MEMORY_os[511];
qmemcpy(
(void *)((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)MEMORY_os
- ((char *)&MEMORY_mem[6144]
- ((unsigned __int64)&MEMORY_mem[6145] & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)&MEMORY_mem[6144] - ((unsigned int)&MEMORY_mem[6145] & 0xFFFFFFF8) + 4096) >> 3));
memcpy(&MEMORY_mem[6912], &MEMORY_os[768], 0x2800uLL);
func5();
return (int *)Devices_UpdatePatches();
}
return result;
}

original_code:
void ESC_UpdatePatches(void)
{
 switch (Atari800_machine_type) {
 case 0:
 case 1:
  memcpy(MEMORY_mem + (0xd800), MEMORY_os, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 case 2:
  if ((PIA_PORTB & 1) == 0)
   break;
  memcpy(MEMORY_mem + (0xc000), MEMORY_os, 0x1000);
  memcpy(MEMORY_mem + (0xd800), MEMORY_os + 0x1800, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 default:
  break;
 }
}

canonical_original_code:
void ESC_UpdatePatches()
{
 switch (Atari800_machine_type) {
 case 0:
 case 1:
  memcpy(MEMORY_mem + (0xd800), MEMORY_os, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 case 2:
  if ((PIA_PORTB & 1) == 0)
   break;
  memcpy(MEMORY_mem + (0xc000), MEMORY_os, 0x1000);
  memcpy(MEMORY_mem + (0xd800), MEMORY_os + 0x1800, 0x2800);
  ESC_PatchOS();
  Devices_UpdatePatches();
  break;
 default:
  break;
 }
}


decompiled_code:
char *__fastcall CFG_FindROMImages(__int64 a1, int a2)
{
const char *v2; // rdx
char **v3; // r13
const char **i; // rbx
char *result; // rax
const char **v6; // rcx
const char **v7; // [rsp+8h] [rbp-1040h]
char src[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = "atariosa.rom";
v3 = (char **)&rom_filenames_1;
for ( i = (const char **)common_filenames_2; ; v2 = *i )
{
if ( a2 && (result = *v3, **v3 != 10) )
{
LABEL_6:
while ( 1 )
{
++i;
if ( !v2 )
break;
v2 = *i;
}
}
else
{
do
{
Util_catpath(src, a1, v2);
result = (char *)Util_fileexists(src);
v6 = i++;
if ( (_DWORD)result )
{
v7 = v6;
result = strcpy(*v3, src);
i = v7;
v2 = *v7;
goto LABEL_6;
}
v2 = *i;
}
while ( *i );
i = v6 + 2;
}
if ( ++v3 == (char **)(&rom_filenames_1 + 5) )
break;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, int a2)
{
const char *v2; // rdx
char **v3; // r13
const char **i; // rbx
char *result; // rax
const char **v6; // rcx
const char **v7; // [rsp+8h] [rbp-1040h]
char src[4152]; // [rsp+10h] [rbp-1038h] BYREF

v2 = "atariosa.rom";
v3 = (char **)&rom_filenames_1;
for ( i = (const char **)common_filenames_2; ; v2 = *i )
{
if ( a2 && (result = *v3, **v3 != 10) )
{
LABEL_6:
while ( 1 )
{
++i;
if ( !v2 )
break;
v2 = *i;
}
}
else
{
do
{
Util_catpath(src, a1, v2);
result = (char *)Util_fileexists(src);
v6 = i++;
if ( (_DWORD)result )
{
v7 = v6;
result = strcpy(*v3, src);
i = v7;
v2 = *v7;
goto LABEL_6;
}
v2 = *i;
}
while ( *i );
i = v6 + 2;
}
if ( ++v3 == (char **)(&rom_filenames_1 + 5) )
break;
}
return result;
}

original_code:
void CFG_FindROMImages(const char *directory, int only_if_not_set)
{
 static char * const rom_filenames[5] = {
  CFG_osa_filename,
  CFG_osb_filename,
  CFG_xlxe_filename,
  CFG_5200_filename,
  CFG_basic_filename
 };
 static const char * const common_filenames[] = {
  "atariosa.rom", "atari_osa.rom", "atari_os_a.rom",
  "ATARIOSA.ROM", "ATARI_OSA.ROM", "ATARI_OS_A.ROM",
  ((void *)0),
  "atariosb.rom", "atari_osb.rom", "atari_os_b.rom",
  "ATARIOSB.ROM", "ATARI_OSB.ROM", "ATARI_OS_B.ROM",
  ((void *)0),
  "atarixlxe.rom", "atarixl.rom", "atari_xlxe.rom", "atari_xl_xe.rom",
  "ATARIXLXE.ROM", "ATARIXL.ROM", "ATARI_XLXE.ROM", "ATARI_XL_XE.ROM",
  ((void *)0),
  "atari5200.rom", "atar5200.rom", "5200.rom", "5200.bin", "atari_5200.rom",
  "ATARI5200.ROM", "ATAR5200.ROM", "5200.ROM", "5200.BIN", "ATARI_5200.ROM",
  ((void *)0),
  "ataribasic.rom", "ataribas.rom", "basic.rom", "atari_basic.rom",
  "ATARIBASIC.ROM", "ATARIBAS.ROM", "BASIC.ROM", "ATARI_BASIC.ROM",
  ((void *)0)
 };
 const char * const *common_filename = common_filenames;
 int i;
 for (i = 0; i < 5; i++) {
  if (!only_if_not_set || ((rom_filenames[i])[0] == '\n')) {
   do {
    char full_filename[4096];
    Util_catpath(full_filename, directory, *common_filename);
    if (Util_fileexists(full_filename)) {
     strcpy(rom_filenames[i], full_filename);
     break;
    }
   } while (*++common_filename != ((void *)0));
  }
  while (*common_filename++ != ((void *)0));
 }
}

canonical_original_code:
void CFG_FindROMImages(char *directory, int only_if_not_set)
{
 static char *rom_filenames[5] = {
  CFG_osa_filename,
  CFG_osb_filename,
  CFG_xlxe_filename,
  CFG_5200_filename,
  CFG_basic_filename
 };
 static char *common_filenames[] = {
  "atariosa.rom", "atari_osa.rom", "atari_os_a.rom",
  "ATARIOSA.ROM", "ATARI_OSA.ROM", "ATARI_OS_A.ROM",
  ((void *)0),
  "atariosb.rom", "atari_osb.rom", "atari_os_b.rom",
  "ATARIOSB.ROM", "ATARI_OSB.ROM", "ATARI_OS_B.ROM",
  ((void *)0),
  "atarixlxe.rom", "atarixl.rom", "atari_xlxe.rom", "atari_xl_xe.rom",
  "ATARIXLXE.ROM", "ATARIXL.ROM", "ATARI_XLXE.ROM", "ATARI_XL_XE.ROM",
  ((void *)0),
  "atari5200.rom", "atar5200.rom", "5200.rom", "5200.bin", "atari_5200.rom",
  "ATARI5200.ROM", "ATAR5200.ROM", "5200.ROM", "5200.BIN", "ATARI_5200.ROM",
  ((void *)0),
  "ataribasic.rom", "ataribas.rom", "basic.rom", "atari_basic.rom",
  "ATARIBASIC.ROM", "ATARIBAS.ROM", "BASIC.ROM", "ATARI_BASIC.ROM",
  ((void *)0)
 };
 char **common_filename = common_filenames;
 int i;
 for (i = 0; i < 5; i++) {
  if (!only_if_not_set || ((rom_filenames[i])[0] == '\n')) {
   do {
    char full_filename[4096];
    Util_catpath(full_filename, directory, *common_filename);
    if (Util_fileexists(full_filename)) {
     strcpy(rom_filenames[i], full_filename);
     break;
    }
   } while (*++common_filename != ((void *)0));
  }
  while (*common_filename++ != ((void *)0));
 }
}

variable_types:
directory
char *


decompiled_code:
__int64 __fastcall CFG_MatchTextParameter(char *s1, const char **a2, int a3)
{
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edx
unsigned int v6; // er14

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = a3;
v4 = 0LL;
v5 = a3 & 7;
if ( !v5 )
{
LABEL_21:
while ( 1 )
{
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
break;
v6 = v4 + 1;
if ( !strcasecmp(s1, a2[v4 + 1]) )
break;
v6 = v4 + 2;
if ( !strcasecmp(s1, a2[v4 + 2]) )
break;
v6 = v4 + 3;
if ( !strcasecmp(s1, a2[v4 + 3]) )
break;
v6 = v4 + 4;
if ( !strcasecmp(s1, a2[v4 + 4]) )
break;
v6 = v4 + 5;
if ( !strcasecmp(s1, a2[v4 + 5]) )
break;
v6 = v4 + 6;
if ( !strcasecmp(s1, a2[v4 + 6]) )
break;
v6 = v4 + 7;
if ( !strcasecmp(s1, a2[v4 + 7]) )
break;
v4 += 8LL;
if ( v4 == v3 )
return (unsigned int)-1;
}
return v6;
}
if ( v5 != 1LL )
{
if ( v5 != 2LL )
{
if ( v5 != 3LL )
{
if ( v5 != 4LL )
{
if ( v5 != 5LL )
{
if ( v5 != 6LL )
{
v6 = 0;
if ( !strcasecmp(s1, *a2) )
return v6;
v4 = 1LL;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( strcasecmp(s1, a2[v4]) )
{
if ( ++v4 != v3 )
goto LABEL_21;
return (unsigned int)-1;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1, const char **a2, int a3)
{
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edx
unsigned int v6; // er14

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = a3;
v4 = 0LL;
v5 = a3 & 7;
if ( !v5 )
{
LABEL_21:
while ( 1 )
{
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
break;
v6 = v4 + 1;
if ( !strcasecmp(s1, a2[v4 + 1]) )
break;
v6 = v4 + 2;
if ( !strcasecmp(s1, a2[v4 + 2]) )
break;
v6 = v4 + 3;
if ( !strcasecmp(s1, a2[v4 + 3]) )
break;
v6 = v4 + 4;
if ( !strcasecmp(s1, a2[v4 + 4]) )
break;
v6 = v4 + 5;
if ( !strcasecmp(s1, a2[v4 + 5]) )
break;
v6 = v4 + 6;
if ( !strcasecmp(s1, a2[v4 + 6]) )
break;
v6 = v4 + 7;
if ( !strcasecmp(s1, a2[v4 + 7]) )
break;
v4 += 8LL;
if ( v4 == v3 )
return (unsigned int)-1;
}
return v6;
}
if ( v5 != 1LL )
{
if ( v5 != 2LL )
{
if ( v5 != 3LL )
{
if ( v5 != 4LL )
{
if ( v5 != 5LL )
{
if ( v5 != 6LL )
{
v6 = 0;
if ( !strcasecmp(s1, *a2) )
return v6;
v4 = 1LL;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( !strcasecmp(s1, a2[v4]) )
return v6;
++v4;
}
v6 = v4;
if ( strcasecmp(s1, a2[v4]) )
{
if ( ++v4 != v3 )
goto LABEL_21;
return (unsigned int)-1;
}
return v6;
}

original_code:
int CFG_MatchTextParameter(char const *param, char const * const cfg_strings[], int cfg_strings_size)
{
 int i;
 for (i = 0; i < cfg_strings_size; i ++) {
  if (strcasecmp(param, cfg_strings[i]) == 0)
   return i;
 }
 return -1;
}

canonical_original_code:
int CFG_MatchTextParameter(char *param, char *cfg_strings[], int cfg_strings_size)
{
 int i;
 for (i = 0; i < cfg_strings_size; i ++) {
  if (strcasecmp(param, cfg_strings[i]) == 0)
   return i;
 }
 return -1;
}

variable_types:
param
char *


decompiled_code:
void init_mio()
{
free(mio_rom);
mio_rom = (void *)Util_malloc(0x4000LL);
if ( (unsigned int)Atari800_LoadImage(&mio_rom_filename, mio_rom, 0x4000LL) )
{
PBI_MIO_enabled = 1;
if ( PBI_SCSI_disk )
fclose(PBI_SCSI_disk);
if ( mio_scsi_disk_filename != 10 )
{
PBI_SCSI_disk = fopen(&mio_scsi_disk_filename, "rb+");
if ( PBI_SCSI_disk )
{
mio_scsi_enabled = 1;
LABEL_7:
free(mio_ram);
mio_ram = (void *)Util_malloc(mio_ram_size);
memset(mio_ram, 0, mio_ram_size);
return;
}
Log_print("Error opening SCSI disk image:%s", &mio_scsi_disk_filename);
}
if ( !mio_scsi_enabled )
PBI_SCSI_BSY = 1;
goto LABEL_7;
}
free(mio_rom);
}

canonical_decompiled_code:
void func0()
{
free(mio_rom);
mio_rom = (void *)Util_malloc(0x4000LL);
if ( (unsigned int)Atari800_LoadImage(&mio_rom_filename, mio_rom, 0x4000LL) )
{
PBI_MIO_enabled = 1;
if ( PBI_SCSI_disk )
fclose(PBI_SCSI_disk);
if ( mio_scsi_disk_filename != 10 )
{
PBI_SCSI_disk = fopen(&mio_scsi_disk_filename, "rb+");
if ( PBI_SCSI_disk )
{
mio_scsi_enabled = 1;
LABEL_7:
free(mio_ram);
mio_ram = (void *)Util_malloc(mio_ram_size);
memset(mio_ram, 0, mio_ram_size);
return;
}
Log_print("Error opening SCSI disk image:%s", &mio_scsi_disk_filename);
}
if ( !mio_scsi_enabled )
PBI_SCSI_BSY = 1;
goto LABEL_7;
}
free(mio_rom);
}

original_code:
static void init_mio(void)
{
 free(mio_rom);
 mio_rom = (unsigned char *)Util_malloc(mio_rom_size);
 if (!Atari800_LoadImage(mio_rom_filename, mio_rom, mio_rom_size)) {
  free(mio_rom);
  return;
 }
 do{}while(0);
 PBI_MIO_enabled = 1;
 if (PBI_SCSI_disk != ((void *)0)) fclose(PBI_SCSI_disk);
 if (!((mio_scsi_disk_filename)[0] == '\n')) {
  PBI_SCSI_disk = fopen(mio_scsi_disk_filename, "rb+");
  if (PBI_SCSI_disk == ((void *)0)) {
   Log_print("Error opening SCSI disk image:%s", mio_scsi_disk_filename);
  }
  else {
   do{}while(0);
   mio_scsi_enabled = 1;
  }
 }
 if (!mio_scsi_enabled) {
  PBI_SCSI_BSY = 1;
 }
 free(mio_ram);
 mio_ram = (unsigned char *)Util_malloc(mio_ram_size);
 memset(mio_ram, 0, mio_ram_size);
}

canonical_original_code:
static void init_mio()
{
 free(mio_rom);
 mio_rom = (unsigned char *)Util_malloc(mio_rom_size);
 if (!Atari800_LoadImage(mio_rom_filename, mio_rom, mio_rom_size)) {
  free(mio_rom);
  return;
 }
 do{}while(0);
 PBI_MIO_enabled = 1;
 if (PBI_SCSI_disk != ((void *)0)) fclose(PBI_SCSI_disk);
 if (!((mio_scsi_disk_filename)[0] == '\n')) {
  PBI_SCSI_disk = fopen(mio_scsi_disk_filename, "rb+");
  if (PBI_SCSI_disk == ((void *)0)) {
   Log_print("Error opening SCSI disk image:%s", mio_scsi_disk_filename);
  }
  else {
   do{}while(0);
   mio_scsi_enabled = 1;
  }
 }
 if (!mio_scsi_enabled) {
  PBI_SCSI_BSY = 1;
 }
 free(mio_ram);
 mio_ram = (unsigned char *)Util_malloc(mio_ram_size);
 memset(mio_ram, 0, mio_ram_size);
}


original_code:
int PBI_MIO_Initialise(int *argc, char *argv[])
{
 int i, j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-mio") == 0) {
   init_mio();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-mio             Emulate the ICD MIO board");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

canonical_original_code:
int PBI_MIO_Initialise(int *argc, char *argv[])
{
 int i; int j;
 for (i = j = 1; i < *argc; i++) {
  if (strcmp(argv[i], "-mio") == 0) {
   init_mio();
  }
  else {
    if (strcmp(argv[i], "-help") == 0) {
    Log_print("\t-mio             Emulate the ICD MIO board");
   }
   argv[j++] = argv[i];
  }
 }
 *argc = j;
 return 1;
}

variable_types:
argc
int *

variable_types:
argv
char *[]

variable_types: j int


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

if ( !strcmp(a1, "MIO_ROM") )
{
Util_strlcpy(&mio_rom_filename, a2, 4096LL);
result = 1LL;
}
else if ( !strcmp(a1, "MIO_SCSI_DISK") )
{
Util_strlcpy(&mio_scsi_disk_filename, a2, 4096LL);
result = 1LL;
}
else
{
result = 0LL;
}
return result;
}

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

if ( !strcmp(a1, "MIO_ROM") )
{
Util_strlcpy(&mio_rom_filename, a2, 4096LL);
result = 1LL;
}
else if ( !strcmp(a1, "MIO_SCSI_DISK") )
{
Util_strlcpy(&mio_scsi_disk_filename, a2, 4096LL);
result = 1LL;
}
else
{
result = 0LL;
}
return result;
}

original_code:
int PBI_MIO_ReadConfig(char *string, char *ptr)
{
 if (strcmp(string, "MIO_ROM") == 0)
  Util_strlcpy(mio_rom_filename, ptr, sizeof(mio_rom_filename));
 else if (strcmp(string, "MIO_SCSI_DISK") == 0)
  Util_strlcpy(mio_scsi_disk_filename, ptr, sizeof(mio_scsi_disk_filename));
 else return 0;
 return 1;
}

variable_types:
string
char *


decompiled_code:
__int64 __fastcall PBI_MIO_D1GetByte(__int16 a1)
{
__int16 v1; // di
__int64 result; // rax
_DWORD *v3; // rax
unsigned int v4; // edx
unsigned __int8 v5; // [rsp+1h] [rbp-9h]

v1 = a1 & 0xFFE3;
if ( v1 == -11806 )
{
v3 = &PBI_SCSI_CD;
LOBYTE(v3) = PBI_SCSI_CD == 0;
if ( !PBI_SCSI_MSG )
LODWORD(v3) = (unsigned int)v3 | 2;
v4 = (unsigned int)v3 | 4;
if ( PBI_SCSI_IO )
v4 = (unsigned int)v3;
if ( !PBI_SCSI_BSY )
v4 |= 0x20u;
result = (unsigned __int8)v4 | 0xFFFFFF80;
if ( PBI_SCSI_REQ )
result = v4;
}
else
{
result = 0LL;
if ( v1 == -11807 )
{
if ( mio_scsi_enabled )
{
v5 = ~(unsigned __int8)PBI_SCSI_GetByte();
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
result = v5;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1)
{
__int16 v1; // di
__int64 result; // rax
_DWORD *v3; // rax
unsigned int v4; // edx
unsigned __int8 v5; // [rsp+1h] [rbp-9h]

v1 = a1 & 0xFFE3;
if ( v1 == -11806 )
{
v3 = &PBI_SCSI_CD;
LOBYTE(v3) = PBI_SCSI_CD == 0;
if ( !PBI_SCSI_MSG )
LODWORD(v3) = (unsigned int)v3 | 2;
v4 = (unsigned int)v3 | 4;
if ( PBI_SCSI_IO )
v4 = (unsigned int)v3;
if ( !PBI_SCSI_BSY )
v4 |= 0x20u;
result = (unsigned __int8)v4 | 0xFFFFFF80;
if ( PBI_SCSI_REQ )
result = v4;
}
else
{
result = 0LL;
if ( v1 == -11807 )
{
if ( mio_scsi_enabled )
{
v5 = ~(unsigned __int8)PBI_SCSI_GetByte();
PBI_SCSI_PutACK(1LL);
PBI_SCSI_PutACK(0LL);
result = v5;
}
}
}
return result;
}

original_code:
unsigned char PBI_MIO_D1GetByte(unsigned short addr)
{
 unsigned char result = 0x00; ;
 addr &= 0xffe3;
 do{}while(0);
 if (addr == 0xd1e2) {
  result = ((!PBI_SCSI_CD) | (!PBI_SCSI_MSG<<1) | (!PBI_SCSI_IO<<2) | (!PBI_SCSI_BSY<<5) | (!PBI_SCSI_REQ<<7));
 }
 else if (addr == 0xd1e1) {
  if (mio_scsi_enabled) {
   result = PBI_SCSI_GetByte()^0xff;
   PBI_SCSI_PutACK(1);
   PBI_SCSI_PutACK(0);
  }
 }
 return result;
}

variable_types:
addr
unsigned short

variable_types:
result
unsigned char