decompiled_code:
char *__fastcall checksum16(const char *a1)
{
unsigned int v1; // edx
size_t v2; // rax
const char *v3; // rcx

v1 = 0;
if ( a1 )
{
v2 = strlen(a1);
if ( v2 )
{
v3 = a1;
v1 = 0;
do
{
v1 = (unsigned __int16)(((unsigned int)((int)(v1 + *v3) >> 31) >> 16) + v1 + *v3)
- ((unsigned int)((int)(v1 + *v3) >> 31) >> 16);
++v3;
}
while ( v3 != &a1[v2] );
}
else
{
v1 = 0;
}
}
return decToHex(v1, 4);
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1)
{
unsigned int v1; // edx
size_t v2; // rax
const char *v3; // rcx

v1 = 0;
if ( a1 )
{
v2 = strlen(a1);
if ( v2 )
{
v3 = a1;
v1 = 0;
do
{
v1 = (unsigned __int16)(((unsigned int)((int)(v1 + *v3) >> 31) >> 16) + v1 + *v3)
- ((unsigned int)((int)(v1 + *v3) >> 31) >> 16);
++v3;
}
while ( v3 != &a1[v2] );
}
else
{
v1 = 0;
}
}
return func0(v1, 4);
}

original_code:
char *checksum16(char *input)
{
    char *ret;
    int i;
    size_t ln;
    int sum;
    sum = 0;
    if(input != ((void *)0)) {
        ln = strlen(input);
        for(i=0; i<ln; i++) {
            sum += input[i];
            sum %= (int) pow(2, 16);
        }
    }
    ret = decToHex(sum, 4);
    return ret;
}

canonical_original_code:
char *checksum16(char *input)
{
    char *ret;
    int i;
    unsigned long ln;
    int sum;
    sum = 0;
    if(input != ((void *)0)) {
        ln = strlen(input);
        for(i=0; i<ln; i++) {
            sum += input[i];
            sum %= (int) pow(2, 16);
        }
    }
    ret = decToHex(sum, 4);
    return ret;
}

variable_types:
input
char *

variable_types:
ln
unsigned long

variable_types: sum int


strPrepare

repo: sardylan/smlgr

decompiled_code:
char *__fastcall strPrepare(char *s)
{
char *v1; // rbp
size_t v2; // rax
int v3; // er12
char *v4; // r13
char *v5; // r12
char *v6; // rbx
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = s;
if ( s )
{
uiMessage(4LL, "Query to prepare: %s", s);
v2 = strlen(s);
v3 = v2;
v1 = 0LL;
if ( v2 > 2 )
{
v1 = (char *)calloc(v2 + 20, 1uLL);
v4 = decToHex(*(_DWORD *)(conf + 20LL), 2);
v5 = decToHex(v3 + 19, 2);
sprintf(v8, "FB;%s;%s|64:%s|", v4, v5, s);
v6 = checksum16(v8);
sprintf(v1, "{%s%s}", v8, v6);
free(v4);
free(v5);
free(v6);
}
}
uiMessage(4LL, "Query prepared: %s", v1);
return v1;
}

canonical_decompiled_code:
char *__fastcall func2(char *s)
{
char *v1; // rbp
size_t v2; // rax
int v3; // er12
char *v4; // r13
char *v5; // r12
char *v6; // rbx
char v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v1 = s;
if ( s )
{
uiMessage(4LL, "Query to prepare: %s", s);
v2 = strlen(s);
v3 = v2;
v1 = 0LL;
if ( v2 > 2 )
{
v1 = (char *)calloc(v2 + 20, 1uLL);
v4 = func0(*(_DWORD *)(conf + 20LL), 2);
v5 = func0(v3 + 19, 2);
sprintf(v8, "FB;%s;%s|64:%s|", v4, v5, s);
v6 = func1(v8);
sprintf(v1, "{%s%s}", v8, v6);
free(v4);
free(v5);
free(v6);
}
}
uiMessage(4LL, "Query prepared: %s", v1);
return v1;
}

original_code:
char *strPrepare(char *query)
{
    char *ret;
    size_t ln;
    char *inv_id;
    char *len;
    char *checksum;
    char tmp[1024];
    ret = ((void *)0);
    if(query != ((void *)0)) {
        uiMessage(4, "Query to prepare: %s", query);
        ln = strlen(query);
        if(ln >= 3) {
            ret = (char *) calloc(13 + ln + 6 + 1, sizeof(char));
            inv_id = decToHex(conf->inv_num, 2);
            len = decToHex(13 + ln + 6, 2);
            sprintf(tmp, "FB;%s;%s|64:%s|", inv_id, len, query);
            checksum = checksum16(tmp);
            sprintf(ret, "{%s%s}", tmp, checksum);
            free(inv_id);
            free(len);
            free(checksum);
        }
    }
    uiMessage(4, "Query prepared: %s", ret);
    return ret;
}

canonical_original_code:
char *strPrepare(char *query)
{
    char *ret;
    unsigned long ln;
    char *inv_id;
    char *len;
    char *checksum;
    char tmp[1024];
    ret = ((void *)0);
    if(query != ((void *)0)) {
        uiMessage(4, "Query to prepare: %s", query);
        ln = strlen(query);
        if(ln >= 3) {
            ret = (char *) calloc(13 + ln + 6 + 1, sizeof(char));
            inv_id = decToHex(conf->inv_num, 2);
            len = decToHex(13 + ln + 6, 2);
            sprintf(tmp, "FB;%s;%s|64:%s|", inv_id, len, query);
            checksum = checksum16(tmp);
            sprintf(ret, "{%s%s}", tmp, checksum);
            free(inv_id);
            free(len);
            free(checksum);
        }
    }
    uiMessage(4, "Query prepared: %s", ret);
    return ret;
}

variable_types:
query
char *

variable_types:
ret
char *

variable_types:
len
char *

variable_types:
checksum
char *

variable_types:
tmp
char [1024]

return_type: char *


infosPrint

decompiled_code:
int __fastcall infosPrint(__int64 a1)
{
__int64 v1; // rbx
const char *i; // rsi
int result; // eax

v1 = *(_QWORD *)(a1 + 16);
for ( i = *(const char **)v1; **(_BYTE **)v1; i = *(const char **)v1 )
{
result = printf("%s = %s\n", i, *(const char **)(v1 + 8));
v1 = *(_QWORD *)(v1 + 16);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
__int64 v1; // rbx
const char *i; // rsi
int result; // eax

v1 = *(_QWORD *)(a1 + 16);
for ( i = *(const char **)v1; **(_BYTE **)v1; i = *(const char **)v1 )
{
result = printf("%s = %s\n", i, *(const char **)(v1 + 8));
v1 = *(_QWORD *)(v1 + 16);
}
return result;
}

original_code:
void infosPrint(infos *input)
{
    input = input->next;
    while(strlen(input->param) > 0) {
        printf("%s = %s\n", input->param, input->value);
        input = input->next;
    }
}

canonical_original_code:
void infosPrint(struct infos_s *input)
{
    input = input->next;
    while(strlen(input->param) > 0) {
        printf("%s = %s\n", input->param, input->value);
        input = input->next;
    }
}

variable_types: struct infos_s { char *param; char *value; struct infos_s *next; } *

return_type: void


infosCounting

decompiled_code:
__int64 __fastcall infosCounting(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // edx

v1 = *(_QWORD *)(a1 + 16);
if ( !**(_BYTE **)v1 )
return 0;
v2 = 0;
do
{
++v2;
v1 = *(_QWORD *)(v1 + 16);
}
while ( **(_BYTE **)v1 );
return v2;
}

original_code:
int infosCounting(infos *input)
{
    int ret;
    input = input->next;
    ret = 0;
    while(strlen(input->param) > 0) {
        ret++;
        input = input->next;
    }
    return ret;
}

canonical_original_code:
int infosCounting(struct infos_s *input)
{
    int ret;
    input = input->next;
    ret = 0;
    while(strlen(input->param) > 0) {
        ret++;
        input = input->next;
    }
    return ret;
}

variable_types: ret int

return_type: int

user_defined_types: struct infos_s { char *param; char *value; struct infos_s *next; }


infosFree

decompiled_code:
void __fastcall infosFree(void *ptr)
{
void **v1; // rbx
void **v3; // rdi

v1 = (void **)*((_QWORD *)ptr + 2);
if ( ptr != v1 )
{
v1 = (void **)ptr;
do
{
free(*v1);
free(v1[1]);
v3 = v1;
v1 = (void **)v1[2];
free(v3);
}
while ( v1[2] != ptr );
}
free(*v1);
free(v1[1]);
free(v1);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
void **v1; // rbx
void **v3; // rdi

v1 = (void **)*((_QWORD *)ptr + 2);
if ( ptr != v1 )
{
v1 = (void **)ptr;
do
{
free(*v1);
free(v1[1]);
v3 = v1;
v1 = (void **)v1[2];
free(v3);
}
while ( v1[2] != ptr );
}
free(*v1);
free(v1[1]);
free(v1);
}

original_code:
void infosFree(infos *input)
{
    infos *prev;
    infos *first;
    first = input;
    while(input->next != first) {
        free(input->param);
        free(input->value);
        prev = input;
        input = input->next;
        free(prev);
    }
    free(input->param);
    free(input->value);
    free(input);
}

canonical_original_code:
void infosFree(struct infos_s *input)
{
    struct infos_s *prev;
    struct infos_s *first;
    first = input;
    while(input->next != first) {
        free(input->param);
        free(input->value);
        prev = input;
        input = input->next;
        free(prev);
    }
    free(input->param);
    free(input->value);
    free(input);
}


cfgInit

decompiled_code:
_QWORD *cfgInit()
{
_DWORD *v0; // rbx
char *v1; // rax
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
_QWORD *result; // rax

v0 = malloc(0x58uLL);
conf = v0;
*v0 = 2;
v1 = (char *)calloc(1uLL, 0xCuLL);
*((_QWORD *)v0 + 1) = v1;
strcpy(v1, "172.16.83.2");
v0[4] = 12345;
v0[5] = 1;
v2 = (char *)calloc(1uLL, 0x28uLL);
*((_QWORD *)v0 + 3) = v2;
strcpy(v2, "UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD");
v0[8] = 10;
v3 = (char *)calloc(1uLL, 0xAuLL);
*((_QWORD *)v0 + 5) = v3;
strcpy(v3, "127.0.0.1");
v0[12] = 3306;
v4 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 7) = v4;
strcpy(v4, "smlgr");
v5 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 8) = v5;
strcpy(v5, "smlgr");
v6 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 9) = v6;
strcpy(v6, "smlgr");
result = calloc(1uLL, 8uLL);
*((_QWORD *)v0 + 10) = result;
*result = 0x61746164766E69LL;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_DWORD *v0; // rbx
char *v1; // rax
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
_QWORD *result; // rax

v0 = malloc(0x58uLL);
conf = v0;
*v0 = 2;
v1 = (char *)calloc(1uLL, 0xCuLL);
*((_QWORD *)v0 + 1) = v1;
strcpy(v1, "172.16.83.2");
v0[4] = 12345;
v0[5] = 1;
v2 = (char *)calloc(1uLL, 0x28uLL);
*((_QWORD *)v0 + 3) = v2;
strcpy(v2, "UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD");
v0[8] = 10;
v3 = (char *)calloc(1uLL, 0xAuLL);
*((_QWORD *)v0 + 5) = v3;
strcpy(v3, "127.0.0.1");
v0[12] = 3306;
v4 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 7) = v4;
strcpy(v4, "smlgr");
v5 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 8) = v5;
strcpy(v5, "smlgr");
v6 = (char *)calloc(1uLL, 6uLL);
*((_QWORD *)v0 + 9) = v6;
strcpy(v6, "smlgr");
result = calloc(1uLL, 8uLL);
*((_QWORD *)v0 + 10) = result;
*result = 0x61746164766E69LL;
return result;
}

original_code:
void cfgInit()
{
    int ln;
    conf = (cfg *) malloc(sizeof(cfg));
    conf->debug_level = 2;
    ln = strlen("172.16.83.2") + 1;
    conf->inv_addr = (char *) calloc(sizeof(char), ln);
    strcpy(conf->inv_addr, "172.16.83.2");
    conf->inv_port = 12345;
    conf->inv_num = 1;
    ln = strlen("UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD") + 1;
    conf->lgr_query = (char *) calloc(sizeof(char), ln);
    strcpy(conf->lgr_query, "UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD");
    conf->lgr_interval = 10;
    ln = strlen("127.0.0.1") + 1;
    conf->mysql_addr = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_addr, "127.0.0.1");
    conf->mysql_port = 3306;
    ln = strlen("smlgr") + 1;
    conf->mysql_user = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_user, "smlgr");
    ln = strlen("smlgr") + 1;
    conf->mysql_password = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_password, "smlgr");
    ln = strlen("smlgr") + 1;
    conf->mysql_database = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_database, "smlgr");
    ln = strlen("invdata") + 1;
    conf->mysql_table = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_table, "invdata");
}

canonical_original_code:
void cfgInit()
{
    int ln;
    conf = (struct cfg_s *) malloc(sizeof(cfg));
    conf->debug_level = 2;
    ln = strlen("172.16.83.2") + 1;
    conf->inv_addr = (char *) calloc(sizeof(char), ln);
    strcpy(conf->inv_addr, "172.16.83.2");
    conf->inv_port = 12345;
    conf->inv_num = 1;
    ln = strlen("UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD") + 1;
    conf->lgr_query = (char *) calloc(sizeof(char), ln);
    strcpy(conf->lgr_query, "UDC;IDC;UL1;IL1;PAC;PRL;TKK;TNF;KDY;KLD");
    conf->lgr_interval = 10;
    ln = strlen("127.0.0.1") + 1;
    conf->mysql_addr = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_addr, "127.0.0.1");
    conf->mysql_port = 3306;
    ln = strlen("smlgr") + 1;
    conf->mysql_user = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_user, "smlgr");
    ln = strlen("smlgr") + 1;
    conf->mysql_password = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_password, "smlgr");
    ln = strlen("smlgr") + 1;
    conf->mysql_database = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_database, "smlgr");
    ln = strlen("invdata") + 1;
    conf->mysql_table = (char *) calloc(sizeof(char), ln);
    strcpy(conf->mysql_table, "invdata");
}

variable_types: ln int

user_defined_types: struct cfg_s { int debug_level; char *inv_addr; int inv_port; int inv_num; char *lgr_query; int lgr_interval; char *mysql_addr; int mysql_port; char *mysql_user; char *mysql_password; char *mysql_database; char *mysql_table; }


