original_code:
void compile_gd3(void)
{
   uint16_t *u_title = utf8_to_utf16(title);
   uint16_t *u_game = utf8_to_utf16(game);
   uint16_t *u_system = utf8_to_utf16("Sega Mega Drive / Genesis");
   uint16_t *u_composer = utf8_to_utf16(composer);
   uint16_t *u_release = utf8_to_utf16(release);
   uint16_t *u_rippedby = utf8_to_utf16(rippedby);
   uint16_t *u_notes = utf8_to_utf16("");
   size_t size_title = get_utf16_size(u_title);
   size_t size_game = get_utf16_size(u_game);
   size_t size_system = get_utf16_size(u_system);
   size_t size_composer = get_utf16_size(u_composer);
   size_t size_release = get_utf16_size(u_release);
   size_t size_rippedby = get_utf16_size(u_rippedby);
   size_t size_notes = get_utf16_size(u_notes);
   size_t text_size = 2*size_title +
                      2*size_game +
                      2*size_system +
                      2*size_composer +
                      size_release +
                      size_rippedby +
                      size_notes;
   size_t gd3_size = 12 + text_size;
   gd3 = alloc_blob(gd3_size);
   uint8_t *ptr = gd3->data;
   *ptr++ = 'G'; *ptr++ = 'd'; *ptr++ = '3'; *ptr++ = ' ';
   *ptr++ = 0x00; *ptr++ = 0x01; *ptr++ = 0x00; *ptr++ = 0x00;
   *ptr++ = (uint8_t)(gd3_size);
   *ptr++ = (uint8_t)(gd3_size >> 8);
   *ptr++ = (uint8_t)(gd3_size >> 16);
   *ptr++ = (uint8_t)(gd3_size >> 24);
   memcpy(ptr, u_title, size_title); ptr += size_title;
   memcpy(ptr, u_title, size_title); ptr += size_title;
   memcpy(ptr, u_game, size_game); ptr += size_game;
   memcpy(ptr, u_game, size_game); ptr += size_game;
   memcpy(ptr, u_system, size_system); ptr += size_system;
   memcpy(ptr, u_system, size_system); ptr += size_system;
   memcpy(ptr, u_composer, size_composer); ptr += size_composer;
   memcpy(ptr, u_composer, size_composer); ptr += size_composer;
   memcpy(ptr, u_release, size_release); ptr += size_release;
   memcpy(ptr, u_rippedby, size_rippedby); ptr += size_rippedby;
   memcpy(ptr, u_notes, size_notes); ptr += size_notes;
   free(u_title);
   free(u_game);
   free(u_system);
   free(u_composer);
   free(u_release);
   free(u_rippedby);
   free(u_notes);
}

canonical_original_code:
void compile_gd3()
{
   unsigned short *u_title = utf8_to_utf16(title);
   unsigned short *u_game = utf8_to_utf16(game);
   unsigned short *u_system = utf8_to_utf16("Sega Mega Drive / Genesis");
   unsigned short *u_composer = utf8_to_utf16(composer);
   unsigned short *u_release = utf8_to_utf16(release);
   unsigned short *u_rippedby = utf8_to_utf16(rippedby);
   unsigned short *u_notes = utf8_to_utf16("");
   unsigned long size_title = get_utf16_size(u_title);
   unsigned long size_game = get_utf16_size(u_game);
   unsigned long size_system = get_utf16_size(u_system);
   unsigned long size_composer = get_utf16_size(u_composer);
   unsigned long size_release = get_utf16_size(u_release);
   unsigned long size_rippedby = get_utf16_size(u_rippedby);
   unsigned long size_notes = get_utf16_size(u_notes);
   unsigned long text_size = 2*size_title +
                      2*size_game +
                      2*size_system +
                      2*size_composer +
                      size_release +
                      size_rippedby +
                      size_notes;
   unsigned long gd3_size = 12 + text_size;
   gd3 = alloc_blob(gd3_size);
   unsigned char *ptr = gd3->data;
   *ptr++ = 'G'; *ptr++ = 'd'; *ptr++ = '3'; *ptr++ = ' ';
   *ptr++ = 0x00; *ptr++ = 0x01; *ptr++ = 0x00; *ptr++ = 0x00;
   *ptr++ = (unsigned char)(gd3_size);
   *ptr++ = (unsigned char)(gd3_size >> 8);
   *ptr++ = (unsigned char)(gd3_size >> 16);
   *ptr++ = (unsigned char)(gd3_size >> 24);
   memcpy(ptr, u_title, size_title); ptr += size_title;
   memcpy(ptr, u_title, size_title); ptr += size_title;
   memcpy(ptr, u_game, size_game); ptr += size_game;
   memcpy(ptr, u_game, size_game); ptr += size_game;
   memcpy(ptr, u_system, size_system); ptr += size_system;
   memcpy(ptr, u_system, size_system); ptr += size_system;
   memcpy(ptr, u_composer, size_composer); ptr += size_composer;
   memcpy(ptr, u_composer, size_composer); ptr += size_composer;
   memcpy(ptr, u_release, size_release); ptr += size_release;
   memcpy(ptr, u_rippedby, size_rippedby); ptr += size_rippedby;
   memcpy(ptr, u_notes, size_notes); ptr += size_notes;
   free(u_title);
   free(u_game);
   free(u_system);
   free(u_composer);
   free(u_release);
   free(u_rippedby);
   free(u_notes);
}

variable_types:
ptr
unsigned char *


repo: sikthehedgehog/mdtools

decompiled_code:
__int64 get_gd3_blob()
{
return gd3;
}

canonical_decompiled_code:
__int64 func6()
{
return gd3;
}

original_code:
const Blob *get_gd3_blob(void)
{
   return gd3;
}

canonical_original_code:
struct Blob *get_gd3_blob()
{
   return gd3;
}

return_type: struct Blob { unsigned long size; unsigned char data[]; } *


putblock

original_code:
static int putblock(uint8_t **dictionary, size_t *dicsize,
const uint8_t *block, uint16_t *id) {
   uint8_t *ptr = *dictionary;
   if (*dictionary != ((void *)0)) {
      size_t limit = *dicsize;
      while (limit >= 8) {
         if (!memcmp(ptr, block, 8)) {
            *id = (uint16_t)(ptr - *dictionary);
            return ERR_NONE;
         }
         ptr += 2;
         limit -= 2;
      };
   }
   *dicsize += 8;
   ptr = (uint8_t *) realloc(*dictionary, *dicsize);
   if (ptr == ((void *)0)) return ERR_NOMEMORY;
   *dictionary = ptr;
   *id = *dicsize - 8;
   memcpy(*dictionary + *id, block, 8);
   return ERR_NONE;
}

canonical_original_code:
static int putblock(unsigned char **dictionary, unsigned long *dicsize, unsigned char *block, unsigned short *id) {
   unsigned char *ptr = *dictionary;
   if (*dictionary != ((void *)0)) {
      unsigned long limit = *dicsize;
      while (limit >= 8) {
         if (!memcmp(ptr, block, 8)) {
            *id = (unsigned short)(ptr - *dictionary);
            return ERR_NONE;
         }
         ptr += 2;
         limit -= 2;
      };
   }
   *dicsize += 8;
   ptr = (unsigned char *) realloc(*dictionary, *dicsize);
   if (ptr == ((void *)0)) return ERR_NOMEMORY;
   *dictionary = ptr;
   *id = *dicsize - 8;
   memcpy(*dictionary + *id, block, 8);
   return ERR_NONE;
}

variable_types:
dictionary
unsigned char **

variable_types:
dicsize
unsigned long *

variable_types:
block
unsigned char *

variable_types:
id
unsigned short *

variable_types:
limit
unsigned long

return_type: int


decompiled_code:
char *__fastcall get_stream_command(unsigned int a1)
{
return (char *)commands + 12 * a1;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned int a1)
{
return (char *)commands + 12 * a1;
}

original_code:
const StreamCmd *get_stream_command(unsigned id)
{
   return &commands[id];
}

canonical_original_code:
struct StreamCmd *get_stream_command(unsigned int id)
{
   return &commands[id];
}

variable_types:
id
unsigned int

return_type: struct StreamCmd { enum StreamCmdType { STREAM_DUMMY=0, STREAM_DELAY=1, STREAM_YMREG0=2, STREAM_YMREG1=3, STREAM_PSGREG=4, STREAM_INITPCM=5, STREAM_STARTPCM=6, STREAM_STOPPCM=7, STREAM_SETPCMFREQ=8, STREAM_END=9 } type; unsigned int value1; unsigned int value2; } *

user_defined_types: struct StreamCmd { enum StreamCmdType type; unsigned int value1; unsigned int value2; }


decompiled_code:
__int64 get_num_stream_commands()
{
return (unsigned int)num_commands;
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)num_commands;
}

original_code:
unsigned get_num_stream_commands(void)
{
   return num_commands;
}

canonical_original_code:
unsigned int get_num_stream_commands()
{
   return num_commands;
}

return_type: unsigned int


decompiled_code:
__int64 get_num_stream_bytes()
{
return (unsigned int)stream_size;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)stream_size;
}

original_code:
unsigned get_num_stream_bytes(void)
{
   return stream_size;
}

canonical_original_code:
unsigned int get_num_stream_bytes()
{
   return stream_size;
}


decompiled_code:
__int64 get_num_stream_samples()
{
return (unsigned int)stream_samples;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned int)stream_samples;
}

original_code:
unsigned get_num_stream_samples(void)
{
   return stream_samples;
}

canonical_original_code:
unsigned int get_num_stream_samples()
{
   return stream_samples;
}


decompiled_code:
char *new_command()
{
unsigned int v0; // ebx
char *v1; // rax
char *result; // rax

v0 = num_commands++;
v1 = (char *)realloc(commands, 12LL * (unsigned int)num_commands);
if ( !v1 )
abort();
commands = v1;
result = &v1[12 * v0];
*(_DWORD *)result = 0;
*((_DWORD *)result + 1) = 0;
*((_DWORD *)result + 2) = 0;
return result;
}

canonical_decompiled_code:
char *func4()
{
unsigned int v0; // ebx
char *v1; // rax
char *result; // rax

v0 = num_commands++;
v1 = (char *)realloc(commands, 12LL * (unsigned int)num_commands);
if ( !v1 )
abort();
commands = v1;
result = &v1[12 * v0];
*(_DWORD *)result = 0;
*((_DWORD *)result + 1) = 0;
*((_DWORD *)result + 2) = 0;
return result;
}

original_code:
StreamCmd *new_command(void)
{
   num_commands++;
   StreamCmd *temp = realloc(commands, sizeof(StreamCmd) * num_commands);
   if (temp == ((void *)0)) abort();
   commands = temp;
   StreamCmd *ptr = &commands[num_commands-1];
   ptr->type = 0;
   ptr->value1 = 0;
   ptr->value2 = 0;
   return ptr;
}

canonical_original_code:
struct StreamCmd *new_command()
{
   num_commands++;
   struct StreamCmd *temp = realloc(commands, sizeof(StreamCmd) * num_commands);
   if (temp == ((void *)0)) abort();
   commands = temp;
   struct StreamCmd *ptr = &commands[num_commands-1];
   ptr->type = 0;
   ptr->value1 = 0;
   ptr->value2 = 0;
   return ptr;
}

variable_types: struct StreamCmd { enum StreamCmdType { STREAM_DUMMY=0, STREAM_DELAY=1, STREAM_YMREG0=2, STREAM_YMREG1=3, STREAM_PSGREG=4, STREAM_INITPCM=5, STREAM_STARTPCM=6, STREAM_STOPPCM=7, STREAM_SETPCMFREQ=8, STREAM_END=9 } type; unsigned int value1; unsigned int value2; } *


decompiled_code:
void __fastcall add_delay(unsigned int a1)
{
unsigned int i; // ebx
char *v2; // rax

for ( i = a1; i > 0xFFFF; i -= 0xFFFF )
add_delay(0xFFFFLL);
if ( i )
{
v2 = new_command();
*(_DWORD *)v2 = 1;
*((_DWORD *)v2 + 1) = i;
stream_size += 3;
stream_samples += i;
}
}

canonical_decompiled_code:
void __fastcall func5(unsigned int a1)
{
unsigned int i; // ebx
char *v2; // rax

for ( i = a1; i > 0xFFFF; i -= 0xFFFF )
func5(0xFFFFLL);
if ( i )
{
v2 = func4();
*(_DWORD *)v2 = 1;
*((_DWORD *)v2 + 1) = i;
stream_size += 3;
stream_samples += i;
}
}

original_code:
void add_delay(unsigned samples)
{
   while (samples > 65535) {
      add_delay(65535);
      samples -= 65535;
   }
   if (samples == 0) {
      return;
   }
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_DELAY;
   cmd->value1 = samples;
   stream_size += 3;
   stream_samples += samples;
}

canonical_original_code:
void add_delay(unsigned int samples)
{
   while (samples > 65535) {
      add_delay(65535);
      samples -= 65535;
   }
   if (samples == 0) {
      return;
   }
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_DELAY;
   cmd->value1 = samples;
   stream_size += 3;
   stream_samples += samples;
}

variable_types:
samples
unsigned int

return_type: void

user_defined_types: enum StreamCmdType { STREAM_DUMMY=0, STREAM_DELAY=1, STREAM_YMREG0=2, STREAM_YMREG1=3, STREAM_PSGREG=4, STREAM_INITPCM=5, STREAM_STARTPCM=6, STREAM_STOPPCM=7, STREAM_SETPCMFREQ=8, STREAM_END=9 }


