decompiled_code:
bool test_file_content_read_as_string()
{
bool v0; // zf
bool result; // al
char v2[136]; // [rsp+0h] [rbp-88h] BYREF

if ( (unsigned int)file_content_read_as_string("not-there", v2, 128LL) )
__assert_fail(
"file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE",
"test_meep.c",
0xAu,
"test_file_content_read_as_string");
if ( (unsigned int)file_content_read_as_string("test_meep.data", v2, 128LL) != 1 )
__assert_fail(
"file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE",
"test_meep.c",
0xCu,
"test_file_content_read_as_string");
v0 = strcmp(v2, "12345") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(buf, \"12345\") == 0", "test_meep.c", 0xDu, "test_file_content_read_as_string");
return result;
}

canonical_decompiled_code:
bool func7()
{
bool v0; // zf
bool result; // al
char v2[136]; // [rsp+0h] [rbp-88h] BYREF

if ( (unsigned int)func9("not-there", v2, 128LL) )
__assert_fail(
"file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE",
"test_meep.c",
0xAu,
"test_file_content_read_as_string");
if ( (unsigned int)func9("test_meep.data", v2, 128LL) != 1 )
__assert_fail(
"file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE",
"test_meep.c",
0xCu,
"test_file_content_read_as_string");
v0 = strcmp(v2, "12345") == 0;
result = !v0;
if ( !v0 )
__assert_fail("strcmp(buf, \"12345\") == 0", "test_meep.c", 0xDu, "test_file_content_read_as_string");
return result;
}

