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

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getpeername(v1, &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)
{
int v1; // edi
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getpeername(v1, &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; }

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


repo: hahong/pymworks

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

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

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

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getpeername(v1, &addr, &len) < 0
|| addr.sa_family != 2
|| (v2 = inet_ntop(2, &addr.sa_data[2], str_1, 0x80u), result = str_1, !v2) )
{
result = 0LL;
}
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: 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:
str
char [128]

return_type: char *

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

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

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:
char *__fastcall stream_local_address(__int64 a1)
{
int v1; // edi
const char *v2; // r8
char *result; // rax
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

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

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

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getsockname(v1, &addr, &len) < 0
|| addr.sa_family != 2
|| (v2 = inet_ntop(2, &addr.sa_data[2], str_0, 0x80u), result = str_0, !v2) )
{
result = 0LL;
}
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 __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 stream_local_port(__int64 a1)
{
int v1; // edi
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getsockname(v1, &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)
{
int v1; // edi
socklen_t len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

v1 = *(_DWORD *)(a1 + 24);
len = 128;
if ( getsockname(v1, &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;
}

return_type: int


decompiled_code:
__int64 __fastcall stream_read(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // ebx
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
unsigned int v9; // er8

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

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // ebp
unsigned int v5; // ebx
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rcx
unsigned int v9; // er8

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

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

variable_types:
c
unsigned char *

variable_types: r int

variable_types: tot int


decompiled_code:
__int64 __fastcall stream_write(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er12
unsigned int v5; // ebx
unsigned int v6; // edx
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v10; // r8

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

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, char *a2, unsigned int a3)
{
unsigned int v4; // er12
unsigned int v5; // ebx
unsigned int v6; // edx
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rcx
__int64 v10; // r8

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

original_code:
static int stream_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)->err_code_base + (1) ));
            }
  left -= r;
  c += r;
 } while (left);
 return size;
}

canonical_original_code:
static int stream_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)->err_code_base + (1) ));
            }
  left -= r;
  c += r;
 } while (left);
 return size;
}

variable_types:
left
unsigned int


decompiled_code:
__int64 __fastcall stream_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 stream_close(ScarabStream * stream) {
 tcpstr *ts = ((tcpstr*)(stream));
 shutdown(ts->fd, 2);
 close(ts->fd);
 return 0;
}

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


decompiled_code:
__int64 __fastcall stream_connect(__int64 a1, char *a2)
{
char *v3; // rbp
unsigned __int64 v4; // rax
__int64 v5; // rdx
char *v6; // rax
__int64 v7; // rsi
int v8; // ebx
unsigned __int64 v9; // rdx
__int64 v10; // rdi
char *v11; // rax
const char *v12; // r13
__int16 v13; // r14
char *v14; // rbx
struct protoent *v15; // rbp
struct hostent *v16; // rax
__int64 v17; // rdx
__int64 v18; // rcx
__int64 v19; // r8
__int64 v20; // rbx
int v21; // eax
__int64 v22; // rdx
__int64 v23; // rcx
__int64 v24; // r8
__int64 v25; // rdx
__int64 v26; // rcx
__int64 v27; // r8
__int64 result; // rax
struct sockaddr v29; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
v4 = (unsigned __int8)*a2;
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[1];
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[2];
v3 = a2 + 2;
}
else
{
v3 = a2 + 1;
}
}
if ( (unsigned __int8)v4 <= 0x3Au && (v5 = 0x400800000000001LL, _bittest64(&v5, v4)) )
{
v10 = 1LL;
v8 = 0;
}
else
{
v6 = v3;
v7 = 0x400800000000001LL;
do
{
do
{
v8 = 1 - (_DWORD)v3 + (_DWORD)v6;
v9 = (unsigned __int8)*++v6;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v7, v9) );
v10 = (unsigned int)(v8 + 1);
}
v11 = (char *)scarab_mem_malloc(v10);
v12 = v11;
if ( !v11 )
return 0LL;
v13 = 0;
memcpy(v11, v3, v8);
v12[v8] = 0;
v14 = &v3[v8];
if ( *v14 == 58 )
v13 = atoi(v14 + 1);
v15 = getprotobyname("tcp");
if ( !v15 )
return 0LL;
v29 = 0LL;
v29.sa_family = 2;
*(_WORD *)v29.sa_data = _bswap_16(v13);
v16 = gethostbyname(v12);
if ( v16 )
{
*(_DWORD *)&v29.sa_data[2] = **(_DWORD **)v16->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 3), v17, v18, v19);
*(_DWORD *)&v29.sa_data[2] = inet_addr(v12);
if ( !*(_DWORD *)&v29.sa_data[2] )
return 0LL;
}
v20 = scarab_mem_malloc(32LL);
if ( !v20 )
return 0LL;
v21 = socket(2, 1, v15->p_proto);
*(_DWORD *)(v20 + 24) = v21;
if ( v21 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 4), v22, v23, v24);
return 0LL;
}
if ( connect(v21, &v29, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 5), v25, v26, v27);
close(*(_DWORD *)(v20 + 24));
result = 0LL;
}
else
{
*(_DWORD *)(v20 + 28) = -1;
result = v20;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, char *a2)
{
char *v3; // rbp
unsigned __int64 v4; // rax
__int64 v5; // rdx
char *v6; // rax
__int64 v7; // rsi
int v8; // ebx
unsigned __int64 v9; // rdx
__int64 v10; // rdi
char *v11; // rax
const char *v12; // r13
__int16 v13; // r14
char *v14; // rbx
struct protoent *v15; // rbp
struct hostent *v16; // rax
__int64 v17; // rdx
__int64 v18; // rcx
__int64 v19; // r8
__int64 v20; // rbx
int v21; // eax
__int64 v22; // rdx
__int64 v23; // rcx
__int64 v24; // r8
__int64 v25; // rdx
__int64 v26; // rcx
__int64 v27; // r8
__int64 result; // rax
struct sockaddr v29; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
v4 = (unsigned __int8)*a2;
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[1];
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[2];
v3 = a2 + 2;
}
else
{
v3 = a2 + 1;
}
}
if ( (unsigned __int8)v4 <= 0x3Au && (v5 = 0x400800000000001LL, _bittest64(&v5, v4)) )
{
v10 = 1LL;
v8 = 0;
}
else
{
v6 = v3;
v7 = 0x400800000000001LL;
do
{
do
{
v8 = 1 - (_DWORD)v3 + (_DWORD)v6;
v9 = (unsigned __int8)*++v6;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v7, v9) );
v10 = (unsigned int)(v8 + 1);
}
v11 = (char *)scarab_mem_malloc(v10);
v12 = v11;
if ( !v11 )
return 0LL;
v13 = 0;
memcpy(v11, v3, v8);
v12[v8] = 0;
v14 = &v3[v8];
if ( *v14 == 58 )
v13 = atoi(v14 + 1);
v15 = getprotobyname("tcp");
if ( !v15 )
return 0LL;
v29 = 0LL;
v29.sa_family = 2;
*(_WORD *)v29.sa_data = func13(v13);
v16 = gethostbyname(v12);
if ( v16 )
{
*(_DWORD *)&v29.sa_data[2] = **(_DWORD **)v16->h_addr_list;
}
else
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 3), v17, v18, v19);
*(_DWORD *)&v29.sa_data[2] = inet_addr(v12);
if ( !*(_DWORD *)&v29.sa_data[2] )
return 0LL;
}
v20 = scarab_mem_malloc(32LL);
if ( !v20 )
return 0LL;
v21 = socket(2, 1, v15->p_proto);
*(_DWORD *)(v20 + 24) = v21;
if ( v21 < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 4), v22, v23, v24);
return 0LL;
}
if ( connect(v21, &v29, 0x10u) < 0 )
{
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 5), v25, v26, v27);
close(*(_DWORD *)(v20 + 24));
result = 0LL;
}
else
{
*(_DWORD *)(v20 + 28) = -1;
result = v20;
}
return result;
}

