decompiled_code:
const char *__fastcall selstr_local_address(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_0, 0x80u);
if ( result )
result = str_0;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_0, 0x80u);
if ( result )
result = str_0;
}
return result;
}

original_code:
static char * selstr_local_address(ScarabStream * s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
        return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in * sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}

canonical_original_code:
static char *selstr_local_address(struct __ScarabStream *s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
        return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}


repo: hahong/pymworks

decompiled_code:
__int64 __fastcall selstr_local_port(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

original_code:
static int selstr_local_port(ScarabStream * s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
        return -1;
    }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in * sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

canonical_original_code:
static int selstr_local_port(struct __ScarabStream *s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
        return -1;
    }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in *sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

variable_types: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } *

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

variable_types: len int

variable_types: struct __tcpstr { struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } header; int fd; unsigned int packet_size; } *

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

return_type: int

user_defined_types: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession *session; struct __ScarabStreamEngine *engine; }

user_defined_types: struct __tcpstr { struct __ScarabStream header; int fd; unsigned int packet_size; }

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

user_defined_types: struct __ScarabSession { struct __ScarabEncoderEngine *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; }

user_defined_types: struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; }

user_defined_types: struct __ScarabList { int size; struct __ScarabDatum **values; }

user_defined_types: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque opaque; struct __ScarabDict *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t *mutex; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
int __fastcall selstr_read(__int64 a1, char *a2, int a3)
{
int v5; // er14
int v6; // er12
int result; // eax
int v8; // edi
int v9; // edi
int v10; // eax
struct timeval v11; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v5 = a3;
memset(&readfds, 0, sizeof(readfds));
v6 = a3;
do
{
v8 = *(_DWORD *)(a1 + 24);
readfds.fds_bits[v8 / 64] |= 1LL << (v8 % 64);
v11.tv_sec = 5LL;
v11.tv_usec = 0LL;
result = select(v8 + 1, &readfds, 0LL, 0LL, &v11);
if ( result < 0 )
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 9));
if ( !result )
return result;
v9 = *(_DWORD *)(a1 + 24);
if ( (readfds.fds_bits[v9 / 64] & (1LL << (v9 % 64))) != 0 )
{
v10 = recv(v9, a2, v6, 0);
if ( v10 < 0 )
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 2));
if ( !v10 )
{
READ_FUBAR_FLAG = 1;
return -1;
}
v6 -= v10;
a2 += v10;
}
}
while ( v6 );
return v5;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1, char *a2, int a3)
{
int v5; // er14
int v6; // er12
int result; // eax
int v8; // edi
int v9; // edi
int v10; // eax
struct timeval v11; // [rsp+0h] [rbp-B8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-A8h] BYREF

v5 = a3;
memset(&readfds, 0, sizeof(readfds));
v6 = a3;
do
{
v8 = *(_DWORD *)(a1 + 24);
readfds.fds_bits[v8 / 64] |= 1LL << (v8 % 64);
v11.tv_sec = 5LL;
v11.tv_usec = 0LL;
result = select(v8 + 1, &readfds, 0LL, 0LL, &v11);
if ( result < 0 )
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 9));
if ( !result )
return result;
v9 = *(_DWORD *)(a1 + 24);
if ( (readfds.fds_bits[v9 / 64] & (1LL << (v9 % 64))) != 0 )
{
v10 = recv(v9, a2, v6, 0);
if ( v10 < 0 )
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 2));
if ( !v10 )
{
READ_FUBAR_FLAG = 1;
return -1;
}
v6 -= v10;
a2 += v10;
}
}
while ( v6 );
return v5;
}

original_code:
static int selstr_read(ScarabStream * stream, void *buffer, int size) {
 tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 int tot;
    fd_set readSet;
    struct timeval selectTimeout;
    int select_rc;
 tot = size;
 c = (unsigned char *)buffer;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readSet)->__fds_bits)[0]) : "memory"); } while (0);
 do {
        ((void) (((&readSet)->__fds_bits)[((ts->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->fd) % (8 * (int) sizeof (__fd_mask)))))));
        selectTimeout.tv_sec = 5;
        selectTimeout.tv_usec = 0;
        select_rc = select(ts->fd + 1, &readSet, ((void *)0), ((void *)0), &selectTimeout);
        if(select_rc < 0) {
            return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        }
        if(select_rc == 0) {
            return 0;
        }
        if(((((&readSet)->__fds_bits)[((ts->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            r = recv(ts->fd, c, size, 0);
            if (r < 0) {
                return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (2) ));
            }
            if(r == 0) {
                READ_FUBAR_FLAG = 1;
                return -1;
            }
            size -= r;
            c += r;
        }
    } while (size);
 return tot;
}

canonical_original_code:
static int selstr_read(struct __ScarabStream *stream, void *buffer, int size) {
 struct __tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 int tot;
    struct fd_set readSet;
    struct timeval selectTimeout;
    int select_rc;
 tot = size;
 c = (unsigned char *)buffer;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readSet)->__fds_bits)[0]) : "memory"); } while (0);
 do {
        ((void) (((&readSet)->__fds_bits)[((ts->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((ts->fd) % (8 * (int) sizeof (__fd_mask)))))));
        selectTimeout.tv_sec = 5;
        selectTimeout.tv_usec = 0;
        select_rc = select(ts->fd + 1, &readSet, ((void *)0), ((void *)0), &selectTimeout);
        if(select_rc < 0) {
            return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        }
        if(select_rc == 0) {
            return 0;
        }
        if(((((&readSet)->__fds_bits)[((ts->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ts->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            r = recv(ts->fd, c, size, 0);
            if (r < 0) {
                return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (2) ));
            }
            if(r == 0) {
                READ_FUBAR_FLAG = 1;
                return -1;
            }
            size -= r;
            c += r;
        }
    } while (size);
 return tot;
}

variable_types:
buffer
void *

variable_types: size int

variable_types:
c
unsigned char *

variable_types: r int

variable_types: tot int

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

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

user_defined_types: struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); }

user_defined_types: struct __ScarabOpaque { int size; unsigned char *data; }

user_defined_types: struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); }

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


decompiled_code:
__int64 __fastcall selstr_write(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er13
unsigned int v5; // ebx
unsigned int v6; // edx
int v7; // eax

v4 = a3;
v5 = a3;
while ( 1 )
{
v6 = v5;
if ( *(_DWORD *)(a1 + 28) <= v5 )
v6 = *(_DWORD *)(a1 + 28);
v7 = send(*(_DWORD *)(a1 + 24), a2, (int)v6, 0);
if ( v7 < 0 )
break;
a2 += v7;
v5 -= v7;
if ( !v5 )
return v4;
}
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 1));
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er13
unsigned int v5; // ebx
unsigned int v6; // edx
int v7; // eax

v4 = a3;
v5 = a3;
while ( 1 )
{
v6 = v5;
if ( *(_DWORD *)(a1 + 28) <= v5 )
v6 = *(_DWORD *)(a1 + 28);
v7 = send(*(_DWORD *)(a1 + 24), a2, (int)v6, 0);
if ( v7 < 0 )
break;
a2 += v7;
v5 -= v7;
if ( !v5 )
return v4;
}
return scarab_session_seterr(*(_QWORD *)(a1 + 8), (unsigned int)(scarab_stream_tcpip_select[1] + 1));
}

original_code:
static int selstr_write(ScarabStream * stream, const void *buffer, int size) {
 tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 unsigned int left;
 left = (unsigned int)size;
 c = (unsigned char *)buffer;
 do {
        r = send(ts->fd, c, (int)((left) < (ts->packet_size) ? (left) : (ts->packet_size)), 0);
            if (r < 0) {
                return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (1) ));
            }
  left -= r;
  c += r;
 } while (left);
 return size;
}

