decompiled_code:
__int64 __fastcall identify_filename(const char *a1)
{
int v1; // eax
bool v2; // zf
__int64 v3; // rbp
int v4; // er8
__int64 result; // rax
const char *v6; // rbp
int v7; // er8
int v8; // er8

if ( !a1 )
return 4LL;
v1 = strlen(a1);
v2 = v1 == 5;
if ( v1 <= 5 )
{
result = 4LL;
if ( !v2 )
return result;
v3 = 5LL;
}
else
{
v3 = v1;
v4 = strcasecmp(&a1[v1 - 5], ".rtfd");
result = 2LL;
if ( !v4 )
return result;
}
v6 = &a1[v3 - 4];
v7 = strcasecmp(v6, ".rtf");
result = 0LL;
if ( v7 )
{
v8 = strcasecmp(v6, ".eqn");
result = 1LL;
if ( v8 )
result = 3 - ((unsigned int)(strcasecmp(v6, ".rqn") == 0) - 1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1)
{
int v1; // eax
bool v2; // zf
__int64 v3; // rbp
int v4; // er8
__int64 result; // rax
const char *v6; // rbp
int v7; // er8
int v8; // er8

if ( !a1 )
return 4LL;
v1 = strlen(a1);
v2 = v1 == 5;
if ( v1 <= 5 )
{
result = 4LL;
if ( !v2 )
return result;
v3 = 5LL;
}
else
{
v3 = v1;
v4 = strcasecmp(&a1[v1 - 5], ".rtfd");
result = 2LL;
if ( !v4 )
return result;
}
v6 = &a1[v3 - 4];
v7 = strcasecmp(v6, ".rtf");
result = 0LL;
if ( v7 )
{
v8 = strcasecmp(v6, ".eqn");
result = 1LL;
if ( v8 )
result = 3 - ((unsigned int)(strcasecmp(v6, ".rqn") == 0) - 1);
}
return result;
}

original_code:
static enum INPUT_FILE_TYPE identify_filename(char *name)
{
    int len;
    if (!name) return TYPE_UNKNOWN;
    len = strlen(name);
    if (len > 5 && strcasecmp(name+len-5,".rtfd")==0)
        return TYPE_RTFD;
    if (len > 4 && strcasecmp(name+len-4,".rtf")==0)
        return TYPE_RTF;
    if (len > 4 && strcasecmp(name+len-4,".eqn")==0)
        return TYPE_EQN;
    if (len > 4 && strcasecmp(name+len-4,".rqn")==0)
        return TYPE_RAWEQN;
    return TYPE_UNKNOWN;
}

variable_types:
name
char *

variable_types: len int

return_type: enum INPUT_FILE_TYPE { TYPE_RTF=0, TYPE_EQN=1, TYPE_RTFD=2, TYPE_RAWEQN=3, TYPE_UNKNOWN=4 }


decompiled_code:
char *__fastcall short_name(char *s)
{
char *v1; // rax
char *result; // rax

v1 = strrchr(s, 47);
if ( v1 )
result = strdup(v1 + 1);
else
result = strdup(s);
return result;
}

canonical_decompiled_code:
char *__fastcall func5(char *s)
{
char *v1; // rax
char *result; // rax

v1 = strrchr(s, 47);
if ( v1 )
result = strdup(v1 + 1);
else
result = strdup(s);
return result;
}

original_code:
static char * short_name(char *path)
{
    char *s;
    s = strrchr(path,'/');
    if (s == ((void *)0))
        return strdup(path);
    else
        return strdup(s+1);
}

canonical_original_code:
static char *short_name(char *path)
{
    char *s;
    s = strrchr(path,'/');
    if (s == ((void *)0))
        return strdup(path);
    else
        return strdup(s+1);
}

variable_types:
path
char *

variable_types:
s
char *

return_type: char *


decompiled_code:
char *__fastcall make_output_filename(void *src)
{
char *v2; // r12
char *v3; // r13
size_t v4; // rbx
char *v5; // r12
const char *v6; // rsi
size_t v7; // rbx
char *v8; // rax

if ( !src )
return 0LL;
if ( g_input_file_type == 2 )
{
v7 = strlen((const char *)src);
v5 = (char *)malloc(v7 + 6);
memcpy(v5, src, v7 + 1);
v8 = strstr(v5, ".rtfd");
if ( v8 || (v8 = strstr(v5, ".RTFD")) != 0LL )
strcpy(v8, "-latex");
else
strcpy(&v5[v7], "-latex");
mkdir(v5, 0x1EDu);
v6 = "TXT.ltx";
}
else
{
if ( !g_create_new_directory || (g_input_file_type & 0xFFFFFFFD) == 1 )
{
v2 = strdup((const char *)src);
strcpy(&v2[strlen(v2) - 4], ".ltx");
return v2;
}
if ( g_input_file_type )
return 0LL;
v3 = short_name((char *)src);
strcpy(&v3[strlen(v3) - 4], ".ltx");
v4 = strlen((const char *)src);
v5 = (char *)malloc(v4 + 6);
memcpy(v5, src, v4 + 1);
strcpy(&v5[v4 - 4], "-latex");
mkdir(v5, 0x1EDu);
v6 = v3;
}
return (char *)append_file_to_path(v5, v6);
}

canonical_decompiled_code:
char *__fastcall func6(void *src)
{
char *v2; // r12
char *v3; // r13
size_t v4; // rbx
char *v5; // r12
const char *v6; // rsi
size_t v7; // rbx
char *v8; // rax

if ( !src )
return 0LL;
if ( g_input_file_type == 2 )
{
v7 = strlen((const char *)src);
v5 = (char *)malloc(v7 + 6);
memcpy(v5, src, v7 + 1);
v8 = strstr(v5, ".rtfd");
if ( v8 || (v8 = strstr(v5, ".RTFD")) != 0LL )
strcpy(v8, "-latex");
else
strcpy(&v5[v7], "-latex");
mkdir(v5, 0x1EDu);
v6 = "TXT.ltx";
}
else
{
if ( !g_create_new_directory || (g_input_file_type & 0xFFFFFFFD) == 1 )
{
v2 = strdup((const char *)src);
strcpy(&v2[strlen(v2) - 4], ".ltx");
return v2;
}
if ( g_input_file_type )
return 0LL;
v3 = func5((char *)src);
strcpy(&v3[strlen(v3) - 4], ".ltx");
v4 = strlen((const char *)src);
v5 = (char *)malloc(v4 + 6);
memcpy(v5, src, v4 + 1);
strcpy(&v5[v4 - 4], "-latex");
mkdir(v5, 0x1EDu);
v6 = v3;
}
return (char *)append_file_to_path(v5, v6);
}

original_code:
static char * make_output_filename(char * name)
{
    char *s, *dir, *file, *out;
    if (!name) return ((void *)0);
    if (g_input_file_type == TYPE_RTFD) {
        dir = malloc(strlen(name)+strlen("-latex"));
        strcpy(dir,name);
        s = strstr(dir, ".rtfd");
        if (!s)
         s = strstr(dir, ".RTFD");
        if (s)
         strcpy(s,"-latex");
        else
         strcat(dir,"-latex");
        mkdir(dir, 0755);
        out = append_file_to_path(dir,"TXT.ltx");
        return out;
    }
    if (!g_create_new_directory || g_input_file_type == TYPE_EQN || g_input_file_type == TYPE_RAWEQN) {
        s = strdup(name);
        strcpy(s+strlen(s)-4, ".ltx");
        return s;
    }
    if (g_input_file_type == TYPE_RTF || g_input_file_type == TYPE_RTFD) {
        file = short_name(name);
        strcpy(file+strlen(file)-4, ".ltx");
        dir = malloc(strlen(name)+strlen("-latex"));
        strcpy(dir,name);
        strcpy(dir+strlen(dir)-4,"-latex");
        mkdir(dir, 0755);
        out = append_file_to_path(dir,file);
        return out;
    }
    return ((void *)0);
}

canonical_original_code:
static char *make_output_filename(char *name)
{
    char *s; char *dir; char *file; char *out;
    if (!name) return ((void *)0);
    if (g_input_file_type == TYPE_RTFD) {
        dir = malloc(strlen(name)+strlen("-latex"));
        strcpy(dir,name);
        s = strstr(dir, ".rtfd");
        if (!s)
         s = strstr(dir, ".RTFD");
        if (s)
         strcpy(s,"-latex");
        else
         strcat(dir,"-latex");
        mkdir(dir, 0755);
        out = append_file_to_path(dir,"TXT.ltx");
        return out;
    }
    if (!g_create_new_directory || g_input_file_type == TYPE_EQN || g_input_file_type == TYPE_RAWEQN) {
        s = strdup(name);
        strcpy(s+strlen(s)-4, ".ltx");
        return s;
    }
    if (g_input_file_type == TYPE_RTF || g_input_file_type == TYPE_RTFD) {
        file = short_name(name);
        strcpy(file+strlen(file)-4, ".ltx");
        dir = malloc(strlen(name)+strlen("-latex"));
        strcpy(dir,name);
        strcpy(dir+strlen(dir)-4,"-latex");
        mkdir(dir, 0755);
        out = append_file_to_path(dir,file);
        return out;
    }
    return ((void *)0);
}

variable_types:
dir
char *

variable_types:
file
char *

variable_types:
out
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // al
__int64 v5; // r13
int v6; // er12
char v7; // al
char v8; // al
char v9; // al
char v10; // al
char **v11; // rbx
const char **v12; // r13
const char *v13; // rsi
char *v14; // rbp
__int64 v15; // r12
char *v16; // r12
int v17; // [rsp+0h] [rbp-38h] BYREF
int v18; // [rsp+4h] [rbp-34h] BYREF
int v19; // [rsp+8h] [rbp-30h] BYREF
int v20; // [rsp+Ch] [rbp-2Ch] BYREF

SetEndianness();
v17 = -1;
v18 = -1;
v19 = -1;
v20 = -1;
while ( 2 )
{
v3 = my_getopt((unsigned int)argc, argv, "bDe:fhnp:P:t:T:v");
if ( v3 != -1 )
{
switch ( v3 )
{
case 'D':
g_create_new_directory = 1;
continue;
case 'P':
g_library_path = (__int64)strdup(optarg);
continue;
case 'T':
_isoc99_sscanf(optarg, "%d", &v20);
continue;
case 'V':
case 'v':
print_version();
return 0;
case 'b':
v17 = 63;
v18 = 7;
v19 = 1;
v20 = 3;
continue;
case 'e':
_isoc99_sscanf(optarg, "%d", &v19);
continue;
case 'f':
g_shouldIncludePreamble = 0;
continue;
case 'n':
v17 = 33;
v18 = 12;
v19 = 1;
v20 = 0;
continue;
case 'p':
_isoc99_sscanf(optarg, "%d", &v17);
continue;
case 't':
_isoc99_sscanf(optarg, "%d", &v18);
continue;
default:
goto LABEL_48;
}
}
break;
}
v5 = optind;
v6 = argc - optind;
if ( argc == optind )
LABEL_48:
print_usage();
InitConverter();
v7 = v17;
if ( v17 >= 0 )
{
prefs[8] = v17 & 1;
prefs[9] = v7 & 2;
prefs[10] = v7 & 4;
prefs[11] = v7 & 8;
prefs[21] = v7 & 0x20;
prefs[20] = v7 & 0x10;
}
v8 = v18;
if ( v18 >= 0 )
{
prefs[6] = v18 & 1;
prefs[4] = v8 & 2;
prefs[22] = v8 & 8;
prefs[7] = v8 & 4;
}
v9 = v19;
if ( v19 >= 0 )
{
prefs[14] = v19 & 1;
g_eqn_insert_image = v9 & 2;
g_eqn_insert_name = v9 & 8;
g_eqn_keep_file = v9 & 4;
}
v10 = v20;
if ( v20 >= 0 )
{
prefs[23] = v20 & 1;
prefs[25] = v10 & 4;
prefs[24] = v10 & 2;
}
if ( v6 > 0 )
{
v11 = (char **)&argv[v5];
v12 = &(&argv[v5])[(unsigned int)(v6 - 1) + 1];
do
{
RTFInit();
v14 = establish_filename(*v11);
if ( g_input_file_type == 2 )
{
g_create_new_directory = 1;
}
else if ( g_input_file_type == 4 )
{
v13 = *v11;
if ( v14 )
{
RTFMsg("* Skipping non-RTF file '%s'\n", v13);
free(v14);
}
else
{
RTFMsg("* Skipping non-existent file '%s'\n", v13);
}
goto LABEL_29;
}
ifp = fopen(v14, "rb");
if ( !ifp )
{
RTFPanic("* Cannot open input file %s\n", v14);
exit(1);
}
RTFSetInputName(v14);
RTFSetStream(ifp);
if ( (g_input_file_type & 0xFFFFFFFD) == 1 )
goto LABEL_36;
v15 = ftell(ifp);
RTFGetToken();
RTFGetToken();
if ( (unsigned __int16)RTFCheckCMM(3LL, 0LL, 0LL) )
{
fseek(ifp, v15, 0);
LABEL_36:
RTFInit();
v16 = make_output_filename(v14);
ofp = (__int64)fopen(v16, "wb+");
if ( ofp )
{
RTFSetOutputName(v16);
RTFSetOutputStream(ofp);
fprintf(stderr, "Processing %s\n", v14);
if ( (unsigned int)BeginLaTeXFile() )
{
if ( g_input_file_type == 1 )
{
ConvertEquationFile(v14);
}
else if ( g_input_file_type == 3 )
{
ConvertRawEquationFile(v14);
}
else
{
RTFRead();
}
EndLaTeXFile();
}
fclose(ifp);
free(v14);
free(v16);
}
else
{
RTFMsg("Cannot open output file %s\n", v16);
free(v14);
free(v16);
}
goto LABEL_29;
}
RTFMsg("* Yikes! '%s' is not actually a RTF file! Skipping....\n", v14);
fclose(ifp);
free(v14);
LABEL_29:
++v11;
}
while ( v11 != (char **)v12 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // al
__int64 v5; // r13
int v6; // er12
char v7; // al
char v8; // al
char v9; // al
char v10; // al
char **v11; // rbx
const char **v12; // r13
const char *v13; // rsi
char *v14; // rbp
__int64 v15; // r12
char *v16; // r12
int v17; // [rsp+0h] [rbp-38h] BYREF
int v18; // [rsp+4h] [rbp-34h] BYREF
int v19; // [rsp+8h] [rbp-30h] BYREF
int v20; // [rsp+Ch] [rbp-2Ch] BYREF

func0();
v17 = -1;
v18 = -1;
v19 = -1;
v20 = -1;
while ( 2 )
{
v3 = my_getopt((unsigned int)argc, argv, "bDe:fhnp:P:t:T:v");
if ( v3 != -1 )
{
switch ( v3 )
{
case 'D':
g_create_new_directory = 1;
continue;
case 'P':
g_library_path = (__int64)strdup(optarg);
continue;
case 'T':
_isoc99_sscanf(optarg, "%d", &v20);
continue;
case 'V':
case 'v':
func1();
return 0;
case 'b':
v17 = 63;
v18 = 7;
v19 = 1;
v20 = 3;
continue;
case 'e':
_isoc99_sscanf(optarg, "%d", &v19);
continue;
case 'f':
g_shouldIncludePreamble = 0;
continue;
case 'n':
v17 = 33;
v18 = 12;
v19 = 1;
v20 = 0;
continue;
case 'p':
_isoc99_sscanf(optarg, "%d", &v17);
continue;
case 't':
_isoc99_sscanf(optarg, "%d", &v18);
continue;
default:
goto LABEL_48;
}
}
break;
}
v5 = optind;
v6 = argc - optind;
if ( argc == optind )
LABEL_48:
func2();
InitConverter();
v7 = v17;
if ( v17 >= 0 )
{
prefs[8] = v17 & 1;
prefs[9] = v7 & 2;
prefs[10] = v7 & 4;
prefs[11] = v7 & 8;
prefs[21] = v7 & 0x20;
prefs[20] = v7 & 0x10;
}
v8 = v18;
if ( v18 >= 0 )
{
prefs[6] = v18 & 1;
prefs[4] = v8 & 2;
prefs[22] = v8 & 8;
prefs[7] = v8 & 4;
}
v9 = v19;
if ( v19 >= 0 )
{
prefs[14] = v19 & 1;
g_eqn_insert_image = v9 & 2;
g_eqn_insert_name = v9 & 8;
g_eqn_keep_file = v9 & 4;
}
v10 = v20;
if ( v20 >= 0 )
{
prefs[23] = v20 & 1;
prefs[25] = v10 & 4;
prefs[24] = v10 & 2;
}
if ( v6 > 0 )
{
v11 = (char **)&argv[v5];
v12 = &(&argv[v5])[(unsigned int)(v6 - 1) + 1];
do
{
RTFInit();
v14 = func4(*v11);
if ( g_input_file_type == 2 )
{
g_create_new_directory = 1;
}
else if ( g_input_file_type == 4 )
{
v13 = *v11;
if ( v14 )
{
RTFMsg("* Skipping non-RTF file '%s'\n", v13);
free(v14);
}
else
{
RTFMsg("* Skipping non-existent file '%s'\n", v13);
}
goto LABEL_29;
}
ifp = fopen(v14, "rb");
if ( !ifp )
{
RTFPanic("* Cannot open input file %s\n", v14);
exit(1);
}
RTFSetInputName(v14);
RTFSetStream(ifp);
if ( (g_input_file_type & 0xFFFFFFFD) == 1 )
goto LABEL_36;
v15 = ftell(ifp);
RTFGetToken();
RTFGetToken();
if ( (unsigned __int16)RTFCheckCMM(3LL, 0LL, 0LL) )
{
fseek(ifp, v15, 0);
LABEL_36:
RTFInit();
v16 = func6(v14);
ofp = (__int64)fopen(v16, "wb+");
if ( ofp )
{
RTFSetOutputName(v16);
RTFSetOutputStream(ofp);
fprintf(stderr, "Processing %s\n", v14);
if ( (unsigned int)BeginLaTeXFile() )
{
if ( g_input_file_type == 1 )
{
ConvertEquationFile(v14);
}
else if ( g_input_file_type == 3 )
{
ConvertRawEquationFile(v14);
}
else
{
RTFRead();
}
EndLaTeXFile();
}
fclose(ifp);
free(v14);
free(v16);
}
else
{
RTFMsg("Cannot open output file %s\n", v16);
free(v14);
free(v16);
}
goto LABEL_29;
}
RTFMsg("* Yikes! '%s' is not actually a RTF file! Skipping....\n", v14);
fclose(ifp);
free(v14);
LABEL_29:
++v11;
}
while ( v11 != (char **)v12 );
}
return 0;
}

original_code:
int
main(int argc, char **argv)
{
    char c, *input_filename, *output_filename;
    int fileCounter, cli_paragraph, cli_text, cli_equation, cli_table;
    long cursorPos;
    extern char *optarg;
    extern int optind;
    SetEndianness();
    cli_paragraph = -1;
    cli_text = -1;
    cli_equation = -1;
    cli_table = -1;
    while ((c = my_getopt(argc, argv, "bDe:fhnp:P:t:T:v")) != (-1)) {
        switch (c) {
        case 'b':
            cli_paragraph = 63;
            cli_text = 7;
            cli_equation = 1;
            cli_table = 3;
            break;
        case 'D':
            g_create_new_directory = 1;
            break;
        case 'e':
            sscanf(optarg, "%d", &cli_equation);
            break;
        case 'f':
            g_shouldIncludePreamble = 0;
            break;
        case 'n':
            cli_paragraph = 33;
            cli_text = 12;
            cli_equation = 1;
            cli_table = 0;
            break;
        case 'p':
            sscanf(optarg, "%d", &cli_paragraph);
            break;
        case 'P':
            g_library_path = strdup(optarg);
            break;
        case 't':
            sscanf(optarg, "%d", &cli_text);
            break;
        case 'T':
            sscanf(optarg, "%d", &cli_table);
            break;
        case 'v':
        case 'V':
            print_version();
            return (0);
        case 'h':
        case '?':
        default:
            print_usage();
        }
    }
    argc -= optind;
    argv += optind;
    if (!argc) print_usage();
    InitConverter();
    if (cli_paragraph >= 0) {
        prefs[pConvertParagraphStyle] = cli_paragraph & 1;
        prefs[pConvertParagraphIndent] = cli_paragraph & 2;
        prefs[pConvertInterParagraphSpace] = cli_paragraph & 4;
        prefs[pConvertLineSpacing] = cli_paragraph & 8;
        prefs[pConvertParagraphMargin] = cli_paragraph & 16;
        prefs[pConvertParagraphAlignment] = cli_paragraph & 32;
    }
    if (cli_text >= 0) {
        prefs[pConvertTextSize] = cli_text & 1;
        prefs[pConvertTextColor] = cli_text & 2;
        prefs[pConvertTextForm] = cli_text & 4;
        prefs[pConvertTextNoTab] = cli_text & 8;
    }
    if (cli_equation >= 0) {
        prefs[pConvertEquation] = cli_equation & 1;
        g_eqn_insert_image = cli_equation & 2;
        g_eqn_keep_file = cli_equation & 4;
        g_eqn_insert_name = cli_equation & 8;
    }
    if (cli_table >= 0) {
        prefs[pConvertTableWidths] = cli_table & 1;
        prefs[pConvertTableAlignment] = cli_table & 2;
        prefs[pConvertTableBorders] = cli_table & 4;
    }
    for (fileCounter = 0; fileCounter < argc; fileCounter++) {
        RTFInit();
        input_filename = establish_filename(argv[fileCounter]);
     if (g_input_file_type == TYPE_RTFD)
      g_create_new_directory = 1;
        if (g_input_file_type == TYPE_UNKNOWN) {
            if (!input_filename)
                RTFMsg("* Skipping non-existent file '%s'\n", argv[fileCounter]);
            else
                RTFMsg("* Skipping non-RTF file '%s'\n", argv[fileCounter]);
            if (input_filename) free(input_filename);
            continue;
        }
        ifp = fopen(input_filename, "rb");
        if (!ifp) {
            RTFPanic("* Cannot open input file %s\n", input_filename);
            exit(1);
        }
        RTFSetInputName(input_filename);
        RTFSetStream(ifp);
        if (g_input_file_type != TYPE_EQN && g_input_file_type != TYPE_RAWEQN) {
            cursorPos = ftell(ifp);
            RTFGetToken();
            RTFGetToken();
            if (!RTFCheckCMM(3, 0, 0)) {
                RTFMsg("* Yikes! '%s' is not actually a RTF file!  Skipping....\n", input_filename);
                fclose(ifp);
                free(input_filename);
                continue;
            }
            fseek(ifp, cursorPos, 0);
        }
        RTFInit();
        output_filename = make_output_filename(input_filename);
        ofp = fopen(output_filename, "wb+");
        if (!ofp) {
            RTFMsg("Cannot open output file %s\n", output_filename);
            free(input_filename);
            free(output_filename);
            continue;
        }
        RTFSetOutputName(output_filename);
        RTFSetOutputStream(ofp);
        fprintf(stderr, "Processing %s\n", input_filename);
        if (BeginLaTeXFile()) {
            if (g_input_file_type == TYPE_EQN)
                (void) ConvertEquationFile(input_filename);
            else if (g_input_file_type == TYPE_RAWEQN) {
                (void) ConvertRawEquationFile(input_filename);
            } else
                RTFRead();
            EndLaTeXFile();
        }
        fclose(ifp);
        free(input_filename);
        free(output_filename);
    }
    return (0);
}

canonical_original_code:
int main(int argc, char **argv)
{
    char c; char *input_filename; char *output_filename;
    int fileCounter; int cli_paragraph; int cli_text; int cli_equation; int cli_table;
    long cursorPos;
    extern char *optarg;
    extern int optind;
    SetEndianness();
    cli_paragraph = -1;
    cli_text = -1;
    cli_equation = -1;
    cli_table = -1;
    while ((c = my_getopt(argc, argv, "bDe:fhnp:P:t:T:v")) != (-1)) {
        switch (c) {
        case 'b':
            cli_paragraph = 63;
            cli_text = 7;
            cli_equation = 1;
            cli_table = 3;
            break;
        case 'D':
            g_create_new_directory = 1;
            break;
        case 'e':
            sscanf(optarg, "%d", &cli_equation);
            break;
        case 'f':
            g_shouldIncludePreamble = 0;
            break;
        case 'n':
            cli_paragraph = 33;
            cli_text = 12;
            cli_equation = 1;
            cli_table = 0;
            break;
        case 'p':
            sscanf(optarg, "%d", &cli_paragraph);
            break;
        case 'P':
            g_library_path = strdup(optarg);
            break;
        case 't':
            sscanf(optarg, "%d", &cli_text);
            break;
        case 'T':
            sscanf(optarg, "%d", &cli_table);
            break;
        case 'v':
        case 'V':
            print_version();
            return (0);
        case 'h':
        case '?':
        default:
            print_usage();
        }
    }
    argc -= optind;
    argv += optind;
    if (!argc) print_usage();
    InitConverter();
    if (cli_paragraph >= 0) {
        prefs[pConvertParagraphStyle] = cli_paragraph & 1;
        prefs[pConvertParagraphIndent] = cli_paragraph & 2;
        prefs[pConvertInterParagraphSpace] = cli_paragraph & 4;
        prefs[pConvertLineSpacing] = cli_paragraph & 8;
        prefs[pConvertParagraphMargin] = cli_paragraph & 16;
        prefs[pConvertParagraphAlignment] = cli_paragraph & 32;
    }
    if (cli_text >= 0) {
        prefs[pConvertTextSize] = cli_text & 1;
        prefs[pConvertTextColor] = cli_text & 2;
        prefs[pConvertTextForm] = cli_text & 4;
        prefs[pConvertTextNoTab] = cli_text & 8;
    }
    if (cli_equation >= 0) {
        prefs[pConvertEquation] = cli_equation & 1;
        g_eqn_insert_image = cli_equation & 2;
        g_eqn_keep_file = cli_equation & 4;
        g_eqn_insert_name = cli_equation & 8;
    }
    if (cli_table >= 0) {
        prefs[pConvertTableWidths] = cli_table & 1;
        prefs[pConvertTableAlignment] = cli_table & 2;
        prefs[pConvertTableBorders] = cli_table & 4;
    }
    for (fileCounter = 0; fileCounter < argc; fileCounter++) {
        RTFInit();
        input_filename = establish_filename(argv[fileCounter]);
     if (g_input_file_type == TYPE_RTFD)
      g_create_new_directory = 1;
        if (g_input_file_type == TYPE_UNKNOWN) {
            if (!input_filename)
                RTFMsg("* Skipping non-existent file '%s'\n", argv[fileCounter]);
            else
                RTFMsg("* Skipping non-RTF file '%s'\n", argv[fileCounter]);
            if (input_filename) free(input_filename);
            continue;
        }
        ifp = fopen(input_filename, "rb");
        if (!ifp) {
            RTFPanic("* Cannot open input file %s\n", input_filename);
            exit(1);
        }
        RTFSetInputName(input_filename);
        RTFSetStream(ifp);
        if (g_input_file_type != TYPE_EQN && g_input_file_type != TYPE_RAWEQN) {
            cursorPos = ftell(ifp);
            RTFGetToken();
            RTFGetToken();
            if (!RTFCheckCMM(3, 0, 0)) {
                RTFMsg("* Yikes! '%s' is not actually a RTF file!  Skipping....\n", input_filename);
                fclose(ifp);
                free(input_filename);
                continue;
            }
            fseek(ifp, cursorPos, 0);
        }
        RTFInit();
        output_filename = make_output_filename(input_filename);
        ofp = fopen(output_filename, "wb+");
        if (!ofp) {
            RTFMsg("Cannot open output file %s\n", output_filename);
            free(input_filename);
            free(output_filename);
            continue;
        }
        RTFSetOutputName(output_filename);
        RTFSetOutputStream(ofp);
        fprintf(stderr, "Processing %s\n", input_filename);
        if (BeginLaTeXFile()) {
            if (g_input_file_type == TYPE_EQN)
                (void) ConvertEquationFile(input_filename);
            else if (g_input_file_type == TYPE_RAWEQN) {
                (void) ConvertRawEquationFile(input_filename);
            } else
                RTFRead();
            EndLaTeXFile();
        }
        fclose(ifp);
        free(input_filename);
        free(output_filename);
    }
    return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c char

variable_types: fileCounter int

variable_types: cursorPos long

variable_types:
optarg
char *

variable_types: optind int

return_type: int


Search

decompiled_code:
__int64 __fastcall Search(char *a1, char a2)
{
char v2; // al

if ( a1 )
{
while ( 1 )
{
v2 = *a1++;
if ( !v2 )
break;
if ( v2 == a2 )
return 1LL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, char a2)
{
char v2; // al

if ( a1 )
{
while ( 1 )
{
v2 = *a1++;
if ( !v2 )
break;
if ( v2 == a2 )
return 1LL;
}
}
return 0LL;
}

original_code:
static int Search (char *s, char c)
{
char c2;
 if (s != (char *) ((void *)0))
 {
  while ((c2 = *s++) != '\0')
  {
   if (c == c2)
    return (1);
  }
 }
 return (0);
}

canonical_original_code:
static int Search(char *s, char c)
{
char c2;
 if (s != (char *) ((void *)0))
 {
  while ((c2 = *s++) != '\0')
  {
   if (c == c2)
    return (1);
  }
 }
 return (0);
}


TSSetScanner

decompiled_code:
__int64 __fastcall TSSetScanner(__int64 a1)
{
__int64 (__fastcall *v1)(); // rax
void *v2; // rax
void *v3; // rax
void *v4; // rax
void *v5; // rax
__int64 result; // rax

if ( a1 )
{
curScan = *(__int64 (**)(void))a1;
v1 = *(__int64 (__fastcall **)())(a1 + 8);
if ( !v1 )
v1 = CanonScan;
off_3A8 = v1;
v2 = *(void **)(a1 + 16);
if ( !v2 )
v2 = &unk_3F0;
off_3B0 = v2;
v3 = *(void **)(a1 + 24);
if ( !v3 )
v3 = &unk_3ED;
off_3B8 = v3;
v4 = *(void **)(a1 + 32);
if ( !v4 )
v4 = &unk_3EB;
off_3C0 = v4;
v5 = *(void **)(a1 + 40);
if ( !v5 )
v5 = &unk_3E8;
off_3C8 = v5;
result = *(unsigned int *)(a1 + 48);
dword_3D0 = *(_DWORD *)(a1 + 48);
}
else
{
curScan = 0LL;
result = 0LL;
off_3A8 = CanonScan;
off_3B0 = &unk_3F0;
off_3B8 = &unk_3ED;
off_3C0 = &unk_3EB;
off_3C8 = &unk_3E8;
dword_3D0 = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 (__fastcall *v1)(); // rax
void *v2; // rax
void *v3; // rax
void *v4; // rax
void *v5; // rax
__int64 result; // rax

if ( a1 )
{
curScan = *(__int64 (**)(void))a1;
v1 = *(__int64 (__fastcall **)())(a1 + 8);
if ( !v1 )
v1 = CanonScan;
off_3A8 = v1;
v2 = *(void **)(a1 + 16);
if ( !v2 )
v2 = &unk_3F0;
off_3B0 = v2;
v3 = *(void **)(a1 + 24);
if ( !v3 )
v3 = &unk_3ED;
off_3B8 = v3;
v4 = *(void **)(a1 + 32);
if ( !v4 )
v4 = &unk_3EB;
off_3C0 = v4;
v5 = *(void **)(a1 + 40);
if ( !v5 )
v5 = &unk_3E8;
off_3C8 = v5;
result = *(unsigned int *)(a1 + 48);
dword_3D0 = *(_DWORD *)(a1 + 48);
}
else
{
curScan = 0LL;
result = 0LL;
off_3A8 = CanonScan;
off_3B0 = &unk_3F0;
off_3B8 = &unk_3ED;
off_3C0 = &unk_3EB;
off_3C8 = &unk_3E8;
dword_3D0 = 0;
}
return result;
}

original_code:
void TSSetScanner (TSScanner *p)
{
 if (p == (TSScanner *) ((void *)0) || p->scanInit == (VoidProc) ((void *)0))
  curScan.scanInit = defScan.scanInit;
 else
  curScan.scanInit = p->scanInit;
 if (p == (TSScanner *) ((void *)0) || p->scanScan == (CharPtrProc) ((void *)0))
  curScan.scanScan = defScan.scanScan;
 else
  curScan.scanScan = p->scanScan;
 if (p == (TSScanner *) ((void *)0) || p->scanDelim == (char *) ((void *)0))
  curScan.scanDelim = defScan.scanDelim;
 else
  curScan.scanDelim = p->scanDelim;
 if (p == (TSScanner *) ((void *)0) || p->scanQuote == (char *) ((void *)0))
  curScan.scanQuote = defScan.scanQuote;
 else
  curScan.scanQuote = p->scanQuote;
 if (p == (TSScanner *) ((void *)0) || p->scanEscape == (char *) ((void *)0))
  curScan.scanEscape = defScan.scanEscape;
 else
  curScan.scanEscape = p->scanEscape;
 if (p == (TSScanner *) ((void *)0) || p->scanEos == (char *) ((void *)0))
  curScan.scanEos = defScan.scanEos;
 else
  curScan.scanEos = p->scanEos;
 if (p == (TSScanner *) ((void *)0) || p->scanFlags == 0)
  curScan.scanFlags = defScan.scanFlags;
 else
  curScan.scanFlags = p->scanFlags;
}

canonical_original_code:
void TSSetScanner(struct TSScanner *p)
{
 if (p == (struct TSScanner *) ((void *)0) || p->scanInit == (VoidProc) ((void *)0))
  curScan.scanInit = defScan.scanInit;
 else
  curScan.scanInit = p->scanInit;
 if (p == (struct TSScanner *) ((void *)0) || p->scanScan == (CharPtrProc) ((void *)0))
  curScan.scanScan = defScan.scanScan;
 else
  curScan.scanScan = p->scanScan;
 if (p == (struct TSScanner *) ((void *)0) || p->scanDelim == (char *) ((void *)0))
  curScan.scanDelim = defScan.scanDelim;
 else
  curScan.scanDelim = p->scanDelim;
 if (p == (struct TSScanner *) ((void *)0) || p->scanQuote == (char *) ((void *)0))
  curScan.scanQuote = defScan.scanQuote;
 else
  curScan.scanQuote = p->scanQuote;
 if (p == (struct TSScanner *) ((void *)0) || p->scanEscape == (char *) ((void *)0))
  curScan.scanEscape = defScan.scanEscape;
 else
  curScan.scanEscape = p->scanEscape;
 if (p == (struct TSScanner *) ((void *)0) || p->scanEos == (char *) ((void *)0))
  curScan.scanEos = defScan.scanEos;
 else
  curScan.scanEos = p->scanEos;
 if (p == (struct TSScanner *) ((void *)0) || p->scanFlags == 0)
  curScan.scanFlags = defScan.scanFlags;
 else
  curScan.scanFlags = p->scanFlags;
}

variable_types: struct TSScanner { void (*scanInit)(char *p); char *(*scanScan)(); char *scanDelim; char *scanQuote; char *scanEscape; char *scanEos; int scanFlags; } *

return_type: void


TSGetScanner

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

*(_QWORD *)a1 = curScan;
*(_QWORD *)(a1 + 8) = off_3A8;
*(_QWORD *)(a1 + 16) = off_3B0;
*(_QWORD *)(a1 + 24) = off_3B8;
*(_QWORD *)(a1 + 32) = off_3C0;
*(_QWORD *)(a1 + 40) = off_3C8;
result = (unsigned int)dword_3D0;
*(_DWORD *)(a1 + 48) = dword_3D0;
return result;
}

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

*(_QWORD *)a1 = curScan;
*(_QWORD *)(a1 + 8) = off_3A8;
*(_QWORD *)(a1 + 16) = off_3B0;
*(_QWORD *)(a1 + 24) = off_3B8;
*(_QWORD *)(a1 + 32) = off_3C0;
*(_QWORD *)(a1 + 40) = off_3C8;
result = (unsigned int)dword_3D0;
*(_DWORD *)(a1 + 48) = dword_3D0;
return result;
}

original_code:
void TSGetScanner (TSScanner *p)
{
 p->scanInit = curScan.scanInit;
 p->scanScan = curScan.scanScan;
 p->scanDelim = curScan.scanDelim;
 p->scanQuote = curScan.scanQuote;
 p->scanEscape = curScan.scanEscape;
 p->scanEos = curScan.scanEos;
 p->scanFlags = curScan.scanFlags;
}

canonical_original_code:
void TSGetScanner(struct TSScanner *p)
{
 p->scanInit = curScan.scanInit;
 p->scanScan = curScan.scanScan;
 p->scanDelim = curScan.scanDelim;
 p->scanQuote = curScan.scanQuote;
 p->scanEscape = curScan.scanEscape;
 p->scanEos = curScan.scanEos;
 p->scanFlags = curScan.scanFlags;
}

user_defined_types: struct TSScanner { void (*scanInit)(char *p); char *(*scanScan)(); char *scanDelim; char *scanQuote; char *scanEscape; char *scanEos; int scanFlags; }


TSSetScanPos

decompiled_code:
void __fastcall TSSetScanPos(__int64 a1)
{
curPos = a1;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
curPos = a1;
}

original_code:
void TSSetScanPos (char *p)
{
 curPos = p;
}

canonical_original_code:
void TSSetScanPos(char *p)
{
 curPos = p;
}

variable_types:
p
char *


TSGetScanPos

decompiled_code:
__int64 TSGetScanPos()
{
return curPos;
}

canonical_decompiled_code:
__int64 func4()
{
return curPos;
}

original_code:
char * TSGetScanPos (void)
{
 return (curPos);
}

canonical_original_code:
char *TSGetScanPos()
{
 return (curPos);
}


TSIsScanDelim

decompiled_code:
__int64 __fastcall TSIsScanDelim(char a1)
{
return Search((char *)off_3B0, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(char a1)
{
return func0((char *)off_3B0, a1);
}

original_code:
int TSIsScanDelim (char c)
{
 return (Search (curScan.scanDelim, c));
}

canonical_original_code:
int TSIsScanDelim(char c)
{
 return (Search (curScan.scanDelim, c));
}


TSIsScanQuote

decompiled_code:
__int64 __fastcall TSIsScanQuote(char a1)
{
return Search((char *)off_3B8, a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1)
{
return func0((char *)off_3B8, a1);
}

original_code:
int TSIsScanQuote (char c)
{
 return (Search (curScan.scanQuote, c));
}

canonical_original_code:
int TSIsScanQuote(char c)
{
 return (Search (curScan.scanQuote, c));
}


TSIsScanEscape

decompiled_code:
__int64 __fastcall TSIsScanEscape(char a1)
{
return Search((char *)off_3C0, a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
return func0((char *)off_3C0, a1);
}

original_code:
int TSIsScanEscape (char c)
{
 return (Search (curScan.scanEscape, c));
}

canonical_original_code:
int TSIsScanEscape(char c)
{
 return (Search (curScan.scanEscape, c));
}


TSIsScanEos

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

if ( a1 )
result = Search((char *)off_3C8, a1);
else
result = 1LL;
return result;
}

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

if ( a1 )
result = func0((char *)off_3C8, a1);
else
result = 1LL;
return result;
}

original_code:
int TSIsScanEos (char c)
{
 if (c == '\0')
  return (1);
 return (Search (curScan.scanEos, c));
}

canonical_original_code:
int TSIsScanEos(char c)
{
 if (c == '\0')
  return (1);
 return (Search (curScan.scanEos, c));
}


TSTestScanFlags

decompiled_code:
_BOOL8 __fastcall TSTestScanFlags(int a1)
{
return (a1 & dword_3D0) == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(int a1)
{
return (a1 & dword_3D0) == a1;
}

original_code:
int TSTestScanFlags (int flags)
{
 return ((curScan.scanFlags & flags) == flags);
}

canonical_original_code:
int TSTestScanFlags(int flags)
{
 return ((curScan.scanFlags & flags) == flags);
}

variable_types: flags int


CanonScan

decompiled_code:
char *CanonScan()
{
char *v0; // r12
_BOOL4 v1; // eax
char v2; // r8
char v3; // bl
char v4; // dl
int v5; // ecx
char v6; // r8
_BYTE *v7; // r10
__int64 v8; // r11
char *v9; // r9
char v10; // dl
char v11; // r8
_BYTE *v12; // r10
char *v13; // rax
int v14; // er9
char v16; // dl
char v17; // r8
__int64 v18; // r11
__int64 v19; // r9
_BYTE *v20; // r10

if ( emptyToken )
{
emptyToken = 0;
return (char *)&unk_3F3;
}
v0 = (char *)TSGetScanPos();
v1 = TSTestScanFlags(1);
v2 = *v0;
if ( !v1 )
{
while ( (unsigned int)TSIsScanDelim(*v0) )
++v0;
}
v3 = 0;
while ( !(unsigned int)TSIsScanEos(v2) )
{
v9 = (char *)(v8 + 1);
if ( v5 )
{
*v7 = v4;
goto LABEL_5;
}
if ( !(unsigned int)TSIsScanEscape(v6) )
{
if ( v3 )
{
if ( v3 != v10 )
goto LABEL_11;
v3 = 0;
}
else
{
if ( !(unsigned int)TSIsScanQuote(v11) )
{
if ( (unsigned int)TSIsScanDelim(v17) )
{
if ( (unsigned int)TSIsScanEos(*(_BYTE *)(v18 + 1)) )
{
if ( TSTestScanFlags(1) )
emptyToken = 1;
}
*v20 = 0;
TSSetScanPos(v19);
return v0;
}
LABEL_11:
*v12 = v10;
goto LABEL_5;
}
v3 = v16;
}
}
LABEL_5:
v2 = *v9;
}
*v7 = 0;
TSSetScanPos(v8);
if ( !v14 )
v0 = v13;
return v0;
}

canonical_decompiled_code:
char *func10()
{
char *v0; // r12
_BOOL4 v1; // eax
char v2; // r8
char v3; // bl
char v4; // dl
int v5; // ecx
char v6; // r8
_BYTE *v7; // r10
__int64 v8; // r11
char *v9; // r9
char v10; // dl
char v11; // r8
_BYTE *v12; // r10
char *v13; // rax
int v14; // er9
char v16; // dl
char v17; // r8
__int64 v18; // r11
__int64 v19; // r9
_BYTE *v20; // r10

if ( emptyToken )
{
emptyToken = 0;
return (char *)&unk_3F3;
}
v0 = (char *)func4();
v1 = func9(1);
v2 = *v0;
if ( !v1 )
{
while ( (unsigned int)func5(*v0) )
++v0;
}
v3 = 0;
while ( !(unsigned int)func8(v2) )
{
v9 = (char *)(v8 + 1);
if ( v5 )
{
*v7 = v4;
goto LABEL_5;
}
if ( !(unsigned int)func7(v6) )
{
if ( v3 )
{
if ( v3 != v10 )
goto LABEL_11;
v3 = 0;
}
else
{
if ( !(unsigned int)func6(v11) )
{
if ( (unsigned int)func5(v17) )
{
if ( (unsigned int)func8(*(_BYTE *)(v18 + 1)) )
{
if ( func9(1) )
emptyToken = 1;
}
*v20 = 0;
func3(v19);
return v0;
}
LABEL_11:
*v12 = v10;
goto LABEL_5;
}
v3 = v16;
}
}
LABEL_5:
v2 = *v9;
}
*v7 = 0;
func3(v8);
if ( !v14 )
v0 = v13;
return v0;
}

original_code:
static char * CanonScan (void)
{
char *pos, *start, *p, c, quote = 0;
int escape = 0, haveToken = 0;
 if (emptyToken)
 {
  emptyToken = 0;
  return ("");
 }
 pos = TSGetScanPos ();
 if (!TSTestScanFlags (0x01))
 {
  while (TSIsScanDelim (*pos))
   ++pos;
 }
 start = p = pos;
 while (!TSIsScanEos (c = *pos))
 {
  haveToken = 1;
  if (escape)
  {
   *p++ = *pos++;
   escape = 0;
  }
  else if (TSIsScanEscape (c))
  {
   ++pos;
   escape = 1;
  }
  else if (quote)
  {
   if (c == quote)
    quote = 0;
   else
    *p++ = *pos;
   ++pos;
  }
  else if (TSIsScanQuote (c))
  {
   ++pos;
   quote = c;
  }
  else if (TSIsScanDelim (c))
  {
   ++pos;
   if (TSIsScanEos (*pos)
    && TSTestScanFlags (0x01))
    ++emptyToken;
   break;
  }
  else
   *p++ = *pos++;
 }
 *p = '\0';
 TSSetScanPos (pos);
 return (haveToken ? start : (char *) ((void *)0));
}

canonical_original_code:
static char *CanonScan()
{
char *pos; char *start; char *p; char c; char quote = 0;
int escape = 0; int haveToken = 0;
 if (emptyToken)
 {
  emptyToken = 0;
  return ("");
 }
 pos = TSGetScanPos ();
 if (!TSTestScanFlags (0x01))
 {
  while (TSIsScanDelim (*pos))
   ++pos;
 }
 start = p = pos;
 while (!TSIsScanEos (c = *pos))
 {
  haveToken = 1;
  if (escape)
  {
   *p++ = *pos++;
   escape = 0;
  }
  else if (TSIsScanEscape (c))
  {
   ++pos;
   escape = 1;
  }
  else if (quote)
  {
   if (c == quote)
    quote = 0;
   else
    *p++ = *pos;
   ++pos;
  }
  else if (TSIsScanQuote (c))
  {
   ++pos;
   quote = c;
  }
  else if (TSIsScanDelim (c))
  {
   ++pos;
   if (TSIsScanEos (*pos)
    && TSTestScanFlags (0x01))
    ++emptyToken;
   break;
  }
  else
   *p++ = *pos++;
 }
 *p = '\0';
 TSSetScanPos (pos);
 return (haveToken ? start : (char *) ((void *)0));
}

variable_types:
pos
char *

variable_types:
start
char *

variable_types: quote char

variable_types: escape int

variable_types: haveToken int


TSScan

decompiled_code:
// attributes: thunk
__int64 TSScan()
{
return off_3A8();
}

canonical_decompiled_code:
__int64 func11()
{
return off_3A8();
}

original_code:
char * TSScan (void)
{
 return ((*curScan.scanScan) ());
}

canonical_original_code:
char *TSScan()
{
 return ((*curScan.scanScan) ());
}


decompiled_code:
__int64 __fastcall fil_sreadU16(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
return *a1;
}

original_code:
uint16_t fil_sreadU16(uint8_t * in)
{
    uint16_t ret;
    ret = *((uint16_t *) in);
    return ret;
}

canonical_original_code:
unsigned short fil_sreadU16(unsigned char *in)
{
    unsigned short ret;
    ret = *((unsigned short *) in);
    return ret;
}

variable_types:
in
unsigned char *

variable_types:
ret
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall fil_sreadU32(unsigned int *a1)
{
return *a1;
}

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

original_code:
uint32_t fil_sreadU32(uint8_t * in)
{
    uint32_t ret;
    ret = *((uint32_t *) in);
    return ret;
}

canonical_original_code:
unsigned int fil_sreadU32(unsigned char *in)
{
    unsigned int ret;
    ret = *((unsigned int *) in);
    return ret;
}

variable_types:
ret
unsigned int

return_type: unsigned int


hexdump

original_code:
void hexdump(void *ptr, void *zero, uint32_t length, char *msg)
{
    unsigned char *pm, *m, *start;
    char buff[18];
    long offset;
    if (!ptr) {
        fprintf(stderr,"Cannot show memory because pointer is NULL\n");
        return;
    }
    m = (unsigned char *) ptr;
    start = (zero) ? (unsigned char *) zero : m;
    buff[8] = ' ';
    buff[17] = '\0';
    if (msg)
        printf("%s from 0x%p length 0x%08x (%d bytes)\n", msg, m, (unsigned int) length, (int) length);
    for (pm = m; (uint32_t) (pm - m) < length; pm++) {
        offset = (pm - m) % 16;
        if (offset == 0)
            printf("%08lx  ", (unsigned long) ((pm - m) + (m - start)));
        buff[offset + (offset < 8 ? 0 : 1)] = (((*__ctype_b_loc ())[(int) ((*pm))] & (unsigned short int) _ISprint) ? (char) *pm : '.');
        if (!((pm - m + 1) % 16))
            printf("%02x  %s\n", *pm, buff);
        else if (!((pm - m + 1) % 8))
            printf("%02x  ", *pm);
        else
            printf("%02x ", *pm);
    }
    offset = (pm - m) % 16;
    if (offset) {
        int i;
        for (i = 0; i < (16 - offset) * 3 - 1; i++)
            printf(" ");
        if (offset != 8)
            buff[offset] = 0;
        printf("  %s\n", buff);
    }
}

canonical_original_code:
void hexdump(void *ptr, void *zero, unsigned int length, char *msg)
{
    unsigned char *pm; unsigned char *m; unsigned char *start;
    char buff[18];
    long offset;
    if (!ptr) {
        fprintf(stderr,"Cannot show memory because pointer is NULL\n");
        return;
    }
    m = (unsigned char *) ptr;
    start = (zero) ? (unsigned char *) zero : m;
    buff[8] = ' ';
    buff[17] = '\0';
    if (msg)
        printf("%s from 0x%p length 0x%08x (%d bytes)\n", msg, m, (unsigned int) length, (int) length);
    for (pm = m; (unsigned int) (pm - m) < length; pm++) {
        offset = (pm - m) % 16;
        if (offset == 0)
            printf("%08lx  ", (unsigned long) ((pm - m) + (m - start)));
        buff[offset + (offset < 8 ? 0 : 1)] = (((*__ctype_b_loc ())[(int) ((*pm))] & (unsigned short) _ISprint) ? (char) *pm : '.');
        if (!((pm - m + 1) % 16))
            printf("%02x  %s\n", *pm, buff);
        else if (!((pm - m + 1) % 8))
            printf("%02x  ", *pm);
        else
            printf("%02x ", *pm);
    }
    offset = (pm - m) % 16;
    if (offset) {
        int i;
        for (i = 0; i < (16 - offset) * 3 - 1; i++)
            printf(" ");
        if (offset != 8)
            buff[offset] = 0;
        printf("  %s\n", buff);
    }
}

variable_types:
ptr
void *

variable_types:
zero
void *

variable_types:
length
unsigned int

variable_types:
msg
char *

variable_types:
pm
unsigned char *

variable_types:
m
unsigned char *

variable_types:
start
unsigned char *

variable_types:
buff
char [18]

variable_types: offset long

variable_types: i int


decompiled_code:
_BOOL8 __fastcall reorder_pps_tree(__int64 a1, unsigned __int16 a2)
{
__int64 v3; // rax
_BYTE *v4; // rdi
__int64 v5; // rax
_BYTE *v6; // rdi
_BOOL8 result; // rax
unsigned int v8; // edx
__int64 v9; // rcx
unsigned int *v10; // rax

v3 = *(unsigned int *)(a1 + 64);
*(_WORD *)(a1 + 68) = a2;
if ( (_DWORD)v3 != -1 )
{
if ( (unsigned int)v3 > num_of_pps )
return 0LL;
v4 = (_BYTE *)(pps_list + 100 * v3);
if ( !*v4 || !(unsigned int)reorder_pps_tree(v4, (unsigned __int16)(a2 + 1)) )
return 0LL;
}
v5 = *(unsigned int *)(a1 + 60);
if ( (_DWORD)v5 == -1 )
{
last_next_link_visited = a1 + 60;
goto LABEL_11;
}
if ( (unsigned int)v5 > num_of_pps )
return 0LL;
v6 = (_BYTE *)(pps_list + 100 * v5);
if ( !*v6 || !(unsigned int)reorder_pps_tree(v6, a2) )
return 0LL;
LABEL_11:
v8 = *(_DWORD *)(a1 + 92);
result = 1LL;
if ( v8 != -1 )
{
if ( v8 <= num_of_pps )
{
v9 = pps_list;
if ( *(_BYTE *)(pps_list + 100LL * v8) )
{
v10 = (unsigned int *)last_next_link_visited;
*(_DWORD *)last_next_link_visited = v8;
*(_DWORD *)(a1 + 92) = -1;
return (unsigned int)reorder_pps_tree(v9 + 100LL * *v10, a2) != 0;
}
}
return 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, unsigned __int16 a2)
{
__int64 v3; // rax
_BYTE *v4; // rdi
__int64 v5; // rax
_BYTE *v6; // rdi
_BOOL8 result; // rax
unsigned int v8; // edx
__int64 v9; // rcx
unsigned int *v10; // rax

v3 = *(unsigned int *)(a1 + 64);
*(_WORD *)(a1 + 68) = a2;
if ( (_DWORD)v3 != -1 )
{
if ( (unsigned int)v3 > num_of_pps )
return 0LL;
v4 = (_BYTE *)(pps_list + 100 * v3);
if ( !*v4 || !(unsigned int)func0(v4, (unsigned __int16)(a2 + 1)) )
return 0LL;
}
v5 = *(unsigned int *)(a1 + 60);
if ( (_DWORD)v5 == -1 )
{
last_next_link_visited = a1 + 60;
goto LABEL_11;
}
if ( (unsigned int)v5 > num_of_pps )
return 0LL;
v6 = (_BYTE *)(pps_list + 100 * v5);
if ( !*v6 || !(unsigned int)func0(v6, a2) )
return 0LL;
LABEL_11:
v8 = *(_DWORD *)(a1 + 92);
result = 1LL;
if ( v8 != -1 )
{
if ( v8 <= num_of_pps )
{
v9 = pps_list;
if ( *(_BYTE *)(pps_list + 100LL * v8) )
{
v10 = (unsigned int *)last_next_link_visited;
*(_DWORD *)last_next_link_visited = v8;
*(_DWORD *)(a1 + 92) = -1;
return (unsigned int)func0(v9 + 100LL * *v10, a2) != 0;
}
}
return 0LL;
}
return result;
}

original_code:
int reorder_pps_tree(pps_entry * node, uint16_t level)
{
    node->level = level;
    if (node->dir != (uint32_t) 0xffffffff) {
        if (node->dir > num_of_pps || !pps_list[node->dir].name[0])
            return 0;
        if (!reorder_pps_tree(&pps_list[node->dir], level + 1))
            return 0;
    }
    if (node->next != (uint32_t) 0xffffffff) {
        if (node->next > num_of_pps || !pps_list[node->next].name[0])
            return 0;
        else if (!reorder_pps_tree(&pps_list[node->next], level))
            return 0;
    } else
        last_next_link_visited = &node->next;
    if (node->previous != (uint32_t) 0xffffffff) {
        if (node->previous > num_of_pps || !pps_list[node->previous].name[0])
            return 0;
        *last_next_link_visited = node->previous;
        node->previous = (uint32_t) 0xffffffff;
        if (!reorder_pps_tree(&pps_list[*last_next_link_visited], level))
            return 0;
    }
    return 1;
}

canonical_original_code:
int reorder_pps_tree(struct pps_block *node, unsigned short level)
{
    node->level = level;
    if (node->dir != (unsigned int) 0xffffffff) {
        if (node->dir > num_of_pps || !pps_list[node->dir].name[0])
            return 0;
        if (!reorder_pps_tree(&pps_list[node->dir], level + 1))
            return 0;
    }
    if (node->next != (unsigned int) 0xffffffff) {
        if (node->next > num_of_pps || !pps_list[node->next].name[0])
            return 0;
        else if (!reorder_pps_tree(&pps_list[node->next], level))
            return 0;
    } else
        last_next_link_visited = &node->next;
    if (node->previous != (unsigned int) 0xffffffff) {
        if (node->previous > num_of_pps || !pps_list[node->previous].name[0])
            return 0;
        *last_next_link_visited = node->previous;
        node->previous = (unsigned int) 0xffffffff;
        if (!reorder_pps_tree(&pps_list[*last_next_link_visited], level))
            return 0;
    }
    return 1;
}

variable_types: struct pps_block { char name[32]; char filename[-1]; unsigned char type; unsigned int size; unsigned int next; unsigned int dir; unsigned short level; unsigned int seconds1; unsigned int seconds2; unsigned int days1; unsigned int days2; unsigned int start; unsigned int previous; unsigned int ppsnumber; } *

variable_types:
level
unsigned short


ends

decompiled_code:
void ends()
{
if ( Block )
free(Block);
if ( Root )
free(Root);
if ( sbd_list )
free(sbd_list);
if ( root_list )
free(root_list);
}

canonical_decompiled_code:
void func1()
{
if ( Block )
free(Block);
if ( Root )
free(Root);
if ( sbd_list )
free(sbd_list);
if ( root_list )
free(root_list);
}

original_code:
void ends(void)
{
    if (Block) free(Block);
    if (Root) free(Root);
    if (sbd_list) free(sbd_list);
    if (root_list) free(root_list);
}

canonical_original_code:
void ends()
{
    if (Block) free(Block);
    if (Root) free(Root);
    if (sbd_list) free(sbd_list);
    if (root_list) free(root_list);
}


decompiled_code:
__int64 __fastcall my_getopt(int a1, const char **a2, const char *a3)
{
__int64 v3; // rbx
unsigned int v4; // er12
char *v5; // rax
_BYTE *v6; // rbx
int v7; // eax
__int64 v9; // rcx
const char *v10; // rax

v3 = rem_0;
optarg = 0LL;
if ( !rem_0 )
{
if ( optind < a1 )
{
v10 = a2[optind];
if ( *v10 == 45 )
{
v4 = v10[1];
v3 = (__int64)(v10 + 1);
rem_0 = (__int64)(v10 + 1);
if ( (_BYTE)v4 )
{
if ( (_BYTE)v4 != 45 )
goto LABEL_3;
++optind;
}
}
}
return (unsigned int)-1;
}
v4 = *(char *)rem_0;
LABEL_3:
v5 = strchr(a3, v4);
if ( !v5 || v4 == 58 )
{
v9 = v4;
v4 = 63;
fprintf(stderr, "%s: illegal option -- %c\n", *a2, v9);
v6 = (_BYTE *)++rem_0;
}
else
{
if ( v5[1] == 58 )
{
v7 = optind;
if ( *(_BYTE *)(v3 + 1) )
{
optarg = v3 + 1;
}
else
{
v7 = optind + 1;
optind = v7;
if ( a1 <= v7 )
{
fprintf(stderr, "Missing argument after -%c\n", v4);
exit(1);
}
optarg = (__int64)a2[v7];
}
goto LABEL_9;
}
v6 = (_BYTE *)(v3 + 1);
rem_0 = (__int64)v6;
}
if ( !*v6 )
{
v7 = optind;
LABEL_9:
rem_0 = 0LL;
optind = v7 + 1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, const char **a2, const char *a3)
{
__int64 v3; // rbx
unsigned int v4; // er12
char *v5; // rax
_BYTE *v6; // rbx
int v7; // eax
__int64 v9; // rcx
const char *v10; // rax

v3 = rem_0;
optarg = 0LL;
if ( !rem_0 )
{
if ( optind < a1 )
{
v10 = a2[optind];
if ( *v10 == 45 )
{
v4 = v10[1];
v3 = (__int64)(v10 + 1);
rem_0 = (__int64)(v10 + 1);
if ( (_BYTE)v4 )
{
if ( (_BYTE)v4 != 45 )
goto LABEL_3;
++optind;
}
}
}
return (unsigned int)-1;
}
v4 = *(char *)rem_0;
LABEL_3:
v5 = strchr(a3, v4);
if ( !v5 || v4 == 58 )
{
v9 = v4;
v4 = 63;
fprintf(stderr, "%s: illegal option -- %c\n", *a2, v9);
v6 = (_BYTE *)++rem_0;
}
else
{
if ( v5[1] == 58 )
{
v7 = optind;
if ( *(_BYTE *)(v3 + 1) )
{
optarg = v3 + 1;
}
else
{
v7 = optind + 1;
optind = v7;
if ( a1 <= v7 )
{
fprintf(stderr, "Missing argument after -%c\n", v4);
exit(1);
}
optarg = (__int64)a2[v7];
}
goto LABEL_9;
}
v6 = (_BYTE *)(v3 + 1);
rem_0 = (__int64)v6;
}
if ( !*v6 )
{
v7 = optind;
LABEL_9:
rem_0 = 0LL;
optind = v7 + 1;
}
return v4;
}

original_code:
int my_getopt(int argc, char **argv, char *optstring)
{
    char *q;
    static char *rem = ((void *)0);
    int c;
    int needarg = 0;
    optarg = ((void *)0);
    if (!rem) {
        if (optind < argc && argv[optind][0] == '-') {
            rem = argv[optind] + 1;
            if (*rem == 0)
                return (-1);
            if (*rem == '-') {
                optind++;
                return (-1);
            }
        } else
            return (-1);
    }
    c = *rem;
    q = strchr(optstring, c);
    if (q && c != ':') {
        needarg = (q[1] == ':');
        if (needarg) {
            if (rem[1] != 0)
                optarg = rem + 1;
            else {
                optind++;
                if (optind < argc)
                    optarg = argv[optind];
                else {
                    fprintf(stderr, "Missing argument after -%c\n", c);
                    exit(1);
                }
            }
        } else
            rem++;
    } else {
        fprintf(stderr, "%s: illegal option -- %c\n", argv[0], c);
        c = '?';
        rem++;
    }
    if (needarg || *rem == 0) {
        rem = ((void *)0);
        optind++;
    }
    return c;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
optstring
char *

variable_types:
q
char *

variable_types:
rem
char *

variable_types: c int

variable_types: needarg int


processOpts

repo: gmangal/MIPS-Disassembler

original_code:
int
processOpts(int argc, char *argv[], int *rFormat, int *iFormat, int *jFormat)
{
    struct option longopts[] =
    {
        {"usage", 0, ((void *)0), 'u'},
        {"Rformat", 0, ((void *)0), 'r'},
        {"Iformat", 0, ((void *)0), 'i'},
        {"Jformat", 0, ((void *)0), 'j'},
        {"all", 0, ((void *)0), 'a'},
        {((void *)0), 0, ((void *)0), 0 }
    };
    char *progName = argv[0];
    *rFormat = 0;
    *iFormat = 0;
    *jFormat = 0;
    int ch;
    while ((ch = getopt_long(argc, argv, "rija", longopts, ((void *)0))) != -1)
    {
        switch(ch)
        {
            case 'u':
                usage(argv[0], stdout);
            case 'r':
                *rFormat = 1;
                break;
            case 'i':
                *iFormat = 1;
                break;
            case 'j':
                *jFormat = 1;
                break;
            case 'a':
                *rFormat = 1;
                *iFormat = 1;
                *jFormat = 1;
                break;
            case '?':
            default:
                usage(progName, stderr);
                break;
        }
    }
    return optind;
}

variable_types:
argv
char *[]

variable_types:
rFormat
int *

variable_types:
iFormat
int *

variable_types:
jFormat
int *

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types:
progName
char *

variable_types: ch int


rType

decompiled_code:
int __fastcall rType(unsigned int a1, unsigned int a2)
{
int result; // eax
char v3[6]; // [rsp+Eh] [rbp-2Ah] BYREF
char v4[6]; // [rsp+14h] [rbp-24h] BYREF
char v5[30]; // [rsp+1Ah] [rbp-1Eh] BYREF

printf("0x%08X ", a1);
getRegister((a2 >> 21) & 0x1F, v3);
getRegister(HIWORD(a2) & 0x1F, v4);
result = getRegister((unsigned __int16)a2 >> 11, v5);
switch ( a2 & 0x3F )
{
case 0u:
result = printf("%-8s %s, %s, %u\n", "sll", v5, v4, (a2 >> 6) & 0x1F);
break;
case 2u:
result = printf("%-8s %s, %s, %u\n", "srl", v5, v4, (a2 >> 6) & 0x1F);
break;
case 3u:
result = printf("%-8s %s, %s, %u\n", "sra", v5, v4, (a2 >> 6) & 0x1F);
break;
case 8u:
result = printf("%-8s %s\n", "jr", v3);
break;
case 0xCu:
result = printf("%-8s \n", "syscall");
break;
case 0x20u:
result = printf("%-8s %s, %s, %s\n", "add", v5, v3, v4);
break;
case 0x21u:
result = printf("%-8s %s, %s, %s\n", "addu", v5, v3, v4);
break;
case 0x22u:
result = printf("%-8s %s, %s, %s\n", "sub", v5, v3, v4);
break;
case 0x23u:
result = printf("%-8s %s, %s, %s\n", "subu", v5, v3, v4);
break;
case 0x24u:
result = printf("%-8s %s, %s, %s\n", "and", v5, v3, v4);
break;
case 0x25u:
result = printf("%-8s %s, %s, %s\n", "or", v5, v3, v4);
break;
case 0x26u:
result = printf("%-8s %s, %s, %s\n", "xor", v5, v3, v4);
break;
case 0x27u:
result = printf("%-8s %s, %s, %s\n", "nor", v5, v3, v4);
break;
case 0x2Au:
result = printf("%-8s %s, %s, %s\n", "slt", v5, v3, v4);
break;
case 0x2Bu:
result = printf("%-8s %s, %s, %s\n", "sltu", v5, v3, v4);
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1, unsigned int a2)
{
int result; // eax
char v3[6]; // [rsp+Eh] [rbp-2Ah] BYREF
char v4[6]; // [rsp+14h] [rbp-24h] BYREF
char v5[30]; // [rsp+1Ah] [rbp-1Eh] BYREF

printf("0x%08X ", a1);
func12((a2 >> 21) & 0x1F, v3);
func12(HIWORD(a2) & 0x1F, v4);
result = func12((unsigned __int16)a2 >> 11, v5);
switch ( a2 & 0x3F )
{
case 0u:
result = printf("%-8s %s, %s, %u\n", "sll", v5, v4, (a2 >> 6) & 0x1F);
break;
case 2u:
result = printf("%-8s %s, %s, %u\n", "srl", v5, v4, (a2 >> 6) & 0x1F);
break;
case 3u:
result = printf("%-8s %s, %s, %u\n", "sra", v5, v4, (a2 >> 6) & 0x1F);
break;
case 8u:
result = printf("%-8s %s\n", "jr", v3);
break;
case 0xCu:
result = printf("%-8s \n", "syscall");
break;
case 0x20u:
result = printf("%-8s %s, %s, %s\n", "add", v5, v3, v4);
break;
case 0x21u:
result = printf("%-8s %s, %s, %s\n", "addu", v5, v3, v4);
break;
case 0x22u:
result = printf("%-8s %s, %s, %s\n", "sub", v5, v3, v4);
break;
case 0x23u:
result = printf("%-8s %s, %s, %s\n", "subu", v5, v3, v4);
break;
case 0x24u:
result = printf("%-8s %s, %s, %s\n", "and", v5, v3, v4);
break;
case 0x25u:
result = printf("%-8s %s, %s, %s\n", "or", v5, v3, v4);
break;
case 0x26u:
result = printf("%-8s %s, %s, %s\n", "xor", v5, v3, v4);
break;
case 0x27u:
result = printf("%-8s %s, %s, %s\n", "nor", v5, v3, v4);
break;
case 0x2Au:
result = printf("%-8s %s, %s, %s\n", "slt", v5, v3, v4);
break;
case 0x2Bu:
result = printf("%-8s %s, %s, %s\n", "sltu", v5, v3, v4);
break;
default:
return result;
}
return result;
}

original_code:
void
rType(int PC, struct RFormat rInstruct)
{
    printf("0x%08X ", PC);
    char rsReg[6];
    char rtReg[6];
    char rdReg[6];
    getRegister(rInstruct.rs, rsReg);
    getRegister(rInstruct.rt, rtReg);
    getRegister(rInstruct.rd, rdReg);
    switch(rInstruct.funct)
    {
        case 0:
            printf("%-8s %s, %s, %u\n", "sll", rdReg, rtReg, rInstruct.shamt);
            break;
        case 2:
            printf("%-8s %s, %s, %u\n", "srl", rdReg, rtReg, rInstruct.shamt);
            break;
        case 3:
            printf("%-8s %s, %s, %u\n", "sra", rdReg, rtReg, rInstruct.shamt);
            break;
        case 8:
            printf("%-8s %s\n", "jr", rsReg);
            break;
        case 12:
            printf("%-8s \n", "syscall");
            break;
        case 32:
            printf("%-8s %s, %s, %s\n", "add", rdReg, rsReg, rtReg);
            break;
        case 33:
            printf("%-8s %s, %s, %s\n", "addu", rdReg, rsReg, rtReg);
            break;
        case 34:
            printf("%-8s %s, %s, %s\n", "sub", rdReg, rsReg, rtReg);
            break;
        case 35:
            printf("%-8s %s, %s, %s\n", "subu", rdReg, rsReg, rtReg);
            break;
        case 36:
            printf("%-8s %s, %s, %s\n", "and", rdReg, rsReg, rtReg);
            break;
        case 37:
            printf("%-8s %s, %s, %s\n", "or", rdReg, rsReg, rtReg);
            break;
        case 38:
            printf("%-8s %s, %s, %s\n", "xor", rdReg, rsReg, rtReg);
            break;
        case 39:
            printf("%-8s %s, %s, %s\n", "nor", rdReg, rsReg, rtReg);
            break;
        case 42:
            printf("%-8s %s, %s, %s\n", "slt", rdReg, rsReg, rtReg);
            break;
        case 43:
            printf("%-8s %s, %s, %s\n", "sltu", rdReg, rsReg, rtReg);
            break;
        default:
            break;
    }
    return;
}

variable_types: PC int

variable_types: struct RFormat { unsigned int funct; unsigned int shamt; unsigned int rd; unsigned int rt; unsigned int rs; unsigned int opCode; }

variable_types:
rsReg
char [6]

variable_types:
rtReg
char [6]

variable_types:
rdReg
char [6]


iType

decompiled_code:
int __fastcall iType(unsigned int a1, unsigned __int16 *a2)
{
__int64 v2; // r8
int result; // eax
unsigned int v4; // er12
char v5[6]; // [rsp+4h] [rbp-24h] BYREF
char v6[30]; // [rsp+Ah] [rbp-1Eh] BYREF

printf("0x%08X ", a1);
getRegister((a2[1] >> 5) & 0x1F, v5);
getRegister(a2[1] & 0x1F, v6);
v2 = *a2;
result = ((*((_BYTE *)a2 + 3) >> 2) + 60) & 0x3F;
v4 = (__int16)v2;
switch ( (char)result )
{
case 0:
result = printf("%-8s %s, %s, 0x%08X\n", "beq", v5, v6, a1 + 4 * (__int16)v2);
break;
case 1:
result = printf("%-8s %s, %s, 0x%08X\n", "bne", v5, v6, a1 + 4 * (__int16)v2);
break;
case 4:
printf("%-8s %s, %s, ", "addi", v6, v5);
printf("%d", v4);
result = putchar(10);
break;
case 5:
result = printf("%-8s %s, %s, %u\n", "addiu", v6, v5, v2);
break;
case 6:
result = printf("%-8s %s, %s, %d\n", "slti", v6, v5, (unsigned int)(__int16)v2);
break;
case 8:
result = printf("%-8s %s, %s, %u\n", "andi", v6, v5, v2);
break;
case 9:
result = printf("%-8s %s, %s, %u\n", "ori", v6, v5, v2);
break;
case 10:
result = printf("%-8s %s, %s, %u\n", "xori", v6, v5, v2);
break;
case 11:
result = printf("%-8s %s, %u\n", "lui", v6, *a2);
break;
case 28:
result = printf("%-8s %s, %d(%s)\n", "lb", v6, (unsigned int)(__int16)v2, v5);
break;
case 29:
result = printf("%-8s %s, %d(%s)\n", "lh", v6, (unsigned int)(__int16)v2, v5);
break;
case 31:
result = printf("%-8s %s, %d(%s)\n", "lw", v6, (unsigned int)(__int16)v2, v5);
break;
case 36:
result = printf("%-8s %s, %d(%s)\n", "sb", v6, (unsigned int)(__int16)v2, v5);
break;
case 37:
result = printf("%-8s %s, %d(%s)\n", "sh", v6, (unsigned int)(__int16)v2, v5);
break;
case 39:
result = printf("%-8s %s, %d(%s)\n", "sw", v6, (unsigned int)(__int16)v2, v5);
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int a1, unsigned __int16 *a2)
{
__int64 v2; // r8
int result; // eax
unsigned int v4; // er12
char v5[6]; // [rsp+4h] [rbp-24h] BYREF
char v6[30]; // [rsp+Ah] [rbp-1Eh] BYREF

printf("0x%08X ", a1);
func12((a2[1] >> 5) & 0x1F, v5);
func12(a2[1] & 0x1F, v6);
v2 = *a2;
result = ((*((_BYTE *)a2 + 3) >> 2) + 60) & 0x3F;
v4 = (__int16)v2;
switch ( (char)result )
{
case 0:
result = printf("%-8s %s, %s, 0x%08X\n", "beq", v5, v6, a1 + 4 * (__int16)v2);
break;
case 1:
result = printf("%-8s %s, %s, 0x%08X\n", "bne", v5, v6, a1 + 4 * (__int16)v2);
break;
case 4:
printf("%-8s %s, %s, ", "addi", v6, v5);
printf("%d", v4);
result = putchar(10);
break;
case 5:
result = printf("%-8s %s, %s, %u\n", "addiu", v6, v5, v2);
break;
case 6:
result = printf("%-8s %s, %s, %d\n", "slti", v6, v5, (unsigned int)(__int16)v2);
break;
case 8:
result = printf("%-8s %s, %s, %u\n", "andi", v6, v5, v2);
break;
case 9:
result = printf("%-8s %s, %s, %u\n", "ori", v6, v5, v2);
break;
case 10:
result = printf("%-8s %s, %s, %u\n", "xori", v6, v5, v2);
break;
case 11:
result = printf("%-8s %s, %u\n", "lui", v6, *a2);
break;
case 28:
result = printf("%-8s %s, %d(%s)\n", "lb", v6, (unsigned int)(__int16)v2, v5);
break;
case 29:
result = printf("%-8s %s, %d(%s)\n", "lh", v6, (unsigned int)(__int16)v2, v5);
break;
case 31:
result = printf("%-8s %s, %d(%s)\n", "lw", v6, (unsigned int)(__int16)v2, v5);
break;
case 36:
result = printf("%-8s %s, %d(%s)\n", "sb", v6, (unsigned int)(__int16)v2, v5);
break;
case 37:
result = printf("%-8s %s, %d(%s)\n", "sh", v6, (unsigned int)(__int16)v2, v5);
break;
case 39:
result = printf("%-8s %s, %d(%s)\n", "sw", v6, (unsigned int)(__int16)v2, v5);
break;
default:
return result;
}
return result;
}

original_code:
void
iType(int PC, union Instruction *instruct)
{
    printf("0x%08X ", PC);
    char rsReg[6];
    char rtReg[6];
    getRegister(instruct->iInstruct.rs, rsReg);
    getRegister(instruct->iInstruct.rt, rtReg);
    int temp;
    unsigned int imm = instruct->iInstruct.immediate;
    signed int immSigned = instruct->iInstructSigned.immediate;
    switch (instruct->iInstruct.opCode)
    {
        case 4:
            temp = immSigned;
            temp = temp << 2;
            temp = temp + PC;
            printf("%-8s %s, %s, 0x%08X\n", "beq", rsReg, rtReg, temp);
            break;
        case 5:
            temp = immSigned;
            temp = temp << 2;
            temp = temp + PC;
            printf("%-8s %s, %s, 0x%08X\n", "bne", rsReg, rtReg, temp);
            break;
        case 8:
            printf("%-8s %s, %s, ", "addi", rtReg, rsReg);
            printf("%d", immSigned);
            printf("\n");
            break;
        case 9:
            printf("%-8s %s, %s, %u\n", "addiu", rtReg, rsReg, imm);
            break;
        case 10:
            printf("%-8s %s, %s, %d\n", "slti", rtReg, rsReg, immSigned);
            break;
        case 12:
            printf("%-8s %s, %s, %u\n", "andi", rtReg, rsReg, imm);
            break;
        case 13:
            printf("%-8s %s, %s, %u\n", "ori", rtReg, rsReg, imm);
            break;
        case 14:
            printf("%-8s %s, %s, %u\n", "xori", rtReg, rsReg, imm);
            break;
        case 15:
            printf("%-8s %s, %u\n", "lui", rtReg, imm);
            break;
        case 32:
            printf("%-8s %s, %d(%s)\n", "lb", rtReg, immSigned, rsReg);
            break;
        case 33:
            printf("%-8s %s, %d(%s)\n", "lh", rtReg, immSigned, rsReg);
            break;
        case 35:
            printf("%-8s %s, %d(%s)\n", "lw", rtReg, immSigned, rsReg);
            break;
        case 40:
            printf("%-8s %s, %d(%s)\n", "sb", rtReg, immSigned, rsReg);
            break;
        case 41:
            printf("%-8s %s, %d(%s)\n", "sh", rtReg, immSigned, rsReg);
            break;
        case 43:
            printf("%-8s %s, %d(%s)\n", "sw", rtReg, immSigned, rsReg);
            break;
        default:
            break;
    }
    return;
}

canonical_original_code:
void iType(int PC, union Instruction *instruct)
{
    printf("0x%08X ", PC);
    char rsReg[6];
    char rtReg[6];
    getRegister(instruct->iInstruct.rs, rsReg);
    getRegister(instruct->iInstruct.rt, rtReg);
    int temp;
    unsigned int imm = instruct->iInstruct.immediate;
    int immSigned = instruct->iInstructSigned.immediate;
    switch (instruct->iInstruct.opCode)
    {
        case 4:
            temp = immSigned;
            temp = temp << 2;
            temp = temp + PC;
            printf("%-8s %s, %s, 0x%08X\n", "beq", rsReg, rtReg, temp);
            break;
        case 5:
            temp = immSigned;
            temp = temp << 2;
            temp = temp + PC;
            printf("%-8s %s, %s, 0x%08X\n", "bne", rsReg, rtReg, temp);
            break;
        case 8:
            printf("%-8s %s, %s, ", "addi", rtReg, rsReg);
            printf("%d", immSigned);
            printf("\n");
            break;
        case 9:
            printf("%-8s %s, %s, %u\n", "addiu", rtReg, rsReg, imm);
            break;
        case 10:
            printf("%-8s %s, %s, %d\n", "slti", rtReg, rsReg, immSigned);
            break;
        case 12:
            printf("%-8s %s, %s, %u\n", "andi", rtReg, rsReg, imm);
            break;
        case 13:
            printf("%-8s %s, %s, %u\n", "ori", rtReg, rsReg, imm);
            break;
        case 14:
            printf("%-8s %s, %s, %u\n", "xori", rtReg, rsReg, imm);
            break;
        case 15:
            printf("%-8s %s, %u\n", "lui", rtReg, imm);
            break;
        case 32:
            printf("%-8s %s, %d(%s)\n", "lb", rtReg, immSigned, rsReg);
            break;
        case 33:
            printf("%-8s %s, %d(%s)\n", "lh", rtReg, immSigned, rsReg);
            break;
        case 35:
            printf("%-8s %s, %d(%s)\n", "lw", rtReg, immSigned, rsReg);
            break;
        case 40:
            printf("%-8s %s, %d(%s)\n", "sb", rtReg, immSigned, rsReg);
            break;
        case 41:
            printf("%-8s %s, %d(%s)\n", "sh", rtReg, immSigned, rsReg);
            break;
        case 43:
            printf("%-8s %s, %d(%s)\n", "sw", rtReg, immSigned, rsReg);
            break;
        default:
            break;
    }
    return;
}

variable_types: union Instruction { struct RFormat { unsigned int funct; unsigned int shamt; unsigned int rd; unsigned int rt; unsigned int rs; unsigned int opCode; } rInstruct; struct IFormat { unsigned int immediate; unsigned int rt; unsigned int rs; unsigned int opCode; } iInstruct; struct IFormatSigned { int immediate; unsigned int rt; unsigned int rs; unsigned int opCode; } iInstructSigned; struct JFormat { int target; unsigned int opCode; } jInstruct; } *

variable_types: temp int

variable_types:
imm
unsigned int

variable_types: immSigned int

user_defined_types: union Instruction { struct RFormat rInstruct; struct IFormat iInstruct; struct IFormatSigned iInstructSigned; struct JFormat jInstruct; }


jType

decompiled_code:
int __fastcall jType(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebx
unsigned int v3; // ebp
const char *v4; // rsi

v2 = HIBYTE(a2) & 0xFFFFFFFC;
v3 = (4 * ((int)(a2 << 6) >> 6)) | a1 & 0x10000000;
printf("0x%08X ", a1);
v4 = (const char *)&unk_402610;
if ( (_BYTE)v2 != 8 )
v4 = (const char *)&unk_402612;
printf("%-8s ", v4);
return printf("0x%08X\n", v3);
}

canonical_decompiled_code:
int __fastcall func11(unsigned int a1, unsigned int a2)
{
unsigned int v2; // ebx
unsigned int v3; // ebp
const char *v4; // rsi

v2 = HIBYTE(a2) & 0xFFFFFFFC;
v3 = (4 * ((int)(a2 << 6) >> 6)) | a1 & 0x10000000;
printf("0x%08X ", a1);
v4 = (const char *)&unk_402610;
if ( (_BYTE)v2 != 8 )
v4 = (const char *)&unk_402612;
printf("%-8s ", v4);
return printf("0x%08X\n", v3);
}

original_code:
void
jType(int PC, struct JFormat jInstruct)
{
    int fullTarget = jInstruct.target;
    fullTarget = fullTarget << 2;
    int mask = 0x10000000;
    mask = mask & PC;
    fullTarget = fullTarget | mask;
    printf("0x%08X ", PC);
    printf("%-8s ", jInstruct.opCode == 2 ? "j" : "jal");
    printf("0x%08X\n", fullTarget);
    return;
}

variable_types: struct JFormat { int target; unsigned int opCode; }

variable_types: fullTarget int

variable_types: mask int


getRegister

original_code:
void
getRegister(int reg, char regName[6])
{
    switch (reg)
    {
        case 0:
            sprintf(regName, "$zero");
            break;
        case 1:
            sprintf(regName, "$at");
            break;
        case 2:
            sprintf(regName, "$v0");
            break;
        case 3:
            sprintf(regName, "$v1");
            break;
        case 4:
            sprintf(regName, "$a0");
            break;
        case 5:
            sprintf(regName, "$a1");
            break;
        case 6:
            sprintf(regName, "$a2");
            break;
        case 7:
            sprintf(regName, "$a3");
            break;
        case 8:
            sprintf(regName, "$t0");
            break;
        case 9:
            sprintf(regName, "$t1");
            break;
        case 10:
            sprintf(regName, "$t2");
            break;
        case 11:
            sprintf(regName, "$t3");
            break;
        case 12:
            sprintf(regName, "$t4");
            break;
        case 13:
            sprintf(regName, "$t5");
            break;
        case 14:
            sprintf(regName, "$t6");
            break;
        case 15:
            sprintf(regName, "$t7");
            break;
        case 16:
            sprintf(regName, "$s0");
            break;
        case 17:
            sprintf(regName, "$s1");
            break;
        case 18:
            sprintf(regName, "$s2");
            break;
        case 19:
            sprintf(regName, "$s3");
            break;
        case 20:
            sprintf(regName, "$s4");
            break;
        case 21:
            sprintf(regName, "$s5");
            break;
        case 22:
            sprintf(regName, "$s6");
            break;
        case 23:
            sprintf(regName, "$s7");
            break;
        case 24:
            sprintf(regName, "$t8");
            break;
        case 25:
            sprintf(regName, "$t9");
            break;
        case 26:
            sprintf(regName, "$k0");
            break;
        case 27:
            sprintf(regName, "$k1");
            break;
        case 28:
            sprintf(regName, "$gp");
            break;
        case 29:
            sprintf(regName, "$sp");
            break;
        case 30:
            sprintf(regName, "$fp");
            break;
        case 31:
            sprintf(regName, "$ra");
            break;
    }
    return;
}

variable_types: reg int

variable_types:
regName
char [6]


ring

decompiled_code:
void *__fastcall ring(void *a1)
{
abre_sockets(a1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
abre_sockets(a1);
return 0LL;
}

original_code:
void *ring(void *p)
{
 abre_sockets();
 return (void *)0x0;
}

variable_types:
p
void *

return_type: void *


decompiled_code:
int abrir_chat()
{
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

pthread_create(&th, 0LL, chat, 0LL);
pthread_create(newthread, 0LL, ring, 0LL);
pthread_join(th, 0LL);
return pthread_join(newthread[0], 0LL);
}

canonical_decompiled_code:
int func1()
{
pthread_t th; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

pthread_create(&th, 0LL, chat, 0LL);
pthread_create(newthread, 0LL, ring, 0LL);
pthread_join(th, 0LL);
return pthread_join(newthread[0], 0LL);
}

original_code:
void abrir_chat(void)
{
 pthread_t t_chat, t_ring;
 pthread_create(&t_chat, ((void *)0), chat, ((void *)0));
 pthread_create(&t_ring, ((void *)0), ring, ((void *)0));
 pthread_join(t_chat, ((void *)0));
 pthread_join(t_ring, ((void *)0));
}

canonical_original_code:
void abrir_chat()
{
 unsigned long t_chat; unsigned long t_ring;
 pthread_create(&t_chat, ((void *)0), chat, ((void *)0));
 pthread_create(&t_ring, ((void *)0), ring, ((void *)0));
 pthread_join(t_chat, ((void *)0));
 pthread_join(t_ring, ((void *)0));
}


decompiled_code:
int listar_contatos()
{
const char *v0; // rbx
const char *v1; // rsi
int result; // eax

v0 = (const char *)&contatos;
puts("contatos:");
do
{
v1 = v0;
v0 += 15;
result = printf(">> %s\n", v1);
}
while ( v0 != (char *)&contatos + 60 );
return result;
}

canonical_decompiled_code:
int func2()
{
const char *v0; // rbx
const char *v1; // rsi
int result; // eax

v0 = (const char *)&contatos;
puts("contatos:");
do
{
v1 = v0;
v0 += 15;
result = printf(">> %s\n", v1);
}
while ( v0 != (char *)&contatos + 60 );
return result;
}

original_code:
void listar_contatos(void)
{
 int i;
 printf("contatos:\n");
 for (i = 0; i < 4; i++) {
  printf(">> %s\n", contatos[i]);
 }
}

canonical_original_code:
void listar_contatos()
{
 int i;
 printf("contatos:\n");
 for (i = 0; i < 4; i++) {
  printf(">> %s\n", contatos[i]);
 }
}

variable_types: i int


decompiled_code:
__int64 __fastcall contato_id(char *s1)
{
unsigned int v1; // er12
__int64 v2; // rbx

v1 = 99;
if ( strcmp(s1, "todos") )
{
v2 = 0LL;
while ( 1 )
{
v1 = v2;
if ( !strcmp(s1, &contatos[15 * v2]) )
break;
if ( ++v2 == 4 )
return (unsigned int)-1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1)
{
unsigned int v1; // er12
__int64 v2; // rbx

v1 = 99;
if ( strcmp(s1, "todos") )
{
v2 = 0LL;
while ( 1 )
{
v1 = v2;
if ( !strcmp(s1, &contatos[15 * v2]) )
break;
if ( ++v2 == 4 )
return (unsigned int)-1;
}
}
return v1;
}

original_code:
int contato_id(char *nome)
{
 int i;
 if (!strcmp(nome, "todos"))
  return 99;
 for (i = 0; i < 4; i++) {
  if (!strcmp(nome, contatos[i])) {
   return i;
  }
 }
 return -1;
}

variable_types:
nome
char *


chat

decompiled_code:
void __fastcall __noreturn chat(void *a1)
{
char *v1; // rbx
char *v2; // rbp
char *v3; // rax
char **v4; // rax
char *v5; // rdx
char v6; // al
char v7; // al
bool v8; // zf
char v9[32]; // [rsp+0h] [rbp-138h] BYREF
char s; // [rsp+20h] [rbp-118h] BYREF
char s1[279]; // [rsp+21h] [rbp-117h] BYREF

v1 = 0LL;
while ( 1 )
{
while ( 1 )
{
LABEL_2:
if ( !v1 )
goto LABEL_8;
LABEL_3:
memset(v1, 0, 0x118uLL);
fgets(&s, 256, stdin);
if ( s != 47 )
{
*v1 = 99;
v2 = &s;
goto LABEL_5;
}
if ( strncmp(s1, "listar", 6uLL) )
break;
listar_contatos();
}
v5 = v9;
v2 = s1;
do
{
v6 = *v2;
if ( *v2 == 10 )
goto LABEL_2;
++v5;
++v2;
*(v5 - 1) = v6;
}
while ( v6 != 32 );
if ( *v2 == 10 )
goto LABEL_2;
*(v5 - 1) = 0;
v7 = contato_id(v9);
v8 = v7 == ihost;
*v1 = v7;
if ( v8 )
goto LABEL_2;
if ( v7 >= 0 )
{
LABEL_5:
v3 = strchr(v2, 10);
if ( v3 )
*v3 = 0;
strcpy(v1 + 1, v2);
pthread_mutex_lock(&mutex);
v4 = (char **)my_tailq_head[1];
*((_QWORD *)v1 + 33) = 0LL;
*((_QWORD *)v1 + 34) = v4;
*v4 = v1;
my_tailq_head[1] = v1 + 264;
pthread_mutex_unlock(&mutex);
LABEL_8:
v1 = (char *)malloc(0x118uLL);
if ( !v1 )
erro("erro no malloc");
goto LABEL_3;
}
puts("contato inexistente...");
}
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
char *v1; // rbx
char *v2; // rbp
char *v3; // rax
char **v4; // rax
char *v5; // rdx
char v6; // al
char v7; // al
bool v8; // zf
char v9[32]; // [rsp+0h] [rbp-138h] BYREF
char s; // [rsp+20h] [rbp-118h] BYREF
char s1[279]; // [rsp+21h] [rbp-117h] BYREF

v1 = 0LL;
while ( 1 )
{
while ( 1 )
{
LABEL_2:
if ( !v1 )
goto LABEL_8;
LABEL_3:
memset(v1, 0, 0x118uLL);
fgets(&s, 256, stdin);
if ( s != 47 )
{
*v1 = 99;
v2 = &s;
goto LABEL_5;
}
if ( strncmp(s1, "listar", 6uLL) )
break;
func2();
}
v5 = v9;
v2 = s1;
do
{
v6 = *v2;
if ( *v2 == 10 )
goto LABEL_2;
++v5;
++v2;
*(v5 - 1) = v6;
}
while ( v6 != 32 );
if ( *v2 == 10 )
goto LABEL_2;
*(v5 - 1) = 0;
v7 = func3(v9);
v8 = v7 == ihost;
*v1 = v7;
if ( v8 )
goto LABEL_2;
if ( v7 >= 0 )
{
LABEL_5:
v3 = strchr(v2, 10);
if ( v3 )
*v3 = 0;
strcpy(v1 + 1, v2);
pthread_mutex_lock(&mutex);
v4 = (char **)my_tailq_head[1];
*((_QWORD *)v1 + 33) = 0LL;
*((_QWORD *)v1 + 34) = v4;
*v4 = v1;
my_tailq_head[1] = v1 + 264;
pthread_mutex_unlock(&mutex);
LABEL_8:
v1 = (char *)malloc(0x118uLL);
if ( !v1 )
erro("erro no malloc");
goto LABEL_3;
}
puts("contato inexistente...");
}
}

original_code:
void *chat(void *p)
{
 char buffer[256], destino[30], *ptr, *ptr2;
 struct tailq_entry *item;
 item = ((void *)0);
 while (1) {
  item = item ? item : malloc(sizeof(struct tailq_entry));
  if (!item)
   erro("erro no malloc");
  memset(item, 0, sizeof(struct tailq_entry));
  fgets(buffer, 256, stdin);
  ptr = buffer;
  ptr2 = destino;
  if (ptr[0] == '/') {
   ptr++;
   if (!strncmp(ptr, "listar", 6)) {
    listar_contatos();
    continue;
   }
   while (*ptr != '\n' && (*ptr2++ = *ptr++) != ' ') ;
   if (*ptr == '\n')
    continue;
   *--ptr2 = '\0';
   item->destino = (char)contato_id(destino);
   if (item->destino == ihost)
    continue;
   if (item->destino < 0) {
    printf("contato inexistente...\n");
    continue;
   }
  } else {
   item->destino = 99;
  }
  ptr2 = strchr(ptr, '\n');
  if (ptr2)
   *ptr2 = '\0';
  strcpy(item->mensagem, ptr);
  pthread_mutex_lock(&mutex);
  do { (item)->entries.tqe_next = ((void *)0); (item)->entries.tqe_prev = (&my_tailq_head)->tqh_last; *(&my_tailq_head)->tqh_last = (item); (&my_tailq_head)->tqh_last = &(item)->entries.tqe_next; } while ( 0);
  pthread_mutex_unlock(&mutex);
  item = ((void *)0);
 }
 return (void *)0x0;
}

canonical_original_code:
void *chat(void *p)
{
 char buffer[256]; char destino[30]; char *ptr; char *ptr2;
 struct tailq_entry *item;
 item = ((void *)0);
 while (1) {
  item = item ? item : malloc(sizeof(struct tailq_entry));
  if (!item)
   erro("erro no malloc");
  memset(item, 0, sizeof(struct tailq_entry));
  fgets(buffer, 256, stdin);
  ptr = buffer;
  ptr2 = destino;
  if (ptr[0] == '/') {
   ptr++;
   if (!strncmp(ptr, "listar", 6)) {
    listar_contatos();
    continue;
   }
   while (*ptr != '\n' && (*ptr2++ = *ptr++) != ' ') ;
   if (*ptr == '\n')
    continue;
   *--ptr2 = '\0';
   item->destino = (char)contato_id(destino);
   if (item->destino == ihost)
    continue;
   if (item->destino < 0) {
    printf("contato inexistente...\n");
    continue;
   }
  } else {
   item->destino = 99;
  }
  ptr2 = strchr(ptr, '\n');
  if (ptr2)
   *ptr2 = '\0';
  strcpy(item->mensagem, ptr);
  pthread_mutex_lock(&mutex);
  do { (item)->entries.tqe_next = ((void *)0); (item)->entries.tqe_prev = (&my_tailq_head)->tqh_last; *(&my_tailq_head)->tqh_last = (item); (&my_tailq_head)->tqh_last = &(item)->entries.tqe_next; } while ( 0);
  pthread_mutex_unlock(&mutex);
  item = ((void *)0);
 }
 return (void *)0x0;
}

variable_types:
buffer
char [256]

variable_types:
destino
char [30]

variable_types:
ptr
char *

variable_types: struct tailq_entry { char destino; struct { struct tailq_entry *tqe_next; struct tailq_entry **tqe_prev; } entries; } *


repo: gmcabrita/embed-python-c

decompiled_code:
__int64 __fastcall test_return_int(int a1)
{
return (unsigned int)(5 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return (unsigned int)(5 * a1);
}

original_code:
int test_return_int(int n) {
    return n * 5;
}

variable_types: n int


canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
return (unsigned int)(5 * a1);
}

canonical_original_code:
int test_return_int(int n) {
    return n * 5;
}


decompiled_code:
size_t __fastcall write_response(void *src, __int64 a2, __int64 a3, void **a4)
{
char *v5; // rdi
size_t v7; // r12
size_t v8; // rsi
char *v10; // rax

v5 = (char *)a4[1];
v7 = a3 * a2;
v8 = (size_t)&v5[a3 * a2];
if ( v8 <= 0x3FFFE )
{
v10 = (char *)*a4;
}
else
{
if ( v8 > 0x9FFFFF )
return 0LL;
v10 = (char *)realloc(*a4, v8);
*a4 = v10;
if ( !v10 )
return 0LL;
v5 = (char *)a4[1];
}
memcpy(&v5[(_QWORD)v10], src, v7);
a4[1] = (char *)a4[1] + v7;
return v7;
}

canonical_decompiled_code:
size_t __fastcall func0(void *src, __int64 a2, __int64 a3, void **a4)
{
char *v5; // rdi
size_t v7; // r12
size_t v8; // rsi
char *v10; // rax

v5 = (char *)a4[1];
v7 = a3 * a2;
v8 = (size_t)&v5[a3 * a2];
if ( v8 <= 0x3FFFE )
{
v10 = (char *)*a4;
}
else
{
if ( v8 > 0x9FFFFF )
return 0LL;
v10 = (char *)realloc(*a4, v8);
*a4 = v10;
if ( !v10 )
return 0LL;
v5 = (char *)a4[1];
}
memcpy(&v5[(_QWORD)v10], src, v7);
a4[1] = (char *)a4[1] + v7;
return v7;
}

original_code:
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
    struct response *result = (struct response *)stream;
    size_t required_len = result->pos + size * nmemb;
    if(required_len >= (256 * 1024) - 1)
    {
        if (required_len < (10 * 1024 * 1024))
        {
            result->data = realloc(result->data, required_len);
            if (!result->data){
                return 0;
            }
        } else {
            return 0;
        }
    }
    memcpy(result->data + result->pos, ptr, size * nmemb);
    result->pos += size * nmemb;
    return size * nmemb;
}

canonical_original_code:
static unsigned long write_response(void *ptr, unsigned long size, unsigned long nmemb, void *stream)
{
    struct response *result = (struct response *)stream;
    unsigned long required_len = result->pos + size * nmemb;
    if(required_len >= (256 * 1024) - 1)
    {
        if (required_len < (10 * 1024 * 1024))
        {
            result->data = realloc(result->data, required_len);
            if (!result->data){
                return 0;
            }
        } else {
            return 0;
        }
    }
    memcpy(result->data + result->pos, ptr, size * nmemb);
    result->pos += size * nmemb;
    return size * nmemb;
}

variable_types:
ptr
void *

variable_types:
size
unsigned long

variable_types:
nmemb
unsigned long

variable_types:
stream
void *

variable_types: struct response { char *data; unsigned long pos; } *

return_type: unsigned long


decompiled_code:
size_t j_strlen()
{
const char *v0; // rax

v0 = (const char *)json_string_value();
return strlen(v0);
}

canonical_decompiled_code:
size_t func1()
{
const char *v0; // rax

v0 = (const char *)json_string_value();
return strlen(v0);
}

original_code:
extern size_t
j_strlen(json_t *str)
{
    return strlen(json_string_value(str));
}

canonical_original_code:
extern unsigned long j_strlen(struct json_t *str)
{
    return strlen(json_string_value(str));
}

variable_types: struct json_t { enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 } type; unsigned long refcount; } *

user_defined_types: struct json_t { enum json_type type; unsigned long refcount; }


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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 && !_InterlockedSub64((volatile signed __int64 *)(a1 + 8), 1uLL) )
result = json_delete();
}
return result;
}

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

if ( a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result != -1 && !_InterlockedSub64((volatile signed __int64 *)(a1 + 8), 1uLL) )
result = json_delete();
}
return result;
}

original_code:
static inline
void json_decref(json_t *json)
{
    if(json && json->refcount != (size_t)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}

canonical_original_code:
static inline void json_decref(struct json_t *json)
{
    if(json && json->refcount != (unsigned long)-1 && __atomic_sub_fetch(&json->refcount, 1, 3) == 0)
        json_delete(json);
}

user_defined_types: enum json_type { JSON_OBJECT=0, JSON_ARRAY=1, JSON_STRING=2, JSON_INTEGER=3, JSON_REAL=4, JSON_TRUE=5, JSON_FALSE=6, JSON_NULL=7 }


printGraphCB

repo: gmodena/libgraph

decompiled_code:
int __fastcall printGraphCB(__int64 a1)
{
unsigned int *i; // rbx
double v2; // xmm1_8
int result; // eax

for ( i = *(unsigned int **)(a1 + 8); i; i = (unsigned int *)*((_QWORD *)i + 2) )
{
v2 = *(double *)(*((_QWORD *)i + 4) + 32LL) / ((double)(*(_DWORD *)a1 - 1) * (double)(*(_DWORD *)a1 - 2));
result = printf("%d\t\t\t%.16f\t\t\t%.16f\t\t\t%.16f\n", *i, *(double *)(*((_QWORD *)i + 4) + 32LL), v2 * 0.5, v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
unsigned int *i; // rbx
double v2; // xmm1_8
int result; // eax

for ( i = *(unsigned int **)(a1 + 8); i; i = (unsigned int *)*((_QWORD *)i + 2) )
{
v2 = *(double *)(*((_QWORD *)i + 4) + 32LL) / ((double)(*(_DWORD *)a1 - 1) * (double)(*(_DWORD *)a1 - 2));
result = printf("%d\t\t\t%.16f\t\t\t%.16f\t\t\t%.16f\n", *i, *(double *)(*((_QWORD *)i + 4) + 32LL), v2 * 0.5, v2);
}
return result;
}

original_code:
void printGraphCB(t_graph *graph)
{
  t_vertex *v;
  v = graph->vertices;
    while ( v != ((void *)0)) {
       printf("%d\t\t\t%.16f\t\t\t%.16f\t\t\t%.16f\n",
           v->id,
           v->data->rank,
           v->data->rank/((double)(graph->num_vertices-1)*(double)(graph->num_vertices-2))/2.0,
           v->data->rank/((double)(graph->num_vertices-1)*(double)(graph->num_vertices-2))
           );
       v = (v)->next;
    }
}

canonical_original_code:
void printGraphCB(struct tgraph *graph)
{
  struct tvertex *v;
  v = graph->vertices;
    while ( v != ((void *)0)) {
       printf("%d\t\t\t%.16f\t\t\t%.16f\t\t\t%.16f\n",
           v->id,
           v->data->rank,
           v->data->rank/((double)(graph->num_vertices-1)*(double)(graph->num_vertices-2))/2.0,
           v->data->rank/((double)(graph->num_vertices-1)*(double)(graph->num_vertices-2))
           );
       v = (v)->next;
    }
}

variable_types: struct tgraph { int num_vertices; int is_directed; struct tvertex { unsigned int id; struct tedge { int weight; struct tvertex *links; struct tedge *next; } *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents { struct tvertex *vertex; struct tlParents *next; } *parents; double rank; } *data; } *vertices; } *

user_defined_types: struct tgraph { int num_vertices; int is_directed; struct tvertex *vertices; }

user_defined_types: struct tvertex { unsigned int id; struct tedge *edges; struct tvertex *next; int visited; struct cbData *data; }

user_defined_types: struct cbData { double sp_num; double distance; double dependency; struct tlParents *parents; double rank; }


destroyCBData

decompiled_code:
void __fastcall destroyCBData(__int64 a1)
{
__int64 i; // rbp
_QWORD *v2; // rdi
_QWORD *v3; // rbx
void *v4; // rdi

for ( i = *(_QWORD *)(a1 + 8); i; i = *(_QWORD *)(i + 16) )
{
while ( 1 )
{
v2 = *(_QWORD **)(i + 32);
if ( v2 )
{
v3 = (_QWORD *)v2[3];
if ( !v3 )
break;
do
{
v4 = v3;
v3 = (_QWORD *)v3[1];
free(v4);
}
while ( v3 );
v2 = *(_QWORD **)(i + 32);
if ( v2 )
break;
}
i = *(_QWORD *)(i + 16);
if ( !i )
return;
}
free(v2);
}
}

original_code:
void destroyCBData(t_graph *graph)
{
  t_vertex *w;
  t_parents *p, *tmp;
  w = graph->vertices;
  while (w != ((void *)0))
  {
    if (w->data != ((void *)0)) {
      p = w->data->parents;
      while (p != ((void *)0)) {
        p->vertex = ((void *)0);
        tmp = (p)->next;
        (p)->next = ((void *)0);
        free(p);
        p = tmp;
      }
    }
    if ((w)->data != ((void *)0))
      free(w->data);
    w = (w)->next;
  }
}

canonical_original_code:
void destroyCBData(struct tgraph *graph)
{
  struct tvertex *w;
  struct tlParents *p; struct tlParents *tmp;
  w = graph->vertices;
  while (w != ((void *)0))
  {
    if (w->data != ((void *)0)) {
      p = w->data->parents;
      while (p != ((void *)0)) {
        p->vertex = ((void *)0);
        tmp = (p)->next;
        (p)->next = ((void *)0);
        free(p);
        p = tmp;
      }
    }
    if ((w)->data != ((void *)0))
      free(w->data);
    w = (w)->next;
  }
}

variable_types: struct tvertex { unsigned int id; struct tedge { int weight; struct tvertex *links; struct tedge *next; } *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents { struct tvertex *vertex; struct tlParents *next; } *parents; double rank; } *data; } *

variable_types: struct tlParents { struct tvertex { unsigned int id; struct tedge { int weight; struct tvertex *links; struct tedge *next; } *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents *parents; double rank; } *data; } *vertex; struct tlParents *next; } *


help

decompiled_code:
int help()
{
puts("\nUsage: ");
puts("-b : compute betwenness centrality index and print results on screen ");
puts("-f : load a graph from a CSV file ");
puts("-o : oriented graph ");
puts("-d : dump the graph output in DOT format \n");
puts("Example: ");
return printf("bc -o -d -f net.csv\n ");
}

canonical_decompiled_code:
int func0()
{
puts("\nUsage: ");
puts("-b : compute betwenness centrality index and print results on screen ");
puts("-f : load a graph from a CSV file ");
puts("-o : oriented graph ");
puts("-d : dump the graph output in DOT format \n");
puts("Example: ");
return printf("bc -o -d -f net.csv\n ");
}

original_code:
void help(void)
{
  printf("\nUsage: \n");
  printf("-b : compute betwenness centrality index and print results on screen \n");
  printf("-f : load a graph from a CSV file \n");
  printf("-o : oriented graph \n");
  printf("-d : dump the graph output in DOT format \n\n");
  printf("Example: \n");
  printf("bc -o -d -f net.csv\n ");
}

canonical_original_code:
void help()
{
  printf("\nUsage: \n");
  printf("-b : compute betwenness centrality index and print results on screen \n");
  printf("-f : load a graph from a CSV file \n");
  printf("-o : oriented graph \n");
  printf("-d : dump the graph output in DOT format \n\n");
  printf("Example: \n");
  printf("bc -o -d -f net.csv\n ");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // r13
char *v5; // r12
const char **v7; // rsi
int v8; // eax
__int64 v9; // rdx
unsigned int v10; // er12
const char ***v12; // r15
const char **v13; // rbx
__int64 v14; // rbp
int v15; // er12
int i; // ebx
__int64 v17; // rsi
unsigned int v18; // [rsp+8h] [rbp-40h]
int v19; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0LL;
v5 = 0LL;
createGraph(0LL, argv, envp);
v18 = 1;
v19 = 0;
while ( 2 )
{
v7 = argv;
v8 = getopt(argc, (char *const *)argv, "odhbf:v:");
if ( v8 != -1 )
{
switch ( v8 )
{
case '?':
v10 = optopt;
if ( isprint(optopt) )
fprintf(stderr, "Unknown option: `-%c'.\n", v10);
else
fprintf(stderr, "Invalid option `\\x%x'.\n", v10);
help();
return 1;
case 'b':
v19 = 1;
continue;
case 'd':
v3 = 1;
continue;
case 'f':
v4 = optarg;
continue;
case 'h':
help();
continue;
case 'o':
v18 = 0;
continue;
case 'v':
v5 = optarg;
continue;
default:
abort();
}
}
break;
}
if ( argc > optind )
{
v12 = (const char ***)&argv[optind];
v13 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v7 = *v12++;
printf("Non-option argument %s\n", (const char *)v7);
}
while ( v12 != (const char ***)v13 );
}
v14 = createGraph(v18, v7, v9);
if ( v5 )
{
v15 = atoi(v5);
if ( v15 > 0 )
{
for ( i = 1; i <= v15; ++i )
{
v17 = (unsigned int)i;
addVertex(v14, v17);
}
}
}
if ( v4 )
readFromCSV(v14, v4);
if ( v19 == 1 )
{
betweennessCentrality(v14);
printGraphCB(v14);
}
if ( v3 == 1 )
dumpDotGraph(v14);
destroyCBData(v14);
destroyGraph(v14);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
char *v4; // r13
char *v5; // r12
const char **v7; // rsi
int v8; // eax
__int64 v9; // rdx
unsigned int v10; // er12
const char ***v12; // r15
const char **v13; // rbx
__int64 v14; // rbp
int v15; // er12
int i; // ebx
__int64 v17; // rsi
unsigned int v18; // [rsp+8h] [rbp-40h]
int v19; // [rsp+Ch] [rbp-3Ch]

v3 = 0;
v4 = 0LL;
v5 = 0LL;
createGraph(0LL, argv, envp);
v18 = 1;
v19 = 0;
while ( 2 )
{
v7 = argv;
v8 = getopt(argc, (char *const *)argv, "odhbf:v:");
if ( v8 != -1 )
{
switch ( v8 )
{
case '?':
v10 = optopt;
if ( isprint(optopt) )
fprintf(stderr, "Unknown option: `-%c'.\n", v10);
else
fprintf(stderr, "Invalid option `\\x%x'.\n", v10);
func0();
return 1;
case 'b':
v19 = 1;
continue;
case 'd':
v3 = 1;
continue;
case 'f':
v4 = optarg;
continue;
case 'h':
func0();
continue;
case 'o':
v18 = 0;
continue;
case 'v':
v5 = optarg;
continue;
default:
abort();
}
}
break;
}
if ( argc > optind )
{
v12 = (const char ***)&argv[optind];
v13 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
do
{
v7 = *v12++;
printf("Non-option argument %s\n", (const char *)v7);
}
while ( v12 != (const char ***)v13 );
}
v14 = createGraph(v18, v7, v9);
if ( v5 )
{
v15 = atoi(v5);
if ( v15 > 0 )
{
for ( i = 1; i <= v15; ++i )
{
v17 = (unsigned int)i;
addVertex(v14, v17);
}
}
}
if ( v4 )
readFromCSV(v14, v4);
if ( v19 == 1 )
{
betweennessCentrality(v14);
printGraphCB(v14);
}
if ( v3 == 1 )
dumpDotGraph(v14);
destroyCBData(v14);
destroyGraph(v14);
return 0;
}

original_code:
int main(int argc, char **argv)
{
  t_vertex *v1, *v2, *v3, *v4, *v5, *v6, *v7, *v8, *v9, *v10;
  t_vertex *w;
  t_edge *e;
  t_graph *graph;
  t_stack stack;
  t_queue queue;
  int V = 0;
  int i = 0;
  int dot = 0;
  int bc = 0;
  int is_directed = 1;
  int index;
  int f, v, c, o;
  char *file = ((void *)0);
  char *num_vertices = ((void *)0);
  graph = createGraph(0);
  while ((f = getopt (argc, argv, "odhbf:v:")) != -1)
    switch (f)
      {
      case 'd':
 dot = 1;
 break;
      case 'b':
 bc = 1;
 break;
      case 'o':
        is_directed = 0;
        break;
      case 'f':
 file = optarg;
 break;
      case 'h':
        help();
        break;
      case 'v':
        num_vertices = optarg;
        break;
      case '?':
 if (isprint (optopt)) {
   fprintf (stderr, "Unknown option: `-%c'.\n", optopt);
          help();
        }
 else {
   fprintf (stderr,
     "Invalid option `\\x%x'.\n",
     optopt);
          help();
        }
 return 1;
      default:
  abort();
      }
  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  graph = createGraph(is_directed);
  if (num_vertices != ((void *)0)){
    V = atoi(num_vertices) + 1;
    for (i = 1; i < V; i++)
      addVertex(graph, i);
  }
  if (file != ((void *)0))
    readFromCSV(graph, file);
  if (bc == 1) {
    betweennessCentrality(graph);
    printGraphCB(graph);
  }
  if (dot == 1)
    dumpDotGraph(graph);
  destroyCBData(graph);
  destroyGraph(graph);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct tvertex *v1; struct tvertex *v2; struct tvertex *v3; struct tvertex *v4; struct tvertex *v5; struct tvertex *v6; struct tvertex *v7; struct tvertex *v8; struct tvertex *v9; struct tvertex *v10;
  struct tvertex *w;
  struct tedge *e;
  struct tgraph *graph;
  struct tstack stack;
  struct tqueue queue;
  int V = 0;
  int i = 0;
  int dot = 0;
  int bc = 0;
  int is_directed = 1;
  int index;
  int f; int v; int c; int o;
  char *file = ((void *)0);
  char *num_vertices = ((void *)0);
  graph = createGraph(0);
  while ((f = getopt (argc, argv, "odhbf:v:")) != -1)
    switch (f)
      {
      case 'd':
 dot = 1;
 break;
      case 'b':
 bc = 1;
 break;
      case 'o':
        is_directed = 0;
        break;
      case 'f':
 file = optarg;
 break;
      case 'h':
        help();
        break;
      case 'v':
        num_vertices = optarg;
        break;
      case '?':
 if (isprint (optopt)) {
   fprintf (stderr, "Unknown option: `-%c'.\n", optopt);
          help();
        }
 else {
   fprintf (stderr,
     "Invalid option `\\x%x'.\n",
     optopt);
          help();
        }
 return 1;
      default:
  abort();
      }
  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  graph = createGraph(is_directed);
  if (num_vertices != ((void *)0)){
    V = atoi(num_vertices) + 1;
    for (i = 1; i < V; i++)
      addVertex(graph, i);
  }
  if (file != ((void *)0))
    readFromCSV(graph, file);
  if (bc == 1) {
    betweennessCentrality(graph);
    printGraphCB(graph);
  }
  if (dot == 1)
    dumpDotGraph(graph);
  destroyCBData(graph);
  destroyGraph(graph);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct tedge { int weight; struct tvertex { unsigned int id; struct tedge *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents { struct tvertex *vertex; struct tlParents *next; } *parents; double rank; } *data; } *links; struct tedge *next; } *

variable_types: struct tstack { struct titem { struct tvertex { unsigned int id; struct tedge { int weight; struct tvertex *links; struct tedge *next; } *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents { struct tvertex *vertex; struct tlParents *next; } *parents; double rank; } *data; } *item; struct titem *next; } *top; }

variable_types: struct tqueue { struct tqitem { struct tvertex { unsigned int id; struct tedge { int weight; struct tvertex *links; struct tedge *next; } *edges; struct tvertex *next; int visited; struct cbData { double sp_num; double distance; double dependency; struct tlParents { struct tvertex *vertex; struct tlParents *next; } *parents; double rank; } *data; } *item; struct tqitem *next; } *top; }

variable_types: V int

variable_types: dot int

variable_types: bc int

variable_types: index int

variable_types: f int

variable_types: v int

variable_types: c int

variable_types: o int

variable_types:
file
char *

user_defined_types: struct titem { struct tvertex *item; struct titem *next; }

user_defined_types: struct tqitem { struct tvertex *item; struct tqitem *next; }


original_code:
int main()
{
  char str[]="唱的曲子是北宋大$natureOrder(0123)“蝶恋花”词，写的正\n怪客手掌一碰，只把她牙齿撞得隐隐生痛。$natureOrderDESC(8)开";
  reg_rtn_struct reg_rtn_struct_var;
  char *newstr=cns_str_ereplace(str,"\\$.{4,6}Order(DESC)?\\([0-9]{1,4}\\)","hello");
  puts(newstr);
  return 0;
}

canonical_original_code:
int main()
{
  char str[] = "唱的曲子是北宋大$natureOrder(0123)“蝶恋花”词，写的正\n怪客手掌一碰，只把她牙齿撞得隐隐生痛。$natureOrderDESC(8)开";
  struct _reg_rtn_struct reg_rtn_struct_var;
  char *newstr = cns_str_ereplace(str,"\\$.{4,6}Order(DESC)?\\([0-9]{1,4}\\)","hello");
  puts(newstr);
  return 0;
}

variable_types:
str
char []

variable_types: struct _reg_rtn_struct { int rtn; int pstart; int pend; }

variable_types:
newstr
char *