getopts

decompiled_code:
int __fastcall getopts(int argc, char **argv)
{
int result; // eax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
result = getopt_long(argc, argv, "i:I:p:hvV", &long_options_1, longind);
if ( result == -1 )
break;
if ( result )
print_usage();
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int argc, char **argv)
{
int result; // eax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
result = getopt_long(argc, argv, "i:I:p:hvV", &long_options_1, longind);
if ( result == -1 )
break;
if ( result )
func3();
}
return result;
}

original_code:
static inline void getopts(int argc, char **argv) {
  int c;
  while(1) {
    static struct option long_options[] = {
      {"verbose", 0, &verbose, 1},
      {"quiet", 0, &verbose, 0},
      {"ip-list", 1, 0, 'i'},
      {"ip-search", 1, 0, 'I'},
      {"match-position", 1, 0, 'p'},
      {"invert-match", 0, 0, 'v'},
      {"version", 0, 0, 'V'},
      {"help", 0, 0, 'h'},
      {"dump-ips", 0, &debuglvl, (int) DebugTree},
      {0,0,0,0}
    };
    int opt_index;
    c = getopt_long(argc, argv, "i:I:p:hvV", long_options, &opt_index);
    if(c == -1)
      break;
    switch(c) {
      case 0:
      break;
      case 'i':
      files = ((files == 0) ? list_make_cpy(optarg) : list_append_cpy(files, optarg));
      break;
      case 'p':
      search_ippos = atoi(optarg);
      break;
      case 'v':
      search_invertmatch = 1;
      case 'V':
      print_version();
      break;
      case 'I':
      ips = ((ips == 0) ? list_make_cpy(optarg) : list_append_cpy(ips, optarg));
      break;
      case 'h':
      print_usage();
      break;
      default:
      print_usage();
    }
  }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


decompiled_code:
void __fastcall list_free(void *ptr)
{
_QWORD *v1; // rbx
void **v2; // rbp

if ( ptr )
{
do
{
v1 = ptr;
ptr = (void *)*((_QWORD *)ptr + 2);
}
while ( ptr );
v2 = (void **)v1[3];
if ( v2 )
{
while ( 1 )
{
if ( *((_DWORD *)v1 + 2) )
free((void *)*v1);
free(v1);
v1 = v2;
if ( !v2[3] )
break;
v2 = (void **)v2[3];
}
}
else
{
v2 = (void **)v1;
}
if ( *((_DWORD *)v2 + 2) )
free(*v2);
free(v2);
}
}

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

if ( ptr )
{
do
{
v1 = ptr;
ptr = (void *)*((_QWORD *)ptr + 2);
}
while ( ptr );
v2 = (void **)v1[3];
if ( v2 )
{
while ( 1 )
{
if ( *((_DWORD *)v1 + 2) )
free((void *)*v1);
free(v1);
v1 = v2;
if ( !v2[3] )
break;
v2 = (void **)v2[3];
}
}
else
{
v2 = (void **)v1;
}
if ( *((_DWORD *)v2 + 2) )
free(*v2);
free(v2);
}
}

original_code:
static inline void list_free(ListRef list) {
  if(!list)
    return;
  while (list->next != 0)
    list = list->next;
  ListRef tmp;
  while(list->prev != 0) {
    tmp = list;
    list = list->prev;
    if(tmp->free_value)
      free(tmp->value);
    free(tmp);
  }
  if(list->free_value)
    free(list->value);
  free(list);
}

canonical_original_code:
static inline void list_free(struct List *list) {
  if(!list)
    return;
  while (list->next != 0)
    list = list->next;
  struct List *tmp;
  while(list->prev != 0) {
    tmp = list;
    list = list->prev;
    if(tmp->free_value)
      free(tmp->value);
    free(tmp);
  }
  if(list->free_value)
    free(list->value);
  free(list);
}

variable_types: struct List { void *value; int free_value; struct List *next; struct List *prev; } *

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 1 )
print_usage(argc, argv, envp);
iptree = makeiptree(argc, argv, envp);
buffer = aio_buffer_alloc();
getopts(argc, (char **)argv);
list_each(files, (__int64 (__fastcall *)(_QWORD))loadlist);
list_free(files);
files = 0LL;
list_each(ips, (__int64 (__fastcall *)(_QWORD))loadip);
list_free(ips);
ips = 0LL;
if ( (unsigned int)iptree_empty(iptree) && verbose )
fwrite("Warning: no IP blocks have been loaded.\n", 1uLL, 0x28uLL, stderr);
if ( debuglvl == 1 )
{
dumptree(iptree);
exit(0);
}
work(iptree);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc == 1 )
func3(argc, argv, envp);
iptree = makeiptree(argc, argv, envp);
buffer = aio_buffer_alloc();
func6(argc, (char **)argv);
func0(files, (__int64 (__fastcall *)(_QWORD))loadlist);
func7(files);
files = 0LL;
func0(ips, (__int64 (__fastcall *)(_QWORD))loadip);
func7(ips);
ips = 0LL;
if ( (unsigned int)iptree_empty(iptree) && verbose )
fwrite("Warning: no IP blocks have been loaded.\n", 1uLL, 0x28uLL, stderr);
if ( debuglvl == 1 )
{
dumptree(iptree);
exit(0);
}
func8(iptree);
return 0;
}

original_code:
int main(int argc, char **argv) {
  if(argc == 1)
    print_usage();
  iptree = makeiptree();
  buffer = aio_buffer_alloc();
  getopts(argc, argv);
  list_each(files, &loadlist);
  list_free(files); files = 0;
  list_each(ips, &loadip);
  list_free(ips); ips = 0;
  if(iptree_empty(iptree) && verbose)
    fprintf(stderr, "Warning: no IP blocks have been loaded.\n");
  switch(debuglvl) {
    case DebugTree:
    dumptree(iptree);
    exit(0);
  }
  work(iptree, 0);
  return 0;
}

return_type: int


validateip

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

result = 0LL;
if ( (a1 & (-1 << (32 - a2))) != a1 )
result = 4294966295LL;
return result;
}

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

result = 0LL;
if ( (a1 & (-1 << (32 - a2))) != a1 )
result = 4294966295LL;
return result;
}

original_code:
static int validateip(ip_t ip, int cidr) {
  if(ip > 0xffffffff)
    return -1002;
  if((ip & 0xffffffff & (0xffffffff << (32 - cidr))) != ip)
    return -1001;
  return 0;
}

canonical_original_code:
static int validateip(unsigned int ip, int cidr) {
  if(ip > 0xffffffff)
    return -1002;
  if((ip & 0xffffffff & (0xffffffff << (32 - cidr))) != ip)
    return -1001;
  return 0;
}

variable_types:
ip
unsigned int

variable_types: cidr int


xmalloc

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

result = malloc(a1);
if ( !a1 )
_assert_fail("size != 0", "common.h", 0x11u, "xmalloc");
if ( !result )
{
fwrite("malloc call failed - this is pretty bad\n", 1uLL, 0x28uLL, stderr);
exit(-2);
}
return result;
}

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

result = malloc(a1);
if ( !a1 )
_assert_fail("size != 0", "common.h", 0x11u, "xmalloc");
if ( !result )
{
fwrite("malloc call failed - this is pretty bad\n", 1uLL, 0x28uLL, stderr);
exit(-2);
}
return result;
}

original_code:
inline static void *xmalloc(size_t size) {
    void *ptr = malloc(size);
    ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "common.h", 17, __extension__ __PRETTY_FUNCTION__); }));
    if(!ptr) {
        fprintf(stderr, "malloc call failed - this is pretty bad\n");
        exit(-2);
    }
    return ptr;
}

canonical_original_code:
inline static void *xmalloc(unsigned long size) {
    void *ptr = malloc(size);
    ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "common.h", 17, __extension__ __PRETTY_FUNCTION__); }));
    if(!ptr) {
        fprintf(stderr, "malloc call failed - this is pretty bad\n");
        exit(-2);
    }
    return ptr;
}

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


xrealloc

decompiled_code:
void *__fastcall xrealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !a2 )
_assert_fail("size != 0", "common.h", 0x20u, "xrealloc");
if ( !result )
{
fwrite("realloc failed - that's not good at all\n", 1uLL, 0x28uLL, stderr);
exit(-2);
}
return result;
}

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

result = realloc(a1, a2);
if ( !a2 )
_assert_fail("size != 0", "common.h", 0x20u, "xrealloc");
if ( !result )
{
fwrite("realloc failed - that's not good at all\n", 1uLL, 0x28uLL, stderr);
exit(-2);
}
return result;
}

original_code:
inline static void *xrealloc(void *ptr, size_t size) {
  ptr = realloc(ptr, size);
  ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "common.h", 32, __extension__ __PRETTY_FUNCTION__); }));
  if(!ptr) {
    fprintf(stderr, "realloc failed - that's not good at all\n");
    exit(-2);
  }
  return ptr;
}

canonical_original_code:
inline static void *xrealloc(void *ptr, unsigned long size) {
  ptr = realloc(ptr, size);
  ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "common.h", 32, __extension__ __PRETTY_FUNCTION__); }));
  if(!ptr) {
    fprintf(stderr, "realloc failed - that's not good at all\n");
    exit(-2);
  }
  return ptr;
}


freenode

