main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__uid_t v4; // er12
__gid_t v5; // er13
int v6; // ebp
int *v8; // rbx
unsigned int v9; // eax

v4 = getuid();
v5 = getgid();
if ( setuid(0) < 0 )
{
v8 = __errno_location();
v9 = (unsigned int)strerror(*v8);
fprintf(stderr, "setuid() failed: %d - %s\n", (unsigned int)*v8, (const char *)v9);
exit(-*v8);
}
if ( argc != 3 )
printUsage(argv);
v6 = atoi(argv[2]);
if ( !memcmp(argv[1], "export", 7uLL) )
{
export(v6);
changeOwner(v6, "value", v4, v5);
changeOwner(v6, "direction", v4, v5);
}
if ( !memcmp(argv[1], "unexport", 9uLL) )
unexport(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__uid_t v4; // er12
__gid_t v5; // er13
int v6; // ebp
int *v8; // rbx
unsigned int v9; // eax

v4 = getuid();
v5 = getgid();
if ( setuid(0) < 0 )
{
v8 = __errno_location();
v9 = (unsigned int)strerror(*v8);
fprintf(stderr, "setuid() failed: %d - %s\n", (unsigned int)*v8, (const char *)v9);
exit(-*v8);
}
if ( argc != 3 )
func7(argv);
v6 = atoi(argv[2]);
if ( !memcmp(argv[1], "export", 7uLL) )
{
func10(v6);
func8(v6, "value", v4, v5);
func8(v6, "direction", v4, v5);
}
if ( !memcmp(argv[1], "unexport", 9uLL) )
func11(v6);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 int uid, gid;
 uid = getuid();
 gid = getgid();
    if (setuid(0) < 0) {
     fprintf(stderr, "setuid() failed: %d - %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  exit(-(*__errno_location ()));
    }
    if (argc == 3) {
     int pin = atoi(argv[2]);
     if (strcmp(argv[1], "export") == 0) {
      export(pin);
      changeOwner(pin, "value", uid, gid);
      changeOwner(pin, "direction", uid, gid);
     }
     if (strcmp(argv[1], "unexport") == 0) {
      unexport(pin);
     }
    }
    else {
     printUsage(argv);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int uid; int gid;
 uid = getuid();
 gid = getgid();
    if (setuid(0) < 0) {
     fprintf(stderr, "setuid() failed: %d - %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  exit(-(*__errno_location ()));
    }
    if (argc == 3) {
     int pin = atoi(argv[2]);
     if (strcmp(argv[1], "export") == 0) {
      export(pin);
      changeOwner(pin, "value", uid, gid);
      changeOwner(pin, "direction", uid, gid);
     }
     if (strcmp(argv[1], "unexport") == 0) {
      unexport(pin);
     }
    }
    else {
     printUsage(argv);
    }
    return 0;
}

variable_types: argc int


repo: nuclearsandwich/pash

decompiled_code:
void *parse_value()
{
void *v0; // rbx

v0 = malloc(0x50uLL);
*((_QWORD *)v0 + 9) = 0LL;
*(_DWORD *)v0 = 1;
strncpy((char *)v0 + 4, tokens, 0x40uLL);
return v0;
}

canonical_decompiled_code:
void *func0()
{
void *v0; // rbx

v0 = malloc(0x50uLL);
*((_QWORD *)v0 + 9) = 0LL;
*(_DWORD *)v0 = 1;
strncpy((char *)v0 + 4, tokens, 0x40uLL);
return v0;
}

original_code:
ast_node *parse_value() {
 ast_node *value = malloc(sizeof(ast_node));
 value->children = ((void *)0);
 value->type = VALUE;
 strncpy(value->token, tokens->str, 64);
 return value;
}

canonical_original_code:
struct node *parse_value() {
 struct node *value = malloc(sizeof(ast_node));
 value->children = ((void *)0);
 value->type = VALUE;
 strncpy(value->token, tokens->str, 64);
 return value;
}

variable_types: struct node { enum node_type { VARASSIGN=0, VALUE=1, VARIABLE=2, COMMAND=3, ARGLIST=4, REDIRECTLIST=5, STDIN_REDIRECT=6, STDOUT_REDIRECT=7, STDERR_REDIRECT=8, DATA_VARIABLE=9, BACKGROUND_KEY=10, NEGATED_COMMAND=11 } type; char token[64]; struct nodelist { struct node *node; struct nodelist *next; } *children; } *

user_defined_types: struct node { enum node_type type; char token[64]; struct nodelist *children; }


decompiled_code:
_QWORD *parse_variable_expression()
{
char *v0; // rbx
int v1; // ebp
_QWORD *v2; // rcx
__int64 v3; // rax

v0 = tokens;
v1 = strlen(tokens);
v2 = malloc(0x50uLL);
*(_DWORD *)v2 = 2;
v2[9] = 0LL;
if ( v1 > 0 )
{
v3 = 1LL;
do
{
*((_BYTE *)v2 + v3 + 3) = v0[v3];
++v3;
}
while ( v3 != (unsigned int)(v1 - 1) + 2LL );
}
return v2;
}

canonical_decompiled_code:
_QWORD *func1()
{
char *v0; // rbx
int v1; // ebp
_QWORD *v2; // rcx
__int64 v3; // rax

v0 = tokens;
v1 = strlen(tokens);
v2 = malloc(0x50uLL);
*(_DWORD *)v2 = 2;
v2[9] = 0LL;
if ( v1 > 0 )
{
v3 = 1LL;
do
{
*((_BYTE *)v2 + v3 + 3) = v0[v3];
++v3;
}
while ( v3 != (unsigned int)(v1 - 1) + 2LL );
}
return v2;
}

original_code:
ast_node *parse_variable_expression() {
 int i, varlen = strlen(tokens->str);
 ast_node *variable = malloc(sizeof(ast_node));
 variable->type = VARIABLE;
 variable->children = ((void *)0);
 for (i = 0; i < varlen; i++) {
  variable->token[i] = tokens->str[i + 1];
 }
 return variable;
}

canonical_original_code:
struct node *parse_variable_expression() {
 int i; int varlen = strlen(tokens->str);
 struct node *variable = malloc(sizeof(ast_node));
 variable->type = VARIABLE;
 variable->children = ((void *)0);
 for (i = 0; i < varlen; i++) {
  variable->token[i] = tokens->str[i + 1];
 }
 return variable;
}

variable_types: i int

variable_types: varlen int

return_type: struct node { enum node_type { VARASSIGN=0, VALUE=1, VARIABLE=2, COMMAND=3, ARGLIST=4, REDIRECTLIST=5, STDIN_REDIRECT=6, STDOUT_REDIRECT=7, STDERR_REDIRECT=8, DATA_VARIABLE=9, BACKGROUND_KEY=10, NEGATED_COMMAND=11 } type; char token[64]; struct nodelist { struct node *node; struct nodelist *next; } *children; } *


decompiled_code:
_DWORD *parse_data_variable()
{
char *v0; // rbx
int v1; // ebp
_DWORD *v2; // rcx
__int64 v3; // rax
__int64 v4; // rdx

v0 = tokens;
v1 = strlen(tokens);
v2 = malloc(0x50uLL);
*v2 = 9;
if ( v1 > 0 )
{
v3 = 0LL;
do
{
*((_BYTE *)v2 + v3 + 4) = v0[v3 + 3];
v4 = v3++;
}
while ( v4 != v1 - 1 );
}
return v2;
}

canonical_decompiled_code:
_DWORD *func2()
{
char *v0; // rbx
int v1; // ebp
_DWORD *v2; // rcx
__int64 v3; // rax
__int64 v4; // rdx

v0 = tokens;
v1 = strlen(tokens);
v2 = malloc(0x50uLL);
*v2 = 9;
if ( v1 > 0 )
{
v3 = 0LL;
do
{
*((_BYTE *)v2 + v3 + 4) = v0[v3 + 3];
v4 = v3++;
}
while ( v4 != v1 - 1 );
}
return v2;
}

original_code:
ast_node *parse_data_variable() {
 ast_node *data_variable;
 int i, varlen = strlen(tokens->str);
 data_variable = malloc(sizeof(ast_node));
 data_variable->type = DATA_VARIABLE;
 for (i = 0; i < varlen; i++) {
  data_variable->token[i] = tokens->str[i + 3];
 }
 return data_variable;
}

canonical_original_code:
struct node *parse_data_variable() {
 struct node *data_variable;
 int i; int varlen = strlen(tokens->str);
 data_variable = malloc(sizeof(ast_node));
 data_variable->type = DATA_VARIABLE;
 for (i = 0; i < varlen; i++) {
  data_variable->token[i] = tokens->str[i + 3];
 }
 return data_variable;
}

user_defined_types: enum node_type { VARASSIGN=0, VALUE=1, VARIABLE=2, COMMAND=3, ARGLIST=4, REDIRECTLIST=5, STDIN_REDIRECT=6, STDOUT_REDIRECT=7, STDERR_REDIRECT=8, DATA_VARIABLE=9, BACKGROUND_KEY=10, NEGATED_COMMAND=11 }

user_defined_types: struct nodelist { struct node *node; struct nodelist *next; }


decompiled_code:
void strip_head()
{
char *v0; // rdi

v0 = tokens;
if ( tokens )
{
tokens = (char *)*((_QWORD *)tokens + 8);
free(v0);
}
}

canonical_decompiled_code:
void func3()
{
char *v0; // rdi

v0 = tokens;
if ( tokens )
{
tokens = (char *)*((_QWORD *)tokens + 8);
free(v0);
}
}

original_code:
void strip_head() {
 if (tokens == ((void *)0)) {
  return;
 }
 strlist *head = tokens;
 tokens = head->next;
 free(head);
}

canonical_original_code:
void strip_head() {
 if (tokens == ((void *)0)) {
  return;
 }
 struct sllist *head = tokens;
 tokens = head->next;
 free(head);
}

variable_types: struct sllist { char str[64]; struct sllist *next; } *

return_type: void


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

strip_head();
result = malloc(0x50uLL);
*(_DWORD *)result = 10;
result[9] = 0LL;
return result;
}

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

func3();
result = malloc(0x50uLL);
*(_DWORD *)result = 10;
result[9] = 0LL;
return result;
}

original_code:
ast_node *parse_background_key() {
 strip_head();
 ast_node *background_key = malloc(sizeof(ast_node));
 background_key->type = BACKGROUND_KEY;
 background_key->children = ((void *)0);
 return background_key;
}

canonical_original_code:
struct node *parse_background_key() {
 strip_head();
 struct node *background_key = malloc(sizeof(ast_node));
 background_key->type = BACKGROUND_KEY;
 background_key->children = ((void *)0);
 return background_key;
}


decompiled_code:
__int64 __fastcall contains_eql(const char *a1)
{
size_t v1; // rdx
__int64 v2; // rax
unsigned int v3; // ecx

v1 = strlen(a1);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 == v2 )
break;
if ( a1[v2++] == 61 )
return v3;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
size_t v1; // rdx
__int64 v2; // rax
unsigned int v3; // ecx

v1 = strlen(a1);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 == v2 )
break;
if ( a1[v2++] == 61 )
return v3;
}
return (unsigned int)-1;
}

original_code:
int contains_eql(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (str[i] == '=') {
   return i;
  }
 }
 return -1;
}

variable_types:
str
char *

return_type: int


decompiled_code:
__int64 __fastcall contains_colon(const char *a1)
{
size_t v1; // rdx
__int64 v2; // rax
unsigned int v3; // ecx

v1 = strlen(a1);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 == v2 )
break;
if ( a1[v2++] == 58 )
return v3;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
size_t v1; // rdx
__int64 v2; // rax
unsigned int v3; // ecx

v1 = strlen(a1);
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 == v2 )
break;
if ( a1[v2++] == 58 )
return v3;
}
return (unsigned int)-1;
}

original_code:
int contains_colon(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (str[i] == ':') {
   return i;
  }
 }
 return -1;
}


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

result = 4LL;
if ( strcmp(tokens, "<") )
{
result = 1LL;
if ( strcmp(tokens, ">") )
result = 2 * (unsigned int)(strcmp(tokens, "2>") == 0);
}
return result;
}

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

result = 4LL;
if ( strcmp(tokens, "<") )
{
result = 1LL;
if ( strcmp(tokens, ">") )
result = 2 * (unsigned int)(strcmp(tokens, "2>") == 0);
}
return result;
}

original_code:
int is_redirect_token() {
 if (strcmp(tokens->str, "<") == 0) {
  return 4;
 } else if (strcmp(tokens->str, ">") == 0) {
  return 1;
 } else if (strcmp(tokens->str, "2>") == 0) {
  return 2;
 } else {
  return 0;
 }
}


decompiled_code:
_BOOL8 is_background_key()
{
return strcmp(tokens, "&") == 0;
}

canonical_decompiled_code:
_BOOL8 func8()
{
return strcmp(tokens, "&") == 0;
}

original_code:
int is_background_key() {
 if (strcmp(tokens->str, "&") == 0) {
  return 1;
 } else {
  return 0;
 }
}


