main

repo: patrickmalsom/SDE

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
double v4; // xmm0_8
int v5; // ebx
double v6; // xmm0_8
double v7; // r13
unsigned int v8; // er14
double v10; // [rsp+10h] [rbp-38h]
double v11; // [rsp+18h] [rbp-30h]

setbuf(stdout, 0LL);
gsl_rng_env_setup();
gsl_rng_alloc();
v11 = atof(argv[1]);
v3 = atoi(argv[2]);
v4 = (v11 + v11) * 0.001;
if ( v4 < 0.0 )
v10 = sqrt(v4);
else
v10 = sqrt(v4);
if ( v3 <= 0 )
{
v8 = 0;
}
else
{
v5 = 0;
v6 = 1.0;
v7 = 1.0;
v8 = 0;
do
{
gsl_ran_ugaussian();
v6 = v6 * v10 + (1.0 - v6 * v6) * (v6 * 0.004) + v6;
if ( fabs(v6) > 1.0 && v7 * v6 < 0.0 )
{
++v8;
v7 = 1.0;
if ( v6 <= 0.0 )
v7 = -1.0;
}
++v5;
}
while ( v3 != v5 );
}
printf("Temp=%f,Trans=%d\n", v11, v8);
gsl_rng_free();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  setbuf(stdout,((void *)0));
  const gsl_rng_type * RanNumType;
  gsl_rng *RanNumPointer;
  gsl_rng_env_setup();
  RanNumType = gsl_rng_default;
  RanNumPointer= gsl_rng_alloc (RanNumType);
  double Temp=atof(argv[1]);
  int Numt=atoi(argv[2]);
  double dt=0.001;
  double xstart=1.0;
  double pref=sqrt(2.0*Temp*dt);
  int trans=0;
  double basin=xstart;
  double xold=xstart;
  double xnew;
  int i;
  for(i=0;i<Numt;i++)
  {
    xnew=xold+dt*4.0*xold*(1.0-xold*xold)+pref*gsl_ran_ugaussian(RanNumPointer);
    if(fabs(xnew) > 1.0)
    {
      if(basin * xnew < 0.0 )
      {
        trans=trans+1;
        if(xnew>0.0)
          basin=1.0;
        else
          basin=-1.0;
      }
    }
  xold=xnew;
  }
  printf("Temp=%f,Trans=%d\n",Temp,trans);
  gsl_rng_free (RanNumPointer);
  return(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
  setbuf(stdout,((void *)0));
  struct gsl_rng_type *RanNumType;
  struct gsl_rng *RanNumPointer;
  gsl_rng_env_setup();
  RanNumType = gsl_rng_default;
  RanNumPointer= gsl_rng_alloc (RanNumType);
  double Temp = atof(argv[1]);
  int Numt = atoi(argv[2]);
  double dt = 0.001;
  double xstart = 1.0;
  double pref = sqrt(2.0*Temp*dt);
  int trans = 0;
  double basin = xstart;
  double xold = xstart;
  double xnew;
  int i;
  for(i=0;i<Numt;i++)
  {
    xnew=xold+dt*4.0*xold*(1.0-xold*xold)+pref*gsl_ran_ugaussian(RanNumPointer);
    if(fabs(xnew) > 1.0)
    {
      if(basin * xnew < 0.0 )
      {
        trans=trans+1;
        if(xnew>0.0)
          basin=1.0;
        else
          basin=-1.0;
      }
    }
  xold=xnew;
  }
  printf("Temp=%f,Trans=%d\n",Temp,trans);
  gsl_rng_free (RanNumPointer);
  return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct gsl_rng { struct gsl_rng_type { char *name; unsigned long max; unsigned long min; unsigned long size; void (*set)(void *state, unsigned long seed); unsigned long (*get)(void *state); double (*get_double)(void *state); } *type; void *state; } *

variable_types: Temp double

variable_types: Numt int

variable_types: dt double

variable_types: xstart double

variable_types: pref double

variable_types: trans int

variable_types: basin double

variable_types: xold double

variable_types: xnew double

variable_types: i int

user_defined_types: struct gsl_rng { struct gsl_rng_type *type; void *state; }


repo: paulburton/fitbitd

decompiled_code:
_QWORD *postdata_create()
{
_QWORD *v0; // rbx
__int64 v1; // rbp

v0 = calloc(1uLL, 0x18uLL);
if ( v0 )
{
v1 = curl_easy_init();
v0[2] = v1;
if ( !v1 )
{
free(v0);
v0 = 0LL;
}
}
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx
__int64 v1; // rbp

v0 = calloc(1uLL, 0x18uLL);
if ( v0 )
{
v1 = curl_easy_init();
v0[2] = v1;
if ( !v1 )
{
free(v0);
v0 = 0LL;
}
}
return v0;
}

original_code:
postdata_t *postdata_create(void)
{
    postdata_t *pd;
    pd = calloc(1, sizeof(*pd));
    if (!pd)
        return ((void *)0);
    pd->curl = curl_easy_init();
    if (!pd->curl)
        goto err_curl;
    return pd;
err_curl:
    free(pd);
    return ((void *)0);
}

canonical_original_code:
struct postdata_s *postdata_create()
{
    struct postdata_s *pd;
    pd = calloc(1, sizeof(*pd));
    if (!pd)
        return ((void *)0);
    pd->curl = curl_easy_init();
    if (!pd->curl)
        goto err_curl;
    return pd;
err_curl:
    free(pd);
    return ((void *)0);
}

variable_types: struct postdata_s { char *str; unsigned long str_sz; void *curl; } *


decompiled_code:
void __fastcall postdata_destroy(void *ptr)
{
curl_easy_cleanup(*((_QWORD *)ptr + 2));
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
curl_easy_cleanup(*((_QWORD *)ptr + 2));
free(*(void **)ptr);
free(ptr);
}

original_code:
void postdata_destroy(postdata_t *pd)
{
    curl_easy_cleanup(pd->curl);
    free(pd->str);
    free(pd);
}

canonical_original_code:
void postdata_destroy(struct postdata_s *pd)
{
    curl_easy_cleanup(pd->curl);
    free(pd->str);
    free(pd);
}

return_type: void

user_defined_types: struct postdata_s { char *str; unsigned long str_sz; void *curl; }


decompiled_code:
__int64 __fastcall postdata_append(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rax
const char *v6; // rbp
const char *v7; // r12
size_t v8; // r13
size_t v9; // rax
size_t v10; // r14
void *v11; // rdi
_BYTE *v12; // rax
__int64 v13; // rdx
char *v14; // r13
size_t v15; // rax
char *v16; // r13
size_t v17; // rax

v5 = curl_easy_escape(a1, a2, 0LL);
if ( !v5 )
return 0xFFFFFFFFLL;
v6 = (const char *)v5;
v7 = (const char *)curl_easy_escape(a1, a3, 0LL);
if ( v7 )
{
v8 = strlen(v6);
v9 = v8 + strlen(v7);
v10 = v9 + 1;
v11 = *(void **)a1;
if ( *(_QWORD *)a1 )
{
v12 = realloc(v11, v10 + *(_QWORD *)(a1 + 8) + 2);
if ( v12 )
{
v13 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v13 + 1;
v12[v13] = 38;
v14 = &v12[*(_QWORD *)(a1 + 8)];
*(_QWORD *)a1 = v12;
LABEL_6:
v15 = strlen(v6);
memcpy(v14, v6, v15);
v16 = &v14[strlen(v6)];
*v16 = 61;
v17 = strlen(v7);
memcpy(v16 + 1, v7, v17);
v16[strlen(v7) + 1] = 0;
*(_QWORD *)(a1 + 8) += v10;
curl_free(v7);
curl_free(v6);
return 0LL;
}
}
else
{
v14 = (char *)malloc(v9 + 2);
*(_QWORD *)a1 = v14;
if ( v14 )
goto LABEL_6;
}
curl_free(v7);
}
curl_free(v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rax
const char *v6; // rbp
const char *v7; // r12
size_t v8; // r13
size_t v9; // rax
size_t v10; // r14
void *v11; // rdi
_BYTE *v12; // rax
__int64 v13; // rdx
char *v14; // r13
size_t v15; // rax
char *v16; // r13
size_t v17; // rax

v5 = curl_easy_escape(a1, a2, 0LL);
if ( !v5 )
return 0xFFFFFFFFLL;
v6 = (const char *)v5;
v7 = (const char *)curl_easy_escape(a1, a3, 0LL);
if ( v7 )
{
v8 = strlen(v6);
v9 = v8 + strlen(v7);
v10 = v9 + 1;
v11 = *(void **)a1;
if ( *(_QWORD *)a1 )
{
v12 = realloc(v11, v10 + *(_QWORD *)(a1 + 8) + 2);
if ( v12 )
{
v13 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v13 + 1;
v12[v13] = 38;
v14 = &v12[*(_QWORD *)(a1 + 8)];
*(_QWORD *)a1 = v12;
LABEL_6:
v15 = strlen(v6);
memcpy(v14, v6, v15);
v16 = &v14[strlen(v6)];
*v16 = 61;
v17 = strlen(v7);
memcpy(v16 + 1, v7, v17);
v16[strlen(v7) + 1] = 0;
*(_QWORD *)(a1 + 8) += v10;
curl_free(v7);
curl_free(v6);
return 0LL;
}
}
else
{
v14 = (char *)malloc(v9 + 2);
*(_QWORD *)a1 = v14;
if ( v14 )
goto LABEL_6;
}
curl_free(v7);
}
curl_free(v6);
return 0xFFFFFFFFLL;
}

original_code:
int postdata_append(postdata_t *pd, const char *name, const char *val)
{
    size_t nsz;
    char *name_esc, *val_esc, *nstr, *write_pos;
    name_esc = curl_easy_escape(pd, name, 0);
    if (!name_esc)
        goto oom_name;
    val_esc = curl_easy_escape(pd, val, 0);
    if (!val_esc)
        goto oom_val;
    nsz = strlen(name_esc) + 1 + strlen(val_esc);
    if (pd->str) {
        nstr = realloc(pd->str, pd->str_sz + 1 + nsz + 1);
        if (!nstr)
            goto oom_str;
        nstr[pd->str_sz++] = '&';
        write_pos = &nstr[pd->str_sz];
        pd->str = nstr;
    } else {
        pd->str = malloc(nsz + 1);
        if (!pd->str)
            goto oom_str;
        write_pos = pd->str;
    }
    memcpy(write_pos, name_esc, strlen(name_esc));
    write_pos += strlen(name_esc);
    *write_pos++ = '=';
    memcpy(write_pos, val_esc, strlen(val_esc));
    write_pos += strlen(val_esc);
    *write_pos = 0;
    pd->str_sz += nsz;
    curl_free(val_esc);
    curl_free(name_esc);
    return 0;
oom_str:
    curl_free(val_esc);
oom_val:
    curl_free(name_esc);
oom_name:
    return -1;
}

canonical_original_code:
int postdata_append(struct postdata_s *pd, char *name, char *val)
{
    unsigned long nsz;
    char *name_esc; char *val_esc; char *nstr; char *write_pos;
    name_esc = curl_easy_escape(pd, name, 0);
    if (!name_esc)
        goto oom_name;
    val_esc = curl_easy_escape(pd, val, 0);
    if (!val_esc)
        goto oom_val;
    nsz = strlen(name_esc) + 1 + strlen(val_esc);
    if (pd->str) {
        nstr = realloc(pd->str, pd->str_sz + 1 + nsz + 1);
        if (!nstr)
            goto oom_str;
        nstr[pd->str_sz++] = '&';
        write_pos = &nstr[pd->str_sz];
        pd->str = nstr;
    } else {
        pd->str = malloc(nsz + 1);
        if (!pd->str)
            goto oom_str;
        write_pos = pd->str;
    }
    memcpy(write_pos, name_esc, strlen(name_esc));
    write_pos += strlen(name_esc);
    *write_pos++ = '=';
    memcpy(write_pos, val_esc, strlen(val_esc));
    write_pos += strlen(val_esc);
    *write_pos = 0;
    pd->str_sz += nsz;
    curl_free(val_esc);
    curl_free(name_esc);
    return 0;
oom_str:
    curl_free(val_esc);
oom_val:
    curl_free(name_esc);
oom_name:
    return -1;
}

variable_types:
name
char *

variable_types:
val
char *

variable_types:
nsz
unsigned long

variable_types:
nstr
char *

return_type: int


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

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

original_code:
char *postdata_string(postdata_t *pd)
{
    return pd->str;
}

canonical_original_code:
char *postdata_string(struct postdata_s *pd)
{
    return pd->str;
}

return_type: char *


decompiled_code:
int __fastcall devstate_enum_devices(void (__fastcall *a1)(_QWORD *, __int64), __int64 a2)
{
_QWORD *v2; // rbx

pthread_mutex_lock(&devs_mutex);
v2 = devs;
if ( devs )
{
do
{
a1(v2, a2);
v2 = *(_QWORD **)(v2[8] + 8LL);
}
while ( v2 );
}
return pthread_mutex_unlock(&devs_mutex);
}

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

pthread_mutex_lock(&devs_mutex);
v2 = devs;
if ( devs )
{
do
{
a1(v2, a2);
v2 = *(_QWORD **)(v2[8] + 8LL);
}
while ( v2 );
}
return pthread_mutex_unlock(&devs_mutex);
}

original_code:
void devstate_enum_devices(void (*callback)(devstate_t *dev, void *user), void *user)
{
    devstate_t *dev;
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next)
        callback(dev, user);
    pthread_mutex_unlock(&devs_mutex);
}

canonical_original_code:
void devstate_enum_devices(void (*callback)(struct devstate_s *dev, void *user), void *user)
{
    struct devstate_s *dev;
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next)
        callback(dev, user);
    pthread_mutex_unlock(&devs_mutex);
}

variable_types: void (*)(struct devstate_s { unsigned char serial[5]; long last_sync_time; unsigned int state; char tracker_id[20]; char user_id[20]; struct devstate_priv_s { struct devstate_s *prev; } *priv; } *dev, void *user)

variable_types:
user
void *

user_defined_types: struct devstate_s { unsigned char serial[5]; long last_sync_time; unsigned int state; char tracker_id[20]; char user_id[20]; struct devstate_priv_s *priv; }


