decompiled_code:
double __fastcall stats_numpaths(__int64 a1, int a2, void *a3)
{
size_t v4; // r12
void *v5; // rax
_QWORD *v6; // rbp
double result; // xmm0_8
unsigned int v8; // [rsp+Ch] [rbp-4Ch] BYREF
int v9; // [rsp+10h] [rbp-48h] BYREF
double v10; // [rsp+18h] [rbp-40h] BYREF
void *src; // [rsp+20h] [rbp-38h]
void *ptr; // [rsp+28h] [rbp-30h]

v8 = a2;
v9 = a2;
v10 = 0.0;
ptr = 0LL;
v4 = (unsigned int)graph_num_nodes();
v5 = calloc(v4, 8uLL);
src = v5;
if ( v5 )
{
v6 = v5;
ptr = calloc(v4, 1uLL);
if ( !ptr )
goto LABEL_9;
v6[v8] = LC0;
if ( !(unsigned __int8)bfs(a1, &v8, 1LL, 0LL, &v9) )
{
if ( a3 )
memcpy(a3, src, 8 * v4);
stats_cache_add(a1, 19LL, 2LL, 8LL);
stats_cache_add(a1, 23LL, 3LL, 8LL);
stats_cache_update(a1, 19LL, v8, -1LL, &v10);
stats_cache_update(a1, 23LL, v8, -1LL, src);
free(src);
free(ptr);
return v10;
}
v6 = src;
if ( src )
LABEL_9:
free(v6);
}
result = 0.0;
if ( ptr )
{
free(ptr);
result = 0.0;
}
return result;
}

canonical_decompiled_code:
double __fastcall func2(__int64 a1, int a2, void *a3)
{
size_t v4; // r12
void *v5; // rax
_QWORD *v6; // rbp
double result; // xmm0_8
unsigned int v8; // [rsp+Ch] [rbp-4Ch] BYREF
int v9; // [rsp+10h] [rbp-48h] BYREF
double v10; // [rsp+18h] [rbp-40h] BYREF
void *src; // [rsp+20h] [rbp-38h]
void *ptr; // [rsp+28h] [rbp-30h]

v8 = a2;
v9 = a2;
v10 = 0.0;
ptr = 0LL;
v4 = (unsigned int)graph_num_nodes();
v5 = calloc(v4, 8uLL);
src = v5;
if ( v5 )
{
v6 = v5;
ptr = calloc(v4, 1uLL);
if ( !ptr )
goto LABEL_9;
v6[v8] = LC0;
if ( !(unsigned __int8)bfs(a1, &v8, 1LL, 0LL, &v9) )
{
if ( a3 )
memcpy(a3, src, 8 * v4);
stats_cache_add(a1, 19LL, 2LL, 8LL);
stats_cache_add(a1, 23LL, 3LL, 8LL);
stats_cache_update(a1, 19LL, v8, -1LL, &v10);
stats_cache_update(a1, 23LL, v8, -1LL, src);
free(src);
free(ptr);
return v10;
}
v6 = src;
if ( src )
LABEL_9:
free(v6);
}
result = 0.0;
if ( ptr )
{
free(ptr);
result = 0.0;
}
return result;
}

original_code:
double stats_numpaths(graph_t *g, uint32_t nidx, double *numpaths) {
  bfs_ctx_t ctx;
  uint32_t nnodes;
  ctx.root = nidx;
  ctx.total = 0;
  ctx.numpaths = ((void *)0);
  ctx.visited = ((void *)0);
  nnodes = graph_num_nodes(g);
  ctx.numpaths = calloc(nnodes, sizeof(double));
  if (ctx.numpaths == ((void *)0)) goto fail;
  ctx.visited = calloc(nnodes, sizeof(uint8_t));
  if (ctx.visited == ((void *)0)) goto fail;
  ctx.numpaths[nidx] = 1;
  if (bfs(g, &nidx, 1, ((void *)0), &ctx, &ctx, _bfs_cb, _exp_cb)) goto fail;
  if (numpaths != ((void *)0))
    memcpy(numpaths, ctx.numpaths, nnodes*sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_NODE_NUMPATHS,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_PAIR_NUMPATHS,
                  STATS_CACHE_TYPE_PAIR,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_NODE_NUMPATHS, nidx, -1, &(ctx.total));
  stats_cache_update(g, STATS_CACHE_PAIR_NUMPATHS, nidx, -1, ctx.numpaths);
  free(ctx.numpaths);
  free(ctx.visited);
  return ctx.total;
fail:
  if (ctx.numpaths != ((void *)0)) free(ctx.numpaths);
  if (ctx.visited != ((void *)0)) free(ctx.visited);
  return 0;
}

canonical_original_code:
double stats_numpaths(struct _graph *g, unsigned int nidx, double *numpaths) {
  struct _bfs_ctx ctx;
  unsigned int nnodes;
  ctx.root = nidx;
  ctx.total = 0;
  ctx.numpaths = ((void *)0);
  ctx.visited = ((void *)0);
  nnodes = graph_num_nodes(g);
  ctx.numpaths = calloc(nnodes, sizeof(double));
  if (ctx.numpaths == ((void *)0)) goto fail;
  ctx.visited = calloc(nnodes, sizeof(unsigned char));
  if (ctx.visited == ((void *)0)) goto fail;
  ctx.numpaths[nidx] = 1;
  if (bfs(g, &nidx, 1, ((void *)0), &ctx, &ctx, _bfs_cb, _exp_cb)) goto fail;
  if (numpaths != ((void *)0))
    memcpy(numpaths, ctx.numpaths, nnodes*sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_NODE_NUMPATHS,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_PAIR_NUMPATHS,
                  STATS_CACHE_TYPE_PAIR,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_NODE_NUMPATHS, nidx, -1, &(ctx.total));
  stats_cache_update(g, STATS_CACHE_PAIR_NUMPATHS, nidx, -1, ctx.numpaths);
  free(ctx.numpaths);
  free(ctx.visited);
  return ctx.total;
fail:
  if (ctx.numpaths != ((void *)0)) free(ctx.numpaths);
  if (ctx.visited != ((void *)0)) free(ctx.visited);
  return 0;
}

variable_types: struct _graph { unsigned int numnodes; unsigned int numedges; struct _array { unsigned int capacity; unsigned int size; unsigned int datasz; unsigned char *data; int (*cmp)(void *a, void *b); int (*cmpins)(void *a, void *b); } numneighbours; struct _array nodelabels; struct _array labelvals; struct _array *neighbours; struct _array *weights; unsigned short flags; struct _array event_listeners; void *ctx[5]; void (*ctx_free[5])(void *); } *

variable_types:
nidx
unsigned int

variable_types:
numpaths
double *

variable_types: struct _bfs_ctx { unsigned int root; double total; double *numpaths; unsigned char *visited; }

variable_types:
nnodes
unsigned int


repo: pauldmccarthy/ccnet

decompiled_code:
double __fastcall stats_edge_pathsharing(__int64 a1, unsigned int a2, unsigned int a3)
{
double result; // xmm0_8
int v5; // er15
unsigned int v6; // ebx
__int64 v7; // r14
__int64 v8; // rbp
__int64 v9; // rbx
__int64 i; // rbp
_DWORD *v11; // rbx
double v12; // [rsp+8h] [rbp-80h]
double v13; // [rsp+10h] [rbp-78h]
__int64 v14; // [rsp+18h] [rbp-70h]
__int64 v15; // [rsp+20h] [rbp-68h]
unsigned int v16; // [rsp+2Ch] [rbp-5Ch]
__int64 v17; // [rsp+38h] [rbp-50h]
double v18[8]; // [rsp+48h] [rbp-40h] BYREF

v16 = a3;
result = 0.0;
if ( a2 != a3 )
{
result = 0.0;
if ( (unsigned __int8)graph_are_neighbours(0.0) )
{
v5 = graph_num_neighbours(a1, a2, 0.0);
v6 = graph_num_neighbours(a1, a3, 0.0);
v7 = graph_get_neighbours(a1, a2);
v15 = graph_get_neighbours(a1, a3);
v17 = graph_get_weights(a1, a2);
v8 = graph_get_weights(a1, a3);
v13 = (double)(int)(v6 * v5);
v14 = v6;
if ( v6 )
{
v12 = 0.0;
v9 = 0LL;
do
{
if ( *(_DWORD *)(v15 + 4 * v9) == a2 )
{
result = *(float *)(v8 + 4 * v9) + v12;
v12 = result;
}
else if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = *(float *)(v8 + 4 * v9) + v12;
v12 = result;
}
++v9;
}
while ( v9 != v14 );
}
else
{
v12 = 0.0;
}
if ( v5 )
{
for ( i = 0LL; i != v5; ++i )
{
if ( *(_DWORD *)(v7 + 4 * i) != v16 )
{
if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = *(float *)(v17 + 4 * i) + v12;
v12 = result;
}
if ( v14 )
{
v11 = (_DWORD *)v15;
do
{
if ( *v11 != a2 )
{
if ( *v11 == *(_DWORD *)(v7 + 4 * i) )
{
v13 = v13 - 1.0;
}
else if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = graph_get_weight(a1, *(unsigned int *)(v7 + 4 * i), (unsigned int)*v11) + v12;
v12 = result;
}
}
++v11;
}
while ( v11 != (_DWORD *)(v15 + 4 * v14) );
}
}
}
}
v18[0] = v12 / v13;
stats_cache_add(a1, 24LL, 4LL, 8LL);
stats_cache_update(a1, 24LL, a2, v16, v18);
result = v18[0];
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3)
{
double result; // xmm0_8
int v5; // er15
unsigned int v6; // ebx
__int64 v7; // r14
__int64 v8; // rbp
__int64 v9; // rbx
__int64 i; // rbp
_DWORD *v11; // rbx
double v12; // [rsp+8h] [rbp-80h]
double v13; // [rsp+10h] [rbp-78h]
__int64 v14; // [rsp+18h] [rbp-70h]
__int64 v15; // [rsp+20h] [rbp-68h]
unsigned int v16; // [rsp+2Ch] [rbp-5Ch]
__int64 v17; // [rsp+38h] [rbp-50h]
double v18[8]; // [rsp+48h] [rbp-40h] BYREF

v16 = a3;
result = 0.0;
if ( a2 != a3 )
{
result = 0.0;
if ( (unsigned __int8)graph_are_neighbours(0.0) )
{
v5 = graph_num_neighbours(a1, a2, 0.0);
v6 = graph_num_neighbours(a1, a3, 0.0);
v7 = graph_get_neighbours(a1, a2);
v15 = graph_get_neighbours(a1, a3);
v17 = graph_get_weights(a1, a2);
v8 = graph_get_weights(a1, a3);
v13 = (double)(int)(v6 * v5);
v14 = v6;
if ( v6 )
{
v12 = 0.0;
v9 = 0LL;
do
{
if ( *(_DWORD *)(v15 + 4 * v9) == a2 )
{
result = *(float *)(v8 + 4 * v9) + v12;
v12 = result;
}
else if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = *(float *)(v8 + 4 * v9) + v12;
v12 = result;
}
++v9;
}
while ( v9 != v14 );
}
else
{
v12 = 0.0;
}
if ( v5 )
{
for ( i = 0LL; i != v5; ++i )
{
if ( *(_DWORD *)(v7 + 4 * i) != v16 )
{
if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = *(float *)(v17 + 4 * i) + v12;
v12 = result;
}
if ( v14 )
{
v11 = (_DWORD *)v15;
do
{
if ( *v11 != a2 )
{
if ( *v11 == *(_DWORD *)(v7 + 4 * i) )
{
v13 = v13 - 1.0;
}
else if ( (unsigned __int8)graph_are_neighbours(result) )
{
result = graph_get_weight(a1, *(unsigned int *)(v7 + 4 * i), (unsigned int)*v11) + v12;
v12 = result;
}
}
++v11;
}
while ( v11 != (_DWORD *)(v15 + 4 * v14) );
}
}
}
}
v18[0] = v12 / v13;
stats_cache_add(a1, 24LL, 4LL, 8LL);
stats_cache_update(a1, 24LL, a2, v16, v18);
result = v18[0];
}
}
return result;
}