cfgPrint

decompiled_code:
__int64 cfgPrint()
{
uiMessage(3LL, "debug_level = %d", *(unsigned int *)conf);
uiMessage(3LL, "inv_addr = %s", *((const char **)conf + 1));
uiMessage(3LL, "inv_port = %d", *((unsigned int *)conf + 4));
uiMessage(3LL, "inv_num = %d", *((unsigned int *)conf + 5));
uiMessage(3LL, "lgr_query = %s", *((const char **)conf + 3));
uiMessage(3LL, "lgr_interval = %d", *((unsigned int *)conf + 8));
uiMessage(3LL, "mysql_addr = %s", *((const char **)conf + 5));
uiMessage(3LL, "mysql_port = %d", *((unsigned int *)conf + 12));
uiMessage(3LL, "mysql_user = %s", *((const char **)conf + 7));
uiMessage(3LL, "mysql_password = %s", *((const char **)conf + 8));
uiMessage(3LL, "mysql_database = %s", *((const char **)conf + 9));
return uiMessage(3LL, "mysql_table = %s", *((const char **)conf + 10));
}

canonical_decompiled_code:
__int64 func1()
{
uiMessage(3LL, "debug_level = %d", *(unsigned int *)conf);
uiMessage(3LL, "inv_addr = %s", *((const char **)conf + 1));
uiMessage(3LL, "inv_port = %d", *((unsigned int *)conf + 4));
uiMessage(3LL, "inv_num = %d", *((unsigned int *)conf + 5));
uiMessage(3LL, "lgr_query = %s", *((const char **)conf + 3));
uiMessage(3LL, "lgr_interval = %d", *((unsigned int *)conf + 8));
uiMessage(3LL, "mysql_addr = %s", *((const char **)conf + 5));
uiMessage(3LL, "mysql_port = %d", *((unsigned int *)conf + 12));
uiMessage(3LL, "mysql_user = %s", *((const char **)conf + 7));
uiMessage(3LL, "mysql_password = %s", *((const char **)conf + 8));
uiMessage(3LL, "mysql_database = %s", *((const char **)conf + 9));
return uiMessage(3LL, "mysql_table = %s", *((const char **)conf + 10));
}

original_code:
void cfgPrint()
{
    uiMessage(3, "debug_level = %d", conf->debug_level);
    uiMessage(3, "inv_addr = %s", conf->inv_addr);
    uiMessage(3, "inv_port = %d", conf->inv_port);
    uiMessage(3, "inv_num = %d", conf->inv_num);
    uiMessage(3, "lgr_query = %s", conf->lgr_query);
    uiMessage(3, "lgr_interval = %d", conf->lgr_interval);
    uiMessage(3, "mysql_addr = %s", conf->mysql_addr);
    uiMessage(3, "mysql_port = %d", conf->mysql_port);
    uiMessage(3, "mysql_user = %s", conf->mysql_user);
    uiMessage(3, "mysql_password = %s", conf->mysql_password);
    uiMessage(3, "mysql_database = %s", conf->mysql_database);
    uiMessage(3, "mysql_table = %s", conf->mysql_table);
}


cfgFree

decompiled_code:
void cfgFree()
{
free(*((void **)conf + 1));
free(*((void **)conf + 3));
free(*((void **)conf + 5));
free(*((void **)conf + 7));
free(*((void **)conf + 8));
free(*((void **)conf + 9));
free(*((void **)conf + 10));
free(conf);
}

canonical_decompiled_code:
void func2()
{
free(*((void **)conf + 1));
free(*((void **)conf + 3));
free(*((void **)conf + 5));
free(*((void **)conf + 7));
free(*((void **)conf + 8));
free(*((void **)conf + 9));
free(*((void **)conf + 10));
free(conf);
}

original_code:
void cfgFree()
{
    free(conf->inv_addr);
    free(conf->lgr_query);
    free(conf->mysql_addr);
    free(conf->mysql_user);
    free(conf->mysql_password);
    free(conf->mysql_database);
    free(conf->mysql_table);
    free(conf);
}


cfgParse

