decompiled_code:
void pre_heating_resistor_start()
{
if ( !g_u32StartTime )
{
relay_control_set_on(17LL);
g_u32StartTime = time(0LL);
}
}

canonical_decompiled_code:
void func3()
{
if ( !g_u32StartTime )
{
relay_control_set_on(17LL);
g_u32StartTime = time(0LL);
}
}

original_code:
void pre_heating_resistor_start(void)
{
    if (!g_u32StartTime)
    {
        relay_control_set_on(17);
        g_u32StartTime = time(((void *)0));
    }
}

canonical_original_code:
void pre_heating_resistor_start()
{
    if (!g_u32StartTime)
    {
        relay_control_set_on(17);
        g_u32StartTime = time(((void *)0));
    }
}


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

++g_u32CheckCallCnt;
result = 3600 * (5 * g_u32CheckCallCnt / 0xE10u);
if ( 5 * g_u32CheckCallCnt == result )
result = save_on_time_to_file();
return result;
}

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

++g_u32CheckCallCnt;
result = 3600 * (5 * g_u32CheckCallCnt / 0xE10u);
if ( 5 * g_u32CheckCallCnt == result )
result = func1();
return result;
}

original_code:
void pre_heating_resistor_counter_update(void)
{
    g_u32CheckCallCnt++;
    if ( (g_u32CheckCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
}

canonical_original_code:
void pre_heating_resistor_counter_update()
{
    g_u32CheckCallCnt++;
    if ( (g_u32CheckCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
}

return_type: void


decompiled_code:
__int64 u32_pre_heating_resistor_get_on_time()
{
unsigned int v0; // ebx

v0 = g_u32StartTime;
if ( g_u32StartTime )
v0 = time(0LL) - v0;
return v0;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // ebx

v0 = g_u32StartTime;
if ( g_u32StartTime )
v0 = time(0LL) - v0;
return v0;
}

original_code:
uint32 u32_pre_heating_resistor_get_on_time()
{
    if (g_u32StartTime)
    {
        return time(((void *)0)) - g_u32StartTime;
    }
    else
    {
        return 0;
    }
}

canonical_original_code:
unsigned int u32_pre_heating_resistor_get_on_time()
{
    if (g_u32StartTime)
    {
        return time(((void *)0)) - g_u32StartTime;
    }
    else
    {
        return 0;
    }
}

return_type: unsigned int


decompiled_code:
void pre_heating_resistor_stop()
{
if ( g_u32StartTime )
{
relay_control_set_off(17LL);
g_u32OnTimeTotal += u32_pre_heating_resistor_get_on_time();
g_u32StartTime = 0;
}
}

canonical_decompiled_code:
void func6()
{
if ( g_u32StartTime )
{
relay_control_set_off(17LL);
g_u32OnTimeTotal += func5();
g_u32StartTime = 0;
}
}

original_code:
void pre_heating_resistor_stop(void)
{
    if (g_u32StartTime)
    {
        relay_control_set_off(17);
        g_u32OnTimeTotal += u32_pre_heating_resistor_get_on_time();
        g_u32StartTime = 0;
    }
}

canonical_original_code:
void pre_heating_resistor_stop()
{
    if (g_u32StartTime)
    {
        relay_control_set_off(17);
        g_u32OnTimeTotal += u32_pre_heating_resistor_get_on_time();
        g_u32StartTime = 0;
    }
}


decompiled_code:
bool pre_heating_resistor_get_status()
{
return g_u32StartTime != 0;
}

canonical_decompiled_code:
bool func7()
{
return g_u32StartTime != 0;
}

original_code:
_Bool pre_heating_resistor_get_status()
{
    if (g_u32StartTime)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


decompiled_code:
__int64 u32_pre_heating_resistor_get_on_time_total()
{
return (unsigned int)g_u32OnTimeTotal;
}

canonical_decompiled_code:
__int64 func8()
{
return (unsigned int)g_u32OnTimeTotal;
}

original_code:
uint32 u32_pre_heating_resistor_get_on_time_total()
{
    return g_u32OnTimeTotal;
}

canonical_original_code:
unsigned int u32_pre_heating_resistor_get_on_time_total()
{
    return g_u32OnTimeTotal;
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
pthread_t v3; // [rsp+8h] [rbp-30h] BYREF
pthread_t v4; // [rsp+10h] [rbp-28h] BYREF
pthread_t v5; // [rsp+18h] [rbp-20h] BYREF
pthread_t th; // [rsp+20h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+28h] [rbp-10h] BYREF

pthread_create(newthread, 0LL, (void *(*)(void *))&DS18B20_thread, "Thread: DS18B20");
pthread_create(&th, 0LL, (void *(*)(void *))&digit_receive_thread, "Thread: digit receive");
pthread_create(&v4, 0LL, (void *(*)(void *))&digit_update_thread, "Thread: digit update");
pthread_create(&v5, 0LL, (void *(*)(void *))&udp_server_thread, "Thread: udp-server");
pthread_create(&v3, 0LL, (void *(*)(void *))&ctrl_logic_thread, "Thread: ctrl logic");
pthread_join(newthread[0], 0LL);
pthread_join(th, 0LL);
pthread_join(v5, 0LL);
pthread_join(v4, 0LL);
pthread_join(v3, 0LL);
exit(0);
}

original_code:
int main(int argc, char *argv[])
{
    pthread_t thread1, thread2, thread3, thread4, thread5;
    char *message1 = "Thread: DS18B20";
    char *message2 = "Thread: digit receive";
    char *message3 = "Thread: digit update";
    char *message4 = "Thread: udp-server";
    char *message5 = "Thread: ctrl logic";
    pthread_create( &thread1, ((void *)0), DS18B20_thread, (void*) message1);
    pthread_create( &thread2, ((void *)0), digit_receive_thread, (void*) message2);
    pthread_create( &thread4, ((void *)0), digit_update_thread, (void*) message3);
    pthread_create( &thread3, ((void *)0), udp_server_thread, (void*) message4);
    pthread_create( &thread5, ((void *)0), ctrl_logic_thread, (void*) message5);
    pthread_join( thread1, ((void *)0));
    pthread_join( thread2, ((void *)0));
    pthread_join( thread3, ((void *)0));
    pthread_join( thread4, ((void *)0));
    pthread_join( thread5, ((void *)0));
    exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    unsigned long thread1; unsigned long thread2; unsigned long thread3; unsigned long thread4; unsigned long thread5;
    char *message1 = "Thread: DS18B20";
    char *message2 = "Thread: digit receive";
    char *message3 = "Thread: digit update";
    char *message4 = "Thread: udp-server";
    char *message5 = "Thread: ctrl logic";
    pthread_create( &thread1, ((void *)0), DS18B20_thread, (void *) message1);
    pthread_create( &thread2, ((void *)0), digit_receive_thread, (void *) message2);
    pthread_create( &thread4, ((void *)0), digit_update_thread, (void *) message3);
    pthread_create( &thread3, ((void *)0), udp_server_thread, (void *) message4);
    pthread_create( &thread5, ((void *)0), ctrl_logic_thread, (void *) message5);
    pthread_join( thread1, ((void *)0));
    pthread_join( thread2, ((void *)0));
    pthread_join( thread3, ((void *)0));
    pthread_join( thread4, ((void *)0));
    pthread_join( thread5, ((void *)0));
    exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


decompiled_code:
int defrost_resistor_init()
{
relay_control_init(22LL);
return read_on_time_from_file();
}

canonical_decompiled_code:
int func2()
{
relay_control_init(22LL);
return func0();
}

original_code:
void defrost_resistor_init(void)
{
    relay_control_init(22);
    read_on_time_from_file();
}

canonical_original_code:
void defrost_resistor_init()
{
    relay_control_init(22);
    read_on_time_from_file();
}


decompiled_code:
void defrost_resistor_start()
{
if ( !g_u32StartTime )
{
relay_control_set_on(22LL);
g_u32StartTime = time(0LL);
}
}

canonical_decompiled_code:
void func3()
{
if ( !g_u32StartTime )
{
relay_control_set_on(22LL);
g_u32StartTime = time(0LL);
}
}

original_code:
void defrost_resistor_start(void)
{
    if (!g_u32StartTime)
    {
        relay_control_set_on(22);
        g_u32StartTime = time(((void *)0));
    }
}

canonical_original_code:
void defrost_resistor_start()
{
    if (!g_u32StartTime)
    {
        relay_control_set_on(22);
        g_u32StartTime = time(((void *)0));
    }
}


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

++g_u32CheckCallCnt;
result = 3600 * (5 * g_u32CheckCallCnt / 0xE10u);
if ( 5 * g_u32CheckCallCnt == result )
result = save_on_time_to_file();
return result;
}

original_code:
void defrost_resistor_counter_update(void)
{
    g_u32CheckCallCnt++;
    if ( (g_u32CheckCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
}

canonical_original_code:
void defrost_resistor_counter_update()
{
    g_u32CheckCallCnt++;
    if ( (g_u32CheckCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
}


decompiled_code:
__int64 u32_defrost_resistor_get_on_time()
{
unsigned int v0; // ebx

v0 = g_u32StartTime;
if ( g_u32StartTime )
v0 = time(0LL) - v0;
return v0;
}

original_code:
uint32 u32_defrost_resistor_get_on_time()
{
    if (g_u32StartTime)
    {
        return time(((void *)0)) - g_u32StartTime;
    }
    else
    {
        return 0;
    }
}

canonical_original_code:
unsigned int u32_defrost_resistor_get_on_time()
{
    if (g_u32StartTime)
    {
        return time(((void *)0)) - g_u32StartTime;
    }
    else
    {
        return 0;
    }
}


decompiled_code:
void defrost_resistor_stop()
{
if ( g_u32StartTime )
{
relay_control_set_off(22LL);
g_u32OnTimeTotal += u32_defrost_resistor_get_on_time();
g_u32StartTime = 0;
}
}

canonical_decompiled_code:
void func6()
{
if ( g_u32StartTime )
{
relay_control_set_off(22LL);
g_u32OnTimeTotal += func5();
g_u32StartTime = 0;
}
}

original_code:
void defrost_resistor_stop(void)
{
    if (g_u32StartTime)
    {
        relay_control_set_off(22);
        g_u32OnTimeTotal += u32_defrost_resistor_get_on_time();
        g_u32StartTime = 0;
    }
}

canonical_original_code:
void defrost_resistor_stop()
{
    if (g_u32StartTime)
    {
        relay_control_set_off(22);
        g_u32OnTimeTotal += u32_defrost_resistor_get_on_time();
        g_u32StartTime = 0;
    }
}


decompiled_code:
bool defrost_resistor_get_status()
{
return g_u32StartTime != 0;
}

original_code:
_Bool defrost_resistor_get_status()
{
    if (g_u32StartTime)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


decompiled_code:
__int64 u32_defrost_resistor_get_on_time_total()
{
return (unsigned int)g_u32OnTimeTotal;
}

original_code:
uint32 u32_defrost_resistor_get_on_time_total()
{
    return g_u32OnTimeTotal;
}

canonical_original_code:
unsigned int u32_defrost_resistor_get_on_time_total()
{
    return g_u32OnTimeTotal;
}


decompiled_code:
void __noreturn DS18B20_thread()
{
while ( 1 )
{
read_DS18B20_sensors();
sleep(5u);
}
}

canonical_decompiled_code:
void __noreturn func2()
{
while ( 1 )
{
func1();
sleep(5u);
}
}

original_code:
void *DS18B20_thread( void *ptr )
{
    while(1)
    {
      read_DS18B20_sensors();
      sleep(5);
    }
    return ((void *)0);
}

canonical_original_code:
void *DS18B20_thread(void *ptr)
{
    while(1)
    {
      read_DS18B20_sensors();
      sleep(5);
    }
    return ((void *)0);
}

variable_types:
ptr
void *

return_type: void *


decompiled_code:
__int64 DS18B20_vars_ok()
{
time_t v0; // rax
unsigned int v1; // edx

v0 = time(0LL);
v1 = 0;
if ( v0 - g_DS18B20_timestamp_s1 <= 100 && v0 - g_DS18B20_timestamp_s2 <= 100 )
LOBYTE(v1) = v0 - g_DS18B20_timestamp_s3 <= 100;
return v1;
}

canonical_decompiled_code:
__int64 func3()
{
time_t v0; // rax
unsigned int v1; // edx

v0 = time(0LL);
v1 = 0;
if ( v0 - g_DS18B20_timestamp_s1 <= 100 && v0 - g_DS18B20_timestamp_s2 <= 100 )
LOBYTE(v1) = v0 - g_DS18B20_timestamp_s3 <= 100;
return v1;
}

original_code:
_Bool DS18B20_vars_ok()
{
    time_t current_time = time(((void *)0));
    if (current_time - g_DS18B20_timestamp_s1 > 100 ||
        current_time - g_DS18B20_timestamp_s2 > 100 ||
        current_time - g_DS18B20_timestamp_s3 > 100)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

canonical_original_code:
_Bool DS18B20_vars_ok()
{
    long current_time = time(((void *)0));
    if (current_time - g_DS18B20_timestamp_s1 > 100 ||
        current_time - g_DS18B20_timestamp_s2 > 100 ||
        current_time - g_DS18B20_timestamp_s3 > 100)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


decompiled_code:
float r32_DS18B20_outside_temp()
{
return *(float *)&g_r32DS18B20_temp_s1;
}

canonical_decompiled_code:
float func4()
{
return *(float *)&g_r32DS18B20_temp_s1;
}

original_code:
real32 r32_DS18B20_outside_temp()
{
    return g_r32DS18B20_temp_s1;
}

canonical_original_code:
float r32_DS18B20_outside_temp()
{
    return g_r32DS18B20_temp_s1;
}

return_type: float


decompiled_code:
float r32_DS18B20_exhaust_temp()
{
return *(float *)&g_r32DS18B20_temp_s2;
}

canonical_decompiled_code:
float func5()
{
return *(float *)&g_r32DS18B20_temp_s2;
}

original_code:
real32 r32_DS18B20_exhaust_temp()
{
    return g_r32DS18B20_temp_s2;
}

canonical_original_code:
float r32_DS18B20_exhaust_temp()
{
    return g_r32DS18B20_temp_s2;
}


decompiled_code:
float r32_DS18B20_incoming_temp()
{
return *(float *)&g_r32DS18B20_temp_s3;
}

canonical_decompiled_code:
float func6()
{
return *(float *)&g_r32DS18B20_temp_s3;
}

original_code:
real32 r32_DS18B20_incoming_temp()
{
    return g_r32DS18B20_temp_s3;
}

canonical_original_code:
float r32_DS18B20_incoming_temp()
{
    return g_r32DS18B20_temp_s3;
}


decompiled_code:
__int64 tDS18B20_outside_temp_ts()
{
return g_DS18B20_timestamp_s1;
}

canonical_decompiled_code:
__int64 func7()
{
return g_DS18B20_timestamp_s1;
}

original_code:
time_t tDS18B20_outside_temp_ts()
{
    return g_DS18B20_timestamp_s1;
}

canonical_original_code:
long tDS18B20_outside_temp_ts()
{
    return g_DS18B20_timestamp_s1;
}

return_type: long


decompiled_code:
__int64 tDS18B20_exhaust_temp_ts()
{
return g_DS18B20_timestamp_s2;
}

canonical_decompiled_code:
__int64 func8()
{
return g_DS18B20_timestamp_s2;
}

original_code:
time_t tDS18B20_exhaust_temp_ts()
{
    return g_DS18B20_timestamp_s2;
}

canonical_original_code:
long tDS18B20_exhaust_temp_ts()
{
    return g_DS18B20_timestamp_s2;
}


decompiled_code:
__int64 tDS18B20_incoming_temp_ts()
{
return g_DS18B20_timestamp_s3;
}

canonical_decompiled_code:
__int64 func9()
{
return g_DS18B20_timestamp_s3;
}

original_code:
time_t tDS18B20_incoming_temp_ts()
{
    return g_DS18B20_timestamp_s3;
}

canonical_original_code:
long tDS18B20_incoming_temp_ts()
{
    return g_DS18B20_timestamp_s3;
}


decompiled_code:
size_t __fastcall DS18B20_json_encode_vars(char *s)
{
float v1; // xmm0_4
__int64 v2; // rax
float v3; // xmm0_4
__int64 v4; // rax
float v5; // xmm0_4
__int64 v6; // rax
size_t result; // rax
char v8[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char sa[2]; // [rsp+3F0h] [rbp-3F8h] BYREF

*(_WORD *)s = 123;
sa[0] = 0;
v8[0] = 0;
v1 = r32_DS18B20_outside_temp();
json_encode_real32(v8, "value", v1);
*(_WORD *)&v8[strlen(v8)] = 44;
v2 = tDS18B20_outside_temp_ts(v8);
json_encode_integer(v8, "ts", v2);
json_encode_object(sa, "ds_outside_temp", v8);
*(_WORD *)&sa[strlen(sa)] = 44;
v8[0] = 0;
v3 = r32_DS18B20_exhaust_temp();
json_encode_real32(v8, "value", v3);
*(_WORD *)&v8[strlen(v8)] = 44;
v4 = tDS18B20_exhaust_temp_ts();
json_encode_integer(v8, "ts", v4);
json_encode_object(sa, "ds_exhaust_temp", v8);
*(_WORD *)&sa[strlen(sa)] = 44;
v8[0] = 0;
v5 = r32_DS18B20_incoming_temp();
json_encode_real32(v8, "value", v5);
*(_WORD *)&v8[strlen(v8)] = 44;
v6 = tDS18B20_incoming_temp_ts();
json_encode_integer(v8, "ts", v6);
json_encode_object(sa, "ds_incoming_temp", v8);
json_encode_object(s, "ds18b20_vars", sa);
result = strlen(s);
*(_WORD *)&s[result] = 125;
return result;
}

canonical_decompiled_code:
size_t __fastcall func10(char *s)
{
float v1; // xmm0_4
__int64 v2; // rax
float v3; // xmm0_4
__int64 v4; // rax
float v5; // xmm0_4
__int64 v6; // rax
size_t result; // rax
char v8[1008]; // [rsp+0h] [rbp-7E8h] BYREF
char sa[2]; // [rsp+3F0h] [rbp-3F8h] BYREF

*(_WORD *)s = 123;
sa[0] = 0;
v8[0] = 0;
v1 = func4();
json_encode_real32(v8, "value", v1);
*(_WORD *)&v8[strlen(v8)] = 44;
v2 = func7(v8);
json_encode_integer(v8, "ts", v2);
json_encode_object(sa, "ds_outside_temp", v8);
*(_WORD *)&sa[strlen(sa)] = 44;
v8[0] = 0;
v3 = func5();
json_encode_real32(v8, "value", v3);
*(_WORD *)&v8[strlen(v8)] = 44;
v4 = func8();
json_encode_integer(v8, "ts", v4);
json_encode_object(sa, "ds_exhaust_temp", v8);
*(_WORD *)&sa[strlen(sa)] = 44;
v8[0] = 0;
v5 = func6();
json_encode_real32(v8, "value", v5);
*(_WORD *)&v8[strlen(v8)] = 44;
v6 = func9();
json_encode_integer(v8, "ts", v6);
json_encode_object(sa, "ds_incoming_temp", v8);
json_encode_object(s, "ds18b20_vars", sa);
result = strlen(s);
*(_WORD *)&s[result] = 125;
return result;
}

original_code:
void DS18B20_json_encode_vars(char *mesg)
{
    char sub_str1[1000];
    char sub_str2[1000];
    strcpy(mesg, "{");
    strcpy(sub_str1, "");
    strcpy(sub_str2, "");
    json_encode_real32(sub_str2,
                      "value",
                      r32_DS18B20_outside_temp());
    strncat(sub_str2, ",", 1);
    json_encode_integer(sub_str2,
                        "ts",
                        tDS18B20_outside_temp_ts());
    json_encode_object(sub_str1,
                       "ds_outside_temp",
                       sub_str2);
    strncat(sub_str1, ",", 1);
    strcpy(sub_str2, "");
    json_encode_real32(sub_str2,
                      "value",
                      r32_DS18B20_exhaust_temp());
    strncat(sub_str2, ",", 1);
    json_encode_integer(sub_str2,
                        "ts",
                        tDS18B20_exhaust_temp_ts());
    json_encode_object(sub_str1,
                       "ds_exhaust_temp",
                       sub_str2);
    strncat(sub_str1, ",", 1);
    strcpy(sub_str2, "");
    json_encode_real32(sub_str2,
                      "value",
                      r32_DS18B20_incoming_temp());
    strncat(sub_str2, ",", 1);
    json_encode_integer(sub_str2,
                        "ts",
                        tDS18B20_incoming_temp_ts());
    json_encode_object(sub_str1,
                       "ds_incoming_temp",
                       sub_str2);
    json_encode_object(mesg,
                       "ds18b20_vars",
                       sub_str1);
    strncat(mesg, "}", 1);
}

variable_types:
mesg
char *


decompiled_code:
int __fastcall rs485_open(const char *a1)
{
int v1; // eax
int v2; // ebx
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

v1 = open(a1, 2306);
g_i32RS485_port = v1;
if ( v1 < 0 )
perror("open_port: Unable to open port");
else
fcntl(v1, 4, 0LL);
fsync(g_i32RS485_port);
v2 = g_i32RS485_port;
tcgetattr(g_i32RS485_port, &v4);
cfsetispeed(&v4, 0xDu);
cfsetospeed(&v4, 0xDu);
v4.c_lflag = 0;
v4.c_oflag = 0;
v4.c_cc[6] = 6;
v4.c_cc[5] = 5;
v4.c_iflag &= 0xFFFFE214;
v4.c_cflag = v4.c_cflag & 0x7FFFFC8F | 0x880;
tcsetattr(v2, 0, &v4);
return tcgetattr(v2, &v4);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int v1; // eax
int v2; // ebx
struct termios v4; // [rsp+0h] [rbp-48h] BYREF

v1 = open(a1, 2306);
g_i32RS485_port = v1;
if ( v1 < 0 )
perror("open_port: Unable to open port");
else
fcntl(v1, 4, 0LL);
fsync(g_i32RS485_port);
v2 = g_i32RS485_port;
tcgetattr(g_i32RS485_port, &v4);
cfsetispeed(&v4, 0xDu);
cfsetospeed(&v4, 0xDu);
v4.c_lflag = 0;
v4.c_oflag = 0;
v4.c_cc[6] = 6;
v4.c_cc[5] = 5;
v4.c_iflag &= 0xFFFFE214;
v4.c_cflag = v4.c_cflag & 0x7FFFFC8F | 0x880;
tcsetattr(v2, 0, &v4);
return tcgetattr(v2, &v4);
}

variable_types:
sFile
char *

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


decompiled_code:
int rs485_close()
{
return close(g_i32RS485_port);
}

canonical_decompiled_code:
int func1()
{
return close(g_i32RS485_port);
}

original_code:
void rs485_close()
{
    close(g_i32RS485_port);
}


decompiled_code:
__int64 __fastcall rs485_recv_msg(int a1, void *a2, int a3)
{
int v3; // ebx
int v4; // eax

v3 = a3;
if ( a3 <= 0 )
return 0LL;
while ( 1 )
{
v4 = read(g_i32RS485_port, &g_au8RecvBuf, 0x64uLL);
if ( v4 < 0 )
{
printf("ERROR: reading rs485 port, read_bytes = %d\n", (unsigned int)v4);
return 0LL;
}
if ( v4 == a1 )
break;
if ( !--v3 )
return 0LL;
}
memcpy(a2, &g_au8RecvBuf, a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, void *a2, int a3)
{
int v3; // ebx
int v4; // eax

v3 = a3;
if ( a3 <= 0 )
return 0LL;
while ( 1 )
{
v4 = read(g_i32RS485_port, &g_au8RecvBuf, 0x64uLL);
if ( v4 < 0 )
{
printf("ERROR: reading rs485 port, read_bytes = %d\n", (unsigned int)v4);
return 0LL;
}
if ( v4 == a1 )
break;
if ( !--v3 )
return 0LL;
}
memcpy(a2, &g_au8RecvBuf, a1);
return 1LL;
}

original_code:
_Bool rs485_recv_msg(int32 i32MsgLen, uint8 *au8Msg, int32 i32ReadAttempts)
{
    int32 i32ReadCnt = i32ReadAttempts;
    int32 i32ReadBytes;
    while(i32ReadCnt > 0)
    {
        i32ReadBytes = read(g_i32RS485_port, g_au8RecvBuf, 100);
        if (i32ReadBytes < 0)
        {
            printf("ERROR: reading rs485 port, read_bytes = %d\n",
                   i32ReadBytes);
            return 0;
        }
        else if (i32ReadBytes == i32MsgLen)
        {
            memcpy(au8Msg, g_au8RecvBuf, i32MsgLen);
            return 1;
        }
        i32ReadCnt--;
    }
    return 0;
}

canonical_original_code:
_Bool rs485_recv_msg(int i32MsgLen, unsigned char *au8Msg, int i32ReadAttempts)
{
    int i32ReadCnt = i32ReadAttempts;
    int i32ReadBytes;
    while(i32ReadCnt > 0)
    {
        i32ReadBytes = read(g_i32RS485_port, g_au8RecvBuf, 100);
        if (i32ReadBytes < 0)
        {
            printf("ERROR: reading rs485 port, read_bytes = %d\n",
                   i32ReadBytes);
            return 0;
        }
        else if (i32ReadBytes == i32MsgLen)
        {
            memcpy(au8Msg, g_au8RecvBuf, i32MsgLen);
            return 1;
        }
        i32ReadCnt--;
    }
    return 0;
}


decompiled_code:
bool __fastcall rs485_send_msg(int a1, const void *a2)
{
return write(g_i32RS485_port, a2, a1) > 0;
}

canonical_decompiled_code:
bool __fastcall func3(int a1, const void *a2)
{
return write(g_i32RS485_port, a2, a1) > 0;
}

original_code:
_Bool rs485_send_msg(int32 i32MsgLen, uint8 *au8SendMsg)
{
    if (write(g_i32RS485_port, au8SendMsg, i32MsgLen) > 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

canonical_original_code:
_Bool rs485_send_msg(int i32MsgLen, unsigned char *au8SendMsg)
{
    if (write(g_i32RS485_port, au8SendMsg, i32MsgLen) > 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


decompiled_code:
ssize_t udp_server_thread()
{
int v0; // ebp
size_t v1; // rax
ssize_t result; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-1F7Ch] BYREF
struct sockaddr v4; // [rsp+10h] [rbp-1F78h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-1F68h] BYREF
char s[8024]; // [rsp+30h] [rbp-1F58h] BYREF

addr_len = 16;
v0 = socket(2, 2, 0);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(8056);
bind(v0, &addr, 0x10u);
while ( 1 )
{
result = recvfrom(v0, s, 0x1F40uLL, 0, &v4, &addr_len);
if ( !(_DWORD)result )
break;
u32_json_decode_message((unsigned int)result, s);
v1 = strlen(s);
sendto(v0, s, v1, 0, &v4, 0x10u);
memset(s, 0, 0x1F40uLL);
}
return result;
}

canonical_decompiled_code:
ssize_t func2()
{
int v0; // ebp
size_t v1; // rax
ssize_t result; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-1F7Ch] BYREF
struct sockaddr v4; // [rsp+10h] [rbp-1F78h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-1F68h] BYREF
char s[8024]; // [rsp+30h] [rbp-1F58h] BYREF

addr_len = 16;
v0 = socket(2, 2, 0);
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func0(0);
*(_WORD *)addr.sa_data = func1(8056);
bind(v0, &addr, 0x10u);
while ( 1 )
{
result = recvfrom(v0, s, 0x1F40uLL, 0, &v4, &addr_len);
if ( !(_DWORD)result )
break;
u32_json_decode_message((unsigned int)result, s);
v1 = strlen(s);
sendto(v0, s, v1, 0, &v4, 0x10u);
memset(s, 0, 0x1F40uLL);
}
return result;
}

original_code:
void *udp_server_thread(void *ptr)
{
    char sMesg[8000];
    int32 i32ConnSocket, i32MsgLen;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t clilen = sizeof(cliaddr);
    i32ConnSocket=socket(2,SOCK_DGRAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr=htonl(((in_addr_t) 0x00000000));
    servaddr.sin_port=htons(8056);
    bind(i32ConnSocket,(struct sockaddr *)&servaddr,sizeof(servaddr));
    while(1)
    {
        i32MsgLen = recvfrom(i32ConnSocket, sMesg, 8000, 0,
                            (struct sockaddr *)&cliaddr, &clilen);
        if (i32MsgLen == 0)
        {
           break;
        }
        u32_json_decode_message(i32MsgLen, sMesg);
        sendto(i32ConnSocket, sMesg, strlen(sMesg), 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
        memset(sMesg, 0, 8000);
    }
}

canonical_original_code:
void *udp_server_thread(void *ptr)
{
    char sMesg[8000];
    int i32ConnSocket; int i32MsgLen;
    struct sockaddr_in servaddr; struct sockaddr_in cliaddr;
    unsigned int clilen = sizeof(cliaddr);
    i32ConnSocket=socket(2,SOCK_DGRAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr=htonl(((unsigned int) 0x00000000));
    servaddr.sin_port=htons(8056);
    bind(i32ConnSocket,(struct sockaddr *)&servaddr,sizeof(servaddr));
    while(1)
    {
        i32MsgLen = recvfrom(i32ConnSocket, sMesg, 8000, 0,
                            (struct sockaddr *)&cliaddr, &clilen);
        if (i32MsgLen == 0)
        {
           break;
        }
        u32_json_decode_message(i32MsgLen, sMesg);
        sendto(i32ConnSocket, sMesg, strlen(sMesg), 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
        memset(sMesg, 0, 8000);
    }
}

variable_types:
sMesg
char [8000]

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

variable_types:
clilen
unsigned int

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

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


decompiled_code:
__int64 __fastcall map_peripheral(__int64 a1)
{
int v1; // eax
void *v2; // rax
__int64 result; // rax

v1 = open("/dev/mem", 1052674);
*(_DWORD *)(a1 + 8) = v1;
if ( v1 < 0 )
{
puts("Failed to open /dev/mem, try checking permissions.");
result = 0xFFFFFFFFLL;
}
else
{
v2 = mmap(0LL, 0x1000uLL, 3, 1, v1, *(_QWORD *)a1);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 == (void *)-1LL )
{
perror("mmap");
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)(a1 + 24) = v2;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // eax
void *v2; // rax
__int64 result; // rax

v1 = open("/dev/mem", 1052674);
*(_DWORD *)(a1 + 8) = v1;
if ( v1 < 0 )
{
puts("Failed to open /dev/mem, try checking permissions.");
result = 0xFFFFFFFFLL;
}
else
{
v2 = mmap(0LL, 0x1000uLL, 3, 1, v1, *(_QWORD *)a1);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 == (void *)-1LL )
{
perror("mmap");
result = 0xFFFFFFFFLL;
}
else
{
*(_QWORD *)(a1 + 24) = v2;
result = 0LL;
}
}
return result;
}

original_code:
int map_peripheral(struct bcm2835_peripheral *p)
{
   if ((p->mem_fd = open("/dev/mem", 02|04010000) ) < 0)
   {
      printf("Failed to open /dev/mem, try checking permissions.\n");
      return -1;
   }
   p->map = mmap(
      ((void *)0),
      (4*1024),
      0x1|0x2,
      0x01,
      p->mem_fd,
      p->addr_p
   );
   if (p->map == ((void *) -1))
   {
        perror("mmap");
        return -1;
   }
   p->addr = (volatile unsigned int *)p->map;
   return 0;
}

canonical_original_code:
int map_peripheral(struct bcm2835_peripheral *p)
{
   if ((p->mem_fd = open("/dev/mem", 02|04010000) ) < 0)
   {
      printf("Failed to open /dev/mem, try checking permissions.\n");
      return -1;
   }
   p->map = mmap(
      ((void *)0),
      (4*1024),
      0x1|0x2,
      0x01,
      p->mem_fd,
      p->addr_p
   );
   if (p->map == ((void *) -1))
   {
        perror("mmap");
        return -1;
   }
   p->addr = (unsigned int *)p->map;
   return 0;
}

variable_types: struct bcm2835_peripheral { unsigned long addr_p; int mem_fd; void *map; unsigned int *addr; } *


decompiled_code:
int __fastcall unmap_peripheral(__int64 a1)
{
munmap(*(void **)(a1 + 16), 0x1000uLL);
return close(*(_DWORD *)(a1 + 8));
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
munmap(*(void **)(a1 + 16), 0x1000uLL);
return close(*(_DWORD *)(a1 + 8));
}

original_code:
void unmap_peripheral(struct bcm2835_peripheral *p)
{
    munmap(p->map, (4*1024));
    close(p->mem_fd);
}

user_defined_types: struct bcm2835_peripheral { unsigned long addr_p; int mem_fd; void *map; unsigned int *addr; }


decompiled_code:
__int64 __fastcall jsmn_parse_primitive(int *a1, __int64 a2, unsigned __int64 a3, __int64 a4, unsigned __int64 a5)
{
int v9; // ebp
unsigned __int64 v10; // rdx
unsigned int v11; // eax
char v12; // cl
_DWORD *v13; // rdi
__int64 result; // rax

v9 = *a1;
v10 = (unsigned int)*a1;
v11 = *a1;
if ( a3 > v10 )
{
do
{
v12 = *(_BYTE *)(a2 + v10);
if ( !v12 )
break;
if ( v12 > 58 )
{
if ( (v12 & 0xDF) == 93 )
break;
}
else if ( v12 > 8 && ((1LL << v12) & 0x400100100002600LL) != 0 )
{
break;
}
if ( (unsigned __int8)(v12 - 32) > 0x5Eu )
{
*a1 = v9;
return 4294967294LL;
}
*a1 = ++v11;
v10 = v11;
}
while ( v11 < a3 );
}
if ( a4 )
{
v13 = jsmn_alloc_token((__int64)a1, a4, a5);
if ( v13 )
{
jsmn_fill_token(v13, 0, v9, *a1);
--*a1;
result = 0LL;
}
else
{
*a1 = v9;
result = 0xFFFFFFFFLL;
}
}
else
{
*a1 = v11 - 1;
result = 0LL;
}
return result;
}

original_code:
static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
        size_t len, jsmntok_t *tokens, size_t num_tokens) {
    jsmntok_t *token;
    int start;
    start = parser->pos;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        switch (js[parser->pos]) {
            case ':':
            case '\t' : case '\r' : case '\n' : case ' ' :
            case ',' : case ']' : case '}' :
                goto found;
        }
        if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
            parser->pos = start;
            return JSMN_ERROR_INVAL;
        }
    }
found:
    if (tokens == ((void *)0)) {
        parser->pos--;
        return 0;
    }
    token = jsmn_alloc_token(parser, tokens, num_tokens);
    if (token == ((void *)0)) {
        parser->pos = start;
        return JSMN_ERROR_NOMEM;
    }
    jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
    parser->pos--;
    return 0;
}

canonical_original_code:
static enum jsmnerr_t jsmn_parse_primitive(struct jsmn_parser *parser, char *js, unsigned long len, struct jsmntok_t *tokens, unsigned long num_tokens) {
    struct jsmntok_t *token;
    int start;
    start = parser->pos;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        switch (js[parser->pos]) {
            case ':':
            case '\t' : case '\r' : case '\n' : case ' ' :
            case ',' : case ']' : case '}' :
                goto found;
        }
        if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
            parser->pos = start;
            return JSMN_ERROR_INVAL;
        }
    }
found:
    if (tokens == ((void *)0)) {
        parser->pos--;
        return 0;
    }
    token = jsmn_alloc_token(parser, tokens, num_tokens);
    if (token == ((void *)0)) {
        parser->pos = start;
        return JSMN_ERROR_NOMEM;
    }
    jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
    parser->pos--;
    return 0;
}

variable_types: struct jsmn_parser { unsigned int pos; unsigned int toknext; int toksuper; } *

variable_types:
js
char *

variable_types:
len
unsigned long

variable_types: struct jsmntok_t { enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 } type; int start; int end; int size; } *

variable_types: start int

return_type: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3 }

user_defined_types: struct jsmntok_t { enum jsmntype_t type; int start; int end; int size; }


decompiled_code:
__int64 __fastcall jsmn_parse_string(int *a1, __int64 a2, unsigned __int64 a3, __int64 a4, unsigned __int64 a5)
{
int v7; // ebp
unsigned int v8; // eax
unsigned __int64 v9; // rdx
__int64 result; // rax
_DWORD *v11; // rdi
int v12; // er10
char v13; // al
char v14; // dl
char v15; // dl

v7 = *a1;
v8 = v7 + 1;
++*a1;
v9 = (unsigned int)(v7 + 1);
if ( a3 > v9 )
{
do
{
v14 = *(_BYTE *)(a2 + v9);
if ( !v14 )
break;
if ( v14 == 34 )
{
result = 0LL;
if ( a4 )
{
v11 = jsmn_alloc_token((__int64)a1, a4, a5);
if ( v11 )
{
jsmn_fill_token(v11, 3, v7 + 1, *a1);
result = 0LL;
}
else
{
*a1 = v7;
result = 0xFFFFFFFFLL;
}
}
return result;
}
if ( v14 == 92 )
{
*a1 = v8 + 1;
v15 = *(_BYTE *)(a2 + v8 + 1);
if ( v15 > 117 )
goto LABEL_9;
if ( v15 <= 91 )
{
if ( v15 != 34 && v15 != 47 )
{
LABEL_9:
*a1 = v7;
return 4294967294LL;
}
}
else
{
switch ( v15 )
{
case '\\':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
goto LABEL_18;
case 'u':
*a1 = v8 + 2;
v12 = 4;
break;
default:
goto LABEL_9;
}
do
{
v13 = *(_BYTE *)(a2 + (unsigned int)*a1);
if ( !v13 )
break;
if ( (unsigned __int8)((v13 & 0xDF) - 65) > 5u && (unsigned __int8)(v13 - 48) > 9u )
{
*a1 = v7;
return 4294967294LL;
}
++*a1;
--v12;
}
while ( v12 );
--*a1;
}
}
LABEL_18:
v8 = *a1 + 1;
*a1 = v8;
v9 = v8;
}
while ( v8 < a3 );
}
*a1 = v7;
return 4294967293LL;
}

original_code:
static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
        size_t len, jsmntok_t *tokens, size_t num_tokens) {
    jsmntok_t *token;
    int start = parser->pos;
    parser->pos++;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        char c = js[parser->pos];
        if (c == '\"') {
            if (tokens == ((void *)0)) {
                return 0;
            }
            token = jsmn_alloc_token(parser, tokens, num_tokens);
            if (token == ((void *)0)) {
                parser->pos = start;
                return JSMN_ERROR_NOMEM;
            }
            jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
            return 0;
        }
        if (c == '\\') {
            parser->pos++;
            switch (js[parser->pos]) {
                case '\"': case '/' : case '\\' : case 'b' :
                case 'f' : case 'r' : case 'n' : case 't' :
                    break;
                case 'u':
                    parser->pos++;
                    int i = 0;
                    for(; i < 4 && js[parser->pos] != '\0'; i++) {
                        if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) ||
                                    (js[parser->pos] >= 65 && js[parser->pos] <= 70) ||
                                    (js[parser->pos] >= 97 && js[parser->pos] <= 102))) {
                            parser->pos = start;
                            return JSMN_ERROR_INVAL;
                        }
                        parser->pos++;
                    }
                    parser->pos--;
                    break;
                default:
                    parser->pos = start;
                    return JSMN_ERROR_INVAL;
            }
        }
    }
    parser->pos = start;
    return JSMN_ERROR_PART;
}

canonical_original_code:
static enum jsmnerr_t jsmn_parse_string(struct jsmn_parser *parser, char *js, unsigned long len, struct jsmntok_t *tokens, unsigned long num_tokens) {
    struct jsmntok_t *token;
    int start = parser->pos;
    parser->pos++;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        char c = js[parser->pos];
        if (c == '\"') {
            if (tokens == ((void *)0)) {
                return 0;
            }
            token = jsmn_alloc_token(parser, tokens, num_tokens);
            if (token == ((void *)0)) {
                parser->pos = start;
                return JSMN_ERROR_NOMEM;
            }
            jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
            return 0;
        }
        if (c == '\\') {
            parser->pos++;
            switch (js[parser->pos]) {
                case '\"': case '/' : case '\\' : case 'b' :
                case 'f' : case 'r' : case 'n' : case 't' :
                    break;
                case 'u':
                    parser->pos++;
                    int i = 0;
                    for(; i < 4 && js[parser->pos] != '\0'; i++) {
                        if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) ||
                                    (js[parser->pos] >= 65 && js[parser->pos] <= 70) ||
                                    (js[parser->pos] >= 97 && js[parser->pos] <= 102))) {
                            parser->pos = start;
                            return JSMN_ERROR_INVAL;
                        }
                        parser->pos++;
                    }
                    parser->pos--;
                    break;
                default:
                    parser->pos = start;
                    return JSMN_ERROR_INVAL;
            }
        }
    }
    parser->pos = start;
    return JSMN_ERROR_PART;
}

variable_types: c char

variable_types: i int

user_defined_types: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3 }

user_defined_types: struct jsmn_parser { unsigned int pos; unsigned int toknext; int toksuper; }

user_defined_types: enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 }


original_code:
jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
        jsmntok_t *tokens, unsigned int num_tokens) {
    jsmnerr_t r;
    int i;
    jsmntok_t *token;
    int count = 0;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        char c;
        jsmntype_t type;
        c = js[parser->pos];
        switch (c) {
            case '{': case '[':
                count++;
                if (tokens == ((void *)0)) {
                    break;
                }
                token = jsmn_alloc_token(parser, tokens, num_tokens);
                if (token == ((void *)0))
                    return JSMN_ERROR_NOMEM;
                if (parser->toksuper != -1) {
                    tokens[parser->toksuper].size++;
                }
                token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
                token->start = parser->pos;
                parser->toksuper = parser->toknext - 1;
                break;
            case '}': case ']':
                if (tokens == ((void *)0))
                    break;
                type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
                for (i = parser->toknext - 1; i >= 0; i--) {
                    token = &tokens[i];
                    if (token->start != -1 && token->end == -1) {
                        if (token->type != type) {
                            return JSMN_ERROR_INVAL;
                        }
                        parser->toksuper = -1;
                        token->end = parser->pos + 1;
                        break;
                    }
                }
                if (i == -1) return JSMN_ERROR_INVAL;
                for (; i >= 0; i--) {
                    token = &tokens[i];
                    if (token->start != -1 && token->end == -1) {
                        parser->toksuper = i;
                        break;
                    }
                }
                break;
            case '\"':
                r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
                if (r < 0) return r;
                count++;
                if (parser->toksuper != -1 && tokens != ((void *)0))
                    tokens[parser->toksuper].size++;
                break;
            case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
                break;
            default:
                r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
                if (r < 0) return r;
                count++;
                if (parser->toksuper != -1 && tokens != ((void *)0))
                    tokens[parser->toksuper].size++;
                break;
        }
    }
    for (i = parser->toknext - 1; i >= 0; i--) {
        if (tokens[i].start != -1 && tokens[i].end == -1) {
            return JSMN_ERROR_PART;
        }
    }
    return count;
}

