tcpipPortOpen

decompiled_code:
__int64 __fastcall tcpipPortOpen(void *src, int a2, _BYTE *a3)
{
int v4; // eax
void *v5; // rcx
const char *v6; // rdx
int v7; // eax
const char *v8; // rdx
int v9; // eax
int v10; // er12
in_addr_t v11; // eax
int v12; // eax
int *v13; // rax
int *v14; // r13
int v15; // eax
__int64 v16; // rdx
char *v18; // rax
int *v19; // rax
char *v20; // rax
char *v21; // rax
unsigned __int16 v22; // [rsp+2h] [rbp-E6h] BYREF
int v23; // [rsp+4h] [rbp-E4h] BYREF
socklen_t optlen; // [rsp+8h] [rbp-E0h] BYREF
int optval; // [rsp+Ch] [rbp-DCh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-D8h] BYREF
struct timeval timeout; // [rsp+20h] [rbp-C8h] BYREF
char cp[16]; // [rsp+30h] [rbp-B8h] BYREF
fd_set writefds; // [rsp+40h] [rbp-A8h] BYREF

*a3 = 0;
v4 = validateIpAddress((char *)src, 0LL, 0LL);
v5 = src;
v6 = "TCP/IP: device name '%s' does not appear to be IP address, skipping TCP/IP open attempt (note: this is normal if "
"opening a non-TCP/IP port)\n";
if ( v4 )
goto LABEL_11;
v22 = 4001;
v7 = parseIpAddress(src, cp, &v22);
v8 = "TCP/IP: IP address parse failed\n";
if ( v7 >= 0 )
{
if ( a2 == 460800 )
{
smDebug(-1LL, 1LL, "TCP/IP: Attempting to connect to %s:%d\n", cp, v22);
v9 = socket(2, 1, 6);
v10 = v9;
if ( v9 == -1 )
{
v19 = _errno_location();
v20 = strerror(*v19);
smDebug(-1LL, 1LL, "TCP/IP: Socket open failed (sys error: %s)\n", v20);
return 0LL;
}
optval = 1;
setsockopt(v9, 6, 1, &optval, 4u);
v11 = inet_addr(cp);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = v11;
*(_WORD *)addr.sa_data = _bswap_16(v22);
v12 = fcntl(v10, 3);
BYTE1(v12) |= 8u;
fcntl(v10, 4, v12);
if ( connect(v10, &addr, 0x10u) >= 0 )
{
LABEL_9:
v15 = fcntl(v10, 3, 0LL);
v16 = v15;
BYTE1(v16) = BYTE1(v15) & 0xF7;
fcntl(v10, 4, v16);
*a3 = 1;
return v10;
}
v13 = _errno_location();
v14 = v13;
if ( *v13 == 115 )
{
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[(unsigned int)v10 >> 6] |= 1LL << v10;
if ( select(v10 + 1, 0LL, &writefds, 0LL, &timeout) > 0 )
{
optlen = 4;
getsockopt(v10, 1, 4, &v23, &optlen);
if ( !v23 )
goto LABEL_9;
}
v21 = strerror(*v14);
smDebug(-1LL, 1LL, "TCP/IP: Setting socket properties failed (sys error: %s)\n", v21);
return 0LL;
}
v18 = strerror(*v13);
v6 = "TCP/IP: Connecting socket failed (sys error: %s)\n";
v5 = v18;
LABEL_11:
smDebug(-1LL, 1LL, v6, v5);
return 0LL;
}
v8 = "TCP/IP: Non-default baudrate not supported by TCP/IP protocol\n";
}
smDebug(-1LL, 1LL, v8);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *src, int a2, _BYTE *a3)
{
int v4; // eax
void *v5; // rcx
const char *v6; // rdx
int v7; // eax
const char *v8; // rdx
int v9; // eax
int v10; // er12
in_addr_t v11; // eax
int v12; // eax
int *v13; // rax
int *v14; // r13
int v15; // eax
__int64 v16; // rdx
char *v18; // rax
int *v19; // rax
char *v20; // rax
char *v21; // rax
unsigned __int16 v22; // [rsp+2h] [rbp-E6h] BYREF
int v23; // [rsp+4h] [rbp-E4h] BYREF
socklen_t optlen; // [rsp+8h] [rbp-E0h] BYREF
int optval; // [rsp+Ch] [rbp-DCh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-D8h] BYREF
struct timeval timeout; // [rsp+20h] [rbp-C8h] BYREF
char cp[16]; // [rsp+30h] [rbp-B8h] BYREF
fd_set writefds; // [rsp+40h] [rbp-A8h] BYREF

*a3 = 0;
v4 = func5((char *)src, 0LL, 0LL);
v5 = src;
v6 = "TCP/IP: device name '%s' does not appear to be IP address, skipping TCP/IP open attempt (note: this is normal if "
"opening a non-TCP/IP port)\n";
if ( v4 )
goto LABEL_11;
v22 = 4001;
v7 = func6(src, cp, &v22);
v8 = "TCP/IP: IP address parse failed\n";
if ( v7 >= 0 )
{
if ( a2 == 460800 )
{
smDebug(-1LL, 1LL, "TCP/IP: Attempting to connect to %s:%d\n", cp, v22);
v9 = socket(2, 1, 6);
v10 = v9;
if ( v9 == -1 )
{
v19 = _errno_location();
v20 = strerror(*v19);
smDebug(-1LL, 1LL, "TCP/IP: Socket open failed (sys error: %s)\n", v20);
return 0LL;
}
optval = 1;
setsockopt(v9, 6, 1, &optval, 4u);
v11 = inet_addr(cp);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = v11;
*(_WORD *)addr.sa_data = func0(v22);
v12 = fcntl(v10, 3);
BYTE1(v12) |= 8u;
fcntl(v10, 4, v12);
if ( connect(v10, &addr, 0x10u) >= 0 )
{
LABEL_9:
v15 = fcntl(v10, 3, 0LL);
v16 = v15;
BYTE1(v16) = BYTE1(v15) & 0xF7;
fcntl(v10, 4, v16);
*a3 = 1;
return v10;
}
v13 = _errno_location();
v14 = v13;
if ( *v13 == 115 )
{
timeout.tv_sec = 5LL;
timeout.tv_usec = 0LL;
memset(&writefds, 0, sizeof(writefds));
writefds.fds_bits[(unsigned int)v10 >> 6] |= 1LL << v10;
if ( select(v10 + 1, 0LL, &writefds, 0LL, &timeout) > 0 )
{
optlen = 4;
getsockopt(v10, 1, 4, &v23, &optlen);
if ( !v23 )
goto LABEL_9;
}
v21 = strerror(*v14);
smDebug(-1LL, 1LL, "TCP/IP: Setting socket properties failed (sys error: %s)\n", v21);
return 0LL;
}
v18 = strerror(*v13);
v6 = "TCP/IP: Connecting socket failed (sys error: %s)\n";
v5 = v18;
LABEL_11:
smDebug(-1LL, 1LL, v6, v5);
return 0LL;
}
v8 = "TCP/IP: Non-default baudrate not supported by TCP/IP protocol\n";
}
smDebug(-1LL, 1LL, v8);
return 0LL;
}

original_code:
smBusdevicePointer tcpipPortOpen(const char * devicename, smint32 baudrate_bps, smbool *success)
{
    int sockfd;
    struct sockaddr_in server;
    struct timeval tv;
    fd_set myset;
    int res, valopt;
    socklen_t lon;
    unsigned long arg;
    *success=0;
    if (validateIpAddress(devicename, ((void *)0), ((void *)0)) != 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: device name '%s' does not appear to be IP address, skipping TCP/IP open attempt (note: this is normal if opening a non-TCP/IP port)\n",devicename);
        return ((void *)0);
    }
    char ip_addr[16];
    unsigned short port = 4001;
    if (parseIpAddress(devicename, ip_addr, &port) < 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: IP address parse failed\n");
        return ((void *)0);
    }
    if(baudrate_bps!=460800)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Non-default baudrate not supported by TCP/IP protocol\n");
        return ((void *)0);
    }
    smDebug(-1,SMDebugLow,"TCP/IP: Attempting to connect to %s:%d\n",ip_addr,port);
    sockfd = socket(2 , SOCK_STREAM , IPPROTO_TCP);
    if (sockfd == -1)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Socket open failed (sys error: %s)\n",strerror((*__errno_location ())));
        return ((void *)0);
    }
    int one = 1;
    setsockopt(sockfd, IPPROTO_TCP, 1, (void *)&one, sizeof(one));
    server.sin_addr.s_addr = inet_addr(ip_addr);
    server.sin_family = 2;
    server.sin_port = htons(port);
    arg = fcntl(sockfd, 3, ((void *)0));
    arg |= 04000;
    fcntl(sockfd, 4, arg);
    res = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
    if (res < 0)
    {
        if ((*__errno_location ()) == 115)
        {
            tv.tv_sec = 5;
            tv.tv_usec = 0;
            do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&myset)->__fds_bits)[0]) : "memory"); } while (0);
            ((void) (((&myset)->__fds_bits)[(((unsigned int)sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << (((unsigned int)sockfd) % (8 * (int) sizeof (__fd_mask)))))));
            if (select(sockfd+1, ((void *)0), &myset, ((void *)0), &tv) > 0)
            {
                lon = sizeof(int);
                getsockopt(sockfd, 1, 4, (void*)(&valopt), &lon);
                if (valopt)
                {
                    smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed (sys error: %s)\n",strerror((*__errno_location ())));
                    return ((void *)0);
                }
            }
            else
            {
               smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed (sys error: %s)\n",strerror((*__errno_location ())));
               return ((void *)0);
            }
        }
        else
        {
            smDebug(-1,SMDebugLow,"TCP/IP: Connecting socket failed (sys error: %s)\n",strerror((*__errno_location ())));
            return ((void *)0);
        }
    }
    arg = fcntl(sockfd, 3, ((void *)0));
    arg &= (~04000);
    fcntl(sockfd, 4, arg);
    *success=1;
    return (smBusdevicePointer)sockfd;
}

canonical_original_code:
void *tcpipPortOpen(char *devicename, int baudrate_bps, char *success)
{
    int sockfd;
    struct sockaddr_in server;
    struct timeval tv;
    struct fd_set myset;
    int res; int valopt;
    unsigned int lon;
    unsigned long arg;
    *success=0;
    if (validateIpAddress(devicename, ((void *)0), ((void *)0)) != 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: device name '%s' does not appear to be IP address, skipping TCP/IP open attempt (note: this is normal if opening a non-TCP/IP port)\n",devicename);
        return ((void *)0);
    }
    char ip_addr[16];
    unsigned short port = 4001;
    if (parseIpAddress(devicename, ip_addr, &port) < 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: IP address parse failed\n");
        return ((void *)0);
    }
    if(baudrate_bps!=460800)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Non-default baudrate not supported by TCP/IP protocol\n");
        return ((void *)0);
    }
    smDebug(-1,SMDebugLow,"TCP/IP: Attempting to connect to %s:%d\n",ip_addr,port);
    sockfd = socket(2 , SOCK_STREAM , IPPROTO_TCP);
    if (sockfd == -1)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Socket open failed (sys error: %s)\n",strerror((*__errno_location ())));
        return ((void *)0);
    }
    int one = 1;
    setsockopt(sockfd, IPPROTO_TCP, 1, (void *)&one, sizeof(one));
    server.sin_addr.s_addr = inet_addr(ip_addr);
    server.sin_family = 2;
    server.sin_port = htons(port);
    arg = fcntl(sockfd, 3, ((void *)0));
    arg |= 04000;
    fcntl(sockfd, 4, arg);
    res = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
    if (res < 0)
    {
        if ((*__errno_location ()) == 115)
        {
            tv.tv_sec = 5;
            tv.tv_usec = 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" (&((&myset)->__fds_bits)[0]) : "memory"); } while (0);
            ((void) (((&myset)->__fds_bits)[(((unsigned int)sockfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << (((unsigned int)sockfd) % (8 * (int) sizeof (__fd_mask)))))));
            if (select(sockfd+1, ((void *)0), &myset, ((void *)0), &tv) > 0)
            {
                lon = sizeof(int);
                getsockopt(sockfd, 1, 4, (void *)(&valopt), &lon);
                if (valopt)
                {
                    smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed (sys error: %s)\n",strerror((*__errno_location ())));
                    return ((void *)0);
                }
            }
            else
            {
               smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed (sys error: %s)\n",strerror((*__errno_location ())));
               return ((void *)0);
            }
        }
        else
        {
            smDebug(-1,SMDebugLow,"TCP/IP: Connecting socket failed (sys error: %s)\n",strerror((*__errno_location ())));
            return ((void *)0);
        }
    }
    arg = fcntl(sockfd, 3, ((void *)0));
    arg &= (~04000);
    fcntl(sockfd, 4, arg);
    *success=1;
    return (void *)sockfd;
}

variable_types:
devicename
char *

variable_types:
success
char *

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

variable_types:
myset
struct fd_set { long __fds_bits[-1]; }

variable_types: res int

variable_types: valopt int

variable_types:
lon
unsigned int

variable_types:
arg
unsigned long

variable_types:
port
unsigned short

variable_types: one int

return_type: void *

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

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


serialPortOpen