canonical_original_code:
static int selstr_write(struct __ScarabStream *stream, void *buffer, int size) {
 struct __tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 unsigned int left;
 left = (unsigned int)size;
 c = (unsigned char *)buffer;
 do {
        r = send(ts->fd, c, (int)((left) < (ts->packet_size) ? (left) : (ts->packet_size)), 0);
            if (r < 0) {
                return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip_select)->err_code_base + (1) ));
            }
  left -= r;
  c += r;
 } while (left);
 return size;
}

variable_types:
left
unsigned int


decompiled_code:
__int64 __fastcall selstr_close(__int64 a1)
{
shutdown(*(_DWORD *)(a1 + 24), 2);
close(*(_DWORD *)(a1 + 24));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
shutdown(*(_DWORD *)(a1 + 24), 2);
close(*(_DWORD *)(a1 + 24));
return 0LL;
}

original_code:
static int selstr_close(ScarabStream * stream) {
 tcpstr *ts = ((tcpstr*)(stream));
    shutdown(ts->fd, 2);
 close(ts->fd);
 return 0;
}

canonical_original_code:
static int selstr_close(struct __ScarabStream *stream) {
 struct __tcpstr *ts = ((tcpstr*)(stream));
    shutdown(ts->fd, 2);
 close(ts->fd);
 return 0;
}


decompiled_code:
__int64 __fastcall selstr_connect(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // rax
int v5; // ebx
__int64 v6; // rdx
unsigned __int8 *v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rdx
void *v10; // rax
__int64 v11; // r13
unsigned __int8 *v12; // rbx
__int16 v13; // bp
struct protoent *v14; // rbx
struct hostent *v15; // rax
int v16; // edi
struct sockaddr v18; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
if ( *a2 == 47 )
{
if ( a2[1] == 47 )
v3 = a2 + 2;
else
v3 = a2 + 1;
}
v4 = *v3;
if ( (unsigned __int8)v4 > 0x3Au )
{
v7 = v3;
}
else
{
v5 = 0;
v6 = 0x400800000000001LL;
if ( _bittest64(&v6, v4) )
goto LABEL_12;
v7 = v3;
}
v8 = 0x400800000000001LL;
do
{
do
{
v5 = 1 - (_DWORD)v3 + (_DWORD)v7++;
v9 = *v7;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v8, v9) );
LABEL_12:
v10 = (void *)scarab_mem_malloc((unsigned int)(v5 + 1));
v11 = (__int64)v10;
if ( v10 )
{
memcpy(v10, v3, v5);
*(_BYTE *)(v11 + v5) = 0;
v12 = &v3[v5];
v13 = 0;
if ( *v12 == 58 )
v13 = atoi((const char *)v12 + 1);
v14 = getprotobyname("tcp");
if ( !v14 )
return 0LL;
*(_QWORD *)&v18.sa_family = 2LL;
*(_QWORD *)&v18.sa_data[6] = 0LL;
*(_WORD *)v18.sa_data = _bswap_16(v13);
v15 = gethostbyname((const char *)v11);
if ( v15 )
{
*(_DWORD *)&v18.sa_data[2] = **(_DWORD **)v15->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 3));
*(_DWORD *)&v18.sa_data[2] = inet_addr((const char *)v11);
if ( !*(_DWORD *)&v18.sa_data[2] )
return 0LL;
}
v11 = scarab_mem_malloc(32LL);
if ( v11 )
{
v16 = socket(2, 1, v14->p_proto);
*(_DWORD *)(v11 + 24) = v16;
if ( v16 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
v11 = 0LL;
}
else if ( connect(v16, &v18, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 5));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else
{
*(_DWORD *)(v11 + 28) = -1;
}
}
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // rax
int v5; // ebx
__int64 v6; // rdx
unsigned __int8 *v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rdx
void *v10; // rax
__int64 v11; // r13
unsigned __int8 *v12; // rbx
__int16 v13; // bp
struct protoent *v14; // rbx
struct hostent *v15; // rax
int v16; // edi
struct sockaddr v18; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
if ( *a2 == 47 )
{
if ( a2[1] == 47 )
v3 = a2 + 2;
else
v3 = a2 + 1;
}
v4 = *v3;
if ( (unsigned __int8)v4 > 0x3Au )
{
v7 = v3;
}
else
{
v5 = 0;
v6 = 0x400800000000001LL;
if ( _bittest64(&v6, v4) )
goto LABEL_12;
v7 = v3;
}
v8 = 0x400800000000001LL;
do
{
do
{
v5 = 1 - (_DWORD)v3 + (_DWORD)v7++;
v9 = *v7;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v8, v9) );
LABEL_12:
v10 = (void *)scarab_mem_malloc((unsigned int)(v5 + 1));
v11 = (__int64)v10;
if ( v10 )
{
memcpy(v10, v3, v5);
*(_BYTE *)(v11 + v5) = 0;
v12 = &v3[v5];
v13 = 0;
if ( *v12 == 58 )
v13 = atoi((const char *)v12 + 1);
v14 = getprotobyname("tcp");
if ( !v14 )
return 0LL;
*(_QWORD *)&v18.sa_family = 2LL;
*(_QWORD *)&v18.sa_data[6] = 0LL;
*(_WORD *)v18.sa_data = func13(v13);
v15 = gethostbyname((const char *)v11);
if ( v15 )
{
*(_DWORD *)&v18.sa_data[2] = **(_DWORD **)v15->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 3));
*(_DWORD *)&v18.sa_data[2] = inet_addr((const char *)v11);
if ( !*(_DWORD *)&v18.sa_data[2] )
return 0LL;
}
v11 = scarab_mem_malloc(32LL);
if ( v11 )
{
v16 = socket(2, 1, v14->p_proto);
*(_DWORD *)(v11 + 24) = v16;
if ( v16 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
v11 = 0LL;
}
else if ( connect(v16, &v18, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 5));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else
{
*(_DWORD *)(v11 + 28) = -1;
}
}
}
return v11;
}

original_code:
static ScarabStream * selstr_connect(ScarabSession * session, const char *uri) {
 tcpstr *ts;
 struct protoent *proto;
 struct hostent *host;
 struct sockaddr_in end_point;
 char *host_name;
 char *c;
 int len;
 short tcp_port = 0;
 len = 0;
 if ('/' == *uri)
  uri++;
 if ('/' == *uri)
  uri++;
 for (c = (char *)uri; *c != 0 && *c != ':' && *c != '/'; c++)
 {
  len++;
 }
 host_name = (char *)scarab_mem_malloc(len + 1);
 if (!host_name)
  return ((void *)0);
 memcpy(host_name, uri, len);
 host_name[len] = 0;
 uri += len;
 if (':' == *uri)
 {
  tcp_port = atoi(uri + 1);
 }
 proto = getprotobyname("tcp");
 if (!proto)
  return ((void *)0);
 memset(&end_point, 0, sizeof(end_point));
 end_point.sin_family = 2;
 end_point.sin_port = htons(tcp_port);
 host = gethostbyname(host_name);
 if (host) {
  memcpy(&end_point.sin_addr, host->h_addr_list[0],
      sizeof(end_point.sin_addr));
 } else {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (3) ));
  end_point.sin_addr.s_addr = inet_addr(host_name);
  if (0 == end_point.sin_addr.s_addr)
   return ((void *)0);
 }
 ts = (tcpstr*)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts)
  return ((void *)0);
 ts->fd = socket(2, SOCK_STREAM, proto->p_proto);
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  return ((void *)0);
 }
 if (connect(ts->fd, (struct sockaddr *)&end_point,
                                                    sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (5) ));
  close(ts->fd);
  return ((void *)0);
 }
 ts->packet_size = (unsigned int)-1;
 return &ts->header;
}