canonical_original_code:
enum jsmnerr_t jsmn_parse(struct jsmn_parser *parser, char *js, unsigned long len, struct jsmntok_t *tokens, unsigned int num_tokens) {
    enum jsmnerr_t r;
    int i;
    struct jsmntok_t *token;
    int count = 0;
    for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
        char c;
        enum jsmntype_t type;
        c = js[parser->pos];
        switch (c) {
            case '{': case '[':
                count++;
                if (tokens == ((void *)0)) {
                    break;
                }
                token = jsmn_alloc_token(parser, tokens, num_tokens);
                if (token == ((void *)0))
                    return JSMN_ERROR_NOMEM;
                if (parser->toksuper != -1) {
                    tokens[parser->toksuper].size++;
                }
                token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
                token->start = parser->pos;
                parser->toksuper = parser->toknext - 1;
                break;
            case '}': case ']':
                if (tokens == ((void *)0))
                    break;
                type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
                for (i = parser->toknext - 1; i >= 0; i--) {
                    token = &tokens[i];
                    if (token->start != -1 && token->end == -1) {
                        if (token->type != type) {
                            return JSMN_ERROR_INVAL;
                        }
                        parser->toksuper = -1;
                        token->end = parser->pos + 1;
                        break;
                    }
                }
                if (i == -1) return JSMN_ERROR_INVAL;
                for (; i >= 0; i--) {
                    token = &tokens[i];
                    if (token->start != -1 && token->end == -1) {
                        parser->toksuper = i;
                        break;
                    }
                }
                break;
            case '\"':
                r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
                if (r < 0) return r;
                count++;
                if (parser->toksuper != -1 && tokens != ((void *)0))
                    tokens[parser->toksuper].size++;
                break;
            case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
                break;
            default:
                r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
                if (r < 0) return r;
                count++;
                if (parser->toksuper != -1 && tokens != ((void *)0))
                    tokens[parser->toksuper].size++;
                break;
        }
    }
    for (i = parser->toknext - 1; i >= 0; i--) {
        if (tokens[i].start != -1 && tokens[i].end == -1) {
            return JSMN_ERROR_PART;
        }
    }
    return count;
}