decompiled_code:
int __fastcall devstate_record(void *s1, void (__fastcall *a2)(_DWORD *, __int64), __int64 a3)
{
_QWORD **v4; // r12
_DWORD *v5; // rbx
_QWORD *v6; // rax

pthread_mutex_lock(&devs_mutex);
v4 = (_QWORD **)devs;
if ( devs )
{
v5 = devs;
while ( memcmp(s1, v5, 5uLL) )
{
v5 = *(_DWORD **)(*((_QWORD *)v5 + 8) + 8LL);
if ( !v5 )
goto LABEL_5;
}
}
else
{
LABEL_5:
v5 = calloc(1uLL, 0x48uLL);
if ( !v5 )
{
fwrite("E:devstate: failed to alloc devstate_t\n", 1uLL, 0x27uLL, stderr);
fflush(stderr);
return pthread_mutex_unlock(&devs_mutex);
}
v6 = calloc(1uLL, 0x10uLL);
*((_QWORD *)v5 + 8) = v6;
if ( !v6 )
{
fwrite("E:devstate: failed to alloc devstate_priv_t\n", 1uLL, 0x2CuLL, stderr);
fflush(stderr);
free(v5);
return pthread_mutex_unlock(&devs_mutex);
}
*v5 = *(_DWORD *)s1;
*((_BYTE *)v5 + 4) = *((_BYTE *)s1 + 4);
v6[1] = v4;
if ( v4 )
*v4[8] = v5;
devs = v5;
}
if ( v5 && a2 )
a2(v5, a3);
return pthread_mutex_unlock(&devs_mutex);
}

canonical_decompiled_code:
int __fastcall func1(void *s1, void (__fastcall *a2)(_DWORD *, __int64), __int64 a3)
{
_QWORD **v4; // r12
_DWORD *v5; // rbx
_QWORD *v6; // rax

pthread_mutex_lock(&devs_mutex);
v4 = (_QWORD **)devs;
if ( devs )
{
v5 = devs;
while ( memcmp(s1, v5, 5uLL) )
{
v5 = *(_DWORD **)(*((_QWORD *)v5 + 8) + 8LL);
if ( !v5 )
goto LABEL_5;
}
}
else
{
LABEL_5:
v5 = calloc(1uLL, 0x48uLL);
if ( !v5 )
{
fwrite("E:devstate: failed to alloc devstate_t\n", 1uLL, 0x27uLL, stderr);
fflush(stderr);
return pthread_mutex_unlock(&devs_mutex);
}
v6 = calloc(1uLL, 0x10uLL);
*((_QWORD *)v5 + 8) = v6;
if ( !v6 )
{
fwrite("E:devstate: failed to alloc devstate_priv_t\n", 1uLL, 0x2CuLL, stderr);
fflush(stderr);
free(v5);
return pthread_mutex_unlock(&devs_mutex);
}
*v5 = *(_DWORD *)s1;
*((_BYTE *)v5 + 4) = *((_BYTE *)s1 + 4);
v6[1] = v4;
if ( v4 )
*v4[8] = v5;
devs = v5;
}
if ( v5 && a2 )
a2(v5, a3);
return pthread_mutex_unlock(&devs_mutex);
}

original_code:
void devstate_record(uint8_t serial[5], void (*callback)(devstate_t *dev, void *user), void *user)
{
    devstate_t *dev = ((void *)0);
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next) {
        if (!memcmp(serial, dev->serial, sizeof(dev->serial))) {
            goto out;
        }
    }
    dev = calloc(1, sizeof(*dev));
    if (!dev) {
        do { fprintf(stderr, "E:" "devstate" ": " "failed to alloc devstate_t\n"); fflush(stderr); } while (0);
        goto out;
    }
    dev->priv = calloc(1, sizeof(devstate_priv_t));
    if (!dev->priv) {
        do { fprintf(stderr, "E:" "devstate" ": " "failed to alloc devstate_priv_t\n"); fflush(stderr); } while (0);
        free(dev);
        dev = ((void *)0);
        goto out;
    }
    memcpy(dev->serial, serial, sizeof(dev->serial));
    dev->priv->next = devs;
    if (dev->priv->next)
        dev->priv->next->priv->prev = dev;
    devs = dev;
out:
    if (dev && callback)
        callback(dev, user);
    pthread_mutex_unlock(&devs_mutex);
}

canonical_original_code:
void devstate_record(unsigned char serial[5], void (*callback)(struct devstate_s *dev, void *user), void *user)
{
    struct devstate_s *dev = ((void *)0);
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next) {
        if (!memcmp(serial, dev->serial, sizeof(dev->serial))) {
            goto out;
        }
    }
    dev = calloc(1, sizeof(*dev));
    if (!dev) {
        do { fprintf(stderr, "E:" "devstate" ": " "failed to alloc devstate_t\n"); fflush(stderr); } while (0);
        goto out;
    }
    dev->priv = calloc(1, sizeof(devstate_priv_t));
    if (!dev->priv) {
        do { fprintf(stderr, "E:" "devstate" ": " "failed to alloc devstate_priv_t\n"); fflush(stderr); } while (0);
        free(dev);
        dev = ((void *)0);
        goto out;
    }
    memcpy(dev->serial, serial, sizeof(dev->serial));
    dev->priv->next = devs;
    if (dev->priv->next)
        dev->priv->next->priv->prev = dev;
    devs = dev;
out:
    if (dev && callback)
        callback(dev, user);
    pthread_mutex_unlock(&devs_mutex);
}

variable_types:
serial
unsigned char [5]

variable_types: struct devstate_s { unsigned char serial[5]; long last_sync_time; unsigned int state; char tracker_id[20]; char user_id[20]; struct devstate_priv_s { struct devstate_s *prev; } *priv; } *


decompiled_code:
int __fastcall devstate_clean(__int64 a1)
{
_QWORD *v1; // rbx
_QWORD *v3; // rax
_QWORD *v4; // rax
__int64 v5; // rdx

pthread_mutex_lock(&devs_mutex);
v1 = devs;
if ( devs )
{
do
{
if ( v1[1] < a1 )
{
v3 = (_QWORD *)v1[8];
if ( *v3 )
*(_QWORD *)(*(_QWORD *)(*v3 + 64LL) + 8LL) = v3[1];
v4 = (_QWORD *)v1[8];
v5 = v4[1];
if ( v5 )
**(_QWORD **)(v5 + 64) = *v4;
free(v1);
}
v1 = *(_QWORD **)(v1[8] + 8LL);
}
while ( v1 );
}
return pthread_mutex_unlock(&devs_mutex);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
_QWORD *v1; // rbx
_QWORD *v3; // rax
_QWORD *v4; // rax
__int64 v5; // rdx

pthread_mutex_lock(&devs_mutex);
v1 = devs;
if ( devs )
{
do
{
if ( v1[1] < a1 )
{
v3 = (_QWORD *)v1[8];
if ( *v3 )
*(_QWORD *)(*(_QWORD *)(*v3 + 64LL) + 8LL) = v3[1];
v4 = (_QWORD *)v1[8];
v5 = v4[1];
if ( v5 )
**(_QWORD **)(v5 + 64) = *v4;
free(v1);
}
v1 = *(_QWORD **)(v1[8] + 8LL);
}
while ( v1 );
}
return pthread_mutex_unlock(&devs_mutex);
}

original_code:
void devstate_clean(long discard_prior_to)
{
    devstate_t *dev;
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next) {
        if (dev->last_sync_time >= discard_prior_to)
            continue;
        if (dev->priv->prev)
            dev->priv->prev->priv->next = dev->priv->next;
        if (dev->priv->next)
            dev->priv->next->priv->prev = dev->priv->prev;
        free(dev);
    }
    pthread_mutex_unlock(&devs_mutex);
}

canonical_original_code:
void devstate_clean(long discard_prior_to)
{
    struct devstate_s *dev;
    pthread_mutex_lock(&devs_mutex);
    for (dev = devs; dev; dev = dev->priv->next) {
        if (dev->last_sync_time >= discard_prior_to)
            continue;
        if (dev->priv->prev)
            dev->priv->prev->priv->next = dev->priv->next;
        if (dev->priv->next)
            dev->priv->next->priv->prev = dev->priv->prev;
        free(dev);
    }
    pthread_mutex_unlock(&devs_mutex);
}

user_defined_types: struct devstate_priv_s { struct devstate_s *prev; }


decompiled_code:
__int64 get_config_home()
{
char *v1; // rax
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp
char *v6; // rax
const char *v7; // rbx
size_t v8; // rax
char *v9; // rax
char *v10; // rbp

if ( cfg_home )
return cfg_home;
v1 = getenv("XDG_CONFIG_HOME");
v2 = v1;
if ( v1 && (v3 = strlen(v1), v4 = (char *)malloc(v3 + 9), v5 = v4, (cfg_home = (__int64)v4) != 0) )
{
strcpy(v4, v2);
strcpy(&v5[strlen(v2)], "/fitbitd");
}
else
{
v6 = getenv("HOME");
v7 = v6;
if ( v6 && (v8 = strlen(v6), v9 = (char *)malloc(v8 + 17), v10 = v9, (cfg_home = (__int64)v9) != 0) )
{
strcpy(v9, v7);
strcpy(&v10[strlen(v7)], "/.config/fitbitd");
}
else
{
cfg_home = (__int64)cfg_home_fallback;
}
}
return cfg_home;
}

canonical_decompiled_code:
__int64 func0()
{
char *v1; // rax
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbp
char *v6; // rax
const char *v7; // rbx
size_t v8; // rax
char *v9; // rax
char *v10; // rbp

if ( cfg_home )
return cfg_home;
v1 = getenv("XDG_CONFIG_HOME");
v2 = v1;
if ( v1 && (v3 = strlen(v1), v4 = (char *)malloc(v3 + 9), v5 = v4, (cfg_home = (__int64)v4) != 0) )
{
strcpy(v4, v2);
strcpy(&v5[strlen(v2)], "/fitbitd");
}
else
{
v6 = getenv("HOME");
v7 = v6;
if ( v6 && (v8 = strlen(v6), v9 = (char *)malloc(v8 + 17), v10 = v9, (cfg_home = (__int64)v9) != 0) )
{
strcpy(v9, v7);
strcpy(&v10[strlen(v7)], "/.config/fitbitd");
}
else
{
cfg_home = (__int64)cfg_home_fallback;
}
}
return cfg_home;
}

original_code:
static char *get_config_home(void)
{
    char *home;
    if (!cfg_home) {
        home = getenv("XDG_CONFIG_HOME");
        if (home) {
            cfg_home = malloc(strlen(home) + 9);
            if (cfg_home) {
                strcpy(cfg_home, home);
                strcpy(&cfg_home[strlen(home)], "/fitbitd");
            }
        }
    }
    if (!cfg_home) {
        home = getenv("HOME");
        if (home) {
            cfg_home = malloc(strlen(home) + 17);
            if (cfg_home) {
                strcpy(cfg_home, home);
                strcpy(&cfg_home[strlen(home)], "/.config/fitbitd");
            }
        }
    }
    if (!cfg_home)
        cfg_home = cfg_home_fallback;
    return cfg_home;
}

canonical_original_code:
static char *get_config_home()
{
    char *home;
    if (!cfg_home) {
        home = getenv("XDG_CONFIG_HOME");
        if (home) {
            cfg_home = malloc(strlen(home) + 9);
            if (cfg_home) {
                strcpy(cfg_home, home);
                strcpy(&cfg_home[strlen(home)], "/fitbitd");
            }
        }
    }
    if (!cfg_home) {
        home = getenv("HOME");
        if (home) {
            cfg_home = malloc(strlen(home) + 17);
            if (cfg_home) {
                strcpy(cfg_home, home);
                strcpy(&cfg_home[strlen(home)], "/.config/fitbitd");
            }
        }
    }
    if (!cfg_home)
        cfg_home = cfg_home_fallback;
    return cfg_home;
}

variable_types:
home
char *


decompiled_code:
_DWORD *prefs_create()
{
const char *v0; // r12
_DWORD *v1; // rbx
char *v2; // rax
char *v3; // rax
char *v4; // rax
void *v5; // r13
size_t v6; // rax
char *v7; // rax
char *v8; // rbp
size_t v10; // rax
char *v11; // rdi
struct utsname v12; // [rsp+0h] [rbp-1B8h] BYREF

v0 = (const char *)get_config_home();
v1 = malloc(0x40uLL);
if ( v1 )
{
v2 = strdup("https://client.fitbit.com/device/tracker/uploadData");
*((_QWORD *)v1 + 1) = v2;
if ( !v2 )
{
LABEL_17:
free(v1);
return 0LL;
}
v3 = strdup("2ea32002-a079-48f4-8020-0badd22939e3");
*((_QWORD *)v1 + 2) = v3;
if ( !v3 )
{
LABEL_16:
free(*((void **)v1 + 1));
goto LABEL_17;
}
v4 = strdup("0.1~ba1f562");
*((_QWORD *)v1 + 3) = v4;
if ( !v4 )
{
LABEL_15:
free(*((void **)v1 + 2));
goto LABEL_16;
}
if ( uname(&v12) )
{
*((_QWORD *)v1 + 4) = strdup("fitbitd");
}
else
{
v10 = strlen(v12.sysname);
v11 = (char *)malloc(v10 + 9);
*((_QWORD *)v1 + 4) = v11;
if ( !v11 )
{
LABEL_14:
free(*((void **)v1 + 3));
goto LABEL_15;
}
sprintf(v11, "fitbitd-%s", v12.sysname);
}
v5 = (void *)*((_QWORD *)v1 + 4);
if ( v5 )
{
v6 = strlen(v0);
v7 = (char *)malloc(v6 + 6);
v8 = v7;
*((_QWORD *)v1 + 5) = v7;
if ( v7 )
{
strcpy(v7, v0);
strcpy(&v8[strlen(v0)], "/lock");
*((_QWORD *)v1 + 6) = 0LL;
*((_QWORD *)v1 + 7) = 0LL;
*v1 = 10;
v1[1] = 900;
return v1;
}
free(v5);
}
goto LABEL_14;
}
return v1;
}

canonical_decompiled_code:
_DWORD *func1()
{
const char *v0; // r12
_DWORD *v1; // rbx
char *v2; // rax
char *v3; // rax
char *v4; // rax
void *v5; // r13
size_t v6; // rax
char *v7; // rax
char *v8; // rbp
size_t v10; // rax
char *v11; // rdi
struct utsname v12; // [rsp+0h] [rbp-1B8h] BYREF

v0 = (const char *)func0();
v1 = malloc(0x40uLL);
if ( v1 )
{
v2 = strdup("https://client.fitbit.com/device/tracker/uploadData");
*((_QWORD *)v1 + 1) = v2;
if ( !v2 )
{
LABEL_17:
free(v1);
return 0LL;
}
v3 = strdup("2ea32002-a079-48f4-8020-0badd22939e3");
*((_QWORD *)v1 + 2) = v3;
if ( !v3 )
{
LABEL_16:
free(*((void **)v1 + 1));
goto LABEL_17;
}
v4 = strdup("0.1~ba1f562");
*((_QWORD *)v1 + 3) = v4;
if ( !v4 )
{
LABEL_15:
free(*((void **)v1 + 2));
goto LABEL_16;
}
if ( uname(&v12) )
{
*((_QWORD *)v1 + 4) = strdup("fitbitd");
}
else
{
v10 = strlen(v12.sysname);
v11 = (char *)malloc(v10 + 9);
*((_QWORD *)v1 + 4) = v11;
if ( !v11 )
{
LABEL_14:
free(*((void **)v1 + 3));
goto LABEL_15;
}
sprintf(v11, "fitbitd-%s", v12.sysname);
}
v5 = (void *)*((_QWORD *)v1 + 4);
if ( v5 )
{
v6 = strlen(v0);
v7 = (char *)malloc(v6 + 6);
v8 = v7;
*((_QWORD *)v1 + 5) = v7;
if ( v7 )
{
strcpy(v7, v0);
strcpy(&v8[strlen(v0)], "/lock");
*((_QWORD *)v1 + 6) = 0LL;
*((_QWORD *)v1 + 7) = 0LL;
*v1 = 10;
v1[1] = 900;
return v1;
}
free(v5);
}
goto LABEL_14;
}
return v1;
}

