startServer

decompiled_code:
void __fastcall __noreturn startServer(const char *a1, const char *a2, const char *a3)
{
int v5; // eax
int v6; // esi
int v7; // ebx
__int64 v8; // r14
int v9; // er15
int v10; // eax
__int64 v11; // rax
char buf; // [rsp+Fh] [rbp-169h] BYREF
struct timeval timeout; // [rsp+10h] [rbp-168h] BYREF
fd_set readfds; // [rsp+20h] [rbp-158h] BYREF
int optval; // [rsp+ACh] [rbp-CCh] BYREF
struct stat v16; // [rsp+B0h] [rbp-C8h] BYREF

portnumber = atoi(a1);
period = atoi(a3);
system("rm -f log.txt");
system("touch log.txt");
slog("Starting directory monitor server\n");
if ( period - 1 > 0xFE )
die("Please specify a nonzero period, maximum 255\n");
if ( !(unsigned int)checkPort((unsigned int)portnumber) )
die("Please specify a port number between 1024 and 65535\n");
if ( strlen(a2) > 0xFF )
die("Directory name is too long. Must be under 255 characters.\n");
if ( stat(a2, &v16) )
die("Directory is gone or not accessible.\n");
diff[4] = a2;
printf(
"Starting server monitor on port %d for directory %s every %d second(s)\n",
(unsigned int)portnumber,
a2,
period);
openDirmonitor();
daemonize();
clearHosts(hosts, 3LL);
sockInit = socket(2, 1, 0);
if ( sockInit < 0 )
slog("ERROR opening socket\n");
bzero(&serv_addr, 0x10uLL);
serv_addr.sa_family = 2;
*(_DWORD *)&serv_addr.sa_data[2] = 0;
*(_WORD *)serv_addr.sa_data = _bswap_16(portnumber);
optval = 1;
setsockopt(sockInit, 1, 2, &optval, 4u);
if ( bind(sockInit, &serv_addr, 0x10u) < 0 )
slog("ERROR binding socket\n");
listen(sockInit, 5);
clilen = 16;
timeout.tv_sec = 0LL;
timeout.tv_usec = 100LL;
memset(&inputs, 0, 0x80uLL);
memset(&readfds, 0, sizeof(readfds));
v5 = sockInit;
*((_QWORD *)&inputs + sockInit / 64) |= 1LL << (sockInit % 64);
v6 = fds[0];
*((_QWORD *)&inputs + fds[0] / 64) |= 1LL << (fds[0] % 64);
if ( v5 < v6 )
v5 = v6;
fdmax = v5;
error = 0;
do
{
LABEL_16:
while ( 1 )
{
*(__m128i *)readfds.fds_bits = _mm_load_si128((const __m128i *)&inputs);
*(__m128i *)&readfds.fds_bits[2] = _mm_load_si128((const __m128i *)&inputs + 1);
*(__m128i *)&readfds.fds_bits[4] = _mm_load_si128((const __m128i *)&inputs + 2);
*(__m128i *)&readfds.fds_bits[6] = _mm_load_si128((const __m128i *)&inputs + 3);
*(__m128i *)&readfds.fds_bits[8] = _mm_load_si128((const __m128i *)&inputs + 4);
*(__m128i *)&readfds.fds_bits[10] = _mm_load_si128((const __m128i *)&inputs + 5);
*(__m128i *)&readfds.fds_bits[12] = _mm_load_si128((const __m128i *)&inputs + 6);
*(__m128i *)&readfds.fds_bits[14] = _mm_load_si128((const __m128i *)&inputs + 7);
if ( select(fdmax + 1, &readfds, 0LL, 0LL, &timeout) != -1 )
break;
slog("Select failed on server\n");
}
if ( error == 1 )
{
clientAllDisconnect("SIGHUP received by server, please reconnect");
error = 0;
}
timeout.tv_usec = 100LL;
}
while ( fdmax < 0 );
v7 = 0;
while ( 1 )
{
v8 = 1LL << (v7 % 64);
if ( (readfds.fds_bits[v7 / 64] & v8) != 0 )
{
if ( sockInit == v7 )
{
slog("Connecting to new client\n");
clientConnect(v7, a2);
}
else
{
if ( fds[0] != v7 )
{
v9 = selectHostFD(hosts, (unsigned int)v7, 3LL);
if ( recv(v7, &buf, 1uLL, 0) <= 0 )
{
slog("Client disconnected unexpectedly\n");
hosts[16328 * v9] = 0;
*((_QWORD *)&inputs + sockInit / 64) |= 1LL << (sockInit % 64);
close(v7);
*((_QWORD *)&inputs + v7 / 64) &= ~v8;
goto LABEL_24;
}
slog("Client disconnected safely\n");
if ( buf == -34 )
{
if ( hosts[16328 * v9] == 2 )
{
hosts[16328 * v9] = 3;
goto LABEL_24;
}
}
else if ( buf == -83 )
{
v11 = 16328LL * v9;
if ( hosts[v11] == 3 )
{
clientDisconnect(&hosts[v11], "Goodbye");
if ( hosts[16328 * v9] )
goto LABEL_24;
goto LABEL_32;
}
}
hosts[16328 * v9] = 0;
LABEL_32:
close(hosts[16328 * v9 + 16327]);
v10 = hosts[16328 * v9 + 16327];
*((_QWORD *)&inputs + v10 / 64) &= ~(1LL << (v10 % 64));
goto LABEL_24;
}
slog("Updating client\n");
writeUpdates();
}
}
LABEL_24:
if ( fdmax < ++v7 )
goto LABEL_16;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func12(const char *a1, const char *a2, const char *a3)
{
int v5; // eax
int v6; // esi
int v7; // ebx
__int64 v8; // r14
int v9; // er15
int v10; // eax
__int64 v11; // rax
char buf; // [rsp+Fh] [rbp-169h] BYREF
struct timeval timeout; // [rsp+10h] [rbp-168h] BYREF
fd_set readfds; // [rsp+20h] [rbp-158h] BYREF
int optval; // [rsp+ACh] [rbp-CCh] BYREF
struct stat v16; // [rsp+B0h] [rbp-C8h] BYREF

portnumber = atoi(a1);
period = atoi(a3);
system("rm -f log.txt");
system("touch log.txt");
slog("Starting directory monitor server\n");
if ( period - 1 > 0xFE )
die("Please specify a nonzero period, maximum 255\n");
if ( !(unsigned int)checkPort((unsigned int)portnumber) )
die("Please specify a port number between 1024 and 65535\n");
if ( strlen(a2) > 0xFF )
die("Directory name is too long. Must be under 255 characters.\n");
if ( stat(a2, &v16) )
die("Directory is gone or not accessible.\n");
diff[4] = a2;
printf(
"Starting server monitor on port %d for directory %s every %d second(s)\n",
(unsigned int)portnumber,
a2,
period);
func6();
func4();
clearHosts(hosts, 3LL);
sockInit = socket(2, 1, 0);
if ( sockInit < 0 )
slog("ERROR opening socket\n");
bzero(&serv_addr, 0x10uLL);
serv_addr.sa_family = 2;
*(_DWORD *)&serv_addr.sa_data[2] = 0;
*(_WORD *)serv_addr.sa_data = func0(portnumber);
optval = 1;
setsockopt(sockInit, 1, 2, &optval, 4u);
if ( bind(sockInit, &serv_addr, 0x10u) < 0 )
slog("ERROR binding socket\n");
listen(sockInit, 5);
clilen = 16;
timeout.tv_sec = 0LL;
timeout.tv_usec = 100LL;
memset(&inputs, 0, 0x80uLL);
memset(&readfds, 0, sizeof(readfds));
v5 = sockInit;
*((_QWORD *)&inputs + sockInit / 64) |= 1LL << (sockInit % 64);
v6 = fds[0];
*((_QWORD *)&inputs + fds[0] / 64) |= 1LL << (fds[0] % 64);
if ( v5 < v6 )
v5 = v6;
fdmax = v5;
error = 0;
do
{
LABEL_16:
while ( 1 )
{
*(__m128i *)readfds.fds_bits = _mm_load_si128((const __m128i *)&inputs);
*(__m128i *)&readfds.fds_bits[2] = _mm_load_si128((const __m128i *)&inputs + 1);
*(__m128i *)&readfds.fds_bits[4] = _mm_load_si128((const __m128i *)&inputs + 2);
*(__m128i *)&readfds.fds_bits[6] = _mm_load_si128((const __m128i *)&inputs + 3);
*(__m128i *)&readfds.fds_bits[8] = _mm_load_si128((const __m128i *)&inputs + 4);
*(__m128i *)&readfds.fds_bits[10] = _mm_load_si128((const __m128i *)&inputs + 5);
*(__m128i *)&readfds.fds_bits[12] = _mm_load_si128((const __m128i *)&inputs + 6);
*(__m128i *)&readfds.fds_bits[14] = _mm_load_si128((const __m128i *)&inputs + 7);
if ( select(fdmax + 1, &readfds, 0LL, 0LL, &timeout) != -1 )
break;
slog("Select failed on server\n");
}
if ( error == 1 )
{
func9("SIGHUP received by server, please reconnect");
error = 0;
}
timeout.tv_usec = 100LL;
}
while ( fdmax < 0 );
v7 = 0;
while ( 1 )
{
v8 = 1LL << (v7 % 64);
if ( (readfds.fds_bits[v7 / 64] & v8) != 0 )
{
if ( sockInit == v7 )
{
slog("Connecting to new client\n");
func7(v7, a2);
}
else
{
if ( fds[0] != v7 )
{
v9 = selectHostFD(hosts, (unsigned int)v7, 3LL);
if ( recv(v7, &buf, 1uLL, 0) <= 0 )
{
slog("Client disconnected unexpectedly\n");
hosts[16328 * v9] = 0;
*((_QWORD *)&inputs + sockInit / 64) |= 1LL << (sockInit % 64);
close(v7);
*((_QWORD *)&inputs + v7 / 64) &= ~v8;
goto LABEL_24;
}
slog("Client disconnected safely\n");
if ( buf == -34 )
{
if ( hosts[16328 * v9] == 2 )
{
hosts[16328 * v9] = 3;
goto LABEL_24;
}
}
else if ( buf == -83 )
{
v11 = 16328LL * v9;
if ( hosts[v11] == 3 )
{
func8(&hosts[v11], "Goodbye");
if ( hosts[16328 * v9] )
goto LABEL_24;
goto LABEL_32;
}
}
hosts[16328 * v9] = 0;
LABEL_32:
close(hosts[16328 * v9 + 16327]);
v10 = hosts[16328 * v9 + 16327];
*((_QWORD *)&inputs + v10 / 64) &= ~(1LL << (v10 % 64));
goto LABEL_24;
}
slog("Updating client\n");
func11();
}
}
LABEL_24:
if ( fdmax < ++v7 )
goto LABEL_16;
}
}

original_code:
void startServer(char* portnumberCh, char* dirname, char* periodCh){
    portnumber = atoi(portnumberCh);
    period = atoi(periodCh);
    system("rm -f log.txt");
    system("touch log.txt");
    slog("Starting directory monitor server\n");
    if (period <= 0 || period > 255){
        die("Please specify a nonzero period, maximum 255\n");
    }
    if (checkPort(portnumber) == 0){
        die("Please specify a port number between 1024 and 65535\n");
    }
 if (strlen(dirname) > 255){
  die("Directory name is too long. Must be under 255 characters.\n");
 }
 STAT testdir;
 if (stat(dirname,&testdir) != 0){
  die("Directory is gone or not accessible.\n");
 }
    diff.name=dirname;
 printf("Starting server monitor on port %d for directory %s every %d second(s)\n",portnumber,dirname,period);
    openDirmonitor();
    daemonize();
    clearHosts(hosts,3);
    sockInit = socket(2, SOCK_STREAM, 0);
    if (sockInit < 0)
        slog("ERROR opening socket\n");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    serv_addr.sin_port = htons(portnumber);
    int yes = 1;
    setsockopt(sockInit, 1, 2, &yes, sizeof(yes));
    if (bind(sockInit, (struct sockaddr *) &serv_addr,
                sizeof(serv_addr)) < 0)
        slog("ERROR binding socket\n");
    listen(sockInit,5);
    clilen = sizeof(cli_addr);
    fd_set read_fds;
 struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 100;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inputs)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&inputs)->__fds_bits)[((sockInit) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockInit) % (8 * (int) sizeof (__fd_mask)))))));
    fdmax = sockInit;
    ((void) (((&inputs)->__fds_bits)[((fds[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds[0]) % (8 * (int) sizeof (__fd_mask)))))));
    if (fds[0] > fdmax) fdmax = fds[0];
    error = 0;
    while (1){
        read_fds = inputs;
        if(select(fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv) == -1){
            slog("Select failed on server\n");
   continue;
  }
        if (error == 1){
            clientAllDisconnect("SIGHUP received by server, please reconnect");
            error = 0;
        }
        tv.tv_usec = 100;
        for(int i = 0; i <= fdmax; i++){
   if(((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
    if (i == sockInit){
                    slog("Connecting to new client\n");
                    clientConnect(i,dirname);
                } else if (i == fds[0]){
                    slog("Updating client\n");
                    writeUpdates();
                } else {
                    int n = selectHostFD(hosts,i,3);
                    host* h = &hosts[n];
                    unsigned char r;
                    if((recv(i, &r, 1, 0)) <= 0){
                        slog("Client disconnected unexpectedly\n");
                        hosts[n].status = 0;
                        ((void) (((&inputs)->__fds_bits)[((sockInit) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockInit) % (8 * (int) sizeof (__fd_mask)))))));
                        close(i);
                        ((void) (((&inputs)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))));
                    }
                    else{
                        slog("Client disconnected safely\n");
                        if (r == 0xDE && h->status == 2){
                            h->status = 3;
                        } else if (r == 0xAD && h->status == 3){
                            clientDisconnect(h,"Goodbye");
                        } else {
                            h->status = 0;
                        }
                        if (h->status == 0){
                            close(h->sockfd);
                            ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
                        }
                    }
                }
            }
        }
    }
}

