decompiled_code:
_BYTE *Material_Solid_new()
{
_BYTE *result; // rax

result = malloc(0x18uLL);
*(_QWORD *)result = Material_Solid_get_transmit;
result[8] = -1;
result[9] = -1;
result[10] = -1;
result[11] = -1;
result[12] = -1;
result[13] = -1;
result[14] = 0;
result[15] = 0;
result[16] = 0;
result[17] = 0;
result[18] = 0;
result[19] = 0;
return result;
}

canonical_decompiled_code:
_BYTE *func1()
{
_BYTE *result; // rax

result = malloc(0x18uLL);
*(_QWORD *)result = Material_Solid_get_transmit;
result[8] = -1;
result[9] = -1;
result[10] = -1;
result[11] = -1;
result[12] = -1;
result[13] = -1;
result[14] = 0;
result[15] = 0;
result[16] = 0;
result[17] = 0;
result[18] = 0;
result[19] = 0;
return result;
}

original_code:
Material* Material_Solid_new() {
    Material_Solid* m = (Material_Solid*) malloc(sizeof(Material_Solid));
    m->func = material_solid_func_table;
    m->transmit = (struct material_transmit) {
        {255, 255, 255},
        {255, 255, 255},
        {0, 0, 0},
        {0, 0, 0}
    };
    return (Material*) m;
}

canonical_original_code:
struct Material_struct *Material_Solid_new() {
    struct Material_Solid *m = (Material_Solid*) malloc(sizeof(Material_Solid));
    m->func = material_solid_func_table;
    m->transmit = (struct material_transmit) {
        {255, 255, 255},
        {255, 255, 255},
        {0, 0, 0},
        {0, 0, 0}
    };
    return (struct Material_struct *) m;
}

return_type: struct Material_struct { struct material_func_table { struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; } (*get_transmit)(struct Material_struct *mat, struct Object_struct { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray { struct Vector { double x; } o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct *mat; } *object, struct Vector *intersect); } func; } *


decompiled_code:
__int64 Object_Plane_inside()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 1LL;
}

original_code:
_Bool Object_Plane_inside(const Object* obj, const Point* point) {
    return 1;
}

canonical_original_code:
_Bool Object_Plane_inside(struct Object_struct *obj, struct Vector *point) {
    return 1;
}

variable_types: struct Object_struct { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray { struct Vector { double x; } o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct { struct material_func_table { struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; } (*get_transmit)(struct Material_struct *mat, struct Object_struct *object, struct Vector *intersect); } func; } *mat; } *

variable_types:
point
struct Vector *

user_defined_types: struct Object_struct { struct object_func_table func; struct Material_struct *mat; }

user_defined_types: struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); }

user_defined_types: struct Material_struct { struct material_func_table func; }

user_defined_types: struct material_func_table { struct material_transmit (*get_transmit)(struct Material_struct *mat, struct Object_struct *object, struct Vector *intersect); }

user_defined_types: struct Ray { struct Vector o; struct Vector d; }

user_defined_types: struct material_transmit { struct Color diffuse; struct Color ambient; struct Color reflect; struct Color refract; }


decompiled_code:
__int64 __fastcall Object_Plane_ray_intersect(__int64 a1, double *a2, double *a3)
{
double v4; // r12
__int64 result; // rax
double v6; // xmm0_8
_BYTE v7[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = Vector_dot(a1 + 32, a2 + 3);
if ( v4 == 0.0 )
return 0LL;
Vector_subtract(v7, a2, a1 + 56);
v6 = COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Vector_dot(a1 + 32, v7)) ^ LC2) / v4;
result = 0LL;
if ( v6 > 0.0 && v6 > 0.00001 )
{
*a3 = v6 * a2[3] + *a2;
a3[1] = v6 * a2[4] + a2[1];
a3[2] = v6 * a2[5] + a2[2];
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, double *a2, double *a3)
{
double v4; // r12
__int64 result; // rax
double v6; // xmm0_8
_BYTE v7[72]; // [rsp+0h] [rbp-48h] BYREF

v4 = Vector_dot(a1 + 32, a2 + 3);
if ( v4 == 0.0 )
return 0LL;
Vector_subtract(v7, a2, a1 + 56);
v6 = COERCE_DOUBLE(COERCE_UNSIGNED_INT64(Vector_dot(a1 + 32, v7)) ^ LC2) / v4;
result = 0LL;
if ( v6 > 0.0 && v6 > 0.00001 )
{
*a3 = v6 * a2[3] + *a2;
a3[1] = v6 * a2[4] + a2[1];
a3[2] = v6 * a2[5] + a2[2];
result = 1LL;
}
return result;
}

original_code:
_Bool Object_Plane_ray_intersect(const Object* obj, const Ray* r, Point* intersect) {
    Object_Plane* p = (Object_Plane*) obj;
    Vector tmp_vector;
    double denominator = Vector_dot(&p->norm, &r->d);
    if (denominator == 0.0) {
        return 0;
    }
    Vector_subtract(&tmp_vector, &r->o, &p->point);
    double t = -1*Vector_dot(&p->norm, &tmp_vector) / denominator;
    if (t <= 0) {
        return 0;
    }
    if (t <= ( 1.0e-5 )) {
        return 0;
    }
    intersect->x = r->o.x + t*r->d.x;
    intersect->y = r->o.y + t*r->d.y;
    intersect->z = r->o.z + t*r->d.z;
    return 1;
}

canonical_original_code:
_Bool Object_Plane_ray_intersect(struct Object_struct *obj, struct Ray *r, struct Vector *intersect) {
    struct Object_Plane *p = (Object_Plane*) obj;
    struct Vector tmp_vector;
    double denominator = Vector_dot(&p->norm, &r->d);
    if (denominator == 0.0) {
        return 0;
    }
    Vector_subtract(&tmp_vector, &r->o, &p->point);
    double t = -1*Vector_dot(&p->norm, &tmp_vector) / denominator;
    if (t <= 0) {
        return 0;
    }
    if (t <= ( 1.0e-5 )) {
        return 0;
    }
    intersect->x = r->o.x + t*r->d.x;
    intersect->y = r->o.y + t*r->d.y;
    intersect->z = r->o.z + t*r->d.z;
    return 1;
}

variable_types:
r
struct Ray *

variable_types:
intersect
struct Vector *

variable_types: struct Object_Plane { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct { struct object_func_table func; struct Material_struct { struct material_func_table { struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; } (*get_transmit)(struct Material_struct *mat, struct Object_struct *object, struct Vector { double x; } *intersect); } func; } *mat; } *obj, struct Ray { struct Vector o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct *mat; struct Vector norm; struct Vector point; } *

variable_types: denominator double

variable_types: t double

user_defined_types: struct Object_Plane { struct object_func_table func; struct Material_struct *mat; struct Vector norm; struct Vector point; }


decompiled_code:
double *__fastcall Object_Plane_new(double a1)
{
double *v1; // rbx

v1 = (double *)malloc(0x50uLL);
*(_QWORD *)v1 = Object_Plane_ray_intersect;
*((_QWORD *)v1 + 1) = Object_Plane_normal;
*((_QWORD *)v1 + 2) = Object_Plane_inside;
v1[7] = 0.0;
v1[8] = 0.0;
v1[9] = a1;
v1[4] = 0.0;
v1[5] = 0.0;
*((_QWORD *)v1 + 6) = LC1;
*((_QWORD *)v1 + 3) = Material_new_default();
return v1;
}

canonical_decompiled_code:
double *__fastcall func3(double a1)
{
double *v1; // rbx

v1 = (double *)malloc(0x50uLL);
*(_QWORD *)v1 = Object_Plane_ray_intersect;
*((_QWORD *)v1 + 1) = Object_Plane_normal;
*((_QWORD *)v1 + 2) = Object_Plane_inside;
v1[7] = 0.0;
v1[8] = 0.0;
v1[9] = a1;
v1[4] = 0.0;
v1[5] = 0.0;
*((_QWORD *)v1 + 6) = LC1;
*((_QWORD *)v1 + 3) = Material_new_default();
return v1;
}

original_code:
Object* Object_Plane_new(double z) {
    Object_Plane* p = (Object_Plane*) malloc(sizeof(Object_Plane));
    p->func = object_plane_func_table;
    p->point = (Point) {0, 0, z};
    p->norm = (Vector) {0, 0, 1};
    p->mat = Material_new_default();
    return (Object*) p;
}

canonical_original_code:
struct Object_struct *Object_Plane_new(double z) {
    struct Object_Plane *p = (Object_Plane*) malloc(sizeof(Object_Plane));
    p->func = object_plane_func_table;
    p->point = (struct Vector) {0, 0, z};
    p->norm = (struct Vector) {0, 0, 1};
    p->mat = Material_new_default();
    return (struct Object_struct *) p;
}

variable_types: z double

return_type: struct Object_struct { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray { struct Vector { double x; } o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct { struct material_func_table { struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; } (*get_transmit)(struct Material_struct *mat, struct Object_struct *object, struct Vector *intersect); } func; } *mat; } *


decompiled_code:
_BOOL8 __fastcall Color_is_black(_BYTE *a1)
{
bool v1; // al

v1 = 0;
if ( !*a1 && !a1[1] )
v1 = a1[2] == 0;
return v1;
}

original_code:
_Bool Color_is_black(const Color* c) {
    return c->r == 0 &&
           c->g == 0 &&
           c->b == 0;
}

canonical_original_code:
_Bool Color_is_black(struct Color *c) {
    return c->r == 0 &&
           c->g == 0 &&
           c->b == 0;
}

variable_types: struct Color { unsigned char r; unsigned char g; unsigned char b; } *


decompiled_code:
__int64 __fastcall Canvas_Mem_get_next_row(__int64 a1)
{
return *(_QWORD *)(a1 + 48) + 3LL * (unsigned int)(*(_DWORD *)(a1 + 56) * *(_DWORD *)(a1 + 40));
}

original_code:
Color* Canvas_Mem_get_next_row(Canvas* _canvas) {
    Canvas_Mem* canvas = (Canvas_Mem*) _canvas;
    return &canvas->image[canvas->width * canvas->next_row];
}

canonical_original_code:
struct Color *Canvas_Mem_get_next_row(struct Canvas_struct *_canvas) {
    struct Canvas_Mem *canvas = (Canvas_Mem*) _canvas;
    return &canvas->image[canvas->width * canvas->next_row];
}

variable_types: struct Canvas_struct { struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; } *

variable_types: struct Canvas_Mem { struct canvas_func_table { _Bool (*start)(struct Canvas_struct { struct canvas_func_table func; unsigned int width; } *canvas); struct Color { unsigned char r; unsigned char g; unsigned char b; } *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; struct Color *image; unsigned int next_row; } *

user_defined_types: struct Canvas_Mem { struct canvas_func_table func; unsigned int width; struct Color *image; unsigned int next_row; }


decompiled_code:
__int64 __fastcall Canvas_Mem_write_row(__int64 a1)
{
++*(_DWORD *)(a1 + 56);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
++*(_DWORD *)(a1 + 56);
return 1LL;
}

original_code:
_Bool Canvas_Mem_write_row(Canvas* _canvas, Color* row) {
    Canvas_Mem* canvas = (Canvas_Mem*) _canvas;
    canvas->next_row++;
    return 1;
}

canonical_original_code:
_Bool Canvas_Mem_write_row(struct Canvas_struct *_canvas, struct Color *row) {
    struct Canvas_Mem *canvas = (Canvas_Mem*) _canvas;
    canvas->next_row++;
    return 1;
}

variable_types: struct Canvas_struct { struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color { unsigned char r; unsigned char g; unsigned char b; } *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; } *

variable_types:
row
struct Color *

user_defined_types: struct Canvas_struct { struct canvas_func_table func; unsigned int width; }

user_defined_types: struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); }


decompiled_code:
__int64 Canvas_Mem_finish()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 1LL;
}

original_code:
_Bool Canvas_Mem_finish(Canvas* canvas) {
    return 1;
}

canonical_original_code:
_Bool Canvas_Mem_finish(struct Canvas_struct *canvas) {
    return 1;
}

user_defined_types: struct Color { unsigned char r; unsigned char g; unsigned char b; }


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

if ( *(_QWORD *)(a1 + 48)
|| (v2 = malloc(3 * *(unsigned int *)(a1 + 44) * (unsigned __int64)*(unsigned int *)(a1 + 40)),
(*(_QWORD *)(a1 + 48) = v2) != 0LL) )
{
*(_DWORD *)(a1 + 56) = 0;
result = 1LL;
}
else
{
fwrite("Could not allocate memory for image!", 1uLL, 0x24uLL, stderr);
result = 0LL;
}
return result;
}

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

