getConfiguration

decompiled_code:
const char **__fastcall getConfiguration(char *s)
{
__int64 v1; // r12
__int64 v2; // rax
const char **v3; // rbx
size_t v4; // rax

v1 = listGetIterator(*(_QWORD *)&Server[21038], 0LL);
do
{
v2 = listNext(v1);
v3 = (const char **)v2;
if ( !v2 )
break;
v3 = *(const char ***)(v2 + 16);
v4 = strlen(s);
}
while ( strncasecmp(s, *v3, v4) );
freeListIterator(v1);
return v3;
}

canonical_decompiled_code:
const char **__fastcall func11(char *s)
{
__int64 v1; // r12
__int64 v2; // rax
const char **v3; // rbx
size_t v4; // rax

v1 = listGetIterator(*(_QWORD *)&Server[21038], 0LL);
do
{
v2 = listNext(v1);
v3 = (const char **)v2;
if ( !v2 )
break;
v3 = *(const char ***)(v2 + 16);
v4 = strlen(s);
}
while ( strncasecmp(s, *v3, v4) );
freeListIterator(v1);
return v3;
}

original_code:
struct configuration *getConfiguration(const char *name)
{
    struct listNode *node;
    struct listIterator *iter;
    struct configuration *conf;
    iter = listGetIterator(Server.confs, 0);
    while ((node = listNext(iter)) != ((void *)0)) {
        conf = ((void*)((node)->value));
        if (!strncasecmp(name, conf->name, strlen(name))) {
            freeListIterator(iter);
            return conf;
        }
    }
    freeListIterator(iter);
    return ((void *)0);
}

canonical_original_code:
struct configuration *getConfiguration(char *name)
{
    struct listNode *node;
    struct listIterator *iter;
    struct configuration *conf;
    iter = listGetIterator(Server.confs, 0);
    while ((node = listNext(iter)) != ((void *)0)) {
        conf = ((void *)((node)->value));
        if (!strncasecmp(name, conf->name, strlen(name))) {
            freeListIterator(iter);
            return conf;
        }
    }
    freeListIterator(iter);
    return ((void *)0);
}

variable_types:
name
char *

variable_types: struct listIterator { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *next; int direction; } *

return_type: struct configuration { char *name; int args; int (*validator)(struct configuration *conf, char *key, char *value); union { int val; void *ptr; struct enumIdName { int id; char *name; } *enum_ptr; } target; void *helper; enum printFormat { INT_FORMAT=0, STRING_FORMAT=1, ENUM_FORMAT=2, BOOL_FORMAT=3, LIST_FORMAT=4 } format; } *

user_defined_types: struct listIterator { struct listNode *next; int direction; }


applyConfig