original_code:
void test_file_content_read_as_string()
{
   char buf[128];
   ((void) sizeof ((file_content_read_as_string("not-there", buf, sizeof(buf)) == 0) ? 1 : 0), __extension__ ({ if (file_content_read_as_string("not-there", buf, sizeof(buf)) == 0) ; else __assert_fail ("file_content_read_as_string(\"not-there\", buf, sizeof(buf)) == FALSE", "test_meep.c", 10, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof ((file_content_read_as_string("test_meep.data", buf, sizeof(buf)) == 1) ? 1 : 0), __extension__ ({ if (file_content_read_as_string("test_meep.data", buf, sizeof(buf)) == 1) ; else __assert_fail ("file_content_read_as_string(\"test_meep.data\", buf, sizeof(buf)) == TRUE", "test_meep.c", 12, __extension__ __PRETTY_FUNCTION__); }));
   ((void) sizeof ((strcmp(buf, "12345") == 0) ? 1 : 0), __extension__ ({ if (strcmp(buf, "12345") == 0) ; else __assert_fail ("strcmp(buf, \"12345\") == 0", "test_meep.c", 13, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types:
buf
char [128]


decompiled_code:
__int64 test_str_rstrip()
{
__int64 result; // rax
_WORD v1[4]; // [rsp+9h] [rbp-1Fh] BYREF
char v2; // [rsp+11h] [rbp-17h]
char v3; // [rsp+12h] [rbp-16h] BYREF
char v4[21]; // [rsp+13h] [rbp-15h] BYREF

strcpy(&v4[5], "meep\n \n");
str_rstrip(&v4[5]);
if ( strlen(&v4[5]) != 4 )
__assert_fail("strlen(buf1) == 4", "test_meep.c", 0x14u, "test_str_rstrip");
strcpy(v4, "mep\n");
str_rstrip(v4);
if ( strlen(v4) != 3 )
__assert_fail("strlen(buf2) == 3", "test_meep.c", 0x18u, "test_str_rstrip");
v3 = 0;
str_rstrip(&v3);
if ( v3 )
__assert_fail("strlen(buf3) == 0", "test_meep.c", 0x1Cu, "test_str_rstrip");
v1[3] = 256;
v2 = 0;
str_rstrip(&v1[3]);
if ( v1[3] != 256 )
__assert_fail("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 0x20u, "test_str_rstrip");
strcpy((char *)v1, " \n \t");
result = str_rstrip(v1);
if ( LOBYTE(v1[0]) )
__assert_fail("strlen(buf5) == 0", "test_meep.c", 0x25u, "test_str_rstrip");
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 result; // rax
_WORD v1[4]; // [rsp+9h] [rbp-1Fh] BYREF
char v2; // [rsp+11h] [rbp-17h]
char v3; // [rsp+12h] [rbp-16h] BYREF
char v4[21]; // [rsp+13h] [rbp-15h] BYREF

strcpy(&v4[5], "meep\n \n");
func10(&v4[5]);
if ( strlen(&v4[5]) != 4 )
__assert_fail("strlen(buf1) == 4", "test_meep.c", 0x14u, "test_str_rstrip");
strcpy(v4, "mep\n");
func10(v4);
if ( strlen(v4) != 3 )
__assert_fail("strlen(buf2) == 3", "test_meep.c", 0x18u, "test_str_rstrip");
v3 = 0;
func10(&v3);
if ( v3 )
__assert_fail("strlen(buf3) == 0", "test_meep.c", 0x1Cu, "test_str_rstrip");
v1[3] = 256;
v2 = 0;
func10(&v1[3]);
if ( v1[3] != 256 )
__assert_fail("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 0x20u, "test_str_rstrip");
strcpy((char *)v1, " \n \t");
result = func10(v1);
if ( LOBYTE(v1[0]) )
__assert_fail("strlen(buf5) == 0", "test_meep.c", 0x25u, "test_str_rstrip");
return result;
}

original_code:
void test_str_rstrip()
{
   char buf1[] = "meep\n \n";
   str_rstrip(buf1);
   ((void) sizeof ((strlen(buf1) == 4) ? 1 : 0), __extension__ ({ if (strlen(buf1) == 4) ; else __assert_fail ("strlen(buf1) == 4", "test_meep.c", 20, __extension__ __PRETTY_FUNCTION__); }));
   char buf2[] = "mep\n";
   str_rstrip(buf2);
   ((void) sizeof ((strlen(buf2) == 3) ? 1 : 0), __extension__ ({ if (strlen(buf2) == 3) ; else __assert_fail ("strlen(buf2) == 3", "test_meep.c", 24, __extension__ __PRETTY_FUNCTION__); }));
   char buf3[] = "";
   str_rstrip(buf3);
   ((void) sizeof ((strlen(buf3) == 0) ? 1 : 0), __extension__ ({ if (strlen(buf3) == 0) ; else __assert_fail ("strlen(buf3) == 0", "test_meep.c", 28, __extension__ __PRETTY_FUNCTION__); }));
   char buf4[] = "\0\1";
   str_rstrip(buf4);
   ((void) sizeof ((buf4[0] == '\0' && buf4[1] == '\1') ? 1 : 0), __extension__ ({ if (buf4[0] == '\0' && buf4[1] == '\1') ; else __assert_fail ("buf4[0] == '\\0' && buf4[1] == '\\1'", "test_meep.c", 32, __extension__ __PRETTY_FUNCTION__); }));
   char buf5[] = "  \n \t";
   str_rstrip(buf5);
   ((void) sizeof ((strlen(buf5) == 0) ? 1 : 0), __extension__ ({ if (strlen(buf5) == 0) ; else __assert_fail ("strlen(buf5) == 0", "test_meep.c", 37, __extension__ __PRETTY_FUNCTION__); }));
}

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
test_file_content_read_as_string();
test_str_rstrip(argc, argv);
puts("\n\nAll test are OK\n");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func7();
func8(argc, argv);
puts("\n\nAll test are OK\n");
return 0;
}

original_code:
int main(int argc, char **argv)
{
   test_file_content_read_as_string();
   test_str_rstrip();
   printf("\n\nAll test are OK\n\n");
   return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int


decompiled_code:
size_t __fastcall str_rstrip(const char *a1)
{
size_t result; // rax
int v2; // edx

result = strlen(a1);
v2 = result;
if ( (result & 0x80000000) == 0LL )
{
result = (size_t)&a1[(int)result];
do
{
if ( (unsigned __int8)(*(_BYTE *)result - 9) > 1u && (*(_BYTE *)result & 0xDF) != 0 )
break;
--v2;
--result;
}
while ( v2 != -1 );
}
a1[v2 + 1] = 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func10(const char *a1)
{
size_t result; // rax
int v2; // edx

result = strlen(a1);
v2 = result;
if ( (result & 0x80000000) == 0LL )
{
result = (size_t)&a1[(int)result];
do
{
if ( (unsigned __int8)(*(_BYTE *)result - 9) > 1u && (*(_BYTE *)result & 0xDF) != 0 )
break;
--v2;
--result;
}
while ( v2 != -1 );
}
a1[v2 + 1] = 0;
return result;
}

original_code:
void str_rstrip(char *input)
{
   int i = strlen(input);
   while(i >= 0 && (input[i] == '\0' || input[i] == '\n' ||
                    input[i] == ' ' || input[i] == '\t')) {
      i--;
   }
   input[i+1] = 0;
}

variable_types:
input
char *

variable_types: i int


repo: mvystrcil/digger-mate-panel-monitor

decompiled_code:
int __fastcall Generator__DumpData(const char *a1)
{
const char *v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n\n", "generator.c", 57LL, "Generator__DumpData", a1, v2);
return fprintf(stdout, "[%s:%d]:%s: Scripts: %p, %s\n\n", "generator.c", 58LL, "Generator__DumpData", a1, a1 + 256);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
const char *v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n\n", "generator.c", 57LL, "Generator__DumpData", a1, v2);
return fprintf(stdout, "[%s:%d]:%s: Scripts: %p, %s\n\n", "generator.c", 58LL, "Generator__DumpData", a1, a1 + 256);
}

original_code:
void Generator__DumpData(GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s\n" "\n", "generator.c",57,__FUNCTION__, data, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Scripts: %p, %s\n" "\n", "generator.c",58,__FUNCTION__, data, data->path_to_scripts); } while(0);
}

canonical_original_code:
void Generator__DumpData(struct GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s\n" "\n", "generator.c",57,__FUNCTION__, data, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Scripts: %p, %s\n" "\n", "generator.c",58,__FUNCTION__, data, data->path_to_scripts); } while(0);
}

variable_types: struct GeneratorData { char xml_file[256]; char path_to_scripts[256]; int *stop; } *


decompiled_code:
int __fastcall Generator__RunFile(char *arg, __int64 a2)
{
int result; // eax
int v3; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = *(_DWORD *)(a2 + 24);
if ( (result & 0x40) != 0 )
{
result = *(_DWORD *)(a2 + 24) & 0xF000;
if ( result != 40960 )
{
fprintf(
stdout,
"[%s:%d]:%s: Execute script with arguments %s\n\n",
"generator.c",
117LL,
"Generator__RunFile",
arg);
v3 = fork();
if ( v3 )
{
if ( v3 < 0 )
result = fprintf(stdout, "[%s:%d]:%s: Cannont fork process\n\n", "generator.c", 123LL, "Generator__RunFile");
else
result = waitpid(v3, stat_loc, 0);
}
else
{
execl(arg, arg, generator_data, 0LL);
result = fprintf(stdout, "[%s:%d]:%s: Done\n\n", "generator.c", 121LL, "Generator__RunFile");
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *arg, __int64 a2)
{
int result; // eax
int v3; // eax
int stat_loc[3]; // [rsp+0h] [rbp-Ch] BYREF

result = *(_DWORD *)(a2 + 24);
if ( (result & 0x40) != 0 )
{
result = *(_DWORD *)(a2 + 24) & 0xF000;
if ( result != 40960 )
{
fprintf(
stdout,
"[%s:%d]:%s: Execute script with arguments %s\n\n",
"generator.c",
117LL,
"Generator__RunFile",
arg);
v3 = fork();
if ( v3 )
{
if ( v3 < 0 )
result = fprintf(stdout, "[%s:%d]:%s: Cannont fork process\n\n", "generator.c", 123LL, "Generator__RunFile");
else
result = waitpid(v3, stat_loc, 0);
}
else
{
execl(arg, arg, generator_data, 0LL);
result = fprintf(stdout, "[%s:%d]:%s: Done\n\n", "generator.c", 121LL, "Generator__RunFile");
}
}
}
return result;
}

original_code:
void Generator__RunFile(char *file, struct stat *st){
 pid_t pid;
 int ret;
 if((st->st_mode) & 0100 && !((((st->st_mode)) & 0170000) == (0120000))){
  do { fprintf(stdout, "[%s:%d]:%s: " "Execute script with arguments %s\n" "\n", "generator.c",117,__FUNCTION__, file); } while(0);
  pid = fork();
  if(pid == 0){
   execl(file, file, generator_data.xml_file, (char*) ((void *)0));
   do { fprintf(stdout, "[%s:%d]:%s: " "Done\n" "\n", "generator.c",121,__FUNCTION__); } while(0);
  } else if (pid < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannont fork process\n" "\n", "generator.c",123,__FUNCTION__); } while(0);
  } else {
   waitpid(pid, &ret, 0);
  }
 }
}

canonical_original_code:
void Generator__RunFile(char *file, struct stat *st){
 int pid;
 int ret;
 if((st->st_mode) & 0100 && !((((st->st_mode)) & 0170000) == (0120000))){
  do { fprintf(stdout, "[%s:%d]:%s: " "Execute script with arguments %s\n" "\n", "generator.c",117,__FUNCTION__, file); } while(0);
  pid = fork();
  if(pid == 0){
   execl(file, file, generator_data.xml_file, (char *) ((void *)0));
   do { fprintf(stdout, "[%s:%d]:%s: " "Done\n" "\n", "generator.c",121,__FUNCTION__); } while(0);
  } else if (pid < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannont fork process\n" "\n", "generator.c",123,__FUNCTION__); } while(0);
  } else {
   waitpid(pid, &ret, 0);
  }
 }
}

variable_types:
file
char *

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

variable_types: pid int

variable_types: ret int

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


decompiled_code:
void __fastcall __noreturn Generator__Init(const void *a1)
{
fprintf(stdout, "[%s:%d]:%s: Initializing logs\n\n", "generator.c", 41LL, "Generator__Init");
qmemcpy(generator_data, a1, sizeof(generator_data));
Generator__DumpData(generator_data);
while ( 1 )
{
Generator__RunScripts((const char *)(unsigned int)((_DWORD)generator_data + 256));
fprintf(stdout, "[%s:%d]:%s: Wait - next cycle\n\n", "generator.c", 50LL, "Generator__Init");
sleep(0xAu);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const void *a1)
{
fprintf(stdout, "[%s:%d]:%s: Initializing logs\n\n", "generator.c", 41LL, "Generator__Init");
qmemcpy(generator_data, a1, sizeof(generator_data));
func0(generator_data);
while ( 1 )
{
func2((const char *)(unsigned int)((_DWORD)generator_data + 256));
fprintf(stdout, "[%s:%d]:%s: Wait - next cycle\n\n", "generator.c", 50LL, "Generator__Init");
sleep(0xAu);
}
}

original_code:
void* Generator__Init(void *gen_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "Initializing logs\n" "\n", "generator.c",41,__FUNCTION__); } while(0);
 generator_data = *((GeneratorData*) gen_data);
 const char *path = generator_data.path_to_scripts;
 Generator__DumpData(&generator_data);
 while(1){
  Generator__RunScripts(path);
  do { fprintf(stdout, "[%s:%d]:%s: " "Wait - next cycle\n" "\n", "generator.c",50,__FUNCTION__); } while(0);
  sleep(10);
 }
 return (void *)1;
}

canonical_original_code:
void *Generator__Init(void *gen_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "Initializing logs\n" "\n", "generator.c",41,__FUNCTION__); } while(0);
 generator_data = *((struct GeneratorData *) gen_data);
 char *path = generator_data.path_to_scripts;
 Generator__DumpData(&generator_data);
 while(1){
  Generator__RunScripts(path);
  do { fprintf(stdout, "[%s:%d]:%s: " "Wait - next cycle\n" "\n", "generator.c",50,__FUNCTION__); } while(0);
  sleep(10);
 }
 return (void *)1;
}

variable_types:
path
char *

return_type: void *

user_defined_types: struct GeneratorData { char xml_file[256]; char path_to_scripts[256]; int *stop; }


decompiled_code:
int __fastcall Main__DumpGeneratorData(__int64 a1)
{
fprintf(stdout, "[%s:%d]:%s: ##### Generator data ##################\n", "main.c", 85LL, "Main__DumpGeneratorData");
fprintf(stdout, "[%s:%d]:%s: Generator struct addr %p\n", "main.c", 86LL, "Main__DumpGeneratorData", (const void *)a1);
fprintf(
stdout,
"[%s:%d]:%s: XML file: %p, %s\n",
"main.c",
87LL,
"Main__DumpGeneratorData",
(const void *)a1,
(const char *)a1);
fprintf(
stdout,
"[%s:%d]:%s: Path scr: %p, %s\n",
"main.c",
88LL,
"Main__DumpGeneratorData",
(const void *)(a1 + 256),
(const char *)(a1 + 256));
return fprintf(
stdout,
"[%s:%d]:%s: #######################################\n\n",
"main.c",
90LL,
"Main__DumpGeneratorData");
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
fprintf(stdout, "[%s:%d]:%s: ##### Generator data ##################\n", "main.c", 85LL, "Main__DumpGeneratorData");
fprintf(stdout, "[%s:%d]:%s: Generator struct addr %p\n", "main.c", 86LL, "Main__DumpGeneratorData", (const void *)a1);
fprintf(
stdout,
"[%s:%d]:%s: XML file: %p, %s\n",
"main.c",
87LL,
"Main__DumpGeneratorData",
(const void *)a1,
(const char *)a1);
fprintf(
stdout,
"[%s:%d]:%s: Path scr: %p, %s\n",
"main.c",
88LL,
"Main__DumpGeneratorData",
(const void *)(a1 + 256),
(const char *)(a1 + 256));
return fprintf(
stdout,
"[%s:%d]:%s: #######################################\n\n",
"main.c",
90LL,
"Main__DumpGeneratorData");
}

original_code:
void Main__DumpGeneratorData(GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Generator data ##################" "\n", "main.c",85,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Generator struct addr %p" "\n", "main.c",86,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s" "\n", "main.c",87,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Path scr: %p, %s" "\n", "main.c",88,__FUNCTION__, data->path_to_scripts, data->path_to_scripts); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#######################################\n" "\n", "main.c",90,__FUNCTION__); } while(0);
}

canonical_original_code:
void Main__DumpGeneratorData(struct GeneratorData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Generator data ##################" "\n", "main.c",85,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Generator struct addr %p" "\n", "main.c",86,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file: %p, %s" "\n", "main.c",87,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Path scr: %p, %s" "\n", "main.c",88,__FUNCTION__, data->path_to_scripts, data->path_to_scripts); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#######################################\n" "\n", "main.c",90,__FUNCTION__); } while(0);
}


decompiled_code:
int __fastcall Main__DumpConnectionData(const char *a1)
{
__int64 v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: ##### Connection data ##################\n", "main.c", 94LL, "Main__DumpConnectionData");
fprintf(stdout, "[%s:%d]:%s: Connection struct addr %p\n", "main.c", 95LL, "Main__DumpConnectionData", a1);
fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n", "main.c", 96LL, "Main__DumpConnectionData", a1 + 4, a1 + 4);
LODWORD(v2) = *(_DWORD *)a1;
fprintf(stdout, "[%s:%d]:%s: Port : %p, %d\n", "main.c", 97LL, "Main__DumpConnectionData", a1, v2);
return fprintf(
stdout,
"[%s:%d]:%s: ########################################\n\n",
"main.c",
99LL,
"Main__DumpConnectionData");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
__int64 v2; // [rsp+0h] [rbp-18h]

fprintf(stdout, "[%s:%d]:%s: ##### Connection data ##################\n", "main.c", 94LL, "Main__DumpConnectionData");
fprintf(stdout, "[%s:%d]:%s: Connection struct addr %p\n", "main.c", 95LL, "Main__DumpConnectionData", a1);
fprintf(stdout, "[%s:%d]:%s: XML file: %p, %s\n", "main.c", 96LL, "Main__DumpConnectionData", a1 + 4, a1 + 4);
LODWORD(v2) = *(_DWORD *)a1;
fprintf(stdout, "[%s:%d]:%s: Port : %p, %d\n", "main.c", 97LL, "Main__DumpConnectionData", a1, v2);
return fprintf(
stdout,
"[%s:%d]:%s: ########################################\n\n",
"main.c",
99LL,
"Main__DumpConnectionData");
}

original_code:
void Main__DumpConnectionData(ConnectionData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Connection data ##################" "\n", "main.c",94,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Connection struct addr %p" "\n", "main.c",95,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file:  %p, %s" "\n", "main.c",96,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port    : %p, %d" "\n", "main.c",97,__FUNCTION__, &data->port, data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "########################################\n" "\n", "main.c",99,__FUNCTION__); } while(0);
}

canonical_original_code:
void Main__DumpConnectionData(struct ConnectionData *data){
 do { fprintf(stdout, "[%s:%d]:%s: " "##### Connection data ##################" "\n", "main.c",94,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Connection struct addr %p" "\n", "main.c",95,__FUNCTION__, data); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "XML file:  %p, %s" "\n", "main.c",96,__FUNCTION__, data->xml_file, data->xml_file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port    : %p, %d" "\n", "main.c",97,__FUNCTION__, &data->port, data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "########################################\n" "\n", "main.c",99,__FUNCTION__); } while(0);
}

variable_types: struct ConnectionData { int port; char xml_file[256]; int *stop; } *


decompiled_code:
int __fastcall Main__FillStructuresFromArgs(__int64 a1, __int64 a2, __int64 a3, const char **a4)
{
int result; // eax

fprintf(
stdout,
"[%s:%d]:%s: Fill in structure data %s, %s\n\n",
"main.c",
106LL,
"Main__FillStructuresFromArgs",
a4[1],
a4[2]);
strcpy((char *)a2, a4[2]);
fprintf(stdout, "[%s:%d]:%s: Fill path to scripts\n\n", "main.c", 108LL, "Main__FillStructuresFromArgs");
strcpy((char *)(a2 + 256), a4[1]);
*(_QWORD *)(a2 + 512) = &stop;
strcpy((char *)(a1 + 4), a4[2]);
result = atoi(a4[3]);
*(_DWORD *)a1 = result;
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2, __int64 a3, const char **a4)
{
int result; // eax

fprintf(
stdout,
"[%s:%d]:%s: Fill in structure data %s, %s\n\n",
"main.c",
106LL,
"Main__FillStructuresFromArgs",
a4[1],
a4[2]);
strcpy((char *)a2, a4[2]);
fprintf(stdout, "[%s:%d]:%s: Fill path to scripts\n\n", "main.c", 108LL, "Main__FillStructuresFromArgs");
strcpy((char *)(a2 + 256), a4[1]);
*(_QWORD *)(a2 + 512) = &stop;
strcpy((char *)(a1 + 4), a4[2]);
result = atoi(a4[3]);
*(_DWORD *)a1 = result;
return result;
}

original_code:
void Main__FillStructuresFromArgs(ConnectionData *connection,
                                  GeneratorData *generator,
                                  int argc,
                                  char *argv[]){
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill in structure data %s, %s\n" "\n", "main.c",106,__FUNCTION__, argv[1], argv[2]); } while(0);
 strcpy(generator->xml_file, argv[2]);
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill path to scripts\n" "\n", "main.c",108,__FUNCTION__); } while(0);
 strcpy(generator->path_to_scripts, argv[1]);
 generator->stop = &stop;
 strcpy(connection->xml_file, argv[2]);
 connection->port = atoi(argv[3]);
}

canonical_original_code:
void Main__FillStructuresFromArgs(struct ConnectionData *connection, struct GeneratorData *generator, int argc, char *argv[]){
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill in structure data %s, %s\n" "\n", "main.c",106,__FUNCTION__, argv[1], argv[2]); } while(0);
 strcpy(generator->xml_file, argv[2]);
 do { fprintf(stdout, "[%s:%d]:%s: " "Fill path to scripts\n" "\n", "main.c",108,__FUNCTION__); } while(0);
 strcpy(generator->path_to_scripts, argv[1]);
 generator->stop = &stop;
 strcpy(connection->xml_file, argv[2]);
 connection->port = atoi(argv[3]);
}

variable_types:
argv
char *[]

user_defined_types: struct ConnectionData { int port; char xml_file[256]; int *stop; }


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
pthread_t v4; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

memset(connection_data, 0, sizeof(connection_data));
memset(&generator_data, 0, 0x208uLL);
stop = 0;
if ( argc <= 1 )
{
fprintf(
stdout,
"[%s:%d]:%s: Usage: %s path to scripts path where to store xml port\n\n",
"main.c",
58LL,
"main",
*argv);
result = -1;
}
else
{
Main__FillStructuresFromArgs((__int64)connection_data, (__int64)&generator_data, (unsigned int)argc, argv);
Main__DumpConnectionData(connection_data);
Main__DumpGeneratorData((__int64)&generator_data);
Mutex__Init();
if ( pthread_create(&v4, 0LL, (void *(*)(void *))&Generator__Init, &generator_data) < 0 )
fprintf(stdout, "[%s:%d]:%s: Cannot create Generator Hypervisor thread\n\n", "main.c", 70LL, "main");
if ( pthread_create(newthread, 0LL, (void *(*)(void *))&Connection__Connect, connection_data) < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot create Connection Hypervisor thread\n\n", "main.c", 74LL, "main");
result = -1;
}
else
{
pthread_join(newthread[0], 0LL);
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
pthread_t v4; // [rsp+0h] [rbp-18h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

memset(connection_data, 0, sizeof(connection_data));
memset(&generator_data, 0, 0x208uLL);
stop = 0;
if ( argc <= 1 )
{
fprintf(
stdout,
"[%s:%d]:%s: Usage: %s path to scripts path where to store xml port\n\n",
"main.c",
58LL,
"main",
*argv);
result = -1;
}
else
{
func2((__int64)connection_data, (__int64)&generator_data, (unsigned int)argc, argv);
func1(connection_data);
func0((__int64)&generator_data);
Mutex__Init();
if ( pthread_create(&v4, 0LL, (void *(*)(void *))&Generator__Init, &generator_data) < 0 )
fprintf(stdout, "[%s:%d]:%s: Cannot create Generator Hypervisor thread\n\n", "main.c", 70LL, "main");
if ( pthread_create(newthread, 0LL, (void *(*)(void *))&Connection__Connect, connection_data) < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot create Connection Hypervisor thread\n\n", "main.c", 74LL, "main");
result = -1;
}
else
{
pthread_join(newthread[0], 0LL);
result = 0;
}
}
return result;
}

original_code:
int main(int argc, char* argv[]){
 pthread_t connection, generate;
 memset(&connection_data, 0, sizeof(connection_data));
 memset(&generator_data, 0, sizeof(generator_data));
 stop = 0;
 if(argc < 2){
  do { fprintf(stdout, "[%s:%d]:%s: " "Usage: %s path to scripts path where to store xml port\n" "\n", "main.c",58,__FUNCTION__, argv[0]); } while(0);
  return -1;
 } else {
  Main__FillStructuresFromArgs(&connection_data, &generator_data, argc, argv);
 }
 Main__DumpConnectionData(&connection_data);
 Main__DumpGeneratorData(&generator_data);
 Mutex__Init();
 if(pthread_create(&generate, ((void *)0), Generator__Init, (void *)&generator_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Generator Hypervisor thread\n" "\n", "main.c",70,__FUNCTION__); } while(0);
 }
 if(pthread_create(&connection, ((void *)0), Connection__Connect, (void *)&connection_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Connection Hypervisor thread\n" "\n", "main.c",74,__FUNCTION__); } while(0);
  return -1;
 }
 pthread_join(connection, ((void *)0));
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
 unsigned long connection; unsigned long generate;
 memset(&connection_data, 0, sizeof(connection_data));
 memset(&generator_data, 0, sizeof(generator_data));
 stop = 0;
 if(argc < 2){
  do { fprintf(stdout, "[%s:%d]:%s: " "Usage: %s path to scripts path where to store xml port\n" "\n", "main.c",58,__FUNCTION__, argv[0]); } while(0);
  return -1;
 } else {
  Main__FillStructuresFromArgs(&connection_data, &generator_data, argc, argv);
 }
 Main__DumpConnectionData(&connection_data);
 Main__DumpGeneratorData(&generator_data);
 Mutex__Init();
 if(pthread_create(&generate, ((void *)0), Generator__Init, (void *)&generator_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Generator Hypervisor thread\n" "\n", "main.c",70,__FUNCTION__); } while(0);
 }
 if(pthread_create(&connection, ((void *)0), Connection__Connect, (void *)&connection_data) < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create Connection Hypervisor thread\n" "\n", "main.c",74,__FUNCTION__); } while(0);
  return -1;
 }
 pthread_join(connection, ((void *)0));
 return 0;
}

variable_types:
connection
unsigned long

variable_types:
generate
unsigned long


decompiled_code:
__int64 Mutex__Init()
{
int v0; // edx
__int64 result; // rax

v0 = pthread_mutex_init(&xml_file_mutex, 0LL);
result = 1LL;
if ( v0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot initialize connection mutex\n\n", "mutex.c", 29LL, "Mutex__Init");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // edx
__int64 result; // rax

v0 = pthread_mutex_init(&xml_file_mutex, 0LL);
result = 1LL;
if ( v0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot initialize connection mutex\n\n", "mutex.c", 29LL, "Mutex__Init");
result = 0LL;
}
return result;
}

original_code:
int Mutex__Init(){
 if(pthread_mutex_init(&xml_file_mutex, ((void *)0)) != 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot initialize connection mutex\n" "\n", "mutex.c",29,__FUNCTION__); } while(0);
  return 0;
 }
 return 1;
}


decompiled_code:
__int64 Mutex__LockFileMutex()
{
pthread_mutex_lock(&xml_file_mutex);
return 1LL;
}

canonical_decompiled_code:
__int64 func1()
{
pthread_mutex_lock(&xml_file_mutex);
return 1LL;
}

original_code:
int Mutex__LockFileMutex(){
 pthread_mutex_lock(&xml_file_mutex);
 return 1;
}


decompiled_code:
__int64 Mutex__UnlockFileMutex()
{
pthread_mutex_unlock(&xml_file_mutex);
return 1LL;
}

canonical_decompiled_code:
__int64 func2()
{
pthread_mutex_unlock(&xml_file_mutex);
return 1LL;
}

original_code:
int Mutex__UnlockFileMutex(){
 pthread_mutex_unlock(&xml_file_mutex);
 return 1;
}


decompiled_code:
int Mutex__DestroyAll()
{
return pthread_mutex_destroy(&xml_file_mutex);
}

canonical_decompiled_code:
int func3()
{
return pthread_mutex_destroy(&xml_file_mutex);
}

original_code:
void Mutex__DestroyAll(){
 pthread_mutex_destroy(&xml_file_mutex);
}


decompiled_code:
int __fastcall Connection__HandleConnections(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
pthread_t v6; // [rsp+8h] [rbp-38h] BYREF
socklen_t addr_len; // [rsp+14h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+18h] [rbp-28h] BYREF
int arg[3]; // [rsp+34h] [rbp-Ch] BYREF

addr_len = 16;
fprintf(
stdout,
"[%s:%d]:%s: Dump data %p, %s\n\n",
"connection.c",
84LL,
"Connection__HandleConnections",
connection_data,
&connection_data[4]);
while ( 1 )
{
v4 = accept(a3, &addr, &addr_len);
arg[0] = v4;
if ( v4 < 0 )
break;
fprintf(
stdout,
"[%s:%d]:%s: Client accepted - handle it\n\n",
"connection.c",
92LL,
"Connection__HandleConnections");
if ( pthread_create(&v6, 0LL, Connection__ConnectionHandler, arg) < 0 )
fprintf(
stdout,
"[%s:%d]:%s: Cannot create thread for accepting new client\n\n",
"connection.c",
99LL,
"Connection__HandleConnections");
}
return fprintf(
stdout,
"[%s:%d]:%s: Cannot get socket fd for accept connection %d\n\n",
"connection.c",
89LL,
"Connection__HandleConnections",
(unsigned int)v4);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2, int a3)
{
int v4; // eax
pthread_t v6; // [rsp+8h] [rbp-38h] BYREF
socklen_t addr_len; // [rsp+14h] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+18h] [rbp-28h] BYREF
int arg[3]; // [rsp+34h] [rbp-Ch] BYREF

addr_len = 16;
fprintf(
stdout,
"[%s:%d]:%s: Dump data %p, %s\n\n",
"connection.c",
84LL,
"Connection__HandleConnections",
connection_data,
&connection_data[4]);
while ( 1 )
{
v4 = accept(a3, &addr, &addr_len);
arg[0] = v4;
if ( v4 < 0 )
break;
fprintf(
stdout,
"[%s:%d]:%s: Client accepted - handle it\n\n",
"connection.c",
92LL,
"Connection__HandleConnections");
if ( pthread_create(&v6, 0LL, Connection__ConnectionHandler, arg) < 0 )
fprintf(
stdout,
"[%s:%d]:%s: Cannot create thread for accepting new client\n\n",
"connection.c",
99LL,
"Connection__HandleConnections");
}
return fprintf(
stdout,
"[%s:%d]:%s: Cannot get socket fd for accept connection %d\n\n",
"connection.c",
89LL,
"Connection__HandleConnections",
(unsigned int)v4);
}

original_code:
void Connection__HandleConnections(struct sockaddr_in server_data, int server_fd){
 int client_fd;
 int *new_sock;
 struct sockaddr_in client_data;
 socklen_t tmp = sizeof(server_data);
 do { fprintf(stdout, "[%s:%d]:%s: " "Dump data %p, %s\n" "\n", "connection.c",84,__FUNCTION__, &connection_data, connection_data.xml_file); } while(0);
 while(1){
  client_fd = accept(server_fd, (struct sockaddr *)&client_data, &tmp);
  if(client_fd < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd for accept connection %d\n" "\n", "connection.c",89,__FUNCTION__, client_fd); } while(0);
   return;
  } else {
   do { fprintf(stdout, "[%s:%d]:%s: " "Client accepted - handle it\n" "\n", "connection.c",92,__FUNCTION__); } while(0);
  }
  new_sock = malloc(sizeof(char));
  new_sock = &client_fd;
  pthread_t thread;
  if(pthread_create(&thread, ((void *)0), Connection__ConnectionHandler, (void *) new_sock) < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create thread for accepting new client\n" "\n", "connection.c",99,__FUNCTION__); } while(0);
  }
 }
}

canonical_original_code:
void Connection__HandleConnections(struct sockaddr_in server_data, int server_fd){
 int client_fd;
 int *new_sock;
 struct sockaddr_in client_data;
 unsigned int tmp = sizeof(server_data);
 do { fprintf(stdout, "[%s:%d]:%s: " "Dump data %p, %s\n" "\n", "connection.c",84,__FUNCTION__, &connection_data, connection_data.xml_file); } while(0);
 while(1){
  client_fd = accept(server_fd, (struct sockaddr *)&client_data, &tmp);
  if(client_fd < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd for accept connection %d\n" "\n", "connection.c",89,__FUNCTION__, client_fd); } while(0);
   return;
  } else {
   do { fprintf(stdout, "[%s:%d]:%s: " "Client accepted - handle it\n" "\n", "connection.c",92,__FUNCTION__); } while(0);
  }
  new_sock = malloc(sizeof(char));
  new_sock = &client_fd;
  unsigned long thread;
  if(pthread_create(&thread, ((void *)0), Connection__ConnectionHandler, (void *) new_sock) < 0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot create thread for accepting new client\n" "\n", "connection.c",99,__FUNCTION__); } while(0);
  }
 }
}

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:
tmp
unsigned int

variable_types:
thread
unsigned long

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 { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
int __fastcall Connection__SendString(int fd, void *buf)
{
size_t v2; // rax
int v3; // ebx
size_t v4; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
v4 = strlen((const char *)buf);
if ( v3 != v4 )
LODWORD(v4) = fprintf(
stdout,
"[%s:%d]:%s: Cannot sent all data\n\n",
"connection.c",
136LL,
"Connection__SendString");
return v4;
}

canonical_decompiled_code:
int __fastcall func3(int fd, void *buf)
{
size_t v2; // rax
int v3; // ebx
size_t v4; // rax

v2 = strlen((const char *)buf);
v3 = write(fd, buf, v2);
v4 = strlen((const char *)buf);
if ( v3 != v4 )
LODWORD(v4) = fprintf(
stdout,
"[%s:%d]:%s: Cannot sent all data\n\n",
"connection.c",
136LL,
"Connection__SendString");
return v4;
}

original_code:
void Connection__SendString(int socket, char *string){
 int len;
 len = write(socket, string, strlen(string));
 if(len != strlen(string)){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot sent all data\n" "\n", "connection.c",136,__FUNCTION__); } while(0);
 }
}

variable_types: socket int

variable_types:
string
char *

variable_types: len int


decompiled_code:
__int64 __fastcall Connection__Connect(const void *a1)
{
__int64 v1; // rbp
int v2; // eax
unsigned int v3; // ebx
int v4; // er12
__int64 result; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

qmemcpy(connection_data, a1, sizeof(connection_data));
v1 = *(unsigned int *)connection_data;
fprintf(
stdout,
"[%s:%d]:%s: Port: %d\n\n",
"connection.c",
42LL,
"Connection__Connect",
*(unsigned int *)connection_data);
fprintf(
stdout,
"[%s:%d]:%s: PORT: %p, %d\n\n",
"connection.c",
43LL,
"Connection__Connect",
connection_data,
*(unsigned int *)connection_data);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot get file descriptor for Internet Socket\n\n",
"connection.c",
48LL,
"Connection__Connect");
result = 1LL;
}
else
{
v3 = v2;
fprintf(stdout, "[%s:%d]:%s: Got sockect fd: %d\n\n", "connection.c", 51LL, "Connection__Connect", (unsigned int)v2);
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(v1);
v4 = bind(v3, &addr, 0x10u);
listen(v3, 5);
if ( v4 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot bind to socket fd: %d, port: %d\n\n",
"connection.c",
67LL,
"Connection__Connect",
v3,
v1);
result = 1LL;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Socket binded to port: %d\n\n",
"connection.c",
70LL,
"Connection__Connect",
(unsigned int)v1);
Connection__HandleConnections(*(__int64 *)&addr.sa_family, *(__int64 *)&addr.sa_data[6], v3);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const void *a1)
{
__int64 v1; // rbp
int v2; // eax
unsigned int v3; // ebx
int v4; // er12
__int64 result; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

qmemcpy(connection_data, a1, sizeof(connection_data));
v1 = *(unsigned int *)connection_data;
fprintf(
stdout,
"[%s:%d]:%s: Port: %d\n\n",
"connection.c",
42LL,
"Connection__Connect",
*(unsigned int *)connection_data);
fprintf(
stdout,
"[%s:%d]:%s: PORT: %p, %d\n\n",
"connection.c",
43LL,
"Connection__Connect",
connection_data,
*(unsigned int *)connection_data);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot get file descriptor for Internet Socket\n\n",
"connection.c",
48LL,
"Connection__Connect");
result = 1LL;
}
else
{
v3 = v2;
fprintf(stdout, "[%s:%d]:%s: Got sockect fd: %d\n\n", "connection.c", 51LL, "Connection__Connect", (unsigned int)v2);
optval = 1;
setsockopt(v3, 1, 2, &optval, 4u);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(v1);
v4 = bind(v3, &addr, 0x10u);
listen(v3, 5);
if ( v4 < 0 )
{
fprintf(
stdout,
"[%s:%d]:%s: Cannot bind to socket fd: %d, port: %d\n\n",
"connection.c",
67LL,
"Connection__Connect",
v3,
v1);
result = 1LL;
}
else
{
fprintf(
stdout,
"[%s:%d]:%s: Socket binded to port: %d\n\n",
"connection.c",
70LL,
"Connection__Connect",
(unsigned int)v1);
func1(*(__int64 *)&addr.sa_family, *(__int64 *)&addr.sa_data[6], v3);
result = 0LL;
}
}
return result;
}

original_code:
void* Connection__Connect(void *conn_data){
 connection_data = *((ConnectionData *) conn_data);
 int port = connection_data.port;
 int server_fd, res;
 do { fprintf(stdout, "[%s:%d]:%s: " "Port: %d\n" "\n", "connection.c",42,__FUNCTION__, port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "PORT: %p, %d\n" "\n", "connection.c",43,__FUNCTION__, &connection_data.port, connection_data.port); } while(0);
 struct sockaddr_in server_data;
 server_fd = socket(2, SOCK_STREAM, 0);
 if(server_fd < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get file descriptor for Internet Socket\n" "\n", "connection.c",48,__FUNCTION__); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Got sockect fd: %d\n" "\n", "connection.c",51,__FUNCTION__, server_fd); } while(0);
 }
 int optval = 1;
 setsockopt(server_fd, 1, 2, &optval, sizeof(int));
 bzero(&server_data, sizeof(server_data));
 server_data.sin_family = 2;
 server_data.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 server_data.sin_port = htons(port);
 res = bind(server_fd, (struct sockaddr *) &server_data, sizeof(server_data));
 listen(server_fd, 5);
 if(res < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot bind to socket fd: %d, port: %d\n" "\n", "connection.c",67,__FUNCTION__, server_fd, port); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Socket binded to port: %d\n" "\n", "connection.c",70,__FUNCTION__, port); } while(0);
 }
 Connection__HandleConnections (server_data, server_fd);
 return (void *)0;
}

canonical_original_code:
void *Connection__Connect(void *conn_data){
 connection_data = *((struct ConnectionData *) conn_data);
 int port = connection_data.port;
 int server_fd; int res;
 do { fprintf(stdout, "[%s:%d]:%s: " "Port: %d\n" "\n", "connection.c",42,__FUNCTION__, port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "PORT: %p, %d\n" "\n", "connection.c",43,__FUNCTION__, &connection_data.port, connection_data.port); } while(0);
 struct sockaddr_in server_data;
 server_fd = socket(2, SOCK_STREAM, 0);
 if(server_fd < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get file descriptor for Internet Socket\n" "\n", "connection.c",48,__FUNCTION__); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Got sockect fd: %d\n" "\n", "connection.c",51,__FUNCTION__, server_fd); } while(0);
 }
 int optval = 1;
 setsockopt(server_fd, 1, 2, &optval, sizeof(int));
 bzero(&server_data, sizeof(server_data));
 server_data.sin_family = 2;
 server_data.sin_addr.s_addr = ((unsigned int) 0x00000000);
 server_data.sin_port = htons(port);
 res = bind(server_fd, (struct sockaddr *) &server_data, sizeof(server_data));
 listen(server_fd, 5);
 if(res < 0){
  do { fprintf(stdout, "[%s:%d]:%s: " "Cannot bind to socket fd: %d, port: %d\n" "\n", "connection.c",67,__FUNCTION__, server_fd, port); } while(0);
  return (void *)1;
 } else {
  do { fprintf(stdout, "[%s:%d]:%s: " "Socket binded to port: %d\n" "\n", "connection.c",70,__FUNCTION__, port); } while(0);
 }
 Connection__HandleConnections (server_data, server_fd);
 return (void *)0;
}

variable_types: port int

variable_types: res int

variable_types: optval int


decompiled_code:
int __fastcall Connection__DumpData(const char *a1)
{
fprintf(stdout, "[%s:%d]:%s: ####### Connect to #######\n", "connection.c", 79LL, "Connection__DumpData");
fprintf(stdout, "[%s:%d]:%s: Port %d\n", "connection.c", 80LL, "Connection__DumpData", *(unsigned int *)a1);
fprintf(stdout, "[%s:%d]:%s: Addr: %s\n", "connection.c", 81LL, "Connection__DumpData", a1 + 12);
fprintf(stdout, "[%s:%d]:%s: Refresh: %d\n", "connection.c", 82LL, "Connection__DumpData", *((unsigned int *)a1 + 1));
fprintf(stdout, "[%s:%d]:%s: Retry: %d\n", "connection.c", 83LL, "Connection__DumpData", *((unsigned int *)a1 + 2));
fprintf(stdout, "[%s:%d]:%s: File: %s\n", "connection.c", 84LL, "Connection__DumpData", a1 + 268);
fprintf(stdout, "[%s:%d]:%s: Error: %d\n", "connection.c", 85LL, "Connection__DumpData", *((unsigned int *)a1 + 131));
return fprintf(stdout, "[%s:%d]:%s: #########################\n\n", "connection.c", 86LL, "Connection__DumpData");
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
fprintf(stdout, "[%s:%d]:%s: ####### Connect to #######\n", "connection.c", 79LL, "Connection__DumpData");
fprintf(stdout, "[%s:%d]:%s: Port %d\n", "connection.c", 80LL, "Connection__DumpData", *(unsigned int *)a1);
fprintf(stdout, "[%s:%d]:%s: Addr: %s\n", "connection.c", 81LL, "Connection__DumpData", a1 + 12);
fprintf(stdout, "[%s:%d]:%s: Refresh: %d\n", "connection.c", 82LL, "Connection__DumpData", *((unsigned int *)a1 + 1));
fprintf(stdout, "[%s:%d]:%s: Retry: %d\n", "connection.c", 83LL, "Connection__DumpData", *((unsigned int *)a1 + 2));
fprintf(stdout, "[%s:%d]:%s: File: %s\n", "connection.c", 84LL, "Connection__DumpData", a1 + 268);
fprintf(stdout, "[%s:%d]:%s: Error: %d\n", "connection.c", 85LL, "Connection__DumpData", *((unsigned int *)a1 + 131));
return fprintf(stdout, "[%s:%d]:%s: #########################\n\n", "connection.c", 86LL, "Connection__DumpData");
}

original_code:
void Connection__DumpData(ConnectionData *connection_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "####### Connect to #######" "\n", "connection.c",79,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port %d" "\n", "connection.c",80,__FUNCTION__, connection_data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Addr: %s" "\n", "connection.c",81,__FUNCTION__, connection_data->address); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Refresh: %d" "\n", "connection.c",82,__FUNCTION__, connection_data->refresh); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Retry: %d" "\n", "connection.c",83,__FUNCTION__, connection_data->retry); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "File: %s" "\n", "connection.c",84,__FUNCTION__, connection_data->file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Error: %d" "\n", "connection.c",85,__FUNCTION__, connection_data->error); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#########################\n" "\n", "connection.c",86,__FUNCTION__); } while(0);
}

