sanitize

decompiled_code:
__int64 __fastcall sanitize(unsigned __int8 a1)
{
__int64 result; // rax

result = 2LL;
if ( (unsigned __int8)(a1 - 32) <= 0x5Eu )
{
if ( a1 > 0x3Eu )
result = (unsigned __int8)(a1 - 123) <= 2u;
else
result = ((1LL << a1) & 0x5000000500000401LL) != 0;
}
return result;
}

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

result = 2LL;
if ( (unsigned __int8)(a1 - 32) <= 0x5Eu )
{
if ( a1 > 0x3Eu )
result = (unsigned __int8)(a1 - 123) <= 2u;
else
result = ((1LL << a1) & 0x5000000500000401LL) != 0;
}
return result;
}

original_code:
int sanitize(unsigned char c) {
 if ((signed char) c < 32 || (signed char) c > 126)
  return 2;
 switch ((signed char) c){
  case 10:
  case '"':
  case '|':
  case ' ':
  case '<':
  case '>':
  case '{':
  case '}':
  case 0:
   return 1;
   break;
  default:
   return 0;
 }
}

canonical_original_code:
int sanitize(unsigned char c) {
 if ((char) c < 32 || (char) c > 126)
  return 2;
 switch ((char) c){
  case 10:
  case '"':
  case '|':
  case ' ':
  case '<':
  case '>':
  case '{':
  case '}':
  case 0:
   return 1;
   break;
  default:
   return 0;
 }
}

variable_types:
c
unsigned char


repo: ivokub/huffkub

decompiled_code:
void __fastcall print_tree(_BYTE *a1)
{
_BYTE *v1; // rbp
unsigned int v2; // er12
int v3; // eax
_BYTE *v4; // rbp
unsigned int v5; // er12
int v6; // eax

if ( *a1 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%p|%d\"]\n ", a1, a1, *((unsigned int *)a1 + 4));
v1 = (_BYTE *)*((_QWORD *)a1 + 3);
if ( *v1 == 1 )
{
v2 = (unsigned __int8)v1[20];
v3 = sanitize(v1[20]);
if ( v3 == 1 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
else if ( v3 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
else
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, v1);
}
else
{
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, *((const void **)a1 + 3));
}
print_tree(*((_QWORD *)a1 + 3));
v4 = (_BYTE *)*((_QWORD *)a1 + 4);
if ( *v4 == 1 )
{
v5 = (unsigned __int8)v4[20];
v6 = sanitize(v4[20]);
if ( v6 == 1 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
else if ( v6 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
else
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, v4);
}
else
{
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, *((const void **)a1 + 4));
}
print_tree(*((_QWORD *)a1 + 4));
}
}

canonical_decompiled_code:
void __fastcall func3(_BYTE *a1)
{
_BYTE *v1; // rbp
unsigned int v2; // er12
int v3; // eax
_BYTE *v4; // rbp
unsigned int v5; // er12
int v6; // eax

if ( *a1 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%p|%d\"]\n ", a1, a1, *((unsigned int *)a1 + 4));
v1 = (_BYTE *)*((_QWORD *)a1 + 3);
if ( *v1 == 1 )
{
v2 = (unsigned __int8)v1[20];
v3 = func2(v1[20]);
if ( v3 == 1 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
else if ( v3 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
else
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", v1, v2, *((unsigned int *)v1 + 4));
}
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, v1);
}
else
{
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, *((const void **)a1 + 3));
}
func3(*((_QWORD *)a1 + 3));
v4 = (_BYTE *)*((_QWORD *)a1 + 4);
if ( *v4 == 1 )
{
v5 = (unsigned __int8)v4[20];
v6 = func2(v4[20]);
if ( v6 == 1 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
else if ( v6 == 2 )
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
else
{
fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", v4, v5, *((unsigned int *)v4 + 4));
}
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, v4);
}
else
{
fprintf(stderr, "\t\"%p\" -> \"%p\";\n", a1, *((const void **)a1 + 4));
}
func3(*((_QWORD *)a1 + 4));
}
}

original_code:
void print_tree(node * rr) {
 leaf *l,*r;
 int printmode;
 if (rr->type == 2) {
  fprintf(stderr, "\t\"%p\" [shape=record,label=\"%p|%d\"]\n ", rr, rr, rr->freq);
  if ( (((hub *) rr)->left)->type == 1){
   l = ( (leaf *) ((hub *) rr)->left);
   if ((printmode = sanitize(l->ch)) == 1)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", l, l->ch, l->freq);
   else if (printmode ==2)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", l, (int) l->ch, l->freq);
   else
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", l, l->ch, l->freq);
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, l);
  } else {
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((hub *) rr)->left);
  }
  print_tree(((hub *) rr)->left);
  if ( (((hub *) rr)->right)->type == 1){
   r = ( (leaf *) ((hub *) rr)->right);
   if ((printmode = sanitize(r->ch)) == 1)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", r, r->ch, r->freq);
   else if (printmode == 2)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", r, (int) r->ch, r->freq);
   else
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", r, r->ch, r->freq);
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, r);
  } else {
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((hub *) rr)->right);
  }
  print_tree(((hub *) rr)->right);
 }
}

canonical_original_code:
void print_tree(struct node *rr) {
 struct nodeleaf *l; struct nodeleaf *r;
 int printmode;
 if (rr->type == 2) {
  fprintf(stderr, "\t\"%p\" [shape=record,label=\"%p|%d\"]\n ", rr, rr, rr->freq);
  if ( (((struct nodehub *) rr)->left)->type == 1){
   l = ( (struct nodeleaf *) ((struct nodehub *) rr)->left);
   if ((printmode = sanitize(l->ch)) == 1)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", l, l->ch, l->freq);
   else if (printmode ==2)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", l, (int) l->ch, l->freq);
   else
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", l, l->ch, l->freq);
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, l);
  } else {
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((struct nodehub *) rr)->left);
  }
  print_tree(((struct nodehub *) rr)->left);
  if ( (((struct nodehub *) rr)->right)->type == 1){
   r = ( (struct nodeleaf *) ((struct nodehub *) rr)->right);
   if ((printmode = sanitize(r->ch)) == 1)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\%c|%d\"]\n ", r, r->ch, r->freq);
   else if (printmode == 2)
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"\\\\%d|%d\"]\n ", r, (int) r->ch, r->freq);
   else
    fprintf(stderr, "\t\"%p\" [shape=record,label=\"%c|%d\"]\n ", r, r->ch, r->freq);
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, r);
  } else {
   fprintf(stderr, "\t\"%p\" -> \"%p\";\n", rr, ((struct nodehub *) rr)->right);
  }
  print_tree(((struct nodehub *) rr)->right);
 }
}

variable_types: struct nodeleaf { char type; struct nodehub { char type; struct nodehub *parent; int freq; struct node { char type; struct node *parent; int freq; } *left; struct node *right; } *parent; int freq; unsigned char ch; } *

variable_types: printmode int

return_type: void

user_defined_types: struct nodeleaf { char type; struct nodehub *parent; int freq; unsigned char ch; }

user_defined_types: struct nodehub { char type; struct nodehub *parent; int freq; struct node *left; struct node *right; }


decompiled_code:
size_t __fastcall print_tree1(_BYTE *a1)
{
fwrite("digraph bt {\n\trandkir = LR;\n", 1uLL, 0x1CuLL, stderr);
print_tree(a1);
return fwrite("}\n", 1uLL, 2uLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func4(_BYTE *a1)
{
fwrite("digraph bt {\n\trandkir = LR;\n", 1uLL, 0x1CuLL, stderr);
func3(a1);
return fwrite("}\n", 1uLL, 2uLL, stderr);
}

original_code:
void print_tree1(node * rr) {
 fprintf(stderr, "digraph bt {\n"
    "\trandkir = LR;\n");
 print_tree(rr);
 fprintf(stderr, "}\n");
}

canonical_original_code:
void print_tree1(struct node *rr) {
 fprintf(stderr, "digraph bt {\n"
    "\trandkir = LR;\n");
 print_tree(rr);
 fprintf(stderr, "}\n");
}

variable_types: struct node { char type; struct node *parent; int freq; } *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
puts("Use -h for usage");
exit(0);
}
blksize = 1024;
parse_args(argc, (char **)argv);
if ( (opts & 1) != 0 )
compress();
if ( (opts & 2) != 0 )
extract();
if ( (opts & 0x40) != 0 )
{
print_tree1((_BYTE *)tip);
verbose_print();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
puts("Use -h for usage");
exit(0);
}
blksize = 1024;
func0(argc, (char **)argv);
if ( (opts & 1) != 0 )
compress();
if ( (opts & 2) != 0 )
extract();
if ( (opts & 0x40) != 0 )
{
func4((_BYTE *)tip);
func1();
}
return 0;
}