canonical_original_code:
void startServer(char *portnumberCh, char *dirname, char *periodCh){
    portnumber = atoi(portnumberCh);
    period = atoi(periodCh);
    system("rm -f log.txt");
    system("touch log.txt");
    slog("Starting directory monitor server\n");
    if (period <= 0 || period > 255){
        die("Please specify a nonzero period, maximum 255\n");
    }
    if (checkPort(portnumber) == 0){
        die("Please specify a port number between 1024 and 65535\n");
    }
 if (strlen(dirname) > 255){
  die("Directory name is too long. Must be under 255 characters.\n");
 }
 struct stat testdir;
 if (stat(dirname,&testdir) != 0){
  die("Directory is gone or not accessible.\n");
 }
    diff.name=dirname;
 printf("Starting server monitor on port %d for directory %s every %d second(s)\n",portnumber,dirname,period);
    openDirmonitor();
    daemonize();
    clearHosts(hosts,3);
    sockInit = socket(2, SOCK_STREAM, 0);
    if (sockInit < 0)
        slog("ERROR opening socket\n");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    serv_addr.sin_port = htons(portnumber);
    int yes = 1;
    setsockopt(sockInit, 1, 2, &yes, sizeof(yes));
    if (bind(sockInit, (struct sockaddr *) &serv_addr,
                sizeof(serv_addr)) < 0)
        slog("ERROR binding socket\n");
    listen(sockInit,5);
    clilen = sizeof(cli_addr);
    struct fd_set read_fds;
 struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 100;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inputs)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&inputs)->__fds_bits)[((sockInit) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockInit) % (8 * (int) sizeof (__fd_mask)))))));
    fdmax = sockInit;
    ((void) (((&inputs)->__fds_bits)[((fds[0]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fds[0]) % (8 * (int) sizeof (__fd_mask)))))));
    if (fds[0] > fdmax) fdmax = fds[0];
    error = 0;
    while (1){
        read_fds = inputs;
        if(select(fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv) == -1){
            slog("Select failed on server\n");
   continue;
  }
        if (error == 1){
            clientAllDisconnect("SIGHUP received by server, please reconnect");
            error = 0;
        }
        tv.tv_usec = 100;
        for(int i = 0; i <= fdmax; i++){
   if(((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
    if (i == sockInit){
                    slog("Connecting to new client\n");
                    clientConnect(i,dirname);
                } else if (i == fds[0]){
                    slog("Updating client\n");
                    writeUpdates();
                } else {
                    int n = selectHostFD(hosts,i,3);
                    struct host *h = &hosts[n];
                    unsigned char r;
                    if((recv(i, &r, 1, 0)) <= 0){
                        slog("Client disconnected unexpectedly\n");
                        hosts[n].status = 0;
                        ((void) (((&inputs)->__fds_bits)[((sockInit) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sockInit) % (8 * (int) sizeof (__fd_mask)))))));
                        close(i);
                        ((void) (((&inputs)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))));
                    }
                    else{
                        slog("Client disconnected safely\n");
                        if (r == 0xDE && h->status == 2){
                            h->status = 3;
                        } else if (r == 0xAD && h->status == 3){
                            clientDisconnect(h,"Goodbye");
                        } else {
                            h->status = 0;
                        }
                        if (h->status == 0){
                            close(h->sockfd);
                            ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
                        }
                    }
                }
            }
        }
    }
}

variable_types:
portnumberCh
char *

variable_types:
dirname
char *

variable_types:
periodCh
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; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }

variable_types: yes int

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

variable_types: struct host { int status; int pstep; int expected; char lines[254][255]; int cur_line; int lines_i; char hostname[255]; int portnum; char dirname[255]; char period; int sockfd; } *

variable_types:
r
unsigned char

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


terminateAll

repo: krisrm/socketdirmonitor

decompiled_code:
void terminateAll()
{
if ( *(_DWORD *)hosts )
terminateClientConnection(hosts);
if ( *(_DWORD *)&hosts[65312] )
terminateClientConnection((unsigned int)((_DWORD)hosts + 65312));
}

canonical_decompiled_code:
void func0()
{
if ( *(_DWORD *)hosts )
terminateClientConnection(hosts);
if ( *(_DWORD *)&hosts[65312] )
terminateClientConnection((unsigned int)((_DWORD)hosts + 65312));
}

original_code:
void terminateAll(){
 for (int n = 0; n < 2; n++){
  if (hosts[n].status != 0){
   terminateClientConnection(&hosts[n]);
  }
 }
}

variable_types: n int

return_type: void


protocolError

decompiled_code:
__int64 __fastcall protocolError(_DWORD *a1)
{
int v1; // ecx
__int64 result; // rax

fwrite("Protocol error: 0xFE and 0xED not sent. Aborting.\n", 1uLL, 0x32uLL, stderr);
*a1 = 0;
close(a1[16327]);
v1 = a1[16327];
result = v1 / 64;
*((_QWORD *)&inputs + result) &= __ROL8__(-2LL, v1 % 64);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
int v1; // ecx
__int64 result; // rax

fwrite("Protocol error: 0xFE and 0xED not sent. Aborting.\n", 1uLL, 0x32uLL, stderr);
*a1 = 0;
close(a1[16327]);
v1 = a1[16327];
result = v1 / 64;
*((_QWORD *)&inputs + result) &= __ROL8__(-2LL, v1 % 64);
return result;
}

original_code:
void protocolError(host* h){
 fprintf(stderr,"Protocol error: 0xFE and 0xED not sent. Aborting.\n");
 h->status = 0;
 close(h->sockfd);
 ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
}

canonical_original_code:
void protocolError(struct host *h){
 fprintf(stderr,"Protocol error: 0xFE and 0xED not sent. Aborting.\n");
 h->status = 0;
 close(h->sockfd);
 ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
}

user_defined_types: struct host { int status; int pstep; int expected; char lines[254][255]; int cur_line; int lines_i; char hostname[255]; int portnum; char dirname[255]; char period; int sockfd; }


cleanup

decompiled_code:
void cleanup()
{
terminateAll();
}

canonical_decompiled_code:
void func3()
{
func0();
}

original_code:
void cleanup(){
 terminateAll();
}

canonical_original_code:
void cleanup(){
 terminateAll();
}


decompiled_code:
int my_rl_printf(char *format, ...)
{
__int64 v1; // rbp
int v2; // er12
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( reprint )
{
v2 = rl_point;
v1 = rl_copy_text(0LL, rl_end);
rl_save_prompt();
rl_replace_line(&unk_20DE8, 0LL);
rl_redisplay();
}
va_start(arg, format);
result = vprintf(format, arg);
if ( reprint )
{
rl_restore_prompt();
rl_replace_line(v1, 0LL);
rl_point = v2;
result = rl_redisplay();
}
return result;
}

canonical_decompiled_code:
int func4(char *format, ...)
{
__int64 v1; // rbp
int v2; // er12
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( reprint )
{
v2 = rl_point;
v1 = rl_copy_text(0LL, rl_end);
rl_save_prompt();
rl_replace_line(&unk_20DE8, 0LL);
rl_redisplay();
}
va_start(arg, format);
result = vprintf(format, arg);
if ( reprint )
{
rl_restore_prompt();
rl_replace_line(v1, 0LL);
rl_point = v2;
result = rl_redisplay();
}
return result;
}

original_code:
void my_rl_printf(char *fmt, ...){
    char *saved_line;
    int saved_point;
    if (reprint){
  saved_point = rl_point;
     saved_line = rl_copy_text(0, rl_end);
        rl_save_prompt();
        rl_replace_line("", 0);
        rl_redisplay();
    }
    va_list args;
    __builtin_va_start(args,fmt);
    vprintf(fmt, args);
    __builtin_va_end(args);
    if (reprint){
        rl_restore_prompt();
        rl_replace_line(saved_line, 0);
        rl_point = saved_point;
        rl_redisplay();
    }
}

canonical_original_code:
void my_rl_printf(char *fmt, ...){
    char *saved_line;
    int saved_point;
    if (reprint){
  saved_point = rl_point;
     saved_line = rl_copy_text(0, rl_end);
        rl_save_prompt();
        rl_replace_line("", 0);
        rl_redisplay();
    }
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    vprintf(fmt, args);
    __builtin_va_end(args);
    if (reprint){
        rl_restore_prompt();
        rl_replace_line(saved_line, 0);
        rl_point = saved_point;
        rl_redisplay();
    }
}

variable_types:
fmt
char *


handleMessage

decompiled_code:
unsigned int __fastcall handleMessage(char *src, __int64 a2)
{
unsigned __int8 v4; // dl
unsigned int result; // eax
__int64 v6; // rsi
int v7; // ebp
const char *v8; // r12
int v9; // edi

v4 = *src;
result = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 1 )
{
result = *(_DWORD *)(a2 + 4);
switch ( result )
{
case 0u:
if ( v4 != 0xFE )
goto LABEL_8;
*(_DWORD *)(a2 + 4) = 1;
break;
case 1u:
if ( v4 == 0xED )
*(_DWORD *)(a2 + 4) = 2;
else
LABEL_8:
result = protocolError((_DWORD *)a2);
break;
case 2u:
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 3;
break;
case 3u:
result = (unsigned int)strncpy((char *)(a2 + 65052), src, *(int *)(a2 + 8));
*(_DWORD *)(a2 + 8) = 1;
*(_DWORD *)(a2 + 4) = 4;
break;
case 4u:
*(_BYTE *)(a2 + 65307) = v4;
result = my_rl_printf(
"Now monitoring %s every %d seconds.\n",
(const char *)(a2 + 65052),
(unsigned int)(char)v4);
*(_DWORD *)a2 = 2;
*(_DWORD *)(a2 + 4) = 0;
*(_DWORD *)(a2 + 8) = 1;
break;
default:
return result;
}
}
else if ( result == 2 )
{
result = *(_DWORD *)(a2 + 4);
switch ( result )
{
case 1u:
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 2;
break;
case 2u:
--*(_DWORD *)(a2 + 64788);
strcpy((char *)(a2 + 255LL * *(int *)(a2 + 64784) + 12), src);
result = *(_DWORD *)(a2 + 64784);
v6 = result + 1;
*(_DWORD *)(a2 + 64784) = v6;
*(_DWORD *)(a2 + 8) = 1;
v7 = *(_DWORD *)(a2 + 64788);
if ( v7 )
{
*(_DWORD *)(a2 + 4) = 1;
}
else
{
result = my_rl_printf(
"%d Change(s) on %s:%d (%s):\n",
v6,
(const char *)(a2 + 64792),
*(unsigned int *)(a2 + 65048),
(const char *)(a2 + 65052));
if ( *(int *)(a2 + 64784) > 0 )
{
v8 = (const char *)(a2 + 12);
do
{
result = my_rl_printf("\t%s\n", v8);
++v7;
v8 += 255;
}
while ( *(_DWORD *)(a2 + 64784) > v7 );
}
*(_DWORD *)(a2 + 4) = 0;
}
break;
case 0u:
result = v4;
*(_DWORD *)(a2 + 64788) = v4;
*(_DWORD *)(a2 + 8) = 1;
if ( v4 == 0xFF )
{
*(_DWORD *)(a2 + 4) = 1;
*(_DWORD *)a2 = 3;
}
else if ( v4 )
{
*(_DWORD *)(a2 + 64784) = 0;
*(_DWORD *)(a2 + 4) = 1;
result = (unsigned int)memset((void *)(a2 + 12), 0, 0xFD02uLL);
}
break;
}
}
else if ( result == 3 )
{
result = *(_DWORD *)(a2 + 4);
if ( result == 1 )
{
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 2;
}
else if ( result == 2 )
{
*(_DWORD *)a2 = 0;
v9 = *(_DWORD *)(a2 + 65308);
*((_QWORD *)&inputs + v9 / 64) &= __ROL8__(-2LL, v9 % 64);
close(v9);
if ( !memcmp("Goodbye", src, 7uLL) )
result = my_rl_printf("Disconnected from %s on %d\n", (const char *)(a2 + 64792), *(unsigned int *)(a2 + 65048));
else
result = my_rl_printf("Server Error (%s): %s\n", (const char *)(a2 + 64792), src);
}
else if ( !result && v4 == 0xFF )
{
*(_DWORD *)(a2 + 8) = 1;
*(_DWORD *)(a2 + 4) = 1;
}
}
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func5(char *src, __int64 a2)
{
unsigned __int8 v4; // dl
unsigned int result; // eax
__int64 v6; // rsi
int v7; // ebp
const char *v8; // r12
int v9; // edi

v4 = *src;
result = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 1 )
{
result = *(_DWORD *)(a2 + 4);
switch ( result )
{
case 0u:
if ( v4 != 0xFE )
goto LABEL_8;
*(_DWORD *)(a2 + 4) = 1;
break;
case 1u:
if ( v4 == 0xED )
*(_DWORD *)(a2 + 4) = 2;
else
LABEL_8:
result = func2((_DWORD *)a2);
break;
case 2u:
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 3;
break;
case 3u:
result = (unsigned int)strncpy((char *)(a2 + 65052), src, *(int *)(a2 + 8));
*(_DWORD *)(a2 + 8) = 1;
*(_DWORD *)(a2 + 4) = 4;
break;
case 4u:
*(_BYTE *)(a2 + 65307) = v4;
result = func4(
"Now monitoring %s every %d seconds.\n",
(const char *)(a2 + 65052),
(unsigned int)(char)v4);
*(_DWORD *)a2 = 2;
*(_DWORD *)(a2 + 4) = 0;
*(_DWORD *)(a2 + 8) = 1;
break;
default:
return result;
}
}
else if ( result == 2 )
{
result = *(_DWORD *)(a2 + 4);
switch ( result )
{
case 1u:
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 2;
break;
case 2u:
--*(_DWORD *)(a2 + 64788);
strcpy((char *)(a2 + 255LL * *(int *)(a2 + 64784) + 12), src);
result = *(_DWORD *)(a2 + 64784);
v6 = result + 1;
*(_DWORD *)(a2 + 64784) = v6;
*(_DWORD *)(a2 + 8) = 1;
v7 = *(_DWORD *)(a2 + 64788);
if ( v7 )
{
*(_DWORD *)(a2 + 4) = 1;
}
else
{
result = func4(
"%d Change(s) on %s:%d (%s):\n",
v6,
(const char *)(a2 + 64792),
*(unsigned int *)(a2 + 65048),
(const char *)(a2 + 65052));
if ( *(int *)(a2 + 64784) > 0 )
{
v8 = (const char *)(a2 + 12);
do
{
result = func4("\t%s\n", v8);
++v7;
v8 += 255;
}
while ( *(_DWORD *)(a2 + 64784) > v7 );
}
*(_DWORD *)(a2 + 4) = 0;
}
break;
case 0u:
result = v4;
*(_DWORD *)(a2 + 64788) = v4;
*(_DWORD *)(a2 + 8) = 1;
if ( v4 == 0xFF )
{
*(_DWORD *)(a2 + 4) = 1;
*(_DWORD *)a2 = 3;
}
else if ( v4 )
{
*(_DWORD *)(a2 + 64784) = 0;
*(_DWORD *)(a2 + 4) = 1;
result = (unsigned int)memset((void *)(a2 + 12), 0, 0xFD02uLL);
}
break;
}
}
else if ( result == 3 )
{
result = *(_DWORD *)(a2 + 4);
if ( result == 1 )
{
*(_DWORD *)(a2 + 8) = v4;
*(_DWORD *)(a2 + 4) = 2;
}
else if ( result == 2 )
{
*(_DWORD *)a2 = 0;
v9 = *(_DWORD *)(a2 + 65308);
*((_QWORD *)&inputs + v9 / 64) &= __ROL8__(-2LL, v9 % 64);
close(v9);
if ( !memcmp("Goodbye", src, 7uLL) )
result = func4("Disconnected from %s on %d\n", (const char *)(a2 + 64792), *(unsigned int *)(a2 + 65048));
else
result = func4("Server Error (%s): %s\n", (const char *)(a2 + 64792), src);
}
else if ( !result && v4 == 0xFF )
{
*(_DWORD *)(a2 + 8) = 1;
*(_DWORD *)(a2 + 4) = 1;
}
}
return result;
}