original_code:
static ScarabStream * stream_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)->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)->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)->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 *stream_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)->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)->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)->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 stream_accept(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // r12
int v5; // eax
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
socklen_t addr_len; // [rsp+0h] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-38h] BYREF

addr_len = 16;
addr = 0LL;
v4 = scarab_mem_malloc(32LL);
if ( v4 )
{
v5 = accept(*(_DWORD *)(a3 + 24), &addr, &addr_len);
*(_DWORD *)(v4 + 24) = v5;
if ( v5 < 0 )
{
v4 = 0LL;
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 5), v6, v7, v8);
}
else
{
*(_DWORD *)(v4 + 28) = -1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // r12
int v5; // eax
__int64 v6; // rdx
__int64 v7; // rcx
__int64 v8; // r8
socklen_t addr_len; // [rsp+0h] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+4h] [rbp-38h] BYREF

addr_len = 16;
addr = 0LL;
v4 = scarab_mem_malloc(32LL);
if ( v4 )
{
v5 = accept(*(_DWORD *)(a3 + 24), &addr, &addr_len);
*(_DWORD *)(v4 + 24) = v5;
if ( v5 < 0 )
{
v4 = 0LL;
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 5), v6, v7, v8);
}
else
{
*(_DWORD *)(v4 + 28) = -1;
}
}
return v4;
}

original_code:
static ScarabStream *
stream_accept(ScarabSession * session,
     const char *uri, ScarabStream * binding) {
 tcpstr *ts;
 tcpstr *bd;
 struct sockaddr end_point;
 socklen_t len;
 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);
 ts->fd = accept(bd->fd, (struct sockaddr *)&end_point, (socklen_t*)(&len));
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (5) ));
  return ((void *)0);
 }
 ts->packet_size = (unsigned int)-1;
 return &ts->header;
}

canonical_original_code:
static struct __ScarabStream *stream_accept(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding) {
 struct __tcpstr *ts;
 struct __tcpstr *bd;
 struct sockaddr end_point;
 unsigned int len;
 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);
 ts->fd = accept(bd->fd, (struct sockaddr *)&end_point, (unsigned int *)(&len));
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (5) ));
  return ((void *)0);
 }
 ts->packet_size = (unsigned int)-1;
 return &ts->header;
}

variable_types:
binding
struct __ScarabStream *

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

variable_types:
len
unsigned int


