decompiled_code:
__int64 __fastcall rsh_command(_QWORD *a1)
{
int v2; // eax
int v3; // eax
int v4; // eax
unsigned int v5; // er12
__int64 result; // rax
int v7; // eax
__int64 *v8; // rbx
int v9; // eax
__int64 *v10; // rbx
int v11; // eax
__int64 *v12; // rbx
int v13; // eax
__int64 *v14; // rbx
const char *v15; // r12
const char *v16; // rdi
const char *v17; // r12
const char *v18; // rdi
unsigned int v19; // er12

clear_argv();
while ( 2 )
{
switch ( *((_DWORD *)a1 + 2) )
{
case 0:
v5 = command_state;
if ( !command_state )
return v5;
if ( (pipe_status & 6) != 0 )
pipe_status = 1;
v19 = dispatch_process(0);
clear_argv();
result = v19;
pipe_status = 0;
return result;
case 1:
v17 = (const char *)*a1;
if ( !*a1 )
goto LABEL_7;
v18 = (const char *)*a1;
if ( command_state == 1 )
{
append_to_argv((__int64)v18);
a1 += 2;
}
else
{
a1 += 2;
command = strdup(v18);
append_to_argv((__int64)v17);
command_state = 1;
}
continue;
case 2:
v15 = (const char *)*a1;
v15[strlen((const char *)*a1) - 1] = 0;
if ( *((_DWORD *)a1 + 6) == 1 )
{
symtable_add(*a1, a1[2]);
a1 += 4;
}
else
{
v16 = (const char *)*a1;
a1 += 2;
symtable_add(v16, 0LL);
}
continue;
case 0xB:
v13 = *((_DWORD *)a1 + 6);
if ( v13 == 18 )
{
v13 = *((_DWORD *)a1 + 10);
v14 = a1 + 4;
}
else
{
v14 = a1 + 2;
}
if ( !v13 )
goto LABEL_56;
if ( (unsigned int)set_stdin(*v14) == -1 )
return (unsigned int)-1;
a1 = v14 + 2;
continue;
case 0xC:
v11 = *((_DWORD *)a1 + 6);
if ( v11 == 18 )
{
v11 = *((_DWORD *)a1 + 10);
v12 = a1 + 4;
}
else
{
v12 = a1 + 2;
}
if ( !v11 )
goto LABEL_56;
if ( (unsigned int)set_stdout(*v12, 0) == -1 )
return (unsigned int)-1;
a1 = v12 + 2;
continue;
case 0xD:
v9 = *((_DWORD *)a1 + 6);
if ( v9 == 18 )
{
v9 = *((_DWORD *)a1 + 10);
v10 = a1 + 4;
}
else
{
v10 = a1 + 2;
}
if ( !v9 )
goto LABEL_56;
if ( (unsigned int)set_stderr(*v10, 0) == -1 )
return (unsigned int)-1;
a1 = v10 + 2;
continue;
case 0xE:
v7 = *((_DWORD *)a1 + 6);
if ( v7 == 18 )
{
v7 = *((_DWORD *)a1 + 10);
v8 = a1 + 4;
}
else
{
v8 = a1 + 2;
}
if ( !v7 )
goto LABEL_56;
if ( (unsigned int)set_stdout(*v8, 1) == -1 )
return (unsigned int)-1;
a1 = v8 + 2;
continue;
case 0xF:
v4 = *((_DWORD *)a1 + 6);
if ( v4 == 18 )
{
v4 = *((_DWORD *)a1 + 10);
a1 += 4;
}
else
{
a1 += 2;
}
if ( !v4 )
{
LABEL_56:
v5 = -1;
puts("Syntax error: missing redirect destination.");
return v5;
}
if ( (unsigned int)set_stderr(*a1, 1) != -1 )
{
LABEL_7:
a1 += 2;
continue;
}
return (unsigned int)-1;
case 0x10:
if ( !*((_DWORD *)a1 + 6) )
goto LABEL_58;
if ( pipe(pipe_fds) < 0 )
goto LABEL_57;
v3 = pipe_fds[1];
pipe_status |= 2u;
proc_stdout = v3;
goto LABEL_6;
case 0x11:
if ( !*((_DWORD *)a1 + 6) )
{
LABEL_58:
v5 = -1;
puts("Missing read half of pipe command.");
return v5;
}
if ( pipe(pipe_fds) < 0 )
{
LABEL_57:
v5 = -1;
perror("pipe");
return v5;
}
v2 = pipe_fds[1];
pipe_status |= 4u;
proc_stdout = v2;
LABEL_6:
dispatch_process(1u);
clear_argv();
proc_stdin = pipe_fds[0];
goto LABEL_7;
case 0x15:
if ( (pipe_status & 6) != 0 )
pipe_status = 1;
a1 += 2;
dispatch_process(1u);
clear_argv();
pipe_status = 0;
continue;
default:
goto LABEL_7;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
int v2; // eax
int v3; // eax
int v4; // eax
unsigned int v5; // er12
__int64 result; // rax
int v7; // eax
__int64 *v8; // rbx
int v9; // eax
__int64 *v10; // rbx
int v11; // eax
__int64 *v12; // rbx
int v13; // eax
__int64 *v14; // rbx
const char *v15; // r12
const char *v16; // rdi
const char *v17; // r12
const char *v18; // rdi
unsigned int v19; // er12

func0();
while ( 2 )
{
switch ( *((_DWORD *)a1 + 2) )
{
case 0:
v5 = command_state;
if ( !command_state )
return v5;
if ( (pipe_status & 6) != 0 )
pipe_status = 1;
v19 = func1(0);
func0();
result = v19;
pipe_status = 0;
return result;
case 1:
v17 = (const char *)*a1;
if ( !*a1 )
goto LABEL_7;
v18 = (const char *)*a1;
if ( command_state == 1 )
{
func2((__int64)v18);
a1 += 2;
}
else
{
a1 += 2;
command = strdup(v18);
func2((__int64)v17);
command_state = 1;
}
continue;
case 2:
v15 = (const char *)*a1;
v15[strlen((const char *)*a1) - 1] = 0;
if ( *((_DWORD *)a1 + 6) == 1 )
{
symtable_add(*a1, a1[2]);
a1 += 4;
}
else
{
v16 = (const char *)*a1;
a1 += 2;
symtable_add(v16, 0LL);
}
continue;
case 0xB:
v13 = *((_DWORD *)a1 + 6);
if ( v13 == 18 )
{
v13 = *((_DWORD *)a1 + 10);
v14 = a1 + 4;
}
else
{
v14 = a1 + 2;
}
if ( !v13 )
goto LABEL_56;
if ( (unsigned int)func3(*v14) == -1 )
return (unsigned int)-1;
a1 = v14 + 2;
continue;
case 0xC:
v11 = *((_DWORD *)a1 + 6);
if ( v11 == 18 )
{
v11 = *((_DWORD *)a1 + 10);
v12 = a1 + 4;
}
else
{
v12 = a1 + 2;
}
if ( !v11 )
goto LABEL_56;
if ( (unsigned int)func4(*v12, 0) == -1 )
return (unsigned int)-1;
a1 = v12 + 2;
continue;
case 0xD:
v9 = *((_DWORD *)a1 + 6);
if ( v9 == 18 )
{
v9 = *((_DWORD *)a1 + 10);
v10 = a1 + 4;
}
else
{
v10 = a1 + 2;
}
if ( !v9 )
goto LABEL_56;
if ( (unsigned int)func5(*v10, 0) == -1 )
return (unsigned int)-1;
a1 = v10 + 2;
continue;
case 0xE:
v7 = *((_DWORD *)a1 + 6);
if ( v7 == 18 )
{
v7 = *((_DWORD *)a1 + 10);
v8 = a1 + 4;
}
else
{
v8 = a1 + 2;
}
if ( !v7 )
goto LABEL_56;
if ( (unsigned int)func4(*v8, 1) == -1 )
return (unsigned int)-1;
a1 = v8 + 2;
continue;
case 0xF:
v4 = *((_DWORD *)a1 + 6);
if ( v4 == 18 )
{
v4 = *((_DWORD *)a1 + 10);
a1 += 4;
}
else
{
a1 += 2;
}
if ( !v4 )
{
LABEL_56:
v5 = -1;
puts("Syntax error: missing redirect destination.");
return v5;
}
if ( (unsigned int)func5(*a1, 1) != -1 )
{
LABEL_7:
a1 += 2;
continue;
}
return (unsigned int)-1;
case 0x10:
if ( !*((_DWORD *)a1 + 6) )
goto LABEL_58;
if ( pipe(pipe_fds) < 0 )
goto LABEL_57;
v3 = pipe_fds[1];
pipe_status |= 2u;
proc_stdout = v3;
goto LABEL_6;
case 0x11:
if ( !*((_DWORD *)a1 + 6) )
{
LABEL_58:
v5 = -1;
puts("Missing read half of pipe command.");
return v5;
}
if ( pipe(pipe_fds) < 0 )
{
LABEL_57:
v5 = -1;
perror("pipe");
return v5;
}
v2 = pipe_fds[1];
pipe_status |= 4u;
proc_stdout = v2;
LABEL_6:
func1(1u);
func0();
proc_stdin = pipe_fds[0];
goto LABEL_7;
case 0x15:
if ( (pipe_status & 6) != 0 )
pipe_status = 1;
a1 += 2;
func1(1u);
func0();
pipe_status = 0;
continue;
default:
goto LABEL_7;
}
}
}

original_code:
int rsh_command(struct rsh_token *seq){
  int ret;
  clear_argv();
  while ( 1 ){
    switch (seq->type){
    case 1:
      if ( ! seq->tok )
 break;
      if ( command_state == 1 ){
 append_to_argv(seq->tok);
      } else {
 command = strdup(seq->tok);
 append_to_argv(seq->tok);
 command_state = 1;
      }
      break;
    case 2:
      seq->tok[strlen(seq->tok)-1] = 0;
      if ( (seq+1)->type == 1 ){
 symtable_add(seq->tok, (seq+1)->tok);
 seq++;
      } else {
 symtable_add(seq->tok, ((void *)0));
      }
      break;
    case 11:
      seq++;
      if ( seq->type == 18 )
 seq++;
      if ( seq->type == 0 ){
 printf("Syntax error: missing redirect destination.\n");
 return -1;
      }
      if ( set_stdin(seq->tok) == -1 )
 return -1;
      break;
    case 12:
      seq++;
      if ( seq->type == 18 )
 seq++;
      if ( seq->type == 0 ){
 printf("Syntax error: missing redirect destination.\n");
 return -1;
      }
      if ( set_stdout(seq->tok, 0) == -1 )
 return -1;
      break;
    case 13:
      seq++;
      if ( seq->type == 18 )
 seq++;
      if ( seq->type == 0 ){
 printf("Syntax error: missing redirect destination.\n");
 return -1;
      }
      if ( set_stderr(seq->tok, 0) == -1 )
 return -1;
      break;
    case 14:
      seq++;
      if ( seq->type == 18 )
 seq++;
      if ( seq->type == 0 ){
 printf("Syntax error: missing redirect destination.\n");
 return -1;
      }
      if ( set_stdout(seq->tok, 1) == -1 )
 return -1;
      break;
    case 15:
      seq++;
      if ( seq->type == 18 )
 seq++;
      if ( seq->type == 0 ){
 printf("Syntax error: missing redirect destination.\n");
 return -1;
      }
      if ( set_stderr(seq->tok, 1) == -1 )
 return -1;
      break;
    case 16:
      if ( (seq+1)->type == 0 ){
 printf("Missing read half of pipe command.\n");
 return -1;
      }
      ret = pipe(pipe_fds);
      if ( ret < 0 ){
 perror("pipe");
 return -1;
      }
      proc_stdout = pipe_fds[1];
      pipe_status |= 0x02;
      dispatch_process(1);
      clear_argv();
      proc_stdin = pipe_fds[0];
      break;
    case 17:
      if ( (seq+1)->type == 0 ){
 printf("Missing read half of pipe command.\n");
 return -1;
      }
      ret = pipe(pipe_fds);
      if ( ret < 0 ){
 perror("pipe");
 return -1;
      }
      proc_stdout = pipe_fds[1];
      pipe_status |= 0x04;
      dispatch_process(1);
      clear_argv();
      proc_stdin = pipe_fds[0];
      break;
    case 21:
      if ( pipe_status & (0x02|0x04) )
 pipe_status = 0x01;
      dispatch_process(1);
      clear_argv();
      pipe_status = 0x00;
      break;
    case 0:
      if ( command_state != 0){
 if ( pipe_status & (0x02|0x04) )
   pipe_status = 0x01;
 ret = dispatch_process(0);
 clear_argv();
 pipe_status = 0x00;
 return ret;
      } else {
 return 0;
      }
    }
    seq++;
  }
  return 0;
}

variable_types: struct rsh_token { char *tok; int type; } *

variable_types: ret int


repo: imnotlistening/rsh

decompiled_code:
__int64 run_script()
{
_QWORD *v0; // rbp
unsigned int v1; // ebx
__int64 v2; // rcx
void *i; // rbp
char s[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v0 = (_QWORD *)script_argv;
symtable_add(&unk_E9, script);
if ( !*v0 )
goto LABEL_6;
v1 = 1;
do
{
v2 = v1;
++v0;
++v1;
snprintf(s, 6uLL, "%d", v2);
symtable_add(s, *(v0 - 1));
}
while ( *v0 );
for ( i = (void *)read_next_statement(); i; i = (void *)read_next_statement() )
{
exec_token_seq(i);
free(i);
LABEL_6:
;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
_QWORD *v0; // rbp
unsigned int v1; // ebx
__int64 v2; // rcx
void *i; // rbp
char s[30]; // [rsp+Ah] [rbp-1Eh] BYREF

v0 = (_QWORD *)script_argv;
symtable_add(&unk_E9, script);
if ( !*v0 )
goto LABEL_6;
v1 = 1;
do
{
v2 = v1;
++v0;
++v1;
snprintf(s, 6uLL, "%d", v2);
symtable_add(s, *(v0 - 1));
}
while ( *v0 );
for ( i = (void *)read_next_statement(); i; i = (void *)read_next_statement() )
{
exec_token_seq(i);
free(i);
LABEL_6:
;
}
return 0LL;
}

original_code:
int run_script(){
  struct rsh_token *tokens;
  char buf[6];
  char **argv = script_argv;
  int argc = 1;
  symtable_add("0", script);
  while ( *argv != ((void *)0)){
    snprintf(buf, 6, "%d", argc);
    symtable_add(buf, *argv);
    argv++;
    argc++;
  }
  while ( ( tokens = read_next_statement()) != ((void *)0) ){
    exec_token_seq(tokens);
    free(tokens);
  }
  return 0;
}

variable_types:
buf
char [6]

variable_types:
argv
char **

variable_types: argc int

return_type: int

user_defined_types: struct rsh_token { char *tok; int type; }


decompiled_code:
__int64 run_interactive()
{
__int64 v0; // rax

while ( 1 )
{
prompt_print();
v0 = read_next_statement();
if ( !v0 )
break;
exec_token_seq(v0);
}
return 0LL;
}

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

while ( 1 )
{
prompt_print();
v0 = read_next_statement();
if ( !v0 )
break;
exec_token_seq(v0);
}
return 0LL;
}

original_code:
int run_interactive(){
  struct rsh_token *tokens;
  do {
    prompt_print();
    tokens = read_next_statement();
    if ( ! tokens )
      break;
    exec_token_seq(tokens);
  } while (1);
  return 0;
}


decompiled_code:
__int64 sigchld_handler()
{
return check_processes();
}

canonical_decompiled_code:
__int64 func2()
{
return check_processes();
}

original_code:
void sigchld_handler(int sig){
  check_processes();
}

variable_types: sig int

return_type: void


decompiled_code:
__uid_t prompt_init()
{
__uid_t result; // eax

symtable_add("PROMPT", "[\\u@\\h \\W]$ ");
result = geteuid();
euid = result;
return result;
}

canonical_decompiled_code:
__uid_t func0()
{
__uid_t result; // eax

symtable_add("PROMPT", "[\\u@\\h \\W]$ ");
result = geteuid();
euid = result;
return result;
}

original_code:
void prompt_init(){
  symtable_add("PROMPT", "[\\u@\\h \\W]$ ");
  euid = geteuid();
}


decompiled_code:
int prompt_print()
{
_BYTE *v0; // rax
int v1; // edi
_BYTE *v2; // rbp
_BYTE *v3; // rbx
char v4; // al

v0 = (_BYTE *)symtable_get("PROMPT");
if ( v0 )
{
v1 = (char)*v0;
v2 = v0;
if ( *v0 )
{
while ( 1 )
{
LABEL_3:
while ( 1 )
{
v3 = v2 + 1;
if ( (_BYTE)v1 == 92 )
break;
putchar(v1);
v3 = v2;
LABEL_7:
v1 = (char)v3[1];
v2 = v3 + 1;
if ( !v3[1] )
goto LABEL_8;
}
v4 = v2[1];
if ( v4 != 36 )
break;
if ( euid )
{
v2 += 2;
putchar(36);
v1 = (char)v3[1];
if ( !v3[1] )
goto LABEL_8;
}
else
{
v2 += 2;
putchar(35);
v1 = (char)v3[1];
if ( !v3[1] )
goto LABEL_8;
}
}
switch ( v4 )
{
case 'H':
v2 += 2;
display_hostname();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'W':
v2 += 2;
display_cwd();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case '\\':
v2 += 2;
putchar(47);
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'e':
goto LABEL_7;
case 'h':
v2 += 2;
display_short_hostname();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 's':
v2 += 2;
display_shell_name();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'u':
v2 += 2;
display_user_name();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
default:
v2 += 2;
putchar(94);
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
}
}
LABEL_8:
printf("%s", v2);
}
else
{
printf("> ");
}
return fflush(stdout);
}

canonical_decompiled_code:
int func6()
{
_BYTE *v0; // rax
int v1; // edi
_BYTE *v2; // rbp
_BYTE *v3; // rbx
char v4; // al

v0 = (_BYTE *)symtable_get("PROMPT");
if ( v0 )
{
v1 = (char)*v0;
v2 = v0;
if ( *v0 )
{
while ( 1 )
{
LABEL_3:
while ( 1 )
{
v3 = v2 + 1;
if ( (_BYTE)v1 == 92 )
break;
putchar(v1);
v3 = v2;
LABEL_7:
v1 = (char)v3[1];
v2 = v3 + 1;
if ( !v3[1] )
goto LABEL_8;
}
v4 = v2[1];
if ( v4 != 36 )
break;
if ( euid )
{
v2 += 2;
putchar(36);
v1 = (char)v3[1];
if ( !v3[1] )
goto LABEL_8;
}
else
{
v2 += 2;
putchar(35);
v1 = (char)v3[1];
if ( !v3[1] )
goto LABEL_8;
}
}
switch ( v4 )
{
case 'H':
v2 += 2;
func3();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'W':
v2 += 2;
func2();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case '\\':
v2 += 2;
putchar(47);
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'e':
goto LABEL_7;
case 'h':
v2 += 2;
func4();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 's':
v2 += 2;
func5();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
case 'u':
v2 += 2;
func1();
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
default:
v2 += 2;
putchar(94);
v1 = (char)v3[1];
if ( v3[1] )
goto LABEL_3;
break;
}
}
LABEL_8:
printf("%s", v2);
}
else
{
printf("> ");
}
return fflush(stdout);
}

original_code:
void prompt_print(){
  char *prompt = symtable_get("PROMPT");
  if ( ! prompt ){
    printf("> ");
    fflush(stdout);
    return;
  }
  while ( *prompt ){
    if ( *prompt == '\\' ){
      prompt++;
      switch ( *prompt ){
      case '\\':
 printf("/");
 break;
      case 'u':
 _display_user_name();
 break;
      case 'W':
 _display_cwd();
 break;
      case 's':
 _display_shell_name();
 break;
      case 'H':
 _display_hostname();
 break;
      case 'h':
 _display_short_hostname();
 break;
      case 'e':
 break;
      case '$':
 if ( euid == 0 )
   printf("#");
 else
   printf("$");
 break;
      default:
 printf("^");
 break;
      }
    } else {
      printf("%c", *prompt);
    }
    prompt++;
  }
  printf("%s", prompt);
  fflush(stdout);
}

variable_types:
prompt
char *


decompiled_code:
__int64 __fastcall rsh_native_path(char *s1)
{
const char *v2; // r12
size_t v3; // rax

if ( *s1 != 47 )
return (unsigned int)native;
v2 = rsh_root;
v3 = strlen(rsh_root);
return strncmp(s1, v2, v3) != 0;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
const char *v2; // r12
size_t v3; // rax

if ( *s1 != 47 )
return (unsigned int)native;
v2 = rsh_root;
v3 = strlen(rsh_root);
return strncmp(s1, v2, v3) != 0;
}

original_code:
int rsh_native_path(const char *path){
  if ( *path != '/' )
    return native;
  if ( strncmp(path, rsh_root, strlen(rsh_root)) )
    return 1;
  return 0;
}

canonical_original_code:
int rsh_native_path(char *path){
  if ( *path != '/' )
    return native;
  if ( strncmp(path, rsh_root, strlen(rsh_root)) )
    return 1;
  return 0;
}

variable_types:
path
char *


decompiled_code:
size_t __fastcall rsh_builtin_path(__int64 a1)
{
return a1 + strlen(rsh_root);
}

canonical_decompiled_code:
size_t __fastcall func1(__int64 a1)
{
return a1 + strlen(rsh_root);
}

original_code:
char *rsh_builtin_path(char *path){
  return path + strlen(rsh_root);
}

return_type: char *


decompiled_code:
ssize_t __fastcall rsh_read(__int64 fd, void *buf, size_t nbytes)
{
ssize_t result; // rax

if ( (fd & 0x8000) != 0 )
result = rsh_read(fd, buf, nbytes);
else
result = read(fd, buf, nbytes);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func2(__int64 fd, void *buf, size_t nbytes)
{
ssize_t result; // rax

if ( (fd & 0x8000) != 0 )
result = func2(fd, buf, nbytes);
else
result = read(fd, buf, nbytes);
return result;
}

original_code:
ssize_t rsh_read(int fd, void *buf, size_t count){
  if ( ! ( fd & 0x8000 ) )
    return read(fd, buf, count);
  else
    return _rsh_read(fd, buf, count);
}

canonical_original_code:
long rsh_read(int fd, void *buf, unsigned long count){
  if ( ! ( fd & 0x8000 ) )
    return read(fd, buf, count);
  else
    return _rsh_read(fd, buf, count);
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long

return_type: long


decompiled_code:
ssize_t __fastcall rsh_write(__int64 fd, const void *buf, size_t n)
{
ssize_t result; // rax

if ( (fd & 0x8000) != 0 )
result = rsh_write(fd, buf, n);
else
result = write(fd, buf, n);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func3(__int64 fd, const void *buf, size_t n)
{
ssize_t result; // rax

if ( (fd & 0x8000) != 0 )
result = func3(fd, buf, n);
else
result = write(fd, buf, n);
return result;
}

original_code:
ssize_t rsh_write(int fd, const void *buf, size_t count){
  if ( ! ( fd & 0x8000 ) )
    return write(fd, buf, count);
  else
    return _rsh_write(fd, buf, count);
}

canonical_original_code:
long rsh_write(int fd, void *buf, unsigned long count){
  if ( ! ( fd & 0x8000 ) )
    return write(fd, buf, count);
  else
    return _rsh_write(fd, buf, count);
}


decompiled_code:
__int64 rsh_dprintf(unsigned int a1, const char *a2, ...)
{
size_t v2; // rax
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

va_start(arg, a2);
vsnprintf(s, 0x200uLL, a2, arg);
v2 = strlen(s);
return (unsigned int)-((_DWORD)v2 != (unsigned int)rsh_write(a1, s, v2));
}

canonical_decompiled_code:
__int64 func4(unsigned int a1, const char *a2, ...)
{
size_t v2; // rax
gcc_va_list arg; // [rsp+8h] [rbp-2E0h] BYREF
char s[512]; // [rsp+20h] [rbp-2C8h] BYREF

va_start(arg, a2);
vsnprintf(s, 0x200uLL, a2, arg);
v2 = strlen(s);
return (unsigned int)-((_DWORD)v2 != (unsigned int)func3(a1, s, v2));
}

original_code:
int rsh_dprintf(int fd, char *format, ...){
  va_list args;
  char buf[512];
  int len;
  int written;
  __builtin_va_start(args,format);
  vsnprintf(buf, 512, format, args);
  len = strlen(buf);
  written = rsh_write(fd, buf, len);
  if ( written != len )
    return -1;
  return 0;
}

canonical_original_code:
int rsh_dprintf(int fd, char *format, ...){
  __builtin_va_list args;
  char buf[512];
  int len;
  int written;
  __builtin_va_start(args,format);
  vsnprintf(buf, 512, format, args);
  len = strlen(buf);
  written = rsh_write(fd, buf, len);
  if ( written != len )
    return -1;
  return 0;
}

variable_types:
format
char *

variable_types:
buf
char [512]

variable_types: len int

variable_types: written int


decompiled_code:
__int64 __fastcall builtin_native(__int64 a1, __int64 a2, __int64 a3, unsigned int a4)
{
if ( native )
{
native = 0;
rsh_dprintf(a4, "Using built in filesystem.\n");
}
else
{
native = 1;
rsh_dprintf(a4, "Using native filesystem.\n");
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, unsigned int a4)
{
if ( native )
{
native = 0;
func4(a4, "Using built in filesystem.\n");
}
else
{
native = 1;
func4(a4, "Using native filesystem.\n");
}
return 0LL;
}

original_code:
int builtin_native(int argc, char **argv, int in, int out, int err){
  if ( native ){
    native = 0;
    rsh_dprintf(out, "Using built in filesystem.\n");
  } else {
    native = 1;
    rsh_dprintf(out, "Using native filesystem.\n");
  }
  return 0;
}

variable_types: in int

variable_types: out int

variable_types: err int


decompiled_code:
__int64 rsh_dup2()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func6()
{
return 0xFFFFFFFFLL;
}

original_code:
int rsh_dup2(int oldfd, int newfd){
  return -1;
}

variable_types: oldfd int

variable_types: newfd int


decompiled_code:
int __fastcall rsh_open(char *file, int oflag, unsigned int a3)
{
int result; // eax

if ( (unsigned int)rsh_native_path(file) )
result = open(file, oflag, a3);
else
result = rsh_open(file, (unsigned int)oflag, a3);
return result;
}

canonical_decompiled_code:
int __fastcall func7(char *file, int oflag, unsigned int a3)
{
int result; // eax

if ( (unsigned int)func0(file) )
result = open(file, oflag, a3);
else
result = func7(file, (unsigned int)oflag, a3);
return result;
}

original_code:
int rsh_open(const char *pathname, int flags, mode_t mode){
  if ( rsh_native_path(pathname) ){
    return open(pathname, flags, mode);
  } else {
    return _rsh_open(pathname, flags, mode);
  }
}

canonical_original_code:
int rsh_open(char *pathname, int flags, unsigned int mode){
  if ( rsh_native_path(pathname) ){
    return open(pathname, flags, mode);
  } else {
    return _rsh_open(pathname, flags, mode);
  }
}

variable_types:
pathname
char *

variable_types: flags int

variable_types:
mode
unsigned int


decompiled_code:
int __fastcall rsh_close(int fd)
{
int result; // eax

if ( (fd & 0x8000) != 0 )
result = rsh_close();
else
result = close((unsigned __int16)fd);
return result;
}

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

if ( (fd & 0x8000) != 0 )
result = func8();
else
result = close((unsigned __int16)fd);
return result;
}

original_code:
int rsh_close(int fd){
  if ( ! ( fd & 0x8000 ) )
    return close(fd);
  else
    return _rsh_close(fd);
}


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

if ( (a1 & 0x8000) != 0 )
result = rsh_readdir();
else
result = 0LL;
return result;
}

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

if ( (a1 & 0x8000) != 0 )
result = func9();
else
result = 0LL;
return result;
}

original_code:
struct dirent *rsh_readdir(int dfd){
  if ( ! ( dfd & 0x8000 ) )
    return ((void *)0);
  else
    return _rsh_readdir(dfd);
}

variable_types: dfd int

return_type: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *


decompiled_code:
int __fastcall rsh_fstat(__int64 fd, struct stat *buf)
{
int result; // eax

if ( (fd & 0x8000) != 0 )
result = rsh_fstat(fd, buf);
else
result = fstat(fd, buf);
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 fd, struct stat *buf)
{
int result; // eax

if ( (fd & 0x8000) != 0 )
result = func10(fd, buf);
else
result = fstat(fd, buf);
return result;
}

original_code:
int rsh_fstat(int fd, struct stat *buf){
  if ( ! ( fd & 0x8000 ) )
    return fstat(fd, buf);
  else
    return _rsh_fstat(fd, buf);
}

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

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


decompiled_code:
int __fastcall rsh_mkdir(const char *path, __int64 mode)
{
int result; // eax

if ( native )
result = mkdir(path, mode);
else
result = rsh_mkdir(path, mode);
return result;
}

canonical_decompiled_code:
int __fastcall func11(const char *path, __int64 mode)
{
int result; // eax

if ( native )
result = mkdir(path, mode);
else
result = func11(path, mode);
return result;
}

original_code:
int rsh_mkdir(const char *path, mode_t mode){
  if ( native )
    return mkdir(path, mode);
  else
    return _rsh_mkdir(path);
}

canonical_original_code:
int rsh_mkdir(char *path, unsigned int mode){
  if ( native )
    return mkdir(path, mode);
  else
    return _rsh_mkdir(path);
}

variable_types:
path
char *


decompiled_code:
int __fastcall rsh_unlink(const char *name)
{
int result; // eax

if ( native )
result = unlink(name);
else
result = rsh_unlink(name);
return result;
}

canonical_decompiled_code:
int __fastcall func12(const char *name)
{
int result; // eax

if ( native )
result = unlink(name);
else
result = func12(name);
return result;
}

original_code:
int rsh_unlink(const char *path){
  if ( native )
    return unlink(path);
  else
    return _rsh_unlink(path);
}

canonical_original_code:
int rsh_unlink(char *path){
  if ( native )
    return unlink(path);
  else
    return _rsh_unlink(path);
}


decompiled_code:
int __fastcall rsh_chdir(const char *path)
{
int result; // eax

if ( native )
result = chdir(path);
else
result = rsh_chdir(path);
return result;
}

canonical_decompiled_code:
int __fastcall func13(const char *path)
{
int result; // eax

if ( native )
result = chdir(path);
else
result = func13(path);
return result;
}

original_code:
int rsh_chdir(const char *dir){
  if ( native )
    return chdir(dir);
  else
    return _rsh_chdir(dir);
}

canonical_original_code:
int rsh_chdir(char *dir){
  if ( native )
    return chdir(dir);
  else
    return _rsh_chdir(dir);
}

variable_types:
dir
char *


decompiled_code:
char *__fastcall rsh_getcwd(char *buf, size_t size)
{
char *result; // rax

if ( native )
result = getcwd(buf, size);
else
result = (char *)rsh_getcwd(buf, size);
return result;
}

canonical_decompiled_code:
char *__fastcall func14(char *buf, size_t size)
{
char *result; // rax

if ( native )
result = getcwd(buf, size);
else
result = (char *)func14(buf, size);
return result;
}

original_code:
char *rsh_getcwd(char *buf, size_t size){
  if ( native )
    return getcwd(buf, size);
  else
    return _rsh_getcwd(buf, size);
}

canonical_original_code:
char *rsh_getcwd(char *buf, unsigned long size){
  if ( native )
    return getcwd(buf, size);
  else
    return _rsh_getcwd(buf, size);
}

variable_types:
buf
char *

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall builtin_source(__int64 a1, __int64 a2)
{
__int64 v2; // rsi
char *v3; // rax
int v4; // er14
int v5; // er13
char *v6; // r12
__int64 v7; // rbp
_QWORD *i; // rbx
int v9; // eax
unsigned int v10; // er15
const char *v12; // [rsp+8h] [rbp-70h] BYREF
const char *v13; // [rsp+10h] [rbp-68h] BYREF
__int64 v14; // [rsp+18h] [rbp-60h] BYREF
__int64 v15[2]; // [rsp+20h] [rbp-58h] BYREF
_QWORD *v16; // [rsp+30h] [rbp-48h]

v2 = a2 + 16;
v3 = *(char **)(v2 - 8);
v4 = interactive;
interactive = 0;
v5 = EOF_encountered;
v6 = script;
v14 = 0LL;
v7 = script_argv;
script = v3;
EOF_encountered = 0;
script_argv = v2;
v15[0] = 0LL;
v15[1] = 0LL;
v16 = 0LL;
while ( (unsigned int)symtable_numeric(&v14, &v12, &v13) )
sym_stack_push((__int64)v15, v12, v13);
for ( i = v16; i; i = (_QWORD *)i[2] )
symtable_remove(*i);
v9 = open(script, 0);
if ( v9 < 0 )
{
v10 = -1;
perror(script);
}
else
{
rsh_set_input((unsigned int)v9);
v10 = run_script();
}
interactive = v4;
EOF_encountered = v5;
script = v6;
script_argv = v7;
rsh_set_input(0LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rsi
char *v3; // rax
int v4; // er14
int v5; // er13
char *v6; // r12
__int64 v7; // rbp
_QWORD *i; // rbx
int v9; // eax
unsigned int v10; // er15
const char *v12; // [rsp+8h] [rbp-70h] BYREF
const char *v13; // [rsp+10h] [rbp-68h] BYREF
__int64 v14; // [rsp+18h] [rbp-60h] BYREF
__int64 v15[2]; // [rsp+20h] [rbp-58h] BYREF
_QWORD *v16; // [rsp+30h] [rbp-48h]

v2 = a2 + 16;
v3 = *(char **)(v2 - 8);
v4 = interactive;
interactive = 0;
v5 = EOF_encountered;
v6 = script;
v14 = 0LL;
v7 = script_argv;
script = v3;
EOF_encountered = 0;
script_argv = v2;
v15[0] = 0LL;
v15[1] = 0LL;
v16 = 0LL;
while ( (unsigned int)symtable_numeric(&v14, &v12, &v13) )
sym_stack_push((__int64)v15, v12, v13);
for ( i = v16; i; i = (_QWORD *)i[2] )
symtable_remove(*i);
v9 = open(script, 0);
if ( v9 < 0 )
{
v10 = -1;
perror(script);
}
else
{
rsh_set_input((unsigned int)v9);
v10 = run_script();
}
interactive = v4;
EOF_encountered = v5;
script = v6;
script_argv = v7;
rsh_set_input(0LL);
return v10;
}

original_code:
int builtin_source(int argc, char **argv, int in, int out, int err){
  int ret;
  int script_file;
  int interactive_save;
  int EOF_encountered_save;
  char *script_save;
  char *name, *val;
  char **script_argv_save;
  struct sym_entry *ent = ((void *)0);
  sym_stack_head syms;
  struct _sym_stack_frame *tmp;
  argv++;
  argc--;
  interactive_save = interactive;
  EOF_encountered_save = EOF_encountered;
  script_save = script;
  script_argv_save = script_argv;
  interactive = 0;
  EOF_encountered = 0;
  script = *argv;
  script_argv = (argv+1);
  syms.name = 0; syms.data = 0; syms.next = 0;;
  while ( symtable_numeric(&ent, &name, &val) )
    _sym_stack_push(&syms, name, val);
  for ( tmp = (&syms)->next; tmp != ((void *)0); tmp = tmp->next)
    symtable_remove(tmp->name);
  script_file = open(script, 00);
  if ( script_file < 0 ){
    perror(script);
    ret = -1;
    goto clean_up;
  }
  rsh_set_input(script_file);
  ret = run_script();
 clean_up:
  interactive = interactive_save;
  EOF_encountered = EOF_encountered_save;
  script = script_save;
  script_argv = script_argv_save;
  rsh_set_input(0);
  return ret;
}

canonical_original_code:
int builtin_source(int argc, char **argv, int in, int out, int err){
  int ret;
  int script_file;
  int interactive_save;
  int EOF_encountered_save;
  char *script_save;
  char *name; char *val;
  char **script_argv_save;
  struct sym_entry *ent = ((void *)0);
  struct _sym_stack_frame syms;
  struct _sym_stack_frame *tmp;
  argv++;
  argc--;
  interactive_save = interactive;
  EOF_encountered_save = EOF_encountered;
  script_save = script;
  script_argv_save = script_argv;
  interactive = 0;
  EOF_encountered = 0;
  script = *argv;
  script_argv = (argv+1);
  syms.name = 0; syms.data = 0; syms.next = 0;;
  while ( symtable_numeric(&ent, &name, &val) )
    _sym_stack_push(&syms, name, val);
  for ( tmp = (&syms)->next; tmp != ((void *)0); tmp = tmp->next)
    symtable_remove(tmp->name);
  script_file = open(script, 00);
  if ( script_file < 0 ){
    perror(script);
    ret = -1;
    goto clean_up;
  }
  rsh_set_input(script_file);
  ret = run_script();
 clean_up:
  interactive = interactive_save;
  EOF_encountered = EOF_encountered_save;
  script = script_save;
  script_argv = script_argv_save;
  rsh_set_input(0);
  return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ret int

variable_types:
name
char *

variable_types:
val
char *

variable_types: struct sym_entry { char *name; char *data; struct sym_entry *next; } *

variable_types: struct _sym_stack_frame { char *name; char *data; struct _sym_stack_frame *next; } *


decompiled_code:
__int64 __fastcall rsh_parse_args(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
char *v4; // rax
int v5; // ebp
__int64 v6; // r12
char *v7; // rcx
int v8; // er13
char *v9; // rax

shell_name = (__int64)*argv;
while ( 1 )
{
v2 = getopt_long(argc, argv, "dl", &options, 0LL);
if ( v2 == -1 )
break;
if ( v2 == 63 )
return 0xFFFFFFFFLL;
switch ( v2 )
{
case 'd':
debug = 1;
break;
case 'f':
bifs = optarg;
break;
case 'g':
if ( (unsigned int)rsh_fat16_geometry(optarg, &geometry) )
{
fwrite("Warning: unable to parse geometry.\n", 1uLL, 0x23uLL, stderr);
geometry = 5242880LL;
*(&geometry + 1) = 0x2000LL;
}
break;
case 'l':
login = 1;
break;
case 'o':
override = 1;
break;
default:
continue;
}
}
if ( optind < argc )
{
v4 = argv[optind];
v5 = argc - ++optind;
script = v4;
v6 = 8LL * v5;
script_argv = (__int64)malloc(v6 + 1);
v7 = (char *)script_argv;
if ( v5 > 0 )
{
v8 = optind;
v9 = (char *)memcpy((void *)script_argv, &argv[optind], 8LL * v5);
optind = v8 + v5;
v7 = &v9[v6];
}
*(_QWORD *)v7 = 0LL;
result = 0LL;
interactive = 0;
}
else
{
interactive = 1;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
char *v4; // rax
int v5; // ebp
__int64 v6; // r12
char *v7; // rcx
int v8; // er13
char *v9; // rax

shell_name = (__int64)*argv;
while ( 1 )
{
v2 = getopt_long(argc, argv, "dl", &options, 0LL);
if ( v2 == -1 )
break;
if ( v2 == 63 )
return 0xFFFFFFFFLL;
switch ( v2 )
{
case 'd':
debug = 1;
break;
case 'f':
bifs = optarg;
break;
case 'g':
if ( (unsigned int)rsh_fat16_geometry(optarg, &geometry) )
{
fwrite("Warning: unable to parse geometry.\n", 1uLL, 0x23uLL, stderr);
geometry = 5242880LL;
*(&geometry + 1) = 0x2000LL;
}
break;
case 'l':
login = 1;
break;
case 'o':
override = 1;
break;
default:
continue;
}
}
if ( optind < argc )
{
v4 = argv[optind];
v5 = argc - ++optind;
script = v4;
v6 = 8LL * v5;
script_argv = (__int64)malloc(v6 + 1);
v7 = (char *)script_argv;
if ( v5 > 0 )
{
v8 = optind;
v9 = (char *)memcpy((void *)script_argv, &argv[optind], 8LL * v5);
optind = v8 + v5;
v7 = &v9[v6];
}
*(_QWORD *)v7 = 0LL;
result = 0LL;
interactive = 0;
}
else
{
interactive = 1;
result = 0LL;
}
return result;
}

original_code:
int rsh_parse_args(int argc, char **argv){
  int c, args, i;
  shell_name = argv[0];
  while ( (c = getopt_long(argc, argv, "dl", options, ((void *)0))) != -1 ){
    switch (c){
    case 'd':
      debug = 1;
      break;
    case 'l':
      login = 1;
      break;
    case 'f':
      bifs = optarg;
      break;
    case 'g':
      if ( _rsh_fat16_geometry(optarg, geometry) ){
 fprintf(stderr, "Warning: unable to parse geometry.\n");
 geometry[0] = 5*1024*1024;
 geometry[1] = 8*1024;
      }
      break;
    case 'o':
      override = 1;
      break;
    case '?':
      return -1;
    }
  }
  if ( optind >= argc ){
    interactive = 1;
    return 0;
  }
  script = argv[optind++];
  args = argc - optind;
  script_argv = (char **)malloc(1 + (sizeof(char **) * args));
  for ( i = 0; i < args; i++){
    script_argv[i] = argv[optind++];
  }
  script_argv[i] = ((void *)0);
  interactive = 0;
  return 0;
}

canonical_original_code:
int rsh_parse_args(int argc, char **argv){
  int c; int args; int i;
  shell_name = argv[0];
  while ( (c = getopt_long(argc, argv, "dl", options, ((void *)0))) != -1 ){
    switch (c){
    case 'd':
      debug = 1;
      break;
    case 'l':
      login = 1;
      break;
    case 'f':
      bifs = optarg;
      break;
    case 'g':
      if ( _rsh_fat16_geometry(optarg, geometry) ){
 fprintf(stderr, "Warning: unable to parse geometry.\n");
 geometry[0] = 5*1024*1024;
 geometry[1] = 8*1024;
      }
      break;
    case 'o':
      override = 1;
      break;
    case '?':
      return -1;
    }
  }
  if ( optind >= argc ){
    interactive = 1;
    return 0;
  }
  script = argv[optind++];
  args = argc - optind;
  script_argv = (char **)malloc(1 + (sizeof(char **) * args));
  for ( i = 0; i < args; i++){
    script_argv[i] = argv[optind++];
  }
  script_argv[i] = ((void *)0);
  interactive = 0;
  return 0;
}

variable_types: c int

variable_types: args int

variable_types: i int


decompiled_code:
__int64 interactive_shell()
{
run_interactive();
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
run_interactive();
return 0LL;
}

original_code:
int interactive_shell(){
  run_interactive();
  return 0;
}


decompiled_code:
__int64 script_shell()
{
int v0; // eax
__int64 result; // rax

v0 = open(script, 0);
if ( v0 < 0 )
{
perror(script);
result = 0xFFFFFFFFLL;
}
else
{
rsh_set_input((unsigned int)v0);
result = run_script();
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
__int64 result; // rax

v0 = open(script, 0);
if ( v0 < 0 )
{
perror(script);
result = 0xFFFFFFFFLL;
}
else
{
rsh_set_input((unsigned int)v0);
result = run_script();
}
return result;
}

original_code:
int script_shell(){
  int script_file;
  script_file = open(script, 00);
  if ( script_file < 0 ){
    perror(script);
    return -1;
  }
  rsh_set_input(script_file);
  return run_script();
}


decompiled_code:
__int64 rsh_init()
{
__int64 v0; // rcx

if ( interactive )
{
signal(2, (__sighandler_t)&rsh_history_display_async);
signal(3, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(20, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(21, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(22, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(17, (__sighandler_t)&sigchld_handler);
}
if ( (unsigned int)symtable_init(50LL) )
{
fwrite("Unable to init symbol table.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
save_default_term_settings();
rsh_init_fs();
if ( (unsigned __int64)(geometry - 5242880) > 0x2D00000 )
{
if ( !override )
{
printf("Disk size must be between 5 and 50 Megabytes. %d not acceptable\n", geometry);
puts("Specify --override to force a disk size outside of these limits.");
exit(1);
}
puts("Overriding default limits.");
}
v0 = *(&geometry + 1);
if ( (unsigned __int64)(v0 - 0x2000) > 0x2000 )
{
if ( !override )
{
puts("Cluster size must be between 8 and 16 KBytes.");
puts("Specify --override to force a disk size outside of these limits.");
exit(1);
}
puts("Overriding default limits.");
v0 = *(&geometry + 1);
}
if ( (v0 & 0x3FF) != 0 )
{
puts("Cluster size not a multiple of 1KByte; this is bad: you cannot");
puts("override this.");
exit(1);
}
printf("Loading disk image: %s (%ld:%ld)\n", bifs, geometry, v0);
if ( (unsigned int)rsh_fat16_init(bifs, geometry, *(&geometry + 1)) )
puts("WARNING: Could not load internal FS.");
rsh_set_input(0LL);
rsh_term_init();
init_rsh_pgroup(5LL);
rsh_history_init();
return prompt_init();
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rcx

if ( interactive )
{
signal(2, (__sighandler_t)&rsh_history_display_async);
signal(3, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(20, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(21, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(22, (__sighandler_t)((char *)rsh_parse_args + 1));
signal(17, (__sighandler_t)&sigchld_handler);
}
if ( (unsigned int)symtable_init(50LL) )
{
fwrite("Unable to init symbol table.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
}
save_default_term_settings();
rsh_init_fs();
if ( (unsigned __int64)(geometry - 5242880) > 0x2D00000 )
{
if ( !override )
{
printf("Disk size must be between 5 and 50 Megabytes. %d not acceptable\n", geometry);
puts("Specify --override to force a disk size outside of these limits.");
exit(1);
}
puts("Overriding default limits.");
}
v0 = *(&geometry + 1);
if ( (unsigned __int64)(v0 - 0x2000) > 0x2000 )
{
if ( !override )
{
puts("Cluster size must be between 8 and 16 KBytes.");
puts("Specify --override to force a disk size outside of these limits.");
exit(1);
}
puts("Overriding default limits.");
v0 = *(&geometry + 1);
}
if ( (v0 & 0x3FF) != 0 )
{
puts("Cluster size not a multiple of 1KByte; this is bad: you cannot");
puts("override this.");
exit(1);
}
printf("Loading disk image: %s (%ld:%ld)\n", bifs, geometry, v0);
if ( (unsigned int)rsh_fat16_init(bifs, geometry, *(&geometry + 1)) )
puts("WARNING: Could not load internal FS.");
rsh_set_input(0LL);
rsh_term_init();
init_rsh_pgroup(5LL);
rsh_history_init();
return prompt_init();
}

original_code:
void rsh_init(){
  int err;
  if ( interactive ){
    signal(2, rsh_history_display_async);
    signal(3, ((__sighandler_t) 1));
    signal(20, ((__sighandler_t) 1));
    signal(21, ((__sighandler_t) 1));
    signal(22, ((__sighandler_t) 1));
    signal(17, sigchld_handler);
  }
  err = symtable_init(50);
  if ( err ){
    fprintf(stderr, "Unable to init symbol table.\n");
    exit(1);
  }
  save_default_term_settings();
  rsh_init_fs();
  if ( geometry[0] < (5 * 1024*1024) || geometry[0] > (50 * 1024*1024) ){
    if ( ! override ){
      printf("Disk size must be between 5 and 50 Megabytes."
      " %d not acceptable\n", geometry[0]);
      printf(
 "Specify --override to force a disk size outside of these limits.\n");
      exit(1);
    } else {
      printf("Overriding default limits.\n");
    }
  }
  if ( geometry[1] < (8*1024) || geometry[1] > (16*1024) ){
    if ( ! override ){
      printf("Cluster size must be between 8 and 16 KBytes.\n");
      printf(
 "Specify --override to force a disk size outside of these limits.\n");
      exit(1);
    } else {
      printf("Overriding default limits.\n");
    }
  }
  if ( geometry[1] & 0x3ff ){
    printf("Cluster size not a multiple of 1KByte; this is bad: you cannot\n");
    printf("override this.\n");
    exit(1);
  }
  printf("Loading disk image: %s (%ld:%ld)\n", bifs, geometry[0], geometry[1]);
  err = rsh_fat16_init(bifs, geometry[0], geometry[1]);
  if ( err ){
    printf("WARNING: Could not load internal FS.\n");
  }
  rsh_set_input(0);
  rsh_term_init();
  init_rsh_pgroup(5);
  rsh_history_init();
  prompt_init();
}

canonical_original_code:
void rsh_init(){
  int err;
  if ( interactive ){
    signal(2, rsh_history_display_async);
    signal(3, ((void (*)(int)) 1));
    signal(20, ((void (*)(int)) 1));
    signal(21, ((void (*)(int)) 1));
    signal(22, ((void (*)(int)) 1));
    signal(17, sigchld_handler);
  }
  err = symtable_init(50);
  if ( err ){
    fprintf(stderr, "Unable to init symbol table.\n");
    exit(1);
  }
  save_default_term_settings();
  rsh_init_fs();
  if ( geometry[0] < (5 * 1024*1024) || geometry[0] > (50 * 1024*1024) ){
    if ( ! override ){
      printf("Disk size must be between 5 and 50 Megabytes."
      " %d not acceptable\n", geometry[0]);
      printf(
 "Specify --override to force a disk size outside of these limits.\n");
      exit(1);
    } else {
      printf("Overriding default limits.\n");
    }
  }
  if ( geometry[1] < (8*1024) || geometry[1] > (16*1024) ){
    if ( ! override ){
      printf("Cluster size must be between 8 and 16 KBytes.\n");
      printf(
 "Specify --override to force a disk size outside of these limits.\n");
      exit(1);
    } else {
      printf("Overriding default limits.\n");
    }
  }
  if ( geometry[1] & 0x3ff ){
    printf("Cluster size not a multiple of 1KByte; this is bad: you cannot\n");
    printf("override this.\n");
    exit(1);
  }
  printf("Loading disk image: %s (%ld:%ld)\n", bifs, geometry[0], geometry[1]);
  err = rsh_fat16_init(bifs, geometry[0], geometry[1]);
  if ( err ){
    printf("WARNING: Could not load internal FS.\n");
  }
  rsh_set_input(0);
  rsh_term_init();
  init_rsh_pgroup(5);
  rsh_history_init();
  prompt_init();
}

variable_types: err int


decompiled_code:
int rsh_rc_init()
{
char *v0; // r12
size_t v1; // rbp
char *v2; // rax
const char *v3; // rbx
__int64 v5[4]; // [rsp+0h] [rbp-C8h] BYREF
struct stat v6; // [rsp+20h] [rbp-A8h] BYREF

v0 = getenv("HOME");
v1 = strlen(v0);
v2 = (char *)malloc((int)v1 + 8);
if ( v2 )
{
v3 = v2;
memcpy(v2, v0, v1);
*(_QWORD *)&v3[v1] = 0x63726873722E2FLL;
LODWORD(v2) = stat(v3, &v6);
if ( (int)v2 >= 0 )
{
v5[1] = (__int64)v3;
v5[0] = (__int64)"source";
v5[2] = 0LL;
LODWORD(v2) = builtin_source(2LL, v5, 0LL, 1LL, 2LL);
}
}
return (int)v2;
}

canonical_decompiled_code:
int func4()
{
char *v0; // r12
size_t v1; // rbp
char *v2; // rax
const char *v3; // rbx
__int64 v5[4]; // [rsp+0h] [rbp-C8h] BYREF
struct stat v6; // [rsp+20h] [rbp-A8h] BYREF

v0 = getenv("HOME");
v1 = strlen(v0);
v2 = (char *)malloc((int)v1 + 8);
if ( v2 )
{
v3 = v2;
memcpy(v2, v0, v1);
*(_QWORD *)&v3[v1] = 0x63726873722E2FLL;
LODWORD(v2) = stat(v3, &v6);
if ( (int)v2 >= 0 )
{
v5[1] = (__int64)v3;
v5[0] = (__int64)"source";
v5[2] = 0LL;
LODWORD(v2) = builtin_source(2LL, v5, 0LL, 1LL, 2LL);
}
}
return (int)v2;
}

original_code:
void rsh_rc_init(){
  int len, _argc;
  char *home;
  char *rc = "/.rshrc";
  char *rc_script;
  char *_argv[3];
  struct stat sbuf;
  home = getenv("HOME");
  len = strlen(home) + strlen(rc) + 1;
  rc_script = (char *)malloc(len);
  if ( ! rc_script )
    return;
  strcpy(rc_script, home);
  strcat(rc_script, rc);
  if ( stat(rc_script, &sbuf) < 0)
    return;
  _argc = 2;
  _argv[0] = "source";
  _argv[1] = rc_script;
  _argv[2] = ((void *)0);
  builtin_source(_argc, _argv, 0, 1, 2);
}

canonical_original_code:
void rsh_rc_init(){
  int len; int _argc;
  char *home;
  char *rc = "/.rshrc";
  char *rc_script;
  char *_argv[3];
  struct stat sbuf;
  home = getenv("HOME");
  len = strlen(home) + strlen(rc) + 1;
  rc_script = (char *)malloc(len);
  if ( ! rc_script )
    return;
  strcpy(rc_script, home);
  strcat(rc_script, rc);
  if ( stat(rc_script, &sbuf) < 0)
    return;
  _argc = 2;
  _argv[0] = "source";
  _argv[1] = rc_script;
  _argv[2] = ((void *)0);
  builtin_source(_argc, _argv, 0, 1, 2);
}

variable_types: len int

variable_types:
home
char *

variable_types:
rc
char *

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


decompiled_code:
__int64 __fastcall rsh_main(int a1, char **a2)
{
if ( (unsigned int)rsh_parse_args(a1, a2) )
{
fwrite("Failed to parse args.\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
rsh_init();
rsh_rc_init();
if ( interactive )
{
interactive_shell();
if ( !interactive )
return 0LL;
}
else
{
script_shell();
if ( !interactive )
return 0LL;
}
puts("Good bye.");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, char **a2)
{
if ( (unsigned int)func0(a1, a2) )
{
fwrite("Failed to parse args.\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
func3();
func4();
if ( interactive )
{
func1();
if ( !interactive )
return 0LL;
}
else
{
func2();
if ( !interactive )
return 0LL;
}
puts("Good bye.");
return 0LL;
}

original_code:
int rsh_main(int argc, char **argv){
  int err, status;
  err = rsh_parse_args(argc, argv);
  if ( err ){
    fprintf(stderr, "Failed to parse args.\n");
    exit(1);
  }
  rsh_init();
  rsh_rc_init();
  if ( interactive )
    status = interactive_shell();
  else
    status = script_shell();
  if ( interactive )
    printf("Good bye.\n");
  return 0;
}

canonical_original_code:
int rsh_main(int argc, char **argv){
  int err; int status;
  err = rsh_parse_args(argc, argv);
  if ( err ){
    fprintf(stderr, "Failed to parse args.\n");
    exit(1);
  }
  rsh_init();
  rsh_rc_init();
  if ( interactive )
    status = interactive_shell();
  else
    status = script_shell();
  if ( interactive )
    printf("Good bye.\n");
  return 0;
}

variable_types: status int


decompiled_code:
void __fastcall __noreturn rsh_exit(int status)
{
fflush(stdout);
fflush(stderr);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(int status)
{
fflush(stdout);
fflush(stderr);
exit(status);
}

original_code:
void rsh_exit(int status){
  fflush(stdout);
  fflush(stderr);
  exit(status);
}


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

result = 0LL;
*((_QWORD *)&table + 4) = 0LL;
table = 0LL;
*(&table + 1) = 0LL;
return result;
}

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

result = 0LL;
*((_QWORD *)&table + 4) = 0LL;
table = 0LL;
*(&table + 1) = 0LL;
return result;
}

original_code:
int symtable_init(int start_size){
  memset(&table, 0, sizeof(struct sym_table));
  return 0;
}

user_defined_types: struct sym_table { struct sym_entry *tbl_entries; int entries; struct sym_entry *cach_add; struct sym_entry *cach_get; struct sym_entry *last; }

user_defined_types: struct sym_entry { char *name; char *data; struct sym_entry *next; }


decompiled_code:
__int64 __fastcall symtable_numeric(const char ***a1, const char **a2, _QWORD *a3)
{
const char **v4; // rax
const char **v5; // rax
const char **v6; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v4 = *a1;
if ( *a1 )
{
if ( !done_0 )
goto LABEL_3;
LABEL_11:
done_0 = 0;
return 0LL;
}
if ( done_0 )
goto LABEL_11;
v4 = (const char **)table;
*a1 = (const char **)table;
LABEL_3:
if ( !v4 )
return 0LL;
while ( 1 )
{
strtol(*v4, &endptr, 10);
v5 = *a1;
if ( !*endptr )
break;
v4 = (const char **)v5[2];
*a1 = v4;
if ( !v4 )
{
done_0 = 1;
return 0LL;
}
}
*a3 = v5[1];
if ( a2 )
*a2 = *v5;
v6 = (const char **)v5[2];
if ( !v6 )
done_0 = 1;
*a1 = v6;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char ***a1, const char **a2, _QWORD *a3)
{
const char **v4; // rax
const char **v5; // rax
const char **v6; // rax
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v4 = *a1;
if ( *a1 )
{
if ( !done_0 )
goto LABEL_3;
LABEL_11:
done_0 = 0;
return 0LL;
}
if ( done_0 )
goto LABEL_11;
v4 = (const char **)table;
*a1 = (const char **)table;
LABEL_3:
if ( !v4 )
return 0LL;
while ( 1 )
{
strtol(*v4, &endptr, 10);
v5 = *a1;
if ( !*endptr )
break;
v4 = (const char **)v5[2];
*a1 = v4;
if ( !v4 )
{
done_0 = 1;
return 0LL;
}
}
*a3 = v5[1];
if ( a2 )
*a2 = *v5;
v6 = (const char **)v5[2];
if ( !v6 )
done_0 = 1;
*a1 = v6;
return 1LL;
}

original_code:
int symtable_numeric(struct sym_entry **status, char **name, char **data){
  char *end;
  static int done = 0;
  if ( *status == ((void *)0) && ! done ){
    *status = table.tbl_entries;
  }
  if ( done ){
    done = 0;
    return 0;
  }
  while ( *status != ((void *)0) ){
    strtol( (*status)->name, &end, 10);
    if ( *end ){
      *status = (*status)->next;
      if ( ! *status )
 done = 1;
      continue;
    }
    *data = (*status)->data;
    if ( name )
      *name = (*status)->name;
    if ( ! (*status)->next )
      done = 1;
    *status = (*status)->next;
    return 1;
  }
  return 0;
}

variable_types: struct sym_entry { char *name; char *data; struct sym_entry *next; } **

variable_types:
name
char **

variable_types:
data
char **

variable_types:
end
char *

variable_types: done int


decompiled_code:
int __fastcall symtable_remove(char *name)
{
__int64 v1; // rbx
const char **v2; // rax
const char **v3; // rax
char *v4; // r13
int v5; // eax
__int64 v6; // rbp
int v7; // er8
__int64 v8; // rax

if ( !getenv(name) )
{
v1 = table;
if ( (_QWORD)table )
{
v2 = (const char **)*((_QWORD *)&table + 2);
if ( v2 && !strcmp(name, *v2) )
*((_QWORD *)&table + 2) = 0LL;
v3 = (const char **)*((_QWORD *)&table + 3);
if ( v3 )
{
if ( !strcmp(name, *v3) )
*((_QWORD *)&table + 3) = 0LL;
}
v4 = *(char **)v1;
v5 = strcmp(*(const char **)v1, name);
v6 = *(_QWORD *)(v1 + 16);
if ( v5 )
{
if ( !v6 )
return 0;
while ( 1 )
{
v4 = *(char **)v6;
v7 = strcmp(*(const char **)v6, name);
v8 = *(_QWORD *)(v6 + 16);
if ( !v7 )
break;
v1 = v6;
if ( !v8 )
return 0;
v6 = *(_QWORD *)(v6 + 16);
}
*(_QWORD *)(v1 + 16) = v8;
}
else
{
*(_QWORD *)&table = *(_QWORD *)(v1 + 16);
v6 = v1;
}
free(v4);
free(*(void **)(v6 + 8));
free((void *)v6);
}
return 0;
}
return unsetenv(name);
}

canonical_decompiled_code:
int __fastcall func2(char *name)
{
__int64 v1; // rbx
const char **v2; // rax
const char **v3; // rax
char *v4; // r13
int v5; // eax
__int64 v6; // rbp
int v7; // er8
__int64 v8; // rax

if ( !getenv(name) )
{
v1 = table;
if ( (_QWORD)table )
{
v2 = (const char **)*((_QWORD *)&table + 2);
if ( v2 && !strcmp(name, *v2) )
*((_QWORD *)&table + 2) = 0LL;
v3 = (const char **)*((_QWORD *)&table + 3);
if ( v3 )
{
if ( !strcmp(name, *v3) )
*((_QWORD *)&table + 3) = 0LL;
}
v4 = *(char **)v1;
v5 = strcmp(*(const char **)v1, name);
v6 = *(_QWORD *)(v1 + 16);
if ( v5 )
{
if ( !v6 )
return 0;
while ( 1 )
{
v4 = *(char **)v6;
v7 = strcmp(*(const char **)v6, name);
v8 = *(_QWORD *)(v6 + 16);
if ( !v7 )
break;
v1 = v6;
if ( !v8 )
return 0;
v6 = *(_QWORD *)(v6 + 16);
}
*(_QWORD *)(v1 + 16) = v8;
}
else
{
*(_QWORD *)&table = *(_QWORD *)(v1 + 16);
v6 = v1;
}
free(v4);
free(*(void **)(v6 + 8));
free((void *)v6);
}
return 0;
}
return unsetenv(name);
}

original_code:
int symtable_remove(char *sym){
  char *env_sym;
  struct sym_entry *ent;
  struct sym_entry *prev;
  env_sym = getenv(sym);
  if ( env_sym ){
    return unsetenv(sym);
  }
  if ( ! table.tbl_entries )
    return 0;
  if ( table.cach_add )
    if ( strcmp(sym, table.cach_add->name) == 0 )
      table.cach_add = ((void *)0);
  if ( table.cach_get )
    if ( strcmp(sym, table.cach_get->name) == 0 )
      table.cach_get = ((void *)0);
  if ( strcmp(table.tbl_entries->name, sym) == 0 ){
    ent = table.tbl_entries;
    table.tbl_entries = ent->next;
    goto cleanup;
  }
  ent = table.tbl_entries->next;
  prev = table.tbl_entries;
  for ( ; ent != ((void *)0); ent = ent->next){
    if ( strcmp(ent->name, sym) == 0 )
      break;
    prev = ent;
  }
  if ( ! ent )
    return 0;
  prev->next = ent->next;
 cleanup:
  free(ent->name);
  free(ent->data);
  free(ent);
  return 0;
}

variable_types:
sym
char *


decompiled_code:
int __fastcall symtable_add(char *s, char *value)
{
_QWORD *v3; // rax
_QWORD *v4; // rbp
void *v5; // rdi

if ( !s )
return -1;
if ( getenv(s) )
return setenv(s, value, 1);
v3 = symtable_get_entry(s);
v4 = v3;
if ( !v3 )
return -1;
v5 = (void *)v3[1];
if ( v5 )
free(v5);
if ( value )
v4[1] = strdup(value);
else
v4[1] = 0LL;
*((_QWORD *)&table + 2) = v4;
return 0;
}

canonical_decompiled_code:
int __fastcall func3(char *s, char *value)
{
_QWORD *v3; // rax
_QWORD *v4; // rbp
void *v5; // rdi

if ( !s )
return -1;
if ( getenv(s) )
return setenv(s, value, 1);
v3 = symtable_get_entry(s);
v4 = v3;
if ( !v3 )
return -1;
v5 = (void *)v3[1];
if ( v5 )
free(v5);
if ( value )
v4[1] = strdup(value);
else
v4[1] = 0LL;
*((_QWORD *)&table + 2) = v4;
return 0;
}

original_code:
int symtable_add(char *sym, char *data){
  struct sym_entry *syment;
  if ( ! sym )
    return -1;
  if ( getenv(sym) )
    return setenv(sym, data, 1);
  syment = _symtable_get_entry(sym);
  if ( ! syment )
    return -1;
  if ( syment->data )
    free(syment->data);
  if ( data ){
    syment->data = strdup(data);
  } else
    syment->data = ((void *)0);
  table.cach_add = syment;
  return 0;
}

variable_types:
data
char *

variable_types: struct sym_entry { char *name; char *data; struct sym_entry *next; } *


decompiled_code:
char *__fastcall symtable_get(char *s)
{
char *result; // rax
_QWORD *v2; // rax

if ( !s )
return 0LL;
result = getenv(s);
if ( !result )
{
v2 = symtable_get_entry(s);
*((_QWORD *)&table + 3) = v2;
result = (char *)v2[1];
}
return result;
}

canonical_decompiled_code:
char *__fastcall func4(char *s)
{
char *result; // rax
_QWORD *v2; // rax

if ( !s )
return 0LL;
result = getenv(s);
if ( !result )
{
v2 = symtable_get_entry(s);
*((_QWORD *)&table + 3) = v2;
result = (char *)v2[1];
}
return result;
}

original_code:
char *symtable_get(char *sym){
  char *env_sym;
  struct sym_entry *syment;
  if ( ! sym )
    return ((void *)0);
  env_sym = getenv(sym);
  if ( env_sym )
    return env_sym;
  syment = _symtable_get_entry(sym);
  table.cach_get = syment;
  return syment->data;
}


repo: impiaaa/texmex

canonical_original_code:
unsigned short rgb565interp(unsigned short color0, unsigned short color1) {
 char r; char g; char b;
 char r0; char g0; char b0;
 char r1; char g1; char b1;
 r0 = (color0 & 0xF800) >> 11;
 g0 = (color0 & 0x07E0) >> 5;
 b0 = (color0 & 0x001F);
 r1 = (color1 & 0xF800) >> 11;
 g1 = (color1 & 0x07E0) >> 5;
 b1 = (color1 & 0x001F);
 r = (2 * r0 + r1 + 1) / 3;
 g = (2 * g0 + g1 + 1) / 3;
 b = (2 * b0 + b1 + 1) / 3;
 return (r << 11) | (g << 5) | (b);
}

variable_types: r char

variable_types: g char

variable_types: b char

return_type: unsigned short


decompiled_code:
__int64 __fastcall rgba8888interp(unsigned int a1, unsigned int a2)
{
return (((BYTE1(a2) + 2 * (unsigned int)BYTE1(a1) + 1) / 3) << 8) | (((((a1 >> 15) & 0x1FE) + BYTE2(a2) + 1) / 3) << 16) | (((unsigned __int8)a2 + 2 * (unsigned int)(unsigned __int8)a1 + 1) / 3) | (((HIBYTE(a2) + 2 * HIBYTE(a1) + 1) / 3u) << 24);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
return (((BYTE1(a2) + 2 * (unsigned int)BYTE1(a1) + 1) / 3) << 8) | (((((a1 >> 15) & 0x1FE) + BYTE2(a2) + 1) / 3) << 16) | (((unsigned __int8)a2 + 2 * (unsigned int)(unsigned __int8)a1 + 1) / 3) | (((HIBYTE(a2) + 2 * HIBYTE(a1) + 1) / 3u) << 24);
}

canonical_original_code:
unsigned int rgba8888interp(unsigned int color0, unsigned int color1) {
 unsigned char r; unsigned char g; unsigned char b; unsigned char a;
 unsigned char r0; unsigned char g0; unsigned char b0; unsigned char a0;
 unsigned char r1; unsigned char g1; unsigned char b1; unsigned char a1;
 r0 = (color0 & 0xFF000000) >> 24;
 g0 = (color0 & 0x00FF0000) >> 16;
 b0 = (color0 & 0x0000FF00) >> 8;
 a0 = (color0 & 0x000000FF) >> 0;
 r1 = (color1 & 0xFF000000) >> 24;
 g1 = (color1 & 0x00FF0000) >> 16;
 b1 = (color1 & 0x0000FF00) >> 8;
 a1 = (color1 & 0x000000FF) >> 0;
 r = (2 * r0 + r1 + 1) / 3;
 g = (2 * g0 + g1 + 1) / 3;
 b = (2 * b0 + b1 + 1) / 3;
 a = (2 * a0 + a1 + 1) / 3;
 return (r << 24) | (g << 16) | (b << 8) | a;
}

variable_types:
r
unsigned char

variable_types:
g
unsigned char

variable_types:
b
unsigned char

variable_types:
a
unsigned char

return_type: unsigned int


canonical_original_code:
unsigned short rgb565interp2(unsigned short color0, unsigned short color1) {
 char r; char g; char b;
 char r0; char g0; char b0;
 char r1; char g1; char b1;
 r0 = (color0 & 0xF800) >> 11;
 g0 = (color0 & 0x07E0) >> 5;
 b0 = (color0 & 0x001F);
 r1 = (color1 & 0xF800) >> 11;
 g1 = (color1 & 0x07E0) >> 5;
 b1 = (color1 & 0x001F);
 r = (r0 + r1) / 2;
 g = (g0 + g1) / 2;
 b = (b0 + b1) / 2;
 return (r << 11) | (g << 5) | (b);
}


decompiled_code:
__int64 __fastcall rgba8888interp2(unsigned int a1, unsigned int a2)
{
return ((BYTE2(a2) + BYTE2(a1)) >> 1 << 16) | (((unsigned __int8)a2 + (unsigned __int8)a1) >> 1) | ((int)(HIBYTE(a2) + HIBYTE(a1)) >> 1 << 24) | (unsigned int)((BYTE1(a2) + BYTE1(a1)) >> 1 << 8);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned int a2)
{
return ((BYTE2(a2) + BYTE2(a1)) >> 1 << 16) | (((unsigned __int8)a2 + (unsigned __int8)a1) >> 1) | ((int)(HIBYTE(a2) + HIBYTE(a1)) >> 1 << 24) | (unsigned int)((BYTE1(a2) + BYTE1(a1)) >> 1 << 8);
}

canonical_original_code:
unsigned int rgba8888interp2(unsigned int color0, unsigned int color1) {
 unsigned char r; unsigned char g; unsigned char b; unsigned char a;
 unsigned char r0; unsigned char g0; unsigned char b0; unsigned char a0;
 unsigned char r1; unsigned char g1; unsigned char b1; unsigned char a1;
 r0 = (color0 & 0xFF000000) >> 24;
 g0 = (color0 & 0x00FF0000) >> 16;
 b0 = (color0 & 0x0000FF00) >> 8;
 a0 = (color0 & 0x000000FF) >> 0;
 r1 = (color1 & 0xFF000000) >> 24;
 g1 = (color1 & 0x00FF0000) >> 16;
 b1 = (color1 & 0x0000FF00) >> 8;
 a1 = (color1 & 0x000000FF) >> 0;
 r = (r0 + r1) / 2;
 g = (g0 + g1) / 2;
 b = (b0 + b1) / 2;
 a = (a0 + a1) / 2;
 return (r << 24) | (g << 16) | (b << 8) | a;
}


decompiled_code:
char rgb565expand()
{
return -1;
}

canonical_decompiled_code:
char func4()
{
return -1;
}

canonical_original_code:
unsigned int rgb565expand(unsigned short color0) {
 return ((color0 & 0xF800) << 16) | ((color0 & 0x07E0) << 13) | ((color0 & 0x001F) << 11) | 0xFF;
}


original_code:
char *DXT1AtoRGBA8888(unsigned char *dataIn, unsigned short width, unsigned short height) {
 int x, y;
 uint32_t colors[4];
 memset(colors, 0, 4*sizeof(uint32_t));
 uint32_t pixels;
 uint32_t chunkIdx;
 char *dataOut = malloc(4*width*height);
 for (y = 0; y < height; y += 4) {
  for (x = 0; x < width; x += 4) {
   chunkIdx = ((y * width / 2) + x * 2);
   colors[0] = rgb565expand((((uint32_t)(dataIn[chunkIdx+1]))<<8)|(((uint32_t)(dataIn[chunkIdx+0]))));
   colors[1] = rgb565expand((((uint32_t)(dataIn[chunkIdx+3]))<<8)|(((uint32_t)(dataIn[chunkIdx+2]))));
   pixels = (dataIn[chunkIdx+4 ])<<24;
   pixels |= (dataIn[chunkIdx+4+1])<<16;
   pixels |= (dataIn[chunkIdx+4+2])<< 8;
   pixels |= (dataIn[chunkIdx+4+3]);
   if (colors[0] > colors[1]) {
    colors[2] = rgba8888interp(colors[0], colors[1]);
    colors[3] = rgba8888interp(colors[1], colors[0]);
   }
   else {
    colors[2] = rgba8888interp2(colors[0], colors[1]);
    colors[3] = 0;
   }
   int xs, ys;
   for (ys = 0; ys < 4; ys++) {
    for (xs = 0; xs < 4; xs++) {
     int shamt = (((3-ys) * 4) + xs)*2;
     uint32_t pixel = colors[(pixels&(3<<shamt))>>shamt];
     char *outp = dataOut + (((y + ys) * width) + (x + xs))*4;
     *(outp ) = pixel>> 8;
     *(outp+1) = pixel>>16;
     *(outp+2) = pixel>>24;
     *(outp+3) = pixel;
    }
   }
  }
 }
 return dataOut;
}

canonical_original_code:
char *DXT1AtoRGBA8888(unsigned char *dataIn, unsigned short width, unsigned short height) {
 int x; int y;
 unsigned int colors[4];
 memset(colors, 0, 4*sizeof(unsigned int));
 unsigned int pixels;
 unsigned int chunkIdx;
 char *dataOut = malloc(4*width*height);
 for (y = 0; y < height; y += 4) {
  for (x = 0; x < width; x += 4) {
   chunkIdx = ((y * width / 2) + x * 2);
   colors[0] = rgb565expand((((unsigned int)(dataIn[chunkIdx+1]))<<8)|(((unsigned int)(dataIn[chunkIdx+0]))));
   colors[1] = rgb565expand((((unsigned int)(dataIn[chunkIdx+3]))<<8)|(((unsigned int)(dataIn[chunkIdx+2]))));
   pixels = (dataIn[chunkIdx+4 ])<<24;
   pixels |= (dataIn[chunkIdx+4+1])<<16;
   pixels |= (dataIn[chunkIdx+4+2])<< 8;
   pixels |= (dataIn[chunkIdx+4+3]);
   if (colors[0] > colors[1]) {
    colors[2] = rgba8888interp(colors[0], colors[1]);
    colors[3] = rgba8888interp(colors[1], colors[0]);
   }
   else {
    colors[2] = rgba8888interp2(colors[0], colors[1]);
    colors[3] = 0;
   }
   int xs; int ys;
   for (ys = 0; ys < 4; ys++) {
    for (xs = 0; xs < 4; xs++) {
     int shamt = (((3-ys) * 4) + xs)*2;
     unsigned int pixel = colors[(pixels&(3<<shamt))>>shamt];
     char *outp = dataOut + (((y + ys) * width) + (x + xs))*4;
     *(outp ) = pixel>> 8;
     *(outp+1) = pixel>>16;
     *(outp+2) = pixel>>24;
     *(outp+3) = pixel;
    }
   }
  }
 }
 return dataOut;
}

variable_types:
dataIn
unsigned char *

variable_types:
width
unsigned short

variable_types:
height
unsigned short

variable_types: x int

variable_types: y int

variable_types:
colors
unsigned int [4]

variable_types:
pixels
unsigned int

variable_types:
chunkIdx
unsigned int

variable_types:
dataOut
char *

variable_types: xs int

variable_types: ys int

variable_types: shamt int

variable_types:
pixel
unsigned int

variable_types:
outp
char *


tmTgaValidateFooter

decompiled_code:
bool __fastcall tmTgaValidateFooter(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, char a8)
{
return strcmp(&a8, "TRUEVISION-XFILE.") == 0;
}

canonical_decompiled_code:
bool __fastcall func0(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, char a8)
{
return strcmp(&a8, "TRUEVISION-XFILE.") == 0;
}

original_code:
char tmTgaValidateFooter(TMTGAFooter footer) {
 return strncmp(footer.signature, "TRUEVISION-XFILE.", 18) == 0;
}

canonical_original_code:
char tmTgaValidateFooter(struct TMTGAFooter footer) {
 return strncmp(footer.signature, "TRUEVISION-XFILE.", 18) == 0;
}

variable_types: struct TMTGAFooter { int extensionAreaOffset; int developerDirectoryOffset; char signature[18]; }

return_type: char


TMLog

decompiled_code:
int TMLog(unsigned int a1, const char *a2, __int64 a3, const char *a4, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( TMLogGlobalLevel >= a1 )
{
if ( a1 == 3 )
{
fprintf(stderr, "[%s] %s:%d: ", "INFO", a2, a3);
goto LABEL_7;
}
if ( a1 <= 3 )
{
if ( a1 == 1 )
{
fprintf(stderr, "[%s] %s:%d: ", "ERROR", a2, a3);
}
else
{
if ( a1 != 2 )
return result;
fprintf(stderr, "[%s] %s:%d: ", "WARNING", a2, a3);
}
goto LABEL_7;
}
if ( a1 == 4 )
{
fprintf(stderr, "[%s] %s:%d: ", "DEBUG", a2, a3);
LABEL_7:
va_start(arg, a4);
vfprintf(stderr, a4, arg);
return fputc(10, stderr);
}
}
return result;
}

canonical_decompiled_code:
int func0(unsigned int a1, const char *a2, __int64 a3, const char *a4, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( TMLogGlobalLevel >= a1 )
{
if ( a1 == 3 )
{
fprintf(stderr, "[%s] %s:%d: ", "INFO", a2, a3);
goto LABEL_7;
}
if ( a1 <= 3 )
{
if ( a1 == 1 )
{
fprintf(stderr, "[%s] %s:%d: ", "ERROR", a2, a3);
}
else
{
if ( a1 != 2 )
return result;
fprintf(stderr, "[%s] %s:%d: ", "WARNING", a2, a3);
}
goto LABEL_7;
}
if ( a1 == 4 )
{
fprintf(stderr, "[%s] %s:%d: ", "DEBUG", a2, a3);
LABEL_7:
va_start(arg, a4);
vfprintf(stderr, a4, arg);
return fputc(10, stderr);
}
}
return result;
}

original_code:
void TMLog(TMLogLevel level, char *file, unsigned long line, char *message, ...) {
 if (level > TMLogGlobalLevel)
  return;
 switch (level) {
  case TMLogLevelError:
   fprintf(stderr, "[%s] %s:%d: ", "ERROR", file, line);
   break;
  case TMLogLevelWarning:
   fprintf(stderr, "[%s] %s:%d: ", "WARNING", file, line);
   break;
  case TMLogLevelInfo:
   fprintf(stderr, "[%s] %s:%d: ", "INFO", file, line);
   break;
  case TMLogLevelDebug:
   fprintf(stderr, "[%s] %s:%d: ", "DEBUG", file, line);
   break;
  default:
   return;
 }
 va_list argptr;
 __builtin_va_start(argptr,message);
 vfprintf(stderr, message, argptr);
 __builtin_va_end(argptr);
 fprintf(stderr, "\n");
}

canonical_original_code:
void TMLog(enum TMLogLevel level, char *file, unsigned long line, char *message, ...) {
 if (level > TMLogGlobalLevel)
  return;
 switch (level) {
  case TMLogLevelError:
   fprintf(stderr, "[%s] %s:%d: ", "ERROR", file, line);
   break;
  case TMLogLevelWarning:
   fprintf(stderr, "[%s] %s:%d: ", "WARNING", file, line);
   break;
  case TMLogLevelInfo:
   fprintf(stderr, "[%s] %s:%d: ", "INFO", file, line);
   break;
  case TMLogLevelDebug:
   fprintf(stderr, "[%s] %s:%d: ", "DEBUG", file, line);
   break;
  default:
   return;
 }
 __builtin_va_list argptr;
 __builtin_va_start(argptr,message);
 vfprintf(stderr, message, argptr);
 __builtin_va_end(argptr);
 fprintf(stderr, "\n");
}

variable_types: enum TMLogLevel { TMLogLevelQuiet=0, TMLogLevelError=1, TMLogLevelWarning=2, TMLogLevelInfo=3, TMLogLevelDebug=4, TMLogLevelAll=5 }

variable_types:
file
char *

variable_types:
line
unsigned long

variable_types:
message
char *


TMLogSetLevel

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

if ( a1 > 5 )
return TMLog(1u, "utils.c", 9LL, "Invalid log level %d", a1);
TMLogGlobalLevel = a1;
return result;
}

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

if ( a1 > 5 )
return func0(1u, "utils.c", 9LL, "Invalid log level %d", a1);
TMLogGlobalLevel = a1;
return result;
}

original_code:
void TMLogSetLevel(TMLogLevel i) {
 if ((i < 0) || (i > TMLogLevelAll)) {
  TMLog(TMLogLevelError, "utils.c", 9, "Invalid log level %d", i);
 }
 else {
  TMLogGlobalLevel = i;
 }
}

canonical_original_code:
void TMLogSetLevel(enum TMLogLevel i) {
 if ((i < 0) || (i > TMLogLevelAll)) {
  TMLog(TMLogLevelError, "utils.c", 9, "Invalid log level %d", i);
 }
 else {
  TMLogGlobalLevel = i;
 }
}

user_defined_types: enum TMLogLevel { TMLogLevelQuiet=0, TMLogLevelError=1, TMLogLevelWarning=2, TMLogLevelInfo=3, TMLogLevelDebug=4, TMLogLevelAll=5 }


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(stderr, "Usage:\n %s: [vh] [-i infile]... [-o outfile]...\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "Usage:\n %s: [vh] [-i infile]... [-o outfile]...\n", a1);
}

original_code:
void usage(char *execname) {
 fprintf(stderr,
   "Usage:\n"
   "  %s: [vh] [-i infile]... [-o outfile]...\n",
   execname);
}

variable_types:
execname
char *


setupFB

decompiled_code:
void *setupFB()
{
int v0; // er8
void *result; // rax

fbfd = open("/dev/fb0", 2);
printf("Framebuffer fd: %i\n", (unsigned int)fbfd);
v0 = fbfd;
if ( fbfd < 0 )
{
puts("Error opening FB");
v0 = fbfd;
}
result = mmap(0LL, 0x25800uLL, 3, 1, v0, 0LL);
fbMap = result;
return result;
}

canonical_decompiled_code:
void *func0()
{
int v0; // er8
void *result; // rax

fbfd = open("/dev/fb0", 2);
printf("Framebuffer fd: %i\n", (unsigned int)fbfd);
v0 = fbfd;
if ( fbfd < 0 )
{
puts("Error opening FB");
v0 = fbfd;
}
result = mmap(0LL, 0x25800uLL, 3, 1, v0, 0LL);
fbMap = result;
return result;
}

original_code:
void setupFB() {
 fbfd = open("/dev/fb0", 02);
 printf("Framebuffer fd: %i\n", fbfd);
 if (fbfd < 0) printf("Error opening FB\n");
 fbMap = (uint16_t*)mmap(((void *)0), 320*240*2, 0x1 | 0x2, 0x01, fbfd, 0);
}

canonical_original_code:
void setupFB() {
 fbfd = open("/dev/fb0", 02);
 printf("Framebuffer fd: %i\n", fbfd);
 if (fbfd < 0) printf("Error opening FB\n");
 fbMap = (unsigned short *)mmap(((void *)0), 320*240*2, 0x1 | 0x2, 0x01, fbfd, 0);
}


paintRect

decompiled_code:
int __fastcall paintRect(int a1, int a2, int a3, int a4)
{
int v4; // edi

rect[0] = a1;
v4 = fbfd;
rect[1] = a2;
rect[2] = a3;
rect[3] = a4;
return ioctl(v4, 0x4680uLL, rect);
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2, int a3, int a4)
{
int v4; // edi

rect[0] = a1;
v4 = fbfd;
rect[1] = a2;
rect[2] = a3;
rect[3] = a4;
return ioctl(v4, 0x4680uLL, rect);
}

original_code:
void paintRect(int dx, int dy, int width, int height) {
  rect.dx = dx;
 rect.dy = dy;
 rect.width = width;
 rect.height = height;
 ioctl(fbfd, 0x4680, &rect);
}

variable_types: dx int

variable_types: dy int

variable_types: width int

variable_types: height int


blankScreen

decompiled_code:
int blankScreen()
{
void *v0; // rbp

v0 = fbMap;
puts("Blank screen");
memset(v0, 0, 0x25800uLL);
return paintRect(0, 0, 320, 240);
}

canonical_decompiled_code:
int func2()
{
void *v0; // rbp

v0 = fbMap;
puts("Blank screen");
memset(v0, 0, 0x25800uLL);
return func1(0, 0, 320, 240);
}

original_code:
void blankScreen() {
 int i;
 uint16_t *tempfbmap = fbMap;
 printf("Blank screen\n");
 for (i = 0; i < 320*240; i++) {
  *tempfbmap = BL;
  tempfbmap++;
 }
 paintRect(0, 0, 320, 240);
}

canonical_original_code:
void blankScreen() {
 int i;
 unsigned short *tempfbmap = fbMap;
 printf("Blank screen\n");
 for (i = 0; i < 320*240; i++) {
  *tempfbmap = BL;
  tempfbmap++;
 }
 paintRect(0, 0, 320, 240);
}

variable_types: i int

variable_types:
tempfbmap
unsigned short *


paintSprite

original_code:
void paintSprite(struct sprite *s) {
 uint16_t *tempfbmap = fbMap;
 tempfbmap += 320 * s->y + s->x;
 uint16_t *spriteP = s->a;
 int j;
 int i;
 for (j = 0; j < s->h; j++) {
  for (i = 0; i < s->w; i++) {
   *tempfbmap = *spriteP;
   tempfbmap++;
   spriteP++;
  }
  tempfbmap += 320 - s->w;
 }
 paintRect(s->x - s->speed, s->y - s->speed, s->w + s->speed * 2, s->h+1 + s->speed * 2);
}

canonical_original_code:
void paintSprite(struct sprite *s) {
 unsigned short *tempfbmap = fbMap;
 tempfbmap += 320 * s->y + s->x;
 unsigned short *spriteP = s->a;
 int j;
 int i;
 for (j = 0; j < s->h; j++) {
  for (i = 0; i < s->w; i++) {
   *tempfbmap = *spriteP;
   tempfbmap++;
   spriteP++;
  }
  tempfbmap += 320 - s->w;
 }
 paintRect(s->x - s->speed, s->y - s->speed, s->w + s->speed * 2, s->h+1 + s->speed * 2);
}

variable_types: struct sprite { int x; int y; int dir; int speed; int pad; int w; int h; unsigned short *a; } *

variable_types:
spriteP
unsigned short *

variable_types: j int


repo: imrehg/SpinAPI

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

result = do_debug;
if ( do_debug )
result = debug("usb_set_device", "usb_set_device: board num %d\n", a1);
cur_dev = a1;
return result;
}

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

result = do_debug;
if ( do_debug )
result = debug("usb_set_device", "usb_set_device: board num %d\n", a1);
cur_dev = a1;
return result;
}

original_code:
void
usb_set_device (int board_num)
{
  if(do_debug) _debug(__FUNCTION__, "usb_set_device: board num %d\n", board_num);
  cur_dev = board_num;
}

canonical_original_code:
void usb_set_device(int board_num)
{
  if(do_debug) _debug(__FUNCTION__, "usb_set_device: board num %d\n", board_num);
  cur_dev = board_num;
}


decompiled_code:
__int64 __fastcall usb_write_address(int a1)
{
char v2; // [rsp+Bh] [rbp-Dh] BYREF
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
v2 = -63;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v2, 1LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v3, 4LL) < 0 )
{
return 0xFFFFFFFFLL;
}
v2 = 65;
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 1LL, &v2, 1LL) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
char v2; // [rsp+Bh] [rbp-Dh] BYREF
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a1;
v2 = -63;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v2, 1LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v3, 4LL) < 0 )
{
return 0xFFFFFFFFLL;
}
v2 = 65;
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 1LL, &v2, 1LL) >> 31);
}

original_code:
int
usb_write_address (int addr)
{
  char buf;
  char addr_buf[4];
  addr_buf[3] = (addr >> 24) & 0x0FF;
  addr_buf[2] = (addr >> 16) & 0x0FF;
  addr_buf[1] = (addr >> 8) & 0x0FF;
  addr_buf[0] = addr & 0x0FF;
  buf = 0x80 | 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, &buf, 1) < 0)
    return -1;
  if (os_usb_write (cur_dev, 0x02, addr_buf, 4) < 0)
    return -1;
  buf = 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, &buf, 1) < 0)
    return -1;
    return 0;
}

canonical_original_code:
int usb_write_address(int addr)
{
  char buf;
  char addr_buf[4];
  addr_buf[3] = (addr >> 24) & 0x0FF;
  addr_buf[2] = (addr >> 16) & 0x0FF;
  addr_buf[1] = (addr >> 8) & 0x0FF;
  addr_buf[0] = addr & 0x0FF;
  buf = 0x80 | 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, &buf, 1) < 0)
    return -1;
  if (os_usb_write (cur_dev, 0x02, addr_buf, 4) < 0)
    return -1;
  buf = 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, &buf, 1) < 0)
    return -1;
    return 0;
}

variable_types: addr int

variable_types: buf char


decompiled_code:
__int64 __fastcall usb_write_data(__int64 a1, int a2)
{
__int64 v2; // r13
int v3; // er12
int v4; // er14
int v5; // er14
__int64 v6; // rbp
int v7; // ebx

v2 = a1;
v3 = 4 * a2;
v4 = 4 * a2 + 511;
if ( (a2 & 0x20000000) == 0 )
v4 = 4 * a2;
v5 = v4 >> 9;
if ( v3 > 511 )
{
v6 = a1;
v7 = 0;
do
{
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v6, 512LL) < 0 )
return 0xFFFFFFFFLL;
++v7;
v6 += 512LL;
}
while ( v5 > v7 );
v2 = (v7 << 9) + a1;
}
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 2LL, v2, (unsigned int)(v3 % 512)) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // r13
int v3; // er12
int v4; // er14
int v5; // er14
__int64 v6; // rbp
int v7; // ebx

