original_code:
uint16_t eclib_test_queue(void)
{
 uint16_t nErrors = 0;
 uint16_t nErrorsFound;
 uint16_t i, i2;
 uint16_t n, nn;
 uint32_t start_time = 0L;
 uint32_t end_time = 0L;
 eclib_status_t eStatus;
 uint32_t eclib_queue_version_id = eclib_queue_version();
 printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "Apr 23 2024", eclib_queue_version_id);
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 eclib_queue_init(&uint16Queue11, uint16Queue11_buffer, sizeof(uint16_t), 5);
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_clear( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++)
   {
    eStatus = eclib_queue_push(&uint16Queue13, &i);
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("push<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_full( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++ )
   {
    eStatus = eclib_queue_pop( &uint16Queue13, &i2 );
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("pop<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 return nErrors;
}

canonical_original_code:
unsigned short eclib_test_queue()
{
 unsigned short nErrors = 0;
 unsigned short nErrorsFound;
 unsigned short i; unsigned short i2;
 unsigned short n; unsigned short nn;
 unsigned int start_time = 0L;
 unsigned int end_time = 0L;
 enum eclib_status_enum eStatus;
 unsigned int eclib_queue_version_id = eclib_queue_version();
 printf("%s: %s\tv.%x\n", "eclib_test_queue.c", "Apr 23 2024", eclib_queue_version_id);
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 10; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > 1 )
 {
  nErrors ++;
 }
 eclib_queue_init(&uint16Queue11, uint16Queue11_buffer, sizeof(unsigned short), 5);
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_push(&uint16Queue11, &i);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 for ( i = 0; i <= 5; i ++)
 {
  eStatus = eclib_queue_pop(&uint16Queue11, &i2);
  if ( ECLIB_OK != eStatus )
  {
   nErrorsFound ++;
  }
 }
 if ( nErrorsFound > ( 1 + 10 - 5 ) )
 {
  nErrors ++;
 }
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_clear( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++)
   {
    eStatus = eclib_queue_push(&uint16Queue13, &i);
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("push<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 nErrorsFound = 0;
 start_time = clock();
 for ( nn = 0; nn < 100; nn ++ )
 {
  for ( n = 0; n < 1000; n ++ )
  {
   eStatus = eclib_queue_full( &uint16Queue13 );
   for ( i = 0; i < 5000UL; i ++ )
   {
    eStatus = eclib_queue_pop( &uint16Queue13, &i2 );
    if ( ECLIB_OK != eStatus )
    {
     nErrorsFound ++;
    }
   }
  }
 }
 end_time = clock();
 printf("pop<%d>( %d , %d )\t%d\n", i, n, eStatus, end_time-start_time);
 return nErrors;
}

variable_types:
nErrors
unsigned short

variable_types:
nErrorsFound
unsigned short

variable_types:
i
unsigned short

variable_types:
n
unsigned short

variable_types:
nn
unsigned short


main

canonical_name: main

repo: dingdongnigetou/shell

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
getcwd(pwd, 0x401uLL);
initialize_readline();
if ( argc == 1 )
{
INPUT = 0;
source = (__int64)stdin;
}
else
{
INPUT = 1;
source = (__int64)fopen(argv[1], "r");
if ( !source )
{
fprintf(stderr, "File %s not found\n", argv[1]);
exit(1);
}
}
parse();
exit(0);
}

original_code:
int main(int argc, char *argv[])
{
 getcwd(pwd, 1024 + 1);
 initialize_readline();
 if (argc == 1){
  INPUT = 0;
  source = stdin;
 }
 else{
  INPUT = 1;
  source = fopen(argv[1], "r");
  if (source == ((void *)0)){
   fprintf(stderr, "File %s not found\n", argv[1]);
   exit(1);
  }
 }
 parse();
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


isbuildin

decompiled_code:
__int64 __fastcall isbuildin(const char *a1)
{
unsigned int v1; // ecx

v1 = 1;
if ( strcmp(a1, "cd") )
{
v1 = 2;
if ( strcmp(a1, "pwd") )
{
v1 = 3;
if ( strcmp(a1, "exit") )
v1 = 4 * (strcmp(a1, "history") == 0);
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // ecx

v1 = 1;
if ( strcmp(a1, "cd") )
{
v1 = 2;
if ( strcmp(a1, "pwd") )
{
v1 = 3;
if ( strcmp(a1, "exit") )
v1 = 4 * (strcmp(a1, "history") == 0);
}
}
return v1;
}

original_code:
int isbuildin(char *s)
{
 BuildInType command = NO;
 if(!strcmp(s, "cd"))
  command = CD;
 else if (!strcmp(s, "pwd"))
  command = PWD;
 else if (!strcmp(s, "exit"))
  command = EXIT;
 else if (!strcmp(s, "history"))
  command = HISTORY;
 return command;
}

canonical_original_code:
int isbuildin(char *s)
{
 enum BuildInType command = NO;
 if(!strcmp(s, "cd"))
  command = CD;
 else if (!strcmp(s, "pwd"))
  command = PWD;
 else if (!strcmp(s, "exit"))
  command = EXIT;
 else if (!strcmp(s, "history"))
  command = HISTORY;
 return command;
}

variable_types:
s
char *

variable_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


decompiled_code:
void do_cd()
{
int *v0; // rbx

if ( arg[1025] )
{
if ( chdir((const char *)(unsigned int)((_DWORD)arg + 1025)) )
{
v0 = _errno_location();
if ( *v0 == 2 )
{
fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
else if ( *v0 == 20 )
{
fprintf(stdout, "ddsh: cd: %s: Not a directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
}
else
{
strcpy(&pwd, (const char *)(unsigned int)((_DWORD)arg + 1025));
}
}
}

canonical_decompiled_code:
void func1()
{
int *v0; // rbx

if ( arg[1025] )
{
if ( chdir((const char *)(unsigned int)((_DWORD)arg + 1025)) )
{
v0 = _errno_location();
if ( *v0 == 2 )
{
fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
else if ( *v0 == 20 )
{
fprintf(stdout, "ddsh: cd: %s: Not a directory\n", (const char *)(unsigned int)((_DWORD)arg + 1025));
*v0 = 0;
}
}
else
{
strcpy(&pwd, (const char *)(unsigned int)((_DWORD)arg + 1025));
}
}
}

original_code:
void do_cd()
{
 if (arg[1][0] != 0){
  if (!chdir(arg[1]))
   strcpy(pwd, arg[1]);
  else{
   if ((*__errno_location ()) == 2){
    fprintf(stdout, "ddsh: cd: %s: No such file or directory\n", arg[1]);
    (*__errno_location ()) = 0;
   }
   else if ((*__errno_location ()) == 20){
    fprintf(stdout, "ddsh: cd: %s: Not a directory\n", arg[1]);
    (*__errno_location ()) = 0;
   }
  }
 }
}

return_type: void


decompiled_code:
void __noreturn do_exit()
{
int v0; // eax

v0 = atoi((const char *)(unsigned int)((_DWORD)arg + 1025));
exit(v0);
}

canonical_decompiled_code:
void __noreturn func2()
{
int v0; // eax

v0 = atoi((const char *)(unsigned int)((_DWORD)arg + 1025));
exit(v0);
}

original_code:
void do_exit()
{
 exit(atoi(arg[1]));
}

canonical_original_code:
void do_exit()
{
 exit(atoi(arg[1]));
}


decompiled_code:
int do_pwd()
{
return fprintf(stdout, "%s\n", &pwd);
}

canonical_decompiled_code:
int func3()
{
return fprintf(stdout, "%s\n", &pwd);
}

original_code:
void do_pwd()
{
 fprintf(stdout, "%s\n", pwd);
}


runbuildin

decompiled_code:
void __fastcall runbuildin(unsigned int a1)
{
if ( a1 == 2 )
{
do_pwd();
}
else if ( a1 <= 2 )
{
if ( a1 == 1 )
do_cd();
}
else
{
if ( a1 == 3 )
do_exit();
if ( a1 == 4 )
do_history();
}
}

canonical_decompiled_code:
void __fastcall func5(unsigned int a1)
{
if ( a1 == 2 )
{
func3();
}
else if ( a1 <= 2 )
{
if ( a1 == 1 )
func1();
}
else
{
if ( a1 == 3 )
func2();
if ( a1 == 4 )
func4();
}
}

original_code:
void runbuildin(BuildInType command)
{
 switch(command)
 {
  case CD:
   do_cd();
   break;
  case PWD:
   do_pwd();
   break;
  case EXIT:
   do_exit();
   break;
  case HISTORY:
   do_history();
   break;
  case NO:
  default:break;
 }
}

canonical_original_code:
void runbuildin(enum BuildInType command)
{
 switch(command)
 {
  case CD:
   do_cd();
   break;
  case PWD:
   do_pwd();
   break;
  case EXIT:
   do_exit();
   break;
  case HISTORY:
   do_history();
   break;
  case NO:
  default:break;
 }
}

user_defined_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


decompiled_code:
char *__fastcall command_generator(char *s2, int a2)
{
int v2; // er13
char **v3; // rbp
const char *v4; // rbx

if ( !a2 )
v2 = strlen(s2);
v3 = off_2A8;
v4 = "login";
while ( strncmp(v4, s2, v2) )
{
v4 = *v3++;
if ( !v4 )
return (char *)v4;
}
return strdup(v4);
}

canonical_decompiled_code:
char *__fastcall func0(char *s2, int a2)
{
int v2; // er13
char **v3; // rbp
const char *v4; // rbx

if ( !a2 )
v2 = strlen(s2);
v3 = off_2A8;
v4 = "login";
while ( strncmp(v4, s2, v2) )
{
v4 = *v3++;
if ( !v4 )
return (char *)v4;
}
return strdup(v4);
}

original_code:
char *command_generator(const char *text, int state)
{
 const char *name;
 int list_index;
 int len;
 if (!state){
  list_index = 0;
  len = strlen(text);
 }
 while ( (name = commandarray[list_index++]) )
  if (strncmp(name, text, len) == 0)
   return strdup(name);
 return (char *)0;
}

canonical_original_code:
char *command_generator(char *text, int state)
{
 char *name;
 int list_index;
 int len;
 if (!state){
  list_index = 0;
  len = strlen(text);
 }
 while ( (name = commandarray[list_index++]) )
  if (strncmp(name, text, len) == 0)
   return strdup(name);
 return (char *)0;
}

variable_types:
text
char *

variable_types: state int

variable_types:
name
char *

variable_types: len int

return_type: char *


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

result = 0LL;
if ( !a2 )
result = rl_completion_matches(a1, command_generator);
return result;
}

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

result = 0LL;
if ( !a2 )
result = rl_completion_matches(a1, command_generator);
return result;
}

original_code:
char **command_completion(const char *text, int start, int end)
{
 char **matches = ((void *)0);
 if (start == 0)
  matches = rl_completion_matches(text, command_generator);
 return matches;
}

canonical_original_code:
char **command_completion(char *text, int start, int end)
{
 char **matches = ((void *)0);
 if (start == 0)
  matches = rl_completion_matches(text, command_generator);
 return matches;
}

variable_types: start int

variable_types: end int

variable_types:
matches
char **

return_type: char **


decompiled_code:
void *rl_gets()
{
char *v0; // rax
_BYTE *v1; // rax
char v3[72]; // [rsp+0h] [rbp-48h] BYREF

if ( line_read )
{
free(line_read);
line_read = 0LL;
}
v0 = strrchr(&pwd, 47);
sprintf(v3, "[dingdong %s]# ", v0 + 1);
v1 = (_BYTE *)readline(v3);
line_read = v1;
if ( v1 && *v1 )
add_history(v1);
return line_read;
}

canonical_decompiled_code:
void *func2()
{
char *v0; // rax
_BYTE *v1; // rax
char v3[72]; // [rsp+0h] [rbp-48h] BYREF

if ( line_read )
{
free(line_read);
line_read = 0LL;
}
v0 = strrchr(&pwd, 47);
sprintf(v3, "[dingdong %s]# ", v0 + 1);
v1 = (_BYTE *)readline(v3);
line_read = v1;
if ( v1 && *v1 )
add_history(v1);
return line_read;
}

original_code:
char *rl_gets()
{
 if (line_read){
  free(line_read);
  line_read = (char *)0;
 }
 char prompt[50];
 sprintf(prompt, "[dingdong %s]# ", strrchr(pwd, '/') + 1);
 line_read = readline(prompt);
 if (line_read && *line_read)
  add_history(line_read);
 return line_read;
}

variable_types:
prompt
char [50]


decompiled_code:
void initialize_readline()
{
rl_readline_name = "ddsh";
rl_attempted_completion_function = command_completion;
}

canonical_decompiled_code:
void func3()
{
rl_readline_name = "ddsh";
rl_attempted_completion_function = command_completion;
}


clearToken

decompiled_code:
void clearToken()
{
for ( ; token != 10; token = getToken() )
;
}

canonical_decompiled_code:
void func0()
{
for ( ; token != 10; token = getToken() )
;
}

original_code:
static void clearToken()
{
 while (token != NEWLINE)
  token = getToken();
}


match

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

if ( token != a1 )
return fprintf(stderr, "unexpected token: %s\n", tokenString);
result = getToken();
token = result;
return result;
}

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

if ( token != a1 )
return fprintf(stderr, "unexpected token: %s\n", tokenString);
result = getToken();
token = result;
return result;
}

original_code:
static void match(TokenType expected)
{
 if (token == expected)
  token = getToken();
 else
  fprintf(stderr, "unexpected token: %s\n", tokenString);
}

canonical_original_code:
static void match(enum TokenType expected)
{
 if (token == expected)
  token = getToken();
 else
  fprintf(stderr, "unexpected token: %s\n", tokenString);
}

variable_types: enum TokenType { ID=0, NUM=1, ASSIGN=2, SEMI=3, PIPE=4, DAEMON=5, OR=6, AND=7, RPAREN=8, LPAREN=9, NEWLINE=10, ENDFILE=11, COMMAND=12, ERROR=13, PARAM=14, IF=15, THEN=16, ELSE=17, FI=18, WHILE=19, DO=20, DONEWHILE=21 }


decompiled_code:
void while_stmt()
{
match(19);
if ( token == 9 )
{
match(9);
if ( token == 1 )
{
match(1);
strcpy(number, tokenString);
if ( token == 8 )
{
match(8);
if ( token == 3 )
{
match(3);
if ( token == 20 )
{
match(20);
while ( token != 3 )
{
token = getToken();
if ( token == 10 )
{
fwrite("missing ';' behinds 'do'\n", 1uLL, 0x19uLL, stderr);
clearToken();
return;
}
}
match(3);
if ( token == 21 )
{
match(21);
while ( atoi(number) )
forktoexec();
}
else
{
fwrite("missing 'done' at the end\n", 1uLL, 0x1AuLL, stderr);
clearToken();
}
}
else
{
fwrite("missing 'do' behinds commands\n", 1uLL, 0x1EuLL, stderr);
clearToken();
}
}
else
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
clearToken();
}
}
else
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
clearToken();
}
}
else
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
clearToken();
}
}
else
{
fwrite("missing '[' behinds while\n", 1uLL, 0x1AuLL, stderr);
clearToken();
}
}

canonical_decompiled_code:
void func2()
{
func1(19);
if ( token == 9 )
{
func1(9);
if ( token == 1 )
{
func1(1);
strcpy(number, tokenString);
if ( token == 8 )
{
func1(8);
if ( token == 3 )
{
func1(3);
if ( token == 20 )
{
func1(20);
while ( token != 3 )
{
token = getToken();
if ( token == 10 )
{
fwrite("missing ';' behinds 'do'\n", 1uLL, 0x19uLL, stderr);
func0();
return;
}
}
func1(3);
if ( token == 21 )
{
func1(21);
while ( atoi(number) )
forktoexec();
}
else
{
fwrite("missing 'done' at the end\n", 1uLL, 0x1AuLL, stderr);
func0();
}
}
else
{
fwrite("missing 'do' behinds commands\n", 1uLL, 0x1EuLL, stderr);
func0();
}
}
else
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
func0();
}
}
else
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
func0();
}
}
else
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
func0();
}
}
else
{
fwrite("missing '[' behinds while\n", 1uLL, 0x1AuLL, stderr);
func0();
}
}