canonical_original_code:
static struct __ScarabStream *selstr_connect(struct __ScarabSession *session, char *uri) {
 struct __tcpstr *ts;
 struct protoent *proto;
 struct hostent *host;
 struct sockaddr_in end_point;
 char *host_name;
 char *c;
 int len;
 short tcp_port = 0;
 len = 0;
 if ('/' == *uri)
  uri++;
 if ('/' == *uri)
  uri++;
 for (c = (char *)uri; *c != 0 && *c != ':' && *c != '/'; c++)
 {
  len++;
 }
 host_name = (char *)scarab_mem_malloc(len + 1);
 if (!host_name)
  return ((void *)0);
 memcpy(host_name, uri, len);
 host_name[len] = 0;
 uri += len;
 if (':' == *uri)
 {
  tcp_port = atoi(uri + 1);
 }
 proto = getprotobyname("tcp");
 if (!proto)
  return ((void *)0);
 memset(&end_point, 0, sizeof(end_point));
 end_point.sin_family = 2;
 end_point.sin_port = htons(tcp_port);
 host = gethostbyname(host_name);
 if (host) {
  memcpy(&end_point.sin_addr, host->h_addr_list[0],
      sizeof(end_point.sin_addr));
 } else {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (3) ));
  end_point.sin_addr.s_addr = inet_addr(host_name);
  if (0 == end_point.sin_addr.s_addr)
   return ((void *)0);
 }
 ts = (struct __tcpstr *)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts)
  return ((void *)0);
 ts->fd = socket(2, SOCK_STREAM, proto->p_proto);
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  return ((void *)0);
 }
 if (connect(ts->fd, (struct sockaddr *)&end_point,
                                                    sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (5) ));
  close(ts->fd);
  return ((void *)0);
 }
 ts->packet_size = (unsigned int)-1;
 return &ts->header;
}

variable_types:
session
struct __ScarabSession *

variable_types:
uri
char *

variable_types: struct protoent { char *p_name; char **p_aliases; int p_proto; } *

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

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:
c
char *

return_type: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } *


decompiled_code:
__int64 __fastcall selstr_accept(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rbx
int v6; // edi
int v7; // eax
int v8; // edi
int v9; // eax
struct timeval v11; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+9Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr_len = 16;
v5 = scarab_mem_malloc(32LL);
if ( v5 )
{
memset(&readfds, 0, sizeof(readfds));
v6 = *(_DWORD *)(a3 + 24);
readfds.fds_bits[v6 / 64] |= 1LL << (v6 % 64);
v11.tv_sec = 5LL;
v11.tv_usec = 0LL;
v7 = select(v6 + 1, &readfds, 0LL, 0LL, &v11);
if ( v7 )
{
if ( v7 >= 0 && (v8 = *(_DWORD *)(a3 + 24), (readfds.fds_bits[v8 / 64] & (1LL << (v8 % 64))) != 0) )
{
v9 = accept(v8, &addr, &addr_len);
*(_DWORD *)(v5 + 24) = v9;
if ( v9 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 5));
v5 = 0LL;
}
else
{
*(_DWORD *)(v5 + 28) = -1;
}
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 9));
v5 = 0LL;
}
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 10));
v5 = 0LL;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v5; // rbx
int v6; // edi
int v7; // eax
int v8; // edi
int v9; // eax
struct timeval v11; // [rsp+0h] [rbp-C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-B8h] BYREF
socklen_t addr_len; // [rsp+9Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr_len = 16;
v5 = scarab_mem_malloc(32LL);
if ( v5 )
{
memset(&readfds, 0, sizeof(readfds));
v6 = *(_DWORD *)(a3 + 24);
readfds.fds_bits[v6 / 64] |= 1LL << (v6 % 64);
v11.tv_sec = 5LL;
v11.tv_usec = 0LL;
v7 = select(v6 + 1, &readfds, 0LL, 0LL, &v11);
if ( v7 )
{
if ( v7 >= 0 && (v8 = *(_DWORD *)(a3 + 24), (readfds.fds_bits[v8 / 64] & (1LL << (v8 % 64))) != 0) )
{
v9 = accept(v8, &addr, &addr_len);
*(_DWORD *)(v5 + 24) = v9;
if ( v9 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 5));
v5 = 0LL;
}
else
{
*(_DWORD *)(v5 + 28) = -1;
}
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 9));
v5 = 0LL;
}
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 10));
v5 = 0LL;
}
}
return v5;
}

original_code:
static ScarabStream *
selstr_accept(ScarabSession * session, const char *uri, ScarabStream * binding) {
 tcpstr *ts;
 tcpstr *bd;
 struct sockaddr_in end_point;
 int len;
    fd_set read_set;
    int maxfdplus1;
    struct timeval selectTimeout;
    int select_rc;
    select_rc = 0;
 bd = ((tcpstr*)(binding));
 memset(&end_point, 0, sizeof(end_point));
 len = sizeof(end_point);
 ts = (tcpstr*)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts)
  return ((void *)0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&read_set)->__fds_bits)[((bd->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((bd->fd) % (8 * (int) sizeof (__fd_mask)))))));
    maxfdplus1 = bd->fd + 1;
    selectTimeout.tv_sec = 5;
    selectTimeout.tv_usec = 0;
    select_rc = select(maxfdplus1, &read_set, ((void *)0), ((void *)0), &selectTimeout);
    if(select_rc == 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (10) ));
        return ((void *)0);
    }
    if(select_rc < 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        return ((void *)0);
    }
    if(!((((&read_set)->__fds_bits)[((bd->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((bd->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        return ((void *)0);
    }
    ts->fd = accept(bd->fd, (struct sockaddr *)&end_point, (socklen_t*)(&len));
    if (ts->fd < 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (5) ));
        return ((void *)0);
    }
    ts->packet_size = (unsigned int)-1;
    return &ts->header;
}

canonical_original_code:
static struct __ScarabStream *selstr_accept(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding) {
 struct __tcpstr *ts;
 struct __tcpstr *bd;
 struct sockaddr_in end_point;
 int len;
    struct fd_set read_set;
    int maxfdplus1;
    struct timeval selectTimeout;
    int select_rc;
    select_rc = 0;
 bd = ((struct __tcpstr *)(binding));
 memset(&end_point, 0, sizeof(end_point));
 len = sizeof(end_point);
 ts = (struct __tcpstr *)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts)
  return ((void *)0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_set)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&read_set)->__fds_bits)[((bd->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((bd->fd) % (8 * (int) sizeof (__fd_mask)))))));
    maxfdplus1 = bd->fd + 1;
    selectTimeout.tv_sec = 5;
    selectTimeout.tv_usec = 0;
    select_rc = select(maxfdplus1, &read_set, ((void *)0), ((void *)0), &selectTimeout);
    if(select_rc == 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (10) ));
        return ((void *)0);
    }
    if(select_rc < 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        return ((void *)0);
    }
    if(!((((&read_set)->__fds_bits)[((bd->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((bd->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (9) ));
        return ((void *)0);
    }
    ts->fd = accept(bd->fd, (struct sockaddr *)&end_point, (unsigned int *)(&len));
    if (ts->fd < 0) {
        scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (5) ));
        return ((void *)0);
    }
    ts->packet_size = (unsigned int)-1;
    return &ts->header;
}

variable_types:
binding
struct __ScarabStream *

variable_types: len int

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


setNonBlocking

decompiled_code:
int __fastcall setNonBlocking(int fd)
{
int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result >= 0 )
{
BYTE1(result) |= 8u;
result = fcntl(fd, 4, (unsigned int)result);
}
return result;
}

canonical_decompiled_code:
int __fastcall func17(int fd)
{
int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result >= 0 )
{
BYTE1(result) |= 8u;
result = fcntl(fd, 4, (unsigned int)result);
}
return result;
}

original_code:
int setNonBlocking(int fd) {
    int flags;
    if((flags = fcntl(fd, 3, 0)) < 0) {
        return flags;
    }
    flags |= 04000;
    return fcntl(fd, 4, flags);
}