decompiled_code:
__int64 __fastcall stream_bind(__int64 a1, char *a2)
{
char *v3; // rbp
unsigned __int64 v4; // rax
__int64 v5; // rdx
char *v6; // rax
__int64 v7; // rsi
int v8; // ebx
unsigned __int64 v9; // rdx
__int64 v10; // rdi
char *v11; // rax
const char *v12; // r13
__int16 v13; // r14
char *v14; // rbx
struct protoent *v15; // rbp
struct hostent *v16; // rax
__int64 v17; // rdx
__int64 v18; // rcx
__int64 v19; // r8
__int64 v20; // r13
unsigned int v21; // eax
__int64 v22; // rdi
__int64 v23; // rdx
__int64 v24; // rcx
__int64 v25; // r8
int v26; // edi
__int64 v27; // rsi
__int64 v29; // rsi
in_addr_t v30; // eax
struct sockaddr v31; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
v4 = (unsigned __int8)*a2;
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[1];
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[2];
v3 = a2 + 2;
}
else
{
v3 = a2 + 1;
}
}
if ( (unsigned __int8)v4 <= 0x3Au && (v5 = 0x400800000000001LL, _bittest64(&v5, v4)) )
{
v10 = 1LL;
v8 = 0;
}
else
{
v6 = v3;
v7 = 0x400800000000001LL;
do
{
do
{
v8 = 1 - (_DWORD)v3 + (_DWORD)v6;
v9 = (unsigned __int8)*++v6;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v7, v9) );
v10 = (unsigned int)(v8 + 1);
}
v11 = (char *)scarab_mem_malloc(v10);
v12 = v11;
if ( !v11 )
goto LABEL_22;
v13 = 0;
memcpy(v11, v3, v8);
v12[v8] = 0;
v14 = &v3[v8];
if ( *v14 == 58 )
v13 = atoi(v14 + 1);
v15 = getprotobyname("tcp");
if ( !v15 )
goto LABEL_22;
v31 = 0LL;
v31.sa_family = 2;
*(_WORD *)v31.sa_data = _bswap_16(v13);
v16 = gethostbyname(v12);
if ( v16 )
{
*(_DWORD *)&v31.sa_data[2] = **(_DWORD **)v16->h_addr_list;
}
else
{
v29 = (unsigned int)(scarab_stream_tcpip[1] + 3);
scarab_session_seterr(a1, v29, v17, v18, v19);
v30 = inet_addr(v12);
if ( !v30 )
v30 = _bswap_32_constprop_0(v12, v29);
*(_DWORD *)&v31.sa_data[2] = v30;
}
v20 = scarab_mem_malloc(32LL);
if ( !v20 )
goto LABEL_22;
v21 = socket(2, 1, v15->p_proto);
*(_DWORD *)(v20 + 24) = v21;
v22 = v21;
if ( setSocketOptionFlag_constprop_0(v21) < 0 && (unsigned int)scarab_os_geterr(v22, 1LL) != 92 )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 4);
goto LABEL_21;
}
v26 = *(_DWORD *)(v20 + 24);
if ( v26 < 0 )
{
v20 = 0LL;
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 4), v23, v24, v25);
return v20;
}
if ( bind(v26, &v31, 0x10u) < 0 )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 6);
goto LABEL_21;
}
if ( listen(*(_DWORD *)(v20 + 24), 5) )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 7);
LABEL_21:
scarab_session_seterr(a1, v27, v23, v24, v25);
close(*(_DWORD *)(v20 + 24));
LABEL_22:
v20 = 0LL;
}
return v20;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, char *a2)
{
char *v3; // rbp
unsigned __int64 v4; // rax
__int64 v5; // rdx
char *v6; // rax
__int64 v7; // rsi
int v8; // ebx
unsigned __int64 v9; // rdx
__int64 v10; // rdi
char *v11; // rax
const char *v12; // r13
__int16 v13; // r14
char *v14; // rbx
struct protoent *v15; // rbp
struct hostent *v16; // rax
__int64 v17; // rdx
__int64 v18; // rcx
__int64 v19; // r8
__int64 v20; // r13
unsigned int v21; // eax
__int64 v22; // rdi
__int64 v23; // rdx
__int64 v24; // rcx
__int64 v25; // r8
int v26; // edi
__int64 v27; // rsi
__int64 v29; // rsi
in_addr_t v30; // eax
struct sockaddr v31; // [rsp+0h] [rbp-38h] BYREF

v3 = a2;
v4 = (unsigned __int8)*a2;
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[1];
if ( (_BYTE)v4 == 47 )
{
v4 = (unsigned __int8)a2[2];
v3 = a2 + 2;
}
else
{
v3 = a2 + 1;
}
}
if ( (unsigned __int8)v4 <= 0x3Au && (v5 = 0x400800000000001LL, _bittest64(&v5, v4)) )
{
v10 = 1LL;
v8 = 0;
}
else
{
v6 = v3;
v7 = 0x400800000000001LL;
do
{
do
{
v8 = 1 - (_DWORD)v3 + (_DWORD)v6;
v9 = (unsigned __int8)*++v6;
}
while ( (unsigned __int8)v9 > 0x3Au );
}
while ( !_bittest64(&v7, v9) );
v10 = (unsigned int)(v8 + 1);
}
v11 = (char *)scarab_mem_malloc(v10);
v12 = v11;
if ( !v11 )
goto LABEL_22;
v13 = 0;
memcpy(v11, v3, v8);
v12[v8] = 0;
v14 = &v3[v8];
if ( *v14 == 58 )
v13 = atoi(v14 + 1);
v15 = getprotobyname("tcp");
if ( !v15 )
goto LABEL_22;
v31 = 0LL;
v31.sa_family = 2;
*(_WORD *)v31.sa_data = func13(v13);
v16 = gethostbyname(v12);
if ( v16 )
{
*(_DWORD *)&v31.sa_data[2] = **(_DWORD **)v16->h_addr_list;
}
else
{
v29 = (unsigned int)(scarab_stream_tcpip[1] + 3);
scarab_session_seterr(a1, v29, v17, v18, v19);
v30 = inet_addr(v12);
if ( !v30 )
v30 = func17(v12, v29);
*(_DWORD *)&v31.sa_data[2] = v30;
}
v20 = scarab_mem_malloc(32LL);
if ( !v20 )
goto LABEL_22;
v21 = socket(2, 1, v15->p_proto);
*(_DWORD *)(v20 + 24) = v21;
v22 = v21;
if ( func16(v21) < 0 && (unsigned int)scarab_os_geterr(v22, 1LL) != 92 )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 4);
goto LABEL_21;
}
v26 = *(_DWORD *)(v20 + 24);
if ( v26 < 0 )
{
v20 = 0LL;
scarab_session_seterr(a1, (unsigned int)(scarab_stream_tcpip[1] + 4), v23, v24, v25);
return v20;
}
if ( bind(v26, &v31, 0x10u) < 0 )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 6);
goto LABEL_21;
}
if ( listen(*(_DWORD *)(v20 + 24), 5) )
{
v27 = (unsigned int)(scarab_stream_tcpip[1] + 7);
LABEL_21:
scarab_session_seterr(a1, v27, v23, v24, v25);
close(*(_DWORD *)(v20 + 24));
LABEL_22:
v20 = 0LL;
}
return v20;
}

