decompiled_code:
__int64 __fastcall iterate_hash_next(_QWORD *a1)
{
__int64 *i; // rax
__int64 *v2; // rax
__int64 v3; // rdx
__int64 v4; // rdx
__int64 result; // rax

if ( !a1 )
_assert_fail("iter != NULL", "hash_table.c", 0xD2u, "iterate_hash_next");
if ( !a1[1] )
return 0LL;
for ( i = (__int64 *)a1[3]; !i; a1[3] = i )
{
v2 = (__int64 *)a1[2];
if ( !v2 )
return 0LL;
v3 = *v2;
a1[1] = v2;
a1[2] = v3;
i = *(__int64 **)(*a1 + 8LL * *((int *)v2 + 2));
}
v4 = *i;
result = i[1];
a1[3] = v4;
return result;
}

original_code:
hash_entry *
iterate_hash_next( hash_iterator *iter ) {
  ((void) sizeof ((iter != ((void *)0)) ? 1 : 0), __extension__ ({ if (iter != ((void *)0)) ; else __assert_fail ("iter != NULL", "hash_table.c", 210, __extension__ __PRETTY_FUNCTION__); }));
  for ( ;; ) {
    if ( iter->bucket_index == ((void *)0) ) {
      return ((void *)0);
    }
    list_element *e = iter->element;
    if ( e == ((void *)0) ) {
      if ( iter->next_bucket_index != ((void *)0) ) {
        iter->bucket_index = iter->next_bucket_index;
        iter->next_bucket_index = iter->next_bucket_index->next;
        iter->element = iter->buckets[ ( int ) ( unsigned long ) iter->bucket_index->data ];
      }
      else {
        return ((void *)0);
      }
    }
    else {
      iter->element = e->next;
      return e->data;
    }
  }
}

canonical_original_code:
struct hash_entry *iterate_hash_next(struct hash_iterator *iter) {
  ((void) sizeof ((iter != ((void *)0)) ? 1 : 0), __extension__ ({ if (iter != ((void *)0)) ; else __assert_fail ("iter != NULL", "hash_table.c", 210, __extension__ __PRETTY_FUNCTION__); }));
  for ( ;; ) {
    if ( iter->bucket_index == ((void *)0) ) {
      return ((void *)0);
    }
    struct list_element *e = iter->element;
    if ( e == ((void *)0) ) {
      if ( iter->next_bucket_index != ((void *)0) ) {
        iter->bucket_index = iter->next_bucket_index;
        iter->next_bucket_index = iter->next_bucket_index->next;
        iter->element = iter->buckets[ ( int ) ( unsigned long ) iter->bucket_index->data ];
      }
      else {
        return ((void *)0);
      }
    }
    else {
      iter->element = e->next;
      return e->data;
    }
  }
}

variable_types: struct hash_iterator { struct list_element { struct list_element *next; void *data; } **buckets; struct list_element *bucket_index; struct list_element *next_bucket_index; struct list_element *element; } *

return_type: struct hash_entry { void *key; void *value; } *

user_defined_types: struct hash_iterator { struct list_element **buckets; struct list_element *bucket_index; struct list_element *next_bucket_index; struct list_element *element; }


decompiled_code:
void __fastcall delete_hash(void *ptr)
{
unsigned int v1; // ebx
_QWORD *v2; // rbp
_QWORD *v3; // rax

if ( !ptr )
_assert_fail("table != NULL", "hash_table.c", 0xEEu, "delete_hash");
if ( *((_DWORD *)ptr + 6) && *(_DWORD *)ptr )
{
v1 = 0;
do
{
v2 = *(_QWORD **)(*((_QWORD *)ptr + 4) + 8LL * v1);
if ( v2 )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free((void *)v3[1]);
}
while ( v2 );
v2 = *(_QWORD **)(*((_QWORD *)ptr + 4) + 8LL * v1);
}
if ( !(unsigned __int8)delete_list(v2) )
_assert_fail("list_deleted == true", "hash_table.c", 0xFCu, "delete_hash");
++v1;
}
while ( *(_DWORD *)ptr > v1 );
}
free(*((void **)ptr + 4));
if ( !(unsigned __int8)delete_list(*((_QWORD *)ptr + 5)) )
_assert_fail("list_deleted == true", "hash_table.c", 0x102u, "delete_hash");
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
unsigned int v1; // ebx
_QWORD *v2; // rbp
_QWORD *v3; // rax

if ( !ptr )
_assert_fail("table != NULL", "hash_table.c", 0xEEu, "delete_hash");
if ( *((_DWORD *)ptr + 6) && *(_DWORD *)ptr )
{
v1 = 0;
do
{
v2 = *(_QWORD **)(*((_QWORD *)ptr + 4) + 8LL * v1);
if ( v2 )
{
do
{
v3 = v2;
v2 = (_QWORD *)*v2;
free((void *)v3[1]);
}
while ( v2 );
v2 = *(_QWORD **)(*((_QWORD *)ptr + 4) + 8LL * v1);
}
if ( !(unsigned __int8)delete_list(v2) )
_assert_fail("list_deleted == true", "hash_table.c", 0xFCu, "delete_hash");
++v1;
}
while ( *(_DWORD *)ptr > v1 );
}
free(*((void **)ptr + 4));
if ( !(unsigned __int8)delete_list(*((_QWORD *)ptr + 5)) )
_assert_fail("list_deleted == true", "hash_table.c", 0x102u, "delete_hash");
free(ptr);
}

original_code:
void
delete_hash( hash_table *table ) {
  ((void) sizeof ((table != ((void *)0)) ? 1 : 0), __extension__ ({ if (table != ((void *)0)) ; else __assert_fail ("table != NULL", "hash_table.c", 238, __extension__ __PRETTY_FUNCTION__); }));
  _Bool list_deleted = 0;
  if ( table->length > 0 ) {
    unsigned int i;
    for ( i = 0; i < table->number_of_buckets; i++ ) {
      list_element *e = ((void *)0);
      for ( e = table->buckets[ i ]; e != ((void *)0); ) {
        list_element *delete_me = e;
        e = e->next;
        free( delete_me->data );
      }
      list_deleted = delete_list( table->buckets[ i ] );
      ((void) sizeof ((list_deleted == 1) ? 1 : 0), __extension__ ({ if (list_deleted == 1) ; else __assert_fail ("list_deleted == true", "hash_table.c", 252, __extension__ __PRETTY_FUNCTION__); }));
    }
  }
  free( table->buckets );
  list_deleted = delete_list( table->nonempty_bucket_index );
  ((void) sizeof ((list_deleted == 1) ? 1 : 0), __extension__ ({ if (list_deleted == 1) ; else __assert_fail ("list_deleted == true", "hash_table.c", 258, __extension__ __PRETTY_FUNCTION__); }));
  free( table );
}

canonical_original_code:
void delete_hash(struct hash_table *table) {
  ((void) sizeof ((table != ((void *)0)) ? 1 : 0), __extension__ ({ if (table != ((void *)0)) ; else __assert_fail ("table != NULL", "hash_table.c", 238, __extension__ __PRETTY_FUNCTION__); }));
  _Bool list_deleted = 0;
  if ( table->length > 0 ) {
    unsigned int i;
    for ( i = 0; i < table->number_of_buckets; i++ ) {
      struct list_element *e = ((void *)0);
      for ( e = table->buckets[ i ]; e != ((void *)0); ) {
        struct list_element *delete_me = e;
        e = e->next;
        free( delete_me->data );
      }
      list_deleted = delete_list( table->buckets[ i ] );
      ((void) sizeof ((list_deleted == 1) ? 1 : 0), __extension__ ({ if (list_deleted == 1) ; else __assert_fail ("list_deleted == true", "hash_table.c", 252, __extension__ __PRETTY_FUNCTION__); }));
    }
  }
  free( table->buckets );
  list_deleted = delete_list( table->nonempty_bucket_index );
  ((void) sizeof ((list_deleted == 1) ? 1 : 0), __extension__ ({ if (list_deleted == 1) ; else __assert_fail ("list_deleted == true", "hash_table.c", 258, __extension__ __PRETTY_FUNCTION__); }));
  free( table );
}

variable_types: struct hash_table { unsigned int number_of_buckets; _Bool (*compare)(void *x, void *y); unsigned int (*hash)(void *key); unsigned int length; struct list_element { struct list_element *next; void *data; } **buckets; struct list_element *nonempty_bucket_index; } *

variable_types:
i
unsigned int

return_type: void

user_defined_types: struct hash_table { unsigned int number_of_buckets; _Bool (*compare)(void *x, void *y); unsigned int (*hash)(void *key); unsigned int length; struct list_element **buckets; struct list_element *nonempty_bucket_index; }


plus

decompiled_code:
// attributes: thunk
__int64 plus()
{
return create_sum();
}

canonical_decompiled_code:
__int64 func0()
{
return create_sum();
}

original_code:
Expression *
plus( const Expression *augend, const Expression *addend ) {
  return create_sum( augend, addend );
}

canonical_original_code:
struct Expression *plus(struct Expression *augend, struct Expression *addend) {
  return create_sum( augend, addend );
}

variable_types:
augend
struct Expression *

variable_types:
addend
struct Expression *

return_type: struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *

user_defined_types: struct Expression { void *value; struct Money *(*reduce)(struct Expression *exp, enum Currency to); }


multiply

decompiled_code:
// attributes: thunk
__int64 multiply()
{
return create_mul();
}

canonical_decompiled_code:
__int64 func1()
{
return create_mul();
}

original_code:
Expression *
multiply( const Expression *multiplicand, unsigned int multiplier ) {
  return create_mul( multiplicand, multiplier );
}

canonical_original_code:
struct Expression *multiply(struct Expression *multiplicand, unsigned int multiplier) {
  return create_mul( multiplicand, multiplier );
}

variable_types:
multiplicand
struct Expression *

variable_types:
multiplier
unsigned int


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

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

original_code:
Currency
currency_of( const Money *money ) {
  return ( ( MoneyProtected * ) money )->currency;
}

canonical_original_code:
enum Currency currency_of(struct Money *money) {
  return ( ( struct MoneyProtected * ) money )->currency;
}

variable_types:
money
struct Money {  } *

return_type: enum Currency { USD=0, CHF=1 }

user_defined_types: struct MoneyProtected { unsigned int amount; enum Currency currency; }


equal

