decompiled_code:
int __fastcall time_update(double *a1)
{
int result; // eax
double v2; // xmm1_8
double v3; // xmm0_8
struct timeval v4; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v4, 0LL);
v2 = a1[1];
*a1 = v2;
v3 = (double)SLODWORD(v4.tv_usec) / 1000000.0 + (double)SLODWORD(v4.tv_sec);
a1[1] = v3;
a1[2] = v3 - v2;
return result;
}

canonical_decompiled_code:
int __fastcall func0(double *a1)
{
int result; // eax
double v2; // xmm1_8
double v3; // xmm0_8
struct timeval v4; // [rsp+0h] [rbp-18h] BYREF

result = gettimeofday(&v4, 0LL);
v2 = a1[1];
*a1 = v2;
v3 = (double)SLODWORD(v4.tv_usec) / 1000000.0 + (double)SLODWORD(v4.tv_sec);
a1[1] = v3;
a1[2] = v3 - v2;
return result;
}

original_code:
void time_update(timespan* span)
{
  struct timeval tim;
  gettimeofday(&tim, 0);
  span->last = span->current;
  span->current = (double)tim.tv_sec + (tim.tv_usec / 1000000.0);
  span->elapsed = span->current - span->last;
}

canonical_original_code:
void time_update(struct timespan *span)
{
  struct timeval tim;
  gettimeofday(&tim, 0);
  span->last = span->current;
  span->current = (double)tim.tv_sec + (tim.tv_usec / 1000000.0);
  span->elapsed = span->current - span->last;
}

variable_types: struct timespan { double last; double current; double elapsed; } *

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


decompiled_code:
double __fastcall time_elapsed(__int64 a1)
{
return *(double *)(a1 + 16);
}

canonical_decompiled_code:
double __fastcall func1(__int64 a1)
{
return *(double *)(a1 + 16);
}

original_code:
double time_elapsed(timespan* span)
{
  return span->elapsed;
}

canonical_original_code:
double time_elapsed(struct timespan *span)
{
  return span->elapsed;
}

return_type: double

user_defined_types: struct timespan { double last; double current; double elapsed; }


decompiled_code:
void __fastcall vec2_rotate(float *a1, float a2)
{
float v2; // xmm0_4
float v3; // xmm1_4
float v4; // xmm4_4
float v5; // xmm3_4
double v6; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

sincos(a2, sinx, &v6);
v2 = sinx[0];
v3 = v6;
v4 = *a1;
v5 = a1[1];
*a1 = (float)(v3 * *a1) - (float)(v2 * v5);
a1[1] = (float)(v2 * v4) + (float)(v3 * v5);
}

canonical_decompiled_code:
void __fastcall func0(float *a1, float a2)
{
float v2; // xmm0_4
float v3; // xmm1_4
float v4; // xmm4_4
float v5; // xmm3_4
double v6; // [rsp+0h] [rbp-18h] BYREF
double sinx[2]; // [rsp+8h] [rbp-10h] BYREF

sincos(a2, sinx, &v6);
v2 = sinx[0];
v3 = v6;
v4 = *a1;
v5 = a1[1];
*a1 = (float)(v3 * *a1) - (float)(v2 * v5);
a1[1] = (float)(v2 * v4) + (float)(v3 * v5);
}

original_code:
void vec2_rotate(vec2_t* ray, vec2_type theta)
{
  float x, y, s_t, c_t;
  s_t = sin(theta);
  c_t = cos(theta);
  x = ray->x;
  y = ray->y;
  ray->x = x * c_t - y * s_t;
  ray->y = x * s_t + y * c_t;
}

canonical_original_code:
void vec2_rotate(struct vec2 *ray, float theta)
{
  float x; float y; float s_t; float c_t;
  s_t = sin(theta);
  c_t = cos(theta);
  x = ray->x;
  y = ray->y;
  ray->x = x * c_t - y * s_t;
  ray->y = x * s_t + y * c_t;
}

variable_types:
ray
struct vec2 { float x; float y; } *

variable_types: theta float

variable_types: x float

variable_types: y float

return_type: void


decompiled_code:
float __fastcall vec2_dot(float *a1, float *a2)
{
return (float)(*a1 * *a2) + (float)(a1[1] * a2[1]);
}

canonical_decompiled_code:
float __fastcall func1(float *a1, float *a2)
{
return (float)(*a1 * *a2) + (float)(a1[1] * a2[1]);
}

original_code:
vec2_type vec2_dot(const vec2_t* ray1, const vec2_t* ray2)
{
  vec2_type dot_prod;
  dot_prod = ray1->x * ray2->x + ray1->y * ray2->y;
  return dot_prod;
}

canonical_original_code:
float vec2_dot(struct vec2 *ray1, struct vec2 *ray2)
{
  float dot_prod;
  dot_prod = ray1->x * ray2->x + ray1->y * ray2->y;
  return dot_prod;
}

return_type: float


decompiled_code:
void __fastcall vec2_scale(float *a1, float a2)
{
*a1 = a2 * *a1;
a1[1] = a2 * a1[1];
}

canonical_decompiled_code:
void __fastcall func2(float *a1, float a2)
{
*a1 = a2 * *a1;
a1[1] = a2 * a1[1];
}

original_code:
void vec2_scale(vec2_t* ray, vec2_type scale_fact)
{
  ray->x *= scale_fact;
  ray->y *= scale_fact;
}

canonical_original_code:
void vec2_scale(struct vec2 *ray, float scale_fact)
{
  ray->x *= scale_fact;
  ray->y *= scale_fact;
}


decompiled_code:
void __fastcall vec2_normalize(float *a1)
{
float v1; // xmm0_4
double v2; // xmm0_8
double v3; // xmm0_8
float v4; // xmm0_4

v1 = vec2_dot(a1, a1);
v2 = v1 * v1;
if ( v2 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
v4 = 1.0 / v3;
vec2_scale(a1, v4);
}

canonical_decompiled_code:
void __fastcall func3(float *a1)
{
float v1; // xmm0_4
double v2; // xmm0_8
double v3; // xmm0_8
float v4; // xmm0_4

v1 = func1(a1, a1);
v2 = v1 * v1;
if ( v2 < 0.0 )
v3 = sqrt(v2);
else
v3 = sqrt(v2);
v4 = 1.0 / v3;
func2(a1, v4);
}

original_code:
void vec2_normalize(vec2_t* ray)
{
  vec2_type scale_fact;
  scale_fact = 1.0 / sqrt(pow(vec2_dot(ray, ray),2.0));
  vec2_scale(ray, scale_fact);
}

canonical_original_code:
void vec2_normalize(struct vec2 *ray)
{
  float scale_fact;
  scale_fact = 1.0 / sqrt(pow(vec2_dot(ray, ray),2.0));
  vec2_scale(ray, scale_fact);
}


decompiled_code:
double __fastcall vec2_add(unsigned int *a1, float *a2)
{
__m128i v2; // xmm1
__m128i v3; // xmm0
double result; // xmm0_8

v2 = (__m128i)a1[1];
v3 = (__m128i)*a1;
*(float *)v3.m128i_i32 = *(float *)v3.m128i_i32 + *a2;
*(float *)v2.m128i_i32 = *(float *)v2.m128i_i32 + a2[1];
*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(v2) << 32) | (unsigned int)_mm_cvtsi128_si32(v3);
return result;
}

canonical_decompiled_code:
double __fastcall func4(unsigned int *a1, float *a2)
{
__m128i v2; // xmm1
__m128i v3; // xmm0
double result; // xmm0_8

v2 = (__m128i)a1[1];
v3 = (__m128i)*a1;
*(float *)v3.m128i_i32 = *(float *)v3.m128i_i32 + *a2;
*(float *)v2.m128i_i32 = *(float *)v2.m128i_i32 + a2[1];
*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32(v2) << 32) | (unsigned int)_mm_cvtsi128_si32(v3);
return result;
}

original_code:
vec2_t vec2_add(const vec2_t* left, const vec2_t* right)
{
  vec2_t ray;
  ray.x = left->x + right->x;
  ray.y = left->y + right->y;
  return ray;
}

canonical_original_code:
struct vec2 vec2_add(struct vec2 *left, struct vec2 *right)
{
  struct vec2 ray;
  ray.x = left->x + right->x;
  ray.y = left->y + right->y;
  return ray;
}

variable_types:
left
struct vec2 *

variable_types:
right
struct vec2 *

variable_types:
ray
struct vec2 { float x; float y; }

return_type: struct vec2 { float x; float y; }


decompiled_code:
double __fastcall vec2_negate(unsigned int *a1)
{
double result; // xmm0_8

*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32((__m128i)_mm_xor_ps(
(__m128)a1[1],
(__m128)(unsigned int)LC2)) << 32) | (unsigned int)_mm_cvtsi128_si32((__m128i)_mm_xor_ps((__m128)*a1, (__m128)(unsigned int)LC2));
return result;
}

canonical_decompiled_code:
double __fastcall func5(unsigned int *a1)
{
double result; // xmm0_8

*(_QWORD *)&result = ((unsigned __int64)(unsigned int)_mm_cvtsi128_si32((__m128i)_mm_xor_ps(
(__m128)a1[1],
(__m128)(unsigned int)LC2)) << 32) | (unsigned int)_mm_cvtsi128_si32((__m128i)_mm_xor_ps((__m128)*a1, (__m128)(unsigned int)LC2));
return result;
}

original_code:
vec2_t vec2_negate(const vec2_t* ray)
{
  vec2_t negation;
  negation.x = -ray->x;
  negation.y = -ray->y;
  return negation;
}

canonical_original_code:
struct vec2 vec2_negate(struct vec2 *ray)
{
  struct vec2 negation;
  negation.x = -ray->x;
  negation.y = -ray->y;
  return negation;
}

variable_types:
ray
struct vec2 *

variable_types:
negation
struct vec2 { float x; float y; }


decompiled_code:
void __fastcall vec2_scale_x_to(float *a1, float a2)
{
vec2_scale(a1, a2 / *a1);
}

canonical_decompiled_code:
void __fastcall func6(float *a1, float a2)
{
func2(a1, a2 / *a1);
}

original_code:
void vec2_scale_x_to(vec2_t* ray, vec2_type target)
{
  vec2_type scale_fact;
  scale_fact = target / ray->x;
  vec2_scale(ray, scale_fact);
}

canonical_original_code:
void vec2_scale_x_to(struct vec2 *ray, float target)
{
  float scale_fact;
  scale_fact = target / ray->x;
  vec2_scale(ray, scale_fact);
}

variable_types: target float


decompiled_code:
void __fastcall vec2_scale_y_to(float *a1, float a2)
{
vec2_scale(a1, a2 / a1[1]);
}

canonical_decompiled_code:
void __fastcall func7(float *a1, float a2)
{
func2(a1, a2 / a1[1]);
}

original_code:
void vec2_scale_y_to(vec2_t* ray, vec2_type target)
{
  vec2_type scale_fact;
  scale_fact = target / ray->y;
  vec2_scale(ray, scale_fact);
}

canonical_original_code:
void vec2_scale_y_to(struct vec2 *ray, float target)
{
  float scale_fact;
  scale_fact = target / ray->y;
  vec2_scale(ray, scale_fact);
}


decompiled_code:
void *texture_name_list_get()
{
return &name_list_0;
}

canonical_decompiled_code:
void *func0()
{
return &name_list_0;
}

original_code:
static texture_name_list_t* texture_name_list_get()
{
  static texture_name_list_t name_list;
  return &name_list;
}

canonical_original_code:
static struct texture_name_list_s *texture_name_list_get()
{
  static struct texture_name_list_s name_list;
  return &name_list;
}

return_type: struct texture_name_list_s { struct texture_name_s { char *name; unsigned char id; } names[255]; unsigned char size; } *

user_defined_types: struct texture_name_list_s { struct texture_name_s names[255]; unsigned char size; }