original_code:
double stats_edge_pathsharing(graph_t *g, uint32_t u, uint32_t v) {
  uint64_t i;
  uint64_t j;
  uint32_t nunbrs;
  uint32_t nvnbrs;
  uint32_t *unbrs;
  float *uwts;
  uint32_t *vnbrs;
  float *vwts;
  double count;
  double divisor;
  double ps;
  if (u == v) return 0;
  if (!graph_are_neighbours(g, u, v)) return 0;
  nunbrs = graph_num_neighbours(g, u);
  nvnbrs = graph_num_neighbours(g, v);
  unbrs = graph_get_neighbours(g, u);
  vnbrs = graph_get_neighbours(g, v);
  uwts = graph_get_weights (g, u);
  vwts = graph_get_weights (g, v);
  count = 0;
  divisor = (nunbrs) * (nvnbrs);
  for (j = 0; j < nvnbrs; j++) {
    if (vnbrs[j] == u) {count += vwts[j]; continue;}
    if (graph_are_neighbours(g, u, vnbrs[j])) count += vwts[j];
  }
  for (i = 0; i < nunbrs; i++) {
    if (unbrs[i] == v) continue;
    if (graph_are_neighbours(g, v, unbrs[i])) count += uwts[i];
    for (j = 0; j < nvnbrs; j++) {
      if (vnbrs[j] == u) continue;
      if (unbrs[i] == vnbrs[j]) {divisor -= 1; continue;}
      if (graph_are_neighbours(g, unbrs[i], vnbrs[j]))
        count += graph_get_weight(g, unbrs[i], vnbrs[j]);
    }
  }
  ps = count / divisor;
  stats_cache_add(g,
                  STATS_CACHE_EDGE_PATHSHARING,
                  STATS_CACHE_TYPE_EDGE,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_EDGE_PATHSHARING, u, v, &ps);
  return ps;
}

canonical_original_code:
double stats_edge_pathsharing(struct _graph *g, unsigned int u, unsigned int v) {
  unsigned long i;
  unsigned long j;
  unsigned int nunbrs;
  unsigned int nvnbrs;
  unsigned int *unbrs;
  float *uwts;
  unsigned int *vnbrs;
  float *vwts;
  double count;
  double divisor;
  double ps;
  if (u == v) return 0;
  if (!graph_are_neighbours(g, u, v)) return 0;
  nunbrs = graph_num_neighbours(g, u);
  nvnbrs = graph_num_neighbours(g, v);
  unbrs = graph_get_neighbours(g, u);
  vnbrs = graph_get_neighbours(g, v);
  uwts = graph_get_weights (g, u);
  vwts = graph_get_weights (g, v);
  count = 0;
  divisor = (nunbrs) * (nvnbrs);
  for (j = 0; j < nvnbrs; j++) {
    if (vnbrs[j] == u) {count += vwts[j]; continue;}
    if (graph_are_neighbours(g, u, vnbrs[j])) count += vwts[j];
  }
  for (i = 0; i < nunbrs; i++) {
    if (unbrs[i] == v) continue;
    if (graph_are_neighbours(g, v, unbrs[i])) count += uwts[i];
    for (j = 0; j < nvnbrs; j++) {
      if (vnbrs[j] == u) continue;
      if (unbrs[i] == vnbrs[j]) {divisor -= 1; continue;}
      if (graph_are_neighbours(g, unbrs[i], vnbrs[j]))
        count += graph_get_weight(g, unbrs[i], vnbrs[j]);
    }
  }
  ps = count / divisor;
  stats_cache_add(g,
                  STATS_CACHE_EDGE_PATHSHARING,
                  STATS_CACHE_TYPE_EDGE,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_EDGE_PATHSHARING, u, v, &ps);
  return ps;
}

variable_types:
u
unsigned int

variable_types:
v
unsigned int

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
nunbrs
unsigned int

variable_types:
nvnbrs
unsigned int

variable_types:
unbrs
unsigned int *

variable_types:
uwts
float *

variable_types:
vnbrs
unsigned int *

variable_types:
vwts
float *

variable_types: count double

variable_types: divisor double

variable_types: ps double

return_type: double

user_defined_types: struct _graph { unsigned int numnodes; unsigned int numedges; struct _array numneighbours; struct _array nodelabels; struct _array labelvals; struct _array *neighbours; struct _array *weights; unsigned short flags; struct _array event_listeners; void *ctx[5]; void (*ctx_free[5])(void *); }

user_defined_types: struct _array { unsigned int capacity; unsigned int size; unsigned int datasz; unsigned char *data; int (*cmp)(void *a, void *b); int (*cmpins)(void *a, void *b); }


decompiled_code:
__int64 __fastcall graph_get_component(__int64 a1, int a2, __int64 a3)
{
size_t v4; // rbp
void *v5; // rax
void *v6; // rbx
unsigned int v7; // er14
unsigned __int64 v8; // rax
unsigned __int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v4 = (unsigned int)graph_num_nodes();
v5 = calloc(v4, 4uLL);
if ( !v5 )
return (unsigned int)-1;
v6 = v5;
v7 = stats_cache_node_component(a1, -1LL, v5);
if ( !(_BYTE)v7 )
{
v10[0] = 0LL;
if ( !v4 )
{
LABEL_10:
free(v6);
return v7;
}
v8 = 0LL;
while ( *((_DWORD *)v6 + v8) != a2 || !(unsigned __int8)array_append(a3, v10) )
{
v8 = v10[0] + 1;
v10[0] = v8;
if ( v4 <= v8 )
goto LABEL_10;
}
}
free(v6);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, __int64 a3)
{
size_t v4; // rbp
void *v5; // rax
void *v6; // rbx
unsigned int v7; // er14
unsigned __int64 v8; // rax
unsigned __int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v4 = (unsigned int)graph_num_nodes();
v5 = calloc(v4, 4uLL);
if ( !v5 )
return (unsigned int)-1;
v6 = v5;
v7 = stats_cache_node_component(a1, -1LL, v5);
if ( !(_BYTE)v7 )
{
v10[0] = 0LL;
if ( !v4 )
{
LABEL_10:
free(v6);
return v7;
}
v8 = 0LL;
while ( *((_DWORD *)v6 + v8) != a2 || !(unsigned __int8)array_append(a3, v10) )
{
v8 = v10[0] + 1;
v10[0] = v8;
if ( v4 <= v8 )
goto LABEL_10;
}
}
free(v6);
return (unsigned int)-1;
}

original_code:
uint8_t graph_get_component(graph_t *g, uint32_t cmp, array_t *nodeids) {
  uint64_t i;
  uint32_t nnodes;
  uint32_t *cmpnums;
  cmpnums = ((void *)0);
  nnodes = graph_num_nodes(g);
  cmpnums = calloc(nnodes, sizeof(uint32_t));
  if (cmpnums == ((void *)0)) goto fail;
  if (stats_cache_node_component(g, -1, cmpnums)) goto fail;
  for (i = 0; i < nnodes; i++) {
    if (cmpnums[i] != cmp) continue;
    if (array_append(nodeids, &i)) goto fail;
  }
  free(cmpnums);
  return 0;
fail:
  if (cmpnums != ((void *)0)) free(cmpnums);
  return -1;
}

canonical_original_code:
unsigned char graph_get_component(struct _graph *g, unsigned int cmp, struct _array *nodeids) {
  unsigned long i;
  unsigned int nnodes;
  unsigned int *cmpnums;
  cmpnums = ((void *)0);
  nnodes = graph_num_nodes(g);
  cmpnums = calloc(nnodes, sizeof(unsigned int));
  if (cmpnums == ((void *)0)) goto fail;
  if (stats_cache_node_component(g, -1, cmpnums)) goto fail;
  for (i = 0; i < nnodes; i++) {
    if (cmpnums[i] != cmp) continue;
    if (array_append(nodeids, &i)) goto fail;
  }
  free(cmpnums);
  return 0;
fail:
  if (cmpnums != ((void *)0)) free(cmpnums);
  return -1;
}

variable_types: struct _graph { unsigned int numnodes; unsigned int numedges; struct _array { unsigned int capacity; unsigned int size; unsigned int datasz; unsigned char *data; int (*cmp)(void *a, void *b); int (*cmpins)(void *a, void *b); } numneighbours; struct _array nodelabels; struct _array labelvals; struct _array *neighbours; struct _array *weights; unsigned short flags; struct _array event_listeners; void *ctx[5]; void (*ctx_free[5])(void *); } *

variable_types:
cmp
unsigned int

variable_types:
nodeids
struct _array *

variable_types:
cmpnums
unsigned int *

return_type: unsigned char


decompiled_code:
double __fastcall stats_assortativity(__int64 a1)
{
unsigned int i; // er12
unsigned int *v2; // rbx
__int64 v3; // rsi
int v4; // eax
int v5; // ebp
__int64 v6; // rax
__int64 v7; // r13
double v8; // xmm2_8
double v10; // [rsp+0h] [rbp-68h]
double v11; // [rsp+8h] [rbp-60h]
double v12; // [rsp+10h] [rbp-58h]
int v13; // [rsp+18h] [rbp-50h]
int v14; // [rsp+1Ch] [rbp-4Ch]
double v15[8]; // [rsp+28h] [rbp-40h] BYREF

v13 = graph_num_nodes();
v14 = graph_num_edges(a1);
if ( v13 )
{
v12 = 0.0;
v11 = 0.0;
v10 = 0.0;
for ( i = 0; i != v13; ++i )
{
v5 = graph_num_neighbours(a1, i);
v6 = graph_get_neighbours(a1, i);
if ( v5 )
{
v2 = (unsigned int *)v6;
v7 = v6 + 4LL * (unsigned int)(v5 - 1) + 4;
do
{
v3 = *v2;
if ( (unsigned int)v3 > i )
{
v4 = graph_num_neighbours(a1, v3);
v10 = (double)(v4 * v5) + v10;
v11 = (double)(v5 + v4) * 0.5 + v11;
v12 = (double)(v5 * v5 + v4 * v4) * 0.5 + v12;
}
++v2;
}
while ( v2 != (unsigned int *)v7 );
}
}
}
else
{
v12 = 0.0;
v11 = 0.0;
v10 = 0.0;
}
v8 = v11 / (double)v14 * (v11 / (double)v14);
v15[0] = (v10 / (double)v14 - v8) / (v12 / (double)v14 - v8);
stats_cache_add(a1, 4LL, 0LL, 8LL);
stats_cache_update(a1, 4LL, 0LL, -1LL, v15);
return v15[0];
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1)
{
unsigned int i; // er12
unsigned int *v2; // rbx
__int64 v3; // rsi
int v4; // eax
int v5; // ebp
__int64 v6; // rax
__int64 v7; // r13
double v8; // xmm2_8
double v10; // [rsp+0h] [rbp-68h]
double v11; // [rsp+8h] [rbp-60h]
double v12; // [rsp+10h] [rbp-58h]
int v13; // [rsp+18h] [rbp-50h]
int v14; // [rsp+1Ch] [rbp-4Ch]
double v15[8]; // [rsp+28h] [rbp-40h] BYREF

v13 = graph_num_nodes();
v14 = graph_num_edges(a1);
if ( v13 )
{
v12 = 0.0;
v11 = 0.0;
v10 = 0.0;
for ( i = 0; i != v13; ++i )
{
v5 = graph_num_neighbours(a1, i);
v6 = graph_get_neighbours(a1, i);
if ( v5 )
{
v2 = (unsigned int *)v6;
v7 = v6 + 4LL * (unsigned int)(v5 - 1) + 4;
do
{
v3 = *v2;
if ( (unsigned int)v3 > i )
{
v4 = graph_num_neighbours(a1, v3);
v10 = (double)(v4 * v5) + v10;
v11 = (double)(v5 + v4) * 0.5 + v11;
v12 = (double)(v5 * v5 + v4 * v4) * 0.5 + v12;
}
++v2;
}
while ( v2 != (unsigned int *)v7 );
}
}
}
else
{
v12 = 0.0;
v11 = 0.0;
v10 = 0.0;
}
v8 = v11 / (double)v14 * (v11 / (double)v14);
v15[0] = (v10 / (double)v14 - v8) / (v12 / (double)v14 - v8);
stats_cache_add(a1, 4LL, 0LL, 8LL);
stats_cache_update(a1, 4LL, 0LL, -1LL, v15);
return v15[0];
}

