decompiled_code:
int get_rand()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return rand();
}

canonical_decompiled_code:
int func8()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return rand();
}

original_code:
unsigned short get_rand() {
 srand(time(((void *)0)));
 return (unsigned short)rand();
}


repo: sakkko/Karmanet

decompiled_code:
char *__fastcall get_dirpath(char *dest, char *src)
{
int v2; // eax
char *v3; // rdx
__int64 v4; // rbx
char *result; // rax

v2 = strlen(src) - 1;
if ( v2 >= 0 )
{
v3 = &src[v2];
do
{
if ( *v3 == 47 )
break;
--v2;
--v3;
}
while ( v2 != -1 );
}
v4 = v2;
result = strncpy(dest, src, v2);
dest[v4] = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func9(char *dest, char *src)
{
int v2; // eax
char *v3; // rdx
__int64 v4; // rbx
char *result; // rax

v2 = strlen(src) - 1;
if ( v2 >= 0 )
{
v3 = &src[v2];
do
{
if ( *v3 == 47 )
break;
--v2;
--v3;
}
while ( v2 != -1 );
}
v4 = v2;
result = strncpy(dest, src, v2);
dest[v4] = 0;
return result;
}

original_code:
void get_dirpath(char *destpath, const char *fullpath) {
 int i, len;
 len = strlen(fullpath);
 for (i = len - 1; i >= 0; i --) {
  if (fullpath[i] == '/') {
   break;
  }
 }
 strncpy(destpath, fullpath, i);
 destpath[i] = 0;
}

canonical_original_code:
void get_dirpath(char *destpath, char *fullpath) {
 int i; int len;
 len = strlen(fullpath);
 for (i = len - 1; i >= 0; i --) {
  if (fullpath[i] == '/') {
   break;
  }
 }
 strncpy(destpath, fullpath, i);
 destpath[i] = 0;
}

variable_types:
destpath
char *

variable_types:
fullpath
char *

variable_types: i int

variable_types: len int

return_type: void


decompiled_code:
__int64 __fastcall get_file_size(int fd)
{
int v1; // ebp
unsigned int v2; // er12

v1 = lseek(fd, 0LL, 1);
v2 = lseek(fd, 0LL, 2);
lseek(fd, v1, 0);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
int v1; // ebp
unsigned int v2; // er12

v1 = lseek(fd, 0LL, 1);
v2 = lseek(fd, 0LL, 2);
lseek(fd, v1, 0);
return v2;
}

original_code:
int get_file_size(int fd) {
 int old_offset = lseek(fd, 0, 1);
 int ret = lseek(fd, 0, 2);
 lseek(fd, old_offset, 0);
 return ret;
}

variable_types: fd int

variable_types: ret int

return_type: int


readn

decompiled_code:
__int64 __fastcall readn(int fd, void *buf, size_t nbytes)
{
size_t v3; // rbx
ssize_t v5; // rax

v3 = nbytes;
if ( !nbytes )
return (unsigned int)v3;
while ( 1 )
{
v5 = read(fd, buf, v3);
if ( v5 < 0 )
break;
if ( !v5 )
return (unsigned int)v3;
LABEL_5:
buf = (char *)buf + v5;
v3 -= v5;
if ( !v3 )
return (unsigned int)v3;
}
if ( *_errno_location() == 4 )
{
v5 = 0LL;
goto LABEL_5;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd, void *buf, size_t nbytes)
{
size_t v3; // rbx
ssize_t v5; // rax

v3 = nbytes;
if ( !nbytes )
return (unsigned int)v3;
while ( 1 )
{
v5 = read(fd, buf, v3);
if ( v5 < 0 )
break;
if ( !v5 )
return (unsigned int)v3;
LABEL_5:
buf = (char *)buf + v5;
v3 -= v5;
if ( !v3 )
return (unsigned int)v3;
}
if ( *_errno_location() == 4 )
{
v5 = 0LL;
goto LABEL_5;
}
return 0xFFFFFFFFLL;
}

original_code:
int readn(int fd, void *buf, size_t n) {
 size_t nleft;
 ssize_t nread;
 char *ptr;
 ptr = buf;
 nleft = n;
 while (nleft > 0) {
  if ((nread = read(fd, ptr, nleft)) < 0) {
   if ((*__errno_location ()) == 4) {
    nread = 0;
   } else {
    return -1;
   }
  } else if (nread == 0) {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return nleft;
}

canonical_original_code:
int readn(int fd, void *buf, unsigned long n) {
 unsigned long nleft;
 long nread;
 char *ptr;
 ptr = buf;
 nleft = n;
 while (nleft > 0) {
  if ((nread = read(fd, ptr, nleft)) < 0) {
   if ((*__errno_location ()) == 4) {
    nread = 0;
   } else {
    return -1;
   }
  } else if (nread == 0) {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return nleft;
}

variable_types:
buf
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall insert_packet(int a1, __int64 a2, __int64 a3, __int16 a4, __int16 a5, __int16 a6)
{
_WORD *v6; // rax
__int64 result; // rax

if ( !packet_list_head )
packet_count = 0;
v6 = new_packet_node(a1, a2, a3, a4, a5, a6);
result = insert_node(packet_list_head, v6);
packet_list_head = result;
++packet_count;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, __int64 a3, __int16 a4, __int16 a5, __int16 a6)
{
_WORD *v6; // rax
__int64 result; // rax

if ( !packet_list_head )
packet_count = 0;
v6 = new_packet_node(a1, a2, a3, a4, a5, a6);
result = insert_node(packet_list_head, v6);
packet_list_head = result;
++packet_count;
return result;
}

original_code:
void insert_packet(int socksd, const struct packet *pck, const struct sockaddr_in *addr, short nretx, short countdown, short errors) {
 struct packet_node *new_node;
 if (packet_list_head == ((void *)0)) {
  packet_count = 0;
 }
 new_node = new_packet_node(socksd, pck, addr, nretx, countdown, errors);
 packet_list_head = insert_node(packet_list_head, new_node);
 packet_count ++;
}

canonical_original_code:
void insert_packet(int socksd, struct packet *pck, struct sockaddr_in *addr, short nretx, short countdown, short errors) {
 struct packet_node *new_node;
 if (packet_list_head == ((void *)0)) {
  packet_count = 0;
 }
 new_node = new_packet_node(socksd, pck, addr, nretx, countdown, errors);
 packet_list_head = insert_node(packet_list_head, new_node);
 packet_count ++;
}

variable_types: socksd 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 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: nretx short

variable_types: countdown short

variable_types: errors short

variable_types: struct packet_node { int socksd; struct packet { char cmd[3]; char flag; unsigned short index; short TTL; unsigned short data_len; char data[1024]; } pck; 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; short nretx; short countdown; short errors; } *

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 packet_node { int socksd; struct packet pck; struct sockaddr_in addrto; short nretx; short countdown; short errors; }


decompiled_code:
void **__fastcall remove_packet(__int16 a1)
{
void **result; // rax
void **v2; // rbx

result = (void **)get_packet_node(a1);
if ( result )
{
v2 = result;
free(*result);
*v2 = 0LL;
result = (void **)remove_node(packet_list_head, v2);
packet_list_head = (__int64)result;
--packet_count;
}
return result;
}

canonical_decompiled_code:
void **__fastcall func1(__int16 a1)
{
void **result; // rax
void **v2; // rbx

result = (void **)get_packet_node(a1);
if ( result )
{
v2 = result;
free(*result);
*v2 = 0LL;
result = (void **)remove_node(packet_list_head, v2);
packet_list_head = (__int64)result;
--packet_count;
}
return result;
}

original_code:
void remove_packet(unsigned short pck_index) {
 struct node *tmp_node;
 if ((tmp_node = get_packet_node(pck_index)) != ((void *)0)) {
  free(tmp_node->data);
  tmp_node->data = ((void *)0);
  packet_list_head = remove_node(packet_list_head, tmp_node);
  packet_count --;
 }
}

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


decompiled_code:
void __fastcall remove_packet_node(void **a1)
{
if ( a1 )
{
if ( packet_list_head )
{
free(*a1);
*a1 = 0LL;
packet_list_head = remove_node(packet_list_head, a1);
--packet_count;
}
}
}

canonical_decompiled_code:
void __fastcall func2(void **a1)
{
if ( a1 )
{
if ( packet_list_head )
{
free(*a1);
*a1 = 0LL;
packet_list_head = remove_node(packet_list_head, a1);
--packet_count;
}
}
}

original_code:
void remove_packet_node(struct node *packet_node) {
 if (packet_node == ((void *)0) || packet_list_head == ((void *)0)) {
  return;
 }
 free(packet_node->data);
 packet_node->data = ((void *)0);
 packet_list_head = remove_node(packet_list_head, packet_node);
 packet_count --;
}

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


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

for ( i = (void **)packet_list_head; packet_list_head; i = (void **)packet_list_head )
remove_packet_node(i);
}

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

for ( i = (void **)packet_list_head; packet_list_head; i = (void **)packet_list_head )
func2(i);
}

original_code:
void free_packet_list() {
 while (packet_list_head != ((void *)0)) {
  remove_packet_node(packet_list_head);
 }
}


decompiled_code:
__int64 __fastcall pinger_run(__int64 a1)
{
return thread_run(a1, pinger_func, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return thread_run(a1, pinger_func, a1);
}

original_code:
int pinger_run(struct pinger_info *pinfo) {
 return thread_run(&pinfo->thinfo, (void *)&pinger_func, (void *)pinfo);
}

variable_types: struct pinger_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; int socksd; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr_to_ping; } *

user_defined_types: struct pinger_info { struct th_info thinfo; int socksd; struct sockaddr_in addr_to_ping; }

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


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

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

original_code:
int pinger_stop(struct pinger_info *pinfo) {
 return thread_stop(&pinfo->thinfo);
}

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


decompiled_code:
int tcp_ping()
{
int v0; // eax
unsigned int *v1; // rbx
int v2; // eax
int result; // eax
char *v4; // rax
char *v5; // rax
int v6; // edi
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // ebx
char *v12; // rax
char *v13; // rax
char *v14; // rax
_WORD v15[532]; // [rsp+0h] [rbp-428h] BYREF

new_ping_packet(v15, 0LL);
v0 = pthread_mutex_lock(&NEAR_LIST_LOCK);
if ( v0 )
{
v4 = strerror(v0);
fprintf(stderr, "tcp_ping error - can't acquire lock: %s\n", v4);
return -1;
}
add_near_to_packet(v15, near_str, 36LL);
v15[4] = 6 * nsock;
v1 = (unsigned int *)near_list_head;
if ( !near_list_head )
{
LABEL_7:
result = pthread_mutex_unlock(&NEAR_LIST_LOCK);
if ( result )
{
v14 = strerror(result);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v14);
result = -1;
}
return result;
}
while ( 1 )
{
v15[2] = get_index();
v2 = pthread_mutex_lock((pthread_mutex_t *)(v1 + 14));
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "tcp_ping error - can't acquire tcp lock: %s\n", v5);
v6 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
result = -1;
if ( !v6 )
return result;
goto LABEL_11;
}
if ( (int)send_packet_tcp(*v1, v15) < 0 )
break;
if ( pthread_mutex_unlock((pthread_mutex_t *)(v1 + 14)) )
{
v10 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
v11 = v10;
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v13);
}
v12 = strerror(v11);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v12);
return -1;
}
v1 = (unsigned int *)*((_QWORD *)v1 + 13);
if ( !v1 )
goto LABEL_7;
}
fwrite("tcp_ping error - send_packet_tcp failed \n", 1uLL, 0x29uLL, stderr);
v8 = pthread_mutex_unlock((pthread_mutex_t *)(v1 + 14));
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v9);
}
v6 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
result = -1;
if ( v6 )
{
LABEL_11:
v7 = strerror(v6);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v7);
result = -1;
}
return result;
}

