decompiled_code:
__int64 __fastcall ptable_put(_QWORD *a1, const char *a2, int *a3)
{
unsigned int v5; // er14
__int64 v6; // rdi
int v7; // eax
__int64 v8; // r12
__int64 v9; // rdi
size_t v10; // rax
char *v11; // rax
char *v12; // r12
int v14; // eax
__int64 v15; // rcx
void *v16; // [rsp+8h] [rbp-40h] BYREF
__int64 v17; // [rsp+10h] [rbp-38h] BYREF
void *ptr; // [rsp+18h] [rbp-30h] BYREF

if ( a3 == 0LL || a2 == 0LL || !a1 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key or data\"\n", "ptable_put", 119LL);
return v5;
}
v5 = table_put(*a1, a2, a3);
if ( v5 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: table_put auxKey, auxData\"\n", "ptable_put", 125LL);
return v5;
}
v6 = *((_QWORD *)a3 + 1);
if ( v6 )
{
v7 = base64_encode_alloc(v6, *a3, &v16);
v8 = v7;
if ( v7 >= 0 )
goto LABEL_6;
v15 = 135LL;
LABEL_17:
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: base64_encode_alloc\"\n", "ptable_put", v15);
return v5;
}
v14 = base64_encode_alloc("0", 2LL, &v16);
v8 = v14;
if ( v14 < 0 )
{
v15 = 139LL;
goto LABEL_17;
}
LABEL_6:
v9 = *((_QWORD *)a3 + 2);
ptr = 0LL;
encode_timestamp(v9, &v17, &ptr);
if ( v17 )
{
*((_BYTE *)ptr + v17) = 0;
*((_BYTE *)v16 + v8) = 0;
v10 = strlen(a2);
v11 = (char *)malloc(v10 + v8 + v17 + 7);
v12 = v11;
if ( !v11 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: malloc msg\"\n", "ptable_put", 156LL);
return v5;
}
sprintf(v11, "put %s %s %s", (const char *)ptr, a2, (const char *)v16);
if ( (int)pmanager_log(a1[1], v12) >= 0 )
goto LABEL_9;
if ( (int)pmanager_store_table(a1[1], *a1) < 0 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_store_table\"\n", "ptable_put", 172LL);
return v5;
}
if ( (unsigned int)pmanager_rotate_log(a1[1]) )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_rotate_log\"\n", "ptable_put", 178LL);
return v5;
}
if ( (int)pmanager_log(a1[1], v12) >= 0 )
{
LABEL_9:
free(v12);
free(ptr);
free(v16);
}
else
{
v5 = -1;
free(v12);
}
}
else
{
v5 = -1;
fprintf(stderr, "%s : %d : \"encode_timestamp\"\n", "ptable_put", 147LL);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, const char *a2, int *a3)
{
unsigned int v5; // er14
__int64 v6; // rdi
int v7; // eax
__int64 v8; // r12
__int64 v9; // rdi
size_t v10; // rax
char *v11; // rax
char *v12; // r12
int v14; // eax
__int64 v15; // rcx
void *v16; // [rsp+8h] [rbp-40h] BYREF
__int64 v17; // [rsp+10h] [rbp-38h] BYREF
void *ptr; // [rsp+18h] [rbp-30h] BYREF

if ( a3 == 0LL || a2 == 0LL || !a1 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key or data\"\n", "ptable_put", 119LL);
return v5;
}
v5 = table_put(*a1, a2, a3);
if ( v5 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: table_put auxKey, auxData\"\n", "ptable_put", 125LL);
return v5;
}
v6 = *((_QWORD *)a3 + 1);
if ( v6 )
{
v7 = base64_encode_alloc(v6, *a3, &v16);
v8 = v7;
if ( v7 >= 0 )
goto LABEL_6;
v15 = 135LL;
LABEL_17:
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: base64_encode_alloc\"\n", "ptable_put", v15);
return v5;
}
v14 = base64_encode_alloc("0", 2LL, &v16);
v8 = v14;
if ( v14 < 0 )
{
v15 = 139LL;
goto LABEL_17;
}
LABEL_6:
v9 = *((_QWORD *)a3 + 2);
ptr = 0LL;
encode_timestamp(v9, &v17, &ptr);
if ( v17 )
{
*((_BYTE *)ptr + v17) = 0;
*((_BYTE *)v16 + v8) = 0;
v10 = strlen(a2);
v11 = (char *)malloc(v10 + v8 + v17 + 7);
v12 = v11;
if ( !v11 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: malloc msg\"\n", "ptable_put", 156LL);
return v5;
}
sprintf(v11, "put %s %s %s", (const char *)ptr, a2, (const char *)v16);
if ( (int)pmanager_log(a1[1], v12) >= 0 )
goto LABEL_9;
if ( (int)pmanager_store_table(a1[1], *a1) < 0 )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_store_table\"\n", "ptable_put", 172LL);
return v5;
}
if ( (unsigned int)pmanager_rotate_log(a1[1]) )
{
v5 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_rotate_log\"\n", "ptable_put", 178LL);
return v5;
}
if ( (int)pmanager_log(a1[1], v12) >= 0 )
{
LABEL_9:
free(v12);
free(ptr);
free(v16);
}
else
{
v5 = -1;
free(v12);
}
}
else
{
v5 = -1;
fprintf(stderr, "%s : %d : \"encode_timestamp\"\n", "ptable_put", 147LL);
}
return v5;
}