decompiled_code:
bool __fastcall equal(_DWORD *a1, _DWORD *a2)
{
bool result; // al

result = 0;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

canonical_decompiled_code:
bool __fastcall func3(_DWORD *a1, _DWORD *a2)
{
bool result; // al

result = 0;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

original_code:
_Bool
equal( const void *money, const void *other ) {
  MoneyProtected *moneyp = ( MoneyProtected * ) money;
  MoneyProtected *otherp = ( MoneyProtected * ) other;
  return moneyp->amount == otherp->amount && moneyp->currency == otherp->currency;
}

canonical_original_code:
_Bool equal(void *money, void *other) {
  struct MoneyProtected *moneyp = ( MoneyProtected * ) money;
  struct MoneyProtected *otherp = ( MoneyProtected * ) other;
  return moneyp->amount == otherp->amount && moneyp->currency == otherp->currency;
}

variable_types:
money
void *

variable_types:
other
void *

variable_types: struct MoneyProtected { unsigned int amount; enum Currency { USD=0, CHF=1 } currency; } *


decompiled_code:
__int64 __fastcall money_from(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
Money *
money_from( const Expression *exp ) {
  return ( Money * ) exp->value;
}

canonical_original_code:
struct Money *money_from(struct Expression *exp) {
  return ( struct Money * ) exp->value;
}

variable_types: struct Expression { void *value; struct Money *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *

return_type: struct Money {  } *


decompiled_code:
void __fastcall free_expression(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

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

original_code:
void
free_expression( Expression *exp ) {
  free( exp->value );
  free( exp );
}

canonical_original_code:
void free_expression(struct Expression *exp) {
  free( exp->value );
  free( exp );
}

variable_types: struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *


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

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

original_code:
void
free_money( Money *money ) {
  free( ( MoneyProtected * ) money );
}

canonical_original_code:
void free_money(struct Money *money) {
  free( ( struct MoneyProtected * ) money );
}


decompiled_code:
__int64 __fastcall create_list(_QWORD *a1)
{
if ( !a1 )
_assert_fail("list != NULL", "linked_list.c", 0x1Cu, "create_list");
*a1 = 0LL;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
if ( !a1 )
_assert_fail("list != NULL", "linked_list.c", 0x1Cu, "create_list");
*a1 = 0LL;
return 1LL;
}

original_code:
_Bool
create_list( list_element **list ) {
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "linked_list.c", 28, __extension__ __PRETTY_FUNCTION__); }));
  *list = ((void *)0);
  return 1;
}

canonical_original_code:
_Bool create_list(struct list_element **list) {
  ((void) sizeof ((list != ((void *)0)) ? 1 : 0), __extension__ ({ if (list != ((void *)0)) ; else __assert_fail ("list != NULL", "linked_list.c", 28, __extension__ __PRETTY_FUNCTION__); }));
  *list = ((void *)0);
  return 1;
}

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


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

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x25u, "insert_in_front");
v2 = *a1;
v3 = malloc(0x10uLL);
v3[1] = a2;
*a1 = (__int64)v3;
*v3 = v2;
return 1LL;
}

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

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x25u, "insert_in_front");
v2 = *a1;
v3 = malloc(0x10uLL);
v3[1] = a2;
*a1 = (__int64)v3;
*v3 = v2;
return 1LL;
}

original_code:
_Bool
insert_in_front( list_element **head, void *data ) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  list_element *old_head = *head;
  list_element *new_head = malloc( sizeof( list_element ) );
  new_head->data = data;
  *head = new_head;
  ( *head )->next = old_head;
  return 1;
}

canonical_original_code:
_Bool insert_in_front(struct list_element **head, void *data) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  struct list_element *old_head = *head;
  struct list_element *new_head = malloc( sizeof( list_element ) );
  new_head->data = data;
  *head = new_head;
  ( *head )->next = old_head;
  return 1;
}

variable_types:
data
void *

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


decompiled_code:
__int64 __fastcall insert_before(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rbx
_QWORD *v5; // rbp
_QWORD *v7; // rax

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x33u, "insert_before");
v3 = (_QWORD *)*a1;
do
{
v5 = v3;
v3 = (_QWORD *)*v3;
if ( !v3 )
return 0LL;
}
while ( v3[1] != a2 );
v7 = malloc(0x10uLL);
*v7 = v3;
v7[1] = a3;
*v5 = v7;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v3; // rbx
_QWORD *v5; // rbp
_QWORD *v7; // rax

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x33u, "insert_before");
v3 = (_QWORD *)*a1;
do
{
v5 = v3;
v3 = (_QWORD *)*v3;
if ( !v3 )
return 0LL;
}
while ( v3[1] != a2 );
v7 = malloc(0x10uLL);
*v7 = v3;
v7[1] = a3;
*v5 = v7;
return 1LL;
}

original_code:
_Bool
insert_before( list_element **head, const void *sibling, void *data ) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  list_element *e;
  list_element *new_element;
  for ( e = *head; e->next != ((void *)0); e = e->next ) {
    if ( e->next->data == sibling ) {
      new_element = malloc( sizeof( list_element ) );
      new_element->next = e->next;
      new_element->data = data;
      e->next = new_element;
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
_Bool insert_before(struct list_element **head, void *sibling, void *data) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 51, __extension__ __PRETTY_FUNCTION__); }));
  struct list_element *e;
  struct list_element *new_element;
  for ( e = *head; e->next != ((void *)0); e = e->next ) {
    if ( e->next->data == sibling ) {
      new_element = malloc( sizeof( list_element ) );
      new_element->next = e->next;
      new_element->data = data;
      e->next = new_element;
      return 1;
    }
  }
  return 0;
}

variable_types:
sibling
void *

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


decompiled_code:
__int64 __fastcall append_to_tail(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rcx
_QWORD *v4; // rax
_QWORD *v5; // rdx
__int64 result; // rax

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x48u, "append_to_tail");
v2 = malloc(0x10uLL);
v2[1] = a2;
v3 = v2;
*v2 = 0LL;
v4 = *a1;
if ( *a1 )
{
do
{
v5 = v4;
v4 = (_QWORD *)*v4;
}
while ( v4 );
*v5 = v3;
result = 1LL;
}
else
{
*a1 = v3;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rcx
_QWORD *v4; // rax
_QWORD *v5; // rdx
__int64 result; // rax

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x48u, "append_to_tail");
v2 = malloc(0x10uLL);
v2[1] = a2;
v3 = v2;
*v2 = 0LL;
v4 = *a1;
if ( *a1 )
{
do
{
v5 = v4;
v4 = (_QWORD *)*v4;
}
while ( v4 );
*v5 = v3;
result = 1LL;
}
else
{
*a1 = v3;
result = 1LL;
}
return result;
}

original_code:
_Bool
append_to_tail( list_element **head, void *data ) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  list_element *e;
  list_element *new_tail = malloc( sizeof( list_element ) );
  new_tail->data = data;
  new_tail->next = ((void *)0);
  if ( *head == ((void *)0) ) {
    *head = new_tail;
    return 1;
  }
  for ( e = *head; e->next != ((void *)0); e = e->next );
  e->next = new_tail;
  return 1;
}

canonical_original_code:
_Bool append_to_tail(struct list_element **head, void *data) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 72, __extension__ __PRETTY_FUNCTION__); }));
  struct list_element *e;
  struct list_element *new_tail = malloc( sizeof( list_element ) );
  new_tail->data = data;
  new_tail->next = ((void *)0);
  if ( *head == ((void *)0) ) {
    *head = new_tail;
    return 1;
  }
  for ( e = *head; e->next != ((void *)0); e = e->next );
  e->next = new_tail;
  return 1;
}


decompiled_code:
__int64 __fastcall list_length_of(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

if ( !a1 )
return 0LL;
v1 = *a1;
for ( i = 1; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD **a1)
{
_QWORD *v1; // rax
unsigned int i; // er8

if ( !a1 )
return 0LL;
v1 = *a1;
for ( i = 1; v1; ++i )
v1 = (_QWORD *)*v1;
return i;
}

original_code:
unsigned int
list_length_of( const list_element *head ) {
  if ( head == ((void *)0) ) {
    return 0;
  }
  unsigned int length = 1;
  for ( list_element *e = head->next; e; e = e->next, length++ );
  return length;
}

canonical_original_code:
unsigned int list_length_of(struct list_element *head) {
  if ( head == ((void *)0) ) {
    return 0;
  }
  unsigned int length = 1;
  for ( struct list_element *e = head->next; e; e = e->next, length++ );
  return length;
}

variable_types:
length
unsigned int

return_type: unsigned int


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

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x68u, "delete_element");
v2 = a1;
v3 = *a1;
if ( v3[1] == a2 )
{
LABEL_7:
*v2 = *v3;
free(v3);
result = 1LL;
}
else
{
while ( 1 )
{
v2 = v3;
v3 = (_QWORD *)*v3;
if ( !v3 )
break;
if ( v3[1] == a2 )
goto LABEL_7;
}
result = 0LL;
}
return result;
}

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

if ( !a1 )
_assert_fail("head != NULL", "linked_list.c", 0x68u, "delete_element");
v2 = a1;
v3 = *a1;
if ( v3[1] == a2 )
{
LABEL_7:
*v2 = *v3;
free(v3);
result = 1LL;
}
else
{
while ( 1 )
{
v2 = v3;
v3 = (_QWORD *)*v3;
if ( !v3 )
break;
if ( v3[1] == a2 )
goto LABEL_7;
}
result = 0LL;
}
return result;
}

original_code:
_Bool
delete_element( list_element **head, const void *data ) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 104, __extension__ __PRETTY_FUNCTION__); }));
  list_element *e = *head;
  list_element *delete_me;
  if ( e->data == data ) {
    *head = e->next;
    free( e );
    return 1;
  }
  for ( ; e->next != ((void *)0); e = e->next ) {
    if ( e->next->data == data ) {
      delete_me = e->next;
      e->next = e->next->next;
      free( delete_me );
      return 1;
    }
  }
  return 0;
}

canonical_original_code:
_Bool delete_element(struct list_element **head, void *data) {
  ((void) sizeof ((head != ((void *)0)) ? 1 : 0), __extension__ ({ if (head != ((void *)0)) ; else __assert_fail ("head != NULL", "linked_list.c", 104, __extension__ __PRETTY_FUNCTION__); }));
  struct list_element *e = *head;
  struct list_element *delete_me;
  if ( e->data == data ) {
    *head = e->next;
    free( e );
    return 1;
  }
  for ( ; e->next != ((void *)0); e = e->next ) {
    if ( e->next->data == data ) {
      delete_me = e->next;
      e->next = e->next->next;
      free( delete_me );
      return 1;
    }
  }
  return 0;
}


