decompiled_code:
void __fastcall __noreturn start_thread(void *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
__int64 v4; // r12
socklen_t addr_len; // [rsp+4h] [rbp-42Ch] BYREF
unsigned int v6; // [rsp+8h] [rbp-428h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-424h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-420h] BYREF
char v9[256]; // [rsp+20h] [rbp-410h] BYREF
char v10[256]; // [rsp+120h] [rbp-310h] BYREF
char v11[256]; // [rsp+220h] [rbp-210h] BYREF
char v12[272]; // [rsp+320h] [rbp-110h] BYREF

while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v1 = accept(listen_socket, &addr, &addr_len);
if ( v1 != -1 )
break;
perror("An invalid socket has been accepted:");
}
v2 = v1;
if ( (unsigned int)read_request(v1, (__int64)v12, 256)
|| (v3 = _isoc99_sscanf(v12, "%255s %255s %255s", v11, v10, v9), v9[255] = 0, v10[255] = 0, v11[255] = 0, v3 != 3) )
{
clienterror(v2, v12, "400", "Bad request", "The request is not well-formatted");
}
else if ( !strcmp(v11, "GET") )
{
if ( !strcmp(v9, "HTTP/1.0") )
{
v4 = file_manager_get(v10, &v6, &v7);
if ( v4 )
{
clientsuccess(v2, (__int64)v10, v7);
write_complete(v2, v4, v7);
file_manager_release(v4, v7);
}
else if ( v6 == 404 )
{
clienterror(v2, v10, "404", "Not found", "The file cannot be found");
}
else
{
if ( v6 != 401 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Internal error: unknown status %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"start_thread",
140LL,
v6,
27LL,
0LL,
37LL);
exit(-1);
}
clienterror(v2, v10, "401", "Not authorized", "The file cannot be accessed");
}
}
else
{
clienterror(v2, v9, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
}
}
else
{
clienterror(v2, v12, "501", "Not implemented", "The request is not implemented");
}
close(v2);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
__int64 v4; // r12
socklen_t addr_len; // [rsp+4h] [rbp-42Ch] BYREF
unsigned int v6; // [rsp+8h] [rbp-428h] BYREF
unsigned int v7; // [rsp+Ch] [rbp-424h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-420h] BYREF
char v9[256]; // [rsp+20h] [rbp-410h] BYREF
char v10[256]; // [rsp+120h] [rbp-310h] BYREF
char v11[256]; // [rsp+220h] [rbp-210h] BYREF
char v12[272]; // [rsp+320h] [rbp-110h] BYREF

while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v1 = accept(listen_socket, &addr, &addr_len);
if ( v1 != -1 )
break;
perror("An invalid socket has been accepted:");
}
v2 = v1;
if ( (unsigned int)func0(v1, (__int64)v12, 256)
|| (v3 = _isoc99_sscanf(v12, "%255s %255s %255s", v11, v10, v9), v9[255] = 0, v10[255] = 0, v11[255] = 0, v3 != 3) )
{
func2(v2, v12, "400", "Bad request", "The request is not well-formatted");
}
else if ( !strcmp(v11, "GET") )
{
if ( !strcmp(v9, "HTTP/1.0") )
{
v4 = file_manager_get(v10, &v6, &v7);
if ( v4 )
{
func3(v2, (__int64)v10, v7);
func1(v2, v4, v7);
file_manager_release(v4, v7);
}
else if ( v6 == 404 )
{
func2(v2, v10, "404", "Not found", "The file cannot be found");
}
else
{
if ( v6 != 401 )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Internal error: unknown status %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"start_thread",
140LL,
v6,
27LL,
0LL,
37LL);
exit(-1);
}
func2(v2, v10, "401", "Not authorized", "The file cannot be accessed");
}
}
else
{
func2(v2, v9, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
}
}
else
{
func2(v2, v12, "501", "Not implemented", "The request is not implemented");
}
close(v2);
}
}

original_code:
void* start_thread(void *arg) {
   char request[256];
   char op[256];
   char file[256];
   char version[256];
   while (1) {
      struct sockaddr_in csin;
      int sinsize = sizeof(csin);
      int s = accept(listen_socket, (struct sockaddr*) &csin, (socklen_t*)&sinsize);
      if (s == -1) {
         perror("An invalid socket has been accepted:");
         continue;
      }
      ;
      int r = read_request(s, request, 256);
      ;
      if (r == 0) {
         int m = sscanf(request, "%255s %255s %255s", op, file, version);
         op[256 -1] = file[256 -1] = version[256 -1] = '\0';
         if (m != 3) {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         } else {
            if (strcmp(op, "GET")) {
               clienterror(s, request, "501", "Not implemented", "The request is not implemented");
            } else if (strcmp(version, "HTTP/1.0")) {
               clienterror(s, version, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
            } else {
               int status, length;
                  char *c = file_manager_get(file, &status, &length);
                  if (c != ((void *)0)) {
                     clientsuccess(s, file, length, status);
                     write_complete(s, c, length);
                     file_manager_release(c, length);
                  } else {
                     if (status == 404) {
                        clienterror(s, file, "404", "Not found", "The file cannot be found");
                     } else if (status == 401) {
                        clienterror(s, file, "401", "Not authorized", "The file cannot be accessed");
                     } else {
                        do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Internal error: unknown status %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 140, status, 0x1B, 0, 37); exit(-1); } while(0);
                     }
                  }
               }
            }
         } else {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         }
         close(s);
      }
   }

canonical_original_code:
void *start_thread(void *arg) {
   char request[256];
   char op[256];
   char file[256];
   char version[256];
   while (1) {
      struct sockaddr_in csin;
      int sinsize = sizeof(csin);
      int s = accept(listen_socket, (struct sockaddr*) &csin, (socklen_t*)&sinsize);
      if (s == -1) {
         perror("An invalid socket has been accepted:");
         continue;
      }
      ;
      int r = read_request(s, request, 256);
      ;
      if (r == 0) {
         int m = sscanf(request, "%255s %255s %255s", op, file, version);
         op[256 -1] = file[256 -1] = version[256 -1] = '\0';
         if (m != 3) {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         } else {
            if (strcmp(op, "GET")) {
               clienterror(s, request, "501", "Not implemented", "The request is not implemented");
            } else if (strcmp(version, "HTTP/1.0")) {
               clienterror(s, version, "505", "HTTP version not supported", "The server does not implement this version of HTTP");
            } else {
               int status; int length;
                  char *c = file_manager_get(file, &status, &length);
                  if (c != ((void *)0)) {
                     clientsuccess(s, file, length, status);
                     write_complete(s, c, length);
                     file_manager_release(c, length);
                  } else {
                     if (status == 404) {
                        clienterror(s, file, "404", "Not found", "The file cannot be found");
                     } else if (status == 401) {
                        clienterror(s, file, "401", "Not authorized", "The file cannot be accessed");
                     } else {
                        do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Internal error: unknown status %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 140, status, 0x1B, 0, 37); exit(-1); } while(0);
                     }
                  }
               }
            }
         } else {
            clienterror(s, request, "400", "Bad request", "The request is not well-formatted");
         }
         close(s);
      }
   }

variable_types:
arg
void *

variable_types:
request
char [256]

variable_types:
op
char [256]

variable_types:
file
char [256]

variable_types:
version
char [256]

variable_types: 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]; }

variable_types: sinsize int

variable_types: s int

variable_types: m int

variable_types:
c
char *

return_type: void *


repo: schaars/simple-web-server

decompiled_code:
void __fastcall create_pool(int a1, int a2)
{
__int64 v2; // rbx
pthread_t v3; // [rsp-20h] [rbp-20h] BYREF

listen_socket = a2;
if ( a1 > 0 )
{
v2 = 0LL;
do
{
if ( pthread_create(&v3, 0LL, (void *(*)(void *))start_thread, 0LL) )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Failed to create the thread %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"create_pool",
166LL,
v2,
27LL,
0LL,
37LL);
exit(-1);
}
v2 = (unsigned int)(v2 + 1);
}
while ( a1 != (_DWORD)v2 );
}
}

canonical_decompiled_code:
void __fastcall func5(int a1, int a2)
{
__int64 v2; // rbx
pthread_t v3; // [rsp-20h] [rbp-20h] BYREF

listen_socket = a2;
if ( a1 > 0 )
{
v2 = 0LL;
do
{
if ( pthread_create(&v3, 0LL, (void *(*)(void *))start_thread, 0LL) )
{
fprintf(
stderr,
"%c[%d;%dm(%s,%d) Failed to create the thread %d%c[%d;%dm\n",
27LL,
0LL,
31LL,
"create_pool",
166LL,
v2,
27LL,
0LL,
37LL);
exit(-1);
}
v2 = (unsigned int)(v2 + 1);
}
while ( a1 != (_DWORD)v2 );
}
}

original_code:
void create_pool(const int pool_size, int ls) {
      int i;
      listen_socket = ls;
      for (i=0; i<pool_size; i++) {
         pthread_t thread;
         int rc = pthread_create(&thread, ((void *)0), start_thread, ((void *)0));
         if (rc) {
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Failed to create the thread %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 166, i, 0x1B, 0, 37); exit(-1); } while(0);
         }
      }
   }

canonical_original_code:
void create_pool(int pool_size, int ls) {
      int i;
      listen_socket = ls;
      for (i=0; i<pool_size; i++) {
         unsigned long thread;
         int rc = pthread_create(&thread, ((void *)0), start_thread, ((void *)0));
         if (rc) {
            do { fprintf(stderr, "%c[%d;%dm(%s,%d) " "Failed to create the thread %d" "%c[%d;%dm\n", 0x1B, 0, 31, __FUNCTION__ , 166, i, 0x1B, 0, 37); exit(-1); } while(0);
         }
      }
   }

variable_types: ls int

variable_types: i int

variable_types:
thread
unsigned long

variable_types: rc int

return_type: void


decompiled_code:
void delete_pool()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code:
void delete_pool(void) {
   }

canonical_original_code:
void delete_pool() {
   }


decompiled_code:
void __fastcall range_tree_clear(void *ptr)
{
if ( ptr )
{
range_tree_clear(*(void **)ptr);
range_tree_clear(*((void **)ptr + 1));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
if ( ptr )
{
func0(*(void **)ptr);
func0(*((void **)ptr + 1));
free(ptr);
}
}

original_code:
void range_tree_clear(range_node* tree)
{
 if(! tree) return;
 range_tree_clear(tree->left);
 range_tree_clear(tree->right);
 free(tree);
 tree = ((void *)0);
}

canonical_original_code:
void range_tree_clear(struct range_node *tree)
{
 if(! tree) return;
 range_tree_clear(tree->left);
 range_tree_clear(tree->right);
 free(tree);
 tree = ((void *)0);
}

variable_types: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; } *


