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

binary = a1;
ent = 0LL;
incirc = LC2;
result = 0LL;
memset(&ccount, 0, 0x800uLL);
chisq = 0LL;
datasum = 0LL;
mp = 0;
mcount = 0LL;
inmont = 0LL;
sccfirst = 1;
scct3 = 0LL;
scct2 = 0LL;
scct1 = 0LL;
totalc = 0LL;
return result;
}

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

binary = a1;
ent = 0LL;
incirc = LC2;
result = 0LL;
memset(&ccount, 0, 0x800uLL);
chisq = 0LL;
datasum = 0LL;
mp = 0;
mcount = 0LL;
inmont = 0LL;
sccfirst = 1;
scct3 = 0LL;
scct2 = 0LL;
scct1 = 0LL;
totalc = 0LL;
return result;
}

original_code:
void rt_init(int binmode)
{
    int i;
    binary = binmode;
    ent = 0.0;
    chisq = 0.0;
    datasum = 0.0;
    mp = 0;
    mcount = 0;
    inmont = 0;
    incirc = 65535.0 * 65535.0;
    sccfirst = 1;
    scct1 = scct2 = scct3 = 0.0;
    incirc = pow(pow(256.0, (double) (6 / 2)) - 1, 2.0);
    for (i = 0; i < 256; i++) {
 ccount[i] = 0;
    }
    totalc = 0;
}

variable_types: binmode int

variable_types: i int


original_code:
void rt_add(void *buf, int bufl)
{
    unsigned char *bp = buf;
    int oc, c, bean;
    while (bean = 0, (bufl-- > 0)) {
       oc = *bp++;
       do {
   if (binary) {
      c = !!(oc & 0x80);
   } else {
      c = oc;
   }
   ccount[c]++;
   totalc++;
   if (bean == 0) {
       monte[mp++] = oc;
       if (mp >= 6) {
   int mj;
   mp = 0;
   mcount++;
   montex = montey = 0;
   for (mj = 0; mj < 6 / 2; mj++) {
      montex = (montex * 256.0) + monte[mj];
      montey = (montey * 256.0) + monte[(6 / 2) + mj];
   }
   if ((montex * montex + montey * montey) <= incirc) {
      inmont++;
   }
       }
   }
   sccun = c;
   if (sccfirst) {
      sccfirst = 0;
      scclast = 0;
      sccu0 = sccun;
   } else {
      scct1 = scct1 + scclast * sccun;
   }
   scct2 = scct2 + sccun;
   scct3 = scct3 + (sccun * sccun);
   scclast = sccun;
   oc <<= 1;
       } while (binary && (++bean < 8));
    }
}

canonical_original_code:
void rt_add(void *buf, int bufl)
{
    unsigned char *bp = buf;
    int oc; int c; int bean;
    while (bean = 0, (bufl-- > 0)) {
       oc = *bp++;
       do {
   if (binary) {
      c = !!(oc & 0x80);
   } else {
      c = oc;
   }
   ccount[c]++;
   totalc++;
   if (bean == 0) {
       monte[mp++] = oc;
       if (mp >= 6) {
   int mj;
   mp = 0;
   mcount++;
   montex = montey = 0;
   for (mj = 0; mj < 6 / 2; mj++) {
      montex = (montex * 256.0) + monte[mj];
      montey = (montey * 256.0) + monte[(6 / 2) + mj];
   }
   if ((montex * montex + montey * montey) <= incirc) {
      inmont++;
   }
       }
   }
   sccun = c;
   if (sccfirst) {
      sccfirst = 0;
      scclast = 0;
      sccu0 = sccun;
   } else {
      scct1 = scct1 + scclast * sccun;
   }
   scct2 = scct2 + sccun;
   scct3 = scct3 + (sccun * sccun);
   scclast = sccun;
   oc <<= 1;
       } while (binary && (++bean < 8));
    }
}

variable_types:
buf
void *

variable_types: bufl int

variable_types:
bp
unsigned char *

variable_types: oc int

variable_types: c int

variable_types: bean int

variable_types: mj int

return_type: void


help

decompiled_code:
int help()
{
printf("ent -- Calculate entropy of file. Call");
printf("\n with ent [options] [input-file]");
putchar(10);
printf("\n Options: -b Treat input as a stream of bits");
printf("\n -c Print occurrence counts");
printf("\n -f Fold upper to lower case letters");
printf("\n -t Terse output in CSV format");
puts("\n -u Print this message");
printf("\nBy John Walker");
printf("\n http://www.fourmilab.ch/");
return printf("\n %s\n", "January 28th, 2008");
}

canonical_decompiled_code:
int func0()
{
printf("ent -- Calculate entropy of file. Call");
printf("\n with ent [options] [input-file]");
putchar(10);
printf("\n Options: -b Treat input as a stream of bits");
printf("\n -c Print occurrence counts");
printf("\n -f Fold upper to lower case letters");
printf("\n -t Terse output in CSV format");
puts("\n -u Print this message");
printf("\nBy John Walker");
printf("\n http://www.fourmilab.ch/");
return printf("\n %s\n", "January 28th, 2008");
}

original_code:
static void help(void)
{
        printf("ent --  Calculate entropy of file.  Call");
        printf("\n        with ent [options] [input-file]");
        printf("\n");
        printf("\n        Options:   -b   Treat input as a stream of bits");
        printf("\n                   -c   Print occurrence counts");
        printf("\n                   -f   Fold upper to lower case letters");
        printf("\n                   -t   Terse output in CSV format");
        printf("\n                   -u   Print this message\n");
        printf("\nBy John Walker");
        printf("\n   http://www.fourmilab.ch/");
        printf("\n   %s\n", "January 28th, 2008");
}

canonical_original_code:
static void help()
{
        printf("ent --  Calculate entropy of file.  Call");
        printf("\n        with ent [options] [input-file]");
        printf("\n");
        printf("\n        Options:   -b   Treat input as a stream of bits");
        printf("\n                   -c   Print occurrence counts");
        printf("\n                   -f   Fold upper to lower case letters");
        printf("\n                   -t   Terse output in CSV format");
        printf("\n                   -u   Print this message\n");
        printf("\nBy John Walker");
        printf("\n   http://www.fourmilab.ch/");
        printf("\n   %s\n", "January 28th, 2008");
}


repo: pstiasny/eiti-tin-derpftp

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

for ( result = 0LL; result != 32; ++result )
{
if ( !servers[2 * result] )
return (unsigned int)result;
}
return 0xFFFFFFFFLL;
}

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

for ( result = 0LL; result != 32; ++result )
{
if ( !servers[2 * result] )
return (unsigned int)result;
}
return 0xFFFFFFFFLL;
}

original_code:
static int get_free_handle()
{
    int i;
    for (i = 0; i < 32; ++i)
        if (!servers[i].in_use)
            return i;
    return -1;
}

return_type: int


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

