decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // eax
int v5; // er15
struct addrinfo *v6; // rbx
sockaddr *v7; // rax
int v8; // edi
char *v9; // rbp
char *v10; // rax
int v11; // eax
int v12; // er12
unsigned int v13; // ebp
const char *v15; // rax
double v16; // [rsp+8h] [rbp-1F0h]
socklen_t addr_len; // [rsp+14h] [rbp-1E4h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-1E0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-1D8h] BYREF
char buf[48]; // [rsp+30h] [rbp-1C8h] BYREF
char v21[48]; // [rsp+60h] [rbp-198h] BYREF
struct addrinfo req; // [rsp+90h] [rbp-168h] BYREF
char s[112]; // [rsp+C0h] [rbp-138h] BYREF
struct stat addr; // [rsp+130h] [rbp-C8h] BYREF

if ( argc == 2 )
{
v3 = (char *)argv[1];
if ( (unsigned int)stat(v3, &addr) )
{
fprintf(stderr, "Failure: Prefix '%s' could not be found\n", v3);
return -1;
}
if ( (addr.st_mode & 0x4000) == 0 )
{
fprintf(stderr, "Failure: Prefix '%s' must be a directory\n", v3);
return -1;
}
}
else
{
v3 = ".";
if ( argc > 2 )
puts("Usage: server [path-prefix]");
}
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
v4 = getaddrinfo(0LL, "3490", &req, &pai);
v5 = v4;
if ( v4 )
{
v15 = gai_strerror(v4);
fprintf(stderr, "Server error at getaddrinfo: %s\n", v15);
exit(1);
}
v6 = pai;
if ( !pai )
{
LABEL_18:
fwrite("Server: Failed to bind\n", 1uLL, 0x17uLL, stderr);
return -1;
}
while ( 1 )
{
v7 = v6->ai_addr;
v8 = v6->ai_family;
v9 = &v7->sa_data[2];
v10 = &v7->sa_data[6];
if ( v8 != 2 )
v9 = v10;
v11 = socket(v8, v6->ai_socktype, v6->ai_protocol);
v12 = v11;
if ( v11 != -1 )
break;
perror("Server: Failed to create socket");
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_18;
}
if ( bind(v11, v6->ai_addr, v6->ai_addrlen) == -1 )
{
close(v12);
perror("Server: Failed to bind to socket");
exit(1);
}
inet_ntop(v6->ai_family, v9, buf, 0x2Eu);
printf("Starting server at %s...\n", buf);
freeaddrinfo(pai);
if ( listen(v12, 10) == -1 )
{
perror("Server: Failed to listen for new connections");
exit(1);
}
while ( 1 )
{
addr_len = 128;
v13 = accept(v12, (struct sockaddr *)&addr, &addr_len);
if ( v13 == -1 )
{
perror("Server: Error when accepting connection");
exit(1);
}
inet_ntop(LOWORD(addr.st_dev), (char *)&addr.st_dev + 4, v21, 0x2Eu);
gettimeofday(&tv, 0LL);
v16 = (double)(int)(1000 * LODWORD(tv.tv_sec) + tv.tv_usec / 1000);
printf("Got connection from %s at %.0f\n", v21, v16);
snprintf(s, 0x64uLL, "%s/%s_%.0f", v3, v21, v16);
if ( (unsigned int)write_file(s, v13) )
break;
printf("Successfully wrote file '%s'\n", s);
close(v13);
}
fwrite("Error, shutting down\n", 1uLL, 0x15uLL, stderr);
close(v12);
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r14
int v4; // eax
int v5; // er15
struct addrinfo *v6; // rbx
sockaddr *v7; // rax
int v8; // edi
char *v9; // rbp
char *v10; // rax
int v11; // eax
int v12; // er12
unsigned int v13; // ebp
const char *v15; // rax
double v16; // [rsp+8h] [rbp-1F0h]
socklen_t addr_len; // [rsp+14h] [rbp-1E4h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-1E0h] BYREF
struct timeval tv; // [rsp+20h] [rbp-1D8h] BYREF
char buf[48]; // [rsp+30h] [rbp-1C8h] BYREF
char v21[48]; // [rsp+60h] [rbp-198h] BYREF
struct addrinfo req; // [rsp+90h] [rbp-168h] BYREF
char s[112]; // [rsp+C0h] [rbp-138h] BYREF
struct stat addr; // [rsp+130h] [rbp-C8h] BYREF

if ( argc == 2 )
{
v3 = (char *)argv[1];
if ( (unsigned int)func10(v3, &addr) )
{
fprintf(stderr, "Failure: Prefix '%s' could not be found\n", v3);
return -1;
}
if ( (addr.st_mode & 0x4000) == 0 )
{
fprintf(stderr, "Failure: Prefix '%s' must be a directory\n", v3);
return -1;
}
}
else
{
v3 = ".";
if ( argc > 2 )
puts("Usage: server [path-prefix]");
}
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_socktype = 1;
req.ai_flags = 1;
*(_OWORD *)&req.ai_canonname = 0LL;
v4 = getaddrinfo(0LL, "3490", &req, &pai);
v5 = v4;
if ( v4 )
{
v15 = gai_strerror(v4);
fprintf(stderr, "Server error at getaddrinfo: %s\n", v15);
exit(1);
}
v6 = pai;
if ( !pai )
{
LABEL_18:
fwrite("Server: Failed to bind\n", 1uLL, 0x17uLL, stderr);
return -1;
}
while ( 1 )
{
v7 = v6->ai_addr;
v8 = v6->ai_family;
v9 = &v7->sa_data[2];
v10 = &v7->sa_data[6];
if ( v8 != 2 )
v9 = v10;
v11 = socket(v8, v6->ai_socktype, v6->ai_protocol);
v12 = v11;
if ( v11 != -1 )
break;
perror("Server: Failed to create socket");
v6 = v6->ai_next;
if ( !v6 )
goto LABEL_18;
}
if ( bind(v11, v6->ai_addr, v6->ai_addrlen) == -1 )
{
close(v12);
perror("Server: Failed to bind to socket");
exit(1);
}
inet_ntop(v6->ai_family, v9, buf, 0x2Eu);
printf("Starting server at %s...\n", buf);
freeaddrinfo(pai);
if ( listen(v12, 10) == -1 )
{
perror("Server: Failed to listen for new connections");
exit(1);
}
while ( 1 )
{
addr_len = 128;
v13 = accept(v12, (struct sockaddr *)&addr, &addr_len);
if ( v13 == -1 )
{
perror("Server: Error when accepting connection");
exit(1);
}
inet_ntop(LOWORD(addr.st_dev), (char *)&addr.st_dev + 4, v21, 0x2Eu);
gettimeofday(&tv, 0LL);
v16 = (double)(int)(1000 * LODWORD(tv.tv_sec) + tv.tv_usec / 1000);
printf("Got connection from %s at %.0f\n", v21, v16);
snprintf(s, 0x64uLL, "%s/%s_%.0f", v3, v21, v16);
if ( (unsigned int)func7(s, v13) )
break;
printf("Successfully wrote file '%s'\n", s);
close(v13);
}
fwrite("Error, shutting down\n", 1uLL, 0x15uLL, stderr);
close(v12);
return v5;
}