decompiled_code:
__int64 __fastcall range_tree_contains(__int64 *a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 v5; // rdi
__int64 result; // rax
int v7; // edx
__int64 v8; // rdi

if ( !a1 )
return 0LL;
if ( !a2 )
_assert_fail("tree && val", "range_tree.c", 0x21u, "range_tree_contains");
if ( ((int (__fastcall *)(__int64))a3)(a1[2]) <= 0 )
{
v7 = a3(a1[3], a2);
result = 1LL;
if ( v7 < 0 )
{
v8 = a1[1];
result = 0LL;
if ( v8 )
result = range_tree_contains(v8, a2, a3);
}
}
else
{
v5 = *a1;
result = 0LL;
if ( *a1 )
result = range_tree_contains(v5, a2, a3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, __int64 a2, __int64 (__fastcall *a3)(__int64, __int64))
{
__int64 v5; // rdi
__int64 result; // rax
int v7; // edx
__int64 v8; // rdi

if ( !a1 )
return 0LL;
if ( !a2 )
_assert_fail("tree && val", "range_tree.c", 0x21u, "range_tree_contains");
if ( ((int (__fastcall *)(__int64))a3)(a1[2]) <= 0 )
{
v7 = a3(a1[3], a2);
result = 1LL;
if ( v7 < 0 )
{
v8 = a1[1];
result = 0LL;
if ( v8 )
result = func1(v8, a2, a3);
}
}
else
{
v5 = *a1;
result = 0LL;
if ( *a1 )
result = func1(v5, a2, a3);
}
return result;
}

original_code:
int range_tree_contains(const range_node* tree, const void* val, val_cmp comp)
{
 if(!tree) return 0;
 ((tree && val) ? (void) (0) : __assert_fail ("tree && val", "range_tree.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(comp(tree->min, val) > 0) {
  if(tree->left) return range_tree_contains(tree->left, val, comp);
  else return 0;
 } else if(comp(tree->max, val) < 0) {
  if(tree->right) return range_tree_contains(tree->right, val, comp);
  else return 0;
 }
 return 1;
}

canonical_original_code:
int range_tree_contains(struct range_node *tree, void *val, int (*comp)(void *a, void *b))
{
 if(!tree) return 0;
 ((tree && val) ? (void) (0) : __assert_fail ("tree && val", "range_tree.c", 33, __extension__ __PRETTY_FUNCTION__));
 if(comp(tree->min, val) > 0) {
  if(tree->left) return range_tree_contains(tree->left, val, comp);
  else return 0;
 } else if(comp(tree->max, val) < 0) {
  if(tree->right) return range_tree_contains(tree->right, val, comp);
  else return 0;
 }
 return 1;
}

variable_types:
val
void *

variable_types:
comp
int (*)(void *a, void *b)

return_type: int

user_defined_types: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; }


cmp

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

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

original_code:
int cmp(const void* a, const void* b)
{
 return *(int*)a - *(int*)b;
}

canonical_original_code:
int cmp(void *a, void *b)
{
 return *(int *)a - *(int *)b;
}

variable_types:
a
void *

variable_types:
b
void *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = 5;
v5 = 7;
v3 = range_tree_create(v6, &v5, envp);
range_tree_clear(v3);
return 0;
}

original_code:
int main(int argc, char* argv[])
{
 int a = 5;
 int b = 7;
 range_node* node = range_tree_create(&a, &b);
 range_tree_clear(node);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int a = 5;
 int b = 7;
 struct range_node *node = range_tree_create(&a, &b);
 range_tree_clear(node);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: a int

variable_types: b int


original_code:
range_node* range_list_merge_ranges(range_node* first, val_cmp comp, val_inc inc)
{
 ((first) ? (void) (0) : __assert_fail ("first", "range_list.c", 13, __extension__ __PRETTY_FUNCTION__));
 range_node* curr = first;
 range_node* last = first;
 while(curr != ((void *)0)) {
  ((comp(curr->min, curr->max) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, curr->max) <= 0", "range_list.c", 17, __extension__ __PRETTY_FUNCTION__));
  void* new_curr_max = curr->max;
  range_node* tmp = curr->right;
  while(tmp != ((void *)0)) {
   ((comp(curr->min, tmp->min) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, tmp->min) <= 0", "range_list.c", 21, __extension__ __PRETTY_FUNCTION__));
   void* a = new_curr_max;
   inc(a);
   if(comp(a, tmp->min) < 0) break;
   if(comp(new_curr_max, tmp->max) < 0)
    new_curr_max = tmp->max;
   tmp->left = ((void *)0);
   tmp = tmp->right;
   curr->right->right = ((void *)0);
   free(curr->right);
   curr->right = tmp;
  }
  curr->max = new_curr_max;
  if(tmp) tmp->left = curr;
  last = curr;
  curr = curr->right;
 }
 return last;
}

canonical_original_code:
struct range_node *range_list_merge_ranges(struct range_node *first, int (*comp)(void *a, void *b), void (*inc)(void *val))
{
 ((first) ? (void) (0) : __assert_fail ("first", "range_list.c", 13, __extension__ __PRETTY_FUNCTION__));
 struct range_node *curr = first;
 struct range_node *last = first;
 while(curr != ((void *)0)) {
  ((comp(curr->min, curr->max) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, curr->max) <= 0", "range_list.c", 17, __extension__ __PRETTY_FUNCTION__));
  void *new_curr_max = curr->max;
  struct range_node *tmp = curr->right;
  while(tmp != ((void *)0)) {
   ((comp(curr->min, tmp->min) <= 0) ? (void) (0) : __assert_fail ("comp(curr->min, tmp->min) <= 0", "range_list.c", 21, __extension__ __PRETTY_FUNCTION__));
   void *a = new_curr_max;
   inc(a);
   if(comp(a, tmp->min) < 0) break;
   if(comp(new_curr_max, tmp->max) < 0)
    new_curr_max = tmp->max;
   tmp->left = ((void *)0);
   tmp = tmp->right;
   curr->right->right = ((void *)0);
   free(curr->right);
   curr->right = tmp;
  }
  curr->max = new_curr_max;
  if(tmp) tmp->left = curr;
  last = curr;
  curr = curr->right;
 }
 return last;
}

variable_types:
first
struct range_node *

variable_types:
inc
void (*)(void *val)

return_type: struct range_node { struct range_node *left; struct range_node *right; void *min; void *max; } *


canonical_decompiled_code:
__int64 __fastcall main(int a1, char **a2, char **a3)
{
_QWORD *v3; // rax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = 5;
v5 = 7;
v3 = sub_401167((__int64)v6, (__int64)&v5);
func6(v3);
return 0LL;
}


repo: schachmat/slcp

decompiled_code:
int __fastcall change_col(unsigned int a1)
{
int v2; // edi

fputc(1, edata);
fwrite(&unk_402004, 1uLL, 3uLL, edata);
v2 = a1 + 48;
if ( a1 >= 8 )
v2 = 48;
fputc(v2, edata);
fputc(109, edata);
return fputc(2, edata);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1)
{
int v2; // edi

fputc(1, edata);
fwrite(&unk_402004, 1uLL, 3uLL, edata);
v2 = a1 + 48;
if ( a1 >= 8 )
v2 = 48;
fputc(v2, edata);
fputc(109, edata);
return fputc(2, edata);
}

original_code:
static void change_col(unsigned int col)
{
 fputs("\x01", stdout);
 fputs("\033[3", stdout);
 fputc((col > 7 ? 0 : col) + '0', stdout);
 fputc('m', stdout);
 fputs("\x02", stdout);
}

variable_types:
col
unsigned int


catscol

decompiled_code:
int __fastcall catscol(char *s, unsigned int a2)
{
change_col(a2);
return fputs(s, edata);
}

canonical_decompiled_code:
int __fastcall func9(char *s, unsigned int a2)
{
func8(a2);
return fputs(s, edata);
}

original_code:
static void catscol(const char* str, unsigned int col)
{
 change_col(col);
 fputs(str, stdout);
}

canonical_original_code:
static void catscol(char *str, unsigned int col)
{
 change_col(col);
 fputs(str, stdout);
}

variable_types:
str
char *


ppidCmp

repo: schischi/xcwd

decompiled_code:
__int64 __fastcall ppidCmp(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 1) - *((_DWORD *)a2 + 2);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 1) - *((_DWORD *)a2 + 2);
}

original_code:
int ppidCmp(const void *p1, const void *p2)
{
    return ((struct proc_s *)p1)->ppid - ((struct proc_s *)p2)->ppid;
}

canonical_original_code:
int ppidCmp(void *p1, void *p2)
{
    return ((struct proc_s *)p1)->ppid - ((struct proc_s *)p2)->ppid;
}

user_defined_types: struct proc_s { long pid; long ppid; char name[32]; }


nameCmp

decompiled_code:
int __fastcall nameCmp(const void *a1, const void *a2)
{
return strcasecmp((const char *)a1 + 16, (const char *)a2 + 16);
}

canonical_decompiled_code:
int __fastcall func1(const void *a1, const void *a2)
{
return strcasecmp((const char *)a1 + 16, (const char *)a2 + 16);
}

original_code:
int nameCmp(const void *p1, const void *p2)
{
    return strcasecmp(((struct proc_s *)p1)->name,
            ((struct proc_s *)p2)->name);
}

canonical_original_code:
int nameCmp(void *p1, void *p2)
{
    return strcasecmp(((struct proc_s *)p1)->name,
            ((struct proc_s *)p2)->name);
}


freeProcesses

decompiled_code:
void __fastcall freeProcesses(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
static void freeProcesses(processes_t p)
{
    free(p->ps);
    free(p);
}

canonical_original_code:
static void freeProcesses(struct processes_s *p)
{
    free(p->ps);
    free(p);
}

variable_types: struct processes_s { struct proc_s { long pid; long ppid; char name[32]; } *ps; unsigned long n; } *

user_defined_types: struct processes_s { struct proc_s *ps; unsigned long n; }


windowPid

decompiled_code:
__int64 __fastcall windowPid(Window a1)
{
Atom v1; // rbp
Atom v2; // rax
__int64 v3; // rbx
unsigned __int8 *v5; // [rsp+8h] [rbp-40h] BYREF
unsigned __int64 v6; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v7; // [rsp+18h] [rbp-30h] BYREF
int v8; // [rsp+24h] [rbp-24h] BYREF
Atom v9[4]; // [rsp+28h] [rbp-20h] BYREF

v1 = XInternAtom(dpy, "_NET_WM_PID", 1);
v2 = XInternAtom(dpy, "CARDINAL", 0);
if ( XGetWindowProperty(dpy, a1, v1, 0LL, 1024LL, 0, v2, v9, &v8, &v7, &v6, &v5) )
return -1LL;
if ( !v5 )
return -1LL;
v3 = *(_QWORD *)v5;
XFree(v5);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(Window a1)
{
Atom v1; // rbp
Atom v2; // rax
__int64 v3; // rbx
unsigned __int8 *v5; // [rsp+8h] [rbp-40h] BYREF
unsigned __int64 v6; // [rsp+10h] [rbp-38h] BYREF
unsigned __int64 v7; // [rsp+18h] [rbp-30h] BYREF
int v8; // [rsp+24h] [rbp-24h] BYREF
Atom v9[4]; // [rsp+28h] [rbp-20h] BYREF

v1 = XInternAtom(dpy, "_NET_WM_PID", 1);
v2 = XInternAtom(dpy, "CARDINAL", 0);
if ( XGetWindowProperty(dpy, a1, v1, 0LL, 1024LL, 0, v2, v9, &v8, &v7, &v6, &v5) )
return -1LL;
if ( !v5 )
return -1LL;
v3 = *(_QWORD *)v5;
XFree(v5);
return v3;
}

original_code:
static long windowPid(Window focuswin)
{
    Atom nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1);
    Atom type;
    int format, status;
    long pid = -1;
    unsigned long nitems, after;
    unsigned char *data;
    status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0,
            (XInternAtom(dpy, "CARDINAL", 0)), &type, &format, &nitems, &after, &data);
    if (status == 0 && data) {
        pid = *((long*)data);
        XFree(data);
        do { if (0) fprintf(stderr, "_NET_WM_PID = %lu\n", pid); } while (0);
    }
    else
        do { if (0) fprintf(stderr, "%s", "_NET_WM_PID not found\n"); } while (0);
    return pid;
}

canonical_original_code:
static long windowPid(unsigned long focuswin)
{
    unsigned long nameAtom = XInternAtom(dpy, "_NET_WM_PID", 1);
    unsigned long type;
    int format; int status;
    long pid = -1;
    unsigned long nitems; unsigned long after;
    unsigned char *data;
    status = XGetWindowProperty(dpy, focuswin, nameAtom, 0, 1024, 0,
            (XInternAtom(dpy, "CARDINAL", 0)), &type, &format, &nitems, &after, &data);
    if (status == 0 && data) {
        pid = *((long *)data);
        XFree(data);
        do { if (0) fprintf(stderr, "_NET_WM_PID = %lu\n", pid); } while (0);
    }
    else
        do { if (0) fprintf(stderr, "%s", "_NET_WM_PID not found\n"); } while (0);
    return pid;
}

variable_types:
focuswin
unsigned long

variable_types:
nameAtom
unsigned long

variable_types:
type
unsigned long

variable_types: format int

variable_types: status int

variable_types: pid long

variable_types:
nitems
unsigned long

variable_types:
after
unsigned long

variable_types:
data
unsigned char *

return_type: long


focusedWindow

decompiled_code:
Window focusedWindow()
{
Display *v0; // rax
__int64 v1; // rdx
Atom v2; // rbx
Atom v3; // rax
void *v5; // [rsp+0h] [rbp-58h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned __int64 v7; // [rsp+10h] [rbp-48h] BYREF
unsigned __int64 v8; // [rsp+18h] [rbp-40h] BYREF
int v9; // [rsp+24h] [rbp-34h] BYREF
Window *v10; // [rsp+28h] [rbp-30h] BYREF
Window v11; // [rsp+30h] [rbp-28h] BYREF
Window v12; // [rsp+38h] [rbp-20h] BYREF
Atom v13; // [rsp+40h] [rbp-18h] BYREF
int v14[3]; // [rsp+4Ch] [rbp-Ch] BYREF

v0 = XOpenDisplay(0LL);
dpy = v0;
if ( !v0 )
exit(1);
v14[0] = 0;
XGetInputFocus(v0, &v12, v14);
v11 = XDefaultRootWindow(dpy);
v1 = 0LL;
if ( v11 != v12 )
{
while ( 1 )
{
v2 = XInternAtom(dpy, "WM_STATE", 0);
v3 = XInternAtom(dpy, "WM_STATE", 0);
if ( XGetWindowProperty(dpy, v12, v3, 0LL, 1024LL, 0, v2, &v13, &v9, &v8, &v7, (unsigned __int8 **)&v5) )
break;
if ( v5 )
{
XFree(v5);
return v12;
}
XQueryTree(dpy, v12, &v11, &v12, &v10, &v6);
if ( v12 == v11 )
return 0LL;
}
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
Window func4()
{
Display *v0; // rax
__int64 v1; // rdx
Atom v2; // rbx
Atom v3; // rax
void *v5; // [rsp+0h] [rbp-58h] BYREF
unsigned int v6; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned __int64 v7; // [rsp+10h] [rbp-48h] BYREF
unsigned __int64 v8; // [rsp+18h] [rbp-40h] BYREF
int v9; // [rsp+24h] [rbp-34h] BYREF
Window *v10; // [rsp+28h] [rbp-30h] BYREF
Window v11; // [rsp+30h] [rbp-28h] BYREF
Window v12; // [rsp+38h] [rbp-20h] BYREF
Atom v13; // [rsp+40h] [rbp-18h] BYREF
int v14[3]; // [rsp+4Ch] [rbp-Ch] BYREF

v0 = XOpenDisplay(0LL);
dpy = v0;
if ( !v0 )
exit(1);
v14[0] = 0;
XGetInputFocus(v0, &v12, v14);
v11 = XDefaultRootWindow(dpy);
v1 = 0LL;
if ( v11 != v12 )
{
while ( 1 )
{
v2 = XInternAtom(dpy, "WM_STATE", 0);
v3 = XInternAtom(dpy, "WM_STATE", 0);
if ( XGetWindowProperty(dpy, v12, v3, 0LL, 1024LL, 0, v2, &v13, &v9, &v8, &v7, (unsigned __int8 **)&v5) )
break;
if ( v5 )
{
XFree(v5);
return v12;
}
XQueryTree(dpy, v12, &v11, &v12, &v10, &v6);
if ( v12 == v11 )
return 0LL;
}
v1 = 0LL;
}
return v1;
}

original_code:
static Window focusedWindow()
{
    Atom type;
    Window focuswin, root, *children;
    int format, status;
    unsigned long nitems, after;
    unsigned int nchildren;
    unsigned char *data;
    dpy = XOpenDisplay (((void *)0));
    if (!dpy)
        exit(1);
    XGetInputFocus (dpy, &focuswin, (int[1]){});
    root = XDefaultRootWindow(dpy);
    if(root == focuswin)
        return 0L;
    do {
        status = XGetWindowProperty(dpy, focuswin, (XInternAtom(dpy, "WM_STATE", 0)), 0, 1024, 0,
                (XInternAtom(dpy, "WM_STATE", 0)), &type, &format, &nitems, &after, &data);
        if (status == 0) {
            if (data) {
                XFree(data);
                do { if (0) fprintf(stderr, "Window ID = %lu\n", focuswin); } while (0);
                return focuswin;
            }
        }
        else
            return 0;
        XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
        do { if (0) fprintf(stderr, "%s", "Current window does not have WM_STATE, getting parent\n"); } while (0);
    } while(focuswin != root);
    return 0;
}

canonical_original_code:
static unsigned long focusedWindow()
{
    unsigned long type;
    unsigned long focuswin; unsigned long root; unsigned long *children;
    int format; int status;
    unsigned long nitems; unsigned long after;
    unsigned int nchildren;
    unsigned char *data;
    dpy = XOpenDisplay (((void *)0));
    if (!dpy)
        exit(1);
    XGetInputFocus (dpy, &focuswin, (int [1]){});
    root = XDefaultRootWindow(dpy);
    if(root == focuswin)
        return 0L;
    do {
        status = XGetWindowProperty(dpy, focuswin, (XInternAtom(dpy, "WM_STATE", 0)), 0, 1024, 0,
                (XInternAtom(dpy, "WM_STATE", 0)), &type, &format, &nitems, &after, &data);
        if (status == 0) {
            if (data) {
                XFree(data);
                do { if (0) fprintf(stderr, "Window ID = %lu\n", focuswin); } while (0);
                return focuswin;
            }
        }
        else
            return 0;
        XQueryTree(dpy, focuswin, &root, &focuswin, &children, &nchildren);
        do { if (0) fprintf(stderr, "%s", "Current window does not have WM_STATE, getting parent\n"); } while (0);
    } while(focuswin != root);
    return 0;
}

variable_types:
root
unsigned long

variable_types:
children
unsigned long *

variable_types:
nchildren
unsigned int

return_type: unsigned long


cwdOfDeepestChild

original_code:
static int cwdOfDeepestChild(processes_t p, long pid)
{
    int i;
    struct proc_s key = { .pid = pid, .ppid = pid},
                  *res = ((void *)0), *lastRes = ((void *)0);
    do {
        if(res) {
            lastRes = res;
            key.ppid = res->pid;
        }
        res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                sizeof(struct proc_s), ppidCmp);
    } while(res);
    if(!lastRes) {
        return readPath(&key);
    }
    for(i = 0; lastRes != p->ps && (lastRes - i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes - i)))
            return 1;
    for(i = 1; lastRes != p->ps + p->n && (lastRes + i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes + i)))
            return 1;
    return 0;
}

canonical_original_code:
static int cwdOfDeepestChild(struct processes_s *p, long pid)
{
    int i;
    struct proc_s key = { .pid = pid, .ppid = pid}; struct proc_s *res = ((void *)0); struct proc_s *lastRes = ((void *)0);
    do {
        if(res) {
            lastRes = res;
            key.ppid = res->pid;
        }
        res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                sizeof(struct proc_s), ppidCmp);
    } while(res);
    if(!lastRes) {
        return readPath(&key);
    }
    for(i = 0; lastRes != p->ps && (lastRes - i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes - i)))
            return 1;
    for(i = 1; lastRes != p->ps + p->n && (lastRes + i)->ppid == lastRes->ppid; ++i)
        if(readPath((lastRes + i)))
            return 1;
    return 0;
}