original_code:
int main(int argc, char **argv) {
 if (argc < 2) {
  printf("Use -h for usage\n");
  exit(0);
 }
 blksize = 1024;
 parse_args(argc, argv);
 if (opts & 0x1) {
  compress();
 }
 if (opts & 0x2) {
  extract();
 }
 if (opts & 0x40) {
  print_tree1((node* ) tip);
  verbose_print();
 }
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 if (argc < 2) {
  printf("Use -h for usage\n");
  exit(0);
 }
 blksize = 1024;
 parse_args(argc, argv);
 if (opts & 0x1) {
  compress();
 }
 if (opts & 0x2) {
  extract();
 }
 if (opts & 0x40) {
  print_tree1((struct node * ) tip);
  verbose_print();
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

return_type: int

user_defined_types: struct node { char type; struct node *parent; int freq; }


decompiled_code:
__int64 __fastcall node_cmp_ll(const void *a1, const void *a2)
{
int v2; // ecx
int v3; // edx
__int64 result; // rax

if ( !*(_QWORD *)a1 )
return 1LL;
if ( !*(_QWORD *)a2 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(*(_QWORD *)a1 + 16LL);
v3 = *(_DWORD *)(*(_QWORD *)a2 + 16LL);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v2 < v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
int v2; // ecx
int v3; // edx
__int64 result; // rax

if ( !*(_QWORD *)a1 )
return 1LL;
if ( !*(_QWORD *)a2 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(*(_QWORD *)a1 + 16LL);
v3 = *(_DWORD *)(*(_QWORD *)a2 + 16LL);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v2 < v3);
return result;
}

original_code:
int node_cmp_ll(const void *c1, const void *c2) {
 if (*(void **) c1 == ((void *)0)) return 1;
 if (*(void **) c2 == ((void *)0)) return -1;
 if ((*(node **) c1)->freq > (*(node **) c2)->freq)
  return 1;
 else if ((*(node **) c1)->freq < (*(node **) c2)->freq)
  return -1;
 else
  return 0;
}

canonical_original_code:
int node_cmp_ll(void *c1, void *c2) {
 if (*(void **) c1 == ((void *)0)) return 1;
 if (*(void **) c2 == ((void *)0)) return -1;
 if ((*(struct node **) c1)->freq > (*(struct node **) c2)->freq)
  return 1;
 else if ((*(struct node **) c1)->freq < (*(struct node **) c2)->freq)
  return -1;
 else
  return 0;
}


decompiled_code:
__int64 __fastcall create_tree(const void *a1)
{
_QWORD *v1; // rbx
__int64 i; // rsi
_QWORD *v3; // rax

v1 = calloc(0x100uLL, 8uLL);
qmemcpy(v1, a1, 0x800uLL);
qsort(v1, 0x100uLL, 8uLL, (__compar_fn_t)node_cmp_ll);
for ( i = v1[1]; i; i = v1[1] )
{
v3 = create_hub(*v1, i);
*(_QWORD *)(*v1 + 8LL) = v3;
*(_QWORD *)(v1[1] + 8LL) = v3;
*v1 = 0LL;
v1[1] = v3;
qsort(v1, 0x100uLL, 8uLL, (__compar_fn_t)node_cmp_ll);
}
return *v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1)
{
_QWORD *v1; // rbx
__int64 i; // rsi
_QWORD *v3; // rax

v1 = calloc(0x100uLL, 8uLL);
qmemcpy(v1, a1, 0x800uLL);
qsort(v1, 0x100uLL, 8uLL, (__compar_fn_t)node_cmp_ll);
for ( i = v1[1]; i; i = v1[1] )
{
v3 = create_hub(*v1, i);
*(_QWORD *)(*v1 + 8LL) = v3;
*(_QWORD *)(v1[1] + 8LL) = v3;
*v1 = 0LL;
v1[1] = v3;
qsort(v1, 0x100uLL, 8uLL, (__compar_fn_t)node_cmp_ll);
}
return *v1;
}

original_code:
hub * create_tree(leaf ** leafarray) {
 int i;
 int full = 0;
 node ** tree = calloc(256, sizeof(node *));
 hub * thishub;
 memcpy(tree, leafarray, sizeof(void *) * 256);
 qsort(tree, 256, sizeof(node *), node_cmp_ll);
 while (tree[1] != ((void *)0)) {
  thishub = create_hub((leaf *) tree[0], (leaf *) tree[1]);
  tree[0]->parent = (node *) thishub;
  tree[1]->parent = (node *) thishub;
  tree[0] = ((void *)0);
  tree[1] = (node *) thishub;
  qsort(tree, 256, sizeof(node *), node_cmp_ll);
 }
 return (hub *) tree[0];
}

canonical_original_code:
struct nodehub *create_tree(struct nodeleaf **leafarray) {
 int i;
 int full = 0;
 struct node **tree = calloc(256, sizeof(node *));
 struct nodehub *thishub;
 memcpy(tree, leafarray, sizeof(void *) * 256);
 qsort(tree, 256, sizeof(struct node *), node_cmp_ll);
 while (tree[1] != ((void *)0)) {
  thishub = create_hub((struct nodeleaf *) tree[0], (struct nodeleaf *) tree[1]);
  tree[0]->parent = (struct node *) thishub;
  tree[1]->parent = (struct node *) thishub;
  tree[0] = ((void *)0);
  tree[1] = (struct node *) thishub;
  qsort(tree, 256, sizeof(struct node *), node_cmp_ll);
 }
 return (struct nodehub *) tree[0];
}

variable_types: struct nodeleaf { char type; struct nodehub *parent; int freq; unsigned char ch; } **

variable_types: i int

variable_types: full int

variable_types: struct node { char type; struct node *parent; int freq; } **

variable_types: struct nodehub { char type; struct nodehub *parent; int freq; struct node { char type; struct node *parent; int freq; } *left; struct node *right; } *


decompiled_code:
void __fastcall fill_node(unsigned __int8 a1)
{
_QWORD *v1; // rbx

v1 = (_QWORD *)chararray[a1];
if ( !v1 )
{
v1 = malloc(0x18uLL);
if ( !v1 )
error(4, 0, "Cannot allocate memory!");
*(_BYTE *)v1 = 1;
v1[1] = root;
chararray[a1] = (__int64)v1;
++leafcount;
}
*((_BYTE *)v1 + 20) = a1;
++*((_DWORD *)v1 + 4);
++root[4];
}

canonical_decompiled_code:
void __fastcall func2(unsigned __int8 a1)
{
_QWORD *v1; // rbx

v1 = (_QWORD *)chararray[a1];
if ( !v1 )
{
v1 = malloc(0x18uLL);
if ( !v1 )
error(4, 0, "Cannot allocate memory!");
*(_BYTE *)v1 = 1;
v1[1] = root;
chararray[a1] = (__int64)v1;
++leafcount;
}
*((_BYTE *)v1 + 20) = a1;
++*((_DWORD *)v1 + 4);
++root[4];
}

original_code:
void fill_node(unsigned char c) {
 leaf *thisnode;
 if ((thisnode = chararray[c]) == ((void *)0)) {
  thisnode = (leaf *) malloc(sizeof(leaf));
  if (thisnode == ((void *)0))
   error(4, 0, "Cannot allocate memory!");
  thisnode->type = 1;
                thisnode->parent = &root;
  chararray[c] = thisnode;
  leafcount++;
 }
 thisnode->ch = c;
 thisnode->freq++;
 root.freq++;
}

canonical_original_code:
void fill_node(unsigned char c) {
 struct nodeleaf *thisnode;
 if ((thisnode = chararray[c]) == ((void *)0)) {
  thisnode = (struct nodeleaf *) malloc(sizeof(leaf));
  if (thisnode == ((void *)0))
   error(4, 0, "Cannot allocate memory!");
  thisnode->type = 1;
                thisnode->parent = &root;
  chararray[c] = thisnode;
  leafcount++;
 }
 thisnode->ch = c;
 thisnode->freq++;
 root.freq++;
}


decompiled_code:
int __fastcall print_code(int *a1)
{
int v1; // ebx
int v2; // ebp

v1 = a1[1];
if ( *a1 > 0 )
{
v2 = 0;
do
{
fprintf(stderr, "%d", (unsigned int)(v1 % 2));
v1 /= 2;
++v2;
}
while ( *a1 > v2 );
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func3(int *a1)
{
int v1; // ebx
int v2; // ebp

v1 = a1[1];
if ( *a1 > 0 )
{
v2 = 0;
do
{
fprintf(stderr, "%d", (unsigned int)(v1 % 2));
v1 /= 2;
++v2;
}
while ( *a1 > v2 );
}
return fputc(10, stderr);
}

original_code:
void print_code(code * bytecode) {
 int i = 0, bitcode = bytecode->ch;
 while (i < bytecode->len) {
  fprintf(stderr, "%d", bitcode % 2);
  bitcode /= 2;
  i++;
 }
 fprintf(stderr, "\n");
}

canonical_original_code:
void print_code(struct code *bytecode) {
 int i = 0; int bitcode = bytecode->ch;
 while (i < bytecode->len) {
  fprintf(stderr, "%d", bitcode % 2);
  bitcode /= 2;
  i++;
 }
 fprintf(stderr, "\n");
}

variable_types:
bytecode
struct code { int len; int ch; } *

variable_types: bitcode int


decompiled_code:
void __fastcall recursive_write(__int64 a1)
{
_BYTE *v1; // rax
_BYTE *v2; // rax

v1 = *(_BYTE **)(a1 + 24);
if ( *v1 == 1 )
{
if ( (opts & 0x40) != 0 )
fprintf(stderr, "1%c", (unsigned int)(char)v1[20]);
if ( (unsigned int)writebit(1LL) || (unsigned int)write_char_bit(*(unsigned __int8 *)(*(_QWORD *)(a1 + 24) + 20LL)) )
error(3, 0, "Write error!");
}
else
{
if ( (opts & 0x40) != 0 )
fputc(48, stderr);
writebit(0LL);
recursive_write(*(_QWORD *)(a1 + 24));
}
v2 = *(_BYTE **)(a1 + 32);
if ( *v2 == 1 )
{
if ( (opts & 0x40) != 0 )
fprintf(stderr, "1%c", (unsigned int)(char)v2[20]);
if ( (unsigned int)writebit(1LL) || (unsigned int)write_char_bit(*(unsigned __int8 *)(*(_QWORD *)(a1 + 32) + 20LL)) )
error(3, 0, "Write error!");
}
else
{
if ( (opts & 0x40) != 0 )
fputc(48, stderr);
writebit(0LL);
recursive_write(*(_QWORD *)(a1 + 32));
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
_BYTE *v1; // rax
_BYTE *v2; // rax

v1 = *(_BYTE **)(a1 + 24);
if ( *v1 == 1 )
{
if ( (opts & 0x40) != 0 )
fprintf(stderr, "1%c", (unsigned int)(char)v1[20]);
if ( (unsigned int)writebit(1LL) || (unsigned int)write_char_bit(*(unsigned __int8 *)(*(_QWORD *)(a1 + 24) + 20LL)) )
error(3, 0, "Write error!");
}
else
{
if ( (opts & 0x40) != 0 )
fputc(48, stderr);
writebit(0LL);
func8(*(_QWORD *)(a1 + 24));
}
v2 = *(_BYTE **)(a1 + 32);
if ( *v2 == 1 )
{
if ( (opts & 0x40) != 0 )
fprintf(stderr, "1%c", (unsigned int)(char)v2[20]);
if ( (unsigned int)writebit(1LL) || (unsigned int)write_char_bit(*(unsigned __int8 *)(*(_QWORD *)(a1 + 32) + 20LL)) )
error(3, 0, "Write error!");
}
else
{
if ( (opts & 0x40) != 0 )
fputc(48, stderr);
writebit(0LL);
func8(*(_QWORD *)(a1 + 32));
}
}

original_code:
void recursive_write(hub * parent){
 if (parent->left->type == 1){
  if (opts & 0x40) fprintf(stderr, "1%c", (signed char) ((leaf *) parent->left)->ch);
  if (writebit(1) || write_char_bit(((leaf *) parent->left)->ch)) error(3, 0, "Write error!");
 } else {
  if (opts & 0x40) fprintf(stderr, "0");
  writebit(0);
  recursive_write((hub *) parent->left);
 }
 if (parent->right->type == 1){
  if (opts & 0x40) fprintf(stderr, "1%c", (signed char) ((leaf *) parent->right)->ch);
  if (writebit(1) || write_char_bit(((leaf *) parent->right)->ch)) error(3, 0, "Write error!");
 } else {
  if (opts & 0x40) fprintf(stderr, "0");
  writebit(0);
  recursive_write((hub *) parent->right);
 }
}

canonical_original_code:
void recursive_write(struct nodehub *parent){
 if (parent->left->type == 1){
  if (opts & 0x40) fprintf(stderr, "1%c", (char) ((struct nodeleaf *) parent->left)->ch);
  if (writebit(1) || write_char_bit(((struct nodeleaf *) parent->left)->ch)) error(3, 0, "Write error!");
 } else {
  if (opts & 0x40) fprintf(stderr, "0");
  writebit(0);
  recursive_write((struct nodehub *) parent->left);
 }
 if (parent->right->type == 1){
  if (opts & 0x40) fprintf(stderr, "1%c", (char) ((struct nodeleaf *) parent->right)->ch);
  if (writebit(1) || write_char_bit(((struct nodeleaf *) parent->right)->ch)) error(3, 0, "Write error!");
 } else {
  if (opts & 0x40) fprintf(stderr, "0");
  writebit(0);
  recursive_write((struct nodehub *) parent->right);
 }
}


readbit

decompiled_code:
__int64 readbit()
{
char v0; // al
char v2; // bl
__int64 i; // rcx

if ( bitindex == 8 )
{
if ( eof_reached )
goto LABEL_5;
v2 = fgetc(fbitstream);
if ( feof(fbitstream) )
eof_reached = 1;
*(_QWORD *)bitbuffer = *(_QWORD *)bitbuffer2;
if ( !eof_reached )
{
for ( i = 0LL; i != 8; ++i )
*(_BYTE *)(bitbuffer2 + i) = (v2 & (1 << i)) >> i;
}
bitindex = 0;
}
if ( !eof_reached )
{
LABEL_3:
v0 = bitindex++;
return *(unsigned __int8 *)(bitbuffer + 7 - v0);
}
LABEL_5:
if ( bitindex + padsize != 8 )
goto LABEL_3;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
char v0; // al
char v2; // bl
__int64 i; // rcx

if ( bitindex == 8 )
{
if ( eof_reached )
goto LABEL_5;
v2 = fgetc(fbitstream);
if ( feof(fbitstream) )
eof_reached = 1;
*(_QWORD *)bitbuffer = *(_QWORD *)bitbuffer2;
if ( !eof_reached )
{
for ( i = 0LL; i != 8; ++i )
*(_BYTE *)(bitbuffer2 + i) = (v2 & (1 << i)) >> i;
}
bitindex = 0;
}
if ( !eof_reached )
{
LABEL_3:
v0 = bitindex++;
return *(unsigned __int8 *)(bitbuffer + 7 - v0);
}
LABEL_5:
if ( bitindex + padsize != 8 )
goto LABEL_3;
return 0xFFFFFFFFLL;
}

original_code:
char readbit(){
 char c;
 int i;
 if (bitindex == 8 && !eof_reached){
  c = fgetc(fbitstream);
  if (feof(fbitstream)) {
   eof_reached = 1;
  }
  memcpy(bitbuffer, bitbuffer2, 8*sizeof(char));
  if (!eof_reached){
   for (i=0; i<8; i++){
    bitbuffer2[i] = (c & (0x1 << i)) >> i;
    }
  }
  bitindex = 0;
 }
 if (eof_reached && padsize+bitindex == 8) return -1;
 return bitbuffer[7-bitindex++];
}

variable_types: c char

return_type: char


writebit

decompiled_code:
__int64 __fastcall writebit(char a1)
{
char v1; // dl
__int64 result; // rax
int v3; // edx

v1 = bitindex++;
*(_BYTE *)bitbuffer |= (a1 & 1) << (7 - v1);
result = 0LL;
if ( bitindex == 8 )
{
v3 = fputc(*(char *)bitbuffer, fbitstream);
result = 1LL;
if ( v3 != -1 )
{
*(_BYTE *)bitbuffer = 0;
bitindex = 0;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
char v1; // dl
__int64 result; // rax
int v3; // edx

v1 = bitindex++;
*(_BYTE *)bitbuffer |= (a1 & 1) << (7 - v1);
result = 0LL;
if ( bitindex == 8 )
{
v3 = fputc(*(char *)bitbuffer, fbitstream);
result = 1LL;
if ( v3 != -1 )
{
*(_BYTE *)bitbuffer = 0;
bitindex = 0;
result = 0LL;
}
}
return result;
}

original_code:
int writebit(char c){
 int ret;
 *bitbuffer |= (0x1&c)<<(7-bitindex++);
 if (bitindex == 8){
  if ((ret = fputc(*bitbuffer, fbitstream)) == (-1)) return 1;
  *bitbuffer = 0;
  bitindex = 0;
 }
 return 0;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall write_char_bit(unsigned __int8 a1)
{
int v2; // ebp
__int64 result; // rax

v2 = 8;
while ( 1 )
{
result = writebit(a1 & 1);
if ( (_DWORD)result )
break;
a1 >>= 1;
if ( !--v2 )
return result;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1)
{
int v2; // ebp
__int64 result; // rax

v2 = 8;
while ( 1 )
{
result = func2(a1 & 1);
if ( (_DWORD)result )
break;
a1 >>= 1;
if ( !--v2 )
return result;
}
return 1LL;
}

original_code:
int write_char_bit(unsigned char c){
 int i;
 for (i=0; i<8; i++){
  if (writebit(c & 0x1)) return 1;
  c >>= 1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall cmp_compresscode(const void *a1, const void *a2)
{
char v2; // cl
char v3; // dl
__int64 result; // rax

v2 = *((_BYTE *)a1 + 2);
v3 = *((_BYTE *)a2 + 2);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v2 < v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
char v2; // cl
char v3; // dl
__int64 result; // rax

v2 = *((_BYTE *)a1 + 2);
v3 = *((_BYTE *)a2 + 2);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v2 < v3);
return result;
}

original_code:
int cmp_compresscode(const void * c1, const void * c2){
 if (((compresscode *) c1)->len > ((compresscode *) c2)->len) return 1;
 else if (((compresscode *) c1)-> len < ((compresscode *) c2)->len) return -1;
 else return 0;
}

canonical_original_code:
int cmp_compresscode(void *c1, void *c2){
 if (((struct compresscode *) c1)->len > ((struct compresscode *) c2)->len) return 1;
 else if (((struct compresscode *) c1)-> len < ((struct compresscode *) c2)->len) return -1;
 else return 0;
}

user_defined_types: struct compresscode { short code; char len; char ch; }


reverse

original_code:
void reverse(compresscode * ctable, int arraylen){
 int i, j;
 short int code = 0;
 for (i = 0; i < arraylen; i++){
  for (j = 0; j < ctable[i].len; j++){
   code <<= 1;
   code += ctable[i].code & 0x1;
   ctable[i].code >>= 1;
  }
  ctable[i].code = code;
  code = 0;
 }
}

canonical_original_code:
void reverse(struct compresscode *ctable, int arraylen){
 int i; int j;
 short code = 0;
 for (i = 0; i < arraylen; i++){
  for (j = 0; j < ctable[i].len; j++){
   code <<= 1;
   code += ctable[i].code & 0x1;
   ctable[i].code >>= 1;
  }
  ctable[i].code = code;
  code = 0;
 }
}

variable_types: struct compresscode { short code; char len; char ch; } *

variable_types: arraylen int

variable_types: j int

variable_types: code short


decompiled_code:
__int64 __fastcall recursive_tree(__int64 a1, int a2)
{
__int64 result; // rax
_QWORD *v3; // rax
_QWORD *v4; // rbx
_QWORD *v5; // rbx
int v6; // ebp
_QWORD *v7; // rbx
int v8; // ebp
__int64 v9; // rax

result = readbit();
if ( a2 )
{
if ( a2 != 1 )
{
if ( a2 == -1 )
{
v3 = malloc(0x28uLL);
tip2 = (__int64)v3;
v3[1] = &root;
recursive_tree(v3, 0LL);
result = recursive_tree(tip2, 1LL);
}
return result;
}
if ( (_BYTE)result != 1 )
{
v7 = malloc(0x28uLL);
*(_BYTE *)v7 = 2;
v7[1] = a1;
*(_QWORD *)(a1 + 32) = v7;
recursive_tree(v7, 0LL);
return recursive_tree(v7, 1LL);
}
v5 = malloc(0x18uLL);
v5[1] = a1;
*(_BYTE *)v5 = 1;
*(_QWORD *)(a1 + 32) = v5;
*((_BYTE *)v5 + 20) = 0;
v8 = 8;
do
{
result = (*((_BYTE *)v5 + 20) >> 1) | ((unsigned int)readbit() << 7);
*((_BYTE *)v5 + 20) = result;
--v8;
}
while ( v8 );
if ( (opts & 0x60) == 96 )
{
LABEL_17:
printf("%c\t", (unsigned __int8)result);
v9 = calc_code(v5);
return print_code(v9);
}
}
else
{
if ( (_BYTE)result != 1 )
{
v4 = malloc(0x28uLL);
v4[1] = a1;
*(_QWORD *)(a1 + 24) = v4;
*(_BYTE *)v4 = 2;
recursive_tree(v4, 0LL);
return recursive_tree(v4, 1LL);
}
v5 = malloc(0x18uLL);
v5[1] = a1;
*(_BYTE *)v5 = 1;
*(_QWORD *)(a1 + 24) = v5;
*((_BYTE *)v5 + 20) = 0;
v6 = 8;
do
{
result = (*((_BYTE *)v5 + 20) >> 1) | ((unsigned int)readbit() << 7);
*((_BYTE *)v5 + 20) = result;
--v6;
}
while ( v6 );
if ( (opts & 0x60) == 96 )
goto LABEL_17;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 result; // rax
_QWORD *v3; // rax
_QWORD *v4; // rbx
_QWORD *v5; // rbx
int v6; // ebp
_QWORD *v7; // rbx
int v8; // ebp
__int64 v9; // rax

result = readbit();
if ( a2 )
{
if ( a2 != 1 )
{
if ( a2 == -1 )
{
v3 = malloc(0x28uLL);
tip2 = (__int64)v3;
v3[1] = &root;
func2(v3, 0LL);
result = func2(tip2, 1LL);
}
return result;
}
if ( (_BYTE)result != 1 )
{
v7 = malloc(0x28uLL);
*(_BYTE *)v7 = 2;
v7[1] = a1;
*(_QWORD *)(a1 + 32) = v7;
func2(v7, 0LL);
return func2(v7, 1LL);
}
v5 = malloc(0x18uLL);
v5[1] = a1;
*(_BYTE *)v5 = 1;
*(_QWORD *)(a1 + 32) = v5;
*((_BYTE *)v5 + 20) = 0;
v8 = 8;
do
{
result = (*((_BYTE *)v5 + 20) >> 1) | ((unsigned int)readbit() << 7);
*((_BYTE *)v5 + 20) = result;
--v8;
}
while ( v8 );
if ( (opts & 0x60) == 96 )
{
LABEL_17:
printf("%c\t", (unsigned __int8)result);
v9 = calc_code(v5);
return print_code(v9);
}
}
else
{
if ( (_BYTE)result != 1 )
{
v4 = malloc(0x28uLL);
v4[1] = a1;
*(_QWORD *)(a1 + 24) = v4;
*(_BYTE *)v4 = 2;
func2(v4, 0LL);
return func2(v4, 1LL);
}
v5 = malloc(0x18uLL);
v5[1] = a1;
*(_BYTE *)v5 = 1;
*(_QWORD *)(a1 + 24) = v5;
*((_BYTE *)v5 + 20) = 0;
v6 = 8;
do
{
result = (*((_BYTE *)v5 + 20) >> 1) | ((unsigned int)readbit() << 7);
*((_BYTE *)v5 + 20) = result;
--v6;
}
while ( v6 );
if ( (opts & 0x60) == 96 )
goto LABEL_17;
}
return result;
}

original_code:
void recursive_tree(hub * parent, int direction){
 unsigned char c = readbit();
 leaf * thisleaf;
 hub * thishub;
 int i;
 if (c == -1) error(3, 0, "Read error!");
 switch (direction){
  case -1:
   tip2 = malloc(sizeof(hub));
   tip2->parent = &root;
   recursive_tree(tip2, 0);
   recursive_tree(tip2, 1);
   break;
  case 0:
   if (c == 1){
    thisleaf = malloc(sizeof(leaf));
    thisleaf->parent = parent;
    thisleaf->type = 1;
    parent->left = (node *) thisleaf;
    thisleaf->ch = 0;
    for (i=0; i<8; i++){
     if ((c = readbit()) == -1) error(3, 0, "Read error!");
     thisleaf->ch >>= 1;
     thisleaf->ch |= c<<7;
    }
    if ((opts & 0x40) && (opts & 0x20)) {
     printf("%c\t", thisleaf->ch);
     print_code(calc_code(thisleaf));
    }
   } else {
    thishub = malloc(sizeof(hub));
    thishub->parent = parent;
    parent->left = (node *) thishub;
    thishub->type = 2;
    recursive_tree(thishub, 0);
    recursive_tree(thishub, 1);
   }
   break;
  case 1:
   if (c == 1){
    thisleaf = malloc(sizeof(leaf));
    thisleaf->parent = parent;
    thisleaf->type = 1;
    parent->right = (node *) thisleaf;
    thisleaf->ch = 0;
    for (i=0; i<8; i++){
     if ((c = readbit()) == -1) error(3, 0, "Read error!");
     thisleaf->ch >>= 1;
     thisleaf->ch |= c<<7;
    }
    if ((opts & 0x40) && (opts & 0x20)) {
     printf("%c\t", thisleaf->ch);
     print_code(calc_code(thisleaf));
    }
   } else {
    thishub = malloc(sizeof(hub));
    thishub->type = 2;
    thishub->parent = parent;
    parent->right = (node *) thishub;
    recursive_tree(thishub, 0);
    recursive_tree(thishub, 1);
   }
   break;
  default:
   break;
 }
}

canonical_original_code:
void recursive_tree(struct nodehub *parent, int direction){
 unsigned char c = readbit();
 struct nodeleaf *thisleaf;
 struct nodehub *thishub;
 int i;
 if (c == -1) error(3, 0, "Read error!");
 switch (direction){
  case -1:
   tip2 = malloc(sizeof(hub));
   tip2->parent = &root;
   recursive_tree(tip2, 0);
   recursive_tree(tip2, 1);
   break;
  case 0:
   if (c == 1){
    thisleaf = malloc(sizeof(leaf));
    thisleaf->parent = parent;
    thisleaf->type = 1;
    parent->left = (struct node *) thisleaf;
    thisleaf->ch = 0;
    for (i=0; i<8; i++){
     if ((c = readbit()) == -1) error(3, 0, "Read error!");
     thisleaf->ch >>= 1;
     thisleaf->ch |= c<<7;
    }
    if ((opts & 0x40) && (opts & 0x20)) {
     printf("%c\t", thisleaf->ch);
     print_code(calc_code(thisleaf));
    }
   } else {
    thishub = malloc(sizeof(hub));
    thishub->parent = parent;
    parent->left = (struct node *) thishub;
    thishub->type = 2;
    recursive_tree(thishub, 0);
    recursive_tree(thishub, 1);
   }
   break;
  case 1:
   if (c == 1){
    thisleaf = malloc(sizeof(leaf));
    thisleaf->parent = parent;
    thisleaf->type = 1;
    parent->right = (struct node *) thisleaf;
    thisleaf->ch = 0;
    for (i=0; i<8; i++){
     if ((c = readbit()) == -1) error(3, 0, "Read error!");
     thisleaf->ch >>= 1;
     thisleaf->ch |= c<<7;
    }
    if ((opts & 0x40) && (opts & 0x20)) {
     printf("%c\t", thisleaf->ch);
     print_code(calc_code(thisleaf));
    }
   } else {
    thishub = malloc(sizeof(hub));
    thishub->type = 2;
    thishub->parent = parent;
    parent->right = (struct node *) thishub;
    recursive_tree(thishub, 0);
    recursive_tree(thishub, 1);
   }
   break;
  default:
   break;
 }
}

variable_types: direction int

variable_types:
c
unsigned char

variable_types: struct nodeleaf { char type; struct nodehub { char type; struct nodehub *parent; int freq; struct node { char type; struct node *parent; int freq; } *left; struct node *right; } *parent; int freq; unsigned char ch; } *


decompiled_code:
__int64 __fastcall sort_compresstable(char *a1, int a2)
{
__int64 result; // rax

qsort(a1, a2, 4uLL, (__compar_fn_t)cmp_compresscode);
maxlen = a1[4 * a2 - 2];
result = (unsigned int)a1[2];
minlen = a1[2];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, int a2)
{
__int64 result; // rax

qsort(a1, a2, 4uLL, (__compar_fn_t)cmp_compresscode);
maxlen = a1[4 * a2 - 2];
result = (unsigned int)a1[2];
minlen = a1[2];
return result;
}

original_code:
void sort_compresstable(compresscode ctable[], int len){
 qsort(ctable, len, sizeof(compresscode), cmp_compresscode);
 maxlen = ctable[len-1].len;
 minlen = ctable[0].len;
}

canonical_original_code:
void sort_compresstable(struct compresscode ctable[], int len){
 qsort(ctable, len, sizeof(compresscode), cmp_compresscode);
 maxlen = ctable[len-1].len;
 minlen = ctable[0].len;
}

variable_types: struct compresscode { short code; char len; char ch; } []

variable_types: len int


decompiled_code:
__int64 __fastcall find_char(__int64 a1, int a2, int a3)
{
__int64 result; // rax
int v6; // esi
__int64 v7; // rax
int v8; // edx
__int64 v9; // rdx
__int16 *i; // rax
int v11; // esi

result = 0LL;
if ( minlen <= a3 )
{
v6 = searchpointer;
if ( a3 > *(char *)(a1 + 4LL * searchpointer + 2) )
{
v7 = searchpointer + 1;
do
{
v6 = v7;
v8 = *(char *)(a1 + 4 * v7++ + 2);
}
while ( v8 < a3 );
}
v9 = a1 + 4LL * v6;
if ( a3 == *(char *)(v9 + 2) )
{
for ( i = (__int16 *)(a1 + 4LL * v6); *i != a2; i += 2 )
{
v11 = *(char *)(v9 + 6);
v9 += 4LL;
if ( v11 != a3 )
return 0LL;
}
result = *((unsigned __int8 *)i + 3);
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, int a3)
{
__int64 result; // rax
int v6; // esi
__int64 v7; // rax
int v8; // edx
__int64 v9; // rdx
__int16 *i; // rax
int v11; // esi

result = 0LL;
if ( minlen <= a3 )
{
v6 = searchpointer;
if ( a3 > *(char *)(a1 + 4LL * searchpointer + 2) )
{
v7 = searchpointer + 1;
do
{
v6 = v7;
v8 = *(char *)(a1 + 4 * v7++ + 2);
}
while ( v8 < a3 );
}
v9 = a1 + 4LL * v6;
if ( a3 == *(char *)(v9 + 2) )
{
for ( i = (__int16 *)(a1 + 4LL * v6); *i != a2; i += 2 )
{
v11 = *(char *)(v9 + 6);
v9 += 4LL;
if ( v11 != a3 )
return 0LL;
}
result = *((unsigned __int8 *)i + 3);
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
char find_char(compresscode * ctable, int code, int len){
 int i = searchpointer;
 if (len>=minlen){
  while (ctable[i].len<len) i++;
  for (; ctable[i].len == len; i++){
   if (ctable[i].code == code) return ctable[i].ch;
  }
 }
 return 0;
}

canonical_original_code:
char find_char(struct compresscode *ctable, int code, int len){
 int i = searchpointer;
 if (len>=minlen){
  while (ctable[i].len<len) i++;
  for (; ctable[i].len == len; i++){
   if (ctable[i].code == code) return ctable[i].ch;
  }
 }
 return 0;
}

variable_types: code int

variable_types: i int


getsource

repo: iwabuchiken/AI

decompiled_code:
__int64 __fastcall getsource(__int64 a1)
{
__int64 v1; // rbx
char v2; // al
unsigned int v3; // edx

v1 = 1LL;
do
{
v2 = getchar();
v3 = v1;
*(_BYTE *)(a1 + v1++ - 1) = v2;
}
while ( v2 != -1 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rbx
char v2; // al
unsigned int v3; // edx

v1 = 1LL;
do
{
v2 = getchar();
v3 = v1;
*(_BYTE *)(a1 + v1++ - 1) = v2;
}
while ( v2 != -1 );
return v3;
}

original_code:
int getsource(char *s)
{
 int n=0;
 while((s[n++] = getchar()) != (-1));
 return n;
}

canonical_original_code:
int getsource(char *s)
{
 int n = 0;
 while((s[n++] = getchar()) != (-1));
 return n;
}

variable_types:
s
char *

variable_types: n int


decompiled_code:
int show_shift_jis_char()
{
__int64 v1; // [rsp+6h] [rbp-12h] BYREF
__int16 v2; // [rsp+Eh] [rbp-Ah]

v1 = 0x81E38481E38281E3LL;
v2 = 134;
printf("c=%s\n", (const char *)&v1);
printf("c=%c\n", (unsigned int)(char)v1);
printf("c=%d\n", (unsigned __int8)v1);
return printf("c=%x\n", (unsigned __int8)v1);
}

canonical_decompiled_code:
int func11()
{
__int64 v1; // [rsp+6h] [rbp-12h] BYREF
__int16 v2; // [rsp+Eh] [rbp-Ah]

v1 = 0x81E38481E38281E3LL;
v2 = 134;
printf("c=%s\n", (const char *)&v1);
printf("c=%c\n", (unsigned int)(char)v1);
printf("c=%d\n", (unsigned __int8)v1);
return printf("c=%x\n", (unsigned __int8)v1);
}

original_code:
void show_shift_jis_char()
{
 int i, j;
 int num;
 char c[10] = "あいう";
 printf("c=%s\n", c);
 printf("c=%c\n", c[0]);
 printf("c=%d\n", (unsigned char)c[0]);
 printf("c=%x\n", (unsigned char)c[0]);
}

canonical_original_code:
void show_shift_jis_char()
{
 int i; int j;
 int num;
 char c[10] = "あいう";
 printf("c=%s\n", c);
 printf("c=%c\n", c[0]);
 printf("c=%d\n", (unsigned char)c[0]);
 printf("c=%x\n", (unsigned char)c[0]);
}

variable_types: num int

variable_types:
c
char [10]


decompiled_code:
int show_shift_jis_wchar()
{
char v1[40]; // [rsp+0h] [rbp-28h] BYREF

setlocale(0, "jpn");
v1[wctomb(v1, 12354)] = 0;
return printf("%s(%d, %%x=%x, (unsigned char)wc=%c wc(%%d)=%d wc(%%x)=%x\n", v1, v1, v1, 66LL, 12354LL, 12354LL);
}

original_code:
void show_shift_jis_wchar()
{
 int i, j;
 int num;
 num = 0;
 char mb[16 + 1];
  wchar_t wc = L'あ';
  int len;
  setlocale( 0, "jpn" );
  len = wctomb( mb, wc );
  mb[len] = '\0';
  printf( "%s(%d, %%x=%x, (unsigned char)wc=%c wc(%%d)=%d wc(%%x)=%x\n",
    mb, mb, mb, (unsigned char)wc, wc, wc);
}

canonical_original_code:
void show_shift_jis_wchar()
{
 int i; int j;
 int num;
 num = 0;
 char mb[-1];
  int wc = L'あ';
  int len;
  setlocale( 0, "jpn" );
  len = wctomb( mb, wc );
  mb[len] = '\0';
  printf( "%s(%d, %%x=%x, (unsigned char)wc=%c wc(%%d)=%d wc(%%x)=%x\n",
    mb, mb, mb, (unsigned char)wc, wc, wc);
}

variable_types:
mb
char [-1]

variable_types: wc int


decompiled_code:
int show_shift_jis()
{
return show_shift_jis_char();
}

canonical_decompiled_code:
int func13()
{
return func11();
}

original_code:
void show_shift_jis()
{
 show_shift_jis_char();
}


repo: ixonos/sensact-emulator

decompiled_code:
__int64 test_orientation_sweep()
{
int v0; // er12
int i; // ebx
_DWORD *v2; // rax
int v3; // er12
int j; // ebx
int v5; // er12
int k; // ebx

v0 = 10;
do
{
for ( i = 0; i != 1000; ++i )
{
v2 = senshub;
senshub[8] = i;
v2[16] = i;
*v2 = 0;
usleep(0x1F40u);
}
sleep(5u);
--v0;
}
while ( v0 );
v3 = 10;
do
{
for ( j = 0; j != 1000; ++j )
{
senshub[16] = j;
usleep(0x640u);
}
--v3;
}
while ( v3 );
sleep(5u);
v5 = 10;
do
{
for ( k = 0; k != 1000; ++k )
{
*senshub = k;
usleep(0x320u);
}
--v5;
}
while ( v5 );
return 0LL;
}

original_code:
static char * test_orientation_sweep() {
 int i, count = 0;
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->pitch = i;
   senshub->yaw = i;
   senshub->roll = 0;
   usleep(8000);
  }
  sleep(5);
 }
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->yaw = i;
   usleep(8000/5);
  }
 }
 sleep(5);
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->roll = i;
   usleep(8000/10);
  }
 }
 return 0;
}

canonical_original_code:
static char *test_orientation_sweep() {
 int i; int count = 0;
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->pitch = i;
   senshub->yaw = i;
   senshub->roll = 0;
   usleep(8000);
  }
  sleep(5);
 }
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->yaw = i;
   usleep(8000/5);
  }
 }
 sleep(5);
 for (count = 0; count < 10; count++) {
  for (i = 0; i < 1000; i++) {
   senshub->roll = i;
   usleep(8000/10);
  }
 }
 return 0;
}

variable_types: count int

return_type: char *


decompiled_code:
void *__fastcall runorientation_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_orientation_sweep(a1);
++tests_run;
if ( result )
break;
a1 = test_orientation_sweep + 1;
sleep(1u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func0(a1);
++tests_run;
if ( result )
break;
a1 = test_orientation_sweep + 1;
sleep(1u);
}
return result;
}

original_code:
void * runorientation_test()
{
        while(1)
        {
            do { char *message = test_orientation_sweep(); tests_run++; if (message) return message; } while (0);
            sleep(1);
        }
}

canonical_original_code:
void *runorientation_test()
{
        while(1)
        {
            do { char *message = test_orientation_sweep(); tests_run++; if (message) return message; } while (0);
            sleep(1);
        }
}

variable_types:
message
char *

return_type: void *


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

v0 = rand();
*((float *)senshub + 40) = (float)(5 * (v0 / 5) - v0 + 20);
return 0LL;
}

original_code:
static char * test_temperature_sweep() {
  senshub->objtemp = 20 - (rand() % 5);
 return 0;
}

canonical_original_code:
static char *test_temperature_sweep() {
  senshub->objtemp = 20 - (rand() % 5);
 return 0;
}


decompiled_code:
void *__fastcall runtemperature_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_temperature_sweep(a1);
++tests_run;
if ( result )
break;
a1 = test_orientation_sweep + 1;
sleep(1u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func2(a1);
++tests_run;
if ( result )
break;
a1 = test_orientation_sweep + 1;
sleep(1u);
}
return result;
}

original_code:
void * runtemperature_test()
{
        while(1)
        {
           do { char *message = test_temperature_sweep(); tests_run++; if (message) return message; } while (0);
           sleep(1);
        }
}

canonical_original_code:
void *runtemperature_test()
{
        while(1)
        {
           do { char *message = test_temperature_sweep(); tests_run++; if (message) return message; } while (0);
           sleep(1);
        }
}


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

v0 = rand();
*((float *)senshub + 48) = (float)(5 * (v0 / 5) - v0);
return 0LL;
}

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

v0 = rand();
*((float *)senshub + 48) = (float)(5 * (v0 / 5) - v0);
return 0LL;
}

original_code:
static char * test_ambient_temperature_sweep() {
  senshub->ambtemp = 0 - (rand() % 5);
 return 0;
}

canonical_original_code:
static char *test_ambient_temperature_sweep() {
  senshub->ambtemp = 0 - (rand() % 5);
 return 0;
}


decompiled_code:
void *__fastcall runambienttemperature_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_ambient_temperature_sweep(a1);
++tests_run;
if ( result )
break;
a1 = (void *)500000;
usleep(0x7A120u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func4(a1);
++tests_run;
if ( result )
break;
a1 = (void *)500000;
usleep(0x7A120u);
}
return result;
}

original_code:
void * runambienttemperature_test()
{
        while(1)
        {
           do { char *message = test_ambient_temperature_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(500000);
        }
}

canonical_original_code:
void *runambienttemperature_test()
{
        while(1)
        {
           do { char *message = test_ambient_temperature_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(500000);
        }
}


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

v0 = rand();
*((float *)senshub + 56) = (float)(20 * (v0 / 20) - v0 + 80);
return 0LL;
}

original_code:
static char * test_humidity_sweep() {
  senshub->humidity = 80 - (rand() % 20);
 return 0;
}

canonical_original_code:
static char *test_humidity_sweep() {
  senshub->humidity = 80 - (rand() % 20);
 return 0;
}


decompiled_code:
void *__fastcall runhumidity_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_humidity_sweep(a1);
++tests_run;
if ( result )
break;
a1 = (void *)1000000;
usleep(0xF4240u);
}
return result;
}

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

while ( 1 )
{
result = (void *)func6(a1);
++tests_run;
if ( result )
break;
a1 = (void *)1000000;
usleep(0xF4240u);
}
return result;
}

