decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // bp
int v4; // eax
int v5; // eax
char v6; // bl
char *v7; // rax
char *v9; // rax
struct timeval v10; // [rsp+0h] [rbp-698h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-688h] BYREF
time_t arg[201]; // [rsp+50h] [rbp-648h] BYREF

memset(arg, 0, 0x630uLL);
v10.tv_sec = 0LL;
v10.tv_usec = 0LL;
if ( (unsigned int)parser_para((__int64)arg, argc, (char *const *)argv) )
{
dbg(1LL, "%s.", "Parser parameters failed, exit");
exit(1);
}
if ( !LOBYTE(arg[1]) )
{
dbg(1LL, "%s.", "Specify the config file with -f");
exit(1);
}
if ( (unsigned int)read_cfg(arg, &arg[1]) )
{
dbg(1LL, "%s.", "Read config file: %s failed");
exit(1);
}
if ( (unsigned int)time_init(arg) )
{
dbg(1LL, "%s.", "Initiate time failed, exit");
exit(1);
}
if ( (unsigned int)uld_init(arg, &arg[1]) )
exit(1);
v3 = permit_to_run((__int64)arg);
if ( !v3 )
exit(1);
pthread_mutex_init(&mtx_gen, 0LL);
pthread_mutex_init(&mtx_uld, 0LL);
pthread_mutex_lock(&mtx_gen);
pthread_mutex_lock(&mtx_uld);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v4 = pthread_create(&tid_uld, &attr, (void *(*)(void *))uld_thread_hdl, arg);
if ( v4 )
{
v7 = strerror(v4);
dbg(1LL, "Create upload pthread failed: %s.", v7);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
else
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v5 = pthread_create(&tid_gen, &attr, (void *(*)(void *))gen_thread_hdl, arg);
if ( !v5 )
{
pthread_attr_destroy(&attr);
_sysv_signal(2, (__sighandler_t)sig_hdl);
_sysv_signal(15, (__sighandler_t)sig_hdl);
v6 = 0;
while ( 1 )
{
if ( (time(0LL) - arg[35]) % SLODWORD(arg[34]) )
{
v6 = 0;
}
else if ( !v6 )
{
pthread_mutex_unlock(&mtx_gen);
v6 = v3;
}
v10.tv_sec = 0LL;
v10.tv_usec = 100000LL;
select(0, 0LL, 0LL, 0LL, &v10);
}
}
v9 = strerror(v5);
dbg(1LL, "Create generate pthread failed: %s.", v9);
pthread_cancel(tid_uld);
sleep(1u);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // bp
int v4; // eax
int v5; // eax
char v6; // bl
char *v7; // rax
char *v9; // rax
struct timeval v10; // [rsp+0h] [rbp-698h] BYREF
pthread_attr_t attr; // [rsp+10h] [rbp-688h] BYREF
time_t arg[201]; // [rsp+50h] [rbp-648h] BYREF

memset(arg, 0, 0x630uLL);
v10.tv_sec = 0LL;
v10.tv_usec = 0LL;
if ( (unsigned int)func4((__int64)arg, argc, (char *const *)argv) )
{
dbg(1LL, "%s.", "Parser parameters failed, exit");
exit(1);
}
if ( !LOBYTE(arg[1]) )
{
dbg(1LL, "%s.", "Specify the config file with -f");
exit(1);
}
if ( (unsigned int)read_cfg(arg, &arg[1]) )
{
dbg(1LL, "%s.", "Read config file: %s failed");
exit(1);
}
if ( (unsigned int)func5(arg) )
{
dbg(1LL, "%s.", "Initiate time failed, exit");
exit(1);
}
if ( (unsigned int)uld_init(arg, &arg[1]) )
exit(1);
v3 = func7((__int64)arg);
if ( !v3 )
exit(1);
pthread_mutex_init(&mtx_gen, 0LL);
pthread_mutex_init(&mtx_uld, 0LL);
pthread_mutex_lock(&mtx_gen);
pthread_mutex_lock(&mtx_uld);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v4 = pthread_create(&tid_uld, &attr, (void *(*)(void *))uld_thread_hdl, arg);
if ( v4 )
{
v7 = strerror(v4);
dbg(1LL, "Create upload pthread failed: %s.", v7);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
else
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
v5 = pthread_create(&tid_gen, &attr, (void *(*)(void *))gen_thread_hdl, arg);
if ( !v5 )
{
pthread_attr_destroy(&attr);
_sysv_signal(2, (__sighandler_t)sig_hdl);
_sysv_signal(15, (__sighandler_t)sig_hdl);
v6 = 0;
while ( 1 )
{
if ( (time(0LL) - arg[35]) % SLODWORD(arg[34]) )
{
v6 = 0;
}
else if ( !v6 )
{
pthread_mutex_unlock(&mtx_gen);
v6 = v3;
}
v10.tv_sec = 0LL;
v10.tv_usec = 100000LL;
select(0, 0LL, 0LL, 0LL, &v10);
}
}
v9 = strerror(v5);
dbg(1LL, "Create generate pthread failed: %s.", v9);
pthread_cancel(tid_uld);
sleep(1u);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
}
return -1;
}

original_code:
int main(int argc, char *argv[])
{
    cheer_ctx_t ctx = { 0 };
    pthread_attr_t attr;
    _Bool has_gen = 0;
    struct timeval tv = { 0 };
    int err;
    if (parser_para(&ctx, argc, argv) != 0) {
        dbg((1 << 0), "%s.", "Parser parameters failed, exit");
        exit(1);
    }
    if (strlen(ctx.cfg) == 0) {
        dbg((1 << 0), "%s.", "Specify the config file with -f");
        exit(1);
    }
    if (read_cfg(&ctx, ctx.cfg) != 0) {
        dbg((1 << 0), "%s.", "Read config file: %s failed");
        exit(1);
    }
    if (time_init(&ctx) != 0) {
        dbg((1 << 0), "%s.", "Initiate time failed, exit");
        exit(1);
    }
    if (uld_init() != 0) {
        exit(1);
    }
    if (!permit_to_run(&ctx)) {
        exit(1);
    }
    pthread_mutex_init(&mtx_gen, ((void *)0));
    pthread_mutex_init(&mtx_uld, ((void *)0));
    pthread_mutex_lock(&mtx_gen);
    pthread_mutex_lock(&mtx_uld);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_uld, &attr, uld_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create upload pthread failed: %s.", strerror(err));
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_gen, &attr, gen_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create generate pthread failed: %s.", strerror(err));
        pthread_cancel(tid_uld);
        sleep(1);
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_destroy(&attr);
    signal(2, sig_hdl);
    signal(15, sig_hdl);
    while (1) {
        if ((time(((void *)0)) - ctx.t_uld_time) % ctx.interval == 0) {
            if (!has_gen) {
                pthread_mutex_unlock(&mtx_gen);
                has_gen = 1;
            }
        } else {
            has_gen = 0;
        }
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        select(0, ((void *)0), ((void *)0), ((void *)0), &tv);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct cheer_ctx ctx = { 0 };
    union pthread_attr_t attr;
    _Bool has_gen = 0;
    struct timeval tv = { 0 };
    int err;
    if (parser_para(&ctx, argc, argv) != 0) {
        dbg((1 << 0), "%s.", "Parser parameters failed, exit");
        exit(1);
    }
    if (strlen(ctx.cfg) == 0) {
        dbg((1 << 0), "%s.", "Specify the config file with -f");
        exit(1);
    }
    if (read_cfg(&ctx, ctx.cfg) != 0) {
        dbg((1 << 0), "%s.", "Read config file: %s failed");
        exit(1);
    }
    if (time_init(&ctx) != 0) {
        dbg((1 << 0), "%s.", "Initiate time failed, exit");
        exit(1);
    }
    if (uld_init() != 0) {
        exit(1);
    }
    if (!permit_to_run(&ctx)) {
        exit(1);
    }
    pthread_mutex_init(&mtx_gen, ((void *)0));
    pthread_mutex_init(&mtx_uld, ((void *)0));
    pthread_mutex_lock(&mtx_gen);
    pthread_mutex_lock(&mtx_uld);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_uld, &attr, uld_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create upload pthread failed: %s.", strerror(err));
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    err = pthread_create(&tid_gen, &attr, gen_thread_hdl, (void *)&ctx);
    if (err != 0) {
        dbg((1 << 0), "Create generate pthread failed: %s.", strerror(err));
        pthread_cancel(tid_uld);
        sleep(1);
        pthread_mutex_destroy(&mtx_gen);
        pthread_mutex_destroy(&mtx_uld);
        return -1;
    }
    pthread_attr_destroy(&attr);
    signal(2, sig_hdl);
    signal(15, sig_hdl);
    while (1) {
        if ((time(((void *)0)) - ctx.t_uld_time) % ctx.interval == 0) {
            if (!has_gen) {
                pthread_mutex_unlock(&mtx_gen);
                has_gen = 1;
            }
        } else {
            has_gen = 0;
        }
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        select(0, ((void *)0), ((void *)0), ((void *)0), &tv);
    }
    return 0;
}

variable_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; }

variable_types: union pthread_attr_t { char __size[56]; long __align; }

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

variable_types: err int


ltrim

repo: mczoden/hems

decompiled_code:
const char *__fastcall ltrim(const char *a1)
{
const unsigned __int16 *v1; // rcx
const char *result; // rax

if ( !strlen(a1) )
return a1;
v1 = *_ctype_b_loc();
for ( result = a1; (v1[*result] & 0x2000) != 0; ++result )
;
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(const char *a1)
{
const unsigned __int16 *v1; // rcx
const char *result; // rax

if ( !strlen(a1) )
return a1;
v1 = *_ctype_b_loc();
for ( result = a1; (v1[*result] & 0x2000) != 0; ++result )
;
return result;
}

original_code:
static char *ltrim(char *src)
{
    size_t i = 0;
    char *p = src;
    size_t len = strlen(src);
    for (; (i < len) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace)); p++) {
        ;
    }
    return p;
}

canonical_original_code:
static char *ltrim(char *src)
{
    unsigned long i = 0;
    char *p = src;
    unsigned long len = strlen(src);
    for (; (i < len) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace)); p++) {
        ;
    }
    return p;
}

variable_types:
src
char *

variable_types:
i
unsigned long

variable_types:
p
char *

variable_types:
len
unsigned long

return_type: char *


rtrim

decompiled_code:
const char *__fastcall rtrim(const char *a1)
{
const char *v1; // rbx
const char *v2; // rbp
const unsigned __int16 *v3; // rdx

v1 = &a1[strlen(a1) - 1];
v2 = a1 - 1;
if ( v1 != a1 - 1 )
{
v3 = *_ctype_b_loc();
while ( (v3[*v1] & 0x2000) != 0 )
{
if ( --v1 == v2 )
goto LABEL_7;
}
v2 = v1;
}
LABEL_7:
*((_BYTE *)v2 + 1) = 0;
return a1;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
const char *v1; // rbx
const char *v2; // rbp
const unsigned __int16 *v3; // rdx

v1 = &a1[strlen(a1) - 1];
v2 = a1 - 1;
if ( v1 != a1 - 1 )
{
v3 = *_ctype_b_loc();
while ( (v3[*v1] & 0x2000) != 0 )
{
if ( --v1 == v2 )
goto LABEL_7;
}
v2 = v1;
}
LABEL_7:
*((_BYTE *)v2 + 1) = 0;
return a1;
}