variable_types: i int

variable_types: struct proc_s { long pid; long ppid; char name[32]; } *


getHomeDirectory

decompiled_code:
__int64 getHomeDirectory()
{
char *v0; // rax

v0 = getenv("HOME");
fprintf(stdout, "%s\n", v0);
return 1LL;
}

canonical_decompiled_code:
__int64 func8()
{
char *v0; // rax

v0 = getenv("HOME");
fprintf(stdout, "%s\n", v0);
return 1LL;
}

original_code:
int getHomeDirectory()
{
    do { if (0) fprintf(stderr, "%s", "getenv $HOME...\n"); } while (0);
    fprintf(stdout, "%s\n", getenv("HOME"));
    return 1;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Window v3; // rax
Window v4; // rbx
Window v5; // rdi
__int64 v6; // r13
void **v7; // rax
__int64 v8; // rbp
unsigned __int64 *v9; // rsi
char *v10; // rdi
int v11; // ebx
char *v13; // r14
__int64 v14; // rax
unsigned int v15; // er12
const char *v16; // rbx
__int64 *v17; // rax
unsigned __int64 v18; // [rsp+8h] [rbp-60h] BYREF
char key[16]; // [rsp+10h] [rbp-58h] BYREF
char dest[72]; // [rsp+20h] [rbp-48h] BYREF

v3 = focusedWindow();
if ( !v3 )
return getHomeDirectory(argc, argv);
v4 = v3;
v5 = v3;
v6 = windowPid(v3);
v7 = (void **)getProcesses();
v8 = (__int64)v7;
if ( !v7 )
return getHomeDirectory(v5, argv);
if ( v6 != -1 )
{
qsort(*v7, (size_t)v7[1], 0x30uLL, (__compar_fn_t)ppidCmp);
goto LABEL_5;
}
qsort(*v7, (size_t)v7[1], 0x30uLL, nameCmp);
v9 = &v18;
v10 = (char *)v4;
v13 = windowStrings(v4, &v18, "WM_CLASS");
if ( !v18 )
{
LABEL_15:
if ( !v18 )
goto LABEL_6;
goto LABEL_16;
}
v14 = 0LL;
v15 = 0;
while ( 1 )
{
v16 = &v13[v14];
strcpy(dest, &v13[v14]);
v9 = *(unsigned __int64 **)v8;
v10 = key;
v17 = (__int64 *)bsearch(key, *(const void **)v8, *(_QWORD *)(v8 + 8), 0x30uLL, nameCmp);
if ( v17 )
break;
v10 = (char *)v16;
v15 += strlen(v16) + 1;
v14 = v15;
if ( v15 >= v18 )
goto LABEL_15;
}
v6 = *v17;
if ( v18 )
{
LABEL_16:
v10 = v13;
free(v13);
}
if ( v6 == -1 )
goto LABEL_6;
LABEL_5:
v9 = (unsigned __int64 *)v6;
v10 = (char *)v8;
v11 = 0;
if ( !(unsigned int)cwdOfDeepestChild(v8, v6) )
LABEL_6:
v11 = getHomeDirectory(v10, v9);
freeProcesses((void *)v8);
return v11;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Window v3; // rax
Window v4; // rbx
Window v5; // rdi
__int64 v6; // r13
void **v7; // rax
__int64 v8; // rbp
unsigned __int64 *v9; // rsi
char *v10; // rdi
int v11; // ebx
char *v13; // r14
__int64 v14; // rax
unsigned int v15; // er12
const char *v16; // rbx
__int64 *v17; // rax
unsigned __int64 v18; // [rsp+8h] [rbp-60h] BYREF
char key[16]; // [rsp+10h] [rbp-58h] BYREF
char dest[72]; // [rsp+20h] [rbp-48h] BYREF

v3 = func4();
if ( !v3 )
return func8(argc, argv);
v4 = v3;
v5 = v3;
v6 = func3(v3);
v7 = (void **)func5();
v8 = (__int64)v7;
if ( !v7 )
return func8(v5, argv);
if ( v6 != -1 )
{
qsort(*v7, (size_t)v7[1], 0x30uLL, (__compar_fn_t)ppidCmp);
goto LABEL_5;
}
qsort(*v7, (size_t)v7[1], 0x30uLL, nameCmp);
v9 = &v18;
v10 = (char *)v4;
v13 = windowStrings(v4, &v18, "WM_CLASS");
if ( !v18 )
{
LABEL_15:
if ( !v18 )
goto LABEL_6;
goto LABEL_16;
}
v14 = 0LL;
v15 = 0;
while ( 1 )
{
v16 = &v13[v14];
strcpy(dest, &v13[v14]);
v9 = *(unsigned __int64 **)v8;
v10 = key;
v17 = (__int64 *)bsearch(key, *(const void **)v8, *(_QWORD *)(v8 + 8), 0x30uLL, nameCmp);
if ( v17 )
break;
v10 = (char *)v16;
v15 += strlen(v16) + 1;
v14 = v15;
if ( v15 >= v18 )
goto LABEL_15;
}
v6 = *v17;
if ( v18 )
{
LABEL_16:
v10 = v13;
free(v13);
}
if ( v6 == -1 )
goto LABEL_6;
LABEL_5:
v9 = (unsigned __int64 *)v6;
v10 = (char *)v8;
v11 = 0;
if ( !(unsigned int)func7(v8, v6) )
LABEL_6:
v11 = func8(v10, v9);
func2((void *)v8);
return v11;
}