decompiled_code:
__int64 __fastcall delete_list(void *ptr)
{
_QWORD *v1; // rbx
void *v2; // rdi

if ( !ptr )
return 1LL;
v1 = ptr;
do
{
v2 = v1;
v1 = (_QWORD *)*v1;
free(v2);
}
while ( v1 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *ptr)
{
_QWORD *v1; // rbx
void *v2; // rdi

if ( !ptr )
return 1LL;
v1 = ptr;
do
{
v2 = v1;
v1 = (_QWORD *)*v1;
free(v2);
}
while ( v1 );
return 1LL;
}

original_code:
_Bool
delete_list( list_element *head ) {
  list_element *e;
  list_element *delete_me;
  for ( e = head; e != ((void *)0); ) {
    delete_me = e;
    e = e->next;
    free( delete_me );
  }
  return 1;
}

canonical_original_code:
_Bool delete_list(struct list_element *head) {
  struct list_element *e;
  struct list_element *delete_me;
  for ( e = head; e != ((void *)0); ) {
    delete_me = e;
    e = e->next;
    free( delete_me );
  }
  return 1;
}


decompiled_code:
__int64 __fastcall reduce_sum(_QWORD **a1, unsigned int a2)
{
__int64 v2; // r12
int *v3; // r13
_DWORD *v4; // rax
int v5; // ebx
int v6; // er14
_DWORD *v7; // r12

v2 = (*a1)[1];
v3 = (int *)reduce(**a1);
v4 = (_DWORD *)reduce(v2);
v5 = *v3;
v6 = *v4;
v7 = v4;
free_money(v3);
free_money(v7);
return create_money((unsigned int)(v5 + v6), a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD **a1, unsigned int a2)
{
__int64 v2; // r12
int *v3; // r13
_DWORD *v4; // rax
int v5; // ebx
int v6; // er14
_DWORD *v7; // r12

v2 = (*a1)[1];
v3 = (int *)reduce(**a1);
v4 = (_DWORD *)reduce(v2);
v5 = *v3;
v6 = *v4;
v7 = v4;
free_money(v3);
free_money(v7);
return create_money((unsigned int)(v5 + v6), a2);
}

original_code:
static Money *
reduce_sum( const Expression *exp, Currency to ){
  Sum *sum = exp->value;
  Expression *augend_exp = ( Expression * ) sum->augend;
  Expression *addend_exp = ( Expression * ) sum->addend;
  MoneyProtected *reduced_augend = ( MoneyProtected * ) reduce( augend_exp, to );
  MoneyProtected *reduced_addend = ( MoneyProtected * ) reduce( addend_exp, to );
  unsigned int augend = reduced_augend->amount;
  unsigned int addend = reduced_addend->amount;
  free_money( ( Money * ) reduced_augend );
  free_money( ( Money * ) reduced_addend );
  return create_money( augend + addend, to );
}

canonical_original_code:
static struct Money *reduce_sum(struct Expression *exp, enum Currency to){
  struct Sum *sum = exp->value;
  struct Expression *augend_exp = ( Expression * ) sum->augend;
  struct Expression *addend_exp = ( Expression * ) sum->addend;
  struct MoneyProtected *reduced_augend = ( MoneyProtected * ) reduce( augend_exp, to );
  struct MoneyProtected *reduced_addend = ( MoneyProtected * ) reduce( addend_exp, to );
  unsigned int augend = reduced_augend->amount;
  unsigned int addend = reduced_addend->amount;
  free_money( ( struct Money * ) reduced_augend );
  free_money( ( struct Money * ) reduced_addend );
  return create_money( augend + addend, to );
}

variable_types:
to
enum Currency { USD=0, CHF=1 }

variable_types: struct Sum { struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *augend; struct Expression *addend; } *

variable_types:
augend
unsigned int

variable_types:
addend
unsigned int

user_defined_types: struct Sum { struct Expression *augend; struct Expression *addend; }


decompiled_code:
__int64 hash_pair()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
unsigned int
hash_pair( const void *key ) {
  return 0;
}

canonical_original_code:
unsigned int hash_pair(void *key) {
  return 0;
}

variable_types:
key
void *


decompiled_code:
bool __fastcall compare_pair(_DWORD *a1, _DWORD *a2)
{
bool result; // al

result = 0;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

canonical_decompiled_code:
bool __fastcall func1(_DWORD *a1, _DWORD *a2)
{
bool result; // al

result = 0;
if ( *a1 == *a2 )
result = a1[1] == a2[1];
return result;
}

original_code:
_Bool
compare_pair( const void *x, const void *y ) {
  const Pair *px = x;
  const Pair *py = y;
  return px->from == py->from && px->to == py->to;
}

canonical_original_code:
_Bool compare_pair(void *x, void *y) {
  struct Pair *px = x;
  struct Pair *py = y;
  return px->from == py->from && px->to == py->to;
}

variable_types:
x
void *

variable_types:
y
void *

variable_types: struct Pair { enum Currency { USD=0, CHF=1 } from; enum Currency { USD=0, CHF=1 } to; } *

user_defined_types: struct Pair { enum Currency from; enum Currency to; }


decompiled_code:
__int64 __fastcall reduce_mul(_QWORD **a1, unsigned int a2)
{
_QWORD *v2; // r12
int *v3; // rax
int v4; // ebx

v2 = *a1;
v3 = (int *)reduce(**a1);
LODWORD(v2) = *((_DWORD *)v2 + 2);
v4 = *v3;
free_money(v3);
return create_money((unsigned int)((_DWORD)v2 * v4), a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD **a1, unsigned int a2)
{
_QWORD *v2; // r12
int *v3; // rax
int v4; // ebx

v2 = *a1;
v3 = (int *)reduce(**a1);
LODWORD(v2) = *((_DWORD *)v2 + 2);
v4 = *v3;
free_money(v3);
return create_money((unsigned int)((_DWORD)v2 * v4), a2);
}

original_code:
static Money *
reduce_mul( const Expression *exp, Currency to ){
  Mul *mul = exp->value;
  Expression *multiplicand_exp = ( Expression * ) mul->multiplicand ;
  MoneyProtected *reduced_multiplicand = ( MoneyProtected * ) reduce( multiplicand_exp, to );
  unsigned int multiplicand = reduced_multiplicand->amount;
  unsigned int multiplier = mul->multiplier;
  free_money( (Money * ) reduced_multiplicand );
  return create_money( multiplicand * multiplier, to );
}

canonical_original_code:
static struct Money *reduce_mul(struct Expression *exp, enum Currency to){
  struct Mul *mul = exp->value;
  struct Expression *multiplicand_exp = ( Expression * ) mul->multiplicand;
  struct MoneyProtected *reduced_multiplicand = ( MoneyProtected * ) reduce( multiplicand_exp, to );
  unsigned int multiplicand = reduced_multiplicand->amount;
  unsigned int multiplier = mul->multiplier;
  free_money( (struct Money * ) reduced_multiplicand );
  return create_money( multiplicand * multiplier, to );
}

variable_types: struct Mul { struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *multiplicand; unsigned int multiplier; } *

variable_types:
multiplicand
unsigned int

variable_types:
multiplier
unsigned int

user_defined_types: struct Mul { struct Expression *multiplicand; unsigned int multiplier; }


repo: yatsukhnenko/redis-proxy

decompiled_code:
int *__fastcall rp_socket_init(int *a1)
{
int *v1; // r12
int v2; // eax
int *result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = a1;
optval[0] = 1;
v2 = socket(2, 2049, 0);
*a1 = v2;
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9LL, v5);
result = 0LL;
}
else
{
if ( setsockopt(v2, 1, 9, optval, 4u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24LL, v7);
v1 = 0LL;
close(*a1);
}
result = v1;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func0(int *a1)
{
int *v1; // r12
int v2; // eax
int *result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = a1;
optval[0] = 1;
v2 = socket(2, 2049, 0);
*a1 = v2;
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9LL, v5);
result = 0LL;
}
else
{
if ( setsockopt(v2, 1, 9, optval, 4u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24LL, v7);
v1 = 0LL;
close(*a1);
}
result = v1;
}
return result;
}

original_code:
int *rp_socket_init(int *sockfd)
{
    int yes = 1;
    if((*sockfd = socket(2, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) {
        syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9, strerror((*__errno_location ())));
        return ((void *)0);
    }
    if(setsockopt(*sockfd, 1, 9, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24, strerror((*__errno_location ())));
        close(*sockfd);
        return ((void *)0);
    }
    return sockfd;
}

variable_types:
sockfd
int *

variable_types: yes int

return_type: int *


decompiled_code:
int *__fastcall rp_listen(int *a1, int a2, __int16 a3)
{
int *result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int fd; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
result = rp_socket_init(&fd);
if ( result )
{
if ( setsockopt(fd, 1, 2, &optval, 4u) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40LL, v6);
}
else
{
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = a2;
*(_WORD *)addr.sa_data = a3;
if ( bind(fd, &addr, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48LL, v8);
}
else
{
if ( listen(fd, 128) >= 0 )
{
*a1 = fd;
return a1;
}
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53LL, v10);
}
}
close(fd);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func1(int *a1, int a2, __int16 a3)
{
int *result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int fd; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

optval = 1;
result = func0(&fd);
if ( result )
{
if ( setsockopt(fd, 1, 2, &optval, 4u) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40LL, v6);
}
else
{
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = a2;
*(_WORD *)addr.sa_data = a3;
if ( bind(fd, &addr, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48LL, v8);
}
else
{
if ( listen(fd, 128) >= 0 )
{
*a1 = fd;
return a1;
}
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53LL, v10);
}
}
close(fd);
result = 0LL;
}
return result;
}

original_code:
int *rp_listen(int *sockfd, in_addr_t address, in_port_t port)
{
    int s, yes = 1;
    struct sockaddr_in addr;
    if(rp_socket_init(&s) == ((void *)0)) {
        return ((void *)0);
    }
    if(setsockopt(s, 1, 2, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    addr.sin_family = 2;
    addr.sin_addr.s_addr = address;
    addr.sin_port = port;
    if(bind(s, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
        syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    if(listen(s, 128) < 0) {
        syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    *sockfd = s;
    return sockfd;
}

canonical_original_code:
int *rp_listen(int *sockfd, unsigned int address, unsigned short port)
{
    int s; int yes = 1;
    struct sockaddr_in addr;
    if(rp_socket_init(&s) == ((void *)0)) {
        return ((void *)0);
    }
    if(setsockopt(s, 1, 2, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    addr.sin_family = 2;
    addr.sin_addr.s_addr = address;
    addr.sin_port = port;
    if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    if(listen(s, 128) < 0) {
        syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    *sockfd = s;
    return sockfd;
}

variable_types:
address
unsigned int

variable_types:
port
unsigned short

variable_types: s int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


original_code:
int rp_recv(int sockfd, rp_buffer_t *buffer)
{
    ssize_t i;
    for(;;) {
        if((int)buffer->used + 4096 > buffer->s.length) {
            if(rp_buffer_resize(buffer, 4096) != 1) {
                break;
            }
        }
        if((i = recv(sockfd, &buffer->s.data[buffer->used],
            buffer->s.length - buffer->used - 1, MSG_DONTWAIT)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->used += i;
    }
    buffer->s.data[buffer->used] = '\0';
    return 1;
}

canonical_original_code:
int rp_recv(int sockfd, struct rp_buffer_t *buffer)
{
    long i;
    for(;;) {
        if((int)buffer->used + 4096 > buffer->s.length) {
            if(rp_buffer_resize(buffer, 4096) != 1) {
                break;
            }
        }
        if((i = recv(sockfd, &buffer->s.data[buffer->used],
            buffer->s.length - buffer->used - 1, MSG_DONTWAIT)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->used += i;
    }
    buffer->s.data[buffer->used] = '\0';
    return 1;
}

variable_types: sockfd int

variable_types: struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } *

variable_types: i long

return_type: int

user_defined_types: struct rp_buffer_t { unsigned long used; struct rp_string_t s; unsigned int r; unsigned int w; }


decompiled_code:
__int64 __fastcall rp_send(int fd, __int64 *a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
ssize_t v4; // rax
unsigned int v5; // er12

v2 = *((_DWORD *)a2 + 7);
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
goto LABEL_7;
while ( 1 )
{
v4 = send(fd, (const void *)(a2[2] + v2), v3 - v2, 16448);
if ( v4 <= 0 )
break;
v3 = *a2;
v2 = *((_DWORD *)a2 + 7) + v4;
*((_DWORD *)a2 + 7) = v2;
if ( v2 >= (unsigned int)v3 )
goto LABEL_7;
}
v5 = -1;
if ( v4 )
{
if ( *_errno_location() == 11 )
LABEL_7:
v5 = 1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 *a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
ssize_t v4; // rax
unsigned int v5; // er12

v2 = *((_DWORD *)a2 + 7);
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
goto LABEL_7;
while ( 1 )
{
v4 = send(fd, (const void *)(a2[2] + v2), v3 - v2, 16448);
if ( v4 <= 0 )
break;
v3 = *a2;
v2 = *((_DWORD *)a2 + 7) + v4;
*((_DWORD *)a2 + 7) = v2;
if ( v2 >= (unsigned int)v3 )
goto LABEL_7;
}
v5 = -1;
if ( v4 )
{
if ( *_errno_location() == 11 )
LABEL_7:
v5 = 1;
}
return v5;
}

original_code:
int rp_send(int sockfd, rp_buffer_t *buffer)
{
    ssize_t i;
    while(buffer->w < (int)buffer->used) {
        if((i = send(sockfd, &buffer->s.data[buffer->w],
            buffer->used - buffer->w, MSG_DONTWAIT | MSG_NOSIGNAL)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->w += i;
    }
    return 1;
}

canonical_original_code:
int rp_send(int sockfd, struct rp_buffer_t *buffer)
{
    long i;
    while(buffer->w < (int)buffer->used) {
        if((i = send(sockfd, &buffer->s.data[buffer->w],
            buffer->used - buffer->w, MSG_DONTWAIT | MSG_NOSIGNAL)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->w += i;
    }
    return 1;
}

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


decompiled_code:
__int64 __fastcall rp_event_handler_init(__int64 a1)
{
__int64 v1; // r12
void *v2; // rax
int *v4; // rax
char *v5; // rax

v1 = a1;
*(_QWORD *)(a1 + 8) = 4096LL;
v2 = calloc(0x1000uLL, 0x10uLL);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 )
{
if ( !rp_epoll_init(a1) )
{
v1 = 0LL;
free(*(void **)(a1 + 16));
}
}
else
{
v4 = _errno_location();
v1 = 0LL;
v5 = strerror(*v4);
syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19LL, v5);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // r12
void *v2; // rax
int *v4; // rax
char *v5; // rax

v1 = a1;
*(_QWORD *)(a1 + 8) = 4096LL;
v2 = calloc(0x1000uLL, 0x10uLL);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 )
{
if ( !rp_epoll_init(a1) )
{
v1 = 0LL;
free(*(void **)(a1 + 16));
}
}
else
{
v4 = _errno_location();
v1 = 0LL;
v5 = strerror(*v4);
syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19LL, v5);
}
return v1;
}

original_code:
rp_event_handler_t *rp_event_handler_init(rp_event_handler_t *eh)
{
    eh->maxevents = 4096;
    if((eh->events = calloc(eh->maxevents, sizeof(rp_event_t))) == ((void *)0)) {
        syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19, strerror((*__errno_location ())));
        return ((void *)0);
    }
    memset(eh->events, 0, eh->maxevents * sizeof(rp_event_t));
    if(rp_epoll_init(eh) == ((void *)0)) {
        free(eh->events);
        return ((void *)0);
    }
    return eh;
}

canonical_original_code:
struct rp_event_handler *rp_event_handler_init(struct rp_event_handler *eh)
{
    eh->maxevents = 4096;
    if((eh->events = calloc(eh->maxevents, sizeof(rp_event_t))) == ((void *)0)) {
        syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19, strerror((*__errno_location ())));
        return ((void *)0);
    }
    memset(eh->events, 0, eh->maxevents * sizeof(rp_event_t));
    if(rp_epoll_init(eh) == ((void *)0)) {
        free(eh->events);
        return ((void *)0);
    }
    return eh;
}

variable_types:
eh
struct rp_event_handler *

return_type: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t { short events; void *data; } *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval { long tv_sec; long tv_usec; } *timeout); } *

user_defined_types: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval *timeout); }


decompiled_code:
__int64 __fastcall rp_epoll_add(int **a1, int a2, __int16 *a3)
{
__int16 v5; // ax
int *v6; // rdi
int *v7; // rdx
__int64 v8; // rbx
__int16 v9; // cx
uint32_t v10; // edx
uint32_t v11; // edx
__int64 result; // rax
int v13; // edi
__int64 v14; // rbx
int *v15; // rax
char *v16; // rax
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v5 = *a3;
v6 = *a1;
event.events = 0;
if ( (v5 & 1) != 0 )
{
v7 = a1[2];
event.events = 1;
v8 = 4LL * a2;
v9 = v7[v8];
if ( (v9 & 1) == 0 )
{
v11 = 25;
if ( (v5 & 2) == 0 )
goto LABEL_11;
v10 = 5;
goto LABEL_6;
}
v5 &= 0xFFFEu;
*a3 = v5;
}
if ( (v5 & 2) != 0 )
{
v8 = 4LL * a2;
v9 = a1[2][v8];
v10 = event.events | 4;
LABEL_6:
event.events = v10;
v11 = v10 | 0x18;
if ( (v9 & 2) == 0 )
goto LABEL_11;
v5 &= 0xFFFDu;
*a3 = v5;
}
if ( !v5 )
return 1LL;
v8 = 4LL * a2;
v9 = a1[2][v8];
v11 = event.events | 0x18;
LABEL_11:
v13 = *v6;
event.events = v11;
event.data.fd = a2;
if ( epoll_ctl(v13, v9 == 0 ? 1 : 3, a2, &event) )
{
v15 = _errno_location();
v16 = strerror(*v15);
syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59LL, v16);
result = 0xFFFFFFFFLL;
}
else
{
v14 = (__int64)&a1[2][v8];
*(_WORD *)v14 |= *a3;
*(_QWORD *)(v14 + 8) = *((_QWORD *)a3 + 1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int **a1, int a2, __int16 *a3)
{
__int16 v5; // ax
int *v6; // rdi
int *v7; // rdx
__int64 v8; // rbx
__int16 v9; // cx
uint32_t v10; // edx
uint32_t v11; // edx
__int64 result; // rax
int v13; // edi
__int64 v14; // rbx
int *v15; // rax
char *v16; // rax
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v5 = *a3;
v6 = *a1;
event.events = 0;
if ( (v5 & 1) != 0 )
{
v7 = a1[2];
event.events = 1;
v8 = 4LL * a2;
v9 = v7[v8];
if ( (v9 & 1) == 0 )
{
v11 = 25;
if ( (v5 & 2) == 0 )
goto LABEL_11;
v10 = 5;
goto LABEL_6;
}
v5 &= 0xFFFEu;
*a3 = v5;
}
if ( (v5 & 2) != 0 )
{
v8 = 4LL * a2;
v9 = a1[2][v8];
v10 = event.events | 4;
LABEL_6:
event.events = v10;
v11 = v10 | 0x18;
if ( (v9 & 2) == 0 )
goto LABEL_11;
v5 &= 0xFFFDu;
*a3 = v5;
}
if ( !v5 )
return 1LL;
v8 = 4LL * a2;
v9 = a1[2][v8];
v11 = event.events | 0x18;
LABEL_11:
v13 = *v6;
event.events = v11;
event.data.fd = a2;
if ( epoll_ctl(v13, v9 == 0 ? 1 : 3, a2, &event) )
{
v15 = _errno_location();
v16 = strerror(*v15);
syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59LL, v16);
result = 0xFFFFFFFFLL;
}
else
{
v14 = (__int64)&a1[2][v8];
*(_WORD *)v14 |= *a3;
*(_QWORD *)(v14 + 8) = *((_QWORD *)a3 + 1);
result = 1LL;
}
return result;
}

original_code:
int rp_epoll_add(rp_event_handler_t *eh, int sockfd, rp_event_t *e)
{
    int op;
    struct epoll_event event;
    rp_epoll_data_t *ed = eh->data;
    event.events = 0;
    if(e->events & 1) {
        event.events |= EPOLLIN;
        if(eh->events[sockfd].events & 1) {
            e->events &= ~1;
        }
    }
    if(e->events & 2) {
        event.events |= EPOLLOUT;
        if(eh->events[sockfd].events & 2) {
            e->events &= ~2;
        }
    }
    if(e->events) {
        event.data.fd = sockfd;
        event.events |= EPOLLERR | EPOLLHUP;
        op = !eh->events[sockfd].events ? 1 : 3;
        if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
            syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59, strerror((*__errno_location ())));
            return -1;
        }
        eh->events[sockfd].events |= e->events;
        eh->events[sockfd].data = e->data;
    }
    return 1;
}

canonical_original_code:
int rp_epoll_add(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e)
{
    int op;
    struct epoll_event event;
    struct rp_epoll_data_t *ed = eh->data;
    event.events = 0;
    if(e->events & 1) {
        event.events |= EPOLLIN;
        if(eh->events[sockfd].events & 1) {
            e->events &= ~1;
        }
    }
    if(e->events & 2) {
        event.events |= EPOLLOUT;
        if(eh->events[sockfd].events & 2) {
            e->events &= ~2;
        }
    }
    if(e->events) {
        event.data.fd = sockfd;
        event.events |= EPOLLERR | EPOLLHUP;
        op = !eh->events[sockfd].events ? 1 : 3;
        if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
            syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59, strerror((*__errno_location ())));
            return -1;
        }
        eh->events[sockfd].events |= e->events;
        eh->events[sockfd].data = e->data;
    }
    return 1;
}

variable_types: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t { short events; void *data; } *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval { long tv_sec; long tv_usec; } *timeout); } *

variable_types:
e
struct rp_event_t *

variable_types: op int

variable_types: struct rp_epoll_data_t { int epfd; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; } *

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct rp_epoll_data_t { int epfd; struct epoll_event *events; }


original_code:
int rp_epoll_del(rp_event_handler_t *eh, int sockfd, rp_event_t *e)
{
    int op;
    struct epoll_event event;
    rp_epoll_data_t *ed = eh->data;
    e->events &= eh->events[sockfd].events;
    if(e->events) {
        event.events = 0;
        if(eh->events[sockfd].events & 1) {
            if(e->events & 1) {
                e->events &= ~1;
            } else {
                e->events |= 1;
                event.events |= EPOLLIN;
            }
        } else {
            e->events &= ~1;
        }
        if(eh->events[sockfd].events & 2) {
            if(e->events & 2) {
                e->events &= ~2;
            } else {
                e->events |= 2;
                event.events |= EPOLLOUT;
            }
        } else {
            e->events &= ~2;
        }
        if(e->events != eh->events[sockfd].events) {
            event.data.fd = sockfd;
            event.events |= EPOLLERR | EPOLLHUP;
            op = !e->events ? 2 : 3;
            if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
                syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 102, strerror((*__errno_location ())));
                return -1;
            }
            eh->events[sockfd].events = e->events;
            if(!eh->events[sockfd].events) {
                eh->events[sockfd].data = ((void *)0);
            }
        }
    }
    return 1;
}

