dbInit

decompiled_code:
__int64 __fastcall dbInit(char *src, int a2, const char *a3, const char *a4)
{
const char *v6; // r15
const char *v7; // r14
const char *v8; // r13
const char *v9; // rax
__int64 v10; // rbp
size_t v11; // rax
const char *v12; // rsi
char *v13; // rax
const char *v14; // rsi
int v16; // er13
int *v17; // rax
unsigned int *v18; // r14
const char *v19; // rax
const char *v20; // r12
unsigned int v21; // eax
const char *v22; // r12
unsigned int v23; // eax
const char *v24; // r12
unsigned int v25; // eax
const char *v26; // r12
unsigned int v27; // eax
const char *v28; // r12
unsigned int v29; // eax
const char *v30; // r12
unsigned int v31; // eax
const char *v32; // r12
unsigned int v33; // eax
const char *v34; // r12
unsigned int v35; // eax
char v36[32]; // [rsp+0h] [rbp-8C8h] BYREF
char v37[32]; // [rsp+20h] [rbp-8A8h] BYREF
char v38[32]; // [rsp+40h] [rbp-888h] BYREF
char v39[32]; // [rsp+60h] [rbp-868h] BYREF
char v40[32]; // [rsp+80h] [rbp-848h] BYREF
char v41[32]; // [rsp+A0h] [rbp-828h] BYREF
char s[16]; // [rsp+C0h] [rbp-808h] BYREF
__m128i v43; // [rsp+D0h] [rbp-7F8h]
__m128i v44; // [rsp+E0h] [rbp-7E8h]
__m128i v45; // [rsp+F0h] [rbp-7D8h]
char v46[8]; // [rsp+100h] [rbp-7C8h] BYREF

strcpy(iniFilename, src);
channelCount = a2;
GetPrivateProfileString_("DB", "root_username", "root", v38, 30LL, iniFilename);
GetPrivateProfileString_("DB", "root_password", &unk_1AF6, v39, 30LL, iniFilename);
GetPrivateProfileString_("DB", "username", "sma", v40, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v41, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v36, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v37, 30LL, iniFilename);
v6 = (const char *)mysql_get_client_info();
v7 = (const char *)trim(v37);
v8 = (const char *)trim(v36);
v9 = (const char *)trim(v40);
printLog(1LL, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", v9, v8, v7, v6);
conn = mysql_init(0LL);
if ( !conn )
{
v32 = (const char *)((__int64 (*)(void))mysql_error)();
v33 = mysql_errno(conn);
printLog(6LL, "Error %u initializing connection structure : %s\n", v33, v32);
exit(-10);
}
if ( !mysql_real_connect(conn, v36, v40, v41, v37, 0LL, 0LL, 0LL) )
{
v16 = mysql_errno(conn);
v17 = _errno_location();
*v17 = v16;
v18 = (unsigned int *)v17;
if ( ((v16 - 1045) & 0xFFFFFFFB) != 0 )
{
v19 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u connecting to Database: %s\n", *v18, v19);
exit(-11);
}
printf("Database '%s' doesn't exist. Trying to create it...", v37);
if ( !mysql_real_connect(conn, v36, v38, v39, 0LL, 0LL, 0LL, 0LL) )
{
v20 = (const char *)mysql_error(conn);
v21 = mysql_errno(conn);
printf("Error %u connecting to Database: %s\n", v21, v20);
exit(-12);
}
sprintf(s, "create database if not exists %s", v37);
if ( (unsigned int)mysql_query(conn, s) )
{
v22 = (const char *)mysql_error(conn);
v23 = mysql_errno(conn);
printf("Error %u creating Database: %s\n", v23, v22);
exit(-13);
}
sprintf(s, "create user %s@localhost identified by '%s'", v40, v41);
if ( (unsigned int)mysql_query(conn, s) )
{
v24 = (const char *)mysql_error(conn);
v25 = mysql_errno(conn);
printf("Error %u creating user: %s\n", v25, v24);
exit(-14);
}
sprintf(s, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", v37, v40);
if ( (unsigned int)mysql_query(conn, s) )
{
v26 = (const char *)mysql_error(conn);
v27 = mysql_errno(conn);
printf("Error %u granting rights: %s\n", v27, v26);
exit(-15);
}
mysql_close(conn);
printf("Done. Reconnecting to newly created database...\n");
conn = mysql_init(0LL);
if ( !conn )
{
v28 = (const char *)mysql_error(0LL);
v29 = mysql_errno(conn);
printf("Error %u re-initializing connection structure : %s\n", v29, v28);
exit(-16);
}
if ( !mysql_real_connect(conn, v36, v40, v41, v37, 0LL, 0LL, 0LL) )
{
v30 = (const char *)mysql_error(conn);
v31 = mysql_errno(conn);
printLog(6LL, "Error %u connecting to new Database: %s\n", v31, v30);
exit(-17);
}
printf(
"\n"
"Congratulations. A new database called '%s' has been created, with the table 'logged_values'.\n"
"A new user '%s' has also been created and was granted access to the new DB.\n"
"It is now strongly recommended to edit yasdi.ini to remove the\n"
"root_username/root_password information.\n"
"\n"
"Waiting 20 seconds...\n",
v37,
v40);
fflush(stdout);
sleep(0x14u);
}
strcpy(v46, "TIME");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC29);
v43 = _mm_load_si128((const __m128i *)&LC30);
v44 = _mm_load_si128((const __m128i *)&LC31);
v45 = _mm_load_si128((const __m128i *)&LC32);
if ( a2 > 0 )
{
v10 = (__int64)&a3[30 * (a2 - 1) + 30];
do
{
v11 = strlen(s);
v12 = a3;
a3 += 30;
*(_WORD *)&s[v11] = 8236;
v13 = stpcpy(&s[v11 + 2], v12);
v14 = a4;
a4 += 30;
*v13 = 32;
strcpy(v13 + 1, v14);
}
while ( a3 != (const char *)v10 );
}
*(_WORD *)&s[strlen(s)] = 41;
printLog(0LL, "Executing : %s\n", s);
if ( (unsigned int)mysql_query(conn, s) )
{
v34 = (const char *)mysql_error(conn);
v35 = mysql_errno(conn);
printLog(6LL, "Error %u creating table: %s\n", v35, v34);
exit(-18);
}
return printLog(1LL, "Connection OK.\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, int a2, const char *a3, const char *a4)
{
const char *v6; // r15
const char *v7; // r14
const char *v8; // r13
const char *v9; // rax
__int64 v10; // rbp
size_t v11; // rax
const char *v12; // rsi
char *v13; // rax
const char *v14; // rsi
int v16; // er13
int *v17; // rax
unsigned int *v18; // r14
const char *v19; // rax
const char *v20; // r12
unsigned int v21; // eax
const char *v22; // r12
unsigned int v23; // eax
const char *v24; // r12
unsigned int v25; // eax
const char *v26; // r12
unsigned int v27; // eax
const char *v28; // r12
unsigned int v29; // eax
const char *v30; // r12
unsigned int v31; // eax
const char *v32; // r12
unsigned int v33; // eax
const char *v34; // r12
unsigned int v35; // eax
char v36[32]; // [rsp+0h] [rbp-8C8h] BYREF
char v37[32]; // [rsp+20h] [rbp-8A8h] BYREF
char v38[32]; // [rsp+40h] [rbp-888h] BYREF
char v39[32]; // [rsp+60h] [rbp-868h] BYREF
char v40[32]; // [rsp+80h] [rbp-848h] BYREF
char v41[32]; // [rsp+A0h] [rbp-828h] BYREF
char s[16]; // [rsp+C0h] [rbp-808h] BYREF
__m128i v43; // [rsp+D0h] [rbp-7F8h]
__m128i v44; // [rsp+E0h] [rbp-7E8h]
__m128i v45; // [rsp+F0h] [rbp-7D8h]
char v46[8]; // [rsp+100h] [rbp-7C8h] BYREF

strcpy(iniFilename, src);
channelCount = a2;
GetPrivateProfileString_("DB", "root_username", "root", v38, 30LL, iniFilename);
GetPrivateProfileString_("DB", "root_password", &unk_1AF6, v39, 30LL, iniFilename);
GetPrivateProfileString_("DB", "username", "sma", v40, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v41, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v36, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v37, 30LL, iniFilename);
v6 = (const char *)mysql_get_client_info();
v7 = (const char *)trim(v37);
v8 = (const char *)trim(v36);
v9 = (const char *)trim(v40);
printLog(1LL, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", v9, v8, v7, v6);
conn = mysql_init(0LL);
if ( !conn )
{
v32 = (const char *)((__int64 (*)(void))mysql_error)();
v33 = mysql_errno(conn);
printLog(6LL, "Error %u initializing connection structure : %s\n", v33, v32);
exit(-10);
}
if ( !mysql_real_connect(conn, v36, v40, v41, v37, 0LL, 0LL, 0LL) )
{
v16 = mysql_errno(conn);
v17 = _errno_location();
*v17 = v16;
v18 = (unsigned int *)v17;
if ( ((v16 - 1045) & 0xFFFFFFFB) != 0 )
{
v19 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u connecting to Database: %s\n", *v18, v19);
exit(-11);
}
printf("Database '%s' doesn't exist. Trying to create it...", v37);
if ( !mysql_real_connect(conn, v36, v38, v39, 0LL, 0LL, 0LL, 0LL) )
{
v20 = (const char *)mysql_error(conn);
v21 = mysql_errno(conn);
printf("Error %u connecting to Database: %s\n", v21, v20);
exit(-12);
}
sprintf(s, "create database if not exists %s", v37);
if ( (unsigned int)mysql_query(conn, s) )
{
v22 = (const char *)mysql_error(conn);
v23 = mysql_errno(conn);
printf("Error %u creating Database: %s\n", v23, v22);
exit(-13);
}
sprintf(s, "create user %s@localhost identified by '%s'", v40, v41);
if ( (unsigned int)mysql_query(conn, s) )
{
v24 = (const char *)mysql_error(conn);
v25 = mysql_errno(conn);
printf("Error %u creating user: %s\n", v25, v24);
exit(-14);
}
sprintf(s, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", v37, v40);
if ( (unsigned int)mysql_query(conn, s) )
{
v26 = (const char *)mysql_error(conn);
v27 = mysql_errno(conn);
printf("Error %u granting rights: %s\n", v27, v26);
exit(-15);
}
mysql_close(conn);
printf("Done. Reconnecting to newly created database...\n");
conn = mysql_init(0LL);
if ( !conn )
{
v28 = (const char *)mysql_error(0LL);
v29 = mysql_errno(conn);
printf("Error %u re-initializing connection structure : %s\n", v29, v28);
exit(-16);
}
if ( !mysql_real_connect(conn, v36, v40, v41, v37, 0LL, 0LL, 0LL) )
{
v30 = (const char *)mysql_error(conn);
v31 = mysql_errno(conn);
printLog(6LL, "Error %u connecting to new Database: %s\n", v31, v30);
exit(-17);
}
printf(
"\n"
"Congratulations. A new database called '%s' has been created, with the table 'logged_values'.\n"
"A new user '%s' has also been created and was granted access to the new DB.\n"
"It is now strongly recommended to edit yasdi.ini to remove the\n"
"root_username/root_password information.\n"
"\n"
"Waiting 20 seconds...\n",
v37,
v40);
fflush(stdout);
sleep(0x14u);
}
strcpy(v46, "TIME");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC29);
v43 = _mm_load_si128((const __m128i *)&LC30);
v44 = _mm_load_si128((const __m128i *)&LC31);
v45 = _mm_load_si128((const __m128i *)&LC32);
if ( a2 > 0 )
{
v10 = (__int64)&a3[30 * (a2 - 1) + 30];
do
{
v11 = strlen(s);
v12 = a3;
a3 += 30;
*(_WORD *)&s[v11] = 8236;
v13 = stpcpy(&s[v11 + 2], v12);
v14 = a4;
a4 += 30;
*v13 = 32;
strcpy(v13 + 1, v14);
}
while ( a3 != (const char *)v10 );
}
*(_WORD *)&s[strlen(s)] = 41;
printLog(0LL, "Executing : %s\n", s);
if ( (unsigned int)mysql_query(conn, s) )
{
v34 = (const char *)mysql_error(conn);
v35 = mysql_errno(conn);
printLog(6LL, "Error %u creating table: %s\n", v35, v34);
exit(-18);
}
return printLog(1LL, "Connection OK.\n");
}

original_code:
void dbInit(char * iniFilename, int channelCount, char dbColumns[][30], char dbTypes[][30])
{
 char server[30];
 char database[30];
 char rootUsername[30];
 char rootPassword[30];
 char username[30];
 char password[30];
 char query[2000];
 int (*__errno_location ());
 int channelNr;
    strcpy(_iniFilename, iniFilename);
    _channelCount = channelCount;
 GetPrivateProfileString_("DB", "root_username", "root", rootUsername, sizeof(rootUsername), _iniFilename);
 GetPrivateProfileString_("DB", "root_password", "", rootPassword, sizeof(rootPassword), _iniFilename);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
  if ((*__errno_location ()) != 1049 && (*__errno_location ()) != 1045) {
   printLog(6, "Error %u connecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
   exit(-11);
  }
  else {
   printf("Database '%s' doesn't exist. Trying to create it...", database);
   if (mysql_real_connect(_conn, server, rootUsername, rootPassword, ((void *)0), 0, ((void *)0), 0) == ((void *)0)) {
    printf("Error %u connecting to Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-12);
   }
   sprintf(query, "create database if not exists %s", database);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-13);
   }
   sprintf(query, "create user %s@localhost identified by '%s'", username, password);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating user: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-14);
   }
   sprintf(query, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", database, username);
   if (mysql_query(_conn, query)) {
    printf("Error %u granting rights: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-15);
   }
   mysql_close(_conn);
   printf("Done. Reconnecting to newly created database...\n", database);
   _conn = mysql_init(((void *)0));
   if (_conn == ((void *)0)) {
    printf("Error %u re-initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-16);
   }
   if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
    printLog(6, "Error %u connecting to new Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-17);
   }
   printf("\nCongratulations. A new database called '%s' has been created, with the table 'logged_values'.\nA new user '%s' has also been created and was granted access to the new DB.\nIt is now strongly recommended to edit yasdi.ini to remove the\nroot_username/root_password information.\n\nWaiting 20 seconds...\n", database, username);
   fflush(stdout);
   sleep(20);
  }
 }
 sprintf(query, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
  strcat(query, " ");
  strcat(query, dbTypes[channelNr]);
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
 if (mysql_query(_conn, query)) {
  printLog(6, "Error %u creating table: %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-18);
 }
 printLog(1, "Connection OK.\n");
}

canonical_original_code:
void dbInit(char *iniFilename, int channelCount, char dbColumns[][30], char dbTypes[][30])
{
 char server[30];
 char database[30];
 char rootUsername[30];
 char rootPassword[30];
 char username[30];
 char password[30];
 char query[2000];
 int *__errno_location();
 int channelNr;
    strcpy(_iniFilename, iniFilename);
    _channelCount = channelCount;
 GetPrivateProfileString_("DB", "root_username", "root", rootUsername, sizeof(rootUsername), _iniFilename);
 GetPrivateProfileString_("DB", "root_password", "", rootPassword, sizeof(rootPassword), _iniFilename);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
  if ((*__errno_location ()) != 1049 && (*__errno_location ()) != 1045) {
   printLog(6, "Error %u connecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
   exit(-11);
  }
  else {
   printf("Database '%s' doesn't exist. Trying to create it...", database);
   if (mysql_real_connect(_conn, server, rootUsername, rootPassword, ((void *)0), 0, ((void *)0), 0) == ((void *)0)) {
    printf("Error %u connecting to Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-12);
   }
   sprintf(query, "create database if not exists %s", database);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-13);
   }
   sprintf(query, "create user %s@localhost identified by '%s'", username, password);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating user: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-14);
   }
   sprintf(query, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", database, username);
   if (mysql_query(_conn, query)) {
    printf("Error %u granting rights: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-15);
   }
   mysql_close(_conn);
   printf("Done. Reconnecting to newly created database...\n", database);
   _conn = mysql_init(((void *)0));
   if (_conn == ((void *)0)) {
    printf("Error %u re-initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-16);
   }
   if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
    printLog(6, "Error %u connecting to new Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-17);
   }
   printf("\nCongratulations. A new database called '%s' has been created, with the table 'logged_values'.\nA new user '%s' has also been created and was granted access to the new DB.\nIt is now strongly recommended to edit yasdi.ini to remove the\nroot_username/root_password information.\n\nWaiting 20 seconds...\n", database, username);
   fflush(stdout);
   sleep(20);
  }
 }
 sprintf(query, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
  strcat(query, " ");
  strcat(query, dbTypes[channelNr]);
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
 if (mysql_query(_conn, query)) {
  printLog(6, "Error %u creating table: %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-18);
 }
 printLog(1, "Connection OK.\n");
}

variable_types:
iniFilename
char *

variable_types:
dbColumns
char [][30]

variable_types:
dbTypes
char [][30]

variable_types:
server
char [30]

variable_types:
database
char [30]

variable_types:
rootUsername
char [30]

variable_types:
rootPassword
char [30]

variable_types:
username
char [30]

variable_types:
password
char [30]

variable_types:
query
char [2000]

variable_types: channelNr int


reconnect

repo: vdeconinck/SMysqLogger

decompiled_code:
__int64 reconnect()
{
const char *v0; // rbp
const char *v1; // rbx
const char *v2; // r12
const char *v3; // rax
const char *v5; // r12
unsigned int v6; // eax
int v7; // ebp
int *v8; // rax
unsigned int *v9; // rbx
const char *v10; // rax
__int64 v11; // [rsp-10h] [rbp-A8h]
char v12[32]; // [rsp+0h] [rbp-98h] BYREF
char v13[32]; // [rsp+20h] [rbp-78h] BYREF
char v14[32]; // [rsp+40h] [rbp-58h] BYREF
char v15[56]; // [rsp+60h] [rbp-38h] BYREF

mysql_close(conn);
GetPrivateProfileString_("DB", "username", "sma", v14, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v15, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v12, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v13, 30LL, iniFilename);
v0 = (const char *)mysql_get_client_info();
v1 = (const char *)trim(v13);
v2 = (const char *)trim(v12);
v3 = (const char *)trim(v14);
printLog(1LL, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", v3, v2, v1, v0);
conn = mysql_init(0LL);
if ( !conn )
{
v5 = (const char *)mysql_error(0LL);
v6 = mysql_errno(conn);
printLog(6LL, "Error %u reinitializing connection structure : %s\n", v6, v5);
exit(-10);
}
if ( !mysql_real_connect(conn, v12, v14, v15, v13, 0LL, 0LL, 0LL) )
{
v7 = mysql_errno(conn);
v8 = _errno_location();
*v8 = v7;
v9 = (unsigned int *)v8;
v10 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u reconnecting to Database: %s\n", *v9, v10);
exit(-11);
}
return printLog(2LL, "Reconnected\n", v11);
}

canonical_decompiled_code:
__int64 func1()
{
const char *v0; // rbp
const char *v1; // rbx
const char *v2; // r12
const char *v3; // rax
const char *v5; // r12
unsigned int v6; // eax
int v7; // ebp
int *v8; // rax
unsigned int *v9; // rbx
const char *v10; // rax
__int64 v11; // [rsp-10h] [rbp-A8h]
char v12[32]; // [rsp+0h] [rbp-98h] BYREF
char v13[32]; // [rsp+20h] [rbp-78h] BYREF
char v14[32]; // [rsp+40h] [rbp-58h] BYREF
char v15[56]; // [rsp+60h] [rbp-38h] BYREF

mysql_close(conn);
GetPrivateProfileString_("DB", "username", "sma", v14, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v15, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v12, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v13, 30LL, iniFilename);
v0 = (const char *)mysql_get_client_info();
v1 = (const char *)trim(v13);
v2 = (const char *)trim(v12);
v3 = (const char *)trim(v14);
printLog(1LL, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", v3, v2, v1, v0);
conn = mysql_init(0LL);
if ( !conn )
{
v5 = (const char *)mysql_error(0LL);
v6 = mysql_errno(conn);
printLog(6LL, "Error %u reinitializing connection structure : %s\n", v6, v5);
exit(-10);
}
if ( !mysql_real_connect(conn, v12, v14, v15, v13, 0LL, 0LL, 0LL) )
{
v7 = mysql_errno(conn);
v8 = _errno_location();
*v8 = v7;
v9 = (unsigned int *)v8;
v10 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u reconnecting to Database: %s\n", *v9, v10);
exit(-11);
}
return printLog(2LL, "Reconnected\n", v11);
}

original_code:
void reconnect()
{
 char server[30];
 char database[30];
 char username[30];
 char password[30];
 int (*__errno_location ());
 int channelNr;
    mysql_close(_conn);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u reinitializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
        printLog(6, "Error %u reconnecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
  exit(-11);
 }
 printLog(2, "Reconnected\n");
}

canonical_original_code:
void reconnect()
{
 char server[30];
 char database[30];
 char username[30];
 char password[30];
 int *__errno_location();
 int channelNr;
    mysql_close(_conn);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u reinitializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
        printLog(6, "Error %u reconnecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
  exit(-11);
 }
 printLog(2, "Reconnected\n");
}

return_type: void


insertValues

decompiled_code:
__int64 __fastcall insertValues(int a1, const char *a2, const char *a3)
{
const char *v3; // rbp
struct tm *v5; // r12
__int64 v6; // r12
__int64 v7; // r13
size_t v8; // rax
const char *v9; // rsi
char *v10; // rax
__int64 v11; // rbp
char *v12; // rax
size_t v13; // rax
const char *v14; // rsi
int *v15; // rax
int *v16; // rbx
int v17; // eax
const char *v19; // r12
unsigned int v20; // eax
size_t v21; // rax
char *v22; // rax
time_t v23; // [rsp+0h] [rbp-818h] BYREF
char src[9]; // [rsp+Ch] [rbp-80Ch] BYREF
char s[11]; // [rsp+15h] [rbp-803h] BYREF
char dest[16]; // [rsp+20h] [rbp-7F8h] BYREF
__m128i v27; // [rsp+30h] [rbp-7E8h]
char v28[16]; // [rsp+40h] [rbp-7D8h] BYREF

v3 = a2;
v23 = time(0LL);
v5 = localtime(&v23);
strftime(s, 0xBuLL, "%Y-%m-%d", v5);
strftime(src, 9uLL, "%H:%M:%S", v5);
strcpy(v28, "te, logtime");
*(__m128i *)dest = _mm_load_si128((const __m128i *)&LC40);
v27 = _mm_load_si128((const __m128i *)&LC41);
if ( a1 <= 0 )
{
v21 = strlen(dest);
strcpy(&dest[v21], ") VALUES ('");
v22 = stpcpy(&dest[v21 + 11], s);
*(_DWORD *)v22 = 656419879;
*(_WORD *)stpcpy(v22 + 4, src) = 39;
}
else
{
v6 = 30LL * (unsigned int)(a1 - 1);
v7 = (__int64)&a2[v6 + 30];
do
{
v8 = strlen(dest);
v9 = v3;
v3 += 30;
*(_WORD *)&dest[v8] = 8236;
v10 = stpcpy(&dest[v8 + 2], v9);
}
while ( v3 != (const char *)v7 );
strcpy(v10, ") VALUES ('");
v11 = (__int64)&a3[v6 + 30];
v12 = stpcpy(v10 + 11, s);
*(_DWORD *)v12 = 656419879;
*(_WORD *)stpcpy(v12 + 4, src) = 39;
do
{
v13 = strlen(dest);
v14 = a3;
a3 += 30;
*(_DWORD *)&dest[v13] = 2564140;
*(_WORD *)stpcpy(&dest[v13 + 3], v14) = 39;
}
while ( a3 != (const char *)v11 );
}
*(_WORD *)&dest[strlen(dest)] = 41;
printLog(0LL, "Executing : %s\n", dest);
v15 = _errno_location();
*v15 = -1;
v16 = v15;
do
{
v17 = mysql_query(conn, dest);
*v16 = v17;
if ( !v17 )
break;
v19 = (const char *)mysql_error(conn);
v20 = mysql_errno(conn);
printLog(4LL, "Error %u performing insert into Database: %s.\n", v20, v19);
reconnect();
sleep(1u);
}
while ( *v16 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, const char *a2, const char *a3)
{
const char *v3; // rbp
struct tm *v5; // r12
__int64 v6; // r12
__int64 v7; // r13
size_t v8; // rax
const char *v9; // rsi
char *v10; // rax
__int64 v11; // rbp
char *v12; // rax
size_t v13; // rax
const char *v14; // rsi
int *v15; // rax
int *v16; // rbx
int v17; // eax
const char *v19; // r12
unsigned int v20; // eax
size_t v21; // rax
char *v22; // rax
time_t v23; // [rsp+0h] [rbp-818h] BYREF
char src[9]; // [rsp+Ch] [rbp-80Ch] BYREF
char s[11]; // [rsp+15h] [rbp-803h] BYREF
char dest[16]; // [rsp+20h] [rbp-7F8h] BYREF
__m128i v27; // [rsp+30h] [rbp-7E8h]
char v28[16]; // [rsp+40h] [rbp-7D8h] BYREF

v3 = a2;
v23 = time(0LL);
v5 = localtime(&v23);
strftime(s, 0xBuLL, "%Y-%m-%d", v5);
strftime(src, 9uLL, "%H:%M:%S", v5);
strcpy(v28, "te, logtime");
*(__m128i *)dest = _mm_load_si128((const __m128i *)&LC40);
v27 = _mm_load_si128((const __m128i *)&LC41);
if ( a1 <= 0 )
{
v21 = strlen(dest);
strcpy(&dest[v21], ") VALUES ('");
v22 = stpcpy(&dest[v21 + 11], s);
*(_DWORD *)v22 = 656419879;
*(_WORD *)stpcpy(v22 + 4, src) = 39;
}
else
{
v6 = 30LL * (unsigned int)(a1 - 1);
v7 = (__int64)&a2[v6 + 30];
do
{
v8 = strlen(dest);
v9 = v3;
v3 += 30;
*(_WORD *)&dest[v8] = 8236;
v10 = stpcpy(&dest[v8 + 2], v9);
}
while ( v3 != (const char *)v7 );
strcpy(v10, ") VALUES ('");
v11 = (__int64)&a3[v6 + 30];
v12 = stpcpy(v10 + 11, s);
*(_DWORD *)v12 = 656419879;
*(_WORD *)stpcpy(v12 + 4, src) = 39;
do
{
v13 = strlen(dest);
v14 = a3;
a3 += 30;
*(_DWORD *)&dest[v13] = 2564140;
*(_WORD *)stpcpy(&dest[v13 + 3], v14) = 39;
}
while ( a3 != (const char *)v11 );
}
*(_WORD *)&dest[strlen(dest)] = 41;
printLog(0LL, "Executing : %s\n", dest);
v15 = _errno_location();
*v15 = -1;
v16 = v15;
do
{
v17 = mysql_query(conn, dest);
*v16 = v17;
if ( !v17 )
break;
v19 = (const char *)mysql_error(conn);
v20 = mysql_errno(conn);
printLog(4LL, "Error %u performing insert into Database: %s.\n", v20, v19);
func1();
sleep(1u);
}
while ( *v16 );
return 0LL;
}

original_code:
int insertValues(int channelCount, char dbColumns[][30], char channelValues[][30]) {
 char query[2000];
 int (*__errno_location ());
 int channelNr;
 time_t now;
 struct tm *tm_now;
 char logdate[11];
 char logtime[9];
 now = time(((void *)0));
 tm_now = localtime(&now);
 strftime(logdate, sizeof(logdate), "%Y-%m-%d", tm_now);
 strftime(logtime, sizeof(logtime), "%H:%M:%S", tm_now);
 sprintf(query, "INSERT INTO logged_values (logdate, logtime");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
 }
 strcat(query, ") VALUES ('");
 strcat(query, logdate);
 strcat(query, "', '");
 strcat(query, logtime);
 strcat(query, "'");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", '");
  strcat(query, channelValues[channelNr]);
  strcat(query, "'");
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
    (*__errno_location ()) = -1;
    while ((*__errno_location ()) != 0) {
        (*__errno_location ()) = mysql_query(_conn, query);
        if ((*__errno_location ()) != 0) {
            printLog(4, "Error %u performing insert into Database: %s.\n", mysql_errno(_conn), mysql_error(_conn));
            reconnect();
            sleep(1);
        }
    }
 return (*__errno_location ());
}

canonical_original_code:
int insertValues(int channelCount, char dbColumns[][30], char channelValues[][30]) {
 char query[2000];
 int *__errno_location();
 int channelNr;
 long now;
 struct tm *tm_now;
 char logdate[11];
 char logtime[9];
 now = time(((void *)0));
 tm_now = localtime(&now);
 strftime(logdate, sizeof(logdate), "%Y-%m-%d", tm_now);
 strftime(logtime, sizeof(logtime), "%H:%M:%S", tm_now);
 sprintf(query, "INSERT INTO logged_values (logdate, logtime");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
 }
 strcat(query, ") VALUES ('");
 strcat(query, logdate);
 strcat(query, "', '");
 strcat(query, logtime);
 strcat(query, "'");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", '");
  strcat(query, channelValues[channelNr]);
  strcat(query, "'");
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
    (*__errno_location ()) = -1;
    while ((*__errno_location ()) != 0) {
        (*__errno_location ()) = mysql_query(_conn, query);
        if ((*__errno_location ()) != 0) {
            printLog(4, "Error %u performing insert into Database: %s.\n", mysql_errno(_conn), mysql_error(_conn));
            reconnect();
            sleep(1);
        }
    }
 return (*__errno_location ());
}

variable_types: channelCount int

variable_types:
channelValues
char [][30]

variable_types: now long

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

variable_types:
logdate
char [11]

variable_types:
logtime
char [9]

return_type: int


dbShutdown

decompiled_code:
__int64 dbShutdown()
{
return mysql_close(conn);
}

canonical_decompiled_code:
__int64 func3()
{
return mysql_close(conn);
}

original_code:
void dbShutdown()
{
 mysql_close(_conn);
}


repo: vedantk/evt-server

decompiled_code:
int __fastcall evt_server_start(__int64 a1, void *a2)
{
event_init();
qword_A50 = a1;
return pthread_create(&newthread, 0LL, server_worker, a2);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, void *a2)
{
event_init();
qword_A50 = a1;
return pthread_create(&newthread, 0LL, server_worker, a2);
}

original_code:
int evt_server_start(evt_callback cb, void* arg)
{
 event_init();
 serv.handler = cb;
 return pthread_create(&serv.server, ((void *)0), server_worker, arg);
}

canonical_original_code:
int evt_server_start(void (*cb)(int fd, short evt, void *arg), void *arg)
{
 event_init();
 serv.handler = cb;
 return pthread_create(&serv.server, ((void *)0), server_worker, arg);
}

variable_types:
cb
void (*)(int fd, short evt, void *arg)

variable_types:
arg
void *


decompiled_code:
struct addrinfo *get_server_addrinfo()
{
return ai;
}

canonical_decompiled_code:
struct addrinfo *func3()
{
return ai;
}

original_code:
struct addrinfo* get_server_addrinfo()
{
 return serv.ai;
}

canonical_original_code:
struct addrinfo *get_server_addrinfo()
{
 return serv.ai;
}

return_type: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
int __fastcall print_addrinfo(const char **a1)
{
return printf(
"ai_flags; %d\nai_family; %d\nai_socktype; %d\nai_protocol; %d\nai_addrlen; %u\nai_canonname; %s\n",
*(unsigned int *)a1,
*((unsigned int *)a1 + 1),
*((unsigned int *)a1 + 2),
*((unsigned int *)a1 + 3),
*((unsigned int *)a1 + 4),
a1[4]);
}

canonical_decompiled_code:
int __fastcall func4(const char **a1)
{
return printf(
"ai_flags; %d\nai_family; %d\nai_socktype; %d\nai_protocol; %d\nai_addrlen; %u\nai_canonname; %s\n",
*(unsigned int *)a1,
*((unsigned int *)a1 + 1),
*((unsigned int *)a1 + 2),
*((unsigned int *)a1 + 3),
*((unsigned int *)a1 + 4),
a1[4]);
}

original_code:
void print_addrinfo(struct addrinfo* ai)
{
 printf ("ai_flags; %d\n"
  "ai_family; %d\n"
  "ai_socktype; %d\n"
  "ai_protocol; %d\n"
  "ai_addrlen; %u\n"
  "ai_canonname; %s\n"
 , ai->ai_flags, ai->ai_family, ai->ai_socktype,
 ai->ai_protocol, ai->ai_addrlen, ai->ai_canonname);
}

canonical_original_code:
void print_addrinfo(struct addrinfo *ai)
{
 printf ("ai_flags; %d\n"
  "ai_family; %d\n"
  "ai_socktype; %d\n"
  "ai_protocol; %d\n"
  "ai_addrlen; %u\n"
  "ai_canonname; %s\n"
 , ai->ai_flags, ai->ai_family, ai->ai_socktype,
 ai->ai_protocol, ai->ai_addrlen, ai->ai_canonname);
}

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *


decompiled_code:
__int64 get_sockaddr_size()
{
return (unsigned int)len;
}

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

original_code:
int get_sockaddr_size()
{
 return serv.socksize;
}


decompiled_code:
_WORD *new_sockaddr()
{
_WORD *result; // rax

result = calloc(1uLL, (unsigned int)len);
*result = ai->ai_family;
return result;
}

canonical_decompiled_code:
_WORD *func6()
{
_WORD *result; // rax

result = calloc(1uLL, (unsigned int)len);
*result = ai->ai_family;
return result;
}

original_code:
struct sockaddr* new_sockaddr()
{
 struct sockaddr* sa = calloc(1, serv.socksize);
 sa->sa_family = serv.ai->ai_family;
 return sa;
}

canonical_original_code:
struct sockaddr *new_sockaddr()
{
 struct sockaddr *sa = calloc(1, serv.socksize);
 sa->sa_family = serv.ai->ai_family;
 return sa;
}

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


original_code:
int serialize_addr(struct sockaddr* src, char* dest, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < IP4_SOCKLEN) return 0;
  sa4_serial* dsa4 = (sa4_serial*) dest;
  struct sockaddr_in* sa4 = (struct sockaddr_in*) src;
  dsa4->sin_family = htons(sa4->sin_family);
  dsa4->sin_port = htons(sa4->sin_port);
  dsa4->sin_addr = htonl(sa4->sin_addr.s_addr);
  return IP4_SOCKLEN;
 } else {
  if (size < IP6_SOCKLEN) return 0;
  struct sockaddr_in6* dsa6 = (struct sockaddr_in6*) dest;
  struct sockaddr_in6* sa6 = (struct sockaddr_in6*) src;
  dsa6->sin6_family = htons(sa6->sin6_family);
  dsa6->sin6_port = htons(sa6->sin6_port);
  dsa6->sin6_flowinfo = htonl(sa6->sin6_flowinfo);
  dsa6->sin6_scope_id = htonl(sa6->sin6_scope_id);
  memcpy(&dsa6->sin6_addr.__in6_u.__u6_addr8,
         &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  return IP6_SOCKLEN;
 }
}

canonical_original_code:
int serialize_addr(struct sockaddr *src, char *dest, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < IP4_SOCKLEN) return 0;
  struct sa4_serial *dsa4 = (sa4_serial*) dest;
  struct sockaddr_in *sa4 = (struct sockaddr_in*) src;
  dsa4->sin_family = htons(sa4->sin_family);
  dsa4->sin_port = htons(sa4->sin_port);
  dsa4->sin_addr = htonl(sa4->sin_addr.s_addr);
  return IP4_SOCKLEN;
 } else {
  if (size < IP6_SOCKLEN) return 0;
  struct sockaddr_in6 *dsa6 = (struct sockaddr_in6*) dest;
  struct sockaddr_in6 *sa6 = (struct sockaddr_in6*) src;
  dsa6->sin6_family = htons(sa6->sin6_family);
  dsa6->sin6_port = htons(sa6->sin6_port);
  dsa6->sin6_flowinfo = htonl(sa6->sin6_flowinfo);
  dsa6->sin6_scope_id = htonl(sa6->sin6_scope_id);
  memcpy(&dsa6->sin6_addr.__in6_u.__u6_addr8,
         &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  return IP6_SOCKLEN;
 }
}

variable_types:
dest
char *

variable_types: size int

variable_types: struct sa4_serial { short sin_family; unsigned short sin_port; unsigned int sin_addr; } *

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

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } *

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

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

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }


original_code:
int deserialize_addr(char* src, struct sockaddr* dest, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < IP4_SOCKLEN) return 0;
  sa4_serial* sa4 = (sa4_serial*) src;
  struct sockaddr_in* dsa4 = (struct sockaddr_in*) dest;
  dsa4->sin_family = ntohs(sa4->sin_family);
  if (dsa4->sin_family != 2) return 0;
  dsa4->sin_port = ntohs(sa4->sin_port);
  dsa4->sin_addr.s_addr = ntohl(sa4->sin_addr);
  return IP4_SOCKLEN;
 } else {
  if (size < IP6_SOCKLEN) return 0;
  struct sockaddr_in6* sa6 = (struct sockaddr_in6*) src;
  struct sockaddr_in6* dsa6 = (struct sockaddr_in6*) dest;
  dsa6->sin6_family = ntohs(sa6->sin6_family);
  if (dsa6->sin6_family != 10) return 0;
  dsa6->sin6_port = ntohs(sa6->sin6_port);
  dsa6->sin6_flowinfo = ntohl(sa6->sin6_flowinfo);
  memcpy(&dsa6->sin6_addr.__in6_u.__u6_addr8,
         &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  dsa6->sin6_scope_id = ntohl(sa6->sin6_scope_id);
  return IP6_SOCKLEN;
 }
}

canonical_original_code:
int deserialize_addr(char *src, struct sockaddr *dest, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < IP4_SOCKLEN) return 0;
  struct sa4_serial *sa4 = (sa4_serial*) src;
  struct sockaddr_in *dsa4 = (struct sockaddr_in*) dest;
  dsa4->sin_family = ntohs(sa4->sin_family);
  if (dsa4->sin_family != 2) return 0;
  dsa4->sin_port = ntohs(sa4->sin_port);
  dsa4->sin_addr.s_addr = ntohl(sa4->sin_addr);
  return IP4_SOCKLEN;
 } else {
  if (size < IP6_SOCKLEN) return 0;
  struct sockaddr_in6 *sa6 = (struct sockaddr_in6*) src;
  struct sockaddr_in6 *dsa6 = (struct sockaddr_in6*) dest;
  dsa6->sin6_family = ntohs(sa6->sin6_family);
  if (dsa6->sin6_family != 10) return 0;
  dsa6->sin6_port = ntohs(sa6->sin6_port);
  dsa6->sin6_flowinfo = ntohl(sa6->sin6_flowinfo);
  memcpy(&dsa6->sin6_addr.__in6_u.__u6_addr8,
         &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  dsa6->sin6_scope_id = ntohl(sa6->sin6_scope_id);
  return IP6_SOCKLEN;
 }
}

variable_types:
src
char *

user_defined_types: struct sa4_serial { short sin_family; unsigned short sin_port; unsigned int sin_addr; }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


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

result = 0LL;
if ( ai->ai_family == 2 )
{
if ( a3 > 3 )
{
a2->m128i_i32[0] = *(_DWORD *)(a1 + 4);
result = 4LL;
}
}
else if ( a3 > 15 )
{
result = 16LL;
*a2 = _mm_loadu_si128((const __m128i *)(a1 + 8));
}
return result;
}

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