decompiled_code:
char *__fastcall add_ym_write(int a1, int a2, int a3)
{
char *result; // rax

result = new_command();
*(_DWORD *)result = 2 - ((a1 == 0) - 1);
*((_DWORD *)result + 1) = a2;
*((_DWORD *)result + 2) = a3;
stream_size += 3;
return result;
}

canonical_decompiled_code:
char *__fastcall func6(int a1, int a2, int a3)
{
char *result; // rax

result = func4();
*(_DWORD *)result = 2 - ((a1 == 0) - 1);
*((_DWORD *)result + 1) = a2;
*((_DWORD *)result + 2) = a3;
stream_size += 3;
return result;
}

original_code:
void add_ym_write(unsigned bank, unsigned reg, unsigned value)
{
   StreamCmd *cmd = new_command();
   cmd->type = bank ? STREAM_YMREG1 : STREAM_YMREG0;
   cmd->value1 = reg;
   cmd->value2 = value;
   stream_size += 3;
}

canonical_original_code:
void add_ym_write(unsigned int bank, unsigned int reg, unsigned int value)
{
   struct StreamCmd *cmd = new_command();
   cmd->type = bank ? STREAM_YMREG1 : STREAM_YMREG0;
   cmd->value1 = reg;
   cmd->value2 = value;
   stream_size += 3;
}

variable_types:
bank
unsigned int

variable_types:
reg
unsigned int

variable_types:
value
unsigned int


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

result = new_command();
*(_DWORD *)result = 4;
*((_DWORD *)result + 1) = a1;
stream_size += 2;
return result;
}

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

result = func4();
*(_DWORD *)result = 4;
*((_DWORD *)result + 1) = a1;
stream_size += 2;
return result;
}

original_code:
void add_psg_write(unsigned value)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_PSGREG;
   cmd->value1 = value;
   stream_size += 2;
}

canonical_original_code:
void add_psg_write(unsigned int value)
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_PSGREG;
   cmd->value1 = value;
   stream_size += 2;
}


decompiled_code:
char *setup_ym2612_pcm()
{
char *result; // rax

result = new_command();
*(_DWORD *)result = 5;
stream_size += 10;
return result;
}

canonical_decompiled_code:
char *func8()
{
char *result; // rax

result = func4();
*(_DWORD *)result = 5;
stream_size += 10;
return result;
}

original_code:
void setup_ym2612_pcm(void)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_INITPCM;
   stream_size += 10;
}

canonical_original_code:
void setup_ym2612_pcm()
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_INITPCM;
   stream_size += 10;
}


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

result = new_command();
*(_DWORD *)result = 6;
*((_DWORD *)result + 1) = a1;
stream_size += 5;
return result;
}

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

result = func4();
*(_DWORD *)result = 6;
*((_DWORD *)result + 1) = a1;
stream_size += 5;
return result;
}

original_code:
void start_pcm_output(unsigned id)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_STARTPCM;
   cmd->value1 = id;
   stream_size += 5;
}

canonical_original_code:
void start_pcm_output(unsigned int id)
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_STARTPCM;
   cmd->value1 = id;
   stream_size += 5;
}


decompiled_code:
char *stop_pcm_output()
{
char *result; // rax

result = new_command();
*(_DWORD *)result = 7;
stream_size += 2;
return result;
}

canonical_decompiled_code:
char *func10()
{
char *result; // rax

result = func4();
*(_DWORD *)result = 7;
stream_size += 2;
return result;
}

original_code:
void stop_pcm_output(void)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_STOPPCM;
   stream_size += 2;
}

canonical_original_code:
void stop_pcm_output()
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_STOPPCM;
   stream_size += 2;
}


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

result = new_command();
*(_DWORD *)result = 8;
*((_DWORD *)result + 1) = a1;
stream_size += 6;
return result;
}

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

result = func4();
*(_DWORD *)result = 8;
*((_DWORD *)result + 1) = a1;
stream_size += 6;
return result;
}

original_code:
void set_pcm_freq(unsigned hz)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_SETPCMFREQ;
   cmd->value1 = hz;
   stream_size += 6;
}

canonical_original_code:
void set_pcm_freq(unsigned int hz)
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_SETPCMFREQ;
   cmd->value1 = hz;
   stream_size += 6;
}

variable_types:
hz
unsigned int


decompiled_code:
char *end_of_stream()
{
char *result; // rax

result = new_command();
*(_DWORD *)result = 9;
++stream_size;
return result;
}

canonical_decompiled_code:
char *func12()
{
char *result; // rax

result = func4();
*(_DWORD *)result = 9;
++stream_size;
return result;
}

original_code:
void end_of_stream(void)
{
   StreamCmd *cmd = new_command();
   cmd->type = STREAM_END;
   stream_size++;
}

canonical_original_code:
void end_of_stream()
{
   struct StreamCmd *cmd = new_command();
   cmd->type = STREAM_END;
   stream_size++;
}


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

has_loop = 1;
loop_offset = stream_size;
result = (unsigned int)stream_samples;
loop_samples = stream_samples;
return result;
}

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

has_loop = 1;
loop_offset = stream_size;
result = (unsigned int)stream_samples;
loop_samples = stream_samples;
return result;
}

original_code:
void set_loop_point(void)
{
   has_loop = 1;
   loop_offset = stream_size;
   loop_samples = stream_samples;
}

canonical_original_code:
void set_loop_point()
{
   has_loop = 1;
   loop_offset = stream_size;
   loop_samples = stream_samples;
}


decompiled_code:
__int64 does_stream_loop()
{
return (unsigned int)has_loop;
}

canonical_decompiled_code:
__int64 func14()
{
return (unsigned int)has_loop;
}

original_code:
unsigned does_stream_loop(void)
{
   return has_loop;
}

canonical_original_code:
unsigned int does_stream_loop()
{
   return has_loop;
}


decompiled_code:
__int64 get_loop_offset()
{
return (unsigned int)loop_offset;
}

canonical_decompiled_code:
__int64 func15()
{
return (unsigned int)loop_offset;
}

original_code:
unsigned get_loop_offset(void)
{
   return loop_offset;
}

canonical_original_code:
unsigned int get_loop_offset()
{
   return loop_offset;
}


decompiled_code:
__int64 get_num_loop_samples()
{
return (unsigned int)(stream_samples - loop_samples);
}

canonical_decompiled_code:
__int64 func16()
{
return (unsigned int)(stream_samples - loop_samples);
}

original_code:
unsigned get_num_loop_samples(void)
{
   return stream_samples - loop_samples;
}

canonical_original_code:
unsigned int get_num_loop_samples()
{
   return stream_samples - loop_samples;
}


decompiled_code:
__int64 __fastcall sort_func(const void *a1, const void *a2)
{
__int64 result; // rax
unsigned int v3; // ecx
unsigned int v4; // edx

result = 0xFFFFFFFFLL;
if ( *(_QWORD *)a1 >= *(_QWORD *)a2 )
{
result = 1LL;
if ( *(_QWORD *)a1 <= *(_QWORD *)a2 )
{
v3 = *((_DWORD *)a1 + 2);
v4 = *((_DWORD *)a2 + 2);
result = 0xFFFFFFFFLL;
if ( v3 >= v4 )
{
result = 1LL;
if ( v3 <= v4 )
result = *((_DWORD *)a1 + 3) < *((_DWORD *)a2 + 3) ? 1 : -1;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
__int64 result; // rax
unsigned int v3; // ecx
unsigned int v4; // edx

result = 0xFFFFFFFFLL;
if ( *(_QWORD *)a1 >= *(_QWORD *)a2 )
{
result = 1LL;
if ( *(_QWORD *)a1 <= *(_QWORD *)a2 )
{
v3 = *((_DWORD *)a1 + 2);
v4 = *((_DWORD *)a2 + 2);
result = 0xFFFFFFFFLL;
if ( v3 >= v4 )
{
result = 1LL;
if ( v3 <= v4 )
result = *((_DWORD *)a1 + 3) < *((_DWORD *)a2 + 3) ? 1 : -1;
}
}
}
return result;
}

original_code:
static int sort_func(const void *ptr1, const void *ptr2) {
   const Event *ev1 = (const Event*) ptr1;
   const Event *ev2 = (const Event*) ptr2;
   if (ev1->timestamp < ev2->timestamp) return -1;
   if (ev1->timestamp > ev2->timestamp) return 1;
   if (ev1->channel < ev2->channel) return -1;
   if (ev1->channel > ev2->channel) return 1;
   if (ev1->type < ev2->type) return 1;
   if (ev1->type > ev2->type) return -1;
   return -1;
}

canonical_original_code:
static int sort_func(void *ptr1, void *ptr2) {
   struct Event *ev1 = (const Event*) ptr1;
   struct Event *ev2 = (const Event*) ptr2;
   if (ev1->timestamp < ev2->timestamp) return -1;
   if (ev1->timestamp > ev2->timestamp) return 1;
   if (ev1->channel < ev2->channel) return -1;
   if (ev1->channel > ev2->channel) return 1;
   if (ev1->type < ev2->type) return 1;
   if (ev1->type > ev2->type) return -1;
   return -1;
}

variable_types: struct Event { unsigned long timestamp; unsigned int channel; enum EventType { EV_NOP=0, EV_NOTEON=1, EV_NOTEOFF=2, EV_SETNOTE=3, EV_SETFREQ=4, EV_SETVOL=5, EV_SETPAN=6, EV_SETINSTR=7, EV_SETTEMPO=8, EV_SETREG=9, EV_FLAGS=10, EV_LOCK=11, EV_LOOP=12 } type; unsigned int value; } *

user_defined_types: struct Event { unsigned long timestamp; unsigned int channel; enum EventType type; unsigned int value; }


decompiled_code:
__int64 __fastcall alloc_event(__int64 a1, int a2, int a3, int a4)
{
size_t v6; // r14
void *v7; // rax
__int64 result; // rax

++num_events;
v6 = 24 * num_events;
v7 = realloc(events, 24 * num_events);
if ( !v7 )
{
fwrite("Error: out of memory!\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
events = v7;
result = (__int64)v7 + v6 - 24;
*(_QWORD *)result = a1;
*(_DWORD *)(result + 8) = a2;
*(_DWORD *)(result + 12) = a3;
*(_DWORD *)(result + 16) = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3, int a4)
{
size_t v6; // r14
void *v7; // rax
__int64 result; // rax

++num_events;
v6 = 24 * num_events;
v7 = realloc(events, 24 * num_events);
if ( !v7 )
{
fwrite("Error: out of memory!\n", 1uLL, 0x16uLL, stderr);
exit(1);
}
events = v7;
result = (__int64)v7 + v6 - 24;
*(_QWORD *)result = a1;
*(_DWORD *)(result + 8) = a2;
*(_DWORD *)(result + 12) = a3;
*(_DWORD *)(result + 16) = a4;
return result;
}

original_code:
static void alloc_event(uint64_t timestamp, unsigned channel,
EventType type, unsigned value) {
   num_events++;
   Event *tmp = (Event*) realloc(events, sizeof(Event) * num_events);
   if (tmp == ((void *)0)) {
      fprintf(stderr, "Error: out of memory!\n");
      exit(1);
   } else {
      events = tmp;
   }
   Event *ev = &events[num_events - 1];
   ev->timestamp = timestamp;
   ev->channel = channel;
   ev->type = type;
   ev->value = value;
}

canonical_original_code:
static void alloc_event(unsigned long timestamp, unsigned int channel, enum EventType type, unsigned int value) {
   num_events++;
   struct Event *tmp = (Event*) realloc(events, sizeof(Event) * num_events);
   if (tmp == ((void *)0)) {
      fprintf(stderr, "Error: out of memory!\n");
      exit(1);
   } else {
      events = tmp;
   }
   struct Event *ev = &events[num_events - 1];
   ev->timestamp = timestamp;
   ev->channel = channel;
   ev->type = type;
   ev->value = value;
}

variable_types:
timestamp
unsigned long

variable_types:
channel
unsigned int

variable_types: enum EventType { EV_NOP=0, EV_NOTEON=1, EV_NOTEOFF=2, EV_SETNOTE=3, EV_SETFREQ=4, EV_SETVOL=5, EV_SETPAN=6, EV_SETINSTR=7, EV_SETTEMPO=8, EV_SETREG=9, EV_FLAGS=10, EV_LOCK=11, EV_LOOP=12 }


decompiled_code:
__int64 __fastcall add_nop(__int64 a1)
{
return alloc_event(a1, 0, 0, 0);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return func1(a1, 0, 0, 0);
}

original_code:
void add_nop(uint64_t timestamp)
{
   alloc_event(timestamp, 0, EV_NOP, 0);
}

canonical_original_code:
void add_nop(unsigned long timestamp)
{
   alloc_event(timestamp, 0, EV_NOP, 0);
}


decompiled_code:
__int64 __fastcall add_note_on(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 1, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 1, a3);
}

original_code:
void add_note_on(uint64_t timestamp, unsigned channel, unsigned note)
{
   alloc_event(timestamp, channel, EV_NOTEON, note);
}

canonical_original_code:
void add_note_on(unsigned long timestamp, unsigned int channel, unsigned int note)
{
   alloc_event(timestamp, channel, EV_NOTEON, note);
}

variable_types:
note
unsigned int


decompiled_code:
__int64 __fastcall add_note_off(__int64 a1, int a2)
{
return alloc_event(a1, a2, 2, 0);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
return func1(a1, a2, 2, 0);
}

original_code:
void add_note_off(uint64_t timestamp, unsigned channel)
{
   alloc_event(timestamp, channel, EV_NOTEOFF, 0);
}

canonical_original_code:
void add_note_off(unsigned long timestamp, unsigned int channel)
{
   alloc_event(timestamp, channel, EV_NOTEOFF, 0);
}


decompiled_code:
__int64 __fastcall add_set_note(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 3, a3);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 3, a3);
}

original_code:
void add_set_note(uint64_t timestamp, unsigned channel,
unsigned semitone)
{
   alloc_event(timestamp, channel, EV_SETNOTE, semitone);
}

canonical_original_code:
void add_set_note(unsigned long timestamp, unsigned int channel, unsigned int semitone)
{
   alloc_event(timestamp, channel, EV_SETNOTE, semitone);
}

variable_types:
semitone
unsigned int


decompiled_code:
__int64 __fastcall add_set_freq(__int64 a1, unsigned int a2, unsigned int a3, __int16 a4)
{
unsigned __int16 v4; // ax

if ( a2 > 7 )
{
v4 = a3;
if ( a2 - 8 <= 2 )
v4 = ((unsigned __int16)(a3 >> a4) >> 4) | ((unsigned __int16)(a3 >> a4) << 8) & 0xF00;
}
else
{
v4 = a3 | (a4 << 11);
}
return alloc_event(a1, a2, 4, v4);
}

original_code:
void add_set_freq(uint64_t timestamp, unsigned channel,
unsigned freq, unsigned octave)
{
   uint16_t raw;
   if ( channel <= 0x07) {
      raw = freq | (octave << 11);
   } else if (channel >= 0x08 && channel <= 0x0A) {
      raw = freq >> octave;
      raw = ((raw & 0x0F) << 8) | (raw >> 4);
   } else {
      raw = freq;
   }
   alloc_event(timestamp, channel, EV_SETFREQ, raw);
}

canonical_original_code:
void add_set_freq(unsigned long timestamp, unsigned int channel, unsigned int freq, unsigned int octave)
{
   unsigned short raw;
   if ( channel <= 0x07) {
      raw = freq | (octave << 11);
   } else if (channel >= 0x08 && channel <= 0x0A) {
      raw = freq >> octave;
      raw = ((raw & 0x0F) << 8) | (raw >> 4);
   } else {
      raw = freq;
   }
   alloc_event(timestamp, channel, EV_SETFREQ, raw);
}

variable_types:
freq
unsigned int

variable_types:
octave
unsigned int

variable_types:
raw
unsigned short


decompiled_code:
__int64 __fastcall add_set_vol(__int64 a1, unsigned int a2, int a3)
{
int v3; // ecx

v3 = 15 - a3;
if ( a2 <= 7 )
v3 = table_0[v3];
return alloc_event(a1, a2, 5, v3);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2, int a3)
{
int v3; // ecx

v3 = 15 - a3;
if ( a2 <= 7 )
v3 = table_0[v3];
return func1(a1, a2, 5, v3);
}

original_code:
void add_set_vol(uint64_t timestamp, unsigned channel, unsigned volume)
{
   volume = 0x0F - volume;
   if ( channel <= 0x07) {
      static unsigned table[] = {
         0x00, 0x02, 0x04, 0x06,
         0x08, 0x0A, 0x0C, 0x0E,
         0x10, 0x14, 0x18, 0x1C,
         0x20, 0x30, 0x40, 0x7F
      };
      volume = table[volume];
   }
   alloc_event(timestamp, channel, EV_SETVOL, volume);
}

canonical_original_code:
void add_set_vol(unsigned long timestamp, unsigned int channel, unsigned int volume)
{
   volume = 0x0F - volume;
   if ( channel <= 0x07) {
      static unsigned int table[] = {
         0x00, 0x02, 0x04, 0x06,
         0x08, 0x0A, 0x0C, 0x0E,
         0x10, 0x14, 0x18, 0x1C,
         0x20, 0x30, 0x40, 0x7F
      };
      volume = table[volume];
   }
   alloc_event(timestamp, channel, EV_SETVOL, volume);
}

variable_types:
volume
unsigned int

variable_types:
table
unsigned int []


decompiled_code:
__int64 __fastcall add_set_pan(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 6, a3);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 6, a3);
}

