decompiled_code:
void __fastcall destroy_queue(void *ptr)
{
_QWORD *v2; // rdi
_QWORD *i; // rbx

v2 = *(_QWORD **)ptr;
for ( i = (_QWORD *)v2[1]; i; i = (_QWORD *)i[1] )
{
free(v2);
v2 = i;
}
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *v2; // rdi
_QWORD *i; // rbx

v2 = *(_QWORD **)ptr;
for ( i = (_QWORD *)v2[1]; i; i = (_QWORD *)i[1] )
{
free(v2);
v2 = i;
}
free(v2);
free(ptr);
}

original_code:
void destroy_queue(queue *q) {
 elem *current = q->head;
 elem *nxt = current->next;
 while (nxt != ((void *)0)) {
  free(current);
  current = nxt;
  nxt = current->next;
 }
 free(current);
 free(q);
}

canonical_original_code:
void destroy_queue(struct queue *q) {
 struct elem *current = q->head;
 struct elem *nxt = current->next;
 while (nxt != ((void *)0)) {
  free(current);
  current = nxt;
  nxt = current->next;
 }
 free(current);
 free(q);
}

user_defined_types: struct elem { void *content; struct elem *next; }


enqueue

repo: shellygr/SwProj

decompiled_code:
__int64 __fastcall enqueue(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8) = a2;
*(_QWORD *)(a1 + 8) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8) = a2;
*(_QWORD *)(a1 + 8) = a2;
return result;
}

original_code:
void enqueue(queue* q, elem* elm) {
 (q->tail)->next = elm;
 q->tail = elm;
}

canonical_original_code:
void enqueue(struct queue *q, struct elem *elm) {
 (q->tail)->next = elm;
 q->tail = elm;
}

variable_types: struct queue { struct elem { void *content; struct elem *next; } *head; struct elem *tail; } *

variable_types:
elm
struct elem *

return_type: void

user_defined_types: struct queue { struct elem *head; struct elem *tail; }


decompiled_code:
__int64 __fastcall is_empty(_QWORD *a1)
{
return (a1[1] == *a1) & (unsigned __int8)(*a1 == 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1)
{
return (a1[1] == *a1) & (unsigned __int8)(*a1 == 0LL);
}

original_code:
int is_empty(queue* q) {
 if ((q->head == q->tail) && (q->head == ((void *)0)))
  return 1;
 return 0;
}

canonical_original_code:
int is_empty(struct queue *q) {
 if ((q->head == q->tail) && (q->head == ((void *)0)))
  return 1;
 return 0;
}

return_type: int


dequeue

decompiled_code:
__int64 __fastcall dequeue(_QWORD *a1)
{
__int64 result; // rax

if ( (unsigned int)is_empty(a1) )
return 0LL;
result = *a1;
*a1 = *(_QWORD *)(*a1 + 8LL);
return result;
}

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

if ( (unsigned int)func4(a1) )
return 0LL;
result = *a1;
*a1 = *(_QWORD *)(*a1 + 8LL);
return result;
}

original_code:
elem* dequeue(queue* q) {
 elem *dequeued;
 if (is_empty(q)) {
  return ((void *)0);
 }
 dequeued = q->head;
 q->head = (q->head)->next;
 return dequeued;
}

canonical_original_code:
struct elem *dequeue(struct queue *q) {
 struct elem *dequeued;
 if (is_empty(q)) {
  return ((void *)0);
 }
 dequeued = q->head;
 q->head = (q->head)->next;
 return dequeued;
}

variable_types: struct queue { struct elem *head; struct elem *tail; } *

variable_types: struct elem { void *content; struct elem *next; } *


decompiled_code:
__int64 __fastcall is_power_two(unsigned int a1)
{
__int64 result; // rax

result = a1;
if ( a1 )
result = (a1 & (a1 - 1)) == 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
__int64 result; // rax

result = a1;
if ( a1 )
result = (a1 & (a1 - 1)) == 0;
return result;
}

original_code:
int is_power_two(int x) {
 return ((x != 0) && !(x & (x - 1)));
}

variable_types: x int


decompiled_code:
void **__fastcall init_array(void **a1)
{
*a1 = malloc(8uLL);
return a1;
}

canonical_decompiled_code:
void **__fastcall func1(void **a1)
{
*a1 = malloc(8uLL);
return a1;
}

original_code:
dynamic_array* init_array(dynamic_array *arr) {
 *arr = (void **) malloc(sizeof(void *));
 if (arr == ((void *)0)) {
  send_perror("malloc");
 }
 return arr;
}

canonical_original_code:
void ***init_array(void ***arr) {
 *arr = (void **) malloc(sizeof(void *));
 if (arr == ((void *)0)) {
  send_perror("malloc");
 }
 return arr;
}

variable_types:
arr
void ***

return_type: void ***


decompiled_code:
__int64 __fastcall get_element(int a1, __int64 a2)
{
return *(_QWORD *)(a2 + 8LL * a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
return *(_QWORD *)(a2 + 8LL * a1);
}

original_code:
void* get_element(int index, dynamic_array *arr) {
 return (arr[index]);
}

canonical_original_code:
void *get_element(int index, void ***arr) {
 return (arr[index]);
}

variable_types: index int

return_type: void *


decompiled_code:
__int64 __fastcall double_and_add(void *ptr, int *a2, __int64 a3)
{
__int64 v5; // rsi
int v7; // eax
void *v8; // rdi
void *v9; // rax

v5 = *a2;
v7 = is_power_two(v5);
v8 = *(void **)ptr;
if ( !v7 )
goto LABEL_4;
v9 = realloc(v8, 16LL * (_DWORD)v5);
v8 = v9;
if ( v9 )
{
*(_QWORD *)ptr = v9;
v5 = *a2;
LABEL_4:
*a2 = v5 + 1;
*((_QWORD *)v8 + v5) = a3;
printf("array size now %d\n", (unsigned int)(v5 + 1));
return 0LL;
}
free(ptr);
send_perror("realloc");
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *ptr, int *a2, __int64 a3)
{
__int64 v5; // rsi
int v7; // eax
void *v8; // rdi
void *v9; // rax

v5 = *a2;
v7 = func0(v5);
v8 = *(void **)ptr;
if ( !v7 )
goto LABEL_4;
v9 = realloc(v8, 16LL * (_DWORD)v5);
v8 = v9;
if ( v9 )
{
*(_QWORD *)ptr = v9;
v5 = *a2;
LABEL_4:
*a2 = v5 + 1;
*((_QWORD *)v8 + v5) = a3;
printf("array size now %d\n", (unsigned int)(v5 + 1));
return 0LL;
}
free(ptr);
send_perror("realloc");
return 2LL;
}

original_code:
int double_and_add(dynamic_array *arr, int *arr_size, void *element) {
 if (is_power_two(*arr_size)) {
  dynamic_array tmp;
  if ((tmp = realloc(*arr, 2 * (*arr_size) * sizeof(void*))) == ((void *)0)) {
   free(arr);
   send_perror("realloc");
   return 2;
  } else {
   *arr = tmp;
  }
 }
 (*arr)[(*arr_size)++] = element;
 printf("array size now %d\n", *arr_size);
 return 0;
}

canonical_original_code:
int double_and_add(void ***arr, int *arr_size, void *element) {
 if (is_power_two(*arr_size)) {
  void **tmp;
  if ((tmp = realloc(*arr, 2 * (*arr_size) * sizeof(void *))) == ((void *)0)) {
   free(arr);
   send_perror("realloc");
   return 2;
  } else {
   *arr = tmp;
  }
 }
 (*arr)[(*arr_size)++] = element;
 printf("array size now %d\n", *arr_size);
 return 0;
}

variable_types:
element
void *

variable_types:
tmp
void **


decompiled_code:
_BOOL8 __fastcall check_one_param(const char *a1)
{
return strpbrk(a1, delim) == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char *a1)
{
return strpbrk(a1, delim) == 0LL;
}

original_code:
int check_one_param(char *param) {
 if (strpbrk(param, " \n\t\v\f\r") != ((void *)0)) {
  return 0;
 }
 return 1;
}

variable_types:
param
char *


decompiled_code:
_BOOL8 __fastcall check_and_get_three_params(char *a1, char **a2, char **a3, char **a4)
{
char *v6; // rax
int v7; // er13

v6 = strtok(a1, delim);
if ( !v6 )
return 0LL;
v7 = 0;
do
{
switch ( ++v7 )
{
case 1:
*a2 = v6;
break;
case 2:
*a3 = v6;
break;
case 3:
*a4 = v6;
break;
}
v6 = strtok(0LL, delim);
}
while ( v6 );
return v7 == 3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char *a1, char **a2, char **a3, char **a4)
{
char *v6; // rax
int v7; // er13

v6 = strtok(a1, delim);
if ( !v6 )
return 0LL;
v7 = 0;
do
{
switch ( ++v7 )
{
case 1:
*a2 = v6;
break;
case 2:
*a3 = v6;
break;
case 3:
*a4 = v6;
break;
}
v6 = strtok(0LL, delim);
}
while ( v6 );
return v7 == 3;
}

original_code:
int check_and_get_three_params(char *params, char **first_param,
  char **second_param, char **weight) {
 char *next_token = strtok(params, " \n\t\v\f\r");
 int count = 0;
 while (next_token != ((void *)0)) {
  count++;
  if (count == 1) {
   *first_param = next_token;
  } else if (count == 2) {
   *second_param = next_token;
  } else if (count == 3)
   *weight = next_token;
  next_token = strtok(((void *)0), " \n\t\v\f\r");
 }
 return (count == 3 ? 1 : 0);
}

variable_types:
params
char *

variable_types:
weight
char **

variable_types: count int


decompiled_code:
_BOOL8 __fastcall check_string_is_non_negative_integer(char *nptr)
{
char *v1; // rbx
size_t v2; // r13
const unsigned __int16 **v3; // rax
__int64 v4; // rdx
int v5; // eax

v1 = nptr;
v2 = strlen(nptr);
while ( v1 != &nptr[v2] )
{
v3 = _ctype_b_loc();
v4 = *v1++;
if ( ((*v3)[v4] & 0x800) == 0 )
return 0LL;
}
v5 = atoi(nptr);
if ( v2 <= 0xA && v5 >= 0 )
return !((v5 == 0x7FFFFFFF) & (memcmp(nptr, "2147483647", 0xAuLL) != 0));
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(char *nptr)
{
char *v1; // rbx
size_t v2; // r13
const unsigned __int16 **v3; // rax
__int64 v4; // rdx
int v5; // eax

v1 = nptr;
v2 = strlen(nptr);
while ( v1 != &nptr[v2] )
{
v3 = _ctype_b_loc();
v4 = *v1++;
if ( ((*v3)[v4] & 0x800) == 0 )
return 0LL;
}
v5 = atoi(nptr);
if ( v2 <= 0xA && v5 >= 0 )
return !((v5 == 0x7FFFFFFF) & (memcmp(nptr, "2147483647", 0xAuLL) != 0));
return 0LL;
}

original_code:
int check_string_is_non_negative_integer(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short int) _ISdigit))
   return 0;
 }
 if (atoi(str) < 0 || strlen(str) > 10)
  return 0;
 else if ((strncmp(str, "2147483647", 10) != 0) && (atoi(str) == 2147483647))
  return 0;
 return 1;
}

canonical_original_code:
int check_string_is_non_negative_integer(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short) _ISdigit))
   return 0;
 }
 if (atoi(str) < 0 || strlen(str) > 10)
  return 0;
 else if ((strncmp(str, "2147483647", 10) != 0) && (atoi(str) == 2147483647))
  return 0;
 return 1;
}

variable_types:
str
char *

variable_types: i int