canonical_original_code:
int rp_epoll_del(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e)
{
    int op;
    struct epoll_event event;
    struct rp_epoll_data_t *ed = eh->data;
    e->events &= eh->events[sockfd].events;
    if(e->events) {
        event.events = 0;
        if(eh->events[sockfd].events & 1) {
            if(e->events & 1) {
                e->events &= ~1;
            } else {
                e->events |= 1;
                event.events |= EPOLLIN;
            }
        } else {
            e->events &= ~1;
        }
        if(eh->events[sockfd].events & 2) {
            if(e->events & 2) {
                e->events &= ~2;
            } else {
                e->events |= 2;
                event.events |= EPOLLOUT;
            }
        } else {
            e->events &= ~2;
        }
        if(e->events != eh->events[sockfd].events) {
            event.data.fd = sockfd;
            event.events |= EPOLLERR | EPOLLHUP;
            op = !e->events ? 2 : 3;
            if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
                syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 102, strerror((*__errno_location ())));
                return -1;
            }
            eh->events[sockfd].events = e->events;
            if(!eh->events[sockfd].events) {
                eh->events[sockfd].data = ((void *)0);
            }
        }
    }
    return 1;
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

user_defined_types: struct rp_event_t { short events; void *data; }

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


original_code:
void rp_epoll_wait(rp_event_handler_t *eh, struct timeval *timeout)
{
    int i, n;
    struct timeval tv;
    rp_connection_t *c;
    struct epoll_event *event;
    rp_epoll_data_t *ed = eh->data;
    i = timeout != ((void *)0) ? timeout->tv_sec * 1000 + timeout->tv_usec / 1000 : -1;
    if((n = epoll_wait(ed->epfd, ed->events, eh->maxevents, i)) < 0) {
        syslog(3, "epoll_wait at %s:%d - %s", "rp_epoll.c", 124, strerror((*__errno_location ())));
    } else if(n > 0) {
        gettimeofday(&tv, ((void *)0));
        for(i = 0; i < n; i++) {
            event = &ed->events[i];
            c = eh->events[event->data.fd].data;
            c->time = tv.tv_sec + tv.tv_usec / 1000000.0;
            if(!(event->events & (EPOLLERR | EPOLLHUP))) {
                if((!(event->events & EPOLLIN) || c->on.read(c, eh) == 1)
                    && (!(event->events & EPOLLOUT) || c->on.write(c, eh) == 1)) {
                    continue;
                }
            }
            c->on.close(c, eh);
        }
    }
}