decompiled_code:
_BOOL8 is_special_token()
{
int v0; // edx
_BOOL8 result; // rax
_BOOL4 v2; // edx

v0 = is_redirect_token();
result = 1LL;
if ( !v0 )
{
v2 = is_background_key();
result = 1LL;
if ( !v2 )
{
if ( strcmp(tokens, "&&") )
result = strcmp(tokens, "||") == 0;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 func9()
{
int v0; // edx
_BOOL8 result; // rax
_BOOL4 v2; // edx

v0 = func7();
result = 1LL;
if ( !v0 )
{
v2 = func8();
result = 1LL;
if ( !v2 )
{
if ( strcmp(tokens, "&&") )
result = strcmp(tokens, "||") == 0;
}
}
return result;
}

original_code:
int is_special_token() {
 if (is_redirect_token() || is_background_key()) {
  return 1;
 }
 if (strcmp(tokens->str, "&&") == 0) {
  return 1;
 } else if (strcmp(tokens->str, "||") == 0) {
  return 1;
 } else {
  return 0;
 }
}


decompiled_code:
char *parse_arg()
{
char *v0; // rbx

v0 = tokens;
if ( tokens )
{
if ( is_special_token() )
{
v0 = 0LL;
}
else if ( *v0 == 36 )
{
v0 = (char *)parse_variable_expression();
}
else if ( *(_WORD *)v0 == 17443 && v0[2] == 58 )
{
v0 = (char *)parse_data_variable();
}
else
{
v0 = (char *)parse_value();
}
}
return v0;
}

original_code:
ast_node *parse_arg() {
 if (tokens == ((void *)0) || is_special_token()) {
  return ((void *)0);
 }
 if (tokens->str[0] == '$') {
  return parse_variable_expression();
 }
 if (tokens->str[0] == '#' && tokens->str[1] == 'D' && tokens->str[2] == ':') {
  return parse_data_variable();
 }
 return parse_value();
}

canonical_original_code:
struct node *parse_arg() {
 if (tokens == ((void *)0) || is_special_token()) {
  return ((void *)0);
 }
 if (tokens->str[0] == '$') {
  return parse_variable_expression();
 }
 if (tokens->str[0] == '#' && tokens->str[1] == 'D' && tokens->str[2] == ':') {
  return parse_data_variable();
 }
 return parse_value();
}


decompiled_code:
_QWORD *parse_arg_list()
{
_QWORD *v0; // r12
_QWORD *v1; // r13
char *v2; // rax
char *v3; // rbp
_QWORD *v4; // rbx

v0 = malloc(0x50uLL);
v1 = malloc(0x10uLL);
*(_DWORD *)v0 = 4;
v0[9] = v1;
v2 = parse_arg();
*v1 = v2;
v1[1] = 0LL;
if ( v2 )
strip_head();
v3 = parse_arg();
if ( v3 )
{
strip_head();
while ( 1 )
{
v4 = malloc(0x10uLL);
v1[1] = v4;
*v4 = v3;
v4[1] = 0LL;
v3 = parse_arg();
if ( !v3 )
break;
strip_head();
v1 = v4;
}
}
return v0;
}

original_code:
ast_node *parse_arg_list() {
 ast_node *arg_list = malloc(sizeof(ast_node)), *arg_node;
 ast_nodelist *head, *tail;
 head = tail = malloc(sizeof(ast_nodelist));
 arg_list->type = ARGLIST;
 arg_list->children = head;
 arg_node = parse_arg();
 tail->node = arg_node;
 tail->next = ((void *)0);
 if (arg_node != ((void *)0)) {
  strip_head();
 }
 arg_node = parse_arg();
 if (arg_node != ((void *)0)) {
  strip_head();
 }
 while (arg_node != ((void *)0)) {
  tail->next = malloc(sizeof(ast_nodelist));
  tail = tail->next;
  tail->node = arg_node;
  tail->next = ((void *)0);
  arg_node = parse_arg();
  if (arg_node != ((void *)0)) {
   strip_head();
  }
 }
 return arg_list;
}

canonical_original_code:
struct node *parse_arg_list() {
 struct node *arg_list = malloc(sizeof(ast_node)); struct node *arg_node;
 struct nodelist *head; struct nodelist *tail;
 head = tail = malloc(sizeof(ast_nodelist));
 arg_list->type = ARGLIST;
 arg_list->children = head;
 arg_node = parse_arg();
 tail->node = arg_node;
 tail->next = ((void *)0);
 if (arg_node != ((void *)0)) {
  strip_head();
 }
 arg_node = parse_arg();
 if (arg_node != ((void *)0)) {
  strip_head();
 }
 while (arg_node != ((void *)0)) {
  tail->next = malloc(sizeof(ast_nodelist));
  tail = tail->next;
  tail->node = arg_node;
  tail->next = ((void *)0);
  arg_node = parse_arg();
  if (arg_node != ((void *)0)) {
   strip_head();
  }
 }
 return arg_list;
}

variable_types: struct nodelist { struct node { enum node_type { VARASSIGN=0, VALUE=1, VARIABLE=2, COMMAND=3, ARGLIST=4, REDIRECTLIST=5, STDIN_REDIRECT=6, STDOUT_REDIRECT=7, STDERR_REDIRECT=8, DATA_VARIABLE=9, BACKGROUND_KEY=10, NEGATED_COMMAND=11 } type; char token[64]; struct nodelist *children; } *node; struct nodelist *next; } *


decompiled_code:
_QWORD *parse_redirect_list()
{
_QWORD *v0; // r13
char *v1; // rax
char *v2; // rbp
_QWORD *v3; // rbx
char *i; // rbp
_QWORD *v5; // r12

v0 = malloc(0x50uLL);
*(_DWORD *)v0 = 5;
v1 = parse_redirect();
if ( v1 )
{
v2 = v1;
v3 = malloc(0x50uLL);
v0[9] = v3;
*v3 = v2;
v3[1] = 0LL;
for ( i = parse_redirect(); i; i = parse_redirect() )
{
v5 = v3;
v3 = malloc(0x10uLL);
v5[1] = v3;
*v3 = i;
v3[1] = 0LL;
}
}
return v0;
}

original_code:
ast_node *parse_redirect_list() {
 ast_node *redirect, *redirect_list = malloc(sizeof(ast_node));
 ast_nodelist *tail_child;
 redirect_list->type = REDIRECTLIST;
 redirect = parse_redirect();
 if (redirect != ((void *)0)) {
  redirect_list->children = malloc(sizeof(ast_node));
  redirect_list->children->node = redirect;
  redirect_list->children->next = ((void *)0);
  tail_child = redirect_list->children;
  redirect = parse_redirect();
 }
 while (redirect != ((void *)0)) {
  tail_child->next = malloc(sizeof(ast_nodelist));
  tail_child = tail_child->next;
  tail_child->node = redirect;
  tail_child->next = ((void *)0);
  redirect = parse_redirect();
 }
 return redirect_list;
}

canonical_original_code:
struct node *parse_redirect_list() {
 struct node *redirect; struct node *redirect_list = malloc(sizeof(ast_node));
 struct nodelist *tail_child;
 redirect_list->type = REDIRECTLIST;
 redirect = parse_redirect();
 if (redirect != ((void *)0)) {
  redirect_list->children = malloc(sizeof(ast_node));
  redirect_list->children->node = redirect;
  redirect_list->children->next = ((void *)0);
  tail_child = redirect_list->children;
  redirect = parse_redirect();
 }
 while (redirect != ((void *)0)) {
  tail_child->next = malloc(sizeof(ast_nodelist));
  tail_child = tail_child->next;
  tail_child->node = redirect;
  tail_child->next = ((void *)0);
  redirect = parse_redirect();
 }
 return redirect_list;
}


decompiled_code:
char *parse_command()
{
char *v0; // rbx
_QWORD *v1; // r12
_QWORD *v2; // rbp
_QWORD *v3; // rbp
_QWORD *v5; // rbp
_QWORD *v6; // rbp

v0 = (char *)malloc(0x50uLL);
*(_DWORD *)v0 = 3;
strncpy(v0 + 4, tokens, 0x40uLL);
strip_head();
if ( tokens )
{
v1 = 0LL;
if ( is_special_token()
|| (v5 = malloc(0x10uLL),
*((_QWORD *)v0 + 9) = v5,
*v5 = parse_arg_list(),
*(_QWORD *)(*((_QWORD *)v0 + 9) + 8LL) = 0LL,
v1 = (_QWORD *)*((_QWORD *)v0 + 9),
tokens) )
{
if ( !(unsigned int)is_redirect_token() )
goto LABEL_18;
if ( *((_QWORD *)v0 + 9) )
{
v2 = malloc(0x10uLL);
v1[1] = v2;
*v2 = parse_redirect_list();
v2[1] = 0LL;
v1 = v2;
}
else
{
v6 = malloc(0x10uLL);
*((_QWORD *)v0 + 9) = v6;
*v6 = parse_redirect_list();
*(_QWORD *)(*((_QWORD *)v0 + 9) + 8LL) = 0LL;
}
if ( tokens )
{
LABEL_18:
if ( is_background_key() )
{
if ( *((_QWORD *)v0 + 9) )
{
v3 = malloc(0x10uLL);
v1[1] = v3;
}
else
{
v3 = malloc(0x10uLL);
*((_QWORD *)v0 + 9) = v3;
}
*v3 = parse_background_key();
v3[1] = 0LL;
}
}
}
}
return v0;
}

original_code:
ast_node *parse_command() {
 ast_node *command = malloc(sizeof(ast_node));
 ast_nodelist *tail_child = ((void *)0);
 command->type = COMMAND;
 strncpy(command->token, tokens->str, 64);
 strip_head();
 if (tokens != ((void *)0) && !is_special_token()) {
  command->children = malloc(sizeof(ast_nodelist));
  command->children->node = parse_arg_list();
  command->children->next = ((void *)0);
  tail_child = command->children;
 }
 if (tokens != ((void *)0) && is_redirect_token()) {
  if (command->children == ((void *)0)) {
   command->children = malloc(sizeof(ast_nodelist));
   command->children->node = parse_redirect_list();
   command->children->next = ((void *)0);
  } else {
   tail_child->next = malloc(sizeof(ast_nodelist));
   tail_child = tail_child->next;
   tail_child->node = parse_redirect_list();
   tail_child->next = ((void *)0);
  }
 }
 if (tokens != ((void *)0) && is_background_key()) {
  if (command->children == ((void *)0)) {
   tail_child = command->children = malloc(sizeof(ast_nodelist));
  } else {
   tail_child->next = malloc(sizeof(ast_nodelist));
   tail_child = tail_child->next;
  }
   tail_child->node = parse_background_key();
   tail_child->next = ((void *)0);
 }
 return command;
}

canonical_original_code:
struct node *parse_command() {
 struct node *command = malloc(sizeof(ast_node));
 struct nodelist *tail_child = ((void *)0);
 command->type = COMMAND;
 strncpy(command->token, tokens->str, 64);
 strip_head();
 if (tokens != ((void *)0) && !is_special_token()) {
  command->children = malloc(sizeof(ast_nodelist));
  command->children->node = parse_arg_list();
  command->children->next = ((void *)0);
  tail_child = command->children;
 }
 if (tokens != ((void *)0) && is_redirect_token()) {
  if (command->children == ((void *)0)) {
   command->children = malloc(sizeof(ast_nodelist));
   command->children->node = parse_redirect_list();
   command->children->next = ((void *)0);
  } else {
   tail_child->next = malloc(sizeof(ast_nodelist));
   tail_child = tail_child->next;
   tail_child->node = parse_redirect_list();
   tail_child->next = ((void *)0);
  }
 }
 if (tokens != ((void *)0) && is_background_key()) {
  if (command->children == ((void *)0)) {
   tail_child = command->children = malloc(sizeof(ast_nodelist));
  } else {
   tail_child->next = malloc(sizeof(ast_nodelist));
   tail_child = tail_child->next;
  }
   tail_child->node = parse_background_key();
   tail_child->next = ((void *)0);
 }
 return command;
}


decompiled_code:
_QWORD *parse_negated_command()
{
_QWORD *v0; // rbx
_QWORD *v1; // rbp

v0 = malloc(0x50uLL);
*(_DWORD *)v0 = 11;
strip_head();
v1 = malloc(0x10uLL);
v0[9] = v1;
v1[1] = 0LL;
*v1 = parse_command();
return v0;
}

canonical_decompiled_code:
_QWORD *func15()
{
_QWORD *v0; // rbx
_QWORD *v1; // rbp

v0 = malloc(0x50uLL);
*(_DWORD *)v0 = 11;
func3();
v1 = malloc(0x10uLL);
v0[9] = v1;
v1[1] = 0LL;
*v1 = func14();
return v0;
}

original_code:
ast_node *parse_negated_command() {
 ast_node *negated_command = malloc(sizeof(ast_node));
 negated_command->type = NEGATED_COMMAND;
 strip_head();
 negated_command->children = malloc(sizeof(ast_nodelist));
 negated_command->children->next = ((void *)0);
 negated_command->children->node = parse_command();
 return negated_command;
}

canonical_original_code:
struct node *parse_negated_command() {
 struct node *negated_command = malloc(sizeof(ast_node));
 negated_command->type = NEGATED_COMMAND;
 strip_head();
 negated_command->children = malloc(sizeof(ast_nodelist));
 negated_command->children->next = ((void *)0);
 negated_command->children->node = parse_command();
 return negated_command;
}


decompiled_code:
char *__fastcall parse_network_command(int a1)
{
char *v1; // rbp
char *v2; // r12
char *v3; // r13

v1 = (char *)malloc(0x48uLL);
v2 = (char *)malloc(0x48uLL);
*(_QWORD *)v1 = 6845299LL;
*((_QWORD *)v1 + 1) = 0LL;
*((_QWORD *)v1 + 2) = 0LL;
*((_QWORD *)v1 + 3) = 0LL;
*((_QWORD *)v1 + 4) = 0LL;
*((_QWORD *)v1 + 5) = 0LL;
*((_QWORD *)v1 + 6) = 0LL;
*((_QWORD *)v1 + 7) = 0LL;
tokens[a1] = 0;
v3 = tokens;
strncpy(v2, tokens, 0x40uLL);
strncpy(v3, &v3[a1 + 1], 63 - a1);
*((_QWORD *)v1 + 8) = v2;
*((_QWORD *)v2 + 8) = tokens;
tokens = v1;
return parse_command();
}

original_code:
ast_node *parse_network_command(int colonidx) {
 strlist *ssh_token, *hostname_token;
 ssh_token = malloc(sizeof(strlist));
 hostname_token = malloc(sizeof(strlist));
 strncpy(ssh_token->str, "ssh", 64);
 tokens->str[colonidx] = '\0';
 strncpy(hostname_token->str, tokens->str, 64);
 strncpy(tokens->str, &tokens->str[colonidx+1], 64 - (colonidx + 1));
 ssh_token->next = hostname_token;
 hostname_token->next = tokens;
 tokens = ssh_token;
 return parse_command();
}

canonical_original_code:
struct node *parse_network_command(int colonidx) {
 struct sllist *ssh_token; struct sllist *hostname_token;
 ssh_token = malloc(sizeof(strlist));
 hostname_token = malloc(sizeof(strlist));
 strncpy(ssh_token->str, "ssh", 64);
 tokens->str[colonidx] = '\0';
 strncpy(hostname_token->str, tokens->str, 64);
 strncpy(tokens->str, &tokens->str[colonidx+1], 64 - (colonidx + 1));
 ssh_token->next = hostname_token;
 hostname_token->next = tokens;
 tokens = ssh_token;
 return parse_command();
}

variable_types: colonidx int

user_defined_types: struct sllist { char str[64]; struct sllist *next; }


decompiled_code:
_BOOL8 __fastcall is_negation(const char *a1)
{
return strcmp(a1, "!") == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func17(const char *a1)
{
return strcmp(a1, "!") == 0;
}

original_code:
int is_negation(char *str) {
 if (strcmp(str, "!") == 0) {
  return 1;
 } else {
  return 0;
 }
}


parse

decompiled_code:
char *parse()
{
const char *v0; // rbx
int v1; // eax
int v2; // eax

v0 = tokens;
v1 = contains_eql(tokens);
if ( v1 != -1 )
return parse_var_assign(v1);
v2 = contains_colon(v0);
if ( v2 != -1 )
return parse_network_command(v2);
if ( is_negation(v0) )
return (char *)parse_negated_command();
return parse_command();
}

canonical_decompiled_code:
char *func18()
{
const char *v0; // rbx
int v1; // eax
int v2; // eax

v0 = tokens;
v1 = func5(tokens);
if ( v1 != -1 )
return parse_var_assign(v1);
v2 = func6(v0);
if ( v2 != -1 )
return func16(v2);
if ( func17(v0) )
return (char *)func15();
return func14();
}

original_code:
ast_node *parse() {
 int eql, colon;
 ast_node *root;
 if ((eql = contains_eql(tokens->str)) != -1) {
  root = parse_var_assign(eql);
 } else if ((colon = contains_colon(tokens->str)) != -1) {
  root = parse_network_command(colon);
 } else if (is_negation(tokens->str)) {
  root = parse_negated_command();
 } else {
  root = parse_command();
 }
 return root;
}

canonical_original_code:
struct node *parse() {
 int eql; int colon;
 struct node *root;
 if ((eql = contains_eql(tokens->str)) != -1) {
  root = parse_var_assign(eql);
 } else if ((colon = contains_colon(tokens->str)) != -1) {
  root = parse_network_command(colon);
 } else if (is_negation(tokens->str)) {
  root = parse_negated_command();
 } else {
  root = parse_command();
 }
 return root;
}

variable_types: eql int

variable_types: colon int


decompiled_code:
size_t parser_error()
{
return fwrite("Parser error\n", 1uLL, 0xDuLL, stderr);
}

canonical_decompiled_code:
size_t func19()
{
return fwrite("Parser error\n", 1uLL, 0xDuLL, stderr);
}

original_code:
void parser_error() {
 fprintf(stderr, "Parser error\n");
 return;
}


decompiled_code:
int __fastcall interpret_var_assign(__int64 a1)
{
return setenv(
(const char *)(**(_QWORD **)(a1 + 72) + 4LL),
(const char *)(**(_QWORD **)(*(_QWORD *)(a1 + 72) + 8LL) + 4LL),
1);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
return setenv(
(const char *)(**(_QWORD **)(a1 + 72) + 4LL),
(const char *)(**(_QWORD **)(*(_QWORD *)(a1 + 72) + 8LL) + 4LL),
1);
}

original_code:
int interpret_var_assign(ast_node *var_assign) {
 char *varname, *value;
 varname = var_assign->children->node->token;
 value = var_assign->children->next->node->token;
 return setenv(varname, value, 1);
}

canonical_original_code:
int interpret_var_assign(struct node *var_assign) {
 char *varname; char *value;
 varname = var_assign->children->node->token;
 value = var_assign->children->next->node->token;
 return setenv(varname, value, 1);
}

variable_types:
varname
char *

variable_types:
value
char *


original_code:
char *interpret_variable(ast_node *variable) {
 char *value = (char*)malloc(sizeof(char) * 256);
 char *envar = getenv(variable->token);
 if (envar != ((void *)0)) {
  strncpy(value, envar, 256);
 } else {
  strncpy(value, "", 256);
 }
 return value;
}

canonical_original_code:
char *interpret_variable(struct node *variable) {
 char *value = (char*)malloc(sizeof(char) * 256);
 char *envar = getenv(variable->token);
 if (envar != ((void *)0)) {
  strncpy(value, envar, 256);
 } else {
  strncpy(value, "", 256);
 }
 return value;
}

variable_types:
envar
char *

return_type: char *


decompiled_code:
__int64 __fastcall interpret_data_variable(__int64 a1)
{
return resolve_data(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return resolve_data(a1 + 4);
}

original_code:
char *interpret_data_variable(ast_node *data_variable) {
 return resolve_data(data_variable->token);
}

canonical_original_code:
char *interpret_data_variable(struct node *data_variable) {
 return resolve_data(data_variable->token);
}


decompiled_code:
__int64 __fastcall interpret_value(__int64 a1)
{
return a1 + 4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return a1 + 4;
}

original_code:
char *interpret_value(ast_node *value) {
 return value->token;
}

canonical_original_code:
char *interpret_value(struct node *value) {
 return value->token;
}


decompiled_code:
char *__fastcall interpret_arg(_DWORD *a1)
{
if ( *a1 == 2 )
return interpret_variable((__int64)a1);
if ( *a1 == 9 )
return (char *)interpret_data_variable((__int64)a1);
return (char *)interpret_value((__int64)a1);
}

canonical_decompiled_code:
char *__fastcall func4(_DWORD *a1)
{
if ( *a1 == 2 )
return func1((__int64)a1);
if ( *a1 == 9 )
return (char *)func2((__int64)a1);
return (char *)func3((__int64)a1);
}

original_code:
char *interpret_arg(ast_node *arg) {
 if (arg->type == VARIABLE) {
  return interpret_variable(arg);
 } else if (arg->type == DATA_VARIABLE) {
  return interpret_data_variable(arg);
 } else {
  return interpret_value(arg);
 }
}

canonical_original_code:
char *interpret_arg(struct node *arg) {
 if (arg->type == VARIABLE) {
  return interpret_variable(arg);
 } else if (arg->type == DATA_VARIABLE) {
  return interpret_data_variable(arg);
 } else {
  return interpret_value(arg);
 }
}


decompiled_code:
_BOOL8 __fastcall interpret_negated_command(__int64 a1)
{
return (unsigned int)interpret_command(**(_QWORD **)(a1 + 72)) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(__int64 a1)
{
return (unsigned int)func5(**(_QWORD **)(a1 + 72)) == 0;
}

original_code:
int interpret_negated_command(ast_node *negated_command) {
 int command_status;
 command_status = interpret_command(negated_command->children->node);
 if (command_status == 0) {
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
int interpret_negated_command(struct node *negated_command) {
 int command_status;
 command_status = interpret_command(negated_command->children->node);
 if (command_status == 0) {
  return 1;
 } else {
  return 0;
 }
}


decompiled_code:
int set_exitstatuses()
{
unsigned int i; // ebx
int result; // eax
unsigned int v2; // eax
char s[3]; // [rsp+Bh] [rbp-Dh] BYREF
char name[10]; // [rsp+Eh] [rbp-Ah] BYREF

for ( i = 0; i != 10; ++i )
{
sprintf(name, "?%1d", i);
v2 = fifo_peek(10LL, &exitstatuses, exitstatus_head, i);
if ( v2 == -99 )
{
result = unsetenv(name);
}
else
{
sprintf(s, "%3d", v2);
result = setenv(name, s, 1);
}
}
return result;
}

canonical_decompiled_code:
int func7()
{
unsigned int i; // ebx
int result; // eax
unsigned int v2; // eax
char s[3]; // [rsp+Bh] [rbp-Dh] BYREF
char name[10]; // [rsp+Eh] [rbp-Ah] BYREF

for ( i = 0; i != 10; ++i )
{
sprintf(name, "?%1d", i);
v2 = fifo_peek(10LL, &exitstatuses, exitstatus_head, i);
if ( v2 == -99 )
{
result = unsetenv(name);
}
else
{
sprintf(s, "%3d", v2);
result = setenv(name, s, 1);
}
}
return result;
}

original_code:
void set_exitstatuses() {
 int i, status;
 char var[2];
 char val[3];
 for (i = 0; i < 10; i++) {
  sprintf(var, "?%1d", i);
  status = fifo_peek(10, exitstatuses,
    exitstatus_head, i);
  if (status != -99) {
   sprintf(val, "%3d", status);
   setenv(var, val, 1);
  } else {
   unsetenv(var);
  }
 }
 return;
}

canonical_original_code:
void set_exitstatuses() {
 int i; int status;
 char var[2];
 char val[3];
 for (i = 0; i < 10; i++) {
  sprintf(var, "?%1d", i);
  status = fifo_peek(10, exitstatuses,
    exitstatus_head, i);
  if (status != -99) {
   sprintf(val, "%3d", status);
   setenv(var, val, 1);
  } else {
   unsetenv(var);
  }
 }
 return;
}

variable_types: status int

variable_types:
var
char [2]

variable_types:
val
char [3]


decompiled_code:
void __fastcall free_ast(void **a1)
{
if ( a1 )
{
if ( a1[9] )
free_ast_list(a1[9]);
else
free(a1);
}
}

canonical_decompiled_code:
void __fastcall func9(void **a1)
{
if ( a1 )
{
if ( a1[9] )
func8(a1[9]);
else
free(a1);
}
}

original_code:
void free_ast(ast_node *root) {
 if (root != ((void *)0)) {
  if (root->children == ((void *)0)) {
   free(root);
  } else {
   free_ast_list(root->children);
  }
 }
}

canonical_original_code:
void free_ast(struct node *root) {
 if (root != ((void *)0)) {
  if (root->children == ((void *)0)) {
   free(root);
  } else {
   free_ast_list(root->children);
  }
 }
}


interpret

decompiled_code:
void __fastcall interpret(void **a1)
{
__int64 v1; // rcx

if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 11 )
v1 = interpret_negated_command((__int64)a1);
else
v1 = (unsigned int)interpret_command((__int64)a1);
}
else
{
v1 = (unsigned int)interpret_var_assign((__int64)a1);
}
fifo_push(10LL, &exitstatuses, exitstatus_head, v1);
set_exitstatuses();
free_ast(a1);
}

canonical_decompiled_code:
void __fastcall func10(void **a1)
{
__int64 v1; // rcx

if ( *(_DWORD *)a1 )
{
if ( *(_DWORD *)a1 == 11 )
v1 = func6((__int64)a1);
else
v1 = (unsigned int)func5((__int64)a1);
}
else
{
v1 = (unsigned int)func0((__int64)a1);
}
fifo_push(10LL, &exitstatuses, exitstatus_head, v1);
func7();
func9(a1);
}

original_code:
void interpret(ast_node *root) {
 int exit_status;
 if (root->type == VARASSIGN) {
  exit_status = interpret_var_assign(root);
 } else if (root->type == NEGATED_COMMAND) {
  exit_status = interpret_negated_command(root);
 }else {
  exit_status = interpret_command(root);
 }
 fifo_push(10, exitstatuses, exitstatus_head, exit_status);
 set_exitstatuses();
 free_ast(root);
 return;
}

canonical_original_code:
void interpret(struct node *root) {
 int exit_status;
 if (root->type == VARASSIGN) {
  exit_status = interpret_var_assign(root);
 } else if (root->type == NEGATED_COMMAND) {
  exit_status = interpret_negated_command(root);
 }else {
  exit_status = interpret_command(root);
 }
 fifo_push(10, exitstatuses, exitstatus_head, exit_status);
 set_exitstatuses();
 free_ast(root);
 return;
}


chomp

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

v1 = &a1[(int)strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
return a1;
}

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

v1 = &a1[(int)strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
return a1;
}

original_code:
char *chomp(char* str) {
 int len = strlen(str);
 if (str[len - 1] == '\n') {
  str[len - 1] = '\0';
 }
 return str;
}

canonical_original_code:
char *chomp(char *str) {
 int len = strlen(str);
 if (str[len - 1] == '\n') {
  str[len - 1] = '\0';
 }
 return str;
}

variable_types: len int


searchVariable

decompiled_code:
char *__fastcall searchVariable(char *a1, char *a2)
{
char *result; // rax

for ( result = strtok(a1, delimToken); result; result = strtok(0LL, delimToken) )
{
result = searchPath(result, a2);
if ( result )
break;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func1(char *a1, char *a2)
{
char *result; // rax

for ( result = strtok(a1, delimToken); result; result = strtok(0LL, delimToken) )
{
result = func0(result, a2);
if ( result )
break;
}
return result;
}

original_code:
char* searchVariable(char* inputVariable, char* inputExecutableName)
{
 char* currentPath = strtok(inputVariable, delimToken);
 if(currentPath == ((void *)0))
  return ((void *)0);
 while(currentPath != ((void *)0))
 {
  char* filePath = searchPath(currentPath, inputExecutableName);
  if(filePath != ((void *)0))
   return filePath;
  currentPath = strtok(((void *)0), delimToken);
 }
 return ((void *)0);
}

canonical_original_code:
char *searchVariable(char *inputVariable, char *inputExecutableName)
{
 char *currentPath = strtok(inputVariable, delimToken);
 if(currentPath == ((void *)0))
  return ((void *)0);
 while(currentPath != ((void *)0))
 {
  char *filePath = searchPath(currentPath, inputExecutableName);
  if(filePath != ((void *)0))
   return filePath;
  currentPath = strtok(((void *)0), delimToken);
 }
 return ((void *)0);
}

variable_types:
inputVariable
char *

variable_types:
inputExecutableName
char *

variable_types:
currentPath
char *

variable_types:
filePath
char *


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

v1 = getenv("DATA");
return searchVariable(v1, a1);
}

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

v1 = getenv("DATA");
return func1(v1, a1);
}

original_code:
char* resolve_data(char* inputExecutableName)
{
 return searchVariable(getenv("DATA"), inputExecutableName);
}

canonical_original_code:
char *resolve_data(char *inputExecutableName)
{
 return searchVariable(getenv("DATA"), inputExecutableName);
}


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

v1 = getenv("PATH");
return searchVariable(v1, a1);
}

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

v1 = getenv("PATH");
return func1(v1, a1);
}

original_code:
char* resolve_path(char* inputExecutableName)
{
 return searchVariable(getenv("PATH"), inputExecutableName);
}

canonical_original_code:
char *resolve_path(char *inputExecutableName)
{
 return searchVariable(getenv("PATH"), inputExecutableName);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v4; // ax
int v5; // ebx
size_t v6; // rax
int fd; // [rsp+0h] [rbp-458h] BYREF
socklen_t addr_len; // [rsp+4h] [rbp-454h] BYREF
__int64 v9; // [rsp+8h] [rbp-450h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-448h] BYREF
pthread_t v11; // [rsp+18h] [rbp-440h] BYREF
char buf[10]; // [rsp+26h] [rbp-432h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-428h] BYREF
struct sockaddr v14; // [rsp+40h] [rbp-418h] BYREF
char s[1032]; // [rsp+50h] [rbp-408h] BYREF

if ( argc == 2 )
{
v9 = 0LL;
client_list = (__int64)&v9;
initialize_hue(argc, argv, envp);
fd = socket(2, 2, 0);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
v4 = atoi(argv[1]);
*(_WORD *)addr.sa_data = _bswap_16(v4);
bind(fd, &addr, 0x10u);
pthread_create(&newthread, 0LL, (void *(*)(void *))&loop_check_timeouts, 0LL);
pthread_create(&v11, 0LL, (void *(*)(void *))&loop_ping_clients, 0LL);
while ( 1 )
{
addr_len = 16;
v5 = recvfrom(fd, buf, 0xAuLL, 0, &v14, &addr_len);
process_recvfrom(client_list, buf, &v14, &addr_len, &fd, s);
printf("From main() reply: %s\n", s);
v6 = strlen(s);
sendto(fd, s, v6, 0, &v14, 0x10u);
puts("-------------------------------------------------------");
buf[v5] = 0;
printf("Received the following:\n%s", buf);
puts("-------------------------------------------------------");
}
}
printf("usage: %s <port>\n", *argv);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v4; // ax
int v5; // ebx
size_t v6; // rax
int fd; // [rsp+0h] [rbp-458h] BYREF
socklen_t addr_len; // [rsp+4h] [rbp-454h] BYREF
__int64 v9; // [rsp+8h] [rbp-450h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-448h] BYREF
pthread_t v11; // [rsp+18h] [rbp-440h] BYREF
char buf[10]; // [rsp+26h] [rbp-432h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-428h] BYREF
struct sockaddr v14; // [rsp+40h] [rbp-418h] BYREF
char s[1032]; // [rsp+50h] [rbp-408h] BYREF

if ( argc == 2 )
{
v9 = 0LL;
client_list = (__int64)&v9;
initialize_hue(argc, argv, envp);
fd = socket(2, 2, 0);
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func0(0);
v4 = atoi(argv[1]);
*(_WORD *)addr.sa_data = func1(v4);
bind(fd, &addr, 0x10u);
pthread_create(&newthread, 0LL, (void *(*)(void *))&loop_check_timeouts, 0LL);
pthread_create(&v11, 0LL, (void *(*)(void *))&loop_ping_clients, 0LL);
while ( 1 )
{
addr_len = 16;
v5 = recvfrom(fd, buf, 0xAuLL, 0, &v14, &addr_len);
process_recvfrom(client_list, buf, &v14, &addr_len, &fd, s);
printf("From main() reply: %s\n", s);
v6 = strlen(s);
sendto(fd, s, v6, 0, &v14, 0x10u);
puts("-------------------------------------------------------");
buf[v5] = 0;
printf("Received the following:\n%s", buf);
puts("-------------------------------------------------------");
}
}
printf("usage: %s <port>\n", *argv);
return 0;
}

original_code:
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("usage: %s <port>\n", argv[0]);
  } else {
  int sockfd, n;
  struct sockaddr_in servaddr, client_address;
  socklen_t len;
  char mesg[10], reply[1000];
  list *end = ((void *)0);
  client_list = &end;
  pthread_t timeout_thread;
  pthread_t ping_client_thread;
  initialize_hue();
  sockfd = socket(2, SOCK_DGRAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  servaddr.sin_port = htons(atoi(argv[1]));
  bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  pthread_create(&timeout_thread, ((void *)0), loop_check_timeouts, ((void *)0));
  pthread_create(&ping_client_thread, ((void *)0), loop_ping_clients, ((void *)0));
  while (1) {
    len = sizeof(client_address);
    n = recvfrom(sockfd, mesg, 10, 0, (struct sockaddr *) &client_address, &len);
    process_recvfrom(client_list, mesg, &client_address, &len, &sockfd, reply);
    printf("From main() reply: %s\n", reply);
    sendto(sockfd, reply, strlen(reply), 0, (struct sockaddr *) &client_address, sizeof(client_address));
    printf("-------------------------------------------------------\n");
    mesg[n] = 0;
    printf("Received the following:\n%s", mesg);
    printf("-------------------------------------------------------\n");
  }}
}

canonical_original_code:
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("usage: %s <port>\n", argv[0]);
  } else {
  int sockfd; int n;
  struct sockaddr_in servaddr; struct sockaddr_in client_address;
  unsigned int len;
  char mesg[10]; char reply[1000];
  struct list *end = ((void *)0);
  client_list = &end;
  unsigned long timeout_thread;
  unsigned long ping_client_thread;
  initialize_hue();
  sockfd = socket(2, SOCK_DGRAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = 2;
  servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  servaddr.sin_port = htons(atoi(argv[1]));
  bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  pthread_create(&timeout_thread, ((void *)0), loop_check_timeouts, ((void *)0));
  pthread_create(&ping_client_thread, ((void *)0), loop_ping_clients, ((void *)0));
  while (1) {
    len = sizeof(client_address);
    n = recvfrom(sockfd, mesg, 10, 0, (struct sockaddr *) &client_address, &len);
    process_recvfrom(client_list, mesg, &client_address, &len, &sockfd, reply);
    printf("From main() reply: %s\n", reply);
    sendto(sockfd, reply, strlen(reply), 0, (struct sockaddr *) &client_address, sizeof(client_address));
    printf("-------------------------------------------------------\n");
    mesg[n] = 0;
    printf("Received the following:\n%s", mesg);
    printf("-------------------------------------------------------\n");
  }}
}

variable_types: argc int

variable_types:
argv
char **

variable_types: sockfd int

variable_types: n int

variable_types:
len
unsigned int

variable_types:
mesg
char [10]

variable_types:
reply
char [1000]

variable_types: struct list { struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *player; struct list *next; } *

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct list { struct player_struct *player; struct list *next; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }

user_defined_types: struct player_struct { struct pixel_struct *pixel; struct sockaddr_in *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; }


decompiled_code:
int __fastcall move_player(__int64 *a1, char a2)
{
__int64 v2; // rbp
int result; // eax
_WORD *v4; // rdx
_WORD *v5; // rdx

v2 = *a1;
result = printf(
"\tOld X: %.2hu\n\tOld Y: %.2hu\n",
**(unsigned __int16 **)(*a1 + 24),
**(unsigned __int16 **)(*a1 + 32));
if ( a2 == 78 )
{
v4 = *(_WORD **)(v2 + 32);
result = (unsigned __int16)*v4;
if ( (_WORD)result )
goto LABEL_5;
return result;
}
if ( a2 == 83 )
{
v5 = *(_WORD **)(v2 + 32);
result = (unsigned __int16)*v5;
if ( (unsigned __int16)result > 0x30u )
return result;
goto LABEL_13;
}
if ( a2 != 87 )
{
if ( a2 != 69 )
return result;
v5 = *(_WORD **)(v2 + 24);
result = (unsigned __int16)*v5;
if ( (unsigned __int16)result > 0x30u )
return result;
LABEL_13:
*v5 = result + 1;
return printf(
"\tNew X: %.2hu\n\tNew Y: %.2hu\n",
**(unsigned __int16 **)(v2 + 24),
**(unsigned __int16 **)(v2 + 32));
}
v4 = *(_WORD **)(v2 + 24);
result = (unsigned __int16)*v4;
if ( (_WORD)result )
{
LABEL_5:
*v4 = result - 1;
result = printf(
"\tNew X: %.2hu\n\tNew Y: %.2hu\n",
**(unsigned __int16 **)(v2 + 24),
**(unsigned __int16 **)(v2 + 32));
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 *a1, char a2)
{
__int64 v2; // rbp
int result; // eax
_WORD *v4; // rdx
_WORD *v5; // rdx

v2 = *a1;
result = printf(
"\tOld X: %.2hu\n\tOld Y: %.2hu\n",
**(unsigned __int16 **)(*a1 + 24),
**(unsigned __int16 **)(*a1 + 32));
if ( a2 == 78 )
{
v4 = *(_WORD **)(v2 + 32);
result = (unsigned __int16)*v4;
if ( (_WORD)result )
goto LABEL_5;
return result;
}
if ( a2 == 83 )
{
v5 = *(_WORD **)(v2 + 32);
result = (unsigned __int16)*v5;
if ( (unsigned __int16)result > 0x30u )
return result;
goto LABEL_13;
}
if ( a2 != 87 )
{
if ( a2 != 69 )
return result;
v5 = *(_WORD **)(v2 + 24);
result = (unsigned __int16)*v5;
if ( (unsigned __int16)result > 0x30u )
return result;
LABEL_13:
*v5 = result + 1;
return printf(
"\tNew X: %.2hu\n\tNew Y: %.2hu\n",
**(unsigned __int16 **)(v2 + 24),
**(unsigned __int16 **)(v2 + 32));
}
v4 = *(_WORD **)(v2 + 24);
result = (unsigned __int16)*v4;
if ( (_WORD)result )
{
LABEL_5:
*v4 = result - 1;
result = printf(
"\tNew X: %.2hu\n\tNew Y: %.2hu\n",
**(unsigned __int16 **)(v2 + 24),
**(unsigned __int16 **)(v2 + 32));
}
return result;
}

original_code:
void move_player(player_struct *player, char direction) {
  struct pixel_struct *current_pixel;
  current_pixel = player->pixel;
  printf("\tOld X: %.2hu\n\tOld Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
  if (direction == 'N' && *current_pixel->y > 0) {
    *current_pixel->y -= 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'S' && *current_pixel->y < 49) {
    *current_pixel->y += 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'W' && *current_pixel->x > 0) {
    *current_pixel->x -= 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'E' && *current_pixel->x < 49) {
    *current_pixel->x += 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
}

canonical_original_code:
void move_player(struct player_struct *player, char direction) {
  struct pixel_struct *current_pixel;
  current_pixel = player->pixel;
  printf("\tOld X: %.2hu\n\tOld Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
  if (direction == 'N' && *current_pixel->y > 0) {
    *current_pixel->y -= 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'S' && *current_pixel->y < 49) {
    *current_pixel->y += 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'W' && *current_pixel->x > 0) {
    *current_pixel->x -= 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
  if (direction == 'E' && *current_pixel->x < 49) {
    *current_pixel->x += 1;
    printf("\tNew X: %.2hu\n\tNew Y: %.2hu\n", *(current_pixel->x), *(current_pixel->y));
    return;
  }
}

variable_types: struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *

variable_types: direction char


decompiled_code:
time_t __fastcall update_timeout(__int64 a1)
{
time_t *v1; // rbx
time_t result; // rax

puts("update_timeout called!");
v1 = *(time_t **)(a1 + 24);
result = time(0LL);
*v1 = result;
return result;
}

canonical_decompiled_code:
time_t __fastcall func1(__int64 a1)
{
time_t *v1; // rbx
time_t result; // rax

puts("update_timeout called!");
v1 = *(time_t **)(a1 + 24);
result = time(0LL);
*v1 = result;
return result;
}

original_code:
void update_timeout(player_struct *player) {
  printf("update_timeout called!\n");
  *(player->last_pong) = time(((void *)0));
}

canonical_original_code:
void update_timeout(struct player_struct *player) {
  printf("update_timeout called!\n");
  *(player->last_pong) = time(((void *)0));
}

user_defined_types: struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; }


decompiled_code:
__int64 __fastcall add_player(__int64 a1, const __m128i *a2, _DWORD *a3, unsigned int *a4)
{
unsigned __int16 **v6; // rbx
_QWORD *v7; // rbp
__m128i *v8; // rax
_DWORD *v9; // rax
_QWORD *v10; // rbx
_DWORD *v11; // rax
__int64 v12; // rsi

puts("Adding a player..");
v6 = (unsigned __int16 **)malloc(0x28uLL);
initialize_pixel(v6);
generate_pos(v6[3], v6[4]);
generate_rgb(*v6, v6[1], v6[2]);
printf("\tRed: %.3hu\n\tGreen: %.3hu\n\tBlue: %.3hu\n", **v6, *v6[1], *v6[2]);
printf("\n\tX: %.2hu\n\tY: %.2hu\n", *v6[3], *v6[4]);
v7 = malloc(0x28uLL);
*v7 = v6;
v8 = (__m128i *)malloc(0x10uLL);
v7[1] = v8;
*v8 = _mm_loadu_si128(a2);
v9 = malloc(4uLL);
v7[2] = v9;
*v9 = *a3;
v10 = malloc(8uLL);
v7[3] = v10;
*v10 = time(0LL);
v11 = malloc(4uLL);
v7[4] = v11;
v12 = *a4;
*v11 = v12;
printf("Socket: %d\n", v12);
return insert_list(a1, v7);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const __m128i *a2, _DWORD *a3, unsigned int *a4)
{
unsigned __int16 **v6; // rbx
_QWORD *v7; // rbp
__m128i *v8; // rax
_DWORD *v9; // rax
_QWORD *v10; // rbx
_DWORD *v11; // rax
__int64 v12; // rsi

puts("Adding a player..");
v6 = (unsigned __int16 **)malloc(0x28uLL);
initialize_pixel(v6);
func2(v6[3], v6[4]);
generate_rgb(*v6, v6[1], v6[2]);
printf("\tRed: %.3hu\n\tGreen: %.3hu\n\tBlue: %.3hu\n", **v6, *v6[1], *v6[2]);
printf("\n\tX: %.2hu\n\tY: %.2hu\n", *v6[3], *v6[4]);
v7 = malloc(0x28uLL);
*v7 = v6;
v8 = (__m128i *)malloc(0x10uLL);
v7[1] = v8;
*v8 = _mm_loadu_si128(a2);
v9 = malloc(4uLL);
v7[2] = v9;
*v9 = *a3;
v10 = malloc(8uLL);
v7[3] = v10;
*v10 = time(0LL);
v11 = malloc(4uLL);
v7[4] = v11;
v12 = *a4;
*v11 = v12;
printf("Socket: %d\n", v12);
return insert_list(a1, v7);
}

original_code:
void add_player(list **players, struct sockaddr_in *client_address, socklen_t *client_address_len, int *client_socket) {
  struct pixel_struct *new_pixel;
  player_struct *new_player;
  printf("Adding a player..\n");
  new_pixel = malloc(sizeof(struct pixel_struct));
  initialize_pixel(new_pixel);
  generate_pos(new_pixel->x, new_pixel->y);
  generate_rgb(new_pixel->red, new_pixel->green, new_pixel->blue);
  printf("\tRed: %.3hu\n\tGreen: %.3hu\n\tBlue: %.3hu\n", *(new_pixel->red), *(new_pixel->green), *(new_pixel->blue));
  printf("\n\tX: %.2hu\n\tY: %.2hu\n", *(new_pixel->x), *(new_pixel->y));
  new_player = malloc(sizeof(player_struct));
  new_player->pixel = new_pixel;
  new_player->clientaddr = malloc(sizeof(struct sockaddr_in));
  *(new_player->clientaddr) = *client_address;
  new_player->clientaddr_len = malloc(sizeof(socklen_t));
  *(new_player->clientaddr_len) = *client_address_len;
  new_player->last_pong = malloc(sizeof(time_t));
  *(new_player->last_pong) = time(((void *)0));
  new_player->socket = malloc(sizeof(int));
  *(new_player->socket) = *client_socket;
  printf("Socket: %d\n", *new_player->socket);
  insert_list(players, new_player);
}

canonical_original_code:
void add_player(struct list **players, struct sockaddr_in *client_address, unsigned int *client_address_len, int *client_socket) {
  struct pixel_struct *new_pixel;
  struct player_struct *new_player;
  printf("Adding a player..\n");
  new_pixel = malloc(sizeof(struct pixel_struct));
  initialize_pixel(new_pixel);
  generate_pos(new_pixel->x, new_pixel->y);
  generate_rgb(new_pixel->red, new_pixel->green, new_pixel->blue);
  printf("\tRed: %.3hu\n\tGreen: %.3hu\n\tBlue: %.3hu\n", *(new_pixel->red), *(new_pixel->green), *(new_pixel->blue));
  printf("\n\tX: %.2hu\n\tY: %.2hu\n", *(new_pixel->x), *(new_pixel->y));
  new_player = malloc(sizeof(player_struct));
  new_player->pixel = new_pixel;
  new_player->clientaddr = malloc(sizeof(struct sockaddr_in));
  *(new_player->clientaddr) = *client_address;
  new_player->clientaddr_len = malloc(sizeof(socklen_t));
  *(new_player->clientaddr_len) = *client_address_len;
  new_player->last_pong = malloc(sizeof(time_t));
  *(new_player->last_pong) = time(((void *)0));
  new_player->socket = malloc(sizeof(int));
  *(new_player->socket) = *client_socket;
  printf("Socket: %d\n", *new_player->socket);
  insert_list(players, new_player);
}

variable_types: struct list { struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *player; struct list *next; } **


decompiled_code:
void __fastcall check_timeouts(__int64 **a1, _QWORD *a2)
{
__int64 v2; // r12
time_t v3; // rax
double v4; // r13
__int64 *i; // rbx
__int64 v6; // r13
size_t v7; // rax
char v8[104]; // [rsp-68h] [rbp-68h] BYREF

if ( a2 )
{
v2 = a2[1];
v3 = time(0LL);
v4 = difftime(v3, **(_QWORD **)(*a2 + 24LL));
printf("Time since last PONG by client: %.2f\n", v4);
if ( v4 > 20.0 )
{
puts("deleting player from list, timed out");
removal_message(*a2, v8);
delete_list(a1, *a2);
for ( i = *a1; i; i = (__int64 *)i[1] )
{
v6 = *i;
v7 = strlen(v8);
sendto(**(_DWORD **)(v6 + 32), v8, v7, 0, *(const struct sockaddr **)(v6 + 8), 0x10u);
}
}
check_timeouts(a1, v2);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 **a1, _QWORD *a2)
{
__int64 v2; // r12
time_t v3; // rax
double v4; // r13
__int64 *i; // rbx
__int64 v6; // r13
size_t v7; // rax
char v8[104]; // [rsp-68h] [rbp-68h] BYREF

if ( a2 )
{
v2 = a2[1];
v3 = time(0LL);
v4 = difftime(v3, **(_QWORD **)(*a2 + 24LL));
printf("Time since last PONG by client: %.2f\n", v4);
if ( v4 > 20.0 )
{
puts("deleting player from list, timed out");
removal_message(*a2, v8);
delete_list(a1, *a2);
for ( i = *a1; i; i = (__int64 *)i[1] )
{
v6 = *i;
v7 = strlen(v8);
sendto(**(_DWORD **)(v6 + 32), v8, v7, 0, *(const struct sockaddr **)(v6 + 8), 0x10u);
}
}
func0(a1, v2);
}
}

original_code:
void check_timeouts(list **original_players, list *players) {
  time_t current_time;
  double delta;
  list *next_ptr, *p;
  int timeout = 20;
  char reply_buffer[50];
  if (players == ((void *)0))
    return;
  next_ptr = players->next;
  current_time = time(((void *)0));
  delta = difftime(current_time, *((players->player)->last_pong));
  printf("Time since last PONG by client: %.2f\n", delta);
  if (delta > timeout) {
    printf("deleting player from list, timed out\n");
    removal_message(players->player, reply_buffer);
    delete_list(original_players, players->player);
    p = *original_players;
    while (p != ((void *)0)) {
      sendto(*p->player->socket, reply_buffer, strlen(reply_buffer), 0, (struct sockaddr *) p->player->clientaddr, sizeof(*p->player->clientaddr));
      p = p->next;
    }
  }
  check_timeouts(original_players, next_ptr);
}

canonical_original_code:
void check_timeouts(struct list **original_players, struct list *players) {
  long current_time;
  double delta;
  struct list *next_ptr; struct list *p;
  int timeout = 20;
  char reply_buffer[50];
  if (players == ((void *)0))
    return;
  next_ptr = players->next;
  current_time = time(((void *)0));
  delta = difftime(current_time, *((players->player)->last_pong));
  printf("Time since last PONG by client: %.2f\n", delta);
  if (delta > timeout) {
    printf("deleting player from list, timed out\n");
    removal_message(players->player, reply_buffer);
    delete_list(original_players, players->player);
    p = *original_players;
    while (p != ((void *)0)) {
      sendto(*p->player->socket, reply_buffer, strlen(reply_buffer), 0, (struct sockaddr *) p->player->clientaddr, sizeof(*p->player->clientaddr));
      p = p->next;
    }
  }
  check_timeouts(original_players, next_ptr);
}

variable_types:
players
struct list *

variable_types: delta double

variable_types: timeout int


decompiled_code:
void __noreturn loop_check_timeouts()
{
while ( 1 )
{
sleep(0x1Eu);
puts("Checking for timeouts...");
check_timeouts(client_list, (_QWORD *)*client_list);
}
}

canonical_decompiled_code:
void __noreturn func1()
{
while ( 1 )
{
sleep(0x1Eu);
puts("Checking for timeouts...");
func0(client_list, (_QWORD *)*client_list);
}
}

original_code:
void *loop_check_timeouts(void *arg) {
  while (1) {
    sleep(30);
    printf("Checking for timeouts...\n");
    check_timeouts(client_list, *client_list);
  }
}

variable_types:
arg
void *

return_type: void *


decompiled_code:
_BOOL8 __fastcall pong_response(const void *a1)
{
return memcmp("PONG", a1, 4uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(const void *a1)
{
return memcmp("PONG", a1, 4uLL) == 0;
}

original_code:
int pong_response(char message[]) {
  return 0 == strncmp("PONG", message, 4);
}

variable_types:
message
char []


decompiled_code:
_BOOL8 __fastcall move_request(const void *a1)
{
bool v2; // zf
_BOOL4 v3; // ecx

v2 = memcmp("MOVEN", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
{
v2 = memcmp("MOVES", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
{
v2 = memcmp("MOVEW", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
v3 = memcmp("MOVEE", a1, 5uLL) == 0;
}
}
return v3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const void *a1)
{
bool v2; // zf
_BOOL4 v3; // ecx

v2 = memcmp("MOVEN", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
{
v2 = memcmp("MOVES", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
{
v2 = memcmp("MOVEW", a1, 5uLL) == 0;
v3 = 1;
if ( !v2 )
v3 = memcmp("MOVEE", a1, 5uLL) == 0;
}
}
return v3;
}

original_code:
int move_request(char message[]) {
  return (0 == strncmp("MOVEN", message, 5) ||
          0 == strncmp("MOVES", message, 5) ||
          0 == strncmp("MOVEW", message, 5) ||
          0 == strncmp("MOVEE", message, 5));
}


decompiled_code:
_BOOL8 __fastcall client_connecting(const void *a1)
{
return memcmp("HELLO", a1, 5uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const void *a1)
{
return memcmp("HELLO", a1, 5uLL) == 0;
}

original_code:
int client_connecting(char message[]) {
  return 0 == strncmp("HELLO", message, 5);
}


decompiled_code:
__int64 __fastcall check_address(_QWORD *a1, __int64 a2, _DWORD *a3, _QWORD *a4)
{
__int64 result; // rax
char *v7; // r14
char *v8; // rax
__int64 v9; // r15
__int16 v10; // ax

result = 0LL;
if ( a1 )
{
v7 = inet_ntoa(*(struct in_addr *)(a2 + 4));
v8 = inet_ntoa(*(struct in_addr *)(*(_QWORD *)(*a1 + 8LL) + 4LL));
if ( !strcmp(v8, v7)
&& (v9 = *a1, v10 = _bswap_16(*(_WORD *)(*(_QWORD *)(*a1 + 8LL) + 2LL)), v10 == _bswap_16(*(_WORD *)(a2 + 2)))
&& **(_DWORD **)(v9 + 16) == *a3 )
{
*a4 = v9;
result = 1LL;
}
else
{
result = check_address(a1[1], a2, a3, a4);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, __int64 a2, _DWORD *a3, _QWORD *a4)
{
__int64 result; // rax
char *v7; // r14
char *v8; // rax
__int64 v9; // r15
__int16 v10; // ax

result = 0LL;
if ( a1 )
{
v7 = inet_ntoa(*(struct in_addr *)(a2 + 4));
v8 = inet_ntoa(*(struct in_addr *)(*(_QWORD *)(*a1 + 8LL) + 4LL));
if ( !strcmp(v8, v7)
&& (v9 = *a1, v10 = func0(*(_WORD *)(*(_QWORD *)(*a1 + 8LL) + 2LL)), v10 == func0(*(_WORD *)(a2 + 2)))
&& **(_DWORD **)(v9 + 16) == *a3 )
{
*a4 = v9;
result = 1LL;
}
else
{
result = func4(a1[1], a2, a3, a4);
}
}
return result;
}

original_code:
int check_address(list *players, struct sockaddr_in *client_addr, socklen_t *client_addr_len, player_struct **player) {
  if (players == ((void *)0))
    return 0;
  if (!strcmp(inet_ntoa(((players->player)->clientaddr)->sin_addr), inet_ntoa(client_addr->sin_addr)) &&
      ntohs(((players->player)->clientaddr)->sin_port) == ntohs(client_addr->sin_port) &&
      *((players->player)->clientaddr_len) == *client_addr_len) {
    *player = players->player;
    return 1;
  }
  return check_address(players->next, client_addr, client_addr_len, player);
}

canonical_original_code:
int check_address(struct list *players, struct sockaddr_in *client_addr, unsigned int *client_addr_len, struct player_struct **player) {
  if (players == ((void *)0))
    return 0;
  if (!strcmp(inet_ntoa(((players->player)->clientaddr)->sin_addr), inet_ntoa(client_addr->sin_addr)) &&
      ntohs(((players->player)->clientaddr)->sin_port) == ntohs(client_addr->sin_port) &&
      *((players->player)->clientaddr_len) == *client_addr_len) {
    *player = players->player;
    return 1;
  }
  return check_address(players->next, client_addr, client_addr_len, player);
}

variable_types:
player
struct player_struct **


decompiled_code:
_BOOL8 __fastcall process_recvfrom(__int64 **a1, char *a2, __int64 a3, _DWORD *a4, __int64 a5, __int64 a6)
{
_BOOL4 v10; // ebx
unsigned __int16 v11; // r13
char *v12; // rax
__int64 v14[8]; // [rsp+8h] [rbp-40h] BYREF

if ( pong_response(a2) )
{
puts("Caught a pong!");
if ( (unsigned int)check_address(*a1, a3, a4, v14) )
{
update_timeout(v14[0]);
pong_message(a6);
return 1;
}
LABEL_8:
error_message(a6);
return 1;
}
if ( move_request(a2) )
{
puts("Caught a move request!");
if ( (unsigned int)check_address(*a1, a3, a4, v14) )
{
move_player(v14[0], (unsigned int)a2[4]);
move_message((unsigned int)a2[4], a6);
send_updates();
puts("post grid_message");
return 1;
}
goto LABEL_8;
}
v10 = client_connecting(a2);
if ( v10 )
{
puts("Caught a connection request!");
if ( (unsigned int)check_address(*a1, a3, a4, v14) )
{
error_message(a6);
}
else
{
add_player(a1, a3, a4, a5);
v14[0] = **a1;
welcome_message(v14[0], a6);
send_updates();
}
v10 = 1;
}
else
{
v11 = _bswap_16(*(_WORD *)(a3 + 2));
v12 = inet_ntoa(*(struct in_addr *)(a3 + 4));
printf("[Error]: Unresolved udp message from %s:%d.\n", v12, v11);
error_message(a6);
}
return v10;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(__int64 **a1, char *a2, __int64 a3, _DWORD *a4, __int64 a5, __int64 a6)
{
_BOOL4 v10; // ebx
unsigned __int16 v11; // r13
char *v12; // rax
__int64 v14[8]; // [rsp+8h] [rbp-40h] BYREF

if ( func1(a2) )
{
puts("Caught a pong!");
if ( (unsigned int)func4(*a1, a3, a4, v14) )
{
update_timeout(v14[0]);
pong_message(a6);
return 1;
}
LABEL_8:
error_message(a6);
return 1;
}
if ( func2(a2) )
{
puts("Caught a move request!");
if ( (unsigned int)func4(*a1, a3, a4, v14) )
{
move_player(v14[0], (unsigned int)a2[4]);
move_message((unsigned int)a2[4], a6);
send_updates();
puts("post grid_message");
return 1;
}
goto LABEL_8;
}
v10 = func3(a2);
if ( v10 )
{
puts("Caught a connection request!");
if ( (unsigned int)func4(*a1, a3, a4, v14) )
{
error_message(a6);
}
else
{
add_player(a1, a3, a4, a5);
v14[0] = **a1;
welcome_message(v14[0], a6);
send_updates();
}
v10 = 1;
}
else
{
v11 = func0(*(_WORD *)(a3 + 2));
v12 = inet_ntoa(*(struct in_addr *)(a3 + 4));
printf("[Error]: Unresolved udp message from %s:%d.\n", v12, v11);
error_message(a6);
}
return v10;
}

original_code:
int process_recvfrom( list **players,
                      char message[],
                      struct sockaddr_in *client_addr_ptr,
                      socklen_t *len_ptr,
                      int *socket_ptr,
                      char reply[]) {
  player_struct *client;
  if (pong_response(message)) {
    printf("Caught a pong!\n");
    if (check_address(*players, client_addr_ptr, len_ptr, &client)) {
      update_timeout(client);
      pong_message(reply);
    } else
        error_message(reply);
    return 1;
  }
  if (move_request(message)) {
    printf("Caught a move request!\n");
    if (check_address(*players, client_addr_ptr, len_ptr, &client)) {
      move_player(client, message[4]);
      move_message(message[4], reply);
      send_updates();
      printf("post grid_message\n");
    } else
        error_message(reply);
    return 1;
  }
  if (client_connecting(message)) {
    printf("Caught a connection request!\n");
    if (!check_address(*players, client_addr_ptr, len_ptr, &client)) {
      add_player(players, client_addr_ptr, len_ptr, socket_ptr);
      client = (*players)->player;
      welcome_message(client, reply);
      send_updates();
    } else
        error_message(reply);
    return 1;
  }
  printf("[Error]: Unresolved udp message from %s:%d.\n", inet_ntoa(client_addr_ptr->sin_addr), ntohs(client_addr_ptr->sin_port));
  error_message(reply);
  return 0;
}

canonical_original_code:
int process_recvfrom(struct list **players, char message[], struct sockaddr_in *client_addr_ptr, unsigned int *len_ptr, int *socket_ptr, char reply[]) {
  struct player_struct *client;
  if (pong_response(message)) {
    printf("Caught a pong!\n");
    if (check_address(*players, client_addr_ptr, len_ptr, &client)) {
      update_timeout(client);
      pong_message(reply);
    } else
        error_message(reply);
    return 1;
  }
  if (move_request(message)) {
    printf("Caught a move request!\n");
    if (check_address(*players, client_addr_ptr, len_ptr, &client)) {
      move_player(client, message[4]);
      move_message(message[4], reply);
      send_updates();
      printf("post grid_message\n");
    } else
        error_message(reply);
    return 1;
  }
  if (client_connecting(message)) {
    printf("Caught a connection request!\n");
    if (!check_address(*players, client_addr_ptr, len_ptr, &client)) {
      add_player(players, client_addr_ptr, len_ptr, socket_ptr);
      client = (*players)->player;
      welcome_message(client, reply);
      send_updates();
    } else
        error_message(reply);
    return 1;
  }
  printf("[Error]: Unresolved udp message from %s:%d.\n", inet_ntoa(client_addr_ptr->sin_addr), ntohs(client_addr_ptr->sin_port));
  error_message(reply);
  return 0;
}

variable_types:
reply
char []


decompiled_code:
int __fastcall grid_message(__int64 *a1)
{
__int64 *v1; // rbx
unsigned __int16 ****v2; // rbp
unsigned __int16 **v3; // rax
__int64 v4; // rbp
size_t v5; // rax
int result; // eax
char s[1008]; // [rsp+0h] [rbp-7F8h] BYREF
char dest[1032]; // [rsp+3F0h] [rbp-408h] BYREF

dest[0] = 0;
if ( !a1 )
return snprintf(s, 0x3E8uLL, "%s", dest);
v1 = a1;
v2 = (unsigned __int16 ****)a1;
do
{
v3 = **v2;
snprintf(s, 0x28uLL, "#%hu,%hu,%hu,%hu,%hu$\n", *v3[3], *v3[4], **v3, *v3[1], *v3[2]);
strcat(dest, s);
v2 = (unsigned __int16 ****)v2[1];
}
while ( v2 );
snprintf(s, 0x3E8uLL, "%s", dest);
do
{
v4 = *v1;
v5 = strlen(s);
result = sendto(**(_DWORD **)(v4 + 32), s, v5, 0, *(const struct sockaddr **)(v4 + 8), 0x10u);
v1 = (__int64 *)v1[1];
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 *a1)
{
__int64 *v1; // rbx
unsigned __int16 ****v2; // rbp
unsigned __int16 **v3; // rax
__int64 v4; // rbp
size_t v5; // rax
int result; // eax
char s[1008]; // [rsp+0h] [rbp-7F8h] BYREF
char dest[1032]; // [rsp+3F0h] [rbp-408h] BYREF

dest[0] = 0;
if ( !a1 )
return snprintf(s, 0x3E8uLL, "%s", dest);
v1 = a1;
v2 = (unsigned __int16 ****)a1;
do
{
v3 = **v2;
snprintf(s, 0x28uLL, "#%hu,%hu,%hu,%hu,%hu$\n", *v3[3], *v3[4], **v3, *v3[1], *v3[2]);
strcat(dest, s);
v2 = (unsigned __int16 ****)v2[1];
}
while ( v2 );
snprintf(s, 0x3E8uLL, "%s", dest);
do
{
v4 = *v1;
v5 = strlen(s);
result = sendto(**(_DWORD **)(v4 + 32), s, v5, 0, *(const struct sockaddr **)(v4 + 8), 0x10u);
v1 = (__int64 *)v1[1];
}
while ( v1 );
return result;
}

original_code:
void grid_message(list *players) {
  unsigned short x, y, r, g, b;
  list *p = players;
  char temp[1000];
  char str_ptr[1000];
  strcpy(temp, "");
  while (p != ((void *)0)) {
    x = *p->player->pixel->x;
    y = *p->player->pixel->y;
    r = *p->player->pixel->red;
    g = *p->player->pixel->green;
    b = *p->player->pixel->blue;
    snprintf(str_ptr, 40, "#%hu,%hu,%hu,%hu,%hu$\n", x, y, r, g, b);
    strcat(temp, str_ptr);
    p = p->next;
  }
  snprintf(str_ptr, 1000, "%s", temp);
  p = players;
  while (p != ((void *)0)) {
    sendto(*p->player->socket, str_ptr, strlen(str_ptr), 0, (struct sockaddr *) p->player->clientaddr, sizeof(*p->player->clientaddr));
    p = p->next;
  }
}

canonical_original_code:
void grid_message(struct list *players) {
  unsigned short x; unsigned short y; unsigned short r; unsigned short g; unsigned short b;
  struct list *p = players;
  char temp[1000];
  char str_ptr[1000];
  strcpy(temp, "");
  while (p != ((void *)0)) {
    x = *p->player->pixel->x;
    y = *p->player->pixel->y;
    r = *p->player->pixel->red;
    g = *p->player->pixel->green;
    b = *p->player->pixel->blue;
    snprintf(str_ptr, 40, "#%hu,%hu,%hu,%hu,%hu$\n", x, y, r, g, b);
    strcat(temp, str_ptr);
    p = p->next;
  }
  snprintf(str_ptr, 1000, "%s", temp);
  p = players;
  while (p != ((void *)0)) {
    sendto(*p->player->socket, str_ptr, strlen(str_ptr), 0, (struct sockaddr *) p->player->clientaddr, sizeof(*p->player->clientaddr));
    p = p->next;
  }
}

variable_types:
x
unsigned short

variable_types:
y
unsigned short

variable_types:
r
unsigned short

variable_types:
g
unsigned short

variable_types:
b
unsigned short

variable_types:
temp
char [1000]


decompiled_code:
void __noreturn loop_ping_clients()
{
while ( 1 )
{
sleep(0xFu);
puts("Sending out pings...");
ping_clients((_QWORD *)*client_list);
}
}

canonical_decompiled_code:
void __noreturn func2()
{
while ( 1 )
{
sleep(0xFu);
puts("Sending out pings...");
func0((_QWORD *)*client_list);
}
}

original_code:
void *loop_ping_clients(void *arg) {
  while (1) {
    sleep(15);
    printf("Sending out pings...\n");
    ping_clients(*client_list);
  }
}


decompiled_code:
int send_updates()
{
return grid_message((__int64 *)*client_list);
}

canonical_decompiled_code:
int func3()
{
return func1((__int64 *)*client_list);
}

original_code:
void send_updates(void) {
  grid_message(*client_list);
}

canonical_original_code:
void send_updates() {
  grid_message(*client_list);
}


decompiled_code:
void __fastcall free_pixel(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}

original_code:
void free_pixel(struct pixel_struct *pixel) {
  free(pixel->red);
  free(pixel->green);
  free(pixel->blue);
  free(pixel->x);
  free(pixel->y);
  free(pixel);
}

variable_types: struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *


decompiled_code:
void __fastcall free_player(void *ptr)
{
free_pixel(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
func0(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
free(ptr);
}

original_code:
void free_player(player_struct *player) {
  free_pixel(player->pixel);
  free(player->clientaddr);
  free(player->clientaddr_len);
  free(player->last_pong);
  free(player->socket);
  free(player);
}

canonical_original_code:
void free_player(struct player_struct *player) {
  free_pixel(player->pixel);
  free(player->clientaddr);
  free(player->clientaddr_len);
  free(player->last_pong);
  free(player->socket);
  free(player);
}

variable_types: struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *


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

*a1 = malloc(2uLL);
a1[1] = malloc(2uLL);
a1[2] = malloc(2uLL);
a1[3] = malloc(2uLL);
result = malloc(2uLL);
a1[4] = result;
return result;
}

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

*a1 = malloc(2uLL);
a1[1] = malloc(2uLL);
a1[2] = malloc(2uLL);
a1[3] = malloc(2uLL);
result = malloc(2uLL);
a1[4] = result;
return result;
}

original_code:
void initialize_pixel(struct pixel_struct *pixel) {
  pixel->red = malloc(sizeof(unsigned short));
  pixel->green = malloc(sizeof(unsigned short));
  pixel->blue = malloc(sizeof(unsigned short));
  pixel->x = malloc(sizeof(unsigned short));
  pixel->y = malloc(sizeof(unsigned short));
}


decompiled_code:
void __fastcall error_message(char *a1)
{
strcpy(a1, "ERROR\n");
}

canonical_decompiled_code:
void __fastcall func0(char *a1)
{
strcpy(a1, "ERROR\n");
}

original_code:
void error_message(char *str_ptr) {
  snprintf(str_ptr, 40, "ERROR\n");
}


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

if ( a1 == 83 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE SOUTH\n");
}
else if ( a1 > 83 )
{
if ( a1 == 87 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE WEST\n");
}
}
else if ( a1 == 69 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE EAST\n");
}
else if ( a1 == 78 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE NORTH\n");
}
return result;
}

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

if ( a1 == 83 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE SOUTH\n");
}
else if ( a1 > 83 )
{
if ( a1 == 87 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE WEST\n");
}
}
else if ( a1 == 69 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE EAST\n");
}
else if ( a1 == 78 )
{
result = 0x45564F4D204B4341LL;
strcpy(a2, "ACK MOVE NORTH\n");
}
return result;
}

original_code:
void move_message(char direction, char *str_ptr) {
  switch (direction) {
    case 'N':
      snprintf(str_ptr, 40, "ACK MOVE NORTH\n");
      break;
    case 'S':
      snprintf(str_ptr, 40, "ACK MOVE SOUTH\n");
      break;
    case 'E':
      snprintf(str_ptr, 40, "ACK MOVE EAST\n");
      break;
    case 'W':
      snprintf(str_ptr, 40, "ACK MOVE WEST\n");
      break;
  }
}

variable_types: direction char


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

result = 0x4B434120474E4F50LL;
strcpy(a1, "PONG ACK\n");
return result;
}

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

result = 0x4B434120474E4F50LL;
strcpy(a1, "PONG ACK\n");
return result;
}

original_code:
void pong_message(char *str_ptr) {
  snprintf(str_ptr, 40, "PONG ACK\n");
}


decompiled_code:
int __fastcall removal_message(unsigned __int16 ***a1, char *a2)
{
unsigned __int16 **v2; // rax

v2 = *a1;
return snprintf(a2, 0x28uLL, "REMOVING: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", *v2[3], *v2[4], **v2, *v2[1], *v2[2]);
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int16 ***a1, char *a2)
{
unsigned __int16 **v2; // rax

v2 = *a1;
return snprintf(a2, 0x28uLL, "REMOVING: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", *v2[3], *v2[4], **v2, *v2[1], *v2[2]);
}

original_code:
void removal_message(player_struct *player, char *str_ptr) {
  unsigned short x, y, r, g, b;
  x = *player->pixel->x;
  y = *player->pixel->y;
  r = *player->pixel->red;
  g = *player->pixel->green;
  b = *player->pixel->blue;
  snprintf(str_ptr, 40, "REMOVING: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", x, y, r, g, b);
  return ;
}

canonical_original_code:
void removal_message(struct player_struct *player, char *str_ptr) {
  unsigned short x; unsigned short y; unsigned short r; unsigned short g; unsigned short b;
  x = *player->pixel->x;
  y = *player->pixel->y;
  r = *player->pixel->red;
  g = *player->pixel->green;
  b = *player->pixel->blue;
  snprintf(str_ptr, 40, "REMOVING: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", x, y, r, g, b);
  return ;
}


decompiled_code:
int __fastcall welcome_message(unsigned __int16 ***a1, char *a2)
{
unsigned __int16 **v2; // rax

v2 = *a1;
return snprintf(a2, 0x28uLL, "WELCOME: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", *v2[3], *v2[4], **v2, *v2[1], *v2[2]);
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 ***a1, char *a2)
{
unsigned __int16 **v2; // rax

v2 = *a1;
return snprintf(a2, 0x28uLL, "WELCOME: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", *v2[3], *v2[4], **v2, *v2[1], *v2[2]);
}

original_code:
void welcome_message(player_struct *player, char *str_ptr) {
  unsigned short x, y, r, g, b;
  x = *player->pixel->x;
  y = *player->pixel->y;
  r = *player->pixel->red;
  g = *player->pixel->green;
  b = *player->pixel->blue;
  snprintf(str_ptr, 40, "WELCOME: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", x, y, r, g, b);
  return ;
}

canonical_original_code:
void welcome_message(struct player_struct *player, char *str_ptr) {
  unsigned short x; unsigned short y; unsigned short r; unsigned short g; unsigned short b;
  x = *player->pixel->x;
  y = *player->pixel->y;
  r = *player->pixel->red;
  g = *player->pixel->green;
  b = *player->pixel->blue;
  snprintf(str_ptr, 40, "WELCOME: x:%hu,y:%hu,r:%hu,g:%hu,b:%hu\n", x, y, r, g, b);
  return ;
}


decompiled_code:
void __fastcall delete_list(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // r13
__int64 v3; // rax
_QWORD *v4; // rbx
_QWORD *v5; // rax

v2 = *a1;
v3 = search_list(*a1, a2);
if ( v3 )
{
v4 = (_QWORD *)v3;
v5 = predecessor_list(v2, a2);
if ( v5 )
v5[1] = v4[1];
else
*a1 = (_QWORD *)v4[1];
free_player(*v4);
free(v4);
}
}

canonical_decompiled_code:
void __fastcall func1(_QWORD **a1, __int64 a2)
{
_QWORD *v2; // r13
__int64 v3; // rax
_QWORD *v4; // rbx
_QWORD *v5; // rax

v2 = *a1;
v3 = func0(*a1, a2);
if ( v3 )
{
v4 = (_QWORD *)v3;
v5 = predecessor_list(v2, a2);
if ( v5 )
v5[1] = v4[1];
else
*a1 = (_QWORD *)v4[1];
free_player(*v4);
free(v4);
}
}

original_code:
void delete_list(list **l, player_struct *x) {
  list *p, *pred;
  list *search_list(), *predecessor_list();
  p = search_list(*l, x);
  if (p != ((void *)0)) {
    pred = predecessor_list(*l, x);
    if (pred == ((void *)0))
      *l = p->next;
    else
      pred->next = p->next;
    free_player(p->player);
    free(p);
  }
}

canonical_original_code:
void delete_list(struct list **l, struct player_struct *x) {
  struct list *p; struct list *pred;
  struct list *search_list(); struct list *predecessor_list();
  p = search_list(*l, x);
  if (p != ((void *)0)) {
    pred = predecessor_list(*l, x);
    if (pred == ((void *)0))
      *l = p->next;
    else
      pred->next = p->next;
    free_player(p->player);
    free(p);
  }
}

variable_types: struct list { struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *player; struct list *next; } **

variable_types:
x
struct player_struct *

variable_types: struct list { struct player_struct { struct pixel_struct { unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *x; unsigned short *y; } *pixel; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *clientaddr; unsigned int *clientaddr_len; long *last_pong; int *socket; } *player; struct list *next; } *()


writeEdgeDeletedPlanarCodeChar

repo: nvcleemp/graphtools

decompiled_code:
int writeEdgeDeletedPlanarCodeChar()
{
int result; // eax
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rbp

result = fputc(nv, stdout);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v3 = firstedge[v1];
v2 = v3;
do
{
if ( *(_DWORD *)(v2 + 40) < markvalue )
fputc(*(_DWORD *)(v2 + 4) + 1, stdout);
v2 = *(_QWORD *)(v2 + 24);
}
while ( v3 != v2 );
result = fputc(0, stdout);
++v1;
}
while ( nv > (int)v1 );
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rbp

result = fputc(nv, stdout);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v3 = firstedge[v1];
v2 = v3;
do
{
if ( *(_DWORD *)(v2 + 40) < markvalue )
fputc(*(_DWORD *)(v2 + 4) + 1, stdout);
v2 = *(_QWORD *)(v2 + 24);
}
while ( v3 != v2 );
result = fputc(0, stdout);
++v1;
}
while ( nv > (int)v1 );
}
return result;
}

original_code:
void writeEdgeDeletedPlanarCodeChar(){
    int i;
    EDGE *e, *elast;
    fputc(nv, stdout);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            if(!((e)->mark >= markvalue)){
                fputc(e->end + 1, stdout);
            }
            e = e->next;
        } while (e != elast);
        fputc(0, stdout);
    }
}

canonical_original_code:
void writeEdgeDeletedPlanarCodeChar(){
    int i;
    struct e *e; struct e *elast;
    fputc(nv, stdout);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            if(!((e)->mark >= markvalue)){
                fputc(e->end + 1, stdout);
            }
            e = e->next;
        } while (e != elast);
        fputc(0, stdout);
    }
}

variable_types: i int

variable_types: struct e { int start; int end; int rightface; struct e *prev; struct e *next; struct e *inverse; int mark; } *


writeShort

decompiled_code:
size_t __fastcall writeShort(__int16 a1)
{
size_t result; // rax
__int16 ptr[6]; // [rsp+Ch] [rbp-Ch] BYREF

ptr[0] = a1;
result = fwrite(ptr, 2uLL, 1uLL, stdout);
if ( result != 1 )
{
fwrite("fwrite() failed -- exiting!\n", 1uLL, 0x1CuLL, stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func8(__int16 a1)
{
size_t result; // rax
__int16 ptr[6]; // [rsp+Ch] [rbp-Ch] BYREF

ptr[0] = a1;
result = fwrite(ptr, 2uLL, 1uLL, stdout);
if ( result != 1 )
{
fwrite("fwrite() failed -- exiting!\n", 1uLL, 0x1CuLL, stderr);
exit(-1);
}
return result;
}

original_code:
void writeShort(unsigned short value){
    if (fwrite(&value, sizeof (unsigned short), 1, stdout) != 1) {
        fprintf(stderr, "fwrite() failed -- exiting!\n");
        exit(-1);
    }
}

variable_types:
value
unsigned short


writeEdgeDeletedPlanarCodeShort

decompiled_code:
size_t writeEdgeDeletedPlanarCodeShort()
{
size_t result; // rax
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rbp

fputc(0, stdout);
result = writeShort(nv);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v3 = firstedge[v1];
v2 = v3;
do
{
if ( *(_DWORD *)(v2 + 40) < markvalue )
writeShort(*(_WORD *)(v2 + 4) + 1);
v2 = *(_QWORD *)(v2 + 24);
}
while ( v3 != v2 );
result = writeShort(0);
++v1;
}
while ( nv > (int)v1 );
}
return result;
}

canonical_decompiled_code:
size_t func9()
{
size_t result; // rax
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rbp

fputc(0, stdout);
result = func8(nv);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v3 = firstedge[v1];
v2 = v3;
do
{
if ( *(_DWORD *)(v2 + 40) < markvalue )
func8(*(_WORD *)(v2 + 4) + 1);
v2 = *(_QWORD *)(v2 + 24);
}
while ( v3 != v2 );
result = func8(0);
++v1;
}
while ( nv > (int)v1 );
}
return result;
}

original_code:
void writeEdgeDeletedPlanarCodeShort(){
    int i;
    EDGE *e, *elast;
    fputc(0, stdout);
    writeShort(nv);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            if(!((e)->mark >= markvalue)){
                writeShort(e->end + 1);
            }
            e = e->next;
        } while (e != elast);
        writeShort(0);
    }
}

canonical_original_code:
void writeEdgeDeletedPlanarCodeShort(){
    int i;
    struct e *e; struct e *elast;
    fputc(0, stdout);
    writeShort(nv);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            if(!((e)->mark >= markvalue)){
                writeShort(e->end + 1);
            }
            e = e->next;
        } while (e != elast);
        writeShort(0);
    }
}

user_defined_types: struct e { int start; int end; int rightface; struct e *prev; struct e *next; struct e *inverse; int mark; }


writeEdgeDeletedPlanarCode

decompiled_code:
int writeEdgeDeletedPlanarCode()
{
if ( first_2 )
{
first_2 = 0;
fwrite(">>planar_code<<", 1uLL, 0xFuLL, stdout);
}
if ( nv <= 254 )
return writeEdgeDeletedPlanarCodeChar();
if ( nv > 65534 )
{
fwrite("Graphs of that size are currently not supported -- exiting!\n", 1uLL, 0x3CuLL, stderr);
exit(-1);
}
return writeEdgeDeletedPlanarCodeShort();
}

canonical_decompiled_code:
int func10()
{
if ( first_2 )
{
first_2 = 0;
fwrite(">>planar_code<<", 1uLL, 0xFuLL, stdout);
}
if ( nv <= 254 )
return func7();
if ( nv > 65534 )
{
fwrite("Graphs of that size are currently not supported -- exiting!\n", 1uLL, 0x3CuLL, stderr);
exit(-1);
}
return func9();
}

original_code:
void writeEdgeDeletedPlanarCode(){
    static int first = 1;
    if(first){
        first = 0;
        fprintf(stdout, ">>planar_code<<");
    }
    if (nv + 1 <= 255) {
        writeEdgeDeletedPlanarCodeChar();
    } else if (nv + 1 <= 65535) {
        writeEdgeDeletedPlanarCodeShort();
    } else {
        fprintf(stderr, "Graphs of that size are currently not supported -- exiting!\n");
        exit(-1);
    }
}

variable_types: first int


writeSubdividedPlanarCodeChar

decompiled_code:
int writeSubdividedPlanarCodeChar()
{
int result; // eax
__int64 v1; // r12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // rbx

result = fputc(nv + nf, stdout);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v2 = firstedge[v1];
v3 = v2;
do
{
fputc(*(_DWORD *)(v3 + 4) + 1, stdout);
fputc(*(_DWORD *)(v3 + 8) + nv + 1, stdout);
v3 = *(_QWORD *)(v3 + 24);
}
while ( v2 != v3 );
result = fputc(0, stdout);
++v1;
}
while ( nv > (int)v1 );
}
if ( nf > 0 )
{
v4 = 0LL;
do
{
v5 = facestart[v4];
v6 = v5;
do
{
fputc(*(_DWORD *)v6 + 1, stdout);
v6 = *(_QWORD *)(*(_QWORD *)(v6 + 32) + 16LL);
}
while ( v5 != v6 );
result = fputc(0, stdout);
++v4;
}
while ( nf > (int)v4 );
}
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax
__int64 v1; // r12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // rbx

result = fputc(nv + nf, stdout);
if ( nv > 0 )
{
v1 = 0LL;
do
{
v2 = firstedge[v1];
v3 = v2;
do
{
fputc(*(_DWORD *)(v3 + 4) + 1, stdout);
fputc(*(_DWORD *)(v3 + 8) + nv + 1, stdout);
v3 = *(_QWORD *)(v3 + 24);
}
while ( v2 != v3 );
result = fputc(0, stdout);
++v1;
}
while ( nv > (int)v1 );
}
if ( nf > 0 )
{
v4 = 0LL;
do
{
v5 = facestart[v4];
v6 = v5;
do
{
fputc(*(_DWORD *)v6 + 1, stdout);
v6 = *(_QWORD *)(*(_QWORD *)(v6 + 32) + 16LL);
}
while ( v5 != v6 );
result = fputc(0, stdout);
++v4;
}
while ( nf > (int)v4 );
}
return result;
}

original_code:
void writeSubdividedPlanarCodeChar(){
    int i;
    EDGE *e, *elast;
    fputc(nv + nf, stdout);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            fputc(e->end + 1, stdout);
            fputc(nv + e->rightface + 1, stdout);
            e = e->next;
        } while (e != elast);
        fputc(0, stdout);
    }
    for(i=0; i<nf; i++){
        e = elast = facestart[i];
        do {
            fputc(e->start + 1, stdout);
            e = e->inverse->prev;
        } while (e != elast);
        fputc(0, stdout);
    }
}

