decompiled_code:
__int64 __fastcall get_addr_sp(int *a1)
{
_QWORD *v1; // rbx
__int64 v3; // rdi
int v4; // eax

v1 = (_QWORD *)sp_list_head;
*a1 = 0;
if ( v1 )
{
v3 = 0LL;
do
{
if ( *(_BYTE *)(*v1 + 16LL) )
{
v3 = insert_node(v3, *v1);
v4 = *a1 + 1;
*a1 = v4;
if ( v4 >= addr_to_send )
break;
}
v1 = (_QWORD *)v1[1];
}
while ( v1 );
if ( v3 )
return v3;
}
fwrite("Nessun super-peer libero trovato\n", 1uLL, 0x21uLL, stderr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int *a1)
{
_QWORD *v1; // rbx
__int64 v3; // rdi
int v4; // eax

v1 = (_QWORD *)sp_list_head;
*a1 = 0;
if ( v1 )
{
v3 = 0LL;
do
{
if ( *(_BYTE *)(*v1 + 16LL) )
{
v3 = insert_node(v3, *v1);
v4 = *a1 + 1;
*a1 = v4;
if ( v4 >= addr_to_send )
break;
}
v1 = (_QWORD *)v1[1];
}
while ( v1 );
if ( v3 )
return v3;
}
fwrite("Nessun super-peer libero trovato\n", 1uLL, 0x21uLL, stderr);
return 0LL;
}

original_code:
struct node *get_addr_sp(int *dim) {
 struct node *ret, *it;
 struct spaddr_node *sp_node;
 it = sp_list_head;
 *dim = 0;
 ret = ((void *)0);
 while (it != ((void *)0)) {
  sp_node = (struct spaddr_node *)it->data;
  if (sp_node->free_spots > 0) {
   ret = insert_node(ret, sp_node);
   (*dim) ++;
   if (*dim >= addr_to_send) {
    break;
   }
  }
  it = it->next;
 }
 if (ret == ((void *)0)) {
  fprintf(stderr, "Nessun super-peer libero trovato\n");
 }
 return ret;
}

canonical_original_code:
struct node *get_addr_sp(int *dim) {
 struct node *ret; struct node *it;
 struct spaddr_node *sp_node;
 it = sp_list_head;
 *dim = 0;
 ret = ((void *)0);
 while (it != ((void *)0)) {
  sp_node = (struct spaddr_node *)it->data;
  if (sp_node->free_spots > 0) {
   ret = insert_node(ret, sp_node);
   (*dim) ++;
   if (*dim >= addr_to_send) {
    break;
   }
  }
  it = it->next;
 }
 if (ret == ((void *)0)) {
  fprintf(stderr, "Nessun super-peer libero trovato\n");
 }
 return ret;
}

variable_types:
dim
int *

return_type: struct node { void *data; struct node *next; struct node *prev; } *


repo: sakkko/Karmanet

decompiled_code:
__int64 __fastcall update_near(int a1, const void *a2, unsigned int a3)
{
__int64 v3; // rbx
__int64 result; // rax
__int64 v5; // rbp

v3 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v3 != a1 )
{
v3 = *(_QWORD *)(v3 + 104);
if ( !v3 )
{
fwrite("update_near error - entry not found\n", (size_t)&loc_1, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
}
v5 = a3;
memcpy((void *)(v3 + 20), a2, a3);
result = 0LL;
*(_WORD *)(v3 + 96) = (unsigned __int64)(2863311531LL * v5) >> 34;
}
else
{
fwrite("update_near error - list is empty\n", 1uLL, 0x22uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, const void *a2, unsigned int a3)
{
__int64 v3; // rbx
__int64 result; // rax
__int64 v5; // rbp

v3 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v3 != a1 )
{
v3 = *(_QWORD *)(v3 + 104);
if ( !v3 )
{
fwrite("update_near error - entry not found\n", (size_t)&loc_1, 0x24uLL, stderr);
return 0xFFFFFFFFLL;
}
}
v5 = a3;
memcpy((void *)(v3 + 20), a2, a3);
result = 0LL;
*(_WORD *)(v3 + 96) = (unsigned __int64)(2863311531LL * v5) >> 34;
}
else
{
fwrite("update_near error - list is empty\n", 1uLL, 0x22uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int update_near(int socksd, const char *data, unsigned int data_len) {
 if (near_list_head == ((void *)0)) {
  fprintf(stderr, "update_near error - list is empty\n");
  return -1;
 }
 struct near_node *iterator = near_list_head;
 while (iterator != ((void *)0)) {
  if (iterator->socksd == socksd) {
   memcpy(iterator->data, data, data_len);
   iterator->near_number = data_len / 6;
   return 0;
  }
  iterator = iterator->next;
 }
 if (iterator == ((void *)0)) {
  fprintf(stderr, "update_near error - entry not found\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int update_near(int socksd, char *data, unsigned int data_len) {
 if (near_list_head == ((void *)0)) {
  fprintf(stderr, "update_near error - list is empty\n");
  return -1;
 }
 struct near_node *iterator = near_list_head;
 while (iterator != ((void *)0)) {
  if (iterator->socksd == socksd) {
   memcpy(iterator->data, data, data_len);
   iterator->near_number = data_len / 6;
   return 0;
  }
  iterator = iterator->next;
 }
 if (iterator == ((void *)0)) {
  fprintf(stderr, "update_near error - entry not found\n");
  return -1;
 }
 return 0;
}

variable_types: socksd int

variable_types:
data
char *

variable_types: struct near_node { int socksd; 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]; } addr; char data[36]; 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; short near_number; struct near_node *next; struct near_node *prev; } *

return_type: int

user_defined_types: struct near_node { int socksd; struct sockaddr_in addr; char data[36]; union pthread_mutex_t mutex; short near_number; struct near_node *next; struct near_node *prev; }

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

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


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

for ( result = near_list_head; result; result = *(_QWORD *)(result + 104) )
{
if ( *(_DWORD *)result == a1 )
break;
}
return result;
}

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

for ( result = near_list_head; result; result = *(_QWORD *)(result + 104) )
{
if ( *(_DWORD *)result == a1 )
break;
}
return result;
}

original_code:
struct near_node *get_near_node(int socksd) {
 struct near_node * iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (iterator->socksd == socksd) {
   break;
  }
  iterator = iterator->next;
 }
 return iterator;
}

canonical_original_code:
struct near_node *get_near_node(int socksd) {
 struct near_node *iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (iterator->socksd == socksd) {
   break;
  }
  iterator = iterator->next;
 }
 return iterator;
}

return_type: struct near_node { int socksd; 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]; } addr; char data[36]; 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; short near_number; struct near_node *next; struct near_node *prev; } *


decompiled_code:
__int64 __fastcall get_near_by_addr(__int64 a1)
{
__int64 v1; // r12

v1 = near_list_head;
if ( !near_list_head )
return 0LL;
while ( !(unsigned int)addrcmp(v1 + 4, a1) )
{
v1 = *(_QWORD *)(v1 + 104);
if ( !v1 )
return 0LL;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r12

v1 = near_list_head;
if ( !near_list_head )
return 0LL;
while ( !(unsigned int)addrcmp(v1 + 4, a1) )
{
v1 = *(_QWORD *)(v1 + 104);
if ( !v1 )
return 0LL;
}
return v1;
}

original_code:
struct near_node *get_near_by_addr(const struct sockaddr_in *addr) {
 struct near_node * iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (addrcmp(&iterator->addr, addr)) {
   break;
  }
  iterator = iterator->next;
 }
 return iterator;
}

canonical_original_code:
struct near_node *get_near_by_addr(struct sockaddr_in *addr) {
 struct near_node *iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (addrcmp(&iterator->addr, addr)) {
   break;
  }
  iterator = iterator->next;
 }
 return iterator;
}

variable_types:
addr
struct sockaddr_in *

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall insert_near(int a1, _OWORD *a2)
{
__int64 v2; // rax
_DWORD *v3; // rax
__int64 v4; // rdx
__int64 v5; // rcx
__int64 result; // rax
_DWORD *v7; // rax

v2 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v2 != a1 )
{
v2 = *(_QWORD *)(v2 + 104);
if ( !v2 )
{
v3 = create_new_near_node(a1, (__int64)a2);
v4 = near_list_head;
v5 = *(_QWORD *)(near_list_head + 104);
*((_QWORD *)v3 + 14) = near_list_head;
*((_QWORD *)v3 + 13) = v5;
if ( v5 )
*(_QWORD *)(v5 + 112) = v3;
*(_QWORD *)(v4 + 104) = v3;
return 0LL;
}
}
if ( *a2 == *(_OWORD *)(v2 + 4) )
return 0LL;
fwrite("insert_near error - list is not consistent\n", 1uLL, 0x2BuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v7 = create_new_near_node(a1, (__int64)a2);
near_list_head = (__int64)v7;
*((_QWORD *)v7 + 13) = 0LL;
*((_QWORD *)v7 + 14) = 0LL;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, _OWORD *a2)
{
__int64 v2; // rax
_DWORD *v3; // rax
__int64 v4; // rdx
__int64 v5; // rcx
__int64 result; // rax
_DWORD *v7; // rax

v2 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v2 != a1 )
{
v2 = *(_QWORD *)(v2 + 104);
if ( !v2 )
{
v3 = create_new_near_node(a1, (__int64)a2);
v4 = near_list_head;
v5 = *(_QWORD *)(near_list_head + 104);
*((_QWORD *)v3 + 14) = near_list_head;
*((_QWORD *)v3 + 13) = v5;
if ( v5 )
*(_QWORD *)(v5 + 112) = v3;
*(_QWORD *)(v4 + 104) = v3;
return 0LL;
}
}
if ( *a2 == *(_OWORD *)(v2 + 4) )
return 0LL;
fwrite("insert_near error - list is not consistent\n", 1uLL, 0x2BuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v7 = create_new_near_node(a1, (__int64)a2);
near_list_head = (__int64)v7;
*((_QWORD *)v7 + 13) = 0LL;
*((_QWORD *)v7 + 14) = 0LL;
result = 0LL;
}
return result;
}

original_code:
int insert_near(int socksd, const struct sockaddr_in *addr){
 struct near_node *iterator;
 struct near_node *to_create;
 if (near_list_head == ((void *)0)){
  near_list_head = create_new_near_node(socksd, addr);
  near_list_head->next = ((void *)0);
  near_list_head->prev = ((void *)0);
  return 0;
 }
 iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (iterator->socksd == socksd) {
   if (!memcmp(&(iterator->addr), addr, sizeof(struct sockaddr_in))) {
    return 0;
   } else {
    fprintf(stderr, "insert_near error - list is not consistent\n");
    return -1;
   }
  }
  iterator = iterator->next;
 }
 to_create = create_new_near_node(socksd, addr);
 to_create->next = near_list_head->next;
 to_create->prev = near_list_head;
 if (near_list_head->next != ((void *)0)) {
  near_list_head->next->prev = to_create;
 }
 near_list_head->next = to_create;
 return 0;
}

canonical_original_code:
int insert_near(int socksd, struct sockaddr_in *addr){
 struct near_node *iterator;
 struct near_node *to_create;
 if (near_list_head == ((void *)0)){
  near_list_head = create_new_near_node(socksd, addr);
  near_list_head->next = ((void *)0);
  near_list_head->prev = ((void *)0);
  return 0;
 }
 iterator = near_list_head;
 while(iterator != ((void *)0)){
  if (iterator->socksd == socksd) {
   if (!memcmp(&(iterator->addr), addr, sizeof(struct sockaddr_in))) {
    return 0;
   } else {
    fprintf(stderr, "insert_near error - list is not consistent\n");
    return -1;
   }
  }
  iterator = iterator->next;
 }
 to_create = create_new_near_node(socksd, addr);
 to_create->next = near_list_head->next;
 to_create->prev = near_list_head;
 if (near_list_head->next != ((void *)0)) {
  near_list_head->next->prev = to_create;
 }
 near_list_head->next = to_create;
 return 0;
}

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


original_code:
struct near_node *remove_near_node(struct near_node *head, struct near_node *toremove) {
 if (toremove == ((void *)0)) {
  return head;
 }
 if (toremove->prev == ((void *)0)) {
  if (toremove->next == ((void *)0)) {
   head = ((void *)0);
  } else {
   toremove->next->prev = ((void *)0);
   head = toremove->next;
  }
 } else {
  if (toremove->next == ((void *)0)) {
   toremove->prev->next = ((void *)0);
  } else {
    toremove->prev->next = toremove->next;
    toremove->next->prev = toremove->prev;
  }
 }
 free(toremove);
 toremove = ((void *)0);
 return head;
}

variable_types:
head
struct near_node *

variable_types:
toremove
struct near_node *


decompiled_code:
__int64 __fastcall delete_near(int a1)
{
__int64 v1; // rbp
int v2; // eax
char *v4; // rax

v1 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v1 != a1 )
{
v1 = *(_QWORD *)(v1 + 104);
if ( !v1 )
return 0xFFFFFFFFLL;
}
v2 = pthread_mutex_destroy((pthread_mutex_t *)(v1 + 56));
if ( !v2 )
{
near_list_head = remove_near_node(near_list_head, (_QWORD *)v1);
return 0LL;
}
v4 = strerror(v2);
fprintf(stderr, "delete_near error - can't destroy lock: %s\n", v4);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
__int64 v1; // rbp
int v2; // eax
char *v4; // rax

