decompiled_code:
__int64 load_id_handlers()
{
int v0; // ebx
_DWORD *v1; // rbp
unsigned int v2; // er12

v0 = 0;
while ( 1 )
{
v1 = init_id_handler(v0);
v2 = load_id_handler((__int64)v1);
if ( !(_BYTE)v2 )
break;
++v0;
AttachToList(v1, id_handlers);
if ( v0 == 4 )
return 1LL;
}
free_id_handler(v1);
bug("%s: could not load %s id handler.", "load_id_handlers", *((const char **)&id_handler_names + v0));
return v2;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // ebx
_DWORD *v1; // rbp
unsigned int v2; // er12

v0 = 0;
while ( 1 )
{
v1 = init_id_handler(v0);
v2 = func6((__int64)v1);
if ( !(_BYTE)v2 )
break;
++v0;
AttachToList(v1, id_handlers);
if ( v0 == 4 )
return 1LL;
}
func1(v1);
bug("%s: could not load %s id handler.", "load_id_handlers", *((const char **)&id_handler_names + v0));
return v2;
}

original_code:
bool load_id_handlers( void )
{
   int x;
   ID_HANDLER *handler;
   for( x = 0; x < MAX_STRUCT; x++ )
   {
      handler = init_id_handler( x );
      if( !load_id_handler( handler ) )
      {
         free_id_handler( handler );
         bug( "%s: could not load %s id handler.", __FUNCTION__, id_handler_names[x] );
         return 0;
      }
      AttachToList( handler, id_handlers );
   }
   return 1;
}

canonical_original_code:
unsigned char load_id_handlers()
{
   int x;
   struct id_handler *handler;
   for( x = 0; x < MAX_STRUCT; x++ )
   {
      handler = init_id_handler( x );
      if( !load_id_handler( handler ) )
      {
         free_id_handler( handler );
         bug( "%s: could not load %s id handler.", __FUNCTION__, id_handler_names[x] );
         return 0;
      }
      AttachToList( handler, id_handlers );
   }
   return 1;
}

variable_types: x int


decompiled_code:
__int64 __fastcall get_id_handler(int a1)
{
__int64 v1; // rax
__int64 v3; // [rsp+8h] [rbp-20h]
char v4[24]; // [rsp+10h] [rbp-18h] BYREF

AttachIterator(v4, id_handlers);
do
v1 = NextInList(v4);
while ( v1 && *(_DWORD *)(v1 + 8) != a1 );
v3 = v1;
DetachIterator(v4);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 v1; // rax
__int64 v3; // [rsp+8h] [rbp-20h]
char v4[24]; // [rsp+10h] [rbp-18h] BYREF

AttachIterator(v4, id_handlers);
do
v1 = NextInList(v4);
while ( v1 && *(_DWORD *)(v1 + 8) != a1 );
v3 = v1;
DetachIterator(v4);
return v3;
}

original_code:
ID_HANDLER *get_id_handler( int type )
{
   ID_HANDLER *handler = ((void *)0);
   ITERATOR Iter;
   AttachIterator( &Iter, id_handlers );
   while( ( handler = (ID_HANDLER *)NextInList( &Iter ) ) != ((void *)0) )
      if( handler->type == type )
         break;
   DetachIterator( &Iter );
   return handler;
}

canonical_original_code:
struct id_handler *get_id_handler(int type)
{
   struct id_handler *handler = ((void *)0);
   struct Iterator Iter;
   AttachIterator( &Iter, id_handlers );
   while( ( handler = (struct id_handler *)NextInList( &Iter ) ) != ((void *)0) )
      if( handler->type == type )
         break;
   DetachIterator( &Iter );
   return handler;
}

variable_types: type int

variable_types: struct id_handler { struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *free_ids; int type; int top_id; } *

variable_types: struct Iterator { struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *_pList; struct Cell *_pCell; }

user_defined_types: struct Iterator { struct List *_pList; struct Cell *_pCell; }

user_defined_types: struct id_handler { struct List *free_ids; int type; int top_id; }

user_defined_types: struct List { struct Cell *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; }


