decompiled_code:
const char *__fastcall parse_cmdline(int a1, const char **a2)
{
int i; // ebp
const char *v4; // rdx
char v5; // al
char *v6; // r13
const char *result; // rax
__int64 *v8; // rax
__int64 v9; // rcx
__int64 v10; // rdx
unsigned int j; // ebx
const char *v12; // rdx
__int64 v13; // rsi
__int64 v14; // rax
__int64 v15; // rbx
__int64 v16; // rax
__int64 v17; // rax
__int64 v18; // r12
unsigned int *v19; // rbx
int v20; // ebp
__int64 v21; // rdx
const char *v22; // rsi
__int64 v23; // r14
__int64 v24; // r15
unsigned int v25; // [rsp+Ch] [rbp-3Ch]

if ( a1 )
{
for ( i = a1; i; --i )
{
v4 = *a2;
v5 = (*a2)[1];
if ( **a2 != 45 )
break;
if ( v4[2] )
{
v6 = (char *)(v4 + 2);
++i;
--a2;
if ( v5 != 45 )
{
switch ( v5 )
{
case 'd':
goto LABEL_7;
case 'f':
goto LABEL_38;
case 'h':
goto LABEL_50;
case 'i':
goto LABEL_26;
case 'l':
goto LABEL_21;
case 'm':
goto LABEL_17;
case 'o':
goto LABEL_15;
default:
goto LABEL_49;
}
}
}
else if ( i == 1 )
{
if ( v5 == 45 )
{
++a2;
return *a2;
}
switch ( v5 )
{
case 'd':
LABEL_7:
*((_DWORD *)&cobaye_conf + 118) = 1;
break;
case 'f':
LABEL_43:
puts("option -f expects a format specifier");
break;
case 'h':
goto LABEL_50;
case 'i':
LABEL_41:
puts("option -i expects a test or sequence name");
goto LABEL_25;
case 'l':
LABEL_21:
for ( j = 0; cobaye_count > j; ++j )
{
v12 = "manual";
v13 = cobaye_list + 96LL * j;
if ( (*(_BYTE *)(*(_QWORD *)(v13 + 72) + 324LL) & 2) != 0 )
v12 = "auto";
printf(" - %s (%s)\n", v13 + 4, v12);
}
LABEL_25:
exit(0);
case 'm':
LABEL_17:
if ( cobaye_count )
{
v8 = (__int64 *)(cobaye_list + 72);
v9 = cobaye_list + 96LL * (unsigned int)(cobaye_count - 1) + 168;
do
{
v10 = *v8;
v8 += 12;
*(_DWORD *)(v10 + 324) |= 2u;
}
while ( v8 != (__int64 *)v9 );
}
break;
case 'o':
LABEL_42:
puts("option -o expects a report name");
break;
default:
LABEL_49:
printf("unknown argument '%s'\n", *a2);
LABEL_50:
usage_isra_0();
return result;
}
}
else
{
v6 = (char *)a2[1];
if ( v5 != 45 )
{
switch ( v5 )
{
case 'd':
goto LABEL_7;
case 'f':
if ( !v6 )
goto LABEL_43;
LABEL_38:
if ( strstr(v6, "all") )
{
*((_DWORD *)&cobaye_conf + 70) = -1;
}
else
{
v23 = cobaye_report;
v24 = 0LL;
v25 = cobaye_report_count;
while ( v25 > (unsigned int)v24 )
{
if ( strstr(v6, *(const char **)(v23 + 8 * v24)) )
*((_DWORD *)&cobaye_conf + 70) |= 1 << v24;
++v24;
}
}
--i;
++a2;
break;
case 'h':
goto LABEL_50;
case 'i':
LABEL_26:
if ( !v6 )
goto LABEL_41;
v14 = cobaye_test_exist(v6);
v15 = v14;
if ( v14 )
{
printf("test: '%s':\n", (const char *)(v14 + 4));
printf("descr: %s\n", (const char *)(*(_QWORD *)(v15 + 72) + 64LL));
printf("flags: ");
v16 = *(_QWORD *)(v15 + 72);
if ( !*(_DWORD *)(v16 + 324) )
{
printf("none");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 1) != 0 )
{
printf("NO_FORK");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 2) != 0 )
{
printf("NO_USER");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 4) != 0 )
printf("BENCH");
putchar(10);
}
else
{
v17 = cobaye_build_seq(v6);
v18 = v17;
if ( v17 )
{
v19 = (unsigned int *)(v17 + 184);
v20 = 0;
printf("sequence: '%s': %d entries\n", (const char *)(v17 + 4), *(unsigned int *)(v17 + 88));
while ( *(_DWORD *)(v18 + 88) > v20 )
{
v21 = *v19;
v22 = (const char *)(v19 - 21);
++v20;
v19 += 24;
printf(" - test '%s' x %d\n", v22, v21);
}
}
else
{
printf("test '%s' doesn't exist\n", v6);
}
}
goto LABEL_25;
case 'l':
goto LABEL_21;
case 'm':
goto LABEL_17;
case 'o':
LABEL_15:
if ( !v6 )
goto LABEL_42;
--i;
++a2;
*((_QWORD *)&cobaye_conf + 47) = strdup(v6);
break;
default:
goto LABEL_49;
}
}
}
++a2;
}
}
return *a2;
}

canonical_decompiled_code:
const char *__fastcall func3(int a1, const char **a2)
{
int i; // ebp
const char *v4; // rdx
char v5; // al
char *v6; // r13
const char *result; // rax
__int64 *v8; // rax
__int64 v9; // rcx
__int64 v10; // rdx
unsigned int j; // ebx
const char *v12; // rdx
__int64 v13; // rsi
__int64 v14; // rax
__int64 v15; // rbx
__int64 v16; // rax
__int64 v17; // rax
__int64 v18; // r12
unsigned int *v19; // rbx
int v20; // ebp
__int64 v21; // rdx
const char *v22; // rsi
__int64 v23; // r14
__int64 v24; // r15
unsigned int v25; // [rsp+Ch] [rbp-3Ch]

if ( a1 )
{
for ( i = a1; i; --i )
{
v4 = *a2;
v5 = (*a2)[1];
if ( **a2 != 45 )
break;
if ( v4[2] )
{
v6 = (char *)(v4 + 2);
++i;
--a2;
if ( v5 != 45 )
{
switch ( v5 )
{
case 'd':
goto LABEL_7;
case 'f':
goto LABEL_38;
case 'h':
goto LABEL_50;
case 'i':
goto LABEL_26;
case 'l':
goto LABEL_21;
case 'm':
goto LABEL_17;
case 'o':
goto LABEL_15;
default:
goto LABEL_49;
}
}
}
else if ( i == 1 )
{
if ( v5 == 45 )
{
++a2;
return *a2;
}
switch ( v5 )
{
case 'd':
LABEL_7:
*((_DWORD *)&cobaye_conf + 118) = 1;
break;
case 'f':
LABEL_43:
puts("option -f expects a format specifier");
break;
case 'h':
goto LABEL_50;
case 'i':
LABEL_41:
puts("option -i expects a test or sequence name");
goto LABEL_25;
case 'l':
LABEL_21:
for ( j = 0; cobaye_count > j; ++j )
{
v12 = "manual";
v13 = cobaye_list + 96LL * j;
if ( (*(_BYTE *)(*(_QWORD *)(v13 + 72) + 324LL) & 2) != 0 )
v12 = "auto";
printf(" - %s (%s)\n", v13 + 4, v12);
}
LABEL_25:
exit(0);
case 'm':
LABEL_17:
if ( cobaye_count )
{
v8 = (__int64 *)(cobaye_list + 72);
v9 = cobaye_list + 96LL * (unsigned int)(cobaye_count - 1) + 168;
do
{
v10 = *v8;
v8 += 12;
*(_DWORD *)(v10 + 324) |= 2u;
}
while ( v8 != (__int64 *)v9 );
}
break;
case 'o':
LABEL_42:
puts("option -o expects a report name");
break;
default:
LABEL_49:
printf("unknown argument '%s'\n", *a2);
LABEL_50:
func1();
return result;
}
}
else
{
v6 = (char *)a2[1];
if ( v5 != 45 )
{
switch ( v5 )
{
case 'd':
goto LABEL_7;
case 'f':
if ( !v6 )
goto LABEL_43;
LABEL_38:
if ( strstr(v6, "all") )
{
*((_DWORD *)&cobaye_conf + 70) = -1;
}
else
{
v23 = cobaye_report;
v24 = 0LL;
v25 = cobaye_report_count;
while ( v25 > (unsigned int)v24 )
{
if ( strstr(v6, *(const char **)(v23 + 8 * v24)) )
*((_DWORD *)&cobaye_conf + 70) |= 1 << v24;
++v24;
}
}
--i;
++a2;
break;
case 'h':
goto LABEL_50;
case 'i':
LABEL_26:
if ( !v6 )
goto LABEL_41;
v14 = func2(v6);
v15 = v14;
if ( v14 )
{
printf("test: '%s':\n", (const char *)(v14 + 4));
printf("descr: %s\n", (const char *)(*(_QWORD *)(v15 + 72) + 64LL));
printf("flags: ");
v16 = *(_QWORD *)(v15 + 72);
if ( !*(_DWORD *)(v16 + 324) )
{
printf("none");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 1) != 0 )
{
printf("NO_FORK");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 2) != 0 )
{
printf("NO_USER");
v16 = *(_QWORD *)(v15 + 72);
}
if ( (*(_BYTE *)(v16 + 324) & 4) != 0 )
printf("BENCH");
putchar(10);
}
else
{
v17 = cobaye_build_seq(v6);
v18 = v17;
if ( v17 )
{
v19 = (unsigned int *)(v17 + 184);
v20 = 0;
printf("sequence: '%s': %d entries\n", (const char *)(v17 + 4), *(unsigned int *)(v17 + 88));
while ( *(_DWORD *)(v18 + 88) > v20 )
{
v21 = *v19;
v22 = (const char *)(v19 - 21);
++v20;
v19 += 24;
printf(" - test '%s' x %d\n", v22, v21);
}
}
else
{
printf("test '%s' doesn't exist\n", v6);
}
}
goto LABEL_25;
case 'l':
goto LABEL_21;
case 'm':
goto LABEL_17;
case 'o':
LABEL_15:
if ( !v6 )
goto LABEL_42;
--i;
++a2;
*((_QWORD *)&cobaye_conf + 47) = strdup(v6);
break;
default:
goto LABEL_49;
}
}
}
++a2;
}
}
return *a2;
}

original_code:
static char *parse_cmdline(int argc, char **argv)
{
 unsigned int index = 0;
 while (argc) {
  char opt = argv[0][1];
  char *value = argv[1];
  if (argv[0][0] != '-')
   break;
  if (argv[0][2]) {
   value = argv[0] + 2;
   argc++;
   argv--;
  } else if (argc == 1) {
   value = ((void *)0);
  }
  switch (opt) {
  case '-':
   break;
  case 'm':
   for (index = 0; index < cobaye_count; index++)
    cobaye_list[index].test->flags |= 0x0002;
   break;
  case 'd':
   cobaye_conf[item_date].value = 1;
   break;
  case 'i':
   if (value) {
    struct cobaye_entry *entry = cobaye_test_exist(value);
    if (entry) {
     printf("test: '%s':\n", entry->name);
     printf("descr: %s\n", entry->test->descr);
     printf("flags: ");
     if (!entry->test->flags)
      printf("none");
     if (entry->test->flags & 0x0001)
      printf("NO_FORK");
     if (entry->test->flags & 0x0002)
      printf("NO_USER");
     if (entry->test->flags & 0x0004)
      printf("BENCH");
     printf("\n");
    } else {
     entry = cobaye_build_seq(value);
     if (entry) {
      int i;
      printf("sequence: '%s': %d entries\n", entry->name, entry->value);
      for (i = 0; i < entry->value; i++)
       printf("  - test '%s' x %d\n", entry[i+1].name, entry[i+1].value);
     } else {
      printf("test '%s' doesn't exist\n", value);
     }
    }
   } else {
    printf("option -i expects a test or sequence name\n");
   }
   exit(0);
   break;
  case 'l':
   for (index = 0; index < cobaye_count; index++)
    printf(" - %s (%s)\n", cobaye_list[index].name, (cobaye_list[index].test-> flags & 0x0002) ? "auto" : "manual");
   exit(0);
   break;
  case 'h':
   usage();
   break;
  case 'f':
   if (value) {
    if(strstr(value, "all")) {
     cobaye_conf[item_report].value = 0xffffffff;
    } else {
     for (index = 0; index < cobaye_report_count; index++)
      if (strstr(value, cobaye_report[index]->ext))
       cobaye_conf[item_report].value |= (1 << index);
    }
    argc--;
    argv++;
   } else {
    printf("option -f expects a format specifier\n");
   }
   break;
  case 'o':
   if (value) {
    cobaye_conf[item_filename].string = strdup(value);
    argc--;
    argv++;
   } else {
    printf("option -o expects a report name\n");
   }
   break;
  default:
   printf("unknown argument '%s'\n", argv[0]);
   usage();
   break;
  }
  argc--;
  argv++;
 }
 return argv[0];
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
index
unsigned int

variable_types: opt char

variable_types:
value
char *

variable_types: i int

user_defined_types: enum cobaye_type { cobaye_inval=-1, cobaye_func=0, cobaye_int=1, cobaye_str=2 }

user_defined_types: struct cobaye_test { char name[64]; char descr[256]; int result; int flags; int (*main)(); }


main

canonical_name: main

repo: blunderer/cobaye

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rbp
__int64 v5; // rdi
__int64 v7; // rax
__int64 v8; // r12
int v9; // er13

cobaye_count = cobaye_build_list(0LL);
cobaye_list = (__int64)calloc((unsigned int)cobaye_count, 0x60uLL);
cobaye_count = cobaye_build_list(cobaye_list);
cobaye_report_count = cobaye_build_report_list(&cobaye_report);
v3 = (char *)parse_cmdline(argc - 1, argv + 1);
if ( !v3 )
usage_isra_0();
v4 = v3;
v5 = cobaye_test_exist(v3);
if ( v5 )
return cobaye_run_tst(v5);
v7 = cobaye_build_seq(v4);
v8 = v7;
if ( v7 )
{
v9 = cobaye_run_tst(v7);
cobaye_destroy_seq(v8);
}
else
{
v9 = -1;
printf("test '%s' doesn't exist\n", v4);
}
return v9;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rbp
__int64 v5; // rdi
__int64 v7; // rax
__int64 v8; // r12
int v9; // er13

cobaye_count = func0(0LL);
cobaye_list = (__int64)calloc((unsigned int)cobaye_count, 0x60uLL);
cobaye_count = func0(cobaye_list);
cobaye_report_count = cobaye_build_report_list(&cobaye_report);
v3 = (char *)func3(argc - 1, argv + 1);
if ( !v3 )
func1();
v4 = v3;
v5 = func2(v3);
if ( v5 )
return cobaye_run_tst(v5);
v7 = cobaye_build_seq(v4);
v8 = v7;
if ( v7 )
{
v9 = cobaye_run_tst(v7);
cobaye_destroy_seq(v8);
}
else
{
v9 = -1;
printf("test '%s' doesn't exist\n", v4);
}
return v9;
}

