decompiled_code:
__int64 __fastcall lineserver_accept(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
int v3; // edx
_DWORD *v4; // rdx

pollconn_operate(a1 + 64);
if ( !(unsigned int)pollconn_did_accept(a1 + 64) )
return 0xFFFFFFFFLL;
v1 = pollconn_accept_sock(a1 + 64);
result = pollset_addaccepted(a1, v1);
if ( (_DWORD)result != -1 )
{
v3 = *(_DWORD *)(a1 + 200);
if ( v3 >= *(_DWORD *)(a1 + 204) )
{
pollset_delconn(a1, (unsigned int)result);
result = 0xFFFFFFFFLL;
}
else
{
v4 = (_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v3);
*v4 = result;
v4[1] = -1;
v4[2] = 0;
v4[3] = 0;
v4[4] = 0;
++*(_DWORD *)(a1 + 200);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
int v3; // edx
_DWORD *v4; // rdx

pollconn_operate(a1 + 64);
if ( !(unsigned int)pollconn_did_accept(a1 + 64) )
return 0xFFFFFFFFLL;
v1 = pollconn_accept_sock(a1 + 64);
result = pollset_addaccepted(a1, v1);
if ( (_DWORD)result != -1 )
{
v3 = *(_DWORD *)(a1 + 200);
if ( v3 >= *(_DWORD *)(a1 + 204) )
{
pollset_delconn(a1, (unsigned int)result);
result = 0xFFFFFFFFLL;
}
else
{
v4 = (_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v3);
*v4 = result;
v4[1] = -1;
v4[2] = 0;
v4[3] = 0;
v4[4] = 0;
++*(_DWORD *)(a1 + 200);
}
}
return result;
}

original_code:
pollset_handle lineserver_accept(lineserver *l)
{
    pollset_handle h;
    lineserver_client *lsc;
    pollconn_operate(&l->ls);
    if(pollconn_did_accept(&l->ls))
    {
        h=pollset_addaccepted(&l->ps, pollconn_accept_sock(&l->ls));
        if(h!=-1)
        {
            if(l->num_clients>=l->max_clients)
            {
                pollset_delconn(&l->ps, h);
                return -1;
            }
            lsc=l->clients+(l->num_clients);
            lsc->h=h;
            lsc->next_pos=-1;
            lsc->closed=0;
            lsc->shutdown=0;
            lsc->error=0;
            (l->num_clients)++;
            return h;
        }
    }
    return -1;
}

canonical_original_code:
int lineserver_accept(struct lineserver *l)
{
    int h;
    struct lineserver_client *lsc;
    pollconn_operate(&l->ls);
    if(pollconn_did_accept(&l->ls))
    {
        h=pollset_addaccepted(&l->ps, pollconn_accept_sock(&l->ls));
        if(h!=-1)
        {
            if(l->num_clients>=l->max_clients)
            {
                pollset_delconn(&l->ps, h);
                return -1;
            }
            lsc=l->clients+(l->num_clients);
            lsc->h=h;
            lsc->next_pos=-1;
            lsc->closed=0;
            lsc->shutdown=0;
            lsc->error=0;
            (l->num_clients)++;
            return h;
        }
    }
    return -1;
}

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

variable_types: h int


repo: sheheitthey/pipeserver

decompiled_code:
__int64 __fastcall lineserver_enqueue(__int64 a1, __int64 a2, __int64 a3, int a4)
{
__int64 v6; // rax
__int64 v7; // rbp

if ( a4 < 0 )
return 0xFFFFFFFFLL;
v6 = pollset_conn(a1, a2);
v7 = v6;
if ( !v6 )
return 0xFFFFFFFFLL;
if ( (int)pollconn_sendbuf_space(v6) <= a4 )
return 0xFFFFFFFFLL;
pollconn_enqueue(v7, (unsigned int)a4, a3);
pollconn_enqueue(v7, 1LL, a1 + 212);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2, __int64 a3, int a4)
{
__int64 v6; // rax
__int64 v7; // rbp

if ( a4 < 0 )
return 0xFFFFFFFFLL;
v6 = pollset_conn(a1, a2);
v7 = v6;
if ( !v6 )
return 0xFFFFFFFFLL;
if ( (int)pollconn_sendbuf_space(v6) <= a4 )
return 0xFFFFFFFFLL;
pollconn_enqueue(v7, (unsigned int)a4, a3);
pollconn_enqueue(v7, 1LL, a1 + 212);
return 0LL;
}

original_code:
int lineserver_enqueue(lineserver *l, pollset_handle h,
                       const char *data, int len)
{
    pollconn *c;
    if(len<0)
        return -1;
    c=pollset_conn(&l->ps, h);
    if(!c)
        return -1;
    if(pollconn_sendbuf_space(c)<len+1)
        return -1;
    pollconn_enqueue(c, len, data);
    pollconn_enqueue(c, 1, &l->delim);
    return 0;
}

canonical_original_code:
int lineserver_enqueue(struct lineserver *l, int h, char *data, int len)
{
    struct pollconn *c;
    if(len<0)
        return -1;
    c=pollset_conn(&l->ps, h);
    if(!c)
        return -1;
    if(pollconn_sendbuf_space(c)<len+1)
        return -1;
    pollconn_enqueue(c, len, data);
    pollconn_enqueue(c, 1, &l->delim);
    return 0;
}

variable_types:
data
char *

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

return_type: int

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

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 pollset { struct pollpool pp; struct pollconn **handles; int handles_len; }

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

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

user_defined_types: struct lineserver_client { int h; int next_pos; int closed; int shutdown; int error; }


decompiled_code:
__int64 __fastcall lineserver_shutdown(__int64 a1, __int64 a2)
{
__int64 v2; // rax

v2 = pollset_conn(a1, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
pollconn_shutdown(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2)
{
__int64 v2; // rax

v2 = pollset_conn(a1, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
pollconn_shutdown(v2);
return 0LL;
}

original_code:
int lineserver_shutdown(lineserver *l, pollset_handle h)
{
    pollconn *c;
    c=pollset_conn(&l->ps, h);
    if(c)
    {
        pollconn_shutdown(c);
        return 0;
    }
    return -1;
}

canonical_original_code:
int lineserver_shutdown(struct lineserver *l, int h)
{
    struct pollconn *c;
    c=pollset_conn(&l->ps, h);
    if(c)
    {
        pollconn_shutdown(c);
        return 0;
    }
    return -1;
}

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 lineserver_close(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp
int v4; // esi
__int64 result; // rax

v2 = pollset_conn(a1, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
if ( !(unsigned int)pollconn_ready(v2) || (result = pollconn_close(v3), (_DWORD)result != -1) )
{
v4 = lineserver_find_handle(a1, a2);
if ( v4 < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
lineserver_remove(a1, v4);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rbp
int v4; // esi
__int64 result; // rax

v2 = pollset_conn(a1, a2);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
if ( !(unsigned int)pollconn_ready(v2) || (result = pollconn_close(v3), (_DWORD)result != -1) )
{
v4 = func0(a1, a2);
if ( v4 < 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
func7(a1, v4);
result = 0LL;
}
}
return result;
}

original_code:
int lineserver_close(lineserver *l, pollset_handle h)
{
    pollconn *c;
    int index;
    c=pollset_conn(&l->ps, h);
    if(!c)
        return -1;
    if(pollconn_ready(c))
    {
        if(pollconn_close(c)==-1)
            return -1;
    }
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    lineserver_remove(l, index);
    return 0;
}

canonical_original_code:
int lineserver_close(struct lineserver *l, int h)
{
    struct pollconn *c;
    int index;
    c=pollset_conn(&l->ps, h);
    if(!c)
        return -1;
    if(pollconn_ready(c))
    {
        if(pollconn_close(c)==-1)
            return -1;
    }
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    lineserver_remove(l, index);
    return 0;
}

variable_types: index int


decompiled_code:
__int64 __fastcall lineserver_dequeue(__int64 a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // edx
int v4; // ebp
_DWORD *v5; // r13
unsigned int v6; // eax
__int64 v7; // r12

v2 = lineserver_find_handle(a1, a2);
v3 = -1;
if ( v2 >= 0 )
{
v4 = v2;
v5 = (_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v2);
v6 = lineserver_getevent_i(a1, v2);
if ( v6 == 3 )
{
v5[3] = 0;
goto LABEL_12;
}
if ( v6 > 3 )
{
v3 = 0;
if ( v6 == 4 )
{
v7 = pollset_conn(a1, a2);
if ( (unsigned int)lineserver_getline_i(a1, v4, 0LL, 0LL) )
{
pollconn_dequeue(v7, (unsigned int)(v5[1] + 1));
v5[1] = -1;
advance_client_pos(a1, v4);
}
goto LABEL_12;
}
}
else
{
if ( v6 == 1 )
{
v5[2] = 0;
lineserver_remove(a1, v4);
goto LABEL_12;
}
if ( v6 == 2 )
{
v5[4] = 0;
lineserver_close(a1, a2);
LABEL_12:
advance_next_conn(a1);
return 1;
}
v3 = 0;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // edx
int v4; // ebp
_DWORD *v5; // r13
unsigned int v6; // eax
__int64 v7; // r12

v2 = func0(a1, a2);
v3 = -1;
if ( v2 >= 0 )
{
v4 = v2;
v5 = (_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v2);
v6 = func5(a1, v2);
if ( v6 == 3 )
{
v5[3] = 0;
goto LABEL_12;
}
if ( v6 > 3 )
{
v3 = 0;
if ( v6 == 4 )
{
v7 = pollset_conn(a1, a2);
if ( (unsigned int)func4(a1, v4, 0LL, 0LL) )
{
pollconn_dequeue(v7, (unsigned int)(v5[1] + 1));
v5[1] = -1;
func3(a1, v4);
}
goto LABEL_12;
}
}
else
{
if ( v6 == 1 )
{
v5[2] = 0;
func7(a1, v4);
goto LABEL_12;
}
if ( v6 == 2 )
{
v5[4] = 0;
func16(a1, a2);
LABEL_12:
func6(a1);
return 1;
}
v3 = 0;
}
}
return v3;
}

original_code:
int lineserver_dequeue(lineserver *l, pollset_handle h)
{
    int index;
    lineserver_client *lsc;
    pollconn *c;
    lineserver_event e;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    lsc=l->clients+index;
    e=lineserver_getevent_i(l, index);
    switch(e)
    {
    case LINESERVER_LINE:
        c=pollset_conn(&l->ps, h);
        if(lineserver_getline_i(l, index, ((void *)0), ((void *)0)))
        {
            pollconn_dequeue(c, (lsc->next_pos)+1);
            lsc->next_pos=-1;
            advance_client_pos(l, index);
        }
        break;
    case LINESERVER_SHUTDOWN:
        lsc->shutdown=0;
        break;
    case LINESERVER_CLOSED:
        lsc->closed=0;
        lineserver_remove(l, index);
        break;
    case LINESERVER_ERROR:
        lsc->error=0;
        lineserver_close(l, h);
        break;
    default:
        return 0;
    }
    advance_next_conn(l);
    return 1;
}

canonical_original_code:
int lineserver_dequeue(struct lineserver *l, int h)
{
    int index;
    struct lineserver_client *lsc;
    struct pollconn *c;
    enum lineserver_event e;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    lsc=l->clients+index;
    e=lineserver_getevent_i(l, index);
    switch(e)
    {
    case LINESERVER_LINE:
        c=pollset_conn(&l->ps, h);
        if(lineserver_getline_i(l, index, ((void *)0), ((void *)0)))
        {
            pollconn_dequeue(c, (lsc->next_pos)+1);
            lsc->next_pos=-1;
            advance_client_pos(l, index);
        }
        break;
    case LINESERVER_SHUTDOWN:
        lsc->shutdown=0;
        break;
    case LINESERVER_CLOSED:
        lsc->closed=0;
        lineserver_remove(l, index);
        break;
    case LINESERVER_ERROR:
        lsc->error=0;
        lineserver_close(l, h);
        break;
    default:
        return 0;
    }
    advance_next_conn(l);
    return 1;
}

variable_types: struct lineserver_client { int h; int next_pos; int closed; int shutdown; int error; } *

variable_types: enum lineserver_event { LINESERVER_NULLEVENT=0, LINESERVER_CLOSED=1, LINESERVER_ERROR=2, LINESERVER_SHUTDOWN=3, LINESERVER_LINE=4 }


decompiled_code:
__int64 __fastcall lineserver_getevent(__int64 a1, int a2)
{
int v2; // esi
__int64 result; // rax

v2 = lineserver_find_handle(a1, a2);
result = 0LL;
if ( v2 >= 0 )
result = lineserver_getevent_i(a1, v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, int a2)
{
int v2; // esi
__int64 result; // rax

v2 = func0(a1, a2);
result = 0LL;
if ( v2 >= 0 )
result = func5(a1, v2);
return result;
}

original_code:
lineserver_event
lineserver_getevent(const lineserver *l, pollset_handle h)
{
    int index;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return LINESERVER_NULLEVENT;
    return lineserver_getevent_i(l, index);
}

canonical_original_code:
enum lineserver_event lineserver_getevent(struct lineserver *l, int h)
{
    int index;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return LINESERVER_NULLEVENT;
    return lineserver_getevent_i(l, index);
}

return_type: enum lineserver_event { LINESERVER_NULLEVENT=0, LINESERVER_CLOSED=1, LINESERVER_ERROR=2, LINESERVER_SHUTDOWN=3, LINESERVER_LINE=4 }


decompiled_code:
__int64 __fastcall lineserver_getline(__int64 a1, int a2, __int64 *a3, _DWORD *a4)
{
int v6; // eax
__int64 result; // rax

v6 = lineserver_find_handle(a1, a2);
if ( v6 < 0 )
result = 0xFFFFFFFFLL;
else
result = lineserver_getline_i(a1, v6, a3, a4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1, int a2, __int64 *a3, _DWORD *a4)
{
int v6; // eax
__int64 result; // rax

v6 = func0(a1, a2);
if ( v6 < 0 )
result = 0xFFFFFFFFLL;
else
result = func4(a1, v6, a3, a4);
return result;
}

original_code:
int lineserver_getline(const lineserver *l, pollset_handle h,
                       const char **data, int *len)
{
    int index;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    return lineserver_getline_i(l, index, data, len);
}

canonical_original_code:
int lineserver_getline(struct lineserver *l, int h, char **data, int *len)
{
    int index;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return -1;
    return lineserver_getline_i(l, index, data, len);
}

variable_types:
data
char **

variable_types:
len
int *


decompiled_code:
__int64 __fastcall lineserver_geterror(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // edx

v2 = lineserver_find_handle(a1, a2);
v3 = 0;
if ( v2 >= 0 )
v3 = *(_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v2 + 16);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // edx

v2 = func0(a1, a2);
v3 = 0;
if ( v2 >= 0 )
v3 = *(_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v2 + 16);
return v3;
}

original_code:
int
lineserver_geterror(const lineserver *l, pollset_handle h)
{
    int index;
    lineserver_client *lsc;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return 0;
    lsc=l->clients+index;
    return lsc->error;
}

canonical_original_code:
int lineserver_geterror(struct lineserver *l, int h)
{
    int index;
    struct lineserver_client *lsc;
    index=lineserver_find_handle(l, h);
    if(index<0)
        return 0;
    lsc=l->clients+index;
    return lsc->error;
}


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

result = *(unsigned int *)(a1 + 208);
if ( (_DWORD)result != -1 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 192) + 20LL * (int)result);
return result;
}

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

result = *(unsigned int *)(a1 + 208);
if ( (_DWORD)result != -1 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 192) + 20LL * (int)result);
return result;
}

original_code:
pollset_handle lineserver_getnextclient(const lineserver *l)
{
    if(l->next_conn_pos==-1)
        return -1;
    return l->clients[l->next_conn_pos].h;
}

canonical_original_code:
int lineserver_getnextclient(struct lineserver *l)
{
    if(l->next_conn_pos==-1)
        return -1;
    return l->clients[l->next_conn_pos].h;
}


decompiled_code:
__int64 __fastcall lineserver_getnextevent(__int64 a1, _DWORD *a2, _DWORD *a3)
{
int v4; // eax
unsigned int v5; // edx

v4 = *(_DWORD *)(a1 + 208);
v5 = 0;
if ( v4 != -1 )
{
if ( a2 )
*a2 = *(_DWORD *)(*(_QWORD *)(a1 + 192) + 20LL * v4);
v5 = 1;
if ( a3 )
{
*a3 = lineserver_getevent_i(a1, *(_DWORD *)(a1 + 208));
v5 = 1;
}
}
return v5;
}

original_code:
int
lineserver_getnextevent(const lineserver *l, pollset_handle *h,
                        lineserver_event *e)
{
    if(l->next_conn_pos==-1)
        return 0;
    if(h)
        *h=l->clients[l->next_conn_pos].h;
    if(e)
        *e=lineserver_getevent_i(l, l->next_conn_pos);
    return 1;
}

canonical_original_code:
int lineserver_getnextevent(struct lineserver *l, int *h, enum lineserver_event *e)
{
    if(l->next_conn_pos==-1)
        return 0;
    if(h)
        *h=l->clients[l->next_conn_pos].h;
    if(e)
        *e=lineserver_getevent_i(l, l->next_conn_pos);
    return 1;
}

variable_types:
h
int *

variable_types: enum lineserver_event { LINESERVER_NULLEVENT=0, LINESERVER_CLOSED=1, LINESERVER_ERROR=2, LINESERVER_SHUTDOWN=3, LINESERVER_LINE=4 } *


decompiled_code:
__int64 __fastcall lineserver_conn(__int64 a1, __int64 a2)
{
return pollset_conn(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, __int64 a2)
{
return pollset_conn(a1, a2);
}

original_code:
const pollconn* lineserver_conn(const lineserver *l, pollset_handle h)
{
    return pollset_conn(&l->ps, h);
}

canonical_original_code:
struct pollconn *lineserver_conn(struct lineserver *l, int h)
{
    return pollset_conn(&l->ps, h);
}

variable_types: struct lineserver { struct pollset { struct pollpool { int len; struct pollconn *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; } *

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


original_code:
static int
pollpool_freelist_dequeue(pollpool *pp)
{
    int ret;
    if(pp->freelist_len<=0)
        return -1;
    ret=pp->freelist[pp->freelist_head];
    (pp->freelist_head)++;
    if(pp->freelist_head>=pp->len)
        (pp->freelist_head)%=(pp->len);
    (pp->freelist_len)--;
    return ret;
}

canonical_original_code:
static int pollpool_freelist_dequeue(struct pollpool *pp)
{
    int ret;
    if(pp->freelist_len<=0)
        return -1;
    ret=pp->freelist[pp->freelist_head];
    (pp->freelist_head)++;
    if(pp->freelist_head>=pp->len)
        (pp->freelist_head)%=(pp->len);
    (pp->freelist_len)--;
    return ret;
}

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


decompiled_code:
__int64 __fastcall pollpool_freelist_enqueue(__int64 a1, int a2)
{
int v2; // eax

v2 = *(_DWORD *)(a1 + 36);
if ( v2 >= *(_DWORD *)a1 )
return 0xFFFFFFFFLL;
*(_DWORD *)(*(_QWORD *)(a1 + 24) + 4LL * ((*(_DWORD *)(a1 + 32) + v2) % *(_DWORD *)a1)) = a2;
++*(_DWORD *)(a1 + 36);
return 0LL;
}

original_code:
static int
pollpool_freelist_enqueue(pollpool *pp, int index)
{
    int i;
    if(pp->freelist_len>=pp->len)
        return -1;
    i=(pp->freelist_head+pp->freelist_len)%(pp->len);
    pp->freelist[i]=index;
    (pp->freelist_len)++;
    return 0;
}

canonical_original_code:
static int pollpool_freelist_enqueue(struct pollpool *pp, int index)
{
    int i;
    if(pp->freelist_len>=pp->len)
        return -1;
    i=(pp->freelist_head+pp->freelist_len)%(pp->len);
    pp->freelist[i]=index;
    (pp->freelist_len)++;
    return 0;
}

variable_types: i int


original_code:
static int
pollpool_index_allocated(const pollpool *pp, int index)
{
    int i;
    if(index<0 || index>=pp->len)
        return 0;
    for(i=0; i<pp->freelist_len; i++)
    {
        if(pp->freelist[(pp->freelist_head+i)%pp->len]==index)
            return 0;
    }
    return 1;
}

canonical_original_code:
static int pollpool_index_allocated(struct pollpool *pp, int index)
{
    int i;
    if(index<0 || index>=pp->len)
        return 0;
    for(i=0; i<pp->freelist_len; i++)
    {
        if(pp->freelist[(pp->freelist_head+i)%pp->len]==index)
            return 0;
    }
    return 1;
}


original_code:
void
pollpool_destruct(pollpool *pp)
{
    int i;
    for(i=0; i<pp->len; i++)
        pollconn_destruct(pp->pollconns+i);
    pp->ufds=((void *)0);
    free(pp->pollconns);
    pp->pollconns=((void *)0);
    free(pp->freelist);
    pp->freelist=((void *)0);
    pp->freelist_head=0;
    pp->freelist_len=0;
    pp->len=0;
}

canonical_original_code:
void pollpool_destruct(struct pollpool *pp)
{
    int i;
    for(i=0; i<pp->len; i++)
        pollconn_destruct(pp->pollconns+i);
    pp->ufds=((void *)0);
    free(pp->pollconns);
    pp->pollconns=((void *)0);
    free(pp->freelist);
    pp->freelist=((void *)0);
    pp->freelist_head=0;
    pp->freelist_len=0;
    pp->len=0;
}

return_type: void


decompiled_code:
void pollpool_resize()
{
;
}

canonical_decompiled_code:
void func4()
{
;
}

original_code:
void
pollpool_resize(pollpool *pp, struct pollfd *ufds, int newlen)
{
}

canonical_original_code:
void pollpool_resize(struct pollpool *pp, struct pollfd *ufds, int newlen)
{
}

variable_types:
ufds
struct pollfd *

variable_types: newlen int


decompiled_code:
__int64 __fastcall pollpool_new(__int64 a1)
{
int v1; // eax
__int64 result; // rax

v1 = pollpool_freelist_dequeue(a1);
if ( v1 < 0 )
result = 0LL;
else
result = *(_QWORD *)(a1 + 8) + ((__int64)v1 << 7);
return result;
}

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

v1 = func0(a1);
if ( v1 < 0 )
result = 0LL;
else
result = *(_QWORD *)(a1 + 8) + ((__int64)v1 << 7);
return result;
}

original_code:
pollconn*
pollpool_new(pollpool *pp)
{
    int index;
    index=pollpool_freelist_dequeue(pp);
    if(index<0)
        return ((void *)0);
    return pp->pollconns+index;
}

canonical_original_code:
struct pollconn *pollpool_new(struct pollpool *pp)
{
    int index;
    index=pollpool_freelist_dequeue(pp);
    if(index<0)
        return ((void *)0);
    return pp->pollconns+index;
}

variable_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 __fastcall pollpool_free(__int64 a1, __int64 a2)
{
__int64 v2; // rbp

v2 = (a2 - *(_QWORD *)(a1 + 8)) >> 7;
if ( !(unsigned int)pollpool_index_allocated(a1, v2) )
return 0xFFFFFFFFLL;
pollconn_reset(a2);
*(_DWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v2) = -1;
*(_WORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v2 + 4) = 0;
return pollpool_freelist_enqueue(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // rbp

v2 = (a2 - *(_QWORD *)(a1 + 8)) >> 7;
if ( !(unsigned int)func2(a1, v2) )
return 0xFFFFFFFFLL;
pollconn_reset(a2);
*(_DWORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v2) = -1;
*(_WORD *)(*(_QWORD *)(a1 + 16) + 8LL * (int)v2 + 4) = 0;
return func1(a1, v2);
}

original_code:
int
pollpool_free(pollpool *pp, pollconn *c)
{
    int index;
    index=c-(pp->pollconns);
    if(!pollpool_index_allocated(pp, index))
        return -1;
    pollconn_reset(c);
    pp->ufds[index].fd=-1;
    pp->ufds[index].events=0;
    return pollpool_freelist_enqueue(pp, index);
}

canonical_original_code:
int pollpool_free(struct pollpool *pp, struct pollconn *c)
{
    int index;
    index=c-(pp->pollconns);
    if(!pollpool_index_allocated(pp, index))
        return -1;
    pollconn_reset(c);
    pp->ufds[index].fd=-1;
    pp->ufds[index].events=0;
    return pollpool_freelist_enqueue(pp, index);
}

variable_types:
c
struct pollconn *


decompiled_code:
int __fastcall pollpool_poll(struct pollfd **a1, int a2)
{
return poll(a1[2], *(int *)a1, a2);
}

canonical_decompiled_code:
int __fastcall func7(struct pollfd **a1, int a2)
{
return poll(a1[2], *(int *)a1, a2);
}

original_code:
int
pollpool_poll(pollpool *pp, int timeout)
{
    return poll(pp->ufds, pp->len, timeout);
}

canonical_original_code:
int pollpool_poll(struct pollpool *pp, int timeout)
{
    return poll(pp->ufds, pp->len, timeout);
}

variable_types: timeout int


decompiled_code:
__int64 __fastcall pollpool_index(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx

v2 = (a2 - *(_QWORD *)(a1 + 8)) >> 7;
if ( !(unsigned int)pollpool_index_allocated(a1, v2) )
v2 = -1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
unsigned int v2; // ebx

v2 = (a2 - *(_QWORD *)(a1 + 8)) >> 7;
if ( !(unsigned int)func2(a1, v2) )
v2 = -1;
return v2;
}

original_code:
int
pollpool_index(const pollpool *pp, const pollconn *c)
{
    int index;
    index=c-(pp->pollconns);
    if(!pollpool_index_allocated(pp, index))
        return -1;
    return index;
}

canonical_original_code:
int pollpool_index(struct pollpool *pp, struct pollconn *c)
{
    int index;
    index=c-(pp->pollconns);
    if(!pollpool_index_allocated(pp, index))
        return -1;
    return index;
}


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

if ( a1 == 3 )
return 3LL;
if ( a1 > 3 )
return 5 * (unsigned int)(a1 == 4);
result = 2LL;
if ( a1 != 1 )
result = 4 * (unsigned int)(a1 == 2);
return result;
}

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

if ( a1 == 3 )
return 3LL;
if ( a1 > 3 )
return 5 * (unsigned int)(a1 == 4);
result = 2LL;
if ( a1 != 1 )
result = 4 * (unsigned int)(a1 == 2);
return result;
}

original_code:
static pipeserver_event
translate_lineserver_event(lineserver_event e)
{
    switch(e)
    {
    case LINESERVER_CLOSED:
        return PIPESERVER_CLOSED;
    case LINESERVER_ERROR:
        return PIPESERVER_ERROR;
    case LINESERVER_SHUTDOWN:
        return PIPESERVER_CLOSING;
    case LINESERVER_LINE:
        return PIPESERVER_RECEIVED;
    default:
        return PIPESERVER_NULLEVENT;
    }
}

canonical_original_code:
static enum pipeserver_event translate_lineserver_event(enum lineserver_event e)
{
    switch(e)
    {
    case LINESERVER_CLOSED:
        return PIPESERVER_CLOSED;
    case LINESERVER_ERROR:
        return PIPESERVER_ERROR;
    case LINESERVER_SHUTDOWN:
        return PIPESERVER_CLOSING;
    case LINESERVER_LINE:
        return PIPESERVER_RECEIVED;
    default:
        return PIPESERVER_NULLEVENT;
    }
}

return_type: 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 }

user_defined_types: enum lineserver_event { LINESERVER_NULLEVENT=0, LINESERVER_CLOSED=1, LINESERVER_ERROR=2, LINESERVER_SHUTDOWN=3, LINESERVER_LINE=4 }


decompiled_code:
void *__fastcall pipeserver_construct(__int64 a1, __int64 a2, __int64 a3, int a4, int a5, unsigned int a6, unsigned int a7, unsigned int a8, unsigned int a9)
{
int v10; // ebx
int v11; // er12
int v12; // ebx
void *result; // rax

v10 = 0;
if ( a5 < 0 )
a5 = 0;
v11 = a5;
if ( a4 >= 0 )
v10 = a4;
lineserver_construct(a1, a2, a3, (unsigned int)v10);
linepiper_construct(a1 + 216, a6, a7, a8, a9);
*(_DWORD *)(a1 + 264) = -1;
*(_DWORD *)(a1 + 276) = 0;
*(_DWORD *)(a1 + 280) = 0;
if ( v10 < v11 )
v10 = v11;
v12 = v10 + 20;
*(_DWORD *)(a1 + 328) = v12;
result = malloc(v12);
*(_QWORD *)(a1 + 320) = result;
*(_QWORD *)(a1 + 288) = 0LL;
*(_DWORD *)(a1 + 296) = 0;
*(_QWORD *)(a1 + 304) = 0LL;
*(_DWORD *)(a1 + 312) = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func3(__int64 a1, __int64 a2, __int64 a3, int a4, int a5, unsigned int a6, unsigned int a7, unsigned int a8, unsigned int a9)
{
int v10; // ebx
int v11; // er12
int v12; // ebx
void *result; // rax

v10 = 0;
if ( a5 < 0 )
a5 = 0;
v11 = a5;
if ( a4 >= 0 )
v10 = a4;
lineserver_construct(a1, a2, a3, (unsigned int)v10);
linepiper_construct(a1 + 216, a6, a7, a8, a9);
*(_DWORD *)(a1 + 264) = -1;
*(_DWORD *)(a1 + 276) = 0;
*(_DWORD *)(a1 + 280) = 0;
if ( v10 < v11 )
v10 = v11;
v12 = v10 + 20;
*(_DWORD *)(a1 + 328) = v12;
result = malloc(v12);
*(_QWORD *)(a1 + 320) = result;
*(_QWORD *)(a1 + 288) = 0LL;
*(_DWORD *)(a1 + 296) = 0;
*(_QWORD *)(a1 + 304) = 0LL;
*(_DWORD *)(a1 + 312) = 0;
return result;
}

original_code:
void
pipeserver_construct(pipeserver *p, struct pollfd *ufds, int len,
                     int sendbuf_len, int recvbuf_len,
                     int fd_in, int fd_out,
                     int inbuf_len, int outbuf_len)
{
    if(recvbuf_len<0)
        recvbuf_len=0;
    if(sendbuf_len<0)
        sendbuf_len=0;
    lineserver_construct(&p->ls, ufds, len, sendbuf_len, recvbuf_len);
    linepiper_construct(&p->lp, fd_in, fd_out, inbuf_len, outbuf_len);
    p->accepted=-1;
    p->in_event=PIPESERVER_NULLEVENT;
    p->out_event=PIPESERVER_NULLEVENT;
    p->msg_len=(sendbuf_len>recvbuf_len?sendbuf_len:recvbuf_len)+msg_bytes;
    p->msg=(char*)malloc(p->msg_len);
    p->in_data=((void *)0);
    p->in_datalen=0;
    p->out_data=((void *)0);
    p->out_datalen=0;
}

canonical_original_code:
void pipeserver_construct(struct pipeserver *p, struct pollfd *ufds, int len, int sendbuf_len, int recvbuf_len, int fd_in, int fd_out, int inbuf_len, int outbuf_len)
{
    if(recvbuf_len<0)
        recvbuf_len=0;
    if(sendbuf_len<0)
        sendbuf_len=0;
    lineserver_construct(&p->ls, ufds, len, sendbuf_len, recvbuf_len);
    linepiper_construct(&p->lp, fd_in, fd_out, inbuf_len, outbuf_len);
    p->accepted=-1;
    p->in_event=PIPESERVER_NULLEVENT;
    p->out_event=PIPESERVER_NULLEVENT;
    p->msg_len=(sendbuf_len>recvbuf_len?sendbuf_len:recvbuf_len)+msg_bytes;
    p->msg=(char *)malloc(p->msg_len);
    p->in_data=((void *)0);
    p->in_datalen=0;
    p->out_data=((void *)0);
    p->out_datalen=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: len 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; }


decompiled_code:
void __fastcall pipeserver_destruct(__int64 a1)
{
lineserver_destruct();
linepiper_destruct(a1 + 216);
free(*(void **)(a1 + 320));
*(_QWORD *)(a1 + 320) = 0LL;
*(_DWORD *)(a1 + 328) = 0;
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
lineserver_destruct();
linepiper_destruct(a1 + 216);
free(*(void **)(a1 + 320));
*(_QWORD *)(a1 + 320) = 0LL;
*(_DWORD *)(a1 + 328) = 0;
}

original_code:
void
pipeserver_destruct(pipeserver *p)
{
    lineserver_destruct(&p->ls);
    linepiper_destruct(&p->lp);
    free(p->msg);
    p->msg=((void *)0);
    p->msg_len=0;
}

canonical_original_code:
void pipeserver_destruct(struct pipeserver *p)
{
    lineserver_destruct(&p->ls);
    linepiper_destruct(&p->lp);
    free(p->msg);
    p->msg=((void *)0);
    p->msg_len=0;
}

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

user_defined_types: 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 }


decompiled_code:
__int64 __fastcall pipeserver_listen(__int64 a1, unsigned __int16 a2)
{
return lineserver_listen(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int16 a2)
{
return lineserver_listen(a1, a2);
}

original_code:
int
pipeserver_listen(pipeserver *p, u_int16_t port)
{
    return lineserver_listen(&p->ls, port);
}

canonical_original_code:
int pipeserver_listen(struct pipeserver *p, unsigned int port)
{
    return lineserver_listen(&p->ls, port);
}

variable_types:
port
unsigned int


decompiled_code:
__int64 pipeserver_bind()
{
return lineserver_bind();
}

canonical_decompiled_code:
__int64 func6()
{
return lineserver_bind();
}

original_code:
int
pipeserver_bind(pipeserver *p, u_int32_t ip_addr)
{
    return lineserver_bind(&p->ls, ip_addr);
}

canonical_original_code:
int pipeserver_bind(struct pipeserver *p, unsigned int ip_addr)
{
    return lineserver_bind(&p->ls, ip_addr);
}


decompiled_code:
__int64 __fastcall pipeserver_operate(_DWORD *a1)
{
lineserver_operate();
a1[66] = lineserver_accept(a1);
return pipeserver_update_in(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
lineserver_operate();
a1[66] = lineserver_accept(a1);
return func1(a1);
}

original_code:
void
pipeserver_operate(pipeserver *p)
{
    lineserver_operate(&p->ls);
    p->accepted=lineserver_accept(&p->ls);
    pipeserver_update_in(p);
}

canonical_original_code:
void pipeserver_operate(struct pipeserver *p)
{
    lineserver_operate(&p->ls);
    p->accepted=lineserver_accept(&p->ls);
    pipeserver_update_in(p);
}


decompiled_code:
__int64 __fastcall pipeserver_read(__int64 a1)
{
int v1; // ebx

v1 = linepiper_read(a1 + 216);
if ( v1 >= 0 )
pipeserver_update_out(a1);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int v1; // ebx

v1 = linepiper_read(a1 + 216);
if ( v1 >= 0 )
func2(a1);
return (unsigned int)v1;
}

original_code:
int
pipeserver_read(pipeserver *p)
{
    int ret;
    ret=linepiper_read(&p->lp);
    if(ret<0)
        return ret;
    pipeserver_update_out(p);
    return ret;
}

canonical_original_code:
int pipeserver_read(struct pipeserver *p)
{
    int ret;
    ret=linepiper_read(&p->lp);
    if(ret<0)
        return ret;
    pipeserver_update_out(p);
    return ret;
}


decompiled_code:
__int64 __fastcall pipeserver_write(__int64 a1)
{
return linepiper_write(a1 + 216);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return linepiper_write(a1 + 216);
}

original_code:
int
pipeserver_write(pipeserver *p)
{
    return linepiper_write(&p->lp);
}

canonical_original_code:
int pipeserver_write(struct pipeserver *p)
{
    return linepiper_write(&p->lp);
}


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

result = 0LL;
if ( *(_DWORD *)(a1 + 276) )
{
if ( a2 )
*a2 = *(_DWORD *)(a1 + 268);
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 276);
result = 1LL;
}
}
return result;
}

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

result = 0LL;
if ( *(_DWORD *)(a1 + 276) )
{
if ( a2 )
*a2 = *(_DWORD *)(a1 + 268);
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 276);
result = 1LL;
}
}
return result;
}

original_code:
int
pipeserver_event_in(const pipeserver *p,
                    pollset_handle *h, pipeserver_event *e)
{
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    if(h)
        *h=p->in_conn;
    if(e)
        *e=p->in_event;
    return 1;
}

canonical_original_code:
int pipeserver_event_in(struct pipeserver *p, int *h, enum pipeserver_event *e)
{
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    if(h)
        *h=p->in_conn;
    if(e)
        *e=p->in_event;
    return 1;
}

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


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

result = 0LL;
if ( *(_DWORD *)(a1 + 280) )
{
if ( a2 )
*a2 = *(_DWORD *)(a1 + 272);
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 280);
result = 1LL;
}
}
return result;
}

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

result = 0LL;
if ( *(_DWORD *)(a1 + 280) )
{
if ( a2 )
*a2 = *(_DWORD *)(a1 + 272);
result = 1LL;
if ( a3 )
{
*a3 = *(_DWORD *)(a1 + 280);
result = 1LL;
}
}
return result;
}

