decompiled_code:
int __fastcall wrap_line(int a1)
{
size_t v1; // rbx
size_t v2; // rax
int v3; // edx
int result; // eax
char *v5; // rax

v1 = strlen(line);
v2 = strlen(buff);
v3 = ind_indent + v1 + v2;
if ( a1 )
{
if ( v3 > linemax )
{
fputs(line, ind_fp);
fputc(10, ind_fp);
++ind_lc;
fputs(&indent_space, ind_fp);
ind_indent = indent_length;
}
else
{
fputs(line, ind_fp);
}
result = fputs(buff, ind_fp);
}
else if ( v3 <= linemax )
{
v5 = stpcpy(&buff[v2], &delim_n);
result = (unsigned int)memcpy(&line[v1], buff, v5 - (char *)&unk_1CDF);
}
else
{
fputs(line, ind_fp);
fputc(10, ind_fp);
++ind_lc;
sprintf(line, "%s%s%s", &indent_space, buff, &delim_n);
result = indent_length;
ind_indent = indent_length;
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1)
{
size_t v1; // rbx
size_t v2; // rax
int v3; // edx
int result; // eax
char *v5; // rax

v1 = strlen(line);
v2 = strlen(buff);
v3 = ind_indent + v1 + v2;
if ( a1 )
{
if ( v3 > linemax )
{
fputs(line, ind_fp);
fputc(10, ind_fp);
++ind_lc;
fputs(&indent_space, ind_fp);
ind_indent = indent_length;
}
else
{
fputs(line, ind_fp);
}
result = fputs(buff, ind_fp);
}
else if ( v3 <= linemax )
{
v5 = stpcpy(&buff[v2], &delim_n);
result = (unsigned int)memcpy(&line[v1], buff, v5 - (char *)&unk_1CDF);
}
else
{
fputs(line, ind_fp);
fputc(10, ind_fp);
++ind_lc;
sprintf(line, "%s%s%s", &indent_space, buff, &delim_n);
result = indent_length;
ind_indent = indent_length;
}
return result;
}

original_code:
static void
wrap_line(int print)
{
    int len;
    len = strlen(line) + strlen(buff) + ind_indent;
    if (print) {
 if (len > linemax) {
     { fputs(line, ind_fp); fputc('\n', ind_fp); ind_lc++; };
     { fputs(indent_space, ind_fp); };
     ind_indent = indent_length;
 } else
     { fputs(line, ind_fp); };
 { fputs(buff, ind_fp); };
    } else {
 if (len > linemax) {
     { fputs(line, ind_fp); fputc('\n', ind_fp); ind_lc++; };
     sprintf(line, "%s%s%s", indent_space, buff, delim_n);
     ind_indent = indent_length;
 } else {
     strcat(buff, delim_n);
     strcat(line, buff);
 }
    }
}

variable_types: print int

variable_types: len int


repo: nlasso/pancetaYHuevoFrito

decompiled_code:
int __fastcall flush_line(int a1)
{
__int64 v2; // rdi
const char *v3; // r12
int v4; // eax
__int64 v5; // r11
int v6; // er10
const char *v7; // rcx
const char *v8; // r12
char v10[2072]; // [rsp+0h] [rbp-818h] BYREF

v2 = begin;
v3 = (const char *)(begin + 52);
v4 = page_diff(begin, end);
if ( !v4 )
{
encap_range = 0;
strcpy(buff, v3);
v8 = prev_encap;
if ( !*prev_encap )
return wrap_line(a1);
LABEL_16:
strcpy(v10, buff);
sprintf(buff, "%s%s%s%s%s", (const char *)&encap_p, v8, (const char *)&encap_i, v10, (const char *)&encap_s);
return wrap_line(a1);
}
v6 = v4;
if ( encap_range || (int)page_diff(v2, prev) > (suffix_2p == 0) )
{
if ( v6 == 1 )
{
v7 = (const char *)&suffix_2p;
if ( !suffix_2p )
goto LABEL_7;
}
else
{
if ( v6 == 2 )
{
if ( suffix_3p )
{
v7 = (const char *)&suffix_3p;
goto LABEL_14;
}
}
else if ( v6 <= 1 )
{
LABEL_7:
sprintf(buff, "%s%s%s", v3, (const char *)&delim_r, (const char *)(v5 + 52));
LABEL_8:
encap_range = 0;
goto LABEL_9;
}
v7 = (const char *)&suffix_mp;
if ( !suffix_mp )
goto LABEL_7;
}
LABEL_14:
sprintf(buff, "%s%s", v3, v7);
goto LABEL_8;
}
sprintf(buff, "%s%s%s", v3, &delim_n, (const char *)(v5 + 52));
LABEL_9:
v8 = prev_encap;
if ( *prev_encap )
goto LABEL_16;
return wrap_line(a1);
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
__int64 v2; // rdi
const char *v3; // r12
int v4; // eax
__int64 v5; // r11
int v6; // er10
const char *v7; // rcx
const char *v8; // r12
char v10[2072]; // [rsp+0h] [rbp-818h] BYREF

v2 = begin;
v3 = (const char *)(begin + 52);
v4 = func0(begin, end);
if ( !v4 )
{
encap_range = 0;
strcpy(buff, v3);
v8 = prev_encap;
if ( !*prev_encap )
return func4(a1);
LABEL_16:
strcpy(v10, buff);
sprintf(buff, "%s%s%s%s%s", (const char *)&encap_p, v8, (const char *)&encap_i, v10, (const char *)&encap_s);
return func4(a1);
}
v6 = v4;
if ( encap_range || (int)func0(v2, prev) > (suffix_2p == 0) )
{
if ( v6 == 1 )
{
v7 = (const char *)&suffix_2p;
if ( !suffix_2p )
goto LABEL_7;
}
else
{
if ( v6 == 2 )
{
if ( suffix_3p )
{
v7 = (const char *)&suffix_3p;
goto LABEL_14;
}
}
else if ( v6 <= 1 )
{
LABEL_7:
sprintf(buff, "%s%s%s", v3, (const char *)&delim_r, (const char *)(v5 + 52));
LABEL_8:
encap_range = 0;
goto LABEL_9;
}
v7 = (const char *)&suffix_mp;
if ( !suffix_mp )
goto LABEL_7;
}
LABEL_14:
sprintf(buff, "%s%s", v3, v7);
goto LABEL_8;
}
sprintf(buff, "%s%s%s", v3, &delim_n, (const char *)(v5 + 52));
LABEL_9:
v8 = prev_encap;
if ( *prev_encap )
goto LABEL_16;
return func4(a1);
}

original_code:
static void
flush_line(int print)
{
    char tmp[sizeof(buff)];
    if (page_diff(begin, end) != 0)
 if (encap_range || (page_diff(begin, prev) > (*suffix_2p ? 0 : 1)))
 {
  int diff = page_diff(begin, end);
  if ((diff == 1) && *suffix_2p)
      sprintf(buff, "%s%s", begin->lpg, suffix_2p);
  else if ((diff == 2) && *suffix_3p)
      sprintf(buff, "%s%s", begin->lpg, suffix_3p);
  else if ((diff >= 2) && *suffix_mp)
      sprintf(buff, "%s%s", begin->lpg, suffix_mp);
  else
      sprintf(buff, "%s%s%s", begin->lpg, delim_r, end->lpg);
     encap_range = 0;
 }
 else
     sprintf(buff, "%s%s%s", begin->lpg, delim_n, end->lpg);
    else
    {
 encap_range = 0;
 strcpy(buff, begin->lpg);
    }
    if (*prev_encap != '\0')
    {
 strcpy(tmp, buff);
 sprintf(buff, "%s%s%s%s%s",
  encap_p, prev_encap, encap_i, tmp, encap_s);
    }
    wrap_line(print);
}

canonical_original_code:
static void flush_line(int print)
{
    char tmp[-1];
    if (page_diff(begin, end) != 0)
 if (encap_range || (page_diff(begin, prev) > (*suffix_2p ? 0 : 1)))
 {
  int diff = page_diff(begin, end);
  if ((diff == 1) && *suffix_2p)
      sprintf(buff, "%s%s", begin->lpg, suffix_2p);
  else if ((diff == 2) && *suffix_3p)
      sprintf(buff, "%s%s", begin->lpg, suffix_3p);
  else if ((diff >= 2) && *suffix_mp)
      sprintf(buff, "%s%s", begin->lpg, suffix_mp);
  else
      sprintf(buff, "%s%s%s", begin->lpg, delim_r, end->lpg);
     encap_range = 0;
 }
 else
     sprintf(buff, "%s%s%s", begin->lpg, delim_n, end->lpg);
    else
    {
 encap_range = 0;
 strcpy(buff, begin->lpg);
    }
    if (*prev_encap != '\0')
    {
 strcpy(tmp, buff);
 sprintf(buff, "%s%s%s%s%s",
  encap_p, prev_encap, encap_i, tmp, encap_s);
    }
    wrap_line(print);
}

variable_types:
tmp
char [-1]

variable_types: diff int

return_type: void


decompiled_code:
int old_entry()
{
__int64 v0; // rbp
int v1; // ebx
const char *v2; // r12
int result; // eax
__int64 v4; // rax
__int64 v5; // rdx

v0 = curr;
v1 = page_diff(end, curr);
if ( *(_WORD *)(prev + 90) != *(_WORD *)(v0 + 90) )
{
flush_line(0);
if ( v1 )
{
LABEL_14:
v4 = curr;
if ( !in_range )
goto LABEL_27;
v5 = curr;
if ( *(_WORD *)(prev + 90) == *(_WORD *)(curr + 90) )
{
if ( v1 == -1 )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v4 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v4 + 104),
*(unsigned int *)(v4 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Illegal range formation: starting & ending pages cross chap/sec breaks.\n");
LABEL_26:
++ind_ec;
v4 = curr;
}
LABEL_27:
end = v4;
begin = v4;
result = (int)encap;
prev_encap = encap;
return result;
}
LABEL_23:
if ( idx_dot )
{
fputc(10, ilg_fp);
v5 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v5 + 104),
*(unsigned int *)(v5 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Illegal range formation: starting & ending pages are of different types.\n");
goto LABEL_26;
}
LABEL_21:
v4 = curr;
v5 = curr;
if ( *(_WORD *)(prev + 90) == *(_WORD *)(curr + 90) )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v4 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v4 + 104),
*(unsigned int *)(v4 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Conflicting entries: multiple encaps for the same page under same key.\n");
goto LABEL_26;
}
if ( !in_range )
goto LABEL_27;
goto LABEL_23;
}
if ( v1 == -1 )
{
LABEL_13:
flush_line(0);
goto LABEL_14;
}
if ( v1 )
{
if ( merge_page )
{
if ( v1 == 1 )
{
if ( prev_encap )
{
result = strcmp(encap, prev_encap);
if ( !result )
goto LABEL_31;
}
}
}
if ( in_range )
goto LABEL_7;
goto LABEL_13;
}
if ( prev_encap )
{
result = strcmp(encap, prev_encap);
if ( !result )
{
LABEL_31:
end = v0;
if ( !in_range )
return result;
goto LABEL_8;
}
}
if ( !in_range )
{
flush_line(0);
goto LABEL_21;
}
LABEL_7:
end = v0;
LABEL_8:
v2 = *(const char **)(v0 + 96);
result = *(unsigned __int8 *)v2;
if ( !(_BYTE)result
|| (_BYTE)result == idx_rclose
|| (result = strcmp(*(const char **)(v0 + 96), prev_encap)) == 0
|| (sprintf(
buff,
"%s%s%s%s%s",
(const char *)&encap_p,
v2,
(const char *)&encap_i,
(const char *)(v0 + 52),
(const char *)&encap_s),
result = wrap_line(0),
in_range) )
{
encap_range = 1;
}
return result;
}