original_code:
fitbitd_prefs_t *prefs_create(void)
{
    fitbitd_prefs_t *prefs;
    struct utsname un;
    char *cfg_home = get_config_home();
    prefs = malloc(sizeof(*prefs));
    if (!prefs)
        goto oom_prefs;
    prefs->upload_url = strdup("https://client.fitbit.com/device/tracker/uploadData");
    if (!prefs->upload_url)
        goto oom_upload_url;
    prefs->client_id = strdup("2ea32002-a079-48f4-8020-0badd22939e3");
    if (!prefs->client_id)
        goto oom_client_id;
    prefs->client_version = strdup("0.1~ba1f562");
    if (!prefs->client_version)
        goto oom_client_version;
    if (uname(&un)) {
        prefs->os_name = strdup("fitbitd");
    } else {
        prefs->os_name = malloc(8 + strlen(un.sysname) + 1);
        if (prefs->os_name)
            sprintf(prefs->os_name, "fitbitd-%s", un.sysname);
    }
    if (!prefs->os_name)
        goto oom_os_name;
    prefs->lock_filename = malloc(strlen(cfg_home) + 6);
    if (!prefs->lock_filename)
        goto oom_lock_filename;
    strcpy(prefs->lock_filename, cfg_home);
    strcpy(&prefs->lock_filename[strlen(cfg_home)], "/lock");
    prefs->dump_directory = ((void *)0);
    prefs->log_filename = ((void *)0);
    prefs->scan_delay = 10;
    prefs->sync_delay = 15 * 60;
    return prefs;
oom_lock_filename:
    free(prefs->os_name);
oom_os_name:
    free(prefs->client_version);
oom_client_version:
    free(prefs->client_id);
oom_client_id:
    free(prefs->upload_url);
oom_upload_url:
    free(prefs);
oom_prefs:
    return ((void *)0);
}

canonical_original_code:
struct fitbitd_prefs_t *prefs_create()
{
    struct fitbitd_prefs_t *prefs;
    struct utsname un;
    char *cfg_home = get_config_home();
    prefs = malloc(sizeof(*prefs));
    if (!prefs)
        goto oom_prefs;
    prefs->upload_url = strdup("https://client.fitbit.com/device/tracker/uploadData");
    if (!prefs->upload_url)
        goto oom_upload_url;
    prefs->client_id = strdup("2ea32002-a079-48f4-8020-0badd22939e3");
    if (!prefs->client_id)
        goto oom_client_id;
    prefs->client_version = strdup("0.1~ba1f562");
    if (!prefs->client_version)
        goto oom_client_version;
    if (uname(&un)) {
        prefs->os_name = strdup("fitbitd");
    } else {
        prefs->os_name = malloc(8 + strlen(un.sysname) + 1);
        if (prefs->os_name)
            sprintf(prefs->os_name, "fitbitd-%s", un.sysname);
    }
    if (!prefs->os_name)
        goto oom_os_name;
    prefs->lock_filename = malloc(strlen(cfg_home) + 6);
    if (!prefs->lock_filename)
        goto oom_lock_filename;
    strcpy(prefs->lock_filename, cfg_home);
    strcpy(&prefs->lock_filename[strlen(cfg_home)], "/lock");
    prefs->dump_directory = ((void *)0);
    prefs->log_filename = ((void *)0);
    prefs->scan_delay = 10;
    prefs->sync_delay = 15 * 60;
    return prefs;
oom_lock_filename:
    free(prefs->os_name);
oom_os_name:
    free(prefs->client_version);
oom_client_version:
    free(prefs->client_id);
oom_client_id:
    free(prefs->upload_url);
oom_upload_url:
    free(prefs);
oom_prefs:
    return ((void *)0);
}

variable_types: struct fitbitd_prefs_t { unsigned int scan_delay; unsigned int sync_delay; char *upload_url; char *client_id; char *client_version; char *os_name; char *lock_filename; char *dump_directory; char *log_filename; } *

variable_types: struct utsname { char sysname[65]; char nodename[65]; char release[65]; char version[65]; char machine[65]; char __domainname[65]; }


decompiled_code:
void __fastcall prefs_destroy(void *ptr)
{
free(*((void **)ptr + 7));
free(*((void **)ptr + 6));
free(*((void **)ptr + 5));
free(*((void **)ptr + 4));
free(*((void **)ptr + 3));
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*((void **)ptr + 7));
free(*((void **)ptr + 6));
free(*((void **)ptr + 5));
free(*((void **)ptr + 4));
free(*((void **)ptr + 3));
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void prefs_destroy(fitbitd_prefs_t *prefs)
{
    free(prefs->log_filename);
    free(prefs->dump_directory);
    free(prefs->lock_filename);
    free(prefs->os_name);
    free(prefs->client_version);
    free(prefs->client_id);
    free(prefs->upload_url);
    free(prefs);
}

canonical_original_code:
void prefs_destroy(struct fitbitd_prefs_t *prefs)
{
    free(prefs->log_filename);
    free(prefs->dump_directory);
    free(prefs->lock_filename);
    free(prefs->os_name);
    free(prefs->client_version);
    free(prefs->client_id);
    free(prefs->upload_url);
    free(prefs);
}

user_defined_types: struct fitbitd_prefs_t { unsigned int scan_delay; unsigned int sync_delay; char *upload_url; char *client_id; char *client_version; char *os_name; char *lock_filename; char *dump_directory; char *log_filename; }


variable_types:
buf
unsigned char *

variable_types:
sz
unsigned long

variable_types:
str
unsigned char *

variable_types:
cur
unsigned char *

variable_types:
start
unsigned char *

variable_types: d int

variable_types: dlast int

variable_types: phase int

variable_types:
rem
unsigned long

variable_types:
c
unsigned char

variable_types:
table
int [256]


decompiled_code:
__int64 __fastcall b64encode(__int64 a1, unsigned __int64 a2, __int64 a3, unsigned __int64 a4)
{
unsigned __int64 v6; // rdi
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rdx
unsigned int v9; // eax
unsigned __int64 v10; // r8
__int64 v12; // rdi
unsigned __int64 v13; // rax
char v14[96]; // [rsp+0h] [rbp-60h] BYREF

v6 = a4;
strcpy(v14, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
if ( a4 )
{
v7 = 2LL;
v8 = 1LL;
a4 = 0LL;
while ( 1 )
{
v9 = *(unsigned __int8 *)(a3 + v8 - 1) << 16;
if ( v8 < v6 )
v9 += *(unsigned __int8 *)(a3 + v8) << 8;
if ( v6 > v7 )
v9 += *(unsigned __int8 *)(a3 + v8 + 1);
if ( a2 <= a4 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + a4) = v14[v9 >> 18];
if ( a2 <= a4 + 1 )
return 0xFFFFFFFFLL;
v10 = a4 + 2;
*(_BYTE *)(a1 + a4 + 1) = v14[(v9 >> 12) & 0x3F];
if ( v8 < v6 )
{
if ( a2 <= v10 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + v10) = v14[(v9 >> 6) & 0x3F];
v10 = a4 + 3;
}
if ( v6 <= v7 )
{
a4 = v10;
}
else
{
if ( v10 >= a2 )
return 0xFFFFFFFFLL;
a4 = v10 + 1;
*(_BYTE *)(a1 + v10) = v14[v9 & 0x3F];
}
v7 += 3LL;
if ( v6 <= v8 + 2 )
break;
v8 += 3LL;
}
v12 = v6 - (v6 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v6) >> 64) & 0xFFFFFFFFFFFFFFFELL));
if ( (int)v12 <= 0 )
goto LABEL_26;
if ( a4 >= a2 )
return 0xFFFFFFFFLL;
v13 = a4;
while ( 1 )
{
++v13;
*(_BYTE *)(a1 + v13 - 1) = 61;
if ( (int)v13 + (int)v12 - (int)a4 > 2 )
break;
if ( a2 == v13 )
return 0xFFFFFFFFLL;
}
a4 = v13;
}
LABEL_26:
if ( a4 >= a2 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + a4) = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int64 a2, __int64 a3, unsigned __int64 a4)
{
unsigned __int64 v6; // rdi
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rdx
unsigned int v9; // eax
unsigned __int64 v10; // r8
__int64 v12; // rdi
unsigned __int64 v13; // rax
char v14[96]; // [rsp+0h] [rbp-60h] BYREF

v6 = a4;
strcpy(v14, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
if ( a4 )
{
v7 = 2LL;
v8 = 1LL;
a4 = 0LL;
while ( 1 )
{
v9 = *(unsigned __int8 *)(a3 + v8 - 1) << 16;
if ( v8 < v6 )
v9 += *(unsigned __int8 *)(a3 + v8) << 8;
if ( v6 > v7 )
v9 += *(unsigned __int8 *)(a3 + v8 + 1);
if ( a2 <= a4 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + a4) = v14[v9 >> 18];
if ( a2 <= a4 + 1 )
return 0xFFFFFFFFLL;
v10 = a4 + 2;
*(_BYTE *)(a1 + a4 + 1) = v14[(v9 >> 12) & 0x3F];
if ( v8 < v6 )
{
if ( a2 <= v10 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + v10) = v14[(v9 >> 6) & 0x3F];
v10 = a4 + 3;
}
if ( v6 <= v7 )
{
a4 = v10;
}
else
{
if ( v10 >= a2 )
return 0xFFFFFFFFLL;
a4 = v10 + 1;
*(_BYTE *)(a1 + v10) = v14[v9 & 0x3F];
}
v7 += 3LL;
if ( v6 <= v8 + 2 )
break;
v8 += 3LL;
}
v12 = v6 - (v6 / 3 + (((0xAAAAAAAAAAAAAAABLL * (unsigned __int128)v6) >> 64) & 0xFFFFFFFFFFFFFFFELL));
if ( (int)v12 <= 0 )
goto LABEL_26;
if ( a4 >= a2 )
return 0xFFFFFFFFLL;
v13 = a4;
while ( 1 )
{
++v13;
*(_BYTE *)(a1 + v13 - 1) = 61;
if ( (int)v13 + (int)v12 - (int)a4 > 2 )
break;
if ( a2 == v13 )
return 0xFFFFFFFFLL;
}
a4 = v13;
}
LABEL_26:
if ( a4 >= a2 )
return 0xFFFFFFFFLL;
*(_BYTE *)(a1 + a4) = 0;
return 0LL;
}

original_code:
int b64encode(uint8_t *buf, size_t buf_sz, const uint8_t* data, size_t data_sz)
{
   const char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
   size_t buf_idx = 0;
   size_t x;
   uint32_t n = 0;
   int padCount = data_sz % 3;
   uint8_t n0, n1, n2, n3;
   for (x = 0; x < data_sz; x += 3)
   {
      n = data[x] << 16;
      if((x+1) < data_sz)
         n += data[x+1] << 8;
      if((x+2) < data_sz)
         n += data[x+2];
      n0 = (uint8_t)(n >> 18) & 63;
      n1 = (uint8_t)(n >> 12) & 63;
      n2 = (uint8_t)(n >> 6) & 63;
      n3 = (uint8_t)n & 63;
      if(buf_idx >= buf_sz) return -1;
      buf[buf_idx++] = base64chars[n0];
      if(buf_idx >= buf_sz) return -1;
      buf[buf_idx++] = base64chars[n1];
      if((x+1) < data_sz)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = base64chars[n2];
      }
      if((x+2) < data_sz)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = base64chars[n3];
      }
   }
   if (padCount > 0)
   {
      for (; padCount < 3; padCount++)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = '=';
      }
   }
   if(buf_idx >= buf_sz) return -1;
   buf[buf_idx] = 0;
   return 0;
}

canonical_original_code:
int b64encode(unsigned char *buf, unsigned long buf_sz, unsigned char *data, unsigned long data_sz)
{
   char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
   unsigned long buf_idx = 0;
   unsigned long x;
   unsigned int n = 0;
   int padCount = data_sz % 3;
   unsigned char n0; unsigned char n1; unsigned char n2; unsigned char n3;
   for (x = 0; x < data_sz; x += 3)
   {
      n = data[x] << 16;
      if((x+1) < data_sz)
         n += data[x+1] << 8;
      if((x+2) < data_sz)
         n += data[x+2];
      n0 = (unsigned char)(n >> 18) & 63;
      n1 = (unsigned char)(n >> 12) & 63;
      n2 = (unsigned char)(n >> 6) & 63;
      n3 = (unsigned char)n & 63;
      if(buf_idx >= buf_sz) return -1;
      buf[buf_idx++] = base64chars[n0];
      if(buf_idx >= buf_sz) return -1;
      buf[buf_idx++] = base64chars[n1];
      if((x+1) < data_sz)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = base64chars[n2];
      }
      if((x+2) < data_sz)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = base64chars[n3];
      }
   }
   if (padCount > 0)
   {
      for (; padCount < 3; padCount++)
      {
         if(buf_idx >= buf_sz) return -1;
         buf[buf_idx++] = '=';
      }
   }
   if(buf_idx >= buf_sz) return -1;
   buf[buf_idx] = 0;
   return 0;
}

variable_types:
data
unsigned char *

variable_types:
x
unsigned long

variable_types:
n
unsigned int

variable_types: padCount int


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

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

original_code:
void ant_message_destroy(ant_message_t *msg)
{
    free(msg->data);
    free(msg);
}

canonical_original_code:
void ant_message_destroy(struct ant_message_t *msg)
{
    free(msg->data);
    free(msg);
}

variable_types: struct ant_message_t { unsigned char id; unsigned char len; unsigned char *data; } *


original_code:
int ant_message_encode(ant_message_t *msg, uint8_t *buf, size_t sz, size_t *len)
{
    uint8_t cksum = 0;
    int i;
    if (sz < (msg->len + 4)) {
        do { fprintf(stderr, "D:" "ant-message" ": " "buffer too small\n"); fflush(stderr); } while (0);
        return -1;
    }
    buf[0] = 0xa4;
    buf[1] = msg->len;
    buf[2] = msg->id;
    memcpy(&buf[3], msg->data, msg->len);
    for (i = 0; i < (3 + msg->len); i++)
        cksum ^= buf[i];
    buf[3+msg->len] = cksum;
    if (len)
        *len = 4 + msg->len;
    return 0;
}