v2 = a1;
v3 = 4 * a2;
v4 = 4 * a2 + 511;
if ( (a2 & 0x20000000) == 0 )
v4 = 4 * a2;
v5 = v4 >> 9;
if ( v3 > 511 )
{
v6 = a1;
v7 = 0;
do
{
if ( (int)os_usb_write((unsigned int)cur_dev, 2LL, v6, 512LL) < 0 )
return 0xFFFFFFFFLL;
++v7;
v6 += 512LL;
}
while ( v5 > v7 );
v2 = (v7 << 9) + a1;
}
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 2LL, v2, (unsigned int)(v3 % 512)) >> 31);
}

original_code:
int
usb_write_data (int *data, int nData)
{
  char *data_buf = (char *) data;
  int num_bytes;
  int num_xfers;
  int excess_xfer;
  int i = 0;
  num_bytes = nData * sizeof (int);
  num_xfers = (int) (num_bytes / 512);
  excess_xfer = num_bytes - 512 * num_xfers;
  for (i = 0; i < num_xfers; ++i)
    if (os_usb_write (cur_dev, 0x02, data_buf + 512 * i, 512) < 0)
      return -1;
  if (os_usb_write (cur_dev, 0x02, data_buf + 512 * i, excess_xfer) < 0)
    return -1;
  return 0;
}