decompiled_code:
int __fastcall texture_cmp_name(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
int texture_cmp_name(const void * a, const void * b)
{
  const texture_name_t * name_a = a;
  const texture_name_t * name_b = b;
  return strcmp(name_a->name, name_b->name);
}

canonical_original_code:
int texture_cmp_name(void *a, void *b)
{
  struct texture_name_s *name_a = a;
  struct texture_name_s *name_b = b;
  return strcmp(name_a->name, name_b->name);
}

variable_types:
a
void *

variable_types:
b
void *

variable_types: struct texture_name_s { char *name; unsigned char id; } *

return_type: int


decompiled_code:
__int64 __fastcall texture_name_locate(void *key, _BYTE *a2)
{
unsigned __int8 *v2; // rax
_BYTE *v3; // rax
unsigned int v4; // edx

v2 = (unsigned __int8 *)texture_name_list_get();
v3 = bsearch(key, v2, v2[4080], 0x10uLL, texture_cmp_name);
v4 = 0;
if ( v3 )
{
*a2 = v3[8];
v4 = 1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *key, _BYTE *a2)
{
unsigned __int8 *v2; // rax
_BYTE *v3; // rax
unsigned int v4; // edx

v2 = (unsigned __int8 *)func0();
v3 = bsearch(key, v2, v2[4080], 0x10uLL, texture_cmp_name);
v4 = 0;
if ( v3 )
{
*a2 = v3[8];
v4 = 1;
}
return v4;
}

original_code:
static int8_t texture_name_locate(const char* name, uint8_t* id)
{
  texture_name_t* element;
  texture_name_list_t* list;
  list = texture_name_list_get();
  element = bsearch(name, list->names, list->size, sizeof(texture_name_t), &texture_cmp_name);
  if(element) {
    *id = element->id;
    return 1;
  } else {
    return 0;
  }
}

canonical_original_code:
static char texture_name_locate(char *name, unsigned char *id)
{
  struct texture_name_s *element;
  struct texture_name_list_s *list;
  list = texture_name_list_get();
  element = bsearch(name, list->names, list->size, sizeof(texture_name_t), &texture_cmp_name);
  if(element) {
    *id = element->id;
    return 1;
  } else {
    return 0;
  }
}

variable_types:
name
char *

variable_types:
id
unsigned char *

variable_types: struct texture_name_list_s { struct texture_name_s { char *name; unsigned char id; } names[255]; unsigned char size; } *

return_type: char


decompiled_code:
void __fastcall texture_name_add(__int64 a1, char a2)
{
_BYTE *v4; // rdi
unsigned __int8 v5; // si
_BYTE *v6; // rax

v4 = texture_name_list_get();
v5 = v4[4080];
v6 = &v4[16 * v5];
*(_QWORD *)v6 = a1;
v4[4080] = ++v5;
v6[8] = a2;
qsort(v4, v5, 0x10uLL, texture_cmp_name);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, char a2)
{
_BYTE *v4; // rdi
unsigned __int8 v5; // si
_BYTE *v6; // rax

v4 = func0();
v5 = v4[4080];
v6 = &v4[16 * v5];
*(_QWORD *)v6 = a1;
v4[4080] = ++v5;
v6[8] = a2;
qsort(v4, v5, 0x10uLL, texture_cmp_name);
}

original_code:
static void texture_name_add(const char* name, uint8_t id)
{
  texture_name_list_t* list;
  list = texture_name_list_get();
  list->names[list->size].name = name;
  list->names[list->size++].id = id;
  qsort(list, list->size, sizeof(texture_name_t), &texture_cmp_name);
}

canonical_original_code:
static void texture_name_add(char *name, unsigned char id)
{
  struct texture_name_list_s *list;
  list = texture_name_list_get();
  list->names[list->size].name = name;
  list->names[list->size++].id = id;
  qsort(list, list->size, sizeof(texture_name_t), &texture_cmp_name);
}

variable_types:
id
unsigned char

user_defined_types: struct texture_name_s { char *name; unsigned char id; }


decompiled_code:
void __fastcall set_seed(__int64 a1)
{
unseeded = 0;
g_seed = a1;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
unseeded = 0;
g_seed = a1;
}

original_code:
static void set_seed(uint64_t input)
{
  unseeded = 0;
  g_seed = input;
}

canonical_original_code:
static void set_seed(unsigned long input)
{
  unseeded = 0;
  g_seed = input;
}

variable_types:
input
unsigned long


decompiled_code:
__suseconds_t get_seed()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( !unseeded )
return g_seed;
gettimeofday(&v1, 0LL);
g_seed = v1.tv_usec + (v1.tv_sec << 32);
unseeded = 0;
return v1.tv_usec + (v1.tv_sec << 32);
}

canonical_decompiled_code:
__suseconds_t func1()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( !unseeded )
return g_seed;
gettimeofday(&v1, 0LL);
g_seed = v1.tv_usec + (v1.tv_sec << 32);
unseeded = 0;
return v1.tv_usec + (v1.tv_sec << 32);
}

original_code:
static uint64_t get_seed()
{
  if(unseeded) {
    struct timeval tim;
    gettimeofday(&tim, 0);
    g_seed = tim.tv_sec;
    g_seed <<= 32;
    g_seed += tim.tv_usec;
    unseeded = 0;
  }
  return g_seed;
}

canonical_original_code:
static unsigned long get_seed()
{
  if(unseeded) {
    struct timeval tim;
    gettimeofday(&tim, 0);
    g_seed = tim.tv_sec;
    g_seed <<= 32;
    g_seed += tim.tv_usec;
    unseeded = 0;
  }
  return g_seed;
}

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

return_type: unsigned long


decompiled_code:
void __fastcall xorshift_srand(__int64 a1)
{
if ( !a1 )
a1 = 1LL;
set_seed(a1);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
if ( !a1 )
a1 = 1LL;
func0(a1);
}

original_code:
void xorshift_srand(uint64_t seed)
{
  set_seed(seed ? seed : 1);
}

canonical_original_code:
void xorshift_srand(unsigned long seed)
{
  set_seed(seed ? seed : 1);
}

variable_types:
seed
unsigned long


decompiled_code:
__int64 xorshift_rand()
{
__suseconds_t v0; // rax
unsigned __int64 v1; // rax
__int64 v2; // rbx

v0 = get_seed();
v1 = ((v0 ^ (unsigned __int64)(v0 << 21)) >> 1) ^ v0 ^ (v0 << 21);
v2 = v1 ^ (v1 << 27);
set_seed(v2);
return v2;
}

original_code:
uint64_t xorshift_rand(void)
{
  static const uint16_t a = 21;
  static const uint16_t b = 1;
  static const uint16_t c = 27;
  uint64_t y;
  y = get_seed();
  y ^= y<<a;
  y ^= y>>b;
  y ^= y<<c;
  set_seed(y);
  return y;
}

canonical_original_code:
unsigned long xorshift_rand()
{
  static unsigned short a = 21;
  static unsigned short b = 1;
  static unsigned short c = 27;
  unsigned long y;
  y = get_seed();
  y ^= y<<a;
  y ^= y>>b;
  y ^= y<<c;
  set_seed(y);
  return y;
}

variable_types:
a
unsigned short

variable_types:
b
unsigned short

variable_types:
c
unsigned short

variable_types:
y
unsigned long


original_code:
void enemy_director_add(enemy_director_t* dir, enemy_t* enemy)
{
  long_free_list_t* old_free;
  if(dir->first_free == 0)
    enemy_director_grow(dir);
  old_free = dir->first_free;
  dir->first_free = old_free->next;
  enemy->id = old_free->data;
  enemy->valid = 1;
  memcpy(dir->enemies + old_free->data, enemy, sizeof(enemy_t));
  free(old_free);
}

canonical_original_code:
void enemy_director_add(struct enemy_director_t *dir, struct enemy_t *enemy)
{
  struct long_free_list_t *old_free;
  if(dir->first_free == 0)
    enemy_director_grow(dir);
  old_free = dir->first_free;
  dir->first_free = old_free->next;
  enemy->id = old_free->data;
  enemy->valid = 1;
  memcpy(dir->enemies + old_free->data, enemy, sizeof(enemy_t));
  free(old_free);
}

variable_types: struct enemy_director_t { struct enemy_t { struct vec2 { float x; float y; } position; float width; long id; unsigned char valid; } *enemies; long size; long _max_size; struct long_free_list_t { long data; struct long_free_list_t *next; } *first_free; } *

variable_types:
enemy
struct enemy_t *

user_defined_types: struct enemy_director_t { struct enemy_t *enemies; long size; long _max_size; struct long_free_list_t *first_free; }

user_defined_types: struct enemy_t { struct vec2 position; float width; long id; unsigned char valid; }


decompiled_code:
void __fastcall enemy_director_free(__int64 a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)(a1 + 24);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
free(*(void **)a1);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = *(_QWORD **)(a1 + 24);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[1];
free(v3);
}
free(*(void **)a1);
}

original_code:
void enemy_director_free(enemy_director_t* dir)
{
  long_free_list_t* free_list;
  free_list = dir->first_free;
  while(free_list) {
    long_free_list_t* current;
    current = free_list;
    free_list = free_list->next;
    free(current);
  }
  free(dir->enemies);
}

canonical_original_code:
void enemy_director_free(struct enemy_director_t *dir)
{
  struct long_free_list_t *free_list;
  free_list = dir->first_free;
  while(free_list) {
    struct long_free_list_t *current;
    current = free_list;
    free_list = free_list->next;
    free(current);
  }
  free(dir->enemies);
}

variable_types: struct long_free_list_t { long data; struct long_free_list_t *next; } *


decompiled_code:
_DWORD *controller_new()
{
_DWORD *v0; // rdx

v0 = malloc(0x3FCuLL);
memset(v0, 0, 0x3F8uLL);
v0[254] = 0;
return v0;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rdx

v0 = malloc(0x3FCuLL);
memset(v0, 0, 0x3F8uLL);
v0[254] = 0;
return v0;
}

original_code:
controller_t* controller_new()
{
  struct controller_s * new_controller = malloc(sizeof(struct controller_s));
  memset(new_controller, 0, sizeof(controller_t));
  return new_controller;
}

canonical_original_code:
struct controller_s *controller_new()
{
  struct controller_s *new_controller = malloc(sizeof(struct controller_s));
  memset(new_controller, 0, sizeof(controller_t));
  return new_controller;
}

variable_types: struct controller_s { int state[-1]; } *


decompiled_code:
__int64 __fastcall controller_is_button_down(__int64 a1, int a2)
{
return *(unsigned int *)(a1 + 4LL * a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
return *(unsigned int *)(a1 + 4LL * a2);
}

original_code:
int controller_is_button_down(controller_t* ctrl, int keysym)
{
  return ctrl->state[keysym];
}

canonical_original_code:
int controller_is_button_down(struct controller_s *ctrl, int keysym)
{
  return ctrl->state[keysym];
}

variable_types: keysym int


decompiled_code:
_BOOL8 __fastcall controller_is_button_up(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 4LL * a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 4LL * a2) == 0;
}

original_code:
int controller_is_button_up(controller_t* ctrl, int keysym)
{
  return !(ctrl->state[keysym]);
}

canonical_original_code:
int controller_is_button_up(struct controller_s *ctrl, int keysym)
{
  return !(ctrl->state[keysym]);
}


decompiled_code:
void __fastcall controller_button_came_up(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4LL * a2) = 0;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4LL * a2) = 0;
}

original_code:
void controller_button_came_up(controller_t* ctrl, int keysym)
{
  ctrl->state[keysym] = 0;
}

canonical_original_code:
void controller_button_came_up(struct controller_s *ctrl, int keysym)
{
  ctrl->state[keysym] = 0;
}


decompiled_code:
void __fastcall controller_button_went_down(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4LL * a2) = 1;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 4LL * a2) = 1;
}

original_code:
void controller_button_went_down(controller_t* ctrl, int keysym)
{
  ctrl->state[keysym] = 1;
}

canonical_original_code:
void controller_button_went_down(struct controller_s *ctrl, int keysym)
{
  ctrl->state[keysym] = 1;
}


decompiled_code:
float __fastcall calculate_fov(float a1, float a2, float a3)
{
float v3; // xmm1_4

v3 = a2 * 0.5 / tan(a3 * 0.5);
return atan2(a1 * 0.5, v3);
}

canonical_decompiled_code:
float __fastcall func0(float a1, float a2, float a3)
{
float v3; // xmm1_4

v3 = a2 * 0.5 / tan(a3 * 0.5);
return atan2(a1 * 0.5, v3);
}

original_code:
vec2_type calculate_fov(vec2_type xres, vec2_type yres, vec2_type vfov)
{
  vec2_type out;
  vec2_type fd;
  fd = yres * 0.5 / tan(vfov * 0.5);
  out = atan2(xres * 0.5, fd);
  return out;
}

canonical_original_code:
float calculate_fov(float xres, float yres, float vfov)
{
  float out;
  float fd;
  fd = yres * 0.5 / tan(vfov * 0.5);
  out = atan2(xres * 0.5, fd);
  return out;
}

variable_types: xres float

variable_types: yres float

variable_types: vfov float

variable_types: out float

variable_types: fd float


decompiled_code:
__int64 __fastcall raycast_scene(__int64 a1, __int64 a2)
{
int v2; // er12
__int64 v3; // rax
float v4; // xmm0_4

v2 = *(_DWORD *)(framebuffer_get() + 16);
v3 = framebuffer_get();
v4 = calculate_fov((float)v2, (float)*(int *)(v3 + 20), 90.0);
return raycaster_cast_rays(&state, (unsigned int)((_DWORD)&state + 8), a1, a2, v4);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // er12
__int64 v3; // rax
float v4; // xmm0_4

v2 = *(_DWORD *)(framebuffer_get() + 16);
v3 = framebuffer_get();
v4 = func0((float)v2, (float)*(int *)(v3 + 20), 90.0);
return raycaster_cast_rays(&state, (unsigned int)((_DWORD)&state + 8), a1, a2, v4);
}

original_code:
void raycast_scene(vec2_t* hits, const size_t num_hits)
{
  vec2_type theta;
  int width, height;
  width = framebuffer_get()->w;
  height = framebuffer_get()->h;
  theta = calculate_fov(width, height, 90.0);
  raycaster_cast_rays(&state.cam_pos, &state.cam_dir, theta,
        hits, num_hits);
}

canonical_original_code:
void raycast_scene(struct vec2 *hits, unsigned long num_hits)
{
  float theta;
  int width; int height;
  width = framebuffer_get()->w;
  height = framebuffer_get()->h;
  theta = calculate_fov(width, height, 90.0);
  raycaster_cast_rays(&state.cam_pos, &state.cam_dir, theta,
        hits, num_hits);
}

variable_types:
hits
struct vec2 { float x; float y; } *

variable_types: theta float

variable_types: width int

variable_types: height int