variable_types: fd int

variable_types: flags int


decompiled_code:
__int64 __fastcall selstr_bind(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // rax
int v5; // ebx
__int64 v6; // rdx
unsigned __int8 *v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rdx
void *v10; // rax
__int64 v11; // r13
unsigned __int8 *v12; // rbx
__int16 v13; // bp
struct protoent *v14; // rbx
struct hostent *v15; // rax
int v16; // edi
in_addr_t v18; // eax
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

v3 = a2;
if ( *a2 == 47 )
{
if ( a2[1] == 47 )
v3 = a2 + 2;
else
v3 = a2 + 1;
}
v4 = *v3;
if ( (unsigned __int8)v4 > 0x3Au )
{
v7 = v3;
}
else
{
v5 = 0;
v6 = 0x400800000000001LL;
if ( _bittest64(&v6, v4) )
goto LABEL_12;
v7 = v3;
}
v8 = 0x400800000000001LL;
do
{
do
{
v5 = 1 - (_DWORD)v3 + (_DWORD)v7++;
v9 = *v7;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v8, v9) );
LABEL_12:
v10 = (void *)scarab_mem_malloc((unsigned int)(v5 + 1));
v11 = (__int64)v10;
if ( v10 )
{
memcpy(v10, v3, v5);
*(_BYTE *)(v11 + v5) = 0;
v12 = &v3[v5];
v13 = 0;
if ( *v12 == 58 )
v13 = atoi((const char *)v12 + 1);
v14 = getprotobyname("tcp");
if ( v14 )
{
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(v13);
v15 = gethostbyname((const char *)v11);
if ( v15 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v15->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 3));
v18 = inet_addr((const char *)v11);
if ( v18 )
*(_DWORD *)&addr.sa_data[2] = v18;
else
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
}
v11 = scarab_mem_malloc(32LL);
if ( v11 )
{
v16 = socket(2, 1, v14->p_proto);
*(_DWORD *)(v11 + 24) = v16;
if ( v16 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
v11 = 0LL;
}
else if ( setNonBlocking(v16) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else
{
optval = 1;
setsockopt(*(_DWORD *)(v11 + 24), 1, 2, &optval, 4u);
if ( bind(*(_DWORD *)(v11 + 24), &addr, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 6));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else if ( listen(*(_DWORD *)(v11 + 24), 5) )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 7));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
}
}
}
else
{
v11 = 0LL;
}
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, unsigned __int8 *a2)
{
unsigned __int8 *v3; // rbp
unsigned __int64 v4; // rax
int v5; // ebx
__int64 v6; // rdx
unsigned __int8 *v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rdx
void *v10; // rax
__int64 v11; // r13
unsigned __int8 *v12; // rbx
__int16 v13; // bp
struct protoent *v14; // rbx
struct hostent *v15; // rax
int v16; // edi
in_addr_t v18; // eax
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

v3 = a2;
if ( *a2 == 47 )
{
if ( a2[1] == 47 )
v3 = a2 + 2;
else
v3 = a2 + 1;
}
v4 = *v3;
if ( (unsigned __int8)v4 > 0x3Au )
{
v7 = v3;
}
else
{
v5 = 0;
v6 = 0x400800000000001LL;
if ( _bittest64(&v6, v4) )
goto LABEL_12;
v7 = v3;
}
v8 = 0x400800000000001LL;
do
{
do
{
v5 = 1 - (_DWORD)v3 + (_DWORD)v7++;
v9 = *v7;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v8, v9) );
LABEL_12:
v10 = (void *)scarab_mem_malloc((unsigned int)(v5 + 1));
v11 = (__int64)v10;
if ( v10 )
{
memcpy(v10, v3, v5);
*(_BYTE *)(v11 + v5) = 0;
v12 = &v3[v5];
v13 = 0;
if ( *v12 == 58 )
v13 = atoi((const char *)v12 + 1);
v14 = getprotobyname("tcp");
if ( v14 )
{
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func13(v13);
v15 = gethostbyname((const char *)v11);
if ( v15 )
{
*(_DWORD *)&addr.sa_data[2] = **(_DWORD **)v15->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 3));
v18 = inet_addr((const char *)v11);
if ( v18 )
*(_DWORD *)&addr.sa_data[2] = v18;
else
*(_DWORD *)&addr.sa_data[2] = func16(0);
}
v11 = scarab_mem_malloc(32LL);
if ( v11 )
{
v16 = socket(2, 1, v14->p_proto);
*(_DWORD *)(v11 + 24) = v16;
if ( v16 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
v11 = 0LL;
}
else if ( func17(v16) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 4));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else
{
optval = 1;
setsockopt(*(_DWORD *)(v11 + 24), 1, 2, &optval, 4u);
if ( bind(*(_DWORD *)(v11 + 24), &addr, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 6));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
else if ( listen(*(_DWORD *)(v11 + 24), 5) )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip_select[1] + 7));
close(*(_DWORD *)(v11 + 24));
v11 = 0LL;
}
}
}
}
else
{
v11 = 0LL;
}
}
return v11;
}

original_code:
static ScarabStream * selstr_bind(ScarabSession * session, const char *uri) {
 tcpstr *ts;
 struct protoent *proto;
 struct hostent *host;
 struct sockaddr_in end_point;
 char *host_name;
 char *c;
 int len;
    int turn_on;
 short tcp_port = 0;
 len = 0;
 if ('/' == *uri)
  uri++;
 if ('/' == *uri)
  uri++;
 for (c = (char *)uri; *c != 0 && *c != ':' && *c != '/'; c++) {
  len++;
 }
 host_name = (char*)scarab_mem_malloc(len + 1);
 if (!host_name)
  return ((void *)0);
 memcpy(host_name, uri, len);
 host_name[len] = 0;
 uri += len;
 if (':' == *uri) {
  tcp_port = atoi(uri + 1);
 }
 proto = getprotobyname("tcp");
 if (!proto)
  return ((void *)0);
 memset(&end_point, 0, sizeof(end_point));
 end_point.sin_family = 2;
 end_point.sin_port = htons(tcp_port);
 host = gethostbyname(host_name);
 if (host) {
  memcpy(&end_point.sin_addr, host->h_addr_list[0],
                                                 sizeof(end_point.sin_addr));
 } else {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (3) ));
  end_point.sin_addr.s_addr = inet_addr(host_name);
  if (0 == end_point.sin_addr.s_addr)
   end_point.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 }
 ts = (tcpstr*)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts) return ((void *)0);
    ts->fd = socket(2, SOCK_STREAM, proto->p_proto);
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  return ((void *)0);
 }
    if(setNonBlocking(ts->fd) < 0 ) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  close(ts->fd);
  return ((void *)0);
    }
    turn_on = 1;
    setsockopt(ts->fd, 1, 2, &turn_on, sizeof(int));
 if (bind(ts->fd, (struct sockaddr *)&end_point, sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (6) ));
  close(ts->fd);
  return ((void *)0);
 }
 if (listen(ts->fd, 5) != 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (7) ));
  close(ts->fd);
  return ((void *)0);
 }
 return &ts->header;
}