original_code:
static char *rtrim(char *src)
{
    char *p = src + strlen(src) - 1;
    for (; (p != src - 1) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace)); p--) {
        ;
    }
    *(p + 1) = '\0';
    return src;
}

canonical_original_code:
static char *rtrim(char *src)
{
    char *p = src + strlen(src) - 1;
    for (; (p != src - 1) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace)); p--) {
        ;
    }
    *(p + 1) = '\0';
    return src;
}


dbg

decompiled_code:
void dbg(unsigned __int8 a1, const char *a2, ...)
{
const __m128i *v2; // rax
__m128i v3; // [rsp+0h] [rbp-128h]
time_t timer; // [rsp+40h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+48h] [rbp-E0h] BYREF

timer = 0LL;
if ( (a1 & (unsigned __int8)dbg_lvl) != 0 )
{
timer = time(0LL);
v2 = (const __m128i *)localtime(&timer);
v3 = _mm_loadu_si128(v2);
fprintf(
stderr,
"[%04d%02d%02d %02d:%02d:%02d] ",
(unsigned int)(HIDWORD(v2[1].m128i_i64[0]) + 1900),
(unsigned int)v2[1].m128i_i64[0] + 1,
v3.m128i_u32[3],
v3.m128i_u32[2],
v3.m128i_u32[1],
v3.m128i_u32[0]);
if ( a1 == 4 )
{
fwrite("[INF] ", 1uLL, 6uLL, stderr);
}
else if ( a1 > 4u )
{
if ( a1 == 8 )
fwrite("[DBG] ", 1uLL, 6uLL, stderr);
}
else if ( a1 == 1 )
{
fwrite("[PNC] ", 1uLL, 6uLL, stderr);
}
else if ( a1 == 2 )
{
fwrite("[ERR] ", 1uLL, 6uLL, stderr);
}
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
}
}

canonical_decompiled_code:
void func0(unsigned __int8 a1, const char *a2, ...)
{
const __m128i *v2; // rax
__m128i v3; // [rsp+0h] [rbp-128h]
time_t timer; // [rsp+40h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+48h] [rbp-E0h] BYREF

timer = 0LL;
if ( (a1 & (unsigned __int8)dbg_lvl) != 0 )
{
timer = time(0LL);
v2 = (const __m128i *)localtime(&timer);
v3 = _mm_loadu_si128(v2);
fprintf(
stderr,
"[%04d%02d%02d %02d:%02d:%02d] ",
(unsigned int)(HIDWORD(v2[1].m128i_i64[0]) + 1900),
(unsigned int)v2[1].m128i_i64[0] + 1,
v3.m128i_u32[3],
v3.m128i_u32[2],
v3.m128i_u32[1],
v3.m128i_u32[0]);
if ( a1 == 4 )
{
fwrite("[INF] ", 1uLL, 6uLL, stderr);
}
else if ( a1 > 4u )
{
if ( a1 == 8 )
fwrite("[DBG] ", 1uLL, 6uLL, stderr);
}
else if ( a1 == 1 )
{
fwrite("[PNC] ", 1uLL, 6uLL, stderr);
}
else if ( a1 == 2 )
{
fwrite("[ERR] ", 1uLL, 6uLL, stderr);
}
va_start(arg, a2);
vfprintf(stderr, a2, arg);
fputc(10, stderr);
}
}

original_code:
void dbg(unsigned char lvl, const char *fmt, ...)
{
    va_list arg;
    time_t t_now = 0;
    struct tm tm_now;
    if ((dbg_lvl & lvl) == 0) {
        return;
    }
    t_now = time(((void *)0));
    memcpy(&tm_now, localtime(&t_now), sizeof(tm_now));
    fprintf(stderr, "[%04d%02d%02d %02d:%02d:%02d] ",
            tm_now.tm_year + 1900,
            tm_now.tm_mon + 1,
            tm_now.tm_mday,
            tm_now.tm_hour,
            tm_now.tm_min,
            tm_now.tm_sec);
    switch (lvl) {
    case (1 << 0):
        fprintf(stderr, "[PNC] ");
        break;
    case (1 << 1):
        fprintf(stderr, "[ERR] ");
        break;
    case (1 << 2):
        fprintf(stderr, "[INF] ");
        break;
    case (1 << 3):
        fprintf(stderr, "[DBG] ");
        break;
    default:
        break;
    }
    __builtin_va_start(arg,fmt);
    vfprintf(stderr, fmt, arg);
    __builtin_va_end(arg);
    fprintf(stderr, "\n");
}

canonical_original_code:
void dbg(unsigned char lvl, char *fmt, ...)
{
    __builtin_va_list arg;
    long t_now = 0;
    struct tm tm_now;
    if ((dbg_lvl & lvl) == 0) {
        return;
    }
    t_now = time(((void *)0));
    memcpy(&tm_now, localtime(&t_now), sizeof(tm_now));
    fprintf(stderr, "[%04d%02d%02d %02d:%02d:%02d] ",
            tm_now.tm_year + 1900,
            tm_now.tm_mon + 1,
            tm_now.tm_mday,
            tm_now.tm_hour,
            tm_now.tm_min,
            tm_now.tm_sec);
    switch (lvl) {
    case (1 << 0):
        fprintf(stderr, "[PNC] ");
        break;
    case (1 << 1):
        fprintf(stderr, "[ERR] ");
        break;
    case (1 << 2):
        fprintf(stderr, "[INF] ");
        break;
    case (1 << 3):
        fprintf(stderr, "[DBG] ");
        break;
    default:
        break;
    }
    __builtin_va_start(arg,fmt);
    vfprintf(stderr, fmt, arg);
    __builtin_va_end(arg);
    fprintf(stderr, "\n");
}

variable_types:
lvl
unsigned char

variable_types:
fmt
char *

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long __tm_gmtoff; char *__tm_zone; }

return_type: void


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

if ( a1 < 0 )
{
dbg(2u, "%s.", "Invalid debug level specified, keep default: panic, error & info");
result = 0xFFFFFFFFLL;
}
else
{
if ( a1 > 7 )
LOBYTE(a1) = 7;
dbg_lvl = 255 >> (8 - a1);
result = 0LL;
}
return result;
}

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

if ( a1 < 0 )
{
func0(2u, "%s.", "Invalid debug level specified, keep default: panic, error & info");
result = 0xFFFFFFFFLL;
}
else
{
if ( a1 > 7 )
LOBYTE(a1) = 7;
dbg_lvl = 255 >> (8 - a1);
result = 0LL;
}
return result;
}

original_code:
int set_dbg_lvl(int lvl)
{
    if (lvl < 0) {
        dbg((1 << 1), "%s.", "Invalid debug level specified," " keep default: panic, error & info");
        return -1;
    }
    if (lvl > 7) {
        lvl = 7;
    }
    dbg_lvl = 0xff >> (8 - lvl);
    return 0;
}

variable_types: lvl int

return_type: int


decompiled_code:
__int64 __fastcall get_proto_by_url(const void *a1)
{
bool v2; // zf
__int64 result; // rax

v2 = memcmp(a1, "ftp:", 4uLL) == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( memcmp(a1, "http:", 5uLL) )
result = memcmp(a1, "https:", 6uLL) == 0 ? 2 : -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1)
{
bool v2; // zf
__int64 result; // rax

v2 = memcmp(a1, "ftp:", 4uLL) == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( memcmp(a1, "http:", 5uLL) )
result = memcmp(a1, "https:", 6uLL) == 0 ? 2 : -1;
}
return result;
}

original_code:
static int get_proto_by_url(const char *url)
{
    if (strncmp(url, "ftp:", 4) == 0) {
        return PROTO_FTP;
    } else if (strncmp(url, "http:", 5) == 0) {
        return PROTO_HTTP;
    } else if (strncmp(url, "https:", 6) == 0) {
        return PROTO_HTTPS;
    }
    return -1;
}

canonical_original_code:
static int get_proto_by_url(char *url)
{
    if (strncmp(url, "ftp:", 4) == 0) {
        return PROTO_FTP;
    } else if (strncmp(url, "http:", 5) == 0) {
        return PROTO_HTTP;
    } else if (strncmp(url, "https:", 6) == 0) {
        return PROTO_HTTPS;
    }
    return -1;
}

variable_types:
url
char *


decompiled_code:
__int64 uld_init()
{
if ( !(unsigned int)curl_global_init(0LL) )
return 0LL;
dbg(2LL, "%s.", "Initiate global curl failed, retry at next intval");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( !(unsigned int)curl_global_init(0LL) )
return 0LL;
dbg(2LL, "%s.", "Initiate global curl failed, retry at next intval");
return 0xFFFFFFFFLL;
}

original_code:
int uld_init(void)
{
    if (curl_global_init(0) != CURLE_OK) {
        dbg((1 << 1), "%s.", "Initiate global curl failed, retry at next intval");
        return -1;
    }
    return 0;
}