original_code:
double stats_assortativity(graph_t *g) {
  uint32_t j;
  uint32_t k;
  uint32_t numnodes;
  uint32_t numedges;
  uint32_t *nbrsj;
  uint32_t degj;
  uint32_t degk;
  double r;
  double jkprod;
  double jkhalfsum;
  double jkhalfsumsq;
  jkprod = 0;
  jkhalfsum = 0;
  jkhalfsumsq = 0;
  numnodes = graph_num_nodes(g);
  numedges = graph_num_edges(g);
  for (j = 0; j < numnodes; j++) {
    degj = graph_num_neighbours(g, j);
    nbrsj = graph_get_neighbours(g, j);
    for (k = 0; k < degj; k++) {
      if (nbrsj[k] <= j) continue;
      degk = graph_num_neighbours(g, nbrsj[k]);
      jkprod += degj * degk;
      jkhalfsum += 0.5 * (degj + degk);
      jkhalfsumsq += 0.5 * ((degj * degj) + (degk * degk));
    }
  }
  r = (jkprod / numedges) - (pow(jkhalfsum / numedges, 2));
  r /= (jkhalfsumsq / numedges) - (pow(jkhalfsum / numedges, 2));
  stats_cache_add(g,
                  STATS_CACHE_ASSORTATIVITY,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_ASSORTATIVITY, 0, -1, &r);
  return r;
}

canonical_original_code:
double stats_assortativity(struct _graph *g) {
  unsigned int j;
  unsigned int k;
  unsigned int numnodes;
  unsigned int numedges;
  unsigned int *nbrsj;
  unsigned int degj;
  unsigned int degk;
  double r;
  double jkprod;
  double jkhalfsum;
  double jkhalfsumsq;
  jkprod = 0;
  jkhalfsum = 0;
  jkhalfsumsq = 0;
  numnodes = graph_num_nodes(g);
  numedges = graph_num_edges(g);
  for (j = 0; j < numnodes; j++) {
    degj = graph_num_neighbours(g, j);
    nbrsj = graph_get_neighbours(g, j);
    for (k = 0; k < degj; k++) {
      if (nbrsj[k] <= j) continue;
      degk = graph_num_neighbours(g, nbrsj[k]);
      jkprod += degj * degk;
      jkhalfsum += 0.5 * (degj + degk);
      jkhalfsumsq += 0.5 * ((degj * degj) + (degk * degk));
    }
  }
  r = (jkprod / numedges) - (pow(jkhalfsum / numedges, 2));
  r /= (jkhalfsumsq / numedges) - (pow(jkhalfsum / numedges, 2));
  stats_cache_add(g,
                  STATS_CACHE_ASSORTATIVITY,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_ASSORTATIVITY, 0, -1, &r);
  return r;
}

variable_types:
j
unsigned int

variable_types:
k
unsigned int

variable_types:
numnodes
unsigned int

variable_types:
numedges
unsigned int

variable_types:
nbrsj
unsigned int *

variable_types:
degj
unsigned int

variable_types:
degk
unsigned int

variable_types: r double

variable_types: jkprod double

variable_types: jkhalfsum double

variable_types: jkhalfsumsq double


expand

decompiled_code:
__int64 __fastcall expand(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, unsigned __int8 (__fastcall *a6)(char *, __int64))
{
unsigned int v8; // er14
int *v9; // rbx
int v11; // er13
__int64 v12; // rax
__int64 v13; // r13
char v16[4]; // [rsp+20h] [rbp-48h] BYREF
unsigned int v17; // [rsp+24h] [rbp-44h]
char v18; // [rsp+28h] [rbp-40h]
unsigned int v19[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

if ( !*(_DWORD *)(a2 + 4) )
return 0LL;
v8 = 0;
while ( 1 )
{
array_get(a2, v8, v19);
v11 = graph_num_neighbours(a1, v19[0]);
v12 = graph_get_neighbours(a1, v19[0]);
if ( v11 )
break;
LABEL_9:
if ( *(_DWORD *)(a2 + 4) <= ++v8 )
return 0LL;
}
v9 = (int *)v12;
v13 = v12 + 4LL * (unsigned int)(v11 - 1) + 4;
while ( 1 )
{
if ( a6 )
{
array_get(a2, v8, v16);
v17 = *v9;
v18 = *(_BYTE *)(a4 + v17);
if ( a6(v16, a5) )
return 1LL;
}
if ( !*(_BYTE *)(a4 + (unsigned int)*v9) )
{
array_append(a3, v9);
*(_BYTE *)(a4 + (unsigned int)*v9) = 1;
}
if ( ++v9 == (int *)v13 )
goto LABEL_9;
}
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, unsigned __int8 (__fastcall *a6)(char *, __int64))
{
unsigned int v8; // er14
int *v9; // rbx
int v11; // er13
__int64 v12; // rax
__int64 v13; // r13
char v16[4]; // [rsp+20h] [rbp-48h] BYREF
unsigned int v17; // [rsp+24h] [rbp-44h]
char v18; // [rsp+28h] [rbp-40h]
unsigned int v19[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

if ( !*(_DWORD *)(a2 + 4) )
return 0LL;
v8 = 0;
while ( 1 )
{
array_get(a2, v8, v19);
v11 = graph_num_neighbours(a1, v19[0]);
v12 = graph_get_neighbours(a1, v19[0]);
if ( v11 )
break;
LABEL_9:
if ( *(_DWORD *)(a2 + 4) <= ++v8 )
return 0LL;
}
v9 = (int *)v12;
v13 = v12 + 4LL * (unsigned int)(v11 - 1) + 4;
while ( 1 )
{
if ( a6 )
{
array_get(a2, v8, v16);
v17 = *v9;
v18 = *(_BYTE *)(a4 + v17);
if ( a6(v16, a5) )
return 1LL;
}
if ( !*(_BYTE *)(a4 + (unsigned int)*v9) )
{
array_append(a3, v9);
*(_BYTE *)(a4 + (unsigned int)*v9) = 1;
}
if ( ++v9 == (int *)v13 )
goto LABEL_9;
}
}

original_code:
uint8_t expand(
  graph_t *g,
  array_t *thislevel,
  array_t *nextlevel,
  uint8_t *visited,
  void *context,
  uint8_t (*callback) (
    expand_state_t *state,
    void *context
  ))
{
  uint32_t i;
  uint32_t j;
  uint32_t ni;
  uint32_t nneighbours;
  uint32_t *neighbours;
  expand_state_t state;
  neighbours = ((void *)0);
  for (i = 0; i < thislevel->size; i++) {
    array_get(thislevel, i, &ni);
    nneighbours = graph_num_neighbours(g, ni);
    neighbours = graph_get_neighbours(g, ni);
    for (j = 0; j < nneighbours; j++) {
      if (callback != ((void *)0)) {
        array_get(thislevel, i, &(state.parent));
        state.child = neighbours[j];
        state.visited = visited[neighbours[j]];
        if (callback(&state, context)) {
          i = thislevel->size;
          return 1;
        }
      }
      if (!visited[neighbours[j]]) {
        array_append(nextlevel, &(neighbours[j]));
        visited[neighbours[j]] = 1;
      }
    }
  }
  return 0;
}

canonical_original_code:
unsigned char expand(struct _graph *g, struct _array *thislevel, struct _array *nextlevel, unsigned char *visited, void *context, unsigned char (*callback)(struct _expand_state *state, void *context))
{
  unsigned int i;
  unsigned int j;
  unsigned int ni;
  unsigned int nneighbours;
  unsigned int *neighbours;
  struct _expand_state state;
  neighbours = ((void *)0);
  for (i = 0; i < thislevel->size; i++) {
    array_get(thislevel, i, &ni);
    nneighbours = graph_num_neighbours(g, ni);
    neighbours = graph_get_neighbours(g, ni);
    for (j = 0; j < nneighbours; j++) {
      if (callback != ((void *)0)) {
        array_get(thislevel, i, &(state.parent));
        state.child = neighbours[j];
        state.visited = visited[neighbours[j]];
        if (callback(&state, context)) {
          i = thislevel->size;
          return 1;
        }
      }
      if (!visited[neighbours[j]]) {
        array_append(nextlevel, &(neighbours[j]));
        visited[neighbours[j]] = 1;
      }
    }
  }
  return 0;
}

variable_types:
thislevel
struct _array *

variable_types:
nextlevel
struct _array *

variable_types:
visited
unsigned char *

variable_types:
context
void *

variable_types: unsigned char (*)(struct _expand_state { unsigned int parent; unsigned int child; unsigned char visited; } *state, void *context)

variable_types:
i
unsigned int

variable_types:
ni
unsigned int

variable_types:
nneighbours
unsigned int

variable_types:
neighbours
unsigned int *


main

canonical_name: main

repo: paulgriffiths/basicnetworking

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

set_logging(1LL, argv, envp);
chatc_ui_initialize_screen();
chatc_main_loop();
chatc_ui_free_screen();
set_logging(0LL, argv, v3);
return 0;
}

original_code:
int main(void) {
    set_logging(1);
    chatc_ui_initialize_screen();
    chatc_main_loop();
    chatc_ui_free_screen();
    set_logging(0);
    return 0;
}

canonical_original_code:
int main() {
    set_logging(1);
    chatc_ui_initialize_screen();
    chatc_main_loop();
    chatc_ui_free_screen();
    set_logging(0);
    return 0;
}

return_type: int


decompiled_code:
const char *__fastcall trim_line_ending(const char *a1)
{
int v1; // edx
const char *v2; // rax

if ( *a1 )
{
v1 = strlen(a1);
if ( v1 - 1 >= 0 )
{
v2 = &a1[v1 - 1];
do
{
if ( *v2 != 10 && *v2 != 13 )
break;
*v2-- = 0;
}
while ( v2 != &a1[v1 - 2 - (v1 - 1)] );
}
}
return a1;
}

original_code:
char * trim_line_ending(char * buffer) {
    if ( *buffer != '\0' ) {
        int last;
        for ( last = (int) strlen(buffer) - 1;
              last >= 0 && ( buffer[last] == '\n' || buffer[last] == '\r' );
              --last ) {
            buffer[last] = '\0';
        }
    }
    return buffer;
}

canonical_original_code:
char *trim_line_ending(char *buffer) {
    if ( *buffer != '\0' ) {
        int last;
        for ( last = (int) strlen(buffer) - 1;
              last >= 0 && ( buffer[last] == '\n' || buffer[last] == '\r' );
              --last ) {
            buffer[last] = '\0';
        }
    }
    return buffer;
}

variable_types:
buffer
char *

variable_types: last int

return_type: char *


original_code:
char * trim_right(char * buffer) {
    if ( *buffer != '\0' ) {
        int last;
        for ( last = (int) strlen(buffer) - 1;
              last >= 0 && ((*__ctype_b_loc ())[(int) ((buffer[last]))] & (unsigned short int) _ISspace);
              --last ) {
            buffer[last] = '\0';
        }
    }
    return buffer;
}

canonical_original_code:
char *trim_right(char *buffer) {
    if ( *buffer != '\0' ) {
        int last;
        for ( last = (int) strlen(buffer) - 1;
              last >= 0 && ((*__ctype_b_loc ())[(int) ((buffer[last]))] & (unsigned short) _ISspace);
              --last ) {
            buffer[last] = '\0';
        }
    }
    return buffer;
}


trim

decompiled_code:
char *__fastcall trim(char *a1)
{
trim_left(a1);
trim_right(a1);
return a1;
}

canonical_decompiled_code:
char *__fastcall func2(char *a1)
{
trim_left(a1);
func1(a1);
return a1;
}

original_code:
char * trim(char * buffer) {
    (void) trim_left(buffer);
    (void) trim_right(buffer);
    return buffer;
}

canonical_original_code:
char *trim(char *buffer) {
    (void) trim_left(buffer);
    (void) trim_right(buffer);
    return buffer;
}


decompiled_code:
int chatc_ui_free_screen()
{
msgwin_destroy(qword_218);
qword_218 = 0LL;
sliwin_destroy(qword_220);
qword_220 = 0LL;
delwin(mainwin);
mainwin = 0LL;
curs_set(dword_210);
endwin();
return wrefresh(stdscr);
}

canonical_decompiled_code:
int func0()
{
msgwin_destroy(qword_218);
qword_218 = 0LL;
sliwin_destroy(qword_220);
qword_220 = 0LL;
delwin(mainwin);
mainwin = 0LL;
curs_set(dword_210);
endwin();
return wrefresh(stdscr);
}

original_code:
void chatc_ui_free_screen(void) {
    msgwin_destroy(mainwin.msgwin);
    mainwin.msgwin = ((void *)0);
    sliwin_destroy(mainwin.inpwin);
    mainwin.inpwin = ((void *)0);
    delwin(mainwin.window);
    mainwin.window = ((void *)0);
    curs_set(mainwin.old_cursor);
    endwin();
    wrefresh(stdscr);
}

canonical_original_code:
void chatc_ui_free_screen() {
    msgwin_destroy(mainwin.msgwin);
    mainwin.msgwin = ((void *)0);
    sliwin_destroy(mainwin.inpwin);
    mainwin.inpwin = ((void *)0);
    delwin(mainwin.window);
    mainwin.window = ((void *)0);
    curs_set(mainwin.old_cursor);
    endwin();
    wrefresh(stdscr);
}

return_type: void


decompiled_code:
__int64 chatc_term_rows()
{
return (unsigned __int16)word_208;
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned __int16)word_208;
}