original_code:
void while_stmt()
{
 match(WHILE);
 if (token != LPAREN){
  fprintf(stderr, "missing '[' behinds while\n");
  clearToken();
  return;
 }
 match(LPAREN);
 if (token != NUM){
  fprintf(stderr, "missing number in '[]'\n");
  clearToken();
  return;
 }
 match(NUM);
 strcpy(number, tokenString);
 if (token != RPAREN){
  fprintf(stderr, "missing ']'\n");
  clearToken();
  return;
 }
 match(RPAREN);
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds ']'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != DO){
  fprintf(stderr, "missing 'do' behinds commands\n");
  clearToken();
  return;
 }
 match(DO);
 while(token != SEMI){
  token = getToken();
  if (token == NEWLINE)
   break;
 }
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds 'do'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != DONEWHILE){
  fprintf(stderr, "missing 'done' at the end\n");
  clearToken();
  return;
 }
 match(DONEWHILE);
 while(atoi(number))
  forktoexec();
}


decompiled_code:
__int64 do_command()
{
_QWORD *v0; // rcx
__int64 result; // rax

forktoexec();
v0 = &arg;
result = 0LL;
do
{
*v0 = 0LL;
*(_QWORD *)((char *)v0 + 1017) = 0LL;
memset(
(void *)((unsigned __int64)(v0 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v0 - (((_DWORD)v0 + 8) & 0xFFFFFFF8) + 1025) >> 3));
v0 = (_QWORD *)((char *)v0 + 1025);
}
while ( v0 != (_QWORD *)(unsigned int)((_DWORD)&arg + 1050625) );
return result;
}

canonical_decompiled_code:
__int64 func3()
{
_QWORD *v0; // rcx
__int64 result; // rax

forktoexec();
v0 = &arg;
result = 0LL;
do
{
*v0 = 0LL;
*(_QWORD *)((char *)v0 + 1017) = 0LL;
memset(
(void *)((unsigned __int64)(v0 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v0 - (((_DWORD)v0 + 8) & 0xFFFFFFF8) + 1025) >> 3));
v0 = (_QWORD *)((char *)v0 + 1025);
}
while ( v0 != (_QWORD *)(unsigned int)((_DWORD)&arg + 1050625) );
return result;
}

original_code:
void do_command()
{
 forktoexec();
 for (int i = 0; i <= 1024; i++) memset(arg + i, 0, 1024 + 1);;
}

variable_types: i int


decompiled_code:
void if_stmt()
{
match(15);
if ( token == 9 )
{
match(9);
if ( token == 1 )
{
match(1);
strcpy(number, tokenString);
if ( token == 8 )
{
match(8);
if ( token == 3 )
{
match(3);
if ( token == 16 )
{
match(16);
while ( token != 3 )
{
token = getToken();
if ( token == 10 )
{
fwrite("missing ';' behinds commands\n", 1uLL, 0x1DuLL, stderr);
clearToken();
return;
}
}
match(3);
if ( token == 18 )
{
match(18);
if ( atoi(number) )
do_command();
}
else
{
fwrite("missing 'fi' at the end\n", 1uLL, 0x18uLL, stderr);
clearToken();
}
}
else
{
fwrite("missing 'then'\n", 1uLL, 0xFuLL, stderr);
clearToken();
}
}
else
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
clearToken();
}
}
else
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
clearToken();
}
}
else
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
clearToken();
}
}
else
{
fwrite("missing '[' behinds if\n", 1uLL, 0x17uLL, stderr);
clearToken();
}
}

canonical_decompiled_code:
void func4()
{
func1(15);
if ( token == 9 )
{
func1(9);
if ( token == 1 )
{
func1(1);
strcpy(number, tokenString);
if ( token == 8 )
{
func1(8);
if ( token == 3 )
{
func1(3);
if ( token == 16 )
{
func1(16);
while ( token != 3 )
{
token = getToken();
if ( token == 10 )
{
fwrite("missing ';' behinds commands\n", 1uLL, 0x1DuLL, stderr);
func0();
return;
}
}
func1(3);
if ( token == 18 )
{
func1(18);
if ( atoi(number) )
func3();
}
else
{
fwrite("missing 'fi' at the end\n", 1uLL, 0x18uLL, stderr);
func0();
}
}
else
{
fwrite("missing 'then'\n", 1uLL, 0xFuLL, stderr);
func0();
}
}
else
{
fwrite("missing ';' behinds ']'\n", 1uLL, 0x18uLL, stderr);
func0();
}
}
else
{
fwrite("missing ']'\n", 1uLL, 0xCuLL, stderr);
func0();
}
}
else
{
fwrite("missing number in '[]'\n", 1uLL, 0x17uLL, stderr);
func0();
}
}
else
{
fwrite("missing '[' behinds if\n", 1uLL, 0x17uLL, stderr);
func0();
}
}

original_code:
void if_stmt()
{
 match(IF);
 if (token != LPAREN){
  fprintf(stderr, "missing '[' behinds if\n");
  clearToken();
  return;
 }
 match(LPAREN);
 if (token != NUM){
  fprintf(stderr, "missing number in '[]'\n");
  clearToken();
  return;
 }
 else{
  match(NUM);
  strcpy(number, tokenString);
 }
 if (token != RPAREN){
  fprintf(stderr, "missing ']'\n");
  clearToken();
  return;
 }
 match(RPAREN);
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds ']'\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != THEN){
  fprintf(stderr, "missing 'then'\n");
  clearToken();
  return;
 }
 match(THEN);
 while(token != SEMI){
  token = getToken();
  if (token == NEWLINE)
   break;
 }
 if (token != SEMI){
  fprintf(stderr, "missing ';' behinds commands\n");
  clearToken();
  return;
 }
 match(SEMI);
 if (token != FI){
  fprintf(stderr, "missing 'fi' at the end\n");
  clearToken();
  return;
 }
 match(FI);
 if (atoi(number))
  do_command();
}


statement

decompiled_code:
void statement()
{
int v0; // eax

switch ( token )
{
case 3:
match(3);
break;
case 10:
match(10);
break;
case 12:
do
{
v0 = getToken();
token = v0;
}
while ( v0 != 10 && v0 != 3 );
do_command();
break;
case 15:
if_stmt();
break;
case 19:
while_stmt();
break;
default:
fprintf(stderr, "unexpected token %s\n", tokenString);
break;
}
}

canonical_decompiled_code:
void func5()
{
int v0; // eax

switch ( token )
{
case 3:
func1(3);
break;
case 10:
func1(10);
break;
case 12:
do
{
v0 = getToken();
token = v0;
}
while ( v0 != 10 && v0 != 3 );
func3();
break;
case 15:
func4();
break;
case 19:
func2();
break;
default:
fprintf(stderr, "unexpected token %s\n", tokenString);
break;
}
}

original_code:
void statement()
{
 switch(token)
 {
 case IF:
  if_stmt();
         break;
 case WHILE:
  while_stmt();
         break;
 case COMMAND:
  while(token != SEMI){
   token = getToken();
   if (token == NEWLINE)
    break;
  }
  do_command();
  break;
 case SEMI:
  match(SEMI);
  break;
 case NEWLINE:
  match(NEWLINE);
  break;
 default:
  fprintf(stderr, "unexpected token %s\n", tokenString);
  break;
 }
}


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

statement();
for ( result = (unsigned int)token; token != 11; result = (unsigned int)token )
{
if ( (_DWORD)result == 3 )
match(3);
statement();
}
return result;
}

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

func5();
for ( result = (unsigned int)token; token != 11; result = (unsigned int)token )
{
if ( (_DWORD)result == 3 )
func1(3);
func5();
}
return result;
}

original_code:
void stmt_sequence()
{
 statement();
 while (token != ENDFILE){
  if (token == SEMI)
   match(SEMI);
  statement();
 }
}


parse

decompiled_code:
__int64 parse()
{
token = getToken();
return stmt_sequence();
}

canonical_decompiled_code:
__int64 func7()
{
token = getToken();
return func6();
}

original_code:
void parse()
{
 token = getToken();
 stmt_sequence();
}


decompiled_code:
int error_process()
{
int *v0; // rbx
int result; // eax

v0 = _errno_location();
result = *v0;
switch ( *v0 )
{
case 2:
result = fprintf(stderr, "ddsh: %s: command not found\n", (const char *)&commands);
break;
case 5:
result = fwrite("ddsh: Input/output error\n", 1uLL, 0x19uLL, stderr);
break;
case 7:
result = fwrite("ddsh: Argument list too long\n", 1uLL, 0x1DuLL, stderr);
break;
case 13:
result = fprintf(stderr, "ddsh: %s: Permission denied\n", (const char *)&commands);
break;
case 16:
result = fwrite("ddsh: Device or resource busy\n", 1uLL, 0x1EuLL, stderr);
break;
case 17:
result = fwrite("ddsh: File exists\n", 1uLL, 0x12uLL, stderr);
break;
case 19:
result = fwrite("ddsh: No such device\n", 1uLL, 0x15uLL, stderr);
break;
case 21:
result = fprintf(stderr, "ddsh: %s: Is a dirextory\n", (const char *)&commands);
break;
case 22:
result = fwrite("ddsh: Invalid argument\n", 1uLL, 0x17uLL, stderr);
break;
case 27:
result = fwrite("ddsh: File too large\n", 1uLL, 0x15uLL, stderr);
break;
default:
break;
}
*v0 = 0;
return result;
}

canonical_decompiled_code:
int func0()
{
int *v0; // rbx
int result; // eax

v0 = _errno_location();
result = *v0;
switch ( *v0 )
{
case 2:
result = fprintf(stderr, "ddsh: %s: command not found\n", (const char *)&commands);
break;
case 5:
result = fwrite("ddsh: Input/output error\n", 1uLL, 0x19uLL, stderr);
break;
case 7:
result = fwrite("ddsh: Argument list too long\n", 1uLL, 0x1DuLL, stderr);
break;
case 13:
result = fprintf(stderr, "ddsh: %s: Permission denied\n", (const char *)&commands);
break;
case 16:
result = fwrite("ddsh: Device or resource busy\n", 1uLL, 0x1EuLL, stderr);
break;
case 17:
result = fwrite("ddsh: File exists\n", 1uLL, 0x12uLL, stderr);
break;
case 19:
result = fwrite("ddsh: No such device\n", 1uLL, 0x15uLL, stderr);
break;
case 21:
result = fprintf(stderr, "ddsh: %s: Is a dirextory\n", (const char *)&commands);
break;
case 22:
result = fwrite("ddsh: Invalid argument\n", 1uLL, 0x17uLL, stderr);
break;
case 27:
result = fwrite("ddsh: File too large\n", 1uLL, 0x15uLL, stderr);
break;
default:
break;
}
*v0 = 0;
return result;
}

original_code:
void error_process()
{
 switch((*__errno_location ()))
 {
 case 7:fprintf(stderr, "ddsh: Argument list too long\n");
     break;
 case 13:fprintf(stderr, "ddsh: %s: Permission denied\n", commands);
      break;
 case 16:fprintf(stderr, "ddsh: Device or resource busy\n");
     break;
 case 17:fprintf(stderr, "ddsh: File exists\n");
      break;
 case 27:fprintf(stderr, "ddsh: File too large\n");
     break;
 case 22:fprintf(stderr, "ddsh: Invalid argument\n");
      break;
 case 5:fprintf(stderr, "ddsh: Input/output error\n");
   break;
 case 21:fprintf(stderr, "ddsh: %s: Is a dirextory\n", commands);
      break;
 case 19:fprintf(stderr, "ddsh: No such device\n");
      break;
 case 2:fprintf(stderr, "ddsh: %s: command not found\n", commands);
      break;
 }
 (*__errno_location ()) = 0;
}


ungetNextChar

decompiled_code:
void ungetNextChar()
{
--linepos;
}

canonical_decompiled_code:
void func2()
{
--linepos;
}

original_code:
void ungetNextChar(void)
{
 linepos--;
}

canonical_original_code:
void ungetNextChar()
{
 linepos--;
}


getToken