original_code:
int
pipeserver_event_out(const pipeserver *p,
                     pollset_handle *h, pipeserver_event *e)
{
    if(p->out_event==PIPESERVER_NULLEVENT)
        return 0;
    if(h)
        *h=p->out_conn;
    if(e)
        *e=p->out_event;
    return 1;
}

canonical_original_code:
int pipeserver_event_out(struct pipeserver *p, int *h, enum pipeserver_event *e)
{
    if(p->out_event==PIPESERVER_NULLEVENT)
        return 0;
    if(h)
        *h=p->out_conn;
    if(e)
        *e=p->out_event;
    return 1;
}


decompiled_code:
__int64 __fastcall pipeserver_line_in(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)(a1 + 276) == 5 )
result = lineserver_getline(a1, *(unsigned int *)(a1 + 268), a3, a4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)(a1 + 276) == 5 )
result = lineserver_getline(a1, *(unsigned int *)(a1 + 268), a3, a4);
return result;
}

original_code:
int
pipeserver_line_in(const pipeserver *p, pollset_handle *h,
                   const char **data, int *len)
{
    if(p->in_event!=PIPESERVER_RECEIVED)
        return 0;
    return lineserver_getline(&p->ls, p->in_conn, data, len);
}

canonical_original_code:
int pipeserver_line_in(struct pipeserver *p, int *h, char **data, int *len)
{
    if(p->in_event!=PIPESERVER_RECEIVED)
        return 0;
    return lineserver_getline(&p->ls, p->in_conn, data, len);
}