canonical_original_code:
int usb_write_data(int *data, int nData)
{
  char *data_buf = (char *) data;
  int num_bytes;
  int num_xfers;
  int excess_xfer;
  int i = 0;
  num_bytes = nData * sizeof (int);
  num_xfers = (int) (num_bytes / 512);
  excess_xfer = num_bytes - 512 * num_xfers;
  for (i = 0; i < num_xfers; ++i)
    if (os_usb_write (cur_dev, 0x02, data_buf + 512 * i, 512) < 0)
      return -1;
  if (os_usb_write (cur_dev, 0x02, data_buf + 512 * i, excess_xfer) < 0)
    return -1;
  return 0;
}

variable_types:
data
int *

variable_types: nData int


decompiled_code:
__int64 __fastcall usb_reset_gpif(__int64 a1)
{
char v2[11]; // [rsp+Dh] [rbp-Bh] BYREF

v2[0] = 81;
return (unsigned int)((int)os_usb_write(a1, 1LL, v2, 1LL) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
char v2[11]; // [rsp+Dh] [rbp-Bh] BYREF

v2[0] = 81;
return (unsigned int)((int)os_usb_write(a1, 1LL, v2, 1LL) >> 31);
}

original_code:
int
usb_reset_gpif (int dev_num)
{
  char buf[3];
  buf[0] = 0x40 | 0x10 | 0x01;
  if (os_usb_write (dev_num, 0x01, buf, 1) < 0)
    return -1;
  return 0;
}

canonical_original_code:
int usb_reset_gpif(int dev_num)
{
  char buf[3];
  buf[0] = 0x40 | 0x10 | 0x01;
  if (os_usb_write (dev_num, 0x01, buf, 1) < 0)
    return -1;
  return 0;
}

variable_types:
buf
char [3]


decompiled_code:
__int64 __fastcall setup_xfer(int a1, __int16 a2)
{
char v3; // [rsp+9h] [rbp-Fh] BYREF
__int16 v4; // [rsp+Ah] [rbp-Eh]
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a1;
v4 = a2;
v3 = -63;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v3, 3LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v5, 4LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v5, 2LL) < 0 )
{
return 0xFFFFFFFFLL;
}
v3 = 65;
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 1LL, &v3, 3LL) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, __int16 a2)
{
char v3; // [rsp+9h] [rbp-Fh] BYREF
__int16 v4; // [rsp+Ah] [rbp-Eh]
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a1;
v4 = a2;
v3 = -63;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v3, 3LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v5, 4LL) < 0
|| (int)os_usb_write((unsigned int)cur_dev, 2LL, v5, 2LL) < 0 )
{
return 0xFFFFFFFFLL;
}
v3 = 65;
return (unsigned int)((int)os_usb_write((unsigned int)cur_dev, 1LL, &v3, 3LL) >> 31);
}