decompiled_code:
__int64 __fastcall applyConfig(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rsi
int v3; // eax
const char *v4; // rbx
_BYTE *v5; // rdi
char **v6; // rax
char **v7; // rbx
const char **v8; // rax
const char **v9; // r12
int v10; // eax
int v12; // [rsp+4h] [rbp-24h] BYREF
unsigned int v13; // [rsp+8h] [rbp-20h] BYREF
int v14[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = wstrNewSplit(a1, "\n", 1LL, v14);
v12 = 0;
if ( !v1 )
{
v4 = "Memory alloc failed";
goto LABEL_20;
}
v2 = (unsigned int)v14[0];
if ( v14[0] <= 0 )
{
LABEL_18:
wstrFreeSplit(v1, v2);
return fillServerConfig();
}
v3 = 0;
while ( 1 )
{
v5 = (_BYTE *)wstrStrip(*(_QWORD *)(v1 + 8LL * v3), "\t\n\r ");
if ( (*v5 & 0xDF) == 0 || *v5 == 35 )
goto LABEL_9;
v6 = (char **)wstrNewSplit(v5, " ", 1LL, &v13);
v7 = v6;
if ( !v6 )
{
v4 = "Memory alloc failed";
goto LABEL_20;
}
v8 = getConfiguration(*v6);
v9 = v8;
if ( !v8 )
{
v4 = "Unknown configuration name";
goto LABEL_20;
}
if ( v13 == 1 && *((_DWORD *)v8 + 10) == 4 && !handleList(v1, &v12, v14[0]) )
{
v4 = "parse list failed";
goto LABEL_20;
}
v10 = *((_DWORD *)v9 + 2);
if ( v13 != v10 && v10 != -1 )
{
v4 = "Incorrect args";
goto LABEL_20;
}
if ( ((unsigned int (__fastcall *)(const char **, char *))v9[2])(v9, *v7) )
break;
wstrFreeSplit(v7, v13);
LABEL_9:
v3 = v12 + 1;
v12 = v3;
v2 = (unsigned int)v14[0];
if ( v3 >= v14[0] )
goto LABEL_18;
}
v4 = "Validate Failed";
LABEL_20:
fwrite("\n*** FATAL CONFIG FILE ERROR ***\n", 1uLL, 0x21uLL, stderr);
fprintf(stderr, "Reading the configuration file, at line %d\n", (unsigned int)(v12 + 1));
fprintf(stderr, ">>> '%s'\n", *(const char **)(v1 + 8LL * v12));
fprintf(stderr, "Reason: %s\n", v4);
return halt(1LL);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rsi
int v3; // eax
const char *v4; // rbx
_BYTE *v5; // rdi
char **v6; // rax
char **v7; // rbx
const char **v8; // rax
const char **v9; // r12
int v10; // eax
int v12; // [rsp+4h] [rbp-24h] BYREF
unsigned int v13; // [rsp+8h] [rbp-20h] BYREF
int v14[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = wstrNewSplit(a1, "\n", 1LL, v14);
v12 = 0;
if ( !v1 )
{
v4 = "Memory alloc failed";
goto LABEL_20;
}
v2 = (unsigned int)v14[0];
if ( v14[0] <= 0 )
{
LABEL_18:
wstrFreeSplit(v1, v2);
return func8();
}
v3 = 0;
while ( 1 )
{
v5 = (_BYTE *)wstrStrip(*(_QWORD *)(v1 + 8LL * v3), "\t\n\r ");
if ( (*v5 & 0xDF) == 0 || *v5 == 35 )
goto LABEL_9;
v6 = (char **)wstrNewSplit(v5, " ", 1LL, &v13);
v7 = v6;
if ( !v6 )
{
v4 = "Memory alloc failed";
goto LABEL_20;
}
v8 = func11(*v6);
v9 = v8;
if ( !v8 )
{
v4 = "Unknown configuration name";
goto LABEL_20;
}
if ( v13 == 1 && *((_DWORD *)v8 + 10) == 4 && !func6(v1, &v12, v14[0]) )
{
v4 = "parse list failed";
goto LABEL_20;
}
v10 = *((_DWORD *)v9 + 2);
if ( v13 != v10 && v10 != -1 )
{
v4 = "Incorrect args";
goto LABEL_20;
}
if ( ((unsigned int (__fastcall *)(const char **, char *))v9[2])(v9, *v7) )
break;
wstrFreeSplit(v7, v13);
LABEL_9:
v3 = v12 + 1;
v12 = v3;
v2 = (unsigned int)v14[0];
if ( v3 >= v14[0] )
goto LABEL_18;
}
v4 = "Validate Failed";
LABEL_20:
fwrite("\n*** FATAL CONFIG FILE ERROR ***\n", 1uLL, 0x21uLL, stderr);
fprintf(stderr, "Reading the configuration file, at line %d\n", (unsigned int)(v12 + 1));
fprintf(stderr, ">>> '%s'\n", *(const char **)(v1 + 8LL * v12));
fprintf(stderr, "Reason: %s\n", v4);
return halt(1LL);
}

original_code:
void applyConfig(wstr config)
{
    int count, args;
    wstr *lines = wstrNewSplit(config, "\n", 1, &count);
    char *err = ((void *)0);
    struct configuration *conf;
    void *ptr = ((void *)0);
    int i = 0;
    if (lines == ((void *)0)) {
        err = "Memory alloc failed";
        goto loaderr;
    }
    for (i = 0; i < count; i++) {
        wstr line = wstrStrip(lines[i], "\t\n\r ");
        if (line[0] == '#' || line[0] == '\0' || line[0] == ' ')
            continue;
        wstr *argvs = wstrNewSplit(line, " ", 1, &args);
        if (argvs == ((void *)0)) {
            err = "Memory alloc failed";
            goto loaderr;
        }
        if ((conf = getConfiguration(argvs[0])) == ((void *)0)) {
            err = "Unknown configuration name";
            goto loaderr;
        }
        ptr = argvs[1];
        if (args == 1 && conf->format == LIST_FORMAT) {
            struct list *l = handleList(lines, &i, count);
            if (!l) {
                err = "parse list failed";
                goto loaderr;
            }
            ptr = l;
        } else if (args != conf->args && args == 2) {
            int j;
            for (j = 2; j < args; ++j) {
                argvs[1] = wstrCatLen(argvs[1], argvs[j], wstrlen(argvs[j]));
                if (!argvs[1]) {
                    err = "Unknown configuration name";
                    goto loaderr;
                }
            }
        }
        if (args != conf->args && conf->args != (-1)) {
            err = "Incorrect args";
            goto loaderr;
        }
        if (conf->validator(conf, argvs[0], ptr) != 0) {
            err = "Validate Failed";
            goto loaderr;
        }
        wstrFreeSplit(argvs, args);
    }
    wstrFreeSplit(lines, count);
    fillServerConfig();
    return ;
loaderr:
    fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
    fprintf(stderr, "Reading the configuration file, at line %d\n", i+1);
    fprintf(stderr, ">>> '%s'\n", lines[i]);
    fprintf(stderr, "Reason: %s\n", err);
    halt(1);
}

canonical_original_code:
void applyConfig(char *config)
{
    int count; int args;
    char **lines = wstrNewSplit(config, "\n", 1, &count);
    char *err = ((void *)0);
    struct configuration *conf;
    void *ptr = ((void *)0);
    int i = 0;
    if (lines == ((void *)0)) {
        err = "Memory alloc failed";
        goto loaderr;
    }
    for (i = 0; i < count; i++) {
        char *line = wstrStrip(lines[i], "\t\n\r ");
        if (line[0] == '#' || line[0] == '\0' || line[0] == ' ')
            continue;
        char **argvs = wstrNewSplit(line, " ", 1, &args);
        if (argvs == ((void *)0)) {
            err = "Memory alloc failed";
            goto loaderr;
        }
        if ((conf = getConfiguration(argvs[0])) == ((void *)0)) {
            err = "Unknown configuration name";
            goto loaderr;
        }
        ptr = argvs[1];
        if (args == 1 && conf->format == LIST_FORMAT) {
            struct list *l = handleList(lines, &i, count);
            if (!l) {
                err = "parse list failed";
                goto loaderr;
            }
            ptr = l;
        } else if (args != conf->args && args == 2) {
            int j;
            for (j = 2; j < args; ++j) {
                argvs[1] = wstrCatLen(argvs[1], argvs[j], wstrlen(argvs[j]));
                if (!argvs[1]) {
                    err = "Unknown configuration name";
                    goto loaderr;
                }
            }
        }
        if (args != conf->args && conf->args != (-1)) {
            err = "Incorrect args";
            goto loaderr;
        }
        if (conf->validator(conf, argvs[0], ptr) != 0) {
            err = "Validate Failed";
            goto loaderr;
        }
        wstrFreeSplit(argvs, args);
    }
    wstrFreeSplit(lines, count);
    fillServerConfig();
    return ;
loaderr:
    fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
    fprintf(stderr, "Reading the configuration file, at line %d\n", i+1);
    fprintf(stderr, ">>> '%s'\n", lines[i]);
    fprintf(stderr, "Reason: %s\n", err);
    halt(1);
}

variable_types:
config
char *

variable_types: count int

variable_types: args int

variable_types:
lines
char **

variable_types:
err
char *

variable_types: struct configuration { char *name; int args; int (*validator)(struct configuration *conf, char *key, char *value); union { int val; void *ptr; struct enumIdName { int id; char *name; } *enum_ptr; } target; void *helper; enum printFormat { INT_FORMAT=0, STRING_FORMAT=1, ENUM_FORMAT=2, BOOL_FORMAT=3, LIST_FORMAT=4 } format; } *

variable_types:
ptr
void *

variable_types: i int

variable_types:
line
char *

variable_types:
argvs
char **

variable_types: struct list { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; } *

variable_types: j int

return_type: void

user_defined_types: struct configuration { char *name; int args; int (*validator)(struct configuration *conf, char *key, char *value); union { int val; void *ptr; struct enumIdName *enum_ptr; } target; void *helper; enum printFormat format; }

user_defined_types: struct list { struct listNode *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; }


configCommand

decompiled_code:
__int64 __fastcall configCommand(__int64 a1)
{
const char **v1; // rax
__int64 v2; // rdx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = getConfiguration(*(char **)(*(_QWORD *)(a1 + 32) + 8LL));
if ( v1 )
{
v2 = constructConfigFormat(v1, v4, 0xFFuLL);
}
else
{
strcpy(v4, "No Correspond Configuration");
v2 = 27LL;
}
return replyMasterClient(a1, v4, v2);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
const char **v1; // rax
__int64 v2; // rdx
char v4[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = func11(*(char **)(*(_QWORD *)(a1 + 32) + 8LL));
if ( v1 )
{
v2 = func7(v1, v4, 0xFFuLL);
}
else
{
strcpy(v4, "No Correspond Configuration");
v2 = 27LL;
}
return replyMasterClient(a1, v4, v2);
}

original_code:
void configCommand(struct masterClient *c)
{
    struct configuration *conf = ((void *)0);
    char buf[255];
    ssize_t len;
    conf = getConfiguration(c->argv[1]);
    if (conf == ((void *)0)) {
        len = snprintf(buf, 255, "No Correspond Configuration");
    } else {
        len = constructConfigFormat(conf, buf, 255);
    }
    replyMasterClient(c, buf, len);
}

canonical_original_code:
void configCommand(struct masterClient *c)
{
    struct configuration *conf = ((void *)0);
    char buf[255];
    long len;
    conf = getConfiguration(c->argv[1]);
    if (conf == ((void *)0)) {
        len = snprintf(buf, 255, "No Correspond Configuration");
    } else {
        len = constructConfigFormat(conf, buf, 255);
    }
    replyMasterClient(c, buf, len);
}

variable_types: struct masterClient { int fd; char *request_buf; char *response_buf; int argc; char **argv; } *

variable_types:
buf
char [255]

variable_types: len long

user_defined_types: enum printFormat { INT_FORMAT=0, STRING_FORMAT=1, ENUM_FORMAT=2, BOOL_FORMAT=3, LIST_FORMAT=4 }

user_defined_types: struct enumIdName { int id; char *name; }


printServerConfig

decompiled_code:
__int64 __fastcall printServerConfig(unsigned int a1)
{
unsigned int v1; // ebx
__int64 v2; // rbp
__int64 v3; // rax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

v1 = a1;
if ( a1 )
v1 = 2;
wheatLog(v1, "---- Now Configuration are ----");
v2 = listGetIterator(*(_QWORD *)&Server[21038], 0LL);
while ( 1 )
{
v3 = listNext(v2);
if ( !v3 )
break;
constructConfigFormat(*(const char ***)(v3 + 16), v5, 0xFFuLL);
wheatLog(v1, "%s", v5);
}
freeListIterator(v2);
return wheatLog(v1, "-------------------------------");
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1)
{
unsigned int v1; // ebx
__int64 v2; // rbp
__int64 v3; // rax
char v5[280]; // [rsp+0h] [rbp-118h] BYREF

v1 = a1;
if ( a1 )
v1 = 2;
wheatLog(v1, "---- Now Configuration are ----");
v2 = listGetIterator(*(_QWORD *)&Server[21038], 0LL);
while ( 1 )
{
v3 = listNext(v2);
if ( !v3 )
break;
func7(*(const char ***)(v3 + 16), v5, 0xFFuLL);
wheatLog(v1, "%s", v5);
}
freeListIterator(v2);
return wheatLog(v1, "-------------------------------");
}

original_code:
void printServerConfig(int test)
{
    struct configuration *conf;
    int level;
    struct listNode *node;
    struct listIterator *iter;
    char buf[255];
    level = test ? 2 : 0;
    wheatLog(level, "---- Now Configuration are ----");
    iter = listGetIterator(Server.confs, 0);
    while ((node = listNext(iter)) != ((void *)0)) {
        conf = ((void*)((node)->value));
        constructConfigFormat(conf, buf, 255);
        wheatLog(level, "%s", buf);
    }
    freeListIterator(iter);
    wheatLog(level, "-------------------------------");
}

canonical_original_code:
void printServerConfig(int test)
{
    struct configuration *conf;
    int level;
    struct listNode *node;
    struct listIterator *iter;
    char buf[255];
    level = test ? 2 : 0;
    wheatLog(level, "---- Now Configuration are ----");
    iter = listGetIterator(Server.confs, 0);
    while ((node = listNext(iter)) != ((void *)0)) {
        conf = ((void *)((node)->value));
        constructConfigFormat(conf, buf, 255);
        wheatLog(level, "%s", buf);
    }
    freeListIterator(iter);
    wheatLog(level, "-------------------------------");
}

variable_types: test int

variable_types: level int

variable_types: struct listNode { struct listNode *next; struct listNode *prev; void *value; } *


eventInit

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

v1 = wmalloc(16LL);
if ( v1 )
{
v2 = wmalloc(12LL * a1);
*(_QWORD *)(v1 + 8) = v2;
if ( v2 )
{
v3 = epoll_create(1024);
*(_DWORD *)v1 = v3;
if ( v3 == -1 )
{
wfree(*(_QWORD *)(v1 + 8));
wfree(v1);
v1 = 0LL;
}
}
else
{
wfree(v1);
v1 = 0LL;
}
}
return v1;
}

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

v1 = wmalloc(16LL);
if ( v1 )
{
v2 = wmalloc(12LL * a1);
*(_QWORD *)(v1 + 8) = v2;
if ( v2 )
{
v3 = epoll_create(1024);
*(_DWORD *)v1 = v3;
if ( v3 == -1 )
{
wfree(*(_QWORD *)(v1 + 8));
wfree(v1);
v1 = 0LL;
}
}
else
{
wfree(v1);
v1 = 0LL;
}
}
return v1;
}

original_code:
static struct apiState *eventInit(int nevent) {
    struct apiState *state = wmalloc(sizeof(struct apiState));
    if (!state) return ((void *)0);
    state->events = wmalloc(sizeof(struct epoll_event)*nevent);
    if (!state->events) {
        wfree(state);
        return ((void *)0);
    }
    state->epfd = epoll_create(1024);
    if (state->epfd == -1) {
        wfree(state->events);
        wfree(state);
        return ((void *)0);
    }
    return state;
}

variable_types: nevent int

variable_types: struct apiState { int epfd; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; } *

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct apiState { int epfd; struct epoll_event *events; }


eventDeinit

decompiled_code:
__int64 __fastcall eventDeinit(__int64 a1)
{
close(*(_DWORD *)a1);
wfree(*(_QWORD *)(a1 + 8));
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
close(*(_DWORD *)a1);
wfree(*(_QWORD *)(a1 + 8));
return wfree(a1);
}

original_code:
static void eventDeinit(struct apiState *state) {
    close(state->epfd);
    wfree(state->events);
    wfree(state);
}

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


eventWait

original_code:
static int eventWait(struct evcenter *center, struct timeval *tvp) {
    struct apiState *state = center->apidata;
    int retval, numevents = 0;
    retval = epoll_wait(state->epfd, state->events, center->nevent,
            tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
    if (retval > 0) {
        int j;
        numevents = retval;
        for (j = 0; j < numevents; j++) {
            int mask = 0;
            struct epoll_event *e = state->events + j;
            if (e->events & EPOLLIN) mask |= 1;
            if (e->events & EPOLLOUT) mask |= 2;
            if (e->events & EPOLLERR) mask |= 2;
            if (e->events & EPOLLHUP) mask |= 2;
            center->fired_events[j].fd = e->data.fd;
            center->fired_events[j].mask = mask;
        }
    }
    return numevents;
}

canonical_original_code:
static int eventWait(struct evcenter *center, struct timeval *tvp) {
    struct apiState *state = center->apidata;
    int retval; int numevents = 0;
    retval = epoll_wait(state->epfd, state->events, center->nevent,
            tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
    if (retval > 0) {
        int j;
        numevents = retval;
        for (j = 0; j < numevents; j++) {
            int mask = 0;
            struct epoll_event *e = state->events + j;
            if (e->events & EPOLLIN) mask |= 1;
            if (e->events & EPOLLOUT) mask |= 2;
            if (e->events & EPOLLERR) mask |= 2;
            if (e->events & EPOLLHUP) mask |= 2;
            center->fired_events[j].fd = e->data.fd;
            center->fired_events[j].mask = mask;
        }
    }
    return numevents;
}

variable_types: struct evcenter { int nevent; struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); } *events; struct fired_event { int mask; int fd; } *fired_events; void *apidata; } *

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

variable_types: retval int

variable_types: numevents int

variable_types: mask int

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *

return_type: int

user_defined_types: struct evcenter { int nevent; struct event *events; struct fired_event *fired_events; void *apidata; }


eventcenterInit

decompiled_code:
__int64 __fastcall eventcenterInit(int a1)
{
__int64 v1; // rbx
void *v2; // rbp
void *v3; // r12
__int64 v4; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax

v1 = wmalloc(32LL);
if ( !v1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
wheatLog(4LL, "center create failed: %s", v7);
return v1;
}
v2 = (void *)wmalloc(32LL * a1);
v3 = (void *)wmalloc(8LL * a1);
if ( v3 && v2 )
{
memset(v2, 0, 32LL * a1);
memset(v3, 0, 8LL * a1);
v4 = eventInit(a1);
if ( v4 )
{
*(_DWORD *)v1 = a1;
*(_QWORD *)(v1 + 8) = v2;
*(_QWORD *)(v1 + 16) = v3;
*(_QWORD *)(v1 + 24) = v4;
return v1;
}
v10 = _errno_location();
v11 = strerror(*v10);
wheatLog(4LL, "naive event init failed", v11);
wfree(v1);
}
else
{
v8 = _errno_location();
v9 = strerror(*v8);
wheatLog(4LL, "event and fired_event create failed: %s", v9);
wfree(v1);
if ( !v3 )
goto LABEL_10;
}
wfree(v3);
LABEL_10:
v1 = (__int64)v2;
if ( v2 )
{
wfree(v2);
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1)
{
__int64 v1; // rbx
void *v2; // rbp
void *v3; // r12
__int64 v4; // rax
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax

v1 = wmalloc(32LL);
if ( !v1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
wheatLog(4LL, "center create failed: %s", v7);
return v1;
}
v2 = (void *)wmalloc(32LL * a1);
v3 = (void *)wmalloc(8LL * a1);
if ( v3 && v2 )
{
memset(v2, 0, 32LL * a1);
memset(v3, 0, 8LL * a1);
v4 = func0(a1);
if ( v4 )
{
*(_DWORD *)v1 = a1;
*(_QWORD *)(v1 + 8) = v2;
*(_QWORD *)(v1 + 16) = v3;
*(_QWORD *)(v1 + 24) = v4;
return v1;
}
v10 = _errno_location();
v11 = strerror(*v10);
wheatLog(4LL, "naive event init failed", v11);
wfree(v1);
}
else
{
v8 = _errno_location();
v9 = strerror(*v8);
wheatLog(4LL, "event and fired_event create failed: %s", v9);
wfree(v1);
if ( !v3 )
goto LABEL_10;
}
wfree(v3);
LABEL_10:
v1 = (__int64)v2;
if ( v2 )
{
wfree(v2);
v1 = 0LL;
}
return v1;
}

original_code:
struct evcenter *eventcenterInit(int nevent)
{
    struct event *events = ((void *)0);
    struct evcenter *center = ((void *)0);
    struct fired_event *fired_events = ((void *)0);
    void *api_state = ((void *)0);
    center = wmalloc(sizeof(struct evcenter));
    if (center == ((void *)0)) {
        wheatLog(4, "center create failed: %s", strerror((*__errno_location ())));
        return ((void *)0);
    }
    events = wmalloc(nevent*sizeof(struct event));
    fired_events = wmalloc(nevent*sizeof(struct fired_event));
    if (fired_events == ((void *)0) || events == ((void *)0)) {
        wheatLog(4, "event and fired_event create failed: %s",
                strerror((*__errno_location ())));
        goto cleanup;
    }
    memset(events, 0, nevent*sizeof(struct event));
    memset(fired_events, 0, nevent*sizeof(struct fired_event));
    api_state = eventInit(nevent);
    if (!api_state) {
        wheatLog(4, "naive event init failed", strerror((*__errno_location ())));
        goto cleanup;
    }
    center->nevent = nevent;
    center->events = events;
    center->fired_events = fired_events;
    center->apidata = api_state;
    return center;
cleanup:
    if (center)
        wfree(center);
    if (fired_events)
        wfree(fired_events);
    if (events)
        wfree(events);
    return ((void *)0);
}

variable_types: struct event { int mask; void *client_data; void (*read_proc)(struct evcenter { int nevent; struct event *events; struct fired_event { int mask; int fd; } *fired_events; void *apidata; } *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); } *

return_type: struct evcenter { int nevent; struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); } *events; struct fired_event { int mask; int fd; } *fired_events; void *apidata; } *


eventcenterDealloc

decompiled_code:
__int64 __fastcall eventcenterDealloc(__int64 a1)
{
eventDeinit(*(_QWORD *)(a1 + 24));
wfree(*(_QWORD *)(a1 + 8));
return wfree(a1);
}

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

original_code:
void eventcenterDealloc(struct evcenter *center)
{
    eventDeinit(center->apidata);
    wfree(center->events);
    wfree(center);
}

user_defined_types: struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); }

user_defined_types: struct fired_event { int mask; int fd; }


createEvent

decompiled_code:
__int64 __fastcall createEvent(__int64 a1, int a2, int a3, __int64 a4, __int64 a5)
{
__int64 v8; // rbx
__int64 result; // rax

if ( *(_DWORD *)a1 <= a2 )
{
*_errno_location() = 34;
result = 0xFFFFFFFFLL;
}
else
{
v8 = *(_QWORD *)(a1 + 8) + 32LL * a2;
result = addEvent(a1, a2, a3);
if ( (_DWORD)result != -1 )
{
*(_DWORD *)v8 |= a3;
if ( (a3 & 1) != 0 )
*(_QWORD *)(v8 + 16) = a4;
if ( (a3 & 2) != 0 )
*(_QWORD *)(v8 + 24) = a4;
*(_QWORD *)(v8 + 8) = a5;
result = 0LL;
}
}
return result;
}