canonical_original_code:
static struct __ScarabStream *selstr_bind(struct __ScarabSession *session, char *uri) {
 struct __tcpstr *ts;
 struct protoent *proto;
 struct hostent *host;
 struct sockaddr_in end_point;
 char *host_name;
 char *c;
 int len;
    int turn_on;
 short tcp_port = 0;
 len = 0;
 if ('/' == *uri)
  uri++;
 if ('/' == *uri)
  uri++;
 for (c = (char *)uri; *c != 0 && *c != ':' && *c != '/'; c++) {
  len++;
 }
 host_name = (char *)scarab_mem_malloc(len + 1);
 if (!host_name)
  return ((void *)0);
 memcpy(host_name, uri, len);
 host_name[len] = 0;
 uri += len;
 if (':' == *uri) {
  tcp_port = atoi(uri + 1);
 }
 proto = getprotobyname("tcp");
 if (!proto)
  return ((void *)0);
 memset(&end_point, 0, sizeof(end_point));
 end_point.sin_family = 2;
 end_point.sin_port = htons(tcp_port);
 host = gethostbyname(host_name);
 if (host) {
  memcpy(&end_point.sin_addr, host->h_addr_list[0],
                                                 sizeof(end_point.sin_addr));
 } else {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (3) ));
  end_point.sin_addr.s_addr = inet_addr(host_name);
  if (0 == end_point.sin_addr.s_addr)
   end_point.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 }
 ts = (struct __tcpstr *)scarab_mem_malloc(sizeof(tcpstr));
 if (!ts) return ((void *)0);
    ts->fd = socket(2, SOCK_STREAM, proto->p_proto);
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  return ((void *)0);
 }
    if(setNonBlocking(ts->fd) < 0 ) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (4) ));
  close(ts->fd);
  return ((void *)0);
    }
    turn_on = 1;
    setsockopt(ts->fd, 1, 2, &turn_on, sizeof(int));
 if (bind(ts->fd, (struct sockaddr *)&end_point, sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (6) ));
  close(ts->fd);
  return ((void *)0);
 }
 if (listen(ts->fd, 5) != 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip_select)->err_code_base + (7) ));
  close(ts->fd);
  return ((void *)0);
 }
 return &ts->header;
}

variable_types: struct __tcpstr { struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } header; int fd; unsigned int packet_size; } *

user_defined_types: struct protoent { char *p_name; char **p_aliases; int p_proto; }

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


turnOffNonBlocking

decompiled_code:
int __fastcall turnOffNonBlocking(int fd)
{
int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result >= 0 )
{
BYTE1(result) &= 0xF7u;
result = fcntl(fd, 4, (unsigned int)result);
}
return result;
}

canonical_decompiled_code:
int __fastcall func19(int fd)
{
int result; // eax

result = fcntl(fd, 3, 0LL);
if ( result >= 0 )
{
BYTE1(result) &= 0xF7u;
result = fcntl(fd, 4, (unsigned int)result);
}
return result;
}

original_code:
int turnOffNonBlocking(int fd) {
    int flags;
    if((flags = fcntl(fd, 3, 0)) < 0) {
        return flags;
    }
    flags &= ~04000;
    return fcntl(fd, 4, flags);
}


decompiled_code:
void __fastcall scarab_init_lock(__int64 a1)
{
pthread_mutex_t *v1; // rax

if ( a1 )
{
v1 = (pthread_mutex_t *)scarab_mem_malloc(40LL);
*(_QWORD *)(a1 + 32) = v1;
pthread_mutex_init(v1, 0LL);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
pthread_mutex_t *v1; // rax

if ( a1 )
{
v1 = (pthread_mutex_t *)scarab_mem_malloc(40LL);
*(_QWORD *)(a1 + 32) = v1;
pthread_mutex_init(v1, 0LL);
}
}

original_code:
ScarabDatum *scarab_init_lock(ScarabDatum *datum){
  if(datum != ((void *)0)){
   datum->mutex =
    (pthread_mutex_t *)scarab_mem_malloc(sizeof(pthread_mutex_t));
   pthread_mutex_init(datum->mutex, ((void *)0));
  }
}

canonical_original_code:
struct __ScarabDatum *scarab_init_lock(struct __ScarabDatum *datum){
  if(datum != ((void *)0)){
   datum->mutex =
    (union pthread_mutex_t *)scarab_mem_malloc(sizeof(pthread_mutex_t));
   pthread_mutex_init(datum->mutex, ((void *)0));
  }
}

variable_types:
datum
struct __ScarabDatum *

return_type: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList { int size; struct __ScarabDatum **values; } *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } *


decompiled_code:
__int64 scarab_new_atomic()
{
__int64 v0; // rbx

v0 = scarab_mem_malloc(40LL);
*(_DWORD *)(v0 + 24) = 0;
*(_DWORD *)(v0 + 28) = 1;
scarab_init_lock(v0);
return v0;
}

original_code:
ScarabDatum *
scarab_new_atomic()
{
 ScarabDatum *datum;
 datum=(ScarabDatum*)scarab_mem_malloc(sizeof(ScarabDatum));
 datum->type=0;
 datum->ref_count = 1;
 scarab_init_lock(datum);
 return datum;
}

canonical_original_code:
struct __ScarabDatum *scarab_new_atomic()
{
 struct __ScarabDatum *datum;
 datum=(struct __ScarabDatum *)scarab_mem_malloc(sizeof(ScarabDatum));
 datum->type=0;
 datum->ref_count = 1;
 scarab_init_lock(datum);
 return datum;
}

variable_types: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList { int size; struct __ScarabDatum **values; } *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } *


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

result = scarab_new_atomic();
*(_DWORD *)(result + 24) = 1;
*(_QWORD *)result = a1;
return result;
}

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

result = func2();
*(_DWORD *)(result + 24) = 1;
*(_QWORD *)result = a1;
return result;
}

original_code:
ScarabDatum *scarab_new_integer(long long num)
{
 ScarabDatum *d=scarab_new_atomic();
 d->type=1;
 d->data.integer=num;
 return d;
}

canonical_original_code:
struct __ScarabDatum *scarab_new_integer(long long num)
{
 struct __ScarabDatum *d = scarab_new_atomic();
 d->type=1;
 d->data.integer=num;
 return d;
}

variable_types:
num
long long


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

result = scarab_new_atomic();
*(_DWORD *)(result + 24) = 2;
*(double *)result = a1;
return result;
}

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

result = func2();
*(_DWORD *)(result + 24) = 2;
*(double *)result = a1;
return result;
}

original_code:
ScarabDatum *scarab_new_float( double num)
{
 ScarabDatum *d=scarab_new_atomic();
 d->type=2;
 d->data.floatp=num;
 return d;
}

canonical_original_code:
struct __ScarabDatum *scarab_new_float(double num)
{
 struct __ScarabDatum *d = scarab_new_atomic();
 d->type=2;
 d->data.floatp=num;
 return d;
}

variable_types: num double


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

if ( a1 )
result = pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32));
return result;
}

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

if ( a1 )
result = pthread_mutex_lock(*(pthread_mutex_t **)(a1 + 32));
return result;
}

original_code:
ScarabDatum *scarab_lock_datum(ScarabDatum *datum){
  if(datum != ((void *)0)){
   pthread_mutex_lock(datum->mutex);
  }
}

canonical_original_code:
struct __ScarabDatum *scarab_lock_datum(struct __ScarabDatum *datum){
  if(datum != ((void *)0)){
   pthread_mutex_lock(datum->mutex);
  }
}


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

if ( a1 )
result = pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32));
return result;
}

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

if ( a1 )
result = pthread_mutex_unlock(*(pthread_mutex_t **)(a1 + 32));
return result;
}

original_code:
ScarabDatum *scarab_unlock_datum(ScarabDatum *datum){
  if(datum != ((void *)0)){
   pthread_mutex_unlock(datum->mutex);
  }
}

canonical_original_code:
struct __ScarabDatum *scarab_unlock_datum(struct __ScarabDatum *datum){
  if(datum != ((void *)0)){
   pthread_mutex_unlock(datum->mutex);
  }
}


