original_code:
static int session_is_closed(struct fctl_session_t* session){
    if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return 0;
    }
    if (session->local_status == SESSION_CLOSED
        && session->remote_status == SESSION_CLOSED){
        return 1;
    }
    if ((session->local_status == SESSION_CLOSED
           || session->remote_status == SESSION_CLOSED)
        && ( (time(0) - session->closing_time) >= 60)
       )
       return 1;
    if ( (session->local_status == SESSION_CLOSING
           || session->remote_status == SESSION_CLOSING)
        && ( (time(0) - session->closing_time) >= 60)
       )
       return 1;
    return 0;
}

canonical_original_code:
static int session_is_closed(struct fctl_session_t *session){
    if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return 0;
    }
    if (session->local_status == SESSION_CLOSED
        && session->remote_status == SESSION_CLOSED){
        return 1;
    }
    if ((session->local_status == SESSION_CLOSED
           || session->remote_status == SESSION_CLOSED)
        && ( (time(0) - session->closing_time) >= 60)
       )
       return 1;
    if ( (session->local_status == SESSION_CLOSING
           || session->remote_status == SESSION_CLOSING)
        && ( (time(0) - session->closing_time) >= 60)
       )
       return 1;
    return 0;
}

variable_types: struct fctl_session_t { struct list_head { struct list_head *next; } list; struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 } proxy_type; int (*on_local_rev)(struct fctl_session_t *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t { int session_id; int event_type; int event_len; } *); } *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); } *proxy; int local_fd; int remote_fd; 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]; } local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t { int cache_count; struct list_head cache_head; } cache_pool; } local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t { int fd; char name[512]; int size; int exists; } spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); } *

user_defined_types: struct fctl_session_t { struct list_head list; struct fctl_proxy_t *proxy; int local_fd; int remote_fd; struct sockaddr_in local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); }


repo: dzli/hproxy

decompiled_code:
__int64 __fastcall session_close(__int64 a1)
{
int v2; // edi
int v3; // edi
__int64 result; // rax

if ( a1 )
{
v2 = *(_DWORD *)(a1 + 28);
if ( v2 > 0 )
{
while ( close(v2) == -1 && *_errno_location() == 4 )
v2 = *(_DWORD *)(a1 + 28);
}
v3 = *(_DWORD *)(a1 + 24);
if ( v3 > 0 )
{
while ( close(v3) == -1 && *_errno_location() == 4 )
v3 = *(_DWORD *)(a1 + 24);
}
*(_QWORD *)(a1 + 24) = -1LL;
cp_destroy(a1 + 67744);
cp_destroy(a1 + 33896);
*(_QWORD *)(a1 + 67768) = 0x400000004LL;
result = 0LL;
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

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

if ( a1 )
{
v2 = *(_DWORD *)(a1 + 28);
if ( v2 > 0 )
{
while ( close(v2) == -1 && *_errno_location() == 4 )
v2 = *(_DWORD *)(a1 + 28);
}
v3 = *(_DWORD *)(a1 + 24);
if ( v3 > 0 )
{
while ( close(v3) == -1 && *_errno_location() == 4 )
v3 = *(_DWORD *)(a1 + 24);
}
*(_QWORD *)(a1 + 24) = -1LL;
cp_destroy(a1 + 67744);
cp_destroy(a1 + 33896);
*(_QWORD *)(a1 + 67768) = 0x400000004LL;
result = 0LL;
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int session_close(struct fctl_session_t* session){
    if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
     if (session->remote_fd > 0) {while (close(session->remote_fd) == -1 && (*__errno_location ()) == 4);} ;
     if (session->local_fd > 0) {while (close(session->local_fd) == -1 && (*__errno_location ()) == 4);} ;
     session->remote_fd = -1;
     session->local_fd = -1;
     cp_destroy(&session->remote_buf.cache_pool);
     cp_destroy(&session->local_buf.cache_pool);
     session->local_status = SESSION_CLOSED;
     session->remote_status = SESSION_CLOSED;
    return 0;
}

canonical_original_code:
static int session_close(struct fctl_session_t *session){
    if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
     if (session->remote_fd > 0) {while (close(session->remote_fd) == -1 && (*__errno_location ()) == 4);} ;
     if (session->local_fd > 0) {while (close(session->local_fd) == -1 && (*__errno_location ()) == 4);} ;
     session->remote_fd = -1;
     session->local_fd = -1;
     cp_destroy(&session->remote_buf.cache_pool);
     cp_destroy(&session->local_buf.cache_pool);
     session->local_status = SESSION_CLOSED;
     session->remote_status = SESSION_CLOSED;
    return 0;
}

return_type: int

user_defined_types: struct list_head { struct list_head *next; }

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 rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t cache_pool; }

user_defined_types: struct fctl_spool_t { int fd; char name[512]; int size; int exists; }

user_defined_types: struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); }

user_defined_types: struct cache_pool_t { int cache_count; struct list_head cache_head; }

user_defined_types: struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE proxy_type; int (*on_local_rev)(struct fctl_session_t *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t *); }

user_defined_types: enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 }

user_defined_types: struct event_header_t { int session_id; int event_type; int event_len; }