canonical_original_code:
void rp_epoll_wait(struct rp_event_handler *eh, struct timeval *timeout)
{
    int i; int n;
    struct timeval tv;
    struct rp_connection *c;
    struct epoll_event *event;
    struct rp_epoll_data_t *ed = eh->data;
    i = timeout != ((void *)0) ? timeout->tv_sec * 1000 + timeout->tv_usec / 1000 : -1;
    if((n = epoll_wait(ed->epfd, ed->events, eh->maxevents, i)) < 0) {
        syslog(3, "epoll_wait at %s:%d - %s", "rp_epoll.c", 124, strerror((*__errno_location ())));
    } else if(n > 0) {
        gettimeofday(&tv, ((void *)0));
        for(i = 0; i < n; i++) {
            event = &ed->events[i];
            c = eh->events[event->data.fd].data;
            c->time = tv.tv_sec + tv.tv_usec / 1000000.0;
            if(!(event->events & (EPOLLERR | EPOLLHUP))) {
                if((!(event->events & EPOLLIN) || c->on.read(c, eh) == 1)
                    && (!(event->events & EPOLLOUT) || c->on.write(c, eh) == 1)) {
                    continue;
                }
            }
            c->on.close(c, eh);
        }
    }
}

variable_types:
timeout
struct timeval *

variable_types: i int

variable_types: n int

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

variable_types: struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t { int length; char *data; } auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *

user_defined_types: struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; }


decompiled_code:
__int64 __fastcall rp_strstr(int *a1, char *a2)
{
unsigned __int64 v2; // r9
__int64 v4; // rcx
unsigned __int64 v5; // rax
char v6; // di
__int64 v7; // r8
char v8; // dl
char *v9; // rsi

v2 = *a1;
if ( *a1 )
{
v4 = *((_QWORD *)a1 + 1);
v5 = 0LL;
v6 = *a2;
do
{
while ( 1 )
{
v7 = v4 + v5;
if ( *(_BYTE *)(v4 + v5) == v6 )
break;
if ( ++v5 >= v2 )
return 0LL;
}
v8 = v6;
v9 = &a2[-v5];
do
{
if ( !v8 )
return v7;
v8 = v9[++v5];
}
while ( *(_BYTE *)(v4 + v5) == v8 );
if ( !v8 )
return v7;
}
while ( v5 < v2 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, char *a2)
{
unsigned __int64 v2; // r9
__int64 v4; // rcx
unsigned __int64 v5; // rax
char v6; // di
__int64 v7; // r8
char v8; // dl
char *v9; // rsi

v2 = *a1;
if ( *a1 )
{
v4 = *((_QWORD *)a1 + 1);
v5 = 0LL;
v6 = *a2;
do
{
while ( 1 )
{
v7 = v4 + v5;
if ( *(_BYTE *)(v4 + v5) == v6 )
break;
if ( ++v5 >= v2 )
return 0LL;
}
v8 = v6;
v9 = &a2[-v5];
do
{
if ( !v8 )
return v7;
v8 = v9[++v5];
}
while ( *(_BYTE *)(v4 + v5) == v8 );
if ( !v8 )
return v7;
}
while ( v5 < v2 );
}
return 0LL;
}

original_code:
char *rp_strstr(rp_string_t *s, const char *needle)
{
    char *ptr, *str;
    size_t i = 0;
    while(i < s->length) {
        if(s->data[i] != *needle) {
            i++;
            continue;
        }
        ptr = &s->data[i];
        str = (char *)needle;
        while(*str != '\0' && s->data[++i] == *++str);
        if(*str == '\0') {
            return ptr;
        }
    }
    return ((void *)0);
}

canonical_original_code:
char *rp_strstr(struct rp_string_t *s, char *needle)
{
    char *ptr; char *str;
    unsigned long i = 0;
    while(i < s->length) {
        if(s->data[i] != *needle) {
            i++;
            continue;
        }
        ptr = &s->data[i];
        str = (char *)needle;
        while(*str != '\0' && s->data[++i] == *++str);
        if(*str == '\0') {
            return ptr;
        }
    }
    return ((void *)0);
}

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

variable_types:
needle
char *

variable_types:
ptr
char *

variable_types:
str
char *

variable_types:
i
unsigned long

return_type: char *


original_code:
long int rp_strtol(rp_string_t *s)
{
    long i = 0;
    int sign = 1;
    while(s->length > 0) {
        if(*s->data == '-') {
            if(sign > 0) {
                sign = -1;
                s->data++;
                s->length--;
                continue;
            }
        } else if(*s->data >= '0' && *s->data <= '9') {
            i = i * 10 + *s->data - '0';
            s->data++;
            s->length--;
            continue;
        }
        break;
    }
    return i * sign;
}

canonical_original_code:
long rp_strtol(struct rp_string_t *s)
{
    long i = 0;
    int sign = 1;
    while(s->length > 0) {
        if(*s->data == '-') {
            if(sign > 0) {
                sign = -1;
                s->data++;
                s->length--;
                continue;
            }
        } else if(*s->data >= '0' && *s->data <= '9') {
            i = i * 10 + *s->data - '0';
            s->data++;
            s->length--;
            continue;
        }
        break;
    }
    return i * sign;
}

variable_types: i long

variable_types: sign int

return_type: long


decompiled_code:
__int64 __fastcall rp_buffer_resize(__int64 a1, int a2)
{
int v3; // eax
void *v4; // rdi
size_t v5; // rbx
void *v6; // rax
__int64 result; // rax
int *v8; // rax
char *v9; // rax

v3 = *(_DWORD *)(a1 + 8);
v4 = *(void **)(a1 + 16);
v5 = v3 + a2;
if ( v3 < 0 )
v5 = a2;
v6 = realloc(v4, v5);
if ( v6 )
{
*(_DWORD *)(a1 + 8) = v5;
*(_QWORD *)(a1 + 16) = v6;
result = 1LL;
}
else
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10LL, v9);
result = 0xFFFFFFFFLL;
}
return result;
}

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

v3 = *(_DWORD *)(a1 + 8);
v4 = *(void **)(a1 + 16);
v5 = v3 + a2;
if ( v3 < 0 )
v5 = a2;
v6 = realloc(v4, v5);
if ( v6 )
{
*(_DWORD *)(a1 + 8) = v5;
*(_QWORD *)(a1 + 16) = v6;
result = 1LL;
}
else
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10LL, v9);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int rp_buffer_resize(rp_buffer_t *buffer, int delta)
{
    char *data;
    size_t size;
    size = buffer->s.length < 0 ? delta : buffer->s.length + delta;
    if((data = realloc(buffer->s.data, size)) == ((void *)0)) {
        syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10, strerror((*__errno_location ())));
        return -1;
    }
    buffer->s.length = size;
    buffer->s.data = data;
    return 1;
}

canonical_original_code:
int rp_buffer_resize(struct rp_buffer_t *buffer, int delta)
{
    char *data;
    unsigned long size;
    size = buffer->s.length < 0 ? delta : buffer->s.length + delta;
    if((data = realloc(buffer->s.data, size)) == ((void *)0)) {
        syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10, strerror((*__errno_location ())));
        return -1;
    }
    buffer->s.length = size;
    buffer->s.data = data;
    return 1;
}

variable_types: struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } *

variable_types: delta int