original_code:
static ScarabStream * stream_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;
 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)->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(setSocketOptionFlag(ts->fd, 2) < 0) {
        if(scarab_os_geterr() != 92) {
            scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (4) ));
            close(ts->fd);
            return ((void *)0);
        }
    }
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (4) ));
  return ((void *)0);
 }
 if (bind(ts->fd, (struct sockaddr *)&end_point, sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (6) ));
  close(ts->fd);
  return ((void *)0);
 }
 if (listen(ts->fd, 5) != 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (7) ));
  close(ts->fd);
  return ((void *)0);
 }
 return &ts->header;
}

canonical_original_code:
static struct __ScarabStream *stream_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;
 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)->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(setSocketOptionFlag(ts->fd, 2) < 0) {
        if(scarab_os_geterr() != 92) {
            scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (4) ));
            close(ts->fd);
            return ((void *)0);
        }
    }
 if (ts->fd < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (4) ));
  return ((void *)0);
 }
 if (bind(ts->fd, (struct sockaddr *)&end_point, sizeof(end_point)) < 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->err_code_base + (6) ));
  close(ts->fd);
  return ((void *)0);
 }
 if (listen(ts->fd, 5) != 0) {
  scarab_session_seterr(session, ( (&scarab_stream_tcpip)->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; } *

variable_types: len int

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


decompiled_code:
__int64 __fastcall scarab_hash(__int64 a1, int a2, int a3)
{
int v4; // edx
int v8; // eax
int v9; // ecx
unsigned __int8 *v10; // rdx
__int64 v11; // rdi
int v12; // eax
int v13; // ecx

v4 = *(_DWORD *)(a1 + 24);
if ( v4 == 4 )
return (unsigned int)(a3 % a2);
if ( v4 > 4 )
{
if ( v4 == 7 )
{
v8 = 4 * a3;
v9 = v8 + 4;
if ( *(_DWORD *)a1 <= v8 + 4 )
v9 = *(_DWORD *)a1;
if ( v8 >= v9 )
return 0;
v10 = (unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v8);
v11 = (__int64)&v10[v9 + ~v8 + 1];
v12 = 0;
do
{
v13 = *v10++;
v12 = v13 + (v12 << 8);
}
while ( v10 != (unsigned __int8 *)v11 );
return (unsigned int)(v12 % a2);
}
goto LABEL_7;
}
if ( v4 == 3 )
return (unsigned int)(a3 * a3 % a2);
if ( v4 <= 0 )
{
LABEL_7:
a3 = *(_DWORD *)a1 * (a3 + 1);
return (unsigned int)(a3 % a2);
}
return (unsigned int)(*(_DWORD *)a1 % a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, int a3)
{
int v4; // edx
int v8; // eax
int v9; // ecx
unsigned __int8 *v10; // rdx
__int64 v11; // rdi
int v12; // eax
int v13; // ecx

v4 = *(_DWORD *)(a1 + 24);
if ( v4 == 4 )
return (unsigned int)(a3 % a2);
if ( v4 > 4 )
{
if ( v4 == 7 )
{
v8 = 4 * a3;
v9 = v8 + 4;
if ( *(_DWORD *)a1 <= v8 + 4 )
v9 = *(_DWORD *)a1;
if ( v8 >= v9 )
return 0;
v10 = (unsigned __int8 *)(*(_QWORD *)(a1 + 8) + v8);
v11 = (__int64)&v10[v9 + ~v8 + 1];
v12 = 0;
do
{
v13 = *v10++;
v12 = v13 + (v12 << 8);
}
while ( v10 != (unsigned __int8 *)v11 );
return (unsigned int)(v12 % a2);
}
goto LABEL_7;
}
if ( v4 == 3 )
return (unsigned int)(a3 * a3 % a2);
if ( v4 <= 0 )
{
LABEL_7:
a3 = *(_DWORD *)a1 * (a3 + 1);
return (unsigned int)(a3 % a2);
}
return (unsigned int)(*(_DWORD *)a1 % a2);
}

original_code:
int
scarab_hash(ScarabDatum *datum, int modulo, int atry)
{
 int hv = 0;
 int i = 0;
 int s = 0;
 switch (datum->type)
    {
    case 3:
  hv = atry*atry;
  break;
    case 4:
  hv = atry;
  break;
    case 1:
    case 2:
  hv = (int)datum->data.integer;
  break;
    case 7:
  s = datum->data.opaque.size;
  for (i = 4*atry; i< ((s > (4*atry+4)) ? (4*atry+4) : s); i++)
  {
   hv <<= 8;
   hv += datum->data.opaque.data[i];
  }
  break;
    default:
  hv = datum->data.integer * (atry+1);
    }
 return hv % modulo;
}

canonical_original_code:
int scarab_hash(struct __ScarabDatum *datum, int modulo, int atry)
{
 int hv = 0;
 int i = 0;
 int s = 0;
 switch (datum->type)
    {
    case 3:
  hv = atry*atry;
  break;
    case 4:
  hv = atry;
  break;
    case 1:
    case 2:
  hv = (int)datum->data.integer;
  break;
    case 7:
  s = datum->data.opaque.size;
  for (i = 4*atry; i< ((s > (4*atry+4)) ? (4*atry+4) : s); i++)
  {
   hv <<= 8;
   hv += datum->data.opaque.data[i];
  }
  break;
    default:
  hv = datum->data.integer * (atry+1);
    }
 return hv % modulo;
}

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

variable_types: modulo int

variable_types: atry int

variable_types: hv int

variable_types: i int

variable_types: s int


decompiled_code:
_BOOL8 __fastcall scarab_equals(int *a1, __int64 a2)
{
int v2; // edx
_BOOL8 result; // rax
__int64 v4; // rdx
__int64 v5; // rdi
__int64 v6; // rax

if ( !a1 || !a2 )
return 0LL;
v2 = a1[6];
result = 0LL;
if ( v2 == *(_DWORD *)(a2 + 24) )
{
switch ( v2 )
{
case 0:
case 3:
case 4:
return 1LL;
case 2:
return *(double *)a1 == *(double *)a2;
case 7:
v4 = *a1;
result = 0LL;
if ( (_DWORD)v4 != *(_DWORD *)a2 )
return result;
if ( (int)v4 <= 0 )
return 1LL;
v5 = *((_QWORD *)a1 + 1);
v6 = 0LL;
break;
default:
return *(_QWORD *)a1 == *(_QWORD *)a2;
}
while ( *(_BYTE *)(v5 + v6) == *(_BYTE *)(*(_QWORD *)(a2 + 8) + v6) )
{
if ( v4 == ++v6 )
return 1LL;
}
return 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int *a1, __int64 a2)
{
int v2; // edx
_BOOL8 result; // rax
__int64 v4; // rdx
__int64 v5; // rdi
__int64 v6; // rax

if ( !a1 || !a2 )
return 0LL;
v2 = a1[6];
result = 0LL;
if ( v2 == *(_DWORD *)(a2 + 24) )
{
switch ( v2 )
{
case 0:
case 3:
case 4:
return 1LL;
case 2:
return *(double *)a1 == *(double *)a2;
case 7:
v4 = *a1;
result = 0LL;
if ( (_DWORD)v4 != *(_DWORD *)a2 )
return result;
if ( (int)v4 <= 0 )
return 1LL;
v5 = *((_QWORD *)a1 + 1);
v6 = 0LL;
break;
default:
return *(_QWORD *)a1 == *(_QWORD *)a2;
}
while ( *(_BYTE *)(v5 + v6) == *(_BYTE *)(*(_QWORD *)(a2 + 8) + v6) )
{
if ( v4 == ++v6 )
return 1LL;
}
return 0LL;
}
return result;
}

original_code:
int
scarab_equals(ScarabDatum *datum1, ScarabDatum *datum2)
{
 int i;
 int s1;
 int s2;
 ScarabOpaque *o1;
 ScarabOpaque *o2;
 if (datum1 != ((void *)0)
  && datum2 != ((void *)0)
  && datum1->type == datum2->type)
    {
  switch (datum1->type)
  {
  case 0:
  case 4:
  case 3:
   return 1;
  case 7:
   o1 = &datum1->data.opaque;
   o2 = &datum2->data.opaque;
   s1 = o1->size;
   s2 = o2->size;
   if (s1 != s2)
    return 0;
   for (i = 0; i < s1; i++)
   {
    if (o1->data[i] != o2->data[i])
     return 0;
   }
   return 1;
   break;
  case 1:
   return datum1->data.integer == datum2->data.integer;
  case 2:
   return datum1->data.floatp == datum2->data.floatp;
  default:
   return datum1->data.p == datum2->data.p;
  }
    }
 return 0;
}

canonical_original_code:
int scarab_equals(struct __ScarabDatum *datum1, struct __ScarabDatum *datum2)
{
 int i;
 int s1;
 int s2;
 struct __ScarabOpaque *o1;
 struct __ScarabOpaque *o2;
 if (datum1 != ((void *)0)
  && datum2 != ((void *)0)
  && datum1->type == datum2->type)
    {
  switch (datum1->type)
  {
  case 0:
  case 4:
  case 3:
   return 1;
  case 7:
   o1 = &datum1->data.opaque;
   o2 = &datum2->data.opaque;
   s1 = o1->size;
   s2 = o2->size;
   if (s1 != s2)
    return 0;
   for (i = 0; i < s1; i++)
   {
    if (o1->data[i] != o2->data[i])
     return 0;
   }
   return 1;
   break;
  case 1:
   return datum1->data.integer == datum2->data.integer;
  case 2:
   return datum1->data.floatp == datum2->data.floatp;
  default:
   return datum1->data.p == datum2->data.p;
  }
    }
 return 0;
}

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


decompiled_code:
void scarab_mem_init()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void scarab_mem_init(void)
{
}

canonical_original_code:
void scarab_mem_init()
{
}

return_type: void


decompiled_code:
void *__fastcall scarab_mem_malloc(int a1)
{
return malloc(a1);
}

canonical_decompiled_code:
void *__fastcall func1(int a1)
{
return malloc(a1);
}

original_code:
void *scarab_mem_malloc(int bytes)
{
 return (void *)malloc(bytes);
}

variable_types: bytes int

return_type: void *


decompiled_code:
// attributes: thunk
void scarab_mem_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func2(void *ptr)
{
free(ptr);
}

variable_types:
ptr
void *


decompiled_code:
void scarab_os_init()
{
;
}

original_code:
void
scarab_os_init()
{
}

canonical_original_code:
void scarab_os_init()
{
}


decompiled_code:
// attributes: thunk
char *scarab_os_strerror(int errnum)
{
return strerror(errnum);
}

canonical_decompiled_code:
char *func1(int errnum)
{
return strerror(errnum);
}

original_code:
const char*
scarab_os_strerror(int err)
{
 return strerror(err);
}

canonical_original_code:
char *scarab_os_strerror(int err)
{
 return strerror(err);
}

variable_types: err int


decompiled_code:
__int64 scarab_os_geterr()
{
return (unsigned int)*_errno_location();
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)*_errno_location();
}

original_code:
int
scarab_os_geterr()
{
 return (*__errno_location ());
}


decompiled_code:
__int64 __fastcall scarab_list_new(int a1)
{
__int64 v1; // r12
int *v2; // rax
int *v3; // rbp
void *v4; // rax
__int64 result; // rax

v1 = scarab_new_molecular();
v2 = (int *)scarab_mem_malloc(16LL);
*v2 = a1;
v3 = v2;
v4 = (void *)scarab_mem_malloc((unsigned int)(8 * a1));
*((_QWORD *)v3 + 1) = v4;
if ( a1 > 0 )
memset(v4, 0, 8LL * a1);
*(_QWORD *)v1 = v3;
result = v1;
*(_DWORD *)(v1 + 24) = 6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 v1; // r12
int *v2; // rax
int *v3; // rbp
void *v4; // rax
__int64 result; // rax

v1 = scarab_new_molecular();
v2 = (int *)scarab_mem_malloc(16LL);
*v2 = a1;
v3 = v2;
v4 = (void *)scarab_mem_malloc((unsigned int)(8 * a1));
*((_QWORD *)v3 + 1) = v4;
if ( a1 > 0 )
memset(v4, 0, 8LL * a1);
*(_QWORD *)v1 = v3;
result = v1;
*(_DWORD *)(v1 + 24) = 6;
return result;
}

original_code:
ScarabDatum*
scarab_list_new(int initialsize)
{
 int i;
 ScarabDatum *d = scarab_new_molecular();
 ScarabList *list;
 list = (ScarabList*)scarab_mem_malloc(sizeof(ScarabList));
 list->size = initialsize;
 list->values = (ScarabDatum**)scarab_mem_malloc(initialsize * sizeof(void*));
 for (i = 0; i < initialsize; i++)
 {
  list->values[i] = ((void *)0);
 }
 d->data.list = list;
 d->type = 6;
 return d;
}

canonical_original_code:
struct __ScarabDatum *scarab_list_new(int initialsize)
{
 int i;
 struct __ScarabDatum *d = scarab_new_molecular();
 struct __ScarabList *list;
 list = (struct __ScarabList *)scarab_mem_malloc(sizeof(ScarabList));
 list->size = initialsize;
 list->values = (struct __ScarabDatum **)scarab_mem_malloc(initialsize * sizeof(void *));
 for (i = 0; i < initialsize; i++)
 {
  list->values[i] = ((void *)0);
 }
 d->data.list = list;
 d->type = 6;
 return d;
}

variable_types: initialsize int

variable_types: 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; } *

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_list_put(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rdx
__int64 *v5; // rax
__int64 v6; // r13
__int64 v7; // rdx

scarab_lock_datum(a1);
scarab_lock_datum(a3);
v5 = (__int64 *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL) + 8LL * (int)a2);
v6 = *v5;
*v5 = a3;
if ( a3 )
++*(_DWORD *)(a3 + 28);
scarab_unlock_datum(a3, a2, v4);
scarab_unlock_datum(a1, a2, v7);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rdx
__int64 *v5; // rax
__int64 v6; // r13
__int64 v7; // rdx

scarab_lock_datum(a1);
scarab_lock_datum(a3);
v5 = (__int64 *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL) + 8LL * (int)a2);
v6 = *v5;
*v5 = a3;
if ( a3 )
++*(_DWORD *)(a3 + 28);
scarab_unlock_datum(a3, a2, v4);
scarab_unlock_datum(a1, a2, v7);
return v6;
}