result = 0LL;
if ( ai->ai_family == 2 )
{
if ( a3 > 3 )
{
a2->m128i_i32[0] = *(_DWORD *)(a1 + 4);
result = 4LL;
}
}
else if ( a3 > 15 )
{
result = 16LL;
*a2 = _mm_loadu_si128((const __m128i *)(a1 + 8));
}
return result;
}

original_code:
int copy_ip_addr(struct sockaddr* src, char* buf, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < 4) return 0;
  struct sockaddr_in* sa4 = (struct sockaddr_in*) src;
  memcpy(buf, &sa4->sin_addr.s_addr, 4);
  return 4;
 } else {
  if (size < 16) return 0;
  struct sockaddr_in6* sa6 = (struct sockaddr_in6*) src;
  memcpy(buf, &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  return 16;
 }
}

canonical_original_code:
int copy_ip_addr(struct sockaddr *src, char *buf, int size)
{
 if (serv.ai->ai_family == 2) {
  if (size < 4) return 0;
  struct sockaddr_in *sa4 = (struct sockaddr_in*) src;
  memcpy(buf, &sa4->sin_addr.s_addr, 4);
  return 4;
 } else {
  if (size < 16) return 0;
  struct sockaddr_in6 *sa6 = (struct sockaddr_in6*) src;
  memcpy(buf, &sa6->sin6_addr.__in6_u.__u6_addr8, 16);
  return 16;
 }
}