canonical_original_code:
int ant_message_encode(struct ant_message_t *msg, unsigned char *buf, unsigned long sz, unsigned long *len)
{
    unsigned char cksum = 0;
    int i;
    if (sz < (msg->len + 4)) {
        do { fprintf(stderr, "D:" "ant-message" ": " "buffer too small\n"); fflush(stderr); } while (0);
        return -1;
    }
    buf[0] = 0xa4;
    buf[1] = msg->len;
    buf[2] = msg->id;
    memcpy(&buf[3], msg->data, msg->len);
    for (i = 0; i < (3 + msg->len); i++)
        cksum ^= buf[i];
    buf[3+msg->len] = cksum;
    if (len)
        *len = 4 + msg->len;
    return 0;
}

variable_types:
len
unsigned long *

variable_types:
cksum
unsigned char

variable_types: i int

user_defined_types: struct ant_message_t { unsigned char id; unsigned char len; unsigned char *data; }


decompiled_code:
__kernel_long_t get_uptime()
{
int v0; // edx
__kernel_long_t result; // rax
int *v2; // rax
struct sysinfo v3; // [rsp+0h] [rbp-78h] BYREF

v0 = sysinfo(&v3);
result = v3.uptime;
if ( v0 )
{
v2 = _errno_location();
fprintf(stderr, "E:fitbitd-utils: sysinfo failure %d\n", (unsigned int)*v2);
fflush(stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__kernel_long_t func0()
{
int v0; // edx
__kernel_long_t result; // rax
int *v2; // rax
struct sysinfo v3; // [rsp+0h] [rbp-78h] BYREF

v0 = sysinfo(&v3);
result = v3.uptime;
if ( v0 )
{
v2 = _errno_location();
fprintf(stderr, "E:fitbitd-utils: sysinfo failure %d\n", (unsigned int)*v2);
fflush(stderr);
result = 0LL;
}
return result;
}

original_code:
long get_uptime(void)
{
    struct sysinfo info;
    if (sysinfo(&info)) {
        do { fprintf(stderr, "E:" "fitbitd-utils" ": " "sysinfo failure %d\n", (*__errno_location ())); fflush(stderr); } while (0);
        return 0;
    }
    return info.uptime;
}

canonical_original_code:
long get_uptime()
{
    struct sysinfo info;
    if (sysinfo(&info)) {
        do { fprintf(stderr, "E:" "fitbitd-utils" ": " "sysinfo failure %d\n", (*__errno_location ())); fflush(stderr); } while (0);
        return 0;
    }
    return info.uptime;
}

variable_types: struct sysinfo { long uptime; unsigned long loads[3]; unsigned long totalram; unsigned long freeram; unsigned long sharedram; unsigned long bufferram; unsigned long totalswap; unsigned long freeswap; unsigned short procs; unsigned short pad; unsigned long totalhigh; unsigned long freehigh; unsigned int mem_unit; char _f[-1]; }

return_type: long


repo: pauldmccarthy/ccnet

decompiled_code:
__int64 __fastcall stats_edge_betweenness(__int64 a1, __int64 a2, void *a3)
{
unsigned int v4; // er12
unsigned int v5; // ebx
const void *v7; // rsi
int v8; // [rsp+Ch] [rbp-ACh] BYREF
char v9[16]; // [rsp+10h] [rbp-A8h] BYREF
__int64 v10; // [rsp+20h] [rbp-98h]
char v11[16]; // [rsp+50h] [rbp-68h] BYREF
__int64 v12; // [rsp+60h] [rbp-58h]

v12 = 0LL;
v10 = 0LL;
v4 = graph_num_neighbours(a1, a2);
stats_cache_node_component(a1, (unsigned int)a2, &v8);
if ( (unsigned __int8)edge_array_create(a1, 8LL, v11)
|| (unsigned __int8)edge_array_create(a1, 8LL, v9)
|| (v5 = all_edge_betweenness(a1, (__int64)v11, (__int64)v9, v8), (_BYTE)v5) )
{
if ( v12 )
edge_array_free(v11);
v5 = 1;
if ( v10 )
edge_array_free(v9);
}
else
{
if ( a3 )
{
v7 = (const void *)edge_array_get_all(v9, (unsigned int)a2);
memcpy(a3, v7, 8LL * v4);
}
edge_array_free(v11);
edge_array_free(v9);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, void *a3)
{
unsigned int v4; // er12
unsigned int v5; // ebx
const void *v7; // rsi
int v8; // [rsp+Ch] [rbp-ACh] BYREF
char v9[16]; // [rsp+10h] [rbp-A8h] BYREF
__int64 v10; // [rsp+20h] [rbp-98h]
char v11[16]; // [rsp+50h] [rbp-68h] BYREF
__int64 v12; // [rsp+60h] [rbp-58h]

v12 = 0LL;
v10 = 0LL;
v4 = graph_num_neighbours(a1, a2);
stats_cache_node_component(a1, (unsigned int)a2, &v8);
if ( (unsigned __int8)edge_array_create(a1, 8LL, v11)
|| (unsigned __int8)edge_array_create(a1, 8LL, v9)
|| (v5 = func1(a1, (__int64)v11, (__int64)v9, v8), (_BYTE)v5) )
{
if ( v12 )
edge_array_free(v11);
v5 = 1;
if ( v10 )
edge_array_free(v9);
}
else
{
if ( a3 )
{
v7 = (const void *)edge_array_get_all(v9, (unsigned int)a2);
memcpy(a3, v7, 8LL * v4);
}
edge_array_free(v11);
edge_array_free(v9);
}
return v5;
}

original_code:
uint8_t stats_edge_betweenness(graph_t *g, uint32_t v, double *betweenness) {
  uint32_t nnbrs;
  edge_array_t betw;
  edge_array_t ttlbetw;
  uint32_t cmp;
  betw.vals = ((void *)0);
  ttlbetw.vals = ((void *)0);
  nnbrs = graph_num_neighbours( g, v);
  stats_cache_node_component(g, v, &cmp);
  if (edge_array_create(g, sizeof(double), &betw)) goto fail;
  if (edge_array_create(g, sizeof(double), &ttlbetw)) goto fail;
  if (_all_edge_betweenness(g, &betw, &ttlbetw, cmp)) goto fail;
  if (betweenness != ((void *)0))
    memcpy(betweenness,
           edge_array_get_all(&ttlbetw, v),
           nnbrs*sizeof(double));
  edge_array_free(&betw);
  edge_array_free(&ttlbetw);
  return 0;
fail:
  if (betw.vals != ((void *)0)) edge_array_free(&betw);
  if (ttlbetw.vals != ((void *)0)) edge_array_free(&ttlbetw);
  return 1;
}

canonical_original_code:
unsigned char stats_edge_betweenness(struct _graph *g, unsigned int v, double *betweenness) {
  unsigned int nnbrs;
  struct _edge_array betw;
  struct _edge_array ttlbetw;
  unsigned int cmp;
  betw.vals = ((void *)0);
  ttlbetw.vals = ((void *)0);
  nnbrs = graph_num_neighbours( g, v);
  stats_cache_node_component(g, v, &cmp);
  if (edge_array_create(g, sizeof(double), &betw)) goto fail;
  if (edge_array_create(g, sizeof(double), &ttlbetw)) goto fail;
  if (_all_edge_betweenness(g, &betw, &ttlbetw, cmp)) goto fail;
  if (betweenness != ((void *)0))
    memcpy(betweenness,
           edge_array_get_all(&ttlbetw, v),
           nnbrs*sizeof(double));
  edge_array_free(&betw);
  edge_array_free(&ttlbetw);
  return 0;
fail:
  if (betw.vals != ((void *)0)) edge_array_free(&betw);
  if (ttlbetw.vals != ((void *)0)) edge_array_free(&ttlbetw);
  return 1;
}

variable_types:
v
unsigned int

variable_types:
betweenness
double *

variable_types:
nnbrs
unsigned int

variable_types: struct _edge_array { 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 *); } *g; unsigned short valsz; struct _array *vals; struct _graph_event_listener { unsigned long id; void *ctx; void (*edge_added)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx, float wt); void (*edge_removed)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx); } gel; }

variable_types:
cmp
unsigned int

return_type: unsigned char

user_defined_types: struct _edge_array { struct _graph *g; unsigned short valsz; struct _array *vals; struct _graph_event_listener gel; }

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


