decompiled_code:
__int64 __fastcall pipeserver_msg_read(_BYTE *a1, int a2, _DWORD *a3, _DWORD *a4, _QWORD *a5, _DWORD *a6)
{
__int64 result; // rax
int v11; // edx
int v12; // [rsp+8h] [rbp-40h] BYREF
int v13[15]; // [rsp+Ch] [rbp-3Ch] BYREF

result = get_msg_lens(a1, a2, v13, &v12);
if ( (_DWORD)result )
{
result = read_pipeserver_event((__int64)a1, v13[0], a3);
if ( (_DWORD)result )
{
result = read_pollset_handle((__int64)&a1[v13[0] + 1], v12, a4);
if ( (_DWORD)result )
{
result = 1LL;
if ( (*a3 & 0xFFFFFFFD) == 5 )
{
v11 = v13[0] + v12 + 2;
result = 0LL;
if ( a2 >= v11 && a1[v11 - 1] == 32 )
{
*a5 = &a1[v11];
*a6 = a2 - v11;
result = 1LL;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_BYTE *a1, int a2, _DWORD *a3, _DWORD *a4, _QWORD *a5, _DWORD *a6)
{
__int64 result; // rax
int v11; // edx
int v12; // [rsp+8h] [rbp-40h] BYREF
int v13[15]; // [rsp+Ch] [rbp-3Ch] BYREF

result = func3(a1, a2, v13, &v12);
if ( (_DWORD)result )
{
result = func2((__int64)a1, v13[0], a3);
if ( (_DWORD)result )
{
result = func4((__int64)&a1[v13[0] + 1], v12, a4);
if ( (_DWORD)result )
{
result = 1LL;
if ( (*a3 & 0xFFFFFFFD) == 5 )
{
v11 = v13[0] + v12 + 2;
result = 0LL;
if ( a2 >= v11 && a1[v11 - 1] == 32 )
{
*a5 = &a1[v11];
*a6 = a2 - v11;
result = 1LL;
}
}
}
}
}
return result;
}

original_code:
int
pipeserver_msg_read(const char *msg, int len,
                    pipeserver_event *e, pollset_handle *h,
                    const char **data, int *datalen)
{
    int event_len, handle_len, total_len;
    if(!get_msg_lens(msg, len, &event_len, &handle_len))
        return 0;
    if(!read_pipeserver_event(msg, event_len, e))
        return 0;
    if(!read_pollset_handle(msg+event_len+1, handle_len, h))
        return 0;
    if(*e!=PIPESERVER_RECEIVED && *e!=PIPESERVER_SEND)
        return 1;
    total_len=event_len+handle_len+2;
    if(len<total_len)
        return 0;
    if(msg[total_len-1]!=' ')
        return 0;
    *data=msg+total_len;
    *datalen=len-total_len;
    return 1;
}

canonical_original_code:
int pipeserver_msg_read(char *msg, int len, enum pipeserver_event *e, int *h, char **data, int *datalen)
{
    int event_len; int handle_len; int total_len;
    if(!get_msg_lens(msg, len, &event_len, &handle_len))
        return 0;
    if(!read_pipeserver_event(msg, event_len, e))
        return 0;
    if(!read_pollset_handle(msg+event_len+1, handle_len, h))
        return 0;
    if(*e!=PIPESERVER_RECEIVED && *e!=PIPESERVER_SEND)
        return 1;
    total_len=event_len+handle_len+2;
    if(len<total_len)
        return 0;
    if(msg[total_len-1]!=' ')
        return 0;
    *data=msg+total_len;
    *datalen=len-total_len;
    return 1;
}

variable_types:
data
char **

variable_types:
datalen
int *


repo: sheheitthey/pipeserver

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

pollconn_construct();
result = pollconn_socket(a1);
*(_DWORD *)(a1 + 128) = -1;
*(_DWORD *)(a1 + 132) = 0;
*(_DWORD *)(a1 + 136) = 0;
*(_DWORD *)(a1 + 140) = 0;
*(_BYTE *)(a1 + 144) = 10;
return result;
}

original_code:
void lineclient_construct(lineclient *lc, struct pollfd *pfd,
                          int sendbuf_len, int recvbuf_len)
{
    pollconn_construct(&lc->c, pfd, sendbuf_len, recvbuf_len);
    pollconn_socket(&lc->c);
    lc->next_pos=-1;
    lc->closed=0;
    lc->shutdown=0;
    lc->error=0;
    lc->delim='\n';
}

canonical_original_code:
void lineclient_construct(struct lineclient *lc, struct pollfd *pfd, int sendbuf_len, int recvbuf_len)
{
    pollconn_construct(&lc->c, pfd, sendbuf_len, recvbuf_len);
    pollconn_socket(&lc->c);
    lc->next_pos=-1;
    lc->closed=0;
    lc->shutdown=0;
    lc->error=0;
    lc->delim='\n';
}

variable_types: struct lineclient { struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } c; int next_pos; int closed; int shutdown; int error; char delim; } *

variable_types:
pfd
struct pollfd *

return_type: void

user_defined_types: struct lineclient { struct pollconn c; int next_pos; int closed; int shutdown; int error; char delim; }

user_defined_types: struct pollconn { struct pollsock s; struct sockaddr_in saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; }

user_defined_types: struct pollsock { struct pollfd *pfd; struct tcpbuf sendbuf; struct tcpbuf recvbuf; }

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


decompiled_code:
__int64 lineclient_destruct()
{
return pollconn_destruct();
}

canonical_decompiled_code:
__int64 func2()
{
return pollconn_destruct();
}

original_code:
void lineclient_destruct(lineclient *lc)
{
    pollconn_destruct(&lc->c);
}

canonical_original_code:
void lineclient_destruct(struct lineclient *lc)
{
    pollconn_destruct(&lc->c);
}

user_defined_types: struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; }

user_defined_types: struct tcpbuf { int len; int pos; unsigned char *data; }

user_defined_types: struct pollfd { int fd; short events; short revents; }


decompiled_code:
__int64 __fastcall lineclient_resolve(__int64 a1, __int64 a2, unsigned __int16 a3)
{
return pollconn_resolve(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned __int16 a3)
{
return pollconn_resolve(a1, a2, a3);
}

original_code:
int lineclient_resolve(lineclient *lc, const char *host, u_int16_t port)
{
    return pollconn_resolve(&lc->c, host, port);
}

canonical_original_code:
int lineclient_resolve(struct lineclient *lc, char *host, unsigned int port)
{
    return pollconn_resolve(&lc->c, host, port);
}

variable_types:
host
char *

variable_types:
port
unsigned int

return_type: int


decompiled_code:
__int64 lineclient_connect()
{
return pollconn_connect();
}

canonical_decompiled_code:
__int64 func4()
{
return pollconn_connect();
}

original_code:
int lineclient_connect(lineclient *lc)
{
    return pollconn_connect(&lc->c);
}

canonical_original_code:
int lineclient_connect(struct lineclient *lc)
{
    return pollconn_connect(&lc->c);
}


decompiled_code:
__int64 __fastcall lineclient_operate(_DWORD *a1)
{
__int64 result; // rax
int v2; // eax
int v3; // eax

pollconn_operate();
advance_next_pos((__int64)a1);
if ( (unsigned int)pollconn_did_close(a1) )
a1[33] = 1;
if ( (unsigned int)pollconn_did_recv(a1) )
{
if ( !(unsigned int)pollconn_recv_bytes(a1) )
a1[34] = 1;
v2 = pollconn_err_recv(a1);
if ( v2 )
a1[35] = v2;
}
if ( (unsigned int)pollconn_did_send(a1) )
{
v3 = pollconn_err_send(a1);
if ( v3 )
a1[35] = v3;
}
result = pollconn_did_shutdown(a1);
if ( (_DWORD)result )
{
result = pollconn_err_shutdown(a1);
if ( (_DWORD)result )
a1[35] = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_DWORD *a1)
{
__int64 result; // rax
int v2; // eax
int v3; // eax

pollconn_operate();
func0((__int64)a1);
if ( (unsigned int)pollconn_did_close(a1) )
a1[33] = 1;
if ( (unsigned int)pollconn_did_recv(a1) )
{
if ( !(unsigned int)pollconn_recv_bytes(a1) )
a1[34] = 1;
v2 = pollconn_err_recv(a1);
if ( v2 )
a1[35] = v2;
}
if ( (unsigned int)pollconn_did_send(a1) )
{
v3 = pollconn_err_send(a1);
if ( v3 )
a1[35] = v3;
}
result = pollconn_did_shutdown(a1);
if ( (_DWORD)result )
{
result = pollconn_err_shutdown(a1);
if ( (_DWORD)result )
a1[35] = result;
}
return result;
}

original_code:
void lineclient_operate(lineclient *lc)
{
    int error;
    pollconn *c;
    pollconn_operate(&lc->c);
    advance_next_pos(lc);
    c=&lc->c;
    if(pollconn_did_close(c))
       lc->closed=1;
    if(pollconn_did_recv(c))
    {
        if(pollconn_recv_bytes(c)==0)
            lc->shutdown=1;
        error=pollconn_err_recv(c);
        if(error!=0)
            lc->error=error;
    }
    if(pollconn_did_send(c))
    {
        error=pollconn_err_send(c);
        if(error!=0)
            lc->error=error;
    }
    if(pollconn_did_shutdown(c))
    {
        error=pollconn_err_shutdown(c);
        if(error!=0)
            lc->error=error;
    }
}

canonical_original_code:
void lineclient_operate(struct lineclient *lc)
{
    int error;
    struct pollconn *c;
    pollconn_operate(&lc->c);
    advance_next_pos(lc);
    c=&lc->c;
    if(pollconn_did_close(c))
       lc->closed=1;
    if(pollconn_did_recv(c))
    {
        if(pollconn_recv_bytes(c)==0)
            lc->shutdown=1;
        error=pollconn_err_recv(c);
        if(error!=0)
            lc->error=error;
    }
    if(pollconn_did_send(c))
    {
        error=pollconn_err_send(c);
        if(error!=0)
            lc->error=error;
    }
    if(pollconn_did_shutdown(c))
    {
        error=pollconn_err_shutdown(c);
        if(error!=0)
            lc->error=error;
    }
}

variable_types: error int

variable_types: struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *


decompiled_code:
__int64 __fastcall lineclient_enqueue(__int64 a1, __int64 a2, int a3)
{
if ( a3 < 0 )
return 0xFFFFFFFFLL;
if ( (int)pollconn_sendbuf_space() <= a3 )
return 0xFFFFFFFFLL;
pollconn_enqueue(a1, (unsigned int)a3, a2);
pollconn_enqueue(a1, 1LL, a1 + 144);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3)
{
if ( a3 < 0 )
return 0xFFFFFFFFLL;
if ( (int)pollconn_sendbuf_space() <= a3 )
return 0xFFFFFFFFLL;
pollconn_enqueue(a1, (unsigned int)a3, a2);
pollconn_enqueue(a1, 1LL, a1 + 144);
return 0LL;
}

original_code:
int lineclient_enqueue(lineclient *lc, const char *data, int len)
{
    pollconn *c;
    if(len<0)
        return -1;
    c=&lc->c;
    if(pollconn_sendbuf_space(c)<len+1)
        return -1;
    pollconn_enqueue(c, len, data);
    pollconn_enqueue(c, 1, &lc->delim);
    return 0;
}

canonical_original_code:
int lineclient_enqueue(struct lineclient *lc, char *data, int len)
{
    struct pollconn *c;
    if(len<0)
        return -1;
    c=&lc->c;
    if(pollconn_sendbuf_space(c)<len+1)
        return -1;
    pollconn_enqueue(c, len, data);
    pollconn_enqueue(c, 1, &lc->delim);
    return 0;
}

variable_types:
data
char *

variable_types: len int


decompiled_code:
__int64 lineclient_shutdown()
{
pollconn_shutdown();
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
pollconn_shutdown();
return 0LL;
}

original_code:
int lineclient_shutdown(lineclient *lc)
{
    pollconn_shutdown(&lc->c);
    return 0;
}

canonical_original_code:
int lineclient_shutdown(struct lineclient *lc)
{
    pollconn_shutdown(&lc->c);
    return 0;
}


decompiled_code:
__int64 lineclient_close()
{
return pollconn_close();
}

canonical_decompiled_code:
__int64 func8()
{
return pollconn_close();
}

original_code:
int lineclient_close(lineclient *lc)
{
    return pollconn_close(&lc->c);
}

canonical_original_code:
int lineclient_close(struct lineclient *lc)
{
    return pollconn_close(&lc->c);
}


decompiled_code:
__int64 __fastcall lineclient_getline(__int64 a1, __int64 *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v5; // rdx

result = 0LL;
if ( *(int *)(a1 + 128) >= 0 )
{
v5 = pollconn_recvbuf_data(a1);
result = 0LL;
if ( *(unsigned __int8 *)(v5 + *(int *)(a1 + 128)) == *(char *)(a1 + 144) )
{
if ( a2 )
*a2 = v5;
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 128);
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v5; // rdx

result = 0LL;
if ( *(int *)(a1 + 128) >= 0 )
{
v5 = pollconn_recvbuf_data(a1);
result = 0LL;
if ( *(unsigned __int8 *)(v5 + *(int *)(a1 + 128)) == *(char *)(a1 + 144) )
{
if ( a2 )
*a2 = v5;
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 128);
result = 1LL;
}
}
}
return result;
}

original_code:
int lineclient_getline(const lineclient *lc, const char **data, int *len)
{
    const pollconn *c;
    const u_char *t;
    if(lc->next_pos<0)
        return 0;
    c=&lc->c;
    t=pollconn_recvbuf_data(c);
    if(t[lc->next_pos]!=lc->delim)
        return 0;
    if(data)
        *data=t;
    if(len)
        *len=lc->next_pos;
    return 1;
}

canonical_original_code:
int lineclient_getline(struct lineclient *lc, char **data, int *len)
{
    struct pollconn *c;
    unsigned char *t;
    if(lc->next_pos<0)
        return 0;
    c=&lc->c;
    t=pollconn_recvbuf_data(c);
    if(t[lc->next_pos]!=lc->delim)
        return 0;
    if(data)
        *data=t;
    if(len)
        *len=lc->next_pos;
    return 1;
}

variable_types:
len
int *

variable_types:
t
unsigned char *


decompiled_code:
__int64 __fastcall lineclient_getevent(_DWORD *a1)
{
__int64 result; // rax
int v2; // edx

result = 2LL;
if ( !a1[35] )
{
v2 = lineclient_getline((__int64)a1, 0LL, 0LL);
result = 4LL;
if ( !v2 )
{
result = 3LL;
if ( !a1[34] )
result = a1[33] != 0;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(_DWORD *a1)
{
__int64 result; // rax
int v2; // edx

result = 2LL;
if ( !a1[35] )
{
v2 = func9((__int64)a1, 0LL, 0LL);
result = 4LL;
if ( !v2 )
{
result = 3LL;
if ( !a1[34] )
result = a1[33] != 0;
}
}
return result;
}

original_code:
lineclient_event lineclient_getevent(const lineclient *lc)
{
    if(lc->error)
        return LINECLIENT_ERROR;
    if(lineclient_getline(lc, ((void *)0), ((void *)0)))
        return LINECLIENT_LINE;
    if(lc->shutdown)
        return LINECLIENT_SHUTDOWN;
    if(lc->closed)
        return LINECLIENT_CLOSED;
    return LINECLIENT_NULLEVENT;
}

canonical_original_code:
enum lineclient_event lineclient_getevent(struct lineclient *lc)
{
    if(lc->error)
        return LINECLIENT_ERROR;
    if(lineclient_getline(lc, ((void *)0), ((void *)0)))
        return LINECLIENT_LINE;
    if(lc->shutdown)
        return LINECLIENT_SHUTDOWN;
    if(lc->closed)
        return LINECLIENT_CLOSED;
    return LINECLIENT_NULLEVENT;
}

return_type: enum lineclient_event { LINECLIENT_NULLEVENT=0, LINECLIENT_CLOSED=1, LINECLIENT_ERROR=2, LINECLIENT_SHUTDOWN=3, LINECLIENT_LINE=4 }


decompiled_code:
__int64 __fastcall lineclient_dequeue(_DWORD *a1)
{
unsigned int v1; // eax
unsigned int v2; // edx
int v3; // eax

v1 = lineclient_getevent(a1);
if ( v1 == 3 )
{
a1[34] = 0;
v2 = 1;
}
else if ( v1 > 3 )
{
v2 = 0;
if ( v1 == 4 )
{
v3 = lineclient_getline((__int64)a1, 0LL, 0LL);
v2 = 1;
if ( v3 )
{
pollconn_dequeue(a1, (unsigned int)(a1[32] + 1), 1LL);
a1[32] = -1;
advance_next_pos((__int64)a1);
v2 = 1;
}
}
}
else if ( v1 == 1 )
{
a1[33] = 0;
v2 = 1;
}
else if ( v1 == 2 )
{
a1[35] = 0;
lineclient_close(a1);
v2 = 1;
}
else
{
v2 = 0;
}
return v2;
}

original_code:
int lineclient_dequeue(lineclient *lc)
{
    pollconn *c;
    lineclient_event e;
    e=lineclient_getevent(lc);
    switch(e)
    {
    case LINECLIENT_LINE:
        c=&lc->c;
        if(lineclient_getline(lc, ((void *)0), ((void *)0)))
        {
            pollconn_dequeue(c, (lc->next_pos)+1);
            lc->next_pos=-1;
            advance_next_pos(lc);
        }
        break;
    case LINECLIENT_SHUTDOWN:
        lc->shutdown=0;
        break;
    case LINECLIENT_CLOSED:
        lc->closed=0;
        break;
    case LINECLIENT_ERROR:
        lc->error=0;
        lineclient_close(lc);
        break;
    default:
        return 0;
    }
    return 1;
}

canonical_original_code:
int lineclient_dequeue(struct lineclient *lc)
{
    struct pollconn *c;
    enum lineclient_event e;
    e=lineclient_getevent(lc);
    switch(e)
    {
    case LINECLIENT_LINE:
        c=&lc->c;
        if(lineclient_getline(lc, ((void *)0), ((void *)0)))
        {
            pollconn_dequeue(c, (lc->next_pos)+1);
            lc->next_pos=-1;
            advance_next_pos(lc);
        }
        break;
    case LINECLIENT_SHUTDOWN:
        lc->shutdown=0;
        break;
    case LINECLIENT_CLOSED:
        lc->closed=0;
        break;
    case LINECLIENT_ERROR:
        lc->error=0;
        lineclient_close(lc);
        break;
    default:
        return 0;
    }
    return 1;
}

variable_types: enum lineclient_event { LINECLIENT_NULLEVENT=0, LINECLIENT_CLOSED=1, LINECLIENT_ERROR=2, LINECLIENT_SHUTDOWN=3, LINECLIENT_LINE=4 }


decompiled_code:
__int64 __fastcall lineclient_geterror(__int64 a1)
{
return *(unsigned int *)(a1 + 140);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return *(unsigned int *)(a1 + 140);
}

original_code:
int lineclient_geterror(const lineclient *lc)
{
    return lc->error;
}

canonical_original_code:
int lineclient_geterror(struct lineclient *lc)
{
    return lc->error;
}


decompiled_code:
__int64 __fastcall lineclient_conn(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
return a1;
}

original_code:
const pollconn* lineclient_conn(const lineclient *lc)
{
    return &lc->c;
}

canonical_original_code:
struct pollconn *lineclient_conn(struct lineclient *lc)
{
    return &lc->c;
}

variable_types: struct lineclient { struct pollconn c; int next_pos; int closed; int shutdown; int error; char delim; } *

return_type: struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *


decompiled_code:
void __fastcall test_strings_destruct(__int64 a1)
{
free(*(void **)(a1 + 8));
free(*(void **)(a1 + 16));
}

original_code:
void
test_strings_destruct(test_strings *ts)
{
    free(ts->strings);
    free(ts->buf);
}

canonical_original_code:
void test_strings_destruct(struct test_strings *ts)
{
    free(ts->strings);
    free(ts->buf);
}

variable_types: struct test_strings { int num_strings; struct test_string { int len; char *str; int time; } *strings; char *buf; int next_send; int next_receive; } *

user_defined_types: struct test_strings { int num_strings; struct test_string *strings; char *buf; int next_send; int next_receive; }


original_code:
int
test_strings_send(test_strings *ts, const char **buf, int *len)
{
    test_string *t;
    if(ts->next_send>=ts->num_strings)
        return -1;
    t=ts->strings+ts->next_send;
    if(time(((void *)0))>=t->time)
    {
        *buf=t->str;
        *len=t->len;
        return 1;
    }
    return 0;
}

canonical_original_code:
int test_strings_send(struct test_strings *ts, char **buf, int *len)
{
    struct test_string *t;
    if(ts->next_send>=ts->num_strings)
        return -1;
    t=ts->strings+ts->next_send;
    if(time(((void *)0))>=t->time)
    {
        *buf=t->str;
        *len=t->len;
        return 1;
    }
    return 0;
}

variable_types:
buf
char **

variable_types: struct test_string { int len; char *str; int time; } *


decompiled_code:
__int64 __fastcall test_strings_sent(_DWORD *a1)
{
__int64 result; // rax

result = (unsigned int)a1[6];
if ( (int)result < *a1 )
{
result = (unsigned int)(result + 1);
a1[6] = result;
}
return result;
}

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

result = (unsigned int)a1[6];
if ( (int)result < *a1 )
{
result = (unsigned int)(result + 1);
a1[6] = result;
}
return result;
}

original_code:
void
test_strings_sent(test_strings *ts)
{
    if(ts->next_send<ts->num_strings)
        (ts->next_send)++;
}

canonical_original_code:
void test_strings_sent(struct test_strings *ts)
{
    if(ts->next_send<ts->num_strings)
        (ts->next_send)++;
}

user_defined_types: struct test_string { int len; char *str; int time; }


decompiled_code:
__int64 __fastcall test_strings_received(__int64 a1, __int64 a2, unsigned int a3)
{
int v3; // er10
__int64 result; // rax
__int64 v5; // r9
__int64 v6; // rax
__int64 v7; // r8
char v8; // cl
char v9; // dl

v3 = *(_DWORD *)(a1 + 28);
result = 0xFFFFFFFFLL;
if ( v3 < *(_DWORD *)a1 )
{
v5 = *(_QWORD *)(a1 + 8) + 24LL * v3;
result = 0LL;
if ( *(_DWORD *)v5 == a3 )
{
v6 = (int)(a3 - 1);
v7 = (int)a3 - 1LL - a3;
while ( v6 != v7 )
{
v8 = *(_BYTE *)(a2 + v6);
v9 = *(_BYTE *)(*(_QWORD *)(v5 + 8) + v6--);
if ( v8 != v9 )
return 0LL;
}
*(_DWORD *)(a1 + 28) = v3 + 1;
result = 1LL;
}
}
return result;
}

original_code:
int
test_strings_received(test_strings *ts, const char *buf, int len)
{
    test_string *t;
    if(ts->next_receive>=ts->num_strings)
        return -1;
    t=ts->strings+ts->next_receive;
    if(len!=t->len)
        return 0;
    while(len--)
    {
        if(buf[len]!=t->str[len])
            return 0;
    }
    (ts->next_receive)++;
    return 1;
}

canonical_original_code:
int test_strings_received(struct test_strings *ts, char *buf, int len)
{
    struct test_string *t;
    if(ts->next_receive>=ts->num_strings)
        return -1;
    t=ts->strings+ts->next_receive;
    if(len!=t->len)
        return 0;
    while(len--)
    {
        if(buf[len]!=t->str[len])
            return 0;
    }
    (ts->next_receive)++;
    return 1;
}

variable_types:
buf
char *


decompiled_code:
void __fastcall generate_line(__int64 a1, int a2)
{
_BYTE *v2; // rbx
__int64 v3; // rbp
unsigned __int64 v4; // rax

if ( a2 )
{
v2 = (_BYTE *)(a1 + a2 - 1);
v3 = 9217LL;
do
{
do
{
LODWORD(v4) = rand();
*v2 = v4;
}
while ( (unsigned __int8)v4 <= 0xDu && _bittest64(&v3, v4) );
--v2;
}
while ( v2 != (_BYTE *)(a1 + a2 - 2 - (unsigned int)(a2 - 1)) );
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1, int a2)
{
_BYTE *v2; // rbx
__int64 v3; // rbp
unsigned __int64 v4; // rax

if ( a2 )
{
v2 = (_BYTE *)(a1 + a2 - 1);
v3 = 9217LL;
do
{
do
{
LODWORD(v4) = rand();
*v2 = v4;
}
while ( (unsigned __int8)v4 <= 0xDu && _bittest64(&v3, v4) );
--v2;
}
while ( v2 != (_BYTE *)(a1 + a2 - 2 - (unsigned int)(a2 - 1)) );
}
}

original_code:
void
generate_line(char *buf, int len)
{
    while(len--)
    {
        do
            buf[len]=(char)rand();
        while(buf[len]=='\n' || buf[len]=='\0' || buf[len]=='\r'
              );
    }
}


original_code:
void
test_strings_construct(test_strings *ts, int num_strings,
                            int min_len, int max_len,
                            int min_wait, int max_wait)
{
    int i;
    int last_wait=0;
    ts->num_strings=num_strings;
    ts->strings=(test_string*)malloc(sizeof(test_string)*num_strings);
    ts->buf=(char*)malloc(max_len*num_strings);
    ts->next_send=0;
    ts->next_receive=0;
    for(i=0; i<ts->num_strings; i++)
    {
        ts->strings[i].len=rand_range(min_len, max_len);
        last_wait+=rand_range(min_wait, max_wait);
        ts->strings[i].time=start_time+last_wait;
        ts->strings[i].str=ts->buf+(max_len*i);
        generate_line(ts->strings[i].str, ts->strings[i].len);
    }
}

canonical_original_code:
void test_strings_construct(struct test_strings *ts, int num_strings, int min_len, int max_len, int min_wait, int max_wait)
{
    int i;
    int last_wait = 0;
    ts->num_strings=num_strings;
    ts->strings=(struct test_string *)malloc(sizeof(test_string)*num_strings);
    ts->buf=(char *)malloc(max_len*num_strings);
    ts->next_send=0;
    ts->next_receive=0;
    for(i=0; i<ts->num_strings; i++)
    {
        ts->strings[i].len=rand_range(min_len, max_len);
        last_wait+=rand_range(min_wait, max_wait);
        ts->strings[i].time=start_time+last_wait;
        ts->strings[i].str=ts->buf+(max_len*i);
        generate_line(ts->strings[i].str, ts->strings[i].len);
    }
}

variable_types: i int


decompiled_code:
__int64 __fastcall get_min_max(char *nptr, int *a2, int *a3)
{
int v5; // eax
int v6; // er13
const unsigned __int16 *v7; // r8
__int64 v8; // rax
int v9; // edi
char v10; // dl

v5 = strlen(nptr);
if ( v5 > 0 )
{
v6 = v5;
v7 = *_ctype_b_loc();
v8 = 0LL;
v9 = -1;
while ( 1 )
{
v10 = nptr[v8];
if ( (v7[v10] & 0x800) == 0 )
{
if ( v9 != -1 || v10 != 44 )
return 0LL;
v9 = v8;
}
if ( v8 == v6 - 1 )
goto LABEL_10;
++v8;
}
}
v9 = -1;
LABEL_10:
nptr[v9] = 0;
*a2 = atoi(nptr);
*a3 = atoi(&nptr[v9 + 1]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *nptr, int *a2, int *a3)
{
int v5; // eax
int v6; // er13
const unsigned __int16 *v7; // r8
__int64 v8; // rax
int v9; // edi
char v10; // dl

v5 = strlen(nptr);
if ( v5 > 0 )
{
v6 = v5;
v7 = *_ctype_b_loc();
v8 = 0LL;
v9 = -1;
while ( 1 )
{
v10 = nptr[v8];
if ( (v7[v10] & 0x800) == 0 )
{
if ( v9 != -1 || v10 != 44 )
return 0LL;
v9 = v8;
}
if ( v8 == v6 - 1 )
goto LABEL_10;
++v8;
}
}
v9 = -1;
LABEL_10:
nptr[v9] = 0;
*a2 = atoi(nptr);
*a3 = atoi(&nptr[v9 + 1]);
return 1LL;
}

original_code:
int get_min_max(char *buf, int *min, int *max)
{
    int i, len, comma_pos=-1;
    char c;
    len=strlen(buf);
    for(i=0; i<len; i++)
    {
        c=buf[i];
        if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
        {
            if(comma_pos==-1 && c==',')
                comma_pos=i;
            else
                return 0;
        }
    }
    buf[comma_pos]='\0';
    *min=atoi(buf);
    *max=atoi(buf+comma_pos+1);
    if(min<0 || max<0)
        return 0;
    return 1;
}

canonical_original_code:
int get_min_max(char *buf, int *min, int *max)
{
    int i; int len; int comma_pos = -1;
    char c;
    len=strlen(buf);
    for(i=0; i<len; i++)
    {
        c=buf[i];
        if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
        {
            if(comma_pos==-1 && c==',')
                comma_pos=i;
            else
                return 0;
        }
    }
    buf[comma_pos]='\0';
    *min=atoi(buf);
    *max=atoi(buf+comma_pos+1);
    if(min<0 || max<0)
        return 0;
    return 1;
}

variable_types:
min
int *

variable_types:
max
int *

variable_types: c char


decompiled_code:
int __fastcall print_string(char *a1, int a2)
{
char *v2; // rbx
int result; // eax

if ( a2 > 0 )
{
v2 = a1;
do
result = putchar(*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *a1, int a2)
{
char *v2; // rbx
int result; // eax

if ( a2 > 0 )
{
v2 = a1;
do
result = putchar(*v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return result;
}

original_code:
void
print_string(const char *buf, int len)
{
    int i;
    for(i=0; i<len; i++)
        printf("%c", buf[i]);
}

canonical_original_code:
void print_string(char *buf, int len)
{
    int i;
    for(i=0; i<len; i++)
        printf("%c", buf[i]);
}


decompiled_code:
int __fastcall print_test_strings(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rbp
int result; // eax

if ( *(int *)a1 > 0 )
{
v1 = 0LL;
do
{
v2 = *(_QWORD *)(a1 + 8) + 24 * v1;
printf("time=%d\n", *(unsigned int *)(v2 + 16));
printf("string=");
print_string(*(char **)(v2 + 8), *(_DWORD *)v2);
result = puts("\n");
++v1;
}
while ( *(_DWORD *)a1 > (int)v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rbp
int result; // eax

if ( *(int *)a1 > 0 )
{
v1 = 0LL;
do
{
v2 = *(_QWORD *)(a1 + 8) + 24 * v1;
printf("time=%d\n", *(unsigned int *)(v2 + 16));
printf("string=");
func9(*(char **)(v2 + 8), *(_DWORD *)v2);
result = puts("\n");
++v1;
}
while ( *(_DWORD *)a1 > (int)v1 );
}
return result;
}

original_code:
void
print_test_strings(test_strings *ts)
{
    int i;
    test_string *t;
    for(i=0; i<ts->num_strings; i++)
    {
        t=ts->strings+i;
        printf("time=%d\n", t->time);
        printf("string=");
        print_string(t->str, t->len);
        printf("\n\n");
    }
}

canonical_original_code:
void print_test_strings(struct test_strings *ts)
{
    int i;
    struct test_string *t;
    for(i=0; i<ts->num_strings; i++)
    {
        t=ts->strings+i;
        printf("time=%d\n", t->time);
        printf("string=");
        print_string(t->str, t->len);
        printf("\n\n");
    }
}


decompiled_code:
int print_usage_str()
{
return fprintf(
stderr,
"Usage: echo_test <-n number of lines (default %d)\n"
" <-s min,max size of lines (default %d,%d)\n"
" <-w min,max wait between lines (default %d,%d)\n"
" [host] [port]\n",
32LL,
0LL,
256LL,
0LL,
8LL);
}

canonical_decompiled_code:
int func7()
{
return fprintf(
stderr,
"Usage: echo_test <-n number of lines (default %d)\n"
" <-s min,max size of lines (default %d,%d)\n"
" <-w min,max wait between lines (default %d,%d)\n"
" [host] [port]\n",
32LL,
0LL,
256LL,
0LL,
8LL);
}

original_code:
void
print_usage_str()
{
    fprintf(stderr, usage_fmt, 32, 0, 256,
            0, 8);
}


decompiled_code:
__int64 __fastcall read_args(int argc, char **argv)
{
unsigned int v2; // ebx
char v3; // al

while ( 1 )
{
v3 = getopt(argc, argv, "n:s:w:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 's':
v2 = get_min_max(optarg, &min_size, &max_size);
if ( !v2 )
{
fwrite("Error: invalid size range.\n", 1uLL, 0x1BuLL, stderr);
return v2;
}
break;
case 'w':
v2 = get_min_max(optarg, &min_wait, &max_wait);
if ( !v2 )
{
fwrite("Error: invalid wait range.\n", 1uLL, 0x1BuLL, stderr);
return v2;
}
break;
case 'n':
num_lines = atoi(optarg);
if ( num_lines < 0 )
{
fwrite("Error: invalid number of lines.\n", 1uLL, 0x20uLL, stderr);
return 0;
}
break;
}
}
if ( argc - optind <= 1 )
{
print_usage_str();
v2 = 0;
}
else
{
host = (__int64)argv[optind];
port = atoi(argv[optind + 1]);
v2 = 1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int argc, char **argv)
{
unsigned int v2; // ebx
char v3; // al

while ( 1 )
{
v3 = getopt(argc, argv, "n:s:w:");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 's':
v2 = func6(optarg, &min_size, &max_size);
if ( !v2 )
{
fwrite("Error: invalid size range.\n", 1uLL, 0x1BuLL, stderr);
return v2;
}
break;
case 'w':
v2 = func6(optarg, &min_wait, &max_wait);
if ( !v2 )
{
fwrite("Error: invalid wait range.\n", 1uLL, 0x1BuLL, stderr);
return v2;
}
break;
case 'n':
num_lines = atoi(optarg);
if ( num_lines < 0 )
{
fwrite("Error: invalid number of lines.\n", 1uLL, 0x20uLL, stderr);
return 0;
}
break;
}
}
if ( argc - optind <= 1 )
{
func7();
v2 = 0;
}
else
{
host = (__int64)argv[optind];
port = atoi(argv[optind + 1]);
v2 = 1;
}
return v2;
}

original_code:
int
read_args(int argc, char *argv[])
{
    char opt;
    while((opt=getopt(argc, argv, "n:s:w:"))!=-1)
    {
        switch(opt)
        {
        case 'n':
            num_lines=atoi(optarg);
            if(num_lines<0)
            {
                fprintf(stderr, "Error: invalid number of lines.\n");
                return 0;
            }
            break;
        case 's':
            if(!get_min_max(optarg, &min_size, &max_size))
            {
                fprintf(stderr, "Error: invalid size range.\n");
                return 0;
            }
            break;
        case 'w':
            if(!get_min_max(optarg, &min_wait, &max_wait))
            {
                fprintf(stderr, "Error: invalid wait range.\n");
                return 0;
            }
            break;
        }
    }
    if(argc-optind<=1)
    {
        print_usage_str();
        return 0;
    }
    host=argv[optind];
    port=atoi(argv[optind+1]);
    return 1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt char


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
__int64 v5; // rbx
int v6; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // eax
struct timeval v12; // [rsp+0h] [rbp-108h] BYREF
unsigned int v13; // [rsp+14h] [rbp-F4h] BYREF
__int64 v14; // [rsp+18h] [rbp-F0h] BYREF
_DWORD v15[10]; // [rsp+20h] [rbp-E8h] BYREF
struct pollfd fds; // [rsp+48h] [rbp-C0h] BYREF
char v17[184]; // [rsp+50h] [rbp-B8h] BYREF

gettimeofday(&v12, 0LL);
srand(LODWORD(v12.tv_sec) ^ LODWORD(v12.tv_usec));
v3 = 1;
if ( (unsigned int)read_args(argc, (char **)argv) )
{
lineclient_construct(v17, &fds, 4096LL, 4096LL);
v3 = lineclient_resolve(v17, host, (unsigned __int16)port);
if ( v3 )
{
fwrite("Error resolving host.\n", 1uLL, 0x16uLL, stderr);
lineclient_destruct(v17);
v3 = 2;
}
else
{
do
{
if ( poll(&fds, 1uLL, 250) < 0 )
{
perror("echo_test poll");
lineclient_destruct(v17);
return 3;
}
lineclient_operate(v17);
v5 = lineclient_conn(v17);
if ( (unsigned int)pollconn_err_connect(v5) != 115 && (unsigned int)pollconn_err_connect(v5)
|| (unsigned int)pollconn_err_send(v5) )
{
fwrite("Error connecting socket.\n", 1uLL, 0x19uLL, stderr);
v6 = pollconn_err_connect(v5);
v7 = strerror(v6);
fprintf(stderr, "err_connect=%s\n", v7);
v8 = pollconn_err_send(v5);
v9 = strerror(v8);
fprintf(stderr, "err_send=%s\n", v9);
lineclient_destruct(v17);
return 4;
}
}
while ( !(unsigned int)pollconn_ready(v5) );
start_time = time(0LL);
test_strings_construct((__int64)v15, num_lines, min_size, max_size, min_wait, max_wait);
while ( 1 )
{
if ( num_lines <= 0 )
{
test_strings_destruct((__int64)v15);
lineclient_destruct(v17);
return v3;
}
if ( poll(&fds, 1uLL, 250) < 0 )
{
perror("echo_test poll");
lineclient_destruct(v17);
test_strings_destruct((__int64)v15);
return 5;
}
lineclient_operate(v17);
v10 = test_strings_send((__int64)v15, &v14, &v13);
if ( v10 == 1 )
break;
if ( v10 < 0 )
lineclient_shutdown(v17);
LABEL_25:
while ( 1 )
{
v11 = lineclient_getevent(v17);
if ( !v11 )
break;
if ( v11 == 4 )
{
lineclient_getline(v17, &v14, &v13);
if ( !(unsigned int)test_strings_received((__int64)v15, v14, v13) )
{
lineclient_destruct(v17);
test_strings_destruct((__int64)v15);
return 7;
}
--num_lines;
}
else if ( v11 == 1 && num_lines > 0 )
{
test_strings_destruct((__int64)v15);
lineclient_destruct(v17);
return 8;
}
lineclient_dequeue(v17);
}
}
if ( (int)lineclient_enqueue(v17, v14, v13) >= 0 )
{
test_strings_sent(v15);
goto LABEL_25;
}
fwrite("Error enqueuing to send buffers.\n", 1uLL, 0x21uLL, stderr);
lineclient_destruct(v17);
test_strings_destruct((__int64)v15);
v3 = 6;
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
__int64 v5; // rbx
int v6; // eax
char *v7; // rax
int v8; // eax
char *v9; // rax
int v10; // eax
int v11; // eax
struct timeval v12; // [rsp+0h] [rbp-108h] BYREF
unsigned int v13; // [rsp+14h] [rbp-F4h] BYREF
__int64 v14; // [rsp+18h] [rbp-F0h] BYREF
_DWORD v15[10]; // [rsp+20h] [rbp-E8h] BYREF
struct pollfd fds; // [rsp+48h] [rbp-C0h] BYREF
char v17[184]; // [rsp+50h] [rbp-B8h] BYREF

gettimeofday(&v12, 0LL);
srand(LODWORD(v12.tv_sec) ^ LODWORD(v12.tv_usec));
v3 = 1;
if ( (unsigned int)func8(argc, (char **)argv) )
{
lineclient_construct(v17, &fds, 4096LL, 4096LL);
v3 = lineclient_resolve(v17, host, (unsigned __int16)port);
if ( v3 )
{
fwrite("Error resolving host.\n", 1uLL, 0x16uLL, stderr);
lineclient_destruct(v17);
v3 = 2;
}
else
{
do
{
if ( poll(&fds, 1uLL, 250) < 0 )
{
perror("echo_test poll");
lineclient_destruct(v17);
return 3;
}
lineclient_operate(v17);
v5 = lineclient_conn(v17);
if ( (unsigned int)pollconn_err_connect(v5) != 115 && (unsigned int)pollconn_err_connect(v5)
|| (unsigned int)pollconn_err_send(v5) )
{
fwrite("Error connecting socket.\n", 1uLL, 0x19uLL, stderr);
v6 = pollconn_err_connect(v5);
v7 = strerror(v6);
fprintf(stderr, "err_connect=%s\n", v7);
v8 = pollconn_err_send(v5);
v9 = strerror(v8);
fprintf(stderr, "err_send=%s\n", v9);
lineclient_destruct(v17);
return 4;
}
}
while ( !(unsigned int)pollconn_ready(v5) );
start_time = time(0LL);
func5((__int64)v15, num_lines, min_size, max_size, min_wait, max_wait);
while ( 1 )
{
if ( num_lines <= 0 )
{
func0((__int64)v15);
lineclient_destruct(v17);
return v3;
}
if ( poll(&fds, 1uLL, 250) < 0 )
{
perror("echo_test poll");
lineclient_destruct(v17);
func0((__int64)v15);
return 5;
}
lineclient_operate(v17);
v10 = func1((__int64)v15, &v14, &v13);
if ( v10 == 1 )
break;
if ( v10 < 0 )
lineclient_shutdown(v17);
LABEL_25:
while ( 1 )
{
v11 = lineclient_getevent(v17);
if ( !v11 )
break;
if ( v11 == 4 )
{
lineclient_getline(v17, &v14, &v13);
if ( !(unsigned int)func3((__int64)v15, v14, v13) )
{
lineclient_destruct(v17);
func0((__int64)v15);
return 7;
}
--num_lines;
}
else if ( v11 == 1 && num_lines > 0 )
{
func0((__int64)v15);
lineclient_destruct(v17);
return 8;
}
lineclient_dequeue(v17);
}
}
if ( (int)lineclient_enqueue(v17, v14, v13) >= 0 )
{
func2(v15);
goto LABEL_25;
}
fwrite("Error enqueuing to send buffers.\n", 1uLL, 0x21uLL, stderr);
lineclient_destruct(v17);
func0((__int64)v15);
v3 = 6;
}
}
return v3;
}

original_code:
int
main(int argc, char *argv[])
{
    lineclient client;
    lineclient_event e;
    const pollconn *c;
    struct pollfd pfd;
    test_strings ts;
    const char *buf;
    int len, ret;
    struct timeval init_time;
    gettimeofday(&init_time, ((void *)0));
    srand(init_time.tv_sec^init_time.tv_usec);
    if(!read_args(argc, argv))
        return 1;
    lineclient_construct(&client, &pfd, sendbuf_len, recvbuf_len);
    if(lineclient_resolve(&client, host, port)!=0)
    {
        fprintf(stderr, "Error resolving host.\n");
        lineclient_destruct(&client);
        return 2;
    }
    while(1)
    {
        if(poll(&pfd, 1, 250)<0)
        {
            perror("echo_test poll");
            lineclient_destruct(&client);
            return 3;
        }
        lineclient_operate(&client);
        c=lineclient_conn(&client);
        if((pollconn_err_connect(c)!=115 &&
            pollconn_err_connect(c)!=0) ||
           pollconn_err_send(c)!=0)
        {
            fprintf(stderr, "Error connecting socket.\n");
            fprintf(stderr, "err_connect=%s\n",
                    strerror(pollconn_err_connect(c)));
            fprintf(stderr, "err_send=%s\n",
                    strerror(pollconn_err_send(c)));
            lineclient_destruct(&client);
            return 4;
        }
        if(pollconn_ready(c))
            break;
    }
    start_time=time(((void *)0));
    test_strings_construct(&ts, num_lines,
                           min_size, max_size, min_wait, max_wait);
    while(num_lines>0)
    {
        if(poll(&pfd, 1, 250)<0)
        {
            perror("echo_test poll");
            lineclient_destruct(&client);
            test_strings_destruct(&ts);
            return 5;
        }
        lineclient_operate(&client);
        if((ret=test_strings_send(&ts, &buf, &len))==1)
        {
            if(lineclient_enqueue(&client, buf, len)<0)
            {
                fprintf(stderr, "Error enqueuing to send buffers.\n");
                lineclient_destruct(&client);
                test_strings_destruct(&ts);
                return 6;
            }
            test_strings_sent(&ts);
        }
        else if(ret<0)
            lineclient_shutdown(&client);
        while((e=lineclient_getevent(&client))!=LINECLIENT_NULLEVENT)
        {
            if(e==LINECLIENT_LINE)
            {
                lineclient_getline(&client, &buf, &len);
                if(!test_strings_received(&ts, buf, len))
                {
                    lineclient_destruct(&client);
                    test_strings_destruct(&ts);
                    return 7;
                }
                num_lines--;
            }
            else if(e==LINECLIENT_CLOSED)
            {
                if(num_lines>0)
                {
                    test_strings_destruct(&ts);
                    lineclient_destruct(&client);
                    return 8;
                }
            }
            lineclient_dequeue(&client);
        }
    }
    test_strings_destruct(&ts);
    lineclient_destruct(&client);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct lineclient client;
    enum lineclient_event e;
    struct pollconn *c;
    struct pollfd pfd;
    struct test_strings ts;
    char *buf;
    int len; int ret;
    struct timeval init_time;
    gettimeofday(&init_time, ((void *)0));
    srand(init_time.tv_sec^init_time.tv_usec);
    if(!read_args(argc, argv))
        return 1;
    lineclient_construct(&client, &pfd, sendbuf_len, recvbuf_len);
    if(lineclient_resolve(&client, host, port)!=0)
    {
        fprintf(stderr, "Error resolving host.\n");
        lineclient_destruct(&client);
        return 2;
    }
    while(1)
    {
        if(poll(&pfd, 1, 250)<0)
        {
            perror("echo_test poll");
            lineclient_destruct(&client);
            return 3;
        }
        lineclient_operate(&client);
        c=lineclient_conn(&client);
        if((pollconn_err_connect(c)!=115 &&
            pollconn_err_connect(c)!=0) ||
           pollconn_err_send(c)!=0)
        {
            fprintf(stderr, "Error connecting socket.\n");
            fprintf(stderr, "err_connect=%s\n",
                    strerror(pollconn_err_connect(c)));
            fprintf(stderr, "err_send=%s\n",
                    strerror(pollconn_err_send(c)));
            lineclient_destruct(&client);
            return 4;
        }
        if(pollconn_ready(c))
            break;
    }
    start_time=time(((void *)0));
    test_strings_construct(&ts, num_lines,
                           min_size, max_size, min_wait, max_wait);
    while(num_lines>0)
    {
        if(poll(&pfd, 1, 250)<0)
        {
            perror("echo_test poll");
            lineclient_destruct(&client);
            test_strings_destruct(&ts);
            return 5;
        }
        lineclient_operate(&client);
        if((ret=test_strings_send(&ts, &buf, &len))==1)
        {
            if(lineclient_enqueue(&client, buf, len)<0)
            {
                fprintf(stderr, "Error enqueuing to send buffers.\n");
                lineclient_destruct(&client);
                test_strings_destruct(&ts);
                return 6;
            }
            test_strings_sent(&ts);
        }
        else if(ret<0)
            lineclient_shutdown(&client);
        while((e=lineclient_getevent(&client))!=LINECLIENT_NULLEVENT)
        {
            if(e==LINECLIENT_LINE)
            {
                lineclient_getline(&client, &buf, &len);
                if(!test_strings_received(&ts, buf, len))
                {
                    lineclient_destruct(&client);
                    test_strings_destruct(&ts);
                    return 7;
                }
                num_lines--;
            }
            else if(e==LINECLIENT_CLOSED)
            {
                if(num_lines>0)
                {
                    test_strings_destruct(&ts);
                    lineclient_destruct(&client);
                    return 8;
                }
            }
            lineclient_dequeue(&client);
        }
    }
    test_strings_destruct(&ts);
    lineclient_destruct(&client);
    return 0;
}

variable_types: struct lineclient { struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } c; int next_pos; int closed; int shutdown; int error; char delim; }

variable_types: struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *

variable_types: struct test_strings { int num_strings; struct test_string { int len; char *str; int time; } *strings; char *buf; int next_send; int next_receive; }

variable_types: ret int

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

user_defined_types: enum lineclient_event { LINECLIENT_NULLEVENT=0, LINECLIENT_CLOSED=1, LINECLIENT_ERROR=2, LINECLIENT_SHUTDOWN=3, LINECLIENT_LINE=4 }


decompiled_code:
int __fastcall print_pollset_state(__int64 a1)
{
__int64 v1; // rbx
unsigned int v2; // eax

printf("handles: ");
if ( *(int *)(a1 + 56) > 0 )
{
v1 = 0LL;
do
{
v2 = pollset_connhandle(a1, *(_QWORD *)(*(_QWORD *)(a1 + 48) + 8 * v1));
printf("%d ", v2);
++v1;
}
while ( *(_DWORD *)(a1 + 56) > (int)v1 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
unsigned int v2; // eax

printf("handles: ");
if ( *(int *)(a1 + 56) > 0 )
{
v1 = 0LL;
do
{
v2 = pollset_connhandle(a1, *(_QWORD *)(*(_QWORD *)(a1 + 48) + 8 * v1));
printf("%d ", v2);
++v1;
}
while ( *(_DWORD *)(a1 + 56) > (int)v1 );
}
return putchar(10);
}

original_code:
void
print_pollset_state(const pollset *s)
{
    int i;
    printf("handles: ");
    for(i=0; i<s->handles_len; i++)
        printf("%d ", pollset_connhandle(s, s->handles[i]));
    printf("\n");
}

canonical_original_code:
void print_pollset_state(struct pollset *s)
{
    int i;
    printf("handles: ");
    for(i=0; i<s->handles_len; i++)
        printf("%d ", pollset_connhandle(s, s->handles[i]));
    printf("\n");
}

variable_types: struct pollset { struct pollpool { int len; struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *pollconns; struct pollfd *ufds; int *freelist; int freelist_head; int freelist_len; int sendbuf_len; int recvbuf_len; } pp; struct pollconn **handles; int handles_len; } *

user_defined_types: struct pollset { struct pollpool pp; struct pollconn **handles; int handles_len; }

user_defined_types: struct pollpool { int len; struct pollconn *pollconns; struct pollfd *ufds; int *freelist; int freelist_head; int freelist_len; int sendbuf_len; int recvbuf_len; }


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

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

original_code:
int handle_cmd_quit(char *cmd)
{
    return 0;
}

variable_types:
cmd
char *


decompiled_code:
__int64 __fastcall handle_cmd(const char *a1)
{
const char *v1; // rbx
int v2; // eax
bool v3; // cf
bool v4; // zf
unsigned int v5; // ecx
const char *v6; // rax
unsigned int v7; // edx
const char *v8; // rdx
const char *v9; // rdi
__int64 v10; // rcx
const char *v11; // rsi
bool v12; // al
bool v13; // cf
bool v14; // zf
const char *v15; // rdi
__int64 v16; // rcx
const char *v17; // rsi
char v18; // al
bool v19; // cf
bool v20; // zf
const char *v21; // rdi
__int64 v22; // rcx
const char *v23; // rsi
char v24; // al
bool v25; // cf
bool v26; // zf
const char *v27; // rdi
__int64 v28; // rcx

v1 = a1;
v2 = strlen(a1);
v3 = 0;
v4 = v2 == 0;
if ( v2 <= 0 )
{
v7 = 0;
}
else
{
v5 = v2;
v6 = a1;
v7 = 0;
while ( 1 )
{
v3 = *v6 < 0x20u;
v4 = *v6 == 32;
if ( *v6 == 32 )
break;
++v7;
++v6;
v3 = v5 < v7;
v4 = v5 == v7;
if ( v5 == v7 )
goto LABEL_7;
}
*v6 = 0;
}
LABEL_7:
v8 = &a1[v7 + 1];
v9 = "add";
v10 = 4LL;
v11 = v1;
do
{
if ( !v10 )
break;
v3 = *v11 < (unsigned int)*v9;
v4 = *v11++ == *v9++;
--v10;
}
while ( v4 );
if ( (!v3 && !v4) == v3 )
return handle_cmd_add(v8, v11);
v12 = strcmp(v1, "del") != 0;
v13 = 0;
v14 = !v12;
if ( !v12 )
return handle_cmd_del(v8);
v15 = "print";
v16 = 6LL;
v17 = v1;
do
{
if ( !v16 )
break;
v13 = *v17 < (unsigned int)*v15;
v14 = *v17++ == *v15++;
--v16;
}
while ( v14 );
v18 = (!v13 && !v14) - v13;
v19 = 0;
v20 = v18 == 0;
if ( !v18 )
return handle_cmd_print(v8, v17);
v21 = "quit";
v22 = 5LL;
v23 = v1;
do
{
if ( !v22 )
break;
v19 = *v23 < (unsigned int)*v21;
v20 = *v23++ == *v21++;
--v22;
}
while ( v20 );
v24 = (!v19 && !v20) - v19;
v25 = 0;
v26 = v24 == 0;
if ( !v24 )
return handle_cmd_quit(v8, v23);
v27 = "exit";
v28 = 5LL;
v23 = v1;
do
{
if ( !v28 )
break;
v25 = *v23 < (unsigned int)*v27;
v26 = *v23++ == *v27++;
--v28;
}
while ( v26 );
if ( (!v25 && !v26) == v25 )
return handle_cmd_quit(v8, v23);
puts("Unrecognized command.");
return 1LL;
}

original_code:
int handle_cmd(char *cmd)
{
    int i, len;
    len=strlen(cmd);
    const char *args;
    for(i=0; i<len; i++)
    {
        if(cmd[i]==' ')
        {
            cmd[i]='\0';
            break;
        }
    }
    args=cmd+i+1;
    if(strcmp(cmd, "add")==0)
        return handle_cmd_add(cmd+i+1);
    else if(strcmp(cmd, "del")==0)
        return handle_cmd_del(cmd+i+1);
    else if(strcmp(cmd, "print")==0)
        return handle_cmd_print(cmd+i+1);
    else if(strcmp(cmd, "quit")==0 || strcmp(cmd, "exit")==0)
        return handle_cmd_quit(cmd+i+1);
    else
    {
        printf("Unrecognized command.\n");
        return 1;
    }
}

canonical_original_code:
int handle_cmd(char *cmd)
{
    int i; int len;
    len=strlen(cmd);
    char *args;
    for(i=0; i<len; i++)
    {
        if(cmd[i]==' ')
        {
            cmd[i]='\0';
            break;
        }
    }
    args=cmd+i+1;
    if(strcmp(cmd, "add")==0)
        return handle_cmd_add(cmd+i+1);
    else if(strcmp(cmd, "del")==0)
        return handle_cmd_del(cmd+i+1);
    else if(strcmp(cmd, "print")==0)
        return handle_cmd_print(cmd+i+1);
    else if(strcmp(cmd, "quit")==0 || strcmp(cmd, "exit")==0)
        return handle_cmd_quit(cmd+i+1);
    else
    {
        printf("Unrecognized command.\n");
        return 1;
    }
}

variable_types: len int

variable_types:
args
char *


decompiled_code:
__int64 __fastcall linepiper_construct(__int64 a1, int a2, int a3, int a4, int a5)
{
__int64 v6; // rsi
__int64 result; // rax

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = a3;
if ( a4 < 0 )
a4 = 0;
tcpbuf_construct(a1 + 8, (unsigned int)a4);
v6 = 0LL;
if ( a5 >= 0 )
v6 = (unsigned int)a5;
result = tcpbuf_construct(a1 + 24, v6);
*(_DWORD *)(a1 + 40) = -1;
*(_BYTE *)(a1 + 44) = 10;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3, int a4, int a5)
{
__int64 v6; // rsi
__int64 result; // rax

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = a3;
if ( a4 < 0 )
a4 = 0;
tcpbuf_construct(a1 + 8, (unsigned int)a4);
v6 = 0LL;
if ( a5 >= 0 )
v6 = (unsigned int)a5;
result = tcpbuf_construct(a1 + 24, v6);
*(_DWORD *)(a1 + 40) = -1;
*(_BYTE *)(a1 + 44) = 10;
return result;
}

original_code:
void
linepiper_construct(linepiper *lp, int fd_in, int fd_out,
                    int inbuf_len, int outbuf_len)
{
    lp->fd_in=fd_in;
    lp->fd_out=fd_out;
    if(inbuf_len<0)
        inbuf_len=0;
    if(outbuf_len<0)
        outbuf_len=0;
    tcpbuf_construct(&lp->buf_in, inbuf_len);
    tcpbuf_construct(&lp->buf_out, outbuf_len);
    lp->in_pos=-1;
    lp->delim='\n';
}

canonical_original_code:
void linepiper_construct(struct linepiper *lp, int fd_in, int fd_out, int inbuf_len, int outbuf_len)
{
    lp->fd_in=fd_in;
    lp->fd_out=fd_out;
    if(inbuf_len<0)
        inbuf_len=0;
    if(outbuf_len<0)
        outbuf_len=0;
    tcpbuf_construct(&lp->buf_in, inbuf_len);
    tcpbuf_construct(&lp->buf_out, outbuf_len);
    lp->in_pos=-1;
    lp->delim='\n';
}

variable_types: struct linepiper { int fd_in; int fd_out; struct tcpbuf { int len; int pos; unsigned char *data; } buf_in; struct tcpbuf buf_out; int in_pos; char delim; } *

user_defined_types: struct linepiper { int fd_in; int fd_out; struct tcpbuf buf_in; struct tcpbuf buf_out; int in_pos; char delim; }


decompiled_code:
__int64 __fastcall linepiper_destruct(_DWORD *a1)
{
*a1 = -1;
a1[1] = -1;
tcpbuf_destruct(a1 + 2);
return tcpbuf_destruct(a1 + 6);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
*a1 = -1;
a1[1] = -1;
tcpbuf_destruct(a1 + 2);
return tcpbuf_destruct(a1 + 6);
}

original_code:
void
linepiper_destruct(linepiper *lp)
{
    lp->fd_in=-1;
    lp->fd_out=-1;
    tcpbuf_destruct(&lp->buf_in);
    tcpbuf_destruct(&lp->buf_out);
}

canonical_original_code:
void linepiper_destruct(struct linepiper *lp)
{
    lp->fd_in=-1;
    lp->fd_out=-1;
    tcpbuf_destruct(&lp->buf_in);
    tcpbuf_destruct(&lp->buf_out);
}


decompiled_code:
__int64 __fastcall linepiper_read(int *a1)
{
unsigned int v1; // eax
int v2; // er12
int v3; // eax
unsigned int v4; // ebx
void *buf; // [rsp+8h] [rbp-30h] BYREF

v1 = tcpbuf_space(a1 + 2);
v2 = tcpbuf_alloc(a1 + 2, v1, &buf);
v3 = read(*a1, buf, v2);
v4 = v3;
if ( v3 >= 0 )
{
tcpbuf_dealloc(a1 + 2, (unsigned int)(v2 - v3));
advance_in_pos((__int64)a1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1)
{
unsigned int v1; // eax
int v2; // er12
int v3; // eax
unsigned int v4; // ebx
void *buf; // [rsp+8h] [rbp-30h] BYREF

v1 = tcpbuf_space(a1 + 2);
v2 = tcpbuf_alloc(a1 + 2, v1, &buf);
v3 = read(*a1, buf, v2);
v4 = v3;
if ( v3 >= 0 )
{
tcpbuf_dealloc(a1 + 2, (unsigned int)(v2 - v3));
func0((__int64)a1);
}
return v4;
}

original_code:
int
linepiper_read(linepiper *lp)
{
    int ret, buflen;
    u_char *buf;
    buflen=tcpbuf_alloc(&lp->buf_in, tcpbuf_space(&lp->buf_in), &buf);
    ret=read(lp->fd_in, buf, buflen);
    if(ret>=0)
    {
        tcpbuf_dealloc(&lp->buf_in, buflen-ret);
        advance_in_pos(lp);
    }
    return ret;
}

canonical_original_code:
int linepiper_read(struct linepiper *lp)
{
    int ret; int buflen;
    unsigned char *buf;
    buflen=tcpbuf_alloc(&lp->buf_in, tcpbuf_space(&lp->buf_in), &buf);
    ret=read(lp->fd_in, buf, buflen);
    if(ret>=0)
    {
        tcpbuf_dealloc(&lp->buf_in, buflen-ret);
        advance_in_pos(lp);
    }
    return ret;
}

variable_types: buflen int

variable_types:
buf
unsigned char *


decompiled_code:
__int64 __fastcall linepiper_write(__int64 a1)
{
int v1; // ebx
const void *v2; // rax
int v3; // eax
unsigned int v4; // ebx

v1 = tcpbuf_filled(a1 + 24);
v2 = (const void *)tcpbuf_data(a1 + 24);
v3 = write(*(_DWORD *)(a1 + 4), v2, v1);
v4 = v3;
if ( v3 > 0 )
tcpbuf_dequeue(a1 + 24, (unsigned int)v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v1; // ebx
const void *v2; // rax
int v3; // eax
unsigned int v4; // ebx

v1 = tcpbuf_filled(a1 + 24);
v2 = (const void *)tcpbuf_data(a1 + 24);
v3 = write(*(_DWORD *)(a1 + 4), v2, v1);
v4 = v3;
if ( v3 > 0 )
tcpbuf_dequeue(a1 + 24, (unsigned int)v3);
return v4;
}

original_code:
int
linepiper_write(linepiper *lp)
{
    int ret;
    ret=write(lp->fd_out, tcpbuf_data(&lp->buf_out),
              tcpbuf_filled(&lp->buf_out));
    if(ret>0)
        tcpbuf_dequeue(&lp->buf_out, ret);
    return ret;
}

canonical_original_code:
int linepiper_write(struct linepiper *lp)
{
    int ret;
    ret=write(lp->fd_out, tcpbuf_data(&lp->buf_out),
              tcpbuf_filled(&lp->buf_out));
    if(ret>0)
        tcpbuf_dequeue(&lp->buf_out, ret);
    return ret;
}


decompiled_code:
__int64 __fastcall linepiper_enqueue(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // r13

if ( a3 < 0 )
return 0xFFFFFFFFLL;
v4 = a1 + 24;
if ( (int)tcpbuf_space(a1 + 24) <= a3 )
return 0xFFFFFFFFLL;
tcpbuf_enqueue(v4, (unsigned int)a3, a2);
tcpbuf_enqueue(v4, 1LL, a1 + 44);
return (unsigned int)(a3 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // r13

if ( a3 < 0 )
return 0xFFFFFFFFLL;
v4 = a1 + 24;
if ( (int)tcpbuf_space(a1 + 24) <= a3 )
return 0xFFFFFFFFLL;
tcpbuf_enqueue(v4, (unsigned int)a3, a2);
tcpbuf_enqueue(v4, 1LL, a1 + 44);
return (unsigned int)(a3 + 1);
}

original_code:
int
linepiper_enqueue(linepiper *lp, const char *data, int len)
{
    if(len<0)
        return -1;
    if(tcpbuf_space(&lp->buf_out)<len+1)
        return -1;
    tcpbuf_enqueue(&lp->buf_out, len, data);
    tcpbuf_enqueue(&lp->buf_out, 1, &lp->delim);
    return len+1;
}

canonical_original_code:
int linepiper_enqueue(struct linepiper *lp, char *data, int len)
{
    if(len<0)
        return -1;
    if(tcpbuf_space(&lp->buf_out)<len+1)
        return -1;
    tcpbuf_enqueue(&lp->buf_out, len, data);
    tcpbuf_enqueue(&lp->buf_out, 1, &lp->delim);
    return len+1;
}

variable_types:
data
char *


decompiled_code:
__int64 __fastcall linepiper_dequeue(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
int v3; // esi

result = 0LL;
if ( *(int *)(a1 + 40) >= 0 )
{
v2 = tcpbuf_data(a1 + 8);
v3 = *(_DWORD *)(a1 + 40);
result = 0LL;
if ( *(_BYTE *)(v2 + v3) == *(_BYTE *)(a1 + 44) )
{
tcpbuf_dequeue(a1 + 8, (unsigned int)(v3 + 1));
*(_DWORD *)(a1 + 40) = -1;
advance_in_pos(a1);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rcx
int v3; // esi

result = 0LL;
if ( *(int *)(a1 + 40) >= 0 )
{
v2 = tcpbuf_data(a1 + 8);
v3 = *(_DWORD *)(a1 + 40);
result = 0LL;
if ( *(_BYTE *)(v2 + v3) == *(_BYTE *)(a1 + 44) )
{
tcpbuf_dequeue(a1 + 8, (unsigned int)(v3 + 1));
*(_DWORD *)(a1 + 40) = -1;
func0(a1);
result = 1LL;
}
}
return result;
}

original_code:
int
linepiper_dequeue(linepiper *lp)
{
    const char *t;
    if(lp->in_pos<0)
        return 0;
    t=tcpbuf_data(&lp->buf_in);
    if(t[lp->in_pos]!=lp->delim)
        return 0;
    tcpbuf_dequeue(&lp->buf_in, (lp->in_pos)+1);
    lp->in_pos=-1;
    advance_in_pos(lp);
    return 1;
}

canonical_original_code:
int linepiper_dequeue(struct linepiper *lp)
{
    char *t;
    if(lp->in_pos<0)
        return 0;
    t=tcpbuf_data(&lp->buf_in);
    if(t[lp->in_pos]!=lp->delim)
        return 0;
    tcpbuf_dequeue(&lp->buf_in, (lp->in_pos)+1);
    lp->in_pos=-1;
    advance_in_pos(lp);
    return 1;
}

variable_types:
t
char *


decompiled_code:
__int64 __fastcall linepiper_line(__int64 a1, __int64 *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v5; // rdx

result = 0LL;
if ( *(int *)(a1 + 40) >= 0 )
{
v5 = tcpbuf_data(a1 + 8);
result = 0LL;
if ( *(_BYTE *)(v5 + *(int *)(a1 + 40)) == *(_BYTE *)(a1 + 44) )
{
if ( a2 )
*a2 = v5;
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 40);
result = 1LL;
}
}
}
return result;
}

original_code:
int
linepiper_line(linepiper *lp, const char **data, int *len)
{
    const char *t;
    if(lp->in_pos<0)
        return 0;
    t=tcpbuf_data(&lp->buf_in);
    if(t[lp->in_pos]!=lp->delim)
        return 0;
    if(data)
        *data=t;
    if(len)
        *len=lp->in_pos;
    return 1;
}

canonical_original_code:
int linepiper_line(struct linepiper *lp, char **data, int *len)
{
    char *t;
    if(lp->in_pos<0)
        return 0;
    t=tcpbuf_data(&lp->buf_in);
    if(t[lp->in_pos]!=lp->delim)
        return 0;
    if(data)
        *data=t;
    if(len)
        *len=lp->in_pos;
    return 1;
}

variable_types:
data
char **

variable_types:
len
int *


decompiled_code:
int __fastcall print_pollpool_state(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // eax
int v3; // ebp

printf("pollconns: ");
if ( *(int *)a1 > 0 )
{
v1 = 0LL;
do
{
v2 = pollconn_sock(*(_QWORD *)(a1 + 8) + (v1 << 7));
printf("%d ", v2);
++v1;
}
while ( *(_DWORD *)a1 > (int)v1 );
}
putchar(10);
printf("freelist: ");
if ( *(int *)(a1 + 36) > 0 )
{
v3 = 0;
do
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL * ((*(_DWORD *)(a1 + 32) + v3++) % *(_DWORD *)a1)));
while ( *(_DWORD *)(a1 + 36) > v3 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // eax
int v3; // ebp

printf("pollconns: ");
if ( *(int *)a1 > 0 )
{
v1 = 0LL;
do
{
v2 = pollconn_sock(*(_QWORD *)(a1 + 8) + (v1 << 7));
printf("%d ", v2);
++v1;
}
while ( *(_DWORD *)a1 > (int)v1 );
}
putchar(10);
printf("freelist: ");
if ( *(int *)(a1 + 36) > 0 )
{
v3 = 0;
do
printf("%d ", *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL * ((*(_DWORD *)(a1 + 32) + v3++) % *(_DWORD *)a1)));
while ( *(_DWORD *)(a1 + 36) > v3 );
}
return putchar(10);
}

original_code:
void
print_pollpool_state(const pollpool *p)
{
    int i;
    printf("pollconns: ");
    for(i=0; i<p->len; i++)
        printf("%d ", pollconn_sock(p->pollconns+i));
    printf("\n");
    printf("freelist: ");
    for(i=0; i<p->freelist_len; i++)
    {
        printf("%d ", p->freelist[(p->freelist_head+i)%p->len]);
    }
    printf("\n");
}

canonical_original_code:
void print_pollpool_state(struct pollpool *p)
{
    int i;
    printf("pollconns: ");
    for(i=0; i<p->len; i++)
        printf("%d ", pollconn_sock(p->pollconns+i));
    printf("\n");
    printf("freelist: ");
    for(i=0; i<p->freelist_len; i++)
    {
        printf("%d ", p->freelist[(p->freelist_head+i)%p->len]);
    }
    printf("\n");
}

variable_types: struct pollpool { int len; struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *pollconns; struct pollfd *ufds; int *freelist; int freelist_head; int freelist_len; int sendbuf_len; int recvbuf_len; } *

variable_types: i int


canonical_original_code:
int handle_cmd_quit(char *cmd)
{
    return 0;
}


decompiled_code:
__int64 daemon_init()
{
__pid_t v0; // eax
unsigned int v1; // ebx

v0 = fork();
if ( v0 < 0 )
return (unsigned int)-1;
v1 = v0;
if ( v0 )
exit(0);
setsid();
chdir("/");
umask(0);
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
__pid_t v0; // eax
unsigned int v1; // ebx

v0 = fork();
if ( v0 < 0 )
return (unsigned int)-1;
v1 = v0;
if ( v0 )
exit(0);
setsid();
chdir("/");
umask(0);
return v1;
}

original_code:
int
daemon_init(void)
{
    pid_t pid;
    if((pid=fork())<0)
        return -1;
    if(pid!=0)
        exit(0);
    setsid();
    chdir("/");
    umask(0);
    return 0;
}

canonical_original_code:
int daemon_init()
{
    int pid;
    if((pid=fork())<0)
        return -1;
    if(pid!=0)
        exit(0);
    setsid();
    chdir("/");
    umask(0);
    return 0;
}

variable_types: pid int


decompiled_code:
__int64 __fastcall pipe_std_exec(int *a1, _DWORD *a2, const char *a3, char *const *a4)
{
unsigned int v6; // ebx
__pid_t v7; // eax
int v8; // eax
int v9; // eax
int v11; // eax
int v12; // eax
int fd; // [rsp+0h] [rbp-38h] BYREF
int v14; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

v6 = pipe(pipedes);
if ( v6 != -1 )
{
v6 = pipe(&fd);
if ( v6 == -1 )
{
close(pipedes[0]);
close(pipedes[1]);
}
else
{
v7 = fork();
v6 = v7;
if ( v7 == -1 )
{
close(pipedes[0]);
close(pipedes[1]);
close(fd);
close(v14);
}
else if ( !v7
&& (close(fd),
close(pipedes[1]),
v8 = fileno(stdout),
dup2(v14, v8),
v9 = fileno(stdin),
dup2(pipedes[0], v9),
execv(a3, a4) == -1) )
{
close(v14);
close(pipedes[0]);
v11 = fileno(stdout);
close(v11);
v12 = fileno(stdin);
close(v12);
v6 = -1;
}
else
{
*a1 = fd;
*a2 = pipedes[1];
close(v14);
close(pipedes[0]);
}
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, _DWORD *a2, const char *a3, char *const *a4)
{
unsigned int v6; // ebx
__pid_t v7; // eax
int v8; // eax
int v9; // eax
int v11; // eax
int v12; // eax
int fd; // [rsp+0h] [rbp-38h] BYREF
int v14; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

v6 = pipe(pipedes);
if ( v6 != -1 )
{
v6 = pipe(&fd);
if ( v6 == -1 )
{
close(pipedes[0]);
close(pipedes[1]);
}
else
{
v7 = fork();
v6 = v7;
if ( v7 == -1 )
{
close(pipedes[0]);
close(pipedes[1]);
close(fd);
close(v14);
}
else if ( !v7
&& (close(fd),
close(pipedes[1]),
v8 = fileno(stdout),
dup2(v14, v8),
v9 = fileno(stdin),
dup2(pipedes[0], v9),
execv(a3, a4) == -1) )
{
close(v14);
close(pipedes[0]);
v11 = fileno(stdout);
close(v11);
v12 = fileno(stdin);
close(v12);
v6 = -1;
}
else
{
*a1 = fd;
*a2 = pipedes[1];
close(v14);
close(pipedes[0]);
}
}
}
return v6;
}

original_code:
pid_t pipe_std_exec(int *fd_stdout, int *fd_stdin,
                    const char *path, char *const argv[])
{
    pid_t ret;
    int in[2];
    int out[2];
    if(pipe(in)==-1)
        return -1;
    if(pipe(out)==-1)
    {
        close(in[0]);
        close(in[1]);
        return -1;
    }
    if((ret=fork())==-1)
    {
        close(in[0]);
        close(in[1]);
        close(out[0]);
        close(out[1]);
        return -1;
    }
    if(ret==0)
    {
        close(out[0]);
        close(in[1]);
        dup2(out[1], fileno(stdout));
        dup2(in[0], fileno(stdin));
        if(execv(path, argv)==-1)
        {
            close(out[1]);
            close(in[0]);
            close(fileno(stdout));
            close(fileno(stdin));
            return -1;
        }
    }
    *fd_stdout=out[0];
    *fd_stdin=in[1];
    close(out[1]);
    close(in[0]);
    return ret;
}

canonical_original_code:
int pipe_std_exec(int *fd_stdout, int *fd_stdin, char *path, char *argv[])
{
    int ret;
    int in[2];
    int out[2];
    if(pipe(in)==-1)
        return -1;
    if(pipe(out)==-1)
    {
        close(in[0]);
        close(in[1]);
        return -1;
    }
    if((ret=fork())==-1)
    {
        close(in[0]);
        close(in[1]);
        close(out[0]);
        close(out[1]);
        return -1;
    }
    if(ret==0)
    {
        close(out[0]);
        close(in[1]);
        dup2(out[1], fileno(stdout));
        dup2(in[0], fileno(stdin));
        if(execv(path, argv)==-1)
        {
            close(out[1]);
            close(in[0]);
            close(fileno(stdout));
            close(fileno(stdin));
            return -1;
        }
    }
    *fd_stdout=out[0];
    *fd_stdin=in[1];
    close(out[1]);
    close(in[0]);
    return ret;
}

variable_types:
path
char *

variable_types:
argv
char *[]

variable_types:
in
int [2]

variable_types:
out
int [2]


decompiled_code:
int print_usage_str()
{
return fprintf(
stderr,
"Usage: pipeserve <-p port (default %d)>\n"
" <-n max clients (default %d)>\n"
" <-s send buffer size (default %d)>\n"
" <-r receive buffer size (default %d)>\n"
" <-i pipe read buffer size (default %d)>\n"
" <-o pipe write buffer size (defaut %d)>\n"
" <-f (run in the foreground)\n"
" [path] args...\n",
7584LL,
1000LL,
4096LL,
4096LL,
0x100000LL,
0x100000LL);
}

canonical_decompiled_code:
int func2()
{
return fprintf(
stderr,
"Usage: pipeserve <-p port (default %d)>\n"
" <-n max clients (default %d)>\n"
" <-s send buffer size (default %d)>\n"
" <-r receive buffer size (default %d)>\n"
" <-i pipe read buffer size (default %d)>\n"
" <-o pipe write buffer size (defaut %d)>\n"
" <-f (run in the foreground)\n"
" [path] args...\n",
7584LL,
1000LL,
4096LL,
4096LL,
0x100000LL,
0x100000LL);
}


decompiled_code:
__int64 __fastcall read_args(int argc, char **argv)
{
__int64 result; // rax
char v3; // al
int v4; // eax
__int64 v5; // rdx
int v6; // ecx

while ( 2 )
{
v3 = getopt(argc, argv, "p:n:s:r:i:o:f");
if ( v3 == -1 )
{
if ( argc - optind <= 0 )
{
print_usage_str();
result = 0LL;
}
else
{
execv_path = argv[optind];
execv_argv = malloc(8LL * (argc - optind + 1));
v4 = optind;
if ( argc <= optind )
{
v6 = 0;
}
else
{
v5 = 0LL;
v6 = 0;
do
{
*(_QWORD *)((char *)execv_argv + v5) = argv[v4];
v4 = optind + 1;
optind = v4;
++v6;
v5 += 8LL;
}
while ( v4 < argc );
}
*((_QWORD *)execv_argv + v6) = 0LL;
result = 1LL;
}
}
else
{
switch ( v3 )
{
case 'f':
foreground = 1;
continue;
case 'i':
inbuf_len = atoi(optarg);
if ( inbuf_len > 0 )
continue;
fwrite("Error: invalid pipe read buffer size.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
break;
case 'n':
max_clients = atoi(optarg);
if ( max_clients > 0 )
continue;
fwrite("Error: invalid number of max clients.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
break;
case 'o':
outbuf_len = atoi(optarg);
if ( outbuf_len > 0 )
continue;
fwrite("Error: invalid pipe out buffer size.\n", 1uLL, 0x25uLL, stderr);
result = 0LL;
break;
case 'p':
listen_port = atoi(optarg);
if ( listen_port > 0 )
continue;
fwrite("Error: invalid port.\n", 1uLL, 0x15uLL, stderr);
result = 0LL;
break;
case 'r':
recvbuf_len = atoi(optarg);
if ( recvbuf_len > 0 )
continue;
fwrite("Error: invalid receive buffer size.\n", 1uLL, 0x24uLL, stderr);
result = 0LL;
break;
case 's':
sendbuf_len = atoi(optarg);
if ( sendbuf_len > 0 )
continue;
fwrite("Error: invalid send buffer size.\n", 1uLL, 0x21uLL, stderr);
result = 0LL;
break;
default:
continue;
}
}
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int argc, char **argv)
{
__int64 result; // rax
char v3; // al
int v4; // eax
__int64 v5; // rdx
int v6; // ecx

while ( 2 )
{
v3 = getopt(argc, argv, "p:n:s:r:i:o:f");
if ( v3 == -1 )
{
if ( argc - optind <= 0 )
{
func2();
result = 0LL;
}
else
{
execv_path = argv[optind];
execv_argv = malloc(8LL * (argc - optind + 1));
v4 = optind;
if ( argc <= optind )
{
v6 = 0;
}
else
{
v5 = 0LL;
v6 = 0;
do
{
*(_QWORD *)((char *)execv_argv + v5) = argv[v4];
v4 = optind + 1;
optind = v4;
++v6;
v5 += 8LL;
}
while ( v4 < argc );
}
*((_QWORD *)execv_argv + v6) = 0LL;
result = 1LL;
}
}
else
{
switch ( v3 )
{
case 'f':
foreground = 1;
continue;
case 'i':
inbuf_len = atoi(optarg);
if ( inbuf_len > 0 )
continue;
fwrite("Error: invalid pipe read buffer size.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
break;
case 'n':
max_clients = atoi(optarg);
if ( max_clients > 0 )
continue;
fwrite("Error: invalid number of max clients.\n", 1uLL, 0x26uLL, stderr);
result = 0LL;
break;
case 'o':
outbuf_len = atoi(optarg);
if ( outbuf_len > 0 )
continue;
fwrite("Error: invalid pipe out buffer size.\n", 1uLL, 0x25uLL, stderr);
result = 0LL;
break;
case 'p':
listen_port = atoi(optarg);
if ( listen_port > 0 )
continue;
fwrite("Error: invalid port.\n", 1uLL, 0x15uLL, stderr);
result = 0LL;
break;
case 'r':
recvbuf_len = atoi(optarg);
if ( recvbuf_len > 0 )
continue;
fwrite("Error: invalid receive buffer size.\n", 1uLL, 0x24uLL, stderr);
result = 0LL;
break;
case 's':
sendbuf_len = atoi(optarg);
if ( sendbuf_len > 0 )
continue;
fwrite("Error: invalid send buffer size.\n", 1uLL, 0x21uLL, stderr);
result = 0LL;
break;
default:
continue;
}
}
break;
}
return result;
}

original_code:
int
read_args(int argc, char *argv[])
{
    int i;
    char opt;
    while((opt=getopt(argc, argv, "p:n:s:r:i:o:f"))!=-1)
    {
        switch(opt)
        {
        case 'p':
            listen_port=atoi(optarg);
            if(listen_port<=0)
            {
                fprintf(stderr, "Error: invalid port.\n");
                return 0;
            }
            break;
        case 'n':
            max_clients=atoi(optarg);
            if(max_clients<=0)
            {
                fprintf(stderr, "Error: invalid number of max clients.\n");
                return 0;
            }
            break;
        case 's':
            sendbuf_len=atoi(optarg);
            if(sendbuf_len<=0)
            {
                fprintf(stderr, "Error: invalid send buffer size.\n");
                return 0;
            }
            break;
        case 'r':
            recvbuf_len=atoi(optarg);
            if(recvbuf_len<=0)
            {
                fprintf(stderr, "Error: invalid receive buffer size.\n");
                return 0;
            }
            break;
        case 'i':
            inbuf_len=atoi(optarg);
            if(inbuf_len<=0)
            {
                fprintf(stderr, "Error: invalid pipe read buffer size.\n");
                return 0;
            }
            break;
        case 'o':
            outbuf_len=atoi(optarg);
            if(outbuf_len<=0)
            {
                fprintf(stderr, "Error: invalid pipe out buffer size.\n");
                return 0;
            }
            break;
        case 'f':
            foreground=1;
            break;
        }
    }
    if(argc-optind<=0)
    {
        print_usage_str();
        return 0;
    }
    execv_path=argv[optind];
    execv_argv=(char **)malloc(sizeof(char *)*(argc-optind+1));
    i=0;
    while(optind<argc)
    {
        execv_argv[i]=argv[optind];
        optind++;
        i++;
    }
    execv_argv[i]=(char *)((void *)0);
    return 1;
}

variable_types: argc int

variable_types: opt char


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct pollfd *v4; // rbx
const char *v5; // rbp
int v6; // ebp
int v7; // [rsp+8h] [rbp-170h] BYREF
int v8; // [rsp+Ch] [rbp-16Ch] BYREF
char v9[360]; // [rsp+10h] [rbp-168h] BYREF

setvbuf(stdout, 0LL, 2, 0LL);
setvbuf(stdin, 0LL, 2, 0LL);
if ( (unsigned int)read_args(argc, (char **)argv) )
{
v4 = (struct pollfd *)malloc(8LL * (max_clients + 2));
v5 = execv_path;
if ( !strcmp(execv_path, "-") )
{
v8 = fileno(stdin);
v7 = fileno(stdout);
}
else if ( (int)pipe_std_exec(&v8, &v7, v5, (char *const *)execv_argv) < 0 )
{
perror("pipeserve execv");
return 0;
}
free(execv_argv);
v4->fd = v8;
v4->events = 1;
pipeserver_construct(
v9,
&v4[1],
(unsigned int)(max_clients + 1),
(unsigned int)sendbuf_len,
(unsigned int)recvbuf_len);
if ( (unsigned int)pipeserver_listen(v9, (unsigned __int16)listen_port) == -1 )
{
perror("pipeserver bind() and listen()");
pipeserver_destruct(v9);
}
else if ( foreground || (int)daemon_init() >= 0 )
{
v6 = -1;
while ( poll(v4, max_clients + 1, 250) != -1 )
{
pipeserver_operate(v9);
if ( (v4->revents & 1) != 0 )
v6 = pipeserver_read(v9);
while ( (int)pipeserver_process_in(v9) > 0 )
pipeserver_write(v9);
while ( (int)pipeserver_process_out(v9) > 0 )
;
if ( !v6 )
goto LABEL_14;
}
perror("pipeserver poll()");
LABEL_14:
pipeserver_destruct(v9);
free(v4);
}
else
{
perror("pipeserve backgrounding");
}
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct pollfd *v4; // rbx
const char *v5; // rbp
int v6; // ebp
int v7; // [rsp+8h] [rbp-170h] BYREF
int v8; // [rsp+Ch] [rbp-16Ch] BYREF
char v9[360]; // [rsp+10h] [rbp-168h] BYREF

setvbuf(stdout, 0LL, 2, 0LL);
setvbuf(stdin, 0LL, 2, 0LL);
if ( (unsigned int)func3(argc, (char **)argv) )
{
v4 = (struct pollfd *)malloc(8LL * (max_clients + 2));
v5 = execv_path;
if ( !strcmp(execv_path, "-") )
{
v8 = fileno(stdin);
v7 = fileno(stdout);
}
else if ( (int)func1(&v8, &v7, v5, (char *const *)execv_argv) < 0 )
{
perror("pipeserve execv");
return 0;
}
free(execv_argv);
v4->fd = v8;
v4->events = 1;
pipeserver_construct(
v9,
&v4[1],
(unsigned int)(max_clients + 1),
(unsigned int)sendbuf_len,
(unsigned int)recvbuf_len);
if ( (unsigned int)pipeserver_listen(v9, (unsigned __int16)listen_port) == -1 )
{
perror("pipeserver bind() and listen()");
pipeserver_destruct(v9);
}
else if ( foreground || (int)func0() >= 0 )
{
v6 = -1;
while ( poll(v4, max_clients + 1, 250) != -1 )
{
pipeserver_operate(v9);
if ( (v4->revents & 1) != 0 )
v6 = pipeserver_read(v9);
while ( (int)pipeserver_process_in(v9) > 0 )
pipeserver_write(v9);
while ( (int)pipeserver_process_out(v9) > 0 )
;
if ( !v6 )
goto LABEL_14;
}
perror("pipeserver poll()");
LABEL_14:
pipeserver_destruct(v9);
free(v4);
}
else
{
perror("pipeserve backgrounding");
}
}
return 0;
}

original_code:
int
main(int argc, char *argv[])
{
    pipeserver server;
    struct pollfd *ufds;
    int fd_stdout, fd_stdin, ret=-1;
    setvbuf(stdout, ((void *)0), 2, 0);
    setvbuf(stdin, ((void *)0), 2, 0);
    if(!read_args(argc, argv))
        return 0;
    ufds=(struct pollfd*)malloc(sizeof(struct pollfd)*(max_clients+2));
    if(strcmp(execv_path, "-")==0)
    {
        fd_stdout=fileno(stdin);
        fd_stdin=fileno(stdout);
    }
    else if(pipe_std_exec(&fd_stdout, &fd_stdin, execv_path, execv_argv)<0)
    {
        perror("pipeserve execv");
        return 0;
    }
    free(execv_argv);
    ufds[0].fd=fd_stdout;
    ufds[0].events=0x001;
    pipeserver_construct(&server, ufds+1, max_clients+1,
                         sendbuf_len, recvbuf_len,
                         fd_stdout, fd_stdin,
                         inbuf_len, outbuf_len);
    if(pipeserver_listen(&server, listen_port)==-1)
    {
        perror("pipeserver bind() and listen()");
        pipeserver_destruct(&server);
        return 0;
    }
    if(!foreground && daemon_init()<0)
    {
        perror("pipeserve backgrounding");
        return 0;
    }
    while(1)
    {
        if(poll(ufds, max_clients+1, 250)==-1)
        {
            perror("pipeserver poll()");
            break;
        }
        pipeserver_operate(&server);
        if(ufds[0].revents&0x001)
            ret=pipeserver_read(&server);
        while(pipeserver_process_in(&server)>0)
            pipeserver_write(&server);
        while(pipeserver_process_out(&server)>0);
        if(ret==0)
            break;
    }
    pipeserver_destruct(&server);
    free(ufds);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct pipeserver server;
    struct pollfd *ufds;
    int fd_stdout; int fd_stdin; int ret = -1;
    setvbuf(stdout, ((void *)0), 2, 0);
    setvbuf(stdin, ((void *)0), 2, 0);
    if(!read_args(argc, argv))
        return 0;
    ufds=(struct pollfd *)malloc(sizeof(struct pollfd)*(max_clients+2));
    if(strcmp(execv_path, "-")==0)
    {
        fd_stdout=fileno(stdin);
        fd_stdin=fileno(stdout);
    }
    else if(pipe_std_exec(&fd_stdout, &fd_stdin, execv_path, execv_argv)<0)
    {
        perror("pipeserve execv");
        return 0;
    }
    free(execv_argv);
    ufds[0].fd=fd_stdout;
    ufds[0].events=0x001;
    pipeserver_construct(&server, ufds+1, max_clients+1,
                         sendbuf_len, recvbuf_len,
                         fd_stdout, fd_stdin,
                         inbuf_len, outbuf_len);
    if(pipeserver_listen(&server, listen_port)==-1)
    {
        perror("pipeserver bind() and listen()");
        pipeserver_destruct(&server);
        return 0;
    }
    if(!foreground && daemon_init()<0)
    {
        perror("pipeserve backgrounding");
        return 0;
    }
    while(1)
    {
        if(poll(ufds, max_clients+1, 250)==-1)
        {
            perror("pipeserver poll()");
            break;
        }
        pipeserver_operate(&server);
        if(ufds[0].revents&0x001)
            ret=pipeserver_read(&server);
        while(pipeserver_process_in(&server)>0)
            pipeserver_write(&server);
        while(pipeserver_process_out(&server)>0);
        if(ret==0)
            break;
    }
    pipeserver_destruct(&server);
    free(ufds);
    return 0;
}

variable_types: struct pipeserver { struct lineserver { struct pollset { struct pollpool { int len; struct pollconn { struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } s; 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]; } saddr; unsigned int saddr_len; struct sockaddr_in daddr; unsigned int daddr_len; struct pollconn_flags { unsigned int bound; unsigned int connected; unsigned int connecting; unsigned int listening; unsigned int recvdone; unsigned int senddone; unsigned int finsent; unsigned int did_accept; unsigned int did_bind; unsigned int did_close; unsigned int did_connect; unsigned int did_recv; unsigned int did_send; unsigned int did_shutdown; unsigned int saddr_dirty; unsigned int padding; } flags; int ret_accept; int ret_send; int ret_recv; int err_accept; int err_bind; int err_close; int err_connect; int err_recv; int err_send; int err_shutdown; } *pollconns; struct pollfd *ufds; int *freelist; int freelist_head; int freelist_len; int sendbuf_len; int recvbuf_len; } pp; struct pollconn **handles; int handles_len; } ps; struct pollconn ls; struct lineserver_client { int h; int next_pos; int closed; int shutdown; int error; } *clients; int num_clients; int max_clients; int next_conn_pos; char delim; } ls; struct linepiper { int fd_in; int fd_out; struct tcpbuf buf_in; struct tcpbuf buf_out; int in_pos; char delim; } lp; int accepted; int in_conn; int out_conn; enum pipeserver_event { PIPESERVER_NULLEVENT=0, PIPESERVER_ACCEPTED=1, PIPESERVER_CLOSED=2, PIPESERVER_CLOSING=3, PIPESERVER_ERROR=4, PIPESERVER_RECEIVED=5, PIPESERVER_CLOSE=6, PIPESERVER_SEND=7, PIPESERVER_SHUTDOWN=8 } in_event; enum pipeserver_event { PIPESERVER_NULLEVENT=0, PIPESERVER_ACCEPTED=1, PIPESERVER_CLOSED=2, PIPESERVER_CLOSING=3, PIPESERVER_ERROR=4, PIPESERVER_RECEIVED=5, PIPESERVER_CLOSE=6, PIPESERVER_SEND=7, PIPESERVER_SHUTDOWN=8 } out_event; char *in_data; int in_datalen; char *out_data; int out_datalen; char *msg; int msg_len; }

variable_types: ret int

user_defined_types: struct pipeserver { struct lineserver ls; struct linepiper lp; int accepted; int in_conn; int out_conn; enum pipeserver_event in_event; enum pipeserver_event out_event; char *in_data; int in_datalen; char *out_data; int out_datalen; char *msg; int msg_len; }

user_defined_types: struct lineserver { struct pollset ps; struct pollconn ls; struct lineserver_client *clients; int num_clients; int max_clients; int next_conn_pos; char delim; }


repo: sheimi/tiny-find

decompiled_code:
void init_option()
{
options = 0LL;
*(&options + 1) = 0LL;
*(&options + 3) = 0LL;
*(&options + 4) = 0LL;
*(&options + 5) = 0LL;
*(&options + 6) = 0LL;
*(&options + 7) = 0LL;
*(&options + 8) = 0LL;
*(&options + 9) = 0LL;
*((_DWORD *)&options + 5) = -1;
*((_DWORD *)&options + 4) = -1;
*((_DWORD *)&options + 17) = 1;
}

canonical_decompiled_code:
void func0()
{
options = 0LL;
*(&options + 1) = 0LL;
*(&options + 3) = 0LL;
*(&options + 4) = 0LL;
*(&options + 5) = 0LL;
*(&options + 6) = 0LL;
*(&options + 7) = 0LL;
*(&options + 8) = 0LL;
*(&options + 9) = 0LL;
*((_DWORD *)&options + 5) = -1;
*((_DWORD *)&options + 4) = -1;
*((_DWORD *)&options + 17) = 1;
}

original_code:
static void init_option() {
  memset(&options, 0, sizeof(FindOption));
  options.max_depth = -1;
  options.min_depth = -1;
  options.no_actions = 1;
}


decompiled_code:
void __fastcall execute_exec(int a1, const char **a2, const char *a3, int a4)
{
size_t v5; // r14
const char **v6; // rax
const char **v7; // rbp
const char **v8; // rax
const char **v9; // rbx
__int64 v10; // r13
const char **v11; // r8
int v12; // eax

v5 = a1;
v6 = (const char **)malloc(v5 * 8);
v7 = v6;
if ( a1 > 1 )
{
v8 = a2;
v9 = v7;
v10 = (unsigned int)(a1 - 2);
v11 = v7;
do
{
if ( !strcmp(*v8, "{}") )
*v11 = a3;
else
*v11 = *v8;
++v8;
++v11;
}
while ( v8 != &a2[v10 + 1] );
v7[v5 - 1] = 0LL;
if ( !a4 )
goto LABEL_14;
fputc(34, stdout);
do
fprintf(stdout, "%s ", *v9++);
while ( v9 != &v7[v10 + 1] );
LABEL_11:
fwrite("\"?", 1uLL, 2uLL, stdout);
if ( (getchar() & 0xDF) != 89 )
return;
goto LABEL_14;
}
v6[v5 - 1] = 0LL;
if ( a4 )
{
fputc(34, stdout);
goto LABEL_11;
}
LABEL_14:
v12 = fork();
if ( v12 < 0 )
{
fwrite("ERROR\n", 1uLL, 6uLL, stderr);
}
else if ( v12 )
{
waitpid(v12, 0LL, 0);
}
else
{
execvp(*v7, (char *const *)v7);
}
free(v7);
}

canonical_decompiled_code:
void __fastcall func1(int a1, const char **a2, const char *a3, int a4)
{
size_t v5; // r14
const char **v6; // rax
const char **v7; // rbp
const char **v8; // rax
const char **v9; // rbx
__int64 v10; // r13
const char **v11; // r8
int v12; // eax

v5 = a1;
v6 = (const char **)malloc(v5 * 8);
v7 = v6;
if ( a1 > 1 )
{
v8 = a2;
v9 = v7;
v10 = (unsigned int)(a1 - 2);
v11 = v7;
do
{
if ( !strcmp(*v8, "{}") )
*v11 = a3;
else
*v11 = *v8;
++v8;
++v11;
}
while ( v8 != &a2[v10 + 1] );
v7[v5 - 1] = 0LL;
if ( !a4 )
goto LABEL_14;
fputc(34, stdout);
do
fprintf(stdout, "%s ", *v9++);
while ( v9 != &v7[v10 + 1] );
LABEL_11:
fwrite("\"?", 1uLL, 2uLL, stdout);
if ( (getchar() & 0xDF) != 89 )
return;
goto LABEL_14;
}
v6[v5 - 1] = 0LL;
if ( a4 )
{
fputc(34, stdout);
goto LABEL_11;
}
LABEL_14:
v12 = fork();
if ( v12 < 0 )
{
fwrite("ERROR\n", 1uLL, 6uLL, stderr);
}
else if ( v12 )
{
waitpid(v12, 0LL, 0);
}
else
{
execvp(*v7, (char *const *)v7);
}
free(v7);
}

original_code:
static void execute_exec(int argc, char ** argv, char * path, bool ask) {
  char ** argv_r = (char **)(malloc(sizeof(char *) * (argc)));
  int i = 0;
  for (i = 0; i < argc - 1; i++) {
    if ((strcmp(argv[i], "{}") == 0)) {
      argv_r[i] = path;
    } else {
      argv_r[i] = argv[i];
    }
  }
  argv_r[argc - 1] = '\0';
  if (ask) {
    fputs("\"", stdout);
    for (i = 0; i < argc - 1; i++) {
      fprintf(stdout, "%s ", argv_r[i]);
    }
    fputs("\"?", stdout);
    char o = getchar();
    if (o != 'y' && o != 'Y')
      return;
  }
  pid_t pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "ERROR\n");
  } else if (pid == 0) {
    execvp(argv_r[0], argv_r);
  } else {
    waitpid(pid, ((void *)0), 0);
  }
  free(argv_r);
}

canonical_original_code:
static void execute_exec(int argc, char **argv, char *path, int ask) {
  char **argv_r = (char **)(malloc(sizeof(char *) * (argc)));
  int i = 0;
  for (i = 0; i < argc - 1; i++) {
    if ((strcmp(argv[i], "{}") == 0)) {
      argv_r[i] = path;
    } else {
      argv_r[i] = argv[i];
    }
  }
  argv_r[argc - 1] = '\0';
  if (ask) {
    fputs("\"", stdout);
    for (i = 0; i < argc - 1; i++) {
      fprintf(stdout, "%s ", argv_r[i]);
    }
    fputs("\"?", stdout);
    char o = getchar();
    if (o != 'y' && o != 'Y')
      return;
  }
  int pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "ERROR\n");
  } else if (pid == 0) {
    execvp(argv_r[0], argv_r);
  } else {
    waitpid(pid, ((void *)0), 0);
  }
  free(argv_r);
}

variable_types:
argv
char **

variable_types: ask int

variable_types: o char


check

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

result = execute_filter_tree();
if ( result )
{
if ( *((_DWORD *)&options + 6) )
execute_exec(*((_DWORD *)&options + 10), (const char **)*(&options + 4), *(const char **)(a1 + 48), 0);
if ( *((_DWORD *)&options + 11) )
execute_exec(*((_DWORD *)&options + 14), (const char **)*(&options + 6), *(const char **)(a1 + 48), 1);
if ( *((_DWORD *)&options + 16) )
fputs(*(const char **)(a1 + 48), stdout);
result = *((_DWORD *)&options + 17) | *((_DWORD *)&options + 15);
if ( result )
result = fprintf(stdout, "%s\n", *(const char **)(a1 + 48));
}
return result;
}

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

result = execute_filter_tree();
if ( result )
{
if ( *((_DWORD *)&options + 6) )
func1(*((_DWORD *)&options + 10), (const char **)*(&options + 4), *(const char **)(a1 + 48), 0);
if ( *((_DWORD *)&options + 11) )
func1(*((_DWORD *)&options + 14), (const char **)*(&options + 6), *(const char **)(a1 + 48), 1);
if ( *((_DWORD *)&options + 16) )
fputs(*(const char **)(a1 + 48), stdout);
result = *((_DWORD *)&options + 17) | *((_DWORD *)&options + 15);
if ( result )
result = fprintf(stdout, "%s\n", *(const char **)(a1 + 48));
}
return result;
}

original_code:
static void check(FTSENT * ent) {
  if (execute_filter_tree(ent)) {
    if (options.is_exec) {
      execute_exec(options.argc, options.argv, ent->fts_path, 0);
    }
    if (options.is_ok) {
      execute_exec(options.ok_argc, options.ok_argv, ent->fts_path, 1);
    }
    if (options.is_print0) {
      fprintf(stdout, "%s", ent->fts_path);
    }
    if (options.is_print || options.no_actions){
      fprintf(stdout, "%s\n", ent->fts_path);
    }
  }
}

canonical_original_code:
static void check(struct _ftsent *ent) {
  if (execute_filter_tree(ent)) {
    if (options.is_exec) {
      execute_exec(options.argc, options.argv, ent->fts_path, 0);
    }
    if (options.is_ok) {
      execute_exec(options.ok_argc, options.ok_argv, ent->fts_path, 1);
    }
    if (options.is_print0) {
      fprintf(stdout, "%s", ent->fts_path);
    }
    if (options.is_print || options.no_actions){
      fprintf(stdout, "%s\n", ent->fts_path);
    }
  }
}

variable_types: struct _ftsent { struct _ftsent *fts_cycle; struct _ftsent *fts_parent; struct _ftsent *fts_link; long fts_number; void *fts_pointer; char *fts_accpath; char *fts_path; int fts_errno; int fts_symfd; unsigned short fts_pathlen; unsigned short fts_namelen; unsigned long fts_ino; unsigned long fts_dev; unsigned long fts_nlink; short fts_level; unsigned short fts_info; unsigned short fts_flags; unsigned short fts_instr; struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *fts_statp; char fts_name[1]; } *

user_defined_types: struct _ftsent { struct _ftsent *fts_cycle; struct _ftsent *fts_parent; struct _ftsent *fts_link; long fts_number; void *fts_pointer; char *fts_accpath; char *fts_path; int fts_errno; int fts_symfd; unsigned short fts_pathlen; unsigned short fts_namelen; unsigned long fts_ino; unsigned long fts_dev; unsigned long fts_nlink; short fts_level; unsigned short fts_info; unsigned short fts_flags; unsigned short fts_instr; struct stat *fts_statp; char fts_name[1]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
int __fastcall consider_check(FTS *a1, FTSENT *a2)
{
u_short v2; // bp
int v3; // eax
int result; // eax

v2 = a2->fts_info;
v3 = *((_DWORD *)&options + 5);
if ( v3 >= 0 && v3 <= a2->fts_level )
{
fts_set(a1, a2, 4);
result = a2->fts_level;
if ( result > *((_DWORD *)&options + 5) )
return result;
goto LABEL_5;
}
result = *((_DWORD *)&options + 4);
if ( result < 0 || result <= a2->fts_level )
{
LABEL_5:
if ( v2 != 6 )
result = check((__int64)a2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(FTS *a1, FTSENT *a2)
{
u_short v2; // bp
int v3; // eax
int result; // eax

v2 = a2->fts_info;
v3 = *((_DWORD *)&options + 5);
if ( v3 >= 0 && v3 <= a2->fts_level )
{
fts_set(a1, a2, 4);
result = a2->fts_level;
if ( result > *((_DWORD *)&options + 5) )
return result;
goto LABEL_5;
}
result = *((_DWORD *)&options + 4);
if ( result < 0 || result <= a2->fts_level )
{
LABEL_5:
if ( v2 != 6 )
result = func2((__int64)a2);
}
return result;
}

original_code:
static void consider_check(FTS * fts, FTSENT * ent) {
  bool ignore = 0;
  if (ent->fts_info == 6) {
    ignore = 1;
  }
  if (options.max_depth >=0 && ent->fts_level >= options.max_depth) {
    fts_set(fts, ent, 4);
    if (ent->fts_level > options.max_depth)
      ignore = 1;
  } else if (options.min_depth >=0 && ent->fts_level < options.min_depth) {
    ignore = 1;
  }
  if (!ignore) {
    check(ent);
  }
}

canonical_original_code:
static void consider_check(struct FTS *fts, struct _ftsent *ent) {
  int ignore = 0;
  if (ent->fts_info == 6) {
    ignore = 1;
  }
  if (options.max_depth >=0 && ent->fts_level >= options.max_depth) {
    fts_set(fts, ent, 4);
    if (ent->fts_level > options.max_depth)
      ignore = 1;
  } else if (options.min_depth >=0 && ent->fts_level < options.min_depth) {
    ignore = 1;
  }
  if (!ignore) {
    check(ent);
  }
}

variable_types: struct FTS { struct _ftsent { struct _ftsent *fts_cycle; struct _ftsent *fts_parent; struct _ftsent *fts_link; long fts_number; void *fts_pointer; char *fts_accpath; char *fts_path; int fts_errno; int fts_symfd; unsigned short fts_pathlen; unsigned short fts_namelen; unsigned long fts_ino; unsigned long fts_dev; unsigned long fts_nlink; short fts_level; unsigned short fts_info; unsigned short fts_flags; unsigned short fts_instr; struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; } *fts_statp; char fts_name[1]; } *fts_cur; struct _ftsent *fts_child; struct _ftsent **fts_array; unsigned long fts_dev; char *fts_path; int fts_rfd; int fts_pathlen; int fts_nitems; int (*fts_compar)(void *, void *); int fts_options; } *

variable_types:
ent
struct _ftsent *

variable_types: ignore int

user_defined_types: struct FTS { struct _ftsent *fts_cur; struct _ftsent *fts_child; struct _ftsent **fts_array; unsigned long fts_dev; char *fts_path; int fts_rfd; int fts_pathlen; int fts_nitems; int (*fts_compar)(void *, void *); int fts_options; }


decompiled_code:
void __fastcall __noreturn print_error(const char *a1)
{
if ( a1 )
fputs(a1, stderr);
else
fwrite("encounter a error, exit\n", 1uLL, 0x18uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(const char *a1)
{
if ( a1 )
fputs(a1, stderr);
else
fwrite("encounter a error, exit\n", 1uLL, 0x18uLL, stderr);
exit(1);
}

original_code:
void print_error(const char * message) {
  if (message != ((void *)0)) {
    fputs(message, stderr);
  } else {
    fputs("encounter a error, exit\n", stderr);
  }
  exit(1);
}

canonical_original_code:
void print_error(char *message) {
  if (message != ((void *)0)) {
    fputs(message, stderr);
  } else {
    fputs("encounter a error, exit\n", stderr);
  }
  exit(1);
}

variable_types:
message
char *


decompiled_code:
void __fastcall __noreturn require_arg(const char *a1)
{
if ( a1 )
{
fprintf(stderr, "find: %s: requires additional arguments\n", a1);
exit(1);
}
print_error("find: requires additional arguments\n");
}

canonical_decompiled_code:
void __fastcall __noreturn func5(const char *a1)
{
if ( a1 )
{
fprintf(stderr, "find: %s: requires additional arguments\n", a1);
exit(1);
}
func4("find: requires additional arguments\n");
}

original_code:
void require_arg(const char * exp) {
  if (exp != ((void *)0)) {
    fprintf(stderr, "find: %s: requires additional arguments\n", exp);
    exit(1);
  } else {
    print_error("find: requires additional arguments\n");
  }
}

canonical_original_code:
void require_arg(char *exp) {
  if (exp != ((void *)0)) {
    fprintf(stderr, "find: %s: requires additional arguments\n", exp);
    exit(1);
  } else {
    print_error("find: requires additional arguments\n");
  }
}

variable_types:
exp
char *