variable_types:
data
char **


decompiled_code:
__int64 __fastcall pipeserver_line_out(__int64 a1, __int64 a2, _QWORD *a3, _DWORD *a4)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)(a1 + 280) == 7 )
{
if ( a3 )
*a3 = *(_QWORD *)(a1 + 304);
result = 1LL;
if ( a4 )
{
*a4 = *(_DWORD *)(a1 + 312);
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2, _QWORD *a3, _DWORD *a4)
{
__int64 result; // rax

result = 0LL;
if ( *(_DWORD *)(a1 + 280) == 7 )
{
if ( a3 )
*a3 = *(_QWORD *)(a1 + 304);
result = 1LL;
if ( a4 )
{
*a4 = *(_DWORD *)(a1 + 312);
result = 1LL;
}
}
return result;
}

original_code:
int
pipeserver_line_out(const pipeserver *p, pollset_handle *h,
                    const char **data, int *len)
{
    if(p->out_event!=PIPESERVER_SEND)
        return 0;
    if(data)
        *data=p->out_data;
    if(len)
        *len=p->out_datalen;
    return 1;
}

canonical_original_code:
int pipeserver_line_out(struct pipeserver *p, int *h, char **data, int *len)
{
    if(p->out_event!=PIPESERVER_SEND)
        return 0;
    if(data)
        *data=p->out_data;
    if(len)
        *len=p->out_datalen;
    return 1;
}

variable_types:
len
int *


decompiled_code:
__int64 pipeserver_error()
{
return lineserver_geterror();
}

canonical_decompiled_code:
__int64 func14()
{
return lineserver_geterror();
}

original_code:
int
pipeserver_error(const pipeserver *p, pollset_handle h)
{
    return lineserver_geterror(&p->ls, h);
}

canonical_original_code:
int pipeserver_error(struct pipeserver *p, int h)
{
    return lineserver_geterror(&p->ls, h);
}

variable_types: h int


decompiled_code:
__int64 __fastcall pipeserver_enqueue_in(__int64 a1)
{
__int64 v1; // rdx
__int64 result; // rax
int v3; // eax

v1 = *(unsigned int *)(a1 + 276);
result = 0LL;
if ( (_DWORD)v1 )
{
v3 = pipeserver_msg_build(
*(_QWORD *)(a1 + 320),
*(unsigned int *)(a1 + 328),
v1,
*(unsigned int *)(a1 + 268),
*(_QWORD *)(a1 + 288),
*(unsigned int *)(a1 + 296));
if ( v3 <= 0 )
result = 0xFFFFFFFFLL;
else
result = linepiper_enqueue(a1 + 216, *(_QWORD *)(a1 + 320), (unsigned int)v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
__int64 v1; // rdx
__int64 result; // rax
int v3; // eax

v1 = *(unsigned int *)(a1 + 276);
result = 0LL;
if ( (_DWORD)v1 )
{
v3 = pipeserver_msg_build(
*(_QWORD *)(a1 + 320),
*(unsigned int *)(a1 + 328),
v1,
*(unsigned int *)(a1 + 268),
*(_QWORD *)(a1 + 288),
*(unsigned int *)(a1 + 296));
if ( v3 <= 0 )
result = 0xFFFFFFFFLL;
else
result = linepiper_enqueue(a1 + 216, *(_QWORD *)(a1 + 320), (unsigned int)v3);
}
return result;
}

original_code:
int
pipeserver_enqueue_in(pipeserver *p)
{
    int ret;
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    ret=pipeserver_msg_build(p->msg, p->msg_len, p->in_event, p->in_conn,
                             p->in_data, p->in_datalen);
    if(ret<=0)
        return -1;
    return linepiper_enqueue(&p->lp, p->msg, ret);
}

canonical_original_code:
int pipeserver_enqueue_in(struct pipeserver *p)
{
    int ret;
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    ret=pipeserver_msg_build(p->msg, p->msg_len, p->in_event, p->in_conn,
                             p->in_data, p->in_datalen);
    if(ret<=0)
        return -1;
    return linepiper_enqueue(&p->lp, p->msg, ret);
}


decompiled_code:
__int64 __fastcall pipeserver_dequeue_out(__int64 a1)
{
unsigned int v1; // ebp

v1 = linepiper_dequeue(a1 + 216);
pipeserver_update_out(a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
unsigned int v1; // ebp

v1 = linepiper_dequeue(a1 + 216);
func2(a1);
return v1;
}

original_code:
int
pipeserver_dequeue_out(pipeserver *p)
{
    int ret;
    ret=linepiper_dequeue(&p->lp);
    pipeserver_update_out(p);
    return ret;
}

canonical_original_code:
int pipeserver_dequeue_out(struct pipeserver *p)
{
    int ret;
    ret=linepiper_dequeue(&p->lp);
    pipeserver_update_out(p);
    return ret;
}


decompiled_code:
__int64 __fastcall pipeserver_dequeue_in(_DWORD *a1)
{
int v1; // eax
unsigned int v2; // ebp

v1 = a1[69];
v2 = 0;
if ( v1 )
{
if ( v1 == 1 )
{
a1[69] = 0;
a1[67] = -1;
v2 = 1;
}
else
{
v2 = lineserver_dequeue(a1, (unsigned int)a1[67]);
pipeserver_update_in(a1);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func17(_DWORD *a1)
{
int v1; // eax
unsigned int v2; // ebp

v1 = a1[69];
v2 = 0;
if ( v1 )
{
if ( v1 == 1 )
{
a1[69] = 0;
a1[67] = -1;
v2 = 1;
}
else
{
v2 = lineserver_dequeue(a1, (unsigned int)a1[67]);
func1(a1);
}
}
return v2;
}

original_code:
int
pipeserver_dequeue_in(pipeserver *p)
{
    int ret;
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    if(p->in_event==PIPESERVER_ACCEPTED)
    {
        p->in_event=PIPESERVER_NULLEVENT;
        p->in_conn=-1;
        return 1;
    }
    ret=lineserver_dequeue(&p->ls, p->in_conn);
    pipeserver_update_in(p);
    return ret;
}

canonical_original_code:
int pipeserver_dequeue_in(struct pipeserver *p)
{
    int ret;
    if(p->in_event==PIPESERVER_NULLEVENT)
        return 0;
    if(p->in_event==PIPESERVER_ACCEPTED)
    {
        p->in_event=PIPESERVER_NULLEVENT;
        p->in_conn=-1;
        return 1;
    }
    ret=lineserver_dequeue(&p->ls, p->in_conn);
    pipeserver_update_in(p);
    return ret;
}


decompiled_code:
__int64 __fastcall pipeserver_process_in(_DWORD *a1)
{
int v1; // ebx

v1 = pipeserver_enqueue_in((__int64)a1);
if ( v1 > 0 )
pipeserver_dequeue_in(a1);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func18(_DWORD *a1)
{
int v1; // ebx

v1 = func15((__int64)a1);
if ( v1 > 0 )
func17(a1);
return (unsigned int)v1;
}

original_code:
int
pipeserver_process_in(pipeserver *p)
{
    int ret;
    ret=pipeserver_enqueue_in(p);
    if(ret>0)
        pipeserver_dequeue_in(p);
    return ret;
}

canonical_original_code:
int pipeserver_process_in(struct pipeserver *p)
{
    int ret;
    ret=pipeserver_enqueue_in(p);
    if(ret>0)
        pipeserver_dequeue_in(p);
    return ret;
}


decompiled_code:
__int64 __fastcall pipeserver_enqueue_out(__int64 a1)
{
int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 280);
if ( v1 == 7 )
{
lineserver_enqueue(a1, *(unsigned int *)(a1 + 272), *(_QWORD *)(a1 + 304), *(unsigned int *)(a1 + 312));
v2 = 1;
}
else if ( v1 == 8 )
{
lineserver_shutdown(a1, *(unsigned int *)(a1 + 272));
v2 = 1;
}
else
{
v2 = 0;
if ( v1 == 6 )
{
lineserver_close(a1, *(unsigned int *)(a1 + 272), 0LL);
v2 = 1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
int v1; // eax
unsigned int v2; // edx

v1 = *(_DWORD *)(a1 + 280);
if ( v1 == 7 )
{
lineserver_enqueue(a1, *(unsigned int *)(a1 + 272), *(_QWORD *)(a1 + 304), *(unsigned int *)(a1 + 312));
v2 = 1;
}
else if ( v1 == 8 )
{
lineserver_shutdown(a1, *(unsigned int *)(a1 + 272));
v2 = 1;
}
else
{
v2 = 0;
if ( v1 == 6 )
{
lineserver_close(a1, *(unsigned int *)(a1 + 272), 0LL);
v2 = 1;
}
}
return v2;
}

original_code:
int
pipeserver_enqueue_out(pipeserver *p)
{
    switch(p->out_event)
    {
    case PIPESERVER_CLOSE:
        lineserver_close(&p->ls, p->out_conn);
        break;
    case PIPESERVER_SHUTDOWN:
        lineserver_shutdown(&p->ls, p->out_conn);
        break;
    case PIPESERVER_SEND:
        lineserver_enqueue(&p->ls, p->out_conn,
            p->out_data, p->out_datalen);
        break;
    default:
        return 0;
    }
    return 1;
}

canonical_original_code:
int pipeserver_enqueue_out(struct pipeserver *p)
{
    switch(p->out_event)
    {
    case PIPESERVER_CLOSE:
        lineserver_close(&p->ls, p->out_conn);
        break;
    case PIPESERVER_SHUTDOWN:
        lineserver_shutdown(&p->ls, p->out_conn);
        break;
    case PIPESERVER_SEND:
        lineserver_enqueue(&p->ls, p->out_conn,
            p->out_data, p->out_datalen);
        break;
    default:
        return 0;
    }
    return 1;
}


decompiled_code:
__int64 __fastcall pipeserver_process_out(__int64 a1)
{
int v1; // ebx

v1 = pipeserver_enqueue_out(a1);
if ( v1 > 0 )
pipeserver_dequeue_out(a1);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1)
{
int v1; // ebx

v1 = func19(a1);
if ( v1 > 0 )
func16(a1);
return (unsigned int)v1;
}

original_code:
int
pipeserver_process_out(pipeserver *p)
{
    int ret;
    ret=pipeserver_enqueue_out(p);
    if(ret>0)
        pipeserver_dequeue_out(p);
    return ret;
}

canonical_original_code:
int pipeserver_process_out(struct pipeserver *p)
{
    int ret;
    ret=pipeserver_enqueue_out(p);
    if(ret>0)
        pipeserver_dequeue_out(p);
    return ret;
}


decompiled_code:
__int64 __fastcall pipeserver_enqueue_event(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, unsigned int a5)
{
int v5; // eax
__int64 result; // rax

v5 = pipeserver_msg_build(*(_QWORD *)(a1 + 320), *(unsigned int *)(a1 + 328), a2, a3, a4, a5);
if ( v5 < 0 )
result = 0xFFFFFFFFLL;
else
result = linepiper_enqueue(a1 + 216, *(_QWORD *)(a1 + 320), (unsigned int)v5);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, unsigned int a5)
{
int v5; // eax
__int64 result; // rax

v5 = pipeserver_msg_build(*(_QWORD *)(a1 + 320), *(unsigned int *)(a1 + 328), a2, a3, a4, a5);
if ( v5 < 0 )
result = 0xFFFFFFFFLL;
else
result = linepiper_enqueue(a1 + 216, *(_QWORD *)(a1 + 320), (unsigned int)v5);
return result;
}

original_code:
int
pipeserver_enqueue_event(pipeserver *p,
                         pipeserver_event e, pollset_handle h,
                         const char *data, int len)
{
    int ret;
    ret=pipeserver_msg_build(p->msg, p->msg_len, e, h, data, len);
    if(ret<0)
        return -1;
    return linepiper_enqueue(&p->lp, p->msg, ret);
}

canonical_original_code:
int pipeserver_enqueue_event(struct pipeserver *p, enum pipeserver_event e, int h, char *data, int len)
{
    int ret;
    ret=pipeserver_msg_build(p->msg, p->msg_len, e, h, data, len);
    if(ret<0)
        return -1;
    return linepiper_enqueue(&p->lp, p->msg, ret);
}

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

variable_types:
data
char *


decompiled_code:
__int64 __fastcall pipeserver_enqueue_line(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return lineserver_enqueue(a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
return lineserver_enqueue(a1, a2, a3, a4);
}

original_code:
int
pipeserver_enqueue_line(pipeserver *p, pollset_handle h,
                            const char *data, int len)
{
    return lineserver_enqueue(&p->ls, h, data, len);
}

canonical_original_code:
int pipeserver_enqueue_line(struct pipeserver *p, int h, char *data, int len)
{
    return lineserver_enqueue(&p->ls, h, data, len);
}


decompiled_code:
__int64 __fastcall pipeserver_shutdown(__int64 a1, __int64 a2)
{
return lineserver_shutdown(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, __int64 a2)
{
return lineserver_shutdown(a1, a2);
}

original_code:
int
pipeserver_shutdown(pipeserver *p, pollset_handle h)
{
    return lineserver_shutdown(&p->ls, h);
}

canonical_original_code:
int pipeserver_shutdown(struct pipeserver *p, int h)
{
    return lineserver_shutdown(&p->ls, h);
}


decompiled_code:
__int64 __fastcall pipeserver_close(__int64 a1, __int64 a2, __int64 a3)
{
return lineserver_close(a1, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1, __int64 a2, __int64 a3)
{
return lineserver_close(a1, a2, a3);
}

original_code:
int
pipeserver_close(pipeserver *p, pollset_handle h)
{
    return lineserver_close(&p->ls, h);
}

canonical_original_code:
int pipeserver_close(struct pipeserver *p, int h)
{
    return lineserver_close(&p->ls, h);
}


decompiled_code:
__int64 __fastcall pollsock_construct(__int64 a1, _DWORD *a2, unsigned int a3, unsigned int a4)
{
*(_QWORD *)a1 = a2;
*a2 = -1;
*(_WORD *)(*(_QWORD *)a1 + 4LL) = 0;
*(_WORD *)(*(_QWORD *)a1 + 6LL) = 0;
tcpbuf_construct(a1 + 8, a3);
return tcpbuf_construct(a1 + 24, a4);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, _DWORD *a2, unsigned int a3, unsigned int a4)
{
*(_QWORD *)a1 = a2;
*a2 = -1;
*(_WORD *)(*(_QWORD *)a1 + 4LL) = 0;
*(_WORD *)(*(_QWORD *)a1 + 6LL) = 0;
tcpbuf_construct(a1 + 8, a3);
return tcpbuf_construct(a1 + 24, a4);
}

original_code:
void
pollsock_construct(pollsock *s,
                   struct pollfd *pfd, int sendbuf_len, int recvbuf_len)
{
    s->pfd=pfd;
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    tcpbuf_construct(&s->sendbuf, sendbuf_len);
    tcpbuf_construct(&s->recvbuf, recvbuf_len);
}

canonical_original_code:
void pollsock_construct(struct pollsock *s, struct pollfd *pfd, int sendbuf_len, int recvbuf_len)
{
    s->pfd=pfd;
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    tcpbuf_construct(&s->sendbuf, sendbuf_len);
    tcpbuf_construct(&s->recvbuf, recvbuf_len);
}

variable_types: struct pollsock { struct pollfd { int fd; short events; short revents; } *pfd; struct tcpbuf { int len; int pos; unsigned char *data; } sendbuf; struct tcpbuf recvbuf; } *

variable_types:
pfd
struct pollfd *


decompiled_code:
__int64 __fastcall pollsock_destruct(__int64 a1)
{
tcpbuf_destruct(a1 + 8);
return tcpbuf_destruct(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
tcpbuf_destruct(a1 + 8);
return tcpbuf_destruct(a1 + 24);
}

original_code:
void
pollsock_destruct(pollsock *s)
{
    tcpbuf_destruct(&s->sendbuf);
    tcpbuf_destruct(&s->recvbuf);
}

canonical_original_code:
void pollsock_destruct(struct pollsock *s)
{
    tcpbuf_destruct(&s->sendbuf);
    tcpbuf_destruct(&s->recvbuf);
}


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

**(_DWORD **)a1 = -1;
*(_WORD *)(*(_QWORD *)a1 + 4LL) = 0;
*(_WORD *)(*(_QWORD *)a1 + 6LL) = 0;
v1 = tcpbuf_filled(a1 + 8);
tcpbuf_dequeue(a1 + 8, v1);
v2 = tcpbuf_filled(a1 + 24);
return tcpbuf_dequeue(a1 + 24, v2);
}

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

**(_DWORD **)a1 = -1;
*(_WORD *)(*(_QWORD *)a1 + 4LL) = 0;
*(_WORD *)(*(_QWORD *)a1 + 6LL) = 0;
v1 = tcpbuf_filled(a1 + 8);
tcpbuf_dequeue(a1 + 8, v1);
v2 = tcpbuf_filled(a1 + 24);
return tcpbuf_dequeue(a1 + 24, v2);
}

original_code:
void
pollsock_reset(pollsock *s)
{
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    tcpbuf_dequeue(&s->sendbuf, tcpbuf_filled(&s->sendbuf));
    tcpbuf_dequeue(&s->recvbuf, tcpbuf_filled(&s->recvbuf));
}

canonical_original_code:
void pollsock_reset(struct pollsock *s)
{
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    tcpbuf_dequeue(&s->sendbuf, tcpbuf_filled(&s->sendbuf));
    tcpbuf_dequeue(&s->recvbuf, tcpbuf_filled(&s->recvbuf));
}


decompiled_code:
__int64 __fastcall pollsock_revents(__int64 a1, int a2)
{
return a2 & (unsigned int)*(__int16 *)(*(_QWORD *)a1 + 6LL);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
return a2 & (unsigned int)*(__int16 *)(*(_QWORD *)a1 + 6LL);
}

original_code:
int
pollsock_revents(const pollsock *s, int event)
{
    return s->pfd->revents&event;
}

canonical_original_code:
int pollsock_revents(struct pollsock *s, int event)
{
    return s->pfd->revents&event;
}

variable_types: event int


decompiled_code:
__int64 __fastcall pollsock_socket(int **a1)
{
int *v1; // rbp

v1 = *a1;
*v1 = socket(2, 1, 6);
return (unsigned int)**a1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int **a1)
{
int *v1; // rbp

v1 = *a1;
*v1 = socket(2, 1, 6);
return (unsigned int)**a1;
}

original_code:
int
pollsock_socket(pollsock *s)
{
    s->pfd->fd=socket(2, SOCK_STREAM, IPPROTO_TCP);
    return s->pfd->fd;
}

canonical_original_code:
int pollsock_socket(struct pollsock *s)
{
    s->pfd->fd=socket(2, SOCK_STREAM, IPPROTO_TCP);
    return s->pfd->fd;
}


decompiled_code:
int __fastcall pollsock_bind(int **a1, const struct sockaddr *a2)
{
return bind(**a1, a2, 0x10u);
}

canonical_decompiled_code:
int __fastcall func5(int **a1, const struct sockaddr *a2)
{
return bind(**a1, a2, 0x10u);
}

original_code:
int
pollsock_bind(pollsock *s, const struct sockaddr_in *addr)
{
    return bind(s->pfd->fd, (const struct sockaddr*)addr, sizeof(*addr));
}

canonical_original_code:
int pollsock_bind(struct pollsock *s, struct sockaddr_in *addr)
{
    return bind(s->pfd->fd, (struct sockaddr *)addr, sizeof(*addr));
}

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

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


decompiled_code:
int __fastcall pollsock_listen(int **a1, int a2)
{
int result; // eax

result = listen(**a1, a2);
if ( !result )
*((_WORD *)*a1 + 2) |= 1u;
return result;
}

canonical_decompiled_code:
int __fastcall func6(int **a1, int a2)
{
int result; // eax

result = listen(**a1, a2);
if ( !result )
*((_WORD *)*a1 + 2) |= 1u;
return result;
}

original_code:
int
pollsock_listen(pollsock *s, int backlog)
{
    int ret;
    if((ret=listen(s->pfd->fd, backlog))==0)
        s->pfd->events|=0x001;
    return ret;
}

canonical_original_code:
int pollsock_listen(struct pollsock *s, int backlog)
{
    int ret;
    if((ret=listen(s->pfd->fd, backlog))==0)
        s->pfd->events|=0x001;
    return ret;
}

variable_types: backlog int

variable_types: ret int


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

*(_WORD *)(*a1 + 4) = 0;
if ( (int)tcpbuf_filled(a1 + 1) > 0 )
*(_WORD *)(*a1 + 4) |= 4u;
result = tcpbuf_space(a1 + 3);
if ( (int)result > 0 )
{
result = *a1;
*(_WORD *)(*a1 + 4) |= 1u;
}
return result;
}

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

*(_WORD *)(*a1 + 4) = 0;
if ( (int)tcpbuf_filled(a1 + 1) > 0 )
*(_WORD *)(*a1 + 4) |= 4u;
result = tcpbuf_space(a1 + 3);
if ( (int)result > 0 )
{
result = *a1;
*(_WORD *)(*a1 + 4) |= 1u;
}
return result;
}

original_code:
void
pollsock_established(pollsock *s)
{
    s->pfd->events=0;
    if(tcpbuf_filled(&s->sendbuf)>0)
        s->pfd->events|=0x004;
    if(tcpbuf_space(&s->recvbuf)>0)
        s->pfd->events|=0x001;
}

canonical_original_code:
void pollsock_established(struct pollsock *s)
{
    s->pfd->events=0;
    if(tcpbuf_filled(&s->sendbuf)>0)
        s->pfd->events|=0x004;
    if(tcpbuf_space(&s->recvbuf)>0)
        s->pfd->events|=0x001;
}


decompiled_code:
__int64 __fastcall pollsock_accepted(__int64 *a1, int a2)
{
*(_DWORD *)*a1 = a2;
return pollsock_established(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 *a1, int a2)
{
*(_DWORD *)*a1 = a2;
return func7(a1);
}

original_code:
void
pollsock_accepted(pollsock *s, int sock)
{
    s->pfd->fd=sock;
    pollsock_established(s);
}

canonical_original_code:
void pollsock_accepted(struct pollsock *s, int sock)
{
    s->pfd->fd=sock;
    pollsock_established(s);
}

variable_types: sock int


decompiled_code:
__int64 __fastcall pollsock_accept(int **a1, __int64 *a2, struct sockaddr *a3, socklen_t *a4)
{
int v4; // eax
unsigned int v5; // ebx

if ( a3 && a4 )
*a4 = 16;
v4 = accept(**a1, a3, a4);
v5 = v4;
if ( v4 != -1 && a2 )
pollsock_accepted(a2, v4);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(int **a1, __int64 *a2, struct sockaddr *a3, socklen_t *a4)
{
int v4; // eax
unsigned int v5; // ebx

if ( a3 && a4 )
*a4 = 16;
v4 = accept(**a1, a3, a4);
v5 = v4;
if ( v4 != -1 && a2 )
func8(a2, v4);
return v5;
}

original_code:
int
pollsock_accept(pollsock *s, pollsock *n,
                struct sockaddr_in *addr, socklen_t *addrlen)
{
    int ret;
    if(addr && addrlen)
        *addrlen=sizeof(*addr);
    ret=accept(s->pfd->fd, (struct sockaddr*)addr, addrlen);
    if(ret!=-1 && n)
        pollsock_accepted(n, ret);
    return ret;
}

canonical_original_code:
int pollsock_accept(struct pollsock *s, struct pollsock *n, struct sockaddr_in *addr, unsigned int *addrlen)
{
    int ret;
    if(addr && addrlen)
        *addrlen=sizeof(*addr);
    ret=accept(s->pfd->fd, (struct sockaddr *)addr, addrlen);
    if(ret!=-1 && n)
        pollsock_accepted(n, ret);
    return ret;
}

variable_types:
n
struct pollsock *

variable_types:
addrlen
unsigned int *


decompiled_code:
__int64 __fastcall pollsock_connect(int **a1, const struct sockaddr *a2)
{
unsigned int v2; // ebx

v2 = connect(**a1, a2, 0x10u);
if ( v2 )
*((_WORD *)*a1 + 2) |= 4u;
else
pollsock_established((__int64 *)a1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(int **a1, const struct sockaddr *a2)
{
unsigned int v2; // ebx

v2 = connect(**a1, a2, 0x10u);
if ( v2 )
*((_WORD *)*a1 + 2) |= 4u;
else
func7((__int64 *)a1);
return v2;
}

original_code:
int
pollsock_connect(pollsock *s, const struct sockaddr_in *addr)
{
    int ret;
    ret=connect(s->pfd->fd, (const struct sockaddr*)addr, sizeof(*addr));
    if(ret==0)
        pollsock_established(s);
    else
        s->pfd->events|=0x004;
    return ret;
}

canonical_original_code:
int pollsock_connect(struct pollsock *s, struct sockaddr_in *addr)
{
    int ret;
    ret=connect(s->pfd->fd, (struct sockaddr *)addr, sizeof(*addr));
    if(ret==0)
        pollsock_established(s);
    else
        s->pfd->events|=0x004;
    return ret;
}


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

v1 = send(**(_DWORD **)a1, *(const void **)(a1 + 16), *(int *)(a1 + 12), 0);
v2 = v1;
if ( v1 >= 0 )
{
tcpbuf_dequeue(a1 + 8, (unsigned int)v1);
if ( (unsigned int)tcpbuf_empty(a1 + 8) )
*(_WORD *)(*(_QWORD *)a1 + 4LL) &= 0xFFFBu;
}
return v2;
}

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

v1 = send(**(_DWORD **)a1, *(const void **)(a1 + 16), *(int *)(a1 + 12), 0);
v2 = v1;
if ( v1 >= 0 )
{
tcpbuf_dequeue(a1 + 8, (unsigned int)v1);
if ( (unsigned int)tcpbuf_empty(a1 + 8) )
*(_WORD *)(*(_QWORD *)a1 + 4LL) &= 0xFFFBu;
}
return v2;
}

original_code:
int
pollsock_send(pollsock *s)
{
    int ret;
    ret=send(s->pfd->fd, s->sendbuf.data, s->sendbuf.pos, 0);
    if(ret>=0)
    {
        tcpbuf_dequeue(&s->sendbuf, ret);
        if(tcpbuf_empty(&s->sendbuf))
            s->pfd->events&=~0x004;
    }
    return ret;
}

canonical_original_code:
int pollsock_send(struct pollsock *s)
{
    int ret;
    ret=send(s->pfd->fd, s->sendbuf.data, s->sendbuf.pos, 0);
    if(ret>=0)
    {
        tcpbuf_dequeue(&s->sendbuf, ret);
        if(tcpbuf_empty(&s->sendbuf))
            s->pfd->events&=~0x004;
    }
    return ret;
}


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

v1 = a1 + 3;
v2 = tcpbuf_space(a1 + 3);
v3 = tcpbuf_alloc(a1 + 3, v2, &buf);
v4 = recv(**a1, buf, v3, 0);
v5 = v4;
if ( v4 >= 0 )
{
tcpbuf_dealloc(v1, (unsigned int)(v3 - v4));
v7 = tcpbuf_full(v1);
if ( !v5 || v7 )
*((_WORD *)*a1 + 2) &= 0xFFFEu;
}
return v5;
}

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

v1 = a1 + 3;
v2 = tcpbuf_space(a1 + 3);
v3 = tcpbuf_alloc(a1 + 3, v2, &buf);
v4 = recv(**a1, buf, v3, 0);
v5 = v4;
if ( v4 >= 0 )
{
tcpbuf_dealloc(v1, (unsigned int)(v3 - v4));
v7 = tcpbuf_full(v1);
if ( !v5 || v7 )
*((_WORD *)*a1 + 2) &= 0xFFFEu;
}
return v5;
}

original_code:
int
pollsock_recv(pollsock *s)
{
    int ret, buflen;
    u_char *buf;
    buflen=tcpbuf_alloc(&s->recvbuf, tcpbuf_space(&s->recvbuf), &buf);
    ret=recv(s->pfd->fd, buf, buflen, 0);
    if(ret>=0)
    {
        tcpbuf_dealloc(&s->recvbuf, buflen-ret);
        if(tcpbuf_full(&s->recvbuf) || ret==0)
            s->pfd->events&=~0x001;
    }
    return ret;
}

canonical_original_code:
int pollsock_recv(struct pollsock *s)
{
    int ret; int buflen;
    unsigned char *buf;
    buflen=tcpbuf_alloc(&s->recvbuf, tcpbuf_space(&s->recvbuf), &buf);
    ret=recv(s->pfd->fd, buf, buflen, 0);
    if(ret>=0)
    {
        tcpbuf_dealloc(&s->recvbuf, buflen-ret);
        if(tcpbuf_full(&s->recvbuf) || ret==0)
            s->pfd->events&=~0x001;
    }
    return ret;
}

variable_types: buflen int

variable_types:
buf
unsigned char *


decompiled_code:
int __fastcall pollsock_shutdown(int **a1)
{
int result; // eax

result = shutdown(**a1, 1);
if ( !result )
*((_WORD *)*a1 + 2) &= 0xFFFBu;
return result;
}

canonical_decompiled_code:
int __fastcall func13(int **a1)
{
int result; // eax

result = shutdown(**a1, 1);
if ( !result )
*((_WORD *)*a1 + 2) &= 0xFFFBu;
return result;
}

original_code:
int
pollsock_shutdown(pollsock *s)
{
    int ret;
    ret=shutdown(s->pfd->fd, SHUT_WR);
    if(ret==0)
        s->pfd->events&=~0x004;
    return ret;
}

canonical_original_code:
int pollsock_shutdown(struct pollsock *s)
{
    int ret;
    ret=shutdown(s->pfd->fd, SHUT_WR);
    if(ret==0)
        s->pfd->events&=~0x004;
    return ret;
}


decompiled_code:
int __fastcall pollsock_close(int **a1)
{
int result; // eax

result = close(**a1);
**a1 = -1;
*((_WORD *)*a1 + 2) = 0;
*((_WORD *)*a1 + 3) = 0;
return result;
}

canonical_decompiled_code:
int __fastcall func14(int **a1)
{
int result; // eax

result = close(**a1);
**a1 = -1;
*((_WORD *)*a1 + 2) = 0;
*((_WORD *)*a1 + 3) = 0;
return result;
}

original_code:
int
pollsock_close(pollsock *s)
{
    int ret;
    ret=close(s->pfd->fd);
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    return ret;
}

canonical_original_code:
int pollsock_close(struct pollsock *s)
{
    int ret;
    ret=close(s->pfd->fd);
    s->pfd->fd=-1;
    s->pfd->events=0;
    s->pfd->revents=0;
    return ret;
}


decompiled_code:
__int64 __fastcall pollsock_enqueue(__int64 a1)
{
unsigned int v1; // ebp

v1 = tcpbuf_enqueue(a1 + 8);
if ( (int)tcpbuf_filled(a1 + 8) > 0 )
*(_WORD *)(*(_QWORD *)a1 + 4LL) |= 4u;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
unsigned int v1; // ebp

v1 = tcpbuf_enqueue(a1 + 8);
if ( (int)tcpbuf_filled(a1 + 8) > 0 )
*(_WORD *)(*(_QWORD *)a1 + 4LL) |= 4u;
return v1;
}

original_code:
int
pollsock_enqueue(pollsock *s, int len, const u_char *data)
{
    int ret;
    ret=tcpbuf_enqueue(&s->sendbuf, len, data);
    if(tcpbuf_filled(&s->sendbuf)>0)
        s->pfd->events|=0x004;
    return ret;
}

canonical_original_code:
int pollsock_enqueue(struct pollsock *s, int len, unsigned char *data)
{
    int ret;
    ret=tcpbuf_enqueue(&s->sendbuf, len, data);
    if(tcpbuf_filled(&s->sendbuf)>0)
        s->pfd->events|=0x004;
    return ret;
}

variable_types: len int

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall pollsock_dequeue(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp

v2 = tcpbuf_dequeue(a1 + 24, a2);
if ( (int)tcpbuf_space(a1 + 24) > 0 )
*(_WORD *)(*(_QWORD *)a1 + 4LL) |= 1u;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp

v2 = tcpbuf_dequeue(a1 + 24, a2);
if ( (int)tcpbuf_space(a1 + 24) > 0 )
*(_WORD *)(*(_QWORD *)a1 + 4LL) |= 1u;
return v2;
}

original_code:
int
pollsock_dequeue(pollsock *s, int len)
{
    int ret;
    ret=tcpbuf_dequeue(&s->recvbuf, len);
    if(tcpbuf_space(&s->recvbuf)>0)
        s->pfd->events|=0x001;
    return ret;
}

canonical_original_code:
int pollsock_dequeue(struct pollsock *s, int len)
{
    int ret;
    ret=tcpbuf_dequeue(&s->recvbuf, len);
    if(tcpbuf_space(&s->recvbuf)>0)
        s->pfd->events|=0x001;
    return ret;
}


decompiled_code:
__int64 __fastcall pollsock_sendbuf_data(__int64 a1)
{
return tcpbuf_data(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
return tcpbuf_data(a1 + 8);
}

original_code:
const u_char*
pollsock_sendbuf_data(const pollsock *s)
{
    return tcpbuf_data(&s->sendbuf);
}

canonical_original_code:
unsigned char *pollsock_sendbuf_data(struct pollsock *s)
{
    return tcpbuf_data(&s->sendbuf);
}

return_type: unsigned char *


decompiled_code:
__int64 __fastcall pollsock_recvbuf_data(__int64 a1)
{
return tcpbuf_data(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
return tcpbuf_data(a1 + 24);
}

original_code:
const u_char*
pollsock_recvbuf_data(const pollsock *s)
{
    return tcpbuf_data(&s->recvbuf);
}

canonical_original_code:
unsigned char *pollsock_recvbuf_data(struct pollsock *s)
{
    return tcpbuf_data(&s->recvbuf);
}


decompiled_code:
__int64 __fastcall pollsock_sendbuf_filled(__int64 a1)
{
return tcpbuf_filled(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func19(__int64 a1)
{
return tcpbuf_filled(a1 + 8);
}

original_code:
int
pollsock_sendbuf_filled(const pollsock *s)
{
    return tcpbuf_filled(&s->sendbuf);
}

canonical_original_code:
int pollsock_sendbuf_filled(struct pollsock *s)
{
    return tcpbuf_filled(&s->sendbuf);
}


decompiled_code:
__int64 __fastcall pollsock_recvbuf_filled(__int64 a1)
{
return tcpbuf_filled(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1)
{
return tcpbuf_filled(a1 + 24);
}

original_code:
int
pollsock_recvbuf_filled(const pollsock *s)
{
    return tcpbuf_filled(&s->recvbuf);
}

canonical_original_code:
int pollsock_recvbuf_filled(struct pollsock *s)
{
    return tcpbuf_filled(&s->recvbuf);
}


decompiled_code:
__int64 __fastcall pollsock_sendbuf_space(__int64 a1)
{
return tcpbuf_space(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1)
{
return tcpbuf_space(a1 + 8);
}

original_code:
int
pollsock_sendbuf_space(const pollsock *s)
{
    return tcpbuf_space(&s->sendbuf);
}

canonical_original_code:
int pollsock_sendbuf_space(struct pollsock *s)
{
    return tcpbuf_space(&s->sendbuf);
}


decompiled_code:
__int64 __fastcall pollsock_recvbuf_space(__int64 a1)
{
return tcpbuf_space(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1)
{
return tcpbuf_space(a1 + 24);
}

original_code:
int
pollsock_recvbuf_space(const pollsock *s)
{
    return tcpbuf_space(&s->recvbuf);
}

canonical_original_code:
int pollsock_recvbuf_space(struct pollsock *s)
{
    return tcpbuf_space(&s->recvbuf);
}


decompiled_code:
__int64 __fastcall pollsock_sendbuf_full(__int64 a1)
{
return tcpbuf_full(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1)
{
return tcpbuf_full(a1 + 8);
}

original_code:
int
pollsock_sendbuf_full(const pollsock *s)
{
    return tcpbuf_full(&s->sendbuf);
}

canonical_original_code:
int pollsock_sendbuf_full(struct pollsock *s)
{
    return tcpbuf_full(&s->sendbuf);
}


decompiled_code:
__int64 __fastcall pollsock_recvbuf_full(__int64 a1)
{
return tcpbuf_full(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func24(__int64 a1)
{
return tcpbuf_full(a1 + 24);
}

original_code:
int
pollsock_recvbuf_full(const pollsock *s)
{
    return tcpbuf_full(&s->recvbuf);
}

canonical_original_code:
int pollsock_recvbuf_full(struct pollsock *s)
{
    return tcpbuf_full(&s->recvbuf);
}


decompiled_code:
__int64 __fastcall pollsock_sendbuf_empty(__int64 a1)
{
return tcpbuf_empty(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func25(__int64 a1)
{
return tcpbuf_empty(a1 + 8);
}

original_code:
int
pollsock_sendbuf_empty(const pollsock *s)
{
    return tcpbuf_empty(&s->sendbuf);
}

canonical_original_code:
int pollsock_sendbuf_empty(struct pollsock *s)
{
    return tcpbuf_empty(&s->sendbuf);
}


decompiled_code:
__int64 __fastcall pollsock_recvbuf_empty(__int64 a1)
{
return tcpbuf_empty(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func26(__int64 a1)
{
return tcpbuf_empty(a1 + 24);
}

original_code:
int
pollsock_recvbuf_empty(const pollsock *s)
{
    return tcpbuf_empty(&s->recvbuf);
}

canonical_original_code:
int pollsock_recvbuf_empty(struct pollsock *s)
{
    return tcpbuf_empty(&s->recvbuf);
}


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

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

original_code:
int
pollsock_sock(const pollsock *s)
{
    return s->pfd->fd;
}

canonical_original_code:
int pollsock_sock(struct pollsock *s)
{
    return s->pfd->fd;
}


decompiled_code:
int __fastcall pollsock_sockname(int **a1, struct sockaddr *a2, socklen_t *a3)
{
return getsockname(**a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func28(int **a1, struct sockaddr *a2, socklen_t *a3)
{
return getsockname(**a1, a2, a3);
}

original_code:
int
pollsock_sockname(const pollsock *s,
                  struct sockaddr_in *name, socklen_t *namelen)
{
    return getsockname(s->pfd->fd, (struct sockaddr*)name, namelen);
}

canonical_original_code:
int pollsock_sockname(struct pollsock *s, struct sockaddr_in *name, unsigned int *namelen)
{
    return getsockname(s->pfd->fd, (struct sockaddr *)name, namelen);
}

variable_types:
namelen
unsigned int *


decompiled_code:
int __fastcall pollsock_peername(int **a1, struct sockaddr *a2, socklen_t *a3)
{
return getpeername(**a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func29(int **a1, struct sockaddr *a2, socklen_t *a3)
{
return getpeername(**a1, a2, a3);
}

original_code:
int
pollsock_peername(const pollsock *s,
                  struct sockaddr_in *name, socklen_t *namelen)
{
    return getpeername(s->pfd->fd, (struct sockaddr*)name, namelen);
}

canonical_original_code:
int pollsock_peername(struct pollsock *s, struct sockaddr_in *name, unsigned int *namelen)
{
    return getpeername(s->pfd->fd, (struct sockaddr *)name, namelen);
}


decompiled_code:
void __fastcall tcpbuf_construct(__int64 a1, int a2)
{
if ( a2 > 0 )
{
*(_QWORD *)(a1 + 8) = malloc(a2);
*(_DWORD *)a1 = a2;
}
else
{
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)a1 = 0;
}
*(_DWORD *)(a1 + 4) = 0;
}

original_code:
void
tcpbuf_construct(tcpbuf *t, int len)
{
    if(len<=0)
    {
        t->data=((void *)0);
        t->len=0;
    }
    else
    {
        t->data=(u_char*)malloc(len);
        t->len=len;
    }
    t->pos=0;
}

canonical_original_code:
void tcpbuf_construct(struct tcpbuf *t, int len)
{
    if(len<=0)
    {
        t->data=((void *)0);
        t->len=0;
    }
    else
    {
        t->data=(unsigned char *)malloc(len);
        t->len=len;
    }
    t->pos=0;
}

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


original_code:
void
tcpbuf_destruct(tcpbuf *t)
{
    free(t->data);
    t->data=((void *)0);
    t->len=0;
    t->pos=0;
}

canonical_original_code:
void tcpbuf_destruct(struct tcpbuf *t)
{
    free(t->data);
    t->data=((void *)0);
    t->len=0;
    t->pos=0;
}


decompiled_code:
__int64 __fastcall tcpbuf_alloc(__int64 a1, int a2, _QWORD *a3)
{
__int64 result; // rax

result = 0LL;
if ( a2 >= 0 )
{
*a3 = *(_QWORD *)(a1 + 8) + *(int *)(a1 + 4);
result = (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)(a1 + 4));
if ( (int)result > a2 )
result = (unsigned int)a2;
*(_DWORD *)(a1 + 4) += result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, _QWORD *a3)
{
__int64 result; // rax

result = 0LL;
if ( a2 >= 0 )
{
*a3 = *(_QWORD *)(a1 + 8) + *(int *)(a1 + 4);
result = (unsigned int)(*(_DWORD *)a1 - *(_DWORD *)(a1 + 4));
if ( (int)result > a2 )
result = (unsigned int)a2;
*(_DWORD *)(a1 + 4) += result;
}
return result;
}

original_code:
int
tcpbuf_alloc(tcpbuf *t, int len, u_char **ptr)
{
    int avail, ret;
    if(len<0)
        return 0;
    *ptr=t->data+t->pos;
    avail=t->len-t->pos;
    ret=(len<avail?len:avail);
    t->pos+=ret;
    return ret;
}

canonical_original_code:
int tcpbuf_alloc(struct tcpbuf *t, int len, unsigned char **ptr)
{
    int avail; int ret;
    if(len<0)
        return 0;
    *ptr=t->data+t->pos;
    avail=t->len-t->pos;
    ret=(len<avail?len:avail);
    t->pos+=ret;
    return ret;
}

variable_types:
ptr
unsigned char **

variable_types: avail int


decompiled_code:
__int64 __fastcall tcpbuf_dealloc(__int64 a1, int a2)
{
unsigned int v2; // edx
int v3; // eax

v2 = 0;
if ( a2 >= 0 )
{
v3 = *(_DWORD *)(a1 + 4);
v2 = a2;
if ( v3 <= a2 )
v2 = *(_DWORD *)(a1 + 4);
*(_DWORD *)(a1 + 4) = v3 - v2;
}
return v2;
}

original_code:
int
tcpbuf_dealloc(tcpbuf *t, int len)
{
    if(len<0)
        return 0;
    if(len>t->pos)
        len=t->pos;
    t->pos-=len;
    return len;
}

canonical_original_code:
int tcpbuf_dealloc(struct tcpbuf *t, int len)
{
    if(len<0)
        return 0;
    if(len>t->pos)
        len=t->pos;
    t->pos-=len;
    return len;
}


decompiled_code:
__int64 __fastcall tcpbuf_enqueue(__int64 a1, int a2, const void *a3)
{
unsigned int v4; // ebx
void *dest; // [rsp+8h] [rbp-20h] BYREF

v4 = tcpbuf_alloc(a1, a2, &dest);
memcpy(dest, a3, (int)v4);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, const void *a3)
{
unsigned int v4; // ebx
void *dest; // [rsp+8h] [rbp-20h] BYREF

v4 = func2(a1, a2, &dest);
memcpy(dest, a3, (int)v4);
return v4;
}

original_code:
int
tcpbuf_enqueue(tcpbuf *t, int len, const u_char *data)
{
    u_char *buf;
    int copylen;
    copylen=tcpbuf_alloc(t, len, &buf);
    memcpy(buf, data, copylen);
    return copylen;
}

canonical_original_code:
int tcpbuf_enqueue(struct tcpbuf *t, int len, unsigned char *data)
{
    unsigned char *buf;
    int copylen;
    copylen=tcpbuf_alloc(t, len, &buf);
    memcpy(buf, data, copylen);
    return copylen;
}

variable_types: copylen int


decompiled_code:
__int64 __fastcall tcpbuf_dequeue(__int64 a1, signed int a2)
{
int v2; // edx
unsigned int v3; // ebx

v2 = *(_DWORD *)(a1 + 4);
v3 = a2;
if ( v2 <= a2 )
v3 = *(_DWORD *)(a1 + 4);
memmove(*(void **)(a1 + 8), (const void *)(*(_QWORD *)(a1 + 8) + (int)v3), (int)(v2 - v3));
*(_DWORD *)(a1 + 4) -= v3;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, signed int a2)
{
int v2; // edx
unsigned int v3; // ebx

v2 = *(_DWORD *)(a1 + 4);
v3 = a2;
if ( v2 <= a2 )
v3 = *(_DWORD *)(a1 + 4);
memmove(*(void **)(a1 + 8), (const void *)(*(_QWORD *)(a1 + 8) + (int)v3), (int)(v2 - v3));
*(_DWORD *)(a1 + 4) -= v3;
return v3;
}

original_code:
int
tcpbuf_dequeue(tcpbuf *t, int len)
{
    int deqlen, movelen;
    deqlen=(len<t->pos?len:t->pos);
    movelen=t->pos-deqlen;
    memmove(t->data, t->data+deqlen, movelen);
    t->pos-=deqlen;
    return deqlen;
}

canonical_original_code:
int tcpbuf_dequeue(struct tcpbuf *t, int len)
{
    int deqlen; int movelen;
    deqlen=(len<t->pos?len:t->pos);
    movelen=t->pos-deqlen;
    memmove(t->data, t->data+deqlen, movelen);
    t->pos-=deqlen;
    return deqlen;
}

variable_types: deqlen int

variable_types: movelen int


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

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

original_code:
const char*
tcpbuf_data(const tcpbuf *t)
{
    return (const char*)t->data;
}

canonical_original_code:
char *tcpbuf_data(struct tcpbuf *t)
{
    return (char *)t->data;
}

return_type: char *


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

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

original_code:
int
tcpbuf_filled(const tcpbuf *t)
{
    return t->pos;
}

canonical_original_code:
int tcpbuf_filled(struct tcpbuf *t)
{
    return t->pos;
}


decompiled_code:
__int64 __fastcall tcpbuf_space(_DWORD *a1)
{
return (unsigned int)(*a1 - a1[1]);
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1)
{
return (unsigned int)(*a1 - a1[1]);
}

original_code:
int
tcpbuf_space(const tcpbuf *t)
{
    return t->len-t->pos;
}

canonical_original_code:
int tcpbuf_space(struct tcpbuf *t)
{
    return t->len-t->pos;
}


decompiled_code:
_BOOL8 __fastcall tcpbuf_empty(__int64 a1)
{
return *(_DWORD *)(a1 + 4) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(__int64 a1)
{
return *(_DWORD *)(a1 + 4) == 0;
}

original_code:
int
tcpbuf_empty(const tcpbuf *t)
{
    return t->pos==0;
}

canonical_original_code:
int tcpbuf_empty(struct tcpbuf *t)
{
    return t->pos==0;
}


decompiled_code:
_BOOL8 __fastcall tcpbuf_full(_DWORD *a1)
{
return a1[1] == *a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(_DWORD *a1)
{
return a1[1] == *a1;
}

original_code:
int
tcpbuf_full(const tcpbuf *t)
{
    return t->pos==t->len;
}

canonical_original_code:
int tcpbuf_full(struct tcpbuf *t)
{
    return t->pos==t->len;
}


decompiled_code:
const char *__fastcall pipeserver_event_str(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 1:
result = "ACCEPTED";
break;
case 2:
result = "CLOSED";
break;
case 3:
result = "CLOSING";
break;
case 4:
result = "ERROR";
break;
case 5:
result = "RECEIVED";
break;
case 6:
result = "CLOSE";
break;
case 7:
result = "SEND";
break;
case 8:
result = "SHUTDOWN";
break;
default:
result = "NULL";
break;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 1:
result = "ACCEPTED";
break;
case 2:
result = "CLOSED";
break;
case 3:
result = "CLOSING";
break;
case 4:
result = "ERROR";
break;
case 5:
result = "RECEIVED";
break;
case 6:
result = "CLOSE";
break;
case 7:
result = "SEND";
break;
case 8:
result = "SHUTDOWN";
break;
default:
result = "NULL";
break;
}
return result;
}

original_code:
static const char*
pipeserver_event_str(pipeserver_event e)
{
    switch(e)
    {
    case PIPESERVER_ACCEPTED:
        return pipeserver_accepted;
    case PIPESERVER_CLOSED:
        return pipeserver_closed;
    case PIPESERVER_CLOSING:
        return pipeserver_closing;
    case PIPESERVER_ERROR:
        return pipeserver_error;
    case PIPESERVER_RECEIVED:
        return pipeserver_received;
    case PIPESERVER_CLOSE:
        return pipeserver_close;
    case PIPESERVER_SEND:
        return pipeserver_send;
    case PIPESERVER_SHUTDOWN:
        return pipeserver_shutdown;
    default:
        return pipeserver_nullevent;
    }
}

canonical_original_code:
static char *pipeserver_event_str(enum pipeserver_event e)
{
    switch(e)
    {
    case PIPESERVER_ACCEPTED:
        return pipeserver_accepted;
    case PIPESERVER_CLOSED:
        return pipeserver_closed;
    case PIPESERVER_CLOSING:
        return pipeserver_closing;
    case PIPESERVER_ERROR:
        return pipeserver_error;
    case PIPESERVER_RECEIVED:
        return pipeserver_received;
    case PIPESERVER_CLOSE:
        return pipeserver_close;
    case PIPESERVER_SEND:
        return pipeserver_send;
    case PIPESERVER_SHUTDOWN:
        return pipeserver_shutdown;
    default:
        return pipeserver_nullevent;
    }
}

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


decompiled_code:
__int64 __fastcall read_pipeserver_event(__int64 a1, int a2, _DWORD *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 8:
if ( (unsigned int)bufs_match((__int64)"ACCEPTED", a1, 8) )
{
*a3 = 1;
result = 1LL;
}
else if ( (unsigned int)bufs_match((__int64)"RECEIVED", a1, 8) )
{
*a3 = 5;
result = 1LL;
}
else
{
result = bufs_match((__int64)"SHUTDOWN", a1, 8);
if ( (_DWORD)result )
{
*a3 = 8;
result = 1LL;
}
}
break;
case 6:
result = bufs_match((__int64)"CLOSED", a1, 6);
if ( (_DWORD)result )
{
*a3 = 2;
result = 1LL;
}
break;
case 7:
result = bufs_match((__int64)"CLOSING", a1, 7);
if ( (_DWORD)result )
{
*a3 = 3;
result = 1LL;
}
break;
case 5:
if ( (unsigned int)bufs_match((__int64)"ERROR", a1, 5) )
{
*a3 = 4;
result = 1LL;
}
else
{
result = bufs_match((__int64)"CLOSE", a1, 5);
if ( (_DWORD)result )
{
*a3 = 6;
result = 1LL;
}
}
break;
default:
result = 0LL;
if ( a2 == 4 )
{
result = bufs_match((__int64)"SEND", a1, 4);
if ( (_DWORD)result )
{
*a3 = 7;
result = 1LL;
}
}
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, _DWORD *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 8:
if ( (unsigned int)func1((__int64)"ACCEPTED", a1, 8) )
{
*a3 = 1;
result = 1LL;
}
else if ( (unsigned int)func1((__int64)"RECEIVED", a1, 8) )
{
*a3 = 5;
result = 1LL;
}
else
{
result = func1((__int64)"SHUTDOWN", a1, 8);
if ( (_DWORD)result )
{
*a3 = 8;
result = 1LL;
}
}
break;
case 6:
result = func1((__int64)"CLOSED", a1, 6);
if ( (_DWORD)result )
{
*a3 = 2;
result = 1LL;
}
break;
case 7:
result = func1((__int64)"CLOSING", a1, 7);
if ( (_DWORD)result )
{
*a3 = 3;
result = 1LL;
}
break;
case 5:
if ( (unsigned int)func1((__int64)"ERROR", a1, 5) )
{
*a3 = 4;
result = 1LL;
}
else
{
result = func1((__int64)"CLOSE", a1, 5);
if ( (_DWORD)result )
{
*a3 = 6;
result = 1LL;
}
}
break;
default:
result = 0LL;
if ( a2 == 4 )
{
result = func1((__int64)"SEND", a1, 4);
if ( (_DWORD)result )
{
*a3 = 7;
result = 1LL;
}
}
break;
}
return result;
}

original_code:
static int
read_pipeserver_event(const char *event, int len, pipeserver_event *e)
{
    if(len==strlen(pipeserver_accepted) &&
       bufs_match(pipeserver_accepted, event, len))
    {
        *e=PIPESERVER_ACCEPTED;
        return 1;
    }
    if(len==strlen(pipeserver_closed) &&
       bufs_match(pipeserver_closed, event, len))
    {
        *e=PIPESERVER_CLOSED;
        return 1;
    }
    if(len==strlen(pipeserver_closing) &&
       bufs_match(pipeserver_closing, event, len))
    {
        *e=PIPESERVER_CLOSING;
        return 1;
    }
    if(len==strlen(pipeserver_error) &&
       bufs_match(pipeserver_error, event, len))
    {
        *e=PIPESERVER_ERROR;
        return 1;
    }
    if(len==strlen(pipeserver_received) &&
       bufs_match(pipeserver_received, event, len))
    {
        *e=PIPESERVER_RECEIVED;
        return 1;
    }
    if(len==strlen(pipeserver_close) &&
       bufs_match(pipeserver_close, event, len))
    {
        *e=PIPESERVER_CLOSE;
        return 1;
    }
    if(len==strlen(pipeserver_send) &&
       bufs_match(pipeserver_send, event, len))
    {
        *e=PIPESERVER_SEND;
        return 1;
    }
    if(len==strlen(pipeserver_shutdown) &&
       bufs_match(pipeserver_shutdown, event, len))
    {
        *e=PIPESERVER_SHUTDOWN;
        return 1;
    }
    return 0;
}

canonical_original_code:
static int read_pipeserver_event(char *event, int len, enum pipeserver_event *e)
{
    if(len==strlen(pipeserver_accepted) &&
       bufs_match(pipeserver_accepted, event, len))
    {
        *e=PIPESERVER_ACCEPTED;
        return 1;
    }
    if(len==strlen(pipeserver_closed) &&
       bufs_match(pipeserver_closed, event, len))
    {
        *e=PIPESERVER_CLOSED;
        return 1;
    }
    if(len==strlen(pipeserver_closing) &&
       bufs_match(pipeserver_closing, event, len))
    {
        *e=PIPESERVER_CLOSING;
        return 1;
    }
    if(len==strlen(pipeserver_error) &&
       bufs_match(pipeserver_error, event, len))
    {
        *e=PIPESERVER_ERROR;
        return 1;
    }
    if(len==strlen(pipeserver_received) &&
       bufs_match(pipeserver_received, event, len))
    {
        *e=PIPESERVER_RECEIVED;
        return 1;
    }
    if(len==strlen(pipeserver_close) &&
       bufs_match(pipeserver_close, event, len))
    {
        *e=PIPESERVER_CLOSE;
        return 1;
    }
    if(len==strlen(pipeserver_send) &&
       bufs_match(pipeserver_send, event, len))
    {
        *e=PIPESERVER_SEND;
        return 1;
    }
    if(len==strlen(pipeserver_shutdown) &&
       bufs_match(pipeserver_shutdown, event, len))
    {
        *e=PIPESERVER_SHUTDOWN;
        return 1;
    }
    return 0;
}

variable_types:
event
char *

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


original_code:
static int
get_msg_lens(const char *msg, int len, int *event_len, int *handle_len)
{
    int i, j;
    for(i=0; i<len; i++)
    {
        if(msg[i]==' ')
        {
            *event_len=i;
            break;
        }
    }
    for(j=i+1; j<len; j++)
    {
        if(msg[j]==' ')
        {
            break;
            *handle_len=j-i-1;
            return 1;
        }
    }
    if(i>=len)
        return 0;
    *handle_len=j-i-1;
    return 1;
}

canonical_original_code:
static int get_msg_lens(char *msg, int len, int *event_len, int *handle_len)
{
    int i; int j;
    for(i=0; i<len; i++)
    {
        if(msg[i]==' ')
        {
            *event_len=i;
            break;
        }
    }
    for(j=i+1; j<len; j++)
    {
        if(msg[j]==' ')
        {
            break;
            *handle_len=j-i-1;
            return 1;
        }
    }
    if(i>=len)
        return 0;
    *handle_len=j-i-1;
    return 1;
}

variable_types:
msg
char *

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall read_pollset_handle(__int64 a1, int a2, _DWORD *a3)
{
const unsigned __int16 **v4; // r9
char *v5; // rcx
int v6; // edx

*a3 = 0;
if ( a2 - 1 < 0 )
return 1LL;
v4 = _ctype_b_loc();
v5 = (char *)(a1 + a2 - 1);
v6 = 1;
while ( ((*v4)[*v5] & 0x800) != 0 )
{
*a3 += v6 * (*v5 - 48);
v6 *= 10;
if ( --v5 == (char *)(a1 + a2 - 2 - (unsigned int)(a2 - 1)) )
return 1LL;
}
return 0LL;
}

original_code:
static int
read_pollset_handle(const char *handle, int len, pollset_handle *h)
{
    *h=0;
    int place=1;
    for(len--; len>=0; len--)
    {
        if(!((*__ctype_b_loc ())[(int) ((handle[len]))] & (unsigned short int) _ISdigit))
            return 0;
        (*h)+=place*(handle[len]-'0');
        place*=10;
    }
    return 1;
}

canonical_original_code:
static int read_pollset_handle(char *handle, int len, int *h)
{
    *h=0;
    int place = 1;
    for(len--; len>=0; len--)
    {
        if(!((*__ctype_b_loc ())[(int) ((handle[len]))] & (unsigned short) _ISdigit))
            return 0;
        (*h)+=place*(handle[len]-'0');
        place*=10;
    }
    return 1;
}

variable_types:
handle
char *

variable_types:
h
int *

variable_types: place int


decompiled_code:
int __fastcall pipeserver_msg_build(char *s, int a2, int a3, unsigned int a4, __int64 a5, int a6)
{
const char *v11; // rax
int result; // eax
char *v13; // r13
int v14; // esi
int v15; // ebx
__int64 i; // rax

v11 = pipeserver_event_str(a3);
result = snprintf(s, a2, "%s %d", v11, a4);
if ( a2 <= result || result < 0 )
return -1;
if ( (a3 & 0xFFFFFFFD) == 5 )
{
v13 = &s[result];
*v13 = 32;
v14 = result + 1;
v15 = a2 - (result + 1);
if ( v15 > a6 )
v15 = a6;
if ( v15 > 0 )
{
for ( i = 0LL; i != v15; ++i )
v13[i + 1] = *(_BYTE *)(a5 + i);
}
result = v14 + a6;
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(char *s, int a2, int a3, unsigned int a4, __int64 a5, int a6)
{
const char *v11; // rax
int result; // eax
char *v13; // r13
int v14; // esi
int v15; // ebx
__int64 i; // rax

v11 = func0(a3);
result = snprintf(s, a2, "%s %d", v11, a4);
if ( a2 <= result || result < 0 )
return -1;
if ( (a3 & 0xFFFFFFFD) == 5 )
{
v13 = &s[result];
*v13 = 32;
v14 = result + 1;
v15 = a2 - (result + 1);
if ( v15 > a6 )
v15 = a6;
if ( v15 > 0 )
{
for ( i = 0LL; i != v15; ++i )
v13[i + 1] = *(_BYTE *)(a5 + i);
}
result = v14 + a6;
}
return result;
}

original_code:
int
pipeserver_msg_build(char *buf, int buflen,
                     pipeserver_event e, pollset_handle h,
                     const char *data, int len)
{
    const char *event_str;
    int ret, i;
    event_str=pipeserver_event_str(e);
    ret=snprintf(buf, buflen, "%s %d", event_str, h);
    if(ret>=buflen || ret<0)
        return -1;
    if(e!=PIPESERVER_RECEIVED && e!=PIPESERVER_SEND)
        return ret;
    buf[ret]=' ';
    ret++;
    buflen-=ret;
    buf+=ret;
    for(i=0; i<len && i<buflen; i++)
        buf[i]=data[i];
    return ret+len;
}

canonical_original_code:
int pipeserver_msg_build(char *buf, int buflen, enum pipeserver_event e, int h, char *data, int len)
{
    char *event_str;
    int ret; int i;
    event_str=pipeserver_event_str(e);
    ret=snprintf(buf, buflen, "%s %d", event_str, h);
    if(ret>=buflen || ret<0)
        return -1;
    if(e!=PIPESERVER_RECEIVED && e!=PIPESERVER_SEND)
        return ret;
    buf[ret]=' ';
    ret++;
    buflen-=ret;
    buf+=ret;
    for(i=0; i<len && i<buflen; i++)
        buf[i]=data[i];
    return ret+len;
}

variable_types:
buf
char *

variable_types: buflen int

variable_types: h int

variable_types:
data
char *

variable_types: len int

variable_types: ret int