decompiled_code:
__int64 __fastcall write_cache_updates(const char ***a1)
{
const char **v1; // rbp
unsigned int v2; // er13
size_t v4; // rax
char *v5; // rbx
size_t v6; // rax
char *v7; // r12
struct stat v8; // [rsp+0h] [rbp-148h] BYREF
struct stat v9; // [rsp+90h] [rbp-B8h] BYREF

v1 = *a1;
if ( !*a1 )
return 0;
while ( 1 )
{
v4 = strlen(*v1);
v5 = (char *)malloc(v4 + 10);
strcpy(v5, "/tmp/test");
strcat(v5, *v1);
v6 = strlen(*v1);
v7 = (char *)malloc(v6 + 16);
strcpy(v7, "/tmp/test_cache");
strcat(v7, *v1);
if ( stat(v5, &v9) )
{
printf("%s doesn't exist\n", v5);
return (unsigned int)-1;
}
v2 = stat(v7, &v8);
if ( v2 )
{
printf("%s doesn't exist\n", v7);
return (unsigned int)-1;
}
if ( v9.st_mtim.tv_sec >= v8.st_mtim.tv_sec )
{
puts("cached file hasn't been modified");
goto LABEL_8;
}
printf("%s: cached file has been modified and needs to be updated\n", *v1);
if ( remove(v5) )
{
printf("unable to remove %s\n", v5);
return (unsigned int)-1;
}
if ( (unsigned int)copy_file(v5, v7) )
break;
LABEL_8:
free(v5);
free(v7);
v1 = (const char **)v1[1];
if ( !v1 )
return v2;
}
printf("unable to copy cached file %s\n", *v1);
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char ***a1)
{
const char **v1; // rbp
unsigned int v2; // er13
size_t v4; // rax
char *v5; // rbx
size_t v6; // rax
char *v7; // r12
struct stat v8; // [rsp+0h] [rbp-148h] BYREF
struct stat v9; // [rsp+90h] [rbp-B8h] BYREF

v1 = *a1;
if ( !*a1 )
return 0;
while ( 1 )
{
v4 = strlen(*v1);
v5 = (char *)malloc(v4 + 10);
strcpy(v5, "/tmp/test");
strcat(v5, *v1);
v6 = strlen(*v1);
v7 = (char *)malloc(v6 + 16);
strcpy(v7, "/tmp/test_cache");
strcat(v7, *v1);
if ( stat(v5, &v9) )
{
printf("%s doesn't exist\n", v5);
return (unsigned int)-1;
}
v2 = stat(v7, &v8);
if ( v2 )
{
printf("%s doesn't exist\n", v7);
return (unsigned int)-1;
}
if ( v9.st_mtim.tv_sec >= v8.st_mtim.tv_sec )
{
puts("cached file hasn't been modified");
goto LABEL_8;
}
printf("%s: cached file has been modified and needs to be updated\n", *v1);
if ( remove(v5) )
{
printf("unable to remove %s\n", v5);
return (unsigned int)-1;
}
if ( (unsigned int)copy_file(v5, v7) )
break;
LABEL_8:
free(v5);
free(v7);
v1 = (const char **)v1[1];
if ( !v1 )
return v2;
}
printf("unable to copy cached file %s\n", *v1);
return (unsigned int)-1;
}

original_code:
int write_cache_updates(cache* file_cache) {
  cache_entry* aux;
  char* back_path;
  char* cache_path;
  struct stat back_stat, cache_stat;
  for(aux=file_cache->file_cache;aux;aux=aux->next) {
    back_path = malloc(sizeof(char) * (strlen(backing_dir) + strlen(aux->filename) + 1));
    strcpy(back_path,backing_dir);
    strcat(back_path,aux->filename);
    cache_path = malloc(sizeof(char) * (strlen(cache_dir) + strlen(aux->filename) + 1));
    strcpy(cache_path,cache_dir);
    strcat(cache_path,aux->filename);
    if(stat(back_path,&back_stat)) {
      printf("%s doesn't exist\n", back_path);
      return -1;
    }
    if(stat(cache_path,&cache_stat)) {
      printf("%s doesn't exist\n", cache_path);
      return -1;
    }
    if(back_stat.st_mtim.tv_sec < cache_stat.st_mtim.tv_sec) {
      printf("%s: cached file has been modified and needs to be updated\n",aux->filename);
      if(remove(back_path)) {
 printf("unable to remove %s\n",back_path);
 return -1;
      }
      if(copy_file(back_path,cache_path)) {
 printf("unable to copy cached file %s\n",aux->filename);
 return -1;
      }
    }
    else
      printf("cached file hasn't been modified\n");
    free(back_path);
    free(cache_path);
  }
  return 0;
}

canonical_original_code:
int write_cache_updates(struct cache_t *file_cache) {
  struct cache_entry_t *aux;
  char *back_path;
  char *cache_path;
  struct stat back_stat; struct stat cache_stat;
  for(aux=file_cache->file_cache;aux;aux=aux->next) {
    back_path = malloc(sizeof(char) * (strlen(backing_dir) + strlen(aux->filename) + 1));
    strcpy(back_path,backing_dir);
    strcat(back_path,aux->filename);
    cache_path = malloc(sizeof(char) * (strlen(cache_dir) + strlen(aux->filename) + 1));
    strcpy(cache_path,cache_dir);
    strcat(cache_path,aux->filename);
    if(stat(back_path,&back_stat)) {
      printf("%s doesn't exist\n", back_path);
      return -1;
    }
    if(stat(cache_path,&cache_stat)) {
      printf("%s doesn't exist\n", cache_path);
      return -1;
    }
    if(back_stat.st_mtim.tv_sec < cache_stat.st_mtim.tv_sec) {
      printf("%s: cached file has been modified and needs to be updated\n",aux->filename);
      if(remove(back_path)) {
 printf("unable to remove %s\n",back_path);
 return -1;
      }
      if(copy_file(back_path,cache_path)) {
 printf("unable to copy cached file %s\n",aux->filename);
 return -1;
      }
    }
    else
      printf("cached file hasn't been modified\n");
    free(back_path);
    free(cache_path);
  }
  return 0;
}

variable_types: struct cache_entry_t { char *filename; struct cache_entry_t *next; } *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: jdlourenco/gpu