original_code:
int chatc_term_rows(void) {
    return mainwin.ws.ws_row;
}

canonical_original_code:
int chatc_term_rows() {
    return mainwin.ws.ws_row;
}


decompiled_code:
__int64 chatc_term_cols()
{
return (unsigned __int16)word_20A;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned __int16)word_20A;
}

original_code:
int chatc_term_cols(void) {
    return mainwin.ws.ws_col;
}

canonical_original_code:
int chatc_term_cols() {
    return mainwin.ws.ws_col;
}


decompiled_code:
__int64 chatc_ui_initialize_screen()
{
unsigned int v0; // ebx
__int64 v1; // rbp

mainwin = initscr();
if ( !mainwin )
{
perror("chat-client: couldn't initialize screen");
exit(1);
}
if ( ioctl(0, 0x5413uLL, &word_208) == -1 )
{
perror("chat-client: couldn't get terminal size");
exit(1);
}
noecho();
keypad(mainwin, 1);
dword_210 = curs_set(0);
wrefresh(stdscr);
v0 = chatc_term_rows();
v1 = (unsigned int)chatc_term_cols();
v0 -= 3;
qword_218 = msgwin_create(mainwin, 0LL, 0LL, v1, v0);
qword_220 = sliwin_create(mainwin, 0LL, v0, (unsigned int)v1, 3LL);
msgwin_refresh(qword_218);
return sliwin_refresh(qword_220);
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx
__int64 v1; // rbp

mainwin = initscr();
if ( !mainwin )
{
perror("chat-client: couldn't initialize screen");
exit(1);
}
if ( ioctl(0, 0x5413uLL, &word_208) == -1 )
{
perror("chat-client: couldn't get terminal size");
exit(1);
}
noecho();
keypad(mainwin, 1);
dword_210 = curs_set(0);
wrefresh(stdscr);
v0 = func1();
v1 = (unsigned int)func2();
v0 -= 3;
qword_218 = msgwin_create(mainwin, 0LL, 0LL, v1, v0);
qword_220 = sliwin_create(mainwin, 0LL, v0, (unsigned int)v1, 3LL);
msgwin_refresh(qword_218);
return sliwin_refresh(qword_220);
}

original_code:
void chatc_ui_initialize_screen(void) {
    if ( (mainwin.window = initscr()) == ((void *)0) ) {
        perror("chat-client: couldn't initialize screen");
        exit(1);
    }
    if ( ioctl(0, 0x5413, &mainwin.ws) == -1 ) {
        perror("chat-client: couldn't get terminal size");
        exit(1);
    }
    noecho();
    keypad(mainwin.window, 1);
    mainwin.old_cursor = curs_set(0);
    wrefresh(stdscr);
    const int iwh = 3;
    int rows = chatc_term_rows();
    int cols = chatc_term_cols();
    mainwin.msgwin = msgwin_create(mainwin.window, 0, 0, cols, rows - iwh);
    mainwin.inpwin = sliwin_create(mainwin.window, 0, rows - iwh, cols, iwh);
    msgwin_refresh(mainwin.msgwin);
    sliwin_refresh(mainwin.inpwin);
}

canonical_original_code:
void chatc_ui_initialize_screen() {
    if ( (mainwin.window = initscr()) == ((void *)0) ) {
        perror("chat-client: couldn't initialize screen");
        exit(1);
    }
    if ( ioctl(0, 0x5413, &mainwin.ws) == -1 ) {
        perror("chat-client: couldn't get terminal size");
        exit(1);
    }
    noecho();
    keypad(mainwin.window, 1);
    mainwin.old_cursor = curs_set(0);
    wrefresh(stdscr);
    int iwh = 3;
    int rows = chatc_term_rows();
    int cols = chatc_term_cols();
    mainwin.msgwin = msgwin_create(mainwin.window, 0, 0, cols, rows - iwh);
    mainwin.inpwin = sliwin_create(mainwin.window, 0, rows - iwh, cols, iwh);
    msgwin_refresh(mainwin.msgwin);
    sliwin_refresh(mainwin.inpwin);
}

variable_types: iwh int

variable_types: rows int

variable_types: cols int


decompiled_code:
__int64 __fastcall chatc_ui_print_message(__int64 a1)
{
msgwin_print_message(qword_218, a1);
return msgwin_refresh(qword_218);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
msgwin_print_message(qword_218, a1);
return msgwin_refresh(qword_218);
}

original_code:
void chatc_ui_print_message(const char * msg) {
    msgwin_print_message(mainwin.msgwin, msg);
    msgwin_refresh(mainwin.msgwin);
}

canonical_original_code:
void chatc_ui_print_message(char *msg) {
    msgwin_print_message(mainwin.msgwin, msg);
    msgwin_refresh(mainwin.msgwin);
}

variable_types:
msg
char *


decompiled_code:
__int64 chatc_ui_process_input()
{
return sliwin_get_char(qword_220);
}

canonical_decompiled_code:
__int64 func5()
{
return sliwin_get_char(qword_220);
}

original_code:
void chatc_ui_process_input(void) {
    sliwin_get_char(mainwin.inpwin);
}

canonical_original_code:
void chatc_ui_process_input() {
    sliwin_get_char(mainwin.inpwin);
}


decompiled_code:
__int64 chatc_ui_input_ready()
{
return sliwin_message_ready(qword_220);
}

canonical_decompiled_code:
__int64 func6()
{
return sliwin_message_ready(qword_220);
}

original_code:
_Bool chatc_ui_input_ready(void) {
    return sliwin_message_ready(mainwin.inpwin);
}

canonical_original_code:
_Bool chatc_ui_input_ready() {
    return sliwin_message_ready(mainwin.inpwin);
}


decompiled_code:
__int64 chatc_ui_retrieve_input()
{
return sliwin_get_contents(qword_220);
}

canonical_decompiled_code:
__int64 func7()
{
return sliwin_get_contents(qword_220);
}

original_code:
const char * chatc_ui_retrieve_input(void) {
    return sliwin_get_contents(mainwin.inpwin);
}

canonical_original_code:
char *chatc_ui_retrieve_input() {
    return sliwin_get_contents(mainwin.inpwin);
}


decompiled_code:
__int64 chatc_ui_message_used()
{
return sliwin_message_used(qword_220);
}

canonical_decompiled_code:
__int64 func8()
{
return sliwin_message_used(qword_220);
}

original_code:
void chatc_ui_message_used(void) {
    sliwin_message_used(mainwin.inpwin);
}

canonical_original_code:
void chatc_ui_message_used() {
    sliwin_message_used(mainwin.inpwin);
}


decompiled_code:
void __fastcall set_logging(char a1)
{
FILE *v1; // rcx

if ( a1 )
{
if ( !logging_on )
{
v1 = fopen("chat-client.log", "w");
log_file = v1;
if ( !v1 )
{
perror("chat-client: couldn't open log file");
exit(1);
}
logging_on = 1;
fwrite("Starting to log...\n", 1uLL, 0x13uLL, v1);
}
}
else if ( logging_on )
{
fwrite("Ending logging...\n", 1uLL, 0x12uLL, log_file);
fclose(log_file);
logging_on = 0;
}
}

canonical_decompiled_code:
void __fastcall func0(char a1)
{
FILE *v1; // rcx

if ( a1 )
{
if ( !logging_on )
{
v1 = fopen("chat-client.log", "w");
log_file = v1;
if ( !v1 )
{
perror("chat-client: couldn't open log file");
exit(1);
}
logging_on = 1;
fwrite("Starting to log...\n", 1uLL, 0x13uLL, v1);
}
}
else if ( logging_on )
{
fwrite("Ending logging...\n", 1uLL, 0x12uLL, log_file);
fclose(log_file);
logging_on = 0;
}
}

original_code:
void set_logging(const _Bool status) {
    if ( status && !logging_on ) {
        log_file = fopen(log_file_name, "w");
        if ( !log_file ) {
            perror("chat-client: couldn't open log file");
            exit(1);
        }
        logging_on = 1;
        fprintf(log_file, "Starting to log...\n");
    }
    else if ( !status && logging_on ) {
        fprintf(log_file, "Ending logging...\n");
        fclose(log_file);
        logging_on = 0;
    }
}

canonical_original_code:
void set_logging(_Bool status) {
    if ( status && !logging_on ) {
        log_file = fopen(log_file_name, "w");
        if ( !log_file ) {
            perror("chat-client: couldn't open log file");
            exit(1);
        }
        logging_on = 1;
        fprintf(log_file, "Starting to log...\n");
    }
    else if ( !status && logging_on ) {
        fprintf(log_file, "Ending logging...\n");
        fclose(log_file);
        logging_on = 0;
    }
}


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

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

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

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

original_code:
void log_msg(const char * format, ...) {
    if ( logging_on ) {
        va_list ap;
        __builtin_va_start(ap,format);
        vfprintf(log_file, format, ap);
        __builtin_va_end(ap);
    }
}

canonical_original_code:
void log_msg(char *format, ...) {
    if ( logging_on ) {
        __builtin_va_list ap;
        __builtin_va_start(ap,format);
        vfprintf(log_file, format, ap);
        __builtin_va_end(ap);
    }
}

variable_types:
format
char *


decompiled_code:
__int64 chatc_main_loop()
{
const char *v0; // r14
size_t v1; // rax
char *v2; // rax
char *v3; // r13
const char **v4; // r15
const char *v5; // r14
size_t v6; // r13
const char *v7; // r15
size_t v8; // rax
char *v9; // rax
char *v10; // r13
const char *v11; // r14
size_t v12; // rax
char *v13; // rax
char *v14; // r13
char v15; // bl
__int64 result; // rax
__int64 v17; // rax
__int64 v18; // rbp
fd_set v19; // [rsp+0h] [rbp-B8h] BYREF

while ( 1 )
{
memset(&v19, 0, sizeof(v19));
v19.fds_bits[0] |= 1uLL;
if ( select(1, &v19, 0LL, 0LL, 0LL) == -1 )
{
perror("chat-client: error calling select()");
exit(1);
}
if ( (v19.fds_bits[0] & 1) != 0 )
chatc_ui_process_input();
v15 = chatc_ui_input_ready();
if ( v15 )
{
v17 = chatc_ui_retrieve_input();
v18 = chatc_parse_message(v17);
switch ( *(_DWORD *)v18 )
{
case 0:
v0 = **(const char ***)(v18 + 8);
v1 = strlen(v0);
v2 = (char *)malloc(v1 + 15);
v3 = v2;
if ( !v2 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v2, "[You said: %s]", v0);
chatc_ui_print_message(v3);
free(v3);
break;
case 1:
v4 = *(const char ***)(v18 + 8);
v5 = *v4;
v6 = strlen(*v4);
v7 = v4[1];
v8 = strlen(v7);
v9 = (char *)malloc(v6 + v8 + 20);
v10 = v9;
if ( !v9 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v9, "[You told '%s': %s]", v5, v7);
chatc_ui_print_message(v10);
free(v10);
break;
case 2:
v11 = **(const char ***)(v18 + 8);
v12 = strlen(v11);
v13 = (char *)malloc(v12 + 32);
v14 = v13;
if ( !v13 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v13, "[You changed your nick to '%s']", v11);
chatc_ui_print_message(v14);
free(v14);
break;
case 3:
chatc_ui_print_message("[Quitting - goodbye!]");
v15 = 0;
break;
case 4:
chatc_ui_print_message("[Bad message!]");
break;
default:
break;
}
chatc_ui_message_used();
result = chatc_free_message(v18);
if ( !v15 )
return result;
}
}
}