original_code:
void add_set_pan(uint64_t timestamp, unsigned channel, unsigned panning)
{
   alloc_event(timestamp, channel, EV_SETPAN, panning);
}

canonical_original_code:
void add_set_pan(unsigned long timestamp, unsigned int channel, unsigned int panning)
{
   alloc_event(timestamp, channel, EV_SETPAN, panning);
}

variable_types:
panning
unsigned int


decompiled_code:
__int64 __fastcall add_set_instr(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 7, a3);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 7, a3);
}

original_code:
void add_set_instr(uint64_t timestamp, unsigned channel, unsigned instrument)
{
   alloc_event(timestamp, channel, EV_SETINSTR, instrument);
}

canonical_original_code:
void add_set_instr(unsigned long timestamp, unsigned int channel, unsigned int instrument)
{
   alloc_event(timestamp, channel, EV_SETINSTR, instrument);
}

variable_types:
instrument
unsigned int


decompiled_code:
__int64 __fastcall add_set_tempo(__int64 a1, int a2)
{
return alloc_event(a1, 0, 8, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
return func1(a1, 0, 8, a2);
}

original_code:
void add_set_tempo(uint64_t timestamp, unsigned tempo)
{
   alloc_event(timestamp, 0, EV_SETTEMPO, tempo);
}

canonical_original_code:
void add_set_tempo(unsigned long timestamp, unsigned int tempo)
{
   alloc_event(timestamp, 0, EV_SETTEMPO, tempo);
}

variable_types:
tempo
unsigned int


decompiled_code:
__int64 __fastcall add_set_reg(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 9, a3);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 9, a3);
}

original_code:
void add_set_reg(uint64_t timestamp, unsigned reg, unsigned value)
{
   alloc_event(timestamp, reg, EV_SETREG, value);
}

canonical_original_code:
void add_set_reg(unsigned long timestamp, unsigned int reg, unsigned int value)
{
   alloc_event(timestamp, reg, EV_SETREG, value);
}

variable_types:
reg
unsigned int


decompiled_code:
__int64 __fastcall add_set_flags(__int64 a1, int a2, int a3)
{
return alloc_event(a1, a2, 10, a3);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2, int a3)
{
return func1(a1, a2, 10, a3);
}

original_code:
void add_set_flags(uint64_t timestamp, unsigned setclr, unsigned flags)
{
   alloc_event(timestamp, setclr, EV_FLAGS, flags);
}

canonical_original_code:
void add_set_flags(unsigned long timestamp, unsigned int setclr, unsigned int flags)
{
   alloc_event(timestamp, setclr, EV_FLAGS, flags);
}

variable_types:
setclr
unsigned int

variable_types:
flags
unsigned int


decompiled_code:
__int64 __fastcall add_lock(__int64 a1, int a2)
{
return alloc_event(a1, a2, 11, 0);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2)
{
return func1(a1, a2, 11, 0);
}

original_code:
void add_lock(uint64_t timestamp, unsigned channel)
{
   alloc_event(timestamp, channel, EV_LOCK, 0);
}

canonical_original_code:
void add_lock(unsigned long timestamp, unsigned int channel)
{
   alloc_event(timestamp, channel, EV_LOCK, 0);
}


decompiled_code:
__int64 __fastcall add_loop(__int64 a1)
{
return alloc_event(a1, 0, 12, 0);
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return func1(a1, 0, 12, 0);
}

original_code:
void add_loop(uint64_t timestamp)
{
   alloc_event(timestamp, 0, EV_LOOP, 0);
}

canonical_original_code:
void add_loop(unsigned long timestamp)
{
   alloc_event(timestamp, 0, EV_LOOP, 0);
}


decompiled_code:
void sort_stream()
{
qsort(events, num_events, 0x18uLL, (__compar_fn_t)sort_func);
}

canonical_decompiled_code:
void func15()
{
qsort(events, num_events, 0x18uLL, (__compar_fn_t)sort_func);
}

original_code:
void sort_stream(void)
{
   qsort(events, num_events, sizeof(Event), sort_func);
}

canonical_original_code:
void sort_stream()
{
   qsort(events, num_events, sizeof(Event), sort_func);
}


decompiled_code:
size_t get_num_events()
{
return num_events;
}

canonical_decompiled_code:
size_t func16()
{
return num_events;
}

original_code:
size_t get_num_events(void)
{
   return num_events;
}

canonical_original_code:
unsigned long get_num_events()
{
   return num_events;
}

return_type: unsigned long


decompiled_code:
char *__fastcall get_event(__int64 a1)
{
return (char *)events + 24 * a1;
}

canonical_decompiled_code:
char *__fastcall func17(__int64 a1)
{
return (char *)events + 24 * a1;
}

original_code:
const Event *get_event(size_t id)
{
   return &events[id];
}

canonical_original_code:
struct Event *get_event(unsigned long id)
{
   return &events[id];
}

variable_types:
id
unsigned long

return_type: struct Event { unsigned long timestamp; unsigned int channel; enum EventType { EV_NOP=0, EV_NOTEON=1, EV_NOTEOFF=2, EV_SETNOTE=3, EV_SETFREQ=4, EV_SETVOL=5, EV_SETPAN=6, EV_SETINSTR=7, EV_SETTEMPO=8, EV_SETREG=9, EV_FLAGS=10, EV_LOCK=11, EV_LOOP=12 } type; unsigned int value; } *


canonical_original_code:
static int read_varlen(unsigned char **data, unsigned long *size) {
   unsigned char *ptr = *data;
   if (*size >= 1 && ptr[0] < 0x80) {
      *data += 1;
      *size -= 1;
      return *ptr;
   }
   else if (*size >= 2 && ptr[1] < 0x80) {
      *data += 2;
      *size -= 2;
      return (ptr[0] & 0x7F) << 7 | ptr[1];
   }
   else if (*size >= 3 && ptr[2] < 0x80) {
      *data += 3;
      *size -= 3;
      return (ptr[0] & 0x7F) << 14 |
             (ptr[1] & 0x7F) << 7 |
              ptr[2];
   }
   else if (*size >= 4 && ptr[3] < 0x80) {
      *data += 4;
      *size -= 4;
      return (ptr[0] & 0x7F) << 21 |
             (ptr[1] & 0x7F) << 14 |
             (ptr[2] & 0x7F) << 7 |
              ptr[3];
   }
   else
      return -1;
}

variable_types:
data
unsigned char **

variable_types:
size
unsigned long *

variable_types:
ptr
unsigned char *


decompiled_code:
void __fastcall calculate_timestamp(int a1, __int64 a2)
{
unsigned __int64 v2; // rax

if ( a1 )
{
if ( (*(_BYTE *)a2 & 1) != 0 )
v2 = 393216000LL * a1 / (unsigned __int64)*(unsigned int *)(a2 + 8) / *(unsigned int *)(a2 + 4);
else
v2 = 235929600LL * a1 / (unsigned __int64)*(unsigned int *)(a2 + 8) / *(unsigned int *)(a2 + 4);
*(_QWORD *)(a2 + 16) += v2;
}
}

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

if ( a1 )
{
if ( (*(_BYTE *)a2 & 1) != 0 )
v2 = 393216000LL * a1 / (unsigned __int64)*(unsigned int *)(a2 + 8) / *(unsigned int *)(a2 + 4);
else
v2 = 235929600LL * a1 / (unsigned __int64)*(unsigned int *)(a2 + 8) / *(unsigned int *)(a2 + 4);
*(_QWORD *)(a2 + 16) += v2;
}
}

original_code:
static void calculate_timestamp(int32_t delta, MidiTiming *timing) {
   if (delta == 0)
      return;
   uint64_t value;
   if (timing->smpte) {
      value = delta;
      value *= 60 * 100 << 16;
      value /= timing->speed;
      value /= timing->ticks;
   }
   else {
      value = delta;
      value *= 60 * 60 << 16;
      value /= timing->speed;
      value /= timing->ticks;
   }
   timing->last += value;
}

canonical_original_code:
static void calculate_timestamp(int delta, struct MidiTiming *timing) {
   if (delta == 0)
      return;
   unsigned long value;
   if (timing->smpte) {
      value = delta;
      value *= 60 * 100 << 16;
      value /= timing->speed;
      value /= timing->ticks;
   }
   else {
      value = delta;
      value *= 60 * 60 << 16;
      value /= timing->speed;
      value /= timing->ticks;
   }
   timing->last += value;
}

variable_types: delta int

variable_types: struct MidiTiming { unsigned int smpte; unsigned int ticks; unsigned int speed; unsigned long last; } *

variable_types:
value
unsigned long


decompiled_code:
__int64 __fastcall calculate_volume(int a1, unsigned int a2)
{
int v2; // eax
int v3; // edx
__int64 result; // rax

v2 = dword_D68[5 * a1] * dword_D64[5 * a1] / 127;
if ( a2 <= 5 )
{
v3 = dword_EA8[3 * status[5 * a1]] * v2 / 100;
goto LABEL_3;
}
if ( a2 - 6 <= 4 )
{
v3 = dword_14A8[3 * status[5 * a1]] * v2 / 100;
LABEL_3:
result = 127LL;
if ( v3 > 127 )
return result;
goto LABEL_4;
}
v3 = 127;
LABEL_4:
result = 0LL;
if ( v3 >= 0 )
result = (unsigned int)v3;
return result;
}

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