variable_types:
data
char *

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall rp_buffer_append(__int64 a1, int *a2)
{
size_t v2; // rdx
__int64 result; // rax
__int64 v5; // rdi
int v6; // er8

v2 = *a2;
result = 1LL;
if ( (int)v2 > 0 )
{
v5 = *(_QWORD *)a1;
if ( (int)v2 + (int)v5 <= *(_DWORD *)(a1 + 8) )
{
LABEL_3:
memcpy((void *)(*(_QWORD *)(a1 + 16) + v5), *((const void **)a2 + 1), v2);
*(_QWORD *)a1 += *a2;
return 1LL;
}
v6 = rp_buffer_resize(a1, ((unsigned int)v2 & 0xFFFFF000) + 4096);
result = 0xFFFFFFFFLL;
if ( v6 == 1 )
{
v2 = *a2;
v5 = *(_QWORD *)a1;
goto LABEL_3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int *a2)
{
size_t v2; // rdx
__int64 result; // rax
__int64 v5; // rdi
int v6; // er8

v2 = *a2;
result = 1LL;
if ( (int)v2 > 0 )
{
v5 = *(_QWORD *)a1;
if ( (int)v2 + (int)v5 <= *(_DWORD *)(a1 + 8) )
{
LABEL_3:
memcpy((void *)(*(_QWORD *)(a1 + 16) + v5), *((const void **)a2 + 1), v2);
*(_QWORD *)a1 += *a2;
return 1LL;
}
v6 = func0(a1, ((unsigned int)v2 & 0xFFFFF000) + 4096);
result = 0xFFFFFFFFLL;
if ( v6 == 1 )
{
v2 = *a2;
v5 = *(_QWORD *)a1;
goto LABEL_3;
}
}
return result;
}

original_code:
int rp_buffer_append(rp_buffer_t *buffer, rp_string_t *s)
{
    if(s->length > 0) {
        if((int)buffer->used + s->length > buffer->s.length) {
            if(rp_buffer_resize(buffer, (s->length / 4096 + 1) * 4096) != 1) {
                return -1;
            }
        }
        memcpy(&buffer->s.data[buffer->used], s->data, s->length);
        buffer->used += s->length;
    }
    return 1;
}

canonical_original_code:
int rp_buffer_append(struct rp_buffer_t *buffer, struct rp_string_t *s)
{
    if(s->length > 0) {
        if((int)buffer->used + s->length > buffer->s.length) {
            if(rp_buffer_resize(buffer, (s->length / 4096 + 1) * 4096) != 1) {
                return -1;
            }
        }
        memcpy(&buffer->s.data[buffer->used], s->data, s->length);
        buffer->used += s->length;
    }
    return 1;
}

variable_types:
s
struct rp_string_t *


decompiled_code:
__int64 __fastcall rp_command_auth(__int64 a1, __int64 a2)
{
__int16 v2; // bp
__int64 v3; // rax
size_t v4; // rdx

v2 = *(_WORD *)(a2 + 16);
if ( (v2 & 0x40) == 0 )
{
v3 = *(_QWORD *)(a2 + 88);
if ( *(_DWORD *)(v3 + 32) != *(_DWORD *)(*(_QWORD *)(v3 + 72) + 12LL) )
return rp_sprintf(a1, "-ERR operation not permitted");
v4 = *(int *)(a2 + 40);
if ( (_DWORD)v4 != *(_DWORD *)(v3 + 40) || strncmp(*(const char **)(a2 + 48), *(const char **)(v3 + 48), v4) )
return rp_sprintf(a1, "-ERR operation not permitted");
}
*(_WORD *)(a2 + 16) = v2 | 0x40;
return rp_sprintf(a1, "+OK");
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int16 v2; // bp
__int64 v3; // rax
size_t v4; // rdx

v2 = *(_WORD *)(a2 + 16);
if ( (v2 & 0x40) == 0 )
{
v3 = *(_QWORD *)(a2 + 88);
if ( *(_DWORD *)(v3 + 32) != *(_DWORD *)(*(_QWORD *)(v3 + 72) + 12LL) )
return rp_sprintf(a1, "-ERR operation not permitted");
v4 = *(int *)(a2 + 40);
if ( (_DWORD)v4 != *(_DWORD *)(v3 + 40) || strncmp(*(const char **)(a2 + 48), *(const char **)(v3 + 48), v4) )
return rp_sprintf(a1, "-ERR operation not permitted");
}
*(_WORD *)(a2 + 16) = v2 | 0x40;
return rp_sprintf(a1, "+OK");
}

original_code:
rp_string_t *rp_command_auth(rp_string_t *s, void *data)
{
    rp_connection_t *c = data;
    rp_client_t *client = c->data;
    if(!(c->flags & 0x0040) && (
        client->cmd.argc != client->cmd.proto->argc ||
        c->settings.auth.length != client->cmd.argv.length ||
        strncmp(c->settings.auth.data, client->cmd.argv.data, c->settings.auth.length))
    ) {
        return rp_sprintf(s, "-ERR operation not permitted");
    }
    c->flags |= 0x0040;
    return rp_sprintf(s, "+OK");
}

canonical_original_code:
struct rp_string_t *rp_command_auth(struct rp_string_t *s, void *data)
{
    struct rp_connection *c = data;
    struct rp_client_t *client = c->data;
    if(!(c->flags & 0x0040) && (
        client->cmd.argc != client->cmd.proto->argc ||
        c->settings.auth.length != client->cmd.argv.length ||
        strncmp(c->settings.auth.data, client->cmd.argv.data, c->settings.auth.length))
    ) {
        return rp_sprintf(s, "-ERR operation not permitted");
    }
    c->flags |= 0x0040;
    return rp_sprintf(s, "+OK");
}

variable_types:
data
void *

variable_types: struct rp_client_t { struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } buffer; struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); } *proto; struct rp_string_t txt; } cmd; struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; } *server; } *

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

user_defined_types: struct rp_client_t { struct rp_buffer_t buffer; struct rp_command_t cmd; struct rp_connection *server; }

user_defined_types: struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t *proto; struct rp_string_t txt; }


decompiled_code:
__int64 __fastcall rp_command_ping(__int64 a1)
{
return rp_sprintf(a1, "+PONG");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return rp_sprintf(a1, "+PONG");
}

original_code:
rp_string_t *rp_command_ping(rp_string_t *s, void *data)
{
    return rp_sprintf(s, "+PONG");
}

canonical_original_code:
struct rp_string_t *rp_command_ping(struct rp_string_t *s, void *data)
{
    return rp_sprintf(s, "+PONG");
}


decompiled_code:
__int64 __fastcall rp_command_quit(__int64 a1, __int64 a2)
{
*(_WORD *)(a2 + 16) |= 0x200u;
return rp_sprintf(a1, "+OK");
}

original_code:
rp_string_t *rp_command_quit(rp_string_t *s, void *data)
{
    rp_connection_t *c = data;
    c->flags |= 0x0200;
    return rp_sprintf(s, "+OK");
}

canonical_original_code:
struct rp_string_t *rp_command_quit(struct rp_string_t *s, void *data)
{
    struct rp_connection *c = data;
    c->flags |= 0x0200;
    return rp_sprintf(s, "+OK");
}


decompiled_code:
__int64 __fastcall rp_command_lookup(int *a1)
{
__int64 v1; // r12
size_t v2; // rbx
const char *v3; // rsi
const char *v4; // rbp

v1 = 0LL;
v2 = *a1;
if ( (unsigned int)(v2 - 1) > 0xE )
return v1;
v1 = rp_commands[(int)v2 - 1];
if ( !v1 )
return v1;
v3 = *(const char **)v1;
if ( *(_QWORD *)v1 )
{
v4 = (const char *)*((_QWORD *)a1 + 1);
while ( strncasecmp(v4, v3, v2) )
{
v3 = *(const char **)(v1 + 24);
v1 += 24LL;
if ( !v3 )
return 0LL;
}
return v1;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1)
{
__int64 v1; // r12
size_t v2; // rbx
const char *v3; // rsi
const char *v4; // rbp

v1 = 0LL;
v2 = *a1;
if ( (unsigned int)(v2 - 1) > 0xE )
return v1;
v1 = rp_commands[(int)v2 - 1];
if ( !v1 )
return v1;
v3 = *(const char **)v1;
if ( *(_QWORD *)v1 )
{
v4 = (const char *)*((_QWORD *)a1 + 1);
while ( strncasecmp(v4, v3, v2) )
{
v3 = *(const char **)(v1 + 24);
v1 += 24LL;
if ( !v3 )
return 0LL;
}
return v1;
}
return 0LL;
}

original_code:
rp_command_proto_t *rp_command_lookup(rp_string_t *name)
{
    rp_command_proto_t *ptr;
    if(name->length > 0 && name->length < 16 &&
        (ptr = rp_commands[name->length - 1]) != ((void *)0)) {
        while(ptr->name != ((void *)0)) {
            if(strncasecmp(name->data, ptr->name, name->length) == 0) {
                return ptr;
            }
            ptr++;
        }
    }
    return ((void *)0);
}

canonical_original_code:
struct rp_command_proto_t *rp_command_lookup(struct rp_string_t *name)
{
    struct rp_command_proto_t *ptr;
    if(name->length > 0 && name->length < 16 &&
        (ptr = rp_commands[name->length - 1]) != ((void *)0)) {
        while(ptr->name != ((void *)0)) {
            if(strncasecmp(name->data, ptr->name, name->length) == 0) {
                return ptr;
            }
            ptr++;
        }
    }
    return ((void *)0);
}

variable_types:
name
struct rp_string_t *

variable_types: struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t { int length; char *data; } *(*handler)(struct rp_string_t *s, void *data); } *

user_defined_types: struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); }


original_code:
int rp_request_parse(rp_buffer_t *buffer, rp_command_t *cmd)
{
    int i;
    char *ptr;
    rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->txt.data = s.data;
            if(*s.data != '*') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) <= 0 || s.data != ptr) {
                return -1;
            }
            cmd->i = 0;
            cmd->argc = i;
            cmd->argv.data = ((void *)0);
            cmd->argv.length = -1 - 1;
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(!cmd->i) {
                    cmd->name.data = cmd->argv.data;
                    cmd->name.length = cmd->argv.length;
                }
                if(++cmd->i == cmd->argc) {
                    cmd->txt.length = ptr - cmd->txt.data + 2;
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

canonical_original_code:
int rp_request_parse(struct rp_buffer_t *buffer, struct rp_command_t *cmd)
{
    int i;
    char *ptr;
    struct rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->txt.data = s.data;
            if(*s.data != '*') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) <= 0 || s.data != ptr) {
                return -1;
            }
            cmd->i = 0;
            cmd->argc = i;
            cmd->argv.data = ((void *)0);
            cmd->argv.length = -1 - 1;
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(!cmd->i) {
                    cmd->name.data = cmd->argv.data;
                    cmd->name.length = cmd->argv.length;
                }
                if(++cmd->i == cmd->argc) {
                    cmd->txt.length = ptr - cmd->txt.data + 2;
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

variable_types: struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); } *proto; struct rp_string_t txt; } *

variable_types: i int

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


original_code:
int rp_reply_parse(rp_buffer_t *buffer, rp_command_t *cmd)
{
    int i;
    char *ptr;
    rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->i = 0;
            switch(*s.data) {
                case '*':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i <= 0) {
                        return 1;
                    }
                    cmd->argc = i;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = -1 - 1;
                    break;
                case '$':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i < 0) {
                        return 1;
                    }
                    cmd->argc = 1;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = i;
                    break;
                case ':':
                    s.data++;
                    s.length--;
                    i = rp_strtol(&s);
                    if(s.data != ptr) {
                        return -1;
                    }
                case '+':
                case '-':
                    return 1;
                default:
                    return -1;
            }
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(++cmd->i == cmd->argc) {
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

canonical_original_code:
int rp_reply_parse(struct rp_buffer_t *buffer, struct rp_command_t *cmd)
{
    int i;
    char *ptr;
    struct rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->i = 0;
            switch(*s.data) {
                case '*':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i <= 0) {
                        return 1;
                    }
                    cmd->argc = i;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = -1 - 1;
                    break;
                case '$':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i < 0) {
                        return 1;
                    }
                    cmd->argc = 1;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = i;
                    break;
                case ':':
                    s.data++;
                    s.length--;
                    i = rp_strtol(&s);
                    if(s.data != ptr) {
                        return -1;
                    }
                case '+':
                case '-':
                    return 1;
                default:
                    return -1;
            }
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(++cmd->i == cmd->argc) {
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall rp_queue_push(__int64 *a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax
__int64 v5; // rcx
int *v6; // rax
char *v7; // rax

v2 = malloc(0x18uLL);
if ( v2 )
{
v3 = *a1;
if ( *a1 )
{
v5 = a1[2];
v2[1] = v5;
*(_QWORD *)(v5 + 16) = v2;
}
else
{
v2[1] = 0LL;
a1[1] = (__int64)v2;
}
*v2 = a2;
*a1 = v3 + 1;
v2[2] = 0LL;
a1[2] = (__int64)v2;
result = 1LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8LL, v7);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax
__int64 v5; // rcx
int *v6; // rax
char *v7; // rax

v2 = malloc(0x18uLL);
if ( v2 )
{
v3 = *a1;
if ( *a1 )
{
v5 = a1[2];
v2[1] = v5;
*(_QWORD *)(v5 + 16) = v2;
}
else
{
v2[1] = 0LL;
a1[1] = (__int64)v2;
}
*v2 = a2;
*a1 = v3 + 1;
v2[2] = 0LL;
a1[2] = (__int64)v2;
result = 1LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8LL, v7);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int rp_queue_push(rp_queue_t *queue, void *data)
{
    rp_queue_element_t *qe;
    if((qe = malloc(sizeof(rp_queue_element_t))) == ((void *)0)) {
        syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8, strerror((*__errno_location ())));
        return -1;
    }
    if(queue->size) {
        qe->prev = queue->last;
        queue->last->next = qe;
    } else {
        qe->next = qe->prev = ((void *)0);
        queue->first = qe;
    }
    qe->data = data;
    qe->next = ((void *)0);
    queue->last = qe;
    queue->size++;
    return 1;
}

canonical_original_code:
int rp_queue_push(struct rp_queue_t *queue, void *data)
{
    struct rp_queue_element *qe;
    if((qe = malloc(sizeof(rp_queue_element_t))) == ((void *)0)) {
        syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8, strerror((*__errno_location ())));
        return -1;
    }
    if(queue->size) {
        qe->prev = queue->last;
        queue->last->next = qe;
    } else {
        qe->next = qe->prev = ((void *)0);
        queue->first = qe;
    }
    qe->data = data;
    qe->next = ((void *)0);
    queue->last = qe;
    queue->size++;
    return 1;
}