v3 = 8LL;
while ( 1 )
{
v4 = read(fd, buf, v3);
if ( v4 == -1 )
break;
buf = (char *)buf + v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

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

v3 = 8LL;
while ( 1 )
{
v4 = read(fd, buf, v3);
if ( v4 == -1 )
break;
buf = (char *)buf + v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
static int read_response(int sock, struct fs_response *buf)
{
    size_t len = sizeof(struct fs_response);
    void *bufp = buf;
    ssize_t got;
    while (len) {
        got = read(sock, bufp, len);
        if (got == -1)
            return -1;
        len -= got;
        bufp += got;
    }
    return 0;
}

canonical_original_code:
static int read_response(int sock, struct fs_response *buf)
{
    unsigned long len = sizeof(struct fs_response);
    void *bufp = buf;
    long got;
    while (len) {
        got = read(sock, bufp, len);
        if (got == -1)
            return -1;
        len -= got;
        bufp += got;
    }
    return 0;
}

variable_types: sock int

variable_types: struct fs_response { int status; int val; } *

variable_types:
len
unsigned long

variable_types:
bufp
void *

variable_types: got long


decompiled_code:
__int64 __fastcall process_response(int *a1)
{
int v1; // ebx

if ( !*a1 )
return (unsigned int)a1[1];
v1 = *a1;
*_errno_location() = v1;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1)
{
int v1; // ebx

if ( !*a1 )
return (unsigned int)a1[1];
v1 = *a1;
*_errno_location() = v1;
return 0xFFFFFFFFLL;
}

original_code:
static int process_response(struct fs_response *res)
{
    if (res->status) {
        (*__errno_location ()) = res->status;
        return (-1);
    } else {
        return res->val;
    }
}

user_defined_types: struct fs_response { int status; int val; }


decompiled_code:
ssize_t __fastcall write_command(int fd, char a2, int a3, int a4, int a5)
{
size_t v5; // rbp
char *v6; // rbx
ssize_t result; // rax
char v8; // [rsp+0h] [rbp-28h] BYREF
int v9; // [rsp+4h] [rbp-24h]
int v10; // [rsp+8h] [rbp-20h]
int v11; // [rsp+Ch] [rbp-1Ch]

v5 = 16LL;
v8 = a2;
v6 = &v8;
v9 = a3;
v10 = a4;
v11 = a5;
while ( 1 )
{
result = send(fd, v6, v5, 0);
if ( !result )
break;
if ( result == -1 )
return 0xFFFFFFFFLL;
v5 -= result;
v6 += result;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int fd, char a2, int a3, int a4, int a5)
{
size_t v5; // rbp
char *v6; // rbx
ssize_t result; // rax
char v8; // [rsp+0h] [rbp-28h] BYREF
int v9; // [rsp+4h] [rbp-24h]
int v10; // [rsp+8h] [rbp-20h]
int v11; // [rsp+Ch] [rbp-1Ch]

v5 = 16LL;
v8 = a2;
v6 = &v8;
v9 = a3;
v10 = a4;
v11 = a5;
while ( 1 )
{
result = send(fd, v6, v5, 0);
if ( !result )
break;
if ( result == -1 )
return 0xFFFFFFFFLL;
v5 -= result;
v6 += result;
}
return result;
}

original_code:
static int write_command(int sock, enum FSMSG_TYPE type, int32_t fd, int32_t arg1, int32_t arg2)
{
    struct fs_command cmd = { type, fd, arg1, arg2 };
    void *cmdp = &cmd;
    size_t sent, length = sizeof(cmd);
    while (0 != (sent = send(sock, cmdp, length, 0))) {
        if (sent == -1)
            return -1;
        length -= sent;
        cmdp += sent;
    }
    return 0;
}

canonical_original_code:
static int write_command(int sock, enum FSMSG_TYPE type, int fd, int arg1, int arg2)
{
    struct fs_command cmd = { type, fd, arg1, arg2 };
    void *cmdp = &cmd;
    unsigned long sent; unsigned long length = sizeof(cmd);
    while (0 != (sent = send(sock, cmdp, length, 0))) {
        if (sent == -1)
            return -1;
        length -= sent;
        cmdp += sent;
    }
    return 0;
}

variable_types: enum FSMSG_TYPE { FSMSG_OPEN=0, FSMSG_WRITE=1, FSMSG_READ=2, FSMSG_LSEEK=3, FSMSG_CLOSE=4, FSMSG_STAT=5 }

variable_types: fd int

variable_types: struct fs_command { char type; int fd; }

variable_types:
cmdp
void *

variable_types:
sent
unsigned long

variable_types:
length
unsigned long


decompiled_code:
__int64 __fastcall fs_open_server(char *src)
{
unsigned int v1; // eax
unsigned int v2; // er12
char *v3; // rax
size_t v4; // rbx
__int16 v5; // bp
char *v6; // rdi
struct hostent *v7; // rax
int v8; // edx
int v9; // eax
struct sockaddr v11; // [rsp+0h] [rbp-438h] BYREF
char dest[1064]; // [rsp+10h] [rbp-428h] BYREF

v1 = get_free_handle(src);
if ( v1 == -1 )
return (unsigned int)-2;
v2 = v1;
v3 = strchr(src, 58);
if ( v3 )
{
v4 = v3 - src;
v5 = atoi(v3 + 1);
v6 = strncpy(dest, src, v4);
}
else
{
v5 = 1337;
v6 = strncpy(dest, src, 0x400uLL);
}
v7 = gethostbyname(v6);
if ( !v7 )
return (unsigned int)-4;
memcpy(&v11.sa_data[2], *(const void **)v7->h_addr_list, v7->h_length);
v11.sa_family = 2;
*(_WORD *)v11.sa_data = _bswap_16(v5);
v9 = socket(2, 1, v8);
servers[2 * v2 + 1] = v9;
if ( v9 < 0 || connect(v9, &v11, 0x10u) == -1 )
return (unsigned int)-3;
servers[2 * v2] = 1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src)
{
unsigned int v1; // eax
unsigned int v2; // er12
char *v3; // rax
size_t v4; // rbx
__int16 v5; // bp
char *v6; // rdi
struct hostent *v7; // rax
int v8; // edx
int v9; // eax
struct sockaddr v11; // [rsp+0h] [rbp-438h] BYREF
char dest[1064]; // [rsp+10h] [rbp-428h] BYREF

v1 = func0(src);
if ( v1 == -1 )
return (unsigned int)-2;
v2 = v1;
v3 = strchr(src, 58);
if ( v3 )
{
v4 = v3 - src;
v5 = atoi(v3 + 1);
v6 = strncpy(dest, src, v4);
}
else
{
v5 = 1337;
v6 = strncpy(dest, src, 0x400uLL);
}
v7 = gethostbyname(v6);
if ( !v7 )
return (unsigned int)-4;
memcpy(&v11.sa_data[2], *(const void **)v7->h_addr_list, v7->h_length);
v11.sa_family = 2;
*(_WORD *)v11.sa_data = func1(v5);
v9 = socket(2, 1, v8);
servers[2 * v2 + 1] = v9;
if ( v9 < 0 || connect(v9, &v11, 0x10u) == -1 )
return (unsigned int)-3;
servers[2 * v2] = 1;
return v2;
}

original_code:
int fs_open_server(const char *server_addr)
{
    int server_handle, port = 1337;
    struct serverd *sd;
    struct sockaddr_in addr;
    struct hostent *hp;
    char host_part[1024], *port_part;
    if (-1 == (server_handle = get_free_handle()))
        return (-2);
    sd = &servers[server_handle];
    if (0 != (port_part = strchr(server_addr, ':'))) {
        port = atoi(port_part + 1);
        strncpy(host_part, server_addr, port_part - server_addr);
    } else {
        strncpy(host_part, server_addr, sizeof(host_part));
    }
    hp = gethostbyname(host_part);
    if (hp == 0)
        return (-4);
    memcpy((char*)&addr.sin_addr, (char*)hp->h_addr_list[0], hp->h_length);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    sd->sock = socket(2, SOCK_STREAM, 0);
    if (sd->sock < 0)
        return (-3);
    if (-1 == connect(sd->sock, (struct sockaddr*)&addr, sizeof(addr)))
        return (-3);
    sd->in_use = 1;
    return server_handle;
}

canonical_original_code:
int fs_open_server(char *server_addr)
{
    int server_handle; int port = 1337;
    struct serverd *sd;
    struct sockaddr_in addr;
    struct hostent *hp;
    char host_part[1024]; char *port_part;
    if (-1 == (server_handle = get_free_handle()))
        return (-2);
    sd = &servers[server_handle];
    if (0 != (port_part = strchr(server_addr, ':'))) {
        port = atoi(port_part + 1);
        strncpy(host_part, server_addr, port_part - server_addr);
    } else {
        strncpy(host_part, server_addr, sizeof(host_part));
    }
    hp = gethostbyname(host_part);
    if (hp == 0)
        return (-4);
    memcpy((char *)&addr.sin_addr, (char *)hp->h_addr_list[0], hp->h_length);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    sd->sock = socket(2, SOCK_STREAM, 0);
    if (sd->sock < 0)
        return (-3);
    if (-1 == connect(sd->sock, (struct sockaddr *)&addr, sizeof(addr)))
        return (-3);
    sd->in_use = 1;
    return server_handle;
}

variable_types: port int

variable_types: struct serverd { int in_use; int sock; } *

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

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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 fs_close_server(unsigned int a1)
{
int result; // eax

if ( a1 > 0x1F || !servers[2 * a1] )
return -5;
servers[2 * a1] = 0;
result = close(servers[2 * a1 + 1]);
if ( result )
result = -3;
return result;
}

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

if ( a1 > 0x1F || !servers[2 * a1] )
return -5;
servers[2 * a1] = 0;
result = close(servers[2 * a1 + 1]);
if ( result )
result = -3;
return result;
}

original_code:
int fs_close_server(int server_handle)
{
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    servers[server_handle].in_use = 0;
    if (0 == close(servers[server_handle].sock))
        return (0);
    else
        return (-3);
}


decompiled_code:
__int64 __fastcall fs_open(unsigned int a1, const char *a2, int a3)
{
__int64 result; // rax
int v4; // [rsp+0h] [rbp-130h] BYREF
int buf[74]; // [rsp+8h] [rbp-128h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
memset(buf, 0, 0x110uLL);
buf[2] = a3;
strncpy((char *)&buf[4], a2, 0x100uLL);
if ( write(servers[2 * a1 + 1], buf, 0x110uLL) != 272 || (unsigned int)read_response(servers[2 * a1 + 1], &v4) == -1 )
result = 4294967293LL;
else
result = process_response(&v4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, const char *a2, int a3)
{
__int64 result; // rax
int v4; // [rsp+0h] [rbp-130h] BYREF
int buf[74]; // [rsp+8h] [rbp-128h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
memset(buf, 0, 0x110uLL);
buf[2] = a3;
strncpy((char *)&buf[4], a2, 0x100uLL);
if ( write(servers[2 * a1 + 1], buf, 0x110uLL) != 272 || (unsigned int)func2(servers[2 * a1 + 1], &v4) == -1 )
result = 4294967293LL;
else
result = func3(&v4);
return result;
}

original_code:
int fs_open(int server_handle, const char *name, int flags)
{
    struct fs_open_command cmd;
    struct fs_response res;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    memset(&cmd, 0, sizeof(cmd));
    cmd.base_command.type = FSMSG_OPEN;
    cmd.base_command.arg1 = flags;
    strncpy(cmd.filename, name, 256);
    if (sizeof(cmd) != write(servers[server_handle].sock, &cmd, sizeof(cmd)))
        return (-3);
    if (-1 == read_response(servers[server_handle].sock, &res))
        return (-3);
    return process_response(&res);
}

canonical_original_code:
int fs_open(int server_handle, char *name, int flags)
{
    struct fs_open_command cmd;
    struct fs_response res;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    memset(&cmd, 0, sizeof(cmd));
    cmd.base_command.type = FSMSG_OPEN;
    cmd.base_command.arg1 = flags;
    strncpy(cmd.filename, name, 256);
    if (sizeof(cmd) != write(servers[server_handle].sock, &cmd, sizeof(cmd)))
        return (-3);
    if (-1 == read_response(servers[server_handle].sock, &res))
        return (-3);
    return process_response(&res);
}

variable_types:
name
char *

variable_types: flags int

variable_types: struct fs_open_command { struct fs_command { char type; int fd; } base_command; char filename[256]; }

variable_types: struct fs_response { int status; int val; }

user_defined_types: struct fs_open_command { struct fs_command base_command; char filename[256]; }


decompiled_code:
__int64 __fastcall fs_write(unsigned int a1, int a2, char *a3, size_t a4)
{
size_t v5; // rbx
int v6; // er12
ssize_t v7; // rax
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 <= 0x1F && servers[2 * a1] )
{
v5 = a4;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)write_command(v6, 1, a2, a4, 0) != -1 )
{
if ( v5 )
{
while ( 1 )
{
v7 = send(v6, a3, v5, 0);
if ( v7 == -1 )
break;
a3 += v7;
v5 -= v7;
if ( !v5 )
goto LABEL_9;
}
}
else
{
LABEL_9:
if ( (unsigned int)read_response(v6, buf) != -1 )
return process_response(buf);
}
}
return 4294967293LL;
}
return 4294967291LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, int a2, char *a3, size_t a4)
{
size_t v5; // rbx
int v6; // er12
ssize_t v7; // rax
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 <= 0x1F && servers[2 * a1] )
{
v5 = a4;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)func4(v6, 1, a2, a4, 0) != -1 )
{
if ( v5 )
{
while ( 1 )
{
v7 = send(v6, a3, v5, 0);
if ( v7 == -1 )
break;
a3 += v7;
v5 -= v7;
if ( !v5 )
goto LABEL_9;
}
}
else
{
LABEL_9:
if ( (unsigned int)func2(v6, buf) != -1 )
return func3(buf);
}
}
return 4294967293LL;
}
return 4294967291LL;
}

original_code:
int fs_write(int server_handle, int fd, void *buf, size_t len)
{
    struct fs_response res;
    size_t sent = 0;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_WRITE, fd, len, 0))
        return (-3);
    for (; len > 0; len -= sent) {
        sent = send(sock, buf, len, 0);
        if (sent == -1)
            return (-3);
        buf += sent;
    }
    if (-1 == read_response(sock, &res))
        return (-3);
    return process_response(&res);
}

canonical_original_code:
int fs_write(int server_handle, int fd, void *buf, unsigned long len)
{
    struct fs_response res;
    unsigned long sent = 0;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_WRITE, fd, len, 0))
        return (-3);
    for (; len > 0; len -= sent) {
        sent = send(sock, buf, len, 0);
        if (sent == -1)
            return (-3);
        buf += sent;
    }
    if (-1 == read_response(sock, &res))
        return (-3);
    return process_response(&res);
}