decompiled_code:
__int64 __fastcall check_string_is_double(char *nptr)
{
int v1; // er12
char *v2; // rbx
char *v3; // rbp
const unsigned __int16 **v4; // rax
__int64 v5; // rcx
double v6; // xmm0_8
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v1 = 0;
v2 = nptr;
v3 = &nptr[strlen(nptr)];
while ( v2 != v3 )
{
v4 = _ctype_b_loc();
v5 = *v2;
if ( ((*v4)[v5] & 0x800) == 0 )
{
if ( v1 || (_BYTE)v5 != 46 )
return 0LL;
v1 = 1;
}
++v2;
}
v6 = strtod(nptr, &endptr);
if ( v6 == INFINITY )
return 0LL;
if ( *endptr )
return 0LL;
if ( v6 > 1.0 )
return 0LL;
result = 1LL;
if ( v6 < 0.0 )
return 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *nptr)
{
int v1; // er12
char *v2; // rbx
char *v3; // rbp
const unsigned __int16 **v4; // rax
__int64 v5; // rcx
double v6; // xmm0_8
__int64 result; // rax
char *endptr; // [rsp+8h] [rbp-30h] BYREF

v1 = 0;
v2 = nptr;
v3 = &nptr[strlen(nptr)];
while ( v2 != v3 )
{
v4 = _ctype_b_loc();
v5 = *v2;
if ( ((*v4)[v5] & 0x800) == 0 )
{
if ( v1 || (_BYTE)v5 != 46 )
return 0LL;
v1 = 1;
}
++v2;
}
v6 = strtod(nptr, &endptr);
if ( v6 == INFINITY )
return 0LL;
if ( *endptr )
return 0LL;
if ( v6 > 1.0 )
return 0LL;
result = 1LL;
if ( v6 < 0.0 )
return 0LL;
return result;
}

original_code:
int check_string_is_double(char *str) {
 int i;
 int flag = 0;
 char *tmp;
 double weight;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short int) _ISdigit)) {
   if (flag == 1) {
    return 0;
   }
   if (*(str + i) == '.') {
    flag = 1;
   } else {
    return 0;
   }
  }
 }
 weight = strtod(str, &tmp);
 if (weight == (__builtin_huge_val ()) || strlen(tmp) > 0 || weight > 1 || weight < 0) {
  return 0;
 }
 return 1;
}

canonical_original_code:
int check_string_is_double(char *str) {
 int i;
 int flag = 0;
 char *tmp;
 double weight;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short) _ISdigit)) {
   if (flag == 1) {
    return 0;
   }
   if (*(str + i) == '.') {
    flag = 1;
   } else {
    return 0;
   }
  }
 }
 weight = strtod(str, &tmp);
 if (weight == (__builtin_huge_val ()) || strlen(tmp) > 0 || weight > 1 || weight < 0) {
  return 0;
 }
 return 1;
}

variable_types: flag int

variable_types:
tmp
char *

variable_types: weight double


decompiled_code:
_BOOL8 __fastcall is_legal_id(int a1, _DWORD *a2)
{
return *a2 > a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(int a1, _DWORD *a2)
{
return *a2 > a1;
}

original_code:
int is_legal_id(int id, network *net) {
 return (id >= net->num_of_vertices ? 0 : 1);
}

canonical_original_code:
int is_legal_id(int id, struct network *net) {
 return (id >= net->num_of_vertices ? 0 : 1);
}

variable_types: id int

variable_types: struct network { int num_of_vertices; void **vertices; } *


decompiled_code:
__int64 __fastcall add_vertex(__int64 a1, unsigned int *a2)
{
__int64 v2; // rax
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = a2;
v2 = init_vertex(a1, *a2);
if ( v2 )
return add_vertex_to_network(&v4, v2);
send_perror("malloc");
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned int *a2)
{
__int64 v2; // rax
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v4 = a2;
v2 = init_vertex(a1, *a2);
if ( v2 )
return add_vertex_to_network(&v4, v2);
send_perror("malloc");
return 2LL;
}

original_code:
int add_vertex(char *name, network *net) {
 vertex *vrtx = init_vertex(name, net->num_of_vertices);
 if (vrtx == ((void *)0)) {
  send_perror("malloc");
  return 2;
 }
 return add_vertex_to_network(&net, vrtx);
}

canonical_original_code:
int add_vertex(char *name, struct network *net) {
 struct vertex *vrtx = init_vertex(name, net->num_of_vertices);
 if (vrtx == ((void *)0)) {
  send_perror("malloc");
  return 2;
 }
 return add_vertex_to_network(&net, vrtx);
}

variable_types:
name
char *

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } *

user_defined_types: struct network { int num_of_vertices; void **vertices; }


decompiled_code:
__int64 __fastcall add_edge(__int64 a1, unsigned int a2, __int64 a3, double a4)
{
__int64 result; // rax
__int64 v6; // [rsp+18h] [rbp-30h] BYREF
__int64 v7; // [rsp+20h] [rbp-28h] BYREF
__int64 v8[4]; // [rsp+28h] [rbp-20h] BYREF

v6 = get_vertex(a1, a3);
v7 = get_vertex(a2, a3);
v8[0] = 0LL;
init_edge(v8, (unsigned int)a1, a2, a4);
result = add_edge_to_vertex(&v6, &v7, v8[0]);
if ( (_DWORD)result != 2 )
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, unsigned int a2, __int64 a3, double a4)
{
__int64 result; // rax
__int64 v6; // [rsp+18h] [rbp-30h] BYREF
__int64 v7; // [rsp+20h] [rbp-28h] BYREF
__int64 v8[4]; // [rsp+28h] [rbp-20h] BYREF

v6 = get_vertex(a1, a3);
v7 = get_vertex(a2, a3);
v8[0] = 0LL;
init_edge(v8, (unsigned int)a1, a2, a4);
result = add_edge_to_vertex(&v6, &v7, v8[0]);
if ( (_DWORD)result != 2 )
result = 0LL;
return result;
}

original_code:
int add_edge(int first_id, int second_id, double weight, network *net) {
 vertex *src_vrtx = get_vertex(first_id, net);
 vertex *dest_vrtx = get_vertex(second_id, net);
 edge *e = ((void *)0);
 init_edge(&e, first_id, second_id, weight);
 if ((add_edge_to_vertex(&src_vrtx, &dest_vrtx, e) == 2))
  return 2;
 return 0;
}

canonical_original_code:
int add_edge(int first_id, int second_id, double weight, struct network *net) {
 struct vertex *src_vrtx = get_vertex(first_id, net);
 struct vertex *dest_vrtx = get_vertex(second_id, net);
 struct edge *e = ((void *)0);
 init_edge(&e, first_id, second_id, weight);
 if ((add_edge_to_vertex(&src_vrtx, &dest_vrtx, e) == 2))
  return 2;
 return 0;
}

variable_types: struct edge { int from; int to; double weight; } *

user_defined_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; }


decompiled_code:
__int64 __fastcall dispatch_add_vertex(char *a1, unsigned int *a2)
{
__int64 v2; // rbp
__int64 result; // rax

v2 = *a1;
if ( !(_BYTE)v2 )
goto LABEL_4;
if ( ((*_ctype_b_loc())[v2] & 0x2000) != 0 )
{
if ( !check_one_param(a1 + 1) )
{
LABEL_4:
send_error(14LL);
return 1LL;
}
result = add_vertex((__int64)(a1 + 1), a2);
}
else
{
send_error(3LL);
result = 1LL;
}
return result;
}

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

v2 = *a1;
if ( !(_BYTE)v2 )
goto LABEL_4;
if ( ((*_ctype_b_loc())[v2] & 0x2000) != 0 )
{
if ( !func0(a1 + 1) )
{
LABEL_4:
send_error(14LL);
return 1LL;
}
result = func5((__int64)(a1 + 1), a2);
}
else
{
send_error(3LL);
result = 1LL;
}
return result;
}

original_code:
int dispatch_add_vertex(char *params, network *net) {
 if (params[0] == '\0')
  send_error(14);
 else if (!((*__ctype_b_loc ())[(int) ((params[0]))] & (unsigned short int) _ISspace))
  send_error(3);
 else if (check_one_param(params + 1) == 0) {
  send_error(14);
 } else {
  return add_vertex(params + 1, net);
 }
 return 1;
}

canonical_original_code:
int dispatch_add_vertex(char *params, struct network *net) {
 if (params[0] == '\0')
  send_error(14);
 else if (!((*__ctype_b_loc ())[(int) ((params[0]))] & (unsigned short) _ISspace))
  send_error(3);
 else if (check_one_param(params + 1) == 0) {
  send_error(14);
 } else {
  return add_vertex(params + 1, net);
 }
 return 1;
}