variable_types:
buf
char *


decompiled_code:
__int64 get_nr_cpus()
{
return sysconf(84);
}

canonical_decompiled_code:
__int64 func10()
{
return sysconf(84);
}

original_code:
int get_nr_cpus()
{
 return sysconf(_SC_NPROCESSORS_ONLN);
}


decompiled_code:
int __fastcall print_buffer(const char *a1, char *a2, int a3)
{
__int64 v3; // rbp
char *v4; // rbx
char *v5; // rbp
__int64 v6; // rsi

v3 = a3;
v4 = a2;
puts(a1);
if ( (int)v3 > 0 )
{
v5 = &a2[v3];
do
{
v6 = (unsigned int)*v4++;
printf("%d ", v6);
}
while ( v4 != v5 );
}
return puts(&s);
}

canonical_decompiled_code:
int __fastcall func11(const char *a1, char *a2, int a3)
{
__int64 v3; // rbp
char *v4; // rbx
char *v5; // rbp
__int64 v6; // rsi

v3 = a3;
v4 = a2;
puts(a1);
if ( (int)v3 > 0 )
{
v5 = &a2[v3];
do
{
v6 = (unsigned int)*v4++;
printf("%d ", v6);
}
while ( v4 != v5 );
}
return puts(&s);
}

original_code:
void print_buffer(const char* name, void* buf, int size)
{
 int i;
 char* buffer = buf;
 puts(name);
 for (i = 0; i < size; ++i) {
  printf("%d ", buffer[i]);
 }
 puts("");
}