variable_types:
buf
void *

variable_types:
len
unsigned long

variable_types: sock int


decompiled_code:
__int64 __fastcall fs_read(unsigned int a1, int a2, char *a3, unsigned __int64 a4)
{
int v4; // ebp
int v7; // er12
size_t v8; // rdx
int v9; // er12
int v10; // eax
int buf; // [rsp+8h] [rbp-30h] BYREF
unsigned int v13; // [rsp+Ch] [rbp-2Ch]

if ( a1 <= 0x1F && servers[2 * a1] )
{
v4 = servers[2 * a1 + 1];
if ( (unsigned int)write_command(v4, 2, a2, a4, 0) != -1 && (unsigned int)read_response(v4, &buf) != -1 )
{
v7 = buf;
if ( buf )
{
*_errno_location() = v7;
return (unsigned int)-1;
}
v8 = (int)v13;
v9 = v13;
if ( (int)v13 > a4 )
return (unsigned int)-6;
if ( (int)v13 <= 0 )
return (unsigned int)v9;
while ( 1 )
{
v10 = recv(v4, a3, v8, 0);
if ( v10 <= 0 )
break;
v9 -= v10;
a3 += v10;
if ( v9 <= 0 )
return v13;
v8 = v9;
}
}
v9 = -3;
}
else
{
v9 = -5;
}
return (unsigned int)v9;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, int a2, char *a3, unsigned __int64 a4)
{
int v4; // ebp
int v7; // er12
size_t v8; // rdx
int v9; // er12
int v10; // eax
int buf; // [rsp+8h] [rbp-30h] BYREF
unsigned int v13; // [rsp+Ch] [rbp-2Ch]

if ( a1 <= 0x1F && servers[2 * a1] )
{
v4 = servers[2 * a1 + 1];
if ( (unsigned int)func4(v4, 2, a2, a4, 0) != -1 && (unsigned int)func2(v4, &buf) != -1 )
{
v7 = buf;
if ( buf )
{
*_errno_location() = v7;
return (unsigned int)-1;
}
v8 = (int)v13;
v9 = v13;
if ( (int)v13 > a4 )
return (unsigned int)-6;
if ( (int)v13 <= 0 )
return (unsigned int)v9;
while ( 1 )
{
v10 = recv(v4, a3, v8, 0);
if ( v10 <= 0 )
break;
v9 -= v10;
a3 += v10;
if ( v9 <= 0 )
return v13;
v8 = v9;
}
}
v9 = -3;
}
else
{
v9 = -5;
}
return (unsigned int)v9;
}

original_code:
int fs_read(int server_handle, int fd, void *buf, size_t len)
{
    struct fs_response res;
    int sock, rcvd, response_len;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_READ, fd, len, 0))
        return (-3);
    if (-1 == read_response(sock, &res))
        return (-3);
    if (res.status) {
        (*__errno_location ()) = res.status;
        return (-1);
    }
    response_len = res.val;
    if (response_len > len)
        return (-6);
    for (; response_len > 0; response_len -= rcvd) {
        rcvd = recv(sock, buf, response_len, 0);
        if (rcvd <= 0)
            return (-3);
        buf += rcvd;
    }
    return res.val;
}

canonical_original_code:
int fs_read(int server_handle, int fd, void *buf, unsigned long len)
{
    struct fs_response res;
    int sock; int rcvd; int response_len;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_READ, fd, len, 0))
        return (-3);
    if (-1 == read_response(sock, &res))
        return (-3);
    if (res.status) {
        (*__errno_location ()) = res.status;
        return (-1);
    }
    response_len = res.val;
    if (response_len > len)
        return (-6);
    for (; response_len > 0; response_len -= rcvd) {
        rcvd = recv(sock, buf, response_len, 0);
        if (rcvd <= 0)
            return (-3);
        buf += rcvd;
    }
    return res.val;
}

variable_types: rcvd int


decompiled_code:
__int64 __fastcall fs_lseek(unsigned int a1, int a2, int a3, int a4)
{
int v4; // er12
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F || !servers[2 * a1] )
return 4294967291LL;
v4 = servers[2 * a1 + 1];
write_command(v4, 3, a2, a3, a4);
read_response(v4, buf);
return process_response(buf);
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1, int a2, int a3, int a4)
{
int v4; // er12
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F || !servers[2 * a1] )
return 4294967291LL;
v4 = servers[2 * a1 + 1];
func4(v4, 3, a2, a3, a4);
func2(v4, buf);
return func3(buf);
}

original_code:
int fs_lseek(int server_handle, int fd, long offset, int whence)
{
    struct fs_response res;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_LSEEK, fd, offset, whence);
    read_response(sock, &res);
    return process_response(&res);
}

variable_types: fd int

variable_types: offset long

variable_types: whence int


decompiled_code:
__int64 __fastcall fs_close(unsigned int a1, int a2)
{
int v2; // er12
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F || !servers[2 * a1] )
return 4294967291LL;
v2 = servers[2 * a1 + 1];
write_command(v2, 4, a2, 0, 0);
read_response(v2, buf);
return process_response(buf);
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, int a2)
{
int v2; // er12
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F || !servers[2 * a1] )
return 4294967291LL;
v2 = servers[2 * a1 + 1];
func4(v2, 4, a2, 0, 0);
func2(v2, buf);
return func3(buf);
}

original_code:
int fs_close(int server_handle, int fd)
{
    struct fs_response res;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_CLOSE, fd, 0, 0);
    read_response(sock, &res);
    return process_response(&res);
}


original_code:
int fs_fstat(int server_handle, int fd, struct fs_stat *buf)
{
    int sock;
    struct fs_stat_response resp;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_STAT, fd, 0, 0);
    read_stat_response(sock, &resp);
    memcpy(buf, &(resp.stat), sizeof(struct fs_stat));
    return process_response(&resp.base_response);
}

variable_types: struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; } *

variable_types: struct fs_stat_response { struct fs_response { int status; int val; } base_response; struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; } stat; }

user_defined_types: struct fs_stat_response { struct fs_response base_response; struct fs_stat stat; }


decompiled_code:
__int64 __fastcall send_reponse(int a1, int a2, int a3)
{
int buf[4]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = a2;
buf[1] = a3;
send(a1, buf, 8uLL, 0);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3)
{
int buf[4]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = a2;
buf[1] = a3;
send(a1, buf, 8uLL, 0);
return 0LL;
}

original_code:
int send_reponse(int sock, int status, int value) {
    struct fs_response resp = {status, value};
    send(sock, &resp, sizeof(resp), 0);
    return 0;
}

variable_types: status int

variable_types: value int


original_code:
int send_stat_reponse(int sock, int status, int value, struct stat *st) {
    struct fs_stat_response resp;
    resp.base_response.status = status;
    resp.base_response.val = value;
    resp.stat.st_mode = st->st_mode;
    resp.stat.st_size = st->st_size;
    resp.stat.st_atim = st->st_atim.tv_sec;
    resp.stat.st_mtim = st->st_mtim.tv_sec;
    resp.stat.st_ctim = st->st_ctim.tv_sec;
    resp.stat.st_blocks = st->st_blocks;
    resp.stat.st_blksize = st->st_blksize;
    send(sock, &resp, sizeof(resp), 0);
    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]; } *

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

user_defined_types: struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; }


decompiled_code:
__int64 __fastcall handle_connection(int fd)
{
int v1; // eax
int v2; // er12
int *v3; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rbx
int v7; // edx
int v8; // er12
int *v9; // rax
int v10; // eax
int v11; // edx
int v12; // eax
int v13; // er12
int *v14; // rax
char buf; // [rsp+Fh] [rbp-29D9h] BYREF
char v17[4]; // [rsp+10h] [rbp-29D8h] BYREF
int fda; // [rsp+14h] [rbp-29D4h]
int v19; // [rsp+18h] [rbp-29D0h]
int whence; // [rsp+1Ch] [rbp-29CCh]
struct stat stat_buf; // [rsp+20h] [rbp-29C8h] BYREF
char v22[8]; // [rsp+B0h] [rbp-2938h] BYREF
int oflag; // [rsp+B8h] [rbp-2930h]
char file[256]; // [rsp+C0h] [rbp-2928h] BYREF
char v25[10280]; // [rsp+1C0h] [rbp-2828h] BYREF

while ( 2 )
{
if ( recv(fd, &buf, 1uLL, 2) == 1 )
{
switch ( buf )
{
case 0:
recv(fd, v22, 0x110uLL, 256);
file[255] = 0;
printf("received FSMSG_OPEN, filename = %s\n", file);
v1 = open(file, oflag, 432LL);
goto LABEL_4;
case 1:
v2 = 0;
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_WRITE, fd = %i\n", (unsigned int)fda);
while ( 1 )
{
v11 = 10240;
if ( v19 <= 10240 )
v11 = v19;
v12 = read(fd, v25, v11);
if ( v12 <= 0 )
break;
v19 -= v12;
v2 += v12;
write(fda, v25, v12);
}
goto LABEL_5;
case 2:
recv(fd, v17, 0x10uLL, 256);
v6 = v25;
printf("received FSMSG_READ, fd = %i\n", (unsigned int)fda);
v7 = 10240;
if ( v19 <= 10240 )
v7 = v19;
v8 = read(fda, v25, v7);
v9 = __errno_location();
send_reponse(fd, *v9, v8);
for ( ; v8; v8 -= v10 )
{
v10 = write(fd, v6, v8);
v6 += v10;
}
continue;
case 3:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_LSEEK, fd = %i\n", (unsigned int)fda);
v4 = lseek(fda, v19, whence);
v5 = __errno_location();
send_reponse(fd, *v5, v4);
continue;
case 4:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_CLOSE, fd = %i\n", (unsigned int)fda);
v1 = close(fda);
LABEL_4:
v2 = v1;
LABEL_5:
v3 = __errno_location();
send_reponse(fd, *v3, v2);
continue;
case 5:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_STAT, fd = %i\n", (unsigned int)fda);
v13 = fstat(fda, &stat_buf);
v14 = __errno_location();
send_stat_reponse(fd, *v14, v13, (__int64)&stat_buf);
continue;
default:
printf("unknown msg type %d\n", (unsigned int)buf);
return 1LL;
}
}
break;
}
puts("subprocess exiting");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
int v1; // eax
int v2; // er12
int *v3; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rbx
int v7; // edx
int v8; // er12
int *v9; // rax
int v10; // eax
int v11; // edx
int v12; // eax
int v13; // er12
int *v14; // rax
char buf; // [rsp+Fh] [rbp-29D9h] BYREF
char v17[4]; // [rsp+10h] [rbp-29D8h] BYREF
int fda; // [rsp+14h] [rbp-29D4h]
int v19; // [rsp+18h] [rbp-29D0h]
int whence; // [rsp+1Ch] [rbp-29CCh]
struct stat stat_buf; // [rsp+20h] [rbp-29C8h] BYREF
char v22[8]; // [rsp+B0h] [rbp-2938h] BYREF
int oflag; // [rsp+B8h] [rbp-2930h]
char file[256]; // [rsp+C0h] [rbp-2928h] BYREF
char v25[10280]; // [rsp+1C0h] [rbp-2828h] BYREF

while ( 2 )
{
if ( recv(fd, &buf, 1uLL, 2) == 1 )
{
switch ( buf )
{
case 0:
recv(fd, v22, 0x110uLL, 256);
file[255] = 0;
printf("received FSMSG_OPEN, filename = %s\n", file);
v1 = open(file, oflag, 432LL);
goto LABEL_4;
case 1:
v2 = 0;
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_WRITE, fd = %i\n", (unsigned int)fda);
while ( 1 )
{
v11 = 10240;
if ( v19 <= 10240 )
v11 = v19;
v12 = read(fd, v25, v11);
if ( v12 <= 0 )
break;
v19 -= v12;
v2 += v12;
write(fda, v25, v12);
}
goto LABEL_5;
case 2:
recv(fd, v17, 0x10uLL, 256);
v6 = v25;
printf("received FSMSG_READ, fd = %i\n", (unsigned int)fda);
v7 = 10240;
if ( v19 <= 10240 )
v7 = v19;
v8 = read(fda, v25, v7);
v9 = __errno_location();
func8(fd, *v9, v8);
for ( ; v8; v8 -= v10 )
{
v10 = write(fd, v6, v8);
v6 += v10;
}
continue;
case 3:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_LSEEK, fd = %i\n", (unsigned int)fda);
v4 = lseek(fda, v19, whence);
v5 = __errno_location();
func8(fd, *v5, v4);
continue;
case 4:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_CLOSE, fd = %i\n", (unsigned int)fda);
v1 = close(fda);
LABEL_4:
v2 = v1;
LABEL_5:
v3 = __errno_location();
func8(fd, *v3, v2);
continue;
case 5:
recv(fd, v17, 0x10uLL, 256);
printf("received FSMSG_STAT, fd = %i\n", (unsigned int)fda);
v13 = func13(fda, &stat_buf);
v14 = __errno_location();
func9(fd, *v14, v13, (__int64)&stat_buf);
continue;
default:
printf("unknown msg type %d\n", (unsigned int)buf);
return 1LL;
}
}
break;
}
puts("subprocess exiting");
return 0LL;
}