original_code:
ScarabDatum*
scarab_list_put(ScarabDatum *list, int idx, ScarabDatum *val)
{
 scarab_lock_datum(list);
 scarab_lock_datum(val);
 ScarabList *l = list->data.list;
 ScarabDatum *oldval = l->values[idx];
 l->values[idx] = val;
 if(val != ((void *)0)){
  val->ref_count++;
 }
 scarab_unlock_datum(val);
 scarab_unlock_datum(list);
 return oldval;
}

canonical_original_code:
struct __ScarabDatum *scarab_list_put(struct __ScarabDatum *list, int idx, struct __ScarabDatum *val)
{
 scarab_lock_datum(list);
 scarab_lock_datum(val);
 struct __ScarabList *l = list->data.list;
 struct __ScarabDatum *oldval = l->values[idx];
 l->values[idx] = val;
 if(val != ((void *)0)){
  val->ref_count++;
 }
 scarab_unlock_datum(val);
 scarab_unlock_datum(list);
 return oldval;
}

variable_types:
list
struct __ScarabDatum *

variable_types: idx int

variable_types:
val
struct __ScarabDatum *


decompiled_code:
__int64 __fastcall scarab_list_get(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rdx

scarab_lock_datum(a1);
v2 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL) + 8LL * (int)a2);
scarab_unlock_datum(a1, a2, v3);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rdx