canonical_decompiled_code:
__int64 func0()
{
const char *v0; // r14
size_t v1; // rax
char *v2; // rax
char *v3; // r13
const char **v4; // r15
const char *v5; // r14
size_t v6; // r13
const char *v7; // r15
size_t v8; // rax
char *v9; // rax
char *v10; // r13
const char *v11; // r14
size_t v12; // rax
char *v13; // rax
char *v14; // r13
char v15; // bl
__int64 result; // rax
__int64 v17; // rax
__int64 v18; // rbp
fd_set v19; // [rsp+0h] [rbp-B8h] BYREF

while ( 1 )
{
memset(&v19, 0, sizeof(v19));
v19.fds_bits[0] |= 1uLL;
if ( select(1, &v19, 0LL, 0LL, 0LL) == -1 )
{
perror("chat-client: error calling select()");
exit(1);
}
if ( (v19.fds_bits[0] & 1) != 0 )
chatc_ui_process_input();
v15 = chatc_ui_input_ready();
if ( v15 )
{
v17 = chatc_ui_retrieve_input();
v18 = chatc_parse_message(v17);
switch ( *(_DWORD *)v18 )
{
case 0:
v0 = **(const char ***)(v18 + 8);
v1 = strlen(v0);
v2 = (char *)malloc(v1 + 15);
v3 = v2;
if ( !v2 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v2, "[You said: %s]", v0);
chatc_ui_print_message(v3);
free(v3);
break;
case 1:
v4 = *(const char ***)(v18 + 8);
v5 = *v4;
v6 = strlen(*v4);
v7 = v4[1];
v8 = strlen(v7);
v9 = (char *)malloc(v6 + v8 + 20);
v10 = v9;
if ( !v9 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v9, "[You told '%s': %s]", v5, v7);
chatc_ui_print_message(v10);
free(v10);
break;
case 2:
v11 = **(const char ***)(v18 + 8);
v12 = strlen(v11);
v13 = (char *)malloc(v12 + 32);
v14 = v13;
if ( !v13 )
{
perror("chat-client: couldn't allocate memory");
exit(1);
}
sprintf(v13, "[You changed your nick to '%s']", v11);
chatc_ui_print_message(v14);
free(v14);
break;
case 3:
chatc_ui_print_message("[Quitting - goodbye!]");
v15 = 0;
break;
case 4:
chatc_ui_print_message("[Bad message!]");
break;
default:
break;
}
chatc_ui_message_used();
result = chatc_free_message(v18);
if ( !v15 )
return result;
}
}
}