original_code:
int handle_connection(int sock, struct sockaddr_in *addr)
{
    int8_t cmd_type;
    struct fs_open_command cmd_open;
    struct fs_command cmd;
    int read_bytes, written_bytes, ret, file;
    char buffer[10240], *bufp = 0;
    while (1 == recv(sock, &cmd_type, sizeof(cmd_type), MSG_PEEK)) {
        switch (cmd_type) {
        case FSMSG_OPEN:
            recv(sock, &cmd_open, sizeof(cmd_open), MSG_WAITALL);
            cmd_open.filename[255] = 0;
            printf("received FSMSG_OPEN, filename = %s\n", cmd_open.filename);
            file = open(cmd_open.filename, cmd_open.base_command.arg1, 0660);
            send_reponse(sock, (*__errno_location ()), file);
            break;
        case FSMSG_WRITE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_WRITE, fd = %i\n", cmd.fd);
            ret = 0;
            while (0 < (read_bytes = read(sock, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) )))) {
                ret += read_bytes;
                cmd.arg1 -= read_bytes;
                write(cmd.fd, buffer, read_bytes);
            }
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_READ:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_READ, fd = %i\n", cmd.fd);
            read_bytes = read(cmd.fd, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) ));
            send_reponse(sock, (*__errno_location ()), read_bytes);
            bufp = buffer;
            while (read_bytes) {
                written_bytes = write(sock, bufp, read_bytes);
                read_bytes -= written_bytes;
                bufp += written_bytes;
            }
            break;
        case FSMSG_LSEEK:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_LSEEK, fd = %i\n", cmd.fd);
            ret = lseek(cmd.fd, cmd.arg1, cmd.arg2);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_CLOSE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_CLOSE, fd = %i\n", cmd.fd);
            ret = close(cmd.fd);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_STAT:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_STAT, fd = %i\n", cmd.fd);
            struct stat sb;
            ret = fstat(cmd.fd, &sb);
            send_stat_reponse(sock, (*__errno_location ()), ret, &sb);
            break;
        default:
            printf("unknown msg type %d\n", cmd_type);
            return 1;
        }
    }
    printf("subprocess exiting\n");
    return 0;
}

canonical_original_code:
int handle_connection(int sock, struct sockaddr_in *addr)
{
    char cmd_type;
    struct fs_open_command cmd_open;
    struct fs_command cmd;
    int read_bytes; int written_bytes; int ret; int file;
    char buffer[10240]; char *bufp = 0;
    while (1 == recv(sock, &cmd_type, sizeof(cmd_type), MSG_PEEK)) {
        switch (cmd_type) {
        case FSMSG_OPEN:
            recv(sock, &cmd_open, sizeof(cmd_open), MSG_WAITALL);
            cmd_open.filename[255] = 0;
            printf("received FSMSG_OPEN, filename = %s\n", cmd_open.filename);
            file = open(cmd_open.filename, cmd_open.base_command.arg1, 0660);
            send_reponse(sock, (*__errno_location ()), file);
            break;
        case FSMSG_WRITE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_WRITE, fd = %i\n", cmd.fd);
            ret = 0;
            while (0 < (read_bytes = read(sock, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) )))) {
                ret += read_bytes;
                cmd.arg1 -= read_bytes;
                write(cmd.fd, buffer, read_bytes);
            }
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_READ:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_READ, fd = %i\n", cmd.fd);
            read_bytes = read(cmd.fd, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) ));
            send_reponse(sock, (*__errno_location ()), read_bytes);
            bufp = buffer;
            while (read_bytes) {
                written_bytes = write(sock, bufp, read_bytes);
                read_bytes -= written_bytes;
                bufp += written_bytes;
            }
            break;
        case FSMSG_LSEEK:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_LSEEK, fd = %i\n", cmd.fd);
            ret = lseek(cmd.fd, cmd.arg1, cmd.arg2);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_CLOSE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_CLOSE, fd = %i\n", cmd.fd);
            ret = close(cmd.fd);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_STAT:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_STAT, fd = %i\n", cmd.fd);
            struct stat sb;
            ret = fstat(cmd.fd, &sb);
            send_stat_reponse(sock, (*__errno_location ()), ret, &sb);
            break;
        default:
            printf("unknown msg type %d\n", cmd_type);
            return 1;
        }
    }
    printf("subprocess exiting\n");
    return 0;
}

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

variable_types: struct fs_command { char type; int fd; }

variable_types: ret int

variable_types: file int

variable_types:
buffer
char [10240]

variable_types:
bufp
char *

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
unsigned int v4; // er12
char *v5; // rax
int v6; // ebp
unsigned __int16 v7; // ax
int result; // eax
int optval; // [rsp+8h] [rbp-40h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v12; // [rsp+20h] [rbp-28h] BYREF