decompiled_code:
__int64 __fastcall scarab_destroy_lock(__int64 a1)
{
pthread_mutex_t *v2; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = *(pthread_mutex_t **)(a1 + 32);
if ( v2 )
{
pthread_mutex_destroy(v2);
result = scarab_mem_free(*(_QWORD *)(a1 + 32));
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
pthread_mutex_t *v2; // rdi
__int64 result; // rax

if ( a1 )
{
v2 = *(pthread_mutex_t **)(a1 + 32);
if ( v2 )
{
pthread_mutex_destroy(v2);
result = scarab_mem_free(*(_QWORD *)(a1 + 32));
}
}
return result;
}

original_code:
ScarabDatum *scarab_destroy_lock(ScarabDatum *datum){
  if(datum != ((void *)0) && datum->mutex != ((void *)0)){
   pthread_mutex_destroy(datum->mutex);
   scarab_mem_free(datum->mutex);
  }
}

canonical_original_code:
struct __ScarabDatum *scarab_destroy_lock(struct __ScarabDatum *datum){
  if(datum != ((void *)0) && datum->mutex != ((void *)0)){
   pthread_mutex_destroy(datum->mutex);
   scarab_mem_free(datum->mutex);
  }
}


decompiled_code:
__int64 __fastcall scarab_copy_datum(__int64 a1)
{
if ( a1 )
{
scarab_lock_datum(a1);
++*(_DWORD *)(a1 + 28);
scarab_unlock_datum(a1);
}
return a1;
}

original_code:
ScarabDatum * scarab_copy_datum(ScarabDatum *datum){
 if(datum != ((void *)0)){
  scarab_lock_datum(datum);
  datum->ref_count++;
  scarab_unlock_datum(datum);
 }
 return datum;
}

canonical_original_code:
struct __ScarabDatum *scarab_copy_datum(struct __ScarabDatum *datum){
 if(datum != ((void *)0)){
  scarab_lock_datum(datum);
  datum->ref_count++;
  scarab_unlock_datum(datum);
 }
 return datum;
}


decompiled_code:
__int64 __fastcall scarab_dict_times2(__int64 a1)
{
return (unsigned int)(2 * *(_DWORD *)(a1 + 4));
}

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

original_code:
int
scarab_dict_times2(ScarabDict *d)
{
 return d->tablesize * 2;
}

canonical_original_code:
int scarab_dict_times2(struct __ScarabDict *d)
{
 return d->tablesize * 2;
}

variable_types: struct __ScarabDict { int size; int tablesize; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict *dict; struct __ScarabList { int size; struct __ScarabDatum **values; } *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *


decompiled_code:
__int64 __fastcall scarab_dict_new(int a1, __int64 a2)
{
__int64 v3; // rbp
int v4; // edi

v3 = scarab_new_molecular();
v4 = 1;
if ( a1 > 0 )
v4 = a1;
*(_QWORD *)v3 = newdict(v4, a2);
*(_DWORD *)(v3 + 24) = 5;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2)
{
__int64 v3; // rbp
int v4; // edi

v3 = scarab_new_molecular();
v4 = 1;
if ( a1 > 0 )
v4 = a1;
*(_QWORD *)v3 = func0(v4, a2);
*(_DWORD *)(v3 + 24) = 5;
return v3;
}

original_code:
ScarabDatum*
scarab_dict_new(int initialsize, ScarabDictExpFun f)
{
 ScarabDatum *d = scarab_new_molecular();
 if(initialsize < 1) {initialsize = 1; }
 ScarabDict *dict = newdict(initialsize, f);
 d->data.dict = dict;
 d->type = 5;
 return d;
}

canonical_original_code:
struct __ScarabDatum *scarab_dict_new(int initialsize, int (*f)(struct __ScarabDict *dict))
{
 struct __ScarabDatum *d = scarab_new_molecular();
 if(initialsize < 1) {initialsize = 1; }
 struct __ScarabDict *dict = newdict(initialsize, f);
 d->data.dict = dict;
 d->type = 5;
 return d;
}

variable_types: initialsize int

variable_types:
f
int (*)(struct __ScarabDict *dict)


rehash

original_code:
static void
rehash(ScarabDatum *dictionary)
{
 int i = 0;
 ScarabDict *olddict = dictionary->data.dict;
 int newsize = olddict->expansion_function(olddict);
 ScarabDict *new_dict = newdict(newsize, olddict->expansion_function);
 ScarabDatum **keys;
 ScarabDatum **values;
 ScarabDatum *existing_val;
 keys = olddict->keys;
 values = olddict->values;
 dictionary->data.dict = new_dict;
 for(i = 0; i < olddict->tablesize; i++)
 {
  if (keys[i])
  {
   existing_val = scarab_dict_put(dictionary, keys[i], values[i]);
   if(existing_val) {
   }
   scarab_free_datum(keys[i]);
   scarab_free_datum(values[i]);
  }
 }
 scarab_mem_free(keys);
 scarab_mem_free(values);
 scarab_mem_free(olddict);
}

canonical_original_code:
static void rehash(struct __ScarabDatum *dictionary)
{
 int i = 0;
 struct __ScarabDict *olddict = dictionary->data.dict;
 int newsize = olddict->expansion_function(olddict);
 struct __ScarabDict *new_dict = newdict(newsize, olddict->expansion_function);
 struct __ScarabDatum **keys;
 struct __ScarabDatum **values;
 struct __ScarabDatum *existing_val;
 keys = olddict->keys;
 values = olddict->values;
 dictionary->data.dict = new_dict;
 for(i = 0; i < olddict->tablesize; i++)
 {
  if (keys[i])
  {
   existing_val = scarab_dict_put(dictionary, keys[i], values[i]);
   if(existing_val) {
   }
   scarab_free_datum(keys[i]);
   scarab_free_datum(values[i]);
  }
 }
 scarab_mem_free(keys);
 scarab_mem_free(values);
 scarab_mem_free(olddict);
}

variable_types: i int

variable_types: newsize int

variable_types: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList { int size; struct __ScarabDatum **values; } *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **

return_type: void


decompiled_code:
__int64 __fastcall scarab_dict_get(__int64 *a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // er12
int v4; // er12
int v5; // ecx
int v6; // ebx
__int64 v7; // rbx

scarab_lock_datum();
v2 = *a1;
v3 = scarab_hash(a2, *(unsigned int *)(*a1 + 4), 0LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 1LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 2LL);
if ( (unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
goto LABEL_12;
v4 = v3 + 1;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 <= 0 )
goto LABEL_10;
v6 = v4;
while ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * (v6 % v5)), a2) )
{
++v6;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 + v4 <= v6 )
goto LABEL_10;
}
v3 = v6 % *(_DWORD *)(v2 + 4);
if ( v3 == -1 )
{
LABEL_10:
scarab_unlock_datum(a1);
v7 = 0LL;
}
else
{
LABEL_12:
v7 = *(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3);
scarab_unlock_datum(a1);
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // er12
int v4; // er12
int v5; // ecx
int v6; // ebx
__int64 v7; // rbx

scarab_lock_datum();
v2 = *a1;
v3 = scarab_hash(a2, *(unsigned int *)(*a1 + 4), 0LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 1LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 2LL);
if ( (unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
goto LABEL_12;
v4 = v3 + 1;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 <= 0 )
goto LABEL_10;
v6 = v4;
while ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * (v6 % v5)), a2) )
{
++v6;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 + v4 <= v6 )
goto LABEL_10;
}
v3 = v6 % *(_DWORD *)(v2 + 4);
if ( v3 == -1 )
{
LABEL_10:
scarab_unlock_datum(a1);
v7 = 0LL;
}
else
{
LABEL_12:
v7 = *(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3);
scarab_unlock_datum(a1);
}
return v7;
}

original_code:
ScarabDatum*
scarab_dict_get(ScarabDatum *dictionary, ScarabDatum *key)
{
 int i = 0;
 int h = 0;
 int hashval = 0;
 scarab_lock_datum(dictionary);
 ScarabDict *dict = dictionary->data.dict;
 h = scarab_hash(key, dict->tablesize, 0);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 1);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 2);
 if (!scarab_equals(dict->keys[h], key))
 {
  hashval = h;
  h = -1;
  for (i = hashval + 1; i < hashval + 1 + dict->tablesize; i++)
  {
   if (scarab_equals(dict->keys[i % dict->tablesize],key))
   {
    h=i % dict->tablesize;
    break;
   }
  }
  if (h == -1){
   scarab_unlock_datum(dictionary);
   return ((void *)0);
  }
 }
 ScarabDatum *result = dict->values[h];
 scarab_unlock_datum(dictionary);
 return result;
}