original_code:
int main(int argc, const char *argv[])
{
    (void)argc;
    (void)argv;
    processes_t p;
    long pid;
    int ret = 0;
    Window w = focusedWindow();
    if (w == 0L)
        return getHomeDirectory();
    pid = windowPid(w);
    p = getProcesses();
    if(!p)
        return getHomeDirectory();
    if(pid != -1)
        qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
    else {
        long unsigned int size;
        unsigned int i;
        char* strings;
        struct proc_s *res = ((void *)0), key;
        qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
        strings = windowStrings(w, &size, "WM_CLASS");
        for(i = 0; i < size; i += strlen(strings + i) + 1) {
            do { if (0) fprintf(stderr, "pidof %s\n", strings + i); } while (0);
            strcpy(key.name, strings + i);
            res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                    sizeof(struct proc_s), nameCmp);
            if(res)
                break;
        }
        if (res) {
            pid = res->pid;
            do { if (0) fprintf(stderr, "Found %s (%ld)\n", res->name, res->pid); } while (0);
        }
        if (size)
            free(strings);
    }
    if (pid == -1 || !cwdOfDeepestChild(p, pid))
        ret = getHomeDirectory();
    freeProcesses(p);
    return ret;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    (void)argc;
    (void)argv;
    struct processes_s *p;
    long pid;
    int ret = 0;
    unsigned long w = focusedWindow();
    if (w == 0L)
        return getHomeDirectory();
    pid = windowPid(w);
    p = getProcesses();
    if(!p)
        return getHomeDirectory();
    if(pid != -1)
        qsort(p->ps, p->n, sizeof(struct proc_s), ppidCmp);
    else {
        unsigned long size;
        unsigned int i;
        char *strings;
        struct proc_s *res = ((void *)0); struct proc_s key;
        qsort(p->ps, p->n, sizeof(struct proc_s), nameCmp);
        strings = windowStrings(w, &size, "WM_CLASS");
        for(i = 0; i < size; i += strlen(strings + i) + 1) {
            do { if (0) fprintf(stderr, "pidof %s\n", strings + i); } while (0);
            strcpy(key.name, strings + i);
            res = (struct proc_s *)bsearch(&key, p->ps, p->n,
                    sizeof(struct proc_s), nameCmp);
            if(res)
                break;
        }
        if (res) {
            pid = res->pid;
            do { if (0) fprintf(stderr, "Found %s (%ld)\n", res->name, res->pid); } while (0);
        }
        if (size)
            free(strings);
    }
    if (pid == -1 || !cwdOfDeepestChild(p, pid))
        ret = getHomeDirectory();
    freeProcesses(p);
    return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ret int

variable_types:
w
unsigned long

variable_types:
size
unsigned long

variable_types:
i
unsigned int

variable_types:
strings
char *

variable_types: struct proc_s { long pid; long ppid; char name[32]; }


repo: schmijos/dancepads

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
int v4; // ebp
struct sockaddr v6; // [rsp+0h] [rbp-88h] BYREF

v3 = socket(1, 1, 0);
if ( (v3 & 0x80000000) != 0 )
errorExit("socket");
v6.sa_family = 1;
strcpy(v6.sa_data, "/tmp/dancepad_socket");
v4 = strlen(v6.sa_data);
printf("Found Socket %d\n", v3);
if ( connect(v3, &v6, v4 + 2) )
errorExit("connect");
puts("Choose Pad by Number:");
copyData(v3, 0LL);
printf("Pad Status: ");
copyData(1LL, v3);
puts("\nSend Command to Pad:");
copyData(v3, 0LL);
close(v3);
puts("Connection closed.");
return 0;
}

original_code:
int main(int argc, char *argv[]) {
 struct sockaddr_un strAddr;
 socklen_t lenAddr;
 int fdSock;
 if ((fdSock = socket(1, SOCK_STREAM, 0)) < 0)
  errorExit("socket");
 strAddr.sun_family = 1;
 strcpy(strAddr.sun_path, "/tmp/dancepad_socket");
 lenAddr = sizeof(strAddr.sun_family) + strlen(strAddr.sun_path);
 printf("Found Socket %d\n", fdSock);
 if (connect(fdSock, (struct sockaddr*) &strAddr, lenAddr) != 0)
  errorExit("connect");
 printf("Choose Pad by Number:\n");
 copyData(fdSock, 0);
 printf("Pad Status: ");
 copyData(1, fdSock);
 printf("\nSend Command to Pad:\n");
 copyData(fdSock, 0);
 close(fdSock);
 printf("Connection closed.\n");
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 struct sockaddr_un strAddr;
 unsigned int lenAddr;
 int fdSock;
 if ((fdSock = socket(1, SOCK_STREAM, 0)) < 0)
  errorExit("socket");
 strAddr.sun_family = 1;
 strcpy(strAddr.sun_path, "/tmp/dancepad_socket");
 lenAddr = sizeof(strAddr.sun_family) + strlen(strAddr.sun_path);
 printf("Found Socket %d\n", fdSock);
 if (connect(fdSock, (struct sockaddr *) &strAddr, lenAddr) != 0)
  errorExit("connect");
 printf("Choose Pad by Number:\n");
 copyData(fdSock, 0);
 printf("Pad Status: ");
 copyData(1, fdSock);
 printf("\nSend Command to Pad:\n");
 copyData(fdSock, 0);
 close(fdSock);
 printf("Connection closed.\n");
 return 0;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types:
lenAddr
unsigned int

variable_types: fdSock int

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


decompiled_code:
int uds_listen()
{
int v0; // ebp
int v1; // eax
int v2; // ebx
char v4[2]; // [rsp+Eh] [rbp-49Ah] BYREF
char buf[1036]; // [rsp+10h] [rbp-498h] BYREF
socklen_t addr_len; // [rsp+41Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+420h] [rbp-88h] BYREF

v0 = socket(1, 1, 0);
if ( v0 < 0 )
errorExit("socket");
unlink("/tmp/dancepad_socket");
addr.sa_family = 1;
strcpy(addr.sa_data, "/tmp/dancepad_socket");
addr_len = strlen(addr.sa_data) + 2;
if ( bind(v0, &addr, addr_len) )
errorExit("bind");
if ( listen(v0, 5) )
errorExit("listen");
printf("uds server initialized with socket %d\n", (unsigned int)v0);
while ( 1 )
{
v1 = accept(v0, &addr, &addr_len);
v2 = v1;
if ( v1 < 0 )
break;
if ( read(v1, buf, 8uLL) < 0 )
errorExit("read");
printf("Read: %s\n", buf);
if ( write(v2, "42", 3uLL) < 0 )
errorExit("write");
printf("Wrote: %s\n", "42");
if ( read(v2, v4, 2uLL) < 0 )
errorExit("read");
printf("uds got %s for Pad %s\n", v4, buf);
close(v2);
}
return close(v0);
}

canonical_decompiled_code:
int func0()
{
int v0; // ebp
int v1; // eax
int v2; // ebx
char v4[2]; // [rsp+Eh] [rbp-49Ah] BYREF
char buf[1036]; // [rsp+10h] [rbp-498h] BYREF
socklen_t addr_len; // [rsp+41Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+420h] [rbp-88h] BYREF

v0 = socket(1, 1, 0);
if ( v0 < 0 )
errorExit("socket");
unlink("/tmp/dancepad_socket");
addr.sa_family = 1;
strcpy(addr.sa_data, "/tmp/dancepad_socket");
addr_len = strlen(addr.sa_data) + 2;
if ( bind(v0, &addr, addr_len) )
errorExit("bind");
if ( listen(v0, 5) )
errorExit("listen");
printf("uds server initialized with socket %d\n", (unsigned int)v0);
while ( 1 )
{
v1 = accept(v0, &addr, &addr_len);
v2 = v1;
if ( v1 < 0 )
break;
if ( read(v1, buf, 8uLL) < 0 )
errorExit("read");
printf("Read: %s\n", buf);
if ( write(v2, "42", 3uLL) < 0 )
errorExit("write");
printf("Wrote: %s\n", "42");
if ( read(v2, v4, 2uLL) < 0 )
errorExit("read");
printf("uds got %s for Pad %s\n", v4, buf);
close(v2);
}
return close(v0);
}

original_code:
void uds_listen(dp_exchange_t* p) {
 struct sockaddr_un strAddr;
 socklen_t lenAddr;
 int fdSock;
 int fdConn;
 if ((fdSock = socket(1, SOCK_STREAM, 0)) < 0)
  errorExit("socket");
 unlink("/tmp/dancepad_socket");
 strAddr.sun_family = 1;
 strcpy(strAddr.sun_path, "/tmp/dancepad_socket");
 lenAddr = sizeof(strAddr.sun_family) + strlen(strAddr.sun_path);
 if (bind(fdSock, (struct sockaddr*) &strAddr, lenAddr) != 0)
  errorExit("bind");
 if (listen(fdSock, 5) != 0)
  errorExit("listen");
 printf("uds server initialized with socket %d\n", fdSock);
 while ((fdConn = accept(fdSock, (struct sockaddr*) &strAddr, &lenAddr)) >= 0) {
  char padNr[1024];
  if (read(fdConn, padNr, 8) < 0) {
   errorExit("read");
  }
  printf("Read: %s\n", padNr);
  char* padStatus = "42";
  if (write(fdConn, padStatus, 3) < 0) {
   errorExit("write");
  }
  printf("Wrote: %s\n", padStatus);
  char padCmd[2];
  if (read(fdConn, padCmd, 2) < 0) {
   errorExit("read");
  }
  printf("uds got %s for Pad %s\n", padCmd, padNr);
  close(fdConn);
 }
 close(fdSock);
}

canonical_original_code:
void uds_listen(struct dp_exchange *p) {
 struct sockaddr_un strAddr;
 unsigned int lenAddr;
 int fdSock;
 int fdConn;
 if ((fdSock = socket(1, SOCK_STREAM, 0)) < 0)
  errorExit("socket");
 unlink("/tmp/dancepad_socket");
 strAddr.sun_family = 1;
 strcpy(strAddr.sun_path, "/tmp/dancepad_socket");
 lenAddr = sizeof(strAddr.sun_family) + strlen(strAddr.sun_path);
 if (bind(fdSock, (struct sockaddr *) &strAddr, lenAddr) != 0)
  errorExit("bind");
 if (listen(fdSock, 5) != 0)
  errorExit("listen");
 printf("uds server initialized with socket %d\n", fdSock);
 while ((fdConn = accept(fdSock, (struct sockaddr *) &strAddr, &lenAddr)) >= 0) {
  char padNr[1024];
  if (read(fdConn, padNr, 8) < 0) {
   errorExit("read");
  }
  printf("Read: %s\n", padNr);
  char *padStatus = "42";
  if (write(fdConn, padStatus, 3) < 0) {
   errorExit("write");
  }
  printf("Wrote: %s\n", padStatus);
  char padCmd[2];
  if (read(fdConn, padCmd, 2) < 0) {
   errorExit("read");
  }
  printf("uds got %s for Pad %s\n", padCmd, padNr);
  close(fdConn);
 }
 close(fdSock);
}

variable_types: struct dp_exchange { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned int padnr; union dp_command { unsigned int is_rgb; struct { unsigned int; unsigned int r; unsigned int g; unsigned int b; } rgb; unsigned char bytes[2]; unsigned int is_not_empty; } command; union dp_status { unsigned int is_pressed; struct { unsigned int; unsigned int value; } data; unsigned char byte; } status; } *

variable_types: fdConn int

variable_types:
padNr
char [1024]

variable_types:
padStatus
char *

variable_types:
padCmd
char [2]

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

user_defined_types: struct dp_exchange { union pthread_mutex_t mutex; unsigned int padnr; union dp_command command; union dp_status status; }

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

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


errorExit

decompiled_code:
void __fastcall __noreturn errorExit(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void errorExit(char* msg) {
 perror(msg);
 exit(1);
}

canonical_original_code:
void errorExit(char *msg) {
 perror(msg);
 exit(1);
}

variable_types:
msg
char *


copyData

decompiled_code:
ssize_t __fastcall copyData(int fd, int a2)
{
ssize_t result; // rax
_BYTE v3[1048]; // [rsp+0h] [rbp-418h] BYREF

while ( 1 )
{
result = read(a2, v3, 0x400uLL);
if ( (int)result <= 0 )
break;
if ( write(fd, v3, (int)result) != (int)result )
errorExit("write");
}
if ( (int)result < 0 )
errorExit("read");
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func1(int fd, int a2)
{
ssize_t result; // rax
_BYTE v3[1048]; // [rsp+0h] [rbp-418h] BYREF

while ( 1 )
{
result = read(a2, v3, 0x400uLL);
if ( (int)result <= 0 )
break;
if ( write(fd, v3, (int)result) != (int)result )
func0("write");
}
if ( (int)result < 0 )
func0("read");
return result;
}

original_code:
void copyData(int iDest, int iSrc) {
 char acBuff[1024];
 int iCount;
 while ((iCount = read(iSrc, acBuff, sizeof(acBuff))) > 0) {
  if (write(iDest, acBuff, iCount) != iCount)
   errorExit("write");
 }
 if (iCount < 0)
  errorExit("read");
 return;
}

variable_types: iDest int

variable_types: iSrc int

variable_types:
acBuff
char [1024]

variable_types: iCount int


onSignal

repo: schoentoon/mtop

decompiled_code:
void __noreturn onSignal()
{
event_base_free(event_base);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
event_base_free(event_base);
exit(0);
}

original_code:
void onSignal(int signal) {
  event_base_free(event_base);
  exit(0);
}

variable_types: signal int


usage

decompiled_code:
__int64 __fastcall usage(const char *a1)
{
fprintf(stderr, "USAGE: %s [options]\n", a1);
fwrite("-h, --help\tShow this help message.\n", 1uLL, 0x23uLL, stderr);
fwrite("-C, --config\tUse this config file.\n", 1uLL, 0x23uLL, stderr);
fwrite("-D, --debug\tIncrease the debug level\n", 1uLL, 0x25uLL, stderr);
fwrite(
" \tYou can also directly set a certain debug level with -D5 where 5 is the debug level.\n",
1uLL,
0x61uLL,
stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
fprintf(stderr, "USAGE: %s [options]\n", a1);
fwrite("-h, --help\tShow this help message.\n", 1uLL, 0x23uLL, stderr);
fwrite("-C, --config\tUse this config file.\n", 1uLL, 0x23uLL, stderr);
fwrite("-D, --debug\tIncrease the debug level\n", 1uLL, 0x25uLL, stderr);
fwrite(
" \tYou can also directly set a certain debug level with -D5 where 5 is the debug level.\n",
1uLL,
0x61uLL,
stderr);
return 0LL;
}

original_code:
int usage(char* program) {
  fprintf(stderr, "USAGE: %s [options]\n", program);
  fprintf(stderr, "-h, --help\tShow this help message.\n");
  fprintf(stderr, "-C, --config\tUse this config file.\n");
  fprintf(stderr, "-D, --debug\tIncrease the debug level\n");
  fprintf(stderr, "           \tYou can also directly set a certain debug level with -D5 where 5 is the debug level.\n");
  return 0;
}

canonical_original_code:
int usage(char *program) {
  fprintf(stderr, "USAGE: %s [options]\n", program);
  fprintf(stderr, "-h, --help\tShow this help message.\n");
  fprintf(stderr, "-C, --config\tUse this config file.\n");
  fprintf(stderr, "-D, --debug\tIncrease the debug level\n");
  fprintf(stderr, "           \tYou can also directly set a certain debug level with -D5 where 5 is the debug level.\n");
  return 0;
}

variable_types:
program
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int result; // eax
int *v5; // r12
unsigned __int64 v6; // rax
int v7; // edx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "hD::C:", &options, longind);
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'D':
if ( optarg )
{
v5 = _errno_location();
*v5 = 0;
v6 = strtol(optarg, 0LL, 0);
if ( *v5 || v6 > 0xFF )
{
fprintf(stderr, "%ld is an invalid debug level.\n", v6);
return 1;
}
debug = v6;
}
else
{
++debug;
}
break;
case 'h':
return usage(*argv);
case 'C':
result = parse_config(optarg);
if ( result )
return result;
break;
}
}
event_base = event_base_new();
v7 = dispatch_config(event_base);
result = 1;
if ( !v7 )
{
signal(15, (__sighandler_t)onSignal);
signal(2, (__sighandler_t)onSignal);
while ( 1 )
event_base_dispatch(event_base);
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int result; // eax
int *v5; // r12
unsigned __int64 v6; // rax
int v7; // edx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
v3 = getopt_long(argc, (char *const *)argv, "hD::C:", &options, longind);
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'D':
if ( optarg )
{
v5 = _errno_location();
*v5 = 0;
v6 = strtol(optarg, 0LL, 0);
if ( *v5 || v6 > 0xFF )
{
fprintf(stderr, "%ld is an invalid debug level.\n", v6);
return 1;
}
debug = v6;
}
else
{
++debug;
}
break;
case 'h':
return func1(*argv);
case 'C':
result = parse_config(optarg);
if ( result )
return result;
break;
}
}
event_base = event_base_new();
v7 = dispatch_config(event_base);
result = 1;
if ( !v7 )
{
signal(15, (__sighandler_t)onSignal);
signal(2, (__sighandler_t)onSignal);
while ( 1 )
event_base_dispatch(event_base);
}
return result;
}