decompiled_code:
__int64 __fastcall check_free(_QWORD *a1)
{
__int64 v1; // r12
_BYTE v3[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = 0LL;
if ( (int)SizeOfList(*a1) > 0 )
{
AttachIterator(v3, *a1);
v1 = NextInList(v3);
DetachFromList(v1, *a1);
DetachIterator(v3);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1)
{
__int64 v1; // r12
_BYTE v3[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = 0LL;
if ( (int)SizeOfList(*a1) > 0 )
{
AttachIterator(v3, *a1);
v1 = NextInList(v3);
DetachFromList(v1, *a1);
DetachIterator(v3);
}
return v1;
}

original_code:
I_ID *check_free( ID_HANDLER *handler )
{
   ITERATOR Iter;
   I_ID *id;
   if( SizeOfList( handler->free_ids ) <= 0 )
      return ((void *)0);
   AttachIterator( &Iter, handler->free_ids );
   id = NextInList( &Iter );
   DetachFromList( id, handler->free_ids );
   DetachIterator( &Iter );
   return id;
}

canonical_original_code:
struct instance_id *check_free(struct id_handler *handler)
{
   struct Iterator Iter;
   struct instance_id *id;
   if( SizeOfList( handler->free_ids ) <= 0 )
      return ((void *)0);
   AttachIterator( &Iter, handler->free_ids );
   id = NextInList( &Iter );
   DetachFromList( id, handler->free_ids );
   DetachIterator( &Iter );
   return id;
}

variable_types: struct instance_id { int id; char *created_by; char *created_on; char *modified_by; char *last_modified; } *

user_defined_types: struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; }


decompiled_code:
__int64 __fastcall use_top_id(__int64 a1)
{
++*(_DWORD *)(a1 + 12);
save_id_handler(a1);
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
++*(_DWORD *)(a1 + 12);
func5(a1);
return *(unsigned int *)(a1 + 12);
}

original_code:
int use_top_id( ID_HANDLER *handler )
{
   handler->top_id++;
   save_id_handler( handler );
   return handler->top_id;
}

canonical_original_code:
int use_top_id(struct id_handler *handler)
{
   handler->top_id++;
   save_id_handler( handler );
   return handler->top_id;
}

return_type: int


decompiled_code:
__int64 __fastcall get_top_id(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
int get_top_id( ID_HANDLER *handler )
{
   return handler->top_id;
}

canonical_original_code:
int get_top_id(struct id_handler *handler)
{
   return handler->top_id;
}


decompiled_code:
__int64 __fastcall match_string_table(char *s1, const char **a2)
{
const char *v3; // rsi
__int64 v4; // rbx

v3 = *a2;
if ( !v3 )
return 0xFFFFFFFFLL;
v4 = 0LL;
while ( strcmp(s1, v3) )
{
v3 = a2[++v4];
if ( !v3 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1, const char **a2)
{
const char *v3; // rsi
__int64 v4; // rbx

v3 = *a2;
if ( !v3 )
return 0xFFFFFFFFLL;
v4 = 0LL;
while ( strcmp(s1, v3) )
{
v3 = a2[++v4];
if ( !v3 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v4;
}

original_code:
int match_string_table( const char *string, const char *const string_table[] )
{
   int x;
   for( x = 0; string_table[x] != '\0'; x++ )
      if( !strcmp( string, string_table[x] ) )
         return x;
   return -1;
}

canonical_original_code:
int match_string_table(char *string, char *string_table[])
{
   int x;
   for( x = 0; string_table[x] != '\0'; x++ )
      if( !strcmp( string, string_table[x] ) )
         return x;
   return -1;
}

variable_types:
string
char *


decompiled_code:
char *__fastcall get_table(const char **a1)
{
const char *v1; // rsi
const char **v2; // rbx

memset(buf_0, 0, sizeof(buf_0));
v1 = *a1;
if ( **a1 )
{
v2 = a1 + 1;
do
{
++v2;
strcat(buf_0, v1);
v1 = *(v2 - 1);
}
while ( *v1 );
}
buf_0[strlen(buf_0)] = 0;
return buf_0;
}

canonical_decompiled_code:
char *__fastcall func1(const char **a1)
{
const char *v1; // rsi
const char **v2; // rbx

memset(buf_0, 0, sizeof(buf_0));
v1 = *a1;
if ( **a1 )
{
v2 = a1 + 1;
do
{
++v2;
strcat(buf_0, v1);
v1 = *(v2 - 1);
}
while ( *v1 );
}
buf_0[strlen(buf_0)] = 0;
return buf_0;
}

original_code:
const char *get_table( const char *const string_table[] )
{
   static char buf[2048];
   int x;
   memset( &buf[0], 0, sizeof( buf ) );
   for( x = 0; string_table[x][0] != '\0'; x++ )
      strcat( buf, string_table[x] );
   buf[strlen(buf)] = '\0';
   return buf;
}

canonical_original_code:
char *get_table(char *string_table[])
{
   static char buf[2048];
   int x;
   memset( &buf[0], 0, sizeof( buf ) );
   for( x = 0; string_table[x][0] != '\0'; x++ )
      strcat( buf, string_table[x] );
   buf[strlen(buf)] = '\0';
   return buf;
}

variable_types:
buf
char [2048]

return_type: char *


decompiled_code:
__int64 __fastcall str_table_max_strlen(_BYTE **a1)
{
_BYTE **v1; // rbx
unsigned int v2; // er12
const char *v3; // rax
int v4; // eax
_BYTE *v5; // rdi

if ( !**a1 )
return 0LL;
v1 = a1 + 1;
v2 = 0;
do
{
v3 = (const char *)smash_color();
v4 = strlen(v3);
v5 = *v1;
if ( (int)v2 < v4 )
v2 = v4;
++v1;
}
while ( *v5 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE **a1)
{
_BYTE **v1; // rbx
unsigned int v2; // er12
const char *v3; // rax
int v4; // eax
_BYTE *v5; // rdi

if ( !**a1 )
return 0LL;
v1 = a1 + 1;
v2 = 0;
do
{
v3 = (const char *)smash_color();
v4 = strlen(v3);
v5 = *v1;
if ( (int)v2 < v4 )
v2 = v4;
++v1;
}
while ( *v5 );
return v2;
}

original_code:
int str_table_max_strlen( const char *const table[] )
{
   int x, j, length;
   for( x = 0, length = 0; table[x][0] != '\0'; x++ )
      if( ( j = strlen( smash_color( table[x] ) ) ) > length )
         length = j;
   return length;
}

canonical_original_code:
int str_table_max_strlen(char *table[])
{
   int x; int j; int length;
   for( x = 0, length = 0; table[x][0] != '\0'; x++ )
      if( ( j = strlen( smash_color( table[x] ) ) ) > length )
         length = j;
   return length;
}

variable_types:
table
char *[]

variable_types: j int

variable_types: length int


decompiled_code:
__int64 __fastcall fit_space_with_color(char *a1, const char *a2, int a3)
{
const char *v4; // rax
int v5; // eax

snprintf(a1, 0x800uLL, "%s", a2);
v4 = (const char *)smash_color(a1);
v5 = strlen(v4);
return add_spaces(a1, (unsigned int)(a3 - v5));
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, const char *a2, int a3)
{
const char *v4; // rax
int v5; // eax

snprintf(a1, 0x800uLL, "%s", a2);
v4 = (const char *)smash_color(a1);
v5 = strlen(v4);
return add_spaces(a1, (unsigned int)(a3 - v5));
}

original_code:
void fit_space_with_color( char *dest, const char *orig, int space )
{
   snprintf( dest, 2048, "%s", orig );
   add_spaces( dest, ( space - strlen( smash_color( dest ) ) ) );
}

canonical_original_code:
void fit_space_with_color(char *dest, char *orig, int space)
{
   snprintf( dest, 2048, "%s", orig );
   add_spaces( dest, ( space - strlen( smash_color( dest ) ) ) );
}

variable_types:
dest
char *

variable_types:
orig
char *

variable_types: space int

return_type: void


AllocStack

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

result = malloc(0x10uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
return result;
}

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

result = malloc(0x10uLL);
*(_QWORD *)result = 0LL;
result[2] = 0;
return result;
}

original_code:
STACK *AllocStack()
{
  STACK *pStack;
  pStack = malloc(sizeof(*pStack));
  pStack->_pCells = ((void *)0);
  pStack->_iSize = 0;
  return pStack;
}

canonical_original_code:
struct BBGStack *AllocStack()
{
  struct BBGStack *pStack;
  pStack = malloc(sizeof(*pStack));
  pStack->_pCells = ((void *)0);
  pStack->_iSize = 0;
  return pStack;
}

variable_types: struct BBGStack { struct BBGStackCell { struct BBGStackCell *_pNext; void *_pContent; } *_pCells; int _iSize; } *

user_defined_types: struct BBGStack { struct BBGStackCell *_pCells; int _iSize; }


FreeStack

decompiled_code:
void __fastcall FreeStack(void *ptr)
{
_QWORD *v2; // rdi

v2 = *(_QWORD **)ptr;
if ( v2 )
{
do
{
*(_QWORD *)ptr = *v2;
free(v2);
v2 = *(_QWORD **)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v2; // rdi

v2 = *(_QWORD **)ptr;
if ( v2 )
{
do
{
*(_QWORD *)ptr = *v2;
free(v2);
v2 = *(_QWORD **)ptr;
}
while ( *(_QWORD *)ptr );
}
free(ptr);
}

original_code:
void FreeStack(STACK *pStack)
{
  SCELL *pCell;
  while ((pCell = pStack->_pCells) != ((void *)0))
  {
    pStack->_pCells = pCell->_pNext;
    free(pCell);
  }
  free(pStack);
}

canonical_original_code:
void FreeStack(struct BBGStack *pStack)
{
  struct BBGStackCell *pCell;
  while ((pCell = pStack->_pCells) != ((void *)0))
  {
    pStack->_pCells = pCell->_pNext;
    free(pCell);
  }
  free(pStack);
}

variable_types: struct BBGStackCell { struct BBGStackCell *_pNext; void *_pContent; } *


StackSize

decompiled_code:
__int64 __fastcall StackSize(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int StackSize(STACK *pStack)
{
  return pStack->_iSize;
}

canonical_original_code:
int StackSize(struct BBGStack *pStack)
{
  return pStack->_iSize;
}

user_defined_types: struct BBGStackCell { struct BBGStackCell *_pNext; void *_pContent; }


decompiled_code:
__int64 __fastcall get_coord_hash(int a1)
{
return abs32(a1) % 0x64;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return abs32(a1) % 0x64;
}

original_code:
int get_coord_hash( int x )
{
   return abs(x) % 100;
}

canonical_original_code:
int get_coord_hash(int x)
{
   return abs(x) % 100;
}


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

switch ( a1 )
{
case 0:
case 1:
case 6:
case 7:
result = (unsigned int)(a1 + 2);
break;
case 2:
case 3:
case 8:
case 9:
result = (unsigned int)(a1 - 2);
break;
case 4:
result = 5LL;
break;
case 5:
result = 4LL;
break;
default:
JUMPOUT(0x542LL);
}
return result;
}

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

switch ( a1 )
{
case 0:
case 1:
case 6:
case 7:
result = (unsigned int)(a1 + 2);
break;
case 2:
case 3:
case 8:
case 9:
result = (unsigned int)(a1 - 2);
break;
case 4:
result = 5LL;
break;
case 5:
result = 4LL;
break;
default:
JUMPOUT(0x542LL);
}
return result;
}

original_code:
int get_directional_opposite( int dir )
{
   switch( dir )
   {
      case DIR_NORTH:
      case DIR_EAST:
      case DIR_NORTH_EAST:
      case DIR_NORTH_WEST:
         return ( dir + 2 );
      case DIR_SOUTH:
      case DIR_WEST:
      case DIR_SOUTH_WEST:
      case DIR_SOUTH_EAST:
         return ( dir - 2 );
      case DIR_UP:
         return ( dir + 1 );
      case DIR_DOWN:
         return ( dir - 1 );
   }
   return -1;
}

canonical_original_code:
int get_directional_opposite(int dir)
{
   switch( dir )
   {
      case DIR_NORTH:
      case DIR_EAST:
      case DIR_NORTH_EAST:
      case DIR_NORTH_WEST:
         return ( dir + 2 );
      case DIR_SOUTH:
      case DIR_WEST:
      case DIR_SOUTH_WEST:
      case DIR_SOUTH_EAST:
         return ( dir - 2 );
      case DIR_UP:
         return ( dir + 1 );
      case DIR_DOWN:
         return ( dir - 1 );
   }
   return -1;
}

variable_types: dir int


decompiled_code:
void can_occupy()
{
;
}

canonical_decompiled_code:
void func8()
{
;
}

original_code:
bool can_occupy( int x, int y, int z )
{
}

canonical_original_code:
unsigned char can_occupy(int x, int y, int z)
{
}

variable_types: y int

variable_types: z int

return_type: unsigned char


decompiled_code:
__int64 __fastcall check_name(const char *a1)
{
int v1; // eax
unsigned int v2; // edx
int v3; // ebx
__int64 result; // rax
const unsigned __int16 *v5; // rcx
__int64 v6; // rax

v1 = strlen(a1);
v2 = v1 - 3;
v3 = v1;
result = 0LL;
if ( v2 <= 9 )
{
v5 = *_ctype_b_loc();
v6 = 0LL;
while ( (v5[a1[v6]] & 0x400) != 0 )
{
if ( v3 <= (int)++v6 )
return 1LL;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
int v1; // eax
unsigned int v2; // edx
int v3; // ebx
__int64 result; // rax
const unsigned __int16 *v5; // rcx
__int64 v6; // rax

v1 = strlen(a1);
v2 = v1 - 3;
v3 = v1;
result = 0LL;
if ( v2 <= 9 )
{
v5 = *_ctype_b_loc();
v6 = 0LL;
while ( (v5[a1[v6]] & 0x400) != 0 )
{
if ( v3 <= (int)++v6 )
return 1LL;
}
result = 0LL;
}
return result;
}

original_code:
bool check_name(const char *name)
{
  int size, i;
  if ((size = strlen(name)) < 3 || size > 12)
    return 0;
  for (i = 0 ;i < size; i++)
    if (!((*__ctype_b_loc ())[(int) ((name[i]))] & (unsigned short int) _ISalpha)) return 0;
  return 1;
}

canonical_original_code:
unsigned char check_name(char *name)
{
  int size; int i;
  if ((size = strlen(name)) < 3 || size > 12)
    return 0;
  for (i = 0 ;i < size; i++)
    if (!((*__ctype_b_loc ())[(int) ((name[i]))] & (unsigned short) _ISalpha)) return 0;
  return 1;
}

variable_types:
name
char *

variable_types: size int

variable_types: i int


decompiled_code:
// attributes: thunk
__int64 load_muddata()
{
return load_helps();
}

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

original_code:
void load_muddata(bool fCopyOver)
{
  load_helps();
}

canonical_original_code:
void load_muddata(unsigned char fCopyOver)
{
  load_helps();
}

variable_types:
fCopyOver
unsigned char


decompiled_code:
void *get_time()
{
char *v0; // rcx
__int64 i; // rax
char v2; // dl

v0 = ctime(&current_time);
for ( i = 0LL; i != 15; *(_BYTE *)(i + 1503) = v2 )
v2 = v0[i++ + 4];
byte_5EF = 0;
return &buf_1;
}

canonical_decompiled_code:
void *func3()
{
char *v0; // rcx
__int64 i; // rax
char v2; // dl

v0 = ctime(&current_time);
for ( i = 0LL; i != 15; *(_BYTE *)(i + 1503) = v2 )
v2 = v0[i++ + 4];
byte_5EF = 0;
return &buf_1;
}

original_code:
char *get_time()
{
  static char buf[16];
  char *strtime;
  int i;
  strtime = ctime(&current_time);
  for (i = 0; i < 15; i++)
    buf[i] = strtime[i + 4];
  buf[15] = '\0';
  return buf;
}

variable_types:
buf
char [16]

variable_types:
strtime
char *


decompiled_code:
// attributes: thunk
void free_command(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func5(void *ptr)
{
free(ptr);
}

original_code:
void free_command( COMMAND *command )
{
   command->cmd_name = ((void *)0);
   command->cmd_funct = ((void *)0);
   free(command);
}

canonical_original_code:
void free_command(struct typCmd *command)
{
   command->cmd_name = ((void *)0);
   command->cmd_funct = ((void *)0);
   free(command);
}

variable_types: struct typCmd { char *cmd_name; void (*cmd_funct)(void *passed, char *arg); short level; short state; } *


decompiled_code:
__int64 __fastcall count_color(const char *a1)
{
unsigned int v1; // er12
const char *v2; // rdx
int v3; // eax

if ( !a1 )
return 0LL;
v1 = 0;
if ( *a1 )
{
v2 = &a1[strlen(a1)];
while ( a1 != v2 )
{
v3 = *a1++ == 35;
v1 += v3;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
unsigned int v1; // er12
const char *v2; // rdx
int v3; // eax

if ( !a1 )
return 0LL;
v1 = 0;
if ( *a1 )
{
v2 = &a1[strlen(a1)];
while ( a1 != v2 )
{
v3 = *a1++ == 35;
v1 += v3;
}
}
return v1;
}

original_code:
int count_color( const char *str )
{
   int x;
   int count = 0;
   if( !str || str[0] == '\0' )
      return count;
   for( x = 0; x < strlen( str ); x++ )
      if( str[x] == '#' )
         count++;
   return count;
}

canonical_original_code:
int count_color(char *str)
{
   int x;
   int count = 0;
   if( !str || str[0] == '\0' )
      return count;
   for( x = 0; x < strlen( str ); x++ )
      if( str[x] == '#' )
         count++;
   return count;
}

variable_types:
str
char *

variable_types: x int

variable_types: count int


decompiled_code:
__int64 __fastcall is_number(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
char *v3; // rax
__int64 result; // rax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
if ( (_BYTE)v1 == 45 )
goto LABEL_4;
while ( ((*_ctype_b_loc())[v1] & 0x800) != 0 )
{
LABEL_4:
v3 = v2++;
v1 = v3[1];
result = 1LL;
if ( !(_BYTE)v1 )
return result;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
char *v3; // rax
__int64 result; // rax

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
if ( (_BYTE)v1 == 45 )
goto LABEL_4;
while ( ((*_ctype_b_loc())[v1] & 0x800) != 0 )
{
LABEL_4:
v3 = v2++;
v1 = v3[1];
result = 1LL;
if ( !(_BYTE)v1 )
return result;
}
}
return 0LL;
}

original_code:
bool is_number( const char *arg )
{
   bool first = 1;
   if( *arg == '\0' )
      return 0;
   for( ; *arg != '\0'; arg++ )
   {
      if( first && *arg == '-' )
      {
         first = 0;
         continue;
      }
      if( !((*__ctype_b_loc ())[(int) ((*arg))] & (unsigned short int) _ISdigit) )
         return 0;
      first = 0;
   }
   return 1;
}

canonical_original_code:
unsigned char is_number(char *arg)
{
   unsigned char first = 1;
   if( *arg == '\0' )
      return 0;
   for( ; *arg != '\0'; arg++ )
   {
      if( first && *arg == '-' )
      {
         first = 0;
         continue;
      }
      if( !((*__ctype_b_loc ())[(int) ((*arg))] & (unsigned short) _ISdigit) )
         return 0;
      first = 0;
   }
   return 1;
}

variable_types:
arg
char *

variable_types:
first
unsigned char


decompiled_code:
const char *__fastcall unload_commands(__int64 a1, const char **a2, int a3, int a4)
{
const char *result; // rax
const char **v7; // rbx
__int16 *v8; // rbp
_BYTE v9[56]; // [rsp-38h] [rbp-38h] BYREF

result = *a2;
if ( **a2 )
{
v7 = a2;
do
{
AttachIterator(v9, a1);
while ( 1 )
{
v8 = (__int16 *)NextInList(v9);
if ( !v8 )
break;
while ( !strcmp(*(const char **)v8, *v7) && v8[9] == a3 && v8[8] <= a4 )
{
DetachFromList(v8, a1);
free_command(v8);
v8 = (__int16 *)NextInList(v9);
if ( !v8 )
goto LABEL_9;
}
}
LABEL_9:
v7 += 3;
DetachIterator(v9);
result = *v7;
}
while ( **v7 );
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func9(__int64 a1, const char **a2, int a3, int a4)
{
const char *result; // rax
const char **v7; // rbx
__int16 *v8; // rbp
_BYTE v9[56]; // [rsp-38h] [rbp-38h] BYREF

result = *a2;
if ( **a2 )
{
v7 = a2;
do
{
AttachIterator(v9, a1);
while ( 1 )
{
v8 = (__int16 *)NextInList(v9);
if ( !v8 )
break;
while ( !strcmp(*(const char **)v8, *v7) && v8[9] == a3 && v8[8] <= a4 )
{
DetachFromList(v8, a1);
func5(v8);
v8 = (__int16 *)NextInList(v9);
if ( !v8 )
goto LABEL_9;
}
}
LABEL_9:
v7 += 3;
DetachIterator(v9);
result = *v7;
}
while ( **v7 );
}
return result;
}

original_code:
void unload_commands( LIST *commands, const struct typCmd to_unload[], int state, int level )
{
   COMMAND *com;
   ITERATOR Iter;
   int x;
   for( x = 0; to_unload[x].cmd_name[0] != '\0'; x++ )
   {
      AttachIterator( &Iter, commands );
      while( ( com = (COMMAND *)NextInList( &Iter ) ) != ((void *)0) )
         if( !strcmp( com->cmd_name, to_unload[x].cmd_name ) && com->state == state && com->level <= level )
         {
            DetachFromList( com, commands );
            free_command( com );
         }
      DetachIterator( &Iter );
   }
   return;
}

canonical_original_code:
void unload_commands(struct List *commands, struct typCmd to_unload[], int state, int level)
{
   struct typCmd *com;
   struct Iterator Iter;
   int x;
   for( x = 0; to_unload[x].cmd_name[0] != '\0'; x++ )
   {
      AttachIterator( &Iter, commands );
      while( ( com = (struct typCmd *)NextInList( &Iter ) ) != ((void *)0) )
         if( !strcmp( com->cmd_name, to_unload[x].cmd_name ) && com->state == state && com->level <= level )
         {
            DetachFromList( com, commands );
            free_command( com );
         }
      DetachIterator( &Iter );
   }
   return;
}

variable_types: struct typCmd { char *cmd_name; void (*cmd_funct)(void *passed, char *arg); short level; short state; } []

variable_types: state int

variable_types: level int

variable_types: struct Iterator { struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *_pList; struct Cell *_pCell; }


decompiled_code:
__int64 __fastcall clear_commands(__int64 a1)
{
__int64 v1; // rax
void *v2; // rbp
_BYTE v4[40]; // [rsp+0h] [rbp-28h] BYREF

AttachIterator(v4, a1);
while ( 1 )
{
v1 = NextInList(v4);
v2 = (void *)v1;
if ( !v1 )
break;
DetachFromList(v1, a1);
free_command(v2);
}
return DetachIterator(v4);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rax
void *v2; // rbp
_BYTE v4[40]; // [rsp+0h] [rbp-28h] BYREF

AttachIterator(v4, a1);
while ( 1 )
{
v1 = NextInList(v4);
v2 = (void *)v1;
if ( !v1 )
break;
DetachFromList(v1, a1);
func5(v2);
}
return DetachIterator(v4);
}

original_code:
void clear_commands( LIST *commands )
{
   COMMAND *com;
   ITERATOR Iter;
   AttachIterator( &Iter, commands );
   while( ( com = (COMMAND *)NextInList( &Iter ) ) != ((void *)0) )
   {
      DetachFromList( com, commands );
      free_command( com );
   }
   DetachIterator( &Iter );
   return;
}

canonical_original_code:
void clear_commands(struct List *commands)
{
   struct typCmd *com;
   struct Iterator Iter;
   AttachIterator( &Iter, commands );
   while( ( com = (struct typCmd *)NextInList( &Iter ) ) != ((void *)0) )
   {
      DetachFromList( com, commands );
      free_command( com );
   }
   DetachIterator( &Iter );
   return;
}

variable_types: struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *

user_defined_types: struct typCmd { char *cmd_name; void (*cmd_funct)(void *passed, char *arg); short level; short state; }


decompiled_code:
__int64 __fastcall free_command_list(__int64 a1)
{
clear_commands(a1);
return FreeList(a1);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
func10(a1);
return FreeList(a1);
}

original_code:
void free_command_list( LIST *commands )
{
   clear_commands( commands );
   FreeList( commands );
}

canonical_original_code:
void free_command_list(struct List *commands)
{
   clear_commands( commands );
   FreeList( commands );
}


decompiled_code:
void __fastcall clear_shape(__int64 a1)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 16) = 0;
}

original_code:
void clear_shape( SHAPE *shape )
{
   shape->name = ((void *)0);
   shape->max_x = 0;
   shape->max_y = 0;
   shape->max_z = 0;
   return;
}

canonical_original_code:
void clear_shape(struct shape *shape)
{
   shape->name = ((void *)0);
   shape->max_x = 0;
   shape->max_y = 0;
   shape->max_z = 0;
   return;
}

variable_types: struct shape { char *name; int max_x; int max_y; int max_z; struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *offsets; } *

user_defined_types: struct shape { char *name; int max_x; int max_y; int max_z; struct List *offsets; }


decompiled_code:
_QWORD *init_shape()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = calloc(1uLL, 0x20uLL);
if ( !v0 )
{
perror("calloc failure");
fprintf(stderr, "Malloc failure @ %s:%d\n", "shape.c", 10LL);
abort();
}
v1 = v0;
clear_shape((__int64)v0);
v1[3] = AllocList();
return v1;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *v0; // rax
_QWORD *v1; // r12

v0 = calloc(1uLL, 0x20uLL);
if ( !v0 )
{
perror("calloc failure");
fprintf(stderr, "Malloc failure @ %s:%d\n", "shape.c", 10LL);
abort();
}
v1 = v0;
func0((__int64)v0);
v1[3] = AllocList();
return v1;
}

original_code:
SHAPE *init_shape( void )
{
   SHAPE *shape;
   do { if (!((shape) = (SHAPE *) calloc ((1), sizeof(SHAPE)))) { perror("calloc failure"); fprintf(stderr, "Malloc failure @ %s:%d\n", "shape.c", 10 ); abort(); } } while(0);
   clear_shape( shape );
   shape->offsets = AllocList();
   return shape;
}

canonical_original_code:
struct shape *init_shape()
{
   struct shape *shape;
   do { if (!((shape) = (struct shape *) calloc ((1), sizeof(SHAPE)))) { perror("calloc failure"); fprintf(stderr, "Malloc failure @ %s:%d\n", "shape.c", 10 ); abort(); } } while(0);
   clear_shape( shape );
   shape->offsets = AllocList();
   return shape;
}

return_type: struct shape { char *name; int max_x; int max_y; int max_z; struct List { struct Cell { struct Cell *_pNextCell; struct Cell *_pPrevCell; void *_pContent; int _valid; } *_pFirstCell; struct Cell *_pLastCell; int _iterators; int _size; int _valid; } *offsets; } *


decompiled_code:
void __fastcall free_shape(void *ptr)
{
if ( *(_QWORD *)ptr )
free(ptr);
free_offset_list(*((_QWORD *)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
if ( *(_QWORD *)ptr )
free(ptr);
free_offset_list(*((_QWORD *)ptr + 3));
free(ptr);
}

original_code:
void free_shape( SHAPE *shape )
{
   if( shape->name )
      free( shape );
   free_offset_list( shape->offsets );
   free( shape );
   return;
}

canonical_original_code:
void free_shape(struct shape *shape)
{
   if( shape->name )
      free( shape );
   free_offset_list( shape->offsets );
   free( shape );
   return;
}


decompiled_code:
__time_t __fastcall last_modified(const char *a1)
{
bool v1; // sf
__time_t result; // rax
struct stat v3; // [rsp+0h] [rbp-898h] BYREF
char s[2056]; // [rsp+90h] [rbp-808h] BYREF

snprintf(s, 0x800uLL, "../help/%s", a1);
v1 = stat(s, &v3) < 0;
result = 0LL;
if ( !v1 )
result = v3.st_mtim.tv_sec;
return result;
}

canonical_decompiled_code:
__time_t __fastcall func4(const char *a1)
{
bool v1; // sf
__time_t result; // rax
struct stat v3; // [rsp+0h] [rbp-898h] BYREF
char s[2056]; // [rsp+90h] [rbp-808h] BYREF

snprintf(s, 0x800uLL, "../help/%s", a1);
v1 = stat(s, &v3) < 0;
result = 0LL;
if ( !v1 )
result = v3.st_mtim.tv_sec;
return result;
}

original_code:
time_t last_modified(char *helpfile)
{
  char fHelp[2048];
  struct stat sBuf;
  time_t mTime = 0;
  snprintf(fHelp, 2048, "../help/%s", helpfile);
  if (stat(fHelp, &sBuf) >= 0)
    mTime = sBuf.st_mtim.tv_sec;
  return mTime;
}

canonical_original_code:
long last_modified(char *helpfile)
{
  char fHelp[2048];
  struct stat sBuf;
  long mTime = 0;
  snprintf(fHelp, 2048, "../help/%s", helpfile);
  if (stat(fHelp, &sBuf) >= 0)
    mTime = sBuf.st_mtim.tv_sec;
  return mTime;
}

variable_types:
helpfile
char *

variable_types:
fHelp
char [2048]

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

variable_types: mTime long

return_type: long

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


decompiled_code:
_BOOL8 __fastcall verify_pcap(const char *a1)
{
char *v1; // rax
_BOOL4 v2; // er8

v1 = strrchr(a1, 46);
v2 = 0;
if ( v1 )
v2 = strcmp(v1 + 1, "pcap") == 0;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1)
{
char *v1; // rax
_BOOL4 v2; // er8

v1 = strrchr(a1, 46);
v2 = 0;
if ( v1 )
v2 = strcmp(v1 + 1, "pcap") == 0;
return v2;
}

original_code:
int verify_pcap( const char *filename )
{
      char *p = strrchr(filename,'.');
      if ( p != ((void *)0))
     return ( strcmp(++p, "pcap") == 0 );
      return 0;
}

canonical_original_code:
int verify_pcap(char *filename)
{
      char *p = strrchr(filename,'.');
      if ( p != ((void *)0))
     return ( strcmp(++p, "pcap") == 0 );
      return 0;
}

variable_types:
filename
char *

variable_types:
p
char *


decompiled_code:
__int64 ip_after_mac()
{
__int16 v0; // r8
__int64 result; // rax
__int16 v2; // dx
__int16 v3; // ax
__int16 v4; // dx

v0 = _bswap_16(2048);
result = 1LL;
if ( v2 != v0 )
{
v3 = _bswap_16(34525);
result = 2 * (unsigned int)(v4 == v3);
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int16 v0; // r8
__int64 result; // rax
__int16 v2; // dx
__int16 v3; // ax
__int16 v4; // dx

v0 = func0(2048);
result = 1LL;
if ( v2 != v0 )
{
v3 = func0(34525);
result = 2 * (unsigned int)(v4 == v3);
}
return result;
}

original_code:
int ip_after_mac( const u_char *packet )
{
      struct ether_header *header = (struct ether_header *)packet;
      if ( header->ether_type == ntohs(0x0800) )
     return 1;
      if ( header->ether_type == ntohs(0x86dd) )
     return 2;
      return 0;
}

canonical_original_code:
int ip_after_mac(unsigned char *packet)
{
      struct ether_header *header = (struct ether_header *)packet;
      if ( header->ether_type == ntohs(0x0800) )
     return 1;
      if ( header->ether_type == ntohs(0x86dd) )
     return 2;
      return 0;
}

variable_types:
packet
unsigned char *

variable_types: struct ether_header { unsigned char ether_dhost[6]; unsigned char ether_shost[6]; unsigned short ether_type; } *


decompiled_code:
__int64 __fastcall get_ipdaddr(__int64 a1)
{
return *(unsigned int *)(a1 + 30);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(unsigned int *)(a1 + 30);
}

original_code:
u_int32_t get_ipdaddr( const u_char *packet )
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return header->daddr;
}

canonical_original_code:
unsigned int get_ipdaddr(unsigned char *packet)
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return header->daddr;
}

variable_types: struct iphdr { unsigned int ihl; unsigned int version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short frag_off; unsigned char ttl; unsigned char protocol; unsigned short check; unsigned int saddr; unsigned int daddr; } *

return_type: unsigned int


decompiled_code:
__int64 __fastcall get_ip6daddr(__int64 a1)
{
return a1 + 38;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return a1 + 38;
}

original_code:
uint8_t *get_ip6daddr( const u_char *packet )
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return header->ip6_dst.__in6_u.__u6_addr8;
}

canonical_original_code:
unsigned char *get_ip6daddr(unsigned char *packet)
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return header->ip6_dst.__in6_u.__u6_addr8;
}

variable_types: struct ip6_hdr { union { struct ip6_hdrctl { unsigned int ip6_un1_flow; unsigned short ip6_un1_plen; unsigned char ip6_un1_nxt; unsigned char ip6_un1_hlim; } ip6_un1; unsigned char ip6_un2_vfc; } ip6_ctlun; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ip6_src; struct in6_addr ip6_dst; } *

return_type: unsigned char *

user_defined_types: struct ip6_hdr { union { struct ip6_hdrctl ip6_un1; unsigned char ip6_un2_vfc; } ip6_ctlun; struct in6_addr ip6_src; struct in6_addr ip6_dst; }


decompiled_code:
_BOOL8 __fastcall verify_daddr(__int64 a1, _DWORD *a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // eax
__int64 v6; // r9
bool v7; // zf
_BOOL8 result; // rax

v5 = ip_after_mac(a1, a2, a3, a4, a5, a1);
if ( v5 == 1 )
{
result = 0LL;
if ( !*a2 )
result = a2[2] == (unsigned int)get_ipdaddr(v6);
}
else
{
v7 = v5 == 2;
result = 0LL;
if ( v7 && *a2 == 1 )
result = *((_QWORD *)a2 + 1) == get_ip6daddr(v6);
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1, _DWORD *a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // eax
__int64 v6; // r9
bool v7; // zf
_BOOL8 result; // rax

v5 = func3(a1, a2, a3, a4, a5, a1);
if ( v5 == 1 )
{
result = 0LL;
if ( !*a2 )
result = a2[2] == (unsigned int)func4(v6);
}
else
{
v7 = v5 == 2;
result = 0LL;
if ( v7 && *a2 == 1 )
result = *((_QWORD *)a2 + 1) == func5(v6);
}
return result;
}

original_code:
int verify_daddr( const u_char *packet, const wanted_ip *ipdaddr )
{
      switch (ip_after_mac(packet))
      {
      case 1:
     if (ipdaddr->t_ip != IPV4) break;
     return ( get_ipdaddr(packet) == ipdaddr->w_ip.ipv4 );
      case 2:
     if (ipdaddr->t_ip != IPV6) break;
     return ( get_ip6daddr(packet) == ipdaddr->w_ip.ipv6 );
      default:
     return 0;
      }
      return 0;
}

canonical_original_code:
int verify_daddr(unsigned char *packet, struct wanted_ip *ipdaddr)
{
      switch (ip_after_mac(packet))
      {
      case 1:
     if (ipdaddr->t_ip != IPV4) break;
     return ( get_ipdaddr(packet) == ipdaddr->w_ip.ipv4 );
      case 2:
     if (ipdaddr->t_ip != IPV6) break;
     return ( get_ip6daddr(packet) == ipdaddr->w_ip.ipv6 );
      default:
     return 0;
      }
      return 0;
}

variable_types: struct wanted_ip { enum type_ip { IPV4=0, IPV6=1 } t_ip; union { unsigned char *ipv6; unsigned int ipv4; } w_ip; } *

user_defined_types: struct wanted_ip { enum type_ip t_ip; union { unsigned char *ipv6; unsigned int ipv4; } w_ip; }


decompiled_code:
_BOOL8 __fastcall tcp_after_ip(__int64 a1)
{
return *(_BYTE *)(a1 + 23) == 6;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(__int64 a1)
{
return *(_BYTE *)(a1 + 23) == 6;
}

original_code:
int tcp_after_ip( const u_char *packet )
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return ( header->protocol == 0x06 );
}

canonical_original_code:
int tcp_after_ip(unsigned char *packet)
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return ( header->protocol == 0x06 );
}

user_defined_types: struct iphdr { unsigned int ihl; unsigned int version; unsigned char tos; unsigned short tot_len; unsigned short id; unsigned short frag_off; unsigned char ttl; unsigned char protocol; unsigned short check; unsigned int saddr; unsigned int daddr; }


decompiled_code:
_BOOL8 __fastcall tcp_after_ipv6(__int64 a1)
{
return *(_BYTE *)(a1 + 20) == 6;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(__int64 a1)
{
return *(_BYTE *)(a1 + 20) == 6;
}

original_code:
int tcp_after_ipv6( const u_char *packet )
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return ( header->ip6_ctlun.ip6_un1.ip6_un1_nxt == 0x06 );
}

canonical_original_code:
int tcp_after_ipv6(unsigned char *packet)
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return ( header->ip6_ctlun.ip6_un1.ip6_un1_nxt == 0x06 );
}

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }

user_defined_types: struct ip6_hdrctl { unsigned int ip6_un1_flow; unsigned short ip6_un1_plen; unsigned char ip6_un1_nxt; unsigned char ip6_un1_hlim; }


decompiled_code:
__int64 __fastcall get_ip6_plen(__int64 a1)
{
return *(unsigned __int16 *)(a1 + 18);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return *(unsigned __int16 *)(a1 + 18);
}

original_code:
uint16_t get_ip6_plen( const u_char *packet )
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return header->ip6_ctlun.ip6_un1.ip6_un1_plen;
}

canonical_original_code:
unsigned short get_ip6_plen(unsigned char *packet)
{
      struct ip6_hdr *header = (struct ip6_hdr *)(packet + 14);
      return header->ip6_ctlun.ip6_un1.ip6_un1_plen;
}

return_type: unsigned short


original_code:
uint16_t get_ip_plen( const u_char *packet )
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return (ntohs(header->tot_len) - 4*(header->ihl));
}

canonical_original_code:
unsigned short get_ip_plen(unsigned char *packet)
{
      struct iphdr *header = (struct iphdr *)(packet + 14);
      return (ntohs(header->tot_len) - 4*(header->ihl));
}


decompiled_code:
__int64 __fastcall valid_packet(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // eax
__int64 v6; // r9
unsigned int v7; // er8
unsigned int v9; // er8

v5 = ip_after_mac(a1, a2, a3, a4, a5, a1);
if ( v5 == 1 )
{
if ( tcp_after_ip(v6) )
return v7;
}
else if ( v5 != 2 )
{
return 0;
}
v9 = tcp_after_ipv6(v6);
if ( v9 )
v9 = 2;
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // eax
__int64 v6; // r9
unsigned int v7; // er8
unsigned int v9; // er8

v5 = func3(a1, a2, a3, a4, a5, a1);
if ( v5 == 1 )
{
if ( func7(v6) )
return v7;
}
else if ( v5 != 2 )
{
return 0;
}
v9 = func8(v6);
if ( v9 )
v9 = 2;
return v9;
}

original_code:
int valid_packet( const u_char *packet )
{
      switch (ip_after_mac(packet))
      {
      case 1:
     if (tcp_after_ip(packet)) return 1;
      case 2:
     if (tcp_after_ipv6(packet)) return 2;
      default:
     return 0;
      }
}

canonical_original_code:
int valid_packet(unsigned char *packet)
{
      switch (ip_after_mac(packet))
      {
      case 1:
     if (tcp_after_ip(packet)) return 1;
      case 2:
     if (tcp_after_ipv6(packet)) return 2;
      default:
     return 0;
      }
}


original_code:
struct tcphdr *get_tcphdr( const u_char *packet )
{
      size_t ip_hdr = 0;
      int valid = valid_packet(packet);
      if (!valid)
     return ((void *)0);
      if (valid == 1)
     ip_hdr = 0x14;
      if (valid == 2)
     ip_hdr = 0x28;
      return (struct tcphdr *)(packet + (14 + ip_hdr));
}

canonical_original_code:
struct tcphdr *get_tcphdr(unsigned char *packet)
{
      unsigned long ip_hdr = 0;
      int valid = valid_packet(packet);
      if (!valid)
     return ((void *)0);
      if (valid == 1)
     ip_hdr = 0x14;
      if (valid == 2)
     ip_hdr = 0x28;
      return (struct tcphdr *)(packet + (14 + ip_hdr));
}

variable_types: valid int

return_type: struct tcphdr { union { struct { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2; unsigned char th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; }; struct { unsigned short source; unsigned short dest; unsigned int seq; unsigned int ack_seq; unsigned short res1; unsigned short doff; unsigned short fin; unsigned short syn; unsigned short rst; unsigned short psh; unsigned short ack; unsigned short urg; unsigned short res2; unsigned short window; unsigned short check; unsigned short urg_ptr; }; }; } *


decompiled_code:
__int64 __fastcall fix_ipdaddr(__int64 a1, _DWORD *a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v5; // rdx
__int64 v6; // rcx
__int64 v7; // r9
__int64 v8; // r10
__int64 result; // rax
int v10; // eax
__int64 v11; // r10
unsigned int v12; // er8
unsigned int v13; // er8

if ( (*(_BYTE *)(get_tcphdr(a1, (__int64)a2, a3, a4, a5) + 13) & 0x12) != 18 )
return 0LL;
v10 = ip_after_mac(v8, a2, v5, v6, 0LL, v7);
if ( v10 == 1 )
{
*a2 = 0;
a2[2] = get_ipdaddr(v11);
result = v13;
}
else if ( v10 == 2 )
{
*a2 = 1;
*((_QWORD *)a2 + 1) = get_ip6daddr(v11);
result = v12;
}
else
{
result = 0LL;
}
return result;
}

original_code:
int fix_ipdaddr( const u_char *packet, wanted_ip *ipdaddr )
{
      struct tcphdr *tcphdr = get_tcphdr(packet);
      if ( (tcphdr->th_flags & (0x02 | 0x10)) == (0x02 | 0x10) )
      {
     switch(ip_after_mac(packet))
     {
     case 1:
    ipdaddr->t_ip = IPV4;
    ipdaddr->w_ip.ipv4 = get_ipdaddr(packet);
    return 1;
     case 2:
    ipdaddr->t_ip = IPV6;
    ipdaddr->w_ip.ipv6 = get_ip6daddr(packet);
    return 1;
     default:
    return 0;
     }
      }
      return 0;
}

canonical_original_code:
int fix_ipdaddr(unsigned char *packet, struct wanted_ip *ipdaddr)
{
      struct tcphdr *tcphdr = get_tcphdr(packet);
      if ( (tcphdr->th_flags & (0x02 | 0x10)) == (0x02 | 0x10) )
      {
     switch(ip_after_mac(packet))
     {
     case 1:
    ipdaddr->t_ip = IPV4;
    ipdaddr->w_ip.ipv4 = get_ipdaddr(packet);
    return 1;
     case 2:
    ipdaddr->t_ip = IPV6;
    ipdaddr->w_ip.ipv6 = get_ip6daddr(packet);
    return 1;
     default:
    return 0;
     }
      }
      return 0;
}

variable_types: struct tcphdr { union { struct { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2; unsigned char th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; }; struct { unsigned short source; unsigned short dest; unsigned int seq; unsigned int ack_seq; unsigned short res1; unsigned short doff; unsigned short fin; unsigned short syn; unsigned short rst; unsigned short psh; unsigned short ack; unsigned short urg; unsigned short res2; unsigned short window; unsigned short check; unsigned short urg_ptr; }; }; } *


decompiled_code:
__int64 __fastcall get_sequence_number(__int64 a1)
{
return (unsigned int)_bswap_32(*(_DWORD *)(a1 + 4));
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return (unsigned int)func1(*(_DWORD *)(a1 + 4));
}

original_code:
long get_sequence_number( const struct tcphdr *header )
{
      return htonl(header->th_seq);
}

canonical_original_code:
long get_sequence_number(struct tcphdr *header)
{
      return htonl(header->th_seq);
}

user_defined_types: struct tcphdr { union { struct { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2; unsigned char th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; }; struct { unsigned short source; unsigned short dest; unsigned int seq; unsigned int ack_seq; unsigned short res1; unsigned short doff; unsigned short fin; unsigned short syn; unsigned short rst; unsigned short psh; unsigned short ack; unsigned short urg; unsigned short res2; unsigned short window; unsigned short check; unsigned short urg_ptr; }; }; }


decompiled_code:
__int64 __fastcall get_payload_lgt(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
int v5; // eax
__int64 v6; // r9
unsigned int v7; // er8
__int16 v8; // ax
char v9; // cl
int v10; // er8

v5 = ip_after_mac(a1, a2, a3, *(_BYTE *)(a2 + 12) >> 4, a5, a1);
if ( v5 == 1 )
{
v10 = (unsigned __int16)get_ip_plen(v6);
goto LABEL_4;
}
v7 = -1;
if ( v5 == 2 )
{
v8 = get_ip6_plen(v6);
v10 = (unsigned __int16)_bswap_16(v8);
LABEL_4:
v7 = v10 - (unsigned __int8)(4 * v9);
}
return v7;
}

original_code:
int get_payload_lgt( const u_char *packet , const struct tcphdr *tcph )
{
      u_int8_t offset = tcph->th_off * 4;
      u_int16_t plen;
      switch (ip_after_mac(packet))
      {
      case 1:
     plen = get_ip_plen(packet);
     break;
      case 2:
     plen = htons(get_ip6_plen(packet));
     break;
      default:
     return -1;
      }
      return (plen - offset);
}

canonical_original_code:
int get_payload_lgt(unsigned char *packet, struct tcphdr *tcph)
{
      unsigned int offset = tcph->th_off * 4;
      unsigned int plen;
      switch (ip_after_mac(packet))
      {
      case 1:
     plen = get_ip_plen(packet);
     break;
      case 2:
     plen = htons(get_ip6_plen(packet));
     break;
      default:
     return -1;
      }
      return (plen - offset);
}

variable_types:
offset
unsigned int

variable_types:
plen
unsigned int


decompiled_code:
__int64 __fastcall get_next_sequence_number(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
__int64 v4; // r8
int v5; // eax
__int64 v6; // r10

get_sequence_number(a2);
v5 = get_payload_lgt(v4, a2, v2, v3, v4);
return v6 + v5;
}

original_code:
long get_next_sequence_number( const u_char *packet , const struct tcphdr *tcph )
{
      return get_sequence_number(tcph)+ get_payload_lgt( packet, tcph);
}

canonical_original_code:
long get_next_sequence_number(unsigned char *packet, struct tcphdr *tcph)
{
      return get_sequence_number(tcph)+ get_payload_lgt( packet, tcph);
}


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

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

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

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

original_code:
packet_late *init_late()
{
      packet_late *begin = malloc(sizeof(packet_late));
      begin->p_sequence = 0;
      begin->expected = 0;
      begin->next = ((void *)0);
      return begin;
}

canonical_original_code:
struct packet_late *init_late()
{
      struct packet_late *begin = malloc(sizeof(packet_late));
      begin->p_sequence = 0;
      begin->expected = 0;
      begin->next = ((void *)0);
      return begin;
}

variable_types: struct packet_late { long p_sequence; long expected; struct packet_late *next; } *


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

while ( 1 )
{
v2 = a1;
a1 = (_QWORD *)a1[2];
if ( !a1 )
break;
if ( *a1 == a2 )
{
v2[2] = a1[2];
free(a1);
return 0LL;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func19(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax

while ( 1 )
{
v2 = a1;
a1 = (_QWORD *)a1[2];
if ( !a1 )
break;
if ( *a1 == a2 )
{
v2[2] = a1[2];
free(a1);
return 0LL;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int free_first( packet_late *begin, long seq )
{
      packet_late *tmp;
      for( ;
    begin->next != ((void *)0) && begin->next->p_sequence != seq;
    begin = begin->next);
      if (begin->next == ((void *)0))
     return -1;
      tmp = begin->next;
      begin->next = begin->next->next;
      free(tmp);
      return 0;
}

canonical_original_code:
int free_first(struct packet_late *begin, long seq)
{
      struct packet_late *tmp;
      for( ;
    begin->next != ((void *)0) && begin->next->p_sequence != seq;
    begin = begin->next);
      if (begin->next == ((void *)0))
     return -1;
      tmp = begin->next;
      begin->next = begin->next->next;
      free(tmp);
      return 0;
}

variable_types: seq long

user_defined_types: struct packet_late { long p_sequence; long expected; struct packet_late *next; }


decompiled_code:
__int64 __fastcall free_all_packet_late(_QWORD *a1)
{
_QWORD *v1; // rbp

if ( !a1 )
return 1LL;
v1 = (_QWORD *)a1[2];
if ( v1 )
{
while ( 1 )
{
free(a1);
a1 = v1;
if ( !v1[2] )
break;
v1 = (_QWORD *)v1[2];
}
}
else
{
v1 = a1;
}
free(v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func20(_QWORD *a1)
{
_QWORD *v1; // rbp

if ( !a1 )
return 1LL;
v1 = (_QWORD *)a1[2];
if ( v1 )
{
while ( 1 )
{
free(a1);
a1 = v1;
if ( !v1[2] )
break;
v1 = (_QWORD *)v1[2];
}
}
else
{
v1 = a1;
}
free(v1);
return 0xFFFFFFFFLL;
}

original_code:
int free_all_packet_late( packet_late *begin )
{
      packet_late *tmp;
      if (begin == ((void *)0))
     return 1;
      while ( begin->next != ((void *)0) )
      {
     tmp = begin;
     begin = begin->next;
     free(tmp);
      }
      free(begin);
      if ( begin != ((void *)0) ){
     return -1;
      }
      return 1;
}

canonical_original_code:
int free_all_packet_late(struct packet_late *begin)
{
      struct packet_late *tmp;
      if (begin == ((void *)0))
     return 1;
      while ( begin->next != ((void *)0) )
      {
     tmp = begin;
     begin = begin->next;
     free(tmp);
      }
      free(begin);
      if ( begin != ((void *)0) ){
     return -1;
      }
      return 1;
}


repo: mabalew/Budget

decompiled_code:
bool __fastcall is_add_string(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 97 || (result = 1, *(_BYTE *)(a1 + 1)) )
result = strcmp((const char *)a1, "add") == 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func0(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 97 || (result = 1, *(_BYTE *)(a1 + 1)) )
result = strcmp((const char *)a1, "add") == 0;
return result;
}

original_code:
_Bool is_add_string(char *s) {
 if (strcmp(s, "a") == 0 || strcmp(s, "add") == 0) {
  return 1;
 }
 return 0;
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall is_update_string(__int64 a1)
{
unsigned int v2; // er8

if ( *(_BYTE *)a1 == 117 && !*(_BYTE *)(a1 + 1) )
return 1LL;
v2 = 1;
if ( !strcmp((const char *)a1, "upd") )
return 1LL;
LOBYTE(v2) = strcmp((const char *)a1, "update") == 0;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned int v2; // er8

if ( *(_BYTE *)a1 == 117 && !*(_BYTE *)(a1 + 1) )
return 1LL;
v2 = 1;
if ( !strcmp((const char *)a1, "upd") )
return 1LL;
LOBYTE(v2) = strcmp((const char *)a1, "update") == 0;
return v2;
}

original_code:
_Bool is_update_string(char *s) {
 if (strcmp(s, "u") == 0 || strcmp(s, "upd") == 0 || strcmp(s, "update") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall is_delete_string(__int64 a1)
{
unsigned int v2; // er8

if ( *(_BYTE *)a1 == 100 && !*(_BYTE *)(a1 + 1) )
return 1LL;
v2 = 1;
if ( !strcmp((const char *)a1, "del") )
return 1LL;
LOBYTE(v2) = strcmp((const char *)a1, "delete") == 0;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v2; // er8

if ( *(_BYTE *)a1 == 100 && !*(_BYTE *)(a1 + 1) )
return 1LL;
v2 = 1;
if ( !strcmp((const char *)a1, "del") )
return 1LL;
LOBYTE(v2) = strcmp((const char *)a1, "delete") == 0;
return v2;
}

original_code:
_Bool is_delete_string(char *s) {
 if (strcmp(s, "d") == 0 || strcmp(s, "del") == 0 || strcmp(s, "delete") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_login_string(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 108 || *(_BYTE *)(a1 + 1) != 111 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "login") == 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func3(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 108 || *(_BYTE *)(a1 + 1) != 111 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "login") == 0;
return result;
}

original_code:
_Bool is_login_string(char *s) {
 if (strcmp(s, "lo") == 0 || strcmp(s, "login") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_list_string(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 108 || (result = 1, *(_BYTE *)(a1 + 1)) )
{
if ( *(_BYTE *)a1 != 108 || *(_BYTE *)(a1 + 1) != 115 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "list") == 0;
}
return result;
}

canonical_decompiled_code:
bool __fastcall func4(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 108 || (result = 1, *(_BYTE *)(a1 + 1)) )
{
if ( *(_BYTE *)a1 != 108 || *(_BYTE *)(a1 + 1) != 115 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "list") == 0;
}
return result;
}

original_code:
_Bool is_list_string(char *s) {
 if (strcmp(s, "l") == 0 || strcmp(s, "ls") == 0 || strcmp(s, "list") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_list_products_in_category_string(unsigned __int8 *a1)
{
int v1; // eax
bool result; // al

v1 = *a1 - 108;
if ( *a1 == 108 && (v1 = a1[1] - 99, a1[1] == 99) )
result = a1[2] == 0;
else
result = v1 == 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func5(unsigned __int8 *a1)
{
int v1; // eax
bool result; // al

v1 = *a1 - 108;
if ( *a1 == 108 && (v1 = a1[1] - 99, a1[1] == 99) )
result = a1[2] == 0;
else
result = v1 == 0;
return result;
}

original_code:
_Bool is_list_products_in_category_string(char *s) {
 if (strcmp(s, "lc") == 0) {
  return 1;
 }
 return 0;
}


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

v1 = 1;
if ( strcmp(a1, "gid") )
LOBYTE(v1) = strcmp(a1, "getbyid") == 0;
return v1;
}

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

v1 = 1;
if ( strcmp(a1, "gid") )
LOBYTE(v1) = strcmp(a1, "getbyid") == 0;
return v1;
}

original_code:
_Bool is_get_by_id_string(char *s) {
 if (strcmp(s, "gid") == 0 || strcmp(s, "getbyid") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_get_by_login_string(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 103 || *(_BYTE *)(a1 + 1) != 108 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "getbylogin") == 0;
return result;
}

canonical_decompiled_code:
bool __fastcall func7(__int64 a1)
{
bool result; // al

if ( *(_BYTE *)a1 != 103 || *(_BYTE *)(a1 + 1) != 108 || (result = 1, *(_BYTE *)(a1 + 2)) )
result = strcmp((const char *)a1, "getbylogin") == 0;
return result;
}

original_code:
_Bool is_get_by_login_string(char *s) {
 if (strcmp(s, "gl") == 0 || strcmp(s, "getbylogin") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_add_to_category_string(const char *a1)
{
return strcmp(a1, "atc") == 0;
}

canonical_decompiled_code:
bool __fastcall func8(const char *a1)
{
return strcmp(a1, "atc") == 0;
}

original_code:
_Bool is_add_to_category_string(char *s) {
 if (strcmp(s, "atc") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
bool __fastcall is_help_string(const char *a1)
{
return strcmp(a1, "help") == 0;
}

canonical_decompiled_code:
bool __fastcall func9(const char *a1)
{
return strcmp(a1, "help") == 0;
}

original_code:
_Bool is_help_string(char *s) {
 if (strcmp(s, "help") == 0) {
  return 1;
 }
 return 0;
}


decompiled_code:
// attributes: thunk
bool __fastcall is_help_for_user_module(const char *a1)
{
return is_help_string(a1);
}

canonical_decompiled_code:
bool __fastcall func10(const char *a1)
{
return func9(a1);
}

original_code:
_Bool is_help_for_user_module(char *s) {
 return is_help_string(s);
}


decompiled_code:
// attributes: thunk
bool __fastcall is_help_for_category_module(const char *a1)
{
return is_help_for_user_module(a1);
}

canonical_decompiled_code:
bool __fastcall func11(const char *a1)
{
return func10(a1);
}

original_code:
_Bool is_help_for_category_module(char *s) {
 return is_help_string(s);
}


decompiled_code:
// attributes: thunk
bool __fastcall is_help_for_expense_module(const char *a1)
{
return is_help_for_user_module(a1);
}

canonical_decompiled_code:
bool __fastcall func12(const char *a1)
{
return func10(a1);
}

original_code:
_Bool is_help_for_expense_module(char *s) {
 return is_help_string(s);
}


decompiled_code:
// attributes: thunk
bool __fastcall is_help_for_shop_module(const char *a1)
{
return is_help_for_user_module(a1);
}

canonical_decompiled_code:
bool __fastcall func13(const char *a1)
{
return func10(a1);
}

original_code:
_Bool is_help_for_shop_module(char *s) {
 return is_help_string(s);
}


decompiled_code:
__int64 __fastcall _add_category(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(8uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = add_category(v2);
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(8uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = func2(v2);
free(v2);
return v3;
}

original_code:
int _add_category(char *name) {
 char *args[4] = {"","","",name};
 return __add_category(4, args);
}

variable_types:
name
char *

variable_types:
args
char *[4]


decompiled_code:
__int64 __fastcall _del_category(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(8uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = del_category(v2);
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(8uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = func4(v2);
free(v2);
return v3;
}

original_code:
int _del_category(char *name) {
 char *args[4] = {"","","",name};
 return __del_category(4, args);
}


original_code:
void free_category_list(Category *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

canonical_original_code:
void free_category_list(struct category *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

variable_types: struct category { int id; char *name; } *[]

variable_types: size int

variable_types: counter int


decompiled_code:
__int64 __fastcall manage_category(unsigned int a1, __int64 a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return _update_category(a1, a2);
if ( (unsigned __int8)is_add_string(*(_QWORD *)(a2 + 16)) )
return _add_category(a1, a2);
if ( (unsigned __int8)is_delete_string(*(_QWORD *)(a2 + 16)) )
return _del_category(a1, a2);
if ( (unsigned __int8)is_list_string(*(_QWORD *)(a2 + 16)) )
return _list_categories();
if ( (unsigned __int8)is_help_for_category_module(*(_QWORD *)(a2 + 16)) )
_show_help_for_category_module();
else
_show_common_help();
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, __int64 a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return _update_category(a1, a2);
if ( (unsigned __int8)is_add_string(*(_QWORD *)(a2 + 16)) )
return func1(a1, a2);
if ( (unsigned __int8)is_delete_string(*(_QWORD *)(a2 + 16)) )
return func3(a1, a2);
if ( (unsigned __int8)is_list_string(*(_QWORD *)(a2 + 16)) )
return func6();
if ( (unsigned __int8)is_help_for_category_module(*(_QWORD *)(a2 + 16)) )
_show_help_for_category_module();
else
_show_common_help();
return 0xFFFFFFFFLL;
}

original_code:
int manage_category(int argc, char *argv[]) {
 if (argc == 2) {
  __show_common_help();
 }
 if (is_update_string(argv[2])) {
  return __update_category(argc, argv);
 } else if (is_add_string(argv[2])) {
  return __add_category(argc, argv);
 } else if (is_delete_string(argv[2])) {
  return __del_category(argc, argv);
 } else if (is_list_string(argv[2])) {
  return __list_categories();
 } else if (is_help_for_category_module(argv[2])) {
  __show_help_for_category_module();
 } else {
  __show_common_help();
 }
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]


original_code:
void free_expense_list(Expense *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->exp_date);
  free(list[counter]->product);
  free(list[counter]->category);
  free(list[counter]->shop);
  free(list[counter]);
 }
}

canonical_original_code:
void free_expense_list(struct expense *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->exp_date);
  free(list[counter]->product);
  free(list[counter]->category);
  free(list[counter]->shop);
  free(list[counter]);
 }
}

variable_types: struct expense { int id; char *exp_date; int category_id; char *category; int product_id; char *product; int shop_id; char *shop; float unit_price; int count; float amount; float price; } *[]


decompiled_code:
int __fastcall print_expense(const char **a1)
{
printf("\tdata: %s sklep: %s\n", a1[1], a1[7]);
printf(aZakupNr6dNaKwo, *(unsigned int *)a1, *((float *)a1 + 19), *((float *)a1 + 18), *((float *)a1 + 16));
printf("\tprodukt: %s z kategorii '%s' [%d]\n", a1[5], a1[3], *((unsigned int *)a1 + 4));
return puts("----------------------------------");
}

canonical_decompiled_code:
int __fastcall func4(const char **a1)
{
printf("\tdata: %s sklep: %s\n", a1[1], a1[7]);
printf(aZakupNr6dNaKwo, *(unsigned int *)a1, *((float *)a1 + 19), *((float *)a1 + 18), *((float *)a1 + 16));
printf("\tprodukt: %s z kategorii '%s' [%d]\n", a1[5], a1[3], *((unsigned int *)a1 + 4));
return puts("----------------------------------");
}

original_code:
void print_expense(Expense *e) {
 printf("\tdata: %s sklep: %s\n", e->exp_date, e->shop);
 printf("\tzakup nr. %.6d na kwotę %.2f / ilość  %.2f / cena jedn. %.2f\n", e->id, e->price, e->amount, e->unit_price);
 printf("\tprodukt: %s z kategorii '%s' [%d]\n", e->product, e->category, e->category_id);
 puts("----------------------------------");
}

canonical_original_code:
void print_expense(struct expense *e) {
 printf("\tdata: %s sklep: %s\n", e->exp_date, e->shop);
 printf("\tzakup nr. %.6d na kwotę %.2f / ilość  %.2f / cena jedn. %.2f\n", e->id, e->price, e->amount, e->unit_price);
 printf("\tprodukt: %s z kategorii '%s' [%d]\n", e->product, e->category, e->category_id);
 puts("----------------------------------");
}

variable_types: struct expense { int id; char *exp_date; int category_id; char *category; int product_id; char *product; int shop_id; char *shop; float unit_price; int count; float amount; float price; } *


decompiled_code:
__int64 __fastcall manage_expense(unsigned int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
return _update_expense(a1, (__int64)a2);
if ( (unsigned __int8)is_add_string(a2[2]) )
return _add_expense(a1, a2);
if ( (unsigned __int8)is_delete_string(a2[2]) )
return _del_expense(a1, (__int64)a2);
if ( (unsigned __int8)is_list_string(a2[2]) )
return _list_expenses(a2[3], a2[4]);
if ( (unsigned __int8)is_help_for_expense_module(a2[2]) )
_show_help_for_expense_module();
else
_show_common_help();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
return func0(a1, (__int64)a2);
if ( (unsigned __int8)is_add_string(a2[2]) )
return func1(a1, a2);
if ( (unsigned __int8)is_delete_string(a2[2]) )
return func2(a1, (__int64)a2);
if ( (unsigned __int8)is_list_string(a2[2]) )
return func5(a2[3], a2[4]);
if ( (unsigned __int8)is_help_for_expense_module(a2[2]) )
_show_help_for_expense_module();
else
_show_common_help();
return 0LL;
}

original_code:
int manage_expense(int argc, char *argv[]) {
 if (argc == 2) {
  __show_common_help();
 }
 if (is_update_string(argv[2])) {
  return __update_expense(argc, argv);
 } else if (is_add_string(argv[2])) {
  return __add_expense(argc, argv);
 } else if (is_delete_string(argv[2])) {
  return __del_expense(argc, argv);
 } else if (is_list_string(argv[2])) {
  return __list_expenses(argv[3], argv[4]);
 } else if (is_help_for_expense_module(argv[2])) {
  __show_help_for_expense_module();
 } else {
  __show_common_help();
 }
 return 0;
}


decompiled_code:
__int64 __fastcall _update_shop(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12
_QWORD *v3; // rbp
unsigned int v4; // er13

verify_number_of_parameters(a1, 5LL);
v2 = malloc(0x10uLL);
v3 = malloc(0x10uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3[1] = *(_QWORD *)(a2 + 32);
v4 = update_shop(v2, v3);
free(v2);
free(v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12
_QWORD *v3; // rbp
unsigned int v4; // er13

verify_number_of_parameters(a1, 5LL);
v2 = malloc(0x10uLL);
v3 = malloc(0x10uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3[1] = *(_QWORD *)(a2 + 32);
v4 = func2(v2, v3);
free(v2);
free(v3);
return v4;
}

original_code:
int _update_shop(char *old_name, char *new_name) {
 char *args[5] = {"","","",old_name,new_name};
 return __update_shop(5, args);
}

variable_types:
args
char *[5]


decompiled_code:
__int64 __fastcall _add_shop(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(8uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = add_shop(v2);
free(v2);
return v3;
}

original_code:
int _add_shop(char *name) {
 char *args[4] = {"","","",name};
 return __add_shop(4, args);
}


decompiled_code:
__int64 __fastcall _del_shop(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(0x10uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = del_shop(v2);
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(0x10uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = func6(v2);
free(v2);
return v3;
}

original_code:
int _del_shop(char *name) {
 char *args[4] = {"","","",name};
 return __del_shop(4, args);
}


original_code:
void free_shop_list(Shop *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

canonical_original_code:
void free_shop_list(struct shop *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

variable_types:
list
struct shop { int id; char *name; } *[]


decompiled_code:
__int64 __fastcall manage_shop(unsigned int a1, __int64 a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return _update_shop(a1, a2);
if ( (unsigned __int8)is_add_string(*(_QWORD *)(a2 + 16)) )
return _add_shop(a1, a2);
if ( (unsigned __int8)is_delete_string(*(_QWORD *)(a2 + 16)) )
return _del_shop(a1, a2);
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return _update_shop(a1, a2);
if ( (unsigned __int8)is_list_string(*(_QWORD *)(a2 + 16)) )
return _list_shops();
if ( (unsigned __int8)is_help_for_shop_module(*(_QWORD *)(a2 + 16)) )
_show_help_for_shop_module();
else
_show_common_help();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, __int64 a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return func1(a1, a2);
if ( (unsigned __int8)is_add_string(*(_QWORD *)(a2 + 16)) )
return func3(a1, a2);
if ( (unsigned __int8)is_delete_string(*(_QWORD *)(a2 + 16)) )
return func5(a1, a2);
if ( (unsigned __int8)is_update_string(*(_QWORD *)(a2 + 16)) )
return func1(a1, a2);
if ( (unsigned __int8)is_list_string(*(_QWORD *)(a2 + 16)) )
return func8();
if ( (unsigned __int8)is_help_for_shop_module(*(_QWORD *)(a2 + 16)) )
func0();
else
_show_common_help();
return 0LL;
}

original_code:
int manage_shop(int argc, char *argv[]) {
 if (argc == 2) {
  __show_common_help();
 }
 if (is_update_string(argv[2])) {
  return __update_shop(argc, argv);
 } else if (is_add_string(argv[2])) {
  return __add_shop(argc, argv);
 } else if (is_delete_string(argv[2])) {
  return __del_shop(argc, argv);
 } else if (is_update_string(argv[2])) {
  return __update_shop(argc, argv);
 } else if (is_list_string(argv[2])) {
  return __list_shops();
 } else if (is_help_for_shop_module(argv[2])) {
  __show_help_for_shop_module();
 } else {
  __show_common_help();
 }
 return 0;
}


decompiled_code:
void __fastcall free_rows_list(void ***a1, int a2)
{
void ***v2; // rbx
void **v3; // rax

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = *v2++;
free(*v3);
free(*(v2 - 1));
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

canonical_decompiled_code:
void __fastcall func0(void ***a1, int a2)
{
void ***v2; // rbx
void **v3; // rax

if ( a2 > 0 )
{
v2 = a1;
do
{
v3 = *v2++;
free(*v3);
free(*(v2 - 1));
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

original_code:
void free_rows_list(Row **rows, int count) {
 int i = 0;
 for (i = 0; i < count; i++) {
  free(rows[i]->category);
  free(rows[i]);
 }
}

canonical_original_code:
void free_rows_list(struct report_row **rows, int count) {
 int i = 0;
 for (i = 0; i < count; i++) {
  free(rows[i]->category);
  free(rows[i]);
 }
}

variable_types: struct report_row { char *category; double value; } **

variable_types: count int

variable_types: i int


original_code:
void free_expense_list2(Expense **e, int count) {
 int i = 0;
 for (i = 0; i < count; i++) {
  free(e[i]->exp_date);
  free(e[i]->category);
  free(e[i]->product);
  free(e[i]->shop);
  free(e[i]);
 }
}

canonical_original_code:
void free_expense_list2(struct expense **e, int count) {
 int i = 0;
 for (i = 0; i < count; i++) {
  free(e[i]->exp_date);
  free(e[i]->category);
  free(e[i]->product);
  free(e[i]->shop);
  free(e[i]);
 }
}

variable_types: struct expense { int id; char *exp_date; int category_id; char *category; int product_id; char *product; int shop_id; char *shop; float unit_price; int count; float amount; float price; } **


variable_types: total double

user_defined_types: struct report_row { char *category; double value; }


decompiled_code:
bool __fastcall is_two_digit_number(int a1)
{
bool result; // al

result = 1;
if ( (unsigned int)(a1 - 10) > 0x59 )
result = (unsigned int)(a1 + 99) <= 0x59;
return result;
}

canonical_decompiled_code:
bool __fastcall func3(int a1)
{
bool result; // al

result = 1;
if ( (unsigned int)(a1 - 10) > 0x59 )
result = (unsigned int)(a1 + 99) <= 0x59;
return result;
}

original_code:
_Bool is_two_digit_number(int number) {
 return ((number >= 10 && number < 100) || (number <= -10 && number > -100));
}

variable_types: number int


decompiled_code:
__int64 __fastcall fetch_monthly_report(const char *a1, const char *a2, __int64 a3)
{
if ( (unsigned int)(atoi(a2) - 1) > 0xB )
{
printf(aErrorNiepopraw);
exit(0);
}
return get_monthly_expenses(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, const char *a2, __int64 a3)
{
if ( (unsigned int)(atoi(a2) - 1) > 0xB )
{
printf(aErrorNiepopraw);
exit(0);
}
return func5(a1, a2, a3);
}

original_code:
void fetch_monthly_report(char *year, char *month, Row **rows) {
 if (atoi(month) < 1 || atoi(month) > 12) {
  printf("ERROR: niepoprawny miesiąc. Miesiąc musi zawierać się w przedziale od 1 do 12.");
  exit(0);
 }
 get_monthly_expenses(year, month, rows);
}

canonical_original_code:
void fetch_monthly_report(char *year, char *month, struct report_row **rows) {
 if (atoi(month) < 1 || atoi(month) > 12) {
  printf("ERROR: niepoprawny miesiąc. Miesiąc musi zawierać się w przedziale od 1 do 12.");
  exit(0);
 }
 get_monthly_expenses(year, month, rows);
}

variable_types:
year
char *

variable_types:
month
char *


decompiled_code:
void __fastcall get_exp_from(const char **a1, __int64 a2)
{
const char *v2; // r14
size_t v3; // rax
const char *v4; // r13
size_t v5; // rbp
size_t v6; // rax
const char *v7; // rbx
size_t v8; // r12
size_t v9; // rax
char *v10; // rbp
int v11; // eax
char *v12; // r12
int v13; // eax
char *v14; // r12

v2 = *a1;
v3 = strlen(*a1);
v4 = a1[1];
v5 = v3;
v6 = strlen(v4);
v7 = a1[2];
v8 = v6;
v9 = strlen(v7);
v10 = (char *)malloc(v9 + v5 + v8 + 3);
if ( v8 != 1 || (v11 = atoi(v4), is_two_digit_number(v11)) )
{
if ( strlen(v7) != 1 )
goto LABEL_3;
}
else
{
v12 = (char *)malloc(1uLL);
sprintf(v12, "%s%s", "0", v4);
v4 = v12;
free(v12);
if ( strlen(v7) != 1 )
goto LABEL_3;
}
v13 = atoi(v7);
if ( !is_two_digit_number(v13) )
{
v14 = (char *)malloc(1uLL);
sprintf(v14, "%s%s", "0", v7);
v7 = v14;
free(v14);
}
LABEL_3:
sprintf(v10, "%s-%s-%s", v2, v4, v7);
get_expenses(v2, v4, a2);
free(v10);
}

canonical_decompiled_code:
void __fastcall func11(const char **a1, __int64 a2)
{
const char *v2; // r14
size_t v3; // rax
const char *v4; // r13
size_t v5; // rbp
size_t v6; // rax
const char *v7; // rbx
size_t v8; // r12
size_t v9; // rax
char *v10; // rbp
int v11; // eax
char *v12; // r12
int v13; // eax
char *v14; // r12

v2 = *a1;
v3 = strlen(*a1);
v4 = a1[1];
v5 = v3;
v6 = strlen(v4);
v7 = a1[2];
v8 = v6;
v9 = strlen(v7);
v10 = (char *)malloc(v9 + v5 + v8 + 3);
if ( v8 != 1 || (v11 = atoi(v4), func3(v11)) )
{
if ( strlen(v7) != 1 )
goto LABEL_3;
}
else
{
v12 = (char *)malloc(1uLL);
sprintf(v12, "%s%s", "0", v4);
v4 = v12;
free(v12);
if ( strlen(v7) != 1 )
goto LABEL_3;
}
v13 = atoi(v7);
if ( !func3(v13) )
{
v14 = (char *)malloc(1uLL);
sprintf(v14, "%s%s", "0", v7);
v7 = v14;
free(v14);
}
LABEL_3:
sprintf(v10, "%s-%s-%s", v2, v4, v7);
get_expenses(v2, v4, a2);
free(v10);
}

original_code:
void get_exp_from(char *argv[], Expense **list) {
 char *date = malloc(sizeof(char) * (strlen(argv[0]) + strlen(argv[1]) + strlen(argv[2])) + 3);
 char *year = argv[0];
 char *month= argv[1];
 char *day = argv[2];
 if (strlen(month) == 1 && !is_two_digit_number(atoi(month))) {
  char *tmp = malloc(sizeof(char));
  sprintf(tmp, "%s%s", "0", month);
  month = tmp;
  free(tmp);
 }
 if (strlen(day) == 1 && !is_two_digit_number(atoi(day))) {
  char *tmp = malloc(sizeof(char));
  sprintf(tmp, "%s%s", "0", day);
  day = tmp;
  free(tmp);
 }
 sprintf(date, "%s-%s-%s", year, month, day);
 Expense *e = malloc(sizeof(Expense));
 e->exp_date = date;
 get_expenses(year, month, list);
 free(date);
 free(e);
}

canonical_original_code:
void get_exp_from(char *argv[], struct expense **list) {
 char *date = malloc(sizeof(char) * (strlen(argv[0]) + strlen(argv[1]) + strlen(argv[2])) + 3);
 char *year = argv[0];
 char *month = argv[1];
 char *day = argv[2];
 if (strlen(month) == 1 && !is_two_digit_number(atoi(month))) {
  char *tmp = malloc(sizeof(char));
  sprintf(tmp, "%s%s", "0", month);
  month = tmp;
  free(tmp);
 }
 if (strlen(day) == 1 && !is_two_digit_number(atoi(day))) {
  char *tmp = malloc(sizeof(char));
  sprintf(tmp, "%s%s", "0", day);
  day = tmp;
  free(tmp);
 }
 sprintf(date, "%s-%s-%s", year, month, day);
 struct expense *e = malloc(sizeof(Expense));
 e->exp_date = date;
 get_expenses(year, month, list);
 free(date);
 free(e);
}

variable_types:
date
char *

variable_types:
day
char *

variable_types:
tmp
char *

user_defined_types: struct expense { int id; char *exp_date; int category_id; char *category; int product_id; char *product; int shop_id; char *shop; float unit_price; int count; float amount; float price; }


decompiled_code:
__int64 __fastcall _update_product(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // r12
_QWORD *v3; // rax
const char *v4; // r13
const char *v5; // rdi
_QWORD *v6; // rbp
__int64 v7; // rax

verify_number_of_parameters(a1, 6LL);
v2 = malloc(0x18uLL);
v3 = malloc(0x18uLL);
v4 = (const char *)a2[3];
v5 = (const char *)a2[5];
v6 = v3;
v7 = a2[4];
v2[1] = v4;
v6[1] = v7;
*((_DWORD *)v6 + 4) = atoi(v5);
printf("po->name: %s\n", v4);
printf("pn->name: %s\n", (const char *)v6[1]);
printf("pn->category_id: %d\n", *((unsigned int *)v6 + 4));
LODWORD(v4) = update_product(v2, v6);
free(v2);
free(v6);
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2)
{
_QWORD *v2; // r12
_QWORD *v3; // rax
const char *v4; // r13
const char *v5; // rdi
_QWORD *v6; // rbp
__int64 v7; // rax

verify_number_of_parameters(a1, 6LL);
v2 = malloc(0x18uLL);
v3 = malloc(0x18uLL);
v4 = (const char *)a2[3];
v5 = (const char *)a2[5];
v6 = v3;
v7 = a2[4];
v2[1] = v4;
v6[1] = v7;
*((_DWORD *)v6 + 4) = atoi(v5);
printf("po->name: %s\n", v4);
printf("pn->name: %s\n", (const char *)v6[1]);
printf("pn->category_id: %d\n", *((unsigned int *)v6 + 4));
LODWORD(v4) = func1(v2, v6);
free(v2);
free(v6);
return (unsigned int)v4;
}

original_code:
int _update_product(char *old_name, char *new_name, int category_id) {
 char *cat_id = malloc(20);
 sprintf(cat_id, "%d", category_id);
 char *args[6] = {"","","",old_name, new_name, cat_id};
 int ret = __update_product(6, args);
 free(cat_id);
 return ret;
}

variable_types:
args
char *[6]

variable_types: ret int


decompiled_code:
__int64 __fastcall _add_product(__int64 a1, __int64 a2)
{
void *v2; // rax
const char *v3; // rdi
void *v4; // rbp
unsigned int v5; // er12

verify_number_of_parameters(a1, 5LL);
v2 = malloc(0x18uLL);
v3 = *(const char **)(a2 + 32);
v4 = v2;
*((_QWORD *)v2 + 1) = *(_QWORD *)(a2 + 24);
*((_DWORD *)v2 + 4) = atoi(v3);
v5 = add_product(v4);
free(v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
void *v2; // rax
const char *v3; // rdi
void *v4; // rbp
unsigned int v5; // er12

verify_number_of_parameters(a1, 5LL);
v2 = malloc(0x18uLL);
v3 = *(const char **)(a2 + 32);
v4 = v2;
*((_QWORD *)v2 + 1) = *(_QWORD *)(a2 + 24);
*((_DWORD *)v2 + 4) = atoi(v3);
v5 = func3(v4);
free(v4);
return v5;
}

original_code:
int _add_product(char *name, int category_id) {
 char *cat_id = malloc(20);
 sprintf(cat_id, "%d", category_id);
 char *args[5] = {"","","",name,cat_id};
 return __add_product(5, args);
}

variable_types:
name
char *


decompiled_code:
__int64 __fastcall _del_product(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(0x18uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = del_product(v2);
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
unsigned int v3; // er12

verify_number_of_parameters(a1, 4LL);
v2 = malloc(0x18uLL);
v2[1] = *(_QWORD *)(a2 + 24);
v3 = func5(v2);
free(v2);
return v3;
}

original_code:
int _del_product(char *name) {
 char *args[4] = {"","","",name};
 return __del_product(4, args);
}

variable_types:
args
char *[4]


original_code:
void free_product_list(Product *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

canonical_original_code:
void free_product_list(struct product *list[], int size) {
 int counter = 0;
 for (counter = 0; counter < size; counter++) {
  free(list[counter]->name);
  free(list[counter]);
 }
}

variable_types: struct product { int id; char *name; int category_id; } *[]

variable_types: size int

variable_types: counter int


decompiled_code:
__int64 __fastcall manage_product(unsigned int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
return _update_product(a1, a2);
if ( (unsigned __int8)is_add_string(a2[2]) )
return _add_product(a1, (__int64)a2);
if ( (unsigned __int8)is_delete_string(a2[2]) )
return _del_product(a1, (__int64)a2);
if ( (unsigned __int8)is_list_string(a2[2]) )
return _list_products();
if ( (unsigned __int8)is_help_string(a2[2]) )
{
_show_help_for_product_module();
return 0LL;
}
if ( (unsigned __int8)is_add_to_category_string(a2[2]) )
return _add_to_category(a1, (__int64)a2);
if ( !(unsigned __int8)is_list_products_in_category_string(a2[2]) )
{
_show_common_help();
return 0LL;
}
return _list_products_in_category(a1, (__int64)a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
return func0(a1, a2);
if ( (unsigned __int8)is_add_string(a2[2]) )
return func2(a1, (__int64)a2);
if ( (unsigned __int8)is_delete_string(a2[2]) )
return func4(a1, (__int64)a2);
if ( (unsigned __int8)is_list_string(a2[2]) )
return func8();
if ( (unsigned __int8)is_help_string(a2[2]) )
{
_show_help_for_product_module();
return 0LL;
}
if ( (unsigned __int8)is_add_to_category_string(a2[2]) )
return func6(a1, (__int64)a2);
if ( !(unsigned __int8)is_list_products_in_category_string(a2[2]) )
{
_show_common_help();
return 0LL;
}
return func9(a1, (__int64)a2);
}

original_code:
int manage_product(int argc, char *argv[]) {
 if (argc == 2) {
  __show_common_help();
 }
 if (is_update_string(argv[2])) {
  return __update_product(argc, argv);
 } else if (is_add_string(argv[2])) {
  return __add_product(argc, argv);
 } else if (is_delete_string(argv[2])) {
  return __del_product(argc, argv);
 } else if (is_list_string(argv[2])) {
  return __list_products();
 } else if (is_help_string(argv[2])) {
  __show_help_for_product_module();
 } else if (is_add_to_category_string(argv[2])) {
  return __add_to_category(argc, argv);
 } else if (is_list_products_in_category_string(argv[2])) {
  return __list_products_in_category(argc, argv);
 } else {
  __show_common_help();
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void __fastcall check_db_open(int a1)
{
if ( a1 )
{
puts("Can't open database");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 )
{
puts("Can't open database");
exit(0);
}
}

original_code:
void check_db_open(int error) {
 if (error) {
  puts("Can't open database");
  exit(0);
 }
}

variable_types: error int


login

decompiled_code:
bool __fastcall login(__int64 a1)
{
void *v1; // rbx

v1 = malloc(8uLL);
*((_QWORD *)v1 + 1) = *(_QWORD *)(a1 + 8);
get_user_by_login((__int64)v1);
return strcmp(*(const char **)(a1 + 16), *((const char **)v1 + 2)) == 0;
}

canonical_decompiled_code:
bool __fastcall func7(__int64 a1)
{
void *v1; // rbx

v1 = malloc(8uLL);
*((_QWORD *)v1 + 1) = *(_QWORD *)(a1 + 8);
func6((__int64)v1);
return strcmp(*(const char **)(a1 + 16), *((const char **)v1 + 2)) == 0;
}

original_code:
_Bool login(User *p) {
 User *user = (User *)malloc(sizeof user);
 user->nick = p->nick;
 get_user_by_login(user);
 if (strcmp(p->password, user->password) == 0) {
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
_Bool login(struct user *p) {
 struct user *user = (User *)malloc(sizeof user);
 user->nick = p->nick;
 get_user_by_login(user);
 if (strcmp(p->password, user->password) == 0) {
  return 1;
 } else {
  return 0;
 }
}

variable_types: struct user { int id; char *nick; char *password; } *


decompiled_code:
void __fastcall verify_number_of_parameters(int a1, int a2)
{
if ( a1 < a2 )
{
puts("Niepoprawna liczba parametrow");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
if ( a1 < a2 )
{
puts("Niepoprawna liczba parametrow");
exit(0);
}
}

original_code:
void verify_number_of_parameters(int argc, int should_be) {
 if (argc < should_be) {
  printf("Niepoprawna liczba parametrow\n");
  exit(0);
 }
}


decompiled_code:
char *__fastcall replace_str(char *src, char *s, const char *a3)
{
char *v4; // rbp
char *result; // rax
size_t v6; // rax

v4 = strstr(src, s);
result = src;
if ( v4 )
{
strncpy(buffer_0, src, v4 - src);
buffer_0[v4 - src] = 0;
v6 = strlen(s);
sprintf(&buffer_0[v4 - src], "%s%s", a3, &v4[v6]);
result = buffer_0;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *src, char *s, const char *a3)
{
char *v4; // rbp
char *result; // rax
size_t v6; // rax

v4 = strstr(src, s);
result = src;
if ( v4 )
{
strncpy(buffer_0, src, v4 - src);
buffer_0[v4 - src] = 0;
v6 = strlen(s);
sprintf(&buffer_0[v4 - src], "%s%s", a3, &v4[v6]);
result = buffer_0;
}
return result;
}

original_code:
char *replace_str(char *str, char *orig, char *rep) {
 static char buffer[4096];
 char *p;
 if(!(p = strstr(str, orig)))
  return str;
 strncpy(buffer, str, p-str);
 buffer[p-str] = '\0';
 sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
 return buffer;
}

variable_types:
str
char *

variable_types:
orig
char *

variable_types:
rep
char *

variable_types:
buffer
char [4096]

variable_types:
p
char *


decompiled_code:
void __fastcall manage_user(int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
{
_update_user(a2);
}
else if ( (unsigned __int8)is_add_string(a2[2]) )
{
_add_user((__int64)a2);
}
else if ( (unsigned __int8)is_delete_string(a2[2]) )
{
_del_user((__int64)a2);
}
else if ( (unsigned __int8)is_login_string(a2[2]) )
{
_login(a2);
}
else if ( (unsigned __int8)is_list_string(a2[2]) )
{
_list_users();
}
else if ( (unsigned __int8)is_get_by_id_string(a2[2]) )
{
_get_user_by_id((__int64)a2);
}
else if ( (unsigned __int8)is_get_by_login_string(a2[2]) )
{
_get_user_by_login((__int64)a2);
}
else if ( (unsigned __int8)is_help_for_user_module(a2[2]) )
{
_show_help_for_user_module();
}
else
{
_show_common_help();
}
}

canonical_decompiled_code:
void __fastcall func7(int a1, _QWORD *a2)
{
if ( a1 == 2 )
_show_common_help();
if ( (unsigned __int8)is_update_string(a2[2]) )
{
func1(a2);
}
else if ( (unsigned __int8)is_add_string(a2[2]) )
{
func0((__int64)a2);
}
else if ( (unsigned __int8)is_delete_string(a2[2]) )
{
func2((__int64)a2);
}
else if ( (unsigned __int8)is_login_string(a2[2]) )
{
func3(a2);
}
else if ( (unsigned __int8)is_list_string(a2[2]) )
{
func4();
}
else if ( (unsigned __int8)is_get_by_id_string(a2[2]) )
{
func5((__int64)a2);
}
else if ( (unsigned __int8)is_get_by_login_string(a2[2]) )
{
func6((__int64)a2);
}
else if ( (unsigned __int8)is_help_for_user_module(a2[2]) )
{
_show_help_for_user_module();
}
else
{
_show_common_help();
}
}

original_code:
void manage_user(int argc, char *argv[]) {
 if (argc == 2) {
  __show_common_help();
 }
 if (is_update_string(argv[2])) {
  __update_user(argv);
 } else if (is_add_string(argv[2])) {
  __add_user(argv);
 } else if (is_delete_string(argv[2])) {
  __del_user(argv);
 } else if (is_login_string(argv[2])) {
  __login(argv);
 } else if (is_list_string(argv[2])) {
  __list_users();
 } else if (is_get_by_id_string(argv[2])) {
  __get_user_by_id(argv);
 } else if (is_get_by_login_string(argv[2])) {
  __get_user_by_login(argv);
 } else if (is_help_for_user_module(argv[2])) {
  __show_help_for_user_module();
 } else {
  __show_common_help();
 }
}


help

decompiled_code:
char __fastcall help(const char *a1)
{
bool v1; // cf
bool v2; // zf
__int64 v4; // rcx
const char *v5; // rdi
char result; // al

v4 = 5LL;
v5 = "main";
do
{
if ( !v4 )
break;
v1 = *a1 < (unsigned int)*v5;
v2 = *a1++ == *v5++;
--v4;
}
while ( v2 );
result = (!v1 && !v2) - v1;
if ( !result )
_show_common_help(v5, a1);
return result;
}

canonical_decompiled_code:
char __fastcall func5(const char *a1)
{
bool v1; // cf
bool v2; // zf
__int64 v4; // rcx
const char *v5; // rdi
char result; // al

v4 = 5LL;
v5 = "main";
do
{
if ( !v4 )
break;
v1 = *a1 < (unsigned int)*v5;
v2 = *a1++ == *v5++;
--v4;
}
while ( v2 );
result = (!v1 && !v2) - v1;
if ( !result )
func0(v5, a1);
return result;
}

original_code:
void help(char *h) {
 if (strcmp(h, "main") == 0) {
  __show_common_help();
 }
}

variable_types:
h
char *


main

canonical_name: main

repo: macdonad/MultiThreadedServer

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // r8
_QWORD *v4; // rbp
const char *v5; // rsi

signal(2, handle_sigint);
input_buffer = (char *)calloc(0x400uLL, 8uLL);
while ( 1 )
{
while ( 1 )
{
read_filename();
if ( !(unsigned __int8)str_is_whitespace(input_buffer) )
break;
puts("No file given...");
}
v4 = (_QWORD *)file_worker_new(v3);
if ( (unsigned int)file_worker_start(v4) )
{
v5 = "unknown";
if ( v4 )
{
v5 = (const char *)v4[2];
if ( !v5 )
v5 = "unknown";
}
printf("Error starting worker for \"%s\"\n", v5);
file_worker_free(v4);
}
else
{
last_worker = v4;
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // r8
_QWORD *v4; // rbp
const char *v5; // rsi

signal(2, handle_sigint);
input_buffer = (char *)calloc(0x400uLL, 8uLL);
while ( 1 )
{
while ( 1 )
{
func11();
if ( !(unsigned __int8)func12(input_buffer) )
break;
puts("No file given...");
}
v4 = (_QWORD *)file_worker_new(v3);
if ( (unsigned int)func9(v4) )
{
v5 = "unknown";
if ( v4 )
{
v5 = (const char *)v4[2];
if ( !v5 )
v5 = "unknown";
}
printf("Error starting worker for \"%s\"\n", v5);
func8(v4);
}
else
{
last_worker = v4;
}
}
}

original_code:
int
main(void)
{
  signal(2, handle_sigint);
  input_buffer = calloc(1024, sizeof(char*));
  while(1)
    {
      read_filename();
      if (!str_is_whitespace(input_buffer))
 {
   file_worker_t* worker = file_worker_new(input_buffer, last_worker);
   if(file_worker_start(worker) != 0)
     {
       printf("Error starting worker for \"%s\"\n",
       (worker && worker->filename) ? worker->filename : "unknown");
       file_worker_free(worker);
     }
   else
     {
       last_worker = worker;
     }
 }
      else
 {
   printf("No file given...\n");
 }
    }
  pthread_exit(((void *)0));
}

canonical_original_code:
int main()
{
  signal(2, handle_sigint);
  input_buffer = calloc(1024, sizeof(char *));
  while(1)
    {
      read_filename();
      if (!str_is_whitespace(input_buffer))
 {
   struct _file_worker_t *worker = file_worker_new(input_buffer, last_worker);
   if(file_worker_start(worker) != 0)
     {
       printf("Error starting worker for \"%s\"\n",
       (worker && worker->filename) ? worker->filename : "unknown");
       file_worker_free(worker);
     }
   else
     {
       last_worker = worker;
     }
 }
      else
 {
   printf("No file given...\n");
 }
    }
  pthread_exit(((void *)0));
}

variable_types: struct _file_worker_t { unsigned long id; double time_taken; char *filename; _Bool active; struct _file_worker_t *peer; } *


decompiled_code:
void __fastcall __noreturn worker_function(void *a1)
{
double v2; // xmm0_8
signed int v3; // edi
__int64 v4; // rsi
const char *v5; // rdx

v2 = 1.0;
v3 = 1;
if ( rand() % 10 <= 1 )
{
v3 = rand() % 4 + 7;
v2 = (double)v3;
}
sleep(v3);
v4 = *(_QWORD *)a1;
*((_BYTE *)a1 + 24) = 0;
v5 = (const char *)*((_QWORD *)a1 + 2);
*((double *)a1 + 1) = v2;
printf("Thread: [%u] retrieved: %s\n", v4, v5);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
double v2; // xmm0_8
signed int v3; // edi
__int64 v4; // rsi
const char *v5; // rdx

v2 = 1.0;
v3 = 1;
if ( rand() % 10 <= 1 )
{
v3 = rand() % 4 + 7;
v2 = (double)v3;
}
sleep(v3);
v4 = *(_QWORD *)a1;
*((_BYTE *)a1 + 24) = 0;
v5 = (const char *)*((_QWORD *)a1 + 2);
*((double *)a1 + 1) = v2;
printf("Thread: [%u] retrieved: %s\n", v4, v5);
pthread_exit(0LL);
}

original_code:
void*
worker_function(void* arg)
{
  file_worker_t* this = (file_worker_t*) arg;
  double sleep_time = ((rand() % 10) >= 2) ? 1 : 7 + (rand() % 4);
  sleep(sleep_time);
  this->time_taken = sleep_time;
  this->active = 0;
  printf("Thread: [%u] retrieved: %s\n", ((unsigned int)(this->id)), this->filename);
  pthread_exit(((void *)0));
}

canonical_original_code:
void *worker_function(void *arg)
{
  struct _file_worker_t *this = (file_worker_t*) arg;
  double sleep_time = ((rand() % 10) >= 2) ? 1 : 7 + (rand() % 4);
  sleep(sleep_time);
  this->time_taken = sleep_time;
  this->active = 0;
  printf("Thread: [%u] retrieved: %s\n", ((unsigned int)(this->id)), this->filename);
  pthread_exit(((void *)0));
}

variable_types:
arg
void *

return_type: void *

user_defined_types: struct _file_worker_t { unsigned long id; double time_taken; char *filename; _Bool active; struct _file_worker_t *peer; }


decompiled_code:
void __fastcall file_worker_free(void *ptr)
{
_BYTE *v2; // rdi

if ( ptr )
{
v2 = (_BYTE *)*((_QWORD *)ptr + 2);
if ( v2 && *v2 )
{
free(v2);
free(ptr);
}
else
{
free(ptr);
}
}
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
_BYTE *v2; // rdi

if ( ptr )
{
v2 = (_BYTE *)*((_QWORD *)ptr + 2);
if ( v2 && *v2 )
{
free(v2);
free(ptr);
}
else
{
free(ptr);
}
}
}

original_code:
void
file_worker_free(file_worker_t* this)
{
  if (this)
    {
      if (this->filename && *(this->filename)) free(this->filename);
      free(this);
    }
}

canonical_original_code:
void file_worker_free(struct _file_worker_t *this)
{
  if (this)
    {
      if (this->filename && *(this->filename)) free(this->filename);
      free(this);
    }
}


decompiled_code:
int __fastcall file_worker_start(void *arg)
{
return pthread_create((pthread_t *)arg, 0LL, (void *(*)(void *))worker_function, arg);
}

canonical_decompiled_code:
int __fastcall func9(void *arg)
{
return pthread_create((pthread_t *)arg, 0LL, (void *(*)(void *))worker_function, arg);
}

original_code:
int
file_worker_start(file_worker_t* this)
{
  return pthread_create(&(this->id), ((void *)0), worker_function, this);
}

canonical_original_code:
int file_worker_start(struct _file_worker_t *this)
{
  return pthread_create(&(this->id), ((void *)0), worker_function, this);
}


decompiled_code:
int __fastcall file_worker_join(pthread_t *a1)
{
pthread_t v1; // rdi
int result; // eax

if ( a1 && (v1 = *a1) != 0 )
result = pthread_join(v1, 0LL);
else
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func10(pthread_t *a1)
{
pthread_t v1; // rdi
int result; // eax

if ( a1 && (v1 = *a1) != 0 )
result = pthread_join(v1, 0LL);
else
result = -1;
return result;
}

original_code:
int
file_worker_join(file_worker_t* this)
{
  if (this && this->id) return pthread_join(this->id, (void*)((void *)0));
  else return -1;
}

canonical_original_code:
int file_worker_join(struct _file_worker_t *this)
{
  if (this && this->id) return pthread_join(this->id, (void *)((void *)0));
  else return -1;
}


decompiled_code:
char *read_filename()
{
char *result; // rax

fgets(input_buffer, 1024, stdin);
result = strchr(input_buffer, 10);
if ( result )
*result = 0;
return result;
}

canonical_decompiled_code:
char *func11()
{
char *result; // rax

fgets(input_buffer, 1024, stdin);
result = strchr(input_buffer, 10);
if ( result )
*result = 0;
return result;
}

original_code:
void
read_filename(void)
{
  fgets(input_buffer, 1024, stdin);
  char* newline_pos = strchr(input_buffer, '\n');
  if (newline_pos != ((void *)0)) *newline_pos = '\0';
}

canonical_original_code:
void read_filename()
{
  fgets(input_buffer, 1024, stdin);
  char *newline_pos = strchr(input_buffer, '\n');
  if (newline_pos != ((void *)0)) *newline_pos = '\0';
}


decompiled_code:
__int64 __fastcall str_is_whitespace(char *a1)
{
char v1; // al

if ( a1 )
{
while ( 1 )
{
v1 = *a1;
if ( !*a1 )
break;
if ( (unsigned __int8)(v1 - 9) > 1u && v1 != 32 )
return 0LL;
++a1;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *a1)
{
char v1; // al

if ( a1 )
{
while ( 1 )
{
v1 = *a1;
if ( !*a1 )
break;
if ( (unsigned __int8)(v1 - 9) > 1u && v1 != 32 )
return 0LL;
++a1;
}
}
return 1LL;
}

original_code:
_Bool
str_is_whitespace(char* str)
{
  char* c;
  for (c = str; c && *c; c++) {
    if (!(*c == ' ' || *c == '\t' || *c == '\n')) {
      return 0;
    }
  }
  return 1;
}

canonical_original_code:
_Bool str_is_whitespace(char *str)
{
  char *c;
  for (c = str; c && *c; c++) {
    if (!(*c == ' ' || *c == '\t' || *c == '\n')) {
      return 0;
    }
  }
  return 1;
}

variable_types:
c
char *


decompiled_code:
int __fastcall print_server_stats(int a1, double a2)
{
printf("total_time_taken: %f\n", a2);
return printf("Retrieved %d files, Average time: %f seconds\n", (unsigned int)a1, a2 / (double)a1);
}

canonical_decompiled_code:
int __fastcall func13(int a1, double a2)
{
printf("total_time_taken: %f\n", a2);
return printf("Retrieved %d files, Average time: %f seconds\n", (unsigned int)a1, a2 / (double)a1);
}

original_code:
void
print_server_stats(int worker_count, double total_time_taken)
{
  printf("total_time_taken: %f\n", total_time_taken);
  printf("Retrieved %d files, Average time: %f seconds\n",
  worker_count,
  (total_time_taken / (double)worker_count));
}