original_code:
int main(int argc, char * args[]) {
    int socketfd, status, new_fd;
    struct addrinfo hints, *servinfo, *current;
    struct sockaddr_storage remote_addr;
    struct timeval tv;
    char ipstr[46], filename[100];
    socklen_t remote_addr_size;
    double timestamp;
    pid_t childId;
    char * address_prefix;
    if (argc == 2) {
        struct stat file_status;
        address_prefix = args[1];
        if (stat(address_prefix, &file_status) == 0) {
            if (!(file_status.st_mode & 0040000)) {
                fprintf(stderr, "Failure: Prefix '%s' must be a directory\n", address_prefix);
                return -1;
            }
        } else {
            fprintf(stderr, "Failure: Prefix '%s' could not be found\n", address_prefix);
            return -1;
        }
    } else if (argc > 2) {
        printf("Usage: server [path-prefix]\n");
    } else {
        address_prefix = ".";
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((status = getaddrinfo(((void *)0), "3490", &hints, &servinfo)) != 0) {
        fprintf(stderr, "Server error at getaddrinfo: %s\n", gai_strerror(status));
        exit(1);
    }
    for (current = servinfo; current != ((void *)0); current = current->ai_next) {
        void * address;
        char * ip_version;
        if (current->ai_family == 2) {
            address = &(((struct sockaddr_in *) current->ai_addr)->sin_addr);
        } else {
            address = &(((struct sockaddr_in6 *) current->ai_addr)->sin6_addr);
        }
        if ((socketfd = socket(current->ai_family,
                             current->ai_socktype,
                             current->ai_protocol)) == -1) {
            perror("Server: Failed to create socket");
            continue;
        }
        if (bind(socketfd, current->ai_addr, current->ai_addrlen) == -1) {
            close(socketfd);
            perror("Server: Failed to bind to socket");
            exit(1);
        }
        inet_ntop(current->ai_family, address, ipstr, sizeof ipstr);
        printf("Starting server at %s...\n", ipstr);
        break;
    }
    if (current == ((void *)0)) {
        fprintf(stderr, "Server: Failed to bind\n");
        return -1;
    }
    freeaddrinfo(servinfo);
    if (listen(socketfd, 10) == -1) {
        perror("Server: Failed to listen for new connections");
        exit(1);
    }
    while (1) {
        remote_addr_size = sizeof remote_addr;
        if ((new_fd = accept(socketfd, (struct sockaddr *)&remote_addr, &remote_addr_size)) == -1) {
            perror("Server: Error when accepting connection");
            exit(1);
        }
        char s[46];
        inet_ntop(remote_addr.ss_family,
            &((struct sockaddr_in *)&remote_addr)->sin_addr,
            s, sizeof s);
        gettimeofday(&tv, ((void *)0));
        timestamp = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
        printf("Got connection from %s at %.0f\n", s, timestamp);
        snprintf(filename, sizeof(filename), "%s/%s_%.0f", address_prefix, s, timestamp);
        if (write_file(filename, new_fd) != 0) {
            fprintf(stderr, "Error, shutting down\n");
            break;
        }
        printf("Successfully wrote file '%s'\n", filename);
        close (new_fd);
    }
    close(socketfd);
    return 0;
}

canonical_original_code:
int main(int argc, char *args[]) {
    int socketfd; int status; int new_fd;
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *current;
    struct sockaddr_storage remote_addr;
    struct timeval tv;
    char ipstr[46]; char filename[100];
    unsigned int remote_addr_size;
    double timestamp;
    int childId;
    char *address_prefix;
    if (argc == 2) {
        struct stat file_status;
        address_prefix = args[1];
        if (stat(address_prefix, &file_status) == 0) {
            if (!(file_status.st_mode & 0040000)) {
                fprintf(stderr, "Failure: Prefix '%s' must be a directory\n", address_prefix);
                return -1;
            }
        } else {
            fprintf(stderr, "Failure: Prefix '%s' could not be found\n", address_prefix);
            return -1;
        }
    } else if (argc > 2) {
        printf("Usage: server [path-prefix]\n");
    } else {
        address_prefix = ".";
    }
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((status = getaddrinfo(((void *)0), "3490", &hints, &servinfo)) != 0) {
        fprintf(stderr, "Server error at getaddrinfo: %s\n", gai_strerror(status));
        exit(1);
    }
    for (current = servinfo; current != ((void *)0); current = current->ai_next) {
        void *address;
        char *ip_version;
        if (current->ai_family == 2) {
            address = &(((struct sockaddr_in *) current->ai_addr)->sin_addr);
        } else {
            address = &(((struct sockaddr_in6 *) current->ai_addr)->sin6_addr);
        }
        if ((socketfd = socket(current->ai_family,
                             current->ai_socktype,
                             current->ai_protocol)) == -1) {
            perror("Server: Failed to create socket");
            continue;
        }
        if (bind(socketfd, current->ai_addr, current->ai_addrlen) == -1) {
            close(socketfd);
            perror("Server: Failed to bind to socket");
            exit(1);
        }
        inet_ntop(current->ai_family, address, ipstr, sizeof ipstr);
        printf("Starting server at %s...\n", ipstr);
        break;
    }
    if (current == ((void *)0)) {
        fprintf(stderr, "Server: Failed to bind\n");
        return -1;
    }
    freeaddrinfo(servinfo);
    if (listen(socketfd, 10) == -1) {
        perror("Server: Failed to listen for new connections");
        exit(1);
    }
    while (1) {
        remote_addr_size = sizeof remote_addr;
        if ((new_fd = accept(socketfd, (struct sockaddr *)&remote_addr, &remote_addr_size)) == -1) {
            perror("Server: Error when accepting connection");
            exit(1);
        }
        char s[46];
        inet_ntop(remote_addr.ss_family,
            &((struct sockaddr_in *)&remote_addr)->sin_addr,
            s, sizeof s);
        gettimeofday(&tv, ((void *)0));
        timestamp = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
        printf("Got connection from %s at %.0f\n", s, timestamp);
        snprintf(filename, sizeof(filename), "%s/%s_%.0f", address_prefix, s, timestamp);
        if (write_file(filename, new_fd) != 0) {
            fprintf(stderr, "Error, shutting down\n");
            break;
        }
        printf("Successfully wrote file '%s'\n", filename);
        close (new_fd);
    }
    close(socketfd);
    return 0;
}

variable_types: socketfd int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

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

variable_types:
filename
char [100]

variable_types: timestamp double

variable_types: childId int

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
s
char [46]

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: JekaMelnikov/Numerical-Methods

decompiled_code:
double __fastcall get_h1(int a1)
{
return 1.0 / (double)a1;
}

canonical_decompiled_code:
double __fastcall func0(int a1)
{
return 1.0 / (double)a1;
}

original_code:
double get_h1 (spec_mesh mesh)
{
  return 1.0 / mesh.M1;
}

canonical_original_code:
double get_h1(struct _spec_mesh mesh)
{
  return 1.0 / mesh.M1;
}

variable_types:
mesh
struct _spec_mesh { int M1; int M2; }

return_type: double


decompiled_code:
double __fastcall get_h2(__int64 a1)
{
return 1.0 / (double)SHIDWORD(a1);
}

canonical_decompiled_code:
double __fastcall func1(__int64 a1)
{
return 1.0 / (double)SHIDWORD(a1);
}

original_code:
double get_h2 (spec_mesh mesh)
{
  return 1.0 / mesh.M2;
}

canonical_original_code:
double get_h2(struct _spec_mesh mesh)
{
  return 1.0 / mesh.M2;
}


size

decompiled_code:
__int64 __fastcall size(__int64 a1)
{
return (unsigned int)((HIDWORD(a1) + 1) * (a1 + 1) - SHIDWORD(a1) / 4 * ((int)a1 / 5 - 1));
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return (unsigned int)((HIDWORD(a1) + 1) * (a1 + 1) - SHIDWORD(a1) / 4 * ((int)a1 / 5 - 1));
}

original_code:
int size (spec_mesh mesh)
{
  return (mesh.M1 + 1) * (mesh.M2 + 1) - (mesh.M1 / 5 - 1) * (mesh.M2 / 4);
}

canonical_original_code:
int size(struct _spec_mesh mesh)
{
  return (mesh.M1 + 1) * (mesh.M2 + 1) - (mesh.M1 / 5 - 1) * (mesh.M2 / 4);
}

return_type: int


decompiled_code:
__int64 __fastcall boundary_size(__int64 a1)
{
return (unsigned int)(SHIDWORD(a1) / 2 + 2 * (HIDWORD(a1) + a1));
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return (unsigned int)(SHIDWORD(a1) / 2 + 2 * (HIDWORD(a1) + a1));
}

original_code:
int boundary_size (spec_mesh mesh)
{
  return 2 * (mesh.M1 + mesh.M2) + mesh.M2 / 2;
}

canonical_original_code:
int boundary_size(struct _spec_mesh mesh)
{
  return 2 * (mesh.M1 + mesh.M2) + mesh.M2 / 2;
}


decompiled_code:
int __fastcall print_properties(__int64 a1)
{
unsigned int v1; // eax
int v2; // r9^4

v1 = size(a1);
return printf(
"Special mesh %d x %d\nh1 = %.4lf\nh2 = %.4lf\nTotal number of cells %d\n",
a1,
(unsigned int)v2,
1.0 / (double)(int)a1,
1.0 / (double)v2,
v1);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
unsigned int v1; // eax
int v2; // r9^4

v1 = func3(a1);
return printf(
"Special mesh %d x %d\nh1 = %.4lf\nh2 = %.4lf\nTotal number of cells %d\n",
a1,
(unsigned int)v2,
1.0 / (double)(int)a1,
1.0 / (double)v2,
v1);
}

original_code:
void print_properties (spec_mesh mesh)
{
  printf ("Special mesh %d x %d\nh1 = %.4lf\nh2 = %.4lf\nTotal number of cells %d\n",
          mesh.M1, mesh.M2, (double)1 / mesh.M1, (double)1 / mesh.M2, size (mesh));
}

canonical_original_code:
void print_properties(struct _spec_mesh mesh)
{
  printf ("Special mesh %d x %d\nh1 = %.4lf\nh2 = %.4lf\nTotal number of cells %d\n",
          mesh.M1, mesh.M2, (double)1 / mesh.M1, (double)1 / mesh.M2, size (mesh));
}

return_type: void


decompiled_code:
__int64 __fastcall get_up(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // edi
int v5; // eax
int v6; // er8
int v7; // ecx
int v8; // edx
__int64 result; // rax
unsigned int v10; // ecx

if ( a2 < 0 || (int)size(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 + 1;
v5 = a2 / (v2 + 1);
v6 = v3 / -4 + v3;
if ( v6 >= v5 )
{
if ( v5 )
return (unsigned int)(a2 - v4);
return 0xFFFFFFFFLL;
}
v7 = v2 / 5 - 1;
v8 = (a2 - v4 * (v3 / -4 + v3 + 1)) % (v4 - v7);
result = (unsigned int)(a2 - (v4 - v7));
if ( !((a2 - v4 * (v3 / -4 + v3 + 1)) / (v4 - v7)) )
{
result = (unsigned int)(v6 * v4 + v8);
v10 = result + v7;
if ( 2 * v2 / 5 < v8 )
result = v10;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // edi
int v5; // eax
int v6; // er8
int v7; // ecx
int v8; // edx
__int64 result; // rax
unsigned int v10; // ecx

if ( a2 < 0 || (int)func3(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 + 1;
v5 = a2 / (v2 + 1);
v6 = v3 / -4 + v3;
if ( v6 >= v5 )
{
if ( v5 )
return (unsigned int)(a2 - v4);
return 0xFFFFFFFFLL;
}
v7 = v2 / 5 - 1;
v8 = (a2 - v4 * (v3 / -4 + v3 + 1)) % (v4 - v7);
result = (unsigned int)(a2 - (v4 - v7));
if ( !((a2 - v4 * (v3 / -4 + v3 + 1)) / (v4 - v7)) )
{
result = (unsigned int)(v6 * v4 + v8);
v10 = result + v7;
if ( 2 * v2 / 5 < v8 )
result = v10;
}
return result;
}

original_code:
int get_up (spec_mesh mesh, int ind)
{
  int row, col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2/4)
    {
      row = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      col = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (row == 0)
        {
          if (col > (2 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4) + col + (mesh.M1 / 5 - 1);
          else
            return (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4) + col;
        }
      else
        return ind - (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
    }
  else
    {
      if (row == 0)
        return -1;
      else
        return ind - (mesh.M1 + 1);
    }
}

canonical_original_code:
int get_up(struct _spec_mesh mesh, int ind)
{
  int row; int col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2/4)
    {
      row = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      col = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (row == 0)
        {
          if (col > (2 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4) + col + (mesh.M1 / 5 - 1);
          else
            return (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4) + col;
        }
      else
        return ind - (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
    }
  else
    {
      if (row == 0)
        return -1;
      else
        return ind - (mesh.M1 + 1);
    }
}

variable_types: ind int

variable_types: row int

variable_types: col int


original_code:
int get_down (spec_mesh mesh, int ind)
{
  int row, col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      row = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      col = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (row == mesh.M2 / 4 - 1)
        return -1;
      else
        return ind + mesh.M1 + 1 - (mesh.M1 / 5 - 1);
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (row == mesh.M2 - mesh.M2/4)
        {
          if (col <= (2 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2/4) + col;
          else if (col >= (3 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2/4) + col - (mesh.M1 / 5 - 1);
          else
            return -1;
        }
      else
        return ind + (mesh.M1 + 1);
    }
}

canonical_original_code:
int get_down(struct _spec_mesh mesh, int ind)
{
  int row; int col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      row = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      col = (ind - (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2 / 4)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (row == mesh.M2 / 4 - 1)
        return -1;
      else
        return ind + mesh.M1 + 1 - (mesh.M1 / 5 - 1);
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (row == mesh.M2 - mesh.M2/4)
        {
          if (col <= (2 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2/4) + col;
          else if (col >= (3 * mesh.M1) / 5)
            return (mesh.M1 + 1) * (mesh.M2 + 1 - mesh.M2/4) + col - (mesh.M1 / 5 - 1);
          else
            return -1;
        }
      else
        return ind + (mesh.M1 + 1);
    }
}


decompiled_code:
__int64 __fastcall get_left(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // er9
int v5; // ecx
int v6; // edx

if ( a2 < 0 || (int)size(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 - v2 / 4;
if ( v4 >= a2 / (v3 + 1) )
{
if ( !(a2 % (v3 + 1)) )
return 0xFFFFFFFFLL;
}
else
{
v5 = v3 / 5 - 1;
v6 = (a2 - (v3 + 1) * (v4 + 1)) % (v3 + 1 - v5);
if ( !v6 || 3 * v3 / 5 - v5 == v6 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(a2 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // er9
int v5; // ecx
int v6; // edx

if ( a2 < 0 || (int)func3(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 - v2 / 4;
if ( v4 >= a2 / (v3 + 1) )
{
if ( !(a2 % (v3 + 1)) )
return 0xFFFFFFFFLL;
}
else
{
v5 = v3 / 5 - 1;
v6 = (a2 - (v3 + 1) * (v4 + 1)) % (v3 + 1 - v5);
if ( !v6 || 3 * v3 / 5 - v5 == v6 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(a2 - 1);
}

original_code:
int get_left (spec_mesh mesh, int ind)
{
  int row, col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col == 0 || col == (3 * mesh.M1) / 5 - (mesh.M1 / 5 - 1))
        return -1;
      else
        return ind - 1;
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (col == 0)
        return -1;
      else
        return ind - 1;
    }
}

canonical_original_code:
int get_left(struct _spec_mesh mesh, int ind)
{
  int row; int col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col == 0 || col == (3 * mesh.M1) / 5 - (mesh.M1 / 5 - 1))
        return -1;
      else
        return ind - 1;
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (col == 0)
        return -1;
      else
        return ind - 1;
    }
}


decompiled_code:
__int64 __fastcall get_right(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // er9
int v5; // er8
int v6; // edx

if ( a2 < 0 || (int)size(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 - v2 / 4;
if ( v4 >= a2 / (v3 + 1) )
{
if ( a2 % (v3 + 1) == v3 )
return 0xFFFFFFFFLL;
}
else
{
v5 = v3 / 5 - 1;
v6 = (a2 - (v3 + 1) * (v4 + 1)) % (v3 + 1 - v5);
if ( v3 - v5 == v6 || 2 * (v3 / 5) == v6 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(a2 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
int v2; // er9
int v3; // er10
int v4; // er9
int v5; // er8
int v6; // edx

if ( a2 < 0 || (int)func3(a1) <= a2 )
return 0xFFFFFFFFLL;
v4 = v2 - v2 / 4;
if ( v4 >= a2 / (v3 + 1) )
{
if ( a2 % (v3 + 1) == v3 )
return 0xFFFFFFFFLL;
}
else
{
v5 = v3 / 5 - 1;
v6 = (a2 - (v3 + 1) * (v4 + 1)) % (v3 + 1 - v5);
if ( v3 - v5 == v6 || 2 * (v3 / 5) == v6 )
return 0xFFFFFFFFLL;
}
return (unsigned int)(a2 + 1);
}

original_code:
int get_right (spec_mesh mesh, int ind)
{
  int row, col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col == mesh.M1 - (mesh.M1 / 5 - 1) || col == 2 * (mesh.M1 / 5))
        return -1;
      else
        return ind + 1;
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (col == mesh.M1)
        return -1;
      else
        return ind + 1;
    }
}

canonical_original_code:
int get_right(struct _spec_mesh mesh, int ind)
{
  int row; int col;
  if (ind < 0 || ind >= size (mesh))
    return -1;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col == mesh.M1 - (mesh.M1 / 5 - 1) || col == 2 * (mesh.M1 / 5))
        return -1;
      else
        return ind + 1;
    }
  else
    {
      col = ind % (mesh.M1 + 1);
      if (col == mesh.M1)
        return -1;
      else
        return ind + 1;
    }
}


original_code:
double get_x (spec_mesh mesh, int ind)
{
  int row, col;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col > (2 * mesh.M1) / 5)
        col += mesh.M1 / 5 - 1;
    }
  else
    col = ind % (mesh.M1 + 1);
  return (double)(1.0 / mesh.M1) * col;
}

canonical_original_code:
double get_x(struct _spec_mesh mesh, int ind)
{
  int row; int col;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    {
      col = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) % (mesh.M1 + 1 - (mesh.M1 / 5 - 1));
      if (col > (2 * mesh.M1) / 5)
        col += mesh.M1 / 5 - 1;
    }
  else
    col = ind % (mesh.M1 + 1);
  return (double)(1.0 / mesh.M1) * col;
}


original_code:
double get_y (spec_mesh mesh, int ind)
{
  int row;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    row = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1)) + mesh.M2 - mesh.M2 / 4 + 1;
  return (double)(1.0 / mesh.M2) * (mesh.M2 - row);
}

canonical_original_code:
double get_y(struct _spec_mesh mesh, int ind)
{
  int row;
  row = ind / (mesh.M1 + 1);
  if (row > mesh.M2 - mesh.M2 / 4)
    row = (ind - (mesh.M1 + 1) * (mesh.M2 - mesh.M2 / 4 + 1)) / (mesh.M1 + 1 - (mesh.M1 / 5 - 1)) + mesh.M2 - mesh.M2 / 4 + 1;
  return (double)(1.0 / mesh.M2) * (mesh.M2 - row);
}


repo: Jery-Mong/flyfile

decompiled_code:
void __noreturn cmd_quit()
{
bcast_offline();
list_destroy(peer_list);
winds_quit();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
bcast_offline();
list_destroy(peer_list);
winds_quit();
exit(0);
}

original_code:
int cmd_quit(char **cmd)
{
 bcast_offline();
 list_destroy(peer_list);
 winds_quit();
 exit(0);
}

variable_types:
cmd
char **


decompiled_code:
__int64 __fastcall wait_chat_ack(__int64 a1)
{
unsigned int v1; // ebx
__int64 result; // rax

v1 = 10;
while ( 1 )
{
sleep(1u);
m_printf(&unk_7D1, v1);
result = *(unsigned int *)(a1 + 44);
if ( (_DWORD)result == 1 )
break;
if ( !--v1 )
{
*(_DWORD *)(a1 + 44) = 0;
return result;
}
}
return result;
}

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

v1 = 10;
while ( 1 )
{
sleep(1u);
m_printf(&unk_7D1, v1);
result = *(unsigned int *)(a1 + 44);
if ( (_DWORD)result == 1 )
break;
if ( !--v1 )
{
*(_DWORD *)(a1 + 44) = 0;
return result;
}
}
return result;
}

original_code:
static void wait_chat_ack(struct peer *pr)
{
 int i;
 for (i = 10; i > 0; i--) {
  sleep(1);
  m_printf("%d ", i);
  if (pr->chat_rsq_stat == RSP_YES)
   return;
 }
 pr->chat_rsq_stat = RSP_NO;
}

variable_types: struct peer { int idnum; struct base_inf { char name[32]; unsigned int ip; } id; int file_rsq_stat; int chat_rsq_stat; } *

variable_types: i int

user_defined_types: struct peer { int idnum; struct base_inf id; int file_rsq_stat; int chat_rsq_stat; }


decompiled_code:
__int64 cmd_help()
{
char **v0; // rbp
unsigned int i; // ebx
__int64 v2; // rsi

v0 = cmd_list;
for ( i = 0; i != 6; ++i )
{
v2 = i;
v0 += 3;
m_printf("%d. %s%c %s\n", v2);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
char **v0; // rbp
unsigned int i; // ebx
__int64 v2; // rsi

v0 = cmd_list;
for ( i = 0; i != 6; ++i )
{
v2 = i;
v0 += 3;
m_printf("%d. %s%c %s\n", v2);
}
return 0LL;
}

original_code:
int cmd_help(char **cmd)
{
 int cmd_cnt = sizeof(cmd_list) / sizeof(cmd_list[0]);
 int i;
 for (i = 0; i < cmd_cnt; i++) {
  m_printf("%d. %s%c %s\n", i, cmd_list[i].name, 9, cmd_list[i].help_info);
 }
 return 0;
}


decompiled_code:
__int64 __fastcall cmd_chrsq(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
int v4; // ebp
__int64 result; // rax
int v6; // [rsp+0h] [rbp-88h] BYREF
__m128i v7; // [rsp+4h] [rbp-84h]
__m128i v8; // [rsp+14h] [rbp-74h]
int v9; // [rsp+24h] [rbp-64h]

v2 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v2 )
{
v3 = v2;
v6 = 4;
v7 = _mm_loadu_si128(self);
v8 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v9 = *(_DWORD *)(self + 32LL);
v4 = getsockfd(4LL, v2);
send(v4, &v6, 0x6CuLL, 0);
close(v4);
wait_chat_ack(v3);
if ( *(_DWORD *)(v3 + 44) )
m_printf("Ok\n", &v6);
else
m_printf("Rejected\n", &v6);
result = 0LL;
}
else
{
m_printf("No Such Peer\n", a2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbx
int v4; // ebp
__int64 result; // rax
int v6; // [rsp+0h] [rbp-88h] BYREF
__m128i v7; // [rsp+4h] [rbp-84h]
__m128i v8; // [rsp+14h] [rbp-74h]
int v9; // [rsp+24h] [rbp-64h]

v2 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v2 )
{
v3 = v2;
v6 = 4;
v7 = _mm_loadu_si128(self);
v8 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v9 = *(_DWORD *)(self + 32LL);
v4 = getsockfd(4LL, v2);
send(v4, &v6, 0x6CuLL, 0);
close(v4);
func1(v3);
if ( *(_DWORD *)(v3 + 44) )
m_printf("Ok\n", &v6);
else
m_printf("Rejected\n", &v6);
result = 0LL;
}
else
{
m_printf("No Such Peer\n", a2);
result = 0LL;
}
return result;
}

original_code:
int cmd_chrsq(char **cmd)
{
 struct peer *pr = getpeerbyidnum(cmd[1]);
 if (pr == ((void *)0)) {
  m_printf ("No Such Peer\n");
  return 0;
 }
 struct message msg;
 msg.type = MSG_CHAT_RQST;
 memcpy(&msg.id, &self->id, sizeof(struct base_inf));
 int fd = getsockfd(FD_SENDMSG, pr);
 send(fd, &msg, sizeof(msg), 0);
 close(fd);
 wait_chat_ack(pr);
 if (pr->chat_rsq_stat == RSP_NO)
  m_printf ("Rejected\n");
 else
  m_printf ("Ok\n");
 return 0;
}

variable_types: struct message { int type; struct base_inf { char name[32]; unsigned int ip; } id; union { int m_answer; struct file_inf { char fname[64]; unsigned int fsize; } m_file; char m_news[64]; char m_chat[64]; } mdata; }

variable_types: fd int

user_defined_types: struct message { int type; struct base_inf id; union { int m_answer; struct file_inf m_file; char m_news[64]; char m_chat[64]; } mdata; }


decompiled_code:
__int64 cmd_printpr()
{
__int64 v0; // rax
struct in_addr **v1; // rbx
struct in_addr *v2; // rbp

v0 = *(_QWORD *)(peer_list + 8LL);
v1 = *(struct in_addr ***)(v0 + 16);
if ( (struct in_addr **)v0 != v1 )
{
do
{
v2 = *v1;
inet_ntoa((*v1)[9]);
m_printf("%d. %s : %s\n", v2->s_addr);
v1 = (struct in_addr **)v1[2];
}
while ( *(struct in_addr ***)(peer_list + 8LL) != v1 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v0; // rax
struct in_addr **v1; // rbx
struct in_addr *v2; // rbp

v0 = *(_QWORD *)(peer_list + 8LL);
v1 = *(struct in_addr ***)(v0 + 16);
if ( (struct in_addr **)v0 != v1 )
{
do
{
v2 = *v1;
inet_ntoa((*v1)[9]);
m_printf("%d. %s : %s\n", v2->s_addr);
v1 = (struct in_addr **)v1[2];
}
while ( *(struct in_addr ***)(peer_list + 8LL) != v1 );
}
return 0LL;
}

original_code:
int cmd_printpr(char **cmd)
{
 node_t *iter;
 for(iter = (peer_list)->head->next; iter != (peer_list)->head ; iter = iter->next) {
  struct peer *pr = (struct peer *)(iter->data);
  m_printf("%d. %s : %s\n", pr->idnum, pr->id.name, inet_ntoa(*((struct in_addr *)&(pr->id.ip))));
 }
 return 0;
}

canonical_original_code:
int cmd_printpr(char **cmd)
{
 struct list_node *iter;
 for(iter = (peer_list)->head->next; iter != (peer_list)->head ; iter = iter->next) {
  struct peer *pr = (struct peer *)(iter->data);
  m_printf("%d. %s : %s\n", pr->idnum, pr->id.name, inet_ntoa(*((struct in_addr *)&(pr->id.ip))));
 }
 return 0;
}

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

user_defined_types: struct base_inf { char name[32]; unsigned int ip; }


decompiled_code:
__int64 __fastcall cmd_chat(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
const char *v3; // r13
__int64 v4; // rbx
size_t v5; // rax
int v6; // ebp
__int64 result; // rax
int v8[38]; // [rsp+0h] [rbp-98h] BYREF

v1 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v1 )
{
v2 = v1;
if ( *(_DWORD *)(v1 + 44) )
{
v3 = *(const char **)(a1 + 16);
if ( v3 )
{
v4 = a1 + 24;
memset(v8, 0, 0x68uLL);
v8[0] = 7;
v8[26] = 0;
*(__m128i *)&v8[1] = _mm_loadu_si128(self);
*(__m128i *)&v8[5] = _mm_loadu_si128((const __m128i *)(self + 16LL));
v8[9] = *(_DWORD *)(self + 32LL);
do
{
v4 += 8LL;
v5 = strlen((const char *)&v8[10]);
*((_BYTE *)&v8[10] + v5) = 32;
strcpy((char *)&v8[10] + v5 + 1, v3);
v3 = *(const char **)(v4 - 8);
}
while ( v3 );
v6 = getsockfd(4LL, v2);
send(v6, v8, 0x6CuLL, 0);
close(v6);
result = 0LL;
}
else
{
m_printf("No comment\n");
result = 0xFFFFFFFFLL;
}
}
else
{
m_printf("chat with %s is not allowed\n", (const char *)(v1 + 4));
result = 0xFFFFFFFFLL;
}
}
else
{
m_printf("No Such Peer\n");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
const char *v3; // r13
__int64 v4; // rbx
size_t v5; // rax
int v6; // ebp
__int64 result; // rax
int v8[38]; // [rsp+0h] [rbp-98h] BYREF

v1 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v1 )
{
v2 = v1;
if ( *(_DWORD *)(v1 + 44) )
{
v3 = *(const char **)(a1 + 16);
if ( v3 )
{
v4 = a1 + 24;
memset(v8, 0, 0x68uLL);
v8[0] = 7;
v8[26] = 0;
*(__m128i *)&v8[1] = _mm_loadu_si128(self);
*(__m128i *)&v8[5] = _mm_loadu_si128((const __m128i *)(self + 16LL));
v8[9] = *(_DWORD *)(self + 32LL);
do
{
v4 += 8LL;
v5 = strlen((const char *)&v8[10]);
*((_BYTE *)&v8[10] + v5) = 32;
strcpy((char *)&v8[10] + v5 + 1, v3);
v3 = *(const char **)(v4 - 8);
}
while ( v3 );
v6 = getsockfd(4LL, v2);
send(v6, v8, 0x6CuLL, 0);
close(v6);
result = 0LL;
}
else
{
m_printf("No comment\n");
result = 0xFFFFFFFFLL;
}
}
else
{
m_printf("chat with %s is not allowed\n", (const char *)(v1 + 4));
result = 0xFFFFFFFFLL;
}
}
else
{
m_printf("No Such Peer\n");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int cmd_chat(char **cmd)
{
 struct peer *pr = getpeerbyidnum(cmd[1]);
 if (pr == ((void *)0)) {
  m_printf ("No Such Peer\n");
  return -1;
 }
 if (pr->chat_rsq_stat == RSP_NO) {
  m_printf ("chat with %s is not allowed\n", pr->id.name);
  return -1;
 }
 if (cmd[2] == ((void *)0)) {
  m_printf ("No comment\n");
  return -1;
 }
 struct message msg;
 memset(&msg, 0, sizeof(struct message));
 msg.type = MSG_CHAT;
 memcpy(&msg.id, &self->id, sizeof(struct base_inf));
 int i;
 for (i = 2; cmd[i] != ((void *)0); i++) {
  strcat(msg.mdata.m_chat, " ");
  strcat(msg.mdata.m_chat, cmd[i]);
 }
 int fd = getsockfd(FD_SENDMSG, pr);
 send(fd, &msg, sizeof(msg), 0);
 close(fd);
 return 0;
}

user_defined_types: struct file_inf { char fname[64]; unsigned int fsize; }


original_code:
static void free_cmd(char **cmd)
{
 if (cmd == ((void *)0))
  return;
 int i;
 for (i = 0; cmd[i] != ((void *)0); i++)
  free(cmd[i]);
}


decompiled_code:
__int64 __fastcall is_cmd(char *s1)
{
__int64 v1; // rax
char v2; // dl
size_t v3; // r12
const char **v4; // rbx

if ( (*s1 & 0xDF) != 0 )
{
v1 = 1LL;
do
{
v2 = s1[v1];
v3 = v1++;
}
while ( (v2 & 0xDF) != 0 );
}
else
{
v3 = 0LL;
}
v4 = (const char **)cmd_list;
while ( strncmp(s1, *v4, v3) )
{
v4 += 3;
if ( (const char **)(unsigned int)((_DWORD)cmd_list + 144) == v4 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s1)
{
__int64 v1; // rax
char v2; // dl
size_t v3; // r12
const char **v4; // rbx

if ( (*s1 & 0xDF) != 0 )
{
v1 = 1LL;
do
{
v2 = s1[v1];
v3 = v1++;
}
while ( (v2 & 0xDF) != 0 );
}
else
{
v3 = 0LL;
}
v4 = (const char **)cmd_list;
while ( strncmp(s1, *v4, v3) )
{
v4 += 3;
if ( (const char **)(unsigned int)((_DWORD)cmd_list + 144) == v4 )
return 0LL;
}
return 1LL;
}

original_code:
static int is_cmd(const char *cmd)
{
 int i, j;
 for (i = 0; cmd[i] != ' ' && cmd[i] != '\0'; i++);
 for (j = 0; j < sizeof(cmd_list) / sizeof(cmd_list[0]); j++) {
  if (!strncmp(cmd, cmd_list[j].name, i))
   return 1;
 }
 return 0;
}

canonical_original_code:
static int is_cmd(char *cmd)
{
 int i; int j;
 for (i = 0; cmd[i] != ' ' && cmd[i] != '\0'; i++);
 for (j = 0; j < sizeof(cmd_list) / sizeof(cmd_list[0]); j++) {
  if (!strncmp(cmd, cmd_list[j].name, i))
   return 1;
 }
 return 0;
}

variable_types:
cmd
char *

variable_types: j int


decompiled_code:
__int64 __fastcall wait_file_ack(__int64 a1)
{
unsigned int v1; // ebx
__int64 result; // rax

v1 = 10;
while ( 1 )
{
sleep(1u);
m_printf("%d ", v1);
result = *(unsigned int *)(a1 + 40);
if ( (_DWORD)result == 1 )
break;
if ( !--v1 )
{
*(_DWORD *)(a1 + 40) = 0;
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned int v1; // ebx
__int64 result; // rax

v1 = 10;
while ( 1 )
{
sleep(1u);
m_printf("%d ", v1);
result = *(unsigned int *)(a1 + 40);
if ( (_DWORD)result == 1 )
break;
if ( !--v1 )
{
*(_DWORD *)(a1 + 40) = 0;
return result;
}
}
return result;
}

original_code:
void wait_file_ack(struct peer *pr)
{
 int i;
 for (i = 10; i > 0; i--) {
  sleep(1);
  m_printf("%d ", i);
  if (pr->file_rsq_stat == RSP_YES)
   return;
 }
 pr->file_rsq_stat = RSP_NO;
}


decompiled_code:
char *__fastcall get_filename(char *dest, char *s)
{
char *v2; // rax

v2 = strrchr(s, 47);
if ( v2 )
s = v2 + 1;
return strcpy(dest, s);
}

canonical_decompiled_code:
char *__fastcall func9(char *dest, char *s)
{
char *v2; // rax

v2 = strrchr(s, 47);
if ( v2 )
s = v2 + 1;
return strcpy(dest, s);
}

original_code:
void get_filename(char *buf, char *path)
{
 char *p;
 p = strrchr(path, '/');
 if (p == ((void *)0))
  strcpy(buf, path);
 else
  strcpy(buf, p + 1);
}

variable_types:
buf
char *

variable_types:
path
char *

variable_types:
p
char *


decompiled_code:
__int64 __fastcall cmd_send(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
unsigned int v3; // er12
char *v5; // rsi
int v6; // er13
int v7; // ebp
__int64 v8; // rsi
int v9; // [rsp+0h] [rbp-128h] BYREF
__m128i v10; // [rsp+4h] [rbp-124h]
__m128i v11; // [rsp+14h] [rbp-114h]
int v12; // [rsp+24h] [rbp-104h]
char dest[64]; // [rsp+28h] [rbp-100h] BYREF
int v14; // [rsp+68h] [rbp-C0h]
struct stat v15; // [rsp+70h] [rbp-B8h] BYREF

v1 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v1 && (v2 = v1, (v3 = stat(*(const char **)(a1 + 16), &v15)) == 0) )
{
v5 = *(char **)(a1 + 16);
v9 = 2;
v10 = _mm_loadu_si128(self);
v11 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v12 = *(_DWORD *)(self + 32LL);
get_filename(dest, v5);
v14 = v15.st_size;
v6 = getsockfd(4LL, v2);
send(v6, &v9, 0x6CuLL, 0);
close(v6);
wait_file_ack(v2);
if ( *(_DWORD *)(v2 + 40) && (v7 = getsockfd(1LL, v2), v7 >= 0) )
{
v8 = *(_QWORD *)(a1 + 16);
v3 = 1;
*(_DWORD *)(self + 48LL) = 0;
send_file((unsigned int)v7, v8);
*(_DWORD *)(self + 48LL) = 1;
close(v7);
}
else
{
m_printf("rejected\n");
}
}
else
{
v3 = 0;
m_printf("Invalid\n");
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbp
unsigned int v3; // er12
char *v5; // rsi
int v6; // er13
int v7; // ebp
__int64 v8; // rsi
int v9; // [rsp+0h] [rbp-128h] BYREF
__m128i v10; // [rsp+4h] [rbp-124h]
__m128i v11; // [rsp+14h] [rbp-114h]
int v12; // [rsp+24h] [rbp-104h]
char dest[64]; // [rsp+28h] [rbp-100h] BYREF
int v14; // [rsp+68h] [rbp-C0h]
struct stat v15; // [rsp+70h] [rbp-B8h] BYREF

v1 = getpeerbyidnum(*(_QWORD *)(a1 + 8));
if ( v1 && (v2 = v1, (v3 = stat(*(const char **)(a1 + 16), &v15)) == 0) )
{
v5 = *(char **)(a1 + 16);
v9 = 2;
v10 = _mm_loadu_si128(self);
v11 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v12 = *(_DWORD *)(self + 32LL);
func9(dest, v5);
v14 = v15.st_size;
v6 = getsockfd(4LL, v2);
send(v6, &v9, 0x6CuLL, 0);
close(v6);
func8(v2);
if ( *(_DWORD *)(v2 + 40) && (v7 = getsockfd(1LL, v2), v7 >= 0) )
{
v8 = *(_QWORD *)(a1 + 16);
v3 = 1;
*(_DWORD *)(self + 48LL) = 0;
send_file((unsigned int)v7, v8);
*(_DWORD *)(self + 48LL) = 1;
close(v7);
}
else
{
m_printf("rejected\n");
}
}
else
{
v3 = 0;
m_printf("Invalid\n");
}
return v3;
}

original_code:
int cmd_send(char **cmd)
{
 struct peer *pr = getpeerbyidnum(cmd[1]);
 if (pr == ((void *)0))
  goto err;
 struct stat st;
 if (stat(cmd[2], &st))
  goto err;
 struct message msg;
 msg.type = MSG_FILE_RQST;
 memcpy(&msg.id, &self->id, sizeof(struct base_inf));
 get_filename(msg.mdata.m_file.fname, cmd[2]);
 msg.mdata.m_file.fsize = st.st_size;
 int fd = getsockfd(FD_SENDMSG, pr);
 send(fd, &msg, sizeof(msg), 0);
 close(fd);
 wait_file_ack(pr);
 if (pr->file_rsq_stat == RSP_NO)
  goto out;
 fd = getsockfd(FD_DATA_SEND, pr);
 if (fd < 0)
  goto out;
 self->file_status = FILE_BUSY;
 send_file(fd, cmd[2]);
 self->file_status = FILE_AVAL;
 close(fd);
 return 1;
err:
 m_printf("Invalid\n");
 return 0;
out:
 m_printf("rejected\n");
 return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


original_code:
char **cmdstr_to_list(char *raw_cmd)
{
 int i;
 for (i = 0; raw_cmd[i] == ' '; i++);
 raw_cmd += i;
 if (*raw_cmd == '\0')
  goto out2;
 char **args_list = (char **)malloc(sizeof(char **) * 16);
 memset(args_list, 0, sizeof(char **) * 8);
 if (!is_cmd(raw_cmd))
  goto out1;
 int k = 0;
 for (;;) {
  for (i = 0; raw_cmd[i] != ' ' && raw_cmd[i] != '\0'; i++);
  args_list[k] = (char *)malloc(i + 1);
  strncpy(args_list[k], raw_cmd, i);
  args_list[k][i] = '\0';
  raw_cmd += i;
  for (; *raw_cmd == ' '; raw_cmd++);
  if (*raw_cmd == '\0')
   return args_list;
  k++;
 }
out1:
 free(args_list);
out2:
 return ((void *)0);
}

variable_types: k int

return_type: char **


decompiled_code:
__int64 __fastcall cmd_handler(void **a1)
{
const char *v1; // rbp
__int64 v2; // rbx

v1 = (const char *)*a1;
v2 = 0LL;
while ( strcmp(v1, cmd_list[3 * v2]) )
{
if ( ++v2 == 6 )
return 0xFFFFFFFFLL;
}
((void (__fastcall *)(void **))cmd_list[3 * (int)v2 + 2])(a1);
free_cmd(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(void **a1)
{
const char *v1; // rbp
__int64 v2; // rbx

v1 = (const char *)*a1;
v2 = 0LL;
while ( strcmp(v1, cmd_list[3 * v2]) )
{
if ( ++v2 == 6 )
return 0xFFFFFFFFLL;
}
((void (__fastcall *)(void **))cmd_list[3 * (int)v2 + 2])(a1);
func6(a1);
return 0LL;
}

original_code:
int cmd_handler(char **cmd)
{
 int i;
 for (i = 0; i < sizeof(cmd_list) / sizeof(cmd_list[0]); i++)
  if (!strcmp(cmd[0], cmd_list[i].name)) {
   cmd_list[i].cmd_func(cmd);
   free_cmd(cmd);
   return 0;
  }
 return -1;
}


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

global_init(argc, argv, envp);
pthread_create(newthread, 0LL, (void *(*)(void *))&recv_msg, 0LL);
bcast_online();
while ( 1 )
main_wind();
}

original_code:
int main()
{
 pthread_t tid1;
 global_init();
 pthread_create(&tid1, ((void *)0), recv_msg, ((void *)0));
 bcast_online();
 while(1)
  main_wind();
}

canonical_original_code:
int main()
{
 unsigned long tid1;
 global_init();
 pthread_create(&tid1, ((void *)0), recv_msg, ((void *)0));
 bcast_online();
 while(1)
  main_wind();
}


decompiled_code:
void sig_do_nothing()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void sig_do_nothing(int signo)
{
}

variable_types: signo int


decompiled_code:
__int64 __fastcall popwin_getrsp(__int64 a1)
{
signal(10, (__sighandler_t)sig_do_nothing);
display_popinf(a1);
pop_flag = 1;
pause();
return (unsigned int)dword_720;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
signal(10, (__sighandler_t)sig_do_nothing);
func1(a1);
pop_flag = 1;
pause();
return (unsigned int)dword_720;
}

original_code:
int popwin_getrsp(void *data)
{
 signal(10, sig_do_nothing);
 display_popinf((struct message *)data);
 pop_flag = 1;
 pause();
 int ret = rsp;
 return ret;
}

variable_types:
data
void *

variable_types: ret int


decompiled_code:
int winds_quit()
{
del_panel(pn_inf);
del_panel(pn_main);
del_panel(pn_prgs);
return endwin();
}

canonical_decompiled_code:
int func8()
{
del_panel(pn_inf);
del_panel(pn_main);
del_panel(pn_prgs);
return endwin();
}

original_code:
void winds_quit()
{
 del_panel(pn_inf);
 del_panel(pn_main);
 del_panel(pn_prgs);
 endwin();
}


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

v0 = (char *)calloc(0x38uLL, 1uLL);
*((_DWORD *)v0 + 12) = 1;
self = v0;
gethostname(v0, 0x20uLL);
get_local_ipinf(self + 32LL, self + 36LL);
if ( !*(_DWORD *)(self + 32LL) )
{
puts("error: you don't seem to have connected to LAN, please check your network");
exit(1);
}
peer_list = malloc(0x10uLL);
list_init(peer_list);
return winds_init();
}

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

v0 = (char *)calloc(0x38uLL, 1uLL);
*((_DWORD *)v0 + 12) = 1;
self = v0;
gethostname(v0, 0x20uLL);
get_local_ipinf(self + 32LL, self + 36LL);
if ( !*(_DWORD *)(self + 32LL) )
{
puts("error: you don't seem to have connected to LAN, please check your network");
exit(1);
}
peer_list = malloc(0x10uLL);
list_init(peer_list);
return winds_init();
}

original_code:
void global_init()
{
 self = (struct host *)malloc(sizeof(struct host));
 memset(self, 0, sizeof(struct host));
 self->file_status = FILE_AVAL;
 gethostname(self->id.name, 32);
 get_local_ipinf(&(self->id.ip), &self->bcastaddr);
 if (!self->id.ip) {
  printf("error: you don't seem to have connected to LAN, please check your network\n");
  exit(1);
 }
 peer_list = (list_t *)malloc(sizeof(list_t));
 list_init(peer_list);
 winds_init();
}

canonical_original_code:
void global_init()
{
 self = (struct host *)malloc(sizeof(struct host));
 memset(self, 0, sizeof(struct host));
 self->file_status = FILE_AVAL;
 gethostname(self->id.name, 32);
 get_local_ipinf(&(self->id.ip), &self->bcastaddr);
 if (!self->id.ip) {
  printf("error: you don't seem to have connected to LAN, please check your network\n");
  exit(1);
 }
 peer_list = (struct list *)malloc(sizeof(list_t));
 list_init(peer_list);
 winds_init();
}

user_defined_types: struct host { struct base_inf id; unsigned int bcastaddr; unsigned long msg_handler_thr; int file_status; }

user_defined_types: struct list { int node_cnt; struct list_node *head; }

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


getpeerbyid

decompiled_code:
__int64 __fastcall getpeerbyid(__int64 a1)
{
_QWORD *v1; // rdx
_QWORD *v2; // rax

v1 = *(_QWORD **)(peer_list + 8LL);
v2 = (_QWORD *)v1[2];
if ( v1 == v2 )
return 0LL;
while ( *(_DWORD *)(a1 + 32) != *(_DWORD *)(*v2 + 36LL) )
{
v2 = (_QWORD *)v2[2];
if ( v1 == v2 )
return 0LL;
}
return *v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
_QWORD *v1; // rdx
_QWORD *v2; // rax

v1 = *(_QWORD **)(peer_list + 8LL);
v2 = (_QWORD *)v1[2];
if ( v1 == v2 )
return 0LL;
while ( *(_DWORD *)(a1 + 32) != *(_DWORD *)(*v2 + 36LL) )
{
v2 = (_QWORD *)v2[2];
if ( v1 == v2 )
return 0LL;
}
return *v2;
}

original_code:
struct peer *getpeerbyid(struct base_inf *id)
{
 node_t *iter;
 for(iter = (peer_list)->head->next; iter != (peer_list)->head ; iter = iter->next) {
  if (id->ip == ((struct peer *)(iter->data))->id.ip)
   return iter->data;
 }
 return ((void *)0);
}

canonical_original_code:
struct peer *getpeerbyid(struct base_inf *id)
{
 struct list_node *iter;
 for(iter = (peer_list)->head->next; iter != (peer_list)->head ; iter = iter->next) {
  if (id->ip == ((struct peer *)(iter->data))->id.ip)
   return iter->data;
 }
 return ((void *)0);
}

variable_types:
id
struct base_inf *

return_type: struct peer { int idnum; struct base_inf { char name[32]; unsigned int ip; } id; int file_rsq_stat; int chat_rsq_stat; } *


decompiled_code:
void __fastcall peer_outlist(__int64 a1)
{
__int64 v1; // rax
void *v2; // rbp

v1 = getpeerbyid(a1 + 4);
if ( v1 )
{
v2 = (void *)v1;
list_del_node(peer_list, v1);
free(v2);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
__int64 v1; // rax
void *v2; // rbp

v1 = func3(a1 + 4);
if ( v1 )
{
v2 = (void *)v1;
list_del_node(peer_list, v1);
free(v2);
}
}

original_code:
void peer_outlist(struct message *msg)
{
 struct peer *pr = getpeerbyid(&msg->id);
 if (pr == ((void *)0))
  return;
 list_del_node(peer_list, pr);
 free(pr);
}

variable_types: struct message { int type; struct base_inf { char name[32]; unsigned int ip; } id; union { int m_answer; struct file_inf { char fname[64]; unsigned int fsize; } m_file; char m_news[64]; char m_chat[64]; } mdata; } *


decompiled_code:
void __fastcall respond_rqst(void *ptr)
{
__int64 v1; // rax
__int64 v2; // r12
int v3; // ebx
int v4; // eax
int v5; // er13
int v6; // er12
int v7; // [rsp+0h] [rbp-98h] BYREF
__m128i v8; // [rsp+4h] [rbp-94h]
__m128i v9; // [rsp+14h] [rbp-84h]
int v10; // [rsp+24h] [rbp-74h]
int v11; // [rsp+28h] [rbp-70h]

v1 = getpeerbyid((__int64)ptr + 4);
if ( v1 )
{
v2 = v1;
if ( *(_DWORD *)(self + 48LL) )
{
v3 = popwin_getrsp(ptr);
v7 = 2 * (*(_DWORD *)ptr != 2) + 3;
v8 = _mm_loadu_si128(self);
v9 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v4 = *(_DWORD *)(self + 32LL);
v11 = v3;
v10 = v4;
v5 = getsockfd(4LL, v2);
send(v5, &v7, 0x6CuLL, 0);
close(v5);
if ( v3 )
{
m_printf("You Answer Is [Yes]\n");
if ( *(_DWORD *)ptr != 4 )
{
LABEL_6:
v6 = getsockfd(2LL, v2);
if ( v6 >= 0 )
{
*(_DWORD *)(self + 48LL) = 0;
recv_file((unsigned int)v6, (char *)ptr + 40);
*(_DWORD *)(self + 48LL) = 1;
close(v6);
}
goto LABEL_8;
}
}
else
{
m_printf("You Answer Is [No]\n");
if ( *(_DWORD *)ptr != 4 )
goto LABEL_6;
}
*(_DWORD *)(v2 + 44) = v3;
LABEL_8:
free(ptr);
return;
}
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
__int64 v1; // rax
__int64 v2; // r12
int v3; // ebx
int v4; // eax
int v5; // er13
int v6; // er12
int v7; // [rsp+0h] [rbp-98h] BYREF
__m128i v8; // [rsp+4h] [rbp-94h]
__m128i v9; // [rsp+14h] [rbp-84h]
int v10; // [rsp+24h] [rbp-74h]
int v11; // [rsp+28h] [rbp-70h]

v1 = func3((__int64)ptr + 4);
if ( v1 )
{
v2 = v1;
if ( *(_DWORD *)(self + 48LL) )
{
v3 = popwin_getrsp(ptr);
v7 = 2 * (*(_DWORD *)ptr != 2) + 3;
v8 = _mm_loadu_si128(self);
v9 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v4 = *(_DWORD *)(self + 32LL);
v11 = v3;
v10 = v4;
v5 = getsockfd(4LL, v2);
send(v5, &v7, 0x6CuLL, 0);
close(v5);
if ( v3 )
{
m_printf("You Answer Is [Yes]\n");
if ( *(_DWORD *)ptr != 4 )
{
LABEL_6:
v6 = getsockfd(2LL, v2);
if ( v6 >= 0 )
{
*(_DWORD *)(self + 48LL) = 0;
recv_file((unsigned int)v6, (char *)ptr + 40);
*(_DWORD *)(self + 48LL) = 1;
close(v6);
}
goto LABEL_8;
}
}
else
{
m_printf("You Answer Is [No]\n");
if ( *(_DWORD *)ptr != 4 )
goto LABEL_6;
}
*(_DWORD *)(v2 + 44) = v3;
LABEL_8:
free(ptr);
return;
}
}
}

original_code:
void respond_rqst(struct message *msg)
{
 struct peer *pr;
 int rsp;
 struct message r_msg;
 if ((pr = getpeerbyid(&msg->id)) == ((void *)0))
  return;
 if (self->file_status == FILE_BUSY)
  return;
 else
  rsp = popwin_getrsp(msg);
 if (msg->type == MSG_FILE_RQST)
  r_msg.type = MSG_FILE_ACK;
 else
  r_msg.type = MSG_CHAT_ACK;
 memcpy(&r_msg.id, &self->id, sizeof(struct base_inf));
 r_msg.mdata.m_answer = rsp;
 int fd = getsockfd(FD_SENDMSG, pr);
 send(fd, &r_msg, sizeof(struct message), 0);
 close(fd);
 if (rsp == RSP_NO)
  m_printf("You Answer Is [No]\n");
 else
  m_printf("You Answer Is [Yes]\n");
 if (msg->type == MSG_CHAT_RQST) {
  pr->chat_rsq_stat = rsp;
  goto out;
 }
 fd = getsockfd(FD_DATA_RECV, pr);
 if (fd < 0)
  goto out;
 self->file_status = FILE_BUSY;
 recv_file(fd, &msg->mdata.m_file);
 self->file_status = FILE_AVAL;
 close(fd);
out:
 free(msg);
}

variable_types: struct peer { int idnum; struct base_inf { char name[32]; unsigned int ip; } id; int file_rsq_stat; int chat_rsq_stat; } *

variable_types: rsp int


decompiled_code:
__int64 __fastcall handle_ack(_DWORD *a1)
{
__int64 result; // rax
int v2; // edx

result = getpeerbyid((__int64)(a1 + 1));
if ( result )
{
v2 = a1[10];
if ( *a1 == 3 )
*(_DWORD *)(result + 40) = v2;
else
*(_DWORD *)(result + 44) = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
__int64 result; // rax
int v2; // edx

result = func3((__int64)(a1 + 1));
if ( result )
{
v2 = a1[10];
if ( *a1 == 3 )
*(_DWORD *)(result + 40) = v2;
else
*(_DWORD *)(result + 44) = v2;
}
return result;
}

original_code:
void handle_ack(struct message *msg)
{
 struct peer *pr;
 if ((pr = getpeerbyid(&msg->id)) == ((void *)0))
  return;
 if (msg->type == MSG_FILE_ACK) {
  pr->file_rsq_stat = msg->mdata.m_answer;
 } else {
  pr->chat_rsq_stat = msg->mdata.m_answer;
 }
}


decompiled_code:
void __fastcall chat_get_comment(void *ptr)
{
char *v2; // rdi
unsigned int *v3; // rax

v2 = (char *)ptr + 4;
v3 = (unsigned int *)getpeerbyid((__int64)v2);
if ( v3 && v3[11] )
{
print_chat_comment(v2, *v3, (char *)ptr + 40);
free(ptr);
}
else
{
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
char *v2; // rdi
unsigned int *v3; // rax

v2 = (char *)ptr + 4;
v3 = (unsigned int *)func3((__int64)v2);
if ( v3 && v3[11] )
{
print_chat_comment(v2, *v3, (char *)ptr + 40);
free(ptr);
}
else
{
free(ptr);
}
}

original_code:
void chat_get_comment(struct message *msg)
{
 struct peer *pr;
 if ((pr = getpeerbyid(&msg->id)) == ((void *)0))
  goto out;
 if (pr->chat_rsq_stat == RSP_NO)
  goto out;
 print_chat_comment(msg->id.name, pr->idnum, msg->mdata.m_chat);
out:
 free(msg);
}


getsockfd

decompiled_code:
__int64 __fastcall getsockfd(int a1, __int64 a2)
{
int v2; // edx
int v3; // eax
unsigned int v4; // er12
int v5; // edx
int v7; // edx
int v8; // ebp
int v9; // edx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr.sa_family = 2;
switch ( a1 )
{
case 0:
case 4:
*(_WORD *)addr.sa_data = _bswap_16(3333);
v3 = socket(2, 2, v2);
v4 = v3;
if ( a1 )
{
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a2 + 36);
}
else
{
optval = 1;
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(self + 36LL);
setsockopt(v3, 1, 6, &optval, 4u);
}
goto LABEL_8;
case 1:
*(_WORD *)addr.sa_data = _bswap_16(3334);
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a2 + 36);
v4 = socket(2, 1, v9);
LABEL_8:
if ( connect(v4, &addr, 0x10u) < 0 )
v4 = -1;
return v4;
case 2:
*(_WORD *)addr.sa_data = _bswap_16(3334);
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(self + 32LL);
v8 = socket(2, 1, v7);
bind(v8, &addr, 0x10u);
listen(v8, 1);
return (unsigned int)accept(v8, 0LL, 0LL);
case 3:
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = _bswap_16(3333);
v4 = socket(2, 2, v5);
bind(v4, &addr, 0x10u);
return v4;
default:
JUMPOUT(0x4BFLL);
}
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
int v2; // edx
int v3; // eax
unsigned int v4; // er12
int v5; // edx
int v7; // edx
int v8; // ebp
int v9; // edx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr.sa_family = 2;
switch ( a1 )
{
case 0:
case 4:
*(_WORD *)addr.sa_data = func0(3333);
v3 = socket(2, 2, v2);
v4 = v3;
if ( a1 )
{
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a2 + 36);
}
else
{
optval = 1;
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(self + 36LL);
setsockopt(v3, 1, 6, &optval, 4u);
}
goto LABEL_8;
case 1:
*(_WORD *)addr.sa_data = func0(3334);
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(a2 + 36);
v4 = socket(2, 1, v9);
LABEL_8:
if ( connect(v4, &addr, 0x10u) < 0 )
v4 = -1;
return v4;
case 2:
*(_WORD *)addr.sa_data = func0(3334);
*(_DWORD *)&addr.sa_data[2] = *(_DWORD *)(self + 32LL);
v8 = socket(2, 1, v7);
bind(v8, &addr, 0x10u);
listen(v8, 1);
return (unsigned int)accept(v8, 0LL, 0LL);
case 3:
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = func0(3333);
v4 = socket(2, 2, v5);
bind(v4, &addr, 0x10u);
return v4;
default:
JUMPOUT(0x4BFLL);
}
}

original_code:
int getsockfd(int type, void *data)
{
 struct peer *pr = (struct peer *)data;
 int fd;
 struct sockaddr_in addr;
 addr.sin_family = 2;
 switch (type) {
 case FD_GETMSG:
  addr.sin_port = htons(3333);
  addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
  fd = socket(2, SOCK_DGRAM, 0);
  bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  return fd;
 case FD_BROADCAST:
 case FD_SENDMSG:
  addr.sin_port = htons(3333);
  fd = socket(2, SOCK_DGRAM, 0);
  if (type == FD_BROADCAST) {
   addr.sin_addr.s_addr = self->bcastaddr;
   int broadcast = 1;
   setsockopt(fd, 1, 6, &broadcast, sizeof(int));
  } else {
   addr.sin_addr.s_addr = pr->id.ip;
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
   return -1;
  return fd;
 case FD_DATA_SEND:
  addr.sin_port = htons(3334);
  addr.sin_addr.s_addr = pr->id.ip;
  fd = socket(2, SOCK_STREAM, 0);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
   return -1;
  return fd;
 case FD_DATA_RECV:
  addr.sin_port = htons(3334);
  addr.sin_addr.s_addr = self->id.ip;
  fd = socket(2, SOCK_STREAM, 0);
  bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  listen(fd, 1);
  return accept(fd, ((void *)0), ((void *)0));
 default:
  break;
 }
 return -1;
}

canonical_original_code:
int getsockfd(int type, void *data)
{
 struct peer *pr = (struct peer *)data;
 int fd;
 struct sockaddr_in addr;
 addr.sin_family = 2;
 switch (type) {
 case FD_GETMSG:
  addr.sin_port = htons(3333);
  addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
  fd = socket(2, SOCK_DGRAM, 0);
  bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  return fd;
 case FD_BROADCAST:
 case FD_SENDMSG:
  addr.sin_port = htons(3333);
  fd = socket(2, SOCK_DGRAM, 0);
  if (type == FD_BROADCAST) {
   addr.sin_addr.s_addr = self->bcastaddr;
   int broadcast = 1;
   setsockopt(fd, 1, 6, &broadcast, sizeof(int));
  } else {
   addr.sin_addr.s_addr = pr->id.ip;
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
   return -1;
  return fd;
 case FD_DATA_SEND:
  addr.sin_port = htons(3334);
  addr.sin_addr.s_addr = pr->id.ip;
  fd = socket(2, SOCK_STREAM, 0);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
   return -1;
  return fd;
 case FD_DATA_RECV:
  addr.sin_port = htons(3334);
  addr.sin_addr.s_addr = self->id.ip;
  fd = socket(2, SOCK_STREAM, 0);
  bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  listen(fd, 1);
  return accept(fd, ((void *)0), ((void *)0));
 default:
  break;
 }
 return -1;
}

variable_types: type 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: broadcast int

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

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


decompiled_code:
int __fastcall bcast_status(int a1)
{
int v1; // ebp
int v3; // [rsp+0h] [rbp-88h] BYREF
__m128i v4; // [rsp+4h] [rbp-84h]
__m128i v5; // [rsp+14h] [rbp-74h]
int v6; // [rsp+24h] [rbp-64h]

v3 = a1;
v1 = getsockfd(0, 0LL);
v4 = _mm_loadu_si128(self);
v5 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v6 = *(_DWORD *)(self + 32LL);
send(v1, &v3, 0x6CuLL, 0);
return close(v1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
int v1; // ebp
int v3; // [rsp+0h] [rbp-88h] BYREF
__m128i v4; // [rsp+4h] [rbp-84h]
__m128i v5; // [rsp+14h] [rbp-74h]
int v6; // [rsp+24h] [rbp-64h]

v3 = a1;
v1 = func1(0, 0LL);
v4 = _mm_loadu_si128(self);
v5 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v6 = *(_DWORD *)(self + 32LL);
send(v1, &v3, 0x6CuLL, 0);
return close(v1);
}

original_code:
void bcast_status(int status)
{
 int fd = getsockfd(FD_BROADCAST, ((void *)0));
 struct message bmsg;
 bmsg.type = status;
 memcpy(&bmsg.id, &self->id, sizeof(struct base_inf));
 send(fd, &bmsg, sizeof(struct message), 0);
 close(fd);
}

variable_types: status int

variable_types: fd int

variable_types: struct message { int type; struct base_inf { char name[32]; unsigned int ip; } id; union { int m_answer; struct file_inf { char fname[64]; unsigned int fsize; } m_file; char m_news[64]; char m_chat[64]; } mdata; }


decompiled_code:
int bcast_online()
{
return bcast_status(0);
}

canonical_decompiled_code:
int func3()
{
return func2(0);
}

original_code:
void bcast_online()
{
 bcast_status(MSG_ONLINE);
}


decompiled_code:
int bcast_offline()
{
return bcast_status(1);
}

canonical_decompiled_code:
int func4()
{
return func2(1);
}

original_code:
void bcast_offline()
{
 bcast_status(MSG_OFFLINE);
}


decompiled_code:
int __fastcall get_local_ipinf(int *a1, int *a2)
{
in_addr_t v2; // ebp
int v3; // er14
__int64 v4; // rbx
unsigned __int64 v5; // rdx
char *v6; // rcx
int result; // eax
int v8; // [rsp+0h] [rbp-38h] BYREF
char *v9; // [rsp+8h] [rbp-30h]

v8 = 160;
v2 = inet_addr("192.168.0.0");
v9 = (char *)malloc(0xA0uLL);
v3 = socket(2, 2, 0);
if ( ioctl(v3, 0x8912uLL, &v8) < 0 )
perror("ioctl");
v4 = (__int64)v9;
v5 = v8 / 0x28uLL;
if ( (int)v5 <= 0 )
return close(v3);
v6 = &v9[40 * (unsigned int)(v5 - 1) + 40];
while ( 1 )
{
result = *(_DWORD *)(v4 + 20);
if ( (result & v2) == v2 )
break;
v4 += 40LL;
if ( v6 == (char *)v4 )
return close(v3);
}
if ( a1 )
*a1 = result;
if ( a2 )
{
ioctl(v3, 0x8919uLL, v4, v6);
result = *(_DWORD *)(v4 + 20);
*a2 = result;
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int *a1, int *a2)
{
in_addr_t v2; // ebp
int v3; // er14
__int64 v4; // rbx
unsigned __int64 v5; // rdx
char *v6; // rcx
int result; // eax
int v8; // [rsp+0h] [rbp-38h] BYREF
char *v9; // [rsp+8h] [rbp-30h]

v8 = 160;
v2 = inet_addr("192.168.0.0");
v9 = (char *)malloc(0xA0uLL);
v3 = socket(2, 2, 0);
if ( ioctl(v3, 0x8912uLL, &v8) < 0 )
perror("ioctl");
v4 = (__int64)v9;
v5 = v8 / 0x28uLL;
if ( (int)v5 <= 0 )
return close(v3);
v6 = &v9[40 * (unsigned int)(v5 - 1) + 40];
while ( 1 )
{
result = *(_DWORD *)(v4 + 20);
if ( (result & v2) == v2 )
break;
v4 += 40LL;
if ( v6 == (char *)v4 )
return close(v3);
}
if ( a1 )
*a1 = result;
if ( a2 )
{
ioctl(v3, 0x8919uLL, v4, v6);
result = *(_DWORD *)(v4 + 20);
*a2 = result;
}
return result;
}

original_code:
void get_local_ipinf(void *lip, void *bip)
{
 int fd;
 in_addr_t ip;
 struct sockaddr_in *addr;
 in_addr_t mask = inet_addr("192.168.0.0");
 struct ifconf ifc;
 struct ifreq *ifr;
 ifc.ifc_len = sizeof(struct ifreq) * 4;
 ifc.ifc_ifcu.ifcu_req = (struct ifreq *)malloc(ifc.ifc_len);
 fd = socket(2, SOCK_DGRAM, 0);
 if (ioctl(fd, 0x8912, &ifc) < 0)
  perror("ioctl");
 int i = ifc.ifc_len / sizeof(struct ifreq);
 for (ifr = ifc.ifc_ifcu.ifcu_req; i > 0; i--) {
  addr = (struct sockaddr_in *)&ifr->ifr_ifru.ifru_addr;
  ip = addr->sin_addr.s_addr;
  if ((ip & mask) == mask) {
   if (lip != ((void *)0))
    *(in_addr_t *)lip = ip;
   if (bip != ((void *)0)) {
    ioctl(fd, 0x8919, ifr);
    *(in_addr_t *)bip = (((struct sockaddr_in *)&ifr->ifr_ifru.ifru_addr)->sin_addr).s_addr;
   }
   return;
  }
  ifr++;
 }
 close(fd);
}

canonical_original_code:
void get_local_ipinf(void *lip, void *bip)
{
 int fd;
 unsigned int ip;
 struct sockaddr_in *addr;
 unsigned int mask = inet_addr("192.168.0.0");
 struct ifconf ifc;
 struct ifreq *ifr;
 ifc.ifc_len = sizeof(struct ifreq) * 4;
 ifc.ifc_ifcu.ifcu_req = (struct ifreq *)malloc(ifc.ifc_len);
 fd = socket(2, SOCK_DGRAM, 0);
 if (ioctl(fd, 0x8912, &ifc) < 0)
  perror("ioctl");
 int i = ifc.ifc_len / sizeof(struct ifreq);
 for (ifr = ifc.ifc_ifcu.ifcu_req; i > 0; i--) {
  addr = (struct sockaddr_in *)&ifr->ifr_ifru.ifru_addr;
  ip = addr->sin_addr.s_addr;
  if ((ip & mask) == mask) {
   if (lip != ((void *)0))
    *(unsigned int *)lip = ip;
   if (bip != ((void *)0)) {
    ioctl(fd, 0x8919, ifr);
    *(unsigned int *)bip = (((struct sockaddr_in *)&ifr->ifr_ifru.ifru_addr)->sin_addr).s_addr;
   }
   return;
  }
  ifr++;
 }
 close(fd);
}

variable_types:
lip
void *

variable_types:
bip
void *

variable_types:
ip
unsigned 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:
mask
unsigned int

variable_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } *ifcu_req; } ifc_ifcu; }

variable_types: i int

user_defined_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


decompiled_code:
int peer_online()
{
__int64 v0; // rsi
int v1; // ebp
int v2; // ebx
int result; // eax
int v4; // [rsp+0h] [rbp-88h] BYREF
__m128i v5; // [rsp+4h] [rbp-84h]
__m128i v6; // [rsp+14h] [rbp-74h]
int v7; // [rsp+24h] [rbp-64h]

v4 = 6;
v0 = peer_inlist();
v5 = _mm_loadu_si128(self);
v6 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v7 = *(_DWORD *)(self + 32LL);
v1 = getsockfd(4, v0);
if ( v1 < 0 )
m_printf("send msg error\n");
v2 = 3;
do
{
sleep(1u);
send(v1, &v4, 0x6CuLL, 0);
result = close(v1);
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
int func6()
{
__int64 v0; // rsi
int v1; // ebp
int v2; // ebx
int result; // eax
int v4; // [rsp+0h] [rbp-88h] BYREF
__m128i v5; // [rsp+4h] [rbp-84h]
__m128i v6; // [rsp+14h] [rbp-74h]
int v7; // [rsp+24h] [rbp-64h]

v4 = 6;
v0 = peer_inlist();
v5 = _mm_loadu_si128(self);
v6 = _mm_loadu_si128((const __m128i *)(self + 16LL));
v7 = *(_DWORD *)(self + 32LL);
v1 = func1(4, v0);
if ( v1 < 0 )
m_printf("send msg error\n");
v2 = 3;
do
{
sleep(1u);
send(v1, &v4, 0x6CuLL, 0);
result = close(v1);
--v2;
}
while ( v2 );
return result;
}

original_code:
void peer_online(void *data)
{
 struct peer *pr = peer_inlist((struct message *)data);
 struct message msg;
 msg.type = MSG_PEER_INF;
 memcpy(&msg.id, &self->id, sizeof(struct base_inf));
 int fd = getsockfd(FD_SENDMSG, pr);
 if (fd < 0)
  m_printf("send msg error\n");
 int i;
 for (i = 0; i < 3; i++) {
  sleep(1);
  send(fd, &msg, sizeof(struct message), 0);
  close(fd);
 }
}

variable_types:
data
void *


original_code:
void *recv_msg(void *data)
{
 struct message msg;
 struct message *dupmsg;
 pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 int fd = getsockfd(FD_GETMSG, ((void *)0));
 while (1) {
  recv(fd, &msg, sizeof(struct message), 0);
  if (msg.id.ip == self->id.ip)
   continue;
  dupmsg = (struct message *)malloc(sizeof(struct message));
  memcpy(dupmsg, &msg, sizeof(struct message));
  pthread_create(&self->msg_handler_thr, &attr, msg_handler, dupmsg);
 }
}

canonical_original_code:
void *recv_msg(void *data)
{
 struct message msg;
 struct message *dupmsg;
 union pthread_attr_t attr;
 pthread_attr_init(&attr);
 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 int fd = getsockfd(FD_GETMSG, ((void *)0));
 while (1) {
  recv(fd, &msg, sizeof(struct message), 0);
  if (msg.id.ip == self->id.ip)
   continue;
  dupmsg = (struct message *)malloc(sizeof(struct message));
  memcpy(dupmsg, &msg, sizeof(struct message));
  pthread_create(&self->msg_handler_thr, &attr, msg_handler, dupmsg);
 }
}

variable_types: struct message { int type; struct base_inf { char name[32]; unsigned int ip; } id; union { int m_answer; struct file_inf { char fname[64]; unsigned int fsize; } m_file; char m_news[64]; char m_chat[64]; } mdata; } *

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

return_type: void *


decompiled_code:
__int64 __fastcall msg_handler(_DWORD *a1)
{
__int64 result; // rax

switch ( *a1 )
{
case 0:
peer_online();
result = 0LL;
break;
case 1:
peer_outlist();
result = 0LL;
break;
case 2:
case 4:
respond_rqst();
result = 0LL;
break;
case 3:
case 5:
handle_ack();
result = 0LL;
break;
case 6:
peer_inlist();
result = 0LL;
break;
case 7:
chat_get_comment();
result = 0LL;
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
__int64 result; // rax

switch ( *a1 )
{
case 0:
peer_online();
result = 0LL;
break;
case 1:
peer_outlist();
result = 0LL;
break;
case 2:
case 4:
respond_rqst();
result = 0LL;
break;
case 3:
case 5:
handle_ack();
result = 0LL;
break;
case 6:
peer_inlist();
result = 0LL;
break;
case 7:
chat_get_comment();
result = 0LL;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
void *msg_handler(void *data)
{
 struct message *msg = (struct message *)data;
 switch (msg->type) {
 case MSG_OFFLINE:
  peer_outlist(msg) ;
  break;
 case MSG_ONLINE:
  peer_online(msg);
  break;
 case MSG_PEER_INF:
  peer_inlist(msg);
  break;
 case MSG_FILE_RQST:
 case MSG_CHAT_RQST:
  respond_rqst(msg);
  break;
 case MSG_FILE_ACK:
 case MSG_CHAT_ACK:
  handle_ack(msg);
  break;
 case MSG_CHAT:
  chat_get_comment(msg);
 default:
  break;
 }
 return ((void *)0);
}


decompiled_code:
int __fastcall send_file(int fd, char *file)
{
int result; // eax
int v3; // ebp
unsigned int v4; // ebx
int v5; // er13
unsigned __int64 v6; // rax
__int64 v7; // r15
int v8; // ebx
unsigned int v9; // eax
unsigned __int64 v10; // [rsp+8h] [rbp-2F0h]
int arg; // [rsp+14h] [rbp-2E4h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-2E0h] BYREF
struct stat v13; // [rsp+20h] [rbp-2D8h] BYREF
int v14; // [rsp+B0h] [rbp-248h] BYREF
char buf[580]; // [rsp+B4h] [rbp-244h] BYREF

arg = 0;
result = open(file, 0);
if ( result >= 0 )
{
v3 = result;
stat(file, &v13);
v4 = v13.st_size;
v5 = LODWORD(v13.st_size) >> 9;
if ( (v13.st_size & 0x1FF) != 0 )
{
++v5;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
}
else
{
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
if ( !v5 )
return pthread_join(newthread, 0LL);
}
v6 = v4;
v7 = 0LL;
v8 = 0;
v10 = v6;
do
{
v14 = v8;
v9 = _readn(v3, buf, 0x200uLL);
++v8;
v7 += v9;
_writen_isra_0(fd, &v14, v9 + 4);
arg = 100 * v7 / v10;
}
while ( v8 < v5 );
return pthread_join(newthread, 0LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int fd, char *file)
{
int result; // eax
int v3; // ebp
unsigned int v4; // ebx
int v5; // er13
unsigned __int64 v6; // rax
__int64 v7; // r15
int v8; // ebx
unsigned int v9; // eax
unsigned __int64 v10; // [rsp+8h] [rbp-2F0h]
int arg; // [rsp+14h] [rbp-2E4h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-2E0h] BYREF
struct stat v13; // [rsp+20h] [rbp-2D8h] BYREF
int v14; // [rsp+B0h] [rbp-248h] BYREF
char buf[580]; // [rsp+B4h] [rbp-244h] BYREF

arg = 0;
result = open(file, 0);
if ( result >= 0 )
{
v3 = result;
stat(file, &v13);
v4 = v13.st_size;
v5 = LODWORD(v13.st_size) >> 9;
if ( (v13.st_size & 0x1FF) != 0 )
{
++v5;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
}
else
{
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
if ( !v5 )
return pthread_join(newthread, 0LL);
}
v6 = v4;
v7 = 0LL;
v8 = 0;
v10 = v6;
do
{
v14 = v8;
v9 = func0(v3, buf, 0x200uLL);
++v8;
v7 += v9;
func1(fd, &v14, v9 + 4);
arg = 100 * v7 / v10;
}
while ( v8 < v5 );
return pthread_join(newthread, 0LL);
}
return result;
}

original_code:
void send_file(int sockfd, char *path)
{
 int fd;
 int pack_cnt;
 pthread_t tid;
 struct stat st;
 unsigned int fsize;
 struct file_piece piece;
 int percent = 0;
 unsigned int rnbytes;
 unsigned long long cur_sum = 0;
 if ((fd = open(path, 00)) < 0)
  return;
 stat(path, &st);
 fsize = st.st_size;
 if (fsize % 512)
  pack_cnt = fsize / 512 + 1;
 else
  pack_cnt = fsize / 512;
 pthread_create(&tid, ((void *)0), show_progress, &percent);
 int i;
 for (i = 0; i < pack_cnt; i++) {
  piece.pi_idx = i;
  rnbytes = __readn(fd, &piece.pi_data, 512);
  __writen(sockfd, &piece, rnbytes + sizeof(piece.pi_idx));
  cur_sum += rnbytes;
  percent = (cur_sum*100) / fsize;
 }
 pthread_join(tid, ((void *)0));
}

canonical_original_code:
void send_file(int sockfd, char *path)
{
 int fd;
 int pack_cnt;
 unsigned long tid;
 struct stat st;
 unsigned int fsize;
 struct file_piece piece;
 int percent = 0;
 unsigned int rnbytes;
 unsigned long long cur_sum = 0;
 if ((fd = open(path, 00)) < 0)
  return;
 stat(path, &st);
 fsize = st.st_size;
 if (fsize % 512)
  pack_cnt = fsize / 512 + 1;
 else
  pack_cnt = fsize / 512;
 pthread_create(&tid, ((void *)0), show_progress, &percent);
 int i;
 for (i = 0; i < pack_cnt; i++) {
  piece.pi_idx = i;
  rnbytes = __readn(fd, &piece.pi_data, 512);
  __writen(sockfd, &piece, rnbytes + sizeof(piece.pi_idx));
  cur_sum += rnbytes;
  percent = (cur_sum*100) / fsize;
 }
 pthread_join(tid, ((void *)0));
}

variable_types: sockfd int

variable_types:
path
char *

variable_types:
tid
unsigned long

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
fsize
unsigned int

variable_types: struct file_piece { int pi_idx; char pi_data[512]; }

variable_types: percent int

variable_types:
rnbytes
unsigned int


decompiled_code:
int __fastcall recv_file(int fd, char *file)
{
char *v2; // rax
int v3; // eax
unsigned int v4; // ebp
int v5; // ebx
__int64 v6; // r15
int v7; // ebp
unsigned int v8; // er14
int v10; // [rsp+Ch] [rbp-25Ch]
int arg; // [rsp+14h] [rbp-254h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-250h] BYREF
int buf; // [rsp+20h] [rbp-248h] BYREF
char v14[580]; // [rsp+24h] [rbp-244h] BYREF

arg = 0;
v2 = getenv("HOME");
chdir(v2);
v3 = open(file, 577, 420LL);
v4 = *((_DWORD *)file + 16);
v5 = v3;
if ( (v4 & 0x1FF) != 0 )
{
v10 = (v4 >> 9) + 1;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
}
else
{
v10 = v4 >> 9;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
if ( !(v4 >> 9) )
return pthread_join(newthread, 0LL);
}
v6 = 0LL;
v7 = 0;
do
{
++v7;
v8 = _readn(fd, &buf, 0x204uLL);
v6 = v6 + v8 - 4;
lseek(v5, buf << 9, 0);
_writen_isra_0(v5, v14, v8 - 4);
arg = 100 * v6 / (unsigned __int64)*((unsigned int *)file + 16);
}
while ( v7 < v10 );
return pthread_join(newthread, 0LL);
}

canonical_decompiled_code:
int __fastcall func3(int fd, char *file)
{
char *v2; // rax
int v3; // eax
unsigned int v4; // ebp
int v5; // ebx
__int64 v6; // r15
int v7; // ebp
unsigned int v8; // er14
int v10; // [rsp+Ch] [rbp-25Ch]
int arg; // [rsp+14h] [rbp-254h] BYREF
pthread_t newthread; // [rsp+18h] [rbp-250h] BYREF
int buf; // [rsp+20h] [rbp-248h] BYREF
char v14[580]; // [rsp+24h] [rbp-244h] BYREF

arg = 0;
v2 = getenv("HOME");
chdir(v2);
v3 = open(file, 577, 420LL);
v4 = *((_DWORD *)file + 16);
v5 = v3;
if ( (v4 & 0x1FF) != 0 )
{
v10 = (v4 >> 9) + 1;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
}
else
{
v10 = v4 >> 9;
pthread_create(&newthread, 0LL, (void *(*)(void *))&show_progress, &arg);
if ( !(v4 >> 9) )
return pthread_join(newthread, 0LL);
}
v6 = 0LL;
v7 = 0;
do
{
++v7;
v8 = func0(fd, &buf, 0x204uLL);
v6 = v6 + v8 - 4;
lseek(v5, buf << 9, 0);
func1(v5, v14, v8 - 4);
arg = 100 * v6 / (unsigned __int64)*((unsigned int *)file + 16);
}
while ( v7 < v10 );
return pthread_join(newthread, 0LL);
}

original_code:
void recv_file(int sockfd, void *data)
{
 int fd;
 pthread_t tid;
 int pack_cnt, i;
 struct file_piece piece;
 int percent = 0;
 unsigned int rnbytes;
 unsigned long long cur_sum = 0;
 struct file_inf *finf = (struct file_inf *)data;
 chdir(getenv("HOME"));
 fd = open(finf->fname, 0100 | 01 | 01000, 0644);
 if (finf->fsize % 512)
  pack_cnt = finf->fsize / 512 + 1;
 else
  pack_cnt = finf->fsize / 512;
 pthread_create(&tid, ((void *)0), show_progress, &percent);
 for (i = 0; i < pack_cnt; i++) {
  rnbytes = __readn(sockfd, &piece, (512 + sizeof(unsigned int)));
  lseek(fd, piece.pi_idx * 512, 0);
  __writen(fd, &piece.pi_data, rnbytes - sizeof(piece.pi_idx));
  cur_sum += rnbytes - sizeof(piece.pi_idx);
  percent = (cur_sum * 100) / finf->fsize;
 }
 pthread_join(tid, ((void *)0));
}

canonical_original_code:
void recv_file(int sockfd, void *data)
{
 int fd;
 unsigned long tid;
 int pack_cnt; int i;
 struct file_piece piece;
 int percent = 0;
 unsigned int rnbytes;
 unsigned long long cur_sum = 0;
 struct file_inf *finf = (struct file_inf *)data;
 chdir(getenv("HOME"));
 fd = open(finf->fname, 0100 | 01 | 01000, 0644);
 if (finf->fsize % 512)
  pack_cnt = finf->fsize / 512 + 1;
 else
  pack_cnt = finf->fsize / 512;
 pthread_create(&tid, ((void *)0), show_progress, &percent);
 for (i = 0; i < pack_cnt; i++) {
  rnbytes = __readn(sockfd, &piece, (512 + sizeof(unsigned int)));
  lseek(fd, piece.pi_idx * 512, 0);
  __writen(fd, &piece.pi_data, rnbytes - sizeof(piece.pi_idx));
  cur_sum += rnbytes - sizeof(piece.pi_idx);
  percent = (cur_sum * 100) / finf->fsize;
 }
 pthread_join(tid, ((void *)0));
}

variable_types: fd int

variable_types: struct file_inf { char fname[64]; unsigned int fsize; } *

user_defined_types: struct file_piece { int pi_idx; char pi_data[512]; }


original_code:
void list_destroy(list_t *lst)
{
 node_t *iter;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  free(iter->data);
  free(iter);
 }
 free(lst->head);
}

canonical_original_code:
void list_destroy(struct list *lst)
{
 struct list_node *iter;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  free(iter->data);
  free(iter);
 }
 free(lst->head);
}

variable_types: struct list { int node_cnt; struct list_node { void *data; struct list_node *prev; struct list_node *next; } *head; } *


original_code:
void *list_del_next(list_t *lst, void *data)
{
 void *tmp;
 node_t *iter;
 if (data != ((void *)0)) {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (iter->data == data)
    break;
  }
  if (iter == lst->head || iter->next == lst->head)
   return ((void *)0);
 } else {
  iter = lst->head;
 }
 tmp = iter->next->data;
 free(iter->next);
 iter->next = iter->next->next;
 iter->next->prev = iter;
 lst->node_cnt--;
 return tmp;
}

canonical_original_code:
void *list_del_next(struct list *lst, void *data)
{
 void *tmp;
 struct list_node *iter;
 if (data != ((void *)0)) {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (iter->data == data)
    break;
  }
  if (iter == lst->head || iter->next == lst->head)
   return ((void *)0);
 } else {
  iter = lst->head;
 }
 tmp = iter->next->data;
 free(iter->next);
 iter->next = iter->next->next;
 iter->next->prev = iter;
 lst->node_cnt--;
 return tmp;
}

variable_types:
tmp
void *

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


original_code:
void *list_del_after(list_t *lst, void *data)
{
 void *tmp;
 node_t *iter;
 if (data != ((void *)0)) {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (iter->data == data)
    break;
  }
  if(iter->prev == lst->head || iter == lst->head)
   return ((void *)0);
 } else {
  iter = lst->head;
 }
 tmp = iter->prev->data;
 free(iter->prev);
 iter->prev = iter->prev->prev;
 iter->prev->next = iter;
 lst->node_cnt--;
 return tmp;
}

canonical_original_code:
void *list_del_after(struct list *lst, void *data)
{
 void *tmp;
 struct list_node *iter;
 if (data != ((void *)0)) {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (iter->data == data)
    break;
  }
  if(iter->prev == lst->head || iter == lst->head)
   return ((void *)0);
 } else {
  iter = lst->head;
 }
 tmp = iter->prev->data;
 free(iter->prev);
 iter->prev = iter->prev->prev;
 iter->prev->next = iter;
 lst->node_cnt--;
 return tmp;
}


original_code:
int list_del_node(list_t *lst, void *data)
{
 node_t *iter;
 if (data == ((void *)0) || lst->node_cnt == 0)
  return -1;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  if (iter->data == data) {
   iter->prev->next = iter->next;
   iter->next->prev = iter->prev;
   lst->node_cnt--;
   free(iter);
   return 0;
  }
 }
 return -1;
}

canonical_original_code:
int list_del_node(struct list *lst, void *data)
{
 struct list_node *iter;
 if (data == ((void *)0) || lst->node_cnt == 0)
  return -1;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  if (iter->data == data) {
   iter->prev->next = iter->next;
   iter->next->prev = iter->prev;
   lst->node_cnt--;
   free(iter);
   return 0;
  }
 }
 return -1;
}


decompiled_code:
__int64 __fastcall list_insert_next(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

if ( a3 )
{
v4 = *(_QWORD **)(a1 + 8);
if ( a2 )
{
v5 = (_QWORD *)v4[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
while ( *v5 != a2 )
{
v5 = (_QWORD *)v5[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
}
}
else
{
v5 = *(_QWORD **)(a1 + 8);
}
v6 = malloc(0x18uLL);
v7 = v5[2];
*v6 = a3;
v6[2] = v7;
v6[1] = v5;
v5[2] = v6;
*(_QWORD *)(v6[2] + 8LL) = v6;
result = 0LL;
++*(_DWORD *)a1;
return result;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

if ( a3 )
{
v4 = *(_QWORD **)(a1 + 8);
if ( a2 )
{
v5 = (_QWORD *)v4[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
while ( *v5 != a2 )
{
v5 = (_QWORD *)v5[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
}
}
else
{
v5 = *(_QWORD **)(a1 + 8);
}
v6 = malloc(0x18uLL);
v7 = v5[2];
*v6 = a3;
v6[2] = v7;
v6[1] = v5;
v5[2] = v6;
*(_QWORD *)(v6[2] + 8LL) = v6;
result = 0LL;
++*(_DWORD *)a1;
return result;
}
return 0xFFFFFFFFLL;
}

original_code:
int list_insert_next(list_t *lst, void *tag_data, void *inst_data)
{
 if (inst_data == ((void *)0))
  return -1;
 node_t *node1;
 if (tag_data != ((void *)0)) {
  for(node1 = (lst)->head->next; node1 != (lst)->head ; node1 = node1->next) {
   if (node1->data == tag_data)
    break;
  }
  if (node1 == lst->head)
   return -1;
 } else {
  node1 = lst->head;
 }
 node_t *node2 = (node_t *)malloc(sizeof(node_t));
 node2->data = inst_data;
 node2->prev = node1;
 node2->next = node1->next;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}

canonical_original_code:
int list_insert_next(struct list *lst, void *tag_data, void *inst_data)
{
 if (inst_data == ((void *)0))
  return -1;
 struct list_node *node1;
 if (tag_data != ((void *)0)) {
  for(node1 = (lst)->head->next; node1 != (lst)->head ; node1 = node1->next) {
   if (node1->data == tag_data)
    break;
  }
  if (node1 == lst->head)
   return -1;
 } else {
  node1 = lst->head;
 }
 struct list_node *node2 = (node_t *)malloc(sizeof(node_t));
 node2->data = inst_data;
 node2->prev = node1;
 node2->next = node1->next;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}


decompiled_code:
__int64 __fastcall list_insert_after(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

if ( a3 )
{
v4 = *(_QWORD **)(a1 + 8);
if ( a2 )
{
v5 = (_QWORD *)v4[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
while ( *v5 != a2 )
{
v5 = (_QWORD *)v5[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
}
}
else
{
v5 = *(_QWORD **)(a1 + 8);
}
v6 = malloc(0x18uLL);
v7 = v5[1];
*v6 = a3;
v6[1] = v7;
v6[2] = v5;
*(_QWORD *)(v7 + 16) = v6;
*(_QWORD *)(v6[2] + 8LL) = v6;
result = 0LL;
++*(_DWORD *)a1;
return result;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax
_QWORD *v5; // rbx
_QWORD *v6; // rax
__int64 v7; // rdx
__int64 result; // rax

if ( a3 )
{
v4 = *(_QWORD **)(a1 + 8);
if ( a2 )
{
v5 = (_QWORD *)v4[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
while ( *v5 != a2 )
{
v5 = (_QWORD *)v5[2];
if ( v4 == v5 )
return 0xFFFFFFFFLL;
}
}
else
{
v5 = *(_QWORD **)(a1 + 8);
}
v6 = malloc(0x18uLL);
v7 = v5[1];
*v6 = a3;
v6[1] = v7;
v6[2] = v5;
*(_QWORD *)(v7 + 16) = v6;
*(_QWORD *)(v6[2] + 8LL) = v6;
result = 0LL;
++*(_DWORD *)a1;
return result;
}
return 0xFFFFFFFFLL;
}

original_code:
int list_insert_after(list_t *lst, void *tag_data, void *inst_data)
{
 if (inst_data == ((void *)0))
  return -1;
 node_t *node1;
 if (tag_data != ((void *)0)) {
  for(node1 = (lst)->head->next; node1 != (lst)->head ; node1 = node1->next) {
   if (node1->data == tag_data)
    break;
  }
  if (node1 == lst->head)
   return -1;
 } else {
  node1 = lst->head;
 }
 node_t *node2 = (node_t *)malloc(sizeof(node_t));
 node2->data = inst_data;
 node2->prev = node1->prev;
 node2->next = node1;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}

canonical_original_code:
int list_insert_after(struct list *lst, void *tag_data, void *inst_data)
{
 if (inst_data == ((void *)0))
  return -1;
 struct list_node *node1;
 if (tag_data != ((void *)0)) {
  for(node1 = (lst)->head->next; node1 != (lst)->head ; node1 = node1->next) {
   if (node1->data == tag_data)
    break;
  }
  if (node1 == lst->head)
   return -1;
 } else {
  node1 = lst->head;
 }
 struct list_node *node2 = (node_t *)malloc(sizeof(node_t));
 node2->data = inst_data;
 node2->prev = node1->prev;
 node2->next = node1;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}


decompiled_code:
__int64 __fastcall list_insert_tail(__int64 a1, __int64 a2)
{
list_insert_after(a1, 0LL, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
func5(a1, 0LL, a2);
return 0LL;
}

original_code:
int list_insert_tail(list_t *lst, void *data)
{
 list_insert_after(lst, ((void *)0), data);
 return 0;
}

canonical_original_code:
int list_insert_tail(struct list *lst, void *data)
{
 list_insert_after(lst, ((void *)0), data);
 return 0;
}


decompiled_code:
__int64 __fastcall list_insert_head(__int64 a1, __int64 a2)
{
list_insert_next(a1, 0LL, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
func4(a1, 0LL, a2);
return 0LL;
}

original_code:
int list_insert_head(list_t *lst, void *data)
{
 list_insert_next(lst, ((void *)0), data);
 return 0;
}

canonical_original_code:
int list_insert_head(struct list *lst, void *data)
{
 list_insert_next(lst, ((void *)0), data);
 return 0;
}


decompiled_code:
__int64 __fastcall list_del_head(__int64 a1)
{
return list_del_next(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return func1(a1, 0LL);
}

original_code:
void *list_del_head(list_t *lst)
{
 return list_del_next(lst, ((void *)0));
}

canonical_original_code:
void *list_del_head(struct list *lst)
{
 return list_del_next(lst, ((void *)0));
}


decompiled_code:
__int64 __fastcall list_del_tail(__int64 a1)
{
return list_del_after(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return func2(a1, 0LL);
}

original_code:
void *list_del_tail(list_t *lst)
{
 return list_del_after(lst, ((void *)0));
}

canonical_original_code:
void *list_del_tail(struct list *lst)
{
 return list_del_after(lst, ((void *)0));
}


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

if ( !a2 )
return 0xFFFFFFFFLL;
if ( *(_DWORD *)a1 && a3(a2, **(_QWORD **)(*(_QWORD *)(a1 + 8) + 8LL)) )
{
v4 = *(_QWORD *)(a1 + 8);
v5 = *(__int64 **)(v4 + 16);
if ( (__int64 *)v4 != v5 )
{
while ( !a3(a2, *v5) )
{
v5 = (__int64 *)v5[2];
if ( *(__int64 **)(a1 + 8) == v5 )
goto LABEL_9;
}
list_insert_after(a1, *v5, a2);
}
LABEL_9:
result = 0LL;
}
else
{
list_insert_tail(a1, a2);
result = 0LL;
}
return result;
}

original_code:
int list_sort_insert(list_t *lst, void *data, int (*cmp_fn)(void *d1, void *d2))
{
 node_t *iter;
 if (data == ((void *)0))
  return -1;
 if (lst->node_cnt == 0)
  list_insert_tail(lst, data);
 else if (!cmp_fn(data, (lst->head->prev)->data))
  list_insert_tail(lst, data);
 else {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (cmp_fn(data, iter->data)) {
    list_insert_after(lst, iter->data, data);
    break;
   }
  }
 }
 return 0;
}

canonical_original_code:
int list_sort_insert(struct list *lst, void *data, int (*cmp_fn)(void *d1, void *d2))
{
 struct list_node *iter;
 if (data == ((void *)0))
  return -1;
 if (lst->node_cnt == 0)
  list_insert_tail(lst, data);
 else if (!cmp_fn(data, (lst->head->prev)->data))
  list_insert_tail(lst, data);
 else {
  for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
   if (cmp_fn(data, iter->data)) {
    list_insert_after(lst, iter->data, data);
    break;
   }
  }
 }
 return 0;
}


repo: Jery-Mong/huffman

decompiled_code:
__int64 __fastcall list_del_next(_DWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a2 + 16);
if ( result == a2 )
return 0LL;
v3 = *(_QWORD *)(result + 16);
*(_QWORD *)(a2 + 16) = v3;
*(_QWORD *)(v3 + 8) = a2;
--*a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a2 + 16);
if ( result == a2 )
return 0LL;
v3 = *(_QWORD *)(result + 16);
*(_QWORD *)(a2 + 16) = v3;
*(_QWORD *)(v3 + 8) = a2;
--*a1;
return result;
}

original_code:
node_t* list_del_next(list_t *lst, node_t *node)
{
 node_t *tmp;
 if (node->next == node)
  return ((void *)0);
 tmp = node->next;
 node->next = node->next->next;
 node->next->prev = node;
 lst->node_cnt--;
 return tmp;
}

canonical_original_code:
struct list_node *list_del_next(struct list *lst, struct list_node *node)
{
 struct list_node *tmp;
 if (node->next == node)
  return ((void *)0);
 tmp = node->next;
 node->next = node->next->next;
 node->next->prev = node;
 lst->node_cnt--;
 return tmp;
}

variable_types: struct list { int node_cnt; struct list_node *head; } *

variable_types:
node
struct list_node *

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


decompiled_code:
__int64 __fastcall list_del_after(_DWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a2 + 8);
if ( result == a2 )
return 0LL;
v3 = *(_QWORD *)(result + 8);
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)(v3 + 16) = a2;
--*a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a2 + 8);
if ( result == a2 )
return 0LL;
v3 = *(_QWORD *)(result + 8);
*(_QWORD *)(a2 + 8) = v3;
*(_QWORD *)(v3 + 16) = a2;
--*a1;
return result;
}

original_code:
node_t * list_del_after(list_t *lst, node_t *node)
{
 node_t *tmp;
 if(node->prev == node)
  return ((void *)0);
 tmp = node->prev;
 node->prev = node->prev->prev;
 node->prev->next = node;
 lst->node_cnt--;
 return tmp;
}

canonical_original_code:
struct list_node *list_del_after(struct list *lst, struct list_node *node)
{
 struct list_node *tmp;
 if(node->prev == node)
  return ((void *)0);
 tmp = node->prev;
 node->prev = node->prev->prev;
 node->prev->next = node;
 lst->node_cnt--;
 return tmp;
}


decompiled_code:
__int64 __fastcall list_insert_next(_DWORD *a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax

if ( !a2 || !a3 )
return 0xFFFFFFFFLL;
v3 = *(_QWORD *)(a2 + 16);
*(_QWORD *)(a3 + 8) = a2;
*(_QWORD *)(a3 + 16) = v3;
*(_QWORD *)(a2 + 16) = a3;
*(_QWORD *)(*(_QWORD *)(a3 + 16) + 8LL) = a3;
result = 0LL;
++*a1;
return result;
}

original_code:
int list_insert_next(list_t *lst, node_t *node1, node_t *node2)
{
 if (node1 == ((void *)0) || node2 == ((void *)0))
  return -1;
 node2->prev = node1;
 node2->next = node1->next;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}

canonical_original_code:
int list_insert_next(struct list *lst, struct list_node *node1, struct list_node *node2)
{
 if (node1 == ((void *)0) || node2 == ((void *)0))
  return -1;
 node2->prev = node1;
 node2->next = node1->next;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}


decompiled_code:
__int64 __fastcall list_insert_after(_DWORD *a1, __int64 a2, __int64 a3)
{
__int64 v3; // rax
__int64 result; // rax

if ( !a2 || !a3 )
return 0xFFFFFFFFLL;
v3 = *(_QWORD *)(a2 + 8);
*(_QWORD *)(a3 + 16) = a2;
*(_QWORD *)(a3 + 8) = v3;
*(_QWORD *)(v3 + 16) = a3;
*(_QWORD *)(*(_QWORD *)(a3 + 16) + 8LL) = a3;
result = 0LL;
++*a1;
return result;
}

original_code:
int list_insert_after(list_t *lst, node_t *node1, node_t *node2)
{
 if (node1 == ((void *)0) || node2 == ((void *)0))
  return -1;
 node2->prev = node1->prev;
 node2->next = node1;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}

canonical_original_code:
int list_insert_after(struct list *lst, struct list_node *node1, struct list_node *node2)
{
 if (node1 == ((void *)0) || node2 == ((void *)0))
  return -1;
 node2->prev = node1->prev;
 node2->next = node1;
 node2->prev->next = node2;
 node2->next->prev = node2;
 lst->node_cnt++;
 return 0;
}


decompiled_code:
__int64 __fastcall list_del_node(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
if ( !*(_DWORD *)a1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_QWORD *)(v2 + 16);
if ( v2 == v3 )
return 0xFFFFFFFFLL;
while ( a2 != v3 )
{
v3 = *(_QWORD *)(v3 + 16);
if ( v2 == v3 )
return 0xFFFFFFFFLL;
}
list_del_next((_DWORD *)a1, *(_QWORD *)(a2 + 8));
--*(_DWORD *)a1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
if ( !*(_DWORD *)a1 )
return 0xFFFFFFFFLL;
v2 = *(_QWORD *)(a1 + 8);
v3 = *(_QWORD *)(v2 + 16);
if ( v2 == v3 )
return 0xFFFFFFFFLL;
while ( a2 != v3 )
{
v3 = *(_QWORD *)(v3 + 16);
if ( v2 == v3 )
return 0xFFFFFFFFLL;
}
func0((_DWORD *)a1, *(_QWORD *)(a2 + 8));
--*(_DWORD *)a1;
return 0LL;
}

original_code:
int list_del_node(list_t *lst, node_t *node)
{
 node_t *iter;
 if (node == ((void *)0) || lst->node_cnt == 0)
  return -1;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  if (iter == node) {
   list_del_next(lst, node->prev);
   lst->node_cnt--;
   return 0;
  }
 }
 return -1;
}

canonical_original_code:
int list_del_node(struct list *lst, struct list_node *node)
{
 struct list_node *iter;
 if (node == ((void *)0) || lst->node_cnt == 0)
  return -1;
 for(iter = (lst)->head->next; iter != (lst)->head ; iter = iter->next) {
  if (iter == node) {
   list_del_next(lst, node->prev);
   lst->node_cnt--;
   return 0;
  }
 }
 return -1;
}


decompiled_code:
__int64 __fastcall list_insert_tail(__int64 a1, __int64 a2)
{
list_insert_after((_DWORD *)a1, *(_QWORD *)(a1 + 8), a2);
return 0LL;
}

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

original_code:
int list_insert_tail(list_t *lst, node_t *node)
{
 list_insert_after(lst, lst->head, node);
 return 0;
}

canonical_original_code:
int list_insert_tail(struct list *lst, struct list_node *node)
{
 list_insert_after(lst, lst->head, node);
 return 0;
}


decompiled_code:
__int64 __fastcall list_insert_head(__int64 a1, __int64 a2)
{
list_insert_next((_DWORD *)a1, *(_QWORD *)(a1 + 8), a2);
return 0LL;
}

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

original_code:
int list_insert_head(list_t *lst, node_t *node)
{
 list_insert_next(lst, lst->head, node);
 return 0;
}

canonical_original_code:
int list_insert_head(struct list *lst, struct list_node *node)
{
 list_insert_next(lst, lst->head, node);
 return 0;
}


decompiled_code:
__int64 __fastcall list_del_head(__int64 a1)
{
return list_del_next((_DWORD *)a1, *(_QWORD *)(a1 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return func0((_DWORD *)a1, *(_QWORD *)(a1 + 8));
}

original_code:
node_t *list_del_head(list_t *lst)
{
 return list_del_next(lst, lst->head);
}

canonical_original_code:
struct list_node *list_del_head(struct list *lst)
{
 return list_del_next(lst, lst->head);
}


decompiled_code:
__int64 __fastcall list_del_tail(__int64 a1)
{
return list_del_after((_DWORD *)a1, *(_QWORD *)(a1 + 8));
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return func1((_DWORD *)a1, *(_QWORD *)(a1 + 8));
}

original_code:
node_t *list_del_tail(list_t *lst)
{
 return list_del_after(lst, lst->head);
}

canonical_original_code:
struct list_node *list_del_tail(struct list *lst)
{
 return list_del_after(lst, lst->head);
}