canonical_original_code:
void Connection__DumpData(struct ConnectionData *connection_data){
 do { fprintf(stdout, "[%s:%d]:%s: " "####### Connect to #######" "\n", "connection.c",79,__FUNCTION__); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Port %d" "\n", "connection.c",80,__FUNCTION__, connection_data->port); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Addr: %s" "\n", "connection.c",81,__FUNCTION__, connection_data->address); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Refresh: %d" "\n", "connection.c",82,__FUNCTION__, connection_data->refresh); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Retry: %d" "\n", "connection.c",83,__FUNCTION__, connection_data->retry); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "File: %s" "\n", "connection.c",84,__FUNCTION__, connection_data->file); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "Error: %d" "\n", "connection.c",85,__FUNCTION__, connection_data->error); } while(0);
 do { fprintf(stdout, "[%s:%d]:%s: " "#########################\n" "\n", "connection.c",86,__FUNCTION__); } while(0);
}

variable_types: struct ConnectionData { int port; int refresh; int retry; char address[256]; char file[256]; int error; } *


decompiled_code:
__int64 __fastcall Connection__Connect(_DWORD *a1)
{
int v1; // ebx
int v2; // ebp
int *v3; // rbx
struct sockaddr buf; // [rsp+0h] [rbp-238h] BYREF
unsigned int v6[138]; // [rsp+10h] [rbp-228h] BYREF

qmemcpy(v6, a1, 0x210uLL);
Connection__DumpData((const char *)v6);
v1 = 5;
while ( 1 )
{
*(_QWORD *)&buf.sa_family = 0LL;
*(_QWORD *)&buf.sa_data[6] = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot get socket fd\n\n", "connection.c", 49LL, "Connection__Connect");
return 0LL;
}
buf.sa_family = 2;
*(_WORD *)buf.sa_data = _bswap_16(v6[0]);
if ( inet_pton(2, (const char *)&v6[3], &buf.sa_data[2]) <= 0 )
break;
while ( connect(v2, &buf, 0x10u) < 0 )
{
sleep(v6[2]);
if ( --v1 <= 0 )
{
v3 = _errno_location();
fprintf(
stdout,
"[%s:%d]:%s: Cannot connect to server: %s, errno %d\n\n",
"connection.c",
65LL,
"Connection__Connect",
(const char *)&v6[3],
(unsigned int)*v3);
a1[131] = *v3;
return 0LL;
}
}
Connection__SaveServerData(v2, (char *)&v6[67]);
sleep(v6[1]);
}
fprintf(
stdout,
"[%s:%d]:%s: Cannot set server address: %s\n\n",
"connection.c",
57LL,
"Connection__Connect",
(const char *)&v6[3]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1)
{
int v1; // ebx
int v2; // ebp
int *v3; // rbx
struct sockaddr buf; // [rsp+0h] [rbp-238h] BYREF
unsigned int v6[138]; // [rsp+10h] [rbp-228h] BYREF

qmemcpy(v6, a1, 0x210uLL);
func1((const char *)v6);
v1 = 5;
while ( 1 )
{
*(_QWORD *)&buf.sa_family = 0LL;
*(_QWORD *)&buf.sa_data[6] = 0LL;
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
fprintf(stdout, "[%s:%d]:%s: Cannot get socket fd\n\n", "connection.c", 49LL, "Connection__Connect");
return 0LL;
}
buf.sa_family = 2;
*(_WORD *)buf.sa_data = func0(v6[0]);
if ( inet_pton(2, (const char *)&v6[3], &buf.sa_data[2]) <= 0 )
break;
while ( connect(v2, &buf, 0x10u) < 0 )
{
sleep(v6[2]);
if ( --v1 <= 0 )
{
v3 = _errno_location();
fprintf(
stdout,
"[%s:%d]:%s: Cannot connect to server: %s, errno %d\n\n",
"connection.c",
65LL,
"Connection__Connect",
(const char *)&v6[3],
(unsigned int)*v3);
a1[131] = *v3;
return 0LL;
}
}
func2(v2, (char *)&v6[67]);
sleep(v6[1]);
}
fprintf(
stdout,
"[%s:%d]:%s: Cannot set server address: %s\n\n",
"connection.c",
57LL,
"Connection__Connect",
(const char *)&v6[3]);
return 0LL;
}

