decompiled_code:
__int64 __fastcall sp_register(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbx
int v7; // eax
int v8; // eax
unsigned int v9; // ebx
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 )
{
v11 = strerror(v7);
fprintf(stderr, "sp_register error - can't acquire lock: %s\n", v11);
v9 = -1;
}
else
{
insert_sp(a2, *(unsigned __int8 *)(a3 + 10));
v8 = pthread_mutex_unlock(v6);
v9 = v8;
if ( v8 )
{
v12 = strerror(v8);
fprintf(stderr, "sp_register error - can't release lock: %s\n", v12);
v9 = -1;
}
else
{
new_ack_packet(v13, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v13) < 0 )
{
perror("errore in sendto");
v9 = -1;
}
}
}
}
else
{
fwrite("sp_register error - invalid packet format\n", 1uLL, 0x2AuLL, stderr);
v9 = -1;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3, __int64 a4)
{
pthread_mutex_t *v6; // rbx
int v7; // eax
int v8; // eax
unsigned int v9; // ebx
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 )
{
v11 = strerror(v7);
fprintf(stderr, "sp_register error - can't acquire lock: %s\n", v11);
v9 = -1;
}
else
{
insert_sp(a2, *(unsigned __int8 *)(a3 + 10));
v8 = pthread_mutex_unlock(v6);
v9 = v8;
if ( v8 )
{
v12 = strerror(v8);
fprintf(stderr, "sp_register error - can't release lock: %s\n", v12);
v9 = -1;
}
else
{
new_ack_packet(v13, *(unsigned __int16 *)(a3 + 4));
if ( (int)send_packet(a1, a2, v13) < 0 )
{
perror("errore in sendto");
v9 = -1;
}
}
}
}
else
{
fwrite("sp_register error - invalid packet format\n", 1uLL, 0x2AuLL, stderr);
v9 = -1;
}
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; }


repo: sakkko/Karmanet

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

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

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

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

variable_types: rc int

return_type: int

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

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

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

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

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


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

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

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

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

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
_BYTE v9[1044]; // [rsp+0h] [rbp-448h] BYREF
char v10[52]; // [rsp+414h] [rbp-34h] BYREF