variable_types: struct rp_queue_t { unsigned long size; struct rp_queue_element { void *data; struct rp_queue_element *prev; struct rp_queue_element *next; } *first; struct rp_queue_element *last; } *

user_defined_types: struct rp_queue_t { unsigned long size; struct rp_queue_element *first; struct rp_queue_element *last; }


repo: yeahimfreaky/rash

decompiled_code:
void sig_handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void sig_handler(int sig)
{
 if( sig == 2 );
}

variable_types: sig int


decompiled_code:
int __fastcall builtin_cmd(_QWORD *a1)
{
_BYTE *v1; // rax
const char *v3; // rdi
int result; // eax
char *v5; // rax

v1 = (_BYTE *)*a1;
if ( *(_BYTE *)*a1 != 99 || v1[1] != 100 || v1[2] )
return 1;
v3 = (const char *)a1[1];
if ( !v3 )
{
v5 = getenv("HOME");
a1[1] = v5;
v3 = v5;
}
result = chdir(v3);
if ( result )
{
printf("rash: can't cd to %s\n", (const char *)a1[1]);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(_QWORD *a1)
{
_BYTE *v1; // rax
const char *v3; // rdi
int result; // eax
char *v5; // rax

v1 = (_BYTE *)*a1;
if ( *(_BYTE *)*a1 != 99 || v1[1] != 100 || v1[2] )
return 1;
v3 = (const char *)a1[1];
if ( !v3 )
{
v5 = getenv("HOME");
a1[1] = v5;
v3 = v5;
}
result = chdir(v3);
if ( result )
{
printf("rash: can't cd to %s\n", (const char *)a1[1]);
result = 0;
}
return result;
}

original_code:
int builtin_cmd(char **cmd)
{
 if( !strcmp("cd",cmd[0]) ) {
  if( cmd[1]==((void *)0) )
   cmd[1] = getenv("HOME");
  if( chdir(cmd[1]) )
   printf("rash: can't cd to %s\n", cmd[1]);
  return 0;
 }
 return 1;
}

variable_types:
cmd
char **


decompiled_code:
__int64 __fastcall execute_cmd(char **argv)
{
parseandredirectin(*argv);
execvp(*argv, argv);
perror("execute_cmd failed");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char **argv)
{
parseandredirectin(*argv);
execvp(*argv, argv);
perror("execute_cmd failed");
return 1LL;
}

original_code:
int execute_cmd(char **cmd)
{
 parseandredirectin(cmd[0]);
 execvp( cmd[0] , cmd );
 perror("execute_cmd failed");
 return 1;
}


decompiled_code:
__int64 input_loop()
{
time_t v0; // rax
const char *v1; // rbp
int v2; // ebx
char *v3; // rax
__int64 v4; // rbx
__int64 v5; // rbp
__pid_t v6; // eax

puts("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit");
printf("->) ");
while ( 1 )
{
if ( !fgets(linebuf, 255, stdin) )
return 0LL;
v0 = time(0LL);
v1 = linebuf;
rash_log_data = v0;
v2 = strlen(linebuf);
qword_A88 = (__int64)calloc(v2 + 1, 1uLL);
strncpy((char *)qword_A88, v1, v2);
addmsg(rash_log_data, qword_A88);
v3 = strtok_r(linebuf, delim, &tokp);
p = (__int64)v3;
if ( v3 )
{
v4 = 1LL;
do
{
v5 = (int)v4;
(&argvv)[v4++ - 1] = v3;
v3 = strtok_r(0LL, delim, &tokp);
p = (__int64)v3;
}
while ( v3 );
}
else
{
v5 = 0LL;
}
(&argvv)[v5] = 0LL;
if ( argvv )
{
if ( builtin_cmd(&argvv) )
break;
}
LABEL_12:
printf("->) ");
}
if ( !strcmp("exit", argvv) )
return 0LL;
v6 = fork();
if ( v6 == -1 )
{
perror("Failed to fork child");
goto LABEL_12;
}
if ( v6 )
{
wait(0LL);
goto LABEL_12;
}
if ( !(unsigned int)execute_cmd(&argvv) )
goto LABEL_12;
return 1LL;
}

canonical_decompiled_code:
__int64 func3()
{
time_t v0; // rax
const char *v1; // rbp
int v2; // ebx
char *v3; // rax
__int64 v4; // rbx
__int64 v5; // rbp
__pid_t v6; // eax

puts("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit");
printf("->) ");
while ( 1 )
{
if ( !fgets(linebuf, 255, stdin) )
return 0LL;
v0 = time(0LL);
v1 = linebuf;
rash_log_data = v0;
v2 = strlen(linebuf);
qword_A88 = (__int64)calloc(v2 + 1, 1uLL);
strncpy((char *)qword_A88, v1, v2);
addmsg(rash_log_data, qword_A88);
v3 = strtok_r(linebuf, delim, &tokp);
p = (__int64)v3;
if ( v3 )
{
v4 = 1LL;
do
{
v5 = (int)v4;
(&argvv)[v4++ - 1] = v3;
v3 = strtok_r(0LL, delim, &tokp);
p = (__int64)v3;
}
while ( v3 );
}
else
{
v5 = 0LL;
}
(&argvv)[v5] = 0LL;
if ( argvv )
{
if ( func1(&argvv) )
break;
}
LABEL_12:
printf("->) ");
}
if ( !strcmp("exit", argvv) )
return 0LL;
v6 = fork();
if ( v6 == -1 )
{
perror("Failed to fork child");
goto LABEL_12;
}
if ( v6 )
{
wait(0LL);
goto LABEL_12;
}
if ( !(unsigned int)func2(&argvv) )
goto LABEL_12;
return 1LL;
}

original_code:
int input_loop(void)
{
 int childpid, i, linelen;
 for( printf("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit\n")
     , printf("->) ")
     ; fgets( linebuf, 255, stdin ) != ((void *)0)
     ; printf("->) ") )
 {
  rash_log_data.time = time(((void *)0));
  linelen = strlen(linebuf);
  rash_log_data.string = calloc(linelen + 1, sizeof(char));
  strncpy(rash_log_data.string, linebuf, linelen);
  addmsg(rash_log_data);
  i = 0;
  for( p = strtok_r(linebuf, " \f\n\r\t\v", &tokp); p;
      p = strtok_r(((void *)0), " \f\n\r\t\v", &tokp) )
  {
   argvv[i++] = p;
  }
  argvv[i] = ((void *)0);
  if( argvv[0] != ((void *)0) ) {
   if( builtin_cmd(argvv) ) {
    if( !strcmp("exit",argvv[0]) )
     return 0;
    if( (childpid = fork()) == -1 )
     perror("Failed to fork child");
    else if( childpid == 0 ) {
     if( execute_cmd(argvv) )
      return 1;
    } else {
     wait(((void *)0));
    }
   }
  }
 }
 return 0;
}

canonical_original_code:
int input_loop()
{
 int childpid; int i; int linelen;
 for( printf("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit\n")
     , printf("->) ")
     ; fgets( linebuf, 255, stdin ) != ((void *)0)
     ; printf("->) ") )
 {
  rash_log_data.time = time(((void *)0));
  linelen = strlen(linebuf);
  rash_log_data.string = calloc(linelen + 1, sizeof(char));
  strncpy(rash_log_data.string, linebuf, linelen);
  addmsg(rash_log_data);
  i = 0;
  for( p = strtok_r(linebuf, " \f\n\r\t\v", &tokp); p;
      p = strtok_r(((void *)0), " \f\n\r\t\v", &tokp) )
  {
   argvv[i++] = p;
  }
  argvv[i] = ((void *)0);
  if( argvv[0] != ((void *)0) ) {
   if( builtin_cmd(argvv) ) {
    if( !strcmp("exit",argvv[0]) )
     return 0;
    if( (childpid = fork()) == -1 )
     perror("Failed to fork child");
    else if( childpid == 0 ) {
     if( execute_cmd(argvv) )
      return 1;
    } else {
     wait(((void *)0));
    }
   }
  }
 }
 return 0;
}

variable_types: childpid int

variable_types: linelen int


cleanup

decompiled_code:
void cleanup()
{
free(linebuf);
}

canonical_decompiled_code:
void func4()
{
free(linebuf);
}

original_code:
void cleanup(void)
{
 free(linebuf);
}

canonical_original_code:
void cleanup()
{
 free(linebuf);
}


main

canonical_name: main

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

linebuf = (char *)calloc(0xFFuLL, 1uLL);
if ( linebuf )
{
if ( signal(2, (__sighandler_t)sig_handler) == (__sighandler_t)-1LL )
fwrite("signal() error!\n", 1uLL, 0x10uLL, stderr);
v3 = input_loop();
if ( (unsigned int)savelog("rash.log") )
perror("rash: savelog() failed");
puts(&byte_B2D);
cleanup();
exit(v3);
}
fwrite("malloc problem\n", 1uLL, 0xFuLL, stderr);
exit(1);
}

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

linebuf = (char *)calloc(0xFFuLL, 1uLL);
if ( linebuf )
{
if ( signal(2, (__sighandler_t)sig_handler) == (__sighandler_t)-1LL )
fwrite("signal() error!\n", 1uLL, 0x10uLL, stderr);
v3 = func3();
if ( (unsigned int)savelog("rash.log") )
perror("rash: savelog() failed");
puts(&byte_B2D);
func4();
exit(v3);
}
fwrite("malloc problem\n", 1uLL, 0xFuLL, stderr);
exit(1);
}