original_code:
int main(int argc, char **argv)
{
 int ret;
 char *target;
 struct cobaye_entry *entry;
 cobaye_count = cobaye_build_list(((void *)0));
 cobaye_list = calloc(cobaye_count, sizeof(struct cobaye_entry));
 cobaye_count = cobaye_build_list(cobaye_list);
 cobaye_report_count = cobaye_build_report_list(&cobaye_report);
 target = parse_cmdline(argc - 1, argv + 1);
 if (!target)
  usage();
 entry = cobaye_test_exist(target);
 if (entry) {
  ret = cobaye_run_tst(entry);
 } else {
  struct cobaye_entry *seq = cobaye_build_seq(target);
  if (seq) {
   ret = cobaye_run_tst(seq);
   cobaye_destroy_seq(seq);
  } else {
   ret = -1;
   printf("test '%s' doesn't exist\n", target);
  }
 }
 return ret;
}

variable_types: ret int

variable_types:
target
char *

variable_types: struct cobaye_entry { enum cobaye_type { cobaye_inval=-1, cobaye_func=0, cobaye_int=1, cobaye_str=2 } type; char name[64]; struct cobaye_test { char name[64]; char descr[256]; int result; int flags; int (*main)(); } *test; struct cobaye_entry *seq; union { int value; char *string; }; } *

return_type: int

user_defined_types: struct cobaye_entry { enum cobaye_type type; char name[64]; struct cobaye_test *test; struct cobaye_entry *seq; union { int value; char *string; }; }


decompiled_code:
__int64 __fastcall cobaye_fork(__int64 a1, unsigned int a2)
{
__pid_t v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // er15
int v8; // er14
unsigned int v9; // er13
int v11; // er12
__pid_t pid; // [rsp+Ch] [rbp-ECh]
int stat_loc; // [rsp+14h] [rbp-E4h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-E0h] BYREF
int v15[2]; // [rsp+20h] [rbp-D8h] BYREF
int v16[2]; // [rsp+28h] [rbp-D0h] BYREF
int v17[2]; // [rsp+30h] [rbp-C8h] BYREF
int v18[2]; // [rsp+38h] [rbp-C0h] BYREF
int buf[46]; // [rsp+40h] [rbp-B8h] BYREF

pipe(pipedes);
pipe(v15);
pipe(v16);
pipe(v17);
pipe(v18);
v2 = fork();
pid = v2;
if ( !v2 )
{
close(v18[1]);
close(v17[0]);
close(pipedes[0]);
close(v16[1]);
close(v15[0]);
dup2(v16[0], 0);
dup2(v15[1], 1);
dup2(pipedes[1], 2);
close(v16[0]);
close(v15[1]);
close(pipedes[1]);
v11 = (*(__int64 (**)(void))(a1 + 328))();
fflush(stdout);
fflush(stderr);
write(v17[1], &unk_558, 1uLL);
do
{
buf[0] = 0;
read(v18[0], buf, 1uLL);
}
while ( !buf[0] );
exit(v11);
}
if ( v2 <= 0 )
return (unsigned int)-1;
close(pipedes[1]);
close(v17[1]);
close(v18[0]);
close(v15[1]);
close(v16[0]);
v3 = fcntl(0, 3, 0LL);
BYTE1(v3) |= 8u;
fcntl(0, 4, v3);
v4 = fcntl(v15[0], 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v15[0], 4, v4);
v5 = fcntl(pipedes[0], 3, 0LL);
BYTE1(v5) |= 8u;
fcntl(pipedes[0], 4, v5);
v6 = fcntl(v17[0], 3, 0LL);
BYTE1(v6) |= 8u;
stat_loc = v6;
fcntl(v17[0], 4, v6);
do
{
v8 = read(v17[0], buf, 1uLL);
while ( 1 )
{
memset(buf, 0, 0x80uLL);
if ( (int)read(v15[0], buf, 0x7FuLL) <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 1LL);
}
while ( 1 )
{
memset(buf, 0, 0x80uLL);
if ( (int)read(pipedes[0], buf, 0x7FuLL) <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 2LL);
}
while ( 1 )
{
memset(buf, 0, 0x80uLL);
v7 = read(0, buf, 0x7FuLL);
if ( v7 <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 0LL);
write(v16[1], buf, v7);
}
}
while ( v8 <= 0 );
write(v18[1], &unk_558, 1uLL);
v9 = waitpid(pid, &stat_loc, 0);
if ( pid == v9 )
{
v9 = BYTE1(stat_loc);
*(_DWORD *)(a1 + 320) = BYTE1(stat_loc);
}
close(pipedes[0]);
close(v15[0]);
close(v16[1]);
close(v17[0]);
close(v18[1]);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
__pid_t v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // er15
int v8; // er14
unsigned int v9; // er13
int v11; // er12
__pid_t pid; // [rsp+Ch] [rbp-ECh]
int stat_loc; // [rsp+14h] [rbp-E4h] BYREF
int pipedes[2]; // [rsp+18h] [rbp-E0h] BYREF
int v15[2]; // [rsp+20h] [rbp-D8h] BYREF
int v16[2]; // [rsp+28h] [rbp-D0h] BYREF
int v17[2]; // [rsp+30h] [rbp-C8h] BYREF
int v18[2]; // [rsp+38h] [rbp-C0h] BYREF
int buf[46]; // [rsp+40h] [rbp-B8h] BYREF

pipe(pipedes);
pipe(v15);
pipe(v16);
pipe(v17);
pipe(v18);
v2 = fork();
pid = v2;
if ( !v2 )
{
close(v18[1]);
close(v17[0]);
close(pipedes[0]);
close(v16[1]);
close(v15[0]);
dup2(v16[0], 0);
dup2(v15[1], 1);
dup2(pipedes[1], 2);
close(v16[0]);
close(v15[1]);
close(pipedes[1]);
v11 = (*(__int64 (**)(void))(a1 + 328))();
fflush(stdout);
fflush(stderr);
write(v17[1], &unk_558, 1uLL);
do
{
buf[0] = 0;
read(v18[0], buf, 1uLL);
}
while ( !buf[0] );
exit(v11);
}
if ( v2 <= 0 )
return (unsigned int)-1;
close(pipedes[1]);
close(v17[1]);
close(v18[0]);
close(v15[1]);
close(v16[0]);
v3 = fcntl(0, 3, 0LL);
BYTE1(v3) |= 8u;
fcntl(0, 4, v3);
v4 = fcntl(v15[0], 3, 0LL);
BYTE1(v4) |= 8u;
fcntl(v15[0], 4, v4);
v5 = fcntl(pipedes[0], 3, 0LL);
BYTE1(v5) |= 8u;
fcntl(pipedes[0], 4, v5);
v6 = fcntl(v17[0], 3, 0LL);
BYTE1(v6) |= 8u;
stat_loc = v6;
fcntl(v17[0], 4, v6);
do
{
v8 = read(v17[0], buf, 1uLL);
while ( 1 )
{
memset(buf, 0, 0x80uLL);
if ( (int)read(v15[0], buf, 0x7FuLL) <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 1LL);
}
while ( 1 )
{
memset(buf, 0, 0x80uLL);
if ( (int)read(pipedes[0], buf, 0x7FuLL) <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 2LL);
}
while ( 1 )
{
memset(buf, 0, 0x80uLL);
v7 = read(0, buf, 0x7FuLL);
if ( v7 <= 0 )
break;
cobaye_report_all(8LL, a1, a2, buf, 0LL);
write(v16[1], buf, v7);
}
}
while ( v8 <= 0 );
write(v18[1], &unk_558, 1uLL);
v9 = waitpid(pid, &stat_loc, 0);
if ( pid == v9 )
{
v9 = BYTE1(stat_loc);
*(_DWORD *)(a1 + 320) = BYTE1(stat_loc);
}
close(pipedes[0]);
close(v15[0]);
close(v16[1]);
close(v17[0]);
close(v18[1]);
return v9;
}

original_code:
static int cobaye_fork(struct cobaye_test *test, int iter)
{
 int ret = 0;
 int pid = 0;
 int err_from_tst[2];
 int data_from_tst[2];
 int data_to_tst[2];
 int ctrl_from_tst[2];
 int ctrl_to_tst[2];
 ret = pipe(err_from_tst);
 ret = pipe(data_from_tst);
 ret = pipe(data_to_tst);
 ret = pipe(ctrl_from_tst);
 ret = pipe(ctrl_to_tst);
 pid = fork();
 if (pid == 0) {
  int ret;
  close(ctrl_to_tst[1]);
  close(ctrl_from_tst[0]);
  close(err_from_tst[0]);
  close(data_to_tst[1]);
  close(data_from_tst[0]);
  dup2(data_to_tst[0], 0);
  dup2(data_from_tst[1], 1);
  dup2(err_from_tst[1], 2);
  close(data_to_tst[0]);
  close(data_from_tst[1]);
  close(err_from_tst[1]);
  ret = test->main();
  fflush(stdout);
  fflush(stderr);
  write(ctrl_from_tst[1], "Q", 1);
  do {
   int cmd = 0;
   read(ctrl_to_tst[0], &cmd, 1);
   if (cmd)
    break;
  } while (1);
  exit(ret);
 } else if (pid > 0) {
  int status;
  int tst_running = 1;
  close(err_from_tst[1]);
  close(ctrl_from_tst[1]);
  close(ctrl_to_tst[0]);
  close(data_from_tst[1]);
  close(data_to_tst[0]);
  status = fcntl(0, 3, 0) | 04000;
  fcntl(0, 4, status);
  status = fcntl(data_from_tst[0], 3, 0) | 04000;
  fcntl(data_from_tst[0], 4, status);
  status = fcntl(err_from_tst[0], 3, 0) | 04000;
  fcntl(err_from_tst[0], 4, status);
  status = fcntl(ctrl_from_tst[0], 3, 0) | 04000;
  fcntl(ctrl_from_tst[0], 4, status);
  while (tst_running) {
   int nbytes;
   char buffer[128];
   nbytes = read(ctrl_from_tst[0], buffer, 1);
   if (nbytes > 0)
    tst_running = 0;
   do {
    memset(buffer, 0, sizeof(buffer));
    nbytes = read(data_from_tst[0], buffer, sizeof(buffer) - 1);
    if (nbytes > 0)
     cobaye_report_all(8, test->name, iter, buffer, 1);
   } while (nbytes > 0);
   do {
    memset(buffer, 0, sizeof(buffer));
    nbytes = read(err_from_tst[0], buffer, sizeof(buffer) - 1);
    if (nbytes > 0)
     cobaye_report_all(8, test->name, iter, buffer, 2);
   } while (nbytes > 0);
   do {
    memset(buffer, 0, sizeof(buffer));
    nbytes = read(0, buffer, sizeof(buffer) - 1);
    if (nbytes > 0) {
     cobaye_report_all(8, test->name, iter, buffer, 0);
     write(data_to_tst[1], buffer, nbytes);
    }
   } while (nbytes > 0);
  }
  write(ctrl_to_tst[1], "Q", 1);
  ret = waitpid(pid, &status, 0);
  if (ret == pid) {
   ret = (((status) & 0xff00) >> 8);
   test->result = ret;
  }
  close(err_from_tst[0]);
  close(data_from_tst[0]);
  close(data_to_tst[1]);
  close(ctrl_from_tst[0]);
  close(ctrl_to_tst[1]);
 } else {
  ret = -1;
 }
 return ret;
}

variable_types: struct cobaye_test { char name[64]; char descr[256]; int result; int flags; int (*main)(); } *

variable_types: iter int

variable_types: pid int

variable_types: cmd int

variable_types: status int

variable_types: nbytes int

variable_types:
buffer
char [128]


decompiled_code:
_BOOL8 cobaye_forked()
{
return cobaye_pid != getpid();
}

canonical_decompiled_code:
_BOOL8 func3()
{
return cobaye_pid != getpid();
}

original_code:
int cobaye_forked(void)
{
 if (getpid() == cobaye_pid) {
  return 0;
 }
 return 1;
}

canonical_original_code:
int cobaye_forked()
{
 if (getpid() == cobaye_pid) {
  return 0;
 }
 return 1;
}