v2 = dword_D68[5 * a1] * dword_D64[5 * a1] / 127;
if ( a2 <= 5 )
{
v3 = dword_EA8[3 * status[5 * a1]] * v2 / 100;
goto LABEL_3;
}
if ( a2 - 6 <= 4 )
{
v3 = dword_14A8[3 * status[5 * a1]] * v2 / 100;
LABEL_3:
result = 127LL;
if ( v3 > 127 )
return result;
goto LABEL_4;
}
v3 = 127;
LABEL_4:
result = 0LL;
if ( v3 >= 0 )
result = (unsigned int)v3;
return result;
}

original_code:
static int calculate_volume(int midichan, int echochan) {
   int output = status[midichan].volume *
      status[midichan].velocity / 0x7F;
   if (echochan >= CHAN_FM1 && echochan <= CHAN_FM6) {
      output = output * instr_map[INSTR_FM]
      [status[midichan].instrument].volume / 100;
   } else if (echochan >= CHAN_PSG1 && echochan <= CHAN_PSG4EX) {
      output = output * instr_map[INSTR_PSG]
      [status[midichan].instrument].volume / 100;
   } else
      output = 0x7F;
   if (output > 0x7F)
      output = 0x7F;
   if (output < 0x00)
      output = 0x00;
   return output;
}

variable_types: midichan int

variable_types: echochan int

variable_types: output int


original_code:
static void free_chunk(Chunk *chunk) {
   if (chunk->data != ((void *)0))
      free(chunk->data);
   chunk->type = 0;
   chunk->size = 0;
   chunk->data = ((void *)0);
}

canonical_original_code:
static void free_chunk(struct Chunk *chunk) {
   if (chunk->data != ((void *)0))
      free(chunk->data);
   chunk->type = 0;
   chunk->size = 0;
   chunk->data = ((void *)0);
}

variable_types: struct Chunk { unsigned int type; unsigned int size; unsigned char *data; } *


decompiled_code:
void __fastcall map_channel(int a1, int a2)
{
channel_map[a1 - 1] = a2;
}

canonical_decompiled_code:
void __fastcall func6(int a1, int a2)
{
channel_map[a1 - 1] = a2;
}

original_code:
void map_channel(int midichan, int echochan) {
   channel_map[midichan - 1] = echochan;
}


decompiled_code:
__int64 __fastcall map_instrument(int a1, int a2, int a3, int a4, int a5)
{
__int64 result; // rax
__int64 v7; // r9
__int64 v8; // rdi
__int64 v9; // rcx

result = a1;
v7 = 3LL * a2;
v8 = 2LL * a1;
instr_map[128 * (v8 + result) + v7] = a3;
v9 = (v8 + result) << 9;
*(int *)((char *)&dword_EA4[v7] + v9) = a4;
*(int *)((char *)&dword_EA8[v7] + v9) = a5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, int a3, int a4, int a5)
{
__int64 result; // rax
__int64 v7; // r9
__int64 v8; // rdi
__int64 v9; // rcx

result = a1;
v7 = 3LL * a2;
v8 = 2LL * a1;
instr_map[128 * (v8 + result) + v7] = a3;
v9 = (v8 + result) << 9;
*(int *)((char *)&dword_EA4[v7] + v9) = a4;
*(int *)((char *)&dword_EA8[v7] + v9) = a5;
return result;
}

original_code:
void map_instrument(int type, int midiinstr, int echoinstr, int transpose,
int volume) {
   instr_map[type][midiinstr].instrument = echoinstr;
   instr_map[type][midiinstr].transpose = transpose;
   instr_map[type][midiinstr].volume = volume;
}

variable_types: type int

variable_types: midiinstr int

variable_types: echoinstr int

variable_types: transpose int

variable_types: volume int


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

result = (unsigned int)(512 / a1);
pitch_factor = 512 / a1;
return result;
}

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

result = (unsigned int)(512 / a1);
pitch_factor = 512 / a1;
return result;
}

original_code:
void set_pitch_range(int range) {
   pitch_factor = 0x200 / range;
}

variable_types: range int


original_code:
uint32_t decode_utf8(const char *text)
{
   const uint8_t *s = (const uint8_t *)(text);
   if (s[0] < 0x80) {
      return s[0];
   }
   else if ((s[0] & 0xE0) == 0xC0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      uint32_t code = (s[0] & 0x1F) << 6 |
                      (s[1] & 0x3F);
      if (code < 0x80) return 0xFFFD;
      return code;
   }
   else if ((s[0] & 0xF0) == 0xE0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[2] & 0xC0) != 0x80) return 0xFFFD;
      uint32_t code = (s[0] & 0x0F) << 12 |
                      (s[1] & 0x3F) << 6 |
                      (s[2] & 0x3F);
      if (code < 0x800) return 0xFFFD;
      if (code >= 0xD800 && code <= 0xDFFF) return 0xFFFD;
      if (code >= 0xFFFE) return 0xFFFD;
      return code;
   }
   else if ((s[0] & 0xF8) == 0xF0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[2] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[3] & 0xC0) != 0x80) return 0xFFFD;
      uint32_t code = (s[0] & 0x07) << 18 |
                      (s[1] & 0x3F) << 12 |
                      (s[2] & 0x3F) << 6 |
                      (s[3] & 0x3F);
      if (code < 0x10000) return 0xFFFD;
      if ((code & 0xFFFE) == 0xFFFE) return 0xFFFD;
      return code;
   }
   else {
      return 0xFFFD;
   }
}

canonical_original_code:
unsigned int decode_utf8(char *text)
{
   unsigned char *s = (const uint8_t *)(text);
   if (s[0] < 0x80) {
      return s[0];
   }
   else if ((s[0] & 0xE0) == 0xC0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      unsigned int code = (s[0] & 0x1F) << 6 |
                      (s[1] & 0x3F);
      if (code < 0x80) return 0xFFFD;
      return code;
   }
   else if ((s[0] & 0xF0) == 0xE0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[2] & 0xC0) != 0x80) return 0xFFFD;
      unsigned int code = (s[0] & 0x0F) << 12 |
                      (s[1] & 0x3F) << 6 |
                      (s[2] & 0x3F);
      if (code < 0x800) return 0xFFFD;
      if (code >= 0xD800 && code <= 0xDFFF) return 0xFFFD;
      if (code >= 0xFFFE) return 0xFFFD;
      return code;
   }
   else if ((s[0] & 0xF8) == 0xF0) {
      if ((s[1] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[2] & 0xC0) != 0x80) return 0xFFFD;
      if ((s[3] & 0xC0) != 0x80) return 0xFFFD;
      unsigned int code = (s[0] & 0x07) << 18 |
                      (s[1] & 0x3F) << 12 |
                      (s[2] & 0x3F) << 6 |
                      (s[3] & 0x3F);
      if (code < 0x10000) return 0xFFFD;
      if ((code & 0xFFFE) == 0xFFFE) return 0xFFFD;
      return code;
   }
   else {
      return 0xFFFD;
   }
}

variable_types:
text
char *

variable_types:
s
unsigned char *

variable_types:
code
unsigned int


decompiled_code:
__int64 __fastcall get_utf16_size(_WORD *a1)
{
__int64 result; // rax

result = 1LL;
if ( *a1 )
{
do
++result;
while ( a1[result - 1] );
}
return result * 2;
}

canonical_decompiled_code:
__int64 __fastcall func2(_WORD *a1)
{
__int64 result; // rax

result = 1LL;
if ( *a1 )
{
do
++result;
while ( a1[result - 1] );
}
return result * 2;
}

original_code:
size_t get_utf16_size(const uint16_t *text)
{
   size_t size = 0;
   for (;;) {
      size += 2;
      if (*text == 0) return size;
      text++;
   }
}

canonical_original_code:
unsigned long get_utf16_size(unsigned short *text)
{
   unsigned long size = 0;
   for (;;) {
      size += 2;
      if (*text == 0) return size;
      text++;
   }
}

variable_types:
text
unsigned short *

variable_types:
size
unsigned long


decompiled_code:
__int64 __fastcall get_palette_mapping(int a1)
{
return (unsigned int)mappings[a1];
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)mappings[a1];
}

original_code:
unsigned get_palette_mapping(unsigned group) {
    return mappings[group];
}

canonical_original_code:
unsigned int get_palette_mapping(unsigned int group) {
    return mappings[group];
}

variable_types:
group
unsigned int


decompiled_code:
void __fastcall remap_palette(char a1, char a2)
{
mappings[a1 & 0xF] = a2 & 7;
}

canonical_decompiled_code:
void __fastcall func1(char a1, char a2)
{
mappings[a1 & 0xF] = a2 & 7;
}

original_code:
void remap_palette(unsigned group, unsigned which) {
    mappings[group & 0x0F] = which & 0x07;
}

canonical_original_code:
void remap_palette(unsigned int group, unsigned int which) {
    mappings[group & 0x0F] = which & 0x07;
}

variable_types:
which
unsigned int


decompiled_code:
void __fastcall set_bitmap_palette(const __m128i *a1)
{
bitmap_pal = (__int128)_mm_loadu_si128(a1);
xmmword_8210 = (__int128)_mm_loadu_si128(a1 + 1);
}

canonical_decompiled_code:
void __fastcall func2(const __m128i *a1)
{
bitmap_pal = (__int128)_mm_loadu_si128(a1);
xmmword_8210 = (__int128)_mm_loadu_si128(a1 + 1);
}

original_code:
void set_bitmap_palette(const uint16_t *colors) {
   memcpy(bitmap_pal, colors, sizeof(bitmap_pal));
}

canonical_original_code:
void set_bitmap_palette(unsigned short *colors) {
   memcpy(bitmap_pal, colors, sizeof(bitmap_pal));
}

variable_types:
colors
unsigned short *


decompiled_code:
void set_fallback_palette()
{
set_bitmap_palette((const __m128i *)&fallback_pal);
}

canonical_decompiled_code:
void func3()
{
func2((const __m128i *)&fallback_pal);
}

original_code:
void set_fallback_palette(void) {
   set_bitmap_palette(fallback_pal);
}

canonical_original_code:
void set_fallback_palette() {
   set_bitmap_palette(fallback_pal);
}


decompiled_code:
void *__fastcall get_instrument(int a1)
{
void *result; // rax

result = (void *)instruments[a1];
if ( !result )
result = &dummy_blob;
return result;
}

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

result = (void *)instruments[a1];
if ( !result )
result = &dummy_blob;
return result;
}

original_code:
const Blob *get_instrument(unsigned id) {
   return (instruments[id] != ((void *)0)) ? instruments[id] : &dummy_blob;
}

canonical_original_code:
struct Blob *get_instrument(unsigned int id) {
   return (instruments[id] != ((void *)0)) ? instruments[id] : &dummy_blob;
}

variable_types:
id
unsigned int

user_defined_types: struct Blob { unsigned long size; unsigned char data[]; }