original_code:
void * runhumidity_test()
{
        while(1)
        {
           do { char *message = test_humidity_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(1000000);
        }
}

canonical_original_code:
void *runhumidity_test()
{
        while(1)
        {
           do { char *message = test_humidity_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(1000000);
        }
}


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

v0 = rand();
*((float *)senshub + 24) = (float)(10 * (v0 / 10) - v0 + 80);
return 0LL;
}

original_code:
static char * test_brightness_sweep() {
  senshub->light = 80 - (rand() % 10);
 return 0;
}

canonical_original_code:
static char *test_brightness_sweep() {
  senshub->light = 80 - (rand() % 10);
 return 0;
}


decompiled_code:
void *__fastcall runbrightness_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_brightness_sweep(a1);
++tests_run;
if ( result )
break;
a1 = (void *)900000;
usleep(0xDBBA0u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func9(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func8(a1);
++tests_run;
if ( result )
break;
a1 = (void *)900000;
usleep(0xDBBA0u);
}
return result;
}

original_code:
void * runbrightness_test()
{
        while(1)
        {
           do { char *message = test_brightness_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(900000);
        }
}

canonical_original_code:
void *runbrightness_test()
{
        while(1)
        {
           do { char *message = test_brightness_sweep(); tests_run++; if (message) return message; } while (0);
           usleep(900000);
        }
}


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

v0 = rand();
*((float *)senshub + 32) = (float)(50 * (v0 / 50) - v0 + 800);
return 0LL;
}

original_code:
static char * test_air_pressure() {
  senshub->presure = 800 - (rand() % 50);
 return 0;
}

canonical_original_code:
static char *test_air_pressure() {
  senshub->presure = 800 - (rand() % 50);
 return 0;
}


decompiled_code:
void *__fastcall runairpressure_test(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_air_pressure(a1);
++tests_run;
if ( result )
break;
a1 = (void *)900000;
usleep(0xDBBA0u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func11(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func10(a1);
++tests_run;
if ( result )
break;
a1 = (void *)900000;
usleep(0xDBBA0u);
}
return result;
}

original_code:
void * runairpressure_test()
{
        while(1)
        {
           do { char *message = test_air_pressure(); tests_run++; if (message) return message; } while (0);
           usleep(900000);
        }
}

canonical_original_code:
void *runairpressure_test()
{
        while(1)
        {
           do { char *message = test_air_pressure(); tests_run++; if (message) return message; } while (0);
           usleep(900000);
        }
}


decompiled_code:
void __fastcall __noreturn runbletemp_test(void *a1)
{
while ( 1 )
{
test_ble_sweep(a1);
a1 = (void *)800000;
usleep(0xC3500u);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func13(void *a1)
{
while ( 1 )
{
func12(a1);
a1 = (void *)800000;
usleep(0xC3500u);
}
}

original_code:
void * runbletemp_test() {
  while (1) {
    test_ble_sweep();
    usleep(800000);
  }
}

canonical_original_code:
void *runbletemp_test() {
  while (1) {
    test_ble_sweep();
    usleep(800000);
  }
}


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

result = engine;
*(_BYTE *)(engine + 64LL) = a1;
return result;
}

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

result = engine;
*(_BYTE *)(engine + 64LL) = a1;
return result;
}

original_code:
void emulator_set_direction(int direction) {
 engine->direction = direction;
}


decompiled_code:
__int64 emulator_get_rpm()
{
return (unsigned int)*engine;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)*engine;
}

original_code:
int emulator_get_rpm() {
 return engine->rpm;
}


decompiled_code:
__int64 emulator_get_rpm_max()
{
return (unsigned int)engine[8];
}

canonical_decompiled_code:
__int64 func18()
{
return (unsigned int)engine[8];
}

original_code:
int emulator_get_rpm_max(){
  return engine->rpm_max;
}


decompiled_code:
__int64 test_engine_setting_its_value()
{
int v0; // ebx
int v1; // ebp

emulator_set_rpm(0);
v0 = emulator_get_rpm();
v1 = emulator_get_rpm_max();
if ( rpm_max_last == v1 )
{
test_engine_rand_value();
}
else
{
for ( rpm_max_last = v1; v1 >= v0; ++v0 )
emulator_set_rpm(v0);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func20()
{
int v0; // ebx
int v1; // ebp

emulator_set_rpm(0);
v0 = func17();
v1 = func18();
if ( rpm_max_last == v1 )
{
func19();
}
else
{
for ( rpm_max_last = v1; v1 >= v0; ++v0 )
emulator_set_rpm(v0);
}
return 0LL;
}

original_code:
static char * test_engine_setting_its_value() {
 emulator_set_rpm(0);
 int l_rpm = emulator_get_rpm();
 int l_rpm_max = emulator_get_rpm_max();
 if (rpm_max_last != l_rpm_max) {
   rpm_max_last = l_rpm_max;
   if (l_rpm <= l_rpm_max){
     for (; l_rpm <= l_rpm_max ; l_rpm++) {
       emulator_set_rpm(l_rpm);
   }
  }
  else {
    for (l_rpm = l_rpm_max; l_rpm > l_rpm_max ; l_rpm--) {
       emulator_set_rpm(l_rpm);
    }
  }
 }
 else {
   test_engine_rand_value();
 }
 return 0;
}

canonical_original_code:
static char *test_engine_setting_its_value() {
 emulator_set_rpm(0);
 int l_rpm = emulator_get_rpm();
 int l_rpm_max = emulator_get_rpm_max();
 if (rpm_max_last != l_rpm_max) {
   rpm_max_last = l_rpm_max;
   if (l_rpm <= l_rpm_max){
     for (; l_rpm <= l_rpm_max ; l_rpm++) {
       emulator_set_rpm(l_rpm);
   }
  }
  else {
    for (l_rpm = l_rpm_max; l_rpm > l_rpm_max ; l_rpm--) {
       emulator_set_rpm(l_rpm);
    }
  }
 }
 else {
   test_engine_rand_value();
 }
 return 0;
}


decompiled_code:
void *__fastcall runengine_tests(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)test_engine_setting_its_value(a1);
++tests_run;
if ( result )
break;
a1 = (void *)100000;
usleep(0x186A0u);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func21(void *a1)
{
void *result; // rax

while ( 1 )
{
result = (void *)func20(a1);
++tests_run;
if ( result )
break;
a1 = (void *)100000;
usleep(0x186A0u);
}
return result;
}

original_code:
void * runengine_tests() {
 while(1) {
   do { char *message = test_engine_setting_its_value(); tests_run++; if (message) return message; } while (0);
   usleep(100000);
 }
 return 0;
}

canonical_original_code:
void *runengine_tests() {
 while(1) {
   do { char *message = test_engine_setting_its_value(); tests_run++; if (message) return message; } while (0);
   usleep(100000);
 }
 return 0;
}


setup

decompiled_code:
__int64 setup()
{
__int64 v0; // rbx
unsigned int v1; // ebp
__int64 v2; // rbx
__int64 v3; // rbx
int *v5; // rax
unsigned int v6; // eax
int *v7; // rax
unsigned int v8; // eax
int *v9; // rax
unsigned int v10; // eax

printf("setup() : shared_memory_senshub = %d\n", 1235LL);
v0 = (unsigned int)shmget(1235, 0x100uLL, 950);
printf("setup() : shmid = %d\n", v0);
if ( (_DWORD)v0 == -1 )
{
v5 = _errno_location();
v6 = (unsigned int)strerror(*v5);
printf("shmget failed %s \n", (const char *)v6);
v1 = 0;
}
else
{
shared_memory = shmat(v0, 0LL, 0);
senshub = (_DWORD *)shared_memory;
v1 = 1;
}
v2 = (unsigned int)shmget(1234, 0x60uLL, 950);
printf("setup() : shmid = %d\n", v2);
if ( (_DWORD)v2 == -1 )
{
v7 = _errno_location();
v8 = (unsigned int)strerror(*v7);
printf("shmget failed %s \n", (const char *)v8);
}
else
{
puts("setup:shmid");
shared_memory = shmat(v2, 0LL, 0);
printf("setup:shmid:shared_memory 0x%x\n", shared_memory);
engine = (_DWORD *)shared_memory;
printf("setup:shmid:engine 0x%x\n", shared_memory);
v1 = 1;
}
v3 = (unsigned int)shmget(1236, 0x20uLL, 950);
printf("setup() : shmid = %d\n", v3);
if ( (_DWORD)v3 == -1 )
{
v9 = _errno_location();
v10 = (unsigned int)strerror(*v9);
printf("shmget failed %s \n", (const char *)v10);
}
else
{
puts("setup:shmid");
shared_memory = shmat(v3, 0LL, 0);
printf("setup:shmid:shared_memory 0x%x\n", shared_memory);
ble = shared_memory;
printf("setup:shmid:ble 0x%x\n", shared_memory);
v1 = 1;
}
return v1;
}

canonical_decompiled_code:
__int64 func22()
{
__int64 v0; // rbx
unsigned int v1; // ebp
__int64 v2; // rbx
__int64 v3; // rbx
int *v5; // rax
unsigned int v6; // eax
int *v7; // rax
unsigned int v8; // eax
int *v9; // rax
unsigned int v10; // eax

printf("setup() : shared_memory_senshub = %d\n", 1235LL);
v0 = (unsigned int)shmget(1235, 0x100uLL, 950);
printf("setup() : shmid = %d\n", v0);
if ( (_DWORD)v0 == -1 )
{
v5 = _errno_location();
v6 = (unsigned int)strerror(*v5);
printf("shmget failed %s \n", (const char *)v6);
v1 = 0;
}
else
{
shared_memory = shmat(v0, 0LL, 0);
senshub = (_DWORD *)shared_memory;
v1 = 1;
}
v2 = (unsigned int)shmget(1234, 0x60uLL, 950);
printf("setup() : shmid = %d\n", v2);
if ( (_DWORD)v2 == -1 )
{
v7 = _errno_location();
v8 = (unsigned int)strerror(*v7);
printf("shmget failed %s \n", (const char *)v8);
}
else
{
puts("setup:shmid");
shared_memory = shmat(v2, 0LL, 0);
printf("setup:shmid:shared_memory 0x%x\n", shared_memory);
engine = (_DWORD *)shared_memory;
printf("setup:shmid:engine 0x%x\n", shared_memory);
v1 = 1;
}
v3 = (unsigned int)shmget(1236, 0x20uLL, 950);
printf("setup() : shmid = %d\n", v3);
if ( (_DWORD)v3 == -1 )
{
v9 = _errno_location();
v10 = (unsigned int)strerror(*v9);
printf("shmget failed %s \n", (const char *)v10);
}
else
{
puts("setup:shmid");
shared_memory = shmat(v3, 0LL, 0);
printf("setup:shmid:shared_memory 0x%x\n", shared_memory);
ble = shared_memory;
printf("setup:shmid:ble 0x%x\n", shared_memory);
v1 = 1;
}
return v1;
}

original_code:
int setup() {
 int retval = 0;
 int shmid = 0;
  printf("setup() : shared_memory_senshub = %d\n",1235);
 shmid = shmget((key_t) 1235, sizeof(senshub_t),
   0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_memory = shmat(shmid, (void *) 0, 0);
  senshub = (senshub_t *) shared_memory;
  retval = 1;
  }
 shmid = shmget((key_t) 1234, sizeof(engine_t),0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
    printf("setup:shmid\n");
  shared_memory = shmat(shmid, (void *) 0, 0);
    printf("setup:shmid:shared_memory 0x%x\n",shared_memory);
  engine = (engine_t *) shared_memory;
    printf("setup:shmid:engine 0x%x\n",engine);
  retval = 1;
  }
 shmid = shmget((key_t) 1236, sizeof(ble_t),0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
    printf("setup:shmid\n");
  shared_memory = shmat(shmid, (void *) 0, 0);
    printf("setup:shmid:shared_memory 0x%x\n",shared_memory);
  ble = (ble_t *) shared_memory;
    printf("setup:shmid:ble 0x%x\n",ble);
  retval = 1;
  }
 return retval;
}

canonical_original_code:
int setup() {
 int retval = 0;
 int shmid = 0;
  printf("setup() : shared_memory_senshub = %d\n",1235);
 shmid = shmget((int) 1235, sizeof(senshub_t),
   0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_memory = shmat(shmid, (void *) 0, 0);
  senshub = (struct senshub_t *) shared_memory;
  retval = 1;
  }
 shmid = shmget((int) 1234, sizeof(engine_t),0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
    printf("setup:shmid\n");
  shared_memory = shmat(shmid, (void *) 0, 0);
    printf("setup:shmid:shared_memory 0x%x\n",shared_memory);
  engine = (struct engine_t *) shared_memory;
    printf("setup:shmid:engine 0x%x\n",engine);
  retval = 1;
  }
 shmid = shmget((int) 1236, sizeof(ble_t),0666 | 01000);
  printf("setup() : shmid = %d\n",shmid);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
    printf("setup:shmid\n");
  shared_memory = shmat(shmid, (void *) 0, 0);
    printf("setup:shmid:shared_memory 0x%x\n",shared_memory);
  ble = (struct ble_t *) shared_memory;
    printf("setup:shmid:ble 0x%x\n",ble);
  retval = 1;
  }
 return retval;
}

variable_types: retval int

variable_types: shmid int

user_defined_types: struct senshub_t { int roll; char *roll_name; void (*setroll)(int roll); int (*getroll)(); int pitch; char *pitch_name; void (*setpitch)(int roll); int (*getpitch)(); int yaw; char *yaw_name; void (*setyaw)(int yaw); int (*getyaw)(); float light; char *light_name; void (*setlight)(float light); float (*getlight)(); float presure; char *presure_name; void (*setpresure)(float presure); float (*getpresure)(); float objtemp; char *objtemp_name; float (*getobjtemp)(); void (*setobjtemp)(float objtemp); float ambtemp; char *ambtemp_name; float (*getambtemp)(); void (*setambtemp)(float ambtemp); float humidity; char *humidity_name; void (*sethumidity)(float humidity); float (*gethumidity)(); }

user_defined_types: struct engine_t { int rpm; char *rpm_name; void (*setrpm)(int rpm); int (*getrpm)(); int rpm_max; char *rpm_max_name; void (*setrpm_max)(int rpm_max); int (*getrpm_max)(); unsigned char direction; char *direction_name; void (*setdirection)(unsigned char direction); unsigned char (*getdirection)(); }

user_defined_types: struct ble_t { float temp; char *temp_name; void (*settemp)(float rpm); float (*gettemp)(); }


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rbx
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v3 = time(&timer);
srand(v3);
puts("main()");
v4 = (unsigned int)setup("main()", argv);
printf("main() : retval = %d\n", v4);
if ( (_DWORD)v4 )
{
all_tests();
while ( emulatorRunning )
sleep(1u);
}
else
{
printf("setup failed!");
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__int64 v4; // rbx
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v3 = time(&timer);
srand(v3);
puts("main()");
v4 = (unsigned int)func22("main()", argv);
printf("main() : retval = %d\n", v4);
if ( (_DWORD)v4 )
{
func15();
while ( emulatorRunning )
sleep(1u);
}
else
{
printf("setup failed!");
}
return v4;
}

original_code:
int main(int argc, char **argv) {
  time_t t;
  srand((unsigned) time(&t));
  printf("main()\n");
 int retval = setup();
  printf("main() : retval = %d\n",retval);
 if (retval) {
    char *result = all_tests();
           while(emulatorRunning != 0)
           {
             sleep(1);
           }
 } else {
  printf("setup failed!");
 }
 return retval;
}

canonical_original_code:
int main(int argc, char **argv) {
  long t;
  srand((unsigned int) time(&t));
  printf("main()\n");
 int retval = setup();
  printf("main() : retval = %d\n",retval);
 if (retval) {
    char *result = all_tests();
           while(emulatorRunning != 0)
           {
             sleep(1);
           }
 } else {
  printf("setup failed!");
 }
 return retval;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: t long

variable_types:
result
char *


getroll

decompiled_code:
__int64 getroll()
{
return (unsigned int)*senshub;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)*senshub;
}

original_code:
int getroll() {
 return senshub->roll;
}

canonical_original_code:
int getroll() {
 return senshub->roll;
}


getpitch

decompiled_code:
__int64 getpitch()
{
return (unsigned int)senshub[8];
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)senshub[8];
}

original_code:
int getpitch() {
 return senshub->pitch;
}


getyaw

decompiled_code:
__int64 getyaw()
{
return (unsigned int)senshub[16];
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)senshub[16];
}