original_code:
smBusdevicePointer serialPortOpen(const char * port_device_name, smint32 baudrate_bps, smbool *success)
{
    int port_handle;
    int err;
    int baudrateEnumValue;
    struct termios new_port_settings;
    int customBaudRate = 0;
    *success=0;
    if( strncmp(port_device_name,"/dev/tty",8) != 0 && strncmp(port_device_name,"/dev/cu.",8) != 0)
        return ((void *)0);
    port_handle = open(port_device_name, 02 | 0400 | 04000);
    if(port_handle==-1)
    {
        smDebug(-1, SMDebugLow, "Serial port error: port open failed");
        return ((void *)0);
    }
    if (ioctl(port_handle, 0x540C) == -1) {
        smDebug(-1, SMDebugLow, "Serial port error: error setting TIOCEXCL");
        close(port_handle);
        return ((void *)0);
    }
    if (fcntl(port_handle, 4, 0) == -1) {
        smDebug(-1, SMDebugLow, "Serial port error: error clearing O_NONBLOCK");
        close(port_handle);
        return ((void *)0);
    }
    switch(baudrate_bps)
    {
        case 9600 : baudrateEnumValue = 0000015; break;
        case 19200 : baudrateEnumValue = 0000016; break;
        case 38400 : baudrateEnumValue = 0000017; break;
        case 57600 : baudrateEnumValue = 0010001; break;
        case 115200 : baudrateEnumValue = 0010002; break;
        case 230400 : baudrateEnumValue = 0010003; break;
        case 460800 : baudrateEnumValue = 0010004; break;
        case 500000 : baudrateEnumValue = 0010005; break;
        case 576000 : baudrateEnumValue = 0010006; break;
        case 921600 : baudrateEnumValue = 0010007; break;
        case 1000000 : baudrateEnumValue = 0010010; break;
        case 1115200 : baudrateEnumValue = 0010011; break;
        case 1500000 : baudrateEnumValue = 0010012; break;
        case 2000000 : baudrateEnumValue = 0010013; break;
        case 2500000 : baudrateEnumValue = 0010014; break;
        case 3000000 : baudrateEnumValue = 0010015; break;
        case 3500000 : baudrateEnumValue = 0010016; break;
        case 4000000 : baudrateEnumValue = 0010017; break;
        default:
            customBaudRate = 1;
            baudrateEnumValue=0000015;
        break;
    }
    memset(&new_port_settings, 0, sizeof(new_port_settings));
    cfmakeraw(&new_port_settings);
    new_port_settings.c_cflag = 0000060 | 0004000 | 0000200;
    new_port_settings.c_iflag = 0000004;
    new_port_settings.c_oflag = 0;
    new_port_settings.c_lflag = 0;
    new_port_settings.c_cc[6] = 0;
    new_port_settings.c_cc[5] = readTimeoutMs/100;
    if(new_port_settings.c_cc[5]<1)
        new_port_settings.c_cc[5]=1;
    cfsetispeed(&new_port_settings, baudrateEnumValue);
    cfsetospeed(&new_port_settings, baudrateEnumValue);
    err = tcsetattr(port_handle, 0, &new_port_settings);
    if(err==-1)
    {
        close(port_handle);
        smDebug(-1, SMDebugLow, "Serial port error: failed to set port parameters");
        return ((void *)0);
    }
    if(customBaudRate)
    {
        smDebug(-1, SMDebugLow, "Serial port error: unsupported baudrate\n");
        close(port_handle);
        return ((void *)0);
    }
    struct serial_struct serial;
    if(ioctl(port_handle, 0x541E, &serial)!=-1)
    {
        serial.flags |= (1U << 13);
        if(ioctl(port_handle, 0x541F, &serial) == -1 )
        {
            smDebug(-1, SMDebugLow, "Serial port warning: unable to set low latency mode, maybe try running with root permissions.");
        }
    }
    else
        smDebug(-1, SMDebugLow, "Serial port warning: unable to read TIOCGSERIAL for low latency mode, maybe try running with root permissions.");
    usleep(100000);
    tcflush(port_handle,2);
    *success=1;
    return (smBusdevicePointer)port_handle;
}

canonical_original_code:
void *serialPortOpen(char *port_device_name, int baudrate_bps, char *success)
{
    int port_handle;
    int err;
    int baudrateEnumValue;
    struct termios new_port_settings;
    int customBaudRate = 0;
    *success=0;
    if( strncmp(port_device_name,"/dev/tty",8) != 0 && strncmp(port_device_name,"/dev/cu.",8) != 0)
        return ((void *)0);
    port_handle = open(port_device_name, 02 | 0400 | 04000);
    if(port_handle==-1)
    {
        smDebug(-1, SMDebugLow, "Serial port error: port open failed");
        return ((void *)0);
    }
    if (ioctl(port_handle, 0x540C) == -1) {
        smDebug(-1, SMDebugLow, "Serial port error: error setting TIOCEXCL");
        close(port_handle);
        return ((void *)0);
    }
    if (fcntl(port_handle, 4, 0) == -1) {
        smDebug(-1, SMDebugLow, "Serial port error: error clearing O_NONBLOCK");
        close(port_handle);
        return ((void *)0);
    }
    switch(baudrate_bps)
    {
        case 9600 : baudrateEnumValue = 0000015; break;
        case 19200 : baudrateEnumValue = 0000016; break;
        case 38400 : baudrateEnumValue = 0000017; break;
        case 57600 : baudrateEnumValue = 0010001; break;
        case 115200 : baudrateEnumValue = 0010002; break;
        case 230400 : baudrateEnumValue = 0010003; break;
        case 460800 : baudrateEnumValue = 0010004; break;
        case 500000 : baudrateEnumValue = 0010005; break;
        case 576000 : baudrateEnumValue = 0010006; break;
        case 921600 : baudrateEnumValue = 0010007; break;
        case 1000000 : baudrateEnumValue = 0010010; break;
        case 1115200 : baudrateEnumValue = 0010011; break;
        case 1500000 : baudrateEnumValue = 0010012; break;
        case 2000000 : baudrateEnumValue = 0010013; break;
        case 2500000 : baudrateEnumValue = 0010014; break;
        case 3000000 : baudrateEnumValue = 0010015; break;
        case 3500000 : baudrateEnumValue = 0010016; break;
        case 4000000 : baudrateEnumValue = 0010017; break;
        default:
            customBaudRate = 1;
            baudrateEnumValue=0000015;
        break;
    }
    memset(&new_port_settings, 0, sizeof(new_port_settings));
    cfmakeraw(&new_port_settings);
    new_port_settings.c_cflag = 0000060 | 0004000 | 0000200;
    new_port_settings.c_iflag = 0000004;
    new_port_settings.c_oflag = 0;
    new_port_settings.c_lflag = 0;
    new_port_settings.c_cc[6] = 0;
    new_port_settings.c_cc[5] = readTimeoutMs/100;
    if(new_port_settings.c_cc[5]<1)
        new_port_settings.c_cc[5]=1;
    cfsetispeed(&new_port_settings, baudrateEnumValue);
    cfsetospeed(&new_port_settings, baudrateEnumValue);
    err = tcsetattr(port_handle, 0, &new_port_settings);
    if(err==-1)
    {
        close(port_handle);
        smDebug(-1, SMDebugLow, "Serial port error: failed to set port parameters");
        return ((void *)0);
    }
    if(customBaudRate)
    {
        smDebug(-1, SMDebugLow, "Serial port error: unsupported baudrate\n");
        close(port_handle);
        return ((void *)0);
    }
    struct serial_struct serial;
    if(ioctl(port_handle, 0x541E, &serial)!=-1)
    {
        serial.flags |= (1U << 13);
        if(ioctl(port_handle, 0x541F, &serial) == -1 )
        {
            smDebug(-1, SMDebugLow, "Serial port warning: unable to set low latency mode, maybe try running with root permissions.");
        }
    }
    else
        smDebug(-1, SMDebugLow, "Serial port warning: unable to read TIOCGSERIAL for low latency mode, maybe try running with root permissions.");
    usleep(100000);
    tcflush(port_handle,2);
    *success=1;
    return (void *)port_handle;
}

variable_types: err int

variable_types: baudrateEnumValue int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types: customBaudRate int

variable_types: struct serial_struct { int type; int line; unsigned int port; int irq; int flags; int xmit_fifo_size; int custom_divisor; int baud_base; unsigned short close_delay; char io_type; char reserved_char[1]; int hub6; unsigned short closing_wait; unsigned short closing_wait2; unsigned char *iomem_base; unsigned short iomem_reg_shift; unsigned int port_high; unsigned long iomap_base; }


serialPortRead