canonical_original_code:
struct __ScarabDatum *scarab_dict_get(struct __ScarabDatum *dictionary, struct __ScarabDatum *key)
{
 int i = 0;
 int h = 0;
 int hashval = 0;
 scarab_lock_datum(dictionary);
 struct __ScarabDict *dict = dictionary->data.dict;
 h = scarab_hash(key, dict->tablesize, 0);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 1);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 2);
 if (!scarab_equals(dict->keys[h], key))
 {
  hashval = h;
  h = -1;
  for (i = hashval + 1; i < hashval + 1 + dict->tablesize; i++)
  {
   if (scarab_equals(dict->keys[i % dict->tablesize],key))
   {
    h=i % dict->tablesize;
    break;
   }
  }
  if (h == -1){
   scarab_unlock_datum(dictionary);
   return ((void *)0);
  }
 }
 struct __ScarabDatum *result = dict->values[h];
 scarab_unlock_datum(dictionary);
 return result;
}

variable_types:
dictionary
struct __ScarabDatum *

variable_types:
key
struct __ScarabDatum *

variable_types: h int

variable_types: hashval int


decompiled_code:
__int64 __fastcall scarab_dict_remove(__int64 *a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // er12
int v4; // er12
int v5; // ecx
int v6; // ebx
__int64 v7; // rbx

scarab_lock_datum();
v2 = *a1;
v3 = scarab_hash(a2, *(unsigned int *)(*a1 + 4), 0LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 1LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 2LL);
if ( (unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
goto LABEL_18;
v4 = v3 + 1;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 <= 0 )
goto LABEL_10;
v6 = v4;
while ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * (v6 % v5)), a2) )
{
++v6;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 + v4 <= v6 )
goto LABEL_10;
}
v3 = v6 % *(_DWORD *)(v2 + 4);
if ( v3 == -1 )
{
LABEL_10:
scarab_unlock_datum(a1);
v7 = 0LL;
}
else
{
LABEL_18:
if ( *(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3) )
--*(_DWORD *)v2;
v7 = scarab_copy_datum(*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3));
scarab_free_datum(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3));
*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3) = 0LL;
scarab_free_datum(*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3));
*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3) = 0LL;
scarab_unlock_datum(a1);
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 *a1, __int64 a2)
{
__int64 v2; // rbp
int v3; // er12
int v4; // er12
int v5; // ecx
int v6; // ebx
__int64 v7; // rbx

scarab_lock_datum();
v2 = *a1;
v3 = scarab_hash(a2, *(unsigned int *)(*a1 + 4), 0LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 1LL);
if ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
v3 = scarab_hash(a2, *(unsigned int *)(v2 + 4), 2LL);
if ( (unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3), a2) )
goto LABEL_18;
v4 = v3 + 1;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 <= 0 )
goto LABEL_10;
v6 = v4;
while ( !(unsigned int)scarab_equals(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * (v6 % v5)), a2) )
{
++v6;
v5 = *(_DWORD *)(v2 + 4);
if ( v5 + v4 <= v6 )
goto LABEL_10;
}
v3 = v6 % *(_DWORD *)(v2 + 4);
if ( v3 == -1 )
{
LABEL_10:
scarab_unlock_datum(a1);
v7 = 0LL;
}
else
{
LABEL_18:
if ( *(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3) )
--*(_DWORD *)v2;
v7 = scarab_copy_datum(*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3));
scarab_free_datum(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3));
*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL * v3) = 0LL;
scarab_free_datum(*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3));
*(_QWORD *)(*(_QWORD *)(v2 + 16) + 8LL * v3) = 0LL;
scarab_unlock_datum(a1);
}
return v7;
}

original_code:
ScarabDatum*
scarab_dict_remove(ScarabDatum *dictionary, ScarabDatum *key)
{
 scarab_lock_datum(dictionary);
 ScarabDict *dict = dictionary->data.dict;
 ScarabDatum *oldval;
 int i = 0;
 int h = scarab_hash(key, dict->tablesize, 0);
 int hashval = h;
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 1);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 2);
 if (!scarab_equals(dict->keys[h], key))
 {
  hashval = h;
  h = -1;
  for (i = hashval + 1; i < hashval + 1 + dict->tablesize; i++)
  {
   if (scarab_equals(dict->keys[i % dict->tablesize],key))
   {
    h = i % dict->tablesize;
    break;
   }
  }
  if (h == -1){
   scarab_unlock_datum(dictionary);
   return ((void *)0);
  }
 }
 if (dict->keys[h] != ((void *)0)) {
  --dict->size;
 }
 oldval = scarab_copy_datum(dict->values[h]);
 scarab_free_datum(dict->keys[h]);
 dict->keys[h] = ((void *)0);
 scarab_free_datum(dict->values[h]);
 dict->values[h] = ((void *)0);
 scarab_unlock_datum(dictionary);
 return oldval;
}

canonical_original_code:
struct __ScarabDatum *scarab_dict_remove(struct __ScarabDatum *dictionary, struct __ScarabDatum *key)
{
 scarab_lock_datum(dictionary);
 struct __ScarabDict *dict = dictionary->data.dict;
 struct __ScarabDatum *oldval;
 int i = 0;
 int h = scarab_hash(key, dict->tablesize, 0);
 int hashval = h;
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 1);
 if (!scarab_equals(dict->keys[h], key))
  h = scarab_hash(key, dict->tablesize, 2);
 if (!scarab_equals(dict->keys[h], key))
 {
  hashval = h;
  h = -1;
  for (i = hashval + 1; i < hashval + 1 + dict->tablesize; i++)
  {
   if (scarab_equals(dict->keys[i % dict->tablesize],key))
   {
    h = i % dict->tablesize;
    break;
   }
  }
  if (h == -1){
   scarab_unlock_datum(dictionary);
   return ((void *)0);
  }
 }
 if (dict->keys[h] != ((void *)0)) {
  --dict->size;
 }
 oldval = scarab_copy_datum(dict->values[h]);
 scarab_free_datum(dict->keys[h]);
 dict->keys[h] = ((void *)0);
 scarab_free_datum(dict->values[h]);
 dict->values[h] = ((void *)0);
 scarab_unlock_datum(dictionary);
 return oldval;
}


decompiled_code:
__int64 __fastcall scarab_dict_keys(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + 8LL);
}

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

original_code:
ScarabDatum ** scarab_dict_keys(ScarabDatum * dictionary) {
 ScarabDict * dict = dictionary->data.dict;
    ScarabDatum **keys = dict->keys;
 return keys;
}

canonical_original_code:
struct __ScarabDatum **scarab_dict_keys(struct __ScarabDatum *dictionary) {
 struct __ScarabDict *dict = dictionary->data.dict;
    struct __ScarabDatum **keys = dict->keys;
 return keys;
}

return_type: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList { int size; struct __ScarabDatum **values; } *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **


decompiled_code:
__int64 __fastcall scarab_dict_values(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + 16LL);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + 16LL);
}

original_code:
ScarabDatum ** scarab_dict_values(ScarabDatum * dictionary) {
 ScarabDict * dict = dictionary->data.dict;
    ScarabDatum **values = dict->values;
 return values;
}

canonical_original_code:
struct __ScarabDatum **scarab_dict_values(struct __ScarabDatum *dictionary) {
 struct __ScarabDict *dict = dictionary->data.dict;
    struct __ScarabDatum **values = dict->values;
 return values;
}