decompiled_code:
__int64 __fastcall cobaye_run_tst(__int64 a1)
{
__pid_t v2; // eax
__int64 v3; // rdi
__int64 v4; // rsi
unsigned int v5; // er12
unsigned int v6; // er13
__int64 v7; // rsi
__int64 v8; // rdi
int v9; // eax
__int64 v10; // rsi
bool v11; // zf
int v13; // eax

v2 = getpid();
v3 = *(_QWORD *)(a1 + 80);
cobaye_pid = v2;
cobaye_run_tst_pre_isra_0(v3);
v4 = *(_QWORD *)(a1 + 72);
if ( v4 )
{
cobaye_report_all(1LL, v4, 0LL, 0LL, 0LL);
if ( cobaye_conf[46] > 0 )
{
v5 = 0;
v6 = 0;
while ( 1 )
{
v7 = *(_QWORD *)(a1 + 72);
if ( (*(_BYTE *)(v7 + 324) & 2) == 0 )
{
cobaye_report_all(4LL, v7, v5, 0LL, 0LL);
goto LABEL_12;
}
cobaye_report_all(3LL, v7, v5, 0LL, 0LL);
v8 = *(_QWORD *)(a1 + 72);
if ( (*(_BYTE *)(v8 + 324) & 1) != 0 )
{
v9 = (*(__int64 (**)(void))(v8 + 328))();
v10 = *(_QWORD *)(a1 + 72);
v6 = v9;
v11 = v9 == 0;
if ( v9 < 0 )
goto LABEL_15;
}
else
{
v13 = cobaye_fork(v8, v5);
v10 = *(_QWORD *)(a1 + 72);
v6 = v13;
v11 = v13 == 0;
if ( v13 < 0 )
{
LABEL_15:
cobaye_report_all(5LL, v10, v5, 0LL, v6);
goto LABEL_11;
}
}
if ( v11 )
{
cobaye_report_all(6LL, v10, v5, 0LL, 0LL);
goto LABEL_5;
}
cobaye_report_all(7LL, v10, v5, 0LL, v6);
v6 = -1;
LABEL_11:
if ( cobaye_conf[22] == 1 )
goto LABEL_12;
LABEL_5:
if ( cobaye_conf[46] <= (int)++v5 )
goto LABEL_12;
}
}
v6 = 0;
LABEL_12:
cobaye_report_all(2LL, *(_QWORD *)(a1 + 72), 0LL, 0LL, 0LL);
}
else
{
v6 = 0;
cobaye_run_seq(a1);
}
cobaye_run_tst_post_isra_0(*(_QWORD *)(a1 + 80));
return v6;
}

original_code:
int cobaye_run_tst(struct cobaye_entry *entry)
{
 int ret = 0;
 cobaye_pid = getpid();
 cobaye_run_tst_pre(entry);
 if (entry->test) {
  int iter;
  cobaye_report_all(1, entry->test->name, 0, ((void *)0), 0);
  for (iter = 0; iter < cobaye_conf[item_repeat].value; iter++) {
   if (((entry->test->flags & 0x0002) == 0)) {
    cobaye_report_all(4, entry->test->name, iter, ((void *)0), 0);
    break;
   }
   cobaye_report_all(3, entry->test->name, iter, ((void *)0), 0);
   if (entry->test->flags & 0x0001)
    ret = entry->test->main();
   else
    ret = cobaye_fork(entry->test, iter);
   if (ret < 0) {
    cobaye_report_all(5, entry->test->name, iter, ((void *)0), ret);
   } else if (ret == 0) {
    cobaye_report_all(6, entry->test->name, iter, ((void *)0), ret);
   } else {
    cobaye_report_all(7, entry->test->name, iter, ((void *)0), ret);
    ret = -1;
   }
   if ((ret != 0) && (cobaye_conf[item_stop_on_error].value == 1))
    break;
  }
  cobaye_report_all(2, entry->test->name, 0, ((void *)0), 0);
 } else {
  cobaye_run_seq(entry);
 }
 cobaye_run_tst_post(entry);
 return ret;
}


decompiled_code:
__int64 __fastcall cobaye_seq_add_entry(__int64 a1, const char *a2, unsigned int a3)
{
int v4; // eax
int v6; // er12
__int64 v7; // r13
__int64 v8; // r14
__int64 v9; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v4 = strtol(a2, &endptr, 0);
if ( *endptr != 58 )
return a3;
v6 = v4;
++endptr;
v7 = cobaye_test_exist();
if ( !v7 )
return a3;
v8 = a1 + 96LL * (int)a3;
strcpy((char *)(v8 + 4), *(const char **)(v7 + 72));
v9 = *(_QWORD *)(v7 + 72);
*(_DWORD *)(v8 + 88) = v6;
*(_QWORD *)(v8 + 80) = a1;
*(_QWORD *)(v8 + 72) = v9;
return a3 + 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, unsigned int a3)
{
int v4; // eax
int v6; // er12
__int64 v7; // r13
__int64 v8; // r14
__int64 v9; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v4 = strtol(a2, &endptr, 0);
if ( *endptr != 58 )
return a3;
v6 = v4;
++endptr;
v7 = cobaye_test_exist();
if ( !v7 )
return a3;
v8 = a1 + 96LL * (int)a3;
strcpy((char *)(v8 + 4), *(const char **)(v7 + 72));
v9 = *(_QWORD *)(v7 + 72);
*(_DWORD *)(v8 + 88) = v6;
*(_QWORD *)(v8 + 80) = a1;
*(_QWORD *)(v8 + 72) = v9;
return a3 + 1;
}

original_code:
static int cobaye_seq_add_entry(struct cobaye_entry *sequence, char *data, int index)
{
 char *cobaye_name;
 int cobaye_iter = 0;
 struct cobaye_test *newtest = ((void *)0);
 cobaye_iter = strtol(data, &cobaye_name, 0);
 if (cobaye_name[0] == ':') {
  struct cobaye_entry *entry;
  cobaye_name++;
  entry = cobaye_test_exist(cobaye_name);
  if (entry) {
   strcpy(sequence[index].name, entry->test->name);
   sequence[index].test = entry->test;
   sequence[index].value = cobaye_iter;
   sequence[index].seq = sequence;
   index++;
  }
 }
 return index;
}

variable_types:
data
char *

variable_types: index int


original_code:
int cobaye_run_seq(struct cobaye_entry *entry)
{
 int i;
 for (i = 1; i < entry->value + 1; i++) {
  cobaye_conf[item_repeat].value = entry[i].value;
  if (entry[i].test)
   cobaye_run_tst(&entry[i]);
 }
 return 0;
}