decompiled_code:
void __fastcall compute_cam_dists(__int64 a1, __int64 a2, __int64 a3)
{
__int64 i; // rbx
double v6; // [rsp+0h] [rbp-38h] BYREF
double v7[6]; // [rsp+8h] [rbp-30h] BYREF

if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
v7[0] = vec2_negate(&state);
v6 = vec2_add(a1, v7);
vec2_normalize((unsigned int)((_DWORD)&state + 8));
*(float *)(a3 + 4 * i) = vec2_dot((unsigned int)((_DWORD)&state + 8), &v6);
a1 += 8LL;
}
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 i; // rbx
double v6; // [rsp+0h] [rbp-38h] BYREF
double v7[6]; // [rsp+8h] [rbp-30h] BYREF

if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
v7[0] = vec2_negate(&state);
v6 = vec2_add(a1, v7);
vec2_normalize((unsigned int)((_DWORD)&state + 8));
*(float *)(a3 + 4 * i) = vec2_dot((unsigned int)((_DWORD)&state + 8), &v6);
a1 += 8LL;
}
}
}

original_code:
void compute_cam_dists(const vec2_t* hits, const size_t num_hits,
         vec2_type* dists)
{
  int i;
  vec2_t negative_origin;
  vec2_t result;
  i = 0;
  for(i = 0; i < num_hits; ++i) {
    vec2_type dist;
    negative_origin = vec2_negate(&state.cam_pos);
    result = vec2_add(&hits[i], &negative_origin);
    vec2_normalize(&state.cam_dir);
    dist = vec2_dot(&state.cam_dir, &result);
    dists[i] = dist;
  }
}

canonical_original_code:
void compute_cam_dists(struct vec2 *hits, unsigned long num_hits, float *dists)
{
  int i;
  struct vec2 negative_origin;
  struct vec2 result;
  i = 0;
  for(i = 0; i < num_hits; ++i) {
    float dist;
    negative_origin = vec2_negate(&state.cam_pos);
    result = vec2_add(&hits[i], &negative_origin);
    vec2_normalize(&state.cam_dir);
    dist = vec2_dot(&state.cam_dir, &result);
    dists[i] = dist;
  }
}

variable_types:
dists
float *

variable_types: i int

variable_types:
result
struct vec2 { float x; float y; }

variable_types: dist float


decompiled_code:
__int64 update_scene()
{
return time_update(&span);
}

canonical_decompiled_code:
__int64 func3()
{
return time_update(&span);
}

original_code:
void update_scene(void)
{
  time_update(&span);
}

canonical_original_code:
void update_scene()
{
  time_update(&span);
}


original_code:
uint32_t make_rgb(float r, float g, float b, SDL_PixelFormat* fmt)
{
  uint32_t color, red, green, blue;
  static const uint32_t white = 0xffffffff;
  red = (white & fmt->Rmask) >> fmt->Rshift;
  green = (white & fmt->Gmask) >> fmt->Gshift;
  blue = (white & fmt->Bmask) >> fmt->Bshift;
  red *= r;
  green *= g;
  blue *= b;
  color = red << fmt->Rshift;
  color += green << fmt->Gshift;
  color += blue << fmt->Bshift;
  return color;
}

canonical_original_code:
unsigned int make_rgb(float r, float g, float b, struct SDL_PixelFormat *fmt)
{
  unsigned int color; unsigned int red; unsigned int green; unsigned int blue;
  static unsigned int white = 0xffffffff;
  red = (white & fmt->Rmask) >> fmt->Rshift;
  green = (white & fmt->Gmask) >> fmt->Gshift;
  blue = (white & fmt->Bmask) >> fmt->Bshift;
  red *= r;
  green *= g;
  blue *= b;
  color = red << fmt->Rshift;
  color += green << fmt->Gshift;
  color += blue << fmt->Bshift;
  return color;
}

variable_types: r float

variable_types: g float

variable_types: b float

variable_types: struct SDL_PixelFormat { struct SDL_Palette { int ncolors; struct SDL_Color { unsigned char r; unsigned char g; unsigned char b; unsigned char unused; } *colors; } *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; } *

variable_types:
color
unsigned int

variable_types:
red
unsigned int

variable_types:
green
unsigned int

variable_types:
blue
unsigned int

variable_types:
white
unsigned int

return_type: unsigned int

user_defined_types: struct SDL_PixelFormat { struct SDL_Palette *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; }

user_defined_types: struct SDL_Palette { int ncolors; struct SDL_Color *colors; }


decompiled_code:
__int64 initialize_camera()
{
__int64 result; // rax

span[1] = 0LL;
time_update(span);
result = time_update(span);
LODWORD(state) = LC1;
HIDWORD(state) = LC1;
*((_DWORD *)&state + 2) = -1082130432;
*((_DWORD *)&state + 3) = 0;
return result;
}

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

span[1] = 0LL;
time_update(span);
result = time_update(span);
LODWORD(state) = LC1;
HIDWORD(state) = LC1;
*((_DWORD *)&state + 2) = -1082130432;
*((_DWORD *)&state + 3) = 0;
return result;
}

original_code:
void initialize_camera(void)
{
  span.current = 0.0;
  time_update(&span);
  time_update(&span);
  state.cam_pos.x = 3.5;
  state.cam_pos.y = 3.5;
  state.cam_dir.x = -1.0;
  state.cam_dir.y = 0.0;
}

canonical_original_code:
void initialize_camera()
{
  span.current = 0.0;
  time_update(&span);
  time_update(&span);
  state.cam_pos.x = 3.5;
  state.cam_pos.y = 3.5;
  state.cam_dir.x = -1.0;
  state.cam_dir.y = 0.0;
}


decompiled_code:
void __fastcall handle_normal_keys(char a1)
{
if ( a1 == 27 )
exit(0);
}

canonical_decompiled_code:
void __fastcall func1(char a1)
{
if ( a1 == 27 )
exit(0);
}

original_code:
void handle_normal_keys(unsigned char key, int x, int y)
{
  switch(key) {
  case 27:
    exit(0);
    break;
  }
}

variable_types:
key
unsigned char

variable_types: x int

variable_types: y int


decompiled_code:
void *framebuffer_wipe()
{
return memset(
*(void **)(framebuffer + 32),
0,
*(unsigned __int8 *)(*(_QWORD *)(framebuffer + 8) + 9LL)
* *(_DWORD *)(framebuffer + 20)
* *(_DWORD *)(framebuffer + 16));
}

canonical_decompiled_code:
void *func2()
{
return memset(
*(void **)(framebuffer + 32),
0,
*(unsigned __int8 *)(*(_QWORD *)(framebuffer + 8) + 9LL)
* *(_DWORD *)(framebuffer + 20)
* *(_DWORD *)(framebuffer + 16));
}

original_code:
void framebuffer_wipe()
{
  SDL_PixelFormat *fmt;
  fmt = framebuffer->format;
  memset(framebuffer->pixels,
  0, framebuffer->w*framebuffer->h*fmt->BytesPerPixel);
}

canonical_original_code:
void framebuffer_wipe()
{
  struct SDL_PixelFormat *fmt;
  fmt = framebuffer->format;
  memset(framebuffer->pixels,
  0, framebuffer->w*framebuffer->h*fmt->BytesPerPixel);
}

user_defined_types: struct SDL_Color { unsigned char r; unsigned char g; unsigned char b; unsigned char unused; }


repo: saulpw/brainfuck

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

fprintf(stderr, "IP=%d %c DP=%d ", a1, (unsigned int)code[a1], (unsigned int)dp);
v1 = &data;
do
fprintf(stderr, "% 3d ", (unsigned int)*v1++);
while ( v1 != (char *)&unk_4044EA );
return fputc(10, stderr);
}

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

fprintf(stderr, "IP=%d %c DP=%d ", a1, (unsigned int)code[a1], (unsigned int)dp);
v1 = &data;
do
fprintf(stderr, "% 3d ", (unsigned int)*v1++);
while ( v1 != (char *)&unk_4044EA );
return fputc(10, stderr);
}

original_code:
void dump_state(int ip)
{
    int i;
    fprintf(stderr, "IP=%d %c  DP=%d  ", ip, code[ip], dp);
    for (i=0; i < 10; i++)
        fprintf(stderr, "% 3d ", data[i]);
    fprintf(stderr, "\n");
}

variable_types: ip int