decompiled_code:
ssize_t __fastcall serialPortRead(int a1, void *a2, int a3)
{
if ( a3 > 4096 )
a3 = 4096;
return read(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func1(int a1, void *a2, int a3)
{
if ( a3 > 4096 )
a3 = 4096;
return read(a1, a2, a3);
}

original_code:
smint32 serialPortRead(smBusdevicePointer busdevicePointer, smuint8 *buf, smint32 size)
{
    int serialport_handle=(int)busdevicePointer;
    smint32 n;
    if(size>4096) size = 4096;
    n = read((int)serialport_handle, buf, size);
    return n;
}

canonical_original_code:
int serialPortRead(void *busdevicePointer, unsigned char *buf, int size)
{
    int serialport_handle = (int)busdevicePointer;
    int n;
    if(size>4096) size = 4096;
    n = read((int)serialport_handle, buf, size);
    return n;
}

variable_types:
busdevicePointer
void *

variable_types:
buf
unsigned char *

variable_types: size int

variable_types: n int

return_type: int


serialPortWrite

decompiled_code:
ssize_t __fastcall serialPortWrite(int a1, const void *a2, int a3)
{
return write(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func2(int a1, const void *a2, int a3)
{
return write(a1, a2, a3);
}

original_code:
smint32 serialPortWrite(smBusdevicePointer busdevicePointer, unsigned char *buf, smint32 size)
{
    int serialport_handle=(int)busdevicePointer;
    return(write((int)serialport_handle, buf, size));
}

canonical_original_code:
int serialPortWrite(void *busdevicePointer, unsigned char *buf, int size)
{
    int serialport_handle = (int)busdevicePointer;
    return(write((int)serialport_handle, buf, size));
}


serialPortMiscOperation

decompiled_code:
__int64 __fastcall serialPortMiscOperation(int fd, int a2)
{
__int64 result; // rax

if ( a2 )
{
if ( a2 == 1 )
{
usleep(0x4E20u);
tcflush(fd, 0);
result = 1LL;
}
else
{
smDebug(-1LL, 1LL, "Serial port error: given MiscOperataion not implemented\n");
result = 0LL;
}
}
else
{
usleep(0x4E20u);
tcdrain(fd);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, int a2)
{
__int64 result; // rax

if ( a2 )
{
if ( a2 == 1 )
{
usleep(0x4E20u);
tcflush(fd, 0);
result = 1LL;
}
else
{
smDebug(-1LL, 1LL, "Serial port error: given MiscOperataion not implemented\n");
result = 0LL;
}
}
else
{
usleep(0x4E20u);
tcdrain(fd);
result = 1LL;
}
return result;
}

original_code:
smbool serialPortMiscOperation(smBusdevicePointer busdevicePointer, BusDeviceMiscOperationType operation)
{
    int serialport_handle=(int)busdevicePointer;
    switch(operation)
    {
    case MiscOperationPurgeRX:
        usleep(20000);
        tcflush(serialport_handle,0);
        return 1;
        break;
    case MiscOperationFlushTX:
        usleep(20000);
        tcdrain(serialport_handle);
        return 1;
        break;
    default:
        smDebug( -1, SMDebugLow, "Serial port error: given MiscOperataion not implemented\n");
        return 0;
        break;
    }
}

canonical_original_code:
char serialPortMiscOperation(void *busdevicePointer, enum _BusDeviceMiscOperationType operation)
{
    int serialport_handle = (int)busdevicePointer;
    switch(operation)
    {
    case MiscOperationPurgeRX:
        usleep(20000);
        tcflush(serialport_handle,0);
        return 1;
        break;
    case MiscOperationFlushTX:
        usleep(20000);
        tcdrain(serialport_handle);
        return 1;
        break;
    default:
        smDebug( -1, SMDebugLow, "Serial port error: given MiscOperataion not implemented\n");
        return 0;
        break;
    }
}

variable_types: enum _BusDeviceMiscOperationType { MiscOperationFlushTX=0, MiscOperationPurgeRX=1 }

return_type: char


serialPortClose

decompiled_code:
// attributes: thunk
int serialPortClose(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func4(int fd)
{
return close(fd);
}

original_code:
void serialPortClose(smBusdevicePointer busdevicePointer)
{
    int serialport_handle=(int)busdevicePointer;
    close((int)serialport_handle);
}

canonical_original_code:
void serialPortClose(void *busdevicePointer)
{
    int serialport_handle = (int)busdevicePointer;
    close((int)serialport_handle);
}

return_type: void


GeneralProtectionFault

repo: GreatEmerald/Arcomage-Clone

decompiled_code:
void __noreturn GeneralProtectionFault(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf(format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(3);
}

canonical_decompiled_code:
void __noreturn func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
printf(format);
vfprintf(stderr, format, arg);
fputc(10, stderr);
exit(3);
}

original_code:
void GeneralProtectionFault(char *fmt,...)
{
    va_list args;
    __builtin_va_start(args,fmt);
    printf(fmt);
    vfprintf(stderr,fmt,args);
    fprintf(stderr,"\n");
    __builtin_va_end(args);
    exit(3);
}

canonical_original_code:
void GeneralProtectionFault(char *fmt, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,fmt);
    printf(fmt);
    vfprintf(stderr,fmt,args);
    fprintf(stderr,"\n");
    __builtin_va_end(args);
    exit(3);
}

variable_types:
fmt
char *


Init

decompiled_code:
__int64 Init()
{
rt_init();
InitArcomage();
Graphics_Init();
InitTTF();
SetPlayCardAnimation(&PlayCardAnimation);
SetPlayCardPostAnimation(&PlayCardPostAnimation);
return initGame();
}

canonical_decompiled_code:
__int64 func1()
{
rt_init();
InitArcomage();
Graphics_Init();
InitTTF();
SetPlayCardAnimation(&PlayCardAnimation);
SetPlayCardPostAnimation(&PlayCardPostAnimation);
return initGame();
}

original_code:
void Init()
{
    rt_init();
    InitArcomage();
    Graphics_Init();
    InitTTF();
    SetPlayCardAnimation(&PlayCardAnimation);
    SetPlayCardPostAnimation(&PlayCardPostAnimation);
    initGame();
}


Quit

decompiled_code:
__int64 Quit()
{
QuitTTF();
Graphics_Quit();
return rt_term();
}

canonical_decompiled_code:
__int64 func2()
{
QuitTTF();
Graphics_Quit();
return rt_term();
}

original_code:
void Quit()
{
    QuitTTF();
    Graphics_Quit();
    rt_term();
}


main

canonical_name: main

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

Init(argc, argv, envp);
if ( !(unsigned int)Menu() )
{
SetPlayerInfo((unsigned int)Turn, "Player", 0LL);
v4 = GetEnemy();
SetPlayerInfo(v4, "AI", 1LL);
PrecachePlayerNames();
DoGame();
}
Quit();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // eax

func1(argc, argv, envp);
if ( !(unsigned int)Menu() )
{
SetPlayerInfo((unsigned int)Turn, "Player", 0LL);
v4 = GetEnemy();
SetPlayerInfo(v4, "AI", 1LL);
PrecachePlayerNames();
DoGame();
}
func2();
return 0;
}

original_code:
int main(int argc,char *argv[])
{
    int MenuAction;
    int i, n;
    SizeF TestLocation={0.0, 0.0};
    Init();
    MenuAction = Menu();
    switch (MenuAction)
    {
        case START:
            SetPlayerInfo(Turn, "Player", 0);
            SetPlayerInfo(GetEnemy(), "AI", 1);
            PrecachePlayerNames();
            DoGame();
        default: break;
    }
    Quit();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int MenuAction;
    int i; int n;
    struct S_Range TestLocation = {0.0, 0.0};
    Init();
    MenuAction = Menu();
    switch (MenuAction)
    {
        case START:
            SetPlayerInfo(Turn, "Player", 0);
            SetPlayerInfo(GetEnemy(), "AI", 1);
            PrecachePlayerNames();
            DoGame();
        default: break;
    }
    Quit();
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: MenuAction int

variable_types: i int

variable_types:
TestLocation
struct S_Range { float X; float Y; }


WaitForKey

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

if ( a1 )
{
do
{
do
{
SDL_PollEvent(event);
result = SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 2 );
}
while ( event[2] != a1 );
}
else
{
do
{
SDL_PollEvent(event);
result = SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 3 );
}
return result;
}

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

if ( a1 )
{
do
{
do
{
SDL_PollEvent(event);
result = SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 2 );
}
while ( event[2] != a1 );
}
else
{
do
{
SDL_PollEvent(event);
result = SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 3 );
}
return result;
}

original_code:
void WaitForKey(int sym)
{
 if (!sym)
 {
  do
   {
    SDL_PollEvent(&event);
    SDL_Delay(CPUWAIT);
   } while (event.type!=SDL_KEYUP);
 } else {
  do
  {
   SDL_PollEvent(&event);
   SDL_Delay(CPUWAIT);
  } while (event.type!=SDL_KEYDOWN || event.key.keysym.sym!=sym);
 }
}

variable_types: sym int


WaitForInput

decompiled_code:
__int64 WaitForInput()
{
SDL_PumpEvents();
do
{
SDL_PollEvent(event);
SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 3 && (event[0] & 0xFF00FF) != 65542 );
return SDL_PumpEvents();
}

canonical_decompiled_code:
__int64 func1()
{
SDL_PumpEvents();
do
{
SDL_PollEvent(event);
SDL_Delay(0LL);
}
while ( LOBYTE(event[0]) != 3 && (event[0] & 0xFF00FF) != 65542 );
return SDL_PumpEvents();
}

original_code:
void WaitForInput()
{
 SDL_PumpEvents();
 do {
  SDL_PollEvent(&event);
  SDL_Delay(CPUWAIT);
 } while (!((event.type==SDL_KEYUP)||((event.type==SDL_MOUSEBUTTONUP)&&(event.button.button==1))));
 SDL_PumpEvents();
}


DoGame

decompiled_code:
__int64 DoGame()
{
int v0; // ebx
__int64 result; // rax
unsigned __int8 v2; // r12
__int64 v3; // rbx
unsigned int v4; // ebp
float *v5; // rax
float v6; // xmm1_4
float v7; // xmm6_4
float v8; // xmm5_4
float v9; // xmm4_4
const char *v10; // rax
int v11; // ebx
_BOOL8 v12; // rdi
float v13; // [rsp+0h] [rbp-28h]
float v14; // [rsp+0h] [rbp-28h]
float v15; // [rsp+8h] [rbp-20h]
float v16; // [rsp+Ch] [rbp-1Ch]

if ( !(unsigned __int8)IsVictorious(0LL) )
{
while ( !(unsigned __int8)IsVictorious(1LL) )
{
DrawScene();
UpdateScreen();
while ( (unsigned int)SDL_PollEvent(event) )
;
if ( (unsigned __int8)GetIsAI((unsigned int)Turn) )
{
SDL_Delay(500LL);
AIPlay();
}
else
{
while ( !(unsigned int)SDL_PollEvent(event) )
SDL_Delay(0LL);
result = LOBYTE(event[0]);
if ( LOBYTE(event[0]) == 3 )
{
if ( event[2] == 27 )
return result;
}
else if ( LOBYTE(event[0]) == 6 )
{
v2 = BYTE2(event[0]);
v3 = 0LL;
if ( v2 <= 3u )
{
while ( 1 )
{
v4 = v3;
if ( (int)GetConfig(3LL) <= (int)v3 )
break;
v5 = (float *)(*(_QWORD *)(CardLocations + 8LL * Turn) + 8 * v3++);
v16 = v5[1];
v15 = *v5;
v13 = (float)HIWORD(event[1]);
v6 = v13 / (float)(int)GetConfig(19LL);
v14 = (float)LOWORD(event[1]);
v7 = (float)(int)GetConfig(18LL);
v8 = v16 + 0.2133333333333333;
v9 = v15 + 0.1175;
if ( FInRect(v14 / v7, v6, v15, v16, v9, v8) != 0.0 )
{
ExecutePlayCard(v4, (unsigned __int8)(v2 - 2) <= 1u);
break;
}
}
}
}
}
SDL_Delay(0LL);
if ( (unsigned __int8)IsVictorious(0LL) )
break;
}
}
DrawScene();
if ( (unsigned __int8)IsVictorious(0LL) && (unsigned __int8)IsVictorious(1LL) )
{
DrawDialog(4LL, "Draw!");
}
else if ( (unsigned __int8)GetIsAI(1LL) )
{
if ( (unsigned __int8)IsVictorious(0LL) )
{
v0 = GetResource(0LL, 6LL);
if ( v0 < (int)GetConfig(14LL) )
{
if ( (int)GetResource(1LL, 6LL) <= 0 )
DrawDialog(4LL, "You win by a tower\ndestruction victory!");
else
DrawDialog(4LL, "You win by a\nresource victory!");
}
else
{
DrawDialog(4LL, "You win by a\ntower building victory!");
}
}
else
{
v11 = GetResource(1LL, 6LL);
if ( v11 < (int)GetConfig(14LL) )
{
if ( (int)GetResource(0LL, 6LL) <= 0 )
DrawDialog(5LL, "You lose by a\ntower destruction defeat!");
else
DrawDialog(5LL, "You lose by a\nresource defeat!");
}
else
{
DrawDialog(5LL, "You lose by a\ntower building defeat!");
}
}
}
else
{
v12 = (unsigned __int8)IsVictorious(0LL) == 0;
v10 = (const char *)GetPlayerName(v12);
DrawDialog(4LL, "Winner is\n%s !", v10);
}
UpdateScreen();
SDL_Delay(1000LL);
while ( (unsigned int)SDL_PollEvent(event) )
;
return WaitForInput();
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // ebx
__int64 result; // rax
unsigned __int8 v2; // r12
__int64 v3; // rbx
unsigned int v4; // ebp
float *v5; // rax
float v6; // xmm1_4
float v7; // xmm6_4
float v8; // xmm5_4
float v9; // xmm4_4
const char *v10; // rax
int v11; // ebx
_BOOL8 v12; // rdi
float v13; // [rsp+0h] [rbp-28h]
float v14; // [rsp+0h] [rbp-28h]
float v15; // [rsp+8h] [rbp-20h]
float v16; // [rsp+Ch] [rbp-1Ch]

if ( !(unsigned __int8)IsVictorious(0LL) )
{
while ( !(unsigned __int8)IsVictorious(1LL) )
{
DrawScene();
UpdateScreen();
while ( (unsigned int)SDL_PollEvent(event) )
;
if ( (unsigned __int8)GetIsAI((unsigned int)Turn) )
{
SDL_Delay(500LL);
AIPlay();
}
else
{
while ( !(unsigned int)SDL_PollEvent(event) )
SDL_Delay(0LL);
result = LOBYTE(event[0]);
if ( LOBYTE(event[0]) == 3 )
{
if ( event[2] == 27 )
return result;
}
else if ( LOBYTE(event[0]) == 6 )
{
v2 = BYTE2(event[0]);
v3 = 0LL;
if ( v2 <= 3u )
{
while ( 1 )
{
v4 = v3;
if ( (int)GetConfig(3LL) <= (int)v3 )
break;
v5 = (float *)(*(_QWORD *)(CardLocations + 8LL * Turn) + 8 * v3++);
v16 = v5[1];
v15 = *v5;
v13 = (float)HIWORD(event[1]);
v6 = v13 / (float)(int)GetConfig(19LL);
v14 = (float)LOWORD(event[1]);
v7 = (float)(int)GetConfig(18LL);
v8 = v16 + 0.2133333333333333;
v9 = v15 + 0.1175;
if ( FInRect(v14 / v7, v6, v15, v16, v9, v8) != 0.0 )
{
ExecutePlayCard(v4, (unsigned __int8)(v2 - 2) <= 1u);
break;
}
}
}
}
}
SDL_Delay(0LL);
if ( (unsigned __int8)IsVictorious(0LL) )
break;
}
}
DrawScene();
if ( (unsigned __int8)IsVictorious(0LL) && (unsigned __int8)IsVictorious(1LL) )
{
DrawDialog(4LL, "Draw!");
}
else if ( (unsigned __int8)GetIsAI(1LL) )
{
if ( (unsigned __int8)IsVictorious(0LL) )
{
v0 = GetResource(0LL, 6LL);
if ( v0 < (int)GetConfig(14LL) )
{
if ( (int)GetResource(1LL, 6LL) <= 0 )
DrawDialog(4LL, "You win by a tower\ndestruction victory!");
else
DrawDialog(4LL, "You win by a\nresource victory!");
}
else
{
DrawDialog(4LL, "You win by a\ntower building victory!");
}
}
else
{
v11 = GetResource(1LL, 6LL);
if ( v11 < (int)GetConfig(14LL) )
{
if ( (int)GetResource(0LL, 6LL) <= 0 )
DrawDialog(5LL, "You lose by a\ntower destruction defeat!");
else
DrawDialog(5LL, "You lose by a\nresource defeat!");
}
else
{
DrawDialog(5LL, "You lose by a\ntower building defeat!");
}
}
}
else
{
v12 = (unsigned __int8)IsVictorious(0LL) == 0;
v10 = (const char *)GetPlayerName(v12);
DrawDialog(4LL, "Winner is\n%s !", v10);
}
UpdateScreen();
SDL_Delay(1000LL);
while ( (unsigned int)SDL_PollEvent(event) )
;
return func1();
}

original_code:
void DoGame()
{
    int i, n;
    int crd,netcard;
    char bDiscarded=0, bAllowedToPlay=0;
    while (!IsVictorious(0) && !IsVictorious(1))
    {
        DrawScene();
        UpdateScreen();
        while (SDL_PollEvent(&event));
        if (GetIsAI(Turn))
        {
            SDL_Delay(500);
            AIPlay();
        }
        else
        {
            while (!SDL_PollEvent(&event))
                SDL_Delay(0);
            if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
                return;
            if (event.type != SDL_MOUSEBUTTONUP || event.button.button > 3)
            {
                SDL_Delay(0);
                continue;
            }
            bDiscarded = (event.button.button == 2) || (event.button.button == 3);
            for (i=0; i<GetConfig(CardsInHand); i++)
            {
                if (FInRect((float)event.button.x/GetConfig(ResolutionX), (float)event.button.y/GetConfig(ResolutionY),
                    CardLocations[Turn][i].X, CardLocations[Turn][i].Y,
                    CardLocations[Turn][i].X+94/800.0, CardLocations[Turn][i].Y+128/600.0)
                                                               )
                {
                    crd=i;
                    bAllowedToPlay = 1;
                    break;
                }
            }
            if (bAllowedToPlay)
            {
                ExecutePlayCard(crd, bDiscarded);
                bAllowedToPlay = 0;
            }
        }
        SDL_Delay(CPUWAIT);
    }
    DrawScene();
    if (IsVictorious(0) && IsVictorious(1))
    {
        DrawDialog(DLGWINNER,"Draw!");
    }
    else
    {
        if (GetIsAI(1))
        {
            if (IsVictorious(0))
            {
                if (GetResource(0, RT_Tower) >= GetConfig(TowerVictory))
                    DrawDialog(DLGWINNER, "You win by a\ntower building victory!");
                else if (GetResource(1, RT_Tower) <= 0)
                    DrawDialog(DLGWINNER, "You win by a tower\ndestruction victory!");
                else DrawDialog(DLGWINNER, "You win by a\nresource victory!");
            }
            else
            {
                if (GetResource(1, RT_Tower) >= GetConfig(TowerVictory))
                    DrawDialog(DLGLOOSER, "You lose by a\ntower building defeat!");
                else if (GetResource(0, RT_Tower) <= 0)
                    DrawDialog(DLGLOOSER, "You lose by a\ntower destruction defeat!");
                else DrawDialog(DLGLOOSER, "You lose by a\nresource defeat!");
            }
        } else {
            if (IsVictorious(0))
                DrawDialog(DLGWINNER,"Winner is\n%s !", GetPlayerName(0));
            else
                DrawDialog(DLGWINNER,"Winner is\n%s !", GetPlayerName(1));
        }
    }
    UpdateScreen();
    SDL_Delay(1000);
    while (SDL_PollEvent(&event));
    WaitForInput();
}

canonical_original_code:
void DoGame()
{
    int i; int n;
    int crd; int netcard;
    char bDiscarded = 0; char bAllowedToPlay = 0;
    while (!IsVictorious(0) && !IsVictorious(1))
    {
        DrawScene();
        UpdateScreen();
        while (SDL_PollEvent(&event));
        if (GetIsAI(Turn))
        {
            SDL_Delay(500);
            AIPlay();
        }
        else
        {
            while (!SDL_PollEvent(&event))
                SDL_Delay(0);
            if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
                return;
            if (event.type != SDL_MOUSEBUTTONUP || event.button.button > 3)
            {
                SDL_Delay(0);
                continue;
            }
            bDiscarded = (event.button.button == 2) || (event.button.button == 3);
            for (i=0; i<GetConfig(CardsInHand); i++)
            {
                if (FInRect((float)event.button.x/GetConfig(ResolutionX), (float)event.button.y/GetConfig(ResolutionY),
                    CardLocations[Turn][i].X, CardLocations[Turn][i].Y,
                    CardLocations[Turn][i].X+94/800.0, CardLocations[Turn][i].Y+128/600.0)
                                                               )
                {
                    crd=i;
                    bAllowedToPlay = 1;
                    break;
                }
            }
            if (bAllowedToPlay)
            {
                ExecutePlayCard(crd, bDiscarded);
                bAllowedToPlay = 0;
            }
        }
        SDL_Delay(CPUWAIT);
    }
    DrawScene();
    if (IsVictorious(0) && IsVictorious(1))
    {
        DrawDialog(DLGWINNER,"Draw!");
    }
    else
    {
        if (GetIsAI(1))
        {
            if (IsVictorious(0))
            {
                if (GetResource(0, RT_Tower) >= GetConfig(TowerVictory))
                    DrawDialog(DLGWINNER, "You win by a\ntower building victory!");
                else if (GetResource(1, RT_Tower) <= 0)
                    DrawDialog(DLGWINNER, "You win by a tower\ndestruction victory!");
                else DrawDialog(DLGWINNER, "You win by a\nresource victory!");
            }
            else
            {
                if (GetResource(1, RT_Tower) >= GetConfig(TowerVictory))
                    DrawDialog(DLGLOOSER, "You lose by a\ntower building defeat!");
                else if (GetResource(0, RT_Tower) <= 0)
                    DrawDialog(DLGLOOSER, "You lose by a\ntower destruction defeat!");
                else DrawDialog(DLGLOOSER, "You lose by a\nresource defeat!");
            }
        } else {
            if (IsVictorious(0))
                DrawDialog(DLGWINNER,"Winner is\n%s !", GetPlayerName(0));
            else
                DrawDialog(DLGWINNER,"Winner is\n%s !", GetPlayerName(1));
        }
    }
    UpdateScreen();
    SDL_Delay(1000);
    while (SDL_PollEvent(&event));
    WaitForInput();
}

variable_types: crd int

variable_types: netcard int

variable_types: bDiscarded char

variable_types: bAllowedToPlay char


repo: Greeshma/DFS

decompiled_code:
char *attr_cache_init()
{
char *result; // rax

attr_cache_last_stored = 0;
result = (char *)(unsigned int)((_DWORD)attr_cache + 128);
attr_cache_initialized = 1;
do
{
*result = 0;
result += 273;
}
while ( result != &attr_cache[17600] );
return result;
}

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

attr_cache_last_stored = 0;
result = (char *)(unsigned int)((_DWORD)attr_cache + 128);
attr_cache_initialized = 1;
do
{
*result = 0;
result += 273;
}
while ( result != &attr_cache[17600] );
return result;
}

original_code:
void attr_cache_init()
{
    int ii;
    attr_cache_last_stored=0;
    attr_cache_initialized=1;
    for (ii=0; ii<64; ii++) {
 attr_cache[ii].valid=0;
    }
}

variable_types: ii int


decompiled_code:
__int64 __fastcall attr_cache_lookup(char *s2, __m128i *a2)
{
__int64 i; // rbx

if ( !attr_cache_initialized )
attr_cache_init();
for ( i = 0LL; i != 17472; i += 273LL )
{
if ( attr_cache[i + 128] && !strcmp(&attr_cache[i], s2) )
{
*a2 = _mm_loadu_si128((const __m128i *)&attr_cache[i + 129]);
a2[1] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 145]);
a2[2] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 161]);
a2[3] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 177]);
a2[4] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 193]);
a2[5] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 209]);
a2[6] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 225]);
a2[7] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 241]);
a2[8] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 257]);
printf("cache hit on '%s'.\n", s2);
return 1LL;
}
}
printf("cache MISS on '%s'.\n", s2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s2, __m128i *a2)
{
__int64 i; // rbx

if ( !attr_cache_initialized )
func0();
for ( i = 0LL; i != 17472; i += 273LL )
{
if ( attr_cache[i + 128] && !strcmp(&attr_cache[i], s2) )
{
*a2 = _mm_loadu_si128((const __m128i *)&attr_cache[i + 129]);
a2[1] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 145]);
a2[2] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 161]);
a2[3] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 177]);
a2[4] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 193]);
a2[5] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 209]);
a2[6] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 225]);
a2[7] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 241]);
a2[8] = _mm_loadu_si128((const __m128i *)&attr_cache[i + 257]);
printf("cache hit on '%s'.\n", s2);
return 1LL;
}
}
printf("cache MISS on '%s'.\n", s2);
return 0LL;
}