decompiled_code:
__int64 __fastcall session_check_remote_extra_data(__int64 a1)
{
__int64 v1; // r12
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // ebp

v1 = a1 + 33920;
result = rwbuff_get_sendn(a1 + 33920);
if ( !(_DWORD)result )
{
result = rwbuff_get_readn(v1);
if ( !(_DWORD)result )
{
result = cp_is_empty(a1 + 67744);
if ( !(_DWORD)result )
{
v3 = rwbuff_get_writep(v1);
result = rwbuff_get_writen(v1);
if ( (_DWORD)result )
{
result = cp_read(a1 + 67744, v3, (unsigned int)result);
v4 = result;
if ( (int)result > 0 )
{
rwbuff_write(v1, (unsigned int)result);
result = rwbuff_read(v1, v4);
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r12
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // ebp

v1 = a1 + 33920;
result = rwbuff_get_sendn(a1 + 33920);
if ( !(_DWORD)result )
{
result = rwbuff_get_readn(v1);
if ( !(_DWORD)result )
{
result = cp_is_empty(a1 + 67744);
if ( !(_DWORD)result )
{
v3 = rwbuff_get_writep(v1);
result = rwbuff_get_writen(v1);
if ( (_DWORD)result )
{
result = cp_read(a1 + 67744, v3, (unsigned int)result);
v4 = result;
if ( (int)result > 0 )
{
rwbuff_write(v1, (unsigned int)result);
result = rwbuff_read(v1, v4);
}
}
}
}
}
return result;
}

original_code:
static void session_check_remote_extra_data(struct fctl_session_t* session){
    if (rwbuff_get_sendn(&session->remote_buf) != 0
        || rwbuff_get_readn(&session->remote_buf)!= 0) return;
    if (cp_is_empty(&session->remote_buf.cache_pool) ) return;
    char * buf = rwbuff_get_writep(&session->remote_buf);
    int space = rwbuff_get_writen(&session->remote_buf);
    if ( space == 0) {
        return ;
    }
    int n = cp_read(&session->remote_buf.cache_pool, buf , space);
    if (n > 0){
        rwbuff_write(&session->remote_buf , n);
        rwbuff_read(&session->remote_buf, n);
    }
}

canonical_original_code:
static void session_check_remote_extra_data(struct fctl_session_t *session){
    if (rwbuff_get_sendn(&session->remote_buf) != 0
        || rwbuff_get_readn(&session->remote_buf)!= 0) return;
    if (cp_is_empty(&session->remote_buf.cache_pool) ) return;
    char *buf = rwbuff_get_writep(&session->remote_buf);
    int space = rwbuff_get_writen(&session->remote_buf);
    if ( space == 0) {
        return ;
    }
    int n = cp_read(&session->remote_buf.cache_pool, buf , space);
    if (n > 0){
        rwbuff_write(&session->remote_buf , n);
        rwbuff_read(&session->remote_buf, n);
    }
}

variable_types:
buf
char *

variable_types: space int

variable_types: n int

return_type: void


decompiled_code:
__int64 __fastcall session_on_remote_send(__int64 a1)
{
__int64 v2; // r13
__int64 v3; // r12
int v4; // edi
int v5; // eax
unsigned int v6; // ebp
__int64 result; // rax
char *v8; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
session_check_remote_extra_data(a1);
v2 = rwbuff_get_sendp(a1 + 33920);
v3 = (unsigned int)rwbuff_get_sendn(a1 + 33920);
log_debug("%d data in remote buffer need to be sent!", v3);
if ( (_DWORD)v3 )
{
do
{
v5 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 104LL))(
*(unsigned int *)(a1 + 24),
v2,
(unsigned int)v3);
v6 = v5;
if ( v5 > 0 )
{
log_debug(" %d data in remote buffer are sent successfully.", (unsigned int)v5);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_send(a1 + 33920, v6);
session_check_remote_extra_data(a1);
goto LABEL_8;
}
v4 = *_errno_location();
}
while ( v4 == 4 );
if ( v4 == 11 )
{
result = 1LL;
}
else
{
v8 = strerror(v4);
log_error("write error:%s", v8);
result = 4294967294LL;
}
}
else
{
log_debug("no data in remote buffer need to be sent");
LABEL_8:
result = rwbuff_get_sendn(a1 + 33920);
if ( (_DWORD)result )
{
result = 0LL;
}
else if ( *(_DWORD *)(a1 + 67768) == 3 )
{
*(_DWORD *)(a1 + 67768) = 4;
}
}
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v2; // r13
__int64 v3; // r12
int v4; // edi
int v5; // eax
unsigned int v6; // ebp
__int64 result; // rax
char *v8; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
func2(a1);
v2 = rwbuff_get_sendp(a1 + 33920);
v3 = (unsigned int)rwbuff_get_sendn(a1 + 33920);
log_debug("%d data in remote buffer need to be sent!", v3);
if ( (_DWORD)v3 )
{
do
{
v5 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 104LL))(
*(unsigned int *)(a1 + 24),
v2,
(unsigned int)v3);
v6 = v5;
if ( v5 > 0 )
{
log_debug(" %d data in remote buffer are sent successfully.", (unsigned int)v5);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_send(a1 + 33920, v6);
func2(a1);
goto LABEL_8;
}
v4 = *_errno_location();
}
while ( v4 == 4 );
if ( v4 == 11 )
{
result = 1LL;
}
else
{
v8 = strerror(v4);
log_error("write error:%s", v8);
result = 4294967294LL;
}
}
else
{
log_debug("no data in remote buffer need to be sent");
LABEL_8:
result = rwbuff_get_sendn(a1 + 33920);
if ( (_DWORD)result )
{
result = 0LL;
}
else if ( *(_DWORD *)(a1 + 67768) == 3 )
{
*(_DWORD *)(a1 + 67768) = 4;
}
}
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int session_on_remote_send(struct fctl_session_t* session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    session_check_remote_extra_data(session);
    char* buf = rwbuff_get_sendp(&session->remote_buf);
    int space = rwbuff_get_sendn(&session->remote_buf);
    log_debug("%d data in remote buffer need to be sent!", space);;
    if ( space == 0){
        log_debug("no data in remote buffer need to be sent");;
        goto ret;
    }
    int n;
again:
    n = session->proxy->write(session->local_fd, buf , space);
    if (n <= 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11){
            log_error("write error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 1;
    }
    log_debug(" %d data in remote buffer are sent successfully.", n);;
     session->closing_time = time(0);
    rwbuff_send(&session->remote_buf, n);
    session_check_remote_extra_data(session);
ret :
     if ( rwbuff_get_sendn(&session->remote_buf) == 0
        && session->remote_status == SESSION_CLOSING) {
        session->remote_status = SESSION_CLOSED;
    }
    return 0;
}

canonical_original_code:
static int session_on_remote_send(struct fctl_session_t *session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    session_check_remote_extra_data(session);
    char *buf = rwbuff_get_sendp(&session->remote_buf);
    int space = rwbuff_get_sendn(&session->remote_buf);
    log_debug("%d data in remote buffer need to be sent!", space);;
    if ( space == 0){
        log_debug("no data in remote buffer need to be sent");;
        goto ret;
    }
    int n;
again:
    n = session->proxy->write(session->local_fd, buf , space);
    if (n <= 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11){
            log_error("write error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 1;
    }
    log_debug(" %d data in remote buffer are sent successfully.", n);;
     session->closing_time = time(0);
    rwbuff_send(&session->remote_buf, n);
    session_check_remote_extra_data(session);
ret :
     if ( rwbuff_get_sendn(&session->remote_buf) == 0
        && session->remote_status == SESSION_CLOSING) {
        session->remote_status = SESSION_CLOSED;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall session_check_local_extra_data(__int64 a1)
{
__int64 v1; // r12
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // ebp

v1 = a1 + 72;
result = rwbuff_get_sendn(a1 + 72);
if ( !(_DWORD)result )
{
result = rwbuff_get_readn(v1);
if ( !(_DWORD)result )
{
result = cp_is_empty(a1 + 33896);
if ( !(_DWORD)result )
{
v3 = rwbuff_get_writep(v1);
result = rwbuff_get_writen(v1);
if ( (_DWORD)result )
{
result = cp_read(a1 + 33896, v3, (unsigned int)result);
v4 = result;
if ( (int)result > 0 )
{
rwbuff_write(v1, (unsigned int)result);
result = rwbuff_read(v1, v4);
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // r12
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // ebp

v1 = a1 + 72;
result = rwbuff_get_sendn(a1 + 72);
if ( !(_DWORD)result )
{
result = rwbuff_get_readn(v1);
if ( !(_DWORD)result )
{
result = cp_is_empty(a1 + 33896);
if ( !(_DWORD)result )
{
v3 = rwbuff_get_writep(v1);
result = rwbuff_get_writen(v1);
if ( (_DWORD)result )
{
result = cp_read(a1 + 33896, v3, (unsigned int)result);
v4 = result;
if ( (int)result > 0 )
{
rwbuff_write(v1, (unsigned int)result);
result = rwbuff_read(v1, v4);
}
}
}
}
}
return result;
}

original_code:
static void session_check_local_extra_data(struct fctl_session_t* session){
    if (rwbuff_get_sendn(&session->local_buf) != 0
        || rwbuff_get_readn(&session->local_buf)!= 0) return;
    if (cp_is_empty(&session->local_buf.cache_pool) ) return;
    char * buf = rwbuff_get_writep(&session->local_buf);
    int space = rwbuff_get_writen(&session->local_buf);
    if ( space == 0) {
        return ;
    }
    int n = cp_read(&session->local_buf.cache_pool, buf , space);
    if (n > 0){
        rwbuff_write(&session->local_buf , n);
        rwbuff_read(&session->local_buf, n);
    }
}

canonical_original_code:
static void session_check_local_extra_data(struct fctl_session_t *session){
    if (rwbuff_get_sendn(&session->local_buf) != 0
        || rwbuff_get_readn(&session->local_buf)!= 0) return;
    if (cp_is_empty(&session->local_buf.cache_pool) ) return;
    char *buf = rwbuff_get_writep(&session->local_buf);
    int space = rwbuff_get_writen(&session->local_buf);
    if ( space == 0) {
        return ;
    }
    int n = cp_read(&session->local_buf.cache_pool, buf , space);
    if (n > 0){
        rwbuff_write(&session->local_buf , n);
        rwbuff_read(&session->local_buf, n);
    }
}


decompiled_code:
__int64 __fastcall session_on_local_send(__int64 a1)
{
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // eax
unsigned int v5; // er12
int v6; // edi
int v7; // ebp
char *v8; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
if ( *(_DWORD *)(a1 + 67768) != 2 )
return 0LL;
session_check_local_extra_data(a1);
v3 = rwbuff_get_sendp(a1 + 72);
v4 = rwbuff_get_sendn(a1 + 72);
v5 = v4;
if ( v4 )
{
log_debug("%d data in local buffer need to be sent!", v4);
do
{
v7 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 104LL))(
*(unsigned int *)(a1 + 28),
v3,
v5);
if ( v7 > 0 )
{
*(_DWORD *)(a1 + 67808) = time(0LL);
log_debug(" %d data in local buffer are sent successfully.", (unsigned int)v7);
rwbuff_send(a1 + 72, (unsigned int)v7);
session_check_local_extra_data(a1);
goto LABEL_10;
}
v6 = *_errno_location();
}
while ( v6 == 4 );
if ( v6 == 11 )
{
result = 1LL;
}
else
{
v8 = strerror(v6);
log_error("write error:%s", v8);
result = 4294967294LL;
}
}
else
{
log_debug("no data in local buffer need to be sent");
LABEL_10:
result = rwbuff_get_sendn(a1 + 72);
if ( (_DWORD)result || *(_DWORD *)(a1 + 67772) != 3 )
return 0LL;
*(_DWORD *)(a1 + 67772) = 4;
}
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 result; // rax
__int64 v3; // r13
unsigned int v4; // eax
unsigned int v5; // er12
int v6; // edi
int v7; // ebp
char *v8; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
if ( *(_DWORD *)(a1 + 67768) != 2 )
return 0LL;
func4(a1);
v3 = rwbuff_get_sendp(a1 + 72);
v4 = rwbuff_get_sendn(a1 + 72);
v5 = v4;
if ( v4 )
{
log_debug("%d data in local buffer need to be sent!", v4);
do
{
v7 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 104LL))(
*(unsigned int *)(a1 + 28),
v3,
v5);
if ( v7 > 0 )
{
*(_DWORD *)(a1 + 67808) = time(0LL);
log_debug(" %d data in local buffer are sent successfully.", (unsigned int)v7);
rwbuff_send(a1 + 72, (unsigned int)v7);
func4(a1);
goto LABEL_10;
}
v6 = *_errno_location();
}
while ( v6 == 4 );
if ( v6 == 11 )
{
result = 1LL;
}
else
{
v8 = strerror(v6);
log_error("write error:%s", v8);
result = 4294967294LL;
}
}
else
{
log_debug("no data in local buffer need to be sent");
LABEL_10:
result = rwbuff_get_sendn(a1 + 72);
if ( (_DWORD)result || *(_DWORD *)(a1 + 67772) != 3 )
return 0LL;
*(_DWORD *)(a1 + 67772) = 4;
}
}
else
{
log_error("session can not be NULL!");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int session_on_local_send(struct fctl_session_t* session){
     if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    if (session->remote_status != SESSION_CONNECTED)
        return 0;
    session_check_local_extra_data(session);
    char* local_buf = rwbuff_get_sendp(&session->local_buf);
    int space = rwbuff_get_sendn(&session->local_buf);
    if ( space == 0){
        log_debug("no data in local buffer need to be sent");;
        goto ret;
    }
    log_debug("%d data in local buffer need to be sent!", space);;
    int n;
again:
    n = session->proxy->write(session->remote_fd, local_buf , space);
    if (n <= 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11 ){
            log_error("write error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 1;
    }
    session->closing_time = time(0);
    log_debug(" %d data in local buffer are sent successfully.", n);;
    rwbuff_send(&session->local_buf, n);
    session_check_local_extra_data(session);
ret:
    if ( rwbuff_get_sendn(&session->local_buf) == 0
        && session->local_status == SESSION_CLOSING) {
        session->local_status = SESSION_CLOSED;
    }
    return 0;
}

canonical_original_code:
static int session_on_local_send(struct fctl_session_t *session){
     if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    if (session->remote_status != SESSION_CONNECTED)
        return 0;
    session_check_local_extra_data(session);
    char *local_buf = rwbuff_get_sendp(&session->local_buf);
    int space = rwbuff_get_sendn(&session->local_buf);
    if ( space == 0){
        log_debug("no data in local buffer need to be sent");;
        goto ret;
    }
    log_debug("%d data in local buffer need to be sent!", space);;
    int n;
again:
    n = session->proxy->write(session->remote_fd, local_buf , space);
    if (n <= 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11 ){
            log_error("write error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 1;
    }
    session->closing_time = time(0);
    log_debug(" %d data in local buffer are sent successfully.", n);;
    rwbuff_send(&session->local_buf, n);
    session_check_local_extra_data(session);
ret:
    if ( rwbuff_get_sendn(&session->local_buf) == 0
        && session->local_status == SESSION_CLOSING) {
        session->local_status = SESSION_CLOSED;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall session_on_remote_recv(__int64 a1)
{
__int64 v2; // r14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
int v6; // eax
unsigned int v7; // er12
__int64 v8; // r12
unsigned int v9; // eax
unsigned int v10; // er12
char *v12; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
v2 = a1 + 33920;
v3 = rwbuff_get_writep(a1 + 33920);
v4 = (unsigned int)rwbuff_get_writen(a1 + 33920);
log_debug("%d space left in remote buffuser.", v4);
if ( (_DWORD)v4 )
{
while ( 1 )
{
v6 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 96LL))(
*(unsigned int *)(a1 + 28),
v3,
(unsigned int)v4);
v7 = v6;
if ( v6 >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 4 )
{
if ( v5 == 11 )
goto LABEL_9;
v12 = strerror(v5);
v10 = -2;
log_debug("remote read error:%s", v12);
return v10;
}
}
if ( !v6 )
{
*(_DWORD *)(a1 + 67768) = 3;
*(_DWORD *)(a1 + 67808) = time(0LL);
(*(void (__fastcall **)(__int64, _QWORD, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL) + 40LL))(
a1,
0LL,
0LL);
log_debug("FIN is received from remote.");
return v7;
}
log_debug("%d data are received from remote.", (unsigned int)v6);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_write(v2, v7);
v8 = rwbuff_get_readp(v2);
v9 = rwbuff_get_readn(v2);
if ( (*(int (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL) + 40LL))(
a1,
v8,
v9) >= 0 )
goto LABEL_9;
v10 = -3;
}
else
{
LABEL_9:
v10 = 0;
}
}
else
{
v10 = -1;
log_error("session can not be NULL!");
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 v2; // r14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
int v6; // eax
unsigned int v7; // er12
__int64 v8; // r12
unsigned int v9; // eax
unsigned int v10; // er12
char *v12; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
v2 = a1 + 33920;
v3 = rwbuff_get_writep(a1 + 33920);
v4 = (unsigned int)rwbuff_get_writen(a1 + 33920);
log_debug("%d space left in remote buffuser.", v4);
if ( (_DWORD)v4 )
{
while ( 1 )
{
v6 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 96LL))(
*(unsigned int *)(a1 + 28),
v3,
(unsigned int)v4);
v7 = v6;
if ( v6 >= 0 )
break;
v5 = *_errno_location();
if ( v5 != 4 )
{
if ( v5 == 11 )
goto LABEL_9;
v12 = strerror(v5);
v10 = -2;
log_debug("remote read error:%s", v12);
return v10;
}
}
if ( !v6 )
{
*(_DWORD *)(a1 + 67768) = 3;
*(_DWORD *)(a1 + 67808) = time(0LL);
(*(void (__fastcall **)(__int64, _QWORD, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL) + 40LL))(
a1,
0LL,
0LL);
log_debug("FIN is received from remote.");
return v7;
}
log_debug("%d data are received from remote.", (unsigned int)v6);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_write(v2, v7);
v8 = rwbuff_get_readp(v2);
v9 = rwbuff_get_readn(v2);
if ( (*(int (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 16LL) + 40LL))(
a1,
v8,
v9) >= 0 )
goto LABEL_9;
v10 = -3;
}
else
{
LABEL_9:
v10 = 0;
}
}
else
{
v10 = -1;
log_error("session can not be NULL!");
}
return v10;
}

original_code:
static int session_on_remote_recv(struct fctl_session_t* session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    char * buf = rwbuff_get_writep(&session->remote_buf);
    int space = rwbuff_get_writen(&session->remote_buf);
    log_debug("%d space left in remote buffuser.", space);;
    if ( space == 0) {
        return 0;
    }
    int n;
again:
    n = session->proxy->read(session->remote_fd, buf , space);
    if (n < 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11){
            log_debug("remote read error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 0;
    }else if ( n == 0){
        session->remote_status = SESSION_CLOSING;
        session->closing_time = time(0);
        session->proxy->proto->on_remote_rev(session , ((void *)0), 0);
        log_debug("FIN is received from remote.");;
        return 0;
    }
    log_debug("%d data are received from remote.", n);;
    session->closing_time = time(0);
    rwbuff_write(&session->remote_buf , n);
    buf = rwbuff_get_readp(&session->remote_buf);
    space = rwbuff_get_readn(&session->remote_buf);
    int action = session->proxy->proto->on_remote_rev(session , buf, space);
    if (action < 0)
        return -3;
    return 0;
}

canonical_original_code:
static int session_on_remote_recv(struct fctl_session_t *session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    char *buf = rwbuff_get_writep(&session->remote_buf);
    int space = rwbuff_get_writen(&session->remote_buf);
    log_debug("%d space left in remote buffuser.", space);;
    if ( space == 0) {
        return 0;
    }
    int n;
again:
    n = session->proxy->read(session->remote_fd, buf , space);
    if (n < 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11){
            log_debug("remote read error:%s",strerror((*__errno_location ())));;
            return -2;
        }
        return 0;
    }else if ( n == 0){
        session->remote_status = SESSION_CLOSING;
        session->closing_time = time(0);
        session->proxy->proto->on_remote_rev(session , ((void *)0), 0);
        log_debug("FIN is received from remote.");;
        return 0;
    }
    log_debug("%d data are received from remote.", n);;
    session->closing_time = time(0);
    rwbuff_write(&session->remote_buf , n);
    buf = rwbuff_get_readp(&session->remote_buf);
    space = rwbuff_get_readn(&session->remote_buf);
    int action = session->proxy->proto->on_remote_rev(session , buf, space);
    if (action < 0)
        return -3;
    return 0;
}

variable_types: action int


decompiled_code:
__int64 __fastcall session_on_local_recv(__int64 a1)
{
__int64 v2; // r14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
int v6; // eax
unsigned int v7; // er12
__int64 v8; // r13
__int64 v9; // r12
__int64 result; // rax
unsigned int v11; // er12
char *v12; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
v2 = a1 + 72;
v3 = rwbuff_get_writep(a1 + 72);
v4 = (unsigned int)rwbuff_get_writen(a1 + 72);
log_debug("%d space left in local buffer.", v4);
if ( (_DWORD)v4 )
{
do
{
v6 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 96LL))(
*(unsigned int *)(a1 + 24),
v3,
(unsigned int)v4);
v7 = v6;
if ( v6 >= 0 )
{
if ( v6 )
{
log_debug("%d data are received from local.", (unsigned int)v6);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_write(v2, v7);
v8 = rwbuff_get_readp(v2);
v9 = (unsigned int)rwbuff_get_readn(v2);
log_debug("there are %d data to be handled by protocal handler.", v9);
*(_BYTE *)(v8 + (int)v9) = 0;
result = (unsigned int)((*(int (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16)
+ 16LL)
+ 32LL))(
a1,
v8,
(unsigned int)v9) >> 31);
}
else
{
*(_DWORD *)(a1 + 67772) = 3;
*(_DWORD *)(a1 + 67808) = time(0LL);
log_debug("FIN is received from local.");
result = v7;
}
return result;
}
v5 = *_errno_location();
}
while ( v5 == 4 );
if ( v5 == 11 )
goto LABEL_10;
v12 = strerror(v5);
v11 = -2;
log_debug("read error:%s", v12);
}
else
{
LABEL_10:
v11 = 0;
}
}
else
{
v11 = -1;
log_error("session can not be NULL!");
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v2; // r14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
int v6; // eax
unsigned int v7; // er12
__int64 v8; // r13
__int64 v9; // r12
__int64 result; // rax
unsigned int v11; // er12
char *v12; // rax

if ( a1 && *(_QWORD *)(a1 + 16) )
{
v2 = a1 + 72;
v3 = rwbuff_get_writep(a1 + 72);
v4 = (unsigned int)rwbuff_get_writen(a1 + 72);
log_debug("%d space left in local buffer.", v4);
if ( (_DWORD)v4 )
{
do
{
v6 = (*(__int64 (__fastcall **)(_QWORD, __int64, _QWORD))(*(_QWORD *)(a1 + 16) + 96LL))(
*(unsigned int *)(a1 + 24),
v3,
(unsigned int)v4);
v7 = v6;
if ( v6 >= 0 )
{
if ( v6 )
{
log_debug("%d data are received from local.", (unsigned int)v6);
*(_DWORD *)(a1 + 67808) = time(0LL);
rwbuff_write(v2, v7);
v8 = rwbuff_get_readp(v2);
v9 = (unsigned int)rwbuff_get_readn(v2);
log_debug("there are %d data to be handled by protocal handler.", v9);
*(_BYTE *)(v8 + (int)v9) = 0;
result = (unsigned int)((*(int (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)(*(_QWORD *)(a1 + 16)
+ 16LL)
+ 32LL))(
a1,
v8,
(unsigned int)v9) >> 31);
}
else
{
*(_DWORD *)(a1 + 67772) = 3;
*(_DWORD *)(a1 + 67808) = time(0LL);
log_debug("FIN is received from local.");
result = v7;
}
return result;
}
v5 = *_errno_location();
}
while ( v5 == 4 );
if ( v5 == 11 )
goto LABEL_10;
v12 = strerror(v5);
v11 = -2;
log_debug("read error:%s", v12);
}
else
{
LABEL_10:
v11 = 0;
}
}
else
{
v11 = -1;
log_error("session can not be NULL!");
}
return v11;
}

original_code:
static int session_on_local_recv(struct fctl_session_t* session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    char * local_buf = rwbuff_get_writep(&session->local_buf);
    int space = rwbuff_get_writen(&session->local_buf);
    log_debug("%d space left in local buffer.", space);;
    if ( space == 0) {
        return 0;
    }
    int n;
 again:
    n = session->proxy->read(session->local_fd, local_buf , space);
    if (n < 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11) {
            log_debug("read error:%s",strerror((*__errno_location ())));;
            return -2;
        }else
            return 0;
    }else if ( n == 0){
        session->local_status = SESSION_CLOSING;
        session->closing_time = time(0);
        log_debug("FIN is received from local.");;
        return 0;
    }
    log_debug("%d data are received from local.", n);;
     session->closing_time = time(0);
    rwbuff_write(&session->local_buf , n);
    local_buf = rwbuff_get_readp(&session->local_buf);
    space = rwbuff_get_readn(&session->local_buf);
    log_debug("there are %d data to be handled by protocal handler.", space);;
    local_buf[space] = '\0';
    int action = session->proxy->proto->on_local_rev(session , local_buf, space);
    if ( action < 0 )
        return -1;
    return 0;
}

canonical_original_code:
static int session_on_local_recv(struct fctl_session_t *session){
    if (session == ((void *)0) || session->proxy == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    char *local_buf = rwbuff_get_writep(&session->local_buf);
    int space = rwbuff_get_writen(&session->local_buf);
    log_debug("%d space left in local buffer.", space);;
    if ( space == 0) {
        return 0;
    }
    int n;
 again:
    n = session->proxy->read(session->local_fd, local_buf , space);
    if (n < 0) {
        if ( (*__errno_location ()) == 4) goto again;
        if ( (*__errno_location ()) != 11) {
            log_debug("read error:%s",strerror((*__errno_location ())));;
            return -2;
        }else
            return 0;
    }else if ( n == 0){
        session->local_status = SESSION_CLOSING;
        session->closing_time = time(0);
        log_debug("FIN is received from local.");;
        return 0;
    }
    log_debug("%d data are received from local.", n);;
     session->closing_time = time(0);
    rwbuff_write(&session->local_buf , n);
    local_buf = rwbuff_get_readp(&session->local_buf);
    space = rwbuff_get_readn(&session->local_buf);
    log_debug("there are %d data to be handled by protocal handler.", space);;
    local_buf[space] = '\0';
    int action = session->proxy->proto->on_local_rev(session , local_buf, space);
    if ( action < 0 )
        return -1;
    return 0;
}


decompiled_code:
void __fastcall session_free(void *a1)
{
if ( a1 )
free(a1);
else
log_error("session can not be NULL!");
}

canonical_decompiled_code:
void __fastcall func8(void *a1)
{
if ( a1 )
free(a1);
else
log_error("session can not be NULL!");
}

original_code:
static int session_free(struct fctl_session_t* session){
     if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    free(session);
}

canonical_original_code:
static int session_free(struct fctl_session_t *session){
     if (session == ((void *)0)){
        log_error("session can not be NULL!");;
        return -1;
    }
    free(session);
}


decompiled_code:
__int64 __fastcall pc_add_proxy(int *a1, __int64 a2)
{
__int64 v2; // rax

v2 = *a1;
if ( (int)v2 > 8 )
return 0xFFFFFFFFLL;
*a1 = v2 + 1;
*(_QWORD *)&a1[2 * v2 + 2] = a2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, __int64 a2)
{
__int64 v2; // rax

v2 = *a1;
if ( (int)v2 > 8 )
return 0xFFFFFFFFLL;
*a1 = v2 + 1;
*(_QWORD *)&a1[2 * v2 + 2] = a2;
return 0LL;
}

original_code:
int pc_add_proxy(struct proxy_container_t* pc, struct fctl_proxy_t* proxy){
    if (pc->index >= (10 -1))
        return -1;
    pc->proxies[pc->index++] = proxy;
    return 0;
}

canonical_original_code:
int pc_add_proxy(struct proxy_container_t *pc, struct fctl_proxy_t *proxy){
    if (pc->index >= (10 -1))
        return -1;
    pc->proxies[pc->index++] = proxy;
    return 0;
}

variable_types: struct proxy_container_t { int index; struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 } proxy_type; int (*on_local_rev)(struct fctl_session_t { struct list_head { struct list_head *next; } list; struct fctl_proxy_t *proxy; int local_fd; int remote_fd; 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]; } local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t { int cache_count; struct list_head cache_head; } cache_pool; } local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t { int fd; char name[512]; int size; int exists; } spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); } *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t { int session_id; int event_type; int event_len; } *); } *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); } *proxies[10]; int others_count; struct other_process_t { int pid; int daemon; char filename[512]; char arg1[1024]; } others[64]; } *