canonical_original_code:
void print_buffer(char *name, void *buf, int size)
{
 int i;
 char *buffer = buf;
 puts(name);
 for (i = 0; i < size; ++i) {
  printf("%d ", buffer[i]);
 }
 puts("");
}

variable_types:
name
char *

variable_types:
buf
void *

variable_types: i int

variable_types:
buffer
char *


decompiled_code:
__int64 __fastcall sys_error(const char *a1)
{
perror(a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char *a1)
{
perror(a1);
return 0xFFFFFFFFLL;
}

original_code:
int sys_error(const char* msg)
{
 perror(msg);
 return -1;
}

canonical_original_code:
int sys_error(char *msg)
{
 perror(msg);
 return -1;
}

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall evt_server_threads(__int64 a1, int a2)
{
__int64 v2; // rbx
char *i; // rdi

if ( pool != -1 )
return 0xFFFFFFFFLL;
pool = a2;
qword_A28 = a1;
if ( a2 <= 0 )
return 0LL;
dword_A30 = 0;
v2 = 0LL;
ptr = calloc(a2, 0x48uLL);
for ( i = (char *)ptr; !pthread_create((pthread_t *)&i[v2], 0LL, dispatch_worker, &i[v2]); i = (char *)ptr )
{
v2 += 72LL;
if ( 72LL * a2 == v2 )
return (unsigned int)a2;
}
pool = 0;
free(ptr);
return sys_error("Failed to start threads.");
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, int a2)
{
__int64 v2; // rbx
char *i; // rdi

if ( pool != -1 )
return 0xFFFFFFFFLL;
pool = a2;
qword_A28 = a1;
if ( a2 <= 0 )
return 0LL;
dword_A30 = 0;
v2 = 0LL;
ptr = calloc(a2, 0x48uLL);
for ( i = (char *)ptr; !pthread_create((pthread_t *)&i[v2], 0LL, dispatch_worker, &i[v2]); i = (char *)ptr )
{
v2 += 72LL;
if ( 72LL * a2 == v2 )
return (unsigned int)a2;
}
pool = 0;
free(ptr);
return func12("Failed to start threads.");
}

original_code:
int evt_server_threads(evt_callback tcb, int nr)
{
 if (pool.nr != -1) return -1;
 pool.nr = nr;
 pool.slave = tcb;
 if (nr <= 0) return 0;
 pool.last_sched = 0;
 pool.conns = calloc(nr, sizeof(cli_queue));
 int i;
 int ret;
 for (i = 0; i < nr; ++i) {
  ret = pthread_create(&pool.conns[i].tid, ((void *)0),
         dispatch_worker, &pool.conns[i]);
  if (ret != 0) {
   pool.nr = 0;
   free(pool.conns);
   return sys_error("Failed to start threads.");
  }
 }
 return nr;
}

canonical_original_code:
int evt_server_threads(void (*tcb)(int fd, short evt, void *arg), int nr)
{
 if (pool.nr != -1) return -1;
 pool.nr = nr;
 pool.slave = tcb;
 if (nr <= 0) return 0;
 pool.last_sched = 0;
 pool.conns = calloc(nr, sizeof(cli_queue));
 int i;
 int ret;
 for (i = 0; i < nr; ++i) {
  ret = pthread_create(&pool.conns[i].tid, ((void *)0),
         dispatch_worker, &pool.conns[i]);
  if (ret != 0) {
   pool.nr = 0;
   free(pool.conns);
   return sys_error("Failed to start threads.");
  }
 }
 return nr;
}

variable_types:
tcb
void (*)(int fd, short evt, void *arg)

variable_types: nr int

variable_types: ret int


decompiled_code:
int __fastcall evt_tcp(int a1, struct sockaddr *a2)
{
int result; // eax

result = accept(a1, a2, (socklen_t *)&len);
if ( result == -1 )
result = sys_error("accept");
return result;
}

canonical_decompiled_code:
int __fastcall func14(int a1, struct sockaddr *a2)
{
int result; // eax

result = accept(a1, a2, (socklen_t *)&len);
if ( result == -1 )
result = func12("accept");
return result;
}

original_code:
int evt_tcp(sockfd_t fd, struct sockaddr* sock)
{
 sockfd_t conn = accept(fd, sock, &serv.socksize);
 if (conn == -1) {
  return sys_error("accept");
 }
 return conn;
}

canonical_original_code:
int evt_tcp(int fd, struct sockaddr *sock)
{
 int conn = accept(fd, sock, &serv.socksize);
 if (conn == -1) {
  return sys_error("accept");
 }
 return conn;
}

variable_types: fd int

variable_types: conn int


decompiled_code:
ssize_t __fastcall evt_udp(int a1, void *a2, int a3, int a4, struct sockaddr *a5)
{
ssize_t result; // rax

result = recvfrom(a1, a2, a3, a4, a5, (socklen_t *)&len);
if ( (_DWORD)result == -1 )
result = sys_error("recvfrom");
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func15(int a1, void *a2, int a3, int a4, struct sockaddr *a5)
{
ssize_t result; // rax

result = recvfrom(a1, a2, a3, a4, a5, (socklen_t *)&len);
if ( (_DWORD)result == -1 )
result = func12("recvfrom");
return result;
}

original_code:
int evt_udp(sockfd_t fd, char* buf, int size, int flags,
  struct sockaddr* sock)
{
 int len = recvfrom(fd, buf, size, flags, sock, &serv.socksize);
 if (len == -1) {
  return sys_error("recvfrom");
 }
 return len;
}

canonical_original_code:
int evt_udp(int fd, char *buf, int size, int flags, struct sockaddr *sock)
{
 int len = recvfrom(fd, buf, size, flags, sock, &serv.socksize);
 if (len == -1) {
  return sys_error("recvfrom");
 }
 return len;
}

variable_types: flags int

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

variable_types: len int


decompiled_code:
int __fastcall evt_close(int fd)
{
if ( shutdown(fd, 2) )
sys_error("evt_close: shutdown");
return close(fd);
}

canonical_decompiled_code:
int __fastcall func16(int fd)
{
if ( shutdown(fd, 2) )
func12("evt_close: shutdown");
return close(fd);
}

original_code:
void evt_close(sockfd_t fd)
{
 if (shutdown(fd, SHUT_RDWR) != 0) {
  sys_error("evt_close: shutdown");
 }
 close(fd);
}

canonical_original_code:
void evt_close(int fd)
{
 if (shutdown(fd, SHUT_RDWR) != 0) {
  sys_error("evt_close: shutdown");
 }
 close(fd);
}


getnetinfo

decompiled_code:
struct addrinfo *__fastcall getnetinfo(char *name, unsigned int a2, int a3)
{
int v3; // eax
int v5; // ebp
const char *v6; // rax
struct addrinfo *v7; // [rsp+0h] [rbp-48h] BYREF
char s[8]; // [rsp+8h] [rbp-40h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = a3;
req.ai_flags = 1;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
snprintf(s, 8uLL, "%d", a2);
v3 = getaddrinfo(name, s, &req, &v7);
if ( v3 )
{
v5 = v3;
sys_error("getaddrinfo");
v6 = gai_strerror(v5);
fprintf(stderr, "getaddrinfo %s\n", v6);
}
return v7;
}

canonical_decompiled_code:
struct addrinfo *__fastcall func17(char *name, unsigned int a2, int a3)
{
int v3; // eax
int v5; // ebp
const char *v6; // rax
struct addrinfo *v7; // [rsp+0h] [rbp-48h] BYREF
char s[8]; // [rsp+8h] [rbp-40h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-38h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
req.ai_socktype = a3;
req.ai_flags = 1;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
snprintf(s, 8uLL, "%d", a2);
v3 = getaddrinfo(name, s, &req, &v7);
if ( v3 )
{
v5 = v3;
func12("getaddrinfo");
v6 = gai_strerror(v5);
fprintf(stderr, "getaddrinfo %s\n", v6);
}
return v7;
}

original_code:
struct addrinfo* getnetinfo(const char* host, int port, int socktype)
{
 struct addrinfo hints;
 struct addrinfo* result;
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 0;
 hints.ai_socktype = socktype;
 hints.ai_flags = 0x0001;
 char portstr[8];
 snprintf(portstr, sizeof(portstr), "%d", port);
 int err;
 if ((err = getaddrinfo(host, portstr, &hints, &result)) != 0) {
  sys_error("getaddrinfo");
  fprintf(stderr, "getaddrinfo %s\n", gai_strerror(err));
 }
 return result;
}

canonical_original_code:
struct addrinfo *getnetinfo(char *host, int port, int socktype)
{
 struct addrinfo hints;
 struct addrinfo *result;
 memset(&hints, 0, sizeof(struct addrinfo));
 hints.ai_family = 0;
 hints.ai_socktype = socktype;
 hints.ai_flags = 0x0001;
 char portstr[8];
 snprintf(portstr, sizeof(portstr), "%d", port);
 int err;
 if ((err = getaddrinfo(host, portstr, &hints, &result)) != 0) {
  sys_error("getaddrinfo");
  fprintf(stderr, "getaddrinfo %s\n", gai_strerror(err));
 }
 return result;
}

variable_types:
host
char *

variable_types: port int

variable_types: socktype int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
portstr
char [8]

variable_types: err int


decompiled_code:
__int64 __fastcall evt_server_init(unsigned int a1, int a2)
{
struct addrinfo *v2; // rax

serv = a1;
dword_A44 = a2;
v2 = getnetinfo(0LL, a1, a2);
pool = -1;
ai = v2;
LODWORD(len) = v2->ai_addrlen;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned int a1, int a2)
{
struct addrinfo *v2; // rax

serv = a1;
dword_A44 = a2;
v2 = func17(0LL, a1, a2);
pool = -1;
ai = v2;
LODWORD(len) = v2->ai_addrlen;
return 0LL;
}

original_code:
int evt_server_init(int port, int socktype)
{
 serv.port = port;
 serv.socktype = socktype;
 serv.ai = getnetinfo(((void *)0), port, socktype);
 serv.socksize = serv.ai->ai_addrlen;
 pool.nr = -1;
 return 0;
}


decompiled_code:
__int64 __fastcall get_conn_to_host(char *a1, unsigned int a2, int a3)
{
struct addrinfo *v4; // rax
struct addrinfo *v5; // rbp
struct addrinfo *v6; // rbx
int v7; // eax
unsigned int v8; // er12
__int64 result; // rax

v4 = getnetinfo(a1, a2, a3);
v5 = v4;
if ( v4 )
{
v6 = v4;
while ( 1 )
{
v7 = socket(v6->ai_family, v6->ai_socktype, v6->ai_protocol);
v8 = v7;
if ( v7 >= 0 )
break;
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_7;
}
if ( a3 == 1 && connect(v7, v6->ai_addr, len) == -1 )
{
evt_close(v8);
freeaddrinfo(v5);
result = sys_error("connect");
}
else
{
freeaddrinfo(v5);
result = v8;
}
}
else
{
LABEL_7:
freeaddrinfo(v5);
result = sys_error("socket");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(char *a1, unsigned int a2, int a3)
{
struct addrinfo *v4; // rax
struct addrinfo *v5; // rbp
struct addrinfo *v6; // rbx
int v7; // eax
unsigned int v8; // er12
__int64 result; // rax

v4 = func17(a1, a2, a3);
v5 = v4;
if ( v4 )
{
v6 = v4;
while ( 1 )
{
v7 = socket(v6->ai_family, v6->ai_socktype, v6->ai_protocol);
v8 = v7;
if ( v7 >= 0 )
break;
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_7;
}
if ( a3 == 1 && connect(v7, v6->ai_addr, len) == -1 )
{
func16(v8);
freeaddrinfo(v5);
result = func12("connect");
}
else
{
freeaddrinfo(v5);
result = v8;
}
}
else
{
LABEL_7:
freeaddrinfo(v5);
result = func12("socket");
}
return result;
}

original_code:
int get_conn_to_host(const char* host, int port, int socktype)
{
 sockfd_t sock;
 struct addrinfo *rp, *result = getnetinfo(host, port, socktype);
 for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype,
         rp->ai_protocol);
  if (sock >= 0) break;
 }
 if (rp == ((void *)0)) {
  freeaddrinfo(result);
  return sys_error("socket");
 }
 if (socktype == SOCK_STREAM) {
  if (connect(sock, rp->ai_addr, serv.socksize) == -1) {
   evt_close(sock);
   freeaddrinfo(result);
   return sys_error("connect");
  }
 }
 freeaddrinfo(result);
 return sock;
}

canonical_original_code:
int get_conn_to_host(char *host, int port, int socktype)
{
 int sock;
 struct addrinfo *rp; struct addrinfo *result = getnetinfo(host, port, socktype);
 for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
  sock = socket(rp->ai_family, rp->ai_socktype,
         rp->ai_protocol);
  if (sock >= 0) break;
 }
 if (rp == ((void *)0)) {
  freeaddrinfo(result);
  return sys_error("socket");
 }
 if (socktype == SOCK_STREAM) {
  if (connect(sock, rp->ai_addr, serv.socksize) == -1) {
   evt_close(sock);
   freeaddrinfo(result);
   return sys_error("connect");
  }
 }
 freeaddrinfo(result);
 return sock;
}

variable_types: sock int


decompiled_code:
int __fastcall get_conn_to_addr(__int64 a1, __int16 a2, int a3)
{
bool v5; // zf
int v6; // edx
int result; // eax

v5 = a3 == 1;
v6 = 6;
if ( !v5 )
v6 = 17;
result = socket(ai->ai_family, a3, v6);
if ( result < 0 )
return sys_error("socket");
*(_WORD *)(a1 + 32) = a2;
return result;
}

canonical_decompiled_code:
int __fastcall func20(__int64 a1, __int16 a2, int a3)
{
bool v5; // zf
int v6; // edx
int result; // eax

v5 = a3 == 1;
v6 = 6;
if ( !v5 )
v6 = 17;
result = socket(ai->ai_family, a3, v6);
if ( result < 0 )
return func12("socket");
*(_WORD *)(a1 + 32) = a2;
return result;
}

original_code:
int get_conn_to_addr(struct sockaddr* addr, int port, int socktype)
{
 int ai_protocol = (socktype == SOCK_STREAM)
     ? IPPROTO_TCP : IPPROTO_UDP;
 sockfd_t fd = socket(serv.ai->ai_family, socktype, ai_protocol);
 if (fd < 0) {
  return sys_error("socket");
 }
 memcpy(addr + 2, &port, 2);
 return fd;
}

canonical_original_code:
int get_conn_to_addr(struct sockaddr *addr, int port, int socktype)
{
 int ai_protocol = (socktype == SOCK_STREAM)
     ? IPPROTO_TCP : IPPROTO_UDP;
 int fd = socket(serv.ai->ai_family, socktype, ai_protocol);
 if (fd < 0) {
  return sys_error("socket");
 }
 memcpy(addr + 2, &port, 2);
 return fd;
}


decompiled_code:
__int64 __fastcall set_nonblocking(int fd)
{
unsigned int v1; // eax
__int64 v2; // rdx
int v3; // er12

v1 = fcntl(fd, 3, 0LL);
v2 = v1;
BYTE1(v2) = BYTE1(v1) | 8;
if ( v1 == -1 )
v2 = 2048LL;
v3 = fcntl(fd, 4, v2);
if ( v3 < 0 )
sys_error("Setting O_NONBLOCK failed.");
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func21(int fd)
{
unsigned int v1; // eax
__int64 v2; // rdx
int v3; // er12

v1 = fcntl(fd, 3, 0LL);
v2 = v1;
BYTE1(v2) = BYTE1(v1) | 8;
if ( v1 == -1 )
v2 = 2048LL;
v3 = fcntl(fd, 4, v2);
if ( v3 < 0 )
func12("Setting O_NONBLOCK failed.");
return (unsigned int)v3;
}

original_code:
int set_nonblocking(sockfd_t fd)
{
 int flags;
 if ((flags = fcntl(fd, 3, 0)) == -1) {
  flags = 0;
 }
 int ret = fcntl(fd, 4, flags | 04000);
 if (ret < 0) {
  sys_error("Setting O_NONBLOCK failed.");
 }
 return ret;
}

canonical_original_code:
int set_nonblocking(int fd)
{
 int flags;
 if ((flags = fcntl(fd, 3, 0)) == -1) {
  flags = 0;
 }
 int ret = fcntl(fd, 4, flags | 04000);
 if (ret < 0) {
  sys_error("Setting O_NONBLOCK failed.");
 }
 return ret;
}


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

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

original_code:
client* cq_front(cli_queue* q)
{
 return q->head;
}

canonical_original_code:
struct client *cq_front(struct cli_queue *q)
{
 return q->head;
}

variable_types: struct cli_queue { unsigned long tid; int size; struct client *head; struct client *tail; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *

return_type: struct client { int conn; short evt; void *arg; struct client *next; } *

user_defined_types: struct cli_queue { unsigned long tid; int size; struct client *head; struct client *tail; union pthread_mutex_t lock; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


original_code:
void cq_push_back(cli_queue* q, sockfd_t conn, short evt, void* arg)
{
 client* p = malloc(sizeof(client));
 p->conn = conn;
 p->evt = evt;
 p->arg = arg;
 p->next = ((void *)0);
 pthread_mutex_lock(&q->lock);
 if (((void *)0) == q->head && ((void *)0) == q->tail) {
  q->head = q->tail = p;
 } else {
  q->tail->next = p;
  q->tail = p;
 }
 ++q->size;
 pthread_mutex_unlock(&q->lock);
}

canonical_original_code:
void cq_push_back(struct cli_queue *q, int conn, short evt, void *arg)
{
 struct client *p = malloc(sizeof(client));
 p->conn = conn;
 p->evt = evt;
 p->arg = arg;
 p->next = ((void *)0);
 pthread_mutex_lock(&q->lock);
 if (((void *)0) == q->head && ((void *)0) == q->tail) {
  q->head = q->tail = p;
 } else {
  q->tail->next = p;
  q->tail = p;
 }
 ++q->size;
 pthread_mutex_unlock(&q->lock);
}

variable_types: struct cli_queue { unsigned long tid; int size; struct client { int conn; short evt; void *arg; struct client *next; } *head; struct client *tail; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; } *

variable_types: evt short

variable_types:
arg
void *


decompiled_code:
void evt_server_stop()
{
void *i; // rdi
__int64 v1; // rbp

if ( (unsigned int)event_loopexit(&min_time) )
event_loopbreak();
if ( pool > 0 )
{
i = ptr;
v1 = 0LL;
do
{
cq_push_back((__int64)i + 72 * v1, -1, 0, 0LL);
for ( i = ptr; *((int *)ptr + 18 * v1 + 2) > 1; i = ptr )
usleep(0x3E8u);
++v1;
}
while ( pool > (int)v1 );
free(i);
}
freeaddrinfo(ai);
}

canonical_decompiled_code:
void func25()
{
void *i; // rdi
__int64 v1; // rbp

if ( (unsigned int)event_loopexit(&min_time) )
event_loopbreak();
if ( pool > 0 )
{
i = ptr;
v1 = 0LL;
do
{
func24((__int64)i + 72 * v1, -1, 0, 0LL);
for ( i = ptr; *((int *)ptr + 18 * v1 + 2) > 1; i = ptr )
usleep(0x3E8u);
++v1;
}
while ( pool > (int)v1 );
free(i);
}
freeaddrinfo(ai);
}

original_code:
void evt_server_stop()
{
 if (event_loopexit(&min_time) != 0) {
  event_loopbreak();
 }
 if (pool.nr > 0) {
  int i;
  for (i = 0; i < pool.nr; ++i) {
   cq_push_back(&pool.conns[i], -1, 0, ((void *)0));
   while (pool.conns[i].size > 1) {
    usleep(1000);
   }
  }
  free(pool.conns);
 }
 freeaddrinfo(serv.ai);
}

variable_types: i int


decompiled_code:
int __fastcall evt_thread_dispatch(int a1, __int16 a2, __int64 a3)
{
__int64 v3; // rbx
int result; // eax

v3 = (dword_A30 + 1) % pool;
result = cq_push_back((__int64)ptr + 72 * v3, a1, a2, a3);
dword_A30 = v3;
return result;
}

canonical_decompiled_code:
int __fastcall func26(int a1, __int16 a2, __int64 a3)
{
__int64 v3; // rbx
int result; // eax

v3 = (dword_A30 + 1) % pool;
result = func24((__int64)ptr + 72 * v3, a1, a2, a3);
dword_A30 = v3;
return result;
}

original_code:
void evt_thread_dispatch(sockfd_t conn, short evt, void* arg)
{
 int i = (pool.last_sched + 1) % pool.nr;
 cq_push_back(&pool.conns[i], conn, evt, arg);
 pool.last_sched = i;
}

canonical_original_code:
void evt_thread_dispatch(int conn, short evt, void *arg)
{
 int i = (pool.last_sched + 1) % pool.nr;
 cq_push_back(&pool.conns[i], conn, evt, arg);
 pool.last_sched = i;
}


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

result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL);
*(_QWORD *)(a1 + 16) = result;
--*(_DWORD *)(a1 + 8);
if ( !result )
*(_QWORD *)(a1 + 24) = 0LL;
return result;
}

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

result = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL);
*(_QWORD *)(a1 + 16) = result;
--*(_DWORD *)(a1 + 8);
if ( !result )
*(_QWORD *)(a1 + 24) = 0LL;
return result;
}