decompiled_code:
__int64 __fastcall mark_as_pcm(unsigned int a1)
{
__int64 result; // rax
_QWORD *v2; // rax
size_t v3; // rbx
__int64 v4; // rbp
_QWORD *v5; // rax
__int64 v6; // rdx
int v7; // eax
void *src; // [rsp+8h] [rbp-40h]

result = a1;
if ( !used_as_pcm[a1] )
{
used_as_pcm[a1] = 1;
v2 = (_QWORD *)instruments[a1];
src = v2 + 1;
v3 = *v2 - 1LL;
v4 = 0LL;
if ( pcm_blob )
v4 = *(_QWORD *)pcm_blob;
v5 = realloc(pcm_blob, v4 + v3 + 15);
pcm_blob = v5;
if ( !v5 )
abort();
*v5 = v4 + v3 + 7;
v6 = (__int64)v5 + v4 + 8;
*(_BYTE *)v6 = 103;
*(_BYTE *)(v6 + 1) = 102;
*(_BYTE *)(v6 + 2) = 0;
*(_WORD *)(v6 + 3) = v3;
*(_BYTE *)(v6 + 5) = BYTE2(v3);
*(_BYTE *)(v6 + 6) = BYTE3(v3);
memcpy((char *)v5 + v4 + 15, src, v3);
v7 = next_pcm_id;
pcm_map[a1] = next_pcm_id;
result = (unsigned int)(v7 + 1);
next_pcm_id = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 result; // rax
_QWORD *v2; // rax
size_t v3; // rbx
__int64 v4; // rbp
_QWORD *v5; // rax
__int64 v6; // rdx
int v7; // eax
void *src; // [rsp+8h] [rbp-40h]

result = a1;
if ( !used_as_pcm[a1] )
{
used_as_pcm[a1] = 1;
v2 = (_QWORD *)instruments[a1];
src = v2 + 1;
v3 = *v2 - 1LL;
v4 = 0LL;
if ( pcm_blob )
v4 = *(_QWORD *)pcm_blob;
v5 = realloc(pcm_blob, v4 + v3 + 15);
pcm_blob = v5;
if ( !v5 )
abort();
*v5 = v4 + v3 + 7;
v6 = (__int64)v5 + v4 + 8;
*(_BYTE *)v6 = 103;
*(_BYTE *)(v6 + 1) = 102;
*(_BYTE *)(v6 + 2) = 0;
*(_WORD *)(v6 + 3) = v3;
*(_BYTE *)(v6 + 5) = BYTE2(v3);
*(_BYTE *)(v6 + 6) = BYTE3(v3);
memcpy((char *)v5 + v4 + 15, src, v3);
v7 = next_pcm_id;
pcm_map[a1] = next_pcm_id;
result = (unsigned int)(v7 + 1);
next_pcm_id = result;
}
return result;
}

original_code:
void mark_as_pcm(unsigned id)
{
   if (used_as_pcm[id]) return;
   used_as_pcm[id] = 1;
   const uint8_t *data = instruments[id]->data;
   size_t size = instruments[id]->size - 1;
   uint8_t header[] = {
      0x67, 0x66, 0x00,
      (uint8_t)(size),
      (uint8_t)(size >> 8),
      (uint8_t)(size >> 16),
      (uint8_t)(size >> 24),
   };
   size_t total_size;
   if (pcm_blob == ((void *)0)) total_size = 0;
   else total_size = pcm_blob->size;
   size_t offset = total_size;
   total_size += 7 + size;
   pcm_blob = realloc(pcm_blob, sizeof(Blob) + total_size);
   if (pcm_blob == ((void *)0)) abort();
   pcm_blob->size = total_size;
   memcpy(&pcm_blob->data[offset+0], header, 7);
   memcpy(&pcm_blob->data[offset+7], data, size);
   pcm_map[id] = next_pcm_id;
   next_pcm_id++;
}

canonical_original_code:
void mark_as_pcm(unsigned int id)
{
   if (used_as_pcm[id]) return;
   used_as_pcm[id] = 1;
   unsigned char *data = instruments[id]->data;
   unsigned long size = instruments[id]->size - 1;
   unsigned char header[] = {
      0x67, 0x66, 0x00,
      (uint8_t)(size),
      (uint8_t)(size >> 8),
      (uint8_t)(size >> 16),
      (uint8_t)(size >> 24),
   };
   unsigned long total_size;
   if (pcm_blob == ((void *)0)) total_size = 0;
   else total_size = pcm_blob->size;
   unsigned long offset = total_size;
   total_size += 7 + size;
   pcm_blob = realloc(pcm_blob, sizeof(Blob) + total_size);
   if (pcm_blob == ((void *)0)) abort();
   pcm_blob->size = total_size;
   memcpy(&pcm_blob->data[offset+0], header, 7);
   memcpy(&pcm_blob->data[offset+7], data, size);
   pcm_map[id] = next_pcm_id;
   next_pcm_id++;
}

variable_types:
data
unsigned char *

variable_types:
header
unsigned char []

variable_types:
offset
unsigned long


decompiled_code:
__int64 __fastcall get_pcm_id(unsigned int a1)
{
mark_as_pcm(a1);
return (unsigned int)pcm_map[a1];
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
func2(a1);
return (unsigned int)pcm_map[a1];
}

original_code:
unsigned get_pcm_id(unsigned id)
{
   mark_as_pcm(id);
   return pcm_map[id];
}

canonical_original_code:
unsigned int get_pcm_id(unsigned int id)
{
   mark_as_pcm(id);
   return pcm_map[id];
}


decompiled_code:
void *get_pcm_blob()
{
return pcm_blob;
}

canonical_decompiled_code:
void *func4()
{
return pcm_blob;
}

original_code:
const Blob *get_pcm_blob(void)
{
   return pcm_blob;
}

canonical_original_code:
struct Blob *get_pcm_blob()
{
   return pcm_blob;
}


decompiled_code:
void __fastcall set_sprite_origin(int a1, int a2)
{
origin_x = a1;
origin_y = a2;
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
origin_x = a1;
origin_y = a2;
}

original_code:
void set_sprite_origin(int x, int y)
{
   origin_x = x;
   origin_y = y;
}

variable_types: x int

variable_types: y int


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

if ( a2 < 0 || a3 < 0 )
return 0LL;
result = 0LL;
if ( *(_DWORD *)a1 > a2 && *(_DWORD *)(a1 + 4) > a3 )
result = *(unsigned __int8 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a3) + a2);
return result;
}

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

if ( a2 < 0 || a3 < 0 )
return 0LL;
result = 0LL;
if ( *(_DWORD *)a1 > a2 && *(_DWORD *)(a1 + 4) > a3 )
result = *(unsigned __int8 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * a3) + a2);
return result;
}

original_code:
uint8_t get_pixel(const Bitmap *ptr, int x, int y) {
   if (x < 0 || y < 0 || x >= ptr->width || y >= ptr->height)
      return 0x00;
   return ptr->rows[y][x];
}

canonical_original_code:
unsigned char get_pixel(struct Bitmap *ptr, int x, int y) {
   if (x < 0 || y < 0 || x >= ptr->width || y >= ptr->height)
      return 0x00;
   return ptr->rows[y][x];
}

variable_types: struct Bitmap { int width; int height; unsigned char *data; unsigned char **rows; } *

return_type: unsigned char


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

v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

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

v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void destroy_bitmap(Bitmap *ptr) {
   if (ptr->rows)
      free(ptr->rows);
   if (ptr->data)
      free(ptr->data);
   free(ptr);
}

canonical_original_code:
void destroy_bitmap(struct Bitmap *ptr) {
   if (ptr->rows)
      free(ptr->rows);
   if (ptr->data)
      free(ptr->data);
   free(ptr);
}

user_defined_types: struct Bitmap { int width; int height; unsigned char *data; unsigned char **rows; }


decompiled_code:
int show_version()
{
return puts("1.2a");
}

canonical_decompiled_code:
int func0()
{
return puts("1.2a");
}

original_code:
void show_version(void)
{
   puts("1.2a");
}

canonical_original_code:
void show_version()
{
   puts("1.2a");
}


decompiled_code:
int __fastcall show_usage(const char *a1)
{
return fprintf(stderr, "Usage: %s <infile.mml> <outfile.esf>\n", a1);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
return fprintf(stderr, "Usage: %s <infile.mml> <outfile.esf>\n", a1);
}

original_code:
void show_usage(const char *name)
{
   fprintf(stderr, "Usage: %s <infile.mml> <outfile.esf>\n", name);
}

canonical_original_code:
void show_usage(char *name)
{
   fprintf(stderr, "Usage: %s <infile.mml> <outfile.esf>\n", name);
}

variable_types:
name
char *


main

canonical_name: main

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

if ( argc == 2 )
{
v4 = argv[1];
if ( !strcmp(v4, "-v") || (v3 = strcmp(v4, "--version")) != 0 )
{
show_version();
return 0;
}
if ( !strcmp(v4, "-h") || strcmp(v4, "--help") || !strcmp(v4, "-?") )
{
show_usage(*argv);
return v3;
}
goto LABEL_10;
}
if ( argc != 3 )
{
LABEL_10:
show_usage(*argv);
return 1;
}
v3 = 1;
if ( !(unsigned int)parse_mml(argv[1]) )
v3 = generate_esf(argv[2]) != 0;
return v3;
}

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

if ( argc == 2 )
{
v4 = argv[1];
if ( !strcmp(v4, "-v") || (v3 = strcmp(v4, "--version")) != 0 )
{
func0();
return 0;
}
if ( !strcmp(v4, "-h") || strcmp(v4, "--help") || !strcmp(v4, "-?") )
{
func1(*argv);
return v3;
}
goto LABEL_10;
}
if ( argc != 3 )
{
LABEL_10:
func1(*argv);
return 1;
}
v3 = 1;
if ( !(unsigned int)parse_mml(argv[1]) )
v3 = generate_esf(argv[2]) != 0;
return v3;
}

original_code:
int main(int argc, char **argv)
{
   if (argc == 2) {
      if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version")) {
         show_version();
         return 0;
      }
      if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") ||
      strcmp(argv[1], "-?") == 0) {
         show_usage(argv[0]);
         return 0;
      }
   }
   if (argc != 3) {
      show_usage(argv[0]);
      return 1;
   }
   if (parse_mml(argv[1]))
      return 1;
   if (generate_esf(argv[2]))
      return 1;
   return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall get_tile(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned int v4; // er14
unsigned __int8 v5; // r13
unsigned int v6; // er12
unsigned int v7; // ebp
int v8; // ebx
unsigned __int8 v9; // al
int v10; // eax
__int64 result; // rax
int *v12; // [rsp+8h] [rbp-50h]
unsigned int v13; // [rsp+10h] [rbp-48h]

v12 = (int *)a2;
v4 = a4;
v13 = a4 + 8;
v5 = 0;
do
{
v6 = a3;
v7 = 0;
v8 = 0;
do
{
v9 = get_pixel(a1, v6, v4);
v5 |= v9 >> 4;
v10 = v9 & 0xF;
v8 = v10 | (16 * v8);
v7 = (v10 << 28) | (v7 >> 4);
++v6;
}
while ( v6 != a3 + 8 );
*v12 = v8;
v12[8] = v7;
++v12;
++v4;
}
while ( v13 != v4 );
result = get_palette_mapping(v5);
*(_BYTE *)(a2 + 64) = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4)
{
unsigned int v4; // er14
unsigned __int8 v5; // r13
unsigned int v6; // er12
unsigned int v7; // ebp
int v8; // ebx
unsigned __int8 v9; // al
int v10; // eax
__int64 result; // rax
int *v12; // [rsp+8h] [rbp-50h]
unsigned int v13; // [rsp+10h] [rbp-48h]

v12 = (int *)a2;
v4 = a4;
v13 = a4 + 8;
v5 = 0;
do
{
v6 = a3;
v7 = 0;
v8 = 0;
do
{
v9 = get_pixel(a1, v6, v4);
v5 |= v9 >> 4;
v10 = v9 & 0xF;
v8 = v10 | (16 * v8);
v7 = (v10 << 28) | (v7 >> 4);
++v6;
}
while ( v6 != a3 + 8 );
*v12 = v8;
v12[8] = v7;
++v12;
++v4;
}
while ( v13 != v4 );
result = get_palette_mapping(v5);
*(_BYTE *)(a2 + 64) = result;
return result;
}

original_code:
void get_tile(const Bitmap *in, Tile *out, int bx, int by) {
   uint8_t flags = 0;
   for (int y = 0; y < 8; y++) {
      uint32_t normal = 0;
      uint32_t flipped = 0;
      for (int x = 0; x < 8; x++) {
         uint8_t pixel = get_pixel(in, bx + x, by + y);
         flags |= (pixel >> 4) & 0x0F;
         pixel &= 0x0F;
         normal = normal << 4 | pixel;
         flipped = flipped >> 4 | pixel << 28;
      }
      out->normal[y] = normal;
      out->flipped[y] = flipped;
   }
   out->flags = get_palette_mapping(flags);
}

canonical_original_code:
void get_tile(struct Bitmap *in, struct Tile *out, int bx, int by) {
   unsigned char flags = 0;
   for (int y = 0; y < 8; y++) {
      unsigned int normal = 0;
      unsigned int flipped = 0;
      for (int x = 0; x < 8; x++) {
         unsigned char pixel = get_pixel(in, bx + x, by + y);
         flags |= (pixel >> 4) & 0x0F;
         pixel &= 0x0F;
         normal = normal << 4 | pixel;
         flipped = flipped >> 4 | pixel << 28;
      }
      out->normal[y] = normal;
      out->flipped[y] = flipped;
   }
   out->flags = get_palette_mapping(flags);
}

variable_types: struct Tile { unsigned int normal[8]; unsigned int flipped[8]; unsigned char flags; } *

variable_types: bx int

variable_types: by int

variable_types:
flags
unsigned char

variable_types:
normal
unsigned int

variable_types:
flipped
unsigned int

variable_types:
pixel
unsigned char


decompiled_code:
__int64 get_output_format()
{
return (unsigned int)format;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)format;
}

original_code:
Format get_output_format(void) {
   return format;
}

canonical_original_code:
enum Format get_output_format() {
   return format;
}

return_type: enum Format { FORMAT_DEFAULT=0, FORMAT_4BPP=1, FORMAT_1BPP=2, FORMAT_TOOMANY=3 }


decompiled_code:
void __fastcall set_output_format(int a1)
{
format = a1;
}

canonical_decompiled_code:
void __fastcall func5(int a1)
{
format = a1;
}

original_code:
void set_output_format(Format value) {
   format = value;
}

canonical_original_code:
void set_output_format(enum Format value) {
   format = value;
}

variable_types: enum Format { FORMAT_DEFAULT=0, FORMAT_4BPP=1, FORMAT_1BPP=2, FORMAT_TOOMANY=3 }


decompiled_code:
void *get_events()
{
return list;
}

canonical_decompiled_code:
void *func0()
{
return list;
}

original_code:
const Event *get_events(void) {
   return list;
}

canonical_original_code:
struct Event *get_events() {
   return list;
}

return_type: struct Event { unsigned long timestamp; struct Event *next; struct Event *prev; short param; unsigned char type; unsigned char channel; short instrument; short volume; short panning; } *


decompiled_code:
void reset_events()
{
void *v0; // rbx
void *v1; // rdi

v0 = list;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 1);
free(v1);
}
list = 0LL;
}

canonical_decompiled_code:
void func1()
{
void *v0; // rbx
void *v1; // rdi

v0 = list;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 1);
free(v1);
}
list = 0LL;
}