variable_types:
proxy
struct fctl_proxy_t *

user_defined_types: struct proxy_container_t { int index; struct fctl_proxy_t *proxies[10]; int others_count; struct other_process_t others[64]; }


original_code:
int pc_run_all(struct proxy_container_t* pc){
    int i =0;
    for(i = 0 ; i < pc->others_count; i++ ){
       pid_t pid;
       if ((pid = fork()) < 0) {
           pid = 0;
       } else if (pid == 0) {
           int ret = 0;
           if (pc->others[i].arg1[0] == 0)
               ret = execl(pc->others[i].filename ,pc->others[i].filename, (char*)0);
           else
               ret = execl(pc->others[i].filename ,pc->others[i].filename, pc->others[i].arg1, (char*)0);
           log_error("execute %s error: %s\n", pc->others[i].filename, strerror((*__errno_location ())));;
           exit(0);
       }
       printf("%s started\n",pc->others[i].filename);
       sleep(1);
       pc->others[i].pid = pid;
    }
    for(i = 0 ; i < pc->index; i++ ){
        pc->proxies[i]->run( pc->proxies[i]);
    }
    return 0 ;
}

canonical_original_code:
int pc_run_all(struct proxy_container_t *pc){
    int i = 0;
    for(i = 0 ; i < pc->others_count; i++ ){
       int pid;
       if ((pid = fork()) < 0) {
           pid = 0;
       } else if (pid == 0) {
           int ret = 0;
           if (pc->others[i].arg1[0] == 0)
               ret = execl(pc->others[i].filename ,pc->others[i].filename, (char *)0);
           else
               ret = execl(pc->others[i].filename ,pc->others[i].filename, pc->others[i].arg1, (char *)0);
           log_error("execute %s error: %s\n", pc->others[i].filename, strerror((*__errno_location ())));;
           exit(0);
       }
       printf("%s started\n",pc->others[i].filename);
       sleep(1);
       pc->others[i].pid = pid;
    }
    for(i = 0 ; i < pc->index; i++ ){
        pc->proxies[i]->run( pc->proxies[i]);
    }
    return 0 ;
}

variable_types: i int

variable_types: pid int