original_code:
int ptable_put(struct ptable_t *table, char *key, struct data_t *data) {
 struct timeval start, end;
 long secDiff, usecDiff;
    if(table == ((void *)0) || key == ((void *)0) || data == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: NULL table or key or data\"""\n", __FUNCTION__, 119);
        return -1;
    }
    if(table_put(table->table, key, data) != 0) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: table_put auxKey, auxData\"""\n", __FUNCTION__, 125);
        return -1;
    }
    char *encodedData;
    int encodedSize;
    if(data->data) {
  if((encodedSize = base64_encode_alloc(data->data, data->datasize, &encodedData)) < 0) {
   fprintf( stderr, "%s : %d : ""\"persistent_table: base64_encode_alloc\"""\n", __FUNCTION__, 135);
   return -1;
  }
    } else if((encodedSize = base64_encode_alloc("0", 2, &encodedData)) < 0) {
  fprintf( stderr, "%s : %d : ""\"persistent_table: base64_encode_alloc\"""\n", __FUNCTION__, 139);
  return -1;
 }
 size_t encodedTsSize;
 char *ts = ((void *)0);
 encode_timestamp(data->timestamp, &encodedTsSize, &ts);
 if(encodedTsSize <= 0) {
  fprintf( stderr, "%s : %d : ""\"encode_timestamp\"""\n", __FUNCTION__, 147);
  return -1;
 }
 ts[encodedTsSize] = '\0';
 encodedData[encodedSize] = '\0';
    char *msg = ((void *)0);
    if((msg = (char *) malloc(sizeof(char) * (7 + strlen(key) + encodedSize + encodedTsSize))) == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: malloc msg\"""\n", __FUNCTION__, 156);
        return -1;
    }
    sprintf(msg, "put %s %s %s", ts, key, encodedData);
 if(0) {
  gettimeofday(&start, ((void *)0));
 }
    if((pmanager_log(table->pmanager, msg)) < 0) {
        if(pmanager_store_table(table->pmanager, table->table) < 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_store_table\"""\n", __FUNCTION__, 172);
            return -1;
        }
        if(pmanager_rotate_log(table->pmanager) != 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_rotate_log\"""\n", __FUNCTION__, 178);
            return -1;
        }
 if((pmanager_log(table->pmanager, msg)) < 0) {
         free(msg);
         return -1;
 }
    }
 if(0) {
  gettimeofday(&end, ((void *)0));
  secDiff = end.tv_sec - start.tv_sec;
  if(secDiff > 0) {
   usecDiff = end.tv_usec + (1000000 - start.tv_usec);
  } else {
   usecDiff = end.tv_usec - start.tv_usec;
  }
  printf("*** Time elapsed: %ld us.\n", usecDiff);
 }
    free(msg);
 free(ts);
 free(encodedData);
    return 0;
}

canonical_original_code:
int ptable_put(struct ptable_t *table, char *key, struct data_t *data) {
 struct timeval start; struct timeval end;
 long secDiff; long usecDiff;
    if(table == ((void *)0) || key == ((void *)0) || data == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: NULL table or key or data\"""\n", __FUNCTION__, 119);
        return -1;
    }
    if(table_put(table->table, key, data) != 0) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: table_put auxKey, auxData\"""\n", __FUNCTION__, 125);
        return -1;
    }
    char *encodedData;
    int encodedSize;
    if(data->data) {
  if((encodedSize = base64_encode_alloc(data->data, data->datasize, &encodedData)) < 0) {
   fprintf( stderr, "%s : %d : ""\"persistent_table: base64_encode_alloc\"""\n", __FUNCTION__, 135);
   return -1;
  }
    } else if((encodedSize = base64_encode_alloc("0", 2, &encodedData)) < 0) {
  fprintf( stderr, "%s : %d : ""\"persistent_table: base64_encode_alloc\"""\n", __FUNCTION__, 139);
  return -1;
 }
 unsigned long encodedTsSize;
 char *ts = ((void *)0);
 encode_timestamp(data->timestamp, &encodedTsSize, &ts);
 if(encodedTsSize <= 0) {
  fprintf( stderr, "%s : %d : ""\"encode_timestamp\"""\n", __FUNCTION__, 147);
  return -1;
 }
 ts[encodedTsSize] = '\0';
 encodedData[encodedSize] = '\0';
    char *msg = ((void *)0);
    if((msg = (char *) malloc(sizeof(char) * (7 + strlen(key) + encodedSize + encodedTsSize))) == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: malloc msg\"""\n", __FUNCTION__, 156);
        return -1;
    }
    sprintf(msg, "put %s %s %s", ts, key, encodedData);
 if(0) {
  gettimeofday(&start, ((void *)0));
 }
    if((pmanager_log(table->pmanager, msg)) < 0) {
        if(pmanager_store_table(table->pmanager, table->table) < 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_store_table\"""\n", __FUNCTION__, 172);
            return -1;
        }
        if(pmanager_rotate_log(table->pmanager) != 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_rotate_log\"""\n", __FUNCTION__, 178);
            return -1;
        }
 if((pmanager_log(table->pmanager, msg)) < 0) {
         free(msg);
         return -1;
 }
    }
 if(0) {
  gettimeofday(&end, ((void *)0));
  secDiff = end.tv_sec - start.tv_sec;
  if(secDiff > 0) {
   usecDiff = end.tv_usec + (1000000 - start.tv_usec);
  } else {
   usecDiff = end.tv_usec - start.tv_usec;
  }
  printf("*** Time elapsed: %ld us.\n", usecDiff);
 }
    free(msg);
 free(ts);
 free(encodedData);
    return 0;
}

variable_types:
key
char *

variable_types:
data
struct data_t *

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

variable_types: secDiff long

variable_types: usecDiff long

variable_types:
encodedData
char *

variable_types: encodedSize int

variable_types:
encodedTsSize
unsigned long

variable_types:
ts
char *

variable_types:
msg
char *


repo: vascoorey/DistributedTable

decompiled_code:
__int64 __fastcall ptable_get(_QWORD *a1, __int64 a2)
{
if ( a1 && a2 )
return table_get(*a1);
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key\"\n", "ptable_get", 224LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2)
{
if ( a1 && a2 )
return table_get(*a1);
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key\"\n", "ptable_get", 224LL);
return 0LL;
}

original_code:
struct data_t *ptable_get(struct ptable_t *table, char *key) {
    if(table == ((void *)0) || key == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: NULL table or key\"""\n", __FUNCTION__, 224);
        return ((void *)0);
    }
    return table_get(table->table, key);
}

variable_types: struct ptable_t { struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *table; struct pmanager_t { char *ckp_name; int ckp_fd; char *stt_name; int stt_fd; char *log_name; int log_fd; int create_flags; int max_log_size; int current_log_size; } *pmanager; } *

return_type: struct data_t { int datasize; void *data; long timestamp; } *

user_defined_types: struct ptable_t { struct table_t *table; struct pmanager_t *pmanager; }

user_defined_types: struct table_t { int hashSize; int numElems; int numUpdates; struct list_t **table; }

user_defined_types: struct list_t { struct node_t *head; struct node_t *tail; int elements; }

user_defined_types: struct node_t { struct node_t *prev; struct node_t *next; struct entry_t *entry; }


decompiled_code:
__int64 __fastcall ptable_del(_QWORD *a1, const char *a2)
{
unsigned int v2; // er13
size_t v3; // rax
char *v4; // rax
char *v5; // rbp

if ( a1 && a2 )
{
v2 = table_del(*a1);
if ( v2 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: table_del\"\n", "ptable_del", 247LL);
}
else
{
v3 = strlen(a2);
v4 = (char *)malloc(v3 + 5);
v5 = v4;
if ( !v4 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: malloc msg\"\n", "ptable_del", 254LL);
return v2;
}
sprintf(v4, "del %s", a2);
if ( (int)pmanager_log(a1[1], v5) >= 0 )
goto LABEL_6;
if ( (int)pmanager_store_table(a1[1], *a1) < 0 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_store_table\"\n", "ptable_del", 264LL);
return v2;
}
if ( (unsigned int)pmanager_rotate_log(a1[1]) )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_rotate_log\"\n", "ptable_del", 270LL);
return v2;
}
if ( (int)pmanager_log(a1[1], v5) >= 0 )
{
LABEL_6:
free(v5);
}
else
{
v2 = -1;
free(v5);
}
}
}
else
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key\"\n", "ptable_del", 241LL);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, const char *a2)
{
unsigned int v2; // er13
size_t v3; // rax
char *v4; // rax
char *v5; // rbp

if ( a1 && a2 )
{
v2 = table_del(*a1);
if ( v2 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: table_del\"\n", "ptable_del", 247LL);
}
else
{
v3 = strlen(a2);
v4 = (char *)malloc(v3 + 5);
v5 = v4;
if ( !v4 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: malloc msg\"\n", "ptable_del", 254LL);
return v2;
}
sprintf(v4, "del %s", a2);
if ( (int)pmanager_log(a1[1], v5) >= 0 )
goto LABEL_6;
if ( (int)pmanager_store_table(a1[1], *a1) < 0 )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_store_table\"\n", "ptable_del", 264LL);
return v2;
}
if ( (unsigned int)pmanager_rotate_log(a1[1]) )
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: pmanager_rotate_log\"\n", "ptable_del", 270LL);
return v2;
}
if ( (int)pmanager_log(a1[1], v5) >= 0 )
{
LABEL_6:
free(v5);
}
else
{
v2 = -1;
free(v5);
}
}
}
else
{
v2 = -1;
fprintf(stderr, "%s : %d : \"persistent_table: NULL table or key\"\n", "ptable_del", 241LL);
}
return v2;
}

original_code:
int ptable_del(struct ptable_t *table, char *key) {
    if(table == ((void *)0) || key == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: NULL table or key\"""\n", __FUNCTION__, 241);
        return -1;
    }
    if(table_del(table->table, key) != 0) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: table_del\"""\n", __FUNCTION__, 247);
        return -1;
    }
    char *msg;
    if((msg = (char *) malloc(sizeof(char) * (5 + strlen(key)))) == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"persistent_table: malloc msg\"""\n", __FUNCTION__, 254);
        return -1;
    }
    sprintf(msg, "del %s", key);
    if((pmanager_log(table->pmanager, msg)) < 0) {
        if(pmanager_store_table(table->pmanager, table->table) < 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_store_table\"""\n", __FUNCTION__, 264);
            return -1;
        }
        if(pmanager_rotate_log(table->pmanager) != 0) {
            fprintf( stderr, "%s : %d : ""\"persistent_table: pmanager_rotate_log\"""\n", __FUNCTION__, 270);
            return -1;
        }
 if((pmanager_log(table->pmanager, msg)) < 0) {
         free(msg);
         return -1;
 }
    }
    free(msg);
    return 0;
}

variable_types: struct ptable_t { struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *table; struct pmanager_t { char *ckp_name; int ckp_fd; char *stt_name; int stt_fd; char *log_name; int log_fd; int create_flags; int max_log_size; int current_log_size; } *pmanager; } *

return_type: int

user_defined_types: struct entry_t { char *key; struct data_t *value; }


decompiled_code:
__int64 __fastcall ptable_size(_QWORD *a1)
{
__int64 result; // rax

if ( a1 )
result = table_size(*a1);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
__int64 result; // rax

if ( a1 )
result = table_size(*a1);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int ptable_size(struct ptable_t *table) {
    if(table) {
        return table_size(table->table);
    }
    return -1;
}

user_defined_types: struct pmanager_t { char *ckp_name; int ckp_fd; char *stt_name; int stt_fd; char *log_name; int log_fd; int create_flags; int max_log_size; int current_log_size; }

user_defined_types: struct data_t { int datasize; void *data; long timestamp; }


decompiled_code:
__int64 __fastcall ptable_get_keys(_QWORD *a1)
{
__int64 result; // rax

if ( a1 )
result = table_get_keys(*a1);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
__int64 result; // rax

if ( a1 )
result = table_get_keys(*a1);
else
result = 0LL;
return result;
}

original_code:
char **ptable_get_keys(struct ptable_t *table) {
    if(table) {
        return table_get_keys(table->table);
    }
    return ((void *)0);
}

return_type: char **


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

if ( a1 )
result = table_free_keys();
return result;
}

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

if ( a1 )
result = table_free_keys();
return result;
}

original_code:
void ptable_free_keys(char **keys) {
    if(keys) {
        table_free_keys(keys);
    }
}

variable_types:
keys
char **

return_type: void


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

if ( !a1 || !a2 )
{
v2 = -1LL;
fprintf(stderr, "%s : %d : \"NULL ptable or key\"\n", "ptable_get_ts", 331LL);
return v2;
}
v2 = table_get_ts(*a1);
if ( v2 >= 0 )
return v2;
fprintf(stderr, "%s : %d : \"table_get_ts\"\n", "ptable_get_ts", 338LL);
return v2;
}

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

if ( !a1 || !a2 )
{
v2 = -1LL;
fprintf(stderr, "%s : %d : \"NULL ptable or key\"\n", "ptable_get_ts", 331LL);
return v2;
}
v2 = table_get_ts(*a1);
if ( v2 >= 0 )
return v2;
fprintf(stderr, "%s : %d : \"table_get_ts\"\n", "ptable_get_ts", 338LL);
return v2;
}

original_code:
long ptable_get_ts(struct ptable_t *ptable, char *key) {
    if(ptable == ((void *)0) || key == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"NULL ptable or key\"""\n", __FUNCTION__, 331);
        return -1;
    }
    long ts;
    if((ts = table_get_ts(ptable->table, key)) < 0) {
        fprintf( stderr, "%s : %d : ""\"table_get_ts\"""\n", __FUNCTION__, 338);
        return ts;
    }
    return ts;
}

variable_types: ts long

return_type: long


original_code:
int ptable_collect_garbage(struct ptable_t *ptable) {
 if(!ptable) {
  return -1;
 }
 struct table_t *table = ptable->table;
 struct node_t *node = ((void *)0), *nextNode;
 int currentList = 0, element = 0, max = 0;
 for(currentList = 0; currentList < table->hashSize; currentList ++) {
  node = table->table[currentList]->head;
  max = table->table[currentList]->elements;
  element = 0;
  while(node && element < max) {
   nextNode = node->next;
   if(memcmp(node->entry->value->data, "0", 1) == 0 && node->entry->value->datasize == 1) {
    printf("Deleting key: %s\n", node->entry->key);
    table_del(table, node->entry->key);
   }
   element ++;
   node = nextNode;
  }
 }
 return 0;
}

canonical_original_code:
int ptable_collect_garbage(struct ptable_t *ptable) {
 if(!ptable) {
  return -1;
 }
 struct table_t *table = ptable->table;
 struct node_t *node = ((void *)0); struct node_t *nextNode;
 int currentList = 0; int element = 0; int max = 0;
 for(currentList = 0; currentList < table->hashSize; currentList ++) {
  node = table->table[currentList]->head;
  max = table->table[currentList]->elements;
  element = 0;
  while(node && element < max) {
   nextNode = node->next;
   if(memcmp(node->entry->value->data, "0", 1) == 0 && node->entry->value->datasize == 1) {
    printf("Deleting key: %s\n", node->entry->key);
    table_del(table, node->entry->key);
   }
   element ++;
   node = nextNode;
  }
 }
 return 0;
}

variable_types: struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *

variable_types: currentList int

variable_types: element int

variable_types: max int


signalHandler

decompiled_code:
void signalHandler()
{
signal(2, signalHandler);
if ( !shutdownServer )
{
puts("OK you're the boss... But the leaks are your fault.");
exit(0);
}
puts("\nSinal apanhado, iremos desligar o servidor assim que possivel!");
shutdownServer = 0;
}

canonical_decompiled_code:
void func0()
{
signal(2, signalHandler);
if ( !shutdownServer )
{
puts("OK you're the boss... But the leaks are your fault.");
exit(0);
}
puts("\nSinal apanhado, iremos desligar o servidor assim que possivel!");
shutdownServer = 0;
}

original_code:
void signalHandler(sig_t sig) {
 signal(2, (__sighandler_t)signalHandler);
 if(shutdownServer) {
  printf("\nSinal apanhado, iremos desligar o servidor assim que possivel!\n");
  shutdownServer = 0;
 } else {
  printf("OK you're the boss... But the leaks are your fault.\n");
  exit(0);
 }
}

canonical_original_code:
void signalHandler(void (*sig)(int)) {
 signal(2, (void (*)(int))signalHandler);
 if(shutdownServer) {
  printf("\nSinal apanhado, iremos desligar o servidor assim que possivel!\n");
  shutdownServer = 0;
 } else {
  printf("OK you're the boss... But the leaks are your fault.\n");
  exit(0);
 }
}

variable_types:
sig
void (*)(int)


decompiled_code:
__int64 __fastcall server_send_receive(int fd)
{
int v2; // ebp
int v3; // eax
__int16 *v4; // rax
__int16 *v5; // rbp
signed int v6; // er12
size_t v7; // rdx
__int64 result; // rax
_DWORD *v9; // rax
_DWORD *v10; // rbp
void *v11; // rdi
unsigned int buf; // [rsp+4h] [rbp-24h] BYREF
void *ptr[4]; // [rsp+8h] [rbp-20h] BYREF

if ( (int)read(fd, &buf, 4uLL) <= 0 )
return 0xFFFFFFFFLL;
v2 = _bswap_32(buf);
ptr[0] = malloc(v2 + 1);
if ( !ptr[0] )
{
perror("malloc");
exit(1);
}
v3 = read(fd, ptr[0], v2);
if ( v2 > v3 )
{
perror("read buffer");
free(ptr[0]);
return 0xFFFFFFFFLL;
}
*((_BYTE *)ptr[0] + v3) = 0;
v4 = (__int16 *)string_to_message(ptr[0]);
v5 = v4;
if ( !v4 )
{
perror("string_to_message");
puts("servidor recebeu uma mensagem invalida...");
puts("criando mensagem de erro para enviar de volta...");
v9 = malloc(0x10uLL);
v10 = v9;
if ( !v9 )
{
perror("malloc message!");
exit(1);
}
*v9 = 3276899;
v11 = ptr[0];
v9[2] = -1;
free(v11);
v6 = message_to_string(v10, ptr);
if ( v6 > 0 )
goto LABEL_7;
LABEL_15:
perror("message_to_string...\n");
exit(1);
}
printf("Mensagem %d: %hd %hd\n", (unsigned int)relogioLogico, (unsigned int)*v4, (unsigned int)v4[1]);
++relogioLogico;
invoke(v5);
free(ptr[0]);
v6 = message_to_string(v5, ptr);
if ( v6 <= 0 )
goto LABEL_15;
free_message(v5);
LABEL_7:
buf = _bswap_32(v6);
if ( write(fd, &buf, v7) == -1 )
{
perror("write bufferLength");
result = 0LL;
}
else
{
if ( write(fd, ptr[0], v6) == -1 )
perror("write buffer");
else
puts("Resposta enviada com sucesso.");
free(ptr[0]);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
int v2; // ebp
int v3; // eax
__int16 *v4; // rax
__int16 *v5; // rbp
signed int v6; // er12
size_t v7; // rdx
__int64 result; // rax
_DWORD *v9; // rax
_DWORD *v10; // rbp
void *v11; // rdi
unsigned int buf; // [rsp+4h] [rbp-24h] BYREF
void *ptr[4]; // [rsp+8h] [rbp-20h] BYREF

if ( (int)read(fd, &buf, 4uLL) <= 0 )
return 0xFFFFFFFFLL;
v2 = func1(buf);
ptr[0] = malloc(v2 + 1);
if ( !ptr[0] )
{
perror("malloc");
exit(1);
}
v3 = read(fd, ptr[0], v2);
if ( v2 > v3 )
{
perror("read buffer");
free(ptr[0]);
return 0xFFFFFFFFLL;
}
*((_BYTE *)ptr[0] + v3) = 0;
v4 = (__int16 *)string_to_message(ptr[0]);
v5 = v4;
if ( !v4 )
{
perror("string_to_message");
puts("servidor recebeu uma mensagem invalida...");
puts("criando mensagem de erro para enviar de volta...");
v9 = malloc(0x10uLL);
v10 = v9;
if ( !v9 )
{
perror("malloc message!");
exit(1);
}
*v9 = 3276899;
v11 = ptr[0];
v9[2] = -1;
free(v11);
v6 = message_to_string(v10, ptr);
if ( v6 > 0 )
goto LABEL_7;
LABEL_15:
perror("message_to_string...\n");
exit(1);
}
printf("Mensagem %d: %hd %hd\n", (unsigned int)relogioLogico, (unsigned int)*v4, (unsigned int)v4[1]);
++relogioLogico;
invoke(v5);
free(ptr[0]);
v6 = message_to_string(v5, ptr);
if ( v6 <= 0 )
goto LABEL_15;
free_message(v5);
LABEL_7:
buf = func1(v6);
if ( write(fd, &buf, v7) == -1 )
{
perror("write bufferLength");
result = 0LL;
}
else
{
if ( write(fd, ptr[0], v6) == -1 )
perror("write buffer");
else
puts("Resposta enviada com sucesso.");
free(ptr[0]);
result = 0LL;
}
return result;
}

original_code:
int server_send_receive(struct pollfd connection) {
 int numBytes, temp, retVal = -1, bufferSize;
 uint32_t bufferLength;
 char *buffer;
 struct message_t *message;
 if((numBytes = (int)read(connection.fd, &bufferLength, sizeof(uint32_t))) > 0) {
  temp = (int)ntohl(bufferLength);
  if(!(buffer = (char*)malloc(temp + 1))) {
   perror("malloc");
   exit(1);
  }
  if((numBytes = (int)read(connection.fd, buffer, temp)) < temp) {
   perror("read buffer");
   free(buffer);
  } else {
   buffer[numBytes] = '\0';
   if(!(message = string_to_message(buffer))) {
    perror("string_to_message");
    printf("servidor recebeu uma mensagem invalida...\n");
    printf("criando mensagem de erro para enviar de volta...\n");
    if(!(message = (struct message_t*)malloc(sizeof(struct message_t)))) {
     perror("malloc message!");
     exit(1);
    }
    message->opcode = 99;
    message->c_type = 50;
    message->content.result = -1;
    free(buffer);
    if((numBytes = message_to_string(message, &buffer)) <= 0) {
     perror("message_to_string...\n");
     exit(1);
    }
   } else {
    printf("Mensagem %d: %hd %hd\n", relogioLogico, message->opcode, message->c_type);
    relogioLogico ++;
    invoke(message);
    free(buffer);
    if((numBytes = message_to_string(message, &buffer)) <= 0) {
     perror("message_to_string...\n");
     exit(1);
    }
    free_message(message);
   }
   bufferLength = htonl(numBytes);
   if(write(connection.fd, &bufferLength, sizeof(bufferLength)) == -1) {
    perror("write bufferLength");
   } else {
    if(write(connection.fd, buffer, numBytes) == -1) {
     perror("write buffer");
    } else {
     printf("Resposta enviada com sucesso.\n");
    }
    free(buffer);
   }
   retVal = 0;
  }
 }
 return retVal;
}

canonical_original_code:
int server_send_receive(struct pollfd connection) {
 int numBytes; int temp; int retVal = -1; int bufferSize;
 unsigned int bufferLength;
 char *buffer;
 struct message_t *message;
 if((numBytes = (int)read(connection.fd, &bufferLength, sizeof(unsigned int))) > 0) {
  temp = (int)ntohl(bufferLength);
  if(!(buffer = (char *)malloc(temp + 1))) {
   perror("malloc");
   exit(1);
  }
  if((numBytes = (int)read(connection.fd, buffer, temp)) < temp) {
   perror("read buffer");
   free(buffer);
  } else {
   buffer[numBytes] = '\0';
   if(!(message = string_to_message(buffer))) {
    perror("string_to_message");
    printf("servidor recebeu uma mensagem invalida...\n");
    printf("criando mensagem de erro para enviar de volta...\n");
    if(!(message = (struct message_t *)malloc(sizeof(struct message_t)))) {
     perror("malloc message!");
     exit(1);
    }
    message->opcode = 99;
    message->c_type = 50;
    message->content.result = -1;
    free(buffer);
    if((numBytes = message_to_string(message, &buffer)) <= 0) {
     perror("message_to_string...\n");
     exit(1);
    }
   } else {
    printf("Mensagem %d: %hd %hd\n", relogioLogico, message->opcode, message->c_type);
    relogioLogico ++;
    invoke(message);
    free(buffer);
    if((numBytes = message_to_string(message, &buffer)) <= 0) {
     perror("message_to_string...\n");
     exit(1);
    }
    free_message(message);
   }
   bufferLength = htonl(numBytes);
   if(write(connection.fd, &bufferLength, sizeof(bufferLength)) == -1) {
    perror("write bufferLength");
   } else {
    if(write(connection.fd, buffer, numBytes) == -1) {
     perror("write buffer");
    } else {
     printf("Resposta enviada com sucesso.\n");
    }
    free(buffer);
   }
   retVal = 0;
  }
 }
 return retVal;
}

variable_types: struct pollfd { int fd; short events; short revents; }

variable_types: numBytes int

variable_types: temp int

variable_types: retVal int

variable_types: bufferSize int

variable_types:
bufferLength
unsigned int

variable_types:
buffer
char *

variable_types: struct message_t { short opcode; short c_type; union content_u { struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; char *key; char **keys; struct data_t *value; int result; long timestamp; } content; } *

user_defined_types: struct message_t { short opcode; short c_type; union content_u content; }

user_defined_types: union content_u { struct entry_t *entry; char *key; char **keys; struct data_t *value; int result; long timestamp; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rsi
int v5; // eax
struct addrinfo *v6; // rbp
int v7; // eax
int v8; // er13
struct pollfd *v9; // rbx
struct sockaddr *v10; // rax
unsigned int v11; // eax
int v12; // er12
__int64 v13; // rax
int v14; // esi
struct pollfd *v15; // r14
unsigned int v16; // er15
__int16 v17; // ax
__int64 v18; // rsi
const char *v19; // rdi
time_t v20; // r15
const char *v22; // rax
time_t v23; // [rsp+0h] [rbp-158h]
int v24; // [rsp+Ch] [rbp-14Ch]
int optval; // [rsp+10h] [rbp-148h] BYREF
socklen_t addr_len; // [rsp+14h] [rbp-144h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-140h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-138h] BYREF
struct pollfd fds[2]; // [rsp+50h] [rbp-108h] BYREF
__int128 v30; // [rsp+60h] [rbp-F8h]
__int128 v31; // [rsp+70h] [rbp-E8h]
__int128 v32; // [rsp+80h] [rbp-D8h]
__int128 v33; // [rsp+90h] [rbp-C8h]
struct sockaddr addr; // [rsp+A0h] [rbp-B8h] BYREF

optval = 1;
if ( argc != 4 )
{
fwrite("usage: server <port> <num lists> <filename>\n", 1uLL, 0x2CuLL, stderr);
exit(-1);
}
signal(2, (__sighandler_t)signalHandler);
signal(13, (__sighandler_t)((char *)signalHandler + 1));
v4 = argv[1];
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
v5 = getaddrinfo(0LL, v4, &req, &pai);
v24 = v5;
if ( v5 )
{
v22 = gai_strerror(v5);
fprintf(stderr, "getaddrinfo: %s\n", v22);
return -1;
}
v6 = pai;
if ( !pai )
{
LABEL_42:
fwrite("server: failed to bind\n", 1uLL, 0x17uLL, stderr);
return -1;
}
while ( 1 )
{
v7 = socket(v6->ai_family, v6->ai_socktype, v6->ai_protocol);
v8 = v7;
if ( v7 != -1 )
break;
perror("server: socket");
LABEL_41:
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_42;
}
if ( setsockopt(v7, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v8, v6->ai_addr, v6->ai_addrlen) == -1 )
{
close(v8);
perror("server: bind");
goto LABEL_41;
}
v9 = fds;
freeaddrinfo(v6);
fcntl(v8, 4, 2048LL);
*(_OWORD *)&fds[0].fd = 0LL;
fds[0].fd = v8;
fds[0].events = 1;
v10 = (struct sockaddr *)&fds[1];
v30 = 0LL;
v31 = 0LL;
v32 = 0LL;
v33 = 0LL;
do
{
*(_DWORD *)&v10->sa_family = -1;
v10 = (struct sockaddr *)((char *)v10 + 8);
}
while ( v10 != &addr );
if ( listen(v8, 10) == -1 )
{
perror("listen");
close(v8);
exit(-1);
}
puts("server: waiting..");
v11 = atoi(argv[2]);
if ( (unsigned int)table_skel_init(v11, argv[3]) == -1 )
{
perror("table_skel_init");
exit(-1);
}
v12 = -1;
v23 = time(0LL);
LABEL_12:
while ( poll(fds, 0xAuLL, 0) != -1 && shutdownServer )
{
if ( (fds[0].revents & 1) == 0 )
goto LABEL_19;
puts(aNovaLiga);
addr_len = 128;
v12 = accept(v8, &addr, &addr_len);
if ( v12 != -1 )
{
v13 = 2LL;
v14 = 1;
if ( fds[1].fd != -1 )
{
do
v14 = v13++;
while ( *((_DWORD *)&req.ai_next + 2 * v13) != -1 );
}
fds[v14].fd = v12;
fds[v14].events = 1;
printf(aALiga);
LABEL_19:
v15 = &fds[1];
v16 = 1;
while ( 1 )
{
v17 = v15->revents;
v18 = v16;
v19 = aAFecharLiga;
if ( (v17 & 0x10) == 0 )
{
if ( v15->fd <= 0 || (v17 & 1) == 0 || (unsigned int)server_send_receive(*(_QWORD *)&fds[v16]) != -1 )
{
++v16;
++v15;
if ( v16 == 10 )
goto LABEL_25;
continue;
}
v18 = (unsigned int)v15->fd;
v19 = "servidor: ligacao perdida, a fechar: %d...\n";
}
++v16;
++v15;
printf(v19, v18);
close(v15[-1].fd);
v18 = 0xFFFFFFFFLL;
v15[-1].fd = -1;
v15[-1].revents = -1;
v15[-1].events = -1;
if ( v16 == 10 )
{
LABEL_25:
v20 = time(0LL);
if ( v20 - v23 > 300 )
{
puts("*** Collecting Garbage ***");
table_skel_collect("*** Collecting Garbage ***", v18);
v23 = v20;
}
goto LABEL_12;
}
}
}
perror("accept");
}
do
{
if ( v9->fd > 0 )
close(v9->fd);
++v9;
}
while ( v9 != (struct pollfd *)&addr );
if ( (unsigned int)table_skel_destroy() == -1 )
perror("table_skel_destroy");
if ( v8 )
close(v8);
if ( v12 > 0 )
close(v12);
return v24;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rsi
int v5; // eax
struct addrinfo *v6; // rbp
int v7; // eax
int v8; // er13
struct pollfd *v9; // rbx
struct sockaddr *v10; // rax
unsigned int v11; // eax
int v12; // er12
__int64 v13; // rax
int v14; // esi
struct pollfd *v15; // r14
unsigned int v16; // er15
__int16 v17; // ax
__int64 v18; // rsi
const char *v19; // rdi
time_t v20; // r15
const char *v22; // rax
time_t v23; // [rsp+0h] [rbp-158h]
int v24; // [rsp+Ch] [rbp-14Ch]
int optval; // [rsp+10h] [rbp-148h] BYREF
socklen_t addr_len; // [rsp+14h] [rbp-144h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-140h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-138h] BYREF
struct pollfd fds[2]; // [rsp+50h] [rbp-108h] BYREF
__int128 v30; // [rsp+60h] [rbp-F8h]
__int128 v31; // [rsp+70h] [rbp-E8h]
__int128 v32; // [rsp+80h] [rbp-D8h]
__int128 v33; // [rsp+90h] [rbp-C8h]
struct sockaddr addr; // [rsp+A0h] [rbp-B8h] BYREF

optval = 1;
if ( argc != 4 )
{
fwrite("usage: server <port> <num lists> <filename>\n", 1uLL, 0x2CuLL, stderr);
exit(-1);
}
signal(2, (__sighandler_t)signalHandler);
signal(13, (__sighandler_t)((char *)signalHandler + 1));
v4 = argv[1];
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
v5 = getaddrinfo(0LL, v4, &req, &pai);
v24 = v5;
if ( v5 )
{
v22 = gai_strerror(v5);
fprintf(stderr, "getaddrinfo: %s\n", v22);
return -1;
}
v6 = pai;
if ( !pai )
{
LABEL_42:
fwrite("server: failed to bind\n", 1uLL, 0x17uLL, stderr);
return -1;
}
while ( 1 )
{
v7 = socket(v6->ai_family, v6->ai_socktype, v6->ai_protocol);
v8 = v7;
if ( v7 != -1 )
break;
perror("server: socket");
LABEL_41:
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_42;
}
if ( setsockopt(v7, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v8, v6->ai_addr, v6->ai_addrlen) == -1 )
{
close(v8);
perror("server: bind");
goto LABEL_41;
}
v9 = fds;
freeaddrinfo(v6);
fcntl(v8, 4, 2048LL);
*(_OWORD *)&fds[0].fd = 0LL;
fds[0].fd = v8;
fds[0].events = 1;
v10 = (struct sockaddr *)&fds[1];
v30 = 0LL;
v31 = 0LL;
v32 = 0LL;
v33 = 0LL;
do
{
*(_DWORD *)&v10->sa_family = -1;
v10 = (struct sockaddr *)((char *)v10 + 8);
}
while ( v10 != &addr );
if ( listen(v8, 10) == -1 )
{
perror("listen");
close(v8);
exit(-1);
}
puts("server: waiting..");
v11 = atoi(argv[2]);
if ( (unsigned int)table_skel_init(v11, argv[3]) == -1 )
{
perror("table_skel_init");
exit(-1);
}
v12 = -1;
v23 = time(0LL);
LABEL_12:
while ( poll(fds, 0xAuLL, 0) != -1 && shutdownServer )
{
if ( (fds[0].revents & 1) == 0 )
goto LABEL_19;
puts(aNovaLiga);
addr_len = 128;
v12 = accept(v8, &addr, &addr_len);
if ( v12 != -1 )
{
v13 = 2LL;
v14 = 1;
if ( fds[1].fd != -1 )
{
do
v14 = v13++;
while ( *((_DWORD *)&req.ai_next + 2 * v13) != -1 );
}
fds[v14].fd = v12;
fds[v14].events = 1;
printf(aALiga);
LABEL_19:
v15 = &fds[1];
v16 = 1;
while ( 1 )
{
v17 = v15->revents;
v18 = v16;
v19 = aAFecharLiga;
if ( (v17 & 0x10) == 0 )
{
if ( v15->fd <= 0 || (v17 & 1) == 0 || (unsigned int)func2(*(_QWORD *)&fds[v16]) != -1 )
{
++v16;
++v15;
if ( v16 == 10 )
goto LABEL_25;
continue;
}
v18 = (unsigned int)v15->fd;
v19 = "servidor: ligacao perdida, a fechar: %d...\n";
}
++v16;
++v15;
printf(v19, v18);
close(v15[-1].fd);
v18 = 0xFFFFFFFFLL;
v15[-1].fd = -1;
v15[-1].revents = -1;
v15[-1].events = -1;
if ( v16 == 10 )
{
LABEL_25:
v20 = time(0LL);
if ( v20 - v23 > 300 )
{
puts("*** Collecting Garbage ***");
table_skel_collect("*** Collecting Garbage ***", v18);
v23 = v20;
}
goto LABEL_12;
}
}
}
perror("accept");
}
do
{
if ( v9->fd > 0 )
close(v9->fd);
++v9;
}
while ( v9 != (struct pollfd *)&addr );
if ( (unsigned int)table_skel_destroy() == -1 )
perror("table_skel_destroy");
if ( v8 )
close(v8);
if ( v12 > 0 )
close(v12);
return v24;
}

original_code:
int main(int argc, char **argv) {
 int socketFileDescriptor, newSocket = -1, numBytes, yes = 1, retVal, pollCounter, temp;
 struct addrinfo hints, *serverInfo, *anAddress;
 struct sockaddr_storage connectorAddress;
 socklen_t socketSize;
 struct sigaction signalAction;
 struct pollfd ufd[10];
 char addressDescription[46], *buffer = ((void *)0);
 uint32_t bufferLength;
 struct message_t *message = ((void *)0);
 if(argc != 4) {
  fprintf(stderr, "usage: server <port> <num lists> <filename>\n");
  exit(-1);
 }
 signal(2, (__sighandler_t)signalHandler);
 signal(13, ((__sighandler_t) 1));
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0x0001;
 if((retVal = getaddrinfo(((void *)0), argv[1], &hints, &serverInfo)) != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retVal));
  return -1;
 }
 for(anAddress = serverInfo; anAddress; anAddress = anAddress->ai_next) {
  if((socketFileDescriptor = socket(anAddress->ai_family, anAddress->ai_socktype, anAddress->ai_protocol)) == -1) {
   perror("server: socket");
   continue;
  }
  if(setsockopt(socketFileDescriptor, 1, 2, &yes, sizeof(int)) == -1) {
   perror("setsockopt");
   exit(1);
  }
  if(bind(socketFileDescriptor, anAddress->ai_addr, anAddress->ai_addrlen) == -1) {
   close(socketFileDescriptor);
   perror("server: bind");
   continue;
  }
  break;
 }
 if(!anAddress) {
  fprintf(stderr, "server: failed to bind\n");
  return -1;
 }
 freeaddrinfo(anAddress);
 fcntl(socketFileDescriptor, 4, 04000);
 memset(ufd, 0, sizeof(ufd));
 ufd[0].fd = socketFileDescriptor;
 ufd[0].events = 0x001;
 for(pollCounter = 1; pollCounter < 10; pollCounter++)
  ufd[pollCounter].fd = -1;
 if(listen(ufd[0].fd, 10) == -1) {
  perror("listen");
  close(socketFileDescriptor);
  exit(-1);
 }
 printf("server: waiting..\n");
 if(table_skel_init(atoi(argv[2]), argv[3]) == -1) {
  perror("table_skel_init");
  exit(-1);
 }
 time_t lastTime = time(((void *)0)), currentTime;
 while((retVal = poll(ufd, 10, 0)) != -1 && shutdownServer) {
  if(ufd[0].revents & 0x001) {
   printf("Nova ligação detectada!\n");
   socketSize = sizeof(connectorAddress);
   if((newSocket = accept(socketFileDescriptor, (struct sockaddr*)&connectorAddress, &socketSize)) == -1) {
    perror("accept");
    continue;
   } else {
    for(pollCounter = 1; ufd[pollCounter].fd != -1; pollCounter++);
    ufd[pollCounter].fd = newSocket;
    ufd[pollCounter].events = 0x001;
    printf("A ligação foi estabelecida com sucesso. (%d)\n", pollCounter);
   }
  }
  for(pollCounter = 1; pollCounter < 10; pollCounter ++) {
   if(ufd[pollCounter].revents & 0x010) {
    printf("A fechar ligação %d\n", pollCounter);
    close(ufd[pollCounter].fd);
    ufd[pollCounter].fd = -1;
    ufd[pollCounter].revents = -1;
    ufd[pollCounter].events = -1;
   }
   else if(ufd[pollCounter].fd > 0 && ufd[pollCounter].revents & 0x001) {
    if(server_send_receive(ufd[pollCounter]) == -1) {
     printf("servidor: ligacao perdida, a fechar: %d...\n", ufd[pollCounter].fd);
     close(ufd[pollCounter].fd);
     ufd[pollCounter].fd = -1;
     ufd[pollCounter].revents = -1;
     ufd[pollCounter].events = -1;
    }
   }
  }
  currentTime = time(((void *)0));
  if((currentTime - lastTime) > 300) {
   printf("*** Collecting Garbage ***\n");
   table_skel_collect();
   lastTime = currentTime;
  }
 }
 for(pollCounter = 0; pollCounter < 10; pollCounter ++) {
  if(ufd[pollCounter].fd > 0) {
   close(ufd[pollCounter].fd);
  }
 }
 if(table_skel_destroy() == -1) {
  perror("table_skel_destroy");
 }
 if(buffer) {
  printf("Libertando buffer...\n");
  free(buffer);
  buffer = ((void *)0);
 }
 if(message) {
  printf("Libertando mensagem...\n");
  free_message(message);
 }
 if(socketFileDescriptor) {
  close(socketFileDescriptor);
 }
 if(newSocket > 0) {
  close(newSocket);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 int socketFileDescriptor; int newSocket = -1; int numBytes; int yes = 1; int retVal; int pollCounter; int temp;
 struct addrinfo hints; struct addrinfo *serverInfo; struct addrinfo *anAddress;
 struct sockaddr_storage connectorAddress;
 unsigned int socketSize;
 struct sigaction signalAction;
 struct pollfd ufd[10];
 char addressDescription[46]; char *buffer = ((void *)0);
 unsigned int bufferLength;
 struct message_t *message = ((void *)0);
 if(argc != 4) {
  fprintf(stderr, "usage: server <port> <num lists> <filename>\n");
  exit(-1);
 }
 signal(2, (void (*)(int))signalHandler);
 signal(13, ((void (*)(int)) 1));
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 0;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = 0x0001;
 if((retVal = getaddrinfo(((void *)0), argv[1], &hints, &serverInfo)) != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(retVal));
  return -1;
 }
 for(anAddress = serverInfo; anAddress; anAddress = anAddress->ai_next) {
  if((socketFileDescriptor = socket(anAddress->ai_family, anAddress->ai_socktype, anAddress->ai_protocol)) == -1) {
   perror("server: socket");
   continue;
  }
  if(setsockopt(socketFileDescriptor, 1, 2, &yes, sizeof(int)) == -1) {
   perror("setsockopt");
   exit(1);
  }
  if(bind(socketFileDescriptor, anAddress->ai_addr, anAddress->ai_addrlen) == -1) {
   close(socketFileDescriptor);
   perror("server: bind");
   continue;
  }
  break;
 }
 if(!anAddress) {
  fprintf(stderr, "server: failed to bind\n");
  return -1;
 }
 freeaddrinfo(anAddress);
 fcntl(socketFileDescriptor, 4, 04000);
 memset(ufd, 0, sizeof(ufd));
 ufd[0].fd = socketFileDescriptor;
 ufd[0].events = 0x001;
 for(pollCounter = 1; pollCounter < 10; pollCounter++)
  ufd[pollCounter].fd = -1;
 if(listen(ufd[0].fd, 10) == -1) {
  perror("listen");
  close(socketFileDescriptor);
  exit(-1);
 }
 printf("server: waiting..\n");
 if(table_skel_init(atoi(argv[2]), argv[3]) == -1) {
  perror("table_skel_init");
  exit(-1);
 }
 long lastTime = time(((void *)0)); long currentTime;
 while((retVal = poll(ufd, 10, 0)) != -1 && shutdownServer) {
  if(ufd[0].revents & 0x001) {
   printf("Nova ligação detectada!\n");
   socketSize = sizeof(connectorAddress);
   if((newSocket = accept(socketFileDescriptor, (struct sockaddr *)&connectorAddress, &socketSize)) == -1) {
    perror("accept");
    continue;
   } else {
    for(pollCounter = 1; ufd[pollCounter].fd != -1; pollCounter++);
    ufd[pollCounter].fd = newSocket;
    ufd[pollCounter].events = 0x001;
    printf("A ligação foi estabelecida com sucesso. (%d)\n", pollCounter);
   }
  }
  for(pollCounter = 1; pollCounter < 10; pollCounter ++) {
   if(ufd[pollCounter].revents & 0x010) {
    printf("A fechar ligação %d\n", pollCounter);
    close(ufd[pollCounter].fd);
    ufd[pollCounter].fd = -1;
    ufd[pollCounter].revents = -1;
    ufd[pollCounter].events = -1;
   }
   else if(ufd[pollCounter].fd > 0 && ufd[pollCounter].revents & 0x001) {
    if(server_send_receive(ufd[pollCounter]) == -1) {
     printf("servidor: ligacao perdida, a fechar: %d...\n", ufd[pollCounter].fd);
     close(ufd[pollCounter].fd);
     ufd[pollCounter].fd = -1;
     ufd[pollCounter].revents = -1;
     ufd[pollCounter].events = -1;
    }
   }
  }
  currentTime = time(((void *)0));
  if((currentTime - lastTime) > 300) {
   printf("*** Collecting Garbage ***\n");
   table_skel_collect();
   lastTime = currentTime;
  }
 }
 for(pollCounter = 0; pollCounter < 10; pollCounter ++) {
  if(ufd[pollCounter].fd > 0) {
   close(ufd[pollCounter].fd);
  }
 }
 if(table_skel_destroy() == -1) {
  perror("table_skel_destroy");
 }
 if(buffer) {
  printf("Libertando buffer...\n");
  free(buffer);
  buffer = ((void *)0);
 }
 if(message) {
  printf("Libertando mensagem...\n");
  free_message(message);
 }
 if(socketFileDescriptor) {
  close(socketFileDescriptor);
 }
 if(newSocket > 0) {
  close(newSocket);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: socketFileDescriptor int

variable_types: newSocket int

variable_types: yes int

variable_types: pollCounter int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
socketSize
unsigned int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: struct pollfd { int fd; short events; short revents; } [10]

variable_types:
addressDescription
char [46]

variable_types: lastTime long

variable_types: currentTime long

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
void __fastcall table_destroy(void *ptr)
{
_QWORD *v2; // r8
__int64 v3; // rbx
__int64 v4; // rdi

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( *(int *)ptr > 0 )
{
v3 = 0LL;
do
{
v4 = v2[v3];
if ( v4 )
{
list_destroy(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
++v3;
}
while ( *(_DWORD *)ptr > (int)v3 );
}
free(v2);
free(ptr);
}
else
{
fprintf(stderr, "%s : %d : \"NULL table\"\n", "table_destroy", 76LL);
}
}

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

if ( ptr )
{
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( *(int *)ptr > 0 )
{
v3 = 0LL;
do
{
v4 = v2[v3];
if ( v4 )
{
list_destroy(v4);
v2 = (_QWORD *)*((_QWORD *)ptr + 2);
}
++v3;
}
while ( *(_DWORD *)ptr > (int)v3 );
}
free(v2);
free(ptr);
}
else
{
fprintf(stderr, "%s : %d : \"NULL table\"\n", "table_destroy", 76LL);
}
}

original_code:
void table_destroy(struct table_t *table) {
    int counter = 0;
    if(table) {
        for(counter = 0; counter < table->hashSize; counter ++) {
            if(table->table[counter]) {
                list_destroy(table->table[counter]);
            }
        }
        free(table->table);
        free(table);
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL table\"""\n", __FUNCTION__, 76);
    }
}

variable_types: struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *

variable_types: counter int


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

if ( a1 )
result = *(unsigned int *)(a1 + 4);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *(unsigned int *)(a1 + 4);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int table_size(struct table_t *table) {
    return (table ? table->numElems : -1);
}


decompiled_code:
void **__fastcall table_get_keys(__int64 a1)
{
__int64 v1; // r13
void **v3; // r14
int v4; // ebx
int v5; // ebp
int v6; // eax
__int64 v7; // r13
const char **v8; // r15
const char **v9; // r13
char *v10; // rax
void **v12; // rbp
void *v13; // rdi
void **v14; // rdi
void **v15; // rbp
void *v16; // rdi
void **v17; // rdi
__int64 v18; // [rsp+0h] [rbp-48h]

if ( !a1 || (v1 = *(int *)(a1 + 4), (v3 = (void **)malloc(8LL * ((int)v1 + 1))) == 0LL) )
{
v3 = 0LL;
fprintf(stderr, "%s : %d : \"NULL table or malloc\"\n", "table_get_keys", 268LL);
return v3;
}
v4 = 0;
v5 = 0;
if ( *(int *)a1 <= 0 )
{
LABEL_20:
v3[v1] = 0LL;
return v3;
}
while ( (int)v1 > v4 )
{
v7 = *(unsigned int *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5) + 16LL);
if ( (int)v7 <= 0 )
goto LABEL_5;
v18 = list_get_keys();
if ( v18 )
{
v8 = (const char **)v18;
v9 = (const char **)(v18 + 8 * v7);
do
{
while ( !*v8 )
{
++v8;
fprintf(stderr, "%s : %d : \"NULL pointers were going to strdup!\"\n", "table_get_keys", 255LL);
if ( v8 == v9 )
goto LABEL_17;
}
v10 = strdup(*v8);
v3[v4] = v10;
if ( v10 )
{
++v4;
}
else
{
fprintf(stderr, "%s : %d : \"strdup\"\n", "table_get_keys", 245LL);
if ( v4 > 0 )
{
v15 = v3;
do
{
v16 = *v15++;
free(v16);
}
while ( &v3[(unsigned int)(v4 - 1) + 1] != v15 );
}
v17 = v3;
v5 = *(_DWORD *)a1;
v3 = 0LL;
free(v17);
}
++v8;
}
while ( v8 != v9 );
LABEL_17:
list_free_keys(v18);
LABEL_5:
v6 = *(_DWORD *)a1;
goto LABEL_6;
}
fprintf(stderr, "%s : %d : \"list_get_keys\"\n", "table_get_keys", 227LL);
if ( v4 > 0 )
{
v12 = v3;
do
{
v13 = *v12++;
free(v13);
}
while ( &v3[(unsigned int)(v4 - 1) + 1] != v12 );
}
v5 = *(_DWORD *)a1;
v14 = v3;
v3 = 0LL;
free(v14);
v6 = v5;
LABEL_6:
if ( ++v5 >= v6 )
break;
LODWORD(v1) = *(_DWORD *)(a1 + 4);
}
if ( v3 )
{
v1 = *(int *)(a1 + 4);
goto LABEL_20;
}
return v3;
}

canonical_decompiled_code:
void **__fastcall func2(__int64 a1)
{
__int64 v1; // r13
void **v3; // r14
int v4; // ebx
int v5; // ebp
int v6; // eax
__int64 v7; // r13
const char **v8; // r15
const char **v9; // r13
char *v10; // rax
void **v12; // rbp
void *v13; // rdi
void **v14; // rdi
void **v15; // rbp
void *v16; // rdi
void **v17; // rdi
__int64 v18; // [rsp+0h] [rbp-48h]

if ( !a1 || (v1 = *(int *)(a1 + 4), (v3 = (void **)malloc(8LL * ((int)v1 + 1))) == 0LL) )
{
v3 = 0LL;
fprintf(stderr, "%s : %d : \"NULL table or malloc\"\n", "table_get_keys", 268LL);
return v3;
}
v4 = 0;
v5 = 0;
if ( *(int *)a1 <= 0 )
{
LABEL_20:
v3[v1] = 0LL;
return v3;
}
while ( (int)v1 > v4 )
{
v7 = *(unsigned int *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5) + 16LL);
if ( (int)v7 <= 0 )
goto LABEL_5;
v18 = list_get_keys();
if ( v18 )
{
v8 = (const char **)v18;
v9 = (const char **)(v18 + 8 * v7);
do
{
while ( !*v8 )
{
++v8;
fprintf(stderr, "%s : %d : \"NULL pointers were going to strdup!\"\n", "table_get_keys", 255LL);
if ( v8 == v9 )
goto LABEL_17;
}
v10 = strdup(*v8);
v3[v4] = v10;
if ( v10 )
{
++v4;
}
else
{
fprintf(stderr, "%s : %d : \"strdup\"\n", "table_get_keys", 245LL);
if ( v4 > 0 )
{
v15 = v3;
do
{
v16 = *v15++;
free(v16);
}
while ( &v3[(unsigned int)(v4 - 1) + 1] != v15 );
}
v17 = v3;
v5 = *(_DWORD *)a1;
v3 = 0LL;
free(v17);
}
++v8;
}
while ( v8 != v9 );
LABEL_17:
list_free_keys(v18);
LABEL_5:
v6 = *(_DWORD *)a1;
goto LABEL_6;
}
fprintf(stderr, "%s : %d : \"list_get_keys\"\n", "table_get_keys", 227LL);
if ( v4 > 0 )
{
v12 = v3;
do
{
v13 = *v12++;
free(v13);
}
while ( &v3[(unsigned int)(v4 - 1) + 1] != v12 );
}
v5 = *(_DWORD *)a1;
v14 = v3;
v3 = 0LL;
free(v14);
v6 = v5;
LABEL_6:
if ( ++v5 >= v6 )
break;
LODWORD(v1) = *(_DWORD *)(a1 + 4);
}
if ( v3 )
{
v1 = *(int *)(a1 + 4);
goto LABEL_20;
}
return v3;
}

original_code:
char **table_get_keys(struct table_t *table) {
    char **keys, **tempKeys;
    int elementsInList, listCounter,
        counter = 0, numMallocs = 0, elementsCopied = 0;
    if(table && (keys = (char**)malloc(sizeof(char*) * (table->numElems + 1)))) {
        for(counter = 0; counter < table->hashSize && (numMallocs < table->numElems); counter++) {
            if((elementsInList = table->table[counter]->elements) > 0) {
                if(!(tempKeys = list_get_keys(table->table[counter]))) {
                    fprintf( stderr, "%s : %d : ""\"list_get_keys\"""\n", __FUNCTION__, 227);
                    for(counter = 0; counter < numMallocs; counter++) {
                        free(keys[counter]);
                    }
                    counter = table->hashSize;
                    free(keys);
                    keys = ((void *)0);
                }
                else {
                    for(listCounter = 0; listCounter < elementsInList; listCounter++) {
                        if(tempKeys[listCounter]) {
                            if((keys[numMallocs] = strdup(tempKeys[listCounter]))) {
                                elementsCopied ++;
                                numMallocs ++;
                            }
                            else {
                                fprintf( stderr, "%s : %d : ""\"strdup\"""\n", __FUNCTION__, 245);
                                for(counter = 0; counter < numMallocs; counter++) {
                                    free(keys[counter]);
                                }
                                counter = table->hashSize;
                                free(keys);
                                keys = ((void *)0);
                            }
                        }
                        else {
                            fprintf( stderr, "%s : %d : ""\"NULL pointers were going to strdup!\"""\n", __FUNCTION__, 255);
                        }
                    }
                    list_free_keys(tempKeys);
                }
            }
        }
        if(keys) {
            keys[table->numElems] = ((void *)0);
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL table or malloc\"""\n", __FUNCTION__, 268);
    }
    return keys;
}

canonical_original_code:
char **table_get_keys(struct table_t *table) {
    char **keys; char **tempKeys;
    int elementsInList; int listCounter; int counter = 0; int numMallocs = 0; int elementsCopied = 0;
    if(table && (keys = (char **)malloc(sizeof(char *) * (table->numElems + 1)))) {
        for(counter = 0; counter < table->hashSize && (numMallocs < table->numElems); counter++) {
            if((elementsInList = table->table[counter]->elements) > 0) {
                if(!(tempKeys = list_get_keys(table->table[counter]))) {
                    fprintf( stderr, "%s : %d : ""\"list_get_keys\"""\n", __FUNCTION__, 227);
                    for(counter = 0; counter < numMallocs; counter++) {
                        free(keys[counter]);
                    }
                    counter = table->hashSize;
                    free(keys);
                    keys = ((void *)0);
                }
                else {
                    for(listCounter = 0; listCounter < elementsInList; listCounter++) {
                        if(tempKeys[listCounter]) {
                            if((keys[numMallocs] = strdup(tempKeys[listCounter]))) {
                                elementsCopied ++;
                                numMallocs ++;
                            }
                            else {
                                fprintf( stderr, "%s : %d : ""\"strdup\"""\n", __FUNCTION__, 245);
                                for(counter = 0; counter < numMallocs; counter++) {
                                    free(keys[counter]);
                                }
                                counter = table->hashSize;
                                free(keys);
                                keys = ((void *)0);
                            }
                        }
                        else {
                            fprintf( stderr, "%s : %d : ""\"NULL pointers were going to strdup!\"""\n", __FUNCTION__, 255);
                        }
                    }
                    list_free_keys(tempKeys);
                }
            }
        }
        if(keys) {
            keys[table->numElems] = ((void *)0);
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL table or malloc\"""\n", __FUNCTION__, 268);
    }
    return keys;
}

variable_types:
keys
char **

variable_types:
tempKeys
char **

variable_types: elementsInList int

variable_types: listCounter int

variable_types: numMallocs int

variable_types: elementsCopied int


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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
}
free(ptr);
}

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

if ( ptr )
{
v2 = *(void **)ptr;
if ( v2 )
{
v3 = (void **)((char *)ptr + 8);
do
{
free(v2);
v2 = *v3++;
}
while ( v2 );
}
}
free(ptr);
}

original_code:
void table_free_keys(char **keylist) {
    int counter = 0;
    if(keylist && keylist[counter]) {
        while(keylist[counter]) {
            free(keylist[counter]);
            counter ++;
        }
    }
    free(keylist);
}

variable_types:
keylist
char **


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

if ( a1 )
result = *(unsigned int *)(a1 + 8);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *(unsigned int *)(a1 + 8);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int table_get_num_updates(struct table_t *table) {
    return (table ? table->numUpdates : -1);
}


decompiled_code:
void __fastcall table_free_entries(void *ptr)
{
_QWORD *v2; // rbx

if ( ptr )
{
if ( *(_QWORD *)ptr )
{
v2 = (char *)ptr + 8;
do
entry_destroy();
while ( *v2++ );
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
_QWORD *v2; // rbx

if ( ptr )
{
if ( *(_QWORD *)ptr )
{
v2 = (char *)ptr + 8;
do
entry_destroy();
while ( *v2++ );
}
free(ptr);
}
}

original_code:
void table_free_entries(struct entry_t **entries) {
    int counter = 0;
    if (entries) {
        while (entries[counter]) {
            entry_destroy(entries[counter]);
            counter++;
        }
        free(entries);
    }
}

variable_types: struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } **


hash

decompiled_code:
__int64 __fastcall hash(const char *a1, int a2)
{
const char *v2; // rbp
int v3; // eax
int v4; // ebx
int v5; // er13
int v6; // eax
__int64 result; // rax

if ( a1 )
{
v2 = a1;
v3 = strlen(a1);
if ( v3 <= 0 )
{
result = 0LL;
}
else
{
v4 = v3 - 1;
v5 = 0;
do
{
v6 = *v2++;
v5 = (int)((double)v5 + pow(31.0, (double)v4) * (double)v6);
}
while ( v4-- != 0 );
result = abs32(v5 % a2);
}
}
else
{
fprintf(stderr, "%s : %d : \"NULL string\"\n", "hash", 389LL);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1, int a2)
{
const char *v2; // rbp
int v3; // eax
int v4; // ebx
int v5; // er13
int v6; // eax
__int64 result; // rax

if ( a1 )
{
v2 = a1;
v3 = strlen(a1);
if ( v3 <= 0 )
{
result = 0LL;
}
else
{
v4 = v3 - 1;
v5 = 0;
do
{
v6 = *v2++;
v5 = (int)((double)v5 + pow(31.0, (double)v4) * (double)v6);
}
while ( v4-- != 0 );
result = abs32(v5 % a2);
}
}
else
{
fprintf(stderr, "%s : %d : \"NULL string\"\n", "hash", 389LL);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int hash(char *string, int mod) {
    int length, counter = 0, result = 0, power;
    if(string) {
        length = (int)strlen(string);
        for(counter = 0; counter < length; counter++) {
            power = length - counter - 1;
            result += (int)string[counter] * pow(31, power);
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL string\"""\n", __FUNCTION__, 389);
    }
    return (string ? abs(result % mod) : -1);
}

canonical_original_code:
int hash(char *string, int mod) {
    int length; int counter = 0; int result = 0; int power;
    if(string) {
        length = (int)strlen(string);
        for(counter = 0; counter < length; counter++) {
            power = length - counter - 1;
            result += (int)string[counter] * pow(31, power);
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL string\"""\n", __FUNCTION__, 389);
    }
    return (string ? abs(result % mod) : -1);
}

variable_types:
string
char *

variable_types: mod int

variable_types: length int

variable_types: result int

variable_types: power int


decompiled_code:
__int64 __fastcall table_put(__int64 a1, const char *a2, __int64 a3)
{
__int64 v4; // r12
__int64 v5; // rax
__int64 v6; // r14
__int64 v7; // rbp
__int64 result; // rax
__int64 v9; // r13
char *v10; // rax
__int64 v11; // rsi

if ( a3 != 0 && a2 != 0LL && a1 )
{
v4 = (int)hash(a2, *(_DWORD *)a1);
if ( *(int *)(a1 + 4) > 0 && (v5 = list_get(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v4), a2), (v6 = v5) != 0) )
{
if ( *(_QWORD *)(v5 + 8) && (v7 = data_dup(a3)) != 0 )
{
data_destroy(*(_QWORD *)(v6 + 8));
*(_QWORD *)(v6 + 8) = v7;
result = 0LL;
++*(_DWORD *)(a1 + 8);
}
else
{
fprintf(stderr, aSDDataDupOuMaI, "table_put", 105LL);
result = 0xFFFFFFFFLL;
}
}
else
{
v9 = data_dup(a3);
v10 = strdup(a2);
v11 = entry_create(v10, v9);
if ( v11 )
{
result = list_add(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * v4), v11);
if ( (_DWORD)result )
{
fprintf(stderr, "%s : %d : \"list_add\"\n", "table_put", 117LL);
result = 0xFFFFFFFFLL;
}
else
{
++*(_DWORD *)(a1 + 4);
++*(_DWORD *)(a1 + 8);
}
}
else
{
fprintf(stderr, "%s : %d : \"entry_create\"\n", "table_put", 122LL);
result = 0xFFFFFFFFLL;
}
}
}
else
{
fprintf(stderr, "%s : %d : \"NULL table, key or data\"\n", "table_put", 128LL);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2, __int64 a3)
{
__int64 v4; // r12
__int64 v5; // rax
__int64 v6; // r14
__int64 v7; // rbp
__int64 result; // rax
__int64 v9; // r13
char *v10; // rax
__int64 v11; // rsi

if ( a3 != 0 && a2 != 0LL && a1 )
{
v4 = (int)func6(a2, *(_DWORD *)a1);
if ( *(int *)(a1 + 4) > 0 && (v5 = list_get(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v4), a2), (v6 = v5) != 0) )
{
if ( *(_QWORD *)(v5 + 8) && (v7 = data_dup(a3)) != 0 )
{
data_destroy(*(_QWORD *)(v6 + 8));
*(_QWORD *)(v6 + 8) = v7;
result = 0LL;
++*(_DWORD *)(a1 + 8);
}
else
{
fprintf(stderr, aSDDataDupOuMaI, "table_put", 105LL);
result = 0xFFFFFFFFLL;
}
}
else
{
v9 = data_dup(a3);
v10 = strdup(a2);
v11 = entry_create(v10, v9);
if ( v11 )
{
result = list_add(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8 * v4), v11);
if ( (_DWORD)result )
{
fprintf(stderr, "%s : %d : \"list_add\"\n", "table_put", 117LL);
result = 0xFFFFFFFFLL;
}
else
{
++*(_DWORD *)(a1 + 4);
++*(_DWORD *)(a1 + 8);
}
}
else
{
fprintf(stderr, "%s : %d : \"entry_create\"\n", "table_put", 122LL);
result = 0xFFFFFFFFLL;
}
}
}
else
{
fprintf(stderr, "%s : %d : \"NULL table, key or data\"\n", "table_put", 128LL);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int table_put(struct table_t *table, char *key, struct data_t *data) {
    int retVal = 0, hashValue;
    struct entry_t *tempEntry;
    struct data_t *tempData;
    if (table && key && data) {
        hashValue = hash(key, table->hashSize);
        if (table->numElems > 0 && (tempEntry = list_get(table->table[hashValue], key))) {
            if (tempEntry->value && (tempData = data_dup(data))) {
                data_destroy(tempEntry->value);
                tempEntry->value = tempData;
                table->numUpdates++;
   } else {
                fprintf( stderr, "%s : %d : ""\"data_dup ou ma inicialização previa\"""\n", __FUNCTION__, 105);
                retVal = -1;
            }
        }
        else {
            if ((tempEntry = entry_create(strdup(key), data_dup(data)))) {
                if ((list_add(table->table[hashValue], tempEntry)) == 0) {
                    table->numElems++;
                    table->numUpdates++;
                }
                else {
                    fprintf( stderr, "%s : %d : ""\"list_add\"""\n", __FUNCTION__, 117);
                    retVal = -1;
                }
            }
            else {
                fprintf( stderr, "%s : %d : ""\"entry_create\"""\n", __FUNCTION__, 122);
                retVal = -1;
            }
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL table, key or data\"""\n", __FUNCTION__, 128);
        retVal = -1;
    }
    return retVal;
}

canonical_original_code:
int table_put(struct table_t *table, char *key, struct data_t *data) {
    int retVal = 0; int hashValue;
    struct entry_t *tempEntry;
    struct data_t *tempData;
    if (table && key && data) {
        hashValue = hash(key, table->hashSize);
        if (table->numElems > 0 && (tempEntry = list_get(table->table[hashValue], key))) {
            if (tempEntry->value && (tempData = data_dup(data))) {
                data_destroy(tempEntry->value);
                tempEntry->value = tempData;
                table->numUpdates++;
   } else {
                fprintf( stderr, "%s : %d : ""\"data_dup ou ma inicialização previa\"""\n", __FUNCTION__, 105);
                retVal = -1;
            }
        }
        else {
            if ((tempEntry = entry_create(strdup(key), data_dup(data)))) {
                if ((list_add(table->table[hashValue], tempEntry)) == 0) {
                    table->numElems++;
                    table->numUpdates++;
                }
                else {
                    fprintf( stderr, "%s : %d : ""\"list_add\"""\n", __FUNCTION__, 117);
                    retVal = -1;
                }
            }
            else {
                fprintf( stderr, "%s : %d : ""\"entry_create\"""\n", __FUNCTION__, 122);
                retVal = -1;
            }
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL table, key or data\"""\n", __FUNCTION__, 128);
        retVal = -1;
    }
    return retVal;
}

variable_types:
key
char *

variable_types:
data
struct data_t *

variable_types: retVal int

variable_types: hashValue int

variable_types: struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *


decompiled_code:
__int64 __fastcall table_get(__int64 a1, const char *a2)
{
int v2; // eax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rbx
int *v6; // rax
void *v7; // rdi
const void *v8; // rsi
__int64 result; // rax
__int64 v10; // rdi

if ( a2 )
{
v2 = hash(a2, *(_DWORD *)a1);
v3 = 0LL;
v4 = list_get(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v2), a2);
v5 = v4;
if ( v4 )
{
v3 = data_create(**(unsigned int **)(v4 + 8));
if ( v3 )
{
v6 = *(int **)(v5 + 8);
v7 = *(void **)(v3 + 8);
v8 = (const void *)*((_QWORD *)v6 + 1);
*(_QWORD *)(v3 + 16) = *((_QWORD *)v6 + 2);
memcpy(v7, v8, *v6);
if ( memcmp(*(const void **)(v3 + 8), *(const void **)(*(_QWORD *)(v5 + 8) + 8LL), **(int **)(v5 + 8)) )
{
fprintf(stderr, "%s : %d : \"memcpy tempEntry->value->data\"\n", "table_get", 161LL);
v10 = v3;
v3 = 0LL;
data_destroy(v10);
}
}
else
{
fprintf(stderr, "%s : %d : \"data_create\"\n", "table_get", 169LL);
}
}
result = v3;
}
else
{
fprintf(stderr, "%s : %d : \"NULL key\"\n", "table_get", 174LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2)
{
int v2; // eax
__int64 v3; // r12
__int64 v4; // rax
__int64 v5; // rbx
int *v6; // rax
void *v7; // rdi
const void *v8; // rsi
__int64 result; // rax
__int64 v10; // rdi

if ( a2 )
{
v2 = func6(a2, *(_DWORD *)a1);
v3 = 0LL;
v4 = list_get(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v2), a2);
v5 = v4;
if ( v4 )
{
v3 = data_create(**(unsigned int **)(v4 + 8));
if ( v3 )
{
v6 = *(int **)(v5 + 8);
v7 = *(void **)(v3 + 8);
v8 = (const void *)*((_QWORD *)v6 + 1);
*(_QWORD *)(v3 + 16) = *((_QWORD *)v6 + 2);
memcpy(v7, v8, *v6);
if ( memcmp(*(const void **)(v3 + 8), *(const void **)(*(_QWORD *)(v5 + 8) + 8LL), **(int **)(v5 + 8)) )
{
fprintf(stderr, "%s : %d : \"memcpy tempEntry->value->data\"\n", "table_get", 161LL);
v10 = v3;
v3 = 0LL;
data_destroy(v10);
}
}
else
{
fprintf(stderr, "%s : %d : \"data_create\"\n", "table_get", 169LL);
}
}
result = v3;
}
else
{
fprintf(stderr, "%s : %d : \"NULL key\"\n", "table_get", 174LL);
result = 0LL;
}
return result;
}

original_code:
struct data_t *table_get(struct table_t *table, char *key) {
    struct data_t *tempData = ((void *)0);
    struct entry_t *tempEntry = ((void *)0);
    int hashValue;
    if (key) {
        hashValue = hash(key, table->hashSize);
        if((tempEntry = list_get(table->table[hashValue], key))) {
            if((tempData = data_create(tempEntry->value->datasize))) {
    tempData->timestamp = tempEntry->value->timestamp;
                memcpy(tempData->data, tempEntry->value->data, tempEntry->value->datasize);
                if((memcmp(tempData->data, tempEntry->value->data, tempEntry->value->datasize) != 0)) {
                    fprintf( stderr, "%s : %d : ""\"memcpy tempEntry->value->data\"""\n", __FUNCTION__, 161);
                    if (tempData) {
                        data_destroy(tempData);
                    }
                    tempData = ((void *)0);
                }
            }
            else {
                fprintf( stderr, "%s : %d : ""\"data_create\"""\n", __FUNCTION__, 169);
            }
        }
    }
    else {
        fprintf( stderr, "%s : %d : ""\"NULL key\"""\n", __FUNCTION__, 174);
    }
    return tempData;
}

variable_types: struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *

variable_types: struct data_t { int datasize; void *data; long timestamp; } *


decompiled_code:
__int64 __fastcall table_del(__int64 a1, const char *a2)
{
__int64 v2; // r12
int v3; // eax
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 16);
v3 = hash(a2, *(_DWORD *)a1);
result = list_remove(*(_QWORD *)(v2 + 8LL * v3), a2);
if ( !(_DWORD)result )
{
--*(_DWORD *)(a1 + 4);
++*(_DWORD *)(a1 + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, const char *a2)
{
__int64 v2; // r12
int v3; // eax
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 16);
v3 = func6(a2, *(_DWORD *)a1);
result = list_remove(*(_QWORD *)(v2 + 8LL * v3), a2);
if ( !(_DWORD)result )
{
--*(_DWORD *)(a1 + 4);
++*(_DWORD *)(a1 + 8);
}
return result;
}

original_code:
int table_del(struct table_t *table, char *key) {
    int result = -1;
 if(table) {
            result = list_remove(table->table[hash(key,table->hashSize)], key);
 }
    if(result == 0) {
        table->numElems--;
        table->numUpdates++;
    }
    return result;
}

variable_types: struct table_t { int hashSize; int numElems; int numUpdates; struct list_t { struct node_t { struct node_t *prev; struct node_t *next; struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; } *head; struct node_t *tail; int elements; } **table; } *


decompiled_code:
__int64 __fastcall table_get_ts(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // r12

if ( a1 && a2 )
{
v2 = table_get(a1, a2);
v3 = 0LL;
if ( v2 )
{
v3 = *(_QWORD *)(v2 + 16);
data_destroy(v2);
}
}
else
{
v3 = -1LL;
fprintf(stderr, "%s : %d : \"NULL table or key\"\n", "table_get_ts", 405LL);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, const char *a2)
{
__int64 v2; // rax
__int64 v3; // r12

if ( a1 && a2 )
{
v2 = func8(a1, a2);
v3 = 0LL;
if ( v2 )
{
v3 = *(_QWORD *)(v2 + 16);
data_destroy(v2);
}
}
else
{
v3 = -1LL;
fprintf(stderr, "%s : %d : \"NULL table or key\"\n", "table_get_ts", 405LL);
}
return v3;
}

original_code:
long table_get_ts(struct table_t *table, char *key) {
    if(table == ((void *)0) || key == ((void *)0)) {
        fprintf( stderr, "%s : %d : ""\"NULL table or key\"""\n", __FUNCTION__, 405);
        return -1;
    }
    struct data_t *tempData;
    if((tempData = table_get(table, key)) == ((void *)0)) {
        return 0;
    }
    long ts = tempData->timestamp;
    data_destroy(tempData);
    return ts;
}

variable_types: ts long


decompiled_code:
void qa_pipe_handler()
{
pthread_t v0; // rdx
_QWORD *v1; // rax
_QWORD *v2; // rcx
__int64 v3; // rbp
char v4; // bl

signal(13, qa_pipe_handler);
pthread_mutex_lock(&connections_lock);
v0 = pthread_self();
v1 = connections;
do
{
v2 = v1;
v1 += 2;
}
while ( *(v1 - 2) != v0 );
v3 = v2[1];
pthread_mutex_unlock(&connections_lock);
while ( 1 )
{
pthread_mutex_lock(&check_quit);
v4 = quit_and_cleanup;
pthread_mutex_unlock(&check_quit);
if ( (unsigned int)rtable_connect(v3) != -1 )
break;
if ( v4 )
goto LABEL_8;
sleep(5u);
}
if ( v4 )
LABEL_8:
pthread_exit(0LL);
}

canonical_decompiled_code:
void func0()
{
pthread_t v0; // rdx
_QWORD *v1; // rax
_QWORD *v2; // rcx
__int64 v3; // rbp
char v4; // bl

signal(13, qa_pipe_handler);
pthread_mutex_lock(&connections_lock);
v0 = pthread_self();
v1 = connections;
do
{
v2 = v1;
v1 += 2;
}
while ( *(v1 - 2) != v0 );
v3 = v2[1];
pthread_mutex_unlock(&connections_lock);
while ( 1 )
{
pthread_mutex_lock(&check_quit);
v4 = quit_and_cleanup;
pthread_mutex_unlock(&check_quit);
if ( (unsigned int)rtable_connect(v3) != -1 )
break;
if ( v4 )
goto LABEL_8;
sleep(5u);
}
if ( v4 )
LABEL_8:
pthread_exit(0LL);
}

original_code:
void *qa_pipe_handler(sig_t sig) {
 int i = 0;
 struct rtable_t *table;
 _Bool quit;
 signal(13, (__sighandler_t)qa_pipe_handler);
 pthread_mutex_lock(&connections_lock);
 while(connections[i].self != pthread_self()) {
  i++;
 }
 table = connections[i].table;
 pthread_mutex_unlock(&connections_lock);
 pthread_mutex_lock(&check_quit);
 quit = quit_and_cleanup;
 pthread_mutex_unlock(&check_quit);
 while(rtable_connect(table) == -1 && !quit) {
  sleep(5);
  pthread_mutex_lock(&check_quit);
  quit = quit_and_cleanup;
  pthread_mutex_unlock(&check_quit);
 }
 if(quit) {
  pthread_exit(((void *)0));
 }
}

canonical_original_code:
void *qa_pipe_handler(void (*sig)(int)) {
 int i = 0;
 struct rtable_t *table;
 _Bool quit;
 signal(13, (void (*)(int))qa_pipe_handler);
 pthread_mutex_lock(&connections_lock);
 while(connections[i].self != pthread_self()) {
  i++;
 }
 table = connections[i].table;
 pthread_mutex_unlock(&connections_lock);
 pthread_mutex_lock(&check_quit);
 quit = quit_and_cleanup;
 pthread_mutex_unlock(&check_quit);
 while(rtable_connect(table) == -1 && !quit) {
  sleep(5);
  pthread_mutex_lock(&check_quit);
  quit = quit_and_cleanup;
  pthread_mutex_unlock(&check_quit);
 }
 if(quit) {
  pthread_exit(((void *)0));
 }
}

variable_types:
sig
void (*)(int)

variable_types: i int

variable_types: struct rtable_t { int socket; char *ip; char *porto; } *

return_type: void *


decompiled_code:
void __fastcall task_destroy(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

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

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void **v1; // rbx
void **v2; // rbp

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

original_code:
void task_destroy(struct task_t *tasks) {
 struct task_t *temp;
 while(tasks) {
  temp = tasks->next;
  free(tasks->task);
  free(tasks);
  tasks = temp;
 }
}

variable_types: struct task_t { struct quorum_op_t { int id; int sender; int opcode; union content_u { struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; long timestamp; char *key; char **keys; struct data_t *value; int result; } content; } *task; struct task_t *next; int id; } *

user_defined_types: struct task_t { struct quorum_op_t *task; struct task_t *next; int id; }

user_defined_types: struct quorum_op_t { int id; int sender; int opcode; union content_u content; }

user_defined_types: union content_u { struct entry_t *entry; long timestamp; char *key; char **keys; struct data_t *value; int result; }


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

if ( pthread_mutex_init(&queue_lock, 0LL)
|| pthread_cond_init(&queue_not_empty, 0LL)
|| pthread_mutex_init(&done_lock, 0LL)
|| pthread_cond_init(&done_not_empty, 0LL)
|| (result = pthread_mutex_init(&check_quit, 0LL)) != 0 )
{
fprintf(stderr, "%s : %d : \"queue init\"\n", "queue_init", 352LL);
exit(-1);
}
return result;
}

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

if ( pthread_mutex_init(&queue_lock, 0LL)
|| pthread_cond_init(&queue_not_empty, 0LL)
|| pthread_mutex_init(&done_lock, 0LL)
|| pthread_cond_init(&done_not_empty, 0LL)
|| (result = pthread_mutex_init(&check_quit, 0LL)) != 0 )
{
fprintf(stderr, "%s : %d : \"queue init\"\n", "queue_init", 352LL);
exit(-1);
}
return result;
}

original_code:
void queue_init() {
 if(pthread_mutex_init(&queue_lock, ((void *)0)) != 0 || pthread_cond_init(&queue_not_empty, ((void *)0)) != 0
    || pthread_mutex_init(&done_lock, ((void *)0)) != 0 || pthread_cond_init(&done_not_empty, ((void *)0)) != 0
    || pthread_mutex_init(&check_quit, ((void *)0)) != 0) {
  fprintf( stderr, "%s : %d : ""\"queue init\"""\n", __FUNCTION__, 352);
  exit(-1);
 }
}


decompiled_code:
int __fastcall init_thread_pool(__int64 a1, int a2, void *(*a3)(void *))
{
__int64 v4; // r12
int result; // eax
__int64 i; // rbx
int v7; // esi
__int64 v8; // rbp
__int64 v9; // rdi
_DWORD *v10; // rax

v4 = a2;
result = queue_init();
if ( a2 > 0 )
{
for ( i = 0LL; i != v4; ++i )
{
v7 = i;
v8 = 8 * i;
v9 = *(_QWORD *)(*((_QWORD *)shared_quorum + 1) + 8 * i);
v10 = qa_table(v9, v7);
result = pthread_create((pthread_t *)(v8 + *(_QWORD *)shared_quorum), 0LL, a3, v10);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, int a2, void *(*a3)(void *))
{
__int64 v4; // r12
int result; // eax
__int64 i; // rbx
int v7; // esi
__int64 v8; // rbp
__int64 v9; // rdi
_DWORD *v10; // rax

v4 = a2;
result = func3();
if ( a2 > 0 )
{
for ( i = 0LL; i != v4; ++i )
{
v7 = i;
v8 = 8 * i;
v9 = *(_QWORD *)(*((_QWORD *)shared_quorum + 1) + 8 * i);
v10 = qa_table(v9, v7);
result = pthread_create((pthread_t *)(v8 + *(_QWORD *)shared_quorum), 0LL, a3, v10);
}
}
return result;
}

original_code:
void init_thread_pool(pthread_t *pool, int n_threads, void *(*tfunc)(void*)) {
 int i;
 struct qa_table_t *table;
 queue_init();
 for(i = 0; i < n_threads; i ++) {
  table = qa_table(shared_quorum->tables[i], i);
  pthread_create(&shared_quorum->pool[i], ((void *)0), tfunc, table);
 }
}

canonical_original_code:
void init_thread_pool(unsigned long *pool, int n_threads, void *(*tfunc)(void *)) {
 int i;
 struct qa_table_t *table;
 queue_init();
 for(i = 0; i < n_threads; i ++) {
  table = qa_table(shared_quorum->tables[i], i);
  pthread_create(&shared_quorum->pool[i], ((void *)0), tfunc, table);
 }
}

variable_types:
pool
unsigned long *

variable_types:
tfunc
void *(*)(void *)

variable_types: struct qa_table_t { struct rtable_t { int socket; char *ip; char *porto; } *table; int id; } *

user_defined_types: struct qa_table_t { struct rtable_t *table; int id; }


decompiled_code:
__int64 __fastcall init_quorum_access(__int64 a1, int a2)
{
_QWORD *v2; // r12
void *v3; // rax
__int64 v4; // r13
_QWORD *v5; // rax
_QWORD *v6; // rdx
__int64 result; // rax
void *v8; // rbp

if ( shared_quorum )
{
fprintf(stderr, "%s : %d : \"init_quorum_access: already called.\"\n", "init_quorum_access", 63LL);
result = 0xFFFFFFFFLL;
}
else
{
shared_quorum = malloc(0x18uLL);
v2 = shared_quorum;
if ( shared_quorum )
{
v3 = malloc(8LL * a2);
*v2 = v3;
v4 = (__int64)v3;
if ( v3 )
{
v5 = malloc(16LL * a2);
connections = v5;
if ( v5 )
{
v6 = &v5[2 * (unsigned int)a2];
if ( a2 > 0 )
{
do
{
v5[1] = 0LL;
v5 += 2;
*(v5 - 2) = 0LL;
}
while ( v5 != v6 );
}
v2[1] = a1;
*((_DWORD *)v2 + 4) = a2;
init_thread_pool(v4, a2, (void *(*)(void *))worker_thread_function);
result = 0LL;
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 47LL);
v8 = shared_quorum;
free(*(void **)shared_quorum);
free(v8);
result = 0xFFFFFFFFLL;
shared_quorum = 0LL;
}
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 42LL);
free(shared_quorum);
result = 0xFFFFFFFFLL;
shared_quorum = 0LL;
}
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 40LL);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
_QWORD *v2; // r12
void *v3; // rax
__int64 v4; // r13
_QWORD *v5; // rax
_QWORD *v6; // rdx
__int64 result; // rax
void *v8; // rbp

if ( shared_quorum )
{
fprintf(stderr, "%s : %d : \"init_quorum_access: already called.\"\n", "init_quorum_access", 63LL);
result = 0xFFFFFFFFLL;
}
else
{
shared_quorum = malloc(0x18uLL);
v2 = shared_quorum;
if ( shared_quorum )
{
v3 = malloc(8LL * a2);
*v2 = v3;
v4 = (__int64)v3;
if ( v3 )
{
v5 = malloc(16LL * a2);
connections = v5;
if ( v5 )
{
v6 = &v5[2 * (unsigned int)a2];
if ( a2 > 0 )
{
do
{
v5[1] = 0LL;
v5 += 2;
*(v5 - 2) = 0LL;
}
while ( v5 != v6 );
}
v2[1] = a1;
*((_DWORD *)v2 + 4) = a2;
func4(v4, a2, (void *(*)(void *))worker_thread_function);
result = 0LL;
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 47LL);
v8 = shared_quorum;
free(*(void **)shared_quorum);
free(v8);
result = 0xFFFFFFFFLL;
shared_quorum = 0LL;
}
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 42LL);
free(shared_quorum);
result = 0xFFFFFFFFLL;
shared_quorum = 0LL;
}
}
else
{
fprintf(stderr, "%s : %d : \"malloc\"\n", "init_quorum_access", 40LL);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int init_quorum_access(struct rtable_t **rtable, int n) {
 int ret = -1, i;
 if(!shared_quorum) {
  if(!(shared_quorum = malloc(sizeof(struct quorum_access_t)))) {
   fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 40);
  } else if(!(shared_quorum->pool = malloc(sizeof(pthread_t) * n))) {
   fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 42);
   free(shared_quorum);
   shared_quorum = ((void *)0);
  } else {
   if(!(connections = malloc(sizeof(struct connection_t) * n))) {
    fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 47);
    free(shared_quorum->pool);
    free(shared_quorum);
    shared_quorum = ((void *)0);
   } else {
    for(i = 0; i < n; i ++) {
     connections[i].table = ((void *)0);
     connections[i].self = 0;
    }
    shared_quorum->tables = rtable;
    shared_quorum->n_threads = n;
    init_thread_pool(shared_quorum->pool, n, worker_thread_function);
    ret = 0;
   }
  }
 } else {
  fprintf( stderr, "%s : %d : ""\"init_quorum_access: already called.\"""\n", __FUNCTION__, 63);
 }
 return ret;
}

canonical_original_code:
int init_quorum_access(struct rtable_t **rtable, int n) {
 int ret = -1; int i;
 if(!shared_quorum) {
  if(!(shared_quorum = malloc(sizeof(struct quorum_access_t)))) {
   fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 40);
  } else if(!(shared_quorum->pool = malloc(sizeof(pthread_t) * n))) {
   fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 42);
   free(shared_quorum);
   shared_quorum = ((void *)0);
  } else {
   if(!(connections = malloc(sizeof(struct connection_t) * n))) {
    fprintf( stderr, "%s : %d : ""\"malloc\"""\n", __FUNCTION__, 47);
    free(shared_quorum->pool);
    free(shared_quorum);
    shared_quorum = ((void *)0);
   } else {
    for(i = 0; i < n; i ++) {
     connections[i].table = ((void *)0);
     connections[i].self = 0;
    }
    shared_quorum->tables = rtable;
    shared_quorum->n_threads = n;
    init_thread_pool(shared_quorum->pool, n, worker_thread_function);
    ret = 0;
   }
  }
 } else {
  fprintf( stderr, "%s : %d : ""\"init_quorum_access: already called.\"""\n", __FUNCTION__, 63);
 }
 return ret;
}

variable_types: struct rtable_t { int socket; char *ip; char *porto; } **

variable_types: n int

variable_types: ret int

user_defined_types: struct quorum_access_t { unsigned long *pool; struct rtable_t **tables; int n_threads; }

user_defined_types: struct connection_t { unsigned long self; struct rtable_t *table; }


decompiled_code:
int queue_destroy()
{
pthread_mutex_destroy(&queue_lock);
pthread_mutex_destroy(&done_lock);
pthread_mutex_destroy(&check_quit);
pthread_cond_destroy(&done_not_empty);
return pthread_cond_destroy(&queue_not_empty);
}

canonical_decompiled_code:
int func6()
{
pthread_mutex_destroy(&queue_lock);
pthread_mutex_destroy(&done_lock);
pthread_mutex_destroy(&check_quit);
pthread_cond_destroy(&done_not_empty);
return pthread_cond_destroy(&queue_not_empty);
}

original_code:
void queue_destroy() {
 pthread_mutex_destroy(&queue_lock);
 pthread_mutex_destroy(&done_lock);
 pthread_mutex_destroy(&check_quit);
 pthread_cond_destroy(&done_not_empty);
 pthread_cond_destroy(&queue_not_empty);
}


decompiled_code:
__int64 destroy_quorum_access()
{
void **v0; // rbp
int v1; // ebx
int v2; // eax
__int64 v3; // rbx
pthread_t v4; // rdi
__int64 result; // rax

pthread_mutex_lock(&check_quit);
quit_and_cleanup = 1;
pthread_mutex_unlock(&check_quit);
v0 = (void **)shared_quorum;
if ( *((int *)shared_quorum + 4) > 0 )
{
v1 = 0;
do
{
++v1;
pthread_cond_signal(&done_not_empty);
pthread_cond_signal(&queue_not_empty);
v0 = (void **)shared_quorum;
v2 = *((_DWORD *)shared_quorum + 4);
}
while ( v2 > v1 );
if ( v2 > 0 )
{
v3 = 0LL;
do
{
v4 = *((_QWORD *)*v0 + v3++);
pthread_join(v4, 0LL);
v0 = (void **)shared_quorum;
}
while ( *((_DWORD *)shared_quorum + 4) > (int)v3 );
}
}
task_destroy(queue_head);
task_destroy(done);
queue_destroy();
free(*v0);
free(v0);
free(connections);
result = 1LL;
shared_quorum = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
void **v0; // rbp
int v1; // ebx
int v2; // eax
__int64 v3; // rbx
pthread_t v4; // rdi
__int64 result; // rax

pthread_mutex_lock(&check_quit);
quit_and_cleanup = 1;
pthread_mutex_unlock(&check_quit);
v0 = (void **)shared_quorum;
if ( *((int *)shared_quorum + 4) > 0 )
{
v1 = 0;
do
{
++v1;
pthread_cond_signal(&done_not_empty);
pthread_cond_signal(&queue_not_empty);
v0 = (void **)shared_quorum;
v2 = *((_DWORD *)shared_quorum + 4);
}
while ( v2 > v1 );
if ( v2 > 0 )
{
v3 = 0LL;
do
{
v4 = *((_QWORD *)*v0 + v3++);
pthread_join(v4, 0LL);
v0 = (void **)shared_quorum;
}
while ( *((_DWORD *)shared_quorum + 4) > (int)v3 );
}
}
func1(queue_head);
func1(done);
func6();
free(*v0);
free(v0);
free(connections);
result = 1LL;
shared_quorum = 0LL;
return result;
}

original_code:
int destroy_quorum_access() {
 int i;
 pthread_mutex_lock(&check_quit);
 quit_and_cleanup = 1;
 pthread_mutex_unlock(&check_quit);
 for(i = 0; i < shared_quorum->n_threads; i++) {
  pthread_cond_signal(&done_not_empty);
  pthread_cond_signal(&queue_not_empty);
 }
 for(i = 0; i < shared_quorum->n_threads; i++) {
  pthread_join(shared_quorum->pool[i], ((void *)0));
 }
 task_destroy(queue_head);
 task_destroy(done);
 queue_destroy();
 free(shared_quorum->pool);
 free(shared_quorum);
 free(connections);
 shared_quorum = ((void *)0);
 return 1;
}


decompiled_code:
int __fastcall queue_add_task(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx

pthread_mutex_lock(&queue_lock);
v1 = queue_head;
if ( queue_head )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v2[1] = a1;
}
else
{
queue_head = a1;
}
a1[1] = 0LL;
pthread_cond_signal(&queue_not_empty);
return pthread_mutex_unlock(&queue_lock);
}

canonical_decompiled_code:
int __fastcall func8(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx

pthread_mutex_lock(&queue_lock);
v1 = queue_head;
if ( queue_head )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v2[1] = a1;
}
else
{
queue_head = a1;
}
a1[1] = 0LL;
pthread_cond_signal(&queue_not_empty);
return pthread_mutex_unlock(&queue_lock);
}

original_code:
void queue_add_task(struct task_t *task) {
 pthread_mutex_lock(&queue_lock);
 if(!queue_head) {
  queue_head = task;
 } else {
  struct task_t *ptr = queue_head;
  while(ptr->next) {
   ptr = ptr->next;
  }
  ptr->next = task;
 }
 task->next = ((void *)0);
 pthread_cond_signal(&queue_not_empty);
 pthread_mutex_unlock(&queue_lock);
}


decompiled_code:
_QWORD *queue_get_task()
{
_QWORD *v0; // r12
char v1; // bl

pthread_mutex_lock(&queue_lock);
v0 = queue_head;
if ( queue_head )
goto LABEL_6;
do
{
pthread_cond_wait(&queue_not_empty, &queue_lock);
pthread_mutex_lock(&check_quit);
v1 = quit_and_cleanup;
pthread_mutex_unlock(&check_quit);
v0 = queue_head;
if ( queue_head )
break;
}
while ( !v1 );
if ( v1 )
v0 = 0LL;
else
LABEL_6:
queue_head = (void *)v0[1];
pthread_mutex_unlock(&queue_lock);
return v0;
}

canonical_decompiled_code:
_QWORD *func9()
{
_QWORD *v0; // r12
char v1; // bl

pthread_mutex_lock(&queue_lock);
v0 = queue_head;
if ( queue_head )
goto LABEL_6;
do
{
pthread_cond_wait(&queue_not_empty, &queue_lock);
pthread_mutex_lock(&check_quit);
v1 = quit_and_cleanup;
pthread_mutex_unlock(&check_quit);
v0 = queue_head;
if ( queue_head )
break;
}
while ( !v1 );
if ( v1 )
v0 = 0LL;
else
LABEL_6:
queue_head = (void *)v0[1];
pthread_mutex_unlock(&queue_lock);
return v0;
}

original_code:
struct task_t *queue_get_task() {
 _Bool quit = 0;
 struct task_t *task = ((void *)0);
 pthread_mutex_lock(&queue_lock);
 while(!queue_head && !quit) {
  pthread_cond_wait(&queue_not_empty, &queue_lock);
  pthread_mutex_lock(&check_quit);
  quit = quit_and_cleanup;
  pthread_mutex_unlock(&check_quit);
 }
 if(!quit) {
  task = queue_head;
  queue_head = queue_head->next;
 }
 pthread_mutex_unlock(&queue_lock);
 return task;
}

return_type: struct task_t { struct quorum_op_t { int id; int sender; int opcode; union content_u { struct entry_t { char *key; struct data_t { int datasize; void *data; long timestamp; } *value; } *entry; long timestamp; char *key; char **keys; struct data_t *value; int result; } content; } *task; struct task_t *next; int id; } *


decompiled_code:
int __fastcall add_completed_task(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx

pthread_mutex_lock(&done_lock);
v1 = done;
if ( done )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v2[1] = a1;
}
else
{
done = a1;
}
a1[1] = 0LL;
pthread_cond_signal(&done_not_empty);
return pthread_mutex_unlock(&done_lock);
}

canonical_decompiled_code:
int __fastcall func10(_QWORD *a1)
{
_QWORD *v1; // rax
_QWORD *v2; // rdx

pthread_mutex_lock(&done_lock);
v1 = done;
if ( done )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
}
while ( v1 );
v2[1] = a1;
}
else
{
done = a1;
}
a1[1] = 0LL;
pthread_cond_signal(&done_not_empty);
return pthread_mutex_unlock(&done_lock);
}

original_code:
void add_completed_task(struct task_t *task) {
 pthread_mutex_lock(&done_lock);
 if(!done) {
  done = task;
 } else {
  struct task_t *ptr = done;
  while(ptr->next) {
   ptr = ptr->next;
  }
  ptr->next = task;
 }
 task->next = ((void *)0);
 pthread_cond_signal(&done_not_empty);
 pthread_mutex_unlock(&done_lock);
}