v1 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v1 != a1 )
{
v1 = *(_QWORD *)(v1 + 104);
if ( !v1 )
return 0xFFFFFFFFLL;
}
v2 = pthread_mutex_destroy((pthread_mutex_t *)(v1 + 56));
if ( !v2 )
{
near_list_head = func4(near_list_head, (_QWORD *)v1);
return 0LL;
}
v4 = strerror(v2);
fprintf(stderr, "delete_near error - can't destroy lock: %s\n", v4);
}
return 0xFFFFFFFFLL;
}

original_code:
int delete_near(int socksd) {
 struct near_node *iterator = near_list_head;
 int rc;
 while(iterator != ((void *)0)){
  if (iterator->socksd == socksd) {
   if ((rc = pthread_mutex_destroy(&iterator->mutex)) != 0) {
    fprintf(stderr, "delete_near error - can't destroy lock: %s\n", strerror(rc));
    return -1;
   }
   near_list_head = remove_near_node(near_list_head, iterator);
   return 0;
  }
  iterator = iterator->next;
 }
 return -1;
}

variable_types: rc int


decompiled_code:
__int64 __fastcall sp_register(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbp
int v7; // eax
int v8; // eax
unsigned int v9; // er14
char *v11; // rax
char *v12; // rax
_BYTE v13[1080]; // [rsp+0h] [rbp-438h] BYREF

puts("SP REGISTER");
if ( *(_WORD *)(a3 + 8) == 1 )
{
v6 = (pthread_mutex_t *)(a4 + 8);
v7 = pthread_mutex_lock(v6);
if ( v7 )
{
v9 = -1;
v11 = strerror(v7);
fprintf(stderr, "sp_register error - can't acquire lock: %s\n", v11);
}
else
{
insert_sp(a2, *(unsigned __int8 *)(a3 + 10));
v8 = pthread_mutex_unlock(v6);
v9 = v8;
if ( v8 )
{
v9 = -1;
v12 = strerror(v8);
fprintf(stderr, "sp_register error - can't release lock: %s\n", v12);
}
else
{
new_ack_packet(v13, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v13) < 0 )
{
v9 = -1;
perror("errore in sendto");
}
}
}
}
else
{
v9 = -1;
fwrite("sp_register error - invalid packet format\n", 1uLL, 0x2AuLL, stderr);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbp
int v7; // eax
int v8; // eax
unsigned int v9; // er14
char *v11; // rax
char *v12; // rax
_BYTE v13[1080]; // [rsp+0h] [rbp-438h] BYREF

puts("SP REGISTER");
if ( *(_WORD *)(a3 + 8) == 1 )
{
v6 = (pthread_mutex_t *)(a4 + 8);
v7 = pthread_mutex_lock(v6);
if ( v7 )
{
v9 = -1;
v11 = strerror(v7);
fprintf(stderr, "sp_register error - can't acquire lock: %s\n", v11);
}
else
{
insert_sp(a2, *(unsigned __int8 *)(a3 + 10));
v8 = pthread_mutex_unlock(v6);
v9 = v8;
if ( v8 )
{
v9 = -1;
v12 = strerror(v8);
fprintf(stderr, "sp_register error - can't release lock: %s\n", v12);
}
else
{
new_ack_packet(v13, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v13) < 0 )
{
v9 = -1;
perror("errore in sendto");
}
}
}
}
else
{
v9 = -1;
fwrite("sp_register error - invalid packet format\n", 1uLL, 0x2AuLL, stderr);
}
return v9;
}

original_code:
int sp_register(int sockfd, const struct sockaddr_in *addr, const struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet send_pack;
 int rc;
 printf("SP REGISTER\n");
 if (pck->data_len != 1) {
  fprintf(stderr, "sp_register error - invalid packet format\n");
  return -1;
 }
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_register error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 insert_sp(addr, *pck->data);
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_register error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 new_ack_packet(&send_pack, pck->index);
 if (send_packet(sockfd, addr, &send_pack) < 0) {
  perror("errore in sendto");
  return -1;
 }
 return 0;
}

canonical_original_code:
int sp_register(int sockfd, struct sockaddr_in *addr, struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet send_pack;
 int rc;
 printf("SP REGISTER\n");
 if (pck->data_len != 1) {
  fprintf(stderr, "sp_register error - invalid packet format\n");
  return -1;
 }
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_register error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 insert_sp(addr, *pck->data);
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_register error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 new_ack_packet(&send_pack, pck->index);
 if (send_packet(sockfd, addr, &send_pack) < 0) {
  perror("errore in sendto");
  return -1;
 }
 return 0;
}

variable_types: sockfd int

variable_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; } *

variable_types: struct sp_list_checker_info { struct th_info { unsigned long thread; 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; int go; } thinfo; struct node { void *data; struct node *next; struct node *prev; } **sp_list; } *

user_defined_types: struct sp_list_checker_info { struct th_info thinfo; struct node **sp_list; }

user_defined_types: struct th_info { unsigned long thread; union pthread_mutex_t mutex; int go; }


decompiled_code:
__int64 __fastcall sp_leave(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v4; // r12
int v6; // eax
int v7; // eax
unsigned int v8; // er12
char *v10; // rax
char *v11; // rax
_BYTE v12[1080]; // [rsp+0h] [rbp-438h] BYREF

v4 = (pthread_mutex_t *)(a4 + 8);
v6 = pthread_mutex_lock((pthread_mutex_t *)(a4 + 8));
if ( v6 )
{
v8 = -1;
v10 = strerror(v6);
fprintf(stderr, "sp_leave error - can't acquire lock: %s\n", v10);
}
else
{
remove_sp(a2);
v7 = pthread_mutex_unlock(v4);
v8 = v7;
if ( v7 )
{
v8 = -1;
v11 = strerror(v7);
fprintf(stderr, "sp_leave error - can't release lock: %s\n", v11);
}
else
{
new_ack_packet(v12, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v12) < 0 )
{
v8 = -1;
perror("errore in sendto");
}
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v4; // r12
int v6; // eax
int v7; // eax
unsigned int v8; // er12
char *v10; // rax
char *v11; // rax
_BYTE v12[1080]; // [rsp+0h] [rbp-438h] BYREF

v4 = (pthread_mutex_t *)(a4 + 8);
v6 = pthread_mutex_lock((pthread_mutex_t *)(a4 + 8));
if ( v6 )
{
v8 = -1;
v10 = strerror(v6);
fprintf(stderr, "sp_leave error - can't acquire lock: %s\n", v10);
}
else
{
remove_sp(a2);
v7 = pthread_mutex_unlock(v4);
v8 = v7;
if ( v7 )
{
v8 = -1;
v11 = strerror(v7);
fprintf(stderr, "sp_leave error - can't release lock: %s\n", v11);
}
else
{
new_ack_packet(v12, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v12) < 0 )
{
v8 = -1;
perror("errore in sendto");
}
}
}
return v8;
}

original_code:
int sp_leave(int sockfd, const struct sockaddr_in *addr, const struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet ack_packet;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_leave error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 remove_sp(addr);
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_leave error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 new_ack_packet(&ack_packet, pck->index);
 if (send_packet(sockfd, addr, &ack_packet) < 0) {
  perror("errore in sendto");
  return -1;
 }
 return 0;
}

canonical_original_code:
int sp_leave(int sockfd, struct sockaddr_in *addr, struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet ack_packet;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_leave error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 remove_sp(addr);
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_leave error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 new_ack_packet(&ack_packet, pck->index);
 if (send_packet(sockfd, addr, &ack_packet) < 0) {
  perror("errore in sendto");
  return -1;
 }
 return 0;
}

variable_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; }

user_defined_types: struct node { void *data; struct node *next; struct node *prev; }