canonical_original_code:
int uld_init()
{
    if (curl_global_init(0) != CURLE_OK) {
        dbg((1 << 1), "%s.", "Initiate global curl failed, retry at next intval");
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 uld_deinit()
{
return curl_global_cleanup();
}

canonical_decompiled_code:
__int64 func3()
{
return curl_global_cleanup();
}

original_code:
void uld_deinit(void)
{
    curl_global_cleanup();
}

canonical_original_code:
void uld_deinit()
{
    curl_global_cleanup();
}


decompiled_code:
__int64 __fastcall mgostring_add(__int64 a1, const char *a2)
{
char *v2; // r13
size_t v3; // rbx
size_t v4; // rbx
void *v5; // rax

v2 = *(char **)a1;
v3 = strlen(*(const char **)a1);
v4 = strlen(a2) + v3;
if ( *(_QWORD *)(a1 + 8) >= v4 )
goto LABEL_4;
v5 = realloc(v2, v4 + 1);
if ( v5 )
{
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v4;
printf("*** realloc, str->length is now %lu ***\n", v4);
LABEL_4:
strcat(*(char **)a1, a2);
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2)
{
char *v2; // r13
size_t v3; // rbx
size_t v4; // rbx
void *v5; // rax

v2 = *(char **)a1;
v3 = strlen(*(const char **)a1);
v4 = strlen(a2) + v3;
if ( *(_QWORD *)(a1 + 8) >= v4 )
goto LABEL_4;
v5 = realloc(v2, v4 + 1);
if ( v5 )
{
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v4;
printf("*** realloc, str->length is now %lu ***\n", v4);
LABEL_4:
strcat(*(char **)a1, a2);
return 1LL;
}
return 0LL;
}

original_code:
_Bool mgostring_add(struct MgoString *str, const char *new){
    unsigned long new_len = strlen(str->data) + strlen(new);
    if (new_len > str->length) {
        char *new_data = realloc(str->data, new_len + 1);
        if (new_data == ((void *)0)) {
            return 0;
        }
        str->data = new_data;
        str->length = new_len;
            printf("*** realloc, str->length is now %lu ***\n", str->length);
    }
    strcat(str->data, new);
    return 1;
}

canonical_original_code:
_Bool mgostring_add(struct MgoString *str, char *new){
    unsigned long new_len = strlen(str->data) + strlen(new);
    if (new_len > str->length) {
        char *new_data = realloc(str->data, new_len + 1);
        if (new_data == ((void *)0)) {
            return 0;
        }
        str->data = new_data;
        str->length = new_len;
            printf("*** realloc, str->length is now %lu ***\n", str->length);
    }
    strcat(str->data, new);
    return 1;
}

variable_types: struct MgoString { char *data; unsigned long length; } *

variable_types:
new
char *


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

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

original_code:
char *mgostring_c_str(struct MgoString *str){
    return str->data;
}

user_defined_types: struct MgoString { char *data; unsigned long length; }


decompiled_code:
const unsigned __int16 *__fastcall mgostring_rtrim(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
const unsigned __int16 *result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = __ctype_b_loc();
while ( 1 )
{
result = *v3;
if ( ((*v3)[*v2] & 0x2000) == 0 )
break;
*v2-- = 0;
}
return result;
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func9(const char **a1)
{
const char *v1; // rbx
const char *v2; // rbx
const unsigned __int16 **v3; // rcx
const unsigned __int16 *result; // rax

v1 = *a1;
v2 = &v1[strlen(*a1) - 1];
v3 = __ctype_b_loc();
while ( 1 )
{
result = *v3;
if ( ((*v3)[*v2] & 0x2000) == 0 )
break;
*v2-- = 0;
}
return result;
}

original_code:
void mgostring_rtrim(struct MgoString *str){
    char *non_space = str->data + (strlen(str->data) - 1);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short int) _ISspace)) {
        *non_space = '\0';
        non_space--;
    }
}

canonical_original_code:
void mgostring_rtrim(struct MgoString *str){
    char *non_space = str->data + (strlen(str->data) - 1);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short) _ISspace)) {
        *non_space = '\0';
        non_space--;
    }
}


decompiled_code:
void *__fastcall mgostring_ltrim(const char **a1)
{
char *v1; // rbp
size_t v2; // rbx
const unsigned __int16 *v3; // rdx
char *i; // rsi

v1 = (char *)*a1;
v2 = strlen(*a1);
v3 = *__ctype_b_loc();
for ( i = v1; (v3[*i] & 0x2000) != 0; ++i )
;
return memmove(v1, i, v2 + 1);
}

canonical_decompiled_code:
void *__fastcall func10(const char **a1)
{
char *v1; // rbp
size_t v2; // rbx
const unsigned __int16 *v3; // rdx
char *i; // rsi

v1 = (char *)*a1;
v2 = strlen(*a1);
v3 = *__ctype_b_loc();
for ( i = v1; (v3[*i] & 0x2000) != 0; ++i )
;
return memmove(v1, i, v2 + 1);
}

original_code:
void mgostring_ltrim(struct MgoString *str){
    char *non_space = str->data;
    unsigned long len = strlen(str->data);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short int) _ISspace)) non_space++;
    memmove(str->data, non_space, len + 1);
}

canonical_original_code:
void mgostring_ltrim(struct MgoString *str){
    char *non_space = str->data;
    unsigned long len = strlen(str->data);
    while (((*__ctype_b_loc ())[(int) ((*non_space))] & (unsigned short) _ISspace)) non_space++;
    memmove(str->data, non_space, len + 1);
}