decompiled_code:
int __fastcall show_cache_entries(__int64 *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int result; // eax

v1 = *a1;
v2 = 0;
if ( !*a1 )
return puts("The list is empty");
do
{
result = printf("%d: %s\n", v2++, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 *a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int result; // eax

v1 = *a1;
v2 = 0;
if ( !*a1 )
return puts("The list is empty");
do
{
result = printf("%d: %s\n", v2++, *(const char **)v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return result;
}

original_code:
void show_cache_entries(cache* cache) {
  int i;
  cache_entry* entry = cache->file_cache;
  if(entry==((void *)0)) {
    printf("The list is empty\n");
    return;
  }
  for(i=0;entry!=((void *)0);i++,entry=entry->next) {
    printf("%d: %s\n",i,entry->filename);
  }
}

canonical_original_code:
void show_cache_entries(struct cache_t *cache) {
  int i;
  struct cache_entry_t *entry = cache->file_cache;
  if(entry==((void *)0)) {
    printf("The list is empty\n");
    return;
  }
  for(i=0;entry!=((void *)0);i++,entry=entry->next) {
    printf("%d: %s\n",i,entry->filename);
  }
}

variable_types: struct cache_t { struct cache_entry_t { char *filename; struct cache_entry_t *next; } *file_cache; short cache_size; } *

variable_types: i int

return_type: void

user_defined_types: struct cache_t { struct cache_entry_t *file_cache; short cache_size; }


decompiled_code:
void __fastcall __noreturn cache_loop(__int64 *a1)
{
show_cache_entries(a1);
while ( 1 )
{
sleep(3u);
puts("I'm the thread of the world");
write_cache_updates((const char ***)a1);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(__int64 *a1)
{
func2(a1);
while ( 1 )
{
sleep(3u);
puts("I'm the thread of the world");
func0((const char ***)a1);
}
}

original_code:
void* cache_loop(void *arg) {
  cache* file_cache = (cache*) arg;
  show_cache_entries(file_cache);
  while(1) {
    sleep(3);
    printf("I'm the thread of the world\n");
    write_cache_updates(file_cache);
  }
}

canonical_original_code:
void *cache_loop(void *arg) {
  struct cache_t *file_cache = (cache*) arg;
  show_cache_entries(file_cache);
  while(1) {
    sleep(3);
    printf("I'm the thread of the world\n");
    write_cache_updates(file_cache);
  }
}

variable_types:
arg
void *

return_type: void *

user_defined_types: struct cache_entry_t { char *filename; struct cache_entry_t *next; }


decompiled_code:
void __fastcall cache_remove(__int64 *a1, const char *a2)
{
__int64 v2; // r13
__int64 v3; // rbx
__int64 v4; // r12

v2 = *a1;
v3 = *a1;
v4 = *a1;
if ( *a1 )
{
while ( strcmp(*(const char **)v3, a2) )
{
v4 = v3;
if ( !*(_QWORD *)(v3 + 8) )
return;
v3 = *(_QWORD *)(v3 + 8);
}
if ( v2 == v3 )
*a1 = *(_QWORD *)(v3 + 8);
else
*(_QWORD *)(v4 + 8) = *(_QWORD *)(v3 + 8);
free((void *)v3);
}
else
{
puts("The list is empty");
}
}

original_code:
void cache_remove(cache* cache, char* filename) {
  cache_entry *prev,*temp;
  temp = cache->file_cache;
  if(temp==((void *)0)) {
    printf("The list is empty\n");
    return;
  }
  prev=temp;
  while(temp!=((void *)0)) {
    if(strcmp(temp->filename,filename)==0) {
      if(temp==cache->file_cache)
 cache->file_cache=temp->next;
      else
 prev->next=temp->next;
      free(temp);
      return;
    }
    else {
      prev=temp;
      temp=temp->next;
    }
  }
}

canonical_original_code:
void cache_remove(struct cache_t *cache, char *filename) {
  struct cache_entry_t *prev; struct cache_entry_t *temp;
  temp = cache->file_cache;
  if(temp==((void *)0)) {
    printf("The list is empty\n");
    return;
  }
  prev=temp;
  while(temp!=((void *)0)) {
    if(strcmp(temp->filename,filename)==0) {
      if(temp==cache->file_cache)
 cache->file_cache=temp->next;
      else
 prev->next=temp->next;
      free(temp);
      return;
    }
    else {
      prev=temp;
      temp=temp->next;
    }
  }
}

variable_types:
filename
char *


decompiled_code:
__int64 __fastcall contains_entry(__int64 *a1, const char *a2)
{
__int64 v2; // rbx

v2 = *a1;
if ( !*a1 )
return 0LL;
while ( strcmp(*(const char **)v2, a2) )
{
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, const char *a2)
{
__int64 v2; // rbx

v2 = *a1;
if ( !*a1 )
return 0LL;
while ( strcmp(*(const char **)v2, a2) )
{
v2 = *(_QWORD *)(v2 + 8);
if ( !v2 )
return 0LL;
}
return 1LL;
}

original_code:
int contains_entry(cache* cache, char* filename) {
  cache_entry* entry = cache->file_cache;
  while(entry) {
    if(!strcmp(entry->filename,filename))
      return 1;
    entry=entry->next;
  }
  return 0;
}

canonical_original_code:
int contains_entry(struct cache_t *cache, char *filename) {
  struct cache_entry_t *entry = cache->file_cache;
  while(entry) {
    if(!strcmp(entry->filename,filename))
      return 1;
    entry=entry->next;
  }
  return 0;
}

return_type: int


decompiled_code:
__int64 __fastcall copy_file(char *file, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
void *i; // rbx
__int64 result; // rax
int v7; // eax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

v2 = open(a2, 0);
if ( v2 < 0 )
{
puts("unable to open source file");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
fstat(v2, &v8);
v4 = open(file, 65, v8.st_mode);
if ( v4 < 0 )
{
puts("unable to open destination file");
result = 0xFFFFFFFFLL;
}
else
{
for ( i = malloc(0x200uLL); ; write(v4, i, v7) )
{
v7 = read(v3, i, 0x200uLL);
if ( v7 <= 0 )
break;
}
futimes(v3, 0LL);
close(v3);
close(v4);
free(i);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *file, char *a2)
{
int v2; // eax
int v3; // ebp
int v4; // er12
void *i; // rbx
__int64 result; // rax
int v7; // eax
struct stat v8; // [rsp+0h] [rbp-A8h] BYREF

v2 = open(a2, 0);
if ( v2 < 0 )
{
puts("unable to open source file");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
fstat(v2, &v8);
v4 = open(file, 65, v8.st_mode);
if ( v4 < 0 )
{
puts("unable to open destination file");
result = 0xFFFFFFFFLL;
}
else
{
for ( i = malloc(0x200uLL); ; write(v4, i, v7) )
{
v7 = read(v3, i, 0x200uLL);
if ( v7 <= 0 )
break;
}
futimes(v3, 0LL);
close(v3);
close(v4);
free(i);
result = 0LL;
}
}
return result;
}

original_code:
int copy_file(char* dst_path, char* src_path) {
  int src_fd, dst_fd;
  char *buf;
  int n_read;
  struct stat source_stat;
  if((src_fd = open(src_path,00)) < 0) {
    printf("unable to open source file\n");
    return -1;
  }
  fstat(src_fd,&source_stat);
  if((dst_fd = open(dst_path,0100|01,source_stat.st_mode)) < 0) {
    printf("unable to open destination file\n");
    return -1;
  }
  buf = malloc(sizeof(char) * 512);
  while((n_read = read(src_fd,buf,512)) > 0) {
    write(dst_fd,buf,n_read);
  }
  futimes(src_fd,((void *)0));
  close(src_fd);
  close(dst_fd);
  free(buf);
  return 0;
}

canonical_original_code:
int copy_file(char *dst_path, char *src_path) {
  int src_fd; int dst_fd;
  char *buf;
  int n_read;
  struct stat source_stat;
  if((src_fd = open(src_path,00)) < 0) {
    printf("unable to open source file\n");
    return -1;
  }
  fstat(src_fd,&source_stat);
  if((dst_fd = open(dst_path,0100|01,source_stat.st_mode)) < 0) {
    printf("unable to open destination file\n");
    return -1;
  }
  buf = malloc(sizeof(char) * 512);
  while((n_read = read(src_fd,buf,512)) > 0) {
    write(dst_fd,buf,n_read);
  }
  futimes(src_fd,((void *)0));
  close(src_fd);
  close(dst_fd);
  free(buf);
  return 0;
}

variable_types:
buf
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


Absf

repo: jdoliner/Ray-Tracer

decompiled_code:
__m128 __fastcall Absf(__m128 result)
{
if ( result.m128_f32[0] <= 0.0 )
result = _mm_xor_ps(result, (__m128)LC1);
return result;
}

canonical_decompiled_code:
__m128 __fastcall func0(__m128 result)
{
if ( result.m128_f32[0] <= 0.0 )
result = _mm_xor_ps(result, (__m128)LC1);
return result;
}

original_code:
static inline float Absf (float a) {
    if (a > 0)
 return a;
    else
 return -a;
}

canonical_original_code:
static inline float Absf(float a) {
    if (a > 0)
 return a;
    else
 return -a;
}

variable_types: a float

return_type: float


decompiled_code:
void __fastcall ScaledAddV3f(float *a1, float *a2, float *a3, float a4)
{
*a3 = (float)(a4 * *a2) + *a1;
a3[1] = (float)(a4 * a2[1]) + a1[1];
a3[2] = (float)(a4 * a2[2]) + a1[2];
}

original_code:
static inline void ScaledAddV3f (Vec3f_t u, float s, Vec3f_t v, Vec3f_t dst)
{
    dst[0] = u[0] + s*v[0];
    dst[1] = u[1] + s*v[1];
    dst[2] = u[2] + s*v[2];
}

canonical_original_code:
static inline void ScaledAddV3f(float u[3], float s, float v[3], float dst[3])
{
    dst[0] = u[0] + s*v[0];
    dst[1] = u[1] + s*v[1];
    dst[2] = u[2] + s*v[2];
}

variable_types:
u
float [3]

variable_types: s float

variable_types:
v
float [3]

variable_types:
dst
float [3]


RayToPointf

decompiled_code:
void __fastcall RayToPointf(float *a1, float *a2, float a3)
{
ScaledAddV3f(a1, a1 + 3, a2, a3);
}

canonical_decompiled_code:
void __fastcall func3(float *a1, float *a2, float a3)
{
func2(a1, a1 + 3, a2, a3);
}

original_code:
static inline void RayToPointf (Rayf_t *r, float t, Vec3f_t dst)
{
    ScaledAddV3f (r->orig, t, r->dir, dst);
}

canonical_original_code:
static inline void RayToPointf(struct Rayf_t *r, float t, float dst[3])
{
    ScaledAddV3f (r->orig, t, r->dir, dst);
}

variable_types: struct Rayf_t { float orig[3]; float dir[3]; } *

variable_types: t float


original_code:
Intersection_t *Intersect_Box(Rayf_t *ray, Geo_Box_t *box) {
    _Bool hit;
    int i;
    Vec3f_t normal;
    Vec3f_t xpoint;
    float t_to_beat = 3.40282346638528859811704183484516925e+38F;
    for (i = 0; i < 3; i++) {
 float r;
 float t;
 if(ray->dir[i] < 0)
     r = box->r[i];
 else
     r = -box->r[i];
 t = (r - ray->orig[i]) / ray->dir[i];
 RayToPointf(ray, t, xpoint);
 if (0 < t && t < t_to_beat && Absf(xpoint[(i + 1) % 3]) <= box->r[(i + 1) % 3] && Absf(xpoint[(i + 2) % 3]) <= box->r[(i + 2) % 3]) {
     t_to_beat = t;
     normal[0] = normal[1] = normal[2] = 0.0;
     normal[i] = 1.0;
     hit = 1;
 }
    }
    if (hit) {
 Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t_to_beat;
 RayToPointf(ray, t_to_beat, intersection->point);
 CopyV3f(normal, intersection->norm);
 return intersection;
    }
    return ((void *)0);
}

canonical_original_code:
struct Intersection_t *Intersect_Box(struct Rayf_t *ray, struct Geo_Box_t *box) {
    _Bool hit;
    int i;
    float normal[3];
    float xpoint[3];
    float t_to_beat = 3.40282346638528859811704183484516925e+38F;
    for (i = 0; i < 3; i++) {
 float r;
 float t;
 if(ray->dir[i] < 0)
     r = box->r[i];
 else
     r = -box->r[i];
 t = (r - ray->orig[i]) / ray->dir[i];
 RayToPointf(ray, t, xpoint);
 if (0 < t && t < t_to_beat && Absf(xpoint[(i + 1) % 3]) <= box->r[(i + 1) % 3] && Absf(xpoint[(i + 2) % 3]) <= box->r[(i + 2) % 3]) {
     t_to_beat = t;
     normal[0] = normal[1] = normal[2] = 0.0;
     normal[i] = 1.0;
     hit = 1;
 }
    }
    if (hit) {
 struct Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t_to_beat;
 RayToPointf(ray, t_to_beat, intersection->point);
 CopyV3f(normal, intersection->norm);
 return intersection;
    }
    return ((void *)0);
}

variable_types:
box
struct Geo_Box_t { float r[3]; } *

variable_types:
normal
float [3]

variable_types:
xpoint
float [3]

variable_types: r float

variable_types: struct Intersection_t { float t; float point[3]; float norm[3]; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; float u; float v; void *geo; } *

user_defined_types: struct Rayf_t { float orig[3]; float dir[3]; }

user_defined_types: struct Intersection_t { float t; float point[3]; float norm[3]; struct Material_t *material; float u; float v; void *geo; }


main

canonical_name: main

original_code:
int main (int argc, char *argv[]) {
    srand(time(((void *)0)));
    int i, j;
    if (argc < 2)
 ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../src/raytracer.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    Scene_t *scene;
    scene = Parse_File(argv[1]);
    if (scene->settings->radiosity) {
 Calculate_Rex(scene, 1024, scene->settings->rad_accuracy);
    }
    int width = 1024, height = 1024;
    Color_t *render = Render_Scene(scene, width, height);
    Image_t *output = New_Image(width, height, render);
    Write_Image(output, "output.ppm");
    Delete_Image(output);
    for (i = 0; i < scene->nLights; i++)
 free(scene->light[i]);
    for (i = 0; i < scene->nGeo; i++) {
 for (j = 0; j < scene->geometry[i]->diffuse_rex->resolution; j++) {
     free(scene->geometry[i]->diffuse_rex->value[i]);
     free(scene->geometry[i]->diffuse_rex->vec[i]);
     free(scene->geometry[i]->diffuse_rex->nSamples[i]);
 }
 free(scene->geometry[i]->diffuse_rex);
 free(scene->geometry[i]);
    }
    free(scene);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    srand(time(((void *)0)));
    int i; int j;
    if (argc < 2)
 ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../src/raytracer.c", 15, __extension__ __PRETTY_FUNCTION__); }));
    struct Scene_t *scene;
    scene = Parse_File(argv[1]);
    if (scene->settings->radiosity) {
 Calculate_Rex(scene, 1024, scene->settings->rad_accuracy);
    }
    int width = 1024; int height = 1024;
    unsigned char (*render)[4] = Render_Scene(scene, width, height);
    struct Image_t *output = New_Image(width, height, render);
    Write_Image(output, "output.ppm");
    Delete_Image(output);
    for (i = 0; i < scene->nLights; i++)
 free(scene->light[i]);
    for (i = 0; i < scene->nGeo; i++) {
 for (j = 0; j < scene->geometry[i]->diffuse_rex->resolution; j++) {
     free(scene->geometry[i]->diffuse_rex->value[i]);
     free(scene->geometry[i]->diffuse_rex->vec[i]);
     free(scene->geometry[i]->diffuse_rex->nSamples[i]);
 }
 free(scene->geometry[i]->diffuse_rex);
 free(scene->geometry[i]);
    }
    free(scene);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: j int

variable_types: struct Scene_t { int nGeo; int nLights; struct Geometry_t { float scale[3]; float rot[4]; float trans[3]; struct BBox_t { float corner[2][3]; } bBox; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; enum Prim_Type_t { SPHERE=0, BOX=1, TORUS=2, PLANE=3, NUM_PRIMS=4 } prim_type; union Primitive_t { struct Geo_Sphere_t { float radius; } sphere; struct Geo_Box_t { float r[3]; } box; struct Geo_Torus_t { float revRadius; float circRadius; } torus; struct Geo_Plane_t { float N[3]; float P[3]; } plane; } *primitive; struct Rex_t { unsigned char (**value)[4]; float (**vec)[3]; int **nSamples; int resolution; } *diffuse_rex; } **geometry; struct Light_t { unsigned char color[4]; float intensity; float pos[3]; float look_at[3]; } **light; struct Camera_t { float pos[3]; float look_at[3]; float up[3]; float focal_length; float width; float height; } *camera; struct Settings_t { unsigned char background[4]; char radiosity; int rad_accuracy; } *settings; } *

variable_types: width int

variable_types: height int

variable_types:
render
unsigned char (*)[4]

variable_types: struct Image_t { int width; int height; unsigned char (*data)[4]; } *

user_defined_types: struct Scene_t { int nGeo; int nLights; struct Geometry_t **geometry; struct Light_t **light; struct Camera_t *camera; struct Settings_t *settings; }

user_defined_types: struct Geometry_t { float scale[3]; float rot[4]; float trans[3]; struct BBox_t bBox; struct Material_t *material; enum Prim_Type_t prim_type; union Primitive_t *primitive; struct Rex_t *diffuse_rex; }

user_defined_types: union Primitive_t { struct Geo_Sphere_t sphere; struct Geo_Box_t box; struct Geo_Torus_t torus; struct Geo_Plane_t plane; }


original_code:
static inline void AddV3f (Vec3f_t u, Vec3f_t v, Vec3f_t dst)
{
    dst[0] = u[0] + v[0];
    dst[1] = u[1] + v[1];
    dst[2] = u[2] + v[2];
}

canonical_original_code:
static inline void AddV3f(float u[3], float v[3], float dst[3])
{
    dst[0] = u[0] + v[0];
    dst[1] = u[1] + v[1];
    dst[2] = u[2] + v[2];
}

variable_types:
u
float [3]

variable_types:
v
float [3]


original_code:
static inline void SubV3f (Vec3f_t u, Vec3f_t v, Vec3f_t dst)
{
    dst[0] = u[0] - v[0];
    dst[1] = u[1] - v[1];
    dst[2] = u[2] - v[2];
}

canonical_original_code:
static inline void SubV3f(float u[3], float v[3], float dst[3])
{
    dst[0] = u[0] - v[0];
    dst[1] = u[1] - v[1];
    dst[2] = u[2] - v[2];
}

variable_types:
dst
float [3]


CopyColor

decompiled_code:
__int64 __fastcall CopyColor(_BYTE *a1, _BYTE *a2)
{
__int64 result; // rax

*a2 = *a1;
a2[1] = a1[1];
a2[2] = a1[2];
result = (unsigned __int8)a1[3];
a2[3] = result;
return result;
}

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

*a2 = *a1;
a2[1] = a1[1];
a2[2] = a1[2];
result = (unsigned __int8)a1[3];
a2[3] = result;
return result;
}

original_code:
static inline void CopyColor (Color_t src, Color_t dst)
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
}

canonical_original_code:
static inline void CopyColor(unsigned char src[4], unsigned char dst[4])
{
    dst[0] = src[0];
    dst[1] = src[1];
    dst[2] = src[2];
    dst[3] = src[3];
}

variable_types:
src
unsigned char [4]

variable_types:
dst
unsigned char [4]


decompiled_code:
__int64 __fastcall GetIndex_Rex(__int64 a1, float a2)
{
int v2; // eax

v2 = lrint((float)((float)*(int *)(a1 + 24) * a2));
return v2 - (unsigned int)(*(_DWORD *)(a1 + 24) == v2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, float a2)
{
int v2; // eax

v2 = lrint((float)((float)*(int *)(a1 + 24) * a2));
return v2 - (unsigned int)(*(_DWORD *)(a1 + 24) == v2);
}

original_code:
static inline int GetIndex_Rex(Rex_t *rex, float p) {
    int i = lrint(rex->resolution * p);
    if (i == rex->resolution)
 i--;
    return i;
}

canonical_original_code:
static inline int GetIndex_Rex(struct Rex_t *rex, float p) {
    int i = lrint(rex->resolution * p);
    if (i == rex->resolution)
 i--;
    return i;
}

variable_types: struct Rex_t { unsigned char (**value)[4]; float (**vec)[3]; int **nSamples; int resolution; } *

variable_types: p float

variable_types: i int


decompiled_code:
void Calculate_Bbox()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void Calculate_Bbox(Geometry_t *geometry) {
    switch (geometry->prim_type) {
 case SPHERE:
     break;
 case BOX:
     break;
 case TORUS:
     break;
 default:
     return ;
    }
}

canonical_original_code:
void Calculate_Bbox(struct Geometry_t *geometry) {
    switch (geometry->prim_type) {
 case SPHERE:
     break;
 case BOX:
     break;
 case TORUS:
     break;
 default:
     return ;
    }
}

variable_types: struct Geometry_t { float scale[3]; float rot[4]; float trans[3]; struct BBox_t { float corner[2][3]; } bBox; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; enum Prim_Type_t { SPHERE=0, BOX=1, TORUS=2, PLANE=3, NUM_PRIMS=4 } prim_type; union Primitive_t { struct Geo_Sphere_t { float radius; } sphere; struct Geo_Box_t { float r[3]; } box; struct Geo_Torus_t { float revRadius; float circRadius; } torus; struct Geo_Plane_t { float N[3]; float P[3]; } plane; } *primitive; struct Rex_t { unsigned char (**value)[4]; float (**vec)[3]; int **nSamples; int resolution; } *diffuse_rex; } *


original_code:
Intersection_t *Intersect_Geo(Rayf_t ray, Geometry_t *geometry) {
    Rayf_t geospaceRay;
    CopyV3f(ray.dir, geospaceRay.dir);
    CopyV3f(ray.orig, geospaceRay.orig);
    SubV3f(geospaceRay.orig, geometry->trans, geospaceRay.orig);
    Intersection_t *intersection;
    switch(geometry->prim_type) {
 case SPHERE:
     intersection = Intersect_Sphere(&geospaceRay, &(geometry->primitive->sphere));
     break;
 case BOX:
     intersection = Intersect_Box(&geospaceRay, &(geometry->primitive->box));
     break;
 case TORUS:
     intersection = Intersect_Torus(&geospaceRay, &(geometry->primitive->torus));
     break;
 case PLANE:
     intersection = Intersect_Plane(&geospaceRay, &(geometry->primitive->plane));
     break;
 default:
     ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../src/objects/geometry.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    }
    if(intersection != ((void *)0)) {
 AddV3f(intersection->point, geometry->trans, intersection->point);
 intersection->material = geometry->material;
 intersection->geo = geometry;
    }
    return intersection;
}

canonical_original_code:
struct Intersection_t *Intersect_Geo(struct Rayf_t ray, struct Geometry_t *geometry) {
    struct Rayf_t geospaceRay;
    CopyV3f(ray.dir, geospaceRay.dir);
    CopyV3f(ray.orig, geospaceRay.orig);
    SubV3f(geospaceRay.orig, geometry->trans, geospaceRay.orig);
    struct Intersection_t *intersection;
    switch(geometry->prim_type) {
 case SPHERE:
     intersection = Intersect_Sphere(&geospaceRay, &(geometry->primitive->sphere));
     break;
 case BOX:
     intersection = Intersect_Box(&geospaceRay, &(geometry->primitive->box));
     break;
 case TORUS:
     intersection = Intersect_Torus(&geospaceRay, &(geometry->primitive->torus));
     break;
 case PLANE:
     intersection = Intersect_Plane(&geospaceRay, &(geometry->primitive->plane));
     break;
 default:
     ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "../src/objects/geometry.c", 66, __extension__ __PRETTY_FUNCTION__); }));
    }
    if(intersection != ((void *)0)) {
 AddV3f(intersection->point, geometry->trans, intersection->point);
 intersection->material = geometry->material;
 intersection->geo = geometry;
    }
    return intersection;
}

variable_types: struct Rayf_t { float orig[3]; float dir[3]; }

variable_types: struct Geometry_t { float scale[3]; float rot[4]; float trans[3]; struct BBox_t { float corner[2][3]; } bBox; struct Material_t *material; enum Prim_Type_t { SPHERE=0, BOX=1, TORUS=2, PLANE=3, NUM_PRIMS=4 } prim_type; union Primitive_t { struct Geo_Sphere_t { float radius; } sphere; struct Geo_Box_t { float r[3]; } box; struct Geo_Torus_t { float revRadius; float circRadius; } torus; struct Geo_Plane_t { float N[3]; float P[3]; } plane; } *primitive; struct Rex_t { unsigned char (**value)[4]; float (**vec)[3]; int **nSamples; int resolution; } *diffuse_rex; } *

variable_types: struct Intersection_t { float t; float point[3]; float norm[3]; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; float u; float v; void *geo; } *


original_code:
void Init_Rex(Rex_t *rex, int resolution) {
    int i, j;
    rex->value = (Color_t * *)CheckMalloc(sizeof(Color_t *)*(resolution));
    rex->vec = (Vec3f_t * *)CheckMalloc(sizeof(Vec3f_t *)*(resolution));
    rex->nSamples = (int * *)CheckMalloc(sizeof(int *)*(resolution));
    for (i = 0; i < resolution; i++) {
 rex->value[i] = (Color_t *)CheckMalloc(sizeof(Color_t)*(resolution));
 rex->vec[i] = (Vec3f_t *)CheckMalloc(sizeof(Vec3f_t)*(resolution));
 rex->nSamples[i] = (int *)CheckMalloc(sizeof(int)*(resolution));
    }
    Color_t init_color = {0, 0, 0, 255};
    Vec3f_t init_vec = {0.0f, 0.0f, 0.0f};
    for (i = 0; i < resolution; i++) {
 for (j = 0; j < resolution; j++) {
     CopyColor(init_color, rex->value[i][j]);
     CopyV3f(init_vec, rex->vec[i][j]);
     rex->nSamples[i][j] = 0;
 }
    }
    rex->resolution = resolution;
}

canonical_original_code:
void Init_Rex(struct Rex_t *rex, int resolution) {
    int i; int j;
    rex->value = (unsigned char (**)[4])CheckMalloc(sizeof(unsigned char (*)[4])*(resolution));
    rex->vec = (float (**)[3])CheckMalloc(sizeof(float (*)[3])*(resolution));
    rex->nSamples = (int **)CheckMalloc(sizeof(int *)*(resolution));
    for (i = 0; i < resolution; i++) {
 rex->value[i] = (unsigned char (*)[4])CheckMalloc(sizeof(Color_t)*(resolution));
 rex->vec[i] = (float (*)[3])CheckMalloc(sizeof(Vec3f_t)*(resolution));
 rex->nSamples[i] = (int *)CheckMalloc(sizeof(int)*(resolution));
    }
    unsigned char init_color[4] = {0, 0, 0, 255};
    float init_vec[3] = {0.0f, 0.0f, 0.0f};
    for (i = 0; i < resolution; i++) {
 for (j = 0; j < resolution; j++) {
     CopyColor(init_color, rex->value[i][j]);
     CopyV3f(init_vec, rex->vec[i][j]);
     rex->nSamples[i][j] = 0;
 }
    }
    rex->resolution = resolution;
}

variable_types: resolution int

variable_types: j int

user_defined_types: struct Rex_t { unsigned char (**value)[4]; float (**vec)[3]; int **nSamples; int resolution; }


decompiled_code:
__int64 __fastcall CatchDiffuse_Rex(_QWORD *a1, _BYTE *a2, float a3, float a4)
{
int v4; // ebx
int v5; // eax

v4 = GetIndex_Rex((__int64)a1, a3);
v5 = GetIndex_Rex((__int64)a1, a4);
return CopyColor((_BYTE *)(*(_QWORD *)(*a1 + 8LL * v4) + 4LL * v5), a2);
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1, _BYTE *a2, float a3, float a4)
{
int v4; // ebx
int v5; // eax

v4 = func4((__int64)a1, a3);
v5 = func4((__int64)a1, a4);
return func3((_BYTE *)(*(_QWORD *)(*a1 + 8LL * v4) + 4LL * v5), a2);
}

original_code:
void CatchDiffuse_Rex(Rex_t *rex, float u, float v, Color_t dst) {
    int i = GetIndex_Rex(rex, u), j = GetIndex_Rex(rex, v);
    CopyColor(rex->value[i][j], dst);
}

canonical_original_code:
void CatchDiffuse_Rex(struct Rex_t *rex, float u, float v, unsigned char dst[4]) {
    int i = GetIndex_Rex(rex, u); int j = GetIndex_Rex(rex, v);
    CopyColor(rex->value[i][j], dst);
}

variable_types: u float

variable_types: v float


original_code:
void Output_Rex(Rex_t *rex, const char * fname) {
    int i, j;
    Color_t *data = (Color_t *)CheckMalloc(sizeof(Color_t)*(rex->resolution * rex->resolution));
    for (i = 0; i < rex->resolution; i++)
 for (j = 0; j < rex->resolution; j++)
     CopyColor(rex->value[i][j], data[i + rex->resolution * j]);
    Image_t *rex_image = New_Image(rex->resolution, rex->resolution, data);
    Write_Image(rex_image, fname);
}

canonical_original_code:
void Output_Rex(struct Rex_t *rex, char *fname) {
    int i; int j;
    unsigned char (*data)[4] = (Color_t *)CheckMalloc(sizeof(Color_t)*(rex->resolution * rex->resolution));
    for (i = 0; i < rex->resolution; i++)
 for (j = 0; j < rex->resolution; j++)
     CopyColor(rex->value[i][j], data[i + rex->resolution * j]);
    struct Image_t *rex_image = New_Image(rex->resolution, rex->resolution, data);
    Write_Image(rex_image, fname);
}

variable_types:
fname
char *

variable_types:
data
unsigned char (*)[4]

variable_types: struct Image_t { int width; int height; unsigned char (*data)[4]; } *


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

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


decompiled_code:
float __fastcall LengthSqV3f(float *a1)
{
return DotV3f(a1, a1);
}

canonical_decompiled_code:
float __fastcall func1(float *a1)
{
return func0(a1, a1);
}

original_code:
static inline float LengthSqV3f (Vec3f_t v)
{
    return DotV3f (v, v);
}

canonical_original_code:
static inline float LengthSqV3f(float v[3])
{
    return DotV3f (v, v);
}

variable_types:
v
float [3]


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

original_code:
static inline void ScaleV3f (float s, Vec3f_t v, Vec3f_t dst)
{
    dst[0] = s*v[0];
    dst[1] = s*v[1];
    dst[2] = s*v[2];
}

canonical_original_code:
static inline void ScaleV3f(float s, float v[3], float dst[3])
{
    dst[0] = s*v[0];
    dst[1] = s*v[1];
    dst[2] = s*v[2];
}

variable_types: s float

variable_types:
dst
float [3]


variable_types: x float

variable_types: y float

variable_types: z float


decompiled_code:
void __fastcall ProjectV3f(float *a1, float *a2, float *a3)
{
float v4; // xmm0_4

CopyV3f(a2, a3);
v4 = DotV3f(a1, a2);
ScaleV3f(a3, a3, v4);
}

canonical_decompiled_code:
void __fastcall func7(float *a1, float *a2, float *a3)
{
float v4; // xmm0_4

func3(a2, a3);
v4 = func0(a1, a2);
func2(a3, a3, v4);
}

original_code:
static inline void ProjectV3f(Vec3f_t u, Vec3f_t v, Vec3f_t dst)
{
    CopyV3f(v, dst);
    ScaleV3f(DotV3f(u, v), dst, dst);
}

canonical_original_code:
static inline void ProjectV3f(float u[3], float v[3], float dst[3])
{
    CopyV3f(v, dst);
    ScaleV3f(DotV3f(u, v), dst, dst);
}

variable_types:
u
float [3]


RayToPointf

canonical_decompiled_code:
void __fastcall func8(float *a1, float *a2, float a3)
{
func4(a1, a1 + 3, a2, a3);
}

variable_types: struct Rayf_t { float orig[3]; float dir[3]; } *

variable_types: t float


decompiled_code:
float __fastcall LengthV3f(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = LengthSqV3f(a1);
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

canonical_decompiled_code:
float __fastcall func9(float *a1)
{
float v1; // xmm0_4
float result; // xmm0_4

v1 = func1(a1);
if ( v1 < 0.0 )
result = sqrtf(v1);
else
result = fsqrt(v1);
return result;
}

original_code:
static inline float LengthV3f (Vec3f_t v)
{
    return sqrt (LengthSqV3f(v));
}

canonical_original_code:
static inline float LengthV3f(float v[3])
{
    return sqrt (LengthSqV3f(v));
}


original_code:
Intersection_t *Intersect_Plane(Rayf_t *ray, Geo_Plane_t *plane) {
    float t;
    t = - (DotV3f(plane->N, ray->orig) - DotV3f(plane->N, plane->P)) / DotV3f(plane->N, ray->dir);
    if (t > 1e-3) {
 Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t;
 RayToPointf(ray, t, intersection->point);
 CopyV3f(plane->N, intersection->norm);
 Vec3f_t v1 = {1, 0, 0}, v2;
 if (ParallelV3f(v1, plane->N)) {
     v1[0] = 0;
     v1[1] = 1;
     v1[2] = 0;
 }
 Vec3f_t v1projN;
 ProjectV3f(v1, plane->N, v1projN);
 SubV3f(v1, v1projN, v1);
 CrossV3f(v1, plane->N, v2);
 NormalizeV3f(v1);
 NormalizeV3f(v2);
 Vec3f_t pvec;
 SubV3f(intersection->point, plane->P, pvec);
 float u = DotV3f(pvec, v1), v = DotV3f(pvec, v2);
 if (u < 0)
     u--;
 else
     u++;
 u = ((1 / u) + 1) / 2;
 if (v < 0)
     v--;
 else
     v++;
 v = ((1 / v) + 1) / 2;
 intersection->u = u;
 intersection->v = v;
 return intersection;
    }
    return ((void *)0);
}

canonical_original_code:
struct Intersection_t *Intersect_Plane(struct Rayf_t *ray, struct Geo_Plane_t *plane) {
    float t;
    t = - (DotV3f(plane->N, ray->orig) - DotV3f(plane->N, plane->P)) / DotV3f(plane->N, ray->dir);
    if (t > 1e-3) {
 struct Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t;
 RayToPointf(ray, t, intersection->point);
 CopyV3f(plane->N, intersection->norm);
 float v1[3] = {1, 0, 0}; float v2[3];
 if (ParallelV3f(v1, plane->N)) {
     v1[0] = 0;
     v1[1] = 1;
     v1[2] = 0;
 }
 float v1projN[3];
 ProjectV3f(v1, plane->N, v1projN);
 SubV3f(v1, v1projN, v1);
 CrossV3f(v1, plane->N, v2);
 NormalizeV3f(v1);
 NormalizeV3f(v2);
 float pvec[3];
 SubV3f(intersection->point, plane->P, pvec);
 float u = DotV3f(pvec, v1); float v = DotV3f(pvec, v2);
 if (u < 0)
     u--;
 else
     u++;
 u = ((1 / u) + 1) / 2;
 if (v < 0)
     v--;
 else
     v++;
 v = ((1 / v) + 1) / 2;
 intersection->u = u;
 intersection->v = v;
 return intersection;
    }
    return ((void *)0);
}

variable_types: struct Geo_Plane_t { float N[3]; float P[3]; } *

variable_types: struct Intersection_t { float t; float point[3]; float norm[3]; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; float u; float v; void *geo; } *

variable_types:
pvec
float [3]


Minf

decompiled_code:
float __fastcall Minf(float a1, float a2)
{
return fminf(a1, a2);
}

canonical_decompiled_code:
float __fastcall func0(float a1, float a2)
{
return fminf(a1, a2);
}

original_code:
static inline float Minf (float a, float b) {
    if (a < b)
 return a;
    else
 return b;
}

canonical_original_code:
static inline float Minf(float a, float b) {
    if (a < b)
 return a;
    else
 return b;
}

variable_types: a float

variable_types: b float


Sqrf

decompiled_code:
float __fastcall Sqrf(float a1)
{
return a1 * a1;
}

canonical_decompiled_code:
float __fastcall func1(float a1)
{
return a1 * a1;
}

original_code: static inline float Sqrf (float x) { return (x*x); }

canonical_original_code: static inline float Sqrf(float x) { return (x*x); }


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


canonical_decompiled_code:
float __fastcall func3(float *a1)
{
return func2(a1, a1);
}


original_code:
static inline void ScaledAddV3f (Vec3f_t u, float s, Vec3f_t v, Vec3f_t dst)
{
    dst[0] = u[0] + s*v[0];
    dst[1] = u[1] + s*v[1];
    dst[2] = u[2] + s*v[2];
}

canonical_original_code:
static inline void ScaledAddV3f(float u[3], float s, float v[3], float dst[3])
{
    dst[0] = u[0] + s*v[0];
    dst[1] = u[1] + s*v[1];
    dst[2] = u[2] + s*v[2];
}


RayToPointf

canonical_decompiled_code:
void __fastcall func7(float *a1, float *a2, float a3)
{
func6(a1, a1 + 3, a2, a3);
}

original_code:
static inline void RayToPointf (Rayf_t *r, float t, Vec3f_t dst)
{
    ScaledAddV3f (r->orig, t, r->dir, dst);
}

canonical_original_code:
static inline void RayToPointf(struct Rayf_t *r, float t, float dst[3])
{
    ScaledAddV3f (r->orig, t, r->dir, dst);
}


original_code:
Intersection_t *Intersect_Sphere(Rayf_t *ray, Geo_Sphere_t *sphere) {
    float a = LengthSqV3f(ray->dir);
    float b = 2 * DotV3f(ray->dir, ray->orig);
    float c = LengthSqV3f(ray->orig) - Sqrf(sphere->radius);
    float D = Sqrf(b) - (4 * a * c);
    if (D >= 0) {
 float t1 = (-b - sqrtf(D)) / (2 * a), t2 = (-b + sqrtf(D)) / (2 * a);
 float t;
 if (t1 <= 1e-3 || t2 <= 1e-3)
     return ((void *)0);
 else if (t1 <= 1e-3)
     t = t2;
 else if (t2 <= 1e-3)
     t = t1;
 else
     t = Minf(t1, t2);
 Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t;
 RayToPointf(ray, t, intersection->point);
 CopyV3f(intersection->point, intersection->norm);
 NormalizeV3f(intersection->norm);
 intersection->u = ((atan2(intersection->norm[1], intersection->norm[0]) / 3.142) + 1) / 2;
 intersection->v = (intersection->norm[2] + 1) / 2;
 return intersection;
    }
    return ((void *)0);
}

canonical_original_code:
struct Intersection_t *Intersect_Sphere(struct Rayf_t *ray, struct Geo_Sphere_t *sphere) {
    float a = LengthSqV3f(ray->dir);
    float b = 2 * DotV3f(ray->dir, ray->orig);
    float c = LengthSqV3f(ray->orig) - Sqrf(sphere->radius);
    float D = Sqrf(b) - (4 * a * c);
    if (D >= 0) {
 float t1 = (-b - sqrtf(D)) / (2 * a); float t2 = (-b + sqrtf(D)) / (2 * a);
 float t;
 if (t1 <= 1e-3 || t2 <= 1e-3)
     return ((void *)0);
 else if (t1 <= 1e-3)
     t = t2;
 else if (t2 <= 1e-3)
     t = t1;
 else
     t = Minf(t1, t2);
 struct Intersection_t *intersection = (Intersection_t *)CheckMalloc(sizeof(Intersection_t));
 intersection->t = t;
 RayToPointf(ray, t, intersection->point);
 CopyV3f(intersection->point, intersection->norm);
 NormalizeV3f(intersection->norm);
 intersection->u = ((atan2(intersection->norm[1], intersection->norm[0]) / 3.142) + 1) / 2;
 intersection->v = (intersection->norm[2] + 1) / 2;
 return intersection;
    }
    return ((void *)0);
}

variable_types:
sphere
struct Geo_Sphere_t { float radius; } *

variable_types: c float

variable_types: D float

return_type: struct Intersection_t { float t; float point[3]; float norm[3]; struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; } *material; float u; float v; void *geo; } *


GetTime

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

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) * 0.000001 + (double)SLODWORD(v1.tv_sec);
}

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

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) * 0.000001 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double GetTime ()
{
    struct timeval t;
    gettimeofday (&t, 0);
    return (double)t.tv_sec + 0.000001*(double)t.tv_usec;
}

canonical_original_code:
double GetTime()
{
    struct timeval t;
    gettimeofday (&t, 0);
    return (double)t.tv_sec + 0.000001*(double)t.tv_usec;
}

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

return_type: double


CheckMalloc

decompiled_code:
void *__fastcall CheckMalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "Fatel error: unable to allocate %d bytes\n", (unsigned int)a1);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fprintf(stderr, "Fatel error: unable to allocate %d bytes\n", (unsigned int)a1);
exit(1);
}
return result;
}