variable_types: ret int

user_defined_types: struct other_process_t { int pid; int daemon; char filename[512]; char arg1[1024]; }


original_code:
int pc_kill_all(struct proxy_container_t* pc, int signo){
    int i =0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid >0)
        kill(pc->proxies[i]->pid , signo);
    }
    for(i = 0 ; i < pc->others_count; i++ ){
        if (pc->others[i].pid >0)
            kill(pc->others[i].pid , signo);
    }
    return 0 ;
}

canonical_original_code:
int pc_kill_all(struct proxy_container_t *pc, int signo){
    int i = 0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid >0)
        kill(pc->proxies[i]->pid , signo);
    }
    for(i = 0 ; i < pc->others_count; i++ ){
        if (pc->others[i].pid >0)
            kill(pc->others[i].pid , signo);
    }
    return 0 ;
}

variable_types: signo int


original_code:
int pc_restart_proxy(struct proxy_container_t* pc, pid_t proxy_pid){
   int i =0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid == proxy_pid){
            pc->proxies[i]->run( pc->proxies[i]);
            break;
        }
    }
    return 0 ;
}

canonical_original_code:
int pc_restart_proxy(struct proxy_container_t *pc, int proxy_pid){
   int i = 0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid == proxy_pid){
            pc->proxies[i]->run( pc->proxies[i]);
            break;
        }
    }
    return 0 ;
}


original_code:
int pc_remove_pid(struct proxy_container_t* pc, pid_t proxy_pid){
   int i =0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid == proxy_pid){
            pc->proxies[i]->pid = 0;
            break;
        }
    }
    for(i = 0 ; i < pc->others_count; i++ ){
        if (pc->others[i].pid == proxy_pid ){
                pc->others[i].pid = 0;
            break;
        }
    }
    return 0 ;
}

canonical_original_code:
int pc_remove_pid(struct proxy_container_t *pc, int proxy_pid){
   int i = 0;
    for(i = 0 ; i < pc->index; i++ ){
        if (pc->proxies[i]->pid == proxy_pid){
            pc->proxies[i]->pid = 0;
            break;
        }
    }
    for(i = 0 ; i < pc->others_count; i++ ){
        if (pc->others[i].pid == proxy_pid ){
                pc->others[i].pid = 0;
            break;
        }
    }
    return 0 ;
}


original_code:
int pc_add_others(struct proxy_container_t* pc, int daemon, char* filename, char* arg1){
    strncpy(pc->others[pc->others_count].filename, filename , 512 -1);
    pc->others[pc->others_count].daemon = daemon;
    if (arg1 != ((void *)0))
        strncpy(pc->others[pc->others_count].arg1, arg1 , sizeof(pc->others[pc->others_count].arg1)-1);
    pc->others_count++;
    return 0;
}

canonical_original_code:
int pc_add_others(struct proxy_container_t *pc, int daemon, char *filename, char *arg1){
    strncpy(pc->others[pc->others_count].filename, filename , 512 -1);
    pc->others[pc->others_count].daemon = daemon;
    if (arg1 != ((void *)0))
        strncpy(pc->others[pc->others_count].arg1, arg1 , sizeof(pc->others[pc->others_count].arg1)-1);
    pc->others_count++;
    return 0;
}

variable_types: daemon int

variable_types:
filename
char *


original_code:
int pc_stop_others(struct proxy_container_t* pc, char* filename ){
    int i = 0;
     for(i = 0 ; i < pc->others_count; i++ ){
        if ( strcmp(pc->others[i].filename , filename) == 0 ){
            if ( pc->others[i].pid > 0){
                kill(pc->others[i].pid , 2);
                pc->others[i].pid = 0;
            }
            break;
        }
    }
    return 0;
}

canonical_original_code:
int pc_stop_others(struct proxy_container_t *pc, char *filename){
    int i = 0;
     for(i = 0 ; i < pc->others_count; i++ ){
        if ( strcmp(pc->others[i].filename , filename) == 0 ){
            if ( pc->others[i].pid > 0){
                kill(pc->others[i].pid , 2);
                pc->others[i].pid = 0;
            }
            break;
        }
    }
    return 0;
}


decompiled_code:
__int64 __fastcall pc_restart_others(__int64 a1, int a2)
{
int v2; // edx
_DWORD *v3; // rax
int v4; // ebx
__pid_t v5; // eax
__int64 v6; // rax
const char *v7; // r12
int *v8; // rax
char *v9; // rax

v2 = *(_DWORD *)(a1 + 88);
if ( v2 <= 0 )
return 0LL;
v3 = (_DWORD *)(a1 + 92);
v4 = 0;
while ( *v3 != a2 )
{
++v4;
v3 += 386;
if ( v4 == v2 )
return 0LL;
}
v5 = fork();
if ( v5 < 0 )
{
v5 = 0;
}
else if ( !v5 )
{
v6 = 1544LL * v4;
v7 = (const char *)(a1 + v6 + 100);
if ( *(_BYTE *)(a1 + v6 + 612) )
execl((const char *)(a1 + v6 + 100), (const char *)(a1 + v6 + 100), a1 + v6 + 612, 0LL);
else
execl((const char *)(a1 + v6 + 100), (const char *)(a1 + v6 + 100), 0LL);
v8 = _errno_location();
v9 = strerror(*v8);
log_error("execute %s error: %s\n", v7, v9);
exit(0);
}
*(_DWORD *)(a1 + 1544LL * v4 + 92) = v5;
return 0LL;
}

original_code:
int pc_restart_others(struct proxy_container_t* pc, pid_t pid ){
    int i = 0;
    for(i = 0 ; i < pc->others_count; i++ ){
        if ( pc->others[i].pid == pid ){
            pid_t pid;
            if ((pid = fork()) < 0) {
                pid = 0;
            } else if (pid == 0) {
                int ret = 0;
                if (pc->others[i].arg1[0] == 0)
                    ret = execl(pc->others[i].filename ,pc->others[i].filename, (char*)0);
                else
                    ret = execl(pc->others[i].filename ,pc->others[i].filename, pc->others[i].arg1, (char*)0);
                log_error("execute %s error: %s\n", pc->others[i].filename, strerror((*__errno_location ())));;
                exit(0);
            }
            pc->others[i].pid = pid;
            break;
        }
    }
    return 0;
}

canonical_original_code:
int pc_restart_others(struct proxy_container_t *pc, int pid){
    int i = 0;
    for(i = 0 ; i < pc->others_count; i++ ){
        if ( pc->others[i].pid == pid ){
            int pid;
            if ((pid = fork()) < 0) {
                pid = 0;
            } else if (pid == 0) {
                int ret = 0;
                if (pc->others[i].arg1[0] == 0)
                    ret = execl(pc->others[i].filename ,pc->others[i].filename, (char *)0);
                else
                    ret = execl(pc->others[i].filename ,pc->others[i].filename, pc->others[i].arg1, (char *)0);
                log_error("execute %s error: %s\n", pc->others[i].filename, strerror((*__errno_location ())));;
                exit(0);
            }
            pc->others[i].pid = pid;
            break;
        }
    }
    return 0;
}


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

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

original_code:
int nat_disable_port(int src_port , int dst_port ){
    return (0);
}

canonical_original_code:
int nat_disable_port(int src_port, int dst_port){
    return (0);
}


decompiled_code:
// attributes: thunk
__int64 nat_enable_port()
{
return nat_disable_port();
}

canonical_decompiled_code:
__int64 func1()
{
return func0();
}

original_code:
int nat_enable_port(int src_port , int dst_port ){
    return (0);
}

canonical_original_code:
int nat_enable_port(int src_port, int dst_port){
    return (0);
}


decompiled_code:
int init_iptables_nat_rules()
{
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v1, 0x80uLL, "%s -t nat -F", (const char *)(unsigned int)((_DWORD)&g_conf + 1536));
system(v1);
snprintf(
v1,
0x80uLL,
"%s -t nat -A OUTPUT -m owner --uid-owner 0 -j ACCEPT",
(const char *)(unsigned int)((_DWORD)&g_conf + 1536));
return system(v1);
}

canonical_decompiled_code:
int func2()
{
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v1, 0x80uLL, "%s -t nat -F", (const char *)(unsigned int)((_DWORD)&g_conf + 1536));
system(v1);
snprintf(
v1,
0x80uLL,
"%s -t nat -A OUTPUT -m owner --uid-owner 0 -j ACCEPT",
(const char *)(unsigned int)((_DWORD)&g_conf + 1536));
return system(v1);
}

original_code:
void init_iptables_nat_rules(){
    char cmd[128];
    snprintf(cmd , sizeof(cmd), "%s -t nat -F", g_conf.iptables);
 system(cmd);
 snprintf(cmd , sizeof(cmd), "%s -t nat -A OUTPUT -m owner --uid-owner 0 -j ACCEPT", g_conf.iptables);
 system(cmd);
}

variable_types:
cmd
char [128]


decompiled_code:
int __fastcall create_iptables_nat_rule(unsigned int a1, unsigned int a2)
{
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(
v3,
0x80uLL,
"%s -t nat -A OUTPUT -p tcp --dport %d -j REDIRECT --to-ports %d",
(const char *)(unsigned int)((_DWORD)&g_conf + 1536),
a1,
a2);
puts(v3);
return system(v3);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int a1, unsigned int a2)
{
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(
v3,
0x80uLL,
"%s -t nat -A OUTPUT -p tcp --dport %d -j REDIRECT --to-ports %d",
(const char *)(unsigned int)((_DWORD)&g_conf + 1536),
a1,
a2);
puts(v3);
return system(v3);
}

original_code:
void create_iptables_nat_rule(int dest_port , int redirect_port){
    char cmd[128];
    snprintf(cmd , sizeof(cmd), "%s -t nat -A OUTPUT -p tcp --dport %d -j REDIRECT --to-ports %d" ,
         g_conf.iptables, dest_port, redirect_port);
    printf("%s\n", cmd);
    system(cmd);
}

canonical_original_code:
void create_iptables_nat_rule(int dest_port, int redirect_port){
    char cmd[128];
    snprintf(cmd , sizeof(cmd), "%s -t nat -A OUTPUT -p tcp --dport %d -j REDIRECT --to-ports %d" ,
         g_conf.iptables, dest_port, redirect_port);
    printf("%s\n", cmd);
    system(cmd);
}


decompiled_code:
int destroy_iptables_nat_rules()
{
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v1, 0x80uLL, "%s -t nat -F", (const char *)(unsigned int)((_DWORD)&g_conf + 1536));
return system(v1);
}

canonical_decompiled_code:
int func4()
{
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

snprintf(v1, 0x80uLL, "%s -t nat -F", (const char *)(unsigned int)((_DWORD)&g_conf + 1536));
return system(v1);
}

original_code:
void destroy_iptables_nat_rules(){
    char cmd[128];
    snprintf(cmd , sizeof(cmd), "%s -t nat -F", g_conf.iptables);
 system(cmd);
}


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

if ( a1 )
result = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 16));
else
result = 0LL;
return result;
}

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

if ( a1 )
result = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 16));
else
result = 0LL;
return result;
}

original_code:
unsigned int istream_datalen(input_stream* pstream)
{
    if (pstream == ((void *)0))
        return 0;
    return pstream->windex - pstream->rindex;
}

canonical_original_code:
unsigned int istream_datalen(struct input_stream_t *pstream)
{
    if (pstream == ((void *)0))
        return 0;
    return pstream->windex - pstream->rindex;
}

variable_types: struct input_stream_t { unsigned char *inbuffer; unsigned int total_len; unsigned int windex; unsigned int rindex; unsigned int avail_len; } *

return_type: unsigned int


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

if ( a1 )
result = *(_QWORD *)a1 + *(unsigned int *)(a1 + 16);
else
result = 0LL;
return result;
}

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

if ( a1 )
result = *(_QWORD *)a1 + *(unsigned int *)(a1 + 16);
else
result = 0LL;
return result;
}

original_code:
unsigned char* istream_buffer(input_stream* pstream)
{
    if (pstream == ((void *)0))
        return 0;
    return pstream->inbuffer + pstream->rindex;
}

canonical_original_code:
unsigned char *istream_buffer(struct input_stream_t *pstream)
{
    if (pstream == ((void *)0))
        return 0;
    return pstream->inbuffer + pstream->rindex;
}