decompiled_code:
__int64 __fastcall cfgParse(int argc, char **argv)
{
char *v2; // r14
int v3; // er13
unsigned int v4; // ebx
char *v5; // r13
int v6; // eax
int v7; // eax
int v8; // ebx
int v9; // eax
_QWORD *v10; // rbx
int v11; // eax
_QWORD *v12; // r15
int v13; // eax
_QWORD *v14; // r15
int v15; // eax
_QWORD *v16; // r15
int v17; // eax
_QWORD *v18; // r15
int v19; // eax
_QWORD *v20; // rbx
int v21; // eax
_QWORD *v22; // rbx
int longind[15]; // [rsp+Ch] [rbp-3Ch] BYREF

longind[0] = 0;
v3 = 0;
while ( 1 )
{
v7 = getopt_long(argc, argv, "c:hVqvd:a:p:n:k:i:s:t:u:w:b:l:", &long_options_0, longind);
v8 = v7;
if ( v7 == -1 )
break;
switch ( v7 )
{
case '?':
goto LABEL_2;
case 'c':
v5 = optarg;
v6 = strlen(optarg);
v2 = (char *)calloc(1uLL, v6 + 1);
strcpy(v2, v5);
v3 = 1;
LABEL_4:
if ( v8 == 100 )
{
*(_DWORD *)conf = atoi(optarg);
LABEL_6:
if ( v8 == 110 )
{
*((_DWORD *)conf + 5) = atoi(optarg);
goto LABEL_8;
}
LABEL_34:
if ( v8 == 107 )
{
v13 = strlen(optarg);
v14 = conf;
v14[3] = realloc(*((void **)conf + 3), v13 + 1);
strcpy(*((char **)conf + 3), optarg);
goto LABEL_9;
}
goto LABEL_24;
}
goto LABEL_32;
case 'h':
LABEL_2:
uiHelp();
v4 = 0;
goto LABEL_40;
case 'V':
uiVersion();
v4 = 0;
goto LABEL_40;
}
if ( v7 != 113 )
{
if ( v7 == 118 )
{
*(_DWORD *)conf = 4;
goto LABEL_22;
}
goto LABEL_4;
}
*(_DWORD *)conf = 0;
LABEL_32:
if ( v8 == 97 )
{
v11 = strlen(optarg);
v12 = conf;
v12[1] = realloc(*((void **)conf + 1), v11 + 1);
strcpy(*((char **)conf + 1), optarg);
goto LABEL_34;
}
LABEL_22:
if ( v8 != 112 )
goto LABEL_6;
*((_DWORD *)conf + 4) = atoi(optarg);
LABEL_24:
if ( v8 == 105 )
{
*((_DWORD *)conf + 8) = atoi(optarg);
LABEL_26:
if ( v8 == 117 )
{
v17 = strlen(optarg);
v18 = conf;
v18[7] = realloc(*((void **)conf + 7), v17 + 1);
strcpy(*((char **)conf + 7), optarg);
goto LABEL_12;
}
LABEL_27:
if ( v8 != 119 )
goto LABEL_11;
v9 = strlen(optarg);
v10 = conf;
v10[8] = realloc(*((void **)conf + 8), v9 + 1);
strcpy(*((char **)conf + 8), optarg);
}
else
{
LABEL_8:
if ( v8 == 115 )
{
v15 = strlen(optarg);
v16 = conf;
v16[5] = realloc(*((void **)conf + 5), v15 + 1);
strcpy(*((char **)conf + 5), optarg);
goto LABEL_27;
}
LABEL_9:
if ( v8 != 116 )
goto LABEL_26;
*((_DWORD *)conf + 12) = atoi(optarg);
LABEL_11:
if ( v8 == 98 )
{
v19 = strlen(optarg);
v20 = conf;
v20[9] = realloc(*((void **)conf + 9), v19 + 1);
strcpy(*((char **)conf + 9), optarg);
}
else
{
LABEL_12:
if ( v8 == 108 )
{
v21 = strlen(optarg);
v22 = conf;
v22[10] = realloc(*((void **)conf + 10), v21 + 1);
strcpy(*((char **)conf + 10), optarg);
}
}
}
}
v4 = 1;
LABEL_40:
if ( v3 == 1 )
{
v4 = cfgFileParse(v2);
free(v2);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(int argc, char **argv)
{
char *v2; // r14
int v3; // er13
unsigned int v4; // ebx
char *v5; // r13
int v6; // eax
int v7; // eax
int v8; // ebx
int v9; // eax
_QWORD *v10; // rbx
int v11; // eax
_QWORD *v12; // r15
int v13; // eax
_QWORD *v14; // r15
int v15; // eax
_QWORD *v16; // r15
int v17; // eax
_QWORD *v18; // r15
int v19; // eax
_QWORD *v20; // rbx
int v21; // eax
_QWORD *v22; // rbx
int longind[15]; // [rsp+Ch] [rbp-3Ch] BYREF

longind[0] = 0;
v3 = 0;
while ( 1 )
{
v7 = getopt_long(argc, argv, "c:hVqvd:a:p:n:k:i:s:t:u:w:b:l:", &long_options_0, longind);
v8 = v7;
if ( v7 == -1 )
break;
switch ( v7 )
{
case '?':
goto LABEL_2;
case 'c':
v5 = optarg;
v6 = strlen(optarg);
v2 = (char *)calloc(1uLL, v6 + 1);
strcpy(v2, v5);
v3 = 1;
LABEL_4:
if ( v8 == 100 )
{
*(_DWORD *)conf = atoi(optarg);
LABEL_6:
if ( v8 == 110 )
{
*((_DWORD *)conf + 5) = atoi(optarg);
goto LABEL_8;
}
LABEL_34:
if ( v8 == 107 )
{
v13 = strlen(optarg);
v14 = conf;
v14[3] = realloc(*((void **)conf + 3), v13 + 1);
strcpy(*((char **)conf + 3), optarg);
goto LABEL_9;
}
goto LABEL_24;
}
goto LABEL_32;
case 'h':
LABEL_2:
uiHelp();
v4 = 0;
goto LABEL_40;
case 'V':
uiVersion();
v4 = 0;
goto LABEL_40;
}
if ( v7 != 113 )
{
if ( v7 == 118 )
{
*(_DWORD *)conf = 4;
goto LABEL_22;
}
goto LABEL_4;
}
*(_DWORD *)conf = 0;
LABEL_32:
if ( v8 == 97 )
{
v11 = strlen(optarg);
v12 = conf;
v12[1] = realloc(*((void **)conf + 1), v11 + 1);
strcpy(*((char **)conf + 1), optarg);
goto LABEL_34;
}
LABEL_22:
if ( v8 != 112 )
goto LABEL_6;
*((_DWORD *)conf + 4) = atoi(optarg);
LABEL_24:
if ( v8 == 105 )
{
*((_DWORD *)conf + 8) = atoi(optarg);
LABEL_26:
if ( v8 == 117 )
{
v17 = strlen(optarg);
v18 = conf;
v18[7] = realloc(*((void **)conf + 7), v17 + 1);
strcpy(*((char **)conf + 7), optarg);
goto LABEL_12;
}
LABEL_27:
if ( v8 != 119 )
goto LABEL_11;
v9 = strlen(optarg);
v10 = conf;
v10[8] = realloc(*((void **)conf + 8), v9 + 1);
strcpy(*((char **)conf + 8), optarg);
}
else
{
LABEL_8:
if ( v8 == 115 )
{
v15 = strlen(optarg);
v16 = conf;
v16[5] = realloc(*((void **)conf + 5), v15 + 1);
strcpy(*((char **)conf + 5), optarg);
goto LABEL_27;
}
LABEL_9:
if ( v8 != 116 )
goto LABEL_26;
*((_DWORD *)conf + 12) = atoi(optarg);
LABEL_11:
if ( v8 == 98 )
{
v19 = strlen(optarg);
v20 = conf;
v20[9] = realloc(*((void **)conf + 9), v19 + 1);
strcpy(*((char **)conf + 9), optarg);
}
else
{
LABEL_12:
if ( v8 == 108 )
{
v21 = strlen(optarg);
v22 = conf;
v22[10] = realloc(*((void **)conf + 10), v21 + 1);
strcpy(*((char **)conf + 10), optarg);
}
}
}
}
v4 = 1;
LABEL_40:
if ( v3 == 1 )
{
v4 = func3(v2);
free(v2);
}
return v4;
}

original_code:
int cfgParse(int argc, char **argv)
{
    int ret = 0;
    int option_index = 0;
    int c;
    int ln;
    int conf_file = 0;
    char *config_file;
    static struct option long_options[] = {
        {"config", 1, 0, 'c'},
        {"help", 0, 0, 'h'},
        {"version", 0, 0, 'V'},
        {"quiet", 0, 0, 'q'},
        {"verbose", 0, 0, 'v'},
        {"debug", 1, 0, 'd'},
        {"inv-addr", 1, 0, 'a'},
        {"inv-port", 1, 0, 'p'},
        {"inv-num", 1, 0, 'n'},
        {"query", 1, 0, 'k'},
        {"interval", 1, 0, 'i'},
        {"mysql-addr", 1, 0, 's'},
        {"mysql-port", 1, 0, 't'},
        {"mysql-user", 1, 0, 'u'},
        {"mysql-password", 1, 0, 'w'},
        {"mysql-database", 1, 0, 'b'},
        {"mysql-table", 1, 0, 'l'},
        {0, 0, 0, 0}
    };
    while(1) {
        c = getopt_long(argc, argv, "c:hVqvd:a:p:n:k:i:s:t:u:w:b:l:", long_options, &option_index);
        if(c == -1) {
            ret = 1;
            break;
        }
        if(c == '?') {
            uiHelp();
            break;
        }
        if(c == 'c') {
            conf_file = 1;
            ln = strlen(optarg) + 1;
            config_file = (char *) calloc(sizeof(char), ln);
            strcpy(config_file, optarg);
        }
        if(c == 'h') {
            uiHelp();
            break;
        }
        if(c == 'V') {
            uiVersion();
            break;
        }
        if(c == 'q') {
            conf->debug_level = 0;
        }
        if(c == 'v') {
            conf->debug_level = 4;
        }
        if(c == 'd') {
            conf->debug_level = atoi(optarg);
        }
        if(c == 'a') {
            ln = strlen(optarg) + 1;
            conf->inv_addr = (char *) realloc((void *) conf->inv_addr, sizeof(char) * ln);
            strcpy(conf->inv_addr, optarg);
        }
        if(c == 'p') {
            conf->inv_port = atoi(optarg);
        }
        if(c == 'n') {
            conf->inv_num = atoi(optarg);
        }
        if(c == 'k') {
            ln = strlen(optarg) + 1;
            conf->lgr_query = (char *) realloc((void *) conf->lgr_query, sizeof(char) * ln);
            strcpy(conf->lgr_query, optarg);
        }
        if(c == 'i') {
            conf->lgr_interval = atoi(optarg);
        }
        if(c == 's') {
            ln = strlen(optarg) + 1;
            conf->mysql_addr = (char *) realloc((void *) conf->mysql_addr, sizeof(char) * ln);
            strcpy(conf->mysql_addr, optarg);
        }
        if(c == 't') {
            conf->mysql_port = atoi(optarg);
        }
        if(c == 'u') {
            ln = strlen(optarg) + 1;
            conf->mysql_user = (char *) realloc((void *) conf->mysql_user, sizeof(char) * ln);
            strcpy(conf->mysql_user, optarg);
        }
        if(c == 'w') {
            ln = strlen(optarg) + 1;
            conf->mysql_password = (char *) realloc((void *) conf->mysql_password, sizeof(char) * ln);
            strcpy(conf->mysql_password, optarg);
        }
        if(c == 'b') {
            ln = strlen(optarg) + 1;
            conf->mysql_database = (char *) realloc((void *) conf->mysql_database, sizeof(char) * ln);
            strcpy(conf->mysql_database, optarg);
        }
        if(c == 'l') {
            ln = strlen(optarg) + 1;
            conf->mysql_table = (char *) realloc((void *) conf->mysql_table, sizeof(char) * ln);
            strcpy(conf->mysql_table, optarg);
        }
    }
    if(conf_file == 1) {
        ret = cfgFileParse(config_file);
        free(config_file);
    }
    return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

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


closeWave

repo: sarfata/learning-dsp

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

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

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

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

original_code:
void closeWave(t_wavef* wave)
{
  close(wave->fd);
  if (wave->header)
    free(wave->header);
  if (wave->audio)
    free(wave->audio);
  free(wave);
}

canonical_original_code:
void closeWave(struct t_wavef *wave)
{
  close(wave->fd);
  if (wave->header)
    free(wave->header);
  if (wave->audio)
    free(wave->audio);
  free(wave);
}

variable_types: struct t_wavef { int fd; struct t_wave_header { char chunkId[4]; unsigned int chunkSize; char chunkFormat[4]; unsigned int subChunk1Id; unsigned int formatSize; unsigned short audioFormat; unsigned short numChannels; unsigned int sampleRate; unsigned int byteRate; unsigned short blockAlign; unsigned short bitsPerSample; unsigned int subChunk2Id; unsigned int audioSize; } *header; unsigned char *audio; } *

user_defined_types: struct t_wavef { int fd; struct t_wave_header *header; unsigned char *audio; }


readHeader

decompiled_code:
__int64 __fastcall readHeader(__int64 a1)
{
_QWORD *v1; // rax
int v2; // eax

v1 = malloc(0x2CuLL);
*(_QWORD *)(a1 + 8) = v1;
*v1 = 0LL;
v1[1] = 0LL;
v1[2] = 0LL;
v1[3] = 0LL;
v1[4] = 0LL;
*((_DWORD *)v1 + 10) = 0;
v2 = pread(*(_DWORD *)a1, v1, 0x2CuLL, 0LL);
if ( v2 == 44 )
return 0LL;
fprintf(stderr, "Unable to read entire headers (%zi vs %zi)", v2, 0x2CuLL);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
_QWORD *v1; // rax
int v2; // eax

v1 = malloc(0x2CuLL);
*(_QWORD *)(a1 + 8) = v1;
*v1 = 0LL;
v1[1] = 0LL;
v1[2] = 0LL;
v1[3] = 0LL;
v1[4] = 0LL;
*((_DWORD *)v1 + 10) = 0;
v2 = pread(*(_DWORD *)a1, v1, 0x2CuLL, 0LL);
if ( v2 == 44 )
return 0LL;
fprintf(stderr, "Unable to read entire headers (%zi vs %zi)", v2, 0x2CuLL);
return 0xFFFFFFFFLL;
}

original_code:
int readHeader(t_wavef* wave)
{
  ssize_t len;
  wave->header = malloc(sizeof(t_wave_header));
  memset(wave->header, 0, sizeof(t_wave_header));
  len = pread(wave->fd, wave->header, sizeof(t_wave_header), 0);
  if (len != sizeof(t_wave_header)) {
    fprintf (stderr, "Unable to read entire headers (%zi vs %zi)", len, sizeof(t_wave_header));
    return -1;
  }
  return 0;
}

canonical_original_code:
int readHeader(struct t_wavef *wave)
{
  long len;
  wave->header = malloc(sizeof(t_wave_header));
  memset(wave->header, 0, sizeof(t_wave_header));
  len = pread(wave->fd, wave->header, sizeof(t_wave_header), 0);
  if (len != sizeof(t_wave_header)) {
    fprintf (stderr, "Unable to read entire headers (%zi vs %zi)", len, sizeof(t_wave_header));
    return -1;
  }
  return 0;
}

variable_types: len long

user_defined_types: struct t_wave_header { char chunkId[4]; unsigned int chunkSize; char chunkFormat[4]; unsigned int subChunk1Id; unsigned int formatSize; unsigned short audioFormat; unsigned short numChannels; unsigned int sampleRate; unsigned int byteRate; unsigned short blockAlign; unsigned short bitsPerSample; unsigned int subChunk2Id; unsigned int audioSize; }


readAudio

decompiled_code:
__int64 __fastcall readAudio(__int64 a1)
{
__int64 v1; // rbp
void *v2; // rax
int v3; // eax

v1 = *(_QWORD *)(a1 + 8);
v2 = malloc(*(unsigned int *)(v1 + 40));
*(_QWORD *)(a1 + 16) = v2;
v3 = pread(*(_DWORD *)a1, v2, *(unsigned int *)(v1 + 40), 44LL);
if ( *(_DWORD *)(*(_QWORD *)(a1 + 8) + 40LL) == (__int64)v3 )
return 0LL;
fprintf(stderr, "Unable to read complete audio data (%zi vs %zi)", v3, 4uLL);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbp
void *v2; // rax
int v3; // eax

v1 = *(_QWORD *)(a1 + 8);
v2 = malloc(*(unsigned int *)(v1 + 40));
*(_QWORD *)(a1 + 16) = v2;
v3 = pread(*(_DWORD *)a1, v2, *(unsigned int *)(v1 + 40), 44LL);
if ( *(_DWORD *)(*(_QWORD *)(a1 + 8) + 40LL) == (__int64)v3 )
return 0LL;
fprintf(stderr, "Unable to read complete audio data (%zi vs %zi)", v3, 4uLL);
return 0xFFFFFFFFLL;
}

original_code:
int readAudio(t_wavef* wave)
{
  ssize_t len;
  wave->audio = malloc(wave->header->audioSize);
  len = pread(wave->fd, wave->audio, wave->header->audioSize, sizeof(t_wave_header));
  if (len != wave->header->audioSize) {
    fprintf (stderr, "Unable to read complete audio data (%zi vs %zi)", len, sizeof(wave->header->audioSize));
    return -1;
  }
  return 0;
}

canonical_original_code:
int readAudio(struct t_wavef *wave)
{
  long len;
  wave->audio = malloc(wave->header->audioSize);
  len = pread(wave->fd, wave->audio, wave->header->audioSize, sizeof(t_wave_header));
  if (len != wave->header->audioSize) {
    fprintf (stderr, "Unable to read complete audio data (%zi vs %zi)", len, sizeof(wave->header->audioSize));
    return -1;
  }
  return 0;
}


waveSamples

decompiled_code:
__int64 __fastcall waveSamples(__int64 a1)
{
return *(_DWORD *)(a1 + 40) / (unsigned int)*(unsigned __int16 *)(a1 + 32);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(_DWORD *)(a1 + 40) / (unsigned int)*(unsigned __int16 *)(a1 + 32);
}

original_code:
uint32_t waveSamples(t_wave_header *header)
{
  return header->audioSize / header->blockAlign;
}

canonical_original_code:
unsigned int waveSamples(struct t_wave_header *header)
{
  return header->audioSize / header->blockAlign;
}

variable_types: struct t_wave_header { char chunkId[4]; unsigned int chunkSize; char chunkFormat[4]; unsigned int subChunk1Id; unsigned int formatSize; unsigned short audioFormat; unsigned short numChannels; unsigned int sampleRate; unsigned int byteRate; unsigned short blockAlign; unsigned short bitsPerSample; unsigned int subChunk2Id; unsigned int audioSize; } *

return_type: unsigned int


waveDuration

decompiled_code:
float __fastcall waveDuration(__int64 a1)
{
return (double)(int)waveSamples(a1) / (double)*(int *)(a1 + 24);
}

canonical_decompiled_code:
float __fastcall func4(__int64 a1)
{
return (double)(int)func3(a1) / (double)*(int *)(a1 + 24);
}

original_code:
float waveDuration(t_wave_header *header)
{
  return waveSamples(header) * 1.0 / header->sampleRate;
}

canonical_original_code:
float waveDuration(struct t_wave_header *header)
{
  return waveSamples(header) * 1.0 / header->sampleRate;
}

return_type: float


debugWaveHeader

decompiled_code:
int __fastcall debugWaveHeader(__int64 a1)
{
unsigned int v1; // eax
float v2; // xmm0_4

printf("ChunkId : %.4s\n", (const char *)a1);
printf("Format : %.4s\n\n", (const char *)(a1 + 8));
printf("Audio Format: %i\n", *(unsigned __int16 *)(a1 + 20));
printf("Num Channels: %i\n", *(unsigned __int16 *)(a1 + 22));
printf("Sample Rate: %i\n", *(unsigned int *)(a1 + 24));
printf("Byte Rate: %i\n", *(unsigned int *)(a1 + 28));
printf("Block Align: %i\n", *(unsigned __int16 *)(a1 + 32));
printf("BitsPerSample: %i\n\n", *(unsigned __int16 *)(a1 + 34));
printf("Data size: %i\n", *(unsigned int *)(a1 + 40));
v1 = waveSamples(a1);
printf("NumSamples: %i\n", v1);
v2 = waveDuration(a1);
return printf("File duration: %f\n", v2);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
unsigned int v1; // eax
float v2; // xmm0_4

printf("ChunkId : %.4s\n", (const char *)a1);
printf("Format : %.4s\n\n", (const char *)(a1 + 8));
printf("Audio Format: %i\n", *(unsigned __int16 *)(a1 + 20));
printf("Num Channels: %i\n", *(unsigned __int16 *)(a1 + 22));
printf("Sample Rate: %i\n", *(unsigned int *)(a1 + 24));
printf("Byte Rate: %i\n", *(unsigned int *)(a1 + 28));
printf("Block Align: %i\n", *(unsigned __int16 *)(a1 + 32));
printf("BitsPerSample: %i\n\n", *(unsigned __int16 *)(a1 + 34));
printf("Data size: %i\n", *(unsigned int *)(a1 + 40));
v1 = func3(a1);
printf("NumSamples: %i\n", v1);
v2 = func4(a1);
return printf("File duration: %f\n", v2);
}

original_code:
void debugWaveHeader(t_wave_header *header)
{
  printf("ChunkId   : %.4s\n", header->chunkId);
  printf("Format    : %.4s\n\n", header->chunkFormat);
  printf("Audio Format: %i\n", header->audioFormat);
  printf("Num Channels: %i\n", header->numChannels);
  printf("Sample Rate: %i\n", header->sampleRate);
  printf("Byte Rate: %i\n", header->byteRate);
  printf("Block Align: %i\n", header->blockAlign);
  printf("BitsPerSample: %i\n\n", header->bitsPerSample);
  printf("Data size: %i\n", header->audioSize);
  printf("NumSamples: %i\n", waveSamples(header));
  printf("File duration: %f\n", waveDuration(header));
}

canonical_original_code:
void debugWaveHeader(struct t_wave_header *header)
{
  printf("ChunkId   : %.4s\n", header->chunkId);
  printf("Format    : %.4s\n\n", header->chunkFormat);
  printf("Audio Format: %i\n", header->audioFormat);
  printf("Num Channels: %i\n", header->numChannels);
  printf("Sample Rate: %i\n", header->sampleRate);
  printf("Byte Rate: %i\n", header->byteRate);
  printf("Block Align: %i\n", header->blockAlign);
  printf("BitsPerSample: %i\n\n", header->bitsPerSample);
  printf("Data size: %i\n", header->audioSize);
  printf("NumSamples: %i\n", waveSamples(header));
  printf("File duration: %f\n", waveDuration(header));
}


dumpSamples

decompiled_code:
void __fastcall dumpSamples(__int64 a1, int a2)
{
__int64 v2; // r13
__int64 i; // rbx

v2 = *(_QWORD *)(a1 + 16);
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
{
if ( (unsigned int)waveSamples(*(_QWORD *)(a1 + 8)) <= (unsigned int)i )
break;
printf(
"audio[%i]: %02X%02X %i\n",
(unsigned int)i,
*(unsigned __int8 *)(*(_QWORD *)(a1 + 16) + 2 * i),
*(unsigned __int8 *)(*(_QWORD *)(a1 + 16) + 2 * i + 1),
(unsigned int)*(__int16 *)(v2 + 2 * i));
}
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2)
{
__int64 v2; // r13
__int64 i; // rbx

v2 = *(_QWORD *)(a1 + 16);
if ( a2 > 0 )
{
for ( i = 0LL; i != a2; ++i )
{
if ( (unsigned int)func3(*(_QWORD *)(a1 + 8)) <= (unsigned int)i )
break;
printf(
"audio[%i]: %02X%02X %i\n",
(unsigned int)i,
*(unsigned __int8 *)(*(_QWORD *)(a1 + 16) + 2 * i),
*(unsigned __int8 *)(*(_QWORD *)(a1 + 16) + 2 * i + 1),
(unsigned int)*(__int16 *)(v2 + 2 * i));
}
}
}

original_code:
void dumpSamples(t_wavef *wave, int count)
{
  int16_t *audio = (int16_t*)wave->audio;
  for (int i = 0; i < count && i < waveSamples(wave->header); i++) {
    printf("audio[%i]: %02X%02X %i\n", i, wave->audio[i*2], wave->audio[i*2+1], audio[i]);
  }
}

canonical_original_code:
void dumpSamples(struct t_wavef *wave, int count)
{
  short *audio = (int16_t*)wave->audio;
  for (int i = 0; i < count && i < waveSamples(wave->header); i++) {
    printf("audio[%i]: %02X%02X %i\n", i, wave->audio[i*2], wave->audio[i*2+1], audio[i]);
  }
}

variable_types: count int

variable_types:
audio
short *

variable_types: i int


main

canonical_name: main

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

printf("%s: put some DSP classes to work\n", *argv);
if ( argc <= 2 )
{
fprintf(stderr, " Usage: %s <wave file> <samples to dft>\n", *argv);
fprintf(stderr, " Example: %s samples/sine-3200.wav 512\n", *argv);
exit(-1);
}
v3 = atoi(argv[2]);
v4 = openWave(argv[1]);
debugWaveHeader(*(_QWORD *)(v4 + 8));
v5 = performDft(*(_QWORD *)(v4 + 16), v3, 1LL);
printSpectrum(v5, *(unsigned int *)(*(_QWORD *)(v4 + 8) + 24LL));
freeSpectrum(v5);
closeWave(v4);
return 0;
}

original_code:
int main(int argc, char **argv) {
  t_wavef *wave;
  spectrum_t *mydft;
  int length;
  printf("%s: put some DSP classes to work\n", argv[0]);
  if (argc < 3) {
    fprintf(stderr, "  Usage: %s <wave file> <samples to dft>\n", argv[0]);
    fprintf(stderr, "  Example: %s samples/sine-3200.wav 512\n", argv[0]);
    exit(-1);
  }
  length = atoi(argv[2]);
  wave = openWave(argv[1]);
  debugWaveHeader(wave->header);
  mydft = performDft((int16_t*)wave->audio, length, 1);
  printSpectrum(mydft, wave->header->sampleRate);
  freeSpectrum(mydft);
  closeWave(wave);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  struct t_wavef *wave;
  struct spectrum_t *mydft;
  int length;
  printf("%s: put some DSP classes to work\n", argv[0]);
  if (argc < 3) {
    fprintf(stderr, "  Usage: %s <wave file> <samples to dft>\n", argv[0]);
    fprintf(stderr, "  Example: %s samples/sine-3200.wav 512\n", argv[0]);
    exit(-1);
  }
  length = atoi(argv[2]);
  wave = openWave(argv[1]);
  debugWaveHeader(wave->header);
  mydft = performDft((short *)wave->audio, length, 1);
  printSpectrum(mydft, wave->header->sampleRate);
  freeSpectrum(mydft);
  closeWave(wave);
  return 0;
}

variable_types: struct spectrum_t { unsigned int k; double *spectrum_r; double *spectrum_i; } *

variable_types: length int


original_code:
void test_remove_spaces()
{
    const char *cases[8][2] = {{" test  str  ", "test str"},
                         {"", ""},
                         {" ", ""},
                         {"  ", ""},
                         {" test ", "test"},
                         {"  test s  tring  s", "test s tring s"},
                         {"te st ", "te st"},
                         {"testvalue", "testvalue"}
                        };
    int i;
    for(i = 0; i < sizeof(cases) / (2 * sizeof(char *)); i++)
    {
        char buf[256];
        strcpy(buf, cases[i][0]);
        remove_spaces_impl(buf);
        ((void) sizeof ((strcmp(buf, cases[i][1]) == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, cases[i][1]) == 0) ; else __assert_fail ("strcmp(buf, cases[i][1]) == 0", "test.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    }
    remove_spaces_impl(0);
}

canonical_original_code:
void test_remove_spaces()
{
    char *cases[8][2] = {{" test  str  ", "test str"},
                         {"", ""},
                         {" ", ""},
                         {"  ", ""},
                         {" test ", "test"},
                         {"  test s  tring  s", "test s tring s"},
                         {"te st ", "te st"},
                         {"testvalue", "testvalue"}
                        };
    int i;
    for(i = 0; i < sizeof(cases) / (2 * sizeof(char *)); i++)
    {
        char buf[256];
        strcpy(buf, cases[i][0]);
        remove_spaces_impl(buf);
        ((void) sizeof ((strcmp(buf, cases[i][1]) == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, cases[i][1]) == 0) ; else __assert_fail ("strcmp(buf, cases[i][1]) == 0", "test.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    }
    remove_spaces_impl(0);
}

variable_types:
cases
char *[8][2]

variable_types:
buf
char [256]


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

if ( (unsigned int)is_stop_word("non_stop") )
__assert_fail("is_stop_word(\"non_stop\") == 0", "test.c", 0x21u, "test_stop_words");
if ( (unsigned int)is_stop_word("the der") )
__assert_fail("is_stop_word(\"the der\") == 0", "test.c", 0x22u, "test_stop_words");
if ( (unsigned int)is_stop_word("the") != 1 )
__assert_fail("is_stop_word(\"the\") == 1", "test.c", 0x23u, "test_stop_words");
if ( (unsigned int)is_stop_word("der") != 1 )
__assert_fail("is_stop_word(\"der\") == 1", "test.c", 0x24u, "test_stop_words");
if ( (unsigned int)is_stop_word(&unk_402101) != 1 )
__assert_fail(aIsStopWord, "test.c", 0x25u, "test_stop_words");
result = is_stop_word("");
if ( (_DWORD)result != 1 )
__assert_fail("is_stop_word(\"\") == 1", "test.c", 0x26u, "test_stop_words");
return result;
}

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

if ( (unsigned int)func13("non_stop") )
__assert_fail("is_stop_word(\"non_stop\") == 0", "test.c", 0x21u, "test_stop_words");
if ( (unsigned int)func13("the der") )
__assert_fail("is_stop_word(\"the der\") == 0", "test.c", 0x22u, "test_stop_words");
if ( (unsigned int)func13("the") != 1 )
__assert_fail("is_stop_word(\"the\") == 1", "test.c", 0x23u, "test_stop_words");
if ( (unsigned int)func13("der") != 1 )
__assert_fail("is_stop_word(\"der\") == 1", "test.c", 0x24u, "test_stop_words");
if ( (unsigned int)func13(&unk_402101) != 1 )
__assert_fail(aIsStopWord, "test.c", 0x25u, "test_stop_words");
result = func13("");
if ( (_DWORD)result != 1 )
__assert_fail("is_stop_word(\"\") == 1", "test.c", 0x26u, "test_stop_words");
return result;
}

original_code:
void test_stop_words()
{
    ((void) sizeof ((is_stop_word("non_stop") == 0) ? 1 : 0), __extension__ ({ if (is_stop_word("non_stop") == 0) ; else __assert_fail ("is_stop_word(\"non_stop\") == 0", "test.c", 33, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((is_stop_word("the der") == 0) ? 1 : 0), __extension__ ({ if (is_stop_word("the der") == 0) ; else __assert_fail ("is_stop_word(\"the der\") == 0", "test.c", 34, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((is_stop_word("the") == 1) ? 1 : 0), __extension__ ({ if (is_stop_word("the") == 1) ; else __assert_fail ("is_stop_word(\"the\") == 1", "test.c", 35, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((is_stop_word("der") == 1) ? 1 : 0), __extension__ ({ if (is_stop_word("der") == 1) ; else __assert_fail ("is_stop_word(\"der\") == 1", "test.c", 36, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((is_stop_word("на") == 1) ? 1 : 0), __extension__ ({ if (is_stop_word("на") == 1) ; else __assert_fail ("is_stop_word(\"на\") == 1", "test.c", 37, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((is_stop_word("") == 1) ? 1 : 0), __extension__ ({ if (is_stop_word("") == 1) ; else __assert_fail ("is_stop_word(\"\") == 1", "test.c", 38, __extension__ __PRETTY_FUNCTION__); }));
}


original_code:
void test_remove_stop_words()
{
    const char *cases[9][2] = {{"test", "test"},
                               {"the test", "test"},
                               {"two tests", "two tests"},
                               {"test of the tests hoho", "test tests hoho"},
                               {"", ""},
                               {"the of", ""},
                               {"  ", ""},
                               {" word  ", "word"},
                               {" the word ", "word"}
                        };
    int i;
    for(i = 0; i < sizeof(cases) / (2 * sizeof(char *)); i++)
    {
        char buf[256];
        strcpy(buf, cases[i][0]);
        remove_stop_words_impl(buf);
        ((void) sizeof ((strcmp(buf, cases[i][1]) == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, cases[i][1]) == 0) ; else __assert_fail ("strcmp(buf, cases[i][1]) == 0", "test.c", 60, __extension__ __PRETTY_FUNCTION__); }));
    }
    remove_stop_words_impl(0);
}

canonical_original_code:
void test_remove_stop_words()
{
    char *cases[9][2] = {{"test", "test"},
                               {"the test", "test"},
                               {"two tests", "two tests"},
                               {"test of the tests hoho", "test tests hoho"},
                               {"", ""},
                               {"the of", ""},
                               {"  ", ""},
                               {" word  ", "word"},
                               {" the word ", "word"}
                        };
    int i;
    for(i = 0; i < sizeof(cases) / (2 * sizeof(char *)); i++)
    {
        char buf[256];
        strcpy(buf, cases[i][0]);
        remove_stop_words_impl(buf);
        ((void) sizeof ((strcmp(buf, cases[i][1]) == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, cases[i][1]) == 0) ; else __assert_fail ("strcmp(buf, cases[i][1]) == 0", "test.c", 60, __extension__ __PRETTY_FUNCTION__); }));
    }
    remove_stop_words_impl(0);
}

variable_types:
cases
char *[9][2]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
test_remove_spaces(argc, argv, envp);
test_stop_words();
test_remove_stop_words();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
func8();
func9();
return 0;
}

original_code:
int main(int argc, const char* argv[])
{
    test_remove_spaces();
    test_stop_words();
    test_remove_stop_words();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    test_remove_spaces();
    test_stop_words();
    test_remove_stop_words();
    return 0;
}

variable_types:
argv
char *[]


decompiled_code:
void __fastcall remove_stop_words_impl(void *s)
{
char *v1; // r12
size_t v2; // rax
char *v3; // rbx
int v4; // ebp
char *v5; // r12
size_t v6; // rbp
char delim[2]; // [rsp+0h] [rbp-32h] BYREF
char *stringp[6]; // [rsp+2h] [rbp-30h] BYREF

if ( s )
{
v1 = (char *)s;
if ( *(_BYTE *)s )
{
strcpy(delim, " ");
stringp[0] = strdup((const char *)s);
v2 = strlen((const char *)s);
memset(s, 32, v2);
v3 = strsep(stringp, delim);
if ( v3 )
{
v4 = 0;
do
{
if ( !(unsigned int)is_stop_word(v3) )
{
v5 = &v1[-(v4 == 0) + 1];
v6 = (int)strlen(v3);
memcpy(v5, v3, v6);
v1 = &v5[v6];
v4 = 1;
}
v3 = strsep(stringp, delim);
}
while ( v3 );
}
*v1 = 0;
free(stringp[0]);
}
}
}

canonical_decompiled_code:
void __fastcall func11(void *s)
{
char *v1; // r12
size_t v2; // rax
char *v3; // rbx
int v4; // ebp
char *v5; // r12
size_t v6; // rbp
char delim[2]; // [rsp+0h] [rbp-32h] BYREF
char *stringp[6]; // [rsp+2h] [rbp-30h] BYREF

if ( s )
{
v1 = (char *)s;
if ( *(_BYTE *)s )
{
strcpy(delim, " ");
stringp[0] = strdup((const char *)s);
v2 = strlen((const char *)s);
memset(s, 32, v2);
v3 = strsep(stringp, delim);
if ( v3 )
{
v4 = 0;
do
{
if ( !(unsigned int)func13(v3) )
{
v5 = &v1[-(v4 == 0) + 1];
v6 = (int)strlen(v3);
memcpy(v5, v3, v6);
v1 = &v5[v6];
v4 = 1;
}
v3 = strsep(stringp, delim);
}
while ( v3 );
}
*v1 = 0;
free(stringp[0]);
}
}
}

original_code:
void remove_stop_words_impl(char *str)
{
    if(str && *str)
    {
        const char delimiters[] = " ";
        char *token = ((void *)0);
        char *cp = strdup(str);
        memset(str, ' ', strlen(str));
        int first = 0;
        token = strsep(&cp, delimiters);
        while(token)
        {
            if(!is_stop_word(token))
            {
                if(first != 0)
                    *++str;
                else
                    first = 1;
                int len = strlen(token);
                memcpy(str, token, len);
                str += len;
            }
            token = strsep(&cp, delimiters);
        }
        *str = 0;
        free((void *)cp);
    }
}

canonical_original_code:
void remove_stop_words_impl(char *str)
{
    if(str && *str)
    {
        char delimiters[] = " ";
        char *token = ((void *)0);
        char *cp = strdup(str);
        memset(str, ' ', strlen(str));
        int first = 0;
        token = strsep(&cp, delimiters);
        while(token)
        {
            if(!is_stop_word(token))
            {
                if(first != 0)
                    *++str;
                else
                    first = 1;
                int len = strlen(token);
                memcpy(str, token, len);
                str += len;
            }
            token = strsep(&cp, delimiters);
        }
        *str = 0;
        free((void *)cp);
    }
}

variable_types:
str
char *

variable_types:
delimiters
char []

variable_types:
token
char *

variable_types:
cp
char *

variable_types: first int

variable_types: len int


rnd

repo: sashabaranov/random-walk

decompiled_code:
__int64 __fastcall rnd(int a1, int a2, unsigned __int16 *a3)
{
return a1 + (unsigned int)(nrand48(a3) % a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2, unsigned __int16 *a3)
{
return a1 + (unsigned int)(nrand48(a3) % a2);
}

original_code:
int rnd(int min, int max, unsigned short *state)
{
  return nrand48(state) % max + min;
}

variable_types: min int

variable_types: max int

variable_types:
state
unsigned short *


step

decompiled_code:
__int64 __fastcall step(unsigned int a1, int a2, int a3, unsigned __int16 *a4, int a5)
{
int v8; // ebx
double i; // xmm0_8
int v10; // eax
double x; // [rsp+8h] [rbp-40h]

if ( a2 > 0 )
{
v8 = 0;
x = (double)a5;
for ( i = pow((double)a5, (double)0); ; i = pow(x, (double)v8) )
{
v10 = a1 + (int)((double)(int)rnd(-1, 3, a4) * i);
if ( v10 <= a3 && v10 > 0 )
a1 = v10;
if ( a2 == ++v8 )
break;
}
}
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, int a2, int a3, unsigned __int16 *a4, int a5)
{
int v8; // ebx
double i; // xmm0_8
int v10; // eax
double x; // [rsp+8h] [rbp-40h]

if ( a2 > 0 )
{
v8 = 0;
x = (double)a5;
for ( i = pow((double)a5, (double)0); ; i = pow(x, (double)v8) )
{
v10 = a1 + (int)((double)(int)func7(-1, 3, a4) * i);
if ( v10 <= a3 && v10 > 0 )
a1 = v10;
if ( a2 == ++v8 )
break;
}
}
return a1;
}

original_code:
int step(int offset, int dim, int grid_size, unsigned short *state, int N)
{
    int k, d;
    for(k = 0; k < dim; ++k)
    {
        d = pow(N, k) * rnd(-1, 3, state);
        if(offset + d > 0 && offset + d <= grid_size) offset += d;
    }
    return offset;
}

canonical_original_code:
int step(int offset, int dim, int grid_size, unsigned short *state, int N)
{
    int k; int d;
    for(k = 0; k < dim; ++k)
    {
        d = pow(N, k) * rnd(-1, 3, state);
        if(offset + d > 0 && offset + d <= grid_size) offset += d;
    }
    return offset;
}

variable_types: offset int

variable_types: dim int

variable_types: N int

variable_types: k int

variable_types: d int


regenerate

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

result = (char *)memset(a1, 0, 4LL * a2);
v5 = 0;
while ( v5 < a3 )
{
result = &a1[4 * (rand() % a2)];
if ( *(_DWORD *)result != 1 )
{
*(_DWORD *)result = 1;
++v5;
}
}
return result;
}

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

result = (char *)memset(a1, 0, 4LL * a2);
v5 = 0;
while ( v5 < a3 )
{
result = &a1[4 * (rand() % a2)];
if ( *(_DWORD *)result != 1 )
{
*(_DWORD *)result = 1;
++v5;
}
}
return result;
}

original_code:
void regenerate(int* grid, int grid_size, int cops)
{
    memset(grid, 0, grid_size * sizeof(int));
    int pos, count = 0;
    while(count < cops)
    {
         pos = rand() % grid_size;
         if(grid[pos] != 1)
         {
            grid[pos] = 1;
            ++ count;
         }
    }
}

canonical_original_code:
void regenerate(int *grid, int grid_size, int cops)
{
    memset(grid, 0, grid_size * sizeof(int));
    int pos; int count = 0;
    while(count < cops)
    {
         pos = rand() % grid_size;
         if(grid[pos] != 1)
         {
            grid[pos] = 1;
            ++ count;
         }
    }
}

variable_types:
grid
int *

variable_types: cops int

variable_types: pos int

variable_types: count int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // er15
double v5; // xmm0_8
unsigned int v6; // eax
int v7; // eax
int v9; // er14
void *v10; // r12
int *v11; // rbx
int i; // ebp
int *v13; // rax
__int64 v14; // rdx
double v15; // xmm1_8
double v16; // xmm2_8
double v17; // rbx
double v18; // [rsp+8h] [rbp-90h]
double v19; // [rsp+10h] [rbp-88h]
double v20; // [rsp+10h] [rbp-88h]
int v21; // [rsp+1Ch] [rbp-7Ch]
int v22; // [rsp+20h] [rbp-78h]
unsigned int v23; // [rsp+24h] [rbp-74h]
double v24; // [rsp+28h] [rbp-70h]

omp_get_wtime();
v24 = v5;
v6 = time(0LL);
srand(v6);
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "d:N:c:K:M:h");
if ( v7 == -1 )
{
v9 = (int)pow((double)v22, (double)v21);
v10 = malloc(4LL * v9);
memset(v10, 0, v9);
v11 = (int *)calloc(v3, 4uLL);
if ( v3 <= 0 )
{
v16 = (double)v3;
v20 = 0.0 / (double)v3;
}
else
{
for ( i = 0; i != v3; ++i )
{
regenerate((char *)v10, v9, v23);
GOMP_parallel();
}
v13 = v11;
v14 = (__int64)&v11[v3 - 1 + 1];
v15 = (double)v4;
do
v19 = (double)*v11++ / v15 + v19;
while ( v11 != (int *)v14 );
v16 = (double)v3;
v20 = v19 / (double)v3;
do
{
v18 = ((double)*v13 / v15 - v20) * ((double)*v13 / v15 - v20) + v18;
++v13;
}
while ( v13 != (int *)v14 );
}
v17 = sqrt(v18 / v16);
omp_get_wtime();
printf("%d %f %f %f %d %d\n", v23, v20, v17, v17 - v24, (unsigned int)v4, (unsigned int)v3);
free(v10);
}
else
{
switch ( v7 )
{
case 'K':
v3 = atoi(_bss_start);
continue;
case 'M':
v4 = atoi(_bss_start);
continue;
case 'N':
v22 = atoi(_bss_start);
continue;
case 'c':
v23 = atoi(_bss_start);
continue;
case 'd':
v21 = atoi(_bss_start);
continue;
case 'h':
puts(
"Usage:\n"
"\t-d <number>\tDimensions count\n"
"\t-N <number>\tGrid size\n"
"\t-c <number>\tCops count\n"
"\t-K <number>\tGrid regenerations count\n"
"\t-M <number>\tSimulations count per regeneration\n"
"\t-s\t\tShouting between drunkards");
break;
default:
continue;
}
}
break;
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // er15
double v5; // xmm0_8
unsigned int v6; // eax
int v7; // eax
int v9; // er14
void *v10; // r12
int *v11; // rbx
int i; // ebp
int *v13; // rax
__int64 v14; // rdx
double v15; // xmm1_8
double v16; // xmm2_8
double v17; // rbx
double v18; // [rsp+8h] [rbp-90h]
double v19; // [rsp+10h] [rbp-88h]
double v20; // [rsp+10h] [rbp-88h]
int v21; // [rsp+1Ch] [rbp-7Ch]
int v22; // [rsp+20h] [rbp-78h]
unsigned int v23; // [rsp+24h] [rbp-74h]
double v24; // [rsp+28h] [rbp-70h]

omp_get_wtime();
v24 = v5;
v6 = time(0LL);
srand(v6);
while ( 2 )
{
v7 = getopt(argc, (char *const *)argv, "d:N:c:K:M:h");
if ( v7 == -1 )
{
v9 = (int)pow((double)v22, (double)v21);
v10 = malloc(4LL * v9);
memset(v10, 0, v9);
v11 = (int *)calloc(v3, 4uLL);
if ( v3 <= 0 )
{
v16 = (double)v3;
v20 = 0.0 / (double)v3;
}
else
{
for ( i = 0; i != v3; ++i )
{
func10((char *)v10, v9, v23);
GOMP_parallel();
}
v13 = v11;
v14 = (__int64)&v11[v3 - 1 + 1];
v15 = (double)v4;
do
v19 = (double)*v11++ / v15 + v19;
while ( v11 != (int *)v14 );
v16 = (double)v3;
v20 = v19 / (double)v3;
do
{
v18 = ((double)*v13 / v15 - v20) * ((double)*v13 / v15 - v20) + v18;
++v13;
}
while ( v13 != (int *)v14 );
}
v17 = sqrt(v18 / v16);
omp_get_wtime();
printf("%d %f %f %f %d %d\n", v23, v20, v17, v17 - v24, (unsigned int)v4, (unsigned int)v3);
free(v10);
}
else
{
switch ( v7 )
{
case 'K':
v3 = atoi(_bss_start);
continue;
case 'M':
v4 = atoi(_bss_start);
continue;
case 'N':
v22 = atoi(_bss_start);
continue;
case 'c':
v23 = atoi(_bss_start);
continue;
case 'd':
v21 = atoi(_bss_start);
continue;
case 'h':
puts(
"Usage:\n"
"\t-d <number>\tDimensions count\n"
"\t-N <number>\tGrid size\n"
"\t-c <number>\tCops count\n"
"\t-K <number>\tGrid regenerations count\n"
"\t-M <number>\tSimulations count per regeneration\n"
"\t-s\t\tShouting between drunkards");
break;
default:
continue;
}
}
break;
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    double start = omp_get_wtime();
    srand ( time(((void *)0)) );
    int dim, N, grid_size;
    int cops;
    int K;
    int M;
    int i, j;
    int *grid, *stats;
    int threads_num, tid;
    int go_flag, count;
    unsigned short state[3];
    unsigned int seed;
    int res = 0;
    char* usage = "Usage:\n"
                  "\t-d <number>\tDimensions count\n"
                  "\t-N <number>\tGrid size\n"
                  "\t-c <number>\tCops count\n"
                  "\t-K <number>\tGrid regenerations count\n"
                  "\t-M <number>\tSimulations count per regeneration\n"
                  "\t-s\t\tShouting between drunkards\n";
    while((res = getopt(argc, argv, "d:N:c:K:M:h")) != -1)
    {
        switch(res)
        {
            case 'd': dim = atoi(optarg); break;
            case 'N': N = atoi(optarg); break;
            case 'c': cops = atoi(optarg); break;
            case 'K': K = atoi(optarg); break;
            case 'M': M = atoi(optarg); break;
            case 'h': printf("%s", usage); return 0;
        }
    }
    grid_size = pow(N, dim);
    grid = malloc(grid_size * sizeof(int));
    memset(grid, 0, grid_size);
    stats = calloc(K, sizeof(int));
    for(i = 0; i < K; ++i)
    {
        regenerate(grid, grid_size, cops);

#pragma omp parallel shared(grid, stats) private(j, tid, threads_num, state, seed, go_flag, count) firstprivate(i, grid_size, K, M, N, dim)
        {
            tid = omp_get_thread_num();
            threads_num = omp_get_num_threads();
            seed = time(((void *)0)) + (unsigned int) omp_get_thread_num();
            memcpy(state, &seed, sizeof(seed));
            int* drunkards = calloc(M/threads_num, sizeof(int));
            go_flag = M/threads_num;
            while(go_flag)
            {
                count = 0;
                for(j = 0; j < M/threads_num; ++j)
                {
                    if(drunkards[j] != -1)
                    {
                        ++ count;
                        drunkards[j] = step(drunkards[j], dim, grid_size, state, N);
                        if(grid[drunkards[j]] == 1)
                        {
                            drunkards[j] = -1;
                        }
                        if(drunkards[j] == grid_size)
                        {

#pragma omp atomic
                            stats[i] += 1;
                            drunkards[j] = -1;
                        }
                    }
                }
                if(count == 0) go_flag = 0;
            }
            free(drunkards);
        }
    }
    double avg, stddev;
    for(i = 0; i < K; ++i) avg += (double) stats[i] / M;
    avg /= K;
    for(i = 0; i < K; ++i) stddev += ((double)stats[i]/M - avg) * ((double)stats[i]/M - avg);
    stddev = sqrt(stddev / K);
    double end = omp_get_wtime();
    printf("%d %f %f %f %d %d\n", cops, avg, stddev, end - start, M, K);
    free(grid);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    double start = omp_get_wtime();
    srand ( time(((void *)0)) );
    int dim; int N; int grid_size;
    int cops;
    int K;
    int M;
    int i; int j;
    int *grid; int *stats;
    int threads_num; int tid;
    int go_flag; int count;
    unsigned short state[3];
    unsigned int seed;
    int res = 0;
    char *usage = "Usage:\n"
                  "\t-d <number>\tDimensions count\n"
                  "\t-N <number>\tGrid size\n"
                  "\t-c <number>\tCops count\n"
                  "\t-K <number>\tGrid regenerations count\n"
                  "\t-M <number>\tSimulations count per regeneration\n"
                  "\t-s\t\tShouting between drunkards\n";
    while((res = getopt(argc, argv, "d:N:c:K:M:h")) != -1)
    {
        switch(res)
        {
            case 'd': dim = atoi(optarg); break;
            case 'N': N = atoi(optarg); break;
            case 'c': cops = atoi(optarg); break;
            case 'K': K = atoi(optarg); break;
            case 'M': M = atoi(optarg); break;
            case 'h': printf("%s", usage); return 0;
        }
    }
    grid_size = pow(N, dim);
    grid = malloc(grid_size * sizeof(int));
    memset(grid, 0, grid_size);
    stats = calloc(K, sizeof(int));
    for(i = 0; i < K; ++i)
    {
        regenerate(grid, grid_size, cops);

#pragma omp parallel shared(grid, stats) private(j, tid, threads_num, state, seed, go_flag, count) firstprivate(i, grid_size, K, M, N, dim)
        {
            tid = omp_get_thread_num();
            threads_num = omp_get_num_threads();
            seed = time(((void *)0)) + (unsigned int) omp_get_thread_num();
            memcpy(state, &seed, sizeof(seed));
            int *drunkards = calloc(M/threads_num, sizeof(int));
            go_flag = M/threads_num;
            while(go_flag)
            {
                count = 0;
                for(j = 0; j < M/threads_num; ++j)
                {
                    if(drunkards[j] != -1)
                    {
                        ++ count;
                        drunkards[j] = step(drunkards[j], dim, grid_size, state, N);
                        if(grid[drunkards[j]] == 1)
                        {
                            drunkards[j] = -1;
                        }
                        if(drunkards[j] == grid_size)
                        {

#pragma omp atomic
                            stats[i] += 1;
                            drunkards[j] = -1;
                        }
                    }
                }
                if(count == 0) go_flag = 0;
            }
            free(drunkards);
        }
    }
    double avg; double stddev;
    for(i = 0; i < K; ++i) avg += (double) stats[i] / M;
    avg /= K;
    for(i = 0; i < K; ++i) stddev += ((double)stats[i]/M - avg) * ((double)stats[i]/M - avg);
    stddev = sqrt(stddev / K);
    double end = omp_get_wtime();
    printf("%d %f %f %f %d %d\n", cops, avg, stddev, end - start, M, K);
    free(grid);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: start double

variable_types: K int

variable_types: M int

variable_types: i int

variable_types: j int

variable_types:
stats
int *

variable_types: tid int

variable_types:
state
unsigned short [3]

variable_types:
seed
unsigned int

variable_types: res int

variable_types:
usage
char *

variable_types:
drunkards
int *

variable_types: avg double

variable_types: stddev double

variable_types: end double


repo: sashalevin/uvirtio

decompiled_code:
__int64 __fastcall vring_init(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
__int64 v4; // rdx
__int64 result; // rax

*(_DWORD *)a1 = a2;
*(_QWORD *)(a1 + 8) = a3;
v4 = 16LL * a2 + a3;
*(_QWORD *)(a1 + 16) = v4;
result = a4 + v4 + 2LL * a2 + 4 + 1;
*(_QWORD *)(a1 + 24) = result & -a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2, __int64 a3, __int64 a4)
{
__int64 v4; // rdx
__int64 result; // rax

*(_DWORD *)a1 = a2;
*(_QWORD *)(a1 + 8) = a3;
v4 = 16LL * a2 + a3;
*(_QWORD *)(a1 + 16) = v4;
result = a4 + v4 + 2LL * a2 + 4 + 1;
*(_QWORD *)(a1 + 24) = result & -a4;
return result;
}

original_code:
static __inline__ void vring_init(struct vring *vr, unsigned int num, void *p,
         unsigned long align)
{
 vr->num = num;
 vr->desc = p;
 vr->avail = p + num*sizeof(struct vring_desc);
 vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16)
  + align-1) & ~(align - 1));
}

canonical_original_code:
static __inline__ void vring_init(struct vring *vr, unsigned int num, void *p, unsigned long align)
{
 vr->num = num;
 vr->desc = p;
 vr->avail = p + num*sizeof(struct vring_desc);
 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(__virtio16)
  + align-1) & ~(align - 1));
}

variable_types: struct vring { unsigned int num; struct vring_desc { unsigned long long addr; unsigned int len; unsigned short flags; unsigned short next; } *desc; struct vring_avail { unsigned short flags; unsigned short idx; unsigned short ring[]; } *avail; struct vring_used { unsigned short flags; unsigned short idx; struct vring_used_elem { unsigned int id; unsigned int len; } ring[]; } *used; } *

variable_types:
num
unsigned int

variable_types:
p
void *

variable_types:
align
unsigned long

user_defined_types: struct vring { unsigned int num; struct vring_desc *desc; struct vring_avail *avail; struct vring_used *used; }

user_defined_types: struct vring_used { unsigned short flags; unsigned short idx; struct vring_used_elem ring[]; }


decompiled_code:
int test1()
{
int v0; // ebp
int v1; // ebx
int result; // eax
double v3; // xmm0_8

puts("simulator test 1");
puts("starting server");
system("gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./server ");
v0 = 2;
do
{
printf("delay");
v1 = 101;
do
{
putchar(46);
--v1;
}
while ( v1 );
putchar(10);
puts("starting client");
result = system("gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./client 127.0.0.1 ");
v3 = 1000000.0;
do
v3 = v3 - 1.0;
while ( v3 >= 0.0 );
--v0;
}
while ( v0 );
return result;
}

canonical_decompiled_code:
int func7()
{
int v0; // ebp
int v1; // ebx
int result; // eax
double v3; // xmm0_8

puts("simulator test 1");
puts("starting server");
system("gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./server ");
v0 = 2;
do
{
printf("delay");
v1 = 101;
do
{
putchar(46);
--v1;
}
while ( v1 );
putchar(10);
puts("starting client");
result = system("gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./client 127.0.0.1 ");
v3 = 1000000.0;
do
v3 = v3 - 1.0;
while ( v3 >= 0.0 );
--v0;
}
while ( v0 );
return result;
}

original_code:
void test1() {
    printf("simulator test 1\n");
    double delay = 100, wait = 1000000;
    char *client_str, *server_str, *server_str2;
    int count = 2, buf, status;
    pid_t pid;
    client_str = "gnome-terminal  --window-with-profile=NAMEOFTHEPROFILE -x ./client  127.0.0.1 ";
    server_str = "gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./server ";
    server_str2 = "./server";
    printf("starting server\n");
    system(server_str);
    while (count > 0) {
        count--;
        printf("delay");
        while (delay >= 0) {
            printf(".");
            delay--;
        }
        delay = 100;
        printf("\n");
         printf("starting client\n");
        system(client_str);
        while(wait>=0){
            wait--;
        }
        wait = 1000000;
    }
}

canonical_original_code:
void test1() {
    printf("simulator test 1\n");
    double delay = 100; double wait = 1000000;
    char *client_str; char *server_str; char *server_str2;
    int count = 2; int buf; int status;
    int pid;
    client_str = "gnome-terminal  --window-with-profile=NAMEOFTHEPROFILE -x ./client  127.0.0.1 ";
    server_str = "gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -x ./server ";
    server_str2 = "./server";
    printf("starting server\n");
    system(server_str);
    while (count > 0) {
        count--;
        printf("delay");
        while (delay >= 0) {
            printf(".");
            delay--;
        }
        delay = 100;
        printf("\n");
         printf("starting client\n");
        system(client_str);
        while(wait>=0){
            wait--;
        }
        wait = 1000000;
    }
}

variable_types: delay double

variable_types: wait double

variable_types: count int

variable_types: buf int

variable_types: status int

variable_types: pid int


decompiled_code:
int test2()
{
puts("simulator test 2");
return printf("%%TEST_FAILED%% time=0 testname=test2 (simulator) message=error message sample\n");
}

canonical_decompiled_code:
int func8()
{
puts("simulator test 2");
return printf("%%TEST_FAILED%% time=0 testname=test2 (simulator) message=error message sample\n");
}

original_code:
void test2() {
    printf("simulator test 2\n");
    printf("%%TEST_FAILED%% time=0 testname=test2 (simulator) message=error message sample\n");
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printf("%%SUITE_STARTING%% simulator\n");
printf("%%SUITE_STARTED%%\n");
printf("%%TEST_STARTED%% test1 (simulator)\n");
test1();
printf("%%TEST_FINISHED%% time=0 test1 (simulator) \n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printf("%%SUITE_STARTING%% simulator\n");
printf("%%SUITE_STARTED%%\n");
printf("%%TEST_STARTED%% test1 (simulator)\n");
func7();
printf("%%TEST_FINISHED%% time=0 test1 (simulator) \n");
return 0;
}

original_code:
int main(int argc, char** argv) {
    printf("%%SUITE_STARTING%% simulator\n");
    printf("%%SUITE_STARTED%%\n");
    printf("%%TEST_STARTED%% test1 (simulator)\n");
    test1();
    printf("%%TEST_FINISHED%% time=0 test1 (simulator) \n");
    return (0);
}

canonical_original_code:
int main(int argc, char **argv) {
    printf("%%SUITE_STARTING%% simulator\n");
    printf("%%SUITE_STARTED%%\n");
    printf("%%TEST_STARTED%% test1 (simulator)\n");
    test1();
    printf("%%TEST_FINISHED%% time=0 test1 (simulator) \n");
    return (0);
}

variable_types: argc int

variable_types:
argv
char **


repo: sathesh/hash-library

decompiled_code:
__int64 __fastcall uint_key_cmp(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)-(*a1 != *a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)-(*a1 != *a2);
}

original_code:
int uint_key_cmp(void *v1, void *v2)
{
  unsigned int *p1 = (unsigned int *)v1;
  unsigned int *p2 = (unsigned int *)v2;
  if (*p1 == *p2)
    return 0;
  else
    return -1;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // r13
__int64 v4; // r12
int v5; // ebx
unsigned int *v6; // rax
unsigned int *v7; // rbx
_DWORD *v8; // rax
unsigned int *v9; // rax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v12; // [rsp+10h] [rbp-38h] BYREF
unsigned int v13; // [rsp+1Ch] [rbp-2Ch]

v3 = (void *)hash_init(10LL, 0LL, uint_key_cmp);
while ( 1 )
{
v13 = 777;
puts("\n\n\n0. quit\n1. Insert \n2. Delete \n3. Lookup \n4. Print Distribution");
printf("\tEnter ur option: ");
__isoc99_scanf();
getchar();
if ( !v13 )
return hash_deinit(v3);
if ( v13 == 3 )
{
printf("\nLook up key: ");
__isoc99_scanf();
v9 = (unsigned int *)hash_lookup(v3, &v12);
if ( v9 )
printf("data: %u", *v9);
else
LABEL_18:
puts("key not found");
}
else if ( v13 > 3 )
{
if ( v13 == 4 )
{
v11 = 0;
hash_distribution(v3, &v12, &v11);
if ( v11 > 0 )
{
v4 = 0LL;
do
{
printf("[%u]:", (unsigned int)v4);
if ( *(int *)(v12 + 4 * v4) > 0 )
{
v5 = 0;
do
{
putchar(46);
++v5;
}
while ( *(_DWORD *)(v12 + 4 * v4) > v5 );
}
putchar(10);
++v4;
}
while ( v11 > (int)v4 );
}
}
}
else if ( v13 == 1 )
{
v8 = create_data_box();
if ( (unsigned int)hash_insert(v3, v8, v8) == 1 )
puts("Insert done");
else
puts("Insert failed");
}
else
{
printf("\nDelete key: ");
__isoc99_scanf();
v6 = (unsigned int *)hash_delete(v3, &v12);
v7 = v6;
if ( !v6 )
goto LABEL_18;
printf("data: %u", *v6);
free(v7);
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // r13
__int64 v4; // r12
int v5; // ebx
unsigned int *v6; // rax
unsigned int *v7; // rbx
_DWORD *v8; // rax
unsigned int *v9; // rax
int v11; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v12; // [rsp+10h] [rbp-38h] BYREF
unsigned int v13; // [rsp+1Ch] [rbp-2Ch]

v3 = (void *)hash_init(10LL, 0LL, uint_key_cmp);
while ( 1 )
{
v13 = 777;
puts("\n\n\n0. quit\n1. Insert \n2. Delete \n3. Lookup \n4. Print Distribution");
printf("\tEnter ur option: ");
__isoc99_scanf();
getchar();
if ( !v13 )
return func14(v3);
if ( v13 == 3 )
{
printf("\nLook up key: ");
__isoc99_scanf();
v9 = (unsigned int *)hash_lookup(v3, &v12);
if ( v9 )
printf("data: %u", *v9);
else
LABEL_18:
puts("key not found");
}
else if ( v13 > 3 )
{
if ( v13 == 4 )
{
v11 = 0;
func15(v3, &v12, &v11);
if ( v11 > 0 )
{
v4 = 0LL;
do
{
printf("[%u]:", (unsigned int)v4);
if ( *(int *)(v12 + 4 * v4) > 0 )
{
v5 = 0;
do
{
putchar(46);
++v5;
}
while ( *(_DWORD *)(v12 + 4 * v4) > v5 );
}
putchar(10);
++v4;
}
while ( v11 > (int)v4 );
}
}
}
else if ( v13 == 1 )
{
v8 = func8();
if ( (unsigned int)func16(v3, v8, v8) == 1 )
puts("Insert done");
else
puts("Insert failed");
}
else
{
printf("\nDelete key: ");
__isoc99_scanf();
v6 = (unsigned int *)func17(v3, &v12);
v7 = v6;
if ( !v6 )
goto LABEL_18;
printf("data: %u", *v6);
free(v7);
}
}
}

original_code:
void main()
{
  unsigned int user_input;
  hash_handle_t hash_handle;
   hash_handle = hash_init(10,
                           ((void *)0),
                           uint_key_cmp);
  do
  {
    user_input = 777;
    printf("\n\n\n0. quit\n1. Insert \n2. Delete \n" "3. Lookup \n4. Print Distribution\n");
    printf("\tEnter ur option: ");
    scanf("%u", &user_input);
    getchar();
    if (user_input == 0)
      break;
    switch(user_input)
    {
      case 1:
        {
          data_box_t *data_box = create_data_box();
          if (1 == hash_insert(hash_handle, &data_box->data, data_box))
          {
            printf("Insert done\n");
          }
          else
          {
            printf("Insert failed\n");
          }
        }
        break;
      case 2:
        {
          data_box_t *data_box;
          unsigned int key;
          printf("\nDelete key: ");
          scanf("%u", &key);
          data_box = hash_delete(hash_handle, &key);
          if (data_box)
          {
            printf ("data: %u", data_box->data);
            free (data_box);
          }
          else
          {
            printf ("key not found\n");
          }
        }
        break;
      case 3:
        {
          data_box_t *data_box;
          unsigned int key;
          printf("\nLook up key: ");
          scanf("%u", &key);
          data_box = hash_lookup(hash_handle, &key);
          if (data_box)
            printf ("data: %u", data_box->data);
          else
            printf ("key not found\n");
        }
        break;
      case 4:
        {
          int bkt_size= 0;
          int *result;
          int i,j;
          hash_distribution(hash_handle, &result, &bkt_size);
          for (i=0; i< bkt_size; i++)
          {
              printf("[%u]:", i);
              for (j=0; j<result[i]; j++)
                printf(".");
              printf("\n");
          }
        }
    }
  }while(1);
  hash_deinit(hash_handle);
}

canonical_original_code:
void main()
{
  unsigned int user_input;
  void *hash_handle;
   hash_handle = hash_init(10,
                           ((void *)0),
                           uint_key_cmp);
  do
  {
    user_input = 777;
    printf("\n\n\n0. quit\n1. Insert \n2. Delete \n" "3. Lookup \n4. Print Distribution\n");
    printf("\tEnter ur option: ");
    scanf("%u", &user_input);
    getchar();
    if (user_input == 0)
      break;
    switch(user_input)
    {
      case 1:
        {
          struct data_box *data_box = create_data_box();
          if (1 == hash_insert(hash_handle, &data_box->data, data_box))
          {
            printf("Insert done\n");
          }
          else
          {
            printf("Insert failed\n");
          }
        }
        break;
      case 2:
        {
          struct data_box *data_box;
          unsigned int key;
          printf("\nDelete key: ");
          scanf("%u", &key);
          data_box = hash_delete(hash_handle, &key);
          if (data_box)
          {
            printf ("data: %u", data_box->data);
            free (data_box);
          }
          else
          {
            printf ("key not found\n");
          }
        }
        break;
      case 3:
        {
          struct data_box *data_box;
          unsigned int key;
          printf("\nLook up key: ");
          scanf("%u", &key);
          data_box = hash_lookup(hash_handle, &key);
          if (data_box)
            printf ("data: %u", data_box->data);
          else
            printf ("key not found\n");
        }
        break;
      case 4:
        {
          int bkt_size = 0;
          int *result;
          int i; int j;
          hash_distribution(hash_handle, &result, &bkt_size);
          for (i=0; i< bkt_size; i++)
          {
              printf("[%u]:", i);
              for (j=0; j<result[i]; j++)
                printf(".");
              printf("\n");
          }
        }
    }
  }while(1);
  hash_deinit(hash_handle);
}

variable_types: struct data_box { unsigned int data; } *

variable_types:
key
unsigned int

variable_types:
result
int *

variable_types: i int

variable_types: j int


decompiled_code:
_BOOL8 __fastcall cmp_char(_BYTE *a1, _BYTE *a2)
{
return *a1 != *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_BYTE *a1, _BYTE *a2)
{
return *a1 != *a2;
}

original_code:
int cmp_char(char* a, char* b)
{
  if (*a == *b)
    return 0;
  else
    return 1;
}

canonical_original_code:
int cmp_char(char *a, char *b)
{
  if (*a == *b)
    return 0;
  else
    return 1;
}

variable_types:
a
char *

variable_types:
b
char *


decompiled_code:
__int64 __fastcall hash_function_int(unsigned int a1, _DWORD *a2)
{
return *a2 % a1;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, _DWORD *a2)
{
return *a2 % a1;
}

original_code:
int hash_function_int(unsigned int bkt_size, void *key_p)
{
  return (*(unsigned int *)key_p%bkt_size);
}


decompiled_code:
__int64 __fastcall INSERT_NODE(int *a1, __int64 a2, __int64 a3)
{
int v4; // ebp
__int64 v5; // r14
_QWORD *v6; // rax
__int64 result; // rax

v4 = *a1;
if ( *a1 )
{
v5 = *((_QWORD *)a1 + 1);
if ( !v5 )
LABEL_3:
__assert_fail(
"(bkt->length == 0 && bkt->head == NULL) || (bkt->length != 0 && bkt->head != NULL)",
"../hashlib/hash.c",
0xD9u,
"INSERT_NODE");
v6 = malloc(0x18uLL);
if ( v6 )
{
v6[1] = a2;
v6[2] = a3;
*v6 = v5;
LABEL_7:
*((_QWORD *)a1 + 1) = v6;
*a1 = v4 + 1;
return 1LL;
}
result = 0LL;
}
else
{
if ( *((_QWORD *)a1 + 1) )
goto LABEL_3;
v6 = malloc(0x18uLL);
if ( v6 )
{
*v6 = 0LL;
v6[1] = a2;
v6[2] = a3;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int *a1, __int64 a2, __int64 a3)
{
int v4; // ebp
__int64 v5; // r14
_QWORD *v6; // rax
__int64 result; // rax

v4 = *a1;
if ( *a1 )
{
v5 = *((_QWORD *)a1 + 1);
if ( !v5 )
LABEL_3:
__assert_fail(
"(bkt->length == 0 && bkt->head == NULL) || (bkt->length != 0 && bkt->head != NULL)",
"../hashlib/hash.c",
0xD9u,
"INSERT_NODE");
v6 = malloc(0x18uLL);
if ( v6 )
{
v6[1] = a2;
v6[2] = a3;
*v6 = v5;
LABEL_7:
*((_QWORD *)a1 + 1) = v6;
*a1 = v4 + 1;
return 1LL;
}
result = 0LL;
}
else
{
if ( *((_QWORD *)a1 + 1) )
goto LABEL_3;
v6 = malloc(0x18uLL);
if ( v6 )
{
*v6 = 0LL;
v6[1] = a2;
v6[2] = a3;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

original_code:
static int INSERT_NODE(hash_bkt_t *bkt, void *key, void *data)
{
  list_node_t *tmp;
  ((void) sizeof (((bkt->length == 0 && bkt->head == ((void *)0)) || (bkt->length != 0 && bkt->head != ((void *)0))) ? 1 : 0), __extension__ ({ if ((bkt->length == 0 && bkt->head == ((void *)0)) || (bkt->length != 0 && bkt->head != ((void *)0))) ; else __assert_fail ("(bkt->length == 0 && bkt->head == NULL) || (bkt->length != 0 && bkt->head != NULL)", "../hashlib/hash.c", 217, __extension__ __PRETTY_FUNCTION__); }));
  tmp = malloc(sizeof(list_node_t));
  if (!tmp)
    return 0;
  tmp->next = ((void *)0);
  tmp->key = key;
  tmp->data = data;
  if (!bkt->head)
  {
    tmp->next = ((void *)0);
  }
  else
  {
    tmp->next = bkt->head;
  }
  bkt->head = tmp;
  bkt->length++;
  return 1;
}

canonical_original_code:
static int INSERT_NODE(struct hash_bkt_struct *bkt, void *key, void *data)
{
  struct list_node *tmp;
  ((void) sizeof (((bkt->length == 0 && bkt->head == ((void *)0)) || (bkt->length != 0 && bkt->head != ((void *)0))) ? 1 : 0), __extension__ ({ if ((bkt->length == 0 && bkt->head == ((void *)0)) || (bkt->length != 0 && bkt->head != ((void *)0))) ; else __assert_fail ("(bkt->length == 0 && bkt->head == NULL) || (bkt->length != 0 && bkt->head != NULL)", "../hashlib/hash.c", 217, __extension__ __PRETTY_FUNCTION__); }));
  tmp = malloc(sizeof(list_node_t));
  if (!tmp)
    return 0;
  tmp->next = ((void *)0);
  tmp->key = key;
  tmp->data = data;
  if (!bkt->head)
  {
    tmp->next = ((void *)0);
  }
  else
  {
    tmp->next = bkt->head;
  }
  bkt->head = tmp;
  bkt->length++;
  return 1;
}

variable_types: struct hash_bkt_struct { unsigned int length; struct list_node { struct list_node *next; void *key; void *data; } *head; } *

variable_types:
key
void *

variable_types:
data
void *

user_defined_types: struct hash_bkt_struct { unsigned int length; struct list_node *head; }


decompiled_code:
__int64 __fastcall hashpjw_string(unsigned int a1, char *a2)
{
char v2; // dl
unsigned int v3; // eax

v2 = *a2;
if ( *a2 )
{
v3 = 0;
do
{
v3 = v2 + 16 * v3;
if ( (v3 & 0xF0000000) != 0 )
v3 = ((int)(v3 & 0xF0000000) >> 24) ^ v3 & 0xFFFFFFF;
v2 = *++a2;
}
while ( *a2 );
}
else
{
v3 = 0;
}
return v3 % a1;
}

original_code:
int hashpjw_string(unsigned int bkt_size, const void *key)
{
  const char *ptr;
  int val;
  val = 0;
  ptr = key;
  while (*ptr != '\0') {
    int tmp;
    val = (val << 4) + (*ptr);
    if (tmp = (val & 0xf0000000)) {
      val = val ^ (tmp >> 24);
      val = val ^ tmp;
    }
    ptr++;
  }
  return val % bkt_size;
}

canonical_original_code:
int hashpjw_string(unsigned int bkt_size, void *key)
{
  char *ptr;
  int val;
  val = 0;
  ptr = key;
  while (*ptr != '\0') {
    int tmp;
    val = (val << 4) + (*ptr);
    if (tmp = (val & 0xf0000000)) {
      val = val ^ (tmp >> 24);
      val = val ^ tmp;
    }
    ptr++;
  }
  return val % bkt_size;
}

variable_types:
ptr
char *

variable_types: val int

variable_types: tmp int


decompiled_code:
void __fastcall hash_deinit(void *ptr)
{
__int64 v2; // rbx
__int64 v3; // rdi

if ( *(_DWORD *)ptr )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 3) + 8 * v2);
if ( v3 )
{
hash_bkt_deinit(v3);
free(*(void **)(*((_QWORD *)ptr + 3) + 8 * v2));
}
++v2;
}
while ( *(_DWORD *)ptr > (unsigned int)v2 );
}
free(*((void **)ptr + 3));
free(ptr);
}

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

if ( *(_DWORD *)ptr )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 3) + 8 * v2);
if ( v3 )
{
func11(v3);
free(*(void **)(*((_QWORD *)ptr + 3) + 8 * v2));
}
++v2;
}
while ( *(_DWORD *)ptr > (unsigned int)v2 );
}
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
int hash_deinit(hash_handle_t handle)
{
  hash_root_t *root = (hash_root_t*)handle;
  int i;
  for (i=0; i<root->bkt_size; i++)
  {
    if (root->hash_bkt[i])
    {
      hash_bkt_deinit(root->hash_bkt[i]);
      free(root->hash_bkt[i]);
    }
  }
  free(root->hash_bkt);
  free(root);
}

canonical_original_code:
int hash_deinit(void *handle)
{
  struct hash_root *root = (hash_root_t*)handle;
  int i;
  for (i=0; i<root->bkt_size; i++)
  {
    if (root->hash_bkt[i])
    {
      hash_bkt_deinit(root->hash_bkt[i]);
      free(root->hash_bkt[i]);
    }
  }
  free(root->hash_bkt);
  free(root);
}

variable_types:
handle
void *

variable_types: struct hash_root { unsigned int bkt_size; int (*hash_fn)(unsigned int bkt_size, void *); int (*key_cmp_fn)(void *, void *); struct hash_bkt_struct { unsigned int length; struct list_node { struct list_node *next; void *key; void *data; } *head; } **hash_bkt; unsigned int total_elements; } *

user_defined_types: struct hash_root { unsigned int bkt_size; int (*hash_fn)(unsigned int bkt_size, void *); int (*key_cmp_fn)(void *, void *); struct hash_bkt_struct **hash_bkt; unsigned int total_elements; }


original_code:
int hash_insert(hash_handle_t handle, void *key, void* data)
{
  hash_root_t *root = (hash_root_t*)handle;
  unsigned int hash_index = root->hash_fn(root->bkt_size, key);
  int result;
  if (!root->hash_bkt[hash_index])
  {
     root->hash_bkt[hash_index] = malloc(sizeof(hash_bkt_t));
     if (!root->hash_bkt[hash_index])
       return 0;
     root->hash_bkt[hash_index]->length = 0;
     root->hash_bkt[hash_index]->head = ((void *)0);
  }
  result = INSERT_NODE(root->hash_bkt[hash_index], key, data);
  if (result == 1)
    root->total_elements++;
  return result;
}

canonical_original_code:
int hash_insert(void *handle, void *key, void *data)
{
  struct hash_root *root = (hash_root_t*)handle;
  unsigned int hash_index = root->hash_fn(root->bkt_size, key);
  int result;
  if (!root->hash_bkt[hash_index])
  {
     root->hash_bkt[hash_index] = malloc(sizeof(hash_bkt_t));
     if (!root->hash_bkt[hash_index])
       return 0;
     root->hash_bkt[hash_index]->length = 0;
     root->hash_bkt[hash_index]->head = ((void *)0);
  }
  result = INSERT_NODE(root->hash_bkt[hash_index], key, data);
  if (result == 1)
    root->total_elements++;
  return result;
}

variable_types: result int

user_defined_types: struct list_node { struct list_node *next; void *key; void *data; }


original_code:
void* hash_delete(hash_handle_t handle, void *key)
{
  hash_root_t *root = (hash_root_t*)handle;
  unsigned int hash_index = root->hash_fn(root->bkt_size, key);
  list_node_t *node_to_be_del = ((void *)0);
  hash_bkt_t *bkt = root->hash_bkt[hash_index];
  if (bkt == ((void *)0) || bkt->head == ((void *)0))
    return ((void *)0);
  if (root->key_cmp_fn(key, bkt->head->key) ==0)
  {
    node_to_be_del = bkt->head;
    bkt->head = bkt->head->next;
  }
  else
  {
    list_node_t *node = bkt->head;
    while (node->next)
    {
      if (root->key_cmp_fn(key, node->next->key) ==0)
      {
        list_node_t *tmp = node->next;
        node->next = tmp->next;
        node_to_be_del = tmp;
        break;
      }
      node = node->next;
    }
  }
  if (node_to_be_del)
  {
    void *data = node_to_be_del->data;
    free(node_to_be_del);
    bkt->length--;
    return data;
  }
  return ((void *)0);
}

canonical_original_code:
void *hash_delete(void *handle, void *key)
{
  struct hash_root *root = (hash_root_t*)handle;
  unsigned int hash_index = root->hash_fn(root->bkt_size, key);
  struct list_node *node_to_be_del = ((void *)0);
  struct hash_bkt_struct *bkt = root->hash_bkt[hash_index];
  if (bkt == ((void *)0) || bkt->head == ((void *)0))
    return ((void *)0);
  if (root->key_cmp_fn(key, bkt->head->key) ==0)
  {
    node_to_be_del = bkt->head;
    bkt->head = bkt->head->next;
  }
  else
  {
    struct list_node *node = bkt->head;
    while (node->next)
    {
      if (root->key_cmp_fn(key, node->next->key) ==0)
      {
        struct list_node *tmp = node->next;
        node->next = tmp->next;
        node_to_be_del = tmp;
        break;
      }
      node = node->next;
    }
  }
  if (node_to_be_del)
  {
    void *data = node_to_be_del->data;
    free(node_to_be_del);
    bkt->length--;
    return data;
  }
  return ((void *)0);
}

variable_types: struct list_node { struct list_node *next; void *key; void *data; } *

return_type: void *


decompiled_code:
_BOOL8 __fastcall map_generate(__int64 a1)
{
_DWORD *v1; // rbp
_DWORD *v2; // rbx
_BOOL8 result; // rax

v1 = (_DWORD *)(a1 + 8188);
do
{
v2 = v1 - 1022;
do
{
result = (float)((float)rand() * 4.6566129e-10) <= 0.05;
*v2++ = result;
}
while ( v2 != v1 );
v1 += 1024;
}
while ( v1 != (_DWORD *)(a1 + 4194300) );
return result;
}

variable_types: data int


decompiled_code:
int __fastcall map_print(__int64 a1)
{
unsigned int *v1; // rbp
unsigned int *v2; // rbx
int result; // eax

if ( a1 )
{
v1 = (unsigned int *)(a1 + 4198396);
do
{
v2 = v1 - 0x100000;
do
{
printf("%d", *v2);
v2 += 1024;
}
while ( v2 != v1 );
result = putchar(10);
--v1;
}
while ( v1 != (unsigned int *)(a1 + 4194300) );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
unsigned int *v1; // rbp
unsigned int *v2; // rbx
int result; // eax

if ( a1 )
{
v1 = (unsigned int *)(a1 + 4198396);
do
{
v2 = v1 - 0x100000;
do
{
printf("%d", *v2);
v2 += 1024;
}
while ( v2 != v1 );
result = putchar(10);
--v1;
}
while ( v1 != (unsigned int *)(a1 + 4194300) );
}
return result;
}


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

if ( a2 > 0x3FF || a3 > 0x3FF )
result = 0LL;
else
result = *(unsigned int *)(a1 + 4 * ((int)a3 + ((__int64)(int)a2 << 10)));
return result;
}

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

if ( a2 > 0x3FF || a3 > 0x3FF )
result = 0LL;
else
result = *(unsigned int *)(a1 + 4 * ((int)a3 + ((__int64)(int)a2 << 10)));
return result;
}

original_code:
int map_is_block(map_t* map, int x, int y)
{
  if(x < 0 || x >= 1024 || y < 0 || y >= 1024)
    return 0;
  return (*map)[x][y];
}

variable_types: x int

variable_types: y int


original_code:
static int raycaster_cast_xplanes(const vec2_t* origin,
       const vec2_t* ray,
       vec2_t* intersection)
{
  unsigned char negative;
  vec2_t pt;
  vec2_t starter_offset;
  vec2_t offset;
  vec2_type x_off;
  int intersected;
  intersected = 0;
  if(ray->x < 0.0f)
    negative = 1;
  else
    negative = 0;
  x_off = ((int)origin->x + !negative) - origin->x;
  if(negative)
    x_off -= 0.0001f;
  starter_offset = *ray;
  offset = *ray;
  vec2_scale_x_to(&starter_offset, x_off);
  vec2_scale_x_to(&offset, negative ? -1.0 : 1.0);
  pt = vec2_add(&starter_offset, origin);
  while(!intersected && pt.x >= 0.0 && pt.x < 1024) {
    if(map_is_block(map_get(), (int)pt.x, (int)pt.y)) {
      *intersection = pt;
      intersected = 1;
    }
    pt = vec2_add(&offset, &pt);
  }
  return intersected;
}

canonical_original_code:
static int raycaster_cast_xplanes(struct vec2 *origin, struct vec2 *ray, struct vec2 *intersection)
{
  unsigned char negative;
  struct vec2 pt;
  struct vec2 starter_offset;
  struct vec2 offset;
  float x_off;
  int intersected;
  intersected = 0;
  if(ray->x < 0.0f)
    negative = 1;
  else
    negative = 0;
  x_off = ((int)origin->x + !negative) - origin->x;
  if(negative)
    x_off -= 0.0001f;
  starter_offset = *ray;
  offset = *ray;
  vec2_scale_x_to(&starter_offset, x_off);
  vec2_scale_x_to(&offset, negative ? -1.0 : 1.0);
  pt = vec2_add(&starter_offset, origin);
  while(!intersected && pt.x >= 0.0 && pt.x < 1024) {
    if(map_is_block(map_get(), (int)pt.x, (int)pt.y)) {
      *intersection = pt;
      intersected = 1;
    }
    pt = vec2_add(&offset, &pt);
  }
  return intersected;
}

variable_types:
origin
struct vec2 { float x; float y; } *

variable_types:
ray
struct vec2 *

variable_types:
intersection
struct vec2 *

variable_types:
negative
unsigned char

variable_types:
pt
struct vec2 { float x; float y; }

variable_types:
offset
struct vec2 { float x; float y; }

variable_types: intersected int


original_code:
static int raycaster_cast_yplanes(const vec2_t* origin,
       const vec2_t* ray,
       vec2_t* intersection)
{
  unsigned char negative;
  vec2_t pt;
  vec2_t starter_offset;
  vec2_t offset;
  vec2_type y_off;
  int intersected;
  intersected = 0;
  if(ray->y < 0.0f)
    negative = 1;
  else
    negative = 0;
  y_off = ((int)origin->y + !negative) - origin->y;
  if(negative)
    y_off -= 0.0001f;
  starter_offset = *ray;
  offset = *ray;
  vec2_scale_y_to(&starter_offset, y_off);
  vec2_scale_y_to(&offset, negative ? -1.0 : 1.0);
  pt = vec2_add(&starter_offset, origin);
  while(!intersected && pt.y >= 0.0 && pt.y < 1024) {
    if(map_is_block(map_get(), (int)pt.x, (int)pt.y)) {
      *intersection = pt;
      intersected = 1;
    }
    pt = vec2_add(&offset, &pt);
  }
  return intersected;
}

canonical_original_code:
static int raycaster_cast_yplanes(struct vec2 *origin, struct vec2 *ray, struct vec2 *intersection)
{
  unsigned char negative;
  struct vec2 pt;
  struct vec2 starter_offset;
  struct vec2 offset;
  float y_off;
  int intersected;
  intersected = 0;
  if(ray->y < 0.0f)
    negative = 1;
  else
    negative = 0;
  y_off = ((int)origin->y + !negative) - origin->y;
  if(negative)
    y_off -= 0.0001f;
  starter_offset = *ray;
  offset = *ray;
  vec2_scale_y_to(&starter_offset, y_off);
  vec2_scale_y_to(&offset, negative ? -1.0 : 1.0);
  pt = vec2_add(&starter_offset, origin);
  while(!intersected && pt.y >= 0.0 && pt.y < 1024) {
    if(map_is_block(map_get(), (int)pt.x, (int)pt.y)) {
      *intersection = pt;
      intersected = 1;
    }
    pt = vec2_add(&offset, &pt);
  }
  return intersected;
}


decompiled_code:
float __fastcall vec2_dist_from_cam_2(__int64 a1, __int64 a2)
{
float v2; // xmm0_4
double v4; // [rsp+0h] [rbp-18h] BYREF
double v5[2]; // [rsp+8h] [rbp-10h] BYREF

v5[0] = vec2_negate(a2);
v4 = vec2_add(a1, v5);
v2 = vec2_dot(&v4, &v4);
return v2 * v2;
}

canonical_decompiled_code:
float __fastcall func2(__int64 a1, __int64 a2)
{
float v2; // xmm0_4
double v4; // [rsp+0h] [rbp-18h] BYREF
double v5[2]; // [rsp+8h] [rbp-10h] BYREF

v5[0] = vec2_negate(a2);
v4 = vec2_add(a1, v5);
v2 = vec2_dot(&v4, &v4);
return v2 * v2;
}

original_code:
static vec2_type vec2_dist_from_cam_2(const vec2_t* test, const vec2_t* cam)
{
  vec2_t neg_cam;
  vec2_t heading;
  vec2_type dot;
  neg_cam = vec2_negate(cam);
  heading = vec2_add(test, &neg_cam);
  dot = vec2_dot(&heading, &heading);
  return pow(dot, 2.0);
}

canonical_original_code:
static float vec2_dist_from_cam_2(struct vec2 *test, struct vec2 *cam)
{
  struct vec2 neg_cam;
  struct vec2 heading;
  float dot;
  neg_cam = vec2_negate(cam);
  heading = vec2_add(test, &neg_cam);
  dot = vec2_dot(&heading, &heading);
  return pow(dot, 2.0);
}

variable_types:
test
struct vec2 { float x; float y; } *

variable_types:
cam
struct vec2 *

variable_types:
heading
struct vec2 { float x; float y; }

variable_types: dot float


decompiled_code:
__int64 __fastcall raycaster_cast(float *a1, float *a2, _QWORD *a3, __m128i a4)
{
unsigned int v5; // ebp
float v6; // er12
float v7; // xmm0_4
__int64 v9; // [rsp+0h] [rbp-38h] BYREF
__int64 v10[6]; // [rsp+8h] [rbp-30h] BYREF

v5 = raycaster_cast_xplanes(a1, a2, v10);
if ( (unsigned int)raycaster_cast_yplanes((__int64)a1, (__int64)a2, &v9) )
{
*(float *)a4.m128i_i32 = vec2_dist_from_cam_2((__int64)&v9, (__int64)a1);
v6 = COERCE_FLOAT(_mm_cvtsi128_si32(a4));
if ( !v5 )
{
LABEL_10:
if ( v6 < 0.0 )
return 0;
*a3 = v9;
return 1;
}
}
else
{
v6 = *(float *)&LC0;
if ( !v5 )
return v5;
}
v7 = vec2_dist_from_cam_2((__int64)v10, (__int64)a1);
if ( v7 < 0.0 )
goto LABEL_10;
if ( v6 < 0.0 )
{
*a3 = v10[0];
v5 = 1;
}
else
{
if ( fminf(v7, v6) == v7 )
*a3 = v10[0];
else
*a3 = v9;
v5 = 1;
}
return v5;
}

original_code:
int raycaster_cast(const vec2_t* origin, const vec2_t* ray,
     vec2_t* intersection)
{
  vec2_t intersection_x;
  vec2_t intersection_y;
  vec2_type dist_x, dist_y;
  int ret_val;
  int cast_x, cast_y;
  ret_val = 0;
  dist_x = -1.0;
  dist_y = -1.0;
  cast_x = raycaster_cast_xplanes(origin, ray, &intersection_x);
  cast_y = raycaster_cast_yplanes(origin, ray, &intersection_y);
  if(cast_y)
    dist_y = vec2_dist_from_cam_2(&intersection_y, origin);
  if(cast_x)
    dist_x = vec2_dist_from_cam_2(&intersection_x, origin);
  if(dist_x < 0.0 && dist_y < 0.0) {
    ret_val = 0;
    goto raycaster_cleanup_cast_rays;
  }
  if(dist_x < 0.0) {
    intersection[0] = intersection_y;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
  if(dist_y < 0.0) {
    intersection[0] = intersection_x;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
  if(((dist_x) < (dist_y) ? (dist_x) : (dist_y)) == dist_x) {
    intersection[0] = intersection_x;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  } else {
    intersection[0] = intersection_y;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
 raycaster_cleanup_cast_rays:
  return ret_val;
}

canonical_original_code:
int raycaster_cast(struct vec2 *origin, struct vec2 *ray, struct vec2 *intersection)
{
  struct vec2 intersection_x;
  struct vec2 intersection_y;
  float dist_x; float dist_y;
  int ret_val;
  int cast_x; int cast_y;
  ret_val = 0;
  dist_x = -1.0;
  dist_y = -1.0;
  cast_x = raycaster_cast_xplanes(origin, ray, &intersection_x);
  cast_y = raycaster_cast_yplanes(origin, ray, &intersection_y);
  if(cast_y)
    dist_y = vec2_dist_from_cam_2(&intersection_y, origin);
  if(cast_x)
    dist_x = vec2_dist_from_cam_2(&intersection_x, origin);
  if(dist_x < 0.0 && dist_y < 0.0) {
    ret_val = 0;
    goto raycaster_cleanup_cast_rays;
  }
  if(dist_x < 0.0) {
    intersection[0] = intersection_y;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
  if(dist_y < 0.0) {
    intersection[0] = intersection_x;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
  if(((dist_x) < (dist_y) ? (dist_x) : (dist_y)) == dist_x) {
    intersection[0] = intersection_x;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  } else {
    intersection[0] = intersection_y;
    ret_val = 1;
    goto raycaster_cleanup_cast_rays;
  }
 raycaster_cleanup_cast_rays:
  return ret_val;
}


decompiled_code:
void __fastcall raycaster_cast_rays(float *a1, __int64 *a2, _QWORD *a3, __int64 a4, float a5)
{
float v5; // xmm1_4
int v6; // er14
int i; // ebp
__m128i v9; // xmm0
float v10; // [rsp+8h] [rbp-40h]
float v11; // [rsp+Ch] [rbp-3Ch]
__int64 v12[6]; // [rsp+18h] [rbp-30h] BYREF

v10 = a5 * 0.5;
if ( a4 < 0 )
v5 = (float)(a4 & 1 | (unsigned int)((unsigned __int64)a4 >> 1))
+ (float)(a4 & 1 | (unsigned int)((unsigned __int64)a4 >> 1));
else
v5 = (float)(int)a4;
v11 = a5 / v5;
v6 = (int)(float)((float)(v10 + v10) / (float)(a5 / v5));
if ( v6 > 0 )
{
for ( i = 0; i != v6; ++i )
{
v12[0] = *a2;
v9 = 0LL;
*(double *)v9.m128i_i64 = vec2_rotate(v12, (float)((float)i * v11) - v10);
if ( !(unsigned int)raycaster_cast(a1, (float *)v12, a3, v9) )
{
*((_DWORD *)a3 - 2) = LC0;
*((_DWORD *)a3 - 1) = LC0;
}
++a3;
}
}
}

original_code:
void raycaster_cast_rays(const vec2_t* origin,
    const vec2_t* ray,
    vec2_type fov,
    vec2_t* intersections,
    size_t count)
{
  vec2_type lower_bound, upper_bound, d_theta;
  vec2_t bad_ray;
  int upper_ind, counter;
  upper_bound = fov * 0.5;
  lower_bound = -upper_bound;
  d_theta = fov / (vec2_type)count;
  bad_ray.x = -1.0;
  bad_ray.y = -1.0;
  upper_ind = (int)((upper_bound - lower_bound) / d_theta);
  for (counter = 0; counter < upper_ind; ++counter) {
    vec2_t current_ray;
    vec2_type theta;
    theta = counter * d_theta + lower_bound;
    memcpy(&current_ray, ray, sizeof(vec2_t));
    vec2_rotate(&current_ray, theta);
    if(!raycaster_cast(origin, &current_ray, intersections + counter))
      *((intersections) + (counter - 1)) = bad_ray;
  }
}

canonical_original_code:
void raycaster_cast_rays(struct vec2 *origin, struct vec2 *ray, float fov, struct vec2 *intersections, unsigned long count)
{
  float lower_bound; float upper_bound; float d_theta;
  struct vec2 bad_ray;
  int upper_ind; int counter;
  upper_bound = fov * 0.5;
  lower_bound = -upper_bound;
  d_theta = fov / (float)count;
  bad_ray.x = -1.0;
  bad_ray.y = -1.0;
  upper_ind = (int)((upper_bound - lower_bound) / d_theta);
  for (counter = 0; counter < upper_ind; ++counter) {
    struct vec2 current_ray;
    float theta;
    theta = counter * d_theta + lower_bound;
    memcpy(&current_ray, ray, sizeof(vec2_t));
    vec2_rotate(&current_ray, theta);
    if(!raycaster_cast(origin, &current_ray, intersections + counter))
      *((intersections) + (counter - 1)) = bad_ray;
  }
}

variable_types: fov float

variable_types:
intersections
struct vec2 *

variable_types:
count
unsigned long

variable_types: counter int

variable_types: theta float