decompiled_code:
int __fastcall out_data(char a1)
{
FILE *v2; // rdi
int result; // eax

v2 = fpgolden;
if ( fpgolden && !verbose || (result = putchar(a1), (v2 = fpgolden) != 0LL) )
{
result = fgetc(v2);
if ( a1 != (_BYTE)result )
result = fprintf(stderr, "Unexpected output '%c' != golden '%c'\n", (unsigned int)a1, (unsigned int)(char)result);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char a1)
{
FILE *v2; // rdi
int result; // eax

v2 = fpgolden;
if ( fpgolden && !verbose || (result = putchar(a1), (v2 = fpgolden) != 0LL) )
{
result = fgetc(v2);
if ( a1 != (_BYTE)result )
result = fprintf(stderr, "Unexpected output '%c' != golden '%c'\n", (unsigned int)a1, (unsigned int)(char)result);
}
return result;
}

original_code:
void out_data(char d)
{
    if (fpgolden == ((void *)0) || verbose)
        printf("%c", d);
    if (fpgolden)
    {
        char ch = fgetc(fpgolden);
        if (d != ch)
        {
            fprintf(stderr, "Unexpected output '%c' != golden '%c'\n", d, ch);
        }
    }
}

variable_types: d char

variable_types: ch char


scan

decompiled_code:
__int64 __fastcall scan(__int64 a1, int a2, int a3, unsigned __int8 a4, char a5)
{
_BYTE *v5; // rdi
int v6; // eax

v5 = (_BYTE *)(a3 + (__int64)a2 + a1);
v6 = 1;
while ( 1 )
{
if ( a2 < 0 )
return 4294967294LL;
if ( ncode < a2 )
break;
a2 += a3;
if ( *v5 == a5 )
++v6;
else
v6 -= *v5 == a4;
v5 += a3;
if ( !v6 )
return (unsigned int)a2;
}
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, int a3, unsigned __int8 a4, char a5)
{
_BYTE *v5; // rdi
int v6; // eax

v5 = (_BYTE *)(a3 + (__int64)a2 + a1);
v6 = 1;
while ( 1 )
{
if ( a2 < 0 )
return 4294967294LL;
if ( ncode < a2 )
break;
a2 += a3;
if ( *v5 == a5 )
++v6;
else
v6 -= *v5 == a4;
v5 += a3;
if ( !v6 )
return (unsigned int)a2;
}
return 4294967294LL;
}

original_code:
int scan(const char *p, int start, int dir, char match, char recurse)
{
    int idx = start;
    int n = 1;
    do {
        if (idx < 0 || idx > ncode) return -2;
        idx += dir;
        if (p[idx] == recurse) ++n;
        else if (p[idx] == match) --n;
    } while (n != 0);
    return idx;
}

canonical_original_code:
int scan(char *p, int start, int dir, char match, char recurse)
{
    int idx = start;
    int n = 1;
    do {
        if (idx < 0 || idx > ncode) return -2;
        idx += dir;
        if (p[idx] == recurse) ++n;
        else if (p[idx] == match) --n;
    } while (n != 0);
    return idx;
}

variable_types:
p
char *

variable_types: start int

variable_types: dir int

variable_types: match char

variable_types: recurse char

variable_types: idx int

variable_types: n int


execute

decompiled_code:
__int64 __fastcall execute(int a1)
{
int i; // ebp
__int64 result; // rax
char v3; // bl

for ( i = a1; i >= 0; ++i )
{
if ( ncode < i || (unsigned int)dp > 0x7530 )
break;
v3 = code[i];
if ( verbose > 2 )
dump_state(i);
if ( v3 > 93 )
{
if ( v3 == 123 )
{
opcodes[code[i + 1]] = i + 2;
i = scan((__int64)code, i, 1, 0x7Du, 123);
continue;
}
if ( v3 == 125 )
return 1LL;
}
else
{
if ( v3 > 34 )
{
switch ( v3 )
{
case '#':
if ( verbose )
dump_state(i);
continue;
case '+':
++data[dp];
continue;
case ',':
data[dp] = getc(fpinput);
continue;
case '-':
--data[dp];
continue;
case '.':
out_data(data[dp]);
continue;
case '<':
--dp;
continue;
case '>':
++dp;
continue;
case '[':
if ( !data[dp] )
i = scan((__int64)code, i, 1, 0x5Du, 91);
continue;
case ']':
if ( data[dp] )
i = scan((__int64)code, i, -1, 0x5Bu, 93);
continue;
default:
goto LABEL_29;
}
}
if ( !v3 )
return 0LL;
}
LABEL_29:
if ( opcodes[v3] )
{
result = execute();
if ( (int)result <= 0 )
return result;
}
else if ( verbose > 1 )
{
fputc(v3, stderr);
}
}
fwrite("out-of-bounds: ", 1uLL, 0xFuLL, stderr);
dump_state(i);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
int i; // ebp
__int64 result; // rax
char v3; // bl

for ( i = a1; i >= 0; ++i )
{
if ( ncode < i || (unsigned int)dp > 0x7530 )
break;
v3 = code[i];
if ( verbose > 2 )
func7(i);
if ( v3 > 93 )
{
if ( v3 == 123 )
{
opcodes[code[i + 1]] = i + 2;
i = func9((__int64)code, i, 1, 0x7Du, 123);
continue;
}
if ( v3 == 125 )
return 1LL;
}
else
{
if ( v3 > 34 )
{
switch ( v3 )
{
case '#':
if ( verbose )
func7(i);
continue;
case '+':
++data[dp];
continue;
case ',':
data[dp] = getc(fpinput);
continue;
case '-':
--data[dp];
continue;
case '.':
func8(data[dp]);
continue;
case '<':
--dp;
continue;
case '>':
++dp;
continue;
case '[':
if ( !data[dp] )
i = func9((__int64)code, i, 1, 0x5Du, 91);
continue;
case ']':
if ( data[dp] )
i = func9((__int64)code, i, -1, 0x5Bu, 93);
continue;
default:
goto LABEL_29;
}
}
if ( !v3 )
return 0LL;
}
LABEL_29:
if ( opcodes[v3] )
{
result = func10();
if ( (int)result <= 0 )
return result;
}
else if ( verbose > 1 )
{
fputc(v3, stderr);
}
}
fwrite("out-of-bounds: ", 1uLL, 0xFuLL, stderr);
func7(i);
return 0xFFFFFFFFLL;
}

original_code:
int execute(int ip)
{
    while (1) {
        if (ip < 0 || ip > ncode || dp < 0 || dp > sizeof(data))
        {
            fprintf(stderr, "out-of-bounds: ");
            dump_state(ip);
            return -1;
        }
        char op = code[ip];
        if (verbose >= 3) dump_state(ip);
        switch (op)
        {
        case '>': dp += 1; break;
        case '<': dp -= 1; break;
        case '+': data[dp] += 1; break;
        case '-': data[dp] -= 1; break;
        case '.': out_data(data[dp]); break;
        case ',': data[dp] = getc(fpinput); break;
        case '[': if (data[dp] == 0)
                    ip = scan(code, ip, 1, ']', '[');
                  break;
        case ']': if (data[dp] != 0)
                    ip = scan(code, ip, -1, '[', ']');
                  break;
        case 0: return 0;
        case '#': if (verbose) dump_state(ip); break;
        case '{':
                  opcodes[ code[ip+1] ] = ip+2;
                  ip = scan(code, ip, 1, '}', '{');
                  break;
        case '}': return 1;
        default:
                  if (opcodes[op] != 0) {
                    int r = execute(opcodes[op]);
                    if (r <= 0) return r;
                  } else
                  if (verbose >= 2) fprintf(stderr, "%c", op);
                  break;
        };
        ip += 1;
    }
}

variable_types: op char

variable_types: r int


repo: saulrh/Liberty-Daemon

decompiled_code:
void __fastcall fork_sig_handler(int a1)
{
if ( a1 == 10 )
exit(0);
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
if ( a1 == 10 )
exit(0);
}

original_code:
static void fork_sig_handler(int signum)
{
    switch(signum)
    {
        case 10: exit(0); break;
    }
}

variable_types: signum int


decompiled_code:
void __fastcall daemon_sig_handler(int a1)
{
switch ( a1 )
{
case 2:
case 3:
case 15:
daemon_sig_quit = 1;
break;
case 10:
daemon_sig_usr1 = 1;
break;
case 12:
daemon_sig_usr2 = 1;
break;
case 14:
exit(1);
case 17:
exit(1);
default:
return;
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
switch ( a1 )
{
case 2:
case 3:
case 15:
daemon_sig_quit = 1;
break;
case 10:
daemon_sig_usr1 = 1;
break;
case 12:
daemon_sig_usr2 = 1;
break;
case 14:
exit(1);
case 17:
exit(1);
default:
return;
}
}

original_code:
static void daemon_sig_handler(int signum)
{
    switch(signum)
    {
        case 14: exit(1); break;
        case 10: daemon_sig_usr1 = 1; break;
        case 12: daemon_sig_usr2 = 1; break;
        case 17: exit(1); break;
        case 2:
        case 3:
        case 15: daemon_sig_quit=1; break;
    }
}


daemonize

decompiled_code:
void __fastcall daemonize(char *src)
{
struct passwd *v1; // rbp
__pid_t v2; // eax
__pid_t v3; // eax
__pid_t v4; // ebp
int *v5; // rbx
char *v6; // rax
int *v7; // rbx
char *v8; // rax
int *v9; // rbx
char *v10; // rax
__pid_t v11; // eax
int *v12; // rbx
char *v13; // rax
int *v14; // rbx
char *v15; // rax
int *v16; // rbx
char *v17; // rax
int *v18; // rbx
char *v19; // rax
struct sched_param param; // [rsp+Ch] [rbp-FCh] BYREF
char filename[112]; // [rsp+10h] [rbp-F8h] BYREF
char s[136]; // [rsp+80h] [rbp-88h] BYREF

strcpy(gdaemon_name, src);
syslog(5, "Starting daemonization for %s", src);
daemon_sig_quit = 0;
daemon_sig_usr1 = 0;
daemon_sig_usr2 = 0;
if ( getppid() != 1 )
{
sprintf(lockfile, "/var/lock/%s", src);
if ( lockfile[0] && open(lockfile, 194, 416LL) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(
3,
"Unable to create lock file %s, code=%d (%s) Check if daemon already exists!",
lockfile,
(unsigned int)*v5,
v6);
exit(1);
}
if ( !getuid() || !geteuid() )
{
v1 = getpwnam("root");
if ( v1 )
{
syslog(5, "Setting user to root");
setuid(v1->pw_uid);
}
}
signal(17, daemon_sig_handler);
signal(12, daemon_sig_handler);
signal(14, daemon_sig_handler);
signal(2, daemon_sig_handler);
signal(3, daemon_sig_handler);
signal(15, daemon_sig_handler);
signal(10, fork_sig_handler);
v2 = fork();
if ( v2 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "Unable to fork daemon, code=%d (%s)", (unsigned int)*v7, v8);
exit(1);
}
if ( v2 > 0 )
{
alarm(2u);
pause();
exit(1);
}
v3 = fork();
if ( v3 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "Unable to fork daemon, code=%d (%s)", (unsigned int)*v9, v10);
exit(1);
}
if ( v3 > 0 )
{
v11 = getppid();
kill(v11, 10);
alarm(2u);
pause();
exit(1);
}
v4 = getppid();
signal(17, 0LL);
signal(20, (void (*)(int))((char *)fork_sig_handler + 1));
signal(22, (void (*)(int))((char *)fork_sig_handler + 1));
signal(21, (void (*)(int))((char *)fork_sig_handler + 1));
signal(1, (void (*)(int))((char *)fork_sig_handler + 1));
signal(10, daemon_sig_handler);
umask(0);
if ( setsid() < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
syslog(3, "Unable to create new session, code=%d (%s)", (unsigned int)*v12, v13);
exit(1);
}
if ( chdir("/") < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
syslog(3, "Unable to change directory, code=%d (%s)", (unsigned int)*v14, v15);
exit(1);
}
sprintf(s, "/var/log/daemon/%s-output", src);
sprintf(filename, "/var/log/daemon/%s-error", src);
if ( !fopen(s, "w") || !fopen(filename, "w") )
{
v16 = _errno_location();
v17 = strerror(*v16);
syslog(3, "Unable to create log files, code=%d (%s)", (unsigned int)*v16, v17);
exit(1);
}
if ( !freopen(s, "w", stdout) || !freopen(filename, "w", stderr) )
{
v18 = _errno_location();
v19 = strerror(*v18);
syslog(3, "Unable to stream output, code=%d (%s)", (unsigned int)*v18, v19);
exit(1);
}
kill(v4, 10);
param.__sched_priority = 49;
if ( sched_setscheduler(0, 1, &param) == -1 )
{
fwrite("sched_setscheduler Failed!\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
if ( mlockall(3) == -1 )
{
fwrite("mlockall Failed!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
syslog(5, "Daemonization finished - Process Beginning");
}
}

canonical_decompiled_code:
void __fastcall func2(char *src)
{
struct passwd *v1; // rbp
__pid_t v2; // eax
__pid_t v3; // eax
__pid_t v4; // ebp
int *v5; // rbx
char *v6; // rax
int *v7; // rbx
char *v8; // rax
int *v9; // rbx
char *v10; // rax
__pid_t v11; // eax
int *v12; // rbx
char *v13; // rax
int *v14; // rbx
char *v15; // rax
int *v16; // rbx
char *v17; // rax
int *v18; // rbx
char *v19; // rax
struct sched_param param; // [rsp+Ch] [rbp-FCh] BYREF
char filename[112]; // [rsp+10h] [rbp-F8h] BYREF
char s[136]; // [rsp+80h] [rbp-88h] BYREF

strcpy(gdaemon_name, src);
syslog(5, "Starting daemonization for %s", src);
daemon_sig_quit = 0;
daemon_sig_usr1 = 0;
daemon_sig_usr2 = 0;
if ( getppid() != 1 )
{
sprintf(lockfile, "/var/lock/%s", src);
if ( lockfile[0] && open(lockfile, 194, 416LL) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(
3,
"Unable to create lock file %s, code=%d (%s) Check if daemon already exists!",
lockfile,
(unsigned int)*v5,
v6);
exit(1);
}
if ( !getuid() || !geteuid() )
{
v1 = getpwnam("root");
if ( v1 )
{
syslog(5, "Setting user to root");
setuid(v1->pw_uid);
}
}
signal(17, daemon_sig_handler);
signal(12, daemon_sig_handler);
signal(14, daemon_sig_handler);
signal(2, daemon_sig_handler);
signal(3, daemon_sig_handler);
signal(15, daemon_sig_handler);
signal(10, fork_sig_handler);
v2 = fork();
if ( v2 < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "Unable to fork daemon, code=%d (%s)", (unsigned int)*v7, v8);
exit(1);
}
if ( v2 > 0 )
{
alarm(2u);
pause();
exit(1);
}
v3 = fork();
if ( v3 < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "Unable to fork daemon, code=%d (%s)", (unsigned int)*v9, v10);
exit(1);
}
if ( v3 > 0 )
{
v11 = getppid();
kill(v11, 10);
alarm(2u);
pause();
exit(1);
}
v4 = getppid();
signal(17, 0LL);
signal(20, (void (*)(int))((char *)fork_sig_handler + 1));
signal(22, (void (*)(int))((char *)fork_sig_handler + 1));
signal(21, (void (*)(int))((char *)fork_sig_handler + 1));
signal(1, (void (*)(int))((char *)fork_sig_handler + 1));
signal(10, daemon_sig_handler);
umask(0);
if ( setsid() < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
syslog(3, "Unable to create new session, code=%d (%s)", (unsigned int)*v12, v13);
exit(1);
}
if ( chdir("/") < 0 )
{
v14 = _errno_location();
v15 = strerror(*v14);
syslog(3, "Unable to change directory, code=%d (%s)", (unsigned int)*v14, v15);
exit(1);
}
sprintf(s, "/var/log/daemon/%s-output", src);
sprintf(filename, "/var/log/daemon/%s-error", src);
if ( !fopen(s, "w") || !fopen(filename, "w") )
{
v16 = _errno_location();
v17 = strerror(*v16);
syslog(3, "Unable to create log files, code=%d (%s)", (unsigned int)*v16, v17);
exit(1);
}
if ( !freopen(s, "w", stdout) || !freopen(filename, "w", stderr) )
{
v18 = _errno_location();
v19 = strerror(*v18);
syslog(3, "Unable to stream output, code=%d (%s)", (unsigned int)*v18, v19);
exit(1);
}
kill(v4, 10);
param.__sched_priority = 49;
if ( sched_setscheduler(0, 1, &param) == -1 )
{
fwrite("sched_setscheduler Failed!\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
if ( mlockall(3) == -1 )
{
fwrite("mlockall Failed!\n", 1uLL, 0x11uLL, stderr);
exit(1);
}
syslog(5, "Daemonization finished - Process Beginning");
}
}

original_code:
void daemonize(const char *daemon_name)
{
    char pbuff[100];
    sprintf(gdaemon_name, "%s", daemon_name);
    syslog( 5, "Starting daemonization for %s", daemon_name );
    daemon_sig_quit = 0;
    daemon_sig_usr1 = 0;
    daemon_sig_usr2 = 0;
    pid_t pid, child, sid, parent;
    int lfp = -1;
    if( getppid() == 1 ) return;
    sprintf(lockfile, "/var/lock/%s", daemon_name);
    if( lockfile && lockfile[0] )
    {
        lfp = open(lockfile,02|0100|0200,0640);
        if( lfp < 0 )
        {
            syslog( 3, "Unable to create lock file %s, code=%d (%s)"
                             "  Check if daemon already exists!",
                    lockfile, (*__errno_location ()), strerror((*__errno_location ())) );
            exit( 1 );
        }
    }
    if( getuid()==0 || geteuid()==0 )
    {
        struct passwd *pw = getpwnam("root");
        if( pw )
        {
            syslog( 5, "Setting user to " "root" );
            setuid( pw->pw_uid );
        }
    }
    signal( 17, daemon_sig_handler );
    signal( 12, daemon_sig_handler );
    signal( 14, daemon_sig_handler );
    signal( 2, daemon_sig_handler );
    signal( 3, daemon_sig_handler );
    signal( 15, daemon_sig_handler );
    signal( 10, fork_sig_handler );
    child = fork();
    if( child<0 )
    {
        syslog( 3, "Unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( child>0 )
    {
        alarm(2);
        pause();
        exit( 1 );
    }
    pid = fork();
    if( pid<0 )
    {
        syslog( 3, "Unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( pid>0 )
    {
        parent = getppid();
        kill( parent, 10 );
        alarm(2);
        pause();
        exit( 1 );
    }
    parent = getppid();
    signal( 17, ((__sighandler_t) 0) );
    signal( 20, ((__sighandler_t) 1) );
    signal( 22, ((__sighandler_t) 1) );
    signal( 21, ((__sighandler_t) 1) );
    signal( 1, ((__sighandler_t) 1) );
    signal( 10, daemon_sig_handler );
    umask(0);
    sid = setsid();
    if( sid<0 )
    {
        syslog( 3, "Unable to create new session, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( (chdir("/")) < 0 )
    {
        syslog( 3, "Unable to change directory, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    char outfile[100];
    char errfile[100];
    sprintf(outfile, "/var/log/daemon/%s-output", daemon_name);
    sprintf(errfile, "/var/log/daemon/%s-error", daemon_name);
    if( !fopen( outfile, "w" ) ||
            !fopen( errfile, "w" ) )
    {
        syslog( 3, "Unable to create log files, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if( !freopen( outfile, "w", stdout ) ||
            !freopen( errfile, "w", stderr ) )
    {
        syslog( 3, "Unable to stream output, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    kill( parent, 10 );
    struct sched_param param;
    param.sched_priority = (49);
    if( sched_setscheduler(0, 1, &param) == -1 )
    {
        fprintf(stderr, "sched_setscheduler Failed!\n");
        exit( 1 );
    }
    if(mlockall(1|2) == -1)
    {
        fprintf(stderr, "mlockall Failed!\n");
        exit( 1 );
    }
    stack_prefault();
    syslog( 5, "Daemonization finished - Process Beginning" );
}

canonical_original_code:
void daemonize(char *daemon_name)
{
    char pbuff[100];
    sprintf(gdaemon_name, "%s", daemon_name);
    syslog( 5, "Starting daemonization for %s", daemon_name );
    daemon_sig_quit = 0;
    daemon_sig_usr1 = 0;
    daemon_sig_usr2 = 0;
    int pid; int child; int sid; int parent;
    int lfp = -1;
    if( getppid() == 1 ) return;
    sprintf(lockfile, "/var/lock/%s", daemon_name);
    if( lockfile && lockfile[0] )
    {
        lfp = open(lockfile,02|0100|0200,0640);
        if( lfp < 0 )
        {
            syslog( 3, "Unable to create lock file %s, code=%d (%s)"
                             "  Check if daemon already exists!",
                    lockfile, (*__errno_location ()), strerror((*__errno_location ())) );
            exit( 1 );
        }
    }
    if( getuid()==0 || geteuid()==0 )
    {
        struct passwd *pw = getpwnam("root");
        if( pw )
        {
            syslog( 5, "Setting user to " "root" );
            setuid( pw->pw_uid );
        }
    }
    signal( 17, daemon_sig_handler );
    signal( 12, daemon_sig_handler );
    signal( 14, daemon_sig_handler );
    signal( 2, daemon_sig_handler );
    signal( 3, daemon_sig_handler );
    signal( 15, daemon_sig_handler );
    signal( 10, fork_sig_handler );
    child = fork();
    if( child<0 )
    {
        syslog( 3, "Unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( child>0 )
    {
        alarm(2);
        pause();
        exit( 1 );
    }
    pid = fork();
    if( pid<0 )
    {
        syslog( 3, "Unable to fork daemon, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( pid>0 )
    {
        parent = getppid();
        kill( parent, 10 );
        alarm(2);
        pause();
        exit( 1 );
    }
    parent = getppid();
    signal( 17, ((void (*)(int)) 0) );
    signal( 20, ((void (*)(int)) 1) );
    signal( 22, ((void (*)(int)) 1) );
    signal( 21, ((void (*)(int)) 1) );
    signal( 1, ((void (*)(int)) 1) );
    signal( 10, daemon_sig_handler );
    umask(0);
    sid = setsid();
    if( sid<0 )
    {
        syslog( 3, "Unable to create new session, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    if( (chdir("/")) < 0 )
    {
        syslog( 3, "Unable to change directory, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    char outfile[100];
    char errfile[100];
    sprintf(outfile, "/var/log/daemon/%s-output", daemon_name);
    sprintf(errfile, "/var/log/daemon/%s-error", daemon_name);
    if( !fopen( outfile, "w" ) ||
            !fopen( errfile, "w" ) )
    {
        syslog( 3, "Unable to create log files, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit(1);
    }
    if( !freopen( outfile, "w", stdout ) ||
            !freopen( errfile, "w", stderr ) )
    {
        syslog( 3, "Unable to stream output, code=%d (%s)",
                (*__errno_location ()), strerror((*__errno_location ())) );
        exit( 1 );
    }
    kill( parent, 10 );
    struct sched_param param;
    param.sched_priority = (49);
    if( sched_setscheduler(0, 1, &param) == -1 )
    {
        fprintf(stderr, "sched_setscheduler Failed!\n");
        exit( 1 );
    }
    if(mlockall(1|2) == -1)
    {
        fprintf(stderr, "mlockall Failed!\n");
        exit( 1 );
    }
    stack_prefault();
    syslog( 5, "Daemonization finished - Process Beginning" );
}

variable_types:
pbuff
char [100]

variable_types: pid int

variable_types: child int

variable_types: sid int

variable_types: parent int

variable_types: lfp int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
outfile
char [100]

variable_types:
errfile
char [100]

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
void daemon_close()
{
remove(lockfile);
fclose(stdout);
fclose(stderr);
syslog(5, "Terminated process %s gracefully", gdaemon_name);
closelog();
}

canonical_decompiled_code:
void func3()
{
remove(lockfile);
fclose(stdout);
fclose(stderr);
syslog(5, "Terminated process %s gracefully", gdaemon_name);
closelog();
}

original_code:
void daemon_close()
{
    remove( lockfile );
    fclose( stdout );
    fclose( stderr );
    char buff[100];
    syslog( 5, "Terminated process %s gracefully", gdaemon_name );
    closelog();
}

variable_types:
buff
char [100]


repo: sballesteros/ssm

decompiled_code:
__int64 __fastcall ssm_zmq_send_par(__int64 a1, _QWORD *a2, unsigned int a3)
{
return zmq_send(a1, a2[2], 8LL * *a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _QWORD *a2, unsigned int a3)
{
return zmq_send(a1, a2[2], 8LL * *a2, a3);
}

original_code:
void ssm_zmq_send_par(void *socket, ssm_par_t *par, int zmq_options)
{
    zmq_send(socket, par->data, par->size * sizeof (double), zmq_options);
}

canonical_original_code:
void ssm_zmq_send_par(void *socket, struct gsl_vector *par, int zmq_options)
{
    zmq_send(socket, par->data, par->size * sizeof (double), zmq_options);
}

variable_types:
socket
void *

variable_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

user_defined_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; }


decompiled_code:
__int64 __fastcall ssm_zmq_recv_par(_QWORD *a1, __int64 a2)
{
return zmq_recv(a2, a1[2], 8LL * *a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2)
{
return zmq_recv(a2, a1[2], 8LL * *a1, 0LL);
}

original_code:
void ssm_zmq_recv_par(ssm_par_t *par, void *socket)
{
    zmq_recv(socket, par->data, par->size * sizeof (double), 0);
}

canonical_original_code:
void ssm_zmq_recv_par(struct gsl_vector *par, void *socket)
{
    zmq_recv(socket, par->data, par->size * sizeof (double), 0);
}

user_defined_types: struct gsl_block_struct { unsigned long size; double *data; }


decompiled_code:
__int64 __fastcall ssm_zmq_send_X(__int64 a1, int *a2, unsigned int a3)
{
zmq_send(a1, a2 + 4, 8LL, 2LL);
return zmq_send(a1, *((_QWORD *)a2 + 1), 8LL * *a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int *a2, unsigned int a3)
{
zmq_send(a1, a2 + 4, 8LL, 2LL);
return zmq_send(a1, *((_QWORD *)a2 + 1), 8LL * *a2, a3);
}

original_code:
void ssm_zmq_send_X(void *socket, ssm_X_t *X, int zmq_options)
{
    zmq_send(socket, &(X->dt), sizeof (double), 2);
    zmq_send(socket, X->proj, X->length * sizeof (double), zmq_options);
}

canonical_original_code:
void ssm_zmq_send_X(void *socket, struct ssm_X_t *X, int zmq_options)
{
    zmq_send(socket, &(X->dt), sizeof (double), 2);
    zmq_send(socket, X->proj, X->length * sizeof (double), zmq_options);
}

variable_types: struct ssm_X_t { int length; double *proj; double dt; double dt0; } *


decompiled_code:
__int64 __fastcall ssm_zmq_recv_X(int *a1, __int64 a2)
{
zmq_recv(a2, a1 + 4, 8LL, 0LL);
return zmq_recv(a2, *((_QWORD *)a1 + 1), 8LL * *a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, __int64 a2)
{
zmq_recv(a2, a1 + 4, 8LL, 0LL);
return zmq_recv(a2, *((_QWORD *)a1 + 1), 8LL * *a1, 0LL);
}

original_code:
void ssm_zmq_recv_X(ssm_X_t *X, void *socket)
{
    zmq_recv(socket, &(X->dt), sizeof (double), 0);
    zmq_recv(socket, X->proj, X->length * sizeof (double), 0);
}

canonical_original_code:
void ssm_zmq_recv_X(struct ssm_X_t *X, void *socket)
{
    zmq_recv(socket, &(X->dt), sizeof (double), 0);
    zmq_recv(socket, X->proj, X->length * sizeof (double), 0);
}

user_defined_types: struct ssm_X_t { int length; double *proj; double dt; double dt0; }


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

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

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

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

original_code:
void ssm_d2_free(double **tab, int n)
{
    int i;
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
double **

variable_types: n int

variable_types: i int


original_code:
void ssm_d3_free(double ***tab, int n, int p1)
{
    int i, j;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_d3_free(double ***tab, int n, int p1)
{
    int i; int j;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
double ***

variable_types: j int


original_code:
void ssm_d4_free(double ****tab, int n, int p1, int p2)
{
    int i, j, k;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            for(k=0; k<p2; k++)
                free(tab[i][j][k]);
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_d4_free(double ****tab, int n, int p1, int p2)
{
    int i; int j; int k;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            for(k=0; k<p2; k++)
                free(tab[i][j][k]);
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
double ****

variable_types: k int


original_code:
void ssm_d3_var_free(double ***tab, int n, unsigned int *p1)
{
    int i, j;
    for(i=0; i<n; i++)
        for(j=0; j<p1[i]; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_d3_var_free(double ***tab, int n, unsigned int *p1)
{
    int i; int j;
    for(i=0; i<n; i++)
        for(j=0; j<p1[i]; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}


decompiled_code:
void ssm_f_id()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
double ssm_f_id(double x)
{
    return x;
}

canonical_original_code:
double ssm_f_id(double x)
{
    return x;
}

variable_types: x double


decompiled_code:
double __fastcall ssm_f_log(double a1)
{
if ( a1 <= 1.0e-17 )
a1 = LC0;
return log(a1);
}

canonical_decompiled_code:
double __fastcall func1(double a1)
{
if ( a1 <= 1.0e-17 )
a1 = LC0;
return log(a1);
}

original_code:
double ssm_f_log(double x)
{
    double safe = ( x > 1e-17 ) ? x : 1e-17;
    return log(safe);
}

variable_types: safe double


decompiled_code:
double __fastcall ssm_f_inv_log(double a1)
{
return exp(a1);
}

canonical_decompiled_code:
double __fastcall func2(double a1)
{
return exp(a1);
}

original_code:
double ssm_f_inv_log(double x)
{
    return exp(x);
}


original_code:
double ssm_f_logit(double x)
{
    double safe = ( x > 1e-17 ) ? x : 1e-17;
    safe = (safe < 0.999999999 ) ? safe : 0.999999999;
    return log(safe/(1.0-safe));
}


decompiled_code:
double __fastcall ssm_f_logit_ab(double result, double a2, double a3)
{
double v3; // xmm3_8
double v4; // xmm0_8

if ( a2 != a3 )
{
v3 = (result - a2) / (a3 - result);
v4 = v3;
if ( v3 < 1.0e-17 )
{
v4 = LC0;
}
else if ( v3 > 1.0e17 )
{
v4 = *(double *)&LC5;
}
result = log(v4);
}
return result;
}

canonical_decompiled_code:
double __fastcall func5(double result, double a2, double a3)
{
double v3; // xmm3_8
double v4; // xmm0_8

if ( a2 != a3 )
{
v3 = (result - a2) / (a3 - result);
v4 = v3;
if ( v3 < 1.0e-17 )
{
v4 = LC0;
}
else if ( v3 > 1.0e17 )
{
v4 = *(double *)&LC5;
}
result = log(v4);
}
return result;
}

original_code:
double ssm_f_logit_ab(double x, double a, double b)
{
    if (a == b)
        return x;
    else{
        double ratio = (x-a)/(b-x);
        if(ratio < 1e-17){
            ratio = 1e-17;
        } else if(ratio > (1.0/1e-17)) {
            ratio = 1.0/1e-17;
        }
        return log(ratio);
    }
}

variable_types: a double

variable_types: b double

variable_types: ratio double


decompiled_code:
double __fastcall ssm_f_inv_logit_ab(double result, double a2, double a3)
{
double v3; // rbx
double v4; // r14
double v5; // r14

v3 = result;
if ( a2 != a3 )
{
if ( result < 0.0 )
{
v5 = exp(result);
result = (v5 * a3 + a2) / (exp(v3) + 1.0);
}
else
{
v4 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
result = (a2 * v4 + a3) / (exp(COERCE_DOUBLE(*(_QWORD *)&v3 ^ LC4)) + 1.0);
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func6(double result, double a2, double a3)
{
double v3; // rbx
double v4; // r14
double v5; // r14

v3 = result;
if ( a2 != a3 )
{
if ( result < 0.0 )
{
v5 = exp(result);
result = (v5 * a3 + a2) / (exp(v3) + 1.0);
}
else
{
v4 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
result = (a2 * v4 + a3) / (exp(COERCE_DOUBLE(*(_QWORD *)&v3 ^ LC4)) + 1.0);
}
}
return result;
}

original_code:
double ssm_f_inv_logit_ab(double x, double a, double b)
{
    if (a == b) {
        return x ;
    } else {
        if (x < 0) {
            return (b*exp(x)+a)/(1.0+exp(x));
        } else {
            return (b+a*exp(-x))/(1.0+exp(-x));
        }
    }
}


decompiled_code:
double ssm_f_der_id()
{
return *(double *)&LC2;
}

canonical_decompiled_code:
double func7()
{
return *(double *)&LC2;
}

original_code:
double ssm_f_der_id(double x)
{
    return 1;
}


decompiled_code:
double __fastcall ssm_f_der_log(double a1)
{
return 1.0 / a1;
}

canonical_decompiled_code:
double __fastcall func8(double a1)
{
return 1.0 / a1;
}

original_code:
double ssm_f_der_log(double x)
{
    return 1.0/x;
}


decompiled_code:
double __fastcall ssm_f_der_inv_log(double a1)
{
return exp(a1);
}

canonical_decompiled_code:
double __fastcall func9(double a1)
{
return exp(a1);
}

original_code:
double ssm_f_der_inv_log(double x)
{
    return exp(x);
}


decompiled_code:
double ssm_f_der2_inv_id()
{
return 0.0;
}

canonical_decompiled_code:
double func10()
{
return 0.0;
}

original_code:
double ssm_f_der2_inv_id(double x)
{
    return 0;
}


decompiled_code:
double __fastcall ssm_f_der2_inv_log(double a1)
{
return exp(a1);
}

canonical_decompiled_code:
double __fastcall func11(double a1)
{
return exp(a1);
}

original_code:
double ssm_f_der2_inv_log(double x)
{
    return exp(x);
}


decompiled_code:
double __fastcall ssm_f_der2_inv_logit(double a1)
{
double v2; // xmm2_8
double v3; // r15
double v4; // rbx
double v5; // xmm0_8
double result; // xmm0_8
double v7; // r15
double v8; // r14
double v9; // xmm0_8

if ( a1 <= 0.0 )
{
v7 = exp(a1);
v8 = exp(a1);
v9 = exp(a1);
result = (1.0 - v8) * v7 / pow(v9 + 1.0, 3.0);
}
else
{
*(_QWORD *)&v2 = *(_QWORD *)&a1 ^ LC4;
v3 = exp(COERCE_DOUBLE(*(_QWORD *)&a1 ^ LC4));
v4 = exp(v2);
v5 = exp(v2);
result = (v4 - 1.0) * v3 / pow(v5 + 1.0, 3.0);
}
return result;
}

canonical_decompiled_code:
double __fastcall func14(double a1)
{
double v2; // xmm2_8
double v3; // r15
double v4; // rbx
double v5; // xmm0_8
double result; // xmm0_8
double v7; // r15
double v8; // r14
double v9; // xmm0_8

if ( a1 <= 0.0 )
{
v7 = exp(a1);
v8 = exp(a1);
v9 = exp(a1);
result = (1.0 - v8) * v7 / pow(v9 + 1.0, 3.0);
}
else
{
*(_QWORD *)&v2 = *(_QWORD *)&a1 ^ LC4;
v3 = exp(COERCE_DOUBLE(*(_QWORD *)&a1 ^ LC4));
v4 = exp(v2);
v5 = exp(v2);
result = (v4 - 1.0) * v3 / pow(v5 + 1.0, 3.0);
}
return result;
}

original_code:
double ssm_f_der2_inv_logit(double x)
{
    if (x > 0) {
        return exp(-x)*(exp(-x)-1)/pow(1.0 + exp(-x), 3.0);
    } else {
        return exp(x)*(1-exp(x))/pow(1.0 + exp(x), 3.0);
    }
}


original_code:
double ssm_f_der_logit_ab(double x, double a, double b)
{
    if (a == b) {
        return x ;
    } else {
        return (b-a)/((x-a)*(b-x));
    }
}


decompiled_code:
double __fastcall ssm_f_der_inv_logit_ab(double result, double a2, double a3)
{
double v3; // rbx
double v5; // xmm6_8
double v6; // r14
double v7; // xmm1_8
double v8; // r14
double v9; // xmm1_8

v3 = result;
if ( a2 != a3 )
{
if ( result <= 0.0 )
{
v8 = exp(result);
v9 = exp(v3) + 1.0;
result = (a3 - a2) * v8 / (v9 * v9);
}
else
{
*(_QWORD *)&v5 = *(_QWORD *)&result ^ LC4;
v6 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
v7 = exp(v5) + 1.0;
result = (a3 - a2) * v6 / (v7 * v7);
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func16(double result, double a2, double a3)
{
double v3; // rbx
double v5; // xmm6_8
double v6; // r14
double v7; // xmm1_8
double v8; // r14
double v9; // xmm1_8

v3 = result;
if ( a2 != a3 )
{
if ( result <= 0.0 )
{
v8 = exp(result);
v9 = exp(v3) + 1.0;
result = (a3 - a2) * v8 / (v9 * v9);
}
else
{
*(_QWORD *)&v5 = *(_QWORD *)&result ^ LC4;
v6 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
v7 = exp(v5) + 1.0;
result = (a3 - a2) * v6 / (v7 * v7);
}
}
return result;
}

original_code:
double ssm_f_der_inv_logit_ab(double x, double a, double b)
{
    if (a == b) {
        return x ;
    } else {
        if (x > 0) {
            return (b-a)*exp(-x)/pow(exp(-x) + 1.0, 2.0);
        } else {
            return (b-a)*exp(x)/pow(exp(x) + 1.0, 2.0);
        }
    }
}


decompiled_code:
double __fastcall ssm_f_der2_inv_logit_ab(double result, double a2, double a3)
{
double v3; // rbx
double v4; // xmm6_8
double v5; // r14
double v6; // rbp
double v7; // xmm0_8
double v8; // r14
double v9; // rbp
double v10; // xmm0_8

v3 = result;
if ( a2 != a3 )
{
if ( result <= 0.0 )
{
v8 = exp(result);
v9 = exp(v3);
v10 = exp(v3);
result = (a3 - a2) * v8 * (1.0 - v9) / pow(v10 + 1.0, 3.0);
}
else
{
*(_QWORD *)&v4 = *(_QWORD *)&result ^ LC4;
v5 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
v6 = exp(v4);
v7 = exp(v4);
result = (a3 - a2) * v5 * (v6 - 1.0) / pow(v7 + 1.0, 3.0);
}
}
return result;
}

canonical_decompiled_code:
double __fastcall func17(double result, double a2, double a3)
{
double v3; // rbx
double v4; // xmm6_8
double v5; // r14
double v6; // rbp
double v7; // xmm0_8
double v8; // r14
double v9; // rbp
double v10; // xmm0_8

v3 = result;
if ( a2 != a3 )
{
if ( result <= 0.0 )
{
v8 = exp(result);
v9 = exp(v3);
v10 = exp(v3);
result = (a3 - a2) * v8 * (1.0 - v9) / pow(v10 + 1.0, 3.0);
}
else
{
*(_QWORD *)&v4 = *(_QWORD *)&result ^ LC4;
v5 = exp(COERCE_DOUBLE(*(_QWORD *)&result ^ LC4));
v6 = exp(v4);
v7 = exp(v4);
result = (a3 - a2) * v5 * (v6 - 1.0) / pow(v7 + 1.0, 3.0);
}
}
return result;
}

original_code:
double ssm_f_der2_inv_logit_ab(double x, double a, double b)
{
    if (a == b) {
        return x ;
    } else {
        if (x > 0) {
            return (b-a)*exp(-x)*(exp(-x)-1)/pow(1.0 + exp(-x), 3.0);
        } else {
            return (b-a)*exp(x)*(1-exp(x))/pow(1.0 + exp(x), 3.0);
        }
    }
}


decompiled_code:
__int64 __fastcall ssm_rmvnorm(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, double a6)
{
__int64 v9; // r14
__int64 i; // rbx

v9 = gsl_matrix_alloc(a2, a2);
gsl_matrix_memcpy(v9, a4);
gsl_matrix_scale(v9, a6 * a6);
gsl_linalg_cholesky_decomp(v9);
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; gsl_vector_set(a5, i++) )
gsl_ran_ugaussian(a1);
}
gsl_blas_dtrmv(122LL, 111LL, 131LL, v9, a5);
gsl_vector_add(a5, a3);
gsl_matrix_free(v9);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, __int64 a3, __int64 a4, __int64 a5, double a6)
{
__int64 v9; // r14
__int64 i; // rbx

v9 = gsl_matrix_alloc(a2, a2);
gsl_matrix_memcpy(v9, a4);
gsl_matrix_scale(v9, a6 * a6);
gsl_linalg_cholesky_decomp(v9);
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; gsl_vector_set(a5, i++) )
gsl_ran_ugaussian(a1);
}
gsl_blas_dtrmv(122LL, 111LL, 131LL, v9, a5);
gsl_vector_add(a5, a3);
gsl_matrix_free(v9);
return 0LL;
}

original_code:
int ssm_rmvnorm(const gsl_rng *r, const int n, const gsl_vector *mean, const gsl_matrix *var, double sd_fac, gsl_vector *result)
{
    int k;
    gsl_matrix *work = gsl_matrix_alloc(n,n);
    gsl_matrix_memcpy(work,var);
    gsl_matrix_scale(work, sd_fac*sd_fac);
    gsl_linalg_cholesky_decomp(work);
    for(k=0; k<n; k++)
        gsl_vector_set( result, k, gsl_ran_ugaussian(r) );
    gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, work, result);
    gsl_vector_add(result,mean);
    gsl_matrix_free(work);
    return 0;
}

canonical_original_code:
int ssm_rmvnorm(struct gsl_rng *r, int n, struct gsl_vector *mean, struct gsl_matrix *var, double sd_fac, struct gsl_vector *result)
{
    int k;
    struct gsl_matrix *work = gsl_matrix_alloc(n,n);
    gsl_matrix_memcpy(work,var);
    gsl_matrix_scale(work, sd_fac*sd_fac);
    gsl_linalg_cholesky_decomp(work);
    for(k=0; k<n; k++)
        gsl_vector_set( result, k, gsl_ran_ugaussian(r) );
    gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, work, result);
    gsl_vector_add(result,mean);
    gsl_matrix_free(work);
    return 0;
}

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: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct *block; int owner; } *

variable_types:
result
struct gsl_vector *

variable_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

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


original_code:
void ssm_adapt_var(ssm_adapt_t *adapt, ssm_theta_t *x, int m)
{
    int i, k;
    double *x_bar = adapt->mean_sampling;
    gsl_matrix *cov = adapt->var_sampling;
    double dm = (double) m;
    double val;
    for (i=0; i < x->size; i++) {
        for (k=0; k <= i; k++) {
            val = ((dm - 1.0) / dm) * (gsl_vector_get(x, i) - x_bar[i]) * (gsl_vector_get(x, k) - x_bar[k]);
            gsl_matrix_set(cov, i, k, (((dm-1.0)*gsl_matrix_get(cov, i, k)) + val ) / dm);
        }
    }
    for (i=0; i < x->size; i++) {
        for (k=0; k < i; k++) {
            val = gsl_matrix_get(cov, i, k);
            gsl_matrix_set(cov, k, i, val);
        }
    }
    for (i=0; i < x->size; i++) {
        x_bar[i] += (gsl_vector_get(x, i) - x_bar[i]) / dm;
    }
}

canonical_original_code:
void ssm_adapt_var(struct ssm_adapt_t *adapt, struct gsl_vector *x, int m)
{
    int i; int k;
    double *x_bar = adapt->mean_sampling;
    struct gsl_matrix *cov = adapt->var_sampling;
    double dm = (double) m;
    double val;
    for (i=0; i < x->size; i++) {
        for (k=0; k <= i; k++) {
            val = ((dm - 1.0) / dm) * (gsl_vector_get(x, i) - x_bar[i]) * (gsl_vector_get(x, k) - x_bar[k]);
            gsl_matrix_set(cov, i, k, (((dm-1.0)*gsl_matrix_get(cov, i, k)) + val ) / dm);
        }
    }
    for (i=0; i < x->size; i++) {
        for (k=0; k < i; k++) {
            val = gsl_matrix_get(cov, i, k);
            gsl_matrix_set(cov, k, i, val);
        }
    }
    for (i=0; i < x->size; i++) {
        x_bar[i] += (gsl_vector_get(x, i) - x_bar[i]) / dm;
    }
}

variable_types: struct ssm_adapt_t { double ar; double ar_smoothed; double eps; double eps_a; double eps_max; int eps_switch; int m_switch; int flag_smooth; double alpha; double *mean_sampling; struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *var_sampling; } *

variable_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; } *

variable_types: m int

variable_types: dm double

variable_types: val double

user_defined_types: struct ssm_adapt_t { double ar; double ar_smoothed; double eps; double eps_a; double eps_max; int eps_switch; int m_switch; int flag_smooth; double alpha; double *mean_sampling; struct gsl_matrix *var_sampling; }

user_defined_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct *block; int owner; }


decompiled_code:
void *__fastcall ssm_X_copy(__int64 a1, int *a2)
{
void *result; // rax

result = memcpy(*(void **)(a1 + 8), *((const void **)a2 + 1), 8LL * *a2);
*(_QWORD *)(a1 + 16) = *((_QWORD *)a2 + 2);
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1, int *a2)
{
void *result; // rax

result = memcpy(*(void **)(a1 + 8), *((const void **)a2 + 1), 8LL * *a2);
*(_QWORD *)(a1 + 16) = *((_QWORD *)a2 + 2);
return result;
}

original_code:
void ssm_X_copy(ssm_X_t *dest, ssm_X_t *src)
{
    memcpy(dest->proj, src->proj, src->length * sizeof(double));
    dest->dt = src->dt;
}

canonical_original_code:
void ssm_X_copy(struct ssm_X_t *dest, struct ssm_X_t *src)
{
    memcpy(dest->proj, src->proj, src->length * sizeof(double));
    dest->dt = src->dt;
}

variable_types: struct ssm_X_t { int length; double *proj; double dt; double dt0; } *

variable_types:
src
struct ssm_X_t *


original_code:
void ssm_ran_multinomial (const gsl_rng * r, const size_t K, unsigned int N, const double p[], unsigned int n[])
{
    size_t k;
    double sum_p = 0.0;
    unsigned int sum_n = 0;
    for (k = 0; k < K; k++) {
        if (p[k] > 0.0) {
            n[k] = gsl_ran_binomial (r, p[k] / (1.0 - sum_p), N - sum_n);
        }
        else {
            n[k] = 0;
        }
        sum_p += p[k];
        sum_n += n[k];
    }
}

canonical_original_code:
void ssm_ran_multinomial(struct gsl_rng *r, unsigned long K, unsigned int N, double p[], unsigned int n[])
{
    unsigned long k;
    double sum_p = 0.0;
    unsigned int sum_n = 0;
    for (k = 0; k < K; k++) {
        if (p[k] > 0.0) {
            n[k] = gsl_ran_binomial (r, p[k] / (1.0 - sum_p), N - sum_n);
        }
        else {
            n[k] = 0;
        }
        sum_p += p[k];
        sum_n += n[k];
    }
}

variable_types:
K
unsigned long

variable_types:
N
unsigned int

variable_types:
p
double []

variable_types:
n
unsigned int []

variable_types:
k
unsigned long

user_defined_types: 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); }


decompiled_code:
double __fastcall ssm_correct_rate(double a1, double a2)
{
return COERCE_DOUBLE(COERCE_UNSIGNED_INT64(log(1.0 - a1 * a2)) ^ LC2) / a2;
}

canonical_decompiled_code:
double __fastcall func3(double a1, double a2)
{
return COERCE_DOUBLE(COERCE_UNSIGNED_INT64(log(1.0 - a1 * a2)) ^ LC2) / a2;
}

original_code:
double ssm_correct_rate(double rate, double dt)
{
    return -log(1.0-rate*dt)/dt;
}

variable_types: rate double

variable_types: dt double


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

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

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

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

original_code:
void ssm_u2_free(unsigned int **tab, int n)
{
    int i;
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
unsigned int **

variable_types: n int

variable_types: i int


original_code:
void ssm_u3_free(unsigned int ***tab, int n, int p1)
{
    int i, j;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_u3_free(unsigned int ***tab, int n, int p1)
{
    int i; int j;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
unsigned int ***

variable_types: j int


original_code:
void ssm_u4_free(unsigned int ****tab, int n, int p1, int p2)
{
    int i, j, k;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            for(k=0; k<p2; k++)
                free(tab[i][j][k]);
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_u4_free(unsigned int ****tab, int n, int p1, int p2)
{
    int i; int j; int k;
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            for(k=0; k<p2; k++)
                free(tab[i][j][k]);
    for(i=0; i<n; i++)
        for(j=0; j<p1; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
unsigned int ****

variable_types: k int


original_code:
void ssm_u3_var_free(unsigned int ***tab, int n, unsigned int *p1)
{
    int i, j;
    for(i=0; i<n; i++)
        for(j=0; j<p1[i]; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

canonical_original_code:
void ssm_u3_var_free(unsigned int ***tab, int n, unsigned int *p1)
{
    int i; int j;
    for(i=0; i<n; i++)
        for(j=0; j<p1[i]; j++)
            free(tab[i][j]);
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}


decompiled_code:
void __fastcall ssm_dic_init(double *a1, double a2, double a3)
{
double v3; // xmm0_8

a1[16] = a2;
a1[17] = a3 + a2;
v3 = a2 * -2.0;
a1[20] = v3;
a1[21] = v3;
}

original_code:
void ssm_dic_init(ssm_fitness_t *fitness, double log_like, double log_prior)
{
    fitness->summary_log_likelihood = log_like;
    fitness->summary_log_ltp = log_like + log_prior;
    fitness->_min_deviance = -2*log_like;
    fitness->_deviance_cum = -2*log_like;
}

canonical_original_code:
void ssm_dic_init(struct ssm_fitness_t *fitness, double log_like, double log_prior)
{
    fitness->summary_log_likelihood = log_like;
    fitness->summary_log_ltp = log_like + log_prior;
    fitness->_min_deviance = -2*log_like;
    fitness->_deviance_cum = -2*log_like;
}

variable_types: struct ssm_fitness_t { int J; int data_length; double like_min; double log_like_min; double ess_n; double log_like_n; double log_like; double *weights; unsigned int **select; enum ssm_err_code_t { SSM_SUCCESS=expr, SSM_ERR_LIKE=expr, SSM_ERR_REM_SV=expr, SSM_ERR_PRED=expr, SSM_ERR_KAL=expr, SSM_ERR_IC=expr, SSM_MH_REJECT=expr, SSM_ERR_PROPOSAL=expr, SSM_ERR_PRIOR=expr } *cum_status; int n_all_fail; double log_like_prev; double log_prior; double log_prior_prev; double AIC; double AICc; double DIC; double summary_log_likelihood; double summary_log_ltp; double summary_sum_squares; int n; double _min_deviance; double _deviance_cum; } *

user_defined_types: struct ssm_fitness_t { int J; int data_length; double like_min; double log_like_min; double ess_n; double log_like_n; double log_like; double *weights; unsigned int **select; enum ssm_err_code_t *cum_status; int n_all_fail; double log_like_prev; double log_prior; double log_prior_prev; double AIC; double AICc; double DIC; double summary_log_likelihood; double summary_log_ltp; double summary_sum_squares; int n; double _min_deviance; double _deviance_cum; }


original_code:
void ssm_dic_update(ssm_fitness_t *fitness, double log_like, double log_prior)
{
    if( log_like > fitness->summary_log_likelihood){
 fitness->summary_log_likelihood = log_like;
    }
    if( (log_like+log_prior) > fitness->summary_log_ltp){
 fitness->summary_log_ltp = (log_like+log_prior);
    }
    double deviance = -2*log_like;
    fitness->_deviance_cum += deviance;
    if( deviance < fitness->_min_deviance){
 fitness->_min_deviance = deviance;
    }
}

canonical_original_code:
void ssm_dic_update(struct ssm_fitness_t *fitness, double log_like, double log_prior)
{
    if( log_like > fitness->summary_log_likelihood){
 fitness->summary_log_likelihood = log_like;
    }
    if( (log_like+log_prior) > fitness->summary_log_ltp){
 fitness->summary_log_ltp = (log_like+log_prior);
    }
    double deviance = -2*log_like;
    fitness->_deviance_cum += deviance;
    if( deviance < fitness->_min_deviance){
 fitness->_min_deviance = deviance;
    }
}

variable_types: deviance double

user_defined_types: enum ssm_err_code_t { SSM_SUCCESS=expr, SSM_ERR_LIKE=expr, SSM_ERR_REM_SV=expr, SSM_ERR_PRED=expr, SSM_ERR_KAL=expr, SSM_ERR_IC=expr, SSM_MH_REJECT=expr, SSM_ERR_PROPOSAL=expr, SSM_ERR_PRIOR=expr }


decompiled_code:
void *__fastcall ssm_c1_new(int a1)
{
void *result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

result = malloc(a1);
if ( !result )
{
sprintf(v2, "Allocation impossible in file :%s line : %d", "core/alloc_c.c", 28LL);
ssm_print_err(v2);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
void *result; // rax
char v2[264]; // [rsp+0h] [rbp-108h] BYREF

result = malloc(a1);
if ( !result )
{
sprintf(v2, "Allocation impossible in file :%s line : %d", "core/alloc_c.c", 28LL);
ssm_print_err(v2);
exit(1);
}
return result;
}

original_code:
char *ssm_c1_new(int n)
{
    char *tab = malloc(n*sizeof (char));
    if(tab==((void *)0))
    {
        char str[255];
        sprintf(str, "Allocation impossible in file :%s line : %d","core/alloc_c.c",28);
        ssm_print_err(str);
        exit(1);
    }
    return tab;
}

variable_types:
tab
char *

variable_types:
str
char [255]

return_type: char *


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

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

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

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

original_code:
void ssm_c2_free(char **tab, int n)
{
    int i;
    for(i=0; i<n; i++)
        free(tab[i]);
    free(tab);
}

variable_types:
tab
char **


original_code:
void ssm_adapt_ar(ssm_adapt_t *a, int is_accepted, int m)
{
    a->ar_smoothed = (1.0 - a->alpha) * a->ar_smoothed + a->alpha * is_accepted;
    a->ar += ( ((double) is_accepted - a->ar) / ((double) (m + 1)) );
}

canonical_original_code:
void ssm_adapt_ar(struct ssm_adapt_t *a, int is_accepted, int m)
{
    a->ar_smoothed = (1.0 - a->alpha) * a->ar_smoothed + a->alpha * is_accepted;
    a->ar += ( ((double) is_accepted - a->ar) / ((double) (m + 1)) );
}

variable_types: struct ssm_adapt_t { double ar; double ar_smoothed; double eps; double eps_a; double eps_max; int eps_switch; int m_switch; int flag_smooth; double alpha; double *mean_sampling; struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *var_sampling; } *

variable_types: m int


decompiled_code:
__int64 ssm_theta_copy()
{
return gsl_vector_memcpy();
}

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

original_code:
int ssm_theta_copy(ssm_theta_t *dest, ssm_theta_t *src)
{
    return gsl_vector_memcpy(dest, src);
}

canonical_original_code:
int ssm_theta_copy(struct gsl_vector *dest, struct gsl_vector *src)
{
    return gsl_vector_memcpy(dest, src);
}

variable_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

variable_types:
src
struct gsl_vector *


decompiled_code:
__int64 ssm_par_copy()
{
return gsl_vector_memcpy();
}

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

original_code:
int ssm_par_copy(ssm_par_t *dest, ssm_par_t *src)
{
    return gsl_vector_memcpy(dest, src);
}

canonical_original_code:
int ssm_par_copy(struct gsl_vector *dest, struct gsl_vector *src)
{
    return gsl_vector_memcpy(dest, src);
}


decompiled_code:
__int64 __fastcall ssm_in_jarray(__int64 a1, const char *a2)
{
unsigned __int64 i; // rbx
__int64 v3; // rax
const char *v4; // rax

for ( i = 0LL; json_array_size(a1) > i; ++i )
{
v3 = json_array_get(a1, i);
v4 = (const char *)json_string_value(v3);
if ( !strcmp(v4, a2) )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
unsigned __int64 i; // rbx
__int64 v3; // rax
const char *v4; // rax

for ( i = 0LL; json_array_size(a1) > i; ++i )
{
v3 = json_array_get(a1, i);
v4 = (const char *)json_string_value(v3);
if ( !strcmp(v4, a2) )
return 1LL;
}
return 0LL;
}

original_code:
int ssm_in_jarray(json_t *jarray, const char *name)
{
    int i;
    for(i=0; i<json_array_size(jarray); i++){
        if(strcmp(json_string_value(json_array_get(jarray, i)), name) == 0){
            return 1;
        }
    }
    return 0;
}

canonical_original_code:
int ssm_in_jarray(struct json_t *jarray, char *name)
{
    int i;
    for(i=0; i<json_array_size(jarray); i++){
        if(strcmp(json_string_value(json_array_get(jarray, i)), name) == 0){
            return 1;
        }
    }
    return 0;
}

variable_types: struct json_t { enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 } type; unsigned long refcount; } *

variable_types:
name
char *

user_defined_types: struct json_t { enum json_type type; unsigned long refcount; }


decompiled_code:
__int64 __fastcall ssm_str_to_interp_type(const char *a1)
{
bool v2; // dl
bool v3; // cf
bool v4; // zf
const char *v5; // rdi
__int64 v6; // rcx
const char *v7; // rsi

if ( !strcmp(a1, "linear") )
return gsl_interp_linear;
if ( !strcmp(a1, "polynomial") )
return gsl_interp_polynomial;
if ( !strcmp(a1, "cspline") )
return gsl_interp_cspline;
if ( !strcmp(a1, "cspline_periodic") )
return gsl_interp_cspline_periodic;
v2 = strcmp(a1, "akima") != 0;
v3 = 0;
v4 = !v2;
if ( !v2 )
return gsl_interp_akima;
v5 = "akima_periodic";
v6 = 15LL;
v7 = a1;
do
{
if ( !v6 )
break;
v3 = *v7 < (unsigned int)*v5;
v4 = *v7++ == *v5++;
--v6;
}
while ( v4 );
if ( (!v3 && !v4) == v3 )
return gsl_interp_akima_periodic;
ssm_print_warning("Unknown gsl interpolator for metadata. Linear interpolator will be used instead.", v7);
return gsl_interp_linear;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
bool v2; // dl
bool v3; // cf
bool v4; // zf
const char *v5; // rdi
__int64 v6; // rcx
const char *v7; // rsi

if ( !strcmp(a1, "linear") )
return gsl_interp_linear;
if ( !strcmp(a1, "polynomial") )
return gsl_interp_polynomial;
if ( !strcmp(a1, "cspline") )
return gsl_interp_cspline;
if ( !strcmp(a1, "cspline_periodic") )
return gsl_interp_cspline_periodic;
v2 = strcmp(a1, "akima") != 0;
v3 = 0;
v4 = !v2;
if ( !v2 )
return gsl_interp_akima;
v5 = "akima_periodic";
v6 = 15LL;
v7 = a1;
do
{
if ( !v6 )
break;
v3 = *v7 < (unsigned int)*v5;
v4 = *v7++ == *v5++;
--v6;
}
while ( v4 );
if ( (!v3 && !v4) == v3 )
return gsl_interp_akima_periodic;
ssm_print_warning("Unknown gsl interpolator for metadata. Linear interpolator will be used instead.", v7);
return gsl_interp_linear;
}

original_code:
const gsl_interp_type *ssm_str_to_interp_type(const char *optarg)
{
    if (strcmp(optarg, "linear") == 0) {
        return gsl_interp_linear;
    } else if (strcmp(optarg, "polynomial") == 0){
        return gsl_interp_polynomial;
    } else if (strcmp(optarg, "cspline") == 0){
        return gsl_interp_cspline;
    } else if (strcmp(optarg, "cspline_periodic") == 0){
        return gsl_interp_cspline_periodic;
    } else if (strcmp(optarg, "akima") == 0){
        return gsl_interp_akima;
    } else if (strcmp(optarg, "akima_periodic") == 0){
        return gsl_interp_akima_periodic;
    }
    ssm_print_warning("Unknown gsl interpolator for metadata. Linear interpolator will be used instead.");
    return gsl_interp_linear;
}

canonical_original_code:
struct gsl_interp_type *ssm_str_to_interp_type(char *optarg)
{
    if (strcmp(optarg, "linear") == 0) {
        return gsl_interp_linear;
    } else if (strcmp(optarg, "polynomial") == 0){
        return gsl_interp_polynomial;
    } else if (strcmp(optarg, "cspline") == 0){
        return gsl_interp_cspline;
    } else if (strcmp(optarg, "cspline_periodic") == 0){
        return gsl_interp_cspline_periodic;
    } else if (strcmp(optarg, "akima") == 0){
        return gsl_interp_akima;
    } else if (strcmp(optarg, "akima_periodic") == 0){
        return gsl_interp_akima_periodic;
    }
    ssm_print_warning("Unknown gsl interpolator for metadata. Linear interpolator will be used instead.");
    return gsl_interp_linear;
}

variable_types:
optarg
char *

return_type: struct gsl_interp_type { char *name; unsigned int min_size; void *(*alloc)(unsigned long size); int (*init)(void *, double xa[], double ya[], unsigned long size); int (*eval)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel { unsigned long cache; unsigned long miss_count; unsigned long hit_count; } *, double *y); int (*eval_deriv)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel *, double *y_p); int (*eval_deriv2)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel *, double *y_pp); int (*eval_integ)(void *, double xa[], double ya[], unsigned long size, struct gsl_interp_accel *, double a, double b, double *result); void (*free)(void *); } *

user_defined_types: struct gsl_interp_type { char *name; unsigned int min_size; void *(*alloc)(unsigned long size); int (*init)(void *, double xa[], double ya[], unsigned long size); int (*eval)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel *, double *y); int (*eval_deriv)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel *, double *y_p); int (*eval_deriv2)(void *, double xa[], double ya[], unsigned long size, double x, struct gsl_interp_accel *, double *y_pp); int (*eval_integ)(void *, double xa[], double ya[], unsigned long size, struct gsl_interp_accel *, double a, double b, double *result); void (*free)(void *); }


decompiled_code:
__int64 __fastcall ssm_sanitize_n_threads(unsigned int a1, _DWORD *a2)
{
__int64 result; // rax

result = a1;
if ( *a2 <= (signed int)a1 )
result = (unsigned int)*a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, _DWORD *a2)
{
__int64 result; // rax

result = a1;
if ( *a2 <= (signed int)a1 )
result = (unsigned int)*a2;
return result;
}

original_code:
int ssm_sanitize_n_threads(int n_threads, ssm_fitness_t *fitness)
{
    if(n_threads > fitness->J){
        return fitness->J;
    } else {
        return n_threads;
    }
}

canonical_original_code:
int ssm_sanitize_n_threads(int n_threads, struct ssm_fitness_t *fitness)
{
    if(n_threads > fitness->J){
        return fitness->J;
    } else {
        return n_threads;
    }
}


original_code:
void ssm_resample_X(ssm_fitness_t *fitness, ssm_X_t ***J_p_X, ssm_X_t ***J_p_X_tmp, int n)
{
    int j;
    unsigned int *select = fitness->select[n];
    for(j=0;j<fitness->J;j++) {
        (*J_p_X_tmp)[j]->dt = (*J_p_X)[select[j]]->dt;
        memcpy((*J_p_X_tmp)[j]->proj, (*J_p_X)[select[j]]->proj, (*J_p_X)[select[j]]->length * sizeof(double));
    }
    ssm_swap_X(J_p_X, J_p_X_tmp);
}

canonical_original_code:
void ssm_resample_X(struct ssm_fitness_t *fitness, struct ssm_X_t ***J_p_X, struct ssm_X_t ***J_p_X_tmp, int n)
{
    int j;
    unsigned int *select = fitness->select[n];
    for(j=0;j<fitness->J;j++) {
        (*J_p_X_tmp)[j]->dt = (*J_p_X)[select[j]]->dt;
        memcpy((*J_p_X_tmp)[j]->proj, (*J_p_X)[select[j]]->proj, (*J_p_X)[select[j]]->length * sizeof(double));
    }
    ssm_swap_X(J_p_X, J_p_X_tmp);
}

variable_types: struct ssm_X_t { int length; double *proj; double dt; double dt0; } ***

variable_types: n int

variable_types:
select
unsigned int *


decompiled_code:
__int64 __fastcall ssm_load_json_file(__int64 a1)
{
__int64 result; // rax
__int64 v2; // [rsp+0h] [rbp-108h] BYREF
char v3[172]; // [rsp+5Ch] [rbp-ACh] BYREF

result = json_load_file(a1, 0LL, &v2);
if ( !result )
{
ssm_print_err(v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 result; // rax
__int64 v2; // [rsp+0h] [rbp-108h] BYREF
char v3[172]; // [rsp+5Ch] [rbp-ACh] BYREF

result = json_load_file(a1, 0LL, &v2);
if ( !result )
{
ssm_print_err(v3);
exit(1);
}
return result;
}

original_code:
json_t *ssm_load_json_file(const char *path)
{
    json_error_t error;
    json_t *data = json_load_file(path, 0, &error);
    if(!data) {
        ssm_print_err(error.text);
        exit(1);
    }
    return data;
}

canonical_original_code:
struct json_t *ssm_load_json_file(char *path)
{
    struct json_error_t error;
    struct json_t *data = json_load_file(path, 0, &error);
    if(!data) {
        ssm_print_err(error.text);
        exit(1);
    }
    return data;
}

variable_types:
path
char *

variable_types: struct json_error_t { int line; int column; int position; char source[80]; char text[160]; }

return_type: struct json_t { enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 } type; unsigned long refcount; } *


decompiled_code:
__int64 ssm_load_data()
{
return ssm_load_json_file((__int64)".data.json");
}

canonical_decompiled_code:
__int64 func2()
{
return func1((__int64)".data.json");
}

original_code:
json_t *ssm_load_data(ssm_options_t *opts)
{
    json_t *jdata = ssm_load_json_file(".data.json");
    return jdata;
}

canonical_original_code:
struct json_t *ssm_load_data(struct ssm_options_t *opts)
{
    struct json_t *jdata = ssm_load_json_file(".data.json");
    return jdata;
}

variable_types: struct ssm_options_t { enum ssm_algo_t { SSM_SMC=expr, SSM_MIF=expr, SSM_PMCMC=expr, SSM_KMCMC=expr, SSM_KALMAN=expr, SSM_KSIMPLEX=expr, SSM_SIMUL=expr, SSM_SIMPLEX=expr, SSM_WORKER=expr } algo; enum ssm_algo_t { SSM_SMC=expr, SSM_MIF=expr, SSM_PMCMC=expr, SSM_KMCMC=expr, SSM_KALMAN=expr, SSM_KSIMPLEX=expr, SSM_SIMUL=expr, SSM_SIMPLEX=expr, SSM_WORKER=expr } worker_algo; enum ssm_implementations_t { SSM_ODE=0, SSM_SDE=1, SSM_PSR=2, SSM_EKF=3 } implementation; enum ssm_noises_off_t { SSM_NO_DEM_STO=expr, SSM_NO_WHITE_NOISE=expr, SSM_NO_DIFF=expr } noises_off; enum ssm_print_t { SSM_PRINT_TRACE=expr, SSM_PRINT_X=expr, SSM_PRINT_HAT=expr, SSM_PRINT_DIAG=expr, SSM_PRINT_LOG=expr, SSM_PRINT_WARNING=expr } print; int id; int flag_seed_time; int flag_prior; double dt; double eps_abs; double eps_rel; char *freeze_forcing; char *root; char *next; int n_thread; double like_min; int J; int n_obs; char *interpolator; int n_iter; double a; double b; double L; int m_switch; int flag_ic_only; int eps_switch; double eps_max; double flag_smooth; double alpha; int n_traj; int flag_tcp; int flag_least_squares; double size_stop; int freq; char *start; char *end; char *server; int flag_no_filter; } *

user_defined_types: struct ssm_options_t { enum ssm_algo_t algo; enum ssm_algo_t worker_algo; enum ssm_implementations_t implementation; enum ssm_noises_off_t noises_off; enum ssm_print_t print; int id; int flag_seed_time; int flag_prior; double dt; double eps_abs; double eps_rel; char *freeze_forcing; char *root; char *next; int n_thread; double like_min; int J; int n_obs; char *interpolator; int n_iter; double a; double b; double L; int m_switch; int flag_ic_only; int eps_switch; double eps_max; double flag_smooth; double alpha; int n_traj; int flag_tcp; int flag_least_squares; double size_stop; int freq; char *start; char *end; char *server; int flag_no_filter; }

user_defined_types: enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 }


heaviside

decompiled_code:
double __fastcall heaviside(double a1)
{
double result; // xmm0_8

if ( a1 < 0.0 )
result = 0.0;
else
result = *(double *)&LC1;
return result;
}

canonical_decompiled_code:
double __fastcall func0(double a1)
{
double result; // xmm0_8

if ( a1 < 0.0 )
result = 0.0;
else
result = *(double *)&LC1;
return result;
}

original_code:
double heaviside(double x)
{
    return (x < 0.0) ? 0.0 : 1.0;
}

variable_types: x double


ramp

decompiled_code:
double __fastcall ramp(double result)
{
if ( result < 0.0 )
result = 0.0;
return result;
}

canonical_decompiled_code:
double __fastcall func1(double result)
{
if ( result < 0.0 )
result = 0.0;
return result;
}

original_code:
double ramp(double x)
{
    return (x >= 0) ? x : 0.0;
}


slowstep

original_code:
double slowstep(double x, double d)
{
    return ( x >= 0.0 ? ( x >= d ? d : x ) : 0.0 );
}

variable_types: d double


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

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

original_code:
void ssm_st2_free(size_t **tab, int n)
{
    int i;
    for(i=0; i<n; i++){
        free(tab[i]);
    }
    free(tab);
}

canonical_original_code:
void ssm_st2_free(unsigned long **tab, int n)
{
    int i;
    for(i=0; i<n; i++){
        free(tab[i]);
    }
    free(tab);
}

variable_types:
tab
unsigned long **

variable_types: i int


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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 )
{
result = _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 8), 0xFFFFFFFFFFFFFFFFLL);
if ( result == 1 )
result = json_delete();
}
}
return result;
}

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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 )
{
result = _InterlockedExchangeAdd64((volatile signed __int64 *)(a1 + 8), 0xFFFFFFFFFFFFFFFFLL);
if ( result == 1 )
result = json_delete();
}
}
return result;
}

original_code:
static inline
void json_decref(json_t *json)
{
    if(json && json->refcount != (size_t)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}

canonical_original_code:
static inline void json_decref(struct json_t *json)
{
    if(json && json->refcount != (unsigned long)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}


main

canonical_name: main

original_code:
int main(int argc, char *argv[])
{
    return clar_test(argc, argv);
}

variable_types: argc int

variable_types:
argv
char *[]