canonical_decompiled_code:
int func2()
{
int v0; // eax
unsigned int *v1; // rbx
int v2; // eax
int result; // eax
char *v4; // rax
char *v5; // rax
int v6; // edi
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // ebx
char *v12; // rax
char *v13; // rax
char *v14; // rax
_WORD v15[532]; // [rsp+0h] [rbp-428h] BYREF

new_ping_packet(v15, 0LL);
v0 = pthread_mutex_lock(&NEAR_LIST_LOCK);
if ( v0 )
{
v4 = strerror(v0);
fprintf(stderr, "tcp_ping error - can't acquire lock: %s\n", v4);
return -1;
}
add_near_to_packet(v15, near_str, 36LL);
v15[4] = 6 * nsock;
v1 = (unsigned int *)near_list_head;
if ( !near_list_head )
{
LABEL_7:
result = pthread_mutex_unlock(&NEAR_LIST_LOCK);
if ( result )
{
v14 = strerror(result);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v14);
result = -1;
}
return result;
}
while ( 1 )
{
v15[2] = get_index();
v2 = pthread_mutex_lock((pthread_mutex_t *)(v1 + 14));
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "tcp_ping error - can't acquire tcp lock: %s\n", v5);
v6 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
result = -1;
if ( !v6 )
return result;
goto LABEL_11;
}
if ( (int)send_packet_tcp(*v1, v15) < 0 )
break;
if ( pthread_mutex_unlock((pthread_mutex_t *)(v1 + 14)) )
{
v10 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
v11 = v10;
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v13);
}
v12 = strerror(v11);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v12);
return -1;
}
v1 = (unsigned int *)*((_QWORD *)v1 + 13);
if ( !v1 )
goto LABEL_7;
}
fwrite("tcp_ping error - send_packet_tcp failed \n", 1uLL, 0x29uLL, stderr);
v8 = pthread_mutex_unlock((pthread_mutex_t *)(v1 + 14));
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v9);
}
v6 = pthread_mutex_unlock(&NEAR_LIST_LOCK);
result = -1;
if ( v6 )
{
LABEL_11:
v7 = strerror(v6);
fprintf(stderr, "tcp_ping error - can't release lock: %s\n", v7);
result = -1;
}
return result;
}