if ( *(_QWORD *)(a1 + 48)
|| (v2 = malloc(3 * *(unsigned int *)(a1 + 44) * (unsigned __int64)*(unsigned int *)(a1 + 40)),
(*(_QWORD *)(a1 + 48) = v2) != 0LL) )
{
*(_DWORD *)(a1 + 56) = 0;
result = 1LL;
}
else
{
fwrite("Could not allocate memory for image!", 1uLL, 0x24uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
_Bool Canvas_Mem_start(Canvas* _canvas) {
    Canvas_Mem* canvas = (Canvas_Mem*) _canvas;
    if (!canvas->image) {
        canvas->image = (Color*) malloc(sizeof(Color) * canvas->width * canvas->height);
    }
    if (!canvas->image) {
        fprintf(stderr, "Could not allocate memory for image!");
        return 0;
    }
    canvas->next_row = 0;
    return 1;
}

canonical_original_code:
_Bool Canvas_Mem_start(struct Canvas_struct *_canvas) {
    struct Canvas_Mem *canvas = (Canvas_Mem*) _canvas;
    if (!canvas->image) {
        canvas->image = (struct Color *) malloc(sizeof(Color) * canvas->width * canvas->height);
    }
    if (!canvas->image) {
        fprintf(stderr, "Could not allocate memory for image!");
        return 0;
    }
    canvas->next_row = 0;
    return 1;
}


decompiled_code:
void __fastcall Canvas_Mem_free(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 6);
if ( v2 )
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 6);
if ( v2 )
free(v2);
free(ptr);
}

original_code:
void Canvas_Mem_free(Canvas* _canvas) {
    Canvas_Mem* canvas = (Canvas_Mem*) _canvas;
    if (canvas->image) {
        free(canvas->image);
    }
    free(canvas);
}

canonical_original_code:
void Canvas_Mem_free(struct Canvas_struct *_canvas) {
    struct Canvas_Mem *canvas = (Canvas_Mem*) _canvas;
    if (canvas->image) {
        free(canvas->image);
    }
    free(canvas);
}

return_type: void


decompiled_code:
unsigned __int64 __fastcall trace_ray(__int64 a1, _QWORD **a2, __int64 a3, unsigned int a4, double a5)
{
unsigned __int8 (__fastcall **v7)(_QWORD, _QWORD, __int64 *); // rax
unsigned __int8 (__fastcall **v8)(_QWORD, _QWORD, __int64 *); // rbx
unsigned __int8 v9; // dl
char v10; // dh
char v11; // edx^2
char v12; // edx^3
__int64 v13; // rbx
double v14; // xmm0_8
double v15; // xmm3_8
double v16; // xmm0_8
double v17; // xmm0_8
__int64 v18; // rax
__int64 v20; // rax
double v22; // [rsp+0h] [rbp-168h]
double v23; // [rsp+8h] [rbp-160h]
char v24[32]; // [rsp+10h] [rbp-158h] BYREF
char v25[32]; // [rsp+30h] [rbp-138h] BYREF
__m128i v26; // [rsp+50h] [rbp-118h] BYREF
__int64 v27; // [rsp+60h] [rbp-108h]
__m128i v28; // [rsp+70h] [rbp-F8h] BYREF
__int64 v29; // [rsp+80h] [rbp-E8h]
__m128i v30; // [rsp+88h] [rbp-E0h] BYREF
__int64 v31; // [rsp+98h] [rbp-D0h]
char v32[24]; // [rsp+A0h] [rbp-C8h] BYREF
char v33[38]; // [rsp+B8h] [rbp-B0h] BYREF
__int16 v34; // [rsp+DEh] [rbp-8Ah] BYREF
char v35; // [rsp+E0h] [rbp-88h]
__int64 v36; // [rsp+E1h] [rbp-87h] BYREF
unsigned __int8 v37; // [rsp+E9h] [rbp-7Fh]
char v38[3]; // [rsp+EAh] [rbp-7Eh] BYREF
unsigned __int16 v39; // [rsp+EDh] [rbp-7Bh] BYREF
unsigned __int8 v40; // [rsp+EFh] [rbp-79h]
__int64 v41[4]; // [rsp+F0h] [rbp-78h] BYREF
__m128i v42; // [rsp+110h] [rbp-58h] BYREF
__int64 v43; // [rsp+120h] [rbp-48h]
unsigned __int16 v44; // [rsp+12Dh] [rbp-3Bh]
unsigned __int8 v45; // [rsp+12Fh] [rbp-39h]

v39 = 0;
v40 = 0;
Vector_assign(v32, a3);
Vector_assign(v33, a3 + 24);
Vector_normalize(v33);
v7 = ray_intersect(a2, (__int64)v32, v42.m128i_i64);
if ( v7 && (v8 = v7, *(_DWORD *)(a1 + 8) >= a4) )
{
((unsigned __int8 (__fastcall **)(_QWORD, __m128i *, __int64 *))v7)[1](v7, &v42, v41);
v36 = (*(__int64 (__fastcall **)(_QWORD, _QWORD, __m128i *))v8[3])(v8[3], v8, &v42);
v37 = v9;
v38[0] = v10;
v38[1] = v11;
v38[2] = v12;
Vector_normalize(v41);
v39 = *(_WORD *)((char *)&v36 + 3);
v40 = BYTE5(v36);
v26 = (__m128i)(unsigned __int64)LC5;
v27 = LC5;
Vector_normalize(&v26);
v30 = _mm_loadu_si128(&v26);
v31 = v27;
v28 = _mm_loadu_si128(&v42);
v29 = v43;
if ( !ray_intersect(a2, (__int64)&v28, 0LL) )
{
if ( Vector_dot(&v26, v41) >= 0.0 )
Vector_dot(&v26, v41);
Color_scalar_mult(&v34, &v36);
Color_add(&v39, &v39, &v34);
if ( ((unsigned __int8 (__fastcall *)(unsigned __int8 (__fastcall **)(_QWORD, _QWORD, __int64 *), char *))v8[2])(
v8,
v32) )
{
Vector_scalar_mult(v41, v41, -1.0);
}
}
if ( !(unsigned __int8)Color_is_black((char *)&v36 + 6) )
{
ray_reflect(&v28, v32, &v42, v41);
v20 = trace_ray(a1, a2, &v28, a4 + 1, a5);
LOBYTE(v34) = (int)((double)BYTE6(v36) / 255.0 * (double)(unsigned __int8)v20);
HIBYTE(v34) = (int)((double)HIBYTE(v36) / 255.0 * (double)BYTE1(v20));
v35 = (int)((double)v37 / 255.0 * (double)BYTE2(v20));
Color_add(&v39, &v39, &v34);
}
if ( !(unsigned __int8)Color_is_black(v38) )
{
v13 = LC3;
if ( COERCE_DOUBLE(COERCE_UNSIGNED_INT64(a5 - 1.000277) & LC8) > 0.00001 )
v13 = LC4;
v22 = a5 / *(double *)&v13;
v14 = Vector_dot(v33, v41);
v23 = v14;
*(_QWORD *)&v15 = *(_QWORD *)&v14 ^ LC10;
v16 = 1.0 - (1.0 - v14 * v14) * (v22 * v22);
if ( v16 < 0.0 )
v17 = sqrt(v16);
else
v17 = sqrt(v16);
Vector_scalar_mult(v25, v41, v15 * v22 - v17);
if ( v23 > 0.0 )
Vector_scalar_mult(v25, v25, -1.0);
Vector_scalar_mult(v24, v33, v22);
Vector_add(v25, v25, v24);
Vector_assign(&v28, &v42);
Vector_assign(&v30, v25);
v18 = trace_ray(a1, a2, &v28, a4 + 1, *(double *)&v13);
v34 = v18;
v35 = BYTE2(v18);
Color_mult(&v34, &v34, v38);
Color_add(&v39, &v39, &v34);
}
v44 = v39;
v45 = v40;
}
else
{
v44 = 0;
v45 = 0;
}
return ((unsigned __int64)v45 << 16) | v44;
}

original_code:
static Color trace_ray(Renderer_RayTraceSimple* renderer, const Scene* scene, const Ray* _r, unsigned int depth, double n1) {
    Point intersect;
    Vector norm;
    Object* obj;
    Color result = {0, 0, 0};
    struct material_transmit transmit;
    Color tmp_color;
    Ray r, tmp_ray;
    Vector_assign(&r.o, &_r->o);
    Vector_assign(&r.d, &_r->d);
    Vector_normalize(&r.d);
    obj = ray_intersect(scene, &r, &intersect);
    if (!obj || depth > renderer->max_depth) {
        return (Color) {0, 0, 0};
    }
    (obj)->func.normal((Object*) obj, &intersect, &norm);
    transmit = (obj->mat)->func.get_transmit((Material*) obj->mat, obj, &intersect);
    Vector_normalize(&norm);
    result = transmit.ambient;
    Vector light_direction = {1, 0, 1};
    Vector_normalize(&light_direction);
    tmp_ray.d = light_direction;
    tmp_ray.o = intersect;
    if (!ray_intersect(scene, &tmp_ray, ((void *)0))) {
        double angle_factor = ( (0)>(Vector_dot(&light_direction, &norm)) ? (0) : (Vector_dot(&light_direction, &norm)) );
        Color_scalar_mult(&tmp_color, &transmit.diffuse, angle_factor);
        Color_add(&result, &result, &tmp_color);
        if ((obj)->func.inside((Object*) obj, &r.o)) {
            Vector_scalar_mult(&norm, &norm, -1);
        }
    }
    if (!Color_is_black(&transmit.reflect)) {
        ray_reflect(&tmp_ray, &r, &intersect, &norm);
        tmp_color = trace_ray(renderer, scene, &tmp_ray, depth+1, n1);
        tmp_color = (Color) {
            tmp_color.r * (transmit.reflect.r / 255.0),
            tmp_color.g * (transmit.reflect.g / 255.0),
            tmp_color.b * (transmit.reflect.b / 255.0)
        };
        Color_add(&result, &result, &tmp_color);
    }
    if (!Color_is_black(&transmit.refract)) {
        Vector tmp_vector;
        Vector refracted;
        double n2;
        if (fabs(n1 - 1.000277) <= ( 1.0e-5 )) {
            n2 = 1.1;
        } else {
            n2 = 1.000277;
        }
        double ratio = n1 / n2;
        double cos_theta_i = -1 * Vector_dot(&r.d, &norm);
        double cos_theta_t = sqrt(1 - (ratio*ratio * (1- cos_theta_i*cos_theta_i )));
        if (!__builtin_isnan (cos_theta_t)) {
            Vector_scalar_mult(&refracted, &norm, ratio*cos_theta_i - cos_theta_t);
            if (cos_theta_i < 0) {
                Vector_scalar_mult(&refracted, &refracted, -1);
            }
            Vector_scalar_mult(&tmp_vector, &r.d, ratio);
            Vector_add(&refracted, &refracted, &tmp_vector);
            Vector_assign(&tmp_ray.o, &intersect);
            Vector_assign(&tmp_ray.d, &refracted);
            tmp_color = trace_ray(renderer, scene, &tmp_ray, depth+1, n2);
            Color_mult(&tmp_color, &tmp_color, &transmit.refract);
            Color_add(&result, &result, &tmp_color);
        }
    }
    return result;
}

canonical_original_code:
static struct Color trace_ray(struct Renderer_RayTraceSimple *renderer, struct Scene *scene, struct Ray *_r, unsigned int depth, double n1) {
    struct Vector intersect;
    struct Vector norm;
    struct Object_struct *obj;
    struct Color result = {0, 0, 0};
    struct material_transmit transmit;
    struct Color tmp_color;
    struct Ray r; struct Ray tmp_ray;
    Vector_assign(&r.o, &_r->o);
    Vector_assign(&r.d, &_r->d);
    Vector_normalize(&r.d);
    obj = ray_intersect(scene, &r, &intersect);
    if (!obj || depth > renderer->max_depth) {
        return (struct Color) {0, 0, 0};
    }
    (obj)->func.normal((struct Object_struct *) obj, &intersect, &norm);
    transmit = (obj->mat)->func.get_transmit((struct Material_struct *) obj->mat, obj, &intersect);
    Vector_normalize(&norm);
    result = transmit.ambient;
    struct Vector light_direction = {1, 0, 1};
    Vector_normalize(&light_direction);
    tmp_ray.d = light_direction;
    tmp_ray.o = intersect;
    if (!ray_intersect(scene, &tmp_ray, ((void *)0))) {
        double angle_factor = ( (0)>(Vector_dot(&light_direction, &norm)) ? (0) : (Vector_dot(&light_direction, &norm)) );
        Color_scalar_mult(&tmp_color, &transmit.diffuse, angle_factor);
        Color_add(&result, &result, &tmp_color);
        if ((obj)->func.inside((struct Object_struct *) obj, &r.o)) {
            Vector_scalar_mult(&norm, &norm, -1);
        }
    }
    if (!Color_is_black(&transmit.reflect)) {
        ray_reflect(&tmp_ray, &r, &intersect, &norm);
        tmp_color = trace_ray(renderer, scene, &tmp_ray, depth+1, n1);
        tmp_color = (struct Color) {
            tmp_color.r * (transmit.reflect.r / 255.0),
            tmp_color.g * (transmit.reflect.g / 255.0),
            tmp_color.b * (transmit.reflect.b / 255.0)
        };
        Color_add(&result, &result, &tmp_color);
    }
    if (!Color_is_black(&transmit.refract)) {
        struct Vector tmp_vector;
        struct Vector refracted;
        double n2;
        if (fabs(n1 - 1.000277) <= ( 1.0e-5 )) {
            n2 = 1.1;
        } else {
            n2 = 1.000277;
        }
        double ratio = n1 / n2;
        double cos_theta_i = -1 * Vector_dot(&r.d, &norm);
        double cos_theta_t = sqrt(1 - (ratio*ratio * (1- cos_theta_i*cos_theta_i )));
        if (!__builtin_isnan (cos_theta_t)) {
            Vector_scalar_mult(&refracted, &norm, ratio*cos_theta_i - cos_theta_t);
            if (cos_theta_i < 0) {
                Vector_scalar_mult(&refracted, &refracted, -1);
            }
            Vector_scalar_mult(&tmp_vector, &r.d, ratio);
            Vector_add(&refracted, &refracted, &tmp_vector);
            Vector_assign(&tmp_ray.o, &intersect);
            Vector_assign(&tmp_ray.d, &refracted);
            tmp_color = trace_ray(renderer, scene, &tmp_ray, depth+1, n2);
            Color_mult(&tmp_color, &tmp_color, &transmit.refract);
            Color_add(&result, &result, &tmp_color);
        }
    }
    return result;
}