original_code:
int
setup_xfer (unsigned int addr, unsigned int packet_len)
{
  char buf[3];
  char addr_buf[4];
  addr_buf[3] = (addr >> 24) & 0x0FF;
  addr_buf[2] = (addr >> 16) & 0x0FF;
  addr_buf[1] = (addr >> 8) & 0x0FF;
  addr_buf[0] = addr & 0x0FF;
  buf[2] = (packet_len >> 8) & 0x0FF;
  buf[1] = packet_len & 0x0FF;
  buf[0] = 0x80 | 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, buf, 3) < 0)
    return -1;
      if (os_usb_write (cur_dev, 0x02, addr_buf, 4) < 0)
        return -1;
  else
    if (os_usb_write (cur_dev, 0x02, addr_buf, 2) < 0)
      return -1;
  buf[0] = 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, buf, 3) < 0)
    return -1;
  return 0;
}

canonical_original_code:
int setup_xfer(unsigned int addr, unsigned int packet_len)
{
  char buf[3];
  char addr_buf[4];
  addr_buf[3] = (addr >> 24) & 0x0FF;
  addr_buf[2] = (addr >> 16) & 0x0FF;
  addr_buf[1] = (addr >> 8) & 0x0FF;
  addr_buf[0] = addr & 0x0FF;
  buf[2] = (packet_len >> 8) & 0x0FF;
  buf[1] = packet_len & 0x0FF;
  buf[0] = 0x80 | 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, buf, 3) < 0)
    return -1;
      if (os_usb_write (cur_dev, 0x02, addr_buf, 4) < 0)
        return -1;
  else
    if (os_usb_write (cur_dev, 0x02, addr_buf, 2) < 0)
      return -1;
  buf[0] = 0x40 | 0x01;
  if (os_usb_write (cur_dev, 0x01, buf, 3) < 0)
    return -1;
  return 0;
}