variable_types: enum jsmnerr_t { JSMN_ERROR_NOMEM=-1, JSMN_ERROR_INVAL=-2, JSMN_ERROR_PART=-3 }

variable_types: count int

variable_types: enum jsmntype_t { JSMN_PRIMITIVE=0, JSMN_OBJECT=1, JSMN_ARRAY=2, JSMN_STRING=3 }


decompiled_code:
int post_heating_counter_init()
{
return read_on_time_from_file();
}

canonical_decompiled_code:
int func2()
{
return func0();
}

original_code:
void post_heating_counter_init()
{
    read_on_time_from_file();
}


decompiled_code:
int post_heating_counter_update()
{
int v0; // ebx
int result; // eax
int v2; // eax
float v3; // [rsp+Ch] [rbp-Ch]

v0 = (int)r32_digit_post_heating_on_cnt();
v3 = r32_digit_post_heating_off_cnt();
++g_u32UpdateCallCnt;
result = 3600 * (5 * g_u32UpdateCallCnt / 0xE10u);
if ( 5 * g_u32UpdateCallCnt == result )
result = save_on_time_to_file();
if ( g_bPostHeatingStarted != 1 && (_BYTE)v0 )
{
v2 = 205 * (unsigned __int8)(int)v3;
LOWORD(v2) = (unsigned __int8)(int)v3 / 0xAu;
result = 10 * v2;
if ( (_BYTE)result == (unsigned __int8)(int)v3 )
{
g_bPostHeatingStarted = 1;
result = g_u32OnTimeTotal + 100 - (unsigned __int8)(int)v3;
g_u32OnTimeTotal = result;
}
}
else if ( !(_BYTE)v0 )
{
g_bPostHeatingStarted = 0;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int v0; // ebx
int result; // eax
int v2; // eax
float v3; // [rsp+Ch] [rbp-Ch]

v0 = (int)r32_digit_post_heating_on_cnt();
v3 = r32_digit_post_heating_off_cnt();
++g_u32UpdateCallCnt;
result = 3600 * (5 * g_u32UpdateCallCnt / 0xE10u);
if ( 5 * g_u32UpdateCallCnt == result )
result = func1();
if ( g_bPostHeatingStarted != 1 && (_BYTE)v0 )
{
v2 = 205 * (unsigned __int8)(int)v3;
LOWORD(v2) = (unsigned __int8)(int)v3 / 0xAu;
result = 10 * v2;
if ( (_BYTE)result == (unsigned __int8)(int)v3 )
{
g_bPostHeatingStarted = 1;
result = g_u32OnTimeTotal + 100 - (unsigned __int8)(int)v3;
g_u32OnTimeTotal = result;
}
}
else if ( !(_BYTE)v0 )
{
g_bPostHeatingStarted = 0;
}
return result;
}

original_code:
void post_heating_counter_update()
{
    uint8 u8OnCnt = (uint8) r32_digit_post_heating_on_cnt();
    uint8 u8OffCnt = (uint8) r32_digit_post_heating_off_cnt();
    g_u32UpdateCallCnt++;
    if ( (g_u32UpdateCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
    if ( g_bPostHeatingStarted == 0 &&
         u8OnCnt > 0 &&
         !(u8OffCnt % 10) )
    {
        g_bPostHeatingStarted = 1;
        g_u32OnTimeTotal += (100 - u8OffCnt);
    }
    else if (u8OnCnt == 0)
    {
        g_bPostHeatingStarted = 0;
    }
}

canonical_original_code:
void post_heating_counter_update()
{
    unsigned char u8OnCnt = (uint8) r32_digit_post_heating_on_cnt();
    unsigned char u8OffCnt = (uint8) r32_digit_post_heating_off_cnt();
    g_u32UpdateCallCnt++;
    if ( (g_u32UpdateCallCnt * (5)) %
         (60*60) == 0)
    {
        save_on_time_to_file();
    }
    if ( g_bPostHeatingStarted == 0 &&
         u8OnCnt > 0 &&
         !(u8OffCnt % 10) )
    {
        g_bPostHeatingStarted = 1;
        g_u32OnTimeTotal += (100 - u8OffCnt);
    }
    else if (u8OnCnt == 0)
    {
        g_bPostHeatingStarted = 0;
    }
}


decompiled_code:
__int64 u32_post_heating_counter_get_on_time_total()
{
return (unsigned int)g_u32OnTimeTotal;
}

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

original_code:
uint32 u32_post_heating_counter_get_on_time_total()
{
    return g_u32OnTimeTotal;
}

canonical_original_code:
unsigned int u32_post_heating_counter_get_on_time_total()
{
    return g_u32OnTimeTotal;
}


decompiled_code:
float __fastcall r32_NTC_to_celsius(unsigned __int16 a1)
{
return c_NTC_temperature_conversion_table[a1];
}

canonical_decompiled_code:
float __fastcall func0(unsigned __int16 a1)
{
return c_NTC_temperature_conversion_table[a1];
}

original_code:
real32 r32_NTC_to_celsius(uint16 u16Ntc)
{
    return c_NTC_temperature_conversion_table[u16Ntc];
}

canonical_original_code:
float r32_NTC_to_celsius(unsigned short u16Ntc)
{
    return c_NTC_temperature_conversion_table[u16Ntc];
}


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

result = 0LL;
while ( a1 < c_NTC_temperature_conversion_table[result] || c_NTC_temperature_conversion_table[result + 1] < a1 )
{
if ( ++result == 255 )
return 0LL;
}
return result;
}

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

result = 0LL;
while ( a1 < c_NTC_temperature_conversion_table[result] || c_NTC_temperature_conversion_table[result + 1] < a1 )
{
if ( ++result == 255 )
return 0LL;
}
return result;
}

original_code:
uint16 u16_celsius_to_NTC(real32 r32Celcius)
{
    for (int i = 0; i < 255; i++)
    {
        if (c_NTC_temperature_conversion_table[i] <= r32Celcius &&
            r32Celcius <= c_NTC_temperature_conversion_table[i+1])
        {
            return i;
        }
    }
    return 0;
}

canonical_original_code:
unsigned short u16_celsius_to_NTC(float r32Celcius)
{
    for (int i = 0; i < 255; i++)
    {
        if (c_NTC_temperature_conversion_table[i] <= r32Celcius &&
            r32Celcius <= c_NTC_temperature_conversion_table[i+1])
        {
            return i;
        }
    }
    return 0;
}

return_type: unsigned short


initPilha

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

v2 = malloc(a2);
if ( !v2 )
return 0LL;
*(_DWORD *)a1 = -1;
*(_QWORD *)(a1 + 8) = v2;
*(_DWORD *)(a1 + 16) = a2;
return 1LL;
}

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