return_type: unsigned char *

user_defined_types: struct input_stream_t { unsigned char *inbuffer; unsigned int total_len; unsigned int windex; unsigned int rindex; unsigned int avail_len; }


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

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

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

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

original_code:
int istream_init(input_stream* pstream)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    memset(pstream, 0, sizeof(*pstream));
    return ISTREAM_OK;
}

canonical_original_code:
int istream_init(struct input_stream_t *pstream)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    memset(pstream, 0, sizeof(*pstream));
    return ISTREAM_OK;
}


decompiled_code:
__int64 __fastcall istream_initx(__int64 a1, int a2)
{
void *v2; // rax
__int64 result; // rax

if ( !a1 )
return 2LL;
*(_OWORD *)(a1 + 8) = 0LL;
v2 = malloc(a2);
*(_QWORD *)a1 = v2;
if ( !v2 )
return 1LL;
*(_DWORD *)(a1 + 8) = a2;
result = 0LL;
*(_DWORD *)(a1 + 20) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
void *v2; // rax
__int64 result; // rax

if ( !a1 )
return 2LL;
*(_OWORD *)(a1 + 8) = 0LL;
v2 = malloc(a2);
*(_QWORD *)a1 = v2;
if ( !v2 )
return 1LL;
*(_DWORD *)(a1 + 8) = a2;
result = 0LL;
*(_DWORD *)(a1 + 20) = a2;
return result;
}

original_code:
int istream_initx(input_stream* pstream, int size)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    memset(pstream, 0, sizeof(*pstream));
    pstream->inbuffer = (unsigned char*)malloc(size);
    if (pstream->inbuffer == ((void *)0))
        return ISTREAM_ERROR_MEM;
    pstream->total_len = size;
    pstream->windex = 0;
    pstream->rindex = 0;
    pstream->avail_len = size;
    return ISTREAM_OK;
}

canonical_original_code:
int istream_initx(struct input_stream_t *pstream, int size)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    memset(pstream, 0, sizeof(*pstream));
    pstream->inbuffer = (unsigned char *)malloc(size);
    if (pstream->inbuffer == ((void *)0))
        return ISTREAM_ERROR_MEM;
    pstream->total_len = size;
    pstream->windex = 0;
    pstream->rindex = 0;
    pstream->avail_len = size;
    return ISTREAM_OK;
}

variable_types: size int


decompiled_code:
__int64 __fastcall istream_reset(__int64 a1)
{
int v1; // eax

if ( !a1 )
return 2LL;
v1 = *(_DWORD *)(a1 + 8);
*(_QWORD *)(a1 + 12) = 0LL;
*(_DWORD *)(a1 + 20) = v1;
return 0LL;
}

original_code:
int istream_reset(input_stream* pstream)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    pstream->rindex = 0;
    pstream->windex = 0;
    pstream->avail_len = pstream->total_len;
    return ISTREAM_OK;
}

canonical_original_code:
int istream_reset(struct input_stream_t *pstream)
{
    if (pstream == ((void *)0))
        return ISTREAM_ERROR_PARAM;
    pstream->rindex = 0;
    pstream->windex = 0;
    pstream->avail_len = pstream->total_len;
    return ISTREAM_OK;
}


decompiled_code:
__int64 __fastcall istream_destroy(void **a1)
{
void *v1; // rdi

if ( !a1 )
return 0LL;
v1 = *a1;
if ( !v1 )
return 0LL;
free(v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(void **a1)
{
void *v1; // rdi

if ( !a1 )
return 0LL;
v1 = *a1;
if ( !v1 )
return 0LL;
free(v1);
return 0LL;
}

original_code:
int istream_destroy(input_stream* pstream)
{
    if (pstream != ((void *)0) && pstream->inbuffer != ((void *)0))
        free(pstream->inbuffer);
    return ISTREAM_OK;
}

canonical_original_code:
int istream_destroy(struct input_stream_t *pstream)
{
    if (pstream != ((void *)0) && pstream->inbuffer != ((void *)0))
        free(pstream->inbuffer);
    return ISTREAM_OK;
}


original_code:
int istream_write(input_stream* pstream, unsigned char* buff, unsigned int len)
{
    if (pstream == ((void *)0) || buff == ((void *)0) || len <=0)
        return ISTREAM_ERROR_PARAM;
    if (pstream->inbuffer == ((void *)0))
    {
        pstream->inbuffer = (unsigned char*)malloc(len);
        if (pstream->inbuffer == ((void *)0))
            return ISTREAM_ERROR_MEM;
        pstream->total_len = len;
        pstream->windex = 0;
        pstream->rindex = 0;
        pstream->avail_len = len;
    }
    else
    {
        if (pstream->rindex != 0)
        {
            unsigned int datalen = (unsigned int)(pstream->windex - pstream->rindex);
            memcpy(pstream->inbuffer, pstream->inbuffer + pstream->rindex, datalen);
            pstream->rindex = 0;
            pstream->windex = pstream->rindex + datalen;
            pstream->avail_len = pstream->total_len - datalen;
        }
        if (pstream->avail_len < len)
        {
            unsigned int inc_size = (len > 256) ? len : 256;
            pstream->inbuffer = (unsigned char*)realloc(pstream->inbuffer, pstream->total_len + inc_size);
            if (pstream->inbuffer == ((void *)0))
                return ISTREAM_ERROR_MEM;
            pstream->total_len = pstream->total_len + inc_size;
        }
    }
    memcpy(pstream->inbuffer + pstream->windex , buff, len);
    pstream->windex += len;
    pstream->avail_len = pstream->total_len - pstream->windex ;
    return ISTREAM_OK;
}

canonical_original_code:
int istream_write(struct input_stream_t *pstream, unsigned char *buff, unsigned int len)
{
    if (pstream == ((void *)0) || buff == ((void *)0) || len <=0)
        return ISTREAM_ERROR_PARAM;
    if (pstream->inbuffer == ((void *)0))
    {
        pstream->inbuffer = (unsigned char *)malloc(len);
        if (pstream->inbuffer == ((void *)0))
            return ISTREAM_ERROR_MEM;
        pstream->total_len = len;
        pstream->windex = 0;
        pstream->rindex = 0;
        pstream->avail_len = len;
    }
    else
    {
        if (pstream->rindex != 0)
        {
            unsigned int datalen = (unsigned int)(pstream->windex - pstream->rindex);
            memcpy(pstream->inbuffer, pstream->inbuffer + pstream->rindex, datalen);
            pstream->rindex = 0;
            pstream->windex = pstream->rindex + datalen;
            pstream->avail_len = pstream->total_len - datalen;
        }
        if (pstream->avail_len < len)
        {
            unsigned int inc_size = (len > 256) ? len : 256;
            pstream->inbuffer = (unsigned char *)realloc(pstream->inbuffer, pstream->total_len + inc_size);
            if (pstream->inbuffer == ((void *)0))
                return ISTREAM_ERROR_MEM;
            pstream->total_len = pstream->total_len + inc_size;
        }
    }
    memcpy(pstream->inbuffer + pstream->windex , buff, len);
    pstream->windex += len;
    pstream->avail_len = pstream->total_len - pstream->windex ;
    return ISTREAM_OK;
}

variable_types:
buff
unsigned char *

variable_types:
len
unsigned int

variable_types:
datalen
unsigned int


decompiled_code:
__int64 __fastcall istream_read(__int64 a1, void *a2, unsigned int a3)
{
__int64 v5; // rsi
unsigned int v6; // er12
__int64 result; // rax

v5 = *(unsigned int *)(a1 + 16);
v6 = *(_DWORD *)(a1 + 12) - v5;
if ( v6 > a3 )
v6 = a3;
if ( a2 && v6 )
{
memcpy(a2, (const void *)(*(_QWORD *)a1 + v5), (int)v6);
LODWORD(v5) = *(_DWORD *)(a1 + 16);
}
result = v6;
*(_DWORD *)(a1 + 16) = v6 + v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, void *a2, unsigned int a3)
{
__int64 v5; // rsi
unsigned int v6; // er12
__int64 result; // rax

v5 = *(unsigned int *)(a1 + 16);
v6 = *(_DWORD *)(a1 + 12) - v5;
if ( v6 > a3 )
v6 = a3;
if ( a2 && v6 )
{
memcpy(a2, (const void *)(*(_QWORD *)a1 + v5), (int)v6);
LODWORD(v5) = *(_DWORD *)(a1 + 16);
}
result = v6;
*(_DWORD *)(a1 + 16) = v6 + v5;
return result;
}

original_code:
int istream_read(input_stream* pstream, unsigned char* buff, unsigned int maxlen)
{
    int readlen = (maxlen <= (pstream->windex - pstream->rindex))?maxlen : (pstream->windex - pstream->rindex);
    if (buff != ((void *)0) && readlen != 0)
    {
        memcpy(buff, pstream->inbuffer + pstream->rindex, readlen);
    }
    pstream->rindex += readlen;
    return readlen;
}

canonical_original_code:
int istream_read(struct input_stream_t *pstream, unsigned char *buff, unsigned int maxlen)
{
    int readlen = (maxlen <= (pstream->windex - pstream->rindex))?maxlen : (pstream->windex - pstream->rindex);
    if (buff != ((void *)0) && readlen != 0)
    {
        memcpy(buff, pstream->inbuffer + pstream->rindex, readlen);
    }
    pstream->rindex += readlen;
    return readlen;
}

variable_types:
maxlen
unsigned int

variable_types: readlen int


decompiled_code:
const char *__fastcall istream_strerror(__int64 a1, int a2)
{
const char *result; // rax

result = "Memory error";
if ( a2 != 1 )
{
result = "Unknown error";
if ( a2 == 2 )
result = "Param error";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(__int64 a1, int a2)
{
const char *result; // rax

result = "Memory error";
if ( a2 != 1 )
{
result = "Unknown error";
if ( a2 == 2 )
result = "Param error";
}
return result;
}

original_code:
char* istream_strerror(input_stream* pstream, int code)
{
    switch (code)
    {
        case ISTREAM_ERROR_MEM:
            return "Memory error";
        case ISTREAM_ERROR_PARAM:
            return "Param error";
        default:
            return "Unknown error";
    }
    return "";
}

canonical_original_code:
char *istream_strerror(struct input_stream_t *pstream, int code)
{
    switch (code)
    {
        case ISTREAM_ERROR_MEM:
            return "Memory error";
        case ISTREAM_ERROR_PARAM:
            return "Param error";
        default:
            return "Unknown error";
    }
    return "";
}

variable_types: code int

return_type: char *


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

v1 = a1[6];
if ( v1 == a1[8] )
v1 = a1[7];
result = (unsigned int)(v1 - 1);
a1[6] = result;
return result;
}

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

v1 = a1[6];
if ( v1 == a1[8] )
v1 = a1[7];
result = (unsigned int)(v1 - 1);
a1[6] = result;
return result;
}

original_code:
int proxy_get_valid_port(struct fctl_proxy_t* proxy){
    int port = 0;
    if (proxy->valid_port == proxy->min_bind_port)
  proxy->valid_port = proxy->max_bind_port;
    proxy->valid_port--;
    return proxy->valid_port;
}

canonical_original_code:
int proxy_get_valid_port(struct fctl_proxy_t *proxy){
    int port = 0;
    if (proxy->valid_port == proxy->min_bind_port)
  proxy->valid_port = proxy->max_bind_port;
    proxy->valid_port--;
    return proxy->valid_port;
}

variable_types: struct fctl_proxy_t { int daemon; int port; int pid; struct fctl_proto_t { char proto_name[20]; int port; enum PROXY_TYPE { PROXY_SYN=0, PROXY_ASY=1 } proxy_type; int (*on_local_rev)(struct fctl_session_t { struct list_head { struct list_head *next; } list; struct fctl_proxy_t *proxy; int local_fd; int remote_fd; 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]; } local_addr; int remote_src_port; int remote_dst_port; struct sockaddr_in remote_addr; struct rwbuff_t { char buf[-1]; char *readp; char *writep; char *sendp; int buf_len; int buf_default_len; struct cache_pool_t { int cache_count; struct list_head cache_head; } cache_pool; } local_buf; struct rwbuff_t remote_buf; int remote_status; int local_status; int proto_status; int local_retry_times; int remote_retry_times; void *private_data; long create_time; int closing_time; struct fctl_spool_t { int fd; char name[512]; int size; int exists; } spool; int session_id; int (*on_local_recv)(struct fctl_session_t *); int (*on_remote_recv)(struct fctl_session_t *); int (*on_local_send)(struct fctl_session_t *); int (*on_remote_send)(struct fctl_session_t *); int (*on_local_close)(struct fctl_session_t *); int (*on_local_connect)(struct fctl_session_t *); int (*on_remote_close)(struct fctl_session_t *); int (*on_remote_connect)(struct fctl_session_t *); int (*is_closed)(struct fctl_session_t *); int (*close)(struct fctl_session_t *); int (*free)(struct fctl_session_t *); } *, char *, int); int (*on_remote_rev)(struct fctl_session_t *, char *, int); int (*on_connect)(struct fctl_session_t *); int (*on_disconnect)(struct fctl_session_t *); int (*on_notification)(struct fctl_session_t *, struct event_header_t { int session_id; int event_type; int event_len; } *); } *proto; int valid_port; int max_bind_port; int min_bind_port; char proto_name[20]; unsigned int cur_session_id; struct list_head session_head; int (*run)(struct fctl_proxy_t *); int (*get_port)(struct fctl_proxy_t *); int (*read)(int, char *, int); int (*write)(int, char *, int); int (*listen)(struct fctl_proxy_t *); int (*local_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_send)(struct fctl_proxy_t *, struct fctl_session_t *); int (*local_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_close)(struct fctl_proxy_t *, struct fctl_session_t *); int (*remote_connect)(struct fctl_proxy_t *, struct fctl_session_t *, char *, int); } *