variable_types: struct Renderer_RayTraceSimple { struct renderer_func_table { _Bool (*render)(struct Renderer_struct { struct renderer_func_table func; } *renderer, struct Scene { struct List { struct ListNode_struct { void *data; struct ListNode_struct *next; } *head; struct ListNode_struct *tail; unsigned int length; } *objects; } *scene, struct Camera_struct { struct camera_func_table { void (*map)(struct Camera_struct *cam, double x, double y, struct Ray { struct Vector { double x; } o; struct Vector d; } *r); } func; struct Vector pos; } *camera, struct Canvas_struct { struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; } *canvas); } func; unsigned int max_depth; } *

variable_types:
scene
struct Scene *

variable_types:
depth
unsigned int

variable_types:
intersect
struct Vector { double x; }

variable_types:
norm
struct Vector { double x; }

variable_types: struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; }

variable_types:
refracted
struct Vector { double x; }

variable_types: ratio double

user_defined_types: struct Renderer_RayTraceSimple { struct renderer_func_table func; unsigned int max_depth; }

user_defined_types: struct renderer_func_table { _Bool (*render)(struct Renderer_struct *renderer, struct Scene *scene, struct Camera_struct *camera, struct Canvas_struct *canvas); }

user_defined_types: struct Camera_struct { struct camera_func_table func; struct Vector pos; }

user_defined_types: struct camera_func_table { void (*map)(struct Camera_struct *cam, double x, double y, struct Ray *r); }

user_defined_types: struct List { struct ListNode_struct *head; struct ListNode_struct *tail; unsigned int length; }


original_code:
_Bool Renderer_RayTraceSimple_render(Renderer* _renderer, Scene* scene, Camera* cam, Canvas* canvas) {
    Renderer_RayTraceSimple* renderer = (Renderer_RayTraceSimple*) _renderer;
    Color* row;
    Ray ray;
    (canvas)->func.start((Canvas*) canvas);
    for (int y=canvas->height-1; y>=0; y--) {
        row = (canvas)->func.get_next_row((Canvas*) canvas);
        for (int x=0; x<canvas->width; x++) {
            (cam)->func.map((Camera*) cam, ((double)x)/canvas->width, ((double)y)/canvas->height, &ray);
            row[x] = trace_ray(renderer, scene, &ray, 0, 1.000277);
        }
        (canvas)->func.write_row((Canvas*) canvas, row);
    }
    (canvas)->func.finish((Canvas*) canvas);
    return 1;
}

canonical_original_code:
_Bool Renderer_RayTraceSimple_render(struct Renderer_struct *_renderer, struct Scene *scene, struct Camera_struct *cam, struct Canvas_struct *canvas) {
    struct Renderer_RayTraceSimple *renderer = (Renderer_RayTraceSimple*) _renderer;
    struct Color *row;
    struct Ray ray;
    (canvas)->func.start((struct Canvas_struct *) canvas);
    for (int y = canvas->height-1; y>=0; y--) {
        row = (canvas)->func.get_next_row((struct Canvas_struct *) canvas);
        for (int x = 0; x<canvas->width; x++) {
            (cam)->func.map((struct Camera_struct *) cam, ((double)x)/canvas->width, ((double)y)/canvas->height, &ray);
            row[x] = trace_ray(renderer, scene, &ray, 0, 1.000277);
        }
        (canvas)->func.write_row((struct Canvas_struct *) canvas, row);
    }
    (canvas)->func.finish((struct Canvas_struct *) canvas);
    return 1;
}

variable_types: struct Renderer_struct { struct renderer_func_table { _Bool (*render)(struct Renderer_struct *renderer, struct Scene { struct List { struct ListNode_struct { void *data; struct ListNode_struct *next; } *head; struct ListNode_struct *tail; unsigned int length; } *objects; } *scene, struct Camera_struct { struct camera_func_table { void (*map)(struct Camera_struct *cam, double x, double y, struct Ray { struct Vector { double x; } o; struct Vector d; } *r); } func; struct Vector pos; } *camera, struct Canvas_struct { struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color { unsigned char r; unsigned char g; unsigned char b; } *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; } *canvas); } func; } *

variable_types:
cam
struct Camera_struct *

variable_types:
canvas
struct Canvas_struct *

variable_types: struct Renderer_RayTraceSimple { struct renderer_func_table { _Bool (*render)(struct Renderer_struct { struct renderer_func_table func; } *renderer, struct Scene { struct List { struct ListNode_struct { void *data; struct ListNode_struct *next; } *head; struct ListNode_struct *tail; unsigned int length; } *objects; } *scene, struct Camera_struct { struct camera_func_table { void (*map)(struct Camera_struct *cam, double x, double y, struct Ray { struct Vector { double x; } o; struct Vector d; } *r); } func; struct Vector pos; } *camera, struct Canvas_struct { struct canvas_func_table { _Bool (*start)(struct Canvas_struct *canvas); struct Color { unsigned char r; unsigned char g; unsigned char b; } *(*get_next_row)(struct Canvas_struct *canvas); _Bool (*write_row)(struct Canvas_struct *canvas, struct Color *row); _Bool (*finish)(struct Canvas_struct *canvas); void (*free)(struct Canvas_struct *canvas); } func; unsigned int width; } *canvas); } func; unsigned int max_depth; } *

variable_types: y int

variable_types: x int


decompiled_code:
__int64 __fastcall Material_Checker_get_transmit(__int64 a1, __int64 a2, double *a3)
{
unsigned int v4; // ebx
double v6; // [rsp+0h] [rbp-48h]
double v7; // [rsp+8h] [rbp-40h]
__int64 v8; // [rsp+24h] [rbp-24h]

v6 = round(*a3);
v4 = *(_DWORD *)(a1 + 8);
v7 = round(a3[1]);
if ( (((unsigned __int8)((int)round(a3[2]) / v4) + (unsigned __int8)((int)v7 / v4) + (unsigned __int8)((int)v6 / v4)) & 1) != 0 )
v8 = *(_QWORD *)(a1 + 12);
else
v8 = *(_QWORD *)(a1 + 24);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, double *a3)
{
unsigned int v4; // ebx
double v6; // [rsp+0h] [rbp-48h]
double v7; // [rsp+8h] [rbp-40h]
__int64 v8; // [rsp+24h] [rbp-24h]

v6 = round(*a3);
v4 = *(_DWORD *)(a1 + 8);
v7 = round(a3[1]);
if ( (((unsigned __int8)((int)round(a3[2]) / v4) + (unsigned __int8)((int)v7 / v4) + (unsigned __int8)((int)v6 / v4)) & 1) != 0 )
v8 = *(_QWORD *)(a1 + 12);
else
v8 = *(_QWORD *)(a1 + 24);
return v8;
}

original_code:
struct material_transmit Material_Checker_get_transmit(const Material* _m, const Object* object, const Point* intersect) {
    Material_Checker* m = (Material_Checker*) _m;
    if (( ((int) round(intersect->x)) / m->scale + ((int) round(intersect->y)) / m->scale + ((int) round(intersect->z)) / m->scale) % 2)
    {
        return m->tile1_transmit;
    } else {
        return m->tile2_transmit;
    }
}

canonical_original_code:
struct material_transmit Material_Checker_get_transmit(struct Material_struct *_m, struct Object_struct *object, struct Vector *intersect) {
    struct Material_Checker *m = (Material_Checker*) _m;
    if (( ((int) round(intersect->x)) / m->scale + ((int) round(intersect->y)) / m->scale + ((int) round(intersect->z)) / m->scale) % 2)
    {
        return m->tile1_transmit;
    } else {
        return m->tile2_transmit;
    }
}

variable_types: struct Material_struct { struct material_func_table { struct material_transmit (*get_transmit)(struct Material_struct *mat, struct Object_struct { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray { struct Vector { double x; } o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct *mat; } *object, struct Vector *intersect); } func; } *

variable_types:
object
struct Object_struct *

variable_types:
intersect
struct Vector *

variable_types: struct Material_Checker { struct material_func_table { struct material_transmit { struct Color { unsigned char r; unsigned char g; unsigned char b; } diffuse; struct Color ambient; struct Color reflect; struct Color refract; } (*get_transmit)(struct Material_struct { struct material_func_table func; } *mat, struct Object_struct { struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray { struct Vector { double x; } o; struct Vector d; } *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); } func; struct Material_struct *mat; } *object, struct Vector *intersect); } func; unsigned int scale; struct material_transmit tile1_transmit; struct material_transmit tile2_transmit; } *

user_defined_types: struct Material_Checker { struct material_func_table func; unsigned int scale; struct material_transmit tile1_transmit; struct material_transmit tile2_transmit; }


decompiled_code:
char *__fastcall Material_Checker_new(int a1)
{
char *result; // rax

result = (char *)malloc(0x28uLL);
*(_QWORD *)result = Material_Checker_get_transmit;
*((_DWORD *)result + 2) = a1;
*(_QWORD *)(result + 12) = 0LL;
*((_DWORD *)result + 5) = 0;
result[24] = 0;
result[25] = 0;
result[26] = 0;
result[27] = -1;
result[28] = -1;
result[29] = -1;
result[30] = 0;
result[31] = 0;
result[32] = 0;
result[33] = 0;
result[34] = 0;
result[35] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(int a1)
{
char *result; // rax

result = (char *)malloc(0x28uLL);
*(_QWORD *)result = Material_Checker_get_transmit;
*((_DWORD *)result + 2) = a1;
*(_QWORD *)(result + 12) = 0LL;
*((_DWORD *)result + 5) = 0;
result[24] = 0;
result[25] = 0;
result[26] = 0;
result[27] = -1;
result[28] = -1;
result[29] = -1;
result[30] = 0;
result[31] = 0;
result[32] = 0;
result[33] = 0;
result[34] = 0;
result[35] = 0;
return result;
}

original_code:
Material* Material_Checker_new(unsigned int scale) {
    Material_Checker* m = (Material_Checker*) malloc(sizeof(Material_Checker));
    m->func = material_checker_func_table;
    m->scale = scale;
    m->tile1_transmit = (struct material_transmit) {
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0}
    };
    m->tile2_transmit = (struct material_transmit) {
        {0, 0, 0},
        {255 ,255, 255},
        {0, 0, 0},
        {0, 0, 0}
    };
    return (Material*) m;
}

canonical_original_code:
struct Material_struct *Material_Checker_new(unsigned int scale) {
    struct Material_Checker *m = (Material_Checker*) malloc(sizeof(Material_Checker));
    m->func = material_checker_func_table;
    m->scale = scale;
    m->tile1_transmit = (struct material_transmit) {
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0}
    };
    m->tile2_transmit = (struct material_transmit) {
        {0, 0, 0},
        {255 ,255, 255},
        {0, 0, 0},
        {0, 0, 0}
    };
    return (struct Material_struct *) m;
}

variable_types:
scale
unsigned int

user_defined_types: struct Object_struct { struct object_func_table func; struct Material_struct *mat; }


decompiled_code:
__int64 Material_new_default()
{
return Material_Solid_new();
}

canonical_decompiled_code:
__int64 func0()
{
return Material_Solid_new();
}

original_code:
Material* Material_new_default() {
    return (Material*) Material_Solid_new();
}

canonical_original_code:
struct Material_struct *Material_new_default() {
    return (struct Material_struct *) Material_Solid_new();
}

user_defined_types: struct object_func_table { _Bool (*ray_intersect)(struct Object_struct *obj, struct Ray *ray, struct Vector *intersect); void (*normal)(struct Object_struct *obj, struct Vector *intersect, struct Vector *normal); _Bool (*inside)(struct Object_struct *obj, struct Vector *point); }


original_code:
void ray_reflect(
        Ray* result,
        const Ray* r,
        const Point* intersect,
        const Vector* norm) {
    result->o.x = intersect->x;
    result->o.y = intersect->y;
    result->o.z = intersect->z;
    double dot_norm_ray = Vector_dot(norm, &r->d);
    double norm_mag_squared = Vector_mag(norm) * Vector_mag(norm);
    double double_proj_mag = 2 * dot_norm_ray / norm_mag_squared;
    result->d.x = r->d.x - double_proj_mag*norm->x;
    result->d.y = r->d.y - double_proj_mag*norm->y;
    result->d.z = r->d.z - double_proj_mag*norm->z;
}