scarab_lock_datum(a1);
v2 = *(_QWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 8LL) + 8LL * (int)a2);
scarab_unlock_datum(a1, a2, v3);
return v2;
}

original_code:
ScarabDatum*
scarab_list_get(ScarabDatum *list, int idx)
{
 scarab_lock_datum(list);
 ScarabList *l = list->data.list;
 ScarabDatum *value = l->values[idx];
 scarab_unlock_datum(list);
 return value;
}

canonical_original_code:
struct __ScarabDatum *scarab_list_get(struct __ScarabDatum *list, int idx)
{
 scarab_lock_datum(list);
 struct __ScarabList *l = list->data.list;
 struct __ScarabDatum *value = l->values[idx];
 scarab_unlock_datum(list);
 return value;
}


decompiled_code:
double __fastcall scarab_extract_float(__int64 a1)
{
return **(double **)(a1 + 8);
}

canonical_decompiled_code:
double __fastcall func1(__int64 a1)
{
return **(double **)(a1 + 8);
}

original_code:
double scarab_extract_float(ScarabDatum *datum){
 return *((double *)(datum->data.opaque.data));
}

canonical_original_code:
double scarab_extract_float(struct __ScarabDatum *datum){
 return *((double *)(datum->data.opaque.data));
}

return_type: double