original_code:
void cq_pop_front(cli_queue* q)
{
 client* h = q->head;
 client* p = h->next;
 q->head = p;
 if (((void *)0) == q->head) {
  q->tail = ((void *)0);
 }
 --q->size;
}

canonical_original_code:
void cq_pop_front(struct cli_queue *q)
{
 struct client *h = q->head;
 struct client *p = h->next;
 q->head = p;
 if (((void *)0) == q->head) {
  q->tail = ((void *)0);
 }
 --q->size;
}

variable_types: struct client { int conn; short evt; void *arg; struct client *next; } *

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
void *__fastcall dispatch_worker(void *a1)
{
pthread_mutex_t *v1; // r12
unsigned int *v3; // rbp
__int64 v4; // rdi

v1 = (pthread_mutex_t *)((char *)a1 + 32);
*((_DWORD *)a1 + 2) = 0;
*((_QWORD *)a1 + 3) = 0LL;
*((_QWORD *)a1 + 2) = 0LL;
pthread_mutex_init((pthread_mutex_t *)((char *)a1 + 32), 0LL);
while ( 1 )
{
pthread_mutex_lock(v1);
if ( *((int *)a1 + 2) > 0 )
break;
LABEL_5:
pthread_mutex_unlock(v1);
usleep(0x3E8u);
}
while ( 1 )
{
v3 = (unsigned int *)cq_front((__int64)a1);
cq_pop_front((__int64)a1);
pthread_mutex_unlock(v1);
v4 = *v3;
if ( (_DWORD)v4 == -1 )
break;
qword_A28(v4, (unsigned int)*((__int16 *)v3 + 2), *((_QWORD *)v3 + 1));
free(v3);
pthread_mutex_lock(v1);
if ( *((int *)a1 + 2) <= 0 )
goto LABEL_5;
}
free(v3);
pthread_mutex_destroy(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func28(void *a1)
{
pthread_mutex_t *v1; // r12
unsigned int *v3; // rbp
__int64 v4; // rdi

v1 = (pthread_mutex_t *)((char *)a1 + 32);
*((_DWORD *)a1 + 2) = 0;
*((_QWORD *)a1 + 3) = 0LL;
*((_QWORD *)a1 + 2) = 0LL;
pthread_mutex_init((pthread_mutex_t *)((char *)a1 + 32), 0LL);
while ( 1 )
{
pthread_mutex_lock(v1);
if ( *((int *)a1 + 2) > 0 )
break;
LABEL_5:
pthread_mutex_unlock(v1);
usleep(0x3E8u);
}
while ( 1 )
{
v3 = (unsigned int *)func23((__int64)a1);
func27((__int64)a1);
pthread_mutex_unlock(v1);
v4 = *v3;
if ( (_DWORD)v4 == -1 )
break;
qword_A28(v4, (unsigned int)*((__int16 *)v3 + 2), *((_QWORD *)v3 + 1));
free(v3);
pthread_mutex_lock(v1);
if ( *((int *)a1 + 2) <= 0 )
goto LABEL_5;
}
free(v3);
pthread_mutex_destroy(v1);
return 0LL;
}

original_code:
void* dispatch_worker(void* arg)
{
 cli_queue* q = arg;
 q->size = 0;
 q->head = q->tail = ((void *)0);
 pthread_mutex_init(&q->lock, ((void *)0));
 while (1) {
  pthread_mutex_lock(&q->lock);
  if (q->size > 0) {
   client* cli = cq_front(q);
   cq_pop_front(q);
   pthread_mutex_unlock(&q->lock);
   if (cli->conn == -1) {
    free(cli);
    pthread_mutex_destroy(&q->lock);
    return ((void *)0);
   } else {
    pool.slave(cli->conn, cli->evt,
        cli->arg);
    free(cli);
   }
  } else {
   pthread_mutex_unlock(&q->lock);
   usleep(1000);
  }
 }
 return ((void *)0);
}

canonical_original_code:
void *dispatch_worker(void *arg)
{
 struct cli_queue *q = arg;
 q->size = 0;
 q->head = q->tail = ((void *)0);
 pthread_mutex_init(&q->lock, ((void *)0));
 while (1) {
  pthread_mutex_lock(&q->lock);
  if (q->size > 0) {
   struct client *cli = cq_front(q);
   cq_pop_front(q);
   pthread_mutex_unlock(&q->lock);
   if (cli->conn == -1) {
    free(cli);
    pthread_mutex_destroy(&q->lock);
    return ((void *)0);
   } else {
    pool.slave(cli->conn, cli->evt,
        cli->arg);
    free(cli);
   }
  } else {
   pthread_mutex_unlock(&q->lock);
   usleep(1000);
  }
 }
 return ((void *)0);
}

return_type: void *

user_defined_types: struct client { int conn; short evt; void *arg; struct client *next; }


pushbyte

repo: vegard/rgbds-linux

decompiled_code:
__int64 __fastcall pushbyte(__int64 a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 264);
*(_QWORD *)(a1 + 264) = result + 1;
*(_BYTE *)(a1 + result + 8) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 264);
*(_QWORD *)(a1 + 264) = result + 1;
*(_BYTE *)(a1 + result + 8) = a2;
return result;
}