decompiled_code:
const unsigned __int16 *__fastcall mgostring_trim(const char **a1)
{
mgostring_ltrim(a1);
return mgostring_rtrim(a1);
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func11(const char **a1)
{
func10(a1);
return func9(a1);
}

original_code:
void mgostring_trim(struct MgoString *str){
    mgostring_ltrim(str);
    mgostring_rtrim(str);
}


decompiled_code:
__int64 __fastcall mgostring_replace(const char **a1, const char *a2, const char *a3)
{
const char *v4; // rbx
unsigned int v5; // er14
char *v6; // rbp
char *v7; // rax
char *v8; // rbx
char *ptr; // [rsp+8h] [rbp-40h]

v4 = *a1;
v5 = 0;
if ( strstr(*a1, a2) )
{
v6 = strdup(v4);
ptr = v6;
if ( v6 )
{
*v4 = 0;
while ( 1 )
{
v7 = strstr(v6, a2);
v8 = v7;
if ( !v7 )
break;
++v5;
*v7 = 0;
mgostring_add((__int64)a1, v6);
mgostring_add((__int64)a1, a3);
v6 = &v8[strlen(a2)];
}
mgostring_add((__int64)a1, v6);
free(ptr);
}
else
{
v5 = 0;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char **a1, const char *a2, const char *a3)
{
const char *v4; // rbx
unsigned int v5; // er14
char *v6; // rbp
char *v7; // rax
char *v8; // rbx
char *ptr; // [rsp+8h] [rbp-40h]

v4 = *a1;
v5 = 0;
if ( strstr(*a1, a2) )
{
v6 = strdup(v4);
ptr = v6;
if ( v6 )
{
*v4 = 0;
while ( 1 )
{
v7 = strstr(v6, a2);
v8 = v7;
if ( !v7 )
break;
++v5;
*v7 = 0;
func7((__int64)a1, v6);
func7((__int64)a1, a3);
v6 = &v8[strlen(a2)];
}
func7((__int64)a1, v6);
free(ptr);
}
else
{
v5 = 0;
}
}
return v5;
}

original_code:
int mgostring_replace(struct MgoString *str, const char *from, const char *to) {
    if (strstr(str->data, from) == ((void *)0)) return 0;
    char *copy = strdup(str->data);
    if (copy == ((void *)0)) return 0;
    int num_changes = 0;
    strcpy(str->data,"");
    char *copy_from = copy;
    char *up_to = ((void *)0);
    do {
        up_to = strstr(copy_from, from);
        if (up_to == ((void *)0)) break;
        num_changes++;
        *up_to = '\0';
        mgostring_add(str, copy_from);
        mgostring_add(str, to);
        copy_from = up_to + strlen(from);
    } while(1);
    mgostring_add(str, copy_from);
    free(copy);
    return num_changes;
}

canonical_original_code:
int mgostring_replace(struct MgoString *str, char *from, char *to) {
    if (strstr(str->data, from) == ((void *)0)) return 0;
    char *copy = strdup(str->data);
    if (copy == ((void *)0)) return 0;
    int num_changes = 0;
    strcpy(str->data,"");
    char *copy_from = copy;
    char *up_to = ((void *)0);
    do {
        up_to = strstr(copy_from, from);
        if (up_to == ((void *)0)) break;
        num_changes++;
        *up_to = '\0';
        mgostring_add(str, copy_from);
        mgostring_add(str, to);
        copy_from = up_to + strlen(from);
    } while(1);
    mgostring_add(str, copy_from);
    free(copy);
    return num_changes;
}

variable_types:
from
char *

variable_types:
to
char *

variable_types:
copy
char *


decompiled_code:
void __fastcall mgostring_destruct(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func13(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void mgostring_destruct(struct MgoString *str){
    free(str->data);
    free(str);
}


callback

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

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

original_code:
void callback(char*k, char*v){
    printf("%s = \"%s\"\n", k, v);
}

canonical_original_code:
void callback(char *k, char *v){
    printf("%s = \"%s\"\n", k, v);
}

variable_types:
k
char *

variable_types:
v
char *


trimwhitespace

decompiled_code:
char *__fastcall trimwhitespace(char *s)
{
const unsigned __int16 *v2; // rbp
char *v3; // rax

v2 = *__ctype_b_loc();
while ( (v2[(unsigned __int8)*s] & 0x2000) != 0 )
++s;
if ( *s )
{
v3 = &s[strlen(s) - 1];
if ( s < v3 )
{
do
{
if ( (v2[(unsigned __int8)*v3] & 0x2000) == 0 )
break;
--v3;
}
while ( s != v3 );
}
v3[1] = 0;
}
return s;
}

canonical_decompiled_code:
char *__fastcall func8(char *s)
{
const unsigned __int16 *v2; // rbp
char *v3; // rax

v2 = *__ctype_b_loc();
while ( (v2[(unsigned __int8)*s] & 0x2000) != 0 )
++s;
if ( *s )
{
v3 = &s[strlen(s) - 1];
if ( s < v3 )
{
do
{
if ( (v2[(unsigned __int8)*v3] & 0x2000) == 0 )
break;
--v3;
}
while ( s != v3 );
}
v3[1] = 0;
}
return s;
}

original_code:
static char *trimwhitespace(char *str)
{
    char *end;
    while(((*__ctype_b_loc ())[(int) (((unsigned char) *str))] & (unsigned short int) _ISspace)) {
        str++;
    }
    if(*str == 0) {
        return str;
    }
    end = str + strlen(str) - 1;
    while(end > str && ((*__ctype_b_loc ())[(int) (((unsigned char) *end))] & (unsigned short int) _ISspace)) {
        end--;
    }
    *(end+1) = 0;
    return str;
}

canonical_original_code:
static char *trimwhitespace(char *str)
{
    char *end;
    while(((*__ctype_b_loc ())[(int) (((unsigned char) *str))] & (unsigned short) _ISspace)) {
        str++;
    }
    if(*str == 0) {
        return str;
    }
    end = str + strlen(str) - 1;
    while(end > str && ((*__ctype_b_loc ())[(int) (((unsigned char) *end))] & (unsigned short) _ISspace)) {
        end--;
    }
    *(end+1) = 0;
    return str;
}

variable_types:
str
char *

variable_types:
end
char *


decompiled_code:
__int64 __fastcall cfg_get_string(__int64 a1, const char *a2, __int64 a3)
{
__int64 v3; // rbx
char *v5; // r12
unsigned __int8 v6; // bp
const __int32_t *v7; // rcx
char *v8; // rax

v3 = a1;
if ( a1 )
{
v5 = strdup(a2);
v6 = *v5;
if ( *v5 )
{
v7 = *__ctype_toupper_loc();
v8 = v5;
do
{
*v8++ = v7[v6];
v6 = *v8;
}
while ( *v8 );
}
while ( strcmp(*(const char **)v3, v5) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
{
free(v5);
return a3;
}
}
v3 = *(_QWORD *)(v3 + 8);
free(v5);
if ( !v3 )
v3 = a3;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, const char *a2, __int64 a3)
{
__int64 v3; // rbx
char *v5; // r12
unsigned __int8 v6; // bp
const __int32_t *v7; // rcx
char *v8; // rax

v3 = a1;
if ( a1 )
{
v5 = strdup(a2);
v6 = *v5;
if ( *v5 )
{
v7 = *__ctype_toupper_loc();
v8 = v5;
do
{
*v8++ = v7[v6];
v6 = *v8;
}
while ( *v8 );
}
while ( strcmp(*(const char **)v3, v5) )
{
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
{
free(v5);
return a3;
}
}
v3 = *(_QWORD *)(v3 + 8);
free(v5);
if ( !v3 )
v3 = a3;
}
return v3;
}

original_code:
char *
cfg_get_string(struct Config *cfg, char *key, char *default_value)
{
    if (cfg == ((void *)0)) {
        return ((void *)0);
    }
    char *upper_key = strdup(key);
    unsigned char *t = (unsigned char *)upper_key;
    while (*t) {
        *t = toupper(*t);
        t++;
    }
    struct Config *current_record = cfg;
    char *rc = ((void *)0);
    do {
        if (strcmp(current_record->key, upper_key) == 0) {
            rc = current_record->value;
            break;
        }
        if (current_record->next == ((void *)0)) {
            break;
        }
        current_record = current_record->next;
    } while (1);
    free(upper_key);
    if (rc == ((void *)0)) {
        return default_value;
    } else {
        return rc;
    }
}

canonical_original_code:
char *cfg_get_string(struct Config *cfg, char *key, char *default_value)
{
    if (cfg == ((void *)0)) {
        return ((void *)0);
    }
    char *upper_key = strdup(key);
    unsigned char *t = (unsigned char *)upper_key;
    while (*t) {
        *t = toupper(*t);
        t++;
    }
    struct Config *current_record = cfg;
    char *rc = ((void *)0);
    do {
        if (strcmp(current_record->key, upper_key) == 0) {
            rc = current_record->value;
            break;
        }
        if (current_record->next == ((void *)0)) {
            break;
        }
        current_record = current_record->next;
    } while (1);
    free(upper_key);
    if (rc == ((void *)0)) {
        return default_value;
    } else {
        return rc;
    }
}

variable_types: struct Config { char *key; char *value; struct Config *next; } *

variable_types:
key
char *

variable_types:
t
unsigned char *

variable_types:
rc
char *


decompiled_code:
__int64 __fastcall cfg_get_long(__int64 a1, const char *a2, int a3)
{
__int64 result; // rax
const char *v5; // rdi

result = a3;
if ( a1 )
{
v5 = (const char *)cfg_get_string(a1, a2, 4202510LL);
result = a3;
if ( v5 )
result = strtol(v5, 0LL, 0);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, const char *a2, int a3)
{
__int64 result; // rax
const char *v5; // rdi

result = a3;
if ( a1 )
{
v5 = (const char *)func12(a1, a2, 4202510LL);
result = a3;
if ( v5 )
result = strtol(v5, 0LL, 0);
}
return result;
}

original_code:
long
cfg_get_long(struct Config *cfg, char *key, int default_value)
{
    if (cfg == ((void *)0)) {
        return default_value;
    }
    char *value = cfg_get_string(cfg, key, "");
    if (!value) {
        return default_value;
    }
    return strtol(value, ((void *)0), 0);
}

variable_types:
value
char *

return_type: long

user_defined_types: struct Config { char *key; char *value; struct Config *next; }


decompiled_code:
void __fastcall cfg_free(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

v1 = (void **)ptr;
if ( ptr )
{
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[2];
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

v1 = (void **)ptr;
if ( ptr )
{
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[2];
free(v2);
}
while ( v1 );
}
}

original_code:
void
cfg_free(struct Config *cfg)
{
    if (cfg == ((void *)0)) return;
    struct Config *next = ((void *)0);
    do {
        free(cfg->key);
        free(cfg->value);
        next = cfg->next;
        free(cfg);
        if (next == ((void *)0)) break;
        cfg = next;
    } while(1);
}


main

canonical_name: main

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

v3 = (void *)cfg_init("settings.cfg");
v4 = (const char *)cfg_get_string(v3, "barney", "The default value");
printf("\"%s\"\n", v4);
v5 = (const char *)cfg_get_string(v3, "non-existent", "A default value");
printf("\"%s\"\n", v5);
v6 = cfg_get_long(v3, "NuMbEr", 69LL);
printf("Number is %ld\n", v6);
puts("DEBUG dump all:");
cfg_foreach(v3, callback);
cfg_free(v3);
return 0;
}

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

v3 = (void *)func11("settings.cfg");
v4 = (const char *)func12(v3, "barney", "The default value");
printf("\"%s\"\n", v4);
v5 = (const char *)func12(v3, "non-existent", "A default value");
printf("\"%s\"\n", v5);
v6 = func13(v3, "NuMbEr", 69LL);
printf("Number is %ld\n", v6);
puts("DEBUG dump all:");
func15(v3, callback);
func14(v3);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    struct Config *cfg = cfg_init("settings.cfg");
    char *value = cfg_get_string(cfg, "barney", "The default value");
    printf("\"%s\"\n", value);
    char *value2 = cfg_get_string(cfg, "non-existent", "A default value");
    printf("\"%s\"\n", value2);
    long num = cfg_get_long(cfg, "NuMbEr", 69);
    printf("Number is %ld\n", num);
    printf("DEBUG dump all:\n");
    cfg_foreach(cfg, callback);
    cfg_free(cfg);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: num long


testDone

repo: mdavidsaver/avr

decompiled_code:
_BOOL8 testDone()
{
putchar(10);
if ( nplan && nplan != npass + nfail )
printf("Planned %lu tests but ran %lu\n", nplan, npass + nfail);
printf("%lu test pass\n", npass);
if ( nfail )
printf("%lu tests failed!\n", nfail);
return nfail != 0;
}

canonical_decompiled_code:
_BOOL8 func1()
{
putchar(10);
if ( nplan && nplan != npass + nfail )
printf("Planned %lu tests but ran %lu\n", nplan, npass + nfail);
printf("%lu test pass\n", npass);
if ( nfail )
printf("%lu tests failed!\n", nfail);
return nfail != 0;
}

original_code:
static int testDone(void)
{
    printf("\n");
    if(nplan && npass+nfail!=nplan) {
        printf("Planned %lu tests but ran %lu\n",
               (unsigned long)nplan,
               (unsigned long)(npass+nfail));
    }
    printf("%lu test pass\n",
           (unsigned long)(npass));
    if(nfail) {
        printf("%lu tests failed!\n",
               (unsigned long)nfail);
    }
    return nfail!=0;
}

canonical_original_code:
static int testDone()
{
    printf("\n");
    if(nplan && npass+nfail!=nplan) {
        printf("Planned %lu tests but ran %lu\n",
               (unsigned long)nplan,
               (unsigned long)(npass+nfail));
    }
    printf("%lu test pass\n",
           (unsigned long)(npass));
    if(nfail) {
        printf("%lu tests failed!\n",
               (unsigned long)nfail);
    }
    return nfail!=0;
}


decompiled_code:
_BOOL8 __fastcall modbus_in_all(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rbx
_BYTE *v3; // rbp
_BOOL8 result; // rax

v2 = a1;
v3 = &a1[a2];
if ( a1 == &a1[a2] )
goto LABEL_4;
while ( (mbus_status & 6) == 0 )
{
mbus_in_byte = *v2++;
mbus_status |= 2u;
mbus_process();
if ( v3 == v2 )
goto LABEL_4;
}
if ( v3 == v2 )
LABEL_4:
result = (mbus_status & 6) != 0;
else
result = 1LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rbx
_BYTE *v3; // rbp
_BOOL8 result; // rax

v2 = a1;
v3 = &a1[a2];
if ( a1 == &a1[a2] )
goto LABEL_4;
while ( (mbus_status & 6) == 0 )
{
mbus_in_byte = *v2++;
mbus_status |= 2u;
mbus_process();
if ( v3 == v2 )
goto LABEL_4;
}
if ( v3 == v2 )
LABEL_4:
result = (mbus_status & 6) != 0;
else
result = 1LL;
return result;
}

original_code:
static
int modbus_in_all(uint8_t* data, size_t dsize)
{
    uint8_t *end=data+dsize;
    while(data!=end && !(mbus_status&(0x02|0x04)) ) {
        mbus_in_byte = *data++;
        mbus_status |= 0x02;
        mbus_process();
    }
    return data!=end || mbus_status&(0x02|0x04);
}

canonical_original_code:
static int modbus_in_all(unsigned char *data, unsigned long dsize)
{
    unsigned char *end = data+dsize;
    while(data!=end && !(mbus_status&(0x02|0x04)) ) {
        mbus_in_byte = *data++;
        mbus_status |= 0x02;
        mbus_process();
    }
    return data!=end || mbus_status&(0x02|0x04);
}

variable_types:
data
unsigned char *

variable_types:
dsize
unsigned long

variable_types:
end
unsigned char *


testDiag

decompiled_code:
int testDiag(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("# ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int func4(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf("# ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testDiag(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    printf("# ");
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

canonical_original_code:
void testDiag(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    printf("# ");
    vprintf(fmt, args);
    printf("\n");
    __builtin_va_end(args);
}

variable_types:
fmt
char *


testPassV

decompiled_code:
int __fastcall testPassV(char *format, __gnuc_va_list arg)
{
++npass;
printf("ok - ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func5(char *format, __gnuc_va_list arg)
{
++npass;
printf("ok - ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testPassV(const char* fmt, va_list args)
{
    npass++;
    printf("ok - ");
    vprintf(fmt, args);
    printf("\n");
}

canonical_original_code:
void testPassV(char *fmt, __builtin_va_list args)
{
    npass++;
    printf("ok - ");
    vprintf(fmt, args);
    printf("\n");
}


testFailV

decompiled_code:
int __fastcall testFailV(char *format, __gnuc_va_list arg)
{
++nfail;
printf("fail - ");
vprintf(format, arg);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func6(char *format, __gnuc_va_list arg)
{
++nfail;
printf("fail - ");
vprintf(format, arg);
return putchar(10);
}

original_code:
void testFailV(const char* fmt, va_list args)
{
    nfail++;
    printf("fail - ");
    vprintf(fmt, args);
    printf("\n");
}

canonical_original_code:
void testFailV(char *fmt, __builtin_va_list args)
{
    nfail++;
    printf("fail - ");
    vprintf(fmt, args);
    printf("\n");
}


testPass

decompiled_code:
int testPass(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return testPassV(a1, arg);
}

canonical_decompiled_code:
int func7(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return func5(a1, arg);
}

original_code:
void testPass(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    testPassV(fmt, args);
    __builtin_va_end(args);
}

canonical_original_code:
void testPass(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    testPassV(fmt, args);
    __builtin_va_end(args);
}


testFail

decompiled_code:
int testFail(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return testFailV(a1, arg);
}

canonical_decompiled_code:
int func8(char *a1, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a1);
return func6(a1, arg);
}

original_code:
void testFail(const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    testFailV(fmt, args);
    __builtin_va_end(args);
}

canonical_original_code:
void testFail(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    testFailV(fmt, args);
    __builtin_va_end(args);
}


testOk

decompiled_code:
__int64 testOk(unsigned int a1, char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 )
testPassV(a2, arg);
else
testFailV(a2, arg);
return a1;
}

canonical_decompiled_code:
__int64 func9(unsigned int a1, char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( a1 )
func5(a2, arg);
else
func6(a2, arg);
return a1;
}

original_code:
int testOk(int v, const char* fmt, ...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    if(v)
        testPassV(fmt, args);
    else
        testFailV(fmt, args);
    return v;
}

canonical_original_code:
int testOk(int v, char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    if(v)
        testPassV(fmt, args);
    else
        testFailV(fmt, args);
    return v;
}

variable_types: v int


testRead

decompiled_code:
int testRead()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

testDiag("Testing read holding registers (command 3)");
v0 = modbus_in_all(cmd_16, 8LL);
testOk(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
testOk(read_counter == 1, "read_counter==1");
testOk(write_counter == 0, "write_counter==0");
testOk(0x1234u, "read_addr=0x1234");
testOk(read_count == 4, "read_count==4");
v1 = modbus_out_all(rep_15, 20);
if ( !(unsigned int)testOk(v1 == 13, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
return testFail("Sizes don't match");
v2 = memcmp(rep_15, &expect_14, 0xDuLL);
return testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}

canonical_decompiled_code:
int func10()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

func4("Testing read holding registers (command 3)");
v0 = func2(cmd_16, 8LL);
func9(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
func9(read_counter == 1, "read_counter==1");
func9(write_counter == 0, "write_counter==0");
func9(0x1234u, "read_addr=0x1234");
func9(read_count == 4, "read_count==4");
v1 = func3(rep_15, 20);
if ( !(unsigned int)func9(v1 == 13, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
return func8("Sizes don't match");
v2 = memcmp(rep_15, &expect_14, 0xDuLL);
return func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}

original_code:
static void testRead(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x3, 0x8, 0x0, 0x1, 0x2, 0x3,
                               0x4, 0x5, 0x6, 0x7, 0xA6, 0x93};
    testDiag("Testing read holding registers (command 3)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    testOk(read_addr=0x1234, "read_addr=0x1234");
    testOk(read_count==4, "read_count==4");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
}

canonical_original_code:
static void testRead()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x3, 0x8, 0x0, 0x1, 0x2, 0x3,
                               0x4, 0x5, 0x6, 0x7, 0xA6, 0x93};
    testDiag("Testing read holding registers (command 3)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    testOk(read_addr=0x1234, "read_addr=0x1234");
    testOk(read_count==4, "read_count==4");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
}

variable_types:
cmd
unsigned char []

variable_types:
rep
unsigned char [20]

variable_types:
expect
unsigned char []


testWrite

decompiled_code:
int testWrite()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

testDiag("Testing write single holding register (command 6)");
v0 = modbus_in_all(cmd_13, 8LL);
testOk(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 1, "write_counter==1");
testOk(0x2143u, "write_addr=0x2143");
testOk(write_data == 22136, "write_data==0x5678");
v1 = modbus_out_all(rep_12, 20);
if ( !(unsigned int)testOk(v1 == 8, "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)") )
return testFail("Sizes don't match");
v2 = memcmp(rep_12, cmd_13, 8uLL);
return testOk(v2 == 0, "memcmp(rep, cmd, sizeof(cmd))==0");
}

canonical_decompiled_code:
int func11()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

func4("Testing write single holding register (command 6)");
v0 = func2(cmd_13, 8LL);
func9(!v0, "modbus_in_all(cmd, sizeof(cmd))==0");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 1, "write_counter==1");
func9(0x2143u, "write_addr=0x2143");
func9(write_data == 22136, "write_data==0x5678");
v1 = func3(rep_12, 20);
if ( !(unsigned int)func9(v1 == 8, "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)") )
return func8("Sizes don't match");
v2 = memcmp(rep_12, cmd_13, 8uLL);
return func9(v2 == 0, "memcmp(rep, cmd, sizeof(cmd))==0");
}

original_code:
static void testWrite(void)
{
    static uint8_t cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static uint8_t rep[20];
    testDiag("Testing write single holding register (command 6)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    testOk(write_addr=0x2143, "write_addr=0x2143");
    testOk(write_data==0x5678, "write_data==0x5678");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(cmd), "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)")) {
        testOk(memcmp(rep, cmd, sizeof(cmd))==0, "memcmp(rep, cmd, sizeof(cmd))==0");
    } else
        testFail("Sizes don't match");
}

canonical_original_code:
static void testWrite()
{
    static unsigned char cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static unsigned char rep[20];
    testDiag("Testing write single holding register (command 6)");
    testOk(modbus_in_all(cmd, sizeof(cmd))==0, "modbus_in_all(cmd, sizeof(cmd))==0");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    testOk(write_addr=0x2143, "write_addr=0x2143");
    testOk(write_data==0x5678, "write_data==0x5678");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(cmd), "modbus_out_all(rep, sizeof(rep))==sizeof(cmd)")) {
        testOk(memcmp(rep, cmd, sizeof(cmd))==0, "memcmp(rep, cmd, sizeof(cmd))==0");
    } else
        testFail("Sizes don't match");
}


testInvalidFunc

decompiled_code:
int testInvalidFunc()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

testDiag("Testing reception of an invalid function code");
v0 = modbus_in_all(cmd_11, 2LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all(rep_10, 20);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_10, &expect_9, 4uLL);
testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
testFail("Sizes don't match");
}
testDiag("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
testOk(mbus_status == 0, "mbus_status==0");
return testWrite();
}

canonical_decompiled_code:
int func12()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

func4("Testing reception of an invalid function code");
v0 = func2(cmd_11, 2LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 0, "write_counter==0");
v1 = func3(rep_10, 20);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_10, &expect_9, 4uLL);
func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
func8("Sizes don't match");
}
func4("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
func9(mbus_status == 0, "mbus_status==0");
return func11();
}

original_code:
static void testInvalidFunc(void)
{
    static uint8_t cmd[] = {0x1, 0x08};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x88, 0x1, 0x76};
    testDiag("Testing reception of an invalid function code");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}

canonical_original_code:
static void testInvalidFunc()
{
    static unsigned char cmd[] = {0x1, 0x08};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x88, 0x1, 0x76};
    testDiag("Testing reception of an invalid function code");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}


testBadCRC

decompiled_code:
int testBadCRC()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

testDiag("Testing reception of an message with bad CRC");
v0 = modbus_in_all(cmd_8, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all(rep_7, 20);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_7, &expect_6, 4uLL);
testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
testFail("Sizes don't match");
}
testDiag("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
testOk(mbus_status == 0, "mbus_status==0");
return testWrite();
}

canonical_decompiled_code:
int func13()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

func4("Testing reception of an message with bad CRC");
v0 = func2(cmd_8, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 0, "write_counter==0");
v1 = func3(rep_7, 20);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_7, &expect_6, 4uLL);
func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
func8("Sizes don't match");
}
func4("Make sure we can still process a valid message");
mbus_status &= 0xFBu;
func9(mbus_status == 0, "mbus_status==0");
return func11();
}

original_code:
static void testBadCRC(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xFF, 0xFF};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x83, 0x4, 0x78};
    testDiag("Testing reception of an message with bad CRC");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}

canonical_original_code:
static void testBadCRC()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xFF, 0xFF};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x83, 0x4, 0x78};
    testDiag("Testing reception of an message with bad CRC");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    testDiag("Make sure we can still process a valid message");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
    testWrite();
}


testUserReadError

decompiled_code:
__int64 testUserReadError()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

read_fail = 1;
testDiag("Testing user error on read");
v0 = modbus_in_all(cmd_5, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
read_fail = 0;
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 1, "read_counter==1");
testOk(write_counter == 0, "write_counter==0");
v1 = modbus_out_all(rep_4, 20);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_4, &expect_3, 4uLL);
testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
testFail("Sizes don't match");
}
mbus_status &= 0xFBu;
return testOk(mbus_status == 0, "mbus_status==0");
}

canonical_decompiled_code:
__int64 func14()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

read_fail = 1;
func4("Testing user error on read");
v0 = func2(cmd_5, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
read_fail = 0;
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 1, "read_counter==1");
func9(write_counter == 0, "write_counter==0");
v1 = func3(rep_4, 20);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_4, &expect_3, 4uLL);
func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
func8("Sizes don't match");
}
mbus_status &= 0xFBu;
return func9(mbus_status == 0, "mbus_status==0");
}

original_code:
static void testUserReadError(void)
{
    static uint8_t cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x83, 0x2, 0x0};
    read_fail = 1;
    testDiag("Testing user error on read");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    read_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}

canonical_original_code:
static void testUserReadError()
{
    static unsigned char cmd[] = {0x1, 0x3, 0x12, 0x34, 0x00, 0x4, 0xBF, 0x00};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x83, 0x2, 0x0};
    read_fail = 1;
    testDiag("Testing user error on read");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    read_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==1, "read_counter==1");
    testOk(write_counter==0, "write_counter==0");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}


testUserWriteError

decompiled_code:
__int64 testUserWriteError()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

write_fail = 1;
testDiag("Testing user error on write");
v0 = modbus_in_all(cmd_2, 8LL);
testOk(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
write_fail = 0;
testOk(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
testOk(read_counter == 0, "read_counter==0");
testOk(write_counter == 1, "write_counter==1");
v1 = modbus_out_all(rep_1, 20);
if ( (unsigned int)testOk(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_1, &expect_0, 4uLL);
testOk(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
testFail("Sizes don't match");
}
mbus_status &= 0xFBu;
return testOk(mbus_status == 0, "mbus_status==0");
}

canonical_decompiled_code:
__int64 func15()
{
_BOOL4 v0; // eax
int v1; // eax
int v2; // eax

write_fail = 1;
func4("Testing user error on write");
v0 = func2(cmd_2, 8LL);
func9(v0, "modbus_in_all(cmd, sizeof(cmd))==1");
write_fail = 0;
func9(mbus_status == 5, "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
func9(read_counter == 0, "read_counter==0");
func9(write_counter == 1, "write_counter==1");
v1 = func3(rep_1, 20);
if ( (unsigned int)func9(v1 == 4, "modbus_out_all(rep, sizeof(rep))==sizeof(expect)") )
{
v2 = memcmp(rep_1, &expect_0, 4uLL);
func9(v2 == 0, "memcmp(rep, expect, sizeof(expect))==0");
}
else
{
func8("Sizes don't match");
}
mbus_status &= 0xFBu;
return func9(mbus_status == 0, "mbus_status==0");
}

original_code:
static void testUserWriteError(void)
{
    static uint8_t cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static uint8_t rep[20];
    static uint8_t expect[] = {0x1, 0x86, 0x3, 0x76};
    write_fail = 1;
    testDiag("Testing user error on write");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    write_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}

canonical_original_code:
static void testUserWriteError()
{
    static unsigned char cmd[] = {0x1, 0x6, 0x21, 0x43, 0x56, 0x78, 0xA0, 0x4D};
    static unsigned char rep[20];
    static unsigned char expect[] = {0x1, 0x86, 0x3, 0x76};
    write_fail = 1;
    testDiag("Testing user error on write");
    testOk(modbus_in_all(cmd, sizeof(cmd))==1, "modbus_in_all(cmd, sizeof(cmd))==1");
    write_fail = 0;
    testOk(mbus_status==(0x04|0x01), "mbus_status==(MBUS_RX_ERROR|MBUS_TX_READY)");
    testOk(read_counter==0, "read_counter==0");
    testOk(write_counter==1, "write_counter==1");
    if(testOk(modbus_out_all(rep, sizeof(rep))==sizeof(expect), "modbus_out_all(rep, sizeof(rep))==sizeof(expect)")) {
        testOk(memcmp(rep, expect, sizeof(expect))==0, "memcmp(rep, expect, sizeof(expect))==0");
    } else
        testFail("Sizes don't match");
    mbus_status &= ~0x04;
    testOk(mbus_status==0, "mbus_status==0");
}


runtests

decompiled_code:
__int64 __fastcall runtests(int a1)
{
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testRead();
if ( a1 )
{
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testWrite();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testInvalidFunc();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testBadCRC();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
testUserReadError();
mbus_reset();
testOk(mbus_status == 0, "mbus_status==0");
}
else
{
write_counter = 0LL;
read_counter = 0LL;
testWrite();
write_counter = 0LL;
read_counter = 0LL;
testInvalidFunc();
write_counter = 0LL;
read_counter = 0LL;
testBadCRC();
write_counter = 0LL;
read_counter = 0LL;
testUserReadError();
}
write_counter = 0LL;
read_counter = 0LL;
return testUserWriteError();
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func10();
if ( a1 )
{
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func11();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func12();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func13();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
write_counter = 0LL;
read_counter = 0LL;
func14();
mbus_reset();
func9(mbus_status == 0, "mbus_status==0");
}
else
{
write_counter = 0LL;
read_counter = 0LL;
func11();
write_counter = 0LL;
read_counter = 0LL;
func12();
write_counter = 0LL;
read_counter = 0LL;
func13();
write_counter = 0LL;
read_counter = 0LL;
func14();
}
write_counter = 0LL;
read_counter = 0LL;
return func15();
}

original_code:
static void runtests(int reset)
{
    mbus_reset();
    testOk(mbus_status==0, "mbus_status==0");
    read_counter = write_counter = 0;
    testRead();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testWrite();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testInvalidFunc();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testBadCRC();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testUserReadError();
    if(reset) {mbus_reset(); testOk(mbus_status==0, "mbus_status==0");}
    read_counter = write_counter = 0;
    testUserWriteError();
}

variable_types: reset int


decompiled_code:
void __fastcall mbus_read_holding(__int64 a1, unsigned __int8 a2, _WORD *a3)
{
_WORD *v3; // rax
__int16 v4; // cx

++read_counter;
read_count = a2;
if ( a2 )
{
v3 = a3;
v4 = 0;
do
{
*v3 = v4 | (1 - (_WORD)a3 + (_WORD)v3);
v4 += 512;
++v3;
}
while ( v3 != &a3[a2] );
}
if ( read_fail )
mbus_exception(2LL);
}

canonical_decompiled_code:
void __fastcall func18(__int64 a1, unsigned __int8 a2, _WORD *a3)
{
_WORD *v3; // rax
__int16 v4; // cx

++read_counter;
read_count = a2;
if ( a2 )
{
v3 = a3;
v4 = 0;
do
{
*v3 = v4 | (1 - (_WORD)a3 + (_WORD)v3);
v4 += 512;
++v3;
}
while ( v3 != &a3[a2] );
}
if ( read_fail )
mbus_exception(2LL);
}

original_code:
void mbus_read_holding(uint16_t addr, uint8_t count, uint16_t * restrict result)
{
    int i;
    read_counter++;
    read_addr = addr;
    read_count = count;
    for(i=0; i<count; i++) {
        result[i]=(2*i+1)|(2*i<<8);
    }
    if(read_fail)
        mbus_exception(2);
}

canonical_original_code:
void mbus_read_holding(unsigned short addr, unsigned char count, unsigned short *result)
{
    int i;
    read_counter++;
    read_addr = addr;
    read_count = count;
    for(i=0; i<count; i++) {
        result[i]=(2*i+1)|(2*i<<8);
    }
    if(read_fail)
        mbus_exception(2);
}

variable_types:
addr
unsigned short

variable_types:
count
unsigned char

variable_types:
result
unsigned short *

variable_types: i int


decompiled_code:
__int64 __fastcall mbus_write_holding(__int64 a1, __int16 a2)
{
__int64 result; // rax

++write_counter;
write_data = a2;
if ( write_fail )
result = mbus_exception(3LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, __int16 a2)
{
__int64 result; // rax

++write_counter;
write_data = a2;
if ( write_fail )
result = mbus_exception(3LL);
return result;
}

original_code:
void mbus_write_holding(uint16_t addr, uint16_t value)
{
    write_counter++;
    write_addr = addr;
    write_data = value;
    if(write_fail)
        mbus_exception(3);
}

canonical_original_code:
void mbus_write_holding(unsigned short addr, unsigned short value)
{
    write_counter++;
    write_addr = addr;
    write_data = value;
    if(write_fail)
        mbus_exception(3);
}

variable_types:
value
unsigned short


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
testPlan(119LL);
testDiag("run and reset state between tests", argv);
runtests(1);
testDiag("run tests w/o resets");
runtests(0);
return testDone();
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func0(119LL);
func4("run and reset state between tests", argv);
func16(1);
func4("run tests w/o resets");
func16(0);
return func1();
}

original_code:
int main(int argc, char** argv)
{
    testPlan(119);
    testDiag("run and reset state between tests");
    runtests(1);
    testDiag("run tests w/o resets");
    runtests(0);
    return testDone();
}

canonical_original_code:
int main(int argc, char **argv)
{
    testPlan(119);
    testDiag("run and reset state between tests");
    runtests(1);
    testDiag("run tests w/o resets");
    runtests(0);
    return testDone();
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 mbus_transmit()
{
char v0; // cl
__int64 result; // rax

if ( (mbus_status & 1) == 0 )
{
v0 = *((_BYTE *)&buf + (unsigned __int8)buf_pos);
mbus_status |= 1u;
mbus_out_byte = v0;
++buf_pos;
}
result = (unsigned __int8)buf_cnt;
if ( buf_pos == buf_cnt )
{
mb_state &= 0xFEu;
buf_pos = 0;
buf_cnt = 8;
}
return result;
}

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

if ( (mbus_status & 1) == 0 )
{
v0 = *((_BYTE *)&buf + (unsigned __int8)buf_pos);
mbus_status |= 1u;
mbus_out_byte = v0;
++buf_pos;
}
result = (unsigned __int8)buf_cnt;
if ( buf_pos == buf_cnt )
{
mb_state &= 0xFEu;
buf_pos = 0;
buf_cnt = 8;
}
return result;
}

original_code:
static void mbus_transmit(void)
{
    uint8_t sts, bpos = buf_pos, next=buf.b_b[bpos];
    {
        sts = mbus_status;
        if(!(sts&0x01)) {
            mbus_status = sts|0x01;
            mbus_out_byte = next;
        }
    }
    if(!(sts&0x01)) {
        bpos++;
        buf_pos=bpos;
    }
    if(buf_pos==buf_cnt) {
        mb_state &= ~1;
        buf_pos = 0;
        buf_cnt = 8;
    }
}

canonical_original_code:
static void mbus_transmit()
{
    unsigned char sts; unsigned char bpos = buf_pos; unsigned char next = buf.b_b[bpos];
    {
        sts = mbus_status;
        if(!(sts&0x01)) {
            mbus_status = sts|0x01;
            mbus_out_byte = next;
        }
    }
    if(!(sts&0x01)) {
        bpos++;
        buf_pos=bpos;
    }
    if(buf_pos==buf_cnt) {
        mb_state &= ~1;
        buf_pos = 0;
        buf_cnt = 8;
    }
}

variable_types:
sts
unsigned char

variable_types:
bpos
unsigned char

variable_types:
next
unsigned char


decompiled_code:
void mbus_rx_clear()
{
buf_cnt = 8;
buf_pos = 0;
}

canonical_decompiled_code:
void func3()
{
buf_cnt = 8;
buf_pos = 0;
}

original_code:
void mbus_rx_clear(void)
{
    buf_cnt = 8;
    buf_pos = 0;
}


decompiled_code:
void mbus_reset()
{
mbus_rx_clear();
mbus_status = 0;
mbus_out_byte = 0;
mbus_in_byte = 0;
err_cnt = 0;
buf = 0LL;
qword_368 = 0LL;
qword_370 = 0LL;
word_378 = 0;
}

canonical_decompiled_code:
void func4()
{
func3();
mbus_status = 0;
mbus_out_byte = 0;
mbus_in_byte = 0;
err_cnt = 0;
buf = 0LL;
qword_368 = 0LL;
qword_370 = 0LL;
word_378 = 0;
}

original_code:
void mbus_reset(void)
{
    {
        mbus_rx_clear();
        mbus_status = 0;
        mbus_in_byte = mbus_out_byte = 0;
        err_cnt = 0;
        memset(buf.b_b, 0, sizeof(buf));
    }
}

canonical_original_code:
void mbus_reset()
{
    {
        mbus_rx_clear();
        mbus_status = 0;
        mbus_in_byte = mbus_out_byte = 0;
        err_cnt = 0;
        memset(buf.b_b, 0, sizeof(buf));
    }
}


decompiled_code:
__int64 __fastcall calculate_crc(unsigned __int8 *a1, char a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v2 = a1;
LOWORD(result) = -1;
do
result = crc16_update((unsigned __int16)result, *v2++);
while ( v2 != &a1[(unsigned __int8)(a2 - 1) + 1] );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int8 *a1, char a2)
{
unsigned __int8 *v2; // rbx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v2 = a1;
LOWORD(result) = -1;
do
result = crc16_update((unsigned __int16)result, *v2++);
while ( v2 != &a1[(unsigned __int8)(a2 - 1) + 1] );
return result;
}

original_code:
uint16_t calculate_crc(const uint8_t* d, uint8_t c)
{
    uint16_t r=0xffff;
    while(c--)
        r=_crc16_update(r, *d++);
    return r;
}

canonical_original_code:
unsigned short calculate_crc(unsigned char *d, unsigned char c)
{
    unsigned short r = 0xffff;
    while(c--)
        r=_crc16_update(r, *d++);
    return r;
}

variable_types:
d
unsigned char *

variable_types:
c
unsigned char

variable_types:
r
unsigned short

return_type: unsigned short


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

BYTE1(buf) |= 0x80u;
BYTE2(buf) = a1;
BYTE3(buf) = -(char)(a1 + buf + BYTE1(buf));
buf_cnt = 4;
mb_state |= 1u;
++err_cnt;
result = (unsigned __int8)mbus_status | 4u;
mbus_status |= 4u;
return result;
}

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

BYTE1(buf) |= 0x80u;
BYTE2(buf) = a1;
BYTE3(buf) = -(char)(a1 + buf + BYTE1(buf));
buf_cnt = 4;
mb_state |= 1u;
++err_cnt;
result = (unsigned __int8)mbus_status | 4u;
mbus_status |= 4u;
return result;
}

original_code:
void mbus_exception(uint8_t code)
{
    uint8_t sum= buf.b_p.node;
    buf.b_p.function |= 0x80;
    sum += buf.b_p.function;
    buf.b_p.mb_e.code = code;
    sum += code;
    buf.b_p.mb_e.lrc = (~sum)+1;
    buf_cnt = 4;
    mb_state |= 1;
    err_cnt++;
    {
        mbus_status |= 0x04;
    }
}

canonical_original_code:
void mbus_exception(unsigned char code)
{
    unsigned char sum = buf.b_p.node;
    buf.b_p.function |= 0x80;
    sum += buf.b_p.function;
    buf.b_p.mb_e.code = code;
    sum += code;
    buf.b_p.mb_e.lrc = (~sum)+1;
    buf_cnt = 4;
    mb_state |= 1;
    err_cnt++;
    {
        mbus_status |= 0x04;
    }
}

variable_types:
code
unsigned char

variable_types:
sum
unsigned char


decompiled_code:
__int16 mbus_dispatch()
{
__int16 v0; // bx
__int16 v1; // ax
unsigned __int8 v2; // r12
__int16 result; // ax
__int64 v4; // rbx
unsigned __int16 v5; // ax
unsigned __int16 v6; // ax
unsigned __int16 v7; // bx
unsigned __int16 v8; // ax

v0 = calculate_crc((unsigned __int8 *)&buf, buf_cnt - 2);
if ( (unsigned __int16)_uint16_identity(HIWORD(buf)) == v0 )
{
if ( BYTE1(buf) == 3 )
{
v1 = _bswap_16(SWORD2(buf));
v2 = v1;
if ( (unsigned __int16)v1 <= 0xAu )
{
v6 = _bswap_16(SWORD1(buf));
mbus_read_holding(v6, v2, (char *)&buf + 3);
}
else
{
mbus_exception(3);
}
result = v2;
v4 = 0LL;
if ( v2 )
{
do
{
result = _bswap_16(*(_WORD *)((char *)&buf + v4 + 3));
*(_WORD *)((char *)&buf + v4 + 3) = result;
v4 += 2LL;
}
while ( v4 != 2LL * v2 );
}
if ( (mb_state & 1) == 0 )
{
BYTE2(buf) = 2 * v2;
v5 = calculate_crc((unsigned __int8 *)&buf, 2 * v2 + 3);
result = _uint16_identity(v5);
*(_WORD *)((char *)&buf + 2 * v2 + 3) = result;
buf_cnt = 2 * v2 + 5;
}
}
else
{
v7 = _bswap_16(SWORD2(buf));
v8 = _bswap_16(SWORD1(buf));
result = mbus_write_holding(v8, v7);
}
}
else
{
result = mbus_exception(4);
}
mb_state |= 1u;
return result;
}

canonical_decompiled_code:
__int16 func7()
{
__int16 v0; // bx
__int16 v1; // ax
unsigned __int8 v2; // r12
__int16 result; // ax
__int64 v4; // rbx
unsigned __int16 v5; // ax
unsigned __int16 v6; // ax
unsigned __int16 v7; // bx
unsigned __int16 v8; // ax

v0 = func5((unsigned __int8 *)&buf, buf_cnt - 2);
if ( (unsigned __int16)func0(HIWORD(buf)) == v0 )
{
if ( BYTE1(buf) == 3 )
{
v1 = func2(SWORD2(buf));
v2 = v1;
if ( (unsigned __int16)v1 <= 0xAu )
{
v6 = func2(SWORD1(buf));
mbus_read_holding(v6, v2, (char *)&buf + 3);
}
else
{
func6(3);
}
result = v2;
v4 = 0LL;
if ( v2 )
{
do
{
result = func2(*(_WORD *)((char *)&buf + v4 + 3));
*(_WORD *)((char *)&buf + v4 + 3) = result;
v4 += 2LL;
}
while ( v4 != 2LL * v2 );
}
if ( (mb_state & 1) == 0 )
{
BYTE2(buf) = 2 * v2;
v5 = func5((unsigned __int8 *)&buf, 2 * v2 + 3);
result = func0(v5);
*(_WORD *)((char *)&buf + 2 * v2 + 3) = result;
buf_cnt = 2 * v2 + 5;
}
}
else
{
v7 = func2(SWORD2(buf));
v8 = func2(SWORD1(buf));
result = mbus_write_holding(v8, v7);
}
}
else
{
result = func6(4);
}
mb_state |= 1u;
return result;
}

original_code:
static void mbus_dispatch(void)
{
    uint16_t crc=calculate_crc(buf.b_b, buf_cnt-2);
    if(__uint16_identity (buf.b_p.mb_s.crc)!=crc) {
        mbus_exception(4);
    } else if(buf.b_p.function==3) {
        uint16_t count = __bswap_16 (buf.b_p.mb_s.data);
        uint8_t cnt = count;
        if(count>20/2)
            mbus_exception(3);
        else
            mbus_read_holding(__bswap_16 (buf.b_p.mb_s.addr),
                              cnt,
                              buf.b_p.mb_m.data);
        {
            size_t i;
            for(i=0; i<cnt; i++)
                buf.b_p.mb_m.data[i] = __bswap_16 (buf.b_p.mb_m.data[i]);
        }
        if(!(mb_state&1)) {
            buf.b_p.mb_m.count = 2*cnt;
            buf.b_p.mb_m.data[cnt] = __uint16_identity (calculate_crc(buf.b_b, 3+2*cnt));
            buf_cnt = 5+2*cnt;
        }
    } else {
        mbus_write_holding(__bswap_16 (buf.b_p.mb_s.addr),
                           __bswap_16 (buf.b_p.mb_s.data));
    }
    mb_state |= 1;
}

canonical_original_code:
static void mbus_dispatch()
{
    unsigned short crc = calculate_crc(buf.b_b, buf_cnt-2);
    if(__uint16_identity (buf.b_p.mb_s.crc)!=crc) {
        mbus_exception(4);
    } else if(buf.b_p.function==3) {
        unsigned short count = __bswap_16 (buf.b_p.mb_s.data);
        unsigned char cnt = count;
        if(count>20/2)
            mbus_exception(3);
        else
            mbus_read_holding(__bswap_16 (buf.b_p.mb_s.addr),
                              cnt,
                              buf.b_p.mb_m.data);
        {
            unsigned long i;
            for(i=0; i<cnt; i++)
                buf.b_p.mb_m.data[i] = __bswap_16 (buf.b_p.mb_m.data[i]);
        }
        if(!(mb_state&1)) {
            buf.b_p.mb_m.count = 2*cnt;
            buf.b_p.mb_m.data[cnt] = __uint16_identity (calculate_crc(buf.b_b, 3+2*cnt));
            buf_cnt = 5+2*cnt;
        }
    } else {
        mbus_write_holding(__bswap_16 (buf.b_p.mb_s.addr),
                           __bswap_16 (buf.b_p.mb_s.data));
    }
    mb_state |= 1;
}

variable_types:
crc
unsigned short

variable_types:
count
unsigned short

variable_types:
cnt
unsigned char

variable_types:
i
unsigned long


decompiled_code:
void mbus_recieve()
{
char v0; // al
char v1; // bl

v0 = mbus_status;
mbus_status &= 0xFDu;
if ( (v0 & 2) != 0 )
{
v1 = buf_pos + 1;
*((_BYTE *)&buf + (unsigned __int8)buf_pos) = mbus_in_byte;
if ( buf_cnt == v1 )
{
mbus_dispatch();
}
else if ( v1 == 2 && BYTE1(buf) != 3 && BYTE1(buf) != 6 )
{
mbus_exception(1);
}
if ( (mb_state & 1) != 0 )
{
buf_pos = 1;
mbus_status |= 1u;
mbus_out_byte = buf;
}
else
{
buf_pos = v1;
}
}
else
{
mbus_rx_clear();
}
}

canonical_decompiled_code:
void func8()
{
char v0; // al
char v1; // bl

v0 = mbus_status;
mbus_status &= 0xFDu;
if ( (v0 & 2) != 0 )
{
v1 = buf_pos + 1;
*((_BYTE *)&buf + (unsigned __int8)buf_pos) = mbus_in_byte;
if ( buf_cnt == v1 )
{
func7();
}
else if ( v1 == 2 && BYTE1(buf) != 3 && BYTE1(buf) != 6 )
{
func6(1);
}
if ( (mb_state & 1) != 0 )
{
buf_pos = 1;
mbus_status |= 1u;
mbus_out_byte = buf;
}
else
{
buf_pos = v1;
}
}
else
{
func3();
}
}

original_code:
static void mbus_recieve(void)
{
    uint8_t next, sts;
    {
        sts = mbus_status;
        mbus_status = sts & ~0x02;
        next = mbus_in_byte;
    }
    if(!(sts&0x02)) {
        mbus_rx_clear();
        return;
    }
    uint8_t bpos = buf_pos;
    buf.b_b[bpos++] = next;
    if(bpos==buf_cnt) {
        mbus_dispatch();
    } else if(bpos==2) {
        if(buf.b_p.function!=3 && buf.b_p.function!=6) {
            mbus_exception(1);
        }
    }
    if(mb_state&1) {
        buf_pos = 1;
        {
            mbus_status |= 0x01;
            mbus_out_byte = buf.b_b[0];
        }
    }else
        buf_pos = bpos;
}

canonical_original_code:
static void mbus_recieve()
{
    unsigned char next; unsigned char sts;
    {
        sts = mbus_status;
        mbus_status = sts & ~0x02;
        next = mbus_in_byte;
    }
    if(!(sts&0x02)) {
        mbus_rx_clear();
        return;
    }
    unsigned char bpos = buf_pos;
    buf.b_b[bpos++] = next;
    if(bpos==buf_cnt) {
        mbus_dispatch();
    } else if(bpos==2) {
        if(buf.b_p.function!=3 && buf.b_p.function!=6) {
            mbus_exception(1);
        }
    }
    if(mb_state&1) {
        buf_pos = 1;
        {
            mbus_status |= 0x01;
            mbus_out_byte = buf.b_b[0];
        }
    }else
        buf_pos = bpos;
}


decompiled_code:
void mbus_process()
{
if ( (mb_state & 1) != 0 )
mbus_transmit();
else
mbus_recieve();
}

canonical_decompiled_code:
void func9()
{
if ( (mb_state & 1) != 0 )
func1();
else
func8();
}

original_code:
void mbus_process(void)
{
    if(mb_state&1)
        mbus_transmit();
    else
        mbus_recieve();
}

canonical_original_code:
void mbus_process()
{
    if(mb_state&1)
        mbus_transmit();
    else
        mbus_recieve();
}


repo: mdcja/prometheus

decompiled_code:
__int64 __fastcall generate_prime(__int64 a1)
{
_DWORD *v1; // rdx
__int64 result; // rax
unsigned int v3; // ebx
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
if ( (unsigned int)a1 <= 2 )
{
error_code = 5;
error_print("generate_prime");
result = 0LL;
}
else
{
v1 = sieve_eratosthenes(a1, v4);
result = 0LL;
if ( v4[0] > 0 )
{
if ( (int)a1 <= 2 )
{
v3 = 0;
}
else
{
a1 = (int)a1;
while ( 1 )
{
v3 = a1;
if ( !v1[a1] )
break;
if ( (int)--a1 <= 2 )
{
v3 = 0;
break;
}
}
}
free(v1);
result = v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_DWORD *v1; // rdx
__int64 result; // rax
unsigned int v3; // ebx
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
if ( (unsigned int)a1 <= 2 )
{
error_code = 5;
error_print("generate_prime");
result = 0LL;
}
else
{
v1 = sieve_eratosthenes(a1, v4);
result = 0LL;
if ( v4[0] > 0 )
{
if ( (int)a1 <= 2 )
{
v3 = 0;
}
else
{
a1 = (int)a1;
while ( 1 )
{
v3 = a1;
if ( !v1[a1] )
break;
if ( (int)--a1 <= 2 )
{
v3 = 0;
break;
}
}
}
free(v1);
result = v3;
}
}
return result;
}

original_code:
unsigned long generate_prime( unsigned int limit )
{
    int largest_prime = 0;
    int prime_count = 0;
    int * sieve = ((void *)0);
    int i = 0;
    if( limit <= 2 )
    {
        error_code = ERROR_INVALID_CAPACITY;
        error_print( "generate_prime" );
        return 0;
    }
    sieve = sieve_eratosthenes( limit, &prime_count );
    if( prime_count <= 0 )
    {
        return 0;
    }
    for( i = limit; i > 2; --i )
    {
        if( sieve[ i ] == 0 )
        {
            largest_prime = i;
            break;
        }
    }
    free( sieve );
    sieve = ((void *)0);
    return (unsigned int)largest_prime;
}

canonical_original_code:
unsigned long generate_prime(unsigned int limit)
{
    int largest_prime = 0;
    int prime_count = 0;
    int *sieve = ((void *)0);
    int i = 0;
    if( limit <= 2 )
    {
        error_code = ERROR_INVALID_CAPACITY;
        error_print( "generate_prime" );
        return 0;
    }
    sieve = sieve_eratosthenes( limit, &prime_count );
    if( prime_count <= 0 )
    {
        return 0;
    }
    for( i = limit; i > 2; --i )
    {
        if( sieve[ i ] == 0 )
        {
            largest_prime = i;
            break;
        }
    }
    free( sieve );
    sieve = ((void *)0);
    return (unsigned int)largest_prime;
}

variable_types:
limit
unsigned int

variable_types:
sieve
int *

return_type: unsigned long


decompiled_code:
__int64 __fastcall random_number(int a1, int a2)
{
unsigned int v2; // eax

v2 = time(0LL);
srand(v2);
return (unsigned int)(rand() % (a2 - a1) + a1);
}

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

v2 = time(0LL);
srand(v2);
return (unsigned int)(rand() % (a2 - a1) + a1);
}

original_code:
int random_number( int min, int max )
{
    static int is_seeded = 0;
    if( !is_seeded )
    {
        srand( time( ((void *)0) ) );
    }
    return (rand() % ( max - min )) + min;
}

canonical_original_code:
int random_number(int min, int max)
{
    static int is_seeded = 0;
    if( !is_seeded )
    {
        srand( time( ((void *)0) ) );
    }
    return (rand() % ( max - min )) + min;
}

variable_types: min int

variable_types: max int


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

switch ( a1 )
{
case 0:
result = "no error";
break;
case 2:
result = "memory allocation failed";
break;
case 3:
result = "null parameter";
break;
case 4:
result = "not found";
break;
case 5:
result = "invalid capacity";
break;
case 6:
result = "not implemented";
break;
case 7:
result = "overflow";
break;
case 8:
result = "underflow";
break;
default:
result = "unknown";
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
result = "no error";
break;
case 2:
result = "memory allocation failed";
break;
case 3:
result = "null parameter";
break;
case 4:
result = "not found";
break;
case 5:
result = "invalid capacity";
break;
case 6:
result = "not implemented";
break;
case 7:
result = "overflow";
break;
case 8:
result = "underflow";
break;
default:
result = "unknown";
break;
}
return result;
}

original_code:
static char * error_name( error_t error )
{
    static char * error_names[ 256 ] = {
        "no error",
        "unknown",
        "memory allocation failed",
        "null parameter",
        "not found",
        "invalid capacity",
        "not implemented",
        "overflow",
        "underflow"
    };
    switch( error )
    {
    case ERROR_NO_ERROR:
        return error_names[ ERROR_NO_ERROR ];
    case ERROR_MEMORY_ALLOCATION_FAILED:
        return error_names[ ERROR_MEMORY_ALLOCATION_FAILED ];
    case ERROR_NULL_PARAMETER:
        return error_names[ ERROR_NULL_PARAMETER ];
    case ERROR_NOT_FOUND:
        return error_names[ ERROR_NOT_FOUND ];
    case ERROR_INVALID_CAPACITY:
        return error_names[ ERROR_INVALID_CAPACITY ];
    case ERROR_NOT_IMPLEMENTED:
        return error_names[ ERROR_NOT_IMPLEMENTED ];
    case ERROR_OVERFLOW:
        return error_names[ ERROR_OVERFLOW ];
    case ERROR_UNDERFLOW:
        return error_names[ ERROR_UNDERFLOW ];
    default:
        return error_names[ ERROR_UNKNOWN ];
    }
}

canonical_original_code:
static char *error_name(enum _errors error)
{
    static char *error_names[256] = {
        "no error",
        "unknown",
        "memory allocation failed",
        "null parameter",
        "not found",
        "invalid capacity",
        "not implemented",
        "overflow",
        "underflow"
    };
    switch( error )
    {
    case ERROR_NO_ERROR:
        return error_names[ ERROR_NO_ERROR ];
    case ERROR_MEMORY_ALLOCATION_FAILED:
        return error_names[ ERROR_MEMORY_ALLOCATION_FAILED ];
    case ERROR_NULL_PARAMETER:
        return error_names[ ERROR_NULL_PARAMETER ];
    case ERROR_NOT_FOUND:
        return error_names[ ERROR_NOT_FOUND ];
    case ERROR_INVALID_CAPACITY:
        return error_names[ ERROR_INVALID_CAPACITY ];
    case ERROR_NOT_IMPLEMENTED:
        return error_names[ ERROR_NOT_IMPLEMENTED ];
    case ERROR_OVERFLOW:
        return error_names[ ERROR_OVERFLOW ];
    case ERROR_UNDERFLOW:
        return error_names[ ERROR_UNDERFLOW ];
    default:
        return error_names[ ERROR_UNKNOWN ];
    }
}

variable_types: enum _errors { ERROR_NO_ERROR=0, ERROR_UNKNOWN=1, ERROR_MEMORY_ALLOCATION_FAILED=2, ERROR_NULL_PARAMETER=3, ERROR_NOT_FOUND=4, ERROR_INVALID_CAPACITY=5, ERROR_NOT_IMPLEMENTED=6, ERROR_OVERFLOW=7, ERROR_UNDERFLOW=8 }


decompiled_code:
// attributes: thunk
const char *__fastcall error_get_message(int a1)
{
return error_name(a1);
}

canonical_decompiled_code:
const char *__fastcall func1(int a1)
{
return func0(a1);
}

original_code:
inline char * error_get_message( int error )
{
    return error_name( error );
}

canonical_original_code:
inline char *error_get_message(int error)
{
    return error_name( error );
}

variable_types: error int


decompiled_code:
int __fastcall error_print(const char *a1)
{
unsigned int v1; // ebp
const char *v2; // rax

v1 = error_code;
v2 = error_name(error_code);
return fprintf(stderr, "ERROR: (%d) %s : %s\n", v1, v2, a1);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
unsigned int v1; // ebp
const char *v2; // rax

v1 = error_code;
v2 = func0(error_code);
return fprintf(stderr, "ERROR: (%d) %s : %s\n", v1, v2, a1);
}

original_code:
int error_print( const char * msg )
{
    int nchr = 0;
    nchr += fprintf( stderr, "ERROR: (%d) %s : %s\n", error_code, error_name( error_code), msg );
    return nchr;
}

canonical_original_code:
int error_print(char *msg)
{
    int nchr = 0;
    nchr += fprintf( stderr, "ERROR: (%d) %s : %s\n", error_code, error_name( error_code), msg );
    return nchr;
}

variable_types:
msg
char *

variable_types: nchr int


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

if ( a1 == a2 )
result = 0LL;
else
result = a1 < a2 ? -1 : 1;
return result;
}

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