canonical_decompiled_code:
int func7()
{
__int64 v0; // rbp
int v1; // ebx
const char *v2; // r12
int result; // eax
__int64 v4; // rax
__int64 v5; // rdx

v0 = curr;
v1 = func0(end, curr);
if ( *(_WORD *)(prev + 90) != *(_WORD *)(v0 + 90) )
{
func5(0);
if ( v1 )
{
LABEL_14:
v4 = curr;
if ( !in_range )
goto LABEL_27;
v5 = curr;
if ( *(_WORD *)(prev + 90) == *(_WORD *)(curr + 90) )
{
if ( v1 == -1 )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v4 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v4 + 104),
*(unsigned int *)(v4 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Illegal range formation: starting & ending pages cross chap/sec breaks.\n");
LABEL_26:
++ind_ec;
v4 = curr;
}
LABEL_27:
end = v4;
begin = v4;
result = (int)encap;
prev_encap = encap;
return result;
}
LABEL_23:
if ( idx_dot )
{
fputc(10, ilg_fp);
v5 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v5 + 104),
*(unsigned int *)(v5 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Illegal range formation: starting & ending pages are of different types.\n");
goto LABEL_26;
}
LABEL_21:
v4 = curr;
v5 = curr;
if ( *(_WORD *)(prev + 90) == *(_WORD *)(curr + 90) )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v4 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v4 + 104),
*(unsigned int *)(v4 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Conflicting entries: multiple encaps for the same page under same key.\n");
goto LABEL_26;
}
if ( !in_range )
goto LABEL_27;
goto LABEL_23;
}
if ( v1 == -1 )
{
LABEL_13:
func5(0);
goto LABEL_14;
}
if ( v1 )
{
if ( merge_page )
{
if ( v1 == 1 )
{
if ( prev_encap )
{
result = strcmp(encap, prev_encap);
if ( !result )
goto LABEL_31;
}
}
}
if ( in_range )
goto LABEL_7;
goto LABEL_13;
}
if ( prev_encap )
{
result = strcmp(encap, prev_encap);
if ( !result )
{
LABEL_31:
end = v0;
if ( !in_range )
return result;
goto LABEL_8;
}
}
if ( !in_range )
{
func5(0);
goto LABEL_21;
}
LABEL_7:
end = v0;
LABEL_8:
v2 = *(const char **)(v0 + 96);
result = *(unsigned __int8 *)v2;
if ( !(_BYTE)result
|| (_BYTE)result == idx_rclose
|| (result = strcmp(*(const char **)(v0 + 96), prev_encap)) == 0
|| (sprintf(
buff,
"%s%s%s%s%s",
(const char *)&encap_p,
v2,
(const char *)&encap_i,
(const char *)(v0 + 52),
(const char *)&encap_s),
result = func4(0),
in_range) )
{
encap_range = 1;
}
return result;
}

original_code:
static void
old_entry(void)
{
    int diff;
    diff = page_diff(end, curr);
    if ((prev->type == curr->type) && (diff != -1) &&
 (((diff == 0) && (prev_encap != ((void *)0)) && (strcmp(encap, prev_encap) == 0)) ||
  (merge_page && (diff == 1) &&
   (prev_encap != ((void *)0)) && (strcmp(encap, prev_encap) == 0)) ||
  in_range)) {
 end = curr;
 if (in_range &&
     (*curr->encap != '\0') &&
     (*curr->encap != idx_rclose) &&
     (strcmp(curr->encap, prev_encap) != 0)) {
     sprintf(buff, "%s%s%s%s%s", encap_p, curr->encap,
      encap_i, curr->lpg, encap_s);
     wrap_line(0);
 }
 if (in_range)
     encap_range = 1;
    } else {
 flush_line(0);
 if ((diff == 0) && (prev->type == curr->type)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Conflicting entries: multiple encaps for the same page under same key.\n", ""); ind_ec++; };
 } else if (in_range && (prev->type != curr->type)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Illegal range formation: starting & ending pages are of different types.\n", ""); ind_ec++; };
 } else if (in_range && (diff == -1)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Illegal range formation: starting & ending pages cross chap/sec breaks.\n", ""); ind_ec++; };
 }
 { begin = end = curr; prev_encap = encap; };
    }
}

canonical_original_code:
static void old_entry()
{
    int diff;
    diff = page_diff(end, curr);
    if ((prev->type == curr->type) && (diff != -1) &&
 (((diff == 0) && (prev_encap != ((void *)0)) && (strcmp(encap, prev_encap) == 0)) ||
  (merge_page && (diff == 1) &&
   (prev_encap != ((void *)0)) && (strcmp(encap, prev_encap) == 0)) ||
  in_range)) {
 end = curr;
 if (in_range &&
     (*curr->encap != '\0') &&
     (*curr->encap != idx_rclose) &&
     (strcmp(curr->encap, prev_encap) != 0)) {
     sprintf(buff, "%s%s%s%s%s", encap_p, curr->encap,
      encap_i, curr->lpg, encap_s);
     wrap_line(0);
 }
 if (in_range)
     encap_range = 1;
    } else {
 flush_line(0);
 if ((diff == 0) && (prev->type == curr->type)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Conflicting entries: multiple encaps for the same page under same key.\n", ""); ind_ec++; };
 } else if (in_range && (prev->type != curr->type)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Illegal range formation: starting & ending pages are of different types.\n", ""); ind_ec++; };
 } else if (in_range && (diff == -1)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Illegal range formation: starting & ending pages cross chap/sec breaks.\n", ""); ind_ec++; };
 }
 { begin = end = curr; prev_encap = encap; };
    }
}


decompiled_code:
__int64 __fastcall make_entry(int a1)
{
__int64 v1; // r12
__int64 v2; // rbp
char *v3; // rax
int v4; // eax
__int64 v5; // rbx
__int64 v6; // rbx
const char *v7; // rdi
char v8; // al
__int64 result; // rax
char *v10; // rax

v1 = curr;
v2 = *(_QWORD *)(idx_key + 8LL * a1);
prev = curr;
v3 = *(char **)(v2 + 96);
curr = v2;
if ( *v3 != idx_ropen && *v3 != idx_rclose )
{
encap = v3;
if ( a1 )
goto LABEL_4;
LABEL_19:
level = 0;
v10 = *(char **)v2;
prev_level = 0;
put_header(*v10);
make_item((const char *)&unk_253F);
goto LABEL_9;
}
encap = v3 + 1;
if ( !a1 )
goto LABEL_19;
LABEL_4:
v4 = level;
v5 = 1LL;
level = 0;
prev_level = v4;
do
{
if ( strcmp(*(const char **)(v2 + 8 * v5 - 8), *(const char **)(v1 + 8 * v5 - 8))
|| strcmp(*(const char **)(v2 + 8 * v5 + 16), *(const char **)(v1 + 8 * v5 + 16)) )
{
new_entry();
goto LABEL_9;
}
level = v5++;
}
while ( v5 != 4 );
old_entry();
LABEL_9:
v6 = curr;
v7 = *(const char **)(curr + 96);
v8 = *v7;
if ( *v7 == idx_ropen )
{
if ( in_range )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Extra range opening operator %c.\n", (unsigned int)idx_ropen);
result = 1LL;
++ind_ec;
}
else
{
range_ptr = curr;
result = 1LL;
in_range = 1;
}
}
else
{
if ( v8 != idx_rclose )
{
if ( v8 && strcmp(v7, prev_encap) && in_range )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Inconsistent page encapsulator %s within range.\n", *(const char **)(curr + 96));
++ind_ec;
}
return 1LL;
}
if ( in_range )
{
in_range = 0;
if ( v7[1] && strcmp(prev_encap, v7 + 1) )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(
ilg_fp,
"Range closing operator has an inconsistent encapsulator %s.\n",
(const char *)(*(_QWORD *)(curr + 96) + 1LL));
++ind_ec;
}
return 1LL;
}
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Unmatched range closing operator %c.\n", (unsigned int)idx_rclose);
result = 1LL;
++ind_ec;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 v1; // r12
__int64 v2; // rbp
char *v3; // rax
int v4; // eax
__int64 v5; // rbx
__int64 v6; // rbx
const char *v7; // rdi
char v8; // al
__int64 result; // rax
char *v10; // rax

v1 = curr;
v2 = *(_QWORD *)(idx_key + 8LL * a1);
prev = curr;
v3 = *(char **)(v2 + 96);
curr = v2;
if ( *v3 != idx_ropen && *v3 != idx_rclose )
{
encap = v3;
if ( a1 )
goto LABEL_4;
LABEL_19:
level = 0;
v10 = *(char **)v2;
prev_level = 0;
func2(*v10);
func3((const char *)&unk_253F);
goto LABEL_9;
}
encap = v3 + 1;
if ( !a1 )
goto LABEL_19;
LABEL_4:
v4 = level;
v5 = 1LL;
level = 0;
prev_level = v4;
do
{
if ( strcmp(*(const char **)(v2 + 8 * v5 - 8), *(const char **)(v1 + 8 * v5 - 8))
|| strcmp(*(const char **)(v2 + 8 * v5 + 16), *(const char **)(v1 + 8 * v5 + 16)) )
{
func6();
goto LABEL_9;
}
level = v5++;
}
while ( v5 != 4 );
func7();
LABEL_9:
v6 = curr;
v7 = *(const char **)(curr + 96);
v8 = *v7;
if ( *v7 == idx_ropen )
{
if ( in_range )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Extra range opening operator %c.\n", (unsigned int)idx_ropen);
result = 1LL;
++ind_ec;
}
else
{
range_ptr = curr;
result = 1LL;
in_range = 1;
}
}
else
{
if ( v8 != idx_rclose )
{
if ( v8 && strcmp(v7, prev_encap) && in_range )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Inconsistent page encapsulator %s within range.\n", *(const char **)(curr + 96));
++ind_ec;
}
return 1LL;
}
if ( in_range )
{
in_range = 0;
if ( v7[1] && strcmp(prev_encap, v7 + 1) )
{
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(
ilg_fp,
"Range closing operator has an inconsistent encapsulator %s.\n",
(const char *)(*(_QWORD *)(curr + 96) + 1LL));
++ind_ec;
}
return 1LL;
}
if ( idx_dot )
{
fputc(10, ilg_fp);
v6 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v6 + 104),
*(unsigned int *)(v6 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Unmatched range closing operator %c.\n", (unsigned int)idx_rclose);
result = 1LL;
++ind_ec;
}
return result;
}

original_code:
static int
make_entry(int n)
{
    int let;
    prev = curr;
    curr = idx_key[n];
    if ((*curr->encap == idx_ropen) || (*curr->encap == idx_rclose))
 encap = &(curr->encap[1]);
    else
 encap = curr->encap;
    if (n == 0) {
 prev_level = level = 0;
 let = *curr->sf[0];
 put_header(let);
 make_item("");
    } else {
 prev_level = level;
 for (level = 0; level < 3; level++)
     if ((strcmp(curr->sf[level], prev->sf[level]) != 0) ||
  (strcmp(curr->af[level], prev->af[level]) != 0))
  break;
 if (level < 3)
     new_entry();
 else
     old_entry();
    }
    if (*curr->encap == idx_ropen)
 if (in_range) {
     { if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Extra range opening operator %c.\n", idx_ropen); ind_ec++; };
 } else {
     in_range = 1;
     range_ptr = curr;
 }
    else if (*curr->encap == idx_rclose)
 if (in_range) {
     in_range = 0;
     if ((strcmp(&(curr->encap[1]), "") != 0) &&
  (strcmp(prev_encap, &(curr->encap[1])) != 0)) {
{ if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Range closing operator has an inconsistent encapsulator %s.\n", &(curr->encap[1])); ind_ec++; };
     }
 } else {
     { if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Unmatched range closing operator %c.\n", idx_rclose); ind_ec++; };
 }
    else if ((*curr->encap != '\0') &&
      (strcmp(curr->encap, prev_encap) != 0) && in_range)
 { if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Inconsistent page encapsulator %s within range.\n", curr->encap); ind_ec++; };
    return (1);
}

variable_types: n int

variable_types: let int

return_type: int


decompiled_code:
int gen_ind()
{
__int64 v0; // rbx
int v1; // edx
int v2; // eax
__int64 v3; // rax
__int64 v4; // r8
unsigned int v5; // er12
const char *v6; // r9

if ( verbose )
fprintf(stderr, "Generating output file %s...", ind_fn);
fprintf(ilg_fp, "Generating output file %s...", ind_fn);
fputs(&preamble, ind_fp);
ind_lc += prelen;
if ( init_page )
insert_page();
v0 = 0LL;
for ( idx_dc = 0; idx_gt > (int)v0; ++v0 )
{
if ( *(_WORD *)(*(_QWORD *)(idx_key + 8 * v0) + 90LL) != 9999 && (unsigned int)make_entry(v0) )
{
idx_dot = 1;
v1 = idx_dc;
v2 = ++idx_dc;
if ( !v1 )
{
if ( verbose )
fputc(46, stderr);
fputc(46, ilg_fp);
v2 = idx_dc;
}
if ( v2 == 1000 )
idx_dc = 0;
}
}
if ( in_range )
{
v3 = range_ptr;
curr = range_ptr;
if ( idx_dot )
{
fputc(10, ilg_fp);
v3 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v3 + 104),
*(unsigned int *)(v3 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Unmatched range opening operator %c.\n", (unsigned int)idx_ropen);
++ind_ec;
}
prev = curr;
flush_line(1);
fputs(&delim_t, ind_fp);
fputs(&postamble, ind_fp);
v4 = (unsigned int)ind_ec;
v5 = ind_lc + postlen;
if ( ind_ec == 1 )
{
if ( verbose )
{
fprintf(stderr, "done (%d %s, %d %s).\n", v5, "lines written", (unsigned int)ind_ec, "warning");
v4 = (unsigned int)ind_ec;
}
v6 = "warning";
}
else
{
if ( verbose )
{
fprintf(stderr, "done (%d %s, %d %s).\n", v5, "lines written", (unsigned int)ind_ec, "warnings");
v4 = (unsigned int)ind_ec;
}
v6 = "warnings";
}
return fprintf(ilg_fp, "done (%d %s, %d %s).\n", v5, "lines written", v4, v6);
}

canonical_decompiled_code:
int func9()
{
__int64 v0; // rbx
int v1; // edx
int v2; // eax
__int64 v3; // rax
__int64 v4; // r8
unsigned int v5; // er12
const char *v6; // r9

if ( verbose )
fprintf(stderr, "Generating output file %s...", ind_fn);
fprintf(ilg_fp, "Generating output file %s...", ind_fn);
fputs(&preamble, ind_fp);
ind_lc += prelen;
if ( init_page )
func1();
v0 = 0LL;
for ( idx_dc = 0; idx_gt > (int)v0; ++v0 )
{
if ( *(_WORD *)(*(_QWORD *)(idx_key + 8 * v0) + 90LL) != 9999 && (unsigned int)func8(v0) )
{
idx_dot = 1;
v1 = idx_dc;
v2 = ++idx_dc;
if ( !v1 )
{
if ( verbose )
fputc(46, stderr);
fputc(46, ilg_fp);
v2 = idx_dc;
}
if ( v2 == 1000 )
idx_dc = 0;
}
}
if ( in_range )
{
v3 = range_ptr;
curr = range_ptr;
if ( idx_dot )
{
fputc(10, ilg_fp);
v3 = curr;
idx_dot = 0;
}
fprintf(
ilg_fp,
"## Warning (input = %s, line = %d; output = %s, line = %d):\n -- ",
*(const char **)(v3 + 104),
*(unsigned int *)(v3 + 112),
ind_fn,
(unsigned int)(ind_lc + 1));
fprintf(ilg_fp, "Unmatched range opening operator %c.\n", (unsigned int)idx_ropen);
++ind_ec;
}
prev = curr;
func5(1);
fputs(&delim_t, ind_fp);
fputs(&postamble, ind_fp);
v4 = (unsigned int)ind_ec;
v5 = ind_lc + postlen;
if ( ind_ec == 1 )
{
if ( verbose )
{
fprintf(stderr, "done (%d %s, %d %s).\n", v5, "lines written", (unsigned int)ind_ec, "warning");
v4 = (unsigned int)ind_ec;
}
v6 = "warning";
}
else
{
if ( verbose )
{
fprintf(stderr, "done (%d %s, %d %s).\n", v5, "lines written", (unsigned int)ind_ec, "warnings");
v4 = (unsigned int)ind_ec;
}
v6 = "warnings";
}
return fprintf(ilg_fp, "done (%d %s, %d %s).\n", v5, "lines written", v4, v6);
}

original_code:
void
gen_ind(void)
{
    int n;
    int tmp_lc;
    { if (verbose) fprintf(stderr, "Generating output file %s...", ind_fn); fprintf(ilg_fp, "Generating output file %s...", ind_fn); };
    { fputs(preamble, ind_fp); };
    ind_lc += prelen;
    if (init_page)
 insert_page();
    idx_dc = 0;
    for (n = 0; n < idx_gt; n++) {
 if (idx_key[n]->type != 9999)
     if (make_entry(n)) {
  { idx_dot = 1; if (idx_dc++ == 0) { if (verbose) fprintf(stderr, "."); fprintf(ilg_fp, "."); } if (idx_dc == 1000) idx_dc = 0; };
     }
    }
    tmp_lc = ind_lc;
    if (in_range) {
 curr = range_ptr;
 { if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Unmatched range opening operator %c.\n", idx_ropen); ind_ec++; };
    }
    prev = curr;
    flush_line(1);
    { fputs(delim_t, ind_fp); };
    { fputs(postamble, ind_fp); };
    tmp_lc = ind_lc + postlen;
    if (ind_ec == 1) {
 { if (verbose) fprintf(stderr, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warning"); fprintf(ilg_fp, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warning"); };
    } else {
 { if (verbose) fprintf(stderr, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warnings"); fprintf(ilg_fp, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warnings"); };
    }
}

canonical_original_code:
void gen_ind()
{
    int n;
    int tmp_lc;
    { if (verbose) fprintf(stderr, "Generating output file %s...", ind_fn); fprintf(ilg_fp, "Generating output file %s...", ind_fn); };
    { fputs(preamble, ind_fp); };
    ind_lc += prelen;
    if (init_page)
 insert_page();
    idx_dc = 0;
    for (n = 0; n < idx_gt; n++) {
 if (idx_key[n]->type != 9999)
     if (make_entry(n)) {
  { idx_dot = 1; if (idx_dc++ == 0) { if (verbose) fprintf(stderr, "."); fprintf(ilg_fp, "."); } if (idx_dc == 1000) idx_dc = 0; };
     }
    }
    tmp_lc = ind_lc;
    if (in_range) {
 curr = range_ptr;
 { if (idx_dot) { fprintf(ilg_fp, "\n"); idx_dot = 0; } fprintf(ilg_fp, "## Warning (input = %s, line = %d; output = %s, line = %d):\n   -- ", curr->fn, curr->lc, ind_fn, ind_lc+1); fprintf(ilg_fp, "Unmatched range opening operator %c.\n", idx_ropen); ind_ec++; };
    }
    prev = curr;
    flush_line(1);
    { fputs(delim_t, ind_fp); };
    { fputs(postamble, ind_fp); };
    tmp_lc = ind_lc + postlen;
    if (ind_ec == 1) {
 { if (verbose) fprintf(stderr, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warning"); fprintf(ilg_fp, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warning"); };
    } else {
 { if (verbose) fprintf(stderr, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warnings"); fprintf(ilg_fp, "done (%d %s, %d %s).\n", (tmp_lc), "lines written", ind_ec, "warnings"); };
    }
}


original_code:
void umbralizar_c (
 unsigned char *src,
 unsigned char *dst,
 int h,
 int w,
 int row_size,
 unsigned char min,
 unsigned char max,
 unsigned char q
) {
 int dif = row_size - w;
 int i,j;
 for (i = h; i > 0;--i)
 {
  for (j = w; j > 0;--j)
  {
   if (*src < min)
   {
    *dst = 0;
   }
   else if (*src > max)
   {
    *dst = 255;
   }else
   {
    *dst = (*src/q)*q;
   }
   src++;
   dst++;
  }
  src += dif;
  dst += dif;
 }
}

canonical_original_code:
void umbralizar_c(unsigned char *src, unsigned char *dst, int h, int w, int row_size, unsigned char min, unsigned char max, unsigned char q) {
 int dif = row_size - w;
 int i; int j;
 for (i = h; i > 0;--i)
 {
  for (j = w; j > 0;--j)
  {
   if (*src < min)
   {
    *dst = 0;
   }
   else if (*src > max)
   {
    *dst = 255;
   }else
   {
    *dst = (*src/q)*q;
   }
   src++;
   dst++;
  }
  src += dif;
  dst += dif;
 }
}

variable_types:
src
unsigned char *

variable_types:
dst
unsigned char *

variable_types: h int

variable_types: w int

variable_types:
min
unsigned char

variable_types:
max
unsigned char

variable_types:
q
unsigned char

variable_types: dif int

variable_types: i int

variable_types: j int


bold

repo: nlf/git-gh

decompiled_code:
char *__fastcall bold(const char *a1)
{
size_t v1; // rbp
char *v2; // r12

v1 = (int)(strlen(a1) + 9);
v2 = (char *)calloc(v1, 1uLL);
snprintf(v2, v1, "%s%s%s", "\x1B[1m", a1, "\x1B[0m");
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1)
{
size_t v1; // rbp
char *v2; // r12

v1 = (int)(strlen(a1) + 9);
v2 = (char *)calloc(v1, 1uLL);
snprintf(v2, v1, "%s%s%s", "\x1B[1m", a1, "\x1B[0m");
return v2;
}

original_code:
char* bold(char* str) {
    int str_len;
    char* newstr;
    char* boldstart = "\x1b[1m";
    char* boldend = "\x1b[0m";
    str_len = strlen(boldstart) + strlen(boldend) + strlen(str) + 1;
    newstr = calloc(str_len, sizeof(char));
    snprintf(newstr, str_len, "%s%s%s", boldstart, str, boldend);
    return newstr;
}

canonical_original_code:
char *bold(char *str) {
    int str_len;
    char *newstr;
    char *boldstart = "\x1b[1m";
    char *boldend = "\x1b[0m";
    str_len = strlen(boldstart) + strlen(boldend) + strlen(str) + 1;
    newstr = calloc(str_len, sizeof(char));
    snprintf(newstr, str_len, "%s%s%s", boldstart, str, boldend);
    return newstr;
}

variable_types:
str
char *

variable_types:
newstr
char *

variable_types:
boldstart
char *

variable_types:
boldend
char *

return_type: char *


decompiled_code:
int print_header()
{
return fprintf(
stdout,
"\x1B[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1B[0m\n",
"ID",
"Title",
"Assignee",
"Milestone",
"Comments");
}

canonical_decompiled_code:
int func0()
{
return fprintf(
stdout,
"\x1B[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1B[0m\n",
"ID",
"Title",
"Assignee",
"Milestone",
"Comments");
}

original_code:
void print_header() {
    fprintf(stdout, "\x1b[1m%-5s %-100.100s %-15.15s %-15.15s %-5s\x1b[0m\n", "ID", "Title", "Assignee", "Milestone", "Comments");
}


closestate

repo: nlm/throttle

decompiled_code:
// attributes: thunk
int closestate(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func2(int fd)
{
return close(fd);
}

variable_types: fd int


writestate

decompiled_code:
__int64 __fastcall writestate(int fd, void *buf)
{
int v2; // eax

lseek(fd, 0LL, 0);
v2 = write(fd, buf, 0x18uLL);
if ( v2 >= 0 )
return (unsigned int)-(v2 != 24);
perror("err:");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, void *buf)
{
int v2; // eax

lseek(fd, 0LL, 0);
v2 = write(fd, buf, 0x18uLL);
if ( v2 >= 0 )
return (unsigned int)-(v2 != 24);
perror("err:");
return 0xFFFFFFFFLL;
}

original_code:
int writestate(int fd, const thstate_t *state)
{
 int wsize;
 lseek(fd, 0, 0);
 wsize = write(fd, state, sizeof(thstate_t));
 if (wsize < 0)
  perror("err:");
 if (wsize != sizeof(thstate_t))
  return -1;
 return 0;
}

canonical_original_code:
int writestate(int fd, struct thstate_s *state)
{
 int wsize;
 lseek(fd, 0, 0);
 wsize = write(fd, state, sizeof(thstate_t));
 if (wsize < 0)
  perror("err:");
 if (wsize != sizeof(thstate_t))
  return -1;
 return 0;
}

variable_types: struct thstate_s { char magic[4]; long startts; unsigned int times; } *

variable_types: wsize int


initstate

original_code:
thstate_t *initstate(thstate_t *state)
{
 strncpy(state->magic, "THRO", 4);
 state->startts = time(((void *)0));
 state->times = 0;
 return state;
}

canonical_original_code:
struct thstate_s *initstate(struct thstate_s *state)
{
 strncpy(state->magic, "THRO", 4);
 state->startts = time(((void *)0));
 state->times = 0;
 return state;
}

variable_types:
state
struct thstate_s *

return_type: struct thstate_s { char magic[4]; long startts; unsigned int times; } *


xexec

decompiled_code:
int __fastcall xexec(int a1, const void *a2)
{
size_t v2; // rbx
void *v3; // rax
const char **v4; // rax

v2 = 8LL * (a1 + 1);
v3 = malloc(v2);
if ( !v3 )
{
perror("error: unable to allocate\n");
exit(102);
}
v4 = (const char **)memcpy(v3, a2, v2 - 8);
v4[v2 / 8 - 1] = 0LL;
return execv(*v4, (char *const *)v4);
}

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

v2 = 8LL * (a1 + 1);
v3 = malloc(v2);
if ( !v3 )
{
perror("error: unable to allocate\n");
exit(102);
}
v4 = (const char **)memcpy(v3, a2, v2 - 8);
v4[v2 / 8 - 1] = 0LL;
return execv(*v4, (char *const *)v4);
}

original_code:
int xexec(const int argc, char * const *argv)
{
 char **xargv;
 if (!(xargv = malloc(sizeof(char *) * (argc + 1))))
 {
  perror("error: unable to allocate\n");
  exit(102);
 }
 memcpy(xargv, argv, sizeof(char *) * argc);
 xargv[argc] = ((void *)0);
 return execv(xargv[0], xargv);
 free(xargv);
}

canonical_original_code:
int xexec(int argc, char **argv)
{
 char **xargv;
 if (!(xargv = malloc(sizeof(char *) * (argc + 1))))
 {
  perror("error: unable to allocate\n");
  exit(102);
 }
 memcpy(xargv, argv, sizeof(char *) * argc);
 xargv[argc] = ((void *)0);
 return execv(xargv[0], xargv);
 free(xargv);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
xargv
char **


initstate

user_defined_types: struct thstate_s { char magic[4]; long startts; unsigned int times; }


main

canonical_name: main

original_code:
int main(int argc, char **argv)
{
 unsigned int times;
 unsigned int period;
 const char *statefile;
 thstate_t thstate;
 thstate_t *state;
 time_t now;
 int fd;
 if (argc < 4)
 {
  usage();
  exit(101);
 }
 times = atoi(argv[1]);
 period = atoi(argv[2]);
 statefile = argv[3];
 if (times < 1 || period < 1)
 {
  usage();
  exit(101);
 }
 if ((fd = openstate(statefile)) < 0)
 {
  perror("unable to open state file");
  exit(103);
 }
 state = readstate(fd, &thstate);
 if (state == ((void *)0))
 {
  state = initstate(&thstate);
 }
 now = time(((void *)0));
 if (state->startts + period > now)
 {
  state->times++;
 }
 else
 {
  state->startts = now;
  state->times = 1;
 }
 writestate(fd, state);
 closestate(fd);
 if (state->times <= times)
 {
  if (xexec(argc - 4, argv + 4) == -1)
  {
   perror("exec");
   return 1;
  }
 }
 else
 {
  exit(100);
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 unsigned int times;
 unsigned int period;
 char *statefile;
 struct thstate_s thstate;
 struct thstate_s *state;
 long now;
 int fd;
 if (argc < 4)
 {
  usage();
  exit(101);
 }
 times = atoi(argv[1]);
 period = atoi(argv[2]);
 statefile = argv[3];
 if (times < 1 || period < 1)
 {
  usage();
  exit(101);
 }
 if ((fd = openstate(statefile)) < 0)
 {
  perror("unable to open state file");
  exit(103);
 }
 state = readstate(fd, &thstate);
 if (state == ((void *)0))
 {
  state = initstate(&thstate);
 }
 now = time(((void *)0));
 if (state->startts + period > now)
 {
  state->times++;
 }
 else
 {
  state->startts = now;
  state->times = 1;
 }
 writestate(fd, state);
 closestate(fd);
 if (state->times <= times)
 {
  if (xexec(argc - 4, argv + 4) == -1)
  {
   perror("exec");
   return 1;
  }
 }
 else
 {
  exit(100);
 }
 return 0;
}

variable_types:
times
unsigned int

variable_types:
period
unsigned int

variable_types:
statefile
char *

variable_types: struct thstate_s { char magic[4]; long startts; unsigned int times; }

variable_types: now long


closestate

original_code:
int closestate(int fd)
{
 return close(fd);
}


dumpstate

decompiled_code:
int __fastcall dumpstate(__int64 a1)
{
return printf("STATE: startts=%lu, times=%lu\n", *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
return printf("STATE: startts=%lu, times=%lu\n", *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
}

original_code:
void dumpstate(const thstate_t *state)
{
 printf("STATE: startts=%lu, times=%lu\n", (long)state->startts, (long)state->times);
}

canonical_original_code:
void dumpstate(struct thstate_s *state)
{
 printf("STATE: startts=%lu, times=%lu\n", (long)state->startts, (long)state->times);
}


main

repo: nmeyerhans/authd

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
unsigned int v4; // eax
unsigned int v5; // ebp
__pid_t v6; // eax
char *v8; // rax
int v9; // eax
__int64 v10; // rax
unsigned int v11; // er12
unsigned int v12; // eax
char *ptr; // [rsp+8h] [rbp-410h] BYREF
char v14[1032]; // [rsp+10h] [rbp-408h] BYREF

if ( argc > 1 )
{
if ( (v3 = argv[1], *(_WORD *)v3 == 26669) && !v3[2] || !strcmp(v3, "--help") )
{
usage(0LL);
exit(0);
}
}
v4 = getsid(0);
v5 = v4;
if ( v4 == -1 )
{
perror("getsid()");
exit(1);
}
asprintf(&ptr, "%d", v4);
read_ppid_cmdline(v5, v14, 1000LL);
v6 = fork();
if ( v6 )
{
if ( v6 < 0 )
{
perror("fork");
exit(1);
}
}
else
{
signal(1, (__sighandler_t)1);
signal(15, sig_handler);
signal(2, sig_handler);
v8 = (char *)pidfilename(ptr);
v9 = write_pidfile(v8);
if ( v9 )
{
if ( v9 > 0 )
{
v10 = pidfilename(0LL);
v11 = read_pidfile(v10);
fwrite("authd appears to already be running ", 1uLL, 0x24uLL, stderr);
fprintf(stderr, "in this session with pid %d\n", v11);
exit(1);
}
fwrite("Aborting after something unexpected happened.\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
v12 = getpid();
printf("authd is running as %d ", v12);
printf("in session %d\n", v5);
close(0);
close(1);
close(2);
child_loop(v5, v14);
destroy_pidfile();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
unsigned int v4; // eax
unsigned int v5; // ebp
__pid_t v6; // eax
char *v8; // rax
int v9; // eax
__int64 v10; // rax
unsigned int v11; // er12
unsigned int v12; // eax
char *ptr; // [rsp+8h] [rbp-410h] BYREF
char v14[1032]; // [rsp+10h] [rbp-408h] BYREF

if ( argc > 1 )
{
if ( (v3 = argv[1], *(_WORD *)v3 == 26669) && !v3[2] || !strcmp(v3, "--help") )
{
func12(0LL);
exit(0);
}
}
v4 = getsid(0);
v5 = v4;
if ( v4 == -1 )
{
perror("getsid()");
exit(1);
}
asprintf(&ptr, "%d", v4);
func15(v5, v14, 1000LL);
v6 = fork();
if ( v6 )
{
if ( v6 < 0 )
{
perror("fork");
exit(1);
}
}
else
{
signal(1, (__sighandler_t)1);
signal(15, sig_handler);
signal(2, sig_handler);
v8 = (char *)func7(ptr);
v9 = func8(v8);
if ( v9 )
{
if ( v9 > 0 )
{
v10 = func7(0LL);
v11 = func9(v10);
fwrite("authd appears to already be running ", 1uLL, 0x24uLL, stderr);
fprintf(stderr, "in this session with pid %d\n", v11);
exit(1);
}
fwrite("Aborting after something unexpected happened.\n", 1uLL, 0x2EuLL, stderr);
exit(1);
}
v12 = getpid();
printf("authd is running as %d ", v12);
printf("in session %d\n", v5);
close(0);
close(1);
close(2);
func14(v5, v14);
func10();
}
return 0;
}

original_code:
int main(int argc, char **argv) {
    int other_pid, ppid, child_pid;
    int sh_type;
    char cmdline[1000];
    char *pidstr;
    if(argc > 1) {
 if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
     usage(0);
     exit(0);
 }
    }
    if((ppid = getsid(0)) == -1) {
 perror("getsid()");
 exit(1);
    }
    asprintf(&pidstr, "%d", ppid);
    read_ppid_cmdline(ppid, cmdline, 1000);
    if(child_pid = fork()) {
 if(child_pid < 0) {
     perror("fork");
     exit(1);
 }
    }
    else {
 int rv;
 signal(1, ((__sighandler_t) 1));
 signal(15, (void*)sig_handler);
 signal(2, (void*)sig_handler);
 if((rv = write_pidfile(pidfilename(pidstr))) == 0) {
     printf("authd is running as %d ", getpid());
     printf("in session %d\n", ppid);
     close(0); close(1); close(2);
     child_loop(ppid, cmdline);
     destroy_pidfile();
 } else if(rv > 0) {
     int other_pid;
     other_pid = read_pidfile(pidfilename(((void *)0)));
     fprintf(stderr, "authd appears to already be running ");
     fprintf(stderr, "in this session with pid %d\n", other_pid);
     exit(1);
 } else {
     fprintf(stderr, "Aborting after something unexpected happened.\n");
     exit(1);
 }
    }
}

canonical_original_code:
int main(int argc, char **argv) {
    int other_pid; int ppid; int child_pid;
    int sh_type;
    char cmdline[1000];
    char *pidstr;
    if(argc > 1) {
 if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
     usage(0);
     exit(0);
 }
    }
    if((ppid = getsid(0)) == -1) {
 perror("getsid()");
 exit(1);
    }
    asprintf(&pidstr, "%d", ppid);
    read_ppid_cmdline(ppid, cmdline, 1000);
    if(child_pid = fork()) {
 if(child_pid < 0) {
     perror("fork");
     exit(1);
 }
    }
    else {
 int rv;
 signal(1, ((void (*)(int)) 1));
 signal(15, (void *)sig_handler);
 signal(2, (void *)sig_handler);
 if((rv = write_pidfile(pidfilename(pidstr))) == 0) {
     printf("authd is running as %d ", getpid());
     printf("in session %d\n", ppid);
     close(0); close(1); close(2);
     child_loop(ppid, cmdline);
     destroy_pidfile();
 } else if(rv > 0) {
     int other_pid;
     other_pid = read_pidfile(pidfilename(((void *)0)));
     fprintf(stderr, "authd appears to already be running ");
     fprintf(stderr, "in this session with pid %d\n", other_pid);
     exit(1);
 } else {
     fprintf(stderr, "Aborting after something unexpected happened.\n");
     exit(1);
 }
    }
}

variable_types: ppid int

variable_types:
cmdline
char [1000]

variable_types:
pidstr
char *

variable_types: rv int


pidfilename

decompiled_code:
char *__fastcall pidfilename(const char *a1)
{
char *result; // rax

result = pidfile_0;
if ( a1 )
{
result = getenv("LOGNAME");
if ( result )
{
snprintf(pidfile_0, 0x1000uLL, "/tmp/authd-%s/%s", result, a1);
result = pidfile_0;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
char *result; // rax

result = pidfile_0;
if ( a1 )
{
result = getenv("LOGNAME");
if ( result )
{
snprintf(pidfile_0, 0x1000uLL, "/tmp/authd-%s/%s", result, a1);
result = pidfile_0;
}
}
return result;
}

original_code:
char *pidfilename(char *name) {
    static char pidfile[4096];
    char *dir, *logname;
    if(!name)
 return pidfile;
    if(!(logname = getenv("LOGNAME")))
 return 0;
    snprintf(pidfile, 4096, "/tmp/authd-%s/%s", logname, name);
    return pidfile;
}

canonical_original_code:
char *pidfilename(char *name) {
    static char pidfile[4096];
    char *dir; char *logname;
    if(!name)
 return pidfile;
    if(!(logname = getenv("LOGNAME")))
 return 0;
    snprintf(pidfile, 4096, "/tmp/authd-%s/%s", logname, name);
    return pidfile;
}

variable_types:
name
char *

variable_types:
pidfile
char [4096]

variable_types:
dir
char *

variable_types:
logname
char *


decompiled_code:
__int64 __fastcall write_pidfile(char *file)
{
char *v1; // r13
char *v2; // r14
unsigned int v3; // er12
int v4; // ebp
unsigned int v6; // eax
char *v7; // r12
size_t v8; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v1 = strdup(file);
if ( !getenv("LOGNAME") )
return (unsigned int)-1;
v2 = dirname(v1);
v3 = mkdir(v2, 0x1C0u);
if ( v3 == -1 && *__errno_location() != 17 )
{
perror(v2);
return v3;
}
v4 = open(file, 193, 384LL);
if ( v4 == -1 )
{
v3 = *__errno_location();
if ( v3 != 17 )
{
v3 = -1;
perror(v1);
}
return v3;
}
v6 = getpid();
asprintf(&ptr, "%d\n", v6);
v7 = ptr;
v8 = strlen(ptr);
write(v4, v7, v8);
close(v4);
free(v1);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *file)
{
char *v1; // r13
char *v2; // r14
unsigned int v3; // er12
int v4; // ebp
unsigned int v6; // eax
char *v7; // r12
size_t v8; // rax
char *ptr; // [rsp+8h] [rbp-30h] BYREF

v1 = strdup(file);
if ( !getenv("LOGNAME") )
return (unsigned int)-1;
v2 = dirname(v1);
v3 = mkdir(v2, 0x1C0u);
if ( v3 == -1 && *__errno_location() != 17 )
{
perror(v2);
return v3;
}
v4 = open(file, 193, 384LL);
if ( v4 == -1 )
{
v3 = *__errno_location();
if ( v3 != 17 )
{
v3 = -1;
perror(v1);
}
return v3;
}
v6 = getpid();
asprintf(&ptr, "%d\n", v6);
v7 = ptr;
v8 = strlen(ptr);
write(v4, v7, v8);
close(v4);
free(v1);
free(ptr);
return 0LL;
}

original_code:
int write_pidfile(const char *file) {
    char *nam, *dir, *path, *pidstr;
    int fd, pid;
    path = strdup(file);
    nam = getenv("LOGNAME");
    if(!nam)
 return -1;
    dir = dirname(path);
    if(mkdir(dir, 0700) == -1) {
 if((*__errno_location ()) != 17) {
     perror(dir);
     return -1;
 }
    }
    if((fd = open(file, 0100|0200|01, 0600)) == -1) {
 if((*__errno_location ()) == 17) {
     return (*__errno_location ());
 }
 perror(path);
 return -1;
    }
    pid = getpid();
    asprintf(&pidstr, "%d\n", pid);
    write(fd, pidstr, strlen(pidstr));
    close(fd);
    free(path);
    free(pidstr);
    return 0;
}

canonical_original_code:
int write_pidfile(char *file) {
    char *nam; char *dir; char *path; char *pidstr;
    int fd; int pid;
    path = strdup(file);
    nam = getenv("LOGNAME");
    if(!nam)
 return -1;
    dir = dirname(path);
    if(mkdir(dir, 0700) == -1) {
 if((*__errno_location ()) != 17) {
     perror(dir);
     return -1;
 }
    }
    if((fd = open(file, 0100|0200|01, 0600)) == -1) {
 if((*__errno_location ()) == 17) {
     return (*__errno_location ());
 }
 perror(path);
 return -1;
    }
    pid = getpid();
    asprintf(&pidstr, "%d\n", pid);
    write(fd, pidstr, strlen(pidstr));
    close(fd);
    free(path);
    free(pidstr);
    return 0;
}

variable_types:
file
char *

variable_types:
nam
char *

variable_types:
path
char *

variable_types: fd int

variable_types: pid int


decompiled_code:
int destroy_pidfile()
{
char *v0; // rbp
char *v1; // rax
char *v2; // r12

v0 = pidfilename(0LL);
v1 = strdup(v0);
v2 = dirname(v1);
unlink(v0);
return rmdir(v2);
}

canonical_decompiled_code:
int func10()
{
char *v0; // rbp
char *v1; // rax
char *v2; // r12

v0 = func7(0LL);
v1 = strdup(v0);
v2 = dirname(v1);
unlink(v0);
return rmdir(v2);
}

original_code:
void destroy_pidfile() {
    char *pidfile = pidfilename(0);
    char *dir;
    dir = strdup(pidfile);
    dir = dirname(dir);
    unlink(pidfile);
    rmdir(dir);
}

variable_types:
pidfile
char *


decompiled_code:
void __noreturn sig_handler()
{
destroy_pidfile();
exit(0);
}

canonical_decompiled_code:
void __noreturn func11()
{
func10();
exit(0);
}

original_code:
void *sig_handler(int sig) {
    destroy_pidfile();
    exit(0);
}

variable_types: sig int

return_type: void *


decompiled_code:
_BOOL8 __fastcall check_parent(unsigned int a1, const char *a2)
{
int v2; // eax
int v3; // er12
char v5[1008]; // [rsp+0h] [rbp-808h] BYREF
char s[1048]; // [rsp+3F0h] [rbp-418h] BYREF

if ( kill(a1, 0) == -1 )
return 0LL;
snprintf(s, 0x400uLL, "/proc/%d/cmdline", a1);
v2 = open(s, 0);
v3 = v2;
if ( v2 == -1 )
return 0LL;
read(v2, v5, 0x3E8uLL);
close(v3);
return strcmp(a2, v5) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(unsigned int a1, const char *a2)
{
int v2; // eax
int v3; // er12
char v5[1008]; // [rsp+0h] [rbp-808h] BYREF
char s[1048]; // [rsp+3F0h] [rbp-418h] BYREF

if ( kill(a1, 0) == -1 )
return 0LL;
snprintf(s, 0x400uLL, "/proc/%d/cmdline", a1);
v2 = open(s, 0);
v3 = v2;
if ( v2 == -1 )
return 0LL;
read(v2, v5, 0x3E8uLL);
close(v3);
return strcmp(a2, v5) == 0;
}

original_code:
int check_parent(int gp_pid, char *cmdline) {
    char seen_cmdline[1000];
    char path[1024];
    int fd;
    if(kill(gp_pid, 0) == -1)
 return 0;
    snprintf(path, 1024, "/proc/%d/cmdline", gp_pid);
    fd = open(path, 00);
    if(fd == -1)
 return 0;
    read(fd, seen_cmdline, 1000);
    close(fd);
    if(strcmp(cmdline, seen_cmdline)) {
 return 0;
    }
    return 1;
}

variable_types:
cmdline
char *

variable_types:
path
char [1024]


decompiled_code:
_BOOL8 __fastcall child_loop(unsigned int a1, const char *a2)
{
int v2; // ebx
_BOOL8 result; // rax

v2 = 0;
LABEL_2:
while ( 1 )
{
result = check_parent(a1, a2);
if ( !result )
return result;
while ( 1 )
{
++v2;
sleep(0x3Cu);
if ( 60 * v2 <= 3599 )
break;
v2 = system("kinit -R > /dev/null");
if ( !v2 )
{
system("aklog > /dev/null");
goto LABEL_2;
}
v2 = 0;
result = check_parent(a1, a2);
if ( !result )
return result;
}
}
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(unsigned int a1, const char *a2)
{
int v2; // ebx
_BOOL8 result; // rax

v2 = 0;
LABEL_2:
while ( 1 )
{
result = func13(a1, a2);
if ( !result )
return result;
while ( 1 )
{
++v2;
sleep(0x3Cu);
if ( 60 * v2 <= 3599 )
break;
v2 = system("kinit -R > /dev/null");
if ( !v2 )
{
system("aklog > /dev/null");
goto LABEL_2;
}
v2 = 0;
result = func13(a1, a2);
if ( !result )
return result;
}
}
}

original_code:
void child_loop(int gp_pid, char *cmdline) {
    int loop_count = 0;
    while(1) {
 if(loop_count * 60 >= 3600) {
     loop_count = 0;
     if(system("kinit -R > /dev/null") == 0) {
  system("aklog > /dev/null");
     }
 }
 if(!check_parent(gp_pid, cmdline))
     break;
 loop_count++;
 sleep(60);
    }
}


decompiled_code:
ssize_t __fastcall read_ppid_cmdline(unsigned int a1, void *a2, int a3)
{
int v4; // eax
ssize_t result; // rax
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

sprintf(v6, "/proc/%d/cmdline", a1);
v4 = open(v6, 0);
if ( v4 < 0 )
{
perror("open");
exit(1);
}
result = read(v4, a2, a3);
if ( result < 0 )
{
perror("read");
exit(1);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func15(unsigned int a1, void *a2, int a3)
{
int v4; // eax
ssize_t result; // rax
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

sprintf(v6, "/proc/%d/cmdline", a1);
v4 = open(v6, 0);
if ( v4 < 0 )
{
perror("open");
exit(1);
}
result = read(v4, a2, a3);
if ( result < 0 )
{
perror("read");
exit(1);
}
return result;
}

original_code:
void read_ppid_cmdline(int pid, char *buf, int bufsz) {
    char path[1024];
    int fd;
    sprintf(path, "/proc/%d/cmdline", pid);
    fd = open(path, 00);
    if(fd < 0) {
 perror("open");
 exit(1);
    }
    if(read(fd, buf, bufsz) < 0) {
 perror("read");
 exit(1);
    }
}

variable_types:
buf
char *

variable_types: bufsz int


repo: nmsoccer/XXGame

decompiled_code:
_DWORD *create_mem_poll()
{
_DWORD *v0; // rax
_DWORD *v1; // r12

v0 = calloc(0xC0uLL, 1uLL);
v0[8] = 1;
v1 = v0;
v0[16] = 2;
v0[24] = 3;
v0[32] = 4;
v0[40] = 5;
puts("create mem poll success!");
return v1;
}

original_code:
xxmem_poll *create_mem_poll(void){
 xxmem_poll *p;
 int i;
 p = (xxmem_poll *)malloc(sizeof(xxmem_poll));
 memset(p , 0 , sizeof(xxmem_poll));
 for(i=1; i<= 5; i++){
  p->all_index[i].index = i;
 }
 printf("create mem poll success!\n");
 return p;
}

canonical_original_code:
struct _xxmem_poll *create_mem_poll(){
 struct _xxmem_poll *p;
 int i;
 p = (struct _xxmem_poll *)malloc(sizeof(xxmem_poll));
 memset(p , 0 , sizeof(xxmem_poll));
 for(i=1; i<= 5; i++){
  p->all_index[i].index = i;
 }
 printf("create mem poll success!\n");
 return p;
}

variable_types: struct _xxmem_poll { struct _xxmem_index { int index; int used_count; struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; } *used; int free_count; struct _xxmem_node *free; } all_index[-1]; } *

variable_types: i int

user_defined_types: struct _xxmem_poll { struct _xxmem_index all_index[-1]; }

user_defined_types: struct _xxmem_index { int index; int used_count; struct _xxmem_node *used; int free_count; struct _xxmem_node *free; }


decompiled_code:
__int64 __fastcall delete_mem_poll(void *ptr)
{
int v1; // er13
char *i; // r12
void **v4; // rbx
void **v5; // rbp
void **v6; // rbx
void **v7; // rbp

if ( ptr )
{
v1 = 1;
for ( i = (char *)ptr + 32; ; i += 32 )
{
if ( *(_DWORD *)i )
{
if ( *(_DWORD *)i != v1 )
return 0xFFFFFFFFLL;
v4 = (void **)*((_QWORD *)i + 1);
while ( v4 )
{
v5 = v4;
v4 = (void **)*v4;
free(v5[2]);
free(v5);
}
v6 = (void **)*((_QWORD *)i + 3);
while ( v6 )
{
v7 = v6;
v6 = (void **)*v6;
free(v7[2]);
free(v7);
}
}
if ( ++v1 == 6 )
{
free(ptr);
puts("delete mem poll success!");
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
int v1; // er13
char *i; // r12
void **v4; // rbx
void **v5; // rbp
void **v6; // rbx
void **v7; // rbp

if ( ptr )
{
v1 = 1;
for ( i = (char *)ptr + 32; ; i += 32 )
{
if ( *(_DWORD *)i )
{
if ( *(_DWORD *)i != v1 )
return 0xFFFFFFFFLL;
v4 = (void **)*((_QWORD *)i + 1);
while ( v4 )
{
v5 = v4;
v4 = (void **)*v4;
free(v5[2]);
free(v5);
}
v6 = (void **)*((_QWORD *)i + 3);
while ( v6 )
{
v7 = v6;
v6 = (void **)*v6;
free(v7[2]);
free(v7);
}
}
if ( ++v1 == 6 )
{
free(ptr);
puts("delete mem poll success!");
return 0LL;
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int delete_mem_poll(xxmem_poll *poll){
 xxmem_node *now = ((void *)0);
 xxmem_node *tmp = ((void *)0);
 int i;
 if(!poll){
  return -1;
 }
 for(i=1; i<=5; i++){
  if(poll->all_index[i].index == 0){
   continue;
  }
  if(poll->all_index[i].index != i){
   return -1;
  }
  now = poll->all_index[i].used;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
  now = poll->all_index[i].free;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
 }
 free(poll);
 printf("delete mem poll success!\n");
 return 0;
}

canonical_original_code:
int delete_mem_poll(struct _xxmem_poll *poll){
 struct _xxmem_node *now = ((void *)0);
 struct _xxmem_node *tmp = ((void *)0);
 int i;
 if(!poll){
  return -1;
 }
 for(i=1; i<=5; i++){
  if(poll->all_index[i].index == 0){
   continue;
  }
  if(poll->all_index[i].index != i){
   return -1;
  }
  now = poll->all_index[i].used;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
  now = poll->all_index[i].free;
  while(1){
   if(!now){
    break;
   }
   tmp = now->next;
   free(now->data);
   free(now);
   now = tmp;
  }
 }
 free(poll);
 printf("delete mem poll success!\n");
 return 0;
}

variable_types: struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; } *


original_code:
void *xx_alloc_mem(xxmem_poll *poll , int size){
 xxmem_node *now;
 int index = 0;
 if(!poll || size<=0){
  return ((void *)0);
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(index > 5){
  return ((void *)0);
 }
 if(poll->all_index[index].free_count == 0){
  printf("malloc new size: %d\n" , index * 1024);
  now = (xxmem_node *)malloc(sizeof(xxmem_node));
  now->real_size = size;
  now->data = malloc(index * 1024);
  now->next = poll->all_index[index].used;
  poll->all_index[index].used = now;
  poll->all_index[index].used_count++;
  return now->data;
 }
 printf("use free block! \n ");
 now = poll->all_index[index].free;
 now->real_size = size;
 poll->all_index[index].free = now->next;
 poll->all_index[index].free_count--;
 now->next = poll->all_index[index].used;
 poll->all_index[index].used = now;
 poll->all_index[index].used_count++;
 return now->data;
}

canonical_original_code:
void *xx_alloc_mem(struct _xxmem_poll *poll, int size){
 struct _xxmem_node *now;
 int index = 0;
 if(!poll || size<=0){
  return ((void *)0);
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(index > 5){
  return ((void *)0);
 }
 if(poll->all_index[index].free_count == 0){
  printf("malloc new size: %d\n" , index * 1024);
  now = (struct _xxmem_node *)malloc(sizeof(xxmem_node));
  now->real_size = size;
  now->data = malloc(index * 1024);
  now->next = poll->all_index[index].used;
  poll->all_index[index].used = now;
  poll->all_index[index].used_count++;
  return now->data;
 }
 printf("use free block! \n ");
 now = poll->all_index[index].free;
 now->real_size = size;
 poll->all_index[index].free = now->next;
 poll->all_index[index].free_count--;
 now->next = poll->all_index[index].used;
 poll->all_index[index].used = now;
 poll->all_index[index].used_count++;
 return now->data;
}

variable_types: size int

variable_types: index int

user_defined_types: struct _xxmem_node { struct _xxmem_node *next; int real_size; void *data; }


original_code:
int xx_free_mem(xxmem_poll *poll , void *ptr , int size){
 xxmem_node *now = ((void *)0);
 xxmem_node *prev = ((void *)0);
 int index = 0;
 if(!poll || !ptr || size<=0){
  return -1;
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(poll->all_index[index].used->real_size == size && poll->all_index[index].used->data == ptr){
  now = poll->all_index[index].used;
  poll->all_index[index].used = now->next;
  poll->all_index[index].used_count--;
  now->next = poll->all_index[index].free;
  poll->all_index[index].free = now;
  poll->all_index[index].free_count++;
 }else{
  prev = poll->all_index[index].used;
  while(1){
   now = prev->next;
   if(now == ((void *)0)){
    return -1;
   }
   if(now->real_size == size && now->data == ptr){
    prev->next = now->next;
    poll->all_index[index].used_count--;
    now->next = poll->all_index[index].free;
    poll->all_index[index].free = now;
    poll->all_index[index].free_count++;
    break;
   }
   prev = now;
  }
 }
 return 0;
}

canonical_original_code:
int xx_free_mem(struct _xxmem_poll *poll, void *ptr, int size){
 struct _xxmem_node *now = ((void *)0);
 struct _xxmem_node *prev = ((void *)0);
 int index = 0;
 if(!poll || !ptr || size<=0){
  return -1;
 }
 if(size <= 1024){
  index = 1;
 }else{
  index = size / 1024;
  if(size % 1024 != 0){
   index++;
  }
 }
 if(poll->all_index[index].used->real_size == size && poll->all_index[index].used->data == ptr){
  now = poll->all_index[index].used;
  poll->all_index[index].used = now->next;
  poll->all_index[index].used_count--;
  now->next = poll->all_index[index].free;
  poll->all_index[index].free = now;
  poll->all_index[index].free_count++;
 }else{
  prev = poll->all_index[index].used;
  while(1){
   now = prev->next;
   if(now == ((void *)0)){
    return -1;
   }
   if(now->real_size == size && now->data == ptr){
    prev->next = now->next;
    poll->all_index[index].used_count--;
    now->next = poll->all_index[index].free;
    poll->all_index[index].free = now;
    poll->all_index[index].free_count++;
    break;
   }
   prev = now;
  }
 }
 return 0;
}

variable_types:
ptr
void *


decompiled_code:
__int64 __fastcall get_index_container(int a1, int a2)
{
int v2; // ecx
unsigned int v3; // er8
__int64 result; // rax

v2 = 253;
v3 = (a2 | a1) & 0x7FFFFF;
result = v3 % 0xFD;
while ( !interface_container[2 * (int)result] || dword_688[4 * (int)result] != v3 )
{
result = ((int)result + 1) % 0xFDu;
if ( !--v2 )
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
int v2; // ecx
unsigned int v3; // er8
__int64 result; // rax

v2 = 253;
v3 = (a2 | a1) & 0x7FFFFF;
result = v3 % 0xFD;
while ( !interface_container[2 * (int)result] || dword_688[4 * (int)result] != v3 )
{
result = ((int)result + 1) % 0xFDu;
if ( !--v2 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int get_index_container(proc_t proc1 , proc_t proc2){
 int index = 0;
 proc_t or_procs = 0;
 int icount = 0;
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   break;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   return -1;
  }
 }
 return index;
}

canonical_original_code:
static int get_index_container(unsigned int proc1, unsigned int proc2){
 int index = 0;
 unsigned int or_procs = 0;
 int icount = 0;
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   break;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   return -1;
  }
 }
 return index;
}

variable_types: icount int


decompiled_code:
void *__fastcall attach_bus(int a1, int a2)
{
int v2; // eax
void *result; // rax

puts("attach bus...");
v2 = shmget(a2 | a1, 0LL, 0);
if ( v2 < 0 )
result = 0LL;
else
result = shmat(v2, 0LL, 0);
return result;
}

canonical_decompiled_code:
void *__fastcall func1(int a1, int a2)
{
int v2; // eax
void *result; // rax

puts("attach bus...");
v2 = shmget(a2 | a1, 0LL, 0);
if ( v2 < 0 )
result = 0LL;
else
result = shmat(v2, 0LL, 0);
return result;
}

original_code:
static bus_interface *attach_bus(proc_t proc1 , proc_t proc2){
 int ishm_id = -1;
 key_t key;
 bus_interface *pstbus_interface = ((void *)0);
 printf("attach bus...\n");
 key = proc1 | proc2;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  return ((void *)0);
 }
 pstbus_interface = (bus_interface *)shmat(ishm_id , ((void *)0) , 0);
 if(!pstbus_interface){
  return ((void *)0);
 }
 return pstbus_interface;
}

canonical_original_code:
static struct stbus_interface *attach_bus(unsigned int proc1, unsigned int proc2){
 int ishm_id = -1;
 int key;
 struct stbus_interface *pstbus_interface = ((void *)0);
 printf("attach bus...\n");
 key = proc1 | proc2;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  return ((void *)0);
 }
 pstbus_interface = (struct stbus_interface *)shmat(ishm_id , ((void *)0) , 0);
 if(!pstbus_interface){
  return ((void *)0);
 }
 return pstbus_interface;
}

variable_types: key int

variable_types: struct stbus_interface { unsigned int udwproc_id_recv_ch1; struct _bus_channel { struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; } channel_one; unsigned int udwproc_id_recv_ch2; struct _bus_channel channel_two; } *

user_defined_types: struct stbus_interface { unsigned int udwproc_id_recv_ch1; struct _bus_channel channel_one; unsigned int udwproc_id_recv_ch2; struct _bus_channel channel_two; }

user_defined_types: struct _bus_channel { struct _sspackage data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; }

user_defined_types: struct _sspackage { struct _sshead sshead; union _ssdata data; }

user_defined_types: union _ssdata { struct cspackage_t cs_data; }

user_defined_types: struct cspackage_t { struct _cshead cshead; union _csdata data; }

user_defined_types: union _csdata { struct _req_validate_info req_validate_player; struct _reply_validate_info reply_validate_player; char acdata[512]; }


decompiled_code:
__int64 __fastcall send_bus(int a1, int a2, _DWORD *a3, _QWORD *a4)
{
int v6; // er8
_DWORD *v7; // rdx
_QWORD *v8; // rcx
int v9; // eax

if ( !a3 )
return 0xFFFFFFFFLL;
if ( !a4 )
return 0xFFFFFFFFLL;
v6 = a3[5322];
if ( (a1 | a2) != (v6 | *a3) )
return 0xFFFFFFFFLL;
if ( *a3 != a1 )
{
if ( v6 == a1 )
{
v7 = a3 + 5323;
goto LABEL_7;
}
return 0xFFFFFFFFLL;
}
v7 = a3 + 1;
LABEL_7:
if ( *((_BYTE *)v7 + 21280) == 40 )
return 4294967294LL;
v8 = &v7[133 * *((unsigned __int8 *)v7 + 21282)];
*v8 = *a4;
*(_QWORD *)((char *)v8 + 524) = *(_QWORD *)((char *)a4 + 524);
qmemcpy(
(void *)((unsigned __int64)(v8 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)a4 - ((char *)v8 - ((unsigned __int64)(v8 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v8 - (((_DWORD)v8 + 8) & 0xFFFFFFF8) + 532) >> 3));
v9 = *((unsigned __int8 *)v7 + 21282);
++*((_BYTE *)v7 + 21280);
*((_BYTE *)v7 + 21282) = (v9 + 1) % 0x28u;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, _DWORD *a3, _QWORD *a4)
{
int v6; // er8
_DWORD *v7; // rdx
_QWORD *v8; // rcx
int v9; // eax

if ( !a3 )
return 0xFFFFFFFFLL;
if ( !a4 )
return 0xFFFFFFFFLL;
v6 = a3[5322];
if ( (a1 | a2) != (v6 | *a3) )
return 0xFFFFFFFFLL;
if ( *a3 != a1 )
{
if ( v6 == a1 )
{
v7 = a3 + 5323;
goto LABEL_7;
}
return 0xFFFFFFFFLL;
}
v7 = a3 + 1;
LABEL_7:
if ( *((_BYTE *)v7 + 21280) == 40 )
return 4294967294LL;
v8 = &v7[133 * *((unsigned __int8 *)v7 + 21282)];
*v8 = *a4;
*(_QWORD *)((char *)v8 + 524) = *(_QWORD *)((char *)a4 + 524);
qmemcpy(
(void *)((unsigned __int64)(v8 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)a4 - ((char *)v8 - ((unsigned __int64)(v8 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)v8 - (((_DWORD)v8 + 8) & 0xFFFFFFF8) + 532) >> 3));
v9 = *((unsigned __int8 *)v7 + 21282);
++*((_BYTE *)v7 + 21280);
*((_BYTE *)v7 + 21282) = (v9 + 1) % 0x28u;
return 0LL;
}

original_code:
static int send_bus(proc_t recv_proc , proc_t send_proc , bus_interface *bus , sspackage_t *package){
 bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if((recv_proc | send_proc) != (bus->udwproc_id_recv_ch1 | bus->udwproc_id_recv_ch2)){
  return -1;
 }
 do{
  if(recv_proc == bus->udwproc_id_recv_ch1){
   channel = &bus->channel_one;
   break;
  }
  if(recv_proc == bus->udwproc_id_recv_ch2){
   channel = &bus->channel_two;
   break;
  }
  return -1;
 }while(0);
 if(channel->package_num == 40){
  return -2;
 }
 memcpy(&channel->data[channel->tail] , package , sizeof(sspackage_t));
 channel->tail = (channel->tail + 1) % 40;
 channel->package_num++;
 return 0;
}

canonical_original_code:
static int send_bus(unsigned int recv_proc, unsigned int send_proc, struct stbus_interface *bus, struct _sspackage *package){
 struct _bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if((recv_proc | send_proc) != (bus->udwproc_id_recv_ch1 | bus->udwproc_id_recv_ch2)){
  return -1;
 }
 do{
  if(recv_proc == bus->udwproc_id_recv_ch1){
   channel = &bus->channel_one;
   break;
  }
  if(recv_proc == bus->udwproc_id_recv_ch2){
   channel = &bus->channel_two;
   break;
  }
  return -1;
 }while(0);
 if(channel->package_num == 40){
  return -2;
 }
 memcpy(&channel->data[channel->tail] , package , sizeof(sspackage_t));
 channel->tail = (channel->tail + 1) % 40;
 channel->package_num++;
 return 0;
}

variable_types:
package
struct _sspackage *

variable_types: struct _bus_channel { struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } data[40]; unsigned char package_num; unsigned char head; unsigned char tail; unsigned char spin_lock; } *


decompiled_code:
__int64 __fastcall recv_bus(int a1, int a2, _DWORD *a3, _QWORD *a4)
{
int v4; // er8
_DWORD *v5; // rdx
_QWORD *v6; // rsi
int v7; // eax

if ( !a3 || !a4 )
return 0xFFFFFFFFLL;
v4 = a3[5322];
if ( *a3 != a1 )
{
if ( *a3 == a2 && a1 == v4 )
{
v5 = a3 + 5323;
goto LABEL_7;
}
return 0xFFFFFFFFLL;
}
if ( a2 != v4 )
return 0xFFFFFFFFLL;
v5 = a3 + 1;
LABEL_7:
if ( !*((_BYTE *)v5 + 21280) )
return 4294967294LL;
v6 = &v5[133 * *((unsigned __int8 *)v5 + 21281)];
*a4 = *v6;
*(_QWORD *)((char *)a4 + 524) = *(_QWORD *)((char *)v6 + 524);
qmemcpy(
(void *)((unsigned __int64)(a4 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v6 - ((char *)a4 - ((unsigned __int64)(a4 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a4 - (((_DWORD)a4 + 8) & 0xFFFFFFF8) + 532) >> 3));
v7 = *((unsigned __int8 *)v5 + 21281);
--*((_BYTE *)v5 + 21280);
*((_BYTE *)v5 + 21281) = (v7 + 1) % 0x28u;
return 0LL;
}

original_code:
static int recv_bus(u32 recv_proc , u32 send_proc , bus_interface *bus , sspackage_t *package){
 bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if(recv_proc == bus->udwproc_id_recv_ch1){
  if(send_proc != bus->udwproc_id_recv_ch2){
   return -1;
  }
  channel = &bus->channel_one;
 }else if(recv_proc == bus->udwproc_id_recv_ch2){
  if(send_proc != bus->udwproc_id_recv_ch1){
   return -1;
  }
  channel = &bus->channel_two;
 }else{
  return -1;
 }
 if(channel->package_num == 0){
    return -2;
 }
 memcpy(package , &channel->data[channel->head] , sizeof(sspackage_t));
 channel->head = (channel->head + 1) % 40;
 channel->package_num--;
 return 0;
}

canonical_original_code:
static int recv_bus(unsigned int recv_proc, unsigned int send_proc, struct stbus_interface *bus, struct _sspackage *package){
 struct _bus_channel *channel = ((void *)0);
 if(!bus || !package){
  return -1;
 }
 if(recv_proc == bus->udwproc_id_recv_ch1){
  if(send_proc != bus->udwproc_id_recv_ch2){
   return -1;
  }
  channel = &bus->channel_one;
 }else if(recv_proc == bus->udwproc_id_recv_ch2){
  if(send_proc != bus->udwproc_id_recv_ch1){
   return -1;
  }
  channel = &bus->channel_two;
 }else{
  return -1;
 }
 if(channel->package_num == 0){
    return -2;
 }
 memcpy(package , &channel->data[channel->head] , sizeof(sspackage_t));
 channel->head = (channel->head + 1) % 40;
 channel->package_num--;
 return 0;
}

user_defined_types: struct _sshead { unsigned short package_type; int client_fd; }

user_defined_types: struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; }

user_defined_types: struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; }

user_defined_types: struct _reply_validate_info { unsigned char is_validate; }


decompiled_code:
__int64 __fastcall open_bus(unsigned int a1, unsigned int a2)
{
__int64 v2; // r9
int v4; // esi
unsigned int v5; // ebp
signed int v6; // eax
__int64 v7; // rdx
unsigned __int64 v8; // rdx
__int64 *v9; // rbx
int *v10; // r12
__int64 v11; // rdx
int v12; // ecx
__int64 result; // rax
void *v14; // rax

v2 = a2;
v4 = 253;
v5 = (a1 | a2) & 0x7FFFFF;
v6 = v5 % 0xFD;
while ( 1 )
{
v8 = 16LL * v6;
v9 = &interface_container[v8 / 8];
v10 = &dword_688[v8 / 4];
v11 = interface_container[v8 / 8];
v12 = *((_DWORD *)v9 + 2);
if ( !v12 && !v11 )
break;
if ( v12 == v5 && v11 )
{
printf("bus between %d <-> %d is opened already!!\n", a1, (unsigned int)v2);
return 0LL;
}
v7 = (unsigned int)(v6 + 1);
v6 = (v6 + 1) % 0xFDu;
if ( !--v4 )
{
write_log(3LL, "open bus some bad error happened!\n", v7, 253 * ((unsigned int)v7 / 0xFD), 2172947881LL, v2);
return 0xFFFFFFFFLL;
}
}
v14 = attach_bus(a1, v2);
if ( v14 )
{
*v9 = (__int64)v14;
result = 0LL;
*v10 = v5;
}
else
{
puts("open bus failed!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, unsigned int a2)
{
__int64 v2; // r9
int v4; // esi
unsigned int v5; // ebp
signed int v6; // eax
__int64 v7; // rdx
unsigned __int64 v8; // rdx
__int64 *v9; // rbx
int *v10; // r12
__int64 v11; // rdx
int v12; // ecx
__int64 result; // rax
void *v14; // rax

v2 = a2;
v4 = 253;
v5 = (a1 | a2) & 0x7FFFFF;
v6 = v5 % 0xFD;
while ( 1 )
{
v8 = 16LL * v6;
v9 = &interface_container[v8 / 8];
v10 = &dword_688[v8 / 4];
v11 = interface_container[v8 / 8];
v12 = *((_DWORD *)v9 + 2);
if ( !v12 && !v11 )
break;
if ( v12 == v5 && v11 )
{
printf("bus between %d <-> %d is opened already!!\n", a1, (unsigned int)v2);
return 0LL;
}
v7 = (unsigned int)(v6 + 1);
v6 = (v6 + 1) % 0xFDu;
if ( !--v4 )
{
write_log(3LL, "open bus some bad error happened!\n", v7, 253 * ((unsigned int)v7 / 0xFD), 2172947881LL, v2);
return 0xFFFFFFFFLL;
}
}
v14 = func1(a1, v2);
if ( v14 )
{
*v9 = (__int64)v14;
result = 0LL;
*v10 = v5;
}
else
{
puts("open bus failed!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int open_bus(proc_t proc1 , proc_t proc2){
 int index = 0;
 proc_t or_procs = 0;
 bus_interface *interface_of_procs = ((void *)0);
 int icount = 0;
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "open_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].or_two_proc == 0 && interface_container[index].interface == ((void *)0)){
   break;
  }
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   printf("bus between %d <-> %d is opened already!!\n" , proc1 , proc2);
   return 0;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   write_log(3 , "open bus some bad error happened!\n");
   return -1;
  }
 }
 interface_of_procs = attach_bus(proc1 , proc2);
 if(!interface_of_procs){
  printf("open bus failed!\n");
  return -1;
 }
 interface_container[index].interface = interface_of_procs;
 interface_container[index].or_two_proc = or_procs;
 return 0;
}

canonical_original_code:
int open_bus(unsigned int proc1, unsigned int proc2){
 int index = 0;
 unsigned int or_procs = 0;
 struct stbus_interface *interface_of_procs = ((void *)0);
 int icount = 0;
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "open_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 or_procs = (proc1 & 0x007FFFFF) | (proc2 & 0x007FFFFF);
 index = or_procs % (23 * ((23 - 1) >> 1));
 while(1){
  if(interface_container[index].or_two_proc == 0 && interface_container[index].interface == ((void *)0)){
   break;
  }
  if(interface_container[index].interface != ((void *)0) && interface_container[index].or_two_proc == or_procs){
   printf("bus between %d <-> %d is opened already!!\n" , proc1 , proc2);
   return 0;
  }
  index = (index + 1) % (23 * ((23 - 1) >> 1));
  icount++;
  if(icount >= (23 * ((23 - 1) >> 1))){
   write_log(3 , "open bus some bad error happened!\n");
   return -1;
  }
 }
 interface_of_procs = attach_bus(proc1 , proc2);
 if(!interface_of_procs){
  printf("open bus failed!\n");
  return -1;
 }
 interface_container[index].interface = interface_of_procs;
 interface_container[index].or_two_proc = or_procs;
 return 0;
}


decompiled_code:
__int64 __fastcall close_bus(int a1, int a2)
{
int v2; // eax
unsigned __int64 v3; // rbx

v2 = get_index_container(a1, a2);
if ( v2 != -1 )
{
v3 = 4LL * v2;
detach_bus_isra_0((const void *)interface_container[v3 / 2]);
interface_container[v3 / 2] = 0LL;
dword_688[v3] = 0;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, int a2)
{
int v2; // eax
unsigned __int64 v3; // rbx

v2 = func0(a1, a2);
if ( v2 != -1 )
{
v3 = 4LL * v2;
func4((const void *)interface_container[v3 / 2]);
interface_container[v3 / 2] = 0LL;
dword_688[v3] = 0;
}
return 0LL;
}

original_code:
int close_bus(proc_t proc1 , proc_t proc2){
 int index = -1;
 bus_interface *interface_of_procs = ((void *)0);
 if(proc1 & 0xF0000000 != proc2 & 0xF0000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different world" , proc1 , proc2);
  return -1;
 }
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 index = get_index_container(proc1 , proc2);
 if(index == -1){
  return 0;
 }
 interface_of_procs = interface_container[index].interface;
 detach_bus(interface_of_procs);
 interface_container[index].interface = ((void *)0);
 interface_container[index].or_two_proc = 0;
 return 0;
}

canonical_original_code:
int close_bus(unsigned int proc1, unsigned int proc2){
 int index = -1;
 struct stbus_interface *interface_of_procs = ((void *)0);
 if(proc1 & 0xF0000000 != proc2 & 0xF0000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different world" , proc1 , proc2);
  return -1;
 }
 if(proc1 & 0x0F000000 != proc2 & 0x0F000000){
  write_log(3 , "close_bus failed!:proc1:%x < ->  proc2:%x in different line" , proc1 , proc2);
  return -1;
 }
 index = get_index_container(proc1 , proc2);
 if(index == -1){
  return 0;
 }
 interface_of_procs = interface_container[index].interface;
 detach_bus(interface_of_procs);
 interface_container[index].interface = ((void *)0);
 interface_container[index].or_two_proc = 0;
 return 0;
}


decompiled_code:
__int64 __fastcall send_bus_pkg(int a1, int a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v5; // r8
__int64 v6; // r9
int v7; // er10
_QWORD *v8; // r11

v2 = get_index_container(a1, a2);
if ( v2 != -1 )
return send_bus(v6, v7, (_DWORD *)interface_container[2 * v2], v8);
write_log(3LL, "send_bus_pkg:get_index_container index = -1", v3, v4, v5, v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v5; // r8
__int64 v6; // r9
int v7; // er10
_QWORD *v8; // r11

v2 = func0(a1, a2);
if ( v2 != -1 )
return func2(v6, v7, (_DWORD *)interface_container[2 * v2], v8);
write_log(3LL, "send_bus_pkg:get_index_container index = -1", v3, v4, v5, v6);
return 0xFFFFFFFFLL;
}

original_code:
int send_bus_pkg(proc_t recv_proc , proc_t send_proc , sspackage_t *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "send_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = send_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

canonical_original_code:
int send_bus_pkg(unsigned int recv_proc, unsigned int send_proc, struct _sspackage *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "send_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "send_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = send_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

variable_types: struct _sspackage { struct _sshead { unsigned short package_type; int client_fd; } sshead; union _ssdata { struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } cs_data; } data; } *

variable_types: iret int


decompiled_code:
__int64 __fastcall get_bus_pkg(int a1, int a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v5; // r8
__int64 v6; // r9
int v7; // er10
_QWORD *v8; // r11

v2 = get_index_container(a1, a2);
if ( v2 != -1 )
return recv_bus(v6, v7, (_DWORD *)interface_container[2 * v2], v8);
write_log(3LL, "recv_bus_pkg:get_index_container index = -1", v3, v4, v5, v6);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
int v2; // eax
__int64 v3; // rdx
__int64 v4; // rcx
__int64 v5; // r8
__int64 v6; // r9
int v7; // er10
_QWORD *v8; // r11

v2 = func0(a1, a2);
if ( v2 != -1 )
return func3(v6, v7, (_DWORD *)interface_container[2 * v2], v8);
write_log(3LL, "recv_bus_pkg:get_index_container index = -1", v3, v4, v5, v6);
return 0xFFFFFFFFLL;
}

original_code:
int get_bus_pkg(u32 recv_proc , u32 send_proc , sspackage_t *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "recv_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = recv_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}

canonical_original_code:
int get_bus_pkg(unsigned int recv_proc, unsigned int send_proc, struct _sspackage *package){
 int index = -1;
 int iret = -1;
 if(recv_proc & 0xF0000000 != send_proc & 0xF0000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different world" , send_proc , recv_proc);
  return -1;
 }
 if(recv_proc & 0x0F000000 != send_proc & 0x0F000000){
  write_log(3 , "recv_bus_pkg failed!:send_proc:%x  ->  recv_proc:%x in different line" , send_proc , recv_proc);
  return -1;
 }
 index = get_index_container(recv_proc , send_proc);
 if(index == -1){
  write_log(3 , "recv_bus_pkg:get_index_container index = -1");
  return -1;
 }
 iret = recv_bus(recv_proc , send_proc , interface_container[index].interface , package);
 return iret;
}


decompiled_code:
void *__fastcall attach_shm_res(int a1, int a2, int a3)
{
unsigned int v3; // er12
int v4; // edi
void *v5; // r13

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return 0LL;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "attach_shm_res: shmget shm res %x failed!", v3);
v5 = 0LL;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( !v5 )
write_log(3LL, "attach_shm_res: shmat shm res %x failed!", v3);
}
return v5;
}

canonical_decompiled_code:
void *__fastcall func9(int a1, int a2, int a3)
{
unsigned int v3; // er12
int v4; // edi
void *v5; // r13

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return 0LL;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "attach_shm_res: shmget shm res %x failed!", v3);
v5 = 0LL;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( !v5 )
write_log(3LL, "attach_shm_res: shmat shm res %x failed!", v3);
}
return v5;
}

original_code:
void *attach_shm_res(int res_type , int world_id , int line_id){
 void *addr;
 int ishm_id;
 key_t key;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return ((void *)0);
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "attach_shm_res: shmget shm res %x failed!" , key);
  return ((void *)0);
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "attach_shm_res: shmat shm res %x failed!" , key);
  return ((void *)0);
 }
 return addr;
}

canonical_original_code:
void *attach_shm_res(int res_type, int world_id, int line_id){
 void *addr;
 int ishm_id;
 int key;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return ((void *)0);
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "attach_shm_res: shmget shm res %x failed!" , key);
  return ((void *)0);
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "attach_shm_res: shmat shm res %x failed!" , key);
  return ((void *)0);
 }
 return addr;
}

variable_types:
addr
void *


decompiled_code:
__int64 __fastcall detach_shm_res(int a1, int a2, int a3)
{
unsigned int v3; // er12
int v4; // edi
void *v5; // rdi
unsigned int v6; // er13

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return (unsigned int)-1;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "detach_shm_res: shmget shm res %x failed!", v3);
v6 = -1;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( v5 )
{
v6 = shmdt(v5);
if ( v6 == -1 )
write_log(3LL, "detach_shm_res: shmdt shm res %x failed!", v3);
}
else
{
write_log(3LL, "detach_shm_res: shmat shm res %x failed!", v3);
v6 = -1;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2, int a3)
{
unsigned int v3; // er12
int v4; // edi
void *v5; // rdi
unsigned int v6; // er13

if ( a3 <= 0 || a2 <= 0 || a1 <= 0 )
return (unsigned int)-1;
v3 = a1 | (a2 << 28) | (a3 << 24) & 0xF000000 | 0x800000;
v4 = shmget(v3, 0LL, 0);
if ( v4 < 0 )
{
write_log(3LL, "detach_shm_res: shmget shm res %x failed!", v3);
v6 = -1;
}
else
{
v5 = shmat(v4, 0LL, 0);
if ( v5 )
{
v6 = shmdt(v5);
if ( v6 == -1 )
write_log(3LL, "detach_shm_res: shmdt shm res %x failed!", v3);
}
else
{
write_log(3LL, "detach_shm_res: shmat shm res %x failed!", v3);
v6 = -1;
}
}
return v6;
}

original_code:
int detach_shm_res(int res_type , int world_id , int line_id){
 int ishm_id;
 key_t key;
 void *addr;
 int iret;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return -1;
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "detach_shm_res: shmget shm res %x failed!" , key);
  return -1;
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "detach_shm_res: shmat shm res %x failed!" , key);
  return -1;
 }
 iret = shmdt(addr);
 if(iret == -1){
  write_log(3 , "detach_shm_res: shmdt shm res %x failed!" , key);
  return -1;
 }
 return iret;
}

canonical_original_code:
int detach_shm_res(int res_type, int world_id, int line_id){
 int ishm_id;
 int key;
 void *addr;
 int iret;
 if(res_type <= 0 || world_id <= 0 || line_id <= 0){
  return -1;
 }
 key = (world_id << 28 & 0xF0000000) | (line_id << 24 & 0x0F000000) | 0x00800000 | res_type;
 ishm_id = shmget(key , 0 , 0);
 if(ishm_id < 0 ){
  write_log(3 , "detach_shm_res: shmget shm res %x failed!" , key);
  return -1;
 }
 addr = shmat(ishm_id , ((void *)0) , 0);
 if(!addr){
  write_log(3 , "detach_shm_res: shmat shm res %x failed!" , key);
  return -1;
 }
 iret = shmdt(addr);
 if(iret == -1){
  write_log(3 , "detach_shm_res: shmdt shm res %x failed!" , key);
  return -1;
 }
 return iret;
}


decompiled_code:
ssize_t __fastcall read_socket(int a1, void *a2)
{
ssize_t result; // rax

if ( a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = recv(a1, a2, 0x20CuLL, 0);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(int a1, void *a2)
{
ssize_t result; // rax

if ( a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = recv(a1, a2, 0x20CuLL, 0);
return result;
}

original_code:
static int read_socket(int isock_fd , cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return recv(isock_fd , cspackage , sizeof(cspackage_t) , 0);
}

canonical_original_code:
static int read_socket(int isock_fd, struct cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return recv(isock_fd , cspackage , sizeof(cspackage_t) , 0);
}

variable_types: struct cspackage_t { struct _cshead { unsigned int seq; unsigned short proto_type; unsigned short validate_seq; unsigned char major_version; unsigned char minor_version; } cshead; union _csdata { struct _req_validate_info { unsigned char is_validate; unsigned short proto_type; char player_name[64]; char player_passwd[32]; } req_validate_player; struct _reply_validate_info { unsigned char is_validate; } reply_validate_player; char acdata[512]; } data; } *


decompiled_code:
ssize_t __fastcall write_socket(int a1, const void *a2)
{
ssize_t result; // rax

if ( a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = send(a1, a2, 0x20CuLL, 0);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func2(int a1, const void *a2)
{
ssize_t result; // rax

if ( a1 < 0 )
result = 0xFFFFFFFFLL;
else
result = send(a1, a2, 0x20CuLL, 0);
return result;
}

original_code:
static int write_socket(int isock_fd , cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return send(isock_fd , cspackage, sizeof(cspackage_t) , 0);
}

canonical_original_code:
static int write_socket(int isock_fd, struct cspackage_t *cspackage){
 if(isock_fd < 0 || !cspackage){
  return -1;
 }
 return send(isock_fd , cspackage, sizeof(cspackage_t) , 0);
}


decompiled_code:
void __fastcall handle_signal(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
write_log(a1, "receive SIGINT,ready to exit...");
if ( (int)close_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
write_log(3LL, "connect server: detach bus to connect failed!");
write_log(2LL, "connect server: detach bus to connect success!");
if ( (int)delete_mem_poll(poll) < 0 )
write_log(3LL, "connect server: delete_mem_poll failed!");
write_log(2LL, "connect server: delete_mem_poll success!");
if ( (int)detach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id) < 0 )
write_log(3LL, "connect server: detach runtime env connect failed!");
write_log(2LL, "connect server: detach runtime env success!");
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
if ( (_DWORD)a1 == 2 )
{
write_log(a1, "receive SIGINT,ready to exit...");
if ( (int)close_bus((unsigned int)connect_server_id, (unsigned int)logic_server_id) < 0 )
write_log(3LL, "connect server: detach bus to connect failed!");
write_log(2LL, "connect server: detach bus to connect success!");
if ( (int)delete_mem_poll(poll) < 0 )
write_log(3LL, "connect server: delete_mem_poll failed!");
write_log(2LL, "connect server: delete_mem_poll success!");
if ( (int)detach_shm_res(1LL, (unsigned int)world_id, (unsigned int)line_id) < 0 )
write_log(3LL, "connect server: detach runtime env connect failed!");
write_log(2LL, "connect server: detach runtime env success!");
exit(0);
}
}

original_code:
static void handle_signal(int sig_no){
 int iret;
 switch(sig_no){
  case 2:
   write_log(2 , "receive SIGINT,ready to exit...");
   iret = close_bus(connect_server_id , logic_server_id);
   if(iret < 0){
    write_log(3 , "connect server: detach bus to connect failed!");
   }
   write_log(2 , "connect server: detach bus to connect success!");
   iret = delete_mem_poll(poll);
   if(iret < 0){
    write_log(3 , "connect server: delete_mem_poll failed!");
   }
   write_log(2 , "connect server: delete_mem_poll success!");
   iret = detach_shm_res(0x00000001 , world_id , line_id);
   if(iret < 0){
    write_log(3 , "connect server: detach runtime env connect failed!");
   }
   write_log(2 , "connect server: detach runtime env success!");
   exit(0);
  break;
  default:
  break;
 }
}