original_code:
void *CheckMalloc (size_t nbytes)
{
    void *obj = malloc(nbytes);
    if (obj == 0) {
 fprintf(stderr, "Fatel error: unable to allocate %d bytes\n", (int)nbytes);
 exit (1);
    }
    return obj;
}

canonical_original_code:
void *CheckMalloc(unsigned long nbytes)
{
    void *obj = malloc(nbytes);
    if (obj == 0) {
 fprintf(stderr, "Fatel error: unable to allocate %d bytes\n", (int)nbytes);
 exit (1);
    }
    return obj;
}

variable_types:
nbytes
unsigned long

variable_types:
obj
void *


decompiled_code:
__int64 __fastcall New_Image(int a1, int a2, __int64 a3)
{
__int64 result; // rax

result = CheckMalloc(16LL);
*(_DWORD *)result = a1;
*(_DWORD *)(result + 4) = a2;
*(_QWORD *)(result + 8) = a3;
return result;
}

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

result = CheckMalloc(16LL);
*(_DWORD *)result = a1;
*(_DWORD *)(result + 4) = a2;
*(_QWORD *)(result + 8) = a3;
return result;
}

original_code:
Image_t *New_Image(int width, int height, Color_t *data) {
    Image_t *image = (Image_t *)CheckMalloc(sizeof(Image_t));
    image->width = width;
    image->height = height;
    image->data = data;
    return image;
}