original_code:
int getyaw() {
 return senshub->yaw;
}

canonical_original_code:
int getyaw() {
 return senshub->yaw;
}


getlight

decompiled_code:
float getlight()
{
return *((float *)senshub + 24);
}

canonical_decompiled_code:
float func3()
{
return *((float *)senshub + 24);
}

original_code:
float getlight() {
 return senshub->light;
}

return_type: float


getpresure

decompiled_code:
float getpresure()
{
return *((float *)senshub + 32);
}

canonical_decompiled_code:
float func4()
{
return *((float *)senshub + 32);
}

original_code:
float getpresure() {
 return senshub->presure;
}


getobjtemp

decompiled_code:
float getobjtemp()
{
return *((float *)senshub + 40);
}

canonical_decompiled_code:
float func5()
{
return *((float *)senshub + 40);
}

original_code:
float getobjtemp() {
 return senshub->objtemp;
}


getambtemp

decompiled_code:
float getambtemp()
{
return *((float *)senshub + 48);
}

canonical_decompiled_code:
float func6()
{
return *((float *)senshub + 48);
}

original_code:
float getambtemp() {
 return senshub->ambtemp;
}


gethumidity

decompiled_code:
float gethumidity()
{
return *((float *)senshub + 56);
}

canonical_decompiled_code:
float func7()
{
return *((float *)senshub + 56);
}