original_code:
void *Connection__Connect(void *conn_data){
 int sockfd = 0;
 int attempts = 5;
 ConnectionData connection_data = *((ConnectionData *)conn_data);
 struct sockaddr_in serv_addr;
 Connection__DumpData(&connection_data);
 while(1){
  memset(&serv_addr, 0, sizeof(serv_addr));
  if((sockfd = socket(2, SOCK_STREAM, 0)) < 0){
      do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd\n" "\n", "connection.c",49,__FUNCTION__); } while(0);
      return ((void *)0);
  }
  serv_addr.sin_family = 2;
  serv_addr.sin_port = htons(connection_data.port);
  if(inet_pton(2, connection_data.address, &serv_addr.sin_addr)<=0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot set server address: %s\n" "\n", "connection.c",57,__FUNCTION__, connection_data.address); } while(0);
   return ((void *)0);
  }
  while(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
   sleep(connection_data.retry);
   attempts--;
   if(attempts <= 0){
    do { fprintf(stdout, "[%s:%d]:%s: " "Cannot connect to server: %s, errno %d\n" "\n", "connection.c",65,__FUNCTION__, connection_data.address, (*__errno_location ())); } while(0);
    ((ConnectionData *) conn_data)->error = (*__errno_location ());
    return ((void *)0);
   }
  }
  Connection__SaveServerData(sockfd, connection_data.file);
  sleep(connection_data.refresh);
 }
 return ((void *)0);
}