canonical_original_code:
struct Image_t *New_Image(int width, int height, unsigned char (*data)[4]) {
    struct Image_t *image = (Image_t *)CheckMalloc(sizeof(Image_t));
    image->width = width;
    image->height = height;
    image->data = data;
    return image;
}

variable_types: width int

variable_types: height int

variable_types:
data
unsigned char (*)[4]

variable_types: struct Image_t { int width; int height; unsigned char (*data)[4]; } *


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

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

original_code:
void Delete_Image(Image_t *image) {
    free(image->data);
    free(image);
}

canonical_original_code:
void Delete_Image(struct Image_t *image) {
    free(image->data);
    free(image);
}

user_defined_types: struct Image_t { int width; int height; unsigned char (*data)[4]; }


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

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

original_code:
Intersection_t *Intersect_Torus(Rayf_t *ray, Geo_Torus_t *torus) {
    return ((void *)0);
}

canonical_original_code:
struct Intersection_t *Intersect_Torus(struct Rayf_t *ray, struct Geo_Torus_t *torus) {
    return ((void *)0);
}

variable_types: struct Rayf_t { float orig[3]; float dir[3]; } *

variable_types: struct Geo_Torus_t { float revRadius; float circRadius; } *

user_defined_types: struct Material_t { unsigned char diffuse_color[4]; unsigned char spec_color[4]; float spec; float glossiness; float transparency; float reflection; float refraction; }


repo: jdourlens/FirmataC

decompiled_code:
_DWORD *serial_new()
{
_DWORD *result; // rax

result = malloc(0xA8uLL);
if ( result )
{
*result = 0;
result[4] = 38400;
result[39] = 0;
result[40] = 0;
}
return result;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *result; // rax

result = malloc(0xA8uLL);
if ( result )
{
*result = 0;
result[4] = 38400;
result[39] = 0;
result[40] = 0;
}
return result;
}

original_code:
t_serial *serial_new()
{
  t_serial *res;
  res = malloc(sizeof(t_serial));
  if (!res)
    {
      return (((void *)0));
    }
  res->port_is_open = 0;
  res->baud_rate = 38400;
  res->tx = 0;
  res->rx = 0;
  return (res);
}

canonical_original_code:
struct s_serial *serial_new()
{
  struct s_serial *res;
  res = malloc(sizeof(t_serial));
  if (!res)
    {
      return (((void *)0));
    }
  res->port_is_open = 0;
  res->baud_rate = 38400;
  res->tx = 0;
  res->rx = 0;
  return (res);
}

variable_types: struct s_serial { int port_is_open; char *port_name; int baud_rate; char *error_msg; int port_fd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } settings_orig; struct termios settings; int tx; int rx; } *

user_defined_types: struct s_serial { int port_is_open; char *port_name; int baud_rate; char *error_msg; int port_fd; struct termios settings_orig; struct termios settings; int tx; int rx; }