original_code:
int attr_cache_lookup(const char* path, void* return_attr)
{
    int ii;
    if (! attr_cache_initialized) attr_cache_init();
    for (ii=0; ii<64; ii++) {
 if ((attr_cache[ii].valid) && (0==strcmp(attr_cache[ii].path, path))) {
     memcpy(return_attr, attr_cache[ii].attr, (sizeof(struct stat)));
     printf("cache hit on '%s'.\n", path);
     return 1;
 }
    }
    printf("cache MISS on '%s'.\n", path);
    return 0;
}

canonical_original_code:
int attr_cache_lookup(char *path, void *return_attr)
{
    int ii;
    if (! attr_cache_initialized) attr_cache_init();
    for (ii=0; ii<64; ii++) {
 if ((attr_cache[ii].valid) && (0==strcmp(attr_cache[ii].path, path))) {
     memcpy(return_attr, attr_cache[ii].attr, (sizeof(struct stat)));
     printf("cache hit on '%s'.\n", path);
     return 1;
 }
    }
    printf("cache MISS on '%s'.\n", path);
    return 0;
}

variable_types:
path
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; 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]; }


decompiled_code:
__int64 __fastcall attr_cache_remove(char *s2)
{
char *v1; // rbx
int i; // ebp
__int64 result; // rax

if ( !attr_cache_initialized )
attr_cache_init();
v1 = attr_cache;
for ( i = 0; i != 64; ++i )
{
if ( v1[128] && !strcmp(v1, s2) )
{
printf("successfully removed '%s' from cache.\n", s2);
result = 1LL;
attr_cache[273 * i + 128] = 0;
return result;
}
v1 += 273;
}
printf("tried to remove '%s' from cache but not found.\n", s2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s2)
{
char *v1; // rbx
int i; // ebp
__int64 result; // rax

if ( !attr_cache_initialized )
func0();
v1 = attr_cache;
for ( i = 0; i != 64; ++i )
{
if ( v1[128] && !strcmp(v1, s2) )
{
printf("successfully removed '%s' from cache.\n", s2);
result = 1LL;
attr_cache[273 * i + 128] = 0;
return result;
}
v1 += 273;
}
printf("tried to remove '%s' from cache but not found.\n", s2);
return 0LL;
}

original_code:
int attr_cache_remove(const char* path)
{
    int ii;
    if (! attr_cache_initialized) attr_cache_init();
    for (ii=0; ii<64; ii++) {
 if ((attr_cache[ii].valid) && (0==strcmp(attr_cache[ii].path, path))) {
     printf("successfully removed '%s' from cache.\n", path);
     attr_cache[ii].valid=0;
     return 1;
 }
    }
    printf("tried to remove '%s' from cache but not found.\n", path);
    return 0;
}

canonical_original_code:
int attr_cache_remove(char *path)
{
    int ii;
    if (! attr_cache_initialized) attr_cache_init();
    for (ii=0; ii<64; ii++) {
 if ((attr_cache[ii].valid) && (0==strcmp(attr_cache[ii].path, path))) {
     printf("successfully removed '%s' from cache.\n", path);
     attr_cache[ii].valid=0;
     return 1;
 }
    }
    printf("tried to remove '%s' from cache but not found.\n", path);
    return 0;
}


decompiled_code:
__int64 __fastcall attr_cache_store(char *src, const __m128i *a2)
{
unsigned int v2; // er13
__int64 v3; // rbp
char *v4; // rax
__int64 result; // rax

if ( !attr_cache_initialized )
attr_cache_init();
v2 = attr_cache_last_stored;
v3 = 273LL * attr_cache_last_stored;
v4 = strncpy(&attr_cache[v3], src, 0x7FuLL);
v4[127] = 0;
*(__m128i *)&attr_cache[v3 + 129] = _mm_loadu_si128(a2);
*(__m128i *)&attr_cache[v3 + 145] = _mm_loadu_si128(a2 + 1);
*(__m128i *)&attr_cache[v3 + 161] = _mm_loadu_si128(a2 + 2);
*(__m128i *)&attr_cache[v3 + 177] = _mm_loadu_si128(a2 + 3);
*(__m128i *)&attr_cache[v3 + 193] = _mm_loadu_si128(a2 + 4);
*(__m128i *)&attr_cache[v3 + 209] = _mm_loadu_si128(a2 + 5);
*(__m128i *)&attr_cache[v3 + 225] = _mm_loadu_si128(a2 + 6);
*(__m128i *)&attr_cache[v3 + 241] = _mm_loadu_si128(a2 + 7);
*(__m128i *)&attr_cache[v3 + 257] = _mm_loadu_si128(a2 + 8);
v4[128] = 1;
printf("path '%s' stored in cache in slot %i.\n", src, v2);
result = (unsigned int)((attr_cache_last_stored + 1) % 64);
attr_cache_last_stored = (attr_cache_last_stored + 1) % 64;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src, const __m128i *a2)
{
unsigned int v2; // er13
__int64 v3; // rbp
char *v4; // rax
__int64 result; // rax

if ( !attr_cache_initialized )
func0();
v2 = attr_cache_last_stored;
v3 = 273LL * attr_cache_last_stored;
v4 = strncpy(&attr_cache[v3], src, 0x7FuLL);
v4[127] = 0;
*(__m128i *)&attr_cache[v3 + 129] = _mm_loadu_si128(a2);
*(__m128i *)&attr_cache[v3 + 145] = _mm_loadu_si128(a2 + 1);
*(__m128i *)&attr_cache[v3 + 161] = _mm_loadu_si128(a2 + 2);
*(__m128i *)&attr_cache[v3 + 177] = _mm_loadu_si128(a2 + 3);
*(__m128i *)&attr_cache[v3 + 193] = _mm_loadu_si128(a2 + 4);
*(__m128i *)&attr_cache[v3 + 209] = _mm_loadu_si128(a2 + 5);
*(__m128i *)&attr_cache[v3 + 225] = _mm_loadu_si128(a2 + 6);
*(__m128i *)&attr_cache[v3 + 241] = _mm_loadu_si128(a2 + 7);
*(__m128i *)&attr_cache[v3 + 257] = _mm_loadu_si128(a2 + 8);
v4[128] = 1;
printf("path '%s' stored in cache in slot %i.\n", src, v2);
result = (unsigned int)((attr_cache_last_stored + 1) % 64);
attr_cache_last_stored = (attr_cache_last_stored + 1) % 64;
return result;
}

original_code:
void attr_cache_store(const char* path, void* attr)
{
    if (! attr_cache_initialized) attr_cache_init();
    strncpy(attr_cache[attr_cache_last_stored].path, path, 128 -1);
    attr_cache[attr_cache_last_stored].path[128 -1]='\0';
    memcpy(attr_cache[attr_cache_last_stored].attr, attr, (sizeof(struct stat)));
    attr_cache[attr_cache_last_stored].valid=1;
    printf("path '%s' stored in cache in slot %i.\n", path, attr_cache_last_stored);
    attr_cache_last_stored=(attr_cache_last_stored+1) % 64;
}

canonical_original_code:
void attr_cache_store(char *path, void *attr)
{
    if (! attr_cache_initialized) attr_cache_init();
    strncpy(attr_cache[attr_cache_last_stored].path, path, 128 -1);
    attr_cache[attr_cache_last_stored].path[128 -1]='\0';
    memcpy(attr_cache[attr_cache_last_stored].attr, attr, (sizeof(struct stat)));
    attr_cache[attr_cache_last_stored].valid=1;
    printf("path '%s' stored in cache in slot %i.\n", path, attr_cache_last_stored);
    attr_cache_last_stored=(attr_cache_last_stored+1) % 64;
}

variable_types:
attr
void *


decompiled_code:
__int64 __fastcall corefs_getattr(char *src, __m128i *a2)
{
__int64 result; // rax
char *v3; // rdx
__uid_t v4; // eax
__gid_t v5; // eax

if ( !*g_msp || !(unsigned int)attr_cache_lookup(src, a2) )
{
result = ((__int64 (__fastcall *)(char *, __m128i *))mop[4])(src, a2);
if ( (int)result < 0 )
return result;
v3 = g_msp;
if ( !*g_msp )
{
if ( g_msp[3] )
goto LABEL_11;
goto LABEL_5;
}
attr_cache_store(src, a2);
}
v3 = g_msp;
if ( g_msp[3] )
{
LABEL_11:
v4 = getuid();
v3 = g_msp;
a2[1].m128i_i32[3] = v4;
if ( !v3[4] )
goto LABEL_6;
goto LABEL_12;
}
LABEL_5:
if ( !v3[4] )
goto LABEL_6;
LABEL_12:
v5 = getgid();
v3 = g_msp;
a2[2].m128i_i32[0] = v5;
LABEL_6:
result = 0LL;
if ( v3[5] )
a2[1].m128i_i32[2] = *((_DWORD *)v3 + 2) | a2[1].m128i_i32[2] & 0x3F000;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src, __m128i *a2)
{
__int64 result; // rax
char *v3; // rdx
__uid_t v4; // eax
__gid_t v5; // eax

if ( !*g_msp || !(unsigned int)func1(src, a2) )
{
result = ((__int64 (__fastcall *)(char *, __m128i *))mop[4])(src, a2);
if ( (int)result < 0 )
return result;
v3 = g_msp;
if ( !*g_msp )
{
if ( g_msp[3] )
goto LABEL_11;
goto LABEL_5;
}
func3(src, a2);
}
v3 = g_msp;
if ( g_msp[3] )
{
LABEL_11:
v4 = getuid();
v3 = g_msp;
a2[1].m128i_i32[3] = v4;
if ( !v3[4] )
goto LABEL_6;
goto LABEL_12;
}
LABEL_5:
if ( !v3[4] )
goto LABEL_6;
LABEL_12:
v5 = getgid();
v3 = g_msp;
a2[2].m128i_i32[0] = v5;
LABEL_6:
result = 0LL;
if ( v3[5] )
a2[1].m128i_i32[2] = *((_DWORD *)v3 + 2) | a2[1].m128i_i32[2] & 0x3F000;
return result;
}

original_code:
static int corefs_getattr(const char *path, struct stat *stbuf)
{
    int res;
    if ((! g_msp->cache_attr) || (! attr_cache_lookup(path, stbuf))) {
      res = mop.getattr(path, stbuf);
      if(res < 0)
        return res;
      if (g_msp->cache_attr) attr_cache_store(path, stbuf);
    }
    if (g_msp->spoof_uids) stbuf->st_uid=getuid();
    if (g_msp->spoof_gids) stbuf->st_gid=getgid();
    if (g_msp->spoof_perms) {
      stbuf->st_mode &= 0770000;
      stbuf->st_mode |= g_msp->spoof_permbits;
    }
    return 0;
}

canonical_original_code:
static int corefs_getattr(char *path, struct stat *stbuf)
{
    int res;
    if ((! g_msp->cache_attr) || (! attr_cache_lookup(path, stbuf))) {
      res = mop.getattr(path, stbuf);
      if(res < 0)
        return res;
      if (g_msp->cache_attr) attr_cache_store(path, stbuf);
    }
    if (g_msp->spoof_uids) stbuf->st_uid=getuid();
    if (g_msp->spoof_gids) stbuf->st_gid=getgid();
    if (g_msp->spoof_perms) {
      stbuf->st_mode &= 0770000;
      stbuf->st_mode |= g_msp->spoof_permbits;
    }
    return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; 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: res int


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

result = 0LL;
*(_DWORD *)a1 = 0;
*(_WORD *)(a1 + 4) = 0;
*(_DWORD *)(a1 + 8) = 448;
return result;
}

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

result = 0LL;
*(_DWORD *)a1 = 0;
*(_WORD *)(a1 + 4) = 0;
*(_DWORD *)(a1 + 8) = 448;
return result;
}