canonical_original_code:
void *Connection__Connect(void *conn_data){
 int sockfd = 0;
 int attempts = 5;
 struct ConnectionData connection_data = *((ConnectionData *)conn_data);
 struct sockaddr_in serv_addr;
 Connection__DumpData(&connection_data);
 while(1){
  memset(&serv_addr, 0, sizeof(serv_addr));
  if((sockfd = socket(2, SOCK_STREAM, 0)) < 0){
      do { fprintf(stdout, "[%s:%d]:%s: " "Cannot get socket fd\n" "\n", "connection.c",49,__FUNCTION__); } while(0);
      return ((void *)0);
  }
  serv_addr.sin_family = 2;
  serv_addr.sin_port = htons(connection_data.port);
  if(inet_pton(2, connection_data.address, &serv_addr.sin_addr)<=0){
   do { fprintf(stdout, "[%s:%d]:%s: " "Cannot set server address: %s\n" "\n", "connection.c",57,__FUNCTION__, connection_data.address); } while(0);
   return ((void *)0);
  }
  while(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
   sleep(connection_data.retry);
   attempts--;
   if(attempts <= 0){
    do { fprintf(stdout, "[%s:%d]:%s: " "Cannot connect to server: %s, errno %d\n" "\n", "connection.c",65,__FUNCTION__, connection_data.address, (*__errno_location ())); } while(0);
    ((struct ConnectionData *) conn_data)->error = (*__errno_location ());
    return ((void *)0);
   }
  }
  Connection__SaveServerData(sockfd, connection_data.file);
  sleep(connection_data.refresh);
 }
 return ((void *)0);
}

variable_types: sockfd int

variable_types: attempts int

variable_types: struct ConnectionData { int port; int refresh; int retry; char address[256]; char file[256]; int error; }


emptyList

repo: mwalton/data-structures

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

v1 = ptr;
while ( v1 )
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
}

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

v1 = ptr;
while ( v1 )
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 1);
free(v2);
}
}

original_code:
void emptyList(node *head) {
  while (head != ((void *)0)) {
    node *old = head;
    head = head->link;
    free (old);
  }
}

canonical_original_code:
void emptyList(struct node *head) {
  while (head != ((void *)0)) {
    struct node *old = head;
    head = head->link;
    free (old);
  }
}

variable_types: struct node { double item; struct node *link; } *


repo: mwarning/KadNode

decompiled_code:
void __fastcall nss_server_handler(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax
struct sockaddr v5; // [rsp-7Ch] [rbp-88h] BYREF
socklen_t addr_len[3]; // [rsp+0h] [rbp-Ch] BYREF

if ( a1 > 0 )
{
addr_len[0] = 16;
v2 = accept(a2, &v5, addr_len);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &nss_client_handler);
}
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax
struct sockaddr v5; // [rsp-7Ch] [rbp-88h] BYREF
socklen_t addr_len[3]; // [rsp+0h] [rbp-Ch] BYREF

if ( a1 > 0 )
{
addr_len[0] = 16;
v2 = accept(a2, &v5, addr_len);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &nss_client_handler);
}
}
}

original_code:
static void nss_server_handler(int rc, int serversock)
{
 socklen_t addrlen;
 int clientsock;
 struct sockaddr_un addr;
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(struct sockaddr_in);
 clientsock = accept(serversock, (struct sockaddr *) &addr, &addrlen);
 if (clientsock < 0) {
  log_print(3, "accept(): %s", strerror((*__errno_location ())));;
  return;
 }
 net_add_handler(clientsock, &nss_client_handler);
}

canonical_original_code:
static void nss_server_handler(int rc, int serversock)
{
 unsigned int addrlen;
 int clientsock;
 struct sockaddr_un addr;
 if (rc <= 0) {
  return;
 }
 addrlen = sizeof(struct sockaddr_in);
 clientsock = accept(serversock, (struct sockaddr *) &addr, &addrlen);
 if (clientsock < 0) {
  log_print(3, "accept(): %s", strerror((*__errno_location ())));;
  return;
 }
 net_add_handler(clientsock, &nss_client_handler);
}

variable_types: rc int

variable_types: serversock int

variable_types:
addrlen
unsigned int

variable_types: clientsock int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall nss_client_handler(int a1, unsigned int a2)
{
__int64 result; // rax
__int64 v3; // rax
int v4; // er13
const __m128i *v5; // rbp
int v6; // er12
int v7; // eax
_QWORD v8[34]; // [rsp+0h] [rbp-248h] BYREF
int buf[64]; // [rsp+110h] [rbp-138h] BYREF
int v10; // [rsp+210h] [rbp-38h]

result = 0LL;
memset(buf, 0, sizeof(buf));
v10 = 0;
memset(v8, 0, 0x108uLL);
if ( a1 > 0 )
{
if ( (unsigned int)recv(a2, buf, 0x104uLL, 0) != 260 )
{
LABEL_3:
close(a2);
return net_remove_handler(a2, &nss_client_handler);
}
HIBYTE(v10) = 0;
if ( (unsigned __int8)has_tld(&buf[1], *(_QWORD *)(gconf + 24LL)) )
{
v3 = kad_lookup(&buf[1]);
if ( v3 )
{
v4 = buf[0];
v5 = *(const __m128i **)(v3 + 296);
if ( v5 )
{
v6 = 0;
while ( 1 )
{
v7 = is_valid_result(v5);
if ( v6 > 15 || !v7 )
goto LABEL_11;
if ( !v4 )
break;
if ( v5->m128i_u16[4] == v4 )
goto LABEL_16;
LABEL_11:
v5 = (const __m128i *)v5->m128i_i64[0];
if ( !v5 )
goto LABEL_21;
}
v4 = v5->m128i_u16[4];
LABEL_16:
if ( v4 == 10 )
{
*(__m128i *)&v8[2 * v6++ + 1] = _mm_loadu_si128(v5 + 1);
}
else if ( v4 == 2 )
{
*((_DWORD *)&v8[1] + v6++) = v5->m128i_i32[3];
}
goto LABEL_11;
}
v6 = 0;
LABEL_21:
v8[0] = __PAIR64__(v6, v4);
}
}
write(a2, v8, 0x108uLL);
goto LABEL_3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned int a2)
{
__int64 result; // rax
__int64 v3; // rax
int v4; // er13
const __m128i *v5; // rbp
int v6; // er12
int v7; // eax
_QWORD v8[34]; // [rsp+0h] [rbp-248h] BYREF
int buf[64]; // [rsp+110h] [rbp-138h] BYREF
int v10; // [rsp+210h] [rbp-38h]

result = 0LL;
memset(buf, 0, sizeof(buf));
v10 = 0;
memset(v8, 0, 0x108uLL);
if ( a1 > 0 )
{
if ( (unsigned int)recv(a2, buf, 0x104uLL, 0) != 260 )
{
LABEL_3:
close(a2);
return net_remove_handler(a2, &nss_client_handler);
}
HIBYTE(v10) = 0;
if ( (unsigned __int8)has_tld(&buf[1], *(_QWORD *)(gconf + 24LL)) )
{
v3 = kad_lookup(&buf[1]);
if ( v3 )
{
v4 = buf[0];
v5 = *(const __m128i **)(v3 + 296);
if ( v5 )
{
v6 = 0;
while ( 1 )
{
v7 = is_valid_result(v5);
if ( v6 > 15 || !v7 )
goto LABEL_11;
if ( !v4 )
break;
if ( v5->m128i_u16[4] == v4 )
goto LABEL_16;
LABEL_11:
v5 = (const __m128i *)v5->m128i_i64[0];
if ( !v5 )
goto LABEL_21;
}
v4 = v5->m128i_u16[4];
LABEL_16:
if ( v4 == 10 )
{
*(__m128i *)&v8[2 * v6++ + 1] = _mm_loadu_si128(v5 + 1);
}
else if ( v4 == 2 )
{
*((_DWORD *)&v8[1] + v6++) = v5->m128i_i32[3];
}
goto LABEL_11;
}
v6 = 0;
LABEL_21:
v8[0] = __PAIR64__(v6, v4);
}
}
write(a2, v8, 0x108uLL);
goto LABEL_3;
}
return result;
}

original_code:
static void nss_client_handler(int rc, int clientsock)
{
 const struct search_t *search;
 const struct result_t *result;
 struct kadnode_nss_request req = {0};
 struct kadnode_nss_response res = {0};
 int count;
 int af;
 if (rc <= 0) {
  return;
 }
 rc = recv(clientsock, &req, sizeof(req), 0);
 if (rc != sizeof(req)) {
  goto abort;
 }
 req.name[256 - 1] = '\0';
 if (!has_tld(&req.name[0], gconf->query_tld)) {
  goto finish;
 }
 search = kad_lookup(&req.name[0]);
 if (search == ((void *)0)) {
  goto finish;
 }
 af = req.af;
 count = 0;
 for (result = search->results; result; result = result->next) {
  if (is_valid_result(result) && count < 16) {
   if (af == 0) {
    af = result->addr.ss_family;
   }
   if (af != result->addr.ss_family) {
    continue;
   }
   if (af == 10) {
    memcpy(&res.data.ipv6[count], &((IP6 *)&result->addr)->sin6_addr, sizeof(struct in6_addr));
    count += 1;
   }
   if (af == 2) {
    memcpy(&res.data.ipv4[count], &((IP4 *)&result->addr)->sin_addr, sizeof(struct in_addr));
    count += 1;
   }
  }
 }
 res.af = af;
 res.count = count;
finish:
 write(clientsock, &res, sizeof(res));
abort:
 close(clientsock);
 net_remove_handler(clientsock, &nss_client_handler);
}

canonical_original_code:
static void nss_client_handler(int rc, int clientsock)
{
 struct search_t *search;
 struct result_t *result;
 struct kadnode_nss_request req = {0};
 struct kadnode_nss_response res = {0};
 int count;
 int af;
 if (rc <= 0) {
  return;
 }
 rc = recv(clientsock, &req, sizeof(req), 0);
 if (rc != sizeof(req)) {
  goto abort;
 }
 req.name[256 - 1] = '\0';
 if (!has_tld(&req.name[0], gconf->query_tld)) {
  goto finish;
 }
 search = kad_lookup(&req.name[0]);
 if (search == ((void *)0)) {
  goto finish;
 }
 af = req.af;
 count = 0;
 for (result = search->results; result; result = result->next) {
  if (is_valid_result(result) && count < 16) {
   if (af == 0) {
    af = result->addr.ss_family;
   }
   if (af != result->addr.ss_family) {
    continue;
   }
   if (af == 10) {
    memcpy(&res.data.ipv6[count], &((struct sockaddr_in6 *)&result->addr)->sin6_addr, sizeof(struct in6_addr));
    count += 1;
   }
   if (af == 2) {
    memcpy(&res.data.ipv4[count], &((struct sockaddr_in *)&result->addr)->sin_addr, sizeof(struct in_addr));
    count += 1;
   }
  }
 }
 res.af = af;
 res.count = count;
finish:
 write(clientsock, &res, sizeof(res));
abort:
 close(clientsock);
 net_remove_handler(clientsock, &nss_client_handler);
}

variable_types: struct search_t { struct search_t *next; unsigned char id[20]; unsigned short done; char query[256]; long start_time; struct result_t { struct result_t *next; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } addr; enum AUTH_STATE { AUTH_OK=0, AUTH_AGAIN=1, AUTH_FAILED=2, AUTH_ERROR=3, AUTH_SKIP=4, AUTH_PROGRESS=5, AUTH_WAITING=6 } state; } *results; void (*callback)(); } *

variable_types: struct kadnode_nss_request { int af; char name[256]; }

variable_types: struct kadnode_nss_response { int af; int count; union { struct in_addr { unsigned int s_addr; } ipv4[16]; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ipv6[16]; } data; }