variable_types:
addr
unsigned int


decompiled_code:
__int64 __fastcall usb_write_reg(int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a2;
v2 = setup_xfer(a1, 4);
if ( v2 >= 0 )
{
v3 = os_usb_write((unsigned int)cur_dev, 2LL, v5, 4LL);
if ( (v3 & 0x80000000) == 0 )
return 0;
spinerr = "Error doing write";
if ( do_debug )
debug("usb_write_reg", "usb_write_reg: %s\n", "Error doing write");
return v3;
}
v3 = v2;
spinerr = "Error setting up transfer";
if ( !do_debug )
return v3;
debug("usb_write_reg", "usb_write_reg: %s\n", "Error setting up transfer");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
int v5[3]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a2;
v2 = func4(a1, 4);
if ( v2 >= 0 )
{
v3 = os_usb_write((unsigned int)cur_dev, 2LL, v5, 4LL);
if ( (v3 & 0x80000000) == 0 )
return 0;
spinerr = "Error doing write";
if ( do_debug )
debug("usb_write_reg", "usb_write_reg: %s\n", "Error doing write");
return v3;
}
v3 = v2;
spinerr = "Error setting up transfer";
if ( !do_debug )
return v3;
debug("usb_write_reg", "usb_write_reg: %s\n", "Error setting up transfer");
return v3;
}