decompiled_code:
__int64 getToken()
{
int v0; // er14
int v1; // er13
int v2; // ebp
int v3; // er12
__int64 v4; // rax
__int64 v5; // rax
int v6; // eax
unsigned int v7; // ebx
char v9; // al
char v10; // al
int v11; // eax
unsigned __int64 v12; // rbx
char v13[1096]; // [rsp+0h] [rbp-448h] BYREF

v0 = 0;
v1 = 1;
v2 = 0;
v3 = 0;
while ( 2 )
{
v12 = (unsigned int)getNextChar();
switch ( v3 )
{
case 0:
if ( (_BYTE)v12 == 35 )
{
v3 = 3;
}
else
{
if ( ((*_ctype_b_loc())[(char)v12] & 0x800) != 0 )
{
v3 = 4;
goto LABEL_47;
}
switch ( (_BYTE)v12 )
{
case '[':
currentToken = 9;
goto LABEL_30;
case ']':
currentToken = 8;
goto LABEL_30;
case ';':
currentToken = 3;
goto LABEL_30;
}
if ( (_BYTE)v12 != 32 && (_BYTE)v12 != 9 )
{
if ( !(_BYTE)v12 )
{
currentToken = 10;
goto LABEL_30;
}
if ( (_BYTE)v12 == 0xFF )
{
currentToken = 11;
goto LABEL_30;
}
v3 = 1;
if ( currentToken != 12 )
goto LABEL_47;
strcpy(arg, &tokenString);
ungetNextChar();
v3 = 2;
}
}
goto LABEL_45;
case 1:
if ( (unsigned __int8)v12 > 0x3Bu )
{
if ( (_BYTE)v12 == 124 )
{
currentToken = 12;
goto LABEL_30;
}
}
else
{
v4 = 0x800024100000200LL;
if ( _bittest64(&v4, v12) )
{
currentToken = 12;
if ( !(_BYTE)v12 || (_BYTE)v12 == 59 )
goto LABEL_22;
goto LABEL_30;
}
}
if ( !(_BYTE)v12 )
{
currentToken = 12;
goto LABEL_22;
}
LABEL_47:
if ( v2 > 1024 )
{
v11 = v2;
}
else
{
LABEL_48:
v11 = v2 + 1;
*(&tokenString + v2) = v12;
if ( v3 == 5 )
{
++v2;
goto LABEL_30;
}
}
goto LABEL_49;
case 2:
if ( (_BYTE)v12 == 59 )
{
currentToken = 14;
if ( !v0 )
{
*(_WORD *)&arg[1025 * v1] = 35;
goto LABEL_22;
}
LABEL_34:
v13[v0] = 0;
strcpy(&arg[1025 * v1++], v13);
goto LABEL_28;
}
if ( (_BYTE)v12 != 124 )
{
if ( (unsigned __int8)v12 > 0x29u || (v5 = 0x34000000001LL, !_bittest64(&v5, v12)) )
{
if ( (_BYTE)v12 == 32 || (_BYTE)v12 == 9 )
{
v9 = getNextChar();
if ( v9 == 32 || v9 == 9 )
{
do
{
do
v10 = getNextChar();
while ( v10 == 32 );
}
while ( v10 == 9 );
}
ungetNextChar();
v13[v0] = 0;
strcpy(&arg[1025 * v1++], v13);
v0 = 0;
}
else
{
v13[v0++] = v12;
}
LABEL_45:
v11 = v2;
LABEL_49:
v2 = v11;
continue;
}
}
currentToken = 14;
if ( v0 )
goto LABEL_34;
LABEL_28:
*(_WORD *)&arg[1025 * v1] = 35;
if ( (_BYTE)v12 && (_BYTE)v12 != 59 )
goto LABEL_30;
LABEL_22:
ungetNextChar();
LABEL_30:
v6 = currentToken;
if ( currentToken != 12 && currentToken != 1 )
return (unsigned int)currentToken;
*(&tokenString + v2) = 0;
if ( v6 != 12 )
return (unsigned int)currentToken;
currentToken = keywordLookup(&tokenString);
v7 = currentToken;
if ( currentToken == 12 )
strcpy(&commands, &tokenString);
return v7;
case 3:
if ( !(_BYTE)v12 )
v3 = 0;
goto LABEL_45;
case 4:
if ( ((*_ctype_b_loc())[(char)v12] & 0x800) != 0 )
goto LABEL_47;
currentToken = 1;
ungetNextChar();
goto LABEL_30;
default:
currentToken = 13;
if ( v2 > 1024 )
return (unsigned int)currentToken;
v3 = 5;
goto LABEL_48;
}
}
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // er14
int v1; // er13
int v2; // ebp
int v3; // er12
__int64 v4; // rax
__int64 v5; // rax
int v6; // eax
unsigned int v7; // ebx
char v9; // al
char v10; // al
int v11; // eax
unsigned __int64 v12; // rbx
char v13[1096]; // [rsp+0h] [rbp-448h] BYREF

v0 = 0;
v1 = 1;
v2 = 0;
v3 = 0;
while ( 2 )
{
v12 = (unsigned int)func1();
switch ( v3 )
{
case 0:
if ( (_BYTE)v12 == 35 )
{
v3 = 3;
}
else
{
if ( ((*_ctype_b_loc())[(char)v12] & 0x800) != 0 )
{
v3 = 4;
goto LABEL_47;
}
switch ( (_BYTE)v12 )
{
case '[':
currentToken = 9;
goto LABEL_30;
case ']':
currentToken = 8;
goto LABEL_30;
case ';':
currentToken = 3;
goto LABEL_30;
}
if ( (_BYTE)v12 != 32 && (_BYTE)v12 != 9 )
{
if ( !(_BYTE)v12 )
{
currentToken = 10;
goto LABEL_30;
}
if ( (_BYTE)v12 == 0xFF )
{
currentToken = 11;
goto LABEL_30;
}
v3 = 1;
if ( currentToken != 12 )
goto LABEL_47;
strcpy(arg, &tokenString);
func2();
v3 = 2;
}
}
goto LABEL_45;
case 1:
if ( (unsigned __int8)v12 > 0x3Bu )
{
if ( (_BYTE)v12 == 124 )
{
currentToken = 12;
goto LABEL_30;
}
}
else
{
v4 = 0x800024100000200LL;
if ( _bittest64(&v4, v12) )
{
currentToken = 12;
if ( !(_BYTE)v12 || (_BYTE)v12 == 59 )
goto LABEL_22;
goto LABEL_30;
}
}
if ( !(_BYTE)v12 )
{
currentToken = 12;
goto LABEL_22;
}
LABEL_47:
if ( v2 > 1024 )
{
v11 = v2;
}
else
{
LABEL_48:
v11 = v2 + 1;
*(&tokenString + v2) = v12;
if ( v3 == 5 )
{
++v2;
goto LABEL_30;
}
}
goto LABEL_49;
case 2:
if ( (_BYTE)v12 == 59 )
{
currentToken = 14;
if ( !v0 )
{
*(_WORD *)&arg[1025 * v1] = 35;
goto LABEL_22;
}
LABEL_34:
v13[v0] = 0;
strcpy(&arg[1025 * v1++], v13);
goto LABEL_28;
}
if ( (_BYTE)v12 != 124 )
{
if ( (unsigned __int8)v12 > 0x29u || (v5 = 0x34000000001LL, !_bittest64(&v5, v12)) )
{
if ( (_BYTE)v12 == 32 || (_BYTE)v12 == 9 )
{
v9 = func1();
if ( v9 == 32 || v9 == 9 )
{
do
{
do
v10 = func1();
while ( v10 == 32 );
}
while ( v10 == 9 );
}
func2();
v13[v0] = 0;
strcpy(&arg[1025 * v1++], v13);
v0 = 0;
}
else
{
v13[v0++] = v12;
}
LABEL_45:
v11 = v2;
LABEL_49:
v2 = v11;
continue;
}
}
currentToken = 14;
if ( v0 )
goto LABEL_34;
LABEL_28:
*(_WORD *)&arg[1025 * v1] = 35;
if ( (_BYTE)v12 && (_BYTE)v12 != 59 )
goto LABEL_30;
LABEL_22:
func2();
LABEL_30:
v6 = currentToken;
if ( currentToken != 12 && currentToken != 1 )
return (unsigned int)currentToken;
*(&tokenString + v2) = 0;
if ( v6 != 12 )
return (unsigned int)currentToken;
currentToken = func0(&tokenString);
v7 = currentToken;
if ( currentToken == 12 )
strcpy(&commands, &tokenString);
return v7;
case 3:
if ( !(_BYTE)v12 )
v3 = 0;
goto LABEL_45;
case 4:
if ( ((*_ctype_b_loc())[(char)v12] & 0x800) != 0 )
goto LABEL_47;
currentToken = 1;
func2();
goto LABEL_30;
default:
currentToken = 13;
if ( v2 > 1024 )
return (unsigned int)currentToken;
v3 = 5;
goto LABEL_48;
}
}
}

original_code:
TokenType getToken(void)
{
 StateType state = START;
 int tokenStringIndex = 0;
        int save;
 int argrow = 1;
 char temp[1024 + 1];
 int templen = 0;
 while (state != DONE){
  char nextch;
  char ch = getNextChar();
  save = 1;
  switch (state)
  {
  case START:
   if (ch == '#'){
    save = 0;
    state = INCOMMENT;
   }
   else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISdigit))
    state = INNUM;
   else if (ch == '['){
    save = 0;
    state = DONE;
    currentToken = LPAREN;
   }
   else if (ch == ']'){
    save = 0;
    state = DONE;
    currentToken = RPAREN;
   }
   else if (ch == ';'){
    save = 0;
    state = DONE;
    currentToken = SEMI;
   }
   else if (ch == ' ' || ch == '\t'){
    save = 0;
   }
   else if (ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = NEWLINE;
   }
   else if (ch == (-1) ){
    save = 0;
    state = DONE;
    currentToken = ENDFILE;
   }
          else{
    if (currentToken == COMMAND){
     save = 0;
     state = INPARAM;
     strcpy(arg[0], tokenString);
     ungetNextChar();
    }
    else
     state = INCOMMAND;
   }
   break;
  case INCOMMAND:
   if (ch == ' ' || ch == '\t' || ch == ';' || ch == '&' ||
     ch == '|' || ch == ')' || ch == ')' || ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = COMMAND;
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   break;
  case INPARAM:
   if (ch == ';' || ch == '&' || ch == '|' ||
     ch == '(' || ch == ')' || ch == '\0'){
    state = DONE;
    currentToken = PARAM;
    if (templen != 0){
     temp[templen] = '\0';
     templen = 0;
     strcpy(arg[argrow++], temp);
    }
    strcpy(arg[argrow], "#");
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   else if (ch == ' ' || ch == '\t'){
    nextch = getNextChar();
    while(nextch == ' ' || nextch == '\t')
     nextch = getNextChar();
    ungetNextChar();
    temp[templen] = '\0';
    templen = 0;
    strcpy(arg[argrow++], temp);
   }
   else
    temp[templen++] = ch;
   save = 0;
   break;
  case INCOMMENT:
   save = 0;
   if (ch == '\0')
    state = START;
   break;
  case INNUM:
   if (!((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISdigit)){
    save = 0;
    state = DONE;
    currentToken = NUM;
    ungetNextChar();
   }
   break;
  default:
   state = DONE;
   currentToken = ERROR;
   break;
  }
  if (save && tokenStringIndex <= 1024)
   tokenString[tokenStringIndex++] = ch;
  if (state == DONE && (currentToken == COMMAND || currentToken == NUM) ){
   tokenString[tokenStringIndex] = '\0';
   if (currentToken == COMMAND)
    currentToken = keywordLookup(tokenString);
  }
 }
 if (currentToken == COMMAND)
  strcpy(commands, tokenString);
 return currentToken;
}

canonical_original_code:
enum TokenType getToken()
{
 enum StateType state = START;
 int tokenStringIndex = 0;
        int save;
 int argrow = 1;
 char temp[-1];
 int templen = 0;
 while (state != DONE){
  char nextch;
  char ch = getNextChar();
  save = 1;
  switch (state)
  {
  case START:
   if (ch == '#'){
    save = 0;
    state = INCOMMENT;
   }
   else if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISdigit))
    state = INNUM;
   else if (ch == '['){
    save = 0;
    state = DONE;
    currentToken = LPAREN;
   }
   else if (ch == ']'){
    save = 0;
    state = DONE;
    currentToken = RPAREN;
   }
   else if (ch == ';'){
    save = 0;
    state = DONE;
    currentToken = SEMI;
   }
   else if (ch == ' ' || ch == '\t'){
    save = 0;
   }
   else if (ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = NEWLINE;
   }
   else if (ch == (-1) ){
    save = 0;
    state = DONE;
    currentToken = ENDFILE;
   }
          else{
    if (currentToken == COMMAND){
     save = 0;
     state = INPARAM;
     strcpy(arg[0], tokenString);
     ungetNextChar();
    }
    else
     state = INCOMMAND;
   }
   break;
  case INCOMMAND:
   if (ch == ' ' || ch == '\t' || ch == ';' || ch == '&' ||
     ch == '|' || ch == ')' || ch == ')' || ch == '\0'){
    save = 0;
    state = DONE;
    currentToken = COMMAND;
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   break;
  case INPARAM:
   if (ch == ';' || ch == '&' || ch == '|' ||
     ch == '(' || ch == ')' || ch == '\0'){
    state = DONE;
    currentToken = PARAM;
    if (templen != 0){
     temp[templen] = '\0';
     templen = 0;
     strcpy(arg[argrow++], temp);
    }
    strcpy(arg[argrow], "#");
    if (ch == '\0' || ch == ';')
     ungetNextChar();
   }
   else if (ch == ' ' || ch == '\t'){
    nextch = getNextChar();
    while(nextch == ' ' || nextch == '\t')
     nextch = getNextChar();
    ungetNextChar();
    temp[templen] = '\0';
    templen = 0;
    strcpy(arg[argrow++], temp);
   }
   else
    temp[templen++] = ch;
   save = 0;
   break;
  case INCOMMENT:
   save = 0;
   if (ch == '\0')
    state = START;
   break;
  case INNUM:
   if (!((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISdigit)){
    save = 0;
    state = DONE;
    currentToken = NUM;
    ungetNextChar();
   }
   break;
  default:
   state = DONE;
   currentToken = ERROR;
   break;
  }
  if (save && tokenStringIndex <= 1024)
   tokenString[tokenStringIndex++] = ch;
  if (state == DONE && (currentToken == COMMAND || currentToken == NUM) ){
   tokenString[tokenStringIndex] = '\0';
   if (currentToken == COMMAND)
    currentToken = keywordLookup(tokenString);
  }
 }
 if (currentToken == COMMAND)
  strcpy(commands, tokenString);
 return currentToken;
}

variable_types: enum StateType { START=0, INCOMMAND=1, INPARAM=2, INCOMMENT=3, INNUM=4, DONE=5 }

variable_types: tokenStringIndex int

variable_types: save int

variable_types: argrow int

variable_types:
temp
char [-1]

variable_types: templen int

variable_types: nextch char

variable_types: ch char

return_type: enum TokenType { ID=0, NUM=1, ASSIGN=2, SEMI=3, PIPE=4, DAEMON=5, OR=6, AND=7, RPAREN=8, LPAREN=9, NEWLINE=10, ENDFILE=11, COMMAND=12, ERROR=13, PARAM=14, IF=15, THEN=16, ELSE=17, FI=18, WHILE=19, DO=20, DONEWHILE=21 }


forktoexec

decompiled_code:
__pid_t forktoexec()
{
unsigned int v0; // eax
__pid_t v2; // eax
__pid_t v3; // ebx
const char *v4; // rax
__int64 i; // r8
char *v6; // r9
bool v7; // zf
char *v8[1025]; // [rsp+0h] [rbp-2018h] BYREF
int stat_loc[3]; // [rsp+200Ch] [rbp-Ch] BYREF

v0 = isbuildin(&tokenString);
if ( v0 )
return runbuildin(v0);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
fwrite("fork error...", 1uLL, 0xDuLL, stderr);
}
else if ( !v2 )
{
if ( arg[1025] )
{
v4 = arg;
for ( i = 0LL; ; ++i )
{
v6 = (char *)v4;
v7 = strcmp(v4, "#") == 0;
v4 += 1025;
if ( v7 )
break;
v8[i] = v6;
}
v8[(int)i] = 0LL;
execvp(&commands, v8);
}
else
{
execlp(&commands, &commands, 0LL);
}
}
error_process();
return waitpid(v3, stat_loc, 0);
}