original_code:
void pushbyte(struct Expression *expr, int b)
{
 expr->tRPN[expr->nRPNLength++] = b & 0xFF;
}

variable_types: struct Expression { long nVal; unsigned char tRPN[256]; unsigned long nRPNLength; unsigned long nRPNOut; unsigned long isReloc; unsigned long isPCRel; } *

variable_types: b int


original_code:
UWORD rpn_PopByte(struct Expression *expr)
{
 if (expr->nRPNOut == expr->nRPNLength) {
  return (0xDEAD);
 } else
  return (expr->tRPN[expr->nRPNOut++]);
}

canonical_original_code:
unsigned short rpn_PopByte(struct Expression *expr)
{
 if (expr->nRPNOut == expr->nRPNLength) {
  return (0xDEAD);
 } else
  return (expr->tRPN[expr->nRPNOut++]);
}

return_type: unsigned short

user_defined_types: struct Expression { long nVal; unsigned char tRPN[256]; unsigned long nRPNLength; unsigned long nRPNOut; unsigned long isReloc; unsigned long isPCRel; }


decompiled_code:
__int64 __fastcall rpn_isReloc(__int64 a1)
{
return *(_QWORD *)(a1 + 280);
}

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

original_code:
ULONG rpn_isReloc(struct Expression * expr)
{
 return (expr->isReloc);
}