original_code:
float gethumidity() {
 return senshub->humidity;
}


decompiled_code:
_DWORD *create_senshub_emulator()
{
int v0; // eax
_DWORD *v1; // rax
int *v3; // rax
char *v4; // rax

v0 = shmget(1235, 0x100uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
printf("shmget failed %s \n", v4);
}
else
{
shared_mem_senshub = shmat(v0, 0LL, 0);
senshub = (_DWORD *)shared_mem_senshub;
}
if ( senshub )
{
*senshub = 0;
v1 = senshub;
*((_QWORD *)senshub + 1) = "roll";
*((_QWORD *)v1 + 2) = setroll;
*((_QWORD *)v1 + 3) = getroll;
v1[8] = 0;
*((_QWORD *)v1 + 5) = "pitch";
*((_QWORD *)v1 + 6) = setpitch;
*((_QWORD *)v1 + 7) = getpitch;
v1[16] = 0;
*((_QWORD *)v1 + 9) = "yaw";
*((_QWORD *)v1 + 11) = getyaw;
*((_QWORD *)v1 + 10) = setyaw;
v1[24] = 0;
*((_QWORD *)v1 + 13) = "light";
*((_QWORD *)v1 + 14) = setlight;
*((_QWORD *)v1 + 15) = getlight;
v1[32] = 1202702336;
*((_QWORD *)v1 + 17) = "pressure";
*((_QWORD *)v1 + 18) = setpresure;
*((_QWORD *)v1 + 19) = getpresure;
v1[40] = LC8;
*((_QWORD *)v1 + 21) = "objTemp";
*((_QWORD *)v1 + 23) = setobjtemp;
*((_QWORD *)v1 + 22) = getobjtemp;
v1[48] = LC8;
*((_QWORD *)v1 + 25) = "ambTemp";
*((_QWORD *)v1 + 26) = getambtemp;
*((_QWORD *)v1 + 27) = setambtemp;
v1[56] = 0;
*((_QWORD *)v1 + 29) = "humidity";
*((_QWORD *)v1 + 30) = sethumidity;
*((_QWORD *)v1 + 31) = gethumidity;
}
return senshub;
}