original_code:
void chatc_main_loop(void) {
    _Bool keep_going = 1;
    while ( keep_going ) {
        fd_set fds;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
        int status = select(0 + 1, &fds, ((void *)0), ((void *)0), ((void *)0));
        if ( status == -1 ) {
            perror("chat-client: error calling select()");
            exit(1);
        }
        if ( ((((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
            chatc_ui_process_input();
        }
        if ( chatc_ui_input_ready() ) {
            const char * text = chatc_ui_retrieve_input();
            struct chat_msg * msg = chatc_parse_message(text);
            char * fmt;
            char * out;
            switch ( msg->msg_id ) {
                case CHAT_MESSAGE_SAY:
                    fmt = "[You said: %s]";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_TELL:
                    fmt = "[You told '%s': %s]";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) +
                                 strlen(msg->parameters[1]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0], msg->parameters[1]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_NICK:
                    fmt = "[You changed your nick to '%s']";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_QUIT:
                    chatc_ui_print_message("[Quitting - goodbye!]");
                    keep_going = 0;
                    break;
                case CHAT_MESSAGE_BADMSG:
                    chatc_ui_print_message("[Bad message!]");
                    break;
            }
            chatc_ui_message_used();
            chatc_free_message(msg);
        }
    }
}

canonical_original_code:
void chatc_main_loop() {
    _Bool keep_going = 1;
    while ( keep_going ) {
        struct fd_set fds;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
        int status = select(0 + 1, &fds, ((void *)0), ((void *)0), ((void *)0));
        if ( status == -1 ) {
            perror("chat-client: error calling select()");
            exit(1);
        }
        if ( ((((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))) != 0) ) {
            chatc_ui_process_input();
        }
        if ( chatc_ui_input_ready() ) {
            char *text = chatc_ui_retrieve_input();
            struct chat_msg *msg = chatc_parse_message(text);
            char *fmt;
            char *out;
            switch ( msg->msg_id ) {
                case CHAT_MESSAGE_SAY:
                    fmt = "[You said: %s]";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_TELL:
                    fmt = "[You told '%s': %s]";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) +
                                 strlen(msg->parameters[1]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0], msg->parameters[1]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_NICK:
                    fmt = "[You changed your nick to '%s']";
                    out = malloc(strlen(fmt) +
                                 strlen(msg->parameters[0]) + 1);
                    if ( !out ) {
                        perror("chat-client: couldn't allocate memory");
                        exit(1);
                    }
                    sprintf(out, fmt, msg->parameters[0]);
                    chatc_ui_print_message(out);
                    free(out);
                    break;
                case CHAT_MESSAGE_QUIT:
                    chatc_ui_print_message("[Quitting - goodbye!]");
                    keep_going = 0;
                    break;
                case CHAT_MESSAGE_BADMSG:
                    chatc_ui_print_message("[Bad message!]");
                    break;
            }
            chatc_ui_message_used();
            chatc_free_message(msg);
        }
    }
}

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

variable_types: status int

variable_types:
text
char *

variable_types: struct chat_msg { enum chatc_msg { CHAT_MESSAGE_SAY=0, CHAT_MESSAGE_TELL=1, CHAT_MESSAGE_NICK=2, CHAT_MESSAGE_QUIT=3, CHAT_MESSAGE_BADMSG=4 } msg_id; char **parameters; } *

variable_types:
fmt
char *

variable_types:
out
char *

user_defined_types: struct chat_msg { enum chatc_msg msg_id; char **parameters; }


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

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

original_code:
void input_buffer_destroy(struct input_buffer * buffer) {
    free(buffer->buffer);
    free(buffer);
}

canonical_original_code:
void input_buffer_destroy(struct input_buffer *buffer) {
    free(buffer->buffer);
    free(buffer);
}

variable_types: struct input_buffer { char *buffer; unsigned long capacity; unsigned long top; } *


decompiled_code:
__int64 __fastcall input_buffer_capacity(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
size_t input_buffer_capacity(const struct input_buffer * buffer) {
    return buffer->capacity;
}

canonical_original_code:
unsigned long input_buffer_capacity(struct input_buffer *buffer) {
    return buffer->capacity;
}

return_type: unsigned long

user_defined_types: struct input_buffer { char *buffer; unsigned long capacity; unsigned long top; }


decompiled_code:
bool __fastcall input_buffer_is_full(__int64 a1)
{
return *(_QWORD *)(a1 + 16) >= *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1)
{
return *(_QWORD *)(a1 + 16) >= *(_QWORD *)(a1 + 8);
}

original_code:
_Bool input_buffer_is_full(const struct input_buffer * buffer) {
    return buffer->top >= buffer->capacity;
}

canonical_original_code:
_Bool input_buffer_is_full(struct input_buffer *buffer) {
    return buffer->top >= buffer->capacity;
}


decompiled_code:
__int64 __fastcall input_buffer_push(__int64 *a1, char a2)
{
__int64 v2; // rdx
__int64 result; // rax

if ( input_buffer_is_full((__int64)a1) )
_assert_fail("input_buffer_is_full(buffer) == false", "input_buffer.c", 0x30u, "input_buffer_push");
v2 = *a1;
result = a1[2];
a1[2] = result + 1;
*(_BYTE *)(v2 + result) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, char a2)
{
__int64 v2; // rdx
__int64 result; // rax

if ( func2((__int64)a1) )
_assert_fail("input_buffer_is_full(buffer) == false", "input_buffer.c", 0x30u, "input_buffer_push");
v2 = *a1;
result = a1[2];
a1[2] = result + 1;
*(_BYTE *)(v2 + result) = a2;
return result;
}

original_code:
void input_buffer_push(struct input_buffer * buffer, const char c) {
    ((input_buffer_is_full(buffer) == 0) ? (void) (0) : __assert_fail ("input_buffer_is_full(buffer) == false", "input_buffer.c", 48, __extension__ __PRETTY_FUNCTION__));
    buffer->buffer[buffer->top++] = c;
}

canonical_original_code:
void input_buffer_push(struct input_buffer *buffer, char c) {
    ((input_buffer_is_full(buffer) == 0) ? (void) (0) : __assert_fail ("input_buffer_is_full(buffer) == false", "input_buffer.c", 48, __extension__ __PRETTY_FUNCTION__));
    buffer->buffer[buffer->top++] = c;
}

variable_types: c char


decompiled_code:
bool __fastcall input_buffer_is_empty(__int64 a1)
{
return *(_QWORD *)(a1 + 16) == 0LL;
}

canonical_decompiled_code:
bool __fastcall func4(__int64 a1)
{
return *(_QWORD *)(a1 + 16) == 0LL;
}

original_code:
_Bool input_buffer_is_empty(const struct input_buffer * buffer) {
    return buffer->top == 0;
}

canonical_original_code:
_Bool input_buffer_is_empty(struct input_buffer *buffer) {
    return buffer->top == 0;
}


decompiled_code:
__int64 __fastcall input_buffer_pop(_QWORD *a1)
{
__int64 v1; // rax
_BYTE *v2; // rax
unsigned int v3; // edx

if ( input_buffer_is_empty((__int64)a1) )
_assert_fail("input_buffer_is_empty(buffer) == false", "input_buffer.c", 0x35u, "input_buffer_pop");
v1 = a1[2] - 1LL;
a1[2] = v1;
v2 = (_BYTE *)(*a1 + v1);
v3 = (unsigned __int8)*v2;
*v2 = 0;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
__int64 v1; // rax
_BYTE *v2; // rax
unsigned int v3; // edx

if ( func4((__int64)a1) )
_assert_fail("input_buffer_is_empty(buffer) == false", "input_buffer.c", 0x35u, "input_buffer_pop");
v1 = a1[2] - 1LL;
a1[2] = v1;
v2 = (_BYTE *)(*a1 + v1);
v3 = (unsigned __int8)*v2;
*v2 = 0;
return v3;
}

original_code:
char input_buffer_pop(struct input_buffer * buffer) {
    ((input_buffer_is_empty(buffer) == 0) ? (void) (0) : __assert_fail ("input_buffer_is_empty(buffer) == false", "input_buffer.c", 53, __extension__ __PRETTY_FUNCTION__));
    char c = buffer->buffer[--buffer->top];
    buffer->buffer[buffer->top] = '\0';
    return c;
}

canonical_original_code:
char input_buffer_pop(struct input_buffer *buffer) {
    ((input_buffer_is_empty(buffer) == 0) ? (void) (0) : __assert_fail ("input_buffer_is_empty(buffer) == false", "input_buffer.c", 53, __extension__ __PRETTY_FUNCTION__));
    char c = buffer->buffer[--buffer->top];
    buffer->buffer[buffer->top] = '\0';
    return c;
}

return_type: char


decompiled_code:
void *__fastcall input_buffer_clear(__int64 a1)
{
void *result; // rax

result = memset(*(void **)a1, 0, *(_QWORD *)(a1 + 8) + 1LL);
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

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

result = memset(*(void **)a1, 0, *(_QWORD *)(a1 + 8) + 1LL);
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

original_code:
void input_buffer_clear(struct input_buffer * buffer) {
    memset(buffer->buffer, 0, buffer->capacity + 1);
    buffer->top = 0;
}

canonical_original_code:
void input_buffer_clear(struct input_buffer *buffer) {
    memset(buffer->buffer, 0, buffer->capacity + 1);
    buffer->top = 0;
}


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

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

original_code:
const char * input_buffer_get_buffer(struct input_buffer * buffer) {
    return buffer->buffer;
}

canonical_original_code:
char *input_buffer_get_buffer(struct input_buffer *buffer) {
    return buffer->buffer;
}


handler

decompiled_code:
void handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void handler(int signum) {
    switch (signum) {
        case 15:
        case 2:
            break;
        default:
            break;
    }
}

variable_types: signum int


decompiled_code:
int set_signal_handlers()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)handler;
v1.sa_flags = 0;
sigemptyset(&v1.sa_mask);
if ( sigaction(15, &v1, 0LL) == -1 )
{
perror("chat-client: couldn't set SIGTERM handler");
exit(1);
}
if ( sigaction(2, &v1, 0LL) == -1 )
{
perror("chat-client: couldn't set SIGINT handler");
exit(1);
}
v1.sa_handler = (__sighandler_t)set_signal_handlers;
result = sigaction(20, &v1, 0LL);
if ( result == -1 )
{
perror("chat-client: couldn't ignore SIGTSTP");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

v1.sa_handler = (__sighandler_t)handler;
v1.sa_flags = 0;
sigemptyset(&v1.sa_mask);
if ( sigaction(15, &v1, 0LL) == -1 )
{
perror("chat-client: couldn't set SIGTERM handler");
exit(1);
}
if ( sigaction(2, &v1, 0LL) == -1 )
{
perror("chat-client: couldn't set SIGINT handler");
exit(1);
}
v1.sa_handler = (__sighandler_t)set_signal_handlers;
result = sigaction(20, &v1, 0LL);
if ( result == -1 )
{
perror("chat-client: couldn't ignore SIGTSTP");
exit(1);
}
return result;
}

original_code:
void set_signal_handlers(void) {
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = handler;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    if ( sigaction(15, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't set SIGTERM handler");
        exit(1);
    }
    if ( sigaction(2, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't set SIGINT handler");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
    if ( sigaction(20, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't ignore SIGTSTP");
        exit(1);
    }
}

canonical_original_code:
void set_signal_handlers() {
    struct sigaction sa;
    sa.__sigaction_handler.sa_handler = handler;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    if ( sigaction(15, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't set SIGTERM handler");
        exit(1);
    }
    if ( sigaction(2, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't set SIGINT handler");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
    if ( sigaction(20, &sa, ((void *)0)) == -1 ) {
        perror("chat-client: couldn't ignore SIGTSTP");
        exit(1);
    }
}

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)(); }

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 free_string_list(void *ptr)
{
void *v2; // rdi
char *v3; // rbx

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

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

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

original_code:
static void free_string_list(char ** slist) {
    if ( slist ) {
        size_t i = 0;
        while ( slist[i] ) {
            free(slist[i++]);
        }
        free(slist);
    }
}

canonical_original_code:
static void free_string_list(char **slist) {
    if ( slist ) {
        unsigned long i = 0;
        while ( slist[i] ) {
            free(slist[i++]);
        }
        free(slist);
    }
}

variable_types:
slist
char **

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall translate_message_name(char *s2)
{
__int64 v2; // rbx
const char *v3; // rdi

v2 = 0LL;
v3 = "say";
while ( strcasecmp(v3, s2) )
{
++v2;
v3 = (&off_6E8)[3 * v2];
if ( !v3 )
return (unsigned int)known_msgs[6 * v2];
}
return (unsigned int)known_msgs[6 * v2];
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2)
{
__int64 v2; // rbx
const char *v3; // rdi

v2 = 0LL;
v3 = "say";
while ( strcasecmp(v3, s2) )
{
++v2;
v3 = (&off_6E8)[3 * v2];
if ( !v3 )
return (unsigned int)known_msgs[6 * v2];
}
return (unsigned int)known_msgs[6 * v2];
}

original_code:
static enum chatc_msg translate_message_name(const char * msg_name) {
    size_t i = 0;
    while ( known_msgs[i].name ) {
        if ( !strcasecmp(known_msgs[i].name, msg_name) ) {
            return known_msgs[i].msg_id;
        }
        ++i;
    }
    return known_msgs[i].msg_id;
}

canonical_original_code:
static enum chatc_msg translate_message_name(char *msg_name) {
    unsigned long i = 0;
    while ( known_msgs[i].name ) {
        if ( !strcasecmp(known_msgs[i].name, msg_name) ) {
            return known_msgs[i].msg_id;
        }
        ++i;
    }
    return known_msgs[i].msg_id;
}

return_type: enum chatc_msg { CHAT_MESSAGE_SAY=0, CHAT_MESSAGE_TELL=1, CHAT_MESSAGE_NICK=2, CHAT_MESSAGE_QUIT=3, CHAT_MESSAGE_BADMSG=4 }


decompiled_code:
void __fastcall chatc_free_message(void *ptr)
{
free_string_list(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
func0(*((void **)ptr + 1));
free(ptr);
}

original_code:
void chatc_free_message(struct chat_msg * msg) {
    free_string_list(msg->parameters);
    free(msg);
}

canonical_original_code:
void chatc_free_message(struct chat_msg *msg) {
    free_string_list(msg->parameters);
    free(msg);
}

user_defined_types: enum chatc_msg { CHAT_MESSAGE_SAY=0, CHAT_MESSAGE_TELL=1, CHAT_MESSAGE_NICK=2, CHAT_MESSAGE_QUIT=3, CHAT_MESSAGE_BADMSG=4 }


repo: paulgriffiths/cdatastruct

decompiled_code:
__int64 __fastcall stack_init(__int64 a1)
{
return sl_list_init(0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return sl_list_init(0LL, a1);
}

original_code:
stack stack_init(void (*free_func)(void *)) {
    return sl_list_init(((void *)0), free_func);
}

canonical_original_code:
struct sl_list_t *stack_init(void (*free_func)(void *)) {
    return sl_list_init(((void *)0), free_func);
}

return_type: struct sl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct sl_list_node_t { void *data; struct sl_list_node_t *next; } *front; unsigned long length; int (*cfunc)(); void (*free_func)(); } *

user_defined_types: struct sl_list_t { union pthread_mutex_t mutex; struct sl_list_node_t *front; unsigned long length; int (*cfunc)(); void (*free_func)(); }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 stack_free()
{
return sl_list_free();
}

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

original_code:
void stack_free(stack stk) {
    sl_list_free(stk);
}

canonical_original_code:
void stack_free(struct sl_list_t *stk) {
    sl_list_free(stk);
}

variable_types: struct sl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct sl_list_node_t { void *data; struct sl_list_node_t *next; } *front; unsigned long length; int (*cfunc)(); void (*free_func)(); } *


decompiled_code:
__int64 stack_length()
{
return sl_list_length();
}

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

original_code:
size_t stack_length(const stack stk) {
    return sl_list_length(stk);
}

canonical_original_code:
unsigned long stack_length(struct sl_list_t *stk) {
    return sl_list_length(stk);
}

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

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 stack_isempty()
{
return sl_list_isempty();
}

canonical_decompiled_code:
__int64 func3()
{
return sl_list_isempty();
}

original_code:
_Bool stack_isempty(const stack stk) {
    return sl_list_isempty(stk);
}

canonical_original_code:
_Bool stack_isempty(struct sl_list_t *stk) {
    return sl_list_isempty(stk);
}


decompiled_code:
__int64 __fastcall stack_pop(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rbx

v1 = (__int64 *)sl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rbx

v1 = (__int64 *)sl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

original_code:
void * stack_pop(stack stk) {
    sl_list_node popped_node = sl_list_remove_at(stk, 0);
    void * data = popped_node->data;
    free(popped_node);
    return data;
}

canonical_original_code:
void *stack_pop(struct sl_list_t *stk) {
    struct sl_list_node_t *popped_node = sl_list_remove_at(stk, 0);
    void *data = popped_node->data;
    free(popped_node);
    return data;
}

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

variable_types:
data
void *

return_type: void *


decompiled_code:
__int64 stack_peek()
{
return *(_QWORD *)sl_list_first();
}

canonical_decompiled_code:
__int64 func5()
{
return *(_QWORD *)sl_list_first();
}

original_code:
void * stack_peek(stack stk) {
    sl_list_itr peek_node = sl_list_first(stk);
    return peek_node->data;
}

canonical_original_code:
void *stack_peek(struct sl_list_t *stk) {
    struct sl_list_node_t *peek_node = sl_list_first(stk);
    return peek_node->data;
}


decompiled_code:
__int64 stack_push()
{
return sl_list_prepend();
}

canonical_decompiled_code:
__int64 func6()
{
return sl_list_prepend();
}

original_code:
void stack_push(stack stk, void * data) {
    sl_list_prepend(stk, data);
}

canonical_original_code:
void stack_push(struct sl_list_t *stk, void *data) {
    sl_list_prepend(stk, data);
}


decompiled_code:
__int64 stack_lock()
{
return sl_list_lock();
}

canonical_decompiled_code:
__int64 func7()
{
return sl_list_lock();
}

original_code:
void stack_lock(stack stk) {
    sl_list_lock(stk);
}

canonical_original_code:
void stack_lock(struct sl_list_t *stk) {
    sl_list_lock(stk);
}


decompiled_code:
__int64 stack_unlock()
{
return sl_list_unlock();
}

canonical_decompiled_code:
__int64 func8()
{
return sl_list_unlock();
}

original_code:
void stack_unlock(stack stk) {
    sl_list_unlock(stk);
}

canonical_original_code:
void stack_unlock(struct sl_list_t *stk) {
    sl_list_unlock(stk);
}


decompiled_code:
__int64 __fastcall queue_init(__int64 a1)
{
return dl_list_init(0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return dl_list_init(0LL, a1);
}

original_code:
queue queue_init(void (*free_func)(void *)) {
    return dl_list_init(((void *)0), free_func);
}

canonical_original_code:
struct dl_list_t *queue_init(void (*free_func)(void *)) {
    return dl_list_init(((void *)0), free_func);
}

return_type: struct dl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); } *

user_defined_types: struct dl_list_t { union pthread_mutex_t mutex; struct dl_list_node_t *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); }


decompiled_code:
__int64 queue_free()
{
return dl_list_free();
}

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

original_code:
void queue_free(queue que) {
    dl_list_free(que);
}

canonical_original_code:
void queue_free(struct dl_list_t *que) {
    dl_list_free(que);
}

variable_types: struct dl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); } *


decompiled_code:
__int64 queue_length()
{
return dl_list_length();
}

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

original_code:
size_t queue_length(const queue que) {
    return dl_list_length(que);
}

canonical_original_code:
unsigned long queue_length(struct dl_list_t *que) {
    return dl_list_length(que);
}

user_defined_types: struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; }


decompiled_code:
__int64 queue_isempty()
{
return dl_list_isempty();
}

canonical_decompiled_code:
__int64 func3()
{
return dl_list_isempty();
}

original_code:
_Bool queue_isempty(const queue que) {
    return dl_list_isempty(que);
}

canonical_original_code:
_Bool queue_isempty(struct dl_list_t *que) {
    return dl_list_isempty(que);
}


decompiled_code:
__int64 __fastcall queue_pop(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rbx

v1 = (__int64 *)dl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rbx

v1 = (__int64 *)dl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

original_code:
void * queue_pop(queue que) {
    dl_list_node popped_node = dl_list_remove_at(que, 0);
    void * data = popped_node->data;
    free(popped_node);
    return data;
}

canonical_original_code:
void *queue_pop(struct dl_list_t *que) {
    struct dl_list_node_t *popped_node = dl_list_remove_at(que, 0);
    void *data = popped_node->data;
    free(popped_node);
    return data;
}

variable_types: struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *


decompiled_code:
__int64 queue_pushback()
{
return dl_list_append();
}

canonical_decompiled_code:
__int64 func5()
{
return dl_list_append();
}

original_code:
void queue_pushback(queue que, void * data) {
    dl_list_append(que, data);
}

canonical_original_code:
void queue_pushback(struct dl_list_t *que, void *data) {
    dl_list_append(que, data);
}


decompiled_code:
__int64 queue_lock()
{
return dl_list_lock();
}

canonical_decompiled_code:
__int64 func6()
{
return dl_list_lock();
}

original_code:
void queue_lock(queue que) {
    dl_list_lock(que);
}

canonical_original_code:
void queue_lock(struct dl_list_t *que) {
    dl_list_lock(que);
}


decompiled_code:
__int64 queue_unlock()
{
return dl_list_unlock();
}

canonical_decompiled_code:
__int64 func7()
{
return dl_list_unlock();
}

original_code:
void queue_unlock(queue que) {
    dl_list_unlock(que);
}

canonical_original_code:
void queue_unlock(struct dl_list_t *que) {
    dl_list_unlock(que);
}


repo: paulguy/coloradjust

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

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

original_code:
void btext_free(bfont *f) {
 free((f->fontData)[0]);
 free(f);
}

canonical_original_code:
void btext_free(struct bfont *f) {
 free((f->fontData)[0]);
 free(f);
}

variable_types: struct bfont { unsigned char *fontData[96]; unsigned char height; unsigned char widths[96]; } *


decompiled_code:
__int64 __fastcall btext_calcWidth(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // al
int v3; // edx
unsigned int v4; // ecx
int v5; // eax

v2 = *a2;
if ( !*a2 )
return 0;
v3 = 0;
v4 = 0;
do
{
if ( (unsigned __int8)(v2 - 32) > 0x5Fu )
v5 = *(unsigned __int8 *)(a1 + 864);
else
v5 = *(unsigned __int8 *)(a1 + v2 - 32 + 769);
v4 += v5;
v2 = a2[++v3];
}
while ( v2 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 v2; // al
int v3; // edx
unsigned int v4; // ecx
int v5; // eax

v2 = *a2;
if ( !*a2 )
return 0;
v3 = 0;
v4 = 0;
do
{
if ( (unsigned __int8)(v2 - 32) > 0x5Fu )
v5 = *(unsigned __int8 *)(a1 + 864);
else
v5 = *(unsigned __int8 *)(a1 + v2 - 32 + 769);
v4 += v5;
v2 = a2[++v3];
}
while ( v2 );
return v4;
}

original_code:
Uint32 btext_calcWidth(bfont *f, const char *text) {
 Uint32 width, offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  offset++;
 }
 return(width);
}

canonical_original_code:
unsigned int btext_calcWidth(struct bfont *f, char *text) {
 unsigned int width; unsigned int offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  offset++;
 }
 return(width);
}

variable_types:
width
unsigned int

variable_types:
offset
unsigned int

variable_types:
utext
unsigned char *

return_type: unsigned int

user_defined_types: struct bfont { unsigned char *fontData[96]; unsigned char height; unsigned char widths[96]; }


decompiled_code:
__int64 __fastcall btext_clipTextToWidth(__int64 a1, unsigned __int8 *a2, unsigned int a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // edx
unsigned int v8; // ecx
int v9; // eax

v6 = *a2;
if ( !*a2 )
return 0;
v7 = 0;
v8 = 0;
while ( 1 )
{
v9 = (unsigned __int8)(v6 - 32) > 0x5Fu ? *(unsigned __int8 *)(a1 + 864) : *(unsigned __int8 *)(a1 + v6 - 32 + 769);
v8 += v9;
if ( v8 > a3 )
break;
v6 = a2[++v7];
if ( !v6 )
return v7;
}
return (__PAIR64__(v7, a4) - 1) >> 32;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int8 *a2, unsigned int a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // edx
unsigned int v8; // ecx
int v9; // eax

v6 = *a2;
if ( !*a2 )
return 0;
v7 = 0;
v8 = 0;
while ( 1 )
{
v9 = (unsigned __int8)(v6 - 32) > 0x5Fu ? *(unsigned __int8 *)(a1 + 864) : *(unsigned __int8 *)(a1 + v6 - 32 + 769);
v8 += v9;
if ( v8 > a3 )
break;
v6 = a2[++v7];
if ( !v6 )
return v7;
}
return (__PAIR64__(v7, a4) - 1) >> 32;
}

original_code:
Uint32 btext_clipTextToWidth(bfont *f, const char *text, Uint32 limit, int overlap) {
 Uint32 width, offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  if(width > limit) {
   if(overlap == 0)
    return(offset - 1);
   return(offset);
  }
  offset++;
 }
 return(offset);
}

canonical_original_code:
unsigned int btext_clipTextToWidth(struct bfont *f, char *text, unsigned int limit, int overlap) {
 unsigned int width; unsigned int offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  if(width > limit) {
   if(overlap == 0)
    return(offset - 1);
   return(offset);
  }
  offset++;
 }
 return(offset);
}

variable_types:
text
char *

variable_types:
limit
unsigned int

variable_types: overlap int


main

repo: paulguy/filesmasher

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v5; // er12
__off64_t v6; // rbp
__int64 v7; // r14
__int64 v8; // rax
int v9; // ebx
__int64 v10; // rbp
time_t v11; // rax
unsigned int v12; // eax
__int64 v13; // r13
__int64 v14; // rbp
int result; // eax
int v16; // eax
__int64 v17; // rsi
const char *v18; // rax
__int64 v19; // rax
__off64_t *v20; // rdx
__off64_t v21; // r9
__off64_t *v22; // rax
__off64_t v23; // r10
__off64_t v24; // rcx
__int64 v25; // rax
_QWORD *v26; // rcx
__off64_t v27; // rsi
__off64_t v28; // rdx
time_t v29; // [rsp+8h] [rbp-60h]
char buf; // [rsp+1Fh] [rbp-49h] BYREF
__off64_t offset; // [rsp+20h] [rbp-48h] BYREF
__int64 v32[8]; // [rsp+28h] [rbp-40h] BYREF

if ( argc <= 2 )
{
fwrite("USAGE: filesmasher <filename> <amount> [ranges]\n", 1uLL, 0x30uLL, stderr);
result = -1;
}
else
{
v4 = open(argv[1], 1, envp);
v5 = v4;
if ( v4 < 0 )
{
fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
perror("open");
result = -1;
}
else
{
v6 = lseek64(v4, 0LL, 2);
if ( v6 < 0 )
{
perror("lseek64");
result = -1;
}
else
{
v7 = atol(argv[2]);
if ( v7 <= 0 )
{
fwrite("\"amount\" must be a value >0.\n", 1uLL, 0x1DuLL, stderr);
result = -1;
}
else if ( argc == 4 )
{
v32[0] = 0LL;
v16 = range_parse(argv[3], v32);
v9 = v16;
if ( v16 < 0 )
{
v18 = (const char *)range_error_string((unsigned int)v16);
fprintf(stderr, "range_parse: (%d) %s.\n", (unsigned int)v9, v18);
result = -1;
}
else if ( v16 )
{
v17 = 0LL;
while ( 1 )
{
v22 = *(__off64_t **)(v32[0] + 8 * v17);
v23 = *v22;
v24 = v22[1];
if ( *v22 > v24 )
{
fprintf(stderr, "Range start %ld is greater than range end %ld.\n", *v22, v24);
return -1;
}
if ( v24 >= v6 )
break;
v19 = 0LL;
do
{
if ( (_DWORD)v19 != (_DWORD)v17 )
{
v20 = *(__off64_t **)(v32[0] + 8 * v19);
if ( v24 >= *v20 )
{
v21 = v20[1];
if ( v24 <= v21 )
{
fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n", v23, v24, *v20, v21);
return -1;
}
}
}
++v19;
}
while ( v9 > (int)v19 );
if ( v9 <= (int)++v17 )
goto LABEL_7;
}
fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", *v22, v24, v6 - 1);
result = -1;
}
else
{
fwrite("No valid ranges found, this is a bug.\n", 1uLL, 0x26uLL, stderr);
result = -1;
}
}
else
{
v32[0] = range_new(1LL);
**(_QWORD **)v32[0] = 0LL;
v8 = v32[0];
*(_QWORD *)(*(_QWORD *)v32[0] + 8LL) = v6 - 1;
*(_QWORD *)(*(_QWORD *)v8 + 16LL) = 0LL;
*(_QWORD *)(*(_QWORD *)v8 + 24LL) = v6 - 1;
v9 = 1;
LABEL_7:
printf("Filename: %s (%ld bytes)\n", argv[1], v6);
printf("Amount of Corruption: %ld\n", v7);
printf("Ranges: (%d)\n", (unsigned int)v9);
v10 = 0LL;
do
{
printf(
"%d. %ld-%ld, %ld-%ld\n",
(unsigned int)v10,
**(_QWORD **)(v32[0] + 8 * v10),
*(_QWORD *)(*(_QWORD *)(v32[0] + 8 * v10) + 8LL),
*(_QWORD *)(*(_QWORD *)(v32[0] + 8 * v10) + 16LL),
*(_QWORD *)(*(_QWORD *)(v32[0] + 8 * v10) + 24LL));
++v10;
}
while ( v9 > (int)v10 );
v11 = time(0LL);
v12 = arand_init(v11);
printf("random found to be %d bits.\n", v12);
v13 = *(_QWORD *)(*(_QWORD *)(v32[0] + 8LL * v9 - 8) + 24LL) + 1LL;
v14 = 1LL;
v29 = 0LL;
while ( 1 )
{
arand_random64(&offset, 63LL);
v28 = offset % v13;
offset %= v13;
if ( v9 > 0 )
{
v25 = v32[0];
while ( 1 )
{
v26 = *(_QWORD **)v25;
v27 = *(_QWORD *)(*(_QWORD *)v25 + 16LL);
if ( v28 > v27 && v28 < v26[3] )
break;
v25 += 8LL;
if ( v25 == v32[0] + 8LL * (unsigned int)(v9 - 1) + 8 )
goto LABEL_34;
}
offset = *v26 + v28 - v27;
}
LABEL_34:
arand_random8(&buf, 8LL);
if ( lseek64(v5, offset, 0) < 0 )
{
perror("lseek64");
return -1;
}
if ( write(v5, &buf, 1uLL) < 0 )
{
perror("write");
return -1;
}
if ( time(0LL) > v29 )
{
printf("\r%li/%li", v14, v7);
fflush(stdout);
v29 = time(0LL);
}
if ( v14 == v7 )
break;
++v14;
}
printf("\r%li/%li\n", v14, v7);
range_delete(v32[0], (unsigned int)v9);
close(v5);
result = 0;
}
}
}
}
return result;
}

original_code:
int main(int argc, char **argv) {
 int outfile;
 FileRange **ranges;
 int nranges;
 off64_t filesize, randpos, totalarea;
 int i, j;
 long amount, il;
 int32_t randbits;
 uint8_t randval;
 time_t lastupdate;
 if(argc < 3) {
  fprintf(stderr, "USAGE: filesmasher <filename> <amount> [ranges]\n");
  return(-1);
 }
 outfile = open(argv[1], 01);
 if(outfile < 0) {
  fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
  perror("open");
  return(-1);
 }
 filesize = lseek64(outfile, 0, 2);
 if(filesize < 0) {
  perror("lseek64");
  return(-1);
 }
 amount = atol(argv[2]);
 if(amount < 1) {
  fprintf(stderr, "\"amount\" must be a value >0.\n");
  return(-1);
 }
 if(argc == 4) {
  ranges = ((void *)0);
  nranges = range_parse(argv[3], &ranges);
  if(nranges < 0) {
   fprintf(stderr, "range_parse: (%d) %s.\n", nranges, range_error_string(nranges));
   return(-1);
  }
  if(nranges == 0) {
   fprintf(stderr, "No valid ranges found, this is a bug.\n");
   return(-1);
  }
  for(i = 0; i < nranges; i++) {
   if(ranges[i]->start > ranges[i]->end) {
    fprintf(stderr, "Range start %ld is greater than range end %ld.\n", ranges[i]->start, ranges[i]->end);
    return(-1);
   }
   if(ranges[i]->end > filesize - 1) {
    fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", ranges[i]->start, ranges[i]->end, filesize - 1);
    return(-1);
   }
   for(j = 0; j < nranges; j++) {
    if(i != j) {
     if( ranges[i]->end >= ranges[j]->start &&
      ranges[i]->end <= ranges[j]->end) {
      fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n",
          ranges[i]->start, ranges[i]->end,
          ranges[j]->start, ranges[j]->end);
      return(-1);
     }
    }
   }
  }
 } else {
  ranges = range_new(1);
  nranges = 1;
  ranges[0]->start = 0;
  ranges[0]->end = filesize - 1;
  ranges[0]->totalstart = 0;
  ranges[0]->totalend = filesize - 1;
 }
 printf("Filename: %s (%ld bytes)\n", argv[1], filesize);
 printf("Amount of Corruption: %ld\n", amount);
 printf("Ranges: (%d)\n", nranges);
 for(i = 0; i < nranges; i++) {
  printf("%d. %ld-%ld, %ld-%ld\n", i, ranges[i]->start, ranges[i]->end, ranges[i]->totalstart, ranges[i]->totalend);
 }
 randbits = arand_init(time(((void *)0)));
 printf("random found to be %d bits.\n", randbits);
 lastupdate = 0;
 totalarea = ranges[nranges - 1]->totalend + 1;
 for(il = 0; il < amount; il++) {
  arand_random64((uint64_t *)&randpos, 63);
  randpos %= totalarea;
  for(i = 0; i < nranges; i++) {
   if(randpos > ranges[i]->totalstart && randpos < ranges[i]->totalend) {
    randpos = randpos - ranges[i]->totalstart + ranges[i]->start;
    break;
   }
  }
  arand_random8(&randval, 8);
  if(lseek64(outfile, randpos, 0) < 0) {
   perror("lseek64");
   return(-1);
  }
  if(write(outfile, &randval, 1) < 0) {
   perror("write");
   return(-1);
  }
  if(time(((void *)0)) > lastupdate) {
   printf("\r%li/%li", il + 1, amount);
   fflush(stdout);
   lastupdate = time(((void *)0));
  }
 }
 printf("\r%li/%li\n", il, amount);
 range_delete(ranges, nranges);
 close(outfile);
 return(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 int outfile;
 struct FileRange **ranges;
 int nranges;
 long filesize; long randpos; long totalarea;
 int i; int j;
 long amount; long il;
 int randbits;
 unsigned char randval;
 long lastupdate;
 if(argc < 3) {
  fprintf(stderr, "USAGE: filesmasher <filename> <amount> [ranges]\n");
  return(-1);
 }
 outfile = open(argv[1], 01);
 if(outfile < 0) {
  fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
  perror("open");
  return(-1);
 }
 filesize = lseek64(outfile, 0, 2);
 if(filesize < 0) {
  perror("lseek64");
  return(-1);
 }
 amount = atol(argv[2]);
 if(amount < 1) {
  fprintf(stderr, "\"amount\" must be a value >0.\n");
  return(-1);
 }
 if(argc == 4) {
  ranges = ((void *)0);
  nranges = range_parse(argv[3], &ranges);
  if(nranges < 0) {
   fprintf(stderr, "range_parse: (%d) %s.\n", nranges, range_error_string(nranges));
   return(-1);
  }
  if(nranges == 0) {
   fprintf(stderr, "No valid ranges found, this is a bug.\n");
   return(-1);
  }
  for(i = 0; i < nranges; i++) {
   if(ranges[i]->start > ranges[i]->end) {
    fprintf(stderr, "Range start %ld is greater than range end %ld.\n", ranges[i]->start, ranges[i]->end);
    return(-1);
   }
   if(ranges[i]->end > filesize - 1) {
    fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", ranges[i]->start, ranges[i]->end, filesize - 1);
    return(-1);
   }
   for(j = 0; j < nranges; j++) {
    if(i != j) {
     if( ranges[i]->end >= ranges[j]->start &&
      ranges[i]->end <= ranges[j]->end) {
      fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n",
          ranges[i]->start, ranges[i]->end,
          ranges[j]->start, ranges[j]->end);
      return(-1);
     }
    }
   }
  }
 } else {
  ranges = range_new(1);
  nranges = 1;
  ranges[0]->start = 0;
  ranges[0]->end = filesize - 1;
  ranges[0]->totalstart = 0;
  ranges[0]->totalend = filesize - 1;
 }
 printf("Filename: %s (%ld bytes)\n", argv[1], filesize);
 printf("Amount of Corruption: %ld\n", amount);
 printf("Ranges: (%d)\n", nranges);
 for(i = 0; i < nranges; i++) {
  printf("%d. %ld-%ld, %ld-%ld\n", i, ranges[i]->start, ranges[i]->end, ranges[i]->totalstart, ranges[i]->totalend);
 }
 randbits = arand_init(time(((void *)0)));
 printf("random found to be %d bits.\n", randbits);
 lastupdate = 0;
 totalarea = ranges[nranges - 1]->totalend + 1;
 for(il = 0; il < amount; il++) {
  arand_random64((unsigned long *)&randpos, 63);
  randpos %= totalarea;
  for(i = 0; i < nranges; i++) {
   if(randpos > ranges[i]->totalstart && randpos < ranges[i]->totalend) {
    randpos = randpos - ranges[i]->totalstart + ranges[i]->start;
    break;
   }
  }
  arand_random8(&randval, 8);
  if(lseek64(outfile, randpos, 0) < 0) {
   perror("lseek64");
   return(-1);
  }
  if(write(outfile, &randval, 1) < 0) {
   perror("write");
   return(-1);
  }
  if(time(((void *)0)) > lastupdate) {
   printf("\r%li/%li", il + 1, amount);
   fflush(stdout);
   lastupdate = time(((void *)0));
  }
 }
 printf("\r%li/%li\n", il, amount);
 range_delete(ranges, nranges);
 close(outfile);
 return(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: outfile int

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } **

variable_types: nranges int

variable_types: filesize long

variable_types: randpos long

variable_types: totalarea long

variable_types: i int

variable_types: j int

variable_types: amount long

variable_types: il long

variable_types: randbits int

variable_types:
randval
unsigned char

variable_types: lastupdate long


decompiled_code:
__int64 __fastcall arand_init(unsigned int a1)
{
_arand_randtest = 0x7FFFFFFF;
_arand_randbits = 31;
srandom(a1);
return (unsigned int)_arand_randbits;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
_arand_randtest = 0x7FFFFFFF;
_arand_randbits = 31;
srandom(a1);
return (unsigned int)_arand_randbits;
}

original_code:
int32_t arand_init(unsigned int seed) {
 int32_t i;
 __arand_randtest = 0xFFFFFFFF;
 for(i = 32; i > 0; i--) {
  if((__arand_randtest & 2147483647) == __arand_randtest) {
   __arand_randbits = i;
   break;
  }
  __arand_randtest >>= 1;
 }
 srandom(seed);
 return(__arand_randbits);
}

canonical_original_code:
int arand_init(unsigned int seed) {
 int i;
 __arand_randtest = 0xFFFFFFFF;
 for(i = 32; i > 0; i--) {
  if((__arand_randtest & 2147483647) == __arand_randtest) {
   __arand_randbits = i;
   break;
  }
  __arand_randtest >>= 1;
 }
 srandom(seed);
 return(__arand_randbits);
}

variable_types:
seed
unsigned int


decompiled_code:
__int64 __fastcall arand_random64(__int64 *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x3F )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x3F )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

original_code:
int arand_random64(uint64_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 64) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random64(unsigned long *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 64) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned long *

variable_types: bits int

variable_types: bitsleft int


decompiled_code:
__int64 __fastcall arand_random32(_DWORD *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x1F )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x1F )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