decompiled_code:
__int64 __fastcall scarab_dict_number_of_elements(unsigned int **a1)
{
return **a1;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int **a1)
{
return **a1;
}

original_code:
int scarab_dict_number_of_elements(ScarabDatum * dictionary) {
 ScarabDict * dict = dictionary->data.dict;
    int size = dict->size;
 return size;
}

canonical_original_code:
int scarab_dict_number_of_elements(struct __ScarabDatum *dictionary) {
 struct __ScarabDict *dict = dictionary->data.dict;
    int size = dict->size;
 return size;
}

variable_types: size int


decompiled_code:
__int64 stream_send()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
static int stream_send(ScarabStream * stream) {
 return 0;
}

canonical_original_code:
static int stream_send(struct __ScarabStream *stream) {
 return 0;
}

variable_types: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } *

user_defined_types: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession *session; struct __ScarabStreamEngine *engine; }


decompiled_code:
__int64 stream_flush()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 0LL;
}

original_code:
static int stream_flush(ScarabStream * stream) {
 return 0;
}

canonical_original_code:
static int stream_flush(struct __ScarabStream *stream) {
 return 0;
}


decompiled_code:
__int64 stream_seek()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func3()
{
return 0xFFFFFFFFLL;
}

original_code:
static int
stream_seek(ScarabStream *stream, long int offset, int origin)
{
 return -1;
}

canonical_original_code:
static int stream_seek(struct __ScarabStream *stream, long offset, int origin)
{
 return -1;
}

variable_types: offset long

variable_types: origin int


decompiled_code:
__int64 stream_tell()
{
return -1LL;
}

canonical_decompiled_code:
__int64 func4()
{
return -1LL;
}

original_code:
static long int
stream_tell(ScarabStream *stream)
{
 return -1;
}

canonical_original_code:
static long stream_tell(struct __ScarabStream *stream)
{
 return -1;
}

return_type: long


decompiled_code:
char *__fastcall stream_errstr(int a1)
{
return error_text[a1];
}

canonical_decompiled_code:
char *__fastcall func5(int a1)
{
return error_text[a1];
}

original_code:
static char * stream_errstr(int code) {
 return error_text[code];
}

canonical_original_code:
static char *stream_errstr(int code) {
 return error_text[code];
}

variable_types: code int

return_type: char *


decompiled_code:
__int64 __fastcall stream_foreign_port(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getpeername(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getpeername(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

original_code:
static int stream_foreign_port(ScarabStream * s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getpeername(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
            return -1;
        }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in * sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

canonical_original_code:
static int stream_foreign_port(struct __ScarabStream *s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getpeername(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
            return -1;
        }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in *sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

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

variable_types: len int

variable_types: struct __tcpstr { struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } header; int fd; unsigned int packet_size; } *

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

user_defined_types: struct __tcpstr { struct __ScarabStream header; int fd; unsigned int packet_size; }


decompiled_code:
const char *__fastcall stream_foreign_address(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getpeername(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_1, 0x80u);
if ( result )
result = str_1;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func7(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getpeername(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_1, 0x80u);
if ( result )
result = str_1;
}
return result;
}

original_code:
static char * stream_foreign_address(ScarabStream * s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getpeername(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
            return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in * sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}

canonical_original_code:
static char *stream_foreign_address(struct __ScarabStream *s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getpeername(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
            return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}

variable_types:
str
char [128]

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

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


decompiled_code:
const char *__fastcall stream_local_address(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_0, 0x80u);
if ( result )
result = str_0;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(__int64 a1)
{
const char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0LL;
result = 0LL;
if ( addr.sa_family == 2 )
{
result = inet_ntop(2, &addr.sa_data[2], str_0, 0x80u);
if ( result )
result = str_0;
}
return result;
}

original_code:
static char * stream_local_address(ScarabStream * s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
            return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in * sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}

canonical_original_code:
static char *stream_local_address(struct __ScarabStream *s) {
    static char str[128];
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
            return ((void *)0);
    }
    if(addr.ss_family != 2) {
        return ((void *)0);
    }
    struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
    if(inet_ntop(2, &sin->sin_addr, str, sizeof(str)) == ((void *)0)) {
        return(((void *)0));
    }
    return(str);
}

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

user_defined_types: struct __ScarabSession { struct __ScarabEncoderEngine *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; }


decompiled_code:
__int64 __fastcall stream_local_port(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

len = 128;
if ( getsockname(*(_DWORD *)(a1 + 24), &addr, &len) < 0 )
return 0xFFFFFFFFLL;
if ( addr.sa_family == 2 )
return *(unsigned __int16 *)addr.sa_data;
return 4294967294LL;
}

original_code:
static int stream_local_port(ScarabStream * s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    tcpstr * ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (socklen_t*)(&len)) < 0) {
            return -1;
        }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in * sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

canonical_original_code:
static int stream_local_port(struct __ScarabStream *s) {
    struct sockaddr_storage addr;
    int len = sizeof(addr);
    struct __tcpstr *ts = (tcpstr *)(s);
    if(getsockname(ts->fd, (struct sockaddr *)&addr, (unsigned int *)(&len)) < 0) {
            return -1;
        }
    if(addr.ss_family != 2) {
        return -2;
    }
    struct sockaddr_in *sock = (struct sockaddr_in *)&addr;
    return sock->sin_port;
}

user_defined_types: struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; }

user_defined_types: struct __ScarabList { int size; struct __ScarabDatum **values; }


decompiled_code:
__int64 __fastcall stream_read(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er13
unsigned int v5; // ebx
int v6; // eax
unsigned int v7; // edx

v4 = a3;
v5 = a3;
while ( 1 )
{
v6 = recv(*(_DWORD *)(a1 + 24), a2, (int)v5, 0);
v7 = v6;
if ( v6 < 0 )
break;
if ( !v6 )
return v7;
a2 += v6;
v5 -= v6;
if ( !v5 )
return v4;
}
return (unsigned int)scarab_session_seterr(
*(_QWORD *)(a1 + 8),
(unsigned int)(scarab_stream_tcpip[1] + 2),
(unsigned int)v6);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er13
unsigned int v5; // ebx
int v6; // eax
unsigned int v7; // edx

v4 = a3;
v5 = a3;
while ( 1 )
{
v6 = recv(*(_DWORD *)(a1 + 24), a2, (int)v5, 0);
v7 = v6;
if ( v6 < 0 )
break;
if ( !v6 )
return v7;
a2 += v6;
v5 -= v6;
if ( !v5 )
return v4;
}
return (unsigned int)scarab_session_seterr(
*(_QWORD *)(a1 + 8),
(unsigned int)(scarab_stream_tcpip[1] + 2),
(unsigned int)v6);
}

original_code:
static int stream_read(ScarabStream * stream, void *buffer, int size) {
 tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 int tot;
 tot = size;
 c = (unsigned char *)buffer;
 do {
        r = recv(ts->fd, c, size, 0);
        if (r < 0) {
   return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip)->err_code_base + (2) ));
  }
        if(r == 0) {
            return 0;
        }
  size -= r;
  c += r;
 } while (size);
 return tot;
}

canonical_original_code:
static int stream_read(struct __ScarabStream *stream, void *buffer, int size) {
 struct __tcpstr *ts = ((tcpstr*)(stream));
 unsigned char *c;
 int r;
 int tot;
 tot = size;
 c = (unsigned char *)buffer;
 do {
        r = recv(ts->fd, c, size, 0);
        if (r < 0) {
   return scarab_session_seterr(stream->session, ( (&scarab_stream_tcpip)->err_code_base + (2) ));
  }
        if(r == 0) {
            return 0;
        }
  size -= r;
  c += r;
 } while (size);
 return tot;
}

variable_types:
buffer
void *

variable_types:
c
unsigned char *

variable_types: r int

variable_types: tot int

user_defined_types: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque opaque; struct __ScarabDict *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t *mutex; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }