decompiled_code:
char execute_command()
{
char v0; // bh
char result; // al
bool v2; // zf
unsigned int pid; // [rsp+Ch] [rbp-18Ch]
char v4[120]; // [rsp+10h] [rbp-188h] BYREF
__int64 v5; // [rsp+88h] [rbp-110h]
struct rusage usage; // [rsp+F0h] [rbp-A8h] BYREF
__WAIT_STATUS stat_loc; // [rsp+18Ch] [rbp-Ch] BYREF

pid = fork();
if ( !pid )
setup_child();
wait3((__WAIT_STATUS)&stat_loc, 0, &usage);
v0 = BYTE1(stat_loc.__uptr);
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
if ( LOBYTE(stat_loc.__uptr) == 127 )
{
ptrace(PTRACE_GETREGS, pid, 0LL, v4);
if ( v0 == 5 && v5 == 59 )
{
signal(14, alarm_func);
if ( setjmp(jbuf) )
{
kill(pid, 9);
*(_DWORD *)(::result + 4LL) = ltime;
result = ::result;
*::result = 4;
}
else
{
alarm(ltime / 1000 + 2);
ptrace(PTRACE_SYSCALL, pid, 0LL, 0LL);
trace_child(pid);
alarm(0);
v2 = strcmp(lang, "python") == 0;
result = !v2;
if ( v2 )
result = python_filter();
}
}
else
{
kill(pid, 9);
*::result = 7;
result = g_string_assign(*(_QWORD *)(::result + 24LL), "unexpected child stop before execve");
}
}
else
{
kill(pid, 9);
*::result = 7;
result = g_string_assign(*(_QWORD *)(::result + 24LL), "unknow child status before execve");
}
}
else
{
switch ( BYTE1(stat_loc.__uptr) )
{
case 0:
g_string_assign(*(_QWORD *)(::result + 24LL), "drop privileges error");
break;
case 1:
g_string_assign(*(_QWORD *)(::result + 24LL), "execvpe error");
break;
case 2:
g_string_assign(*(_QWORD *)(::result + 24LL), "open input file error");
break;
case 3:
g_string_assign(*(_QWORD *)(::result + 24LL), "open output file error");
break;
case 4:
g_string_assign(*(_QWORD *)(::result + 24LL), "dup2 input error");
break;
case 5:
g_string_assign(*(_QWORD *)(::result + 24LL), "dup2 ouput error");
break;
default:
break;
}
result = ::result;
*::result = 7;
}
return result;
}

canonical_decompiled_code:
char func8()
{
char v0; // bh
char result; // al
bool v2; // zf
unsigned int pid; // [rsp+Ch] [rbp-18Ch]
char v4[120]; // [rsp+10h] [rbp-188h] BYREF
__int64 v5; // [rsp+88h] [rbp-110h]
struct rusage usage; // [rsp+F0h] [rbp-A8h] BYREF
__WAIT_STATUS stat_loc; // [rsp+18Ch] [rbp-Ch] BYREF

pid = fork();
if ( !pid )
func3();
wait3((__WAIT_STATUS)&stat_loc, 0, &usage);
v0 = BYTE1(stat_loc.__uptr);
if ( ((__int64)stat_loc.__uptr & 0x7F) != 0 )
{
if ( LOBYTE(stat_loc.__uptr) == 127 )
{
ptrace(PTRACE_GETREGS, pid, 0LL, v4);
if ( v0 == 5 && v5 == 59 )
{
signal(14, alarm_func);
if ( setjmp(jbuf) )
{
kill(pid, 9);
*(_DWORD *)(::result + 4LL) = ltime;
result = ::result;
*::result = 4;
}
else
{
alarm(ltime / 1000 + 2);
ptrace(PTRACE_SYSCALL, pid, 0LL, 0LL);
func6(pid);
alarm(0);
v2 = strcmp(lang, "python") == 0;
result = !v2;
if ( v2 )
result = func7();
}
}
else
{
kill(pid, 9);
*::result = 7;
result = g_string_assign(*(_QWORD *)(::result + 24LL), "unexpected child stop before execve");
}
}
else
{
kill(pid, 9);
*::result = 7;
result = g_string_assign(*(_QWORD *)(::result + 24LL), "unknow child status before execve");
}
}
else
{
switch ( BYTE1(stat_loc.__uptr) )
{
case 0:
g_string_assign(*(_QWORD *)(::result + 24LL), "drop privileges error");
break;
case 1:
g_string_assign(*(_QWORD *)(::result + 24LL), "execvpe error");
break;
case 2:
g_string_assign(*(_QWORD *)(::result + 24LL), "open input file error");
break;
case 3:
g_string_assign(*(_QWORD *)(::result + 24LL), "open output file error");
break;
case 4:
g_string_assign(*(_QWORD *)(::result + 24LL), "dup2 input error");
break;
case 5:
g_string_assign(*(_QWORD *)(::result + 24LL), "dup2 ouput error");
break;
default:
break;
}
result = ::result;
*::result = 7;
}
return result;
}

original_code:
void
execute_command()
{
    int signo;
    int status;
    pid_t child;
    struct rusage used;
    struct user_regs_struct regs;
    child = fork();
    if (child == 0)
       setup_child();
    wait3(&status, 0, &used);
    if ((((status) & 0x7f) == 0)) {
        switch ((((status) & 0xff00) >> 8)) {
            case 0 : g_string_assign(result->err, "drop privileges error");
                     break;
            case 1 : g_string_assign(result->err, "execvpe error");
                     break;
            case 2 : g_string_assign(result->err, "open input file error");
                     break;
            case 3 : g_string_assign(result->err, "open output file error");
                     break;
            case 4 : g_string_assign(result->err, "dup2 input error");
                     break;
            case 5 : g_string_assign(result->err, "dup2 ouput error");
                     break;
        }
        result->code = EXIT_IE;
        return;
    }
    if (!(((status) & 0xff) == 0x7f)) {
        kill(child, 9);
        result->code = EXIT_IE;
        g_string_assign(result->err,
                "unknow child status before execve");
        return;
    }
    signo = (((status) & 0xff00) >> 8);
    ptrace(PTRACE_GETREGS, child, ((void *)0), &regs);
    if (signo != 5 || regs.orig_rax != 59) {
        kill(child, 9);
        result->code = EXIT_IE;
        g_string_assign(result->err,
                "unexpected child stop before execve");
        return;
    }
    signal(14, alarm_func);
    if (_setjmp (jbuf)) {
        kill(child, 9);
        result->time = ltime;
        result->code = EXIT_TLE;
        return;
    }
    alarm(ltime / 1000 + 2);
    ptrace(PTRACE_SYSCALL, child, ((void *)0), ((void *)0));
    trace_child(child);
    alarm(0);
    if (strcmp(lang, "python") == 0)
        python_filter();
    return;
}

canonical_original_code:
void execute_command()
{
    int signo;
    int status;
    int child;
    struct rusage used;
    struct user_regs_struct regs;
    child = fork();
    if (child == 0)
       setup_child();
    wait3(&status, 0, &used);
    if ((((status) & 0x7f) == 0)) {
        switch ((((status) & 0xff00) >> 8)) {
            case 0 : g_string_assign(result->err, "drop privileges error");
                     break;
            case 1 : g_string_assign(result->err, "execvpe error");
                     break;
            case 2 : g_string_assign(result->err, "open input file error");
                     break;
            case 3 : g_string_assign(result->err, "open output file error");
                     break;
            case 4 : g_string_assign(result->err, "dup2 input error");
                     break;
            case 5 : g_string_assign(result->err, "dup2 ouput error");
                     break;
        }
        result->code = EXIT_IE;
        return;
    }
    if (!(((status) & 0xff) == 0x7f)) {
        kill(child, 9);
        result->code = EXIT_IE;
        g_string_assign(result->err,
                "unknow child status before execve");
        return;
    }
    signo = (((status) & 0xff00) >> 8);
    ptrace(PTRACE_GETREGS, child, ((void *)0), &regs);
    if (signo != 5 || regs.orig_rax != 59) {
        kill(child, 9);
        result->code = EXIT_IE;
        g_string_assign(result->err,
                "unexpected child stop before execve");
        return;
    }
    signal(14, alarm_func);
    if (_setjmp (jbuf)) {
        kill(child, 9);
        result->time = ltime;
        result->code = EXIT_TLE;
        return;
    }
    alarm(ltime / 1000 + 2);
    ptrace(PTRACE_SYSCALL, child, ((void *)0), ((void *)0));
    trace_child(child);
    alarm(0);
    if (strcmp(lang, "python") == 0)
        python_filter();
    return;
}

user_defined_types: struct user_regs_struct { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long rbp; unsigned long long rbx; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rax; unsigned long long rcx; unsigned long long rdx; unsigned long long rsi; unsigned long long rdi; unsigned long long orig_rax; unsigned long long rip; unsigned long long cs; unsigned long long eflags; unsigned long long rsp; unsigned long long ss; unsigned long long fs_base; unsigned long long gs_base; unsigned long long ds; unsigned long long es; unsigned long long fs; unsigned long long gs; }


repo: Onway/anoj

decompiled_code:
int __fastcall cmp_func(const char **a1, const char **a2)
{
return strcmp(*a1, *a2);
}

canonical_decompiled_code:
int __fastcall func0(const char **a1, const char **a2)
{
return strcmp(*a1, *a2);
}

original_code:
static gint
cmp_func(gconstpointer a, gconstpointer b)
{
    return strcmp(*(char **)a, *(char **)b);
}

canonical_original_code:
static int cmp_func(void *a, void *b)
{
    return strcmp(*(char **)a, *(char **)b);
}

variable_types:
a
void *

variable_types:
b
void *

return_type: int


decompiled_code:
__int64 check_data()
{
_QWORD *v0; // rax
__int64 v1; // r12
unsigned int v2; // er13
_QWORD *v3; // rbx
_QWORD *v4; // rbp

g_ptr_array_sort(gpar, cmp_func);
v0 = (_QWORD *)gpar;
if ( !*(_DWORD *)(gpar + 8) )
return 1LL;
v1 = 8LL;
v2 = 0;
do
{
v3 = (_QWORD *)g_strsplit(*(_QWORD *)(*v0 + v1 - 8), &unk_748, 0LL);
v4 = (_QWORD *)g_strsplit(*(_QWORD *)(*(_QWORD *)gpar + v1), &unk_748, 0LL);
if ( (unsigned int)g_strcmp0(*v3, *v4)
|| (unsigned int)g_strcmp0(v3[1], &unk_74A)
|| (unsigned int)g_strcmp0(v4[1], &unk_74E) )
{
g_strfreev(v3);
g_strfreev(v4);
return 0LL;
}
g_strfreev(v3);
g_strfreev(v4);
v2 += 2;
v0 = (_QWORD *)gpar;
v1 += 16LL;
}
while ( *(_DWORD *)(gpar + 8) > v2 );
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
_QWORD *v0; // rax
__int64 v1; // r12
unsigned int v2; // er13
_QWORD *v3; // rbx
_QWORD *v4; // rbp

g_ptr_array_sort(gpar, cmp_func);
v0 = (_QWORD *)gpar;
if ( !*(_DWORD *)(gpar + 8) )
return 1LL;
v1 = 8LL;
v2 = 0;
do
{
v3 = (_QWORD *)g_strsplit(*(_QWORD *)(*v0 + v1 - 8), &unk_748, 0LL);
v4 = (_QWORD *)g_strsplit(*(_QWORD *)(*(_QWORD *)gpar + v1), &unk_748, 0LL);
if ( (unsigned int)g_strcmp0(*v3, *v4)
|| (unsigned int)g_strcmp0(v3[1], &unk_74A)
|| (unsigned int)g_strcmp0(v4[1], &unk_74E) )
{
g_strfreev(v3);
g_strfreev(v4);
return 0LL;
}
g_strfreev(v3);
g_strfreev(v4);
v2 += 2;
v0 = (_QWORD *)gpar;
v1 += 16LL;
}
while ( *(_DWORD *)(gpar + 8) > v2 );
return 1LL;
}

original_code:
static gboolean
check_data()
{
    int i;
    char ** first = ((void *)0);
    char ** second = ((void *)0);;
    gboolean ret = (!(0));
    g_ptr_array_sort(gpar, cmp_func);
    for (i = 0; i < gpar->len; i += 2) {
        first = g_strsplit(((gpar)->pdata)[i], ".", 0);
        second = g_strsplit(((gpar)->pdata)[i + 1], ".", 0);
        if (g_strcmp0(first[0], second[0]) || g_strcmp0(first[1], "ans") ||
                g_strcmp0(second[1], "in"))
            ret = (0);
        g_strfreev(first);
        g_strfreev(second);
        if (!ret) return (0);
    }
    return ret;
}

canonical_original_code:
static int check_data()
{
    int i;
    char **first = ((void *)0);
    char **second = ((void *)0);;
    int ret = (!(0));
    g_ptr_array_sort(gpar, cmp_func);
    for (i = 0; i < gpar->len; i += 2) {
        first = g_strsplit(((gpar)->pdata)[i], ".", 0);
        second = g_strsplit(((gpar)->pdata)[i + 1], ".", 0);
        if (g_strcmp0(first[0], second[0]) || g_strcmp0(first[1], "ans") ||
                g_strcmp0(second[1], "in"))
            ret = (0);
        g_strfreev(first);
        g_strfreev(second);
        if (!ret) return (0);
    }
    return ret;
}

variable_types: i int

variable_types:
first
char **

variable_types:
second
char **

variable_types: ret int


decompiled_code:
__int64 set_output()
{
__int64 *v0; // rbp
int v1; // ebx
unsigned int v2; // eax
__int64 v3; // rbx

v0 = (__int64 *)g_string_new(&unk_751);
v1 = 16;
do
{
v2 = g_rand_int(grand);
g_string_append_c_inline(v0, byte_850[v2 % 0x3E]);
--v1;
}
while ( v1 );
g_string_append(v0, ".out");
v3 = g_build_path("/", workdir, *v0, 0LL);
g_string_assign(output, v3);
g_string_free(v0, 1LL);
return g_free(v3);
}

canonical_decompiled_code:
__int64 func2()
{
__int64 *v0; // rbp
int v1; // ebx
unsigned int v2; // eax
__int64 v3; // rbx

v0 = (__int64 *)g_string_new(&unk_751);
v1 = 16;
do
{
v2 = g_rand_int(grand);
g_string_append_c_inline(v0, byte_850[v2 % 0x3E]);
--v1;
}
while ( v1 );
g_string_append(v0, ".out");
v3 = g_build_path("/", workdir, *v0, 0LL);
g_string_assign(output, v3);
g_string_free(v0, 1LL);
return g_free(v3);
}

original_code:
static void
set_output()
{
    static char * clist =
"ABCDEFGHIGKLMNOPQRSTUVWXYZ"
"abcdefghigklmnopqrstuvwxyz"
"1234567890";
    char * path;
    int i, cnt = 16;
    int length = strlen(clist);
    GString * rnd = g_string_new("");
    while (cnt--)
        g_string_append_c_inline (rnd, clist[g_rand_int(grand) % length]);
    g_string_append(rnd, ".out");
    path = g_build_path("/", workdir, rnd->str, ((void *)0));
    g_string_assign(output, path);
    g_string_free(rnd, (!(0)));
    g_free(path);
}

canonical_original_code:
static void set_output()
{
    static char *clist = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"
"abcdefghigklmnopqrstuvwxyz"
"1234567890";
    char *path;
    int i; int cnt = 16;
    int length = strlen(clist);
    struct _GString *rnd = g_string_new("");
    while (cnt--)
        g_string_append_c_inline (rnd, clist[g_rand_int(grand) % length]);
    g_string_append(rnd, ".out");
    path = g_build_path("/", workdir, rnd->str, ((void *)0));
    g_string_assign(output, path);
    g_string_free(rnd, (!(0)));
    g_free(path);
}

variable_types:
clist
char *

variable_types:
path
char *

variable_types: cnt int

variable_types: length int

variable_types: struct _GString { char *str; unsigned long len; unsigned long allocated_len; } *

return_type: void


decompiled_code:
__int64 next_data()
{
int v0; // eax
__int64 v2; // rdx
__int64 v3; // rbx
__int64 v4; // rdx
int v5; // eax
__int64 v6; // rsi

v0 = gpar_index;
if ( *(_DWORD *)(gpar + 8) <= (unsigned int)gpar_index )
return 0LL;
v2 = *(_QWORD *)gpar;
++gpar_index;
v3 = g_build_path("/", datadir, *(_QWORD *)(v2 + 8LL * v0), 0LL);
g_string_assign(answer, v3);
g_free(v3);
v4 = *(_QWORD *)gpar;
v5 = gpar_index++;
v6 = g_build_path("/", datadir, *(_QWORD *)(v4 + 8LL * v5), 0LL);
g_string_assign(input, v6);
g_free(v6);
set_output();
return 1LL;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // eax
__int64 v2; // rdx
__int64 v3; // rbx
__int64 v4; // rdx
int v5; // eax
__int64 v6; // rsi

v0 = gpar_index;
if ( *(_DWORD *)(gpar + 8) <= (unsigned int)gpar_index )
return 0LL;
v2 = *(_QWORD *)gpar;
++gpar_index;
v3 = g_build_path("/", datadir, *(_QWORD *)(v2 + 8LL * v0), 0LL);
g_string_assign(answer, v3);
g_free(v3);
v4 = *(_QWORD *)gpar;
v5 = gpar_index++;
v6 = g_build_path("/", datadir, *(_QWORD *)(v4 + 8LL * v5), 0LL);
g_string_assign(input, v6);
g_free(v6);
func2();
return 1LL;
}

original_code:
gboolean
next_data()
{
    char * path;
    if (gpar_index >= gpar->len) return (0);
    path = g_build_path("/", datadir,
            ((gpar)->pdata)[gpar_index++], ((void *)0));
    g_string_assign(answer, path);
    g_free(path);
    path = g_build_path("/", datadir,
            ((gpar)->pdata)[gpar_index++], ((void *)0));
    g_string_assign(input, path);
    g_free(path);
    set_output();
    return (!(0));
}

canonical_original_code:
int next_data()
{
    char *path;
    if (gpar_index >= gpar->len) return (0);
    path = g_build_path("/", datadir,
            ((gpar)->pdata)[gpar_index++], ((void *)0));
    g_string_assign(answer, path);
    g_free(path);
    path = g_build_path("/", datadir,
            ((gpar)->pdata)[gpar_index++], ((void *)0));
    g_string_assign(input, path);
    g_free(path);
    set_output();
    return (!(0));
}


repo: OpenDUNE/OpenDUNE

decompiled_code:
__int64 __fastcall add_to_pak(__int64 a1, char *a2)
{
int v2; // eax
char *v3; // rax
const char *v4; // r12
size_t v5; // r14
_QWORD *v6; // rbx
int v7; // er15
const __int32_t *v8; // rcx
__int64 v9; // rax
__int64 result; // rax
struct stat v11; // [rsp+0h] [rbp-C8h] BYREF

if ( (int)stat(a2, &v11) < 0 )
{
perror(a2);
result = 0xFFFFFFFFLL;
}
else
{
v2 = v11.st_mode & 0xF000;
if ( v2 == 0x4000 )
{
result = add_dir_to_pak(a1, a2);
}
else if ( v2 == 0x8000 )
{
v3 = strrchr(a2, 47);
v4 = v3 + 1;
if ( !v3 )
v4 = a2;
v5 = strlen(v4);
if ( v5 > 0xC )
{
fprintf(_bss_start, "%s is too long (only support 8.3)\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
v6 = malloc(0x28uLL);
v6[2] = strdup(a2);
*v6 = 0LL;
v7 = v11.st_size;
*((_DWORD *)v6 + 2) = v11.st_size;
v8 = *__ctype_toupper_loc();
v9 = 0LL;
do
{
*((_BYTE *)v6 + v9 + 24) = v8[v4[v9]];
++v9;
}
while ( (int)v5 >= (int)v9 );
*(_DWORD *)(a1 + 4) += v5 + 5;
*(_DWORD *)a1 += v5 + v7 + 5;
++*(_DWORD *)(a1 + 8);
if ( *(_QWORD *)(a1 + 16) )
**(_QWORD **)(a1 + 24) = v6;
else
*(_QWORD *)(a1 + 16) = v6;
*(_QWORD *)(a1 + 24) = v6;
printf("Add %-12s %6lu bytes\n", (const char *)v6 + 24, *((unsigned int *)v6 + 2));
result = 0LL;
}
}
else
{
fprintf(_bss_start, "%s is not a regular file\n", a2);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, char *a2)
{
int v2; // eax
char *v3; // rax
const char *v4; // r12
size_t v5; // r14
_QWORD *v6; // rbx
int v7; // er15
const __int32_t *v8; // rcx
__int64 v9; // rax
__int64 result; // rax
struct stat v11; // [rsp+0h] [rbp-C8h] BYREF

if ( (int)func15(a2, &v11) < 0 )
{
perror(a2);
result = 0xFFFFFFFFLL;
}
else
{
v2 = v11.st_mode & 0xF000;
if ( v2 == 0x4000 )
{
result = func7(a1, a2);
}
else if ( v2 == 0x8000 )
{
v3 = strrchr(a2, 47);
v4 = v3 + 1;
if ( !v3 )
v4 = a2;
v5 = strlen(v4);
if ( v5 > 0xC )
{
fprintf(_bss_start, "%s is too long (only support 8.3)\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
v6 = malloc(0x28uLL);
v6[2] = strdup(a2);
*v6 = 0LL;
v7 = v11.st_size;
*((_DWORD *)v6 + 2) = v11.st_size;
v8 = *__ctype_toupper_loc();
v9 = 0LL;
do
{
*((_BYTE *)v6 + v9 + 24) = v8[v4[v9]];
++v9;
}
while ( (int)v5 >= (int)v9 );
*(_DWORD *)(a1 + 4) += v5 + 5;
*(_DWORD *)a1 += v5 + v7 + 5;
++*(_DWORD *)(a1 + 8);
if ( *(_QWORD *)(a1 + 16) )
**(_QWORD **)(a1 + 24) = v6;
else
*(_QWORD *)(a1 + 16) = v6;
*(_QWORD *)(a1 + 24) = v6;
printf("Add %-12s %6lu bytes\n", (const char *)v6 + 24, *((unsigned int *)v6 + 2));
result = 0LL;
}
}
else
{
fprintf(_bss_start, "%s is not a regular file\n", a2);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int add_to_pak(struct pakfile * pak, const char * filename)
{
 int i;
 const char * p;
 struct stat st;
 struct fileentry * entry;
 size_t len;
 if (stat(filename, &st) < 0) {
  perror(filename);
  return -1;
 }
 if (((((st.st_mode)) & 0170000) == (0040000))) {
  return add_dir_to_pak(pak, filename);
 }
 if (!((((st.st_mode)) & 0170000) == (0100000))) {
  fprintf(stderr, "%s is not a regular file\n", filename);
  return -1;
 }
 p = strrchr(filename, '/');
 if (p == ((void *)0)) p = filename;
 else p++;
 len = strlen(p);
 if (len >= 13) {
  fprintf(stderr, "%s is too long (only support 8.3)\n", p);
  return -1;
 }
 entry = malloc(sizeof(struct fileentry));
 entry->source = strdup(filename);
 entry->next = ((void *)0);
 entry->size = (uint32_t)st.st_size;
 for (i = 0; i <= (int)len; i++) {
  entry->name[i] = toupper(p[i]);
 }
 pak->headersize += 4 + len + 1;
 pak->paksize += 4 + len + 1 + entry->size;
 pak->entrycount++;
 if (pak->entries.head == ((void *)0)) {
  pak->entries.head = entry;
  pak->entries.tail = entry;
 } else {
  pak->entries.tail->next = entry;
  pak->entries.tail = entry;
 }
 printf("Add %-12s %6lu bytes\n", entry->name, (unsigned long)entry->size);
 return 0;
}

canonical_original_code:
static int add_to_pak(struct pakfile *pak, char *filename)
{
 int i;
 char *p;
 struct stat st;
 struct fileentry *entry;
 unsigned long len;
 if (stat(filename, &st) < 0) {
  perror(filename);
  return -1;
 }
 if (((((st.st_mode)) & 0170000) == (0040000))) {
  return add_dir_to_pak(pak, filename);
 }
 if (!((((st.st_mode)) & 0170000) == (0100000))) {
  fprintf(stderr, "%s is not a regular file\n", filename);
  return -1;
 }
 p = strrchr(filename, '/');
 if (p == ((void *)0)) p = filename;
 else p++;
 len = strlen(p);
 if (len >= 13) {
  fprintf(stderr, "%s is too long (only support 8.3)\n", p);
  return -1;
 }
 entry = malloc(sizeof(struct fileentry));
 entry->source = strdup(filename);
 entry->next = ((void *)0);
 entry->size = (unsigned int)st.st_size;
 for (i = 0; i <= (int)len; i++) {
  entry->name[i] = toupper(p[i]);
 }
 pak->headersize += 4 + len + 1;
 pak->paksize += 4 + len + 1 + entry->size;
 pak->entrycount++;
 if (pak->entries.head == ((void *)0)) {
  pak->entries.head = entry;
  pak->entries.tail = entry;
 } else {
  pak->entries.tail->next = entry;
  pak->entries.tail = entry;
 }
 printf("Add %-12s %6lu bytes\n", entry->name, (unsigned long)entry->size);
 return 0;
}

variable_types: struct pakfile { unsigned int paksize; unsigned int headersize; unsigned int entrycount; struct { struct fileentry { struct fileentry *next; unsigned int size; char *source; char name[13]; } *head; struct fileentry *tail; } entries; } *

variable_types:
filename
char *

variable_types:
p
char *

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

variable_types:
len
unsigned long

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

user_defined_types: struct pakfile { unsigned int paksize; unsigned int headersize; unsigned int entrycount; struct { struct fileentry *head; struct fileentry *tail; } entries; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
const char *v6; // r12
__int64 v8[7]; // [rsp+0h] [rbp-38h] BYREF

v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
if ( argc <= 1 || (v3 = argv[1], !strcmp("--help", v3)) || !strcmp("-h", v3) )
{
printf("Usage: %s [--out FILE.PAK] file1 file2 file3 ...\n", *argv);
}
else
{
v4 = strcmp("--out", v3);
if ( argc <= 2 || v4 )
{
v5 = 1;
v6 = "OUT.PAK";
}
else
{
v6 = argv[2];
v5 = 3;
}
add_files_to_pak((__int64)v8, argc - v5, (char **)&argv[v5]);
HIDWORD(v8[0]) += 4;
LODWORD(v8[0]) += 4;
build_pak((__int64)v8, v6);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
int v4; // eax
int v5; // eax
const char *v6; // r12
__int64 v8[7]; // [rsp+0h] [rbp-38h] BYREF

v8[0] = 0LL;
v8[1] = 0LL;
v8[2] = 0LL;
v8[3] = 0LL;
if ( argc <= 1 || (v3 = argv[1], !strcmp("--help", v3)) || !strcmp("-h", v3) )
{
printf("Usage: %s [--out FILE.PAK] file1 file2 file3 ...\n", *argv);
}
else
{
v4 = strcmp("--out", v3);
if ( argc <= 2 || v4 )
{
v5 = 1;
v6 = "OUT.PAK";
}
else
{
v6 = argv[2];
v5 = 3;
}
func9((__int64)v8, argc - v5, (char **)&argv[v5]);
HIDWORD(v8[0]) += 4;
LODWORD(v8[0]) += 4;
func12((__int64)v8, v6);
}
return 0;
}

original_code:
int main(int argc, char * * argv)
{
 const char * outfile = "OUT.PAK";
 struct pakfile pak = { 0 };
 int i;
 i = 1;
 if (i >= argc || strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
  printf("Usage: %s [--out FILE.PAK] file1 file2 file3 ...\n", argv[0]);
  return 0;
 }
 if (strcmp("--out", argv[i]) == 0 && i + 1 < argc) {
  outfile = argv[i + 1];
  i += 2;
 }
 add_files_to_pak(&pak, argc - i, (const char * *)argv + i);
 pak.headersize += 4;
 pak.paksize += 4;
 build_pak(&pak, outfile);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 char *outfile = "OUT.PAK";
 struct pakfile pak = { 0 };
 int i;
 i = 1;
 if (i >= argc || strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
  printf("Usage: %s [--out FILE.PAK] file1 file2 file3 ...\n", argv[0]);
  return 0;
 }
 if (strcmp("--out", argv[i]) == 0 && i + 1 < argc) {
  outfile = argv[i + 1];
  i += 2;
 }
 add_files_to_pak(&pak, argc - i, (char **)argv + i);
 pak.headersize += 4;
 pak.paksize += 4;
 build_pak(&pak, outfile);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
outfile
char *

variable_types: struct pakfile { unsigned int paksize; unsigned int headersize; unsigned int entrycount; struct { struct fileentry { struct fileentry *next; unsigned int size; char *source; char name[13]; } *head; struct fileentry *tail; } entries; }


tuneToVOCFreqDiv

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

result = 155LL;
if ( a1 != 73 )
{
result = 184LL;
if ( a1 != 213 )
{
result = 172LL;
if ( a1 == 29 )
result = 144LL;
}
}
return result;
}

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

result = 155LL;
if ( a1 != 73 )
{
result = 184LL;
if ( a1 != 213 )
{
result = 172LL;
if ( a1 == 29 )
result = 144LL;
}
}
return result;
}

original_code:
static int tuneToVOCFreqDiv(int tune)
{
 int freq;
 switch(tune) {
 case 29:
  freq = 144;
  break;
 case 73:
  freq = 155;
  break;
 case 213:
  freq = 184;
  break;
 default:
  freq = 172;
 }
 return freq;
}

variable_types: tune int

variable_types: freq int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax

result = 1;
if ( argc > 1 )
{
parseMX((char *)argv[1]);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax

result = 1;
if ( argc > 1 )
{
func9((char *)argv[1]);
result = 0;
}
return result;
}

original_code:
int main(int argc, char * * argv)
{
 if (argc < 2) {
  return 1;
 }
 parseMX(argv[1]);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 if (argc < 2) {
  return 1;
 }
 parseMX(argv[1]);
 return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int i; // ebx
unsigned int v5; // eax
unsigned int j; // ebx
unsigned int v7; // eax
__int16 v8; // ax
__int16 v9; // ax
unsigned int v10; // eax
unsigned int v11; // eax
char v13[264]; // [rsp+0h] [rbp-108h] BYREF

LCD_screen_init(240LL, 128LL, 0LL);
LCD_mode(0LL);
LCD_display_mode(12LL);
v3 = LCD_getBaseText();
LCD_set_address_pointer(v3);
for ( i = 0; (unsigned int)LCD_getTextScreenSize() > i; ++i )
LCD_data_write_up(0LL);
v5 = LCD_getBaseGraphic();
LCD_set_address_pointer(v5);
LCD_auto_write_start();
for ( j = 0; (unsigned int)LCD_getGraphicScreenSize() > j; ++j )
LCD_auto_write(0LL);
LCD_auto_write_stop();
v7 = LCD_getBaseText();
LCD_set_address_pointer(v7);
lprintln(&unk_1D3);
lprintln(" Welcome on this DG-24128");
lprintln(&unk_1D3);
lprintln(&unk_1D3);
v8 = LCD_getCols();
sprintf(v13, "cols=0x%X", (unsigned int)v8);
lprintln(v13);
v9 = LCD_getRows();
sprintf(v13, "rows=0x%X", (unsigned int)v9);
lprintln(v13);
v10 = LCD_getTextScreenSize();
sprintf(v13, "getTextScreenSize=0x%X", v10);
lprintln(v13);
v11 = LCD_getGraphicScreenSize();
sprintf(v13, "getGraphicScreenSize=0x%X", v11);
lprintln(v13);
LCD_Circle(50LL, 100LL, 20LL);
LCD_Rectangle(10LL, 3LL, 210LL, 18LL);
LCD_Line(100LL, 100LL, 210LL, 80LL);
return 0;
}

original_code:
int main(int argc, char **argv)
{
 int pixelX = 240;
 int pixelY = 128;
 int fontSize = 0;
 LCD_screen_init(pixelX,pixelY, fontSize);
 LCD_mode( 0x00 );
 LCD_display_mode( 0x04 + 0x08 );
 int i;
 LCD_set_address_pointer( LCD_getBaseText() );
 for (i=0;i<LCD_getTextScreenSize();i++) {
  LCD_data_write_up( 0x00 );
 }
 LCD_set_address_pointer( LCD_getBaseGraphic() );
 LCD_auto_write_start();
 for (i=0;i<LCD_getGraphicScreenSize();i++) {
  LCD_auto_write( 0x00 );
 }
 LCD_auto_write_stop();
 LCD_set_address_pointer( LCD_getBaseText() );
 lprintln("");
 lprintln("  Welcome on this DG-24128");
 lprintln("");
 lprintln("");
 char buffer[256];
 sprintf(buffer,"cols=0x%X",(unsigned int)LCD_getCols());
 lprintln(buffer);
 sprintf(buffer,"rows=0x%X",(unsigned int)LCD_getRows());
 lprintln(buffer);
 sprintf(buffer,"getTextScreenSize=0x%X",LCD_getTextScreenSize());
 lprintln(buffer);
 sprintf(buffer,"getGraphicScreenSize=0x%X",LCD_getGraphicScreenSize());
 lprintln(buffer);
    LCD_Circle(50,100,20);
 LCD_Rectangle(10,3,210,18);
 LCD_Line(100,100,210,80);
 return 0;
}

variable_types: pixelX int

variable_types: pixelY int

variable_types: fontSize int

variable_types: i int

variable_types:
buffer
char [256]


decompiled_code:
__int64 LCD_setup()
{
reg_setup();
gpio_output_pin(14LL);
gpio_output_pin(15LL);
gpio_output_pin(18LL);
gpio_output_pin(23LL);
return gpio_set(0x800000LL);
}

canonical_decompiled_code:
__int64 func0()
{
reg_setup();
gpio_output_pin(14LL);
gpio_output_pin(15LL);
gpio_output_pin(18LL);
gpio_output_pin(23LL);
return gpio_set(0x800000LL);
}

original_code:
void LCD_setup()
{
  reg_setup();
  gpio_output_pin(14);
  gpio_output_pin(15);
  gpio_output_pin(18);
  gpio_output_pin(23);
  gpio_set( 1<<23 );
}


decompiled_code:
__int64 __fastcall LCD_command(__int16 a1)
{
reg_data((unsigned int)a1);
gpio_set(0x40000LL);
gpio_clr(0x800000LL);
usleep(1u);
return gpio_set(0x800000LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1)
{
reg_data((unsigned int)a1);
gpio_set(0x40000LL);
gpio_clr(0x800000LL);
usleep(1u);
return gpio_set(0x800000LL);
}

original_code:
void LCD_command(short command) {
  reg_data(command);
  gpio_set( 1<<18 );
  gpio_clr( 1<<23 );
  usleep(1);
  gpio_set( 1<<23 );
}

variable_types: command short


decompiled_code:
__int64 __fastcall LCD_data(__int16 a1)
{
reg_data((unsigned int)a1);
gpio_clr(0x40000LL);
gpio_clr(0x800000LL);
usleep(1u);
return gpio_set(0x800000LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
reg_data((unsigned int)a1);
gpio_clr(0x40000LL);
gpio_clr(0x800000LL);
usleep(1u);
return gpio_set(0x800000LL);
}

original_code:
void LCD_data(short data) {
  reg_data(data);
  gpio_clr( 1<<18 );
  gpio_clr( 1<<23 );
  usleep(1);
  gpio_set( 1<<23 );
}

variable_types: data short


decompiled_code:
__int64 __fastcall LCD_command_1D(__int16 a1, __int16 a2)
{
LCD_data(a2);
return LCD_command(a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1, __int16 a2)
{
func2(a2);
return func1(a1);
}

original_code:
void LCD_command_1D(short command, short data) {
  LCD_data(data);
  LCD_command(command);
}


decompiled_code:
__int64 __fastcall LCD_command_2D(__int16 a1, __int16 a2)
{
LCD_data((unsigned __int8)a2);
LCD_data(HIBYTE(a2));
return LCD_command(a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1, __int16 a2)
{
func2((unsigned __int8)a2);
func2(HIBYTE(a2));
return func1(a1);
}

original_code:
void LCD_command_2D(short command, int data) {
  LCD_data(data & 0xFF);
  LCD_data((data >> 8) & 0xFF);
  LCD_command(command);
}

variable_types: data int


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

if ( a1 )
result = gpio_clr(0x4000LL);
else
result = gpio_set(0x4000LL);
return result;
}

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

if ( a1 )
result = gpio_clr(0x4000LL);
else
result = gpio_set(0x4000LL);
return result;
}

original_code:
void LCD_Reverse(short rev) {
  if (rev) {
 gpio_clr( 1<<14 );
 } else {
 gpio_set( 1<<14 );
 }
}

variable_types: rev short


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

if ( a1 )
result = gpio_clr(0x8000LL);
else
result = gpio_set(0x8000LL);
return result;
}

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

if ( a1 )
result = gpio_clr(0x8000LL);
else
result = gpio_set(0x8000LL);
return result;
}

original_code:
void LCD_FontSelect(short high) {
  if (high) {
 gpio_clr( 1<<15 );
 } else {
 gpio_set( 1<<15 );
 }
}

variable_types: high short


decompiled_code:
__int64 __fastcall LCD_init(unsigned int a1, unsigned int a2, __int16 a3)
{
unsigned int v4; // ebx

gpio_setup();
LCD_setup();
LCD_FontSelect((unsigned int)a3);
v4 = a3 == 0 ? 40 : 30;
LCD_command_2D(64LL, a1);
LCD_command_2D(65LL, v4);
LCD_command_2D(66LL, a2);
return LCD_command_2D(67LL, v4);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2, __int16 a3)
{
unsigned int v4; // ebx

gpio_setup();
LCD_setup();
LCD_FontSelect((unsigned int)a3);
v4 = a3 == 0 ? 40 : 30;
LCD_command_2D(64LL, a1);
LCD_command_2D(65LL, v4);
LCD_command_2D(66LL, a2);
return LCD_command_2D(67LL, v4);
}

original_code:
void LCD_init(int textStart, int graphicStart, short fontSize) {
 gpio_setup();
 LCD_setup();
 LCD_FontSelect( fontSize );
 int lineGap = fontSize ? 0x1E : 0x28;
 LCD_command_2D( 0x40, textStart );
 LCD_command_2D( 0x41, lineGap );
 LCD_command_2D( 0x42, graphicStart );
 LCD_command_2D( 0x43, lineGap );
}

variable_types: textStart int

variable_types: graphicStart int

variable_types: fontSize short

variable_types: lineGap int


decompiled_code:
__int64 __fastcall LCD_mode(__int16 a1)
{
return LCD_command((unsigned int)(__int16)(a1 + 128));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1)
{
return LCD_command((unsigned int)(__int16)(a1 + 128));
}

original_code:
void LCD_mode(int mode) {
 LCD_command( 0x80 + mode );
}

variable_types: mode int


decompiled_code:
__int64 __fastcall LCD_display_mode(__int16 a1)
{
return LCD_command((unsigned int)(__int16)(a1 + 144));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
return LCD_command((unsigned int)(__int16)(a1 + 144));
}

original_code:
void LCD_display_mode(int dm) {
 LCD_command( 0x90 + dm );
}

variable_types: dm int


decompiled_code:
__int64 __fastcall LCD_set_offset_register(unsigned int a1)
{
return LCD_command_2D(34LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
return LCD_command_2D(34LL, a1);
}

original_code:
void LCD_set_offset_register( int offset ) {
 LCD_command_2D( 0x22 , offset);
}

canonical_original_code:
void LCD_set_offset_register(int offset) {
 LCD_command_2D( 0x22 , offset);
}

variable_types: offset int


decompiled_code:
__int64 __fastcall LCD_set_cursor_pointer(unsigned int a1)
{
return LCD_command_2D(33LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
return LCD_command_2D(33LL, a1);
}

original_code:
void LCD_set_cursor_pointer( int pointer ) {
 LCD_command_2D( 0x21 , pointer);
}

canonical_original_code:
void LCD_set_cursor_pointer(int pointer) {
 LCD_command_2D( 0x21 , pointer);
}

variable_types: pointer int


decompiled_code:
__int64 __fastcall LCD_set_address_pointer(unsigned int a1)
{
return LCD_command_2D(36LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
return LCD_command_2D(36LL, a1);
}

original_code:
void LCD_set_address_pointer( int address ) {
 LCD_command_2D( 0x24 , address);
}

canonical_original_code:
void LCD_set_address_pointer(int address) {
 LCD_command_2D( 0x24 , address);
}

variable_types: address int


decompiled_code:
__int64 __fastcall LCD_data_write(__int16 a1)
{
return LCD_command_1D(196LL, (unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1)
{
return LCD_command_1D(196LL, (unsigned int)a1);
}

original_code:
void LCD_data_write( short data ) {
 LCD_command_1D( 0xC4 , data );
}

canonical_original_code:
void LCD_data_write(short data) {
 LCD_command_1D( 0xC4 , data );
}


decompiled_code:
__int64 __fastcall LCD_data_write_up(__int16 a1)
{
return LCD_command_1D(192LL, (unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1)
{
return LCD_command_1D(192LL, (unsigned int)a1);
}

original_code:
void LCD_data_write_up( short data ) {
 LCD_command_1D( 0xC0 , data );
}

canonical_original_code:
void LCD_data_write_up(short data) {
 LCD_command_1D( 0xC0 , data );
}


decompiled_code:
__int64 __fastcall LCD_data_write_down(__int16 a1)
{
return LCD_command_1D(194LL, (unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int16 a1)
{
return LCD_command_1D(194LL, (unsigned int)a1);
}

original_code:
void LCD_data_write_down( short data ) {
 LCD_command_1D( 0xC2 , data );
}

canonical_original_code:
void LCD_data_write_down(short data) {
 LCD_command_1D( 0xC2 , data );
}


decompiled_code:
__int64 LCD_auto_write_start()
{
return LCD_command(176LL);
}

canonical_decompiled_code:
__int64 func9()
{
return LCD_command(176LL);
}

original_code:
void LCD_auto_write_start() {
 LCD_command( 0xB0 );
}


decompiled_code:
__int64 __fastcall LCD_auto_write(__int16 a1)
{
return LCD_data((unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int16 a1)
{
return LCD_data((unsigned int)a1);
}

original_code:
void LCD_auto_write(short data) {
 LCD_data( data );
}


decompiled_code:
__int64 LCD_auto_write_stop()
{
return LCD_command(178LL);
}

canonical_decompiled_code:
__int64 func11()
{
return LCD_command(178LL);
}

original_code:
void LCD_auto_write_stop() {
 LCD_command( 0xB2 );
}


decompiled_code:
__int64 __fastcall LCD_set_bit(__int16 a1, __int16 a2)
{
return LCD_command((unsigned int)(__int16)(247 - a1 + 8 * (a2 != 0)));
}

canonical_decompiled_code:
__int64 __fastcall func12(__int16 a1, __int16 a2)
{
return LCD_command((unsigned int)(__int16)(247 - a1 + 8 * (a2 != 0)));
}

original_code:
void LCD_set_bit(short bit,short set) {
 LCD_command( 0xF7 - bit + (set?8:0) );
}

canonical_original_code:
void LCD_set_bit(short bit, short set) {
 LCD_command( 0xF7 - bit + (set?8:0) );
}

variable_types: bit short

variable_types: set short


lprint

decompiled_code:
__int64 __fastcall lprint(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = LCD_data_write_up(v2 - 32);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(_BYTE *a1)
{
_BYTE *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = func7(v2 - 32);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

original_code:
void lprint(char *text) {
  while (*text != 0) {
 LCD_data_write_up( *text - 0x20 );
 text++;
  }
}

variable_types:
text
char *


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

pixelX = a1;
pixelY = a2;
fontSize = a3;
fontX = a3 == 0 ? 6 : 8;
cols = a1 / fontX;
rows = a2 / 8;
result = (unsigned int)(a2 / 8 * (a1 / fontX));
TEXT_SIZE = result;
GRAPHIC_SIZE = 8 * result;
BASE_TEXT = 0;
BASE_GRAPHIC = a2 / 8 * (a1 / fontX);
return result;
}

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

pixelX = a1;
pixelY = a2;
fontSize = a3;
fontX = a3 == 0 ? 6 : 8;
cols = a1 / fontX;
rows = a2 / 8;
result = (unsigned int)(a2 / 8 * (a1 / fontX));
TEXT_SIZE = result;
GRAPHIC_SIZE = 8 * result;
BASE_TEXT = 0;
BASE_GRAPHIC = a2 / 8 * (a1 / fontX);
return result;
}

original_code:
void LCD_screen_compute(int pX, int pY, int font) {
 pixelX = pX;
 pixelY = pY;
 fontSize = font;
 fontX = fontSize ? 8 : 6;
 int fontY = 8;
 cols = pixelX / fontX;
 rows = pixelY / fontY;
 TEXT_SIZE = cols * rows;
 GRAPHIC_SIZE = cols * rows * fontY;
 BASE_TEXT = 0;
 BASE_GRAPHIC = TEXT_SIZE;
}

variable_types: pX int

variable_types: pY int

variable_types: font int

variable_types: fontY int


decompiled_code:
__int64 __fastcall LCD_screen_init(int a1, int a2, int a3)
{
LCD_screen_compute(a1, a2, a3);
return LCD_init((unsigned int)BASE_TEXT, (unsigned int)BASE_GRAPHIC, (unsigned int)(__int16)fontSize);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3)
{
func0(a1, a2, a3);
return LCD_init((unsigned int)BASE_TEXT, (unsigned int)BASE_GRAPHIC, (unsigned int)(__int16)fontSize);
}

original_code:
void LCD_screen_init(int pX, int pY, int font) {
 LCD_screen_compute(pX, pY, font);
 LCD_init(BASE_TEXT,BASE_GRAPHIC,fontSize);
}


decompiled_code:
__int64 LCD_getRows()
{
return (unsigned __int16)rows;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned __int16)rows;
}

original_code: short LCD_getRows() { return (short)rows; }

return_type: short


decompiled_code:
__int64 LCD_getCols()
{
return (unsigned __int16)cols;
}

canonical_decompiled_code:
__int64 func3()
{
return (unsigned __int16)cols;
}

original_code: short LCD_getCols() { return (short)cols; }


decompiled_code:
__int64 LCD_getBaseText()
{
return (unsigned int)BASE_TEXT;
}

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

original_code: unsigned int LCD_getBaseText() { return BASE_TEXT; }

return_type: unsigned int


decompiled_code:
__int64 LCD_getBaseGraphic()
{
return (unsigned int)BASE_GRAPHIC;
}

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

original_code: unsigned int LCD_getBaseGraphic() { return BASE_GRAPHIC; }


decompiled_code:
__int64 LCD_getTextScreenSize()
{
return (unsigned int)TEXT_SIZE;
}

canonical_decompiled_code:
__int64 func6()
{
return (unsigned int)TEXT_SIZE;
}

original_code: unsigned int LCD_getTextScreenSize() { return TEXT_SIZE; }


decompiled_code:
__int64 LCD_getGraphicScreenSize()
{
return (unsigned int)GRAPHIC_SIZE;
}

canonical_decompiled_code:
__int64 func7()
{
return (unsigned int)GRAPHIC_SIZE;
}

original_code: unsigned int LCD_getGraphicScreenSize() { return GRAPHIC_SIZE; }


lprintln

decompiled_code:
__int64 __fastcall lprintln(char *s)
{
char *v1; // rbx
int v2; // ebp
char v3; // di
int v4; // ebx

v1 = s;
LCD_auto_write_start();
v2 = strlen(s);
v3 = *s;
if ( v3 )
{
do
{
LCD_auto_write((unsigned int)(__int16)(v3 - 32));
v3 = *++v1;
}
while ( *v1 );
}
if ( cols > v2 % cols )
{
v4 = v2 % cols;
do
{
LCD_auto_write(0LL);
++v4;
}
while ( cols > v4 );
}
return LCD_auto_write_stop();
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s)
{
char *v1; // rbx
int v2; // ebp
char v3; // di
int v4; // ebx

v1 = s;
LCD_auto_write_start();
v2 = strlen(s);
v3 = *s;
if ( v3 )
{
do
{
LCD_auto_write((unsigned int)(__int16)(v3 - 32));
v3 = *++v1;
}
while ( *v1 );
}
if ( cols > v2 % cols )
{
v4 = v2 % cols;
do
{
LCD_auto_write(0LL);
++v4;
}
while ( cols > v4 );
}
return LCD_auto_write_stop();
}

original_code:
void lprintln(char *text) {
  LCD_auto_write_start();
  int len = strlen(text);
  while (*text != 0) {
 LCD_auto_write( *text - 0x20 );
 text++;
  }
  len %= cols;
  while (len++<cols) {
 LCD_auto_write( 0 );
  }
  LCD_auto_write_stop();
}

variable_types: len int


decompiled_code:
__int64 __fastcall LCD_SetPixel(unsigned __int8 a1, unsigned __int8 a2, __int16 a3)
{
LCD_set_address_pointer(
BASE_GRAPHIC + a1 / fontX + cols * (unsigned int)a2,
cols * (unsigned int)a2,
(unsigned int)(a1 % fontX));
return LCD_set_bit((unsigned int)(__int16)(a1 % fontX - fontX + 8), (unsigned int)a3);
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned __int8 a1, unsigned __int8 a2, __int16 a3)
{
LCD_set_address_pointer(
BASE_GRAPHIC + a1 / fontX + cols * (unsigned int)a2,
cols * (unsigned int)a2,
(unsigned int)(a1 % fontX));
return LCD_set_bit((unsigned int)(__int16)(a1 % fontX - fontX + 8), (unsigned int)a3);
}

original_code:
void LCD_SetPixel(unsigned char x,unsigned char y, short color) {
int address;
address = BASE_GRAPHIC + (x / fontX) + (y * cols);
LCD_set_address_pointer( address );
LCD_set_bit( x % fontX + 8 - fontX, color );
}

canonical_original_code:
void LCD_SetPixel(unsigned char x, unsigned char y, short color) {
int address;
address = BASE_GRAPHIC + (x / fontX) + (y * cols);
LCD_set_address_pointer( address );
LCD_set_bit( x % fontX + 8 - fontX, color );
}

variable_types:
x
unsigned char

variable_types:
y
unsigned char

variable_types: color short


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

result = gpio;
*(_DWORD *)(gpio + 28) = a1;
return result;
}

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

result = gpio;
*(_DWORD *)(gpio + 28) = a1;
return result;
}

original_code:
void gpio_set(int pin) {
 *(gpio+7) = pin;
 }

variable_types: pin int


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

result = gpio;
*(_DWORD *)(gpio + 40) = a1;
return result;
}

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

result = gpio;
*(_DWORD *)(gpio + 40) = a1;
return result;
}

original_code:
void gpio_clr(int pin) {
 *(gpio+10) = pin;
 }


decompiled_code:
__int64 gpio_setup()
{
int v0; // eax
__int64 result; // rax

v0 = open("/dev/mem", 1052674);
mem_fd = v0;
if ( v0 < 0 )
{
puts("can't open /dev/mem ");
exit(-1);
}
gpio_map = (__int64)mmap(0LL, 0x1000uLL, 3, 1, v0, 538968064LL);
close(mem_fd);
result = gpio_map;
if ( gpio_map == -1 )
{
printf("mmap error %d\n", 0xFFFFFFFFLL);
exit(-1);
}
gpio = gpio_map;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // eax
__int64 result; // rax

v0 = open("/dev/mem", 1052674);
mem_fd = v0;
if ( v0 < 0 )
{
puts("can't open /dev/mem ");
exit(-1);
}
gpio_map = (__int64)mmap(0LL, 0x1000uLL, 3, 1, v0, 538968064LL);
close(mem_fd);
result = gpio_map;
if ( gpio_map == -1 )
{
printf("mmap error %d\n", 0xFFFFFFFFLL);
exit(-1);
}
gpio = gpio_map;
return result;
}

original_code:
void gpio_setup()
{
   if ((mem_fd = open("/dev/mem", 02|04010000) ) < 0) {
      printf("can't open /dev/mem \n");
      exit(-1);
   }
   gpio_map = mmap(
      ((void *)0),
      (4*1024),
      0x1|0x2,
      0x01,
      mem_fd,
      (0x20000000 + 0x200000)
   );
   close(mem_fd);
   if (gpio_map == ((void *) -1)) {
      printf("mmap error %d\n", (int)gpio_map);
      exit(-1);
   }
   gpio = (volatile unsigned *)gpio_map;
}

canonical_original_code:
void gpio_setup()
{
   if ((mem_fd = open("/dev/mem", 02|04010000) ) < 0) {
      printf("can't open /dev/mem \n");
      exit(-1);
   }
   gpio_map = mmap(
      ((void *)0),
      (4*1024),
      0x1|0x2,
      0x01,
      mem_fd,
      (0x20000000 + 0x200000)
   );
   close(mem_fd);
   if (gpio_map == ((void *) -1)) {
      printf("mmap error %d\n", (int)gpio_map);
      exit(-1);
   }
   gpio = (unsigned int *)gpio_map;
}


SerialDataInitialize

original_code:
void SerialDataInitialize(SerialData * s)
{
    s->receive_state = 0;
    s->receive_next_char_is_escaped = 0;
    s->Transmit = ((void *)0);
    s->TransmitPacketComplete = ((void *)0);
    s->ReceivePacketComplete = ((void *)0);
    s->ReceiveDataError = ((void *)0);
    s->transmit_state = 20;
    s->transmit_address = 0;
    s->transmit_length = 0;
    s->transmit_checksum = 0;
    s->transmit_data_ptr = 0;
    s->ref = ((void *)0);
}

canonical_original_code:
void SerialDataInitialize(struct SSerialData *s)
{
    s->receive_state = 0;
    s->receive_next_char_is_escaped = 0;
    s->Transmit = ((void *)0);
    s->TransmitPacketComplete = ((void *)0);
    s->ReceivePacketComplete = ((void *)0);
    s->ReceiveDataError = ((void *)0);
    s->transmit_state = 20;
    s->transmit_address = 0;
    s->transmit_length = 0;
    s->transmit_checksum = 0;
    s->transmit_data_ptr = 0;
    s->ref = ((void *)0);
}

variable_types: struct SSerialData { unsigned char receive_data[20]; unsigned char receive_data_count; unsigned char receive_length; unsigned char receive_next_char_is_escaped; unsigned char receive_address; unsigned char receive_checksum; unsigned char receive_state; unsigned char transmit_data[20]; unsigned char transmit_state; unsigned char transmit_address; unsigned char transmit_length; unsigned char transmit_checksum; unsigned char transmit_escaped_char; unsigned char *transmit_data_ptr; void (*Transmit)(struct SSerialData *s, unsigned char data); void (*TransmitPacketComplete)(struct SSerialData *s); void (*ReceivePacketComplete)(struct SSerialData *s); void (*ReceiveDataError)(struct SSerialData *s, unsigned char errCode); void *ref; } *


ProcessDataChar

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

if ( (unsigned __int8)(a2 - 1) > 0xFDu )
return (unsigned __int64)SerialError((__int64)a1, 7u);
result = a2;
if ( (_BYTE)a2 == 0xCA )
{
if ( a1[25] )
result = (unsigned __int64)SerialError((__int64)a1, 1u);
a1[25] = 1;
a1[20] = 0;
a1[22] = 0;
}
else if ( a1[25] )
{
if ( (_BYTE)a2 == 92 )
{
a1[22] = 1;
}
else
{
if ( a1[22] )
{
a1[22] = 0;
if ( (_BYTE)a2 == 4 )
{
LOBYTE(result) = 0;
}
else if ( (unsigned __int8)a2 > 4u )
{
if ( (_BYTE)a2 == 5 )
LOBYTE(result) = -1;
}
else if ( (_BYTE)a2 == 2 )
{
LOBYTE(result) = -54;
}
else if ( (_BYTE)a2 == 3 )
{
LOBYTE(result) = 92;
}
}
result = (unsigned __int64)SerialStateMachineProcess((__int64)a1, result);
}
}
else
{
result = (unsigned __int64)SerialError((__int64)a1, 2u);
}
return result;
}

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

if ( (unsigned __int8)(a2 - 1) > 0xFDu )
return (unsigned __int64)SerialError((__int64)a1, 7u);
result = a2;
if ( (_BYTE)a2 == 0xCA )
{
if ( a1[25] )
result = (unsigned __int64)SerialError((__int64)a1, 1u);
a1[25] = 1;
a1[20] = 0;
a1[22] = 0;
}
else if ( a1[25] )
{
if ( (_BYTE)a2 == 92 )
{
a1[22] = 1;
}
else
{
if ( a1[22] )
{
a1[22] = 0;
if ( (_BYTE)a2 == 4 )
{
LOBYTE(result) = 0;
}
else if ( (unsigned __int8)a2 > 4u )
{
if ( (_BYTE)a2 == 5 )
LOBYTE(result) = -1;
}
else if ( (_BYTE)a2 == 2 )
{
LOBYTE(result) = -54;
}
else if ( (_BYTE)a2 == 3 )
{
LOBYTE(result) = 92;
}
}
result = (unsigned __int64)SerialStateMachineProcess((__int64)a1, result);
}
}
else
{
result = (unsigned __int64)SerialError((__int64)a1, 2u);
}
return result;
}

original_code:
void ProcessDataChar (SerialData * s, byte data)
{
    if (data == 0 || data == 255)
    {
        SerialError(s, 7);
        return;
    }
    if (data == 0xCA)
    {
        if (s->receive_state != 0)
        {
            SerialError(s, 1);
        }
        s->receive_state = 1;
        s->receive_data_count = 0;
        s->receive_next_char_is_escaped = 0;
    }
    else
    {
        if (s->receive_state == 0)
        {
            SerialError(s, 2);
        }
        else
        {
            if (data == '\\')
            {
                s->receive_next_char_is_escaped = 1;
            }
            else
            {
                if (s->receive_next_char_is_escaped)
                {
                    s->receive_next_char_is_escaped = 0;
                    switch (data)
                    {
                        case 3:
                            data = '\\';
                        break;
                        case 2:
                            data = 0xCA;
                        break;
                        case 4:
                            data = 0;
                        break;
                        case 5:
                            data = 255;
                        break;
                    }
                }
                SerialStateMachineProcess(s, data);
            }
        }
    }
}

canonical_original_code:
void ProcessDataChar(struct SSerialData *s, unsigned char data)
{
    if (data == 0 || data == 255)
    {
        SerialError(s, 7);
        return;
    }
    if (data == 0xCA)
    {
        if (s->receive_state != 0)
        {
            SerialError(s, 1);
        }
        s->receive_state = 1;
        s->receive_data_count = 0;
        s->receive_next_char_is_escaped = 0;
    }
    else
    {
        if (s->receive_state == 0)
        {
            SerialError(s, 2);
        }
        else
        {
            if (data == '\\')
            {
                s->receive_next_char_is_escaped = 1;
            }
            else
            {
                if (s->receive_next_char_is_escaped)
                {
                    s->receive_next_char_is_escaped = 0;
                    switch (data)
                    {
                        case 3:
                            data = '\\';
                        break;
                        case 2:
                            data = 0xCA;
                        break;
                        case 4:
                            data = 0;
                        break;
                        case 5:
                            data = 255;
                        break;
                    }
                }
                SerialStateMachineProcess(s, data);
            }
        }
    }
}

variable_types:
data
unsigned char

user_defined_types: struct SSerialData { unsigned char receive_data[20]; unsigned char receive_data_count; unsigned char receive_length; unsigned char receive_next_char_is_escaped; unsigned char receive_address; unsigned char receive_checksum; unsigned char receive_state; unsigned char transmit_data[20]; unsigned char transmit_state; unsigned char transmit_address; unsigned char transmit_length; unsigned char transmit_checksum; unsigned char transmit_escaped_char; unsigned char *transmit_data_ptr; void (*Transmit)(struct SSerialData *s, unsigned char data); void (*TransmitPacketComplete)(struct SSerialData *s); void (*ReceivePacketComplete)(struct SSerialData *s); void (*ReceiveDataError)(struct SSerialData *s, unsigned char errCode); void *ref; }


SerialTransferInProgress

decompiled_code:
bool __fastcall SerialTransferInProgress(__int64 a1)
{
return *(_BYTE *)(a1 + 46) != 20;
}

canonical_decompiled_code:
bool __fastcall func2(__int64 a1)
{
return *(_BYTE *)(a1 + 46) != 20;
}

original_code:
char SerialTransferInProgress(SerialData * s)
{
    return (s->transmit_state != 20);
}

canonical_original_code:
char SerialTransferInProgress(struct SSerialData *s)
{
    return (s->transmit_state != 20);
}

return_type: char


SerialTransmit

decompiled_code:
__int64 __fastcall SerialTransmit(__int64 a1, char a2, unsigned __int8 a3)
{
unsigned int v4; // eax
unsigned int v5; // ebp

LOBYTE(v4) = SerialTransferInProgress(a1);
if ( (_BYTE)v4 )
return (unsigned int)-1;
v5 = v4;
if ( !*(_QWORD *)(a1 + 64) )
return (unsigned int)-2;
if ( a3 > 0x14u )
return (unsigned int)-3;
*(_BYTE *)(a1 + 47) = a2;
*(_BYTE *)(a1 + 48) = a3;
printf("%s", (const char *)(a1 + 26));
*(_QWORD *)(a1 + 56) = a1 + 26;
*(_BYTE *)(a1 + 50) = 0;
*(_BYTE *)(a1 + 46) = 15;
(*(void (__fastcall **)(__int64, __int64))(a1 + 64))(a1, 202LL);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char a2, unsigned __int8 a3)
{
unsigned int v4; // eax
unsigned int v5; // ebp

LOBYTE(v4) = func2(a1);
if ( (_BYTE)v4 )
return (unsigned int)-1;
v5 = v4;
if ( !*(_QWORD *)(a1 + 64) )
return (unsigned int)-2;
if ( a3 > 0x14u )
return (unsigned int)-3;
*(_BYTE *)(a1 + 47) = a2;
*(_BYTE *)(a1 + 48) = a3;
printf("%s", (const char *)(a1 + 26));
*(_QWORD *)(a1 + 56) = a1 + 26;
*(_BYTE *)(a1 + 50) = 0;
*(_BYTE *)(a1 + 46) = 15;
(*(void (__fastcall **)(__int64, __int64))(a1 + 64))(a1, 202LL);
return v5;
}

original_code:
char SerialTransmit(SerialData * s, byte address, byte length)
{
    if (SerialTransferInProgress(s))
    {
        return -1;
    }
    if (s->Transmit == ((void *)0))
    {
        return -2;
    }
    if (length > 20)
    {
        return -3;
    }
    s->transmit_address = address;
    s->transmit_length = length;
    if (length > 2){
       int i;
       i = 1;
    }
    printf("%s", s->transmit_data);
    s->transmit_data_ptr = s->transmit_data;
    s->transmit_escaped_char = 0;
    s->transmit_state = 15;
    s->Transmit(s, 0xCA);
    return 0;
}

canonical_original_code:
char SerialTransmit(struct SSerialData *s, unsigned char address, unsigned char length)
{
    if (SerialTransferInProgress(s))
    {
        return -1;
    }
    if (s->Transmit == ((void *)0))
    {
        return -2;
    }
    if (length > 20)
    {
        return -3;
    }
    s->transmit_address = address;
    s->transmit_length = length;
    if (length > 2){
       int i;
       i = 1;
    }
    printf("%s", s->transmit_data);
    s->transmit_data_ptr = s->transmit_data;
    s->transmit_escaped_char = 0;
    s->transmit_state = 15;
    s->Transmit(s, 0xCA);
    return 0;
}

variable_types:
address
unsigned char

variable_types:
length
unsigned char

variable_types: i int


decompiled_code:
void USART_InitPortStructs()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void USART_InitPortStructs() {
}

canonical_original_code:
void USART_InitPortStructs() {
}


decompiled_code:
int __fastcall USART_Open(__int64 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int16 a4, unsigned __int16 a5, char a6)
{
int result; // eax
char s[50]; // [rsp+6h] [rbp-32h] BYREF

*(_QWORD *)(a1 + 56) = 0LL;
*(_QWORD *)(a1 + 64) = 0LL;
*(_BYTE *)a1 = a2;
*(_BYTE *)(a1 + 16) = usart_ports[a2];
*(_DWORD *)(a1 + 4) = a3;
*(_BYTE *)(a1 + 8) = a6;
RingBufferInit(a1 + 24, a4);
RingBufferInit(a1 + 40, a5);
USART_Table[a2] = a1;
sprintf(s, "serial.%d", a2);
result = open(s, 578, 480LL);
*(_DWORD *)(a1 + 12) = result;
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int16 a4, unsigned __int16 a5, char a6)
{
int result; // eax
char s[50]; // [rsp+6h] [rbp-32h] BYREF

*(_QWORD *)(a1 + 56) = 0LL;
*(_QWORD *)(a1 + 64) = 0LL;
*(_BYTE *)a1 = a2;
*(_BYTE *)(a1 + 16) = usart_ports[a2];
*(_DWORD *)(a1 + 4) = a3;
*(_BYTE *)(a1 + 8) = a6;
RingBufferInit(a1 + 24, a4);
RingBufferInit(a1 + 40, a5);
USART_Table[a2] = a1;
sprintf(s, "serial.%d", a2);
result = open(s, 578, 480LL);
*(_DWORD *)(a1 + 12) = result;
return result;
}

original_code:
void USART_Open(USART * serial, unsigned char port, unsigned char baud_rate, unsigned short tx_buf, unsigned short rx_buf, _Bool use_rs485) {
 char portname[10];
 serial->ref = 0;
 serial->CharacterReceived = 0;
 serial->port_num = port;
 serial->port = usart_ports[port];
 serial->baud_rate = baud_rate;
 serial->use_rs485 = use_rs485;
 RingBufferInit(&serial->tx_buffer, tx_buf);
 RingBufferInit(&serial->rx_buffer, rx_buf);
 USART_Table[port] = serial;
 sprintf(portname,"serial.%d",port);
 serial->fd = open(portname, 0100 | 01000 | 02, (0400|0200|0100) | (0400 >> 3));
}

canonical_original_code:
void USART_Open(struct USART *serial, unsigned char port, unsigned char baud_rate, unsigned short tx_buf, unsigned short rx_buf, _Bool use_rs485) {
 char portname[10];
 serial->ref = 0;
 serial->CharacterReceived = 0;
 serial->port_num = port;
 serial->port = usart_ports[port];
 serial->baud_rate = baud_rate;
 serial->use_rs485 = use_rs485;
 RingBufferInit(&serial->tx_buffer, tx_buf);
 RingBufferInit(&serial->rx_buffer, rx_buf);
 USART_Table[port] = serial;
 sprintf(portname,"serial.%d",port);
 serial->fd = open(portname, 0100 | 01000 | 02, (0400|0200|0100) | (0400 >> 3));
}

variable_types: struct USART { char port_num; int baud_rate; _Bool use_rs485; int fd; struct USART_Port { unsigned char usart_num; } port; struct RingBuffer { unsigned char *data; unsigned short start; unsigned short size; } tx_buffer; struct RingBuffer rx_buffer; void *ref; void (*CharacterReceived)(struct USART *port); } *

variable_types:
port
unsigned char

variable_types:
portname
char [10]

user_defined_types: struct USART { char port_num; int baud_rate; _Bool use_rs485; int fd; struct USART_Port port; struct RingBuffer tx_buffer; struct RingBuffer rx_buffer; void *ref; void (*CharacterReceived)(struct USART *port); }


decompiled_code:
__int64 __fastcall USART_Close(__int64 a1)
{
RingBufferDelete(a1 + 24);
return RingBufferDelete(a1 + 40);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
RingBufferDelete(a1 + 24);
return RingBufferDelete(a1 + 40);
}

original_code:
void USART_Close(USART * serial) {
 RingBufferDelete(&serial->tx_buffer);
 RingBufferDelete(&serial->rx_buffer);
}

canonical_original_code:
void USART_Close(struct USART *serial) {
 RingBufferDelete(&serial->tx_buffer);
 RingBufferDelete(&serial->rx_buffer);
}

user_defined_types: struct USART_Port { unsigned char usart_num; }

user_defined_types: struct RingBuffer { unsigned char *data; unsigned short start; unsigned short size; }


decompiled_code:
__int64 __fastcall USART_Read(__int64 a1, __int64 a2, unsigned __int16 a3)
{
return RingBufferGetData(a1 + 40, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned __int16 a3)
{
return RingBufferGetData(a1 + 40, a2, a3);
}

original_code:
unsigned short USART_Read(USART * serial, char * buf, unsigned short max_bytes) {
 return RingBufferGetData(&serial->rx_buffer, buf, max_bytes);
}

canonical_original_code:
unsigned short USART_Read(struct USART *serial, char *buf, unsigned short max_bytes) {
 return RingBufferGetData(&serial->rx_buffer, buf, max_bytes);
}

variable_types:
buf
char *

return_type: unsigned short


decompiled_code:
__int64 __fastcall USART_ReadByte(__int64 a1)
{
return RingBufferGetByte(a1 + 40);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return RingBufferGetByte(a1 + 40);
}

original_code:
unsigned char USART_ReadByte(USART * serial) {
 return RingBufferGetByte(&serial->rx_buffer);
}

canonical_original_code:
unsigned char USART_ReadByte(struct USART *serial) {
 return RingBufferGetByte(&serial->rx_buffer);
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall USART_TXFree(__int64 a1)
{
return RingBufferBytesFree(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return RingBufferBytesFree(a1 + 24);
}

original_code:
unsigned short USART_TXFree(USART * serial) {
 return RingBufferBytesFree(&serial->tx_buffer);
}

canonical_original_code:
unsigned short USART_TXFree(struct USART *serial) {
 return RingBufferBytesFree(&serial->tx_buffer);
}


decompiled_code:
__int64 __fastcall USART_TXUsed(__int64 a1)
{
return RingBufferBytesUsed(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return RingBufferBytesUsed(a1 + 24);
}

original_code:
unsigned short USART_TXUsed(USART * serial) {
 return RingBufferBytesUsed(&serial->tx_buffer);
}

canonical_original_code:
unsigned short USART_TXUsed(struct USART *serial) {
 return RingBufferBytesUsed(&serial->tx_buffer);
}


decompiled_code:
__int64 __fastcall USART_RXFree(__int64 a1)
{
return RingBufferBytesFree(a1 + 40);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return RingBufferBytesFree(a1 + 40);
}

original_code:
unsigned short USART_RXFree(USART * serial) {
 return RingBufferBytesFree(&serial->rx_buffer);
}

canonical_original_code:
unsigned short USART_RXFree(struct USART *serial) {
 return RingBufferBytesFree(&serial->rx_buffer);
}


decompiled_code:
__int64 __fastcall USART_RXUsed(__int64 a1)
{
return RingBufferBytesUsed(a1 + 40);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return RingBufferBytesUsed(a1 + 40);
}

original_code:
unsigned short USART_RXUsed(USART * serial) {
 return RingBufferBytesUsed(&serial->rx_buffer);
}

canonical_original_code:
unsigned short USART_RXUsed(struct USART *serial) {
 return RingBufferBytesUsed(&serial->rx_buffer);
}


decompiled_code:
int __fastcall USART_TXIntComplete(unsigned __int8 a1)
{
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

result = RingBufferBytesUsed(USART_Table[a1] + 24);
if ( (_WORD)result )
{
buf[0] = RingBufferGetByte(USART_Table[a1] + 24);
write(*(_DWORD *)(USART_Table[a1] + 12), buf, 1uLL);
result = printf("S[%d]: 0x%x\n", a1, (unsigned __int8)buf[0]);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(unsigned __int8 a1)
{
int result; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

result = RingBufferBytesUsed(USART_Table[a1] + 24);
if ( (_WORD)result )
{
buf[0] = RingBufferGetByte(USART_Table[a1] + 24);
write(*(_DWORD *)(USART_Table[a1] + 12), buf, 1uLL);
result = printf("S[%d]: 0x%x\n", a1, (unsigned __int8)buf[0]);
}
return result;
}

original_code:
void USART_TXIntComplete(unsigned char port) {
 if (RingBufferBytesUsed(&USART_Table[port]->tx_buffer)==0) {
 }
 else {
  unsigned char dat = RingBufferGetByte(&USART_Table[port]->tx_buffer);
  write(USART_Table[port]->fd, &dat, 1);
  printf("S[%d]: 0x%x\n",port,dat);
 }
}

variable_types:
dat
unsigned char


decompiled_code:
__int64 __fastcall USART_Write(unsigned __int8 *a1, __int64 a2, unsigned __int16 a3)
{
unsigned int v3; // ebp

v3 = RingBufferAdd(a1 + 24, a2, a3);
USART_TXIntComplete(*a1);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned __int8 *a1, __int64 a2, unsigned __int16 a3)
{
unsigned int v3; // ebp

v3 = RingBufferAdd(a1 + 24, a2, a3);
func9(*a1);
return v3;
}

original_code:
unsigned short USART_Write(USART * serial, unsigned char * buf, unsigned short num_bytes) {
 unsigned short written;
 written = RingBufferAdd(&serial->tx_buffer, buf, num_bytes);
 USART_TXIntComplete(serial->port_num);
 return written;
}

canonical_original_code:
unsigned short USART_Write(struct USART *serial, unsigned char *buf, unsigned short num_bytes) {
 unsigned short written;
 written = RingBufferAdd(&serial->tx_buffer, buf, num_bytes);
 USART_TXIntComplete(serial->port_num);
 return written;
}

variable_types:
buf
unsigned char *

variable_types:
written
unsigned short


decompiled_code:
__int64 __fastcall USART_WriteByte(unsigned __int8 *a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return USART_Write(a1, (__int64)v3, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned __int8 *a1, char a2)
{
char v3[12]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
return func10(a1, (__int64)v3, 1u);
}

original_code:
void USART_WriteByte(USART * serial, unsigned char dat) {
 USART_Write(serial,&dat,1);
}

canonical_original_code:
void USART_WriteByte(struct USART *serial, unsigned char dat) {
 USART_Write(serial,&dat,1);
}


RingBufferInit

decompiled_code:
void *__fastcall RingBufferInit(__int64 a1, unsigned __int16 a2)
{
void *result; // rax

result = malloc(a2);
*(_QWORD *)a1 = result;
*(_WORD *)(a1 + 8) = 0;
*(_WORD *)(a1 + 10) = 0;
*(_WORD *)(a1 + 12) = 0;
*(_WORD *)(a1 + 14) = a2;
return result;
}

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

result = malloc(a2);
*(_QWORD *)a1 = result;
*(_WORD *)(a1 + 8) = 0;
*(_WORD *)(a1 + 10) = 0;
*(_WORD *)(a1 + 12) = 0;
*(_WORD *)(a1 + 14) = a2;
return result;
}

original_code:
void RingBufferInit(RingBuffer * buf, unsigned short size) {
 buf->data = (unsigned char*)malloc(sizeof(unsigned char)*size);
 buf->start = 0;
 buf->end = 0;
 buf->count = 0;
 buf->size = size;
}

canonical_original_code:
void RingBufferInit(struct RingBuffer *buf, unsigned short size) {
 buf->data = (unsigned char *)malloc(sizeof(unsigned char)*size);
 buf->start = 0;
 buf->end = 0;
 buf->count = 0;
 buf->size = size;
}

variable_types: struct RingBuffer { unsigned char *data; unsigned short start; unsigned short size; } *

variable_types:
size
unsigned short


RingBufferDelete

decompiled_code:
void __fastcall RingBufferDelete(__int64 a1)
{
free(*(void **)a1);
*(_QWORD *)a1 = 0LL;
*(_WORD *)(a1 + 8) = 0;
*(_WORD *)(a1 + 10) = 0;
}

original_code:
void RingBufferDelete(RingBuffer * buf) {
 free(buf->data);
 buf->data = 0;
 buf->start = 0;
 buf->end = 0;
}

canonical_original_code:
void RingBufferDelete(struct RingBuffer *buf) {
 free(buf->data);
 buf->data = 0;
 buf->start = 0;
 buf->end = 0;
}


RingBufferAddByte

decompiled_code:
__int64 __fastcall RingBufferAddByte(__int64 a1, char a2)
{
unsigned __int16 v2; // cx
__int64 result; // rax

v2 = *(_WORD *)(a1 + 10);
result = 0LL;
if ( v2 + 1 % *(unsigned __int16 *)(a1 + 14) != *(unsigned __int16 *)(a1 + 8) )
{
++*(_WORD *)(a1 + 12);
*(_BYTE *)(*(_QWORD *)a1 + v2) = a2;
*(_WORD *)(a1 + 10) = (*(unsigned __int16 *)(a1 + 10) + 1) % *(unsigned __int16 *)(a1 + 14);
result = 1LL;
}
return result;
}

original_code:
_Bool RingBufferAddByte(RingBuffer * buf, unsigned char data) {
 if (buf->end+1 % buf->size != buf->start) {
  buf->count++;
  buf->data[buf->end] = data;
  buf->end = (buf->end+1) % buf->size;
  return 1;
 }
 return 0;
}

canonical_original_code:
_Bool RingBufferAddByte(struct RingBuffer *buf, unsigned char data) {
 if (buf->end+1 % buf->size != buf->start) {
  buf->count++;
  buf->data[buf->end] = data;
  buf->end = (buf->end+1) % buf->size;
  return 1;
 }
 return 0;
}


RingBufferAdd

decompiled_code:
__int64 __fastcall RingBufferAdd(__int64 a1, char *a2, unsigned int a3)
{
__int16 v3; // r13
unsigned int v5; // ebx

if ( !(_WORD)a3 )
return a3;
v3 = a3;
v5 = 0;
do
{
if ( !(unsigned __int8)RingBufferAddByte(a1, *a2) )
break;
++v5;
++a2;
}
while ( v3 != (_WORD)v5 );
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char *a2, unsigned int a3)
{
__int16 v3; // r13
unsigned int v5; // ebx

if ( !(_WORD)a3 )
return a3;
v3 = a3;
v5 = 0;
do
{
if ( !(unsigned __int8)func2(a1, *a2) )
break;
++v5;
++a2;
}
while ( v3 != (_WORD)v5 );
return v5;
}

original_code:
unsigned short RingBufferAdd(RingBuffer * buf, unsigned char * data, unsigned short size) {
 unsigned short i;
 for (i=0;i< size; i++) {
  if (!RingBufferAddByte(buf, data[i]))
   break;
 }
 return i;
}

canonical_original_code:
unsigned short RingBufferAdd(struct RingBuffer *buf, unsigned char *data, unsigned short size) {
 unsigned short i;
 for (i=0;i< size; i++) {
  if (!RingBufferAddByte(buf, data[i]))
   break;
 }
 return i;
}

variable_types:
data
unsigned char *

variable_types:
i
unsigned short


RingBufferGetByte

original_code:
unsigned char RingBufferGetByte(RingBuffer * buf) {
 if (buf->end!=buf->start) {
  unsigned char dat = buf->data[buf->start];
  buf->start = (buf->start+1) % buf->size;
  buf->count--;
  return dat;
 }
 return 0;
}

canonical_original_code:
unsigned char RingBufferGetByte(struct RingBuffer *buf) {
 if (buf->end!=buf->start) {
  unsigned char dat = buf->data[buf->start];
  buf->start = (buf->start+1) % buf->size;
  buf->count--;
  return dat;
 }
 return 0;
}


RingBufferBytesUsed

decompiled_code:
__int64 __fastcall RingBufferBytesUsed(__int64 a1)
{
return *(unsigned __int16 *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return *(unsigned __int16 *)(a1 + 12);
}

original_code:
unsigned short RingBufferBytesUsed(RingBuffer * buf) {
 return buf->count;
}

canonical_original_code:
unsigned short RingBufferBytesUsed(struct RingBuffer *buf) {
 return buf->count;
}


RingBufferGetData

decompiled_code:
__int64 __fastcall RingBufferGetData(__int64 a1, _BYTE *a2, unsigned int a3)
{
unsigned int v4; // eax
_BYTE *v5; // rbx

v4 = RingBufferBytesUsed(a1);
if ( (unsigned __int16)v4 <= (unsigned __int16)a3 )
a3 = v4;
if ( (_WORD)a3 )
{
v5 = a2;
do
*v5++ = RingBufferGetByte(a1);
while ( v5 != &a2[(unsigned __int16)(a3 - 1) + 1] );
}
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, _BYTE *a2, unsigned int a3)
{
unsigned int v4; // eax
_BYTE *v5; // rbx

v4 = func5(a1);
if ( (unsigned __int16)v4 <= (unsigned __int16)a3 )
a3 = v4;
if ( (_WORD)a3 )
{
v5 = a2;
do
*v5++ = func4(a1);
while ( v5 != &a2[(unsigned __int16)(a3 - 1) + 1] );
}
return a3;
}

original_code:
unsigned short RingBufferGetData(RingBuffer * buf, unsigned char * dest_buf, unsigned short bytes) {
 unsigned short i, toRead;
 if (RingBufferBytesUsed(buf) < bytes) {
  toRead = RingBufferBytesUsed(buf);
 }
 else {
  toRead = bytes;
 }
 for (i=0;i<toRead;i++) {
  dest_buf[i] = RingBufferGetByte(buf);
 }
 return toRead;
}

canonical_original_code:
unsigned short RingBufferGetData(struct RingBuffer *buf, unsigned char *dest_buf, unsigned short bytes) {
 unsigned short i; unsigned short toRead;
 if (RingBufferBytesUsed(buf) < bytes) {
  toRead = RingBufferBytesUsed(buf);
 }
 else {
  toRead = bytes;
 }
 for (i=0;i<toRead;i++) {
  dest_buf[i] = RingBufferGetByte(buf);
 }
 return toRead;
}

variable_types:
bytes
unsigned short

variable_types:
toRead
unsigned short


RingBufferBytesFree

decompiled_code:
__int16 __fastcall RingBufferBytesFree(__int64 a1)
{
return *(_WORD *)(a1 + 14) - *(_WORD *)(a1 + 12);
}

original_code:
unsigned short RingBufferBytesFree(RingBuffer * buf) {
 return buf->size - buf->count;
}

canonical_original_code:
unsigned short RingBufferBytesFree(struct RingBuffer *buf) {
 return buf->size - buf->count;
}


decompiled_code:
__int64 HighSpeedSetI2CStop()
{
int v0; // esi
char v1; // r8
char v2; // r10
int v3; // eax
int v4; // edx
int v5; // ecx
int v6; // ecx
int v7; // edx
int v8; // esi
__int64 result; // rax

v0 = dwNumBytesToSend;
v1 = gpio;
v2 = 16 * gpio;
v3 = dwNumBytesToSend + 12;
v4 = dwNumBytesToSend;
do
{
OutputBuffer[v4] = 0x80;
v5 = v4 + 2;
OutputBuffer[v4 + 1] = v2 | 1;
v4 += 3;
OutputBuffer[v5] = -13;
}
while ( v4 != v3 );
v6 = v0 + 24;
do
{
OutputBuffer[v3] = 0x80;
v7 = v3 + 2;
OutputBuffer[v3 + 1] = v2 | 3;
v8 = v3;
v3 += 3;
OutputBuffer[v7] = -13;
}
while ( v6 != v3 );
OutputBuffer[v6] = 0x80;
OutputBuffer[v8 + 4] = 16 * v1;
dwNumBytesToSend = v8 + 6;
result = (unsigned int)(v8 + 5);
OutputBuffer[result] = -16;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // esi
char v1; // r8
char v2; // r10
int v3; // eax
int v4; // edx
int v5; // ecx
int v6; // ecx
int v7; // edx
int v8; // esi
__int64 result; // rax

v0 = dwNumBytesToSend;
v1 = gpio;
v2 = 16 * gpio;
v3 = dwNumBytesToSend + 12;
v4 = dwNumBytesToSend;
do
{
OutputBuffer[v4] = 0x80;
v5 = v4 + 2;
OutputBuffer[v4 + 1] = v2 | 1;
v4 += 3;
OutputBuffer[v5] = -13;
}
while ( v4 != v3 );
v6 = v0 + 24;
do
{
OutputBuffer[v3] = 0x80;
v7 = v3 + 2;
OutputBuffer[v3 + 1] = v2 | 3;
v8 = v3;
v3 += 3;
OutputBuffer[v7] = -13;
}
while ( v6 != v3 );
OutputBuffer[v6] = 0x80;
OutputBuffer[v8 + 4] = 16 * v1;
dwNumBytesToSend = v8 + 6;
result = (unsigned int)(v8 + 5);
OutputBuffer[result] = -16;
return result;
}

original_code:
void HighSpeedSetI2CStop(void) {
 int dwCount;
 for(dwCount=0; dwCount<4; dwCount++) {
  OutputBuffer[dwNumBytesToSend++] = '\x80';
  OutputBuffer[dwNumBytesToSend++] = '\x01' | (gpio << 4);
  OutputBuffer[dwNumBytesToSend++] = '\xF3';
 }
 for(dwCount=0; dwCount<4; dwCount++) {
  OutputBuffer[dwNumBytesToSend++] = '\x80';
  OutputBuffer[dwNumBytesToSend++] = '\x03' | (gpio << 4);
  OutputBuffer[dwNumBytesToSend++] = '\xF3';
 }
 OutputBuffer[dwNumBytesToSend++] = '\x80';
 OutputBuffer[dwNumBytesToSend++] = '\x00' | (gpio << 4);
 OutputBuffer[dwNumBytesToSend++] = '\xF0';
}

canonical_original_code:
void HighSpeedSetI2CStop() {
 int dwCount;
 for(dwCount=0; dwCount<4; dwCount++) {
  OutputBuffer[dwNumBytesToSend++] = '\x80';
  OutputBuffer[dwNumBytesToSend++] = '\x01' | (gpio << 4);
  OutputBuffer[dwNumBytesToSend++] = '\xF3';
 }
 for(dwCount=0; dwCount<4; dwCount++) {
  OutputBuffer[dwNumBytesToSend++] = '\x80';
  OutputBuffer[dwNumBytesToSend++] = '\x03' | (gpio << 4);
  OutputBuffer[dwNumBytesToSend++] = '\xF3';
 }
 OutputBuffer[dwNumBytesToSend++] = '\x80';
 OutputBuffer[dwNumBytesToSend++] = '\x00' | (gpio << 4);
 OutputBuffer[dwNumBytesToSend++] = '\xF0';
}

variable_types: dwCount int


SendByteAndCheckACK

decompiled_code:
__int64 __fastcall SendByteAndCheckACK(char a1)
{
int v1; // eax
int v2; // edx
unsigned int v3; // eax
unsigned int v4; // ebx
int v5; // edx

v1 = dwNumBytesToSend;
v2 = dwNumBytesToSend;
OutputBuffer[dwNumBytesToSend] = 17;
OutputBuffer[v2 + 1] = 0;
OutputBuffer[v1 + 2] = 0;
OutputBuffer[v1 + 3] = a1;
OutputBuffer[v1 + 4] = 0x80;
OutputBuffer[v1 + 5] = 16 * gpio;
OutputBuffer[v1 + 6] = -15;
OutputBuffer[v1 + 7] = 34;
OutputBuffer[v1 + 8] = 0;
dwNumBytesToSend = v1 + 10;
OutputBuffer[v1 + 9] = -121;
dwNumBytesSent = ftdi_write_data();
dwNumBytesToSend = 0;
v3 = ftdi_read_data();
dwNumBytesRead = v3;
v4 = 0;
if ( v3 )
{
v4 = ((unsigned __int8)InputBuffer ^ 1) & 1;
if ( debug )
printf("Received: %d, %02X\n", v3, (unsigned __int8)InputBuffer);
v5 = dwNumBytesToSend;
OutputBuffer[dwNumBytesToSend] = 0x80;
OutputBuffer[v5 + 1] = (16 * gpio) | 2;
dwNumBytesToSend = v5 + 3;
OutputBuffer[v5 + 2] = -13;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(char a1)
{
int v1; // eax
int v2; // edx
unsigned int v3; // eax
unsigned int v4; // ebx
int v5; // edx

v1 = dwNumBytesToSend;
v2 = dwNumBytesToSend;
OutputBuffer[dwNumBytesToSend] = 17;
OutputBuffer[v2 + 1] = 0;
OutputBuffer[v1 + 2] = 0;
OutputBuffer[v1 + 3] = a1;
OutputBuffer[v1 + 4] = 0x80;
OutputBuffer[v1 + 5] = 16 * gpio;
OutputBuffer[v1 + 6] = -15;
OutputBuffer[v1 + 7] = 34;
OutputBuffer[v1 + 8] = 0;
dwNumBytesToSend = v1 + 10;
OutputBuffer[v1 + 9] = -121;
dwNumBytesSent = ftdi_write_data();
dwNumBytesToSend = 0;
v3 = ftdi_read_data();
dwNumBytesRead = v3;
v4 = 0;
if ( v3 )
{
v4 = ((unsigned __int8)InputBuffer ^ 1) & 1;
if ( debug )
printf("Received: %d, %02X\n", v3, (unsigned __int8)InputBuffer);
v5 = dwNumBytesToSend;
OutputBuffer[dwNumBytesToSend] = 0x80;
OutputBuffer[v5 + 1] = (16 * gpio) | 2;
dwNumBytesToSend = v5 + 3;
OutputBuffer[v5 + 2] = -13;
}
return v4;
}

original_code:
int SendByteAndCheckACK(unsigned char DataSend) {
 int ftStatus = 0;
 int r;
 OutputBuffer[dwNumBytesToSend++] = MSB_FALLING_EDGE_CLOCK_BYTE_OUT;
 OutputBuffer[dwNumBytesToSend++] = '\x00';
 OutputBuffer[dwNumBytesToSend++] = '\x00';
 OutputBuffer[dwNumBytesToSend++] = DataSend;
 OutputBuffer[dwNumBytesToSend++] = '\x80';
 OutputBuffer[dwNumBytesToSend++] = '\x00' | (gpio << 4);
 OutputBuffer[dwNumBytesToSend++] = '\xF1';
 OutputBuffer[dwNumBytesToSend++] = MSB_RISING_EDGE_CLOCK_BIT_IN;
 OutputBuffer[dwNumBytesToSend++] = '\x0';
 OutputBuffer[dwNumBytesToSend++] = '\x87';
 dwNumBytesSent = ftdi_write_data(&ftdic, OutputBuffer, dwNumBytesToSend);
 dwNumBytesToSend = 0;
 dwNumBytesRead = ftdi_read_data(&ftdic, InputBuffer, 1);
 if(dwNumBytesRead == 0) {
  return 0;
 }
 else if(!(InputBuffer[0] & 0x01)) {
  r = 1;
 }
 else
  r = 0;
 if(debug)
  printf("Received: %d, %02X\n", dwNumBytesRead, InputBuffer[0]);
 OutputBuffer[dwNumBytesToSend++] = '\x80';
 OutputBuffer[dwNumBytesToSend++] = '\x02' | (gpio << 4);
 OutputBuffer[dwNumBytesToSend++] = '\xF3';
 return r;
}

variable_types:
DataSend
unsigned char

variable_types: ftStatus int

variable_types: r int


repo: OrionExplorer/battery-http-server

decompiled_code:
void server_log_prepare()
{
char *v0; // rbx

v0 = (char *)malloc(0x401uLL);
strncpy(v0, app_path, 0x400uLL);
strcat(v0, "logs/");
if ( (unsigned __int16)directory_exists(v0) )
goto LABEL_5;
LOG_print("Creating path: %s...\n", v0);
if ( mkdir(v0, 0x1FFu) )
LOG_print("\t- Error creating path!\n");
if ( chdir(app_path) )
{
LOG_print("Error: chdir().\n");
}
else
{
LABEL_5:
LOG_print("\t- verified: %s.\n", v0);
strncpy(LOG_filename, v0, 0x400uLL);
*(_QWORD *)&LOG_filename[strlen(LOG_filename)] = 0x7478742E676F6CLL;
LOG_print("NEW SERVER SESSION.\n");
LOG_print("LOG_filename: %s.\n", LOG_filename);
free(v0);
}
}

canonical_decompiled_code:
void func0()
{
char *v0; // rbx

v0 = (char *)malloc(0x401uLL);
strncpy(v0, app_path, 0x400uLL);
strcat(v0, "logs/");
if ( (unsigned __int16)directory_exists(v0) )
goto LABEL_5;
LOG_print("Creating path: %s...\n", v0);
if ( mkdir(v0, 0x1FFu) )
LOG_print("\t- Error creating path!\n");
if ( chdir(app_path) )
{
LOG_print("Error: chdir().\n");
}
else
{
LABEL_5:
LOG_print("\t- verified: %s.\n", v0);
strncpy(LOG_filename, v0, 0x400uLL);
*(_QWORD *)&LOG_filename[strlen(LOG_filename)] = 0x7478742E676F6CLL;
LOG_print("NEW SERVER SESSION.\n");
LOG_print("LOG_filename: %s.\n", LOG_filename);
free(v0);
}
}

original_code:
static void server_log_prepare( void ) {
    char *tmp_path = malloc( 1024*sizeof( char )+1 );
    strncpy( tmp_path, app_path, 1024 );
    strncat( tmp_path, "logs""/", 1024 );
    if( directory_exists( tmp_path ) == 0 ) {
        LOG_print( "Creating path: %s...\n", tmp_path );
        if( mkdir( tmp_path, 0777 ) != 0 ) {
            LOG_print( "\t- Error creating path!\n" );
        }
        if( chdir( app_path ) != 0 ) {
            LOG_print( "Error: chdir().\n" );
            return;
        }
    }
    LOG_print( "\t- verified: %s.\n", tmp_path );
    strncpy( LOG_filename, tmp_path, 1024 );
    strncat( LOG_filename, "log.txt", 1024 );
    LOG_print( "NEW SERVER SESSION.\n" );
    LOG_print( "LOG_filename: %s.\n", LOG_filename );
    free( tmp_path );
    tmp_path = ((void *)0);
}

canonical_original_code:
static void server_log_prepare() {
    char *tmp_path = malloc( 1024*sizeof( char )+1 );
    strncpy( tmp_path, app_path, 1024 );
    strncat( tmp_path, "logs""/", 1024 );
    if( directory_exists( tmp_path ) == 0 ) {
        LOG_print( "Creating path: %s...\n", tmp_path );
        if( mkdir( tmp_path, 0777 ) != 0 ) {
            LOG_print( "\t- Error creating path!\n" );
        }
        if( chdir( app_path ) != 0 ) {
            LOG_print( "Error: chdir().\n" );
            return;
        }
    }
    LOG_print( "\t- verified: %s.\n", tmp_path );
    strncpy( LOG_filename, tmp_path, 1024 );
    strncat( LOG_filename, "log.txt", 1024 );
    LOG_print( "NEW SERVER SESSION.\n" );
    LOG_print( "LOG_filename: %s.\n", LOG_filename );
    free( tmp_path );
    tmp_path = ((void *)0);
}


decompiled_code:
void server_validate_paths()
{
char *v0; // rbx

v0 = (char *)malloc(0x401uLL);
LOG_print("Starting server_validate_paths()...\n");
strncpy(v0, app_path, 0x400uLL);
strcat(v0, "logs/");
if ( (unsigned __int16)directory_exists(v0) )
goto LABEL_5;
LOG_print("Creating path: %s...\n", v0);
if ( mkdir(v0, 0x1FFu) )
LOG_print("\t- Error ( %d ) creating path!\n", 0xFFFFFFFFLL);
if ( chdir(app_path) )
{
LOG_print("Error: chdir().\n");
}
else
{
LABEL_5:
LOG_print("\t- verified: %s\n", v0);
server_log_prepare();
LOG_print("server_validate_paths() done.\n");
free(v0);
}
}

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

v0 = (char *)malloc(0x401uLL);
LOG_print("Starting server_validate_paths()...\n");
strncpy(v0, app_path, 0x400uLL);
strcat(v0, "logs/");
if ( (unsigned __int16)directory_exists(v0) )
goto LABEL_5;
LOG_print("Creating path: %s...\n", v0);
if ( mkdir(v0, 0x1FFu) )
LOG_print("\t- Error ( %d ) creating path!\n", 0xFFFFFFFFLL);
if ( chdir(app_path) )
{
LOG_print("Error: chdir().\n");
}
else
{
LABEL_5:
LOG_print("\t- verified: %s\n", v0);
func0();
LOG_print("server_validate_paths() done.\n");
free(v0);
}
}

original_code:
static void server_validate_paths( void ) {
    char *tmp_path = malloc( 1024*sizeof( char )+1 );
    int res = -1;
    LOG_print( "Starting server_validate_paths()...\n" );
    strncpy( tmp_path, app_path, 1024 );
    strncat( tmp_path, "logs""/", 1024 );
    if( directory_exists( tmp_path ) == 0 ) {
        LOG_print( "Creating path: %s...\n", tmp_path );
        if( mkdir( tmp_path, 0777 ) != 0 ) {
            LOG_print( "\t- Error ( %d ) creating path!\n", res );
        }
        if( chdir( app_path ) != 0 ) {
            LOG_print( "Error: chdir().\n" );
            return;
        }
    }
    LOG_print( "\t- verified: %s\n", tmp_path );
    server_log_prepare();
    LOG_print( "server_validate_paths() done.\n" );
    free( tmp_path );
    tmp_path = ((void *)0);
}

canonical_original_code:
static void server_validate_paths() {
    char *tmp_path = malloc( 1024*sizeof( char )+1 );
    int res = -1;
    LOG_print( "Starting server_validate_paths()...\n" );
    strncpy( tmp_path, app_path, 1024 );
    strncat( tmp_path, "logs""/", 1024 );
    if( directory_exists( tmp_path ) == 0 ) {
        LOG_print( "Creating path: %s...\n", tmp_path );
        if( mkdir( tmp_path, 0777 ) != 0 ) {
            LOG_print( "\t- Error ( %d ) creating path!\n", res );
        }
        if( chdir( app_path ) != 0 ) {
            LOG_print( "Error: chdir().\n" );
            return;
        }
    }
    LOG_print( "\t- verified: %s\n", tmp_path );
    server_log_prepare();
    LOG_print( "server_validate_paths() done.\n" );
    free( tmp_path );
    tmp_path = ((void *)0);
}

variable_types: res int


decompiled_code:
__int64 CORE_initialize()
{
const char *v0; // rax

v0 = (const char *)get_app_path();
strncpy(app_path, v0, 0x400uLL);
server_validate_paths();
LOG_print("Application start path:\n%s\n", app_path);
if ( !(unsigned __int16)CORE_load_configuration() )
{
LOG_print("Error: unable to load configuration.\n");
puts("Error: unable to load configuration.");
LOG_save();
exit(1);
}
LOG_save();
atexit((void (*)(void))&SOCKET_free);
strncpy(app_path, document_root, 0x400uLL);
return SOCKET_main();
}

canonical_decompiled_code:
__int64 func4()
{
const char *v0; // rax

v0 = (const char *)get_app_path();
strncpy(app_path, v0, 0x400uLL);
func1();
LOG_print("Application start path:\n%s\n", app_path);
if ( !(unsigned __int16)func3() )
{
LOG_print("Error: unable to load configuration.\n");
puts("Error: unable to load configuration.");
LOG_save();
exit(1);
}
LOG_save();
atexit((void (*)(void))&SOCKET_free);
strncpy(app_path, document_root, 0x400uLL);
return SOCKET_main();
}

original_code:
void CORE_initialize( void ) {
    strncpy( app_path, get_app_path(), 1024 );
    ( void )server_validate_paths();
    LOG_print( "Application start path:\n%s\n", app_path );
    if( CORE_load_configuration() == 0 ) {
        LOG_print( "Error: unable to load configuration.\n" );
        printf( "Error: unable to load configuration.\n" );
        LOG_save();
        exit( 1 );
    }
    LOG_save();
    atexit( SOCKET_free );
    strncpy( app_path, document_root, 1024 );
    SOCKET_main();
}

canonical_original_code:
void CORE_initialize() {
    strncpy( app_path, get_app_path(), 1024 );
    ( void )server_validate_paths();
    LOG_print( "Application start path:\n%s\n", app_path );
    if( CORE_load_configuration() == 0 ) {
        LOG_print( "Error: unable to load configuration.\n" );
        printf( "Error: unable to load configuration.\n" );
        LOG_save();
        exit( 1 );
    }
    LOG_save();
    atexit( SOCKET_free );
    strncpy( app_path, document_root, 1024 );
    SOCKET_main();
}


decompiled_code:
char *get_app_path()
{
char *v0; // rdx
char *result; // rax

v0 = getcwd(buf_0, 0x400uLL);
result = (char *)&unk_45DEE0;
if ( v0 )
{
*(_WORD *)&buf_0[strlen(buf_0)] = 47;
result = buf_0;
}
return result;
}

canonical_decompiled_code:
char *func0()
{
char *v0; // rdx
char *result; // rax

v0 = getcwd(buf_0, 0x400uLL);
result = (char *)&unk_45DEE0;
if ( v0 )
{
*(_WORD *)&buf_0[strlen(buf_0)] = 47;
result = buf_0;
}
return result;
}

original_code:
char* get_app_path( void ) {
    static char buf[ 1024 ];
    if( getcwd( buf, 1024 ) ) {
        return strncat( buf, "/", 1024 );
    } else {
        return "";
    }
}

canonical_original_code:
char *get_app_path() {
    static char buf[1024];
    if( getcwd( buf, 1024 ) ) {
        return strncat( buf, "/", 1024 );
    } else {
        return "";
    }
}

variable_types:
buf
char [1024]

return_type: char *


decompiled_code:
_BOOL8 __fastcall directory_exists(const char *a1)
{
return chdir(a1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(const char *a1)
{
return chdir(a1) == 0;
}

original_code:
short directory_exists( const char *path ) {
    if( chdir( path ) == 0 ) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
short directory_exists(char *path) {
    if( chdir( path ) == 0 ) {
        return 1;
    } else {
        return 0;
    }
}

variable_types:
path
char *


decompiled_code:
void *__fastcall file_get_name(char *s)
{
char *v1; // rax
void *v2; // rdx
bool v3; // zf
void *result; // rax

strchr(s, 47);
v1 = strrchr(s, 47);
v2 = v1 + 1;
v3 = v1 == 0LL;
result = &unk_45DEE0;
if ( !v3 )
result = v2;
return result;
}

canonical_decompiled_code:
void *__fastcall func2(char *s)
{
char *v1; // rax
void *v2; // rdx
bool v3; // zf
void *result; // rax

strchr(s, 47);
v1 = strrchr(s, 47);
v2 = v1 + 1;
v3 = v1 == 0LL;
result = &unk_45DEE0;
if ( !v3 )
result = v2;
return result;
}

original_code:
char* file_get_name( const char *full_filename ) {
    char *filename;
    if( strstr( full_filename, "/" ) ) {
        filename = ( char* )strrchr( full_filename, '/' );
    } else {
        filename = ( char* )strrchr( full_filename, '/' );
    }
    if( filename ) {
        return ++filename;
    }
    return "";
}

canonical_original_code:
char *file_get_name(char *full_filename) {
    char *filename;
    if( strstr( full_filename, "/" ) ) {
        filename = ( char * )strrchr( full_filename, '/' );
    } else {
        filename = ( char * )strrchr( full_filename, '/' );
    }
    if( filename ) {
        return ++filename;
    }
    return "";
}

variable_types:
filename
char *


decompiled_code:
char *__fastcall file_get_ext(char *a1)
{
const char *v1; // rax
char *result; // rax

v1 = (const char *)file_get_name(a1);
result = strrchr(v1, 46);
if ( !result )
result = (char *)&unk_45DEE0;
return result;
}

canonical_decompiled_code:
char *__fastcall func3(char *a1)
{
const char *v1; // rax
char *result; // rax

v1 = (const char *)func2(a1);
result = strrchr(v1, 46);
if ( !result )
result = (char *)&unk_45DEE0;
return result;
}

original_code:
char* file_get_ext( const char *filename ) {
    char* file_ext = strrchr( file_get_name( filename ), '.' );
    if( !file_ext ) {
        return "";
    }
    return file_ext;
}

canonical_original_code:
char *file_get_ext(char *filename) {
    char *file_ext = strrchr( file_get_name( filename ), '.' );
    if( !file_ext ) {
        return "";
    }
    return file_ext;
}


decompiled_code:
const char *__fastcall file_extract_path(const char *a1, char a2)
{
const char *result; // rax
int v3; // ecx

result = (const char *)strlen(a1);
if ( (int)result > 1 )
{
v3 = (int)result;
result = &a1[(int)result - 1];
do
{
if ( *result == a2 )
*((_BYTE *)result + 1) = 0;
--result;
}
while ( result != &a1[v3 - 2 - (v3 - 2)] );
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(const char *a1, char a2)
{
const char *result; // rax
int v3; // ecx

result = (const char *)strlen(a1);
if ( (int)result > 1 )
{
v3 = (int)result;
result = &a1[(int)result - 1];
do
{
if ( *result == a2 )
*((_BYTE *)result + 1) = 0;
--result;
}
while ( result != &a1[v3 - 2 - (v3 - 2)] );
}
return result;
}

original_code:
void file_extract_path(char *full_filename, char delim)
{
    int i = strlen(full_filename);
    if(i > 0) {
        while(--i) {
            if(full_filename[ i ] == delim) {
                full_filename[ i+1 ] = '\0';
            }
        }
    }
}

variable_types: delim char

variable_types: i int


decompiled_code:
void __noreturn app_terminate()
{
LOG_print("Server is being closed...\n");
SOCKET_free();
LOG_print("Server closed.\n");
LOG_save();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
LOG_print("Server is being closed...\n");
SOCKET_free();
LOG_print("Server closed.\n");
LOG_save();
exit(0);
}

original_code:
static void app_terminate( void ) {
    LOG_print( "Server is being closed...\n" );
    SOCKET_free();
    LOG_print( "Server closed.\n" );
    LOG_save();
    exit( 0 );
}

canonical_original_code:
static void app_terminate() {
    LOG_print( "Server is being closed...\n" );
    SOCKET_free();
    LOG_print( "Server closed.\n" );
    LOG_save();
    exit( 0 );
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
signal(2, (__sighandler_t)app_terminate);
signal(13, (__sighandler_t)((char *)app_terminate + 1));
signal(0x10000000, (__sighandler_t)app_terminate);
signal(6, (__sighandler_t)app_terminate);
signal(15, (__sighandler_t)app_terminate);
puts("battery/linux-1.0.0");
CORE_initialize();
return 0;
}

original_code:
int main( void ) {
    signal( 2, ( __sighandler_t )&app_terminate );
        signal( 13, ((__sighandler_t) 1) );
        signal( 0x10000000, ( __sighandler_t )&app_terminate );
    signal( 6, ( __sighandler_t )&app_terminate );
    signal( 15, ( __sighandler_t )&app_terminate );
    printf( "%s\n", "battery""/""linux""-1.0.0" );
    CORE_initialize();
    return 0;
}

canonical_original_code:
int main() {
    signal( 2, ( __sighandler_t )&app_terminate );
        signal( 13, ((void (*)(int)) 1) );
        signal( 0x10000000, ( __sighandler_t )&app_terminate );
    signal( 6, ( __sighandler_t )&app_terminate );
    signal( 15, ( __sighandler_t )&app_terminate );
    printf( "%s\n", "battery""/""linux""-1.0.0" );
    CORE_initialize();
    return 0;
}


decompiled_code:
__int64 __fastcall base64_encode(void *src, size_t n, _BYTE *a3, __int64 a4)
{
size_t v4; // rbx
__int64 result; // rax
__int16 dest; // [rsp+Dh] [rbp-1Bh] BYREF
char v9; // [rsp+Fh] [rbp-19h]

v4 = n;
result = 0LL;
if ( 4 * ((n + 2) / 3) <= a4 - 1 )
{
if ( n > 2 )
{
do
{
base64_encode_triple((unsigned __int8 *)src, a3);
v4 -= 3LL;
src = (char *)src + 3;
a3 += 4;
}
while ( v4 > 2 );
}
if ( v4 )
{
dest = 0;
v9 = 0;
memcpy(&dest, src, v4);
base64_encode_triple((unsigned __int8 *)&dest, a3);
a3[3] = 61;
if ( v4 == 1 )
a3[2] = 61;
a3 += 4;
}
*a3 = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *src, size_t n, _BYTE *a3, __int64 a4)
{
size_t v4; // rbx
__int64 result; // rax
__int16 dest; // [rsp+Dh] [rbp-1Bh] BYREF
char v9; // [rsp+Fh] [rbp-19h]

v4 = n;
result = 0LL;
if ( 4 * ((n + 2) / 3) <= a4 - 1 )
{
if ( n > 2 )
{
do
{
func0((unsigned __int8 *)src, a3);
v4 -= 3LL;
src = (char *)src + 3;
a3 += 4;
}
while ( v4 > 2 );
}
if ( v4 )
{
dest = 0;
v9 = 0;
memcpy(&dest, src, v4);
func0((unsigned __int8 *)&dest, a3);
a3[3] = 61;
if ( v4 == 1 )
a3[2] = 61;
a3 += 4;
}
*a3 = 0;
result = 1LL;
}
return result;
}

original_code:
int base64_encode( unsigned char *source, size_t sourcelen, char *target, size_t targetlen )
{
    if ( ( sourcelen+2 )/3*4 > targetlen-1 )
        return 0;
    while ( sourcelen >= 3 )
    {
        _base64_encode_triple( source, target );
        sourcelen -= 3;
        source += 3;
        target += 4;
    }
    if ( sourcelen > 0 )
    {
        unsigned char temp[ 3 ] ;
        memset( temp, 0, sizeof( temp ) );
        memcpy( temp, source, sourcelen );
        _base64_encode_triple( temp, target );
        target[ 3 ] = '=';
        if ( sourcelen == 1 )
            target[ 2 ] = '=';
        target += 4;
    }
    target[ 0 ] = 0;
    return 1;
}

canonical_original_code:
int base64_encode(unsigned char *source, unsigned long sourcelen, char *target, unsigned long targetlen)
{
    if ( ( sourcelen+2 )/3*4 > targetlen-1 )
        return 0;
    while ( sourcelen >= 3 )
    {
        _base64_encode_triple( source, target );
        sourcelen -= 3;
        source += 3;
        target += 4;
    }
    if ( sourcelen > 0 )
    {
        unsigned char temp[3];
        memset( temp, 0, sizeof( temp ) );
        memcpy( temp, source, sourcelen );
        _base64_encode_triple( temp, target );
        target[ 3 ] = '=';
        if ( sourcelen == 1 )
            target[ 2 ] = '=';
        target += 4;
    }
    target[ 0 ] = 0;
    return 1;
}

variable_types:
source
unsigned char *

variable_types:
sourcelen
unsigned long

variable_types:
target
char *

variable_types:
targetlen
unsigned long

variable_types:
temp
unsigned char [3]


decompiled_code:
__int64 __fastcall base64_decode(char *src, char *a2, unsigned __int64 a3)
{
size_t v5; // rax
char *v6; // rax
char *v7; // rbp
char *v8; // r12
char i; // bl
int v10; // eax
__int64 v11; // rbx
__int64 v13; // [rsp+0h] [rbp-58h]
char *ptr; // [rsp+8h] [rbp-50h]
char srca[3]; // [rsp+19h] [rbp-3Fh] BYREF
char v16[4]; // [rsp+1Ch] [rbp-3Ch] BYREF
char v17; // [rsp+20h] [rbp-38h] BYREF

v5 = strlen(src);
v6 = (char *)malloc(v5 + 5);
ptr = v6;
if ( !v6 )
return -1LL;
v7 = v6;
strcpy(v6, src);
strcat(v7, "====");
v13 = 0LL;
do
{
v8 = v16;
do
{
for ( i = *v7; *v7 != 61; i = *++v7 )
{
if ( (int)base64_char_value(i) >= 0 )
break;
}
++v7;
*v8++ = i;
}
while ( &v17 != v8 );
v10 = base64_decode_triple((__int64)v16, (__int64)srca);
v11 = v10;
if ( v10 > a3 )
{
free(ptr);
return -1LL;
}
memcpy(a2, srca, v10);
a2 += v11;
a3 -= v11;
v13 += v11;
}
while ( v11 == 3 );
free(ptr);
return v13;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src, char *a2, unsigned __int64 a3)
{
size_t v5; // rax
char *v6; // rax
char *v7; // rbp
char *v8; // r12
char i; // bl
int v10; // eax
__int64 v11; // rbx
__int64 v13; // [rsp+0h] [rbp-58h]
char *ptr; // [rsp+8h] [rbp-50h]
char srca[3]; // [rsp+19h] [rbp-3Fh] BYREF
char v16[4]; // [rsp+1Ch] [rbp-3Ch] BYREF
char v17; // [rsp+20h] [rbp-38h] BYREF

v5 = strlen(src);
v6 = (char *)malloc(v5 + 5);
ptr = v6;
if ( !v6 )
return -1LL;
v7 = v6;
strcpy(v6, src);
strcat(v7, "====");
v13 = 0LL;
do
{
v8 = v16;
do
{
for ( i = *v7; *v7 != 61; i = *++v7 )
{
if ( (int)func2(i) >= 0 )
break;
}
++v7;
*v8++ = i;
}
while ( &v17 != v8 );
v10 = func3((__int64)v16, (__int64)srca);
v11 = v10;
if ( v10 > a3 )
{
free(ptr);
return -1LL;
}
memcpy(a2, srca, v10);
a2 += v11;
a3 -= v11;
v13 += v11;
}
while ( v11 == 3 );
free(ptr);
return v13;
}

original_code:
size_t base64_decode( char *source, unsigned char *target, size_t targetlen )
{
    char *src, *tmpptr;
    char quadruple[ 4 ], tmpresult[ 3 ];
    int i;
    size_t tmplen = 3;
    size_t converted = 0;
    src = ( char * )malloc( strlen( source )+5 );
    if ( src == ((void *)0) )
        return -1;
    strcpy( src, source );
    strcat( src, "====" );
    tmpptr = src;
    while ( tmplen == 3 )
    {
        for ( i=0; i<4; i++ )
        {
            while ( *tmpptr != '=' && _base64_char_value( *tmpptr )<0 )
                tmpptr++;
            quadruple[ i ]= *( tmpptr++ );
        }
        tmplen = _base64_decode_triple( quadruple, ( unsigned char* )tmpresult );
        if ( targetlen < tmplen )
        {
            free( src );
            return -1;
        }
        memcpy( target, tmpresult, tmplen );
        target += tmplen;
        targetlen -= tmplen;
        converted += tmplen;
    }
    free( src );
    return converted;
}

canonical_original_code:
unsigned long base64_decode(char *source, unsigned char *target, unsigned long targetlen)
{
    char *src; char *tmpptr;
    char quadruple[4]; char tmpresult[3];
    int i;
    unsigned long tmplen = 3;
    unsigned long converted = 0;
    src = ( char * )malloc( strlen( source )+5 );
    if ( src == ((void *)0) )
        return -1;
    strcpy( src, source );
    strcat( src, "====" );
    tmpptr = src;
    while ( tmplen == 3 )
    {
        for ( i=0; i<4; i++ )
        {
            while ( *tmpptr != '=' && _base64_char_value( *tmpptr )<0 )
                tmpptr++;
            quadruple[ i ]= *( tmpptr++ );
        }
        tmplen = _base64_decode_triple( quadruple, ( unsigned char * )tmpresult );
        if ( targetlen < tmplen )
        {
            free( src );
            return -1;
        }
        memcpy( target, tmpresult, tmplen );
        target += tmplen;
        targetlen -= tmplen;
        converted += tmplen;
    }
    free( src );
    return converted;
}

variable_types:
source
char *

variable_types:
target
unsigned char *

variable_types:
src
char *

variable_types:
tmpptr
char *

variable_types:
quadruple
char [4]

variable_types:
tmpresult
char [3]

variable_types:
tmplen
unsigned long

variable_types:
converted
unsigned long

return_type: unsigned long