decompiled_code:
// attributes: thunk
void cobaye_destroy_seq(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func2(void *ptr)
{
free(ptr);
}

original_code:
void cobaye_destroy_seq(struct cobaye_entry *seq)
{
 free(seq);
}

return_type: void


decompiled_code:
__int64 __fastcall cobaye_report_all(int a1, const char *a2, unsigned int a3, const char *a4, unsigned int a5)
{
int v8; // eax
unsigned int v9; // ebx

if ( a1 == 6 )
{
gettimeofday(&stat_stop_test, 0LL);
++stat_nb_pass;
goto LABEL_6;
}
if ( a1 > 6 )
{
if ( a1 != 7 )
goto LABEL_6;
++stat_nb_fail;
goto LABEL_5;
}
if ( a1 == 3 )
{
gettimeofday(&stat_start_test, 0LL);
++stat_nb_test;
goto LABEL_6;
}
if ( (unsigned int)(a1 - 4) <= 1 )
LABEL_5:
gettimeofday(&stat_stop_test, 0LL);
LABEL_6:
cobaye_report_stdout_isra_0(a1, a2, a3, a4, a5);
v8 = cobaye_conf[70];
if ( v8 && cobaye_report_count )
{
v9 = 0;
while ( 1 )
{
if ( _bittest(&v8, v9) )
{
(*(void (__fastcall **)(_QWORD, const char *, _QWORD, const char *, _QWORD))(*(_QWORD *)(cobaye_report + 8LL * v9)
+ 576LL))(
(unsigned int)a1,
a2,
a3,
a4,
a5);
fflush(*(FILE **)(*(_QWORD *)(cobaye_report + 8LL * v9) + 584LL));
}
if ( cobaye_report_count <= ++v9 )
break;
v8 = cobaye_conf[70];
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, const char *a2, unsigned int a3, const char *a4, unsigned int a5)
{
int v8; // eax
unsigned int v9; // ebx

if ( a1 == 6 )
{
gettimeofday(&stat_stop_test, 0LL);
++stat_nb_pass;
goto LABEL_6;
}
if ( a1 > 6 )
{
if ( a1 != 7 )
goto LABEL_6;
++stat_nb_fail;
goto LABEL_5;
}
if ( a1 == 3 )
{
gettimeofday(&stat_start_test, 0LL);
++stat_nb_test;
goto LABEL_6;
}
if ( (unsigned int)(a1 - 4) <= 1 )
LABEL_5:
gettimeofday(&stat_stop_test, 0LL);
LABEL_6:
func0(a1, a2, a3, a4, a5);
v8 = cobaye_conf[70];
if ( v8 && cobaye_report_count )
{
v9 = 0;
while ( 1 )
{
if ( _bittest(&v8, v9) )
{
(*(void (__fastcall **)(_QWORD, const char *, _QWORD, const char *, _QWORD))(*(_QWORD *)(cobaye_report + 8LL * v9)
+ 576LL))(
(unsigned int)a1,
a2,
a3,
a4,
a5);
fflush(*(FILE **)(*(_QWORD *)(cobaye_report + 8LL * v9) + 584LL));
}
if ( cobaye_report_count <= ++v9 )
break;
v8 = cobaye_conf[70];
}
}
return 0LL;
}

original_code:
int cobaye_report_all(int type, char *name, int iter, char *str, int id)
{
 unsigned int index = 0;
 switch(type) {
 case 3:
  gettimeofday(&stat_start_test, ((void *)0));
  stat_nb_test++;
  break;
 case 7:
  stat_nb_fail++;
 case 4:
 case 5:
  gettimeofday(&stat_stop_test, ((void *)0));
  break;
 case 6:
  gettimeofday(&stat_stop_test, ((void *)0));
  stat_nb_pass++;
  break;
 default:
  break;
 }
 cobaye_report_stdout(type, name, iter, str, id);
 if (!cobaye_conf[item_report].value)
  return 0;
 for (index = 0; index < cobaye_report_count; index++) {
  if (cobaye_conf[item_report].value & (1 << index)) {
   cobaye_report[index]->report(type, name, iter, str, id);
   fflush(cobaye_report[index]->stream);
  }
 }
 return 0;
}

variable_types: type int

variable_types:
name
char *

variable_types:
str
char *

variable_types: id int


decompiled_code:
__int64 cobaye_report_open()
{
int v0; // esi
char *v2; // r13
unsigned int v3; // ebx
int v4; // eax
__int64 v5; // r12
const char **v6; // rbp
__int64 v7; // rbp
FILE *v8; // rax
bool v9; // cc
struct tm *v10; // rax
char *v11; // rax
const char *v12; // rbp
char *v13; // rax
time_t timer; // [rsp+0h] [rbp-250h] BYREF
char s[32]; // [rsp+8h] [rbp-248h] BYREF
char dest[16]; // [rsp+28h] [rbp-228h] BYREF
char v17[496]; // [rsp+38h] [rbp-218h] BYREF

v0 = cobaye_conf[70];
*(_OWORD *)dest = 0LL;
memset(v17, 0, sizeof(v17));
if ( !v0 || reportpath )
return 0LL;
strcpy(dest, *(const char **)&cobaye_conf[94]);
if ( cobaye_conf[118] == 1 )
{
timer = time(0LL);
v10 = localtime(&timer);
sprintf(
s,
"%04d%02d%02d_%02d%02d%02d_",
(unsigned int)(v10->tm_year + 1900),
(unsigned int)(v10->tm_mon + 1),
(unsigned int)v10->tm_mday,
(unsigned int)v10->tm_hour,
(unsigned int)v10->tm_min,
(unsigned int)v10->tm_sec);
v11 = strrchr(dest, 47);
if ( v11 )
strcpy(v11 + 1, s);
else
strcpy(dest, s);
v12 = *(const char **)&cobaye_conf[94];
v13 = strrchr(v12, 47);
if ( v13 )
v12 = v13 + 1;
strcat(dest, v12);
}
v2 = &dest[stpcpy(&reportpath, dest) - &reportpath];
mkdir(&reportpath, 0x1FFu);
if ( cobaye_report_count )
{
v3 = 0;
do
{
while ( 1 )
{
v4 = cobaye_conf[70];
if ( _bittest(&v4, v3) )
break;
if ( cobaye_report_count <= ++v3 )
goto LABEL_10;
}
v5 = v3++;
printf("Open new report type %s in %s\n", *(const char **)(cobaye_report + 8 * v5), &reportpath);
v6 = (const char **)(cobaye_report + 8 * v5);
strcpy((char *)*v6 + 64, &reportpath);
sprintf(v2, "/Report.%s", *v6);
v7 = *(_QWORD *)(cobaye_report + 8 * v5);
v8 = fopen(dest, "w");
v9 = cobaye_report_count <= v3;
*(_QWORD *)(v7 + 584) = v8;
}
while ( !v9 );
}
LABEL_10:
*v2 = 0;
gethostname(dest, 0x200uLL);
cobaye_report_all(0, dest, 0, 0LL, 0);
gettimeofday(&stat_start_tests, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // esi
char *v2; // r13
unsigned int v3; // ebx
int v4; // eax
__int64 v5; // r12
const char **v6; // rbp
__int64 v7; // rbp
FILE *v8; // rax
bool v9; // cc
struct tm *v10; // rax
char *v11; // rax
const char *v12; // rbp
char *v13; // rax
time_t timer; // [rsp+0h] [rbp-250h] BYREF
char s[32]; // [rsp+8h] [rbp-248h] BYREF
char dest[16]; // [rsp+28h] [rbp-228h] BYREF
char v17[496]; // [rsp+38h] [rbp-218h] BYREF

v0 = cobaye_conf[70];
*(_OWORD *)dest = 0LL;
memset(v17, 0, sizeof(v17));
if ( !v0 || reportpath )
return 0LL;
strcpy(dest, *(const char **)&cobaye_conf[94]);
if ( cobaye_conf[118] == 1 )
{
timer = time(0LL);
v10 = localtime(&timer);
sprintf(
s,
"%04d%02d%02d_%02d%02d%02d_",
(unsigned int)(v10->tm_year + 1900),
(unsigned int)(v10->tm_mon + 1),
(unsigned int)v10->tm_mday,
(unsigned int)v10->tm_hour,
(unsigned int)v10->tm_min,
(unsigned int)v10->tm_sec);
v11 = strrchr(dest, 47);
if ( v11 )
strcpy(v11 + 1, s);
else
strcpy(dest, s);
v12 = *(const char **)&cobaye_conf[94];
v13 = strrchr(v12, 47);
if ( v13 )
v12 = v13 + 1;
strcat(dest, v12);
}
v2 = &dest[stpcpy(&reportpath, dest) - &reportpath];
mkdir(&reportpath, 0x1FFu);
if ( cobaye_report_count )
{
v3 = 0;
do
{
while ( 1 )
{
v4 = cobaye_conf[70];
if ( _bittest(&v4, v3) )
break;
if ( cobaye_report_count <= ++v3 )
goto LABEL_10;
}
v5 = v3++;
printf("Open new report type %s in %s\n", *(const char **)(cobaye_report + 8 * v5), &reportpath);
v6 = (const char **)(cobaye_report + 8 * v5);
strcpy((char *)*v6 + 64, &reportpath);
sprintf(v2, "/Report.%s", *v6);
v7 = *(_QWORD *)(cobaye_report + 8 * v5);
v8 = fopen(dest, "w");
v9 = cobaye_report_count <= v3;
*(_QWORD *)(v7 + 584) = v8;
}
while ( !v9 );
}
LABEL_10:
*v2 = 0;
gethostname(dest, 0x200uLL);
func2(0, dest, 0, 0LL, 0);
gettimeofday(&stat_start_tests, 0LL);
return 0LL;
}

original_code:
int cobaye_report_open(void)
{
 unsigned int index;
 time_t now;
 char buffer[512] = "";
 char *hostname = buffer;
 char *filepath = buffer;
 char *filename = buffer;
 if (!cobaye_conf[item_report].value)
  return 0;
 if(reportpath[0])
  return 0;
 strcpy(filepath, cobaye_conf[item_filename].string);
 if (cobaye_conf[item_date].value == 1) {
  char date[18];
  time_t now = time(((void *)0));
  struct tm *now_time = localtime(&now);
  sprintf(date, "%04d%02d%02d_%02d%02d%02d_",
    now_time->tm_year + 1900, now_time->tm_mon + 1,
    now_time->tm_mday, now_time->tm_hour, now_time->tm_min,
    now_time->tm_sec);
  char *filename = strrchr(filepath, '/');
  if (filename) {
   strcpy(filename + 1, date);
  } else {
   strcpy(filepath, date);
  }
  filename = strrchr(cobaye_conf[item_filename].string, '/');
  if (filename) {
   filename++;
  } else {
   filename = cobaye_conf[item_filename].string;
  }
  strcat(filepath, filename);
 }
 strcpy(reportpath, filepath);
 filename += strlen(reportpath);
 mkdir(reportpath, 0777);
 for (index = 0; index < cobaye_report_count; index++) {
  if (cobaye_conf[item_report].value & (1 << index)) {
   printf("Open new report type %s in %s\n", cobaye_report[index]->ext, reportpath);
   strcpy(cobaye_report[index]->path, reportpath);
   sprintf(filename, "/Report.%s", cobaye_report[index]->ext);
   cobaye_report[index]->stream = fopen(filepath, "w");
  }
 }
 strcpy(filename, "");
 gethostname(hostname, 512);
 cobaye_report_all(0, hostname, 0, ((void *)0), 0);
 gettimeofday(&stat_start_tests, ((void *)0));
 return 0;
}

canonical_original_code:
int cobaye_report_open()
{
 unsigned int index;
 long now;
 char buffer[512] = "";
 char *hostname = buffer;
 char *filepath = buffer;
 char *filename = buffer;
 if (!cobaye_conf[item_report].value)
  return 0;
 if(reportpath[0])
  return 0;
 strcpy(filepath, cobaye_conf[item_filename].string);
 if (cobaye_conf[item_date].value == 1) {
  char date[18];
  long now = time(((void *)0));
  struct tm *now_time = localtime(&now);
  sprintf(date, "%04d%02d%02d_%02d%02d%02d_",
    now_time->tm_year + 1900, now_time->tm_mon + 1,
    now_time->tm_mday, now_time->tm_hour, now_time->tm_min,
    now_time->tm_sec);
  char *filename = strrchr(filepath, '/');
  if (filename) {
   strcpy(filename + 1, date);
  } else {
   strcpy(filepath, date);
  }
  filename = strrchr(cobaye_conf[item_filename].string, '/');
  if (filename) {
   filename++;
  } else {
   filename = cobaye_conf[item_filename].string;
  }
  strcat(filepath, filename);
 }
 strcpy(reportpath, filepath);
 filename += strlen(reportpath);
 mkdir(reportpath, 0777);
 for (index = 0; index < cobaye_report_count; index++) {
  if (cobaye_conf[item_report].value & (1 << index)) {
   printf("Open new report type %s in %s\n", cobaye_report[index]->ext, reportpath);
   strcpy(cobaye_report[index]->path, reportpath);
   sprintf(filename, "/Report.%s", cobaye_report[index]->ext);
   cobaye_report[index]->stream = fopen(filepath, "w");
  }
 }
 strcpy(filename, "");
 gethostname(hostname, 512);
 cobaye_report_all(0, hostname, 0, ((void *)0), 0);
 gettimeofday(&stat_start_tests, ((void *)0));
 return 0;
}

variable_types:
index
unsigned int

variable_types: now long

variable_types:
buffer
char [512]

variable_types:
hostname
char *

variable_types:
filepath
char *

variable_types:
filename
char *

variable_types:
date
char [18]

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


decompiled_code:
__int64 cobaye_report_close()
{
unsigned int v1; // ebx
int v2; // eax
__int64 v3; // rbp
bool v4; // cc
__int64 v5; // rax

if ( !cobaye_conf[70] )
return 0LL;
gettimeofday(&stat_stop_tests, 0LL);
cobaye_report_all(9, 0LL, 0, 0LL, 0);
if ( cobaye_report_count )
{
v1 = 0;
do
{
while ( 1 )
{
v2 = cobaye_conf[70];
if ( _bittest(&v2, v1) )
break;
if ( cobaye_report_count <= ++v1 )
goto LABEL_8;
}
v3 = v1++;
fclose(*(FILE **)(*(_QWORD *)(cobaye_report + 8 * v3) + 584LL));
v4 = cobaye_report_count <= v1;
v5 = *(_QWORD *)(cobaye_report + 8 * v3);
*(_QWORD *)(v5 + 584) = 0LL;
*(_BYTE *)(v5 + 64) = 0;
}
while ( !v4 );
}
LABEL_8:
reportpath = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v1; // ebx
int v2; // eax
__int64 v3; // rbp
bool v4; // cc
__int64 v5; // rax

if ( !cobaye_conf[70] )
return 0LL;
gettimeofday(&stat_stop_tests, 0LL);
func2(9, 0LL, 0, 0LL, 0);
if ( cobaye_report_count )
{
v1 = 0;
do
{
while ( 1 )
{
v2 = cobaye_conf[70];
if ( _bittest(&v2, v1) )
break;
if ( cobaye_report_count <= ++v1 )
goto LABEL_8;
}
v3 = v1++;
fclose(*(FILE **)(*(_QWORD *)(cobaye_report + 8 * v3) + 584LL));
v4 = cobaye_report_count <= v1;
v5 = *(_QWORD *)(cobaye_report + 8 * v3);
*(_QWORD *)(v5 + 584) = 0LL;
*(_BYTE *)(v5 + 64) = 0;
}
while ( !v4 );
}
LABEL_8:
reportpath = 0;
return 0LL;
}

original_code:
int cobaye_report_close(void)
{
 unsigned int index;
 if (!cobaye_conf[item_report].value)
  return 0;
 gettimeofday(&stat_stop_tests, ((void *)0));
 cobaye_report_all(9, ((void *)0), 0, ((void *)0), 0);
 for (index = 0; index < cobaye_report_count; index++) {
  if (cobaye_conf[item_report].value & (1 << index)) {
   fclose(cobaye_report[index]->stream);
   cobaye_report[index]->stream = ((void *)0);
   strcpy(cobaye_report[index]->path, "");
  }
 }
 strcpy(reportpath, "");
 return 0;
}

canonical_original_code:
int cobaye_report_close()
{
 unsigned int index;
 if (!cobaye_conf[item_report].value)
  return 0;
 gettimeofday(&stat_stop_tests, ((void *)0));
 cobaye_report_all(9, ((void *)0), 0, ((void *)0), 0);
 for (index = 0; index < cobaye_report_count; index++) {
  if (cobaye_conf[item_report].value & (1 << index)) {
   fclose(cobaye_report[index]->stream);
   cobaye_report[index]->stream = ((void *)0);
   strcpy(cobaye_report[index]->path, "");
  }
 }
 strcpy(reportpath, "");
 return 0;
}


repo: blunderer/libotirc

decompiled_code:
__int64 __fastcall sbot_exec_message(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
const char *v4; // r15
__int64 v8; // rsi
_BYTE *i; // rax
unsigned __int64 v10; // rdx
const char *v11; // r13
char v13[1080]; // [rsp+0h] [rbp-438h] BYREF

v4 = (const char *)(a3 + 512);
v8 = 0x100002601LL;
for ( i = (_BYTE *)(a3 + (int)strlen((const char *)(a3 + 512)) + 512); ; --i )
{
v10 = (unsigned __int8)*i;
if ( (unsigned __int8)v10 > 0x20u || !_bittest64(&v8, v10) )
break;
*i = 0;
}
v11 = (const char *)(a3 + 256);
sprintf(v13, "%s/plugins-enabled/msg.sbot %s %s \"%s\"", basepath, (const char *)(a2 + 1), v11, v4);
exec_action(a1, v13, a2, (__int64)v11, a4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
const char *v4; // r15
__int64 v8; // rsi
_BYTE *i; // rax
unsigned __int64 v10; // rdx
const char *v11; // r13
char v13[1080]; // [rsp+0h] [rbp-438h] BYREF

v4 = (const char *)(a3 + 512);
v8 = 0x100002601LL;
for ( i = (_BYTE *)(a3 + (int)strlen((const char *)(a3 + 512)) + 512); ; --i )
{
v10 = (unsigned __int8)*i;
if ( (unsigned __int8)v10 > 0x20u || !_bittest64(&v8, v10) )
break;
*i = 0;
}
v11 = (const char *)(a3 + 256);
sprintf(v13, "%s/plugins-enabled/msg.sbot %s %s \"%s\"", basepath, (const char *)(a2 + 1), v11, v4);
func0(a1, v13, a2, (__int64)v11, a4);
return 0LL;
}

original_code:
int sbot_exec_message(irc_bot_t* bot, irc_chan_t *chan, irc_msg_t *msg, void * data)
{
 int i;
 char libcommand[1024];
 for(i = strlen(msg->mesg); (msg->mesg[i] == '\0') || (msg->mesg[i] == ' ') || (msg->mesg[i] == '\n' || (msg->mesg[i] == '\r')) || (msg->mesg[i] == '\t'); i--) msg->mesg[i] = '\0';
 sprintf(libcommand, "%s/plugins-enabled/msg.sbot %s %s \"%s\"", basepath, chan->name+1, msg->from, msg->mesg);
 exec_action(bot, libcommand, chan, msg->from, data);
 return 0;
}

canonical_original_code:
int sbot_exec_message(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data)
{
 int i;
 char libcommand[1024];
 for(i = strlen(msg->mesg); (msg->mesg[i] == '\0') || (msg->mesg[i] == ' ') || (msg->mesg[i] == '\n' || (msg->mesg[i] == '\r')) || (msg->mesg[i] == '\t'); i--) msg->mesg[i] = '\0';
 sprintf(libcommand, "%s/plugins-enabled/msg.sbot %s %s \"%s\"", basepath, chan->name+1, msg->from, msg->mesg);
 exec_action(bot, libcommand, chan, msg->from, data);
 return 0;
}

variable_types: struct _irc_bot_t { int soc; int port; char name[256]; char server[512]; struct _irc_chan_t { char name[256]; struct _irc_chan_t *next; } *chans; int (*on_msg)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t { char dest[256]; char from[256]; char mesg[2048]; } *msg, void *data); int (*on_cmd)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); int (*on_prv)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); int (*on_tom)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); void *on_msg_data; void *on_cmd_data; void *on_prv_data; void *on_tom_data; } *

variable_types:
chan
struct _irc_chan_t *

variable_types:
msg
struct _irc_msg_t *

variable_types:
data
void *

variable_types: i int

variable_types:
libcommand
char [1024]

user_defined_types: struct _irc_bot_t { int soc; int port; char name[256]; char server[512]; struct _irc_chan_t *chans; int (*on_msg)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); int (*on_cmd)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); int (*on_prv)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); int (*on_tom)(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data); void *on_msg_data; void *on_cmd_data; void *on_prv_data; void *on_tom_data; }