canonical_decompiled_code:
__pid_t func0()
{
unsigned int v0; // eax
__pid_t v2; // eax
__pid_t v3; // ebx
const char *v4; // rax
__int64 i; // r8
char *v6; // r9
bool v7; // zf
char *v8[1025]; // [rsp+0h] [rbp-2018h] BYREF
int stat_loc[3]; // [rsp+200Ch] [rbp-Ch] BYREF

v0 = isbuildin(&tokenString);
if ( v0 )
return runbuildin(v0);
v2 = fork();
v3 = v2;
if ( v2 < 0 )
{
fwrite("fork error...", 1uLL, 0xDuLL, stderr);
}
else if ( !v2 )
{
if ( arg[1025] )
{
v4 = arg;
for ( i = 0LL; ; ++i )
{
v6 = (char *)v4;
v7 = strcmp(v4, "#") == 0;
v4 += 1025;
if ( v7 )
break;
v8[i] = v6;
}
v8[(int)i] = 0LL;
execvp(&commands, v8);
}
else
{
execlp(&commands, &commands, 0LL);
}
}
error_process();
return waitpid(v3, stat_loc, 0);
}

original_code:
void forktoexec()
{
 pid_t pid;
 int i;
 int status;
 char *arg_temp[1024 + 1];
 BuildInType command;
 if ( (command = isbuildin(tokenString)) )
  runbuildin(command);
 else{
  if ((pid = fork()) < 0)
   fprintf(stderr, "fork error...");
  else if (pid == 0){
   if (arg[1][0] != 0){
    for (i = 0; strcmp(arg[i], "#") != 0; i++)
     arg_temp[i] = arg[i];
    arg_temp[i] = ((void *)0);
    execvp(commands, arg_temp);
   }
   else{
    execlp(commands, commands, (char *)0);
   }
  }
  error_process();
  if ((pid == waitpid(pid, &status, 0)) < 0)
   fprintf(stderr, "waitpid error...");
  return ;
 }
}

canonical_original_code:
void forktoexec()
{
 int pid;
 int i;
 int status;
 char *arg_temp[-1];
 enum BuildInType command;
 if ( (command = isbuildin(tokenString)) )
  runbuildin(command);
 else{
  if ((pid = fork()) < 0)
   fprintf(stderr, "fork error...");
  else if (pid == 0){
   if (arg[1][0] != 0){
    for (i = 0; strcmp(arg[i], "#") != 0; i++)
     arg_temp[i] = arg[i];
    arg_temp[i] = ((void *)0);
    execvp(commands, arg_temp);
   }
   else{
    execlp(commands, commands, (char *)0);
   }
  }
  error_process();
  if ((pid == waitpid(pid, &status, 0)) < 0)
   fprintf(stderr, "waitpid error...");
  return ;
 }
}

variable_types: pid int

variable_types: status int

variable_types: enum BuildInType { NO=0, CD=1, PWD=2, EXIT=3, HISTORY=4 }


repo: dingmaotu/ldcell

variable_types:
a
double [3]

variable_types:
b
double [3]

variable_types:
c
double [3]


decompiled_code:
void __fastcall real_prod(double *a1, double a2)
{
*a1 = a2 * *a1;
a1[1] = a2 * a1[1];
a1[2] = a2 * a1[2];
}

original_code:
void real_prod(double a[3], double num)
{
    do{ for(int __i=0; __i<3; ++__i) { a[__i] *= num; } }while(0);
}

canonical_original_code:
void real_prod(double a[3], double num)
{
    do{ for(int __i = 0; __i<3; ++__i) { a[__i] *= num; } }while(0);
}

variable_types: num double


decompiled_code:
void __fastcall scale_cell(double *a1, double a2)
{
double v2; // rbx
double v3; // xmm0_8

v2 = a2;
if ( a2 < 0.0 )
{
v3 = COERCE_DOUBLE(cell_volume(a1));
v2 = pow(COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC1) / v3, 0.3333333333333333);
}
real_prod(a1, v2);
real_prod(a1 + 3, v2);
real_prod(a1 + 6, v2);
}

canonical_decompiled_code:
void __fastcall func3(double *a1, double a2)
{
double v2; // rbx
double v3; // xmm0_8

v2 = a2;
if ( a2 < 0.0 )
{
v3 = COERCE_DOUBLE(cell_volume(a1));
v2 = pow(COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC1) / v3, 0.3333333333333333);
}
func1(a1, v2);
func1(a1 + 3, v2);
func1(a1 + 6, v2);
}

original_code:
void scale_cell(double c[3][3], double s)
{
    if (s < 0)
        s = pow(-s/cell_volume(c), 1.0/3);
    for(int i=0; i<3; ++i) {
        real_prod(c[i], s);
    }
}

canonical_original_code:
void scale_cell(double c[3][3], double s)
{
    if (s < 0)
        s = pow(-s/cell_volume(c), 1.0/3);
    for(int i = 0; i<3; ++i) {
        real_prod(c[i], s);
    }
}

variable_types:
c
double [3][3]

variable_types: s double


sighandler

repo: dingram/led-display

decompiled_code:
void __noreturn sighandler()
{
puts("Cleaning up...");
ldisplay_reset();
ldisplay_cleanup();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
puts("Cleaning up...");
ldisplay_reset();
ldisplay_cleanup();
exit(0);
}

original_code:
static void sighandler(int sig) {
  printf("Cleaning up...\n");
  ldisplay_reset();
 ldisplay_cleanup();
  exit(0);
}

variable_types: sig int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
puts("Usage:");
printf(" %s [--no-totally-unnecessary-brightness-flicker]\n\n", a1);
puts(" -s, --no-totally-unnecessary-brightness-flicker Disable brightness flicker");
puts(" when updating the time.");
puts(" --help Display this screen.");
putchar(10);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
puts("Usage:");
printf(" %s [--no-totally-unnecessary-brightness-flicker]\n\n", a1);
puts(" -s, --no-totally-unnecessary-brightness-flicker Disable brightness flicker");
puts(" when updating the time.");
puts(" --help Display this screen.");
putchar(10);
exit(0);
}

original_code:
static void usage(char *progname) {
  printf("Usage:\n");
  printf("    %s [--no-totally-unnecessary-brightness-flicker]\n\n", progname);
  printf("  -s, --no-totally-unnecessary-brightness-flicker  Disable brightness flicker\n");
  printf("                                                   when updating the time.\n");
  printf("  --help                                           Display this screen.\n");
  printf("\n");
  exit(0);
}

variable_types:
progname
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__sigset_t *v4; // rdi
__int64 i; // rcx
unsigned int v6; // eax
unsigned int v7; // er12
unsigned int v9; // er13
unsigned int v10; // eax
int v11; // ebp
struct tm *v12; // rax
unsigned int v13; // ebx
int longind; // [rsp+Ch] [rbp-14Ch] BYREF
struct option longopts; // [rsp+10h] [rbp-148h] BYREF
const char *v16; // [rsp+30h] [rbp-128h]
int v17; // [rsp+38h] [rbp-120h]
int *v18; // [rsp+40h] [rbp-118h]
int v19; // [rsp+48h] [rbp-110h]
const char *v20; // [rsp+50h] [rbp-108h]
int v21; // [rsp+58h] [rbp-100h]
__int64 v22; // [rsp+60h] [rbp-F8h]
int v23; // [rsp+68h] [rbp-F0h]
__int64 v24; // [rsp+70h] [rbp-E8h]
int v25; // [rsp+78h] [rbp-E0h]
__int64 v26; // [rsp+80h] [rbp-D8h]
int v27; // [rsp+88h] [rbp-D0h]
struct sigaction v28; // [rsp+90h] [rbp-C8h] BYREF
int v29[11]; // [rsp+12Ch] [rbp-2Ch] BYREF

v29[0] = 1;
longopts.name = "no-totally-unnecessary-brightness-flicker";
longopts.has_arg = 0;
longopts.flag = v29;
longopts.val = 0;
v16 = "disable-totally-unnecessary-brightness-flicker";
v17 = 0;
v18 = v29;
v19 = 0;
v20 = "help";
v21 = 0;
v22 = 0LL;
v23 = 0;
v24 = 0LL;
v25 = 0;
v26 = 0LL;
v27 = 0;
while ( 1 )
{
longind = 0;
v3 = getopt_long(argc, (char *const *)argv, "s", &longopts, &longind);
if ( v3 == -1 )
break;
if ( v3 == 63 )
exit(1);
if ( v3 == 115 )
{
v29[0] = 0;
}
else if ( v3 )
{
printf("??? getopt returned character code 0x%x ???\n", v3);
}
else if ( longind == 2 )
{
usage(*argv);
}
}
setbuf(stdout, 0LL);
puts("LED Display: clock program");
puts("Written by Dave Ingram");
puts("-----------------------------------------------");
puts(" http://www.dmi.me.uk/code/linux/leddisplay/ \n");
if ( (unsigned int)ldisplay_init() )
{
fwrite("\x1B[1;31mDevice failed to initialise!\x1B[0m\n", 1uLL, 0x28uLL, stderr);
}
else
{
v4 = &v28.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v4->__val[0]) = 0;
v4 = (__sigset_t *)((char *)v4 + 4);
}
v28.sa_handler = (__sighandler_t)sighandler;
sigaction(1, &v28, 0LL);
sigaction(2, &v28, 0LL);
v6 = ldisplay_reset();
v7 = v6;
if ( !v6 )
{
ldisplay_setBrightness(0LL);
LABEL_32:
longopts.name = (const char *)time(0LL);
v12 = localtime((const time_t *)&longopts);
v13 = v12->tm_min + 100 * v12->tm_hour;
v11 = 5;
while ( !v29[0] || v13 == v7 )
{
v9 = v13;
v10 = ldisplay_showTime(v13, 0LL);
if ( v10 )
goto LABEL_30;
usleep(0x186A0u);
if ( v13 != v7 )
goto LABEL_29;
LABEL_22:
v10 = ldisplay_showTime(v9, 0LL);
if ( v10 )
goto LABEL_30;
usleep(0x493E0u);
if ( !--v11 )
{
v7 = v13;
goto LABEL_32;
}
v7 = v13;
}
ldisplay_setBrightness(2LL);
v9 = v13;
v10 = ldisplay_showTime(v13, 0LL);
if ( v10 )
{
LABEL_30:
fprintf(stderr, "\x1B[1;31mDevice failed to respond: %d\x1B[0m\n", v10);
ldisplay_cleanup();
return 1;
}
usleep(0x186A0u);
LABEL_29:
ldisplay_setBrightness(0LL);
goto LABEL_22;
}
fprintf(stderr, "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n", v6);
ldisplay_cleanup();
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__sigset_t *v4; // rdi
__int64 i; // rcx
unsigned int v6; // eax
unsigned int v7; // er12
unsigned int v9; // er13
unsigned int v10; // eax
int v11; // ebp
struct tm *v12; // rax
unsigned int v13; // ebx
int longind; // [rsp+Ch] [rbp-14Ch] BYREF
struct option longopts; // [rsp+10h] [rbp-148h] BYREF
const char *v16; // [rsp+30h] [rbp-128h]
int v17; // [rsp+38h] [rbp-120h]
int *v18; // [rsp+40h] [rbp-118h]
int v19; // [rsp+48h] [rbp-110h]
const char *v20; // [rsp+50h] [rbp-108h]
int v21; // [rsp+58h] [rbp-100h]
__int64 v22; // [rsp+60h] [rbp-F8h]
int v23; // [rsp+68h] [rbp-F0h]
__int64 v24; // [rsp+70h] [rbp-E8h]
int v25; // [rsp+78h] [rbp-E0h]
__int64 v26; // [rsp+80h] [rbp-D8h]
int v27; // [rsp+88h] [rbp-D0h]
struct sigaction v28; // [rsp+90h] [rbp-C8h] BYREF
int v29[11]; // [rsp+12Ch] [rbp-2Ch] BYREF