decompiled_code:
__int64 __fastcall dispatch_add_edge(char *a1, _DWORD *a2)
{
__int64 v2; // rbp
__int64 result; // rax
unsigned int v4; // ebp
int v5; // er13
double v6; // xmm0_8
char *nptr; // [rsp+8h] [rbp-40h] BYREF
char *v8; // [rsp+10h] [rbp-38h] BYREF
char *v9; // [rsp+18h] [rbp-30h] BYREF

v2 = *a1;
nptr = 0LL;
v8 = 0LL;
if ( !(_BYTE)v2 )
goto LABEL_8;
if ( ((*_ctype_b_loc())[v2] & 0x2000) == 0 )
{
send_error(3LL);
return 1LL;
}
if ( check_and_get_three_params(a1 + 1, &nptr, &v8, &v9) )
{
if ( check_string_is_non_negative_integer(nptr) && check_string_is_non_negative_integer(v8) )
{
if ( (unsigned int)check_string_is_double(v9) )
{
v4 = atoi(nptr);
v5 = atoi(v8);
v6 = strtod(v9, 0LL);
if ( is_legal_id(v4, a2) && is_legal_id(v5, a2) )
{
if ( v4 == v5 )
{
send_error(8LL);
result = 1LL;
}
else
{
result = add_edge(v4, v5, (__int64)a2, v6);
}
}
else
{
send_error(7LL);
result = 1LL;
}
}
else
{
send_error(4294967292LL);
result = 1LL;
}
}
else
{
send_error(5LL);
result = 1LL;
}
}
else
{
LABEL_8:
send_error(24LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, _DWORD *a2)
{
__int64 v2; // rbp
__int64 result; // rax
unsigned int v4; // ebp
int v5; // er13
double v6; // xmm0_8
char *nptr; // [rsp+8h] [rbp-40h] BYREF
char *v8; // [rsp+10h] [rbp-38h] BYREF
char *v9; // [rsp+18h] [rbp-30h] BYREF

v2 = *a1;
nptr = 0LL;
v8 = 0LL;
if ( !(_BYTE)v2 )
goto LABEL_8;
if ( ((*_ctype_b_loc())[v2] & 0x2000) == 0 )
{
send_error(3LL);
return 1LL;
}
if ( func1(a1 + 1, &nptr, &v8, &v9) )
{
if ( func2(nptr) && func2(v8) )
{
if ( (unsigned int)func3(v9) )
{
v4 = atoi(nptr);
v5 = atoi(v8);
v6 = strtod(v9, 0LL);
if ( func4(v4, a2) && func4(v5, a2) )
{
if ( v4 == v5 )
{
send_error(8LL);
result = 1LL;
}
else
{
result = func6(v4, v5, (__int64)a2, v6);
}
}
else
{
send_error(7LL);
result = 1LL;
}
}
else
{
send_error(4294967292LL);
result = 1LL;
}
}
else
{
send_error(5LL);
result = 1LL;
}
}
else
{
LABEL_8:
send_error(24LL);
result = 1LL;
}
return result;
}

original_code:
int dispatch_add_edge(char *params, network *net) {
 char *first_param = ((void *)0), *second_param = ((void *)0), *third_param;
 if (params[0] == '\0')
  send_error(24);
 else if (!((*__ctype_b_loc ())[(int) ((params[0]))] & (unsigned short int) _ISspace))
  send_error(3);
 else if (check_and_get_three_params(params + 1, &first_param, &second_param,
   &third_param) == 0) {
  send_error(24);
  return 1;
 } else if ((check_string_is_non_negative_integer(first_param) == 0)
   || (check_string_is_non_negative_integer(second_param) == 0)) {
  send_error(5);
  return 1;
 } else if (!check_string_is_double(third_param)) {
  send_error(-4);
  return 1;
 } else {
  int first_id = atoi(first_param);
  int second_id = atoi(second_param);
  double weight = strtod(third_param, ((void *)0));
  if ((is_legal_id(first_id, net) == 0)
    || (is_legal_id(second_id, net) == 0)) {
   send_error(7);
   return 1;
  } else if (first_id == second_id) {
   send_error(8);
   return 1;
  } else {
   return add_edge(first_id, second_id, weight, net);
  }
 }
 return 1;
}

canonical_original_code:
int dispatch_add_edge(char *params, struct network *net) {
 char *first_param = ((void *)0); char *second_param = ((void *)0); char *third_param;
 if (params[0] == '\0')
  send_error(24);
 else if (!((*__ctype_b_loc ())[(int) ((params[0]))] & (unsigned short) _ISspace))
  send_error(3);
 else if (check_and_get_three_params(params + 1, &first_param, &second_param,
   &third_param) == 0) {
  send_error(24);
  return 1;
 } else if ((check_string_is_non_negative_integer(first_param) == 0)
   || (check_string_is_non_negative_integer(second_param) == 0)) {
  send_error(5);
  return 1;
 } else if (!check_string_is_double(third_param)) {
  send_error(-4);
  return 1;
 } else {
  int first_id = atoi(first_param);
  int second_id = atoi(second_param);
  double weight = strtod(third_param, ((void *)0));
  if ((is_legal_id(first_id, net) == 0)
    || (is_legal_id(second_id, net) == 0)) {
   send_error(7);
   return 1;
  } else if (first_id == second_id) {
   send_error(8);
   return 1;
  } else {
   return add_edge(first_id, second_id, weight, net);
  }
 }
 return 1;
}


decompiled_code:
__int64 __fastcall dispatch_command(int a1, char *a2, unsigned int *a3)
{
if ( a1 == 1 )
return dispatch_add_vertex(a2, a3);
if ( a1 == 2 )
return dispatch_add_edge(a2, a3);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, char *a2, unsigned int *a3)
{
if ( a1 == 1 )
return func7(a2, a3);
if ( a1 == 2 )
return func8(a2, a3);
return 1LL;
}

original_code:
int dispatch_command(int code, char *params, network *net) {
 switch (code) {
 case 1:
  return dispatch_add_vertex(params, net);
 case 2:
  return dispatch_add_edge(params, net);
 }
 return 1;
}

canonical_original_code:
int dispatch_command(int code, char *params, struct network *net) {
 switch (code) {
 case 1:
  return dispatch_add_vertex(params, net);
 case 2:
  return dispatch_add_edge(params, net);
 }
 return 1;
}

variable_types: code int


decompiled_code:
__int64 __fastcall init_network(void *ptr)
{
char *v1; // rax
__int64 v2; // rax
__int64 result; // rax

v1 = (char *)malloc(0x10uLL);
*(_QWORD *)ptr = v1;
if ( !v1 )
return 2LL;
v2 = *(_QWORD *)init_array(v1 + 8);
*(_QWORD *)(*(_QWORD *)ptr + 8LL) = v2;
if ( v2 )
{
**(_DWORD **)ptr = 0;
result = 0LL;
}
else
{
free(ptr);
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
char *v1; // rax
__int64 v2; // rax
__int64 result; // rax

v1 = (char *)malloc(0x10uLL);
*(_QWORD *)ptr = v1;
if ( !v1 )
return 2LL;
v2 = *(_QWORD *)init_array(v1 + 8);
*(_QWORD *)(*(_QWORD *)ptr + 8LL) = v2;
if ( v2 )
{
**(_DWORD **)ptr = 0;
result = 0LL;
}
else
{
free(ptr);
result = 2LL;
}
return result;
}

original_code:
int init_network(network **net) {
 if ((*net = (network*) malloc(sizeof(network))) == ((void *)0))
  return 2;
 if (((*net)->vertices = *init_array(&(*net)->vertices)) == ((void *)0)) {
  free(net);
  return 2;
 }
 (*net)->num_of_vertices = 0;
 return 0;
}

canonical_original_code:
int init_network(struct network **net) {
 if ((*net = (struct network *) malloc(sizeof(network))) == ((void *)0))
  return 2;
 if (((*net)->vertices = *init_array(&(*net)->vertices)) == ((void *)0)) {
  free(net);
  return 2;
 }
 (*net)->num_of_vertices = 0;
 return 0;
}

variable_types: struct network { int num_of_vertices; void **vertices; } **


decompiled_code:
void __fastcall destroy_net(void *ptr)
{
__int64 v2; // rbx
__int64 v3; // rdi

if ( *(int *)ptr > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 1) + 8 * v2++);
destroy_vertex(v3);
}
while ( *(_DWORD *)ptr > (int)v2 );
}
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v2; // rbx
__int64 v3; // rdi

if ( *(int *)ptr > 0 )
{
v2 = 0LL;
do
{
v3 = *(_QWORD *)(*((_QWORD *)ptr + 1) + 8 * v2++);
destroy_vertex(v3);
}
while ( *(_DWORD *)ptr > (int)v2 );
}
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void destroy_net(network *net) {
 int i;
 for (i = 0; i < net->num_of_vertices; i++) {
  destroy_vertex((net->vertices[i]));
 }
 free(net->vertices);
 free(net);
}

canonical_original_code:
void destroy_net(struct network *net) {
 int i;
 for (i = 0; i < net->num_of_vertices; i++) {
  destroy_vertex((net->vertices[i]));
 }
 free(net->vertices);
 free(net);
}


decompiled_code:
__int64 __fastcall get_vertex(int a1, __int64 a2)
{
return *(_QWORD *)(*(_QWORD *)(a2 + 8) + 8LL * a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
return *(_QWORD *)(*(_QWORD *)(a2 + 8) + 8LL * a1);
}

original_code:
vertex* get_vertex(int id, network *net) {
 return (net->vertices[id]);
}

canonical_original_code:
struct vertex *get_vertex(int id, struct network *net) {
 return (net->vertices[id]);
}

return_type: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } *


decompiled_code:
__int64 __fastcall add_vertex_to_network(_QWORD *a1, __int64 a2)
{
return double_and_add(*a1 + 8LL, *a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2)
{
return double_and_add(*a1 + 8LL, *a1, a2);
}

original_code:
int add_vertex_to_network(network **net, vertex *vrtx) {
 int i;
 i = double_and_add(&((*net)->vertices), &((*net)->num_of_vertices), vrtx);
 return i;
}

canonical_original_code:
int add_vertex_to_network(struct network **net, struct vertex *vrtx) {
 int i;
 i = double_and_add(&((*net)->vertices), &((*net)->num_of_vertices), vrtx);
 return i;
}


original_code:
void get_prices_array(network *net, double *prices) {
 int i, k;
 vertex currVertex;
 int counter = 0;
 vertex *vertices = (vertex*) (net->vertices);
 int from, to;
 edge **currNeighList;
 for (i = 0; i < net->num_of_vertices; i++) {
  currVertex = vertices[i];
  currNeighList = (edge **)currVertex.adjacency_list;
  for (k = 0; k < currVertex.deg; k++) {
   from = currNeighList[k]->from;
   to = currNeighList[k]->to;
   if ((from == i) && (to > i)) {
    prices[counter + to - i] = currNeighList[k]->weight;
   } else {
    if ((to == i) && (from > i)) {
     prices[counter + from - i] = currNeighList[k]->weight;
    }
   }
  }
  counter += (net->num_of_vertices - i - 1);
 }
}

canonical_original_code:
void get_prices_array(struct network *net, double *prices) {
 int i; int k;
 struct vertex currVertex;
 int counter = 0;
 struct vertex *vertices = (vertex*) (net->vertices);
 int from; int to;
 struct edge **currNeighList;
 for (i = 0; i < net->num_of_vertices; i++) {
  currVertex = vertices[i];
  currNeighList = (struct edge **)currVertex.adjacency_list;
  for (k = 0; k < currVertex.deg; k++) {
   from = currNeighList[k]->from;
   to = currNeighList[k]->to;
   if ((from == i) && (to > i)) {
    prices[counter + to - i] = currNeighList[k]->weight;
   } else {
    if ((to == i) && (from > i)) {
     prices[counter + from - i] = currNeighList[k]->weight;
    }
   }
  }
  counter += (net->num_of_vertices - i - 1);
 }
}

variable_types:
prices
double *

variable_types: k int

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; }

variable_types: counter int

variable_types: from int

variable_types: to int

variable_types: struct edge { int from; int to; double weight; } **


original_code:
void get_id_array(int nV, int ***id) {
 int i, j;
 int counter = 0;
 for (i = 0; i < nV; i++) {
  for (j = 0; j < nV - i - 1; j++) {
   (*id)[i][j] = counter++;
  }
 }
}

canonical_original_code:
void get_id_array(int nV, int ***id) {
 int i; int j;
 int counter = 0;
 for (i = 0; i < nV; i++) {
  for (j = 0; j < nV - i - 1; j++) {
   (*id)[i][j] = counter++;
  }
 }
}

variable_types: nV int

variable_types:
id
int ***

variable_types: i int

variable_types: j int


decompiled_code:
void __fastcall send_perror(const char *a1)
{
char v1[88]; // [rsp+0h] [rbp-58h] BYREF

sprintf(v1, "Error: standard function %s has failed\n", a1);
perror(v1);
}

canonical_decompiled_code:
void __fastcall func0(const char *a1)
{
char v1[88]; // [rsp+0h] [rbp-58h] BYREF

sprintf(v1, "Error: standard function %s has failed\n", a1);
perror(v1);
}

original_code:
void send_perror(char *func) {
 char error_text[80];
 sprintf(error_text, "Error: standard function %s has failed\n", func);
 perror(error_text);
}

variable_types:
func
char *


decompiled_code:
int __fastcall send_error(int a1)
{
__int64 v1; // rdx
const char *v2; // rsi
__int64 v3; // rdx
__int64 v5; // rdi
char v6[19]; // [rsp+1h] [rbp-13h] BYREF

if ( a1 <= 0 )
{
v5 = (unsigned int)(a1 + 4);
v2 = 0LL;
if ( (unsigned int)v5 <= 4 )
return printf("%s", CSWTCH_2[v5]);
return printf("%s", v2);
}
v1 = (unsigned int)(a1 % 10 - 1);
if ( (unsigned int)v1 > 8 )
{
v2 = 0LL;
return printf("%s", v2);
}
v2 = CSWTCH_3[v1];
if ( a1 % 10 != 4 )
return printf("%s", v2);
v3 = (unsigned int)((a1 - 4) / 10);
switch ( (int)v3 )
{
case 1:
strcpy(v6, "add_vertex");
break;
case 2:
v3 = 3LL;
strcpy(v6, "add_edge");
break;
case 3:
strcpy(v6, "print");
v3 = 0LL;
break;
case 4:
v3 = 0LL;
strcpy(v6, "page_rank");
break;
case 5:
strcpy(v6, "quit");
v3 = 0LL;
break;
default:
v3 = 0LL;
break;
}
return printf("Error: command %s must have %d parameters\n", v6, v3);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
__int64 v1; // rdx
const char *v2; // rsi
__int64 v3; // rdx
__int64 v5; // rdi
char v6[19]; // [rsp+1h] [rbp-13h] BYREF

if ( a1 <= 0 )
{
v5 = (unsigned int)(a1 + 4);
v2 = 0LL;
if ( (unsigned int)v5 <= 4 )
return printf("%s", CSWTCH_2[v5]);
return printf("%s", v2);
}
v1 = (unsigned int)(a1 % 10 - 1);
if ( (unsigned int)v1 > 8 )
{
v2 = 0LL;
return printf("%s", v2);
}
v2 = CSWTCH_3[v1];
if ( a1 % 10 != 4 )
return printf("%s", v2);
v3 = (unsigned int)((a1 - 4) / 10);
switch ( (int)v3 )
{
case 1:
strcpy(v6, "add_vertex");
break;
case 2:
v3 = 3LL;
strcpy(v6, "add_edge");
break;
case 3:
strcpy(v6, "print");
v3 = 0LL;
break;
case 4:
v3 = 0LL;
strcpy(v6, "page_rank");
break;
case 5:
strcpy(v6, "quit");
v3 = 0LL;
break;
default:
v3 = 0LL;
break;
}
return printf("Error: command %s must have %d parameters\n", v6, v3);
}

original_code:
void send_error(int code) {
 char *error_message = ((void *)0);
 if (code <= 0) {
  switch (code) {
   case -1: error_message = "Error: input folder must contain a file named 'network'\n"; break;
   case -2: error_message = "Error: Folder name must end with a '/'\n"; break;
   case -3: error_message = "Error: Should have 3 arguments: input folder, output folder, constant C\n"; break;
   case -4: error_message = "Error: Weight must be a number in the form x.xxx... between 0 and 1\n"; break;
   case 0: error_message = "Error: Constant c must be 0 <= c <= 1\n"; break;
  }
  printf("%s", error_message);
 } else {
  switch(code % 10) {
   case 1: error_message = "Error: command must have at least one character\n"; break;
   case 2: error_message = "Error: command length must be less than or equal to 500 characters\n"; break;
   case 3: error_message = "Error: command not found\n"; break;
   case 4: error_message = "Error: command %s must have %d  parameters\n"; break;
   case 5: error_message = "Error: ID parameter is not a non-negative integer\n"; break;
   case 6: error_message = "Error: cannot add vertex - too many vertices\n"; break;
   case 7: error_message = "Error: ID number is not in the system\n"; break;
   case 8: error_message = "Error: edge must be between two different vertices\n"; break;
   case 9: error_message = "Error: edge is in the graph\n"; break;
  }
  if (code % 10 != 4) {
   printf("%s", error_message);
  } else {
   char command_name[11];
   int num_of_params;
   switch ((code-4)/10) {
    case 1: strncpy(command_name, "add_vertex", 11); num_of_params = 1; break;
    case 2: strncpy(command_name, "add_edge", 9); num_of_params = 3; break;
    case 3: strncpy(command_name, "print", 6); num_of_params = 0; break;
    case 4: strncpy(command_name, "page_rank", 10); num_of_params = 0; break;
    case 5: strncpy(command_name, "quit", 5); num_of_params = 0; break;
   }
   printf("Error: command %s must have %d  parameters\n", command_name, num_of_params);
  }
 }
}


decompiled_code:
__int64 __fastcall int_to_string(unsigned int a1)
{
char *v1; // rax
int v2; // er8
__int64 result; // rax

v1 = (char *)malloc(0xAuLL);
if ( v1 )
{
v2 = sprintf(v1, "%d", a1);
result = 0LL;
if ( v2 < 0 )
{
send_perror("sprintf");
result = 2LL;
}
}
else
{
send_perror("malloc");
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
char *v1; // rax
int v2; // er8
__int64 result; // rax

v1 = (char *)malloc(0xAuLL);
if ( v1 )
{
v2 = sprintf(v1, "%d", a1);
result = 0LL;
if ( v2 < 0 )
{
send_perror("sprintf");
result = 2LL;
}
}
else
{
send_perror("malloc");
result = 2LL;
}
return result;
}

original_code:
int int_to_string(int x, char *str) {
 str = (char *) malloc(10 * sizeof(char));
 if (str == ((void *)0)) {
  send_perror("malloc");
  return 2;
 }
 if (sprintf(str, "%d", x) < 0) {
  send_perror("sprintf");
  return 2;
 }
 return 0;
}

variable_types: x int

variable_types:
str
char *


decompiled_code:
const char *__fastcall color_from_cluster_id(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 - 1);
result = "#C0C0C0";
if ( (unsigned int)v1 <= 9 )
result = CSWTCH_7[v1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func4(int a1)
{
__int64 v1; // rdi
const char *result; // rax

v1 = (unsigned int)(a1 - 1);
result = "#C0C0C0";
if ( (unsigned int)v1 <= 9 )
result = CSWTCH_7[v1];
return result;
}

original_code:
char *color_from_cluster_id(int cluster_id) {
 switch (cluster_id) {
 case 1: return "#0000ff";
 case 2: return "#00ffff";
 case 3: return "#8A2BE2";
 case 4: return "#A52A2A";
 case 5: return "#7FFF00";
 case 6: return "#006400";
 case 7: return "#FFD700";
 case 8: return "#FF69B4";
 case 9: return "#FF4500";
 case 10: return "#C0C0C0";
 default: return "#C0C0C0";
 }
}

return_type: char *


decompiled_code:
__int64 __fastcall best_solution_writer(int *a1, _QWORD *a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax

v3 = get_full_path(a3, "best_clusters.xgmml");
if ( v3 )
result = write_all(a1, a2, 5, v3);
else
result = 2LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int *a1, _QWORD *a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax

v3 = get_full_path(a3, "best_clusters.xgmml");
if ( v3 )
result = func7(a1, a2, 5, v3);
else
result = 2LL;
return result;
}

original_code:
int best_solution_writer(network *net, int **edges, char *out_dir) {
 char *filename = get_full_path(out_dir, "best_clusters.xgmml");
 if (filename == ((void *)0)) {
  return 2;
 }
 return write_all(net, edges, 5, filename);
}

canonical_original_code:
int best_solution_writer(struct network *net, int **edges, char *out_dir) {
 char *filename = get_full_path(out_dir, "best_clusters.xgmml");
 if (filename == ((void *)0)) {
  return 2;
 }
 return write_all(net, edges, 5, filename);
}

variable_types: struct network { int num_of_vertices; void **vertices; } *

variable_types:
edges
int **

variable_types:
filename
char *


decompiled_code:
__int64 __fastcall cluster_sol_writer(int *a1, _QWORD *a2, __int64 a3, int a4)
{
__int64 v5; // rax
__int64 result; // rax

v5 = get_full_path(a3, "clustering_solution.xgmml");
if ( v5 )
result = write_all(a1, a2, a4, v5);
else
result = 2LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1, _QWORD *a2, __int64 a3, int a4)
{
__int64 v5; // rax
__int64 result; // rax

v5 = get_full_path(a3, "clustering_solution.xgmml");
if ( v5 )
result = func7(a1, a2, a4, v5);
else
result = 2LL;
return result;
}

original_code:
int cluster_sol_writer(network *net, int **edges, char *out_dir, int num_of_clusters) {
 char *filename = get_full_path(out_dir, "clustering_solution.xgmml");
 if (filename == ((void *)0)) {
  return 2;
 }
 return write_all(net, edges, num_of_clusters, filename);
}

canonical_original_code:
int cluster_sol_writer(struct network *net, int **edges, char *out_dir, int num_of_clusters) {
 char *filename = get_full_path(out_dir, "clustering_solution.xgmml");
 if (filename == ((void *)0)) {
  return 2;
 }
 return write_all(net, edges, num_of_clusters, filename);
}


decompiled_code:
void __fastcall destroy_vertex(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 6) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 6) > (int)v2 );
}
free(*((void **)ptr + 2));
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 6) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*((_QWORD *)ptr + 2) + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 6) > (int)v2 );
}
free(*((void **)ptr + 2));
free(*(void **)ptr);
free(ptr);
}

