decompiled_code:
__int64 __fastcall aio_buffer_fill(__int64 a1, size_t a2, _QWORD *a3)
{
size_t v3; // r12
_BYTE *v5; // rdi
char *v7; // r13
_BYTE *v8; // rsi
ssize_t v9; // rax
ssize_t v10; // rbp
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 32) - a2;
if ( !v3 )
_assert_fail("readsize > 0", "input.c", 0x50u, "aio_buffer_fill");
v5 = *(_BYTE **)(a1 + 16);
v7 = &v5[a2];
v8 = (_BYTE *)(*(_QWORD *)(a1 + 24) - a2);
*a3 = v5 - v8;
if ( a2 )
memmove(v5, v8, a2);
while ( 1 )
{
v9 = read(*(_DWORD *)(a1 + 40), v7, v3);
v10 = v9;
if ( v9 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_6;
}
if ( v9 )
{
LABEL_6:
result = 0LL;
*(_QWORD *)(a1 + 24) = &v7[v10];
return result;
}
return 4294959996LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, size_t a2, _QWORD *a3)
{
size_t v3; // r12
_BYTE *v5; // rdi
char *v7; // r13
_BYTE *v8; // rsi
ssize_t v9; // rax
ssize_t v10; // rbp
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 32) - a2;
if ( !v3 )
_assert_fail("readsize > 0", "input.c", 0x50u, "aio_buffer_fill");
v5 = *(_BYTE **)(a1 + 16);
v7 = &v5[a2];
v8 = (_BYTE *)(*(_QWORD *)(a1 + 24) - a2);
*a3 = v5 - v8;
if ( a2 )
memmove(v5, v8, a2);
while ( 1 )
{
v9 = read(*(_DWORD *)(a1 + 40), v7, v3);
v10 = v9;
if ( v9 >= 0 )
break;
if ( *_errno_location() != 4 )
goto LABEL_6;
}
if ( v9 )
{
LABEL_6:
result = 0LL;
*(_QWORD *)(a1 + 24) = &v7[v10];
return result;
}
return 4294959996LL;
}

original_code:
int aio_buffer_fill(aio_buffer *buffer, size_t keep, off_t *adjust) {
  size_t readsize = buffer->limit - keep;
  ((void) sizeof ((readsize > 0) ? 1 : 0), __extension__ ({ if (readsize > 0) ; else __assert_fail ("readsize > 0", "input.c", 80, __extension__ __PRETTY_FUNCTION__); }));
  char *readstart = buffer->start + keep;
  char *keepstart = buffer->end - keep;
  *adjust = buffer->start - keepstart;
  if(keep) {
    memmove(buffer->start, keepstart, keep);
  }
  ssize_t bytesread;
  while((bytesread = read(buffer->fd, readstart, readsize)) < 0 && (*__errno_location ()) == 4)
    continue;
  if(bytesread == 0)
    return (-7300);
  buffer->end = readstart + bytesread;
  return 0;
}

canonical_original_code:
int aio_buffer_fill(struct aio_buffer *buffer, unsigned long keep, long *adjust) {
  unsigned long readsize = buffer->limit - keep;
  ((void) sizeof ((readsize > 0) ? 1 : 0), __extension__ ({ if (readsize > 0) ; else __assert_fail ("readsize > 0", "input.c", 80, __extension__ __PRETTY_FUNCTION__); }));
  char *readstart = buffer->start + keep;
  char *keepstart = buffer->end - keep;
  *adjust = buffer->start - keepstart;
  if(keep) {
    memmove(buffer->start, keepstart, keep);
  }
  long bytesread;
  while((bytesread = read(buffer->fd, readstart, readsize)) < 0 && (*__errno_location ()) == 4)
    continue;
  if(bytesread == 0)
    return (-7300);
  buffer->end = readstart + bytesread;
  return 0;
}

variable_types:
keep
unsigned long

variable_types:
adjust
long *

variable_types:
readsize
unsigned long

variable_types:
readstart
char *

variable_types:
keepstart
char *

variable_types: bytesread long