original_code:
void handleMessage(char* buf, host* h){
 unsigned char b1 = (unsigned char) buf[0];
 if (h->status == 1){
  switch (h->pstep){
   case 0:
    if (b1 != 0xFE){
     protocolError(h);
    } else {
     h->pstep++;
    }
    break;
   case 1:
    if (b1 != 0xED){
     protocolError(h);
    } else {
     h->pstep++;
    }
    break;
   case 2:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 3:
    strncpy(h->dirname,buf,h->expected);
    h->expected = 1;
    h->pstep++;
    break;
   case 4:
    h->period = b1;
    my_rl_printf("Now monitoring %s every %d seconds.\n",h->dirname,h->period);
    h->status = 2;
    h->pstep = 0;
    h->expected = 1;
    break;
  }
 } else if (h->status == 2){
  switch (h->pstep){
   case 0:
    h->lines_i = b1;
    h->expected = 1;
    if (b1 == 255){
     h->pstep = 1;
                    h->status = 3;
                } else if (b1 > 0){
                    h->cur_line = 0;
     h->pstep++;
     memset(h->lines, 0, 254*255);
                }
    break;
   case 1:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 2:
    h->lines_i--;
    sprintf(h->lines[h->cur_line],"%s",buf);
                h->cur_line++;
    h->expected = 1;
    if (h->lines_i == 0){
     my_rl_printf("%d Change(s) on %s:%d (%s):\n", h->cur_line, h->hostname, h->portnum, h->dirname);
                    for (int i =0; i < h->cur_line;i++){
                        my_rl_printf("\t%s\n",h->lines[i]);
                    }
     h->pstep = 0;
                } else
     h->pstep = 1;
    break;
  }
 } else if (h->status == 3){
  switch (h->pstep){
   case 0:
    if (b1 == 255){
     h->expected = 1;
     h->pstep++;
    }
                break;
   case 1:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 2:
    h->status = 0;
    ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
    close(h->sockfd);
    if (strncmp("Goodbye",buf,7) == 0){
     my_rl_printf("Disconnected from %s on %d\n",h->hostname,h->portnum);
    }else{
     my_rl_printf("Server Error (%s): %s\n",h->hostname,buf);
    }
    break;
  }
 }
}

canonical_original_code:
void handleMessage(char *buf, struct host *h){
 unsigned char b1 = (unsigned char) buf[0];
 if (h->status == 1){
  switch (h->pstep){
   case 0:
    if (b1 != 0xFE){
     protocolError(h);
    } else {
     h->pstep++;
    }
    break;
   case 1:
    if (b1 != 0xED){
     protocolError(h);
    } else {
     h->pstep++;
    }
    break;
   case 2:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 3:
    strncpy(h->dirname,buf,h->expected);
    h->expected = 1;
    h->pstep++;
    break;
   case 4:
    h->period = b1;
    my_rl_printf("Now monitoring %s every %d seconds.\n",h->dirname,h->period);
    h->status = 2;
    h->pstep = 0;
    h->expected = 1;
    break;
  }
 } else if (h->status == 2){
  switch (h->pstep){
   case 0:
    h->lines_i = b1;
    h->expected = 1;
    if (b1 == 255){
     h->pstep = 1;
                    h->status = 3;
                } else if (b1 > 0){
                    h->cur_line = 0;
     h->pstep++;
     memset(h->lines, 0, 254*255);
                }
    break;
   case 1:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 2:
    h->lines_i--;
    sprintf(h->lines[h->cur_line],"%s",buf);
                h->cur_line++;
    h->expected = 1;
    if (h->lines_i == 0){
     my_rl_printf("%d Change(s) on %s:%d (%s):\n", h->cur_line, h->hostname, h->portnum, h->dirname);
                    for (int i = 0; i < h->cur_line;i++){
                        my_rl_printf("\t%s\n",h->lines[i]);
                    }
     h->pstep = 0;
                } else
     h->pstep = 1;
    break;
  }
 } else if (h->status == 3){
  switch (h->pstep){
   case 0:
    if (b1 == 255){
     h->expected = 1;
     h->pstep++;
    }
                break;
   case 1:
    h->expected = (int)b1;
    h->pstep++;
    break;
   case 2:
    h->status = 0;
    ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
    close(h->sockfd);
    if (strncmp("Goodbye",buf,7) == 0){
     my_rl_printf("Disconnected from %s on %d\n",h->hostname,h->portnum);
    }else{
     my_rl_printf("Server Error (%s): %s\n",h->hostname,buf);
    }
    break;
  }
 }
}

variable_types:
buf
char *

variable_types: i int


startClient

decompiled_code:
void __noreturn startClient()
{
unsigned __int64 v0; // rdi
const char *v1; // rsi
int v2; // ebx
int v3; // er13
__int64 v4; // r14
__int64 v5; // r13
struct timeval v6; // [rsp+0h] [rbp-1C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-1B8h] BYREF
char buf[248]; // [rsp+90h] [rbp-138h] BYREF
int v9; // [rsp+188h] [rbp-40h]
__int16 v10; // [rsp+18Ch] [rbp-3Ch]
char v11; // [rsp+18Eh] [rbp-3Ah]

v6.tv_sec = 0LL;
v6.tv_usec = 100LL;
memset(&inputs, 0, 0x80uLL);
memset(&readfds, 0, sizeof(readfds));
clearHosts(hosts, 2LL);
*(_QWORD *)&inputs = inputs | 1;
fdmax = 0;
_sysv_signal(1, (__sighandler_t)terminateAll);
puts("Welcome to Directory Monitor!");
puts(help);
rl_callback_handler_install("> ", execCommand);
using_history("> ", execCommand);
while ( 1 )
{
do
{
do
{
*(__m128i *)readfds.fds_bits = _mm_load_si128((const __m128i *)&inputs);
*(__m128i *)&readfds.fds_bits[2] = _mm_load_si128((const __m128i *)&inputs + 1);
*(__m128i *)&readfds.fds_bits[4] = _mm_load_si128((const __m128i *)&inputs + 2);
*(__m128i *)&readfds.fds_bits[6] = _mm_load_si128((const __m128i *)&inputs + 3);
*(__m128i *)&readfds.fds_bits[8] = _mm_load_si128((const __m128i *)&inputs + 4);
*(__m128i *)&readfds.fds_bits[10] = _mm_load_si128((const __m128i *)&inputs + 5);
*(__m128i *)&readfds.fds_bits[12] = _mm_load_si128((const __m128i *)&inputs + 6);
*(__m128i *)&readfds.fds_bits[14] = _mm_load_si128((const __m128i *)&inputs + 7);
v0 = (unsigned int)(fdmax + 1);
v1 = (const char *)&readfds;
}
while ( select(v0, &readfds, 0LL, 0LL, &v6) == -1 );
v6.tv_usec = 100LL;
}
while ( fdmax < 0 );
v2 = 0;
do
{
v4 = 1LL << (v2 % 64);
if ( (readfds.fds_bits[v2 / 64] & v4) != 0 )
{
if ( v2 )
{
memset(buf, 0, sizeof(buf));
v9 = 0;
v10 = 0;
v11 = 0;
v3 = selectHostFD(hosts, (unsigned int)v2, 2LL);
if ( recv(v2, buf, *(int *)&hosts[65312 * v3 + 8], 0) <= 0 )
{
v5 = 65312LL * v3;
v1 = &hosts[v5 + 64792];
my_rl_printf("%s on %d disconnected unexpectedly\n", v1, *(unsigned int *)&hosts[v5 + 65048]);
*(_DWORD *)&hosts[v5] = 0;
v0 = (unsigned int)v2;
close(v2);
*((_QWORD *)&inputs + v2 / 64) &= ~v4;
}
else
{
v1 = &hosts[65312 * v3];
v0 = (unsigned __int64)buf;
handleMessage(buf, (__int64)v1);
}
}
else
{
reprint = 0;
rl_callback_read_char(v0, v1);
reprint = 1;
}
}
++v2;
}
while ( fdmax >= v2 );
}
}

canonical_decompiled_code:
void __noreturn func6()
{
unsigned __int64 v0; // rdi
const char *v1; // rsi
int v2; // ebx
int v3; // er13
__int64 v4; // r14
__int64 v5; // r13
struct timeval v6; // [rsp+0h] [rbp-1C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-1B8h] BYREF
char buf[248]; // [rsp+90h] [rbp-138h] BYREF
int v9; // [rsp+188h] [rbp-40h]
__int16 v10; // [rsp+18Ch] [rbp-3Ch]
char v11; // [rsp+18Eh] [rbp-3Ah]

v6.tv_sec = 0LL;
v6.tv_usec = 100LL;
memset(&inputs, 0, 0x80uLL);
memset(&readfds, 0, sizeof(readfds));
clearHosts(hosts, 2LL);
*(_QWORD *)&inputs = inputs | 1;
fdmax = 0;
_sysv_signal(1, (__sighandler_t)terminateAll);
puts("Welcome to Directory Monitor!");
puts(help);
rl_callback_handler_install("> ", execCommand);
using_history("> ", execCommand);
while ( 1 )
{
do
{
do
{
*(__m128i *)readfds.fds_bits = _mm_load_si128((const __m128i *)&inputs);
*(__m128i *)&readfds.fds_bits[2] = _mm_load_si128((const __m128i *)&inputs + 1);
*(__m128i *)&readfds.fds_bits[4] = _mm_load_si128((const __m128i *)&inputs + 2);
*(__m128i *)&readfds.fds_bits[6] = _mm_load_si128((const __m128i *)&inputs + 3);
*(__m128i *)&readfds.fds_bits[8] = _mm_load_si128((const __m128i *)&inputs + 4);
*(__m128i *)&readfds.fds_bits[10] = _mm_load_si128((const __m128i *)&inputs + 5);
*(__m128i *)&readfds.fds_bits[12] = _mm_load_si128((const __m128i *)&inputs + 6);
*(__m128i *)&readfds.fds_bits[14] = _mm_load_si128((const __m128i *)&inputs + 7);
v0 = (unsigned int)(fdmax + 1);
v1 = (const char *)&readfds;
}
while ( select(v0, &readfds, 0LL, 0LL, &v6) == -1 );
v6.tv_usec = 100LL;
}
while ( fdmax < 0 );
v2 = 0;
do
{
v4 = 1LL << (v2 % 64);
if ( (readfds.fds_bits[v2 / 64] & v4) != 0 )
{
if ( v2 )
{
memset(buf, 0, sizeof(buf));
v9 = 0;
v10 = 0;
v11 = 0;
v3 = selectHostFD(hosts, (unsigned int)v2, 2LL);
if ( recv(v2, buf, *(int *)&hosts[65312 * v3 + 8], 0) <= 0 )
{
v5 = 65312LL * v3;
v1 = &hosts[v5 + 64792];
func4("%s on %d disconnected unexpectedly\n", v1, *(unsigned int *)&hosts[v5 + 65048]);
*(_DWORD *)&hosts[v5] = 0;
v0 = (unsigned int)v2;
close(v2);
*((_QWORD *)&inputs + v2 / 64) &= ~v4;
}
else
{
v1 = &hosts[65312 * v3];
v0 = (unsigned __int64)buf;
func5(buf, (__int64)v1);
}
}
else
{
reprint = 0;
rl_callback_read_char(v0, v1);
reprint = 1;
}
}
++v2;
}
while ( fdmax >= v2 );
}
}