variable_types: count int

variable_types: af int

user_defined_types: struct kadnode_nss_response { int af; int count; union { struct in_addr ipv4[16]; struct in6_addr ipv6[16]; } data; }

user_defined_types: struct search_t { struct search_t *next; unsigned char id[20]; unsigned short done; char query[256]; long start_time; struct result_t *results; void (*callback)(); }

user_defined_types: struct result_t { struct result_t *next; struct sockaddr_storage addr; enum AUTH_STATE state; }

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; }


decompiled_code:
__int64 nss_setup()
{
unsigned int v0; // ebx

v0 = 1;
if ( !(unsigned int)unix_create_unix_socket(*(_QWORD *)(gconf + 128LL), &g_nss_sock) )
{
v0 = 0;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "NSS: Bind to %s", *(const char **)(gconf + 128LL));
net_add_handler((unsigned int)g_nss_sock, nss_server_handler);
}
return v0;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // ebx

v0 = 1;
if ( !(unsigned int)unix_create_unix_socket(*(_QWORD *)(gconf + 128LL), &g_nss_sock) )
{
v0 = 0;
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "NSS: Bind to %s", *(const char **)(gconf + 128LL));
net_add_handler((unsigned int)g_nss_sock, nss_server_handler);
}
return v0;
}

original_code:
int nss_setup(void)
{
 if (unix_create_unix_socket(gconf->nss_path, &g_nss_sock) == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "NSS: Bind to %s", gconf->nss_path); } while (0);
  net_add_handler(g_nss_sock, &nss_server_handler);
  return 0;
 }
 return 1;
}

canonical_original_code:
int nss_setup()
{
 if (unix_create_unix_socket(gconf->nss_path, &g_nss_sock) == 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "NSS: Bind to %s", gconf->nss_path); } while (0);
  net_add_handler(g_nss_sock, &nss_server_handler);
  return 0;
 }
 return 1;
}


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

if ( g_nss_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 128LL));
return result;
}

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

if ( g_nss_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 128LL));
return result;
}

original_code:
void nss_free(void)
{
 if (g_nss_sock >= 0) {
  unix_remove_unix_socket(gconf->nss_path, g_nss_sock);
 }
}

canonical_original_code:
void nss_free()
{
 if (g_nss_sock >= 0) {
  unix_remove_unix_socket(gconf->nss_path, g_nss_sock);
 }
}


decompiled_code:
__int64 main_run()
{
unsigned int v0; // er12
int v1; // ebx
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx

v0 = conf_load();
if ( v0 != 1 )
{
v1 = kad_setup();
announces_setup();
searches_setup();
peerfile_setup();
v2 = v0 | lpd_setup() | v1;
v3 = bob_setup() | v2;
v4 = dns_setup() | v3;
v5 = nss_setup() | v4;
v6 = tls_client_setup() | v5;
v7 = tls_server_setup() | v6;
v0 = cmd_setup() | v7;
if ( !v0 )
{
net_loop();
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "Shutting down...");
}
peerfile_export();
cmd_free();
nss_free();
dns_free();
bob_free();
lpd_free();
tls_server_free();
tls_client_free();
peerfile_free();
searches_free();
announces_free();
kad_free();
conf_free();
net_free();
if ( g_pidfile_written )
unlink(*(const char **)(gconf + 32LL));
}
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
int v1; // ebx
int v2; // ebx
int v3; // ebx
int v4; // ebx
int v5; // ebx
int v6; // ebx
int v7; // ebx

v0 = conf_load();
if ( v0 != 1 )
{
v1 = kad_setup();
announces_setup();
searches_setup();
peerfile_setup();
v2 = v0 | lpd_setup() | v1;
v3 = bob_setup() | v2;
v4 = dns_setup() | v3;
v5 = nss_setup() | v4;
v6 = tls_client_setup() | v5;
v7 = tls_server_setup() | v6;
v0 = cmd_setup() | v7;
if ( !v0 )
{
net_loop();
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "Shutting down...");
}
peerfile_export();
cmd_free();
nss_free();
dns_free();
bob_free();
lpd_free();
tls_server_free();
tls_client_free();
peerfile_free();
searches_free();
announces_free();
kad_free();
conf_free();
net_free();
if ( g_pidfile_written )
unlink(*(const char **)(gconf + 32LL));
}
return v0;
}

original_code:
int main_run(void)
{
 int rc = 0;
 if (0 != 0) {
  fprintf(stderr, "Problematic EXIT_SUCCESS definition\n");
  return 1;
 }
 rc |= conf_load();
 if (rc == 1) {
  return 1;
 }
 rc |= kad_setup();
 announces_setup();
 searches_setup();
 peerfile_setup();
 rc |= lpd_setup();
 rc |= bob_setup();
 rc |= dns_setup();
 rc |= nss_setup();
 rc |= tls_client_setup();
 rc |= tls_server_setup();
 rc |= cmd_setup();
 if (rc == 0) {
  net_loop();
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "Shutting down..."); } while (0);
 }
 peerfile_export();
 cmd_free();
 nss_free();
 dns_free();
 bob_free();
 lpd_free();
 tls_server_free();
 tls_client_free();
 peerfile_free();
 searches_free();
 announces_free();
 kad_free();
 conf_free();
 net_free();
 if (g_pidfile_written) {
  unlink(gconf->pidfile);
 }
 return rc;
}

canonical_original_code:
int main_run()
{
 int rc = 0;
 if (0 != 0) {
  fprintf(stderr, "Problematic EXIT_SUCCESS definition\n");
  return 1;
 }
 rc |= conf_load();
 if (rc == 1) {
  return 1;
 }
 rc |= kad_setup();
 announces_setup();
 searches_setup();
 peerfile_setup();
 rc |= lpd_setup();
 rc |= bob_setup();
 rc |= dns_setup();
 rc |= nss_setup();
 rc |= tls_client_setup();
 rc |= tls_server_setup();
 rc |= cmd_setup();
 if (rc == 0) {
  net_loop();
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "Shutting down..."); } while (0);
 }
 peerfile_export();
 cmd_free();
 nss_free();
 dns_free();
 bob_free();
 lpd_free();
 tls_server_free();
 tls_client_free();
 peerfile_free();
 searches_free();
 announces_free();
 kad_free();
 conf_free();
 net_free();
 if (g_pidfile_written) {
  unlink(gconf->pidfile);
 }
 return rc;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int *v4; // rax
char *v5; // rax
__int64 v6; // rbx
unsigned int v7; // eax

if ( strstr(*argv, "kadnode-ctl") )
return cmd_client((unsigned int)argc, argv);
result = conf_setup((unsigned int)argc, argv);
if ( result != 1 )
{
if ( *(_BYTE *)(gconf + 56LL) )
{
*(_DWORD *)(gconf + 64LL) = 1;
fclose(stderr);
fclose(stdout);
fclose(stdin);
unix_fork();
if ( chdir("/") )
{
v4 = _errno_location();
v5 = strerror(*v4);
log_print(3LL, "Changing working directory to '/' failed: %s", v5);
exit(1);
}
}
else
{
conf_info();
}
unix_signals();
v6 = *(_QWORD *)(gconf + 32LL);
if ( v6 )
{
v7 = getpid();
unix_write_pidfile(v7, v6);
g_pidfile_written = 1;
}
unix_dropuid0();
result = main_run();
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int *v4; // rax
char *v5; // rax
__int64 v6; // rbx
unsigned int v7; // eax

if ( strstr(*argv, "kadnode-ctl") )
return cmd_client((unsigned int)argc, argv);
result = conf_setup((unsigned int)argc, argv);
if ( result != 1 )
{
if ( *(_BYTE *)(gconf + 56LL) )
{
*(_DWORD *)(gconf + 64LL) = 1;
fclose(stderr);
fclose(stdout);
fclose(stdin);
unix_fork();
if ( chdir("/") )
{
v4 = _errno_location();
v5 = strerror(*v4);
log_print(3LL, "Changing working directory to '/' failed: %s", v5);
exit(1);
}
}
else
{
conf_info();
}
unix_signals();
v6 = *(_QWORD *)(gconf + 32LL);
if ( v6 )
{
v7 = getpid();
unix_write_pidfile(v7, v6);
g_pidfile_written = 1;
}
unix_dropuid0();
result = func0();
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
 int rc = 0;
 if (strstr(argv[0], "kadnode-ctl")) {
  return cmd_client(argc, argv);
 }
 rc |= conf_setup(argc, argv);
 if (rc == 1) {
  return 1;
 }
 if (gconf->is_daemon) {
  gconf->use_syslog = 1;
  fclose(stderr);
  fclose(stdout);
  fclose(stdin);
  unix_fork();
  if (chdir("/") != 0) {
   log_print(3, "Changing working directory to '/' failed: %s", strerror((*__errno_location ())));;
   exit(1);
  }
 } else {
  conf_info();
 }
 unix_signals();
 if (gconf->pidfile) {
  unix_write_pidfile(getpid(), gconf->pidfile);
  g_pidfile_written = 1;
 }
 unix_dropuid0();
 return main_run();
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
void __fastcall tls_server_handler(int a1, int a2)
{
int *v2; // rbx
int *v3; // rdi
int v4; // eax
int v5; // eax
char v6[8]; // [rsp+8h] [rbp-20h] BYREF
__int64 v7[3]; // [rsp+10h] [rbp-18h] BYREF

v7[0] = 0LL;
v7[1] = 0LL;
if ( a1 > 0 && g_client_fd4 < 0 && g_client_fd6 < 0 )
{
v2 = &g_client_fd6;
if ( g_listen_fd6 != a2 )
v2 = &g_client_fd4;
v3 = &g_listen_fd6;
if ( g_listen_fd6 != a2 )
v3 = &g_listen_fd4;
v4 = mbedtls_net_accept(v3, v2, v7, 16LL, v6);
if ( v4 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: mbedtls_net_accept returned -0x%x", (unsigned int)-v4);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Got incoming connection");
v5 = mbedtls_net_set_nonblock(v2);
if ( v5 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: net_set_nonblock() returned -0x%x", (unsigned int)-v5);
}
else
{
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_set_bio(&g_ssl, v2, &mbedtls_net_send, &mbedtls_net_recv, 0LL);
net_add_handler((unsigned int)*v2, tls_client_handler);
}
}
}
}

canonical_decompiled_code:
void __fastcall func1(int a1, int a2)
{
int *v2; // rbx
int *v3; // rdi
int v4; // eax
int v5; // eax
char v6[8]; // [rsp+8h] [rbp-20h] BYREF
__int64 v7[3]; // [rsp+10h] [rbp-18h] BYREF

v7[0] = 0LL;
v7[1] = 0LL;
if ( a1 > 0 && g_client_fd4 < 0 && g_client_fd6 < 0 )
{
v2 = &g_client_fd6;
if ( g_listen_fd6 != a2 )
v2 = &g_client_fd4;
v3 = &g_listen_fd6;
if ( g_listen_fd6 != a2 )
v3 = &g_listen_fd4;
v4 = mbedtls_net_accept(v3, v2, v7, 16LL, v6);
if ( v4 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: mbedtls_net_accept returned -0x%x", (unsigned int)-v4);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Got incoming connection");
v5 = mbedtls_net_set_nonblock(v2);
if ( v5 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(4LL, "TLS-Server: net_set_nonblock() returned -0x%x", (unsigned int)-v5);
}
else
{
mbedtls_ssl_conf_read_timeout(&g_conf, 0LL);
mbedtls_ssl_set_bio(&g_ssl, v2, &mbedtls_net_send, &mbedtls_net_recv, 0LL);
net_add_handler((unsigned int)*v2, tls_client_handler);
}
}
}
}

original_code:
static void tls_server_handler(int rc, int sock)
{
 unsigned char client_ip[16] = { 0 };
 mbedtls_net_context *listen_fd;
 mbedtls_net_context *client_fd;
 size_t cliip_len;
 int ret;
 if (rc <= 0 || g_client_fd4.fd > -1 || g_client_fd6.fd > -1) {
  return;
 }
 if (sock == g_listen_fd6.fd) {
  listen_fd = &g_listen_fd6;
  client_fd = &g_client_fd6;
 } else {
  listen_fd = &g_listen_fd4;
  client_fd = &g_client_fd4;
 }
 if ((ret = mbedtls_net_accept(listen_fd, client_fd,
    client_ip, sizeof(client_ip), &cliip_len)) != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: mbedtls_net_accept returned -0x%x", -ret); } while (0);
  return;
 }
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Got incoming connection"); } while (0);
 ret = mbedtls_net_set_nonblock(client_fd);
 if (ret != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: net_set_nonblock() returned -0x%x", -ret); } while (0);
  return;
 }
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_set_bio(&g_ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, ((void *)0));
 net_add_handler(client_fd->fd, &tls_client_handler);
}