original_code:
int main( int argc, char **argv )
{
 int exitcode;
 linebuf = calloc( 255,sizeof(char) );
 if( linebuf == ((void *)0) ) {
  fprintf( stderr,"malloc problem\n" );
  exit(1);
 }
 if( signal(2, sig_handler) == ((__sighandler_t) -1) )
  fprintf( stderr, "signal() error!\n" );
 exitcode = input_loop();
 if( savelog("rash.log") != 0 )
  perror("rash: savelog() failed");
 puts("");
 cleanup();
 exit(exitcode);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int exitcode;
 linebuf = calloc( 255,sizeof(char) );
 if( linebuf == ((void *)0) ) {
  fprintf( stderr,"malloc problem\n" );
  exit(1);
 }
 if( signal(2, sig_handler) == ((__sighandler_t) -1) )
  fprintf( stderr, "signal() error!\n" );
 exitcode = input_loop();
 if( savelog("rash.log") != 0 )
  perror("rash: savelog() failed");
 puts("");
 cleanup();
 exit(exitcode);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: exitcode int


repo: yelkin/socket-programming

decompiled_code:
void __fastcall check_errno(int a1)
{
int *v1; // rax

if ( a1 == -1 )
{
v1 = __errno_location();
perror((const char *)*v1);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
int *v1; // rax

if ( a1 == -1 )
{
v1 = __errno_location();
perror((const char *)*v1);
exit(1);
}
}

original_code:
void check_errno(int retcode)
{
    if(retcode == -1)
    {
        int err=(*__errno_location ());
        perror(err);
        exit(EXIT_FAILURE);
    }
}

canonical_original_code:
void check_errno(int retcode)
{
    if(retcode == -1)
    {
        int err = (*__errno_location ());
        perror(err);
        exit(EXIT_FAILURE);
    }
}

variable_types: retcode int

variable_types: err int


decompiled_code:
__int64 get_unix_socket()
{
unsigned int v0; // er12
int v1; // eax
int v2; // eax
_BYTE v4[104]; // [rsp+0h] [rbp-88h] BYREF
int v5; // [rsp+68h] [rbp-20h]
__int16 v6; // [rsp+6Ch] [rbp-1Ch]

v0 = socket(1, 1, 0);
check_errno(v0);
memset(v4, 0, sizeof(v4));
*(_WORD *)v4 = 1;
v5 = 0;
v6 = 0;
strncpy(&v4[2], SOCKET_NAME, 0x6BuLL)[107] = 0;
v1 = strlen(&v4[2]);
v4[2] = 0;
v2 = bind(v0, (const struct sockaddr *)v4, v1 + 2);
check_errno(v2);
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // er12
int v1; // eax
int v2; // eax
_BYTE v4[104]; // [rsp+0h] [rbp-88h] BYREF
int v5; // [rsp+68h] [rbp-20h]
__int16 v6; // [rsp+6Ch] [rbp-1Ch]

v0 = socket(1, 1, 0);
func7(v0);
memset(v4, 0, sizeof(v4));
*(_WORD *)v4 = 1;
v5 = 0;
v6 = 0;
strncpy(&v4[2], SOCKET_NAME, 0x6BuLL)[107] = 0;
v1 = strlen(&v4[2]);
v4[2] = 0;
v2 = bind(v0, (const struct sockaddr *)v4, v1 + 2);
func7(v2);
return v0;
}

original_code:
int get_unix_socket()
{
    int sockfd;
    struct sockaddr_un sock_un;
    sockfd = socket(1, SOCK_STREAM, 0);
    check_errno(sockfd);
    memset(&sock_un, 0, sizeof sock_un);
    sock_un.sun_family = 1;
    strncpy(sock_un.sun_path, SOCKET_NAME, sizeof sock_un.sun_path-1)
            [sizeof sock_un.sun_path-1] = 0;
    int len = ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&sock_un)->sun_path));
    sock_un.sun_path[0]=0;
    check_errno(bind(sockfd, (struct sockaddr *)&sock_un, len));
    return sockfd;
}

variable_types: sockfd int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: len int


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

v3 = get_unix_socket(argc, argv, envp);
system("netstat -pa --unix|grep qweasd");
close(v3);
return 0;
}

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

v3 = func8(argc, argv, envp);
system("netstat -pa --unix|grep qweasd");
close(v3);
return 0;
}

original_code:
int main(int argc, char** argv)
{
    int sockfd = get_unix_socket();
    system("netstat -pa --unix|grep qweasd");
    close(sockfd);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int sockfd = get_unix_socket();
    system("netstat -pa --unix|grep qweasd");
    close(sockfd);
    return 0;
}


decompiled_code:
int __fastcall show_usage(const char *a1)
{
return fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n", a1);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
return fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n", a1);
}

original_code:
void show_usage(char *program_name)
{
    fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n",
            program_name);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
unsigned int v4; // er13
int v5; // eax
int v6; // ebp
void *v7; // rdx
const char *v8; // rdx
const char *v9; // rdx
int v10; // eax
int v12; // ebp
char *v13; // rax
int longind; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-48h] BYREF
__int64 optval[7]; // [rsp+20h] [rbp-38h] BYREF

longind = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "h:p:", &long_options_0, &longind);
if ( v5 == -1 )
break;
if ( v5 == 104 )
{
v3 = _bss_start;
}
else
{
if ( v5 != 112 )
{
show_usage(*argv);
exit(1);
}
v4 = atoi(_bss_start);
}
}
fprintf(stderr, "Call arguments: host='%s' port='%d'\n", v3, v4);
v6 = socket(2, 1, 0);
if ( v6 == -1
|| (addr = 0LL,
addr.sa_family = 2,
*(_WORD *)addr.sa_data = _bswap_16((unsigned __int16)v4, v3, &addr.sa_data[2]),
inet_pton(2, v3, v7) == -1) )
{
v12 = *__errno_location();
v13 = strerror(v12);
printf("ERROR: %s\n", v13);
exit(v12);
}
optval[0] = 1LL;
optval[1] = 0LL;
if ( setsockopt(v6, 1, 20, optval, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v8);
if ( setsockopt(v6, 1, 21, optval, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v9);
v10 = connect(v6, &addr, 0x10u);
if ( v10 )
{
if ( v10 == -1 )
printf("Port %d is closed on host %s\n", v4, v3);
else
printf("Error connecting to %s:%d", v3, v4);
}
else
{
printf("Port %d is open on host %s\n", v4, v3);
close(v6);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
unsigned int v4; // er13
int v5; // eax
int v6; // ebp
void *v7; // rdx
const char *v8; // rdx
const char *v9; // rdx
int v10; // eax
int v12; // ebp
char *v13; // rax
int longind; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-48h] BYREF
__int64 optval[7]; // [rsp+20h] [rbp-38h] BYREF

longind = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "h:p:", &long_options_0, &longind);
if ( v5 == -1 )
break;
if ( v5 == 104 )
{
v3 = _bss_start;
}
else
{
if ( v5 != 112 )
{
func8(*argv);
exit(1);
}
v4 = atoi(_bss_start);
}
}
fprintf(stderr, "Call arguments: host='%s' port='%d'\n", v3, v4);
v6 = socket(2, 1, 0);
if ( v6 == -1
|| (addr = 0LL,
addr.sa_family = 2,
*(_WORD *)addr.sa_data = func7((unsigned __int16)v4, v3, &addr.sa_data[2]),
inet_pton(2, v3, v7) == -1) )
{
v12 = *__errno_location();
v13 = strerror(v12);
printf("ERROR: %s\n", v13);
exit(v12);
}
optval[0] = 1LL;
optval[1] = 0LL;
if ( setsockopt(v6, 1, 20, optval, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v8);
if ( setsockopt(v6, 1, 21, optval, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v9);
v10 = connect(v6, &addr, 0x10u);
if ( v10 )
{
if ( v10 == -1 )
printf("Port %d is closed on host %s\n", v4, v3);
else
printf("Error connecting to %s:%d", v3, v4);
}
else
{
printf("Port %d is open on host %s\n", v4, v3);
close(v6);
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
    char* host;
    int port;
    int ret;
    char *short_options = "h:p:";
    static struct option long_options[] = {
        {"host", 1, 0, 'h'},
        {"port", 1, 0, 'p'},
        {0, 0, 0, 0}
    };
    int opt;
    int option_index = 0;
    while(1)
    {
        opt = getopt_long(argc, argv, short_options, long_options, &option_index);
        if(opt == -1)
        {
            break;
        }
        switch(opt)
        {
        case 'h':
            host = optarg;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        default:
            show_usage(argv[0]);
            exit(1);
        }
    }
    fprintf(stderr, "Call arguments: host='%s' port='%d'\n", host, port);
    int sock = socket(2, SOCK_STREAM, 0);
    if((sock) == -1) { int err=(*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct sockaddr_in adr_inet;
    memset(&adr_inet, 0, sizeof adr_inet);
    adr_inet.sin_family = 2;
    adr_inet.sin_port = htons(port);
    ret = inet_pton(2, host, &(adr_inet.sin_addr));
    if((ret) == -1) { int err=(*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    if (setsockopt (sock, 1, 20, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    if (setsockopt (sock, 1, 21, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    ret = connect(sock, &adr_inet, sizeof adr_inet);
    if(ret == 0) {
        printf("Port %d is open on host %s\n", port, host);
        close(sock);
    } else if (ret == -1) {
        printf("Port %d is closed on host %s\n", port, host);
    } else {
        printf("Error connecting to %s:%d", host, port);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *host;
    int port;
    int ret;
    char *short_options = "h:p:";
    static struct option long_options[] = {
        {"host", 1, 0, 'h'},
        {"port", 1, 0, 'p'},
        {0, 0, 0, 0}
    };
    int opt;
    int option_index = 0;
    while(1)
    {
        opt = getopt_long(argc, argv, short_options, long_options, &option_index);
        if(opt == -1)
        {
            break;
        }
        switch(opt)
        {
        case 'h':
            host = optarg;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        default:
            show_usage(argv[0]);
            exit(1);
        }
    }
    fprintf(stderr, "Call arguments: host='%s' port='%d'\n", host, port);
    int sock = socket(2, SOCK_STREAM, 0);
    if((sock) == -1) { int err = (*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct sockaddr_in adr_inet;
    memset(&adr_inet, 0, sizeof adr_inet);
    adr_inet.sin_family = 2;
    adr_inet.sin_port = htons(port);
    ret = inet_pton(2, host, &(adr_inet.sin_addr));
    if((ret) == -1) { int err = (*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    if (setsockopt (sock, 1, 20, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    if (setsockopt (sock, 1, 21, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    ret = connect(sock, &adr_inet, sizeof adr_inet);
    if(ret == 0) {
        printf("Port %d is open on host %s\n", port, host);
        close(sock);
    } else if (ret == -1) {
        printf("Port %d is closed on host %s\n", port, host);
    } else {
        printf("Error connecting to %s:%d", host, port);
    }
    return 0;
}

variable_types:
host
char *

variable_types: port int

variable_types: ret int

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

variable_types: opt int

variable_types: sock int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

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


destroyClient

decompiled_code:
void __fastcall destroyClient(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 > 0 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 40 * (unsigned int)(a2 - 1) + 40);
do
{
v5 = *v3;
v3 += 5;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

v2 = ptr;
if ( a2 > 0 )
{
v3 = (void **)ptr;
v4 = (void **)((char *)ptr + 40 * (unsigned int)(a2 - 1) + 40);
do
{
v5 = *v3;
v3 += 5;
free(v5);
}
while ( v3 != v4 );
}
free(v2);
}

original_code:
void destroyClient(Node clients[], int num){
    int i;
    for(i = 0; i<num; i++){
        free(clients[i].name);
    }
    free(clients);
    return;
}

canonical_original_code:
void destroyClient(struct ClientPTR clients[], int num){
    int i;
    for(i = 0; i<num; i++){
        free(clients[i].name);
    }
    free(clients);
    return;
}

variable_types: struct ClientPTR { char *name; int id; float price; float total; struct ClientPTR *nextSucc; struct ClientPTR *nextFail; } []

variable_types: num int

variable_types: i int