original_code:
int main(int argc, char** argv) {
  int arg, optindex;
  while ((arg = getopt_long(argc, argv, "hD::C:", options, &optindex)) != -1) {
    switch (arg) {
    case 'h':
      return usage(argv[0]);
    case 'D':
      if (optarg) {
        (*__errno_location ()) = 0;
        long tmp = strtol(optarg, ((void *)0), 0);
        if ((*__errno_location ()) == 0 && tmp < 256 && tmp >= 0)
          debug = (unsigned char) tmp;
        else {
          fprintf(stderr, "%ld is an invalid debug level.\n", tmp);
          return 1;
        }
      } else
        debug++;
      break;
    case 'C': {
      int result = parse_config(optarg);
      if (result)
        return result;
      break;
    }
    }
  }
  event_base = event_base_new();
  if (dispatch_config(event_base))
    return 1;
  signal(15, onSignal);
  signal(2, onSignal);
  for (;;)
    event_base_dispatch(event_base);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  int arg; int optindex;
  while ((arg = getopt_long(argc, argv, "hD::C:", options, &optindex)) != -1) {
    switch (arg) {
    case 'h':
      return usage(argv[0]);
    case 'D':
      if (optarg) {
        (*__errno_location ()) = 0;
        long tmp = strtol(optarg, ((void *)0), 0);
        if ((*__errno_location ()) == 0 && tmp < 256 && tmp >= 0)
          debug = (unsigned char) tmp;
        else {
          fprintf(stderr, "%ld is an invalid debug level.\n", tmp);
          return 1;
        }
      } else
        debug++;
      break;
    case 'C': {
      int result = parse_config(optarg);
      if (result)
        return result;
      break;
    }
    }
  }
  event_base = event_base_new();
  if (dispatch_config(event_base))
    return 1;
  signal(15, onSignal);
  signal(2, onSignal);
  for (;;)
    event_base_dispatch(event_base);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: arg int

variable_types: optindex int

variable_types: tmp long

variable_types: result int


listAliases

decompiled_code:
void *listAliases()
{
int v0; // eax
void *v1; // r13
int v2; // er12
size_t v3; // rbx
__int64 i; // rbx
char v6[8232]; // [rsp+0h] [rbp-2028h] BYREF

v0 = count_cpus();
v1 = 0LL;
if ( v0 >= 0 )
{
v2 = v0;
v3 = 8LL * (v0 + 1);
v1 = malloc(v3);
memset(v1, 0, v3);
if ( v2 > 0 )
{
for ( i = 0LL; i != v2; *((_QWORD *)v1 + i++) = strdup(v6) )
snprintf(v6, 0x2000uLL, "cpu%d", (unsigned int)i);
}
}
return v1;
}

canonical_decompiled_code:
void *func9()
{
int v0; // eax
void *v1; // r13
int v2; // er12
size_t v3; // rbx
__int64 i; // rbx
char v6[8232]; // [rsp+0h] [rbp-2028h] BYREF

v0 = func8();
v1 = 0LL;
if ( v0 >= 0 )
{
v2 = v0;
v3 = 8LL * (v0 + 1);
v1 = malloc(v3);
memset(v1, 0, v3);
if ( v2 > 0 )
{
for ( i = 0LL; i != v2; *((_QWORD *)v1 + i++) = strdup(v6) )
snprintf(v6, 0x2000uLL, "cpu%d", (unsigned int)i);
}
}
return v1;
}

original_code:
char** listAliases() {
  int cpu_count = count_cpus();
  if (cpu_count < 0)
    return ((void *)0);
  char** output = malloc(sizeof(char*) * (cpu_count + 1));
  memset(output, 0, sizeof(char*) * (cpu_count + 1));
  int i;
  char buf[8192];
  for (i = 0; i < cpu_count; i++) {
    if (snprintf(buf, sizeof(buf), "cpu%d", i))
      output[i] = strdup(buf);
  };
  return output;
}

canonical_original_code:
char **listAliases() {
  int cpu_count = count_cpus();
  if (cpu_count < 0)
    return ((void *)0);
  char **output = malloc(sizeof(char*) * (cpu_count + 1));
  memset(output, 0, sizeof(char *) * (cpu_count + 1));
  int i;
  char buf[8192];
  for (i = 0; i < cpu_count; i++) {
    if (snprintf(buf, sizeof(buf), "cpu%d", i))
      output[i] = strdup(buf);
  };
  return output;
}

variable_types:
output
char **

variable_types: i int

variable_types:
buf
char [8192]

return_type: char **


freeContext

decompiled_code:
void __fastcall freeContext(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func11(void *a1)
{
free(a1);
}

original_code:
void freeContext(void* context) {
  free(context);
}

canonical_original_code:
void freeContext(void *context) {
  free(context);
}

variable_types:
context
void *


getMaxFloat

decompiled_code:
float getMaxFloat()
{
return 100.0;
}

canonical_decompiled_code:
float func13()
{
return 100.0;
}

original_code:
float getMaxFloat(void* context, size_t i) {
  return 100.0f;
}

canonical_original_code:
float getMaxFloat(void *context, unsigned long i) {
  return 100.0f;
}

variable_types:
i
unsigned long

return_type: float


getMinFloat

decompiled_code:
double getMinFloat()
{
return 0.0;
}

canonical_decompiled_code:
double func14()
{
return 0.0;
}

original_code:
float getMinFloat(void* context, size_t i) {
  return 0.0f;
}

canonical_original_code:
float getMinFloat(void *context, unsigned long i) {
  return 0.0f;
}


getFloat

decompiled_code:
__int64 __fastcall getFloat(int *a1, unsigned __int64 a2)
{
FILE *v2; // rbx
int v3; // ebp
__int64 result; // rax
int v5; // [rsp+Ch] [rbp-4Ch]
__int64 v6; // [rsp+10h] [rbp-48h]
__int64 v7; // [rsp+18h] [rbp-40h]
__int64 v8; // [rsp+20h] [rbp-38h]
__int64 v9; // [rsp+28h] [rbp-30h]

v2 = fopen("/proc/stat", "r");
v3 = *a1;
if ( *a1 == -1 )
{
result = __isoc99_fscanf();
if ( (_DWORD)result != 4 )
return result;
}
else
{
while ( fgetc(v2) != 10 )
;
while ( (unsigned int)__isoc99_fscanf() == 5 && v3 != v5 )
{
while ( fgetc(v2) != 10 )
;
}
}
fclose(v2);
result = v6 + v7 + v9 + v8;
if ( a2 == 2 )
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 3) = v7;
}
else if ( a2 > 2 )
{
if ( a2 == 3 )
{
*((_QWORD *)a1 + 4) = v6;
*((_QWORD *)a1 + 5) = result;
}
}
else if ( a2 )
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 2) = v8;
}
else
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 1) = v9;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(int *a1, unsigned __int64 a2)
{
FILE *v2; // rbx
int v3; // ebp
__int64 result; // rax
int v5; // [rsp+Ch] [rbp-4Ch]
__int64 v6; // [rsp+10h] [rbp-48h]
__int64 v7; // [rsp+18h] [rbp-40h]
__int64 v8; // [rsp+20h] [rbp-38h]
__int64 v9; // [rsp+28h] [rbp-30h]

v2 = fopen("/proc/stat", "r");
v3 = *a1;
if ( *a1 == -1 )
{
result = __isoc99_fscanf();
if ( (_DWORD)result != 4 )
return result;
}
else
{
while ( fgetc(v2) != 10 )
;
while ( (unsigned int)__isoc99_fscanf() == 5 && v3 != v5 )
{
while ( fgetc(v2) != 10 )
;
}
}
fclose(v2);
result = v6 + v7 + v9 + v8;
if ( a2 == 2 )
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 3) = v7;
}
else if ( a2 > 2 )
{
if ( a2 == 3 )
{
*((_QWORD *)a1 + 4) = v6;
*((_QWORD *)a1 + 5) = result;
}
}
else if ( a2 )
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 2) = v8;
}
else
{
result -= *((_QWORD *)a1 + 5);
if ( result < 0 )
result &= 1u;
*((_QWORD *)a1 + 1) = v9;
}
return result;
}