canonical_original_code:
unsigned long rpn_isReloc(struct Expression *expr)
{
 return (expr->isReloc);
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall rpn_isPCRelative(__int64 a1)
{
return *(_QWORD *)(a1 + 288);
}

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

original_code:
ULONG rpn_isPCRelative(struct Expression * expr)
{
 return (expr->isPCRel);
}

canonical_original_code:
unsigned long rpn_isPCRelative(struct Expression *expr)
{
 return (expr->isPCRel);
}


decompiled_code:
__int64 __fastcall rpn_RangeCheck(void *a1, const void *a2)
{
_QWORD *v2; // r8
__int64 v3; // r10
__int64 v4; // r11
__int64 v6; // rdi
char v7; // r10
char v8; // r10^1
char v9; // er10^2
char v10; // er10^3
char v11; // r11
char v12; // r11^1
char v13; // er11^2
char v14; // er11^3

qmemcpy(a1, a2, 0x128uLL);
if ( !rpn_isReloc((__int64)a2) )
return (*v2 <= v4) & (unsigned __int8)(*v2 >= v3);
v6 = (__int64)v2;
pushbyte((__int64)v2, 24);
pushbyte(v6, v7);
pushbyte(v6, v8);
pushbyte(v6, v9);
pushbyte(v6, v10);
pushbyte(v6, v11);
pushbyte(v6, v12);
pushbyte(v6, v13);
pushbyte(v6, v14);
return 1LL;
}

original_code:
int rpn_RangeCheck(struct Expression *expr, struct Expression *src, SLONG low,
     SLONG high)
{
 *expr = *src;
 if (rpn_isReloc(src)) {
  pushbyte(expr, RPN_RANGECHECK);
  pushbyte(expr, low);
  pushbyte(expr, low >> 8);
  pushbyte(expr, low >> 16);
  pushbyte(expr, low >> 24);
  pushbyte(expr, high);
  pushbyte(expr, high >> 8);
  pushbyte(expr, high >> 16);
  pushbyte(expr, high >> 24);
  return (1);
 } else {
  return (expr->nVal >= low && expr->nVal <= high);
 }
}

canonical_original_code:
int rpn_RangeCheck(struct Expression *expr, struct Expression *src, long low, long high)
{
 *expr = *src;
 if (rpn_isReloc(src)) {
  pushbyte(expr, RPN_RANGECHECK);
  pushbyte(expr, low);
  pushbyte(expr, low >> 8);
  pushbyte(expr, low >> 16);
  pushbyte(expr, low >> 24);
  pushbyte(expr, high);
  pushbyte(expr, high >> 8);
  pushbyte(expr, high >> 16);
  pushbyte(expr, high >> 24);
  return (1);
 } else {
  return (expr->nVal >= low && expr->nVal <= high);
 }
}

variable_types:
src
struct Expression *

variable_types: low long

variable_types: high long


decompiled_code:
__int64 __fastcall rpn_CheckHRAM(void *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
return pushbyte((__int64)a1, 22);
}

canonical_decompiled_code:
__int64 __fastcall func10(void *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
return func1((__int64)a1, 22);
}

original_code:
void rpn_CheckHRAM(struct Expression *expr, struct Expression *src)
{
 *expr = *src;
 pushbyte(expr, RPN_HRAM);
}


decompiled_code:
__int64 __fastcall rpn_LOGNOT(void *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
return pushbyte((__int64)a1, 12);
}

canonical_decompiled_code:
__int64 __fastcall func11(void *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
return func1((__int64)a1, 12);
}

original_code:
void rpn_LOGNOT(struct Expression *expr, struct Expression *src)
{
 *expr = *src;
 pushbyte(expr, RPN_LOGUNNOT);
}


decompiled_code:
__int64 __fastcall rpn_LOGOR(_BOOL8 *a1, const void *a2, size_t *a3)
{
_BOOL8 v4; // rax

mergetwoexpressions(a1, a2, a3);
v4 = 1LL;
if ( !*a1 )
v4 = *a3 != 0;
*a1 = v4;
return pushbyte((__int64)a1, 11);
}

canonical_decompiled_code:
__int64 __fastcall func12(_BOOL8 *a1, const void *a2, size_t *a3)
{
_BOOL8 v4; // rax

func0(a1, a2, a3);
v4 = 1LL;
if ( !*a1 )
v4 = *a3 != 0;
*a1 = v4;
return func1((__int64)a1, 11);
}

original_code:
void rpn_LOGOR(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal || src2->nVal);
 pushbyte(expr, RPN_LOGOR);
}


decompiled_code:
__int64 __fastcall rpn_LOGAND(__int64 *a1, const void *a2, size_t *a3)
{
__int64 v4; // rax

mergetwoexpressions(a1, a2, a3);
v4 = *a1;
if ( *a1 )
v4 = *a3 != 0;
*a1 = v4;
return pushbyte((__int64)a1, 10);
}

original_code:
void rpn_LOGAND(struct Expression *expr, struct Expression *src1,
  struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal && src2->nVal);
 pushbyte(expr, RPN_LOGAND);
}


decompiled_code:
__int64 __fastcall rpn_LOGEQU(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 == *a3;
return pushbyte((__int64)a1, 13);
}

canonical_decompiled_code:
__int64 __fastcall func14(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 == *a3;
return func1((__int64)a1, 13);
}

original_code:
void rpn_LOGEQU(struct Expression *expr, struct Expression *src1,
  struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal == src2->nVal);
 pushbyte(expr, RPN_LOGEQ);
}


decompiled_code:
__int64 __fastcall rpn_LOGGT(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 > (__int64)*a3;
return pushbyte((__int64)a1, 15);
}

canonical_decompiled_code:
__int64 __fastcall func15(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 > (__int64)*a3;
return func1((__int64)a1, 15);
}

original_code:
void rpn_LOGGT(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal > src2->nVal);
 pushbyte(expr, RPN_LOGGT);
}


decompiled_code:
__int64 __fastcall rpn_LOGLT(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 < (__int64)*a3;
return pushbyte((__int64)a1, 16);
}

canonical_decompiled_code:
__int64 __fastcall func16(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 < (__int64)*a3;
return func1((__int64)a1, 16);
}

original_code:
void rpn_LOGLT(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal < src2->nVal);
 pushbyte(expr, RPN_LOGLT);
}


decompiled_code:
__int64 __fastcall rpn_LOGGE(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 >= (__int64)*a3;
return pushbyte((__int64)a1, 17);
}

canonical_decompiled_code:
__int64 __fastcall func17(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 >= (__int64)*a3;
return func1((__int64)a1, 17);
}

original_code:
void rpn_LOGGE(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal >= src2->nVal);
 pushbyte(expr, RPN_LOGGE);
}


decompiled_code:
__int64 __fastcall rpn_LOGLE(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 <= (__int64)*a3;
return pushbyte((__int64)a1, 18);
}

canonical_decompiled_code:
__int64 __fastcall func18(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 <= (__int64)*a3;
return func1((__int64)a1, 18);
}

original_code:
void rpn_LOGLE(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal <= src2->nVal);
 pushbyte(expr, RPN_LOGLE);
}


decompiled_code:
__int64 __fastcall rpn_LOGNE(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 = *a1 != *a3;
return pushbyte((__int64)a1, 14);
}

canonical_decompiled_code:
__int64 __fastcall func19(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 = *a1 != *a3;
return func1((__int64)a1, 14);
}

original_code:
void rpn_LOGNE(struct Expression *expr, struct Expression *src1,
        struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal != src2->nVal);
 pushbyte(expr, RPN_LOGNE);
}


decompiled_code:
__int64 __fastcall rpn_ADD(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 += *a3;
return pushbyte((__int64)a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func20(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 += *a3;
return func1((__int64)a1, 0);
}

original_code:
void rpn_ADD(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal + src2->nVal);
 pushbyte(expr, RPN_ADD);
}


decompiled_code:
__int64 __fastcall rpn_SUB(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 -= *a3;
return pushbyte((__int64)a1, 1);
}

canonical_decompiled_code:
__int64 __fastcall func21(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 -= *a3;
return func1((__int64)a1, 1);
}

original_code:
void rpn_SUB(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal - src2->nVal);
 pushbyte(expr, RPN_SUB);
}


decompiled_code:
__int64 __fastcall rpn_XOR(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 ^= *a3;
return pushbyte((__int64)a1, 8);
}

canonical_decompiled_code:
__int64 __fastcall func22(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 ^= *a3;
return func1((__int64)a1, 8);
}

original_code:
void rpn_XOR(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal ^ src2->nVal);
 pushbyte(expr, RPN_XOR);
}


decompiled_code:
__int64 __fastcall rpn_OR(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 |= *a3;
return pushbyte((__int64)a1, 6);
}

canonical_decompiled_code:
__int64 __fastcall func23(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 |= *a3;
return func1((__int64)a1, 6);
}

original_code:
void rpn_OR(struct Expression *expr, struct Expression *src1,
     struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal | src2->nVal);
 pushbyte(expr, RPN_OR);
}


decompiled_code:
__int64 __fastcall rpn_AND(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 &= *a3;
return pushbyte((__int64)a1, 7);
}

canonical_decompiled_code:
__int64 __fastcall func24(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 &= *a3;
return func1((__int64)a1, 7);
}

original_code:
void rpn_AND(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal & src2->nVal);
 pushbyte(expr, RPN_AND);
}


decompiled_code:
__int64 __fastcall rpn_SHL(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 <<= *a3;
return pushbyte((__int64)a1, 19);
}

canonical_decompiled_code:
__int64 __fastcall func25(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 <<= *a3;
return func1((__int64)a1, 19);
}

original_code:
void rpn_SHL(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal << src2->nVal);
 pushbyte(expr, RPN_SHL);
}


decompiled_code:
__int64 __fastcall rpn_SHR(__int64 *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 >>= *a3;
return pushbyte((__int64)a1, 20);
}

canonical_decompiled_code:
__int64 __fastcall func26(__int64 *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 >>= *a3;
return func1((__int64)a1, 20);
}

original_code:
void rpn_SHR(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal >> src2->nVal);
 pushbyte(expr, RPN_SHR);
}


decompiled_code:
__int64 __fastcall rpn_MUL(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 *= *a3;
return pushbyte((__int64)a1, 2);
}

canonical_decompiled_code:
__int64 __fastcall func27(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 *= *a3;
return func1((__int64)a1, 2);
}

original_code:
void rpn_MUL(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal * src2->nVal);
 pushbyte(expr, RPN_MUL);
}


decompiled_code:
__int64 __fastcall rpn_DIV(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 /= (__int64)*a3;
return pushbyte((__int64)a1, 3);
}

canonical_decompiled_code:
__int64 __fastcall func28(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 /= (__int64)*a3;
return func1((__int64)a1, 3);
}

original_code:
void rpn_DIV(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal / src2->nVal);
 pushbyte(expr, RPN_DIV);
}


decompiled_code:
__int64 __fastcall rpn_MOD(_QWORD *a1, const void *a2, size_t *a3)
{
mergetwoexpressions(a1, a2, a3);
*a1 %= (__int64)*a3;
return pushbyte((__int64)a1, 4);
}

canonical_decompiled_code:
__int64 __fastcall func29(_QWORD *a1, const void *a2, size_t *a3)
{
func0(a1, a2, a3);
*a1 %= (__int64)*a3;
return func1((__int64)a1, 4);
}

original_code:
void rpn_MOD(struct Expression *expr, struct Expression *src1,
      struct Expression *src2)
{
 mergetwoexpressions(expr,src1,src2);
 expr->nVal = (expr->nVal % src2->nVal);
 pushbyte(expr, RPN_MOD);
}


decompiled_code:
__int64 __fastcall rpn_UNNEG(_QWORD *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
*a1 = -*a1;
return pushbyte((__int64)a1, 5);
}

canonical_decompiled_code:
__int64 __fastcall func30(_QWORD *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
*a1 = -*a1;
return func1((__int64)a1, 5);
}

original_code:
void rpn_UNNEG(struct Expression *expr, struct Expression *src)
{
 *expr = *src;
 expr->nVal = -expr->nVal;
 pushbyte(expr, RPN_UNSUB);
}


decompiled_code:
__int64 __fastcall rpn_UNNOT(_QWORD *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
*a1 ^= 0xFFFFFFFFuLL;
return pushbyte((__int64)a1, 9);
}

canonical_decompiled_code:
__int64 __fastcall func31(_QWORD *a1, const void *a2)
{
qmemcpy(a1, a2, 0x128uLL);
*a1 ^= 0xFFFFFFFFuLL;
return func1((__int64)a1, 9);
}

original_code:
void rpn_UNNOT(struct Expression *expr, struct Expression *src)
{
 *expr = *src;
 expr->nVal = expr->nVal ^ 0xFFFFFFFF;
 pushbyte(expr, RPN_UNNOT);
}


decompiled_code:
__int64 __fastcall area_Avail(__int64 a1)
{
__int64 v1; // rax
__int64 i; // r8

v1 = BankFree[a1];
for ( i = 0LL; v1; v1 = *(_QWORD *)(v1 + 24) )
i += *(_QWORD *)(v1 + 8);
return i;
}

original_code:
SLONG area_Avail(SLONG bank)
{
 SLONG r;
 struct sFreeArea *pArea;
 r = 0;
 pArea = BankFree[bank];
 while (pArea) {
  r += pArea->nSize;
  pArea = pArea->pNext;
 }
 return (r);
}

canonical_original_code:
long area_Avail(long bank)
{
 long r;
 struct sFreeArea *pArea;
 r = 0;
 pArea = BankFree[bank];
 while (pArea) {
  r += pArea->nSize;
  pArea = pArea->pNext;
 }
 return (r);
}