v2 = malloc(a2);
if ( !v2 )
return 0LL;
*(_DWORD *)a1 = -1;
*(_QWORD *)(a1 + 8) = v2;
*(_DWORD *)(a1 + 16) = a2;
return 1LL;
}

original_code:
_Bool initPilha(pilha * pil, int tm){
    char * p;
    p = (char *) malloc(tm * sizeof(char));
    if(p == ((void *)0))
        return 0;
    pil->topo = -1;
    pil->p = p;
    pil->tm = tm;
    return 1;
}

canonical_original_code:
_Bool initPilha(struct tPilha *pil, int tm){
    char *p;
    p = (char *) malloc(tm * sizeof(char));
    if(p == ((void *)0))
        return 0;
    pil->topo = -1;
    pil->p = p;
    pil->tm = tm;
    return 1;
}

variable_types: struct tPilha { int topo; char *p; int tm; } *

variable_types: tm int

variable_types:
p
char *


isCheia

decompiled_code:
bool __fastcall isCheia(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 - 1 == a7;
}

canonical_decompiled_code:
bool __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9)
{
return a9 - 1 == a7;
}

original_code:
_Bool isCheia(pilha pil){
    if(pil.topo == pil.tm -1)
        return 1;
    return 0;
}

canonical_original_code:
_Bool isCheia(struct tPilha pil){
    if(pil.topo == pil.tm -1)
        return 1;
    return 0;
}

variable_types: struct tPilha { int topo; char *p; int tm; }


push