canonical_original_code:
static void tls_server_handler(int rc, int sock)
{
 unsigned char client_ip[16] = { 0 };
 struct mbedtls_net_context *listen_fd;
 struct mbedtls_net_context *client_fd;
 unsigned long cliip_len;
 int ret;
 if (rc <= 0 || g_client_fd4.fd > -1 || g_client_fd6.fd > -1) {
  return;
 }
 if (sock == g_listen_fd6.fd) {
  listen_fd = &g_listen_fd6;
  client_fd = &g_client_fd6;
 } else {
  listen_fd = &g_listen_fd4;
  client_fd = &g_client_fd4;
 }
 if ((ret = mbedtls_net_accept(listen_fd, client_fd,
    client_ip, sizeof(client_ip), &cliip_len)) != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: mbedtls_net_accept returned -0x%x", -ret); } while (0);
  return;
 }
 do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Got incoming connection"); } while (0);
 ret = mbedtls_net_set_nonblock(client_fd);
 if (ret != 0) {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(4, "TLS-Server: net_set_nonblock() returned -0x%x", -ret); } while (0);
  return;
 }
 mbedtls_ssl_conf_read_timeout(&g_conf, 0);
 mbedtls_ssl_set_bio(&g_ssl, client_fd, mbedtls_net_send, mbedtls_net_recv, ((void *)0));
 net_add_handler(client_fd->fd, &tls_client_handler);
}

variable_types: sock int

variable_types: struct mbedtls_net_context { int fd; } *

variable_types: ret int


decompiled_code:
__int64 __fastcall end_client_connection(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

net_remove_handler(*a1, tls_client_handler);
while ( (unsigned int)mbedtls_ssl_close_notify(&g_ssl) == -26752 )
;
mbedtls_net_free(a1);
mbedtls_ssl_session_reset(&g_ssl);
if ( a2 == -27264 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Hello verification requested");
}
else if ( a2 == -26496 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Client initiated reconnection from same port");
}
else if ( a2 )
{
mbedtls_strerror(a2, v3, 100LL);
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Error -0x%x - %s", -a2, v3);
}
else
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Authentication successful");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

net_remove_handler(*a1, tls_client_handler);
while ( (unsigned int)mbedtls_ssl_close_notify(&g_ssl) == -26752 )
;
mbedtls_net_free(a1);
mbedtls_ssl_session_reset(&g_ssl);
if ( a2 == -27264 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Hello verification requested");
}
else if ( a2 == -26496 )
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Client initiated reconnection from same port");
}
else if ( a2 )
{
mbedtls_strerror(a2, v3, 100LL);
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Error -0x%x - %s", -a2, v3);
}
else
{
result = gconf;
if ( !*(_DWORD *)(gconf + 60LL) )
result = log_print(7LL, "TLS-Server: Authentication successful");
}
return result;
}

original_code:
static void end_client_connection(mbedtls_net_context *client_fd, int result)
{
 int ret;
 net_remove_handler(client_fd->fd, &tls_client_handler);
 do ret = mbedtls_ssl_close_notify(&g_ssl);
 while (ret == -0x6880);
 mbedtls_net_free(client_fd);
 mbedtls_ssl_session_reset(&g_ssl);
 if (result == -0x6A80) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Hello verification requested"); } while (0);
 } else if (result == -0x6780) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Client initiated reconnection from same port"); } while (0);
 } else if (result != 0) {
  char error_buf[100];
  mbedtls_strerror(result, error_buf, 100);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Error -0x%x - %s", -result, error_buf); } while (0);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Authentication successful"); } while (0);
 }
}

canonical_original_code:
static void end_client_connection(struct mbedtls_net_context *client_fd, int result)
{
 int ret;
 net_remove_handler(client_fd->fd, &tls_client_handler);
 do ret = mbedtls_ssl_close_notify(&g_ssl);
 while (ret == -0x6880);
 mbedtls_net_free(client_fd);
 mbedtls_ssl_session_reset(&g_ssl);
 if (result == -0x6A80) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Hello verification requested"); } while (0);
 } else if (result == -0x6780) {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Client initiated reconnection from same port"); } while (0);
 } else if (result != 0) {
  char error_buf[100];
  mbedtls_strerror(result, error_buf, 100);
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Error -0x%x - %s", -result, error_buf); } while (0);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Authentication successful"); } while (0);
 }
}

variable_types: result int