original_code:
void reset_events(void) {
   for (Event *event = list; event != ((void *)0);) {
      Event *next = event->next;
      free(event);
      event = next;
   }
   list = ((void *)0);
}

canonical_original_code:
void reset_events() {
   for (struct Event *event = list; event != ((void *)0);) {
      struct Event *next = event->next;
      free(event);
      event = next;
   }
   list = ((void *)0);
}

variable_types: struct Event { unsigned long timestamp; struct Event *next; struct Event *prev; short param; unsigned char type; unsigned char channel; short instrument; short volume; short panning; } *


decompiled_code:
_BOOL8 __fastcall is_leaf(_QWORD *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( !*a1 )
result = a1[1] == 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(_QWORD *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( !*a1 )
result = a1[1] == 0LL;
return result;
}

original_code:
int is_leaf(node * tree)
{
  return (!tree->left && !tree->right);
}

canonical_original_code:
int is_leaf(struct node *tree)
{
  return (!tree->left && !tree->right);
}

variable_types: struct node { struct node *left; struct node *right; int data; int count; } *


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

result = *a1;
if ( !*a1 )
return a1[1];
if ( a1[1] )
result = 0LL;
return result;
}

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

result = *a1;
if ( !*a1 )
return a1[1];
if ( a1[1] )
result = 0LL;
return result;
}

original_code:
node * get_only_child(node * tree)
{
  if (!tree->left)
    return tree->right;
  if (!tree->right)
    return tree->left;
  return ((void *)0);
}

canonical_original_code:
struct node *get_only_child(struct node *tree)
{
  if (!tree->left)
    return tree->right;
  if (!tree->right)
    return tree->left;
  return ((void *)0);
}

variable_types:
tree
struct node *

return_type: struct node { struct node *left; struct node *right; int data; int count; } *


decompiled_code:
void **__fastcall delete_node_with_only_one_child(void *a1, void **a2, void *a3)
{
if ( *a2 == a1 )
{
*a2 = a3;
}
else if ( a2[1] == a1 )
{
a2[1] = a3;
}
if ( a1 )
free(a1);
return a2;
}

canonical_decompiled_code:
void **__fastcall func10(void *a1, void **a2, void *a3)
{
if ( *a2 == a1 )
{
*a2 = a3;
}
else if ( a2[1] == a1 )
{
a2[1] = a3;
}
if ( a1 )
free(a1);
return a2;
}

original_code:
node * delete_node_with_only_one_child(node * tree, node * vater, node * onlykid)
{
  if(vater->left == tree)
    vater->left = onlykid;
  else if (vater->right == tree)
    vater->right = onlykid;
  if (tree)
    free(tree);
  return vater;
}

canonical_original_code:
struct node *delete_node_with_only_one_child(struct node *tree, struct node *vater, struct node *onlykid)
{
  if(vater->left == tree)
    vater->left = onlykid;
  else if (vater->right == tree)
    vater->right = onlykid;
  if (tree)
    free(tree);
  return vater;
}

variable_types:
vater
struct node *

variable_types:
onlykid
struct node *

user_defined_types: struct node { struct node *left; struct node *right; int data; int count; }


decompiled_code:
__int64 __fastcall largest_from_left(__int64 *a1)
{
__int64 v1; // rax
__int64 v2; // rdx

v1 = *a1;
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return v2;
}

original_code:
node * largest_from_left(node * tree)
{
  node * left = tree->left;
  while (left->right)
    left = left->right;
  return left;
}

canonical_original_code:
struct node *largest_from_left(struct node *tree)
{
  struct node *left = tree->left;
  while (left->right)
    left = left->right;
  return left;
}


decompiled_code:
__int64 __fastcall swap_node_with_viable_node_from_subtrees(__int64 a1)
{
__int64 result; // rax
int v2; // edx
int v3; // edx

if ( (rand() & 1) != 0 )
result = (__int64)smallest_from_right(a1);
else
result = largest_from_left((__int64 *)a1);
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = *(_DWORD *)(result + 16);
*(_DWORD *)(result + 16) = v2;
v3 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = *(_DWORD *)(result + 20);
*(_DWORD *)(result + 20) = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
__int64 result; // rax
int v2; // edx
int v3; // edx

if ( (rand() & 1) != 0 )
result = (__int64)smallest_from_right(a1);
else
result = func11((__int64 *)a1);
v2 = *(_DWORD *)(a1 + 16);
*(_DWORD *)(a1 + 16) = *(_DWORD *)(result + 16);
*(_DWORD *)(result + 16) = v2;
v3 = *(_DWORD *)(a1 + 20);
*(_DWORD *)(a1 + 20) = *(_DWORD *)(result + 20);
*(_DWORD *)(result + 20) = v3;
return result;
}

original_code:
void swap_node_with_viable_node_from_subtrees(node * tree)
{
  node * to_swap = ((void *)0);
  if (rand() % 2)
    to_swap = smallest_from_right(tree);
  else
    to_swap = largest_from_left(tree);
  int data = tree->data; tree->data = to_swap->data; to_swap->data = data;
  int count = tree->count; tree->count = to_swap->count; to_swap->count = count;
}

canonical_original_code:
void swap_node_with_viable_node_from_subtrees(struct node *tree)
{
  struct node *to_swap = ((void *)0);
  if (rand() % 2)
    to_swap = smallest_from_right(tree);
  else
    to_swap = largest_from_left(tree);
  int data = tree->data; tree->data = to_swap->data; to_swap->data = data;
  int count = tree->count; tree->count = to_swap->count; to_swap->count = count;
}

variable_types: data int

variable_types: count int


decompiled_code:
void **__fastcall delete_node_from_tree(_QWORD *a1, void **a2, int a3)
{
void **v3; // rbp
int v4; // eax
int v5; // eax
void *v6; // rax

v3 = a2;
if ( a1 )
{
v4 = *((_DWORD *)a1 + 4);
if ( v4 > a3 )
{
delete_node_from_tree(*a1, a1);
}
else if ( v4 < a3 )
{
delete_node_from_tree(a1[1], a1);
}
else
{
v5 = *((_DWORD *)a1 + 5) - 1;
*((_DWORD *)a1 + 5) = v5;
if ( v5 <= 0 )
{
if ( is_leaf(a1) )
{
v3 = delete_leaf_node(a1, a2);
}
else
{
v6 = (void *)get_only_child(a1);
if ( v6 )
{
v3 = delete_node_with_only_one_child(a1, a2, v6);
}
else
{
swap_node_with_viable_node_from_subtrees((__int64)a1);
v3 = (void **)delete_node_from_tree(a1, a2);
}
}
}
}
}
return v3;
}

original_code:
node * delete_node_from_tree(node * tree, node * vater, int data)
{
  if (!tree)
    return vater;
  if(data < tree->data)
  {
    tree = delete_node_from_tree(tree->left, tree, data);
    return vater;
  }
  if(data > tree->data)
  {
    tree = delete_node_from_tree(tree->right, tree, data);
    return vater;
  }
  tree->count -= 1;
  if (tree->count > 0)
    return vater;
  if(is_leaf(tree))
    return delete_leaf_node(tree, vater);
  node * onlykid = ((void *)0);
  if((onlykid = get_only_child(tree)))
    return delete_node_with_only_one_child(tree, vater, onlykid);
  swap_node_with_viable_node_from_subtrees(tree);
  return delete_node_from_tree(tree, vater, data);
}

canonical_original_code:
struct node *delete_node_from_tree(struct node *tree, struct node *vater, int data)
{
  if (!tree)
    return vater;
  if(data < tree->data)
  {
    tree = delete_node_from_tree(tree->left, tree, data);
    return vater;
  }
  if(data > tree->data)
  {
    tree = delete_node_from_tree(tree->right, tree, data);
    return vater;
  }
  tree->count -= 1;
  if (tree->count > 0)
    return vater;
  if(is_leaf(tree))
    return delete_leaf_node(tree, vater);
  struct node *onlykid = ((void *)0);
  if((onlykid = get_only_child(tree)))
    return delete_node_with_only_one_child(tree, vater, onlykid);
  swap_node_with_viable_node_from_subtrees(tree);
  return delete_node_from_tree(tree, vater, data);
}


inorder

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

if ( a1 )
{
inorder(*(_QWORD *)a1);
printf("%d ", *(unsigned int *)(a1 + 16));
result = inorder(*(_QWORD *)(a1 + 8));
}
return result;
}

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

if ( a1 )
{
func14(*(_QWORD *)a1);
printf("%d ", *(unsigned int *)(a1 + 16));
result = func14(*(_QWORD *)(a1 + 8));
}
return result;
}

original_code:
void inorder(node * tree)
{
  if(tree == ((void *)0))
    return;
  inorder(tree->left);
  printf("%d ", tree->data);
  inorder(tree->right);
}

canonical_original_code:
void inorder(struct node *tree)
{
  if(tree == ((void *)0))
    return;
  inorder(tree->left);
  printf("%d ", tree->data);
  inorder(tree->right);
}


main

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

v3 = (char *)malloc(0x400uLL);
puts("Try a, d, p or q (for add, delete, print and quit respectively)");
v4 = 0LL;
while ( (unsigned int)gets() )
{
v5 = *v3;
if ( *v3 )
{
switch ( v5 )
{
case 'a':
printf("add: ");
__isoc99_scanf();
v4 = (void **)add_new_node_to_tree((__int64)v4, 0);
break;
case 'd':
printf("delete: ");
__isoc99_scanf();
v4 = delete_node_from_tree(v4, v4, 0);
break;
case 'p':
putchar(10);
inorder((__int64)v4);
putchar(10);
break;
case 'q':
goto LABEL_12;
default:
puts("Invalid input: Try a, d, p or q (for add, delete, print and quit respectively).");
break;
}
}
}
LABEL_12:
puts("Good bye");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
void **v4; // rbp
char v5; // al

v3 = (char *)malloc(0x400uLL);
puts("Try a, d, p or q (for add, delete, print and quit respectively)");
v4 = 0LL;
while ( (unsigned int)gets() )
{
v5 = *v3;
if ( *v3 )
{
switch ( v5 )
{
case 'a':
printf("add: ");
__isoc99_scanf();
v4 = (void **)add_new_node_to_tree((__int64)v4, 0);
break;
case 'd':
printf("delete: ");
__isoc99_scanf();
v4 = func13(v4, v4, 0);
break;
case 'p':
putchar(10);
func14((__int64)v4);
putchar(10);
break;
case 'q':
goto LABEL_12;
default:
puts("Invalid input: Try a, d, p or q (for add, delete, print and quit respectively).");
break;
}
}
}
LABEL_12:
puts("Good bye");
return 0;
}

original_code:
int main()
{
  int data = 0;
  char * input = malloc(1024*sizeof(char));
  node * tree = ((void *)0);
  printf("Try a, d, p or q (for add, delete, print and quit respectively)\n");
  while(gets(input) != ((void *)0))
  {
    if (strlen(input) == 0)
      continue;
    if(input[0] == 'a'){
      printf("add: ");
      scanf("%d", &data);
      tree = add_new_node_to_tree(tree, data);
      continue;
    }
    if (input[0] == 'd'){
      printf("delete: ");
      scanf("%d", &data);
      tree = delete_node_from_tree(tree, tree, data);
      continue;
    }
    if (input[0] == 'p'){
      printf("\n");
      inorder(tree);
      printf("\n");
      continue;
    }
    if (input [0] == 'q')
      break;
    printf("Invalid input: Try a, d, p or q (for add, delete, print and quit respectively).\n");
  }
  printf("Good bye\n");
  return 0;
}

canonical_original_code:
int main()
{
  int data = 0;
  char *input = malloc(1024*sizeof(char));
  struct node *tree = ((void *)0);
  printf("Try a, d, p or q (for add, delete, print and quit respectively)\n");
  while(gets(input) != ((void *)0))
  {
    if (strlen(input) == 0)
      continue;
    if(input[0] == 'a'){
      printf("add: ");
      scanf("%d", &data);
      tree = add_new_node_to_tree(tree, data);
      continue;
    }
    if (input[0] == 'd'){
      printf("delete: ");
      scanf("%d", &data);
      tree = delete_node_from_tree(tree, tree, data);
      continue;
    }
    if (input[0] == 'p'){
      printf("\n");
      inorder(tree);
      printf("\n");
      continue;
    }
    if (input [0] == 'q')
      break;
    printf("Invalid input: Try a, d, p or q (for add, delete, print and quit respectively).\n");
  }
  printf("Good bye\n");
  return 0;
}

variable_types:
input
char *


eInitPimplInstance

decompiled_code:
__int64 __fastcall eInitPimplInstance(_QWORD *a1)
{
void *v1; // rax
unsigned int v3; // ebx
char *v4; // rax

if ( !a1 )
return 4294967294LL;
v1 = malloc(4uLL);
*a1 = v1;
if ( v1 )
return 0LL;
v3 = *_errno_location();
v4 = strerror(v3);
printf("[%d] - %s\n", v3, v4);
return 4294967293LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
void *v1; // rax
unsigned int v3; // ebx
char *v4; // rax

if ( !a1 )
return 4294967294LL;
v1 = malloc(4uLL);
*a1 = v1;
if ( v1 )
return 0LL;
v3 = *_errno_location();
v4 = strerror(v3);
printf("[%d] - %s\n", v3, v4);
return 4294967293LL;
}