original_code:
void init_settings(corefs_settings* ms)
{
    ms->cache_attr=0;
    ms->cache_dirlist=0;
    ms->cache_timeout=0;
    ms->spoof_uids=0;
    ms->spoof_gids=0;
    ms->spoof_perms=0;
    ms->spoof_permbits=00700;
}

canonical_original_code:
void init_settings(struct _corefs_settings *ms)
{
    ms->cache_attr=0;
    ms->cache_dirlist=0;
    ms->cache_timeout=0;
    ms->spoof_uids=0;
    ms->spoof_gids=0;
    ms->spoof_perms=0;
    ms->spoof_permbits=00700;
}

variable_types: struct _corefs_settings { char cache_attr; char cache_dirlist; char cache_timeout; char spoof_uids; char spoof_gids; char spoof_perms; int spoof_permbits; } *


original_code:
int corefs_main(int argc, char *argv[], const corefs_client_operations *op)
{
  memset(&corefs_oper, 0, sizeof(corefs_oper));
  if (op->getattr) corefs_oper.getattr = op->getattr;
  if (op->read) corefs_oper.read = op->read;
  if (op->write) corefs_oper.write = op->write;
  if (op->readdir) corefs_oper.readdir = op->readdir;
  if (op->mknod) corefs_oper.mknod = op->mknod;
  if (op->truncate) corefs_oper.truncate = op->truncate;
  if (op->unlink) corefs_oper.unlink = op->unlink;
  if (op->rename) corefs_oper.rename = op->rename;
  if (op->symlink) corefs_oper.symlink = op->symlink;
  if (op->readlink) corefs_oper.readlink = op->readlink;
  if (op->mkdir) corefs_oper.mkdir = op->mkdir;
  if (op->rmdir) corefs_oper.rmdir = op->rmdir;
  if (op->open) corefs_oper.open = op->open;
  if (op->release) corefs_oper.release = op->release;
  if (op->utime) corefs_oper.utime = op->utime;
  if (op->chmod) corefs_oper.chmod = op->chmod;
  if (op->link) corefs_oper.link = op->link;
  if (op->flush) corefs_oper.flush = op->flush;
  if (op->destroy) corefs_oper.destroy = op->destroy;
  if (op->setxattr) corefs_oper.setxattr = op->setxattr;
  if (op->getxattr) corefs_oper.getxattr = op->getxattr;
  if (op->listxattr) corefs_oper.listxattr = op->listxattr;
  if (op->removexattr) corefs_oper.removexattr = op->removexattr;
  umask(0);
  init_settings(&g_ms);
  g_ms.spoof_uids = 1;
  g_ms.spoof_gids = 1;
  g_ms.cache_attr = 0;
  g_ms.spoof_perms = 0;
  g_ms.spoof_permbits = 00700;
  return fuse_main_real_compat25(argc, argv, &corefs_oper, sizeof(*(&corefs_oper)));
}

canonical_original_code:
int corefs_main(int argc, char *argv[], struct _corefs_client_ops *op)
{
  memset(&corefs_oper, 0, sizeof(corefs_oper));
  if (op->getattr) corefs_oper.getattr = op->getattr;
  if (op->read) corefs_oper.read = op->read;
  if (op->write) corefs_oper.write = op->write;
  if (op->readdir) corefs_oper.readdir = op->readdir;
  if (op->mknod) corefs_oper.mknod = op->mknod;
  if (op->truncate) corefs_oper.truncate = op->truncate;
  if (op->unlink) corefs_oper.unlink = op->unlink;
  if (op->rename) corefs_oper.rename = op->rename;
  if (op->symlink) corefs_oper.symlink = op->symlink;
  if (op->readlink) corefs_oper.readlink = op->readlink;
  if (op->mkdir) corefs_oper.mkdir = op->mkdir;
  if (op->rmdir) corefs_oper.rmdir = op->rmdir;
  if (op->open) corefs_oper.open = op->open;
  if (op->release) corefs_oper.release = op->release;
  if (op->utime) corefs_oper.utime = op->utime;
  if (op->chmod) corefs_oper.chmod = op->chmod;
  if (op->link) corefs_oper.link = op->link;
  if (op->flush) corefs_oper.flush = op->flush;
  if (op->destroy) corefs_oper.destroy = op->destroy;
  if (op->setxattr) corefs_oper.setxattr = op->setxattr;
  if (op->getxattr) corefs_oper.getxattr = op->getxattr;
  if (op->listxattr) corefs_oper.listxattr = op->listxattr;
  if (op->removexattr) corefs_oper.removexattr = op->removexattr;
  umask(0);
  init_settings(&g_ms);
  g_ms.spoof_uids = 1;
  g_ms.spoof_gids = 1;
  g_ms.cache_attr = 0;
  g_ms.spoof_perms = 0;
  g_ms.spoof_permbits = 00700;
  return fuse_main_real_compat25(argc, argv, &corefs_oper, sizeof(*(&corefs_oper)));
}

variable_types: struct _corefs_client_ops { int (*open)(char *, struct fuse_file_info { int flags; unsigned long fh_old; int writepage; unsigned int direct_io; unsigned int keep_cache; unsigned int flush; unsigned int nonseekable; unsigned int flock_release; unsigned int padding; unsigned long fh; unsigned long lock_owner; } *); int (*release)(char *, struct fuse_file_info *); int (*read)(char *, char *, unsigned long, long, struct fuse_file_info *); int (*readdir)(char *, void *, int (*)(void *buf, char *name, 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]; } *stbuf, long off), long, struct fuse_file_info *); int (*getattr)(char *, struct stat *); int (*write)(char *, char *, unsigned long, long, struct fuse_file_info *); int (*mknod)(char *, unsigned int, unsigned long); int (*truncate)(char *, long); int (*unlink)(char *); int (*mkdir)(char *, unsigned int); int (*rmdir)(char *); int (*rename)(char *, char *); int (*link)(char *, char *); int (*symlink)(char *, char *); int (*readlink)(char *, char *, unsigned long); int (*chmod)(char *, unsigned int); int (*chown)(char *, unsigned int, unsigned int); int (*utime)(char *, struct utimbuf { long actime; long modtime; } *); int (*flush)(char *, struct fuse_file_info *); int (*access)(char *, int); void (*destroy)(void *); int (*setxattr)(char *, char *, char *, unsigned long, int); int (*getxattr)(char *, char *, char *, unsigned long); int (*listxattr)(char *, char *, unsigned long); int (*removexattr)(char *, char *); int (*up_parse_arguments)(char *argstr, int argc, char **argv); int (*up_new_server)(char *client, char *server, struct _commctx { struct _sock_ctx { int sock; } *sock_ctx; void *sec_ctx; int (*receive)(struct _commctx *, char *, int); int (*send)(struct _commctx *, char *, int); } *ctx); void (*up_get_user_info)(struct _user_info_ { unsigned int uid; unsigned int gid; unsigned int num_sgids; unsigned int sgids[32]; } *u, char *path, char *new_path); void (*up_eof_connection)(struct _commctx *ctx); } *

user_defined_types: struct _corefs_client_ops { int (*open)(char *, struct fuse_file_info *); int (*release)(char *, struct fuse_file_info *); int (*read)(char *, char *, unsigned long, long, struct fuse_file_info *); int (*readdir)(char *, void *, int (*)(void *buf, char *name, struct stat *stbuf, long off), long, struct fuse_file_info *); int (*getattr)(char *, struct stat *); int (*write)(char *, char *, unsigned long, long, struct fuse_file_info *); int (*mknod)(char *, unsigned int, unsigned long); int (*truncate)(char *, long); int (*unlink)(char *); int (*mkdir)(char *, unsigned int); int (*rmdir)(char *); int (*rename)(char *, char *); int (*link)(char *, char *); int (*symlink)(char *, char *); int (*readlink)(char *, char *, unsigned long); int (*chmod)(char *, unsigned int); int (*chown)(char *, unsigned int, unsigned int); int (*utime)(char *, struct utimbuf *); int (*flush)(char *, struct fuse_file_info *); int (*access)(char *, int); void (*destroy)(void *); int (*setxattr)(char *, char *, char *, unsigned long, int); int (*getxattr)(char *, char *, char *, unsigned long); int (*listxattr)(char *, char *, unsigned long); int (*removexattr)(char *, char *); int (*up_parse_arguments)(char *argstr, int argc, char **argv); int (*up_new_server)(char *client, char *server, struct _commctx *ctx); void (*up_get_user_info)(struct _user_info_ *u, char *path, char *new_path); void (*up_eof_connection)(struct _commctx *ctx); }

user_defined_types: struct _commctx { struct _sock_ctx *sock_ctx; void *sec_ctx; int (*receive)(struct _commctx *, char *, int); int (*send)(struct _commctx *, char *, int); }