original_code:
_Bool push(pilha * pil, char value){
    if(isCheia(*pil))
        return 0;
    pil->p[pil->topo++] = value;
    return 1;
}

canonical_original_code:
_Bool push(struct tPilha *pil, char value){
    if(isCheia(*pil))
        return 0;
    pil->p[pil->topo++] = value;
    return 1;
}

variable_types: value char

user_defined_types: struct tPilha { int topo; char *p; int tm; }


pop

original_code:
_Bool pop(pilha * pil, char * value){
    if(pil->topo == -1)
        return 0;
    * value = pil->p[--pil->topo];
    return 1;
}

canonical_original_code:
_Bool pop(struct tPilha *pil, char *value){
    if(pil->topo == -1)
        return 0;
    * value = pil->p[--pil->topo];
    return 1;
}

variable_types:
value
char *


freePilha

decompiled_code:
__int64 __fastcall freePilha(void *a1)
{
free(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *a1)
{
free(a1);
return 1LL;
}

original_code:
_Bool freePilha(pilha * pil){
    free(pil);
    return 1;
}

canonical_original_code:
_Bool freePilha(struct tPilha *pil){
    free(pil);
    return 1;
}


main

original_code:
int main(){
    char strPadrao1[] = "ana maria";
    string str1, str2, str3;
    initString(&str1);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str1.inicio, str1.ultimo, str1.len);
    initString(&str2);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str2.inicio, str2.ultimo, str2.len);
    initString(&str3);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str3.inicio, str3.ultimo, str3.len);
    printf("\n\n<<<-----------createString---------------->>>\n");
    createString(&str1, strPadrao1);
    printStringFrente(str1);
    printf("\n\n<<<-----------copyString---------------->>>\n");
    copyString(&str1, &str2);
    printStringFrente(str2);
    printf("\n\n<<<-----------equalString---------------->>>\n");
    if(equalString(str1, str2))
        printf("String 1 igual a String 2");
    else
        printf("Strings diferentes ");
    printf("\n\n<<<-----------lenPalavras---------------->>>\n");
    printf("QTD: [%d] ",lenPalavras(str1));
    printf("\n\n<<<-----------concat--------------------->>>\n");
    concat(str1, str2, &str3);
    printStringFrente(str3);
    printf("\n\n<<<-----------inverterLetras------------->>>\n");
    inverterLetras(&str1);
    printStringFrente(str1);
    printf("\n\n<<<----------inverterPalavras------------>>>\n");
    inverterPalavras(&str1);
    printStringFrente(str1);
    printf("\n\n<<<--------inverterLetrasPalavras-------->>>\n");
    inverterLetrasPalavras(&str1);
    printStringFrente(str1);
    createString(&str1, "introducao a logica matematica");
    createString(&str2, "estruturas de dados");
    printf("\n\n<<<--------intercarlarPalavras----------->>>\n");
    printf("String 1 ....: ");
    printStringFrente(str1);
    printf("\nString 2 ....: ");
    printStringFrente(str2);
    printf("\n");
    intercarlarPalavras(str1, str2, &str3);
    printf("\n");
    printStringFrente(str3);
    printf("\n");
    return 0;
}

canonical_original_code:
int main(){
    char strPadrao1[] = "ana maria";
    struct tString str1; struct tString str2; struct tString str3;
    initString(&str1);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str1.inicio, str1.ultimo, str1.len);
    initString(&str2);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str2.inicio, str2.ultimo, str2.len);
    initString(&str3);
    printf("INIT: inicio[%p] Fim[%p] len[%d]\n", str3.inicio, str3.ultimo, str3.len);
    printf("\n\n<<<-----------createString---------------->>>\n");
    createString(&str1, strPadrao1);
    printStringFrente(str1);
    printf("\n\n<<<-----------copyString---------------->>>\n");
    copyString(&str1, &str2);
    printStringFrente(str2);
    printf("\n\n<<<-----------equalString---------------->>>\n");
    if(equalString(str1, str2))
        printf("String 1 igual a String 2");
    else
        printf("Strings diferentes ");
    printf("\n\n<<<-----------lenPalavras---------------->>>\n");
    printf("QTD: [%d] ",lenPalavras(str1));
    printf("\n\n<<<-----------concat--------------------->>>\n");
    concat(str1, str2, &str3);
    printStringFrente(str3);
    printf("\n\n<<<-----------inverterLetras------------->>>\n");
    inverterLetras(&str1);
    printStringFrente(str1);
    printf("\n\n<<<----------inverterPalavras------------>>>\n");
    inverterPalavras(&str1);
    printStringFrente(str1);
    printf("\n\n<<<--------inverterLetrasPalavras-------->>>\n");
    inverterLetrasPalavras(&str1);
    printStringFrente(str1);
    createString(&str1, "introducao a logica matematica");
    createString(&str2, "estruturas de dados");
    printf("\n\n<<<--------intercarlarPalavras----------->>>\n");
    printf("String 1 ....: ");
    printStringFrente(str1);
    printf("\nString 2 ....: ");
    printStringFrente(str2);
    printf("\n");
    intercarlarPalavras(str1, str2, &str3);
    printf("\n");
    printStringFrente(str3);
    printf("\n");
    return 0;
}

variable_types: struct tString { struct tCaracter { char value; struct tCaracter *prox; struct tCaracter *ante; } *inicio; struct tCaracter *ultimo; int len; }

user_defined_types: struct tString { struct tCaracter *inicio; struct tCaracter *ultimo; int len; }


initString

decompiled_code:
__int64 __fastcall initString(__int64 a1)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 16) = 0;
return 1LL;
}

original_code:
_Bool initString(string * str){
    str->inicio = ((void *)0);
    str->ultimo = ((void *)0);
    str->len = 0;
    return 1;
}

canonical_original_code:
_Bool initString(struct tString *str){
    str->inicio = ((void *)0);
    str->ultimo = ((void *)0);
    str->len = 0;
    return 1;
}

variable_types: struct tString { struct tCaracter { char value; struct tCaracter *prox; struct tCaracter *ante; } *inicio; struct tCaracter *ultimo; int len; } *


equalString

original_code:
_Bool equalString(string str1, string str2){
    caracter * pos1, * pos2;
    if(str1.len != str2.len)
        return 0;
    pos1 = str1.inicio;
    pos2 = str2.inicio;
    while(pos1 != ((void *)0) && pos2 != ((void *)0)){
        if(pos1->value != pos2->value)
            return 0;
        pos1 = pos1->prox;
        pos2 = pos2->prox;
    }
    return 1;
}

canonical_original_code:
_Bool equalString(struct tString str1, struct tString str2){
    struct tCaracter *pos1; struct tCaracter *pos2;
    if(str1.len != str2.len)
        return 0;
    pos1 = str1.inicio;
    pos2 = str2.inicio;
    while(pos1 != ((void *)0) && pos2 != ((void *)0)){
        if(pos1->value != pos2->value)
            return 0;
        pos1 = pos1->prox;
        pos2 = pos2->prox;
    }
    return 1;
}

variable_types: struct tCaracter { char value; struct tCaracter *prox; struct tCaracter *ante; } *


lenPalavras

original_code:
int lenPalavras(string str){
    caracter * pos;
    int len = 1;
    if(str.len == 0)
        return 0;
    pos = str.inicio;
    while(pos != ((void *)0)){
        if(pos->value == ' ')
            len++;
        pos = pos->prox;
    }
    return len;
}

canonical_original_code:
int lenPalavras(struct tString str){
    struct tCaracter *pos;
    int len = 1;
    if(str.len == 0)
        return 0;
    pos = str.inicio;
    while(pos != ((void *)0)){
        if(pos->value == ' ')
            len++;
        pos = pos->prox;
    }
    return len;
}

variable_types: len int

user_defined_types: struct tCaracter { char value; struct tCaracter *prox; struct tCaracter *ante; }


inserirChar

original_code:
_Bool inserirChar(string *str, char value){
    caracter * novo = (caracter *) malloc(sizeof(caracter));
    novo->value = value;
    novo->ante = str->ultimo;
    novo->prox = ((void *)0);
    if(novo == ((void *)0))
        return 0;
    if(str->inicio == ((void *)0)){
        str->inicio = novo;
    }else{
        str->ultimo->prox = novo;
    }
    str->ultimo = novo;
    str->len++;
    return 1;
}

canonical_original_code:
_Bool inserirChar(struct tString *str, char value){
    struct tCaracter *novo = (caracter *) malloc(sizeof(caracter));
    novo->value = value;
    novo->ante = str->ultimo;
    novo->prox = ((void *)0);
    if(novo == ((void *)0))
        return 0;
    if(str->inicio == ((void *)0)){
        str->inicio = novo;
    }else{
        str->ultimo->prox = novo;
    }
    str->ultimo = novo;
    str->len++;
    return 1;
}


copyString

decompiled_code:
__int64 __fastcall copyString(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax

v2 = *a1;
if ( !*a1 )
return 1LL;
do
{
result = inserirChar(a2, *(_BYTE *)v2);
if ( !(_BYTE)result )
break;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax

v2 = *a1;
if ( !*a1 )
return 1LL;
do
{
result = func3(a2, *(_BYTE *)v2);
if ( !(_BYTE)result )
break;
v2 = *(_QWORD *)(v2 + 8);
}
while ( v2 );
return result;
}

original_code:
_Bool copyString(string * str1, string * str2){
    caracter * pos;
    pos = str1->inicio;
    while(pos != ((void *)0)){
        if(!inserirChar(str2, pos->value))
            return 0;
        pos = pos->prox;
    }
    return 1;
}

canonical_original_code:
_Bool copyString(struct tString *str1, struct tString *str2){
    struct tCaracter *pos;
    pos = str1->inicio;
    while(pos != ((void *)0)){
        if(!inserirChar(str2, pos->value))
            return 0;
        pos = pos->prox;
    }
    return 1;
}


concat

decompiled_code:
__int64 __fastcall concat(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, int a9, __int64 a10)
{
__int64 result; // rax

result = copyString(&a7, a1);
if ( (_BYTE)result )
result = copyString(&a10, a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, int a9, __int64 a10)
{
__int64 result; // rax

result = func4(&a7, a1);
if ( (_BYTE)result )
result = func4(&a10, a1);
return result;
}

original_code:
_Bool concat(string str1, string str2, string * novaString){
    if(!copyString(&str1, novaString))
        return 0;
    if(!copyString(&str2, novaString))
        return 0;
    return 1;
}

canonical_original_code:
_Bool concat(struct tString str1, struct tString str2, struct tString *novaString){
    if(!copyString(&str1, novaString))
        return 0;
    if(!copyString(&str2, novaString))
        return 0;
    return 1;
}

variable_types:
novaString
struct tString *


intercarlarPalavras

decompiled_code:
__int64 __fastcall intercarlarPalavras(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, int a8, int a9, __int64 a10)
{
unsigned int v10; // er13
__int64 v12; // rbp
unsigned int v14; // eax
unsigned int v15; // eax
__int64 v16[9]; // [rsp+0h] [rbp-48h] BYREF

v10 = initString((__int64)v16);
if ( (_BYTE)v10 )
{
v10 = initString(a1);
if ( (_BYTE)v10 )
{
v12 = a7;
if ( a7 )
{
while ( 1 )
{
if ( *(_BYTE *)v12 == 32 )
{
inserirChar((__int64)v16, 32);
copyString(v16, a1);
v14 = initString((__int64)v16);
if ( !(_BYTE)v14 )
return v14;
if ( a10 )
{
while ( *(_BYTE *)a10 != 32 )
{
inserirChar((__int64)v16, *(_BYTE *)a10);
a10 = *(_QWORD *)(a10 + 8);
if ( !a10 )
goto LABEL_7;
}
copyString(v16, a1);
inserirChar(a1, 32);
v15 = initString((__int64)v16);
if ( !(_BYTE)v15 )
return v15;
a10 = *(_QWORD *)(a10 + 8);
}
}
else
{
inserirChar((__int64)v16, *(_BYTE *)v12);
}
LABEL_7:
v12 = *(_QWORD *)(v12 + 8);
if ( !v12 )
{
copyString(v16, a1);
break;
}
}
}
while ( a10 )
{
inserirChar(a1, *(_BYTE *)a10);
a10 = *(_QWORD *)(a10 + 8);
}
}
}
return v10;
}

original_code:
_Bool intercarlarPalavras(string str1, string str2, string * novaString){
    caracter * pos, * pos2, * aux;
    string strAux;
    _Bool palavraInserida = 0;
    if(!initString(&strAux))
        return 0;
    if(!initString(novaString))
        return 0;
    pos = str1.inicio;
    pos2 = str2.inicio;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            inserirChar(&strAux, ' ');
            copyString(&strAux, novaString);
            palavraInserida = 0;
            if(!initString(&strAux))
                return 0;
            while(pos2 != ((void *)0) && !palavraInserida){
                if(pos2->value != ' '){
                    inserirChar(&strAux, pos2->value);
                }else{
                    copyString(&strAux, novaString);
                    inserirChar(novaString, ' ');
                    if(!initString(&strAux))
                        return 0;
                    palavraInserida = 1;
                }
                pos2 = pos2->prox;
            }
        }
        aux = pos;
        pos = pos->prox;
        if(pos == ((void *)0))
            copyString(&strAux, novaString);
    }
    while(pos2 != ((void *)0)){
        inserirChar(novaString, pos2->value);
        pos2 = pos2->prox;
    }
    return 1;
}