original_code:
void destroy_vertex(vertex *vrtx) {
 if (vrtx->deg > 0) {
  int j;
  for (j = 0; j < vrtx->deg; j++)
   free(vrtx->adjacency_list[j]);
 }
 free(vrtx->adjacency_list);
 free(vrtx->name);
 free(vrtx);
}

canonical_original_code:
void destroy_vertex(struct vertex *vrtx) {
 if (vrtx->deg > 0) {
  int j;
  for (j = 0; j < vrtx->deg; j++)
   free(vrtx->adjacency_list[j]);
 }
 free(vrtx->adjacency_list);
 free(vrtx->name);
 free(vrtx);
}

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } *


decompiled_code:
__int64 __fastcall double_vertices(void **a1, __int64 a2)
{
void *v2; // rax
__int64 result; // rax

v2 = realloc(*a1, 16 * a2);
if ( v2 )
{
*a1 = v2;
result = 0LL;
}
else
{
send_perror("realloc");
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(void **a1, __int64 a2)
{
void *v2; // rax
__int64 result; // rax

v2 = realloc(*a1, 16 * a2);
if ( v2 )
{
*a1 = v2;
result = 0LL;
}
else
{
send_perror("realloc");
result = 2LL;
}
return result;
}

original_code:
int double_vertices(vertex ***v, size_t current_size) {
 vertex **tmp;
 if ((tmp = realloc(*v, 2 * current_size * sizeof(vertex*))) == ((void *)0)) {
  send_perror("realloc");
  return 2;
 } else {
  *v = tmp;
 }
 return 0;
}

canonical_original_code:
int double_vertices(struct vertex ***v, unsigned long current_size) {
 struct vertex **tmp;
 if ((tmp = realloc(*v, 2 * current_size * sizeof(struct vertex *))) == ((void *)0)) {
  send_perror("realloc");
  return 2;
 } else {
  *v = tmp;
 }
 return 0;
}

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } ***


decompiled_code:
_BOOL8 __fastcall add_edge_to_vertex(_QWORD *a1, _QWORD *a2, void *a3)
{
int v4; // er8
_BOOL8 result; // rax

if ( (unsigned int)check_edge_exists(*a1, *a2) == 1 )
{
send_error(9LL);
free(a3);
result = 1LL;
}
else
{
v4 = double_and_add(*a2 + 16LL, *a2 + 24LL, a3);
result = 1LL;
if ( !v4 )
result = (unsigned int)double_and_add(*a1 + 16LL, *a1 + 24LL, a3) != 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_QWORD *a1, _QWORD *a2, void *a3)
{
int v4; // er8
_BOOL8 result; // rax

if ( (unsigned int)check_edge_exists(*a1, *a2) == 1 )
{
send_error(9LL);
free(a3);
result = 1LL;
}
else
{
v4 = double_and_add(*a2 + 16LL, *a2 + 24LL, a3);
result = 1LL;
if ( !v4 )
result = (unsigned int)double_and_add(*a1 + 16LL, *a1 + 24LL, a3) != 0;
}
return result;
}

original_code:
int add_edge_to_vertex(vertex **src_vrtx, vertex **dest_vrtx, edge *e) {
 int status;
 if (check_edge_exists(*src_vrtx, *dest_vrtx, e) == 1) {
  send_error(9);
  free(e);
  return 1;
 }
 status = double_and_add(&((*dest_vrtx)->adjacency_list), &((*dest_vrtx)->deg), e)
     || double_and_add(&((*src_vrtx)->adjacency_list), &((*src_vrtx)->deg), e);
 return status;
}

canonical_original_code:
int add_edge_to_vertex(struct vertex **src_vrtx, struct vertex **dest_vrtx, struct edge *e) {
 int status;
 if (check_edge_exists(*src_vrtx, *dest_vrtx, e) == 1) {
  send_error(9);
  free(e);
  return 1;
 }
 status = double_and_add(&((*dest_vrtx)->adjacency_list), &((*dest_vrtx)->deg), e)
     || double_and_add(&((*src_vrtx)->adjacency_list), &((*src_vrtx)->deg), e);
 return status;
}

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } **

variable_types: struct edge { int from; int to; double weight; } *

variable_types: status int


bfs

original_code:
int bfs(network* net, vertex* s, int **edges, int **id, int *cluster_id,
  int *longest_shortest, tuple ***clusterScores) {
 int nV = net->num_of_vertices;
 edge **lst;
 edge *currEdge;
 vertex *currVertex;
 int from, to, size = 1, numEdges = 0;
 int i, currVertexId, newVertexId;
 int distFunc[nV];
 int colors[nV];
 int is_new = 0;
 double sum = 0;
 queue *q = init_queue();
 if (q == ((void *)0)) {
  return 2;
 }
 for (i = 0; i < nV; i++) {
  distFunc[i] = -1;
  colors[i] = 0;
 }
 if (s->cluster_id != -1) {
  is_new = 1;
 }
 enqueue(q, init_elem(&(s->id), ((void *)0)));
 distFunc[s->id] = 0;
 currVertexId = s->id;
 while (!is_empty(q)) {
  currVertexId = *((int *)(dequeue(q)->content));
  currVertex = get_vertex(currVertexId, net);
  lst = (edge**)currVertex->adjacency_list;
  for (i = 0; i < currVertex->deg; i++) {
   currEdge = lst[i];
   to = currEdge->to;
   from = currEdge->from;
   if (to == currVertexId) {
    newVertexId = from;
   } else {
    newVertexId = to;
   }
   if (newVertexId < currVertexId)
    continue;
   elem* elm = init_elem(&newVertexId, ((void *)0));
   if (elm == ((void *)0)) {
    return 2;
   }
   if (edges[currVertexId][newVertexId - currVertexId - 1] == 1) {
    numEdges++;
    sum += currEdge->weight;
    if ((get_vertex(newVertexId, net))->cluster_id != -1)
     (get_vertex(newVertexId, net))->cluster_id = *cluster_id;
   }
   if (!(colors[newVertexId])) {
    enqueue(q, elm);
    size++;
    if (edges[currVertexId][newVertexId - currVertexId - 1]
      == 1) {
     colors[newVertexId] = 1;
     distFunc[newVertexId] = (distFunc[currVertexId]) + 1;
    }
   }
  }
  if (is_new) {
   *clusterScores[*cluster_id-1] = init_tuple(size, sum, *cluster_id);
   if (*clusterScores[*cluster_id-1] == ((void *)0)) {
    return 2;
   }
  }
 }
 for (i = 0; i < nV; i++)
  if ((get_vertex(i, net))->cluster_id == *cluster_id)
   if ((distFunc[i] > longest_shortest[*cluster_id])
     || (distFunc[i] == -1))
    longest_shortest[*cluster_id-1] = distFunc[i];
 if (is_new) {
  (*cluster_id)++;
 }
 return 0;
}

canonical_original_code:
int bfs(struct network *net, struct vertex *s, int **edges, int **id, int *cluster_id, int *longest_shortest, struct tuple ***clusterScores) {
 int nV = net->num_of_vertices;
 struct edge **lst;
 struct edge *currEdge;
 struct vertex *currVertex;
 int from; int to; int size = 1; int numEdges = 0;
 int i; int currVertexId; int newVertexId;
 int distFunc[-1];
 int colors[-1];
 int is_new = 0;
 double sum = 0;
 struct queue *q = init_queue();
 if (q == ((void *)0)) {
  return 2;
 }
 for (i = 0; i < nV; i++) {
  distFunc[i] = -1;
  colors[i] = 0;
 }
 if (s->cluster_id != -1) {
  is_new = 1;
 }
 enqueue(q, init_elem(&(s->id), ((void *)0)));
 distFunc[s->id] = 0;
 currVertexId = s->id;
 while (!is_empty(q)) {
  currVertexId = *((int *)(dequeue(q)->content));
  currVertex = get_vertex(currVertexId, net);
  lst = (struct edge **)currVertex->adjacency_list;
  for (i = 0; i < currVertex->deg; i++) {
   currEdge = lst[i];
   to = currEdge->to;
   from = currEdge->from;
   if (to == currVertexId) {
    newVertexId = from;
   } else {
    newVertexId = to;
   }
   if (newVertexId < currVertexId)
    continue;
   struct elem *elm = init_elem(&newVertexId, ((void *)0));
   if (elm == ((void *)0)) {
    return 2;
   }
   if (edges[currVertexId][newVertexId - currVertexId - 1] == 1) {
    numEdges++;
    sum += currEdge->weight;
    if ((get_vertex(newVertexId, net))->cluster_id != -1)
     (get_vertex(newVertexId, net))->cluster_id = *cluster_id;
   }
   if (!(colors[newVertexId])) {
    enqueue(q, elm);
    size++;
    if (edges[currVertexId][newVertexId - currVertexId - 1]
      == 1) {
     colors[newVertexId] = 1;
     distFunc[newVertexId] = (distFunc[currVertexId]) + 1;
    }
   }
  }
  if (is_new) {
   *clusterScores[*cluster_id-1] = init_tuple(size, sum, *cluster_id);
   if (*clusterScores[*cluster_id-1] == ((void *)0)) {
    return 2;
   }
  }
 }
 for (i = 0; i < nV; i++)
  if ((get_vertex(i, net))->cluster_id == *cluster_id)
   if ((distFunc[i] > longest_shortest[*cluster_id])
     || (distFunc[i] == -1))
    longest_shortest[*cluster_id-1] = distFunc[i];
 if (is_new) {
  (*cluster_id)++;
 }
 return 0;
}

variable_types:
id
int **

variable_types: struct tuple { int size; double score; int place; int diameter; } ***

variable_types: from int

variable_types: to int

variable_types: size int

variable_types: numEdges int

variable_types: currVertexId int

variable_types: newVertexId int

variable_types:
distFunc
int [-1]

variable_types:
colors
int [-1]

variable_types: sum double

variable_types: struct queue { struct elem { void *content; struct elem *next; } *head; struct elem *tail; } *

user_defined_types: struct edge { int from; int to; double weight; }


original_code:
int bfs_all(network *net, int **id, int nV, int *realEdges,
  tuple **clusterScores) {
 int longest_shortest[nV];
 int inv_dic[nV];
 int i, cluster_id = 1;
 for (i = 0; i < nV; i++)
  longest_shortest[i] = 0;
 for (i = 0; i < nV; i++) {
  if (bfs(net, get_vertex(i, net), &realEdges, id, &cluster_id,
    longest_shortest, &clusterScores) == 2) {
   return -1;
  }
 }
 for (i = 0; i < cluster_id; i++) {
  clusterScores[i]->diameter = longest_shortest[i];
 }
 qsort(*clusterScores, cluster_id, sizeof(tuple), compare_to);
 for (i = 0; i < cluster_id; i++) {
  inv_dic[clusterScores[i]->place - 1] = i + 1;
 }
 for (i = 0; i < nV; i++) {
  vertex *v = get_vertex(i, net);
  v->cluster_id = inv_dic[v->cluster_id - 1];
 }
 return cluster_id;
}

canonical_original_code:
int bfs_all(struct network *net, int **id, int nV, int *realEdges, struct tuple **clusterScores) {
 int longest_shortest[-1];
 int inv_dic[-1];
 int i; int cluster_id = 1;
 for (i = 0; i < nV; i++)
  longest_shortest[i] = 0;
 for (i = 0; i < nV; i++) {
  if (bfs(net, get_vertex(i, net), &realEdges, id, &cluster_id,
    longest_shortest, &clusterScores) == 2) {
   return -1;
  }
 }
 for (i = 0; i < cluster_id; i++) {
  clusterScores[i]->diameter = longest_shortest[i];
 }
 qsort(*clusterScores, cluster_id, sizeof(tuple), compare_to);
 for (i = 0; i < cluster_id; i++) {
  inv_dic[clusterScores[i]->place - 1] = i + 1;
 }
 for (i = 0; i < nV; i++) {
  struct vertex *v = get_vertex(i, net);
  v->cluster_id = inv_dic[v->cluster_id - 1];
 }
 return cluster_id;
}

variable_types: struct network { int num_of_vertices; void **vertices; } *

variable_types: nV int

variable_types:
realEdges
int *

variable_types: struct tuple { int size; double score; int place; int diameter; } **

variable_types: i int

variable_types: struct vertex { char *name; int id; void **adjacency_list; int deg; int cluster_id; } *


original_code:
void calc_avg(network* net, double *avg_within, double *avgBetween,
  int *edges, int **id) {
 int i, j;
 int nV = net->num_of_vertices;
 int count_between = 0;
 int count_within = 0;
 edge **lst;
 vertex *currVertex;
 int other_vertex_id;
 int curr_vertex_id;
 *avg_within = 0;
 *avgBetween = 0;
 for (i = 0; i < nV; i++) {
  currVertex = get_vertex(i, net);
  lst = (edge **)currVertex->adjacency_list;
  for (j = 0; j < currVertex->deg; j++) {
   other_vertex_id = lst[j]->to;
   if (other_vertex_id > i) {
    if (edges[id[curr_vertex_id][other_vertex_id]] == 1) {
     double curr_weight = lst[j]->weight;
     if (currVertex->cluster_id
       == get_vertex(other_vertex_id, net)->cluster_id) {
      *avg_within = ((*avg_within * count_within)
        + curr_weight) / (count_within+1);
      count_within++;
     } else {
      *avgBetween = ((*avgBetween * (count_between))
        + curr_weight) / (count_between+1);
      count_between++;
     }
    }
   }
  }
 }
}

canonical_original_code:
void calc_avg(struct network *net, double *avg_within, double *avgBetween, int *edges, int **id) {
 int i; int j;
 int nV = net->num_of_vertices;
 int count_between = 0;
 int count_within = 0;
 struct edge **lst;
 struct vertex *currVertex;
 int other_vertex_id;
 int curr_vertex_id;
 *avg_within = 0;
 *avgBetween = 0;
 for (i = 0; i < nV; i++) {
  currVertex = get_vertex(i, net);
  lst = (struct edge **)currVertex->adjacency_list;
  for (j = 0; j < currVertex->deg; j++) {
   other_vertex_id = lst[j]->to;
   if (other_vertex_id > i) {
    if (edges[id[curr_vertex_id][other_vertex_id]] == 1) {
     double curr_weight = lst[j]->weight;
     if (currVertex->cluster_id
       == get_vertex(other_vertex_id, net)->cluster_id) {
      *avg_within = ((*avg_within * count_within)
        + curr_weight) / (count_within+1);
      count_within++;
     } else {
      *avgBetween = ((*avgBetween * (count_between))
        + curr_weight) / (count_between+1);
      count_between++;
     }
    }
   }
  }
 }
}

variable_types:
avgBetween
double *

variable_types:
edges
int *

variable_types:
id
int **

variable_types: j int

variable_types: struct edge { int from; int to; double weight; } **


decompiled_code:
__int64 __fastcall search_for_edge_in_incoming_edge_list(unsigned int **a1, int a2, unsigned int *a3)
{
unsigned int **v4; // rbx
unsigned int *v5; // rax

if ( a2 <= 0 )
return 0LL;
v4 = a1;
do
{
printf("list len %d\tedge_list[i]->from = %d edge_list[i]->to = %d\n", (unsigned int)a2, **v4, (*v4)[1]);
printf("e: %d->%d\n", *a3, a3[1]);
v5 = *v4;
if ( *(_QWORD *)*v4 == *(_QWORD *)a3 || *v5 == a3[1] && v5[1] == *a3 )
return 1LL;
++v4;
}
while ( v4 != &a1[(unsigned int)(a2 - 1) + 1] );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int **a1, int a2, unsigned int *a3)
{
unsigned int **v4; // rbx
unsigned int *v5; // rax

if ( a2 <= 0 )
return 0LL;
v4 = a1;
do
{
printf("list len %d\tedge_list[i]->from = %d edge_list[i]->to = %d\n", (unsigned int)a2, **v4, (*v4)[1]);
printf("e: %d->%d\n", *a3, a3[1]);
v5 = *v4;
if ( *(_QWORD *)*v4 == *(_QWORD *)a3 || *v5 == a3[1] && v5[1] == *a3 )
return 1LL;
++v4;
}
while ( v4 != &a1[(unsigned int)(a2 - 1) + 1] );
return 0LL;
}

original_code:
int search_for_edge_in_incoming_edge_list(edge** edge_list, int list_len,
  edge *e) {
 int i;
 for (i = 0; i < list_len; ++i) {
  printf("list len %d\tedge_list[i]->from = %d edge_list[i]->to = %d\n",list_len,edge_list[i]->from ,edge_list[i]->to);
  printf("e: %d->%d\n", e->from, e->to);
  if (((edge_list[i]->from == e->from) && (edge_list[i]->to == e->to))
    || ((edge_list[i]->from == e->to)
      && (edge_list[i]->to == e->from))) {
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
int search_for_edge_in_incoming_edge_list(struct edge **edge_list, int list_len, struct edge *e) {
 int i;
 for (i = 0; i < list_len; ++i) {
  printf("list len %d\tedge_list[i]->from = %d edge_list[i]->to = %d\n",list_len,edge_list[i]->from ,edge_list[i]->to);
  printf("e: %d->%d\n", e->from, e->to);
  if (((edge_list[i]->from == e->from) && (edge_list[i]->to == e->to))
    || ((edge_list[i]->from == e->to)
      && (edge_list[i]->to == e->from))) {
   return 1;
  }
 }
 return 0;
}

variable_types:
e
struct edge *


decompiled_code:
__int64 __fastcall check_edge_exists(__int64 a1, __int64 a2, unsigned int *a3)
{
__int64 v4; // rbx
int v5; // eax
__int64 v6; // rdx

v4 = a2;
v5 = *(_DWORD *)(a1 + 24);
v6 = *(unsigned int *)(a2 + 24);
if ( v5 <= (int)v6 )
{
v6 = (unsigned int)v5;
v4 = a1;
}
printf("our src in searching is %d of deg %d\n", *(unsigned int *)(v4 + 8), v6);
return search_for_edge_in_incoming_edge_list(*(unsigned int ***)(v4 + 16), *(_DWORD *)(v4 + 24), a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int *a3)
{
__int64 v4; // rbx
int v5; // eax
__int64 v6; // rdx

v4 = a2;
v5 = *(_DWORD *)(a1 + 24);
v6 = *(unsigned int *)(a2 + 24);
if ( v5 <= (int)v6 )
{
v6 = (unsigned int)v5;
v4 = a1;
}
printf("our src in searching is %d of deg %d\n", *(unsigned int *)(v4 + 8), v6);
return func0(*(unsigned int ***)(v4 + 16), *(_DWORD *)(v4 + 24), a3);
}

original_code:
int check_edge_exists(vertex *src, vertex *to, edge *e) {
 vertex *tmp;
 if (src->deg > to->deg) {
  tmp = src;
  src = to;
  to = tmp;
 }
 printf("our src in searching is %d of deg %d\n", src->id, src->deg);
 return search_for_edge_in_incoming_edge_list((edge**) src->adjacency_list,
   src->deg, e);
}

canonical_original_code:
int check_edge_exists(struct vertex *src, struct vertex *to, struct edge *e) {
 struct vertex *tmp;
 if (src->deg > to->deg) {
  tmp = src;
  src = to;
  to = tmp;
 }
 printf("our src in searching is %d of deg %d\n", src->id, src->deg);
 return search_for_edge_in_incoming_edge_list((struct edge **) src->adjacency_list,
   src->deg, e);
}

variable_types:
to
struct vertex *

variable_types: struct edge { int from; int to; double weight; } *


decompiled_code:
__int64 __fastcall check_args(int a1, __int64 a2)
{
const char *v2; // r12
const char *v3; // r13
size_t v4; // rbp
__int64 result; // rax
char *v6; // rbp
size_t v7; // rax
double v8; // xmm0_8
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

if ( a1 == 4 )
{
v3 = *(const char **)(a2 + 16);
input_dir = *(_QWORD *)(a2 + 8);
v2 = (const char *)input_dir;
output_dir = (__int64)v3;
v4 = strlen((const char *)input_dir);
if ( v2[v4 - 1] == 47 && v3[strlen(v3) - 1] == 47 )
{
v6 = (char *)malloc(v4 + 8);
filename = v6;
v7 = strlen(v2);
strncpy(v6, v2, v7 + 1);
*(_QWORD *)&v6[strlen(v6)] = 0x6B726F7774656ELL;
if ( stat(v6, &v9) == -1 )
{
send_error(0xFFFFFFFFLL);
result = 1LL;
}
else
{
v8 = atof(*(const char **)(a2 + 24));
C = *(_QWORD *)&v8;
if ( v8 > 1.0 || (result = 0LL, v8 < 0.0) )
{
send_error(0LL);
result = 1LL;
}
}
}
else
{
send_error(4294967294LL);
result = 1LL;
}
}
else
{
send_error(4294967293LL);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2)
{
const char *v2; // r12
const char *v3; // r13
size_t v4; // rbp
__int64 result; // rax
char *v6; // rbp
size_t v7; // rax
double v8; // xmm0_8
struct stat v9; // [rsp+0h] [rbp-B8h] BYREF

if ( a1 == 4 )
{
v3 = *(const char **)(a2 + 16);
input_dir = *(_QWORD *)(a2 + 8);
v2 = (const char *)input_dir;
output_dir = (__int64)v3;
v4 = strlen((const char *)input_dir);
if ( v2[v4 - 1] == 47 && v3[strlen(v3) - 1] == 47 )
{
v6 = (char *)malloc(v4 + 8);
filename = v6;
v7 = strlen(v2);
strncpy(v6, v2, v7 + 1);
*(_QWORD *)&v6[strlen(v6)] = 0x6B726F7774656ELL;
if ( stat(v6, &v9) == -1 )
{
send_error(0xFFFFFFFFLL);
result = 1LL;
}
else
{
v8 = atof(*(const char **)(a2 + 24));
C = *(_QWORD *)&v8;
if ( v8 > 1.0 || (result = 0LL, v8 < 0.0) )
{
send_error(0LL);
result = 1LL;
}
}
}
else
{
send_error(4294967294LL);
result = 1LL;
}
}
else
{
send_error(4294967293LL);
result = 1LL;
}
return result;
}

original_code:
int check_args(int argc, char **argv) {
 struct stat tmp;
 if (argc != 4) {
  send_error(-3);
  return 1;
 }
 input_dir = argv[1];
 output_dir = argv[2];
 if ( (input_dir[strlen(input_dir)-1] != '/') || (output_dir[strlen(output_dir)-1] != '/') ) {
  send_error(-2);
  return 1;
 }
 filename = (char *) malloc(strlen(input_dir) + strlen("network") + 1);
 strncpy(filename, input_dir, strlen(input_dir) + 1);
 strncat(filename, "network", strlen("network") +1 );
 if (filename == ((void *)0)) {
  send_perror("malloc");
  return 2;
 }
 if (stat(filename, &tmp) == -1) {
  send_error(-1);
  return 1;
 }
 C = atof(argv[3]);
 if ( (C > 1) || (C < 0) ) {
  send_error(0);
  return 1;
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall check_all_whitespace(const char *a1)
{
const char *v1; // rbx
char *v2; // rbp
const unsigned __int16 **v3; // rax
__int64 v4; // rdx

v1 = a1;
v2 = (char *)&a1[strlen(a1)];
do
{
if ( v1 == v2 )
return 1LL;
v3 = _ctype_b_loc();
v4 = *v1++;
}
while ( ((*v3)[v4] & 0x2000) != 0 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
const char *v1; // rbx
char *v2; // rbp
const unsigned __int16 **v3; // rax
__int64 v4; // rdx

v1 = a1;
v2 = (char *)&a1[strlen(a1)];
do
{
if ( v1 == v2 )
return 1LL;
v3 = _ctype_b_loc();
v4 = *v1++;
}
while ( ((*v3)[v4] & 0x2000) != 0 );
return 0LL;
}

original_code:
int check_all_whitespace(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short int) _ISspace))
   return 0;
 }
 return 1;
}

canonical_original_code:
int check_all_whitespace(char *str) {
 int i;
 for (i = 0; i < strlen(str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(str + i)))] & (unsigned short) _ISspace))
   return 0;
 }
 return 1;
}

variable_types:
str
char *


decompiled_code:
__int64 __fastcall get_action(const void *a1)
{
unsigned int v2; // er8

v2 = 1;
if ( !memcmp(a1, "add_vertex", 0xAuLL) )
return v2;
v2 = 2;
if ( !memcmp(a1, "add_edge", 8uLL) )
return v2;
send_error(3LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1)
{
unsigned int v2; // er8

v2 = 1;
if ( !memcmp(a1, "add_vertex", 0xAuLL) )
return v2;
v2 = 2;
if ( !memcmp(a1, "add_edge", 8uLL) )
return v2;
send_error(3LL);
return 0LL;
}

original_code:
int get_action(char *command) {
 int ret = 0;
 if (strstr(command, "add_vertex") == command) {
  ret = 1;
 } else if (strstr(command, "add_edge") == command) {
  ret = 2;
 }
 if (ret == 0)
  send_error(3);
 return ret;
}

variable_types:
command
char *

variable_types: ret int


decompiled_code:
__int64 __fastcall get_params(__int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdi

result = a1 + 10;
if ( a2 != 1 )
{
v3 = a1 + 8;
result = 0LL;
if ( a2 == 2 )
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 result; // rax
__int64 v3; // rdi

result = a1 + 10;
if ( a2 != 1 )
{
v3 = a1 + 8;
result = 0LL;
if ( a2 == 2 )
result = v3;
}
return result;
}

original_code:
char *get_params(char *command, int code) {
 switch (code) {
 case 1:
  return command + 10;
 case 2:
  return command + 8;
 default:
  return ((void *)0);
 }
}

variable_types: code int


decompiled_code:
char *__fastcall get_full_path(const char *a1, const char *a2)
{
size_t v2; // r12
size_t v3; // rax
char *v4; // rax
char *v5; // r12

v2 = strlen(a1);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
v5 = v4;
if ( v4 )
{
if ( sprintf(v4, "%s%s", a1, a2) < 0 )
{
v5 = 0LL;
send_perror("sprintf");
}
}
else
{
send_perror("malloc");
}
return v5;
}

canonical_decompiled_code:
char *__fastcall func3(const char *a1, const char *a2)
{
size_t v2; // r12
size_t v3; // rax
char *v4; // rax
char *v5; // r12

v2 = strlen(a1);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
v5 = v4;
if ( v4 )
{
if ( sprintf(v4, "%s%s", a1, a2) < 0 )
{
v5 = 0LL;
send_perror("sprintf");
}
}
else
{
send_perror("malloc");
}
return v5;
}

original_code:
char* get_full_path(char *out_dir, char *filename) {
 char *full_path = (char *) malloc(strlen(out_dir) + strlen(filename) + 1);
 if (full_path == ((void *)0)) {
  send_perror("malloc");
  return ((void *)0);
 }
 if (sprintf(full_path, "%s%s", out_dir, filename) < 0) {
  send_perror("sprintf");
  return ((void *)0);
 }
 return full_path;
}

canonical_original_code:
char *get_full_path(char *out_dir, char *filename) {
 char *full_path = (char *) malloc(strlen(out_dir) + strlen(filename) + 1);
 if (full_path == ((void *)0)) {
  send_perror("malloc");
  return ((void *)0);
 }
 if (sprintf(full_path, "%s%s", out_dir, filename) < 0) {
  send_perror("sprintf");
  return ((void *)0);
 }
 return full_path;
}

variable_types:
filename
char *


output

decompiled_code:
__int64 __fastcall output(unsigned int *a1, __int64 a2, unsigned int a3, __int64 a4, const char *a5, double a6, double a7, double a8, double a9)
{
unsigned int v9; // eax
int v12; // er8
__int64 result; // rax
__int64 v14[4]; // [rsp+8h] [rbp-20h] BYREF

v9 = *a1;
v14[0] = a2;
if ( v9 )
{
if ( (unsigned int)results_writer(a1, a6, a7, a8, a9, v14[0], a3, a4, a5) == 2
|| (unsigned int)cluster_sol_writer(a1, v14, a5, a3) == 2
|| (v12 = best_solution_writer(a1, v14, a5), result = 0LL, v12 == 2) )
{
result = 2LL;
}
}
else
{
puts("The network has no vertices. Exiting.");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, __int64 a2, unsigned int a3, __int64 a4, const char *a5, double a6, double a7, double a8, double a9)
{
unsigned int v9; // eax
int v12; // er8
__int64 result; // rax
__int64 v14[4]; // [rsp+8h] [rbp-20h] BYREF

v9 = *a1;
v14[0] = a2;
if ( v9 )
{
if ( (unsigned int)func4(a1, a6, a7, a8, a9, v14[0], a3, a4, a5) == 2
|| (unsigned int)cluster_sol_writer(a1, v14, a5, a3) == 2
|| (v12 = best_solution_writer(a1, v14, a5), result = 0LL, v12 == 2) )
{
result = 2LL;
}
}
else
{
puts("The network has no vertices. Exiting.");
result = 1LL;
}
return result;
}

original_code:
int output(network *net, int *edges, double ghost_const,
  double optimized_score, int num_of_clusters, double avg_within, double avg_between, tuple **scores, char *out_dir) {
 if (net->num_of_vertices == 0) {
  printf("The network has no vertices. Exiting.\n");
  return 1;
 } else {
  if (results_writer(net, edges, ghost_const,
  optimized_score, num_of_clusters, avg_within, avg_between, scores, out_dir) == 2) {
   return 2;
  }
  if (cluster_sol_writer(net, &edges, out_dir, num_of_clusters) == 2) {
   return 2;
  }
  if (best_solution_writer(net, &edges, out_dir) == 2) {
   return 2;
  }
 }
 return 0;
}

canonical_original_code:
int output(struct network *net, int *edges, double ghost_const, double optimized_score, int num_of_clusters, double avg_within, double avg_between, struct tuple **scores, char *out_dir) {
 if (net->num_of_vertices == 0) {
  printf("The network has no vertices. Exiting.\n");
  return 1;
 } else {
  if (results_writer(net, edges, ghost_const,
  optimized_score, num_of_clusters, avg_within, avg_between, scores, out_dir) == 2) {
   return 2;
  }
  if (cluster_sol_writer(net, &edges, out_dir, num_of_clusters) == 2) {
   return 2;
  }
  if (best_solution_writer(net, &edges, out_dir) == 2) {
   return 2;
  }
 }
 return 0;
}

variable_types: struct network { int num_of_vertices; void **vertices; } *

variable_types: struct tuple { int size; double score; int place; int diameter; } **


quit

decompiled_code:
void __noreturn quit()
{
__int64 i; // rbx
__pid_t *v1; // rbp

for ( i = *((_QWORD *)&dword_7A0 + 1); i; i = *(_QWORD *)(i + 16) )
{
v1 = *(__pid_t **)i;
kill(**(_DWORD **)i, 18);
process_close(v1);
}
cleanup_process_family(&dword_7A0);
putchar(13);
fflush(stdout);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
__int64 i; // rbx
__pid_t *v1; // rbp

for ( i = *((_QWORD *)&dword_7A0 + 1); i; i = *(_QWORD *)(i + 16) )
{
v1 = *(__pid_t **)i;
kill(**(_DWORD **)i, 18);
process_close(v1);
}
cleanup_process_family(&dword_7A0);
putchar(13);
fflush(stdout);
exit(0);
}

original_code:
static void quit(int sig)
{
 struct list_node *node = ((void *)0);
 for (node=pf.members.first; node!= ((void *)0); node=node->next) {
  struct process *p = (struct process*)(node->data);
  kill(p->pid, 18);
  process_close(p);
 }
 cleanup_process_family(&pf);
 printf("\r");
 fflush(stdout);
 exit(0);
}

variable_types: sig int

variable_types: struct list_node { void *data; struct list_node *previous; struct list_node *next; } *

variable_types: struct process { int pid; int ppid; int starttime; int member; int last_jiffies; struct timeval { long tv_sec; long tv_usec; } last_sample; double cpu_usage; int is_zombie; char command[-1]; char stat_file[20]; char buffer[1024]; } *

user_defined_types: struct process { int pid; int ppid; int starttime; int member; int last_jiffies; struct timeval last_sample; double cpu_usage; int is_zombie; char command[-1]; char stat_file[20]; char buffer[1024]; }


decompiled_code:
__int64 get_ncpu()
{
return sysconf(84);
}

canonical_decompiled_code:
__int64 func1()
{
return sysconf(84);
}

original_code:
static int get_ncpu() {
 int ncpu = -1;
 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
 return ncpu;
}

variable_types: ncpu int


decompiled_code:
_BOOL8 check_proc()
{
int v0; // er8
_BOOL8 result; // rax
struct statfs v2; // [rsp+0h] [rbp-88h] BYREF

v0 = statfs("/proc", &v2);
result = 0LL;
if ( v0 >= 0 )
result = v2.f_type == 40864;
return result;
}

canonical_decompiled_code:
_BOOL8 func2()
{
int v0; // er8
_BOOL8 result; // rax
struct statfs v2; // [rsp+0h] [rbp-88h] BYREF

v0 = statfs("/proc", &v2);
result = 0LL;
if ( v0 >= 0 )
result = v2.f_type == 40864;
return result;
}

original_code:
static int check_proc()
{
 struct statfs mnt;
 if (statfs("/proc", &mnt) < 0)
  return 0;
 if (mnt.f_type!=0x9fa0)
  return 0;
 return 1;
}

variable_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t { int __val[2]; } f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }

user_defined_types: struct statfs { long f_type; long f_bsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; struct __fsid_t f_fsid; long f_namelen; long f_frsize; long f_flags; long f_spare[4]; }


decompiled_code:
int __fastcall limit_process(unsigned int a1, int a2, double a3)
{
unsigned int v3; // ebp
signed int v4; // er14
unsigned int **v5; // r14
double v6; // xmm0_8
unsigned int *v7; // rbx
__syscall_slong_t v8; // rax
__syscall_slong_t v9; // rdx
__int64 j; // rbx
__pid_t *v11; // r15
__int64 i; // rax
__int64 k; // rbx
__pid_t *v14; // r15
unsigned int v15; // eax
int v16; // edx
unsigned int **v17; // rbx
double v19; // [rsp+0h] [rbp-98h]
int v20; // [rsp+0h] [rbp-98h]
int v21; // [rsp+0h] [rbp-98h]
double v22; // [rsp+8h] [rbp-90h]
signed int v24; // [rsp+1Ch] [rbp-7Ch]
struct timespec requested_time; // [rsp+20h] [rbp-78h] BYREF
struct timespec v26; // [rsp+30h] [rbp-68h] BYREF
struct timeval tv; // [rsp+40h] [rbp-58h] BYREF
struct timeval v28; // [rsp+50h] [rbp-48h] BYREF

requested_time = 0LL;
v26 = 0LL;
tv = 0LL;
v28 = 0LL;
if ( (unsigned int)create_process_family(&dword_7A0, a1) == -1 )
return printf("create process family failed");
if ( a2 )
{
for ( i = *((_QWORD *)&dword_7A0 + 1); i; i = *((_QWORD *)&dword_7A0 + 1) )
{
while ( **(_DWORD **)i == a1 )
{
i = *(_QWORD *)(i + 16);
if ( !i )
goto LABEL_5;
}
remove_process_from_family(&dword_7A0);
}
}
else if ( verbose )
{
printf("Members in the family owned by %d: %d\n", (unsigned int)dword_7A0, (unsigned int)*(&dword_7A0 + 7));
}
LABEL_5:
v3 = 0;
v22 = *(double *)&LC13;
LABEL_6:
v4 = a2 | (v3 % 0xA);
if ( v4 || (v15 = update_process_family(&dword_7A0), !verbose) || !v15 )
{
if ( !*(&dword_7A0 + 7) )
goto LABEL_61;
goto LABEL_8;
}
v20 = v15;
printf("%d new children processes detected (", v15);
v16 = v20;
v17 = (unsigned int **)*((_QWORD *)&dword_7A0 + 2);
v24 = v20 - 1;
if ( v20 > 0 )
{
do
{
v21 = v16;
printf("%d", **v17);
v16 = v21;
if ( v24 > v4 )
{
putchar(32);
v16 = v21;
}
++v4;
v17 = (unsigned int **)v17[1];
}
while ( v16 != v4 );
}
puts(")");
if ( *(&dword_7A0 + 7) )
{
LABEL_8:
v5 = (unsigned int **)*((_QWORD *)&dword_7A0 + 1);
v19 = *(double *)&LC13;
if ( !v5 )
goto LABEL_44;
while ( 1 )
{
while ( 1 )
{
v7 = *v5;
if ( !(*v5)[12] )
break;
fprintf(stderr, "Process %d is zombie!\n", *v7);
LABEL_16:
remove_process_from_family(&dword_7A0);
v5 = (unsigned int **)*((_QWORD *)&dword_7A0 + 1);
if ( !v5 )
goto LABEL_17;
}
if ( (unsigned int)process_monitor(*v5) )
{
if ( verbose )
fprintf(stderr, "Process %d dead!\n", *v7);
goto LABEL_16;
}
v6 = *((double *)v7 + 5);
v5 = (unsigned int **)v5[2];
if ( v6 >= 0.0 )
v19 = v6 + fmax(0.0, v19);
if ( !v5 )
{
LABEL_17:
if ( v19 >= 0.0 )
{
v22 = v22 / v19 * a3;
if ( v22 < 1.0 )
{
v9 = (unsigned int)(int)(v22 * 100000000.0);
v8 = 100000000 - v9;
}
else
{
v8 = 0LL;
v9 = 100000000LL;
v22 = *(double *)&LC15;
}
requested_time.tv_nsec = v9;
goto LABEL_21;
}
LABEL_44:
v19 = a3;
v22 = a3;
requested_time.tv_nsec = (unsigned int)(int)(100000.0 * a3 * 1000.0);
v8 = 100000000 - requested_time.tv_nsec;
LABEL_21:
v26.tv_nsec = v8;
if ( verbose )
{
if ( __ROR4__(-1030792151 * v3, 3) <= 0x147AE14u )
printf("\n%%CPU\twork quantum\tsleep quantum\tactive rate\n");
if ( !(v3 % 0xA) && v3 )
printf(
"%0.2lf%%\t%6ld us\t%6ld us\t%0.2lf%%\n",
v19 * 100.0,
requested_time.tv_nsec / 1000,
v26.tv_nsec / 1000,
100.0 * v22);
}
for ( j = *((_QWORD *)&dword_7A0 + 1); j; j = *((_QWORD *)&dword_7A0 + 1) )
{
while ( 1 )
{
v11 = *(__pid_t **)j;
if ( kill(**(_DWORD **)j, 18) )
break;
j = *(_QWORD *)(j + 16);
if ( !j )
goto LABEL_31;
}
if ( verbose )
fprintf(stderr, "Process %d dead!\n", (unsigned int)*v11);
remove_process_from_family(&dword_7A0);
}
LABEL_31:
gettimeofday(&tv, 0LL);
nanosleep(&requested_time, 0LL);
gettimeofday(&v28, 0LL);
if ( v26.tv_nsec > 0 )
{
for ( k = *((_QWORD *)&dword_7A0 + 1); k; k = *((_QWORD *)&dword_7A0 + 1) )
{
while ( 1 )
{
v14 = *(__pid_t **)k;
if ( kill(**(_DWORD **)k, 19) )
break;
k = *(_QWORD *)(k + 16);
if ( !k )
goto LABEL_49;
}
if ( verbose )
fprintf(stderr, "Process %d dead!\n", (unsigned int)*v14);
remove_process_from_family(&dword_7A0);
}
LABEL_49:
nanosleep(&v26, 0LL);
}
++v3;
goto LABEL_6;
}
}
}
LABEL_61:
if ( verbose )
puts("No more processes.");
return cleanup_process_family(&dword_7A0);
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1, int a2, double a3)
{
unsigned int v3; // ebp
signed int v4; // er14
unsigned int **v5; // r14
double v6; // xmm0_8
unsigned int *v7; // rbx
__syscall_slong_t v8; // rax
__syscall_slong_t v9; // rdx
__int64 j; // rbx
__pid_t *v11; // r15
__int64 i; // rax
__int64 k; // rbx
__pid_t *v14; // r15
unsigned int v15; // eax
int v16; // edx
unsigned int **v17; // rbx
double v19; // [rsp+0h] [rbp-98h]
int v20; // [rsp+0h] [rbp-98h]
int v21; // [rsp+0h] [rbp-98h]
double v22; // [rsp+8h] [rbp-90h]
signed int v24; // [rsp+1Ch] [rbp-7Ch]
struct timespec requested_time; // [rsp+20h] [rbp-78h] BYREF
struct timespec v26; // [rsp+30h] [rbp-68h] BYREF
struct timeval tv; // [rsp+40h] [rbp-58h] BYREF
struct timeval v28; // [rsp+50h] [rbp-48h] BYREF

requested_time = 0LL;
v26 = 0LL;
tv = 0LL;
v28 = 0LL;
if ( (unsigned int)create_process_family(&dword_7A0, a1) == -1 )
return printf("create process family failed");
if ( a2 )
{
for ( i = *((_QWORD *)&dword_7A0 + 1); i; i = *((_QWORD *)&dword_7A0 + 1) )
{
while ( **(_DWORD **)i == a1 )
{
i = *(_QWORD *)(i + 16);
if ( !i )
goto LABEL_5;
}
remove_process_from_family(&dword_7A0);
}
}
else if ( verbose )
{
printf("Members in the family owned by %d: %d\n", (unsigned int)dword_7A0, (unsigned int)*(&dword_7A0 + 7));
}
LABEL_5:
v3 = 0;
v22 = *(double *)&LC13;
LABEL_6:
v4 = a2 | (v3 % 0xA);
if ( v4 || (v15 = update_process_family(&dword_7A0), !verbose) || !v15 )
{
if ( !*(&dword_7A0 + 7) )
goto LABEL_61;
goto LABEL_8;
}
v20 = v15;
printf("%d new children processes detected (", v15);
v16 = v20;
v17 = (unsigned int **)*((_QWORD *)&dword_7A0 + 2);
v24 = v20 - 1;
if ( v20 > 0 )
{
do
{
v21 = v16;
printf("%d", **v17);
v16 = v21;
if ( v24 > v4 )
{
putchar(32);
v16 = v21;
}
++v4;
v17 = (unsigned int **)v17[1];
}
while ( v16 != v4 );
}
puts(")");
if ( *(&dword_7A0 + 7) )
{
LABEL_8:
v5 = (unsigned int **)*((_QWORD *)&dword_7A0 + 1);
v19 = *(double *)&LC13;
if ( !v5 )
goto LABEL_44;
while ( 1 )
{
while ( 1 )
{
v7 = *v5;
if ( !(*v5)[12] )
break;
fprintf(stderr, "Process %d is zombie!\n", *v7);
LABEL_16:
remove_process_from_family(&dword_7A0);
v5 = (unsigned int **)*((_QWORD *)&dword_7A0 + 1);
if ( !v5 )
goto LABEL_17;
}
if ( (unsigned int)process_monitor(*v5) )
{
if ( verbose )
fprintf(stderr, "Process %d dead!\n", *v7);
goto LABEL_16;
}
v6 = *((double *)v7 + 5);
v5 = (unsigned int **)v5[2];
if ( v6 >= 0.0 )
v19 = v6 + fmax(0.0, v19);
if ( !v5 )
{
LABEL_17:
if ( v19 >= 0.0 )
{
v22 = v22 / v19 * a3;
if ( v22 < 1.0 )
{
v9 = (unsigned int)(int)(v22 * 100000000.0);
v8 = 100000000 - v9;
}
else
{
v8 = 0LL;
v9 = 100000000LL;
v22 = *(double *)&LC15;
}
requested_time.tv_nsec = v9;
goto LABEL_21;
}
LABEL_44:
v19 = a3;
v22 = a3;
requested_time.tv_nsec = (unsigned int)(int)(100000.0 * a3 * 1000.0);
v8 = 100000000 - requested_time.tv_nsec;
LABEL_21:
v26.tv_nsec = v8;
if ( verbose )
{
if ( __ROR4__(-1030792151 * v3, 3) <= 0x147AE14u )
printf("\n%%CPU\twork quantum\tsleep quantum\tactive rate\n");
if ( !(v3 % 0xA) && v3 )
printf(
"%0.2lf%%\t%6ld us\t%6ld us\t%0.2lf%%\n",
v19 * 100.0,
requested_time.tv_nsec / 1000,
v26.tv_nsec / 1000,
100.0 * v22);
}
for ( j = *((_QWORD *)&dword_7A0 + 1); j; j = *((_QWORD *)&dword_7A0 + 1) )
{
while ( 1 )
{
v11 = *(__pid_t **)j;
if ( kill(**(_DWORD **)j, 18) )
break;
j = *(_QWORD *)(j + 16);
if ( !j )
goto LABEL_31;
}
if ( verbose )
fprintf(stderr, "Process %d dead!\n", (unsigned int)*v11);
remove_process_from_family(&dword_7A0);
}
LABEL_31:
gettimeofday(&tv, 0LL);
nanosleep(&requested_time, 0LL);
gettimeofday(&v28, 0LL);
if ( v26.tv_nsec > 0 )
{
for ( k = *((_QWORD *)&dword_7A0 + 1); k; k = *((_QWORD *)&dword_7A0 + 1) )
{
while ( 1 )
{
v14 = *(__pid_t **)k;
if ( kill(**(_DWORD **)k, 19) )
break;
k = *(_QWORD *)(k + 16);
if ( !k )
goto LABEL_49;
}
if ( verbose )
fprintf(stderr, "Process %d dead!\n", (unsigned int)*v14);
remove_process_from_family(&dword_7A0);
}
LABEL_49:
nanosleep(&v26, 0LL);
}
++v3;
goto LABEL_6;
}
}
}
LABEL_61:
if ( verbose )
puts("No more processes.");
return cleanup_process_family(&dword_7A0);
}

original_code:
void limit_process(pid_t pid, double limit, int ignore_children)
{
 struct timespec twork;
 struct timespec tsleep;
 struct timeval startwork;
 struct timeval endwork;
 memset(&twork, 0, sizeof(struct timespec));
 memset(&tsleep, 0, sizeof(struct timespec));
 memset(&startwork, 0, sizeof(struct timeval));
 memset(&endwork, 0, sizeof(struct timeval));
 unsigned long workingtime = 0;
 struct list_node *node;
 int c = 0;
 if (create_process_family(&pf, pid) == -1)
    {
        printf("create process family failed");
        return ;
    }
 if (ignore_children) {
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (proc->pid != pid)
            {
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
                continue;
            }
            node = node->next;
  }
 }
 if (!ignore_children && verbose) printf("Members in the family owned by %d: %d\n", pf.father, pf.members.count);
 double workingrate = -1;
 while(1) {
  if (!ignore_children && c%10==0) {
   int new_children = update_process_family(&pf);
   if (verbose && new_children) {
    printf("%d new children processes detected (", new_children);
    int j;
    node = pf.members.last;
    for (j=0; j<new_children; j++) {
     printf("%d", ((struct process*)(node->data))->pid);
     if (j<new_children-1) printf(" ");
     node = node->previous;
    }
    printf(")\n");
   }
  }
  if (pf.members.count==0) {
   if (verbose) printf("No more processes.\n");
   break;
  }
  double pcpu = -1;
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (proc->is_zombie) {
    fprintf(stderr,"Process %d is zombie!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
    continue;
   }
   if (process_monitor(proc) != 0) {
    if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
    continue;
   }
            node = node->next;
   if (proc->cpu_usage<0) {
    continue;
   }
   if (pcpu<0) pcpu = 0;
   pcpu += proc->cpu_usage;
  }
  if (pcpu < 0) {
   pcpu = limit;
   workingrate = limit;
   twork.tv_nsec = 100000*limit*1000;
  }
  else {
   workingrate = (((workingrate / pcpu * limit)<(1))?(workingrate / pcpu * limit):(1));
   twork.tv_nsec = 100000*1000*workingrate;
  }
  tsleep.tv_nsec = 100000*1000-twork.tv_nsec;
  if (verbose) {
   if (c%200==0)
    printf("\n%%CPU\twork quantum\tsleep quantum\tactive rate\n");
   if (c%10==0 && c>0)
    printf("%0.2lf%%\t%6ld us\t%6ld us\t%0.2lf%%\n",pcpu*100,twork.tv_nsec/1000,tsleep.tv_nsec/1000,workingrate*100);
  }
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (kill(proc->pid,18)!=0) {
    if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
                continue;
   }
            node=node->next;
  }
  gettimeofday(&startwork, ((void *)0));
  nanosleep(&twork,((void *)0));
  gettimeofday(&endwork, ((void *)0));
  workingtime = timediff(&endwork,&startwork);
  long delay = workingtime-twork.tv_nsec/1000;
  if (c>0 && delay>10000) {
  }
  if (tsleep.tv_nsec>0) {
            node=pf.members.first;
   while (node!=((void *)0)) {
    struct process *proc = (struct process*)(node->data);
    if (kill(proc->pid,19)!=0) {
     if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
     remove_process_from_family(&pf, proc->pid);
                    node=pf.members.first;
                    continue;
    }
                node=node->next;
   }
   nanosleep(&tsleep,((void *)0));
  }
  c++;
 }
 cleanup_process_family(&pf);
}

canonical_original_code:
void limit_process(int pid, double limit, int ignore_children)
{
 struct timespec twork;
 struct timespec tsleep;
 struct timeval startwork;
 struct timeval endwork;
 memset(&twork, 0, sizeof(struct timespec));
 memset(&tsleep, 0, sizeof(struct timespec));
 memset(&startwork, 0, sizeof(struct timeval));
 memset(&endwork, 0, sizeof(struct timeval));
 unsigned long workingtime = 0;
 struct list_node *node;
 int c = 0;
 if (create_process_family(&pf, pid) == -1)
    {
        printf("create process family failed");
        return ;
    }
 if (ignore_children) {
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (proc->pid != pid)
            {
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
                continue;
            }
            node = node->next;
  }
 }
 if (!ignore_children && verbose) printf("Members in the family owned by %d: %d\n", pf.father, pf.members.count);
 double workingrate = -1;
 while(1) {
  if (!ignore_children && c%10==0) {
   int new_children = update_process_family(&pf);
   if (verbose && new_children) {
    printf("%d new children processes detected (", new_children);
    int j;
    node = pf.members.last;
    for (j=0; j<new_children; j++) {
     printf("%d", ((struct process *)(node->data))->pid);
     if (j<new_children-1) printf(" ");
     node = node->previous;
    }
    printf(")\n");
   }
  }
  if (pf.members.count==0) {
   if (verbose) printf("No more processes.\n");
   break;
  }
  double pcpu = -1;
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (proc->is_zombie) {
    fprintf(stderr,"Process %d is zombie!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
    continue;
   }
   if (process_monitor(proc) != 0) {
    if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
    continue;
   }
            node = node->next;
   if (proc->cpu_usage<0) {
    continue;
   }
   if (pcpu<0) pcpu = 0;
   pcpu += proc->cpu_usage;
  }
  if (pcpu < 0) {
   pcpu = limit;
   workingrate = limit;
   twork.tv_nsec = 100000*limit*1000;
  }
  else {
   workingrate = (((workingrate / pcpu * limit)<(1))?(workingrate / pcpu * limit):(1));
   twork.tv_nsec = 100000*1000*workingrate;
  }
  tsleep.tv_nsec = 100000*1000-twork.tv_nsec;
  if (verbose) {
   if (c%200==0)
    printf("\n%%CPU\twork quantum\tsleep quantum\tactive rate\n");
   if (c%10==0 && c>0)
    printf("%0.2lf%%\t%6ld us\t%6ld us\t%0.2lf%%\n",pcpu*100,twork.tv_nsec/1000,tsleep.tv_nsec/1000,workingrate*100);
  }
        node = pf.members.first;
  while (node!=((void *)0)) {
   struct process *proc = (struct process*)(node->data);
   if (kill(proc->pid,18)!=0) {
    if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
    remove_process_from_family(&pf, proc->pid);
                node = pf.members.first;
                continue;
   }
            node=node->next;
  }
  gettimeofday(&startwork, ((void *)0));
  nanosleep(&twork,((void *)0));
  gettimeofday(&endwork, ((void *)0));
  workingtime = timediff(&endwork,&startwork);
  long delay = workingtime-twork.tv_nsec/1000;
  if (c>0 && delay>10000) {
  }
  if (tsleep.tv_nsec>0) {
            node=pf.members.first;
   while (node!=((void *)0)) {
    struct process *proc = (struct process*)(node->data);
    if (kill(proc->pid,19)!=0) {
     if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid);
     remove_process_from_family(&pf, proc->pid);
                    node=pf.members.first;
                    continue;
    }
                node=node->next;
   }
   nanosleep(&tsleep,((void *)0));
  }
  c++;
 }
 cleanup_process_family(&pf);
}

variable_types: pid int

variable_types: limit double

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

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

variable_types:
workingtime
unsigned long

variable_types: c int

variable_types: workingrate double

variable_types: j int

variable_types: pcpu double

variable_types: delay long

user_defined_types: struct list_node { void *data; struct list_node *previous; struct list_node *next; }