original_code:
float getFloat(void* context, size_t i) {
  return 0.0;
}

canonical_original_code:
float getFloat(void *context, unsigned long i) {
  return 0.0;
}


createContext

decompiled_code:
_QWORD *createContext()
{
_QWORD *v0; // rbx

v0 = malloc(0x30uLL);
*v0 = 0LL;
v0[1] = 0LL;
v0[2] = 0LL;
v0[3] = 0LL;
v0[4] = 0LL;
v0[5] = 0LL;
if ( (unsigned int)__isoc99_sscanf() != 1 )
*(_DWORD *)v0 = -1;
getFloat(v0, 0LL);
getFloat(v0, 1LL);
getFloat(v0, 2LL);
getFloat(v0, 3LL);
return v0;
}

original_code:
void* createContext(char* name) {
  struct CPUData* output = malloc(sizeof(struct CPUData));
  memset(output, 0, sizeof(struct CPUData));
  if (sscanf(name, "cpu%d", &output->cpu) != 1)
    output->cpu = -1;
  getFloat((void*) output, 0);
  getFloat((void*) output, 1);
  getFloat((void*) output, 2);
  getFloat((void*) output, 3);
  return output;
}

canonical_original_code:
void *createContext(char *name) {
  struct CPUData *output = malloc(sizeof(struct CPUData));
  memset(output, 0, sizeof(struct CPUData));
  if (sscanf(name, "cpu%d", &output->cpu) != 1)
    output->cpu = -1;
  getFloat((void *) output, 0);
  getFloat((void *) output, 1);
  getFloat((void *) output, 2);
  getFloat((void *) output, 3);
  return output;
}

variable_types:
name
char *

variable_types: struct CPUData { int cpu; unsigned long long userTime; unsigned long long niceTime; unsigned long long systemTime; unsigned long long idleTime; unsigned long long totalTime; } *


maxInterval

decompiled_code:
__int64 maxInterval()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func17()
{
return 1LL;
}

original_code:
unsigned char maxInterval(void* context) {
  return 1;
}

canonical_original_code:
unsigned char maxInterval(void *context) {
  return 1;
}

return_type: unsigned char


getItemName

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

result = "system";
if ( a2 != 2 )
{
if ( a2 > 2 )
{
result = "idle";
if ( a2 != 3 )
result = 0LL;
}
else
{
result = "nice";
if ( !a2 )
result = "user";
}
}
return result;
}

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

result = "system";
if ( a2 != 2 )
{
if ( a2 > 2 )
{
result = "idle";
if ( a2 != 3 )
result = 0LL;
}
else
{
result = "nice";
if ( !a2 )
result = "user";
}
}
return result;
}

original_code:
char* getItemName(void* context, size_t item) {
  switch (item) {
  case 0:
    return "user";
  case 1:
    return "nice";
  case 2:
    return "system";
  case 3:
    return "idle";
  };
  return ((void *)0);
}

canonical_original_code:
char *getItemName(void *context, unsigned long item) {
  switch (item) {
  case 0:
    return "user";
  case 1:
    return "nice";
  case 2:
    return "system";
  case 3:
    return "idle";
  };
  return ((void *)0);
}

variable_types:
item
unsigned long

return_type: char *


getArrayLength

decompiled_code:
__int64 getArrayLength()
{
return 4LL;
}

canonical_decompiled_code:
__int64 func10()
{
return 4LL;
}

original_code:
size_t getArrayLength(void* context) {
  return 4;
}

canonical_original_code:
unsigned long getArrayLength(void *context) {
  return 4;
}


decompiled_code:
__int64 __fastcall get_module(char *s1)
{
__int64 i; // rbx

for ( i = *(_QWORD *)(config + 8); i; i = *(_QWORD *)(i + 72) )
{
if ( !strcmp(s1, *(const char **)(i + 16)) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1)
{
__int64 i; // rbx

for ( i = *(_QWORD *)(config + 8); i; i = *(_QWORD *)(i + 72) )
{
if ( !strcmp(s1, *(const char **)(i + 16)) )
break;
}
return i;
}

original_code:
struct module* get_module(char* name) {
  struct module* m = config->modules;
  while (m) {
    if (strcmp(name, m->name) == 0)
      return m;
    m = m->next;
  };
  return ((void *)0);
}

canonical_original_code:
struct module *get_module(char *name) {
  struct module *m = config->modules;
  while (m) {
    if (strcmp(name, m->name) == 0)
      return m;
    m = m->next;
  };
  return ((void *)0);
}

variable_types: struct module { void *handle; void *context; char *name; enum module_type { FLOAT=0, FLOAT_RANGE=1 } type; void *module_data; unsigned long array_length; void *update_function; unsigned char max_interval; long last_update; struct module *next; } *

user_defined_types: struct module { void *handle; void *context; char *name; enum module_type type; void *module_data; unsigned long array_length; void *update_function; unsigned char max_interval; long last_update; struct module *next; }


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

result = "FLOAT";
if ( a1 )
{
result = (const char *)&unk_8B0;
if ( a1 == 1 )
result = "FLOAT_RANGE";
}
return result;
}

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

result = "FLOAT";
if ( a1 )
{
result = (const char *)&unk_8B0;
if ( a1 == 1 )
result = "FLOAT_RANGE";
}
return result;
}

original_code:
char* module_type_to_string(module_type type) {
  switch (type) {
  case FLOAT:
    return "FLOAT";
  case FLOAT_RANGE:
    return "FLOAT_RANGE";
  }
  return "";
}

canonical_original_code:
char *module_type_to_string(enum module_type type) {
  switch (type) {
  case FLOAT:
    return "FLOAT";
  case FLOAT_RANGE:
    return "FLOAT_RANGE";
  }
  return "";
}

variable_types: enum module_type { FLOAT=0, FLOAT_RANGE=1 }


decompiled_code:
void __fastcall free_module(void *ptr)
{
void (__fastcall *v1)(_QWORD); // rax
int v2; // eax
unsigned __int64 v3; // rbp

if ( ptr )
{
if ( *((_QWORD *)ptr + 1) )
{
v1 = (void (__fastcall *)(_QWORD))dlsym(*(void **)ptr, "freeContext");
if ( v1 )
v1(*((_QWORD *)ptr + 1));
}
dlclose(*(void **)ptr);
free(*((void **)ptr + 2));
v2 = *((_DWORD *)ptr + 6);
if ( v2 )
{
if ( v2 != 1 )
{
LABEL_11:
free(ptr);
return;
}
if ( *((_QWORD *)ptr + 5) )
{
v3 = 0LL;
do
free(*(void **)(*((_QWORD *)ptr + 4) + 8 * v3++));
while ( *((_QWORD *)ptr + 5) > v3 );
}
}
free(*((void **)ptr + 4));
goto LABEL_11;
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void (__fastcall *v1)(_QWORD); // rax
int v2; // eax
unsigned __int64 v3; // rbp

if ( ptr )
{
if ( *((_QWORD *)ptr + 1) )
{
v1 = (void (__fastcall *)(_QWORD))dlsym(*(void **)ptr, "freeContext");
if ( v1 )
v1(*((_QWORD *)ptr + 1));
}
dlclose(*(void **)ptr);
free(*((void **)ptr + 2));
v2 = *((_DWORD *)ptr + 6);
if ( v2 )
{
if ( v2 != 1 )
{
LABEL_11:
free(ptr);
return;
}
if ( *((_QWORD *)ptr + 5) )
{
v3 = 0LL;
do
free(*(void **)(*((_QWORD *)ptr + 4) + 8 * v3++));
while ( *((_QWORD *)ptr + 5) > v3 );
}
}
free(*((void **)ptr + 4));
goto LABEL_11;
}
}

original_code:
void free_module(struct module* module) {
  if (module) {
    if (module->context) {
      mod_free_context* free_context = dlsym(module->handle, "freeContext");
      if (free_context)
        free_context(module->context);
    }
    dlclose(module->handle);
    free(module->name);
    switch (module->type) {
    case FLOAT_RANGE: {
      size_t i;
      for (i = 0; i < module->array_length; i++)
        free(((struct float_range_data**) module->module_data)[i]);
    }
    case FLOAT:
      free(module->module_data);
      break;
    };
    free(module);
  }
}

canonical_original_code:
void free_module(struct module *module) {
  if (module) {
    if (module->context) {
      void (*free_context)(void *context) = dlsym(module->handle, "freeContext");
      if (free_context)
        free_context(module->context);
    }
    dlclose(module->handle);
    free(module->name);
    switch (module->type) {
    case FLOAT_RANGE: {
      unsigned long i;
      for (i = 0; i < module->array_length; i++)
        free(((struct float_range_data **) module->module_data)[i]);
    }
    case FLOAT:
      free(module->module_data);
      break;
    };
    free(module);
  }
}

user_defined_types: enum module_type { FLOAT=0, FLOAT_RANGE=1 }

user_defined_types: struct float_range_data { float current_float; float max_value; float min_value; float (*mod_min_func)(void *context, unsigned long item); float (*mod_max_func)(void *context, unsigned long item); }


decompiled_code:
char *__fastcall get_module_item_names(__int64 a1, char *a2, __int64 a3)
{
__int64 (__fastcall *v4)(_QWORD, unsigned __int64); // rax
char *v5; // rbx
__int64 (__fastcall *v6)(_QWORD, unsigned __int64); // r15
char *v7; // r13
unsigned __int64 v8; // rbp
const char *v9; // rax

v4 = (__int64 (__fastcall *)(_QWORD, unsigned __int64))dlsym(*(void **)a1, "getItemName");
v5 = a2;
if ( v4 )
{
v6 = v4;
if ( *(_QWORD *)(a1 + 40) )
{
v7 = &a2[a3];
v8 = 0LL;
do
{
v9 = (const char *)v6(*(_QWORD *)(a1 + 8), v8);
v5 += snprintf(v5, v7 - v5, "%s", v9);
if ( *(_QWORD *)(a1 + 40) - 1LL != v8 )
v5 += snprintf(v5, v7 - v5, ", ");
++v8;
}
while ( *(_QWORD *)(a1 + 40) > v8 );
}
}
return (char *)(v5 - a2);
}

canonical_decompiled_code:
char *__fastcall func4(__int64 a1, char *a2, __int64 a3)
{
__int64 (__fastcall *v4)(_QWORD, unsigned __int64); // rax
char *v5; // rbx
__int64 (__fastcall *v6)(_QWORD, unsigned __int64); // r15
char *v7; // r13
unsigned __int64 v8; // rbp
const char *v9; // rax

v4 = (__int64 (__fastcall *)(_QWORD, unsigned __int64))dlsym(*(void **)a1, "getItemName");
v5 = a2;
if ( v4 )
{
v6 = v4;
if ( *(_QWORD *)(a1 + 40) )
{
v7 = &a2[a3];
v8 = 0LL;
do
{
v9 = (const char *)v6(*(_QWORD *)(a1 + 8), v8);
v5 += snprintf(v5, v7 - v5, "%s", v9);
if ( *(_QWORD *)(a1 + 40) - 1LL != v8 )
v5 += snprintf(v5, v7 - v5, ", ");
++v8;
}
while ( *(_QWORD *)(a1 + 40) > v8 );
}
}
return (char *)(v5 - a2);
}

original_code:
size_t get_module_item_names(struct module* module, char* buf, size_t buf_size) {
  char *s = buf;
  char *e = buf + buf_size;
  mod_item_name* item_name_func = dlsym(module->handle, "getItemName");
  if (item_name_func) {
    size_t i;
    for (i = 0; i < module->array_length; i++) {
      s += snprintf(s, e - s, "%s", item_name_func(module->context, i));
      if (i != (module->array_length - 1))
        s += snprintf(s, e - s, ", ");
    }
  }
  return s - buf;
}

canonical_original_code:
unsigned long get_module_item_names(struct module *module, char *buf, unsigned long buf_size) {
  char *s = buf;
  char *e = buf + buf_size;
  char *(*item_name_func)(void *context, unsigned long item) = dlsym(module->handle, "getItemName");
  if (item_name_func) {
    unsigned long i;
    for (i = 0; i < module->array_length; i++) {
      s += snprintf(s, e - s, "%s", item_name_func(module->context, i));
      if (i != (module->array_length - 1))
        s += snprintf(s, e - s, ", ");
    }
  }
  return s - buf;
}

variable_types:
buf
char *

variable_types:
s
char *

variable_types:
e
char *

variable_types: char *(*)(void *context, unsigned long item)


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

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

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

result = malloc(0x18uLL);
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
return result;
}