canonical_original_code:
void writeSubdividedPlanarCodeChar(){
    int i;
    struct e *e; struct e *elast;
    fputc(nv + nf, stdout);
    for(i=0; i<nv; i++){
        e = elast = firstedge[i];
        do {
            fputc(e->end + 1, stdout);
            fputc(nv + e->rightface + 1, stdout);
            e = e->next;
        } while (e != elast);
        fputc(0, stdout);
    }
    for(i=0; i<nf; i++){
        e = elast = facestart[i];
        do {
            fputc(e->start + 1, stdout);
            e = e->inverse->prev;
        } while (e != elast);
        fputc(0, stdout);
    }
}


writeSubdividedPlanarCodeShort

decompiled_code:
void __noreturn writeSubdividedPlanarCodeShort()
{
fwrite("Graphs of this size are not yet supported\n", 1uLL, 0x2AuLL, stderr);
exit(-1);
}

canonical_decompiled_code:
void __noreturn func8()
{
fwrite("Graphs of this size are not yet supported\n", 1uLL, 0x2AuLL, stderr);
exit(-1);
}

original_code:
void writeSubdividedPlanarCodeShort(){
    fprintf(stderr, "Graphs of this size are not yet supported\n");
    exit(-1);
}


writeSubdividedPlanarCode