decompiled_code:
__int64 __fastcall graph_log_init(__int64 a1)
{
void *v1; // rax
void *v2; // rbx
__int64 result; // rax

v1 = calloc(0x28uLL, 1uLL);
if ( !v1 )
return 1LL;
v2 = v1;
result = array_create(v1, 8LL, 10LL);
if ( (_BYTE)result )
{
free(v2);
result = 1LL;
}
else
{
*(_QWORD *)(a1 + 208) = v2;
*(_QWORD *)(a1 + 248) = log_free;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
void *v1; // rax
void *v2; // rbx
__int64 result; // rax

v1 = calloc(0x28uLL, 1uLL);
if ( !v1 )
return 1LL;
v2 = v1;
result = array_create(v1, 8LL, 10LL);
if ( (_BYTE)result )
{
free(v2);
result = 1LL;
}
else
{
*(_QWORD *)(a1 + 208) = v2;
*(_QWORD *)(a1 + 248) = log_free;
}
return result;
}

original_code:
uint8_t graph_log_init(graph_t *g) {
  array_t *log;
  log = calloc(sizeof(array_t), 1);
  if (log == ((void *)0)) goto fail;
  if (array_create(log, sizeof(char *), 10)) goto fail;
  g->ctx[ 2] = log;
  g->ctx_free[2] = _log_free;
  return 0;
fail:
  if (log != ((void *)0)) free(log);
  return 1;
}

canonical_original_code:
unsigned char graph_log_init(struct _graph *g) {
  struct _array *log;
  log = calloc(sizeof(array_t), 1);
  if (log == ((void *)0)) goto fail;
  if (array_create(log, sizeof(char *), 10)) goto fail;
  g->ctx[ 2] = log;
  g->ctx_free[2] = _log_free;
  return 0;
fail:
  if (log != ((void *)0)) free(log);
  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: 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:
bool __fastcall graph_log_exists(__int64 a1)
{
return *(_QWORD *)(a1 + 208) != 0LL;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1)
{
return *(_QWORD *)(a1 + 208) != 0LL;
}

original_code:
uint8_t graph_log_exists(graph_t *g) {
  return g->ctx[2] != ((void *)0) ? 1 : 0;
}

canonical_original_code:
unsigned char graph_log_exists(struct _graph *g) {
  return g->ctx[2] != ((void *)0) ? 1 : 0;
}

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_log_num_msgs(__int64 a1)
{
__int64 v1; // rax
__int64 result; // rax

v1 = *(_QWORD *)(a1 + 208);
if ( v1 )
result = *(unsigned __int16 *)(v1 + 4);
else
result = 0LL;
return result;
}

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

v1 = *(_QWORD *)(a1 + 208);
if ( v1 )
result = *(unsigned __int16 *)(v1 + 4);
else
result = 0LL;
return result;
}

original_code:
uint16_t graph_log_num_msgs(graph_t *g) {
  array_t *log;
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  return log->size;
  return 0;
}

canonical_original_code:
unsigned short graph_log_num_msgs(struct _graph *g) {
  struct _array *log;
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  return log->size;
  return 0;
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall graph_log_get_msg(__int64 a1, unsigned __int16 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 208);
if ( result )
{
if ( (unsigned int)a2 >= *(_DWORD *)(result + 4) )
result = 0LL;
else
result = *(_QWORD *)array_getd(*(_QWORD *)(a1 + 208), a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int16 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 208);
if ( result )
{
if ( (unsigned int)a2 >= *(_DWORD *)(result + 4) )
result = 0LL;
else
result = *(_QWORD *)array_getd(*(_QWORD *)(a1 + 208), a2);
}
return result;
}

original_code:
char * graph_log_get_msg(graph_t *g, uint16_t i) {
  array_t *log;
  log = g->ctx[2];
  if (log == ((void *)0)) return ((void *)0);
  if (i >= log->size) return ((void *)0);
  return *(char **)array_getd(log, i);
}

canonical_original_code:
char *graph_log_get_msg(struct _graph *g, unsigned short i) {
  struct _array *log;
  log = g->ctx[2];
  if (log == ((void *)0)) return ((void *)0);
  if (i >= log->size) return ((void *)0);
  return *(char **)array_getd(log, i);
}

variable_types:
i
unsigned short


decompiled_code:
__int64 __fastcall graph_log_add(__int64 a1, const char *a2)
{
__int64 v2; // rbp
__int64 result; // rax
int v4; // eax
void *v5; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
v2 = *(_QWORD *)(a1 + 208);
result = 0LL;
if ( v2 )
{
v4 = strlen(a2);
v5 = malloc((unsigned int)(v4 + 1));
ptr = v5;
if ( v5 )
{
strcpy((char *)v5, a2);
result = array_append(v2, &ptr);
if ( (_BYTE)result )
{
result = 1LL;
if ( ptr )
{
free(ptr);
result = 1LL;
}
}
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, const char *a2)
{
__int64 v2; // rbp
__int64 result; // rax
int v4; // eax
void *v5; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
v2 = *(_QWORD *)(a1 + 208);
result = 0LL;
if ( v2 )
{
v4 = strlen(a2);
v5 = malloc((unsigned int)(v4 + 1));
ptr = v5;
if ( v5 )
{
strcpy((char *)v5, a2);
result = array_append(v2, &ptr);
if ( (_BYTE)result )
{
result = 1LL;
if ( ptr )
{
free(ptr);
result = 1LL;
}
}
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
uint8_t graph_log_add(graph_t *g, char *msg) {
  array_t *log;
  char *msgcpy;
  uint32_t len;
  msgcpy = ((void *)0);
  len = strlen(msg);
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  msgcpy = malloc(len+1);
  if (msgcpy == ((void *)0)) goto fail;
  strcpy(msgcpy, msg);
  if (array_append(log, &msgcpy)) goto fail;
  return 0;
fail:
  if (msgcpy!= ((void *)0)) free(msgcpy);
  return 1;
}

canonical_original_code:
unsigned char graph_log_add(struct _graph *g, char *msg) {
  struct _array *log;
  char *msgcpy;
  unsigned int len;
  msgcpy = ((void *)0);
  len = strlen(msg);
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  msgcpy = malloc(len+1);
  if (msgcpy == ((void *)0)) goto fail;
  strcpy(msgcpy, msg);
  if (array_append(log, &msgcpy)) goto fail;
  return 0;
fail:
  if (msgcpy!= ((void *)0)) free(msgcpy);
  return 1;
}

variable_types:
msg
char *

variable_types:
msgcpy
char *

variable_types:
len
unsigned int


decompiled_code:
bool __fastcall graph_log_copy(__int64 a1, __int64 a2)
{
bool result; // al
unsigned __int16 v4; // ax
int v5; // er13
int v6; // ebx
char v7; // dl
const char *v8; // rsi

if ( !a2 )
return 1;
if ( !a1 )
return 1;
if ( graph_log_exists(a2) || (v7 = graph_log_init(a2), result = 1, !v7) )
{
result = graph_log_exists(a1);
if ( result )
{
v4 = graph_log_num_msgs(a1);
v5 = v4;
if ( v4 )
{
v6 = 0;
do
{
v8 = (const char *)graph_log_get_msg(a1, v6);
if ( v8 && (unsigned __int8)graph_log_add(a2, v8) )
return 1;
++v6;
}
while ( v6 != v5 );
result = 0;
}
else
{
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
bool __fastcall func6(__int64 a1, __int64 a2)
{
bool result; // al
unsigned __int16 v4; // ax
int v5; // er13
int v6; // ebx
char v7; // dl
const char *v8; // rsi

if ( !a2 )
return 1;
if ( !a1 )
return 1;
if ( func2(a2) || (v7 = func1(a2), result = 1, !v7) )
{
result = func2(a1);
if ( result )
{
v4 = func3(a1);
v5 = v4;
if ( v4 )
{
v6 = 0;
do
{
v8 = (const char *)func4(a1, v6);
if ( v8 && (unsigned __int8)func5(a2, v8) )
return 1;
++v6;
}
while ( v6 != v5 );
result = 0;
}
else
{
result = 0;
}
}
}
return result;
}

original_code:
uint8_t graph_log_copy(graph_t *gin, graph_t *gout) {
  uint32_t i;
  uint16_t nmsgs;
  char *msg;
  if (gin == ((void *)0)) goto fail;
  if (gout == ((void *)0)) goto fail;
  if (!graph_log_exists(gout)) {
    if (graph_log_init(gout))
      goto fail;
  }
  if (!graph_log_exists(gin)) return 0;
  nmsgs = graph_log_num_msgs(gin);
  for (i = 0; i < nmsgs; i++) {
    msg = graph_log_get_msg(gin, i);
    if (msg == ((void *)0)) continue;
    if (graph_log_add(gout, msg)) goto fail;
  }
  return 0;
fail:
  return 1;
}

canonical_original_code:
unsigned char graph_log_copy(struct _graph *gin, struct _graph *gout) {
  unsigned int i;
  unsigned short nmsgs;
  char *msg;
  if (gin == ((void *)0)) goto fail;
  if (gout == ((void *)0)) goto fail;
  if (!graph_log_exists(gout)) {
    if (graph_log_init(gout))
      goto fail;
  }
  if (!graph_log_exists(gin)) return 0;
  nmsgs = graph_log_num_msgs(gin);
  for (i = 0; i < nmsgs; i++) {
    msg = graph_log_get_msg(gin, i);
    if (msg == ((void *)0)) continue;
    if (graph_log_add(gout, msg)) goto fail;
  }
  return 0;
fail:
  return 1;
}

variable_types:
gout
struct _graph *

variable_types:
i
unsigned int

variable_types:
nmsgs
unsigned short


decompiled_code:
__int64 __fastcall graph_log_total_len(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
unsigned __int64 v3; // rbx
const char **v4; // rax

v1 = *(_QWORD *)(a1 + 208);
if ( !v1 )
return 0;
if ( !*(_DWORD *)(v1 + 4) )
return 0;
v2 = 0;
v3 = 0LL;
do
{
v4 = (const char **)array_getd(v1, (unsigned int)v3);
v2 += strlen(*v4);
++v3;
}
while ( *(unsigned int *)(v1 + 4) > v3 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
unsigned __int64 v3; // rbx
const char **v4; // rax

v1 = *(_QWORD *)(a1 + 208);
if ( !v1 )
return 0;
if ( !*(_DWORD *)(v1 + 4) )
return 0;
v2 = 0;
v3 = 0LL;
do
{
v4 = (const char **)array_getd(v1, (unsigned int)v3);
v2 += strlen(*v4);
++v3;
}
while ( *(unsigned int *)(v1 + 4) > v3 );
return v2;
}

original_code:
uint16_t graph_log_total_len(graph_t *g) {
  uint64_t i;
  uint16_t len;
  array_t *log;
  char *msg;
  len = 0;
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  for (i = 0; i < log->size; i++) {
    msg = *(char **)array_getd(log, i);
    len += strlen(msg);
  }
  return len;
}

canonical_original_code:
unsigned short graph_log_total_len(struct _graph *g) {
  unsigned long i;
  unsigned short len;
  struct _array *log;
  char *msg;
  len = 0;
  log = g->ctx[2];
  if (log == ((void *)0)) return 0;
  for (i = 0; i < log->size; i++) {
    msg = *(char **)array_getd(log, i);
    len += strlen(msg);
  }
  return len;
}

variable_types:
i
unsigned long

variable_types:
len
unsigned short


decompiled_code:
__int64 __fastcall graph_log_import(__int64 a1, const char *a2, const char *a3)
{
const char *v3; // rbp
const char *v4; // r14
__int64 v5; // r15
int v6; // ebx
int v7; // er12
unsigned __int16 v8; // ax
unsigned __int16 v9; // r14
int v11; // ebx
char *v12; // rax
unsigned __int16 v13; // bx
_BYTE *v14; // rax
_BYTE *v15; // r13
void *v17; // [rsp+18h] [rbp-40h] BYREF

v3 = a2;
v4 = a3;
v17 = 0LL;
v5 = *(_QWORD *)(a1 + 208);
v6 = strlen(a2);
v7 = v6;
v8 = strlen(v4);
if ( !v5 )
return 0LL;
v9 = v8;
if ( v6 <= 0 )
return 0LL;
while ( 1 )
{
v12 = strstr(v3, a3);
v13 = (_WORD)v12 - (_WORD)v3;
if ( !v12 )
v13 = v7;
if ( v13 )
break;
LABEL_4:
v11 = v9 + v13;
v3 += v11;
v7 -= v11;
if ( v7 <= 0 )
return 0LL;
}
v14 = calloc(v13 + 1LL, 1uLL);
v15 = v14;
v17 = v14;
if ( v14 )
{
memcpy(v14, v3, v13);
v15[v13] = 0;
if ( (unsigned __int8)array_append(v5, &v17) )
return 1LL;
goto LABEL_4;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, const char *a2, const char *a3)
{
const char *v3; // rbp
const char *v4; // r14
__int64 v5; // r15
int v6; // ebx
int v7; // er12
unsigned __int16 v8; // ax
unsigned __int16 v9; // r14
int v11; // ebx
char *v12; // rax
unsigned __int16 v13; // bx
_BYTE *v14; // rax
_BYTE *v15; // r13
void *v17; // [rsp+18h] [rbp-40h] BYREF

v3 = a2;
v4 = a3;
v17 = 0LL;
v5 = *(_QWORD *)(a1 + 208);
v6 = strlen(a2);
v7 = v6;
v8 = strlen(v4);
if ( !v5 )
return 0LL;
v9 = v8;
if ( v6 <= 0 )
return 0LL;
while ( 1 )
{
v12 = strstr(v3, a3);
v13 = (_WORD)v12 - (_WORD)v3;
if ( !v12 )
v13 = v7;
if ( v13 )
break;
LABEL_4:
v11 = v9 + v13;
v3 += v11;
v7 -= v11;
if ( v7 <= 0 )
return 0LL;
}
v14 = calloc(v13 + 1LL, 1uLL);
v15 = v14;
v17 = v14;
if ( v14 )
{
memcpy(v14, v3, v13);
v15[v13] = 0;
if ( (unsigned __int8)array_append(v5, &v17) )
return 1LL;
goto LABEL_4;
}
return 1LL;
}

original_code:
uint8_t graph_log_import(graph_t *g, char *data, char *delim) {
  int32_t len;
  uint16_t dlen;
  array_t *log;
  char *msg;
  char *substr;
  uint16_t substrlen;
  msg = ((void *)0);
  log = g->ctx[2];
  len = strlen(data);
  dlen = strlen(delim);
  if (log == ((void *)0)) return 0;
  while (len > 0) {
    substr = strstr(data, delim);
    if (substr == ((void *)0)) substrlen = len;
    else substrlen = substr - data;
    if (substrlen > 0) {
      msg = calloc(substrlen+1, 1);
      if (msg == ((void *)0)) goto fail;
      memcpy(msg, data, substrlen);
      msg[substrlen] = '\0';
      if (array_append(log, &msg)) goto fail;
    }
    data += (substrlen + dlen);
    len -= (substrlen + dlen);
  }
  return 0;
fail:
  return 1;
}

canonical_original_code:
unsigned char graph_log_import(struct _graph *g, char *data, char *delim) {
  int len;
  unsigned short dlen;
  struct _array *log;
  char *msg;
  char *substr;
  unsigned short substrlen;
  msg = ((void *)0);
  log = g->ctx[2];
  len = strlen(data);
  dlen = strlen(delim);
  if (log == ((void *)0)) return 0;
  while (len > 0) {
    substr = strstr(data, delim);
    if (substr == ((void *)0)) substrlen = len;
    else substrlen = substr - data;
    if (substrlen > 0) {
      msg = calloc(substrlen+1, 1);
      if (msg == ((void *)0)) goto fail;
      memcpy(msg, data, substrlen);
      msg[substrlen] = '\0';
      if (array_append(log, &msg)) goto fail;
    }
    data += (substrlen + dlen);
    len -= (substrlen + dlen);
  }
  return 0;
fail:
  return 1;
}

variable_types:
data
char *

variable_types:
delim
char *

variable_types: len int

variable_types:
dlen
unsigned short

variable_types:
substr
char *

variable_types:
substrlen
unsigned short


decompiled_code:
__int64 __fastcall graph_pathlength(__int64 a1, int a2, int a3, __int64 a4)
{
char v4; // dl
__int64 result; // rax
char v6; // dl
char v7; // dl
char v8; // dl
int v9; // [rsp+Ch] [rbp-9Ch] BYREF
int v10[2]; // [rsp+10h] [rbp-98h] BYREF
unsigned int v11; // [rsp+18h] [rbp-90h]
char v12[40]; // [rsp+20h] [rbp-88h] BYREF
char v13[40]; // [rsp+48h] [rbp-60h] BYREF
char v14[40]; // [rsp+70h] [rbp-38h] BYREF
__int64 v15; // [rsp+98h] [rbp-10h]

v9 = a2;
v10[0] = a2;
v10[1] = a3;
v11 = 0;
v15 = a4;
v4 = array_create(v12, 4LL, 50LL);
result = 0LL;
if ( !v4 )
{
v6 = array_create(v13, 4LL, 50LL);
result = 0LL;
if ( !v6 )
{
v7 = array_create(v14, 4LL, 10LL);
result = 0LL;
if ( !v7 )
{
array_append(v14, 0LL);
v8 = bfs(a1, &v9, 1LL, 0LL, v10);
result = 0LL;
if ( !v8 )
{
array_free(v12);
array_free(v13);
array_free(v14);
result = v11;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, int a3, __int64 a4)
{
char v4; // dl
__int64 result; // rax
char v6; // dl
char v7; // dl
char v8; // dl
int v9; // [rsp+Ch] [rbp-9Ch] BYREF
int v10[2]; // [rsp+10h] [rbp-98h] BYREF
unsigned int v11; // [rsp+18h] [rbp-90h]
char v12[40]; // [rsp+20h] [rbp-88h] BYREF
char v13[40]; // [rsp+48h] [rbp-60h] BYREF
char v14[40]; // [rsp+70h] [rbp-38h] BYREF
__int64 v15; // [rsp+98h] [rbp-10h]

v9 = a2;
v10[0] = a2;
v10[1] = a3;
v11 = 0;
v15 = a4;
v4 = array_create(v12, 4LL, 50LL);
result = 0LL;
if ( !v4 )
{
v6 = array_create(v13, 4LL, 50LL);
result = 0LL;
if ( !v6 )
{
v7 = array_create(v14, 4LL, 10LL);
result = 0LL;
if ( !v7 )
{
array_append(v14, 0LL);
v8 = bfs(a1, &v9, 1LL, 0LL, v10);
result = 0LL;
if ( !v8 )
{
array_free(v12);
array_free(v13);
array_free(v14);
result = v11;
}
}
}
}
return result;
}

original_code:
uint32_t graph_pathlength(
  graph_t *g, uint32_t u, uint32_t v, array_t *path) {
  ctx_t context;
  context.src = u;
  context.dest = v;
  context.pathlength = 0;
  context.path = path;
  if (array_create(&(context.nodes), sizeof(uint32_t), 50)) goto fail;
  if (array_create(&(context.parents), sizeof(uint32_t), 50)) goto fail;
  if (array_create(&(context.offsets), sizeof(uint32_t), 10)) goto fail;
  array_append(&(context.offsets), 0);
  if (bfs(g, &u, 1, ((void *)0), &context, &context, _bfs_cb, _expand_cb))
    goto fail;
  array_free(&(context.nodes));
  array_free(&(context.parents));
  array_free(&(context.offsets));
  return context.pathlength;
fail:
  return 0;
}

canonical_original_code:
unsigned int graph_pathlength(struct _graph *g, unsigned int u, unsigned int v, struct _array *path) {
  struct _ctx context;
  context.src = u;
  context.dest = v;
  context.pathlength = 0;
  context.path = path;
  if (array_create(&(context.nodes), sizeof(unsigned int), 50)) goto fail;
  if (array_create(&(context.parents), sizeof(unsigned int), 50)) goto fail;
  if (array_create(&(context.offsets), sizeof(unsigned int), 10)) goto fail;
  array_append(&(context.offsets), 0);
  if (bfs(g, &u, 1, ((void *)0), &context, &context, _bfs_cb, _expand_cb))
    goto fail;
  array_free(&(context.nodes));
  array_free(&(context.parents));
  array_free(&(context.offsets));
  return context.pathlength;
fail:
  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:
u
unsigned int

variable_types:
v
unsigned int

variable_types:
path
struct _array *

variable_types: struct _ctx { unsigned int src; unsigned int dest; unsigned int pathlength; 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); } nodes; struct _array parents; struct _array offsets; struct _array *path; }

return_type: unsigned int

user_defined_types: struct _ctx { unsigned int src; unsigned int dest; unsigned int pathlength; struct _array nodes; struct _array parents; struct _array offsets; struct _array *path; }


decompiled_code:
__int64 __fastcall stats_num_components(__int64 a1, unsigned int a2, __int64 a3, void *a4)
{
unsigned int v6; // ebp
void *v7; // rbx
__int64 v8; // rbx
__int64 result; // rax
unsigned __int64 i; // rbx
void *v11; // [rsp+0h] [rbp-58h] BYREF
void *ptr; // [rsp+8h] [rbp-50h]
unsigned int v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+14h] [rbp-44h] BYREF
unsigned int v15; // [rsp+18h] [rbp-40h] BYREF
int v16[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v11 = 0LL;
ptr = 0LL;
v13 = 0;
v14 = 0;
v6 = graph_num_nodes(a1);
if ( a3 )
array_clear(a3);
v7 = calloc(v6, 1uLL);
v11 = v7;
if ( !v7 )
goto LABEL_15;
ptr = calloc(v6, 4uLL);
if ( !ptr )
goto LABEL_14;
if ( !v6 )
{
v15 = v13;
stats_cache_add(a1, 5LL, 0LL, 4LL);
stats_cache_add(a1, 20LL, 2LL, 4LL);
stats_cache_update(a1, 5LL, 0LL, -1LL, &v15);
LABEL_22:
free(v11);
free(ptr);
return v13;
}
v8 = 0LL;
while ( *((_BYTE *)v11 + v8) )
{
LABEL_7:
if ( v6 == ++v8 )
{
if ( a4 )
memcpy(a4, ptr, 4LL * v6);
v15 = v13;
stats_cache_add(a1, 5LL, 0LL, 4LL);
stats_cache_add(a1, 20LL, 2LL, 4LL);
stats_cache_update(a1, 5LL, 0LL, -1LL, &v15);
for ( i = 0LL; i < v6; ++i )
stats_cache_update(a1, 20LL, (unsigned int)i, -1LL, (char *)ptr + 4 * i);
goto LABEL_22;
}
}
v14 = 1;
*((_DWORD *)ptr + v8) = v13;
v16[0] = v8;
*((_BYTE *)v11 + v8) = 1;
if ( !(unsigned __int8)bfs(a1, v16, 1LL, 0LL, &v11, 0LL, bfs_cb, 0LL) )
{
if ( v14 >= a2 )
{
++v13;
if ( a3 )
array_append(a3, &v14);
}
goto LABEL_7;
}
v7 = v11;
if ( !v11 )
goto LABEL_15;
LABEL_14:
free(v7);
LABEL_15:
result = 0LL;
if ( ptr )
{
free(ptr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2, __int64 a3, void *a4)
{
unsigned int v6; // ebp
void *v7; // rbx
__int64 v8; // rbx
__int64 result; // rax
unsigned __int64 i; // rbx
void *v11; // [rsp+0h] [rbp-58h] BYREF
void *ptr; // [rsp+8h] [rbp-50h]
unsigned int v13; // [rsp+10h] [rbp-48h]
unsigned int v14; // [rsp+14h] [rbp-44h] BYREF
unsigned int v15; // [rsp+18h] [rbp-40h] BYREF
int v16[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v11 = 0LL;
ptr = 0LL;
v13 = 0;
v14 = 0;
v6 = graph_num_nodes(a1);
if ( a3 )
array_clear(a3);
v7 = calloc(v6, 1uLL);
v11 = v7;
if ( !v7 )
goto LABEL_15;
ptr = calloc(v6, 4uLL);
if ( !ptr )
goto LABEL_14;
if ( !v6 )
{
v15 = v13;
stats_cache_add(a1, 5LL, 0LL, 4LL);
stats_cache_add(a1, 20LL, 2LL, 4LL);
stats_cache_update(a1, 5LL, 0LL, -1LL, &v15);
LABEL_22:
free(v11);
free(ptr);
return v13;
}
v8 = 0LL;
while ( *((_BYTE *)v11 + v8) )
{
LABEL_7:
if ( v6 == ++v8 )
{
if ( a4 )
memcpy(a4, ptr, 4LL * v6);
v15 = v13;
stats_cache_add(a1, 5LL, 0LL, 4LL);
stats_cache_add(a1, 20LL, 2LL, 4LL);
stats_cache_update(a1, 5LL, 0LL, -1LL, &v15);
for ( i = 0LL; i < v6; ++i )
stats_cache_update(a1, 20LL, (unsigned int)i, -1LL, (char *)ptr + 4 * i);
goto LABEL_22;
}
}
v14 = 1;
*((_DWORD *)ptr + v8) = v13;
v16[0] = v8;
*((_BYTE *)v11 + v8) = 1;
if ( !(unsigned __int8)bfs(a1, v16, 1LL, 0LL, &v11, 0LL, bfs_cb, 0LL) )
{
if ( v14 >= a2 )
{
++v13;
if ( a3 )
array_append(a3, &v14);
}
goto LABEL_7;
}
v7 = v11;
if ( !v11 )
goto LABEL_15;
LABEL_14:
free(v7);
LABEL_15:
result = 0LL;
if ( ptr )
{
free(ptr);
result = 0LL;
}
return result;
}

original_code:
uint32_t stats_num_components(
  graph_t *g, uint32_t sz, array_t *sizes, uint32_t *cmpnums) {
  uint64_t i;
  uint32_t root;
  uint32_t numnodes;
  uint32_t cmpnum;
  ctx_t ctx;
  ctx.visited = ((void *)0);
  ctx.component = ((void *)0);
  ctx.cmpnum = 0;
  ctx.size = 0;
  numnodes = graph_num_nodes(g);
  if (sizes != ((void *)0)) array_clear(sizes);
  ctx.visited = calloc(numnodes, sizeof(uint8_t));
  if (ctx.visited == ((void *)0)) goto fail;
  ctx.component = calloc(numnodes, sizeof(uint32_t));
  if (ctx.component == ((void *)0)) goto fail;
  for (i = 0; i < numnodes; i++) {
    if (ctx.visited[i]) continue;
    ctx.size = 1;
    ctx.component[i] = ctx.cmpnum;
    root = i;
    ctx.visited[i] = 1;
    if (bfs(g, &root, 1, ((void *)0), &ctx, ((void *)0), _bfs_cb, ((void *)0))) goto fail;
    if (ctx.size < sz) continue;
    ctx.cmpnum++;
    if (sizes != ((void *)0)) array_append(sizes, &(ctx.size));
  }
  if (cmpnums != ((void *)0))
    memcpy(cmpnums, ctx.component, numnodes*sizeof(uint32_t));
  cmpnum = ctx.cmpnum;
  stats_cache_add(g,
                  STATS_CACHE_NUM_COMPONENTS,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(uint32_t));
  stats_cache_add(g,
                  STATS_CACHE_NODE_COMPONENT,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(uint32_t));
  stats_cache_update(g, STATS_CACHE_NUM_COMPONENTS, 0, -1, &cmpnum);
  for (i = 0; i < numnodes; i++){
    stats_cache_update(g, STATS_CACHE_NODE_COMPONENT, i, -1, ctx.component+i);
  }
  free(ctx.visited);
  free(ctx.component);
  return ctx.cmpnum;
fail:
  if (ctx.visited != ((void *)0)) free(ctx.visited);
  if (ctx.component != ((void *)0)) free(ctx.component);
  return 0;
}

canonical_original_code:
unsigned int stats_num_components(struct _graph *g, unsigned int sz, struct _array *sizes, unsigned int *cmpnums) {
  unsigned long i;
  unsigned int root;
  unsigned int numnodes;
  unsigned int cmpnum;
  struct _ctx ctx;
  ctx.visited = ((void *)0);
  ctx.component = ((void *)0);
  ctx.cmpnum = 0;
  ctx.size = 0;
  numnodes = graph_num_nodes(g);
  if (sizes != ((void *)0)) array_clear(sizes);
  ctx.visited = calloc(numnodes, sizeof(unsigned char));
  if (ctx.visited == ((void *)0)) goto fail;
  ctx.component = calloc(numnodes, sizeof(unsigned int));
  if (ctx.component == ((void *)0)) goto fail;
  for (i = 0; i < numnodes; i++) {
    if (ctx.visited[i]) continue;
    ctx.size = 1;
    ctx.component[i] = ctx.cmpnum;
    root = i;
    ctx.visited[i] = 1;
    if (bfs(g, &root, 1, ((void *)0), &ctx, ((void *)0), _bfs_cb, ((void *)0))) goto fail;
    if (ctx.size < sz) continue;
    ctx.cmpnum++;
    if (sizes != ((void *)0)) array_append(sizes, &(ctx.size));
  }
  if (cmpnums != ((void *)0))
    memcpy(cmpnums, ctx.component, numnodes*sizeof(unsigned int));
  cmpnum = ctx.cmpnum;
  stats_cache_add(g,
                  STATS_CACHE_NUM_COMPONENTS,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(unsigned int));
  stats_cache_add(g,
                  STATS_CACHE_NODE_COMPONENT,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(unsigned int));
  stats_cache_update(g, STATS_CACHE_NUM_COMPONENTS, 0, -1, &cmpnum);
  for (i = 0; i < numnodes; i++){
    stats_cache_update(g, STATS_CACHE_NODE_COMPONENT, i, -1, ctx.component+i);
  }
  free(ctx.visited);
  free(ctx.component);
  return ctx.cmpnum;
fail:
  if (ctx.visited != ((void *)0)) free(ctx.visited);
  if (ctx.component != ((void *)0)) free(ctx.component);
  return 0;
}

variable_types:
sz
unsigned int

variable_types:
sizes
struct _array *

variable_types:
cmpnums
unsigned int *

variable_types:
root
unsigned int

variable_types:
numnodes
unsigned int

variable_types:
cmpnum
unsigned int

variable_types: struct _ctx { unsigned char *visited; unsigned int *component; unsigned int cmpnum; unsigned int size; }


decompiled_code:
double __fastcall stats_component_span(__int64 a1, unsigned int a2)
{
unsigned __int64 v3; // r12
__int64 v4; // rbp
unsigned __int64 v5; // rbx
double v6; // [rsp+8h] [rbp-60h]
char v7[4]; // [rsp+10h] [rbp-58h] BYREF
unsigned int v8; // [rsp+14h] [rbp-54h]
__int64 v9; // [rsp+20h] [rbp-48h]
unsigned int v10; // [rsp+38h] [rbp-30h] BYREF
unsigned int v11[11]; // [rsp+3Ch] [rbp-2Ch] BYREF

v9 = 0LL;
if ( (unsigned __int8)array_create(v7, 4LL, 100LL) || (unsigned __int8)graph_get_component(a1, a2, v7) )
{
v6 = *(double *)&LC0;
if ( v9 )
{
array_free(v7);
v6 = *(double *)&LC0;
}
}
else
{
v3 = v8;
if ( v8 )
{
v4 = 0LL;
v6 = *(double *)&LC0;
while ( 1 )
{
if ( v4 + 1 >= v3 )
{
if ( v4 + 1 == v3 )
goto LABEL_13;
}
else
{
v5 = v4 + 1;
do
{
array_get(v7, (unsigned int)v4, v11);
array_get(v7, (unsigned int)v5, &v10);
v6 = fmax(stats_edge_distance(a1, v11[0], v10), v6);
++v5;
}
while ( v5 < v3 );
}
++v4;
}
}
v6 = *(double *)&LC0;
LABEL_13:
array_free(v7);
}
return v6;
}

canonical_decompiled_code:
double __fastcall func2(__int64 a1, unsigned int a2)
{
unsigned __int64 v3; // r12
__int64 v4; // rbp
unsigned __int64 v5; // rbx
double v6; // [rsp+8h] [rbp-60h]
char v7[4]; // [rsp+10h] [rbp-58h] BYREF
unsigned int v8; // [rsp+14h] [rbp-54h]
__int64 v9; // [rsp+20h] [rbp-48h]
unsigned int v10; // [rsp+38h] [rbp-30h] BYREF
unsigned int v11[11]; // [rsp+3Ch] [rbp-2Ch] BYREF

v9 = 0LL;
if ( (unsigned __int8)array_create(v7, 4LL, 100LL) || (unsigned __int8)graph_get_component(a1, a2, v7) )
{
v6 = *(double *)&LC0;
if ( v9 )
{
array_free(v7);
v6 = *(double *)&LC0;
}
}
else
{
v3 = v8;
if ( v8 )
{
v4 = 0LL;
v6 = *(double *)&LC0;
while ( 1 )
{
if ( v4 + 1 >= v3 )
{
if ( v4 + 1 == v3 )
goto LABEL_13;
}
else
{
v5 = v4 + 1;
do
{
array_get(v7, (unsigned int)v4, v11);
array_get(v7, (unsigned int)v5, &v10);
v6 = fmax(stats_edge_distance(a1, v11[0], v10), v6);
++v5;
}
while ( v5 < v3 );
}
++v4;
}
}
v6 = *(double *)&LC0;
LABEL_13:
array_free(v7);
}
return v6;
}

original_code:
double stats_component_span(graph_t *g, uint32_t cmp) {
  uint64_t i;
  uint64_t j;
  uint32_t ni;
  uint32_t nj;
  uint32_t cmpsz;
  double dist;
  double maxdist;
  array_t nodes;
  nodes.data = ((void *)0);
  maxdist = -1.0;
  if (array_create(&nodes, sizeof(uint32_t), 100)) goto fail;
  if (graph_get_component(g, cmp, &nodes)) goto fail;
  cmpsz = nodes.size;
  for (i = 0; i < cmpsz; i++) {
    for (j = i+1; j < cmpsz; j++) {
      array_get(&nodes, i, &ni);
      array_get(&nodes, j, &nj);
      dist = stats_edge_distance(g, ni, nj);
      if (dist > maxdist) maxdist = dist;
    }
  }
  array_free(&nodes);
  return maxdist;
fail:
  if (nodes.data != ((void *)0)) array_free(&nodes);
  return -1.0;
}

canonical_original_code:
double stats_component_span(struct _graph *g, unsigned int cmp) {
  unsigned long i;
  unsigned long j;
  unsigned int ni;
  unsigned int nj;
  unsigned int cmpsz;
  double dist;
  double maxdist;
  struct _array nodes;
  nodes.data = ((void *)0);
  maxdist = -1.0;
  if (array_create(&nodes, sizeof(unsigned int), 100)) goto fail;
  if (graph_get_component(g, cmp, &nodes)) goto fail;
  cmpsz = nodes.size;
  for (i = 0; i < cmpsz; i++) {
    for (j = i+1; j < cmpsz; j++) {
      array_get(&nodes, i, &ni);
      array_get(&nodes, j, &nj);
      dist = stats_edge_distance(g, ni, nj);
      if (dist > maxdist) maxdist = dist;
    }
  }
  array_free(&nodes);
  return maxdist;
fail:
  if (nodes.data != ((void *)0)) array_free(&nodes);
  return -1.0;
}

variable_types:
cmp
unsigned int

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
ni
unsigned int

variable_types:
nj
unsigned int

variable_types:
cmpsz
unsigned int

variable_types: dist double

variable_types: maxdist double

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

return_type: double


decompiled_code:
__int64 __fastcall stats_largest_component(__int64 a1)
{
__int64 result; // rax
__int64 v2[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v3; // [rsp+10h] [rbp-28h]
__int64 v4; // [rsp+18h] [rbp-20h]
__int64 v5; // [rsp+20h] [rbp-18h]
int v6[3]; // [rsp+2Ch] [rbp-Ch] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
if ( (unsigned __int8)array_create(v2, 4LL, 10LL)
|| (array_set_cmps(v2, &compare_u32, &compare_u32_insert),
stats_num_components(a1, 0, (__int64)v2, 0LL),
array_sort(v2),
(unsigned __int8)array_get(v2, (unsigned int)(HIDWORD(v2[0]) - 1), v6)) )
{
result = 0LL;
if ( v3 )
{
array_free(v2);
result = 0LL;
}
}
else
{
stats_cache_add(a1, 6LL, 0LL, 4LL);
stats_cache_update(a1, 6LL, 0LL, -1LL, v6);
array_free(v2);
result = (unsigned int)v6[0];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
__int64 v2[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v3; // [rsp+10h] [rbp-28h]
__int64 v4; // [rsp+18h] [rbp-20h]
__int64 v5; // [rsp+20h] [rbp-18h]
int v6[3]; // [rsp+2Ch] [rbp-Ch] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
v3 = 0LL;
v4 = 0LL;
v5 = 0LL;
if ( (unsigned __int8)array_create(v2, 4LL, 10LL)
|| (array_set_cmps(v2, &compare_u32, &compare_u32_insert),
func1(a1, 0, (__int64)v2, 0LL),
array_sort(v2),
(unsigned __int8)array_get(v2, (unsigned int)(HIDWORD(v2[0]) - 1), v6)) )
{
result = 0LL;
if ( v3 )
{
array_free(v2);
result = 0LL;
}
}
else
{
stats_cache_add(a1, 6LL, 0LL, 4LL);
stats_cache_update(a1, 6LL, 0LL, -1LL, v6);
array_free(v2);
result = (unsigned int)v6[0];
}
return result;
}

original_code:
uint32_t stats_largest_component(graph_t *g) {
  uint32_t lcmp;
  array_t sizes;
  memset(&sizes, 0, sizeof(sizes));
  if (array_create(&sizes, sizeof(uint32_t), 10)) goto fail;
  array_set_cmps(&sizes, &compare_u32, &compare_u32_insert);
  stats_num_components(g, 0, &sizes, ((void *)0));
  array_sort(&sizes);
  if (array_get(&sizes, sizes.size - 1, &lcmp)) goto fail;
  stats_cache_add(g,
                  STATS_CACHE_LARGEST_COMPONENT,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(uint32_t));
  stats_cache_update(g, STATS_CACHE_LARGEST_COMPONENT, 0, -1, &lcmp);
  array_free(&sizes);
  return lcmp;
fail:
  if (sizes.data != ((void *)0)) array_free(&sizes);
  return 0;
}

canonical_original_code:
unsigned int stats_largest_component(struct _graph *g) {
  unsigned int lcmp;
  struct _array sizes;
  memset(&sizes, 0, sizeof(sizes));
  if (array_create(&sizes, sizeof(unsigned int), 10)) goto fail;
  array_set_cmps(&sizes, &compare_u32, &compare_u32_insert);
  stats_num_components(g, 0, &sizes, ((void *)0));
  array_sort(&sizes);
  if (array_get(&sizes, sizes.size - 1, &lcmp)) goto fail;
  stats_cache_add(g,
                  STATS_CACHE_LARGEST_COMPONENT,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(unsigned int));
  stats_cache_update(g, STATS_CACHE_LARGEST_COMPONENT, 0, -1, &lcmp);
  array_free(&sizes);
  return lcmp;
fail:
  if (sizes.data != ((void *)0)) array_free(&sizes);
  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:
lcmp
unsigned int


decompiled_code:
__int64 __fastcall analyze_open_volume(char *a1, __int64 a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // ebp
unsigned int v5; // ebp
__int64 v6; // rbx
void *v8; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
v2 = list_files(a1, (void ***)&ptr);
v3 = v2;
if ( v2 <= 0 )
{
v4 = 1;
if ( ptr )
{
v5 = v2;
if ( !v2 )
{
LABEL_10:
free(ptr);
return 1;
}
LABEL_5:
v6 = 0LL;
do
{
v8 = (void *)*((_QWORD *)ptr + v6);
if ( v8 )
free(v8);
++v6;
}
while ( v5 > (unsigned int)v6 );
goto LABEL_10;
}
}
else if ( v2 == 1 )
{
v4 = 4d_to_volume(*(const char **)ptr, (unsigned __int16 *)a2);
if ( (_BYTE)v4 )
{
LABEL_4:
v5 = v3;
if ( !ptr )
return 1;
goto LABEL_5;
}
free(*(void **)ptr);
free(ptr);
}
else
{
v4 = 3d_to_volume((__int64)ptr, v2, (unsigned __int16 *)a2);
if ( (_BYTE)v4 )
goto LABEL_4;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, __int64 a2)
{
int v2; // eax
int v3; // ebx
unsigned int v4; // ebp
unsigned int v5; // ebp
__int64 v6; // rbx
void *v8; // rdi
void *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = 0LL;
*(_QWORD *)a2 = 0LL;
*(_QWORD *)(a2 + 8) = 0LL;
*(_QWORD *)(a2 + 16) = 0LL;
*(_QWORD *)(a2 + 24) = 0LL;
v2 = func0(a1, (void ***)&ptr);
v3 = v2;
if ( v2 <= 0 )
{
v4 = 1;
if ( ptr )
{
v5 = v2;
if ( !v2 )
{
LABEL_10:
free(ptr);
return 1;
}
LABEL_5:
v6 = 0LL;
do
{
v8 = (void *)*((_QWORD *)ptr + v6);
if ( v8 )
free(v8);
++v6;
}
while ( v5 > (unsigned int)v6 );
goto LABEL_10;
}
}
else if ( v2 == 1 )
{
v4 = 4dfunc3(*(const char **)ptr, (unsigned __int16 *)a2);
if ( (_BYTE)v4 )
{
LABEL_4:
v5 = v3;
if ( !ptr )
return 1;
goto LABEL_5;
}
free(*(void **)ptr);
free(ptr);
}
else
{
v4 = 3dfunc3((__int64)ptr, v2, (unsigned __int16 *)a2);
if ( (_BYTE)v4 )
goto LABEL_4;
}
return v4;
}

original_code:
uint8_t analyze_open_volume(char *path, analyze_volume_t *vol) {
  uint32_t i;
  char **files;
  int32_t nfiles;
  nfiles = 0;
  files = ((void *)0);
  memset(vol, 0, sizeof(analyze_volume_t));
  nfiles = _list_files(path, &files);
  if (nfiles <= 0) goto fail;
  if (nfiles == 1) {
    if (_4d_to_volume(files[0], vol)) goto fail;
    free(files[0]);
    free(files);
  }
  else {
    if (_3d_to_volume(files, nfiles, vol)) goto fail;
  }
  return 0;
fail:
  if (files != ((void *)0)) {
    for (i = 0; i < nfiles; i++) {
      if (files[i] != ((void *)0))
        free(files[i]);
    }
    free(files);
  }
  return 1;
}

canonical_original_code:
unsigned char analyze_open_volume(char *path, struct __analyze_volume *vol) {
  unsigned int i;
  char **files;
  int nfiles;
  nfiles = 0;
  files = ((void *)0);
  memset(vol, 0, sizeof(analyze_volume_t));
  nfiles = _list_files(path, &files);
  if (nfiles <= 0) goto fail;
  if (nfiles == 1) {
    if (_4d_to_volume(files[0], vol)) goto fail;
    free(files[0]);
    free(files);
  }
  else {
    if (_3d_to_volume(files, nfiles, vol)) goto fail;
  }
  return 0;
fail:
  if (files != ((void *)0)) {
    for (i = 0; i < nfiles; i++) {
      if (files[i] != ((void *)0))
        free(files[i]);
    }
    free(files);
  }
  return 1;
}

variable_types:
path
char *

variable_types: struct __analyze_volume { unsigned short nimgs; char **files; struct dsr { struct header_key { unsigned int sizeof_hdr; char data_type[10]; char db_name[18]; unsigned int extents; unsigned short session_error; char regular; char hkey_un0; } hk; struct image_dimension { unsigned short dim[8]; unsigned short unused8; unsigned short unused9; unsigned short unused10; unsigned short unused11; unsigned short unused12; unsigned short unused13; unsigned short unused14; unsigned short datatype; unsigned short bitpix; unsigned short dim_un0; float pixdim[8]; float vox_offset; float funused1; float funused2; float funused3; float cal_max; float cal_min; float compressed; float verified; unsigned int glmax; unsigned int glmin; } dime; struct data_history { char descrip[80]; char aux_file[24]; char orient; char originator[10]; char generated[10]; char scannum[10]; char patient_id[10]; char exp_date[10]; char exp_time[10]; char hist_un0[3]; unsigned int views; unsigned int vols_added; unsigned int start_field; unsigned int field_skip; unsigned int omax; unsigned int omin; unsigned int smax; unsigned int smin; } hist; unsigned char rev; } *hdrs; unsigned char **imgs; } *

variable_types:
i
unsigned int

variable_types:
files
char **

variable_types: nfiles int

user_defined_types: struct __analyze_volume { unsigned short nimgs; char **files; struct dsr *hdrs; unsigned char **imgs; }

user_defined_types: struct dsr { struct header_key hk; struct image_dimension dime; struct data_history hist; unsigned char rev; }


original_code:
void analyze_free_volume(analyze_volume_t *vol) {
  uint32_t i;
  if (vol == ((void *)0)) return;
  if (vol->hdrs == ((void *)0)) return;
  if (vol->imgs == ((void *)0)) return;
  if (vol->files == ((void *)0)) return;
  if (vol->nimgs == 0) return;
  for (i = 0; i < vol->nimgs; i++) {
    free(vol->imgs[i]);
    free(vol->files[i]);
  }
  free(vol->hdrs);
  free(vol->imgs);
  free(vol->files);
}

canonical_original_code:
void analyze_free_volume(struct __analyze_volume *vol) {
  unsigned int i;
  if (vol == ((void *)0)) return;
  if (vol->hdrs == ((void *)0)) return;
  if (vol->imgs == ((void *)0)) return;
  if (vol->files == ((void *)0)) return;
  if (vol->nimgs == 0) return;
  for (i = 0; i < vol->nimgs; i++) {
    free(vol->imgs[i]);
    free(vol->files[i]);
  }
  free(vol->hdrs);
  free(vol->imgs);
  free(vol->files);
}

user_defined_types: struct header_key { unsigned int sizeof_hdr; char data_type[10]; char db_name[18]; unsigned int extents; unsigned short session_error; char regular; char hkey_un0; }

user_defined_types: struct image_dimension { unsigned short dim[8]; unsigned short unused8; unsigned short unused9; unsigned short unused10; unsigned short unused11; unsigned short unused12; unsigned short unused13; unsigned short unused14; unsigned short datatype; unsigned short bitpix; unsigned short dim_un0; float pixdim[8]; float vox_offset; float funused1; float funused2; float funused3; float cal_max; float cal_min; float compressed; float verified; unsigned int glmax; unsigned int glmin; }

user_defined_types: struct data_history { char descrip[80]; char aux_file[24]; char orient; char originator[10]; char generated[10]; char scannum[10]; char patient_id[10]; char exp_date[10]; char exp_time[10]; char hist_un0[3]; unsigned int views; unsigned int vols_added; unsigned int start_field; unsigned int field_skip; unsigned int omax; unsigned int omin; unsigned int smax; unsigned int smin; }


original_code:
uint8_t analyze_read_timeseries(
  analyze_volume_t *vol,
  uint32_t x,
  uint32_t y,
  uint32_t z,
  double *timeseries) {
  uint64_t i;
  uint32_t idx[4];
  idx[0] = x;
  idx[1] = y;
  idx[2] = z;
  idx[3] = 0;
  for (i = 0; i < vol->nimgs; i++) {
    timeseries[i] = analyze_read_val(&(vol->hdrs[i]), vol->imgs[i], idx);
  }
  return 0;
}

canonical_original_code:
unsigned char analyze_read_timeseries(struct __analyze_volume *vol, unsigned int x, unsigned int y, unsigned int z, double *timeseries) {
  unsigned long i;
  unsigned int idx[4];
  idx[0] = x;
  idx[1] = y;
  idx[2] = z;
  idx[3] = 0;
  for (i = 0; i < vol->nimgs; i++) {
    timeseries[i] = analyze_read_val(&(vol->hdrs[i]), vol->imgs[i], idx);
  }
  return 0;
}

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types:
z
unsigned int

variable_types:
timeseries
double *

variable_types:
idx
unsigned int [4]


decompiled_code:
__int64 __fastcall analyze_read_timeseries_by_idx(__int64 a1, __int64 a2, __int64 a3)
{
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

analyze_get_indices(*(_QWORD *)(a1 + 16), a2, v5);
return analyze_read_timeseries((unsigned __int16 *)a1, v5[0], v5[1], v5[2], a3);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, __int64 a3)
{
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

analyze_get_indices(*(_QWORD *)(a1 + 16), a2, v5);
return func6((unsigned __int16 *)a1, v5[0], v5[1], v5[2], a3);
}

original_code:
uint8_t analyze_read_timeseries_by_idx(
  analyze_volume_t *vol, uint32_t idx, double *timeseries) {
  uint32_t dims[5];
  analyze_get_indices(vol->hdrs, idx, dims);
  return analyze_read_timeseries(vol, dims[0], dims[1], dims[2], timeseries);
}

canonical_original_code:
unsigned char analyze_read_timeseries_by_idx(struct __analyze_volume *vol, unsigned int idx, double *timeseries) {
  unsigned int dims[5];
  analyze_get_indices(vol->hdrs, idx, dims);
  return analyze_read_timeseries(vol, dims[0], dims[1], dims[2], timeseries);
}

variable_types:
idx
unsigned int

variable_types:
dims
unsigned int [5]


decompiled_code:
double __fastcall stats_num_intra_edges(__int64 a1, double *a2)
{
unsigned int v2; // eax
unsigned __int64 v3; // rbp
double result; // xmm0_8
__int64 i; // rbx
int v6; // er12
__int64 v7; // r13
_DWORD *v8; // r15
__int64 v9; // [rsp+0h] [rbp-58h]
double v10; // [rsp+10h] [rbp-48h] BYREF
double v11[8]; // [rsp+18h] [rbp-40h] BYREF

if ( (unsigned int)graph_num_labelvals() <= 1 )
{
if ( a2 )
*a2 = 0.0;
result = (double)(int)graph_num_edges(a1);
}
else
{
v11[0] = 0.0;
v10 = 0.0;
v2 = graph_num_nodes(a1);
v9 = v2;
if ( v2 )
{
v3 = 0LL;
do
{
v6 = graph_num_neighbours(a1, (unsigned int)v3);
v7 = graph_get_neighbours(a1, (unsigned int)v3);
v8 = (_DWORD *)graph_get_nodelabel(a1, (unsigned int)v3);
if ( v6 )
{
for ( i = 0LL; i != v6; ++i )
{
if ( *(unsigned int *)(v7 + 4 * i) >= v3 )
{
if ( *v8 == *(_DWORD *)graph_get_nodelabel(a1, *(unsigned int *)(v7 + 4 * i)) )
v11[0] = v11[0] + 1.0;
else
v10 = v10 + 1.0;
}
}
}
++v3;
}
while ( v3 != v9 );
}
stats_cache_add(a1, 11LL, 0LL, 8LL);
stats_cache_add(a1, 12LL, 0LL, 8LL);
stats_cache_update(a1, 11LL, 0LL, -1LL, v11);
stats_cache_update(a1, 12LL, 0LL, -1LL, &v10);
if ( a2 )
*a2 = v10;
result = v11[0];
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1, double *a2)
{
unsigned int v2; // eax
unsigned __int64 v3; // rbp
double result; // xmm0_8
__int64 i; // rbx
int v6; // er12
__int64 v7; // r13
_DWORD *v8; // r15
__int64 v9; // [rsp+0h] [rbp-58h]
double v10; // [rsp+10h] [rbp-48h] BYREF
double v11[8]; // [rsp+18h] [rbp-40h] BYREF

if ( (unsigned int)graph_num_labelvals() <= 1 )
{
if ( a2 )
*a2 = 0.0;
result = (double)(int)graph_num_edges(a1);
}
else
{
v11[0] = 0.0;
v10 = 0.0;
v2 = graph_num_nodes(a1);
v9 = v2;
if ( v2 )
{
v3 = 0LL;
do
{
v6 = graph_num_neighbours(a1, (unsigned int)v3);
v7 = graph_get_neighbours(a1, (unsigned int)v3);
v8 = (_DWORD *)graph_get_nodelabel(a1, (unsigned int)v3);
if ( v6 )
{
for ( i = 0LL; i != v6; ++i )
{
if ( *(unsigned int *)(v7 + 4 * i) >= v3 )
{
if ( *v8 == *(_DWORD *)graph_get_nodelabel(a1, *(unsigned int *)(v7 + 4 * i)) )
v11[0] = v11[0] + 1.0;
else
v10 = v10 + 1.0;
}
}
}
++v3;
}
while ( v3 != v9 );
}
stats_cache_add(a1, 11LL, 0LL, 8LL);
stats_cache_add(a1, 12LL, 0LL, 8LL);
stats_cache_update(a1, 11LL, 0LL, -1LL, v11);
stats_cache_update(a1, 12LL, 0LL, -1LL, &v10);
if ( a2 )
*a2 = v10;
result = v11[0];
}
return result;
}

original_code:
double stats_num_intra_edges(graph_t *g, double *inter_out) {
  uint64_t i;
  uint64_t j;
  uint32_t nnodes;
  uint32_t nnbrs;
  uint32_t *nbrs;
  graph_label_t *ilbl;
  graph_label_t *jlbl;
  double intra;
  double inter;
  if (graph_num_labelvals(g) <= 1) {
    if (inter_out != ((void *)0)) *inter_out = 0;
    return graph_num_edges(g);
  }
  intra = 0;
  inter = 0;
  nnodes = graph_num_nodes(g);
  for (i = 0; i < nnodes; i++) {
    nnbrs = graph_num_neighbours(g, i);
    nbrs = graph_get_neighbours(g, i);
    ilbl = graph_get_nodelabel( g, i);
    for (j = 0; j < nnbrs; j++) {
      if (i > nbrs[j]) continue;
      jlbl = graph_get_nodelabel(g, nbrs[j]);
      if (ilbl->labelval == jlbl->labelval) intra ++;
      else inter ++;
    }
  }
  stats_cache_add(g,
                  STATS_CACHE_INTRA_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_INTER_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_INTRA_EDGES, 0, -1, &intra);
  stats_cache_update(g, STATS_CACHE_INTER_EDGES, 0, -1, &inter);
  if (inter_out != ((void *)0)) *inter_out = inter;
  return intra;
}

canonical_original_code:
double stats_num_intra_edges(struct _graph *g, double *inter_out) {
  unsigned long i;
  unsigned long j;
  unsigned int nnodes;
  unsigned int nnbrs;
  unsigned int *nbrs;
  struct _graph_label *ilbl;
  struct _graph_label *jlbl;
  double intra;
  double inter;
  if (graph_num_labelvals(g) <= 1) {
    if (inter_out != ((void *)0)) *inter_out = 0;
    return graph_num_edges(g);
  }
  intra = 0;
  inter = 0;
  nnodes = graph_num_nodes(g);
  for (i = 0; i < nnodes; i++) {
    nnbrs = graph_num_neighbours(g, i);
    nbrs = graph_get_neighbours(g, i);
    ilbl = graph_get_nodelabel( g, i);
    for (j = 0; j < nnbrs; j++) {
      if (i > nbrs[j]) continue;
      jlbl = graph_get_nodelabel(g, nbrs[j]);
      if (ilbl->labelval == jlbl->labelval) intra ++;
      else inter ++;
    }
  }
  stats_cache_add(g,
                  STATS_CACHE_INTRA_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_INTER_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_INTRA_EDGES, 0, -1, &intra);
  stats_cache_update(g, STATS_CACHE_INTER_EDGES, 0, -1, &inter);
  if (inter_out != ((void *)0)) *inter_out = inter;
  return intra;
}

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
nnodes
unsigned int

variable_types:
nnbrs
unsigned int

variable_types:
nbrs
unsigned int *

variable_types: struct _graph_label { unsigned int labelval; float xval; float yval; float zval; unsigned int meta[16]; } *

variable_types: intra double

variable_types: inter double


decompiled_code:
double __fastcall stats_edge_distance(__int64 a1, __int64 a2, unsigned int a3)
{
float *v4; // rbx
float *v5; // rax
double v6; // xmm1_8

v4 = (float *)graph_get_nodelabel(a1, a2);
v5 = (float *)graph_get_nodelabel(a1, a3);
if ( !v5 || !v4 )
return *(double *)&LC2;
v6 = (float)(v4[3] - v5[3]);
return pow(
(float)(v4[1] - v5[1]) * (float)(v4[1] - v5[1]) + (float)(v4[2] - v5[2]) * (float)(v4[2] - v5[2]) + v6 * v6,
0.5);
}

canonical_decompiled_code:
double __fastcall func1(__int64 a1, __int64 a2, unsigned int a3)
{
float *v4; // rbx
float *v5; // rax
double v6; // xmm1_8

v4 = (float *)graph_get_nodelabel(a1, a2);
v5 = (float *)graph_get_nodelabel(a1, a3);
if ( !v5 || !v4 )
return *(double *)&LC2;
v6 = (float)(v4[3] - v5[3]);
return pow(
(float)(v4[1] - v5[1]) * (float)(v4[1] - v5[1]) + (float)(v4[2] - v5[2]) * (float)(v4[2] - v5[2]) + v6 * v6,
0.5);
}

original_code:
double stats_edge_distance(graph_t *g, uint32_t u, uint32_t v) {
  double dist;
  double dx;
  double dy;
  double dz;
  graph_label_t *lu;
  graph_label_t *lv;
  lu = graph_get_nodelabel(g, u);
  lv = graph_get_nodelabel(g, v);
  if (lu == ((void *)0)) return -1.0;
  if (lv == ((void *)0)) return -1.0;
  dx = lu->xval - lv->xval;
  dy = lu->yval - lv->yval;
  dz = lu->zval - lv->zval;
  dist = pow((dx*dx) + (dy*dy) + (dz*dz), 0.5);
  return dist;
}

canonical_original_code:
double stats_edge_distance(struct _graph *g, unsigned int u, unsigned int v) {
  double dist;
  double dx;
  double dy;
  double dz;
  struct _graph_label *lu;
  struct _graph_label *lv;
  lu = graph_get_nodelabel(g, u);
  lv = graph_get_nodelabel(g, v);
  if (lu == ((void *)0)) return -1.0;
  if (lv == ((void *)0)) return -1.0;
  dx = lu->xval - lv->xval;
  dy = lu->yval - lv->yval;
  dz = lu->zval - lv->zval;
  dist = pow((dx*dx) + (dy*dy) + (dz*dz), 0.5);
  return dist;
}

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:
u
unsigned int

variable_types:
v
unsigned int

variable_types: dist double

variable_types: dx double

variable_types: dy double

variable_types: dz double

user_defined_types: struct _graph_label { unsigned int labelval; float xval; float yval; float zval; unsigned int meta[16]; }


decompiled_code:
double __fastcall stats_avg_edge_distance(__int64 a1, __int64 a2)
{
int v2; // er15
__int64 v3; // rax
__int64 v4; // r13
__int64 i; // rbx
double v7[8]; // [rsp+8h] [rbp-40h] BYREF

v7[0] = 0.0;
v2 = graph_num_neighbours(a1, a2);
v3 = graph_get_neighbours(a1, (unsigned int)a2);
if ( v2 )
{
v4 = v3;
for ( i = 0LL; i != v2; v7[0] = stats_edge_distance(a1, (unsigned int)a2, *(_DWORD *)(v4 + 4 * i++)) + v7[0] )
;
}
v7[0] = v7[0] / (double)v2;
stats_cache_add(a1, 21LL, 2LL, 8LL);
stats_cache_update(a1, 21LL, (unsigned int)a2, -1LL, v7);
return v7[0];
}

canonical_decompiled_code:
double __fastcall func2(__int64 a1, __int64 a2)
{
int v2; // er15
__int64 v3; // rax
__int64 v4; // r13
__int64 i; // rbx
double v7[8]; // [rsp+8h] [rbp-40h] BYREF

v7[0] = 0.0;
v2 = graph_num_neighbours(a1, a2);
v3 = graph_get_neighbours(a1, (unsigned int)a2);
if ( v2 )
{
v4 = v3;
for ( i = 0LL; i != v2; v7[0] = func1(a1, (unsigned int)a2, *(_DWORD *)(v4 + 4 * i++)) + v7[0] )
;
}
v7[0] = v7[0] / (double)v2;
stats_cache_add(a1, 21LL, 2LL, 8LL);
stats_cache_update(a1, 21LL, (unsigned int)a2, -1LL, v7);
return v7[0];
}

original_code:
double stats_avg_edge_distance(graph_t *g, uint32_t u) {
  uint64_t i;
  uint32_t nnbrs;
  uint32_t *nbrs;
  double avg;
  avg = 0;
  nnbrs = graph_num_neighbours(g, u);
  nbrs = graph_get_neighbours(g, u);
  for (i = 0; i < nnbrs; i++) {
    avg += stats_edge_distance(g, u, nbrs[i]);
  }
  avg /= nnbrs;
  stats_cache_add(g,
                  STATS_CACHE_NODE_EDGEDIST,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_NODE_EDGEDIST, u, -1, &avg);
  return avg;
}

canonical_original_code:
double stats_avg_edge_distance(struct _graph *g, unsigned int u) {
  unsigned long i;
  unsigned int nnbrs;
  unsigned int *nbrs;
  double avg;
  avg = 0;
  nnbrs = graph_num_neighbours(g, u);
  nbrs = graph_get_neighbours(g, u);
  for (i = 0; i < nnbrs; i++) {
    avg += stats_edge_distance(g, u, nbrs[i]);
  }
  avg /= nnbrs;
  stats_cache_add(g,
                  STATS_CACHE_NODE_EDGEDIST,
                  STATS_CACHE_TYPE_NODE,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_NODE_EDGEDIST, u, -1, &avg);
  return avg;
}

variable_types: avg double


decompiled_code:
bool __fastcall graph_add_event_listener(__int64 a1, __int64 *a2)
{
__int64 v2; // rax

v2 = id_counter++;
*a2 = v2;
return (unsigned __int8)array_append(a1 + 152) != 0;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1, __int64 *a2)
{
__int64 v2; // rax

v2 = id_counter++;
*a2 = v2;
return (unsigned __int8)array_append(a1 + 152) != 0;
}

original_code:
uint8_t graph_add_event_listener(graph_t *g, graph_event_listener_t *l) {
  l->id = _id_counter ++;
  if (array_append(&g->event_listeners, l)) return 1;
  return 0;
}

canonical_original_code:
unsigned char graph_add_event_listener(struct _graph *g, struct _graph_event_listener *l) {
  l->id = _id_counter ++;
  if (array_append(&g->event_listeners, l)) return 1;
  return 0;
}

variable_types: struct _graph_event_listener { unsigned long id; void *ctx; void (*edge_added)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx, float wt); void (*edge_removed)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx); } *


decompiled_code:
__int64 __fastcall graph_remove_event_listener(__int64 a1, __int64 a2)
{
return array_remove_by_val(a1 + 152, a2, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
return array_remove_by_val(a1 + 152, a2, 0LL);
}

original_code:
void graph_remove_event_listener(graph_t *g, graph_event_listener_t *l) {
  array_remove_by_val(&g->event_listeners, l, 0);
}

canonical_original_code:
void graph_remove_event_listener(struct _graph *g, struct _graph_event_listener *l) {
  array_remove_by_val(&g->event_listeners, l, 0);
}

user_defined_types: struct _graph_event_listener { unsigned long id; void *ctx; void (*edge_added)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx, float wt); void (*edge_removed)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx); }


decompiled_code:
void __fastcall graph_event_fire(__int64 a1, int a2, unsigned int *a3)
{
if ( a2 )
{
if ( a2 == 1 )
fire_edge_removed(a1, a3);
}
else
{
fire_edge_added(a1, (float *)a3);
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, int a2, unsigned int *a3)
{
if ( a2 )
{
if ( a2 == 1 )
func1(a1, a3);
}
else
{
func0(a1, (float *)a3);
}
}

original_code:
void graph_event_fire(graph_t *g, graph_event_t type, void *ctx) {
  switch(type) {
    case GRAPH_EVENT_EDGE_ADDED: _fire_edge_added( g, ctx); break;
    case GRAPH_EVENT_EDGE_REMOVED: _fire_edge_removed(g, ctx); break;
  }
}

canonical_original_code:
void graph_event_fire(struct _graph *g, enum graph_event_t type, void *ctx) {
  switch(type) {
    case GRAPH_EVENT_EDGE_ADDED: _fire_edge_added( g, ctx); break;
    case GRAPH_EVENT_EDGE_REMOVED: _fire_edge_removed(g, ctx); break;
  }
}

variable_types: enum graph_event_t { GRAPH_EVENT_EDGE_ADDED=0, GRAPH_EVENT_EDGE_REMOVED=1 }

variable_types:
ctx
void *


decompiled_code:
_BOOL8 __fastcall graph_compare_event_listeners(_QWORD *a1, _QWORD *a2)
{
return *a1 != *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(_QWORD *a1, _QWORD *a2)
{
return *a1 != *a2;
}

original_code:
int graph_compare_event_listeners(const void *a, const void *b) {
  graph_event_listener_t *gela;
  graph_event_listener_t *gelb;
  gela = (graph_event_listener_t *)a;
  gelb = (graph_event_listener_t *)b;
  if (gela->id == gelb->id) return 0;
  return 1;
}

canonical_original_code:
int graph_compare_event_listeners(void *a, void *b) {
  struct _graph_event_listener *gela;
  struct _graph_event_listener *gelb;
  gela = (struct _graph_event_listener *)a;
  gelb = (struct _graph_event_listener *)b;
  if (gela->id == gelb->id) return 0;
  return 1;
}

variable_types:
a
void *

variable_types:
b
void *

variable_types: struct _graph_event_listener { unsigned long id; void *ctx; void (*edge_added)(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 *); } *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx, float wt); void (*edge_removed)(struct _graph *g, void *ctx, unsigned int u, unsigned int v, unsigned int uidx, unsigned int vidx); } *