canonical_original_code:
void ray_reflect(struct Ray *result, struct Ray *r, struct Vector *intersect, struct Vector *norm) {
    result->o.x = intersect->x;
    result->o.y = intersect->y;
    result->o.z = intersect->z;
    double dot_norm_ray = Vector_dot(norm, &r->d);
    double norm_mag_squared = Vector_mag(norm) * Vector_mag(norm);
    double double_proj_mag = 2 * dot_norm_ray / norm_mag_squared;
    result->d.x = r->d.x - double_proj_mag*norm->x;
    result->d.y = r->d.y - double_proj_mag*norm->y;
    result->d.z = r->d.z - double_proj_mag*norm->z;
}

variable_types: struct Ray { struct Vector { double x; } o; struct Vector d; } *

variable_types:
r
struct Ray *

variable_types:
norm
struct Vector *


main

canonical_name: main

repo: mbrunton/graph

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

if ( argc != 2 )
{
fprintf(stderr, "usage: %s filename\n", *argv);
exit(1);
}
v3 = read_graph(argv[1]);
print_graph(v3);
v4 = dfs(v3);
fwrite("DFS:\n", 1uLL, 5uLL, stderr);
print_search_tree(v4);
v5 = bfs(v3);
fwrite("BFS:\n", 1uLL, 5uLL, stderr);
print_search_tree(v5);
v6 = dijkstra(v3, 0LL);
print_dist_tree(v6);
return 0;
}

original_code:
int
main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s filename\n", argv[0]);
        exit(1);
    }
    char *filename = argv[1];
    graph_t *G = read_graph(filename);
    print_graph(G);
    search_tree_t *dfs_tree = dfs(G);
    fprintf(stderr, "DFS:\n");
    print_search_tree(dfs_tree);
    search_tree_t *bfs_tree = bfs(G);
    fprintf(stderr, "BFS:\n");
    print_search_tree(bfs_tree);
    dist_tree_t *dij_tree = dijkstra(G, 0);
    print_dist_tree(dij_tree);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s filename\n", argv[0]);
        exit(1);
    }
    char *filename = argv[1];
    struct graph_t *G = read_graph(filename);
    print_graph(G);
    struct search_tree_t *dfs_tree = dfs(G);
    fprintf(stderr, "DFS:\n");
    print_search_tree(dfs_tree);
    struct search_tree_t *bfs_tree = bfs(G);
    fprintf(stderr, "BFS:\n");
    print_search_tree(bfs_tree);
    struct dist_tree_t *dij_tree = dijkstra(G, 0);
    print_dist_tree(dij_tree);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
filename
char *

variable_types: struct search_tree_t { struct graph_t { struct edgelist_t { struct edge_t { int u; int v; int w; struct edge_t *next; } *head; } *edgelists; int n; int directed; } *T; int *parents; int *arrival_times; int arrival_counter; int k; } *

variable_types: struct dist_tree_t { int s; int n; int *dists; int *parents; } *

return_type: int

user_defined_types: struct graph_t { struct edgelist_t *edgelists; int n; int directed; }

user_defined_types: struct search_tree_t { struct graph_t *T; int *parents; int *arrival_times; int arrival_counter; int k; }

user_defined_types: struct edgelist_t { struct edge_t *head; }


decompiled_code:
_QWORD *create_empty_queue()
{
_QWORD *result; // rax

result = malloc(0x18uLL);
if ( !result )
{
fwrite("Error: malloc failed\n", 1uLL, 0x15uLL, stderr);
exit(1);
}
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
return result;
}

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

result = malloc(0x18uLL);
if ( !result )
{
fwrite("Error: malloc failed\n", 1uLL, 0x15uLL, stderr);
exit(1);
}
*result = 0LL;
result[1] = 0LL;
*((_DWORD *)result + 4) = 0;
return result;
}

original_code:
queue_t *
create_empty_queue(void) {
    queue_t *Q = (queue_t *) malloc(sizeof(queue_t));
    if (((void *)0) == Q) {
        fprintf(stderr, "Error: malloc failed\n");
        exit(1);
    }
    Q->head = ((void *)0);
    Q->tail = ((void *)0);
    Q->size = 0;
    return Q;
}

canonical_original_code:
struct queue_t *create_empty_queue() {
    struct queue_t *Q = (queue_t *) malloc(sizeof(queue_t));
    if (((void *)0) == Q) {
        fprintf(stderr, "Error: malloc failed\n");
        exit(1);
    }
    Q->head = ((void *)0);
    Q->tail = ((void *)0);
    Q->size = 0;
    return Q;
}

variable_types: struct queue_t { struct item_t { void *data; struct item_t *next; } *head; struct item_t *tail; int size; } *

user_defined_types: struct queue_t { struct item_t *head; struct item_t *tail; int size; }


decompiled_code:
__int64 __fastcall q_pop(__int64 **a1)
{
int v1; // eax
__int64 *v2; // rdx
__int64 v3; // rbx

v1 = *((_DWORD *)a1 + 4);
if ( !v1 )
{
fwrite("Error: attempting to pop empty queue\n", 1uLL, 0x25uLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 49LL);
exit(1);
}
v2 = *a1;
v3 = **a1;
if ( v1 == 1 )
{
a1[1] = 0LL;
*a1 = 0LL;
}
else
{
*a1 = (__int64 *)v2[1];
}
*((_DWORD *)a1 + 4) = v1 - 1;
free(v2);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 **a1)
{
int v1; // eax
__int64 *v2; // rdx
__int64 v3; // rbx

v1 = *((_DWORD *)a1 + 4);
if ( !v1 )
{
fwrite("Error: attempting to pop empty queue\n", 1uLL, 0x25uLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 49LL);
exit(1);
}
v2 = *a1;
v3 = **a1;
if ( v1 == 1 )
{
a1[1] = 0LL;
*a1 = 0LL;
}
else
{
*a1 = (__int64 *)v2[1];
}
*((_DWORD *)a1 + 4) = v1 - 1;
free(v2);
return v3;
}

original_code:
void *
q_pop(queue_t *Q) {
    if (0 == Q->size) {
        fprintf(stderr, "Error: attempting to pop empty queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 49);
        exit(1);
    }
    item_t *old_head = Q->head;
    void *data = Q->head->data;
    if (1 == Q->size) {
        Q->head = Q->tail = ((void *)0);
    } else {
        Q->head = Q->head->next;
    }
    Q->size--;
    free(old_head);
    return data;
}

canonical_original_code:
void *q_pop(struct queue_t *Q) {
    if (0 == Q->size) {
        fprintf(stderr, "Error: attempting to pop empty queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 49);
        exit(1);
    }
    struct item_t *old_head = Q->head;
    void *data = Q->head->data;
    if (1 == Q->size) {
        Q->head = Q->tail = ((void *)0);
    } else {
        Q->head = Q->head->next;
    }
    Q->size--;
    free(old_head);
    return data;
}

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

variable_types:
data
void *

return_type: void *


decompiled_code:
__int64 __fastcall q_peek(_DWORD *a1)
{
if ( !a1[4] )
{
fwrite("Error: attempting to peek empty queue\n", 1uLL, 0x26uLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 71LL);
exit(1);
}
return **(_QWORD **)a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
if ( !a1[4] )
{
fwrite("Error: attempting to peek empty queue\n", 1uLL, 0x26uLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 71LL);
exit(1);
}
return **(_QWORD **)a1;
}

original_code:
void *
q_peek(queue_t *Q) {
    if (0 == Q->size) {
        fprintf(stderr, "Error: attempting to peek empty queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 71);
        exit(1);
    }
    return Q->head->data;
}

canonical_original_code:
void *q_peek(struct queue_t *Q) {
    if (0 == Q->size) {
        fprintf(stderr, "Error: attempting to peek empty queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "queue.c", 71);
        exit(1);
    }
    return Q->head->data;
}

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


decompiled_code:
_BOOL8 __fastcall q_not_empty(__int64 a1)
{
return *(_DWORD *)(a1 + 16) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
return *(_DWORD *)(a1 + 16) != 0;
}

original_code:
int
q_not_empty(queue_t *Q) {
    return Q->size != 0;
}

canonical_original_code:
int q_not_empty(struct queue_t *Q) {
    return Q->size != 0;
}


decompiled_code:
int __fastcall print_queue(unsigned int ***a1)
{
unsigned int **i; // rbx

fwrite("QUEUE PRINTOUT:\n", 1uLL, 0x10uLL, stderr);
if ( !*((_DWORD *)a1 + 4) )
fwrite("(queue empty)\n", 1uLL, 0xEuLL, stderr);
for ( i = *a1; i; i = (unsigned int **)i[1] )
fprintf(stderr, "%d\n", **i);
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func4(unsigned int ***a1)
{
unsigned int **i; // rbx

fwrite("QUEUE PRINTOUT:\n", 1uLL, 0x10uLL, stderr);
if ( !*((_DWORD *)a1 + 4) )
fwrite("(queue empty)\n", 1uLL, 0xEuLL, stderr);
for ( i = *a1; i; i = (unsigned int **)i[1] )
fprintf(stderr, "%d\n", **i);
return fputc(10, stderr);
}

original_code:
void
print_queue(queue_t *Q) {
    fprintf(stderr, "QUEUE PRINTOUT:\n");
    if (Q->size == 0) {
        fprintf(stderr, "(queue empty)\n");
    }
    item_t *curr;
    int *value;
    for (curr = Q->head; curr != ((void *)0); curr = curr->next) {
        value = (int *) curr->data;
        fprintf(stderr, "%d\n", *value);
    }
    fprintf(stderr, "\n");
}

canonical_original_code:
void print_queue(struct queue_t *Q) {
    fprintf(stderr, "QUEUE PRINTOUT:\n");
    if (Q->size == 0) {
        fprintf(stderr, "(queue empty)\n");
    }
    struct item_t *curr;
    int *value;
    for (curr = Q->head; curr != ((void *)0); curr = curr->next) {
        value = (int *) curr->data;
        fprintf(stderr, "%d\n", *value);
    }
    fprintf(stderr, "\n");
}

variable_types:
value
int *


original_code:
void
pq_push(pq_t *Q, void *data, int priority) {
    if (Q->size >= Q->capacity) {
        fprintf(stderr, "Error: priority queue is full\n");
        fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 30);
    }
    int i = Q->size;
    Q->entries[i].data = data;
    Q->entries[i].p = priority;
    Q->size++;
    entry_t temp_entry;
    while ( ( ((i)-1)/2 ) >= 0 && Q->entries[i].p < Q->entries[ ( ((i)-1)/2 ) ].p ) {
        temp_entry = Q->entries[ ( ((i)-1)/2 ) ];
        Q->entries[ ( ((i)-1)/2 ) ] = Q->entries[i];
        Q->entries[i] = temp_entry;
        i = ( ((i)-1)/2 );
    }
}

canonical_original_code:
void pq_push(struct pq_t *Q, void *data, int priority) {
    if (Q->size >= Q->capacity) {
        fprintf(stderr, "Error: priority queue is full\n");
        fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 30);
    }
    int i = Q->size;
    Q->entries[i].data = data;
    Q->entries[i].p = priority;
    Q->size++;
    struct entry_t temp_entry;
    while ( ( ((i)-1)/2 ) >= 0 && Q->entries[i].p < Q->entries[ ( ((i)-1)/2 ) ].p ) {
        temp_entry = Q->entries[ ( ((i)-1)/2 ) ];
        Q->entries[ ( ((i)-1)/2 ) ] = Q->entries[i];
        Q->entries[i] = temp_entry;
        i = ( ((i)-1)/2 );
    }
}

variable_types: struct pq_t { unsigned long size; unsigned long capacity; struct entry_t { void *data; int p; } *entries; } *

variable_types: priority int

variable_types: i int

user_defined_types: struct pq_t { unsigned long size; unsigned long capacity; struct entry_t *entries; }


decompiled_code:
__int64 __fastcall update_priority(_QWORD *a1, __int64 a2, int a3)
{
__int64 result; // rax
__int64 v5; // rdx

if ( !*a1 )
goto LABEL_5;
result = a1[2];
v5 = 0LL;
while ( *(_QWORD *)result != a2 )
{
++v5;
result += 16LL;
if ( *a1 == v5 )
goto LABEL_5;
}
*(_DWORD *)(result + 8) = a3;
if ( *a1 == v5 )
{
LABEL_5:
fwrite("Error: could not locate item in priority queue\n", 1uLL, 0x2FuLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 62LL);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, int a3)
{
__int64 result; // rax
__int64 v5; // rdx

if ( !*a1 )
goto LABEL_5;
result = a1[2];
v5 = 0LL;
while ( *(_QWORD *)result != a2 )
{
++v5;
result += 16LL;
if ( *a1 == v5 )
goto LABEL_5;
}
*(_DWORD *)(result + 8) = a3;
if ( *a1 == v5 )
{
LABEL_5:
fwrite("Error: could not locate item in priority queue\n", 1uLL, 0x2FuLL, stderr);
fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 62LL);
exit(1);
}
return result;
}

original_code:
void
update_priority(pq_t *Q, void *data, int priority) {
    int i;
    for (i = 0; i < Q->size; i++) {
        if (Q->entries[i].data == data) {
            Q->entries[i].p = priority;
            break;
        }
    }
    if (i == Q->size) {
        fprintf(stderr, "Error: could not locate item in priority queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 62);
        exit(1);
    }
}

canonical_original_code:
void update_priority(struct pq_t *Q, void *data, int priority) {
    int i;
    for (i = 0; i < Q->size; i++) {
        if (Q->entries[i].data == data) {
            Q->entries[i].p = priority;
            break;
        }
    }
    if (i == Q->size) {
        fprintf(stderr, "Error: could not locate item in priority queue\n");
        fprintf(stderr, "file: %s\tline: %d\n", "priority_queue.c", 62);
        exit(1);
    }
}


original_code:
void *
pq_pop(pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 71);
        exit(1);
    }
    void *top = Q->entries[0].data;
    Q->entries[0] = Q->entries[Q->size - 1];
    Q->entries[Q->size - 1].data = ((void *)0);
    Q->entries[Q->size - 1].p = -1;
    Q->size--;
    entry_t temp_entry;
    int min_p;
    int min_child;
    int i = 0;
    while ((1==1)) {
        if ( (2*(i) + 1) < Q->size ) {
            if ( (2*(i) + 2) < Q->size ) {
                if (Q->entries[(2*(i) + 1)].p <= Q->entries[(2*(i) + 2)].p) {
                    min_p = Q->entries[(2*(i) + 1)].p;
                    min_child = (2*(i) + 1);
                } else {
                    min_p = Q->entries[(2*(i) + 2)].p;
                    min_child = (2*(i) + 2);
                }
                if (Q->entries[i].p > min_p) {
                    temp_entry = Q->entries[min_child];
                    Q->entries[min_child] = Q->entries[i];
                    Q->entries[i] = temp_entry;
                    i = min_child;
                } else {
                    break;
                }
            } else {
                if (Q->entries[i].p > Q->entries[(2*(i) + 1)].p) {
                    temp_entry = Q->entries[(2*(i) + 1)];
                    Q->entries[(2*(i) + 1)] = Q->entries[i];
                    Q->entries[i] = Q->entries[(2*(i) + 1)];
                    i = (2*(i) + 1);
                } else {
                    break;
                }
            }
        } else {
            break;
        }
    }
    return top;
}

canonical_original_code:
void *pq_pop(struct pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 71);
        exit(1);
    }
    void *top = Q->entries[0].data;
    Q->entries[0] = Q->entries[Q->size - 1];
    Q->entries[Q->size - 1].data = ((void *)0);
    Q->entries[Q->size - 1].p = -1;
    Q->size--;
    struct entry_t temp_entry;
    int min_p;
    int min_child;
    int i = 0;
    while ((1==1)) {
        if ( (2*(i) + 1) < Q->size ) {
            if ( (2*(i) + 2) < Q->size ) {
                if (Q->entries[(2*(i) + 1)].p <= Q->entries[(2*(i) + 2)].p) {
                    min_p = Q->entries[(2*(i) + 1)].p;
                    min_child = (2*(i) + 1);
                } else {
                    min_p = Q->entries[(2*(i) + 2)].p;
                    min_child = (2*(i) + 2);
                }
                if (Q->entries[i].p > min_p) {
                    temp_entry = Q->entries[min_child];
                    Q->entries[min_child] = Q->entries[i];
                    Q->entries[i] = temp_entry;
                    i = min_child;
                } else {
                    break;
                }
            } else {
                if (Q->entries[i].p > Q->entries[(2*(i) + 1)].p) {
                    temp_entry = Q->entries[(2*(i) + 1)];
                    Q->entries[(2*(i) + 1)] = Q->entries[i];
                    Q->entries[i] = Q->entries[(2*(i) + 1)];
                    i = (2*(i) + 1);
                } else {
                    break;
                }
            }
        } else {
            break;
        }
    }
    return top;
}