if ( a1 == a2 )
result = 0LL;
else
result = a1 < a2 ? -1 : 1;
return result;
}

original_code:
static int default_compare( const void * data1, const void * data2 )
{
    if( data1 == data2 )
    {
        return 0;
    }
    else if( data1 < data2 )
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

canonical_original_code:
static int default_compare(void *data1, void *data2)
{
    if( data1 == data2 )
    {
        return 0;
    }
    else if( data1 < data2 )
    {
        return -1;
    }
    else
    {
        return 1;
    }
}


decompiled_code:
__int64 __fastcall default_copy(__int64 a1)
{
return a1;
}

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

original_code:
static void * default_copy( const void * data )
{
    return (void *)data;
}

canonical_original_code:
static void *default_copy(void *data)
{
    return (void *)data;
}

variable_types:
data
void *

return_type: void *


decompiled_code:
void default_free()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
static void default_free( void * data )
{
    return;
}

canonical_original_code:
static void default_free(void *data)
{
    return;
}


decompiled_code:
__int64 __fastcall default_hash(int *a1)
{
return (32LL * *a1) ^ 1;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1)
{
return (32LL * *a1) ^ 1;
}

original_code:
static unsigned long default_hash( const void * key )
{
    unsigned long hash = *(int*)key;
    unsigned long high_order = 0;
    high_order = hash & 0xf8000000;
    hash = hash << 5;
    hash = hash ^ ( high_order >> 27 );
    hash = hash ^ 1;
    return hash;
}

canonical_original_code:
static unsigned long default_hash(void *key)
{
    unsigned long hash = *(int*)key;
    unsigned long high_order = 0;
    high_order = hash & 0xf8000000;
    hash = hash << 5;
    hash = hash ^ ( high_order >> 27 );
    hash = hash ^ 1;
    return hash;
}

variable_types:
key
void *

variable_types:
hash
unsigned long