decompiled_code:
__int64 __fastcall tls_client_handler(__int64 a1, int a2)
{
int *v2; // rax
unsigned int *v3; // rbp
__int64 result; // rax
unsigned int v5; // ebx
unsigned int v6; // eax
int v7; // eax
const char *v8; // rbx
const char *v9; // rax
char v10[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = &g_client_fd6;
if ( g_client_fd4 == a2 )
v2 = &g_client_fd4;
v3 = (unsigned int *)v2;
do
{
result = mbedtls_ssl_handshake(&g_ssl);
v5 = result;
}
while ( (_DWORD)result == -26752 );
if ( (_DWORD)result != -26880 )
{
if ( (_DWORD)result )
{
if ( (_DWORD)result == -9984 )
{
v6 = mbedtls_ssl_get_verify_result(&g_ssl);
mbedtls_x509_crt_verify_info(v10, 512LL, &unk_A348, v6);
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Verify failed: %s", v10);
}
result = end_client_connection(v3, v5);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
{
v8 = (const char *)mbedtls_ssl_get_ciphersuite(&g_ssl);
v9 = (const char *)mbedtls_ssl_get_version(&g_ssl);
log_print(7LL, "TLS-Server: Protocol is %s, ciphersuite is %s", v9, v8);
}
v7 = mbedtls_ssl_get_record_expansion(&g_ssl);
if ( v7 < 0 )
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Record expansion is unknown (compression)");
}
else if ( !*(_DWORD *)(gconf + 60LL) )
{
log_print(7LL, "TLS-Server: Record expansion is %d", (unsigned int)v7);
}
result = end_client_connection(v3, 0);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
int *v2; // rax
unsigned int *v3; // rbp
__int64 result; // rax
unsigned int v5; // ebx
unsigned int v6; // eax
int v7; // eax
const char *v8; // rbx
const char *v9; // rax
char v10[536]; // [rsp+0h] [rbp-218h] BYREF

v2 = &g_client_fd6;
if ( g_client_fd4 == a2 )
v2 = &g_client_fd4;
v3 = (unsigned int *)v2;
do
{
result = mbedtls_ssl_handshake(&g_ssl);
v5 = result;
}
while ( (_DWORD)result == -26752 );
if ( (_DWORD)result != -26880 )
{
if ( (_DWORD)result )
{
if ( (_DWORD)result == -9984 )
{
v6 = mbedtls_ssl_get_verify_result(&g_ssl);
mbedtls_x509_crt_verify_info(v10, 512LL, &unk_A348, v6);
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Verify failed: %s", v10);
}
result = func2(v3, v5);
}
else
{
if ( !*(_DWORD *)(gconf + 60LL) )
{
v8 = (const char *)mbedtls_ssl_get_ciphersuite(&g_ssl);
v9 = (const char *)mbedtls_ssl_get_version(&g_ssl);
log_print(7LL, "TLS-Server: Protocol is %s, ciphersuite is %s", v9, v8);
}
v7 = mbedtls_ssl_get_record_expansion(&g_ssl);
if ( v7 < 0 )
{
if ( !*(_DWORD *)(gconf + 60LL) )
log_print(7LL, "TLS-Server: Record expansion is unknown (compression)");
}
else if ( !*(_DWORD *)(gconf + 60LL) )
{
log_print(7LL, "TLS-Server: Record expansion is %d", (unsigned int)v7);
}
result = func2(v3, 0);
}
}
return result;
}

original_code:
static void tls_client_handler(int rc, int sock)
{
 mbedtls_net_context *client_fd;
 int ret;
 int exp;
 if (sock == g_client_fd4.fd) {
  client_fd = &g_client_fd4;
 } else {
  client_fd = &g_client_fd6;
 }
 do ret = mbedtls_ssl_handshake(&g_ssl);
 while (ret == -0x6880);
 if (ret == -0x6900) {
  return;
 }
 if (ret) {
  if (ret == -0x2700) {
   char vrfy_buf[512];
   uint32_t flags = mbedtls_ssl_get_verify_result(&g_ssl);
   mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", flags);
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Verify failed: %s", vrfy_buf); } while (0);
  }
  end_client_connection(client_fd, ret);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Protocol is %s, ciphersuite is %s", mbedtls_ssl_get_version(&g_ssl), mbedtls_ssl_get_ciphersuite(&g_ssl)); } while (0);
  if ((exp = mbedtls_ssl_get_record_expansion(&g_ssl)) >= 0) {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Record expansion is %d", exp); } while (0);
  } else {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Record expansion is unknown (compression)"); } while (0);
  }
  end_client_connection(client_fd, 0);
 }
}

canonical_original_code:
static void tls_client_handler(int rc, int sock)
{
 struct mbedtls_net_context *client_fd;
 int ret;
 int exp;
 if (sock == g_client_fd4.fd) {
  client_fd = &g_client_fd4;
 } else {
  client_fd = &g_client_fd6;
 }
 do ret = mbedtls_ssl_handshake(&g_ssl);
 while (ret == -0x6880);
 if (ret == -0x6900) {
  return;
 }
 if (ret) {
  if (ret == -0x2700) {
   char vrfy_buf[512];
   unsigned int flags = mbedtls_ssl_get_verify_result(&g_ssl);
   mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", flags);
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Verify failed: %s", vrfy_buf); } while (0);
  }
  end_client_connection(client_fd, ret);
 } else {
  do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Protocol is %s, ciphersuite is %s", mbedtls_ssl_get_version(&g_ssl), mbedtls_ssl_get_ciphersuite(&g_ssl)); } while (0);
  if ((exp = mbedtls_ssl_get_record_expansion(&g_ssl)) >= 0) {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Record expansion is %d", exp); } while (0);
  } else {
   do { if (gconf->verbosity == VERBOSITY_DEBUG) log_print(7, "TLS-Server: Record expansion is unknown (compression)"); } while (0);
  }
  end_client_connection(client_fd, 0);
 }
}

variable_types: exp int

variable_types:
flags
unsigned int


decompiled_code:
__int64 __fastcall match_pattern(char *s1, char *s)
{
__int64 result; // rax
char *v4; // rdi

if ( *s1 != 42 )
return strcmp(s1, s) == 0;
v4 = strchr(s, 46);
result = 0LL;
if ( v4 )
result = strcmp(v4, s1 + 1) == 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s1, char *s)
{
__int64 result; // rax
char *v4; // rdi

if ( *s1 != 42 )
return strcmp(s1, s) == 0;
v4 = strchr(s, 46);
result = 0LL;
if ( v4 )
result = strcmp(v4, s1 + 1) == 0;
return result;
}

original_code:
static int match_pattern(const char cname[], const char name[])
{
 if (cname[0] == '*') {
  char *dot = strchr(name, '.');
  return dot && (strcmp(dot, cname + 1) == 0);
 } else {
  return strcmp(cname, name) == 0;
 }
}

canonical_original_code:
static int match_pattern(char cname[], char name[])
{
 if (cname[0] == '*') {
  char *dot = strchr(name, '.');
  return dot && (strcmp(dot, cname + 1) == 0);
 } else {
  return strcmp(cname, name) == 0;
 }
}

variable_types:
cname
char []

variable_types:
name
char []

variable_types:
dot
char *


decompiled_code:
__int64 tls_server_setup()
{
unsigned int v0; // ebx
int v1; // eax
int v2; // eax

v0 = 0;
if ( g_sni_entries )
{
mbedtls_net_init(&g_client_fd4);
mbedtls_net_init(&g_listen_fd4);
mbedtls_net_init(&g_client_fd6);
mbedtls_net_init(&g_listen_fd6);
mbedtls_ssl_init(&g_ssl);
mbedtls_ssl_config_init(&g_conf);
mbedtls_ctr_drbg_init(&g_drbg);
tls_announce_all_cnames();
mbedtls_entropy_init(&g_entropy);
v1 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v1 )
{
log_print(3LL, "TLS-Server: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v1);
v0 = 1;
}
else
{
g_listen_fd4 = net_bind("TLS", "0.0.0.0", *(unsigned int *)(gconf + 72LL), 0LL, 6LL);
g_listen_fd6 = net_bind("TLS", "::", *(unsigned int *)(gconf + 72LL), 0LL, 6LL);
v2 = mbedtls_ssl_config_defaults(&g_conf, 1LL, 0LL, 0LL);
if ( v2 )
{
log_print(3LL, "TLS-Server: mbedtls_ssl_config_defaults returned -0x%x", (unsigned int)-v2);
v0 = 1;
}
else
{
mbedtls_ssl_conf_authmode(&g_conf, 2LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_sni(&g_conf, sni_callback, g_sni_entries);
v0 = mbedtls_ssl_setup(&g_ssl, &g_conf);
if ( v0 )
{
log_print(3LL, "TLS-Server: mbedtls_ssl_setup returned -0x%x", -v0);
v0 = 1;
}
else
{
if ( g_listen_fd4 >= 0 )
{
mbedtls_net_set_nonblock(&g_listen_fd4);
net_add_handler((unsigned int)g_listen_fd4, tls_server_handler);
}
if ( g_listen_fd6 >= 0 )
{
mbedtls_net_set_nonblock(&g_listen_fd6);
net_add_handler((unsigned int)g_listen_fd6, tls_server_handler);
}
}
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // ebx
int v1; // eax
int v2; // eax

v0 = 0;
if ( g_sni_entries )
{
mbedtls_net_init(&g_client_fd4);
mbedtls_net_init(&g_listen_fd4);
mbedtls_net_init(&g_client_fd6);
mbedtls_net_init(&g_listen_fd6);
mbedtls_ssl_init(&g_ssl);
mbedtls_ssl_config_init(&g_conf);
mbedtls_ctr_drbg_init(&g_drbg);
func6();
mbedtls_entropy_init(&g_entropy);
v1 = mbedtls_ctr_drbg_seed(&g_drbg, &mbedtls_entropy_func, &g_entropy, "kadnode", 7LL);
if ( v1 )
{
log_print(3LL, "TLS-Server: mbedtls_ctr_drbg_seed returned -0x%x", (unsigned int)-v1);
v0 = 1;
}
else
{
g_listen_fd4 = net_bind("TLS", "0.0.0.0", *(unsigned int *)(gconf + 72LL), 0LL, 6LL);
g_listen_fd6 = net_bind("TLS", "::", *(unsigned int *)(gconf + 72LL), 0LL, 6LL);
v2 = mbedtls_ssl_config_defaults(&g_conf, 1LL, 0LL, 0LL);
if ( v2 )
{
log_print(3LL, "TLS-Server: mbedtls_ssl_config_defaults returned -0x%x", (unsigned int)-v2);
v0 = 1;
}
else
{
mbedtls_ssl_conf_authmode(&g_conf, 2LL);
mbedtls_ssl_conf_rng(&g_conf, &mbedtls_ctr_drbg_random, &g_drbg);
mbedtls_ssl_conf_sni(&g_conf, sni_callback, g_sni_entries);
v0 = mbedtls_ssl_setup(&g_ssl, &g_conf);
if ( v0 )
{
log_print(3LL, "TLS-Server: mbedtls_ssl_setup returned -0x%x", -v0);
v0 = 1;
}
else
{
if ( g_listen_fd4 >= 0 )
{
mbedtls_net_set_nonblock(&g_listen_fd4);
net_add_handler((unsigned int)g_listen_fd4, tls_server_handler);
}
if ( g_listen_fd6 >= 0 )
{
mbedtls_net_set_nonblock(&g_listen_fd6);
net_add_handler((unsigned int)g_listen_fd6, tls_server_handler);
}
}
}
}
}
return v0;
}

original_code:
int tls_server_setup(void)
{
 const char *pers = "kadnode";
 int ret;
 if (g_sni_entries == ((void *)0)) {
  return 0;
 }
 mbedtls_net_init(&g_client_fd4);
 mbedtls_net_init(&g_listen_fd4);
 mbedtls_net_init(&g_client_fd6);
 mbedtls_net_init(&g_listen_fd6);
 mbedtls_ssl_init(&g_ssl);
 mbedtls_ssl_config_init(&g_conf);
 mbedtls_ctr_drbg_init(&g_drbg);
 tls_announce_all_cnames();
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (const unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Server: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 g_listen_fd4.fd = net_bind("TLS", "0.0.0.0", gconf->dht_port, ((void *)0), IPPROTO_TCP);
 g_listen_fd6.fd = net_bind("TLS", "::", gconf->dht_port, ((void *)0), IPPROTO_TCP);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  1,
  0,
  0)) != 0)
 {
  log_print(3, "TLS-Server: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_authmode(&g_conf, 2);
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_sni(&g_conf, sni_callback, g_sni_entries);
 if ((ret = mbedtls_ssl_setup(&g_ssl, &g_conf)) != 0) {
  log_print(3, "TLS-Server: mbedtls_ssl_setup returned -0x%x", -ret);;
  return 1;
 }
 if (g_listen_fd4.fd > -1) {
  mbedtls_net_set_nonblock(&g_listen_fd4);
  net_add_handler(g_listen_fd4.fd, &tls_server_handler);
 }
 if (g_listen_fd6.fd > -1) {
  mbedtls_net_set_nonblock(&g_listen_fd6);
  net_add_handler(g_listen_fd6.fd, &tls_server_handler);
 }
 return 0;
}

canonical_original_code:
int tls_server_setup()
{
 char *pers = "kadnode";
 int ret;
 if (g_sni_entries == ((void *)0)) {
  return 0;
 }
 mbedtls_net_init(&g_client_fd4);
 mbedtls_net_init(&g_listen_fd4);
 mbedtls_net_init(&g_client_fd6);
 mbedtls_net_init(&g_listen_fd6);
 mbedtls_ssl_init(&g_ssl);
 mbedtls_ssl_config_init(&g_conf);
 mbedtls_ctr_drbg_init(&g_drbg);
 tls_announce_all_cnames();
 mbedtls_entropy_init(&g_entropy);
 if ((ret = mbedtls_ctr_drbg_seed(&g_drbg, mbedtls_entropy_func, &g_entropy,
  (unsigned char *) pers, strlen(pers))) != 0) {
  log_print(3, "TLS-Server: mbedtls_ctr_drbg_seed returned -0x%x", -ret);;
  return 1;
 }
 g_listen_fd4.fd = net_bind("TLS", "0.0.0.0", gconf->dht_port, ((void *)0), IPPROTO_TCP);
 g_listen_fd6.fd = net_bind("TLS", "::", gconf->dht_port, ((void *)0), IPPROTO_TCP);
 if ((ret = mbedtls_ssl_config_defaults(&g_conf,
  1,
  0,
  0)) != 0)
 {
  log_print(3, "TLS-Server: mbedtls_ssl_config_defaults returned -0x%x", -ret);;
  return 1;
 }
 mbedtls_ssl_conf_authmode(&g_conf, 2);
 mbedtls_ssl_conf_rng(&g_conf, mbedtls_ctr_drbg_random, &g_drbg);
 mbedtls_ssl_conf_sni(&g_conf, sni_callback, g_sni_entries);
 if ((ret = mbedtls_ssl_setup(&g_ssl, &g_conf)) != 0) {
  log_print(3, "TLS-Server: mbedtls_ssl_setup returned -0x%x", -ret);;
  return 1;
 }
 if (g_listen_fd4.fd > -1) {
  mbedtls_net_set_nonblock(&g_listen_fd4);
  net_add_handler(g_listen_fd4.fd, &tls_server_handler);
 }
 if (g_listen_fd6.fd > -1) {
  mbedtls_net_set_nonblock(&g_listen_fd6);
  net_add_handler(g_listen_fd6.fd, &tls_server_handler);
 }
 return 0;
}

variable_types:
pers
char *


decompiled_code:
void tls_server_free()
{
;
}

canonical_decompiled_code:
void func9()
{
;
}

original_code:
void tls_server_free(void)
{
}

canonical_original_code:
void tls_server_free()
{
}


match

original_code:
static int match(const char request[], const char fmt[])
{
 int n = -1;
 sscanf(request, fmt, &n);
 return (n > 0 && request[n] == '\0');
}

canonical_original_code:
static int match(char request[], char fmt[])
{
 int n = -1;
 sscanf(request, fmt, &n);
 return (n > 0 && request[n] == '\0');
}

variable_types:
request
char []

variable_types:
fmt
char []

variable_types: n int


decompiled_code:
void __fastcall cmd_console_handler(int a1)
{
char v1[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a1 > 0 )
{
if ( fgets(v1, 256, stdin) )
cmd_exec(stdout, (__int64)v1, 1);
}
}

canonical_decompiled_code:
void __fastcall func5(int a1)
{
char v1[264]; // [rsp-108h] [rbp-108h] BYREF

if ( a1 > 0 )
{
if ( fgets(v1, 256, stdin) )
func4(stdout, (__int64)v1, 1);
}
}

original_code:
static void cmd_console_handler(int rc, int fd)
{
 char request[256];
 char *ptr;
 if (rc <= 0) {
  return;
 }
 ptr = fgets(request, sizeof(request), stdin);
 if (ptr == ((void *)0)) {
  return;
 }
 cmd_exec(stdout, request, 1);
}

variable_types: rc int

variable_types: fd int

variable_types:
request
char [256]

variable_types:
ptr
char *


decompiled_code:
void __fastcall cmd_server_handler(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax

if ( a1 > 0 )
{
v2 = accept(a2, 0LL, 0LL);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &cmd_client_handler);
}
}
}

canonical_decompiled_code:
void __fastcall func6(int a1, int a2)
{
int v2; // eax
int *v3; // rax
char *v4; // rax

if ( a1 > 0 )
{
v2 = accept(a2, 0LL, 0LL);
if ( v2 < 0 )
{
v3 = _errno_location();
v4 = strerror(*v3);
log_print(3LL, "accept(): %s", v4);
}
else
{
net_add_handler((unsigned int)v2, &cmd_client_handler);
}
}
}

original_code:
static void cmd_server_handler(int rc, int serversock)
{
 int clientsock;
 if (rc <= 0) {
  return;
 }
 clientsock = accept(serversock, ((void *)0), ((void *)0));
 if (clientsock < 0) {
  log_print(3, "accept(): %s", strerror((*__errno_location ())));;
  return;
 }
 net_add_handler(clientsock, &cmd_client_handler);
}

variable_types: serversock int

variable_types: clientsock int


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

result = unix_create_unix_socket(*(_QWORD *)(gconf + 96LL), &g_cmd_sock);
if ( (_DWORD)result != 1 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "CMD: Bind to %s", *(const char **)(gconf + 96LL));
net_add_handler((unsigned int)g_cmd_sock, cmd_server_handler);
result = 0LL;
if ( !*(_BYTE *)(gconf + 56LL) && !*(_BYTE *)(gconf + 104LL) )
{
fwrite("Press Enter for help.\n", 1uLL, 0x16uLL, stdout);
net_add_handler(0LL, cmd_console_handler);
result = 0LL;
}
}
return result;
}

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

result = unix_create_unix_socket(*(_QWORD *)(gconf + 96LL), &g_cmd_sock);
if ( (_DWORD)result != 1 )
{
if ( *(_DWORD *)(gconf + 60LL) != 2 )
log_print(6LL, "CMD: Bind to %s", *(const char **)(gconf + 96LL));
net_add_handler((unsigned int)g_cmd_sock, cmd_server_handler);
result = 0LL;
if ( !*(_BYTE *)(gconf + 56LL) && !*(_BYTE *)(gconf + 104LL) )
{
fwrite("Press Enter for help.\n", 1uLL, 0x16uLL, stdout);
net_add_handler(0LL, cmd_console_handler);
result = 0LL;
}
}
return result;
}

original_code:
int cmd_setup(void)
{
 if (1 == unix_create_unix_socket(gconf->cmd_path, &g_cmd_sock)) {
  return 1;
 } else {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "CMD: Bind to %s", gconf->cmd_path); } while (0);
  net_add_handler(g_cmd_sock, &cmd_server_handler);
  if (!gconf->is_daemon && !gconf->cmd_disable_stdin) {
   fprintf(stdout, "Press Enter for help.\n");
   net_add_handler(0, &cmd_console_handler);
  }
  return 0;
 }
}

canonical_original_code:
int cmd_setup()
{
 if (1 == unix_create_unix_socket(gconf->cmd_path, &g_cmd_sock)) {
  return 1;
 } else {
  do { if (gconf->verbosity != VERBOSITY_QUIET) log_print(6, "CMD: Bind to %s", gconf->cmd_path); } while (0);
  net_add_handler(g_cmd_sock, &cmd_server_handler);
  if (!gconf->is_daemon && !gconf->cmd_disable_stdin) {
   fprintf(stdout, "Press Enter for help.\n");
   net_add_handler(0, &cmd_console_handler);
  }
  return 0;
 }
}


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

if ( g_cmd_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 96LL));
return result;
}

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

if ( g_cmd_sock >= 0 )
result = unix_remove_unix_socket(*(_QWORD *)(gconf + 96LL));
return result;
}

original_code:
void cmd_free(void)
{
 if (g_cmd_sock >= 0) {
  unix_remove_unix_socket(gconf->cmd_path, g_cmd_sock);
 }
}

canonical_original_code:
void cmd_free()
{
 if (g_cmd_sock >= 0) {
  unix_remove_unix_socket(gconf->cmd_path, g_cmd_sock);
 }
}