original_code:
void startClient(){
    char buf[255];
    fd_set read_fds;
 struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 100;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inputs)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
    clearHosts(hosts,2);
 ((void) (((&inputs)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
    fdmax = 0;
 signal(1, &terminateAll);
 printf("Welcome to Directory Monitor!\n");
    printf("%s\n",help);
 rl_callback_handler_install("> ", &execCommand);
 using_history();
    while (1){
        read_fds = inputs;
        if(select(fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv) == -1){
            continue;
  }
        tv.tv_usec = 100;
  for(int i = 0; i <= fdmax; i++){
   if(((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
    if (i == 0){
     reprint = 0;
     rl_callback_read_char();
     reprint = 1;
                } else {
     memset(buf,0,sizeof(buf));
                    int n = selectHostFD(hosts,i,2);
                    if((recv(i, buf, hosts[n].expected, 0)) <= 0){
                        my_rl_printf("%s on %d disconnected unexpectedly\n", hosts[n].hostname,hosts[n].portnum);
                        hosts[n].status = 0;
                        close(i);
                        ((void) (((&inputs)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))));
                    } else {
      handleMessage(buf,&hosts[n]);
                    }
                }
            }
        }
    }
}

canonical_original_code:
void startClient(){
    char buf[255];
    struct fd_set read_fds;
 struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 100;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&inputs)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
    clearHosts(hosts,2);
 ((void) (((&inputs)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
    fdmax = 0;
 signal(1, &terminateAll);
 printf("Welcome to Directory Monitor!\n");
    printf("%s\n",help);
 rl_callback_handler_install("> ", &execCommand);
 using_history();
    while (1){
        read_fds = inputs;
        if(select(fdmax+1, &read_fds, ((void *)0), ((void *)0), &tv) == -1){
            continue;
  }
        tv.tv_usec = 100;
  for(int i = 0; i <= fdmax; i++){
   if(((((&read_fds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
    if (i == 0){
     reprint = 0;
     rl_callback_read_char();
     reprint = 1;
                } else {
     memset(buf,0,sizeof(buf));
                    int n = selectHostFD(hosts,i,2);
                    if((recv(i, buf, hosts[n].expected, 0)) <= 0){
                        my_rl_printf("%s on %d disconnected unexpectedly\n", hosts[n].hostname,hosts[n].portnum);
                        hosts[n].status = 0;
                        close(i);
                        ((void) (((&inputs)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))));
                    } else {
      handleMessage(buf,&hosts[n]);
                    }
                }
            }
        }
    }
}

variable_types:
buf
char [255]

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


addHost

decompiled_code:
int __fastcall addHost(char *s1, unsigned int a2)
{
int v2; // ebp
struct hostent *v3; // r13
char *v4; // r13
int v5; // eax
int v6; // edx
__int64 v7; // rax
size_t v8; // rax
size_t v9; // rax
struct sockaddr v11[3]; // [rsp+0h] [rbp-38h] BYREF

if ( (unsigned int)checkPort(a2) )
{
if ( *(_DWORD *)hosts == 2
&& (v8 = strlen((const char *)(unsigned int)((_DWORD)hosts + 64792)),
!strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 64792), v8))
&& a2 == *(_DWORD *)&hosts[65048]
|| *(_DWORD *)&hosts[65312] == 2
&& (v9 = strlen((const char *)(unsigned int)((_DWORD)hosts + 130104)),
!strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 130104), v9))
&& *(_DWORD *)&hosts[130360] == a2 )
{
LODWORD(v7) = my_rl_printf("Already connected to %s on port %d\n", s1, a2);
}
else if ( (int)activeHosts(hosts, 2LL) > 1 )
{
LODWORD(v7) = my_rl_printf("Unable to add server; maximum number reached (%d)\n", 2LL);
}
else
{
my_rl_printf("Attempting to connect to %s on port %d\n", s1, a2);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
LODWORD(v7) = fwrite("Error opening socket!\n", 1uLL, 0x16uLL, stderr);
}
else
{
v3 = gethostbyname(s1);
if ( v3 )
{
bzero(v11, 0x10uLL);
v11[0].sa_family = 2;
bcopy(*(const void **)v3->h_addr_list, &v11[0].sa_data[2], v3->h_length);
*(_WORD *)v11[0].sa_data = _bswap_16(a2);
if ( connect(v2, v11, 0x10u) < 0 )
{
LODWORD(v7) = fprintf(stderr, "Error, could not connect (make sure server is running on %s %d)\n", s1, a2);
}
else
{
v4 = &hosts[65312 * (int)selectDiscHost(hosts, 2LL)];
initHost(v4, s1, (unsigned int)v2, a2);
v5 = *((_DWORD *)v4 + 16327);
v6 = v5 + 63;
if ( v5 >= 0 )
v6 = *((_DWORD *)v4 + 16327);
v7 = 1LL << (v5 % 64);
*((_QWORD *)&inputs + (v6 >> 6)) |= v7;
if ( fdmax < v2 )
fdmax = v2;
}
}
else
{
LODWORD(v7) = fprintf(stderr, "Error, host with name %s does not exist!\n", s1);
}
}
}
}
else
{
LODWORD(v7) = my_rl_printf("Port number not correct\n");
}
return v7;
}

canonical_decompiled_code:
int __fastcall func7(char *s1, unsigned int a2)
{
int v2; // ebp
struct hostent *v3; // r13
char *v4; // r13
int v5; // eax
int v6; // edx
__int64 v7; // rax
size_t v8; // rax
size_t v9; // rax
struct sockaddr v11[3]; // [rsp+0h] [rbp-38h] BYREF

if ( (unsigned int)checkPort(a2) )
{
if ( *(_DWORD *)hosts == 2
&& (v8 = strlen((const char *)(unsigned int)((_DWORD)hosts + 64792)),
!strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 64792), v8))
&& a2 == *(_DWORD *)&hosts[65048]
|| *(_DWORD *)&hosts[65312] == 2
&& (v9 = strlen((const char *)(unsigned int)((_DWORD)hosts + 130104)),
!strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 130104), v9))
&& *(_DWORD *)&hosts[130360] == a2 )
{
LODWORD(v7) = func4("Already connected to %s on port %d\n", s1, a2);
}
else if ( (int)activeHosts(hosts, 2LL) > 1 )
{
LODWORD(v7) = func4("Unable to add server; maximum number reached (%d)\n", 2LL);
}
else
{
func4("Attempting to connect to %s on port %d\n", s1, a2);
v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
LODWORD(v7) = fwrite("Error opening socket!\n", 1uLL, 0x16uLL, stderr);
}
else
{
v3 = gethostbyname(s1);
if ( v3 )
{
bzero(v11, 0x10uLL);
v11[0].sa_family = 2;
bcopy(*(const void **)v3->h_addr_list, &v11[0].sa_data[2], v3->h_length);
*(_WORD *)v11[0].sa_data = func1(a2);
if ( connect(v2, v11, 0x10u) < 0 )
{
LODWORD(v7) = fprintf(stderr, "Error, could not connect (make sure server is running on %s %d)\n", s1, a2);
}
else
{
v4 = &hosts[65312 * (int)selectDiscHost(hosts, 2LL)];
initHost(v4, s1, (unsigned int)v2, a2);
v5 = *((_DWORD *)v4 + 16327);
v6 = v5 + 63;
if ( v5 >= 0 )
v6 = *((_DWORD *)v4 + 16327);
v7 = 1LL << (v5 % 64);
*((_QWORD *)&inputs + (v6 >> 6)) |= v7;
if ( fdmax < v2 )
fdmax = v2;
}
}
else
{
LODWORD(v7) = fprintf(stderr, "Error, host with name %s does not exist!\n", s1);
}
}
}
}
else
{
LODWORD(v7) = func4("Port number not correct\n");
}
return v7;
}

original_code:
void addHost(char* hostname, int portnumber){
 if (!checkPort(portnumber)){
  my_rl_printf("Port number not correct\n");
  return;
 }
    for (int n = 0; n < 2; n++){
        if (hosts[n].status == 2
                && strncmp(hostname,hosts[n].hostname,strlen(hosts[n].hostname))== 0
                && hosts[n].portnum == portnumber){
            my_rl_printf("Already connected to %s on port %d\n",hostname,portnumber);
            return;
        }
    }
 if (activeHosts(hosts,2) >= 2){
  my_rl_printf("Unable to add server; maximum number reached (%d)\n",2);
  return;
 }
 my_rl_printf("Attempting to connect to %s on port %d\n",hostname,portnumber);
 struct sockaddr_in serv_addr;
 struct hostent *server;
 int sockfd;
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0){
  fprintf(stderr,"Error opening socket!\n");
  return;
 }
 server = gethostbyname(hostname);
 if (server == ((void *)0)) {
  fprintf(stderr,"Error, host with name %s does not exist!\n",hostname);
  return;
 }
 bzero((char *) &serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portnumber);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
  fprintf(stderr,"Error, could not connect (make sure server is running on %s %d)\n",hostname,portnumber);
  return;
 }
 int n = selectDiscHost(hosts,2);
 host* h = &hosts[n];
    initHost(h, hostname, sockfd, portnumber);
 ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
 if (sockfd > fdmax){
  fdmax = sockfd;
 }
}

canonical_original_code:
void addHost(char *hostname, int portnumber){
 if (!checkPort(portnumber)){
  my_rl_printf("Port number not correct\n");
  return;
 }
    for (int n = 0; n < 2; n++){
        if (hosts[n].status == 2
                && strncmp(hostname,hosts[n].hostname,strlen(hosts[n].hostname))== 0
                && hosts[n].portnum == portnumber){
            my_rl_printf("Already connected to %s on port %d\n",hostname,portnumber);
            return;
        }
    }
 if (activeHosts(hosts,2) >= 2){
  my_rl_printf("Unable to add server; maximum number reached (%d)\n",2);
  return;
 }
 my_rl_printf("Attempting to connect to %s on port %d\n",hostname,portnumber);
 struct sockaddr_in serv_addr;
 struct hostent *server;
 int sockfd;
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0){
  fprintf(stderr,"Error opening socket!\n");
  return;
 }
 server = gethostbyname(hostname);
 if (server == ((void *)0)) {
  fprintf(stderr,"Error, host with name %s does not exist!\n",hostname);
  return;
 }
 bzero((char *) &serv_addr, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portnumber);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
  fprintf(stderr,"Error, could not connect (make sure server is running on %s %d)\n",hostname,portnumber);
  return;
 }
 int n = selectDiscHost(hosts,2);
 struct host *h = &hosts[n];
    initHost(h, hostname, sockfd, portnumber);
 ((void) (((&inputs)->__fds_bits)[((h->sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((h->sockfd) % (8 * (int) sizeof (__fd_mask)))))));
 if (sockfd > fdmax){
  fdmax = sockfd;
 }
}

variable_types:
hostname
char *

variable_types: portnumber int

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

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

variable_types: sockfd int

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


removeHost

decompiled_code:
int __fastcall removeHost(char *s1, unsigned int a2)
{
size_t v2; // rax
int v3; // eax
size_t v4; // rax

if ( !(unsigned int)checkPort(a2) )
return my_rl_printf("Port number not correct\n");
v2 = strlen((const char *)(unsigned int)((_DWORD)hosts + 64792));
v3 = strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 64792), v2);
if ( v3 || a2 != *(_DWORD *)&hosts[65048] )
{
v4 = strlen((const char *)(unsigned int)((_DWORD)hosts + 130104));
if ( strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 130104), v4) || *(_DWORD *)&hosts[130360] != a2 )
return my_rl_printf("Not connected to %s on port %d. Remove failed\n", s1, a2);
v3 = 1;
}
return terminateClientConnection(&hosts[65312 * v3]);
}

canonical_decompiled_code:
int __fastcall func8(char *s1, unsigned int a2)
{
size_t v2; // rax
int v3; // eax
size_t v4; // rax

if ( !(unsigned int)checkPort(a2) )
return func4("Port number not correct\n");
v2 = strlen((const char *)(unsigned int)((_DWORD)hosts + 64792));
v3 = strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 64792), v2);
if ( v3 || a2 != *(_DWORD *)&hosts[65048] )
{
v4 = strlen((const char *)(unsigned int)((_DWORD)hosts + 130104));
if ( strncmp(s1, (const char *)(unsigned int)((_DWORD)hosts + 130104), v4) || *(_DWORD *)&hosts[130360] != a2 )
return func4("Not connected to %s on port %d. Remove failed\n", s1, a2);
v3 = 1;
}
return terminateClientConnection(&hosts[65312 * v3]);
}

original_code:
void removeHost(char* hostname, int portnumber){
 if (!checkPort(portnumber)){
  my_rl_printf("Port number not correct\n");
  return;
 }
 for (int i =0; i < 2; i++){
  host* h = &hosts[i];
  if (strncmp(hostname,h->hostname,strlen(h->hostname)) == 0
    && h->portnum == portnumber){
   terminateClientConnection(h);
   return;
  }
 }
 my_rl_printf("Not connected to %s on port %d. Remove failed\n",hostname,portnumber);
}

canonical_original_code:
void removeHost(char *hostname, int portnumber){
 if (!checkPort(portnumber)){
  my_rl_printf("Port number not correct\n");
  return;
 }
 for (int i = 0; i < 2; i++){
  struct host *h = &hosts[i];
  if (strncmp(hostname,h->hostname,strlen(h->hostname)) == 0
    && h->portnum == portnumber){
   terminateClientConnection(h);
   return;
  }
 }
 my_rl_printf("Not connected to %s on port %d. Remove failed\n",hostname,portnumber);
}


listHosts

decompiled_code:
int listHosts()
{
unsigned int v0; // eax
int result; // eax

v0 = activeHosts(hosts, 2LL);
if ( !v0 )
return my_rl_printf("Not currently connected to any hosts\n");
result = my_rl_printf("Connected to %d of %d hosts:\n", v0, 2LL);
if ( *(_DWORD *)hosts == 2 )
result = my_rl_printf(
"%s on port %d (Directory: %s Period: %d seconds)\n",
(const char *)(unsigned int)((_DWORD)hosts + 64792),
*(unsigned int *)&hosts[65048],
(const char *)(unsigned int)((_DWORD)hosts + 65052),
(unsigned int)hosts[65307]);
if ( *(_DWORD *)&hosts[65312] == 2 )
result = my_rl_printf(
"%s on port %d (Directory: %s Period: %d seconds)\n",
(const char *)(unsigned int)((_DWORD)hosts + 130104),
*(unsigned int *)&hosts[130360],
(const char *)(unsigned int)((_DWORD)hosts + 130364),
(unsigned int)hosts[130619]);
return result;
}

canonical_decompiled_code:
int func9()
{
unsigned int v0; // eax
int result; // eax

v0 = activeHosts(hosts, 2LL);
if ( !v0 )
return func4("Not currently connected to any hosts\n");
result = func4("Connected to %d of %d hosts:\n", v0, 2LL);
if ( *(_DWORD *)hosts == 2 )
result = func4(
"%s on port %d (Directory: %s Period: %d seconds)\n",
(const char *)(unsigned int)((_DWORD)hosts + 64792),
*(unsigned int *)&hosts[65048],
(const char *)(unsigned int)((_DWORD)hosts + 65052),
(unsigned int)hosts[65307]);
if ( *(_DWORD *)&hosts[65312] == 2 )
result = func4(
"%s on port %d (Directory: %s Period: %d seconds)\n",
(const char *)(unsigned int)((_DWORD)hosts + 130104),
*(unsigned int *)&hosts[130360],
(const char *)(unsigned int)((_DWORD)hosts + 130364),
(unsigned int)hosts[130619]);
return result;
}

