original_code:
int pick_up_item( entity_t *e, item_t *i, int quantity )
{
 if( !( i->flags & (1<<0) ) )
 {
  buf_t *b = bufnew( "You can't pick that up." );
  push_message( b );
  bufdestroy( b );
  return 0;
 }
 buf_t *msg = bufprintf( "You pick up %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 if( i->quantity == quantity )
 {
  inventory_add_item( e, i );
  int j = list_find( item_list, (void*)i );
  list_remove_index( item_list, j );
 }
 else
 {
  item_t *ii = alloc_item( i->name );
  ii->face = i->face;
  ii->color = i->color;
  ii->quantity = quantity;
  ii->quality = i->quality;
  ii->type = i->type;
  ii->place = i->place;
  ii->x = i->x;
  ii->y = i->y;
  ii->z = i->z;
  ii->flags = i->flags;
  if( i->type == ITEMTYPE_WEAPON )
  {
   ii->specific = malloc( sizeof(weapon_t) );
   memcpy( ii->specific, i->specific, sizeof(weapon_t) );
   weapon_t *w = (weapon_t*)ii->specific;
   w->ammo_type = clone_item( i->specific );
  }
  if( i->type == ITEMTYPE_ARMOR )
  {
   ii->specific = malloc( sizeof(armor_t) );
   memcpy( ii->specific, i->specific, sizeof(armor_t) );
  }
  i->quantity -= quantity;
  inventory_add_item( e, ii );
 }
 return 1;
}

canonical_original_code:
int pick_up_item(struct entity_t *e, struct item_t *i, int quantity)
{
 if( !( i->flags & (1<<0) ) )
 {
  struct buf_t *b = bufnew( "You can't pick that up." );
  push_message( b );
  bufdestroy( b );
  return 0;
 }
 struct buf_t *msg = bufprintf( "You pick up %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 if( i->quantity == quantity )
 {
  inventory_add_item( e, i );
  int j = list_find( item_list, (void*)i );
  list_remove_index( item_list, j );
 }
 else
 {
  struct item_t *ii = alloc_item( i->name );
  ii->face = i->face;
  ii->color = i->color;
  ii->quantity = quantity;
  ii->quality = i->quality;
  ii->type = i->type;
  ii->place = i->place;
  ii->x = i->x;
  ii->y = i->y;
  ii->z = i->z;
  ii->flags = i->flags;
  if( i->type == ITEMTYPE_WEAPON )
  {
   ii->specific = malloc( sizeof(weapon_t) );
   memcpy( ii->specific, i->specific, sizeof(weapon_t) );
   struct weapon_t *w = (weapon_t*)ii->specific;
   w->ammo_type = clone_item( i->specific );
  }
  if( i->type == ITEMTYPE_ARMOR )
  {
   ii->specific = malloc( sizeof(armor_t) );
   memcpy( ii->specific, i->specific, sizeof(armor_t) );
  }
  i->quantity -= quantity;
  inventory_add_item( e, ii );
 }
 return 1;
}

variable_types:
i
struct item_t *

variable_types: quantity int

variable_types: j int

variable_types: struct weapon_t { int type; int accuracy; int min_damage; struct item_t { struct buf_t { char *data; int length; } *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *ammo_type; int ammo_loaded; char *attack_name; } *


repo: deveah/etraom

original_code:
int drop_item( entity_t *e, item_t *i, int quantity )
{
 list_t *li = item_find_by_position( e->x, e->y, e->z );
 list_element *el = ((void *)0);
 item_t *found = ((void *)0);
 if( li )
  el = li->head;
 if( i->flags & (1<<1) )
 {
  while( el )
  {
   item_t *ij = (item_t*)el->data;
   if( items_alike( ij, i ) )
   {
    found = ij;
    break;
   }
   el = el->next;
  }
 }
 if( i->quantity == quantity )
 {
  int j = list_find( e->inventory, (void*)i );
  list_remove_index( e->inventory, j );
  if( found )
  {
   found->quantity += quantity;
   free_item( i );
  }
  else
  {
   list_add_head( item_list, i );
   i->x = e->x;
   i->y = e->y;
   i->z = e->z;
   i->place = ITEMPLACE_DUNGEON;
  }
 }
 else
 {
  if( found )
  {
   found->quantity += quantity;
  }
  else
  {
   item_t *ii = alloc_item( i->name );
   ii->face = i->face;
   ii->color = i->color;
   ii->quantity = quantity;
   ii->quality = i->quality;
   ii->type = i->type;
   ii->x = e->x;
   ii->y = e->y;
   ii->z = e->z;
   ii->place = ITEMPLACE_DUNGEON;
   ii->flags = i->flags;
   if( i->type == ITEMTYPE_WEAPON )
   {
    ii->specific = malloc( sizeof(weapon_t) );
    memcpy( ii->specific, i->specific, sizeof(weapon_t) );
    weapon_t *w = (weapon_t*)ii->specific;
    w->ammo_type = clone_item( i->specific );
   }
   if( i->type == ITEMTYPE_ARMOR )
   {
    ii->specific = malloc( sizeof(armor_t) );
    memcpy( ii->specific, i->specific, sizeof(armor_t) );
   }
   list_add_head( item_list, ii );
  }
  i->quantity -= quantity;
 }
 buf_t *msg = bufprintf( "You drop %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}

canonical_original_code:
int drop_item(struct entity_t *e, struct item_t *i, int quantity)
{
 struct list_t *li = item_find_by_position( e->x, e->y, e->z );
 struct _list_element *el = ((void *)0);
 struct item_t *found = ((void *)0);
 if( li )
  el = li->head;
 if( i->flags & (1<<1) )
 {
  while( el )
  {
   struct item_t *ij = (item_t*)el->data;
   if( items_alike( ij, i ) )
   {
    found = ij;
    break;
   }
   el = el->next;
  }
 }
 if( i->quantity == quantity )
 {
  int j = list_find( e->inventory, (void*)i );
  list_remove_index( e->inventory, j );
  if( found )
  {
   found->quantity += quantity;
   free_item( i );
  }
  else
  {
   list_add_head( item_list, i );
   i->x = e->x;
   i->y = e->y;
   i->z = e->z;
   i->place = ITEMPLACE_DUNGEON;
  }
 }
 else
 {
  if( found )
  {
   found->quantity += quantity;
  }
  else
  {
   struct item_t *ii = alloc_item( i->name );
   ii->face = i->face;
   ii->color = i->color;
   ii->quantity = quantity;
   ii->quality = i->quality;
   ii->type = i->type;
   ii->x = e->x;
   ii->y = e->y;
   ii->z = e->z;
   ii->place = ITEMPLACE_DUNGEON;
   ii->flags = i->flags;
   if( i->type == ITEMTYPE_WEAPON )
   {
    ii->specific = malloc( sizeof(weapon_t) );
    memcpy( ii->specific, i->specific, sizeof(weapon_t) );
    struct weapon_t *w = (weapon_t*)ii->specific;
    w->ammo_type = clone_item( i->specific );
   }
   if( i->type == ITEMTYPE_ARMOR )
   {
    ii->specific = malloc( sizeof(armor_t) );
    memcpy( ii->specific, i->specific, sizeof(armor_t) );
   }
   list_add_head( item_list, ii );
  }
  i->quantity -= quantity;
 }
 struct buf_t *msg = bufprintf( "You drop %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}

variable_types: struct entity_t { struct buf_t { char *data; int length; } *name; int face; int ap; int agility; int hp; int x; float ***lightmap; struct list_t { struct _list_element { void *data; struct _list_element *next; } *head; int length; } *inventory; struct item_t { struct buf_t *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *worn; struct weapon_t { int type; int accuracy; int min_damage; struct item_t *ammo_type; int ammo_loaded; char *attack_name; } *natural; struct ai_t { int type; int lx; int flags; } *ai; int flags; } *

variable_types: struct item_t { struct buf_t { char *data; int length; } *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *

return_type: int

user_defined_types: struct entity_t { struct buf_t *name; int face; int ap; int agility; int hp; int x; float ***lightmap; struct list_t *inventory; struct item_t *worn; struct weapon_t *natural; struct ai_t *ai; int flags; }

user_defined_types: struct list_t { struct _list_element *head; int length; }


decompiled_code:
__int64 __fastcall put_down_weapon(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rbp

v3 = *(_QWORD *)(a1 + 72);
if ( v3 )
{
*(_QWORD *)(a1 + 72) = 0LL;
inventory_add_item(a1, v3);
v5 = bufnew("You put down your weapon.", v3, v4);
push_message(v5);
bufdestroy(v5);
result = 1LL;
}
else
{
v7 = bufnew("You aren't wielding anything.", 0LL, a3);
push_message(v7);
bufdestroy(v7);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rbp

v3 = *(_QWORD *)(a1 + 72);
if ( v3 )
{
*(_QWORD *)(a1 + 72) = 0LL;
inventory_add_item(a1, v3);
v5 = bufnew("You put down your weapon.", v3, v4);
push_message(v5);
bufdestroy(v5);
result = 1LL;
}
else
{
v7 = bufnew("You aren't wielding anything.", 0LL, a3);
push_message(v7);
bufdestroy(v7);
result = 0LL;
}
return result;
}

original_code:
int put_down_weapon( entity_t *e )
{
 if( e->in_hand )
 {
  item_t *i = e->in_hand;
  e->in_hand = ((void *)0);
  inventory_add_item( e, i );
  buf_t *msg = bufnew( "You put down your weapon." );
  push_message( msg );
  bufdestroy( msg );
  return 1;
 }
 else
 {
  buf_t *msg = bufnew( "You aren't wielding anything." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
}

canonical_original_code:
int put_down_weapon(struct entity_t *e)
{
 if( e->in_hand )
 {
  struct item_t *i = e->in_hand;
  e->in_hand = ((void *)0);
  inventory_add_item( e, i );
  struct buf_t *msg = bufnew( "You put down your weapon." );
  push_message( msg );
  bufdestroy( msg );
  return 1;
 }
 else
 {
  struct buf_t *msg = bufnew( "You aren't wielding anything." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
}

variable_types: struct buf_t { char *data; int length; } *

user_defined_types: struct item_t { struct buf_t *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; }

user_defined_types: struct weapon_t { int type; int accuracy; int min_damage; struct item_t *ammo_type; int ammo_loaded; char *attack_name; }

user_defined_types: struct ai_t { int type; int lx; int flags; }

user_defined_types: struct _list_element { void *data; struct _list_element *next; }


decompiled_code:
__int64 __fastcall take_off_armor(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rbp

v3 = *(_QWORD *)(a1 + 64);
if ( v3 )
{
*(_QWORD *)(a1 + 64) = 0LL;
inventory_add_item(a1, v3);
v5 = bufnew("You take off your armor.", v3, v4);
push_message(v5);
bufdestroy(v5);
result = 1LL;
}
else
{
v7 = bufnew("You aren't wearing anything.", 0LL, a3);
push_message(v7);
bufdestroy(v7);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rsi
__int64 v4; // rdx
__int64 v5; // rbp
__int64 result; // rax
__int64 v7; // rbp

v3 = *(_QWORD *)(a1 + 64);
if ( v3 )
{
*(_QWORD *)(a1 + 64) = 0LL;
inventory_add_item(a1, v3);
v5 = bufnew("You take off your armor.", v3, v4);
push_message(v5);
bufdestroy(v5);
result = 1LL;
}
else
{
v7 = bufnew("You aren't wearing anything.", 0LL, a3);
push_message(v7);
bufdestroy(v7);
result = 0LL;
}
return result;
}

original_code:
int take_off_armor( entity_t *e )
{
 if( e->worn )
 {
  item_t *i = e->worn;
  e->worn = ((void *)0);
  inventory_add_item( e, i );
  buf_t *msg = bufnew( "You take off your armor." );
  push_message( msg );
  bufdestroy( msg );
  return 1;
 }
 else
 {
  buf_t *msg = bufnew( "You aren't wearing anything." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
}

canonical_original_code:
int take_off_armor(struct entity_t *e)
{
 if( e->worn )
 {
  struct item_t *i = e->worn;
  e->worn = ((void *)0);
  inventory_add_item( e, i );
  struct buf_t *msg = bufnew( "You take off your armor." );
  push_message( msg );
  bufdestroy( msg );
  return 1;
 }
 else
 {
  struct buf_t *msg = bufnew( "You aren't wearing anything." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
}

user_defined_types: struct buf_t { char *data; int length; }


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

put_down_weapon(a1, (__int64)a2, a3);
v3 = list_find(*(_QWORD *)(a1 + 56), a2);
list_remove_index(*(_QWORD *)(a1 + 56), v3);
v4 = *a2;
*(_QWORD *)(a1 + 72) = a2;
v5 = bufprintf("You now wield %s.", *v4);
push_message(v5);
bufdestroy(v5);
return 1LL;
}

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

func6(a1, (__int64)a2, a3);
v3 = list_find(*(_QWORD *)(a1 + 56), a2);
list_remove_index(*(_QWORD *)(a1 + 56), v3);
v4 = *a2;
*(_QWORD *)(a1 + 72) = a2;
v5 = bufprintf("You now wield %s.", *v4);
push_message(v5);
bufdestroy(v5);
return 1LL;
}

original_code:
int wield_item( entity_t *e, item_t *i )
{
 int j;
 put_down_weapon( e );
 j = list_find( e->inventory, (void*)i );
 list_remove_index( e->inventory, j );
 e->in_hand = i;
 buf_t *msg = bufprintf( "You now wield %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}

canonical_original_code:
int wield_item(struct entity_t *e, struct item_t *i)
{
 int j;
 put_down_weapon( e );
 j = list_find( e->inventory, (void *)i );
 list_remove_index( e->inventory, j );
 e->in_hand = i;
 struct buf_t *msg = bufprintf( "You now wield %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}


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

take_off_armor(a1, (__int64)a2, a3);
v3 = list_find(*(_QWORD *)(a1 + 56), a2);
list_remove_index(*(_QWORD *)(a1 + 56), v3);
v4 = *a2;
*(_QWORD *)(a1 + 64) = a2;
v5 = bufprintf("You now wear %s.", *v4);
push_message(v5);
bufdestroy(v5);
return 1LL;
}

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

func7(a1, (__int64)a2, a3);
v3 = list_find(*(_QWORD *)(a1 + 56), a2);
list_remove_index(*(_QWORD *)(a1 + 56), v3);
v4 = *a2;
*(_QWORD *)(a1 + 64) = a2;
v5 = bufprintf("You now wear %s.", *v4);
push_message(v5);
bufdestroy(v5);
return 1LL;
}

original_code:
int wear_item( entity_t *e, item_t *i )
{
 int j;
 take_off_armor( e );
 j = list_find( e->inventory, (void*)i );
 list_remove_index( e->inventory, j );
 e->worn = i;
 buf_t *msg = bufprintf( "You now wear %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}

canonical_original_code:
int wear_item(struct entity_t *e, struct item_t *i)
{
 int j;
 take_off_armor( e );
 j = list_find( e->inventory, (void *)i );
 list_remove_index( e->inventory, j );
 e->worn = i;
 struct buf_t *msg = bufprintf( "You now wear %s.", i->name->data );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}


decompiled_code:
__int64 __fastcall reload_weapon(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 v4; // r14
_QWORD *v6; // rbx
__int64 v7; // rbp
int v8; // ecx
__int64 v9; // rdx
int v10; // eax
__int64 v11; // rsi
__int64 v12; // rbp
const char *v14; // rdi
__int64 v15; // rbp
__int64 v16; // rdi

v3 = *(_QWORD *)(a1 + 72);
if ( *(_DWORD *)(v3 + 24) != 1 )
{
v14 = "You can't reload that.";
LABEL_11:
v15 = bufnew(v14, a2, a3);
push_message(v15);
bufdestroy(v15);
return 0LL;
}
v4 = *(_QWORD *)(v3 + 32);
if ( !*(_DWORD *)(v4 + 28) )
{
v14 = "This weapon doesn't require ammo.";
goto LABEL_11;
}
v6 = **(_QWORD ***)(a1 + 56);
if ( !v6 )
{
LABEL_12:
v14 = "You're out of ammo!";
goto LABEL_11;
}
while ( 1 )
{
v7 = *v6;
a2 = *(_QWORD *)(v4 + 16);
if ( (unsigned int)items_alike(*v6, a2) )
break;
v6 = (_QWORD *)v6[1];
if ( !v6 )
goto LABEL_12;
}
v8 = *(_DWORD *)(v4 + 28);
v9 = *(unsigned int *)(v4 + 24);
v10 = *(_DWORD *)(v7 + 16);
v11 = (unsigned int)(v8 - v9);
if ( v10 <= (int)v11 )
{
v16 = *(_QWORD *)(a1 + 56);
*(_DWORD *)(v4 + 24) = v9 + v10;
v11 = (unsigned int)list_find(v16, v7);
list_remove_index(*(_QWORD *)(a1 + 56), v11);
free_item((ITEM *)v7);
}
else
{
*(_DWORD *)(v7 + 16) = v10 - v11;
*(_DWORD *)(v4 + 24) = v8;
}
v12 = bufnew("You reload your weapon.", v11, v9);
push_message(v12);
bufdestroy(v12);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 v4; // r14
_QWORD *v6; // rbx
__int64 v7; // rbp
int v8; // ecx
__int64 v9; // rdx
int v10; // eax
__int64 v11; // rsi
__int64 v12; // rbp
const char *v14; // rdi
__int64 v15; // rbp
__int64 v16; // rdi

v3 = *(_QWORD *)(a1 + 72);
if ( *(_DWORD *)(v3 + 24) != 1 )
{
v14 = "You can't reload that.";
LABEL_11:
v15 = bufnew(v14, a2, a3);
push_message(v15);
bufdestroy(v15);
return 0LL;
}
v4 = *(_QWORD *)(v3 + 32);
if ( !*(_DWORD *)(v4 + 28) )
{
v14 = "This weapon doesn't require ammo.";
goto LABEL_11;
}
v6 = **(_QWORD ***)(a1 + 56);
if ( !v6 )
{
LABEL_12:
v14 = "You're out of ammo!";
goto LABEL_11;
}
while ( 1 )
{
v7 = *v6;
a2 = *(_QWORD *)(v4 + 16);
if ( (unsigned int)items_alike(*v6, a2) )
break;
v6 = (_QWORD *)v6[1];
if ( !v6 )
goto LABEL_12;
}
v8 = *(_DWORD *)(v4 + 28);
v9 = *(unsigned int *)(v4 + 24);
v10 = *(_DWORD *)(v7 + 16);
v11 = (unsigned int)(v8 - v9);
if ( v10 <= (int)v11 )
{
v16 = *(_QWORD *)(a1 + 56);
*(_DWORD *)(v4 + 24) = v9 + v10;
v11 = (unsigned int)list_find(v16, v7);
list_remove_index(*(_QWORD *)(a1 + 56), v11);
free_item((ITEM *)v7);
}
else
{
*(_DWORD *)(v7 + 16) = v10 - v11;
*(_DWORD *)(v4 + 24) = v8;
}
v12 = bufnew("You reload your weapon.", v11, v9);
push_message(v12);
bufdestroy(v12);
return 1LL;
}

original_code:
int reload_weapon( entity_t *e )
{
 list_element *el = e->inventory->head;
 if( e->in_hand->type != ITEMTYPE_WEAPON )
 {
  buf_t *msg = bufnew( "You can't reload that." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 weapon_t *w = (weapon_t*)e->in_hand->specific;
 if( w->clip_size == 0 )
 {
  buf_t *msg = bufnew( "This weapon doesn't require ammo." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 while( el )
 {
  item_t *i = (item_t*)el->data;
  if( items_alike( i, w->ammo_type ) )
  {
   int q = w->clip_size - w->ammo_loaded;
   if( i->quantity > q )
   {
    int q = w->clip_size - w->ammo_loaded;
    i->quantity -= q;
    w->ammo_loaded += q;
   }
   else
   {
    w->ammo_loaded += i->quantity;
    int j = list_find( e->inventory, (void*)i );
    list_remove_index( e->inventory, j );
    free_item( i );
   }
   buf_t *msg = bufnew( "You reload your weapon." );
   push_message( msg );
   bufdestroy( msg );
   return 1;
  }
  el = el->next;
 }
 buf_t *msg = bufnew( "You're out of ammo!" );
 push_message( msg );
 bufdestroy( msg );
 return 0;
}

canonical_original_code:
int reload_weapon(struct entity_t *e)
{
 struct _list_element *el = e->inventory->head;
 if( e->in_hand->type != ITEMTYPE_WEAPON )
 {
  struct buf_t *msg = bufnew( "You can't reload that." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 struct weapon_t *w = (weapon_t*)e->in_hand->specific;
 if( w->clip_size == 0 )
 {
  struct buf_t *msg = bufnew( "This weapon doesn't require ammo." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 while( el )
 {
  struct item_t *i = (item_t*)el->data;
  if( items_alike( i, w->ammo_type ) )
  {
   int q = w->clip_size - w->ammo_loaded;
   if( i->quantity > q )
   {
    int q = w->clip_size - w->ammo_loaded;
    i->quantity -= q;
    w->ammo_loaded += q;
   }
   else
   {
    w->ammo_loaded += i->quantity;
    int j = list_find( e->inventory, (void*)i );
    list_remove_index( e->inventory, j );
    free_item( i );
   }
   struct buf_t *msg = bufnew( "You reload your weapon." );
   push_message( msg );
   bufdestroy( msg );
   return 1;
  }
  el = el->next;
 }
 struct buf_t *msg = bufnew( "You're out of ammo!" );
 push_message( msg );
 bufdestroy( msg );
 return 0;
}

variable_types: struct _list_element { void *data; struct _list_element *next; } *

variable_types: q int


decompiled_code:
__int64 __fastcall unload_weapon(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 v4; // rbx
const char *v5; // rdi
__int64 v6; // rbp
__int64 v8; // rsi
__int64 v9; // rdx
__int64 v10; // rbp

v3 = *(_QWORD *)(a1 + 72);
if ( *(_DWORD *)(v3 + 24) != 1 )
{
v5 = "You can't unload that.";
goto LABEL_5;
}
v4 = *(_QWORD *)(v3 + 32);
a3 = *(unsigned int *)(v4 + 28);
if ( !(_DWORD)a3 || !*(_DWORD *)(v4 + 24) )
{
v5 = "This weapon doesn't have any ammo loaded.";
LABEL_5:
v6 = bufnew(v5, a2, a3);
push_message(v6);
bufdestroy(v6);
return 0LL;
}
v8 = clone_item(*(_QWORD *)(v4 + 16));
*(_DWORD *)(v8 + 16) = *(_DWORD *)(v4 + 24);
*(_DWORD *)(v4 + 24) = 0;
inventory_add_item(a1, v8);
v10 = bufnew("You unload your weapon.", v8, v9);
push_message(v10);
bufdestroy(v10);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 v4; // rbx
const char *v5; // rdi
__int64 v6; // rbp
__int64 v8; // rsi
__int64 v9; // rdx
__int64 v10; // rbp

v3 = *(_QWORD *)(a1 + 72);
if ( *(_DWORD *)(v3 + 24) != 1 )
{
v5 = "You can't unload that.";
goto LABEL_5;
}
v4 = *(_QWORD *)(v3 + 32);
a3 = *(unsigned int *)(v4 + 28);
if ( !(_DWORD)a3 || !*(_DWORD *)(v4 + 24) )
{
v5 = "This weapon doesn't have any ammo loaded.";
LABEL_5:
v6 = bufnew(v5, a2, a3);
push_message(v6);
bufdestroy(v6);
return 0LL;
}
v8 = clone_item(*(_QWORD *)(v4 + 16));
*(_DWORD *)(v8 + 16) = *(_DWORD *)(v4 + 24);
*(_DWORD *)(v4 + 24) = 0;
inventory_add_item(a1, v8);
v10 = bufnew("You unload your weapon.", v8, v9);
push_message(v10);
bufdestroy(v10);
return 1LL;
}

original_code:
int unload_weapon( entity_t *e )
{
 if( e->in_hand->type != ITEMTYPE_WEAPON )
 {
  buf_t *msg = bufnew( "You can't unload that." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 weapon_t *w = (weapon_t*)e->in_hand->specific;
 if( ( w->clip_size == 0 ) || ( w->ammo_loaded == 0 ) )
 {
  buf_t *msg = bufnew( "This weapon doesn't have any ammo loaded." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 item_t *i = clone_item( w->ammo_type );
 i->quantity = w->ammo_loaded;
 w->ammo_loaded = 0;
 inventory_add_item( e, i );
 buf_t *msg = bufnew( "You unload your weapon." );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}

canonical_original_code:
int unload_weapon(struct entity_t *e)
{
 if( e->in_hand->type != ITEMTYPE_WEAPON )
 {
  struct buf_t *msg = bufnew( "You can't unload that." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 struct weapon_t *w = (weapon_t*)e->in_hand->specific;
 if( ( w->clip_size == 0 ) || ( w->ammo_loaded == 0 ) )
 {
  struct buf_t *msg = bufnew( "This weapon doesn't have any ammo loaded." );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 struct item_t *i = clone_item( w->ammo_type );
 i->quantity = w->ammo_loaded;
 w->ammo_loaded = 0;
 inventory_add_item( e, i );
 struct buf_t *msg = bufnew( "You unload your weapon." );
 push_message( msg );
 bufdestroy( msg );
 return 1;
}


decompiled_code:
__int64 __fastcall free_map(void *ptr)
{
_QWORD *v2; // r8
__int64 v3; // rbx
void *v4; // rdi
_QWORD *v5; // r8
__int64 v6; // rbx
void *v7; // rdi
__int64 result; // rax

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 )
{
if ( *((int *)ptr + 2) > 0 )
{
v3 = 0LL;
do
{
v4 = (void *)v2[v3];
if ( v4 )
{
free(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
++v3;
}
while ( *((_DWORD *)ptr + 2) > (int)v3 );
}
free(v2);
}
v5 = (_QWORD *)*((_QWORD *)ptr + 3);
if ( v5 )
{
if ( *((int *)ptr + 2) > 0 )
{
v6 = 0LL;
do
{
v7 = (void *)v5[v6];
if ( v7 )
{
free(v7);
v5 = (_QWORD *)*((_QWORD *)ptr + 3);
}
++v6;
}
while ( *((_DWORD *)ptr + 2) > (int)v6 );
}
free(v5);
}
bufdestroy(*(_QWORD *)ptr);
free(ptr);
result = log_add("[free_map] Freed map 0x%08x.\n", ptr);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
_QWORD *v2; // r8
__int64 v3; // rbx
void *v4; // rdi
_QWORD *v5; // r8
__int64 v6; // rbx
void *v7; // rdi
__int64 result; // rax

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v2 )
{
if ( *((int *)ptr + 2) > 0 )
{
v3 = 0LL;
do
{
v4 = (void *)v2[v3];
if ( v4 )
{
free(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
++v3;
}
while ( *((_DWORD *)ptr + 2) > (int)v3 );
}
free(v2);
}
v5 = (_QWORD *)*((_QWORD *)ptr + 3);
if ( v5 )
{
if ( *((int *)ptr + 2) > 0 )
{
v6 = 0LL;
do
{
v7 = (void *)v5[v6];
if ( v7 )
{
free(v7);
v5 = (_QWORD *)*((_QWORD *)ptr + 3);
}
++v6;
}
while ( *((_DWORD *)ptr + 2) > (int)v6 );
}
free(v5);
}
bufdestroy(*(_QWORD *)ptr);
free(ptr);
result = log_add("[free_map] Freed map 0x%08x.\n", ptr);
}
return result;
}

original_code:
void free_map( map_t *m )
{
 int i;
 if( m )
 {
  if( m->terrain )
  {
   for( i = 0; i < m->width; i++ )
   {
    if( m->terrain[i] )
     free( m->terrain[i] );
   }
   free( m->terrain );
  }
  if( m->memory )
  {
   for( i = 0; i < m->width; i++ )
   {
    if( m->memory[i] )
     free( m->memory[i] );
   }
   free( m->memory );
  }
  bufdestroy( m->name );
  free( m );
  log_add( "[free_map] Freed map 0x%08x.\n", m );
 }
}

canonical_original_code:
void free_map(struct map_t *m)
{
 int i;
 if( m )
 {
  if( m->terrain )
  {
   for( i = 0; i < m->width; i++ )
   {
    if( m->terrain[i] )
     free( m->terrain[i] );
   }
   free( m->terrain );
  }
  if( m->memory )
  {
   for( i = 0; i < m->width; i++ )
   {
    if( m->memory[i] )
     free( m->memory[i] );
   }
   free( m->memory );
  }
  bufdestroy( m->name );
  free( m );
  log_add( "[free_map] Freed map 0x%08x.\n", m );
 }
}

variable_types: struct map_t { struct buf_t { char *data; int length; } *name; int width; struct tile_t { char *name; int face; int fg; int flags; } ***terrain; struct tile_t ***memory; int flags; } *

variable_types: i int

return_type: void

user_defined_types: struct map_t { struct buf_t *name; int width; struct tile_t ***terrain; struct tile_t ***memory; int flags; }


decompiled_code:
unsigned __int64 __fastcall clear_map(__int64 a1)
{
unsigned __int64 result; // rax
int v2; // er8
__int64 v3; // rsi
__int64 v4; // rcx
__int64 v5; // r9
_QWORD *v6; // rdx

result = *(int *)(a1 + 8);
if ( (int)result > 0 )
{
v2 = *(_DWORD *)(a1 + 12);
v3 = 8 * result;
v4 = 0LL;
result = (unsigned int)(v2 - 1);
v5 = 8 * result + 8;
do
{
if ( v2 > 0 )
{
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + v4);
v6 = (_QWORD *)(v5 + result);
do
{
*(_QWORD *)result = &tile_void;
result += 8LL;
}
while ( (_QWORD *)result != v6 );
}
v4 += 8LL;
}
while ( v4 != v3 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1)
{
unsigned __int64 result; // rax
int v2; // er8
__int64 v3; // rsi
__int64 v4; // rcx
__int64 v5; // r9
_QWORD *v6; // rdx

result = *(int *)(a1 + 8);
if ( (int)result > 0 )
{
v2 = *(_DWORD *)(a1 + 12);
v3 = 8 * result;
v4 = 0LL;
result = (unsigned int)(v2 - 1);
v5 = 8 * result + 8;
do
{
if ( v2 > 0 )
{
result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + v4);
v6 = (_QWORD *)(v5 + result);
do
{
*(_QWORD *)result = &tile_void;
result += 8LL;
}
while ( (_QWORD *)result != v6 );
}
v4 += 8LL;
}
while ( v4 != v3 );
}
return result;
}

original_code:
void clear_map( map_t *m )
{
 int i, j;
 for( i = 0; i < m->width; i++ )
 {
  for( j = 0; j < m->height; j++ )
  {
   m->terrain[i][j] = &tile_void;
  }
 }
}

canonical_original_code:
void clear_map(struct map_t *m)
{
 int i; int j;
 for( i = 0; i < m->width; i++ )
 {
  for( j = 0; j < m->height; j++ )
  {
   m->terrain[i][j] = &tile_void;
  }
 }
}

user_defined_types: struct tile_t { char *name; int face; int fg; int flags; }


decompiled_code:
__int64 __fastcall debug_dump_map(__int64 a1)
{
__int64 v1; // r13
__int64 result; // rax
__int64 i; // rbx
__int64 v4; // rax

v1 = 0LL;
for ( result = log_add(
"[debug_dump_map] Dumping map 0x%08x - width: %i, height: %i\n",
a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12)); *(_DWORD *)(a1 + 12) > (int)v1; result = log_add("\n") )
{
for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
while ( 1 )
{
v4 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * i) + 8 * v1);
if ( !v4 )
break;
++i;
log_add("%c", *(unsigned int *)(v4 + 8));
if ( *(_DWORD *)(a1 + 8) <= (int)i )
goto LABEL_7;
}
log_add("?");
}
LABEL_7:
++v1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r13
__int64 result; // rax
__int64 i; // rbx
__int64 v4; // rax

v1 = 0LL;
for ( result = log_add(
"[debug_dump_map] Dumping map 0x%08x - width: %i, height: %i\n",
a1,
*(unsigned int *)(a1 + 8),
*(unsigned int *)(a1 + 12)); *(_DWORD *)(a1 + 12) > (int)v1; result = log_add("\n") )
{
for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
while ( 1 )
{
v4 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * i) + 8 * v1);
if ( !v4 )
break;
++i;
log_add("%c", *(unsigned int *)(v4 + 8));
if ( *(_DWORD *)(a1 + 8) <= (int)i )
goto LABEL_7;
}
log_add("?");
}
LABEL_7:
++v1;
}
return result;
}

original_code:
void debug_dump_map( map_t *m )
{
 int i, j;
 log_add( "[debug_dump_map] Dumping map 0x%08x - width: %i, height: %i\n",
  m, m->width, m->height );
 for( j = 0; j < m->height; j++ )
 {
  for( i = 0; i < m->width; i++ )
  {
   if( m->terrain[i][j] )
    log_add( "%c", m->terrain[i][j]->face );
   else
    log_add( "?" );
  }
  log_add( "\n" );
 }
}

canonical_original_code:
void debug_dump_map(struct map_t *m)
{
 int i; int j;
 log_add( "[debug_dump_map] Dumping map 0x%08x - width: %i, height: %i\n",
  m, m->width, m->height );
 for( j = 0; j < m->height; j++ )
 {
  for( i = 0; i < m->width; i++ )
  {
   if( m->terrain[i][j] )
    log_add( "%c", m->terrain[i][j]->face );
   else
    log_add( "?" );
  }
  log_add( "\n" );
 }
}


decompiled_code:
__int64 __fastcall is_legal(unsigned int a1, unsigned int a2)
{
return (a1 <= 0x4F) & (unsigned __int8)(a2 <= 0x14);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, unsigned int a2)
{
return (a1 <= 0x4F) & (unsigned __int8)(a2 <= 0x14);
}

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall is_legal_strict(int a1, int a2)
{
return ((unsigned int)(a1 - 1) <= 0x4D) & (unsigned __int8)((unsigned int)(a2 - 1) <= 0x12);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2)
{
return ((unsigned int)(a1 - 1) <= 0x4D) & (unsigned __int8)((unsigned int)(a2 - 1) <= 0x12);
}


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

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

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

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

original_code:
list_t *alloc_list( void )
{
 list_t *l = malloc( sizeof( list_t ) );
 l->head = ((void *)0);
 l->tail = ((void *)0);
 l->length = 0;
 return l;
}

canonical_original_code:
struct list_t *alloc_list()
{
 struct list_t *l = malloc( sizeof( list_t ) );
 l->head = ((void *)0);
 l->tail = ((void *)0);
 l->length = 0;
 return l;
}

variable_types: struct list_t { struct _list_element { void *data; struct _list_element *next; } *head; int length; } *


decompiled_code:
int __fastcall print_list(unsigned int **a1)
{
unsigned int *v1; // rbx

v1 = *a1;
printf("Printing list 0x%08x: ", a1);
for ( ; v1; v1 = (unsigned int *)*((_QWORD *)v1 + 1) )
printf("%08x ", *v1);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int **a1)
{
unsigned int *v1; // rbx

v1 = *a1;
printf("Printing list 0x%08x: ", a1);
for ( ; v1; v1 = (unsigned int *)*((_QWORD *)v1 + 1) )
printf("%08x ", *v1);
return putchar(10);
}

original_code:
void print_list( list_t *l )
{
 list_element *el = l->head;
 printf( "Printing list 0x%08x: ", (int)l );
 while( el )
 {
  printf( "%08x ", (int) el->data );
  el = el->next;
 }
 printf( "\n" );
}

canonical_original_code:
void print_list(struct list_t *l)
{
 struct _list_element *el = l->head;
 printf( "Printing list 0x%08x: ", (int)l );
 while( el )
 {
  printf( "%08x ", (int) el->data );
  el = el->next;
 }
 printf( "\n" );
}


decompiled_code:
void __fastcall free_list(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
while ( v2 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
while ( v2 );
}
free(ptr);
}

original_code:
void free_list( list_t *l )
{
 list_element *el = l->head;
 list_element *temp;
 while( el )
 {
  temp = el;
  el = el->next;
  free( temp );
 }
 free( l );
}

canonical_original_code:
void free_list(struct list_t *l)
{
 struct _list_element *el = l->head;
 struct _list_element *temp;
 while( el )
 {
  temp = el;
  el = el->next;
  free( temp );
 }
 free( l );
}


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

v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
result = a2(*v3);
}
while ( v2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD))
{
_QWORD *v2; // rbx
_QWORD *v3; // rax
__int64 result; // rax

v2 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v3 = v2;
v2 = (_QWORD *)v2[1];
result = a2(*v3);
}
while ( v2 );
}
return result;
}

original_code:
void list_traverse( list_t *l, void (*func)(void*) )
{
 list_element *el = l->head;
 list_element *temp;
 while( el )
 {
  temp = el;
  el = el->next;
  func( temp->data );
 }
}

canonical_original_code:
void list_traverse(struct list_t *l, void (*func)(void *))
{
 struct _list_element *el = l->head;
 struct _list_element *temp;
 while( el )
 {
  temp = el;
  el = el->next;
  func( temp->data );
 }
}

variable_types:
func
void (*)(void *)


decompiled_code:
__int64 __fastcall list_find(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
unsigned int v3; // er8

v2 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v3 = 0;
while ( *v2 != a2 )
{
v2 = (_QWORD *)v2[1];
++v3;
if ( !v2 )
return 0xFFFFFFFFLL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
unsigned int v3; // er8

v2 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v3 = 0;
while ( *v2 != a2 )
{
v2 = (_QWORD *)v2[1];
++v3;
if ( !v2 )
return 0xFFFFFFFFLL;
}
return v3;
}

original_code:
int list_find( list_t *l, void *item )
{
 list_element *el = l->head;
 int i = 0;
 while( el )
 {
  if( el->data == item )
   return i;
  el = el->next;
  i++;
 }
 return -1;
}

canonical_original_code:
int list_find(struct list_t *l, void *item)
{
 struct _list_element *el = l->head;
 int i = 0;
 while( el )
 {
  if( el->data == item )
   return i;
  el = el->next;
  i++;
 }
 return -1;
}

variable_types:
item
void *


bufcats

decompiled_code:
char *__fastcall bufcats(__int64 a1, const char *a2)
{
int v3; // eax
void *v4; // rdi
int v5; // ebp
char *v6; // rax
char *result; // rax
char *v8; // rax

v3 = strlen(a2);
v4 = *(void **)a1;
v5 = v3;
if ( v4 )
{
v6 = (char *)realloc(v4, v3 + *(_DWORD *)(a1 + 8));
*(_QWORD *)a1 = v6;
result = strcat(v6, a2);
*(_DWORD *)(a1 + 8) += v5;
}
else
{
v8 = (char *)malloc(v3);
*(_QWORD *)a1 = v8;
result = strcpy(v8, a2);
*(_DWORD *)(a1 + 8) = v5;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, const char *a2)
{
int v3; // eax
void *v4; // rdi
int v5; // ebp
char *v6; // rax
char *result; // rax
char *v8; // rax

v3 = strlen(a2);
v4 = *(void **)a1;
v5 = v3;
if ( v4 )
{
v6 = (char *)realloc(v4, v3 + *(_DWORD *)(a1 + 8));
*(_QWORD *)a1 = v6;
result = strcat(v6, a2);
*(_DWORD *)(a1 + 8) += v5;
}
else
{
v8 = (char *)malloc(v3);
*(_QWORD *)a1 = v8;
result = strcpy(v8, a2);
*(_DWORD *)(a1 + 8) = v5;
}
return result;
}

original_code:
void bufcats( buf_t *b, char* s )
{
 int l = strlen( s );
 if( b->data )
 {
  b->data = realloc( b->data, sizeof(char) * ( l + b->length ) );
  strcat( b->data, s );
  b->length += l;
 }
 else
 {
  b->data = malloc( sizeof(char) * l );
  strcpy( b->data, s );
  b->length = l;
 }
}

canonical_original_code:
void bufcats(struct buf_t *b, char *s)
{
 int l = strlen( s );
 if( b->data )
 {
  b->data = realloc( b->data, sizeof(char) * ( l + b->length ) );
  strcat( b->data, s );
  b->length += l;
 }
 else
 {
  b->data = malloc( sizeof(char) * l );
  strcpy( b->data, s );
  b->length = l;
 }
}

variable_types:
s
char *

variable_types: l int


bufcat

original_code:
void bufcat( buf_t *dest, buf_t *src )
{
 if( !src->data )
  return;
 if( dest->data )
 {
  dest->data = realloc( dest->data,
   sizeof(char) * ( dest->length + src->length ) );
  strcat( dest->data, src->data );
  dest->length += src->length;
 }
 else
 {
  dest->data = malloc( sizeof(char) * src->length );
  strcpy( dest->data, src->data );
  dest->length = src->length;
 }
}

canonical_original_code:
void bufcat(struct buf_t *dest, struct buf_t *src)
{
 if( !src->data )
  return;
 if( dest->data )
 {
  dest->data = realloc( dest->data,
   sizeof(char) * ( dest->length + src->length ) );
  strcat( dest->data, src->data );
  dest->length += src->length;
 }
 else
 {
  dest->data = malloc( sizeof(char) * src->length );
  strcpy( dest->data, src->data );
  dest->length = src->length;
 }
}

variable_types:
src
struct buf_t *


bufprintf

decompiled_code:
_DWORD *bufprintf(char *format, ...)
{
char *v1; // rbp
_DWORD *v2; // r12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = (char *)malloc(0x100uLL);
vsprintf(v1, format, arg);
v2 = bufnew(v1);
free(v1);
return v2;
}

canonical_decompiled_code:
_DWORD *func2(char *format, ...)
{
char *v1; // rbp
_DWORD *v2; // r12
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
v1 = (char *)malloc(0x100uLL);
vsprintf(v1, format, arg);
v2 = bufnew(v1);
free(v1);
return v2;
}

original_code:
buf_t *bufprintf( char* format, ... )
{
 va_list args;
 char *str = malloc( 256 * sizeof(char) );
 buf_t *b;
 __builtin_va_start(args,format);
 vsprintf( str, format, args );
 __builtin_va_end(args);
 b = bufnew( str );
 free( str );
 return b;
}

canonical_original_code:
struct buf_t *bufprintf(char *format, ...)
{
 __builtin_va_list args;
 char *str = malloc( 256 * sizeof(char) );
 struct buf_t *b;
 __builtin_va_start(args,format);
 vsprintf( str, format, args );
 __builtin_va_end(args);
 b = bufnew( str );
 free( str );
 return b;
}

variable_types:
format
char *

variable_types:
str
char *

return_type: struct buf_t { char *data; int length; } *


bufdestroy

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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}
}

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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
free(v2);
free(ptr);
}
}

original_code:
void bufdestroy( buf_t *b )
{
 if( b )
 {
  if( b->data )
   free( b->data );
  free( b );
 }
}

canonical_original_code:
void bufdestroy(struct buf_t *b)
{
 if( b )
 {
  if( b->data )
   free( b->data );
  free( b );
 }
}


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

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 24) == 2 )
result = **(unsigned int **)(a1 + 32);
}
return result;
}

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

result = 0LL;
if ( a1 )
{
if ( *(_DWORD *)(a1 + 24) == 2 )
result = **(unsigned int **)(a1 + 32);
}
return result;
}

original_code:
int get_item_ac( item_t *i )
{
 if( i )
 {
  if( i->type == ITEMTYPE_ARMOR )
  {
   armor_t *a = (armor_t*)i->specific;
   return a->ac;
  }
  else
  {
   return 0;
  }
 }
 else
 {
  return 0;
 }
}

canonical_original_code:
int get_item_ac(struct item_t *i)
{
 if( i )
 {
  if( i->type == ITEMTYPE_ARMOR )
  {
   struct armor_t *a = (armor_t*)i->specific;
   return a->ac;
  }
  else
  {
   return 0;
  }
 }
 else
 {
  return 0;
 }
}

variable_types:
a
struct armor_t { int ac; } *


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

v2 = (_QWORD *)*a1;
if ( !*a1 )
return 0LL;
v3 = 0;
do
{
v4 = *v2;
if ( (unsigned int)items_alike(a2, *v2) )
v3 += *(_DWORD *)(v4 + 16);
v2 = (_QWORD *)v2[1];
}
while ( v2 );
return v3;
}

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

v2 = (_QWORD *)*a1;
if ( !*a1 )
return 0LL;
v3 = 0;
do
{
v4 = *v2;
if ( (unsigned int)items_alike(a2, *v2) )
v3 += *(_DWORD *)(v4 + 16);
v2 = (_QWORD *)v2[1];
}
while ( v2 );
return v3;
}

original_code:
int count_ammo( list_t *li, item_t *a )
{
 list_element *el = li->head;
 int n = 0;
 while( el )
 {
  item_t *i = (item_t*)el->data;
  if( items_alike( a, i ) )
  {
   n += i->quantity;
  }
  el = el->next;
 }
 return n;
}

canonical_original_code:
int count_ammo(struct list_t *li, struct item_t *a)
{
 struct _list_element *el = li->head;
 int n = 0;
 while( el )
 {
  struct item_t *i = (item_t*)el->data;
  if( items_alike( a, i ) )
  {
   n += i->quantity;
  }
  el = el->next;
 }
 return n;
}

variable_types: struct item_t { struct buf_t { char *data; int length; } *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *

variable_types: n int


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

result = (unsigned int)(*(_DWORD *)(a1 + 24)
- (*(_DWORD *)(a2 + 8)
+ rand() % (*(_DWORD *)(a2 + 12) - *(_DWORD *)(a2 + 8))));
*(_DWORD *)(a1 + 24) = result;
if ( (int)result <= 0 )
result = entity_die(a1);
return result;
}

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

result = (unsigned int)(*(_DWORD *)(a1 + 24)
- (*(_DWORD *)(a2 + 8)
+ rand() % (*(_DWORD *)(a2 + 12) - *(_DWORD *)(a2 + 8))));
*(_DWORD *)(a1 + 24) = result;
if ( (int)result <= 0 )
result = entity_die(a1);
return result;
}

original_code:
void take_damage( entity_t *e, weapon_t *w )
{
 int damage = rand() % ( w->max_damage - w->min_damage ) + w->min_damage;
 e->hp -= damage;
 if( e->hp <= 0 )
  entity_die( e );
}

canonical_original_code:
void take_damage(struct entity_t *e, struct weapon_t *w)
{
 int damage = rand() % ( w->max_damage - w->min_damage ) + w->min_damage;
 e->hp -= damage;
 if( e->hp <= 0 )
  entity_die( e );
}

variable_types: struct entity_t { struct buf_t { char *data; int length; } *name; int face; int ap; int agility; int hp; int x; float ***lightmap; struct list_t { struct _list_element { void *data; struct _list_element *next; } *head; int length; } *inventory; struct item_t { struct buf_t *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *worn; struct weapon_t { int type; int accuracy; int min_damage; struct item_t *ammo_type; int ammo_loaded; char *attack_name; } *natural; struct ai_t { int type; int lx; int flags; } *ai; int flags; } *

variable_types:
w
struct weapon_t *

variable_types: damage int


decompiled_code:
__int64 __fastcall melee_attack(__int64 a1, __int64 **a2)
{
__int64 v3; // rax
_DWORD *v5; // r13
const char *v7; // rdi
__int64 v8; // rsi
__int64 v9; // rbp
__int64 *v10; // rax
const char *v11; // rdi
__int64 v12; // rsi
__int64 v13; // r14
__int64 v14; // rsi
const char *v15; // rdi
__int64 v16; // rdx
__int64 v17; // r12
__int64 *v18; // rax

v3 = *(_QWORD *)(a1 + 72);
if ( v3 )
{
if ( *(_DWORD *)(v3 + 24) == 1 )
{
v5 = *(_DWORD **)(v3 + 32);
if ( !*v5 )
{
if ( a1 == player )
{
v11 = "You hit the %s!";
v12 = **a2;
}
else
{
if ( a2 != (__int64 **)player )
{
LABEL_6:
take_damage((__int64)a2, (__int64)v5);
return 1LL;
}
v10 = *(__int64 **)a1;
v11 = "The %s hits you!";
v12 = *v10;
}
v13 = bufprintf(v11, v12, player);
push_message(v13);
bufdestroy(v13);
goto LABEL_6;
}
if ( a1 != player )
return 0LL;
v7 = "You're not wielding a melee weapon.";
}
else
{
v7 = "You're not wielding a weapon.";
if ( a1 != player )
return 0LL;
}
v9 = bufnew(v7, a2, player);
push_message(v9);
bufdestroy(v9);
return 0LL;
}
v8 = *(_QWORD *)(a1 + 80);
if ( a1 == player )
{
v14 = *(_QWORD *)(v8 + 32);
v15 = "You %s the %s.";
v16 = **a2;
LABEL_19:
v17 = bufprintf(v15, v14, v16);
push_message(v17);
bufdestroy(v17);
v8 = *(_QWORD *)(a1 + 80);
goto LABEL_11;
}
if ( a2 == (__int64 **)player )
{
v18 = *(__int64 **)a1;
v16 = *(_QWORD *)(v8 + 32);
v15 = "The %s %ss you.";
v14 = *v18;
goto LABEL_19;
}
LABEL_11:
take_damage((__int64)a2, v8);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 **a2)
{
__int64 v3; // rax
_DWORD *v5; // r13
const char *v7; // rdi
__int64 v8; // rsi
__int64 v9; // rbp
__int64 *v10; // rax
const char *v11; // rdi
__int64 v12; // rsi
__int64 v13; // r14
__int64 v14; // rsi
const char *v15; // rdi
__int64 v16; // rdx
__int64 v17; // r12
__int64 *v18; // rax

v3 = *(_QWORD *)(a1 + 72);
if ( v3 )
{
if ( *(_DWORD *)(v3 + 24) == 1 )
{
v5 = *(_DWORD **)(v3 + 32);
if ( !*v5 )
{
if ( a1 == player )
{
v11 = "You hit the %s!";
v12 = **a2;
}
else
{
if ( a2 != (__int64 **)player )
{
LABEL_6:
func2((__int64)a2, (__int64)v5);
return 1LL;
}
v10 = *(__int64 **)a1;
v11 = "The %s hits you!";
v12 = *v10;
}
v13 = bufprintf(v11, v12, player);
push_message(v13);
bufdestroy(v13);
goto LABEL_6;
}
if ( a1 != player )
return 0LL;
v7 = "You're not wielding a melee weapon.";
}
else
{
v7 = "You're not wielding a weapon.";
if ( a1 != player )
return 0LL;
}
v9 = bufnew(v7, a2, player);
push_message(v9);
bufdestroy(v9);
return 0LL;
}
v8 = *(_QWORD *)(a1 + 80);
if ( a1 == player )
{
v14 = *(_QWORD *)(v8 + 32);
v15 = "You %s the %s.";
v16 = **a2;
LABEL_19:
v17 = bufprintf(v15, v14, v16);
push_message(v17);
bufdestroy(v17);
v8 = *(_QWORD *)(a1 + 80);
goto LABEL_11;
}
if ( a2 == (__int64 **)player )
{
v18 = *(__int64 **)a1;
v16 = *(_QWORD *)(v8 + 32);
v15 = "The %s %ss you.";
v14 = *v18;
goto LABEL_19;
}
LABEL_11:
func2((__int64)a2, v8);
return 1LL;
}

original_code:
int melee_attack( entity_t *atk, entity_t *def )
{
 if( !atk->in_hand )
 {
  if( atk == player )
  {
   buf_t *msg = bufprintf( "You %s the %s.", atk->natural->attack_name, def->name->data );
   push_message( msg );
   bufdestroy( msg );
  }
  else if( def == player )
  {
   buf_t *msg = bufprintf( "The %s %ss you.", atk->name->data, atk->natural->attack_name );
   push_message( msg );
   bufdestroy( msg );
  }
  take_damage( def, atk->natural );
  return 1;
 }
 if( atk->in_hand->type != ITEMTYPE_WEAPON )
 {
  if( atk == player )
  {
   buf_t *msg = bufnew( "You're not wielding a weapon." );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 weapon_t *wpn = (weapon_t*)atk->in_hand->specific;
 if( wpn->type != WEAPONTYPE_MELEE )
 {
  if( atk == player )
  {
   buf_t *msg = bufnew( "You're not wielding a melee weapon." );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( atk == player )
 {
  buf_t *msg = bufprintf( "You hit the %s!", def->name->data );
  push_message( msg );
  bufdestroy( msg );
 }
 else if( def == player )
 {
  buf_t *msg = bufprintf( "The %s hits you!", atk->name->data );
  push_message( msg );
  bufdestroy( msg );
 }
 take_damage( def, wpn );
 return 1;
}

canonical_original_code:
int melee_attack(struct entity_t *atk, struct entity_t *def)
{
 if( !atk->in_hand )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufprintf( "You %s the %s.", atk->natural->attack_name, def->name->data );
   push_message( msg );
   bufdestroy( msg );
  }
  else if( def == player )
  {
   struct buf_t *msg = bufprintf( "The %s %ss you.", atk->name->data, atk->natural->attack_name );
   push_message( msg );
   bufdestroy( msg );
  }
  take_damage( def, atk->natural );
  return 1;
 }
 if( atk->in_hand->type != ITEMTYPE_WEAPON )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufnew( "You're not wielding a weapon." );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 struct weapon_t *wpn = (weapon_t*)atk->in_hand->specific;
 if( wpn->type != WEAPONTYPE_MELEE )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufnew( "You're not wielding a melee weapon." );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( atk == player )
 {
  struct buf_t *msg = bufprintf( "You hit the %s!", def->name->data );
  push_message( msg );
  bufdestroy( msg );
 }
 else if( def == player )
 {
  struct buf_t *msg = bufprintf( "The %s hits you!", atk->name->data );
  push_message( msg );
  bufdestroy( msg );
 }
 take_damage( def, wpn );
 return 1;
}

variable_types:
def
struct entity_t *

variable_types: struct weapon_t { int type; int accuracy; int min_damage; struct item_t { struct buf_t { char *data; int length; } *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *ammo_type; int ammo_loaded; char *attack_name; } *


decompiled_code:
__int64 __fastcall ranged_attack(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
unsigned int v5; // er15
unsigned int *v7; // rax
const char *v8; // rdi
__int64 v9; // rbp
int v10; // eax
bool v11; // zf
unsigned int v12; // ebp
unsigned int v13; // ebx
__int64 v14; // r14
__int64 v15; // r13
const char ***v16; // rax
__int64 v17; // rbp
__int64 v18; // rbp
__int64 v19; // r13
__int64 v20; // [rsp+8h] [rbp-60h]
int v21; // [rsp+14h] [rbp-54h]
__int64 v22; // [rsp+28h] [rbp-40h]

v4 = *(_QWORD *)(a1 + 72);
v21 = a3;
if ( !v4 )
{
v5 = 0;
v8 = "You're not wielding anything!";
if ( player != a1 )
return v5;
goto LABEL_7;
}
if ( *(_DWORD *)(v4 + 24) != 1 )
{
v5 = 0;
if ( player != a1 )
return v5;
v8 = "You're not wielding a weapon!";
LABEL_7:
v9 = bufnew(v8, a2, a3);
push_message(v9);
bufdestroy(v9);
return v5;
}
v7 = *(unsigned int **)(v4 + 32);
v5 = *v7;
v22 = (__int64)v7;
if ( *v7 != 1 )
{
v5 = 0;
v8 = "You're not wielding a ranged weapon!";
if ( player != a1 )
return v5;
goto LABEL_7;
}
v10 = v7[6];
if ( v10 <= 0 )
{
v5 = 0;
v18 = bufnew("The clip is empty!", a2, a3);
push_message(v18);
bufdestroy(v18);
}
else
{
v11 = player == a1;
v12 = *(_DWORD *)(a1 + 32);
v13 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(v22 + 24) = v10 - 1;
if ( v11 )
{
v19 = bufnew("You shoot your weapon!", a2, a3);
push_message(v19);
bufdestroy(v19);
}
v14 = 8 * ((int)a2 + (__int64)(int)v12);
v15 = 8 * (v21 + (__int64)(int)v13);
while ( 1 )
{
v12 += a2;
v13 += v21;
if ( (unsigned int)is_legal(v12, v13) )
{
if ( (*(_BYTE *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(dungeon + 8LL * *(int *)(a1 + 40)) + 16LL) + v14)
+ v15)
+ 16LL) & 1) != 0 )
break;
}
wattrset(stdscr, 512);
if ( wmove(stdscr, v13 + 2, v12) != -1 )
waddch(stdscr, 0x2AuLL);
v16 = (const char ***)entity_find_by_position(v12, v13, *(unsigned int *)(a1 + 40));
v14 += 8LL * (int)a2;
v15 += 8LL * v21;
if ( v16 )
{
v20 = (__int64)v16;
v17 = bufprintf("You hit the %s!", **v16);
push_message(v17);
bufdestroy(v17);
take_damage(v20, v22);
break;
}
}
wrefresh(stdscr);
msleep(100LL);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rax
unsigned int v5; // er15
unsigned int *v7; // rax
const char *v8; // rdi
__int64 v9; // rbp
int v10; // eax
bool v11; // zf
unsigned int v12; // ebp
unsigned int v13; // ebx
__int64 v14; // r14
__int64 v15; // r13
const char ***v16; // rax
__int64 v17; // rbp
__int64 v18; // rbp
__int64 v19; // r13
__int64 v20; // [rsp+8h] [rbp-60h]
int v21; // [rsp+14h] [rbp-54h]
__int64 v22; // [rsp+28h] [rbp-40h]

v4 = *(_QWORD *)(a1 + 72);
v21 = a3;
if ( !v4 )
{
v5 = 0;
v8 = "You're not wielding anything!";
if ( player != a1 )
return v5;
goto LABEL_7;
}
if ( *(_DWORD *)(v4 + 24) != 1 )
{
v5 = 0;
if ( player != a1 )
return v5;
v8 = "You're not wielding a weapon!";
LABEL_7:
v9 = bufnew(v8, a2, a3);
push_message(v9);
bufdestroy(v9);
return v5;
}
v7 = *(unsigned int **)(v4 + 32);
v5 = *v7;
v22 = (__int64)v7;
if ( *v7 != 1 )
{
v5 = 0;
v8 = "You're not wielding a ranged weapon!";
if ( player != a1 )
return v5;
goto LABEL_7;
}
v10 = v7[6];
if ( v10 <= 0 )
{
v5 = 0;
v18 = bufnew("The clip is empty!", a2, a3);
push_message(v18);
bufdestroy(v18);
}
else
{
v11 = player == a1;
v12 = *(_DWORD *)(a1 + 32);
v13 = *(_DWORD *)(a1 + 36);
*(_DWORD *)(v22 + 24) = v10 - 1;
if ( v11 )
{
v19 = bufnew("You shoot your weapon!", a2, a3);
push_message(v19);
bufdestroy(v19);
}
v14 = 8 * ((int)a2 + (__int64)(int)v12);
v15 = 8 * (v21 + (__int64)(int)v13);
while ( 1 )
{
v12 += a2;
v13 += v21;
if ( (unsigned int)is_legal(v12, v13) )
{
if ( (*(_BYTE *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(dungeon + 8LL * *(int *)(a1 + 40)) + 16LL) + v14)
+ v15)
+ 16LL) & 1) != 0 )
break;
}
wattrset(stdscr, 512);
if ( wmove(stdscr, v13 + 2, v12) != -1 )
waddch(stdscr, 0x2AuLL);
v16 = (const char ***)entity_find_by_position(v12, v13, *(unsigned int *)(a1 + 40));
v14 += 8LL * (int)a2;
v15 += 8LL * v21;
if ( v16 )
{
v20 = (__int64)v16;
v17 = bufprintf("You hit the %s!", **v16);
push_message(v17);
bufdestroy(v17);
func2(v20, v22);
break;
}
}
wrefresh(stdscr);
msleep(100LL);
}
return v5;
}

original_code:
int ranged_attack( entity_t *atk, int tx, int ty )
{
 weapon_t *wpn;
 int cx = atk->x, cy = atk->y;
 if( !atk->in_hand )
 {
  if( atk == player )
  {
   buf_t *msg = bufnew( "You're not wielding anything!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( atk->in_hand->type != ITEMTYPE_WEAPON )
 {
  if( atk == player )
  {
   buf_t *msg = bufnew( "You're not wielding a weapon!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 wpn = (weapon_t*)atk->in_hand->specific;
 if( wpn->type != WEAPONTYPE_HANDGUN )
 {
  if( atk == player )
  {
   buf_t *msg = bufnew( "You're not wielding a ranged weapon!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( wpn->ammo_loaded >= 1 )
 {
  wpn->ammo_loaded -= 1;
 }
 else
 {
  buf_t *msg = bufnew( "The clip is empty!" );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 if( atk == player )
 {
  buf_t *msg = bufnew( "You shoot your weapon!" );
  push_message( msg );
  bufdestroy( msg );
 }
 while( 1 )
 {
  cx += tx;
  cy += ty;
  if( is_legal( cx, cy ) &&
   ( dungeon[atk->z]->terrain[cx][cy]->flags & (1<<0) ) )
  {
   break;
  }
  wattrset(stdscr,((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8)))));
  (wmove((stdscr),((cy+2)),((cx))) == (-1) ? (-1) : waddch((stdscr),(('*'))));
  entity_t *e = entity_find_by_position( cx, cy, atk->z );
  if( e )
  {
   buf_t *msg = bufprintf( "You hit the %s!", e->name->data );
   push_message( msg );
   bufdestroy( msg );
   take_damage( e, wpn );
   break;
  }
 }
 wrefresh(stdscr);
 msleep( 100 );
 return 1;
}

canonical_original_code:
int ranged_attack(struct entity_t *atk, int tx, int ty)
{
 struct weapon_t *wpn;
 int cx = atk->x; int cy = atk->y;
 if( !atk->in_hand )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufnew( "You're not wielding anything!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( atk->in_hand->type != ITEMTYPE_WEAPON )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufnew( "You're not wielding a weapon!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 wpn = (struct weapon_t *)atk->in_hand->specific;
 if( wpn->type != WEAPONTYPE_HANDGUN )
 {
  if( atk == player )
  {
   struct buf_t *msg = bufnew( "You're not wielding a ranged weapon!" );
   push_message( msg );
   bufdestroy( msg );
  }
  return 0;
 }
 if( wpn->ammo_loaded >= 1 )
 {
  wpn->ammo_loaded -= 1;
 }
 else
 {
  struct buf_t *msg = bufnew( "The clip is empty!" );
  push_message( msg );
  bufdestroy( msg );
  return 0;
 }
 if( atk == player )
 {
  struct buf_t *msg = bufnew( "You shoot your weapon!" );
  push_message( msg );
  bufdestroy( msg );
 }
 while( 1 )
 {
  cx += tx;
  cy += ty;
  if( is_legal( cx, cy ) &&
   ( dungeon[atk->z]->terrain[cx][cy]->flags & (1<<0) ) )
  {
   break;
  }
  wattrset(stdscr,((((chtype)(((2))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8)))));
  (wmove((stdscr),((cy+2)),((cx))) == (-1) ? (-1) : waddch((stdscr),(('*'))));
  struct entity_t *e = entity_find_by_position( cx, cy, atk->z );
  if( e )
  {
   struct buf_t *msg = bufprintf( "You hit the %s!", e->name->data );
   push_message( msg );
   bufdestroy( msg );
   take_damage( e, wpn );
   break;
  }
 }
 wrefresh(stdscr);
 msleep( 100 );
 return 1;
}

variable_types: tx int

variable_types: ty int

variable_types: cx int

variable_types: cy int

variable_types: struct buf_t { char *data; int length; } *


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

result = 0LL;
if ( a1 > 9 )
{
a1 -= 10;
result = 0x200000LL;
}
switch ( a1 )
{
case 1:
BYTE1(result) |= 1u;
break;
case 2:
BYTE1(result) |= 2u;
break;
case 3:
BYTE1(result) |= 3u;
break;
case 4:
BYTE1(result) |= 4u;
break;
case 5:
BYTE1(result) |= 5u;
break;
case 6:
BYTE1(result) |= 6u;
break;
case 7:
BYTE1(result) |= 7u;
break;
case 8:
BYTE1(result) |= 8u;
break;
default:
result = 0LL;
break;
}
return result;
}

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

result = 0LL;
if ( a1 > 9 )
{
a1 -= 10;
result = 0x200000LL;
}
switch ( a1 )
{
case 1:
BYTE1(result) |= 1u;
break;
case 2:
BYTE1(result) |= 2u;
break;
case 3:
BYTE1(result) |= 3u;
break;
case 4:
BYTE1(result) |= 4u;
break;
case 5:
BYTE1(result) |= 5u;
break;
case 6:
BYTE1(result) |= 6u;
break;
case 7:
BYTE1(result) |= 7u;
break;
case 8:
BYTE1(result) |= 8u;
break;
default:
result = 0LL;
break;
}
return result;
}

variable_types: c int

variable_types: r int


decompiled_code:
__int64 free_entity_types()
{
_QWORD *v0; // rbx

v0 = *(_QWORD **)entity_type_list;
if ( *(_QWORD *)entity_type_list )
{
do
{
free_entity(*v0);
v0 = (_QWORD *)v0[1];
}
while ( v0 );
}
return free_list();
}

canonical_decompiled_code:
__int64 func2()
{
_QWORD *v0; // rbx

v0 = *(_QWORD **)entity_type_list;
if ( *(_QWORD *)entity_type_list )
{
do
{
free_entity(*v0);
v0 = (_QWORD *)v0[1];
}
while ( v0 );
}
return free_list();
}

original_code:
void free_entity_types( void )
{
 list_element *el = entity_type_list->head;
 while( el )
 {
  entity_t *e = (entity_t*)el->data;
  free_entity( e );
  el = el->next;
 }
 free_list( entity_type_list );
}

canonical_original_code:
void free_entity_types()
{
 struct _list_element *el = entity_type_list->head;
 while( el )
 {
  struct entity_t *e = (entity_t*)el->data;
  free_entity( e );
  el = el->next;
 }
 free_list( entity_type_list );
}

variable_types: struct _list_element { void *data; struct _list_element *next; } *


decompiled_code:
__int64 free_ammo_types()
{
__int64 i; // rbx

for ( i = *(_QWORD *)ammo_type_list; i; i = *(_QWORD *)(i + 8) )
free_item(*(ITEM **)i);
return free_list();
}

canonical_decompiled_code:
__int64 func4()
{
__int64 i; // rbx

for ( i = *(_QWORD *)ammo_type_list; i; i = *(_QWORD *)(i + 8) )
free_item(*(ITEM **)i);
return free_list();
}

original_code:
void free_ammo_types( void )
{
 list_element *el = ammo_type_list->head;
 while( el )
 {
  item_t *i = (item_t*)el->data;
  free_item( i );
  el = el->next;
 }
 free_list( ammo_type_list );
}

canonical_original_code:
void free_ammo_types()
{
 struct _list_element *el = ammo_type_list->head;
 while( el )
 {
  struct item_t *i = (item_t*)el->data;
  free_item( i );
  el = el->next;
 }
 free_list( ammo_type_list );
}


decompiled_code:
void __fastcall free_ai(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void free_ai( ai_t *a )
{
 if( a )
 {
  free( a );
 }
}

canonical_original_code:
void free_ai(struct ai_t *a)
{
 if( a )
 {
  free( a );
 }
}

variable_types: struct ai_t { int type; int lx; int flags; } *


decompiled_code:
__int64 __fastcall dumb_ai(_DWORD *a1)
{
int v2; // ebx
unsigned int v3; // er13
unsigned int v4; // er12
__int64 v5; // rdi
__int64 v6; // rsi

v2 = 100;
do
{
v3 = rand() % 3 - 1;
v4 = rand() % 3 - 1;
if ( (int)(v4 * v4 + v3 * v3) > 0 )
{
if ( (unsigned int)is_legal(v3 + a1[8], v4 + a1[9]) )
{
v5 = v3 + a1[8];
v6 = v4 + a1[9];
if ( (*(_BYTE *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(dungeon + 8LL * (int)a1[10]) + 16LL)
+ 8LL * (int)v5)
+ 8LL * (int)v6)
+ 16LL) & 1) == 0
&& !entity_find_by_position(v5, v6, (int)a1[10]) )
{
return move_relative(a1, v3, v4);
}
}
}
--v2;
}
while ( v2 );
return 1LL;
}

original_code:
int dumb_ai( entity_t *e )
{
 int rx, ry;
 int tries = 0;
 while( 1 )
 {
  tries++;
  if( tries > 100 )
   return 1;
  rx = rand()%3 - 1;
  ry = rand()%3 - 1;
  if( ( rx*rx + ry*ry > 0 ) &&
   ( is_legal( e->x+rx, e->y+ry ) ) &&
   !( dungeon[e->z]->terrain[e->x+rx][e->y+ry]->flags & (1<<0) ) &&
   ( !entity_find_by_position( e->x+rx, e->y+ry, e->z ) ) )
   break;
 }
 return move_relative( e, rx, ry );
}

canonical_original_code:
int dumb_ai(struct entity_t *e)
{
 int rx; int ry;
 int tries = 0;
 while( 1 )
 {
  tries++;
  if( tries > 100 )
   return 1;
  rx = rand()%3 - 1;
  ry = rand()%3 - 1;
  if( ( rx*rx + ry*ry > 0 ) &&
   ( is_legal( e->x+rx, e->y+ry ) ) &&
   !( dungeon[e->z]->terrain[e->x+rx][e->y+ry]->flags & (1<<0) ) &&
   ( !entity_find_by_position( e->x+rx, e->y+ry, e->z ) ) )
   break;
 }
 return move_relative( e, rx, ry );
}

variable_types: rx int

variable_types: ry int

variable_types: tries int


original_code:
int basic_melee_ai( entity_t *e )
{
 int dx = 0, dy = 0;
 if( !( e->ai->flags & (1<<0) ) )
 {
  return dumb_ai( e );
 }
 if( ( e->z == player->z ) &&
  ( distance( e->x, e->y, player->x, player->y ) == 1 ) )
 {
  melee_attack( e, player );
  return 1;
 }
 else
 {
  if( e->x > e->ai->lx )
   dx = -1;
  else if( e->x < e->ai->lx )
   dx = 1;
  else
   dx = 0;
  if( e->y > e->ai->ly )
   dy = -1;
  else if( e->y < e->ai->ly )
   dy = 1;
  else
   dy = 0;
  if( is_legal( e->x+dx, e->y ) &&
   !( dungeon[e->z]->terrain[e->x+dx][e->y]->flags & (1<<0) ) &&
   !( entity_find_by_position( e->x+dx, e->y, e->z ) ) )
  {
   e->x += dx;
  }
  if( is_legal( e->x, e->y+dy ) &&
   !( dungeon[e->z]->terrain[e->x][e->y+dy]->flags & (1<<0) ) &&
   !( entity_find_by_position( e->x, e->y+dy, e->z ) ) )
  {
   e->y += dy;
  }
  return 1;
 }
}

canonical_original_code:
int basic_melee_ai(struct entity_t *e)
{
 int dx = 0; int dy = 0;
 if( !( e->ai->flags & (1<<0) ) )
 {
  return dumb_ai( e );
 }
 if( ( e->z == player->z ) &&
  ( distance( e->x, e->y, player->x, player->y ) == 1 ) )
 {
  melee_attack( e, player );
  return 1;
 }
 else
 {
  if( e->x > e->ai->lx )
   dx = -1;
  else if( e->x < e->ai->lx )
   dx = 1;
  else
   dx = 0;
  if( e->y > e->ai->ly )
   dy = -1;
  else if( e->y < e->ai->ly )
   dy = 1;
  else
   dy = 0;
  if( is_legal( e->x+dx, e->y ) &&
   !( dungeon[e->z]->terrain[e->x+dx][e->y]->flags & (1<<0) ) &&
   !( entity_find_by_position( e->x+dx, e->y, e->z ) ) )
  {
   e->x += dx;
  }
  if( is_legal( e->x, e->y+dy ) &&
   !( dungeon[e->z]->terrain[e->x][e->y+dy]->flags & (1<<0) ) &&
   !( entity_find_by_position( e->x, e->y+dy, e->z ) ) )
  {
   e->y += dy;
  }
  return 1;
 }
}

variable_types: struct entity_t { struct buf_t { char *data; int length; } *name; int face; int ap; int agility; int hp; int x; float ***lightmap; struct list_t { struct _list_element { void *data; struct _list_element *next; } *head; int length; } *inventory; struct item_t { struct buf_t *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *worn; struct weapon_t { int type; int accuracy; int min_damage; struct item_t *ammo_type; int ammo_loaded; char *attack_name; } *natural; struct ai_t { int type; int lx; int flags; } *ai; int flags; } *

variable_types: dx int

variable_types: dy int


original_code:
int run_ai( entity_t *e )
{
 if( !e->ai )
 {
  return 1;
 }
 if( e->lightmap[player->z][player->x][player->y] > 0.0f )
 {
  e->ai->lx = player->x;
  e->ai->ly = player->y;
  e->ai->lz = player->z;
  if( !( e->ai->flags & (1<<0) ) )
   e->ai->flags |= (1<<0);
 }
 switch( e->ai->type )
 {
 case AITYPE_DUMB:
  return dumb_ai( e );
 case AITYPE_BASICMELEE:
  return basic_melee_ai( e );
 default:
  return 1;
 }
}

canonical_original_code:
int run_ai(struct entity_t *e)
{
 if( !e->ai )
 {
  return 1;
 }
 if( e->lightmap[player->z][player->x][player->y] > 0.0f )
 {
  e->ai->lx = player->x;
  e->ai->ly = player->y;
  e->ai->lz = player->z;
  if( !( e->ai->flags & (1<<0) ) )
   e->ai->flags |= (1<<0);
 }
 switch( e->ai->type )
 {
 case AITYPE_DUMB:
  return dumb_ai( e );
 case AITYPE_BASICMELEE:
  return basic_melee_ai( e );
 default:
  return 1;
 }
}


original_code:
void reveal_map( int n )
{
 int i, j;
 for( i = 0; i < 80; i++ )
 {
  for( j = 0; j < 21; j++ )
  {
   dungeon[n]->memory[i][j] = dungeon[n]->terrain[i][j];
  }
 }
}

canonical_original_code:
void reveal_map(int n)
{
 int i; int j;
 for( i = 0; i < 80; i++ )
 {
  for( j = 0; j < 21; j++ )
  {
   dungeon[n]->memory[i][j] = dungeon[n]->terrain[i][j];
  }
 }
}

variable_types: n int

variable_types: i int

variable_types: j int


original_code:
int do_ray( int z, int x1, int y1, int x2, int y2 )
{
 int cx = x1, cy = y1;
 int dx, dy, sx, sy;
 float err, e2;
 dx = abs( x2-x1 );
 dy = abs( y2-y1 );
 if( x1 < x2 ) sx = 1; else sx = -1;
 if( y1 < y2 ) sy = 1; else sy = -1;
 err = dx-dy;
 while( 1 )
 {
  if( ( cx == x2 ) && ( cy == y2 ) )
   return 1;
  if( ( is_legal( cx, cy ) ) &&
   ( dungeon[z]->terrain[cx][cy]->flags & (1<<1) ) )
   return 0;
  e2 = 2 * err;
  if( e2 > -dy )
  {
   err -= dy;
   cx += sx;
  }
  if( e2 < dx )
  {
   err += dx;
   cy += sy;
  }
 }
 return 1;
}

canonical_original_code:
int do_ray(int z, int x1, int y1, int x2, int y2)
{
 int cx = x1; int cy = y1;
 int dx; int dy; int sx; int sy;
 float err; float e2;
 dx = abs( x2-x1 );
 dy = abs( y2-y1 );
 if( x1 < x2 ) sx = 1; else sx = -1;
 if( y1 < y2 ) sy = 1; else sy = -1;
 err = dx-dy;
 while( 1 )
 {
  if( ( cx == x2 ) && ( cy == y2 ) )
   return 1;
  if( ( is_legal( cx, cy ) ) &&
   ( dungeon[z]->terrain[cx][cy]->flags & (1<<1) ) )
   return 0;
  e2 = 2 * err;
  if( e2 > -dy )
  {
   err -= dy;
   cx += sx;
  }
  if( e2 < dx )
  {
   err += dx;
   cy += sy;
  }
 }
 return 1;
}

variable_types: z int

variable_types: sx int

variable_types: sy int

variable_types: err float


decompiled_code:
__int64 __fastcall do_fov(__int64 a1, int a2)
{
int v2; // edx
__int64 result; // rax
int v4; // er13
__int64 v5; // r15
int v6; // ebx
__int64 v7; // rbp

clear_lightmap(a1, *(_DWORD *)(a1 + 40));
v2 = *(_DWORD *)(a1 + 32);
result = (unsigned int)(v2 + a2);
v4 = v2 - a2;
if ( v2 - a2 <= (int)result )
{
result = *(unsigned int *)(a1 + 36);
v5 = 8LL * v4;
do
{
v6 = result - a2;
if ( (int)result - a2 <= (int)result + a2 )
{
v7 = 4LL * v6;
do
{
if ( (unsigned int)is_legal((unsigned int)v4, (unsigned int)v6)
&& (int)distance(*(unsigned int *)(a1 + 32), *(unsigned int *)(a1 + 36), (unsigned int)v4, (unsigned int)v6) <= a2 )
{
if ( (unsigned int)do_ray(*(_DWORD *)(a1 + 40), *(_DWORD *)(a1 + 32), *(_DWORD *)(a1 + 36), v4, v6) )
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 48) + 8LL * *(int *)(a1 + 40)) + v5) + v7) = 1065353216;
}
result = *(unsigned int *)(a1 + 36);
++v6;
v7 += 4LL;
}
while ( a2 + (int)result >= v6 );
v2 = *(_DWORD *)(a1 + 32);
}
++v4;
v5 += 8LL;
}
while ( a2 + v2 >= v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
int v2; // edx
__int64 result; // rax
int v4; // er13
__int64 v5; // r15
int v6; // ebx
__int64 v7; // rbp

func1(a1, *(_DWORD *)(a1 + 40));
v2 = *(_DWORD *)(a1 + 32);
result = (unsigned int)(v2 + a2);
v4 = v2 - a2;
if ( v2 - a2 <= (int)result )
{
result = *(unsigned int *)(a1 + 36);
v5 = 8LL * v4;
do
{
v6 = result - a2;
if ( (int)result - a2 <= (int)result + a2 )
{
v7 = 4LL * v6;
do
{
if ( (unsigned int)is_legal((unsigned int)v4, (unsigned int)v6)
&& (int)distance(*(unsigned int *)(a1 + 32), *(unsigned int *)(a1 + 36), (unsigned int)v4, (unsigned int)v6) <= a2 )
{
if ( (unsigned int)func3(*(_DWORD *)(a1 + 40), *(_DWORD *)(a1 + 32), *(_DWORD *)(a1 + 36), v4, v6) )
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 48) + 8LL * *(int *)(a1 + 40)) + v5) + v7) = 1065353216;
}
result = *(unsigned int *)(a1 + 36);
++v6;
v7 += 4LL;
}
while ( a2 + (int)result >= v6 );
v2 = *(_DWORD *)(a1 + 32);
}
++v4;
v5 += 8LL;
}
while ( a2 + v2 >= v4 );
}
return result;
}

original_code:
void do_fov( entity_t *e, int radius )
{
 int i, j;
 clear_lightmap( e, e->z );
 for( i = e->x-radius; i <= e->x+radius; i++ )
  for( j = e->y-radius; j <= e->y+radius; j++ )
  {
   if( is_legal( i, j ) &&
    ( distance( e->x, e->y, i, j ) <= radius ) &&
    do_ray( e->z, e->x, e->y, i, j ) )
    e->lightmap[e->z][i][j] = 1.0f;
  }
}

canonical_original_code:
void do_fov(struct entity_t *e, int radius)
{
 int i; int j;
 clear_lightmap( e, e->z );
 for( i = e->x-radius; i <= e->x+radius; i++ )
  for( j = e->y-radius; j <= e->y+radius; j++ )
  {
   if( is_legal( i, j ) &&
    ( distance( e->x, e->y, i, j ) <= radius ) &&
    do_ray( e->z, e->x, e->y, i, j ) )
    e->lightmap[e->z][i][j] = 1.0f;
  }
}

variable_types: radius int


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

v0 = game_flags & 1;
if ( (game_flags & 1) == 0 )
return 1LL;
logfile = fopen("./log", "w");
if ( !logfile )
return 0LL;
fprintf(logfile, "Etraom version %s.\n\n", "0.0.1");
return v0;
}

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

v0 = game_flags & 1;
if ( (game_flags & 1) == 0 )
return 1LL;
logfile = fopen("./log", "w");
if ( !logfile )
return 0LL;
fprintf(logfile, "Etraom version %s.\n\n", "0.0.1");
return v0;
}

original_code:
int open_logfile( void )
{
 if( !( game_flags & (1<<0) ) )
  return 1;
 logfile = fopen( "./log", "w" );
 if( !logfile )
  return 0;
 fprintf( logfile, "Etraom version %s.\n\n", "0.0.1" );
 return 1;
}

canonical_original_code:
int open_logfile()
{
 if( !( game_flags & (1<<0) ) )
  return 1;
 logfile = fopen( "./log", "w" );
 if( !logfile )
  return 0;
 fprintf( logfile, "Etraom version %s.\n\n", "0.0.1" );
 return 1;
}


decompiled_code:
__int64 close_logfile()
{
if ( logfile )
fclose(logfile);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
if ( logfile )
fclose(logfile);
return 0LL;
}

original_code:
int close_logfile( void )
{
 if( !logfile )
  return 0;
 fclose( logfile );
 return 0;
}

canonical_original_code:
int close_logfile()
{
 if( !logfile )
  return 0;
 fclose( logfile );
 return 0;
}


decompiled_code:
int log_add(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( format )
{
if ( logfile )
{
va_start(arg, format);
result = vfprintf(logfile, format, arg);
}
}
return result;
}

canonical_decompiled_code:
int func2(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( format )
{
if ( logfile )
{
va_start(arg, format);
result = vfprintf(logfile, format, arg);
}
}
return result;
}

original_code:
void log_add( char *format, ... )
{
 va_list args;
 if( !format )
  return;
 if( !logfile )
  return;
 __builtin_va_start(args,format);
 vfprintf( logfile, format, args );
 __builtin_va_end(args);
}

canonical_original_code:
void log_add(char *format, ...)
{
 __builtin_va_list args;
 if( !format )
  return;
 if( !logfile )
  return;
 __builtin_va_start(args,format);
 vfprintf( logfile, format, args );
 __builtin_va_end(args);
}

variable_types:
format
char *


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

result = malloc(0x24uLL);
result[8] = 0;
return result;
}

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

result = malloc(0x24uLL);
result[8] = 0;
return result;
}

original_code:
link_t *alloc_link( void )
{
 link_t *l = malloc( sizeof(link_t) );
 l->flags = 0;
 return l;
}

canonical_original_code:
struct link_t *alloc_link()
{
 struct link_t *l = malloc( sizeof(link_t) );
 l->flags = 0;
 return l;
}

variable_types: struct link_t { int src_x; int dest_x; int face; int flags; } *


decompiled_code:
void __fastcall free_link(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void free_link( link_t *l )
{
 if( l )
  free( l );
}

canonical_original_code:
void free_link(struct link_t *l)
{
 if( l )
  free( l );
}

user_defined_types: struct link_t { int src_x; int dest_x; int face; int flags; }


decompiled_code:
void free_links()
{
__int64 i; // rbx

for ( i = *(_QWORD *)link_list; i; i = *(_QWORD *)(i + 8) )
free_link(*(void **)i);
}

canonical_decompiled_code:
void func2()
{
__int64 i; // rbx

for ( i = *(_QWORD *)link_list; i; i = *(_QWORD *)(i + 8) )
func1(*(void **)i);
}

original_code:
void free_links( void )
{
 list_element *e = link_list->head;
 while( e )
 {
  free_link( (link_t*)e->data );
  e = e->next;
 }
}

canonical_original_code:
void free_links()
{
 struct _list_element *e = link_list->head;
 while( e )
 {
  free_link( (struct link_t *)e->data );
  e = e->next;
 }
}

variable_types: struct _list_element { void *data; struct _list_element *next; } *


distance

decompiled_code:
__int64 __fastcall distance(int a1, int a2, int a3, int a4)
{
double v4; // xmm0_8
__int64 result; // rax

v4 = (double)((a2 - a4) * (a2 - a4) + (a1 - a3) * (a1 - a3));
if ( v4 < 0.0 )
result = (unsigned int)(int)sqrt(v4);
else
result = (unsigned int)(int)sqrt(v4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, int a3, int a4)
{
double v4; // xmm0_8
__int64 result; // rax

v4 = (double)((a2 - a4) * (a2 - a4) + (a1 - a3) * (a1 - a3));
if ( v4 < 0.0 )
result = (unsigned int)(int)sqrt(v4);
else
result = (unsigned int)(int)sqrt(v4);
return result;
}

original_code:
int distance( int x1, int y1, int x2, int y2 )
{
 return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}

canonical_original_code:
int distance(int x1, int y1, int x2, int y2)
{
 return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}


msleep

decompiled_code:
int __fastcall msleep(int a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 1000 * a1;
return select(0, 0LL, 0LL, 0LL, &v2);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_usec = 1000 * a1;
return select(0, 0LL, 0LL, 0LL, &v2);
}

original_code:
void msleep( int ms )
{
 struct timeval tv;
 tv.tv_sec = 0;
 tv.tv_usec = ms * 1000;
 select( 0, ((void *)0), ((void *)0), ((void *)0), &tv );
}

canonical_original_code:
void msleep(int ms)
{
 struct timeval tv;
 tv.tv_sec = 0;
 tv.tv_usec = ms * 1000;
 select( 0, ((void *)0), ((void *)0), ((void *)0), &tv );
}

variable_types: ms int

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


decompiled_code:
__int64 __fastcall inventory_add_item(_QWORD *a1, __int64 a2)
{
__int64 **v3; // rdi
__int64 *v4; // rbx
__int64 v5; // r14
__int64 result; // rax
__int64 v7; // r12

v3 = (__int64 **)a1[7];
if ( (*(_BYTE *)(a2 + 56) & 2) != 0 )
{
v4 = *v3;
v5 = 0LL;
if ( (unsigned int)items_alike(a1[9]) )
v5 = a1[9];
if ( (unsigned int)items_alike(a1[8]) )
v5 = a1[8];
if ( v4 )
{
while ( 1 )
{
v7 = *v4;
if ( (unsigned int)items_alike(a2) )
break;
v4 = (__int64 *)v4[1];
if ( !v4 )
goto LABEL_7;
}
v5 = v7;
if ( v7 )
goto LABEL_8;
}
else
{
LABEL_7:
if ( v5 )
{
LABEL_8:
*(_DWORD *)(v5 + 16) += *(_DWORD *)(a2 + 16);
free_item((ITEM *)a2);
return 1LL;
}
}
list_add_head(a1[7]);
result = 1LL;
*(_DWORD *)(a2 + 40) = 1;
}
else
{
list_add_head(v3);
result = 1LL;
*(_DWORD *)(a2 + 40) = 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, __int64 a2)
{
__int64 **v3; // rdi
__int64 *v4; // rbx
__int64 v5; // r14
__int64 result; // rax
__int64 v7; // r12

v3 = (__int64 **)a1[7];
if ( (*(_BYTE *)(a2 + 56) & 2) != 0 )
{
v4 = *v3;
v5 = 0LL;
if ( (unsigned int)items_alike(a1[9]) )
v5 = a1[9];
if ( (unsigned int)items_alike(a1[8]) )
v5 = a1[8];
if ( v4 )
{
while ( 1 )
{
v7 = *v4;
if ( (unsigned int)items_alike(a2) )
break;
v4 = (__int64 *)v4[1];
if ( !v4 )
goto LABEL_7;
}
v5 = v7;
if ( v7 )
goto LABEL_8;
}
else
{
LABEL_7:
if ( v5 )
{
LABEL_8:
*(_DWORD *)(v5 + 16) += *(_DWORD *)(a2 + 16);
free_item((ITEM *)a2);
return 1LL;
}
}
list_add_head(a1[7]);
result = 1LL;
*(_DWORD *)(a2 + 40) = 1;
}
else
{
list_add_head(v3);
result = 1LL;
*(_DWORD *)(a2 + 40) = 1;
}
return result;
}

original_code:
int inventory_add_item( entity_t *e, item_t *i )
{
 list_element *el = e->inventory->head;
 item_t *found = ((void *)0);
 if( i->flags & (1<<1) )
 {
  if( items_alike( e->in_hand, i ) )
   found = e->in_hand;
  if( items_alike( e->worn, i ) )
   found = e->worn;
  while( el )
  {
   item_t *ii = (item_t*)el->data;
   if( items_alike( i, ii ) )
   {
    found = ii;
    break;
   }
   el = el->next;
  }
  if( found )
  {
   found->quantity += i->quantity;
   free_item( i );
  }
  else
  {
   list_add_head( e->inventory, i );
   i->place = ITEMPLACE_ENTITY;
  }
 }
 else
 {
  list_add_head( e->inventory, i );
  i->place = ITEMPLACE_ENTITY;
 }
 return 1;
}

canonical_original_code:
int inventory_add_item(struct entity_t *e, struct item_t *i)
{
 struct _list_element *el = e->inventory->head;
 struct item_t *found = ((void *)0);
 if( i->flags & (1<<1) )
 {
  if( items_alike( e->in_hand, i ) )
   found = e->in_hand;
  if( items_alike( e->worn, i ) )
   found = e->worn;
  while( el )
  {
   struct item_t *ii = (item_t*)el->data;
   if( items_alike( i, ii ) )
   {
    found = ii;
    break;
   }
   el = el->next;
  }
  if( found )
  {
   found->quantity += i->quantity;
   free_item( i );
  }
  else
  {
   list_add_head( e->inventory, i );
   i->place = ITEMPLACE_ENTITY;
  }
 }
 else
 {
  list_add_head( e->inventory, i );
  i->place = ITEMPLACE_ENTITY;
 }
 return 1;
}

variable_types:
i
struct item_t *

variable_types: struct item_t { struct buf_t { char *data; int length; } *name; int face; int quantity; float quality; int type; void *specific; int place; int x; int flags; } *


main

canonical_name: main

repo: deveah/jeg

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

signal(2, interrupt);
gen_default_drums();
Pa_Initialize();
Pa_GetDefaultOutputDevice();
Pa_GetDeviceInfo();
Pa_OpenStream();
wave_output = sf_open();
v3 = time(0LL);
srand(v3);
bass_z = 0;
bass_freq = midi_to_hz(28LL, 32LL, 44100.0);
Pa_StartStream();
while ( 1 )
Pa_Sleep();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

signal(2, interrupt);
func11();
Pa_Initialize();
Pa_GetDefaultOutputDevice();
Pa_GetDeviceInfo();
Pa_OpenStream();
wave_output = sf_open();
v3 = time(0LL);
srand(v3);
bass_z = 0;
bass_freq = func8(28LL, 32LL, 44100.0);
Pa_StartStream();
while ( 1 )
Pa_Sleep();
}

original_code:
int main( void )
{
 PaStreamParameters outputParameters;
 PaError pa_err;
 SF_INFO sfinfo;
 signal( 2, interrupt );
 gen_default_drums();
 pa_err = Pa_Initialize();
 outputParameters.device = Pa_GetDefaultOutputDevice();
 outputParameters.channelCount = 1;
 outputParameters.sampleFormat = ((PaSampleFormat) 0x00000001);
 outputParameters.suggestedLatency =
  Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
 outputParameters.hostApiSpecificStreamInfo = ((void *)0);
 pa_err = Pa_OpenStream( &stream, ((void *)0), &outputParameters, 44100,
  512, ((PaStreamFlags) 0x00000001), audio_callback, ((void *)0) );
 sfinfo.samplerate = 44100;
 sfinfo.channels = 1;
 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
 wave_output = sf_open( "wub.wav", SFM_WRITE, &sfinfo );
 srand( time( 0 ) );
 bass_z = 0.0f;
 bass_freq = midi_to_hz( 28 );
 pa_err = Pa_StartStream( stream );
 while( 1 )
  Pa_Sleep( 1000 );
 sf_close( wave_output );
 pa_err = Pa_StopStream( stream );
 pa_err = Pa_CloseStream( stream );
 Pa_Terminate();
 return 0;
}

canonical_original_code:
int main()
{
 struct PaStreamParameters outputParameters;
 int pa_err;
 struct SF_INFO sfinfo;
 signal( 2, interrupt );
 gen_default_drums();
 pa_err = Pa_Initialize();
 outputParameters.device = Pa_GetDefaultOutputDevice();
 outputParameters.channelCount = 1;
 outputParameters.sampleFormat = ((unsigned long) 0x00000001);
 outputParameters.suggestedLatency =
  Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
 outputParameters.hostApiSpecificStreamInfo = ((void *)0);
 pa_err = Pa_OpenStream( &stream, ((void *)0), &outputParameters, 44100,
  512, ((unsigned long) 0x00000001), audio_callback, ((void *)0) );
 sfinfo.samplerate = 44100;
 sfinfo.channels = 1;
 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
 wave_output = sf_open( "wub.wav", SFM_WRITE, &sfinfo );
 srand( time( 0 ) );
 bass_z = 0.0f;
 bass_freq = midi_to_hz( 28 );
 pa_err = Pa_StartStream( stream );
 while( 1 )
  Pa_Sleep( 1000 );
 sf_close( wave_output );
 pa_err = Pa_StopStream( stream );
 pa_err = Pa_CloseStream( stream );
 Pa_Terminate();
 return 0;
}

variable_types: struct PaStreamParameters { int device; int channelCount; unsigned long sampleFormat; double suggestedLatency; void *hostApiSpecificStreamInfo; }

variable_types: struct SF_INFO { long frames; int samplerate; int channels; int format; int sections; int seekable; }


interrupt

decompiled_code:
void __noreturn interrupt()
{
sf_close();
Pa_StopStream();
Pa_CloseStream();
Pa_Terminate();
puts("Successfully terminated.");
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
sf_close();
Pa_StopStream();
Pa_CloseStream();
Pa_Terminate();
puts("Successfully terminated.");
exit(0);
}

original_code:
void interrupt( int sig )
{
 (void) sig;
 PaError pa_err;
 sf_close( wave_output );
 pa_err = Pa_StopStream( stream );
 pa_err = Pa_CloseStream( stream );
 Pa_Terminate();
 printf( "Successfully terminated.\n" );
 exit( 0 );
}

canonical_original_code:
void interrupt(int sig)
{
 (void) sig;
 int pa_err;
 sf_close( wave_output );
 pa_err = Pa_StopStream( stream );
 pa_err = Pa_CloseStream( stream );
 Pa_Terminate();
 printf( "Successfully terminated.\n" );
 exit( 0 );
}

variable_types: sig int


decompiled_code:
float __fastcall midi_to_hz(int a1)
{
return powf(2.0, (float)(a1 - 69) / 12.0) * 440.0;
}

canonical_decompiled_code:
float __fastcall func8(int a1)
{
return powf(2.0, (float)(a1 - 69) / 12.0) * 440.0;
}

original_code:
float midi_to_hz( int note )
{
 return ( 440.0f * powf( 2.0f, (float)(note-69) / 12.0f ) );
}

canonical_original_code:
float midi_to_hz(int note)
{
 return ( 440.0f * powf( 2.0f, (float)(note-69) / 12.0f ) );
}

variable_types: note int

return_type: float


original_code:
static int audio_callback( const void *inputBuffer, void *outputBuffer,
       unsigned long framesPerBuffer,
       const PaStreamCallbackTimeInfo *timeInfo,
       PaStreamCallbackFlags statusFlags,
       void *userData )
{
 int i;
 float *out = (float*)outputBuffer;
 float b, d, f, mod;
 int beat;
 float v;
 for( i = 0; i < framesPerBuffer; i++ )
 {
  d = 0;
  beat = global_frame % ( 44100 / ( 140 / 60 ) / 4 );
  if( beat == 0 )
  {
   if( beat_count % 4 == 0 )
    bass_lfofreq = ((float)140/60.0f) *
     flt_freq[ rand()%4 ];
   if( rand()%20 == 0 )
   {
    bass_freq = midi_to_hz( 28 + minor_scale[ rand()%7 ] +
     ((rand()%3==0)?12:0) );
   }
   if( ( beat_count % 4 == 0 ) && ( rand()%3 == 0 ) )
   {
    bass_fmindex = (float)( rand()%999 + 1 );
   }
   if( ( beat_count % 4 == 0 ) && ( rand()%3 == 0 ) )
   {
    bass_fmindex = 0.0f;
   }
   if( rand()%5 == 0 )
   {
    bass_fmmod = (float)( rand()%3 + 1 );
   }
   if( beat_count % 16 == 0 )
    bd_time = 0;
   if( beat_count % 16 == 8 )
   {
    if( rand()%3 == 0 )
     bass_vol = 0.0f;
    sd_time = 0;
   }
   if( ( beat_count % 16 == 10 ) && ( rand()%4 == 0 ) )
    sd_time = 0;
   if( beat_count % 16 == 12 )
    bass_vol = 1.0f;
   if( beat_count % 16 == 6 )
    bd_time = 0;
   if( beat_count % 2 == 0 )
    hh_time = 0;
   beat_count++;
  }
  if( bd_time >= 0 )
  {
   d = bd[bd_time++];
   if( bd_time > 44100 )
    bd_time = -1;
  }
  if( sd_time >= 0 )
  {
   d = d * 0.8f + sd[sd_time++];
   if( sd_time > 44100 )
    sd_time = -1;
  }
  if( hh_time >= 0 )
  {
   d = d * 0.8f + hh[hh_time++] * 0.05f;
   if( hh_time > 44100 )
    hh_time = -1;
  }
  bass_lfoval = sin( 2.0 * 3.14159265358979323846 * bass_lfofreq * (float)global_frame /
   (float)44100 );
  bass_lfoval = bass_lfoval/100.0f + 0.99f;
  mod = sin( 2.0 * 3.14159265358979323846 * bass_fmmod * bass_freq * (float)global_frame /
   (float)44100 );
  b = (((sin( 2.0 * 3.14159265358979323846 * ( bass_freq * (float)global_frame / (float)44100 ) + mod * bass_fmindex ))<0)?-1.0f:1.0f);
  bass_z = bass_lfoval * bass_z + ( 1.0f - bass_lfoval ) * b;
  v = 0.3f;
  out[i] = bass_vol * bass_z * v + d * ( 1.0f-v );
  if( out[i] > 1.0f ) out[i] = 1.0f;
  if( out[i] < -1.0f ) out[i] = -1.0f;
  global_frame++;
 }
 sf_write_float( wave_output, out, framesPerBuffer );
 return paContinue;
}

canonical_original_code:
static int audio_callback(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, struct PaStreamCallbackTimeInfo *timeInfo, unsigned long statusFlags, void *userData)
{
 int i;
 float *out = (float*)outputBuffer;
 float b; float d; float f; float mod;
 int beat;
 float v;
 for( i = 0; i < framesPerBuffer; i++ )
 {
  d = 0;
  beat = global_frame % ( 44100 / ( 140 / 60 ) / 4 );
  if( beat == 0 )
  {
   if( beat_count % 4 == 0 )
    bass_lfofreq = ((float)140/60.0f) *
     flt_freq[ rand()%4 ];
   if( rand()%20 == 0 )
   {
    bass_freq = midi_to_hz( 28 + minor_scale[ rand()%7 ] +
     ((rand()%3==0)?12:0) );
   }
   if( ( beat_count % 4 == 0 ) && ( rand()%3 == 0 ) )
   {
    bass_fmindex = (float)( rand()%999 + 1 );
   }
   if( ( beat_count % 4 == 0 ) && ( rand()%3 == 0 ) )
   {
    bass_fmindex = 0.0f;
   }
   if( rand()%5 == 0 )
   {
    bass_fmmod = (float)( rand()%3 + 1 );
   }
   if( beat_count % 16 == 0 )
    bd_time = 0;
   if( beat_count % 16 == 8 )
   {
    if( rand()%3 == 0 )
     bass_vol = 0.0f;
    sd_time = 0;
   }
   if( ( beat_count % 16 == 10 ) && ( rand()%4 == 0 ) )
    sd_time = 0;
   if( beat_count % 16 == 12 )
    bass_vol = 1.0f;
   if( beat_count % 16 == 6 )
    bd_time = 0;
   if( beat_count % 2 == 0 )
    hh_time = 0;
   beat_count++;
  }
  if( bd_time >= 0 )
  {
   d = bd[bd_time++];
   if( bd_time > 44100 )
    bd_time = -1;
  }
  if( sd_time >= 0 )
  {
   d = d * 0.8f + sd[sd_time++];
   if( sd_time > 44100 )
    sd_time = -1;
  }
  if( hh_time >= 0 )
  {
   d = d * 0.8f + hh[hh_time++] * 0.05f;
   if( hh_time > 44100 )
    hh_time = -1;
  }
  bass_lfoval = sin( 2.0 * 3.14159265358979323846 * bass_lfofreq * (float)global_frame /
   (float)44100 );
  bass_lfoval = bass_lfoval/100.0f + 0.99f;
  mod = sin( 2.0 * 3.14159265358979323846 * bass_fmmod * bass_freq * (float)global_frame /
   (float)44100 );
  b = (((sin( 2.0 * 3.14159265358979323846 * ( bass_freq * (float)global_frame / (float)44100 ) + mod * bass_fmindex ))<0)?-1.0f:1.0f);
  bass_z = bass_lfoval * bass_z + ( 1.0f - bass_lfoval ) * b;
  v = 0.3f;
  out[i] = bass_vol * bass_z * v + d * ( 1.0f-v );
  if( out[i] > 1.0f ) out[i] = 1.0f;
  if( out[i] < -1.0f ) out[i] = -1.0f;
  global_frame++;
 }
 sf_write_float( wave_output, out, framesPerBuffer );
 return paContinue;
}

variable_types:
inputBuffer
void *

variable_types:
outputBuffer
void *

variable_types:
framesPerBuffer
unsigned long

variable_types: struct PaStreamCallbackTimeInfo { double inputBufferAdcTime; double currentTime; double outputBufferDacTime; } *

variable_types:
statusFlags
unsigned long

variable_types:
userData
void *

variable_types: i int

variable_types:
out
float *

variable_types: b float

variable_types: d float

variable_types: f float

variable_types: mod float

variable_types: beat int

variable_types: v float


original_code:
int gen_drum( float *dest, int nframes,
    float amp, float decay,
    float freq, float freqdecay,
    float noise, float noisedecay, float noisefilter )
{
 int i;
 float _noise = noise, _amp = amp, _decay = decay, _freq = freq;
 float f = 0;
 for( i = 0; i < nframes; i++ )
 {
  dest[i] = _amp * sin( 2.0 * 3.14159265358979323846 *_freq * (double)i /
   (double)44100 );
  f = (noisefilter) * f + (1.0f-noisefilter) * _noise * (
   (float)( rand()%100 ) / 100.0f - 0.5f );
  dest[i] += f;
  _noise *= noisedecay;
  _amp *= decay;
  _freq -= freqdecay;
  if( _noise < .0 ) noise = .0;
  if( _amp < .0 ) amp = .0;
  if( _freq < .0 ) freq = .0;
  if( dest[i] < -1.0 ) dest[i] = -1.0;
  if( dest[i] > 1.0 ) dest[i] = 1.0;
 }
 return 0;
}

canonical_original_code:
int gen_drum(float *dest, int nframes, float amp, float decay, float freq, float freqdecay, float noise, float noisedecay, float noisefilter)
{
 int i;
 float _noise = noise; float _amp = amp; float _decay = decay; float _freq = freq;
 float f = 0;
 for( i = 0; i < nframes; i++ )
 {
  dest[i] = _amp * sin( 2.0 * 3.14159265358979323846 *_freq * (double)i /
   (double)44100 );
  f = (noisefilter) * f + (1.0f-noisefilter) * _noise * (
   (float)( rand()%100 ) / 100.0f - 0.5f );
  dest[i] += f;
  _noise *= noisedecay;
  _amp *= decay;
  _freq -= freqdecay;
  if( _noise < .0 ) noise = .0;
  if( _amp < .0 ) amp = .0;
  if( _freq < .0 ) freq = .0;
  if( dest[i] < -1.0 ) dest[i] = -1.0;
  if( dest[i] > 1.0 ) dest[i] = 1.0;
 }
 return 0;
}

variable_types:
dest
float *

variable_types: nframes int

variable_types: amp float

variable_types: decay float

variable_types: freq float

variable_types: freqdecay float

variable_types: noise float

variable_types: noisedecay float

variable_types: noisefilter float


repo: devio/lpcflash

variable_types:
out
unsigned char *

variable_types:
binlen
unsigned int

variable_types:
in
unsigned char *

variable_types:
b
unsigned int

variable_types: incnt int

variable_types: outcnt int

variable_types: modcnt int

return_type: unsigned int


variable_types:
set
unsigned char *


original_code:
unsigned int raw_uu_decode( unsigned char *in, unsigned char *out)
{
   unsigned int binlen = 0;
   unsigned char o1=0;
   unsigned char o2=0;
   unsigned char o3=0;
   unsigned int i=0;
   unsigned b=0;
   binlen = (*(in++) - 32) & 0xFF;
   for(i=0;i<60;i+=4) {
      o1=o2=o3=0;
      o1 =((in[i+0]-32)<<2);
      o1|=((in[i+1]-32)>>4) & 0x03;
      o2 =((in[i+1]-32)<<4) & 0xf0;
      o2|=((in[i+2]-32)>>2) & 0x0f;
      o3 =((in[i+2]-32)<<6) & 0xC0;
      o3|=((in[i+3]-32)) & 0x3F;
      out[b++] = o1;
      out[b++] = o2;
      out[b++] = o3;
      if (b >= binlen) {
         b=binlen;
         break;
      }
   }
   return b;
}

canonical_original_code:
unsigned int raw_uu_decode(unsigned char *in, unsigned char *out)
{
   unsigned int binlen = 0;
   unsigned char o1 = 0;
   unsigned char o2 = 0;
   unsigned char o3 = 0;
   unsigned int i = 0;
   unsigned int b = 0;
   binlen = (*(in++) - 32) & 0xFF;
   for(i=0;i<60;i+=4) {
      o1=o2=o3=0;
      o1 =((in[i+0]-32)<<2);
      o1|=((in[i+1]-32)>>4) & 0x03;
      o2 =((in[i+1]-32)<<4) & 0xf0;
      o2|=((in[i+2]-32)>>2) & 0x0f;
      o3 =((in[i+2]-32)<<6) & 0xC0;
      o3|=((in[i+3]-32)) & 0x3F;
      out[b++] = o1;
      out[b++] = o2;
      out[b++] = o3;
      if (b >= binlen) {
         b=binlen;
         break;
      }
   }
   return b;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall serial_readline(void *buf, int a2, int a3)
{
__int64 v4; // rbp
char *v5; // rbx
ssize_t v6; // rax
int v7; // eax
unsigned int v8; // er12

v4 = a2;
memset(buf, 0, a2);
if ( !a2 )
{
v8 = 0;
LABEL_17:
msg(0LL, "serial_readline", "short line");
return v8;
}
v5 = (char *)buf;
do
{
while ( 1 )
{
v6 = read(a3, v5, 1uLL);
if ( v6 != -1 )
break;
v7 = *_errno_location();
if ( v7 != 11 && v7 != 4 )
err(1, "read(): ");
msg(0LL, "serial_readline", "read interrupted");
}
if ( !v6 )
errx(1, "unexpected EOF read");
v5 += v6;
v4 -= v6;
}
while ( *(v5 - 1) != 10 && v4 );
v8 = (_DWORD)v5 - (_DWORD)buf;
if ( v5 - (_BYTE *)buf <= 2 )
goto LABEL_17;
if ( *(v5 - 2) != 13 || *(v5 - 1) != 10 )
msg(0LL, "serial_readline", "no CR NL at end of line%s");
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *buf, int a2, int a3)
{
__int64 v4; // rbp
char *v5; // rbx
ssize_t v6; // rax
int v7; // eax
unsigned int v8; // er12

v4 = a2;
memset(buf, 0, a2);
if ( !a2 )
{
v8 = 0;
LABEL_17:
msg(0LL, "serial_readline", "short line");
return v8;
}
v5 = (char *)buf;
do
{
while ( 1 )
{
v6 = read(a3, v5, 1uLL);
if ( v6 != -1 )
break;
v7 = *_errno_location();
if ( v7 != 11 && v7 != 4 )
err(1, "read(): ");
msg(0LL, "serial_readline", "read interrupted");
}
if ( !v6 )
errx(1, "unexpected EOF read");
v5 += v6;
v4 -= v6;
}
while ( *(v5 - 1) != 10 && v4 );
v8 = (_DWORD)v5 - (_DWORD)buf;
if ( v5 - (_BYTE *)buf <= 2 )
goto LABEL_17;
if ( *(v5 - 2) != 13 || *(v5 - 1) != 10 )
msg(0LL, "serial_readline", "no CR NL at end of line%s");
return v8;
}

original_code:
int serial_readline(char *buf, int len, int serial_fd)
{
   char *p = buf;
   size_t remaining = len;
   ssize_t nread;
   memset(buf, 0, remaining);
   while(remaining>0) {
      nread = read(serial_fd, p, 1);
      switch (nread) {
         case -1:
            if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4) {
             msg(0, __FUNCTION__, "read interrupted");
             continue;
          }
          err(1, "read(): ");
            break;
       case 0:
          errx(1, "unexpected EOF read");
            break;
      }
      remaining -= nread;
      p += nread;
      if (p[-1] == '\n')
       break;
   }
   if(p-buf <= 2)
      msg(0, __FUNCTION__, "short line");
   else if (p[-2] != '\r' || p[-1] != '\n')
      msg(0, __FUNCTION__, "no CR NL at end of line%s", remaining ? "" : " (buffer too small?)");
   return p - buf;
}

canonical_original_code:
int serial_readline(char *buf, int len, int serial_fd)
{
   char *p = buf;
   unsigned long remaining = len;
   long nread;
   memset(buf, 0, remaining);
   while(remaining>0) {
      nread = read(serial_fd, p, 1);
      switch (nread) {
         case -1:
            if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4) {
             msg(0, __FUNCTION__, "read interrupted");
             continue;
          }
          err(1, "read(): ");
            break;
       case 0:
          errx(1, "unexpected EOF read");
            break;
      }
      remaining -= nread;
      p += nread;
      if (p[-1] == '\n')
       break;
   }
   if(p-buf <= 2)
      msg(0, __FUNCTION__, "short line");
   else if (p[-2] != '\r' || p[-1] != '\n')
      msg(0, __FUNCTION__, "no CR NL at end of line%s", remaining ? "" : " (buffer too small?)");
   return p - buf;
}

variable_types:
buf
char *

variable_types: len int

variable_types:
p
char *

variable_types:
remaining
unsigned long

variable_types: nread long


decompiled_code:
__int64 __fastcall serial_send(int fd, int a2, char *a3)
{
size_t v3; // rbp
int v4; // er15
char *v5; // r12
int *v6; // r13
ssize_t v7; // rax
ssize_t v8; // rbx

v3 = a2;
if ( !a2 )
return 0LL;
v4 = (int)a3;
v5 = a3;
v6 = _errno_location();
do
{
while ( 1 )
{
*v6 = 0;
v7 = write(fd, v5, v3);
v8 = v7;
if ( v7 != -1 )
break;
if ( *v6 != 11 && *v6 != 4 )
err(1, "write(): ");
msg(0LL, "serial_send", "write interrupted");
}
if ( !v7 )
warn("write(): ");
v5 += v8;
v3 -= v8;
}
while ( v3 );
return (unsigned int)((_DWORD)v5 - v4);
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, int a2, char *a3)
{
size_t v3; // rbp
int v4; // er15
char *v5; // r12
int *v6; // r13
ssize_t v7; // rax
ssize_t v8; // rbx

v3 = a2;
if ( !a2 )
return 0LL;
v4 = (int)a3;
v5 = a3;
v6 = _errno_location();
do
{
while ( 1 )
{
*v6 = 0;
v7 = write(fd, v5, v3);
v8 = v7;
if ( v7 != -1 )
break;
if ( *v6 != 11 && *v6 != 4 )
err(1, "write(): ");
msg(0LL, "serial_send", "write interrupted");
}
if ( !v7 )
warn("write(): ");
v5 += v8;
v3 -= v8;
}
while ( v3 );
return (unsigned int)((_DWORD)v5 - v4);
}

original_code:
int serial_send(int serial_fd, int len, char *data)
{
   ssize_t nwritten;
   char *p = data;
   size_t remaining = len;
   while (remaining > 0) {
      (*__errno_location ()) = 0;
      nwritten = write(serial_fd, (unsigned char *)p, remaining);
      switch (nwritten) {
       case -1:
          if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4) {
             msg(0, __FUNCTION__, "write interrupted");
             continue;
          }
          err(1,"write(): ");
            break;
       case 0:
          warn("write(): ");
            break;
      }
      p += nwritten;
      remaining -= nwritten;
   }
   return p - data;
}

canonical_original_code:
int serial_send(int serial_fd, int len, char *data)
{
   long nwritten;
   char *p = data;
   unsigned long remaining = len;
   while (remaining > 0) {
      (*__errno_location ()) = 0;
      nwritten = write(serial_fd, (unsigned char *)p, remaining);
      switch (nwritten) {
       case -1:
          if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4) {
             msg(0, __FUNCTION__, "write interrupted");
             continue;
          }
          err(1,"write(): ");
            break;
       case 0:
          warn("write(): ");
            break;
      }
      p += nwritten;
      remaining -= nwritten;
   }
   return p - data;
}

variable_types:
data
char *

variable_types: nwritten long


decompiled_code:
__int64 __fastcall serial_open(const char *a1, int a2)
{
int v2; // eax
unsigned int v3; // er13
speed_t v4; // ebp
struct termios v6; // [rsp+0h] [rbp-58h] BYREF

v2 = open(a1, 2);
if ( v2 < 0 )
err(1, "serial_open: open(): ");
v3 = v2;
if ( tcgetattr(v2, &v6) < 0 )
err(1, "tcgetattr(): ");
v6.c_oflag &= 0xFFFFFF82;
v6.c_iflag = v6.c_iflag & 0xFFFFF604 | 0x1404;
v6.c_cflag = v6.c_cflag & 0xFFFF7806 | 0x802;
if ( a2 == 38400 )
{
v4 = 15;
}
else
{
if ( a2 > 38400 )
{
v4 = 4097;
if ( a2 == 57600 )
goto LABEL_7;
if ( a2 == 115200 )
{
v4 = 4098;
goto LABEL_7;
}
goto LABEL_15;
}
v4 = 13;
if ( a2 != 9600 )
{
v4 = 14;
if ( a2 != 19200 )
{
v4 = 12;
if ( a2 != 4800 )
{
LABEL_15:
v4 = 13;
msg(1LL, "serial_open", "invalid baud rate (%d)\n", (unsigned int)a2);
goto LABEL_7;
}
}
}
}
LABEL_7:
if ( cfsetispeed(&v6, v4) < 0 )
err(1, "cfsetispeed(): ");
if ( cfsetospeed(&v6, v4) < 0 )
err(1, "cfsetospeed(): ");
if ( tcsetattr(v3, 0, &v6) < 0 )
err(1, "tcsetattr(): ");
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, int a2)
{
int v2; // eax
unsigned int v3; // er13
speed_t v4; // ebp
struct termios v6; // [rsp+0h] [rbp-58h] BYREF

v2 = open(a1, 2);
if ( v2 < 0 )
err(1, "serial_open: open(): ");
v3 = v2;
if ( tcgetattr(v2, &v6) < 0 )
err(1, "tcgetattr(): ");
v6.c_oflag &= 0xFFFFFF82;
v6.c_iflag = v6.c_iflag & 0xFFFFF604 | 0x1404;
v6.c_cflag = v6.c_cflag & 0xFFFF7806 | 0x802;
if ( a2 == 38400 )
{
v4 = 15;
}
else
{
if ( a2 > 38400 )
{
v4 = 4097;
if ( a2 == 57600 )
goto LABEL_7;
if ( a2 == 115200 )
{
v4 = 4098;
goto LABEL_7;
}
goto LABEL_15;
}
v4 = 13;
if ( a2 != 9600 )
{
v4 = 14;
if ( a2 != 19200 )
{
v4 = 12;
if ( a2 != 4800 )
{
LABEL_15:
v4 = 13;
msg(1LL, "serial_open", "invalid baud rate (%d)\n", (unsigned int)a2);
goto LABEL_7;
}
}
}
}
LABEL_7:
if ( cfsetispeed(&v6, v4) < 0 )
err(1, "cfsetispeed(): ");
if ( cfsetospeed(&v6, v4) < 0 )
err(1, "cfsetospeed(): ");
if ( tcsetattr(v3, 0, &v6) < 0 )
err(1, "tcsetattr(): ");
return v3;
}

variable_types:
device
char *

variable_types: baudrate int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types:
speed
unsigned int

variable_types: fd int