original_code:
EN_RetCode eInitPimplInstance( ST_PimplInst **pstPimpl )
{
 EN_RetCode eRet = RET_OK;
 if( pstPimpl == ((void *)0) ){
  return RET_ERR_PARAM;
 }
 *pstPimpl = ( ST_PimplInst * )malloc( sizeof( ST_PimplInst ) * 1 );
 if( *pstPimpl == ((void *)0) ){
  int iErrNo = (*__errno_location ());
  printf( "[%d] - %s\n", iErrNo, strerror( iErrNo ) );
  eRet = RET_ERR_SYSTEM;
 }
 return eRet;
}

canonical_original_code:
enum EN_RetCode eInitPimplInstance(struct ST_PimplInst_tag **pstPimpl)
{
 enum EN_RetCode eRet = RET_OK;
 if( pstPimpl == ((void *)0) ){
  return RET_ERR_PARAM;
 }
 *pstPimpl = ( struct ST_PimplInst_tag * )malloc( sizeof( ST_PimplInst ) * 1 );
 if( *pstPimpl == ((void *)0) ){
  int iErrNo = (*__errno_location ());
  printf( "[%d] - %s\n", iErrNo, strerror( iErrNo ) );
  eRet = RET_ERR_SYSTEM;
 }
 return eRet;
}

variable_types: struct ST_PimplInst_tag { enum EN_Mode { MODE_0=0, MODE_1=1, MODE_2=2, MODE_3=3, MODE_MAX=4 } eMode; } **

variable_types: enum EN_RetCode { RET_ERR_SYSTEM=-3, RET_ERR_PARAM=-2, RET_ERR_GENERAL=-1, RET_OK=0, RET_NOTHING_TO_DO=1 }

variable_types: iErrNo int

user_defined_types: struct ST_PimplInst_tag { enum EN_Mode eMode; }


eDeinitPimplInstance

decompiled_code:
__int64 __fastcall eDeinitPimplInstance(void **a1)
{
void *v1; // rdi
__int64 result; // rax

if ( !a1 )
return 4294967294LL;
v1 = *a1;
result = 1LL;
if ( v1 )
{
free(v1);
result = 0LL;
}
return result;
}

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

if ( !a1 )
return 4294967294LL;
v1 = *a1;
result = 1LL;
if ( v1 )
{
free(v1);
result = 0LL;
}
return result;
}

original_code:
EN_RetCode eDeinitPimplInstance( ST_PimplInst **pstPimpl )
{
 EN_RetCode eRet = RET_OK;
 if( pstPimpl == ((void *)0) ){
  return RET_ERR_PARAM;
 }
 if( *pstPimpl == ((void *)0) ){
  return RET_NOTHING_TO_DO;
 }else{
  free( *pstPimpl );
  pstPimpl = ((void *)0);
 }
 return eRet;
}

canonical_original_code:
enum EN_RetCode eDeinitPimplInstance(struct ST_PimplInst_tag **pstPimpl)
{
 enum EN_RetCode eRet = RET_OK;
 if( pstPimpl == ((void *)0) ){
  return RET_ERR_PARAM;
 }
 if( *pstPimpl == ((void *)0) ){
  return RET_NOTHING_TO_DO;
 }else{
  free( *pstPimpl );
  pstPimpl = ((void *)0);
 }
 return eRet;
}

return_type: enum EN_RetCode { RET_ERR_SYSTEM=-3, RET_ERR_PARAM=-2, RET_ERR_GENERAL=-1, RET_OK=0, RET_NOTHING_TO_DO=1 }

user_defined_types: enum EN_Mode { MODE_0=0, MODE_1=1, MODE_2=2, MODE_3=3, MODE_MAX=4 }


eGetMode

decompiled_code:
__int64 __fastcall eGetMode(unsigned int *a1)
{
__int64 result; // rax

if ( a1 )
result = *a1;
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a1 )
result = *a1;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
EN_Mode eGetMode( const struct ST_PimplInst_tag *pstData )
{
    if( pstData == ((void *)0) ){
        return -1;
    }
    return pstData->eMode;
}

canonical_original_code:
enum EN_Mode eGetMode(struct ST_PimplInst_tag *pstData)
{
    if( pstData == ((void *)0) ){
        return -1;
    }
    return pstData->eMode;
}

variable_types: struct ST_PimplInst_tag { enum EN_Mode { MODE_0=0, MODE_1=1, MODE_2=2, MODE_3=3, MODE_MAX=4 } eMode; } *


vSetMode

decompiled_code:
void __fastcall vSetMode(unsigned int *a1, unsigned int a2)
{
if ( a2 <= 4 )
{
if ( a1 )
*a1 = a2;
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int *a1, unsigned int a2)
{
if ( a2 <= 4 )
{
if ( a1 )
*a1 = a2;
}
}

original_code:
void vSetMode( struct ST_PimplInst_tag *pstData, EN_Mode eMode )
{
    if( pstData == ((void *)0) ){
        return;
    }
    if(
        ( eMode < MODE_0 ) ||
        ( MODE_MAX < eMode )
    ){
        return;
    }
    pstData->eMode = eMode;
    return;
}

canonical_original_code:
void vSetMode(struct ST_PimplInst_tag *pstData, enum EN_Mode eMode)
{
    if( pstData == ((void *)0) ){
        return;
    }
    if(
        ( eMode < MODE_0 ) ||
        ( MODE_MAX < eMode )
    ){
        return;
    }
    pstData->eMode = eMode;
    return;
}

variable_types: enum EN_Mode { MODE_0=0, MODE_1=1, MODE_2=2, MODE_3=3, MODE_MAX=4 }


startoutputtingbits

repo: silviocesare/Fuzzer

decompiled_code:
void startoutputtingbits()
{
out_buffer = 0;
out_bits_to_go = 8;
}

canonical_decompiled_code:
void func0()
{
out_buffer = 0;
out_bits_to_go = 8;
}

original_code:
void startoutputtingbits(void)
{
    _out_buffer = 0;
    _out_bits_to_go = 8;
}

canonical_original_code:
void startoutputtingbits()
{
    _out_buffer = 0;
    _out_bits_to_go = 8;
}


startinputtingbits

decompiled_code:
void startinputtingbits()
{
in_garbage = 0;
in_bit_ptr = 0;
}

canonical_decompiled_code:
void func1()
{
in_garbage = 0;
in_bit_ptr = 0;
}

original_code:
void startinputtingbits(void)
{
    _in_garbage = 0;
    _in_bit_ptr = 0;
}

canonical_original_code:
void startinputtingbits()
{
    _in_garbage = 0;
    _in_bit_ptr = 0;
}


doneoutputtingbits

decompiled_code:
int doneoutputtingbits()
{
int result; // eax

if ( out_bits_to_go != 8 )
{
++bytes_output;
result = putchar(out_buffer << out_bits_to_go);
}
out_bits_to_go = 8;
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

if ( out_bits_to_go != 8 )
{
++bytes_output;
result = putchar(out_buffer << out_bits_to_go);
}
out_bits_to_go = 8;
return result;
}

original_code:
void doneoutputtingbits(void)
{
    if (_out_bits_to_go != 8)
 ( _bytes_output++, putchar(_out_buffer << _out_bits_to_go) );
    _out_bits_to_go = 8;
}

canonical_original_code:
void doneoutputtingbits()
{
    if (_out_bits_to_go != 8)
 ( _bytes_output++, putchar(_out_buffer << _out_bits_to_go) );
    _out_bits_to_go = 8;
}


doneinputtingbits

decompiled_code:
void doneinputtingbits()
{
in_bit_ptr = 0;
}

canonical_decompiled_code:
void func3()
{
in_bit_ptr = 0;
}

original_code:
void doneinputtingbits(void)
{
      _in_bit_ptr = 0;
}

canonical_original_code:
void doneinputtingbits()
{
      _in_bit_ptr = 0;
}


decompiled_code:
__int64 bitio_bytes_in()
{
return (unsigned int)bytes_input;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)bytes_input;
}

original_code:
int bitio_bytes_in(void)
{
    return _bytes_input;
}

canonical_original_code:
int bitio_bytes_in()
{
    return _bytes_input;
}


decompiled_code:
__int64 bitio_bytes_out()
{
return (unsigned int)bytes_output;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)bytes_output;
}

original_code:
int bitio_bytes_out(void)
{
    return _bytes_output;
}

canonical_original_code:
int bitio_bytes_out()
{
    return _bytes_output;
}


decompiled_code:
__int64 __fastcall unget_bit(int a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx
__int64 result; // rax

v1 = 2 * in_bit_ptr;
if ( !(2 * in_bit_ptr) )
v1 = 1;
in_bit_ptr = v1;
v2 = in_buffer & (v1 - 1);
result = v2 | v1;
if ( !a1 )
result = v2;
in_buffer = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx
__int64 result; // rax

v1 = 2 * in_bit_ptr;
if ( !(2 * in_bit_ptr) )
v1 = 1;
in_bit_ptr = v1;
v2 = in_buffer & (v1 - 1);
result = v2 | v1;
if ( !a1 )
result = v2;
in_buffer = result;
return result;
}

original_code:
void unget_bit(int bit)
{
  _in_bit_ptr <<= 1;
  if (_in_bit_ptr == 0)
 _in_bit_ptr = 1;
  _in_buffer = _in_buffer & (_in_bit_ptr - 1);
  if (bit)
 _in_buffer |= _in_bit_ptr;
}

variable_types: bit int


getPacketFromInterface