decompiled_code:
__int64 __fastcall set_str_addrlist(__int64 a1, int a2, __int64 a3)
{
pthread_mutex_t *v3; // r12
int v5; // eax
int v6; // eax
signed int v7; // ebp
__int64 v8; // rax
__int64 v9; // r13
__int64 v10; // rdi
int v11; // eax
int v13; // eax
__int64 v14; // r13
_QWORD *v15; // r12
__int64 v16; // rdi
struct in_addr *v17; // rcx
unsigned __int16 v18; // bp
char *v19; // rax
int v20; // edi
char *v21; // rax
char *v22; // rax
unsigned int v23[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = (pthread_mutex_t *)(a3 + 8);
if ( !a2 )
{
v5 = addr_to_send;
if ( addr_to_send > sp_count )
v5 = sp_count;
v23[0] = v5;
v6 = pthread_mutex_lock(v3);
v7 = v6;
if ( !v6 )
{
v8 = get_addr();
v9 = it_addr;
if ( (int)v23[0] > 0 )
{
do
{
v10 = a1;
++v7;
a1 += 6LL;
addr2str(v10, *(unsigned int *)(v8 + 4), *(unsigned __int16 *)(v8 + 2));
v8 = get_addr();
}
while ( (int)v23[0] > v7 );
}
it_addr = v9;
v11 = pthread_mutex_unlock(v3);
if ( !v11 )
return v23[0];
goto LABEL_17;
}
v20 = v6;
LABEL_16:
v21 = strerror(v20);
fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", v21);
return 0xFFFFFFFFLL;
}
v13 = pthread_mutex_lock((pthread_mutex_t *)(a3 + 8));
if ( v13 )
{
v20 = v13;
goto LABEL_16;
}
v14 = get_addr_sp(v23);
v11 = pthread_mutex_unlock(v3);
if ( v11 )
{
LABEL_17:
v22 = strerror(v11);
fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", v22);
return 0xFFFFFFFFLL;
}
if ( v14 )
{
v15 = (_QWORD *)v14;
do
{
v16 = a1;
a1 += 6LL;
addr2str(v16, *(unsigned int *)(*v15 + 4LL), *(unsigned __int16 *)(*v15 + 2LL));
v18 = _bswap_16(*(_WORD *)(*v15 + 2LL));
v19 = inet_ntoa(v17[1]);
printf("INDIRIZZO SP: %s:%u\n", v19, v18);
v15 = (_QWORD *)v15[1];
}
while ( v15 );
free_list(v14);
return v23[0];
}
fwrite("set_str_addrlist error - no free super-peer found\n", 1uLL, 0x32uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3)
{
pthread_mutex_t *v3; // r12
int v5; // eax
int v6; // eax
signed int v7; // ebp
__int64 v8; // rax
__int64 v9; // r13
__int64 v10; // rdi
int v11; // eax
int v13; // eax
__int64 v14; // r13
_QWORD *v15; // r12
__int64 v16; // rdi
struct in_addr *v17; // rcx
unsigned __int16 v18; // bp
char *v19; // rax
int v20; // edi
char *v21; // rax
char *v22; // rax
unsigned int v23[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v3 = (pthread_mutex_t *)(a3 + 8);
if ( !a2 )
{
v5 = addr_to_send;
if ( addr_to_send > sp_count )
v5 = sp_count;
v23[0] = v5;
v6 = pthread_mutex_lock(v3);
v7 = v6;
if ( !v6 )
{
v8 = get_addr();
v9 = it_addr;
if ( (int)v23[0] > 0 )
{
do
{
v10 = a1;
++v7;
a1 += 6LL;
addr2str(v10, *(unsigned int *)(v8 + 4), *(unsigned __int16 *)(v8 + 2));
v8 = get_addr();
}
while ( (int)v23[0] > v7 );
}
it_addr = v9;
v11 = pthread_mutex_unlock(v3);
if ( !v11 )
return v23[0];
goto LABEL_17;
}
v20 = v6;
LABEL_16:
v21 = strerror(v20);
fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", v21);
return 0xFFFFFFFFLL;
}
v13 = pthread_mutex_lock((pthread_mutex_t *)(a3 + 8));
if ( v13 )
{
v20 = v13;
goto LABEL_16;
}
v14 = get_addr_sp(v23);
v11 = pthread_mutex_unlock(v3);
if ( v11 )
{
LABEL_17:
v22 = strerror(v11);
fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", v22);
return 0xFFFFFFFFLL;
}
if ( v14 )
{
v15 = (_QWORD *)v14;
do
{
v16 = a1;
a1 += 6LL;
addr2str(v16, *(unsigned int *)(*v15 + 4LL), *(unsigned __int16 *)(*v15 + 2LL));
v18 = func0(*(_WORD *)(*v15 + 2LL));
v19 = inet_ntoa(v17[1]);
printf("INDIRIZZO SP: %s:%u\n", v19, v18);
v15 = (_QWORD *)v15[1];
}
while ( v15 );
free_list(v14);
return v23[0];
}
fwrite("set_str_addrlist error - no free super-peer found\n", 1uLL, 0x32uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int set_str_addrlist(char *str, int superpeer, struct sp_list_checker_info *splchinfo) {
 char *ptr;
 int i, rc, ret;
 struct sockaddr_in *saddr;
 struct node *it_tmp, *it_tmp2;
 if (superpeer == 0) {
  ptr = str;
  ret = addr_to_send;
  if (sp_count < addr_to_send) {
   ret = sp_count;
  }
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", strerror(rc));
   return -1;
  }
  saddr = get_addr();
  it_tmp = it_addr;
  for (i = 0; i < ret; i ++) {
   addr2str(ptr, saddr->sin_addr.s_addr, saddr->sin_port);
   ptr += 6;
   saddr = get_addr();
  }
  it_addr = it_tmp;
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
 } else {
  ptr = str;
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", strerror(rc));
   return -1;
  }
  it_tmp = get_addr_sp(&ret);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  if (it_tmp == ((void *)0)) {
   fprintf(stderr, "set_str_addrlist error - no free super-peer found\n");
   return -1;
  }
  it_tmp2 = it_tmp;
  while (it_tmp2 != ((void *)0)) {
   addr2str(ptr, ((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_addr.s_addr, ((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_port);
   printf("INDIRIZZO SP: %s:%u\n", inet_ntoa(((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_addr), ntohs(((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_port));
   ptr += 6;
   it_tmp2 = it_tmp2->next;
  }
  free_list(it_tmp);
 }
 return ret;
}

canonical_original_code:
int set_str_addrlist(char *str, int superpeer, struct sp_list_checker_info *splchinfo) {
 char *ptr;
 int i; int rc; int ret;
 struct sockaddr_in *saddr;
 struct node *it_tmp; struct node *it_tmp2;
 if (superpeer == 0) {
  ptr = str;
  ret = addr_to_send;
  if (sp_count < addr_to_send) {
   ret = sp_count;
  }
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", strerror(rc));
   return -1;
  }
  saddr = get_addr();
  it_tmp = it_addr;
  for (i = 0; i < ret; i ++) {
   addr2str(ptr, saddr->sin_addr.s_addr, saddr->sin_port);
   ptr += 6;
   saddr = get_addr();
  }
  it_addr = it_tmp;
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
 } else {
  ptr = str;
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't acquire lock: %s\n", strerror(rc));
   return -1;
  }
  it_tmp = get_addr_sp(&ret);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "set_str_addrlist error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  if (it_tmp == ((void *)0)) {
   fprintf(stderr, "set_str_addrlist error - no free super-peer found\n");
   return -1;
  }
  it_tmp2 = it_tmp;
  while (it_tmp2 != ((void *)0)) {
   addr2str(ptr, ((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_addr.s_addr, ((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_port);
   printf("INDIRIZZO SP: %s:%u\n", inet_ntoa(((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_addr), ntohs(((struct spaddr_node *)it_tmp2->data)->sp_addr.sin_port));
   ptr += 6;
   it_tmp2 = it_tmp2->next;
  }
  free_list(it_tmp);
 }
 return ret;
}

variable_types:
str
char *

variable_types: superpeer int

variable_types:
ptr
char *

variable_types: i int

variable_types: ret int

variable_types: struct node { void *data; struct node *next; struct node *prev; } *

user_defined_types: struct spaddr_node { struct sockaddr_in sp_addr; unsigned char free_spots; short flag; }


decompiled_code:
__int64 __fastcall send_addr_list(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
int v7; // eax
__int64 result; // rax
char v9[12]; // [rsp+4h] [rbp-444h] BYREF
char v10[1080]; // [rsp+10h] [rbp-438h] BYREF

v6 = is_set_flag(a3, 4294967168LL);
v7 = set_str_addrlist((__int64)v9, v6, a4);
if ( v7 < 0 )
{
fwrite("send_addr_list error - set_str_addrlist failed\n", 1uLL, 0x2FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
new_packet(v10, "LIS", *(unsigned __int16 *)(a3 + 4), v9, (unsigned __int16)(6 * v7), 1LL);
result = (unsigned int)((int)send_packet(a1, a2, v10) >> 31);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
int v6; // eax
int v7; // eax
__int64 result; // rax
char v9[12]; // [rsp+4h] [rbp-444h] BYREF
char v10[1080]; // [rsp+10h] [rbp-438h] BYREF

v6 = is_set_flag(a3, 4294967168LL);
v7 = func3((__int64)v9, v6, a4);
if ( v7 < 0 )
{
fwrite("send_addr_list error - set_str_addrlist failed\n", 1uLL, 0x2FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
new_packet(v10, "LIS", *(unsigned __int16 *)(a3 + 4), v9, (unsigned __int16)(6 * v7), 1LL);
result = (unsigned int)((int)send_packet(a1, a2, v10) >> 31);
}
return result;
}

original_code:
int send_addr_list(int sockfd, const struct sockaddr_in *addr, const struct packet *pck, struct sp_list_checker_info *splchinfo) {
 char str_to_send[6 * 2];
 int dim;
 struct packet send_list;
 dim = set_str_addrlist(str_to_send, is_set_flag(pck, 128), splchinfo);
 if (dim < 0) {
  fprintf(stderr, "send_addr_list error - set_str_addrlist failed\n");
  return -1;
 }
 new_packet(&send_list, "LIS", pck->index, str_to_send, dim * 6, 1);
 if (send_packet(sockfd, addr, &send_list) < 0) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int send_addr_list(int sockfd, struct sockaddr_in *addr, struct packet *pck, struct sp_list_checker_info *splchinfo) {
 char str_to_send[-1];
 int dim;
 struct packet send_list;
 dim = set_str_addrlist(str_to_send, is_set_flag(pck, 128), splchinfo);
 if (dim < 0) {
  fprintf(stderr, "send_addr_list error - set_str_addrlist failed\n");
  return -1;
 }
 new_packet(&send_list, "LIS", pck->index, str_to_send, dim * 6, 1);
 if (send_packet(sockfd, addr, &send_list) < 0) {
  return -1;
 }
 return 0;
}

variable_types: dim int

user_defined_types: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; }


decompiled_code:
__int64 __fastcall sp_join(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbx
int v7; // eax
unsigned int v8; // er12
int v9; // eax
char *v11; // rax
char *v12; // rax
__int64 v13; // [rsp+0h] [rbp-448h] BYREF
__int16 v14; // [rsp+8h] [rbp-440h]
char v15[1086]; // [rsp+Ah] [rbp-43Eh] BYREF

v6 = (pthread_mutex_t *)(a4 + 8);
v7 = pthread_mutex_lock((pthread_mutex_t *)(a4 + 8));
v8 = v7;
if ( v7 )
{
v8 = -1;
v12 = strerror(v7);
fprintf(stderr, "sp_join error - can't acquire lock: %s\n", v12);
return v8;
}
if ( !sp_list_head )
{
puts("lista vuota inviato promote");
insert_sp(a2, 4LL);
v9 = pthread_mutex_unlock(v6);
if ( !v9 )
{
new_promote_packet(&v13, *(unsigned __int16 *)(a3 + 4));
addr2str(v15, *(unsigned int *)(a2 + 4), *(unsigned __int16 *)(a2 + 2));
v14 = 6;
LABEL_11:
if ( (int)send_packet(a1, a2, &v13) < 0 )
{
v8 = -1;
perror("sp_join error - send_packet failed");
}
return v8;
}
LABEL_15:
v8 = -1;
v11 = strerror(v9);
fprintf(stderr, "sp_join error - can't release lock: %s\n", v11);
return v8;
}
if ( sp_count == 1 && (unsigned int)addrcmp(*(_QWORD *)sp_list_head, a2) )
{
v9 = pthread_mutex_unlock(v6);
if ( !v9 )
{
new_promote_packet(&v13, *(unsigned __int16 *)(a3 + 4));
addr2str(v15, *(unsigned int *)(a2 + 4), *(unsigned __int16 *)(a2 + 2));
v14 = 6;
puts("INVIO PROMOTE");
goto LABEL_11;
}
goto LABEL_15;
}
v9 = pthread_mutex_unlock(v6);
if ( v9 )
goto LABEL_15;
if ( (int)send_addr_list(a1, a2, a3, a4) < 0 )
{
v8 = -1;
fwrite("sp_join error - send_addr_list failed", 1uLL, 0x25uLL, stderr);
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbx
int v7; // eax
unsigned int v8; // er12
int v9; // eax
char *v11; // rax
char *v12; // rax
__int64 v13; // [rsp+0h] [rbp-448h] BYREF
__int16 v14; // [rsp+8h] [rbp-440h]
char v15[1086]; // [rsp+Ah] [rbp-43Eh] BYREF

v6 = (pthread_mutex_t *)(a4 + 8);
v7 = pthread_mutex_lock((pthread_mutex_t *)(a4 + 8));
v8 = v7;
if ( v7 )
{
v8 = -1;
v12 = strerror(v7);
fprintf(stderr, "sp_join error - can't acquire lock: %s\n", v12);
return v8;
}
if ( !sp_list_head )
{
puts("lista vuota inviato promote");
insert_sp(a2, 4LL);
v9 = pthread_mutex_unlock(v6);
if ( !v9 )
{
new_promote_packet(&v13, *(unsigned __int16 *)(a3 + 4));
addr2str(v15, *(unsigned int *)(a2 + 4), *(unsigned __int16 *)(a2 + 2));
v14 = 6;
LABEL_11:
if ( (int)send_packet(a1, a2, &v13) < 0 )
{
v8 = -1;
perror("sp_join error - send_packet failed");
}
return v8;
}
LABEL_15:
v8 = -1;
v11 = strerror(v9);
fprintf(stderr, "sp_join error - can't release lock: %s\n", v11);
return v8;
}
if ( sp_count == 1 && (unsigned int)addrcmp(*(_QWORD *)sp_list_head, a2) )
{
v9 = pthread_mutex_unlock(v6);
if ( !v9 )
{
new_promote_packet(&v13, *(unsigned __int16 *)(a3 + 4));
addr2str(v15, *(unsigned int *)(a2 + 4), *(unsigned __int16 *)(a2 + 2));
v14 = 6;
puts("INVIO PROMOTE");
goto LABEL_11;
}
goto LABEL_15;
}
v9 = pthread_mutex_unlock(v6);
if ( v9 )
goto LABEL_15;
if ( (int)func4(a1, a2, a3, a4) < 0 )
{
v8 = -1;
fwrite("sp_join error - send_addr_list failed", 1uLL, 0x25uLL, stderr);
}
return v8;
}

original_code:
int sp_join(int sockfd, const struct sockaddr_in *addr, const struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet promote;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_join error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (sp_list_head == ((void *)0)) {
  printf("lista vuota inviato promote\n");
  insert_sp(addr, 2 * 2);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  new_promote_packet(&promote, pck->index);
  addr2str(promote.data, addr->sin_addr.s_addr, addr->sin_port);
  promote.data_len = 6;
  if (send_packet(sockfd, addr, &promote) < 0) {
   perror("sp_join error - send_packet failed");
   return -1;
  }
 } else {
  if (sp_count == 1) {
   if (addrcmp(&((struct spaddr_node *)sp_list_head->data)->sp_addr, addr)) {
    if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
     fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
     return -1;
    }
    new_promote_packet(&promote, pck->index);
    addr2str(promote.data, addr->sin_addr.s_addr, addr->sin_port);
    promote.data_len = 6;
    printf("INVIO PROMOTE\n");
    if (send_packet(sockfd, addr, &promote) < 0) {
     perror("sp_join error - send_packet failed");
     return -1;
    }
    return 0;
   }
  }
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  if (send_addr_list(sockfd, addr, pck, splchinfo) < 0) {
   fprintf(stderr, "sp_join error - send_addr_list failed");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int sp_join(int sockfd, struct sockaddr_in *addr, struct packet *pck, struct sp_list_checker_info *splchinfo) {
 struct packet promote;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "sp_join error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 if (sp_list_head == ((void *)0)) {
  printf("lista vuota inviato promote\n");
  insert_sp(addr, 2 * 2);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  new_promote_packet(&promote, pck->index);
  addr2str(promote.data, addr->sin_addr.s_addr, addr->sin_port);
  promote.data_len = 6;
  if (send_packet(sockfd, addr, &promote) < 0) {
   perror("sp_join error - send_packet failed");
   return -1;
  }
 } else {
  if (sp_count == 1) {
   if (addrcmp(&((struct spaddr_node *)sp_list_head->data)->sp_addr, addr)) {
    if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
     fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
     return -1;
    }
    new_promote_packet(&promote, pck->index);
    addr2str(promote.data, addr->sin_addr.s_addr, addr->sin_port);
    promote.data_len = 6;
    printf("INVIO PROMOTE\n");
    if (send_packet(sockfd, addr, &promote) < 0) {
     perror("sp_join error - send_packet failed");
     return -1;
    }
    return 0;
   }
  }
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_join error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  if (send_addr_list(sockfd, addr, pck, splchinfo) < 0) {
   fprintf(stderr, "sp_join error - send_addr_list failed");
   return -1;
  }
 }
 return 0;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebx
int v6; // [rsp+Ch] [rbp-46Ch] BYREF
char v7[16]; // [rsp+10h] [rbp-468h] BYREF
char v8[56]; // [rsp+20h] [rbp-458h] BYREF
__int64 *v9; // [rsp+58h] [rbp-420h]
char v10; // [rsp+60h] [rbp-418h] BYREF
char v11; // [rsp+61h] [rbp-417h]
char v12; // [rsp+62h] [rbp-416h]
char v13; // [rsp+6Ah] [rbp-40Eh]

v3 = udp_socket(argc, argv, envp);
if ( v3 < 0 )
{
perror("socket error");
}
else
{
v4 = v3;
set_addr_any(v7, 5193LL);
if ( (int)inet_bind(v4, v7) < 0 )
{
perror("bind error");
}
else
{
addr_to_send = 2;
v9 = &sp_list_head;
if ( (int)sp_list_checker_run(v8) < 0 )
{
fwrite("Can't start superpeer list checker\n", 1uLL, 0x23uLL, stderr);
}
else
{
v6 = 16;
while ( (int)recvfrom_packet(v4, v7, &v10, &v6) >= 0 )
{
if ( v10 == 80 && v11 == 73 && v12 == 78 )
{
if ( (int)update_sp_flag(v8, v7, (unsigned int)v13) < 0 )
fwrite("Ping error\n", 1uLL, 0xBuLL, stderr);
}
else if ( v10 == 74 && v11 == 79 && v12 == 73 )
{
if ( (int)sp_join(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
fwrite("Join error\n", 1uLL, 0xBuLL, stderr);
}
else if ( v10 == 76 && v11 == 69 && v12 == 65 )
{
if ( (int)sp_leave(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
fwrite("Leave error\n", 1uLL, 0xCuLL, stderr);
}
else if ( v10 == 82
&& v11 == 69
&& v12 == 71
&& (int)sp_register(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
{
fwrite("Register error\n", 1uLL, 0xFuLL, stderr);
}
}
perror("errore in recvfrom");
}
}
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebx
int v6; // [rsp+Ch] [rbp-46Ch] BYREF
char v7[16]; // [rsp+10h] [rbp-468h] BYREF
char v8[56]; // [rsp+20h] [rbp-458h] BYREF
__int64 *v9; // [rsp+58h] [rbp-420h]
char v10; // [rsp+60h] [rbp-418h] BYREF
char v11; // [rsp+61h] [rbp-417h]
char v12; // [rsp+62h] [rbp-416h]
char v13; // [rsp+6Ah] [rbp-40Eh]

v3 = udp_socket(argc, argv, envp);
if ( v3 < 0 )
{
perror("socket error");
}
else
{
v4 = v3;
set_addr_any(v7, 5193LL);
if ( (int)inet_bind(v4, v7) < 0 )
{
perror("bind error");
}
else
{
addr_to_send = 2;
v9 = &sp_list_head;
if ( (int)sp_list_checker_run(v8) < 0 )
{
fwrite("Can't start superpeer list checker\n", 1uLL, 0x23uLL, stderr);
}
else
{
v6 = 16;
while ( (int)recvfrom_packet(v4, v7, &v10, &v6) >= 0 )
{
if ( v10 == 80 && v11 == 73 && v12 == 78 )
{
if ( (int)update_sp_flag(v8, v7, (unsigned int)v13) < 0 )
fwrite("Ping error\n", 1uLL, 0xBuLL, stderr);
}
else if ( v10 == 74 && v11 == 79 && v12 == 73 )
{
if ( (int)func5(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
fwrite("Join error\n", 1uLL, 0xBuLL, stderr);
}
else if ( v10 == 76 && v11 == 69 && v12 == 65 )
{
if ( (int)func2(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
fwrite("Leave error\n", 1uLL, 0xCuLL, stderr);
}
else if ( v10 == 82
&& v11 == 69
&& v12 == 71
&& (int)func1(v4, (__int64)v7, (__int64)&v10, (__int64)v8) < 0 )
{
fwrite("Register error\n", 1uLL, 0xFuLL, stderr);
}
}
perror("errore in recvfrom");
}
}
}
return 1;
}

original_code:
int main(int argc, char **argv) {
 int sockfd;
   int len;
   struct sockaddr_in addr;
 int n;
 struct packet recv_pck;
 struct sp_list_checker_info spl_check_info;
    if ((sockfd = udp_socket()) < 0) {
        perror("socket error");
        return 1;
    }
    set_addr_any(&addr, 5193);
    if (inet_bind(sockfd, &addr) < 0) {
        perror("bind error");
        return 1;
     }
 addr_to_send = 2;
 spl_check_info.sp_list = &sp_list_head;
 if (sp_list_checker_run(&spl_check_info) < 0) {
  fprintf(stderr, "Can't start superpeer list checker\n");
  return 1;
 }
 len = sizeof(struct sockaddr_in);
 while (1) {
  if ((n = recvfrom_packet(sockfd, &addr, &recv_pck, &len)) < 0) {
   perror("errore in recvfrom");
   return 1;
  }
  if (!strncmp(recv_pck.cmd, "PIN", 3)) {
   if (update_sp_flag(&spl_check_info, &addr, *recv_pck.data) < 0) {
    fprintf(stderr, "Ping error\n");
   }
  } else if (!strncmp(recv_pck.cmd, "JOI", 3)) {
   if (sp_join(sockfd, &addr, &recv_pck, &spl_check_info) < 0) {
    fprintf(stderr, "Join error\n");
   }
  } else if(!strncmp(recv_pck.cmd, "LEA", 3)) {
   if (sp_leave(sockfd, &addr, &recv_pck, &spl_check_info) < 0) {
    fprintf(stderr, "Leave error\n");
   }
  } else if (!strncmp(recv_pck.cmd, "REG", 3)) {
   if (sp_register(sockfd, &addr, &recv_pck, &spl_check_info) < 0) {
    fprintf(stderr, "Register error\n");
   }
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: len int

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: n int

variable_types: struct sp_list_checker_info { struct th_info { unsigned long thread; 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; int go; } thinfo; struct node { void *data; struct node *next; struct node *prev; } **sp_list; }


decompiled_code:
void __fastcall __noreturn peer_list_checker_func(__int64 a1)
{
__int16 v1; // r14
__int64 v3; // rsi
int v4; // eax
__int64 *v5; // rbx
__int64 *v6; // r15
__int64 v7; // rax
int v8; // eax
__int64 v9; // rbx
__int64 v10; // rdi
__int16 v11; // ax
char *v12; // rdx
int v13; // edi
char *v14; // rdx
struct timespec v15; // [rsp+0h] [rbp-48h] BYREF

v1 = 5;
puts("AVVIO PROCESSO DI CONTROLLO LISTA P");
v15.tv_sec = 6LL;
v15.tv_nsec = 0LL;
while ( 1 )
{
v3 = 0LL;
nanosleep(&v15, 0LL);
v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v13 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( !v13 )
pthread_exit(0LL);
LABEL_25:
v14 = strerror(v13);
fprintf(stderr, "peer_list_checker_func error - can't release lock: %s\n", v14);
LABEL_23:
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v5 = **(__int64 ***)(a1 + 56);
while ( v5 )
{
while ( 1 )
{
v6 = v5;
v5 = (__int64 *)v5[1];
v7 = *v6;
if ( !*(_BYTE *)(*v6 + 26) )
break;
*(_BYTE *)(v7 + 26) = 0;
if ( !v5 )
goto LABEL_8;
}
v3 = *(unsigned __int16 *)(v7 + 24);
remove_all_file(*(unsigned int *)(v7 + 4), v3);
remove_peer_node(v6);
puts("RIMOSSO PEER NON ATTIVO");
}
LABEL_8:
v8 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v8 )
goto LABEL_24;
v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 64));
if ( v4 )
break;
v9 = request_list_head;
while ( v9 )
{
while ( 1 )
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 8);
v11 = *(_WORD *)(*(_QWORD *)v10 + 18LL);
if ( !v11 )
break;
*(_WORD *)(*(_QWORD *)v10 + 18LL) = v11 - 1;
if ( !v9 )
goto LABEL_14;
}
remove_request_node(v10, v3);
}
LABEL_14:
v8 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 64));
if ( v8 )
{
LABEL_24:
v13 = v8;
goto LABEL_25;
}
if ( state == 3 )
{
if ( v1 )
{
--v1;
}
else
{
state = 6;
v1 = 5;
}
}
else
{
v1 = 5;
}
}
v12 = strerror(v4);
fprintf(stderr, "peer_list_checker_func error - can't acquire lock: %s\n", v12);
goto LABEL_23;
}

canonical_decompiled_code:
void __fastcall __noreturn func0(__int64 a1)
{
__int16 v1; // r14
__int64 v3; // rsi
int v4; // eax
__int64 *v5; // rbx
__int64 *v6; // r15
__int64 v7; // rax
int v8; // eax
__int64 v9; // rbx
__int64 v10; // rdi
__int16 v11; // ax
char *v12; // rdx
int v13; // edi
char *v14; // rdx
struct timespec v15; // [rsp+0h] [rbp-48h] BYREF

v1 = 5;
puts("AVVIO PROCESSO DI CONTROLLO LISTA P");
v15.tv_sec = 6LL;
v15.tv_nsec = 0LL;
while ( 1 )
{
v3 = 0LL;
nanosleep(&v15, 0LL);
v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v13 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( !v13 )
pthread_exit(0LL);
LABEL_25:
v14 = strerror(v13);
fprintf(stderr, "peer_list_checker_func error - can't release lock: %s\n", v14);
LABEL_23:
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v5 = **(__int64 ***)(a1 + 56);
while ( v5 )
{
while ( 1 )
{
v6 = v5;
v5 = (__int64 *)v5[1];
v7 = *v6;
if ( !*(_BYTE *)(*v6 + 26) )
break;
*(_BYTE *)(v7 + 26) = 0;
if ( !v5 )
goto LABEL_8;
}
v3 = *(unsigned __int16 *)(v7 + 24);
remove_all_file(*(unsigned int *)(v7 + 4), v3);
remove_peer_node(v6);
puts("RIMOSSO PEER NON ATTIVO");
}
LABEL_8:
v8 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v8 )
goto LABEL_24;
v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 64));
if ( v4 )
break;
v9 = request_list_head;
while ( v9 )
{
while ( 1 )
{
v10 = v9;
v9 = *(_QWORD *)(v9 + 8);
v11 = *(_WORD *)(*(_QWORD *)v10 + 18LL);
if ( !v11 )
break;
*(_WORD *)(*(_QWORD *)v10 + 18LL) = v11 - 1;
if ( !v9 )
goto LABEL_14;
}
remove_request_node(v10, v3);
}
LABEL_14:
v8 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 64));
if ( v8 )
{
LABEL_24:
v13 = v8;
goto LABEL_25;
}
if ( state == 3 )
{
if ( v1 )
{
--v1;
}
else
{
state = 6;
v1 = 5;
}
}
else
{
v1 = 5;
}
}
v12 = strerror(v4);
fprintf(stderr, "peer_list_checker_func error - can't acquire lock: %s\n", v12);
goto LABEL_23;
}

original_code:
void peer_list_checker_func(void *args) {
 struct peer_list_ch_info *plchinfo = (struct peer_list_ch_info *)args;
 struct timespec sleep_time;
 struct node *tmp_node;
 struct node *to_remove;
 int rc;
 short promote_ttl = 5;
 printf("AVVIO PROCESSO DI CONTROLLO LISTA P\n");
 sleep_time.tv_sec = 6;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&plchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "peer_list_checker_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if (plchinfo->thinfo.go == 0) {
   if ((rc = pthread_mutex_unlock(&plchinfo->thinfo.mutex)) != 0) {
    fprintf(stderr, "peer_list_checker_func error - can't release lock: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   pthread_exit((void *)0);
  }
  tmp_node = *plchinfo->peer_list;
  while (tmp_node != ((void *)0)) {
   to_remove = tmp_node;
   tmp_node = tmp_node->next;
   if (((struct peer_node *)to_remove->data)->flag == 0) {
    remove_all_file(((struct peer_node *)to_remove->data)->peer_addr.sin_addr.s_addr,
      ((struct peer_node *)to_remove->data)->dw_port);
    remove_peer_node(to_remove);
    printf("RIMOSSO PEER NON ATTIVO\n");
   } else {
    ((struct peer_node *)to_remove->data)->flag = 0;
   }
  }
  if ((rc = pthread_mutex_unlock(&plchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "peer_list_checker_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if ((rc = pthread_mutex_lock(&plchinfo->request_mutex)) != 0) {
   fprintf(stderr, "peer_list_checker_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  tmp_node = request_list_head;
  while (tmp_node != ((void *)0)) {
   to_remove = tmp_node;
   tmp_node = tmp_node->next;
   if (((struct request_node *)to_remove->data)->ttl == 0) {
    remove_request_node(to_remove);
   } else {
    ((struct request_node *)to_remove->data)->ttl --;
   }
  }
  if ((rc = pthread_mutex_unlock(&plchinfo->request_mutex)) != 0) {
   fprintf(stderr, "peer_list_checker_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if (state == 3) {
   if (promote_ttl == 0) {
    state = 6;
    promote_ttl = 5;
   } else {
    promote_ttl --;
   }
  } else {
   promote_ttl = 5;
  }
 }
}

variable_types:
args
void *

variable_types: struct peer_list_ch_info { struct th_info { unsigned long thread; 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; int go; } thinfo; struct node { void *data; struct node *next; struct node *prev; } **peer_list; union pthread_mutex_t request_mutex; } *

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

variable_types: rc int

return_type: void

user_defined_types: struct peer_list_ch_info { struct th_info thinfo; struct node **peer_list; union pthread_mutex_t request_mutex; }

user_defined_types: struct peer_node { struct sockaddr_in peer_addr; unsigned long peer_rate; unsigned short dw_port; char flag; }

user_defined_types: struct request_node { unsigned short id; unsigned long ip_sender; unsigned short port; unsigned short ttl; }


decompiled_code:
__int64 __fastcall peer_list_checker_run(__int64 a1)
{
int v1; // eax
char *v3; // rax

v1 = pthread_mutex_init((pthread_mutex_t *)(a1 + 64), 0LL);
if ( !v1 )
return thread_run(a1, peer_list_checker_func, a1);
v3 = strerror(v1);
fprintf(stderr, "peer_list_checker_run error - can't initialize lock: %s\n", v3);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v1; // eax
char *v3; // rax

v1 = pthread_mutex_init((pthread_mutex_t *)(a1 + 64), 0LL);
if ( !v1 )
return thread_run(a1, peer_list_checker_func, a1);
v3 = strerror(v1);
fprintf(stderr, "peer_list_checker_run error - can't initialize lock: %s\n", v3);
return 0xFFFFFFFFLL;
}

original_code:
int peer_list_checker_run(struct peer_list_ch_info *plchinfo) {
 int rc;
 if ((rc = pthread_mutex_init(&plchinfo->request_mutex, ((void *)0))) != 0) {
  fprintf(stderr, "peer_list_checker_run error - can't initialize lock: %s\n", strerror(rc));
  return -1;
 }
 return thread_run(&plchinfo->thinfo, (void *)&peer_list_checker_func, (void *)plchinfo);
}


decompiled_code:
// attributes: thunk
__int64 peer_list_checker_stop()
{
return thread_stop();
}

canonical_decompiled_code:
__int64 func2()
{
return thread_stop();
}

original_code:
int peer_list_checker_stop(struct peer_list_ch_info *plchinfo) {
 return thread_stop(&plchinfo->thinfo);
}


decompiled_code:
int __fastcall update_peer_flag(__int64 a1, __int64 a2, __int64 a3)
{
int v4; // eax
_QWORD *v5; // rbx
__int64 v6; // rbx
int result; // eax
char *v8; // rax
char *v9; // rax

v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", v8);
result = -1;
}
else
{
v5 = **(_QWORD ***)(a1 + 56);
if ( v5 )
{
while ( !(unsigned int)addrcmp(*v5, a2) )
{
v5 = (_QWORD *)v5[1];
if ( !v5 )
goto LABEL_7;
}
v6 = *v5;
*(_BYTE *)(v6 + 26) = 1;
*(_QWORD *)(v6 + 16) = btol(a3 + 10);
}
LABEL_7:
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( result )
{
v9 = strerror(result);
fprintf(stderr, "update_peer_flag error - can't release lock: %s\n", v9);
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
int v4; // eax
_QWORD *v5; // rbx
__int64 v6; // rbx
int result; // eax
char *v8; // rax
char *v9; // rax

v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", v8);
result = -1;
}
else
{
v5 = **(_QWORD ***)(a1 + 56);
if ( v5 )
{
while ( !(unsigned int)addrcmp(*v5, a2) )
{
v5 = (_QWORD *)v5[1];
if ( !v5 )
goto LABEL_7;
}
v6 = *v5;
*(_BYTE *)(v6 + 26) = 1;
*(_QWORD *)(v6 + 16) = btol(a3 + 10);
}
LABEL_7:
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( result )
{
v9 = strerror(result);
fprintf(stderr, "update_peer_flag error - can't release lock: %s\n", v9);
result = -1;
}
}
return result;
}

original_code:
int update_peer_flag(struct peer_list_ch_info *plchinfo, const struct sockaddr_in *peer_addr, const struct packet *recv_pck) {
 struct node *tmp_node;
 int rc;
 if ((rc = pthread_mutex_lock(&plchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 tmp_node = *plchinfo->peer_list;
 while (tmp_node != ((void *)0)) {
  if (addrcmp(&((struct peer_node *)tmp_node->data)->peer_addr, peer_addr)) {
   ((struct peer_node *)tmp_node->data)->flag = 1;
   ((struct peer_node *)tmp_node->data)->peer_rate = btol(recv_pck->data);
   break;
  }
  tmp_node = tmp_node->next;
 }
 if ((rc = pthread_mutex_unlock(&plchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_peer_flag error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int update_peer_flag(struct peer_list_ch_info *plchinfo, struct sockaddr_in *peer_addr, struct packet *recv_pck) {
 struct node *tmp_node;
 int rc;
 if ((rc = pthread_mutex_lock(&plchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 tmp_node = *plchinfo->peer_list;
 while (tmp_node != ((void *)0)) {
  if (addrcmp(&((struct peer_node *)tmp_node->data)->peer_addr, peer_addr)) {
   ((struct peer_node *)tmp_node->data)->flag = 1;
   ((struct peer_node *)tmp_node->data)->peer_rate = btol(recv_pck->data);
   break;
  }
  tmp_node = tmp_node->next;
 }
 if ((rc = pthread_mutex_unlock(&plchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_peer_flag error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

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: struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; } *


decompiled_code:
void __fastcall __noreturn sp_list_checker_func(__int64 a1)
{
pthread_mutex_t *v2; // r14
int v3; // eax
unsigned int v4; // er13
__int64 *v5; // rbx
__int64 v6; // rbp
unsigned int v7; // er12
char *v8; // rax
int v9; // eax
char *v10; // rdx
struct in_addr v11; // edi
char *v12; // rax
char *v13; // rdx
struct timespec v14; // [rsp+0h] [rbp-48h] BYREF

v2 = (pthread_mutex_t *)(a1 + 8);
v14.tv_sec = 6LL;
v14.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v14, 0LL);
v3 = pthread_mutex_lock(v2);
v4 = v3;
if ( v3 )
break;
v5 = **(__int64 ***)(a1 + 56);
while ( v5 )
{
while ( 1 )
{
v6 = *v5;
v5 = (__int64 *)v5[1];
v7 = (unsigned __int16)_bswap_16(*(_WORD *)(v6 + 2));
if ( *(_WORD *)(v6 + 18) )
break;
v8 = inet_ntoa(*(struct in_addr *)(v6 + 4));
printf("cancello nodo: %s:%d\n", v8, v7);
remove_sp(v6);
if ( !v5 )
goto LABEL_7;
}
v11.s_addr = *(_DWORD *)(v6 + 4);
++v4;
*(_WORD *)(v6 + 18) = 0;
v12 = inet_ntoa(v11);
printf("Imposto flag: 0 per %s:%d\n", v12, v7);
}
LABEL_7:
printf("trovati %d super peer\n", v4);
v9 = pthread_mutex_unlock(v2);
if ( v9 )
{
v10 = strerror(v9);
fprintf(stderr, "sp_list_checker_func error - can't release lock: %s\n", v10);
goto LABEL_11;
}
}
v13 = strerror(v3);
fprintf(stderr, "sp_list_checker_func error - can't acquire lock: %s\n", v13);
LABEL_11:
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1)
{
pthread_mutex_t *v2; // r14
int v3; // eax
unsigned int v4; // er13
__int64 *v5; // rbx
__int64 v6; // rbp
unsigned int v7; // er12
char *v8; // rax
int v9; // eax
char *v10; // rdx
struct in_addr v11; // edi
char *v12; // rax
char *v13; // rdx
struct timespec v14; // [rsp+0h] [rbp-48h] BYREF

v2 = (pthread_mutex_t *)(a1 + 8);
v14.tv_sec = 6LL;
v14.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v14, 0LL);
v3 = pthread_mutex_lock(v2);
v4 = v3;
if ( v3 )
break;
v5 = **(__int64 ***)(a1 + 56);
while ( v5 )
{
while ( 1 )
{
v6 = *v5;
v5 = (__int64 *)v5[1];
v7 = (unsigned __int16)func0(*(_WORD *)(v6 + 2));
if ( *(_WORD *)(v6 + 18) )
break;
v8 = inet_ntoa(*(struct in_addr *)(v6 + 4));
printf("cancello nodo: %s:%d\n", v8, v7);
remove_sp(v6);
if ( !v5 )
goto LABEL_7;
}
v11.s_addr = *(_DWORD *)(v6 + 4);
++v4;
*(_WORD *)(v6 + 18) = 0;
v12 = inet_ntoa(v11);
printf("Imposto flag: 0 per %s:%d\n", v12, v7);
}
LABEL_7:
printf("trovati %d super peer\n", v4);
v9 = pthread_mutex_unlock(v2);
if ( v9 )
{
v10 = strerror(v9);
fprintf(stderr, "sp_list_checker_func error - can't release lock: %s\n", v10);
goto LABEL_11;
}
}
v13 = strerror(v3);
fprintf(stderr, "sp_list_checker_func error - can't acquire lock: %s\n", v13);
LABEL_11:
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

original_code:
void sp_list_checker_func(void *args) {
 struct sp_list_checker_info *splchinfo = (struct sp_list_checker_info *)args;
 struct node *it;
 struct spaddr_node *spaddr_tmp;
 struct timespec sleep_time;
 int rc, dim = 0;
 sleep_time.tv_sec = 6;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_list_checker_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  it = *splchinfo->sp_list;
  dim = 0;
  while (it != ((void *)0)) {
   spaddr_tmp = (struct spaddr_node *)it->data;
   it = it->next;
   if (spaddr_tmp->flag == 0) {
    printf("cancello nodo: %s:%d\n", inet_ntoa(spaddr_tmp->sp_addr.sin_addr), ntohs(spaddr_tmp->sp_addr.sin_port));
    remove_sp(&spaddr_tmp->sp_addr);
   } else {
    dim ++;
    spaddr_tmp->flag = 0;
    printf("Imposto flag: 0 per %s:%d\n", inet_ntoa(spaddr_tmp->sp_addr.sin_addr), ntohs(spaddr_tmp->sp_addr.sin_port));
   }
  }
  printf("trovati %d super peer\n", dim);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_list_checker_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
 }
}

canonical_original_code:
void sp_list_checker_func(void *args) {
 struct sp_list_checker_info *splchinfo = (struct sp_list_checker_info *)args;
 struct node *it;
 struct spaddr_node *spaddr_tmp;
 struct timespec sleep_time;
 int rc; int dim = 0;
 sleep_time.tv_sec = 6;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_list_checker_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  it = *splchinfo->sp_list;
  dim = 0;
  while (it != ((void *)0)) {
   spaddr_tmp = (struct spaddr_node *)it->data;
   it = it->next;
   if (spaddr_tmp->flag == 0) {
    printf("cancello nodo: %s:%d\n", inet_ntoa(spaddr_tmp->sp_addr.sin_addr), ntohs(spaddr_tmp->sp_addr.sin_port));
    remove_sp(&spaddr_tmp->sp_addr);
   } else {
    dim ++;
    spaddr_tmp->flag = 0;
    printf("Imposto flag: 0 per %s:%d\n", inet_ntoa(spaddr_tmp->sp_addr.sin_addr), ntohs(spaddr_tmp->sp_addr.sin_port));
   }
  }
  printf("trovati %d super peer\n", dim);
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_list_checker_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
 }
}

variable_types: struct sp_list_checker_info { struct th_info { unsigned long thread; 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; int go; } thinfo; struct node { void *data; struct node *next; struct node *prev; } **sp_list; } *

variable_types: struct spaddr_node { 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]; } sp_addr; unsigned char free_spots; short flag; } *

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


decompiled_code:
__int64 __fastcall sp_list_checker_run(__int64 a1)
{
return thread_run(a1, sp_list_checker_func, a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return thread_run(a1, sp_list_checker_func, a1);
}

original_code:
int sp_list_checker_run(struct sp_list_checker_info *splchinfo) {
 return thread_run(&splchinfo->thinfo, (void *)&sp_list_checker_func, (void *)splchinfo);
}


decompiled_code:
// attributes: thunk
__int64 sp_list_checker_stop()
{
return thread_stop();
}

canonical_decompiled_code:
__int64 func3()
{
return thread_stop();
}

original_code:
int sp_list_checker_stop(struct sp_list_checker_info *splchinfo) {
 return thread_stop(&splchinfo->thinfo);
}


decompiled_code:
int __fastcall update_sp_flag(__int64 a1, __int64 a2, char a3)
{
pthread_mutex_t *v3; // rbp
int v5; // eax
__int64 *v6; // rax
__int64 v7; // rax
int result; // eax
char *v9; // rax
int v10; // edi
char *v11; // rax

v3 = (pthread_mutex_t *)(a1 + 8);
v5 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v5 )
{
v9 = strerror(v5);
fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", v9);
return -1;
}
v6 = (__int64 *)get_node_sp(a2);
if ( !v6 )
{
puts("Indirizzo non trovato");
v10 = pthread_mutex_unlock(v3);
if ( !v10 )
return -1;
goto LABEL_9;
}
v7 = *v6;
if ( !v7 )
{
puts(s);
result = pthread_mutex_unlock(v3);
if ( !result )
return -1;
goto LABEL_8;
}
*(_BYTE *)(v7 + 16) = a3;
*(_WORD *)(v7 + 18) = 1;
result = pthread_mutex_unlock(v3);
if ( result )
{
LABEL_8:
v10 = result;
LABEL_9:
v11 = strerror(v10);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v11);
return -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2, char a3)
{
pthread_mutex_t *v3; // rbp
int v5; // eax
__int64 *v6; // rax
__int64 v7; // rax
int result; // eax
char *v9; // rax
int v10; // edi
char *v11; // rax

v3 = (pthread_mutex_t *)(a1 + 8);
v5 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v5 )
{
v9 = strerror(v5);
fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", v9);
return -1;
}
v6 = (__int64 *)get_node_sp(a2);
if ( !v6 )
{
puts("Indirizzo non trovato");
v10 = pthread_mutex_unlock(v3);
if ( !v10 )
return -1;
goto LABEL_9;
}
v7 = *v6;
if ( !v7 )
{
puts(s);
result = pthread_mutex_unlock(v3);
if ( !result )
return -1;
goto LABEL_8;
}
*(_BYTE *)(v7 + 16) = a3;
*(_WORD *)(v7 + 18) = 1;
result = pthread_mutex_unlock(v3);
if ( result )
{
LABEL_8:
v10 = result;
LABEL_9:
v11 = strerror(v10);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v11);
return -1;
}
return result;
}

original_code:
int update_sp_flag(struct sp_list_checker_info *splchinfo, const struct sockaddr_in *sp_addr, char free_spots) {
 struct spaddr_node *sp_node;
 struct node *tmp_node;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 tmp_node = get_node_sp(sp_addr);
 if (tmp_node == ((void *)0)) {
  printf("Indirizzo non trovato\n");
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  }
  return -1;
 }
 sp_node = (struct spaddr_node *)tmp_node->data;
 if (sp_node == ((void *)0)) {
  printf("Errore in set_sp_flag: data è NULL\n");
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  }
  return -1;
 }
 sp_node->flag = 1;
 sp_node->free_spots = free_spots;
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int update_sp_flag(struct sp_list_checker_info *splchinfo, struct sockaddr_in *sp_addr, char free_spots) {
 struct spaddr_node *sp_node;
 struct node *tmp_node;
 int rc;
 if ((rc = pthread_mutex_lock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 tmp_node = get_node_sp(sp_addr);
 if (tmp_node == ((void *)0)) {
  printf("Indirizzo non trovato\n");
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  }
  return -1;
 }
 sp_node = (struct spaddr_node *)tmp_node->data;
 if (sp_node == ((void *)0)) {
  printf("Errore in set_sp_flag: data è NULL\n");
  if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  }
  return -1;
 }
 sp_node->flag = 1;
 sp_node->free_spots = free_spots;
 if ((rc = pthread_mutex_unlock(&splchinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall read_config(unsigned __int16 *a1)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // eax
__int64 v5; // rcx
__int64 v6; // rsi
const char *v7; // rdx
__int64 v8; // rdi
const char *v9; // r8
__int64 result; // rax
char *v11; // [rsp+8h] [rbp-240h]
__int64 v12; // [rsp+10h] [rbp-238h] BYREF
char src[4]; // [rsp+18h] [rbp-230h] BYREF
char v14; // [rsp+1Ch] [rbp-22Ch]
char v15[243]; // [rsp+1Dh] [rbp-22Bh] BYREF
char dest[312]; // [rsp+110h] [rbp-138h] BYREF

v2 = open("../karma.conf", 0);
if ( v2 < 0 )
{
perror("read_config error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
v11 = (char *)(a1 + 4);
while ( 1 )
{
v4 = readline(v3, &v12, 255LL);
if ( v4 <= 0 )
break;
*((_BYTE *)&v12 + v4 - 1) = 0;
if ( (_BYTE)v12 != 35 )
{
if ( v12 == 0x74726F705F706475LL && src[0] == 61 )
{
strcpy(dest, &src[1]);
*a1 = atoi(dest);
}
else if ( v12 == 0x74726F705F706374LL && src[0] == 61 )
{
strcpy(dest, &src[1]);
a1[1] = atoi(dest);
}
else if ( v12 == 0x6F665F6572616873LL && *(_DWORD *)src == 1919247468 && v14 == 61 )
{
strcpy(v11, v15);
}
else if ( v12 == 0x6E776F645F78616DLL && *(_DWORD *)src == 1684107116 && v14 == 61 )
{
strcpy(dest, v15);
a1[2] = atoi(dest);
}
else if ( v12 == 0x6F6C70755F78616DLL && *(_WORD *)src == 25697 && src[2] == 61 )
{
strcpy(dest, &src[3]);
a1[3] = atoi(dest);
}
}
}
v5 = a1[1];
v6 = *a1;
v7 = "(porta scelta dal kernel)";
v8 = a1[2];
v9 = (const char *)&unk_3A6;
if ( !(_WORD)v5 )
v9 = "(porta scelta dal kernel)";
if ( (_WORD)v6 )
v7 = (const char *)&unk_3A6;
printf(
"UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n",
v6,
v7,
v5,
v9,
v11,
v8,
a1[3]);
if ( close(v3) < 0 )
{
perror("read_config error - close failed");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
int v2; // eax
unsigned int v3; // ebp
int v4; // eax
__int64 v5; // rcx
__int64 v6; // rsi
const char *v7; // rdx
__int64 v8; // rdi
const char *v9; // r8
__int64 result; // rax
char *v11; // [rsp+8h] [rbp-240h]
__int64 v12; // [rsp+10h] [rbp-238h] BYREF
char src[4]; // [rsp+18h] [rbp-230h] BYREF
char v14; // [rsp+1Ch] [rbp-22Ch]
char v15[243]; // [rsp+1Dh] [rbp-22Bh] BYREF
char dest[312]; // [rsp+110h] [rbp-138h] BYREF

v2 = open("../karma.conf", 0);
if ( v2 < 0 )
{
perror("read_config error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
v11 = (char *)(a1 + 4);
while ( 1 )
{
v4 = readline(v3, &v12, 255LL);
if ( v4 <= 0 )
break;
*((_BYTE *)&v12 + v4 - 1) = 0;
if ( (_BYTE)v12 != 35 )
{
if ( v12 == 0x74726F705F706475LL && src[0] == 61 )
{
strcpy(dest, &src[1]);
*a1 = atoi(dest);
}
else if ( v12 == 0x74726F705F706374LL && src[0] == 61 )
{
strcpy(dest, &src[1]);
a1[1] = atoi(dest);
}
else if ( v12 == 0x6F665F6572616873LL && *(_DWORD *)src == 1919247468 && v14 == 61 )
{
strcpy(v11, v15);
}
else if ( v12 == 0x6E776F645F78616DLL && *(_DWORD *)src == 1684107116 && v14 == 61 )
{
strcpy(dest, v15);
a1[2] = atoi(dest);
}
else if ( v12 == 0x6F6C70755F78616DLL && *(_WORD *)src == 25697 && src[2] == 61 )
{
strcpy(dest, &src[3]);
a1[3] = atoi(dest);
}
}
}
v5 = a1[1];
v6 = *a1;
v7 = "(porta scelta dal kernel)";
v8 = a1[2];
v9 = (const char *)&unk_3A6;
if ( !(_WORD)v5 )
v9 = "(porta scelta dal kernel)";
if ( (_WORD)v6 )
v7 = (const char *)&unk_3A6;
printf(
"UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n",
v6,
v7,
v5,
v9,
v11,
v8,
a1[3]);
if ( close(v3) < 0 )
{
perror("read_config error - close failed");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int read_config(struct config *conf) {
 int fd;
 char str[255], str2[255];
 int n;
 if ((fd = open("../karma.conf", 00)) < 0) {
  perror("read_config error - open failed");
  return -1;
 }
 while ((n = readline(fd, str, 255)) > 0) {
  str[n - 1] = 0;
  if (*str == '#') {
   continue;
  }
  if (!strncmp(str, "udp_port=", strlen("udp_port="))) {
   strcpy(str2, str + strlen("udp_port="));
   conf->udp_port = atoi(str2);
  } else if (!strncmp(str, "tcp_port=", strlen("tcp_port="))) {
   strcpy(str2, str + strlen("tcp_port="));
   conf->tcp_port = atoi(str2);
  } else if (!strncmp(str, "share_folder=", strlen("share_folder="))) {
   strcpy(conf->share_folder, str + strlen("share_folder="));
  } else if (!strncmp(str, "max_download=", strlen("max_download="))) {
   strcpy(str2, str + strlen("max_download="));
   conf->max_download = atoi(str2);
  } else if (!strncmp(str, "max_upload=", strlen("max_upload="))) {
   strcpy(str2, str + strlen("max_upload="));
   conf->max_upload = atoi(str2);
  }
 }
 printf("UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n", conf->udp_port, (conf->udp_port == 0 ? "(porta scelta dal kernel)" : ""), conf->tcp_port, (conf->tcp_port == 0 ? "(porta scelta dal kernel)" : ""), conf->share_folder, conf->max_download, conf->max_upload);
 if (close(fd) < 0) {
  perror("read_config error - close failed");
  return -1;
 }
 return 0;
}

canonical_original_code:
int read_config(struct config *conf) {
 int fd;
 char str[255]; char str2[255];
 int n;
 if ((fd = open("../karma.conf", 00)) < 0) {
  perror("read_config error - open failed");
  return -1;
 }
 while ((n = readline(fd, str, 255)) > 0) {
  str[n - 1] = 0;
  if (*str == '#') {
   continue;
  }
  if (!strncmp(str, "udp_port=", strlen("udp_port="))) {
   strcpy(str2, str + strlen("udp_port="));
   conf->udp_port = atoi(str2);
  } else if (!strncmp(str, "tcp_port=", strlen("tcp_port="))) {
   strcpy(str2, str + strlen("tcp_port="));
   conf->tcp_port = atoi(str2);
  } else if (!strncmp(str, "share_folder=", strlen("share_folder="))) {
   strcpy(conf->share_folder, str + strlen("share_folder="));
  } else if (!strncmp(str, "max_download=", strlen("max_download="))) {
   strcpy(str2, str + strlen("max_download="));
   conf->max_download = atoi(str2);
  } else if (!strncmp(str, "max_upload=", strlen("max_upload="))) {
   strcpy(str2, str + strlen("max_upload="));
   conf->max_upload = atoi(str2);
  }
 }
 printf("UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n", conf->udp_port, (conf->udp_port == 0 ? "(porta scelta dal kernel)" : ""), conf->tcp_port, (conf->tcp_port == 0 ? "(porta scelta dal kernel)" : ""), conf->share_folder, conf->max_download, conf->max_upload);
 if (close(fd) < 0) {
  perror("read_config error - close failed");
  return -1;
 }
 return 0;
}

variable_types: struct config { unsigned short udp_port; unsigned short tcp_port; unsigned short max_download; unsigned short max_upload; char share_folder[256]; } *

variable_types: fd int

variable_types:
str
char [255]


decompiled_code:
int __fastcall print_file_info(__int64 a1)
{
printf("filename: %s\n", (const char *)a1);
printf("md5: ");
print_as_hex(a1 + 255, 16LL);
printf("file size: %d\n", *(unsigned int *)(a1 + 272));
return printf("chunk number: %d\n", *(unsigned int *)(a1 + 276));
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
printf("filename: %s\n", (const char *)a1);
printf("md5: ");
print_as_hex(a1 + 255, 16LL);
printf("file size: %d\n", *(unsigned int *)(a1 + 272));
return printf("chunk number: %d\n", *(unsigned int *)(a1 + 276));
}

original_code:
void print_file_info(const struct transfer_info *file_info) {
 printf("filename: %s\n", file_info->filename);
 printf("md5: ");
 print_as_hex(file_info->md5, 16);
 printf("file size: %d\n", file_info->file_size);
 printf("chunk number: %d\n", file_info->chunk_number);
}

canonical_original_code:
void print_file_info(struct transfer_info *file_info) {
 printf("filename: %s\n", file_info->filename);
 printf("md5: ");
 print_as_hex(file_info->md5, 16);
 printf("file size: %d\n", file_info->file_size);
 printf("chunk number: %d\n", file_info->chunk_number);
}

variable_types: struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; } *


decompiled_code:
char *__fastcall new_download_node(__int64 a1, const __m128i *a2)
{
char *v2; // r12
char *result; // rax

v2 = (char *)malloc(0x12CuLL);
addrcpy(v2 + 4, a1);
result = v2;
*(__m128i *)(v2 + 275) = _mm_loadu_si128(a2);
return result;
}

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

v2 = (char *)malloc(0x12CuLL);
addrcpy(v2 + 4, a1);
result = v2;
*(__m128i *)(v2 + 275) = _mm_loadu_si128(a2);
return result;
}

original_code:
struct transfer_node *new_download_node(const struct sockaddr_in *addr, const unsigned char *md5) {
 struct transfer_node *new_node;
 new_node = (struct transfer_node *)malloc(sizeof(struct transfer_node));
 addrcpy(&new_node->addrto, addr);
 memcpy(new_node->file_info.md5, md5, 16);
 return new_node;
}

canonical_original_code:
struct transfer_node *new_download_node(struct sockaddr_in *addr, unsigned char *md5) {
 struct transfer_node *new_node;
 new_node = (struct transfer_node *)malloc(sizeof(struct transfer_node));
 addrcpy(&new_node->addrto, addr);
 memcpy(new_node->file_info.md5, md5, 16);
 return new_node;
}

variable_types:
addr
struct sockaddr_in *

variable_types: struct transfer_node { int socksd; 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]; } addrto; struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; } file_info; } *

user_defined_types: struct transfer_node { int socksd; struct sockaddr_in addrto; struct transfer_info file_info; }


decompiled_code:
__int64 __fastcall insert_download(__int64 a1, const __m128i *a2)
{
char *v2; // rax
__int64 result; // rax

if ( !download_list_head )
download_count = 0;
v2 = new_download_node(a1, a2);
result = insert_node(download_list_head, v2);
++download_count;
download_list_head = result;
return result;
}

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

if ( !download_list_head )
download_count = 0;
v2 = func1(a1, a2);
result = insert_node(download_list_head, v2);
++download_count;
download_list_head = result;
return result;
}

original_code:
void insert_download(const struct sockaddr_in *addr, const unsigned char *md5) {
 struct transfer_node *new_node;
 if (download_list_head == ((void *)0)) {
  download_count = 0;
 }
 new_node = new_download_node(addr, md5);
 download_list_head = insert_node(download_list_head, new_node);
 download_count ++;
}

canonical_original_code:
void insert_download(struct sockaddr_in *addr, unsigned char *md5) {
 struct transfer_node *new_node;
 if (download_list_head == ((void *)0)) {
  download_count = 0;
 }
 new_node = new_download_node(addr, md5);
 download_list_head = insert_node(download_list_head, new_node);
 download_count ++;
}

user_defined_types: struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; }


decompiled_code:
__int64 get_download()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax

result = download_list_head;
while ( download_list_head )
{
if ( !*(_QWORD *)(download_list_head + 8) )
{
v1 = *(_QWORD *)download_list_head;
v2 = remove_node(download_list_head, download_list_head);
--download_count;
download_list_head = v2;
return v1;
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
__int64 v1; // rbx
__int64 v2; // rax

result = download_list_head;
while ( download_list_head )
{
if ( !*(_QWORD *)(download_list_head + 8) )
{
v1 = *(_QWORD *)download_list_head;
v2 = remove_node(download_list_head, download_list_head);
--download_count;
download_list_head = v2;
return v1;
}
}
return result;
}

original_code:
struct transfer_node *get_download() {
 struct transfer_node *ret = ((void *)0);
 struct node *it;
 it = download_list_head;
 while (it != ((void *)0)) {
  if (it->next == ((void *)0)) {
   break;
  }
 }
 if (it == ((void *)0)) {
  return ((void *)0);
 }
 ret = (struct transfer_node *)it->data;
 download_list_head = remove_node(download_list_head, it);
 download_count --;
 return ret;
}

variable_types: struct node { void *data; struct node *next; struct node *prev; } *

return_type: struct transfer_node { int socksd; 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]; } addrto; struct transfer_info { char filename[255]; unsigned char md5[16]; unsigned int file_size; unsigned int chunk_number; } file_info; } *


decompiled_code:
unsigned __int64 get_cpu_freq()
{
int v0; // ebp
int v1; // edx
int v2; // ebx
int *v3; // rax
int v4; // ecx
int *v5; // rax
int *v6; // r12
double v7; // xmm0_8
unsigned __int64 result; // rax
int v9; // er8
double v10; // [rsp+8h] [rbp-420h]
int v11; // [rsp+10h] [rbp-418h] BYREF
__int16 v12; // [rsp+14h] [rbp-414h]
char v13; // [rsp+16h] [rbp-412h]

v0 = open("/proc/cpuinfo", 0);
v10 = 0.0;
if ( v0 < 0 )
{
perror("get_cpu_freq error - open failed");
result = 0LL;
}
else
{
while ( 1 )
{
v1 = readline((unsigned int)v0, &v11, 1024LL);
if ( v1 <= 0 )
break;
if ( v11 == 544567395 && v12 == 18509 && v13 == 122 )
{
v2 = 0;
*((_BYTE *)&v11 + v1 - 1) = 0;
v3 = &v11;
while ( 1 )
{
v4 = v2++;
if ( *(_BYTE *)v3 == 58 )
break;
v3 = (int *)((char *)v3 + 1);
if ( v1 <= v2 )
goto LABEL_9;
}
v2 = v4;
LABEL_9:
v5 = _errno_location();
*v5 = 0;
v6 = v5;
v7 = strtod((const char *)&v11 + v2 + 1, 0LL);
if ( *v6 == 34 )
{
perror("get_cpu_freq error - strtod failed");
v9 = close(v0);
result = 0LL;
if ( v9 < 0 )
{
perror("get_cpu_freq error - close failed");
result = 0LL;
}
return result;
}
v10 = v7 + v10;
}
}
if ( close(v0) < 0 )
perror("get_cpu_freq error - close failed");
if ( v10 >= 9.223372036854776e18 )
result = (unsigned int)(int)(v10 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
result = (unsigned int)(int)v10;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func0()
{
int v0; // ebp
int v1; // edx
int v2; // ebx
int *v3; // rax
int v4; // ecx
int *v5; // rax
int *v6; // r12
double v7; // xmm0_8
unsigned __int64 result; // rax
int v9; // er8
double v10; // [rsp+8h] [rbp-420h]
int v11; // [rsp+10h] [rbp-418h] BYREF
__int16 v12; // [rsp+14h] [rbp-414h]
char v13; // [rsp+16h] [rbp-412h]

v0 = open("/proc/cpuinfo", 0);
v10 = 0.0;
if ( v0 < 0 )
{
perror("get_cpu_freq error - open failed");
result = 0LL;
}
else
{
while ( 1 )
{
v1 = readline((unsigned int)v0, &v11, 1024LL);
if ( v1 <= 0 )
break;
if ( v11 == 544567395 && v12 == 18509 && v13 == 122 )
{
v2 = 0;
*((_BYTE *)&v11 + v1 - 1) = 0;
v3 = &v11;
while ( 1 )
{
v4 = v2++;
if ( *(_BYTE *)v3 == 58 )
break;
v3 = (int *)((char *)v3 + 1);
if ( v1 <= v2 )
goto LABEL_9;
}
v2 = v4;
LABEL_9:
v5 = _errno_location();
*v5 = 0;
v6 = v5;
v7 = strtod((const char *)&v11 + v2 + 1, 0LL);
if ( *v6 == 34 )
{
perror("get_cpu_freq error - strtod failed");
v9 = close(v0);
result = 0LL;
if ( v9 < 0 )
{
perror("get_cpu_freq error - close failed");
result = 0LL;
}
return result;
}
v10 = v7 + v10;
}
}
if ( close(v0) < 0 )
perror("get_cpu_freq error - close failed");
if ( v10 >= 9.223372036854776e18 )
result = (unsigned int)(int)(v10 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
result = (unsigned int)(int)v10;
}
return result;
}

original_code:
unsigned long get_cpu_freq() {
 int fd;
 int n, i;
 char buf[1024];
 double tmp, freq;
 if ((fd = open("/proc/cpuinfo", 00)) < 0) {
  perror("get_cpu_freq error - open failed");
  return 0;
 }
 freq = 0;
 while ((n = readline(fd, buf, 1024)) > 0) {
  if (strncmp("cpu MHz", buf, strlen("cpu MHz"))) {
   continue;
  }
  buf[n - 1] = 0;
  for (i = 0; i < n; i ++) {
   if (buf[i] == ':') {
    break;
   }
  }
  i ++;
  (*__errno_location ()) = 0;
  tmp = strtod(buf + i, ((void *)0));
  if ((*__errno_location ()) == 34) {
   perror("get_cpu_freq error - strtod failed");
   if (close(fd) < 0) {
    perror("get_cpu_freq error - close failed");
   }
   return 0;
  }
  freq += tmp;
 }
 if (close(fd) < 0) {
  perror("get_cpu_freq error - close failed");
 }
 return (unsigned long)freq;
}

canonical_original_code:
unsigned long get_cpu_freq() {
 int fd;
 int n; int i;
 char buf[1024];
 double tmp; double freq;
 if ((fd = open("/proc/cpuinfo", 00)) < 0) {
  perror("get_cpu_freq error - open failed");
  return 0;
 }
 freq = 0;
 while ((n = readline(fd, buf, 1024)) > 0) {
  if (strncmp("cpu MHz", buf, strlen("cpu MHz"))) {
   continue;
  }
  buf[n - 1] = 0;
  for (i = 0; i < n; i ++) {
   if (buf[i] == ':') {
    break;
   }
  }
  i ++;
  (*__errno_location ()) = 0;
  tmp = strtod(buf + i, ((void *)0));
  if ((*__errno_location ()) == 34) {
   perror("get_cpu_freq error - strtod failed");
   if (close(fd) < 0) {
    perror("get_cpu_freq error - close failed");
   }
   return 0;
  }
  freq += tmp;
 }
 if (close(fd) < 0) {
  perror("get_cpu_freq error - close failed");
 }
 return (unsigned long)freq;
}

variable_types: n int

variable_types: i int

variable_types:
buf
char [1024]

variable_types: tmp double

variable_types: freq double

return_type: unsigned long


decompiled_code:
__int64 get_peer_rate()
{
int v0; // er8
__int64 result; // rax
__int64 v2; // rax
__int64 v3; // rcx
__int64 v4; // rax
unsigned __int64 v5; // rax
__int64 v6; // rbp
__kernel_ulong_t v7; // rdx
__int64 v8; // r12
struct sysinfo v9; // [rsp+0h] [rbp-88h] BYREF

v0 = sysinfo(&v9);
result = 0LL;
if ( v0 >= 0 )
{
if ( hw_rate )
{
v2 = start_time;
if ( start_time )
{
LABEL_4:
v3 = v9.uptime - v2;
v4 = 4800LL;
if ( v3 <= 288059 )
v4 = v3 / 60;
goto LABEL_6;
}
}
else
{
v5 = get_cpu_freq();
v6 = 4000LL;
v7 = v9.mem_unit * v9.totalram;
v8 = v7 >> 20;
if ( v7 > 0x1000FFFFFLL )
v8 = 4096LL;
if ( v5 <= 0xFA0 )
v6 = v5;
hw_rate = v6 + v8;
printf(
"UPTIME = %ld\nTOTALRAM(B) = %lu\nFREERAM(B) = %lu\nMEM-UNIT = %d\nCPU-FREQ = %ld\n",
v9.uptime - start_time,
v7,
v9.freeram * v9.mem_unit,
v9.mem_unit,
v6);
printf("RAM: %ld\n", v8);
printf("CPU: %ld\n", v6);
v2 = start_time;
if ( start_time )
goto LABEL_4;
}
puts("START TIME RATE");
start_time = v9.uptime;
v4 = 0LL;
LABEL_6:
result = hw_rate + v4;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // er8
__int64 result; // rax
__int64 v2; // rax
__int64 v3; // rcx
__int64 v4; // rax
unsigned __int64 v5; // rax
__int64 v6; // rbp
__kernel_ulong_t v7; // rdx
__int64 v8; // r12
struct sysinfo v9; // [rsp+0h] [rbp-88h] BYREF

v0 = sysinfo(&v9);
result = 0LL;
if ( v0 >= 0 )
{
if ( hw_rate )
{
v2 = start_time;
if ( start_time )
{
LABEL_4:
v3 = v9.uptime - v2;
v4 = 4800LL;
if ( v3 <= 288059 )
v4 = v3 / 60;
goto LABEL_6;
}
}
else
{
v5 = func0();
v6 = 4000LL;
v7 = v9.mem_unit * v9.totalram;
v8 = v7 >> 20;
if ( v7 > 0x1000FFFFFLL )
v8 = 4096LL;
if ( v5 <= 0xFA0 )
v6 = v5;
hw_rate = v6 + v8;
printf(
"UPTIME = %ld\nTOTALRAM(B) = %lu\nFREERAM(B) = %lu\nMEM-UNIT = %d\nCPU-FREQ = %ld\n",
v9.uptime - start_time,
v7,
v9.freeram * v9.mem_unit,
v9.mem_unit,
v6);
printf("RAM: %ld\n", v8);
printf("CPU: %ld\n", v6);
v2 = start_time;
if ( start_time )
goto LABEL_4;
}
puts("START TIME RATE");
start_time = v9.uptime;
v4 = 0LL;
LABEL_6:
result = hw_rate + v4;
}
return result;
}

original_code:
unsigned long get_peer_rate() {
 struct sysinfo sinfo;
 unsigned long cpu_freq;
 unsigned long ram;
 unsigned long uptime;
 if (sysinfo(&sinfo) < 0) {
  return 0;
 }
 if(hw_rate == 0){
  cpu_freq = get_cpu_freq();
  ram = (sinfo.totalram * sinfo.mem_unit / (1024 * 1024));
  if (ram > (4 * 1024)) {
   ram = (4 * 1024);
  }
  if (cpu_freq > 4000) {
   cpu_freq = 4000;
  }
  hw_rate = ram + cpu_freq;
  printf("UPTIME = %ld\nTOTALRAM(B) = %lu\nFREERAM(B) = %lu\nMEM-UNIT = %d\nCPU-FREQ = %ld\n", sinfo.uptime-start_time, sinfo.totalram * sinfo.mem_unit, sinfo.freeram * sinfo.mem_unit, sinfo.mem_unit, cpu_freq);
  printf("RAM: %ld\n", ram);
  printf("CPU: %ld\n", cpu_freq);
 }
 if(start_time == 0){
  printf("START TIME RATE\n");
  start_time = sinfo.uptime;
 }
 if ((sinfo.uptime - start_time) / 60 > 4800) {
  uptime = 4800;
 } else {
  uptime = (sinfo.uptime - start_time) / 60;
 }
 return uptime + hw_rate;
}

variable_types: struct sysinfo { long uptime; unsigned long loads[3]; unsigned long totalram; unsigned long freeram; unsigned long sharedram; unsigned long bufferram; unsigned long totalswap; unsigned long freeswap; unsigned short procs; unsigned short pad; unsigned long totalhigh; unsigned long freehigh; unsigned int mem_unit; char _f[-1]; }

variable_types:
ram
unsigned long

variable_types:
uptime
unsigned long


original_code:
struct node *remove_node(struct node *list, struct node *toremove) {
 if (toremove == ((void *)0)) {
  return list;
 }
 if (toremove->prev == ((void *)0)) {
  if (toremove->next == ((void *)0)) {
   list = ((void *)0);
  } else {
   toremove->next->prev = ((void *)0);
   list = toremove->next;
  }
 } else {
  if (toremove->next == ((void *)0)) {
   toremove->prev->next = ((void *)0);
  } else {
    toremove->prev->next = toremove->next;
    toremove->next->prev = toremove->prev;
  }
 }
 free(toremove);
 toremove = ((void *)0);
 return list;
}

variable_types:
list
struct node *

variable_types:
toremove
struct node *


decompiled_code:
__int64 __fastcall get_list_count(__int64 a1)
{
__int64 result; // rax

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

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

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

original_code:
int get_list_count(const struct node *head){
 struct node *tmp_node;
 tmp_node = (struct node *)head;
 int dim = 0;
 while( tmp_node != ((void *)0)){
  tmp_node = tmp_node->next;
  dim ++;
 }
 return dim;
}

canonical_original_code:
int get_list_count(struct node *head){
 struct node *tmp_node;
 tmp_node = (struct node *)head;
 int dim = 0;
 while( tmp_node != ((void *)0)){
  tmp_node = tmp_node->next;
  dim ++;
 }
 return dim;
}

variable_types: dim int


decompiled_code:
__int64 __fastcall sorted_insert_peer(__int64 a1, unsigned __int64 a2, __int16 a3)
{
_QWORD *v4; // rbp
__int64 v5; // rbx
__int64 result; // rax
_QWORD *v7; // rax

v4 = (_QWORD *)peer_list_head;
if ( peer_list_head )
{
while ( 1 )
{
v5 = *v4;
result = addrcmp(a1, *v4);
if ( (_DWORD)result )
break;
if ( *(_QWORD *)(v5 + 16) < a2 )
{
v4 = (_QWORD *)v4[2];
goto LABEL_8;
}
if ( !v4[1] )
goto LABEL_8;
v4 = (_QWORD *)v4[1];
}
}
else
{
curr_p_count = 0;
LABEL_8:
v7 = new_peer_node(a1, a2, a3);
result = sorted_insert_node(peer_list_head, v4, v7);
++curr_p_count;
peer_list_head = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int64 a2, __int16 a3)
{
_QWORD *v4; // rbp
__int64 v5; // rbx
__int64 result; // rax
_QWORD *v7; // rax

v4 = (_QWORD *)peer_list_head;
if ( peer_list_head )
{
while ( 1 )
{
v5 = *v4;
result = addrcmp(a1, *v4);
if ( (_DWORD)result )
break;
if ( *(_QWORD *)(v5 + 16) < a2 )
{
v4 = (_QWORD *)v4[2];
goto LABEL_8;
}
if ( !v4[1] )
goto LABEL_8;
v4 = (_QWORD *)v4[1];
}
}
else
{
curr_p_count = 0;
LABEL_8:
v7 = new_peer_node(a1, a2, a3);
result = sorted_insert_node(peer_list_head, v4, v7);
++curr_p_count;
peer_list_head = result;
}
return result;
}

original_code:
void sorted_insert_peer(const struct sockaddr_in *peer_addr, unsigned long peer_rate, unsigned short dw_port) {
 struct node *tmp_node;
 struct peer_node *tmp_peernode;
 if (peer_list_head == ((void *)0)) {
  curr_p_count = 0;
 }
 tmp_node = peer_list_head;
 while (tmp_node != ((void *)0)) {
  tmp_peernode = (struct peer_node *)tmp_node->data;
  if (addrcmp(peer_addr, &tmp_peernode->peer_addr)) {
   return;
  }
  if (peer_rate > tmp_peernode->peer_rate) {
   tmp_node = tmp_node->prev;
   break;
  }
  if (tmp_node->next == ((void *)0)) {
   break;
  }
  tmp_node = tmp_node->next;
 }
  peer_list_head = sorted_insert_node(peer_list_head, tmp_node, new_peer_node(peer_addr, peer_rate, dw_port));
 curr_p_count ++;
}

canonical_original_code:
void sorted_insert_peer(struct sockaddr_in *peer_addr, unsigned long peer_rate, unsigned short dw_port) {
 struct node *tmp_node;
 struct peer_node *tmp_peernode;
 if (peer_list_head == ((void *)0)) {
  curr_p_count = 0;
 }
 tmp_node = peer_list_head;
 while (tmp_node != ((void *)0)) {
  tmp_peernode = (struct peer_node *)tmp_node->data;
  if (addrcmp(peer_addr, &tmp_peernode->peer_addr)) {
   return;
  }
  if (peer_rate > tmp_peernode->peer_rate) {
   tmp_node = tmp_node->prev;
   break;
  }
  if (tmp_node->next == ((void *)0)) {
   break;
  }
  tmp_node = tmp_node->next;
 }
  peer_list_head = sorted_insert_node(peer_list_head, tmp_node, new_peer_node(peer_addr, peer_rate, dw_port));
 curr_p_count ++;
}

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: struct peer_node { 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]; } peer_addr; unsigned long peer_rate; unsigned short dw_port; char flag; } *


decompiled_code:
__int64 __fastcall insert_peer(__int64 a1, __int64 a2, __int16 a3)
{
__int64 *v4; // rax
__int64 result; // rax
_QWORD *v6; // rax

if ( peer_list_head )
{
v4 = get_node_peer(a1);
if ( v4 )
{
LABEL_3:
result = *v4;
*(_BYTE *)(result + 26) = 1;
return result;
}
}
else
{
curr_p_count = 0;
v4 = get_node_peer(a1);
if ( v4 )
goto LABEL_3;
}
v6 = new_peer_node(a1, a2, a3);
result = insert_node(peer_list_head, v6);
++curr_p_count;
peer_list_head = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int16 a3)
{
__int64 *v4; // rax
__int64 result; // rax
_QWORD *v6; // rax

if ( peer_list_head )
{
v4 = get_node_peer(a1);
if ( v4 )
{
LABEL_3:
result = *v4;
*(_BYTE *)(result + 26) = 1;
return result;
}
}
else
{
curr_p_count = 0;
v4 = get_node_peer(a1);
if ( v4 )
goto LABEL_3;
}
v6 = new_peer_node(a1, a2, a3);
result = insert_node(peer_list_head, v6);
++curr_p_count;
peer_list_head = result;
return result;
}

original_code:
void insert_peer(const struct sockaddr_in *peer_addr, unsigned long peer_rate, unsigned short dw_port) {
 struct node *tmp_node;
 struct peer_node *new_node;
 if (peer_list_head == ((void *)0)) {
  curr_p_count = 0;
 }
 if ((tmp_node = get_node_peer(peer_addr)) == ((void *)0)) {
  new_node = new_peer_node(peer_addr, peer_rate, dw_port);
  peer_list_head = insert_node(peer_list_head, new_node);
  curr_p_count ++;
 } else {
  ((struct peer_node *)tmp_node->data)->flag = 1;
 }
}

canonical_original_code:
void insert_peer(struct sockaddr_in *peer_addr, unsigned long peer_rate, unsigned short dw_port) {
 struct node *tmp_node;
 struct peer_node *new_node;
 if (peer_list_head == ((void *)0)) {
  curr_p_count = 0;
 }
 if ((tmp_node = get_node_peer(peer_addr)) == ((void *)0)) {
  new_node = new_peer_node(peer_addr, peer_rate, dw_port);
  peer_list_head = insert_node(peer_list_head, new_node);
  curr_p_count ++;
 } else {
  ((struct peer_node *)tmp_node->data)->flag = 1;
 }
}


decompiled_code:
void **__fastcall remove_peer(__int64 a1)
{
void **result; // rax
void **v2; // rbp

result = (void **)get_node_peer(a1);
if ( result )
{
v2 = result;
free(*result);
*v2 = 0LL;
result = (void **)remove_node(peer_list_head, v2);
--curr_p_count;
peer_list_head = (__int64)result;
}
return result;
}

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

result = (void **)get_node_peer(a1);
if ( result )
{
v2 = result;
free(*result);
*v2 = 0LL;
result = (void **)remove_node(peer_list_head, v2);
--curr_p_count;
peer_list_head = (__int64)result;
}
return result;
}

original_code:
void remove_peer(const struct sockaddr_in *peer_addr) {
 struct node *tmp_node;
 if ((tmp_node = get_node_peer(peer_addr)) != ((void *)0)) {
  free(tmp_node->data);
  tmp_node->data = ((void *)0);
  peer_list_head = remove_node(peer_list_head, tmp_node);
  curr_p_count --;
 }
}

canonical_original_code:
void remove_peer(struct sockaddr_in *peer_addr) {
 struct node *tmp_node;
 if ((tmp_node = get_node_peer(peer_addr)) != ((void *)0)) {
  free(tmp_node->data);
  tmp_node->data = ((void *)0);
  peer_list_head = remove_node(peer_list_head, tmp_node);
  curr_p_count --;
 }
}


decompiled_code:
void __fastcall remove_peer_node(void **a1)
{
if ( a1 )
{
if ( peer_list_head )
{
free(*a1);
*a1 = 0LL;
peer_list_head = remove_node(peer_list_head, a1);
printf("CURR PEER COUNT = %d\n", (unsigned int)--curr_p_count);
}
}
}

canonical_decompiled_code:
void __fastcall func3(void **a1)
{
if ( a1 )
{
if ( peer_list_head )
{
free(*a1);
*a1 = 0LL;
peer_list_head = remove_node(peer_list_head, a1);
printf("CURR PEER COUNT = %d\n", (unsigned int)--curr_p_count);
}
}
}

original_code:
void remove_peer_node(struct node *peer_node) {
 if (peer_node == ((void *)0) || peer_list_head == ((void *)0)) {
  return;
 }
 free(peer_node->data);
 peer_node->data = ((void *)0);
 peer_list_head = remove_node(peer_list_head, peer_node);
 curr_p_count --;
 printf("CURR PEER COUNT = %d\n", curr_p_count);
}


decompiled_code:
void free_list_peer()
{
void **i; // rdi

for ( i = (void **)peer_list_head; peer_list_head; i = (void **)peer_list_head )
remove_peer_node(i);
}

canonical_decompiled_code:
void func4()
{
void **i; // rdi

for ( i = (void **)peer_list_head; peer_list_head; i = (void **)peer_list_head )
func3(i);
}

original_code:
void free_list_peer() {
 while (peer_list_head != ((void *)0)) {
  remove_peer_node(peer_list_head);
 }
}


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

result = peer_list_head;
if ( peer_list_head )
result = *(_QWORD *)peer_list_head;
return result;
}

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

result = peer_list_head;
if ( peer_list_head )
result = *(_QWORD *)peer_list_head;
return result;
}

original_code:
struct sockaddr_in *get_addr_head() {
 if (peer_list_head == ((void *)0)) {
  return ((void *)0);
 }
 if (peer_list_head->data == ((void *)0)) {
  return ((void *)0);
 }
 return &((struct peer_node *)peer_list_head->data)->peer_addr;
}

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


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

result = 0LL;
maxd = -1;
fd_count = 0;
memset(&allset, 0, 0x80uLL);
*((_DWORD *)&fds + 8) = -1;
fds = -1LL;
*(&fds + 1) = -1LL;
return result;
}

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

result = 0LL;
maxd = -1;
fd_count = 0;
memset(&allset, 0, 0x80uLL);
*((_DWORD *)&fds + 8) = -1;
fds = -1LL;
*(&fds + 1) = -1LL;
return result;
}


decompiled_code:
__int64 __fastcall fd_add(int a1)
{
int v1; // esi
__int64 v2; // rax
__int64 result; // rax

v1 = fd_count;
if ( fd_count > 8 )
return 0xFFFFFFFFLL;
*((_QWORD *)&allset + a1 / 64) |= 1LL << (a1 % 64);
if ( maxd < a1 )
maxd = a1;
v2 = 0LL;
while ( *((_DWORD *)&fds + v2) != -1 )
{
if ( ++v2 == 9 )
return 0LL;
}
*((_DWORD *)&fds + (int)v2) = a1;
result = 0LL;
fd_count = v1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // esi
__int64 v2; // rax
__int64 result; // rax

v1 = fd_count;
if ( fd_count > 8 )
return 0xFFFFFFFFLL;
*((_QWORD *)&allset + a1 / 64) |= 1LL << (a1 % 64);
if ( maxd < a1 )
maxd = a1;
v2 = 0LL;
while ( *((_DWORD *)&fds + v2) != -1 )
{
if ( ++v2 == 9 )
return 0LL;
}
*((_DWORD *)&fds + (int)v2) = a1;
result = 0LL;
fd_count = v1 + 1;
return result;
}

original_code:
int fd_add(int fd) {
 int i;
 if (fd_count >= 9) {
  return -1;
 }
 ((void) (((&allset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (fd > maxd) {
  maxd = fd;
 }
 for (i = 0; i < 9; i ++) {
  if (fds[i] == -1) {
   fds[i] = fd;
   fd_count ++;
   break;
  }
 }
 return 0;
}

variable_types: fd int


original_code:
int fd_select() {
 rset = allset;
 return select(maxd + 1, &rset, ((void *)0), ((void *)0), ((void *)0));
}