variable_types:
top
void *


decompiled_code:
__int64 __fastcall pq_peek(__int64 a1)
{
if ( !*(_QWORD *)a1 )
{
fwrite("Error: priority queue is empty\n", 1uLL, 0x1FuLL, stderr);
fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 135LL);
exit(1);
}
return **(_QWORD **)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
if ( !*(_QWORD *)a1 )
{
fwrite("Error: priority queue is empty\n", 1uLL, 0x1FuLL, stderr);
fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 135LL);
exit(1);
}
return **(_QWORD **)(a1 + 16);
}

original_code:
void *
pq_peek(pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 135);
        exit(1);
    }
    return Q->entries[0].data;
}

canonical_original_code:
void *pq_peek(struct pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 135);
        exit(1);
    }
    return Q->entries[0].data;
}


decompiled_code:
__int64 __fastcall pq_peek_priority(_QWORD *a1)
{
if ( !*a1 )
{
fwrite("Error: priority queue is empty\n", 1uLL, 0x1FuLL, stderr);
fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 147LL);
exit(1);
}
return *(unsigned int *)(a1[2] + 8LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1)
{
if ( !*a1 )
{
fwrite("Error: priority queue is empty\n", 1uLL, 0x1FuLL, stderr);
fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 147LL);
exit(1);
}
return *(unsigned int *)(a1[2] + 8LL);
}

original_code:
int
pq_peek_priority(pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 147);
        exit(1);
    }
    return Q->entries[0].p;
}

canonical_original_code:
int pq_peek_priority(struct pq_t *Q) {
    if (Q->size <= 0) {
        fprintf(stderr, "Error: priority queue is empty\n");
        fprintf(stderr, "file: %s\tline:%d\n", "priority_queue.c", 147);
        exit(1);
    }
    return Q->entries[0].p;
}


decompiled_code:
_BOOL8 __fastcall pq_not_empty(_QWORD *a1)
{
return *a1 != 0LL;
}

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

original_code:
int
pq_not_empty(pq_t *Q) {
    return Q->size != 0;
}

canonical_original_code:
int pq_not_empty(struct pq_t *Q) {
    return Q->size != 0;
}


decompiled_code:
int __fastcall print_pq(unsigned __int64 *a1)
{
unsigned __int64 v1; // rbx

fwrite("PRIORITY QUEUE PRINTOUT:\n", 1uLL, 0x19uLL, stderr);
if ( *a1 || (fwrite("(queue empty)\n", 1uLL, 0xEuLL, stderr), *a1) )
{
v1 = 0LL;
do
{
fprintf(stderr, "item #%d: p = %d\n", (unsigned int)v1, *(unsigned int *)(a1[2] + 16 * v1 + 8));
++v1;
}
while ( *a1 > v1 );
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func6(unsigned __int64 *a1)
{
unsigned __int64 v1; // rbx

fwrite("PRIORITY QUEUE PRINTOUT:\n", 1uLL, 0x19uLL, stderr);
if ( *a1 || (fwrite("(queue empty)\n", 1uLL, 0xEuLL, stderr), *a1) )
{
v1 = 0LL;
do
{
fprintf(stderr, "item #%d: p = %d\n", (unsigned int)v1, *(unsigned int *)(a1[2] + 16 * v1 + 8));
++v1;
}
while ( *a1 > v1 );
}
return fputc(10, stderr);
}

original_code:
void
print_pq(pq_t *Q) {
    fprintf(stderr, "PRIORITY QUEUE PRINTOUT:\n");
    if (Q->size == 0) {
        fprintf(stderr, "(queue empty)\n");
    }
    int i;
    for (i = 0; i < Q->size; i++) {
        fprintf(stderr, "item #%d: p = %d\n", i, Q->entries[i].p);
    }
    fprintf(stderr, "\n");
}

canonical_original_code:
void print_pq(struct pq_t *Q) {
    fprintf(stderr, "PRIORITY QUEUE PRINTOUT:\n");
    if (Q->size == 0) {
        fprintf(stderr, "(queue empty)\n");
    }
    int i;
    for (i = 0; i < Q->size; i++) {
        fprintf(stderr, "item #%d: p = %d\n", i, Q->entries[i].p);
    }
    fprintf(stderr, "\n");
}


printAllCredits

repo: mbrx/axe

decompiled_code:
void printAllCredits()
{
__int64 v0; // rbx
unsigned int v1; // eax

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
printf("%s : ", *(const char **)(entFileNames + 8 * v0));
if ( !quiet )
printf("Printing credits for %s\n", *(const char **)(entFileNames + 8 * v0));
v1 = readCredits((unsigned int)v0);
printf("%d\n", v1);
++v0;
}
while ( nEntFiles > (int)v0 );
}
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rbx
unsigned int v1; // eax

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
printf("%s : ", *(const char **)(entFileNames + 8 * v0));
if ( !quiet )
printf("Printing credits for %s\n", *(const char **)(entFileNames + 8 * v0));
v1 = readCredits((unsigned int)v0);
printf("%d\n", v1);
++v0;
}
while ( nEntFiles > (int)v0 );
}
}

original_code:
void printAllCredits() {
  for(int i=0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    if(!quiet) printf("Printing credits for %s\n",entFileNames[i]);
    int credits = readCredits(i);
    printf("%d\n",credits);
  }
}

canonical_original_code:
void printAllCredits() {
  for(int i = 0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    if(!quiet) printf("Printing credits for %s\n",entFileNames[i]);
    int credits = readCredits(i);
    printf("%d\n",credits);
  }
}

variable_types: credits int


printAllInventory

decompiled_code:
__int64 printAllInventory()
{
__int64 v0; // rbx
__int64 result; // rax

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
puts(*(const char **)(entFileNames + 8 * v0));
if ( !quiet )
puts("<blockId> <amount>");
result = printInventory((unsigned int)v0++);
}
while ( nEntFiles > (int)v0 );
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbx
__int64 result; // rax

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
puts(*(const char **)(entFileNames + 8 * v0));
if ( !quiet )
puts("<blockId> <amount>");
result = printInventory((unsigned int)v0++);
}
while ( nEntFiles > (int)v0 );
}
return result;
}

original_code:
void printAllInventory() {
  for(int i=0;i<nEntFiles;i++) {
    printf("%s\n",entFileNames[i]);
    if(!quiet) printf("<blockId> <amount>\n");
    printInventory(i);
  }
}

canonical_original_code:
void printAllInventory() {
  for(int i = 0;i<nEntFiles;i++) {
    printf("%s\n",entFileNames[i]);
    if(!quiet) printf("<blockId> <amount>\n");
    printInventory(i);
  }
}


printAllSector

decompiled_code:
void printAllSector()
{
__int64 v0; // rbx
int v1[5]; // [rsp+0h] [rbp-14h] BYREF

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
printf("%s : ", *(const char **)(entFileNames + 8 * v0));
if ( (unsigned int)getSector((unsigned int)v0, v1) )
printf("%d %d %d\n", (unsigned int)v1[0], (unsigned int)v1[1], (unsigned int)v1[2]);
else
puts("fail");
++v0;
}
while ( nEntFiles > (int)v0 );
}
}

canonical_decompiled_code:
void func2()
{
__int64 v0; // rbx
int v1[5]; // [rsp+0h] [rbp-14h] BYREF

if ( nEntFiles > 0 )
{
v0 = 0LL;
do
{
printf("%s : ", *(const char **)(entFileNames + 8 * v0));
if ( (unsigned int)getSector((unsigned int)v0, v1) )
printf("%d %d %d\n", (unsigned int)v1[0], (unsigned int)v1[1], (unsigned int)v1[2]);
else
puts("fail");
++v0;
}
while ( nEntFiles > (int)v0 );
}
}

original_code:
void printAllSector() {
  int xyz[3];
  for(int i=0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    if(getSector(i,xyz) == 0) printf("fail\n");
    else printf("%d %d %d\n",xyz[0],xyz[1],xyz[2]);
  }
}

canonical_original_code:
void printAllSector() {
  int xyz[3];
  for(int i = 0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    if(getSector(i,xyz) == 0) printf("fail\n");
    else printf("%d %d %d\n",xyz[0],xyz[1],xyz[2]);
  }
}

variable_types:
xyz
int [3]


setAllCredits

decompiled_code:
void __fastcall setAllCredits(unsigned int a1)
{
__int64 v1; // rbx
unsigned int v2; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Setting %d credits to %s\n", a1, *(const char **)(entFileNames + 8 * v1));
v2 = max(0LL, a1);
writeCredits((unsigned int)v1++, v2);
}
while ( nEntFiles > (int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int a1)
{
__int64 v1; // rbx
unsigned int v2; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Setting %d credits to %s\n", a1, *(const char **)(entFileNames + 8 * v1));
v2 = max(0LL, a1);
writeCredits((unsigned int)v1++, v2);
}
while ( nEntFiles > (int)v1 );
}
}

original_code:
void setAllCredits(int credits) {
  for(int i=0;i<nEntFiles;i++) {
    if(!quiet) printf("Setting %d credits to %s\n",credits,entFileNames[i]);
    writeCredits(i,max(0,credits));
  }
}

canonical_original_code:
void setAllCredits(int credits) {
  for(int i = 0;i<nEntFiles;i++) {
    if(!quiet) printf("Setting %d credits to %s\n",credits,entFileNames[i]);
    writeCredits(i,max(0,credits));
  }
}


addAllCredits

decompiled_code:
void __fastcall addAllCredits(unsigned int a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Adding %d credits to %s\n", a1, *(const char **)(entFileNames + 8 * v1));
v2 = readCredits((unsigned int)v1);
v3 = max(0LL, a1 + v2);
writeCredits((unsigned int)v1++, v3);
}
while ( nEntFiles > (int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Adding %d credits to %s\n", a1, *(const char **)(entFileNames + 8 * v1));
v2 = readCredits((unsigned int)v1);
v3 = max(0LL, a1 + v2);
writeCredits((unsigned int)v1++, v3);
}
while ( nEntFiles > (int)v1 );
}
}