original_code:
int createEvent(struct evcenter *center, int fd, int mask, eventProc *proc, void *client_data)
{
    if (fd >= center->nevent) {
        (*__errno_location ()) = 34;
        return -1;
    }
    struct event *event = &center->events[fd];
    if (addEvent(center, fd, mask) == -1)
        return -1;
    event->mask |= mask;
    if (mask & 1) event->read_proc = proc;
    if (mask & 2) event->write_proc = proc;
    event->client_data = client_data;
    return 0;
}

canonical_original_code:
int createEvent(struct evcenter *center, int fd, int mask, void (*proc)(struct evcenter *center, int fd, void *client_data, int mask), void *client_data)
{
    if (fd >= center->nevent) {
        (*__errno_location ()) = 34;
        return -1;
    }
    struct event *event = &center->events[fd];
    if (addEvent(center, fd, mask) == -1)
        return -1;
    event->mask |= mask;
    if (mask & 1) event->read_proc = proc;
    if (mask & 2) event->write_proc = proc;
    event->client_data = client_data;
    return 0;
}

variable_types: fd int

variable_types: void (*)(struct evcenter *center, int fd, void *client_data, int mask)


deleteEvent

decompiled_code:
void __fastcall deleteEvent(__int64 a1, signed int a2, int a3)
{
_DWORD *v3; // rax

if ( *(_DWORD *)a1 > a2 )
{
v3 = (_DWORD *)(*(_QWORD *)(a1 + 8) + 32LL * a2);
if ( *v3 )
{
*v3 &= ~a3;
delEvent(a1, a2, a3);
}
}
}

original_code:
void deleteEvent(struct evcenter *center, int fd, int mask)
{
    if (fd >= center->nevent) return;
    struct event *event = &center->events[fd];
    if (event->mask == 0) return;
    event->mask = event->mask & (~mask);
    delEvent(center, fd, mask);
}


processEvents

original_code:
int processEvents(struct evcenter *center, int timeout_millionseconds)
{
    struct timeval tv;
    int j, processed, numevents, mask, fd, rfired;
    struct event *event;
    if (timeout_millionseconds > 0) {
        tv.tv_sec = timeout_millionseconds / 1000;
        tv.tv_usec = (timeout_millionseconds % 1000) * 1000;
    }
    else {
        tv.tv_sec = 0;
        tv.tv_usec = 0;
    }
    processed = 0;
    numevents = eventWait(center, &tv);
    for (j = 0; j < numevents; j++) {
        event = &center->events[center->fired_events[j].fd];
        mask = center->fired_events[j].mask;
        fd = center->fired_events[j].fd;
        rfired = 0;
        if (event->mask & mask & 1) {
            rfired = 1;
            event->read_proc(center, fd, event->client_data, mask);
        }
        if (event->mask & mask & 2) {
            if (!rfired || event->read_proc != event->write_proc)
                event->write_proc(center, fd, event->client_data, mask);
        }
        processed++;
    }
    return processed;
}

canonical_original_code:
int processEvents(struct evcenter *center, int timeout_millionseconds)
{
    struct timeval tv;
    int j; int processed; int numevents; int mask; int fd; int rfired;
    struct event *event;
    if (timeout_millionseconds > 0) {
        tv.tv_sec = timeout_millionseconds / 1000;
        tv.tv_usec = (timeout_millionseconds % 1000) * 1000;
    }
    else {
        tv.tv_sec = 0;
        tv.tv_usec = 0;
    }
    processed = 0;
    numevents = eventWait(center, &tv);
    for (j = 0; j < numevents; j++) {
        event = &center->events[center->fired_events[j].fd];
        mask = center->fired_events[j].mask;
        fd = center->fired_events[j].fd;
        rfired = 0;
        if (event->mask & mask & 1) {
            rfired = 1;
            event->read_proc(center, fd, event->client_data, mask);
        }
        if (event->mask & mask & 2) {
            if (!rfired || event->read_proc != event->write_proc)
                event->write_proc(center, fd, event->client_data, mask);
        }
        processed++;
    }
    return processed;
}

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

variable_types: j int

variable_types: processed int

variable_types: rfired int


createList

decompiled_code:
_QWORD *createList()
{
_QWORD *result; // rax

result = (_QWORD *)wmalloc(48LL);
result[1] = 0LL;
*result = 0LL;
result[5] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = (_QWORD *)wmalloc(48LL);
result[1] = 0LL;
*result = 0LL;
result[5] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
return result;
}

original_code:
struct list *createList()
{
    struct list *l = wmalloc(sizeof(struct list));
    l->first = l->last = ((void *)0);
    l->len = 0;
    l->dup = ((void *)0);
    l->match = ((void *)0);
    l->free = ((void *)0);
    return l;
}

variable_types: struct list { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; } *


listClear

decompiled_code:
__int64 __fastcall listClear(__int64 a1)
{
__int64 result; // rax
_QWORD *v2; // rbp
__int64 v3; // rbx
_QWORD *v4; // r12
void (__fastcall *v5)(_QWORD); // rax

result = *(_QWORD *)(a1 + 40);
v2 = *(_QWORD **)a1;
v3 = result - 1;
if ( result )
{
do
{
v4 = v2;
v2 = (_QWORD *)*v2;
v5 = *(void (__fastcall **)(_QWORD))(a1 + 32);
if ( v5 )
v5(v4[2]);
result = wfree(v4);
--v3;
}
while ( v3 != -1 );
}
*(_QWORD *)(a1 + 40) = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
return result;
}

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

result = *(_QWORD *)(a1 + 40);
v2 = *(_QWORD **)a1;
v3 = result - 1;
if ( result )
{
do
{
v4 = v2;
v2 = (_QWORD *)*v2;
v5 = *(void (__fastcall **)(_QWORD))(a1 + 32);
if ( v5 )
v5(v4[2]);
result = wfree(v4);
--v3;
}
while ( v3 != -1 );
}
*(_QWORD *)(a1 + 40) = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
return result;
}

original_code:
void listClear(struct list *l)
{
    struct listNode *current, *next;
    unsigned long len = l->len;
    current = l->first;
    while (len--) {
        next = current->next;
        if (l->free)
            l->free((void*)current->value);
        wfree(current);
        current = next;
    }
    l->len = 0;
    l->first = l->last = ((void *)0);
}

canonical_original_code:
void listClear(struct list *l)
{
    struct listNode *current; struct listNode *next;
    unsigned long len = l->len;
    current = l->first;
    while (len--) {
        next = current->next;
        if (l->free)
            l->free((void *)current->value);
        wfree(current);
        current = next;
    }
    l->len = 0;
    l->first = l->last = ((void *)0);
}

variable_types: struct listNode { struct listNode *next; struct listNode *prev; void *value; } *

variable_types:
len
unsigned long


freeList

decompiled_code:
__int64 __fastcall freeList(__int64 a1)
{
listClear(a1);
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
func1(a1);
return wfree(a1);
}

original_code:
void freeList(struct list *l)
{
    listClear(l);
    wfree(l);
}

user_defined_types: struct listNode { struct listNode *next; struct listNode *prev; void *value; }


removeListNode

decompiled_code:
void __fastcall removeListNode(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // rax
void (__fastcall *v3)(_QWORD); // rax

if ( a2 )
{
v2 = (_QWORD *)a2[1];
if ( v2 )
*v2 = *a2;
else
*a1 = *a2;
if ( *a2 )
*(_QWORD *)(*a2 + 8LL) = a2[1];
else
a1[1] = a2[1];
v3 = (void (__fastcall *)(_QWORD))a1[4];
if ( v3 )
v3(a2[2]);
--a1[5];
wfree(a2);
}
}

original_code:
void removeListNode(struct list *l, struct listNode *node)
{
    if (node == ((void *)0))
        return ;
    if (node->prev)
        node->prev->next = node->next;
    else
        l->first = node->next;
    if (node->next)
        node->next->prev = node->prev;
    else
        l->last = node->prev;
    if (l->free) l->free((void*)(node->value));
    l->len--;
    wfree(node);
}

canonical_original_code:
void removeListNode(struct list *l, struct listNode *node)
{
    if (node == ((void *)0))
        return ;
    if (node->prev)
        node->prev->next = node->next;
    else
        l->first = node->next;
    if (node->next)
        node->next->prev = node->prev;
    else
        l->last = node->prev;
    if (l->free) l->free((void *)(node->value));
    l->len--;
    wfree(node);
}

variable_types:
node
struct listNode *


listGetIterator

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

result = wmalloc(16LL);
if ( result )
{
if ( a2 )
{
*(_QWORD *)result = a1[1];
*(_DWORD *)(result + 8) = 1;
}
else
{
*(_QWORD *)result = *a1;
*(_DWORD *)(result + 8) = 0;
}
}
return result;
}

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

result = wmalloc(16LL);
if ( result )
{
if ( a2 )
{
*(_QWORD *)result = a1[1];
*(_DWORD *)(result + 8) = 1;
}
else
{
*(_QWORD *)result = *a1;
*(_DWORD *)(result + 8) = 0;
}
}
return result;
}

original_code:
struct listIterator *listGetIterator(struct list *list, int direction)
{
    struct listIterator *iter = wmalloc(sizeof(struct listIterator));
    if (iter == ((void *)0))
        return ((void *)0);
    if (direction == 0) {
        iter->next = list->first;
        iter->direction = 0;
    }
    else {
        iter->next = list->last;
        iter->direction = 1;
    }
    return iter;
}

variable_types: struct list { struct listNode *first; void *(*dup)(void *ptr); int (*match)(void *ptr, void *key); void (*free)(void *ptr); unsigned long len; } *

variable_types: direction int

variable_types: struct listIterator { struct listNode { struct listNode *next; struct listNode *prev; void *value; } *next; int direction; } *


freeListIterator

decompiled_code:
__int64 __fastcall freeListIterator(__int64 a1)
{
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return wfree(a1);
}

original_code:
void freeListIterator(struct listIterator *iter)
{
    wfree(iter);
}


listPrint

decompiled_code:
void __fastcall listPrint(_QWORD *a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
_QWORD *v3; // rax

if ( a1 )
{
v1 = listGetIterator(a1, 0);
for ( i = 0; ; ++i )
{
v3 = listNext(v1);
if ( !v3 )
break;
printf("%d: %s -> ", i, (const char *)v3[2]);
}
freeListIterator(v1);
putchar(10);
}
}

canonical_decompiled_code:
void __fastcall func8(_QWORD *a1)
{
__int64 v1; // rbp
unsigned int i; // ebx
_QWORD *v3; // rax

if ( a1 )
{
v1 = func6(a1, 0);
for ( i = 0; ; ++i )
{
v3 = listNext(v1);
if ( !v3 )
break;
printf("%d: %s -> ", i, (const char *)v3[2]);
}
func7(v1);
putchar(10);
}
}

original_code:
void listPrint(struct list *list)
{
    if (list == ((void *)0))
        return ;
    struct listIterator *iter = listGetIterator(list, 0);
    struct listNode *current;
    int i = 0;
    while ((current = listNext(iter)) != ((void *)0)) {
        printf("%d: %s -> ", i, (char *)current->value);
        i++;
    }
    freeListIterator(iter);
    printf("\n");
}

variable_types: i int


wstrlen