original_code:
int
usb_write_reg (unsigned int addr, unsigned int data)
{
  int ret;
  ret = setup_xfer (addr, 4);
  if (ret < 0)
    {
      spinerr = "Error setting up transfer";
      if(do_debug) _debug(__FUNCTION__, "usb_write_reg: %s\n", spinerr);
      return ret;
    }
  ret = os_usb_write (cur_dev, 0x02, &data, 4);
  if (ret < 0)
    {
      spinerr = "Error doing write";
      if(do_debug) _debug(__FUNCTION__, "usb_write_reg: %s\n", spinerr);
      return ret;
    }
  return 0;
}

canonical_original_code:
int usb_write_reg(unsigned int addr, unsigned int data)
{
  int ret;
  ret = setup_xfer (addr, 4);
  if (ret < 0)
    {
      spinerr = "Error setting up transfer";
      if(do_debug) _debug(__FUNCTION__, "usb_write_reg: %s\n", spinerr);
      return ret;
    }
  ret = os_usb_write (cur_dev, 0x02, &data, 4);
  if (ret < 0)
    {
      spinerr = "Error doing write";
      if(do_debug) _debug(__FUNCTION__, "usb_write_reg: %s\n", spinerr);
      return ret;
    }
  return 0;
}

variable_types:
data
unsigned int