original_code:
void addAllCredits(int credits) {
  for(int i=0;i<nEntFiles;i++) {
    if(!quiet) printf("Adding %d credits to %s\n",credits,entFileNames[i]);
    writeCredits(i,max(0,readCredits(i)+credits));
  }
}

canonical_original_code:
void addAllCredits(int credits) {
  for(int i = 0;i<nEntFiles;i++) {
    if(!quiet) printf("Adding %d credits to %s\n",credits,entFileNames[i]);
    writeCredits(i,max(0,readCredits(i)+credits));
  }
}


multAllCredits

decompiled_code:
void __fastcall multAllCredits(float a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Multiplying credits of %s by %.3f\n", *(const char **)(entFileNames + 8 * v1), a1);
v2 = readCredits((unsigned int)v1);
v3 = max(0LL, (unsigned int)(int)(float)((float)v2 * a1));
writeCredits((unsigned int)v1++, v3);
}
while ( nEntFiles > (int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func5(float a1)
{
__int64 v1; // rbx
int v2; // eax
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
if ( !quiet )
printf("Multiplying credits of %s by %.3f\n", *(const char **)(entFileNames + 8 * v1), a1);
v2 = readCredits((unsigned int)v1);
v3 = max(0LL, (unsigned int)(int)(float)((float)v2 * a1));
writeCredits((unsigned int)v1++, v3);
}
while ( nEntFiles > (int)v1 );
}
}

original_code:
void multAllCredits(float factor) {
  for(int i=0;i<nEntFiles;i++) {
    if(!quiet) printf("Multiplying credits of %s by %.3f\n",entFileNames[i],factor);
    writeCredits(i,max(0,(int)(readCredits(i)*factor)));
  }
}

canonical_original_code:
void multAllCredits(float factor) {
  for(int i = 0;i<nEntFiles;i++) {
    if(!quiet) printf("Multiplying credits of %s by %.3f\n",entFileNames[i],factor);
    writeCredits(i,max(0,(int)(readCredits(i)*factor)));
  }
}

variable_types: factor float


printAllBlock

decompiled_code:
void __fastcall printAllBlock(unsigned int a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
printf("%s : ", (const char *)*entFileNames);
v1 = 0;
v2 = -1;
while ( 1 )
{
v3 = findBlockIndex(0LL, a1, v2);
v2 = v3;
if ( v3 == -1 )
break;
v1 += getValueAsInt(0LL, v3);
}
printf("%d\n", v1);
}
}

canonical_decompiled_code:
void __fastcall func6(unsigned int a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
unsigned int v3; // eax

if ( nEntFiles > 0 )
{
printf("%s : ", (const char *)*entFileNames);
v1 = 0;
v2 = -1;
while ( 1 )
{
v3 = findBlockIndex(0LL, a1, v2);
v2 = v3;
if ( v3 == -1 )
break;
v1 += getValueAsInt(0LL, v3);
}
printf("%d\n", v1);
}
}

original_code:
void printAllBlock(int block) {
  for(int i=0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    int prevIndex=-1;
    for(int total=0;;) {
      int index=findBlockIndex(i,block, prevIndex);
      if(index == -1) {
 printf("%d\n",total);
 return;
      }
      prevIndex=index;
      total += getValueAsInt(i,index);
    }
  }
}

canonical_original_code:
void printAllBlock(int block) {
  for(int i = 0;i<nEntFiles;i++) {
    printf("%s : ",entFileNames[i]);
    int prevIndex = -1;
    for(int total = 0;;) {
      int index = findBlockIndex(i,block, prevIndex);
      if(index == -1) {
 printf("%d\n",total);
 return;
      }
      prevIndex=index;
      total += getValueAsInt(i,index);
    }
  }
}

variable_types: block int

variable_types: prevIndex int

variable_types: total int

variable_types: index int


setBlockAmount

decompiled_code:
__int64 __fastcall setBlockAmount(unsigned int a1, unsigned int a2, int a3)
{
if ( !a3 )
puts("Sorry, we cannot delete items yet - for now it will just have to be a zero entry item");
return findBlockIndex(a1, a2, 0xFFFFFFFFLL);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, unsigned int a2, int a3)
{
if ( !a3 )
puts("Sorry, we cannot delete items yet - for now it will just have to be a zero entry item");
return findBlockIndex(a1, a2, 0xFFFFFFFFLL);
}

original_code:
void setBlockAmount(int entId, int block, int amount) {
  if(amount == 0) {
    printf("Sorry, we cannot delete items yet - for now it will just have to be a zero entry item\n");
  }
  int index=findBlockIndex(entId,block,-1);
}

canonical_original_code:
void setBlockAmount(int entId, int block, int amount) {
  if(amount == 0) {
    printf("Sorry, we cannot delete items yet - for now it will just have to be a zero entry item\n");
  }
  int index = findBlockIndex(entId,block,-1);
}

variable_types: entId int

variable_types: amount int


getBlockAmount