decompiled_code:
void __fastcall freenode(void *ptr)
{
if ( ptr != &zero && ptr != &full )
{
freenode(*(void **)ptr);
freenode(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
if ( ptr != &zero && ptr != &full )
{
func5(*(void **)ptr);
func5(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
static void freenode(IPNodeRef node) {
  if((node == (&_full) || node == (&_zero)))
    return;
  freenode(node->children[0]);
  freenode(node->children[1]);
  free(node);
}

canonical_original_code:
static void freenode(struct IPNode *node) {
  if((node == (&_full) || node == (&_zero)))
    return;
  freenode(node->children[0]);
  freenode(node->children[1]);
  free(node);
}

variable_types: struct IPNode { struct IPNode *children[2]; } *


decompiled_code:
void __fastcall node_insert(void **a1, unsigned int a2, int a3, unsigned int a4)
{
void **v4; // rbp

v4 = (void **)*a1;
if ( *a1 != &full )
{
if ( a3 <= (int)a4 )
{
if ( v4 != (void **)&zero )
freenode(*a1);
*a1 = &full;
}
else
{
if ( v4 == (void **)&zero )
{
v4 = (void **)xmalloc(0x10uLL);
*v4 = &zero;
v4[1] = &zero;
*a1 = v4;
}
node_insert(&v4[(a2 >> a3) & 1], a2, (unsigned int)(a3 - 1), a4);
if ( *v4 == &full && v4[1] == &full )
{
free(v4);
*a1 = &full;
}
}
}
}

canonical_decompiled_code:
void __fastcall func6(void **a1, unsigned int a2, int a3, unsigned int a4)
{
void **v4; // rbp

v4 = (void **)*a1;
if ( *a1 != &full )
{
if ( a3 <= (int)a4 )
{
if ( v4 != (void **)&zero )
func5(*a1);
*a1 = &full;
}
else
{
if ( v4 == (void **)&zero )
{
v4 = (void **)func2(0x10uLL);
*v4 = &zero;
v4[1] = &zero;
*a1 = v4;
}
func6(&v4[(a2 >> a3) & 1], a2, (unsigned int)(a3 - 1), a4);
if ( *v4 == &full && v4[1] == &full )
{
free(v4);
*a1 = &full;
}
}
}
}

original_code:
static void node_insert(IPNodeRef *node_p, ip_t ip, int bit, int end) {
  IPNodeRef node;
  node = *node_p;
  if(node == (&_full))
    return;
  if(bit <= end) {
    if(node != (&_zero)) freenode(node);
    *node_p = (&_full);
    return;
  }
  if(node == (&_zero)) {
    node = xmalloc(sizeof(struct IPNode));
    node->children[0] = (&_zero);
    node->children[1] = (&_zero);
    *node_p = node;
  }
  node_insert(&node->children[(ip >> bit) & 1], ip, bit -1, end);
  if((node->children[0] == (&_full)) && (node->children[1] == (&_full))) {
    free(node);
    *node_p = (&_full);
  }
}

canonical_original_code:
static void node_insert(struct IPNode **node_p, unsigned int ip, int bit, int end) {
  struct IPNode *node;
  node = *node_p;
  if(node == (&_full))
    return;
  if(bit <= end) {
    if(node != (&_zero)) freenode(node);
    *node_p = (&_full);
    return;
  }
  if(node == (&_zero)) {
    node = xmalloc(sizeof(struct IPNode));
    node->children[0] = (&_zero);
    node->children[1] = (&_zero);
    *node_p = node;
  }
  node_insert(&node->children[(ip >> bit) & 1], ip, bit -1, end);
  if((node->children[0] == (&_full)) && (node->children[1] == (&_full))) {
    free(node);
    *node_p = (&_full);
  }
}

variable_types: struct IPNode { struct IPNode *children[2]; } **

variable_types: bit int

variable_types: end int


dumpip

decompiled_code:
int __fastcall dumpip(unsigned int a1, unsigned int a2)
{
return printf("%u.%u.%u.%u/%d\n", HIBYTE(a1), BYTE2(a1), BYTE1(a1), (unsigned __int8)a1, a2);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, unsigned int a2)
{
return printf("%u.%u.%u.%u/%d\n", HIBYTE(a1), BYTE2(a1), BYTE1(a1), (unsigned __int8)a1, a2);
}

original_code:
static inline void dumpip(ip_t ip, int cidr) {
  printf("%u.%u.%u.%u/%d\n", ip >> 24, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, cidr);
}

canonical_original_code:
static inline void dumpip(unsigned int ip, int cidr) {
  printf("%u.%u.%u.%u/%d\n", ip >> 24, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, cidr);
}


dumpnode

decompiled_code:
int __fastcall dumpnode(_QWORD *a1, __int64 a2, int a3)
{
char v3; // r12
unsigned int v4; // er13
int result; // eax

if ( a1 != (_QWORD *)&zero )
{
v3 = a3;
if ( a1 == (_QWORD *)&full )
{
result = dumpip(a2, 31 - a3);
}
else
{
v4 = a3 - 1;
dumpnode(*a1, a2, (unsigned int)(a3 - 1));
a2 = (unsigned int)a2;
LODWORD(a2) = a2 | (1 << v3);
result = dumpnode(a1[1], a2, v4);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(_QWORD *a1, __int64 a2, int a3)
{
char v3; // r12
unsigned int v4; // er13
int result; // eax

if ( a1 != (_QWORD *)&zero )
{
v3 = a3;
if ( a1 == (_QWORD *)&full )
{
result = func7(a2, 31 - a3);
}
else
{
v4 = a3 - 1;
func8(*a1, a2, (unsigned int)(a3 - 1));
a2 = (unsigned int)a2;
LODWORD(a2) = a2 | (1 << v3);
result = func8(a1[1], a2, v4);
}
}
return result;
}

original_code:
static void dumpnode(IPNodeRef node, ip_t ip, int bit) {
  if(node == (&_zero))
    return;
  if(node == (&_full)) {
    dumpip(ip, 31 - bit);
    return;
  }
  dumpnode(node->children[0], ip, bit - 1);
  dumpnode(node->children[1], ip | (1 << bit), bit - 1);
}

canonical_original_code:
static void dumpnode(struct IPNode *node, unsigned int ip, int bit) {
  if(node == (&_zero))
    return;
  if(node == (&_full)) {
    dumpip(ip, 31 - bit);
    return;
  }
  dumpnode(node->children[0], ip, bit - 1);
  dumpnode(node->children[1], ip | (1 << bit), bit - 1);
}

user_defined_types: struct IPNode { struct IPNode *children[2]; }


makeiptree

decompiled_code:
_QWORD *makeiptree()
{
_QWORD *result; // rax

result = xmalloc(8uLL);
*result = &zero;
return result;
}

canonical_decompiled_code:
_QWORD *func9()
{
_QWORD *result; // rax

result = func2(8uLL);
*result = &zero;
return result;
}

original_code:
IPTreeRef makeiptree() {
  IPTreeRef _tree = (IPTreeRef) xmalloc(sizeof(struct IPTree));
  _tree->root = (&_zero);
  return _tree;
}

canonical_original_code:
struct IPTree *makeiptree() {
  struct IPTree *_tree = (IPTreeRef) xmalloc(sizeof(struct IPTree));
  _tree->root = (&_zero);
  return _tree;
}

variable_types: struct IPTree { struct IPNode { struct IPNode *children[2]; } *root; } *


addip

decompiled_code:
__int64 __fastcall addip(void **a1, unsigned int a2, int a3)
{
unsigned int v4; // ebx

v4 = validateip(a2, a3);
if ( !v4 )
node_insert(a1, a2, 31, 31 - a3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(void **a1, unsigned int a2, int a3)
{
unsigned int v4; // ebx

v4 = func0(a2, a3);
if ( !v4 )
func6(a1, a2, 31, 31 - a3);
return v4;
}

original_code:
int addip(IPTreeRef tree, ip_t ip, int block) {
  int res;
  if((res = validateip(ip, block)) != 0)
    return res;
  node_insert(&(tree->root), ip, 31, 31 - block);
  return 0;
}

canonical_original_code:
int addip(struct IPTree *tree, unsigned int ip, int block) {
  int res;
  if((res = validateip(ip, block)) != 0)
    return res;
  node_insert(&(tree->root), ip, 31, 31 - block);
  return 0;
}

variable_types: block int

variable_types: res int


decompiled_code:
__int64 __fastcall addip_str(void **a1, unsigned __int8 *a2, unsigned __int64 a3)
{
__int64 result; // rax
int *v4; // [rsp+0h] [rbp-18h] BYREF
unsigned int *v5; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)detectip_str(a2, a3, &v5, &v4) )
result = addip(a1, *v5, *v4);
else
result = 4294966196LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(void **a1, unsigned __int8 *a2, unsigned __int64 a3)
{
__int64 result; // rax
int *v4; // [rsp+0h] [rbp-18h] BYREF
unsigned int *v5; // [rsp+8h] [rbp-10h] BYREF

if ( (unsigned int)func4(a2, a3, &v5, &v4) )
result = func10(a1, *v5, *v4);
else
result = 4294966196LL;
return result;
}

original_code:
int addip_str(IPTreeRef tree, char *data, const char *end) {
  ip_t *ips;
  int *blocks;
  int count;
  if((count = detectip_str(data, end, &ips, &blocks)) == 0)
    return -1100;
  return addip(tree, ips[0], blocks[0]);
}

canonical_original_code:
int addip_str(struct IPTree *tree, char *data, char *end) {
  unsigned int *ips;
  int *blocks;
  int count;
  if((count = detectip_str(data, end, &ips, &blocks)) == 0)
    return -1100;
  return addip(tree, ips[0], blocks[0]);
}

variable_types:
data
char *

variable_types:
end
char *

variable_types:
ips
unsigned int *

variable_types:
blocks
int *

variable_types: count int


findip

decompiled_code:
__int64 __fastcall findip(_QWORD **a1, __int64 a2)
{
return node_search(*a1, a2, 31);
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD **a1, __int64 a2)
{
return func1(*a1, a2, 31);
}

original_code:
int findip(IPTreeRef tree, ip_t ip) {
  return node_search(tree->root, ip, 31);
}

canonical_original_code:
int findip(struct IPTree *tree, unsigned int ip) {
  return node_search(tree->root, ip, 31);
}


decompiled_code:
__int64 __fastcall findip_str(_QWORD **a1, unsigned __int8 *a2, unsigned __int64 a3, int a4)
{
int v5; // eax
unsigned int *v6; // rbx
__int64 v7; // r12
__int64 result; // rax
int v9; // edx
__int64 v10; // [rsp+0h] [rbp-28h] BYREF
unsigned int *v11; // [rsp+8h] [rbp-20h] BYREF

v5 = detectip_str(a2, a3, &v11, &v10);
if ( !v5 )
return 4294966196LL;
if ( a4 )
{
v9 = v5 - a4;
if ( a4 > 0 )
v9 = a4 - 1;
if ( v9 >= v5 )
result = 4294966195LL;
else
result = findip(a1, v11[v9]);
}
else if ( v5 <= 0 )
{
result = 0LL;
}
else
{
v6 = v11;
v7 = (__int64)&v11[v5 - 1 + 1];
do
{
result = findip(a1, *v6);
if ( (_DWORD)result )
break;
++v6;
}
while ( v6 != (unsigned int *)v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(_QWORD **a1, unsigned __int8 *a2, unsigned __int64 a3, int a4)
{
int v5; // eax
unsigned int *v6; // rbx
__int64 v7; // r12
__int64 result; // rax
int v9; // edx
__int64 v10; // [rsp+0h] [rbp-28h] BYREF
unsigned int *v11; // [rsp+8h] [rbp-20h] BYREF

v5 = func4(a2, a3, &v11, &v10);
if ( !v5 )
return 4294966196LL;
if ( a4 )
{
v9 = v5 - a4;
if ( a4 > 0 )
v9 = a4 - 1;
if ( v9 >= v5 )
result = 4294966195LL;
else
result = func12(a1, v11[v9]);
}
else if ( v5 <= 0 )
{
result = 0LL;
}
else
{
v6 = v11;
v7 = (__int64)&v11[v5 - 1 + 1];
do
{
result = func12(a1, *v6);
if ( (_DWORD)result )
break;
++v6;
}
while ( v6 != (unsigned int *)v7 );
}
return result;
}

original_code:
int findip_str(IPTreeRef tree, char *data, const char *end, int pos) {
  ip_t *ips;
  int *blocks;
  int count;
  int idx;
  int res = 0;
  if((count = detectip_str(data, end, &ips, &blocks)) == 0)
    return -1100;
  if(pos == 0) {
    for(idx = 0; idx < count; ++idx) {
      if((res = findip(tree, ips[idx])))
        return res;
    }
    return 0;
  } else {
    idx = (pos > 0 ? pos - 1: count - pos);
    if(idx >= count)
      return -1101;
    return findip(tree, ips[idx]);
  }
}

canonical_original_code:
int findip_str(struct IPTree *tree, char *data, char *end, int pos) {
  unsigned int *ips;
  int *blocks;
  int count;
  int idx;
  int res = 0;
  if((count = detectip_str(data, end, &ips, &blocks)) == 0)
    return -1100;
  if(pos == 0) {
    for(idx = 0; idx < count; ++idx) {
      if((res = findip(tree, ips[idx])))
        return res;
    }
    return 0;
  } else {
    idx = (pos > 0 ? pos - 1: count - pos);
    if(idx >= count)
      return -1101;
    return findip(tree, ips[idx]);
  }
}

variable_types: pos int

variable_types: idx int


dumptree

decompiled_code:
int __fastcall dumptree(_QWORD **a1)
{
return dumpnode(*a1, 0LL, 31);
}

canonical_decompiled_code:
int __fastcall func14(_QWORD **a1)
{
return func8(*a1, 0LL, 31);
}

original_code:
void dumptree(IPTreeRef tree) {
  dumpnode(tree->root, 0, 31);
}

canonical_original_code:
void dumptree(struct IPTree *tree) {
  dumpnode(tree->root, 0, 31);
}


decompiled_code:
_BOOL8 __fastcall iptree_empty(_QWORD *a1)
{
return *a1 == (_QWORD)&zero;
}

canonical_decompiled_code:
_BOOL8 __fastcall func15(_QWORD *a1)
{
return *a1 == (_QWORD)&zero;
}

original_code:
int iptree_empty(IPTreeRef tree) {
  return (tree->root == (&_zero));
}

canonical_original_code:
int iptree_empty(struct IPTree *tree) {
  return (tree->root == (&_zero));
}


decompiled_code:
__int64 __fastcall read_file(void **a1, _DWORD *a2)
{
__int64 i; // rbp
unsigned int v4; // ebx
void *v5; // rdi
void *v7; // rax

*a1 = malloc(0LL);
for ( i = 0LL; !ferror(stdin); i += fread(v5, 1uLL, 0x1000uLL, stdin) )
{
v4 = feof(stdin);
if ( v4 )
break;
v5 = realloc(*a1, i + 4096);
*a1 = v5;
if ( !v5 )
{
*a2 = 1;
return v4;
}
}
if ( i )
return (unsigned int)i;
v7 = malloc(0x1000uLL);
if ( (*a1 = v7) != 0LL )
return (unsigned int)i;
*a2 = 1;
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func7(void **a1, _DWORD *a2)
{
__int64 i; // rbp
unsigned int v4; // ebx
void *v5; // rdi
void *v7; // rax

*a1 = malloc(0LL);
for ( i = 0LL; !ferror(stdin); i += fread(v5, 1uLL, 0x1000uLL, stdin) )
{
v4 = feof(stdin);
if ( v4 )
break;
v5 = realloc(*a1, i + 4096);
*a1 = v5;
if ( !v5 )
{
*a2 = 1;
return v4;
}
}
if ( i )
return (unsigned int)i;
v7 = malloc(0x1000uLL);
if ( (*a1 = v7) != 0LL )
return (unsigned int)i;
*a2 = 1;
return 0;
}

original_code:
int read_file(char** edit_buffer, int *fails) {
 size_t file_size;
 size_t data_read = 0;
 *edit_buffer = malloc(0);
 while(!ferror(stdin) && !feof(stdin)) {
  *edit_buffer = realloc(*edit_buffer, 4096+data_read);
  if(*edit_buffer == ((void *)0)) {
   *fails = 1;
   return 0;
  }
  data_read += fread(*edit_buffer, 1, 4096, stdin);
 }
 if (data_read == 0) {
  *edit_buffer = malloc(4096);
  if(*edit_buffer == ((void *)0)) {
   *fails = 1;
   return 0;
  }
 }
 return data_read;
}

canonical_original_code:
int read_file(char **edit_buffer, int *fails) {
 unsigned long file_size;
 unsigned long data_read = 0;
 *edit_buffer = malloc(0);
 while(!ferror(stdin) && !feof(stdin)) {
  *edit_buffer = realloc(*edit_buffer, 4096+data_read);
  if(*edit_buffer == ((void *)0)) {
   *fails = 1;
   return 0;
  }
  data_read += fread(*edit_buffer, 1, 4096, stdin);
 }
 if (data_read == 0) {
  *edit_buffer = malloc(4096);
  if(*edit_buffer == ((void *)0)) {
   *fails = 1;
   return 0;
  }
 }
 return data_read;
}

variable_types:
fails
int *


decompiled_code:
__int64 __fastcall add_char(char a1, int a2, const char **a3, char **a4, signed int a5)
{
signed int v7; // ebp
char *v8; // r14
char *v9; // r14
size_t v10; // rax
const char *v11; // r12
size_t v12; // rax
char src[60]; // [rsp+Ch] [rbp-3Ch] BYREF

v7 = a5;
src[0] = a1;
v8 = (char *)*a3;
if ( (int)(strlen(*a3) + 1) < a5
|| (v7 *= 2, *a3 = (const char *)realloc(v8, v7), *a4 = (char *)realloc(*a4, v7), (v8 = (char *)*a3) != 0LL) )
{
if ( *a4 )
{
strcpy(*a4, &v8[a2]);
strncat(*a4, *a3, a2);
v9 = *a4;
v10 = strlen(*a4);
strcpy((char *)*a3, &v9[v10 - a2]);
strncat((char *)*a3, src, 1uLL);
v11 = *a4;
v12 = strlen(v11);
strncat((char *)*a3, v11, v12 - a2);
}
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func8(char a1, int a2, const char **a3, char **a4, signed int a5)
{
signed int v7; // ebp
char *v8; // r14
char *v9; // r14
size_t v10; // rax
const char *v11; // r12
size_t v12; // rax
char src[60]; // [rsp+Ch] [rbp-3Ch] BYREF

v7 = a5;
src[0] = a1;
v8 = (char *)*a3;
if ( (int)(strlen(*a3) + 1) < a5
|| (v7 *= 2, *a3 = (const char *)realloc(v8, v7), *a4 = (char *)realloc(*a4, v7), (v8 = (char *)*a3) != 0LL) )
{
if ( *a4 )
{
strcpy(*a4, &v8[a2]);
strncat(*a4, *a3, a2);
v9 = *a4;
v10 = strlen(*a4);
strcpy((char *)*a3, &v9[v10 - a2]);
strncat((char *)*a3, src, 1uLL);
v11 = *a4;
v12 = strlen(v11);
strncat((char *)*a3, v11, v12 - a2);
}
}
return (unsigned int)v7;
}

original_code:
int add_char(char new_char, int pos, char** edit_buffer, char** temp_buffer, int alloc_size, int* fail) {
 if ((int) strlen( *edit_buffer ) + 1 >= alloc_size) {
  char* realloc_buffer;
  alloc_size += alloc_size;
  *edit_buffer = (realloc_buffer = realloc(*edit_buffer, alloc_size));
  *temp_buffer = (realloc_buffer = realloc(*temp_buffer, alloc_size));
 }
 if (*edit_buffer != ((void *)0) && *temp_buffer != ((void *)0)) {
  strcpy(*temp_buffer, *edit_buffer + pos);
  strncat(*temp_buffer, *edit_buffer, pos);
  strcpy(*edit_buffer, *temp_buffer + (strlen(*temp_buffer) - pos));
  strncat(*edit_buffer, &new_char, 1);
  strncat(*edit_buffer, *temp_buffer, (strlen(*temp_buffer) - pos));
 } else {
  fail = 0;
 }
 return alloc_size;
}

canonical_original_code:
int add_char(char new_char, int pos, char **edit_buffer, char **temp_buffer, int alloc_size, int *fail) {
 if ((int) strlen( *edit_buffer ) + 1 >= alloc_size) {
  char *realloc_buffer;
  alloc_size += alloc_size;
  *edit_buffer = (realloc_buffer = realloc(*edit_buffer, alloc_size));
  *temp_buffer = (realloc_buffer = realloc(*temp_buffer, alloc_size));
 }
 if (*edit_buffer != ((void *)0) && *temp_buffer != ((void *)0)) {
  strcpy(*temp_buffer, *edit_buffer + pos);
  strncat(*temp_buffer, *edit_buffer, pos);
  strcpy(*edit_buffer, *temp_buffer + (strlen(*temp_buffer) - pos));
  strncat(*edit_buffer, &new_char, 1);
  strncat(*edit_buffer, *temp_buffer, (strlen(*temp_buffer) - pos));
 } else {
  fail = 0;
 }
 return alloc_size;
}

variable_types:
fail
int *


decompiled_code:
__int64 __fastcall remove_char(int a1, const char **a2, char **a3, signed int a4, _DWORD *a5)
{
signed int v6; // ebx
char *v8; // r14
char *v9; // r14
size_t v10; // rax
const char *v11; // r13
size_t v12; // rax
__int64 result; // rax

v6 = a4;
v8 = (char *)*a2;
if ( ((int)(4 * strlen(*a2)) > a4
|| (v6 /= 2, *a2 = (const char *)realloc(v8, v6), *a3 = (char *)realloc(*a3, v6), (v8 = (char *)*a2) != 0LL))
&& *a3 )
{
strcpy(*a3, &v8[a1]);
strncat(*a3, *a2, a1 - 1);
v9 = *a3;
v10 = strlen(*a3);
strcpy((char *)*a2, &v9[v10 - (a1 - 1)]);
v11 = *a3;
v12 = strlen(v11);
strncat((char *)*a2, v11, v12 - (a1 + 1));
result = (unsigned int)v6;
}
else
{
*a5 = 0;
result = (unsigned int)v6;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, const char **a2, char **a3, signed int a4, _DWORD *a5)
{
signed int v6; // ebx
char *v8; // r14
char *v9; // r14
size_t v10; // rax
const char *v11; // r13
size_t v12; // rax
__int64 result; // rax

v6 = a4;
v8 = (char *)*a2;
if ( ((int)(4 * strlen(*a2)) > a4
|| (v6 /= 2, *a2 = (const char *)realloc(v8, v6), *a3 = (char *)realloc(*a3, v6), (v8 = (char *)*a2) != 0LL))
&& *a3 )
{
strcpy(*a3, &v8[a1]);
strncat(*a3, *a2, a1 - 1);
v9 = *a3;
v10 = strlen(*a3);
strcpy((char *)*a2, &v9[v10 - (a1 - 1)]);
v11 = *a3;
v12 = strlen(v11);
strncat((char *)*a2, v11, v12 - (a1 + 1));
result = (unsigned int)v6;
}
else
{
*a5 = 0;
result = (unsigned int)v6;
}
return result;
}

original_code:
int remove_char(int pos, char** edit_buffer, char** temp_buffer, int alloc_size, int* fail) {
 if ((int) strlen(*edit_buffer) * 4 <= alloc_size) {
  char* realloc_buffer;
  alloc_size /= 2;
  *edit_buffer = (realloc_buffer = realloc(*edit_buffer, alloc_size));
  *temp_buffer = (realloc_buffer = realloc(*temp_buffer, alloc_size));
 }
 if(*edit_buffer != ((void *)0) && *temp_buffer != ((void *)0)) {
  strcpy(*temp_buffer, *edit_buffer + pos);
  strncat(*temp_buffer, *edit_buffer, pos - 1);
  strcpy(*edit_buffer, *temp_buffer + (strlen(*temp_buffer) - (pos - 1)));
  strncat(*edit_buffer, *temp_buffer, (strlen(*temp_buffer) - (pos + 1)));
 } else {
  *fail = 0;
 }
 return alloc_size;
}

canonical_original_code:
int remove_char(int pos, char **edit_buffer, char **temp_buffer, int alloc_size, int *fail) {
 if ((int) strlen(*edit_buffer) * 4 <= alloc_size) {
  char *realloc_buffer;
  alloc_size /= 2;
  *edit_buffer = (realloc_buffer = realloc(*edit_buffer, alloc_size));
  *temp_buffer = (realloc_buffer = realloc(*temp_buffer, alloc_size));
 }
 if(*edit_buffer != ((void *)0) && *temp_buffer != ((void *)0)) {
  strcpy(*temp_buffer, *edit_buffer + pos);
  strncat(*temp_buffer, *edit_buffer, pos - 1);
  strcpy(*edit_buffer, *temp_buffer + (strlen(*temp_buffer) - (pos - 1)));
  strncat(*edit_buffer, *temp_buffer, (strlen(*temp_buffer) - (pos + 1)));
 } else {
  *fail = 0;
 }
 return alloc_size;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
signed int v3; // eax
int v4; // ebx
signed int v6; // ebp
__m128i v7; // [rsp+0h] [rbp-78h] BYREF
__m128i v8; // [rsp+10h] [rbp-68h] BYREF
__m128i v9; // [rsp+20h] [rbp-58h] BYREF
__int64 v10; // [rsp+30h] [rbp-48h]
speed_t v11; // [rsp+38h] [rbp-40h]
void *ptr; // [rsp+40h] [rbp-38h] BYREF
char *s; // [rsp+48h] [rbp-30h] BYREF
char buf; // [rsp+55h] [rbp-23h] BYREF
__int16 v15; // [rsp+56h] [rbp-22h]
int v16[7]; // [rsp+5Ch] [rbp-1Ch] BYREF

v16[0] = 0;
t_getstate(&original);
t_initstate(&v7, &original);
*(__m128i *)&n_state.c_iflag = _mm_loadu_si128(&v7);
*(__m128i *)&n_state.c_line = _mm_loadu_si128(&v8);
*(__m128i *)&n_state.c_cc[15] = _mm_loadu_si128(&v9);
*(_QWORD *)&n_state.c_cc[31] = v10;
n_state.c_ospeed = v11;
t_setstate(&n_state);
v3 = read_file((void **)&s, v16);
v4 = v16[0];
if ( v16[0] )
goto LABEL_2;
v6 = v3;
ptr = malloc(v3);
while ( 1 )
{
t_read(&buf, 7uLL);
if ( buf != 27 )
{
if ( buf == 127 )
{
if ( !v15 && v4 > 0 )
v6 = remove_char(v4--, (const char **)&s, (char **)&ptr, v6, v16);
}
else if ( buf )
{
goto LABEL_8;
}
goto LABEL_20;
}
if ( (_BYTE)v15 == 106 )
{
if ( !HIBYTE(v15) )
{
v4 -= v4 > 0;
goto LABEL_20;
}
goto LABEL_6;
}
if ( (_BYTE)v15 == 107 )
break;
LABEL_6:
if ( (_BYTE)v15 != 100 )
goto LABEL_7;
if ( HIBYTE(v15) )
goto LABEL_8;
fputs(s, stdout);
LABEL_20:
if ( v16[0] )
goto LABEL_2;
}
if ( !HIBYTE(v15) )
{
v4 += v4 < (int)strlen(s);
goto LABEL_20;
}
LABEL_7:
if ( v15 != 102 )
{
LABEL_8:
if ( !v15 )
v6 = add_char(buf, v4++, (const char **)&s, (char **)&ptr, v6);
goto LABEL_20;
}
if ( v6 > 0 && !v16[0] )
{
free(s);
free(ptr);
}
LABEL_2:
t_setstate(&original);
fflush(stdout);
return v16[0];
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
signed int v3; // eax
int v4; // ebx
signed int v6; // ebp
__m128i v7; // [rsp+0h] [rbp-78h] BYREF
__m128i v8; // [rsp+10h] [rbp-68h] BYREF
__m128i v9; // [rsp+20h] [rbp-58h] BYREF
__int64 v10; // [rsp+30h] [rbp-48h]
speed_t v11; // [rsp+38h] [rbp-40h]
void *ptr; // [rsp+40h] [rbp-38h] BYREF
char *s; // [rsp+48h] [rbp-30h] BYREF
char buf; // [rsp+55h] [rbp-23h] BYREF
__int16 v15; // [rsp+56h] [rbp-22h]
int v16[7]; // [rsp+5Ch] [rbp-1Ch] BYREF

v16[0] = 0;
func10(&original);
t_initstate(&v7, &original);
*(__m128i *)&n_state.c_iflag = _mm_loadu_si128(&v7);
*(__m128i *)&n_state.c_line = _mm_loadu_si128(&v8);
*(__m128i *)&n_state.c_cc[15] = _mm_loadu_si128(&v9);
*(_QWORD *)&n_state.c_cc[31] = v10;
n_state.c_ospeed = v11;
func11(&n_state);
v3 = func7((void **)&s, v16);
v4 = v16[0];
if ( v16[0] )
goto LABEL_2;
v6 = v3;
ptr = malloc(v3);
while ( 1 )
{
func15(&buf, 7uLL);
if ( buf != 27 )
{
if ( buf == 127 )
{
if ( !v15 && v4 > 0 )
v6 = func9(v4--, (const char **)&s, (char **)&ptr, v6, v16);
}
else if ( buf )
{
goto LABEL_8;
}
goto LABEL_20;
}
if ( (_BYTE)v15 == 106 )
{
if ( !HIBYTE(v15) )
{
v4 -= v4 > 0;
goto LABEL_20;
}
goto LABEL_6;
}
if ( (_BYTE)v15 == 107 )
break;
LABEL_6:
if ( (_BYTE)v15 != 100 )
goto LABEL_7;
if ( HIBYTE(v15) )
goto LABEL_8;
fputs(s, stdout);
LABEL_20:
if ( v16[0] )
goto LABEL_2;
}
if ( !HIBYTE(v15) )
{
v4 += v4 < (int)strlen(s);
goto LABEL_20;
}
LABEL_7:
if ( v15 != 102 )
{
LABEL_8:
if ( !v15 )
v6 = func8(buf, v4++, (const char **)&s, (char **)&ptr, v6);
goto LABEL_20;
}
if ( v6 > 0 && !v16[0] )
{
free(s);
free(ptr);
}
LABEL_2:
func11(&original);
fflush(stdout);
return v16[0];
}

original_code:
int main(int argc, char* argv[]) {
 int fail = 0;
 int pos = 0;
 int alloc_size;
 char ch[7];
 char* edit_buffer;
 char* temp_buffer;
 t_getstate(&original);
 n_state = t_initstate(&original);
 t_setstate(&n_state);
 alloc_size = read_file(&edit_buffer, &fail);
 if (!fail) {
  temp_buffer = malloc(alloc_size);
 }
 while(!fail) {
  t_read(ch, 7);
  if (ch[0] == 27 && ch[1] == 'j' && ch[2] == 0) {
   if(pos > 0) {
    pos -= 1;
   }
  } else if (ch[0] == 27 && ch[1] == 'k' && ch[2] == 0) {
   if (pos < (int) strlen(edit_buffer)) {
    pos += 1;
   }
  } else if (ch[0] == 27 && ch[1] == 'd' && ch[2] == 0) {
   fputs(edit_buffer, stdout);
  } else if (ch[0] == 27 && ch[1] == 'f' && ch[2] == 0) {
   break;
  } else if (ch[0] == 127 && ch[1] == 0 && ch[2] == 0) {
   if (pos > 0) {
    alloc_size = remove_char(pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
    pos--;
   }
  } else if(ch[0] != 0 && ch[1] == 0 && ch[2] == 0) {
   alloc_size = add_char(ch[0], pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
   pos++;
  }
 }
 if (alloc_size > 0 && !fail) {
  free(edit_buffer);
  free(temp_buffer);
 }
 t_setstate(&original);
 fflush(stdout);
 return fail;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int fail = 0;
 int pos = 0;
 int alloc_size;
 char ch[7];
 char *edit_buffer;
 char *temp_buffer;
 t_getstate(&original);
 n_state = t_initstate(&original);
 t_setstate(&n_state);
 alloc_size = read_file(&edit_buffer, &fail);
 if (!fail) {
  temp_buffer = malloc(alloc_size);
 }
 while(!fail) {
  t_read(ch, 7);
  if (ch[0] == 27 && ch[1] == 'j' && ch[2] == 0) {
   if(pos > 0) {
    pos -= 1;
   }
  } else if (ch[0] == 27 && ch[1] == 'k' && ch[2] == 0) {
   if (pos < (int) strlen(edit_buffer)) {
    pos += 1;
   }
  } else if (ch[0] == 27 && ch[1] == 'd' && ch[2] == 0) {
   fputs(edit_buffer, stdout);
  } else if (ch[0] == 27 && ch[1] == 'f' && ch[2] == 0) {
   break;
  } else if (ch[0] == 127 && ch[1] == 0 && ch[2] == 0) {
   if (pos > 0) {
    alloc_size = remove_char(pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
    pos--;
   }
  } else if(ch[0] != 0 && ch[1] == 0 && ch[2] == 0) {
   alloc_size = add_char(ch[0], pos, &edit_buffer, &temp_buffer, alloc_size, &fail);
   pos++;
  }
 }
 if (alloc_size > 0 && !fail) {
  free(edit_buffer);
  free(temp_buffer);
 }
 t_setstate(&original);
 fflush(stdout);
 return fail;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: fail int

variable_types:
ch
char [7]


decompiled_code:
__int64 __fastcall t_getstate(struct termios *termios_p)
{
return (unsigned int)-(tcgetattr(1, termios_p) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(struct termios *termios_p)
{
return (unsigned int)-(tcgetattr(1, termios_p) != 0);
}

original_code:
int t_getstate(tstate *state){
 if (tcgetattr(1, state)) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int t_getstate(struct termios *state){
 if (tcgetattr(1, state)) {
  return -1;
 }
 return 0;
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } *


decompiled_code:
__int64 __fastcall t_setstate(struct termios *termios_p)
{
return (unsigned int)-(tcsetattr(1, 0, termios_p) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func11(struct termios *termios_p)
{
return (unsigned int)-(tcsetattr(1, 0, termios_p) != 0);
}

original_code:
int t_setstate(const tstate *state){
 if (tcsetattr(1, 0, state)) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int t_setstate(struct termios *state){
 if (tcsetattr(1, 0, state)) {
  return -1;
 }
 return 0;
}

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int t_clear()
{
return fputs("\x1B[2J", stdout);
}

canonical_decompiled_code:
int func12()
{
return fputs("\x1B[2J", stdout);
}

original_code:
int t_clear() {
 return fputs("", stdout);
}


decompiled_code:
__int64 t_getwidth()
{
__int64 result; // rax
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

if ( ioctl(0, 0x5413uLL, v1) )
result = 0xFFFFFFFFLL;
else
result = v2;
return result;
}

canonical_decompiled_code:
__int64 func13()
{
__int64 result; // rax
char v1[2]; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v2; // [rsp+Ah] [rbp-Eh]

if ( ioctl(0, 0x5413uLL, v1) )
result = 0xFFFFFFFFLL;
else
result = v2;
return result;
}

original_code:
int t_getwidth() {
 struct winsize ts;
 if (ioctl(0, 0x5413, &ts)) {
  return -1;
 }
 return ts.ws_col;
}

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }


decompiled_code:
__int64 t_getheight()
{
__int64 result; // rax
unsigned __int16 v1[8]; // [rsp+8h] [rbp-10h] BYREF

if ( ioctl(0, 0x5413uLL, v1) )
result = 0xFFFFFFFFLL;
else
result = v1[0];
return result;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 result; // rax
unsigned __int16 v1[8]; // [rsp+8h] [rbp-10h] BYREF

if ( ioctl(0, 0x5413uLL, v1) )
result = 0xFFFFFFFFLL;
else
result = v1[0];
return result;
}

original_code:
int t_getheight() {
 struct winsize ts;
 if(ioctl(0, 0x5413, &ts)) {
  return -1;
 }
 return ts.ws_row;
}

user_defined_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }


decompiled_code:
ssize_t __fastcall t_read(void *buf, size_t nbytes)
{
memset(buf, 0, nbytes);
return read(0, buf, nbytes);
}

canonical_decompiled_code:
ssize_t __fastcall func15(void *buf, size_t nbytes)
{
memset(buf, 0, nbytes);
return read(0, buf, nbytes);
}

original_code:
int t_read(char *c, size_t len) {
 memset(c, 0, len*sizeof(char));
 return read(0, c, len);
}

canonical_original_code:
int t_read(char *c, unsigned long len) {
 memset(c, 0, len*sizeof(char));
 return read(0, c, len);
}

variable_types:
c
char *

variable_types:
len
unsigned long


add

repo: theDazzler/Reverse-Polish-Notation-in-C

decompiled_code:
__int64 __fastcall add(int a1, int a2)
{
return (unsigned int)(a1 + a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2)
{
return (unsigned int)(a1 + a2);
}

original_code:
int add(int a, int b)
{
 return a + b;
}

canonical_original_code:
int add(int a, int b)
{
 return a + b;
}

variable_types: a int

variable_types: b int


sub

decompiled_code:
__int64 __fastcall sub(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
return (unsigned int)(a1 - a2);
}

original_code:
int sub(int a, int b)
{
 return a - b;
}

canonical_original_code:
int sub(int a, int b)
{
 return a - b;
}


mul

decompiled_code:
__int64 __fastcall mul(int a1, int a2)
{
return (unsigned int)(a2 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
return (unsigned int)(a2 * a1);
}

original_code:
int mul(int a, int b)
{
 return a * b;
}

canonical_original_code:
int mul(int a, int b)
{
 return a * b;
}


divide

decompiled_code:
__int64 __fastcall divide(int a1, int a2)
{
return (unsigned int)(a1 / a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
return (unsigned int)(a1 / a2);
}

original_code:
int divide(int a, int b)
{
 return a / b;
}


getInput

decompiled_code:
__int64 getInput()
{
__int64 result; // rax
int v1; // ebx
int v2; // eax
unsigned int v3; // eax
int v4; // ebx
int v5; // eax
unsigned int v6; // eax
int v7; // ebx
int v8; // eax
unsigned int v9; // eax
int v10; // ebx
int v11; // eax
unsigned int v12; // eax
unsigned int v13; // eax
char v14[9]; // [rsp+Fh] [rbp-9h] BYREF

while ( 1 )
{
makeEmpty();
printf("Enter an RPN expression: ");
_isoc99_scanf("%c", v14);
result = (unsigned __int8)v14[0];
if ( v14[0] == 113 )
return result;
for ( ; v14[0] != 10; LOBYTE(result) = v14[0] )
{
if ( (unsigned __int8)(result - 48) <= 9u )
{
if ( (unsigned __int8)isFull() )
{
puts("Expression is too complex");
exit(1);
}
push((unsigned int)(v14[0] - 48));
}
switch ( v14[0] )
{
case '*':
v7 = pop();
v8 = pop();
v9 = mul(v8, v7);
push(v9);
break;
case '+':
v1 = pop();
v2 = pop();
v3 = add(v2, v1);
push(v3);
break;
case '-':
v4 = pop();
v5 = pop();
v6 = sub(v5, v4);
push(v6);
break;
case '/':
v10 = pop();
v11 = pop();
v12 = divide(v11, v10);
push(v12);
break;
case '=':
v13 = pop();
printf("%d\n", v13);
break;
default:
break;
}
_isoc99_scanf("%c", v14);
}
}
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
int v1; // ebx
int v2; // eax
unsigned int v3; // eax
int v4; // ebx
int v5; // eax
unsigned int v6; // eax
int v7; // ebx
int v8; // eax
unsigned int v9; // eax
int v10; // ebx
int v11; // eax
unsigned int v12; // eax
unsigned int v13; // eax
char v14[9]; // [rsp+Fh] [rbp-9h] BYREF

while ( 1 )
{
makeEmpty();
printf("Enter an RPN expression: ");
_isoc99_scanf("%c", v14);
result = (unsigned __int8)v14[0];
if ( v14[0] == 113 )
return result;
for ( ; v14[0] != 10; LOBYTE(result) = v14[0] )
{
if ( (unsigned __int8)(result - 48) <= 9u )
{
if ( (unsigned __int8)isFull() )
{
puts("Expression is too complex");
exit(1);
}
push((unsigned int)(v14[0] - 48));
}
switch ( v14[0] )
{
case '*':
v7 = pop();
v8 = pop();
v9 = func2(v8, v7);
push(v9);
break;
case '+':
v1 = pop();
v2 = pop();
v3 = func0(v2, v1);
push(v3);
break;
case '-':
v4 = pop();
v5 = pop();
v6 = func1(v5, v4);
push(v6);
break;
case '/':
v10 = pop();
v11 = pop();
v12 = func3(v11, v10);
push(v12);
break;
case '=':
v13 = pop();
printf("%d\n", v13);
break;
default:
break;
}
_isoc99_scanf("%c", v14);
}
}
}

original_code:
void getInput()
{
 char ch;
 while(1)
 {
  makeEmpty();
  printf("Enter an RPN expression: ");
  scanf("%c", &ch);
  if(ch == 'q')
    break;
  while(1)
  {
   if(ch == '\n')
    break;
   if(ch >= 48 && ch <= 57)
   {
    if(!isFull())
    {
     push(ch - 48);
    }
    else
    {
     printf("Expression is too complex\n");
     exit(1);
    }
   }
   switch(ch)
   {
    case '+':
     push(add(pop(), pop()));
     break;
    case '-':
     push(sub(pop(), pop()));
     break;
    case '*':
     push(mul(pop(), pop()));
     break;
    case '/':
     push(divide(pop(), pop()));
     break;
    case '=':
     printf("%d\n", pop());
     break;
   }
   scanf("%c", &ch);
  }
 }
}

variable_types: ch char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
getInput(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func4(argc, argv, envp);
return 0;
}

original_code:
int main()
{
 getInput();
 return 0;
}

canonical_original_code:
int main()
{
 getInput();
 return 0;
}


stoptui

decompiled_code:
int stoptui()
{
fprintf(stderr, "DEBUG (%s:%s:%d): stopping tui...\n", "src/tui.c", "stoptui", 198LL);
stopwinmgr();
stopkeyctlr();
pthread_join(kbthread, 0LL);
pthread_join(winthread, 0LL);
_free_winstructs();
return endwin();
}

canonical_decompiled_code:
int func2()
{
fprintf(stderr, "DEBUG (%s:%s:%d): stopping tui...\n", "src/tui.c", "stoptui", 198LL);
stopwinmgr();
stopkeyctlr();
pthread_join(kbthread, 0LL);
pthread_join(winthread, 0LL);
_free_winstructs();
return endwin();
}

original_code:
void stoptui()
{
 fprintf(stderr, "DEBUG (%s:%s:%d): " "stopping tui..." "\n", "src/tui.c", __func__, 198);
 stopwinmgr();
 stopkeyctlr();
 pthread_join(kbthread,((void *)0));
 pthread_join(winthread,((void *)0));
 __free_winstructs();
 endwin();
}


decompiled_code:
int tui_ready()
{
fprintf(stderr, "DEBUG (%s:%s:%d): tui_ready called\n", "src/tui.c", "tui_ready", 122LL);
pthread_mutex_lock(&wait_mutex);
wait_sig = 1;
pthread_mutex_unlock(&wait_mutex);
return pthread_cond_signal(&wait_cond);
}

canonical_decompiled_code:
int func4()
{
fprintf(stderr, "DEBUG (%s:%s:%d): tui_ready called\n", "src/tui.c", "tui_ready", 122LL);
pthread_mutex_lock(&wait_mutex);
wait_sig = 1;
pthread_mutex_unlock(&wait_mutex);
return pthread_cond_signal(&wait_cond);
}

original_code:
void tui_ready()
{
 fprintf(stderr, "DEBUG (%s:%s:%d): " "tui_ready called" "\n", "src/tui.c", __func__, 122);
 pthread_mutex_lock(&wait_mutex);
 wait_sig = 1;
 pthread_mutex_unlock(&wait_mutex);
 pthread_cond_signal(&wait_cond);
}


decompiled_code:
int tui__wait()
{
pthread_mutex_lock(&wait_mutex);
while ( !wait_sig )
pthread_cond_wait(&wait_cond, &wait_mutex);
wait_sig = 0;
return pthread_mutex_unlock(&wait_mutex);
}

canonical_decompiled_code:
int func5()
{
pthread_mutex_lock(&wait_mutex);
while ( !wait_sig )
pthread_cond_wait(&wait_cond, &wait_mutex);
wait_sig = 0;
return pthread_mutex_unlock(&wait_mutex);
}

original_code:
void tui__wait()
{
 pthread_mutex_lock(&wait_mutex);
 while(wait_sig == 0) pthread_cond_wait(&wait_cond, &wait_mutex);
 wait_sig = 0;
 pthread_mutex_unlock(&wait_mutex);
 return;
}


inspare

decompiled_code:
const char *__fastcall inspare(__int64 a1)
{
const char *v1; // rbx
size_t v2; // rax
const char *v3; // rbx
size_t v4; // rax
int v5; // eax
int i; // ebp
size_t v7; // rax
size_t v8; // rax
int v9; // ebp
int j; // ebp
__int64 v11; // rax

v1 = (const char *)strins(a1, "(", 0LL);
v2 = strlen(v1);
v3 = (const char *)strins(v1, ")", v2);
v4 = strlen(v3);
v5 = (unsigned int)memchr(v3 + 1, 40, v4) - (_DWORD)v3;
if ( v5 >= 0 )
{
for ( i = v5; i >= 0; i = (unsigned int)memchr(&v3[v9], 40, v7) - (_DWORD)v3 )
{
v3 = (const char *)strins(v3, "(", (unsigned int)i);
v8 = strlen(&v3[i + 1]);
v9 = (unsigned int)memchr(&v3[i], 41, v8) - (_DWORD)v3;
if ( v9 >= 0 )
v3 = (const char *)strins(v3, ")", (unsigned int)v9);
v7 = strlen(v3);
}
}
for ( j = (unsigned int)strpbrk(v3, "+-") - (_DWORD)v3; j >= 0; j = (unsigned int)strpbrk(&v3[j + 2], "+-")
- (_DWORD)v3 )
{
v11 = strins(v3, ")", (unsigned int)j);
v3 = (const char *)strins(v11, "(", (unsigned int)(j + 2));
}
return v3;
}

canonical_decompiled_code:
const char *__fastcall func1(__int64 a1)
{
const char *v1; // rbx
size_t v2; // rax
const char *v3; // rbx
size_t v4; // rax
int v5; // eax
int i; // ebp
size_t v7; // rax
size_t v8; // rax
int v9; // ebp
int j; // ebp
__int64 v11; // rax

v1 = (const char *)strins(a1, "(", 0LL);
v2 = strlen(v1);
v3 = (const char *)strins(v1, ")", v2);
v4 = strlen(v3);
v5 = (unsigned int)memchr(v3 + 1, 40, v4) - (_DWORD)v3;
if ( v5 >= 0 )
{
for ( i = v5; i >= 0; i = (unsigned int)memchr(&v3[v9], 40, v7) - (_DWORD)v3 )
{
v3 = (const char *)strins(v3, "(", (unsigned int)i);
v8 = strlen(&v3[i + 1]);
v9 = (unsigned int)memchr(&v3[i], 41, v8) - (_DWORD)v3;
if ( v9 >= 0 )
v3 = (const char *)strins(v3, ")", (unsigned int)v9);
v7 = strlen(v3);
}
}
for ( j = (unsigned int)strpbrk(v3, "+-") - (_DWORD)v3; j >= 0; j = (unsigned int)strpbrk(&v3[j + 2], "+-")
- (_DWORD)v3 )
{
v11 = strins(v3, ")", (unsigned int)j);
v3 = (const char *)strins(v11, "(", (unsigned int)(j + 2));
}
return v3;
}

original_code:
char* inspare(char* expr)
{
 char* newexpr;
 newexpr = strins(expr,"(",0);
 newexpr = strins(newexpr,")",strlen(newexpr));
 int para_flag = ((char*)memchr(newexpr+1,'(',strlen(newexpr)))-newexpr;
 int epara_flag = 0;
 while(para_flag >= 0 )
 {
  newexpr = strins(newexpr,"(",para_flag);
  epara_flag = ((char*)memchr(newexpr+para_flag,')',strlen(newexpr+para_flag+1)))-newexpr;
  if(epara_flag >= 0)
   newexpr = strins(newexpr,")",epara_flag);
  para_flag = ((char*)memchr(newexpr+epara_flag,'(',strlen(newexpr)))-newexpr;
 }
 int p_flag = strpbrk(newexpr,"+-")-newexpr;
 while(p_flag >= 0)
 {
  newexpr = strins(newexpr,")",p_flag);
  newexpr = strins(newexpr,"(",p_flag+2);
  p_flag = strpbrk(newexpr+p_flag+2,"+-")-newexpr;
 }
 return newexpr;
}

canonical_original_code:
char *inspare(char *expr)
{
 char *newexpr;
 newexpr = strins(expr,"(",0);
 newexpr = strins(newexpr,")",strlen(newexpr));
 int para_flag = ((char*)memchr(newexpr+1,'(',strlen(newexpr)))-newexpr;
 int epara_flag = 0;
 while(para_flag >= 0 )
 {
  newexpr = strins(newexpr,"(",para_flag);
  epara_flag = ((char *)memchr(newexpr+para_flag,')',strlen(newexpr+para_flag+1)))-newexpr;
  if(epara_flag >= 0)
   newexpr = strins(newexpr,")",epara_flag);
  para_flag = ((char *)memchr(newexpr+epara_flag,'(',strlen(newexpr)))-newexpr;
 }
 int p_flag = strpbrk(newexpr,"+-")-newexpr;
 while(p_flag >= 0)
 {
  newexpr = strins(newexpr,")",p_flag);
  newexpr = strins(newexpr,"(",p_flag+2);
  p_flag = strpbrk(newexpr+p_flag+2,"+-")-newexpr;
 }
 return newexpr;
}

variable_types:
expr
char *

variable_types:
newexpr
char *

return_type: char *


expndexpr

decompiled_code:
const char *__fastcall expndexpr(const char *a1)
{
char *v1; // rax

v1 = expndcoef_localalias(a1);
return inspare((__int64)v1);
}

canonical_decompiled_code:
const char *__fastcall func2(const char *a1)
{
char *v1; // rax

v1 = func0(a1);
return func1((__int64)v1);
}

original_code:
char* expndexpr(char* expr)
{
 char* newexpr;
 newexpr = expndcoef(expr);
 newexpr = inspare(newexpr);
 return newexpr;
}

canonical_original_code:
char *expndexpr(char *expr)
{
 char *newexpr;
 newexpr = expndcoef(expr);
 newexpr = inspare(newexpr);
 return newexpr;
}


mtchpar

decompiled_code:
const char *__fastcall mtchpar(const char *a1, int a2)
{
int v2; // ebp
const char *result; // rax
__int64 v4; // rdx

v2 = a2;
if ( !strpbrk(a1, "()") )
return a1;
result = a1 + 1;
if ( (__int16)a2 > 0 || (result = a1 - 1, (_WORD)a2) )
{
while ( 1 )
{
if ( *result == 40 )
{
++v2;
}
else if ( *result == 41 )
{
--v2;
}
if ( !(_WORD)v2 || !result[1] )
break;
v4 = (__int64)(result-- + 1);
if ( (__int16)v2 > 0 )
result = (const char *)v4;
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func3(const char *a1, int a2)
{
int v2; // ebp
const char *result; // rax
__int64 v4; // rdx

v2 = a2;
if ( !strpbrk(a1, "()") )
return a1;
result = a1 + 1;
if ( (__int16)a2 > 0 || (result = a1 - 1, (_WORD)a2) )
{
while ( 1 )
{
if ( *result == 40 )
{
++v2;
}
else if ( *result == 41 )
{
--v2;
}
if ( !(_WORD)v2 || !result[1] )
break;
v4 = (__int64)(result-- + 1);
if ( (__int16)v2 > 0 )
result = (const char *)v4;
}
}
return result;
}

original_code:
char* mtchpar(char* expr, short pcnt)
{
 if(strpbrk(expr,"()") == ((void *)0))
  return expr;
 char* pindex = pcnt > 0 ? expr+1 : expr-1;
 while(pcnt != 0)
 {
  if(*pindex == '(')
   pcnt+=1;
  else if(*pindex == ')')
   pcnt-=1;
  if(pcnt == 0 || *(pindex+1) == '\0')
   break;
  pindex = pcnt > 0 ? pindex+1 : pindex-1;
 }
 return pindex;
}

canonical_original_code:
char *mtchpar(char *expr, short pcnt)
{
 if(strpbrk(expr,"()") == ((void *)0))
  return expr;
 char *pindex = pcnt > 0 ? expr+1 : expr-1;
 while(pcnt != 0)
 {
  if(*pindex == '(')
   pcnt+=1;
  else if(*pindex == ')')
   pcnt-=1;
  if(pcnt == 0 || *(pindex+1) == '\0')
   break;
  pindex = pcnt > 0 ? pindex+1 : pindex-1;
 }
 return pindex;
}

variable_types: pcnt short

variable_types:
pindex
char *


parntrim

decompiled_code:
char *__fastcall parntrim(char *s)
{
char *v1; // rbx
int v2; // eax
int v4; // eax

v1 = s;
if ( *s == 40 )
{
v4 = strlen(s);
v1 = (char *)strsub(s, 1LL, (unsigned int)(v4 - 1));
}
v2 = strlen(v1);
if ( v1[v2 - 1] == 41 )
v1 = (char *)strsub(v1, 0LL, (unsigned int)(v2 - 2));
return v1;
}

canonical_decompiled_code:
char *__fastcall func4(char *s)
{
char *v1; // rbx
int v2; // eax
int v4; // eax

v1 = s;
if ( *s == 40 )
{
v4 = strlen(s);
v1 = (char *)strsub(s, 1LL, (unsigned int)(v4 - 1));
}
v2 = strlen(v1);
if ( v1[v2 - 1] == 41 )
v1 = (char *)strsub(v1, 0LL, (unsigned int)(v2 - 2));
return v1;
}

original_code:
char* parntrim(char* expr)
{
 char* newexpr = expr;
 int len = strlen(newexpr);
 if(*newexpr == '(')
  newexpr = strsub(newexpr,1,len-1);
 len = strlen(newexpr);
 if(*(newexpr+len-1) == ')')
  newexpr = strsub(newexpr,0,len-2);
 return newexpr;
}

canonical_original_code:
char *parntrim(char *expr)
{
 char *newexpr = expr;
 int len = strlen(newexpr);
 if(*newexpr == '(')
  newexpr = strsub(newexpr,1,len-1);
 len = strlen(newexpr);
 if(*(newexpr+len-1) == ')')
  newexpr = strsub(newexpr,0,len-2);
 return newexpr;
}

variable_types: len int


addkeyevent

decompiled_code:
__int64 __fastcall addkeyevent(unsigned int a1, __int64 a2)
{
int *v2; // rbp
char *v3; // r8
__int64 result; // rax
int *v5; // rbp
char *v6; // r8

if ( a2 )
{
if ( a1 <= 0x279 )
{
pthread_mutex_lock(&events_mutex);
events[a1] = a2;
pthread_mutex_unlock(&events_mutex);
result = 0LL;
}
else
{
v2 = _errno_location();
v3 = "None";
if ( *v2 )
v3 = strerror(*v2);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (key invalid)\n",
"src/keyboard.c",
54LL,
v3,
a1);
*v2 = 0;
result = 1LL;
}
}
else
{
v5 = _errno_location();
v6 = "None";
if ( *v5 )
v6 = strerror(*v5);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (function invalid)\n",
"src/keyboard.c",
53LL,
v6,
a1);
*v5 = 0;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2)
{
int *v2; // rbp
char *v3; // r8
__int64 result; // rax
int *v5; // rbp
char *v6; // r8

if ( a2 )
{
if ( a1 <= 0x279 )
{
pthread_mutex_lock(&events_mutex);
events[a1] = a2;
pthread_mutex_unlock(&events_mutex);
result = 0LL;
}
else
{
v2 = _errno_location();
v3 = "None";
if ( *v2 )
v3 = strerror(*v2);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (key invalid)\n",
"src/keyboard.c",
54LL,
v3,
a1);
*v2 = 0;
result = 1LL;
}
}
else
{
v5 = _errno_location();
v6 = "None";
if ( *v5 )
v6 = strerror(*v5);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (function invalid)\n",
"src/keyboard.c",
53LL,
v6,
a1);
*v5 = 0;
result = 1LL;
}
return result;
}

original_code:
int addkeyevent(int key, event_func_type func)
{
 if(!(func)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Could not set event for key: %i (function invalid)" "\n", "src/keyboard.c", 53, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))),key); (*__errno_location ())=0; goto error; };
 if(!(key >= 0 && key < 634)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Could not set event for key: %i (key invalid)" "\n", "src/keyboard.c", 54, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))),key); (*__errno_location ())=0; goto error; };
 pthread_mutex_lock(&events_mutex);
 events[key] = func;
 pthread_mutex_unlock(&events_mutex);
 return 0;
 error:
  return 1;
}

canonical_original_code:
int addkeyevent(int key, int (*func)(int keycode))
{
 if(!(func)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Could not set event for key: %i (function invalid)" "\n", "src/keyboard.c", 53, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))),key); (*__errno_location ())=0; goto error; };
 if(!(key >= 0 && key < 634)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Could not set event for key: %i (key invalid)" "\n", "src/keyboard.c", 54, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))),key); (*__errno_location ())=0; goto error; };
 pthread_mutex_lock(&events_mutex);
 events[key] = func;
 pthread_mutex_unlock(&events_mutex);
 return 0;
 error:
  return 1;
}

variable_types: key int

variable_types:
func
int (*)(int keycode)


removekeyevent

decompiled_code:
int __fastcall removekeyevent(int a1)
{
pthread_mutex_lock(&events_mutex);
events[a1] = 0LL;
return pthread_mutex_unlock(&events_mutex);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
pthread_mutex_lock(&events_mutex);
events[a1] = 0LL;
return pthread_mutex_unlock(&events_mutex);
}

original_code:
void removekeyevent(int key)
{
 if(!(key >= 0 || key < 634)) { fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " "Could not remove event for key: %i (key invalid)" "\n", "src/keyboard.c", 67, ((*__errno_location ()) == 0 ? "None" : strerror((*__errno_location ()))),key); (*__errno_location ())=0; goto error; };
 pthread_mutex_lock(&events_mutex);
 events[key] = ((void *)0);
 pthread_mutex_unlock(&events_mutex);
 error:
  return;
}


stopkeyctlr

decompiled_code:
int stopkeyctlr()
{
run_ctrlr = 0;
pthread_mutex_destroy(&events_mutex);
return pthread_mutex_destroy(&keywin_mutex);
}

canonical_decompiled_code:
int func5()
{
run_ctrlr = 0;
pthread_mutex_destroy(&events_mutex);
return pthread_mutex_destroy(&keywin_mutex);
}

original_code:
void stopkeyctlr()
{
 run_ctrlr = 0;
 pthread_mutex_destroy(&events_mutex);
 pthread_mutex_destroy(&keywin_mutex);
}


makefancy

decompiled_code:
void makefancy()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
struct table* makefancy(struct table* tble)
{
}

canonical_original_code:
struct table *makefancy(struct table *tble)
{
}

variable_types:
tble
struct table *

return_type: struct table { int x_size; int y_size; int *cells; } *


pgraph

decompiled_code:
__int64 __fastcall pgraph(int *a1)
{
int v1; // er12
int v2; // ebp
__int64 result; // rax
int v4; // eax
unsigned int v5; // ebx

if ( !a1 )
return 1LL;
v1 = *a1;
v2 = a1[1] - 1;
result = 0LL;
if ( v2 >= 0 )
{
do
{
v5 = 0;
if ( v1 > 0 )
{
do
{
v4 = getcell(a1, v5, (unsigned int)v2);
putchar(v4);
++v5;
}
while ( v1 != v5 );
}
putchar(10);
--v2;
}
while ( v2 != -1 );
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1)
{
int v1; // er12
int v2; // ebp
__int64 result; // rax
int v4; // eax
unsigned int v5; // ebx

if ( !a1 )
return 1LL;
v1 = *a1;
v2 = a1[1] - 1;
result = 0LL;
if ( v2 >= 0 )
{
do
{
v5 = 0;
if ( v1 > 0 )
{
do
{
v4 = getcell(a1, v5, (unsigned int)v2);
putchar(v4);
++v5;
}
while ( v1 != v5 );
}
putchar(10);
--v2;
}
while ( v2 != -1 );
result = 0LL;
}
return result;
}

original_code:
int pgraph(struct table* tble)
{
 if(tble == ((void *)0))
  return 1;
 int i,j;
 int xsize = tble->x_size;
 int ysize = tble->y_size;
 for (i = ysize-1; i >= 0; i--)
 {
  for (j = 0; j < xsize; j++)
  {
   int cell = getcell(tble,j,i);
   printf("%c",cell);
  }
  printf("\n");
 }
 return 0;
}

canonical_original_code:
int pgraph(struct table *tble)
{
 if(tble == ((void *)0))
  return 1;
 int i; int j;
 int xsize = tble->x_size;
 int ysize = tble->y_size;
 for (i = ysize-1; i >= 0; i--)
 {
  for (j = 0; j < xsize; j++)
  {
   int cell = getcell(tble,j,i);
   printf("%c",cell);
  }
  printf("\n");
 }
 return 0;
}

variable_types: struct table { int x_size; int y_size; int *cells; } *

variable_types: i int

variable_types: j int

variable_types: xsize int

variable_types: ysize int

variable_types: cell int


strins

decompiled_code:
char *__fastcall strins(char *src, char *a2, int a3)
{
int v4; // ebp
size_t v5; // r12
char *v6; // rbp
size_t v7; // rax

v4 = strlen(a2);
v5 = (int)(v4 + strlen(src) + 1);
v6 = (char *)malloc(v5);
strncpy(v6, src, a3);
strcpy(&v6[a3], a2);
v7 = strlen(a2);
strcpy(&v6[a3 + v7], &src[a3]);
v6[v5 - 1] = 0;
return v6;
}

canonical_decompiled_code:
char *__fastcall func0(char *src, char *a2, int a3)
{
int v4; // ebp
size_t v5; // r12
char *v6; // rbp
size_t v7; // rax

v4 = strlen(a2);
v5 = (int)(v4 + strlen(src) + 1);
v6 = (char *)malloc(v5);
strncpy(v6, src, a3);
strcpy(&v6[a3], a2);
v7 = strlen(a2);
strcpy(&v6[a3 + v7], &src[a3]);
v6[v5 - 1] = 0;
return v6;
}

original_code:
char* strins(char* dest, const char* source, const int index)
{
 int len = strlen(source)+strlen(dest)+1;
 char* newstr = (char*)malloc(sizeof(char)*len);
 strncpy(newstr, dest, index);
 strcpy(newstr+index, source);
 strcpy(newstr+index+strlen(source), dest+index);
 newstr[len-1] = '\0';
 return newstr;
}

canonical_original_code:
char *strins(char *dest, char *source, int index)
{
 int len = strlen(source)+strlen(dest)+1;
 char *newstr = (char*)malloc(sizeof(char)*len);
 strncpy(newstr, dest, index);
 strcpy(newstr+index, source);
 strcpy(newstr+index+strlen(source), dest+index);
 newstr[len-1] = '\0';
 return newstr;
}

variable_types:
dest
char *

variable_types:
source
char *

variable_types: index int

variable_types:
newstr
char *


strsub

decompiled_code:
const char *__fastcall strsub(__int64 a1, int a2, int a3)
{
int v3; // er13
const char *v4; // rbx
__int64 v5; // rax

v3 = a3 - a2;
v4 = (const char *)malloc(a3 - a2 + 2);
if ( v3 >= 0 )
{
v5 = 0LL;
do
{
v4[v5] = *(_BYTE *)(a1 + a2 + v5);
++v5;
}
while ( v5 != v3 + 1 );
}
v4[strlen(v4)] = 0;
return v4;
}

canonical_decompiled_code:
const char *__fastcall func1(__int64 a1, int a2, int a3)
{
int v3; // er13
const char *v4; // rbx
__int64 v5; // rax

v3 = a3 - a2;
v4 = (const char *)malloc(a3 - a2 + 2);
if ( v3 >= 0 )
{
v5 = 0LL;
do
{
v4[v5] = *(_BYTE *)(a1 + a2 + v5);
++v5;
}
while ( v5 != v3 + 1 );
}
v4[strlen(v4)] = 0;
return v4;
}

original_code:
char* strsub(char* expr, const int start, const int end)
{
 char* newstr = (char*)malloc(sizeof(char)*(end-start+2));
 int i;
 for (i = 0; i < end-start+1; i++)
  newstr[i] = expr[i+start];
 newstr[strlen(newstr)] = '\0';
 return newstr;
}

canonical_original_code:
char *strsub(char *expr, int start, int end)
{
 char *newstr = (char*)malloc(sizeof(char)*(end-start+2));
 int i;
 for (i = 0; i < end-start+1; i++)
  newstr[i] = expr[i+start];
 newstr[strlen(newstr)] = '\0';
 return newstr;
}

variable_types:
expr
char *

variable_types: start int

variable_types: end int


strrpl

decompiled_code:
char *__fastcall strrpl(char *s, int a2, int a3, const char *a4)
{
char *v6; // rbx
int v7; // eax
const char *v8; // rbp

v6 = (char *)strsub((__int64)s, 0, a2 - 1);
v7 = strlen(s);
v8 = strsub((__int64)s, a3 + 1, v7 - 1);
strcat(v6, a4);
return strcat(v6, v8);
}

canonical_decompiled_code:
char *__fastcall func2(char *s, int a2, int a3, const char *a4)
{
char *v6; // rbx
int v7; // eax
const char *v8; // rbp

v6 = (char *)func1((__int64)s, 0, a2 - 1);
v7 = strlen(s);
v8 = func1((__int64)s, a3 + 1, v7 - 1);
strcat(v6, a4);
return strcat(v6, v8);
}

original_code:
char* strrpl(char* expr,const int strt, const int end, char* needle)
{
 int len = strlen(expr)+strlen(needle)-end+strt;
 char* newexpr = (char*)malloc(sizeof(char)*len);
 newexpr = strsub(expr,0,strt-1);
 char* tok1 = strsub(expr,end+1,strlen(expr)-1);
 strcat(newexpr,needle);
 strcat(newexpr,tok1);
 return newexpr;
}

canonical_original_code:
char *strrpl(char *expr, int strt, int end, char *needle)
{
 int len = strlen(expr)+strlen(needle)-end+strt;
 char *newexpr = (char*)malloc(sizeof(char)*len);
 newexpr = strsub(expr,0,strt-1);
 char *tok1 = strsub(expr,end+1,strlen(expr)-1);
 strcat(newexpr,needle);
 strcat(newexpr,tok1);
 return newexpr;
}

variable_types: strt int

variable_types:
needle
char *

variable_types: len int

variable_types:
newexpr
char *


updatewins

decompiled_code:
int updatewins()
{
int result; // eax
struct timespec v1; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
requested_time.tv_sec = 0LL;
for ( requested_time.tv_nsec = 10000000LL; running; result = doupdate() )
nanosleep(&requested_time, &v1);
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax
struct timespec v1; // [rsp+0h] [rbp-28h] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
requested_time.tv_sec = 0LL;
for ( requested_time.tv_nsec = 10000000LL; running; result = doupdate() )
nanosleep(&requested_time, &v1);
return result;
}

original_code:
void updatewins()
{
 struct timespec tm_intv;
 struct timespec tm_left = {0};
 tm_intv.tv_sec = 0;
 tm_intv.tv_nsec = 10000000;
 while(running)
 {
  nanosleep(&tm_intv,&tm_left);
  doupdate();
 }
}

variable_types: struct timespec { long tv_sec; long tv_nsec; }


startwinmgr

decompiled_code:
__int64 startwinmgr()
{
pthread_mutex_init(&cmdbar_mutex, 0LL);
pthread_mutex_init(&dispwin_mutex, 0LL);
_init_winstructs();
running = 1;
tui_ready();
updatewins();
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
pthread_mutex_init(&cmdbar_mutex, 0LL);
pthread_mutex_init(&dispwin_mutex, 0LL);
func3();
running = 1;
tui_ready();
func9();
return 0LL;
}

original_code:
void* startwinmgr(void* null)
{
 pthread_mutex_init(&cmdbar_mutex, ((void *)0));
 pthread_mutex_init(&dispwin_mutex, ((void *)0));
 __init_winstructs();
 running = 1;
 tui_ready();
 updatewins();
 return ((void *)0);
}

canonical_original_code:
void *startwinmgr(void *null)
{
 pthread_mutex_init(&cmdbar_mutex, ((void *)0));
 pthread_mutex_init(&dispwin_mutex, ((void *)0));
 __init_winstructs();
 running = 1;
 tui_ready();
 updatewins();
 return ((void *)0);
}


stopwinmgr

decompiled_code:
int stopwinmgr()
{
running = 0;
pthread_mutex_destroy(&dispwin_mutex);
return pthread_mutex_destroy(&cmdbar_mutex);
}

canonical_decompiled_code:
int func11()
{
running = 0;
pthread_mutex_destroy(&dispwin_mutex);
return pthread_mutex_destroy(&cmdbar_mutex);
}

original_code:
void stopwinmgr()
{
 running = 0;
 pthread_mutex_destroy(&dispwin_mutex);
 pthread_mutex_destroy(&cmdbar_mutex);
}


inscell

decompiled_code:
void inscell()
{
;
}

original_code:
struct table* inscell(struct table* tble, const int val, const int x, const int y)
{
 ;
}

canonical_original_code:
struct table *inscell(struct table *tble, int val, int x, int y)
{
 ;
}

variable_types:
tble
struct table *

variable_types: val int

variable_types: x int

variable_types: y int

user_defined_types: struct table { int x_size; int y_size; int *cells; }


getcell

decompiled_code:
__int64 __fastcall getcell(__int64 a1, int a2, int a3)
{
__int64 result; // rax
int v4; // ecx

if ( !a1 )
return 0LL;
result = 1LL;
if ( *(_DWORD *)a1 > a2 )
{
v4 = *(_DWORD *)(a1 + 4);
if ( v4 > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 1LL;
else
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 4LL * (v4 * a2 + a3));
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3)
{
__int64 result; // rax
int v4; // ecx

if ( !a1 )
return 0LL;
result = 1LL;
if ( *(_DWORD *)a1 > a2 )
{
v4 = *(_DWORD *)(a1 + 4);
if ( v4 > a3 )
{
if ( a2 < 0 || a3 < 0 )
result = 1LL;
else
result = *(unsigned int *)(*(_QWORD *)(a1 + 8) + 4LL * (v4 * a2 + a3));
}
}
return result;
}

original_code:
int getcell(struct table* tble, int x, int y)
{
 if(tble == ((void *)0))
  return 0;
 else if(x >= tble->x_size || y >= tble->y_size || x < 0 || y < 0)
  return 1;
 return tble->cells[x*(tble->y_size)+y];
}

canonical_original_code:
int getcell(struct table *tble, int x, int y)
{
 if(tble == ((void *)0))
  return 0;
 else if(x >= tble->x_size || y >= tble->y_size || x < 0 || y < 0)
  return 1;
 return tble->cells[x*(tble->y_size)+y];
}

variable_types: struct table { int x_size; int y_size; int *cells; } *


setcell

decompiled_code:
__int64 __fastcall setcell(__int64 a1, int a2, int a3, int a4)
{
__int64 result; // rax
int v5; // er8

if ( !a1 )
return 0LL;
result = 1LL;
if ( *(_DWORD *)a1 > a3 )
{
v5 = *(_DWORD *)(a1 + 4);
if ( v5 > a4 )
{
if ( a3 < 0 || a4 < 0 )
{
result = 1LL;
}
else
{
*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * (v5 * a3 + a4)) = a2;
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int a3, int a4)
{
__int64 result; // rax
int v5; // er8

if ( !a1 )
return 0LL;
result = 1LL;
if ( *(_DWORD *)a1 > a3 )
{
v5 = *(_DWORD *)(a1 + 4);
if ( v5 > a4 )
{
if ( a3 < 0 || a4 < 0 )
{
result = 1LL;
}
else
{
*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * (v5 * a3 + a4)) = a2;
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
int setcell(struct table* tble, int cell_value, int x, int y)
{
 if(tble == ((void *)0))
  return 0;
 if(x >= tble->x_size || y >= tble->y_size || x < 0 || y < 0)
  return 1;
 int index = x*(tble->y_size)+y;
 *(tble->cells+(index)) = cell_value;
 return -1;
}

canonical_original_code:
int setcell(struct table *tble, int cell_value, int x, int y)
{
 if(tble == ((void *)0))
  return 0;
 if(x >= tble->x_size || y >= tble->y_size || x < 0 || y < 0)
  return 1;
 int index = x*(tble->y_size)+y;
 *(tble->cells+(index)) = cell_value;
 return -1;
}


strins

decompiled_code:
char *__fastcall strins(char *src, char *s, size_t n, char *dest)
{
size_t v6; // r15
char *result; // rax
void *v8; // rsp
size_t v9; // rax
char desta[48]; // [rsp+0h] [rbp-30h] BYREF

if ( dest == 0LL || s == 0LL || !src )
return 0LL;
v6 = strlen(src);
result = 0LL;
if ( v6 >= n )
{
v8 = alloca(v6 + strlen(s) + 1);
strncpy(desta, src, n);
strcpy(&desta[n], s);
v9 = strlen(s);
strcpy(&desta[v9 + n], &src[n]);
result = strcpy(dest, desta);
}
return result;
}

canonical_decompiled_code:
char *__fastcall func6(char *src, char *s, size_t n, char *dest)
{
size_t v6; // r15
char *result; // rax
void *v8; // rsp
size_t v9; // rax
char desta[48]; // [rsp+0h] [rbp-30h] BYREF

if ( dest == 0LL || s == 0LL || !src )
return 0LL;
v6 = strlen(src);
result = 0LL;
if ( v6 >= n )
{
v8 = alloca(v6 + strlen(s) + 1);
strncpy(desta, src, n);
strcpy(&desta[n], s);
v9 = strlen(s);
strcpy(&desta[v9 + n], &src[n]);
result = strcpy(dest, desta);
}
return result;
}

original_code:
char* strins(char* haystack, char* needle, size_t index, char* buff)
{
 if(!haystack || !needle || !buff) return ((void *)0);
 if(index < 0 || index > strlen(haystack)) return ((void *)0);
 char tmpbuff[strlen(haystack)+strlen(needle)+1];
 strncpy(tmpbuff, haystack, index);
 strcpy(tmpbuff+index, needle);
 strcpy(tmpbuff+index+strlen(needle), haystack+index);
 strcpy(buff,tmpbuff);
 return buff;
}

canonical_original_code:
char *strins(char *haystack, char *needle, unsigned long index, char *buff)
{
 if(!haystack || !needle || !buff) return ((void *)0);
 if(index < 0 || index > strlen(haystack)) return ((void *)0);
 char tmpbuff[-1];
 strncpy(tmpbuff, haystack, index);
 strcpy(tmpbuff+index, needle);
 strcpy(tmpbuff+index+strlen(needle), haystack+index);
 strcpy(buff,tmpbuff);
 return buff;
}

variable_types:
haystack
char *

variable_types:
index
unsigned long

variable_types:
buff
char *

variable_types:
tmpbuff
char [-1]


strrpl

decompiled_code:
char *__fastcall strrpl(char *src, char *s, size_t n, size_t a4, char *a5)
{
size_t v8; // rax
size_t v9; // rdx
size_t v10; // rax

if ( a5 == 0LL || s == 0LL || !src )
return 0LL;
v8 = strlen(src);
v9 = a4;
if ( v8 <= a4 )
v9 = v8;
if ( n > v9 )
return 0LL;
if ( v8 < a4 || n > a4 )
return 0LL;
strncpy(a5, src, n);
strcpy(&a5[n], s);
v10 = strlen(s);
strcpy(&a5[v10 + n], &src[a4 + 1]);
return a5;
}

canonical_decompiled_code:
char *__fastcall func7(char *src, char *s, size_t n, size_t a4, char *a5)
{
size_t v8; // rax
size_t v9; // rdx
size_t v10; // rax

if ( a5 == 0LL || s == 0LL || !src )
return 0LL;
v8 = strlen(src);
v9 = a4;
if ( v8 <= a4 )
v9 = v8;
if ( n > v9 )
return 0LL;
if ( v8 < a4 || n > a4 )
return 0LL;
strncpy(a5, src, n);
strcpy(&a5[n], s);
v10 = strlen(s);
strcpy(&a5[v10 + n], &src[a4 + 1]);
return a5;
}

original_code:
char* strrpl(char* haystack, char* needle, size_t strt, size_t end, char* buff)
{
 if(!haystack || !needle || !buff) return ((void *)0);
 size_t haystacklen = strlen(haystack);
 if(strt < 0 || strt > haystacklen || strt > end) return ((void *)0);
 if(end < 0 || end > haystacklen || end < strt) return ((void *)0);
 strncpy(buff,haystack,strt);
 strcpy(buff+strt,needle);
 strcpy(buff+strt+strlen(needle),haystack+end+1);
 return buff;
}

canonical_original_code:
char *strrpl(char *haystack, char *needle, unsigned long strt, unsigned long end, char *buff)
{
 if(!haystack || !needle || !buff) return ((void *)0);
 unsigned long haystacklen = strlen(haystack);
 if(strt < 0 || strt > haystacklen || strt > end) return ((void *)0);
 if(end < 0 || end > haystacklen || end < strt) return ((void *)0);
 strncpy(buff,haystack,strt);
 strcpy(buff+strt,needle);
 strcpy(buff+strt+strlen(needle),haystack+end+1);
 return buff;
}

variable_types:
strt
unsigned long

variable_types:
end
unsigned long

variable_types:
haystacklen
unsigned long


strsub

decompiled_code:
char *__fastcall strsub(const char *a1, unsigned __int64 a2, unsigned __int64 a3, char *a4)
{
size_t v6; // rax
unsigned __int64 v7; // rcx
char *v8; // rdx
unsigned __int64 v9; // rbx
void *v10; // rsp
char v12[48]; // [rsp+0h] [rbp-30h] BYREF

if ( a2 > a3 || a4 == 0LL || !a1 )
return 0LL;
v6 = strlen(a1);
v7 = a3;
if ( a2 >= a3 )
v7 = a2;
v8 = 0LL;
if ( v7 <= v6 - 1 )
{
v9 = a3 - a2;
v10 = alloca(v9 + 2);
strncpy(v12, &a1[a2], v9 + 1);
v12[v9 + 1] = 0;
strncpy(a4, v12, v9 + 2);
v8 = a4;
}
return v8;
}

canonical_decompiled_code:
char *__fastcall func8(const char *a1, unsigned __int64 a2, unsigned __int64 a3, char *a4)
{
size_t v6; // rax
unsigned __int64 v7; // rcx
char *v8; // rdx
unsigned __int64 v9; // rbx
void *v10; // rsp
char v12[48]; // [rsp+0h] [rbp-30h] BYREF

if ( a2 > a3 || a4 == 0LL || !a1 )
return 0LL;
v6 = strlen(a1);
v7 = a3;
if ( a2 >= a3 )
v7 = a2;
v8 = 0LL;
if ( v7 <= v6 - 1 )
{
v9 = a3 - a2;
v10 = alloca(v9 + 2);
strncpy(v12, &a1[a2], v9 + 1);
v12[v9 + 1] = 0;
strncpy(a4, v12, v9 + 2);
v8 = a4;
}
return v8;
}

original_code:
char* strsub(char* expr, size_t strt, size_t end, char* buff)
{
 if(!expr || !buff) return ((void *)0);
 if(strt < 0 || strt > end || strt > strlen(expr)-1) return ((void *)0);
 if(end < 0|| end < strt || end > strlen(expr)-1) return ((void *)0);
 char tmpbuff[end-strt+2];
 strncpy(tmpbuff,expr+strt,end-strt+1);
 tmpbuff[end-strt+1] = '\0';
 strncpy(buff,tmpbuff,end-strt+2);
 return buff;
}

canonical_original_code:
char *strsub(char *expr, unsigned long strt, unsigned long end, char *buff)
{
 if(!expr || !buff) return ((void *)0);
 if(strt < 0 || strt > end || strt > strlen(expr)-1) return ((void *)0);
 if(end < 0|| end < strt || end > strlen(expr)-1) return ((void *)0);
 char tmpbuff[-1];
 strncpy(tmpbuff,expr+strt,end-strt+1);
 tmpbuff[end-strt+1] = '\0';
 strncpy(buff,tmpbuff,end-strt+2);
 return buff;
}

variable_types:
expr
char *


decompiled_code:
__int64 __fastcall comp_ia(int a1)
{
unsigned int v1; // ebx

v1 = a1;
printw("The IA choose to remove %d matches\n", 3LL);
if ( a1 <= 0 || (v1 = a1 - 3, a1 - 3 <= 0) )
{
printw("You WIN !\nPress any key to quit\n");
wgetch(stdscr);
}
return player(v1);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
unsigned int v1; // ebx

v1 = a1;
printw("The IA choose to remove %d matches\n", 3LL);
if ( a1 <= 0 || (v1 = a1 - 3, a1 - 3 <= 0) )
{
printw("You WIN !\nPress any key to quit\n");
wgetch(stdscr);
}
return func1(v1);
}

original_code:
int comp_ia(int test)
{
  int taken_ia;
  taken_ia = 3;
  printw("The IA choose to remove %d matches\n", taken_ia);
  if (test > 0)
    test -= taken_ia;
  if (test <= 0)
    {
      printw("You WIN !\nPress any key to quit\n");
      wgetch(stdscr);
    }
  player(test);
}

variable_types: test int


player

decompiled_code:
__int64 __fastcall player(int a1)
{
int v1; // ebp
__int64 v3; // rbx

v1 = a1;
if ( a1 > 0 )
{
while ( 1 )
{
while ( 1 )
{
printw("You are on the row which have %d matches left\n", (unsigned int)v1);
printw("How many matches do you want to take ?\n");
v3 = (unsigned int)(wgetch(stdscr) - 48);
printw("You choose to remove %d matches\n", v3);
if ( (_DWORD)v3 )
break;
printw("Don't cheat !\n");
if ( v1 <= 0 )
return 0LL;
}
v1 -= v3;
if ( v1 <= 0 )
break;
comp_ia(v1);
}
printw("You LOOSE !\nPress any key to quit\n");
wgetch(stdscr);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // ebp
__int64 v3; // rbx

v1 = a1;
if ( a1 > 0 )
{
while ( 1 )
{
while ( 1 )
{
printw("You are on the row which have %d matches left\n", (unsigned int)v1);
printw("How many matches do you want to take ?\n");
v3 = (unsigned int)(wgetch(stdscr) - 48);
printw("You choose to remove %d matches\n", v3);
if ( (_DWORD)v3 )
break;
printw("Don't cheat !\n");
if ( v1 <= 0 )
return 0LL;
}
v1 -= v3;
if ( v1 <= 0 )
break;
func0(v1);
}
printw("You LOOSE !\nPress any key to quit\n");
wgetch(stdscr);
}
return 0LL;
}

original_code:
int player(int test)
{
  int taken;
  while (test > 0)
    {
      printw("You are on the row which have %d matches left\n", test);
      printw("How many matches do you want to take ?\n");
      taken = wgetch(stdscr) - '0';
      printw("You choose to remove %d matches\n", taken);
      if (taken == 0)
 printw("Don't cheat !\n");
      else
 {
   test -= taken;
   if (test <= 0)
     {
       printw("You LOOSE !\nPress any key to quit\n");
       wgetch(stdscr);
       return (0);
     }
   comp_ia(test);
 }
    }
  return (0);
}

variable_types: taken int


decompiled_code:
__int64 who_begins()
{
char v0; // al

printw("Do you want to play first ? y/N\n");
v0 = wgetch(stdscr);
if ( v0 == 121 )
{
printw("You choose to play first\n");
player(16);
}
else if ( v0 == 78 )
{
printw("You choose to play after the IA\n");
comp_ia(16);
}
else
{
printw("Choose y or N please\n");
who_begins();
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
char v0; // al

printw("Do you want to play first ? y/N\n");
v0 = wgetch(stdscr);
if ( v0 == 121 )
{
printw("You choose to play first\n");
func1(16);
}
else if ( v0 == 78 )
{
printw("You choose to play after the IA\n");
func0(16);
}
else
{
printw("Choose y or N please\n");
func2();
}
return 0LL;
}

original_code:
int who_begins()
{
  char choice;
  printw("Do you want to play first ? y/N\n");
  choice = wgetch(stdscr);
  if (choice == 'y')
    {
      printw("You choose to play first\n");
      player(16);
    }
  else if (choice == 'N')
    {
      printw("You choose to play after the IA\n");
      comp_ia(16);
    }
  else
    {
      printw("Choose y or N please\n");
      who_begins();
    }
  return (0);
}

variable_types: choice char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
initscr();
raw();
noecho();
who_begins();
endwin();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
initscr();
raw();
noecho();
func2();
endwin();
return 0;
}

original_code:
int main(int ac, char **av)
{
  initscr();
  raw();
  noecho();
  who_begins();
  endwin();
  return (0);
}

variable_types: ac int

variable_types:
av
char **


sqroot

repo: theaudiobible/de-ess

original_code:
unsigned long long sqroot( unsigned long long num ) {
 unsigned long long l2, u, v, u2, n;
 if( 2 > num ) return( num );
 u = num;
 l2 = 0;
 while( u >>= 2 ) l2++;
 u = 1L << l2;
 v = u;
 u2 = u << l2;
 while( l2-- ) {
  v >>= 1;
  n = (u + u + v) << l2;
  n += u2;
  if( n <= num ) {
   u += v;
   u2 = n;
  }
 }
 return( u );
}

canonical_original_code:
unsigned long long sqroot(unsigned long long num) {
 unsigned long long l2; unsigned long long u; unsigned long long v; unsigned long long u2; unsigned long long n;
 if( 2 > num ) return( num );
 u = num;
 l2 = 0;
 while( u >>= 2 ) l2++;
 u = 1L << l2;
 v = u;
 u2 = u << l2;
 while( l2-- ) {
  v >>= 1;
  n = (u + u + v) << l2;
  n += u2;
  if( n <= num ) {
   u += v;
   u2 = n;
  }
 }
 return( u );
}

variable_types:
num
unsigned long long

variable_types:
u
unsigned long long

variable_types:
v
unsigned long long

variable_types:
n
unsigned long long

return_type: unsigned long long


filter

original_code:
int filter(short *x, short *y, int size, double *h) {
 double sum, divisor;
 int k, n, overflowed = 0;
 divisor = (double)0x7fff;
 for (n = 0; n < size; n++) {
  sum = 0;
  for (k = 0; k < 81; k++) {
   if (k > n) break;
   sum += h[k] * ((double)x[n - k])/divisor;
  }
  y[n] = (short)(sum * divisor);
 }
 if (overflowed) {
  return 0;
 } else {
  return !0;
 }
}

canonical_original_code:
int filter(short *x, short *y, int size, double *h) {
 double sum; double divisor;
 int k; int n; int overflowed = 0;
 divisor = (double)0x7fff;
 for (n = 0; n < size; n++) {
  sum = 0;
  for (k = 0; k < 81; k++) {
   if (k > n) break;
   sum += h[k] * ((double)x[n - k])/divisor;
  }
  y[n] = (short)(sum * divisor);
 }
 if (overflowed) {
  return 0;
 } else {
  return !0;
 }
}

variable_types:
x
short *

variable_types:
y
short *

variable_types: size int

variable_types:
h
double *

variable_types: sum double

variable_types: divisor double

variable_types: k int

variable_types: n int

variable_types: overflowed int


main

repo: thebyrd/libuv-env

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx

v3 = uv_loop_new(argc, argv, envp);
puts("Now quititng.");
uv_run(v3, 0LL);
return 0;
}

original_code:
int
main () {
  uv_loop_t *loop = uv_loop_new();
  printf("Now quititng.\n");
  uv_run(loop, UV_RUN_DEFAULT);
  return 0;
}

canonical_original_code:
int main() {
  struct uv_loop_s *loop = uv_loop_new();
  printf("Now quititng.\n");
  uv_run(loop, UV_RUN_DEFAULT);
  return 0;
}

variable_types: struct uv_loop_s { void *data; unsigned int active_handles; void *handle_queue[2]; union { void *unused[2]; unsigned int count; } active_reqs; unsigned int stop_flag; unsigned long flags; int backend_fd; void *pending_queue[2]; void *watcher_queue[2]; struct uv__io_s { void (*cb)(struct uv_loop_s *loop, struct uv__io_s *w, unsigned int events); void *pending_queue[2]; void *watcher_queue[2]; unsigned int pevents; unsigned int events; int fd; } **watchers; unsigned int nwatchers; unsigned int nfds; void *wq[2]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } wq_mutex; struct uv_async_s { void *data; struct uv_loop_s *loop; enum uv_handle_type { UV_UNKNOWN_HANDLE=0, UV_ASYNC=1, UV_CHECK=2, UV_FS_EVENT=3, UV_FS_POLL=4, UV_HANDLE=5, UV_IDLE=6, UV_NAMED_PIPE=7, UV_POLL=8, UV_PREPARE=9, UV_PROCESS=10, UV_STREAM=11, UV_TCP=12, UV_TIMER=13, UV_TTY=14, UV_UDP=15, UV_SIGNAL=16, UV_FILE=17, UV_HANDLE_TYPE_MAX=18 } type; void (*close_cb)(struct uv_handle_s { void *data; struct uv_loop_s *loop; enum uv_handle_type { UV_UNKNOWN_HANDLE=0, UV_ASYNC=1, UV_CHECK=2, UV_FS_EVENT=3, UV_FS_POLL=4, UV_HANDLE=5, UV_IDLE=6, UV_NAMED_PIPE=7, UV_POLL=8, UV_PREPARE=9, UV_PROCESS=10, UV_STREAM=11, UV_TCP=12, UV_TIMER=13, UV_TTY=14, UV_UDP=15, UV_SIGNAL=16, UV_FILE=17, UV_HANDLE_TYPE_MAX=18 } type; void (*close_cb)(struct uv_handle_s *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; } *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; void (*async_cb)(struct uv_async_s *handle); void *queue[2]; int pending; } wq_async; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } cloexec_lock; struct uv_handle_s *closing_handles; void *process_handles[2]; void *prepare_handles[2]; void *check_handles[2]; void *idle_handles[2]; void *async_handles[2]; void (*async_unused)(); struct uv__io_s async_io_watcher; int async_wfd; struct { void *min; unsigned int nelts; } timer_heap; unsigned long timer_counter; unsigned long time; int signal_pipefd[2]; struct uv__io_s signal_io_watcher; struct uv_signal_s { void *data; struct uv_loop_s *loop; enum uv_handle_type { UV_UNKNOWN_HANDLE=0, UV_ASYNC=1, UV_CHECK=2, UV_FS_EVENT=3, UV_FS_POLL=4, UV_HANDLE=5, UV_IDLE=6, UV_NAMED_PIPE=7, UV_POLL=8, UV_PREPARE=9, UV_PROCESS=10, UV_STREAM=11, UV_TCP=12, UV_TIMER=13, UV_TTY=14, UV_UDP=15, UV_SIGNAL=16, UV_FILE=17, UV_HANDLE_TYPE_MAX=18 } type; void (*close_cb)(struct uv_handle_s *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; void (*signal_cb)(struct uv_signal_s *handle, int signum); int signum; struct { struct uv_signal_s *rbe_left; struct uv_signal_s *rbe_right; struct uv_signal_s *rbe_parent; int rbe_color; } tree_entry; unsigned int caught_signals; unsigned int dispatched_signals; } child_watcher; int emfile_fd; struct uv__io_s inotify_read_watcher; void *inotify_watchers; int inotify_fd; } *

user_defined_types: struct uv_loop_s { void *data; unsigned int active_handles; void *handle_queue[2]; union { void *unused[2]; unsigned int count; } active_reqs; unsigned int stop_flag; unsigned long flags; int backend_fd; void *pending_queue[2]; void *watcher_queue[2]; struct uv__io_s **watchers; unsigned int nwatchers; unsigned int nfds; void *wq[2]; union pthread_mutex_t wq_mutex; struct uv_async_s wq_async; union pthread_rwlock_t cloexec_lock; struct uv_handle_s *closing_handles; void *process_handles[2]; void *prepare_handles[2]; void *check_handles[2]; void *idle_handles[2]; void *async_handles[2]; void (*async_unused)(); struct uv__io_s async_io_watcher; int async_wfd; struct { void *min; unsigned int nelts; } timer_heap; unsigned long timer_counter; unsigned long time; int signal_pipefd[2]; struct uv__io_s signal_io_watcher; struct uv_signal_s child_watcher; int emfile_fd; struct uv__io_s inotify_read_watcher; void *inotify_watchers; int inotify_fd; }

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

user_defined_types: struct uv_async_s { void *data; struct uv_loop_s *loop; enum uv_handle_type type; void (*close_cb)(struct uv_handle_s *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; void (*async_cb)(struct uv_async_s *handle); void *queue[2]; int pending; }

user_defined_types: union pthread_rwlock_t { struct __pthread_rwlock_arch_t __data; char __size[56]; long __align; }

user_defined_types: struct uv_signal_s { void *data; struct uv_loop_s *loop; enum uv_handle_type type; void (*close_cb)(struct uv_handle_s *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; void (*signal_cb)(struct uv_signal_s *handle, int signum); int signum; struct { struct uv_signal_s *rbe_left; struct uv_signal_s *rbe_right; struct uv_signal_s *rbe_parent; int rbe_color; } tree_entry; unsigned int caught_signals; unsigned int dispatched_signals; }

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

user_defined_types: struct uv_handle_s { void *data; struct uv_loop_s *loop; enum uv_handle_type type; void (*close_cb)(struct uv_handle_s *handle); void *handle_queue[2]; union { int fd; void *reserved[4]; } u; struct uv_handle_s *next_closing; unsigned int flags; }


popQueue

repo: thecelticone/aho-corasick

decompiled_code:
__int64 __fastcall popQueue(_QWORD *a1)
{
__int64 v1; // rbx

v1 = a1[1];
free(a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(_QWORD *a1)
{
__int64 v1; // rbx

v1 = a1[1];
free(a1);
return v1;
}

original_code:
Queue popQueue (Queue q) {
  Queue ptr1 = q;
  Queue ptr2 = q->next;
  free (ptr1);
  return ptr2;
}

canonical_original_code:
struct _queue *popQueue(struct _queue *q) {
  struct _queue *ptr1 = q;
  struct _queue *ptr2 = q->next;
  free (ptr1);
  return ptr2;
}

variable_types:
q
struct _queue *

variable_types: struct _queue { int elm; struct _queue *next; } *


topQueue

decompiled_code:
__int64 __fastcall topQueue(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1)
{
return *a1;
}

original_code:
int topQueue (Queue q) {
  return q->elm;
}

canonical_original_code:
int topQueue(struct _queue *q) {
  return q->elm;
}

user_defined_types: struct _queue { int elm; struct _queue *next; }


printQueue

decompiled_code:
int __fastcall printQueue(unsigned int *a1)
{
int result; // eax
unsigned int *i; // rbx

if ( a1 )
{
result = printf("%d ", *a1);
for ( i = (unsigned int *)*((_QWORD *)a1 + 1); i; i = (unsigned int *)*((_QWORD *)i + 1) )
result = printf("| %d ", *i);
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(unsigned int *a1)
{
int result; // eax
unsigned int *i; // rbx

if ( a1 )
{
result = printf("%d ", *a1);
for ( i = (unsigned int *)*((_QWORD *)a1 + 1); i; i = (unsigned int *)*((_QWORD *)i + 1) )
result = printf("| %d ", *i);
}
return result;
}

original_code:
void printQueue (Queue q) {
  Queue ptr = q;
  if (ptr != ((void *)0)) {
    printf("%d ", ptr->elm);
    ptr = ptr->next;
  }
  while (ptr != ((void *)0)) {
    printf("| %d ", ptr->elm);
    ptr = ptr->next;
  }
}

canonical_original_code:
void printQueue(struct _queue *q) {
  struct _queue *ptr = q;
  if (ptr != ((void *)0)) {
    printf("%d ", ptr->elm);
    ptr = ptr->next;
  }
  while (ptr != ((void *)0)) {
    printf("| %d ", ptr->elm);
    ptr = ptr->next;
  }
}


getFromList

decompiled_code:
__int64 __fastcall getFromList(unsigned int *a1, char a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)a1 + 4) != a2 )
{
a1 = (unsigned int *)*((_QWORD *)a1 + 1);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int *a1, char a2)
{
if ( !a1 )
return 0xFFFFFFFFLL;
while ( *((_BYTE *)a1 + 4) != a2 )
{
a1 = (unsigned int *)*((_QWORD *)a1 + 1);
if ( !a1 )
return 0xFFFFFFFFLL;
}
return *a1;
}

original_code:
int getFromList(List list, char label) {
  List tmp = list;
  while (tmp != ((void *)0)) {
    if (tmp->letter == label) {
      return tmp->targetNode;
    }
    tmp = tmp->next;
  }
  return -1;
}

canonical_original_code:
int getFromList(struct _list *list, char label) {
  struct _list *tmp = list;
  while (tmp != ((void *)0)) {
    if (tmp->letter == label) {
      return tmp->targetNode;
    }
    tmp = tmp->next;
  }
  return -1;
}

variable_types: struct _list { int targetNode; char letter; struct _list *next; } *

variable_types: label char


printList

decompiled_code:
int __fastcall printList(unsigned int *a1)
{
unsigned int *i; // rbx

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
printf("|node : %i; label : %c|", *i, (unsigned int)*((char *)i + 4));
return puts("X");
}

canonical_decompiled_code:
int __fastcall func16(unsigned int *a1)
{
unsigned int *i; // rbx

for ( i = a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
printf("|node : %i; label : %c|", *i, (unsigned int)*((char *)i + 4));
return puts("X");
}

original_code:
void printList(List list) {
  while (list != ((void *)0)) {
    printf("|node : %i; label : %c|", list->targetNode, list->letter);
    list = list->next;
  }
  printf("X\n");
}

canonical_original_code:
void printList(struct _list *list) {
  while (list != ((void *)0)) {
    printf("|node : %i; label : %c|", list->targetNode, list->letter);
    list = list->next;
  }
  printf("X\n");
}

user_defined_types: struct _list { int targetNode; char letter; struct _list *next; }


repo: thedotcomchase/Hot-Fuzz

decompiled_code:
__int64 __fastcall get_field(char *s)
{
__int64 v1; // r13
__int64 v2; // rax
char *v3; // rbx
char v4; // bp
int v5; // er12
const unsigned __int16 **v6; // rdx
char v7; // al
char *i; // rax

v1 = cursor;
if ( *(_BYTE *)cursor == 32 )
{
v2 = cursor + 1;
do
{
v1 = v2;
cursor = v2++;
}
while ( *(_BYTE *)(v2 - 1) == 32 );
}
v3 = (char *)cursor;
v4 = *(_BYTE *)cursor;
v5 = 0;
if ( !*(_BYTE *)cursor )
{
LABEL_5:
fprintf(stderr, "unexpected end of line on line %d\n", (unsigned int)lineno);
exit(1);
}
while ( 1 )
{
if ( v5 > 0 || !strchr(s, v4) )
{
if ( v4 == 123 )
++v5;
else
v5 -= v4 == 125;
goto LABEL_7;
}
if ( v4 == 45 )
break;
if ( v4 != 40 || v3[1] == 36 )
goto LABEL_18;
LABEL_7:
cursor = (__int64)++v3;
v4 = *v3;
if ( !*v3 )
goto LABEL_5;
}
v6 = _ctype_b_loc();
v7 = v3[1];
if ( ((*v6)[v7] & 8) != 0 || v7 == 45 )
goto LABEL_7;
LABEL_18:
peek = v4;
for ( i = v3 - 1; *i == 32; --i )
;
i[1] = 0;
++cursor;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s)
{
__int64 v1; // r13
__int64 v2; // rax
char *v3; // rbx
char v4; // bp
int v5; // er12
const unsigned __int16 **v6; // rdx
char v7; // al
char *i; // rax

v1 = cursor;
if ( *(_BYTE *)cursor == 32 )
{
v2 = cursor + 1;
do
{
v1 = v2;
cursor = v2++;
}
while ( *(_BYTE *)(v2 - 1) == 32 );
}
v3 = (char *)cursor;
v4 = *(_BYTE *)cursor;
v5 = 0;
if ( !*(_BYTE *)cursor )
{
LABEL_5:
fprintf(stderr, "unexpected end of line on line %d\n", (unsigned int)lineno);
exit(1);
}
while ( 1 )
{
if ( v5 > 0 || !strchr(s, v4) )
{
if ( v4 == 123 )
++v5;
else
v5 -= v4 == 125;
goto LABEL_7;
}
if ( v4 == 45 )
break;
if ( v4 != 40 || v3[1] == 36 )
goto LABEL_18;
LABEL_7:
cursor = (__int64)++v3;
v4 = *v3;
if ( !*v3 )
goto LABEL_5;
}
v6 = _ctype_b_loc();
v7 = v3[1];
if ( ((*v6)[v7] & 8) != 0 || v7 == 45 )
goto LABEL_7;
LABEL_18:
peek = v4;
for ( i = v3 - 1; *i == 32; --i )
;
i[1] = 0;
++cursor;
return v1;
}

original_code:
char *get_field(char *term)
{
     char *result, *end;
     int level = 0;
     while (*cursor == ' ')
   cursor++;
     result = cursor;
     while (*cursor != '\0' &&
     (level > 0
      || ! strchr(term, *cursor)
      || cursor[0] == '-' && ((*__ctype_b_loc ())[(int) ((cursor[1]))] & (unsigned short int) _ISalnum)
      || cursor[0] == '-' && cursor[1] == '-'
      || cursor[0] == '(' && cursor[1] != '$')) {
   if (*cursor == '{')
        level++;
   else if (*cursor == '}')
        level--;
   cursor++;
     }
     if (*cursor == '\0') {
   fprintf(stderr, "unexpected end of line on line %d\n", lineno);
   exit(1);
     }
     peek = *cursor;
     end = cursor-1;
     while (*end == ' ')
   end--;
     end[1] = '\0';
     cursor++;
     return result;
}

canonical_original_code:
char *get_field(char *term)
{
     char *result; char *end;
     int level = 0;
     while (*cursor == ' ')
   cursor++;
     result = cursor;
     while (*cursor != '\0' &&
     (level > 0
      || ! strchr(term, *cursor)
      || cursor[0] == '-' && ((*__ctype_b_loc ())[(int) ((cursor[1]))] & (unsigned short) _ISalnum)
      || cursor[0] == '-' && cursor[1] == '-'
      || cursor[0] == '(' && cursor[1] != '$')) {
   if (*cursor == '{')
        level++;
   else if (*cursor == '}')
        level--;
   cursor++;
     }
     if (*cursor == '\0') {
   fprintf(stderr, "unexpected end of line on line %d\n", lineno);
   exit(1);
     }
     peek = *cursor;
     end = cursor-1;
     while (*end == ' ')
   end--;
     end[1] = '\0';
     cursor++;
     return result;
}

variable_types:
term
char *

variable_types:
result
char *

variable_types:
end
char *

variable_types: level int


decompiled_code:
char *load_file()
{
char *result; // rax
unsigned int v1; // ebx

while ( 1 )
{
result = fgets(line_0, 1024, stdin);
if ( !result )
break;
v1 = ++lineno;
cursor = (__int64)strchr(line_0, 123);
if ( !cursor )
{
fprintf(stderr, "Missing '{' on line %d\n", v1);
exit(1);
}
read_record();
}
return result;
}

canonical_decompiled_code:
char *func1()
{
char *result; // rax
unsigned int v1; // ebx

while ( 1 )
{
result = fgets(line_0, 1024, stdin);
if ( !result )
break;
v1 = ++lineno;
cursor = (__int64)strchr(line_0, 123);
if ( !cursor )
{
fprintf(stderr, "Missing '{' on line %d\n", v1);
exit(1);
}
read_record();
}
return result;
}

original_code:
void load_file(void)
{
     static char line[1024];
     while (fgets(line, 1024, stdin) != ((void *)0)) {
   lineno++;
   cursor = strchr(line, '{');
   if (cursor == ((void *)0)) {
        fprintf(stderr, "Missing '{' on line %d\n", lineno);
        exit(1);
   }
   read_record();
     }
}

canonical_original_code:
void load_file()
{
     static char line[1024];
     while (fgets(line, 1024, stdin) != ((void *)0)) {
   lineno++;
   cursor = strchr(line, '{');
   if (cursor == ((void *)0)) {
        fprintf(stderr, "Missing '{' on line %d\n", lineno);
        exit(1);
   }
   read_record();
     }
}

variable_types:
line
char [1024]


lexico

decompiled_code:
__int64 __fastcall lexico(const char *a1, const char *a2)
{
const char *v2; // r13
const char *v3; // rbp
const __int32_t *v4; // rax
__int32_t v5; // edx
__int32_t v6; // eax
char v7; // r12
char v8; // bl
const unsigned __int16 *v9; // rdx
const unsigned __int16 *v10; // rax
unsigned int v12; // [rsp+Ch] [rbp-4Ch]

v2 = a1 + 1;
v3 = a2;
v12 = 0;
while ( 1 )
{
v7 = *(v2 - 1);
if ( v7 && ((*_ctype_b_loc())[v7] & 8) == 0 && v7 != 32 )
goto LABEL_7;
v8 = *v3;
if ( *v3 )
{
v9 = *_ctype_b_loc();
do
{
if ( (v9[v8] & 8) != 0 )
break;
if ( v8 == 32 )
break;
v8 = *++v3;
}
while ( *v3 );
}
v10 = *_ctype_b_loc();
if ( (v10[v7] & 8) == 0 )
break;
if ( (v10[v8] & 8) == 0 )
return 1;
v4 = *_ctype_tolower_loc();
v5 = v4[v7];
v6 = v4[v8];
if ( v5 != v6 )
return (unsigned int)(v5 - v6);
if ( !v12 )
v12 = v7 - v8;
++v3;
LABEL_7:
++v2;
}
if ( (v10[v8] & 8) != 0 )
return (unsigned int)-1;
if ( !v12 )
v12 = strcmp(a1, a2);
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
const char *v2; // r13
const char *v3; // rbp
const __int32_t *v4; // rax
__int32_t v5; // edx
__int32_t v6; // eax
char v7; // r12
char v8; // bl
const unsigned __int16 *v9; // rdx
const unsigned __int16 *v10; // rax
unsigned int v12; // [rsp+Ch] [rbp-4Ch]

v2 = a1 + 1;
v3 = a2;
v12 = 0;
while ( 1 )
{
v7 = *(v2 - 1);
if ( v7 && ((*_ctype_b_loc())[v7] & 8) == 0 && v7 != 32 )
goto LABEL_7;
v8 = *v3;
if ( *v3 )
{
v9 = *_ctype_b_loc();
do
{
if ( (v9[v8] & 8) != 0 )
break;
if ( v8 == 32 )
break;
v8 = *++v3;
}
while ( *v3 );
}
v10 = *_ctype_b_loc();
if ( (v10[v7] & 8) == 0 )
break;
if ( (v10[v8] & 8) == 0 )
return 1;
v4 = *_ctype_tolower_loc();
v5 = v4[v7];
v6 = v4[v8];
if ( v5 != v6 )
return (unsigned int)(v5 - v6);
if ( !v12 )
v12 = v7 - v8;
++v3;
LABEL_7:
++v2;
}
if ( (v10[v8] & 8) != 0 )
return (unsigned int)-1;
if ( !v12 )
v12 = strcmp(a1, a2);
return v12;
}

original_code:
int lexico(char *s, char *t)
{
     char *a = s, *b = t;
     int withcase = 0;
     for (;;) {
   while (*a != '\0' && ! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short int) _ISalnum) && *a != ' ') a++;
   while (*b != '\0' && ! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short int) _ISalnum) && *b != ' ') b++;
   if (! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short int) _ISalnum) && ! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short int) _ISalnum))
        break;
   else if (! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short int) _ISalnum))
        return -1;
   else if (! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short int) _ISalnum))
        return 1;
   else if (tolower(*a) != tolower(*b))
        return tolower(*a) - tolower(*b);
   else if (withcase == 0)
        withcase = *a - *b;
   a++; b++;
     }
     if (withcase != 0) return withcase;
     return strcmp(s, t);
}

canonical_original_code:
int lexico(char *s, char *t)
{
     char *a = s; char *b = t;
     int withcase = 0;
     for (;;) {
   while (*a != '\0' && ! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short) _ISalnum) && *a != ' ') a++;
   while (*b != '\0' && ! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short) _ISalnum) && *b != ' ') b++;
   if (! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short) _ISalnum) && ! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short) _ISalnum))
        break;
   else if (! ((*__ctype_b_loc ())[(int) ((*a))] & (unsigned short) _ISalnum))
        return -1;
   else if (! ((*__ctype_b_loc ())[(int) ((*b))] & (unsigned short) _ISalnum))
        return 1;
   else if (tolower(*a) != tolower(*b))
        return tolower(*a) - tolower(*b);
   else if (withcase == 0)
        withcase = *a - *b;
   a++; b++;
     }
     if (withcase != 0) return withcase;
     return strcmp(s, t);
}

variable_types:
s
char *

variable_types:
t
char *

variable_types:
a
char *

variable_types:
b
char *

variable_types: withcase int