decompiled_code:
__int64 init_mem_serv()
{
__int64 v0; // r12
__int64 result; // rax
int v2; // er13
unsigned int v3; // ebx
__int64 v4; // rsi
const char *v5; // rbp
_BYTE v6[120]; // [rsp+0h] [rbp-78h] BYREF

config_init(v6);
if ( (int)init_mem_serv_list(mem_serv_list) < 0 )
{
fwrite("\nError: Initialising memory servers list", 1uLL, 0x28uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !(unsigned int)config_read_file(v6, "mem_serv.cfg") )
{
fwrite("Error: Reading from config file\n", 1uLL, 0x20uLL, stderr);
return 0xFFFFFFFFLL;
}
v0 = config_lookup(v6, "servers");
result = config_setting_length(v0);
v2 = result;
if ( (int)result > 0 )
{
v3 = 0;
do
{
v4 = v3++;
v5 = (const char *)config_setting_get_string_elem(v0, v4);
printf("\nAdding memory server with ip %s", v5);
result = mem_serv_list_append(mem_serv_list, v5);
}
while ( v2 != v3 );
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r12
__int64 result; // rax
int v2; // er13
unsigned int v3; // ebx
__int64 v4; // rsi
const char *v5; // rbp
_BYTE v6[120]; // [rsp+0h] [rbp-78h] BYREF

config_init(v6);
if ( (int)init_mem_serv_list(mem_serv_list) < 0 )
{
fwrite("\nError: Initialising memory servers list", 1uLL, 0x28uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( !(unsigned int)config_read_file(v6, "mem_serv.cfg") )
{
fwrite("Error: Reading from config file\n", 1uLL, 0x20uLL, stderr);
return 0xFFFFFFFFLL;
}
v0 = config_lookup(v6, "servers");
result = config_setting_length(v0);
v2 = result;
if ( (int)result > 0 )
{
v3 = 0;
do
{
v4 = v3++;
v5 = (const char *)config_setting_get_string_elem(v0, v4);
printf("\nAdding memory server with ip %s", v5);
result = mem_serv_list_append(mem_serv_list, v5);
}
while ( v2 != v3 );
}
return result;
}

original_code:
int init_mem_serv() {
    config_t cfg;
    config_setting_t *servers, *ip;
    config_init(&cfg);
    if( init_mem_serv_list(&mem_serv_list) < 0){
        fprintf(stderr, "\nError: Initialising memory servers list");
        return -1;
    }
    if(! config_read_file(&cfg, "mem_serv.cfg")){
        fprintf(stderr, "Error: Reading from config file\n");
        return -1;
    }
    servers = config_lookup(&cfg, "servers");
    int count = config_setting_length(servers);
    int i;
    const char *ip_addr;
    for(i=0; i<count; i++){
        ip_addr = config_setting_get_string_elem(servers, i);
        printf("\nAdding memory server with ip %s", ip_addr);
        mem_serv_list_append(&mem_serv_list, ip_addr);
    }
}

canonical_original_code:
int init_mem_serv() {
    struct config_t cfg;
    struct config_setting_t *servers; struct config_setting_t *ip;
    config_init(&cfg);
    if( init_mem_serv_list(&mem_serv_list) < 0){
        fprintf(stderr, "\nError: Initialising memory servers list");
        return -1;
    }
    if(! config_read_file(&cfg, "mem_serv.cfg")){
        fprintf(stderr, "Error: Reading from config file\n");
        return -1;
    }
    servers = config_lookup(&cfg, "servers");
    int count = config_setting_length(servers);
    int i;
    char *ip_addr;
    for(i=0; i<count; i++){
        ip_addr = config_setting_get_string_elem(servers, i);
        printf("\nAdding memory server with ip %s", ip_addr);
        mem_serv_list_append(&mem_serv_list, ip_addr);
    }
}

variable_types: struct config_t { struct config_setting_t { char *name; short type; short format; union config_value_t { int ival; long long llval; double fval; char *sval; struct config_list_t { unsigned int length; struct config_setting_t **elements; } *list; } value; struct config_setting_t *parent; struct config_t *config; void *hook; unsigned int line; char *file; } *root; void (*destructor)(void *); int options; unsigned short tab_width; short default_format; char *include_dir; char *error_text; char *error_file; int error_line; enum config_error_t { CONFIG_ERR_NONE=0, CONFIG_ERR_FILE_IO=1, CONFIG_ERR_PARSE=2 } error_type; char **filenames; unsigned int num_filenames; }

variable_types: struct config_setting_t { char *name; short type; short format; union config_value_t { int ival; long long llval; double fval; char *sval; struct config_list_t { unsigned int length; struct config_setting_t **elements; } *list; } value; struct config_setting_t *parent; struct config_t { struct config_setting_t *root; void (*destructor)(void *); int options; unsigned short tab_width; short default_format; char *include_dir; char *error_text; char *error_file; int error_line; enum config_error_t { CONFIG_ERR_NONE=0, CONFIG_ERR_FILE_IO=1, CONFIG_ERR_PARSE=2 } error_type; char **filenames; unsigned int num_filenames; } *config; void *hook; unsigned int line; char *file; } *

variable_types: count int

user_defined_types: struct config_t { struct config_setting_t *root; void (*destructor)(void *); int options; unsigned short tab_width; short default_format; char *include_dir; char *error_text; char *error_file; int error_line; enum config_error_t error_type; char **filenames; unsigned int num_filenames; }

user_defined_types: struct config_setting_t { char *name; short type; short format; union config_value_t value; struct config_setting_t *parent; struct config_t *config; void *hook; unsigned int line; char *file; }

user_defined_types: union config_value_t { int ival; long long llval; double fval; char *sval; struct config_list_t *list; }


decompiled_code:
__int64 __fastcall get_client_count(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

original_code:
int get_client_count(mem_serv* curr) {
    return curr->count;
}

canonical_original_code:
int get_client_count(struct _mem_serv *curr) {
    return curr->count;
}

variable_types: struct _mem_serv { char ip[16]; int is_assigned; int is_synced; int count; struct _mem_serv *next; struct _mem_serv *prev; } *


decompiled_code:
void __fastcall set_client_count(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 24) = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 24) = a2;
}

original_code:
void set_client_count(mem_serv* curr, int n) {
    curr->count = n;
    return;
}

canonical_original_code:
void set_client_count(struct _mem_serv *curr, int n) {
    curr->count = n;
    return;
}

variable_types: n int

user_defined_types: struct _mem_serv { char ip[16]; int is_assigned; int is_synced; int count; struct _mem_serv *next; struct _mem_serv *prev; }


decompiled_code:
void __fastcall inc_client_count(__int64 a1)
{
++*(_DWORD *)(a1 + 24);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
++*(_DWORD *)(a1 + 24);
}

original_code:
void inc_client_count(mem_serv* curr) {
    curr->count+=1;
    return;
}

canonical_original_code:
void inc_client_count(struct _mem_serv *curr) {
    curr->count+=1;
    return;
}


decompiled_code:
void __fastcall dec_client_count(__int64 a1)
{
--*(_DWORD *)(a1 + 24);
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
--*(_DWORD *)(a1 + 24);
}

original_code:
void dec_client_count(mem_serv* curr) {
    curr->count--;
    return;
}

canonical_original_code:
void dec_client_count(struct _mem_serv *curr) {
    curr->count--;
    return;
}


decompiled_code:
void __fastcall set_is_assigned(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 16) = a2;
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 16) = a2;
}

original_code:
void set_is_assigned(mem_serv* curr, int n) {
    curr->is_assigned = n;
    return;
}

canonical_original_code:
void set_is_assigned(struct _mem_serv *curr, int n) {
    curr->is_assigned = n;
    return;
}


decompiled_code:
void __fastcall set_is_synced(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 20) = a2;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, int a2)
{
*(_DWORD *)(a1 + 20) = a2;
}

original_code:
void set_is_synced(mem_serv* curr, int n) {
    curr->is_synced = n;
}

canonical_original_code:
void set_is_synced(struct _mem_serv *curr, int n) {
    curr->is_synced = n;
}


