AcedeMemoriaPedidos

decompiled_code:
void *AcedeMemoriaPedidos()
{
int v0; // eax
int v1; // eax

v0 = DevolveSemaforo(3LL);
v1 = shmget(123, 8 * v0, 950);
if ( v1 < 0 )
_assert_fail(
"( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0",
"memoria.c",
0x1Cu,
"AcedeMemoriaPedidos");
return shmat(v1, 0LL, 0);
}

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

v0 = DevolveSemaforo(3LL);
v1 = shmget(123, 8 * v0, 950);
if ( v1 < 0 )
_assert_fail(
"( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0",
"memoria.c",
0x1Cu,
"AcedeMemoriaPedidos");
return shmat(v1, 0LL, 0);
}

original_code:
Escolha* AcedeMemoriaPedidos()
{
  int id;
  Escolha* escolhas;
  int size;
  size = sizeof( Escolha ) * DevolveSemaforo( 3 );
  ((void) sizeof ((( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0", "memoria.c", 28, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( escolhas = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0) ? 1 : 0), __extension__ ({ if (( escolhas = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0) ; else __assert_fail ("( escolhas = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0", "memoria.c", 29, __extension__ __PRETTY_FUNCTION__); }));
  return escolhas;
}

canonical_original_code:
struct EscolhaStr *AcedeMemoriaPedidos()
{
  int id;
  struct EscolhaStr *escolhas;
  int size;
  size = sizeof( Escolha ) * DevolveSemaforo( 3 );
  ((void) sizeof ((( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0", "memoria.c", 28, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( escolhas = (struct EscolhaStr *) shmat( id, 0, 0 ) ) >= (struct EscolhaStr *) 0) ? 1 : 0), __extension__ ({ if (( escolhas = (struct EscolhaStr *) shmat( id, 0, 0 ) ) >= (struct EscolhaStr *) 0) ; else __assert_fail ("( escolhas = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0", "memoria.c", 29, __extension__ __PRETTY_FUNCTION__); }));
  return escolhas;
}

variable_types: struct EscolhaStr { int pid; int menu; } *

variable_types: size int


AcedeMemoriaMenus

repo: TiagoTT/academic

decompiled_code:
void *AcedeMemoriaMenus()
{
int v0; // ebx
int v1; // eax
int v2; // eax

v0 = DevolveSemaforo(10LL);
v1 = DevolveSemaforo(11LL);
v2 = shmget(321, v1 * v0, 0);
if ( v2 < 0 )
_assert_fail("( id = shmget( SHM_MENUS, size, 0 ) ) >= 0", "memoria.c", 0x2Eu, "AcedeMemoriaMenus");
return shmat(v2, 0LL, 0);
}

canonical_decompiled_code:
void *func2()
{
int v0; // ebx
int v1; // eax
int v2; // eax

v0 = DevolveSemaforo(10LL);
v1 = DevolveSemaforo(11LL);
v2 = shmget(321, v1 * v0, 0);
if ( v2 < 0 )
_assert_fail("( id = shmget( SHM_MENUS, size, 0 ) ) >= 0", "memoria.c", 0x2Eu, "AcedeMemoriaMenus");
return shmat(v2, 0LL, 0);
}

original_code:
Menus AcedeMemoriaMenus()
{
  int id;
  Menus menus;
  int size;
  menus.linhas = DevolveSemaforo( 10 );
  menus.colunas = DevolveSemaforo( 11 );
  size = (menus.linhas * menus.colunas )*sizeof(char);
  ((void) sizeof ((( id = shmget( 321, size, 0 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 321, size, 0 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_MENUS, size, 0 ) ) >= 0", "memoria.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( menus.texto = (char*) shmat( id, 0, 0 ) ) >= (char*)0) ? 1 : 0), __extension__ ({ if (( menus.texto = (char*) shmat( id, 0, 0 ) ) >= (char*)0) ; else __assert_fail ("( menus.texto = (char*) shmat( id, 0, 0 ) ) >= (char*)0", "memoria.c", 47, __extension__ __PRETTY_FUNCTION__); }));
  return menus;
}

canonical_original_code:
struct MenusStr AcedeMemoriaMenus()
{
  int id;
  struct MenusStr menus;
  int size;
  menus.linhas = DevolveSemaforo( 10 );
  menus.colunas = DevolveSemaforo( 11 );
  size = (menus.linhas * menus.colunas )*sizeof(char);
  ((void) sizeof ((( id = shmget( 321, size, 0 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 321, size, 0 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_MENUS, size, 0 ) ) >= 0", "memoria.c", 46, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( menus.texto = (char *) shmat( id, 0, 0 ) ) >= (char *)0) ? 1 : 0), __extension__ ({ if (( menus.texto = (char *) shmat( id, 0, 0 ) ) >= (char *)0) ; else __assert_fail ("( menus.texto = (char*) shmat( id, 0, 0 ) ) >= (char*)0", "memoria.c", 47, __extension__ __PRETTY_FUNCTION__); }));
  return menus;
}

variable_types: id int

variable_types: struct MenusStr { char *texto; int linhas; int colunas; }


GetIndiceEscolha

decompiled_code:
__int64 GetIndiceEscolha()
{
unsigned int v0; // er12

EsperaSemaforo(8LL);
v0 = DevolveSemaforo(9LL);
AssinalaSemaforo(9LL);
AssinalaSemaforo(8LL);
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er12

EsperaSemaforo(8LL);
v0 = DevolveSemaforo(9LL);
AssinalaSemaforo(9LL);
AssinalaSemaforo(8LL);
return v0;
}

original_code:
int GetIndiceEscolha()
{
  int i;
  EsperaSemaforo( 8 );
  i = DevolveSemaforo( 9 );
  AssinalaSemaforo( 9 );
  AssinalaSemaforo( 8 );
  return i;
}

variable_types: i int

return_type: int


EscreveMemoriaPedido

decompiled_code:
int __fastcall EscreveMemoriaPedido(int a1, int a2)
{
int v2; // eax
int v3; // eax
char *v4; // r12
char *v5; // rax
int result; // eax

v2 = DevolveSemaforo(3LL);
v3 = shmget(123, 8 * v2, 950);
if ( v3 < 0 )
_assert_fail(
"( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0",
"memoria.c",
0x3Fu,
"EscreveMemoriaPedido");
v4 = (char *)shmat(v3, 0LL, 0);
v5 = &v4[8 * (int)GetIndiceEscolha()];
*(_DWORD *)v5 = a1;
*((_DWORD *)v5 + 1) = a2;
result = shmdt(v4);
if ( result < 0 )
_assert_fail("shmdt( memoria ) >= 0", "memoria.c", 0x47u, "EscreveMemoriaPedido");
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, int a2)
{
int v2; // eax
int v3; // eax
char *v4; // r12
char *v5; // rax
int result; // eax

v2 = DevolveSemaforo(3LL);
v3 = shmget(123, 8 * v2, 950);
if ( v3 < 0 )
_assert_fail(
"( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0",
"memoria.c",
0x3Fu,
"EscreveMemoriaPedido");
v4 = (char *)shmat(v3, 0LL, 0);
v5 = &v4[8 * (int)func3()];
*(_DWORD *)v5 = a1;
*((_DWORD *)v5 + 1) = a2;
result = shmdt(v4);
if ( result < 0 )
_assert_fail("shmdt( memoria ) >= 0", "memoria.c", 0x47u, "EscreveMemoriaPedido");
return result;
}

original_code:
void EscreveMemoriaPedido(int pid, int escolha)
{
  int id;
  int indice;
  Escolha* memoria;
  int size;
  size = sizeof( Escolha ) * DevolveSemaforo( 3 );
  ((void) sizeof ((( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0", "memoria.c", 63, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( memoria = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0) ? 1 : 0), __extension__ ({ if (( memoria = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0) ; else __assert_fail ("( memoria = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0", "memoria.c", 65, __extension__ __PRETTY_FUNCTION__); }));
  indice = GetIndiceEscolha();
  memoria[ indice ].pid = pid;
  memoria[ indice ].menu = escolha;
  ((void) sizeof ((shmdt( memoria ) >= 0) ? 1 : 0), __extension__ ({ if (shmdt( memoria ) >= 0) ; else __assert_fail ("shmdt( memoria ) >= 0", "memoria.c", 71, __extension__ __PRETTY_FUNCTION__); }));
  return;
}

canonical_original_code:
void EscreveMemoriaPedido(int pid, int escolha)
{
  int id;
  int indice;
  struct EscolhaStr *memoria;
  int size;
  size = sizeof( Escolha ) * DevolveSemaforo( 3 );
  ((void) sizeof ((( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ? 1 : 0), __extension__ ({ if (( id = shmget( 123, size, 0666 | 01000 ) ) >= 0) ; else __assert_fail ("( id = shmget( SHM_PEDIDOS, size, 0666 | IPC_CREAT ) ) >= 0", "memoria.c", 63, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((( memoria = (struct EscolhaStr *) shmat( id, 0, 0 ) ) >= (struct EscolhaStr *) 0) ? 1 : 0), __extension__ ({ if (( memoria = (struct EscolhaStr *) shmat( id, 0, 0 ) ) >= (struct EscolhaStr *) 0) ; else __assert_fail ("( memoria = (Escolha*) shmat( id, 0, 0 ) ) >= (Escolha*) 0", "memoria.c", 65, __extension__ __PRETTY_FUNCTION__); }));
  indice = GetIndiceEscolha();
  memoria[ indice ].pid = pid;
  memoria[ indice ].menu = escolha;
  ((void) sizeof ((shmdt( memoria ) >= 0) ? 1 : 0), __extension__ ({ if (shmdt( memoria ) >= 0) ; else __assert_fail ("shmdt( memoria ) >= 0", "memoria.c", 71, __extension__ __PRETTY_FUNCTION__); }));
  return;
}

variable_types: pid int

variable_types: escolha int

variable_types: indice int

return_type: void

user_defined_types: struct EscolhaStr { int pid; int menu; }


decompiled_code:
__int64 __fastcall stack_pop(__int64 **a1)
{
__int64 *v1; // r8
__int64 v2; // r12

v1 = *a1;
if ( *a1 )
{
v2 = *v1;
*a1 = (__int64 *)v1[1];
free(v1);
}
else
{
v2 = 0LL;
fwrite("Atempt to POP from an empty stack.\n", 1uLL, 0x23uLL, stderr);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 **a1)
{
__int64 *v1; // r8
__int64 v2; // r12

v1 = *a1;
if ( *a1 )
{
v2 = *v1;
*a1 = (__int64 *)v1[1];
free(v1);
}
else
{
v2 = 0LL;
fwrite("Atempt to POP from an empty stack.\n", 1uLL, 0x23uLL, stderr);
}
return v2;
}

original_code:
void* stack_pop( Item** base ) {
 Item* topo;
 void* dados;
 if( ((void *)0) == *base) {
  fprintf(stderr,"Atempt to POP from an empty stack.\n");
  return ((void *)0);
 }
 topo = (*base);
 dados = topo -> dados;
 (*base) = topo -> next;
 free( topo );
 return dados;
}

canonical_original_code:
void *stack_pop(struct ItemStr **base) {
 struct ItemStr *topo;
 void *dados;
 if( ((void *)0) == *base) {
  fprintf(stderr,"Atempt to POP from an empty stack.\n");
  return ((void *)0);
 }
 topo = (*base);
 dados = topo -> dados;
 (*base) = topo -> next;
 free( topo );
 return dados;
}

variable_types: struct ItemStr { void *dados; struct ItemStr *next; } **

variable_types:
dados
void *

return_type: void *


decompiled_code:
__int64 __fastcall stack_find(_QWORD *a1, __int64 (__fastcall *a2)(__int64, _QWORD), __int64 a3)
{
_QWORD *v4; // rbx
int i; // eax

if ( !a1 )
return 0LL;
v4 = a1;
for ( i = a2(a3, *a1); ; i = a2(a3, *v4) )
{
if ( !i )
return *v4;
v4 = (_QWORD *)v4[1];
if ( !v4 )
break;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 (__fastcall *a2)(__int64, _QWORD), __int64 a3)
{
_QWORD *v4; // rbx
int i; // eax

if ( !a1 )
return 0LL;
v4 = a1;
for ( i = a2(a3, *a1); ; i = a2(a3, *v4) )
{
if ( !i )
return *v4;
v4 = (_QWORD *)v4[1];
if ( !v4 )
break;
}
return 0LL;
}

original_code:
void* stack_find( Item* base, int (*comparador)(), void* ref ) {
 Item* it;
 for( it = base ; ((void *)0) != it ; it = it -> next ) {
  if( !comparador( ref, it -> dados ) ) {
   return it -> dados;
  }
 }
 return ((void *)0);
}

canonical_original_code:
void *stack_find(struct ItemStr *base, int (*comparador)(), void *ref) {
 struct ItemStr *it;
 for( it = base ; ((void *)0) != it ; it = it -> next ) {
  if( !comparador( ref, it -> dados ) ) {
   return it -> dados;
  }
 }
 return ((void *)0);
}

variable_types: struct ItemStr { void *dados; struct ItemStr *next; } *

variable_types:
comparador
int (*)()

variable_types:
ref
void *


ApagaFila

decompiled_code:
void __fastcall ApagaFila(void *ptr)
{
__int64 v1; // rax
__int64 v2; // rbx

if ( *((_DWORD *)ptr + 2) )
{
v1 = 0LL;
do
{
v2 = v1 + 1;
free(*(void **)(*(_QWORD *)ptr + 8 * (v1 + 1)));
v1 = v2;
}
while ( *((_DWORD *)ptr + 2) >= (unsigned int)(v2 + 1) );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
__int64 v1; // rax
__int64 v2; // rbx

if ( *((_DWORD *)ptr + 2) )
{
v1 = 0LL;
do
{
v2 = v1 + 1;
free(*(void **)(*(_QWORD *)ptr + 8 * (v1 + 1)));
v1 = v2;
}
while ( *((_DWORD *)ptr + 2) >= (unsigned int)(v2 + 1) );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void ApagaFila( pq fila ) {
  int i;
  for( i = 1 ; i <= fila->n_items ; i++ ) {
    free( fila -> table[i] );
  }
  free( fila -> table );
  free( fila );
  return;
}

canonical_original_code:
void ApagaFila(struct FilaPrioritariaStr *fila) {
  int i;
  for( i = 1 ; i <= fila->n_items ; i++ ) {
    free( fila -> table[i] );
  }
  free( fila -> table );
  free( fila );
  return;
}

variable_types: struct FilaPrioritariaStr { void **table; unsigned int n_items; unsigned int size; } *


ColocaNaFila

original_code:
int ColocaNaFila( pq fila, void* item ) {
  fila -> n_items ++;
  fila -> table[ fila -> n_items ] = item;
  FixUp( fila -> n_items , fila );
  return fila -> size - fila -> n_items;
}

canonical_original_code:
int ColocaNaFila(struct FilaPrioritariaStr *fila, void *item) {
  fila -> n_items ++;
  fila -> table[ fila -> n_items ] = item;
  FixUp( fila -> n_items , fila );
  return fila -> size - fila -> n_items;
}

variable_types:
item
void *

user_defined_types: struct FilaPrioritariaStr { void **table; unsigned int n_items; unsigned int size; }


RetiraDaFila

original_code:
void* RetiraDaFila( pq fila ) {
  void* top;
  top = fila -> table[ 1 ];
  fila -> table[ 1 ] = fila -> table[ fila -> n_items ];
  fila -> n_items --;
  FixDown( 1, fila );
  return top;
}

canonical_original_code:
void *RetiraDaFila(struct FilaPrioritariaStr *fila) {
  void *top;
  top = fila -> table[ 1 ];
  fila -> table[ 1 ] = fila -> table[ fila -> n_items ];
  fila -> n_items --;
  FixDown( 1, fila );
  return top;
}

variable_types:
top
void *


IniciaBuffer

decompiled_code:
_QWORD *IniciaBuffer()
{
void *v0; // rax
_QWORD *result; // rax

v0 = malloc(0x80010uLL);
if ( !v0 )
{
fwrite("ERRO: malloc() = NULL in IniciaBuffer\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
result = memset(v0, 0, 0x80000uLL);
result[0x10000] = 0LL;
result[65537] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func6()
{
void *v0; // rax
_QWORD *result; // rax

v0 = malloc(0x80010uLL);
if ( !v0 )
{
fwrite("ERRO: malloc() = NULL in IniciaBuffer\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
result = memset(v0, 0, 0x80000uLL);
result[0x10000] = 0LL;
result[65537] = 0LL;
return result;
}

original_code:
bf IniciaBuffer( void ) {
  bf buf;
  buf = (bf) malloc( sizeof(BufferFIFO) );
  if( ((void *)0) == buf ){
    fprintf(stderr, "ERRO: malloc() = NULL in IniciaBuffer\n");
    exit( -1 );
  }
  memset( buf -> table, (int)((void *)0), sizeof(void*) * 65536 );
  buf -> head = 0;
  buf -> free = 0;
  return buf;
}

canonical_original_code:
struct BufferFIFOStr *IniciaBuffer() {
  struct BufferFIFOStr *buf;
  buf = (struct BufferFIFOStr *) malloc( sizeof(BufferFIFO) );
  if( ((void *)0) == buf ){
    fprintf(stderr, "ERRO: malloc() = NULL in IniciaBuffer\n");
    exit( -1 );
  }
  memset( buf -> table, (int)((void *)0), sizeof(void *) * 65536 );
  buf -> head = 0;
  buf -> free = 0;
  return buf;
}

variable_types: struct BufferFIFOStr { void *table[65536]; unsigned long head; unsigned long free; } *


ColocaNoFim

decompiled_code:
__int64 __fastcall ColocaNoFim(__int64 a1, __int64 a2)
{
__int64 v2; // rcx
unsigned __int64 v3; // rdx
unsigned __int64 v4; // rcx
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 524296);
v3 = *(_QWORD *)(a1 + 0x80000);
if ( *(_QWORD *)(a1 + 8 * v2) )
{
fprintf(stdout, "BufferFIFO: elemento perdido.(head,free)=(%ld,%ld)\n", v3, v2);
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)(a1 + 8 * v2) = a2;
v4 = (unsigned __int16)(v2 + 1);
*(_QWORD *)(a1 + 524296) = v4;
result = (unsigned int)(v3 - v4);
if ( v4 > v3 )
result = (unsigned int)(v3 + 0x10000 - v4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
__int64 v2; // rcx
unsigned __int64 v3; // rdx
unsigned __int64 v4; // rcx
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 524296);
v3 = *(_QWORD *)(a1 + 0x80000);
if ( *(_QWORD *)(a1 + 8 * v2) )
{
fprintf(stdout, "BufferFIFO: elemento perdido.(head,free)=(%ld,%ld)\n", v3, v2);
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)(a1 + 8 * v2) = a2;
v4 = (unsigned __int16)(v2 + 1);
*(_QWORD *)(a1 + 524296) = v4;
result = (unsigned int)(v3 - v4);
if ( v4 > v3 )
result = (unsigned int)(v3 + 0x10000 - v4);
}
return result;
}

original_code:
int ColocaNoFim( bf buf, void* item ) {
  if( ((void *)0) != buf -> table[ buf -> free ] ) {
    fprintf( stdout, "BufferFIFO: elemento perdido." "(head,free)=(%ld,%ld)\n", buf->head, buf->free );
    return -1;
  }
  buf -> table[ buf -> free ] = item;
  buf -> free = (buf -> free + 1) & 65535;
  return (buf->free > buf->head)?
    ( 65536 - (buf->free - buf->head) ) :
    ( buf->head - buf->free );
}

canonical_original_code:
int ColocaNoFim(struct BufferFIFOStr *buf, void *item) {
  if( ((void *)0) != buf -> table[ buf -> free ] ) {
    fprintf( stdout, "BufferFIFO: elemento perdido." "(head,free)=(%ld,%ld)\n", buf->head, buf->free );
    return -1;
  }
  buf -> table[ buf -> free ] = item;
  buf -> free = (buf -> free + 1) & 65535;
  return (buf->free > buf->head)?
    ( 65536 - (buf->free - buf->head) ) :
    ( buf->head - buf->free );
}

user_defined_types: struct BufferFIFOStr { void *table[65536]; unsigned long head; unsigned long free; }


RemoveDoInicio

decompiled_code:
__int64 __fastcall RemoveDoInicio(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // r12

v1 = *(_QWORD *)(a1 + 0x80000);
v2 = *(_QWORD *)(a1 + 8 * v1);
if ( v2 )
{
*(_QWORD *)(a1 + 8 * v1) = 0LL;
*(_QWORD *)(a1 + 0x80000) = (unsigned __int16)(v1 + 1);
}
else
{
fprintf(stdout, "Buffer vazio in RemoveDoInicio.(head,free)=(%ld,%ld)\n", v1, *(_QWORD *)(a1 + 524296));
}
return v2;
}

original_code:
void* RemoveDoInicio( bf buf ) {
  void* item;
  item = buf -> table[ buf -> head ];
  if( ((void *)0) == item ){
    fprintf( stdout, "Buffer vazio in RemoveDoInicio." "(head,free)=(%ld,%ld)\n", buf->head, buf->free );
    return ((void *)0);
  }
  buf -> table[ buf -> head ] = ((void *)0);
  buf -> head = (buf -> head + 1) & 65535;
  return item;
}

canonical_original_code:
void *RemoveDoInicio(struct BufferFIFOStr *buf) {
  void *item;
  item = buf -> table[ buf -> head ];
  if( ((void *)0) == item ){
    fprintf( stdout, "Buffer vazio in RemoveDoInicio." "(head,free)=(%ld,%ld)\n", buf->head, buf->free );
    return ((void *)0);
  }
  buf -> table[ buf -> head ] = ((void *)0);
  buf -> head = (buf -> head + 1) & 65535;
  return item;
}


ConsultaInicio

decompiled_code:
__int64 __fastcall ConsultaInicio(__int64 a1)
{
return *(_QWORD *)(a1 + 8LL * *(_QWORD *)(a1 + 0x80000));
}

original_code:
void* ConsultaInicio( bf buf ) {
  return buf -> table[ buf -> head ];
}

canonical_original_code:
void *ConsultaInicio(struct BufferFIFOStr *buf) {
  return buf -> table[ buf -> head ];
}


ApagaBuffer

decompiled_code:
void __fastcall ApagaBuffer(void *ptr)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = ptr;
LODWORD(v2) = 0;
do
v2 = (unsigned int)v2 - ((*v1++ == 0LL) - 1);
while ( v1 != (_QWORD *)((char *)ptr + 0x80000) );
if ( (_DWORD)v2 )
fprintf(stderr, "Possivel memory leak in ApagaBuffer: %d items\n", v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
_QWORD *v1; // rax
__int64 v2; // rdx

v1 = ptr;
LODWORD(v2) = 0;
do
v2 = (unsigned int)v2 - ((*v1++ == 0LL) - 1);
while ( v1 != (_QWORD *)((char *)ptr + 0x80000) );
if ( (_DWORD)v2 )
fprintf(stderr, "Possivel memory leak in ApagaBuffer: %d items\n", v2);
free(ptr);
}

original_code:
void ApagaBuffer( bf buf ) {
  int i,k;
  for( i = 0, k = 0 ; i < 65536 ; i++ )
    if( ((void *)0) != buf->table[i] )
      k++;
  if( k )
    fprintf( stderr, "Possivel memory leak in ApagaBuffer: %d items\n", k );
  free( buf );
}

canonical_original_code:
void ApagaBuffer(struct BufferFIFOStr *buf) {
  int i; int k;
  for( i = 0, k = 0 ; i < 65536 ; i++ )
    if( ((void *)0) != buf->table[i] )
      k++;
  if( k )
    fprintf( stderr, "Possivel memory leak in ApagaBuffer: %d items\n", k );
  free( buf );
}

variable_types: k int


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

do
{
Escreve_Menu_General();
result = getchar();
}
while ( (_BYTE)result != 113 );
return result;
}

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

do
{
func0();
result = getchar();
}
while ( (_BYTE)result != 113 );
return result;
}

original_code:
void Menu_Gen(void)
{
 char opcao;
 while(1)
 {
  Escreve_Menu_General();
  opcao = getchar();
  if( 'q' == opcao )
    {
      return;
    }
 }
}

canonical_original_code:
void Menu_Gen()
{
 char opcao;
 while(1)
 {
  Escreve_Menu_General();
  opcao = getchar();
  if( 'q' == opcao )
    {
      return;
    }
 }
}

variable_types: opcao char


decompiled_code:
void *hash_init()
{
void *result; // rax

result = calloc(0x50uLL, 1uLL);
if ( !result )
{
fwrite("Memory allocation error at HASH_INIT.\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
return result;
}

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

result = calloc(0x50uLL, 1uLL);
if ( !result )
{
fwrite("Memory allocation error at HASH_INIT.\n", 1uLL, 0x26uLL, stderr);
exit(-1);
}
return result;
}

original_code:
Item** hash_init(void) {
 Item** tabela;
 tabela = (Item**)malloc( 10 * sizeof(Item*) );
 if( ((void *)0) == tabela ) {
  fprintf(stderr, "Memory allocation error at HASH_INIT.\n");
  exit(-1);
 }
 memset( tabela, 0, 10 * sizeof(Item*) );
 return tabela;
}

canonical_original_code:
struct ItemStr **hash_init() {
 struct ItemStr **tabela;
 tabela = (struct ItemStr **)malloc( 10 * sizeof(struct ItemStr *) );
 if( ((void *)0) == tabela ) {
  fprintf(stderr, "Memory allocation error at HASH_INIT.\n");
  exit(-1);
 }
 memset( tabela, 0, 10 * sizeof(struct ItemStr *) );
 return tabela;
}

return_type: struct ItemStr { void *dados; struct ItemStr *next; } **


decompiled_code:
void __fastcall hash_kill(void *ptr, void (__fastcall *a2)(__int64))
{
_QWORD *v2; // rbx
__int64 v3; // rax

v2 = ptr;
do
{
while ( *v2 )
{
v3 = stack_pop(v2);
a2(v3);
}
++v2;
}
while ( v2 != (_QWORD *)((char *)ptr + 80) );
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr, void (__fastcall *a2)(__int64))
{
_QWORD *v2; // rbx
__int64 v3; // rax

v2 = ptr;
do
{
while ( *v2 )
{
v3 = stack_pop(v2);
a2(v3);
}
++v2;
}
while ( v2 != (_QWORD *)((char *)ptr + 80) );
free(ptr);
}

original_code:
void hash_kill( Item** tabela, void (*matador)() ) {
 int i;
 for( i = 0 ; i < 10 ; i++ ) {
  while( ((void *)0) != tabela[ i ] ) {
   matador( stack_pop( tabela + i ) );
  }
 }
 free( tabela );
 return;
}

canonical_original_code:
void hash_kill(struct ItemStr **tabela, void (*matador)()) {
 int i;
 for( i = 0 ; i < 10 ; i++ ) {
  while( ((void *)0) != tabela[ i ] ) {
   matador( stack_pop( tabela + i ) );
  }
 }
 free( tabela );
 return;
}

variable_types:
matador
void (*)()

user_defined_types: struct ItemStr { void *dados; struct ItemStr *next; }


decompiled_code:
__int64 __fastcall hash_function(char **a1)
{
_BYTE *v1; // rdx
int v2; // eax
unsigned int v3; // ecx

v1 = *a1;
v2 = **a1;
if ( !**a1 )
return 0LL;
v3 = 0;
do
{
++v1;
v3 += v2;
v2 = (char)*v1;
}
while ( *v1 );
return v3 % 0xA;
}

canonical_decompiled_code:
__int64 __fastcall func2(char **a1)
{
_BYTE *v1; // rdx
int v2; // eax
unsigned int v3; // ecx

v1 = *a1;
v2 = **a1;
if ( !**a1 )
return 0LL;
v3 = 0;
do
{
++v1;
v3 += v2;
v2 = (char)*v1;
}
while ( *v1 );
return v3 % 0xA;
}

original_code:
unsigned int hash_function( void* dados ) {
 unsigned int val;
 char* str;
 str = ((identificador*)dados) -> nome;
 for( val = 0 ; '\0' != *str ; str++ ) {
  val += *str;
 }
 return val % 10;
}

canonical_original_code:
unsigned int hash_function(void *dados) {
 unsigned int val;
 char *str;
 str = ((struct identificador_str *)dados) -> nome;
 for( val = 0 ; '\0' != *str ; str++ ) {
  val += *str;
 }
 return val % 10;
}

variable_types:
val
unsigned int

variable_types:
str
char *

return_type: unsigned int

user_defined_types: struct identificador_str { char *nome; enum tipo t; enum boolean init; enum boolean use; }

user_defined_types: enum tipo { inteiro=0, semaforo=1, canal_limitado=2, canal_ilimitado=3, lista=4, constante=5 }


decompiled_code:
__int64 __fastcall hash_insert(__int64 a1, char **a2)
{
unsigned int v2; // eax
__int64 v3; // r8

v2 = hash_function(a2);
return stack_push(v3 + 8LL * v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char **a2)
{
unsigned int v2; // eax
__int64 v3; // r8

v2 = func2(a2);
return stack_push(v3 + 8LL * v2);
}

original_code:
void hash_insert( Item** tabela, void* dados ) {
 stack_push( tabela + hash_function( dados ) , dados );
 return;
}

canonical_original_code:
void hash_insert(struct ItemStr **tabela, void *dados) {
 stack_push( tabela + hash_function( dados ) , dados );
 return;
}


decompiled_code:
__int64 __fastcall hash_find(__int64 a1, __int64 a2, char **a3)
{
unsigned int v3; // eax
__int64 v4; // r9
__int64 v5; // r8

v3 = hash_function(a3);
return stack_find(*(_QWORD *)(v4 + 8LL * v3), a2, v5);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, char **a3)
{
unsigned int v3; // eax
__int64 v4; // r9
__int64 v5; // r8

v3 = func2(a3);
return stack_find(*(_QWORD *)(v4 + 8LL * v3), a2, v5);
}

original_code:
void* hash_find( Item** tabela, int (*comparador)(), void* ref ) {
 return stack_find( tabela[ hash_function( ref ) ], comparador, ref );
}

canonical_original_code:
void *hash_find(struct ItemStr **tabela, int (*comparador)(), void *ref) {
 return stack_find( tabela[ hash_function( ref ) ], comparador, ref );
}


simula

decompiled_code:
__int64 __fastcall simula(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rsi

puts("Simulacao Logica: INICIO ");
if ( *(int *)a1 >= 0 )
{
v1 = 0LL;
do
{
while ( (unsigned __int8)(*(_BYTE *)(*(_QWORD *)(a1 + 8) + 72 * v1 + 36) - 110) > 1u )
{
if ( *(_DWORD *)a1 < (int)++v1 )
goto LABEL_6;
}
v2 = (unsigned int)v1++;
calculaValor(a1, v2);
}
while ( *(_DWORD *)a1 >= (int)v1 );
}
LABEL_6:
puts("Simulacao Logica: FIM ");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rsi

puts("Simulacao Logica: INICIO ");
if ( *(int *)a1 >= 0 )
{
v1 = 0LL;
do
{
while ( (unsigned __int8)(*(_BYTE *)(*(_QWORD *)(a1 + 8) + 72 * v1 + 36) - 110) > 1u )
{
if ( *(_DWORD *)a1 < (int)++v1 )
goto LABEL_6;
}
v2 = (unsigned int)v1++;
calculaValor(a1, v2);
}
while ( *(_DWORD *)a1 >= (int)v1 );
}
LABEL_6:
puts("Simulacao Logica: FIM ");
return 0LL;
}

original_code:
int simula(Circuito* circuito)
{
  int i;
  printf("Simulacao Logica: INICIO \n");
  for( i = 0 ; i <= circuito->nNodes ; i++ )
    {
      if( 'n' == circuito->vectorNodes[ i ].flag ||
   'o' == circuito->vectorNodes[ i ].flag )
 {
   calculaValor( circuito, i );
 }
    }
  printf("Simulacao Logica: FIM \n");
  return 0;
}

canonical_original_code:
int simula(struct CircuitoStr *circuito)
{
  int i;
  printf("Simulacao Logica: INICIO \n");
  for( i = 0 ; i <= circuito->nNodes ; i++ )
    {
      if( 'n' == circuito->vectorNodes[ i ].flag ||
   'o' == circuito->vectorNodes[ i ].flag )
 {
   calculaValor( circuito, i );
 }
    }
  printf("Simulacao Logica: FIM \n");
  return 0;
}

variable_types: struct CircuitoStr { int nNodes; struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; } *vectorNodes; int nMax; int nInputs; } *

variable_types: i int

user_defined_types: struct CircuitoStr { int nNodes; struct NodeStr *vectorNodes; int nMax; int nInputs; }


deduz

original_code:
int deduz(Circuito* circuito)
{
  int i, k, n;
  Node* node;
  Node* input;
  int controlo;
  char* sa0;
  char* sa1;
  printf("Deducao de Faltas: INICIO \n");
  for( i = 0 ; i <= circuito->nNodes ; i++ )
    {
      node = circuito->vectorNodes + i;
      if( '\0' != node->flag )
 {
   node->sa0 = (char*)
     malloc( ( circuito->nNodes + 1 ) * sizeof( char ) );
   memset( node->sa0, 0, ( circuito->nNodes + 1 ) * sizeof( char ) );
   node->sa1 = (char*)
     malloc( ( circuito->nNodes + 1 ) * sizeof( char ) );
   memset( node->sa1, 0, ( circuito->nNodes + 1) * sizeof( char ) );
   node->leituras = 0;
   controlo = valorControlo( circuito, i );
   if( 4 == node->tipo || 5 == node->tipo )
     {
       sa0 = (char*)
  malloc( ( i ) * sizeof( char ) );
       memset( node->sa0, 0, ( i ) * sizeof( char ) );
       sa1 = (char*)
  malloc( ( i ) * sizeof( char ) );
       memset( node->sa0, 0, ( i ) * sizeof( char ) );
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] |= input->sa0[ n ];
        node->sa1[ n ] |= input->sa1[ n ];
      }
  }
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        sa0[ n ] &= input->sa0[ n ];
        sa1[ n ] &= input->sa1[ n ];
        if( input->leituras >= input->fanout )
   {
     free( input->sa0 );
     free( input->sa1 );
   }
      }
  }
       for( k = 0 ; k < node->fanin ; k++ )
  {
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] &= ( 1 & ( ~sa0[ n ] ) );
        node->sa1[ n ] &= ( 1 & ( ~sa1[ n ] ) );
      }
  }
       free( sa0 );
       free( sa1 );
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
   else if( ! haEntradasControlo( circuito, i ) )
     {
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] |= input->sa0[ n ];
        node->sa1[ n ] |= input->sa1[ n ];
      }
    if( input->leituras >= input->fanout )
      {
        free( input->sa0 );
        free( input->sa1 );
      }
  }
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
   else
     {
       memset( node->sa0, 1,
        ( i ) *
        sizeof( char ) );
       memset( node->sa1, 1,
        ( i ) *
        sizeof( char ) );
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    if( controlo == input->valor )
      {
        for( n = 0 ; n < i ; n++ )
   {
     node->sa0[ n ] &= input->sa0[ n ];
     node->sa1[ n ] &= input->sa1[ n ];
   }
      }
    else
      {
        for( n = 0 ; n < i ; n++ )
   {
     node->sa0[ n ] &= ( ( ~input->sa0[ n ] ) );
     node->sa1[ n ] &= ( ( ~input->sa1[ n ] ) );
   }
      }
    if( input->leituras >= input->fanout )
      {
        free( input->sa0 );
        free( input->sa1 );
      }
  }
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
 }
    }
  printf("Deducao de Faltas: FIM \n");
  return 0;
}

canonical_original_code:
int deduz(struct CircuitoStr *circuito)
{
  int i; int k; int n;
  struct NodeStr *node;
  struct NodeStr *input;
  int controlo;
  char *sa0;
  char *sa1;
  printf("Deducao de Faltas: INICIO \n");
  for( i = 0 ; i <= circuito->nNodes ; i++ )
    {
      node = circuito->vectorNodes + i;
      if( '\0' != node->flag )
 {
   node->sa0 = (char *)
     malloc( ( circuito->nNodes + 1 ) * sizeof( char ) );
   memset( node->sa0, 0, ( circuito->nNodes + 1 ) * sizeof( char ) );
   node->sa1 = (char *)
     malloc( ( circuito->nNodes + 1 ) * sizeof( char ) );
   memset( node->sa1, 0, ( circuito->nNodes + 1) * sizeof( char ) );
   node->leituras = 0;
   controlo = valorControlo( circuito, i );
   if( 4 == node->tipo || 5 == node->tipo )
     {
       sa0 = (char *)
  malloc( ( i ) * sizeof( char ) );
       memset( node->sa0, 0, ( i ) * sizeof( char ) );
       sa1 = (char *)
  malloc( ( i ) * sizeof( char ) );
       memset( node->sa0, 0, ( i ) * sizeof( char ) );
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] |= input->sa0[ n ];
        node->sa1[ n ] |= input->sa1[ n ];
      }
  }
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        sa0[ n ] &= input->sa0[ n ];
        sa1[ n ] &= input->sa1[ n ];
        if( input->leituras >= input->fanout )
   {
     free( input->sa0 );
     free( input->sa1 );
   }
      }
  }
       for( k = 0 ; k < node->fanin ; k++ )
  {
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] &= ( 1 & ( ~sa0[ n ] ) );
        node->sa1[ n ] &= ( 1 & ( ~sa1[ n ] ) );
      }
  }
       free( sa0 );
       free( sa1 );
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
   else if( ! haEntradasControlo( circuito, i ) )
     {
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    for( n = 0 ; n < i ; n++ )
      {
        node->sa0[ n ] |= input->sa0[ n ];
        node->sa1[ n ] |= input->sa1[ n ];
      }
    if( input->leituras >= input->fanout )
      {
        free( input->sa0 );
        free( input->sa1 );
      }
  }
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
   else
     {
       memset( node->sa0, 1,
        ( i ) *
        sizeof( char ) );
       memset( node->sa1, 1,
        ( i ) *
        sizeof( char ) );
       for( k = 0 ; k < node->fanin ; k++ )
  {
    input = circuito->vectorNodes + node->inputs[ k ];
    if( controlo == input->valor )
      {
        for( n = 0 ; n < i ; n++ )
   {
     node->sa0[ n ] &= input->sa0[ n ];
     node->sa1[ n ] &= input->sa1[ n ];
   }
      }
    else
      {
        for( n = 0 ; n < i ; n++ )
   {
     node->sa0[ n ] &= ( ( ~input->sa0[ n ] ) );
     node->sa1[ n ] &= ( ( ~input->sa1[ n ] ) );
   }
      }
    if( input->leituras >= input->fanout )
      {
        free( input->sa0 );
        free( input->sa1 );
      }
  }
       switch( node->valor )
  {
  case 0 :
    node->sa0[ i ] = 0;
    node->sa1[ i ] = 1;
    break;
  case 1 :
    node->sa0[ i ] = 1;
    node->sa1[ i ] = 0;
    break;
       case 2 :
           node->sa0[ i ] = 1;
    node->sa1[ i ] = 1;
    break;
  default:
    break;
  }
     }
 }
    }
  printf("Deducao de Faltas: FIM \n");
  return 0;
}

variable_types: struct CircuitoStr { int nNodes; struct NodeStr { char nome[16]; int tipo; int fanin; int *inputs; int valor; char flag; int fanout; char *sa0; char *sa1; int leituras; } *vectorNodes; int nMax; int nInputs; int realNodes; } *

variable_types: n int

variable_types: controlo int

user_defined_types: struct CircuitoStr { int nNodes; struct NodeStr *vectorNodes; int nMax; int nInputs; int realNodes; }


AtendeGenerais

decompiled_code:
void __noreturn AtendeGenerais()
{
char *v0; // rbp
unsigned int arg; // [rsp+4h] [rbp-14h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

CriaMailbox(1LL);
while ( 1 )
{
v0 = (char *)RecebeMailMessage(&arg, 1LL, 1LL);
puts(v0);
free(v0);
RegistaEntrada(arg, 3LL);
pthread_create(newthread, 0LL, ThreadGeneral, &arg);
}
}

canonical_decompiled_code:
void __noreturn func0()
{
char *v0; // rbp
unsigned int arg; // [rsp+4h] [rbp-14h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

CriaMailbox(1LL);
while ( 1 )
{
v0 = (char *)RecebeMailMessage(&arg, 1LL, 1LL);
puts(v0);
free(v0);
RegistaEntrada(arg, 3LL);
pthread_create(newthread, 0LL, ThreadGeneral, &arg);
}
}

original_code:
void *AtendeGenerais(void* argumento)
{
 pthread_t Generais[1];
 char* msg;
 unsigned key;
 CriaMailbox( 1 );
 while(1){
  msg = RecebeMailMessage( &key, 1, 1 );
  puts( msg );
  free( msg );
  RegistaEntrada( key, 3 );
  pthread_create(&Generais[0], ((void *)0), ThreadGeneral, (void*)&key);
 }
 return 0;
}

canonical_original_code:
void *AtendeGenerais(void *argumento)
{
 unsigned long Generais[1];
 char *msg;
 unsigned int key;
 CriaMailbox( 1 );
 while(1){
  msg = RecebeMailMessage( &key, 1, 1 );
  puts( msg );
  free( msg );
  RegistaEntrada( key, 3 );
  pthread_create(&Generais[0], ((void *)0), ThreadGeneral, (void *)&key);
 }
 return 0;
}

variable_types:
argumento
void *

variable_types:
Generais
unsigned long [1]

variable_types:
msg
char *

variable_types:
key
unsigned int


ThreadGeneral

decompiled_code:
void *__fastcall ThreadGeneral(void *a1)
{
char *v1; // r12
_DWORD *v2; // rbx
char *v3; // r12
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = *(_DWORD *)a1;
v1 = (char *)RecebeMailMessage(v5, 1LL, (unsigned int)v5[0]);
puts(v1);
v2 = ProcessaMsgGeneral(v1);
free(v1);
if ( v2 )
{
EsperaSemaforoZero(13LL);
EsperaStock(*(_QWORD *)v2, (unsigned int)v2[2]);
EnviaMailMessage(1LL, (unsigned int)v5[0], 2LL, v1);
}
v3 = (char *)RecebeMailMessage(v5, 1LL, (unsigned int)v5[0]);
puts(v3);
ProcessaMsgGeneral(v3);
free(v3);
RegistaSaida((unsigned int)v5[0]);
ReiniciaClientes();
DestroiStock();
CriaStock();
DefineSemaforo(9LL, 0LL);
DefineSemaforo(8LL, 1LL);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
char *v1; // r12
_DWORD *v2; // rbx
char *v3; // r12
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = *(_DWORD *)a1;
v1 = (char *)RecebeMailMessage(v5, 1LL, (unsigned int)v5[0]);
puts(v1);
v2 = ProcessaMsgGeneral(v1);
free(v1);
if ( v2 )
{
EsperaSemaforoZero(13LL);
EsperaStock(*(_QWORD *)v2, (unsigned int)v2[2]);
EnviaMailMessage(1LL, (unsigned int)v5[0], 2LL, v1);
}
v3 = (char *)RecebeMailMessage(v5, 1LL, (unsigned int)v5[0]);
puts(v3);
ProcessaMsgGeneral(v3);
free(v3);
RegistaSaida((unsigned int)v5[0]);
ReiniciaClientes();
DestroiStock();
CriaStock();
DefineSemaforo(9LL, 0LL);
DefineSemaforo(8LL, 1LL);
return 0LL;
}

original_code:
void *ThreadGeneral(void * arg)
{
 unsigned key;
 char* msg;
 Pedido* pedido;
 key = *(unsigned*)arg;
 msg = RecebeMailMessage(&key, 1, key);
 puts( msg );
 pedido = ProcessaMsgGeneral( msg );
 free( msg );
 if( ((void *)0) != pedido )
   {
     EsperaSemaforoZero( 13 );
     EsperaStock( pedido->prod, pedido->n_prod );
     EnviaMailMessage(1, key, 2, msg );
   }
 msg = RecebeMailMessage( &key, 1, key );
 puts( msg );
 ProcessaMsgGeneral( msg );
 free( msg );
 RegistaSaida( key );
 ReiniciaClientes();
 DestroiStock();
 CriaStock();
 DefineSemaforo( 9, 0 );
 DefineSemaforo( 8, 1 );
 return 0;
}

canonical_original_code:
void *ThreadGeneral(void *arg)
{
 unsigned int key;
 char *msg;
 struct PedidoStr *pedido;
 key = *(unsigned int *)arg;
 msg = RecebeMailMessage(&key, 1, key);
 puts( msg );
 pedido = ProcessaMsgGeneral( msg );
 free( msg );
 if( ((void *)0) != pedido )
   {
     EsperaSemaforoZero( 13 );
     EsperaStock( pedido->prod, pedido->n_prod );
     EnviaMailMessage(1, key, 2, msg );
   }
 msg = RecebeMailMessage( &key, 1, key );
 puts( msg );
 ProcessaMsgGeneral( msg );
 free( msg );
 RegistaSaida( key );
 ReiniciaClientes();
 DestroiStock();
 CriaStock();
 DefineSemaforo( 9, 0 );
 DefineSemaforo( 8, 1 );
 return 0;
}

variable_types:
arg
void *

variable_types: struct PedidoStr { struct ProdutoStr { char nome[20]; char quantidade[5]; } *prod; int n_prod; } *

user_defined_types: struct PedidoStr { struct ProdutoStr *prod; int n_prod; }


decompiled_code:
__sighandler_t __fastcall INTOrd_signal(__sighandler_t handler)
{
signal(2, handler);
signal(15, handler);
signal(10, handler);
return signal(12, handler);
}

canonical_decompiled_code:
__sighandler_t __fastcall func0(__sighandler_t handler)
{
signal(2, handler);
signal(15, handler);
signal(10, handler);
return signal(12, handler);
}

original_code:
void INTOrd_signal(void (*handler)(int))
{
  signal(2,handler);
  signal(15,handler);
  signal(10, handler);
  signal(12, handler);
}

variable_types:
handler
void (*)(int)


ProcessaArgumentos

decompiled_code:
char *__fastcall ProcessaArgumentos(int a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
size_t v5; // rax
const char *v6; // r14
size_t v7; // r12
size_t v8; // rax
size_t v9; // rax

v2 = (char *)malloc(1uLL);
*v2 = 0;
v3 = v2;
if ( a1 > 1 )
{
v4 = a2 + 8;
do
{
v4 += 8LL;
v5 = strlen(v3);
v6 = *(const char **)(v4 - 8);
v7 = v5;
v8 = strlen(v6);
v3 = (char *)realloc(v3, v7 + v8 + 2);
v9 = strlen(v3);
v3[v9] = 32;
strcpy(&v3[v9 + 1], v6);
}
while ( v4 != a2 + 8LL * (unsigned int)(a1 - 2) + 16 );
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func0(int a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
size_t v5; // rax
const char *v6; // r14
size_t v7; // r12
size_t v8; // rax
size_t v9; // rax

v2 = (char *)malloc(1uLL);
*v2 = 0;
v3 = v2;
if ( a1 > 1 )
{
v4 = a2 + 8;
do
{
v4 += 8LL;
v5 = strlen(v3);
v6 = *(const char **)(v4 - 8);
v7 = v5;
v8 = strlen(v6);
v3 = (char *)realloc(v3, v7 + v8 + 2);
v9 = strlen(v3);
v3[v9] = 32;
strcpy(&v3[v9 + 1], v6);
}
while ( v4 != a2 + 8LL * (unsigned int)(a1 - 2) + 16 );
}
return v3;
}

original_code:
char* ProcessaArgumentos(int argc, char* argv[])
{
 char* msg;
 int i;
 msg = (char*)malloc(sizeof(char));
        msg[0]='\0';
 for( i = 1 ; i < argc ; i++ )
 {
  msg = (char*)realloc( msg, sizeof(char)*
          ( strlen(msg) + strlen(argv[i]) + 2) );
  strcat(msg, " ");
  strcat(msg, argv[i]);
 }
 return msg;
}

canonical_original_code:
char *ProcessaArgumentos(int argc, char *argv[])
{
 char *msg;
 int i;
 msg = (char *)malloc(sizeof(char));
        msg[0]='\0';
 for( i = 1 ; i < argc ; i++ )
 {
  msg = (char *)realloc( msg, sizeof(char)*
          ( strlen(msg) + strlen(argv[i]) + 2) );
  strcat(msg, " ");
  strcat(msg, argv[i]);
 }
 return msg;
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: char *


dumpCounts

repo: Tilka/dvblast

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

if ( mrtg_fh )
{
rewind(mrtg_fh);
fprintf(
mrtg_fh,
"%lld %lld %lld %lld\n",
188 * l_mrtg_packets,
l_mrtg_error_packets,
l_mrtg_seq_err_packets,
l_mrtg_scram_packets);
result = fflush(mrtg_fh);
}
return result;
}

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

if ( mrtg_fh )
{
rewind(mrtg_fh);
fprintf(
mrtg_fh,
"%lld %lld %lld %lld\n",
188 * l_mrtg_packets,
l_mrtg_error_packets,
l_mrtg_seq_err_packets,
l_mrtg_scram_packets);
result = fflush(mrtg_fh);
}
return result;
}

original_code:
static void dumpCounts()
{
    unsigned int multiplier = 1;
    if(mrtg_fh) {
        rewind(mrtg_fh);
        fprintf(mrtg_fh, "%lld %lld %lld %lld\n",
                l_mrtg_packets * 188 * multiplier,
                l_mrtg_error_packets * multiplier,
                l_mrtg_seq_err_packets * multiplier,
                l_mrtg_scram_packets * multiplier);
        fflush(mrtg_fh);
    }
}

variable_types:
multiplier
unsigned int


mrtgAnalyse

decompiled_code:
void __fastcall mrtgAnalyse(__int64 a1)
{
char v1; // bp
char v2; // bl
char v3; // r9
__int64 v4; // r10
__int64 v5; // r11
__int64 v6; // rcx
__int64 v7; // rdx
__int64 v8; // r8
__int16 v9; // ax
__int64 v10; // rax
char v11; // si
char v12; // r13
char v13; // r12
__time_t v14; // rax
struct timeval v15; // [rsp+0h] [rbp-38h] BYREF

if ( !mrtg_fh )
return;
if ( !a1 )
goto LABEL_22;
v1 = 0;
v2 = 0;
v3 = 0;
v4 = l_mrtg_scram_packets;
v5 = l_mrtg_seq_err_packets;
v6 = l_mrtg_error_packets;
v7 = l_mrtg_packets + 1;
do
{
v8 = v7;
if ( *(_BYTE *)a1 == 71 )
{
v9 = *(unsigned __int8 *)(a1 + 1);
if ( (v9 & 0x80u) == 0 )
{
v10 = *(unsigned __int8 *)(a1 + 2) | (v9 << 8) & 0x1F00u;
if ( (_DWORD)v10 == 0x1FFF )
{
LABEL_14:
a1 = *(_QWORD *)(a1 + 208);
goto LABEL_15;
}
v11 = *(_BYTE *)(a1 + 3);
if ( (v11 & 0xC0) != 0 )
{
++v4;
v2 = 1;
}
v12 = i_pid_seq[v10];
v13 = v11 & 0xF;
if ( v12 != -1 )
{
if ( (*(_BYTE *)(a1 + 3) & 0x10) == 0 )
{
if ( v13 == v12 )
goto LABEL_13;
goto LABEL_12;
}
if ( v13 != ((v12 + 1) & 0xF) )
{
LABEL_12:
++v5;
v1 = 1;
goto LABEL_13;
}
}
LABEL_13:
i_pid_seq[v10] = v13;
goto LABEL_14;
}
}
a1 = *(_QWORD *)(a1 + 208);
++v6;
v3 = 1;
LABEL_15:
++v7;
}
while ( a1 );
l_mrtg_packets = v8;
if ( v3 )
l_mrtg_error_packets = v6;
if ( v2 )
l_mrtg_scram_packets = v4;
if ( v1 )
l_mrtg_seq_err_packets = v5;
LABEL_22:
gettimeofday(&v15, 0LL);
if ( v15.tv_sec == mrtg_time.tv_sec )
{
if ( v15.tv_usec > mrtg_time.tv_usec )
goto LABEL_29;
}
else
{
if ( v15.tv_sec <= mrtg_time.tv_sec )
return;
LABEL_29:
dumpCounts();
v14 = mrtg_time.tv_sec;
if ( v15.tv_sec - mrtg_time.tv_sec > 10 )
{
msg_Dbg(
0LL,
"Dump is %d seconds late - reset timing\n",
(unsigned int)(LODWORD(v15.tv_sec) - LODWORD(mrtg_time.tv_sec)));
v14 = v15.tv_sec;
mrtg_time = (struct timeval)_mm_loadu_si128((const __m128i *)&v15);
}
mrtg_time.tv_sec = v14 + 10;
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
char v1; // bp
char v2; // bl
char v3; // r9
__int64 v4; // r10
__int64 v5; // r11
__int64 v6; // rcx
__int64 v7; // rdx
__int64 v8; // r8
__int16 v9; // ax
__int64 v10; // rax
char v11; // si
char v12; // r13
char v13; // r12
__time_t v14; // rax
struct timeval v15; // [rsp+0h] [rbp-38h] BYREF

if ( !mrtg_fh )
return;
if ( !a1 )
goto LABEL_22;
v1 = 0;
v2 = 0;
v3 = 0;
v4 = l_mrtg_scram_packets;
v5 = l_mrtg_seq_err_packets;
v6 = l_mrtg_error_packets;
v7 = l_mrtg_packets + 1;
do
{
v8 = v7;
if ( *(_BYTE *)a1 == 71 )
{
v9 = *(unsigned __int8 *)(a1 + 1);
if ( (v9 & 0x80u) == 0 )
{
v10 = *(unsigned __int8 *)(a1 + 2) | (v9 << 8) & 0x1F00u;
if ( (_DWORD)v10 == 0x1FFF )
{
LABEL_14:
a1 = *(_QWORD *)(a1 + 208);
goto LABEL_15;
}
v11 = *(_BYTE *)(a1 + 3);
if ( (v11 & 0xC0) != 0 )
{
++v4;
v2 = 1;
}
v12 = i_pid_seq[v10];
v13 = v11 & 0xF;
if ( v12 != -1 )
{
if ( (*(_BYTE *)(a1 + 3) & 0x10) == 0 )
{
if ( v13 == v12 )
goto LABEL_13;
goto LABEL_12;
}
if ( v13 != ((v12 + 1) & 0xF) )
{
LABEL_12:
++v5;
v1 = 1;
goto LABEL_13;
}
}
LABEL_13:
i_pid_seq[v10] = v13;
goto LABEL_14;
}
}
a1 = *(_QWORD *)(a1 + 208);
++v6;
v3 = 1;
LABEL_15:
++v7;
}
while ( a1 );
l_mrtg_packets = v8;
if ( v3 )
l_mrtg_error_packets = v6;
if ( v2 )
l_mrtg_scram_packets = v4;
if ( v1 )
l_mrtg_seq_err_packets = v5;
LABEL_22:
gettimeofday(&v15, 0LL);
if ( v15.tv_sec == mrtg_time.tv_sec )
{
if ( v15.tv_usec > mrtg_time.tv_usec )
goto LABEL_29;
}
else
{
if ( v15.tv_sec <= mrtg_time.tv_sec )
return;
LABEL_29:
func0();
v14 = mrtg_time.tv_sec;
if ( v15.tv_sec - mrtg_time.tv_sec > 10 )
{
msg_Dbg(
0LL,
"Dump is %d seconds late - reset timing\n",
(unsigned int)(LODWORD(v15.tv_sec) - LODWORD(mrtg_time.tv_sec)));
v14 = v15.tv_sec;
mrtg_time = (struct timeval)_mm_loadu_si128((const __m128i *)&v15);
}
mrtg_time.tv_sec = v14 + 10;
}
}

original_code:
void mrtgAnalyse(block_t * p_ts)
{
    unsigned int i_pid;
    block_t *p_block = p_ts;
    if (mrtg_fh == ((void *)0)) return;
    while (p_block != ((void *)0)) {
        uint8_t *ts_packet = p_block->p_ts;
        char i_seq, i_last_seq;
        l_mrtg_packets++;
        if (ts_packet[0] != 0x47) {
            l_mrtg_error_packets++;
            p_block = p_block->p_next;
            continue;
        }
        if (ts_packet[1] & 0x80) {
            l_mrtg_error_packets++;
            p_block = p_block->p_next;
            continue;
        }
        i_pid = (ts_packet[1] & 0x1f) << 8 | ts_packet[2];
        if (i_pid == 0x1fff) {
            p_block = p_block->p_next;
            continue;
        }
        if (ts_packet[3] & 0xc0) {
            l_mrtg_scram_packets++;
        }
        i_seq = ts_packet[3] & 0xf;
        i_last_seq = i_pid_seq[i_pid];
        if (i_last_seq == -1) {
        } else if (ts_packet[3] & 0x10) {
            if (i_seq != ((i_last_seq + 1) & 0x0f)) {
                l_mrtg_seq_err_packets++;
            }
        } else {
            if (i_seq != i_last_seq) {
                l_mrtg_seq_err_packets++;
            }
        }
        i_pid_seq[i_pid] = i_seq;
        p_block = p_block->p_next;
    }
    struct timeval now;
    gettimeofday(&now, ((void *)0));
    if ((((&now)->tv_sec == (&mrtg_time)->tv_sec) ? ((&now)->tv_usec > (&mrtg_time)->tv_usec) : ((&now)->tv_sec > (&mrtg_time)->tv_sec))) {
        dumpCounts();
        if ((now.tv_sec - mrtg_time.tv_sec) > 10) {
            msg_Dbg(((void *)0), "Dump is %d seconds late - reset timing\n",
                    (int) (now.tv_sec - mrtg_time.tv_sec));
            mrtg_time = now;
        }
        mrtg_time.tv_sec += 10;
    }
}

canonical_original_code:
void mrtgAnalyse(struct block_t *p_ts)
{
    unsigned int i_pid;
    struct block_t *p_block = p_ts;
    if (mrtg_fh == ((void *)0)) return;
    while (p_block != ((void *)0)) {
        unsigned char *ts_packet = p_block->p_ts;
        char i_seq; char i_last_seq;
        l_mrtg_packets++;
        if (ts_packet[0] != 0x47) {
            l_mrtg_error_packets++;
            p_block = p_block->p_next;
            continue;
        }
        if (ts_packet[1] & 0x80) {
            l_mrtg_error_packets++;
            p_block = p_block->p_next;
            continue;
        }
        i_pid = (ts_packet[1] & 0x1f) << 8 | ts_packet[2];
        if (i_pid == 0x1fff) {
            p_block = p_block->p_next;
            continue;
        }
        if (ts_packet[3] & 0xc0) {
            l_mrtg_scram_packets++;
        }
        i_seq = ts_packet[3] & 0xf;
        i_last_seq = i_pid_seq[i_pid];
        if (i_last_seq == -1) {
        } else if (ts_packet[3] & 0x10) {
            if (i_seq != ((i_last_seq + 1) & 0x0f)) {
                l_mrtg_seq_err_packets++;
            }
        } else {
            if (i_seq != i_last_seq) {
                l_mrtg_seq_err_packets++;
            }
        }
        i_pid_seq[i_pid] = i_seq;
        p_block = p_block->p_next;
    }
    struct timeval now;
    gettimeofday(&now, ((void *)0));
    if ((((&now)->tv_sec == (&mrtg_time)->tv_sec) ? ((&now)->tv_usec > (&mrtg_time)->tv_usec) : ((&now)->tv_sec > (&mrtg_time)->tv_sec))) {
        dumpCounts();
        if ((now.tv_sec - mrtg_time.tv_sec) > 10) {
            msg_Dbg(((void *)0), "Dump is %d seconds late - reset timing\n",
                    (int) (now.tv_sec - mrtg_time.tv_sec));
            mrtg_time = now;
        }
        mrtg_time.tv_sec += 10;
    }
}

variable_types: struct block_t { unsigned char p_ts[188]; int i_refcount; long i_dts; unsigned short tmp_pid; struct block_t *p_next; } *

variable_types: struct timeval { long tv_sec; long tv_usec; }


mrtgInit

decompiled_code:
__int64 __fastcall mrtgInit(char *filename)
{
__int64 result; // rax

if ( !filename )
return 0xFFFFFFFFLL;
msg_Dbg(0LL, "Opening mrtg file %s.\n", filename);
mrtg_fh = fopen(filename, "wb");
if ( mrtg_fh )
{
fwrite("0 0 0 0\n", 1uLL, 8uLL, mrtg_fh);
fflush(mrtg_fh);
memset(i_pid_seq, 0xFFu, sizeof(i_pid_seq));
gettimeofday(&mrtg_time, 0LL);
result = 0LL;
mrtg_time.tv_sec += 10LL;
}
else
{
msg_Err(0LL, "unable to open mrtg file");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *filename)
{
__int64 result; // rax

if ( !filename )
return 0xFFFFFFFFLL;
msg_Dbg(0LL, "Opening mrtg file %s.\n", filename);
mrtg_fh = fopen(filename, "wb");
if ( mrtg_fh )
{
fwrite("0 0 0 0\n", 1uLL, 8uLL, mrtg_fh);
fflush(mrtg_fh);
memset(i_pid_seq, 0xFFu, sizeof(i_pid_seq));
gettimeofday(&mrtg_time, 0LL);
result = 0LL;
mrtg_time.tv_sec += 10LL;
}
else
{
msg_Err(0LL, "unable to open mrtg file");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int mrtgInit(char *mrtg_file)
{
    if ( !mrtg_file )
        return -1;
    msg_Dbg(((void *)0), "Opening mrtg file %s.\n", mrtg_file);
    if ((mrtg_fh = fopen(mrtg_file, "wb")) == ((void *)0)) {
        msg_Err(((void *)0), "unable to open mrtg file");
        return -1;
    }
    fprintf(mrtg_fh, "0 0 0 0\n");
    fflush(mrtg_fh);
    memset(&i_pid_seq[0], -1, sizeof(signed char) * 0x2000);
    gettimeofday(&mrtg_time, ((void *)0));
    mrtg_time.tv_sec += 10;
    return 0;
}

canonical_original_code:
int mrtgInit(char *mrtg_file)
{
    if ( !mrtg_file )
        return -1;
    msg_Dbg(((void *)0), "Opening mrtg file %s.\n", mrtg_file);
    if ((mrtg_fh = fopen(mrtg_file, "wb")) == ((void *)0)) {
        msg_Err(((void *)0), "unable to open mrtg file");
        return -1;
    }
    fprintf(mrtg_fh, "0 0 0 0\n");
    fflush(mrtg_fh);
    memset(&i_pid_seq[0], -1, sizeof(char) * 0x2000);
    gettimeofday(&mrtg_time, ((void *)0));
    mrtg_time.tv_sec += 10;
    return 0;
}


mrtgClose

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

if ( mrtg_fh )
{
dumpCounts();
result = fclose(mrtg_fh);
mrtg_fh = 0LL;
}
return result;
}

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

if ( mrtg_fh )
{
func0();
result = fclose(mrtg_fh);
mrtg_fh = 0LL;
}
return result;
}

original_code:
void mrtgClose()
{
    if (mrtg_fh) {
        dumpCounts();
        fclose(mrtg_fh);
        mrtg_fh = ((void *)0);
    }
}


decompiled_code:
_QWORD *Dlist_Init()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
return result;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
return result;
}

original_code:
Dlist_t* Dlist_Init(){
 Dlist_t* Dlist = malloc(sizeof(Dlist_t));
 Dlist->head=((void *)0);
 Dlist->tail=((void *)0);
 Dlist->size=0;
 return Dlist;
}

canonical_original_code:
struct __Dlist *Dlist_Init(){
 struct __Dlist *Dlist = malloc(sizeof(Dlist_t));
 Dlist->head=((void *)0);
 Dlist->tail=((void *)0);
 Dlist->size=0;
 return Dlist;
}

variable_types: struct __Dlist { struct Dlist_Node { struct Dlist_Node *prev; struct Dlist_Node *next; char TaskName[256]; unsigned int Time; _Bool repeating; unsigned int repeatTime; } *head; struct Dlist_Node *tail; unsigned int size; } *

user_defined_types: struct __Dlist { struct Dlist_Node *head; struct Dlist_Node *tail; unsigned int size; }


decompiled_code:
__int64 __fastcall Dlist_FindByName(__int64 *a1, const char *a2)
{
__int64 i; // r12

for ( i = *a1; i; i = *(_QWORD *)(i + 8) )
{
if ( !strncmp(a2, (const char *)(i + 16), 0x100uLL) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 *a1, const char *a2)
{
__int64 i; // r12

for ( i = *a1; i; i = *(_QWORD *)(i + 8) )
{
if ( !strncmp(a2, (const char *)(i + 16), 0x100uLL) )
break;
}
return i;
}

original_code:
DlistN_t* Dlist_FindByName(Dlist_t* Dlist, char* TaskName){
 DlistN_t* n = Dlist->head;
 while(n!=((void *)0)){
  if(strncmp(TaskName,n->TaskName,sizeof(n->TaskName))==0){
   return n;
  }
  n=n->next;
 }
 return ((void *)0);
}

canonical_original_code:
struct Dlist_Node *Dlist_FindByName(struct __Dlist *Dlist, char *TaskName){
 struct Dlist_Node *n = Dlist->head;
 while(n!=((void *)0)){
  if(strncmp(TaskName,n->TaskName,sizeof(n->TaskName))==0){
   return n;
  }
  n=n->next;
 }
 return ((void *)0);
}

variable_types: struct __Dlist { struct Dlist_Node *head; struct Dlist_Node *tail; unsigned int size; } *

variable_types:
TaskName
char *

variable_types: struct Dlist_Node { struct Dlist_Node *prev; struct Dlist_Node *next; char TaskName[256]; unsigned int Time; _Bool repeating; unsigned int repeatTime; } *


original_code:
void Dlist_Clear(Dlist_t* Dlist){
 DlistN_t* n = Dlist->head;
 while(n!=((void *)0)){
  DlistN_t* n_next=n->next;
  free(n);
  n=n_next;
 }
 Dlist->head=((void *)0);
 Dlist->tail=((void *)0);
 Dlist->size=0;
}

canonical_original_code:
void Dlist_Clear(struct __Dlist *Dlist){
 struct Dlist_Node *n = Dlist->head;
 while(n!=((void *)0)){
  struct Dlist_Node *n_next = n->next;
  free(n);
  n=n_next;
 }
 Dlist->head=((void *)0);
 Dlist->tail=((void *)0);
 Dlist->size=0;
}

user_defined_types: struct Dlist_Node { struct Dlist_Node *prev; struct Dlist_Node *next; char TaskName[256]; unsigned int Time; _Bool repeating; unsigned int repeatTime; }


decompiled_code:
int __fastcall Dlist_list(__int64 *a1)
{
__int64 v1; // rbx
int result; // eax

v1 = *a1;
for ( result = printf("Dlist Dump, Size=%u:\n", *((unsigned int *)a1 + 4)); v1; v1 = *(_QWORD *)(v1 + 8) )
result = printf("Node: %s, %u\n", (const char *)(v1 + 16), *(unsigned int *)(v1 + 272));
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 *a1)
{
__int64 v1; // rbx
int result; // eax

v1 = *a1;
for ( result = printf("Dlist Dump, Size=%u:\n", *((unsigned int *)a1 + 4)); v1; v1 = *(_QWORD *)(v1 + 8) )
result = printf("Node: %s, %u\n", (const char *)(v1 + 16), *(unsigned int *)(v1 + 272));
return result;
}

original_code:
void Dlist_list(Dlist_t* Dlist){
 DlistN_t* n = Dlist->head;
 printf("Dlist Dump, Size=%u:\n",Dlist->size);
 while(n!=((void *)0)){
  printf("Node: %s, %u\n",n->TaskName,n->Time);
  n=n->next;
 }
}

canonical_original_code:
void Dlist_list(struct __Dlist *Dlist){
 struct Dlist_Node *n = Dlist->head;
 printf("Dlist Dump, Size=%u:\n",Dlist->size);
 while(n!=((void *)0)){
  printf("Node: %s, %u\n",n->TaskName,n->Time);
  n=n->next;
 }
}


original_code:
void Dlist_Insert(Dlist_t* Dlist, DlistN_t* n){
 if(n==((void *)0)){
  perror("Cant Insert NULL");
  return;
 }
 Dlist->size++;
 if(Dlist->head==((void *)0) && Dlist->tail==((void *)0)){
  Dlist->head=n;
  Dlist->tail=n;
  n->next=((void *)0);
  n->prev=((void *)0);
  return;
 }
 if(n->Time < Dlist->head->Time){
  Dlist->head->prev=n;
  n->next=Dlist->head;
  Dlist->head=n;
  n->prev=((void *)0);
  return;
 }
 DlistN_t* testNode=Dlist->tail;
 while(testNode!=((void *)0)){
  if(n->Time >= testNode->Time){
   if(testNode==Dlist->tail){
    Dlist->tail=n;
    n->next=((void *)0);
   }
   if(testNode->next!=((void *)0))
    testNode->next->prev=n;
   n->next=testNode->next;
   testNode->next=n;
   n->prev=testNode;
   return;
  }
  testNode=testNode->prev;
 }
 Dlist->size--;
 perror("ERROR Dlist Insert Failed");
 printf("ERR NODE Name: %s Time: %u\n",n->TaskName,n->Time);
 Dlist_list(Dlist);
}

canonical_original_code:
void Dlist_Insert(struct __Dlist *Dlist, struct Dlist_Node *n){
 if(n==((void *)0)){
  perror("Cant Insert NULL");
  return;
 }
 Dlist->size++;
 if(Dlist->head==((void *)0) && Dlist->tail==((void *)0)){
  Dlist->head=n;
  Dlist->tail=n;
  n->next=((void *)0);
  n->prev=((void *)0);
  return;
 }
 if(n->Time < Dlist->head->Time){
  Dlist->head->prev=n;
  n->next=Dlist->head;
  Dlist->head=n;
  n->prev=((void *)0);
  return;
 }
 struct Dlist_Node *testNode = Dlist->tail;
 while(testNode!=((void *)0)){
  if(n->Time >= testNode->Time){
   if(testNode==Dlist->tail){
    Dlist->tail=n;
    n->next=((void *)0);
   }
   if(testNode->next!=((void *)0))
    testNode->next->prev=n;
   n->next=testNode->next;
   testNode->next=n;
   n->prev=testNode;
   return;
  }
  testNode=testNode->prev;
 }
 Dlist->size--;
 perror("ERROR Dlist Insert Failed");
 printf("ERR NODE Name: %s Time: %u\n",n->TaskName,n->Time);
 Dlist_list(Dlist);
}

variable_types:
n
struct Dlist_Node *


decompiled_code:
void __fastcall Scheduler_AddBasic(char *src, int a2, char a3, int a4)
{
char *v6; // rbp
__int64 v7; // rdi

v6 = (char *)malloc(0x120uLL);
strncpy(v6 + 16, src, 0x100uLL);
v7 = list;
*((_DWORD *)v6 + 68) = a2;
v6[276] = a3;
*((_DWORD *)v6 + 70) = a4;
Dlist_Insert(v7, (__int64)v6);
}

canonical_decompiled_code:
void __fastcall func12(char *src, int a2, char a3, int a4)
{
char *v6; // rbp
__int64 v7; // rdi

v6 = (char *)malloc(0x120uLL);
strncpy(v6 + 16, src, 0x100uLL);
v7 = list;
*((_DWORD *)v6 + 68) = a2;
v6[276] = a3;
*((_DWORD *)v6 + 70) = a4;
func11(v7, (__int64)v6);
}

original_code:
static void Scheduler_AddBasic(char* TaskName, uint32_t Time, _Bool Repeating, uint32_t RepTime){
 DlistN_t* task = malloc(sizeof(DlistN_t));
 strncpy(task->TaskName, TaskName, sizeof(task->TaskName));
 task->Time=Time;
 task->repeating=Repeating;
 task->repeatTime=RepTime;
 Dlist_Insert(list, task);
}

canonical_original_code:
static void Scheduler_AddBasic(char *TaskName, unsigned int Time, _Bool Repeating, unsigned int RepTime){
 struct Dlist_Node *task = malloc(sizeof(DlistN_t));
 strncpy(task->TaskName, TaskName, sizeof(task->TaskName));
 task->Time=Time;
 task->repeating=Repeating;
 task->repeatTime=RepTime;
 Dlist_Insert(list, task);
}

variable_types:
Time
unsigned int

variable_types:
RepTime
unsigned int


decompiled_code:
_QWORD *Scheduler_Init()
{
_QWORD *result; // rax

result = Dlist_Init();
list = (__int64)result;
return result;
}

canonical_decompiled_code:
_QWORD *func13()
{
_QWORD *result; // rax

result = func7();
list = (__int64)result;
return result;
}

original_code:
void Scheduler_Init(){
 list = Dlist_Init();
}


decompiled_code:
void __fastcall Scheduler_Add(int a1, char *a2)
{
Scheduler_AddBasic(a2, a1, 0, 0);
}

canonical_decompiled_code:
void __fastcall func14(int a1, char *a2)
{
func12(a2, a1, 0, 0);
}

original_code:
void Scheduler_Add(uint32_t Time, char* TaskName){
 Scheduler_AddBasic(TaskName,Time,0,0);
}

canonical_original_code:
void Scheduler_Add(unsigned int Time, char *TaskName){
 Scheduler_AddBasic(TaskName,Time,0,0);
}


decompiled_code:
void __fastcall Scheduler_AddRep(int a1, int a2, char *a3)
{
Scheduler_AddBasic(a3, a1, 1, a2);
}

canonical_decompiled_code:
void __fastcall func15(int a1, int a2, char *a3)
{
func12(a3, a1, 1, a2);
}

original_code:
void Scheduler_AddRep(uint32_t Time, uint32_t RepTime, char* TaskName){
 Scheduler_AddBasic(TaskName,Time,1,RepTime);
}

canonical_original_code:
void Scheduler_AddRep(unsigned int Time, unsigned int RepTime, char *TaskName){
 Scheduler_AddBasic(TaskName,Time,1,RepTime);
}


decompiled_code:
__int64 __fastcall Scheduler_Time(unsigned int a1)
{
__int64 result; // rax
__int64 v2; // rdi
__int64 v3; // rsi
char *v4; // rbp

TIME = a1;
result = a1;
v2 = list;
v3 = *(_QWORD *)list;
if ( (*(_QWORD *)list || *(_QWORD *)(list + 8)) && (unsigned int)result >= *(_DWORD *)(v3 + 272) )
{
do
{
v4 = (char *)Dlist_Remove(v2, (_QWORD *)v3);
printf("%u %s\n", *((unsigned int *)v4 + 68), v4 + 16);
if ( v4[276] )
{
*((_DWORD *)v4 + 68) += *((_DWORD *)v4 + 70);
Dlist_Insert(list, (__int64)v4);
}
else
{
free(v4);
}
v2 = list;
result = (unsigned int)TIME;
v3 = *(_QWORD *)list;
}
while ( *(_DWORD *)(*(_QWORD *)list + 272LL) <= (unsigned int)TIME );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int a1)
{
__int64 result; // rax
__int64 v2; // rdi
__int64 v3; // rsi
char *v4; // rbp

TIME = a1;
result = a1;
v2 = list;
v3 = *(_QWORD *)list;
if ( (*(_QWORD *)list || *(_QWORD *)(list + 8)) && (unsigned int)result >= *(_DWORD *)(v3 + 272) )
{
do
{
v4 = (char *)Dlist_Remove(v2, (_QWORD *)v3);
printf("%u %s\n", *((unsigned int *)v4 + 68), v4 + 16);
if ( v4[276] )
{
*((_DWORD *)v4 + 68) += *((_DWORD *)v4 + 70);
func11(list, (__int64)v4);
}
else
{
free(v4);
}
v2 = list;
result = (unsigned int)TIME;
v3 = *(_QWORD *)list;
}
while ( *(_DWORD *)(*(_QWORD *)list + 272LL) <= (unsigned int)TIME );
}
return result;
}

original_code:
void Scheduler_Time(uint32_t Time){
 TIME=Time;
 if(list->head==((void *)0) && list->tail==((void *)0)){
  return;
 }
 while(list->head->Time <= TIME){
  DlistN_t* n = Dlist_Remove(list,list->head);
  printf("%u %s\n",n->Time,n->TaskName);
  if(n->repeating==1){
   n->Time=n->Time+n->repeatTime;
   Dlist_Insert(list,n);
  }else{
   free(n);
  }
 }
}

canonical_original_code:
void Scheduler_Time(unsigned int Time){
 TIME=Time;
 if(list->head==((void *)0) && list->tail==((void *)0)){
  return;
 }
 while(list->head->Time <= TIME){
  struct Dlist_Node *n = Dlist_Remove(list,list->head);
  printf("%u %s\n",n->Time,n->TaskName);
  if(n->repeating==1){
   n->Time=n->Time+n->repeatTime;
   Dlist_Insert(list,n);
  }else{
   free(n);
  }
 }
}


decompiled_code:
void __fastcall Scheduler_Delete(const char *a1)
{
_QWORD *v1; // rax
void *v2; // rbp

v1 = (_QWORD *)Dlist_FindByName((__int64 *)list, a1);
if ( v1 )
{
v2 = v1;
Dlist_Remove(list, v1);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func17(const char *a1)
{
_QWORD *v1; // rax
void *v2; // rbp

v1 = (_QWORD *)func8((__int64 *)list, a1);
if ( v1 )
{
v2 = v1;
Dlist_Remove(list, v1);
free(v2);
}
}

original_code:
void Scheduler_Delete(char* Name){
 DlistN_t* delN = Dlist_FindByName(list,Name);
 if(delN==((void *)0))
  return;
 Dlist_Remove(list,delN);
 free(delN);
}

canonical_original_code:
void Scheduler_Delete(char *Name){
 struct Dlist_Node *delN = Dlist_FindByName(list,Name);
 if(delN==((void *)0))
  return;
 Dlist_Remove(list,delN);
 free(delN);
}

variable_types:
Name
char *


decompiled_code:
int Scheduler_List()
{
int result; // eax
__int64 i; // rbx

puts("Upcoming tasks:");
result = list;
for ( i = *(_QWORD *)list; i; i = *(_QWORD *)(i + 8) )
result = printf("%s %u\n", (const char *)(i + 16), (unsigned int)(*(_DWORD *)(i + 272) - TIME));
return result;
}

canonical_decompiled_code:
int func18()
{
int result; // eax
__int64 i; // rbx

puts("Upcoming tasks:");
result = list;
for ( i = *(_QWORD *)list; i; i = *(_QWORD *)(i + 8) )
result = printf("%s %u\n", (const char *)(i + 16), (unsigned int)(*(_DWORD *)(i + 272) - TIME));
return result;
}

original_code:
void Scheduler_List(){
 printf("Upcoming tasks:\n");
 DlistN_t* n = list->head;
 while(n!=((void *)0)){
  printf("%s %u\n", n->TaskName,(n->Time-TIME));
  n=n->next;
 }
}

canonical_original_code:
void Scheduler_List(){
 printf("Upcoming tasks:\n");
 struct Dlist_Node *n = list->head;
 while(n!=((void *)0)){
  printf("%s %u\n", n->TaskName,(n->Time-TIME));
  n=n->next;
 }
}


decompiled_code:
void Scheduler_Clear()
{
Dlist_Clear(list);
}

canonical_decompiled_code:
void func19()
{
func9(list);
}

original_code:
void Scheduler_Clear(){
 Dlist_Clear(list);
}


decompiled_code:
__suseconds_t __fastcall clk_rd_port(__int64 a1, _DWORD *a2)
{
__suseconds_t result; // rax
char v3; // dl

result = (unsigned int)*a2;
if ( (_DWORD)result )
{
v3 = *(_BYTE *)(a1 + 8);
if ( v3 == 1 )
{
*(_DWORD *)(a1 + 4) = result;
*(_BYTE *)(a1 + 8) = 2;
}
else if ( v3 == 2 )
{
switch ( (_DWORD)result )
{
case 1:
*(_BYTE *)(a1 + 8) = 0;
break;
case 2:
*(_BYTE *)(a1 + 8) = 1;
break;
case 3:
result = clk_get_tv_usec(a2);
break;
}
}
else if ( v3 )
{
*(_BYTE *)(a1 + 8) = 2;
}
else
{
*(_DWORD *)a1 = result;
*(_BYTE *)(a1 + 8) = 2;
}
}
return result;
}

canonical_decompiled_code:
__suseconds_t __fastcall func2(__int64 a1, _DWORD *a2)
{
__suseconds_t result; // rax
char v3; // dl

result = (unsigned int)*a2;
if ( (_DWORD)result )
{
v3 = *(_BYTE *)(a1 + 8);
if ( v3 == 1 )
{
*(_DWORD *)(a1 + 4) = result;
*(_BYTE *)(a1 + 8) = 2;
}
else if ( v3 == 2 )
{
switch ( (_DWORD)result )
{
case 1:
*(_BYTE *)(a1 + 8) = 0;
break;
case 2:
*(_BYTE *)(a1 + 8) = 1;
break;
case 3:
result = func1(a2);
break;
}
}
else if ( v3 )
{
*(_BYTE *)(a1 + 8) = 2;
}
else
{
*(_DWORD *)a1 = result;
*(_BYTE *)(a1 + 8) = 2;
}
}
return result;
}

original_code:
void
clk_rd_port(clk_t *clk, uint32_t *dat)
{
    if (! *dat) return;
    switch (clk->state)
    {
        case CLK_READ_QUANTUM:
                clk->quantum = *dat;
                clk->state = CLK_READ_REQUEST;
            break;
        case CLK_READ_ADDRESS:
                clk->address = *dat;
                clk->state = CLK_READ_REQUEST;
            break;
        case CLK_READ_REQUEST:
                if (*dat == (0x1)) clk->state = CLK_READ_QUANTUM;
                if (*dat == (0x2)) clk->state = CLK_READ_ADDRESS;
                if (*dat == (0x3)) clk_get_tv_usec (dat);
            break;
        default: clk->state = CLK_READ_REQUEST; break;
    }
}

canonical_original_code:
void clk_rd_port(struct clk_s *clk, unsigned int *dat)
{
    if (! *dat) return;
    switch (clk->state)
    {
        case CLK_READ_QUANTUM:
                clk->quantum = *dat;
                clk->state = CLK_READ_REQUEST;
            break;
        case CLK_READ_ADDRESS:
                clk->address = *dat;
                clk->state = CLK_READ_REQUEST;
            break;
        case CLK_READ_REQUEST:
                if (*dat == (0x1)) clk->state = CLK_READ_QUANTUM;
                if (*dat == (0x2)) clk->state = CLK_READ_ADDRESS;
                if (*dat == (0x3)) clk_get_tv_usec (dat);
            break;
        default: clk->state = CLK_READ_REQUEST; break;
    }
}

variable_types: struct clk_s { unsigned int quantum; unsigned int address; unsigned char state; unsigned int prv_tm; } *

variable_types:
dat
unsigned int *


decompiled_code:
__int64 __fastcall clk_chk_tmr(_DWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v6; // eax
__int64 v7; // rsi
__int64 v8; // rdx
unsigned int v9[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( *a1 )
{
result = (unsigned int)a1[1];
if ( (_DWORD)result )
{
clk_get_tv_usec(v9);
result = v9[0] - a1[3];
if ( (unsigned int)result >= 1000 * *a1 )
{
v6 = *(_DWORD *)(a2 + 56);
v7 = *(unsigned int *)(a2 + 60);
v8 = (unsigned int)(v6 - 4);
*(_DWORD *)(a2 + 56) = v8;
utils_store(a3, v7, v8, 32LL);
*(_DWORD *)(a2 + 60) = a1[1];
result = v9[0];
a1[3] = v9[0];
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
int v6; // eax
__int64 v7; // rsi
__int64 v8; // rdx
unsigned int v9[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( *a1 )
{
result = (unsigned int)a1[1];
if ( (_DWORD)result )
{
func1(v9);
result = v9[0] - a1[3];
if ( (unsigned int)result >= 1000 * *a1 )
{
v6 = *(_DWORD *)(a2 + 56);
v7 = *(unsigned int *)(a2 + 60);
v8 = (unsigned int)(v6 - 4);
*(_DWORD *)(a2 + 56) = v8;
utils_store(a3, v7, v8, 32LL);
*(_DWORD *)(a2 + 60) = a1[1];
result = v9[0];
a1[3] = v9[0];
}
}
}
return result;
}

original_code:
void
clk_chk_tmr(clk_t *clk, cpu_t *cpu, ram_t *ram)
{
    uint32_t tv_usec;
    if (!clk->quantum || !clk->address) return;
    clk_get_tv_usec (&tv_usec);
    if (tv_usec - clk->prv_tm >= (clk->quantum * 1000))
    {
        cpu->registers [REGISTER_STACKPOINTER] -= 4;
        utils_store (ram,
                     cpu->registers [REGISTER_PC],
                     cpu->registers[REGISTER_STACKPOINTER],
                     32);
        cpu->registers [REGISTER_PC] = clk->address;
        clk->prv_tm = tv_usec;
    }
}

canonical_original_code:
void clk_chk_tmr(struct clk_s *clk, struct cpu_s *cpu, struct ram_s *ram)
{
    unsigned int tv_usec;
    if (!clk->quantum || !clk->address) return;
    clk_get_tv_usec (&tv_usec);
    if (tv_usec - clk->prv_tm >= (clk->quantum * 1000))
    {
        cpu->registers [REGISTER_STACKPOINTER] -= 4;
        utils_store (ram,
                     cpu->registers [REGISTER_PC],
                     cpu->registers[REGISTER_STACKPOINTER],
                     32);
        cpu->registers [REGISTER_PC] = clk->address;
        clk->prv_tm = tv_usec;
    }
}

variable_types: struct cpu_s { unsigned char state; unsigned char adr_mode; unsigned int registers[-1]; unsigned int ports[-1]; unsigned int cycle; } *

variable_types: struct ram_s { unsigned char address[-1]; struct instr_s { unsigned char opcode; unsigned char dst_src; unsigned short constant; } *instruction; } *

user_defined_types: struct clk_s { unsigned int quantum; unsigned int address; unsigned char state; unsigned int prv_tm; }

user_defined_types: struct ram_s { unsigned char address[-1]; struct instr_s *instruction; }


decompiled_code:
int print_help()
{
puts("=========================================================");
puts("C32-EMU, Emulator for the Custom 32-bit RISC architecture");
puts("Copyright (C) 2013, Joe Mruz");
puts("v0.1a");
puts("=========================================================");
puts("valid options:\n");
puts("-disk <disk filename> -- specify the secondary storage");
puts("-rom <rom filename> -- specify the read-only memory");
puts("-boot_rom -- boot from the rom instead of disk");
puts("-fast -- use 100 percent CPU to speed up emulation");
puts("-slow -- set instruction execution speed equal to the FPS");
puts("-debug -- enable on-screen debug text (can toggle with F1)");
puts("-color -- specify text color (green, amber, white)");
puts("-help -- print this dialog");
return puts(
"\n"
"usage: emu [-fast] [-slow] [-debug] [-boot_rom] -disk <filename> [-rom <filename>] [-color <name>] [-help]");
}

canonical_decompiled_code:
int func0()
{
puts("=========================================================");
puts("C32-EMU, Emulator for the Custom 32-bit RISC architecture");
puts("Copyright (C) 2013, Joe Mruz");
puts("v0.1a");
puts("=========================================================");
puts("valid options:\n");
puts("-disk <disk filename> -- specify the secondary storage");
puts("-rom <rom filename> -- specify the read-only memory");
puts("-boot_rom -- boot from the rom instead of disk");
puts("-fast -- use 100 percent CPU to speed up emulation");
puts("-slow -- set instruction execution speed equal to the FPS");
puts("-debug -- enable on-screen debug text (can toggle with F1)");
puts("-color -- specify text color (green, amber, white)");
puts("-help -- print this dialog");
return puts(
"\n"
"usage: emu [-fast] [-slow] [-debug] [-boot_rom] -disk <filename> [-rom <filename>] [-color <name>] [-help]");
}

original_code:
void print_help(void)
{
    printf ("=========================================================\n");
    printf ("C32-EMU, Emulator for the Custom 32-bit RISC architecture\n");
    printf ("Copyright (C) 2013, Joe Mruz\n");
    printf ("v""0.1a""\n");
    printf ("=========================================================\n");
    printf ("valid options:\n\n");
    printf ("-disk <disk filename>   -- specify the secondary storage\n");
    printf ("-rom <rom filename>     -- specify the read-only memory\n");
    printf ("-boot_rom               -- boot from the rom instead of disk\n");
    printf ("-fast                   -- use 100 percent CPU to speed up emulation\n");
    printf ("-slow                   -- set instruction execution speed equal to the FPS\n");
    printf ("-debug                  -- enable on-screen debug text (can toggle with F1)\n");
    printf ("-color                  -- specify text color (green, amber, white)\n");
    printf ("-help                   -- print this dialog\n");
    printf ("\nusage: emu [-fast] [-slow] [-debug] [-boot_rom] -disk <filename> [-rom <filename>] [-color <name>] [-help]\n");
}

canonical_original_code:
void print_help()
{
    printf ("=========================================================\n");
    printf ("C32-EMU, Emulator for the Custom 32-bit RISC architecture\n");
    printf ("Copyright (C) 2013, Joe Mruz\n");
    printf ("v""0.1a""\n");
    printf ("=========================================================\n");
    printf ("valid options:\n\n");
    printf ("-disk <disk filename>   -- specify the secondary storage\n");
    printf ("-rom <rom filename>     -- specify the read-only memory\n");
    printf ("-boot_rom               -- boot from the rom instead of disk\n");
    printf ("-fast                   -- use 100 percent CPU to speed up emulation\n");
    printf ("-slow                   -- set instruction execution speed equal to the FPS\n");
    printf ("-debug                  -- enable on-screen debug text (can toggle with F1)\n");
    printf ("-color                  -- specify text color (green, amber, white)\n");
    printf ("-help                   -- print this dialog\n");
    printf ("\nusage: emu [-fast] [-slow] [-debug] [-boot_rom] -disk <filename> [-rom <filename>] [-color <name>] [-help]\n");
}


decompiled_code:
__int64 __fastcall runtime_options(int a1, __int64 a2)
{
int v4; // ebx
const char *v5; // rsi
__int64 v6; // r8
__int64 v7; // rdx
const char *v8; // rsi
int v9; // ebx
__int64 result; // rax
const char *v11; // rdx

if ( a1 > 1 )
{
v4 = 1;
while ( 1 )
{
v6 = *(_QWORD *)(a2 + 8LL * v4);
v7 = 8LL * v4;
if ( !strcmp("-disk", (const char *)v6) )
break;
if ( !strcmp("-rom", *(const char **)(a2 + 8LL * v4)) )
{
v8 = *(const char **)(a2 + v7 + 8);
v9 = v4 + 1;
if ( !v8 )
{
LABEL_29:
puts("Error: filename not provided.");
print_help();
exit(2);
}
v4 = v9 + 1;
strncpy(&Rom_filn, v8, 0x80uLL);
if ( a1 <= v4 )
goto LABEL_10;
}
else
{
if ( !strcmp("-color", *(const char **)(a2 + 8LL * v4)) )
{
v11 = *(const char **)(a2 + v7 + 8);
++v4;
if ( !v11 )
{
puts("Error: Color name required.");
print_help();
exit(2);
}
if ( !strcmp("green", v11) )
{
Text_color = 1;
}
else if ( !strcmp("amber", v11) )
{
Text_color = 2;
}
else
{
Text_color = strcmp("red", v11) == 0 ? 3 : 0;
}
}
else if ( !memcmp("-boot_rom", *(const void **)(a2 + 8LL * v4), 8uLL) )
{
--Boot_disk;
}
else if ( !strcmp("-fast", *(const char **)(a2 + 8LL * v4)) )
{
++Emu_max_speed;
Emu_refresh_rate *= 4;
}
else if ( !strcmp("-slow", *(const char **)(a2 + 8LL * v4)) )
{
Emu_refresh_rate = 1;
}
else
{
if ( strcmp("-debug", *(const char **)(a2 + 8LL * v4)) )
{
if ( (*(_BYTE *)v6 != 45 || *(_BYTE *)(v6 + 1) != 104 || *(_BYTE *)(v6 + 2))
&& strcmp("--help", *(const char **)(a2 + 8LL * v4))
&& strcmp("-help", *(const char **)(a2 + 8LL * v4)) )
{
printf("Unrecognized option: %s\n", *(const char **)(a2 + 8LL * v4));
}
print_help();
exit(2);
}
++Emu_dbg_option;
}
LABEL_5:
if ( a1 <= ++v4 )
goto LABEL_10;
}
}
v5 = *(const char **)(a2 + v7 + 8);
++v4;
if ( !v5 )
goto LABEL_29;
strncpy(Disk_filn, v5, 0x80uLL);
++Opt_disk;
goto LABEL_5;
}
LABEL_10:
if ( !Opt_disk )
{
puts("Error: Must specify a disk file");
print_help();
exit(2);
}
result = (unsigned int)Boot_disk;
if ( !Boot_disk && !Rom_filn )
{
puts("Must provide rom file to boot");
exit(2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
int v4; // ebx
const char *v5; // rsi
__int64 v6; // r8
__int64 v7; // rdx
const char *v8; // rsi
int v9; // ebx
__int64 result; // rax
const char *v11; // rdx

if ( a1 > 1 )
{
v4 = 1;
while ( 1 )
{
v6 = *(_QWORD *)(a2 + 8LL * v4);
v7 = 8LL * v4;
if ( !strcmp("-disk", (const char *)v6) )
break;
if ( !strcmp("-rom", *(const char **)(a2 + 8LL * v4)) )
{
v8 = *(const char **)(a2 + v7 + 8);
v9 = v4 + 1;
if ( !v8 )
{
LABEL_29:
puts("Error: filename not provided.");
func0();
exit(2);
}
v4 = v9 + 1;
strncpy(&Rom_filn, v8, 0x80uLL);
if ( a1 <= v4 )
goto LABEL_10;
}
else
{
if ( !strcmp("-color", *(const char **)(a2 + 8LL * v4)) )
{
v11 = *(const char **)(a2 + v7 + 8);
++v4;
if ( !v11 )
{
puts("Error: Color name required.");
func0();
exit(2);
}
if ( !strcmp("green", v11) )
{
Text_color = 1;
}
else if ( !strcmp("amber", v11) )
{
Text_color = 2;
}
else
{
Text_color = strcmp("red", v11) == 0 ? 3 : 0;
}
}
else if ( !memcmp("-boot_rom", *(const void **)(a2 + 8LL * v4), 8uLL) )
{
--Boot_disk;
}
else if ( !strcmp("-fast", *(const char **)(a2 + 8LL * v4)) )
{
++Emu_max_speed;
Emu_refresh_rate *= 4;
}
else if ( !strcmp("-slow", *(const char **)(a2 + 8LL * v4)) )
{
Emu_refresh_rate = 1;
}
else
{
if ( strcmp("-debug", *(const char **)(a2 + 8LL * v4)) )
{
if ( (*(_BYTE *)v6 != 45 || *(_BYTE *)(v6 + 1) != 104 || *(_BYTE *)(v6 + 2))
&& strcmp("--help", *(const char **)(a2 + 8LL * v4))
&& strcmp("-help", *(const char **)(a2 + 8LL * v4)) )
{
printf("Unrecognized option: %s\n", *(const char **)(a2 + 8LL * v4));
}
func0();
exit(2);
}
++Emu_dbg_option;
}
LABEL_5:
if ( a1 <= ++v4 )
goto LABEL_10;
}
}
v5 = *(const char **)(a2 + v7 + 8);
++v4;
if ( !v5 )
goto LABEL_29;
strncpy(Disk_filn, v5, 0x80uLL);
++Opt_disk;
goto LABEL_5;
}
LABEL_10:
if ( !Opt_disk )
{
puts("Error: Must specify a disk file");
func0();
exit(2);
}
result = (unsigned int)Boot_disk;
if ( !Boot_disk && !Rom_filn )
{
puts("Must provide rom file to boot");
exit(2);
}
return result;
}

original_code:
void
runtime_options(int argc, char **argv)
{
    int i;
    for (i = 1; i < argc; i++)
    {
        if (!strncmp ("-disk", argv [i], sizeof (argv [i])))
        {
            ++i;
            if(!argv [i])
            {
                printf ("Error: filename not provided.\n");
                print_help ();
                exit (2);
            }
            strncpy (Disk_filn, argv [i], sizeof (Disk_filn));
            ++Opt_disk;
        }
        else if (!strncmp ("-rom", argv [i], sizeof (argv [i])))
        {
            ++i;
            if (!argv [i])
            {
                printf ("Error: filename not provided.\n");
                print_help ();
                exit (2);
            }
            strncpy (Rom_filn, argv [i], sizeof (Disk_filn));
        }
        else if (!strncmp ("-color", argv [i], sizeof (argv [i])))
        {
            ++i;
            if (!argv [i])
            {
                printf ("Error: Color name required.\n");
                print_help ();
                exit (2);
            }
            if (!strncmp ("green", argv [i], sizeof (argv [i])))
            {
                Text_color = 1;
            }
            else if (!strncmp ("amber", argv [i], sizeof (argv [i])))
            {
                Text_color = 2;
            }
            else if (!strncmp ("red", argv [i], sizeof (argv [i])))
            {
                Text_color = 3;
            }
            else
            {
                Text_color = 0;
            }
        }
        else if (!strncmp ("-boot_rom", argv [i], sizeof (argv [i])))
        {
            --Boot_disk;
        }
        else if (!strncmp ("-fast", argv [i], sizeof (argv [i])))
        {
            ++Emu_max_speed;
            Emu_refresh_rate *= 4;
        }
        else if (!strncmp ("-slow", argv [i], sizeof (argv [i])))
        {
            Emu_refresh_rate = 1;
        }
        else if (!strncmp ("-debug", argv [i], sizeof (argv [i])))
        {
            ++Emu_dbg_option;
        }
        else if (!strncmp ("-h", argv [i], sizeof (argv [i])) ||
                 !strncmp ("--help", argv [i], sizeof (argv [i])) ||
                 !strncmp ("-help", argv [i], sizeof (argv [i])))
        {
            print_help ();
            exit (2);
        }
        else
        {
            printf ("Unrecognized option: %s\n", argv [i]);
            print_help ();
            exit (2);
        }
    }
    if (!Opt_disk)
    {
        printf ("Error: Must specify a disk file\n");
        print_help ();
        exit (2);
    }
    if (!Boot_disk &&
        !Rom_filn [0])
    {
        printf ("Must provide rom file to boot\n");
        exit (2);
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Emu_max_speed = 0;
memset(Disk_filn, 0, sizeof(Disk_filn));
Emu_refresh_rate = 60;
memset(&Rom_filn, 0, 0x80uLL);
Emu_dbg_option = 0;
Opt_disk = 0;
Boot_disk = 1;
Text_color = 0;
runtime_options(argc, (__int64)argv);
puts("C32-EMU v0.1a");
puts("Copyright (C) 2013, Joe Mruz");
puts("=============================");
puts("Initializing everything...");
emu_init();
puts("Starting...");
while ( Emu_run )
emu_main_loop();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Emu_max_speed = 0;
memset(Disk_filn, 0, sizeof(Disk_filn));
Emu_refresh_rate = 60;
memset(&Rom_filn, 0, 0x80uLL);
Emu_dbg_option = 0;
Opt_disk = 0;
Boot_disk = 1;
Text_color = 0;
func1(argc, (__int64)argv);
puts("C32-EMU v0.1a");
puts("Copyright (C) 2013, Joe Mruz");
puts("=============================");
puts("Initializing everything...");
emu_init();
puts("Starting...");
while ( Emu_run )
emu_main_loop();
return 0;
}

original_code:
int
main(int argc, char **argv)
{
    Emu_max_speed = 0;
    Emu_refresh_rate = 60;
    Emu_dbg_option = 0;
    Opt_disk = 0;
    Boot_disk = 1;
    Text_color = 0;
    memset (Disk_filn, 0, sizeof (Disk_filn));
    memset (Rom_filn, 0, sizeof (Rom_filn));
    runtime_options (argc, argv);
    printf ("C32-EMU v""0.1a""\n");
    printf ("Copyright (C) 2013, Joe Mruz\n");
    printf ("=============================\n");
    printf ("Initializing everything...\n");
    emu_init ();
    printf ("Starting...\n");
    while (Emu_run)
        emu_main_loop ();
    return 0;
}


decompiled_code:
unsigned int __fastcall disk_rd_port(__int64 a1, unsigned int *a2, __int64 a3)
{
unsigned int result; // eax
unsigned int v4; // ecx
int v5; // er9
int v6; // er9
unsigned int v7; // er10
__int64 v8; // r8
__int64 v9; // rcx
unsigned int v10; // er10
int v11; // er11
__int64 v12; // r9
__int64 v13; // r8

result = *(unsigned __int8 *)(a1 + 31457284);
if ( (_BYTE)result == 2 )
{
v4 = *a2;
if ( *a2 == -1 )
{
v5 = *(_DWORD *)(a1 + 31457280);
if ( v5 )
{
result = *(_DWORD *)(a1 + 31457292);
v6 = result + v5;
v7 = *(_DWORD *)(a1 + 31457288) - result;
do
{
v8 = result;
v9 = v7 + result++;
*(_BYTE *)(a1 + v9) = *(_BYTE *)(a3 + v8);
}
while ( v6 != result );
}
}
else
{
if ( v4 > 0xFFFFFFFC || v4 == 3 )
return result;
if ( v4 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 3;
return result;
}
if ( v4 > 0x1DFFFFF )
{
result = printf("Error: disk seek request greater than size of disk");
*a2 = -2;
return result;
}
v10 = *(_DWORD *)(a1 + 31457280);
*(_DWORD *)(a1 + 31457288) = v4;
if ( v10 )
{
v11 = *(_DWORD *)(a1 + 31457292);
for ( result = 0; result < v10; ++result )
{
v12 = v4 + result;
v13 = v11 + result;
*(_BYTE *)(a3 + v13) = *(_BYTE *)(a1 + v12);
}
}
}
*a2 = -3;
return result;
}
if ( (unsigned __int8)result > 2u )
{
if ( (_BYTE)result == 3 )
{
if ( *a2 == -4 )
*(_BYTE *)(a1 + 31457284) = 0;
goto LABEL_7;
}
*(_BYTE *)(a1 + 31457284) = 0;
}
else
{
if ( (_BYTE)result )
{
result = *a2;
if ( *a2 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 2;
*a2 = -3;
return result;
}
if ( result + *(_DWORD *)(a1 + 31457292) <= 0xFFFFF )
{
*(_DWORD *)(a1 + 31457280) = result;
LABEL_7:
*a2 = 0;
return result;
}
}
else
{
result = *a2;
if ( *a2 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 1;
goto LABEL_7;
}
if ( result <= 0xFFFFF )
{
*(_DWORD *)(a1 + 31457292) = result;
goto LABEL_7;
}
}
*a2 = -2;
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func0(__int64 a1, unsigned int *a2, __int64 a3)
{
unsigned int result; // eax
unsigned int v4; // ecx
int v5; // er9
int v6; // er9
unsigned int v7; // er10
__int64 v8; // r8
__int64 v9; // rcx
unsigned int v10; // er10
int v11; // er11
__int64 v12; // r9
__int64 v13; // r8

result = *(unsigned __int8 *)(a1 + 31457284);
if ( (_BYTE)result == 2 )
{
v4 = *a2;
if ( *a2 == -1 )
{
v5 = *(_DWORD *)(a1 + 31457280);
if ( v5 )
{
result = *(_DWORD *)(a1 + 31457292);
v6 = result + v5;
v7 = *(_DWORD *)(a1 + 31457288) - result;
do
{
v8 = result;
v9 = v7 + result++;
*(_BYTE *)(a1 + v9) = *(_BYTE *)(a3 + v8);
}
while ( v6 != result );
}
}
else
{
if ( v4 > 0xFFFFFFFC || v4 == 3 )
return result;
if ( v4 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 3;
return result;
}
if ( v4 > 0x1DFFFFF )
{
result = printf("Error: disk seek request greater than size of disk");
*a2 = -2;
return result;
}
v10 = *(_DWORD *)(a1 + 31457280);
*(_DWORD *)(a1 + 31457288) = v4;
if ( v10 )
{
v11 = *(_DWORD *)(a1 + 31457292);
for ( result = 0; result < v10; ++result )
{
v12 = v4 + result;
v13 = v11 + result;
*(_BYTE *)(a3 + v13) = *(_BYTE *)(a1 + v12);
}
}
}
*a2 = -3;
return result;
}
if ( (unsigned __int8)result > 2u )
{
if ( (_BYTE)result == 3 )
{
if ( *a2 == -4 )
*(_BYTE *)(a1 + 31457284) = 0;
goto LABEL_7;
}
*(_BYTE *)(a1 + 31457284) = 0;
}
else
{
if ( (_BYTE)result )
{
result = *a2;
if ( *a2 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 2;
*a2 = -3;
return result;
}
if ( result + *(_DWORD *)(a1 + 31457292) <= 0xFFFFF )
{
*(_DWORD *)(a1 + 31457280) = result;
LABEL_7:
*a2 = 0;
return result;
}
}
else
{
result = *a2;
if ( *a2 == -4 )
{
*(_BYTE *)(a1 + 31457284) = 1;
goto LABEL_7;
}
if ( result <= 0xFFFFF )
{
*(_DWORD *)(a1 + 31457292) = result;
goto LABEL_7;
}
}
*a2 = -2;
}
return result;
}

original_code:
void
disk_rd_port(disk_t *disk, uint32_t *dat, ram_t *ram)
{
 unsigned int i;
    switch (disk->state)
 {
  case DISK_READ_BUFFER_START:
   if (*dat == 0xFFFFFFFC) {
    disk->state = DISK_READ_BUFFER_SIZE;
   }
   else if ( (*dat) || *dat != 0xFFFFFFFE)
   {
    if(*dat >= (1024 * 1024))
    {
     *dat = 0xFFFFFFFE;
     break;
    }
    disk->mapped_buffer_start = *dat;
   }
            *dat = 0;
   break;
  case DISK_READ_BUFFER_SIZE:
   if (*dat == 0xFFFFFFFC) {
    disk->state = DISK_READ_REQUEST;
                *dat = 0xFFFFFFFD;
   }
   else if ((*dat) || *dat != 0xFFFFFFFE)
   {
    if ((*dat) + disk->mapped_buffer_start >= (1024 * 1024))
    {
     *dat = 0xFFFFFFFE;
     break;
    }
    disk->mapped_buffer_size = *dat;
                *dat = 0;
   }
   break;
  case DISK_READ_REQUEST:
   switch (*dat)
   {
    case 0xFFFFFFFC: disk->state = DISK_HOLD; break;
    case 0xFFFFFFFF:
     for(i = 0; i < disk->mapped_buffer_size; i++)
     {
      disk->address [disk->seek + i] =
         ram->address [disk->mapped_buffer_start + i];
     }
     *dat = 0xFFFFFFFD;
     break;
    case 0xFFFFFFFE: break;
    case 0xFFFFFFFD: break;
    case DISK_HOLD: break;
    default:
     if (*dat >= (1024 * 1024 * 30))
     {
                        printf ("Error: disk seek request greater than size of disk");
      *dat = 0xFFFFFFFE;
      return;
     }
     disk->seek = *dat;
     for (i = 0; i < disk->mapped_buffer_size &&
        i < (1024 * 1024 * 30);
        i++)
     {
      ram->address [disk->mapped_buffer_start + i] = disk->address [disk->seek + i];
     }
     *dat = 0xFFFFFFFD;
     break;
   }
            break;
        case DISK_HOLD:
            if(*dat == 0xFFFFFFFC) {
                disk->state = DISK_READ_BUFFER_START;
            }
            *dat = 0;
            break;
  default: disk->state = DISK_READ_BUFFER_START; break;
 }
}

canonical_original_code:
void disk_rd_port(struct disk_s *disk, unsigned int *dat, struct ram_s *ram)
{
 unsigned int i;
    switch (disk->state)
 {
  case DISK_READ_BUFFER_START:
   if (*dat == 0xFFFFFFFC) {
    disk->state = DISK_READ_BUFFER_SIZE;
   }
   else if ( (*dat) || *dat != 0xFFFFFFFE)
   {
    if(*dat >= (1024 * 1024))
    {
     *dat = 0xFFFFFFFE;
     break;
    }
    disk->mapped_buffer_start = *dat;
   }
            *dat = 0;
   break;
  case DISK_READ_BUFFER_SIZE:
   if (*dat == 0xFFFFFFFC) {
    disk->state = DISK_READ_REQUEST;
                *dat = 0xFFFFFFFD;
   }
   else if ((*dat) || *dat != 0xFFFFFFFE)
   {
    if ((*dat) + disk->mapped_buffer_start >= (1024 * 1024))
    {
     *dat = 0xFFFFFFFE;
     break;
    }
    disk->mapped_buffer_size = *dat;
                *dat = 0;
   }
   break;
  case DISK_READ_REQUEST:
   switch (*dat)
   {
    case 0xFFFFFFFC: disk->state = DISK_HOLD; break;
    case 0xFFFFFFFF:
     for(i = 0; i < disk->mapped_buffer_size; i++)
     {
      disk->address [disk->seek + i] =
         ram->address [disk->mapped_buffer_start + i];
     }
     *dat = 0xFFFFFFFD;
     break;
    case 0xFFFFFFFE: break;
    case 0xFFFFFFFD: break;
    case DISK_HOLD: break;
    default:
     if (*dat >= (1024 * 1024 * 30))
     {
                        printf ("Error: disk seek request greater than size of disk");
      *dat = 0xFFFFFFFE;
      return;
     }
     disk->seek = *dat;
     for (i = 0; i < disk->mapped_buffer_size &&
        i < (1024 * 1024 * 30);
        i++)
     {
      ram->address [disk->mapped_buffer_start + i] = disk->address [disk->seek + i];
     }
     *dat = 0xFFFFFFFD;
     break;
   }
            break;
        case DISK_HOLD:
            if(*dat == 0xFFFFFFFC) {
                disk->state = DISK_READ_BUFFER_START;
            }
            *dat = 0;
            break;
  default: disk->state = DISK_READ_BUFFER_START; break;
 }
}

variable_types: struct disk_s { unsigned char address[-1]; unsigned int mapped_buffer_size; unsigned char state; unsigned int seek; unsigned int mapped_buffer_start; } *

variable_types:
i
unsigned int

user_defined_types: struct instr_s { unsigned char opcode; unsigned char dst_src; unsigned short constant; }


decompiled_code:
void *__fastcall disk_get_bootsector(void *src, void *dest, size_t n)
{
return memcpy(dest, src, (unsigned int)n);
}

canonical_decompiled_code:
void *__fastcall func1(void *src, void *dest, size_t n)
{
return memcpy(dest, src, (unsigned int)n);
}

original_code:
void
disk_get_bootsector(disk_t *disk, ram_t *ram, uint32_t num_bytes)
{
    memcpy ((void *)ram->address, (void *)disk->address, num_bytes);
}

canonical_original_code:
void disk_get_bootsector(struct disk_s *disk, struct ram_s *ram, unsigned int num_bytes)
{
    memcpy ((void *)ram->address, (void *)disk->address, num_bytes);
}

user_defined_types: struct disk_s { unsigned char address[-1]; unsigned int mapped_buffer_size; unsigned char state; unsigned int seek; unsigned int mapped_buffer_start; }


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

result = file_utils_read(a1, 31457280LL, a2);
if ( !(_DWORD)result )
{
printf("Error: Could not open disk.dat");
exit(0);
}
return result;
}

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

result = file_utils_read(a1, 31457280LL, a2);
if ( !(_DWORD)result )
{
printf("Error: Could not open disk.dat");
exit(0);
}
return result;
}

original_code:
void
disk_load(disk_t *disk, const char *filename)
{
    if (!file_utils_read ((void *)disk->address,
                          sizeof (disk->address),
                          filename))
    {
        printf ("Error: Could not open ""disk.dat");
        exit(0);
    }
}

canonical_original_code:
void disk_load(struct disk_s *disk, char *filename)
{
    if (!file_utils_read ((void *)disk->address,
                          sizeof (disk->address),
                          filename))
    {
        printf ("Error: Could not open ""disk.dat");
        exit(0);
    }
}

variable_types:
filename
char *


decompiled_code:
int __fastcall disk_save(__int64 a1, __int64 a2)
{
int result; // eax

result = file_utils_write(a1, 31457280LL, a2);
if ( !result )
result = printf("Error: Could not open disk.dat");
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2)
{
int result; // eax

result = file_utils_write(a1, 31457280LL, a2);
if ( !result )
result = printf("Error: Could not open disk.dat");
return result;
}

original_code:
void
disk_save(disk_t *disk, const char *filename)
{
    if (!file_utils_write ((void *)disk->address,
                           sizeof (disk->address),
                           filename))
    {
        printf ("Error: Could not open ""disk.dat");
    }
}

canonical_original_code:
void disk_save(struct disk_s *disk, char *filename)
{
    if (!file_utils_write ((void *)disk->address,
                           sizeof (disk->address),
                           filename))
    {
        printf ("Error: Could not open ""disk.dat");
    }
}


original_code:
void
disp_init(disp_t* disp)
{
 memset (disp->buffer, 0x0, sizeof (disp->buffer));
 disp->state = DISP_READ_ADR;
    disp->buffer_map_adr = 0;
    disp->cursor.pos = 0;
    disp->cursor.shape = 0xdb;
    disp->cursor.enabled = 0;
    memset (Ghost_buf, 0, sizeof (Ghost_buf));
}

canonical_original_code:
void disp_init(struct disp_s *disp)
{
 memset (disp->buffer, 0x0, sizeof (disp->buffer));
 disp->state = DISP_READ_ADR;
    disp->buffer_map_adr = 0;
    disp->cursor.pos = 0;
    disp->cursor.shape = 0xdb;
    disp->cursor.enabled = 0;
    memset (Ghost_buf, 0, sizeof (Ghost_buf));
}

variable_types: struct disp_s { unsigned char buffer[-1]; unsigned int buffer_map_adr; unsigned char state; struct disp_hwcurs_s { unsigned short pos; unsigned char enabled; unsigned char shape; } cursor; } *

user_defined_types: struct disp_s { unsigned char buffer[-1]; unsigned int buffer_map_adr; unsigned char state; struct disp_hwcurs_s cursor; }


original_code:
void
disp_rd_port(disp_t *disp, uint32_t *dat, ram_t *ram)
{
    if (!(*dat))
 {
  return;
 }
    if (disp->state == DISP_READ_CHAR)
    {
        switch (*dat)
        {
            case 0x1:
                memset (disp->buffer, 0, sizeof (disp->buffer));
                break;
            case 0x2:
                disp_rd_adr_map (disp, ram);
                break;
            case 0xFFFFF:
                disp->state = DISP_READ_ADR;
                break;
            case 0xFFFFE:
                disp->cursor.enabled = 1;
                break;
            case 0xFFFFD:
                disp->cursor.enabled = 0;
                break;
            case 0xFFFFC:
                disp->state = DISP_READ_CURSOR_POS;
                break;
            case 0xFFFFB:
                disp->state = DISP_READ_CURSOR_SHAPE;
                break;
        }
    }
    else if (disp->state == DISP_READ_ADR)
    {
        if (*dat)
        {
            disp->buffer_map_adr = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    else if (disp->state == DISP_READ_CURSOR_POS)
    {
        if (*dat)
        {
            disp->cursor.pos = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    else if (disp->state == DISP_READ_CURSOR_SHAPE)
    {
        if (*dat)
        {
            disp->cursor.shape = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    *dat = 0;
}

canonical_original_code:
void disp_rd_port(struct disp_s *disp, unsigned int *dat, struct ram_s *ram)
{
    if (!(*dat))
 {
  return;
 }
    if (disp->state == DISP_READ_CHAR)
    {
        switch (*dat)
        {
            case 0x1:
                memset (disp->buffer, 0, sizeof (disp->buffer));
                break;
            case 0x2:
                disp_rd_adr_map (disp, ram);
                break;
            case 0xFFFFF:
                disp->state = DISP_READ_ADR;
                break;
            case 0xFFFFE:
                disp->cursor.enabled = 1;
                break;
            case 0xFFFFD:
                disp->cursor.enabled = 0;
                break;
            case 0xFFFFC:
                disp->state = DISP_READ_CURSOR_POS;
                break;
            case 0xFFFFB:
                disp->state = DISP_READ_CURSOR_SHAPE;
                break;
        }
    }
    else if (disp->state == DISP_READ_ADR)
    {
        if (*dat)
        {
            disp->buffer_map_adr = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    else if (disp->state == DISP_READ_CURSOR_POS)
    {
        if (*dat)
        {
            disp->cursor.pos = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    else if (disp->state == DISP_READ_CURSOR_SHAPE)
    {
        if (*dat)
        {
            disp->cursor.shape = *dat;
            disp->state = DISP_READ_CHAR;
        }
    }
    *dat = 0;
}

user_defined_types: struct disp_hwcurs_s { unsigned short pos; unsigned char enabled; unsigned char shape; }