variable_types: ret int


decompiled_code:
__int64 __fastcall usb_read_reg(int a1, __int64 a2)
{
unsigned int v2; // er13
int v3; // er12

v2 = 0;
while ( 1 )
{
v3 = setup_xfer(a1, 4);
if ( v3 < 0 )
break;
v3 = os_usb_read((unsigned int)cur_dev, 134LL, a2, 4LL);
if ( v3 < 0 )
{
spinerr = "Error doing read";
if ( do_debug )
debug("usb_read_reg", "usb_read_reg: %s (i=%d)\n", "Error doing read", v2);
return (unsigned int)v3;
}
if ( ++v2 == 3 )
return 0;
}
spinerr = "Error setting up transfer";
if ( do_debug )
debug("usb_read_reg", "usb_read_reg: %s (i=%d)\n", "Error setting up transfer", v2);
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, __int64 a2)
{
unsigned int v2; // er13
int v3; // er12

v2 = 0;
while ( 1 )
{
v3 = func4(a1, 4);
if ( v3 < 0 )
break;
v3 = os_usb_read((unsigned int)cur_dev, 134LL, a2, 4LL);
if ( v3 < 0 )
{
spinerr = "Error doing read";
if ( do_debug )
debug("usb_read_reg", "usb_read_reg: %s (i=%d)\n", "Error doing read", v2);
return (unsigned int)v3;
}
if ( ++v2 == 3 )
return 0;
}
spinerr = "Error setting up transfer";
if ( do_debug )
debug("usb_read_reg", "usb_read_reg: %s (i=%d)\n", "Error setting up transfer", v2);
return (unsigned int)v3;
}