decompiled_code:
int writeSubdividedPlanarCode()
{
if ( first_2 )
{
first_2 = 0;
fwrite(">>planar_code<<", 1uLL, 0xFuLL, stdout);
}
if ( nv + nf > 254 )
{
if ( nv + nf <= 65534 )
writeSubdividedPlanarCodeShort();
fwrite("Graphs of that size are currently not supported -- exiting!\n", 1uLL, 0x3CuLL, stderr);
exit(-1);
}
return writeSubdividedPlanarCodeChar();
}

canonical_decompiled_code:
int func9()
{
if ( first_2 )
{
first_2 = 0;
fwrite(">>planar_code<<", 1uLL, 0xFuLL, stdout);
}
if ( nv + nf > 254 )
{
if ( nv + nf <= 65534 )
func8();
fwrite("Graphs of that size are currently not supported -- exiting!\n", 1uLL, 0x3CuLL, stderr);
exit(-1);
}
return func7();
}

original_code:
void writeSubdividedPlanarCode(){
    static int first = 1;
    if(first){
        first = 0;
        fprintf(stdout, ">>planar_code<<");
    }
    if (nv + nf + 1 <= 255) {
        writeSubdividedPlanarCodeChar();
    } else if (nv + nf + 1 <= 65535) {
        writeSubdividedPlanarCodeShort();
    } else {
        fprintf(stderr, "Graphs of that size are currently not supported -- exiting!\n");
        exit(-1);
    }
}