original_code:
int arand_random32(uint32_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 32) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random32(unsigned int *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 32) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned int *


decompiled_code:
__int64 __fastcall arand_random16(_WORD *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0xF )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_WORD *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0xF )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

original_code:
int arand_random16(uint16_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 16) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random16(unsigned short *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 16) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned short *


decompiled_code:
__int64 __fastcall arand_random8(_BYTE *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 7 )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, int a2)
{
int i; // ebx

if ( (unsigned int)(a2 - 1) > 7 )
return 0xFFFFFFFFLL;
*a1 = random();
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
*a1 |= random() << ((unsigned __int8)a2 - (unsigned __int8)i);
*a1 &= _arand_bitmasks[a2];
return 0LL;
}

original_code:
int arand_random8(uint8_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 8) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random8(unsigned char *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 8) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned char *


decompiled_code:
void __fastcall range_delete(void *ptr, int a2)
{
void **v2; // rbx

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

canonical_decompiled_code:
void __fastcall func1(void *ptr, int a2)
{
void **v2; // rbx

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

original_code:
void range_delete(FileRange ** ranges, int count) {
 int i;
 for(i = 0; i < count; i++) {
  free(ranges[i]);
 }
 free(ranges);
}

canonical_original_code:
void range_delete(struct FileRange **ranges, int count) {
 int i;
 for(i = 0; i < count; i++) {
  free(ranges[i]);
 }
 free(ranges);
}

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } **

variable_types: count int


decompiled_code:
__int64 __fastcall range_verify(char *a1)
{
char v1; // al
char *v2; // rdi
int v3; // esi
int v4; // ecx
int v5; // edx

v1 = *a1;
if ( *a1 )
{
v2 = a1 + 1;
v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
if ( v5 )
{
if ( v1 == 44 )
{
if ( !v4 )
return (unsigned int)-1;
++v3;
v4 = 0;
v5 = 0;
}
else
{
if ( (unsigned __int8)(v1 - 48) > 9u )
return (unsigned int)-1;
v4 = v5;
}
}
else if ( v1 == 45 )
{
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
v4 = 0;
}
else if ( v1 == 44 )
{
if ( !v4 )
return (unsigned int)-1;
++v3;
v4 = 0;
}
else
{
if ( (unsigned __int8)(v1 - 48) > 9u )
return (unsigned int)-1;
v4 = 1;
}
v1 = *v2++;
if ( !v1 )
return (unsigned int)((v4 == 1) + v3);
}
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
char v1; // al
char *v2; // rdi
int v3; // esi
int v4; // ecx
int v5; // edx

v1 = *a1;
if ( *a1 )
{
v2 = a1 + 1;
v3 = 0;
v4 = 0;
v5 = 0;
while ( 1 )
{
if ( v5 )
{
if ( v1 == 44 )
{
if ( !v4 )
return (unsigned int)-1;
++v3;
v4 = 0;
v5 = 0;
}
else
{
if ( (unsigned __int8)(v1 - 48) > 9u )
return (unsigned int)-1;
v4 = v5;
}
}
else if ( v1 == 45 )
{
if ( !v4 )
return (unsigned int)-1;
v5 = v4;
v4 = 0;
}
else if ( v1 == 44 )
{
if ( !v4 )
return (unsigned int)-1;
++v3;
v4 = 0;
}
else
{
if ( (unsigned __int8)(v1 - 48) > 9u )
return (unsigned int)-1;
v4 = 1;
}
v1 = *v2++;
if ( !v1 )
return (unsigned int)((v4 == 1) + v3);
}
}
return 0;
}

original_code:
int range_verify(char *rangeString) {
 int stage, numfound;
 int i;
 int count;
 count = 0; stage = 0; numfound = 0;
 for(i = 0; rangeString[i] != '\0'; i++) {
  switch(stage) {
   case 0:
    if(rangeString[i] == '-') {
     if(numfound == 0) {
      return(-1);
     }
     stage = 1;
     numfound = 0;
    } else if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   case 1:
    if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     stage = 0;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   default:
    return(-2);
  }
 }
 if(numfound == 1) {
  count++;
 }
 return(count);
}

canonical_original_code:
int range_verify(char *rangeString) {
 int stage; int numfound;
 int i;
 int count;
 count = 0; stage = 0; numfound = 0;
 for(i = 0; rangeString[i] != '\0'; i++) {
  switch(stage) {
   case 0:
    if(rangeString[i] == '-') {
     if(numfound == 0) {
      return(-1);
     }
     stage = 1;
     numfound = 0;
    } else if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   case 1:
    if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     stage = 0;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   default:
    return(-2);
  }
 }
 if(numfound == 1) {
  count++;
 }
 return(count);
}

variable_types:
rangeString
char *

variable_types: stage int

variable_types: numfound int