v29[0] = 1;
longopts.name = "no-totally-unnecessary-brightness-flicker";
longopts.has_arg = 0;
longopts.flag = v29;
longopts.val = 0;
v16 = "disable-totally-unnecessary-brightness-flicker";
v17 = 0;
v18 = v29;
v19 = 0;
v20 = "help";
v21 = 0;
v22 = 0LL;
v23 = 0;
v24 = 0LL;
v25 = 0;
v26 = 0LL;
v27 = 0;
while ( 1 )
{
longind = 0;
v3 = getopt_long(argc, (char *const *)argv, "s", &longopts, &longind);
if ( v3 == -1 )
break;
if ( v3 == 63 )
exit(1);
if ( v3 == 115 )
{
v29[0] = 0;
}
else if ( v3 )
{
printf("??? getopt returned character code 0x%x ???\n", v3);
}
else if ( longind == 2 )
{
func1(*argv);
}
}
setbuf(stdout, 0LL);
puts("LED Display: clock program");
puts("Written by Dave Ingram");
puts("-----------------------------------------------");
puts(" http://www.dmi.me.uk/code/linux/leddisplay/ \n");
if ( (unsigned int)ldisplay_init() )
{
fwrite("\x1B[1;31mDevice failed to initialise!\x1B[0m\n", 1uLL, 0x28uLL, stderr);
}
else
{
v4 = &v28.sa_mask;
for ( i = 36LL; i; --i )
{
LODWORD(v4->__val[0]) = 0;
v4 = (__sigset_t *)((char *)v4 + 4);
}
v28.sa_handler = (__sighandler_t)sighandler;
sigaction(1, &v28, 0LL);
sigaction(2, &v28, 0LL);
v6 = ldisplay_reset();
v7 = v6;
if ( !v6 )
{
ldisplay_setBrightness(0LL);
LABEL_32:
longopts.name = (const char *)time(0LL);
v12 = localtime((const time_t *)&longopts);
v13 = v12->tm_min + 100 * v12->tm_hour;
v11 = 5;
while ( !v29[0] || v13 == v7 )
{
v9 = v13;
v10 = ldisplay_showTime(v13, 0LL);
if ( v10 )
goto LABEL_30;
usleep(0x186A0u);
if ( v13 != v7 )
goto LABEL_29;
LABEL_22:
v10 = ldisplay_showTime(v9, 0LL);
if ( v10 )
goto LABEL_30;
usleep(0x493E0u);
if ( !--v11 )
{
v7 = v13;
goto LABEL_32;
}
v7 = v13;
}
ldisplay_setBrightness(2LL);
v9 = v13;
v10 = ldisplay_showTime(v13, 0LL);
if ( v10 )
{
LABEL_30:
fprintf(stderr, "\x1B[1;31mDevice failed to respond: %d\x1B[0m\n", v10);
ldisplay_cleanup();
return 1;
}
usleep(0x186A0u);
LABEL_29:
ldisplay_setBrightness(0LL);
goto LABEL_22;
}
fprintf(stderr, "\x1B[1;31mDevice failed to reset: %d\x1B[0m\n", v6);
ldisplay_cleanup();
}
return 1;
}