original_code:
int tcp_ping(struct pinger_info *pinfo) {
 struct near_node *iterator;
 struct packet ping_packet;
 int rc;
 new_ping_packet(&ping_packet, 0);
 if ((rc = pthread_mutex_lock(&NEAR_LIST_LOCK)) != 0) {
  fprintf(stderr, "tcp_ping error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 add_near_to_packet(&ping_packet, near_str, 6 * 6);
 ping_packet.data_len = 6 * nsock;
 iterator = near_list_head;
 while (iterator != ((void *)0)) {
  ping_packet.index = get_index();
  if ((rc = pthread_mutex_lock(&iterator->mutex)) != 0) {
   fprintf(stderr, "tcp_ping error - can't acquire tcp lock: %s\n", strerror(rc));
   if ((rc = pthread_mutex_unlock(&NEAR_LIST_LOCK)) != 0) {
    fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
   }
   return -1;
  }
  if(send_packet_tcp(iterator->socksd, &ping_packet) < 0){
   fprintf(stderr, "tcp_ping error - send_packet_tcp failed \n");
   if ((rc = pthread_mutex_unlock(&iterator->mutex)) != 0) {
    fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
   }
   if ((rc = pthread_mutex_unlock(&NEAR_LIST_LOCK)) != 0) {
    fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
   }
   return -1;
  }
  if ((rc = pthread_mutex_unlock(&iterator->mutex)) != 0) {
   if ((rc = pthread_mutex_unlock(&NEAR_LIST_LOCK)) != 0) {
    fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
   }
   fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
   return -1;
  }
  iterator = iterator->next;
 }
 if ((rc = pthread_mutex_unlock(&NEAR_LIST_LOCK)) != 0) {
  fprintf(stderr, "tcp_ping error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

variable_types: struct near_node { int socksd; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; char data[36]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; short near_number; struct near_node *next; struct near_node *prev; } *

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

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


decompiled_code:
void __fastcall __noreturn pinger_func(__int64 a1)
{
char *v2; // rax
int v3; // eax
char *v4; // rax
char *v5; // rax
int v6; // eax
int v7; // eax
__int64 v8; // rsi
__int64 v9; // rax
int v10; // edi
int v11; // eax
int v12; // eax
char *v13; // rax
char *v14; // rax
int v15; // edi
char *v16; // rax
struct timespec v17; // [rsp+0h] [rbp-438h] BYREF
char v18[8]; // [rsp+10h] [rbp-428h] BYREF
__int16 v19; // [rsp+18h] [rbp-420h]
char v20[1054]; // [rsp+1Ah] [rbp-41Eh] BYREF

v17.tv_sec = 2LL;
v17.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v17, 0LL);
v6 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v6 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v3 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v3 )
{
v4 = strerror(v3);
fprintf(stderr, "pinger_func error - can't release lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
v7 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v7 )
{
v5 = strerror(v7);
fprintf(stderr, "pinger_func error - can't release lock: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v8 = (unsigned __int16)get_index(a1 + 8);
new_ping_packet(v18, v8);
if ( is_sp )
{
v20[0] = max_tcp_sock - nsock;
v19 = 1;
}
else
{
v9 = get_peer_rate(v18, v8);
ltob(v20, v9);
v19 = 4;
}
if ( (int)mutex_send(*(unsigned int *)(a1 + 56), a1 + 60, v18) < 0 )
fwrite("pinger_func error - can't send ping\n", 1uLL, 0x24uLL, stderr);
if ( is_sp )
{
if ( tcp_ping() < 0 )
{
fwrite("pinger_func error - tcp_ping failed\n", 1uLL, 0x24uLL, stderr);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
if ( have_child )
{
v10 = pthread_mutex_lock(&CHILD_INFO_LOCK);
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "pinger_func error - can't acquire child lock: %s\n", v13);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
if ( child_miss_pong > 2 )
{
puts(s);
have_child = 0;
child_miss_pong = 0;
v15 = pthread_mutex_unlock(&CHILD_INFO_LOCK);
if ( v15 )
{
v16 = strerror(v15);
fprintf(stderr, "pinger_func error - can't release child lock: %s\n", v16);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
else
{
++child_miss_pong;
v11 = pthread_mutex_unlock(&CHILD_INFO_LOCK);
if ( v11 )
{
v14 = strerror(v11);
fprintf(stderr, "pinger_func error - can't release child lock: %s\n", v14);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v12 = (unsigned __int16)get_index(0LL);
new_ping_packet(v18, (unsigned __int16)v12);
if ( (int)mutex_send(*(unsigned int *)(a1 + 56), &child_addr, v18) < 0 )
fwrite("pinger_func error - can't send ping to child\n", 1uLL, 0x2DuLL, stderr);
}
}
}
}
v2 = strerror(v6);
fprintf(stderr, "pinger_func error - can't acquire lock: %s\n", v2);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(__int64 a1)
{
char *v2; // rax
int v3; // eax
char *v4; // rax
char *v5; // rax
int v6; // eax
int v7; // eax
__int64 v8; // rsi
__int64 v9; // rax
int v10; // edi
int v11; // eax
int v12; // eax
char *v13; // rax
char *v14; // rax
int v15; // edi
char *v16; // rax
struct timespec v17; // [rsp+0h] [rbp-438h] BYREF
char v18[8]; // [rsp+10h] [rbp-428h] BYREF
__int16 v19; // [rsp+18h] [rbp-420h]
char v20[1054]; // [rsp+1Ah] [rbp-41Eh] BYREF

v17.tv_sec = 2LL;
v17.tv_nsec = 0LL;
while ( 1 )
{
nanosleep(&v17, 0LL);
v6 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v6 )
break;
if ( !*(_DWORD *)(a1 + 48) )
{
v3 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v3 )
{
v4 = strerror(v3);
fprintf(stderr, "pinger_func error - can't release lock: %s\n", v4);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
pthread_exit(0LL);
}
v7 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v7 )
{
v5 = strerror(v7);
fprintf(stderr, "pinger_func error - can't release lock: %s\n", v5);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v8 = (unsigned __int16)get_index(a1 + 8);
new_ping_packet(v18, v8);
if ( is_sp )
{
v20[0] = max_tcp_sock - nsock;
v19 = 1;
}
else
{
v9 = get_peer_rate(v18, v8);
ltob(v20, v9);
v19 = 4;
}
if ( (int)mutex_send(*(unsigned int *)(a1 + 56), a1 + 60, v18) < 0 )
fwrite("pinger_func error - can't send ping\n", 1uLL, 0x24uLL, stderr);
if ( is_sp )
{
if ( func2() < 0 )
{
fwrite("pinger_func error - tcp_ping failed\n", 1uLL, 0x24uLL, stderr);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
if ( have_child )
{
v10 = pthread_mutex_lock(&CHILD_INFO_LOCK);
if ( v10 )
{
v13 = strerror(v10);
fprintf(stderr, "pinger_func error - can't acquire child lock: %s\n", v13);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
if ( child_miss_pong > 2 )
{
puts(s);
have_child = 0;
child_miss_pong = 0;
v15 = pthread_mutex_unlock(&CHILD_INFO_LOCK);
if ( v15 )
{
v16 = strerror(v15);
fprintf(stderr, "pinger_func error - can't release child lock: %s\n", v16);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
}
else
{
++child_miss_pong;
v11 = pthread_mutex_unlock(&CHILD_INFO_LOCK);
if ( v11 )
{
v14 = strerror(v11);
fprintf(stderr, "pinger_func error - can't release child lock: %s\n", v14);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}
v12 = (unsigned __int16)get_index(0LL);
new_ping_packet(v18, (unsigned __int16)v12);
if ( (int)mutex_send(*(unsigned int *)(a1 + 56), &child_addr, v18) < 0 )
fwrite("pinger_func error - can't send ping to child\n", 1uLL, 0x2DuLL, stderr);
}
}
}
}
v2 = strerror(v6);
fprintf(stderr, "pinger_func error - can't acquire lock: %s\n", v2);
pthread_exit((void *)0xFFFFFFFFFFFFFFFFLL);
}

original_code:
void pinger_func(void *args) {
 struct pinger_info *pinfo = (struct pinger_info *)args;
 struct packet ping_packet;
 struct timespec sleep_time;
 int rc;
 sleep_time.tv_sec = 2;
 sleep_time.tv_nsec = 0;
 while (1) {
  nanosleep(&sleep_time, ((void *)0));
  if ((rc = pthread_mutex_lock(&pinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "pinger_func error - can't acquire lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  if (pinfo->thinfo.go == 0) {
   if ((rc = pthread_mutex_unlock(&pinfo->thinfo.mutex)) != 0) {
    fprintf(stderr, "pinger_func error - can't release lock: %s\n", strerror(rc));
    pthread_exit((void *)-1);
   }
   pthread_exit((void *)0);
  }
  if ((rc = pthread_mutex_unlock(&pinfo->thinfo.mutex)) != 0) {
   fprintf(stderr, "pinger_func error - can't release lock: %s\n", strerror(rc));
   pthread_exit((void *)-1);
  }
  new_ping_packet(&ping_packet, get_index());
  if (is_sp) {
   *ping_packet.data = (char)(max_tcp_sock - nsock);
   ping_packet.data_len = 1;
  }
  else{
   ltob(ping_packet.data, get_peer_rate());
   ping_packet.data_len = 4;
  }
  if (mutex_send(pinfo->socksd, &pinfo->addr_to_ping, &ping_packet) < 0) {
   fprintf(stderr, "pinger_func error - can't send ping\n");
  }
  if (is_sp){
   if (tcp_ping() < 0) {
    fprintf(stderr, "pinger_func error - tcp_ping failed\n");
    pthread_exit((void *)-1);
   }
   if(have_child){
    if ((rc = pthread_mutex_lock(&CHILD_INFO_LOCK)) != 0) {
     fprintf(stderr, "pinger_func error - can't acquire child lock: %s\n", strerror(rc));
     pthread_exit((void *)-1);
    }
    if(child_miss_pong < 3){
     child_miss_pong++;
     if ((rc = pthread_mutex_unlock(&CHILD_INFO_LOCK)) != 0) {
      fprintf(stderr, "pinger_func error - can't release child lock: %s\n", strerror(rc));
      pthread_exit((void *)-1);
     }
     new_ping_packet(&ping_packet, get_index());
     if (mutex_send(pinfo->socksd, &child_addr, &ping_packet) < 0) {
      fprintf(stderr, "pinger_func error - can't send ping to child\n");
     }
    }
    else{
     printf("mio FIGLIO è caduto\n");
     have_child = 0;
     child_miss_pong=0;
     if ((rc = pthread_mutex_unlock(&CHILD_INFO_LOCK)) != 0) {
      fprintf(stderr, "pinger_func error - can't release child lock: %s\n", strerror(rc));
      pthread_exit((void *)-1);
     }
    }
   }
  }
 }
}

variable_types:
args
void *

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

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 get_index()
{
return (unsigned __int16)start_index++;
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned __int16)start_index++;
}

original_code:
unsigned short get_index() {
 unsigned short ret = start_index;
 start_index ++;
 return ret;
}

variable_types:
ret
unsigned short

return_type: unsigned short


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

result = get_rand();
start_index = result;
return result;
}

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

result = get_rand();
start_index = result;
return result;
}

original_code:
void init_index() {
 start_index = get_rand();
}


decompiled_code:
__int64 __fastcall get_pcklen(__int64 a1)
{
return (unsigned int)*(unsigned __int16 *)(a1 + 8) + 10;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
return (unsigned int)*(unsigned __int16 *)(a1 + 8) + 10;
}

original_code:
int get_pcklen(const struct packet *input_pck) {
 return 3 + sizeof(input_pck->index) + sizeof(input_pck->flag) + sizeof(input_pck->TTL) + sizeof(input_pck->data_len) + input_pck->data_len;
}

canonical_original_code:
int get_pcklen(struct packet *input_pck) {
 return 3 + sizeof(input_pck->index) + sizeof(input_pck->flag) + sizeof(input_pck->TTL) + sizeof(input_pck->data_len) + input_pck->data_len;
}


original_code:
void pck_to_b(char *str, const struct packet *input) {
 int offset = 3;
 memcpy(str, input->cmd, 3);
 str[offset] = input->flag;
 offset ++;
 stob(str + offset, input->index);
 offset += sizeof(input->index);
 stob(str + offset, input->TTL);
 offset += sizeof(input->TTL);
 stob(str + offset, input->data_len);
 offset += sizeof(input->data_len);
 memcpy(str + offset, input->data, input->data_len);
}

canonical_original_code:
void pck_to_b(char *str, struct packet *input) {
 int offset = 3;
 memcpy(str, input->cmd, 3);
 str[offset] = input->flag;
 offset ++;
 stob(str + offset, input->index);
 offset += sizeof(input->index);
 stob(str + offset, input->TTL);
 offset += sizeof(input->TTL);
 stob(str + offset, input->data_len);
 offset += sizeof(input->data_len);
 memcpy(str + offset, input->data, input->data_len);
}

variable_types:
str
char *

variable_types: offset int


original_code:
void b_to_header(const char *input, struct packet *output) {
 int offset = 3;
 memcpy(output->cmd, input, 3);
 output->flag = input[offset];
 offset ++;
 output->index = btos(input + offset);
 offset += sizeof(output->index);
 output->TTL = btos(input + offset);
 offset += sizeof(output->TTL);
 output->data_len = btos(input + offset);
 offset += sizeof(output->data_len);
}

canonical_original_code:
void b_to_header(char *input, struct packet *output) {
 int offset = 3;
 memcpy(output->cmd, input, 3);
 output->flag = input[offset];
 offset ++;
 output->index = btos(input + offset);
 offset += sizeof(output->index);
 output->TTL = btos(input + offset);
 offset += sizeof(output->TTL);
 output->data_len = btos(input + offset);
 offset += sizeof(output->data_len);
}

variable_types:
input
char *


original_code:
void b_to_pck(const char *input, struct packet *output) {
 int offset = 3;
 memcpy(output->cmd, input, 3);
 output->flag = input[offset];
 offset ++;
 output->index = btos(input + offset);
 offset += sizeof(output->index);
 output->TTL = btos(input + offset);
 offset += sizeof(output->TTL);
 output->data_len = btos(input + offset);
 offset += sizeof(output->data_len);
 memcpy(output->data, input + offset, output->data_len);
 output->data[output->data_len] = 0;
}

canonical_original_code:
void b_to_pck(char *input, struct packet *output) {
 int offset = 3;
 memcpy(output->cmd, input, 3);
 output->flag = input[offset];
 offset ++;
 output->index = btos(input + offset);
 offset += sizeof(output->index);
 output->TTL = btos(input + offset);
 offset += sizeof(output->TTL);
 output->data_len = btos(input + offset);
 offset += sizeof(output->data_len);
 memcpy(output->data, input + offset, output->data_len);
 output->data[output->data_len] = 0;
}

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


decompiled_code:
__int64 __fastcall recvfrom_packet(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
unsigned int v6; // ebx
_BYTE v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v5 = udp_recvfrom(a1, a2, a4, v8, 1034LL);
if ( v5 < 0 )
return (unsigned int)-1;
v6 = v5;
if ( v5 )
b_to_pck((__int64)v8, a3);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
unsigned int v6; // ebx
_BYTE v8[1064]; // [rsp+0h] [rbp-428h] BYREF

v5 = udp_recvfrom(a1, a2, a4, v8, 1034LL);
if ( v5 < 0 )
return (unsigned int)-1;
v6 = v5;
if ( v5 )
func16((__int64)v8, a3);
return v6;
}

original_code:
int recvfrom_packet(int socksd, struct sockaddr_in *addr, struct packet *pck, int *len) {
 int ret;
 char buf[3 + 7 + 1024];
 if ((ret = udp_recvfrom(socksd, addr, (socklen_t *)len, buf, 3 + 7 + 1024)) < 0) {
  return -1;
 } else if (ret == 0) {
  return 0;
 }
 b_to_pck(buf, pck);
 return ret;
}

canonical_original_code:
int recvfrom_packet(int socksd, struct sockaddr_in *addr, struct packet *pck, int *len) {
 int ret;
 char buf[-1];
 if ((ret = udp_recvfrom(socksd, addr, (unsigned int *)len, buf, 3 + 7 + 1024)) < 0) {
  return -1;
 } else if (ret == 0) {
  return 0;
 }
 b_to_pck(buf, pck);
 return ret;
}

variable_types: socksd 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:
len
int *

variable_types: ret int

variable_types:
buf
char [-1]


decompiled_code:
__int64 __fastcall recv_packet(__int64 a1, __int64 a2)
{
return recvfrom_packet(a1, 0LL, a2, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, __int64 a2)
{
return func17(a1, 0LL, a2, 0LL);
}

original_code:
int recv_packet(int socksd, struct packet *pck) {
 return recvfrom_packet(socksd, ((void *)0), pck, ((void *)0));
}


decompiled_code:
__int64 __fastcall recv_packet_tcp(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // edx
unsigned __int16 v5; // dx
int v6; // eax
char v7[34]; // [rsp+6h] [rbp-22h] BYREF

v2 = readn(a1, v7, 10LL);
if ( v2 < 0 )
goto LABEL_4;
v3 = 0;
if ( v2 <= 0 )
{
b_to_header((__int64)v7, a2);
v5 = *(_WORD *)(a2 + 8);
if ( !v5 )
return (unsigned int)*(unsigned __int16 *)(a2 + 8) + 10;
v6 = readn((unsigned int)a1, a2 + 10, v5);
if ( v6 >= 0 )
{
v3 = 0;
if ( v6 > 0 )
return v3;
return (unsigned int)*(unsigned __int16 *)(a2 + 8) + 10;
}
LABEL_4:
perror("recv_packet_tcp error - read failed");
v3 = -1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, __int64 a2)
{
int v2; // eax
unsigned int v3; // edx
unsigned __int16 v5; // dx
int v6; // eax
char v7[34]; // [rsp+6h] [rbp-22h] BYREF

v2 = readn(a1, v7, 10LL);
if ( v2 < 0 )
goto LABEL_4;
v3 = 0;
if ( v2 <= 0 )
{
func15((__int64)v7, a2);
v5 = *(_WORD *)(a2 + 8);
if ( !v5 )
return (unsigned int)*(unsigned __int16 *)(a2 + 8) + 10;
v6 = readn((unsigned int)a1, a2 + 10, v5);
if ( v6 >= 0 )
{
v3 = 0;
if ( v6 > 0 )
return v3;
return (unsigned int)*(unsigned __int16 *)(a2 + 8) + 10;
}
LABEL_4:
perror("recv_packet_tcp error - read failed");
v3 = -1;
}
return v3;
}

original_code:
int recv_packet_tcp(int socksd, struct packet *pck) {
 int ret;
 char buf[3 + 7];
 if ((ret = readn(socksd, buf, 3 + 7)) < 0) {
  perror("recv_packet_tcp error - read failed");
  return -1;
 } else if (ret > 0) {
  return 0;
 }
 b_to_header(buf, pck);
 if (pck->data_len > 0) {
  if ((ret = readn(socksd, pck->data, pck->data_len)) < 0) {
   perror("recv_packet_tcp error - read failed");
   return -1;
  } else if (ret > 0) {
   return 0;
  }
 }
 return pck->data_len + 3 + 7;
}

canonical_original_code:
int recv_packet_tcp(int socksd, struct packet *pck) {
 int ret;
 char buf[-1];
 if ((ret = readn(socksd, buf, 3 + 7)) < 0) {
  perror("recv_packet_tcp error - read failed");
  return -1;
 } else if (ret > 0) {
  return 0;
 }
 b_to_header(buf, pck);
 if (pck->data_len > 0) {
  if ((ret = readn(socksd, pck->data, pck->data_len)) < 0) {
   perror("recv_packet_tcp error - read failed");
   return -1;
  } else if (ret > 0) {
   return 0;
  }
 }
 return pck->data_len + 3 + 7;
}


decompiled_code:
__int64 __fastcall send_packet(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v4; // eax
__int64 result; // rax
_BYTE v6[1064]; // [rsp+0h] [rbp-428h] BYREF

pck_to_b((__int64)v6, a3);
v4 = get_pcklen(a3);
result = udp_send(a1, a2, v6, v4);
if ( (int)result < 0 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v4; // eax
__int64 result; // rax
_BYTE v6[1064]; // [rsp+0h] [rbp-428h] BYREF

func14((__int64)v6, a3);
v4 = func13(a3);
result = udp_send(a1, a2, v6, v4);
if ( (int)result < 0 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int send_packet(int socksd, const struct sockaddr_in *addr, const struct packet *pck) {
 int ret;
 char buf[3 + 7 + 1024];
 pck_to_b(buf, pck);
 if ((ret = udp_send(socksd, addr, buf, get_pcklen(pck))) < 0) {
  return -1;
 }
 return ret;
}

canonical_original_code:
int send_packet(int socksd, struct sockaddr_in *addr, struct packet *pck) {
 int ret;
 char buf[-1];
 pck_to_b(buf, pck);
 if ((ret = udp_send(socksd, addr, buf, get_pcklen(pck))) < 0) {
  return -1;
 }
 return ret;
}


decompiled_code:
__int64 __fastcall send_packet_tcp(unsigned int a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax
_BYTE v4[1064]; // [rsp+0h] [rbp-428h] BYREF

pck_to_b((__int64)v4, a2);
v2 = get_pcklen(a2);
result = writen(a1, v4, v2);
if ( (int)result < 0 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func21(unsigned int a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax
_BYTE v4[1064]; // [rsp+0h] [rbp-428h] BYREF

func14((__int64)v4, a2);
v2 = func13(a2);
result = writen(a1, v4, v2);
if ( (int)result < 0 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int send_packet_tcp(int socksd, const struct packet *pck) {
 int ret;
 char buf[3 + 7 + 1024];
 pck_to_b(buf, pck);
 if ((ret = writen(socksd, buf, get_pcklen(pck))) < 0) {
  return -1;
 }
 return ret;
}

canonical_original_code:
int send_packet_tcp(int socksd, struct packet *pck) {
 int ret;
 char buf[-1];
 pck_to_b(buf, pck);
 if ((ret = writen(socksd, buf, get_pcklen(pck))) < 0) {
  return -1;
 }
 return ret;
}


pckcpy

decompiled_code:
void *__fastcall pckcpy(__int64 a1, __int64 a2)
{
void *result; // rax
unsigned __int16 v3; // dx

strncpy((char *)a1, (const char *)a2, 3uLL);
*(_WORD *)(a1 + 4) = *(_WORD *)(a2 + 4);
*(_BYTE *)(a1 + 3) = *(_BYTE *)(a2 + 3);
result = (void *)*(unsigned __int16 *)(a2 + 6);
*(_WORD *)(a1 + 6) = (_WORD)result;
v3 = *(_WORD *)(a2 + 8);
*(_WORD *)(a1 + 8) = v3;
if ( v3 )
result = memcpy((void *)(a1 + 10), (const void *)(a2 + 10), v3);
return result;
}

canonical_decompiled_code:
void *__fastcall func22(__int64 a1, __int64 a2)
{
void *result; // rax
unsigned __int16 v3; // dx

strncpy((char *)a1, (const char *)a2, 3uLL);
*(_WORD *)(a1 + 4) = *(_WORD *)(a2 + 4);
*(_BYTE *)(a1 + 3) = *(_BYTE *)(a2 + 3);
result = (void *)*(unsigned __int16 *)(a2 + 6);
*(_WORD *)(a1 + 6) = (_WORD)result;
v3 = *(_WORD *)(a2 + 8);
*(_WORD *)(a1 + 8) = v3;
if ( v3 )
result = memcpy((void *)(a1 + 10), (const void *)(a2 + 10), v3);
return result;
}

original_code:
void pckcpy(struct packet *dest, const struct packet *src) {
 strncpy(dest->cmd, src->cmd, 3);
 dest->index = src->index;
 dest->flag = src->flag;
 dest->TTL = src->TTL;
 dest->data_len = src->data_len;
 if (dest->data_len > 0) {
  memcpy(dest->data, src->data, dest->data_len);
 }
}

canonical_original_code:
void pckcpy(struct packet *dest, struct packet *src) {
 strncpy(dest->cmd, src->cmd, 3);
 dest->index = src->index;
 dest->flag = src->flag;
 dest->TTL = src->TTL;
 dest->data_len = src->data_len;
 if (dest->data_len > 0) {
  memcpy(dest->data, src->data, dest->data_len);
 }
}

variable_types:
src
struct packet *


decompiled_code:
void *__fastcall add_near_to_packet(__int64 a1, const void *a2, int a3)
{
__int16 v3; // bp
void *result; // rax

v3 = a3;
result = memcpy((void *)(a1 + 10), a2, a3);
*(_WORD *)(a1 + 8) = v3;
return result;
}

canonical_decompiled_code:
void *__fastcall func23(__int64 a1, const void *a2, int a3)
{
__int16 v3; // bp
void *result; // rax

v3 = a3;
result = memcpy((void *)(a1 + 10), a2, a3);
*(_WORD *)(a1 + 8) = v3;
return result;
}

original_code:
void add_near_to_packet(struct packet *pck, const char *data, int data_len ){
 memcpy(pck->data,data,data_len);
 pck->data_len = data_len;
}

canonical_original_code:
void add_near_to_packet(struct packet *pck, char *data, int data_len){
 memcpy(pck->data,data,data_len);
 pck->data_len = data_len;
}

variable_types:
data
char *


decompiled_code:
__int64 __fastcall is_set_flag(__int64 a1, char a2)
{
return (unsigned int)(char)(*(_BYTE *)(a1 + 3) & a2);
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1, char a2)
{
return (unsigned int)(char)(*(_BYTE *)(a1 + 3) & a2);
}

original_code:
int is_set_flag(const struct packet *pck, char flag) {
 return pck->flag & flag;
}

canonical_original_code:
int is_set_flag(struct packet *pck, char flag) {
 return pck->flag & flag;
}

variable_types: flag char


decompiled_code:
void __fastcall set_flag(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 3) |= a2;
}

canonical_decompiled_code:
void __fastcall func25(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 3) |= a2;
}

original_code:
void set_flag(struct packet *pck, char flag) {
 pck->flag |= flag;
}


decompiled_code:
void __fastcall set_whohas_name_flag(__int64 a1)
{
set_flag(a1, 1);
}

canonical_decompiled_code:
void __fastcall func26(__int64 a1)
{
func25(a1, 1);
}

original_code:
void set_whohas_name_flag(struct packet *pck) {
 set_flag(pck, 1);
}


decompiled_code:
void __fastcall new_whs_res_packet(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
new_packet(a1, "WHO", a2, a3, a4, a5);
set_whohas_name_flag(a1);
}

canonical_decompiled_code:
void __fastcall func27(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
func2(a1, "WHO", a2, a3, a4, a5);
func26(a1);
}

original_code:
void new_whs_res_packet(struct packet *pck, unsigned short index, const char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_name_flag(pck);
}

canonical_original_code:
void new_whs_res_packet(struct packet *pck, unsigned short index, char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_name_flag(pck);
}

variable_types:
index
unsigned short

variable_types: ttl short


decompiled_code:
void __fastcall set_whohas_md5_flag(__int64 a1)
{
set_flag(a1, 2);
}

canonical_decompiled_code:
void __fastcall func28(__int64 a1)
{
func25(a1, 2);
}

original_code:
void set_whohas_md5_flag(struct packet *pck) {
 set_flag(pck, 2);
}


decompiled_code:
void __fastcall new_whs_res5_packet(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
new_packet(a1, "WHO", a2, a3, a4, a5);
set_whohas_md5_flag(a1);
}

canonical_decompiled_code:
void __fastcall func29(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
func2(a1, "WHO", a2, a3, a4, a5);
func28(a1);
}

original_code:
void new_whs_res5_packet(struct packet *pck, unsigned short index, const char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_md5_flag(pck);
}

canonical_original_code:
void new_whs_res5_packet(struct packet *pck, unsigned short index, char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_md5_flag(pck);
}


decompiled_code:
void __fastcall set_query_flag(__int64 a1)
{
set_flag(a1, 8);
}

canonical_decompiled_code:
void __fastcall func30(__int64 a1)
{
func25(a1, 8);
}

original_code:
void set_query_flag(struct packet *pck) {
 set_flag(pck, 8);
}


decompiled_code:
void __fastcall new_whs_query_packet(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
new_packet(a1, "WHO", a2, a3, a4, a5);
set_whohas_name_flag(a1);
set_query_flag(a1);
}

canonical_decompiled_code:
void __fastcall func31(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
func2(a1, "WHO", a2, a3, a4, a5);
func26(a1);
func30(a1);
}

original_code:
void new_whs_query_packet(struct packet *pck, unsigned short index, const char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_name_flag(pck);
 set_query_flag(pck);
}

canonical_original_code:
void new_whs_query_packet(struct packet *pck, unsigned short index, char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_name_flag(pck);
 set_query_flag(pck);
}


decompiled_code:
void __fastcall new_whs_query5_packet(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
new_packet(a1, "WHO", a2, a3, a4, a5);
set_whohas_md5_flag(a1);
set_query_flag(a1);
}

canonical_decompiled_code:
void __fastcall func32(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
func2(a1, "WHO", a2, a3, a4, a5);
func28(a1);
func30(a1);
}

original_code:
void new_whs_query5_packet(struct packet *pck, unsigned short index, const char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_md5_flag(pck);
 set_query_flag(pck);
}

canonical_original_code:
void new_whs_query5_packet(struct packet *pck, unsigned short index, char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "WHO", index, data, data_len, ttl);
 set_whohas_md5_flag(pck);
 set_query_flag(pck);
}


decompiled_code:
void __fastcall new_get_packet(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
new_packet(a1, "GET", a2, a3, a4, a5);
set_query_flag(a1);
}

canonical_decompiled_code:
void __fastcall func33(__int64 a1, __int16 a2, const void *a3, unsigned __int16 a4, __int16 a5)
{
func2(a1, "GET", a2, a3, a4, a5);
func30(a1);
}

original_code:
void new_get_packet(struct packet *pck, unsigned short index, const char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "GET", index, data, data_len, ttl);
 set_query_flag(pck);
}

canonical_original_code:
void new_get_packet(struct packet *pck, unsigned short index, char *data, unsigned short data_len, short ttl) {
 new_packet(pck, "GET", index, data, data_len, ttl);
 set_query_flag(pck);
}


decompiled_code:
void __fastcall set_nextchunk_flag(__int64 a1)
{
set_flag(a1, 32);
}

canonical_decompiled_code:
void __fastcall func34(__int64 a1)
{
func25(a1, 32);
}

original_code:
void set_nextchunk_flag(struct packet *pck) {
 set_flag(pck, 32);
}


decompiled_code:
void __fastcall set_superpeer_flag(__int64 a1)
{
set_flag(a1, -128);
}

canonical_decompiled_code:
void __fastcall func35(__int64 a1)
{
func25(a1, -128);
}

original_code:
void set_superpeer_flag(struct packet *pck) {
 set_flag(pck, 128);
}


decompiled_code:
void __fastcall unset_flag(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 3) &= ~a2;
}

canonical_decompiled_code:
void __fastcall func36(__int64 a1, char a2)
{
*(_BYTE *)(a1 + 3) &= ~a2;
}

original_code:
void unset_flag(struct packet *pck, char flag) {
 pck->flag &= ~flag;
}


decompiled_code:
void __fastcall unset_whohas_name_flag(__int64 a1)
{
unset_flag(a1, 1);
}

canonical_decompiled_code:
void __fastcall func37(__int64 a1)
{
func36(a1, 1);
}

original_code:
void unset_whohas_name_flag(struct packet *pck) {
 unset_flag(pck, 1);
}


decompiled_code:
void __fastcall unset_whohas_md5_flag(__int64 a1)
{
unset_flag(a1, 2);
}

canonical_decompiled_code:
void __fastcall func38(__int64 a1)
{
func36(a1, 2);
}

original_code:
void unset_whohas_md5_flag(struct packet *pck) {
 unset_flag(pck, 2);
}


decompiled_code:
void __fastcall unset_query_flag(__int64 a1)
{
unset_flag(a1, 8);
}

canonical_decompiled_code:
void __fastcall func39(__int64 a1)
{
func36(a1, 8);
}

original_code:
void unset_query_flag(struct packet *pck) {
 unset_flag(pck, 8);
}


decompiled_code:
void __fastcall unset_nextchunk_flag(__int64 a1)
{
unset_flag(a1, 32);
}

canonical_decompiled_code:
void __fastcall func40(__int64 a1)
{
func36(a1, 32);
}

original_code:
void unset_nextchunk_flag(struct packet *pck) {
 unset_flag(pck, 32);
}


decompiled_code:
void __fastcall unset_superpeer_flag(__int64 a1)
{
unset_flag(a1, -128);
}

canonical_decompiled_code:
void __fastcall func41(__int64 a1)
{
func36(a1, -128);
}

original_code:
void unset_superpeer_flag(struct packet *pck) {
 unset_flag(pck, 128);
}


decompiled_code:
int tcp_socket()
{
return socket(2, 1, 0);
}

canonical_decompiled_code:
int func2()
{
return socket(2, 1, 0);
}

original_code:
int tcp_socket(void) {
 return socket(2, SOCK_STREAM, 0);
}

canonical_original_code:
int tcp_socket() {
 return socket(2, SOCK_STREAM, 0);
}


decompiled_code:
int udp_socket()
{
return socket(2, 2, 0);
}

canonical_decompiled_code:
int func3()
{
return socket(2, 2, 0);
}

original_code:
int udp_socket(void) {
 return socket(2, SOCK_DGRAM, 0);
}

canonical_original_code:
int udp_socket() {
 return socket(2, SOCK_DGRAM, 0);
}


decompiled_code:
__int64 __fastcall set_addr_in(__int64 a1, const char *a2, __int16 a3)
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)a1 = 2;
*(_WORD *)(a1 + 2) = _bswap_16(a3);
return (unsigned int)(inet_pton(2, a2, (void *)(a1 + 4)) >> 31);
}

original_code:
int set_addr_in(struct sockaddr_in *sad, const char *addr, short port) {
    memset((void *)sad, 0, sizeof(struct sockaddr_in));
 sad->sin_family = 2;
 sad->sin_port = htons(port);
 if (inet_pton(2, addr, &(sad->sin_addr)) < 0) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int set_addr_in(struct sockaddr_in *sad, char *addr, short port) {
    memset((void *)sad, 0, sizeof(struct sockaddr_in));
 sad->sin_family = 2;
 sad->sin_port = htons(port);
 if (inet_pton(2, addr, &(sad->sin_addr)) < 0) {
  return -1;
 }
 return 0;
}

variable_types:
addr
char *

variable_types: port short


decompiled_code:
__int16 __fastcall set_addr_any(__int64 a1, __int16 a2)
{
__int16 result; // ax

*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)a1 = 2;
*(_DWORD *)(a1 + 4) = _bswap_32(0);
result = _bswap_16(a2);
*(_WORD *)(a1 + 2) = result;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func5(__int64 a1, __int16 a2)
{
__int16 result; // ax

*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)a1 = 2;
*(_DWORD *)(a1 + 4) = func1(0);
result = func0(a2);
*(_WORD *)(a1 + 2) = result;
return result;
}

original_code:
void set_addr_any(struct sockaddr_in *sad, short port) {
 memset((void *)sad, 0, sizeof(struct sockaddr_in));
 sad->sin_family = 2;
 sad->sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 sad->sin_port = htons(port);
}

canonical_original_code:
void set_addr_any(struct sockaddr_in *sad, short port) {
 memset((void *)sad, 0, sizeof(struct sockaddr_in));
 sad->sin_family = 2;
 sad->sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 sad->sin_port = htons(port);
}


decompiled_code:
__int64 __fastcall tcp_connect(int a1, const struct sockaddr *a2)
{
return (unsigned int)(connect(a1, a2, 0x10u) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, const struct sockaddr *a2)
{
return (unsigned int)(connect(a1, a2, 0x10u) >> 31);
}

original_code:
int tcp_connect(int socksd, const struct sockaddr_in *sad) {
 if (connect(socksd, (struct sockaddr *)sad, sizeof(struct sockaddr_in)) < 0) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int tcp_connect(int socksd, struct sockaddr_in *sad) {
 if (connect(socksd, (struct sockaddr *)sad, sizeof(struct sockaddr_in)) < 0) {
  return -1;
 }
 return 0;
}

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


decompiled_code:
int __fastcall inet_bind(int a1, const struct sockaddr *a2)
{
return bind(a1, a2, 0x10u);
}

canonical_decompiled_code:
int __fastcall func7(int a1, const struct sockaddr *a2)
{
return bind(a1, a2, 0x10u);
}

original_code:
int inet_bind(int socksd, const struct sockaddr_in *sad) {
 return bind(socksd, (struct sockaddr *)sad, sizeof(struct sockaddr_in));
}

canonical_original_code:
int inet_bind(int socksd, struct sockaddr_in *sad) {
 return bind(socksd, (struct sockaddr *)sad, sizeof(struct sockaddr_in));
}


decompiled_code:
ssize_t __fastcall udp_send(int a1, const struct sockaddr *a2, const void *a3, int a4)
{
return sendto(a1, a3, a4, 0, a2, 0x10u);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, const struct sockaddr *a2, const void *a3, int a4)
{
return sendto(a1, a3, a4, 0, a2, 0x10u);
}

original_code:
int udp_send(int socksd, const struct sockaddr_in *sad, const char *buf, int len) {
 return sendto(socksd, buf, len, 0, (struct sockaddr *)sad, sizeof(struct sockaddr_in));
}

canonical_original_code:
int udp_send(int socksd, struct sockaddr_in *sad, char *buf, int len) {
 return sendto(socksd, buf, len, 0, (struct sockaddr *)sad, sizeof(struct sockaddr_in));
}

variable_types:
buf
char *

variable_types: len int


decompiled_code:
ssize_t __fastcall udp_recvfrom(int a1, struct sockaddr *a2, socklen_t *a3, void *a4, int a5)
{
return recvfrom(a1, a4, a5, 0, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, struct sockaddr *a2, socklen_t *a3, void *a4, int a5)
{
return recvfrom(a1, a4, a5, 0, a2, a3);
}

original_code:
int udp_recvfrom(int socksd, struct sockaddr_in *sad, socklen_t *addr_len, char *buf, int maxlen) {
 return recvfrom(socksd, buf, maxlen, 0, (struct sockaddr *)sad, addr_len);
}

canonical_original_code:
int udp_recvfrom(int socksd, struct sockaddr_in *sad, unsigned int *addr_len, char *buf, int maxlen) {
 return recvfrom(socksd, buf, maxlen, 0, (struct sockaddr *)sad, addr_len);
}

variable_types: maxlen int


decompiled_code:
ssize_t __fastcall udp_recv(int a1, void *a2, int a3)
{
return recvfrom(a1, a2, a3, 0, 0LL, 0LL);
}

canonical_decompiled_code:
ssize_t __fastcall func10(int a1, void *a2, int a3)
{
return recvfrom(a1, a2, a3, 0, 0LL, 0LL);
}

original_code:
int udp_recv(int socksd, char *buf, int maxlen) {
 return recvfrom(socksd, buf, maxlen, 0, ((void *)0), ((void *)0));
}


decompiled_code:
int __fastcall close_sock(int a1)
{
int result; // eax

result = 0;
if ( a1 >= 0 )
result = close(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int result; // eax

result = 0;
if ( a1 >= 0 )
result = close(a1);
return result;
}

original_code:
int close_sock(int socksd) {
 if (socksd < 0) {
  return 0;
 } else {
  return close(socksd);
 }
}


addrcpy

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

*(_WORD *)a1 = *(_WORD *)a2;
*(_DWORD *)(a1 + 4) = *(_DWORD *)(a2 + 4);
result = *(unsigned __int16 *)(a2 + 2);
*(_WORD *)(a1 + 2) = result;
return result;
}

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

*(_WORD *)a1 = *(_WORD *)a2;
*(_DWORD *)(a1 + 4) = *(_DWORD *)(a2 + 4);
result = *(unsigned __int16 *)(a2 + 2);
*(_WORD *)(a1 + 2) = result;
return result;
}

original_code:
void addrcpy(struct sockaddr_in *dest, const struct sockaddr_in *src) {
 dest->sin_family = src->sin_family;
 dest->sin_addr.s_addr = src->sin_addr.s_addr;
 dest->sin_port = src->sin_port;
}

canonical_original_code:
void addrcpy(struct sockaddr_in *dest, struct sockaddr_in *src) {
 dest->sin_family = src->sin_family;
 dest->sin_addr.s_addr = src->sin_addr.s_addr;
 dest->sin_port = src->sin_port;
}

variable_types:
src
struct sockaddr_in *


addrcmp

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

result = 0LL;
if ( *(_DWORD *)(a1 + 4) == *(_DWORD *)(a2 + 4) )
result = *(_WORD *)(a1 + 2) == *(_WORD *)(a2 + 2);
return result;
}

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

result = 0LL;
if ( *(_DWORD *)(a1 + 4) == *(_DWORD *)(a2 + 4) )
result = *(_WORD *)(a1 + 2) == *(_WORD *)(a2 + 2);
return result;
}

original_code:
int addrcmp(const struct sockaddr_in *sad1, const struct sockaddr_in *sad2) {
 return ((sad1->sin_addr.s_addr == sad2->sin_addr.s_addr) && (sad1->sin_port == sad2->sin_port));
}

canonical_original_code:
int addrcmp(struct sockaddr_in *sad1, struct sockaddr_in *sad2) {
 return ((sad1->sin_addr.s_addr == sad2->sin_addr.s_addr) && (sad1->sin_port == sad2->sin_port));
}


decompiled_code:
__int64 __fastcall addr2str(__int64 a1, __int64 a2, __int16 a3)
{
ltob();
stob(a1 + 8, (unsigned int)a3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2, __int16 a3)
{
ltob();
stob(a1 + 8, (unsigned int)a3);
return 0LL;
}

original_code:
int addr2str(char *str, unsigned long addr, unsigned short port) {
 ltob(str, addr);
 stob(str + sizeof(addr), port);
 return 0;
}

variable_types:
str
char *

variable_types:
addr
unsigned long

variable_types:
port
unsigned short


original_code:
int str2addr(struct sockaddr_in *addr, const char *str) {
 memset((void *)addr, 0, sizeof(struct sockaddr_in));
 addr->sin_family = 2;
 addr->sin_addr.s_addr = btol(str);
 addr->sin_port = btos(str + sizeof(long));
 return 0;
}

canonical_original_code:
int str2addr(struct sockaddr_in *addr, char *str) {
 memset((void *)addr, 0, sizeof(struct sockaddr_in));
 addr->sin_family = 2;
 addr->sin_addr.s_addr = btol(str);
 addr->sin_port = btos(str + sizeof(long));
 return 0;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *


original_code:
struct sockaddr_in *str_to_addr(const char *str, int dim) {
 struct sockaddr_in *ret = (struct sockaddr_in *)malloc(dim * sizeof(struct sockaddr_in));
 int offset = sizeof(long) + sizeof(short);
 int i;
 for (i = 0; i < dim; i ++) {
  str2addr(&ret[i], str + (i * offset));
 }
 return ret;
}

canonical_original_code:
struct sockaddr_in *str_to_addr(char *str, int dim) {
 struct sockaddr_in *ret = (struct sockaddr_in *)malloc(dim * sizeof(struct sockaddr_in));
 int offset = sizeof(long) + sizeof(short);
 int i;
 for (i = 0; i < dim; i ++) {
  str2addr(&ret[i], str + (i * offset));
 }
 return ret;
}

variable_types: dim int

variable_types: offset int

variable_types: i int

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 __fastcall get_local_addr(int a1, struct sockaddr *a2)
{
socklen_t len[3]; // [rsp+Ch] [rbp-Ch] BYREF

len[0] = 16;
return getsockname(a1, a2, len);
}

canonical_decompiled_code:
int __fastcall func17(int a1, struct sockaddr *a2)
{
socklen_t len[3]; // [rsp+Ch] [rbp-Ch] BYREF

len[0] = 16;
return getsockname(a1, a2, len);
}

original_code:
int get_local_addr(int socksd, struct sockaddr_in *addr) {
 unsigned int len = sizeof(struct sockaddr_in);
 return getsockname(socksd, (struct sockaddr *)addr, &len);
}

variable_types: socksd int

variable_types:
len
unsigned int


decompiled_code:
__int16 __fastcall get_local_port(int a1)
{
bool v1; // sf
__int16 result; // ax
struct sockaddr v3; // [rsp+0h] [rbp-18h] BYREF

v1 = get_local_addr(a1, &v3) < 0;
result = -1;
if ( !v1 )
result = *(_WORD *)v3.sa_data;
return result;
}

canonical_decompiled_code:
__int16 __fastcall func18(int a1)
{
bool v1; // sf
__int16 result; // ax
struct sockaddr v3; // [rsp+0h] [rbp-18h] BYREF

v1 = func17(a1, &v3) < 0;
result = -1;
if ( !v1 )
result = *(_WORD *)v3.sa_data;
return result;
}

original_code:
unsigned short get_local_port(int socksd) {
 struct sockaddr_in addr;
 if (get_local_addr(socksd, &addr) < 0) {
  return -1;
 }
 return addr.sin_port;
}

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


decompiled_code:
__int64 __fastcall contains_addr(__int64 a1, unsigned int a2, __int64 a3)
{
unsigned int v4; // ebx
signed int v5; // edi
const void *v6; // r13
const void *v7; // r14

if ( !a2 )
return 0LL;
v4 = 6;
v5 = 0;
v6 = (const void *)(a3 + 4);
v7 = (const void *)(a3 + 2);
while ( 1 )
{
if ( memcmp((const void *)(a1 + v5), v6, 4uLL) )
{
v5 = v4;
goto LABEL_4;
}
if ( !memcmp((const void *)(a1 + (int)(v4 - 2)), v7, 2uLL) )
return 1LL;
v5 = v4;
LABEL_4:
if ( v4 >= a2 )
return 0LL;
v4 += 6;
}
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, unsigned int a2, __int64 a3)
{
unsigned int v4; // ebx
signed int v5; // edi
const void *v6; // r13
const void *v7; // r14

if ( !a2 )
return 0LL;
v4 = 6;
v5 = 0;
v6 = (const void *)(a3 + 4);
v7 = (const void *)(a3 + 2);
while ( 1 )
{
if ( memcmp((const void *)(a1 + v5), v6, 4uLL) )
{
v5 = v4;
goto LABEL_4;
}
if ( !memcmp((const void *)(a1 + (int)(v4 - 2)), v7, 2uLL) )
return 1LL;
v5 = v4;
LABEL_4:
if ( v4 >= a2 )
return 0LL;
v4 += 6;
}
}

original_code:
int contains_addr(const char *str, unsigned int str_len, const struct sockaddr_in *addr) {
 int offset = 0;
 while (offset < str_len) {
  if (!memcmp(str + offset, (char *)&addr->sin_addr.s_addr, sizeof(addr->sin_addr.s_addr))) {
   offset += sizeof(addr->sin_addr.s_addr);
   if (!memcmp(str + offset, (char *)&addr->sin_port, sizeof(addr->sin_port))) {
    return 1;
   }
   offset += sizeof(addr->sin_port);
  } else {
   offset += sizeof(addr->sin_addr.s_addr) + sizeof(addr->sin_port);
  }
 }
 return 0;
}

canonical_original_code:
int contains_addr(char *str, unsigned int str_len, struct sockaddr_in *addr) {
 int offset = 0;
 while (offset < str_len) {
  if (!memcmp(str + offset, (char *)&addr->sin_addr.s_addr, sizeof(addr->sin_addr.s_addr))) {
   offset += sizeof(addr->sin_addr.s_addr);
   if (!memcmp(str + offset, (char *)&addr->sin_port, sizeof(addr->sin_port))) {
    return 1;
   }
   offset += sizeof(addr->sin_port);
  } else {
   offset += sizeof(addr->sin_addr.s_addr) + sizeof(addr->sin_port);
  }
 }
 return 0;
}


decompiled_code:
int __fastcall write_err(int fd, pthread_mutex_t *mutex, const char *a3)
{
int v3; // eax
size_t v4; // rax
int result; // eax
char *v6; // rax
int v7; // eax
char *v8; // rax
char *v9; // rax
char v10[56]; // [rsp+0h] [rbp-38h] BYREF

sprintf(v10, "ERR %s\n", a3);
v3 = pthread_mutex_lock(mutex);
if ( v3 )
{
v6 = strerror(v3);
fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", v6);
result = -1;
}
else
{
v4 = strlen(v10);
if ( write(fd, v10, v4) < 0 )
{
v7 = pthread_mutex_unlock(mutex);
if ( v7 )
{
v8 = strerror(v7);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v8);
}
else
{
perror("write_err error - can't write on pipe");
}
result = -1;
}
else
{
result = pthread_mutex_unlock(mutex);
if ( result )
{
v9 = strerror(result);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v9);
result = -1;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int fd, pthread_mutex_t *mutex, const char *a3)
{
int v3; // eax
size_t v4; // rax
int result; // eax
char *v6; // rax
int v7; // eax
char *v8; // rax
char *v9; // rax
char v10[56]; // [rsp+0h] [rbp-38h] BYREF

sprintf(v10, "ERR %s\n", a3);
v3 = pthread_mutex_lock(mutex);
if ( v3 )
{
v6 = strerror(v3);
fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", v6);
result = -1;
}
else
{
v4 = strlen(v10);
if ( write(fd, v10, v4) < 0 )
{
v7 = pthread_mutex_unlock(mutex);
if ( v7 )
{
v8 = strerror(v7);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v8);
}
else
{
perror("write_err error - can't write on pipe");
}
result = -1;
}
else
{
result = pthread_mutex_unlock(mutex);
if ( result )
{
v9 = strerror(result);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v9);
result = -1;
}
}
}
return result;
}

original_code:
int write_err(int th_pipe, pthread_mutex_t *pipe_mutex, const char *msg) {
 int rc;
 char str[32];
 sprintf(str, "ERR %s\n", msg);
 if ((rc = pthread_mutex_lock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 if (write(th_pipe, str, strlen(str)) < 0) {
  if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
   fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
   return -1;
  }
  perror("write_err error - can't write on pipe");
  return -1;
 }
 if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int write_err(int th_pipe, union pthread_mutex_t *pipe_mutex, char *msg) {
 int rc;
 char str[32];
 sprintf(str, "ERR %s\n", msg);
 if ((rc = pthread_mutex_lock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 if (write(th_pipe, str, strlen(str)) < 0) {
  if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
   fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
   return -1;
  }
  perror("write_err error - can't write on pipe");
  return -1;
 }
 if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

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

variable_types:
msg
char *

variable_types: rc int

variable_types:
str
char [32]


decompiled_code:
int __fastcall write_on_pipe(int fd, pthread_mutex_t *mutex, void *buf)
{
int v4; // eax
size_t v5; // rax
int result; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
char *v10; // rax

v4 = pthread_mutex_lock(mutex);
if ( v4 )
{
v7 = strerror(v4);
fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", v7);
result = -1;
}
else
{
v5 = strlen((const char *)buf);
if ( write(fd, buf, v5) < 0 )
{
v8 = pthread_mutex_unlock(mutex);
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v9);
}
else
{
perror("write_err error - can't write on pipe");
}
result = -1;
}
else
{
result = pthread_mutex_unlock(mutex);
if ( result )
{
v10 = strerror(result);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v10);
result = -1;
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int fd, pthread_mutex_t *mutex, void *buf)
{
int v4; // eax
size_t v5; // rax
int result; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
char *v10; // rax

v4 = pthread_mutex_lock(mutex);
if ( v4 )
{
v7 = strerror(v4);
fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", v7);
result = -1;
}
else
{
v5 = strlen((const char *)buf);
if ( write(fd, buf, v5) < 0 )
{
v8 = pthread_mutex_unlock(mutex);
if ( v8 )
{
v9 = strerror(v8);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v9);
}
else
{
perror("write_err error - can't write on pipe");
}
result = -1;
}
else
{
result = pthread_mutex_unlock(mutex);
if ( result )
{
v10 = strerror(result);
fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", v10);
result = -1;
}
}
}
return result;
}

original_code:
int write_on_pipe(int th_pipe, pthread_mutex_t *pipe_mutex, const char *str) {
 int rc;
 if ((rc = pthread_mutex_lock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 if (write(th_pipe, str, strlen(str)) < 0) {
  if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
   fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
   return -1;
  }
  perror("write_err error - can't write on pipe");
  return -1;
 }
 if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int write_on_pipe(int th_pipe, union pthread_mutex_t *pipe_mutex, char *str) {
 int rc;
 if ((rc = pthread_mutex_lock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't acquire lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 if (write(th_pipe, str, strlen(str)) < 0) {
  if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
   fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
   return -1;
  }
  perror("write_err error - can't write on pipe");
  return -1;
 }
 if ((rc = pthread_mutex_unlock(pipe_mutex)) != 0) {
  fprintf(stderr, "write_err error - can't release lock on pipe: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}


decompiled_code:
int __fastcall thread_run(pthread_t *newthread, void *(*start_routine)(void *), void *arg)
{
int v4; // eax
int v5; // eax
int v6; // eax
int result; // eax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
pthread_attr_t v12; // [rsp+0h] [rbp-58h] BYREF

v4 = pthread_attr_init(&v12);
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "thread_run error - can't initialize thread attribute: %s\n", v8);
result = -1;
}
else
{
v5 = pthread_attr_setdetachstate(&v12, 0);
if ( v5 )
{
v9 = strerror(v5);
fprintf(stderr, "thread_run error - can't set thread attribute: %s\n", v9);
result = -1;
}
else
{
v6 = pthread_mutex_init((pthread_mutex_t *)(newthread + 1), 0LL);
if ( v6 )
{
v10 = strerror(v6);
fprintf(stderr, "thread_run error - can't initialize lock: %s\n", v10);
result = -1;
}
else
{
*((_DWORD *)newthread + 12) = 1;
result = pthread_create(newthread, &v12, start_routine, arg);
if ( result )
{
v11 = strerror(result);
fprintf(stderr, "thread_run error - can't create thread: %s\n", v11);
result = -1;
}
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(pthread_t *newthread, void *(*start_routine)(void *), void *arg)
{
int v4; // eax
int v5; // eax
int v6; // eax
int result; // eax
char *v8; // rax
char *v9; // rax
char *v10; // rax
char *v11; // rax
pthread_attr_t v12; // [rsp+0h] [rbp-58h] BYREF

v4 = pthread_attr_init(&v12);
if ( v4 )
{
v8 = strerror(v4);
fprintf(stderr, "thread_run error - can't initialize thread attribute: %s\n", v8);
result = -1;
}
else
{
v5 = pthread_attr_setdetachstate(&v12, 0);
if ( v5 )
{
v9 = strerror(v5);
fprintf(stderr, "thread_run error - can't set thread attribute: %s\n", v9);
result = -1;
}
else
{
v6 = pthread_mutex_init((pthread_mutex_t *)(newthread + 1), 0LL);
if ( v6 )
{
v10 = strerror(v6);
fprintf(stderr, "thread_run error - can't initialize lock: %s\n", v10);
result = -1;
}
else
{
*((_DWORD *)newthread + 12) = 1;
result = pthread_create(newthread, &v12, start_routine, arg);
if ( result )
{
v11 = strerror(result);
fprintf(stderr, "thread_run error - can't create thread: %s\n", v11);
result = -1;
}
}
}
}
return result;
}

original_code:
int thread_run(struct th_info *thinfo, void *(*start_routine)(void *), void *args) {
 int rc;
 pthread_attr_t attr;
 if ((rc = pthread_attr_init(&attr)) != 0) {
  fprintf(stderr, "thread_run error - can't initialize thread attribute: %s\n", strerror(rc));
  return -1;
 }
 if ((rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) != 0) {
  fprintf(stderr, "thread_run error - can't set thread attribute: %s\n", strerror(rc));
  return -1;
 }
 if ((rc = pthread_mutex_init(&thinfo->mutex, ((void *)0))) != 0) {
  fprintf(stderr, "thread_run error - can't initialize lock: %s\n", strerror(rc));
  return -1;
 }
 thinfo->go = 1;
 if ((rc = pthread_create(&thinfo->thread, &attr, start_routine, args)) != 0) {
  fprintf(stderr, "thread_run error - can't create thread: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

canonical_original_code:
int thread_run(struct th_info *thinfo, void *(*start_routine)(void *), void *args) {
 int rc;
 union pthread_attr_t attr;
 if ((rc = pthread_attr_init(&attr)) != 0) {
  fprintf(stderr, "thread_run error - can't initialize thread attribute: %s\n", strerror(rc));
  return -1;
 }
 if ((rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) != 0) {
  fprintf(stderr, "thread_run error - can't set thread attribute: %s\n", strerror(rc));
  return -1;
 }
 if ((rc = pthread_mutex_init(&thinfo->mutex, ((void *)0))) != 0) {
  fprintf(stderr, "thread_run error - can't initialize lock: %s\n", strerror(rc));
  return -1;
 }
 thinfo->go = 1;
 if ((rc = pthread_create(&thinfo->thread, &attr, start_routine, args)) != 0) {
  fprintf(stderr, "thread_run error - can't create thread: %s\n", strerror(rc));
  return -1;
 }
 return 0;
}

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

variable_types:
args
void *

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
int __fastcall thread_stop(__int64 a1)
{
int v1; // eax
int v2; // eax
int result; // eax
char *v4; // rax
char *v5; // rax

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
{
v4 = strerror(v1);
fprintf(stderr, "thread_stop error - can't acquire lock: %s\n", v4);
result = -1;
}
else
{
*(_DWORD *)(a1 + 48) = 0;
v2 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "thread_stop error - can't release lock: %s\n", v5);
result = -1;
}
else
{
result = pthread_kill(*(_QWORD *)a1, 14);
if ( result )
{
fwrite("thread_stop error - can't send signal to thread\n", 1uLL, 0x30uLL, stderr);
result = -1;
}
}
}
return result;
}

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

v1 = pthread_mutex_lock((pthread_mutex_t *)(a1 + 8));
if ( v1 )
{
v4 = strerror(v1);
fprintf(stderr, "thread_stop error - can't acquire lock: %s\n", v4);
result = -1;
}
else
{
*(_DWORD *)(a1 + 48) = 0;
v2 = pthread_mutex_unlock((pthread_mutex_t *)(a1 + 8));
if ( v2 )
{
v5 = strerror(v2);
fprintf(stderr, "thread_stop error - can't release lock: %s\n", v5);
result = -1;
}
else
{
result = pthread_kill(*(_QWORD *)a1, 14);
if ( result )
{
fwrite("thread_stop error - can't send signal to thread\n", 1uLL, 0x30uLL, stderr);
result = -1;
}
}
}
return result;
}

original_code:
int thread_stop(struct th_info *thinfo) {
 int rc;
 if ((rc = pthread_mutex_lock(&thinfo->mutex)) != 0) {
  fprintf(stderr, "thread_stop error - can't acquire lock: %s\n", strerror(rc));
  return -1;
 }
 thinfo->go = 0;
 if ((rc = pthread_mutex_unlock(&thinfo->mutex)) != 0) {
  fprintf(stderr, "thread_stop error - can't release lock: %s\n", strerror(rc));
  return -1;
 }
 if ((rc = pthread_kill(thinfo->thread, 14)) != 0) {
  fprintf(stderr, "thread_stop error - can't send signal to thread\n");
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall to_hex(__int64 a1, __int64 a2)
{
__int64 i; // rbx
__int64 result; // rax
char s; // [rsp+Dh] [rbp-1Bh] BYREF
unsigned __int8 v5; // [rsp+Eh] [rbp-1Ah]

for ( i = 0LL; i != 16; ++i )
{
sprintf(&s, "%02x", *(unsigned __int8 *)(a2 + i));
*(_BYTE *)(a1 + 2 * i) = s;
result = v5;
*(_BYTE *)(a1 + 2 * i + 1) = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 i; // rbx
__int64 result; // rax
char s; // [rsp+Dh] [rbp-1Bh] BYREF
unsigned __int8 v5; // [rsp+Eh] [rbp-1Ah]

for ( i = 0LL; i != 16; ++i )
{
sprintf(&s, "%02x", *(unsigned __int8 *)(a2 + i));
*(_BYTE *)(a1 + 2 * i) = s;
result = v5;
*(_BYTE *)(a1 + 2 * i + 1) = v5;
}
return result;
}

original_code:
void to_hex(char *strhex, const unsigned char *md5) {
 char str[3];
 int i;
 for (i = 0; i < 16; i ++) {
  sprintf(str, "%02x", md5[i]);
  strhex[i * 2] = str[0];
  strhex[i * 2 + 1] = str[1];
 }
}

canonical_original_code:
void to_hex(char *strhex, unsigned char *md5) {
 char str[3];
 int i;
 for (i = 0; i < 16; i ++) {
  sprintf(str, "%02x", md5[i]);
  strhex[i * 2] = str[0];
  strhex[i * 2 + 1] = str[1];
 }
}

variable_types:
strhex
char *

variable_types:
str
char [3]


decompiled_code:
__int64 __fastcall getMD5fd(int fd, __int64 a2)
{
ssize_t v2; // rdx
__int64 result; // rax
_BYTE v4[8192]; // [rsp+0h] [rbp-2078h] BYREF
char v5[120]; // [rsp+2000h] [rbp-78h] BYREF

if ( (unsigned int)MD5_Init(v5) )
{
while ( 1 )
{
v2 = read(fd, v4, 0x2000uLL);
if ( !v2 )
break;
if ( !(unsigned int)MD5_Update(v5, v4, v2) )
{
fwrite("MD5_Update failed\n", 1uLL, 0x12uLL, stderr);
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)MD5_Final(a2, v5) )
{
result = 0LL;
}
else
{
fwrite("MD5_Final failed\n", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("MD5_Init failed\n", 1uLL, 0x10uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2)
{
ssize_t v2; // rdx
__int64 result; // rax
_BYTE v4[8192]; // [rsp+0h] [rbp-2078h] BYREF
char v5[120]; // [rsp+2000h] [rbp-78h] BYREF

if ( (unsigned int)MD5_Init(v5) )
{
while ( 1 )
{
v2 = read(fd, v4, 0x2000uLL);
if ( !v2 )
break;
if ( !(unsigned int)MD5_Update(v5, v4, v2) )
{
fwrite("MD5_Update failed\n", 1uLL, 0x12uLL, stderr);
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)MD5_Final(a2, v5) )
{
result = 0LL;
}
else
{
fwrite("MD5_Final failed\n", 1uLL, 0x11uLL, stderr);
result = 0xFFFFFFFFLL;
}
}
else
{
fwrite("MD5_Init failed\n", 1uLL, 0x10uLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int getMD5fd(int fd, unsigned char *md) {
 MD5_CTX md5ctx;
 char buf[8192];
 unsigned long n;
 if (MD5_Init(&md5ctx) == 0) {
  fprintf(stderr, "MD5_Init failed\n");
  return -1;
 }
 while ((n = read(fd, buf, 8192)) > 0) {
  if (MD5_Update(&md5ctx, buf, n) == 0) {
   fprintf(stderr, "MD5_Update failed\n");
   return -1;
  }
 }
 if (MD5_Final(md, &md5ctx) == 0) {
  fprintf(stderr, "MD5_Final failed\n");
  return -1;
 }
 return 0;
}

canonical_original_code:
int getMD5fd(int fd, unsigned char *md) {
 struct MD5state_st md5ctx;
 char buf[8192];
 unsigned long n;
 if (MD5_Init(&md5ctx) == 0) {
  fprintf(stderr, "MD5_Init failed\n");
  return -1;
 }
 while ((n = read(fd, buf, 8192)) > 0) {
  if (MD5_Update(&md5ctx, buf, n) == 0) {
   fprintf(stderr, "MD5_Update failed\n");
   return -1;
  }
 }
 if (MD5_Final(md, &md5ctx) == 0) {
  fprintf(stderr, "MD5_Final failed\n");
  return -1;
 }
 return 0;
}

variable_types: fd int

variable_types:
md
unsigned char *

variable_types: struct MD5state_st { unsigned int A; unsigned int Nl; unsigned int data[-1]; unsigned int num; }

variable_types:
buf
char [8192]

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall getMD5fname(const char *a1, __int64 a2)
{
int v2; // eax
int v3; // ebx
__int64 result; // rax

v2 = open(a1, 0);
if ( v2 < 0 )
{
perror("getMD5name error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( (int)getMD5fd(v2, a2) < 0 )
{
fwrite("getMD5file failed\n", 1uLL, 0x12uLL, stderr);
close(v3);
result = 0xFFFFFFFFLL;
}
else if ( close(v3) < 0 )
{
perror("bad close");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, __int64 a2)
{
int v2; // eax
int v3; // ebx
__int64 result; // rax

v2 = open(a1, 0);
if ( v2 < 0 )
{
perror("getMD5name error - open failed");
result = 0xFFFFFFFFLL;
}
else
{
v3 = v2;
if ( (int)func1(v2, a2) < 0 )
{
fwrite("getMD5file failed\n", 1uLL, 0x12uLL, stderr);
close(v3);
result = 0xFFFFFFFFLL;
}
else if ( close(v3) < 0 )
{
perror("bad close");
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int getMD5fname(const char *name, unsigned char *md5) {
 int fd;
 if ((fd = open(name, 00)) < 0) {
  perror("getMD5name error - open failed");
  return -1;
 }
 if (getMD5fd(fd, md5) < 0) {
  fprintf(stderr, "getMD5file failed\n");
  close(fd);
  return -1;
 }
 if (close(fd) < 0) {
  perror("bad close");
  return -1;
 }
 return 0;
}

canonical_original_code:
int getMD5fname(char *name, unsigned char *md5) {
 int fd;
 if ((fd = open(name, 00)) < 0) {
  perror("getMD5name error - open failed");
  return -1;
 }
 if (getMD5fd(fd, md5) < 0) {
  fprintf(stderr, "getMD5file failed\n");
  close(fd);
  return -1;
 }
 if (close(fd) < 0) {
  perror("bad close");
  return -1;
 }
 return 0;
}

variable_types:
name
char *


decompiled_code:
int __fastcall print_as_hex(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%02x", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int8 *a1, int a2)
{
unsigned __int8 *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%02x", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

original_code:
void print_as_hex(const unsigned char *digest, int len) {
 int i;
 for(i = 0; i < len; i++){
  printf("%02x", digest[i]);
 }
 printf("\n");
}

canonical_original_code:
void print_as_hex(unsigned char *digest, int len) {
 int i;
 for(i = 0; i < len; i++){
  printf("%02x", digest[i]);
 }
 printf("\n");
}

variable_types:
digest
unsigned char *

variable_types: len int

variable_types: i int


decompiled_code:
__int64 __fastcall get_from_hex(__int64 a1, _BYTE *a2, int a3)
{
__int64 v3; // rbx
__int64 v5; // r12
__int64 result; // rax
unsigned int v7; // [rsp+0h] [rbp-1Ch] BYREF

if ( a3 > 0 )
{
v3 = a1;
v5 = a1 + 2LL * (unsigned int)(a3 - 1) + 2;
do
{
_isoc99_sscanf(v3, "%02x", &v7);
result = v7;
*a2 = v7;
v3 += 2LL;
++a2;
}
while ( v3 != v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2, int a3)
{
__int64 v3; // rbx
__int64 v5; // r12
__int64 result; // rax
unsigned int v7; // [rsp+0h] [rbp-1Ch] BYREF

if ( a3 > 0 )
{
v3 = a1;
v5 = a1 + 2LL * (unsigned int)(a3 - 1) + 2;
do
{
_isoc99_sscanf(v3, "%02x", &v7);
result = v7;
*a2 = v7;
v3 += 2LL;
++a2;
}
while ( v3 != v5 );
}
return result;
}

original_code:
void get_from_hex(const char *strhex, unsigned char *digest, int len) {
 int i;
 unsigned int tmp;
 for (i = 0; i < len; i ++) {
  sscanf(strhex + i * 2, "%02x", &tmp);
  digest[i] = (unsigned char)tmp;
 }
}

canonical_original_code:
void get_from_hex(char *strhex, unsigned char *digest, int len) {
 int i;
 unsigned int tmp;
 for (i = 0; i < len; i ++) {
  sscanf(strhex + i * 2, "%02x", &tmp);
  digest[i] = (unsigned char)tmp;
 }
}

variable_types:
tmp
unsigned int


decompiled_code:
void __fastcall remove_sp_node(void **a1)
{
if ( a1 )
{
if ( sp_list_head )
{
free(*a1);
*a1 = 0LL;
sp_list_head = remove_node(sp_list_head, a1);
--sp_count;
}
}
}

canonical_decompiled_code:
void __fastcall func0(void **a1)
{
if ( a1 )
{
if ( sp_list_head )
{
free(*a1);
*a1 = 0LL;
sp_list_head = remove_node(sp_list_head, a1);
--sp_count;
}
}
}

original_code:
void remove_sp_node(struct node *sp_node) {
 if (sp_node == ((void *)0) || sp_list_head == ((void *)0)) {
  return;
 }
 free(sp_node->data);
 sp_node->data = ((void *)0);
 sp_list_head = remove_node(sp_list_head, sp_node);
 sp_count --;
}

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


decompiled_code:
__int64 __fastcall insert_sp(__int64 a1, char a2)
{
__int64 *v2; // rax
__int64 result; // rax
_WORD *v4; // r12

if ( !sp_list_head )
sp_count = 0;
v2 = get_node_sp(a1);
if ( v2 )
{
result = *v2;
*(_WORD *)(result + 18) = 1;
}
else
{
v4 = malloc(0x14uLL);
v4[9] = 1;
*((_BYTE *)v4 + 16) = a2;
addrcpy(v4, a1);
result = insert_node(sp_list_head, v4);
sp_list_head = result;
if ( !it_addr )
it_addr = result;
++sp_count;
}
return result;
}

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

if ( !sp_list_head )
sp_count = 0;
v2 = get_node_sp(a1);
if ( v2 )
{
result = *v2;
*(_WORD *)(result + 18) = 1;
}
else
{
v4 = malloc(0x14uLL);
v4[9] = 1;
*((_BYTE *)v4 + 16) = a2;
addrcpy(v4, a1);
result = insert_node(sp_list_head, v4);
sp_list_head = result;
if ( !it_addr )
it_addr = result;
++sp_count;
}
return result;
}

original_code:
void insert_sp(const struct sockaddr_in *sp_addr, unsigned char free_spots) {
 struct node *tmp_node;
 struct spaddr_node *new_node;
 if (sp_list_head == ((void *)0)) {
  sp_count = 0;
 }
 if ((tmp_node = get_node_sp(sp_addr)) == ((void *)0)) {
  new_node = (struct spaddr_node *)malloc(sizeof(struct spaddr_node));
  new_node->flag = 1;
  new_node->free_spots = free_spots;
  addrcpy(&new_node->sp_addr, sp_addr);
  sp_list_head = insert_node(sp_list_head, new_node);
  if (it_addr == ((void *)0)) {
   it_addr = sp_list_head;
  }
  sp_count ++;
 } else {
  ((struct spaddr_node *)tmp_node->data)->flag = 1;
 }
}

canonical_original_code:
void insert_sp(struct sockaddr_in *sp_addr, unsigned char free_spots) {
 struct node *tmp_node;
 struct spaddr_node *new_node;
 if (sp_list_head == ((void *)0)) {
  sp_count = 0;
 }
 if ((tmp_node = get_node_sp(sp_addr)) == ((void *)0)) {
  new_node = (struct spaddr_node *)malloc(sizeof(struct spaddr_node));
  new_node->flag = 1;
  new_node->free_spots = free_spots;
  addrcpy(&new_node->sp_addr, sp_addr);
  sp_list_head = insert_node(sp_list_head, new_node);
  if (it_addr == ((void *)0)) {
   it_addr = sp_list_head;
  }
  sp_count ++;
 } else {
  ((struct spaddr_node *)tmp_node->data)->flag = 1;
 }
}

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 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 spaddr_node { struct sockaddr_in sp_addr; unsigned char free_spots; short flag; }


decompiled_code:
void __fastcall remove_sp(__int64 a1)
{
void **v1; // rax
void **v2; // rdi
__int64 v3; // rax

v1 = (void **)get_node_sp(a1);
if ( v1 )
{
v2 = v1;
if ( (void **)it_addr == v1 )
{
v3 = (__int64)v1[1];
if ( v3 )
it_addr = v3;
else
it_addr = (__int64)v2[2];
}
remove_sp_node(v2);
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
void **v1; // rax
void **v2; // rdi
__int64 v3; // rax

v1 = (void **)get_node_sp(a1);
if ( v1 )
{
v2 = v1;
if ( (void **)it_addr == v1 )
{
v3 = (__int64)v1[1];
if ( v3 )
it_addr = v3;
else
it_addr = (__int64)v2[2];
}
func0(v2);
}
}

original_code:
void remove_sp(const struct sockaddr_in *sp_addr) {
 struct node *tmp_node;
 if ((tmp_node = get_node_sp(sp_addr)) != ((void *)0)) {
  if (it_addr == tmp_node) {
   if (tmp_node->next != ((void *)0)) {
    it_addr = tmp_node->next;
   } else if (tmp_node->prev != ((void *)0)) {
    it_addr = tmp_node->prev;
   } else {
    it_addr = ((void *)0);
   }
  }
  remove_sp_node(tmp_node);
 }
}

canonical_original_code:
void remove_sp(struct sockaddr_in *sp_addr) {
 struct node *tmp_node;
 if ((tmp_node = get_node_sp(sp_addr)) != ((void *)0)) {
  if (it_addr == tmp_node) {
   if (tmp_node->next != ((void *)0)) {
    it_addr = tmp_node->next;
   } else if (tmp_node->prev != ((void *)0)) {
    it_addr = tmp_node->prev;
   } else {
    it_addr = ((void *)0);
   }
  }
  remove_sp_node(tmp_node);
 }
}


decompiled_code:
__int64 get_addr()
{
__int64 v0; // rdx
__int64 v1; // rax

v0 = *(_QWORD *)it_addr;
v1 = *(_QWORD *)(it_addr + 8);
if ( !v1 )
v1 = sp_list_head;
it_addr = v1;
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rdx
__int64 v1; // rax

v0 = *(_QWORD *)it_addr;
v1 = *(_QWORD *)(it_addr + 8);
if ( !v1 )
v1 = sp_list_head;
it_addr = v1;
return v0;
}

original_code:
struct sockaddr_in *get_addr() {
 struct sockaddr_in *ret;
 ret = &((struct spaddr_node *)it_addr->data)->sp_addr;
 if (it_addr->next != ((void *)0)) {
  it_addr = it_addr->next;
 } else {
  it_addr = sp_list_head;
 }
 return ret;
}


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

for ( i = (void **)sp_list_head; sp_list_head; i = (void **)sp_list_head )
remove_sp_node(i);
}

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

for ( i = (void **)sp_list_head; sp_list_head; i = (void **)sp_list_head )
func0(i);
}

original_code:
void free_list_sp() {
 while (sp_list_head != ((void *)0)) {
  remove_sp_node(sp_list_head);
 }
}


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

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

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

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

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

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

variable_types:
dim
int *

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


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

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

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

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

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

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

variable_types: socksd int

variable_types:
data
char *

variable_types: struct near_node { int socksd; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; char data[36]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; short near_number; struct near_node *next; struct near_node *prev; } *


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

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

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

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

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

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

return_type: struct near_node { int socksd; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } addr; char data[36]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; short near_number; struct near_node *next; struct near_node *prev; } *


decompiled_code:
__int64 __fastcall get_near_by_addr(__int64 a1)
{
__int64 i; // rbx

for ( i = near_list_head; i; i = *(_QWORD *)(i + 104) )
{
if ( (unsigned int)addrcmp(i + 4, a1) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 i; // rbx

for ( i = near_list_head; i; i = *(_QWORD *)(i + 104) )
{
if ( (unsigned int)addrcmp(i + 4, a1) )
break;
}
return i;
}

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

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

variable_types:
addr
struct sockaddr_in *


decompiled_code:
__int64 __fastcall insert_near(int a1, const void *a2)
{
__int64 v3; // rdi
char *v4; // rax
__int64 v5; // rdx
__int64 v6; // rcx
unsigned int v7; // edx
char *v9; // rax

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

canonical_decompiled_code:
__int64 __fastcall func4(int a1, const void *a2)
{
__int64 v3; // rdi
char *v4; // rax
__int64 v5; // rdx
__int64 v6; // rcx
unsigned int v7; // edx
char *v9; // rax

v3 = near_list_head;
if ( near_list_head )
{
while ( *(_DWORD *)v3 != a1 )
{
v3 = *(_QWORD *)(v3 + 104);
if ( !v3 )
{
v4 = func3(a1, (__int64)a2);
v5 = near_list_head;
*((_QWORD *)v4 + 13) = *(_QWORD *)(near_list_head + 104);
*((_QWORD *)v4 + 14) = v5;
v6 = *(_QWORD *)(v5 + 104);
if ( v6 )
*(_QWORD *)(v6 + 112) = v4;
*(_QWORD *)(v5 + 104) = v4;
return 0;
}
}
v7 = memcmp((const void *)(v3 + 4), a2, 0x10uLL);
if ( v7 )
{
fwrite("insert_near error - list is not consistent\n", 1uLL, 0x2BuLL, stderr);
v7 = -1;
}
}
else
{
v9 = func3(a1, (__int64)a2);
near_list_head = (__int64)v9;
*((_QWORD *)v9 + 13) = 0LL;
*((_QWORD *)v9 + 14) = 0LL;
v7 = 0;
}
return v7;
}

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

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


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

variable_types:
head
struct near_node *

variable_types:
toremove
struct near_node *


decompiled_code:
__int64 __fastcall delete_near(int a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int v3; // eax
char *v5; // rax

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

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
__int64 v1; // rbx
unsigned int v2; // ebp
int v3; // eax
char *v5; // rax

v1 = near_list_head;
if ( !near_list_head )
return (unsigned int)-1;
while ( *(_DWORD *)v1 != a1 )
{
v1 = *(_QWORD *)(v1 + 104);
if ( !v1 )
return (unsigned int)-1;
}
v3 = pthread_mutex_destroy((pthread_mutex_t *)(v1 + 56));
v2 = v3;
if ( v3 )
{
v5 = strerror(v3);
fprintf(stderr, "delete_near error - can't destroy lock: %s\n", v5);
v2 = -1;
}
else
{
near_list_head = func5(near_list_head, (_QWORD *)v1);
}
return v2;
}

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

variable_types: rc int