original_code:
int
usb_read_reg (unsigned int addr, unsigned int *data)
{
  int ret;
  int i;
  for (i = 0; i < 3; i++)
    {
      ret = setup_xfer (addr, 4);
      if (ret < 0)
 {
   spinerr = "Error setting up transfer";
   if(do_debug) _debug(__FUNCTION__, "usb_read_reg: %s (i=%d)\n", spinerr, i);
   return ret;
 }
      ret = os_usb_read (cur_dev, 0x86, data, 4);
      if (ret < 0)
 {
   spinerr = "Error doing read";
   if(do_debug) _debug(__FUNCTION__, "usb_read_reg: %s (i=%d)\n", spinerr, i);
   return ret;
 }
    }
  return 0;
}

canonical_original_code:
int usb_read_reg(unsigned int addr, unsigned int *data)
{
  int ret;
  int i;
  for (i = 0; i < 3; i++)
    {
      ret = setup_xfer (addr, 4);
      if (ret < 0)
 {
   spinerr = "Error setting up transfer";
   if(do_debug) _debug(__FUNCTION__, "usb_read_reg: %s (i=%d)\n", spinerr, i);
   return ret;
 }
      ret = os_usb_read (cur_dev, 0x86, data, 4);
      if (ret < 0)
 {
   spinerr = "Error doing read";
   if(do_debug) _debug(__FUNCTION__, "usb_read_reg: %s (i=%d)\n", spinerr, i);
   return ret;
 }
    }
  return 0;
}

variable_types:
data
unsigned int *


decompiled_code:
__int64 __fastcall usb_read_ram(int a1, int a2, int a3, char *a4)
{
int v7; // ebp
signed int v8; // ebx
int v9; // er13
char *v10; // rcx
char *v11; // rdi
int v12; // eax
char *v13; // rcx
char *v14; // rdi
int v15; // ebp
__int16 v16; // [rsp+Dh] [rbp-22Bh] BYREF
char v17; // [rsp+Fh] [rbp-229h]
__int64 src; // [rsp+10h] [rbp-228h] BYREF
__int64 v19; // [rsp+18h] [rbp-220h] BYREF
__int64 v20; // [rsp+208h] [rbp-30h]

if ( a1 != 4096 )
{
if ( a1 == 0x2000 )
{
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: DDRSRAM is write only\n");
return 0xFFFFFFFFLL;
}
}
else if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: invalid RAM bank\n");
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}
usb_write_reg(18, a2);
v7 = a3 + 8;
v8 = (unsigned int)(a3 + 8) >> 9;
if ( (a3 & 7) != 0 )
{
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: length is not multiple of line size\n");
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}
v17 = 2;
v9 = 0;
v16 = 66;
setup_xfer(4096, 512);
while ( 1 )
{
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v16, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) < 0 )
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_read_ram", "usb_read_ram: read not successful (xfer %d)\n", (unsigned int)v9);
return 0xFFFFFFFFLL;
}
if ( v9 <= 1 )
goto LABEL_8;
if ( v9 == 2 )
break;
v10 = a4;
v11 = (char *)((unsigned __int64)(a4 + 8) & 0xFFFFFFFFFFFFFFF8LL);
a4 += 512;
*((_QWORD *)a4 - 64) = src;
*((_QWORD *)a4 - 1) = v20;
qmemcpy(v11, (char *)&src - (v10 - v11), 8LL * ((unsigned int)((_DWORD)v10 - (_DWORD)v11 + 512) >> 3));
LABEL_8:
v12 = v9 + 1;
if ( v8 < v9 )
goto LABEL_15;
LABEL_9:
v9 = v12;
}
v13 = a4;
v14 = (char *)((unsigned __int64)(a4 + 8) & 0xFFFFFFFFFFFFFFF8LL);
a4 += 504;
*((_QWORD *)a4 - 63) = v19;
*((_QWORD *)a4 - 1) = v20;
v12 = 3;
qmemcpy(v14, (char *)&v19 - (v13 - v14), 8LL * ((unsigned int)((_DWORD)v13 - (_DWORD)v14 + 504) >> 3));
if ( v8 >= 2 )
goto LABEL_9;
LABEL_15:
v15 = v7 - (v8 << 9);
if ( !v15 )
goto LABEL_19;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v16, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) >= 0 )
{
memcpy(a4, &src, v15);
LABEL_19:
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) < 0 )
{
if ( do_debug )
debug("usb_read_ram", "usb_read_ram: read not successful (clear 1)\n");
}
else
{
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) >= 0 )
{
reg_read(3LL);
reg_read(3LL);
return 0LL;
}
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: read not successful (clear 2)\n");
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_read_ram", "usb_read_ram: read not successful (excess)\n");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, int a3, char *a4)
{
int v7; // ebp
signed int v8; // ebx
int v9; // er13
char *v10; // rcx
char *v11; // rdi
int v12; // eax
char *v13; // rcx
char *v14; // rdi
int v15; // ebp
__int16 v16; // [rsp+Dh] [rbp-22Bh] BYREF
char v17; // [rsp+Fh] [rbp-229h]
__int64 src; // [rsp+10h] [rbp-228h] BYREF
__int64 v19; // [rsp+18h] [rbp-220h] BYREF
__int64 v20; // [rsp+208h] [rbp-30h]

if ( a1 != 4096 )
{
if ( a1 == 0x2000 )
{
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: DDRSRAM is write only\n");
return 0xFFFFFFFFLL;
}
}
else if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: invalid RAM bank\n");
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}
func5(18, a2);
v7 = a3 + 8;
v8 = (unsigned int)(a3 + 8) >> 9;
if ( (a3 & 7) != 0 )
{
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: length is not multiple of line size\n");
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}
v17 = 2;
v9 = 0;
v16 = 66;
func4(4096, 512);
while ( 1 )
{
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v16, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) < 0 )
{
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_read_ram", "usb_read_ram: read not successful (xfer %d)\n", (unsigned int)v9);
return 0xFFFFFFFFLL;
}
if ( v9 <= 1 )
goto LABEL_8;
if ( v9 == 2 )
break;
v10 = a4;
v11 = (char *)((unsigned __int64)(a4 + 8) & 0xFFFFFFFFFFFFFFF8LL);
a4 += 512;
*((_QWORD *)a4 - 64) = src;
*((_QWORD *)a4 - 1) = v20;
qmemcpy(v11, (char *)&src - (v10 - v11), 8LL * ((unsigned int)((_DWORD)v10 - (_DWORD)v11 + 512) >> 3));
LABEL_8:
v12 = v9 + 1;
if ( v8 < v9 )
goto LABEL_15;
LABEL_9:
v9 = v12;
}
v13 = a4;
v14 = (char *)((unsigned __int64)(a4 + 8) & 0xFFFFFFFFFFFFFFF8LL);
a4 += 504;
*((_QWORD *)a4 - 63) = v19;
*((_QWORD *)a4 - 1) = v20;
v12 = 3;
qmemcpy(v14, (char *)&v19 - (v13 - v14), 8LL * ((unsigned int)((_DWORD)v13 - (_DWORD)v14 + 504) >> 3));
if ( v8 >= 2 )
goto LABEL_9;
LABEL_15:
v15 = v7 - (v8 << 9);
if ( !v15 )
goto LABEL_19;
if ( (int)os_usb_write((unsigned int)cur_dev, 1LL, &v16, 1LL) < 0 )
return 0xFFFFFFFFLL;
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) >= 0 )
{
memcpy(a4, &src, v15);
LABEL_19:
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) < 0 )
{
if ( do_debug )
debug("usb_read_ram", "usb_read_ram: read not successful (clear 1)\n");
}
else
{
if ( (int)os_usb_read((unsigned int)cur_dev, 134LL, &src, 512LL) >= 0 )
{
reg_read(3LL);
reg_read(3LL);
return 0LL;
}
if ( do_debug )
{
debug("usb_read_ram", "usb_read_ram: read not successful (clear 2)\n");
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}
if ( !do_debug )
return 0xFFFFFFFFLL;
debug("usb_read_ram", "usb_read_ram: read not successful (excess)\n");
return 0xFFFFFFFFLL;
}

original_code:
int
usb_read_ram (unsigned int bank, unsigned int start_addr, unsigned int len,
   char *data)
{
  char inbuf[512];
  int i;
  char *ptr;
  char buf[3];
  int num_xfers;
  int excess_xfer;
  int xfer_size;
  int line_size;
  int amount_xferred = 0;
  switch (bank)
    {
    case 0x1000:
      line_size = 8;
      xfer_size = 512;
      usb_write_reg (0x0012, start_addr);
      break;
    case 0x2000:
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: DDRSRAM is write only\n");
      return -1;
      break;
    default:
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: invalid RAM bank\n");
      return -1;
      break;
    }
  num_xfers = (len + line_size) / xfer_size;
  excess_xfer = (len + line_size) - num_xfers * xfer_size;
  if (len % line_size != 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: length is not multiple of line size\n");
      return -1;
    }
  buf[2] = (xfer_size >> 8) & 0x0FF;
  buf[1] = xfer_size & 0x0FF;
  buf[0] = 0x40 | 0x02;
  ptr = data;
  setup_xfer (bank, xfer_size);
  for (i = 0; i < num_xfers + 2; i++)
    {
      if (os_usb_write (cur_dev, 0x01, buf, 1) < 0)
        return -1;
      if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (xfer %d)\n", i);
   return -1;
 }
      if (i == 0 || i == 1)
 {
   continue;
 }
      if (i == 2)
 {
   memcpy (ptr, inbuf + line_size, xfer_size - line_size);
   ptr += xfer_size - line_size;
 }
      else
 {
   memcpy (ptr, inbuf, xfer_size);
   ptr += xfer_size;
 }
    }
  if (excess_xfer != 0)
    {
      if (os_usb_write (cur_dev, 0x01, buf, 1) < 0)
        return -1;
      if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (excess)\n");
   return -1;
 }
      memcpy (ptr, inbuf, excess_xfer);
    }
  if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (clear 1)\n");
      return -1;
    }
  if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (clear 2)\n");
      return -1;
    }
  reg_read (0x0003);
  reg_read (0x0003);
  return amount_xferred;
}

canonical_original_code:
int usb_read_ram(unsigned int bank, unsigned int start_addr, unsigned int len, char *data)
{
  char inbuf[512];
  int i;
  char *ptr;
  char buf[3];
  int num_xfers;
  int excess_xfer;
  int xfer_size;
  int line_size;
  int amount_xferred = 0;
  switch (bank)
    {
    case 0x1000:
      line_size = 8;
      xfer_size = 512;
      usb_write_reg (0x0012, start_addr);
      break;
    case 0x2000:
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: DDRSRAM is write only\n");
      return -1;
      break;
    default:
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: invalid RAM bank\n");
      return -1;
      break;
    }
  num_xfers = (len + line_size) / xfer_size;
  excess_xfer = (len + line_size) - num_xfers * xfer_size;
  if (len % line_size != 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: length is not multiple of line size\n");
      return -1;
    }
  buf[2] = (xfer_size >> 8) & 0x0FF;
  buf[1] = xfer_size & 0x0FF;
  buf[0] = 0x40 | 0x02;
  ptr = data;
  setup_xfer (bank, xfer_size);
  for (i = 0; i < num_xfers + 2; i++)
    {
      if (os_usb_write (cur_dev, 0x01, buf, 1) < 0)
        return -1;
      if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (xfer %d)\n", i);
   return -1;
 }
      if (i == 0 || i == 1)
 {
   continue;
 }
      if (i == 2)
 {
   memcpy (ptr, inbuf + line_size, xfer_size - line_size);
   ptr += xfer_size - line_size;
 }
      else
 {
   memcpy (ptr, inbuf, xfer_size);
   ptr += xfer_size;
 }
    }
  if (excess_xfer != 0)
    {
      if (os_usb_write (cur_dev, 0x01, buf, 1) < 0)
        return -1;
      if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
 {
   if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (excess)\n");
   return -1;
 }
      memcpy (ptr, inbuf, excess_xfer);
    }
  if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (clear 1)\n");
      return -1;
    }
  if (os_usb_read (cur_dev, 0x86, inbuf, xfer_size) < 0)
    {
      if(do_debug) _debug(__FUNCTION__, "usb_read_ram: read not successful (clear 2)\n");
      return -1;
    }
  reg_read (0x0003);
  reg_read (0x0003);
  return amount_xferred;
}

variable_types:
bank
unsigned int

variable_types:
len
unsigned int

variable_types:
data
char *

variable_types:
inbuf
char [512]

variable_types:
ptr
char *