decompiled_code:
__int64 __fastcall aio_buffer_open(__int64 a1, const char *a2)
{
int v2; // eax
__int64 result; // rax

v2 = open(a2, 0);
if ( v2 == -1 )
result = 4294960195LL;
else
result = aio_buffer_init(a1, v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const char *a2)
{
int v2; // eax
__int64 result; // rax

v2 = open(a2, 0);
if ( v2 == -1 )
result = 4294960195LL;
else
result = func5(a1, v2);
return result;
}

original_code:
int aio_buffer_open(aio_buffer *buffer, const char *path) {
  int fd = open(path, 00);
  if(fd == -1)
    return (-7101);
  return aio_buffer_init(buffer, fd);
}

canonical_original_code:
int aio_buffer_open(struct aio_buffer *buffer, char *path) {
  int fd = open(path, 00);
  if(fd == -1)
    return (-7101);
  return aio_buffer_init(buffer, fd);
}

variable_types: struct aio_buffer { char *data; unsigned long size; char *start; char *end; unsigned long limit; int fd; char *linestart; char *linelimit; } *

variable_types:
path
char *

variable_types: fd int

return_type: int


decompiled_code:
__int64 __fastcall aio_buffer_loadline(_QWORD *a1)
{
__int64 v1; // rax
int v2; // ecx
__int64 v3; // r12
bool v4; // zf
unsigned __int64 v5; // rax
int v6; // edx
unsigned __int64 v7; // rbx
__int64 result; // rax
unsigned __int64 v9; // rbx
__int64 v10[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = a1[7];
v2 = (unsigned __int8)aio_eol;
v10[0] = 0LL;
v3 = v1 + 1;
v4 = *(char *)(v1 + 1) == (unsigned __int8)aio_eol;
v5 = v1 + 1;
if ( v4 )
{
LABEL_7:
a1[6] = v3;
a1[7] = v5;
result = 0LL;
}
else
{
while ( 1 )
{
while ( 1 )
{
v7 = v5;
if ( a1[3] <= v5 )
break;
v6 = *(char *)++v5;
if ( v6 == v2 )
goto LABEL_7;
}
result = aio_buffer_fill((__int64)a1, v5 - v3, v10);
v3 += v10[0];
v9 = v10[0] + v7;
if ( (_DWORD)result )
break;
v2 = (unsigned __int8)aio_eol;
v5 = v9 + 1;
if ( *(char *)(v9 + 1) == (unsigned __int8)aio_eol )
goto LABEL_7;
}
a1[6] = v3;
a1[7] = v9;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
__int64 v1; // rax
int v2; // ecx
__int64 v3; // r12
bool v4; // zf
unsigned __int64 v5; // rax
int v6; // edx
unsigned __int64 v7; // rbx
__int64 result; // rax
unsigned __int64 v9; // rbx
__int64 v10[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = a1[7];
v2 = (unsigned __int8)aio_eol;
v10[0] = 0LL;
v3 = v1 + 1;
v4 = *(char *)(v1 + 1) == (unsigned __int8)aio_eol;
v5 = v1 + 1;
if ( v4 )
{
LABEL_7:
a1[6] = v3;
a1[7] = v5;
result = 0LL;
}
else
{
while ( 1 )
{
while ( 1 )
{
v7 = v5;
if ( a1[3] <= v5 )
break;
v6 = *(char *)++v5;
if ( v6 == v2 )
goto LABEL_7;
}
result = func4((__int64)a1, v5 - v3, v10);
v3 += v10[0];
v9 = v10[0] + v7;
if ( (_DWORD)result )
break;
v2 = (unsigned __int8)aio_eol;
v5 = v9 + 1;
if ( *(char *)(v9 + 1) == (unsigned __int8)aio_eol )
goto LABEL_7;
}
a1[6] = v3;
a1[7] = v9;
}
return result;
}

original_code:
int aio_buffer_loadline(aio_buffer *buffer) {
  char *linestart = buffer->linestart;
  char *linelimit = buffer->linelimit;
  linestart = linelimit + 1;
  linelimit = linestart;
  off_t adjust = 0;
  int res = 0;
  for(; *linelimit != aio_eol; ++linelimit) {
    if(linelimit >= buffer->end) {
      res = aio_buffer_fill(buffer, linelimit - linestart, &adjust);
      linestart += adjust;
      linelimit += adjust;
      if(res != 0)
        break;
    }
  }
  buffer->linestart = linestart;
  buffer->linelimit = linelimit;
  if(res != 0)
    return res;
  return 0;
}

canonical_original_code:
int aio_buffer_loadline(struct aio_buffer *buffer) {
  char *linestart = buffer->linestart;
  char *linelimit = buffer->linelimit;
  linestart = linelimit + 1;
  linelimit = linestart;
  long adjust = 0;
  int res = 0;
  for(; *linelimit != aio_eol; ++linelimit) {
    if(linelimit >= buffer->end) {
      res = aio_buffer_fill(buffer, linelimit - linestart, &adjust);
      linestart += adjust;
      linelimit += adjust;
      if(res != 0)
        break;
    }
  }
  buffer->linestart = linestart;
  buffer->linelimit = linelimit;
  if(res != 0)
    return res;
  return 0;
}

variable_types:
linestart
char *

variable_types:
linelimit
char *

variable_types: adjust long

variable_types: res int

user_defined_types: struct aio_buffer { char *data; unsigned long size; char *start; char *end; unsigned long limit; int fd; char *linestart; char *linelimit; }


decompiled_code:
ssize_t __fastcall aio_buffer_writeline(__int64 a1, int a2)
{
write(a2, *(const void **)(a1 + 48), *(_QWORD *)(a1 + 56) - *(_QWORD *)(a1 + 48));
return write(a2, "\n", 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func8(__int64 a1, int a2)
{
write(a2, *(const void **)(a1 + 48), *(_QWORD *)(a1 + 56) - *(_QWORD *)(a1 + 48));
return write(a2, "\n", 1uLL);
}

original_code:
void aio_buffer_writeline(aio_buffer *buffer, int fdout) {
  write(fdout, buffer->linestart, (size_t) (buffer->linelimit - buffer->linestart));
  write(fdout, "\n", 1);
}

canonical_original_code:
void aio_buffer_writeline(struct aio_buffer *buffer, int fdout) {
  write(fdout, buffer->linestart, (unsigned long) (buffer->linelimit - buffer->linestart));
  write(fdout, "\n", 1);
}

variable_types: fdout int

return_type: void


decompiled_code:
__int64 __fastcall aio_buffer_setlinelimit(_QWORD *a1)
{
__int64 v1; // rax
int v2; // edx
__int64 v3; // rbp
int v4; // eax
__int64 v5; // rax
__int64 v6; // rbx
__int64 v8[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = a1[6];
v2 = (unsigned __int8)aio_eol;
v8[0] = 0LL;
v3 = v1 + 1;
if ( *(char *)(v1 + 1) != (unsigned __int8)aio_eol )
{
do
{
while ( a1[3] != v3 )
{
v4 = *(char *)++v3;
if ( v4 == v2 )
goto LABEL_7;
}
if ( (unsigned int)aio_buffer_fill((__int64)a1, v3 - a1[6], v8) )
break;
v5 = v8[0];
v2 = (unsigned __int8)aio_eol;
a1[6] += v8[0];
v6 = v5 + v3;
LODWORD(v5) = *(char *)(v5 + v3 + 1);
v3 = v6 + 1;
}
while ( (_DWORD)v5 != v2 );
}
LABEL_7:
a1[7] = v3;
return 0LL;
}

original_code:
int aio_buffer_setlinelimit(aio_buffer *buffer) {
  char *limit;
  off_t adj = 0;
  for(limit = buffer->linestart + 1; *limit != aio_eol; ++limit) {
    if(limit == buffer->end) {
      if(aio_buffer_fill(buffer, limit - buffer->linestart, &adj) != 0)
        break;
      buffer->linestart += adj;
      limit += adj;
    }
  }
  buffer->linelimit = limit;
  return 0;
}

canonical_original_code:
int aio_buffer_setlinelimit(struct aio_buffer *buffer) {
  char *limit;
  long adj = 0;
  for(limit = buffer->linestart + 1; *limit != aio_eol; ++limit) {
    if(limit == buffer->end) {
      if(aio_buffer_fill(buffer, limit - buffer->linestart, &adj) != 0)
        break;
      buffer->linestart += adj;
      limit += adj;
    }
  }
  buffer->linelimit = limit;
  return 0;
}

variable_types:
limit
char *

variable_types: adj long


loadip

decompiled_code:
int __fastcall loadip(const char *a1)
{
int v1; // eax
int result; // eax

v1 = strlen(a1);
result = addip_str(iptree, a1, &a1[v1]);
if ( verbose )
{
switch ( result )
{
case -1002:
result = fprintf(stderr, "Warning: The IP %s is invalid.\n", a1);
break;
case -1001:
result = fprintf(stderr, "Warning: The CIDR block %s is invalid.\n", a1);
break;
case -1100:
result = fprintf(stderr, "Warning: No IP found in \"%s\".\n", a1);
break;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
int v1; // eax
int result; // eax

v1 = strlen(a1);
result = addip_str(iptree, a1, &a1[v1]);
if ( verbose )
{
switch ( result )
{
case -1002:
result = fprintf(stderr, "Warning: The IP %s is invalid.\n", a1);
break;
case -1001:
result = fprintf(stderr, "Warning: The CIDR block %s is invalid.\n", a1);
break;
case -1100:
result = fprintf(stderr, "Warning: No IP found in \"%s\".\n", a1);
break;
}
}
return result;
}

original_code:
static void loadip(void *arg) {
  char *ip = (char *)arg;
  unsigned int len = strlen(ip);
  int res = addip_str(iptree, ip, (ip + len));
  if(verbose) {
    switch(res) {
      case -1002:
      fprintf(stderr, "Warning: The IP %s is invalid.\n", ip);
      break;
      case -1001:
      fprintf(stderr, "Warning: The CIDR block %s is invalid.\n", ip);
      break;
      case -1100:
      fprintf(stderr, "Warning: No IP found in \"%s\".\n", ip);
      break;
    }
  }
}

variable_types:
arg
void *

variable_types:
ip
char *

variable_types:
len
unsigned int


loadlist

decompiled_code:
unsigned int __fastcall loadlist(const char *a1)
{
unsigned int result; // eax
int v2; // ebp
int v3; // eax

result = aio_buffer_open(buffer, a1);
if ( result )
{
if ( verbose )
result = fprintf(stderr, "Warning: could not open file %s, error code: %d.\n", a1, result);
}
else
{
while ( 1 )
{
result = aio_buffer_loadline(buffer);
v2 = result;
if ( result )
break;
v3 = addip_str(iptree, *(_QWORD *)(buffer + 48), *(_QWORD *)(buffer + 56));
if ( verbose )
{
switch ( v3 )
{
case -1002:
fprintf(
stderr,
"Warning: The IP address on the line below is invalid.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
case -1001:
fprintf(
stderr,
"Warning: The CIDR block on the line below is invalid.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
case -1100:
fprintf(
stderr,
"Warning: The line below does not contain an IP address.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
}
}
}
if ( result != -7300 )
{
fprintf(stderr, "IO Error code %d.\n", result);
exit(v2);
}
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func2(const char *a1)
{
unsigned int result; // eax
int v2; // ebp
int v3; // eax

result = aio_buffer_open(buffer, a1);
if ( result )
{
if ( verbose )
result = fprintf(stderr, "Warning: could not open file %s, error code: %d.\n", a1, result);
}
else
{
while ( 1 )
{
result = aio_buffer_loadline(buffer);
v2 = result;
if ( result )
break;
v3 = addip_str(iptree, *(_QWORD *)(buffer + 48), *(_QWORD *)(buffer + 56));
if ( verbose )
{
switch ( v3 )
{
case -1002:
fprintf(
stderr,
"Warning: The IP address on the line below is invalid.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
case -1001:
fprintf(
stderr,
"Warning: The CIDR block on the line below is invalid.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
case -1100:
fprintf(
stderr,
"Warning: The line below does not contain an IP address.\n%.*s\n",
*(_QWORD *)(buffer + 56) - *(_QWORD *)(buffer + 48),
*(const char **)(buffer + 48));
break;
}
}
}
if ( result != -7300 )
{
fprintf(stderr, "IO Error code %d.\n", result);
exit(v2);
}
}
return result;
}

original_code:
static void loadlist(void *arg) {
  char *path = (char *)arg;
  int res = 0;
  if((res = aio_buffer_open(buffer, path)) != 0) {
    if(verbose)
      fprintf(stderr, "Warning: could not open file %s, error code: %d.\n", path, res);
    return;
  }
  while((res = aio_buffer_loadline(buffer)) == 0) {
    res = addip_str(iptree, buffer->linestart, buffer->linelimit);
    if(verbose) {
      switch(res) {
        case -1002:
        fprintf(stderr, "Warning: The IP address on the line below is invalid.\n%.*s\n", (int) (buffer->linelimit - buffer->linestart), buffer->linestart);
        break;
        case -1001:
        fprintf(stderr, "Warning: The CIDR block on the line below is invalid.\n%.*s\n", (int) (buffer->linelimit - buffer->linestart), buffer->linestart);
        break;
        case -1100:
        fprintf(stderr, "Warning: The line below does not contain an IP address.\n%.*s\n", (int) (buffer->linelimit - buffer->linestart), buffer->linestart);
        break;
      }
    }
  }
  if(res != (-7300)) {
    fprintf(stderr, "IO Error code %d.\n", res);
    exit(res);
  } else {
    return;
  }
}


decompiled_code:
void __noreturn print_usage()
{
puts(
"Usage: ipscan [OPTION]...\n"
"Search for IP addresses or CIDR blocks in STDIN and print out matched lines.\n"
"\n"
"Loading IP lists:\n"
" -i, --ip-list FILE\t\tload newline-separated list of IP addresses (CIDR notation supported)\n"
" -I, --ip-search IP\t\tadd the IP to the list of IP addresses searched for (CIDR notation is supported)\n"
"\n"
"Search options:\n"
" -v, --invert-match\t\tinstead of printing lines that match the IP list, print ones that don't\n"
" -p, --match-position IDX\tinstead of checking against the first IP on the line, check against the IDXth\n"
"\t\t\t\tSupports negative IDX, counting from right instead from left.\n"
"\t\t\t\t(-1 = last IP, 1 = first IP, 0 = any position; default: 0)\n"
"\n"
"Output control:\n"
" --dump-ips\t\t\tinstead of running the search dump the computed CIDR blocks to STDOUT\n"
" --verbose\t\t\tprint additional messages to STDERR (default)\n"
" --quiet\t\t\tdon't print messages to STDERR\n"
"\n"
"Miscellaneous:\n"
" -V, --version\t\t\tprint version information and exit\n"
" -h, --help\t\t\tprint this message and exit\n"
"\n"
"Examples:\n"
"# Find all communication where neither source nor destination are in a private range:\n"
"> cat /var/syslog/* | ipscan -v -I 10.0.0.0/8 -I 192.168.0.0/16 -I 172.16.0.0/12 -p 0\n"
"# Find all communication originating from China:\n"
"> cat /var/syslog/* | ipscan -i chinese_ranges.txt -p 0\n"
"\n"
"# Simplify a list of IP ranges:\n"
"> ipscan -I 10.0.0.0/24 -I 10.0.1.0/24 --dump-ips\n"
"\t# outputs: 10.0.0.0/23");
exit(0);
}

canonical_decompiled_code:
void __noreturn func3()
{
puts(
"Usage: ipscan [OPTION]...\n"
"Search for IP addresses or CIDR blocks in STDIN and print out matched lines.\n"
"\n"
"Loading IP lists:\n"
" -i, --ip-list FILE\t\tload newline-separated list of IP addresses (CIDR notation supported)\n"
" -I, --ip-search IP\t\tadd the IP to the list of IP addresses searched for (CIDR notation is supported)\n"
"\n"
"Search options:\n"
" -v, --invert-match\t\tinstead of printing lines that match the IP list, print ones that don't\n"
" -p, --match-position IDX\tinstead of checking against the first IP on the line, check against the IDXth\n"
"\t\t\t\tSupports negative IDX, counting from right instead from left.\n"
"\t\t\t\t(-1 = last IP, 1 = first IP, 0 = any position; default: 0)\n"
"\n"
"Output control:\n"
" --dump-ips\t\t\tinstead of running the search dump the computed CIDR blocks to STDOUT\n"
" --verbose\t\t\tprint additional messages to STDERR (default)\n"
" --quiet\t\t\tdon't print messages to STDERR\n"
"\n"
"Miscellaneous:\n"
" -V, --version\t\t\tprint version information and exit\n"
" -h, --help\t\t\tprint this message and exit\n"
"\n"
"Examples:\n"
"# Find all communication where neither source nor destination are in a private range:\n"
"> cat /var/syslog/* | ipscan -v -I 10.0.0.0/8 -I 192.168.0.0/16 -I 172.16.0.0/12 -p 0\n"
"# Find all communication originating from China:\n"
"> cat /var/syslog/* | ipscan -i chinese_ranges.txt -p 0\n"
"\n"
"# Simplify a list of IP ranges:\n"
"> ipscan -I 10.0.0.0/24 -I 10.0.1.0/24 --dump-ips\n"
"\t# outputs: 10.0.0.0/23");
exit(0);
}

original_code:
static void print_usage() {
  printf(
    "Usage: ipscan [OPTION]...\n"
    "Search for IP addresses or CIDR blocks in STDIN and print out matched lines.\n"
    "\nLoading IP lists:\n"
    "  -i, --ip-list FILE\t\tload newline-separated list of IP addresses (CIDR notation supported)\n"
    "  -I, --ip-search IP\t\tadd the IP to the list of IP addresses searched for (CIDR notation is supported)\n"
    "\nSearch options:\n"
    "  -v, --invert-match\t\tinstead of printing lines that match the IP list, print ones that don't\n"
    "  -p, --match-position IDX\tinstead of checking against the first IP on the line, check against the IDXth\n"
    "\t\t\t\tSupports negative IDX, counting from right instead from left.\n"
    "\t\t\t\t(-1 = last IP, 1 = first IP, 0 = any position; default: 0)\n"
    "\nOutput control:\n"
    "  --dump-ips\t\t\tinstead of running the search dump the computed CIDR blocks to STDOUT\n"
    "  --verbose\t\t\tprint additional messages to STDERR (default)\n"
    "  --quiet\t\t\tdon't print messages to STDERR\n"
    "\nMiscellaneous:\n"
    "  -V, --version\t\t\tprint version information and exit\n"
    "  -h, --help\t\t\tprint this message and exit\n"
    "\nExamples:\n"
    "# Find all communication where neither source nor destination are in a private range:\n"
    "> cat /var/syslog/* | ipscan -v -I 10.0.0.0/8 -I 192.168.0.0/16 -I 172.16.0.0/12 -p 0\n"
    "# Find all communication originating from China:\n"
    "> cat /var/syslog/* | ipscan -i chinese_ranges.txt -p 0\n\n"
    "# Simplify a list of IP ranges:\n"
    "> ipscan -I 10.0.0.0/24 -I 10.0.1.0/24 --dump-ips\n"
    "\t# outputs: 10.0.0.0/23\n"
    );
  exit(0);
}


decompiled_code:
void __noreturn print_version()
{
printf("ipscan %d.%d.%d\n\n", 0LL, 9LL, 0LL);
exit(0);
}

canonical_decompiled_code:
void __noreturn func4()
{
printf("ipscan %d.%d.%d\n\n", 0LL, 9LL, 0LL);
exit(0);
}

original_code:
static void print_version() {
  printf(
    "ipscan %d.%d.%d\n\n",
    0, 9, 0
  );
  exit(0);
}


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 func5(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 *


getopts

decompiled_code:
int __fastcall getopts(int argc, char **argv)
{
int result; // eax
_QWORD *v3; // rax
_QWORD *v4; // rax
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 )
return result;
if ( result == 105 )
{
if ( files )
v4 = list_append_cpy((__int64)files, optarg);
else
v4 = list_make_cpy(optarg);
files = v4;
}
else if ( result > 105 )
{
if ( result != 112 )
{
if ( result == 118 )
{
search_invertmatch = 1;
LABEL_11:
print_version();
}
LABEL_15:
print_usage();
}
search_ippos = atoi(optarg);
}
else if ( result == 73 )
{
if ( ips )
v3 = list_append_cpy((__int64)ips, optarg);
else
v3 = list_make_cpy(optarg);
ips = v3;
}
else
{
if ( result == 86 )
goto LABEL_11;
if ( result )
goto LABEL_15;
}
}
}

canonical_decompiled_code:
int __fastcall func6(int argc, char **argv)
{
int result; // eax
_QWORD *v3; // rax
_QWORD *v4; // rax
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 )
return result;
if ( result == 105 )
{
if ( files )
v4 = list_append_cpy((__int64)files, optarg);
else
v4 = list_make_cpy(optarg);
files = v4;
}
else if ( result > 105 )
{
if ( result != 112 )
{
if ( result == 118 )
{
search_invertmatch = 1;
LABEL_11:
func4();
}
LABEL_15:
func3();
}
search_ippos = atoi(optarg);
}
else if ( result == 73 )
{
if ( ips )
v3 = list_append_cpy((__int64)ips, optarg);
else
v3 = list_make_cpy(optarg);
ips = v3;
}
else
{
if ( result == 86 )
goto LABEL_11;
if ( result )
goto LABEL_15;
}
}
}

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; // rbp
void **v2; // r12
void *v3; // rdi

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);
v3 = v1;
v1 = v2;
free(v3);
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; // rbp
void **v2; // r12
void *v3; // rdi

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);
v3 = v1;
v1 = v2;
free(v3);
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; } *


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


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

canonical_decompiled_code:
void *__fastcall func1(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;
}


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 func2(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 != &full && ptr != &zero )
{
freenode(*(void **)ptr);
freenode(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
if ( ptr != &full && ptr != &zero )
{
func4(*(void **)ptr);
func4(*((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, __int64 a2, int a3, unsigned int a4)
{
void **v4; // rbp
void **v7; // rax

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

canonical_decompiled_code:
void __fastcall func5(void **a1, __int64 a2, int a3, unsigned int a4)
{
void **v4; // rbp
void **v7; // rax

v4 = (void **)*a1;
if ( *a1 != &full )
{
if ( a3 <= (int)a4 )
{
if ( v4 != (void **)&zero )
func4(*a1);
goto LABEL_9;
}
if ( v4 == (void **)&zero )
{
v7 = (void **)func1(0x10uLL);
a2 = (unsigned int)a2;
*v7 = &zero;
v4 = v7;
v7[1] = &zero;
*a1 = v7;
}
func5(&v4[((unsigned int)a2 >> a3) & 1], a2, (unsigned int)(a3 - 1), a4);
if ( *v4 == &full && v4[1] == &full )
{
free(v4);
LABEL_9:
*a1 = &full;
return;
}
}
}

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 func6(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, unsigned int a2, unsigned int a3)
{
_QWORD *v5; // rbx
char v6; // r13
int result; // eax

if ( a1 != (_QWORD *)&zero )
{
v5 = a1;
if ( a1 == (_QWORD *)&full )
{
LABEL_5:
result = dumpip(a2, 31 - a3);
}
else
{
while ( 1 )
{
v6 = a3--;
dumpnode(*v5, a2, a3);
v5 = (_QWORD *)v5[1];
result = 1 << v6;
a2 |= 1 << v6;
if ( v5 == (_QWORD *)&zero )
break;
if ( v5 == (_QWORD *)&full )
goto LABEL_5;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(_QWORD *a1, unsigned int a2, unsigned int a3)
{
_QWORD *v5; // rbx
char v6; // r13
int result; // eax

if ( a1 != (_QWORD *)&zero )
{
v5 = a1;
if ( a1 == (_QWORD *)&full )
{
LABEL_5:
result = func6(a2, 31 - a3);
}
else
{
while ( 1 )
{
v6 = a3--;
func7(*v5, a2, a3);
v5 = (_QWORD *)v5[1];
result = 1 << v6;
a2 |= 1 << v6;
if ( v5 == (_QWORD *)&zero )
break;
if ( v5 == (_QWORD *)&full )
goto LABEL_5;
}
}
}
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 = func1(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(__int64 a1, int a2, int a3)
{
unsigned int v5; // er8
void **v6; // r9
unsigned int v7; // er12

v7 = validateip(a2, a3);
if ( !v7 )
node_insert(v6, v5, 31, 31 - a3);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2, int a3)
{
unsigned int v5; // er8
void **v6; // r9
unsigned int v7; // er12

v7 = func0(a2, a3);
if ( !v7 )
func5(v6, v5, 31, 31 - a3);
return v7;
}

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(__int64 a1, unsigned __int8 *a2, unsigned __int64 a3)
{
__int64 result; // rax
int *v4; // [rsp+0h] [rbp-18h] BYREF
int *v5; // [rsp+8h] [rbp-10h] BYREF

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

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

if ( (unsigned int)func3(a2, a3, &v4, &v5) )
result = func10(a1, *v4, *v5);
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, unsigned int a2)
{
return node_search_constprop_0(*a1, a2);
}

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

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; // r8
__int64 result; // rax
__int64 v8; // r8
unsigned int *v9; // r9
int v10; // edx
unsigned int *v11; // [rsp+0h] [rbp-28h] BYREF
__int64 v12[4]; // [rsp+8h] [rbp-20h] BYREF

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

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

v5 = func3(a2, a3, &v11, v12);
if ( !v5 )
return 4294966196LL;
if ( a4 )
{
v10 = v5 - a4;
if ( a4 > 0 )
v10 = a4 - 1;
if ( v10 >= v5 )
result = 4294966195LL;
else
result = func12(a1, v11[v10]);
}
else if ( v5 <= 0 )
{
result = 0LL;
}
else
{
v6 = v11;
do
{
result = func12(a1, *v6);
if ( (_DWORD)result )
break;
v6 = (unsigned int *)(v8 + 4);
}
while ( v6 != v9 );
}
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, 0, 0x1Fu);
}

canonical_decompiled_code:
int __fastcall func14(_QWORD **a1)
{
return func7(*a1, 0, 0x1Fu);
}

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__m128i v3; // xmm0
__m128i v4; // xmm1
__m128i v5; // xmm2
int v6; // eax
int v7; // ebx
int v9; // er12
__int64 v10; // rdi
char v11; // al
__int64 v12; // rsi
__m128i v13; // [rsp+0h] [rbp-78h] BYREF
__m128i v14; // [rsp+10h] [rbp-68h] BYREF
__m128i v15; // [rsp+20h] [rbp-58h] BYREF
__int64 v16; // [rsp+30h] [rbp-48h]
speed_t v17; // [rsp+38h] [rbp-40h]
int v18; // [rsp+44h] [rbp-34h] BYREF
char v19; // [rsp+49h] [rbp-2Fh] BYREF
char v20; // [rsp+4Ah] [rbp-2Eh]
char v21; // [rsp+4Bh] [rbp-2Dh]
char *s; // [rsp+50h] [rbp-28h] BYREF
void *ptr[4]; // [rsp+58h] [rbp-20h] BYREF

v18 = 0;
t_getstate(&original);
t_initstate(&v13, &original);
v3 = _mm_loadu_si128(&v13);
v4 = _mm_loadu_si128(&v14);
v5 = _mm_loadu_si128(&v15);
*(_QWORD *)&n_state.c_cc[31] = v16;
*(__m128i *)&n_state.c_iflag = v3;
*(__m128i *)&n_state.c_line = v4;
*(__m128i *)&n_state.c_cc[15] = v5;
n_state.c_ospeed = v17;
t_setstate(&n_state);
v6 = read_file(&s, &v18);
v7 = v18;
if ( v18 )
goto LABEL_2;
v9 = v6;
ptr[0] = malloc(v6);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
t_read(&v19, 7LL);
if ( v19 == 27 )
break;
if ( v19 == 127 )
{
if ( !v20 && !v21 && v7 > 0 )
{
v10 = (unsigned int)v7--;
v9 = remove_char(v10, &s, ptr, (unsigned int)v9, &v18);
}
goto LABEL_10;
}
if ( v19 )
{
v11 = v20;
goto LABEL_14;
}
LABEL_10:
if ( v18 )
goto LABEL_2;
}
v11 = v20;
if ( v20 != 106 )
break;
if ( v21 )
goto LABEL_10;
v7 -= v7 > 0;
if ( v18 )
goto LABEL_2;
}
if ( v20 != 107 )
break;
if ( v21 )
goto LABEL_10;
v7 += v7 < (int)strlen(s);
if ( v18 )
goto LABEL_2;
}
if ( v20 == 100 )
{
if ( !v21 )
fputs(s, stdout);
goto LABEL_10;
}
if ( v20 == 102 )
break;
LABEL_14:
if ( v11 || v21 )
goto LABEL_10;
v12 = (unsigned int)v7++;
v9 = add_char((unsigned int)v19, v12, &s, ptr, (unsigned int)v9, &v18);
if ( v18 )
goto LABEL_2;
}
if ( v21 )
goto LABEL_10;
if ( v9 > 0 && !v18 )
{
free(s);
free(ptr[0]);
}
LABEL_2:
t_setstate(&original);
fflush(stdout);
return v18;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__m128i v3; // xmm0
__m128i v4; // xmm1
__m128i v5; // xmm2
int v6; // eax
int v7; // ebx
int v9; // er12
__int64 v10; // rdi
char v11; // al
__int64 v12; // rsi
__m128i v13; // [rsp+0h] [rbp-78h] BYREF
__m128i v14; // [rsp+10h] [rbp-68h] BYREF
__m128i v15; // [rsp+20h] [rbp-58h] BYREF
__int64 v16; // [rsp+30h] [rbp-48h]
speed_t v17; // [rsp+38h] [rbp-40h]
int v18; // [rsp+44h] [rbp-34h] BYREF
char v19; // [rsp+49h] [rbp-2Fh] BYREF
char v20; // [rsp+4Ah] [rbp-2Eh]
char v21; // [rsp+4Bh] [rbp-2Dh]
char *s; // [rsp+50h] [rbp-28h] BYREF
void *ptr[4]; // [rsp+58h] [rbp-20h] BYREF

v18 = 0;
func10(&original);
t_initstate(&v13, &original);
v3 = _mm_loadu_si128(&v13);
v4 = _mm_loadu_si128(&v14);
v5 = _mm_loadu_si128(&v15);
*(_QWORD *)&n_state.c_cc[31] = v16;
*(__m128i *)&n_state.c_iflag = v3;
*(__m128i *)&n_state.c_line = v4;
*(__m128i *)&n_state.c_cc[15] = v5;
n_state.c_ospeed = v17;
func11(&n_state);
v6 = func7(&s, &v18);
v7 = v18;
if ( v18 )
goto LABEL_2;
v9 = v6;
ptr[0] = malloc(v6);
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
func15(&v19, 7LL);
if ( v19 == 27 )
break;
if ( v19 == 127 )
{
if ( !v20 && !v21 && v7 > 0 )
{
v10 = (unsigned int)v7--;
v9 = func9(v10, &s, ptr, (unsigned int)v9, &v18);
}
goto LABEL_10;
}
if ( v19 )
{
v11 = v20;
goto LABEL_14;
}
LABEL_10:
if ( v18 )
goto LABEL_2;
}
v11 = v20;
if ( v20 != 106 )
break;
if ( v21 )
goto LABEL_10;
v7 -= v7 > 0;
if ( v18 )
goto LABEL_2;
}
if ( v20 != 107 )
break;
if ( v21 )
goto LABEL_10;
v7 += v7 < (int)strlen(s);
if ( v18 )
goto LABEL_2;
}
if ( v20 == 100 )
{
if ( !v21 )
fputs(s, stdout);
goto LABEL_10;
}
if ( v20 == 102 )
break;
LABEL_14:
if ( v11 || v21 )
goto LABEL_10;
v12 = (unsigned int)v7++;
v9 = func8((unsigned int)v19, v12, &s, ptr, (unsigned int)v9, &v18);
if ( v18 )
goto LABEL_2;
}
if ( v21 )
goto LABEL_10;
if ( v9 > 0 && !v18 )
{
free(s);
free(ptr[0]);
}
LABEL_2:
func11(&original);
fflush(stdout);
return v18;
}

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 read_file(void **a1, _DWORD *a2)
{
__int64 v2; // rbx
void *v3; // rax
void *v5; // rax

v2 = 0LL;
*a1 = malloc(0LL);
while ( !ferror(stdin) && !feof(stdin) )
{
v3 = realloc(*a1, v2 + 4096);
*a1 = v3;
if ( !v3 )
goto LABEL_9;
v2 += fread(v3, 1uLL, 0x1000uLL, stdin);
}
if ( v2 )
return (unsigned int)v2;
v5 = malloc(0x1000uLL);
*a1 = v5;
if ( v5 )
return (unsigned int)v2;
LABEL_9:
*a2 = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(void **a1, _DWORD *a2)
{
__int64 v2; // rbx
void *v3; // rax
void *v5; // rax

v2 = 0LL;
*a1 = malloc(0LL);
while ( !ferror(stdin) && !feof(stdin) )
{
v3 = realloc(*a1, v2 + 4096);
*a1 = v3;
if ( !v3 )
goto LABEL_9;
v2 += fread(v3, 1uLL, 0x1000uLL, stdin);
}
if ( v2 )
return (unsigned int)v2;
v5 = malloc(0x1000uLL);
*a1 = v5;
if ( v5 )
return (unsigned int)v2;
LABEL_9:
*a2 = 1;
return 0LL;
}

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, void **a4, signed int a5)
{
signed int v6; // er12
char *v8; // r15
char *v9; // rdi
const char *v10; // rbx
size_t v11; // rax
const char *v12; // r13
size_t v13; // rax
void *v15; // rax
char src[60]; // [rsp+Ch] [rbp-3Ch] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func8(char a1, int a2, const char **a3, void **a4, signed int a5)
{
signed int v6; // er12
char *v8; // r15
char *v9; // rdi
const char *v10; // rbx
size_t v11; // rax
const char *v12; // r13
size_t v13; // rax
void *v15; // rax
char src[60]; // [rsp+Ch] [rbp-3Ch] BYREF

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

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, void **a3, signed int a4, _DWORD *a5)
{
signed int v7; // er12
char *v9; // r14
char *v10; // rdi
size_t v11; // r15
const char *v12; // r14
size_t v13; // rax
const char *v14; // r13
size_t v15; // rax
void *v16; // rax

v7 = a4;
v9 = (char *)*a2;
if ( (int)(4 * strlen(*a2)) <= a4 )
{
v7 /= 2;
*a2 = (const char *)realloc(v9, v7);
v16 = realloc(*a3, v7);
*a3 = v16;
v9 = (char *)*a2;
v10 = (char *)v16;
if ( !*a2 )
goto LABEL_6;
}
else
{
v10 = (char *)*a3;
}
if ( !v10 )
{
LABEL_6:
*a5 = 0;
return (unsigned int)v7;
}
v11 = a1 - 1;
strcpy(v10, &v9[a1]);
strncat((char *)*a3, *a2, v11);
v12 = (const char *)*a3;
v13 = strlen((const char *)*a3);
strcpy((char *)*a2, &v12[v13 - v11]);
v14 = (const char *)*a3;
v15 = strlen(v14);
strncat((char *)*a2, v14, v15 - (a1 + 1));
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, const char **a2, void **a3, signed int a4, _DWORD *a5)
{
signed int v7; // er12
char *v9; // r14
char *v10; // rdi
size_t v11; // r15
const char *v12; // r14
size_t v13; // rax
const char *v14; // r13
size_t v15; // rax
void *v16; // rax

v7 = a4;
v9 = (char *)*a2;
if ( (int)(4 * strlen(*a2)) <= a4 )
{
v7 /= 2;
*a2 = (const char *)realloc(v9, v7);
v16 = realloc(*a3, v7);
*a3 = v16;
v9 = (char *)*a2;
v10 = (char *)v16;
if ( !*a2 )
goto LABEL_6;
}
else
{
v10 = (char *)*a3;
}
if ( !v10 )
{
LABEL_6:
*a5 = 0;
return (unsigned int)v7;
}
v11 = a1 - 1;
strcpy(v10, &v9[a1]);
strncat((char *)*a3, *a2, v11);
v12 = (const char *)*a3;
v13 = strlen((const char *)*a3);
strcpy((char *)*a2, &v12[v13 - v11]);
v14 = (const char *)*a3;
v15 = strlen(v14);
strncat((char *)*a2, v14, v15 - (a1 + 1));
return (unsigned int)v7;
}

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


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 *a1, size_t a2)
{
void *v2; // rax

v2 = memset(a1, 0, a2);
return read(0, v2, a2);
}

canonical_decompiled_code:
ssize_t __fastcall func15(void *a1, size_t a2)
{
void *v2; // rax

v2 = memset(a1, 0, a2);
return read(0, v2, a2);
}

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
unsigned int v1; // eax
int v2; // ebp
int v3; // eax
unsigned int v4; // eax
int v5; // ebp
int v6; // eax
unsigned int v7; // eax
int v8; // ebp
int v9; // eax
unsigned int v10; // eax
int v11; // ebp
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;
while ( (_BYTE)result != 10 )
{
if ( (unsigned __int8)(result - 48) <= 9u )
{
if ( (unsigned __int8)isFull() )
{
puts("Expression is too complex");
exit(1);
}
push((unsigned int)(v14[0] - 48));
LOBYTE(result) = v14[0];
}
switch ( (char)result )
{
case '*':
v11 = pop();
v12 = pop();
v13 = mul(v12, v11);
push(v13);
break;
case '+':
v8 = pop();
v9 = pop();
v10 = add(v9, v8);
push(v10);
break;
case '-':
v5 = pop();
v6 = pop();
v7 = sub(v6, v5);
push(v7);
break;
case '/':
v2 = pop();
v3 = pop();
v4 = divide(v3, v2);
push(v4);
break;
case '=':
v1 = pop();
printf("%d\n", v1);
break;
default:
break;
}
_isoc99_scanf("%c", v14);
LOBYTE(result) = v14[0];
}
}
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax
unsigned int v1; // eax
int v2; // ebp
int v3; // eax
unsigned int v4; // eax
int v5; // ebp
int v6; // eax
unsigned int v7; // eax
int v8; // ebp
int v9; // eax
unsigned int v10; // eax
int v11; // ebp
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;
while ( (_BYTE)result != 10 )
{
if ( (unsigned __int8)(result - 48) <= 9u )
{
if ( (unsigned __int8)isFull() )
{
puts("Expression is too complex");
exit(1);
}
push((unsigned int)(v14[0] - 48));
LOBYTE(result) = v14[0];
}
switch ( (char)result )
{
case '*':
v11 = pop();
v12 = pop();
v13 = func2(v12, v11);
push(v13);
break;
case '+':
v8 = pop();
v9 = pop();
v10 = func0(v9, v8);
push(v10);
break;
case '-':
v5 = pop();
v6 = pop();
v7 = func1(v6, v5);
push(v7);
break;
case '/':
v2 = pop();
v3 = pop();
v4 = func3(v3, v2);
push(v4);
break;
case '=':
v1 = pop();
printf("%d\n", v1);
break;
default:
break;
}
_isoc99_scanf("%c", v14);
LOBYTE(result) = v14[0];
}
}
}

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; // rbp
size_t v2; // rax
const char *v3; // rbp
size_t v4; // rax
int v5; // eax
__int64 v6; // rbx
size_t v7; // rax
int v8; // ebx
size_t v9; // rax
int v10; // eax
int v11; // eax
__int64 v12; // rbx
__int64 v13; // rax
int v14; // eax

v1 = (const char *)strins(a1, &LC2, 0LL);
v2 = strlen(v1);
v3 = (const char *)strins(v1, &LC3, v2);
v4 = strlen(v3);
v5 = (unsigned int)memchr(v3 + 1, 40, v4) - (_DWORD)v3;
if ( v5 >= 0 )
{
v6 = v5;
do
{
v3 = (const char *)strins(v3, &LC2, (unsigned int)v6);
v7 = strlen(&v3[v6 + 1]);
v8 = (unsigned int)memchr(&v3[v6], 41, v7) - (_DWORD)v3;
if ( v8 >= 0 )
v3 = (const char *)strins(v3, &LC3, (unsigned int)v8);
v9 = strlen(v3);
v10 = (unsigned int)memchr(&v3[v8], 40, v9) - (_DWORD)v3;
v6 = v10;
}
while ( v10 >= 0 );
}
v11 = (unsigned int)strpbrk(v3, "+-") - (_DWORD)v3;
v12 = v11;
if ( v11 >= 0 )
{
do
{
v13 = strins(v3, &LC3, (unsigned int)v12);
v3 = (const char *)strins(v13, &LC2, (unsigned int)(v12 + 2));
v14 = (unsigned int)strpbrk(&v3[v12 + 2], "+-") - (_DWORD)v3;
v12 = v14;
}
while ( v14 >= 0 );
}
return v3;
}

canonical_decompiled_code:
const char *__fastcall func1(__int64 a1)
{
const char *v1; // rbp
size_t v2; // rax
const char *v3; // rbp
size_t v4; // rax
int v5; // eax
__int64 v6; // rbx
size_t v7; // rax
int v8; // ebx
size_t v9; // rax
int v10; // eax
int v11; // eax
__int64 v12; // rbx
__int64 v13; // rax
int v14; // eax

v1 = (const char *)strins(a1, &LC2, 0LL);
v2 = strlen(v1);
v3 = (const char *)strins(v1, &LC3, v2);
v4 = strlen(v3);
v5 = (unsigned int)memchr(v3 + 1, 40, v4) - (_DWORD)v3;
if ( v5 >= 0 )
{
v6 = v5;
do
{
v3 = (const char *)strins(v3, &LC2, (unsigned int)v6);
v7 = strlen(&v3[v6 + 1]);
v8 = (unsigned int)memchr(&v3[v6], 41, v7) - (_DWORD)v3;
if ( v8 >= 0 )
v3 = (const char *)strins(v3, &LC3, (unsigned int)v8);
v9 = strlen(v3);
v10 = (unsigned int)memchr(&v3[v8], 40, v9) - (_DWORD)v3;
v6 = v10;
}
while ( v10 >= 0 );
}
v11 = (unsigned int)strpbrk(v3, "+-") - (_DWORD)v3;
v12 = v11;
if ( v11 >= 0 )
{
do
{
v13 = strins(v3, &LC3, (unsigned int)v12);
v3 = (const char *)strins(v13, &LC2, (unsigned int)(v12 + 2));
v14 = (unsigned int)strpbrk(&v3[v12 + 2], "+-") - (_DWORD)v3;
v12 = v14;
}
while ( v14 >= 0 );
}
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
char i; // dl

v2 = a2;
if ( !strpbrk(a1, "()") )
return a1;
result = a1 + 1;
if ( (__int16)a2 > 0 || (result = a1 - 1, (_WORD)a2) )
{
for ( i = *result; i == 40; i = *--result )
{
while ( 1 )
{
++v2;
LABEL_10:
if ( !(_WORD)v2 )
return result;
LABEL_6:
i = result[1];
if ( !i )
return result;
if ( (__int16)v2 <= 0 )
break;
++result;
if ( i != 40 )
goto LABEL_5;
}
if ( !(_WORD)v2 )
{
--result;
return result;
}
}
LABEL_5:
if ( i == 41 )
{
--v2;
goto LABEL_10;
}
goto LABEL_6;
}
return result;
}

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

v2 = a2;
if ( !strpbrk(a1, "()") )
return a1;
result = a1 + 1;
if ( (__int16)a2 > 0 || (result = a1 - 1, (_WORD)a2) )
{
for ( i = *result; i == 40; i = *--result )
{
while ( 1 )
{
++v2;
LABEL_10:
if ( !(_WORD)v2 )
return result;
LABEL_6:
i = result[1];
if ( !i )
return result;
if ( (__int16)v2 <= 0 )
break;
++result;
if ( i != 40 )
goto LABEL_5;
}
if ( !(_WORD)v2 )
{
--result;
return result;
}
}
LABEL_5:
if ( i == 41 )
{
--v2;
goto LABEL_10;
}
goto LABEL_6;
}
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:
__int64 __fastcall parntrim(const char *a1)
{
const char *v1; // r12
int v2; // eax
int v3; // edx

v1 = a1;
v2 = strlen(a1);
v3 = v2;
if ( *a1 == 40 )
{
v1 = (const char *)strsub(a1, 1LL, (unsigned int)(v2 - 1));
v3 = strlen(v1);
if ( v1[v3 - 1] != 41 )
return (__int64)v1;
}
else if ( a1[v2 - 1] != 41 )
{
return (__int64)v1;
}
return strsub(v1, 0LL, (unsigned int)(v3 - 2));
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
const char *v1; // r12
int v2; // eax
int v3; // edx

v1 = a1;
v2 = strlen(a1);
v3 = v2;
if ( *a1 == 40 )
{
v1 = (const char *)strsub(a1, 1LL, (unsigned int)(v2 - 1));
v3 = strlen(v1);
if ( v1[v3 - 1] != 41 )
return (__int64)v1;
}
else if ( a1[v2 - 1] != 41 )
{
return (__int64)v1;
}
return strsub(v1, 0LL, (unsigned int)(v3 - 2));
}

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; // rax
char *v3; // r8
int *v4; // rbp
int *v6; // rax
char *v7; // r8

if ( !a2 )
{
v6 = _errno_location();
v7 = "None";
v4 = v6;
if ( *v6 )
v7 = strerror(*v6);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (function invalid)\n",
"src/keyboard.c",
53LL,
v7,
a1);
goto LABEL_6;
}
if ( a1 > 0x279 )
{
v2 = _errno_location();
v3 = "None";
v4 = v2;
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);
LABEL_6:
*v4 = 0;
return 1LL;
}
pthread_mutex_lock(&events_mutex);
events[a1] = a2;
pthread_mutex_unlock(&events_mutex);
return 0LL;
}

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

if ( !a2 )
{
v6 = _errno_location();
v7 = "None";
v4 = v6;
if ( *v6 )
v7 = strerror(*v6);
fprintf(
stderr,
"[ERROR] (%s:%d: errno: %s) Could not set event for key: %i (function invalid)\n",
"src/keyboard.c",
53LL,
v7,
a1);
goto LABEL_6;
}
if ( a1 > 0x279 )
{
v2 = _errno_location();
v3 = "None";
v4 = v2;
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);
LABEL_6:
*v4 = 0;
return 1LL;
}
pthread_mutex_lock(&events_mutex);
events[a1] = a2;
pthread_mutex_unlock(&events_mutex);
return 0LL;
}

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
unsigned int v3; // ebx
__int64 v4; // rsi
int v5; // eax

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

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

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

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, void *a2, int a3)
{
size_t v3; // rbp
size_t v4; // rbx
size_t v5; // r14
char *v6; // r12

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

canonical_decompiled_code:
char *__fastcall func0(char *src, void *a2, int a3)
{
size_t v3; // rbp
size_t v4; // rbx
size_t v5; // r14
char *v6; // r12

v3 = a3;
v4 = strlen((const char *)a2);
v5 = (int)(v4 + strlen(src) + 1);
v6 = (char *)malloc(v5);
strncpy(v6, src, v3);
memcpy(&v6[v3], a2, v4 + 1);
strcpy(&v6[v4 + v3], &src[v3]);
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; // ebx
char *v4; // rax
const char *v5; // r12

v3 = a3 - a2;
v4 = (char *)malloc(a3 - a2 + 2);
v5 = v4;
if ( v3 >= 0 )
memcpy(v4, (const void *)(a1 + a2), v3 + 1LL);
v5[strlen(v5)] = 0;
return v5;
}

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

v3 = a3 - a2;
v4 = (char *)malloc(a3 - a2 + 2);
v5 = v4;
if ( v3 >= 0 )
memcpy(v4, (const void *)(a1 + a2), v3 + 1LL);
v5[strlen(v5)] = 0;
return v5;
}

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:
const char *__fastcall strrpl(char *s, int a2, int a3, const char *a4)
{
const char *v6; // r12
int v7; // eax
const char *v8; // rbp
size_t v9; // rax
char *v10; // rax

v6 = strsub((__int64)s, 0, a2 - 1);
v7 = strlen(s);
v8 = strsub((__int64)s, a3 + 1, v7 - 1);
v9 = strlen(v6);
v10 = stpcpy((char *)&v6[v9], a4);
strcpy(v10, v8);
return v6;
}

canonical_decompiled_code:
const char *__fastcall func2(char *s, int a2, int a3, const char *a4)
{
const char *v6; // r12
int v7; // eax
const char *v8; // rbp
size_t v9; // rax
char *v10; // rax

v6 = func1((__int64)s, 0, a2 - 1);
v7 = strlen(s);
v8 = func1((__int64)s, a3 + 1, v7 - 1);
v9 = strlen(v6);
v10 = stpcpy((char *)&v6[v9], a4);
strcpy(v10, v8);
return v6;
}

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-38h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-28h] BYREF

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

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