variable_types: bank long

variable_types: r long

variable_types: struct sFreeArea { long nOrg; long nSize; struct sFreeArea *pPrev; } *

return_type: long


original_code:
SLONG area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size)
{
 struct sFreeArea *pArea;
 pArea = *ppArea;
 while (pArea) {
  if (org >= pArea->nOrg
      && (org + size - 1) <= (pArea->nOrg + pArea->nSize - 1)) {
   if (org == pArea->nOrg) {
    pArea->nOrg += size;
    pArea->nSize -= size;
    return (org);
   } else {
    if ((org + size - 1) ==
        (pArea->nOrg + pArea->nSize - 1)) {
     pArea->nSize -= size;
     return (org);
    } else {
     struct sFreeArea *pNewArea;
     if ((pNewArea =
          (struct sFreeArea *)
          malloc(sizeof(struct sFreeArea)))
         != ((void *)0)) {
      *pNewArea = *pArea;
      pNewArea->pPrev = pArea;
      pArea->pNext = pNewArea;
      pArea->nSize =
          org - pArea->nOrg;
      pNewArea->nOrg = org + size;
      pNewArea->nSize -=
          size + pArea->nSize;
      return (org);
     } else
      fatalerror("Out of memory!");
    }
   }
  }
  ppArea = &(pArea->pNext);
  pArea = *ppArea;
 }
 return (-1);
}

canonical_original_code:
long area_AllocAbs(struct sFreeArea **ppArea, long org, long size)
{
 struct sFreeArea *pArea;
 pArea = *ppArea;
 while (pArea) {
  if (org >= pArea->nOrg
      && (org + size - 1) <= (pArea->nOrg + pArea->nSize - 1)) {
   if (org == pArea->nOrg) {
    pArea->nOrg += size;
    pArea->nSize -= size;
    return (org);
   } else {
    if ((org + size - 1) ==
        (pArea->nOrg + pArea->nSize - 1)) {
     pArea->nSize -= size;
     return (org);
    } else {
     struct sFreeArea *pNewArea;
     if ((pNewArea =
          (struct sFreeArea *)
          malloc(sizeof(struct sFreeArea)))
         != ((void *)0)) {
      *pNewArea = *pArea;
      pNewArea->pPrev = pArea;
      pArea->pNext = pNewArea;
      pArea->nSize =
          org - pArea->nOrg;
      pNewArea->nOrg = org + size;
      pNewArea->nSize -=
          size + pArea->nSize;
      return (org);
     } else
      fatalerror("Out of memory!");
    }
   }
  }
  ppArea = &(pArea->pNext);
  pArea = *ppArea;
 }
 return (-1);
}

variable_types: struct sFreeArea { long nOrg; long nSize; struct sFreeArea *pPrev; } **

variable_types: org long

variable_types: size long


decompiled_code:
__int64 __fastcall area_AllocAbsCODEAnyBank(__int64 a1, __int64 a2)
{
__int64 i; // r12

for ( i = 1LL; i != 256; ++i )
{
if ( area_AllocAbs((__m128i **)&BankFree[i], a1, a2) == a1 )
return i;
}
return -1LL;
}

original_code:
SLONG area_AllocAbsCODEAnyBank(SLONG org, SLONG size)
{
 SLONG i;
 for (i = 1; i <= 255; i += 1) {
  if (area_AllocAbs(&BankFree[i], org, size) == org)
   return (i);
 }
 return (-1);
}

canonical_original_code:
long area_AllocAbsCODEAnyBank(long org, long size)
{
 long i;
 for (i = 1; i <= 255; i += 1) {
  if (area_AllocAbs(&BankFree[i], org, size) == org)
   return (i);
 }
 return (-1);
}

variable_types: i long


original_code:
SLONG area_Alloc(struct sFreeArea ** ppArea, SLONG size)
{
 struct sFreeArea *pArea;
 pArea = *ppArea;
 while (pArea) {
  if (size <= pArea->nSize) {
   SLONG r;
   r = pArea->nOrg;
   pArea->nOrg += size;
   pArea->nSize -= size;
   return (r);
  }
  ppArea = &(pArea->pNext);
  pArea = *ppArea;
 }
 return (-1);
}

canonical_original_code:
long area_Alloc(struct sFreeArea **ppArea, long size)
{
 struct sFreeArea *pArea;
 pArea = *ppArea;
 while (pArea) {
  if (size <= pArea->nSize) {
   long r;
   r = pArea->nOrg;
   pArea->nOrg += size;
   pArea->nSize -= size;
   return (r);
  }
  ppArea = &(pArea->pNext);
  pArea = *ppArea;
 }
 return (-1);
}

user_defined_types: struct sFreeArea { long nOrg; long nSize; struct sFreeArea *pPrev; }


decompiled_code:
__int64 __fastcall area_AllocCODEAnyBank(__int64 a1)
{
__int64 v1; // r9
__int64 result; // rax
__int64 v3; // r9

v1 = 1LL;
while ( 1 )
{
result = area_Alloc((__int64 **)&BankFree[v1], a1);
if ( result != -1 )
break;
v1 = v3 + 1;
if ( v1 == 256 )
return result;
}
return (v3 << 16) | result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // r9
__int64 result; // rax
__int64 v3; // r9

v1 = 1LL;
while ( 1 )
{
result = func3((__int64 **)&BankFree[v1], a1);
if ( result != -1 )
break;
v1 = v3 + 1;
if ( v1 == 256 )
return result;
}
return (v3 << 16) | result;
}

original_code:
SLONG area_AllocCODEAnyBank(SLONG size)
{
 SLONG i, org;
 for (i = 1; i <= 255; i += 1) {
  if ((org = area_Alloc(&BankFree[i], size)) != -1)
   return ((i << 16) | org);
 }
 return (-1);
}

canonical_original_code:
long area_AllocCODEAnyBank(long size)
{
 long i; long org;
 for (i = 1; i <= 255; i += 1) {
  if ((org = area_Alloc(&BankFree[i], size)) != -1)
   return ((i << 16) | org);
 }
 return (-1);
}


FindLargestCode

original_code:
struct sSection *FindLargestCode(void)
{
 struct sSection *pSection, *r = ((void *)0);
 SLONG nLargest = 0;
 pSection = pSections;
 while (pSection) {
  if (pSection->oAssigned == 0 && pSection->Type == SECT_CODE) {
   if (pSection->nByteSize > nLargest) {
    nLargest = pSection->nByteSize;
    r = pSection;
   }
  }
  pSection = pSection->pNext;
 }
 return (r);
}

canonical_original_code:
struct sSection *FindLargestCode()
{
 struct sSection *pSection; struct sSection *r = ((void *)0);
 long nLargest = 0;
 pSection = pSections;
 while (pSection) {
  if (pSection->oAssigned == 0 && pSection->Type == SECT_CODE) {
   if (pSection->nByteSize > nLargest) {
    nLargest = pSection->nByteSize;
    r = pSection;
   }
  }
  pSection = pSection->pNext;
 }
 return (r);
}

variable_types: struct sSection { long nBank; long nOrg; char oAssigned; long nByteSize; enum eSectionType { SECT_BSS=0, SECT_VRAM=1, SECT_CODE=2, SECT_HOME=3, SECT_HRAM=4 } Type; unsigned char *pData; long nNumberOfSymbols; struct sSymbol { char *pzName; enum eSymbolType { SYM_LOCAL=0, SYM_IMPORT=1, SYM_EXPORT=2 } Type; long nSectionID; struct sSection *pSection; long nOffset; } **tSymbols; struct sPatch { char *pzFilename; long nLineNo; long nOffset; enum ePatchType { PATCH_BYTE=0, PATCH_WORD_L=1, PATCH_LONG_L=2, PATCH_WORD_B=3, PATCH_LONG_B=4 } Type; long nRPNSize; unsigned char *pRPN; struct sPatch *pNext; char oRelocPatch; } *pPatches; struct sSection *pNext; } *

variable_types: nLargest long

user_defined_types: struct sSection { long nBank; long nOrg; char oAssigned; long nByteSize; enum eSectionType Type; unsigned char *pData; long nNumberOfSymbols; struct sSymbol **tSymbols; struct sPatch *pPatches; struct sSection *pNext; }

user_defined_types: struct sPatch { char *pzFilename; long nLineNo; long nOffset; enum ePatchType Type; long nRPNSize; unsigned char *pRPN; struct sPatch *pNext; char oRelocPatch; }

user_defined_types: struct sSymbol { char *pzName; enum eSymbolType Type; long nSectionID; struct sSection *pSection; long nOffset; }


AssignCodeSections

decompiled_code:
__int64 AssignCodeSections()
{
__int64 result; // rax
__int64 v1; // r10
__int64 v2; // rax
__int64 v3; // r10
__int64 v4; // rdx
__int64 v5; // rax
bool v6; // cc

LABEL_1:
while ( 1 )
{
result = FindLargestCode();
v1 = result;
if ( !result )
return result;
while ( 1 )
{
v2 = area_AllocCODEAnyBank(*(_QWORD *)(v1 + 24));
if ( v2 == -1 )
break;
v4 = (unsigned __int16)v2;
v5 = v2 >> 16;
v6 = v5 <= MaxBankUsed;
*(_BYTE *)(v3 + 16) = 1;
*(_QWORD *)(v3 + 8) = v4;
*(_QWORD *)v3 = v5;
if ( v6 )
goto LABEL_1;
MaxBankUsed = v5;
result = FindLargestCode();
v1 = result;
if ( !result )
return result;
}
fatalerror("Unable to place CODE section anywhere");
}
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
__int64 v1; // r10
__int64 v2; // rax
__int64 v3; // r10
__int64 v4; // rdx
__int64 v5; // rax
bool v6; // cc

LABEL_1:
while ( 1 )
{
result = func5();
v1 = result;
if ( !result )
return result;
while ( 1 )
{
v2 = func4(*(_QWORD *)(v1 + 24));
if ( v2 == -1 )
break;
v4 = (unsigned __int16)v2;
v5 = v2 >> 16;
v6 = v5 <= MaxBankUsed;
*(_BYTE *)(v3 + 16) = 1;
*(_QWORD *)(v3 + 8) = v4;
*(_QWORD *)v3 = v5;
if ( v6 )
goto LABEL_1;
MaxBankUsed = v5;
result = func5();
v1 = result;
if ( !result )
return result;
}
fatalerror("Unable to place CODE section anywhere");
}
}

original_code:
void AssignCodeSections(void)
{
 struct sSection *pSection;
 while ((pSection = FindLargestCode())) {
  SLONG org;
  if ((org = area_AllocCODEAnyBank(pSection->nByteSize)) != -1) {
   pSection->nOrg = org & 0xFFFF;
   pSection->nBank = org >> 16;
   pSection->oAssigned = 1;
   {if( (pSection->nBank)>MaxBankUsed ) MaxBankUsed=(pSection->nBank);};
  } else
   fatalerror("Unable to place CODE section anywhere");
 }
}

canonical_original_code:
void AssignCodeSections()
{
 struct sSection *pSection;
 while ((pSection = FindLargestCode())) {
  long org;
  if ((org = area_AllocCODEAnyBank(pSection->nByteSize)) != -1) {
   pSection->nOrg = org & 0xFFFF;
   pSection->nBank = org >> 16;
   pSection->oAssigned = 1;
   {if( (pSection->nBank)>MaxBankUsed ) MaxBankUsed=(pSection->nBank);};
  } else
   fatalerror("Unable to place CODE section anywhere");
 }
}

user_defined_types: enum eSectionType { SECT_BSS=0, SECT_VRAM=1, SECT_CODE=2, SECT_HOME=3, SECT_HRAM=4 }

user_defined_types: enum ePatchType { PATCH_BYTE=0, PATCH_WORD_L=1, PATCH_LONG_L=2, PATCH_WORD_B=3, PATCH_LONG_B=4 }

user_defined_types: enum eSymbolType { SYM_LOCAL=0, SYM_IMPORT=1, SYM_EXPORT=2 }