v6 = is_set_flag(a3, 4294967168LL);
v7 = set_str_addrlist((__int64)v10, v6, a4);
if ( v7 < 0 )
{
fwrite("send_addr_list error - set_str_addrlist failed\n", 1uLL, 0x2FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
new_packet(v9, "LIS", *(unsigned __int16 *)(a3 + 4), v10, (unsigned __int16)(6 * v7), 1LL);
result = (unsigned int)((int)send_packet(a1, a2, v9) >> 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
_BYTE v9[1044]; // [rsp+0h] [rbp-448h] BYREF
char v10[52]; // [rsp+414h] [rbp-34h] BYREF

v6 = is_set_flag(a3, 4294967168LL);
v7 = func3((__int64)v10, v6, a4);
if ( v7 < 0 )
{
fwrite("send_addr_list error - set_str_addrlist failed\n", 1uLL, 0x2FuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
new_packet(v9, "LIS", *(unsigned __int16 *)(a3 + 4), v10, (unsigned __int16)(6 * v7), 1LL);
result = (unsigned int)((int)send_packet(a1, a2, v9) >> 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
int v8; // eax
unsigned int v9; // ebx
char *v11; // rax
char *v12; // rax
__int64 v13; // [rsp+0h] [rbp-438h] BYREF
__int16 v14; // [rsp+8h] [rbp-430h]
char v15[1070]; // [rsp+Ah] [rbp-42Eh] BYREF

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

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

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

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
_QWORD v6[8]; // [rsp+0h] [rbp-478h] BYREF
char v7[1040]; // [rsp+40h] [rbp-438h] BYREF
char v8[28]; // [rsp+450h] [rbp-28h] BYREF
int v9[3]; // [rsp+46Ch] [rbp-Ch] BYREF

v3 = udp_socket(argc, argv, envp);
if ( v3 < 0 )
{
perror("socket error");
}
else
{
v4 = v3;
set_addr_any(v8, 5193LL);
if ( (int)inet_bind(v4, v8) < 0 )
{
perror("bind error");
}
else
{
addr_to_send = 2;
v6[7] = &sp_list_head;
if ( (int)sp_list_checker_run(v6) < 0 )
{
fwrite("Can't start superpeer list checker\n", 1uLL, 0x23uLL, stderr);
}
else
{
v9[0] = 16;
while ( (int)recvfrom_packet(v4, v8, v7, v9) >= 0 )
{
if ( !memcmp(v7, "PIN", 3uLL) )
{
if ( (int)update_sp_flag(v6, v8, (unsigned int)v7[10]) < 0 )
fwrite("Ping error\n", 1uLL, 0xBuLL, stderr);
}
else if ( !memcmp(v7, "JOI", 3uLL) )
{
if ( (int)sp_join(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 0 )
fwrite("Join error\n", 1uLL, 0xBuLL, stderr);
}
else if ( !memcmp(v7, "LEA", 3uLL) )
{
if ( (int)sp_leave(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 0 )
fwrite("Leave error\n", 1uLL, 0xCuLL, stderr);
}
else if ( !memcmp(v7, "REG", 3uLL) && (int)sp_register(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 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
_QWORD v6[8]; // [rsp+0h] [rbp-478h] BYREF
char v7[1040]; // [rsp+40h] [rbp-438h] BYREF
char v8[28]; // [rsp+450h] [rbp-28h] BYREF
int v9[3]; // [rsp+46Ch] [rbp-Ch] BYREF

v3 = udp_socket(argc, argv, envp);
if ( v3 < 0 )
{
perror("socket error");
}
else
{
v4 = v3;
set_addr_any(v8, 5193LL);
if ( (int)inet_bind(v4, v8) < 0 )
{
perror("bind error");
}
else
{
addr_to_send = 2;
v6[7] = &sp_list_head;
if ( (int)sp_list_checker_run(v6) < 0 )
{
fwrite("Can't start superpeer list checker\n", 1uLL, 0x23uLL, stderr);
}
else
{
v9[0] = 16;
while ( (int)recvfrom_packet(v4, v8, v7, v9) >= 0 )
{
if ( !memcmp(v7, "PIN", 3uLL) )
{
if ( (int)update_sp_flag(v6, v8, (unsigned int)v7[10]) < 0 )
fwrite("Ping error\n", 1uLL, 0xBuLL, stderr);
}
else if ( !memcmp(v7, "JOI", 3uLL) )
{
if ( (int)func5(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 0 )
fwrite("Join error\n", 1uLL, 0xBuLL, stderr);
}
else if ( !memcmp(v7, "LEA", 3uLL) )
{
if ( (int)func2(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 0 )
fwrite("Leave error\n", 1uLL, 0xCuLL, stderr);
}
else if ( !memcmp(v7, "REG", 3uLL) && (int)func1(v4, (__int64)v8, (__int64)v7, (__int64)v6) < 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 v2; // r12
char *v3; // rax
int v4; // eax
char *v5; // rax
__int64 v6; // rsi
__int64 *v7; // r13
__int64 *v8; // rbx
__int64 v9; // rax
char *v10; // rax
char *v11; // rax
__int64 v12; // rdi
__int64 v13; // rbx
__int16 v14; // ax
int v15; // eax
int v16; // eax
int v17; // eax
int v18; // eax
char *v19; // rax
struct timespec v20; // [rsp+0h] [rbp-38h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func0(__int64 a1)
{
__int16 v2; // r12
char *v3; // rax
int v4; // eax
char *v5; // rax
__int64 v6; // rsi
__int64 *v7; // r13
__int64 *v8; // rbx
__int64 v9; // rax
char *v10; // rax
char *v11; // rax
__int64 v12; // rdi
__int64 v13; // rbx
__int16 v14; // ax
int v15; // eax
int v16; // eax
int v17; // eax
int v18; // eax
char *v19; // rax
struct timespec v20; // [rsp+0h] [rbp-38h] BYREF

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

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

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

variable_types: rc int


decompiled_code:
__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
char *v6; // rax
int result; // eax
__int64 v8; // rbx
char *v9; // rax

v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", v6);
result = -1;
}
else
{
v5 = **(_QWORD ***)(a1 + 56);
if ( v5 )
{
while ( !(unsigned int)addrcmp(*v5, a2) )
{
v5 = (_QWORD *)v5[1];
if ( !v5 )
goto LABEL_8;
}
*(_BYTE *)(*v5 + 26LL) = 1;
v8 = *v5;
*(_QWORD *)(v8 + 16) = btol(a3 + 10);
}
LABEL_8:
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
char *v6; // rax
int result; // eax
__int64 v8; // rbx
char *v9; // rax

v4 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v4 )
{
v6 = strerror(v4);
fprintf(stderr, "update_peer_flag error - can't acquire lock: %s\n", v6);
result = -1;
}
else
{
v5 = **(_QWORD ***)(a1 + 56);
if ( v5 )
{
while ( !(unsigned int)addrcmp(*v5, a2) )
{
v5 = (_QWORD *)v5[1];
if ( !v5 )
goto LABEL_8;
}
*(_BYTE *)(*v5 + 26LL) = 1;
v8 = *v5;
*(_QWORD *)(v8 + 16) = btol(a3 + 10);
}
LABEL_8:
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)
{
char *v1; // rax
unsigned int v2; // er13
unsigned __int16 v3; // r12
char *v4; // rax
__int64 v5; // rbx
__int64 *v6; // rbp
unsigned __int16 v7; // r12
char *v8; // rax
int v9; // eax
int v10; // eax
char *v11; // rax
struct timespec v12; // [rsp+0h] [rbp-48h] BYREF

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

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

v12.tv_sec = 6LL;
v12.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v12, 0LL);
v10 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
v2 = v10;
if ( v10 )
break;
v6 = **(__int64 ***)(a1 + 56);
while ( v6 )
{
v5 = *v6;
v6 = (__int64 *)v6[1];
if ( *(_WORD *)(v5 + 18) )
{
++v2;
*(_WORD *)(v5 + 18) = 0;
v3 = func0(*(_WORD *)(v5 + 2));
v4 = inet_ntoa(*(struct in_addr *)(v5 + 4));
printf("Imposto flag: 0 per %s:%d\n", v4, v3);
}
else
{
v7 = func0(*(_WORD *)(v5 + 2));
v8 = inet_ntoa(*(struct in_addr *)(v5 + 4));
printf("cancello nodo: %s:%d\n", v8, v7);
remove_sp(v5);
}
}
printf("trovati %d super peer\n", v2);
v9 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v9 )
{
v11 = strerror(v9);
fprintf(stderr, "sp_list_checker_func error - can't release lock: %s\n", v11);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
v1 = strerror(v10);
fprintf(stderr, "sp_list_checker_func error - can't acquire lock: %s\n", v1);
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:
__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 *v4; // rbx
int v5; // eax
__int64 *v6; // rax
__int64 v7; // rax
int result; // eax
char *v9; // rax
int v10; // edi
char *v11; // rax
char *v12; // rax

v4 = (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(v4);
result = -1;
if ( !v10 )
return result;
goto LABEL_8;
}
v7 = *v6;
if ( !v7 )
{
puts(aErroreInSetSpF);
v10 = pthread_mutex_unlock(v4);
result = -1;
if ( !v10 )
return result;
LABEL_8:
v11 = strerror(v10);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v11);
return -1;
}
*(_WORD *)(v7 + 18) = 1;
*(_BYTE *)(v7 + 16) = a3;
result = pthread_mutex_unlock(v4);
if ( result )
{
v12 = strerror(result);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v12);
result = -1;
}
return result;
}

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

v4 = (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(v4);
result = -1;
if ( !v10 )
return result;
goto LABEL_8;
}
v7 = *v6;
if ( !v7 )
{
puts(aErroreInSetSpF);
v10 = pthread_mutex_unlock(v4);
result = -1;
if ( !v10 )
return result;
LABEL_8:
v11 = strerror(v10);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v11);
return -1;
}
*(_WORD *)(v7 + 18) = 1;
*(_BYTE *)(v7 + 16) = a3;
result = pthread_mutex_unlock(v4);
if ( result )
{
v12 = strerror(result);
fprintf(stderr, "update_sp_flag error - can't release lock: %s\n", v12);
result = -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(char *a1)
{
int v2; // eax
unsigned int v3; // ebp
__int64 result; // rax
int v5; // eax
unsigned __int16 v6; // cx
const char *v7; // r8
const char *v8; // rdx
char v9[256]; // [rsp+0h] [rbp-218h] BYREF
char v10[9]; // [rsp+100h] [rbp-118h] BYREF
char src[2]; // [rsp+109h] [rbp-10Fh] BYREF
char v12[2]; // [rsp+10Bh] [rbp-10Dh] BYREF
char v13[267]; // [rsp+10Dh] [rbp-10Bh] BYREF

v2 = open("../karma.conf", 0);
if ( v2 < 0 )
{
perror("read_config error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
while ( 1 )
{
v5 = readline(v3, v10, 255LL);
if ( v5 <= 0 )
break;
v10[v5 - 1] = 0;
if ( v10[0] != 35 )
{
if ( !memcmp(v10, "udp_port=", 9uLL) )
{
strcpy(v9, src);
*(_WORD *)a1 = atoi(v9);
}
else if ( !memcmp(v10, "tcp_port=", 9uLL) )
{
strcpy(v9, src);
*((_WORD *)a1 + 1) = atoi(v9);
}
else if ( !memcmp(v10, "share_folder=", 0xDuLL) )
{
strcpy(a1 + 8, v13);
}
else if ( !memcmp(v10, "max_download=", 0xDuLL) )
{
strcpy(v9, v13);
*((_WORD *)a1 + 2) = atoi(v9);
}
else if ( !memcmp(v10, "max_upload=", 0xBuLL) )
{
strcpy(v9, v12);
*((_WORD *)a1 + 3) = atoi(v9);
}
}
}
v6 = *((_WORD *)a1 + 1);
v7 = "(porta scelta dal kernel)";
if ( v6 )
v7 = (const char *)&unk_346;
v8 = "(porta scelta dal kernel)";
if ( *(_WORD *)a1 )
v8 = (const char *)&unk_346;
printf(
"UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n",
*(unsigned __int16 *)a1,
v8,
v6,
v7,
a1 + 8,
*((unsigned __int16 *)a1 + 2),
*((unsigned __int16 *)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(char *a1)
{
int v2; // eax
unsigned int v3; // ebp
__int64 result; // rax
int v5; // eax
unsigned __int16 v6; // cx
const char *v7; // r8
const char *v8; // rdx
char v9[256]; // [rsp+0h] [rbp-218h] BYREF
char v10[9]; // [rsp+100h] [rbp-118h] BYREF
char src[2]; // [rsp+109h] [rbp-10Fh] BYREF
char v12[2]; // [rsp+10Bh] [rbp-10Dh] BYREF
char v13[267]; // [rsp+10Dh] [rbp-10Bh] BYREF

v2 = open("../karma.conf", 0);
if ( v2 < 0 )
{
perror("read_config error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
while ( 1 )
{
v5 = readline(v3, v10, 255LL);
if ( v5 <= 0 )
break;
v10[v5 - 1] = 0;
if ( v10[0] != 35 )
{
if ( !memcmp(v10, "udp_port=", 9uLL) )
{
strcpy(v9, src);
*(_WORD *)a1 = atoi(v9);
}
else if ( !memcmp(v10, "tcp_port=", 9uLL) )
{
strcpy(v9, src);
*((_WORD *)a1 + 1) = atoi(v9);
}
else if ( !memcmp(v10, "share_folder=", 0xDuLL) )
{
strcpy(a1 + 8, v13);
}
else if ( !memcmp(v10, "max_download=", 0xDuLL) )
{
strcpy(v9, v13);
*((_WORD *)a1 + 2) = atoi(v9);
}
else if ( !memcmp(v10, "max_upload=", 0xBuLL) )
{
strcpy(v9, v12);
*((_WORD *)a1 + 3) = atoi(v9);
}
}
}
v6 = *((_WORD *)a1 + 1);
v7 = "(porta scelta dal kernel)";
if ( v6 )
v7 = (const char *)&unk_346;
v8 = "(porta scelta dal kernel)";
if ( *(_WORD *)a1 )
v8 = (const char *)&unk_346;
printf(
"UDP_PORT = %u %s\nTCP_PORT = %u %s\nSHARE_FOLDER = %s\nMAX_DOWNLOAD = %d\nMAX_UPLOAD = %d\n",
*(unsigned __int16 *)a1,
v8,
v6,
v7,
a1 + 8,
*((unsigned __int16 *)a1 + 2),
*((unsigned __int16 *)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; // rbx

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

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

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

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_list_head = result;
++download_count;
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_list_head = result;
++download_count;
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

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

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

result = download_list_head;
while ( download_list_head )
{
if ( !*(_QWORD *)(download_list_head + 8) )
{
v1 = *(_QWORD *)download_list_head;
download_list_head = remove_node(download_list_head, download_list_head);
--download_count;
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; // er13
unsigned __int64 result; // rax
int *v2; // r12
double v3; // xmm0_8
int v4; // edx
bool v5; // zf
int v6; // ebx
char *v7; // rax
int v8; // edx
double v9; // [rsp+8h] [rbp-430h]
char v10[1064]; // [rsp+10h] [rbp-428h] BYREF

v0 = open("/proc/cpuinfo", 0);
v9 = 0.0;
if ( v0 >= 0 )
{
while ( 1 )
{
v4 = readline((unsigned int)v0, v10, 1024LL);
if ( v4 <= 0 )
break;
v5 = memcmp("cpu MHz", v10, 7uLL) == 0;
v6 = !v5;
if ( v5 )
{
v10[v4 - 1] = 0;
v7 = v10;
do
{
if ( *v7 == 58 )
break;
++v6;
++v7;
}
while ( v4 > v6 );
v2 = _errno_location();
*v2 = 0;
v3 = strtod(&v10[v6 + 1], 0LL);
if ( *v2 == 34 )
{
perror("get_cpu_freq error - strtod failed");
v8 = close(v0);
result = 0LL;
if ( v8 < 0 )
{
perror("get_cpu_freq error - close failed");
result = 0LL;
}
return result;
}
v9 = v3 + v9;
}
}
if ( close(v0) < 0 )
perror("get_cpu_freq error - close failed");
if ( v9 >= 9.223372036854776e18 )
result = (unsigned int)(int)(v9 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
result = (unsigned int)(int)v9;
}
else
{
perror("get_cpu_freq error - open failed");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func0()
{
int v0; // er13
unsigned __int64 result; // rax
int *v2; // r12
double v3; // xmm0_8
int v4; // edx
bool v5; // zf
int v6; // ebx
char *v7; // rax
int v8; // edx
double v9; // [rsp+8h] [rbp-430h]
char v10[1064]; // [rsp+10h] [rbp-428h] BYREF

v0 = open("/proc/cpuinfo", 0);
v9 = 0.0;
if ( v0 >= 0 )
{
while ( 1 )
{
v4 = readline((unsigned int)v0, v10, 1024LL);
if ( v4 <= 0 )
break;
v5 = memcmp("cpu MHz", v10, 7uLL) == 0;
v6 = !v5;
if ( v5 )
{
v10[v4 - 1] = 0;
v7 = v10;
do
{
if ( *v7 == 58 )
break;
++v6;
++v7;
}
while ( v4 > v6 );
v2 = _errno_location();
*v2 = 0;
v3 = strtod(&v10[v6 + 1], 0LL);
if ( *v2 == 34 )
{
perror("get_cpu_freq error - strtod failed");
v8 = close(v0);
result = 0LL;
if ( v8 < 0 )
{
perror("get_cpu_freq error - close failed");
result = 0LL;
}
return result;
}
v9 = v3 + v9;
}
}
if ( close(v0) < 0 )
perror("get_cpu_freq error - close failed");
if ( v9 >= 9.223372036854776e18 )
result = (unsigned int)(int)(v9 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
result = (unsigned int)(int)v9;
}
else
{
perror("get_cpu_freq error - open failed");
result = 0LL;
}
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; // edx
__int64 result; // rax
__kernel_long_t v2; // rbx
__int64 v3; // rax
unsigned __int64 v4; // rax
__kernel_ulong_t v5; // rdx
__int64 v6; // rbp
__int64 v7; // rbx
struct sysinfo v8; // [rsp+0h] [rbp-88h] BYREF

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

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

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

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


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

if ( !a1 )
return 0LL;
LODWORD(result) = 0;
do
{
a1 = *(_QWORD *)(a1 + 8);
result = (unsigned int)(result + 1);
}
while ( a1 );
return result;
}

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

if ( !a1 )
return 0LL;
LODWORD(result) = 0;
do
{
a1 = *(_QWORD *)(a1 + 8);
result = (unsigned int)(result + 1);
}
while ( a1 );
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; // rbx
_BYTE *v5; // rax
__int64 result; // rax
__int64 v7; // rbp

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

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

v4 = (_QWORD *)peer_list_head;
if ( peer_list_head )
{
while ( 1 )
{
v7 = *v4;
result = addrcmp(a1, *v4);
if ( (_DWORD)result )
break;
if ( *(_QWORD *)(v7 + 16) < a2 )
{
v4 = (_QWORD *)v4[2];
goto LABEL_3;
}
if ( !v4[1] )
goto LABEL_3;
v4 = (_QWORD *)v4[1];
}
}
else
{
curr_p_count = 0;
LABEL_3:
v5 = new_peer_node(a1, a2, a3);
result = sorted_insert_node(peer_list_head, v4, v5);
peer_list_head = result;
++curr_p_count;
}
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
_BYTE *v6; // rax

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

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

if ( !peer_list_head )
curr_p_count = 0;
v4 = get_node_peer(a1);
if ( v4 )
{
result = *v4;
*(_BYTE *)(result + 26) = 1;
}
else
{
v6 = new_peer_node(a1, a2, a3);
result = insert_node(peer_list_head, v6);
peer_list_head = result;
++curr_p_count;
}
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; // rbx

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

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

result = (void **)get_node_peer(a1);
if ( result )
{
v2 = result;
free(*result);
*v2 = 0LL;
result = (void **)remove_node(peer_list_head, v2);
peer_list_head = (__int64)result;
--curr_p_count;
}
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:
int *fd_init()
{
int *result; // rax

maxd = -1;
fd_count = 0;
memset(&allset, 0, 0x80uLL);
result = &fds;
do
*result++ = -1;
while ( result != (int *)(unsigned int)((_DWORD)&fds + 36) );
return result;
}

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

maxd = -1;
fd_count = 0;
memset(&allset, 0, 0x80uLL);
result = &fds;
do
*result++ = -1;
while ( result != (int *)(unsigned int)((_DWORD)&fds + 36) );
return result;
}


decompiled_code:
__int64 __fastcall fd_add(int a1)
{
int v1; // esi
__int64 v2; // 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 ( fds[v2] != -1 )
{
if ( ++v2 == 9 )
return 0LL;
}
fds[(int)v2] = a1;
fd_count = v1 + 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // esi
__int64 v2; // 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 ( fds[v2] != -1 )
{
if ( ++v2 == 9 )
return 0LL;
}
fds[(int)v2] = a1;
fd_count = v1 + 1;
return 0LL;
}

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


decompiled_code:
int fd_select()
{
*(__m128i *)rset.fds_bits = _mm_load_si128(allset);
*(__m128i *)&rset.fds_bits[2] = _mm_load_si128(&allset[1]);
*(__m128i *)&rset.fds_bits[4] = _mm_load_si128(&allset[2]);
*(__m128i *)&rset.fds_bits[6] = _mm_load_si128(&allset[3]);
*(__m128i *)&rset.fds_bits[8] = _mm_load_si128(&allset[4]);
*(__m128i *)&rset.fds_bits[10] = _mm_load_si128(&allset[5]);
*(__m128i *)&rset.fds_bits[12] = _mm_load_si128(&allset[6]);
*(__m128i *)&rset.fds_bits[14] = _mm_load_si128(&allset[7]);
return select(maxd + 1, &rset, 0LL, 0LL, 0LL);
}

canonical_decompiled_code:
int func3()
{
*(__m128i *)rset.fds_bits = _mm_load_si128(allset);
*(__m128i *)&rset.fds_bits[2] = _mm_load_si128(&allset[1]);
*(__m128i *)&rset.fds_bits[4] = _mm_load_si128(&allset[2]);
*(__m128i *)&rset.fds_bits[6] = _mm_load_si128(&allset[3]);
*(__m128i *)&rset.fds_bits[8] = _mm_load_si128(&allset[4]);
*(__m128i *)&rset.fds_bits[10] = _mm_load_si128(&allset[5]);
*(__m128i *)&rset.fds_bits[12] = _mm_load_si128(&allset[6]);
*(__m128i *)&rset.fds_bits[14] = _mm_load_si128(&allset[7]);
return select(maxd + 1, &rset, 0LL, 0LL, 0LL);
}

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


decompiled_code:
_BOOL8 __fastcall fd_ready(int a1)
{
return (rset.fds_bits[a1 / 64] & (1LL << (a1 % 64))) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(int a1)
{
return (rset.fds_bits[a1 / 64] & (1LL << (a1 % 64))) != 0;
}

original_code:
int fd_ready(int fd) {
 return ((((&rset)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))) != 0);
}


decompiled_code:
void __fastcall __noreturn sp_checker_func(__int64 a1)
{
int v1; // eax
int v2; // eax
char *v3; // rax
char *v4; // rax
int v5; // eax
char *v6; // rax
int v7; // eax
char *v8; // rax
int v9; // eax
char *v10; // rax
int v11; // eax
char *v12; // rax
struct timespec v13; // [rsp+0h] [rbp-28h] BYREF

v13.tv_sec = 12LL;
v13.tv_nsec = 0LL;
puts("AVVIO PROCESSO DI CONTROLLO SP");
while ( 1 )
{
nanosleep(&v13, 0LL);
v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v5 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v5 )
{
v6 = strerror(v5);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v6);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
if ( !*(_DWORD *)(a1 + 68) )
{
v7 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v7 )
{
v8 = strerror(v7);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v8);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v9 = pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 56));
if ( v9 )
{
v10 = strerror(v9);
fprintf(stderr, "sp_checker_func error - can't acquire lock on pipe: %s\n", v10);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
puts("IL SUPER PEER NON RISPONDE");
if ( write(*(_DWORD *)(a1 + 64), "RST\n", 4uLL) < 0 )
perror("sp_checker_func error - can't write on pipe");
v11 = pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 56));
if ( v11 )
{
v12 = strerror(v11);
fprintf(stderr, "sp_checker_func error - can't release lock on pipe: %s\n", v12);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
*(_DWORD *)(a1 + 68) = 0;
v2 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v2 )
{
v3 = strerror(v2);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v3);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
v4 = strerror(v1);
fprintf(stderr, "sp_checker_func error - can't acquire lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(__int64 a1)
{
int v1; // eax
int v2; // eax
char *v3; // rax
char *v4; // rax
int v5; // eax
char *v6; // rax
int v7; // eax
char *v8; // rax
int v9; // eax
char *v10; // rax
int v11; // eax
char *v12; // rax
struct timespec v13; // [rsp+0h] [rbp-28h] BYREF

v13.tv_sec = 12LL;
v13.tv_nsec = 0LL;
puts("AVVIO PROCESSO DI CONTROLLO SP");
while ( 1 )
{
nanosleep(&v13, 0LL);
v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v5 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v5 )
{
v6 = strerror(v5);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v6);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
if ( !*(_DWORD *)(a1 + 68) )
{
v7 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v7 )
{
v8 = strerror(v7);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v8);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v9 = pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 56));
if ( v9 )
{
v10 = strerror(v9);
fprintf(stderr, "sp_checker_func error - can't acquire lock on pipe: %s\n", v10);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
puts("IL SUPER PEER NON RISPONDE");
if ( write(*(_DWORD *)(a1 + 64), "RST\n", 4uLL) < 0 )
perror("sp_checker_func error - can't write on pipe");
v11 = pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 56));
if ( v11 )
{
v12 = strerror(v11);
fprintf(stderr, "sp_checker_func error - can't release lock on pipe: %s\n", v12);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
*(_DWORD *)(a1 + 68) = 0;
v2 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v2 )
{
v3 = strerror(v2);
fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", v3);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
v4 = strerror(v1);
fprintf(stderr, "sp_checker_func error - can't acquire lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

original_code:
void sp_checker_func(void *args) {
 struct sp_checker_info *chinfo = (struct sp_checker_info *)args;
 struct timespec sleep_time;
 int rc;
 sleep_time.tv_sec = 12;
 sleep_time.tv_nsec = 0;
 printf("AVVIO PROCESSO DI CONTROLLO SP\n");
 while(1){
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&chinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_checker_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if (chinfo->thinfo.go == 0) {
   if ((rc = pthread_mutex_unlock(&chinfo->thinfo.mutex)) != 0) {
    fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   pthread_exit((void *)0);
  }
  if (chinfo->sp_flag == 0) {
   if ((rc = pthread_mutex_unlock(&chinfo->thinfo.mutex)) != 0) {
    fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   if ((rc = pthread_mutex_lock(chinfo->pipe_mutex)) != 0) {
    fprintf(stderr, "sp_checker_func error - can't acquire lock on pipe: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   printf("IL SUPER PEER NON RISPONDE\n");
   if (write(chinfo->sp_checker_pipe, "RST\n", 4) < 0) {
    perror("sp_checker_func error - can't write on pipe");
   }
   if ((rc = pthread_mutex_unlock(chinfo->pipe_mutex)) != 0) {
    fprintf(stderr, "sp_checker_func error - can't release lock on pipe: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   pthread_exit((void *)0);
  } else {
   chinfo->sp_flag = 0;
  }
  if ((rc = pthread_mutex_unlock(&chinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "sp_checker_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
 }
}

variable_types:
args
void *

variable_types: struct sp_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; union pthread_mutex_t *pipe_mutex; int sp_checker_pipe; int sp_flag; } *

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

variable_types: rc int

user_defined_types: struct sp_checker_info { struct th_info thinfo; union pthread_mutex_t *pipe_mutex; int sp_checker_pipe; int sp_flag; }


decompiled_code:
__int64 __fastcall sp_checker_run(__int64 a1)
{
return thread_run(a1, sp_checker_func, a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return thread_run(a1, sp_checker_func, a1);
}

original_code:
int sp_checker_run(struct sp_checker_info *chinfo) {
 return thread_run(&chinfo->thinfo, (void *)sp_checker_func, (void *)chinfo);
}


decompiled_code:
__int64 sp_checker_stop()
{
return thread_stop();
}

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

original_code:
int sp_checker_stop(struct sp_checker_info *chinfo) {
 return thread_stop(&chinfo->thinfo);
}


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

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
{
v3 = strerror(v1);
fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", v3);
result = -1;
}
else
{
*(_DWORD *)(a1 + 68) = 1;
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( result )
{
v4 = strerror(result);
fprintf(stderr, "update_sp error_flag - can't release lock: %s\n", v4);
result = -1;
}
}
return result;
}

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

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
{
v3 = strerror(v1);
fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", v3);
result = -1;
}
else
{
*(_DWORD *)(a1 + 68) = 1;
result = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( result )
{
v4 = strerror(result);
fprintf(stderr, "update_sp error_flag - can't release lock: %s\n", v4);
result = -1;
}
}
return result;
}

original_code:
int update_sp_flag(struct sp_checker_info *chinfo){
 int rc;
 if ((rc = pthread_mutex_lock(&chinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp_flag error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 chinfo->sp_flag = 1;
 if ((rc = pthread_mutex_unlock(&chinfo->thinfo.mutex)) != 0) {
  fprintf(stderr, "update_sp error_flag - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}


repo: sakti/shttpd

decompiled_code:
__int64 __fastcall split_lines(char *delim, char *s, __int64 a3, int a4)
{
char *v6; // rax
__int64 v7; // rbx
unsigned int v8; // er14
bool v9; // cl

v6 = strtok(s, delim);
if ( a4 <= 0 || !v6 )
return 0;
v7 = 1LL;
do
{
*(_QWORD *)(a3 + 8 * v7 - 8) = v6;
v8 = v7;
v6 = strtok(0LL, delim);
v9 = a4 <= (int)v7++;
}
while ( v6 != 0LL && !v9 );
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *delim, char *s, __int64 a3, int a4)
{
char *v6; // rax
__int64 v7; // rbx
unsigned int v8; // er14
bool v9; // cl

v6 = strtok(s, delim);
if ( a4 <= 0 || !v6 )
return 0;
v7 = 1LL;
do
{
*(_QWORD *)(a3 + 8 * v7 - 8) = v6;
v8 = v7;
v6 = strtok(0LL, delim);
v9 = a4 <= (int)v7++;
}
while ( v6 != 0LL && !v9 );
return v8;
}

original_code:
int split_lines(char *token, char *buffer, char **output, int max_size)
{
    char *tok = ((void *)0);
    int count = 0;
    tok = strtok(buffer, token);
    while (tok != ((void *)0))
    {
        if (count > max_size - 1) break;
        output[count] = tok;
        count++;
        tok = strtok(((void *)0), token);
    }
    return count;
}

variable_types:
token
char *

variable_types:
buffer
char *

variable_types:
output
char **

variable_types:
tok
char *

variable_types: count int