findEdge

decompiled_code:
__int64 __fastcall findEdge(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = firstedge[a1];
while ( *(_DWORD *)(result + 4) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( firstedge[a1] == result )
{
fprintf(stderr, "error while looking for edge from %d to %d.\n", a1, a2);
exit(0);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, unsigned int a2)
{
__int64 result; // rax

result = firstedge[a1];
while ( *(_DWORD *)(result + 4) != a2 )
{
result = *(_QWORD *)(result + 24);
if ( firstedge[a1] == result )
{
fprintf(stderr, "error while looking for edge from %d to %d.\n", a1, a2);
exit(0);
}
}
return result;
}

original_code:
EDGE *findEdge(int from, int to) {
    EDGE *e, *elast;
    e = elast = firstedge[from];
    do {
        if (e->end == to) {
            return e;
        }
        e = e->next;
    } while (e != elast);
    fprintf(stderr, "error while looking for edge from %d to %d.\n", from, to);
    exit(0);
}

canonical_original_code:
struct e *findEdge(int from, int to) {
    struct e *e; struct e *elast;
    e = elast = firstedge[from];
    do {
        if (e->end == to) {
            return e;
        }
        e = e->next;
    } while (e != elast);
    fprintf(stderr, "error while looking for edge from %d to %d.\n", from, to);
    exit(0);
}

variable_types: from int

variable_types: to int

return_type: struct e { int start; int end; int rightface; struct e *prev; struct e *next; struct e *inverse; int mark; } *


makeDual

decompiled_code:
__int64 makeDual()
{
__int64 result; // rax
int v1; // er11
int v2; // edi
__int64 v3; // r9
int v4; // esi
__int64 v5; // rdx
__int64 v6; // rax
int v7; // ecx
__int64 v8; // r8

result = (unsigned int)(markvalue + 2);
if ( (int)result <= 30000 )
{
markvalue += 2;
}
else
{
markvalue = 2;
result = (__int64)&unk_404148;
do
{
*(_DWORD *)result = 0;
result += 48LL;
}
while ( (_UNKNOWN *)result != &unk_408708 );
}
nf = 0;
v1 = nv;
if ( nv > 0 )
{
v2 = markvalue;
v3 = 0LL;
result = 0LL;
v4 = 0;
do
{
v8 = firstedge[v3];
v5 = v8;
do
{
if ( *(_DWORD *)(v5 + 40) != v2 )
{
facestart[v4] = v5;
v6 = v5;
v7 = 0;
do
{
*(_DWORD *)(v6 + 8) = v4;
*(_DWORD *)(v6 + 40) = v2;
v6 = *(_QWORD *)(*(_QWORD *)(v6 + 32) + 16LL);
++v7;
}
while ( v5 != v6 );
faceSize[v4++] = v7;
result = 1LL;
}
v5 = *(_QWORD *)(v5 + 24);
}
while ( v8 != v5 );
++v3;
}
while ( v3 != v1 );
if ( (_BYTE)result )
nf = v4;
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 result; // rax
int v1; // er11
int v2; // edi
__int64 v3; // r9
int v4; // esi
__int64 v5; // rdx
__int64 v6; // rax
int v7; // ecx
__int64 v8; // r8

result = (unsigned int)(markvalue + 2);
if ( (int)result <= 30000 )
{
markvalue += 2;
}
else
{
markvalue = 2;
result = (__int64)&unk_404148;
do
{
*(_DWORD *)result = 0;
result += 48LL;
}
while ( (_UNKNOWN *)result != &unk_408708 );
}
nf = 0;
v1 = nv;
if ( nv > 0 )
{
v2 = markvalue;
v3 = 0LL;
result = 0LL;
v4 = 0;
do
{
v8 = firstedge[v3];
v5 = v8;
do
{
if ( *(_DWORD *)(v5 + 40) != v2 )
{
facestart[v4] = v5;
v6 = v5;
v7 = 0;
do
{
*(_DWORD *)(v6 + 8) = v4;
*(_DWORD *)(v6 + 40) = v2;
v6 = *(_QWORD *)(*(_QWORD *)(v6 + 32) + 16LL);
++v7;
}
while ( v5 != v6 );
faceSize[v4++] = v7;
result = 1LL;
}
v5 = *(_QWORD *)(v5 + 24);
}
while ( v8 != v5 );
++v3;
}
while ( v3 != v1 );
if ( (_BYTE)result )
nf = v4;
}
return result;
}

original_code:
void makeDual() {
    register int i, sz;
    register EDGE *e, *ex, *ef, *efx;
    {int mki; if ((markvalue += 2) > 30000) { markvalue = 2; for (mki=0;mki<(6*64 -12);++mki) edges[mki].mark=0;}};
    nf = 0;
    for (i = 0; i < nv; ++i) {
        e = ex = firstedge[i];
        do {
            if (!((e)->mark == markvalue)) {
                facestart[nf] = ef = efx = e;
                sz = 0;
                do {
                    ef->rightface = nf;
                    (ef)->mark = markvalue;
                    ef = ef->inverse->prev;
                    ++sz;
                } while (ef != efx);
                faceSize[nf] = sz;
                ++nf;
            }
            e = e->next;
        } while (e != ex);
    }
}

canonical_original_code:
void makeDual() {
    register int i; register int sz;
    register struct e *e; register struct e *ex; register struct e *ef; register struct e *efx;
    {int mki; if ((markvalue += 2) > 30000) { markvalue = 2; for (mki=0;mki<(6*64 -12);++mki) edges[mki].mark=0;}};
    nf = 0;
    for (i = 0; i < nv; ++i) {
        e = ex = firstedge[i];
        do {
            if (!((e)->mark == markvalue)) {
                facestart[nf] = ef = efx = e;
                sz = 0;
                do {
                    ef->rightface = nf;
                    (ef)->mark = markvalue;
                    ef = ef->inverse->prev;
                    ++sz;
                } while (ef != efx);
                faceSize[nf] = sz;
                ++nf;
            }
            e = e->next;
        } while (e != ex);
    }
}

variable_types: sz int

variable_types: mki int


decodePlanarCode

original_code:
void decodePlanarCode(unsigned short* code) {
    int i, j, codePosition;
    int edgeCounter = 0;
    EDGE *inverse;
    nv = code[0];
    codePosition = 1;
    for (i = 0; i < nv; i++) {
        degree[i] = 0;
        firstedge[i] = edges + edgeCounter;
        edges[edgeCounter].start = i;
        edges[edgeCounter].end = code[codePosition] - 1;
        edges[edgeCounter].next = edges + edgeCounter + 1;
        if (code[codePosition] - 1 < i) {
            inverse = findEdge(code[codePosition] - 1, i);
            edges[edgeCounter].inverse = inverse;
            inverse->inverse = edges + edgeCounter;
        } else {
            edges[edgeCounter].inverse = ((void *)0);
        }
        edgeCounter++;
        codePosition++;
        for (j = 1; code[codePosition]; j++, codePosition++) {
            if (j == (64 -1)) {
                fprintf(stderr, "MAXVAL too small: %d\n", (64 -1));
                exit(0);
            }
            edges[edgeCounter].start = i;
            edges[edgeCounter].end = code[codePosition] - 1;
            edges[edgeCounter].prev = edges + edgeCounter - 1;
            edges[edgeCounter].next = edges + edgeCounter + 1;
            if (code[codePosition] - 1 < i) {
                inverse = findEdge(code[codePosition] - 1, i);
                edges[edgeCounter].inverse = inverse;
                inverse->inverse = edges + edgeCounter;
            } else {
                edges[edgeCounter].inverse = ((void *)0);
            }
            edgeCounter++;
        }
        firstedge[i]->prev = edges + edgeCounter - 1;
        edges[edgeCounter - 1].next = firstedge[i];
        degree[i] = j;
        codePosition++;
    }
    ne = edgeCounter;
    makeDual();
}

canonical_original_code:
void decodePlanarCode(unsigned short *code) {
    int i; int j; int codePosition;
    int edgeCounter = 0;
    struct e *inverse;
    nv = code[0];
    codePosition = 1;
    for (i = 0; i < nv; i++) {
        degree[i] = 0;
        firstedge[i] = edges + edgeCounter;
        edges[edgeCounter].start = i;
        edges[edgeCounter].end = code[codePosition] - 1;
        edges[edgeCounter].next = edges + edgeCounter + 1;
        if (code[codePosition] - 1 < i) {
            inverse = findEdge(code[codePosition] - 1, i);
            edges[edgeCounter].inverse = inverse;
            inverse->inverse = edges + edgeCounter;
        } else {
            edges[edgeCounter].inverse = ((void *)0);
        }
        edgeCounter++;
        codePosition++;
        for (j = 1; code[codePosition]; j++, codePosition++) {
            if (j == (64 -1)) {
                fprintf(stderr, "MAXVAL too small: %d\n", (64 -1));
                exit(0);
            }
            edges[edgeCounter].start = i;
            edges[edgeCounter].end = code[codePosition] - 1;
            edges[edgeCounter].prev = edges + edgeCounter - 1;
            edges[edgeCounter].next = edges + edgeCounter + 1;
            if (code[codePosition] - 1 < i) {
                inverse = findEdge(code[codePosition] - 1, i);
                edges[edgeCounter].inverse = inverse;
                inverse->inverse = edges + edgeCounter;
            } else {
                edges[edgeCounter].inverse = ((void *)0);
            }
            edgeCounter++;
        }
        firstedge[i]->prev = edges + edgeCounter - 1;
        edges[edgeCounter - 1].next = firstedge[i];
        degree[i] = j;
        codePosition++;
    }
    ne = edgeCounter;
    makeDual();
}

variable_types:
code
unsigned short *

variable_types: j int

variable_types: codePosition int

variable_types: edgeCounter int