decompiled_code:
__int64 __fastcall wstrlen(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

original_code:
static inline int wstrlen(const wstr s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

canonical_original_code:
static inline int wstrlen(char *s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

variable_types:
s
char *

variable_types: struct wstrhd { int len; int free; char buf[]; } *


deallocStaticFile

decompiled_code:
__int64 deallocStaticFile()
{
if ( AllowExtensions )
dictRelease();
if ( DirectoryIndex )
freeList();
MaxFileSize = 0;
return wstrFree(IfModifiedSince);
}

canonical_decompiled_code:
__int64 func4()
{
if ( AllowExtensions )
dictRelease();
if ( DirectoryIndex )
freeList();
MaxFileSize = 0;
return wstrFree(IfModifiedSince);
}

original_code:
void deallocStaticFile()
{
    if (AllowExtensions)
        dictRelease(AllowExtensions);
    if (DirectoryIndex)
        freeList(DirectoryIndex);
    MaxFileSize = 0;
    wstrFree(IfModifiedSince);
}


freeStaticFileData

decompiled_code:
__int64 __fastcall freeStaticFileData(__int64 a1)
{
int v2; // edi

wstrFree(*(_QWORD *)(a1 + 8));
wstrFree(*(_QWORD *)a1);
v2 = *(_DWORD *)(a1 + 16);
if ( v2 > 0 )
close(v2);
return wfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v2; // edi

wstrFree(*(_QWORD *)(a1 + 8));
wstrFree(*(_QWORD *)a1);
v2 = *(_DWORD *)(a1 + 16);
if ( v2 > 0 )
close(v2);
return wfree(a1);
}

original_code:
void freeStaticFileData(void *app_data)
{
    struct staticFileData *data = app_data;
    wstrFree(data->extension);
    wstrFree(data->filename);
    if (data->fd > 0)
        close(data->fd);
    wfree(data);
}

variable_types: struct staticFileData { char *filename; char *extension; int fd; } *


arrayDealloc

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

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

original_code:
void arrayDealloc(struct array *arr)
{
    wfree(arr->elements);
    wfree(arr);
}

variable_types: struct array { unsigned long nelem; unsigned char *elements; unsigned long elem_size; unsigned long capacity; } *


arrayIndex

decompiled_code:
__int64 __fastcall arrayIndex(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 result; // rax

if ( *a1 <= a2 )
result = 0LL;
else
result = a1[1] + a1[2] * a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 *a1, unsigned __int64 a2)
{
__int64 result; // rax

if ( *a1 <= a2 )
result = 0LL;
else
result = a1[1] + a1[2] * a2;
return result;
}

original_code:
void *arrayIndex(struct array *a, size_t pos)
{
    if (pos >= a->nelem)
        return ((void *)0);
    size_t p = pos * a->elem_size;
    return a->elements+p;
}

canonical_original_code:
void *arrayIndex(struct array *a, unsigned long pos)
{
    if (pos >= a->nelem)
        return ((void *)0);
    unsigned long p = pos * a->elem_size;
    return a->elements+p;
}

variable_types:
pos
unsigned long

variable_types:
p
unsigned long

return_type: void *

user_defined_types: struct array { unsigned long nelem; unsigned char *elements; unsigned long elem_size; unsigned long capacity; }


arrayPop

original_code:
void *arrayPop(struct array *a)
{
    return a->elements+a->elem_size*(--a->nelem);
}


arrayTop

decompiled_code:
__int64 __fastcall arrayTop(unsigned __int64 *a1)
{
return arrayIndex(a1, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int64 *a1)
{
return func1(a1, 0LL);
}

original_code:
void *arrayTop(struct array *a)
{
    return arrayIndex(a, 0);
}


arrayLast

decompiled_code:
__int64 __fastcall arrayLast(unsigned __int64 *a1)
{
return arrayIndex(a1, *a1 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned __int64 *a1)
{
return func1(a1, *a1 - 1);
}

original_code:
void *arrayLast(struct array *a)
{
    return arrayIndex(a, a->nelem-1);
}


arrayEach

decompiled_code:
__int64 __fastcall arrayEach(__int64 *a1, __int64 (__fastcall *a2)(__int64))
{
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // rbx

result = *a1;
v3 = a1[1];
v4 = *a1 - 1;
if ( *a1 )
{
do
{
result = a2(v3);
v3 += a1[2];
--v4;
}
while ( v4 != -1 );
}
return result;
}

original_code:
void arrayEach(struct array *a, void(*func)(void *))
{
    size_t len = a->nelem;
    uint8_t *pos = a->elements;
    while (len--) {
        func(pos);
        pos += a->elem_size;
    }
}

canonical_original_code:
void arrayEach(struct array *a, void (*func)(void *))
{
    unsigned long len = a->nelem;
    unsigned char *pos = a->elements;
    while (len--) {
        func(pos);
        pos += a->elem_size;
    }
}

variable_types:
func
void (*)(void *)

variable_types:
pos
unsigned char *


original_code:
void arrayEach2(struct array *a, void(*func)(void *item, void *data), void *data)
{
    size_t len = a->nelem;
    uint8_t *pos = a->elements;
    while (len--) {
        func(pos, data);
        pos += a->elem_size;
    }
}

canonical_original_code:
void arrayEach2(struct array *a, void (*func)(void *item, void *data), void *data)
{
    unsigned long len = a->nelem;
    unsigned char *pos = a->elements;
    while (len--) {
        func(pos, data);
        pos += a->elem_size;
    }
}

variable_types:
func
void (*)(void *item, void *data)

variable_types:
data
void *


narray

decompiled_code:
__int64 __fastcall narray(__int64 a1)
{
return *(_QWORD *)a1;
}

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

original_code:
size_t narray(struct array *a)
{
    return a->nelem;
}

canonical_original_code:
unsigned long narray(struct array *a)
{
    return a->nelem;
}

return_type: unsigned long


arrayData

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

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

original_code:
void *arrayData(struct array *a)
{
    return a->elements;
}


sliceFree

decompiled_code:
__int64 sliceFree()
{
return wfree();
}

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

original_code:
void sliceFree(struct slice *b)
{
    wfree(b);
}

variable_types: struct slice { unsigned char *data; unsigned long len; } *


sliceStartWith

decompiled_code:
_BOOL8 __fastcall sliceStartWith(__int64 a1, __int64 a2)
{
size_t v2; // rdx
_BOOL8 result; // rax

v2 = *(_QWORD *)(a2 + 8);
result = 0LL;
if ( *(_QWORD *)(a1 + 8) >= v2 )
result = memcmp(*(const void **)a1, *(const void **)a2, v2) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1, __int64 a2)
{
size_t v2; // rdx
_BOOL8 result; // rax

v2 = *(_QWORD *)(a2 + 8);
result = 0LL;
if ( *(_QWORD *)(a1 + 8) >= v2 )
result = memcmp(*(const void **)a1, *(const void **)a2, v2) == 0;
return result;
}

original_code:
int sliceStartWith(struct slice *b, struct slice *s)
{
    return ((b->len >= s->len) &&
            (memcmp(b->data, s->data, s->len) == 0));
}

variable_types:
s
struct slice *

user_defined_types: struct slice { unsigned char *data; unsigned long len; }


sliceCompare

decompiled_code:
__int64 __fastcall sliceCompare(__int64 a1, __int64 a2)
{
size_t v2; // rbp
size_t v3; // rbx
size_t v4; // rdx
unsigned int v5; // edx

v2 = *(_QWORD *)(a2 + 8);
v3 = *(_QWORD *)(a1 + 8);
v4 = v3;
if ( v2 <= v3 )
v4 = *(_QWORD *)(a2 + 8);
v5 = memcmp(*(const void **)a1, *(const void **)a2, v4);
if ( !v5 )
{
if ( v2 > v3 )
v5 = -1;
else
v5 = v2 < v3;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
size_t v2; // rbp
size_t v3; // rbx
size_t v4; // rdx
unsigned int v5; // edx

v2 = *(_QWORD *)(a2 + 8);
v3 = *(_QWORD *)(a1 + 8);
v4 = v3;
if ( v2 <= v3 )
v4 = *(_QWORD *)(a2 + 8);
v5 = memcmp(*(const void **)a1, *(const void **)a2, v4);
if ( !v5 )
{
if ( v2 > v3 )
v5 = -1;
else
v5 = v2 < v3;
}
return v5;
}

original_code:
int sliceCompare(struct slice *b, struct slice *s)
{
    const size_t min_len = (b->len < s->len) ? b->len : s->len;
    int r = memcmp(b->data, s->data, min_len);
    if (r == 0) {
        if (b->len < s->len)
            r = -1;
        else if
            (b->len > s->len) r = 1;
    }
    return r;
}

canonical_original_code:
int sliceCompare(struct slice *b, struct slice *s)
{
    unsigned long min_len = (b->len < s->len) ? b->len : s->len;
    int r = memcmp(b->data, s->data, min_len);
    if (r == 0) {
        if (b->len < s->len)
            r = -1;
        else if
            (b->len > s->len) r = 1;
    }
    return r;
}

variable_types: r int


wmalloc

decompiled_code:
void *__fastcall wmalloc(size_t a1)
{
if ( (a1 & 7) != 0 )
a1 = (a1 & 0xFFFFFFFFFFFFFFF8LL) + 8;
MemorySize += a1;
return malloc(a1);
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
if ( (a1 & 7) != 0 )
a1 = (a1 & 0xFFFFFFFFFFFFFFF8LL) + 8;
MemorySize += a1;
return malloc(a1);
}

original_code:
void *wmalloc(size_t size)
{
    do { if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); }while(0);
    MemorySize += size;
    void *p = malloc(size);
    return p;
}

canonical_original_code:
void *wmalloc(unsigned long size)
{
    do { if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); }while(0);
    MemorySize += size;
    void *p = malloc(size);
    return p;
}

variable_types:
size
unsigned long

variable_types:
p
void *


wrealloc

decompiled_code:
void *__fastcall wrealloc(void *a1, size_t a2)
{
if ( (a2 & 7) != 0 )
a2 = (a2 & 0xFFFFFFFFFFFFFFF8LL) + 8;
MemorySize += a2;
return realloc(a1, a2);
}

canonical_decompiled_code:
void *__fastcall func1(void *a1, size_t a2)
{
if ( (a2 & 7) != 0 )
a2 = (a2 & 0xFFFFFFFFFFFFFFF8LL) + 8;
MemorySize += a2;
return realloc(a1, a2);
}

original_code:
void *wrealloc(void *old, size_t size)
{
    do { if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); }while(0);
    MemorySize += size;
    void *p = realloc(old, size);
    return p;
}

canonical_original_code:
void *wrealloc(void *old, unsigned long size)
{
    do { if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); }while(0);
    MemorySize += size;
    void *p = realloc(old, size);
    return p;
}

variable_types:
old
void *


wcalloc

decompiled_code:
void *__fastcall wcalloc(__int64 a1, __int64 a2)
{
return wmalloc(a2 * a1);
}

canonical_decompiled_code:
void *__fastcall func2(__int64 a1, __int64 a2)
{
return func0(a2 * a1);
}

original_code:
void *wcalloc(size_t count, size_t size)
{
    void *p = wmalloc(size*count);
    return p;
}

canonical_original_code:
void *wcalloc(unsigned long count, unsigned long size)
{
    void *p = wmalloc(size*count);
    return p;
}

variable_types:
count
unsigned long


wfree

decompiled_code:
void __fastcall wfree(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
free(a1);
}

original_code:
void wfree(void *ptr)
{
    free(ptr);
}

canonical_original_code:
void wfree(void *ptr)
{
    free(ptr);
}

variable_types:
ptr
void *


wheatSetError

decompiled_code:
int wheatSetError(char *a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(arg, a2);
result = vsnprintf(a1, 0xFFuLL, a2, arg);
}
return result;
}

canonical_decompiled_code:
int func1(char *a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(arg, a2);
result = vsnprintf(a1, 0xFFuLL, a2, arg);
}
return result;
}