original_code:
struct websocket* new_websocket() {
  struct websocket* output = malloc(sizeof(struct websocket));
  memset(output, 0, sizeof(struct websocket));
  return output;
}

canonical_original_code:
struct websocket *new_websocket() {
  struct websocket *output = malloc(sizeof(struct websocket));
  memset(output, 0, sizeof(struct websocket));
  return output;
}

variable_types: struct websocket { char *key; unsigned char is_get; unsigned char is_http_1_1; unsigned char has_upgrade; unsigned char connection_is_upgrade; unsigned char correct_version; unsigned char connected; unsigned char length_code; char mask[4]; unsigned char has_mask; unsigned long length; } *


decompiled_code:
void __fastcall free_websocket(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void free_websocket(struct websocket* websocket) {
  if (websocket) {
    free(websocket->key);
    free(websocket);
  };
}

canonical_original_code:
void free_websocket(struct websocket *websocket) {
  if (websocket) {
    free(websocket->key);
    free(websocket);
  };
}

user_defined_types: struct websocket { char *key; unsigned char is_get; unsigned char is_http_1_1; unsigned char has_upgrade; unsigned char connection_is_upgrade; unsigned char correct_version; unsigned char connected; unsigned char length_code; char mask[4]; unsigned char has_mask; unsigned long length; }


repo: schoentoon/smnl

original_code:
uint32_t eth_parse(struct pcap_pkthdr *header, uint8_t *packet,
                   eth_info * eth) {
    uint32_t pos = 0;
    if (header->len < 14) {
        fprintf(stderr, "Truncated Packet(eth)\n");
        return 0;
    }
    while (pos < 6) {
        eth->dstmac[pos] = packet[pos];
        eth->srcmac[pos] = packet[pos+6];
        pos++;
    }
    pos = pos + 6;
    if (packet[pos] == 0x81 && packet[pos+1] == 0) pos = pos + 4;
    eth->ethtype = (packet[pos] << 8) + packet[pos+1];
    pos = pos + 2;

    return pos;
}

canonical_original_code:
unsigned int eth_parse(struct pcap_pkthdr *header, unsigned char *packet, struct eth_info *eth) {
    unsigned int pos = 0;
    if (header->len < 14) {
        fprintf(stderr, "Truncated Packet(eth)\n");
        return 0;
    }
    while (pos < 6) {
        eth->dstmac[pos] = packet[pos];
        eth->srcmac[pos] = packet[pos+6];
        pos++;
    }
    pos = pos + 6;
    if (packet[pos] == 0x81 && packet[pos+1] == 0) pos = pos + 4;
    eth->ethtype = (packet[pos] << 8) + packet[pos+1];
    pos = pos + 2;

    return pos;
}

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
packet
unsigned char *

variable_types: struct eth_info { unsigned char dstmac[6]; unsigned char srcmac[6]; unsigned short ethtype; } *

variable_types:
pos
unsigned int

return_type: unsigned int

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


decompiled_code:
__int64 __fastcall mpls_parse(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned int v6; // ecx
unsigned int v7; // eax
char v8; // al
__int64 result; // rax

v6 = *(_DWORD *)(a2 + 20);
do
{
v7 = a1;
a1 += 4;
if ( v6 < a1 )
{
fwrite("Truncated Packet(mpls)\n", 1uLL, 0x17uLL, stderr);
return 0LL;
}
}
while ( (*(_BYTE *)(a3 + v7 + 2) & 1) == 0 );
v8 = *(_BYTE *)(a3 + a1) >> 4;
if ( v8 == 4 )
{
*(_WORD *)(a4 + 12) = 2048;
result = a1;
}
else
{
if ( v8 == 6 )
*(_WORD *)(a4 + 12) = -31011;
else
*(_WORD *)(a4 + 12) = 0;
result = a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned int v6; // ecx
unsigned int v7; // eax
char v8; // al
__int64 result; // rax

v6 = *(_DWORD *)(a2 + 20);
do
{
v7 = a1;
a1 += 4;
if ( v6 < a1 )
{
fwrite("Truncated Packet(mpls)\n", 1uLL, 0x17uLL, stderr);
return 0LL;
}
}
while ( (*(_BYTE *)(a3 + v7 + 2) & 1) == 0 );
v8 = *(_BYTE *)(a3 + a1) >> 4;
if ( v8 == 4 )
{
*(_WORD *)(a4 + 12) = 2048;
result = a1;
}
else
{
if ( v8 == 6 )
*(_WORD *)(a4 + 12) = -31011;
else
*(_WORD *)(a4 + 12) = 0;
result = a1;
}
return result;
}

original_code:
uint32_t mpls_parse(uint32_t pos, struct pcap_pkthdr *header,
                    uint8_t *packet, eth_info * eth) {
    uint8_t bos;
    do {

        if (header->len < (pos + 4)) {
            fprintf(stderr, "Truncated Packet(mpls)\n");
            return 0;
        }
        bos = packet[pos + 2] & 0x01;
        pos += 4;

    } while (bos == 0);
    if (header->len < pos) {
        fprintf(stderr, "Truncated Packet(post mpls)\n");
        return 0;
    }
    uint8_t ip_ver = packet[pos] >> 4;
    switch (ip_ver) {
        case 0x04:
            eth->ethtype = 0x0800; break;
        case 0x06:
            eth->ethtype = 0x86DD; break;
        default:
            eth->ethtype = 0;
    }
    return pos;
}

canonical_original_code:
unsigned int mpls_parse(unsigned int pos, struct pcap_pkthdr *header, unsigned char *packet, struct eth_info *eth) {
    unsigned char bos;
    do {

        if (header->len < (pos + 4)) {
            fprintf(stderr, "Truncated Packet(mpls)\n");
            return 0;
        }
        bos = packet[pos + 2] & 0x01;
        pos += 4;

    } while (bos == 0);
    if (header->len < pos) {
        fprintf(stderr, "Truncated Packet(post mpls)\n");
        return 0;
    }
    unsigned char ip_ver = packet[pos] >> 4;
    switch (ip_ver) {
        case 0x04:
            eth->ethtype = 0x0800; break;
        case 0x06:
            eth->ethtype = 0x86DD; break;
        default:
            eth->ethtype = 0;
    }
    return pos;
}

variable_types:
bos
unsigned char

user_defined_types: struct eth_info { unsigned char dstmac[6]; unsigned char srcmac[6]; unsigned short ethtype; }

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


original_code:
ip_fragment * ip_frag_add(ip_fragment * this, config * conf) {
    ip_fragment ** curr = &(conf->ip_fragment_head);
    ip_fragment ** found = ((void *)0);

    while (*curr != ((void *)0)) {
        if ((*curr)->id == this->id &&
            (((*curr)->src.vers == this->src.vers) && ((*curr)->src.vers == 0x04 ? (*curr)->src.addr.v4.s_addr == this->src.addr.v4.s_addr : (((*curr)->src.addr.v6.__in6_u.__u6_addr32[0] == this->src.addr.v6.__in6_u.__u6_addr32[0]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[1] == this->src.addr.v6.__in6_u.__u6_addr32[1]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[2] == this->src.addr.v6.__in6_u.__u6_addr32[2]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[3] == this->src.addr.v6.__in6_u.__u6_addr32[3])) )) &&
            (((*curr)->dst.vers == this->dst.vers) && ((*curr)->dst.vers == 0x04 ? (*curr)->dst.addr.v4.s_addr == this->dst.addr.v4.s_addr : (((*curr)->dst.addr.v6.__in6_u.__u6_addr32[0] == this->dst.addr.v6.__in6_u.__u6_addr32[0]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[1] == this->dst.addr.v6.__in6_u.__u6_addr32[1]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[2] == this->dst.addr.v6.__in6_u.__u6_addr32[2]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[3] == this->dst.addr.v6.__in6_u.__u6_addr32[3])) ))) {
            found = curr;

            break;
        }
        curr = &(*curr)->next;
    }
    if (found == ((void *)0)) {

        this->next = conf->ip_fragment_head;
        conf->ip_fragment_head = this;
        return ((void *)0);
    }
    while (*found != ((void *)0)) {

        if ((*found)->start >= this->end) {

            this->child = *found;
            this->next = (*found)->next;
            *found = this;
            break;
        } else if ((*found)->child == ((void *)0) &&
                    (*found)->end <= this->start) {

            (*found)->child = this;
            break;
        }

        found = &((*found)->child);
    }

    if (*found == ((void *)0)) {

        free(this);
        return ((void *)0);
    }
    found = curr;
    while ((*found != ((void *)0)) && (*found)->child != ((void *)0)) {
        ip_fragment * child = (*found)->child;
        if ((*found)->end == child->start) {

            uint32_t child_len = child->end - child->start;
            uint32_t fnd_len = (*found)->end - (*found)->start;
            uint8_t * buff = malloc(sizeof(uint8_t) * (fnd_len + child_len));
            memcpy(buff, (*found)->data, fnd_len);
            memcpy(buff + fnd_len, child->data, child_len);
            (*found)->end = (*found)->end + child_len;
            (*found)->islast = child->islast;
            (*found)->child = child->child;
            free((*found)->data);
            free(child->data);
            free(child);
            (*found)->data = buff;
        } else {
            found = &(*found)->child;
        }
    }

    if ((*curr)->islast != 0) {
        ip_fragment * ret = *curr;
        *curr = (*curr)->next;

        return ret;
    }
    return ((void *)0);
}

canonical_original_code:
struct ip_fragment *ip_frag_add(struct ip_fragment *this, struct config *conf) {
    struct ip_fragment **curr = &(conf->ip_fragment_head);
    struct ip_fragment **found = ((void *)0);

    while (*curr != ((void *)0)) {
        if ((*curr)->id == this->id &&
            (((*curr)->src.vers == this->src.vers) && ((*curr)->src.vers == 0x04 ? (*curr)->src.addr.v4.s_addr == this->src.addr.v4.s_addr : (((*curr)->src.addr.v6.__in6_u.__u6_addr32[0] == this->src.addr.v6.__in6_u.__u6_addr32[0]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[1] == this->src.addr.v6.__in6_u.__u6_addr32[1]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[2] == this->src.addr.v6.__in6_u.__u6_addr32[2]) && ((*curr)->src.addr.v6.__in6_u.__u6_addr32[3] == this->src.addr.v6.__in6_u.__u6_addr32[3])) )) &&
            (((*curr)->dst.vers == this->dst.vers) && ((*curr)->dst.vers == 0x04 ? (*curr)->dst.addr.v4.s_addr == this->dst.addr.v4.s_addr : (((*curr)->dst.addr.v6.__in6_u.__u6_addr32[0] == this->dst.addr.v6.__in6_u.__u6_addr32[0]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[1] == this->dst.addr.v6.__in6_u.__u6_addr32[1]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[2] == this->dst.addr.v6.__in6_u.__u6_addr32[2]) && ((*curr)->dst.addr.v6.__in6_u.__u6_addr32[3] == this->dst.addr.v6.__in6_u.__u6_addr32[3])) ))) {
            found = curr;

            break;
        }
        curr = &(*curr)->next;
    }
    if (found == ((void *)0)) {

        this->next = conf->ip_fragment_head;
        conf->ip_fragment_head = this;
        return ((void *)0);
    }
    while (*found != ((void *)0)) {

        if ((*found)->start >= this->end) {

            this->child = *found;
            this->next = (*found)->next;
            *found = this;
            break;
        } else if ((*found)->child == ((void *)0) &&
                    (*found)->end <= this->start) {

            (*found)->child = this;
            break;
        }

        found = &((*found)->child);
    }

    if (*found == ((void *)0)) {

        free(this);
        return ((void *)0);
    }
    found = curr;
    while ((*found != ((void *)0)) && (*found)->child != ((void *)0)) {
        struct ip_fragment *child = (*found)->child;
        if ((*found)->end == child->start) {

            unsigned int child_len = child->end - child->start;
            unsigned int fnd_len = (*found)->end - (*found)->start;
            unsigned char *buff = malloc(sizeof(uint8_t) * (fnd_len + child_len));
            memcpy(buff, (*found)->data, fnd_len);
            memcpy(buff + fnd_len, child->data, child_len);
            (*found)->end = (*found)->end + child_len;
            (*found)->islast = child->islast;
            (*found)->child = child->child;
            free((*found)->data);
            free(child->data);
            free(child);
            (*found)->data = buff;
        } else {
            found = &(*found)->child;
        }
    }

    if ((*curr)->islast != 0) {
        struct ip_fragment *ret = *curr;
        *curr = (*curr)->next;

        return ret;
    }
    return ((void *)0);
}

variable_types:
this
struct ip_fragment *

variable_types: struct config { unsigned short EXCLUDED[100]; unsigned short EXCLUDES; char SEP; char *RECORD_SEP; int AD_ENABLED; int NS_ENABLED; int COUNTS; int PRETTY_DATE; int PRINT_RR_NAME; int MISSING_TYPE_WARNINGS; char *TCP_STATE_PATH; unsigned int DEDUPS; struct tcp_info { struct timeval { long tv_sec; long tv_usec; } ts; struct ip_addr src; struct ip_addr dst; unsigned short srcport; unsigned short dstport; unsigned int sequence; unsigned int ack_num; unsigned int len; unsigned char syn; unsigned char ack; unsigned char fin; unsigned char rst; unsigned char *data; struct tcp_info *next_sess; struct tcp_info *next_pkt; struct tcp_info *prev_pkt; } *tcp_sessions_head; struct ip_fragment *ip_fragment_head; unsigned long long *dedup_hashes; unsigned int dedup_pos; } *

variable_types: struct ip_fragment { unsigned int id; struct ip_addr { unsigned char vers; union { struct in_addr { unsigned int s_addr; } v4; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } v6; } addr; } src; struct ip_addr dst; unsigned int start; unsigned int end; unsigned char *data; unsigned char islast; struct ip_fragment *next; struct ip_fragment *child; } **

variable_types:
buff
unsigned char *

user_defined_types: struct config { unsigned short EXCLUDED[100]; unsigned short EXCLUDES; char SEP; char *RECORD_SEP; int AD_ENABLED; int NS_ENABLED; int COUNTS; int PRETTY_DATE; int PRINT_RR_NAME; int MISSING_TYPE_WARNINGS; char *TCP_STATE_PATH; unsigned int DEDUPS; struct tcp_info *tcp_sessions_head; struct ip_fragment *ip_fragment_head; unsigned long long *dedup_hashes; unsigned int dedup_pos; }

user_defined_types: struct ip_fragment { unsigned int id; struct ip_addr src; struct ip_addr dst; unsigned int start; unsigned int end; unsigned char *data; unsigned char islast; struct ip_fragment *next; struct ip_fragment *child; }

user_defined_types: struct ip_addr { unsigned char vers; union { struct in_addr v4; struct in6_addr v6; } addr; }

user_defined_types: struct tcp_info { struct timeval ts; struct ip_addr src; struct ip_addr dst; unsigned short srcport; unsigned short dstport; unsigned int sequence; unsigned int ack_num; unsigned int len; unsigned char syn; unsigned char ack; unsigned char fin; unsigned char rst; unsigned char *data; struct tcp_info *next_sess; struct tcp_info *next_pkt; struct tcp_info *prev_pkt; }


original_code:
uint32_t ipv4_parse(uint32_t pos, struct pcap_pkthdr *header,
                    uint8_t ** p_packet, ip_info * ip, config * conf) {
    uint32_t h_len;
    ip_fragment * frag = ((void *)0);
    uint8_t frag_mf;
    uint16_t frag_offset;
    uint8_t * packet = *p_packet;
    if (header-> len - pos < 20) {
        fprintf(stderr, "Truncated Packet(ipv4)\n");
        p_packet = ((void *)0);
        return 0;
    }
    h_len = packet[pos] & 0x0f;
    ip->length = (packet[pos+2] << 8) + packet[pos+3] - h_len*4;
    ip->proto = packet[pos+9];
    ip->src.addr.v4.s_addr = *(in_addr_t *)(packet + pos + 12); ip->src.vers = 0x04;;
    ip->dst.addr.v4.s_addr = *(in_addr_t *)(packet + pos + 16); ip->dst.vers = 0x04;;
    frag_mf = (packet[pos+6] & 0x20) >> 5;
    frag_offset = ((packet[pos+6] & 0x1f) << 11) + (packet[pos+7] << 3);

    if (frag_mf == 1 || frag_offset != 0) {

        frag = malloc(sizeof(ip_fragment));
        frag->start = frag_offset;
        frag->islast = !frag_mf;
        frag->id = *((uint16_t *)(packet + pos + 4));
        frag->src = ip->src;
        frag->dst = ip->dst;
        frag->end = frag->start + ip->length;
        frag->data = malloc(sizeof(uint8_t) * ip->length);
        frag->next = frag->child = ((void *)0);
        memcpy(frag->data, packet + pos + 4*h_len, ip->length);
        frag = ip_frag_add(frag, conf);
        if (frag != ((void *)0)) {
            header->len = ip->length = frag->end - frag->start;
            p_packet = &(frag->data);
            free(frag);
            return 0;
        }
        p_packet = ((void *)0);
        return 0;
    }
    return pos + 4*h_len;
}

canonical_original_code:
unsigned int ipv4_parse(unsigned int pos, struct pcap_pkthdr *header, unsigned char **p_packet, struct ip_info *ip, struct config *conf) {
    unsigned int h_len;
    struct ip_fragment *frag = ((void *)0);
    unsigned char frag_mf;
    unsigned short frag_offset;
    unsigned char *packet = *p_packet;
    if (header-> len - pos < 20) {
        fprintf(stderr, "Truncated Packet(ipv4)\n");
        p_packet = ((void *)0);
        return 0;
    }
    h_len = packet[pos] & 0x0f;
    ip->length = (packet[pos+2] << 8) + packet[pos+3] - h_len*4;
    ip->proto = packet[pos+9];
    ip->src.addr.v4.s_addr = *(unsigned int *)(packet + pos + 12); ip->src.vers = 0x04;;
    ip->dst.addr.v4.s_addr = *(unsigned int *)(packet + pos + 16); ip->dst.vers = 0x04;;
    frag_mf = (packet[pos+6] & 0x20) >> 5;
    frag_offset = ((packet[pos+6] & 0x1f) << 11) + (packet[pos+7] << 3);

    if (frag_mf == 1 || frag_offset != 0) {

        frag = malloc(sizeof(ip_fragment));
        frag->start = frag_offset;
        frag->islast = !frag_mf;
        frag->id = *((unsigned short *)(packet + pos + 4));
        frag->src = ip->src;
        frag->dst = ip->dst;
        frag->end = frag->start + ip->length;
        frag->data = malloc(sizeof(unsigned char) * ip->length);
        frag->next = frag->child = ((void *)0);
        memcpy(frag->data, packet + pos + 4*h_len, ip->length);
        frag = ip_frag_add(frag, conf);
        if (frag != ((void *)0)) {
            header->len = ip->length = frag->end - frag->start;
            p_packet = &(frag->data);
            free(frag);
            return 0;
        }
        p_packet = ((void *)0);
        return 0;
    }
    return pos + 4*h_len;
}

variable_types: struct ip_info { struct ip_addr { unsigned char vers; union { struct in_addr { unsigned int s_addr; } v4; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } v6; } addr; } src; struct ip_addr dst; unsigned int length; unsigned char proto; } *