original_code:
void listHosts(){
 int aH = activeHosts(hosts,2);
 if (aH == 0){
  my_rl_printf("Not currently connected to any hosts\n");
  return;
 }
 my_rl_printf("Connected to %d of %d hosts:\n",aH, 2);
 for (int i = 0; i < 2; i++){
  host* h = &hosts[i];
  if (h->status == 2)
   my_rl_printf("%s on port %d (Directory: %s Period: %d seconds)\n",h->hostname,h->portnum,h->dirname,h->period);
 }
}

canonical_original_code:
void listHosts(){
 int aH = activeHosts(hosts,2);
 if (aH == 0){
  my_rl_printf("Not currently connected to any hosts\n");
  return;
 }
 my_rl_printf("Connected to %d of %d hosts:\n",aH, 2);
 for (int i = 0; i < 2; i++){
  struct host *h = &hosts[i];
  if (h->status == 2)
   my_rl_printf("%s on port %d (Directory: %s Period: %d seconds)\n",h->hostname,h->portnum,h->dirname,h->period);
 }
}

variable_types: aH int


execCommand

decompiled_code:
int __fastcall execCommand(char *a1)
{
char v2; // dl
int result; // eax
char v4; // [rsp+9h] [rbp-10Fh] BYREF
unsigned int v5; // [rsp+Ch] [rbp-10Ch] BYREF
char s1[264]; // [rsp+10h] [rbp-108h] BYREF

add_history();
v2 = *a1;
if ( *a1 == 113 || !memcmp(a1, "quit", 4uLL) || !strcmp(a1, "exit") )
return quit();
if ( v2 == 97 || !memcmp(a1, "add ", 4uLL) )
{
if ( (unsigned int)_isoc99_sscanf(a1, "%s %s %d", &v4, s1, &v5) == 3 )
result = addHost(s1, v5);
else
result = my_rl_printf("Usage: add <hostname> <portnumber>\n");
}
else if ( v2 == 114 || !memcmp(a1, "remove ", 7uLL) )
{
if ( (unsigned int)_isoc99_sscanf(a1, "%s %s %d", &v4, s1, &v5) == 3 )
result = removeHost(s1, v5);
else
result = my_rl_printf("Usage: remove <hostname> <portnumber>\n");
}
else if ( v2 == 108 || !memcmp(a1, "list", 4uLL) )
{
result = listHosts();
}
else
{
result = my_rl_printf("%s\n", help);
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(char *a1)
{
char v2; // dl
int result; // eax
char v4; // [rsp+9h] [rbp-10Fh] BYREF
unsigned int v5; // [rsp+Ch] [rbp-10Ch] BYREF
char s1[264]; // [rsp+10h] [rbp-108h] BYREF

add_history();
v2 = *a1;
if ( *a1 == 113 || !memcmp(a1, "quit", 4uLL) || !strcmp(a1, "exit") )
return quit();
if ( v2 == 97 || !memcmp(a1, "add ", 4uLL) )
{
if ( (unsigned int)_isoc99_sscanf(a1, "%s %s %d", &v4, s1, &v5) == 3 )
result = func7(s1, v5);
else
result = func4("Usage: add <hostname> <portnumber>\n");
}
else if ( v2 == 114 || !memcmp(a1, "remove ", 7uLL) )
{
if ( (unsigned int)_isoc99_sscanf(a1, "%s %s %d", &v4, s1, &v5) == 3 )
result = func8(s1, v5);
else
result = func4("Usage: remove <hostname> <portnumber>\n");
}
else if ( v2 == 108 || !memcmp(a1, "list", 4uLL) )
{
result = func9();
}
else
{
result = func4("%s\n", help);
}
return result;
}

original_code:
void execCommand(char* command){
 add_history(command);
 if (strncmp(command,"q",1) == 0 || strncmp(command,"quit",4) == 0 || strncmp(command,"exit",5) == 0 ){
  quit();
 }
 else if (strncmp(command,"a",1) == 0 || strncmp(command,"add ",4) == 0){
  char hostname[255];
  int portnumber;
  char add[3];
  if (sscanf(command,"%s %s %d",add,hostname,&portnumber) == 3){
   addHost(hostname,portnumber);
  } else {
   my_rl_printf("Usage: add <hostname> <portnumber>\n");
  }
 }
 else if (strncmp(command,"r",1) == 0 || strncmp(command,"remove ",7) == 0){
  char hostname[255];
  int portnumber;
  char remove[3];
  if (sscanf(command,"%s %s %d",remove,hostname,&portnumber) == 3){
   removeHost(hostname,portnumber);
  } else {
   my_rl_printf("Usage: remove <hostname> <portnumber>\n");
  }
 }
 else if (strncmp(command,"l",1) == 0 || strncmp(command,"list",4) == 0){
  listHosts();
 }
 else {
  my_rl_printf("%s\n",help);
 }
}

canonical_original_code:
void execCommand(char *command){
 add_history(command);
 if (strncmp(command,"q",1) == 0 || strncmp(command,"quit",4) == 0 || strncmp(command,"exit",5) == 0 ){
  quit();
 }
 else if (strncmp(command,"a",1) == 0 || strncmp(command,"add ",4) == 0){
  char hostname[255];
  int portnumber;
  char add[3];
  if (sscanf(command,"%s %s %d",add,hostname,&portnumber) == 3){
   addHost(hostname,portnumber);
  } else {
   my_rl_printf("Usage: add <hostname> <portnumber>\n");
  }
 }
 else if (strncmp(command,"r",1) == 0 || strncmp(command,"remove ",7) == 0){
  char hostname[255];
  int portnumber;
  char remove[3];
  if (sscanf(command,"%s %s %d",remove,hostname,&portnumber) == 3){
   removeHost(hostname,portnumber);
  } else {
   my_rl_printf("Usage: remove <hostname> <portnumber>\n");
  }
 }
 else if (strncmp(command,"l",1) == 0 || strncmp(command,"list",4) == 0){
  listHosts();
 }
 else {
  my_rl_printf("%s\n",help);
 }
}

variable_types:
command
char *

variable_types:
hostname
char [255]

variable_types:
add
char [3]

variable_types:
remove
char [3]


selectHostFD

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

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = (_DWORD *)(a1 + 65308);
result = 0LL;
while ( *v3 != a2 )
{
result = (unsigned int)(result + 1);
v3 += 16328;
if ( a3 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
if ( a3 <= (int)result )
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a3 <= 0 )
return 0xFFFFFFFFLL;
v3 = (_DWORD *)(a1 + 65308);
result = 0LL;
while ( *v3 != a2 )
{
result = (unsigned int)(result + 1);
v3 += 16328;
if ( a3 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
if ( a3 <= (int)result )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int selectHostFD(host* hosts, int i, int size){
    int n;
    for(n = 0; n < size; n++){
        if (hosts[n].sockfd == i) break;
    }
    if (n >= size || hosts[n].sockfd != i)
        return -1;
    return n;
}

canonical_original_code:
int selectHostFD(struct host *hosts, int i, int size){
    int n;
    for(n = 0; n < size; n++){
        if (hosts[n].sockfd == i) break;
    }
    if (n >= size || hosts[n].sockfd != i)
        return -1;
    return n;
}

variable_types: struct host { int status; int pstep; int expected; char lines[254][255]; int cur_line; int lines_i; char hostname[255]; int portnum; char dirname[255]; char period; int sockfd; } *

variable_types: size int

return_type: int


selectDiscHost

decompiled_code:
__int64 __fastcall selectDiscHost(_DWORD *a1, int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *a1 )
{
result = (unsigned int)(result + 1);
a1 += 16328;
if ( a2 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
if ( a2 <= (int)result )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, int a2)
{
__int64 result; // rax

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *a1 )
{
result = (unsigned int)(result + 1);
a1 += 16328;
if ( a2 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
if ( a2 <= (int)result )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int selectDiscHost(host* hosts, int size){
    int n;
    for (n = 0; n < size; n++){
  if (hosts[n].status == 0)
   break;
 }
    if (n >= size || hosts[n].status != 0)
        return -1;
    return n;
}

canonical_original_code:
int selectDiscHost(struct host *hosts, int size){
    int n;
    for (n = 0; n < size; n++){
  if (hosts[n].status == 0)
   break;
 }
    if (n >= size || hosts[n].status != 0)
        return -1;
    return n;
}

variable_types: n int


activeHosts

original_code:
int activeHosts(host* hosts, int size){
 int r = 0;
 for (int i = 0; i < size; i++){
  if (hosts[i].status == 1 || hosts[i].status == 2)
   r++;
 }
 return r;
}

canonical_original_code:
int activeHosts(struct host *hosts, int size){
 int r = 0;
 for (int i = 0; i < size; i++){
  if (hosts[i].status == 1 || hosts[i].status == 2)
   r++;
 }
 return r;
}

variable_types: r int


initHost

decompiled_code:
char *__fastcall initHost(char *a1, const char *a2, int a3, int a4)
{
size_t v6; // rax
char *result; // rax

memset(a1, 0, 0xFF20uLL);
*(_DWORD *)a1 = 1;
v6 = strlen(a2);
result = strncpy(a1 + 64792, a2, v6);
*((_DWORD *)a1 + 16262) = a4;
*((_DWORD *)a1 + 16327) = a3;
*((_DWORD *)a1 + 2) = 1;
return result;
}

canonical_decompiled_code:
char *__fastcall func5(char *a1, const char *a2, int a3, int a4)
{
size_t v6; // rax
char *result; // rax

memset(a1, 0, 0xFF20uLL);
*(_DWORD *)a1 = 1;
v6 = strlen(a2);
result = strncpy(a1 + 64792, a2, v6);
*((_DWORD *)a1 + 16262) = a4;
*((_DWORD *)a1 + 16327) = a3;
*((_DWORD *)a1 + 2) = 1;
return result;
}

original_code:
void initHost(host* h, char* hostname, int sockfd, int portno){
    memset(h,0,sizeof(host));
 h->status = 1;
 strncpy(h->hostname,hostname,strlen(hostname));
 h->portnum = portno;
 h->sockfd = sockfd;
 h->pstep = 0;
 h->expected = 1;
}

canonical_original_code:
void initHost(struct host *h, char *hostname, int sockfd, int portno){
    memset(h,0,sizeof(host));
 h->status = 1;
 strncpy(h->hostname,hostname,strlen(hostname));
 h->portnum = portno;
 h->sockfd = sockfd;
 h->pstep = 0;
 h->expected = 1;
}

variable_types: portno int


writeToClients

decompiled_code:
ssize_t __fastcall writeToClients(_DWORD *a1, int a2, const void *a3, int a4)
{
_DWORD *v5; // rbx
ssize_t result; // rax
__int64 v7; // rbp
size_t v8; // r12

if ( a2 > 0 )
{
v5 = a1;
result = 65312LL * (unsigned int)(a2 - 1);
v7 = (__int64)a1 + result + 65312;
v8 = a4;
do
{
if ( *v5 == 2 )
result = write(v5[16327], a3, v8);
v5 += 16328;
}
while ( v5 != (_DWORD *)v7 );
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func6(_DWORD *a1, int a2, const void *a3, int a4)
{
_DWORD *v5; // rbx
ssize_t result; // rax
__int64 v7; // rbp
size_t v8; // r12

if ( a2 > 0 )
{
v5 = a1;
result = 65312LL * (unsigned int)(a2 - 1);
v7 = (__int64)a1 + result + 65312;
v8 = a4;
do
{
if ( *v5 == 2 )
result = write(v5[16327], a3, v8);
v5 += 16328;
}
while ( v5 != (_DWORD *)v7 );
}
return result;
}

original_code:
void writeToClients(host* hosts, int max, char* buf, int size){
    for (int n = 0; n < max; n++){
        if (hosts[n].status == 2){
            write(hosts[n].sockfd,buf,size);
        }
    }
}

canonical_original_code:
void writeToClients(struct host *hosts, int max, char *buf, int size){
    for (int n = 0; n < max; n++){
        if (hosts[n].status == 2){
            write(hosts[n].sockfd,buf,size);
        }
    }
}

variable_types: max int

variable_types:
buf
char *


repo: krizhanovsky/NatSys-Lab

decompiled_code:
__int64 __fastcall atomic_cmpxchg(volatile signed __int32 *a1, signed __int32 a2, signed __int32 a3)
{
return (unsigned int)_InterlockedCompareExchange(a1, a3, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(volatile signed __int32 *a1, signed __int32 a2, signed __int32 a3)
{
return (unsigned int)_InterlockedCompareExchange(a1, a3, a2);
}

original_code:
static inline int
atomic_cmpxchg(atomic_t *v, int old, int new_p)
{
 __atomic_compare_exchange_n(&v->counter, &old, new_p, 0,
        5, 0);
 return old;
}

canonical_original_code:
static inline int atomic_cmpxchg(struct atomic_t *v, int old, int new_p)
{
 __atomic_compare_exchange_n(&v->counter, &old, new_p, 0,
        5, 0);
 return old;
}

variable_types:
v
struct atomic_t { int counter; } *

variable_types: old int


decompiled_code:
signed __int64 __fastcall atomic64_cmpxchg(volatile signed __int64 *a1, signed __int64 a2, signed __int64 a3)
{
return _InterlockedCompareExchange64(a1, a3, a2);
}

canonical_decompiled_code:
signed __int64 __fastcall func1(volatile signed __int64 *a1, signed __int64 a2, signed __int64 a3)
{
return _InterlockedCompareExchange64(a1, a3, a2);
}

original_code:
static inline long
atomic64_cmpxchg(atomic64_t *v, long old, long new_p)
{
 __atomic_compare_exchange_n(&v->counter, &old, new_p, 0,
        5, 0);
 return old;
}

canonical_original_code:
static inline long atomic64_cmpxchg(struct atomic64_t *v, long old, long new_p)
{
 __atomic_compare_exchange_n(&v->counter, &old, new_p, 0,
        5, 0);
 return old;
}

variable_types:
v
struct atomic64_t { long counter; } *

variable_types: old long

return_type: long


decompiled_code:
bool __fastcall lfs_push(volatile signed __int32 *a1, volatile signed __int32 *a2, signed __int32 a3)
{
signed __int32 v4; // ebx

v4 = *a1;
*a2 = *a1;
while ( (unsigned int)atomic_cmpxchg(a1, v4, a3) != v4 )
{
v4 = *a1;
*a2 = *a1;
}
return v4 == -1;
}

canonical_decompiled_code:
bool __fastcall func4(volatile signed __int32 *a1, volatile signed __int32 *a2, signed __int32 a3)
{
signed __int32 v4; // ebx

v4 = *a1;
*a2 = *a1;
while ( (unsigned int)func0(a1, v4, a3) != v4 )
{
v4 = *a1;
*a2 = *a1;
}
return v4 == -1;
}

original_code:
static inline _Bool
lfs_push(LfStack *stack, SEntry *entry, int off)
{
 int old_head = (*(volatile int *)&(&stack->head)->counter);
 entry->next = old_head;
 while (atomic_cmpxchg(&stack->head, old_head, off) != old_head) {
  old_head = (*(volatile int *)&(&stack->head)->counter);
  entry->next = old_head;
 }
 return old_head == (-1);
}

canonical_original_code:
static inline _Bool lfs_push(struct LfStack *stack, struct SEntry *entry, int off)
{
 int old_head = (*(volatile int *)&(&stack->head)->counter);
 entry->next = old_head;
 while (atomic_cmpxchg(&stack->head, old_head, off) != old_head) {
  old_head = (*(int *)&(&stack->head)->counter);
  entry->next = old_head;
 }
 return old_head == (-1);
}

variable_types: struct LfStack { union { struct { struct atomic_t { int counter; } head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } *

variable_types:
entry
struct SEntry { int next; } *

variable_types: off int

user_defined_types: struct LfStack { union { struct { struct atomic_t head; struct atomic_t gen; }; struct atomic64_t _trx; long _val; }; }


decompiled_code:
__int64 __fastcall ext_id_by_ext(__int64 a1, __int64 a2)
{
return (a2 - a1) >> 21;
}

original_code:
static int
ext_id_by_ext(TdbAlloc *a, TdbExt *e)
{
 return (long)((char *)(e) - (char *)(a)) >> 21;
}

canonical_original_code:
static int ext_id_by_ext(struct TdbAlloc *a, struct TdbExt *e)
{
 return (long)((char *)(e) - (char *)(a)) >> 21;
}

variable_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t { int counter; } ext_shr; struct atomic_t ext_cur; struct LfStack { union { struct { struct atomic_t head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } ext_free; } *

variable_types: struct TdbExt { struct SEntry { int next; } stack; struct LfStack blk_free; } *

user_defined_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t ext_shr; struct atomic_t ext_cur; struct LfStack ext_free; }

user_defined_types: struct TdbExt { struct SEntry stack; struct LfStack blk_free; }


decompiled_code:
bool __fastcall ext_free(__int64 a1, volatile signed __int32 *a2, signed __int32 a3)
{
return lfs_push((volatile signed __int32 *)(a1 + 16), a2, a3);
}

canonical_decompiled_code:
bool __fastcall func6(__int64 a1, volatile signed __int32 *a2, signed __int32 a3)
{
return func4((volatile signed __int32 *)(a1 + 16), a2, a3);
}

original_code:
static void
ext_free(TdbAlloc *a, TdbExt *e, int eid)
{
 lfs_push(&a->ext_free, (SEntry *)e, eid);
}

canonical_original_code:
static void ext_free(struct TdbAlloc *a, struct TdbExt *e, int eid)
{
 lfs_push(&a->ext_free, (struct SEntry *)e, eid);
}

variable_types: eid int


decompiled_code:
char *__fastcall ext_by_id(_DWORD *a1, unsigned int a2)
{
char *result; // rax

result = (char *)&a1[0x80000 * (unsigned __int64)a2];
if ( !a2 )
result = (char *)a1 + ((*a1 + 4095) & 0xFFFFF000);
return result;
}

canonical_decompiled_code:
char *__fastcall func7(_DWORD *a1, unsigned int a2)
{
char *result; // rax

result = (char *)&a1[0x80000 * (unsigned __int64)a2];
if ( !a2 )
result = (char *)a1 + ((*a1 + 4095) & 0xFFFFF000);
return result;
}

original_code:
static TdbExt *
ext_by_id(TdbAlloc *a, uint32_t id)
{
 if (__builtin_expect(!!(!id), 0))
  return (TdbExt *)((char *)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1))));
 return (TdbExt *)((uint64_t)a + id * (1UL << 21));
}

canonical_original_code:
static struct TdbExt *ext_by_id(struct TdbAlloc *a, unsigned int id)
{
 if (__builtin_expect(!!(!id), 0))
  return (struct TdbExt *)((char *)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1))));
 return (struct TdbExt *)((unsigned long)a + id * (1UL << 21));
}

variable_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t ext_shr; struct atomic_t ext_cur; struct LfStack ext_free; } *

variable_types:
id
unsigned int

return_type: struct TdbExt { struct SEntry { int next; } stack; struct LfStack { union { struct { struct atomic_t { int counter; } head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } blk_free; } *


decompiled_code:
unsigned __int64 __fastcall tdb_blk_addr(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdi

result = a2 & 0xFFFFFFFFFFFFF000LL;
v3 = a1 + 12;
if ( v3 > (a2 & 0xFFFFFFFFFFFFF000LL) )
result = v3;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(__int64 a1, __int64 a2)
{
unsigned __int64 result; // rax
unsigned __int64 v3; // rdi

result = a2 & 0xFFFFFFFFFFFFF000LL;
v3 = a1 + 12;
if ( v3 > (a2 & 0xFFFFFFFFFFFFF000LL) )
result = v3;
return result;
}

original_code:
static uint64_t
tdb_blk_addr(TdbExt *e, uint64_t addr)
{
 uint64_t a = addr & (~(4096 - 1));
 if (__builtin_expect(!!((TdbExt *)a < e + 1), 0))
  return (uint64_t)(e + 1);
 return a;
}

canonical_original_code:
static unsigned long tdb_blk_addr(struct TdbExt *e, unsigned long addr)
{
 unsigned long a = addr & (~(4096 - 1));
 if (__builtin_expect(!!((struct TdbExt *)a < e + 1), 0))
  return (unsigned long)(e + 1);
 return a;
}

variable_types: struct TdbExt { struct SEntry { int next; } stack; struct LfStack { union { struct { struct atomic_t { int counter; } head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } blk_free; } *

variable_types:
addr
unsigned long

variable_types:
a
unsigned long

return_type: unsigned long


decompiled_code:
// attributes: thunk
unsigned __int64 __fastcall tdb_blk_ptr(__int64 a1, __int64 a2)
{
return tdb_blk_addr(a1, a2);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func9(__int64 a1, __int64 a2)
{
return func8(a1, a2);
}

original_code:
static SEntry *
tdb_blk_ptr(TdbExt *e, uint64_t addr)
{
 return (SEntry *)tdb_blk_addr(e, addr);
}

canonical_original_code:
static struct SEntry *tdb_blk_ptr(struct TdbExt *e, unsigned long addr)
{
 return (struct SEntry *)tdb_blk_addr(e, addr);
}

variable_types: struct TdbExt { struct SEntry stack; struct LfStack { union { struct { struct atomic_t { int counter; } head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } blk_free; } *

return_type: struct SEntry { int next; } *


decompiled_code:
bool __fastcall ext_init(int *a1, __int64 a2)
{
int v2; // ebx
volatile signed __int32 *v3; // r13
unsigned int v4; // eax
signed __int32 v5; // ebx
__int64 v6; // rbp
volatile signed __int32 *v7; // rax

if ( (unsigned int)ext_id_by_ext((__int64)a1, a2) )
{
lfs_entry_init((_DWORD *)a2);
v3 = (volatile signed __int32 *)(a2 + 4);
lfs_init((_DWORD *)(a2 + 4));
v5 = 2093056;
}
else
{
v2 = *a1;
lfs_entry_init((_DWORD *)a2);
v3 = (volatile signed __int32 *)(a2 + 4);
lfs_init((_DWORD *)(a2 + 4));
v4 = (v2 + 4095) & 0xFFFFF000;
v5 = 2093056 - v4;
if ( v4 == 2093056 )
return lfs_push(v3, (volatile signed __int32 *)(a2 + 12), 12);
}
v6 = a2 + v5 + 1;
do
{
v7 = (volatile signed __int32 *)tdb_blk_ptr(a2, v6);
lfs_push(v3, v7, v5);
v6 -= 4096LL;
v5 -= 4096;
}
while ( v5 );
return lfs_push(v3, (volatile signed __int32 *)(a2 + 12), 12);
}

canonical_decompiled_code:
bool __fastcall func10(int *a1, __int64 a2)
{
int v2; // ebx
volatile signed __int32 *v3; // r13
unsigned int v4; // eax
signed __int32 v5; // ebx
__int64 v6; // rbp
volatile signed __int32 *v7; // rax

if ( (unsigned int)func5((__int64)a1, a2) )
{
func2((_DWORD *)a2);
v3 = (volatile signed __int32 *)(a2 + 4);
func3((_DWORD *)(a2 + 4));
v5 = 2093056;
}
else
{
v2 = *a1;
func2((_DWORD *)a2);
v3 = (volatile signed __int32 *)(a2 + 4);
func3((_DWORD *)(a2 + 4));
v4 = (v2 + 4095) & 0xFFFFF000;
v5 = 2093056 - v4;
if ( v4 == 2093056 )
return func4(v3, (volatile signed __int32 *)(a2 + 12), 12);
}
v6 = a2 + v5 + 1;
do
{
v7 = (volatile signed __int32 *)func9(a2, v6);
func4(v3, v7, v5);
v6 -= 4096LL;
v5 -= 4096;
}
while ( v5 );
return func4(v3, (volatile signed __int32 *)(a2 + 12), 12);
}

original_code:
static void
ext_init(TdbAlloc *a, TdbExt *e)
{
 int o, e_off = ext_id_by_ext(a, e) ? 0 : a->hdr_reserved;
 SEntry *blk;
 lfs_entry_init(&e->stack);
 lfs_init(&e->blk_free);
 for (o = (1UL << 21) - 4096 - (((e_off) + 4096 - 1) & (~(4096 - 1))); o;
      o -= 4096)
 {
  uint64_t any_blk_addr = (uint64_t)e + o + 1;
  blk = tdb_blk_ptr(e, any_blk_addr);
  lfs_push(&e->blk_free, blk, o);
 }
 blk = (SEntry *)(e + 1);
 lfs_push(&e->blk_free, blk, sizeof(*e));
}

canonical_original_code:
static void ext_init(struct TdbAlloc *a, struct TdbExt *e)
{
 int o; int e_off = ext_id_by_ext(a, e) ? 0 : a->hdr_reserved;
 struct SEntry *blk;
 lfs_entry_init(&e->stack);
 lfs_init(&e->blk_free);
 for (o = (1UL << 21) - 4096 - (((e_off) + 4096 - 1) & (~(4096 - 1))); o;
      o -= 4096)
 {
  unsigned long any_blk_addr = (uint64_t)e + o + 1;
  blk = tdb_blk_ptr(e, any_blk_addr);
  lfs_push(&e->blk_free, blk, o);
 }
 blk = (struct SEntry *)(e + 1);
 lfs_push(&e->blk_free, blk, sizeof(*e));
}

variable_types: o int

variable_types:
blk
struct SEntry { int next; } *


decompiled_code:
unsigned __int64 __fastcall tdb_blk_ext_off(__int64 a1, __int64 a2)
{
return tdb_blk_addr(a1, a2) & 0x1FFFFF;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(__int64 a1, __int64 a2)
{
return func8(a1, a2) & 0x1FFFFF;
}

original_code:
static int
tdb_blk_ext_off(TdbExt *e, uint64_t addr)
{
 return tdb_blk_addr(e, addr) & ~(~((1UL << 21) - 1));
}

canonical_original_code:
static int tdb_blk_ext_off(struct TdbExt *e, unsigned long addr)
{
 return tdb_blk_addr(e, addr) & ~(~((1UL << 21) - 1));
}


decompiled_code:
__int64 __fastcall tdb_alloc_check_blk_ptr(int a1, __int64 *a2, __int64 a3)
{
__int64 v3; // rdx
__int64 result; // rax

if ( (a1 & 0xFFF) != 0 )
v3 = *a2 & ~a3;
else
v3 = *a2 | a3;
result = v3 | 8;
if ( (a1 & 0x1FFFFF) == 0 )
v3 |= 8uLL;
*a2 = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, __int64 *a2, __int64 a3)
{
__int64 v3; // rdx
__int64 result; // rax

if ( (a1 & 0xFFF) != 0 )
v3 = *a2 & ~a3;
else
v3 = *a2 | a3;
result = v3 | 8;
if ( (a1 & 0x1FFFFF) == 0 )
v3 |= 8uLL;
*a2 = v3;
return result;
}

original_code:
static void
tdb_alloc_check_blk_ptr(uint64_t new_val, uint64_t *state, uint64_t blk_f)
{
 if (__builtin_expect(!!(new_val & ~(~(4096 - 1))), 1))
  *state &= ~blk_f;
 else
  *state |= blk_f;
 if (__builtin_expect(!!(!(new_val & ~(~((1UL << 21) - 1)))), 0))
  *state |= 0x08;
}

canonical_original_code:
static void tdb_alloc_check_blk_ptr(unsigned long new_val, unsigned long *state, unsigned long blk_f)
{
 if (__builtin_expect(!!(new_val & ~(~(4096 - 1))), 1))
  *state &= ~blk_f;
 else
  *state |= blk_f;
 if (__builtin_expect(!!(!(new_val & ~(~((1UL << 21) - 1)))), 0))
  *state |= 0x08;
}

variable_types:
state
unsigned long *


decompiled_code:
unsigned __int64 __fastcall tdb_ext_ptr(_DWORD *a1, __int64 a2)
{
unsigned __int64 result; // rax

result = a2 & 0xFFFFFFFFFFE00000LL;
if ( (a2 & 0xFFFFFFFFFFE00000LL) < (unsigned __int64)a1 + ((*a1 + 4095) & 0xFFFFF000) )
result = (unsigned __int64)a1 + ((*a1 + 4095) & 0xFFFFF000);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func13(_DWORD *a1, __int64 a2)
{
unsigned __int64 result; // rax

result = a2 & 0xFFFFFFFFFFE00000LL;
if ( (a2 & 0xFFFFFFFFFFE00000LL) < (unsigned __int64)a1 + ((*a1 + 4095) & 0xFFFFF000) )
result = (unsigned __int64)a1 + ((*a1 + 4095) & 0xFFFFF000);
return result;
}

original_code:
static TdbExt *
tdb_ext_ptr(TdbAlloc *a, uint64_t addr)
{
 TdbExt *e = (TdbExt *)(addr & (~((1UL << 21) - 1)));
 if (__builtin_expect(!!((uint64_t)e < (uint64_t)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1)))), 0))
  e = (TdbExt *)((char *)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1))));
 return e;
}

canonical_original_code:
static struct TdbExt *tdb_ext_ptr(struct TdbAlloc *a, unsigned long addr)
{
 struct TdbExt *e = (TdbExt *)(addr & (~((1UL << 21) - 1)));
 if (__builtin_expect(!!((unsigned long)e < (unsigned long)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1)))), 0))
  e = (struct TdbExt *)((char *)a + (((a->hdr_reserved) + 4096 - 1) & (~(4096 - 1))));
 return e;
}


decompiled_code:
char *__fastcall ext_alloc_blk(__int64 a1, __int64 a2)
{
_DWORD *v2; // rax
char *v3; // rdx
bool v4; // zf
char *result; // rax

v2 = lfs_pop((volatile signed __int64 *)(a2 + 4), a2, 0);
v3 = (char *)v2 - a1;
v4 = v2 == 0LL;
result = 0LL;
if ( !v4 )
result = v3;
return result;
}

canonical_decompiled_code:
char *__fastcall func14(__int64 a1, __int64 a2)
{
_DWORD *v2; // rax
char *v3; // rdx
bool v4; // zf
char *result; // rax

v2 = lfs_pop((volatile signed __int64 *)(a2 + 4), a2, 0);
v3 = (char *)v2 - a1;
v4 = v2 == 0LL;
result = 0LL;
if ( !v4 )
result = v3;
return result;
}

original_code:
static uint64_t
ext_alloc_blk(TdbAlloc *a, TdbExt *e)
{
 SEntry *blk = lfs_pop(&e->blk_free, e, 0);
 return blk ? (long)((char *)(blk) - (char *)(a)) : 0;
}

canonical_original_code:
static unsigned long ext_alloc_blk(struct TdbAlloc *a, struct TdbExt *e)
{
 struct SEntry *blk = lfs_pop(&e->blk_free, e, 0);
 return blk ? (long)((char *)(blk) - (char *)(a)) : 0;
}


decompiled_code:
__int64 __fastcall atomic_fetch_inc(volatile signed __int32 *a1)
{
return _InterlockedExchangeAdd(a1, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func15(volatile signed __int32 *a1)
{
return _InterlockedExchangeAdd(a1, 1u);
}

original_code:
static inline long
atomic_fetch_inc(atomic_t *v)
{
 return __atomic_fetch_add(&v->counter, 1, 5);
}

canonical_original_code:
static inline long atomic_fetch_inc(struct atomic_t *v)
{
 return __atomic_fetch_add(&v->counter, 1, 5);
}

variable_types:
v
struct atomic_t { int counter; } *


decompiled_code:
char *__fastcall tdb_alloc_blk(__int64 a1, unsigned int a2, char a3, _BYTE *a4)
{
unsigned __int32 v4; // er14
char *i; // rsi
char *result; // rax
void *v10; // rsi
volatile signed __int32 *v11; // rax
volatile signed __int32 *v12; // r15
signed __int32 v13; // [rsp+4h] [rbp-34h]

if ( (*a4 & 8) != 0 )
goto LABEL_12;
if ( a2 == -1 )
goto LABEL_18;
for ( i = ext_by_id((_DWORD *)a1, a2); ; i = ext_by_id((_DWORD *)a1, v4) )
{
result = ext_alloc_blk(a1, (__int64)i);
if ( result )
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
if ( a2 == -1 && a3 )
return result;
LABEL_12:
v11 = (volatile signed __int32 *)ext_alloc(a1);
v12 = v11;
if ( !v11 )
return 0LL;
*(_QWORD *)a4 &= 0xFFFFFFFFFFFFFFF7LL;
if ( a2 != -1 )
{
result = ext_alloc_blk(a1, (__int64)v11);
if ( !result )
return result;
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
}
v13 = ext_id_by_ext(a1, (__int64)v11);
if ( (unsigned int)atomic_cmpxchg((volatile signed __int32 *)(a1 + 8), v4, v13) != v4 )
ext_free(a1, v12, v13);
LABEL_18:
if ( a3 )
break;
v4 = *(_DWORD *)(a1 + 8);
}
v10 = ext_alloc(a1);
if ( !v10 )
return 0LL;
result = ext_alloc_blk(a1, (__int64)v10);
if ( result )
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
return result;
}

canonical_decompiled_code:
char *__fastcall func18(__int64 a1, unsigned int a2, char a3, _BYTE *a4)
{
unsigned __int32 v4; // er14
char *i; // rsi
char *result; // rax
void *v10; // rsi
volatile signed __int32 *v11; // rax
volatile signed __int32 *v12; // r15
signed __int32 v13; // [rsp+4h] [rbp-34h]

if ( (*a4 & 8) != 0 )
goto LABEL_12;
if ( a2 == -1 )
goto LABEL_18;
for ( i = func7((_DWORD *)a1, a2); ; i = func7((_DWORD *)a1, v4) )
{
result = func14(a1, (__int64)i);
if ( result )
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
if ( a2 == -1 && a3 )
return result;
LABEL_12:
v11 = (volatile signed __int32 *)func17(a1);
v12 = v11;
if ( !v11 )
return 0LL;
*(_QWORD *)a4 &= 0xFFFFFFFFFFFFFFF7LL;
if ( a2 != -1 )
{
result = func14(a1, (__int64)v11);
if ( !result )
return result;
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
}
v13 = func5(a1, (__int64)v11);
if ( (unsigned int)func0((volatile signed __int32 *)(a1 + 8), v4, v13) != v4 )
func6(a1, v12, v13);
LABEL_18:
if ( a3 )
break;
v4 = *(_DWORD *)(a1 + 8);
}
v10 = func17(a1);
if ( !v10 )
return 0LL;
result = func14(a1, (__int64)v10);
if ( result )
return (char *)((unsigned __int64)(result + 63) & 0xFFFFFFFFFFFFFFC0LL);
return result;
}

original_code:
uint64_t
tdb_alloc_blk(TdbAlloc *a, int eid, _Bool new_ext, uint64_t *state)
{
 int new_eid, cur_eid;
 TdbExt *e;
 uint64_t o;
 if (__builtin_expect(!!(*state & 0x08), 0))
  goto alloc_new_extent;
retry:
 if (eid == (-1)) {
  if (__builtin_expect(!!(new_ext), 0)) {
   e = ext_alloc(a);
   if (__builtin_expect(!!(!e), 0))
    return 0;
  } else {
   cur_eid = (*(volatile int *)&(&a->ext_shr)->counter);
   e = ext_by_id(a, cur_eid);
  }
 } else {
  e = ext_by_id(a, eid);
 }
 if (__builtin_expect(!!((o = ext_alloc_blk(a, e))), 1))
  goto done;
 if (__builtin_expect(!!(eid == (-1) && new_ext), 0))
  return 0;
alloc_new_extent:
 if (!(e = ext_alloc(a)))
  return 0;
 *state &= ~0x08;
 if (eid != (-1)) {
  if (__builtin_expect(!!((o = ext_alloc_blk(a, e))), 1))
   goto done;
  return 0;
 }
 new_eid = ext_id_by_ext(a, e);
 if (atomic_cmpxchg(&a->ext_shr, cur_eid, new_eid) != cur_eid)
  ext_free(a, e, new_eid);
 goto retry;
done:
 ;
 return (((o) + 64 - 1) & ~(64 - 1));
}

canonical_original_code:
unsigned long tdb_alloc_blk(struct TdbAlloc *a, int eid, _Bool new_ext, unsigned long *state)
{
 int new_eid; int cur_eid;
 struct TdbExt *e;
 unsigned long o;
 if (__builtin_expect(!!(*state & 0x08), 0))
  goto alloc_new_extent;
retry:
 if (eid == (-1)) {
  if (__builtin_expect(!!(new_ext), 0)) {
   e = ext_alloc(a);
   if (__builtin_expect(!!(!e), 0))
    return 0;
  } else {
   cur_eid = (*(int *)&(&a->ext_shr)->counter);
   e = ext_by_id(a, cur_eid);
  }
 } else {
  e = ext_by_id(a, eid);
 }
 if (__builtin_expect(!!((o = ext_alloc_blk(a, e))), 1))
  goto done;
 if (__builtin_expect(!!(eid == (-1) && new_ext), 0))
  return 0;
alloc_new_extent:
 if (!(e = ext_alloc(a)))
  return 0;
 *state &= ~0x08;
 if (eid != (-1)) {
  if (__builtin_expect(!!((o = ext_alloc_blk(a, e))), 1))
   goto done;
  return 0;
 }
 new_eid = ext_id_by_ext(a, e);
 if (atomic_cmpxchg(&a->ext_shr, cur_eid, new_eid) != cur_eid)
  ext_free(a, e, new_eid);
 goto retry;
done:
 ;
 return (((o) + 64 - 1) & ~(64 - 1));
}

variable_types:
o
unsigned long


original_code:
uint64_t
tdb_alloc_data(TdbAlloc *a, size_t overhead, size_t *len, uint64_t *state,
        uint64_t *alloc_ptr, uint32_t align, _Bool large_alloc)
{
 uint64_t o, new_wcl;
 size_t res_len;
 _Bool curr_blk_empty;
 res_len = (((*len + overhead) + (7 + align)) & ~(7 + align));
 ;
 o = (((*alloc_ptr) + (7 + align)) & ~(7 + align));
 curr_blk_empty = *state & 0x04;
 if (curr_blk_empty || ((o + res_len) & (~(4096 - 1))) > ((o) & (~(4096 - 1)))) {
  size_t tail = 4096 - (o & ~(~(4096 - 1)));
  if (curr_blk_empty
      || (tail < (64 * 2) && res_len > tail + 4096))
  {
   int eid = large_alloc ? ((uint64_t)(o) >> 21) : (-1);
   if (!(o = tdb_alloc_blk(a, eid, 0, state))) {
    *len = 0;
    goto out;
   }
   o = (((o) + (7 + align)) & ~(7 + align));
   tail = 4096 - (o & ~(~(4096 - 1)));
  }
  if (res_len > tail) {
   res_len = tail;
   *len = res_len - overhead;
  }
 }
 new_wcl = o + res_len;
 *alloc_ptr = new_wcl;
 tdb_alloc_check_blk_ptr(*alloc_ptr, state, 0x04);
 ;
out:
 ;
 return o;
}

canonical_original_code:
unsigned long tdb_alloc_data(struct TdbAlloc *a, unsigned long overhead, unsigned long *len, unsigned long *state, unsigned long *alloc_ptr, unsigned int align, _Bool large_alloc)
{
 unsigned long o; unsigned long new_wcl;
 unsigned long res_len;
 _Bool curr_blk_empty;
 res_len = (((*len + overhead) + (7 + align)) & ~(7 + align));
 ;
 o = (((*alloc_ptr) + (7 + align)) & ~(7 + align));
 curr_blk_empty = *state & 0x04;
 if (curr_blk_empty || ((o + res_len) & (~(4096 - 1))) > ((o) & (~(4096 - 1)))) {
  unsigned long tail = 4096 - (o & ~(~(4096 - 1)));
  if (curr_blk_empty
      || (tail < (64 * 2) && res_len > tail + 4096))
  {
   int eid = large_alloc ? ((uint64_t)(o) >> 21) : (-1);
   if (!(o = tdb_alloc_blk(a, eid, 0, state))) {
    *len = 0;
    goto out;
   }
   o = (((o) + (7 + align)) & ~(7 + align));
   tail = 4096 - (o & ~(~(4096 - 1)));
  }
  if (res_len > tail) {
   res_len = tail;
   *len = res_len - overhead;
  }
 }
 new_wcl = o + res_len;
 *alloc_ptr = new_wcl;
 tdb_alloc_check_blk_ptr(*alloc_ptr, state, 0x04);
 ;
out:
 ;
 return o;
}

variable_types:
overhead
unsigned long

variable_types:
len
unsigned long *

variable_types:
align
unsigned int

variable_types:
tail
unsigned long


decompiled_code:
bool __fastcall tdb_free_blk(_DWORD *a1, __int64 a2)
{
volatile signed __int32 *v2; // rbx
volatile signed __int32 *v3; // r13
signed __int32 v4; // eax
bool result; // al
signed __int32 v6; // eax

v2 = (volatile signed __int32 *)tdb_ext_ptr(a1, a2);
v3 = (volatile signed __int32 *)tdb_blk_ptr((__int64)v2, a2);
v4 = tdb_blk_ext_off((__int64)v2, a2);
result = lfs_push(v2 + 1, v3, v4);
if ( result )
{
v6 = ext_id_by_ext((__int64)a1, (__int64)v2);
result = ext_free((__int64)a1, v2, v6);
}
return result;
}

canonical_decompiled_code:
bool __fastcall func21(_DWORD *a1, __int64 a2)
{
volatile signed __int32 *v2; // rbx
volatile signed __int32 *v3; // r13
signed __int32 v4; // eax
bool result; // al
signed __int32 v6; // eax

v2 = (volatile signed __int32 *)func13(a1, a2);
v3 = (volatile signed __int32 *)func9((__int64)v2, a2);
v4 = func11((__int64)v2, a2);
result = func4(v2 + 1, v3, v4);
if ( result )
{
v6 = func5((__int64)a1, (__int64)v2);
result = func6((__int64)a1, v2, v6);
}
return result;
}

original_code:
void
tdb_free_blk(TdbAlloc *a, uint64_t addr)
{
 TdbExt *e = tdb_ext_ptr(a, addr);
 SEntry *blk = tdb_blk_ptr(e, addr);
 int o = tdb_blk_ext_off(e, addr);
 int eid = ext_id_by_ext(a, e);
 if (lfs_push(&e->blk_free, blk, o))
  ext_free(a, e, eid);
}

canonical_original_code:
void tdb_free_blk(struct TdbAlloc *a, unsigned long addr)
{
 struct TdbExt *e = tdb_ext_ptr(a, addr);
 struct SEntry *blk = tdb_blk_ptr(e, addr);
 int o = tdb_blk_ext_off(e, addr);
 int eid = ext_id_by_ext(a, e);
 if (lfs_push(&e->blk_free, blk, o))
  ext_free(a, e, eid);
}

variable_types: struct TdbAlloc { unsigned int hdr_reserved; unsigned int ext_max; struct atomic_t { int counter; } ext_shr; struct atomic_t ext_cur; struct LfStack { union { struct { struct atomic_t head; struct atomic_t gen; }; struct atomic64_t { long counter; } _trx; long _val; }; } ext_free; } *


decompiled_code:
bool __fastcall tdb_alloc_init(__int64 a1, int a2, unsigned __int64 a3)
{
char *v3; // rax

*(_DWORD *)a1 = a2;
*(_QWORD *)(a1 + 4) = (unsigned int)(a3 >> 21) - 1;
*(_DWORD *)(a1 + 12) = 1;
lfs_init((_DWORD *)(a1 + 16));
v3 = ext_by_id((_DWORD *)a1, 0);
return ext_init((int *)a1, (__int64)v3);
}

original_code:
void
tdb_alloc_init(TdbAlloc *a, size_t hdr_sz, size_t db_sz)
{
 ((void) (0));
 a->hdr_reserved = hdr_sz;
 a->ext_max = (db_sz >> 21) - 1;
 ((&a->ext_shr)->counter = (0));
 ((&a->ext_cur)->counter = (1));
 lfs_init(&a->ext_free);
 ext_init(a, ext_by_id(a, 0));
}

canonical_original_code:
void tdb_alloc_init(struct TdbAlloc *a, unsigned long hdr_sz, unsigned long db_sz)
{
 ((void) (0));
 a->hdr_reserved = hdr_sz;
 a->ext_max = (db_sz >> 21) - 1;
 ((&a->ext_shr)->counter = (0));
 ((&a->ext_cur)->counter = (1));
 lfs_init(&a->ext_free);
 ext_init(a, ext_by_id(a, 0));
}


decompiled_code:
__int64 mapfile_size()
{
return 0x200000000LL;
}

original_code:
size_t
mapfile_size(void)
{
 return ((1UL << 21) * 4096);
}

canonical_original_code:
unsigned long mapfile_size()
{
 return ((1UL << 21) * 4096);
}


decompiled_code:
__int64 mapfile_raw_ptr()
{
__int64 v0; // rbx
void *v2; // rax
__int64 v3; // rbp

v0 = mem;
if ( !mem )
{
v2 = mmap((void *)0x600000200000LL, 0x200000000uLL, 3, 33, -1, 0LL);
v3 = (__int64)v2;
mem = (__int64)v2;
if ( v2 == (void *)-1LL )
{
perror("Dummy allocator: cannot allocate memory");
mem = 0LL;
}
else if ( mlock(v2, 0x200000000uLL) )
{
fprintf(stderr, "Dummy allocator: cannot lock memory. Please set ulimit -l %lu\n", 0x800000LL);
mem = 0LL;
}
else
{
v0 = v3;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbx
void *v2; // rax
__int64 v3; // rbp

v0 = mem;
if ( !mem )
{
v2 = mmap((void *)0x600000200000LL, 0x200000000uLL, 3, 33, -1, 0LL);
v3 = (__int64)v2;
mem = (__int64)v2;
if ( v2 == (void *)-1LL )
{
perror("Dummy allocator: cannot allocate memory");
mem = 0LL;
}
else if ( mlock(v2, 0x200000000uLL) )
{
fprintf(stderr, "Dummy allocator: cannot lock memory. Please set ulimit -l %lu\n", 0x800000LL);
mem = 0LL;
}
else
{
v0 = v3;
}
}
return v0;
}

original_code:
void *
mapfile_raw_ptr(void)
{
 int r;
 if (mem)
  return mem;
 mem = (char *)mmap(((void *)(0x600000000000UL + (1UL << 21))), ((1UL << 21) * 4096), 0x1 | 0x2,
      0x01 | 0x20, -1, 0);
 if (mem == ((void *) -1)) {
  perror("Dummy allocator: cannot allocate memory");
  mem = ((void *)0);
  return ((void *)0);
 }
 r = mlock(mem, ((1UL << 21) * 4096));
 if (r) {
  fprintf(stderr, "Dummy allocator: cannot lock memory."
    " Please set ulimit -l %lu\n", ((1UL << 21) * 4096) / 1024);
  mem = ((void *)0);
  return ((void *)0);
 }
 return mem;
}

canonical_original_code:
void *mapfile_raw_ptr()
{
 int r;
 if (mem)
  return mem;
 mem = (char *)mmap(((void *)(0x600000000000UL + (1UL << 21))), ((1UL << 21) * 4096), 0x1 | 0x2,
      0x01 | 0x20, -1, 0);
 if (mem == ((void *) -1)) {
  perror("Dummy allocator: cannot allocate memory");
  mem = ((void *)0);
  return ((void *)0);
 }
 r = mlock(mem, ((1UL << 21) * 4096));
 if (r) {
  fprintf(stderr, "Dummy allocator: cannot lock memory."
    " Please set ulimit -l %lu\n", ((1UL << 21) * 4096) / 1024);
  mem = ((void *)0);
  return ((void *)0);
 }
 return mem;
}

variable_types: r int

return_type: void *


repo: krkhan/bsg

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

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

original_code:
void print_hash_table(gpointer key, gpointer value, gpointer data)
{
  printf("%s\n", (char *) key);
}

canonical_original_code:
void print_hash_table(void *key, void *value, void *data)
{
  printf("%s\n", (char *) key);
}

variable_types:
key
void *

variable_types:
value
void *

variable_types:
data
void *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax

return result;
}

original_code:
int main(){
}

canonical_original_code:
int main(){
}


repo: kroody/tetris-free

original_code:
int write_data_to_buf(DataBuffer * netbuf, char * buf, int len)
{
    int size;
    if (len > netbuf->buf_size - netbuf->start - netbuf->len)
        len = netbuf->buf_size - netbuf->start - netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(netbuf->buf + netbuf->start + netbuf->len, buf, size);
    if (len > size)
        memcpy(netbuf->buf, buf + size, len - size);
    netbuf->len += len;
    return len;
}

canonical_original_code:
int write_data_to_buf(struct _DataBuffer *netbuf, char *buf, int len)
{
    int size;
    if (len > netbuf->buf_size - netbuf->start - netbuf->len)
        len = netbuf->buf_size - netbuf->start - netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(netbuf->buf + netbuf->start + netbuf->len, buf, size);
    if (len > size)
        memcpy(netbuf->buf, buf + size, len - size);
    netbuf->len += len;
    return len;
}

variable_types: struct _DataBuffer { int buf_size; int start; int len; char *buf; } *

variable_types:
buf
char *

variable_types: len int

variable_types: size int


decompiled_code:
__int64 __fastcall read_data_from_buf(__int64 a1, char *a2, int a3)
{
__int64 result; // rax
int v5; // ebp
int v6; // esi
int v7; // eax
int v8; // er13
__int64 v9; // r14
int v10; // eax

result = *(unsigned int *)(a1 + 8);
if ( (_DWORD)result )
{
if ( (int)result <= a3 )
a3 = *(_DWORD *)(a1 + 8);
v5 = a3;
v6 = *(_DWORD *)(a1 + 4);
v7 = v6 + result;
if ( v7 + a3 <= *(_DWORD *)a1 )
{
memcpy(a2, (const void *)(*(_QWORD *)(a1 + 16) + v6), a3);
v8 = v5;
}
else
{
v8 = *(_DWORD *)a1 - v7;
v9 = v8;
memcpy(a2, (const void *)(*(_QWORD *)(a1 + 16) + v6), v8);
if ( v5 > v8 )
{
*(_DWORD *)(a1 + 4) = 0;
v8 = v5 - v8;
memcpy(&a2[v9], *(const void **)(a1 + 16), v8);
}
}
v10 = *(_DWORD *)(a1 + 8) - v5;
*(_DWORD *)(a1 + 8) = v10;
if ( v10 )
*(_DWORD *)(a1 + 4) += v8;
else
*(_DWORD *)(a1 + 4) = 0;
result = (unsigned int)v5;
}
return result;
}

original_code:
int read_data_from_buf(DataBuffer * netbuf, char * buf, int len)
{
    int size;
    if (netbuf->len == 0)
        return 0;
    if (len > netbuf->len)
        len = netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(buf, netbuf->buf + netbuf->start, size);
    if (len > size) {
        netbuf->start = 0;
        memcpy(buf + size, netbuf->buf, len - size);
        size = len - size;
    }
    netbuf->len -= len;
    netbuf->start += size;
    if (netbuf->len == 0)
        netbuf->start = 0;
    return len;
}

canonical_original_code:
int read_data_from_buf(struct _DataBuffer *netbuf, char *buf, int len)
{
    int size;
    if (netbuf->len == 0)
        return 0;
    if (len > netbuf->len)
        len = netbuf->len;
    if (netbuf->start + netbuf->len + len > netbuf->buf_size)
        size = netbuf->buf_size - (netbuf->start + netbuf->len);
    else
        size = len;
    memcpy(buf, netbuf->buf + netbuf->start, size);
    if (len > size) {
        netbuf->start = 0;
        memcpy(buf + size, netbuf->buf, len - size);
        size = len - size;
    }
    netbuf->len -= len;
    netbuf->start += size;
    if (netbuf->len == 0)
        netbuf->start = 0;
    return len;
}

user_defined_types: struct _DataBuffer { int buf_size; int start; int len; char *buf; }


decompiled_code:
void __fastcall release_data_buf(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
free(a1);
}

original_code:
void release_data_buf(DataBuffer * buf)
{
    free(buf);
}

canonical_original_code:
void release_data_buf(struct _DataBuffer *buf)
{
    free(buf);
}


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

switch ( a1 )
{
case 2:
result = 4LL;
break;
case 3:
result = 2LL;
break;
case 4:
result = 40LL;
break;
case 5:
result = 0LL;
break;
case 6:
result = 0LL;
break;
case 7:
result = 0LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

switch ( a1 )
{
case 2:
result = 4LL;
break;
case 3:
result = 2LL;
break;
case 4:
result = 40LL;
break;
case 5:
result = 0LL;
break;
case 6:
result = 0LL;
break;
case 7:
result = 0LL;
break;
default:
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
static int msg_sizeof(int msg)
{
    int size = -1;
    switch (msg) {
    case 2:
        size = (sizeof(int));
    break;
    case 3:
        size = 2;
    break;
    case 4:
        size = (((12 + 7) / 8) * 20);
    break;
    case 5:
        size = 0;
    break;
    case 6:
        size = 0;
    break;
    case 7:
        size = 0;
    break;
    }
    return size;
}

variable_types: msg int


decompiled_code:
__int64 __fastcall bits_to_win_map(char *a1, __int64 a2)
{
__int64 v2; // r8
__int64 result; // rax

v2 = a2 + 240;
do
{
for ( result = 1LL; ; ++result )
{
*(_BYTE *)(a2 + result - 1) = (*a1 & 1) != 0;
*a1 >>= 1;
if ( result == 12 )
break;
if ( (result & 7) == 0 )
a1 += -((_DWORD)result == 0) + 1;
}
++a1;
a2 += 12LL;
}
while ( a2 != v2 );
return result;
}

canonical_original_code:
static void bits_to_win_map(char *buf, unsigned char map[20][12])
{
    char *t = buf;
    int x; int y;
    for (y = 0; y < 20; y++) {
        for (x = 0; x < 12; x++) {
            if (x % 8 == 0 && x != 0)
                t++;
            if (*t & 0x1)
                map[y][x] = 1;
            else
                map[y][x] = 0;
            *t >>= 1;
        }
        t++;
    }
}

variable_types:
map
unsigned char [20][12]

variable_types:
t
char *

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall get_msg_from_buf(__int64 a1, int *a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
unsigned int v7; // edx
int v8; // ebp
unsigned int v9; // edx

result = *(unsigned int *)(a1 + 8);
if ( (_DWORD)result )
{
v7 = read_data_from_buf(a1, a2, 4LL);
result = 0LL;
if ( v7 > 3 )
{
v8 = msg_sizeof(*a2);
if ( v8 < 0 )
{
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 4) = 0;
result = 0LL;
}
else
{
v9 = read_data_from_buf(a1, a3, 4LL);
result = 0LL;
if ( v9 > 3 )
{
result = 1LL;
if ( v8 > 0 )
result = (int)read_data_from_buf(a1, a4, (unsigned int)v8) >= v8;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int *a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
unsigned int v7; // edx
int v8; // ebp
unsigned int v9; // edx

result = *(unsigned int *)(a1 + 8);
if ( (_DWORD)result )
{
v7 = read_data_from_buf(a1, a2, 4LL);
result = 0LL;
if ( v7 > 3 )
{
v8 = func0(*a2);
if ( v8 < 0 )
{
*(_DWORD *)(a1 + 8) = 0;
*(_DWORD *)(a1 + 4) = 0;
result = 0LL;
}
else
{
v9 = read_data_from_buf(a1, a3, 4LL);
result = 0LL;
if ( v9 > 3 )
{
result = 1LL;
if ( v8 > 0 )
result = (int)read_data_from_buf(a1, a4, (unsigned int)v8) >= v8;
}
}
}
}
return result;
}

original_code:
static int get_msg_from_buf(DataBuffer * buf, int * msg, int * id, char * arg)
{
    int len;
    if ((buf->len == 0))
        return 0;
    if (read_data_from_buf(buf, (char *)msg, sizeof(int)) < sizeof(int))
        return 0;
    if ((len = msg_sizeof(*msg)) < 0) {
        { (buf)->len = 0; (buf)->start = 0; };
        return 0;
    }
    if (read_data_from_buf(buf, (char *)id, sizeof(int)) < sizeof(int))
        return 0;
    if (len > 0 && read_data_from_buf(buf, arg, len) < len)
  return 0;
    return 1;
}

canonical_original_code:
static int get_msg_from_buf(struct _DataBuffer *buf, int *msg, int *id, char *arg)
{
    int len;
    if ((buf->len == 0))
        return 0;
    if (read_data_from_buf(buf, (char *)msg, sizeof(int)) < sizeof(int))
        return 0;
    if ((len = msg_sizeof(*msg)) < 0) {
        { (buf)->len = 0; (buf)->start = 0; };
        return 0;
    }
    if (read_data_from_buf(buf, (char *)id, sizeof(int)) < sizeof(int))
        return 0;
    if (len > 0 && read_data_from_buf(buf, arg, len) < len)
  return 0;
    return 1;
}

variable_types:
msg
int *

variable_types:
id
int *

variable_types:
arg
char *