decompiled_code:
__int64 __fastcall getPacketFromInterface(__int64 a1, unsigned int a2)
{
int v2; // ebp
int v3; // ebx
unsigned int v4; // er14
char v6; // al
char v7; // [rsp+0h] [rbp-138h] BYREF
char v8; // [rsp+1h] [rbp-137h]
char src[310]; // [rsp+2h] [rbp-136h] BYREF

v2 = 0;
do
{
v3 = read(a2, &v7, 0x106uLL);
v4 = v3;
if ( v2 && v2 <= 9 )
{
sleep(1u);
}
else if ( v2 > 10 )
{
fprintf(
stderr,
"Aucune nouvelle requete apres %i secondes d'ecoute du tuyau de descripteur de fichier %i..\n",
10LL,
a2);
return (unsigned int)-1;
}
++v2;
}
while ( v3 <= 0 );
v6 = v7;
*(_DWORD *)a1 = v7;
if ( v6 > 5 )
{
if ( (unsigned __int8)(v6 - 6) <= 1u )
*(_BYTE *)(a1 + 4) = v8;
}
else if ( v6 > 3 )
{
*(_BYTE *)(a1 + 4) = v8;
strcpy((char *)(a1 + 5), src);
}
else if ( (unsigned __int8)v6 <= 3u )
{
*(_BYTE *)(a1 + 5) = v8;
*(_BYTE *)(a1 + 6) = src[0];
*(_BYTE *)(a1 + 4) = src[1];
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
int v2; // ebp
int v3; // ebx
unsigned int v4; // er14
char v6; // al
char v7; // [rsp+0h] [rbp-138h] BYREF
char v8; // [rsp+1h] [rbp-137h]
char src[310]; // [rsp+2h] [rbp-136h] BYREF

v2 = 0;
do
{
v3 = read(a2, &v7, 0x106uLL);
v4 = v3;
if ( v2 && v2 <= 9 )
{
sleep(1u);
}
else if ( v2 > 10 )
{
fprintf(
stderr,
"Aucune nouvelle requete apres %i secondes d'ecoute du tuyau de descripteur de fichier %i..\n",
10LL,
a2);
return (unsigned int)-1;
}
++v2;
}
while ( v3 <= 0 );
v6 = v7;
*(_DWORD *)a1 = v7;
if ( v6 > 5 )
{
if ( (unsigned __int8)(v6 - 6) <= 1u )
*(_BYTE *)(a1 + 4) = v8;
}
else if ( v6 > 3 )
{
*(_BYTE *)(a1 + 4) = v8;
strcpy((char *)(a1 + 5), src);
}
else if ( (unsigned __int8)v6 <= 3u )
{
*(_BYTE *)(a1 + 5) = v8;
*(_BYTE *)(a1 + 6) = src[0];
*(_BYTE *)(a1 + 4) = src[1];
}
return v4;
}

original_code:
int getPacketFromInterface(PRIM_PACKET* p, int fd)
{
    int redFromPipe, count = 0;
    char buffer[262];
    do
    {
        redFromPipe = read(fd,buffer,sizeof(buffer));
        if (count && count < 10) {
            sleep(1);
        }
        else if (count > 10) {
            fprintf(stderr, "Aucune nouvelle requete apres %i secondes d'ecoute du tuyau de descripteur de fichier %i..\n",10,fd);
            return -1;
        }
        count++;
    }
    while (redFromPipe < 1);
    p->prim = buffer[0];
    switch(p->prim)
    {
        case N_CONNECT_req:
        case N_CONNECT_ind:
        case N_CONNECT_resp:
        case N_CONNECT_conf:
            p->con_prim_packet.src_addr = buffer[1];
            p->con_prim_packet.dest_addr = buffer[2];
            p->con_prim_packet.con_number = buffer[3];
            break;
        case N_DATA_req:
        case N_DATA_ind:
            p->data_prim_packet.con_number = buffer[1];
            strcpy(p->data_prim_packet.transaction,buffer+2);
            break;
        case N_DISCONNECT_req:
        case N_DISCONNECT_ind:
            p->rel_prim_packet.con_number = buffer[1];
            break;
    }
    return redFromPipe;
}

canonical_original_code:
int getPacketFromInterface(union PRIM_PACKET *p, int fd)
{
    int redFromPipe; int count = 0;
    char buffer[262];
    do
    {
        redFromPipe = read(fd,buffer,sizeof(buffer));
        if (count && count < 10) {
            sleep(1);
        }
        else if (count > 10) {
            fprintf(stderr, "Aucune nouvelle requete apres %i secondes d'ecoute du tuyau de descripteur de fichier %i..\n",10,fd);
            return -1;
        }
        count++;
    }
    while (redFromPipe < 1);
    p->prim = buffer[0];
    switch(p->prim)
    {
        case N_CONNECT_req:
        case N_CONNECT_ind:
        case N_CONNECT_resp:
        case N_CONNECT_conf:
            p->con_prim_packet.src_addr = buffer[1];
            p->con_prim_packet.dest_addr = buffer[2];
            p->con_prim_packet.con_number = buffer[3];
            break;
        case N_DATA_req:
        case N_DATA_ind:
            p->data_prim_packet.con_number = buffer[1];
            strcpy(p->data_prim_packet.transaction,buffer+2);
            break;
        case N_DISCONNECT_req:
        case N_DISCONNECT_ind:
            p->rel_prim_packet.con_number = buffer[1];
            break;
    }
    return redFromPipe;
}

variable_types: union PRIM_PACKET { enum PRIMITIVE { N_CONNECT_req=0, N_CONNECT_ind=1, N_CONNECT_resp=2, N_CONNECT_conf=3, N_DATA_req=4, N_DATA_ind=5, N_DISCONNECT_req=6, N_DISCONNECT_ind=7 } prim; struct _CON_PRIM_PACKET { enum PRIMITIVE { N_CONNECT_req=0, N_CONNECT_ind=1, N_CONNECT_resp=2, N_CONNECT_conf=3, N_DATA_req=4, N_DATA_ind=5, N_DISCONNECT_req=6, N_DISCONNECT_ind=7 } prim; char con_number; char src_addr; char dest_addr; } con_prim_packet; struct _DATA_PRIM_PACKET { enum PRIMITIVE { N_CONNECT_req=0, N_CONNECT_ind=1, N_CONNECT_resp=2, N_CONNECT_conf=3, N_DATA_req=4, N_DATA_ind=5, N_DISCONNECT_req=6, N_DISCONNECT_ind=7 } prim; char con_number; char transaction[256]; } data_prim_packet; struct _REL_PRIM_PACKET { enum PRIMITIVE { N_CONNECT_req=0, N_CONNECT_ind=1, N_CONNECT_resp=2, N_CONNECT_conf=3, N_DATA_req=4, N_DATA_ind=5, N_DISCONNECT_req=6, N_DISCONNECT_ind=7 } prim; char con_number; } rel_prim_packet; } *

variable_types: fd int

variable_types: redFromPipe int

variable_types:
buffer
char [262]

user_defined_types: union PRIM_PACKET { enum PRIMITIVE prim; struct _CON_PRIM_PACKET con_prim_packet; struct _DATA_PRIM_PACKET data_prim_packet; struct _REL_PRIM_PACKET rel_prim_packet; }

user_defined_types: struct _CON_PRIM_PACKET { enum PRIMITIVE prim; char con_number; char src_addr; char dest_addr; }

user_defined_types: struct _DATA_PRIM_PACKET { enum PRIMITIVE prim; char con_number; char transaction[256]; }

user_defined_types: struct _REL_PRIM_PACKET { enum PRIMITIVE prim; char con_number; }


sendPacketToInterface

decompiled_code:
ssize_t __fastcall sendPacketToInterface(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
ssize_t result; // rax
int v4; // ebx
char v5; // [rsp+0h] [rbp-128h] BYREF
char v6; // [rsp+1h] [rbp-127h]
char dest[294]; // [rsp+2h] [rbp-126h] BYREF

v2 = *(_DWORD *)a1;
v5 = *(_DWORD *)a1;
if ( v2 > 5 )
{
if ( v2 - 6 <= 1 )
v6 = *(_BYTE *)(a1 + 4);
}
else if ( v2 > 3 )
{
v6 = *(_BYTE *)(a1 + 4);
strcpy(dest, (const char *)(a1 + 5));
}
else
{
v6 = *(_BYTE *)(a1 + 5);
dest[0] = *(_BYTE *)(a1 + 6);
dest[1] = *(_BYTE *)(a1 + 4);
}
result = write(a2, &v5, 0x106uLL);
v4 = 4;
while ( (int)result < 0 )
{
result = write(a2, &v5, 0x106uLL);
if ( !--v4 )
{
fprintf(stderr, "Erreur: Impossible d'ecrire dans le tuyau de descripteur de fichier %i\n", a2);
return 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(__int64 a1, unsigned int a2)
{
unsigned int v2; // eax
ssize_t result; // rax
int v4; // ebx
char v5; // [rsp+0h] [rbp-128h] BYREF
char v6; // [rsp+1h] [rbp-127h]
char dest[294]; // [rsp+2h] [rbp-126h] BYREF

v2 = *(_DWORD *)a1;
v5 = *(_DWORD *)a1;
if ( v2 > 5 )
{
if ( v2 - 6 <= 1 )
v6 = *(_BYTE *)(a1 + 4);
}
else if ( v2 > 3 )
{
v6 = *(_BYTE *)(a1 + 4);
strcpy(dest, (const char *)(a1 + 5));
}
else
{
v6 = *(_BYTE *)(a1 + 5);
dest[0] = *(_BYTE *)(a1 + 6);
dest[1] = *(_BYTE *)(a1 + 4);
}
result = write(a2, &v5, 0x106uLL);
v4 = 4;
while ( (int)result < 0 )
{
result = write(a2, &v5, 0x106uLL);
if ( !--v4 )
{
fprintf(stderr, "Erreur: Impossible d'ecrire dans le tuyau de descripteur de fichier %i\n", a2);
return 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int sendPacketToInterface(PRIM_PACKET* p, int fd)
{
    int writtenToPipe, count = 0;
    char buffer[262];
    buffer[0] = (char) p->prim;
    switch(p->prim)
    {
        case N_CONNECT_req:
        case N_CONNECT_ind:
        case N_CONNECT_resp:
        case N_CONNECT_conf:
            buffer[1] = p->con_prim_packet.src_addr;
            buffer[2] = p->con_prim_packet.dest_addr;
            buffer[3] = p->con_prim_packet.con_number;
            break;
        case N_DATA_req:
        case N_DATA_ind:
            buffer[1] = p->data_prim_packet.con_number;
            strcpy(buffer+2,p->data_prim_packet.transaction);
            break;
        case N_DISCONNECT_req:
        case N_DISCONNECT_ind:
            buffer[1] = p->rel_prim_packet.con_number;
            break;
    }
    do
    {
        writtenToPipe = write(fd,buffer,sizeof(buffer));
        if (count > 3) {
            fprintf(stderr, "Erreur: Impossible d'ecrire dans le tuyau de descripteur de fichier %i\n",fd);
            return -1;
        }
        count++;
    }
    while (writtenToPipe < 0);
    return writtenToPipe;
}

canonical_original_code:
int sendPacketToInterface(union PRIM_PACKET *p, int fd)
{
    int writtenToPipe; int count = 0;
    char buffer[262];
    buffer[0] = (char) p->prim;
    switch(p->prim)
    {
        case N_CONNECT_req:
        case N_CONNECT_ind:
        case N_CONNECT_resp:
        case N_CONNECT_conf:
            buffer[1] = p->con_prim_packet.src_addr;
            buffer[2] = p->con_prim_packet.dest_addr;
            buffer[3] = p->con_prim_packet.con_number;
            break;
        case N_DATA_req:
        case N_DATA_ind:
            buffer[1] = p->data_prim_packet.con_number;
            strcpy(buffer+2,p->data_prim_packet.transaction);
            break;
        case N_DISCONNECT_req:
        case N_DISCONNECT_ind:
            buffer[1] = p->rel_prim_packet.con_number;
            break;
    }
    do
    {
        writtenToPipe = write(fd,buffer,sizeof(buffer));
        if (count > 3) {
            fprintf(stderr, "Erreur: Impossible d'ecrire dans le tuyau de descripteur de fichier %i\n",fd);
            return -1;
        }
        count++;
    }
    while (writtenToPipe < 0);
    return writtenToPipe;
}

variable_types: writtenToPipe int

user_defined_types: enum PRIMITIVE { N_CONNECT_req=0, N_CONNECT_ind=1, N_CONNECT_resp=2, N_CONNECT_conf=3, N_DATA_req=4, N_DATA_ind=5, N_DISCONNECT_req=6, N_DISCONNECT_ind=7 }


deleteAllConnections

decompiled_code:
void deleteAllConnections()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = first_con_node;
if ( first_con_node )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
free(v1);
}
while ( v0 );
}
}

canonical_decompiled_code:
void func2()
{
_QWORD *v0; // rbx
void *v1; // rdi

v0 = first_con_node;
if ( first_con_node )
{
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
free(v1);
}
while ( v0 );
}
}

original_code:
void deleteAllConnections()
{
    Connection* node = first_con_node, *prev_node = first_con_node;
    while (node != ((void *)0)) {
        node = node->ncon.next;
        free(prev_node);
        prev_node = node;
    }
}

canonical_original_code:
void deleteAllConnections()
{
    union _Connection *node = first_con_node; union _Connection *prev_node = first_con_node;
    while (node != ((void *)0)) {
        node = node->ncon.next;
        free(prev_node);
        prev_node = node;
    }
}

variable_types: union _Connection { struct _TRANS_CON { union _Connection *next; char con_number; char state; } tcon; struct _NET_CON { union _Connection *next; char con_number; char state; char src_addr; char dest_addr; } ncon; } *

user_defined_types: union _Connection { struct _TRANS_CON tcon; struct _NET_CON ncon; }


decompiled_code:
void __fastcall remove_connection(char a1)
{
_QWORD *v2; // rax
_BYTE *v3; // rdi

if ( first_con_node )
{
v2 = first_con_node;
v3 = first_con_node;
while ( v3[8] != a1 )
{
if ( first_con_node != v3 )
v2 = v3;
v3 = *(_BYTE **)v3;
if ( !v3 )
return;
}
if ( first_con_node == v3 )
{
if ( (_BYTE *)last_con_node == v3 )
{
free(v3);
last_con_node = 0LL;
first_con_node = 0LL;
}
else
{
first_con_node = *(void **)v3;
free(v3);
}
}
else
{
if ( (_BYTE *)last_con_node == v3 )
{
last_con_node = (__int64)v2;
*v2 = 0LL;
}
else
{
*v2 = *(_QWORD *)v3;
}
free(v3);
}
}
}

canonical_decompiled_code:
void __fastcall func3(char a1)
{
_QWORD *v2; // rax
_BYTE *v3; // rdi

if ( first_con_node )
{
v2 = first_con_node;
v3 = first_con_node;
while ( v3[8] != a1 )
{
if ( first_con_node != v3 )
v2 = v3;
v3 = *(_BYTE **)v3;
if ( !v3 )
return;
}
if ( first_con_node == v3 )
{
if ( (_BYTE *)last_con_node == v3 )
{
free(v3);
last_con_node = 0LL;
first_con_node = 0LL;
}
else
{
first_con_node = *(void **)v3;
free(v3);
}
}
else
{
if ( (_BYTE *)last_con_node == v3 )
{
last_con_node = (__int64)v2;
*v2 = 0LL;
}
else
{
*v2 = *(_QWORD *)v3;
}
free(v3);
}
}
}

original_code:
void remove_connection(char con_number)
{
    Connection* node = first_con_node, *prev_node = node;
    while (node != ((void *)0)) {
        if (node->ncon.con_number == con_number) {
            if (node == first_con_node && node == last_con_node) {
                free(node);
                first_con_node = last_con_node = ((void *)0);
                return;
            }
            else if (node == first_con_node) {
                first_con_node = node->ncon.next;
                free(node);
                return;
            }
            else if (node == last_con_node) {
                last_con_node = prev_node;
                prev_node->ncon.next = ((void *)0);
                free(node);
                return;
            }
            else {
                prev_node->ncon.next = node->ncon.next;
                free(node);
                return;
            }
        }
        if (node != first_con_node)
            prev_node = node;
        node = node->ncon.next;
    }
}

canonical_original_code:
void remove_connection(char con_number)
{
    union _Connection *node = first_con_node; union _Connection *prev_node = node;
    while (node != ((void *)0)) {
        if (node->ncon.con_number == con_number) {
            if (node == first_con_node && node == last_con_node) {
                free(node);
                first_con_node = last_con_node = ((void *)0);
                return;
            }
            else if (node == first_con_node) {
                first_con_node = node->ncon.next;
                free(node);
                return;
            }
            else if (node == last_con_node) {
                last_con_node = prev_node;
                prev_node->ncon.next = ((void *)0);
                free(node);
                return;
            }
            else {
                prev_node->ncon.next = node->ncon.next;
                free(node);
                return;
            }
        }
        if (node != first_con_node)
            prev_node = node;
        node = node->ncon.next;
    }
}

user_defined_types: struct _TRANS_CON { union _Connection *next; char con_number; char state; }

user_defined_types: struct _NET_CON { union _Connection *next; char con_number; char state; char src_addr; char dest_addr; }