v1.tv_sec = 0LL;
v1.tv_nsec = 10000000LL;
for ( remaining = 0LL; running; result = doupdate() )
nanosleep(&v1, &remaining);
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)
{
unsigned int v3; // er8
int v4; // eax

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

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3)
{
unsigned int v3; // er8
int v4; // eax

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

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)
{
unsigned int v4; // er8
int v5; // eax

if ( !a1 )
return 0LL;
v4 = 1;
if ( *(_DWORD *)a1 <= a3 )
return v4;
v5 = *(_DWORD *)(a1 + 4);
if ( v5 <= a4 )
return v4;
if ( a3 >= 0 && a4 >= 0 )
{
v4 = -1;
*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * (v5 * a3 + a4)) = a2;
return v4;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int a3, int a4)
{
unsigned int v4; // er8
int v5; // eax

if ( !a1 )
return 0LL;
v4 = 1;
if ( *(_DWORD *)a1 <= a3 )
return v4;
v5 = *(_DWORD *)(a1 + 4);
if ( v5 <= a4 )
return v4;
if ( a3 >= 0 && a4 >= 0 )
{
v4 = -1;
*(_DWORD *)(*(_QWORD *)(a1 + 8) + 4LL * (v5 * a3 + a4)) = a2;
return v4;
}
return 1LL;
}

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, void *a2, size_t n, char *dest)
{
size_t v6; // rcx
char *result; // rax
size_t v8; // r13
void *v9; // rsp
char desta[8]; // [rsp+0h] [rbp-40h] BYREF
size_t v11; // [rsp+8h] [rbp-38h]

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

canonical_decompiled_code:
char *__fastcall func6(char *src, void *a2, size_t n, char *dest)
{
size_t v6; // rcx
char *result; // rax
size_t v8; // r13
void *v9; // rsp
char desta[8]; // [rsp+0h] [rbp-40h] BYREF
size_t v11; // [rsp+8h] [rbp-38h]

if ( dest == 0LL || a2 == 0LL || !src )
return 0LL;
v6 = strlen(src);
result = 0LL;
v11 = v6;
if ( v6 >= n )
{
v8 = strlen((const char *)a2);
v9 = alloca(v11 + v8 + 1);
strncpy(desta, src, n);
memcpy(&desta[n], a2, v8 + 1);
strcpy(&desta[v8 + 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 || 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 || 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
char *v7; // r8
unsigned __int64 v8; // rbx
void *v9; // rsp
char v11[48]; // [rsp+0h] [rbp-30h] BYREF

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

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

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

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; // ebp

v1 = a1;
printw("The IA choose to remove %d matches\n", 3LL);
if ( a1 > 0 )
{
v1 = a1 - 3;
if ( a1 - 3 > 0 )
return player(v1);
}
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; // ebp

v1 = a1;
printw("The IA choose to remove %d matches\n", 3LL);
if ( a1 > 0 )
{
v1 = a1 - 3;
if ( a1 - 3 > 0 )
return func1(v1);
}
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
unsigned int v2; // ebx

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");
v2 = wgetch(stdscr) - 48;
printw("You choose to remove %d matches\n", v2);
if ( v2 )
break;
printw("Don't cheat !\n");
}
v1 -= v2;
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
unsigned int v2; // ebx

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");
v2 = wgetch(stdscr) - 48;
printw("You choose to remove %d matches\n", v2);
if ( v2 )
break;
printw("Don't cheat !\n");
}
v1 -= v2;
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