variable_types: struct config { unsigned short EXCLUDED[100]; unsigned short EXCLUDES; char SEP; char *RECORD_SEP; int AD_ENABLED; int NS_ENABLED; int COUNTS; int PRETTY_DATE; int PRINT_RR_NAME; int MISSING_TYPE_WARNINGS; char *TCP_STATE_PATH; unsigned int DEDUPS; struct tcp_info { struct timeval ts; struct ip_addr src; struct ip_addr dst; unsigned short srcport; unsigned short dstport; unsigned int sequence; unsigned int ack_num; unsigned int len; unsigned char syn; unsigned char ack; unsigned char fin; unsigned char rst; unsigned char *data; struct tcp_info *next_sess; struct tcp_info *next_pkt; struct tcp_info *prev_pkt; } *tcp_sessions_head; struct ip_fragment { unsigned int id; struct ip_addr src; struct ip_addr dst; unsigned int start; unsigned int end; unsigned char *data; unsigned char islast; struct ip_fragment *next; struct ip_fragment *child; } *ip_fragment_head; unsigned long long *dedup_hashes; unsigned int dedup_pos; } *

variable_types: struct ip_fragment { unsigned int id; struct ip_addr { unsigned char vers; union { struct in_addr { unsigned int s_addr; } v4; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } v6; } addr; } src; struct ip_addr dst; unsigned int start; unsigned int end; unsigned char *data; unsigned char islast; struct ip_fragment *next; struct ip_fragment *child; } *

user_defined_types: struct ip_info { struct ip_addr src; struct ip_addr dst; unsigned int length; unsigned char proto; }