v3 = socket(2, 1, 0);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)addr.sa_data = _bswap_16(1337LL);
if ( argc > 1 )
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
if ( argc != 2 )
{
v7 = atoi(argv[2]);
*(_WORD *)addr.sa_data = _bswap_16(v7);
}
}
optval = 1;
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
result = 1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
perror("bind");
result = 1;
}
else if ( listen(v3, 128) == -1 )
{
perror("listen");
result = 1;
}
else
{
while ( 1 )
{
addr_len = 16;
v6 = accept(v3, &v12, &addr_len);
if ( v6 < 0 )
return 1;
v4 = fork();
if ( !v4 )
break;
v5 = inet_ntoa(*(struct in_addr *)&v12.sa_data[2]);
printf("connection from %s spawned process %d\n", v5, v4);
close(v6);
}
close(v3);
result = handle_connection(v6);
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
unsigned int v4; // er12
char *v5; // rax
int v6; // ebp
unsigned __int16 v7; // ax
int result; // eax
int optval; // [rsp+8h] [rbp-40h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v12; // [rsp+20h] [rbp-28h] BYREF

v3 = socket(2, 1, 0);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)addr.sa_data = func7(1337LL);
if ( argc > 1 )
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
if ( argc != 2 )
{
v7 = atoi(argv[2]);
*(_WORD *)addr.sa_data = func7(v7);
}
}
optval = 1;
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
result = 1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
perror("bind");
result = 1;
}
else if ( listen(v3, 128) == -1 )
{
perror("listen");
result = 1;
}
else
{
while ( 1 )
{
addr_len = 16;
v6 = accept(v3, &v12, &addr_len);
if ( v6 < 0 )
return 1;
v4 = fork();
if ( !v4 )
break;
v5 = inet_ntoa(*(struct in_addr *)&v12.sa_data[2]);
printf("connection from %s spawned process %d\n", v5, v4);
close(v6);
}
close(v3);
result = func10(v6);
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    int listener_sock = socket(2, SOCK_STREAM, 0);
    addr.sin_family = 2;
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr.sin_port = htons(1337);
    if (argc >= 2)
        addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (argc >= 3)
        addr.sin_port = htons(atoi(argv[2]));
    int yes=1;
    if (-1 == setsockopt(listener_sock, 1, 2, &yes, sizeof(int))) {
        perror("setsockopt");
        return 1;
    }
    if (-1 == bind(listener_sock, (struct sockaddr*)&addr, sizeof(addr))) {
        perror("bind");
        return 1;
    }
    if (-1 == listen(listener_sock, 128)) {
        perror("listen");
        return 1;
    }
    while (1) {
        int handler_pid;
        struct sockaddr_in client_addr;
        socklen_t len = sizeof(client_addr);
        int connection_sock;
        connection_sock = accept(
            listener_sock, (struct sockaddr*)&client_addr, &len);
        if (connection_sock < 0)
            return 1;
        handler_pid = fork();
        if (handler_pid == 0) {
            close(listener_sock);
            return handle_connection(connection_sock, &client_addr);
        } else {
            printf("connection from %s spawned process %d\n",
                inet_ntoa(client_addr.sin_addr), handler_pid);
            close(connection_sock);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    int listener_sock = socket(2, SOCK_STREAM, 0);
    addr.sin_family = 2;
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr.sin_port = htons(1337);
    if (argc >= 2)
        addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (argc >= 3)
        addr.sin_port = htons(atoi(argv[2]));
    int yes = 1;
    if (-1 == setsockopt(listener_sock, 1, 2, &yes, sizeof(int))) {
        perror("setsockopt");
        return 1;
    }
    if (-1 == bind(listener_sock, (struct sockaddr *)&addr, sizeof(addr))) {
        perror("bind");
        return 1;
    }
    if (-1 == listen(listener_sock, 128)) {
        perror("listen");
        return 1;
    }
    while (1) {
        int handler_pid;
        struct sockaddr_in client_addr;
        unsigned int len = sizeof(client_addr);
        int connection_sock;
        connection_sock = accept(
            listener_sock, (struct sockaddr *)&client_addr, &len);
        if (connection_sock < 0)
            return 1;
        handler_pid = fork();
        if (handler_pid == 0) {
            close(listener_sock);
            return handle_connection(connection_sock, &client_addr);
        } else {
            printf("connection from %s spawned process %d\n",
                inet_ntoa(client_addr.sin_addr), handler_pid);
            close(connection_sock);
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
len
unsigned int


decompiled_code:
void __fastcall __noreturn die_with_error(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void die_with_error(char *message) {
    perror(message);
    exit(1);
}

variable_types:
message
char *


decompiled_code:
__int64 __fastcall setup_server_socket(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
die_with_error("could not create socket");
v2 = v1;
*(_QWORD *)&v5[0].sa_data[6] = 0LL;
v5[0].sa_family = 2;
*(_QWORD *)&v5[0].sa_data[2] = (unsigned int)_bswap_32_constprop_0(a1, v5, 16LL);
*(_WORD *)v5[0].sa_data = _bswap_16(a1);
if ( bind(v2, v5, v3) < 0 )
die_with_error("bind() failed");
if ( listen(v2, 5) < 0 )
die_with_error("listen() failed");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // er12
socklen_t v3; // edx
struct sockaddr v5[2]; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
func3("could not create socket");
v2 = v1;
*(_QWORD *)&v5[0].sa_data[6] = 0LL;
v5[0].sa_family = 2;
*(_QWORD *)&v5[0].sa_data[2] = (unsigned int)func2(a1, v5, 16LL);
*(_WORD *)v5[0].sa_data = func0(a1);
if ( bind(v2, v5, v3) < 0 )
func3("bind() failed");
if ( listen(v2, 5) < 0 )
func3("listen() failed");
return v2;
}

original_code:
int setup_server_socket(unsigned short port) {
    int servSock;
    struct sockaddr_in servAddr;
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        die_with_error("could not create socket");
    }
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    servAddr.sin_port = htons(port);
    if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        die_with_error("bind() failed");
    }
    if (listen(servSock, 5) < 0) {
        die_with_error("listen() failed");
    }
    return servSock;
}

canonical_original_code:
int setup_server_socket(unsigned short port) {
    int servSock;
    struct sockaddr_in servAddr;
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        die_with_error("could not create socket");
    }
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    servAddr.sin_port = htons(port);
    if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        die_with_error("bind() failed");
    }
    if (listen(servSock, 5) < 0) {
        die_with_error("listen() failed");
    }
    return servSock;
}

variable_types:
port
unsigned short

variable_types: servSock int


decompiled_code:
__int64 __fastcall send_data(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er13
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( a3 > 0 )
{
v4 = 0;
do
{
v5 = send(fd, (const void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
die_with_error("write() failed");
v3 -= v5;
v4 += v5;
}
while ( v3 > 0 );
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // er13
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( a3 > 0 )
{
v4 = 0;
do
{
v5 = send(fd, (const void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
func3("write() failed");
v3 -= v5;
v4 += v5;
}
while ( v3 > 0 );
}
return 1LL;
}

original_code:
_Bool send_data(int sock, void *data, int size) {
    int sent = 0;
    int remaining = size;
    int result;
    if (data == ((void *)0) || size == 0) return 1;
    while (remaining > 0) {
        result = send(sock, (unsigned char *) data + sent, remaining, 0);
        if (result < 0) die_with_error("write() failed");
        sent += result;
        remaining -= result;
    }
    return 1;
}

variable_types: sock int

variable_types:
data
void *

variable_types: size int

variable_types: sent int

variable_types: remaining int

variable_types: result int


decompiled_code:
__int64 __fastcall get_request_header(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = 0;
v4 = a3;
do
{
v5 = recv(fd, (void *)(a2 + v3), v4, 0);
if ( v5 < 0 )
die_with_error("read() in get_request failed");
v4 -= v5;
v3 += v5;
}
while ( v4 > 0 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = 0;
v4 = a3;
do
{
v5 = recv(fd, (void *)(a2 + v3), v4, 0);
if ( v5 < 0 )
func3("read() in get_request failed");
v4 -= v5;
v3 += v5;
}
while ( v4 > 0 );
return 1LL;
}

original_code:
_Bool get_request_header(int sock, request_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_request failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

canonical_original_code:
_Bool get_request_header(int sock, struct lrequest_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_request failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

variable_types: struct lrequest_header { unsigned int type; unsigned int size; } *

variable_types: received int


decompiled_code:
__int64 __fastcall get_response_header(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = 0;
v4 = a3;
do
{
v5 = recv(fd, (void *)(a2 + v3), v4, 0);
if ( v5 < 0 )
die_with_error("read() in get_response failed");
v4 -= v5;
v3 += v5;
}
while ( v4 > 0 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // ebx
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = 0;
v4 = a3;
do
{
v5 = recv(fd, (void *)(a2 + v3), v4, 0);
if ( v5 < 0 )
func3("read() in get_response failed");
v4 -= v5;
v3 += v5;
}
while ( v4 > 0 );
return 1LL;
}

original_code:
_Bool get_response_header(int sock, response_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_response failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

canonical_original_code:
_Bool get_response_header(int sock, struct lresponse_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_response failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

variable_types: struct lresponse_header { unsigned int status; unsigned int size; } *


decompiled_code:
char *__fastcall get_data(int fd, int a2)
{
int v2; // ebx
char *v3; // r12
size_t v4; // rdx
int v5; // er13
int v6; // eax

v2 = a2;
v3 = (char *)calloc(a2, 1uLL);
if ( a2 > 0 )
{
v4 = a2;
v5 = 0;
while ( 1 )
{
v6 = recv(fd, &v3[v5], v4, 0);
if ( v6 < 0 )
die_with_error("read() failed");
v2 -= v6;
v5 += v6;
if ( v2 <= 0 )
break;
v4 = v2;
}
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func8(int fd, int a2)
{
int v2; // ebx
char *v3; // r12
size_t v4; // rdx
int v5; // er13
int v6; // eax

v2 = a2;
v3 = (char *)calloc(a2, 1uLL);
if ( a2 > 0 )
{
v4 = a2;
v5 = 0;
while ( 1 )
{
v6 = recv(fd, &v3[v5], v4, 0);
if ( v6 < 0 )
func3("read() failed");
v2 -= v6;
v5 += v6;
if ( v2 <= 0 )
break;
v4 = v2;
}
}
return v3;
}

original_code:
unsigned char *get_data(int sock, int size) {
    int received = 0;
    int remaining = size;
    int result;
    char *buffer = malloc(size);
    memset(buffer, 0, size);
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) buffer + received, remaining, 0);
        if (result < 0) die_with_error("read() failed");
        received += result;
        remaining -= result;
    }
    return buffer;
}

variable_types:
buffer
char *

return_type: unsigned char *


decompiled_code:
int __fastcall accept_connection(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

addr_len = 128;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
die_with_error("accept() failed");
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

addr_len = 128;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
func3("accept() failed");
return result;
}

original_code:
int accept_connection(int servSock) {
    struct sockaddr_storage clientAddr;
    socklen_t clientAddrLen = sizeof(clientAddr);
    int clientSock = accept(servSock, (struct sockaddr *) &clientAddr, &clientAddrLen);
    if (clientSock < 0) {
        die_with_error("accept() failed");
    }
    return clientSock;
}

canonical_original_code:
int accept_connection(int servSock) {
    struct sockaddr_storage clientAddr;
    unsigned int clientAddrLen = sizeof(clientAddr);
    int clientSock = accept(servSock, (struct sockaddr *) &clientAddr, &clientAddrLen);
    if (clientSock < 0) {
        die_with_error("accept() failed");
    }
    return clientSock;
}

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

variable_types:
clientAddrLen
unsigned int

variable_types: clientSock int


decompiled_code:
_BOOL8 __fastcall file_comparator(const char **a1, const char **a2)
{
int v2; // er8
_BOOL8 result; // rax

v2 = strcmp(*a1, *a2);
result = 0LL;
if ( !v2 )
result = strcmp(a1[1], a2[1]) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(const char **a1, const char **a2)
{
int v2; // er8
_BOOL8 result; // rax

v2 = strcmp(*a1, *a2);
result = 0LL;
if ( !v2 )
result = strcmp(a1[1], a2[1]) == 0;
return result;
}

original_code:
int file_comparator(const void *data1, const void *data2) {
    int result;
    file_entry *file1 = (file_entry *) data1;
    file_entry *file2 = (file_entry *) data2;
    result = ((strcmp(file1->name, file2->name) == 0) &&
              (strcmp(file1->checksum, file2->checksum) == 0));
    return result;
}

canonical_original_code:
int file_comparator(void *data1, void *data2) {
    int result;
    struct lfile_entry *file1 = (file_entry *) data1;
    struct lfile_entry *file2 = (file_entry *) data2;
    result = ((strcmp(file1->name, file2->name) == 0) &&
              (strcmp(file1->checksum, file2->checksum) == 0));
    return result;
}

variable_types: struct lfile_entry { char *name; char *checksum; unsigned long size; } *


decompiled_code:
int __fastcall print_files(const char **a1)
{
puts(*a1);
return puts(a1[1]);
}

canonical_decompiled_code:
int __fastcall func15(const char **a1)
{
puts(*a1);
return puts(a1[1]);
}

original_code:
void print_files(void *data) {
    file_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
    printf("%s\n", file->checksum);
}

canonical_original_code:
void print_files(void *data) {
    struct lfile_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
    printf("%s\n", file->checksum);
}

user_defined_types: struct lfile_entry { char *name; char *checksum; unsigned long size; }


decompiled_code:
char *__fastcall file_to_string(__int64 a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s\n%s\n%lu\n", *(const char **)a1, *(const char **)(a1 + 8), *(_QWORD *)(a1 + 16));
return ptr;
}

canonical_decompiled_code:
char *__fastcall func16(__int64 a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s\n%s\n%lu\n", *(const char **)a1, *(const char **)(a1 + 8), *(_QWORD *)(a1 + 16));
return ptr;
}

original_code:
char *file_to_string(void *data) {
    char *buffer;
    file_entry *file = (file_entry *) data;
    asprintf(&buffer, "%s\n%s\n%lu\n", file->name, file->checksum, file->size);
    return buffer;
}

canonical_original_code:
char *file_to_string(void *data) {
    char *buffer;
    struct lfile_entry *file = (file_entry *) data;
    asprintf(&buffer, "%s\n%s\n%lu\n", file->name, file->checksum, file->size);
    return buffer;
}

return_type: char *


decompiled_code:
int __fastcall print_filenames(const char **a1)
{
return puts(*a1);
}

canonical_decompiled_code:
int __fastcall func17(const char **a1)
{
return puts(*a1);
}

original_code:
void print_filenames(void *data) {
    file_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
}

canonical_original_code:
void print_filenames(void *data) {
    struct lfile_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
}


decompiled_code:
void free_file()
{
;
}

canonical_decompiled_code:
void func18()
{
;
}

original_code: void free_file(void *data) { }

canonical_original_code: void free_file(void *data) { }


timestamp

decompiled_code:
char *timestamp()
{
char *v0; // r12
struct tm *v1; // rax
char v2; // dl
char *v3; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x32uLL);
timer = time(0LL);
v1 = localtime(&timer);
asctime_r(v1, v0);
v2 = *v0;
if ( *v0 )
{
v3 = v0;
do
{
if ( v2 == 10 )
*v3 = 0;
v2 = *++v3;
}
while ( v2 );
}
return v0;
}

canonical_decompiled_code:
char *func19()
{
char *v0; // r12
struct tm *v1; // rax
char v2; // dl
char *v3; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x32uLL);
timer = time(0LL);
v1 = localtime(&timer);
asctime_r(v1, v0);
v2 = *v0;
if ( *v0 )
{
v3 = v0;
do
{
if ( v2 == 10 )
*v3 = 0;
v2 = *++v3;
}
while ( v2 );
}
return v0;
}

original_code:
char *timestamp() {
 char *ts = (char *)malloc(sizeof(char)*50);
 time_t ltime;
 ltime = time(((void *)0));
 asctime_r(localtime(&ltime), ts);
 char *tmp = ts;
 while (*tmp) {
        if (*tmp == '\n') {
            *tmp='\0';
        }
        tmp++;
    }
 return ts;
}

canonical_original_code:
char *timestamp() {
 char *ts = (char *)malloc(sizeof(char)*50);
 long ltime;
 ltime = time(((void *)0));
 asctime_r(localtime(&ltime), ts);
 char *tmp = ts;
 while (*tmp) {
        if (*tmp == '\n') {
            *tmp='\0';
        }
        tmp++;
    }
 return ts;
}

variable_types:
ts
char *

variable_types: ltime long

variable_types:
tmp
char *


decompiled_code:
__int64 __fastcall parse_command(const char *a1)
{
const char *v1; // rax
bool v2; // zf
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = &a1[strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
v2 = strcmp(a1, "LIST") == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( strcmp(a1, "DIFF") )
{
result = 2LL;
if ( strcmp(a1, "PULL") )
{
result = 4LL;
if ( strcmp(a1, "LEAVE") )
{
result = 3LL;
if ( strcmp(a1, "FETCH") )
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
const char *v1; // rax
bool v2; // zf
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = &a1[strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
v2 = strcmp(a1, "LIST") == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( strcmp(a1, "DIFF") )
{
result = 2LL;
if ( strcmp(a1, "PULL") )
{
result = 4LL;
if ( strcmp(a1, "LEAVE") )
{
result = 3LL;
if ( strcmp(a1, "FETCH") )
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
int parse_command(char *command) {
 if (command == ((void *)0)) {
  return -1;
 }
 size_t length = strlen(command) - 1;
 if (command[length] == '\n') command[length] = '\0';
 if (strcmp(command, "LIST") == 0) {
  return 0;
 } else if (strcmp(command, "DIFF") == 0) {
  return 1;
 } else if (strcmp(command, "PULL") == 0) {
  return 2;
 } else if (strcmp(command, "LEAVE") == 0) {
  return 4;
 } else if (strcmp(command, "FETCH") == 0) {
  return 3;
 } else {
  return -1;
 }
}

canonical_original_code:
int parse_command(char *command) {
 if (command == ((void *)0)) {
  return -1;
 }
 unsigned long length = strlen(command) - 1;
 if (command[length] == '\n') command[length] = '\0';
 if (strcmp(command, "LIST") == 0) {
  return 0;
 } else if (strcmp(command, "DIFF") == 0) {
  return 1;
 } else if (strcmp(command, "PULL") == 0) {
  return 2;
 } else if (strcmp(command, "LEAVE") == 0) {
  return 4;
 } else if (strcmp(command, "FETCH") == 0) {
  return 3;
 } else {
  return -1;
 }
}

variable_types:
command
char *

variable_types:
length
unsigned long


decompiled_code:
int display_help()
{
puts("Invalid Command\nValid commands include:\n");
puts("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer");
puts(
"\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the"
" .mp3 files on this machine");
puts(
"\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same"
" filename that are a different version");
return puts("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n");
}

canonical_decompiled_code:
int func2()
{
puts("Invalid Command\nValid commands include:\n");
puts("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer");
puts(
"\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the"
" .mp3 files on this machine");
puts(
"\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same"
" filename that are a different version");
return puts("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n");
}

original_code:
void display_help(){
 printf("Invalid Command\nValid commands include:\n\n");
 printf("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer\n");
 printf("\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the .mp3 files on this machine\n");
 printf("\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same filename that are a different version\n");
 printf("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n\n");
}


decompiled_code:
int __fastcall perform_list(__int64 a1)
{
int result; // eax
__int64 v2[2]; // [rsp+0h] [rbp-28h] BYREF
char v3[4]; // [rsp+10h] [rbp-18h] BYREF
unsigned int v4; // [rsp+14h] [rbp-14h]
__int64 v5; // [rsp+18h] [rbp-10h]

v2[0] = 0LL;
v2[1] = 0LL;
send_data(a1, v2, 8LL);
get_response_header((unsigned int)a1, v3, 8LL);
v5 = get_data((unsigned int)a1, v4);
empty_list(server_list, &free_file);
deserialize_list(server_list, v5);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
__int64 v2[2]; // [rsp+0h] [rbp-28h] BYREF
char v3[4]; // [rsp+10h] [rbp-18h] BYREF
unsigned int v4; // [rsp+14h] [rbp-14h]
__int64 v5; // [rsp+18h] [rbp-10h]

v2[0] = 0LL;
v2[1] = 0LL;
send_data(a1, v2, 8LL);
get_response_header((unsigned int)a1, v3, 8LL);
v5 = get_data((unsigned int)a1, v4);
empty_list(server_list, &free_file);
deserialize_list(server_list, v5);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

original_code:
void perform_list(int sock) {
 request req;
 response res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_list(int sock) {
 struct lrequest req;
 struct lresponse res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types: sock int

variable_types: struct lrequest { struct lrequest_header { unsigned int type; unsigned int size; } header; void *data; }

variable_types: struct lresponse { struct lresponse_header { unsigned int status; unsigned int size; } header; void *data; }

user_defined_types: struct lrequest { struct lrequest_header header; void *data; }

user_defined_types: struct lresponse { struct lresponse_header header; void *data; }


decompiled_code:
int __fastcall perform_diff(unsigned int a1)
{
int result; // eax
int v2; // [rsp+0h] [rbp-38h] BYREF
unsigned int v3; // [rsp+4h] [rbp-34h]
const char *v4; // [rsp+8h] [rbp-30h]
char v5[4]; // [rsp+10h] [rbp-28h] BYREF
unsigned int v6; // [rsp+14h] [rbp-24h]
__int64 v7; // [rsp+18h] [rbp-20h]

empty_list(local_list, &free_file);
read_directory(local_list);
v2 = 1;
v4 = (const char *)traverse_to_string(local_list, &file_to_string);
v3 = strlen(v4) + 1;
send_data(a1, &v2, 8LL);
send_data(a1, v4, v3);
get_response_header(a1, v5, 8LL);
v7 = get_data(a1, v6);
empty_list(server_list, &free_file);
deserialize_list(server_list, v7);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1)
{
int result; // eax
int v2; // [rsp+0h] [rbp-38h] BYREF
unsigned int v3; // [rsp+4h] [rbp-34h]
const char *v4; // [rsp+8h] [rbp-30h]
char v5[4]; // [rsp+10h] [rbp-28h] BYREF
unsigned int v6; // [rsp+14h] [rbp-24h]
__int64 v7; // [rsp+18h] [rbp-20h]

empty_list(local_list, &free_file);
read_directory(local_list);
v2 = 1;
v4 = (const char *)traverse_to_string(local_list, &file_to_string);
v3 = strlen(v4) + 1;
send_data(a1, &v2, 8LL);
send_data(a1, v4, v3);
get_response_header(a1, v5, 8LL);
v7 = get_data(a1, v6);
empty_list(server_list, &free_file);
deserialize_list(server_list, v7);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

original_code:
void perform_diff(int sock) {
 char *list;
 request req;
 response res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_diff(int sock) {
 char *list;
 struct lrequest req;
 struct lresponse res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types:
list
char *

user_defined_types: struct lrequest_header { unsigned int type; unsigned int size; }

user_defined_types: struct lresponse_header { unsigned int status; unsigned int size; }


decompiled_code:
void perform_fetch()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code: void perform_fetch(int sock) { }

canonical_original_code: void perform_fetch(int sock) { }


decompiled_code:
void __fastcall __noreturn perform_leave(__int64 a1)
{
__int64 v1[3]; // [rsp+0h] [rbp-18h] BYREF

v1[0] = 4LL;
v1[1] = 0LL;
send_data(a1, v1, 8LL);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(__int64 a1)
{
__int64 v1[3]; // [rsp+0h] [rbp-18h] BYREF

v1[0] = 4LL;
v1[1] = 0LL;
send_data(a1, v1, 8LL);
exit(0);
}

original_code:
void perform_leave(int sock) {
 request req;
 req.header.type = 4;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 exit(0);
}

canonical_original_code:
void perform_leave(int sock) {
 struct lrequest req;
 req.header.type = 4;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 exit(0);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er12
const char *v5; // rdi
in_addr_t v6; // eax
const char *v7; // rdi
__int16 v8; // ax
socklen_t v9; // edx
int v10; // eax
struct sockaddr v11; // [rsp+0h] [rbp-428h] BYREF
char v12[512]; // [rsp+10h] [rbp-418h] BYREF
char v13[512]; // [rsp+210h] [rbp-218h] BYREF

v3 = (char *)calloc(0x20uLL, 1uLL);
memset(v12, 0, sizeof(v12));
memset(v13, 0, sizeof(v13));
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", *argv);
exit(1);
}
local_list = create_list();
server_list = create_list();
v4 = socket(2, 1, 6);
if ( v4 < 0 )
die_with_error("socket() failed\n");
v5 = argv[1];
v11 = 0LL;
v11.sa_family = 2;
v6 = inet_addr(v5);
v7 = argv[2];
*(_DWORD *)&v11.sa_data[2] = v6;
v8 = atoi(v7);
*(_WORD *)v11.sa_data = _bswap_16(v8);
if ( connect(v4, &v11, v9) < 0 )
die_with_error("connect() failed");
puts("Welcome to GTmyMusic.");
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
fgets(v3, 32, stdin);
v10 = parse_command(v3);
if ( v10 != 2 )
break;
perform_pull(v4);
}
if ( v10 <= 2 )
break;
if ( v10 == 4 )
perform_leave((unsigned int)v4);
LABEL_16:
display_help();
}
if ( v10 )
{
if ( v10 != 1 )
goto LABEL_16;
perform_diff(v4);
}
else
{
perform_list((unsigned int)v4);
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
int v4; // er12
const char *v5; // rdi
in_addr_t v6; // eax
const char *v7; // rdi
__int16 v8; // ax
socklen_t v9; // edx
int v10; // eax
struct sockaddr v11; // [rsp+0h] [rbp-428h] BYREF
char v12[512]; // [rsp+10h] [rbp-418h] BYREF
char v13[512]; // [rsp+210h] [rbp-218h] BYREF

v3 = (char *)calloc(0x20uLL, 1uLL);
memset(v12, 0, sizeof(v12));
memset(v13, 0, sizeof(v13));
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", *argv);
exit(1);
}
local_list = create_list();
server_list = create_list();
v4 = socket(2, 1, 6);
if ( v4 < 0 )
die_with_error("socket() failed\n");
v5 = argv[1];
v11 = 0LL;
v11.sa_family = 2;
v6 = inet_addr(v5);
v7 = argv[2];
*(_DWORD *)&v11.sa_data[2] = v6;
v8 = atoi(v7);
*(_WORD *)v11.sa_data = func0(v8);
if ( connect(v4, &v11, v9) < 0 )
die_with_error("connect() failed");
puts("Welcome to GTmyMusic.");
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
fgets(v3, 32, stdin);
v10 = func1(v3);
if ( v10 != 2 )
break;
func5(v4);
}
if ( v10 <= 2 )
break;
if ( v10 == 4 )
func7((unsigned int)v4);
LABEL_16:
func2();
}
if ( v10 )
{
if ( v10 != 1 )
goto LABEL_16;
func4(v4);
}
else
{
func3((unsigned int)v4);
}
}
}

original_code:
int main(int argc, char *argv[]) {
 struct sockaddr_in serv_addr;
 int i;
 char *input = (char *) malloc(32);
 char sndBuf[512];
 char rcvBuf[512];
 memset(&sndBuf, 0, 512);
 memset(&rcvBuf, 0, 512);
 memset(input, 0, 32);
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", argv[0]);
        exit(1);
    }
    local_list = create_list();
    server_list = create_list();
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if(sock < 0)
  die_with_error("socket() failed\n");
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
 serv_addr.sin_port = htons(atoi(argv[2]));
 if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  die_with_error("connect() failed");
 }
 printf("Welcome to GTmyMusic.\n");
 while (1) {
  fgets(input, 32, stdin);
  switch(parse_command(input)) {
   case 0:
    perform_list(sock);
    break;
   case 1:
    perform_diff(sock);
    break;
   case 2:
    perform_pull(sock);
    break;
   case 4:
    perform_leave(sock);
    break;
   default:
    display_help();
    break;
  }
 }
 close(sock);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
input
char *

variable_types:
sndBuf
char [512]

variable_types:
rcvBuf
char [512]


decompiled_code:
_DWORD *create_list()
{
_DWORD *result; // rax

result = malloc(0x10uLL);
if ( !result )
{
printf("Malloc failed.");
exit(0);
}
result[2] = 0;
*(_QWORD *)result = 0LL;
return result;
}

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

result = malloc(0x10uLL);
if ( !result )
{
printf("Malloc failed.");
exit(0);
}
result[2] = 0;
*(_QWORD *)result = 0LL;
return result;
}

original_code:
list* create_list(void) {
 list *newList = malloc(sizeof(list));
 if (newList == ((void *)0)) {
  printf("Malloc failed.");
  exit(0);
 } else {
  newList->size = 0;
  newList->head = ((void *)0);
  return newList;
 }
}

canonical_original_code:
struct llist *create_list() {
 struct llist *newList = malloc(sizeof(list));
 if (newList == ((void *)0)) {
  printf("Malloc failed.");
  exit(0);
 } else {
  newList->size = 0;
  newList->head = ((void *)0);
  return newList;
 }
}

variable_types: struct llist { struct lnode { struct lnode *prev; struct lnode *next; void *data; } *head; unsigned int size; } *

user_defined_types: struct llist { struct lnode *head; unsigned int size; }


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

result = 0LL;
if ( !*(_QWORD *)a1 )
result = *(_DWORD *)(a1 + 8) == 0;
return result;
}

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

result = 0LL;
if ( !*(_QWORD *)a1 )
result = *(_DWORD *)(a1 + 8) == 0;
return result;
}

original_code:
int is_empty(list* llist) {
 if (llist->head == ((void *)0) && llist->size == 0) {
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
int is_empty(struct llist *llist) {
 if (llist->head == ((void *)0) && llist->size == 0) {
  return 1;
 } else {
  return 0;
 }
}

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


original_code:
int remove_front(list* llist, list_op free_func) {
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  free_func(llist->head->data);
  free(llist->head);
  llist->head = ((void *)0);
  llist->size--;
  return 0;
 } else {
  node *prev;
  node *next;
  prev = llist->head->prev;
  next = llist->head->next;
  prev->next = next;
  next->prev = prev;
  free_func(llist->head->data);
  free(llist->head);
  llist->head = next;
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_front(struct llist *llist, void (*free_func)(void *)) {
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  free_func(llist->head->data);
  free(llist->head);
  llist->head = ((void *)0);
  llist->size--;
  return 0;
 } else {
  struct lnode *prev;
  struct lnode *next;
  prev = llist->head->prev;
  next = llist->head->next;
  prev->next = next;
  next->prev = prev;
  free_func(llist->head->data);
  free(llist->head);
  llist->head = next;
  llist->size--;
  return 0;
 }
}

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


original_code:
int remove_back(list* llist, list_op free_func)
{
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  return remove_front(llist, free_func);
 } else {
  node *removed = llist->head->prev;
  node *prev;
  node *next;
  prev = removed->prev;
  next = removed->next;
  prev->next = next;
  next->prev = prev;
  free_func(removed->data);
  free(removed);
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_back(struct llist *llist, void (*free_func)(void *))
{
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  return remove_front(llist, free_func);
 } else {
  struct lnode *removed = llist->head->prev;
  struct lnode *prev;
  struct lnode *next;
  prev = removed->prev;
  next = removed->next;
  prev->next = next;
  next->prev = prev;
  free_func(removed->data);
  free(removed);
  llist->size--;
  return 0;
 }
}


decompiled_code:
__int64 __fastcall remove_index(__int64 a1, int a2)
{
void (__fastcall *v2)(__int64); // r8
unsigned int v3; // er13
unsigned int v5; // eax
__int64 *v6; // rbp
int i; // eax
__int64 v8; // rdx
_QWORD *v9; // rax
__int64 v10; // rdi

if ( is_empty(a1) )
return (unsigned int)-1;
v3 = 0;
v5 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v5 <= a2 )
return (unsigned int)-1;
if ( !a2 )
return remove_front(a1, v2);
if ( a2 != v5 - 1 )
{
v6 = *(__int64 **)a1;
for ( i = 0; i < a2; ++i )
v6 = (__int64 *)v6[1];
v8 = *v6;
v9 = (_QWORD *)v6[1];
v10 = v6[2];
*(_QWORD *)(v8 + 8) = v9;
*v9 = v8;
v2(v10);
free(v6);
--*(_DWORD *)(a1 + 8);
return v3;
}
return remove_back(a1, v2);
}

original_code:
int remove_index(list* llist, int index, list_op free_func)
{
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return -1;
 } else if (index == 0) {
  return remove_front(llist, free_func);
 } else if (index == llist->size - 1) {
  return remove_back(llist, free_func);
 } else {
  int i = 0;
  node *current = llist->head;
  while (i < index) {
   current = current->next;
   i++;
  }
  node *prev;
  node *next;
  prev = current->prev;
  next = current->next;
  prev->next = next;
  next->prev = prev;
  free_func(current->data);
  free(current);
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_index(struct llist *llist, int index, void (*free_func)(void *))
{
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return -1;
 } else if (index == 0) {
  return remove_front(llist, free_func);
 } else if (index == llist->size - 1) {
  return remove_back(llist, free_func);
 } else {
  int i = 0;
  struct lnode *current = llist->head;
  while (i < index) {
   current = current->next;
   i++;
  }
  struct lnode *prev;
  struct lnode *next;
  prev = current->prev;
  next = current->next;
  prev->next = next;
  next->prev = prev;
  free_func(current->data);
  free(current);
  llist->size--;
  return 0;
 }
}

variable_types: index int


original_code:
int remove_data(list* llist, const void* data, equal_op compare_func, list_op free_func)
{
 if (!is_empty(llist)) {
  node *upNext = llist->head;
  node *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (compare_func(current->data, data) && i > 0) {
    node *prev;
    node *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (compare_func(current->data, data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

canonical_original_code:
int remove_data(struct llist *llist, void *data, int (*compare_func)(void *, void *), void (*free_func)(void *))
{
 if (!is_empty(llist)) {
  struct lnode *upNext = llist->head;
  struct lnode *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (compare_func(current->data, data) && i > 0) {
    struct lnode *prev;
    struct lnode *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (compare_func(current->data, data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

variable_types:
data
void *

variable_types: count int


original_code:
int remove_if(list* llist, list_pred pred_func, list_op free_func) {
 if (!is_empty(llist)) {
  node *upNext = llist->head;
  node *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (pred_func(current->data) && i > 0) {
    node *prev;
    node *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (pred_func(current->data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

canonical_original_code:
int remove_if(struct llist *llist, int (*pred_func)(void *), void (*free_func)(void *)) {
 if (!is_empty(llist)) {
  struct lnode *upNext = llist->head;
  struct lnode *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (pred_func(current->data) && i > 0) {
    struct lnode *prev;
    struct lnode *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (pred_func(current->data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}


front

decompiled_code:
__int64 __fastcall front(__int64 a1)
{
_BOOL4 v1; // er8
__int64 result; // rax

v1 = is_empty(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
_BOOL4 v1; // er8
__int64 result; // rax

v1 = func1(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
return result;
}

original_code:
void* front(list* llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->data;
 }
}

canonical_original_code:
void *front(struct llist *llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->data;
 }
}

return_type: void *


back

decompiled_code:
__int64 __fastcall back(__int64 a1)
{
_BOOL4 v1; // er8
__int64 result; // rax

v1 = is_empty(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(**(_QWORD **)a1 + 16LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
_BOOL4 v1; // er8
__int64 result; // rax

v1 = func1(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(**(_QWORD **)a1 + 16LL);
return result;
}

original_code:
void* back(list* llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->prev->data;
 }
}

canonical_original_code:
void *back(struct llist *llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->prev->data;
 }
}


decompiled_code:
__int64 __fastcall get_index(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // edx
__int64 v4; // rcx
__int64 v5; // rdx
__int64 v6; // rdi

v2 = is_empty(a1);
if ( v2 )
return 0LL;
v3 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v3 <= a2 )
return 0LL;
if ( !a2 )
return front(a1);
if ( a2 == v3 - 1 )
return back(a1);
v4 = *(_QWORD *)a1;
v5 = *(_QWORD *)a1;
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
if ( v5 == v4 )
return *(_QWORD *)(v6 + 16);
++v2;
}
while ( a2 > v2 );
return *(_QWORD *)(v5 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // edx
__int64 v4; // rcx
__int64 v5; // rdx
__int64 v6; // rdi

v2 = func1(a1);
if ( v2 )
return 0LL;
v3 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v3 <= a2 )
return 0LL;
if ( !a2 )
return func7(a1);
if ( a2 == v3 - 1 )
return func8(a1);
v4 = *(_QWORD *)a1;
v5 = *(_QWORD *)a1;
do
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
if ( v5 == v4 )
return *(_QWORD *)(v6 + 16);
++v2;
}
while ( a2 > v2 );
return *(_QWORD *)(v5 + 16);
}

original_code:
void* get_index(list* llist, int index) {
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return ((void *)0);
 } else if (index == 0) {
  return front(llist);
 } else if (index == llist->size - 1) {
  return back(llist);
 } else {
  int i = 0;
  node *current = llist->head;
  while (i < index && current->next != llist->head) {
   current = current->next;
   i++;
  }
  return current->data;
 }
}

canonical_original_code:
void *get_index(struct llist *llist, int index) {
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return ((void *)0);
 } else if (index == 0) {
  return front(llist);
 } else if (index == llist->size - 1) {
  return back(llist);
 } else {
  int i = 0;
  struct lnode *current = llist->head;
  while (i < index && current->next != llist->head) {
   current = current->next;
   i++;
  }
  return current->data;
 }
}


size

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

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

original_code:
int size(list* llist) {
    return llist->size;
}

canonical_original_code:
int size(struct llist *llist) {
    return llist->size;
}


decompiled_code:
__int64 __fastcall find_occurrence(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(_QWORD, __int64))
{
int v4; // er14
__int64 v5; // rbp
int v6; // ebx

if ( is_empty(a1) )
return 0LL;
v4 = *(_DWORD *)(a1 + 8);
v5 = *(_QWORD *)a1;
v6 = 0;
if ( v4 <= 0 )
return 0LL;
while ( !a3(*(_QWORD *)(v5 + 16), a2) )
{
++v6;
v5 = *(_QWORD *)(v5 + 8);
if ( v4 == v6 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(_QWORD, __int64))
{
int v4; // er14
__int64 v5; // rbp
int v6; // ebx

if ( func1(a1) )
return 0LL;
v4 = *(_DWORD *)(a1 + 8);
v5 = *(_QWORD *)a1;
v6 = 0;
if ( v4 <= 0 )
return 0LL;
while ( !a3(*(_QWORD *)(v5 + 16), a2) )
{
++v6;
v5 = *(_QWORD *)(v5 + 8);
if ( v4 == v6 )
return 0LL;
}
return 1LL;
}

original_code:
int find_occurrence(list* llist, const void* search, equal_op compare_func) {
 if (is_empty(llist)) {
  return 0;
 } else {
  node *current = llist->head;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   if (compare_func(current->data, search)) {
    return 1;
   }
   current = current->next;
   i++;
  }
  return 0;
 }
}

canonical_original_code:
int find_occurrence(struct llist *llist, void *search, int (*compare_func)(void *, void *)) {
 if (is_empty(llist)) {
  return 0;
 } else {
  struct lnode *current = llist->head;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   if (compare_func(current->data, search)) {
    return 1;
   }
   current = current->next;
   i++;
  }
  return 0;
 }
}

variable_types:
search
void *

variable_types: size int


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

while ( 1 )
{
result = is_empty(a1);
if ( result )
break;
remove_front(a1, a2);
}
return result;
}

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

while ( 1 )
{
result = func1(a1);
if ( result )
break;
func2(a1, a2);
}
return result;
}

original_code:
void empty_list(list* llist, list_op free_func) {
 while (!is_empty(llist)) {
  remove_front(llist, free_func);
 }
}

canonical_original_code:
void empty_list(struct llist *llist, void (*free_func)(void *)) {
 while (!is_empty(llist)) {
  remove_front(llist, free_func);
 }
}


traverse

decompiled_code:
__int64 __fastcall traverse(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
int v3; // er13
__int64 v4; // rbp
int v5; // ebx
__int64 v6; // rax

result = is_empty(a1);
if ( !(_DWORD)result )
{
v3 = *(_DWORD *)(a1 + 8);
v4 = *(_QWORD *)a1;
v5 = 0;
if ( v3 > 0 )
{
do
{
v6 = v4;
++v5;
v4 = *(_QWORD *)(v4 + 8);
result = a2(*(_QWORD *)(v6 + 16));
}
while ( v3 != v5 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
int v3; // er13
__int64 v4; // rbp
int v5; // ebx
__int64 v6; // rax

result = func1(a1);
if ( !(_DWORD)result )
{
v3 = *(_DWORD *)(a1 + 8);
v4 = *(_QWORD *)a1;
v5 = 0;
if ( v3 > 0 )
{
do
{
v6 = v4;
++v5;
v4 = *(_QWORD *)(v4 + 8);
result = a2(*(_QWORD *)(v6 + 16));
}
while ( v3 != v5 );
}
}
return result;
}

original_code:
void traverse(list* llist, list_op do_func) {
 if (!is_empty(llist)) {
  node *next = llist->head;
  node *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   do_func(current->data);
   i++;
  }
 }
}

canonical_original_code:
void traverse(struct llist *llist, void (*do_func)(void *)) {
 if (!is_empty(llist)) {
  struct lnode *next = llist->head;
  struct lnode *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   do_func(current->data);
   i++;
  }
 }
}


decompiled_code:
__int64 __fastcall traverse_diff(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned int (__fastcall *v4)(_QWORD, __int64); // rcx
int v5; // er15
__int64 v6; // rbp
unsigned int (__fastcall *v7)(_QWORD, __int64); // r13
int v8; // ebx
__int64 v9; // r14

result = is_empty(a1);
if ( !(_DWORD)result )
{
v5 = *(_DWORD *)(a1 + 8);
v6 = *(_QWORD *)a1;
v7 = v4;
v8 = 0;
if ( v5 > 0 )
{
do
{
v9 = v6;
v6 = *(_QWORD *)(v6 + 8);
result = find_occurrence(a2, *(_QWORD *)(v9 + 16), v7);
if ( !(_DWORD)result )
result = (__int64)push_back(a3, *(_QWORD *)(v9 + 16));
++v8;
}
while ( v5 != v8 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned int (__fastcall *v4)(_QWORD, __int64); // rcx
int v5; // er15
__int64 v6; // rbp
unsigned int (__fastcall *v7)(_QWORD, __int64); // r13
int v8; // ebx
__int64 v9; // r14

result = func1(a1);
if ( !(_DWORD)result )
{
v5 = *(_DWORD *)(a1 + 8);
v6 = *(_QWORD *)a1;
v7 = v4;
v8 = 0;
if ( v5 > 0 )
{
do
{
v9 = v6;
v6 = *(_QWORD *)(v6 + 8);
result = func11(a2, *(_QWORD *)(v9 + 16), v7);
if ( !(_DWORD)result )
result = (__int64)push_back(a3, *(_QWORD *)(v9 + 16));
++v8;
}
while ( v5 != v8 );
}
}
return result;
}

original_code:
void traverse_diff(list *llist, list *client_list, list *output, equal_op compare_func) {
 if (!is_empty(llist)) {
  node *next = llist->head;
  node *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   if (!find_occurrence(client_list, current->data, compare_func)) {
                push_back(output, current->data);
            }
   i++;
  }
 }
}

canonical_original_code:
void traverse_diff(struct llist *llist, struct llist *client_list, struct llist *output, int (*compare_func)(void *, void *)) {
 if (!is_empty(llist)) {
  struct lnode *next = llist->head;
  struct lnode *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   if (!find_occurrence(client_list, current->data, compare_func)) {
                push_back(output, current->data);
            }
   i++;
  }
 }
}

variable_types:
output
struct llist *


decompiled_code:
__int64 __fastcall perform_list(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
const char *v6; // [rsp+8h] [rbp-20h]

empty_list(a3, &free_file);
read_directory(a3);
v6 = (const char *)traverse_to_string(a3, &file_to_string);
HIDWORD(v5) = strlen(v6) + 1;
send_data(a1, &v5, 8LL);
return send_data(a1, v6, HIDWORD(v5));
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
const char *v6; // [rsp+8h] [rbp-20h]

empty_list(a3, &free_file);
read_directory(a3);
v6 = (const char *)traverse_to_string(a3, &file_to_string);
HIDWORD(v5) = strlen(v6) + 1;
send_data(a1, &v5, 8LL);
return send_data(a1, v6, HIDWORD(v5));
}

original_code:
void perform_list(int sock) {
 request req;
 response res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_list(int sock) {
 struct lrequest req;
 struct lresponse res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types: sock int

variable_types: struct lrequest { struct lrequest_header { unsigned int type; unsigned int size; } header; void *data; }

variable_types: struct lresponse { struct lresponse_header { unsigned int status; unsigned int size; } header; void *data; }


decompiled_code:
__int64 __fastcall perform_diff(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // r15
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
const char *v11; // [rsp+8h] [rbp-30h]

v8 = get_data(a1, *(unsigned int *)(a2 + 4));
empty_list(a4, &free_file);
deserialize_list(a4, v8);
empty_list(a3, &free_file);
read_directory(a3);
empty_list(a5, &free_file);
traverse_diff(a3, a4, a5, &file_comparator);
v11 = (const char *)traverse_to_string(a5, &file_to_string);
HIDWORD(v10) = strlen(v11) + 1;
send_data((unsigned int)a1, &v10, 8LL);
return send_data((unsigned int)a1, v11, HIDWORD(v10));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // r15
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
const char *v11; // [rsp+8h] [rbp-30h]

v8 = get_data(a1, *(unsigned int *)(a2 + 4));
empty_list(a4, &free_file);
deserialize_list(a4, v8);
empty_list(a3, &free_file);
read_directory(a3);
empty_list(a5, &free_file);
traverse_diff(a3, a4, a5, &file_comparator);
v11 = (const char *)traverse_to_string(a5, &file_to_string);
HIDWORD(v10) = strlen(v11) + 1;
send_data((unsigned int)a1, &v10, 8LL);
return send_data((unsigned int)a1, v11, HIDWORD(v10));
}

original_code:
void perform_diff(int sock) {
 char *list;
 request req;
 response res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_diff(int sock) {
 char *list;
 struct lrequest req;
 struct lresponse res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types:
list
char *