decompiled_code:
__int64 __fastcall get_is_assigned(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
int get_is_assigned(mem_serv* curr) {
    return curr->is_assigned;
}

canonical_original_code:
int get_is_assigned(struct _mem_serv *curr) {
    return curr->is_assigned;
}


decompiled_code:
__int64 __fastcall get_is_synced(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
int get_is_synced(mem_serv* curr) {
    return curr->is_synced;
}

canonical_original_code:
int get_is_synced(struct _mem_serv *curr) {
    return curr->is_synced;
}


decompiled_code:
char *__fastcall get_mem_serv_by_ip(char *s1)
{
char *v1; // r12

v1 = mem_serv_list;
printf("\nGet memory server by ip %s\n", s1);
fprintf(stderr, "searching for mem servers with ip %s\n", s1);
while ( 1 )
{
fprintf(stderr, "comparing %s with %s \n", s1, v1);
if ( !strcasecmp(s1, v1) )
break;
v1 = (char *)*((_QWORD *)v1 + 4);
if ( !v1 )
return 0LL;
}
printf("\nFound memory server by ip %s with count %d\n", v1, *((unsigned int *)v1 + 6));
return v1;
}

canonical_decompiled_code:
char *__fastcall func9(char *s1)
{
char *v1; // r12

v1 = mem_serv_list;
printf("\nGet memory server by ip %s\n", s1);
fprintf(stderr, "searching for mem servers with ip %s\n", s1);
while ( 1 )
{
fprintf(stderr, "comparing %s with %s \n", s1, v1);
if ( !strcasecmp(s1, v1) )
break;
v1 = (char *)*((_QWORD *)v1 + 4);
if ( !v1 )
return 0LL;
}
printf("\nFound memory server by ip %s with count %d\n", v1, *((unsigned int *)v1 + 6));
return v1;
}

original_code:
mem_serv* get_mem_serv_by_ip(const char *ip) {
    printf("\nGet memory server by ip %s\n", ip);
    mem_serv *curr, *prev;
    curr = &mem_serv_list;
    mem_serv *node = ((void *)0);
    fprintf(stderr, "searching for mem servers with ip %s\n", ip);
    while (curr != ((void *)0)) {
        fprintf(stderr, "comparing %s with %s \n", ip, curr->ip);
        if(strcasecmp(ip, curr->ip) == 0) {
            printf("\nFound memory server by ip %s with count %d\n", curr->ip, curr->count);
            node = curr;
            break;
        }
        prev = curr;
        curr = curr->next;
    }
    return node;
}

canonical_original_code:
struct _mem_serv *get_mem_serv_by_ip(char *ip) {
    printf("\nGet memory server by ip %s\n", ip);
    struct _mem_serv *curr; struct _mem_serv *prev;
    curr = &mem_serv_list;
    struct _mem_serv *node = ((void *)0);
    fprintf(stderr, "searching for mem servers with ip %s\n", ip);
    while (curr != ((void *)0)) {
        fprintf(stderr, "comparing %s with %s \n", ip, curr->ip);
        if(strcasecmp(ip, curr->ip) == 0) {
            printf("\nFound memory server by ip %s with count %d\n", curr->ip, curr->count);
            node = curr;
            break;
        }
        prev = curr;
        curr = curr->next;
    }
    return node;
}

variable_types:
ip
char *

return_type: struct _mem_serv { char ip[16]; int is_assigned; int is_synced; int count; struct _mem_serv *next; struct _mem_serv *prev; } *


decompiled_code:
int __fastcall get_best_mem_serv_ip(char *dest)
{
const char *v1; // rbp
const char *v2; // r13
const char *v3; // r12
const char *i; // rbx
__int64 v5; // rsi

v1 = *(const char **)&mem_serv_list[32];
if ( v1 )
{
v2 = *(const char **)&mem_serv_list[32];
v3 = v2;
for ( i = v2; ; v3 = i )
{
printf("\nInside while with ip %s and count %d\n", v3, *((unsigned int *)i + 6));
printf("\nInside while with min count = %d and ip = %s\n", *((unsigned int *)v1 + 6), v2);
v5 = *((unsigned int *)v1 + 6);
if ( *((_DWORD *)i + 6) >= (int)v5 )
{
i = (const char *)*((_QWORD *)i + 4);
if ( !i )
goto LABEL_7;
}
else
{
v1 = i;
printf("\nInside if with min count = %d and ip = %s\n", v5, v2);
i = (const char *)*((_QWORD *)i + 4);
v2 = v3;
if ( !i )
goto LABEL_7;
}
}
}
v2 = 0LL;
LABEL_7:
strcpy(dest, v2);
return printf("\nSending ip %s ", dest);
}

canonical_decompiled_code:
int __fastcall func10(char *dest)
{
const char *v1; // rbp
const char *v2; // r13
const char *v3; // r12
const char *i; // rbx
__int64 v5; // rsi

v1 = *(const char **)&mem_serv_list[32];
if ( v1 )
{
v2 = *(const char **)&mem_serv_list[32];
v3 = v2;
for ( i = v2; ; v3 = i )
{
printf("\nInside while with ip %s and count %d\n", v3, *((unsigned int *)i + 6));
printf("\nInside while with min count = %d and ip = %s\n", *((unsigned int *)v1 + 6), v2);
v5 = *((unsigned int *)v1 + 6);
if ( *((_DWORD *)i + 6) >= (int)v5 )
{
i = (const char *)*((_QWORD *)i + 4);
if ( !i )
goto LABEL_7;
}
else
{
v1 = i;
printf("\nInside if with min count = %d and ip = %s\n", v5, v2);
i = (const char *)*((_QWORD *)i + 4);
v2 = v3;
if ( !i )
goto LABEL_7;
}
}
}
v2 = 0LL;
LABEL_7:
strcpy(dest, v2);
return printf("\nSending ip %s ", dest);
}

original_code:
int get_best_mem_serv_ip(char *ip) {
    mem_serv *curr, *min;
    curr = &mem_serv_list;
    if(curr == ((void *)0)) {
        fprintf(stderr, "\nList is not initialised");
        return -1;
    }
    curr = curr->next;
    min = curr;
    while(curr != ((void *)0)) {
        printf("\nInside while with ip %s and count %d\n", curr->ip, curr->count);
        printf("\nInside while with min count = %d and ip = %s\n", min->count, min->ip);
        if(curr->count < min->count) {
            printf("\nInside if with min count = %d and ip = %s\n", min->count, min->ip);
            min = curr;
        }
        curr = curr->next;
    }
    strcpy(ip, min->ip);
    printf("\nSending ip %s ", ip);
}

canonical_original_code:
int get_best_mem_serv_ip(char *ip) {
    struct _mem_serv *curr; struct _mem_serv *min;
    curr = &mem_serv_list;
    if(curr == ((void *)0)) {
        fprintf(stderr, "\nList is not initialised");
        return -1;
    }
    curr = curr->next;
    min = curr;
    while(curr != ((void *)0)) {
        printf("\nInside while with ip %s and count %d\n", curr->ip, curr->count);
        printf("\nInside while with min count = %d and ip = %s\n", min->count, min->ip);
        if(curr->count < min->count) {
            printf("\nInside if with min count = %d and ip = %s\n", min->count, min->ip);
            min = curr;
        }
        curr = curr->next;
    }
    strcpy(ip, min->ip);
    printf("\nSending ip %s ", ip);
}


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

if ( a1 )
{
*(_QWORD *)(a1 + 24) = 0LL;
result = 0LL;
*(_QWORD *)(a1 + 32) = 0LL;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
fwrite("init_list_head: received NULL head value - initialize head\n", 1uLL, 0x3BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( a1 )
{
*(_QWORD *)(a1 + 24) = 0LL;
result = 0LL;
*(_QWORD *)(a1 + 32) = 0LL;
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 16) = 0LL;
*(_QWORD *)a1 = 0LL;
}
else
{
fwrite("init_list_head: received NULL head value - initialize head\n", 1uLL, 0x3BuLL, stderr);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int init_list_head(list_head * head)
{
  if(head == ((void *)0)){
    fprintf(stderr, "init_list_head: received NULL head value - initialize head\n");
    return -1;
  }
  head->next = ((void *)0);
  head->prev = ((void *)0);
  head->alength = 0;
  head->data = ((void *)0);
  head->server_addr = ((void *)0);
  return 0;
}

canonical_original_code:
int init_list_head(struct node *head)
{
  if(head == ((void *)0)){
    fprintf(stderr, "init_list_head: received NULL head value - initialize head\n");
    return -1;
  }
  head->next = ((void *)0);
  head->prev = ((void *)0);
  head->alength = 0;
  head->data = ((void *)0);
  head->server_addr = ((void *)0);
  return 0;
}

variable_types: struct node { char *server_addr; unsigned int alength; void *data; struct node *next; struct node *prev; } *


decompiled_code:
__int64 __fastcall insert_info_ptr(__int64 a1, void ***a2, const char *a3, unsigned int a4, __int64 a5)
{
_QWORD *v8; // rbx
size_t v9; // rdx
_DWORD *v10; // r9
_DWORD *v11; // rax
void **v12; // rax
__int64 v13; // rax
__int64 result; // rax
_DWORD *v15; // rax
int *v16; // rax
int *v17; // rax
int *v18; // rax
size_t nmemba; // [rsp+0h] [rbp-48h]
int nmemb; // [rsp+0h] [rbp-48h]

if ( a4 > 0x101 )
{
fprintf(stderr, "Insert: file name %s exceeds length limit %d\n", a3, 257LL);
return 0xFFFFFFFFLL;
}
if ( !a1 )
{
fwrite("Insert: head of the list is NULL - initialize it!\n", 1uLL, 0x32uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( *(_DWORD *)(a1 + 8) == 100 )
{
if ( !a2 )
{
fwrite("INSERT: tail of the list is NULL - potential error while initializing list head\n", 1uLL, 0x50uLL, stderr);
return 0xFFFFFFFFLL;
}
v8 = *a2;
v9 = a4;
v10 = **a2;
if ( *((_DWORD *)*a2 + 2) < a4 )
{
nmemba = a4;
free(**a2);
v11 = calloc(nmemba, 1uLL);
v9 = nmemba;
*v8 = v11;
v10 = v11;
if ( !v11 )
{
v16 = _errno_location();
fprintf(stderr, "INSERT: out of memory errno[%i]\n", (unsigned int)*v16);
return 0xFFFFFFFFLL;
}
}
if ( a4 < 8 )
{
if ( (a4 & 4) != 0 )
{
*v10 = *(_DWORD *)a3;
*(_DWORD *)((char *)v10 + v9 - 4) = *(_DWORD *)&a3[v9 - 4];
}
else if ( a4 )
{
*(_BYTE *)v10 = *a3;
if ( (a4 & 2) != 0 )
*(_WORD *)((char *)v10 + v9 - 2) = *(_WORD *)&a3[v9 - 2];
}
}
else
{
*(_QWORD *)v10 = *(_QWORD *)a3;
*(_QWORD *)((char *)v10 + v9 - 8) = *(_QWORD *)&a3[v9 - 8];
qmemcpy(
(void *)((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a3 - ((const char *)v10 - ((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((a4 + (_DWORD)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8)) >> 3));
}
v8[2] = a5;
v12 = *a2;
*((_DWORD *)v8 + 2) = a4;
v12[3] = 0LL;
*a2 = (void **)(*a2)[4];
}
else
{
nmemb = *(_DWORD *)(a1 + 8);
v8 = calloc(1uLL, 0x28uLL);
if ( !v8 )
{
v17 = _errno_location();
fprintf(stderr, "out of memory errno[%i]\n", (unsigned int)*v17);
return 0xFFFFFFFFLL;
}
v15 = calloc(a4, 1uLL);
*v8 = v15;
if ( !v15 )
{
v18 = _errno_location();
fprintf(stderr, "INSERT out of memory errno[%i]\n", (unsigned int)*v18);
return 0xFFFFFFFFLL;
}
if ( a4 >= 8 )
{
*(_QWORD *)((char *)v15 + a4 - 8) = *(_QWORD *)&a3[a4 - 8];
qmemcpy(v15, a3, 8LL * ((a4 - 1) >> 3));
}
else if ( (a4 & 4) != 0 )
{
*v15 = *(_DWORD *)a3;
*(_DWORD *)((char *)v15 + a4 - 4) = *(_DWORD *)&a3[a4 - 4];
}
else if ( a4 )
{
*(_BYTE *)v15 = *a3;
if ( (a4 & 2) != 0 )
*(_WORD *)((char *)v15 + a4 - 2) = *(_WORD *)&a3[a4 - 2];
}
*((_DWORD *)v8 + 2) = a4;
v8[2] = a5;
if ( !nmemb )
*a2 = (void **)v8;
*(_DWORD *)(a1 + 8) = nmemb + 1;
}
v13 = *(_QWORD *)(a1 + 24);
v8[3] = v13;
if ( v13 )
*(_QWORD *)(v13 + 32) = v8;
v8[4] = a1;
result = 0LL;
*(_QWORD *)(a1 + 24) = v8;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, void ***a2, const char *a3, unsigned int a4, __int64 a5)
{
_QWORD *v8; // rbx
size_t v9; // rdx
_DWORD *v10; // r9
_DWORD *v11; // rax
void **v12; // rax
__int64 v13; // rax
__int64 result; // rax
_DWORD *v15; // rax
int *v16; // rax
int *v17; // rax
int *v18; // rax
size_t nmemba; // [rsp+0h] [rbp-48h]
int nmemb; // [rsp+0h] [rbp-48h]

if ( a4 > 0x101 )
{
fprintf(stderr, "Insert: file name %s exceeds length limit %d\n", a3, 257LL);
return 0xFFFFFFFFLL;
}
if ( !a1 )
{
fwrite("Insert: head of the list is NULL - initialize it!\n", 1uLL, 0x32uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( *(_DWORD *)(a1 + 8) == 100 )
{
if ( !a2 )
{
fwrite("INSERT: tail of the list is NULL - potential error while initializing list head\n", 1uLL, 0x50uLL, stderr);
return 0xFFFFFFFFLL;
}
v8 = *a2;
v9 = a4;
v10 = **a2;
if ( *((_DWORD *)*a2 + 2) < a4 )
{
nmemba = a4;
free(**a2);
v11 = calloc(nmemba, 1uLL);
v9 = nmemba;
*v8 = v11;
v10 = v11;
if ( !v11 )
{
v16 = _errno_location();
fprintf(stderr, "INSERT: out of memory errno[%i]\n", (unsigned int)*v16);
return 0xFFFFFFFFLL;
}
}
if ( a4 < 8 )
{
if ( (a4 & 4) != 0 )
{
*v10 = *(_DWORD *)a3;
*(_DWORD *)((char *)v10 + v9 - 4) = *(_DWORD *)&a3[v9 - 4];
}
else if ( a4 )
{
*(_BYTE *)v10 = *a3;
if ( (a4 & 2) != 0 )
*(_WORD *)((char *)v10 + v9 - 2) = *(_WORD *)&a3[v9 - 2];
}
}
else
{
*(_QWORD *)v10 = *(_QWORD *)a3;
*(_QWORD *)((char *)v10 + v9 - 8) = *(_QWORD *)&a3[v9 - 8];
qmemcpy(
(void *)((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)(a3 - ((const char *)v10 - ((unsigned __int64)(v10 + 2) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * ((a4 + (_DWORD)v10 - (((_DWORD)v10 + 8) & 0xFFFFFFF8)) >> 3));
}
v8[2] = a5;
v12 = *a2;
*((_DWORD *)v8 + 2) = a4;
v12[3] = 0LL;
*a2 = (void **)(*a2)[4];
}
else
{
nmemb = *(_DWORD *)(a1 + 8);
v8 = calloc(1uLL, 0x28uLL);
if ( !v8 )
{
v17 = _errno_location();
fprintf(stderr, "out of memory errno[%i]\n", (unsigned int)*v17);
return 0xFFFFFFFFLL;
}
v15 = calloc(a4, 1uLL);
*v8 = v15;
if ( !v15 )
{
v18 = _errno_location();
fprintf(stderr, "INSERT out of memory errno[%i]\n", (unsigned int)*v18);
return 0xFFFFFFFFLL;
}
if ( a4 >= 8 )
{
*(_QWORD *)((char *)v15 + a4 - 8) = *(_QWORD *)&a3[a4 - 8];
qmemcpy(v15, a3, 8LL * ((a4 - 1) >> 3));
}
else if ( (a4 & 4) != 0 )
{
*v15 = *(_DWORD *)a3;
*(_DWORD *)((char *)v15 + a4 - 4) = *(_DWORD *)&a3[a4 - 4];
}
else if ( a4 )
{
*(_BYTE *)v15 = *a3;
if ( (a4 & 2) != 0 )
*(_WORD *)((char *)v15 + a4 - 2) = *(_WORD *)&a3[a4 - 2];
}
*((_DWORD *)v8 + 2) = a4;
v8[2] = a5;
if ( !nmemb )
*a2 = (void **)v8;
*(_DWORD *)(a1 + 8) = nmemb + 1;
}
v13 = *(_QWORD *)(a1 + 24);
v8[3] = v13;
if ( v13 )
*(_QWORD *)(v13 + 32) = v8;
v8[4] = a1;
result = 0LL;
*(_QWORD *)(a1 + 24) = v8;
return result;
}

original_code:
int insert_info_ptr(list_head * head, list_tail ** tail, const char* addr,
                    unsigned int addr_length, void * data)
{
  server_list * node = ((void *)0);
  if(addr_length > 257){
    fprintf(stderr,"Insert: file name %s exceeds length limit %d\n", addr,257);
    return -1;
  }
  if(head == ((void *)0)){
    fprintf(stderr,"Insert: head of the list is NULL - initialize it!\n");
    return -1;
  }
  if(head->alength == 100){
    if(tail == ((void *)0)){
      fprintf(stderr,"INSERT: tail of the list is NULL - potential error while initializing list head\n");
      return -1;
    }
    node = *tail;
    if(node->alength < addr_length){
      free(node->server_addr);
      node->server_addr = (char *) calloc(addr_length, 1);
      if(node->server_addr == ((void *)0)) {
        fprintf(stderr, "INSERT: out of memory errno[%i]\n",(*__errno_location ()));
        return -1;
      }
    }
    memcpy(node->server_addr,addr,addr_length);
    node->alength = addr_length;
    node->data=data;
    (*tail)->next = ((void *)0);
    if(100 == 1)
      return 0;
    *tail = (*tail)->prev;
  }
  else {
    node = (server_list*)calloc(1,sizeof(server_list));
    if(node == ((void *)0)) {
      fprintf(stderr, "out of memory errno[%i]\n",(*__errno_location ()));
      return -1;
    }
    node->server_addr = (char *) calloc(addr_length, 1);
    if(node->server_addr == ((void *)0)) {
      fprintf(stderr, "INSERT out of memory errno[%i]\n",(*__errno_location ()));
      return -1;
    }
    memcpy(node->server_addr,addr, addr_length);
    node->alength = addr_length;
    node->data=data;
    if(head->alength == 0)
      *tail = node;
    head->alength += 1;
  }
  node->next = head->next;
  if(head->next)
    head->next->prev = node;
  node->prev = head;
  head->next = node;
  return 0;
}

canonical_original_code:
int insert_info_ptr(struct node *head, struct node **tail, char *addr, unsigned int addr_length, void *data)
{
  struct node *node = ((void *)0);
  if(addr_length > 257){
    fprintf(stderr,"Insert: file name %s exceeds length limit %d\n", addr,257);
    return -1;
  }
  if(head == ((void *)0)){
    fprintf(stderr,"Insert: head of the list is NULL - initialize it!\n");
    return -1;
  }
  if(head->alength == 100){
    if(tail == ((void *)0)){
      fprintf(stderr,"INSERT: tail of the list is NULL - potential error while initializing list head\n");
      return -1;
    }
    node = *tail;
    if(node->alength < addr_length){
      free(node->server_addr);
      node->server_addr = (char *) calloc(addr_length, 1);
      if(node->server_addr == ((void *)0)) {
        fprintf(stderr, "INSERT: out of memory errno[%i]\n",(*__errno_location ()));
        return -1;
      }
    }
    memcpy(node->server_addr,addr,addr_length);
    node->alength = addr_length;
    node->data=data;
    (*tail)->next = ((void *)0);
    if(100 == 1)
      return 0;
    *tail = (*tail)->prev;
  }
  else {
    node = (struct node *)calloc(1,sizeof(server_list));
    if(node == ((void *)0)) {
      fprintf(stderr, "out of memory errno[%i]\n",(*__errno_location ()));
      return -1;
    }
    node->server_addr = (char *) calloc(addr_length, 1);
    if(node->server_addr == ((void *)0)) {
      fprintf(stderr, "INSERT out of memory errno[%i]\n",(*__errno_location ()));
      return -1;
    }
    memcpy(node->server_addr,addr, addr_length);
    node->alength = addr_length;
    node->data=data;
    if(head->alength == 0)
      *tail = node;
    head->alength += 1;
  }
  node->next = head->next;
  if(head->next)
    head->next->prev = node;
  node->prev = head;
  head->next = node;
  return 0;
}

variable_types:
tail
struct node **

variable_types:
addr
char *

variable_types:
data
void *

user_defined_types: struct node { char *server_addr; unsigned int alength; void *data; struct node *next; struct node *prev; }


original_code:
int get_info_ptr(list_head * head, list_tail ** tail, const char *addr,
                 unsigned int addr_length, void **pointer_to_data)
{
  server_list * node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->alength == addr_length &&
       memcmp(node->server_addr,addr,addr_length) == 0){
      if(node->prev != head){
        node->prev->next = node->next;
        if(node->next)
          node->next->prev = node->prev;
        else
          *tail = node->prev;
        node->next = head->next;
        node->next->prev = node;
        head->next = node;
        node->prev = head;
      }
      *pointer_to_data=node->data;
      return node->alength;
    }
    node = node->next;
  }
  return -1;
}

canonical_original_code:
int get_info_ptr(struct node *head, struct node **tail, char *addr, unsigned int addr_length, void **pointer_to_data)
{
  struct node *node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->alength == addr_length &&
       memcmp(node->server_addr,addr,addr_length) == 0){
      if(node->prev != head){
        node->prev->next = node->next;
        if(node->next)
          node->next->prev = node->prev;
        else
          *tail = node->prev;
        node->next = head->next;
        node->next->prev = node;
        head->next = node;
        node->prev = head;
      }
      *pointer_to_data=node->data;
      return node->alength;
    }
    node = node->next;
  }
  return -1;
}


original_code:
int change_info_ptr(list_head * head, list_tail ** tail, const char * addr,
                unsigned int addr_length, void * data)
{
  server_list * node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->alength == addr_length &&
       memcmp(node->server_addr,addr, addr_length) == 0){
      if(node->prev != head){
        node->prev->next = node->next;
        if(node->next)
          node->next->prev = node->prev;
        else
          *tail = node->prev;
        node->next = head->next;
        node->next->prev = node;
        head->next = node;
        node->prev = head;
      }
      node->data=data;
      return node->alength;
    }
    node = node->next;
  }
  return -1;
}

canonical_original_code:
int change_info_ptr(struct node *head, struct node **tail, char *addr, unsigned int addr_length, void *data)
{
  struct node *node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->alength == addr_length &&
       memcmp(node->server_addr,addr, addr_length) == 0){
      if(node->prev != head){
        node->prev->next = node->next;
        if(node->next)
          node->next->prev = node->prev;
        else
          *tail = node->prev;
        node->next = head->next;
        node->next->prev = node;
        head->next = node;
        node->prev = head;
      }
      node->data=data;
      return node->alength;
    }
    node = node->next;
  }
  return -1;
}


decompiled_code:
__int64 __fastcall delete_info(__int64 a1, _QWORD *a2, const void *a3, unsigned int a4)
{
_QWORD *v5; // rbx
size_t v6; // rbp
void *v7; // r14
_QWORD *v8; // r15
int v9; // eax
unsigned int v10; // er12
__int64 v11; // rax
void *v12; // rdi

if ( !a1 || !a3 || (v5 = *(_QWORD **)(a1 + 24)) == 0LL )
return (unsigned int)-1;
v6 = a4;
while ( 1 )
{
v7 = (void *)*v5;
v8 = v5;
v9 = memcmp((const void *)*v5, a3, v6);
v5 = (_QWORD *)v5[3];
v10 = v9;
if ( !v9 )
break;
if ( !v5 )
return (unsigned int)-1;
}
v11 = v8[4];
*(_QWORD *)(v11 + 24) = v5;
if ( v5 )
v5[4] = v11;
else
*a2 = v11;
free(v7);
v12 = (void *)v8[2];
if ( v12 )
free(v12);
free(v8);
--*(_DWORD *)(a1 + 8);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _QWORD *a2, const void *a3, unsigned int a4)
{
_QWORD *v5; // rbx
size_t v6; // rbp
void *v7; // r14
_QWORD *v8; // r15
int v9; // eax
unsigned int v10; // er12
__int64 v11; // rax
void *v12; // rdi

if ( !a1 || !a3 || (v5 = *(_QWORD **)(a1 + 24)) == 0LL )
return (unsigned int)-1;
v6 = a4;
while ( 1 )
{
v7 = (void *)*v5;
v8 = v5;
v9 = memcmp((const void *)*v5, a3, v6);
v5 = (_QWORD *)v5[3];
v10 = v9;
if ( !v9 )
break;
if ( !v5 )
return (unsigned int)-1;
}
v11 = v8[4];
*(_QWORD *)(v11 + 24) = v5;
if ( v5 )
v5[4] = v11;
else
*a2 = v11;
free(v7);
v12 = (void *)v8[2];
if ( v12 )
free(v12);
free(v8);
--*(_DWORD *)(a1 + 8);
return v10;
}

original_code:
int delete_info(list_head * head, list_tail ** tail, const char * addr,
                unsigned int addr_length){
  server_list * node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(memcmp(node->server_addr,addr,addr_length) == 0){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      if(node->server_addr) free(node->server_addr);
      if(node->data) free(node->data);
      if(node) free(node);
      head->alength -= 1;
      return 0;
    }
    node = node->next;
  }
  return -1;
}

canonical_original_code:
int delete_info(struct node *head, struct node **tail, char *addr, unsigned int addr_length){
  struct node *node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(memcmp(node->server_addr,addr,addr_length) == 0){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      if(node->server_addr) free(node->server_addr);
      if(node->data) free(node->data);
      if(node) free(node);
      head->alength -= 1;
      return 0;
    }
    node = node->next;
  }
  return -1;
}


decompiled_code:
int __fastcall remove_info(__int64 a1, _QWORD *a2, const void *a3, unsigned int a4)
{
__int64 v5; // rbx
size_t v6; // rbp
int result; // eax
__int64 v8; // rdx
__int64 v9; // rdx

if ( !a1 )
return -1;
if ( !a3 )
return -1;
v5 = *(_QWORD *)(a1 + 24);
if ( !v5 )
return -1;
v6 = a4;
while ( 1 )
{
result = memcmp(*(const void **)v5, a3, v6);
v8 = v5;
v5 = *(_QWORD *)(v5 + 24);
if ( !result )
break;
if ( !v5 )
return -1;
}
v9 = *(_QWORD *)(v8 + 32);
*(_QWORD *)(v9 + 24) = v5;
if ( v5 )
*(_QWORD *)(v5 + 32) = v9;
else
*a2 = v9;
--*(_DWORD *)(a1 + 8);
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, _QWORD *a2, const void *a3, unsigned int a4)
{
__int64 v5; // rbx
size_t v6; // rbp
int result; // eax
__int64 v8; // rdx
__int64 v9; // rdx

if ( !a1 )
return -1;
if ( !a3 )
return -1;
v5 = *(_QWORD *)(a1 + 24);
if ( !v5 )
return -1;
v6 = a4;
while ( 1 )
{
result = memcmp(*(const void **)v5, a3, v6);
v8 = v5;
v5 = *(_QWORD *)(v5 + 24);
if ( !result )
break;
if ( !v5 )
return -1;
}
v9 = *(_QWORD *)(v8 + 32);
*(_QWORD *)(v9 + 24) = v5;
if ( v5 )
*(_QWORD *)(v5 + 32) = v9;
else
*a2 = v9;
--*(_DWORD *)(a1 + 8);
return result;
}

original_code:
int remove_info(list_head * head, list_tail ** tail, const char * addr,
                unsigned int addr_length){
  server_list * node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(memcmp(node->server_addr,addr,addr_length) == 0){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      head->alength -= 1;
      return 0;
    }
    node = node->next;
  }
  return -1;
}

canonical_original_code:
int remove_info(struct node *head, struct node **tail, char *addr, unsigned int addr_length){
  struct node *node;
  if(head == ((void *)0) || addr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(memcmp(node->server_addr,addr,addr_length) == 0){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      head->alength -= 1;
      return 0;
    }
    node = node->next;
  }
  return -1;
}


original_code:
int remove_info_ptr(list_head * head, list_tail ** tail, void *ptr){
  server_list * node;
  if(head == ((void *)0) || ptr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->data == ptr){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      head->alength -= 1;
    }
    node = node->next;
  }
  return -1;
}

canonical_original_code:
int remove_info_ptr(struct node *head, struct node **tail, void *ptr){
  struct node *node;
  if(head == ((void *)0) || ptr == ((void *)0))
    return -1;
  node = head->next;
  while(node){
    if(node->data == ptr){
      node->prev->next = node->next;
      if(node->next)
        node->next->prev = node->prev;
      else
        *tail = node->prev;
      head->alength -= 1;
    }
    node = node->next;
  }
  return -1;
}

variable_types:
ptr
void *


original_code:
void free_list(list_head * head)
{
  if(head == ((void *)0))
    return;
  server_list * node = head->next;
  while(node){
    head->next = node->next;
    if(node->server_addr) free(node->server_addr);
    if(node->data) free(node->data);
    if(node) free(node);
    node = head->next;
  }
  head->alength = 0;
  head->next = ((void *)0);
  head->prev = ((void *)0);
}

canonical_original_code:
void free_list(struct node *head)
{
  if(head == ((void *)0))
    return;
  struct node *node = head->next;
  while(node){
    head->next = node->next;
    if(node->server_addr) free(node->server_addr);
    if(node->data) free(node->data);
    if(node) free(node);
    node = head->next;
  }
  head->alength = 0;
  head->next = ((void *)0);
  head->prev = ((void *)0);
}


decompiled_code:
__int64 init_mem_serv_list()
{
if ( malloc(0x30uLL) )
return 0LL;
fwrite("\nCould not initialise memory servers list", 1uLL, 0x29uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func0()
{
if ( malloc(0x30uLL) )
return 0LL;
fwrite("\nCould not initialise memory servers list", 1uLL, 0x29uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int init_mem_serv_list(mem_serv* mem_serv_head) {
  mem_serv_head = (mem_serv*) malloc(sizeof(mem_serv));
  if(mem_serv_head == ((void *)0)) {
    fprintf(stderr, "\nCould not initialise memory servers list");
    return -1;
  }
  strcpy(mem_serv_head->ip, "");
  mem_serv_head->is_assigned = -1;
  mem_serv_head->is_synced = -1;
  mem_serv_head->count = -1;
  mem_serv_head->next = ((void *)0);
  mem_serv_head->prev = ((void *)0);
  return 0;
}

canonical_original_code:
int init_mem_serv_list(struct _mem_serv *mem_serv_head) {
  mem_serv_head = (struct _mem_serv *) malloc(sizeof(mem_serv));
  if(mem_serv_head == ((void *)0)) {
    fprintf(stderr, "\nCould not initialise memory servers list");
    return -1;
  }
  strcpy(mem_serv_head->ip, "");
  mem_serv_head->is_assigned = -1;
  mem_serv_head->is_synced = -1;
  mem_serv_head->count = -1;
  mem_serv_head->next = ((void *)0);
  mem_serv_head->prev = ((void *)0);
  return 0;
}


decompiled_code:
void __fastcall free_mem_serv_list(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

if ( ptr )
{
v1 = ptr;
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 4);
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v1; // rbx
void *v2; // rdi

if ( ptr )
{
v1 = ptr;
do
{
v2 = v1;
v1 = (void *)*((_QWORD *)v1 + 4);
free(v2);
}
while ( v1 );
}
}

original_code:
void free_mem_serv_list(mem_serv *mem_serv_head) {
    mem_serv *temp;
    while(mem_serv_head != ((void *)0)) {
        temp = mem_serv_head;
        mem_serv_head = mem_serv_head->next;
        free(temp);
    }
    return;
}

canonical_original_code:
void free_mem_serv_list(struct _mem_serv *mem_serv_head) {
    struct _mem_serv *temp;
    while(mem_serv_head != ((void *)0)) {
        temp = mem_serv_head;
        mem_serv_head = mem_serv_head->next;
        free(temp);
    }
    return;
}