original_code:
static void wheatSetError(char *err, const char *fmt, ...)
{
    va_list ap;
    if (!err) return;
    __builtin_va_start(ap,fmt);
    vsnprintf(err, 255, fmt, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
static void wheatSetError(char *err, char *fmt, ...)
{
    __builtin_va_list ap;
    if (!err) return;
    __builtin_va_start(ap,fmt);
    vsnprintf(err, 255, fmt, ap);
    __builtin_va_end(ap);
}

variable_types:
err
char *

variable_types:
fmt
char *


wheatNonBlock

decompiled_code:
__int64 __fastcall wheatNonBlock(char *a1, int a2)
{
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v2 = fcntl(a2, 3);
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
wheatSetError(a1, "fcntl(F_GETFL): %s", v5);
result = 0xFFFFFFFFLL;
}
else
{
BYTE1(v2) |= 8u;
if ( fcntl(a2, 4, (unsigned int)v2) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
wheatSetError(a1, "fcntl(F_SETFL,O_NONBLOCK): %s", v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1, int a2)
{
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v2 = fcntl(a2, 3);
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
func1(a1, "fcntl(F_GETFL): %s", v5);
result = 0xFFFFFFFFLL;
}
else
{
BYTE1(v2) |= 8u;
if ( fcntl(a2, 4, (unsigned int)v2) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
func1(a1, "fcntl(F_SETFL,O_NONBLOCK): %s", v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int wheatNonBlock(char *err, int fd)
{
    int flags;
    if ((flags = fcntl(fd, 3)) < 0 ) {
        wheatSetError(err, "fcntl(F_GETFL): %s", strerror((*__errno_location ())));
        return -1;
    }
    if (fcntl(fd, 4, flags | 04000) < 0) {
        wheatSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}

variable_types: fd int

variable_types: flags int


wheatTcpGenericConnect

decompiled_code:
__int64 __fastcall wheatTcpGenericConnect(char *a1, const char *a2, __int16 a3, char a4)
{
const char *v5; // rbx
unsigned int v8; // ebp
int v9; // edi
struct hostent *v10; // rax
char *v11; // rax
struct sockaddr v13[3]; // [rsp+0h] [rbp-38h] BYREF

v5 = a2;
v8 = wheatCreateSocket(a1, 2);
if ( v8 != -1 )
{
v13[0].sa_family = 2;
*(_WORD *)v13[0].sa_data = _bswap_16(a3);
if ( !a2 )
v5 = "127.0.0.1";
if ( !inet_aton(v5, (struct in_addr *)&v13[0].sa_data[2]) )
{
v10 = gethostbyname(v5);
if ( !v10 )
{
wheatSetError(a1, "can't resolve: %s", v5);
close(v8);
return (unsigned int)-1;
}
*(_DWORD *)&v13[0].sa_data[2] = **(_DWORD **)v10->h_addr_list;
}
if ( (a4 & 1) == 0 )
{
if ( connect(v8, v13, 0x10u) != -1 )
return v8;
v9 = *_errno_location();
goto LABEL_15;
}
if ( (unsigned int)wheatNonBlock(a1, v8) )
{
v8 = -1;
}
else if ( connect(v8, v13, 0x10u) == -1 )
{
v9 = *_errno_location();
if ( v9 != 115 )
{
LABEL_15:
v11 = strerror(v9);
wheatSetError(a1, "connect: %s", v11);
close(v8);
return (unsigned int)-1;
}
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, const char *a2, __int16 a3, char a4)
{
const char *v5; // rbx
unsigned int v8; // ebp
int v9; // edi
struct hostent *v10; // rax
char *v11; // rax
struct sockaddr v13[3]; // [rsp+0h] [rbp-38h] BYREF

v5 = a2;
v8 = func2(a1, 2);
if ( v8 != -1 )
{
v13[0].sa_family = 2;
*(_WORD *)v13[0].sa_data = func0(a3);
if ( !a2 )
v5 = "127.0.0.1";
if ( !inet_aton(v5, (struct in_addr *)&v13[0].sa_data[2]) )
{
v10 = gethostbyname(v5);
if ( !v10 )
{
func1(a1, "can't resolve: %s", v5);
close(v8);
return (unsigned int)-1;
}
*(_DWORD *)&v13[0].sa_data[2] = **(_DWORD **)v10->h_addr_list;
}
if ( (a4 & 1) == 0 )
{
if ( connect(v8, v13, 0x10u) != -1 )
return v8;
v9 = *_errno_location();
goto LABEL_15;
}
if ( (unsigned int)func6(a1, v8) )
{
v8 = -1;
}
else if ( connect(v8, v13, 0x10u) == -1 )
{
v9 = *_errno_location();
if ( v9 != 115 )
{
LABEL_15:
v11 = strerror(v9);
func1(a1, "connect: %s", v11);
close(v8);
return (unsigned int)-1;
}
}
}
return v8;
}

original_code:
static int wheatTcpGenericConnect(char *err, char *addr, int port, int flags)
{
    int s;
    struct sockaddr_in sa;
    if ((s = wheatCreateSocket(err, 2)) == -1)
        return -1;
    sa.sin_family = 2;
    sa.sin_port = htons(port);
    if (!addr)
        addr = "127.0.0.1";
    if (inet_aton(addr, &sa.sin_addr) == 0) {
        struct hostent *he;
        he = gethostbyname(addr);
        if (he == ((void *)0)) {
            wheatSetError(err, "can't resolve: %s", addr);
            close(s);
            return -1;
        }
        memcpy(&sa.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
    }
    if (flags & 1) {
        if (wheatNonBlock(err,s) != 0)
            return -1;
    }
    if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        if ((*__errno_location ()) == 115 &&
            flags & 1)
            return s;
        wheatSetError(err, "connect: %s", strerror((*__errno_location ())));
        close(s);
        return -1;
    }
    return s;
}

canonical_original_code:
static int wheatTcpGenericConnect(char *err, char *addr, int port, int flags)
{
    int s;
    struct sockaddr_in sa;
    if ((s = wheatCreateSocket(err, 2)) == -1)
        return -1;
    sa.sin_family = 2;
    sa.sin_port = htons(port);
    if (!addr)
        addr = "127.0.0.1";
    if (inet_aton(addr, &sa.sin_addr) == 0) {
        struct hostent *he;
        he = gethostbyname(addr);
        if (he == ((void *)0)) {
            wheatSetError(err, "can't resolve: %s", addr);
            close(s);
            return -1;
        }
        memcpy(&sa.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
    }
    if (flags & 1) {
        if (wheatNonBlock(err,s) != 0)
            return -1;
    }
    if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
        if ((*__errno_location ()) == 115 &&
            flags & 1)
            return s;
        wheatSetError(err, "connect: %s", strerror((*__errno_location ())));
        close(s);
        return -1;
    }
    return s;
}

variable_types:
addr
char *

variable_types: port int

variable_types: s int

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

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

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

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


wheatTcpConnect

decompiled_code:
__int64 __fastcall wheatTcpConnect(char *a1, const char *a2, __int16 a3)
{
return wheatTcpGenericConnect(a1, a2, a3, 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, const char *a2, __int16 a3)
{
return func7(a1, a2, a3, 0);
}

original_code:
int wheatTcpConnect(char *err, char *addr, int port)
{
    return wheatTcpGenericConnect(err,addr,port,0);
}


wheatTcpNonBlockConnect

decompiled_code:
__int64 __fastcall wheatTcpNonBlockConnect(char *a1, const char *a2, __int16 a3)
{
return wheatTcpGenericConnect(a1, a2, a3, 1);
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1, const char *a2, __int16 a3)
{
return func7(a1, a2, a3, 1);
}

original_code:
int wheatTcpNonBlockConnect(char *err, char *addr, int port)
{
    return wheatTcpGenericConnect(err, addr, port, 1);
}


wheatCloseOnExec

decompiled_code:
__int64 __fastcall wheatCloseOnExec(char *a1, int a2)
{
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v2 = fcntl(a2, 1, 0LL);
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
wheatSetError(a1, "fcntl(F_GETFL): %s", v5);
result = 0xFFFFFFFFLL;
}
else if ( fcntl(a2, 2, v2 | 1u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
wheatSetError(a1, "fcntl(F_SETFL, FD_CLOEXEC): %s", v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *a1, int a2)
{
int v2; // eax
__int64 result; // rax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax

v2 = fcntl(a2, 1, 0LL);
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
func1(a1, "fcntl(F_GETFL): %s", v5);
result = 0xFFFFFFFFLL;
}
else if ( fcntl(a2, 2, v2 | 1u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
func1(a1, "fcntl(F_SETFL, FD_CLOEXEC): %s", v7);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
return result;
}

original_code:
int wheatCloseOnExec(char *err, int fd)
{
    int flags;
    if ((flags = fcntl(fd, 1, 0)) < 0) {
        wheatSetError(err, "fcntl(F_GETFL): %s", strerror((*__errno_location ())));
        return -1;
    }
    if (fcntl(fd, 2, flags | 1) < 0) {
        wheatSetError(err, "fcntl(F_SETFL, FD_CLOEXEC): %s", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}


wheatSetSendBuffer

decompiled_code:
__int64 __fastcall wheatSetSendBuffer(char *a1, int a2, int a3)
{
unsigned int v3; // ebx
int *v5; // rax
char *v6; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = a3;
if ( setsockopt(a2, 1, 7, optval, 4u) != -1 )
return 0;
v3 = -1;
v5 = _errno_location();
v6 = strerror(*v5);
wheatSetError(a1, "setsockopt SO_SNDBUF: %s", v6);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *a1, int a2, int a3)
{
unsigned int v3; // ebx
int *v5; // rax
char *v6; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = a3;
if ( setsockopt(a2, 1, 7, optval, 4u) != -1 )
return 0;
v3 = -1;
v5 = _errno_location();
v6 = strerror(*v5);
func1(a1, "setsockopt SO_SNDBUF: %s", v6);
return v3;
}

original_code:
int wheatSetSendBuffer(char *err, int fd, int buffsize)
{
    if (setsockopt(fd, 1, 7, &buffsize, sizeof(buffsize)) == -1)
    {
        wheatSetError(err, "setsockopt SO_SNDBUF: %s", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}

variable_types: buffsize int


wheatTcpNoDelay

decompiled_code:
__int64 __fastcall wheatTcpNoDelay(char *a1, int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
if ( setsockopt(a2, 6, 1, optval, 4u) != -1 )
return 0;
v2 = -1;
v4 = _errno_location();
v5 = strerror(*v4);
wheatSetError(a1, "setsockopt TCP_NODELAY: %s", v5);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *a1, int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
if ( setsockopt(a2, 6, 1, optval, 4u) != -1 )
return 0;
v2 = -1;
v4 = _errno_location();
v5 = strerror(*v4);
func1(a1, "setsockopt TCP_NODELAY: %s", v5);
return v2;
}

original_code:
int wheatTcpNoDelay(char *err, int fd)
{
    int yes = 1;
    if (setsockopt(fd, IPPROTO_TCP, 1, &yes, sizeof(yes)) == -1)
    {
        wheatSetError(err, "setsockopt TCP_NODELAY: %s", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}

variable_types: yes int


wheatTcpKeepAlive

decompiled_code:
__int64 __fastcall wheatTcpKeepAlive(char *a1, int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
if ( setsockopt(a2, 1, 9, optval, 4u) != -1 )
return 0;
v2 = -1;
v4 = _errno_location();
v5 = strerror(*v4);
wheatSetError(a1, "setsockopt SO_KEEPALIVE: %s", v5);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *a1, int a2)
{
unsigned int v2; // ebx
int *v4; // rax
char *v5; // rax
int optval[7]; // [rsp+Ch] [rbp-1Ch] BYREF

optval[0] = 1;
if ( setsockopt(a2, 1, 9, optval, 4u) != -1 )
return 0;
v2 = -1;
v4 = _errno_location();
v5 = strerror(*v4);
func1(a1, "setsockopt SO_KEEPALIVE: %s", v5);
return v2;
}

original_code:
int wheatTcpKeepAlive(char *err, int fd)
{
    int yes = 1;
    if (setsockopt(fd, 1, 9, &yes, sizeof(yes)) == -1) {
        wheatSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}


wheatTcpAccept

decompiled_code:
__int64 __fastcall wheatTcpAccept(char *a1, int a2, char *a3, _DWORD *a4)
{
unsigned int v6; // ebx
socklen_t v8; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v9[2]; // [rsp+10h] [rbp-28h] BYREF

v8 = 16;
v6 = wheatGenericAccept(a1, a2, v9, &v8);
if ( v6 != -1 )
{
if ( a3 )
inet_ntop(2, &v9[0].sa_data[2], a3, 0x10u);
if ( a4 )
*a4 = (unsigned __int16)_bswap_16(*(__int16 *)v9[0].sa_data);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *a1, int a2, char *a3, _DWORD *a4)
{
unsigned int v6; // ebx
socklen_t v8; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr v9[2]; // [rsp+10h] [rbp-28h] BYREF

v8 = 16;
v6 = func3(a1, a2, v9, &v8);
if ( v6 != -1 )
{
if ( a3 )
inet_ntop(2, &v9[0].sa_data[2], a3, 0x10u);
if ( a4 )
*a4 = (unsigned __int16)func0(*(__int16 *)v9[0].sa_data);
}
return v6;
}

original_code:
int wheatTcpAccept(char *err, int s, char *ip, int *port) {
    int fd;
    struct sockaddr_in sa;
    socklen_t salen = sizeof(sa);
    if ((fd = wheatGenericAccept(err, s, (struct sockaddr*)&sa,&salen)) == -1)
        return -1;
    if (ip) inet_ntop(2, &sa.sin_addr, ip, 16);
    if (port) *port = ntohs(sa.sin_port);
    return fd;
}

canonical_original_code:
int wheatTcpAccept(char *err, int s, char *ip, int *port) {
    int fd;
    struct sockaddr_in sa;
    unsigned int salen = sizeof(sa);
    if ((fd = wheatGenericAccept(err, s, (struct sockaddr *)&sa,&salen)) == -1)
        return -1;
    if (ip) inet_ntop(2, &sa.sin_addr, ip, 16);
    if (port) *port = ntohs(sa.sin_port);
    return fd;
}

variable_types:
ip
char *

variable_types:
port
int *

variable_types:
salen
unsigned int


wheatTcpServer

decompiled_code:
__int64 __fastcall wheatTcpServer(char *a1, const char *a2, __int16 a3)
{
unsigned int v4; // ebx
struct sockaddr v6[3]; // [rsp+0h] [rbp-38h] BYREF

v4 = wheatCreateSocket(a1, 2);
if ( v4 != -1 )
{
*(_DWORD *)&v6[0].sa_data[10] = 0;
v6[0].sa_family = 2;
*(_WORD *)v6[0].sa_data = _bswap_16(a3);
*(_QWORD *)&v6[0].sa_data[2] = (unsigned int)_bswap_32(0);
if ( a2 && inet_pton(2, a2, &v6[0].sa_data[2]) != 1 )
{
wheatSetError(a1, "wheatTcpServer: invalid bind address");
close(v4);
v4 = -1;
}
else if ( (unsigned int)wheatListen(a1, v4, v6, 0x10u) == -1 )
{
wheatSetError(a1, "wheatTcpServer: listen failed");
close(v4);
v4 = -1;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func15(char *a1, const char *a2, __int16 a3)
{
unsigned int v4; // ebx
struct sockaddr v6[3]; // [rsp+0h] [rbp-38h] BYREF

v4 = func2(a1, 2);
if ( v4 != -1 )
{
*(_DWORD *)&v6[0].sa_data[10] = 0;
v6[0].sa_family = 2;
*(_WORD *)v6[0].sa_data = func0(a3);
*(_QWORD *)&v6[0].sa_data[2] = (unsigned int)func4(0);
if ( a2 && inet_pton(2, a2, &v6[0].sa_data[2]) != 1 )
{
func1(a1, "wheatTcpServer: invalid bind address");
close(v4);
v4 = -1;
}
else if ( (unsigned int)func5(a1, v4, v6, 0x10u) == -1 )
{
func1(a1, "wheatTcpServer: listen failed");
close(v4);
v4 = -1;
}
}
return v4;
}

original_code:
int wheatTcpServer(char *err, char *bind_addr, int port){
    int s;
    struct sockaddr_in sa;
    if ((s = wheatCreateSocket(err, 2)) == -1)
        return -1;
    memset(&sa,0,sizeof(sa));
    sa.sin_family = 2;
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    if (bind_addr && inet_pton(2, bind_addr, &sa.sin_addr) != 1) {
        wheatSetError(err, "wheatTcpServer: invalid bind address");
        close(s);
        return -1;
    }
    if (wheatListen(err, s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        wheatSetError(err, "wheatTcpServer: listen failed");
        close(s);
        return -1;
    }
    return s;
}

canonical_original_code:
int wheatTcpServer(char *err, char *bind_addr, int port){
    int s;
    struct sockaddr_in sa;
    if ((s = wheatCreateSocket(err, 2)) == -1)
        return -1;
    memset(&sa,0,sizeof(sa));
    sa.sin_family = 2;
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    if (bind_addr && inet_pton(2, bind_addr, &sa.sin_addr) != 1) {
        wheatSetError(err, "wheatTcpServer: invalid bind address");
        close(s);
        return -1;
    }
    if (wheatListen(err, s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
        wheatSetError(err, "wheatTcpServer: listen failed");
        close(s);
        return -1;
    }
    return s;
}


decompiled_code:
char **__fastcall wheat_stacktrace(int a1)
{
int v1; // ebx
char **result; // rax
char **v3; // r13
__int64 v4; // r12
char **v5; // rbp
__int64 v6; // rbx
__int64 v7; // rax
void *v8[69]; // [rsp+0h] [rbp-228h] BYREF

v1 = backtrace(v8, 64);
result = backtrace_symbols(v8, v1);
if ( result )
{
v3 = result;
if ( v1 > a1 + 1 )
{
v4 = (unsigned int)(v1 - a1 - 2);
v5 = &result[a1];
v6 = 0LL;
do
{
wheatLog(4LL, "[%d] %s", (unsigned int)v6, v5[v6 + 1]);
v7 = v6++;
}
while ( v7 != v4 );
}
result = (char **)wfree(v3);
}
return result;
}

canonical_decompiled_code:
char **__fastcall func0(int a1)
{
int v1; // ebx
char **result; // rax
char **v3; // r13
__int64 v4; // r12
char **v5; // rbp
__int64 v6; // rbx
__int64 v7; // rax
void *v8[69]; // [rsp+0h] [rbp-228h] BYREF

v1 = backtrace(v8, 64);
result = backtrace_symbols(v8, v1);
if ( result )
{
v3 = result;
if ( v1 > a1 + 1 )
{
v4 = (unsigned int)(v1 - a1 - 2);
v5 = &result[a1];
v6 = 0LL;
do
{
wheatLog(4LL, "[%d] %s", (unsigned int)v6, v5[v6 + 1]);
v7 = v6++;
}
while ( v7 != v4 );
}
result = (char **)wfree(v3);
}
return result;
}

original_code:
void wheat_stacktrace(int skip_count)
{
    void *stack[64];
    char **symbols;
    int size, i, j;
    size = backtrace(stack, 64);
    symbols = backtrace_symbols(stack, size);
    if (symbols == ((void *)0)) {
        return;
    }
    skip_count++;
    for (i = skip_count, j = 0; i < size; i++, j++) {
        wheatLog(4, "[%d] %s", j, symbols[i]);
    }
    wfree(symbols);
}

canonical_original_code:
void wheat_stacktrace(int skip_count)
{
    void *stack[64];
    char **symbols;
    int size; int i; int j;
    size = backtrace(stack, 64);
    symbols = backtrace_symbols(stack, size);
    if (symbols == ((void *)0)) {
        return;
    }
    skip_count++;
    for (i = skip_count, j = 0; i < size; i++, j++) {
        wheatLog(4, "[%d] %s", j, symbols[i]);
    }
    wfree(symbols);
}

variable_types:
stack
void *[64]

variable_types:
symbols
char **

variable_types: size int

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall wheat_assert(const char *a1, const char *a2, unsigned int a3, int a4)
{
__int64 result; // rax

result = wheatLog(4LL, "assert '%s' failed @ (%s, %d)", a1, a2, a3);
if ( a4 )
{
wheat_stacktrace(1);
abort();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2, unsigned int a3, int a4)
{
__int64 result; // rax

result = wheatLog(4LL, "assert '%s' failed @ (%s, %d)", a1, a2, a3);
if ( a4 )
{
func0(1);
abort();
}
return result;
}

original_code:
void wheat_assert(const char *cond, const char *file, int line, int panic)
{
    wheatLog(4, "assert '%s' failed @ (%s, %d)", cond, file, line);
    if (panic) {
        wheat_stacktrace(1);
        abort();
    }
}

canonical_original_code:
void wheat_assert(char *cond, char *file, int line, int panic)
{
    wheatLog(4, "assert '%s' failed @ (%s, %d)", cond, file, line);
    if (panic) {
        wheat_stacktrace(1);
        abort();
    }
}

variable_types:
cond
char *

variable_types:
file
char *

variable_types: line int

variable_types: panic int


decompiled_code:
ssize_t __fastcall portable_sendfile(int a1, int a2, off_t a3, size_t a4)
{
ssize_t result; // rax
off_t offset[2]; // [rsp+8h] [rbp-10h] BYREF

offset[0] = a3;
result = sendfile(a1, a2, offset, a4);
if ( result == -1 )
result = -(__int64)(*_errno_location() != 11);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int a1, int a2, off_t a3, size_t a4)
{
ssize_t result; // rax
off_t offset[2]; // [rsp+8h] [rbp-10h] BYREF

offset[0] = a3;
result = sendfile(a1, a2, offset, a4);
if ( result == -1 )
result = -(__int64)(*_errno_location() != 11);
return result;
}

original_code:
ssize_t portable_sendfile(int out_fd, int in_fd, off_t off, off_t len) {
    ssize_t ret;
    if ((ret = sendfile(out_fd, in_fd, &off, len)) == -1) {
        if ((*__errno_location ()) != 11)
            return -1;
        else
            return 0;
    }
    return ret;
}

canonical_original_code:
long portable_sendfile(int out_fd, int in_fd, long off, long len) {
    long ret;
    if ((ret = sendfile(out_fd, in_fd, &off, len)) == -1) {
        if ((*__errno_location ()) != 11)
            return -1;
        else
            return 0;
    }
    return ret;
}

variable_types: off long

variable_types: len long

variable_types: ret long

return_type: long


setProctitle

decompiled_code:
__int64 __fastcall setProctitle(const char *a1)
{
const char *v1; // rdx

v1 = (const char *)Server;
if ( !Server )
v1 = (const char *)&unk_6E;
return setproctitle(
"wheatserver: %s %s:%d-%d",
a1,
v1,
*((unsigned int *)&Server + 2),
*((unsigned int *)&Server + 3));
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
const char *v1; // rdx

v1 = (const char *)Server;
if ( !Server )
v1 = (const char *)&unk_6E;
return setproctitle(
"wheatserver: %s %s:%d-%d",
a1,
v1,
*((unsigned int *)&Server + 2),
*((unsigned int *)&Server + 3));
}

original_code:
void setProctitle(const char *title)
{
    setproctitle("wheatserver: %s %s:%d-%d", title,
            Server.bind_addr ? Server.bind_addr : "*",
            Server.port_range_start, Server.port_range_end);
}

canonical_original_code:
void setProctitle(char *title)
{
    setproctitle("wheatserver: %s %s:%d-%d", title,
            Server.bind_addr ? Server.bind_addr : "*",
            Server.port_range_start, Server.port_range_end);
}

variable_types:
title
char *


mbufGet

original_code:
static struct mbuf *mbufGet(size_t mbuf_size)
{
    struct mbuf *mbuf;
    uint8_t *m = wmalloc(sizeof(*mbuf)+mbuf_size+1);
    if (m == ((void *)0))
        return ((void *)0);
    mbuf = (struct mbuf *)(m + mbuf_size + 1);
    mbuf->end = m + mbuf_size;
    mbuf->read_pos = mbuf->write_pos = mbuf->start = m;
    mbuf->magic = 0x19920828;
    mbuf->next = ((void *)0);
    return mbuf;
}

canonical_original_code:
static struct mbuf *mbufGet(unsigned long mbuf_size)
{
    struct mbuf *mbuf;
    unsigned char *m = wmalloc(sizeof(*mbuf)+mbuf_size+1);
    if (m == ((void *)0))
        return ((void *)0);
    mbuf = (struct mbuf *)(m + mbuf_size + 1);
    mbuf->end = m + mbuf_size;
    mbuf->read_pos = mbuf->write_pos = mbuf->start = m;
    mbuf->magic = 0x19920828;
    mbuf->next = ((void *)0);
    return mbuf;
}

variable_types: struct mbuf { unsigned int magic; struct mbuf *next; unsigned char *read_pos; unsigned char *write_pos; unsigned char *start; unsigned char *end; } *

variable_types:
m
unsigned char *


mbufFree

decompiled_code:
__int64 __fastcall mbufFree(__int64 a1, __int64 a2)
{
return wfree(~a2 + a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
return wfree(~a2 + a1);
}

original_code:
static void mbufFree(struct mbuf *mbuf, size_t mbuf_size)
{
    uint8_t *m = (uint8_t *)mbuf - mbuf_size - 1;
    wfree(m);
}

canonical_original_code:
static void mbufFree(struct mbuf *mbuf, unsigned long mbuf_size)
{
    unsigned char *m = (uint8_t *)mbuf - mbuf_size - 1;
    wfree(m);
}

user_defined_types: struct mbuf { unsigned int magic; struct mbuf *next; unsigned char *read_pos; unsigned char *write_pos; unsigned char *start; unsigned char *end; }


msgRead

original_code:
void msgRead(struct msghdr *hdr, struct slice *s)
{
    ((void) sizeof ((hdr && s) ? 1 : 0), __extension__ ({ if (hdr && s) ; else __assert_fail ("hdr && s", "worker/mbuf.c", 120, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((hdr->is_set_readed_after_read == 1) ? 1 : 0), __extension__ ({ if (hdr->is_set_readed_after_read == 1) ; else __assert_fail ("hdr->is_set_readed_after_read == 1", "worker/mbuf.c", 121, __extension__ __PRETTY_FUNCTION__); }));
    struct mbuf *mbuf = hdr->last_read;
    ((void) sizeof ((mbuf->magic == 0x19920828) ? 1 : 0), __extension__ ({ if (mbuf->magic == 0x19920828) ; else __assert_fail ("mbuf->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 123, __extension__ __PRETTY_FUNCTION__); }));
    if (mbuf->read_pos == mbuf->write_pos && mbuf->write_pos == mbuf->end &&
            mbuf->next != ((void *)0)) {
        mbuf = mbuf->next;
    }
    sliceTo(s, mbuf->read_pos, mbuf->write_pos - mbuf->read_pos);
    hdr->last_read = mbuf;
    hdr->is_set_readed_after_read = 0;
}

variable_types: struct msghdr { struct mbuf { unsigned int magic; struct mbuf *next; unsigned char *read_pos; unsigned char *write_pos; unsigned char *start; unsigned char *end; } *last_write; struct mbuf *protected; struct mbuf *last_read; unsigned long mbuf_len; unsigned long mbuf_size; unsigned char is_set_writted_after_put; unsigned char is_set_readed_after_read; } *

variable_types: struct slice { unsigned char *data; unsigned long len; } *

user_defined_types: struct msghdr { struct mbuf *last_write; struct mbuf *protected; struct mbuf *last_read; unsigned long mbuf_len; unsigned long mbuf_size; unsigned char is_set_writted_after_put; unsigned char is_set_readed_after_read; }


msgPut

decompiled_code:
__int64 __fastcall msgPut(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 v4; // rax

if ( !a1 || !a2 )
_assert_fail("hdr && s", "worker/mbuf.c", 0x88u, "msgPut");
if ( *(_BYTE *)(a1 + 40) != 1 )
_assert_fail("hdr->is_set_writted_after_put == 1", "worker/mbuf.c", 0x89u, "msgPut");
v2 = *(_QWORD **)a1;
if ( **(_DWORD **)a1 != 429000744 )
_assert_fail("mbuf->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 0x8Cu, "msgPut");
if ( v2[3] != v2[5] )
goto LABEL_6;
v4 = mbufGet(*(_QWORD *)(a1 + 32));
if ( v4 )
{
v2[1] = v4;
++*(_QWORD *)(a1 + 24);
*(_QWORD *)a1 = v4;
v2 = (_QWORD *)v4;
LABEL_6:
sliceTo(a2, v2[3], v2[5] - v2[3]);
*(_BYTE *)(a1 + 40) = 0;
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 v4; // rax

if ( !a1 || !a2 )
_assert_fail("hdr && s", "worker/mbuf.c", 0x88u, "msgPut");
if ( *(_BYTE *)(a1 + 40) != 1 )
_assert_fail("hdr->is_set_writted_after_put == 1", "worker/mbuf.c", 0x89u, "msgPut");
v2 = *(_QWORD **)a1;
if ( **(_DWORD **)a1 != 429000744 )
_assert_fail("mbuf->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 0x8Cu, "msgPut");
if ( v2[3] != v2[5] )
goto LABEL_6;
v4 = func0(*(_QWORD *)(a1 + 32));
if ( v4 )
{
v2[1] = v4;
++*(_QWORD *)(a1 + 24);
*(_QWORD *)a1 = v4;
v2 = (_QWORD *)v4;
LABEL_6:
sliceTo(a2, v2[3], v2[5] - v2[3]);
*(_BYTE *)(a1 + 40) = 0;
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int msgPut(struct msghdr *hdr, struct slice *s)
{
    ((void) sizeof ((hdr && s) ? 1 : 0), __extension__ ({ if (hdr && s) ; else __assert_fail ("hdr && s", "worker/mbuf.c", 136, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((hdr->is_set_writted_after_put == 1) ? 1 : 0), __extension__ ({ if (hdr->is_set_writted_after_put == 1) ; else __assert_fail ("hdr->is_set_writted_after_put == 1", "worker/mbuf.c", 137, __extension__ __PRETTY_FUNCTION__); }));
    struct mbuf *mbuf = hdr->last_write;
    ((void) sizeof ((mbuf->magic == 0x19920828) ? 1 : 0), __extension__ ({ if (mbuf->magic == 0x19920828) ; else __assert_fail ("mbuf->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    if (mbuf->write_pos == mbuf->end) {
        struct mbuf *old_buf = mbuf;
        mbuf = mbufGet(hdr->mbuf_size);
        if (mbuf == ((void *)0))
            return -1;
        old_buf->next = mbuf;
        hdr->mbuf_len++;
        hdr->last_write = mbuf;
    }
    sliceTo(s, mbuf->write_pos, mbuf->end - mbuf->write_pos);
    hdr->is_set_writted_after_put = 0;
    return 0;
}


msgFree

decompiled_code:
__int64 __fastcall msgFree(_QWORD *a1)
{
__int64 i; // rbx
__int64 v3; // rdi

for ( i = a1[1]; i; --a1[3] )
{
v3 = i;
i = *(_QWORD *)(i + 8);
mbufFree(v3, a1[4]);
}
return wfree(a1);
}

original_code:
void msgFree(struct msghdr *hdr)
{
    struct mbuf *next, *curr = hdr->protected;
    while (curr != ((void *)0)) {
        next = curr->next;
        mbufFree(curr, hdr->mbuf_size);
        curr = next;
        hdr->mbuf_len--;
    }
    wfree(hdr);
}

canonical_original_code:
void msgFree(struct msghdr *hdr)
{
    struct mbuf *next; struct mbuf *curr = hdr->protected;
    while (curr != ((void *)0)) {
        next = curr->next;
        mbufFree(curr, hdr->mbuf_size);
        curr = next;
        hdr->mbuf_len--;
    }
    wfree(hdr);
}


msgSetReaded

decompiled_code:
__int64 __fastcall msgSetReaded(__int64 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 16);
if ( *(_DWORD *)result != 429000744 )
_assert_fail("hdr->last_read->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 0xA9u, "msgSetReaded");
v3 = *(_QWORD *)(result + 16);
if ( *(_QWORD *)(result + 24) - v3 < a2 )
_assert_fail("len <= hdr->last_read->write_pos - hdr->last_read->read_pos", "worker/mbuf.c", 0xAAu, "msgSetReaded");
if ( *(_BYTE *)(a1 + 41) )
_assert_fail("hdr->is_set_readed_after_read == 0", "worker/mbuf.c", 0xABu, "msgSetReaded");
*(_QWORD *)(result + 16) = v3 + a2;
*(_BYTE *)(a1 + 41) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = *(_QWORD *)(a1 + 16);
if ( *(_DWORD *)result != 429000744 )
_assert_fail("hdr->last_read->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 0xA9u, "msgSetReaded");
v3 = *(_QWORD *)(result + 16);
if ( *(_QWORD *)(result + 24) - v3 < a2 )
_assert_fail("len <= hdr->last_read->write_pos - hdr->last_read->read_pos", "worker/mbuf.c", 0xAAu, "msgSetReaded");
if ( *(_BYTE *)(a1 + 41) )
_assert_fail("hdr->is_set_readed_after_read == 0", "worker/mbuf.c", 0xABu, "msgSetReaded");
*(_QWORD *)(result + 16) = v3 + a2;
*(_BYTE *)(a1 + 41) = 1;
return result;
}

original_code:
void msgSetReaded(struct msghdr *hdr, size_t len)
{
    ((void) sizeof ((hdr->last_read->magic == 0x19920828) ? 1 : 0), __extension__ ({ if (hdr->last_read->magic == 0x19920828) ; else __assert_fail ("hdr->last_read->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 169, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((len <= hdr->last_read->write_pos - hdr->last_read->read_pos) ? 1 : 0), __extension__ ({ if (len <= hdr->last_read->write_pos - hdr->last_read->read_pos) ; else __assert_fail ("len <= hdr->last_read->write_pos - hdr->last_read->read_pos", "worker/mbuf.c", 170, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((hdr->is_set_readed_after_read == 0) ? 1 : 0), __extension__ ({ if (hdr->is_set_readed_after_read == 0) ; else __assert_fail ("hdr->is_set_readed_after_read == 0", "worker/mbuf.c", 171, __extension__ __PRETTY_FUNCTION__); }));
    hdr->last_read->read_pos += len;
    hdr->is_set_readed_after_read = 1;
}

canonical_original_code:
void msgSetReaded(struct msghdr *hdr, unsigned long len)
{
    ((void) sizeof ((hdr->last_read->magic == 0x19920828) ? 1 : 0), __extension__ ({ if (hdr->last_read->magic == 0x19920828) ; else __assert_fail ("hdr->last_read->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 169, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((len <= hdr->last_read->write_pos - hdr->last_read->read_pos) ? 1 : 0), __extension__ ({ if (len <= hdr->last_read->write_pos - hdr->last_read->read_pos) ; else __assert_fail ("len <= hdr->last_read->write_pos - hdr->last_read->read_pos", "worker/mbuf.c", 170, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((hdr->is_set_readed_after_read == 0) ? 1 : 0), __extension__ ({ if (hdr->is_set_readed_after_read == 0) ; else __assert_fail ("hdr->is_set_readed_after_read == 0", "worker/mbuf.c", 171, __extension__ __PRETTY_FUNCTION__); }));
    hdr->last_read->read_pos += len;
    hdr->is_set_readed_after_read = 1;
}

variable_types:
len
unsigned long


msgGetSize

decompiled_code:
__int64 __fastcall msgGetSize(__int64 a1)
{
return *(_QWORD *)(a1 + 32) * *(_QWORD *)(a1 + 24);
}

original_code:
size_t msgGetSize(struct msghdr *hdr)
{
    return hdr->mbuf_len * hdr->mbuf_size;
}

canonical_original_code:
unsigned long msgGetSize(struct msghdr *hdr)
{
    return hdr->mbuf_len * hdr->mbuf_size;
}


msgCanRead

original_code:
int msgCanRead(struct msghdr *hdr)
{
    struct mbuf *buf = hdr->last_read;
    ((void) sizeof ((buf->magic == 0x19920828) ? 1 : 0), __extension__ ({ if (buf->magic == 0x19920828) ; else __assert_fail ("buf->magic == WHEAT_MBUF_MAGIC", "worker/mbuf.c", 193, __extension__ __PRETTY_FUNCTION__); }));
    return (buf->read_pos != buf->write_pos) ||
        (buf->next != ((void *)0) && buf->next->write_pos != buf->next->start);
}


initHookCenter

decompiled_code:
__int64 initHookCenter()
{
_QWORD *v0; // rax
__int64 v1; // rbx
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rbx
__int64 v5; // rbx
__int64 result; // rax

v0 = (_QWORD *)wmalloc(48LL);
Server[132] = v0;
if ( v0 )
{
*v0 = createList();
v1 = Server[132];
*(_QWORD *)(v1 + 8) = createList();
v2 = Server[132];
*(_QWORD *)(v2 + 16) = createList();
v3 = Server[132];
*(_QWORD *)(v3 + 24) = createList();
v4 = Server[132];
*(_QWORD *)(v4 + 32) = createList();
v5 = Server[132];
result = createList();
*(_QWORD *)(v5 + 40) = result;
}
else
{
wheatLog(4LL, "init hookcenter failed");
result = halt(1LL);
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
_QWORD *v0; // rax
__int64 v1; // rbx
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rbx
__int64 v5; // rbx
__int64 result; // rax

v0 = (_QWORD *)wmalloc(48LL);
Server[132] = v0;
if ( v0 )
{
*v0 = createList();
v1 = Server[132];
*(_QWORD *)(v1 + 8) = createList();
v2 = Server[132];
*(_QWORD *)(v2 + 16) = createList();
v3 = Server[132];
*(_QWORD *)(v3 + 24) = createList();
v4 = Server[132];
*(_QWORD *)(v4 + 32) = createList();
v5 = Server[132];
result = createList();
*(_QWORD *)(v5 + 40) = result;
}
else
{
wheatLog(4LL, "init hookcenter failed");
result = halt(1LL);
}
return result;
}

original_code:
void initHookCenter()
{
    Server.hook_center = wmalloc(sizeof(struct hookCenter));
    if (Server.hook_center == ((void *)0)) {
        wheatLog(4, "init hookcenter failed");
        halt(1);
        return ;
    }
    Server.hook_center->afterinit = createList();
    Server.hook_center->whenready = createList();
    Server.hook_center->prefork_worker = createList();
    Server.hook_center->afterfork_worker = createList();
    Server.hook_center->whenwake = createList();
    Server.hook_center->beforesleep = createList();
}

user_defined_types: struct hookCenter { struct list *afterinit; struct list *whenready; struct list *prefork_worker; struct list *afterfork_worker; struct list *whenwake; struct list *beforesleep; }


sendReplyToClient

decompiled_code:
void __fastcall sendReplyToClient(__int64 a1, __int64 a2, unsigned int *a3)
{
if ( (a3[26] & 4) != 0 )
{
*(__m128i *)(a3 + 6) = _mm_loadu_si128((const __m128i *)((char *)&Server + 84072));
clientSendPacketList(a3);
if ( (a3[26] & 4) == 0 || !(unsigned int)isClientNeedSend(a3) )
{
wheatLog(0LL, "delete write event on sendReplyToClient");
deleteEvent(*(_QWORD *)(WorkerProcess + 48LL), *a3, 2LL);
tryFreeClient(a3);
}
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, __int64 a2, unsigned int *a3)
{
if ( (a3[26] & 4) != 0 )
{
*(__m128i *)(a3 + 6) = _mm_loadu_si128((const __m128i *)((char *)&Server + 84072));
clientSendPacketList(a3);
if ( (a3[26] & 4) == 0 || !(unsigned int)isClientNeedSend(a3) )
{
wheatLog(0LL, "delete write event on sendReplyToClient");
deleteEvent(*(_QWORD *)(WorkerProcess + 48LL), *a3, 2LL);
tryFreeClient(a3);
}
}
}

original_code:
static void sendReplyToClient(struct evcenter *center, int fd, void *data, int mask)
{
    struct masterClient *client = data;
    size_t bufpos = 0, totallen = wstrlen(client->response_buf);
    ssize_t nwritten = 0;
    while (bufpos < totallen) {
        struct slice slice;
        sliceTo(&slice, (uint8_t *)client->response_buf,
                wstrlen(client->response_buf));
        nwritten = writeBulkTo(client->fd, &slice);
        if (nwritten <= 0)
            break;
        bufpos += nwritten;
    }
    if (nwritten == -1) {
        freeMasterClient(client);
        return ;
    }
    if (bufpos == totallen) {
        deleteEvent(center, fd, 2);
    }
}

canonical_original_code:
static void sendReplyToClient(struct evcenter *center, int fd, void *data, int mask)
{
    struct masterClient *client = data;
    unsigned long bufpos = 0; unsigned long totallen = wstrlen(client->response_buf);
    long nwritten = 0;
    while (bufpos < totallen) {
        struct slice slice;
        sliceTo(&slice, (unsigned char *)client->response_buf,
                wstrlen(client->response_buf));
        nwritten = writeBulkTo(client->fd, &slice);
        if (nwritten <= 0)
            break;
        bufpos += nwritten;
    }
    if (nwritten == -1) {
        freeMasterClient(client);
        return ;
    }
    if (bufpos == totallen) {
        deleteEvent(center, fd, 2);
    }
}

variable_types: struct evcenter { int nevent; struct event { int mask; void *client_data; void (*read_proc)(struct evcenter *center, int fd, void *client_data, int mask); void (*write_proc)(struct evcenter *center, int fd, void *client_data, int mask); } *events; struct fired_event { int mask; int fd; } *fired_events; void *apidata; } *

variable_types: fd int

variable_types:
data
void *

variable_types: mask int

variable_types: struct masterClient { int fd; char *request_buf; char *response_buf; int argc; char **argv; } *

variable_types:
bufpos
unsigned long

variable_types:
totallen
unsigned long

variable_types: nwritten long

variable_types: struct slice { unsigned char *data; unsigned long len; }


wstrlen

decompiled_code:
__int64 __fastcall wstrlen(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 - 8);
}

original_code:
static inline int wstrlen(const wstr s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

canonical_original_code:
static inline int wstrlen(char *s)
{
    struct wstrhd *hd = (struct wstrhd *)(s - sizeof(struct wstrhd));
    return hd->len;
}

variable_types:
s
char *

variable_types: struct wstrhd { int len; int free; char buf[]; } *


main

canonical_name: main

repo: ywllx/study

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int **v3; // rax
unsigned int **v4; // rbp
__int64 i; // rbx
unsigned int *j; // rbx
unsigned int *k; // rbx
unsigned int *v8; // rax
unsigned int *v9; // r12
unsigned int *l; // rbx
void *v11; // rax
void *v12; // r15
unsigned int *v13; // rbx
unsigned int *v14; // rbp
unsigned int v15; // er13
int v16; // ebp
FILE *v17; // rsi
void *v18; // rax
void *v19; // r13
unsigned int v20; // ebp
int v21; // ebx
int result; // eax
int v23; // [rsp+0h] [rbp-5Ch]
int v24[8]; // [rsp+4h] [rbp-58h] BYREF
char v25[56]; // [rsp+24h] [rbp-38h] BYREF

v24[0] = 1;
v24[1] = 4;
v24[2] = 5;
v24[3] = 21;
v24[4] = 34;
v24[5] = 12;
v24[6] = 5;
v24[7] = 4;
v3 = (unsigned int **)malloc(0x18uLL);
if ( v3 )
{
v4 = v3;
list_init(v3, argv);
for ( i = 1LL; i != 9; ++i )
list_ins(v4, (unsigned int)i, (unsigned int)*(&v23 + i));
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( j = v4[1]; j; j = (unsigned int *)*((_QWORD *)j + 1) )
printf("%d ", *j);
fputc(10, stdout);
list_ins(v4, 3LL, 134LL);
fprintf(stdout, "after insert the %dth element: %d\n", 3LL, 134LL);
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( k = v4[1]; k; k = (unsigned int *)*((_QWORD *)k + 1) )
printf("%d ", *k);
fputc(10, stdout);
v8 = (unsigned int *)malloc(4uLL);
v9 = v8;
if ( v8 )
{
list_rem(v4, 5LL, v8);
fprintf(stdout, "after remove the %dth element: %d\n", 5LL, *v9);
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( l = v4[1]; l; l = (unsigned int *)*((_QWORD *)l + 1) )
printf("%d ", *l);
fwrite("\n\n", 1uLL, 2uLL, stdout);
v11 = malloc(0x18uLL);
v12 = v11;
if ( v11 )
{
list_init(v11, 1LL);
v13 = (unsigned int *)v24;
v14 = (unsigned int *)v24;
do
{
v15 = *v14;
fprintf(stdout, "Push the element: %d\n", *v14);
stack_push(v12, v15);
++v14;
}
while ( v25 != (char *)v14 );
fputc(10, stdout);
v16 = 4;
do
{
stack_pop(v12, v9);
fprintf(stdout, "Pop the element: %d\n", *v9);
--v16;
}
while ( v16 );
v17 = stdout;
fputc(10, stdout);
v18 = malloc(0x18uLL);
v19 = v18;
if ( v18 )
{
list_init(v18, v17);
do
{
v20 = *v13;
fprintf(stdout, "enqueue the element: %d\n", *v13);
queue_enqueue(v19, v20);
++v13;
}
while ( v25 != (char *)v13 );
fputc(10, stdout);
v21 = 8;
do
{
queue_dequeue(v19, v9);
fprintf(stdout, "dequeue the element: %d\n", *v9);
--v21;
}
while ( v21 );
fputc(10, stdout);
result = 1;
}
else
{
fwrite(&unk_40206F, 1uLL, 0x1CuLL, stderr);
result = -1;
}
}
else
{
fwrite("stack malloc function error\n", 1uLL, 0x1CuLL, stderr);
result = -1;
}
}
else
{
fwrite("malloc function error\n", 1uLL, 0x16uLL, stderr);
result = -1;
}
}
else
{
fwrite("list malloc function error\n", 1uLL, 0x1BuLL, stderr);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int **v3; // rax
unsigned int **v4; // rbp
__int64 i; // rbx
unsigned int *j; // rbx
unsigned int *k; // rbx
unsigned int *v8; // rax
unsigned int *v9; // r12
unsigned int *l; // rbx
void *v11; // rax
void *v12; // r15
unsigned int *v13; // rbx
unsigned int *v14; // rbp
unsigned int v15; // er13
int v16; // ebp
FILE *v17; // rsi
void *v18; // rax
void *v19; // r13
unsigned int v20; // ebp
int v21; // ebx
int result; // eax
int v23; // [rsp+0h] [rbp-5Ch]
int v24[8]; // [rsp+4h] [rbp-58h] BYREF
char v25[56]; // [rsp+24h] [rbp-38h] BYREF

v24[0] = 1;
v24[1] = 4;
v24[2] = 5;
v24[3] = 21;
v24[4] = 34;
v24[5] = 12;
v24[6] = 5;
v24[7] = 4;
v3 = (unsigned int **)malloc(0x18uLL);
if ( v3 )
{
v4 = v3;
func7(v3, argv);
for ( i = 1LL; i != 9; ++i )
func9(v4, (unsigned int)i, (unsigned int)*(&v23 + i));
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( j = v4[1]; j; j = (unsigned int *)*((_QWORD *)j + 1) )
printf("%d ", *j);
fputc(10, stdout);
func9(v4, 3LL, 134LL);
fprintf(stdout, "after insert the %dth element: %d\n", 3LL, 134LL);
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( k = v4[1]; k; k = (unsigned int *)*((_QWORD *)k + 1) )
printf("%d ", *k);
fputc(10, stdout);
v8 = (unsigned int *)malloc(4uLL);
v9 = v8;
if ( v8 )
{
func10(v4, 5LL, v8);
fprintf(stdout, "after remove the %dth element: %d\n", 5LL, *v9);
fwrite("print each element of the list\n", 1uLL, 0x1FuLL, stdout);
for ( l = v4[1]; l; l = (unsigned int *)*((_QWORD *)l + 1) )
printf("%d ", *l);
fwrite("\n\n", 1uLL, 2uLL, stdout);
v11 = malloc(0x18uLL);
v12 = v11;
if ( v11 )
{
func7(v11, 1LL);
v13 = (unsigned int *)v24;
v14 = (unsigned int *)v24;
do
{
v15 = *v14;
fprintf(stdout, "Push the element: %d\n", *v14);
func11(v12, v15);
++v14;
}
while ( v25 != (char *)v14 );
fputc(10, stdout);
v16 = 4;
do
{
func12(v12, v9);
fprintf(stdout, "Pop the element: %d\n", *v9);
--v16;
}
while ( v16 );
v17 = stdout;
fputc(10, stdout);
v18 = malloc(0x18uLL);
v19 = v18;
if ( v18 )
{
func7(v18, v17);
do
{
v20 = *v13;
fprintf(stdout, "enqueue the element: %d\n", *v13);
func13(v19, v20);
++v13;
}
while ( v25 != (char *)v13 );
fputc(10, stdout);
v21 = 8;
do
{
func14(v19, v9);
fprintf(stdout, "dequeue the element: %d\n", *v9);
--v21;
}
while ( v21 );
fputc(10, stdout);
result = 1;
}
else
{
fwrite(&unk_40206F, 1uLL, 0x1CuLL, stderr);
result = -1;
}
}
else
{
fwrite("stack malloc function error\n", 1uLL, 0x1CuLL, stderr);
result = -1;
}
}
else
{
fwrite("malloc function error\n", 1uLL, 0x16uLL, stderr);
result = -1;
}
}
else
{
fwrite("list malloc function error\n", 1uLL, 0x1BuLL, stderr);
result = -1;
}
return result;
}

original_code:
int main( )
{
 int a[] ={1, 4, 5, 21, 34, 12, 5, 4};
 List* list;
 if ((list = (List *)malloc(sizeof(List))) == ((void *)0)) {
  fprintf(stderr, "list malloc function error\n");
  return -1;
 }
 list_init(list);
 int i = 0;
 for (i = 0; i < 8; i++) {
  list_ins(list, i+1, a[i]);
 }
 fprintf(stdout, "print each element of the list\n");
 ListNode *node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n");
 int pos = 3;
 int ins = 134;
 list_ins(list, pos, ins);
 fprintf(stdout, "after insert the %dth element: %d\n", pos, ins);
 fprintf(stdout, "print each element of the list\n");
 node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n");
 int *p;
 if ((p = (int *)malloc(sizeof(int))) == ((void *)0)) {
  fprintf(stderr, "malloc function error\n");
  return -1;
 }
 pos = 5;
 list_rem(list, pos, p);
 fprintf(stdout, "after remove the %dth element: %d\n", pos, *p);
 fprintf(stdout, "print each element of the list\n");
 node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n\n");
 Stack* stack;
 if ((stack = (Stack *)malloc(sizeof(Stack))) == ((void *)0)) {
  fprintf(stderr, "stack malloc function error\n");
  return -1;
 }
 list_init(stack);
 for (i = 0; i < 8; i++) {
  fprintf(stdout, "Push the element: %d\n", a[i]);
  stack_push(stack, a[i]);
 }
 fprintf(stdout, "\n");
 for (i = 0; i < 4; i++) {
  stack_pop(stack, p);
  fprintf(stdout, "Pop the element: %d\n", *p);
 }
 fprintf(stdout, "\n");
 Queue* queue;
 if ((queue = (Queue *)malloc(sizeof(Queue))) == ((void *)0)) {
  fprintf(stderr, "Queue malloc function error\n");
  return -1;
 }
 list_init(queue);
 for (i = 0; i < 8; i++) {
  fprintf(stdout, "enqueue the element: %d\n", a[i]);
  queue_enqueue(queue, a[i]);
 }
 fprintf(stdout, "\n");
 for (i = 0; i < 8; i++) {
  queue_dequeue(queue, p);
  fprintf(stdout, "dequeue the element: %d\n", *p);
 }
 fprintf(stdout, "\n");
 return 1;
}

canonical_original_code:
int main()
{
 int a[] = {1, 4, 5, 21, 34, 12, 5, 4};
 struct List_ *list;
 if ((list = (struct List_ *)malloc(sizeof(List))) == ((void *)0)) {
  fprintf(stderr, "list malloc function error\n");
  return -1;
 }
 list_init(list);
 int i = 0;
 for (i = 0; i < 8; i++) {
  list_ins(list, i+1, a[i]);
 }
 fprintf(stdout, "print each element of the list\n");
 struct ListNode_ *node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n");
 int pos = 3;
 int ins = 134;
 list_ins(list, pos, ins);
 fprintf(stdout, "after insert the %dth element: %d\n", pos, ins);
 fprintf(stdout, "print each element of the list\n");
 node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n");
 int *p;
 if ((p = (int *)malloc(sizeof(int))) == ((void *)0)) {
  fprintf(stderr, "malloc function error\n");
  return -1;
 }
 pos = 5;
 list_rem(list, pos, p);
 fprintf(stdout, "after remove the %dth element: %d\n", pos, *p);
 fprintf(stdout, "print each element of the list\n");
 node = list->head;
 while(node != ((void *)0)) {
  printf("%d ", node->data);
  node = node->next;
 }
 fprintf(stdout, "\n\n");
 struct List_ *stack;
 if ((stack = (struct List_ *)malloc(sizeof(Stack))) == ((void *)0)) {
  fprintf(stderr, "stack malloc function error\n");
  return -1;
 }
 list_init(stack);
 for (i = 0; i < 8; i++) {
  fprintf(stdout, "Push the element: %d\n", a[i]);
  stack_push(stack, a[i]);
 }
 fprintf(stdout, "\n");
 for (i = 0; i < 4; i++) {
  stack_pop(stack, p);
  fprintf(stdout, "Pop the element: %d\n", *p);
 }
 fprintf(stdout, "\n");
 struct List_ *queue;
 if ((queue = (struct List_ *)malloc(sizeof(Queue))) == ((void *)0)) {
  fprintf(stderr, "Queue malloc function error\n");
  return -1;
 }
 list_init(queue);
 for (i = 0; i < 8; i++) {
  fprintf(stdout, "enqueue the element: %d\n", a[i]);
  queue_enqueue(queue, a[i]);
 }
 fprintf(stdout, "\n");
 for (i = 0; i < 8; i++) {
  queue_dequeue(queue, p);
  fprintf(stdout, "dequeue the element: %d\n", *p);
 }
 fprintf(stdout, "\n");
 return 1;
}

variable_types:
a
int []

variable_types: struct List_ { int size; struct ListNode_ { int data; struct ListNode_ *next; } *head; struct ListNode_ *tail; } *

variable_types: i int

variable_types: pos int

variable_types: ins int

variable_types:
p
int *

user_defined_types: struct List_ { int size; struct ListNode_ *head; struct ListNode_ *tail; }


original_code:
void list_init(List *list)
{
 list->size = 0;
 list->head = ((void *)0);
 list->tail = ((void *)0);
}

canonical_original_code:
void list_init(struct List_ *list)
{
 list->size = 0;
 list->head = ((void *)0);
 list->tail = ((void *)0);
}

user_defined_types: struct ListNode_ { int data; struct ListNode_ *next; }


decompiled_code:
__int64 __fastcall get_node(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // esi
int v4; // eax
__int64 result; // rax
__int64 v6; // rdx

if ( a2 <= 0 || *(_DWORD *)a1 < a2 )
{
fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
result = v6;
}
else
{
v2 = *(_QWORD *)(a1 + 8);
if ( a2 > 1 )
{
v3 = a2 - 1;
v4 = 0;
do
{
v2 = *(_QWORD *)(v2 + 8);
++v4;
}
while ( v4 != v3 );
}
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
__int64 v2; // rdx
int v3; // esi
int v4; // eax
__int64 result; // rax
__int64 v6; // rdx

if ( a2 <= 0 || *(_DWORD *)a1 < a2 )
{
fwrite("i is outof range of list size)\n", 1uLL, 0x1FuLL, stderr);
result = v6;
}
else
{
v2 = *(_QWORD *)(a1 + 8);
if ( a2 > 1 )
{
v3 = a2 - 1;
v4 = 0;
do
{
v2 = *(_QWORD *)(v2 + 8);
++v4;
}
while ( v4 != v3 );
}
result = v2;
}
return result;
}

original_code:
ListNode* get_node(List* list, int pos)
{
 if (pos <=0 || pos > (list->size)) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 int i = 0;
 ListNode* head = list->head;
 ListNode* node = head;
 while (i < (pos-1)) {
  node = node->next;
  i++;
 }
 return node;
}

canonical_original_code:
struct ListNode_ *get_node(struct List_ *list, int pos)
{
 if (pos <=0 || pos > (list->size)) {
  fprintf(stderr, "i is outof range of list size)\n");
  return;
 }
 int i = 0;
 struct ListNode_ *head = list->head;
 struct ListNode_ *node = head;
 while (i < (pos-1)) {
  node = node->next;
  i++;
 }
 return node;
}

variable_types: struct List_ { int size; struct ListNode_ *head; struct ListNode_ *tail; } *

variable_types: struct ListNode_ { int data; struct ListNode_ *next; } *