repo: haibbo/improxy

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

v1 = *a1;
v2 = *(_QWORD *)*a1;
if ( v2 )
*(_QWORD *)(v2 + 8) = v1[1];
*(_QWORD *)v1[1] = v2;
free(*a1);
*a1 = 0LL;
}

original_code:
void imp_remove_timer(imp_timer **timer)
{
    do { if ((*timer)->link.le_next != ((void *)0)) (*timer)->link.le_next->link.le_prev = (*timer)->link.le_prev; *(*timer)->link.le_prev = (*timer)->link.le_next; } while ( 0);
    free(*timer);
    *timer = ((void *)0);
}

canonical_original_code:
void imp_remove_timer(struct imp_timer **timer)
{
    do { if ((*timer)->link.le_next != ((void *)0)) (*timer)->link.le_next->link.le_prev = (*timer)->link.le_prev; *(*timer)->link.le_prev = (*timer)->link.le_next; } while ( 0);
    free(*timer);
    *timer = ((void *)0);
}

variable_types: struct imp_timer { struct { struct imp_timer *le_next; struct imp_timer **le_prev; } link; struct timeval { long tv_sec; long tv_usec; } tm; struct imp_timer *(*expire)(void *); void *expire_data; } **

user_defined_types: struct imp_timer { struct { struct imp_timer *le_next; struct imp_timer **le_prev; } link; struct timeval tm; struct imp_timer *(*expire)(void *); void *expire_data; }


decompiled_code:
void imp_init_timer()
{
timer_head = 0LL;
}

canonical_decompiled_code:
void func1()
{
timer_head = 0LL;
}

original_code:
void imp_init_timer(void)
{
    do { (&timer_head)->lh_first = ((void *)0); } while ( 0);
}

canonical_original_code:
void imp_init_timer()
{
    do { (&timer_head)->lh_first = ((void *)0); } while ( 0);
}


decompiled_code:
__int64 __fastcall imp_set_timer(__int64 a1, __m128i *a2)
{
__int64 result; // rax
__m128i v3[2]; // [rsp+0h] [rbp-28h] BYREF

if ( !a2 )
return logger_build(1LL, "ERROR", "imp_set_timer", 84LL, "timer is null");
result = get_sysuptime(v3);
if ( (int)result < 0 )
return logger_build(1LL, "ERROR", "imp_set_timer", 89LL, "Can't get time of now");
v3[0].m128i_i64[0] += a1;
a2[1] = _mm_loadu_si128(v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __m128i *a2)
{
__int64 result; // rax
__m128i v3[2]; // [rsp+0h] [rbp-28h] BYREF

if ( !a2 )
return logger_build(1LL, "ERROR", "imp_set_timer", 84LL, "timer is null");
result = get_sysuptime(v3);
if ( (int)result < 0 )
return logger_build(1LL, "ERROR", "imp_set_timer", 89LL, "Can't get time of now");
v3[0].m128i_i64[0] += a1;
a2[1] = _mm_loadu_si128(v3);
return result;
}

original_code:
void imp_set_timer(time_t tv_sec, imp_timer *timer)
{
    struct timeval now;
    if(timer == ((void *)0)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 84, "timer is null");
        return;
    }
    if(get_sysuptime(&now) < 0) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 89, "Can't get time of now");
        return;
    }
    now.tv_sec += tv_sec;
    memcpy(&timer->tm, &now, sizeof(struct timeval));
    return;
}

canonical_original_code:
void imp_set_timer(long tv_sec, struct imp_timer *timer)
{
    struct timeval now;
    if(timer == ((void *)0)) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 84, "timer is null");
        return;
    }
    if(get_sysuptime(&now) < 0) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 89, "Can't get time of now");
        return;
    }
    now.tv_sec += tv_sec;
    memcpy(&timer->tm, &now, sizeof(struct timeval));
    return;
}

variable_types: struct imp_timer { struct { struct imp_timer *le_next; struct imp_timer **le_prev; } link; struct timeval { long tv_sec; long tv_usec; } tm; struct imp_timer *(*expire)(void *); void *expire_data; } *