variable_types: port int


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

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

original_code:
static int proxy_write(int fd, char* buf , int buflen){
    return write(fd , buf , buflen);
}

canonical_original_code:
static int proxy_write(int fd, char *buf, int buflen){
    return write(fd , buf , buflen);
}

variable_types: fd int

variable_types:
buf
char *

variable_types: buflen int


decompiled_code:
ssize_t __fastcall proxy_read(int a1, void *a2, int a3)
{
return read(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func3(int a1, void *a2, int a3)
{
return read(a1, a2, a3);
}

original_code:
static int proxy_read(int fd, char* buf , int buflen){
    return read(fd , buf , buflen);
}

canonical_original_code:
static int proxy_read(int fd, char *buf, int buflen){
    return read(fd , buf , buflen);
}


decompiled_code:
__int64 __fastcall proxy_listen(__int64 a1)
{
int v2; // eax
__int64 v3; // rdi
unsigned int v4; // er12
int v5; // eax
socklen_t v6; // edx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v15[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
v8 = _errno_location();
v4 = -1;
v9 = strerror(*v8);
log_error("%s proxy socket error: %s", v9);
}
else
{
v3 = (unsigned int)v2;
v4 = v2;
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
*(_QWORD *)&v15[0].sa_data[6] = 0LL;
v15[0].sa_family = 2;
v5 = _bswap_32_constprop_0(v3, v15, 16LL);
LOWORD(v3) = *(_WORD *)(a1 + 4);
*(_DWORD *)&v15[0].sa_data[2] = v5;
*(_WORD *)v15[0].sa_data = _bswap_16(v3);
if ( bind(v4, v15, v6) < 0 )
{
v12 = _errno_location();
v4 = -2;
v13 = strerror(*v12);
log_error("%s proxy bind error: %s", v13);
}
else if ( listen(v4, 128) < 0 )
{
v10 = _errno_location();
v4 = -3;
v11 = strerror(*v10);
log_error("%s proxy listen error: %s", v11);
}
else
{
log_info("%s proxy start and listen on port %d", *(const char **)(a1 + 16), *(unsigned int *)(a1 + 4));
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v2; // eax
__int64 v3; // rdi
unsigned int v4; // er12
int v5; // eax
socklen_t v6; // edx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v15[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
v8 = _errno_location();
v4 = -1;
v9 = strerror(*v8);
log_error("%s proxy socket error: %s", v9);
}
else
{
v3 = (unsigned int)v2;
v4 = v2;
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
*(_QWORD *)&v15[0].sa_data[6] = 0LL;
v15[0].sa_family = 2;
v5 = func4(v3, v15, 16LL);
LOWORD(v3) = *(_WORD *)(a1 + 4);
*(_DWORD *)&v15[0].sa_data[2] = v5;
*(_WORD *)v15[0].sa_data = func1(v3);
if ( bind(v4, v15, v6) < 0 )
{
v12 = _errno_location();
v4 = -2;
v13 = strerror(*v12);
log_error("%s proxy bind error: %s", v13);
}
else if ( listen(v4, 128) < 0 )
{
v10 = _errno_location();
v4 = -3;
v11 = strerror(*v10);
log_error("%s proxy listen error: %s", v11);
}
else
{
log_info("%s proxy start and listen on port %d", *(const char **)(a1 + 16), *(unsigned int *)(a1 + 4));
}
}
return v4;
}

original_code:
static int proxy_listen(struct fctl_proxy_t* proxy){
    int listenfd = 0;
    struct sockaddr_in servaddr;
    if((listenfd=socket(2,SOCK_STREAM,0))<0){
        log_error("%s proxy socket error: %s", strerror((*__errno_location ())));;
        return( -1 );
    }
    int i = 1;
    setsockopt(listenfd, 1, 2, &i, sizeof(i));
    memset(&servaddr,0,sizeof(struct sockaddr_in));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr = htonl (((in_addr_t) 0x00000000));
    servaddr.sin_port = htons (proxy->port);
    if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr))<0){
     log_error("%s proxy bind error: %s", strerror((*__errno_location ())));;
     return(-2);
    }
    if (listen(listenfd, 128) <0 ){
     log_error("%s proxy listen error: %s", strerror((*__errno_location ())));;
     return(-3);
    }
    log_info("%s proxy start and listen on port %d", proxy->proto->proto_name, proxy->port);;
    return listenfd;
}

canonical_original_code:
static int proxy_listen(struct fctl_proxy_t *proxy){
    int listenfd = 0;
    struct sockaddr_in servaddr;
    if((listenfd=socket(2,SOCK_STREAM,0))<0){
        log_error("%s proxy socket error: %s", strerror((*__errno_location ())));;
        return( -1 );
    }
    int i = 1;
    setsockopt(listenfd, 1, 2, &i, sizeof(i));
    memset(&servaddr,0,sizeof(struct sockaddr_in));
    servaddr.sin_family = 2;
    servaddr.sin_addr.s_addr = htonl (((unsigned int) 0x00000000));
    servaddr.sin_port = htons (proxy->port);
    if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))<0){
     log_error("%s proxy bind error: %s", strerror((*__errno_location ())));;
     return(-2);
    }
    if (listen(listenfd, 128) <0 ){
     log_error("%s proxy listen error: %s", strerror((*__errno_location ())));;
     return(-3);
    }
    log_info("%s proxy start and listen on port %d", proxy->proto->proto_name, proxy->port);;
    return listenfd;
}

variable_types: listenfd int

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

variable_types: i int

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


repo: dzo/patchkernel