canonical_original_code:
_Bool intercarlarPalavras(struct tString str1, struct tString str2, struct tString *novaString){
    struct tCaracter *pos; struct tCaracter *pos2; struct tCaracter *aux;
    struct tString strAux;
    _Bool palavraInserida = 0;
    if(!initString(&strAux))
        return 0;
    if(!initString(novaString))
        return 0;
    pos = str1.inicio;
    pos2 = str2.inicio;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            inserirChar(&strAux, ' ');
            copyString(&strAux, novaString);
            palavraInserida = 0;
            if(!initString(&strAux))
                return 0;
            while(pos2 != ((void *)0) && !palavraInserida){
                if(pos2->value != ' '){
                    inserirChar(&strAux, pos2->value);
                }else{
                    copyString(&strAux, novaString);
                    inserirChar(novaString, ' ');
                    if(!initString(&strAux))
                        return 0;
                    palavraInserida = 1;
                }
                pos2 = pos2->prox;
            }
        }
        aux = pos;
        pos = pos->prox;
        if(pos == ((void *)0))
            copyString(&strAux, novaString);
    }
    while(pos2 != ((void *)0)){
        inserirChar(novaString, pos2->value);
        pos2 = pos2->prox;
    }
    return 1;
}


clearString

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

v1 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
free(v2);
}
while ( v1 );
}
return 1LL;
}

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

v1 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
v2 = v1;
v1 = (_QWORD *)v1[1];
free(v2);
}
while ( v1 );
}
return 1LL;
}