canonical_decompiled_code:
_DWORD *func8()
{
int v0; // eax
_DWORD *v1; // rax
int *v3; // rax
char *v4; // rax

v0 = shmget(1235, 0x100uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
printf("shmget failed %s \n", v4);
}
else
{
shared_mem_senshub = shmat(v0, 0LL, 0);
senshub = (_DWORD *)shared_mem_senshub;
}
if ( senshub )
{
*senshub = 0;
v1 = senshub;
*((_QWORD *)senshub + 1) = "roll";
*((_QWORD *)v1 + 2) = setroll;
*((_QWORD *)v1 + 3) = getroll;
v1[8] = 0;
*((_QWORD *)v1 + 5) = "pitch";
*((_QWORD *)v1 + 6) = setpitch;
*((_QWORD *)v1 + 7) = getpitch;
v1[16] = 0;
*((_QWORD *)v1 + 9) = "yaw";
*((_QWORD *)v1 + 11) = getyaw;
*((_QWORD *)v1 + 10) = setyaw;
v1[24] = 0;
*((_QWORD *)v1 + 13) = "light";
*((_QWORD *)v1 + 14) = setlight;
*((_QWORD *)v1 + 15) = getlight;
v1[32] = 1202702336;
*((_QWORD *)v1 + 17) = "pressure";
*((_QWORD *)v1 + 18) = setpresure;
*((_QWORD *)v1 + 19) = getpresure;
v1[40] = LC8;
*((_QWORD *)v1 + 21) = "objTemp";
*((_QWORD *)v1 + 23) = setobjtemp;
*((_QWORD *)v1 + 22) = getobjtemp;
v1[48] = LC8;
*((_QWORD *)v1 + 25) = "ambTemp";
*((_QWORD *)v1 + 26) = getambtemp;
*((_QWORD *)v1 + 27) = setambtemp;
v1[56] = 0;
*((_QWORD *)v1 + 29) = "humidity";
*((_QWORD *)v1 + 30) = sethumidity;
*((_QWORD *)v1 + 31) = gethumidity;
}
return senshub;
}

original_code:
senshub_t * create_senshub_emulator() {
 shmid = shmget((key_t) 1235, sizeof(senshub_t),
   0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_mem_senshub = shmat(shmid, (void *) 0, 0);
  senshub = (senshub_t*) shared_mem_senshub;
 }
 if (senshub != ((void *)0)) {
  senshub->roll = 0;
  senshub->roll_name = "roll";
  senshub->setroll = setroll;
  senshub->getroll = getroll;
  senshub->pitch = 0;
  senshub->pitch_name = "pitch";
  senshub->setpitch = setpitch;
  senshub->getpitch = getpitch;
  senshub->yaw = 0;
  senshub->yaw_name = "yaw";
  senshub->getyaw = getyaw;
  senshub->setyaw = setyaw;
  senshub->light = 0;
  senshub->light_name = "light";
  senshub->setlight = setlight;
  senshub->getlight = getlight;
  senshub->presure = 90000;
  senshub->presure_name = "pressure";
  senshub->setpresure = setpresure;
  senshub->getpresure = getpresure;
  senshub->objtemp = -20;
  senshub->objtemp_name = "objTemp";
  senshub->setobjtemp = setobjtemp;
  senshub->getobjtemp = getobjtemp;
  senshub->ambtemp = -20;
  senshub->ambtemp_name = "ambTemp";
  senshub->getambtemp = getambtemp;
  senshub->setambtemp = setambtemp;
  senshub->humidity = 0;
  senshub->humidity_name = "humidity";
  senshub->sethumidity = sethumidity;
  senshub->gethumidity = gethumidity;
 }
 return senshub;
}

canonical_original_code:
struct senshub_t *create_senshub_emulator() {
 shmid = shmget((int) 1235, sizeof(senshub_t),
   0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_mem_senshub = shmat(shmid, (void *) 0, 0);
  senshub = (struct senshub_t *) shared_mem_senshub;
 }
 if (senshub != ((void *)0)) {
  senshub->roll = 0;
  senshub->roll_name = "roll";
  senshub->setroll = setroll;
  senshub->getroll = getroll;
  senshub->pitch = 0;
  senshub->pitch_name = "pitch";
  senshub->setpitch = setpitch;
  senshub->getpitch = getpitch;
  senshub->yaw = 0;
  senshub->yaw_name = "yaw";
  senshub->getyaw = getyaw;
  senshub->setyaw = setyaw;
  senshub->light = 0;
  senshub->light_name = "light";
  senshub->setlight = setlight;
  senshub->getlight = getlight;
  senshub->presure = 90000;
  senshub->presure_name = "pressure";
  senshub->setpresure = setpresure;
  senshub->getpresure = getpresure;
  senshub->objtemp = -20;
  senshub->objtemp_name = "objTemp";
  senshub->setobjtemp = setobjtemp;
  senshub->getobjtemp = getobjtemp;
  senshub->ambtemp = -20;
  senshub->ambtemp_name = "ambTemp";
  senshub->getambtemp = getambtemp;
  senshub->setambtemp = setambtemp;
  senshub->humidity = 0;
  senshub->humidity_name = "humidity";
  senshub->sethumidity = sethumidity;
  senshub->gethumidity = gethumidity;
 }
 return senshub;
}

return_type: struct senshub_t { int roll; char *roll_name; void (*setroll)(int roll); int (*getroll)(); int pitch; char *pitch_name; void (*setpitch)(int roll); int (*getpitch)(); int yaw; char *yaw_name; void (*setyaw)(int yaw); int (*getyaw)(); float light; char *light_name; void (*setlight)(float light); float (*getlight)(); float presure; char *presure_name; void (*setpresure)(float presure); float (*getpresure)(); float objtemp; char *objtemp_name; float (*getobjtemp)(); void (*setobjtemp)(float objtemp); float ambtemp; char *ambtemp_name; float (*getambtemp)(); void (*setambtemp)(float ambtemp); float humidity; char *humidity_name; void (*sethumidity)(float humidity); float (*gethumidity)(); } *


decompiled_code:
int destroy_senshub_emulator()
{
return shmdt(shared_mem_senshub);
}

canonical_decompiled_code:
int func9()
{
return shmdt(shared_mem_senshub);
}

original_code:
void destroy_senshub_emulator() {
 shmdt(shared_mem_senshub);
}


decompiled_code:
__int64 getrpm_max()
{
return *(unsigned int *)(engine + 32LL);
}

canonical_decompiled_code:
__int64 func1()
{
return *(unsigned int *)(engine + 32LL);
}

original_code:
int getrpm_max(void) {
 return engine->rpm_max;
}

canonical_original_code:
int getrpm_max() {
 return engine->rpm_max;
}


getrpm

decompiled_code:
__int64 getrpm()
{
return (unsigned int)*engine;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)*engine;
}

original_code:
int getrpm(void) {
 return engine->rpm;
}

canonical_original_code:
int getrpm() {
 return engine->rpm;
}


getdirection

decompiled_code:
__int64 getdirection()
{
return *((unsigned __int8 *)engine + 64);
}

canonical_decompiled_code:
__int64 func3()
{
return *((unsigned __int8 *)engine + 64);
}

original_code:
uint8_t getdirection() {
 return engine->direction;
}

canonical_original_code:
unsigned char getdirection() {
 return engine->direction;
}

return_type: unsigned char


decompiled_code:
_DWORD *create_emulator_engine()
{
int v0; // eax
_DWORD *v1; // rax
_DWORD *v2; // rax
int *v4; // rax
char *v5; // rax

v0 = shmget(1234, 0x60uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("shmget failed %s \n", v5);
}
else
{
shared_mem_engine = shmat(v0, 0LL, 0);
engine = (_DWORD *)shared_mem_engine;
}
v1 = engine;
if ( engine )
{
*((_QWORD *)engine + 10) = setdirection;
*((_QWORD *)v1 + 11) = getdirection;
*((_BYTE *)v1 + 64) = 0;
*((_QWORD *)v1 + 9) = "direction";
*v1 = 1000;
v2 = engine;
*((_QWORD *)engine + 1) = "rpm";
*((_QWORD *)v2 + 3) = getrpm;
*((_QWORD *)v2 + 2) = setrpm;
v2[8] = 1000;
*((_QWORD *)v2 + 5) = "rpm_max";
*((_QWORD *)v2 + 7) = getrpm_max;
*((_QWORD *)v2 + 6) = setrpm_max;
}
return engine;
}

canonical_decompiled_code:
_DWORD *func4()
{
int v0; // eax
_DWORD *v1; // rax
_DWORD *v2; // rax
int *v4; // rax
char *v5; // rax

v0 = shmget(1234, 0x60uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
printf("shmget failed %s \n", v5);
}
else
{
shared_mem_engine = shmat(v0, 0LL, 0);
engine = (_DWORD *)shared_mem_engine;
}
v1 = engine;
if ( engine )
{
*((_QWORD *)engine + 10) = setdirection;
*((_QWORD *)v1 + 11) = getdirection;
*((_BYTE *)v1 + 64) = 0;
*((_QWORD *)v1 + 9) = "direction";
*v1 = 1000;
v2 = engine;
*((_QWORD *)engine + 1) = "rpm";
*((_QWORD *)v2 + 3) = getrpm;
*((_QWORD *)v2 + 2) = setrpm;
v2[8] = 1000;
*((_QWORD *)v2 + 5) = "rpm_max";
*((_QWORD *)v2 + 7) = getrpm_max;
*((_QWORD *)v2 + 6) = setrpm_max;
}
return engine;
}

original_code:
engine_t *create_emulator_engine() {
 shmid = shmget((key_t) 1234, sizeof(engine_t),
   0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_mem_engine = shmat(shmid, (void *) 0, 0);
  engine = (engine_t*) shared_mem_engine;
 }
 if (engine != ((void *)0)) {
  engine->setdirection = setdirection;
  engine->getdirection = getdirection;
  engine->direction = 0;
  engine->direction_name = "direction";
  engine->rpm = 1000;
  engine->rpm_name = "rpm";
  engine->getrpm = getrpm;
  engine->setrpm = setrpm;
  engine->rpm_max = 1000;
  engine->rpm_max_name = "rpm_max";
  engine->getrpm_max = getrpm_max;
  engine->setrpm_max = setrpm_max;
 }
 return engine;
}

canonical_original_code:
struct engine_t *create_emulator_engine() {
 shmid = shmget((int) 1234, sizeof(engine_t),
   0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed %s \n", strerror( (*__errno_location ())));
 } else {
  shared_mem_engine = shmat(shmid, (void *) 0, 0);
  engine = (struct engine_t *) shared_mem_engine;
 }
 if (engine != ((void *)0)) {
  engine->setdirection = setdirection;
  engine->getdirection = getdirection;
  engine->direction = 0;
  engine->direction_name = "direction";
  engine->rpm = 1000;
  engine->rpm_name = "rpm";
  engine->getrpm = getrpm;
  engine->setrpm = setrpm;
  engine->rpm_max = 1000;
  engine->rpm_max_name = "rpm_max";
  engine->getrpm_max = getrpm_max;
  engine->setrpm_max = setrpm_max;
 }
 return engine;
}

return_type: struct engine_t { int rpm; char *rpm_name; void (*setrpm)(int rpm); int (*getrpm)(); int rpm_max; char *rpm_max_name; void (*setrpm_max)(int rpm_max); int (*getrpm_max)(); unsigned char direction; char *direction_name; void (*setdirection)(unsigned char direction); unsigned char (*getdirection)(); } *


decompiled_code:
int destroy_engine_emulator()
{
return shmdt(shared_mem_engine);
}

canonical_decompiled_code:
int func5()
{
return shmdt(shared_mem_engine);
}

original_code:
void destroy_engine_emulator() {
 shmdt(shared_mem_engine);
}


gettemp

decompiled_code:
float gettemp()
{
return *ble_device;
}

canonical_decompiled_code:
float func1()
{
return *ble_device;
}

original_code:
float gettemp(void) {
 return ble_device->temp;
}

canonical_original_code:
float gettemp() {
 return ble_device->temp;
}


decompiled_code:
float *create_emulator_ble()
{
int v0; // eax
float *v1; // rax

v0 = shmget(1236, 0x20uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
puts("shmget failed");
}
else
{
shared_mem = shmat(v0, 0LL, 0);
ble_device = (float *)shared_mem;
}
v1 = ble_device;
if ( ble_device )
{
*((_QWORD *)ble_device + 3) = gettemp;
*((_QWORD *)v1 + 2) = settemp;
*v1 = 10.0;
*((_QWORD *)ble_device + 1) = "ble_temp";
}
return ble_device;
}

canonical_decompiled_code:
float *func2()
{
int v0; // eax
float *v1; // rax

v0 = shmget(1236, 0x20uLL, 950);
shmid = v0;
if ( v0 == -1 )
{
puts("shmget failed");
}
else
{
shared_mem = shmat(v0, 0LL, 0);
ble_device = (float *)shared_mem;
}
v1 = ble_device;
if ( ble_device )
{
*((_QWORD *)ble_device + 3) = gettemp;
*((_QWORD *)v1 + 2) = settemp;
*v1 = 10.0;
*((_QWORD *)ble_device + 1) = "ble_temp";
}
return ble_device;
}

original_code:
ble_t *create_emulator_ble() {
 shmid = shmget((key_t) 1236, sizeof(ble_t), 0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed\n");
 } else {
  shared_mem = shmat(shmid, (void *) 0, 0);
  ble_device = (ble_t*) shared_mem;
 }
 if (ble_device != ((void *)0)) {
  ble_device->gettemp = gettemp;
  ble_device->settemp = settemp;
  ble_device->temp = 10;
  ble_device->temp_name = "ble_temp";
 }
 return ble_device;
}

canonical_original_code:
struct ble_t *create_emulator_ble() {
 shmid = shmget((int) 1236, sizeof(ble_t), 0666 | 01000);
 if (shmid == -1) {
  printf("shmget failed\n");
 } else {
  shared_mem = shmat(shmid, (void *) 0, 0);
  ble_device = (struct ble_t *) shared_mem;
 }
 if (ble_device != ((void *)0)) {
  ble_device->gettemp = gettemp;
  ble_device->settemp = settemp;
  ble_device->temp = 10;
  ble_device->temp_name = "ble_temp";
 }
 return ble_device;
}

return_type: struct ble_t { float temp; char *temp_name; void (*settemp)(float rpm); float (*gettemp)(); } *


decompiled_code:
int destroy_ble_emulator()
{
return shmdt(shared_mem);
}

canonical_decompiled_code:
int func3()
{
return shmdt(shared_mem);
}

original_code:
void destroy_ble_emulator() {
 shmdt(shared_mem);
}


readfile

repo: izak/monitortools

decompiled_code:
__int64 __fastcall readfile(char *file, void *s, size_t a3)
{
int v4; // ebp
int i; // ebx
int v6; // eax

memset(s, 0, a3);
v4 = open(file, 0);
if ( v4 == -1 )
{
i = -1;
}
else
{
for ( i = 0; ; i += v6 )
{
v6 = read(v4, (char *)s + i, a3 - i);
if ( v6 <= 0 )
break;
}
close(v4);
}
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *file, void *s, size_t a3)
{
int v4; // ebp
int i; // ebx
int v6; // eax

memset(s, 0, a3);
v4 = open(file, 0);
if ( v4 == -1 )
{
i = -1;
}
else
{
for ( i = 0; ; i += v6 )
{
v6 = read(v4, (char *)s + i, a3 - i);
if ( v6 <= 0 )
break;
}
close(v4);
}
return (unsigned int)i;
}