original_code:
int main(int argc, char *argv[]) {
  int opt_flicker = 1;
  int ret;
  {
    struct option long_options[] = {
      { "no-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "disable-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "help", 0, 0, 0},
      { 0, 0, 0, 0 }
    };
    while (1) {
      int opt_index=0;
      int c = getopt_long(argc, argv, "s", long_options, &opt_index);
      if (c == -1)
        break;
      switch (c) {
        case 0:
          if (opt_index==2) {
            usage(argv[0]);
          }
          break;
        case 's':
          opt_flicker=0;
          break;
        case '?':
          exit(1);
        default:
          printf("??? getopt returned character code 0x%x ???\n", c);
      }
    }
  }
 setbuf(stdout,0);
  printf("LED Display: clock program\n");
  printf("Written by Dave Ingram\n");
  printf("-----------------------------------------------\n");
  printf("  http://www.dmi.me.uk/code/linux/leddisplay/  \n\n");
 if (ldisplay_init() != 0) {
    fprintf(stderr, "\033[1;31mDevice failed to initialise!\033[0m\n");
    return 1;
  }
  struct sigaction sigact = {
    .__sigaction_handler.sa_handler = sighandler
  };
  sigaction(1, &sigact, ((void *)0));
  sigaction(2, &sigact, ((void *)0));
  if ((ret = ldisplay_reset())) {
    fprintf(stderr, "\033[1;31mDevice failed to reset: %d\033[0m\n", ret);
    ldisplay_cleanup();
    return 1;
  }
  ldisplay_setBrightness(0);
  int oldtime_int=0;
  while (1) {
    time_t t = time(((void *)0));
    struct tm *curTime = localtime(&t);
    int curtime_int = (100*curTime->tm_hour) + curTime->tm_min;
    int i=0;
    for (i=0; i<5; ++i) {
      if (opt_flicker && oldtime_int!=curtime_int)
        ldisplay_setBrightness(2);
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(100000);
      if (oldtime_int!=curtime_int) {
        oldtime_int = curtime_int;
        ldisplay_setBrightness(0);
      }
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(300000);
    }
  }
  ldisplay_reset();
 ldisplay_cleanup();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int opt_flicker = 1;
  int ret;
  {
    struct option long_options[] = {
      { "no-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "disable-totally-unnecessary-brightness-flicker", 0, &opt_flicker, 0},
      { "help", 0, 0, 0},
      { 0, 0, 0, 0 }
    };
    while (1) {
      int opt_index = 0;
      int c = getopt_long(argc, argv, "s", long_options, &opt_index);
      if (c == -1)
        break;
      switch (c) {
        case 0:
          if (opt_index==2) {
            usage(argv[0]);
          }
          break;
        case 's':
          opt_flicker=0;
          break;
        case '?':
          exit(1);
        default:
          printf("??? getopt returned character code 0x%x ???\n", c);
      }
    }
  }
 setbuf(stdout,0);
  printf("LED Display: clock program\n");
  printf("Written by Dave Ingram\n");
  printf("-----------------------------------------------\n");
  printf("  http://www.dmi.me.uk/code/linux/leddisplay/  \n\n");
 if (ldisplay_init() != 0) {
    fprintf(stderr, "\033[1;31mDevice failed to initialise!\033[0m\n");
    return 1;
  }
  struct sigaction sigact = {
    .__sigaction_handler.sa_handler = sighandler
  };
  sigaction(1, &sigact, ((void *)0));
  sigaction(2, &sigact, ((void *)0));
  if ((ret = ldisplay_reset())) {
    fprintf(stderr, "\033[1;31mDevice failed to reset: %d\033[0m\n", ret);
    ldisplay_cleanup();
    return 1;
  }
  ldisplay_setBrightness(0);
  int oldtime_int = 0;
  while (1) {
    long t = time(((void *)0));
    struct tm *curTime = localtime(&t);
    int curtime_int = (100*curTime->tm_hour) + curTime->tm_min;
    int i = 0;
    for (i=0; i<5; ++i) {
      if (opt_flicker && oldtime_int!=curtime_int)
        ldisplay_setBrightness(2);
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(100000);
      if (oldtime_int!=curtime_int) {
        oldtime_int = curtime_int;
        ldisplay_setBrightness(0);
      }
      ret = ldisplay_showTime(curtime_int, 0);
      if (ret) {
        fprintf(stderr, "\033[1;31mDevice failed to respond: %d\033[0m\n", ret);
        ldisplay_cleanup();
        return 1;
      }
      usleep(300000);
    }
  }
  ldisplay_reset();
 ldisplay_cleanup();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

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

variable_types: c int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types: i int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: ricpelo/elcirculo

decompiled_code:
const char *__fastcall get_image_dim(_BYTE *a1, int a2, int a3)
{
const char *result; // rax

if ( a3 == 2 )
return get_jpeg_dim(a1, a2);
result = "(unknown)";
if ( a3 == 1 )
result = get_png_dim((__int64)a1, a2);
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(_BYTE *a1, int a2, int a3)
{
const char *result; // rax

if ( a3 == 2 )
return func2(a1, a2);
result = "(unknown)";
if ( a3 == 1 )
result = func3((__int64)a1, a2);
return result;
}

original_code:
static char *get_image_dim(void *img, int32 extent, int fmt)
{
 if (fmt==2) return get_jpeg_dim(img,extent);
 else if (fmt==1) return get_png_dim(img, extent);
 return "(unknown)";
}

canonical_original_code:
static char *get_image_dim(void *img, int extent, int fmt)
{
 if (fmt==2) return get_jpeg_dim(img,extent);
 else if (fmt==1) return get_png_dim(img, extent);
 return "(unknown)";
}

variable_types:
img
void *

variable_types: extent int

variable_types: fmt int


decompiled_code:
char *get_biblio()
{
int v0; // eax
char *v1; // r13
unsigned int v2; // ebp
char *v3; // rbx
char *v4; // rax
const char *v5; // rbp
char *v6; // rax
char *v7; // rax
char *v8; // r14
const char *v9; // r13
char *v10; // rax
char *v11; // r12
char v12; // al
char *v13; // rdx
char *v14; // rax
char *v15; // r14
const char *v16; // r12
char *v17; // rax
char *v18; // rbp
char v19; // al
char *v20; // rdx
size_t v22; // rax

v0 = babel_treaty(261LL, 0LL, 0LL);
v1 = "No bibliographic data";
if ( v0 > 0 )
{
v2 = v0;
v3 = (char *)my_malloc((unsigned int)v0, "Metadata buffer");
if ( (int)babel_treaty(777LL, v3, v2) >= 0 )
{
v4 = strstr(v3, "<bibliographic>");
v5 = v4;
if ( v4 )
{
v6 = strstr(v4, "</bibliographic>");
if ( v6 )
*v6 = 0;
v7 = strstr(v5, "<title>");
v8 = v7;
v1 = "No bibliographic data";
if ( v7 )
{
v9 = v7 + 7;
v10 = strstr(v7 + 7, "</title>");
v11 = v10;
if ( v10 )
{
*v10 = 0;
v12 = v8[7];
if ( v12 )
{
v13 = (char *)v9;
do
{
if ( (unsigned __int8)(v12 - 32) > 0x5Eu )
*v13 = 95;
v12 = *++v13;
}
while ( *v13 );
}
sprintf(buffer_0, "\"%s\" ", v9);
*v11 = 60;
v1 = buffer_0;
}
else
{
strcpy(buffer_0, "<no title found> ");
v1 = "No bibliographic data";
}
}
v14 = strstr(v5, "<author>");
v15 = v14;
if ( v14 )
{
v16 = v14 + 8;
v17 = strstr(v14 + 8, "</author>");
v18 = v17;
if ( v17 )
{
*v17 = 0;
v19 = v15[8];
if ( v19 )
{
v20 = (char *)v16;
do
{
if ( (unsigned __int8)(v19 - 32) > 0x5Eu )
*v20 = 95;
v19 = *++v20;
}
while ( *v20 );
}
*(_DWORD *)&buffer_0[strlen(buffer_0)] = 2128226;
strcat(buffer_0, v16);
*v18 = 60;
v1 = buffer_0;
}
else
{
v22 = strlen(buffer_0);
*(_QWORD *)&buffer_0[v22] = 0x68747561206F6E3CLL;
*(__int64 *)((char *)&qword_D08 + v22) = 0x646E756F6620726FLL;
*(__int16 *)((char *)&word_D10 + v22) = 62;
}
}
}
free(v3);
}
}
return v1;
}

canonical_decompiled_code:
char *func6()
{
int v0; // eax
char *v1; // r13
unsigned int v2; // ebp
char *v3; // rbx
char *v4; // rax
const char *v5; // rbp
char *v6; // rax
char *v7; // rax
char *v8; // r14
const char *v9; // r13
char *v10; // rax
char *v11; // r12
char v12; // al
char *v13; // rdx
char *v14; // rax
char *v15; // r14
const char *v16; // r12
char *v17; // rax
char *v18; // rbp
char v19; // al
char *v20; // rdx
size_t v22; // rax

v0 = babel_treaty(261LL, 0LL, 0LL);
v1 = "No bibliographic data";
if ( v0 > 0 )
{
v2 = v0;
v3 = (char *)my_malloc((unsigned int)v0, "Metadata buffer");
if ( (int)babel_treaty(777LL, v3, v2) >= 0 )
{
v4 = strstr(v3, "<bibliographic>");
v5 = v4;
if ( v4 )
{
v6 = strstr(v4, "</bibliographic>");
if ( v6 )
*v6 = 0;
v7 = strstr(v5, "<title>");
v8 = v7;
v1 = "No bibliographic data";
if ( v7 )
{
v9 = v7 + 7;
v10 = strstr(v7 + 7, "</title>");
v11 = v10;
if ( v10 )
{
*v10 = 0;
v12 = v8[7];
if ( v12 )
{
v13 = (char *)v9;
do
{
if ( (unsigned __int8)(v12 - 32) > 0x5Eu )
*v13 = 95;
v12 = *++v13;
}
while ( *v13 );
}
sprintf(buffer_0, "\"%s\" ", v9);
*v11 = 60;
v1 = buffer_0;
}
else
{
strcpy(buffer_0, "<no title found> ");
v1 = "No bibliographic data";
}
}
v14 = strstr(v5, "<author>");
v15 = v14;
if ( v14 )
{
v16 = v14 + 8;
v17 = strstr(v14 + 8, "</author>");
v18 = v17;
if ( v17 )
{
*v17 = 0;
v19 = v15[8];
if ( v19 )
{
v20 = (char *)v16;
do
{
if ( (unsigned __int8)(v19 - 32) > 0x5Eu )
*v20 = 95;
v19 = *++v20;
}
while ( *v20 );
}
*(_DWORD *)&buffer_0[strlen(buffer_0)] = 2128226;
strcat(buffer_0, v16);
*v18 = 60;
v1 = buffer_0;
}
else
{
v22 = strlen(buffer_0);
*(_QWORD *)&buffer_0[v22] = 0x68747561206F6E3CLL;
*(__int64 *)((char *)&qword_D08 + v22) = 0x646E756F6620726FLL;
*(__int16 *)((char *)&word_D10 + v22) = 62;
}
}
}
free(v3);
}
}
return v1;
}

original_code:
static char *get_biblio(void)
{
 int32 i;
 char *md;
 char *bib="No bibliographic data";
 char *bibb; char *bibe;
 char *t;
 static char buffer[512];
 i=babel_treaty(0x105,((void *)0),0);
 if (i<=0) return bib;
 md=(char *) my_malloc(i,"Metadata buffer");
 if (babel_treaty(0x309,md,i)<0) return bib;
 bibb=strstr(md,"<bibliographic>");
 if (!bibb) { free(md); return bib; }
 bibe=strstr(bibb,"</bibliographic>");
 if (bibe) *bibe=0;
 t=strstr(bibb,"<title>");
 if (t)
 {
  t+=7;
  bibe=strstr(t,"</title>");
  if (bibe)
  {
    *bibe=0;
    bib=buffer;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    sprintf(buffer, "\"%s\" ",t);
    *bibe='<';
  }
  else strcpy(buffer,"<no title found> ");
 }
 t=strstr(bibb,"<author>");
 if (t)
 {
  t+=8;
  bibe=strstr(t,"</author>");
  if (bibe)
  {
    bib=buffer;
    *bibe=0;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    strcat(buffer, "by ");
    strcat(buffer, t);
    *bibe='<';
  }
  else strcat(buffer, "<no author found>");
 }
 free(md);
 return bib;
}

canonical_original_code:
static char *get_biblio()
{
 int i;
 char *md;
 char *bib = "No bibliographic data";
 char *bibb; char *bibe;
 char *t;
 static char buffer[512];
 i=babel_treaty(0x105,((void *)0),0);
 if (i<=0) return bib;
 md=(char *) my_malloc(i,"Metadata buffer");
 if (babel_treaty(0x309,md,i)<0) return bib;
 bibb=strstr(md,"<bibliographic>");
 if (!bibb) { free(md); return bib; }
 bibe=strstr(bibb,"</bibliographic>");
 if (bibe) *bibe=0;
 t=strstr(bibb,"<title>");
 if (t)
 {
  t+=7;
  bibe=strstr(t,"</title>");
  if (bibe)
  {
    *bibe=0;
    bib=buffer;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    sprintf(buffer, "\"%s\" ",t);
    *bibe='<';
  }
  else strcpy(buffer,"<no title found> ");
 }
 t=strstr(bibb,"<author>");
 if (t)
 {
  t+=8;
  bibe=strstr(t,"</author>");
  if (bibe)
  {
    bib=buffer;
    *bibe=0;
    for(i=0;t[i];i++) if (t[i]<0x20 || t[i]>0x7e) t[i]='_';
    strcat(buffer, "by ");
    strcat(buffer, t);
    *bibe='<';
  }
  else strcat(buffer, "<no author found>");
 }
 free(md);
 return bib;
}

variable_types:
md
char *

variable_types:
bib
char *

variable_types:
bibb
char *

variable_types:
bibe
char *

variable_types:
t
char *

variable_types:
buffer
char [512]


decompiled_code:
size_t babel_story_ifid()
{
int v0; // ebx
size_t result; // rax
const char *i; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = babel_treaty(776LL, v3, 512LL);
result = (size_t)strtok(v3, ",");
for ( i = (const char *)result; result; i = (const char *)result )
{
printf("IFID: %s\n", i);
result = (size_t)strtok(0LL, ",");
}
if ( !v0 )
result = fwrite(
"Unable to create an IFID (A serious problem occurred while loading the file).\n",
1uLL,
0x4EuLL,
stderr);
return result;
}

canonical_decompiled_code:
size_t func7()
{
int v0; // ebx
size_t result; // rax
const char *i; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

v0 = babel_treaty(776LL, v3, 512LL);
result = (size_t)strtok(v3, ",");
for ( i = (const char *)result; result; i = (const char *)result )
{
printf("IFID: %s\n", i);
result = (size_t)strtok(0LL, ",");
}
if ( !v0 )
result = fwrite(
"Unable to create an IFID (A serious problem occurred while loading the file).\n",
1uLL,
0x4EuLL,
stderr);
return result;
}

original_code:
void babel_story_ifid()
{
  char buffer[512];
  char *ep;
  int i;
  i=babel_treaty(0x308,buffer,512);
  ep=strtok(buffer, ",");
  while(ep)
  {
   printf("IFID: %s\n",ep);
   ep=strtok(((void *)0),",");
  }
  if (!i)
    fprintf(stderr,"Unable to create an IFID (A serious problem occurred while loading the file).\n");
}

variable_types:
ep
char *


decompiled_code:
int babel_story_format()
{
const char *v0; // rbx
int result; // eax

v0 = (const char *)babel_get_format();
if ( !v0 )
v0 = "unknown";
if ( (unsigned int)babel_get_authoritative() )
result = printf("Format: %s\n", v0);
else
result = printf("Format: %s (non-authoritative)\n", v0);
return result;
}

canonical_decompiled_code:
int func8()
{
const char *v0; // rbx
int result; // eax

v0 = (const char *)babel_get_format();
if ( !v0 )
v0 = "unknown";
if ( (unsigned int)babel_get_authoritative() )
result = printf("Format: %s\n", v0);
else
result = printf("Format: %s (non-authoritative)\n", v0);
return result;
}

original_code:
void babel_story_format()
{
  char *b;
  b=babel_get_format();
  if (!b) b="unknown";
  if (!babel_get_authoritative())
   printf("Format: %s (non-authoritative)\n",b);
  else printf("Format: %s\n",b);
}

variable_types:
b
char *


decompiled_code:
void babel_story_ifiction()
{
deep_babel_ifiction(1);
}

canonical_decompiled_code:
void func9()
{
func1(1);
}


decompiled_code:
void babel_story_cover()
{
deep_babel_cover(1);
}

canonical_decompiled_code:
void func10()
{
func5(1);
}


decompiled_code:
void babel_story_fish()
{
deep_babel_ifiction(0);
deep_babel_cover(0);
}

canonical_decompiled_code:
void func11()
{
func1(0);
func5(0);
}


decompiled_code:
int babel_story_identify()
{
char *v0; // rdi
const char *v1; // rbx
unsigned int v2; // ebp
int v3; // er12
int v4; // eax
int v5; // er13
const char *v6; // rcx
_BYTE *v8; // r14
int v9; // eax
const char *v10; // r12
const char *v11; // rcx
char v12[552]; // [rsp+0h] [rbp-228h] BYREF

v0 = get_biblio();
puts(v0);
babel_story_ifid();
v1 = (const char *)babel_get_format();
if ( !v1 )
v1 = "unknown";
v2 = (int)babel_get_length() / 1024;
v3 = babel_treaty(262LL, 0LL, 0LL);
v4 = babel_treaty(263LL, 0LL, 0LL);
if ( v3 > 0 && (v5 = v4, v4 > 0) )
{
v8 = (_BYTE *)my_malloc((unsigned int)v3, "Image buffer");
v9 = babel_treaty(778LL, v8, (unsigned int)v3);
v6 = "no cover";
if ( v9 >= 0 )
{
v10 = get_image_dim(v8, v3, v5);
v10[strlen(v10 + 1)] = 0;
v11 = "jpeg";
if ( v5 != 2 )
{
v11 = "png";
if ( v5 != 1 )
v11 = "unknown format";
}
sprintf(v12, "cover %s %s", v10 + 1, v11);
v6 = v12;
}
}
else
{
v6 = "no cover";
}
return printf("%s, %dk, %s\n", v1, v2, v6);
}

canonical_decompiled_code:
int func12()
{
char *v0; // rdi
const char *v1; // rbx
unsigned int v2; // ebp
int v3; // er12
int v4; // eax
int v5; // er13
const char *v6; // rcx
_BYTE *v8; // r14
int v9; // eax
const char *v10; // r12
const char *v11; // rcx
char v12[552]; // [rsp+0h] [rbp-228h] BYREF

v0 = func6();
puts(v0);
func7();
v1 = (const char *)babel_get_format();
if ( !v1 )
v1 = "unknown";
v2 = (int)babel_get_length() / 1024;
v3 = babel_treaty(262LL, 0LL, 0LL);
v4 = babel_treaty(263LL, 0LL, 0LL);
if ( v3 > 0 && (v5 = v4, v4 > 0) )
{
v8 = (_BYTE *)my_malloc((unsigned int)v3, "Image buffer");
v9 = babel_treaty(778LL, v8, (unsigned int)v3);
v6 = "no cover";
if ( v9 >= 0 )
{
v10 = func4(v8, v3, v5);
v10[strlen(v10 + 1)] = 0;
v11 = "jpeg";
if ( v5 != 2 )
{
v11 = "png";
if ( v5 != 1 )
v11 = "unknown format";
}
sprintf(v12, "cover %s %s", v10 + 1, v11);
v6 = v12;
}
}
else
{
v6 = "no cover";
}
return printf("%s, %dk, %s\n", v1, v2, v6);
}

original_code:
void babel_story_identify()
{
 int32 i, j, l;
 char *b, *cf, *dim;
 char buffer[512];
 printf("%s\n",get_biblio());
 babel_story_ifid();
 b=babel_get_format();
 if (!b) b="unknown";
 l=babel_get_length() / 1024;
 i=babel_treaty(0x106,((void *)0),0);
 j=babel_treaty(0x107,((void *)0),0);
 if (i<=0 || j<=0)
 {
  cf="no cover";
 }
 else
 {
  char *md=my_malloc(i,"Image buffer");
  if (babel_treaty(0x30A,md,i)<0)
  {
   cf="no cover";
  }
  else
  {
   dim=get_image_dim(md,i,j)+1;
   dim[strlen(dim)-1]=0;
   if (j==2) cf="jpeg";
   else if (j==1) cf="png";
   else cf="unknown format";
   sprintf(buffer,"cover %s %s",dim,cf);
   cf=buffer;
  }
 }
 printf("%s, %dk, %s\n",b, l,cf);
}

canonical_original_code:
void babel_story_identify()
{
 int i; int j; int l;
 char *b; char *cf; char *dim;
 char buffer[512];
 printf("%s\n",get_biblio());
 babel_story_ifid();
 b=babel_get_format();
 if (!b) b="unknown";
 l=babel_get_length() / 1024;
 i=babel_treaty(0x106,((void *)0),0);
 j=babel_treaty(0x107,((void *)0),0);
 if (i<=0 || j<=0)
 {
  cf="no cover";
 }
 else
 {
  char *md = my_malloc(i,"Image buffer");
  if (babel_treaty(0x30A,md,i)<0)
  {
   cf="no cover";
  }
  else
  {
   dim=get_image_dim(md,i,j)+1;
   dim[strlen(dim)-1]=0;
   if (j==2) cf="jpeg";
   else if (j==1) cf="png";
   else cf="unknown format";
   sprintf(buffer,"cover %s %s",dim,cf);
   cf=buffer;
  }
 }
 printf("%s, %dk, %s\n",b, l,cf);
}

variable_types: j int

variable_types: l int

variable_types:
cf
char *

variable_types:
dim
char *


decompiled_code:
void babel_story_meta()
{
deep_babel_ifiction(2);
}

canonical_decompiled_code:
void func13()
{
func1(2);
}


decompiled_code:
int babel_story_unblorb()
{
deep_babel_ifiction(1);
deep_babel_cover(1);
return babel_story_story();
}

canonical_decompiled_code:
int func15()
{
func1(1);
func5(1);
return func14();
}

original_code:
void babel_story_unblorb()
{
 deep_babel_ifiction(1);
 deep_babel_cover(1);
 babel_story_story();
}


decompiled_code:
__int64 __fastcall read_int(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

original_code:
static int32 read_int(unsigned char *mem)
{
  int32 i4 = mem[0],
                    i3 = mem[1],
                    i2 = mem[2],
                    i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

canonical_original_code:
static int read_int(unsigned char *mem)
{
  int i4 = mem[0]; int i3 = mem[1]; int i2 = mem[2]; int i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

variable_types:
mem
unsigned char *


decompiled_code:
const char *__fastcall get_jpeg_dim(_BYTE *a1, int a2)
{
unsigned __int64 v2; // rdx
unsigned __int8 *v3; // rdi
unsigned __int8 *v4; // rax
const char *v5; // rcx
unsigned __int8 *v7; // rdi
unsigned __int8 v8; // si

if ( *a1 != 0xFF || a1[1] != 0xD8 )
return "(invalid)";
v2 = (unsigned __int64)&a1[a2];
v3 = a1 + 2;
while ( 2 )
{
if ( (unsigned __int64)v3 > v2 )
return "(invalid)";
v4 = v3 + 1;
if ( *v3 == 0xFF )
goto LABEL_11;
if ( v2 < (unsigned __int64)v4 )
return "(invalid)";
while ( *v4++ != 0xFF )
{
if ( v4 == (unsigned __int8 *)(v2 + 1) )
return "(invalid)";
}
do
{
LABEL_11:
v7 = v4++;
v8 = *(v4 - 1);
if ( v2 < (unsigned __int64)v4 )
return "(invalid 4)";
}
while ( v8 == 255 );
if ( (v8 & 0xF0) != 0xC0 || (v8 & 0xF7) == 0xC4 || v8 == 200 )
{
if ( (unsigned int)v8 - 216 <= 1 )
return "(invalid)";
if ( v2 < (unsigned __int64)(v7 + 2) )
return "(invalid)";
v3 = &v4[(*v4 << 8) | v4[1]];
if ( v2 >= (unsigned __int64)v3 )
continue;
return "(invalid)";
}
break;
}
if ( v2 < (unsigned __int64)(v7 + 4) )
return "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 4) )
return "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 5) )
return "(invalid)";
v5 = "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 6) )
return v5;
sprintf(buffer_2, "(%dx%d)", (v4[5] << 8) | (unsigned int)v4[6], (v4[3] << 8) | (unsigned int)v4[4]);
return buffer_2;
}

canonical_decompiled_code:
const char *__fastcall func2(_BYTE *a1, int a2)
{
unsigned __int64 v2; // rdx
unsigned __int8 *v3; // rdi
unsigned __int8 *v4; // rax
const char *v5; // rcx
unsigned __int8 *v7; // rdi
unsigned __int8 v8; // si

if ( *a1 != 0xFF || a1[1] != 0xD8 )
return "(invalid)";
v2 = (unsigned __int64)&a1[a2];
v3 = a1 + 2;
while ( 2 )
{
if ( (unsigned __int64)v3 > v2 )
return "(invalid)";
v4 = v3 + 1;
if ( *v3 == 0xFF )
goto LABEL_11;
if ( v2 < (unsigned __int64)v4 )
return "(invalid)";
while ( *v4++ != 0xFF )
{
if ( v4 == (unsigned __int8 *)(v2 + 1) )
return "(invalid)";
}
do
{
LABEL_11:
v7 = v4++;
v8 = *(v4 - 1);
if ( v2 < (unsigned __int64)v4 )
return "(invalid 4)";
}
while ( v8 == 255 );
if ( (v8 & 0xF0) != 0xC0 || (v8 & 0xF7) == 0xC4 || v8 == 200 )
{
if ( (unsigned int)v8 - 216 <= 1 )
return "(invalid)";
if ( v2 < (unsigned __int64)(v7 + 2) )
return "(invalid)";
v3 = &v4[(*v4 << 8) | v4[1]];
if ( v2 >= (unsigned __int64)v3 )
continue;
return "(invalid)";
}
break;
}
if ( v2 < (unsigned __int64)(v7 + 4) )
return "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 4) )
return "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 5) )
return "(invalid)";
v5 = "(invalid)";
if ( v2 < (unsigned __int64)(v4 + 6) )
return v5;
sprintf(buffer_2, "(%dx%d)", (v4[5] << 8) | (unsigned int)v4[6], (v4[3] << 8) | (unsigned int)v4[4]);
return buffer_2;
}

original_code:
static char *get_jpeg_dim(void *img, int32 extent)
{
  unsigned char *dp=(unsigned char *) img;
  unsigned char *ep=dp+extent;
  static char buffer[256];
  unsigned int t1, t2, w, h;
  t1=*(dp++);
  t2=*(dp++);
  if (t1!=0xff || t2!=0xD8 )
  {
   return "(invalid)";
  }
  while(1)
  {
   if (dp>ep) return "(invalid)";
   for(t1=*(dp++);t1!=0xff;t1=*(dp++)) if (dp>ep) return "(invalid)";
   do { t1=*(dp++); if (dp>ep) return "(invalid 4)";} while (t1 == 0xff);
   if ((t1 & 0xF0) == 0xC0 && !(t1==0xC4 || t1==0xC8 || t1==0xCC))
   {
    dp+=3;
    if (dp>ep) return "(invalid)";
    h=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    h|=*(dp++);
    if (dp>ep) return "(invalid)";
    w=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    w|=*(dp);
    sprintf(buffer, "(%dx%d)",w,h);
    return buffer;
   }
   else if (t1==0xD8 || t1==0xD9)
    break;
   else
   { int l;
    if (dp>ep) return "(invalid)";
     l=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
     l|= *(dp++);
     l-=2;
     dp+=l;
     if (dp>ep) return "(invalid)";
    }
   }
  return "(invalid)";
}

canonical_original_code:
static char *get_jpeg_dim(void *img, int extent)
{
  unsigned char *dp = (unsigned char *) img;
  unsigned char *ep = dp+extent;
  static char buffer[256];
  unsigned int t1; unsigned int t2; unsigned int w; unsigned int h;
  t1=*(dp++);
  t2=*(dp++);
  if (t1!=0xff || t2!=0xD8 )
  {
   return "(invalid)";
  }
  while(1)
  {
   if (dp>ep) return "(invalid)";
   for(t1=*(dp++);t1!=0xff;t1=*(dp++)) if (dp>ep) return "(invalid)";
   do { t1=*(dp++); if (dp>ep) return "(invalid 4)";} while (t1 == 0xff);
   if ((t1 & 0xF0) == 0xC0 && !(t1==0xC4 || t1==0xC8 || t1==0xCC))
   {
    dp+=3;
    if (dp>ep) return "(invalid)";
    h=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    h|=*(dp++);
    if (dp>ep) return "(invalid)";
    w=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    w|=*(dp);
    sprintf(buffer, "(%dx%d)",w,h);
    return buffer;
   }
   else if (t1==0xD8 || t1==0xD9)
    break;
   else
   { int l;
    if (dp>ep) return "(invalid)";
     l=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
     l|= *(dp++);
     l-=2;
     dp+=l;
     if (dp>ep) return "(invalid)";
    }
   }
  return "(invalid)";
}

variable_types:
dp
unsigned char *

variable_types:
ep
unsigned char *

variable_types:
buffer
char [256]

variable_types:
w
unsigned int

variable_types:
h
unsigned int


canonical_original_code:
static char *get_png_dim(void *img, int extent)
{
 unsigned char *dp = (unsigned char *)img;
 static char buffer[256];
 int w; int h;
 if (extent<33 ||
 !(dp[0]==137 && dp[1]==80 && dp[2]==78 && dp[3]==71 &&
        dp[4]==13 && dp[5] == 10 && dp[6] == 26 && dp[7]==10)||
 !(dp[12]=='I' && dp[13]=='H' && dp[14]=='D' && dp[15]=='R'))
 return "(invalid)";
 w=read_int(dp+16);
 h=read_int(dp+20);
 sprintf(buffer,"(%dx%d)",w,h);
 return buffer;
}

variable_types:
img
void *

variable_types: extent int

variable_types: w int

variable_types: h int


decompiled_code:
void __fastcall magscrolls_treaty(int a1, const void *a2, int a3, char *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 515 )
{
if ( a5 > 4 )
strncpy(a4, byte_616, a5);
}
else if ( a1 > 515 )
{
if ( a1 > 778 )
{
if ( a1 == 779 )
get_story_file_extension((__int64)a2, a3, a4, a5);
}
else if ( a1 <= 776 && a1 == 776 )
{
get_story_file_IFID((__int64)a2, a3, a4, a5);
}
}
else if ( a1 == 513 )
{
if ( a5 > 50 )
strcpy(a4, "http://www.if-legends.org/~msmemorial/memorial.htm");
}
else if ( a1 > 513 && a5 > 511 )
{
strncpy(a4, "magscrolls", a5 - 1);
}
}
}

original_code:
int32 magscrolls_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.if-legends.org/~msmemorial/memorial.htm")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.if-legends.org/~msmemorial/memorial.htm");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"magscrolls",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".mag")+1) < 512 ? (strlen(".mag")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".mag", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int magscrolls_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.if-legends.org/~msmemorial/memorial.htm")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.if-legends.org/~msmemorial/memorial.htm");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"magscrolls",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".mag")+1) < 512 ? (strlen(".mag")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".mag", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types: selector int

variable_types:
output
void *

variable_types: ll int

variable_types: csf int


decompiled_code:
__int64 __fastcall claim_story_file(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 41 )
result = memcmp(a1, "MaSc", 4uLL) == 0 ? 1 : -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 41 )
result = memcmp(a1, "MaSc", 4uLL) == 0 ? 1 : -1;
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 if (extent<42 ||
     memcmp(story_file,"MaSc",4)
    ) return -1;
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 if (extent<42 ||
     memcmp(story_file,"MaSc",4)
    ) return -1;
 return 1;
}


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, char *a3, int a4)
{
unsigned __int8 v5; // r13
char *v6; // rbx
int v7; // ebp
const char *v8; // rbx
__int64 result; // rax

if ( a2 <= 41 )
return 0xFFFFFFFFLL;
if ( off_360 )
{
v5 = *(_BYTE *)(a1 + 13);
v6 = (char *)&unk_344;
v7 = 0;
while ( (v5 > 2u || *((_DWORD *)v6 - 1) != v5) && memcmp(v6, (const void *)(a1 + 12), 0x14uLL) )
{
++v7;
v6 += 72;
if ( !*(_QWORD *)(v6 + 28) )
goto LABEL_10;
}
v8 = off_370[9 * v7];
if ( a4 <= (int)strlen(v8) )
{
result = 4294967293LL;
}
else
{
strcpy(a3, v8);
result = 1LL;
}
}
else
{
LABEL_10:
strcpy(a3, "MAGNETIC-");
result = 4294967292LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, char *a3, int a4)
{
unsigned __int8 v5; // r13
char *v6; // rbx
int v7; // ebp
const char *v8; // rbx
__int64 result; // rax

if ( a2 <= 41 )
return 0xFFFFFFFFLL;
if ( off_360 )
{
v5 = *(_BYTE *)(a1 + 13);
v6 = (char *)&unk_344;
v7 = 0;
while ( (v5 > 2u || *((_DWORD *)v6 - 1) != v5) && memcmp(v6, (const void *)(a1 + 12), 0x14uLL) )
{
++v7;
v6 += 72;
if ( !*(_QWORD *)(v6 + 28) )
goto LABEL_10;
}
v8 = off_370[9 * v7];
if ( a4 <= (int)strlen(v8) )
{
result = 4294967293LL;
}
else
{
strcpy(a3, v8);
result = 1LL;
}
}
else
{
LABEL_10:
strcpy(a3, "MAGNETIC-");
result = 4294967292LL;
}
return result;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int i;
 unsigned char *sf=(unsigned char *)story_file;
 if (extent < 42) return -1;
 for(i=0;manifest[i].title;i++)
  if ((sf[13]<3 && manifest[i].gv==sf[13]) || memcmp(manifest[i].header,sf+12,20)==0)
   {
    do { if (output_extent < (((int32) strlen(manifest[i].ifid)+1))) return -3; } while (0);
    strcpy(output,manifest[i].ifid);
    return 1;
   }
 strcpy(output,"MAGNETIC-");
 return -4;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int i;
 unsigned char *sf = (unsigned char *)story_file;
 if (extent < 42) return -1;
 for(i=0;manifest[i].title;i++)
  if ((sf[13]<3 && manifest[i].gv==sf[13]) || memcmp(manifest[i].header,sf+12,20)==0)
   {
    do { if (output_extent < (((int) strlen(manifest[i].ifid)+1))) return -3; } while (0);
    strcpy(output,manifest[i].ifid);
    return 1;
   }
 strcpy(output,"MAGNETIC-");
 return -4;
}

variable_types:
output
char *

variable_types: i int

variable_types:
sf
unsigned char *


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_616[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_616, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_616[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_616, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".mag"[i] && ".mag"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".mag",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".mag"[i] && ".mag"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".mag",i);
 out[i]=0;
 return strlen(out);
}

variable_types:
sf
void *

variable_types:
out
char *


decompiled_code:
__int64 __fastcall blorb_get_chunk(__int64 a1, int a2, const void *a3, _DWORD *a4, _DWORD *a5)
{
int v6; // ebx
unsigned int v7; // er14
int v8; // eax
int v10; // eax

if ( a2 - 8 <= 12 )
return 0;
v6 = 12;
while ( 1 )
{
v7 = memcmp((const void *)(a1 + v6), a3, 4uLL);
if ( !v7 )
break;
v8 = read_int((unsigned int *)(a1 + v6 + 4));
v6 += v8 - (((v8 & 1) == 0) - 1) + 8;
if ( v6 >= a2 - 8 )
return 0;
}
v10 = read_int((unsigned int *)(a1 + v6 + 4));
*a5 = v10;
if ( v10 <= a2 )
{
*a4 = v6 + 8;
v7 = 1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, const void *a3, _DWORD *a4, _DWORD *a5)
{
int v6; // ebx
unsigned int v7; // er14
int v8; // eax
int v10; // eax

if ( a2 - 8 <= 12 )
return 0;
v6 = 12;
while ( 1 )
{
v7 = memcmp((const void *)(a1 + v6), a3, 4uLL);
if ( !v7 )
break;
v8 = func0((unsigned int *)(a1 + v6 + 4));
v6 += v8 - (((v8 & 1) == 0) - 1) + 8;
if ( v6 >= a2 - 8 )
return 0;
}
v10 = func0((unsigned int *)(a1 + v6 + 4));
*a5 = v10;
if ( v10 <= a2 )
{
*a4 = v6 + 8;
v7 = 1;
}
return v7;
}

original_code:
static int32 blorb_get_chunk(void *blorb_file, int32 extent, char *id, int32 *begin, int32 *output_extent)
{
 int32 i=12, j;
 while(i<extent-8)
 {
  if (memcmp(((char *)blorb_file)+i,id,4)==0)
  {
   *output_extent=read_int((char *)blorb_file+i+4);
   if (*output_extent > extent) return 0;
   *begin=i+8;
   return 1;
  }
  j=read_int((char *)blorb_file+i+4);
  if (j%2) j++;
  i+=j+8;
 }
 return 0;
}

canonical_original_code:
static int blorb_get_chunk(void *blorb_file, int extent, char *id, int *begin, int *output_extent)
{
 int i = 12; int j;
 while(i<extent-8)
 {
  if (memcmp(((char *)blorb_file)+i,id,4)==0)
  {
   *output_extent=read_int((char *)blorb_file+i+4);
   if (*output_extent > extent) return 0;
   *begin=i+8;
   return 1;
  }
  j=read_int((char *)blorb_file+i+4);
  if (j%2) j++;
  i+=j+8;
 }
 return 0;
}

variable_types:
id
char *

variable_types:
begin
int *

variable_types: j int


decompiled_code:
__int64 __fastcall blorb_get_resource(__int64 a1, int a2, const void *a3, int a4, _DWORD *a5, _DWORD *a6)
{
__int64 result; // rax
__int64 v8; // rbx
int v9; // er12
__int64 v10; // rax
unsigned int *v11; // rbx
int v12; // ebp
__int64 v13; // r14
int v14; // eax
__int64 v18; // [rsp+28h] [rbp-50h]
int v19; // [rsp+38h] [rbp-40h] BYREF
int v20[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

result = blorb_get_chunk(a1, a2, "RIdx", &v19, v20);
if ( (_DWORD)result )
{
v8 = a1 + v19 + 4;
v18 = v8;
v9 = read_int((unsigned int *)(a1 + v19));
v20[0] = v9;
v19 = 0;
if ( v9 <= 0 )
{
result = 0LL;
}
else
{
v10 = v8;
v11 = (unsigned int *)(v8 + 4);
v12 = 1;
v13 = -4 - v10;
while ( 1 )
{
if ( !memcmp(v11 - 1, a3, 4uLL) && (unsigned int)read_int(v11) == a4 )
{
v14 = read_int((unsigned int *)((char *)v11 + v13 + v18 + 8));
v19 = v14;
*a5 = v14 + 8;
*a6 = read_int((unsigned int *)(a1 + v14 + 4));
return 1LL;
}
v19 = v12;
v11 += 3;
if ( v12 == v9 )
break;
++v12;
}
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, const void *a3, int a4, _DWORD *a5, _DWORD *a6)
{
__int64 result; // rax
__int64 v8; // rbx
int v9; // er12
__int64 v10; // rax
unsigned int *v11; // rbx
int v12; // ebp
__int64 v13; // r14
int v14; // eax
__int64 v18; // [rsp+28h] [rbp-50h]
int v19; // [rsp+38h] [rbp-40h] BYREF
int v20[15]; // [rsp+3Ch] [rbp-3Ch] BYREF

result = func2(a1, a2, "RIdx", &v19, v20);
if ( (_DWORD)result )
{
v8 = a1 + v19 + 4;
v18 = v8;
v9 = func0((unsigned int *)(a1 + v19));
v20[0] = v9;
v19 = 0;
if ( v9 <= 0 )
{
result = 0LL;
}
else
{
v10 = v8;
v11 = (unsigned int *)(v8 + 4);
v12 = 1;
v13 = -4 - v10;
while ( 1 )
{
if ( !memcmp(v11 - 1, a3, 4uLL) && (unsigned int)func0(v11) == a4 )
{
v14 = func0((unsigned int *)((char *)v11 + v13 + v18 + 8));
v19 = v14;
*a5 = v14 + 8;
*a6 = func0((unsigned int *)(a1 + v14 + 4));
return 1LL;
}
v19 = v12;
v11 += 3;
if ( v12 == v9 )
break;
++v12;
}
result = 0LL;
}
}
return result;
}

original_code:
static int32 blorb_get_resource(void *blorb_file, int32 extent, char *rid, int32 number, int32 *begin, int32 *output_extent)
{
 int32 ridx_len;
 int32 i,j;
 void *ridx;
 if (blorb_get_chunk(blorb_file, extent,"RIdx",&i,&ridx_len)==0)
  return 0;
 ridx=(char *)blorb_file+i+4;
 ridx_len=read_int((char *)blorb_file+i);
 for(i=0;i<ridx_len;i++)
 {
  if(memcmp((char *)ridx+(i*12),rid,4)==0 && read_int((char *)ridx+(i*12)+4)==number)
  {
   j=i;
   i=read_int((char *)ridx+(j*12)+8);
   *begin=i+8;
   *output_extent=read_int((char *)blorb_file+i+4);
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
static int blorb_get_resource(void *blorb_file, int extent, char *rid, int number, int *begin, int *output_extent)
{
 int ridx_len;
 int i; int j;
 void *ridx;
 if (blorb_get_chunk(blorb_file, extent,"RIdx",&i,&ridx_len)==0)
  return 0;
 ridx=(char *)blorb_file+i+4;
 ridx_len=read_int((char *)blorb_file+i);
 for(i=0;i<ridx_len;i++)
 {
  if(memcmp((char *)ridx+(i*12),rid,4)==0 && read_int((char *)ridx+(i*12)+4)==number)
  {
   j=i;
   i=read_int((char *)ridx+(j*12)+8);
   *begin=i+8;
   *output_extent=read_int((char *)blorb_file+i+4);
   return 1;
  }
 }
 return 0;
}

variable_types:
rid
char *

variable_types: number int

variable_types:
ridx
void *


decompiled_code:
__int64 __fastcall get_story_extent(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = blorb_get_resource(a1, a2, "Exec", 0, v4, &v3);
if ( (_DWORD)result )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = func4(a1, a2, "Exec", 0, v4, &v3);
if ( (_DWORD)result )
result = v3;
return result;
}

original_code:
static int32 get_story_extent(void *blorb_file, int32 extent)
{
 int32 i,j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  return j;
 }
 return 0;
}

canonical_original_code:
static int get_story_extent(void *blorb_file, int extent)
{
 int i; int j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  return j;
 }
 return 0;
}

variable_types: extent int


decompiled_code:
__int64 __fastcall blorb_get_cover(__int64 a1, int a2, _DWORD *a3, _DWORD *a4)
{
__int64 result; // rax
int v7; // eax
const void *v8; // rbx
int v9; // edx
int v10; // [rsp+8h] [rbp-30h] BYREF
int v11[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = blorb_get_chunk(a1, a2, "Fspc", v11, &v10);
if ( (_DWORD)result )
{
result = 0LL;
if ( v10 > 3 )
{
v11[0] = read_int((unsigned int *)(a1 + v11[0]));
result = blorb_get_resource(a1, a2, "Pict", v11[0], v11, &v10);
if ( (_DWORD)result )
{
v7 = v11[0];
*a3 = v11[0];
*a4 = v10;
v8 = (const void *)(a1 + v7 - 8);
v9 = memcmp(v8, "PNG ", 4uLL);
result = 1LL;
if ( v9 )
result = 2 * (unsigned int)(memcmp(v8, "JPEG", 4uLL) == 0);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2, _DWORD *a3, _DWORD *a4)
{
__int64 result; // rax
int v7; // eax
const void *v8; // rbx
int v9; // edx
int v10; // [rsp+8h] [rbp-30h] BYREF
int v11[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = func2(a1, a2, "Fspc", v11, &v10);
if ( (_DWORD)result )
{
result = 0LL;
if ( v10 > 3 )
{
v11[0] = func0((unsigned int *)(a1 + v11[0]));
result = func4(a1, a2, "Pict", v11[0], v11, &v10);
if ( (_DWORD)result )
{
v7 = v11[0];
*a3 = v11[0];
*a4 = v10;
v8 = (const void *)(a1 + v7 - 8);
v9 = memcmp(v8, "PNG ", 4uLL);
result = 1LL;
if ( v9 )
result = 2 * (unsigned int)(memcmp(v8, "JPEG", 4uLL) == 0);
}
}
}
return result;
}

original_code:
static int32 blorb_get_cover(void *blorb_file, int32 extent, int32 *begin, int32 *output_extent)
{
 int i,j;
 if (blorb_get_chunk(blorb_file,extent,"Fspc",&i,&j))
 {
  if (j<4) return 0;
  i=read_int((char *)blorb_file+i);
  if (!blorb_get_resource(blorb_file,extent,"Pict",i,&i,&j)) return 0;
  *begin=i;
  *output_extent=j;
  if (memcmp((char *)blorb_file+i-8,"PNG ",4)==0) return 1;
  else if (memcmp((char *)blorb_file+i-8,"JPEG",4)==0) return 2;
 }
 return 0;
}

canonical_original_code:
static int blorb_get_cover(void *blorb_file, int extent, int *begin, int *output_extent)
{
 int i; int j;
 if (blorb_get_chunk(blorb_file,extent,"Fspc",&i,&j))
 {
  if (j<4) return 0;
  i=read_int((char *)blorb_file+i);
  if (!blorb_get_resource(blorb_file,extent,"Pict",i,&i,&j)) return 0;
  *begin=i;
  *output_extent=j;
  if (memcmp((char *)blorb_file+i-8,"PNG ",4)==0) return 1;
  else if (memcmp((char *)blorb_file+i-8,"JPEG",4)==0) return 2;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall get_story_file(__int64 a1, int a2, void *a3, int a4)
{
unsigned int v6; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = blorb_get_resource(a1, a2, "Exec", 0, v9, &v8);
if ( v6 )
{
v6 = v8;
if ( v8 > a4 )
v6 = -3;
else
memcpy(a3, (const void *)(a1 + v9[0]), v8);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, void *a3, int a4)
{
unsigned int v6; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = func4(a1, a2, "Exec", 0, v9, &v8);
if ( v6 )
{
v6 = v8;
if ( v8 > a4 )
v6 = -3;
else
memcpy(a3, (const void *)(a1 + v9[0]), v8);
}
return v6;
}

original_code:
static int32 get_story_file(void *blorb_file, int32 extent, void *output, int32 output_extent)
{
 int32 i,j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  do { if (output_extent < (j)) return -3; } while (0);
  memcpy(output,(char *)blorb_file+i,j);
  return j;
 }
 return 0;
}

canonical_original_code:
static int get_story_file(void *blorb_file, int extent, void *output, int output_extent)
{
 int i; int j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  do { if (output_extent < (j)) return -3; } while (0);
  memcpy(output,(char *)blorb_file+i,j);
  return j;
 }
 return 0;
}


decompiled_code:
char *__fastcall blorb_chunk_for_name(char *s1)
{
char **v1; // rbx
const char *v2; // rbp
__int64 v3; // rbx
__int64 v4; // rax

v1 = TranslateExec;
v2 = "ZCOD";
while ( strcmp(s1, v1[1]) )
{
v1 += 2;
v2 = *v1;
if ( !*v1 )
{
v3 = 0LL;
while ( s1[v3] )
{
buffer_0[v3] = (*_ctype_toupper_loc())[buffer_0[v3]];
if ( ++v3 == 4 )
goto LABEL_11;
}
if ( (int)v3 <= 3 )
{
v4 = (int)v3;
do
buffer_0[v4++] = 32;
while ( (int)v4 <= 3 );
}
LABEL_11:
byte_984 = 0;
return buffer_0;
}
}
return (char *)v2;
}

canonical_decompiled_code:
char *__fastcall func14(char *s1)
{
char **v1; // rbx
const char *v2; // rbp
__int64 v3; // rbx
__int64 v4; // rax

v1 = TranslateExec;
v2 = "ZCOD";
while ( strcmp(s1, v1[1]) )
{
v1 += 2;
v2 = *v1;
if ( !*v1 )
{
v3 = 0LL;
while ( s1[v3] )
{
buffer_0[v3] = (*_ctype_toupper_loc())[buffer_0[v3]];
if ( ++v3 == 4 )
goto LABEL_11;
}
if ( (int)v3 <= 3 )
{
v4 = (int)v3;
do
buffer_0[v4++] = 32;
while ( (int)v4 <= 3 );
}
LABEL_11:
byte_984 = 0;
return buffer_0;
}
}
return (char *)v2;
}

original_code:
char *blorb_chunk_for_name(char *name)
{
 static char buffer[5];
 int j;
 for(j=0;TranslateExec[j];j+=2)
  if (strcmp(name,TranslateExec[j+1])==0) return TranslateExec[j];
 for(j=0;j<4 && name[j];j++) buffer[j]=toupper(buffer[j]);
 while(j<4) buffer[j++]=' ';
 buffer[4]=0;
 return buffer;
}

variable_types:
name
char *

variable_types:
buffer
char [5]


decompiled_code:
char *__fastcall blorb_get_story_format(__int64 a1, int a2)
{
char *result; // rax
__int64 v3; // rbx
char *v4; // rax
_DWORD v5[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = (char *)treaty_registry;
if ( !treaty_registry )
return 0LL;
v3 = (unsigned int)((_DWORD)&treaty_registry + 8);
while ( 1 )
{
((void (__fastcall *)(__int64, _QWORD, _QWORD, char *, __int64))result)(514LL, 0LL, 0LL, fn_1, 512LL);
v4 = blorb_chunk_for_name(fn_1);
if ( (unsigned int)blorb_get_chunk(a1, a2, v4, v5, v5) )
break;
v3 += 8LL;
result = *(char **)(v3 - 8);
if ( !result )
return result;
}
return fn_1;
}

canonical_decompiled_code:
char *__fastcall func15(__int64 a1, int a2)
{
char *result; // rax
__int64 v3; // rbx
char *v4; // rax
_DWORD v5[7]; // [rsp+0h] [rbp-1Ch] BYREF

result = (char *)treaty_registry;
if ( !treaty_registry )
return 0LL;
v3 = (unsigned int)((_DWORD)&treaty_registry + 8);
while ( 1 )
{
((void (__fastcall *)(__int64, _QWORD, _QWORD, char *, __int64))result)(514LL, 0LL, 0LL, fn_1, 512LL);
v4 = func14(fn_1);
if ( (unsigned int)func2(a1, a2, v4, v5, v5) )
break;
v3 += 8LL;
result = *(char **)(v3 - 8);
if ( !result )
return result;
}
return fn_1;
}

original_code:
static char *blorb_get_story_format(void *blorb_file, int32 extent)
{
 int32 i, j;
 for(j=0;treaty_registry[j];j++)
 {
  static char fn[512];
  treaty_registry[j](0x202,((void *)0),0,fn,512);
  if (blorb_get_chunk(blorb_file,extent,blorb_chunk_for_name(fn),&i, &i)) return fn;
 }
 return ((void *)0);
}

canonical_original_code:
static char *blorb_get_story_format(void *blorb_file, int extent)
{
 int i; int j;
 for(j=0;treaty_registry[j];j++)
 {
  static char fn[512];
  treaty_registry[j](0x202,((void *)0),0,fn,512);
  if (blorb_get_chunk(blorb_file,extent,blorb_chunk_for_name(fn),&i, &i)) return fn;
 }
 return ((void *)0);
}

variable_types:
fn
char [512]


decompiled_code:
__int64 __fastcall get_story_format(__int64 a1, int a2, char *a3, int a4)
{
char *v6; // rax
const char *v7; // rbx

v6 = blorb_get_story_format(a1, a2);
if ( !v6 )
return 0LL;
v7 = v6;
if ( a4 <= (int)strlen(v6) )
return 4294967293LL;
strcpy(a3, v7);
return (unsigned int)strlen(v7) + 1;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2, char *a3, int a4)
{
char *v6; // rax
const char *v7; // rbx

v6 = func15(a1, a2);
if ( !v6 )
return 0LL;
v7 = v6;
if ( a4 <= (int)strlen(v6) )
return 4294967293LL;
strcpy(a3, v7);
return (unsigned int)strlen(v7) + 1;
}

original_code:
static int32 get_story_format(void *blorb_file, int32 extent, char *output, int32 output_extent)
{
 char *o;
 o=blorb_get_story_format(blorb_file, extent);
 if (!o) return 0;
 do { if (output_extent < ((signed) strlen(o)+1)) return -3; } while (0);
 strcpy(output,o);
 return strlen(o)+1;
}

canonical_original_code:
static int get_story_format(void *blorb_file, int extent, char *output, int output_extent)
{
 char *o;
 o=blorb_get_story_format(blorb_file, extent);
 if (!o) return 0;
 do { if (output_extent < ((int) strlen(o)+1)) return -3; } while (0);
 strcpy(output,o);
 return strlen(o)+1;
}

variable_types:
o
char *