original_code:
_Bool clearString(string * str){
    caracter * pos, * aux;
    pos = str->inicio;
    while(pos != ((void *)0)){
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    return 1;
}

canonical_original_code:
_Bool clearString(struct tString *str){
    struct tCaracter *pos; struct tCaracter *aux;
    pos = str->inicio;
    while(pos != ((void *)0)){
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    return 1;
}


createString

decompiled_code:
__int64 __fastcall createString(_QWORD *a1, char *a2)
{
char v3; // si
char *v4; // rbx
__int64 result; // rax

clearString(a1);
initString((__int64)a1);
v3 = *a2;
if ( !v3 )
return 1LL;
v4 = a2 + 1;
do
{
result = inserirChar((__int64)a1, v3);
if ( !(_BYTE)result )
break;
v3 = *v4++;
}
while ( v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, char *a2)
{
char v3; // si
char *v4; // rbx
__int64 result; // rax

func7(a1);
func0((__int64)a1);
v3 = *a2;
if ( !v3 )
return 1LL;
v4 = a2 + 1;
do
{
result = func3((__int64)a1, v3);
if ( !(_BYTE)result )
break;
v3 = *v4++;
}
while ( v3 );
return result;
}

original_code:
_Bool createString(string * str, const char* strPadrao){
    clearString(str);
    initString(str);
    int len;
    for(len = 0; strPadrao[len] != 0; len++){
        if(!inserirChar(str, strPadrao[len]))
            return 0;
    }
    return 1;
}

canonical_original_code:
_Bool createString(struct tString *str, char *strPadrao){
    clearString(str);
    initString(str);
    int len;
    for(len = 0; strPadrao[len] != 0; len++){
        if(!inserirChar(str, strPadrao[len]))
            return 0;
    }
    return 1;
}

variable_types:
strPadrao
char *


inserirStringEmPilha

decompiled_code:
__int64 __fastcall inserirStringEmPilha(__int64 a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
_QWORD *v7; // rbp
unsigned int v8; // ebx
_QWORD *v9; // r12

v7 = ptr;
if ( !ptr )
return 1;
while ( 1 )
{
v8 = push(a1, (unsigned int)*(char *)v7);
if ( !(_BYTE)v8 )
break;
v9 = (_QWORD *)v7[1];
free(v7);
if ( !v9 )
break;
v7 = v9;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, int a3, int a4, int a5, int a6, void *ptr)
{
_QWORD *v7; // rbp
unsigned int v8; // ebx
_QWORD *v9; // r12

v7 = ptr;
if ( !ptr )
return 1;
while ( 1 )
{
v8 = push(a1, (unsigned int)*(char *)v7);
if ( !(_BYTE)v8 )
break;
v9 = (_QWORD *)v7[1];
free(v7);
if ( !v9 )
break;
v7 = v9;
}
return v8;
}

original_code:
_Bool inserirStringEmPilha(string str, pilha * pil){
    caracter * pos, * aux;
    pos = str.inicio;
    while(pos != ((void *)0)){
        if(!push(pil, pos->value))
            return 0;
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    return 1;
}

canonical_original_code:
_Bool inserirStringEmPilha(struct tString str, struct tPilha *pil){
    struct tCaracter *pos; struct tCaracter *aux;
    pos = str.inicio;
    while(pos != ((void *)0)){
        if(!push(pil, pos->value))
            return 0;
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    return 1;
}


inserirPilhaEmString

decompiled_code:
__int64 __fastcall inserirPilhaEmString(__int64 a1, __int64 a2)
{
__int64 result; // rax
char v3[25]; // [rsp+Fh] [rbp-19h] BYREF

while ( (unsigned __int8)pop(a1, v3) )
{
result = inserirChar(a2, v3[0]);
if ( !(_BYTE)result )
return result;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
__int64 result; // rax
char v3[25]; // [rsp+Fh] [rbp-19h] BYREF

while ( (unsigned __int8)pop(a1, v3) )
{
result = func3(a2, v3[0]);
if ( !(_BYTE)result )
return result;
}
return 1LL;
}

original_code:
_Bool inserirPilhaEmString(pilha * pil, string * str){
    char ch;
    while(pop(pil, &ch)){
        if(!inserirChar(str, ch))
            return 0;
    }
    return 1;
}

canonical_original_code:
_Bool inserirPilhaEmString(struct tPilha *pil, struct tString *str){
    char ch;
    while(pop(pil, &ch)){
        if(!inserirChar(str, ch))
            return 0;
    }
    return 1;
}

variable_types: ch char


inverterLetras

decompiled_code:
__int64 __fastcall inverterLetras(__int64 a1)
{
char *v2; // rbx
void *v3; // rdi
_BYTE v5[56]; // [rsp+0h] [rbp-38h] BYREF

initPilha(v5, *(unsigned int *)(a1 + 16));
v2 = *(char **)a1;
if ( *(_QWORD *)a1 )
{
do
{
push(v5, (unsigned int)*v2);
v3 = v2;
v2 = (char *)*((_QWORD *)v2 + 1);
free(v3);
}
while ( v2 );
}
initString(a1);
inserirPilhaEmString((__int64)v5, a1);
freePilha(v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
char *v2; // rbx
void *v3; // rdi
_BYTE v5[56]; // [rsp+0h] [rbp-38h] BYREF

initPilha(v5, *(unsigned int *)(a1 + 16));
v2 = *(char **)a1;
if ( *(_QWORD *)a1 )
{
do
{
push(v5, (unsigned int)*v2);
v3 = v2;
v2 = (char *)*((_QWORD *)v2 + 1);
free(v3);
}
while ( v2 );
}
func0(a1);
func10((__int64)v5, a1);
freePilha(v5);
return 1LL;
}

original_code:
_Bool inverterLetras(string * str){
    caracter * pos, * aux;
    pilha pil;
    initPilha(&pil, str->len);
    pos = str->inicio;
    while(pos != ((void *)0)){
        push(&pil, pos->value);
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    initString(str);
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    return 1;
}

canonical_original_code:
_Bool inverterLetras(struct tString *str){
    struct tCaracter *pos; struct tCaracter *aux;
    struct tPilha pil;
    initPilha(&pil, str->len);
    pos = str->inicio;
    while(pos != ((void *)0)){
        push(&pil, pos->value);
        aux = pos;
        pos = pos->prox;
        free(aux);
    }
    initString(str);
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    return 1;
}


inverterPalavras

decompiled_code:
__int64 __fastcall inverterPalavras(__int64 a1)
{
_QWORD *v1; // r12
unsigned int v2; // ebx
int v4; // esi
_QWORD *v5; // rbx
int v6; // edx
int v7; // ecx
int v8; // er8
int v9; // er9
int v10; // edx
int v11; // ecx
int v12; // er8
int v13; // er9
void *ptr; // [rsp+0h] [rbp-58h] BYREF
int v15[14]; // [rsp+20h] [rbp-38h] BYREF

v1 = *(_QWORD **)a1;
v2 = initString((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
v2 = initPilha(v15, *(unsigned int *)(a1 + 16));
if ( !(_BYTE)v2 )
return v2;
if ( !v1 )
goto LABEL_15;
while ( 1 )
{
LOBYTE(v4) = *(_BYTE *)v1;
if ( *(_BYTE *)v1 == 32 )
{
v2 = inverterLetras((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
v4 = 32;
inserirChar((__int64)&ptr, 32);
inserirStringEmPilha((__int64)v15, 32, v6, v7, v8, v9, ptr);
v2 = initString((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
}
else
{
v4 = (char)v4;
inserirChar((__int64)&ptr, v4);
}
v5 = (_QWORD *)v1[1];
free(v1);
if ( !v5 )
break;
v1 = v5;
}
v2 = inverterLetras((__int64)&ptr);
if ( (_BYTE)v2 )
{
inserirStringEmPilha((__int64)v15, v4, v10, v11, v12, v13, ptr);
LABEL_15:
v2 = initString(a1);
if ( (_BYTE)v2 )
{
inserirPilhaEmString((__int64)v15, a1);
freePilha(v15);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
_QWORD *v1; // r12
unsigned int v2; // ebx
int v4; // esi
_QWORD *v5; // rbx
int v6; // edx
int v7; // ecx
int v8; // er8
int v9; // er9
int v10; // edx
int v11; // ecx
int v12; // er8
int v13; // er9
void *ptr; // [rsp+0h] [rbp-58h] BYREF
int v15[14]; // [rsp+20h] [rbp-38h] BYREF

v1 = *(_QWORD **)a1;
v2 = func0((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
v2 = initPilha(v15, *(unsigned int *)(a1 + 16));
if ( !(_BYTE)v2 )
return v2;
if ( !v1 )
goto LABEL_15;
while ( 1 )
{
LOBYTE(v4) = *(_BYTE *)v1;
if ( *(_BYTE *)v1 == 32 )
{
v2 = func11((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
v4 = 32;
func3((__int64)&ptr, 32);
func9((__int64)v15, 32, v6, v7, v8, v9, ptr);
v2 = func0((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
}
else
{
v4 = (char)v4;
func3((__int64)&ptr, v4);
}
v5 = (_QWORD *)v1[1];
free(v1);
if ( !v5 )
break;
v1 = v5;
}
v2 = func11((__int64)&ptr);
if ( (_BYTE)v2 )
{
func9((__int64)v15, v4, v10, v11, v12, v13, ptr);
LABEL_15:
v2 = func0(a1);
if ( (_BYTE)v2 )
{
func10((__int64)v15, a1);
freePilha(v15);
}
}
return v2;
}

original_code:
_Bool inverterPalavras(string * str){
    pilha pil;
    caracter * pos, * aux;
    string strAux;
    pos = str->inicio;
    if(!initString(&strAux))
        return 0;
    if(!initPilha(&pil, str->len))
        return 0;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            if(!inverterLetras(&strAux))
                return 0;
            inserirChar(&strAux, ' ');
            inserirStringEmPilha(strAux, &pil);
            if(!initString(&strAux))
                return 0;
        }
        aux = pos;
        pos = pos->prox;
        free(aux);
        if(pos == ((void *)0)){
            if(!inverterLetras(&strAux))
                return 0;
            inserirStringEmPilha(strAux, &pil);
        }
    }
    if(!initString(str))
        return 0;
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    return 1;
}

canonical_original_code:
_Bool inverterPalavras(struct tString *str){
    struct tPilha pil;
    struct tCaracter *pos; struct tCaracter *aux;
    struct tString strAux;
    pos = str->inicio;
    if(!initString(&strAux))
        return 0;
    if(!initPilha(&pil, str->len))
        return 0;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            if(!inverterLetras(&strAux))
                return 0;
            inserirChar(&strAux, ' ');
            inserirStringEmPilha(strAux, &pil);
            if(!initString(&strAux))
                return 0;
        }
        aux = pos;
        pos = pos->prox;
        free(aux);
        if(pos == ((void *)0)){
            if(!inverterLetras(&strAux))
                return 0;
            inserirStringEmPilha(strAux, &pil);
        }
    }
    if(!initString(str))
        return 0;
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    return 1;
}


inverterLetrasPalavras

decompiled_code:
__int64 __fastcall inverterLetrasPalavras(__int64 a1)
{
_QWORD *v1; // rbx
unsigned int v2; // ebp
int v4; // esi
_QWORD *v5; // rbp
int v6; // edx
int v7; // ecx
int v8; // er8
int v9; // er9
int v10; // edx
int v11; // ecx
int v12; // er8
int v13; // er9
void *ptr; // [rsp+0h] [rbp-58h] BYREF
int v15[14]; // [rsp+20h] [rbp-38h] BYREF

v1 = *(_QWORD **)a1;
v2 = initString((__int64)&ptr);
if ( (_BYTE)v2 )
{
v2 = initPilha(v15, *(unsigned int *)(a1 + 16));
if ( (_BYTE)v2 )
{
if ( v1 )
{
while ( 1 )
{
LOBYTE(v4) = *(_BYTE *)v1;
if ( *(_BYTE *)v1 == 32 )
{
v4 = 32;
inserirChar((__int64)&ptr, 32);
inserirStringEmPilha((__int64)v15, 32, v10, v11, v12, v13, ptr);
v2 = initString((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
}
else
{
v4 = (char)v4;
inserirChar((__int64)&ptr, v4);
}
v5 = (_QWORD *)v1[1];
free(v1);
if ( !v5 )
break;
v1 = v5;
}
inserirStringEmPilha((__int64)v15, v4, v6, v7, v8, v9, ptr);
}
v2 = initString(a1);
if ( (_BYTE)v2 )
{
inserirPilhaEmString((__int64)v15, a1);
freePilha(v15);
inverterPalavras(a1);
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
_QWORD *v1; // rbx
unsigned int v2; // ebp
int v4; // esi
_QWORD *v5; // rbp
int v6; // edx
int v7; // ecx
int v8; // er8
int v9; // er9
int v10; // edx
int v11; // ecx
int v12; // er8
int v13; // er9
void *ptr; // [rsp+0h] [rbp-58h] BYREF
int v15[14]; // [rsp+20h] [rbp-38h] BYREF

v1 = *(_QWORD **)a1;
v2 = func0((__int64)&ptr);
if ( (_BYTE)v2 )
{
v2 = initPilha(v15, *(unsigned int *)(a1 + 16));
if ( (_BYTE)v2 )
{
if ( v1 )
{
while ( 1 )
{
LOBYTE(v4) = *(_BYTE *)v1;
if ( *(_BYTE *)v1 == 32 )
{
v4 = 32;
func3((__int64)&ptr, 32);
func9((__int64)v15, 32, v10, v11, v12, v13, ptr);
v2 = func0((__int64)&ptr);
if ( !(_BYTE)v2 )
return v2;
}
else
{
v4 = (char)v4;
func3((__int64)&ptr, v4);
}
v5 = (_QWORD *)v1[1];
free(v1);
if ( !v5 )
break;
v1 = v5;
}
func9((__int64)v15, v4, v6, v7, v8, v9, ptr);
}
v2 = func0(a1);
if ( (_BYTE)v2 )
{
func10((__int64)v15, a1);
freePilha(v15);
func12(a1);
}
}
}
return v2;
}

original_code:
_Bool inverterLetrasPalavras(string * str){
    pilha pil;
    caracter * pos, * aux;
    string strAux;
    pos = str->inicio;
    if(!initString(&strAux))
        return 0;
    if(!initPilha(&pil, str->len))
        return 0;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            inserirChar(&strAux, ' ');
            inserirStringEmPilha(strAux, &pil);
            if(!initString(&strAux))
                return 0;
        }
        aux = pos;
        pos = pos->prox;
        free(aux);
        if(pos == ((void *)0))
            inserirStringEmPilha(strAux, &pil);
    }
    if(!initString(str))
        return 0;
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    inverterPalavras(str);
    return 1;
}

canonical_original_code:
_Bool inverterLetrasPalavras(struct tString *str){
    struct tPilha pil;
    struct tCaracter *pos; struct tCaracter *aux;
    struct tString strAux;
    pos = str->inicio;
    if(!initString(&strAux))
        return 0;
    if(!initPilha(&pil, str->len))
        return 0;
    while(pos != ((void *)0)){
        if(pos->value != ' '){
            inserirChar(&strAux, pos->value);
        }else{
            inserirChar(&strAux, ' ');
            inserirStringEmPilha(strAux, &pil);
            if(!initString(&strAux))
                return 0;
        }
        aux = pos;
        pos = pos->prox;
        free(aux);
        if(pos == ((void *)0))
            inserirStringEmPilha(strAux, &pil);
    }
    if(!initString(str))
        return 0;
    inserirPilhaEmString(&pil, str);
    freePilha(&pil);
    inverterPalavras(str);
    return 1;
}


printStringFrente

decompiled_code:
int __fastcall printStringFrente(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char *a7)
{
char *i; // rbx
int result; // eax

for ( i = a7; i; i = (char *)*((_QWORD *)i + 1) )
result = putchar(*i);
return result;
}

canonical_decompiled_code:
int __fastcall func14(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char *a7)
{
char *i; // rbx
int result; // eax

for ( i = a7; i; i = (char *)*((_QWORD *)i + 1) )
result = putchar(*i);
return result;
}

original_code:
void printStringFrente(string str){
    caracter * pos;
    pos = str.inicio;
    while(pos != ((void *)0)){
        printf("%c", pos->value);
        pos = pos->prox;
    }
}

canonical_original_code:
void printStringFrente(struct tString str){
    struct tCaracter *pos;
    pos = str.inicio;
    while(pos != ((void *)0)){
        printf("%c", pos->value);
        pos = pos->prox;
    }
}


printStringTras

decompiled_code:
int __fastcall printStringTras(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, char *a8)
{
char *i; // rbx
int result; // eax

for ( i = a8; i; i = (char *)*((_QWORD *)i + 2) )
result = putchar(*i);
return result;
}

canonical_decompiled_code:
int __fastcall func15(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, char *a8)
{
char *i; // rbx
int result; // eax

for ( i = a8; i; i = (char *)*((_QWORD *)i + 2) )
result = putchar(*i);
return result;
}

original_code:
void printStringTras(string str){
    caracter * pos;
    pos = str.ultimo;
    while(pos != ((void *)0)){
        printf("%c", pos->value);
        pos = pos->ante;
    }
}

canonical_original_code:
void printStringTras(struct tString str){
    struct tCaracter *pos;
    pos = str.ultimo;
    while(pos != ((void *)0)){
        printf("%c", pos->value);
        pos = pos->ante;
    }
}


repo: peete-q/lua-stream

decompiled_code:
void __fastcall buffer_delete(void **a1)
{
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func0(void **a1)
{
free(*a1);
*a1 = 0LL;
}

original_code:
void buffer_delete(buffer_t *self)
{
 free((*self));
 (*self) = ((void *)0);
}

canonical_original_code:
void buffer_delete(struct buffer_ **self)
{
 free((*self));
 (*self) = ((void *)0);
}

variable_types: struct buffer_ { unsigned long pos; unsigned long size; char ptr[]; } **


decompiled_code:
void *__fastcall buffer_read(__int64 *a1, __int64 a2, void *a3, size_t a4)
{
__int64 v6; // rax
size_t v7; // rdx

v6 = *a1;
v7 = *(_QWORD *)(v6 + 8) - a2;
if ( v7 > a4 )
v7 = a4;
return memcpy(a3, (const void *)(v6 + a2 + 16), v7);
}

canonical_decompiled_code:
void *__fastcall func3(__int64 *a1, __int64 a2, void *a3, size_t a4)
{
__int64 v6; // rax
size_t v7; // rdx

v6 = *a1;
v7 = *(_QWORD *)(v6 + 8) - a2;
if ( v7 > a4 )
v7 = a4;
return memcpy(a3, (const void *)(v6 + a2 + 16), v7);
}

original_code:
void buffer_read(buffer_t *self, size_t pos, void* data, size_t size)
{
 size_t read = (((*self)->size - pos) < (size) ? (*self)->size - pos : size);
 memcpy(data, (*self)->ptr + pos, read);
}

canonical_original_code:
void buffer_read(struct buffer_ **self, unsigned long pos, void *data, unsigned long size)
{
 unsigned long read = (((*self)->size - pos) < (size) ? (*self)->size - pos : size);
 memcpy(data, (*self)->ptr + pos, read);
}

variable_types:
pos
unsigned long

variable_types:
data
void *

variable_types:
size
unsigned long

variable_types:
read
unsigned long

user_defined_types: struct buffer_ { unsigned long pos; unsigned long size; char ptr[]; }


decompiled_code:
__int64 __fastcall buffer_tell(__int64 a1)
{
return **(_QWORD **)a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return **(_QWORD **)a1;
}

original_code:
size_t buffer_tell(buffer_t *self)
{
 return (*self)->pos;
}

canonical_original_code:
unsigned long buffer_tell(struct buffer_ **self)
{
 return (*self)->pos;
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall buffer_size(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + 8LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + 8LL);
}

original_code:
size_t buffer_size(buffer_t *self)
{
 return (*self)->size;
}

canonical_original_code:
unsigned long buffer_size(struct buffer_ **self)
{
 return (*self)->size;
}


decompiled_code:
__int64 __fastcall buffer_ptr(_QWORD *a1)
{
return *a1 + 16LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
return *a1 + 16LL;
}

original_code:
char *buffer_ptr(buffer_t *self)
{
 return (*self)->ptr;
}

canonical_original_code:
char *buffer_ptr(struct buffer_ **self)
{
 return (*self)->ptr;
}

return_type: char *


decompiled_code:
__int64 __fastcall buffer_at(unsigned __int64 **a1, unsigned __int64 a2)
{
if ( **a1 <= a2 )
_assert_fail("pos < _self->pos", "buffer.c", 0x71u, "buffer_at");
return (__int64)*a1 + a2 + 16;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int64 **a1, unsigned __int64 a2)
{
if ( **a1 <= a2 )
_assert_fail("pos < _self->pos", "buffer.c", 0x71u, "buffer_at");
return (__int64)*a1 + a2 + 16;
}

original_code:
char *buffer_at(buffer_t *self, size_t pos)
{
 ((void) sizeof ((pos < (*self)->pos) ? 1 : 0), __extension__ ({ if (pos < (*self)->pos) ; else __assert_fail ("pos < _self->pos", "buffer.c", 113, __extension__ __PRETTY_FUNCTION__); }));
 return (*self)->ptr + pos;
}

canonical_original_code:
char *buffer_at(struct buffer_ **self, unsigned long pos)
{
 ((void) sizeof ((pos < (*self)->pos) ? 1 : 0), __extension__ ({ if (pos < (*self)->pos) ; else __assert_fail ("pos < _self->pos", "buffer.c", 113, __extension__ __PRETTY_FUNCTION__); }));
 return (*self)->ptr + pos;
}


setBank

repo: peffis/xmerlin

decompiled_code:
void __fastcall setBank(int a1)
{
currentBank = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
currentBank = a1;
}

original_code:
void
setBank( BANK b )
{
    currentBank = b;
}

canonical_original_code:
void setBank(enum BANK b)
{
    currentBank = b;
}

variable_types: enum BANK { NATURAL=0, NUMERAL=1, EXTENDED=2 }


getBank

decompiled_code:
__int64 getBank()
{
return (unsigned int)currentBank;
}

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

original_code:
BANK
getBank()
{
    return currentBank;
}

canonical_original_code:
enum BANK getBank()
{
    return currentBank;
}

return_type: enum BANK { NATURAL=0, NUMERAL=1, EXTENDED=2 }


initModels

decompiled_code:
__int64 initModels()
{
__int64 v0; // rbx
_QWORD *v1; // rax
_QWORD *v2; // rdx

v0 = 0LL;
while ( 1 )
{
v1 = malloc(0x640uLL);
models[v0] = (__int64)v1;
if ( !v1 )
break;
v2 = v1 + 200;
do
*v1++ = 0LL;
while ( v1 != v2 );
npatterns[v0++] = 0;
if ( v0 == 3 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rbx
_QWORD *v1; // rax
_QWORD *v2; // rdx

v0 = 0LL;
while ( 1 )
{
v1 = malloc(0x640uLL);
models[v0] = (__int64)v1;
if ( !v1 )
break;
v2 = v1 + 200;
do
*v1++ = 0LL;
while ( v1 != v2 );
npatterns[v0++] = 0;
if ( v0 == 3 )
return 0LL;
}
return 1LL;
}

original_code:
int
initModels()
{
    int i, j;
    for ( i = 0; i < 3; i++ )
    {
 models[i] = (Pattern **) malloc( 200 * sizeof(Pattern *));
 if ( models[i] == ((void *)0) )
 {
     for ( j = 0; j < i; j++)
  free( models[i] );
     return 1;
 }
 for ( j = 0; j < 200; j++ )
     models[i][j] = ((void *)0);
 npatterns[i] = 0;
    }
    return 0;
}

canonical_original_code:
int initModels()
{
    int i; int j;
    for ( i = 0; i < 3; i++ )
    {
 models[i] = (struct pattern **) malloc( 200 * sizeof(struct pattern *));
 if ( models[i] == ((void *)0) )
 {
     for ( j = 0; j < i; j++)
  free( models[i] );
     return 1;
 }
 for ( j = 0; j < 200; j++ )
     models[i][j] = ((void *)0);
 npatterns[i] = 0;
    }
    return 0;
}

variable_types: i int

variable_types: j int

user_defined_types: struct pattern { struct point *pv; int *s; int nPoints; unsigned char ascii; int size; }

user_defined_types: struct point { int x; int y; char isLast; }


freeModels

decompiled_code:
void freeModels()
{
void **v0; // rbp
__int64 i; // rbx

v0 = (void **)models;
do
{
for ( i = 0LL; i != 1600; i += 8LL )
{
if ( *(_QWORD *)((char *)*v0 + i) )
deletePattern();
}
if ( *v0 )
free(*v0);
*v0++ = 0LL;
}
while ( v0 != (void **)&preprocessor );
}

canonical_decompiled_code:
void func3()
{
void **v0; // rbp
__int64 i; // rbx

v0 = (void **)models;
do
{
for ( i = 0LL; i != 1600; i += 8LL )
{
if ( *(_QWORD *)((char *)*v0 + i) )
deletePattern();
}
if ( *v0 )
free(*v0);
*v0++ = 0LL;
}
while ( v0 != (void **)&preprocessor );
}

original_code:
void
freeModels()
{
    int i, j;
    for ( i = 0; i < 3; i++ )
    {
 for ( j = 0; j < 200; j++ )
     if ( models[i][j] != ((void *)0) )
  deletePattern( models[i][j] );
 if ( models[i] != ((void *)0) )
     free( models[i] );
 models[i] = ((void *)0);
    }
}

canonical_original_code:
void freeModels()
{
    int i; int j;
    for ( i = 0; i < 3; i++ )
    {
 for ( j = 0; j < 200; j++ )
     if ( models[i][j] != ((void *)0) )
  deletePattern( models[i][j] );
 if ( models[i] != ((void *)0) )
     free( models[i] );
 models[i] = ((void *)0);
    }
}


maxWidth

original_code:
int
maxWidth( Pattern *p )
{
    int minx, maxx, miny, maxy, i;
    Point p1 = p->pv[0];
    minx = maxx = p1.x;
    miny = maxy = p1.y;
    for ( i = 1; i < p->nPoints; i++ )
    {
 Point p2 = p->pv[i];
 if (p2.x > maxx)
     maxx = p2.x;
 if (p2.y > maxy)
     maxy = p2.y;
 if (p2.x < minx)
     minx = p2.x;
 if (p2.y < miny)
     miny = p2.y;
    }
    return ((maxx - minx) > (maxy - miny) ? (maxx - minx) : (maxy - miny));
}

canonical_original_code:
int maxWidth(struct pattern *p)
{
    int minx; int maxx; int miny; int maxy; int i;
    struct point p1 = p->pv[0];
    minx = maxx = p1.x;
    miny = maxy = p1.y;
    for ( i = 1; i < p->nPoints; i++ )
    {
 struct point p2 = p->pv[i];
 if (p2.x > maxx)
     maxx = p2.x;
 if (p2.y > maxy)
     maxy = p2.y;
 if (p2.x < minx)
     minx = p2.x;
 if (p2.y < miny)
     miny = p2.y;
    }
    return ((maxx - minx) > (maxy - miny) ? (maxx - minx) : (maxy - miny));
}

variable_types: struct pattern { struct point { int x; int y; char isLast; } *pv; int *s; int nPoints; unsigned char ascii; int size; } *

variable_types: minx int

variable_types: maxx int

variable_types: miny int

variable_types: maxy int


min

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

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

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

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

original_code:
int
min( int x, int y )
{
    if ( x <= y )
 return x;
    return y;
}

canonical_original_code:
int min(int x, int y)
{
    if ( x <= y )
 return x;
    return y;
}

variable_types: x int

variable_types: y int


slopeDistance

decompiled_code:
__int64 __fastcall slopeDistance(int a1, int a2)
{
int v2; // edi
__int64 result; // rax

v2 = abs32(a1 - a2);
result = (unsigned int)(6283 - v2);
if ( (int)result > v2 )
result = (unsigned int)v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2)
{
int v2; // edi
__int64 result; // rax

v2 = abs32(a1 - a2);
result = (unsigned int)(6283 - v2);
if ( (int)result > v2 )
result = (unsigned int)v2;
return result;
}

variable_types: delta int


pointDistance

decompiled_code:
__int64 __fastcall pointDistance(int a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // r9
int v4; // edx

v2 = (_DWORD *)(*(_QWORD *)unknown + 12LL * a1);
v3 = (_DWORD *)(*(_QWORD *)currentModel + 12LL * a2);
v4 = *v2 - *v3;
LODWORD(v2) = v2[1] - v3[1];
return v4 * v4
+ (_DWORD)v2 * (_DWORD)v2
+ (unsigned int)slopeDistance(
*(_DWORD *)(*(_QWORD *)(unknown + 8) + 4LL * a1),
*(_DWORD *)(*(_QWORD *)(currentModel + 8) + 4LL * a2));
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // r9
int v4; // edx

v2 = (_DWORD *)(*(_QWORD *)unknown + 12LL * a1);
v3 = (_DWORD *)(*(_QWORD *)currentModel + 12LL * a2);
v4 = *v2 - *v3;
LODWORD(v2) = v2[1] - v3[1];
return v4 * v4
+ (_DWORD)v2 * (_DWORD)v2
+ (unsigned int)func6(
*(_DWORD *)(*(_QWORD *)(unknown + 8) + 4LL * a1),
*(_DWORD *)(*(_QWORD *)(currentModel + 8) + 4LL * a2));
}

original_code:
int
pointDistance( int i, int j )
{
    Point p1 = unknown->pv[i];
    Point p2 = currentModel->pv[j];
    return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)
 + slopeDistance( unknown->s[i], currentModel->s[j] );
}

canonical_original_code:
int pointDistance(int i, int j)
{
    struct point p1 = unknown->pv[i];
    struct point p2 = currentModel->pv[j];
    return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)
 + slopeDistance( unknown->s[i], currentModel->s[j] );
}

variable_types: struct point { int x; int y; char isLast; }


freeLookup

decompiled_code:
void freeLookup()
{
__int64 v0; // rbp
__int64 v1; // rbx

if ( lookup_n > 0 )
{
v0 = 8LL * (unsigned int)lookup_n;
v1 = 0LL;
do
{
free(*(void **)((char *)lookup + v1));
v1 += 8LL;
}
while ( v1 != v0 );
}
free(lookup);
}

canonical_decompiled_code:
void func8()
{
__int64 v0; // rbp
__int64 v1; // rbx

if ( lookup_n > 0 )
{
v0 = 8LL * (unsigned int)lookup_n;
v1 = 0LL;
do
{
free(*(void **)((char *)lookup + v1));
v1 += 8LL;
}
while ( v1 != v0 );
}
free(lookup);
}

original_code:
void
freeLookup()
{
    int i;
    for ( i = 0; i < lookup_n; i++ )
 free( lookup[i] );
    free( lookup );
}


newLookup

decompiled_code:
__int64 __fastcall newLookup(int a1, int a2)
{
char *v2; // rax
_DWORD **v3; // r12
void **v4; // rbp
void **v5; // r15
__int64 v6; // rax
_DWORD *v7; // rbx
__int64 v8; // rbp
int i; // ebx
_DWORD *v10; // r14
__int64 result; // rax
__int64 j; // rbx

if ( lookup )
freeLookup();
lookup_n = a1;
v2 = (char *)malloc(8LL * a1);
v3 = (_DWORD **)v2;
lookup = v2;
if ( a1 > 0 )
{
v4 = (void **)v2;
v5 = (void **)&v2[8 * a1];
do
{
*v4 = malloc(4LL * a2);
if ( a2 > 0 )
{
v6 = 0LL;
do
{
*(_DWORD *)((char *)*v4 + v6) = -1;
v6 += 4LL;
}
while ( v6 != 4LL * (unsigned int)(a2 - 1) + 4 );
}
++v4;
}
while ( v4 != v5 );
}
v7 = *v3;
*v7 = pointDistance(0, 0);
if ( a2 > 1 )
{
v8 = 1LL;
for ( i = 1; i != a2; ++i )
{
v10 = *v3;
v10[v8] = v10[v8 - 1] + pointDistance(0, i);
++v8;
}
}
result = (unsigned int)a1;
if ( a1 > 1 )
{
for ( j = 1LL; j != a1; *v3[j++] = result )
result = *v3[j - 1] + (unsigned int)pointDistance(j, 0);
}
return result;
}

original_code:
void
newLookup( int n, int m )
{
    int i, j;
    if ( lookup != ((void *)0) )
     freeLookup();
    lookup = (int **) malloc( (lookup_n = n) * sizeof(int *));
    for ( i = 0; i < lookup_n; i++ )
    {
 lookup[i] = (int *) malloc( m * sizeof(int) );
 for ( j = 0; j < m; j++ )
     lookup[i][j] = -1;
    }
    lookup[0][0] = pointDistance(0, 0);
    for ( j = 1; j < m; j++ )
 lookup[0][j] = lookup[0][j-1] + pointDistance(0, j);
    for ( i = 1; i < n; i++ )
 lookup[i][0] = lookup[i-1][0] + pointDistance(i, 0);
}

canonical_original_code:
void newLookup(int n, int m)
{
    int i; int j;
    if ( lookup != ((void *)0) )
     freeLookup();
    lookup = (int **) malloc( (lookup_n = n) * sizeof(int *));
    for ( i = 0; i < lookup_n; i++ )
    {
 lookup[i] = (int *) malloc( m * sizeof(int) );
 for ( j = 0; j < m; j++ )
     lookup[i][j] = -1;
    }
    lookup[0][0] = pointDistance(0, 0);
    for ( j = 1; j < m; j++ )
 lookup[0][j] = lookup[0][j-1] + pointDistance(0, j);
    for ( i = 1; i < n; i++ )
 lookup[i][0] = lookup[i-1][0] + pointDistance(i, 0);
}

variable_types: n int

variable_types: m int


D

decompiled_code:
__int64 __fastcall D(int a1, int a2)
{
_QWORD *v2; // r13
__int64 result; // rax
int v4; // ebp
unsigned int v5; // er15
unsigned int v6; // eax
unsigned int v7; // er15
unsigned int v8; // eax
int v9; // ebp
unsigned int v10; // er14
unsigned int v11; // eax

v2 = (char *)lookup + 8 * a1;
result = *(unsigned int *)(*v2 + 4LL * a2);
if ( (_DWORD)result == -1 )
{
if ( a2 == 1 )
{
v9 = pointDistance(a1, 1);
v10 = D((unsigned int)(a1 - 1), 0LL);
v11 = D((unsigned int)(a1 - 1), 1LL);
result = v9 + (unsigned int)min(v11, v10);
*(_DWORD *)(*v2 + 4LL * a2) = result;
}
else
{
v4 = pointDistance(a1, a2);
v5 = D((unsigned int)(a1 - 1), (unsigned int)(a2 - 2));
v6 = D((unsigned int)(a1 - 1), (unsigned int)(a2 - 1));
v7 = min(v6, v5);
v8 = D((unsigned int)(a1 - 1), (unsigned int)a2);
result = v4 + (unsigned int)min(v8, v7);
*(_DWORD *)(*v2 + 4LL * a2) = result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2)
{
_QWORD *v2; // r13
__int64 result; // rax
int v4; // ebp
unsigned int v5; // er15
unsigned int v6; // eax
unsigned int v7; // er15
unsigned int v8; // eax
int v9; // ebp
unsigned int v10; // er14
unsigned int v11; // eax

v2 = (char *)lookup + 8 * a1;
result = *(unsigned int *)(*v2 + 4LL * a2);
if ( (_DWORD)result == -1 )
{
if ( a2 == 1 )
{
v9 = func7(a1, 1);
v10 = func10((unsigned int)(a1 - 1), 0LL);
v11 = func10((unsigned int)(a1 - 1), 1LL);
result = v9 + (unsigned int)func5(v11, v10);
*(_DWORD *)(*v2 + 4LL * a2) = result;
}
else
{
v4 = func7(a1, a2);
v5 = func10((unsigned int)(a1 - 1), (unsigned int)(a2 - 2));
v6 = func10((unsigned int)(a1 - 1), (unsigned int)(a2 - 1));
v7 = func5(v6, v5);
v8 = func10((unsigned int)(a1 - 1), (unsigned int)a2);
result = v4 + (unsigned int)func5(v8, v7);
*(_DWORD *)(*v2 + 4LL * a2) = result;
}
}
return result;
}

original_code:
int
D( int i, int j )
{
    if ( lookup[i][j] != -1 )
 return lookup[i][j];
    if ( j == 1 )
 return lookup[i][j] = pointDistance(i, 1)
     + min(D(i-1, 1), D(i-1, 0));
    else
 return lookup[i][j] = pointDistance(i,j)
     + min(D(i-1, j), min(D(i-1, j-1), D(i-1, j-2)));
}

canonical_original_code:
int D(int i, int j)
{
    if ( lookup[i][j] != -1 )
 return lookup[i][j];
    if ( j == 1 )
 return lookup[i][j] = pointDistance(i, 1)
     + min(D(i-1, 1), D(i-1, 0));
    else
 return lookup[i][j] = pointDistance(i,j)
     + min(D(i-1, j), min(D(i-1, j-1), D(i-1, j-2)));
}


distance

decompiled_code:
__int64 __fastcall distance(int a1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = *(_QWORD *)(samples + 8LL * a1);
currentModel = v1;
v2 = unknown;
newLookup(*(_DWORD *)(unknown + 16), *(_DWORD *)(v1 + 16));
return (unsigned int)((int)D(*(_DWORD *)(v2 + 16) - 1, *(_DWORD *)(v1 + 16) - 1) / *(_DWORD *)(v1 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = *(_QWORD *)(samples + 8LL * a1);
currentModel = v1;
v2 = unknown;
func9(*(_DWORD *)(unknown + 16), *(_DWORD *)(v1 + 16));
return (unsigned int)((int)func10(*(_DWORD *)(v2 + 16) - 1, *(_DWORD *)(v1 + 16) - 1) / *(_DWORD *)(v1 + 16));
}

original_code:
int
distance( int sampleIndex )
{
    currentModel = samples[ sampleIndex ];
    newLookup( unknown->nPoints, currentModel->nPoints );
    return D( unknown->nPoints - 1, currentModel->nPoints - 1 )
 / currentModel->nPoints;
}

canonical_original_code:
int distance(int sampleIndex)
{
    currentModel = samples[ sampleIndex ];
    newLookup( unknown->nPoints, currentModel->nPoints );
    return D( unknown->nPoints - 1, currentModel->nPoints - 1 )
 / currentModel->nPoints;
}

variable_types: sampleIndex int