original_code:
int readfile(const char *filename, char *buf, size_t count){
    int i = 0;
    int j;
    int fd;
    bzero(buf, count);
    fd = open(filename, 00);
    if(fd!=-1){
        while((j = read(fd, buf+i, count-i))>0){
            i+=j;
        }
        close(fd);
        return i;
    }
    return -1;
}

canonical_original_code:
int readfile(char *filename, char *buf, unsigned long count){
    int i = 0;
    int j;
    int fd;
    bzero(buf, count);
    fd = open(filename, 00);
    if(fd!=-1){
        while((j = read(fd, buf+i, count-i))>0){
            i+=j;
        }
        close(fd);
        return i;
    }
    return -1;
}

variable_types:
filename
char *

variable_types:
buf
char *

variable_types:
count
unsigned long

variable_types: j int

variable_types: fd int


scanbuf

decompiled_code:
char *__fastcall scanbuf(char *s1, __int64 a2, const char *a3)
{
char *v3; // rbp
char *v4; // rbx
size_t v6; // r13

v3 = &s1[a2];
if ( s1 >= &s1[a2] )
return 0LL;
v4 = s1;
v6 = strlen(a3);
while ( strncmp(v4, a3, v6) )
{
for ( ; *v4 && v4 < v3; ++v4 )
;
if ( ++v4 >= v3 )
return 0LL;
}
return v4;
}

canonical_decompiled_code:
char *__fastcall func1(char *s1, __int64 a2, const char *a3)
{
char *v3; // rbp
char *v4; // rbx
size_t v6; // r13

v3 = &s1[a2];
if ( s1 >= &s1[a2] )
return 0LL;
v4 = s1;
v6 = strlen(a3);
while ( strncmp(v4, a3, v6) )
{
for ( ; *v4 && v4 < v3; ++v4 )
;
if ( ++v4 >= v3 )
return 0LL;
}
return v4;
}

original_code:
const char* scanbuf(const char *buf, size_t count, const char *needle){
    int j;
    const char *ptr = buf;
    while(ptr < buf + count){
        if(strncmp(ptr, needle, strlen(needle))==0){
            return ptr;
        }
        while((*ptr!='\0') && (ptr < buf+count)) { ptr++; }
        ptr++;
    }
    return ((void *)0);
}

canonical_original_code:
char *scanbuf(char *buf, unsigned long count, char *needle){
    int j;
    char *ptr = buf;
    while(ptr < buf + count){
        if(strncmp(ptr, needle, strlen(needle))==0){
            return ptr;
        }
        while((*ptr!='\0') && (ptr < buf+count)) { ptr++; }
        ptr++;
    }
    return ((void *)0);
}

variable_types:
needle
char *

variable_types:
ptr
char *


getzopeuid