decompiled_code:
__int64 __fastcall serial_setBaud(__int64 a1, int a2)
{
speed_t v2; // ebx
__int64 result; // rax

if ( a2 == 57600 )
{
v2 = 4097;
goto LABEL_40;
}
if ( a2 <= 57600 )
{
if ( a2 == 600 )
{
v2 = 8;
}
else if ( a2 <= 600 )
{
if ( a2 == 134 )
{
v2 = 4;
}
else if ( a2 <= 134 )
{
switch ( a2 )
{
case 'K':
v2 = 2;
break;
case 'n':
v2 = 3;
break;
case '2':
v2 = 1;
break;
default:
return 0xFFFFFFFFLL;
}
}
else
{
switch ( a2 )
{
case 200:
v2 = 6;
break;
case 300:
v2 = 7;
break;
case 150:
v2 = 5;
break;
default:
return 0xFFFFFFFFLL;
}
}
}
else if ( a2 == 4800 )
{
v2 = 12;
}
else if ( a2 <= 4800 )
{
switch ( a2 )
{
case 1800:
v2 = 10;
break;
case 2400:
v2 = 11;
break;
case 1200:
v2 = 9;
break;
default:
return 0xFFFFFFFFLL;
}
}
else
{
switch ( a2 )
{
case 19200:
v2 = 14;
break;
case 38400:
v2 = 15;
break;
case 9600:
v2 = 13;
break;
default:
return 0xFFFFFFFFLL;
}
}
goto LABEL_40;
}
if ( a2 == 1152000 )
{
v2 = 4105;
goto LABEL_40;
}
if ( a2 <= 1152000 )
{
if ( a2 == 500000 )
{
v2 = 4101;
goto LABEL_40;
}
if ( a2 <= 500000 )
{
switch ( a2 )
{
case 230400:
v2 = 4099;
goto LABEL_40;
case 460800:
v2 = 4100;
goto LABEL_40;
case 115200:
v2 = 4098;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
else
{
switch ( a2 )
{
case 921600:
v2 = 4103;
goto LABEL_40;
case 1000000:
v2 = 4104;
goto LABEL_40;
case 576000:
v2 = 4102;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
}
else
{
if ( a2 == 3000000 )
{
v2 = 4109;
goto LABEL_40;
}
if ( a2 <= 3000000 )
{
switch ( a2 )
{
case 2000000:
v2 = 4107;
goto LABEL_40;
case 2500000:
v2 = 4108;
goto LABEL_40;
case 1500000:
v2 = 4106;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
else
{
if ( a2 == 3500000 )
{
v2 = 4110;
goto LABEL_40;
}
if ( a2 == 4000000 )
{
v2 = 4111;
LABEL_40:
cfsetospeed((struct termios *)(a1 + 96), v2);
cfsetispeed((struct termios *)(a1 + 96), v2);
return (unsigned int)(tcsetattr(*(_DWORD *)(a1 + 32), 0, (const struct termios *)(a1 + 96)) >> 31);
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
speed_t v2; // ebx
__int64 result; // rax

if ( a2 == 57600 )
{
v2 = 4097;
goto LABEL_40;
}
if ( a2 <= 57600 )
{
if ( a2 == 600 )
{
v2 = 8;
}
else if ( a2 <= 600 )
{
if ( a2 == 134 )
{
v2 = 4;
}
else if ( a2 <= 134 )
{
switch ( a2 )
{
case 'K':
v2 = 2;
break;
case 'n':
v2 = 3;
break;
case '2':
v2 = 1;
break;
default:
return 0xFFFFFFFFLL;
}
}
else
{
switch ( a2 )
{
case 200:
v2 = 6;
break;
case 300:
v2 = 7;
break;
case 150:
v2 = 5;
break;
default:
return 0xFFFFFFFFLL;
}
}
}
else if ( a2 == 4800 )
{
v2 = 12;
}
else if ( a2 <= 4800 )
{
switch ( a2 )
{
case 1800:
v2 = 10;
break;
case 2400:
v2 = 11;
break;
case 1200:
v2 = 9;
break;
default:
return 0xFFFFFFFFLL;
}
}
else
{
switch ( a2 )
{
case 19200:
v2 = 14;
break;
case 38400:
v2 = 15;
break;
case 9600:
v2 = 13;
break;
default:
return 0xFFFFFFFFLL;
}
}
goto LABEL_40;
}
if ( a2 == 1152000 )
{
v2 = 4105;
goto LABEL_40;
}
if ( a2 <= 1152000 )
{
if ( a2 == 500000 )
{
v2 = 4101;
goto LABEL_40;
}
if ( a2 <= 500000 )
{
switch ( a2 )
{
case 230400:
v2 = 4099;
goto LABEL_40;
case 460800:
v2 = 4100;
goto LABEL_40;
case 115200:
v2 = 4098;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
else
{
switch ( a2 )
{
case 921600:
v2 = 4103;
goto LABEL_40;
case 1000000:
v2 = 4104;
goto LABEL_40;
case 576000:
v2 = 4102;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
}
else
{
if ( a2 == 3000000 )
{
v2 = 4109;
goto LABEL_40;
}
if ( a2 <= 3000000 )
{
switch ( a2 )
{
case 2000000:
v2 = 4107;
goto LABEL_40;
case 2500000:
v2 = 4108;
goto LABEL_40;
case 1500000:
v2 = 4106;
goto LABEL_40;
}
result = 0xFFFFFFFFLL;
}
else
{
if ( a2 == 3500000 )
{
v2 = 4110;
goto LABEL_40;
}
if ( a2 == 4000000 )
{
v2 = 4111;
LABEL_40:
cfsetospeed((struct termios *)(a1 + 96), v2);
cfsetispeed((struct termios *)(a1 + 96), v2);
return (unsigned int)(tcsetattr(*(_DWORD *)(a1 + 32), 0, (const struct termios *)(a1 + 96)) >> 31);
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_original_code:
int serial_setBaud(struct s_serial *serial, int baud)
{
  unsigned int spd;
  switch (baud) {
  case 230400: spd = 0010003; break;
  case 115200: spd = 0010002; break;
  case 57600: spd = 0010001; break;
  case 38400: spd = 0000017; break;
  case 19200: spd = 0000016; break;
  case 9600: spd = 0000015; break;
  case 4800: spd = 0000014; break;
  case 2400: spd = 0000013; break;
  case 1800: spd = 0000012; break;
  case 1200: spd = 0000011; break;
  case 600: spd = 0000010; break;
  case 300: spd = 0000007; break;
  case 200: spd = 0000006; break;
  case 150: spd = 0000005; break;
  case 134: spd = 0000004; break;
  case 110: spd = 0000003; break;
  case 75: spd = 0000002; break;
  case 50: spd = 0000001; break;
  case 460800: spd = 0010004; break;
  case 500000: spd = 0010005; break;
  case 576000: spd = 0010006; break;
  case 921600: spd = 0010007; break;
  case 1000000: spd = 0010010; break;
  case 1152000: spd = 0010011; break;
  case 1500000: spd = 0010012; break;
  case 2000000: spd = 0010013; break;
  case 2500000: spd = 0010014; break;
  case 3000000: spd = 0010015; break;
  case 3500000: spd = 0010016; break;
  case 4000000: spd = 0010017; break;
  default: {
    return -1;
  }
  }
  cfsetospeed(&(serial->settings), spd);
  cfsetispeed(&(serial->settings), spd);
  if (tcsetattr(serial->port_fd, 0, &(serial->settings)) < 0)
    return (-1);
  return (0);
}

variable_types: baud int

variable_types:
spd
unsigned int

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
__int64 __fastcall serial_open(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // er12
int v5; // eax
int v6; // [rsp+Ch] [rbp-6Ch] BYREF
char v7[16]; // [rsp+10h] [rbp-68h] BYREF
int v8; // [rsp+20h] [rbp-58h]

v2 = open(a2, 2306);
*(_DWORD *)(a1 + 32) = v2;
if ( v2 < 0 )
{
v5 = *_errno_location();
if ( v5 == 13 )
{
perror("Unable to access, insufficient permission");
v3 = -1;
}
else if ( v5 == 21 )
{
perror("Unable to open, Object is a directory, not a serial port");
v3 = -1;
}
else if ( v5 == 19 || v5 == 6 )
{
perror("Unable to open, Serial port hardware not installed");
v3 = -1;
}
else
{
if ( v5 == 2 )
perror("Unable to open, Device name does not exist");
else
perror("Unable to open: Unknown error.");
v3 = -1;
}
}
else if ( ioctl(v2, 0x5415uLL, &v6) < 0 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to query serial port signals");
v3 = -1;
}
else
{
v6 &= 0xFFFFFFF9;
if ( ioctl(*(_DWORD *)(a1 + 32), 0x5418uLL, &v6) < 0 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to control serial port signals");
v3 = -1;
}
else
{
v3 = tcgetattr(*(_DWORD *)(a1 + 32), (struct termios *)(a1 + 36));
if ( v3 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to query serial port settings (perhaps not a serial port)");
v3 = -1;
}
else
{
*(_QWORD *)(a1 + 96) = 0LL;
*(_QWORD *)(a1 + 104) = 0LL;
*(_QWORD *)(a1 + 112) = 0LL;
*(_QWORD *)(a1 + 120) = 0LL;
*(_QWORD *)(a1 + 128) = 0LL;
*(_QWORD *)(a1 + 136) = 0LL;
*(_QWORD *)(a1 + 144) = 0LL;
*(_DWORD *)(a1 + 152) = 0;
*(_DWORD *)(a1 + 96) = 5;
*(_DWORD *)(a1 + 104) = 3248;
serial_setBaud(a1, *(_DWORD *)(a1 + 16));
if ( !ioctl(*(_DWORD *)(a1 + 32), 0x541EuLL, v7) )
{
v8 |= 0x2000u;
ioctl(*(_DWORD *)(a1 + 32), 0x541FuLL, v7);
}
tcflush(*(_DWORD *)(a1 + 32), 0);
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)a1 = 1;
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // er12
int v5; // eax
int v6; // [rsp+Ch] [rbp-6Ch] BYREF
char v7[16]; // [rsp+10h] [rbp-68h] BYREF
int v8; // [rsp+20h] [rbp-58h]

v2 = open(a2, 2306);
*(_DWORD *)(a1 + 32) = v2;
if ( v2 < 0 )
{
v5 = *_errno_location();
if ( v5 == 13 )
{
perror("Unable to access, insufficient permission");
v3 = -1;
}
else if ( v5 == 21 )
{
perror("Unable to open, Object is a directory, not a serial port");
v3 = -1;
}
else if ( v5 == 19 || v5 == 6 )
{
perror("Unable to open, Serial port hardware not installed");
v3 = -1;
}
else
{
if ( v5 == 2 )
perror("Unable to open, Device name does not exist");
else
perror("Unable to open: Unknown error.");
v3 = -1;
}
}
else if ( ioctl(v2, 0x5415uLL, &v6) < 0 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to query serial port signals");
v3 = -1;
}
else
{
v6 &= 0xFFFFFFF9;
if ( ioctl(*(_DWORD *)(a1 + 32), 0x5418uLL, &v6) < 0 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to control serial port signals");
v3 = -1;
}
else
{
v3 = tcgetattr(*(_DWORD *)(a1 + 32), (struct termios *)(a1 + 36));
if ( v3 )
{
close(*(_DWORD *)(a1 + 32));
perror("Unable to query serial port settings (perhaps not a serial port)");
v3 = -1;
}
else
{
*(_QWORD *)(a1 + 96) = 0LL;
*(_QWORD *)(a1 + 104) = 0LL;
*(_QWORD *)(a1 + 112) = 0LL;
*(_QWORD *)(a1 + 120) = 0LL;
*(_QWORD *)(a1 + 128) = 0LL;
*(_QWORD *)(a1 + 136) = 0LL;
*(_QWORD *)(a1 + 144) = 0LL;
*(_DWORD *)(a1 + 152) = 0;
*(_DWORD *)(a1 + 96) = 5;
*(_DWORD *)(a1 + 104) = 3248;
func1(a1, *(_DWORD *)(a1 + 16));
if ( !ioctl(*(_DWORD *)(a1 + 32), 0x541EuLL, v7) )
{
v8 |= 0x2000u;
ioctl(*(_DWORD *)(a1 + 32), 0x541FuLL, v7);
}
tcflush(*(_DWORD *)(a1 + 32), 0);
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)a1 = 1;
}
}
}
return v3;
}

original_code:
int serial_open(t_serial *serial, char *name)
{
  struct serial_struct kernel_serial_settings;
  int bits;
  serial->port_fd = open(name, 02 | 0400 | 04000);
  if (serial->port_fd < 0)
    {
      if ((*__errno_location ()) == 13)
 {
   perror("Unable to access, insufficient permission");
 }
      else if ((*__errno_location ()) == 21)
 {
   perror("Unable to open, Object is a directory, not a serial port");
 }
      else if ((*__errno_location ()) == 19 || (*__errno_location ()) == 6)
 {
   perror("Unable to open, Serial port hardware not installed");
 }
      else if ((*__errno_location ()) == 2)
 {
   perror("Unable to open, Device name does not exist");
 }
      else
 {
   perror("Unable to open: Unknown error.");
 }
      return (-1);
    }
  if (ioctl(serial->port_fd, 0x5415, &bits) < 0)
    {
      close(serial->port_fd);
      perror("Unable to query serial port signals");
      return (-1);
    }
  bits &= ~(0x002 | 0x004);
  if (ioctl(serial->port_fd, 0x5418, &bits) < 0)
    {
      close(serial->port_fd);
      perror("Unable to control serial port signals");
      return (-1);
    }
  if (tcgetattr(serial->port_fd, &(serial->settings_orig)) != 0)
    {
      close(serial->port_fd);
      perror("Unable to query serial port settings (perhaps not a serial port)");
      return (-1);
    }
  memset(&(serial->settings), 0, sizeof(struct termios));
  serial->settings.c_iflag = 0000001 | 0000004;
  serial->settings.c_cflag = 0000060 | 0000200 | 0002000 | 0004000;
  serial_setBaud(serial, serial->baud_rate);
  if (ioctl(serial->port_fd, 0x541E, &kernel_serial_settings) == 0)
    {
      kernel_serial_settings.flags |= (1U << 13);
      ioctl(serial->port_fd, 0x541F, &kernel_serial_settings);
    }
  tcflush(serial->port_fd, 0);
  serial->port_name = name;
  serial->port_is_open = 1;
  return 0;
}

canonical_original_code:
int serial_open(struct s_serial *serial, char *name)
{
  struct serial_struct kernel_serial_settings;
  int bits;
  serial->port_fd = open(name, 02 | 0400 | 04000);
  if (serial->port_fd < 0)
    {
      if ((*__errno_location ()) == 13)
 {
   perror("Unable to access, insufficient permission");
 }
      else if ((*__errno_location ()) == 21)
 {
   perror("Unable to open, Object is a directory, not a serial port");
 }
      else if ((*__errno_location ()) == 19 || (*__errno_location ()) == 6)
 {
   perror("Unable to open, Serial port hardware not installed");
 }
      else if ((*__errno_location ()) == 2)
 {
   perror("Unable to open, Device name does not exist");
 }
      else
 {
   perror("Unable to open: Unknown error.");
 }
      return (-1);
    }
  if (ioctl(serial->port_fd, 0x5415, &bits) < 0)
    {
      close(serial->port_fd);
      perror("Unable to query serial port signals");
      return (-1);
    }
  bits &= ~(0x002 | 0x004);
  if (ioctl(serial->port_fd, 0x5418, &bits) < 0)
    {
      close(serial->port_fd);
      perror("Unable to control serial port signals");
      return (-1);
    }
  if (tcgetattr(serial->port_fd, &(serial->settings_orig)) != 0)
    {
      close(serial->port_fd);
      perror("Unable to query serial port settings (perhaps not a serial port)");
      return (-1);
    }
  memset(&(serial->settings), 0, sizeof(struct termios));
  serial->settings.c_iflag = 0000001 | 0000004;
  serial->settings.c_cflag = 0000060 | 0000200 | 0002000 | 0004000;
  serial_setBaud(serial, serial->baud_rate);
  if (ioctl(serial->port_fd, 0x541E, &kernel_serial_settings) == 0)
    {
      kernel_serial_settings.flags |= (1U << 13);
      ioctl(serial->port_fd, 0x541F, &kernel_serial_settings);
    }
  tcflush(serial->port_fd, 0);
  serial->port_name = name;
  serial->port_is_open = 1;
  return 0;
}

variable_types:
name
char *

variable_types: struct serial_struct { int type; int line; unsigned int port; int irq; int flags; int xmit_fifo_size; int custom_divisor; int baud_base; unsigned short close_delay; char io_type; char reserved_char[1]; int hub6; unsigned short closing_wait; unsigned short closing_wait2; unsigned char *iomem_base; unsigned short iomem_reg_shift; unsigned int port_high; unsigned long iomap_base; }

variable_types: bits int


decompiled_code:
__int64 __fastcall serial_read(_DWORD *a1, void *a2, int a3)
{
unsigned int v3; // er12
int v4; // eax
int v5; // ebp
int v7; // eax
char v8[28]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( !*a1 )
return (unsigned int)-1;
v3 = 0;
if ( a3 > 0 )
{
v4 = read(a1[8], a2, a3);
v5 = v4;
v3 = v4;
if ( v4 < 0 )
{
v7 = *_errno_location();
if ( v7 == 11 || v7 == 4 )
return 0;
}
else if ( !v4 && ioctl(a1[8], 0x5415uLL, v8) < 0 )
{
return (unsigned int)-99;
}
a1[40] += v5;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, void *a2, int a3)
{
unsigned int v3; // er12
int v4; // eax
int v5; // ebp
int v7; // eax
char v8[28]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( !*a1 )
return (unsigned int)-1;
v3 = 0;
if ( a3 > 0 )
{
v4 = read(a1[8], a2, a3);
v5 = v4;
v3 = v4;
if ( v4 < 0 )
{
v7 = *_errno_location();
if ( v7 == 11 || v7 == 4 )
return 0;
}
else if ( !v4 && ioctl(a1[8], 0x5415uLL, v8) < 0 )
{
return (unsigned int)-99;
}
a1[40] += v5;
}
return v3;
}

original_code:
int serial_read(t_serial *serial, void *ptr, int count)
{
  int n, bits;
  if (!serial->port_is_open)
    return (-1);
  if (count <= 0)
    return (0);
  n = read(serial->port_fd, ptr, count);
  if (n < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 4))
    return (0);
  if (n == 0 && ioctl(serial->port_fd, 0x5415, &bits) < 0)
    return (-99);
  serial->rx += n;
  return (n);
}

canonical_original_code:
int serial_read(struct s_serial *serial, void *ptr, int count)
{
  int n; int bits;
  if (!serial->port_is_open)
    return (-1);
  if (count <= 0)
    return (0);
  n = read(serial->port_fd, ptr, count);
  if (n < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 4))
    return (0);
  if (n == 0 && ioctl(serial->port_fd, 0x5415, &bits) < 0)
    return (-99);
  serial->rx += n;
  return (n);
}

variable_types:
ptr
void *

variable_types: count int

variable_types: n int


decompiled_code:
__int64 __fastcall serial_write(_DWORD *a1, __int64 a2, int a3)
{
int v5; // ebx
int v6; // eax
int v7; // eax
int v9; // edi
int v10; // eax
struct timeval v11; // [rsp+0h] [rbp-B8h] BYREF
fd_set writefds; // [rsp+10h] [rbp-A8h] BYREF

if ( !*a1 )
return (unsigned int)-1;
if ( a3 > 0 )
{
v5 = 0;
while ( 1 )
{
v6 = write(a1[8], (const void *)(a2 + v5), a3 - v5);
if ( v6 >= 0 )
{
if ( v6 > 0 )
{
v5 += v6;
goto LABEL_6;
}
}
else
{
v7 = *_errno_location();
if ( v7 != 11 && v7 != 4 )
return (unsigned int)-1;
}
v11.tv_sec = 10LL;
v11.tv_usec = 0LL;
memset(&writefds, 0, sizeof(writefds));
v9 = a1[8];
writefds.fds_bits[v9 / 64] |= 1LL << (v9 % 64);
v10 = select(v9 + 1, 0LL, &writefds, 0LL, &v11);
if ( v10 < 0 )
{
if ( *_errno_location() != 4 )
return (unsigned int)-1;
}
else if ( !v10 )
{
return (unsigned int)-1;
}
LABEL_6:
if ( v5 >= a3 )
goto LABEL_18;
}
}
v5 = 0;
LABEL_18:
a1[39] += v5;
return (unsigned int)v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(_DWORD *a1, __int64 a2, int a3)
{
int v5; // ebx
int v6; // eax
int v7; // eax
int v9; // edi
int v10; // eax
struct timeval v11; // [rsp+0h] [rbp-B8h] BYREF
fd_set writefds; // [rsp+10h] [rbp-A8h] BYREF

if ( !*a1 )
return (unsigned int)-1;
if ( a3 > 0 )
{
v5 = 0;
while ( 1 )
{
v6 = write(a1[8], (const void *)(a2 + v5), a3 - v5);
if ( v6 >= 0 )
{
if ( v6 > 0 )
{
v5 += v6;
goto LABEL_6;
}
}
else
{
v7 = *_errno_location();
if ( v7 != 11 && v7 != 4 )
return (unsigned int)-1;
}
v11.tv_sec = 10LL;
v11.tv_usec = 0LL;
memset(&writefds, 0, sizeof(writefds));
v9 = a1[8];
writefds.fds_bits[v9 / 64] |= 1LL << (v9 % 64);
v10 = select(v9 + 1, 0LL, &writefds, 0LL, &v11);
if ( v10 < 0 )
{
if ( *_errno_location() != 4 )
return (unsigned int)-1;
}
else if ( !v10 )
{
return (unsigned int)-1;
}
LABEL_6:
if ( v5 >= a3 )
goto LABEL_18;
}
}
v5 = 0;
LABEL_18:
a1[39] += v5;
return (unsigned int)v5;
}

original_code:
int serial_write(t_serial *serial, void *ptr, int len)
{
  if (!serial->port_is_open) return -1;
  int n, written=0;
  fd_set wfds;
  struct timeval tv;
  while (written < len) {
    n = write(serial->port_fd, (const char *)ptr + written, len - written);
    if (n < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)) n = 0;
    if (n < 0) return -1;
    if (n > 0) {
      written += n;
    } else {
      tv.tv_sec = 10;
      tv.tv_usec = 0;
      do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
      ((void) (((&wfds)->__fds_bits)[((serial->port_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial->port_fd) % (8 * (int) sizeof (__fd_mask)))))));
      n = select(serial->port_fd+1, ((void *)0), &wfds, ((void *)0), &tv);
      if (n < 0 && (*__errno_location ()) == 4) n = 1;
      if (n <= 0) return -1;
    }
  }
  serial->tx += written;
  return (written);
}

canonical_original_code:
int serial_write(struct s_serial *serial, void *ptr, int len)
{
  if (!serial->port_is_open) return -1;
  int n; int written = 0;
  struct fd_set wfds;
  struct timeval tv;
  while (written < len) {
    n = write(serial->port_fd, (char *)ptr + written, len - written);
    if (n < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)) n = 0;
    if (n < 0) return -1;
    if (n > 0) {
      written += n;
    } else {
      tv.tv_sec = 10;
      tv.tv_usec = 0;
      do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
      ((void) (((&wfds)->__fds_bits)[((serial->port_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial->port_fd) % (8 * (int) sizeof (__fd_mask)))))));
      n = select(serial->port_fd+1, ((void *)0), &wfds, ((void *)0), &tv);
      if (n < 0 && (*__errno_location ()) == 4) n = 1;
      if (n <= 0) return -1;
    }
  }
  serial->tx += written;
  return (written);
}

variable_types: len int

variable_types: written int

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

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


decompiled_code:
int __fastcall serial_waitInput(_DWORD *a1, int a2)
{
int v2; // edi
struct timeval v4; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

if ( !*a1 )
return -1;
v4.tv_sec = a2 / 1000;
v4.tv_usec = 1000 * (a2 % 1000);
memset(&readfds, 0, sizeof(readfds));
v2 = a1[8];
readfds.fds_bits[v2 / 64] |= 1LL << (v2 % 64);
return select(v2 + 1, &readfds, 0LL, 0LL, &v4);
}

canonical_decompiled_code:
int __fastcall func5(_DWORD *a1, int a2)
{
int v2; // edi
struct timeval v4; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

if ( !*a1 )
return -1;
v4.tv_sec = a2 / 1000;
v4.tv_usec = 1000 * (a2 % 1000);
memset(&readfds, 0, sizeof(readfds));
v2 = a1[8];
readfds.fds_bits[v2 / 64] |= 1LL << (v2 % 64);
return select(v2 + 1, &readfds, 0LL, 0LL, &v4);
}

original_code:
int serial_waitInput(t_serial *serial, int msec)
{
  if (!serial->port_is_open) return -1;
  fd_set rfds;
  struct timeval tv;
  tv.tv_sec = msec / 1000;
  tv.tv_usec = (msec % 1000) * 1000;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((serial->port_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial->port_fd) % (8 * (int) sizeof (__fd_mask)))))));
  return (select(serial->port_fd+1, &rfds, ((void *)0), ((void *)0), &tv));
}

canonical_original_code:
int serial_waitInput(struct s_serial *serial, int msec)
{
  if (!serial->port_is_open) return -1;
  struct fd_set rfds;
  struct timeval tv;
  tv.tv_sec = msec / 1000;
  tv.tv_usec = (msec % 1000) * 1000;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&rfds)->__fds_bits)[((serial->port_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((serial->port_fd) % (8 * (int) sizeof (__fd_mask)))))));
  return (select(serial->port_fd+1, &rfds, ((void *)0), ((void *)0), &tv));
}

variable_types: msec int

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

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


decompiled_code:
int __fastcall serial_discardInput(_DWORD *a1)
{
int result; // eax

if ( *a1 )
result = tcflush(a1[8], 0);
return result;
}

canonical_decompiled_code:
int __fastcall func6(_DWORD *a1)
{
int result; // eax

if ( *a1 )
result = tcflush(a1[8], 0);
return result;
}

original_code:
int serial_discardInput(t_serial *serial)
{
  if (!serial->port_is_open) return;
  tcflush(serial->port_fd, 0);
}

canonical_original_code:
int serial_discardInput(struct s_serial *serial)
{
  if (!serial->port_is_open) return;
  tcflush(serial->port_fd, 0);
}


decompiled_code:
int __fastcall serial_flushOutput(_DWORD *a1)
{
int result; // eax

if ( *a1 )
result = tcdrain(a1[8]);
return result;
}

canonical_decompiled_code:
int __fastcall func7(_DWORD *a1)
{
int result; // eax

if ( *a1 )
result = tcdrain(a1[8]);
return result;
}

original_code:
void serial_flushOutput(t_serial *serial)
{
  if (!serial->port_is_open) return;
  tcdrain(serial->port_fd);
}

canonical_original_code:
void serial_flushOutput(struct s_serial *serial)
{
  if (!serial->port_is_open) return;
  tcdrain(serial->port_fd);
}


decompiled_code:
__int64 __fastcall serial_setControl(_DWORD *a1, int a2, int a3)
{
int v5[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( !*a1 )
return 0xFFFFFFFFLL;
if ( ioctl(a1[8], 0x5415uLL, v5) < 0 )
return 0xFFFFFFFFLL;
if ( a2 == 1 )
{
v5[0] |= 2u;
}
else if ( !a2 )
{
v5[0] &= 0xFFFFFFFD;
}
if ( a3 == 1 )
{
v5[0] |= 4u;
}
else if ( !a3 )
{
v5[0] &= 0xFFFFFFFB;
}
return (unsigned int)(ioctl(a1[8], 0x5418uLL, v5) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1, int a2, int a3)
{
int v5[7]; // [rsp+0h] [rbp-1Ch] BYREF

if ( !*a1 )
return 0xFFFFFFFFLL;
if ( ioctl(a1[8], 0x5415uLL, v5) < 0 )
return 0xFFFFFFFFLL;
if ( a2 == 1 )
{
v5[0] |= 2u;
}
else if ( !a2 )
{
v5[0] &= 0xFFFFFFFD;
}
if ( a3 == 1 )
{
v5[0] |= 4u;
}
else if ( !a3 )
{
v5[0] &= 0xFFFFFFFB;
}
return (unsigned int)(ioctl(a1[8], 0x5418uLL, v5) >> 31);
}

original_code:
int serial_setControl(t_serial *serial, int dtr, int rts)
{
  if (!serial->port_is_open) return -1;
  int bits;
  if (ioctl(serial->port_fd, 0x5415, &bits) < 0) return -1;
  if (dtr == 1) {
    bits |= 0x002;
  } else if (dtr == 0) {
    bits &= ~0x002;
  }
  if (rts == 1) {
    bits |= 0x004;
  } else if (rts == 0) {
    bits &= ~0x004;
  }
  if (ioctl(serial->port_fd, 0x5418, &bits) < 0) return -1;;
  return (0);
}

canonical_original_code:
int serial_setControl(struct s_serial *serial, int dtr, int rts)
{
  if (!serial->port_is_open) return -1;
  int bits;
  if (ioctl(serial->port_fd, 0x5415, &bits) < 0) return -1;
  if (dtr == 1) {
    bits |= 0x002;
  } else if (dtr == 0) {
    bits &= ~0x002;
  }
  if (rts == 1) {
    bits |= 0x004;
  } else if (rts == 0) {
    bits &= ~0x004;
  }
  if (ioctl(serial->port_fd, 0x5418, &bits) < 0) return -1;;
  return (0);
}

variable_types: dtr int

variable_types: rts int


decompiled_code:
__int64 __fastcall servo_write(__int64 a1, unsigned int a2)
{
return firmata_analogWrite(*(_QWORD *)a1, *(unsigned int *)(a1 + 8), a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
return firmata_analogWrite(*(_QWORD *)a1, *(unsigned int *)(a1 + 8), a2);
}

original_code:
int servo_write(t_servo *servo, int value)
{
  return (firmata_analogWrite(servo->firmata, servo->pin, value));
}

canonical_original_code:
int servo_write(struct s_servo *servo, int value)
{
  return (firmata_analogWrite(servo->firmata, servo->pin, value));
}

variable_types: struct s_servo { struct s_firmata { struct s_serial { int port_is_open; char *port_name; int baud_rate; char *error_msg; int port_fd; struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } settings_orig; struct termios settings; int tx; int rx; } *serial; struct s_pin { unsigned char mode; unsigned char analog_channel; unsigned long supported_modes; unsigned int value; } pins[128]; int parse_command_len; int parse_count; unsigned char parse_buff[1024]; int isReady; char firmware[140]; } *firmata; int pin; } *

variable_types: value int

user_defined_types: struct s_servo { struct s_firmata *firmata; int pin; }

user_defined_types: struct s_firmata { struct s_serial *serial; struct s_pin pins[128]; int parse_command_len; int parse_count; unsigned char parse_buff[1024]; int isReady; char firmware[140]; }


panic

decompiled_code:
void __noreturn panic()
{
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
exit(1);
}

original_code:
void
panic() {
 exit(1);
}

canonical_original_code:
void panic() {
 exit(1);
}


decompiled_code:
bool __fastcall is_hg_dir(const char *a1)
{
size_t v2; // rdi
char *v3; // rax
char *v4; // rbx
int v5; // ebp
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v2 = strlen(a1) + 5;
v3 = (char *)malloc(v2);
if ( !v3 )
panic();
v4 = v3;
sprintf(v3, "%s/.hg", a1);
v5 = stat(v4, &v7);
free(v4);
return v5 == 0;
}

canonical_decompiled_code:
bool __fastcall func8(const char *a1)
{
size_t v2; // rdi
char *v3; // rax
char *v4; // rbx
int v5; // ebp
struct stat v7; // [rsp+0h] [rbp-A8h] BYREF

v2 = strlen(a1) + 5;
v3 = (char *)malloc(v2);
if ( !v3 )
func7();
v4 = v3;
sprintf(v3, "%s/.hg", a1);
v5 = func14(v4, &v7);
free(v4);
return v5 == 0;
}

original_code:
_Bool
is_hg_dir(const char *pwd) {
 char *hg_path;
 size_t plen;
 int statret;
 struct stat st;
 plen = strlen(pwd);
 if (((hg_path) = (char *)malloc((plen + 5) * sizeof(char))) == ((void *)0)) panic();;
 sprintf(hg_path, "%s/.hg", pwd);
 statret = stat(hg_path, &st);
 free(hg_path);
 return statret == 0;
}

canonical_original_code:
_Bool is_hg_dir(char *pwd) {
 char *hg_path;
 unsigned long plen;
 int statret;
 struct stat st;
 plen = strlen(pwd);
 if (((hg_path) = (char *)malloc((plen + 5) * sizeof(char))) == ((void *)0)) panic();;
 sprintf(hg_path, "%s/.hg", pwd);
 statret = stat(hg_path, &st);
 free(hg_path);
 return statret == 0;
}

variable_types:
pwd
char *

variable_types:
plen
unsigned long

variable_types: statret int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
void *get_repo()
{
char *v0; // rbx
size_t v1; // rsi
void *result; // rax
int v3; // er12
char *v4; // rax

v0 = getcwd(0LL, 0LL);
while ( 1 )
{
LABEL_5:
v3 = strlen(v0);
if ( v3 <= 0 )
{
free(v0);
return 0LL;
}
if ( is_hg_dir(v0) )
break;
v4 = &v0[v3 - 1];
while ( *v4 != 47 )
{
if ( --v4 == &v0[v3 - 2 - (v3 - 1)] )
goto LABEL_5;
}
*v4 = 0;
}
v1 = strlen(v0) + 1;
result = realloc(v0, v1);
if ( !result )
panic();
return result;
}

canonical_decompiled_code:
void *func11()
{
char *v0; // rbx
size_t v1; // rsi
void *result; // rax
int v3; // er12
char *v4; // rax

v0 = getcwd(0LL, 0LL);
while ( 1 )
{
LABEL_5:
v3 = strlen(v0);
if ( v3 <= 0 )
{
free(v0);
return 0LL;
}
if ( func8(v0) )
break;
v4 = &v0[v3 - 1];
while ( *v4 != 47 )
{
if ( --v4 == &v0[v3 - 2 - (v3 - 1)] )
goto LABEL_5;
}
*v4 = 0;
}
v1 = strlen(v0) + 1;
result = realloc(v0, v1);
if ( !result )
func7();
return result;
}

original_code:
char *
get_repo() {
 char *dir_path;
 int len, k;
 dir_path = getcwd(((void *)0), 0);
 while ((len = strlen(dir_path)) > 0) {
  if (is_hg_dir(dir_path)) {
   if (((dir_path) = (char *)realloc((dir_path), (strlen(dir_path) + 1) * sizeof(char))) == ((void *)0)) panic();;
   return dir_path;
  }
  for (k = len - 1; k >= 0; k--) {
   if (dir_path[k] == '/') {
    dir_path[k] = 0;
    break;
   }
  }
 }
 free(dir_path);
 return ((void *)0);
}

canonical_original_code:
char *get_repo() {
 char *dir_path;
 int len; int k;
 dir_path = getcwd(((void *)0), 0);
 while ((len = strlen(dir_path)) > 0) {
  if (is_hg_dir(dir_path)) {
   if (((dir_path) = (char *)realloc((dir_path), (strlen(dir_path) + 1) * sizeof(char))) == ((void *)0)) panic();;
   return dir_path;
  }
  for (k = len - 1; k >= 0; k--) {
   if (dir_path[k] == '/') {
    dir_path[k] = 0;
    break;
   }
  }
 }
 free(dir_path);
 return ((void *)0);
}

variable_types: k int

return_type: char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char *v4; // rbx
const char *v5; // r13
const char *v6; // rbp
const char *v7; // rax

v3 = (const char *)get_repo();
if ( v3 )
{
v4 = v3;
v5 = ":%s:%s";
if ( argc > 1 )
v5 = argv[1];
v6 = get_current_revision(v3);
v7 = get_branch(v4);
printf(v5, v7, v6);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char *v4; // rbx
const char *v5; // r13
const char *v6; // rbp
const char *v7; // rax

v3 = (const char *)func11();
if ( v3 )
{
v4 = v3;
v5 = ":%s:%s";
if ( argc > 1 )
v5 = argv[1];
v6 = func10(v3);
v7 = func9(v4);
printf(v5, v7, v6);
}
return 0;
}

original_code:
int
main(int argc, char **argv) {
 char *repo;
 char *format;
 repo = get_repo();
 if (repo == ((void *)0))
  return 0;
 if (argc < 2)
  format = ":%s:%s";
 else
  format = argv[1];
 printf(format, get_branch(repo), get_current_revision(repo));
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
repo
char *

variable_types:
format
char *


decompiled_code:
int print_headings()
{
system("clear");
printf("%s %s\n", "HANGMAN", "0.4");
return puts(
"\t\tH H A NN N GGG MM MM A NN N\n"
"\t\tHHHH A A N N N G GG M M M M A A N N N\n"
"\t\tH H AA AA N NN GGGG M M M AA AA N NN");
}

canonical_decompiled_code:
int func7()
{
system("clear");
printf("%s %s\n", "HANGMAN", "0.4");
return puts(
"\t\tH H A NN N GGG MM MM A NN N\n"
"\t\tHHHH A A N N N G GG M M M M A A N N N\n"
"\t\tH H AA AA N NN GGGG M M M AA AA N NN");
}

original_code:
void print_headings(void)
{
 system("clear");
 printf("%s %s\n", "HANGMAN", "0.4");
printf("\t\tH  H    A    NN  N   GGG    MM   MM    A    NN  N\n"
       "\t\tHHHH   A A   N N N  G   GG  M M M M   A A   N N N\n"
       "\t\tH  H  AA AA  N  NN   GGGG   M  M  M  AA AA  N  NN\n");
}

canonical_original_code:
void print_headings()
{
 system("clear");
 printf("%s %s\n", "HANGMAN", "0.4");
printf("\t\tH  H    A    NN  N   GGG    MM   MM    A    NN  N\n"
       "\t\tHHHH   A A   N N N  G   GG  M M M M   A A   N N N\n"
       "\t\tH  H  AA AA  N  NN   GGGG   M  M  M  AA AA  N  NN\n");
}


check

decompiled_code:
__int64 __fastcall check(const char *a1)
{
size_t v2; // rdi
const char *v3; // rax
const char *v4; // rdi

v2 = strlen(a1);
v3 = a1;
v4 = &a1[v2];
while ( v3 != v4 )
{
if ( *v3++ == 95 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
size_t v2; // rdi
const char *v3; // rax
const char *v4; // rdi

v2 = strlen(a1);
v3 = a1;
v4 = &a1[v2];
while ( v3 != v4 )
{
if ( *v3++ == 95 )
return 0LL;
}
return 1LL;
}

original_code:
int check(char *s)
{
 int i;
 for (i = 0; i < strlen(s); i++)
  if (s[i] == '_')
   return 0;
 return 1;
}

variable_types:
s
char *

variable_types: i int


lives

decompiled_code:
int __fastcall lives(int a1)
{
int i; // ebx

printf("Lives left: ");
if ( a1 > 0 )
{
for ( i = 1; i <= a1; ++i )
putchar(88);
}
return puts("\n");
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
int i; // ebx

printf("Lives left: ");
if ( a1 > 0 )
{
for ( i = 1; i <= a1; ++i )
putchar(88);
}
return puts("\n");
}

original_code:
void lives(int lives_left)
{
 int i;
 printf("Lives left: ");
 for (i = 1; i <= lives_left; i++)
  printf("X");
 puts("\n");
}


decompiled_code:
int __fastcall show_stat(char a1)
{
int *v1; // rbx
__int64 i; // rax
int *v3; // rbx

v1 = alphabet;
if ( a1 == 32 )
{
for ( i = 0LL; i != 26; ++i )
alphabet[i] = i + 65;
v3 = alphabet;
do
putchar(*v3++);
while ( v3 != &game );
}
else
{
do
{
if ( *v1 == a1 )
{
*v1 = 32;
putchar(32);
}
else
{
putchar(*v1);
}
++v1;
}
while ( v1 != &game );
}
return puts("\n");
}

canonical_decompiled_code:
int __fastcall func10(char a1)
{
int *v1; // rbx
__int64 i; // rax
int *v3; // rbx

v1 = alphabet;
if ( a1 == 32 )
{
for ( i = 0LL; i != 26; ++i )
alphabet[i] = i + 65;
v3 = alphabet;
do
putchar(*v3++);
while ( v3 != &game );
}
else
{
do
{
if ( *v1 == a1 )
{
*v1 = 32;
putchar(32);
}
else
{
putchar(*v1);
}
++v1;
}
while ( v1 != &game );
}
return puts("\n");
}

original_code:
void show_stat(char c)
{
 int i, a = 65;
 if (c == ' ') {
  for (i = 0; i < 26; i++)
   alphabet[i] = a++;
  for (i = 0; i < 26; i++)
   printf("%c", alphabet[i]);
  puts("\n");
  return ;
 }
 for (i = 0; i < 26; i++)
  if (alphabet[i] == c) {
   alphabet[i] = ' ';
   printf(" ");
  } else {
   printf("%c", alphabet[i]);
  }
 puts("\n");
}

canonical_original_code:
void show_stat(char c)
{
 int i; int a = 65;
 if (c == ' ') {
  for (i = 0; i < 26; i++)
   alphabet[i] = a++;
  for (i = 0; i < 26; i++)
   printf("%c", alphabet[i]);
  puts("\n");
  return ;
 }
 for (i = 0; i < 26; i++)
  if (alphabet[i] == c) {
   alphabet[i] = ' ';
   printf(" ");
  } else {
   printf("%c", alphabet[i]);
  }
 puts("\n");
}

variable_types: c char

variable_types: a int


decompiled_code:
int game_init()
{
print_headings();
printstkfgr(7LL);
show_stat(32);
return lives(7);
}

canonical_decompiled_code:
int func11()
{
func7();
func16(7LL);
func10(32);
return func9(7);
}

original_code:
void game_init(void)
{
 print_headings();
 printstkfgr(7);
 show_stat(' ');
 lives(7);
}

canonical_original_code:
void game_init()
{
 print_headings();
 printstkfgr(7);
 show_stat(' ');
 lives(7);
}


update

decompiled_code:
int __fastcall update(char a1, int a2, int a3, char *a4, unsigned int a5)
{
char *v8; // rbx
__int64 v9; // rbp

print_headings();
printstkfgr(a5);
show_stat(a1);
lives(a5);
a4[a3] = a1;
if ( a2 > 0 )
{
v8 = a4;
v9 = (__int64)&a4[a2 - 1 + 1];
do
printf("%c ", (unsigned int)*v8++);
while ( v8 != (char *)v9 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func12(char a1, int a2, int a3, char *a4, unsigned int a5)
{
char *v8; // rbx
__int64 v9; // rbp

func7();
func16(a5);
func10(a1);
func9(a5);
a4[a3] = a1;
if ( a2 > 0 )
{
v8 = a4;
v9 = (__int64)&a4[a2 - 1 + 1];
do
printf("%c ", (unsigned int)*v8++);
while ( v8 != (char *)v9 );
}
return putchar(10);
}

original_code:
void update(char c, int word_size, int index, char *guess_word, int rem_lives)
{
 int i;
 print_headings();
 printstkfgr(rem_lives);
 show_stat(c);
 lives(rem_lives);
 guess_word[index] = c;
 for (i = 0; i < word_size; i++)
  printf("%c ", guess_word[i]);
 printf("\n");
}

variable_types: index int


decompiled_code:
int __fastcall print_trivia(const char *a1)
{
return printf("\n%s\n", a1);
}

canonical_decompiled_code:
int __fastcall func13(const char *a1)
{
return printf("\n%s\n", a1);
}

original_code:
void print_trivia(char *trivia)
{
 printf("\n%s\n", trivia);
}

variable_types:
trivia
char *


decompiled_code:
__int64 __fastcall play_game(char *s, const char *a2, const char *a3)
{
void *v3; // rsp
size_t i; // rbx
unsigned int v5; // er15
size_t v6; // rbx
size_t v7; // rax
int v8; // er13
int v10; // eax
const char *v11; // [rsp+0h] [rbp-50h] BYREF
const char *v12; // [rsp+8h] [rbp-48h]
int v13; // [rsp+14h] [rbp-3Ch]
int v14; // [rsp+18h] [rbp-38h]
int v15; // [rsp+1Ch] [rbp-34h]

v12 = a2;
v11 = a3;
v3 = alloca(strlen(s));
for ( i = 0LL; strlen(s) > i; ++i )
{
*((_BYTE *)&v11 + i) = 95;
printf("%c ", 95LL);
}
putchar(10);
v5 = 7;
do
{
printf("\nCategory: %s ", v12);
v8 = getch_like();
if ( (unsigned int)(v8 - 97) < 0x1A )
v8 -= 32;
v6 = 0LL;
v14 = 0;
v13 = (char)v8;
while ( 1 )
{
v15 = v6;
v7 = strlen(s);
if ( v7 <= v6 )
break;
if ( v8 == s[v6] )
{
update(v13, v7, v15, (char *)&v11, v5);
v14 = 1;
}
++v6;
}
if ( !v14 )
update(v8, v7, v6, (char *)&v11, --v5);
if ( !v5 )
{
print_trivia(v11);
puts("\nGAME OVER");
return (unsigned int)++game;
}
}
while ( !(unsigned int)check((const char *)&v11) );
v10 = strlen(s);
update(v8, v10, v15, (char *)&v11, 0xFFFFFFFF);
print_trivia(v11);
return (unsigned int)++game;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *s, const char *a2, const char *a3)
{
void *v3; // rsp
size_t i; // rbx
unsigned int v5; // er15
size_t v6; // rbx
size_t v7; // rax
int v8; // er13
int v10; // eax
const char *v11; // [rsp+0h] [rbp-50h] BYREF
const char *v12; // [rsp+8h] [rbp-48h]
int v13; // [rsp+14h] [rbp-3Ch]
int v14; // [rsp+18h] [rbp-38h]
int v15; // [rsp+1Ch] [rbp-34h]

v12 = a2;
v11 = a3;
v3 = alloca(strlen(s));
for ( i = 0LL; strlen(s) > i; ++i )
{
*((_BYTE *)&v11 + i) = 95;
printf("%c ", 95LL);
}
putchar(10);
v5 = 7;
do
{
printf("\nCategory: %s ", v12);
v8 = func17();
if ( (unsigned int)(v8 - 97) < 0x1A )
v8 -= 32;
v6 = 0LL;
v14 = 0;
v13 = (char)v8;
while ( 1 )
{
v15 = v6;
v7 = strlen(s);
if ( v7 <= v6 )
break;
if ( v8 == s[v6] )
{
func12(v13, v7, v15, (char *)&v11, v5);
v14 = 1;
}
++v6;
}
if ( !v14 )
func12(v8, v7, v6, (char *)&v11, --v5);
if ( !v5 )
{
func13(v11);
puts("\nGAME OVER");
return (unsigned int)++game;
}
}
while ( !(unsigned int)func8((const char *)&v11) );
v10 = strlen(s);
func12(v8, v10, v15, (char *)&v11, 0xFFFFFFFF);
func13(v11);
return (unsigned int)++game;
}

original_code:
int play_game(char *s, char *t, char *r)
{
 int guess;
 boolean correct = 0;
 char new_word[strlen(s)];
 int i, remaining_lives = 7;
 for (i = 0; i < strlen(s); i++) {
  new_word[i] = '_';
  printf("%c ", new_word[i]);
 }
 printf("\n");
 for (;;) {
  printf("\nCategory: %s ", t);
  guess = getch_like();
  if (guess >= 97 && guess <= 122)
   guess -= 32;
  for (i = 0; i < strlen(s); i++)
   if (guess == s[i]) {
    update(guess, strlen(s), i, new_word, remaining_lives);
    correct = 1;
   }
  if (!correct)
   update(guess, strlen(s), i, new_word, --remaining_lives);
  correct = 0;
  if (remaining_lives == 0) {
   print_trivia(r);
   printf("\nGAME OVER\n");
   return ++game;
  } else if (check(new_word)) {
   update(guess, strlen(s), i, new_word, -1);
   print_trivia(r);
   return ++game;
  }
 }
}

canonical_original_code:
int play_game(char *s, char *t, char *r)
{
 int guess;
 int correct = 0;
 char new_word[-1];
 int i; int remaining_lives = 7;
 for (i = 0; i < strlen(s); i++) {
  new_word[i] = '_';
  printf("%c ", new_word[i]);
 }
 printf("\n");
 for (;;) {
  printf("\nCategory: %s ", t);
  guess = getch_like();
  if (guess >= 97 && guess <= 122)
   guess -= 32;
  for (i = 0; i < strlen(s); i++)
   if (guess == s[i]) {
    update(guess, strlen(s), i, new_word, remaining_lives);
    correct = 1;
   }
  if (!correct)
   update(guess, strlen(s), i, new_word, --remaining_lives);
  correct = 0;
  if (remaining_lives == 0) {
   print_trivia(r);
   printf("\nGAME OVER\n");
   return ++game;
  } else if (check(new_word)) {
   update(guess, strlen(s), i, new_word, -1);
   print_trivia(r);
   return ++game;
  }
 }
}

variable_types:
t
char *

variable_types:
r
char *

variable_types: guess int

variable_types: correct int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v4; // bl
char v6[256]; // [rsp+0h] [rbp-148h] BYREF
char v7[32]; // [rsp+100h] [rbp-48h] BYREF
char dest[40]; // [rsp+120h] [rbp-28h] BYREF

game_init();
while ( 1 )
{
get_word(dest, v7, v6);
v3 = play_game(dest, v7, v6);
printf("\nGames played: %d\n", v3);
printf("Play again? (y/n): ");
v4 = getchar();
getchar();
if ( (v4 & 0xDF) == 78 )
break;
game_init();
}
system("clear");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
char v4; // bl
char v6[256]; // [rsp+0h] [rbp-148h] BYREF
char v7[32]; // [rsp+100h] [rbp-48h] BYREF
char dest[40]; // [rsp+120h] [rbp-28h] BYREF

func11();
while ( 1 )
{
func15(dest, v7, v6);
v3 = func14(dest, v7, v6);
printf("\nGames played: %d\n", v3);
printf("Play again? (y/n): ");
v4 = getchar();
getchar();
if ( (v4 & 0xDF) == 78 )
break;
func11();
}
system("clear");
return 0;
}

original_code:
int main(void)
{
 char word[25 + 1], category[25 + 1], trivia[240 + 1],
      again;
 int games_played;
 game_init();
 for (;;) {
  get_word(word, category, trivia);
  games_played = play_game(word, category, trivia);
  printf("\nGames played: %d\n", games_played);
  printf("Play again? (y/n): ");
  again = getchar();
  getchar();
  if (again == 'n' || again == 'N') {
   system("clear");
   break;
  }
  game_init();
 }
 return 0;
}

canonical_original_code:
int main()
{
 char word[-1]; char category[-1]; char trivia[-1]; char again;
 int games_played;
 game_init();
 for (;;) {
  get_word(word, category, trivia);
  games_played = play_game(word, category, trivia);
  printf("\nGames played: %d\n", games_played);
  printf("Play again? (y/n): ");
  again = getchar();
  getchar();
  if (again == 'n' || again == 'N') {
   system("clear");
   break;
  }
  game_init();
 }
 return 0;
}

variable_types:
word
char [-1]

variable_types:
category
char [-1]

variable_types:
trivia
char [-1]

variable_types: again char


newgap

repo: jdussouillez/libsort

original_code:
static int newgap(int gap) {
  gap = (gap * 10) / 13;
  if (gap < 1)
    return 1;
  return (gap == 9 || gap == 10) ? 11 : gap;
}

variable_types: gap int


combsort

original_code:
void combsort(void* base, size_t nmemb, size_t size, int(*compare)(const void*, const void*)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int gap = nmemb, swapped, i;
  do {
    swapped = 0;
    gap = newgap(gap);
    for (i = 0; i < (nmemb - gap); i++) {
      if (compare(base + (i * size), base + ((i + gap) * size)) > 0) {
 swapped = 1;
 swap(base + (i * size), base + ((i + gap) * size), size);
      }
    }
  } while (gap > 1 || swapped);
}

canonical_original_code:
void combsort(void *base, unsigned long nmemb, unsigned long size, int (*compare)(void *, void *)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int gap = nmemb; int swapped; int i;
  do {
    swapped = 0;
    gap = newgap(gap);
    for (i = 0; i < (nmemb - gap); i++) {
      if (compare(base + (i * size), base + ((i + gap) * size)) > 0) {
 swapped = 1;
 swap(base + (i * size), base + ((i + gap) * size), size);
      }
    }
  } while (gap > 1 || swapped);
}

variable_types:
base
void *

variable_types:
nmemb
unsigned long

variable_types:
size
unsigned long

variable_types:
compare
int (*)(void *, void *)

variable_types: swapped int


siftdown

decompiled_code:
__int64 __fastcall siftdown(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64), int a4, int a5)
{
int v6; // ebx
__int64 result; // rax
__int64 v9; // rbx
int v10; // ebp

v6 = a4;
result = (unsigned int)(2 * a4);
if ( a5 > (int)result )
{
while ( 1 )
{
v10 = result + 1;
if ( a5 > (int)result + 1 && (int)a3(a1 + a2 * v10, a1 + a2 * (2 * v6 + 2)) < 0 )
v10 = 2 * v6 + 2;
v9 = a1 + a2 * v6;
result = a3(v9, a1 + a2 * v10);
if ( (int)result >= 0 )
break;
swap(v9, a1 + a2 * v10, a2);
result = (unsigned int)(2 * v10);
if ( (int)result >= a5 )
break;
v6 = v10;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64), int a4, int a5)
{
int v6; // ebx
__int64 result; // rax
__int64 v9; // rbx
int v10; // ebp

v6 = a4;
result = (unsigned int)(2 * a4);
if ( a5 > (int)result )
{
while ( 1 )
{
v10 = result + 1;
if ( a5 > (int)result + 1 && (int)a3(a1 + a2 * v10, a1 + a2 * (2 * v6 + 2)) < 0 )
v10 = 2 * v6 + 2;
v9 = a1 + a2 * v6;
result = a3(v9, a1 + a2 * v10);
if ( (int)result >= 0 )
break;
swap(v9, a1 + a2 * v10, a2);
result = (unsigned int)(2 * v10);
if ( (int)result >= a5 )
break;
v6 = v10;
}
}
return result;
}

original_code:
static void siftdown(void* base, size_t size, int(*compare)(const void*, const void*), int start, int end) {
  int root = start, child;
  void *proot, *pchild;
  while (((root * 2) + 1) <= end) {
    child = (root * 2) + 1;
    if (child + 1 <= end && compare(base + (child * size), base + ((child + 1) * size)) < 0) {
      child++;
    }
    proot = base + (root * size);
    pchild = base + (child * size);
    if (compare(proot, pchild) < 0) {
      swap(proot, pchild, size);
      root = child;
    }
    else
      return;
  }
}

canonical_original_code:
static void siftdown(void *base, unsigned long size, int (*compare)(void *, void *), int start, int end) {
  int root = start; int child;
  void *proot; void *pchild;
  while (((root * 2) + 1) <= end) {
    child = (root * 2) + 1;
    if (child + 1 <= end && compare(base + (child * size), base + ((child + 1) * size)) < 0) {
      child++;
    }
    proot = base + (root * size);
    pchild = base + (child * size);
    if (compare(proot, pchild) < 0) {
      swap(proot, pchild, size);
      root = child;
    }
    else
      return;
  }
}

variable_types: start int

variable_types: end int

variable_types: root int

variable_types: child int

variable_types:
proot
void *

variable_types:
pchild
void *


heapify

decompiled_code:
__int64 __fastcall heapify(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64), int a4)
{
__int64 result; // rax
int v5; // ebx
int v7; // er14

result = (unsigned int)(a4 - 2);
v5 = (a4 - 2) / 2;
if ( a4 > 0 )
{
v7 = a4 - 1;
do
result = siftdown(a1, a2, a3, v5--, v7);
while ( v5 >= 0 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64), int a4)
{
__int64 result; // rax
int v5; // ebx
int v7; // er14

result = (unsigned int)(a4 - 2);
v5 = (a4 - 2) / 2;
if ( a4 > 0 )
{
v7 = a4 - 1;
do
result = func0(a1, a2, a3, v5--, v7);
while ( v5 >= 0 );
}
return result;
}

original_code:
static void heapify(void* base, size_t size, int(*compare)(const void*, const void*), int count) {
  int start = (count - 2) / 2;
  while (start >= 0) {
    siftdown(base, size, compare, start, count - 1);
    start--;
  }
}

canonical_original_code:
static void heapify(void *base, unsigned long size, int (*compare)(void *, void *), int count) {
  int start = (count - 2) / 2;
  while (start >= 0) {
    siftdown(base, size, compare, start, count - 1);
    start--;
  }
}

variable_types: count int


heapsort

decompiled_code:
__int64 __fastcall heapsort(__int64 a1, int a2, __int64 a3, __int64 (__fastcall *a4)(__int64, __int64))
{
int v6; // ebx
__int64 result; // rax
__int64 v8; // r12

if ( a1 )
{
if ( a4 )
{
v6 = a2 - 1;
result = heapify(a1, a3, a4, a2);
if ( a2 - 1 > 0 )
{
v8 = a1 + a3 * v6;
do
{
swap(v8, a1, a3);
result = siftdown(a1, a3, a4, 0, --v6);
v8 -= a3;
}
while ( v6 );
}
}
}
return result;
}

original_code:
void heapsort(void* base, size_t nmemb, size_t size, int(*compare)(const void*, const void*)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int end = nmemb - 1;
  heapify(base, size, compare, nmemb);
  while (end > 0) {
    swap(base + (end * size), base, size);
    siftdown(base, size, compare, 0, --end);
  }
}

canonical_original_code:
void heapsort(void *base, unsigned long nmemb, unsigned long size, int (*compare)(void *, void *)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int end = nmemb - 1;
  heapify(base, size, compare, nmemb);
  while (end > 0) {
    swap(base + (end * size), base, size);
    siftdown(base, size, compare, 0, --end);
  }
}


selectionsort

decompiled_code:
void __fastcall selectionsort(char *a1, unsigned __int64 a2, size_t a3, int (__fastcall *a4)(char *, char *))
{
int v5; // ebx
int v6; // er13
unsigned __int64 v7; // r12
char *v8; // rbp
unsigned __int64 i; // [rsp+18h] [rbp-50h]
char *dest; // [rsp+28h] [rbp-40h]

if ( a1 && a4 && a2 != 1 )
{
dest = a1;
for ( i = 1LL; i != a2; ++i )
{
v5 = i - 1;
v6 = i;
if ( a2 > i )
{
v7 = (int)i;
v8 = &a1[(int)i * a3];
do
{
if ( a4(v8, &a1[a3 * v5]) < 0 )
v5 = v6;
++v6;
++v7;
v8 += a3;
}
while ( v7 < a2 );
if ( v5 != (_DWORD)i - 1 )
swap(dest, &a1[a3 * v5], a3);
}
dest += a3;
}
}
}

original_code:
void selectionsort(void* base, size_t nmemb, size_t size, int(*compare)(const void*, const void*)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int i, j, min;
  for (i = 0; i < nmemb - 1; i++) {
    min = i;
    for (j = i + 1; j < nmemb; j++) {
      if (compare(base + (j * size), base + (min * size)) < 0)
 min = j;
    }
    if (min != i)
      swap(base + (i * size), base + (min * size), size);
  }
}

canonical_original_code:
void selectionsort(void *base, unsigned long nmemb, unsigned long size, int (*compare)(void *, void *)) {
  if (base == ((void *)0) || compare == ((void *)0))
    return;
  int i; int j; int min;
  for (i = 0; i < nmemb - 1; i++) {
    min = i;
    for (j = i + 1; j < nmemb; j++) {
      if (compare(base + (j * size), base + (min * size)) < 0)
 min = j;
    }
    if (min != i)
      swap(base + (i * size), base + (min * size), size);
  }
}

variable_types: i int

variable_types: j int

variable_types: min int