decompiled_code:
__int64 __fastcall getBlockAmount(unsigned int a1, unsigned int a2)
{
unsigned int i; // ebp
unsigned int v3; // eax

for ( i = 0; ; i += getValueAsInt(a1, v3) )
{
v3 = findBlockIndex(a1, a2, 0xFFFFFFFFLL);
if ( v3 == -1 )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, unsigned int a2)
{
unsigned int i; // ebp
unsigned int v3; // eax

for ( i = 0; ; i += getValueAsInt(a1, v3) )
{
v3 = findBlockIndex(a1, a2, 0xFFFFFFFFLL);
if ( v3 == -1 )
break;
}
return i;
}

original_code:
int getBlockAmount(int entId, int block) {
  int total=0;
  int prev=-1;
  for(;;) {
    int index=findBlockIndex(entId,block,prev);
    if(index == -1) return total;
    total += getValueAsInt(entId,index);
  }
  return 0;
}

canonical_original_code:
int getBlockAmount(int entId, int block) {
  int total = 0;
  int prev = -1;
  for(;;) {
    int index = findBlockIndex(entId,block,prev);
    if(index == -1) return total;
    total += getValueAsInt(entId,index);
  }
  return 0;
}

variable_types: prev int


setAllBlock

decompiled_code:
int __fastcall setAllBlock(unsigned int a1, unsigned int a2)
{
int result; // eax
__int64 v3; // r12
unsigned int v4; // eax
unsigned int v5; // ebp
int v6; // ebx
unsigned int v7; // eax

result = printf("SetAllBlock %d %d\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
printf("Setting block %d in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v5 = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
if ( v5 == -1 )
{
if ( !quiet )
printf("Block %d not previously existing in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v7 = max(0LL, a2);
result = createNewBlockEntry((unsigned int)v3, a1, v7);
}
else
{
v4 = max(0LL, a2);
setValueAsInt((unsigned int)v3, v5, v4);
while ( 1 )
{
result = findBlockIndex((unsigned int)v3, a1, v5);
v6 = result;
if ( result == -1 )
break;
setValueAsInt((unsigned int)v3, v5, 0LL);
v5 = v6;
}
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1, unsigned int a2)
{
int result; // eax
__int64 v3; // r12
unsigned int v4; // eax
unsigned int v5; // ebp
int v6; // ebx
unsigned int v7; // eax

result = printf("SetAllBlock %d %d\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
printf("Setting block %d in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v5 = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
if ( v5 == -1 )
{
if ( !quiet )
printf("Block %d not previously existing in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v7 = max(0LL, a2);
result = createNewBlockEntry((unsigned int)v3, a1, v7);
}
else
{
v4 = max(0LL, a2);
setValueAsInt((unsigned int)v3, v5, v4);
while ( 1 )
{
result = findBlockIndex((unsigned int)v3, a1, v5);
v6 = result;
if ( result == -1 )
break;
setValueAsInt((unsigned int)v3, v5, 0LL);
v5 = v6;
}
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

original_code:
void setAllBlock(int block, int amount) {
  printf("SetAllBlock %d %d\n",block,amount);
  for(int i=0;i<nEntFiles;i++) {
    printf("Setting block %d in %s\n",block,entFileNames[i]);
    int first=findBlockIndex(i,block,-1);
    if(first != -1) {
      setValueAsInt(i,first,max(0,amount));
      while(1) {
 int next=findBlockIndex(i,block,first);
 if(next == -1) break;
 setValueAsInt(i,first,0);
 first=next;
      }
    }
    else {
      if(!quiet) printf("Block %d not previously existing in %s\n",block,entFileNames[i]);
      createNewBlockEntry(i, block, max(0,amount));
    }
  }
}

canonical_original_code:
void setAllBlock(int block, int amount) {
  printf("SetAllBlock %d %d\n",block,amount);
  for(int i = 0;i<nEntFiles;i++) {
    printf("Setting block %d in %s\n",block,entFileNames[i]);
    int first = findBlockIndex(i,block,-1);
    if(first != -1) {
      setValueAsInt(i,first,max(0,amount));
      while(1) {
 int next = findBlockIndex(i,block,first);
 if(next == -1) break;
 setValueAsInt(i,first,0);
 first=next;
      }
    }
    else {
      if(!quiet) printf("Block %d not previously existing in %s\n",block,entFileNames[i]);
      createNewBlockEntry(i, block, max(0,amount));
    }
  }
}

variable_types: first int

variable_types: next int


addAllBlock

decompiled_code:
int __fastcall addAllBlock(unsigned int a1, unsigned int a2)
{
int result; // eax
__int64 v3; // rbx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // ebp

result = printf("AddAllBlock %d %d\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
v7 = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
v8 = v7;
if ( v7 == -1 )
{
if ( !quiet )
printf("Block %d not previously existing in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v6 = max(0LL, a2);
result = createNewBlockEntry((unsigned int)v3, a1, v6);
}
else
{
v4 = getValueAsInt((unsigned int)v3, v7);
v5 = max(0LL, v4 + a2);
result = setValueAsInt((unsigned int)v3, v8, v5);
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1, unsigned int a2)
{
int result; // eax
__int64 v3; // rbx
int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // ebp

result = printf("AddAllBlock %d %d\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
v7 = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
v8 = v7;
if ( v7 == -1 )
{
if ( !quiet )
printf("Block %d not previously existing in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
v6 = max(0LL, a2);
result = createNewBlockEntry((unsigned int)v3, a1, v6);
}
else
{
v4 = getValueAsInt((unsigned int)v3, v7);
v5 = max(0LL, v4 + a2);
result = setValueAsInt((unsigned int)v3, v8, v5);
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

original_code:
void addAllBlock(int block, int amount) {
  printf("AddAllBlock %d %d\n",block,amount);
  for(int i=0;i<nEntFiles;i++) {
    int index=findBlockIndex(i,block,-1);
    if(index != -1) setValueAsInt(i,index,max(0,getValueAsInt(i,index)+amount));
    else {
      if(!quiet) printf("Block %d not previously existing in %s\n",block,entFileNames[i]);
      createNewBlockEntry(i, block, max(0,amount));
    }
  }
}

canonical_original_code:
void addAllBlock(int block, int amount) {
  printf("AddAllBlock %d %d\n",block,amount);
  for(int i = 0;i<nEntFiles;i++) {
    int index = findBlockIndex(i,block,-1);
    if(index != -1) setValueAsInt(i,index,max(0,getValueAsInt(i,index)+amount));
    else {
      if(!quiet) printf("Block %d not previously existing in %s\n",block,entFileNames[i]);
      createNewBlockEntry(i, block, max(0,amount));
    }
  }
}


multAllBlock

decompiled_code:
unsigned int __fastcall multAllBlock(unsigned int a1, float a2)
{
unsigned int result; // eax
__int64 v3; // rbx
unsigned int v4; // ebp
int v5; // eax
unsigned int v6; // eax

result = printf("MultAllBlock %d %f\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
printf("Multiplying block %d in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
result = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
v4 = result;
if ( result != -1 )
{
v5 = getValueAsInt((unsigned int)v3, result);
v6 = max(0LL, (unsigned int)(int)(float)((float)v5 * a2));
result = setValueAsInt((unsigned int)v3, v4, v6);
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func11(unsigned int a1, float a2)
{
unsigned int result; // eax
__int64 v3; // rbx
unsigned int v4; // ebp
int v5; // eax
unsigned int v6; // eax

result = printf("MultAllBlock %d %f\n", a1, a2);
if ( nEntFiles > 0 )
{
v3 = 0LL;
do
{
printf("Multiplying block %d in %s\n", a1, *(const char **)(entFileNames + 8 * v3));
result = findBlockIndex((unsigned int)v3, a1, 0xFFFFFFFFLL);
v4 = result;
if ( result != -1 )
{
v5 = getValueAsInt((unsigned int)v3, result);
v6 = max(0LL, (unsigned int)(int)(float)((float)v5 * a2));
result = setValueAsInt((unsigned int)v3, v4, v6);
}
++v3;
}
while ( nEntFiles > (int)v3 );
}
return result;
}

original_code:
void multAllBlock(int block, float factor) {
  printf("MultAllBlock %d %f\n",block,factor);
  for(int i=0;i<nEntFiles;i++) {
    printf("Multiplying block %d in %s\n",block,entFileNames[i]);
    int index=findBlockIndex(i,block,-1);
    if(index != -1) setValueAsInt(i,index,max(0,getValueAsInt(i,index)*factor));
    else {
    }
  }
}

canonical_original_code:
void multAllBlock(int block, float factor) {
  printf("MultAllBlock %d %f\n",block,factor);
  for(int i = 0;i<nEntFiles;i++) {
    printf("Multiplying block %d in %s\n",block,entFileNames[i]);
    int index = findBlockIndex(i,block,-1);
    if(index != -1) setValueAsInt(i,index,max(0,getValueAsInt(i,index)*factor));
    else {
    }
  }
}


printFrom

decompiled_code:
void __fastcall printFrom(__int64 a1)
{
__int64 v1; // rbx
char *v2; // [rsp+0h] [rbp-186C0h] BYREF
char v3[100024]; // [rsp+8h] [rbp-186B8h] BYREF

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
puts(*(const char **)(entFileNames + 8 * v1));
if ( (int)readTag((unsigned int)v1, a1, v3, 100000LL) > 0 )
{
v2 = v3;
printStruct(&v2);
}
++v1;
}
while ( nEntFiles > (int)v1 );
}
}

canonical_decompiled_code:
void __fastcall func13(__int64 a1)
{
__int64 v1; // rbx
char *v2; // [rsp+0h] [rbp-186C0h] BYREF
char v3[100024]; // [rsp+8h] [rbp-186B8h] BYREF

if ( nEntFiles > 0 )
{
v1 = 0LL;
do
{
puts(*(const char **)(entFileNames + 8 * v1));
if ( (int)readTag((unsigned int)v1, a1, v3, 100000LL) > 0 )
{
v2 = v3;
printStruct(&v2);
}
++v1;
}
while ( nEntFiles > (int)v1 );
}
}

original_code:
void printFrom(char *tag) {
  uint8_t buf[100000];
  for(int i=0;i<nEntFiles;i++) {
    printf("%s\n",entFileNames[i]);
    int len=readTag(i,tag,buf,sizeof(buf));
    if(len>0) {
      uint8_t *pnt=buf;
      printStruct(&pnt);
    }
  }
}

canonical_original_code:
void printFrom(char *tag) {
  unsigned char buf[100000];
  for(int i = 0;i<nEntFiles;i++) {
    printf("%s\n",entFileNames[i]);
    int len = readTag(i,tag,buf,sizeof(buf));
    if(len>0) {
      unsigned char *pnt = buf;
      printStruct(&pnt);
    }
  }
}

variable_types:
tag
char *

variable_types:
buf
unsigned char [100000]

variable_types: len int

variable_types:
pnt
unsigned char *


min

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

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

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

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

original_code: int min(int a,int b) { return a<b?a:b;}

canonical_original_code: int min(int a, int b) { return a<b?a:b;}

variable_types: a int

variable_types: b int


max

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

original_code: int max(int a,int b) { return a>b?a:b;}

canonical_original_code: int max(int a, int b) { return a>b?a:b;}


decompiled_code:
__int64 __fastcall bufToUInt16(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

original_code:
uint16_t bufToUInt16(uint8_t *buf) {
  return (buf[0]<<8) | (buf[1]<<0);
}

canonical_original_code:
unsigned short bufToUInt16(unsigned char *buf) {
  return (buf[0]<<8) | (buf[1]<<0);
}

variable_types:
buf
unsigned char *

return_type: unsigned short


decompiled_code:
__int64 __fastcall bufToUInt32(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_original_code:
unsigned int bufToUInt32(unsigned char *buf) {
  return (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | (buf[3]<<0);
}

return_type: unsigned int


decompiled_code:
unsigned __int64 __fastcall bufToUInt64(unsigned __int64 *a1)
{
return _byteswap_uint64(*a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(unsigned __int64 *a1)
{
return _byteswap_uint64(*a1);
}

canonical_original_code:
unsigned long bufToUInt64(unsigned char *buf) {
  return (((unsigned long)buf[0])<<56) | (((unsigned long)buf[1])<<48) | (((unsigned long)buf[2])<<40) | (((unsigned long)buf[3])<<32) | (((unsigned long)buf[4])<<24) | (((unsigned long)buf[5])<<16) | (((unsigned long)buf[6])<<8) | (((unsigned long)buf[7])<<0);
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall bufToInt16(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 *a1)
{
return (*a1 << 8) | (unsigned int)a1[1];
}

original_code:
int16_t bufToInt16(uint8_t *buf) {
  return (buf[0]<<8) | (buf[1]<<0);
}

canonical_original_code:
short bufToInt16(unsigned char *buf) {
  return (buf[0]<<8) | (buf[1]<<0);
}

return_type: short


decompiled_code:
__int64 __fastcall bufToInt32(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}


decompiled_code:
unsigned __int64 __fastcall bufToInt64(unsigned __int64 *a1)
{
return _byteswap_uint64(*a1);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(unsigned __int64 *a1)
{
return _byteswap_uint64(*a1);
}

return_type: long


bufToFloat

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

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

original_code:
float bufToFloat(uint8_t *buf) {
  return *((float*) buf);
}

canonical_original_code:
float bufToFloat(unsigned char *buf) {
  return *((float *) buf);
}

return_type: float


hexDump

decompiled_code:
unsigned int __fastcall hexDump(__int64 a1, int a2)
{
int v2; // er14
unsigned int result; // eax
int i; // er13
__int64 v5; // rbp
int v6; // ebx
__int64 j; // rbp
int v8; // er12
int v9; // [rsp+14h] [rbp-44h]
int v10; // [rsp+18h] [rbp-40h]

v2 = a2 / 16;
result = (unsigned int)(a2 >> 31) >> 28;
v9 = a2 % 16;
v10 = 0;
for ( i = 0; i < v2 || i == v2 && (a2 & 0xF) != 0; ++i )
{
v5 = 0LL;
while ( 1 )
{
v6 = v5;
if ( i >= v2 && v9 <= (int)v5 )
break;
printf("%02x", *(unsigned __int8 *)(a1 + v10 + v5));
if ( (int)v5 % 4 == 3 )
putchar(32);
if ( ++v5 == 16 )
goto LABEL_8;
}
if ( (int)v5 <= 15 )
{
do
{
printf(" ");
if ( v6 % 4 == 3 )
putchar(32);
++v6;
}
while ( v6 != 16 );
}
LABEL_8:
printf(" ");
for ( j = 0LL; j != 16; ++j )
{
if ( i >= v2 && v9 <= (int)j )
break;
v8 = *(unsigned __int8 *)(a1 + v10 + j);
if ( isprint(v8) )
putchar(v8);
else
putchar(64);
if ( (int)j % 4 == 3 )
putchar(32);
}
result = putchar(10);
v10 += 16;
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func14(__int64 a1, int a2)
{
int v2; // er14
unsigned int result; // eax
int i; // er13
__int64 v5; // rbp
int v6; // ebx
__int64 j; // rbp
int v8; // er12
int v9; // [rsp+14h] [rbp-44h]
int v10; // [rsp+18h] [rbp-40h]

v2 = a2 / 16;
result = (unsigned int)(a2 >> 31) >> 28;
v9 = a2 % 16;
v10 = 0;
for ( i = 0; i < v2 || i == v2 && (a2 & 0xF) != 0; ++i )
{
v5 = 0LL;
while ( 1 )
{
v6 = v5;
if ( i >= v2 && v9 <= (int)v5 )
break;
printf("%02x", *(unsigned __int8 *)(a1 + v10 + v5));
if ( (int)v5 % 4 == 3 )
putchar(32);
if ( ++v5 == 16 )
goto LABEL_8;
}
if ( (int)v5 <= 15 )
{
do
{
printf(" ");
if ( v6 % 4 == 3 )
putchar(32);
++v6;
}
while ( v6 != 16 );
}
LABEL_8:
printf(" ");
for ( j = 0LL; j != 16; ++j )
{
if ( i >= v2 && v9 <= (int)j )
break;
v8 = *(unsigned __int8 *)(a1 + v10 + j);
if ( isprint(v8) )
putchar(v8);
else
putchar(64);
if ( (int)j % 4 == 3 )
putchar(32);
}
result = putchar(10);
v10 += 16;
}
return result;
}

original_code:
void hexDump(void *data,int datalen) {
  int i, row, nRows=datalen/16;
  for(row=0;row<nRows||(row==nRows && datalen%16 != 0);row++) {
    for(i=0;i<16 && (row < nRows || i < (datalen%16));i++) {
      printf("%02x",((unsigned char*)data)[i+row*16]);
      if(i % 4 == 3) printf(" ");
    }
    for(;i<16;i++) {
      printf("  ");
      if(i % 4 == 3) printf(" ");
    }
    printf("    ");
    for(i=0;i<16 && (row < nRows || i < (datalen%16));i++) {
      unsigned char c = ((unsigned char*)data)[i+row*16];
      if(isprint(c))
 printf("%c",((unsigned char*)data)[i+row*16]);
      else
 printf("@");
      if(i % 4 == 3) printf(" ");
    }
    printf("\n");
  }
}

canonical_original_code:
void hexDump(void *data, int datalen) {
  int i; int row; int nRows = datalen/16;
  for(row=0;row<nRows||(row==nRows && datalen%16 != 0);row++) {
    for(i=0;i<16 && (row < nRows || i < (datalen%16));i++) {
      printf("%02x",((unsigned char *)data)[i+row*16]);
      if(i % 4 == 3) printf(" ");
    }
    for(;i<16;i++) {
      printf("  ");
      if(i % 4 == 3) printf(" ");
    }
    printf("    ");
    for(i=0;i<16 && (row < nRows || i < (datalen%16));i++) {
      unsigned char c = ((unsigned char*)data)[i+row*16];
      if(isprint(c))
 printf("%c",((unsigned char *)data)[i+row*16]);
      else
 printf("@");
      if(i % 4 == 3) printf(" ");
    }
    printf("\n");
  }
}

variable_types:
data
void *

variable_types: datalen int

variable_types: i int

variable_types: row int

variable_types: nRows int

variable_types:
c
unsigned char


min

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

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


max

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}


decompiled_code:
_BOOL8 __fastcall list_empty(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_QWORD *a1)
{
return *a1 == (_QWORD)a1;
}

original_code:
static inline int list_empty(const struct list_head *head)
{
 return head->next == head;
}

canonical_original_code:
static inline int list_empty(struct list_head *head)
{
 return head->next == head;
}

variable_types: struct list_head { struct list_head *next; } *


original_code:
static void assemble_write_mmio(struct ivals_context *ctx,
    unsigned int offset,
    unsigned int size,
    unsigned int value)
{
 struct initval *iv;
 iv = xmalloc(sizeof(struct initval));
 iv->offset = offset;
 iv->size = size;
 iv->value = value;
 do { (&iv->list)->next = (&iv->list); (&iv->list)->prev = (&iv->list); } while (0);
 list_add_tail(&iv->list, &ctx->ivals);
 ctx->ivals_count++;
}

variable_types: struct ivals_context { struct initvals_sect { char *name; struct list_head { struct list_head *next; } ops; struct list_head list; } *sect; struct list_head ivals; unsigned int ivals_count; } *

variable_types:
offset
unsigned int

variable_types:
size
unsigned int

variable_types:
value
unsigned int

variable_types: struct initval { unsigned int offset; unsigned int size; unsigned int value; struct list_head { struct list_head *next; } list; } *

user_defined_types: struct initval { unsigned int offset; unsigned int size; unsigned int value; struct list_head list; }

user_defined_types: struct ivals_context { struct initvals_sect *sect; struct list_head ivals; unsigned int ivals_count; }

user_defined_types: struct initvals_sect { char *name; struct list_head ops; struct list_head list; }


decompiled_code:
void __fastcall assemble_write_phy(__int64 a1, int a2, int a3)
{
assemble_write_mmio(a1, 1020, 2, a2);
assemble_write_mmio(a1, 1022, 2, a3);
}

original_code:
static void assemble_write_phy(struct ivals_context *ctx,
          unsigned int offset,
          unsigned int value)
{
 assemble_write_mmio(ctx, 0x3FC, 2, offset);
 assemble_write_mmio(ctx, 0x3FE, 2, value);
}

user_defined_types: struct list_head { struct list_head *next; }


decompiled_code:
void __fastcall assemble_write_radio(__int64 a1, int a2, int a3)
{
assemble_write_mmio(a1, 1014, 2, a2);
assemble_write_mmio(a1, 1018, 2, a3);
}

original_code:
static void assemble_write_radio(struct ivals_context *ctx,
     unsigned int offset,
     unsigned int value)
{
 assemble_write_mmio(ctx, 0x3F6, 2, offset);
 assemble_write_mmio(ctx, 0x3FA, 2, value);
}


decompiled_code:
void __fastcall shm_control_word(__int64 a1, int a2, unsigned __int16 a3)
{
assemble_write_mmio(a1, 352, 4, (a2 << 16) | a3);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2, unsigned __int16 a3)
{
func3(a1, 352, 4, (a2 << 16) | a3);
}

original_code:
static void shm_control_word(struct ivals_context *ctx,
        unsigned int routing,
        unsigned int offset)
{
 unsigned int control;
 control = (routing & 0xFFFF);
 control <<= 16;
 control |= (offset & 0xFFFF);
 assemble_write_mmio(ctx, 0x160, 4, control);
}

variable_types:
routing
unsigned int

variable_types:
control
unsigned int


decompiled_code:
void __fastcall shm_write16(__int64 a1, char a2, unsigned int a3, int a4)
{
if ( a2 != 1 )
goto LABEL_4;
if ( (a3 & 3) == 0 )
{
a3 >>= 2;
LABEL_4:
shm_control_word(a1, a2, a3);
assemble_write_mmio(a1, 356, 2, a4);
return;
}
shm_control_word(a1, a2, a3 >> 2);
assemble_write_mmio(a1, 358, 2, a4);
}

original_code:
static void shm_write16(struct ivals_context *ctx,
   unsigned int routing,
   unsigned int offset,
   unsigned int value)
{
 if ((routing & 0xFF) == 0x01) {
  if (offset & 0x0003) {
   shm_control_word(ctx, routing, offset >> 2);
   assemble_write_mmio(ctx, 0x166, 2,
         value);
   return;
  }
  offset >>= 2;
 }
 shm_control_word(ctx, routing, offset);
 assemble_write_mmio(ctx, 0x164, 2, value);
}


decompiled_code:
void __fastcall shm_write32(__int64 a1, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // ebx

if ( a2 != 1 )
goto LABEL_4;
if ( (a3 & 3) == 0 )
{
a3 >>= 2;
LABEL_4:
shm_control_word(a1, a2, a3);
assemble_write_mmio(a1, 356, 4, a4);
return;
}
v5 = a3 >> 2;
shm_control_word(a1, a2, a3 >> 2);
assemble_write_mmio(a1, 358, 2, HIWORD(a4));
shm_control_word(a1, a2, v5 + 1);
assemble_write_mmio(a1, 356, 2, (unsigned __int16)a4);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, char a2, unsigned int a3, unsigned int a4)
{
unsigned int v5; // ebx

if ( a2 != 1 )
goto LABEL_4;
if ( (a3 & 3) == 0 )
{
a3 >>= 2;
LABEL_4:
func6(a1, a2, a3);
func3(a1, 356, 4, a4);
return;
}
v5 = a3 >> 2;
func6(a1, a2, a3 >> 2);
func3(a1, 358, 2, HIWORD(a4));
func6(a1, a2, v5 + 1);
func3(a1, 356, 2, (unsigned __int16)a4);
}

original_code:
static void shm_write32(struct ivals_context *ctx,
   unsigned int routing,
   unsigned int offset,
   unsigned int value)
{
 if ((routing & 0xFF) == 0x01) {
  if (offset & 0x0003) {
   shm_control_word(ctx, routing, offset >> 2);
   assemble_write_mmio(ctx, 0x166, 2,
         (value >> 16) & 0xFFFF);
   shm_control_word(ctx, routing, (offset >> 2) + 1);
   assemble_write_mmio(ctx, 0x164, 2,
         (value & 0xFFFF));
   return;
  }
  offset >>= 2;
 }
 shm_control_word(ctx, routing, offset);
 assemble_write_mmio(ctx, 0x164, 4, value);
}


decompiled_code:
void __fastcall assemble_write_tram(__int64 a1, int a2, int a3)
{
assemble_write_mmio(a1, 304, 4, a2);
assemble_write_mmio(a1, 308, 4, a3);
}

original_code:
static void assemble_write_tram(struct ivals_context *ctx,
    unsigned int offset,
    unsigned int value)
{
 assemble_write_mmio(ctx, 0x130, 4, offset);
 assemble_write_mmio(ctx, 0x134, 4, value);
}


decompiled_code:
void __fastcall assemble_write_shm(__int64 a1, char a2, unsigned int a3, unsigned int a4, int a5)
{
if ( a5 == 2 )
{
shm_write16(a1, a2, a3, a4);
}
else
{
if ( a5 != 4 )
{
fwrite("Internal assembler BUG. SHMwrite invalid size\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
shm_write32(a1, a2, a3, a4);
}
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1, char a2, unsigned int a3, unsigned int a4, int a5)
{
if ( a5 == 2 )
{
func7(a1, a2, a3, a4);
}
else
{
if ( a5 != 4 )
{
fwrite("Internal assembler BUG. SHMwrite invalid size\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
func8(a1, a2, a3, a4);
}
}

original_code:
static void assemble_write_shm(struct ivals_context *ctx,
          unsigned int routing,
          unsigned int offset,
          unsigned int value,
          unsigned int size)
{
 switch (routing & 0xFF) {
 case 0: case 1: case 2: case 3: case 4:
  break;
 default:
  break;
 }
 switch (size) {
 case 2:
  shm_write16(ctx, routing, offset, value);
  break;
 case 4:
  shm_write32(ctx, routing, offset, value);
  break;
 default:
  fprintf(stderr, "Internal assembler BUG. SHMwrite invalid size\n");
  exit(1);
 }
}


decompiled_code:
__int64 __fastcall initval_to_raw(const char ***a1, __int64 a2, unsigned int *a3)
{
unsigned int v4; // eax
__int64 result; // rax

*(_DWORD *)a2 = 0;
*(_WORD *)(a2 + 4) = 0;
if ( (*a3 & 0xFFFF8000) != 0 )
{
fwrite("InitVals ERROR", 1uLL, 0xEuLL, stderr);
fprintf(stderr, " (Section \"%s\")", **a1);
fprintf(stderr, ":\n Initval offset 0x%04X too big. Offset must be <= 0x%04X\n", *a3, 0x7FFFLL);
exit(1);
}
*(_WORD *)a2 = cpu_to_be16((unsigned __int16)*a3);
v4 = a3[1];
if ( v4 == 2 )
{
*(_WORD *)(a2 + 2) = cpu_to_be16(*((unsigned __int16 *)a3 + 4));
result = 4LL;
}
else
{
if ( v4 != 4 )
{
fwrite("InitVals ERROR", 1uLL, 0xEuLL, stderr);
fprintf(stderr, " (Section \"%s\")", **a1);
fwrite(":\n Internal error. initval_to_raw invalid size.\n", 1uLL, 0x31uLL, stderr);
exit(1);
}
*(_DWORD *)(a2 + 2) = cpu_to_be32(a3[2]);
*(_WORD *)a2 |= cpu_to_be16(0x8000LL);
result = 6LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char ***a1, __int64 a2, unsigned int *a3)
{
unsigned int v4; // eax
__int64 result; // rax

*(_DWORD *)a2 = 0;
*(_WORD *)(a2 + 4) = 0;
if ( (*a3 & 0xFFFF8000) != 0 )
{
fwrite("InitVals ERROR", 1uLL, 0xEuLL, stderr);
fprintf(stderr, " (Section \"%s\")", **a1);
fprintf(stderr, ":\n Initval offset 0x%04X too big. Offset must be <= 0x%04X\n", *a3, 0x7FFFLL);
exit(1);
}
*(_WORD *)a2 = cpu_to_be16((unsigned __int16)*a3);
v4 = a3[1];
if ( v4 == 2 )
{
*(_WORD *)(a2 + 2) = cpu_to_be16(*((unsigned __int16 *)a3 + 4));
result = 4LL;
}
else
{
if ( v4 != 4 )
{
fwrite("InitVals ERROR", 1uLL, 0xEuLL, stderr);
fprintf(stderr, " (Section \"%s\")", **a1);
fwrite(":\n Internal error. initval_to_raw invalid size.\n", 1uLL, 0x31uLL, stderr);
exit(1);
}
*(_DWORD *)(a2 + 2) = cpu_to_be32(a3[2]);
*(_WORD *)a2 |= cpu_to_be16(0x8000LL);
result = 6LL;
}
return result;
}

original_code:
static unsigned int initval_to_raw(struct ivals_context *ctx,
       struct initval_raw *raw,
       const struct initval *iv)
{
 unsigned int size;
 memset(raw, 0, sizeof(*raw));
 if (iv->offset & ~0x7FFF) {
  do { do { fprintf(stderr, "InitVals " "ERROR"); fprintf(stderr, " (Section \"%s\")", ctx->sect->name); fprintf(stderr, ":\n  " "Initval offset 0x%04X too big. " "Offset must be <= 0x%04X" "\n" , iv->offset, 0x7FFF); } while (0); exit(1); } while (0);
 }
 raw->offset_size = cpu_to_be16(iv->offset);
 switch (iv->size) {
 case 2:
  raw->data.d16 = cpu_to_be16(iv->value);
  size = sizeof(be16_t) + sizeof(be16_t);
  break;
 case 4:
  raw->data.d32 = cpu_to_be32(iv->value);
  raw->offset_size |= cpu_to_be16(0x8000);
  size = sizeof(be16_t) + sizeof(be32_t);
  break;
 default:
  do { do { fprintf(stderr, "InitVals " "ERROR"); fprintf(stderr, " (Section \"%s\")", ctx->sect->name); fprintf(stderr, ":\n  " "Internal error. initval_to_raw invalid size." "\n"); } while (0); exit(1); } while (0);
  break;
 }
 return size;
}

canonical_original_code:
static unsigned int initval_to_raw(struct ivals_context *ctx, struct initval_raw *raw, struct initval *iv)
{
 unsigned int size;
 memset(raw, 0, sizeof(*raw));
 if (iv->offset & ~0x7FFF) {
  do { do { fprintf(stderr, "InitVals " "ERROR"); fprintf(stderr, " (Section \"%s\")", ctx->sect->name); fprintf(stderr, ":\n  " "Initval offset 0x%04X too big. " "Offset must be <= 0x%04X" "\n" , iv->offset, 0x7FFF); } while (0); exit(1); } while (0);
 }
 raw->offset_size = cpu_to_be16(iv->offset);
 switch (iv->size) {
 case 2:
  raw->data.d16 = cpu_to_be16(iv->value);
  size = sizeof(be16_t) + sizeof(be16_t);
  break;
 case 4:
  raw->data.d32 = cpu_to_be32(iv->value);
  raw->offset_size |= cpu_to_be16(0x8000);
  size = sizeof(be16_t) + sizeof(be32_t);
  break;
 default:
  do { do { fprintf(stderr, "InitVals " "ERROR"); fprintf(stderr, " (Section \"%s\")", ctx->sect->name); fprintf(stderr, ":\n  " "Internal error. initval_to_raw invalid size." "\n"); } while (0); exit(1); } while (0);
  break;
 }
 return size;
}

variable_types: struct initval_raw { unsigned short offset_size; union { unsigned short d16; unsigned int d32; } data; } *

variable_types: struct initval { unsigned int offset; unsigned int size; unsigned int value; struct list_head list; } *


dump

decompiled_code:
int __fastcall dump(__int64 a1, __int64 a2, const char *a3)
{
__int64 i; // rbx
unsigned __int8 v4; // dl

fprintf(stderr, "Data dump (%s, %zd bytes):", a3, a2);
if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
v4 = *(_BYTE *)(a1 + i);
if ( (i & 7) != 0 )
fprintf(stderr, "0x%02x, ", v4);
else
fprintf(stderr, "\n0x%08zx: 0x%02x, ", i, v4);
}
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, __int64 a2, const char *a3)
{
__int64 i; // rbx
unsigned __int8 v4; // dl

fprintf(stderr, "Data dump (%s, %zd bytes):", a3, a2);
if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
v4 = *(_BYTE *)(a1 + i);
if ( (i & 7) != 0 )
fprintf(stderr, "0x%02x, ", v4);
else
fprintf(stderr, "\n0x%08zx: 0x%02x, ", i, v4);
}
}
return fputc(10, stderr);
}

original_code:
void dump(const char *data,
   size_t size,
   const char *description)
{
 size_t i;
 char c;
 fprintf(stderr, "Data dump (%s, %zd bytes):",
         description, size);
 for (i = 0; i < size; i++) {
  c = data[i];
  if (i % 8 == 0) {
   fprintf(stderr, "\n0x%08zx:  0x%02x, ",
    i, c & 0xff);
  } else
   fprintf(stderr, "0x%02x, ", c & 0xff);
 }
 fprintf(stderr, "\n");
}

canonical_original_code:
void dump(char *data, unsigned long size, char *description)
{
 unsigned long i;
 char c;
 fprintf(stderr, "Data dump (%s, %zd bytes):",
         description, size);
 for (i = 0; i < size; i++) {
  c = data[i];
  if (i % 8 == 0) {
   fprintf(stderr, "\n0x%08zx:  0x%02x, ",
    i, c & 0xff);
  } else
   fprintf(stderr, "0x%02x, ", c & 0xff);
 }
 fprintf(stderr, "\n");
}

variable_types:
data
char *

variable_types:
size
unsigned long

variable_types:
description
char *

variable_types:
i
unsigned long

variable_types: c char


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t n)
{
void *v1; // rax

v1 = malloc(n);
if ( !v1 )
{
fwrite("Out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func1(size_t n)
{
void *v1; // rax

v1 = malloc(n);
if ( !v1 )
{
fwrite("Out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
return memset(v1, 0, n);
}

original_code:
void * xmalloc(size_t size)
{
 void *p;
 p = malloc(size);
 if (!p) {
  fprintf(stderr, "Out of memory\n");
  exit(1);
 }
 memset(p, 0, size);
 return p;
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
 void *p;
 p = malloc(size);
 if (!p) {
  fprintf(stderr, "Out of memory\n");
  exit(1);
 }
 memset(p, 0, size);
 return p;
}

variable_types:
p
void *