decompiled_code:
__int64 __fastcall imp_time_val(__int64 a1, __m128i *a2)
{
__int64 result; // rax
__m128i v3[2]; // [rsp+0h] [rbp-28h] BYREF

result = get_sysuptime(v3);
if ( (int)result < 0 )
return logger_build(1LL, "ERROR", "imp_time_val", 106LL, "Can't get time of now");
v3[0].m128i_i64[0] += a1;
*a2 = _mm_loadu_si128(v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __m128i *a2)
{
__int64 result; // rax
__m128i v3[2]; // [rsp+0h] [rbp-28h] BYREF

result = get_sysuptime(v3);
if ( (int)result < 0 )
return logger_build(1LL, "ERROR", "imp_time_val", 106LL, "Can't get time of now");
v3[0].m128i_i64[0] += a1;
*a2 = _mm_loadu_si128(v3);
return result;
}

original_code:
void imp_time_val(time_t tv_sec, struct timeval *result)
{
    struct timeval now;
    if(get_sysuptime(&now) < 0) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 106, "Can't get time of now");
        return;
    }
    now.tv_sec += tv_sec;
    memcpy(result, &now, sizeof(struct timeval));
    return;
}

canonical_original_code:
void imp_time_val(long tv_sec, struct timeval *result)
{
    struct timeval now;
    if(get_sysuptime(&now) < 0) {
        logger_build(LOG_PRI_ERROR, "ERROR", __FUNCTION__, 106, "Can't get time of now");
        return;
    }
    now.tv_sec += tv_sec;
    memcpy(result, &now, sizeof(struct timeval));
    return;
}

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


original_code:
struct timeval* imp_check_timer(void)
{
    static struct timeval returnval;
    imp_timer *mt, *mt_next;
    struct timeval *w = &tm_max;
    struct timeval now;
    int res = 0;
    for (mt = ((&timer_head)->lh_first); mt; mt = mt_next)
    {
        mt_next = ((mt)->link.le_next);
        memset(&now, 0, sizeof(now));
        if ((res = get_sysuptime(&now)) == -1 ){
            returnval.tv_sec = returnval.tv_usec = 0;
            return (&returnval);
        }
        if(((mt->tm).tv_sec == 0 && (mt->tm).tv_usec == 0))
            continue;
        if ((((mt->tm).tv_sec < (now).tv_sec) || (((mt->tm).tv_sec == (now).tv_sec) && ((mt->tm).tv_usec < (now).tv_usec)))) {
            if (mt->expire(mt->expire_data) == ((void *)0)) {
                logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 171, "continue............\n");
                mt_next = ((&timer_head)->lh_first);
                w = &tm_max;
                continue;
            }
        }
        if ((((mt->tm).tv_sec < (*w).tv_sec) || (((mt->tm).tv_sec == (*w).tv_sec) && ((mt->tm).tv_usec < (*w).tv_usec))))
            w = &mt->tm;
    }
    if (((*w).tv_sec == (tm_max).tv_sec && (*w).tv_usec == (tm_max).tv_usec))
        return ((void *)0);
    if ((res = get_sysuptime(&now)) == -1 ){
        returnval.tv_sec = returnval.tv_usec = 0;
        return (&returnval);
    }
    if ((((now).tv_sec < (*w).tv_sec) || (((now).tv_sec == (*w).tv_sec) && ((now).tv_usec < (*w).tv_usec)))){
        memcpy(&returnval, w, sizeof(returnval));
        timeval_sub(w, &now, &returnval);
    }
    else {
        returnval.tv_sec = returnval.tv_usec = 0;
    }
    return &returnval;
}

canonical_original_code:
struct timeval *imp_check_timer()
{
    static struct timeval returnval;
    struct imp_timer *mt; struct imp_timer *mt_next;
    struct timeval *w = &tm_max;
    struct timeval now;
    int res = 0;
    for (mt = ((&timer_head)->lh_first); mt; mt = mt_next)
    {
        mt_next = ((mt)->link.le_next);
        memset(&now, 0, sizeof(now));
        if ((res = get_sysuptime(&now)) == -1 ){
            returnval.tv_sec = returnval.tv_usec = 0;
            return (&returnval);
        }
        if(((mt->tm).tv_sec == 0 && (mt->tm).tv_usec == 0))
            continue;
        if ((((mt->tm).tv_sec < (now).tv_sec) || (((mt->tm).tv_sec == (now).tv_sec) && ((mt->tm).tv_usec < (now).tv_usec)))) {
            if (mt->expire(mt->expire_data) == ((void *)0)) {
                logger_build(LOG_PRI_DEBUG, "DEBUG", __FUNCTION__, 171, "continue............\n");
                mt_next = ((&timer_head)->lh_first);
                w = &tm_max;
                continue;
            }
        }
        if ((((mt->tm).tv_sec < (*w).tv_sec) || (((mt->tm).tv_sec == (*w).tv_sec) && ((mt->tm).tv_usec < (*w).tv_usec))))
            w = &mt->tm;
    }
    if (((*w).tv_sec == (tm_max).tv_sec && (*w).tv_usec == (tm_max).tv_usec))
        return ((void *)0);
    if ((res = get_sysuptime(&now)) == -1 ){
        returnval.tv_sec = returnval.tv_usec = 0;
        return (&returnval);
    }
    if ((((now).tv_sec < (*w).tv_sec) || (((now).tv_sec == (*w).tv_sec) && ((now).tv_usec < (*w).tv_usec)))){
        memcpy(&returnval, w, sizeof(returnval));
        timeval_sub(w, &now, &returnval);
    }
    else {
        returnval.tv_sec = returnval.tv_usec = 0;
    }
    return &returnval;
}

variable_types: res int

return_type: struct timeval { long tv_sec; long tv_usec; } *