decompiled_code:
__int64 __fastcall getzopeuid(const char *a1)
{
struct passwd *v1; // rax
__int64 result; // rax

v1 = getpwnam(a1);
if ( v1 )
result = v1->pw_uid;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
struct passwd *v1; // rax
__int64 result; // rax

v1 = getpwnam(a1);
if ( v1 )
result = v1->pw_uid;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int getzopeuid(const char *username){
    struct passwd *pw;
    pw = getpwnam(username);
    if(!pw) return -1;
    return pw->pw_uid;
}

canonical_original_code:
int getzopeuid(char *username){
    struct passwd *pw;
    pw = getpwnam(username);
    if(!pw) return -1;
    return pw->pw_uid;
}

variable_types:
username
char *

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


decompiled_code:
_BOOL8 __fastcall check_string(const char *a1, const char *a2)
{
_BOOL8 result; // rax

if ( *a2 == 33 )
result = strstr(a1, a2 + 1) == 0LL;
else
result = strstr(a1, a2) != 0LL;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1, const char *a2)
{
_BOOL8 result; // rax

if ( *a2 == 33 )
result = strstr(a1, a2 + 1) == 0LL;
else
result = strstr(a1, a2) != 0LL;
return result;
}

original_code:
int check_string(const char* haystack, const char* needle){
    if(needle[0]=='!'){
        return strstr(haystack, needle+1) == ((void *)0);
    }
    return strstr(haystack, needle) != ((void *)0);
}

canonical_original_code:
int check_string(char *haystack, char *needle){
    if(needle[0]=='!'){
        return strstr(haystack, needle+1) == ((void *)0);
    }
    return strstr(haystack, needle) != ((void *)0);
}

variable_types:
haystack
char *


decompiled_code:
_BOOL8 __fastcall check_cmdline(const char *a1, const char *a2)
{
_BOOL4 v2; // er13
const char *v3; // rbp
int v4; // edx
char *v5; // rax
char *v6; // rdx
char *v7; // rbx
char v9[1024]; // [rsp+0h] [rbp-C28h] BYREF
char s[1024]; // [rsp+400h] [rbp-828h] BYREF
char dest[1064]; // [rsp+800h] [rbp-428h] BYREF

v2 = 1;
if ( a2 )
{
v3 = a2;
snprintf(v9, 0x400uLL, "%s/cmdline", a1);
v4 = readfile(v9, s, 0x400uLL);
if ( v4 > 1 )
{
v5 = s;
v6 = &s[v4 - 2 + 1];
do
{
if ( !*v5 )
*v5 = 32;
++v5;
}
while ( v6 != v5 );
}
v7 = strchr(a2, 44);
if ( v7 )
{
v2 = 1;
do
{
strncpy(dest, v3, (int)v7 - (int)v3);
dest[(int)v7 - (int)v3] = 0;
if ( v2 )
v2 = check_string(s, dest);
v3 = v7 + 1;
v7 = strchr(v7 + 1, 44);
}
while ( v7 );
if ( v2 )
v2 = check_string(s, v3);
}
else
{
v2 = check_string(s, a2);
}
}
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const char *a1, const char *a2)
{
_BOOL4 v2; // er13
const char *v3; // rbp
int v4; // edx
char *v5; // rax
char *v6; // rdx
char *v7; // rbx
char v9[1024]; // [rsp+0h] [rbp-C28h] BYREF
char s[1024]; // [rsp+400h] [rbp-828h] BYREF
char dest[1064]; // [rsp+800h] [rbp-428h] BYREF

v2 = 1;
if ( a2 )
{
v3 = a2;
snprintf(v9, 0x400uLL, "%s/cmdline", a1);
v4 = func0(v9, s, 0x400uLL);
if ( v4 > 1 )
{
v5 = s;
v6 = &s[v4 - 2 + 1];
do
{
if ( !*v5 )
*v5 = 32;
++v5;
}
while ( v6 != v5 );
}
v7 = strchr(a2, 44);
if ( v7 )
{
v2 = 1;
do
{
strncpy(dest, v3, (int)v7 - (int)v3);
dest[(int)v7 - (int)v3] = 0;
if ( v2 )
v2 = func3(s, dest);
v3 = v7 + 1;
v7 = strchr(v7 + 1, 44);
}
while ( v7 );
if ( v2 )
v2 = func3(s, v3);
}
else
{
v2 = func3(s, a2);
}
}
return v2;
}

original_code:
int check_cmdline(const char* pidpath, const char* cmdline){
    if(cmdline != ((void *)0)){
        char procpidcmdline[1024];
        char buf[1024];
        int i, j;
        const char *ptr;
        const char *ptr2;
        snprintf(procpidcmdline, sizeof(procpidcmdline), "%s/cmdline", pidpath);
        i = readfile(procpidcmdline, buf, sizeof(buf));
        for (j=0; j<i-1; j++){
            if(buf[j]=='\0'){
                buf[j]=' ';
            }
        }
        ptr = cmdline;
        ptr2 = strchr(ptr, ',');
        if(ptr2){
            int ok = 1;
            char s[1024];
            int len = 0;
            while(ptr2){
                len = ptr2 - ptr;
                strncpy(s, ptr, len);
                s[len] = '\0';
                ok = ok && check_string(buf, s);
                ptr = ptr2+1;
                ptr2 = strchr(ptr, ',');
            }
            ok = ok && check_string(buf, ptr);
            return ok;
        } else {
            return check_string(buf, cmdline);
        }
    }
    return 1;
}

canonical_original_code:
int check_cmdline(char *pidpath, char *cmdline){
    if(cmdline != ((void *)0)){
        char procpidcmdline[1024];
        char buf[1024];
        int i; int j;
        char *ptr;
        char *ptr2;
        snprintf(procpidcmdline, sizeof(procpidcmdline), "%s/cmdline", pidpath);
        i = readfile(procpidcmdline, buf, sizeof(buf));
        for (j=0; j<i-1; j++){
            if(buf[j]=='\0'){
                buf[j]=' ';
            }
        }
        ptr = cmdline;
        ptr2 = strchr(ptr, ',');
        if(ptr2){
            int ok = 1;
            char s[1024];
            int len = 0;
            while(ptr2){
                len = ptr2 - ptr;
                strncpy(s, ptr, len);
                s[len] = '\0';
                ok = ok && check_string(buf, s);
                ptr = ptr2+1;
                ptr2 = strchr(ptr, ',');
            }
            ok = ok && check_string(buf, ptr);
            return ok;
        } else {
            return check_string(buf, cmdline);
        }
    }
    return 1;
}

variable_types:
pidpath
char *

variable_types:
cmdline
char *

variable_types:
procpidcmdline
char [1024]

variable_types:
buf
char [1024]

variable_types: i int

variable_types: ok int

variable_types: len int


monitored

decompiled_code:
_BOOL8 __fastcall monitored(char *file, const char *a2, const char *a3, const char *a4, char *a5, size_t a6)
{
_BOOL8 result; // rax
int v10; // ebp
int v11; // edx
int v12; // eax
char *v13; // rdi
char *v14; // rax
struct stat v15; // [rsp+0h] [rbp-104B8h] BYREF
char s[1024]; // [rsp+90h] [rbp-10428h] BYREF
char s1[65576]; // [rsp+490h] [rbp-10028h] BYREF

result = check_cmdline(file, a4);
if ( result )
{
v10 = getzopeuid(a2);
v11 = stat(file, &v15);
result = 0LL;
if ( v11 != -1 )
{
if ( v15.st_uid == v10
&& (snprintf(s, 0x400uLL, "%s/environ", file),
v12 = readfile(s, s1, 0xFFFFuLL),
(v13 = scanbuf(s1, v12, a3)) != 0LL)
&& (v14 = strchr(v13, 61)) != 0LL )
{
strncpy(a5, v14 + 1, a6);
result = 1LL;
}
else
{
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(char *file, const char *a2, const char *a3, const char *a4, char *a5, size_t a6)
{
_BOOL8 result; // rax
int v10; // ebp
int v11; // edx
int v12; // eax
char *v13; // rdi
char *v14; // rax
struct stat v15; // [rsp+0h] [rbp-104B8h] BYREF
char s[1024]; // [rsp+90h] [rbp-10428h] BYREF
char s1[65576]; // [rsp+490h] [rbp-10028h] BYREF

result = func4(file, a4);
if ( result )
{
v10 = func2(a2);
v11 = stat(file, &v15);
result = 0LL;
if ( v11 != -1 )
{
if ( v15.st_uid == v10
&& (snprintf(s, 0x400uLL, "%s/environ", file),
v12 = func0(s, s1, 0xFFFFuLL),
(v13 = func1(s1, v12, a3)) != 0LL)
&& (v14 = strchr(v13, 61)) != 0LL )
{
strncpy(a5, v14 + 1, a6);
result = 1LL;
}
else
{
result = 0LL;
}
}
}
return result;
}

original_code:
int monitored(const char *pidpath, const char *zopeuser, const char *tag, const char* cmdline, char *name, size_t nlen){
    if(check_cmdline(pidpath, cmdline)){
        struct stat s_procpid;
        uid_t zopeuid = getzopeuid(zopeuser);
        if(stat(pidpath, &s_procpid)==-1){
            return 0;
        }
        if(s_procpid.st_uid == zopeuid){
            char procpidenv[1024];
            char env[65535];
            const char *ptr;
            int i;
            snprintf(procpidenv, sizeof(procpidenv), "%s/environ", pidpath);
            i = readfile(procpidenv, env, sizeof(env));
            if(ptr=scanbuf(env, i, tag)){
                if(ptr = strchr(ptr, '=')){
                    ptr++;
                    strncpy(name, ptr, nlen);
                    return 1;
                }
            }
        }
    }
    return 0;
}

canonical_original_code:
int monitored(char *pidpath, char *zopeuser, char *tag, char *cmdline, char *name, unsigned long nlen){
    if(check_cmdline(pidpath, cmdline)){
        struct stat s_procpid;
        unsigned int zopeuid = getzopeuid(zopeuser);
        if(stat(pidpath, &s_procpid)==-1){
            return 0;
        }
        if(s_procpid.st_uid == zopeuid){
            char procpidenv[1024];
            char env[65535];
            char *ptr;
            int i;
            snprintf(procpidenv, sizeof(procpidenv), "%s/environ", pidpath);
            i = readfile(procpidenv, env, sizeof(env));
            if(ptr=scanbuf(env, i, tag)){
                if(ptr = strchr(ptr, '=')){
                    ptr++;
                    strncpy(name, ptr, nlen);
                    return 1;
                }
            }
        }
    }
    return 0;
}

variable_types:
zopeuser
char *

variable_types:
tag
char *

variable_types:
name
char *

variable_types:
nlen
unsigned long

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

variable_types:
zopeuid
unsigned int

variable_types:
procpidenv
char [1024]

variable_types:
env
char [65535]

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


splitbuf

decompiled_code:
void __fastcall splitbuf(char *s, __int64 a2, char a3)
{
int v3; // ebp
char *v4; // rax

v3 = a3;
if ( s < &s[a2] )
{
do
{
v4 = strchr(s, v3);
if ( !v4 )
break;
*v4 = 0;
}
while ( &s[a2] > v4 + 1 );
}
}

canonical_decompiled_code:
void __fastcall func0(char *s, __int64 a2, char a3)
{
int v3; // ebp
char *v4; // rax

v3 = a3;
if ( s < &s[a2] )
{
do
{
v4 = strchr(s, v3);
if ( !v4 )
break;
*v4 = 0;
}
while ( &s[a2] > v4 + 1 );
}
}

original_code:
void splitbuf(char *buf, size_t count, char separator){
    char *ptr = buf;
    while((ptr < buf + count) && (ptr = strchr(buf, separator))){
        *ptr = '\0';
        ptr++;
    }
}

canonical_original_code:
void splitbuf(char *buf, unsigned long count, char separator){
    char *ptr = buf;
    while((ptr < buf + count) && (ptr = strchr(buf, separator))){
        *ptr = '\0';
        ptr++;
    }
}

variable_types:
buf
char *

variable_types:
count
unsigned long

variable_types: separator char

variable_types:
ptr
char *


getuptime

decompiled_code:
__int64 getuptime()
{
const char *v0; // rbx
__int64 v1; // rbp
char *v2; // rax
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

readfile("/proc/stat", v4, 1024LL);
splitbuf(v4, 1024LL, 10);
v0 = (const char *)scanbuf(v4, 1024LL, "cpu ");
v1 = 0LL;
while ( 1 )
{
v2 = strchr(v0, 32);
if ( !v2 )
break;
v0 = v2 + 1;
if ( (v2[1] & 0xDF) != 0 )
v1 += atoi(v2 + 1);
}
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
const char *v0; // rbx
__int64 v1; // rbp
char *v2; // rax
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

readfile("/proc/stat", v4, 1024LL);
func0(v4, 1024LL, 10);
v0 = (const char *)scanbuf(v4, 1024LL, "cpu ");
v1 = 0LL;
while ( 1 )
{
v2 = strchr(v0, 32);
if ( !v2 )
break;
v0 = v2 + 1;
if ( (v2[1] & 0xDF) != 0 )
v1 += atoi(v2 + 1);
}
return v1;
}

original_code:
unsigned long int getuptime(){
    char buf[1024];
    const char *ptr;
    unsigned long int total;
    readfile("/proc/stat", buf, sizeof(buf));
    splitbuf(buf, sizeof(buf), '\n');
    ptr = scanbuf(buf, sizeof(buf), "cpu ");
    total = 0;
    while(ptr = strchr(ptr, ' ')){
        ptr++;
        if((*ptr != '\0') && (*ptr!=' ')){
            total+=atoi(ptr);
        }
    }
    return total;
}

canonical_original_code:
unsigned long getuptime(){
    char buf[1024];
    char *ptr;
    unsigned long total;
    readfile("/proc/stat", buf, sizeof(buf));
    splitbuf(buf, sizeof(buf), '\n');
    ptr = scanbuf(buf, sizeof(buf), "cpu ");
    total = 0;
    while(ptr = strchr(ptr, ' ')){
        ptr++;
        if((*ptr != '\0') && (*ptr!=' ')){
            total+=atoi(ptr);
        }
    }
    return total;
}

variable_types:
total
unsigned long

return_type: unsigned long


cpuusage

decompiled_code:
__int64 __fastcall cpuusage(unsigned int a1)
{
__int64 v2; // [rsp+0h] [rbp-1418h] BYREF
__int64 v3; // [rsp+8h] [rbp-1410h] BYREF
char v4[4096]; // [rsp+10h] [rbp-1408h] BYREF
char s[1032]; // [rsp+1010h] [rbp-408h] BYREF

snprintf(s, 0x400uLL, "/proc/%d/stat", a1);
readfile(s, v4, 4096LL);
_isoc99_sscanf(
v4,
"%*d %*s %*c %*d %*d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %*ld %*ld %*ld %*lu %*lu %*ld %*lu %"
"*lu %*lu %*lu %*lu %*lu %*u %*u %*u %*u %*lu %*lu %*lu",
&v3,
&v2);
return v3 + v2;
}

original_code:
unsigned long int cpuusage(pid_t pid){
    char procpidstat[1024];
    char buf[4096];
    unsigned long int utime, stime;
    snprintf(procpidstat, sizeof(procpidstat), "/proc/%d/stat", pid);
    readfile(procpidstat, buf, sizeof(buf));
    sscanf(buf,
            "%*d "
            "%*s "
            "%*c "
            "%*d %*d %*d %*d %*d "
            "%*lu %*lu %*lu %*lu %*lu %lu %lu "
            "%*ld %*ld %*ld %*ld %*ld %*ld "
            "%*lu %*lu "
            "%*ld "
            "%*lu %*lu %*lu %*lu %*lu %*lu "
            "%*u %*u %*u %*u "
            "%*lu %*lu %*lu",
            &utime, &stime);
    return utime+stime;
}

canonical_original_code:
unsigned long cpuusage(int pid){
    char procpidstat[1024];
    char buf[4096];
    unsigned long utime; unsigned long stime;
    snprintf(procpidstat, sizeof(procpidstat), "/proc/%d/stat", pid);
    readfile(procpidstat, buf, sizeof(buf));
    sscanf(buf,
            "%*d "
            "%*s "
            "%*c "
            "%*d %*d %*d %*d %*d "
            "%*lu %*lu %*lu %*lu %*lu %lu %lu "
            "%*ld %*ld %*ld %*ld %*ld %*ld "
            "%*lu %*lu "
            "%*ld "
            "%*lu %*lu %*lu %*lu %*lu %*lu "
            "%*u %*u %*u %*u "
            "%*lu %*lu %*lu",
            &utime, &stime);
    return utime+stime;
}

variable_types: pid int

variable_types:
procpidstat
char [1024]

variable_types:
buf
char [4096]

variable_types:
utime
unsigned long

variable_types:
stime
unsigned long


writecache

decompiled_code:
void __fastcall writecache(const char *a1, __int64 a2, __int64 a3)
{
int v4; // eax
int v5; // er12
int i; // ebp
int v7; // ebx
char v8[1048]; // [rsp+0h] [rbp-418h] BYREF

v4 = open(a1, 65, 384LL);
if ( v4 == -1 )
{
perror("open");
}
else
{
v5 = v4;
for ( i = snprintf(v8, 0x400uLL, "%lu %lu\n", a2, a3); i > 0; i -= v7 )
{
v7 = write(v5, v8, i);
if ( v7 == -1 )
perror("write");
}
close(v5);
}
}

canonical_decompiled_code:
void __fastcall func3(const char *a1, __int64 a2, __int64 a3)
{
int v4; // eax
int v5; // er12
int i; // ebp
int v7; // ebx
char v8[1048]; // [rsp+0h] [rbp-418h] BYREF

v4 = open(a1, 65, 384LL);
if ( v4 == -1 )
{
perror("open");
}
else
{
v5 = v4;
for ( i = snprintf(v8, 0x400uLL, "%lu %lu\n", a2, a3); i > 0; i -= v7 )
{
v7 = write(v5, v8, i);
if ( v7 == -1 )
perror("write");
}
close(v5);
}
}

original_code:
void writecache(const char *cachefile, unsigned long int uptime, unsigned long int usage){
    int fd = open(cachefile, 01 | 0100, 0400 | 0200);
    if(fd!=-1){
        char buf[1024];
        int n, i;
        n = snprintf(buf, sizeof(buf), "%lu %lu\n", uptime, usage);
        while(n>0){
            if((i = write(fd, buf, n))==-1){
                perror("write");
            }
            n -= i;
        }
        close(fd);
    } else {
        perror("open");
    }
}

canonical_original_code:
void writecache(char *cachefile, unsigned long uptime, unsigned long usage){
    int fd = open(cachefile, 01 | 0100, 0400 | 0200);
    if(fd!=-1){
        char buf[1024];
        int n; int i;
        n = snprintf(buf, sizeof(buf), "%lu %lu\n", uptime, usage);
        while(n>0){
            if((i = write(fd, buf, n))==-1){
                perror("write");
            }
            n -= i;
        }
        close(fd);
    } else {
        perror("open");
    }
}

variable_types:
cachefile
char *

variable_types:
uptime
unsigned long

variable_types:
usage
unsigned long

variable_types: fd int

variable_types:
buf
char [1024]

variable_types: n int

variable_types: i int


readcache

decompiled_code:
__int64 __fastcall readcache(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 result; // rax
_BYTE v5[1048]; // [rsp+0h] [rbp-418h] BYREF

*a3 = 0LL;
*a2 = 0LL;
result = readfile(a1, v5, 1024LL);
if ( (_DWORD)result != -1 )
result = _isoc99_sscanf(v5, "%lu %lu", a2, a3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 result; // rax
_BYTE v5[1048]; // [rsp+0h] [rbp-418h] BYREF

*a3 = 0LL;
*a2 = 0LL;
result = readfile(a1, v5, 1024LL);
if ( (_DWORD)result != -1 )
result = _isoc99_sscanf(v5, "%lu %lu", a2, a3);
return result;
}

original_code:
void readcache(const char *cachefile, unsigned long int *uptime, unsigned long int *usage){
    char buf[1024];
    *uptime = *usage = 0;
    if(readfile(cachefile, buf, sizeof(buf))!=-1){
        sscanf(buf, "%lu %lu", uptime, usage);
    }
}

canonical_original_code:
void readcache(char *cachefile, unsigned long *uptime, unsigned long *usage){
    char buf[1024];
    *uptime = *usage = 0;
    if(readfile(cachefile, buf, sizeof(buf))!=-1){
        sscanf(buf, "%lu %lu", uptime, usage);
    }
}

variable_types:
uptime
unsigned long *

variable_types:
usage
unsigned long *


memusage

decompiled_code:
__int64 __fastcall memusage(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-810h] BYREF
char v3[1024]; // [rsp+10h] [rbp-808h] BYREF
char s[1032]; // [rsp+410h] [rbp-408h] BYREF

snprintf(s, 0x400uLL, "%s/statm", a1);
memset(v3, 0, sizeof(v3));
readfile(s, v3, 1024LL);
_isoc99_sscanf(v3, "%ld %*ld %*ld %*ld %*ld %*ld %*ld", &v2);
return v2 * getpagesize();
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
__int64 v2; // [rsp+8h] [rbp-810h] BYREF
char v3[1024]; // [rsp+10h] [rbp-808h] BYREF
char s[1032]; // [rsp+410h] [rbp-408h] BYREF

snprintf(s, 0x400uLL, "%s/statm", a1);
memset(v3, 0, sizeof(v3));
readfile(s, v3, 1024LL);
_isoc99_sscanf(v3, "%ld %*ld %*ld %*ld %*ld %*ld %*ld", &v2);
return v2 * getpagesize();
}

original_code:
unsigned long int memusage(const char *pidpath){
    char procpidstatm[1024];
    char buf[1024];
    int i;
    unsigned long int vsz;
    snprintf(procpidstatm, sizeof(procpidstatm), "%s/statm", pidpath);
    bzero(buf, sizeof(buf));
    i = readfile(procpidstatm, buf, sizeof(buf));
    sscanf(buf, "%ld %*ld %*ld %*ld %*ld %*ld %*ld", &vsz);
    return getpagesize()*vsz;
}

canonical_original_code:
unsigned long memusage(char *pidpath){
    char procpidstatm[1024];
    char buf[1024];
    int i;
    unsigned long vsz;
    snprintf(procpidstatm, sizeof(procpidstatm), "%s/statm", pidpath);
    bzero(buf, sizeof(buf));
    i = readfile(procpidstatm, buf, sizeof(buf));
    sscanf(buf, "%ld %*ld %*ld %*ld %*ld %*ld %*ld", &vsz);
    return getpagesize()*vsz;
}

variable_types:
pidpath
char *

variable_types:
procpidstatm
char [1024]

variable_types:
vsz
unsigned long


usage

decompiled_code:
size_t __fastcall usage(const char *a1)
{
fprintf(stderr, "Usage: %s [-O | --owner=username] [-T | --tag=tag] [-V | --target=tagname]\n", a1);
fwrite(" [-W | --warning=integer] [-C | --critical=integer]\n", 1uLL, 0x3DuLL, stderr);
return fwrite(" [-P | --cmdline=path] [-H | --help]\n", 1uLL, 0x2EuLL, stderr);
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
fprintf(stderr, "Usage: %s [-O | --owner=username] [-T | --tag=tag] [-V | --target=tagname]\n", a1);
fwrite(" [-W | --warning=integer] [-C | --critical=integer]\n", 1uLL, 0x3DuLL, stderr);
return fwrite(" [-P | --cmdline=path] [-H | --help]\n", 1uLL, 0x2EuLL, stderr);
}

original_code:
void usage(const char *name){
    fprintf(stderr, "Usage: %s [-O | --owner=username] [-T | --tag=tag] [-V | --target=tagname]\n", name);
    fprintf(stderr, "          [-W | --warning=integer] [-C | --critical=integer]\n");
    fprintf(stderr, "          [-P | --cmdline=path] [-H | --help]\n");
}

canonical_original_code:
void usage(char *name){
    fprintf(stderr, "Usage: %s [-O | --owner=username] [-T | --tag=tag] [-V | --target=tagname]\n", name);
    fprintf(stderr, "          [-W | --warning=integer] [-C | --critical=integer]\n");
    fprintf(stderr, "          [-P | --cmdline=path] [-H | --help]\n");
}

variable_types:
name
char *


printmessage

decompiled_code:
int __fastcall printmessage(const char *a1, const char *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rcx
int v4; // eax
unsigned __int64 v5; // rdx
__int64 v7[5]; // [rsp+0h] [rbp-28h]

v3 = a3;
v7[0] = (__int64)"b";
v7[1] = (__int64)"K";
v7[2] = (__int64)"M";
if ( a3 <= 0x400 )
{
v4 = 0;
}
else
{
v4 = 0;
do
{
v5 = v3;
v3 >>= 10;
++v4;
}
while ( v4 <= 1 && v5 > 0x1003FF );
}
return printf("%s memory %s: %lu %s\n", a1, a2, v3, (const char *)v7[v4]);
}

original_code:
void printmessage(const char *name, const char *stat, unsigned long int u){
    int i = 0;
    char *units[] = {"b", "K", "M"};
    while((i < 2) && (u > 1024)){
        u /= 1024;
        i++;
    }
    printf("%s memory %s: %lu %s\n", name, stat, u, units[i]);
}

canonical_original_code:
void printmessage(char *name, char *stat, unsigned long u){
    int i = 0;
    char *units[] = {"b", "K", "M"};
    while((i < 2) && (u > 1024)){
        u /= 1024;
        i++;
    }
    printf("%s memory %s: %lu %s\n", name, stat, u, units[i]);
}

variable_types:
stat
char *

variable_types:
u
unsigned long

variable_types:
units
char *[]