decompiled_code:
__int64 __fastcall sbot_exec_command(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
size_t v7; // r14
char *v8; // r10
char v9; // al
char *v10; // rdx
int v11; // esi
__int64 v12; // rcx
__int64 v13; // r9
const char *v14; // r9
char *src; // [rsp+8h] [rbp-4C0h]
char dest; // [rsp+10h] [rbp-4B8h] BYREF
char v18; // [rsp+11h] [rbp-4B7h] BYREF
char s[1080]; // [rsp+90h] [rbp-438h] BYREF

src = (char *)(a3 + 512);
v7 = strlen((const char *)(a1 + 8));
if ( !strncmp(src, (const char *)(a1 + 8), v7) )
v8 = strcpy(&dest, &src[v7 + 2]);
else
v8 = strcpy(&dest, src);
v9 = dest;
v10 = &v18;
v11 = 0;
v12 = 0LL;
v13 = 1LL;
if ( (dest & 0xDF) != 0 )
{
do
{
v12 = v11++;
if ( (unsigned __int8)(v9 - 9) <= 1u || v9 == 13 )
{
v13 = v11;
goto LABEL_9;
}
v9 = *v10++;
}
while ( (v9 & 0xDF) != 0 );
*(&dest + v11) = 0;
v14 = &v8[(int)v12 + 2];
if ( a4 != 2 )
goto LABEL_10;
}
else
{
LABEL_9:
*(&dest + v12) = 0;
v14 = &v8[v13];
if ( a4 != 2 )
goto LABEL_10;
}
if ( !strcmp(v8, "quit") )
{
irc_send_command(a1, 3LL, v14);
running = 0;
return 0LL;
}
LABEL_10:
if ( a2 )
sprintf(
s,
"%s/plugins-enabled/%s.sbot %s %s %s",
basepath,
v8,
(const char *)(a2 + 1),
(const char *)(a3 + 256),
v14);
else
sprintf(s, "%s/plugins-enabled/%s.sbot PRIVATE %s %s", basepath, v8, (const char *)(a3 + 256), v14);
exec_action(a1, s, a2, a3 + 256, a4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
size_t v7; // r14
char *v8; // r10
char v9; // al
char *v10; // rdx
int v11; // esi
__int64 v12; // rcx
__int64 v13; // r9
const char *v14; // r9
char *src; // [rsp+8h] [rbp-4C0h]
char dest; // [rsp+10h] [rbp-4B8h] BYREF
char v18; // [rsp+11h] [rbp-4B7h] BYREF
char s[1080]; // [rsp+90h] [rbp-438h] BYREF

src = (char *)(a3 + 512);
v7 = strlen((const char *)(a1 + 8));
if ( !strncmp(src, (const char *)(a1 + 8), v7) )
v8 = strcpy(&dest, &src[v7 + 2]);
else
v8 = strcpy(&dest, src);
v9 = dest;
v10 = &v18;
v11 = 0;
v12 = 0LL;
v13 = 1LL;
if ( (dest & 0xDF) != 0 )
{
do
{
v12 = v11++;
if ( (unsigned __int8)(v9 - 9) <= 1u || v9 == 13 )
{
v13 = v11;
goto LABEL_9;
}
v9 = *v10++;
}
while ( (v9 & 0xDF) != 0 );
*(&dest + v11) = 0;
v14 = &v8[(int)v12 + 2];
if ( a4 != 2 )
goto LABEL_10;
}
else
{
LABEL_9:
*(&dest + v12) = 0;
v14 = &v8[v13];
if ( a4 != 2 )
goto LABEL_10;
}
if ( !strcmp(v8, "quit") )
{
irc_send_command(a1, 3LL, v14);
running = 0;
return 0LL;
}
LABEL_10:
if ( a2 )
sprintf(
s,
"%s/plugins-enabled/%s.sbot %s %s %s",
basepath,
v8,
(const char *)(a2 + 1),
(const char *)(a3 + 256),
v14);
else
sprintf(s, "%s/plugins-enabled/%s.sbot PRIVATE %s %s", basepath, v8, (const char *)(a3 + 256), v14);
func0(a1, s, a2, a3 + 256, a4);
return 0LL;
}

original_code:
int sbot_exec_command(irc_bot_t* bot, irc_chan_t *chan, irc_msg_t *msg, void *data)
{
 int i = 0;
 char * arguments;
 char str[1024];
 char command[128];
 char libcommand[1024];
 if(strncmp(msg->mesg, bot->name, strlen(bot->name)) == 0) {
  strcpy(command, msg->mesg+strlen(bot->name)+2);
 } else {
  strcpy(command, msg->mesg);
 }
 for(i = 0; (command[i] != '\0') && (command[i] != ' ') && (command[i] != '\n' && (command[i] != '\r')) && (command[i] != '\t'); i++);
 command[i] = '\0';
 arguments = command+i+1;
 if(data == (void*)2) {
  if(strcmp(command, "quit") == 0) {
   irc_send_command(bot, quit_cmd, arguments);
   running = 0;
   return 0;
  }
 }
 if(chan) {
  sprintf(libcommand, "%s/plugins-enabled/%s.sbot %s %s %s", basepath, command, chan->name+1, msg->from, arguments);
 } else {
  sprintf(libcommand, "%s/plugins-enabled/%s.sbot PRIVATE %s %s", basepath, command, msg->from, arguments);
 }
 exec_action(bot, libcommand, chan, msg->from, data);
 return 0;
}

canonical_original_code:
int sbot_exec_command(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data)
{
 int i = 0;
 char *arguments;
 char str[1024];
 char command[128];
 char libcommand[1024];
 if(strncmp(msg->mesg, bot->name, strlen(bot->name)) == 0) {
  strcpy(command, msg->mesg+strlen(bot->name)+2);
 } else {
  strcpy(command, msg->mesg);
 }
 for(i = 0; (command[i] != '\0') && (command[i] != ' ') && (command[i] != '\n' && (command[i] != '\r')) && (command[i] != '\t'); i++);
 command[i] = '\0';
 arguments = command+i+1;
 if(data == (void *)2) {
  if(strcmp(command, "quit") == 0) {
   irc_send_command(bot, quit_cmd, arguments);
   running = 0;
   return 0;
  }
 }
 if(chan) {
  sprintf(libcommand, "%s/plugins-enabled/%s.sbot %s %s %s", basepath, command, chan->name+1, msg->from, arguments);
 } else {
  sprintf(libcommand, "%s/plugins-enabled/%s.sbot PRIVATE %s %s", basepath, command, msg->from, arguments);
 }
 exec_action(bot, libcommand, chan, msg->from, data);
 return 0;
}

variable_types:
arguments
char *

variable_types:
str
char [1024]

variable_types:
command
char [128]

user_defined_types: struct _irc_chan_t { char name[256]; struct _irc_chan_t *next; }

user_defined_types: struct _irc_msg_t { char dest[256]; char from[256]; char mesg[2048]; }


decompiled_code:
__int64 __fastcall sbot_command(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
sbot_exec_command(a1, a2, a3, a4);
sbot_exec_message(a1, a2, a3, a4);
return 0LL;
}

original_code:
int sbot_command(irc_bot_t* bot, irc_chan_t *chan, irc_msg_t *msg, void *data)
{
 sbot_exec_command(bot, chan, msg, data);
 sbot_exec_message(bot, chan, msg, data);
 return 0;
}

canonical_original_code:
int sbot_command(struct _irc_bot_t *bot, struct _irc_chan_t *chan, struct _irc_msg_t *msg, void *data)
{
 sbot_exec_command(bot, chan, msg, data);
 sbot_exec_message(bot, chan, msg, data);
 return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
char v4; // dl
const char *v5; // rax
const char *v6; // r12
const char *v7; // r15
size_t v8; // r12
size_t v9; // rax
__int64 v10; // rbp
unsigned int v11; // eax
const char **v12; // r14
const char *v13; // rdx
int v14; // ebx
__int64 i; // r14
size_t v17; // r15
size_t v18; // rax
char v19[128]; // [rsp+0h] [rbp-4B8h] BYREF
char s[1080]; // [rsp+80h] [rbp-438h] BYREF

v3 = *argv;
v4 = **argv;
if ( v4 )
{
v5 = v3 + 1;
do
{
if ( v4 == 47 )
v3 = v5;
v4 = *v5++;
}
while ( v4 );
}
memset(basepath, 0, sizeof(basepath));
v6 = *argv;
if ( **argv == 47 )
{
v17 = strlen(*argv);
v18 = strlen(v3);
strncat(basepath, v6, v17 - v18);
}
else
{
getcwd(basepath, 0x200uLL);
*(_WORD *)&basepath[strlen(basepath)] = 47;
v7 = *argv;
v8 = strlen(*argv);
v9 = strlen(v3);
strncat(basepath, v7, v8 - v9);
}
sprintf(s, "%s/plugins-enabled/cron.sbot", basepath);
v10 = irc_create_bot(v3);
if ( argc <= 2 )
{
puts("syntax: ./[bot name] [server] [port] [chan1] [chan2] ...");
exit(0);
}
v11 = atoi(argv[2]);
if ( (int)irc_connect_bot(v10, argv[1], v11) < 0 )
{
puts("cannot connect bot to server");
exit(1);
}
usleep(0xF4240u);
if ( argc != 3 )
{
v12 = argv + 3;
do
{
v13 = *v12++;
sprintf(v19, "#%s", v13);
irc_join(v10, v19);
}
while ( &argv[(unsigned int)(argc - 4) + 4] != v12 );
}
irc_call_on_chan_message(v10, sbot_exec_message, 1LL);
irc_call_on_chan_message_to_me(v10, sbot_command, 1LL);
irc_call_on_chan_message_private(v10, sbot_exec_command, 2LL);
if ( running )
{
v14 = 0;
do
{
++v14;
if ( __ROR4__(989560465 * v14 - 989560465, 4) <= 0x68DB8u )
{
for ( i = *(_QWORD *)(v10 + 776); i; i = *(_QWORD *)(i + 256) )
exec_action(v10, s, i, 0LL, 1LL);
}
usleep(0x3E8u);
}
while ( running );
}
irc_disconnect_bot(v10);
irc_destroy_bot(v10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
char v4; // dl
const char *v5; // rax
const char *v6; // r12
const char *v7; // r15
size_t v8; // r12
size_t v9; // rax
__int64 v10; // rbp
unsigned int v11; // eax
const char **v12; // r14
const char *v13; // rdx
int v14; // ebx
__int64 i; // r14
size_t v17; // r15
size_t v18; // rax
char v19[128]; // [rsp+0h] [rbp-4B8h] BYREF
char s[1080]; // [rsp+80h] [rbp-438h] BYREF

v3 = *argv;
v4 = **argv;
if ( v4 )
{
v5 = v3 + 1;
do
{
if ( v4 == 47 )
v3 = v5;
v4 = *v5++;
}
while ( v4 );
}
memset(basepath, 0, sizeof(basepath));
v6 = *argv;
if ( **argv == 47 )
{
v17 = strlen(*argv);
v18 = strlen(v3);
strncat(basepath, v6, v17 - v18);
}
else
{
getcwd(basepath, 0x200uLL);
*(_WORD *)&basepath[strlen(basepath)] = 47;
v7 = *argv;
v8 = strlen(*argv);
v9 = strlen(v3);
strncat(basepath, v7, v8 - v9);
}
sprintf(s, "%s/plugins-enabled/cron.sbot", basepath);
v10 = irc_create_bot(v3);
if ( argc <= 2 )
{
puts("syntax: ./[bot name] [server] [port] [chan1] [chan2] ...");
exit(0);
}
v11 = atoi(argv[2]);
if ( (int)irc_connect_bot(v10, argv[1], v11) < 0 )
{
puts("cannot connect bot to server");
exit(1);
}
usleep(0xF4240u);
if ( argc != 3 )
{
v12 = argv + 3;
do
{
v13 = *v12++;
sprintf(v19, "#%s", v13);
irc_join(v10, v19);
}
while ( &argv[(unsigned int)(argc - 4) + 4] != v12 );
}
irc_call_on_chan_message(v10, sbot_exec_message, 1LL);
irc_call_on_chan_message_to_me(v10, sbot_command, 1LL);
irc_call_on_chan_message_private(v10, sbot_exec_command, 2LL);
if ( running )
{
v14 = 0;
do
{
++v14;
if ( __ROR4__(989560465 * v14 - 989560465, 4) <= 0x68DB8u )
{
for ( i = *(_QWORD *)(v10 + 776); i; i = *(_QWORD *)(i + 256) )
func0(v10, s, i, 0LL, 1LL);
}
usleep(0x3E8u);
}
while ( running );
}
irc_disconnect_bot(v10);
irc_destroy_bot(v10);
return 0;
}

original_code:
int main(int argc, const char ** argv)
{
 int i;
 int cnt = 0;
 char libcommand[1024];
 char * name = (char *)argv[0];
 for(i = 0; argv[0][i] != '\0'; i++) {
  if(argv[0][i] == '/') {
   name = (char*)argv[0] + i + 1;
  }
 }
 memset(basepath, 0, 512);
 if(argv[0][0] == '/') {
  strncat(basepath, argv[0], strlen(argv[0])-strlen(name));
 } else {
  getcwd(basepath, 512);
  strcat(basepath, "/");
  strncat(basepath, argv[0], strlen(argv[0])-strlen(name));
 }
 sprintf(libcommand, "%s/plugins-enabled/cron.sbot", basepath);
 irc_bot_t* sbot = irc_create_bot(name);
 if(argc > 2) {
  if(irc_connect_bot(sbot, (char*)argv[1], atoi((char*)argv[2])) < 0) {
   printf("cannot connect bot to server\n");
   exit(1);
  }
 } else {
  printf("syntax: ./[bot name] [server] [port] [chan1] [chan2] ...\n");
  exit(0);
 }
 usleep(1000000);
 for(i = 3; i < argc; i++) {
  char channame[128];
  sprintf(channame, "#%s", argv[i]);
  irc_join(sbot, channame);
 }
 irc_call_on_chan_message(sbot, sbot_exec_message, (void*)1);
 irc_call_on_chan_message_to_me(sbot, sbot_command, (void*)1);
 irc_call_on_chan_message_private(sbot, sbot_exec_command, (void*)2);
 while(running) {
  cnt++;
  if((cnt % 10000) == 1) {
   irc_chan_t * chan = sbot->chans;
   while(chan) {
    exec_action(sbot, libcommand, chan, ((void *)0), (void*)1);
    chan = chan->next;
   }
  }
  usleep(1000);
 }
 irc_disconnect_bot(sbot);
 irc_destroy_bot(sbot);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 int i;
 int cnt = 0;
 char libcommand[1024];
 char *name = (char *)argv[0];
 for(i = 0; argv[0][i] != '\0'; i++) {
  if(argv[0][i] == '/') {
   name = (char *)argv[0] + i + 1;
  }
 }
 memset(basepath, 0, 512);
 if(argv[0][0] == '/') {
  strncat(basepath, argv[0], strlen(argv[0])-strlen(name));
 } else {
  getcwd(basepath, 512);
  strcat(basepath, "/");
  strncat(basepath, argv[0], strlen(argv[0])-strlen(name));
 }
 sprintf(libcommand, "%s/plugins-enabled/cron.sbot", basepath);
 struct _irc_bot_t *sbot = irc_create_bot(name);
 if(argc > 2) {
  if(irc_connect_bot(sbot, (char *)argv[1], atoi((char *)argv[2])) < 0) {
   printf("cannot connect bot to server\n");
   exit(1);
  }
 } else {
  printf("syntax: ./[bot name] [server] [port] [chan1] [chan2] ...\n");
  exit(0);
 }
 usleep(1000000);
 for(i = 3; i < argc; i++) {
  char channame[128];
  sprintf(channame, "#%s", argv[i]);
  irc_join(sbot, channame);
 }
 irc_call_on_chan_message(sbot, sbot_exec_message, (void *)1);
 irc_call_on_chan_message_to_me(sbot, sbot_command, (void *)1);
 irc_call_on_chan_message_private(sbot, sbot_exec_command, (void *)2);
 while(running) {
  cnt++;
  if((cnt % 10000) == 1) {
   struct _irc_chan_t *chan = sbot->chans;
   while(chan) {
    exec_action(sbot, libcommand, chan, ((void *)0), (void *)1);
    chan = chan->next;
   }
  }
  usleep(1000);
 }
 irc_disconnect_bot(sbot);
 irc_destroy_bot(sbot);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: cnt int

variable_types:
name
char *

variable_types:
channame
char [128]

variable_types: struct _irc_chan_t { char name[256]; struct _irc_chan_t *next; } *


usage

decompiled_code:
void __noreturn usage()
{
puts("proxy [-l <listening port>] -f <forward ip>:<forward port>");
exit(0);
}

canonical_decompiled_code:
void __noreturn func9()
{
puts("proxy [-l <listening port>] -f <forward ip>:<forward port>");
exit(0);
}

original_code:
void usage(void) {
 printf("proxy [-l <listening port>] -f <forward ip>:<forward port>\n");
 exit(0);
}

canonical_original_code:
void usage() {
 printf("proxy [-l <listening port>] -f <forward ip>:<forward port>\n");
 exit(0);
}


original_code:
int parse_args(int argc, char ** argv)
{
 if(argc == 5) {
  if(strcmp(argv[1], "-l") == 0) {
   proxy_port = atoi(argv[2]);
  } else if(strcmp(argv[3], "-l") == 0) {
   proxy_port = atoi(argv[4]);
  } else {
   return 0;
  }
  if(strcmp(argv[1], "-f") == 0) {
   strcpy(forward_addr, argv[2]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  } else if(strcmp(argv[3], "-f") == 0) {
   strcpy(forward_addr, argv[4]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  } else {
   return 0;
  }
 }else if(argc == 3) {
  if(strcmp(argv[1], "-f") == 0) {
   strcpy(forward_addr, argv[2]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  }
 } else {
  return 0;
 }
 return 1;
}

canonical_original_code:
int parse_args(int argc, char **argv)
{
 if(argc == 5) {
  if(strcmp(argv[1], "-l") == 0) {
   proxy_port = atoi(argv[2]);
  } else if(strcmp(argv[3], "-l") == 0) {
   proxy_port = atoi(argv[4]);
  } else {
   return 0;
  }
  if(strcmp(argv[1], "-f") == 0) {
   strcpy(forward_addr, argv[2]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  } else if(strcmp(argv[3], "-f") == 0) {
   strcpy(forward_addr, argv[4]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  } else {
   return 0;
  }
 }else if(argc == 3) {
  if(strcmp(argv[1], "-f") == 0) {
   strcpy(forward_addr, argv[2]);
   if(strstr(forward_addr, ":")) {
    char *ptr = strstr(forward_addr, ":");
    ptr[0] = 0;
    forward_port = atoi(ptr+1);
   }
  }
 } else {
  return 0;
 }
 return 1;
}

variable_types:
ptr
char *


repo: bmarcot/kemerald

decompiled_code:
void __noreturn job1()
{
unsigned int v0; // ebp
unsigned int v1; // eax
unsigned int v2; // eax

while ( 1 )
{
v0 = rdesp();
v1 = rdebp();
printf("\t(job1)\tesp:%x ebp:%x\n", v0, v1);
v2 = sc_sysenter(1LL);
printf("\tsc_sleep() exit code = %d\n", v2);
}
}

canonical_decompiled_code:
void __noreturn func0()
{
unsigned int v0; // ebp
unsigned int v1; // eax
unsigned int v2; // eax

while ( 1 )
{
v0 = rdesp();
v1 = rdebp();
printf("\t(job1)\tesp:%x ebp:%x\n", v0, v1);
v2 = sc_sysenter(1LL);
printf("\tsc_sleep() exit code = %d\n", v2);
}
}

original_code:
void job1(void)
{
  uint32_t esp, ebp;
  int32_t status = 0xbebe;
  while (1)
    {
      esp = rdesp();
      ebp = rdebp();
      printf("\t(job1)\tesp:%x ebp:%x\n", esp, ebp);
      status = sc_sysenter(1);
      printf("\tsc_sleep() exit code = %d\n", status);
    }
}

canonical_original_code:
void job1()
{
  unsigned int esp; unsigned int ebp;
  int status = 0xbebe;
  while (1)
    {
      esp = rdesp();
      ebp = rdebp();
      printf("\t(job1)\tesp:%x ebp:%x\n", esp, ebp);
      status = sc_sysenter(1);
      printf("\tsc_sleep() exit code = %d\n", status);
    }
}

variable_types:
esp
unsigned int

variable_types:
ebp
unsigned int

variable_types: status int


decompiled_code:
void __noreturn job2()
{
unsigned int v0; // ebp
unsigned int v1; // eax
unsigned int v2; // eax

while ( 1 )
{
v0 = rdesp();
v1 = rdebp();
printf("\t(job2)\tesp:%x ebp:%x\n", v0, v1);
v2 = sc_sysenter(1LL);
printf("\tsc_sleep() exit code = %d\n", v2);
}
}

canonical_decompiled_code:
void __noreturn func1()
{
unsigned int v0; // ebp
unsigned int v1; // eax
unsigned int v2; // eax

while ( 1 )
{
v0 = rdesp();
v1 = rdebp();
printf("\t(job2)\tesp:%x ebp:%x\n", v0, v1);
v2 = sc_sysenter(1LL);
printf("\tsc_sleep() exit code = %d\n", v2);
}
}

original_code:
void job2(void)
{
  uint32_t esp, ebp;
  int32_t status = 0xbebe;
  while (1)
    {
      esp = rdesp();
      ebp = rdebp();
      printf("\t(job2)\tesp:%x ebp:%x\n", esp, ebp);
      status = sc_sysenter(1);
      printf("\tsc_sleep() exit code = %d\n", status);
    }
}

canonical_original_code:
void job2()
{
  unsigned int esp; unsigned int ebp;
  int status = 0xbebe;
  while (1)
    {
      esp = rdesp();
      ebp = rdebp();
      printf("\t(job2)\tesp:%x ebp:%x\n", esp, ebp);
      status = sc_sysenter(1);
      printf("\tsc_sleep() exit code = %d\n", status);
    }
}


decompiled_code:
__int64 sched_init()
{
unsigned int v0; // eax
unsigned int v1; // ebp
unsigned int v2; // eax
unsigned int v3; // er12
__int64 result; // rax

v0 = thread_create((unsigned int)job1, 1LL, "job1");
if ( v0 == -1
|| (v1 = v0,
printf("Created thread job1 with id %d.\n", v0),
v2 = thread_create((unsigned int)job2, 1LL, "job2"),
v3 = v2,
v2 == -1)
|| (printf("Created thread job2 with id %d.\n", v2), (unsigned int)thread_start(v1)) )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)-((unsigned int)thread_start(v3) != 0);
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax
unsigned int v1; // ebp
unsigned int v2; // eax
unsigned int v3; // er12
__int64 result; // rax

v0 = thread_create((unsigned int)job1, 1LL, "job1");
if ( v0 == -1
|| (v1 = v0,
printf("Created thread job1 with id %d.\n", v0),
v2 = thread_create((unsigned int)job2, 1LL, "job2"),
v3 = v2,
v2 == -1)
|| (printf("Created thread job2 with id %d.\n", v2), (unsigned int)thread_start(v1)) )
{
result = 0xFFFFFFFFLL;
}
else
{
result = (unsigned int)-((unsigned int)thread_start(v3) != 0);
}
return result;
}

original_code:
int32_t sched_init(void)
{
  int32_t id[2];
  uint32_t i;
  if ((id[0] = thread_create((uint32_t)(job1), USER, "job1")) == -1)
    {
      return -1;
    }
  printf("Created thread job1 with id %d.\n", id[0]);
  if ((id[1] = thread_create((uint32_t)(job2), USER, "job2")) == -1)
    {
      return -1;
    }
  printf("Created thread job2 with id %d.\n", id[1]);
  for (i = 0; i < 2; i++)
    {
      if (thread_start(id[i]) != 0)
 {
   return -1;
 }
    }
  return 0;
}

canonical_original_code:
int sched_init()
{
  int id[2];
  unsigned int i;
  if ((id[0] = thread_create((unsigned int)(job1), USER, "job1")) == -1)
    {
      return -1;
    }
  printf("Created thread job1 with id %d.\n", id[0]);
  if ((id[1] = thread_create((unsigned int)(job2), USER, "job2")) == -1)
    {
      return -1;
    }
  printf("Created thread job2 with id %d.\n", id[1]);
  for (i = 0; i < 2; i++)
    {
      if (thread_start(id[i]) != 0)
 {
   return -1;
 }
    }
  return 0;
}

variable_types:
id
int [2]

variable_types:
i
unsigned int


decompiled_code:
__int64 sched_elect()
{
_QWORD *v0; // rax
__int64 v1; // rdi
_QWORD *i; // rbx
_QWORD *v3; // rax
_QWORD *v5; // rax

v0 = (_QWORD *)thread_get_ready();
v1 = *v0;
for ( i = v0; *(_DWORD *)(*i + 16LL) != 2; v1 = *i )
i = (_QWORD *)i[2];
v3 = (_QWORD *)i[2];
if ( v3 )
{
thread_switch(v1, *v3);
}
else
{
v5 = (_QWORD *)thread_get_ready();
thread_switch(*i, *v5);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
_QWORD *v0; // rax
__int64 v1; // rdi
_QWORD *i; // rbx
_QWORD *v3; // rax
_QWORD *v5; // rax

v0 = (_QWORD *)thread_get_ready();
v1 = *v0;
for ( i = v0; *(_DWORD *)(*i + 16LL) != 2; v1 = *i )
i = (_QWORD *)i[2];
v3 = (_QWORD *)i[2];
if ( v3 )
{
thread_switch(v1, *v3);
}
else
{
v5 = (_QWORD *)thread_get_ready();
thread_switch(*i, *v5);
}
return 0LL;
}

original_code:
int32_t sched_elect(void)
{
  list_s *threads = thread_get_ready();
  while (((thread_ctx_s *)(threads->e))->state != RUNNING)
    {
      threads = threads->next;
    }
  if (threads->next)
    {
      thread_switch((thread_ctx_s *)(threads->e),
      (thread_ctx_s *)(threads->next->e));
    }
  else
    {
      thread_switch((thread_ctx_s *)(threads->e),
      (thread_ctx_s *)(thread_get_ready()->e));
    }
  return 0;
}

canonical_original_code:
int sched_elect()
{
  struct _list_s *threads = thread_get_ready();
  while (((struct _thread_ctx_s *)(threads->e))->state != RUNNING)
    {
      threads = threads->next;
    }
  if (threads->next)
    {
      thread_switch((struct _thread_ctx_s *)(threads->e),
      (struct _thread_ctx_s *)(threads->next->e));
    }
  else
    {
      thread_switch((struct _thread_ctx_s *)(threads->e),
      (struct _thread_ctx_s *)(thread_get_ready()->e));
    }
  return 0;
}

variable_types: struct _list_s { void *e; struct _list_s *prev; struct _list_s *next; } *

user_defined_types: struct _thread_ctx_s { unsigned int id; unsigned int priority; unsigned char name[8]; enum _thread_state_e state; enum _thread_class_e class; unsigned int entry; unsigned int eip; unsigned int esp0; unsigned int esp3; struct _s_thread_tss tss; }

user_defined_types: enum _thread_state_e { DORMANT=0, READY=1, RUNNING=2, WAITING=3 }

user_defined_types: enum _thread_class_e { SYSTEM=0, USER=1 }

user_defined_types: struct _s_thread_tss { unsigned int esp0; unsigned int iomap; }


decompiled_code:
// attributes: thunk
__int64 sched_launch()
{
return thread_launch();
}

canonical_decompiled_code:
__int64 func4()
{
return thread_launch();
}

original_code:
void sched_launch(void)
{
  thread_launch();
}

canonical_original_code:
void sched_launch()
{
  thread_launch();
}


decompiled_code:
__int64 serial_init()
{
int *v0; // rax

v0 = &serial;
do
*v0++ = 0;
while ( v0 != (int *)&unk_280 );
return 0LL;
}

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

v0 = &serial;
do
*v0++ = 0;
while ( v0 != (int *)&unk_280 );
return 0LL;
}

original_code:
int32_t serial_init(void)
{
  uint32_t i;
  for (i = 0; i < 128; i++)
    {
      _serial[i] = false;
    }
  return 0;
}

canonical_original_code:
int serial_init()
{
  unsigned int i;
  for (i = 0; i < 128; i++)
    {
      _serial[i] = false;
    }
  return 0;
}


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

for ( result = 0LL; result != 128; ++result )
{
if ( !serial[result] )
{
serial[(unsigned int)result] = 1;
return result;
}
}
return 0xFFFFFFFFLL;
}

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

for ( result = 0LL; result != 128; ++result )
{
if ( !serial[result] )
{
serial[(unsigned int)result] = 1;
return result;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int32_t serial_generate(void)
{
  uint32_t i;
  for (i = 0; i < 128; i++)
    {
      if (_serial[i] == false)
 {
   _serial[i] = true;
   return i;
 }
    }
  return -1;
}

canonical_original_code:
int serial_generate()
{
  unsigned int i;
  for (i = 0; i < 128; i++)
    {
      if (_serial[i] == false)
 {
   _serial[i] = true;
   return i;
 }
    }
  return -1;
}


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

if ( a1 > 0x7F )
return 0xFFFFFFFFLL;
result = 0LL;
serial[a1] = 0;
return result;
}

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

if ( a1 > 0x7F )
return 0xFFFFFFFFLL;
result = 0LL;
serial[a1] = 0;
return result;
}

original_code:
int32_t serial_free(uint32_t i)
{
  if (i >= 128)
    return -1;
  _serial[i] = false;
  return 0;
}

canonical_original_code:
int serial_free(unsigned int i)
{
  if (i >= 128)
    return -1;
  _serial[i] = false;
  return 0;
}


decompiled_code:
__int64 isr_stage2_init()
{
int *v0; // rax

v0 = &_isr_stage2;
do
*v0++ = (unsigned int)isr_stage2_unhandled;
while ( v0 != &_isr_stage2 + 256 );
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
int *v0; // rax

v0 = &_isr_stage2;
do
*v0++ = (unsigned int)isr_stage2_unhandled;
while ( v0 != &_isr_stage2 + 256 );
return 0LL;
}

original_code:
int32_t isr_stage2_init(void)
{
  uint32_t i;
  for (i = 0; i < 256; i++)
    {
      __isr_stage2[i] = (uint32_t) _isr_stage2_unhandled;
    }
  return 0;
}

canonical_original_code:
int isr_stage2_init()
{
  unsigned int i;
  for (i = 0; i < 256; i++)
    {
      __isr_stage2[i] = (unsigned int) _isr_stage2_unhandled;
    }
  return 0;
}


decompiled_code:
void __fastcall isr_stage2_bind(int a1, unsigned __int8 a2)
{
_isr_stage2[a2] = a1;
}

canonical_decompiled_code:
void __fastcall func2(int a1, unsigned __int8 a2)
{
_isr_stage2[a2] = a1;
}

original_code:
void isr_stage2_bind(uint32_t f,
     uint8_t index)
{
  __isr_stage2[index] = f;
}

canonical_original_code:
void isr_stage2_bind(unsigned int f, unsigned char index)
{
  __isr_stage2[index] = f;
}

variable_types:
f
unsigned int

variable_types:
index
unsigned char


decompiled_code:
int isr_stage2_pending()
{
apic_local_ack();
return printf("Int acked.\n");
}

canonical_decompiled_code:
int func3()
{
apic_local_ack();
return printf("Int acked.\n");
}

original_code:
void isr_stage2_pending(void)
{
  uint8_t pending_n;
  apic_local_ack();
  printf("Int acked.\n");
}

canonical_original_code:
void isr_stage2_pending()
{
  unsigned char pending_n;
  apic_local_ack();
  printf("Int acked.\n");
}


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = 0LL;
if ( *a1 )
{
do
{
v2 = (unsigned int)(result + 1);
result = v2;
}
while ( a1[v2] );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = 0LL;
if ( *a1 )
{
do
{
v2 = (unsigned int)(result + 1);
result = v2;
}
while ( a1[v2] );
}
return result;
}

original_code:
int32_t strlen(const char *str)
{
uint32_t i = 0;
  while (str[i])
    i++;
  return i;
}

canonical_original_code:
int strlen(char *str)
{
unsigned int i = 0;
  while (str[i])
    i++;
  return i;
}

variable_types:
str
char *


strcat

decompiled_code:
__int64 __fastcall strcat(_BYTE *a1, char *a2)
{
unsigned int v2; // eax
char v4; // si
unsigned int v5; // er9
__int64 v6; // rdx
__int64 v7; // rcx

v2 = 0;
if ( *a1 )
{
do
++v2;
while ( a1[v2] );
}
v4 = *a2;
if ( v4 )
{
v5 = 1 - v2;
do
{
v6 = v5 + v2;
v7 = v2++;
a1[v7] = v4;
v4 = a2[v6];
}
while ( v4 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, char *a2)
{
unsigned int v2; // eax
char v4; // si
unsigned int v5; // er9
__int64 v6; // rdx
__int64 v7; // rcx

v2 = 0;
if ( *a1 )
{
do
++v2;
while ( a1[v2] );
}
v4 = *a2;
if ( v4 )
{
v5 = 1 - v2;
do
{
v6 = v5 + v2;
v7 = v2++;
a1[v7] = v4;
v4 = a2[v6];
}
while ( v4 );
}
return 0LL;
}

original_code:
int32_t strcat(char *dest,
         const char *src)
{
uint32_t i = 0;
uint32_t j = 0;
  while (dest[i])
    i++;
  while (src[j])
    dest[i++] = src[j++];
  return 0;
}

canonical_original_code:
int strcat(char *dest, char *src)
{
unsigned int i = 0;
unsigned int j = 0;
  while (dest[i])
    i++;
  while (src[j])
    dest[i++] = src[j++];
  return 0;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
j
unsigned int


strcpy

original_code:
int32_t strcpy(char *dest,
         const char *src)
{
uint32_t i = 0;
  if ((dest == 0) || (src == 0))
    return -2;
  while (src[i])
    {
      dest[i] = src[i];
      i++;
    }
  return 0;
}

canonical_original_code:
int strcpy(char *dest, char *src)
{
unsigned int i = 0;
  if ((dest == 0) || (src == 0))
    return -2;
  while (src[i])
    {
      dest[i] = src[i];
      i++;
    }
  return 0;
}


itoa

original_code:
int32_t itoa(char *buffer,
       uint32_t n,
       uint8_t base_length)
{
  char c;
  char *base = "0123456789abcdef";
  uint32_t i = 0;
  uint32_t position = 0;
  while (n / base_length)
    {
      buffer[position++] = base[n % base_length];
      n /= base_length;
    }
  buffer[position] = base[n];
  while ((i <= (position / 2)) && (position > 0))
    {
      c = buffer[i];
      buffer[i] = buffer[position - i];
      buffer[position - i] = c;
      i++;
    }
  return ++position;
}

canonical_original_code:
int itoa(char *buffer, unsigned int n, unsigned char base_length)
{
  char c;
  char *base = "0123456789abcdef";
  unsigned int i = 0;
  unsigned int position = 0;
  while (n / base_length)
    {
      buffer[position++] = base[n % base_length];
      n /= base_length;
    }
  buffer[position] = base[n];
  while ((i <= (position / 2)) && (position > 0))
    {
      c = buffer[i];
      buffer[i] = buffer[position - i];
      buffer[position - i] = c;
      i++;
    }
  return ++position;
}

variable_types:
buffer
char *

variable_types:
n
unsigned int

variable_types: c char

variable_types:
base
char *

variable_types:
position
unsigned int


decompiled_code:
__int64 __fastcall sc_thread_stop(unsigned int a1)
{
printf("Stop thread: %d\n", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
printf("Stop thread: %d\n", a1);
return 0LL;
}

original_code:
int32_t sc_thread_stop(uint32_t thread_id)
{
  printf("Stop thread: %d\n", thread_id);
  return 0;
}

canonical_original_code:
int sc_thread_stop(unsigned int thread_id)
{
  printf("Stop thread: %d\n", thread_id);
  return 0;
}


decompiled_code:
__int64 sc_sleep()
{
sti();
hlt();
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
sti();
hlt();
return 0LL;
}

original_code:
int32_t sc_sleep(uint32_t delay)
{
  sti();
  hlt();
  return 0;
}

canonical_original_code:
int sc_sleep(unsigned int delay)
{
  sti();
  hlt();
  return 0;
}

variable_types:
delay
unsigned int


decompiled_code:
__int64 sc_init()
{
wrmsr(372LL, 8LL);
wrmsr(374LL, (unsigned int)&sc_hook);
printf("System-call hook set at @: %x\n", &sc_hook);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
wrmsr(372LL, 8LL);
wrmsr(374LL, (unsigned int)&sc_hook);
printf("System-call hook set at @: %x\n", &sc_hook);
return 0LL;
}

original_code:
int32_t sc_init(void)
{
  wrmsr(0x174, (1 << 3));
  wrmsr(0x176, (uint64_t) sc_hook & 0xffffffff);
  printf("System-call hook set at @: %x\n", (uint32_t)(sc_hook));
  return 0;
}

canonical_original_code:
int sc_init()
{
  wrmsr(0x174, (1 << 3));
  wrmsr(0x176, (unsigned long long) sc_hook & 0xffffffff);
  printf("System-call hook set at @: %x\n", (unsigned int)(sc_hook));
  return 0;
}


decompiled_code:
__int64 __fastcall sc_enter(int a1)
{
return ((__int64 (__fastcall *)(__int64))service_call[a1])(1LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
return ((__int64 (__fastcall *)(__int64))service_call[a1])(1LL);
}

original_code:
int32_t sc_enter(uint32_t service_id,
                         uint32_t esp3,
                         uint32_t eip3)
{
  return _service_call[service_id](1);
}

canonical_original_code:
int sc_enter(unsigned int service_id, unsigned int esp3, unsigned int eip3)
{
  return _service_call[service_id](1);
}


decompiled_code:
__int64 apic_local_init()
{
int v0; // eax

v0 = rdmsr(27LL);
apic_local_base = v0 & 0xFFFFF000;
wrmsr(27LL, v0 | 0x800u);
printf("Before reserve: %x\n", (unsigned int)apic_local_base);
if ( (unsigned int)mmu_virt_page_reserve((unsigned int)apic_local_base, 538LL) )
{
printf("Can't reserve a page for the Local-APIC memory-mapped registers.\n");
__halt();
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax

v0 = rdmsr(27LL);
apic_local_base = v0 & 0xFFFFF000;
wrmsr(27LL, v0 | 0x800u);
printf("Before reserve: %x\n", (unsigned int)apic_local_base);
if ( (unsigned int)mmu_virt_page_reserve((unsigned int)apic_local_base, 538LL) )
{
printf("Can't reserve a page for the Local-APIC memory-mapped registers.\n");
__halt();
}
return 0LL;
}

original_code:
int32_t apic_local_init(void)
{
  uint32_t msr;
  msr = (uint32_t) rdmsr(0x1b);
  _apic_local_base = msr & ~0xfff;
  msr |= (1 << 11);
  wrmsr(0x1b, ((uint64_t) msr) & 0xffffffff);
  printf("Before reserve: %x\n", _apic_local_base);
  if (mmu_virt_page_reserve((vaddr_t) _apic_local_base,
       (1 << 1)
       | (1 << 3)
       | (1 << 4)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the Local-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  return 0;
}

canonical_original_code:
int apic_local_init()
{
  unsigned int msr;
  msr = (unsigned int) rdmsr(0x1b);
  _apic_local_base = msr & ~0xfff;
  msr |= (1 << 11);
  wrmsr(0x1b, ((unsigned long long) msr) & 0xffffffff);
  printf("Before reserve: %x\n", _apic_local_base);
  if (mmu_virt_page_reserve((void *) _apic_local_base,
       (1 << 1)
       | (1 << 3)
       | (1 << 4)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the Local-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  return 0;
}

variable_types:
msr
unsigned int


decompiled_code:
__int64 apic_local_ack()
{
return apic_local_wrreg(176);
}

canonical_decompiled_code:
__int64 func4()
{
return func1(176);
}

original_code:
void apic_local_ack(void)
{
  _apic_local_wrreg(0xb0, 0);
}

canonical_original_code:
void apic_local_ack()
{
  _apic_local_wrreg(0xb0, 0);
}


decompiled_code:
__int64 apic_local_timer_config()
{
apic_local_wrreg(992);
apic_local_wrreg(896);
apic_local_wrreg(912);
apic_local_rdreg(800);
apic_local_wrreg(800);
return 0LL;
}

original_code:
int32_t apic_local_timer_config(uint32_t initial_count)
{
  uint32_t lvt_timer;
  _apic_local_wrreg(0x3e0, 3);
  _apic_local_wrreg(0x380, initial_count);
  _apic_local_wrreg(0x390, initial_count);
  lvt_timer = _apic_local_rdreg(0x320);
  lvt_timer |= (1 << 17);
  lvt_timer = (lvt_timer & ~0xff) | 32;
  lvt_timer &= ~(1 << 16);
  _apic_local_wrreg(0x320, lvt_timer);
  return 0;
}

canonical_original_code:
int apic_local_timer_config(unsigned int initial_count)
{
  unsigned int lvt_timer;
  _apic_local_wrreg(0x3e0, 3);
  _apic_local_wrreg(0x380, initial_count);
  _apic_local_wrreg(0x390, initial_count);
  lvt_timer = _apic_local_rdreg(0x320);
  lvt_timer |= (1 << 17);
  lvt_timer = (lvt_timer & ~0xff) | 32;
  lvt_timer &= ~(1 << 16);
  _apic_local_wrreg(0x320, lvt_timer);
  return 0;
}


decompiled_code:
__int64 __fastcall apic_local_set_task_priority(unsigned __int8 a1)
{
if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
apic_local_wrreg(128);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int8 a1)
{
if ( a1 > 0xFu )
return 0xFFFFFFFFLL;
func1(128);
return 0LL;
}

original_code:
int32_t apic_local_set_task_priority(uint8_t task_priority)
{
  if (task_priority > 15)
    {
      return -1;
    }
  _apic_local_wrreg(0x80,
      (uint32_t) task_priority << 4);
  return 0;
}

canonical_original_code:
int apic_local_set_task_priority(unsigned char task_priority)
{
  if (task_priority > 15)
    {
      return -1;
    }
  _apic_local_wrreg(0x80,
      (unsigned int) task_priority << 4);
  return 0;
}


decompiled_code:
char apic_local_get_task_priority()
{
return (unsigned __int8)apic_local_rdreg(128) >> 4;
}

canonical_decompiled_code:
char func7()
{
return (unsigned __int8)func0(128) >> 4;
}

original_code:
uint8_t apic_local_get_task_priority(void)
{
  return (uint8_t)((_apic_local_rdreg(0x80) >> 4) & 0xf);
}

canonical_original_code:
unsigned char apic_local_get_task_priority()
{
  return (unsigned char)((_apic_local_rdreg(0x80) >> 4) & 0xf);
}

return_type: unsigned char


decompiled_code:
char apic_local_get_proc_priority()
{
return (unsigned __int8)apic_local_rdreg(160) >> 4;
}

canonical_decompiled_code:
char func8()
{
return (unsigned __int8)func0(160) >> 4;
}

original_code:
uint8_t apic_local_get_proc_priority(void)
{
  return (uint8_t)((_apic_local_rdreg(0xa0) >> 4) & 0xf);
}

canonical_original_code:
unsigned char apic_local_get_proc_priority()
{
  return (unsigned char)((_apic_local_rdreg(0xa0) >> 4) & 0xf);
}


decompiled_code:
__int64 apic_local_is_pending()
{
int i; // ebx
unsigned int v1; // eax
int v2; // eax

for ( i = 8; ; --i )
{
v1 = apic_local_rdreg(4 * (unsigned int)(unsigned __int8)i + 512);
v2 = bsr(v1);
if ( (char)v2 > 0 )
break;
}
return (unsigned int)(32 * i + v2);
}

canonical_decompiled_code:
__int64 func9()
{
int i; // ebx
unsigned int v1; // eax
int v2; // eax

for ( i = 8; ; --i )
{
v1 = func0(4 * (unsigned int)(unsigned __int8)i + 512);
v2 = bsr(v1);
if ( (char)v2 > 0 )
break;
}
return (unsigned int)(32 * i + v2);
}

original_code:
uint8_t apic_local_is_pending(void)
{
  uint8_t i = 8;
  uint32_t irr;
  int8_t msb;
  while (i >= 0)
    {
      irr = _apic_local_rdreg(0x200 + i * sizeof (uint32_t));
      msb = bsr(irr);
      if (msb > 0)
 {
   return i * 32 + msb;
 }
      i--;
    }
  return 0;
}

canonical_original_code:
unsigned char apic_local_is_pending()
{
  unsigned char i = 8;
  unsigned int irr;
  char msb;
  while (i >= 0)
    {
      irr = _apic_local_rdreg(0x200 + i * sizeof (unsigned int));
      msb = bsr(irr);
      if (msb > 0)
 {
   return i * 32 + msb;
 }
      i--;
    }
  return 0;
}

variable_types:
i
unsigned char

variable_types:
irr
unsigned int

variable_types: msb char


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

memzero(&g_console_video, 4000LL);
result = 0LL;
g_console_line = 0;
g_console_column = 0;
return result;
}

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

memzero(&g_console_video, 4000LL);
result = 0LL;
g_console_line = 0;
g_console_column = 0;
return result;
}

original_code:
int32_t console_clear(void)
{
  memzero((void *)g_console_video, 2 * 80 * 25);
  g_console_column = g_console_line = 0;
  return 0;
}

canonical_original_code:
int console_clear()
{
  memzero((void *)g_console_video, 2 * 80 * 25);
  g_console_column = g_console_line = 0;
  return 0;
}


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

g_console_line = 0;
g_console_column = 0;
console_clear();
v0 = &byte_221;
do
{
*v0 = 15;
v0 += 2;
}
while ( v0 != (char *)&unk_11C1 );
return 0LL;
}

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

g_console_line = 0;
g_console_column = 0;
func0();
v0 = &byte_221;
do
{
*v0 = 15;
v0 += 2;
}
while ( v0 != (char *)&unk_11C1 );
return 0LL;
}

original_code:
int32_t console_init(void)
{
uint32_t i = 0;
  g_console_column = g_console_line = 0;
  console_lastline = 0;
  console_clear();
  for (i = 1; i < 2 * 80 * 25; i+=2)
    g_console_video[i] = 0x7 | (0x0 << 4) | 0x8;
  return 0;
}

canonical_original_code:
int console_init()
{
unsigned int i = 0;
  g_console_column = g_console_line = 0;
  console_lastline = 0;
  console_clear();
  for (i = 1; i < 2 * 80 * 25; i+=2)
    g_console_video[i] = 0x7 | (0x0 << 4) | 0x8;
  return 0;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 console_scrolldown()
{
memcpy(&g_console_video, &unk_2C0, 0xE60uLL);
memzero(&unk_1080, 160LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
memcpy(&g_console_video, &unk_2C0, 0xE60uLL);
memzero(&unk_1080, 160LL);
return 0LL;
}

original_code:
int32_t console_scrolldown(void)
{
  memcpy((void *)g_console_video,
  (void *)(&g_console_video[80 * 2]),
  80 * (25 - 2) * 2);
  memzero((void *)(&g_console_video[80
        * (25 - 2) * 2]),
   80 * 2);
  return 0;
}

canonical_original_code:
int console_scrolldown()
{
  memcpy((void *)g_console_video,
  (void *)(&g_console_video[80 * 2]),
  80 * (25 - 2) * 2);
  memzero((void *)(&g_console_video[80
        * (25 - 2) * 2]),
   80 * 2);
  return 0;
}


decompiled_code:
__int64 __fastcall console_printf(char *s, char a2)
{
unsigned int v3; // er8
int v4; // er9
char v5; // r10
unsigned int v6; // eax
unsigned int v7; // edx
unsigned int v8; // esi
__int64 v9; // r11
char v10; // cl

if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)g_console_line > 0x17 )
{
console_scrolldown();
--g_console_line;
}
v3 = strlen(s);
if ( v3 )
{
v4 = g_console_line;
v5 = 0;
v6 = 0;
v7 = g_console_column;
while ( 1 )
{
v10 = *s;
if ( *s == 9 )
{
++v6;
++s;
v7 = (v7 & 0xFFFFFFF8) + 8;
if ( v3 <= v6 )
goto LABEL_13;
}
else
{
if ( v10 == 10
|| (v8 = v7 + 80 * v4,
++v7,
v9 = 2 * v8,
g_console_video[v9] = v10,
g_console_video[(unsigned int)(v9 + 1)] = a2,
v7 > 0x4F) )
{
++v4;
v5 = 1;
v7 = 0;
}
++v6;
++s;
if ( v3 <= v6 )
{
LABEL_13:
g_console_column = v7;
if ( v5 )
g_console_line = v4;
return 0LL;
}
}
if ( v6 > 0x4F )
goto LABEL_13;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s, char a2)
{
unsigned int v3; // er8
int v4; // er9
char v5; // r10
unsigned int v6; // eax
unsigned int v7; // edx
unsigned int v8; // esi
__int64 v9; // r11
char v10; // cl

if ( !s )
return 0xFFFFFFFFLL;
if ( (unsigned int)g_console_line > 0x17 )
{
func2();
--g_console_line;
}
v3 = strlen(s);
if ( v3 )
{
v4 = g_console_line;
v5 = 0;
v6 = 0;
v7 = g_console_column;
while ( 1 )
{
v10 = *s;
if ( *s == 9 )
{
++v6;
++s;
v7 = (v7 & 0xFFFFFFF8) + 8;
if ( v3 <= v6 )
goto LABEL_13;
}
else
{
if ( v10 == 10
|| (v8 = v7 + 80 * v4,
++v7,
v9 = 2 * v8,
g_console_video[v9] = v10,
g_console_video[(unsigned int)(v9 + 1)] = a2,
v7 > 0x4F) )
{
++v4;
v5 = 1;
v7 = 0;
}
++v6;
++s;
if ( v3 <= v6 )
{
LABEL_13:
g_console_column = v7;
if ( v5 )
g_console_line = v4;
return 0LL;
}
}
if ( v6 > 0x4F )
goto LABEL_13;
}
}
return 0LL;
}

original_code:
int32_t console_printf(char *str,
         char attribs)
{
uint32_t i = 0;
uint32_t length;
  if (str == 0)
    return -1;
  if (g_console_line >= 25 - 1)
    {
      console_scrolldown();
      g_console_line--;
    }
  length = strlen(str);
  if (length)
    {
      while ((i < length) && (i < 80))
 {
   switch (str[i])
     {
     case '\n':
       g_console_column = 0;
       g_console_line++;
       break;
     case '\t':
       g_console_column = (g_console_column / 8)
  * 8 + 8;
       break;
     default:
       g_console_video[((g_console_line * 80 + g_console_column) * 2)] = str[i];
       g_console_video[((g_console_line * 80 + g_console_column) * 2) + 1] = attribs;
       g_console_column++;
       if (g_console_column >= 80)
  {
    g_console_column = 0;
    g_console_line++;
  }
     }
   i++;
 }
    }
  return 0;
}

canonical_original_code:
int console_printf(char *str, char attribs)
{
unsigned int i = 0;
unsigned int length;
  if (str == 0)
    return -1;
  if (g_console_line >= 25 - 1)
    {
      console_scrolldown();
      g_console_line--;
    }
  length = strlen(str);
  if (length)
    {
      while ((i < length) && (i < 80))
 {
   switch (str[i])
     {
     case '\n':
       g_console_column = 0;
       g_console_line++;
       break;
     case '\t':
       g_console_column = (g_console_column / 8)
  * 8 + 8;
       break;
     default:
       g_console_video[((g_console_line * 80 + g_console_column) * 2)] = str[i];
       g_console_video[((g_console_line * 80 + g_console_column) * 2) + 1] = attribs;
       g_console_column++;
       if (g_console_column >= 80)
  {
    g_console_column = 0;
    g_console_line++;
  }
     }
   i++;
 }
    }
  return 0;
}

variable_types: attribs char

variable_types:
length
unsigned int


decompiled_code:
__int64 __fastcall console_refresh(FILE *stream, __int64 off)
{
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

if ( !stream )
return 4294967294LL;
if ( fseek(stream, (unsigned int)off, 0) )
return 0xFFFFFFFFLL;
while ( 1 )
{
memzero(v3, 80LL);
if ( !fgets(v3, 80, stream) )
break;
console_printf(v3, 7);
}
return ftell(stream);
}

canonical_decompiled_code:
__int64 __fastcall func4(FILE *stream, __int64 off)
{
char v3[88]; // [rsp-58h] [rbp-58h] BYREF

if ( !stream )
return 4294967294LL;
if ( fseek(stream, (unsigned int)off, 0) )
return 0xFFFFFFFFLL;
while ( 1 )
{
memzero(v3, 80LL);
if ( !fgets(v3, 80, stream) )
break;
func3(v3, 7);
}
return ftell(stream);
}

original_code:
int32_t console_refresh(FILE *stream,
   uint32_t lastline)
{
  char line[80];
  if (stream == 0)
    return -2;
  if (fseek(stream, lastline, 0) != 0)
    return -1;
  memzero(line, 80);
  while (0 != fgets(line, 80, stream))
    {
      console_printf(line, (0x0 << 4) | 0x7);
      memzero(line, 80);
    }
  return ftell(stream);
}

canonical_original_code:
int console_refresh(struct _s_file *stream, unsigned int lastline)
{
  char line[80];
  if (stream == 0)
    return -2;
  if (fseek(stream, lastline, 0) != 0)
    return -1;
  memzero(line, 80);
  while (0 != fgets(line, 80, stream))
    {
      console_printf(line, (0x0 << 4) | 0x7);
      memzero(line, 80);
    }
  return ftell(stream);
}

variable_types: struct _s_file { struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; } buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; } *

variable_types:
lastline
unsigned int

variable_types:
line
char [80]

user_defined_types: struct _s_file { struct _s_buffer buffer; unsigned int fileoffset; unsigned long long filesize; char filename[16]; int fileid; unsigned short filemode; }


decompiled_code:
void __fastcall write_char(char a1)
{
g_console_video[0] = a1;
byte_221 = -32;
}

canonical_decompiled_code:
void __fastcall func5(char a1)
{
g_console_video[0] = a1;
byte_221 = -32;
}

original_code:
void write_char(uint8_t c)
{
  g_console_video[0] = c;
  g_console_video[1] = (0x6 << 4) | 0x0 | (0x8 << 4);
}

canonical_original_code:
void write_char(unsigned char c)
{
  g_console_video[0] = c;
  g_console_video[1] = (0x6 << 4) | 0x0 | (0x8 << 4);
}

variable_types:
c
unsigned char


decompiled_code:
__int64 __fastcall pit_set_frequency(__int16 a1)
{
outb(0x40u, (unsigned __int8)a1);
outb(0x40u, HIBYTE(a1));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1)
{
outb(0x40u, (unsigned __int8)a1);
outb(0x40u, HIBYTE(a1));
return 0LL;
}

original_code:
int32_t pit_set_frequency(uint32_t freq)
{
  outb(0x40, (uint8_t)(freq & 0xff));
  outb(0x40, (uint8_t)(freq >> 8));
  return 0;
}

canonical_original_code:
int pit_set_frequency(unsigned int freq)
{
  outb(0x40, (unsigned char)(freq & 0xff));
  outb(0x40, (unsigned char)(freq >> 8));
  return 0;
}

variable_types:
freq
unsigned int


decompiled_code:
__int64 pit_init()
{
outb(0x43u, 0x36u);
pit_set_frequency(11931);
return 0LL;
}

original_code:
int32_t pit_init(void)
{
  outb(0x43, 0x36);
  pit_set_frequency(1193180 / 100);
  return 0;
}

canonical_original_code:
int pit_init()
{
  outb(0x43, 0x36);
  pit_set_frequency(1193180 / 100);
  return 0;
}


decompiled_code:
__int64 apic_io_init()
{
if ( (unsigned int)mmu_virt_page_reserve(4273995776LL, 538LL) )
{
printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
__halt();
}
apic_io_add_redentry_constprop_0();
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
if ( (unsigned int)mmu_virt_page_reserve(4273995776LL, 538LL) )
{
printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
__halt();
}
func1();
return 0LL;
}

original_code:
int32_t apic_io_init(void)
{
  _apic_io_base = 0xfec00000;
  if (mmu_virt_page_reserve((vaddr_t) _apic_io_base,
       (1 << 1)
       | (1 << 4)
       | (1 << 3)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  _apic_io_add_redentry(18,
   33);
  return 0;
}

canonical_original_code:
int apic_io_init()
{
  _apic_io_base = 0xfec00000;
  if (mmu_virt_page_reserve((void *) _apic_io_base,
       (1 << 1)
       | (1 << 4)
       | (1 << 3)
       | (1 << 9)))
    {
      printf("Can't reserve a page for the IO-APIC memory-mapped registers.\n");
      while (1) __asm__ __volatile__ ("hlt");;;
      return -1;
    }
  _apic_io_add_redentry(18,
   33);
  return 0;
}


fstrcat

original_code:
int32_t fstrcat(FILE *stream,
   const char *s)
{
uint32_t i = 0;
  if ((stream == 0) || (s == 0))
    return -2;
  while (i < strlen(s))
    {
      stream->buffer.data[stream->buffer.offset++] = s[i++];
      stream->fileoffset++;
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return 0;
}

canonical_original_code:
int fstrcat(struct _s_file *stream, char *s)
{
unsigned int i = 0;
  if ((stream == 0) || (s == 0))
    return -2;
  while (i < strlen(s))
    {
      stream->buffer.data[stream->buffer.offset++] = s[i++];
      stream->fileoffset++;
      if ((stream->buffer.mode & (1 << 3))
   && (stream->buffer.offset >= stream->buffer.size))
 stream->fileoffset = stream->buffer.offset = 0;
    }
  return 0;
}

variable_types:
s
char *

user_defined_types: struct _s_buffer { unsigned char *data; unsigned int offset; unsigned int size; unsigned int mode; }