decompiled_code:
int __fastcall update_table(_DWORD *a1)
{
_DWORD *v1; // rbx
int result; // eax
int v3; // edx
int v4; // eax

v1 = a1;
result = printf("update table %p\n", a1);
if ( a1[1] )
{
v3 = l2;
do
{
v4 = v1[6];
v1 += 8;
*(v1 - 2) = v3 + 32 * v4;
result = v1[1];
}
while ( result );
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(_DWORD *a1)
{
_DWORD *v1; // rbx
int result; // eax
int v3; // edx
int v4; // eax

v1 = a1;
result = printf("update table %p\n", a1);
if ( a1[1] )
{
v3 = l2;
do
{
v4 = v1[6];
v1 += 8;
*(v1 - 2) = v3 + 32 * v4;
result = v1[1];
}
while ( result );
}
return result;
}

original_code:
void update_table(struct acpu_level *t) {
 printf("update table %p\n",t);
 while(t->speed.khz) {
  t->l2_level=l2+sizeof(struct l2_level)*t->l2_level;
  t++;
 }
}

variable_types: struct acpu_level { unsigned int use_for_scaling; struct core_speed { unsigned int khz; int src; unsigned int pri_src_sel; unsigned int sec_src_sel; unsigned int pll_l_val; } speed; int l2_level; unsigned int vdd_core; } *

user_defined_types: struct l2_level { struct core_speed speed; unsigned int vdd_dig; unsigned int vdd_mem; unsigned int bw_level; }

user_defined_types: struct acpu_level { unsigned int use_for_scaling; struct core_speed speed; int l2_level; unsigned int vdd_core; }


repo: dzolo/lpm

original_code:
void trie_node_put(trie_t *t, unsigned char *ip, unsigned char cidr, int as)
{
    node_t *current = t->root;
    int i;
    for (i = 0; i < cidr; i++)
    {
        if (((ip[i / 8] >> (7 - i % 8)) & 1) > 0)
        {
            if (current->r == ((void *)0))
            {
                current->r = _node_malloc(t);
            }
            current = current->r;
        }
        else
        {
            if (current->l == ((void *)0))
            {
                current->l = _node_malloc(t);
            }
            current = current->l;
        }
        if ((i + 1) == cidr)
        {
            if (!current->as_in)
            {
                current->as_in = 1;
                current->as = as;
            }
            else if (current->as != as)
            {
                fprintf(stderr, "Some network collides: %d and %d", current->as, as);
            }
        }
    }
}

canonical_original_code:
void trie_node_put(struct _trie_t *t, unsigned char *ip, unsigned char cidr, int as)
{
    struct _node_t *current = t->root;
    int i;
    for (i = 0; i < cidr; i++)
    {
        if (((ip[i / 8] >> (7 - i % 8)) & 1) > 0)
        {
            if (current->r == ((void *)0))
            {
                current->r = _node_malloc(t);
            }
            current = current->r;
        }
        else
        {
            if (current->l == ((void *)0))
            {
                current->l = _node_malloc(t);
            }
            current = current->l;
        }
        if ((i + 1) == cidr)
        {
            if (!current->as_in)
            {
                current->as_in = 1;
                current->as = as;
            }
            else if (current->as != as)
            {
                fprintf(stderr, "Some network collides: %d and %d", current->as, as);
            }
        }
    }
}

variable_types: struct _trie_t { struct _node_t { struct _node_t *l; struct _node_t *r; int as; unsigned char as_in; } *root; struct _trie_memory_t { int ln; struct _trie_memory_t *next; struct _node_t cells[32768]; } *first; struct _trie_memory_t *last; } *

variable_types:
ip
unsigned char *

variable_types:
cidr
unsigned char

variable_types: as int

user_defined_types: struct _trie_t { struct _node_t *root; struct _trie_memory_t *first; struct _trie_memory_t *last; }


original_code:
unsigned char trie_node_search(trie_t *t, unsigned char *ip, unsigned char ln, int *as)
{
    node_t *current = t->root;
    unsigned char found = 0;
    int i, u;
    for (i = 0; i < ln; i++)
    {
        for (u = 7; u >= 0; u--)
        {
            if (((ip[i] >> u) & 1) > 0)
            {
                current = current->r;
            }
            else
            {
                current = current->l;
            }
            if (current == ((void *)0))
            {
                return found;
            }
            if (current->as_in)
            {
                *as = current->as;
                found = 1;
            }
        }
    }
    return found;
}

canonical_original_code:
unsigned char trie_node_search(struct _trie_t *t, unsigned char *ip, unsigned char ln, int *as)
{
    struct _node_t *current = t->root;
    unsigned char found = 0;
    int i; int u;
    for (i = 0; i < ln; i++)
    {
        for (u = 7; u >= 0; u--)
        {
            if (((ip[i] >> u) & 1) > 0)
            {
                current = current->r;
            }
            else
            {
                current = current->l;
            }
            if (current == ((void *)0))
            {
                return found;
            }
            if (current->as_in)
            {
                *as = current->as;
                found = 1;
            }
        }
    }
    return found;
}

variable_types:
ln
unsigned char

variable_types:
as
int *

variable_types: struct _node_t { struct _node_t *l; struct _node_t *r; int as; unsigned char as_in; } *

variable_types:
found
unsigned char

variable_types: u int

return_type: unsigned char

user_defined_types: struct _trie_memory_t { int ln; struct _trie_memory_t *next; struct _node_t cells[32768]; }


fatal

repo: dzonder/yaptcc

decompiled_code:
void __fastcall __noreturn fatal(const char *a1)
{
fprintf(stderr, "Fatal: %s\n", a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
fprintf(stderr, "Fatal: %s\n", a1);
exit(1);
}

original_code:
void fatal(const char *msg)
{
   fprintf(stderr, "Fatal: %s\n", msg);
   exit(1);
}

canonical_original_code:
void fatal(char *msg)
{
   fprintf(stderr, "Fatal: %s\n", msg);
   exit(1);
}

variable_types:
msg
char *


decompiled_code:
void __noreturn curl_fatal()
{
const char *v0; // rax

v0 = (const char *)curl_easy_strerror();
fprintf(stderr, "CURL error: %s\n", v0);
exit(1);
}

canonical_decompiled_code:
void __noreturn func8()
{
const char *v0; // rax

v0 = (const char *)curl_easy_strerror();
fprintf(stderr, "CURL error: %s\n", v0);
exit(1);
}

original_code:
void curl_fatal(CURLcode err)
{
   fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(err));
   exit(1);
}

canonical_original_code:
void curl_fatal(enum CURLcode err)
{
   fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(err));
   exit(1);
}

variable_types: enum CURLcode { CURLE_OK=0, CURLE_UNSUPPORTED_PROTOCOL=1, CURLE_FAILED_INIT=2, CURLE_URL_MALFORMAT=3, CURLE_NOT_BUILT_IN=4, CURLE_COULDNT_RESOLVE_PROXY=5, CURLE_COULDNT_RESOLVE_HOST=6, CURLE_COULDNT_CONNECT=7, CURLE_WEIRD_SERVER_REPLY=8, CURLE_REMOTE_ACCESS_DENIED=9, CURLE_FTP_ACCEPT_FAILED=10, CURLE_FTP_WEIRD_PASS_REPLY=11, CURLE_FTP_ACCEPT_TIMEOUT=12, CURLE_FTP_WEIRD_PASV_REPLY=13, CURLE_FTP_WEIRD_227_FORMAT=14, CURLE_FTP_CANT_GET_HOST=15, CURLE_HTTP2=16, CURLE_FTP_COULDNT_SET_TYPE=17, CURLE_PARTIAL_FILE=18, CURLE_FTP_COULDNT_RETR_FILE=19, CURLE_OBSOLETE20=20, CURLE_QUOTE_ERROR=21, CURLE_HTTP_RETURNED_ERROR=22, CURLE_WRITE_ERROR=23, CURLE_OBSOLETE24=24, CURLE_UPLOAD_FAILED=25, CURLE_READ_ERROR=26, CURLE_OUT_OF_MEMORY=27, CURLE_OPERATION_TIMEDOUT=28, CURLE_OBSOLETE29=29, CURLE_FTP_PORT_FAILED=30, CURLE_FTP_COULDNT_USE_REST=31, CURLE_OBSOLETE32=32, CURLE_RANGE_ERROR=33, CURLE_HTTP_POST_ERROR=34, CURLE_SSL_CONNECT_ERROR=35, CURLE_BAD_DOWNLOAD_RESUME=36, CURLE_FILE_COULDNT_READ_FILE=37, CURLE_LDAP_CANNOT_BIND=38, CURLE_LDAP_SEARCH_FAILED=39, CURLE_OBSOLETE40=40, CURLE_FUNCTION_NOT_FOUND=41, CURLE_ABORTED_BY_CALLBACK=42, CURLE_BAD_FUNCTION_ARGUMENT=43, CURLE_OBSOLETE44=44, CURLE_INTERFACE_FAILED=45, CURLE_OBSOLETE46=46, CURLE_TOO_MANY_REDIRECTS=47, CURLE_UNKNOWN_OPTION=48, CURLE_TELNET_OPTION_SYNTAX=49, CURLE_OBSOLETE50=50, CURLE_OBSOLETE51=51, CURLE_GOT_NOTHING=52, CURLE_SSL_ENGINE_NOTFOUND=53, CURLE_SSL_ENGINE_SETFAILED=54, CURLE_SEND_ERROR=55, CURLE_RECV_ERROR=56, CURLE_OBSOLETE57=57, CURLE_SSL_CERTPROBLEM=58, CURLE_SSL_CIPHER=59, CURLE_PEER_FAILED_VERIFICATION=60, CURLE_BAD_CONTENT_ENCODING=61, CURLE_LDAP_INVALID_URL=62, CURLE_FILESIZE_EXCEEDED=63, CURLE_USE_SSL_FAILED=64, CURLE_SEND_FAIL_REWIND=65, CURLE_SSL_ENGINE_INITFAILED=66, CURLE_LOGIN_DENIED=67, CURLE_TFTP_NOTFOUND=68, CURLE_TFTP_PERM=69, CURLE_REMOTE_DISK_FULL=70, CURLE_TFTP_ILLEGAL=71, CURLE_TFTP_UNKNOWNID=72, CURLE_REMOTE_FILE_EXISTS=73, CURLE_TFTP_NOSUCHUSER=74, CURLE_CONV_FAILED=75, CURLE_CONV_REQD=76, CURLE_SSL_CACERT_BADFILE=77, CURLE_REMOTE_FILE_NOT_FOUND=78, CURLE_SSH=79, CURLE_SSL_SHUTDOWN_FAILED=80, CURLE_AGAIN=81, CURLE_SSL_CRL_BADFILE=82, CURLE_SSL_ISSUER_ERROR=83, CURLE_FTP_PRET_FAILED=84, CURLE_RTSP_CSEQ_ERROR=85, CURLE_RTSP_SESSION_ERROR=86, CURLE_FTP_BAD_FILE_LIST=87, CURLE_CHUNK_FAILED=88, CURLE_NO_CONNECTION_AVAILABLE=89, CURLE_SSL_PINNEDPUBKEYNOTMATCH=90, CURLE_SSL_INVALIDCERTSTATUS=91, CURLE_HTTP2_STREAM=92, CURLE_RECURSIVE_API_CALL=93, CURL_LAST=94 }


decompiled_code:
__int64 __fastcall read_paste(void **a1)
{
__int64 v1; // r12
size_t i; // rbx
char *v3; // rax
FILE *v4; // rcx

v1 = 0LL;
for ( i = 1024LL; ; i += 1024LL )
{
v3 = (char *)realloc(*a1, i);
v4 = stdin;
*a1 = v3;
v1 += fread(&v3[v1], 1uLL, 0x400uLL, v4);
if ( feof(stdin) )
break;
if ( ferror(stdin) )
fatal("fread error");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(void **a1)
{
__int64 v1; // r12
size_t i; // rbx
char *v3; // rax
FILE *v4; // rcx

v1 = 0LL;
for ( i = 1024LL; ; i += 1024LL )
{
v3 = (char *)realloc(*a1, i);
v4 = stdin;
*a1 = v3;
v1 += fread(&v3[v1], 1uLL, 0x400uLL, v4);
if ( feof(stdin) )
break;
if ( ferror(stdin) )
func7("fread error");
}
return v1;
}

original_code:
size_t read_paste(char **buf)
{
   size_t count = 0, size = 0;
   while (1) {
      *buf = (char*)realloc(*buf, sizeof(char)*1024U*(++count));
      size += fread(*buf+size, 1, sizeof(char)*1024U, stdin);
      if (feof(stdin))
         return size;
      if (ferror(stdin))
         fatal("fread error");
   }
}

canonical_original_code:
unsigned long read_paste(char **buf)
{
   unsigned long count = 0; unsigned long size = 0;
   while (1) {
      *buf = (char *)realloc(*buf, sizeof(char)*1024U*(++count));
      size += fread(*buf+size, 1, sizeof(char)*1024U, stdin);
      if (feof(stdin))
         return size;
      if (ferror(stdin))
         fatal("fread error");
   }
}

variable_types:
buf
char **

variable_types:
count
unsigned long

variable_types:
size
unsigned long

return_type: unsigned long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 *v3; // rax
const char *v4; // rdi
int v5; // er12
unsigned __int8 *v6; // rax
void *v7; // rdi

if ( argc != 2 )
{
printf("Usage: %s <source file>\n", *argv);
exit(1);
}
v3 = instr;
do
*v3++ = (__int64)nop;
while ( v3 != &ins );
v4 = argv[1];
qword_404218 = (__int64)inc;
qword_404228 = (__int64)dec;
qword_4042B0 = (__int64)rht;
qword_4042A0 = (__int64)lft;
qword_404230 = (__int64)out;
qword_404220 = (__int64)inp;
qword_404398 = (__int64)lbr;
qword_4043A8 = (__int64)rbr;
v5 = load_file(v4, &src, &src_len);
if ( v5 )
{
v5 = 1;
printf("Error reading %s\n", argv[1]);
}
else
{
v6 = (unsigned __int8 *)src;
ins = (__int64)src;
if ( src < (char *)src + (unsigned int)src_len )
{
do
{
((void (*)(void))instr[*v6])();
if ( ptr < &mem )
{
ptr = &mem;
}
else if ( ptr >= &end )
{
ptr = &unk_40BE0F;
}
v7 = src;
v6 = (unsigned __int8 *)(ins + 1);
ins = (__int64)v6;
}
while ( v6 < (unsigned __int8 *)((char *)src + (unsigned int)src_len) );
}
else
{
v7 = src;
}
free(v7);
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 *v3; // rax
const char *v4; // rdi
int v5; // er12
unsigned __int8 *v6; // rax
void *v7; // rdi

if ( argc != 2 )
{
printf("Usage: %s <source file>\n", *argv);
exit(1);
}
v3 = instr;
do
*v3++ = (__int64)nop;
while ( v3 != &ins );
v4 = argv[1];
qword_404218 = (__int64)inc;
qword_404228 = (__int64)dec;
qword_4042B0 = (__int64)rht;
qword_4042A0 = (__int64)lft;
qword_404230 = (__int64)out;
qword_404220 = (__int64)inp;
qword_404398 = (__int64)lbr;
qword_4043A8 = (__int64)rbr;
v5 = func16(v4, &src, &src_len);
if ( v5 )
{
v5 = 1;
printf("Error reading %s\n", argv[1]);
}
else
{
v6 = (unsigned __int8 *)src;
ins = (__int64)src;
if ( src < (char *)src + (unsigned int)src_len )
{
do
{
((void (*)(void))instr[*v6])();
if ( ptr < &mem )
{
ptr = &mem;
}
else if ( ptr >= &end )
{
ptr = &unk_40BE0F;
}
v7 = src;
v6 = (unsigned __int8 *)(ins + 1);
ins = (__int64)v6;
}
while ( v6 < (unsigned __int8 *)((char *)src + (unsigned int)src_len) );
}
else
{
v7 = src;
}
free(v7);
}
return v5;
}

original_code:
int main(int argc, unsigned char** argv) {
    if(argc != 2) {
        printf("Usage: %s <source file>\n", argv[0]);
        exit(1);
    }
    memset(mem, 30000, 0);
    for(int i=0;i<256;++i) {
        instr[i] = nop;
    }
    instr['+'] = inc;
    instr['-'] = dec;
    instr['>'] = rht;
    instr['<'] = lft;
    instr['.'] = out;
    instr[','] = inp;
    instr['['] = lbr;
    instr[']'] = rbr;
    if(load_file(argv[1], &src, &src_len) != 0) {
        printf("Error reading %s\n", argv[1]);
        return 1;
    }
    for(ins = src; ins < src+src_len; ++ins) {
        (instr[*ins]) ();
        if(ptr < mem) {
            ptr = mem;
        } else if(ptr >= mem + 30000) {
            ptr = mem + 30000 - 1;
        }
    }
    free(src);
    return 0;
}

canonical_original_code:
int main(int argc, unsigned char **argv) {
    if(argc != 2) {
        printf("Usage: %s <source file>\n", argv[0]);
        exit(1);
    }
    memset(mem, 30000, 0);
    for(int i = 0;i<256;++i) {
        instr[i] = nop;
    }
    instr['+'] = inc;
    instr['-'] = dec;
    instr['>'] = rht;
    instr['<'] = lft;
    instr['.'] = out;
    instr[','] = inp;
    instr['['] = lbr;
    instr[']'] = rbr;
    if(load_file(argv[1], &src, &src_len) != 0) {
        printf("Error reading %s\n", argv[1]);
        return 1;
    }
    for(ins = src; ins < src+src_len; ++ins) {
        (instr[*ins]) ();
        if(ptr < mem) {
            ptr = mem;
        } else if(ptr >= mem + 30000) {
            ptr = mem + 30000 - 1;
        }
    }
    free(src);
    return 0;
}

variable_types: argc int

variable_types:
argv
unsigned char **


nop

decompiled_code:
void nop()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code: void nop() { ; }

canonical_original_code: void nop() { ; }


inc

decompiled_code:
void *inc()
{
void *result; // rax

result = ptr;
++*(_BYTE *)ptr;
return result;
}

canonical_decompiled_code:
void *func8()
{
void *result; // rax

result = ptr;
++*(_BYTE *)ptr;
return result;
}

original_code: void inc() { (*ptr)++; }

canonical_original_code: void inc() { (*ptr)++; }


dec

decompiled_code:
void *dec()
{
void *result; // rax

result = ptr;
--*(_BYTE *)ptr;
return result;
}

canonical_decompiled_code:
void *func9()
{
void *result; // rax

result = ptr;
--*(_BYTE *)ptr;
return result;
}

original_code: void dec() { (*ptr)--; }

canonical_original_code: void dec() { (*ptr)--; }


rht

decompiled_code:
void rht()
{
ptr = (char *)ptr + 1;
}

canonical_decompiled_code:
void func10()
{
ptr = (char *)ptr + 1;
}

original_code: void rht() { ptr++; }

canonical_original_code: void rht() { ptr++; }


lft

decompiled_code:
void lft()
{
ptr = (char *)ptr - 1;
}

canonical_decompiled_code:
void func11()
{
ptr = (char *)ptr - 1;
}

original_code: void lft() { ptr--; }

canonical_original_code: void lft() { ptr--; }


lbr

decompiled_code:
void *lbr()
{
void *result; // rax
int v1; // ecx
char v2; // dl

result = ptr;
if ( !*(_BYTE *)ptr )
{
v1 = 1;
do
{
while ( 1 )
{
v2 = *(_BYTE *)(ins + 1);
result = (void *)(ins + 1);
if ( v2 == 93 )
break;
++ins;
if ( v2 == 91 )
++v1;
}
++ins;
--v1;
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
void *func12()
{
void *result; // rax
int v1; // ecx
char v2; // dl

result = ptr;
if ( !*(_BYTE *)ptr )
{
v1 = 1;
do
{
while ( 1 )
{
v2 = *(_BYTE *)(ins + 1);
result = (void *)(ins + 1);
if ( v2 == 93 )
break;
++ins;
if ( v2 == 91 )
++v1;
}
++ins;
--v1;
}
while ( v1 );
}
return result;
}

original_code:
void lbr() {
    if(*ptr!=0) { return; }
    for(int frames = 1; frames > 0; ++ins) {
        if(*(ins+1) == ']') { --frames; }
        if(*(ins+1) == '[') { ++frames; }
    }
}

variable_types: frames int


rbr

decompiled_code:
void *rbr()
{
void *result; // rax
int v1; // ecx
char v2; // dl

result = ptr;
if ( *(_BYTE *)ptr )
{
v1 = 1;
do
{
while ( 1 )
{
v2 = *(_BYTE *)(ins - 1);
result = (void *)(ins - 1);
if ( v2 != 93 )
break;
--ins;
++v1;
}
--ins;
}
while ( v2 != 91 || --v1 );
}
return result;
}

canonical_decompiled_code:
void *func13()
{
void *result; // rax
int v1; // ecx
char v2; // dl

result = ptr;
if ( *(_BYTE *)ptr )
{
v1 = 1;
do
{
while ( 1 )
{
v2 = *(_BYTE *)(ins - 1);
result = (void *)(ins - 1);
if ( v2 != 93 )
break;
--ins;
++v1;
}
--ins;
}
while ( v2 != 91 || --v1 );
}
return result;
}

original_code:
void rbr() {
    if(*ptr==0) { return; }
    for(int frames = 1; frames > 0; --ins) {
        if(*(ins-1) == ']') { ++frames; }
        if(*(ins-1) == '[') { --frames; }
    }
}


out

decompiled_code:
int out()
{
return putchar(*(unsigned __int8 *)ptr);
}

canonical_decompiled_code:
int func14()
{
return putchar(*(unsigned __int8 *)ptr);
}

original_code: void out() { putchar(*ptr); }

canonical_original_code: void out() { putchar(*ptr); }


inp

decompiled_code:
int inp()
{
int result; // eax

result = getchar();
*(_BYTE *)ptr = result;
return result;
}

canonical_decompiled_code:
int func15()
{
int result; // eax

result = getchar();
*(_BYTE *)ptr = result;
return result;
}

original_code: void inp() { *ptr=getchar(); }

canonical_original_code: void inp() { *ptr=getchar(); }


main

repo: eabalea/vrac

original_code:
int main(int argc, char **argv)
{
  wchar_t *mots[25] = {
    L"Appel d'offres",
    L"Price list",
    L"Roadmap",
    L"Budget",
    L"Produits",
    L"E-commerce",
    L"B to B",
    L"B to C",
    L"Package",
    L"Récurrent",
    L"Investissement",
    L"Ressources",
    L"Horodatage",
    L"Process",
    L"Business plan",
    L"Corporate",
    L"Datacenter",
    L"Biométrie",
    L"Déménagement",
    L"Business development",
    L"Qualification",
    L"Key Ceremony",
    L"K.Registration",
    L"Croissance externe",
    L"Dématérialisation"
  };
  int tirage,
      taille = 25,
      tailledepart = 25,
      maxlen,
      seed = 0;
  if (!setlocale(0, ""))
  {
    fprintf(stderr, "Can't set the specified locale. Check LANG, ,LC_CTYPE, LC_ALL.\n");
    return 1;
  }
  if (argc < 2)
  {
    help();
    return 1;
  }
  seed = atoi(argv[1]);
  srand(seed);
  maxlen = calcmaxlen(mots, tailledepart);
  printf("%s\n", horizontale(maxlen));
  while (taille)
  {
    tirage=rand()%taille;
    printmot(maxlen, mots[tirage]);
    if (!((taille-1)%5))
      printf("|\n%s\n", horizontale(maxlen));
    memmove(mots+tirage, mots+tirage+1, sizeof(char*)*(tailledepart-tirage+1));
    taille--;
  }
}

canonical_original_code:
int main(int argc, char **argv)
{
  int *mots[25] = {
    L"Appel d'offres",
    L"Price list",
    L"Roadmap",
    L"Budget",
    L"Produits",
    L"E-commerce",
    L"B to B",
    L"B to C",
    L"Package",
    L"Récurrent",
    L"Investissement",
    L"Ressources",
    L"Horodatage",
    L"Process",
    L"Business plan",
    L"Corporate",
    L"Datacenter",
    L"Biométrie",
    L"Déménagement",
    L"Business development",
    L"Qualification",
    L"Key Ceremony",
    L"K.Registration",
    L"Croissance externe",
    L"Dématérialisation"
  };
  int tirage; int taille = 25; int tailledepart = 25; int maxlen; int seed = 0;
  if (!setlocale(0, ""))
  {
    fprintf(stderr, "Can't set the specified locale. Check LANG, ,LC_CTYPE, LC_ALL.\n");
    return 1;
  }
  if (argc < 2)
  {
    help();
    return 1;
  }
  seed = atoi(argv[1]);
  srand(seed);
  maxlen = calcmaxlen(mots, tailledepart);
  printf("%s\n", horizontale(maxlen));
  while (taille)
  {
    tirage=rand()%taille;
    printmot(maxlen, mots[tirage]);
    if (!((taille-1)%5))
      printf("|\n%s\n", horizontale(maxlen));
    memmove(mots+tirage, mots+tirage+1, sizeof(char *)*(tailledepart-tirage+1));
    taille--;
  }
}

variable_types:
argv
char **

variable_types:
mots
int *[25]

variable_types: tirage int

variable_types: taille int

variable_types: tailledepart int

variable_types: maxlen int

variable_types: seed int


help

decompiled_code:
size_t help()
{
return fwrite(&unk_402008, 1uLL, 0x4FuLL, _bss_start);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite(&unk_402008, 1uLL, 0x4FuLL, _bss_start);
}

original_code:
void help(void)
{
  fprintf(stderr, "Fournir un argument numérique, qui servira de graine au générateur d'aléa.\n");
}

canonical_original_code:
void help()
{
  fprintf(stderr, "Fournir un argument numérique, qui servira de graine au générateur d'aléa.\n");
}


calcmaxlen

decompiled_code:
__int64 __fastcall calcmaxlen(const wchar_t **a1, int a2)
{
const wchar_t **v2; // rbx
unsigned int v3; // er12
size_t v4; // rax

if ( a2 <= 0 )
return 0LL;
v2 = a1;
v3 = 0;
do
{
v4 = wcslen(*v2);
if ( v4 > (int)v3 )
v3 = v4;
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(const wchar_t **a1, int a2)
{
const wchar_t **v2; // rbx
unsigned int v3; // er12
size_t v4; // rax

if ( a2 <= 0 )
return 0LL;
v2 = a1;
v3 = 0;
do
{
v4 = wcslen(*v2);
if ( v4 > (int)v3 )
v3 = v4;
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
return v3;
}

original_code:
int calcmaxlen(wchar_t *mots[], int nbmots)
{
  int i,
      maxlen = 0;
  for(i = 0; i < nbmots; i++)
    if (wcslen(mots[i]) > maxlen)
      maxlen = wcslen(mots[i]);
  return maxlen;
}

canonical_original_code:
int calcmaxlen(int *mots[], int nbmots)
{
  int i; int maxlen = 0;
  for(i = 0; i < nbmots; i++)
    if (wcslen(mots[i]) > maxlen)
      maxlen = wcslen(mots[i]);
  return maxlen;
}

variable_types:
mots
int *[]

variable_types: nbmots int

variable_types: i int


horizontale

original_code:
char *horizontale(int maxlen)
{
  char ligne[1024] = "";
  char elemcolonne[100] = "";
  int i;
  strcat(ligne, "+");
  for(i = 0; i < maxlen; i++)
    strcat(elemcolonne, "-");
  for(i = 0; i < 5; i++)
  {
    strcat(ligne, elemcolonne);
    strcat(ligne, "+");
  }
  return ligne;
}

variable_types:
ligne
char [1024]

variable_types:
elemcolonne
char [100]


printmot

decompiled_code:
int __fastcall printmot(int a1, const wchar_t *a2)
{
int v2; // eax

v2 = wcslen(a2);
return printf("|%*s%ls%*s", (a1 - v2) / 2, "", a2, a1 - (a1 - v2) / 2 - v2, "");
}

canonical_decompiled_code:
int __fastcall func10(int a1, const wchar_t *a2)
{
int v2; // eax

v2 = wcslen(a2);
return printf("|%*s%ls%*s", (a1 - v2) / 2, "", a2, a1 - (a1 - v2) / 2 - v2, "");
}

original_code:
void printmot(int maxlen, wchar_t *mot)
{
  int left = 0,
      right = 0,
      len = wcslen(mot);
  left = (maxlen-len)/2;
  right = maxlen-left-len;
  printf("|%*s%ls%*s", left, "", mot, right, "");
}

canonical_original_code:
void printmot(int maxlen, int *mot)
{
  int left = 0; int right = 0; int len = wcslen(mot);
  left = (maxlen-len)/2;
  right = maxlen-left-len;
  printf("|%*s%ls%*s", left, "", mot, right, "");
}

variable_types:
mot
int *

variable_types: left int

variable_types: right int

variable_types: len int