original_code:
void process_path(char * uri)
{
    char buf[256];
    char temp[256];
    char *bufp=buf;
    int ind=0, i, j;
    int len=strlen(uri);
    buf[0]='/';
    buf[1]=0;
    for (i=0; i<len; i++) {
        while (i<len && uri[i]=='/') i++;
        while (i<len && uri[i]!='/') temp[ind++]=uri[i++];
        temp[ind]=0;
        if (temp[0]==0)
            continue;
        if (strcmp(temp, "..")!=0) {
            *bufp='/';
            bufp++;
            for (j=0; j<ind; j++) {
                *bufp=temp[j];
                bufp++;
            }
            *bufp=0;
        } else {
            while (*bufp!='/') bufp--;
            if (bufp==buf) {
                *bufp='/';
                *(bufp+1)=0;
            } else
                *bufp=0;
        }
        ind=0;
    }
    strcpy(uri, buf);
}

canonical_original_code:
void process_path(char *uri)
{
    char buf[256];
    char temp[256];
    char *bufp = buf;
    int ind = 0; int i; int j;
    int len = strlen(uri);
    buf[0]='/';
    buf[1]=0;
    for (i=0; i<len; i++) {
        while (i<len && uri[i]=='/') i++;
        while (i<len && uri[i]!='/') temp[ind++]=uri[i++];
        temp[ind]=0;
        if (temp[0]==0)
            continue;
        if (strcmp(temp, "..")!=0) {
            *bufp='/';
            bufp++;
            for (j=0; j<ind; j++) {
                *bufp=temp[j];
                bufp++;
            }
            *bufp=0;
        } else {
            while (*bufp!='/') bufp--;
            if (bufp==buf) {
                *bufp='/';
                *(bufp+1)=0;
            } else
                *bufp=0;
        }
        ind=0;
    }
    strcpy(uri, buf);
}

variable_types:
uri
char *

variable_types:
buf
char [256]

variable_types:
bufp
char *

variable_types: ind int

variable_types: i int

variable_types: j int

variable_types: len int


repo: Wilbeibi/sws

decompiled_code:
char *__fastcall get_GET_query(__int64 a1, const char *a2)
{
char *result; // rax
char *v3; // rbx

result = strchr(a2, 63);
if ( result )
{
v3 = result;
if ( *(_DWORD *)a1 == 1 )
result = strcpy((char *)(a1 + 928), result + 1);
*v3 = 0;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 a1, const char *a2)
{
char *result; // rax
char *v3; // rbx

result = strchr(a2, 63);
if ( result )
{
v3 = result;
if ( *(_DWORD *)a1 == 1 )
result = strcpy((char *)(a1 + 928), result + 1);
*v3 = 0;
}
return result;
}

original_code:
void get_GET_query(Req_info * req, char * source)
{
    char * tmp = strstr(source,"?");
    if (tmp != ((void *)0) && req->method == 1) {
        strcpy(req->query,tmp+1);
    }
    if (tmp != ((void *)0))
        *tmp = '\0';
}

canonical_original_code:
void get_GET_query(struct req_info *req, char *source)
{
    char *tmp = strstr(source,"?");
    if (tmp != ((void *)0) && req->method == 1) {
        strcpy(req->query,tmp+1);
    }
    if (tmp != ((void *)0))
        *tmp = '\0';
}

variable_types: struct req_info { int method; int status; int cgi; int contLen; char uri[256]; char fstLine[256]; char clientIp[16]; char recvTime[256]; char ifModified[128]; char query[256]; char msg_body[1024]; } *

variable_types:
source
char *

variable_types:
tmp
char *

return_type: void


decompiled_code:
__int64 __fastcall parse_uri(__int64 a1, const char **a2)
{
char *v2; // r13
char *v5; // rbp
bool v6; // zf
unsigned int v7; // er14
char v9; // al
char *v10; // rbx
__int64 v11; // rdx
__int64 v12; // rcx
char v13[256]; // [rsp+0h] [rbp-328h] BYREF
char dest; // [rsp+100h] [rbp-228h] BYREF
char v15[255]; // [rsp+101h] [rbp-227h] BYREF
char name; // [rsp+200h] [rbp-128h] BYREF
char v17[295]; // [rsp+201h] [rbp-127h] BYREF

v2 = (char *)(a1 + 16);
strcpy(v13, (const char *)(a1 + 16));
v5 = strtok(v13, "?");
strtok(0LL, "?");
if ( strtok(0LL, "?") )
{
*(_DWORD *)(a1 + 4) = 400;
return (unsigned int)-1;
}
*(_DWORD *)(a1 + 8) = 0;
if ( *v5 == 126 )
{
v9 = v5[1];
v10 = v5 + 1;
if ( v9 == 47 )
{
v12 = 0LL;
LABEL_14:
*(&name + v12) = 0;
if ( getpwnam(&name) )
{
v7 = 0;
strncpy(&dest, v10 + 1, 0x100uLL);
process_path(&dest);
sprintf(v2, "/home/%s/sws/%s", &name, v15);
return v7;
}
}
else
{
v11 = 1LL;
while ( v9 )
{
++v10;
v15[v11 + 254] = v9;
v12 = (int)v11++;
v9 = *v10;
if ( *v10 == 47 )
goto LABEL_14;
}
}
*(_DWORD *)(a1 + 4) = 404;
v7 = -1;
}
else
{
v6 = memcmp(v5, "/cgi-bin/", 9uLL) == 0;
v7 = !v6;
if ( v6 )
{
*(_DWORD *)(a1 + 8) = 1;
strncpy(&dest, v5 + 9, 0x100uLL);
if ( *a2 )
{
process_path(&dest);
get_GET_query(a1, &dest);
sprintf(v2, "%s%s", *a2, v15);
}
else
{
strcpy(&name, v2);
process_path(v2);
sprintf(v2, "%s%s", a2[4], v17);
}
}
else
{
v7 = 0;
strcpy(&name, v2);
process_path(&name);
sprintf(v2, "%s%s", a2[4], v17);
}
}
return v7;
}

original_code:
int parse_uri(Req_info * req, Arg_t *optInfo)
{
    char _uri[256]; strcpy(_uri, req->uri);
    char *uri=strtok(_uri, "?");
    (void)strtok(((void *)0), "?");
    char *gar=strtok(((void *)0), "?");
    if (gar!=((void *)0)) {
        req->status=400;
        return -1;
    }
    char *tmp=uri;
    char usr[256];
    char rest[256];
    int i;
    req->cgi=0;
    if (strncmp(uri,"~",1) == 0) {
        tmp += 1;
        i = 0;
        while (*tmp != '/' ) {
            if ( *tmp == '\0') {
    req->status=404;
    return -1;
            }
            usr[i++] = *tmp++;
        }
        usr[i]='\0';
        struct passwd *pwd;
        if ((pwd=getpwnam(usr))==((void *)0)) {
            req->status=404;
            return -1;
        }
        strncpy(rest,tmp+1,256);
        process_path(rest);
            sprintf(req->uri,"/home/%s/sws/%s",usr,rest+1);
    }
    else if (strncmp(uri,"/cgi-bin/",9) == 0) {
        req->cgi=1;
        tmp = uri;
        tmp += 9;
        strncpy(rest,tmp,256);
        if (optInfo->cgiDir != ((void *)0)) {
            req->cgi=1;
            process_path(rest);
            get_GET_query(req,rest);
            sprintf(req->uri,"%s%s",optInfo->cgiDir,rest+1);
        }
        else {
            char tmp2[256];
            strcpy(tmp2,req->uri);
            process_path(req->uri);
            sprintf(req->uri,"%s%s",optInfo->dir, tmp2+1);
        }
    }
    else {
        char tmp2[256];
        strcpy(tmp2,req->uri);
        process_path(tmp2);
        sprintf(req->uri,"%s%s",optInfo->dir, tmp2+1);
    }
    return 0;
}

canonical_original_code:
int parse_uri(struct req_info *req, struct arg_t *optInfo)
{
    char _uri[256]; strcpy(_uri, req->uri);
    char *uri = strtok(_uri, "?");
    (void)strtok(((void *)0), "?");
    char *gar = strtok(((void *)0), "?");
    if (gar!=((void *)0)) {
        req->status=400;
        return -1;
    }
    char *tmp = uri;
    char usr[256];
    char rest[256];
    int i;
    req->cgi=0;
    if (strncmp(uri,"~",1) == 0) {
        tmp += 1;
        i = 0;
        while (*tmp != '/' ) {
            if ( *tmp == '\0') {
    req->status=404;
    return -1;
            }
            usr[i++] = *tmp++;
        }
        usr[i]='\0';
        struct passwd *pwd;
        if ((pwd=getpwnam(usr))==((void *)0)) {
            req->status=404;
            return -1;
        }
        strncpy(rest,tmp+1,256);
        process_path(rest);
            sprintf(req->uri,"/home/%s/sws/%s",usr,rest+1);
    }
    else if (strncmp(uri,"/cgi-bin/",9) == 0) {
        req->cgi=1;
        tmp = uri;
        tmp += 9;
        strncpy(rest,tmp,256);
        if (optInfo->cgiDir != ((void *)0)) {
            req->cgi=1;
            process_path(rest);
            get_GET_query(req,rest);
            sprintf(req->uri,"%s%s",optInfo->cgiDir,rest+1);
        }
        else {
            char tmp2[256];
            strcpy(tmp2,req->uri);
            process_path(req->uri);
            sprintf(req->uri,"%s%s",optInfo->dir, tmp2+1);
        }
    }
    else {
        char tmp2[256];
        strcpy(tmp2,req->uri);
        process_path(tmp2);
        sprintf(req->uri,"%s%s",optInfo->dir, tmp2+1);
    }
    return 0;
}

variable_types: struct arg_t { char *cgiDir; char *ipAddr; char *logFile; char *port; char *dir; } *

variable_types:
gar
char *

variable_types:
usr
char [256]

variable_types:
rest
char [256]

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

return_type: int

user_defined_types: struct req_info { int method; int status; int cgi; int contLen; char uri[256]; char fstLine[256]; char clientIp[16]; char recvTime[256]; char ifModified[128]; char query[256]; char msg_body[1024]; }


decompiled_code:
__int64 __fastcall parse_req_line(char *a1, __int64 a2)
{
char *v3; // rax
char *v4; // rsi
char *v5; // rbp
__int64 result; // rax
char *v7; // rax
char *v8; // rbp
unsigned int v9; // [rsp+8h] [rbp-20h] BYREF
unsigned int v10; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = strtok(a1, " ");
if ( !strcmp(v3, "GET") )
{
*(_DWORD *)a2 = 1;
}
else if ( !strcmp(v3, "POST") )
{
*(_DWORD *)a2 = 2;
}
else
{
if ( strcmp(v3, "HEAD") )
{
*(_DWORD *)(a2 + 4) = 501;
return 0xFFFFFFFFLL;
}
*(_DWORD *)a2 = 3;
head_response = 1;
}
v4 = strtok(0LL, " ");
if ( !v4 )
goto LABEL_6;
strcpy((char *)(a2 + 16), v4);
v5 = strtok(0LL, " ");
if ( !v5 )
{
if ( (*(_DWORD *)a2 & 0xFFFFFFFD) == 1 )
{
simple_response = 1;
return 0LL;
}
goto LABEL_6;
}
if ( strtok(0LL, " ") || (v7 = strstr(v5, "HTTP/")) == 0LL || v5 != v7 || (v8 = strtok(v7 + 5, " "), strtok(0LL, " ")) )
{
LABEL_6:
*(_DWORD *)(a2 + 4) = 400;
return 0xFFFFFFFFLL;
}
v9 = -1;
v10 = -1;
_isoc99_sscanf(v8, "%d.%d", &v9, &v10);
result = v9;
if ( v9 == 1 )
{
result = v10;
if ( v10 )
{
LABEL_14:
*(_DWORD *)(a2 + 4) = 505;
return 0xFFFFFFFFLL;
}
}
else if ( v9 || v10 != 9 )
{
goto LABEL_14;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, __int64 a2)
{
char *v3; // rax
char *v4; // rsi
char *v5; // rbp
__int64 result; // rax
char *v7; // rax
char *v8; // rbp
unsigned int v9; // [rsp+8h] [rbp-20h] BYREF
unsigned int v10; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = strtok(a1, " ");
if ( !strcmp(v3, "GET") )
{
*(_DWORD *)a2 = 1;
}
else if ( !strcmp(v3, "POST") )
{
*(_DWORD *)a2 = 2;
}
else
{
if ( strcmp(v3, "HEAD") )
{
*(_DWORD *)(a2 + 4) = 501;
return 0xFFFFFFFFLL;
}
*(_DWORD *)a2 = 3;
head_response = 1;
}
v4 = strtok(0LL, " ");
if ( !v4 )
goto LABEL_6;
strcpy((char *)(a2 + 16), v4);
v5 = strtok(0LL, " ");
if ( !v5 )
{
if ( (*(_DWORD *)a2 & 0xFFFFFFFD) == 1 )
{
simple_response = 1;
return 0LL;
}
goto LABEL_6;
}
if ( strtok(0LL, " ") || (v7 = strstr(v5, "HTTP/")) == 0LL || v5 != v7 || (v8 = strtok(v7 + 5, " "), strtok(0LL, " ")) )
{
LABEL_6:
*(_DWORD *)(a2 + 4) = 400;
return 0xFFFFFFFFLL;
}
v9 = -1;
v10 = -1;
_isoc99_sscanf(v8, "%d.%d", &v9, &v10);
result = v9;
if ( v9 == 1 )
{
result = v10;
if ( v10 )
{
LABEL_14:
*(_DWORD *)(a2 + 4) = 505;
return 0xFFFFFFFFLL;
}
}
else if ( v9 || v10 != 9 )
{
goto LABEL_14;
}
return result;
}

original_code:
int parse_req_line(char * buf, Req_info * req, Arg_t *optInfo)
{
    char *method;
    char *uri;
    char *version;
    char *gar;
    method=strtok(buf, " ");
    if (strcmp(method, "GET") == 0)
        req->method=1;
    else if (strcmp(method, "POST") == 0)
        req->method=2;
    else if (strcmp(method, "HEAD") == 0) {
        req->method=3;
        _head_response = 1;
    } else {
        req->status=501;
        return -1;
    }
    uri=strtok(((void *)0), " ");
    if (uri==((void *)0)) {
        req->status=400;
        return -1;
    }
    strcpy(req->uri, uri);
    version=strtok(((void *)0), " ");
    if (version==((void *)0)) {
        if (req->method == 1 || req->method==3)
            _simple_response = 1;
        else {
            req->status=400;
            return -1;
        }
    } else {
        gar=strtok(((void *)0), " ");
        if (gar!=((void *)0)) {
            req->status=400;
            return -1;
        }
        char *http=strstr(version, "HTTP/");
        if (http==((void *)0) || http!=version) {
            req->status=400;
            return -1;
        }
        http+=5;
        char *ver=strtok(http, " ");
        gar=strtok(((void *)0), " ");
        if (gar!=((void *)0)) {
            req->status=400;
            return -1;
        }
        int major=-1;
        int minor=-1;
        sscanf(ver, "%d.%d", &major, &minor);
        if ((major==1 && minor==0)
            || (major==0 && minor==9)) {
            return 0;
        }
        req->status=505;
        return -1;
    }
    return 0;
}

canonical_original_code:
int parse_req_line(char *buf, struct req_info *req, struct arg_t *optInfo)
{
    char *method;
    char *uri;
    char *version;
    char *gar;
    method=strtok(buf, " ");
    if (strcmp(method, "GET") == 0)
        req->method=1;
    else if (strcmp(method, "POST") == 0)
        req->method=2;
    else if (strcmp(method, "HEAD") == 0) {
        req->method=3;
        _head_response = 1;
    } else {
        req->status=501;
        return -1;
    }
    uri=strtok(((void *)0), " ");
    if (uri==((void *)0)) {
        req->status=400;
        return -1;
    }
    strcpy(req->uri, uri);
    version=strtok(((void *)0), " ");
    if (version==((void *)0)) {
        if (req->method == 1 || req->method==3)
            _simple_response = 1;
        else {
            req->status=400;
            return -1;
        }
    } else {
        gar=strtok(((void *)0), " ");
        if (gar!=((void *)0)) {
            req->status=400;
            return -1;
        }
        char *http = strstr(version, "HTTP/");
        if (http==((void *)0) || http!=version) {
            req->status=400;
            return -1;
        }
        http+=5;
        char *ver = strtok(http, " ");
        gar=strtok(((void *)0), " ");
        if (gar!=((void *)0)) {
            req->status=400;
            return -1;
        }
        int major = -1;
        int minor = -1;
        sscanf(ver, "%d.%d", &major, &minor);
        if ((major==1 && minor==0)
            || (major==0 && minor==9)) {
            return 0;
        }
        req->status=505;
        return -1;
    }
    return 0;
}

variable_types:
buf
char *

variable_types:
method
char *

variable_types:
version
char *

variable_types:
http
char *

variable_types:
ver
char *

variable_types: major int

variable_types: minor int

user_defined_types: struct arg_t { char *cgiDir; char *ipAddr; char *logFile; char *port; char *dir; }


original_code:
void parse_rest(int sock, char * buf, Req_info * req)
{
    char * tmp = strstr(buf,"If-Modified-Since:");
    int i= 0;
    if (tmp != ((void *)0) && req->method == 1) {
        tmp += 18;
        while ( *tmp++ != '\r') {
            req->ifModified[i++] = *tmp;
        }
        req->ifModified[i] = '\0';
    }
    tmp = strstr(buf,"Content-Length:");
    i= 0;
    if (tmp != ((void *)0)) {
        char len[10];
        tmp += 15;
        while ( *tmp++ != '\r' && i<10) {
            len[i++] = *tmp;
        }
        req->contLen = atoi(len);
    }
}

canonical_original_code:
void parse_rest(int sock, char *buf, struct req_info *req)
{
    char *tmp = strstr(buf,"If-Modified-Since:");
    int i = 0;
    if (tmp != ((void *)0) && req->method == 1) {
        tmp += 18;
        while ( *tmp++ != '\r') {
            req->ifModified[i++] = *tmp;
        }
        req->ifModified[i] = '\0';
    }
    tmp = strstr(buf,"Content-Length:");
    i= 0;
    if (tmp != ((void *)0)) {
        char len[10];
        tmp += 15;
        while ( *tmp++ != '\r' && i<10) {
            len[i++] = *tmp;
        }
        req->contLen = atoi(len);
    }
}

variable_types: sock int

variable_types: i int

variable_types:
len
char [10]


decompiled_code:
__int64 __fastcall read_sock(unsigned int a1, __int64 a2, const char **a3)
{
int v4; // ebx
unsigned __int8 v6[8232]; // [rsp+0h] [rbp-2028h] BYREF

sock = a1;
signal(14, (__sighandler_t)rd_timeout);
alarm(0x3Cu);
init_req(a2);
v4 = read_req_line(a1, a2, v6);
strcpy((char *)(a2 + 272), (const char *)v6);
*(_BYTE *)(a2 + strlen((const char *)v6) + 271) = 0;
get_timestamp(a2 + 544);
if ( v4 == -1
|| (unsigned int)parse_req_line((char *)v6, a2) == -1
|| (unsigned int)parse_uri(a2, a3) == -1
|| (unsigned int)read_rest(a1, a2) == -1 )
{
return sws_response(a1, a2);
}
if ( *(_DWORD *)a2 == 2 )
parse_rest(a1, (const char *)v6, (_DWORD *)a2);
if ( *(int *)(a2 + 12) > 0 )
Read(a1, a2 + 1184);
alarm(0);
signal(14, (__sighandler_t)wt_timeout);
signal(14, (__sighandler_t)wt_timeout);
alarm(0x78u);
return serve_request(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, __int64 a2, const char **a3)
{
int v4; // ebx
unsigned __int8 v6[8232]; // [rsp+0h] [rbp-2028h] BYREF

sock = a1;
signal(14, (__sighandler_t)rd_timeout);
alarm(0x3Cu);
func4(a2);
v4 = func2(a1, a2, v6);
strcpy((char *)(a2 + 272), (const char *)v6);
*(_BYTE *)(a2 + strlen((const char *)v6) + 271) = 0;
get_timestamp(a2 + 544);
if ( v4 == -1
|| (unsigned int)func8((char *)v6, a2) == -1
|| (unsigned int)func7(a2, a3) == -1
|| (unsigned int)func3(a1, a2) == -1 )
{
return sws_response(a1, a2);
}
if ( *(_DWORD *)a2 == 2 )
func9(a1, (const char *)v6, (_DWORD *)a2);
if ( *(int *)(a2 + 12) > 0 )
Read(a1, a2 + 1184);
alarm(0);
signal(14, (__sighandler_t)wt_timeout);
signal(14, (__sighandler_t)wt_timeout);
alarm(0x78u);
return serve_request(a1, a2);
}

original_code:
void read_sock(int sock, Req_info *req, Arg_t *optInfo)
{
    _sock=sock;
    signal(14, rd_timeout);
    alarm(60);
    int ret;
    char buf[8192];
    init_req(req);
    ret=read_req_line(sock,req,buf);
    strcpy(req->fstLine,buf);
    req->fstLine[strlen(buf)-1]=0;
    get_timestamp(req->recvTime);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=parse_req_line(buf,req,optInfo);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=parse_uri(req, optInfo);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=read_rest(sock,req);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    if (req->method == 2)
    parse_rest(sock,buf,req);
    if (req->contLen > 0)
        Read(sock, req->msg_body, req->contLen);
    alarm(0);
    signal(14, wt_timeout);
    signal(14, wt_timeout);
    alarm(120);
    serve_request(sock,req);
    return;
}

canonical_original_code:
void read_sock(int sock, struct req_info *req, struct arg_t *optInfo)
{
    _sock=sock;
    signal(14, rd_timeout);
    alarm(60);
    int ret;
    char buf[8192];
    init_req(req);
    ret=read_req_line(sock,req,buf);
    strcpy(req->fstLine,buf);
    req->fstLine[strlen(buf)-1]=0;
    get_timestamp(req->recvTime);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=parse_req_line(buf,req,optInfo);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=parse_uri(req, optInfo);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    ret=read_rest(sock,req);
    if (ret==-1) {
        sws_response(sock, req);
        return;
    }
    if (req->method == 2)
    parse_rest(sock,buf,req);
    if (req->contLen > 0)
        Read(sock, req->msg_body, req->contLen);
    alarm(0);
    signal(14, wt_timeout);
    signal(14, wt_timeout);
    alarm(120);
    serve_request(sock,req);
    return;
}

variable_types: struct req_info { int method; int status; int cgi; int contLen; char uri[256]; char fstLine[256]; char clientIp[16]; char recvTime[256]; char ifModified[128]; char query[256]; char msg_body[1024]; } *

variable_types: ret int

variable_types:
buf
char [8192]


original_code:
void get_timestamp(char *buf)
{
    time_t t;
    struct tm * tmp;
    char *Wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    char *Mth[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    time(&t);
    tmp = gmtime(&t);
    sprintf(buf,"%s, %d %s %d %d:%d:%d GMT",Wday[tmp->tm_wday],(tmp->tm_mday),Mth[tmp->tm_mon],(1900+tmp->tm_year),(tmp->tm_hour),(tmp->tm_min),(tmp->tm_sec));
}

canonical_original_code:
void get_timestamp(char *buf)
{
    long t;
    struct tm *tmp;
    char *Wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    char *Mth[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    time(&t);
    tmp = gmtime(&t);
    sprintf(buf,"%s, %d %s %d %d:%d:%d GMT",Wday[tmp->tm_wday],(tmp->tm_mday),Mth[tmp->tm_mon],(1900+tmp->tm_year),(tmp->tm_hour),(tmp->tm_min),(tmp->tm_sec));
}

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types:
Wday
char *[]

variable_types:
Mth
char *[]


original_code:
void get_status_msg(int code, char msg[]) {
    bzero(msg, 8192);
    switch(code){
    case 200:
        strcpy(msg, "OK");
        break;
    case 304:
        strcpy(msg,"Not Modified");
        break;
    case 400:
        strcpy(msg, "Bad Request");
        break;
    case 403:
        strcpy(msg, "Forbidden");
        break;
    case 404:
        strcpy(msg, "Not Found");
        break;
    case 408:
        strcpy(msg, "Request Timeout");
        break;
    case 501:
        strcpy(msg, "Not Implemented");
        break;
    case 505:
        strcpy(msg, "Version Not Support");
        break;
    case 522:
        strcpy(msg, "Connection Timed Out");
        break;
    default:
        strcpy(msg, "Unrecognized Statues");
        break;
    }
}

variable_types: code int

variable_types:
msg
char []


decompiled_code:
size_t __fastcall err_response(unsigned int a1, unsigned int a2)
{
size_t v2; // rax
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
char v10[256]; // [rsp+0h] [rbp-6118h] BYREF
char v11[16]; // [rsp+100h] [rbp-6018h] BYREF
char v12[16]; // [rsp+110h] [rbp-6008h] BYREF
char s[16]; // [rsp+2100h] [rbp-4018h] BYREF
__m128i v14; // [rsp+2110h] [rbp-4008h]
__m128i v15; // [rsp+2120h] [rbp-3FF8h]
char v16[8]; // [rsp+2130h] [rbp-3FE8h] BYREF
char v17[8216]; // [rsp+4100h] [rbp-2018h] BYREF

get_status_msg(a2, (__int64)v17);
strcpy(v16, "\r\n");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC30);
v14 = _mm_load_si128((const __m128i *)&LC31);
v15 = _mm_load_si128((const __m128i *)&LC32);
sprintf(s, "%s%d: %s\r\n", s, a2, v17);
sprintf(s, "%s May the force be with you.\r\n", s);
if ( simple_response != 1 )
{
sprintf(v11, "HTTP/1.0 %d %s\r\n", a2, v17);
v2 = strlen(v11);
Send(a1, v11, v2, 0LL);
get_timestamp(v10);
sprintf(v11, "Date: %s\r\n", v10);
v3 = strlen(v11);
Send(a1, v11, v3, 0LL);
strcpy(v12, "r\r\n");
*(__m128i *)v11 = _mm_load_si128((const __m128i *)&LC33);
v4 = strlen(v11);
Send(a1, v11, v4, 0LL);
strcpy(v12, "xt/html\r\n");
*(__m128i *)v11 = _mm_load_si128((const __m128i *)&LC34);
v5 = strlen(v11);
Send(a1, v11, v5, 0LL);
v6 = strlen(s);
sprintf(v11, "Content-length: %d\r\n\r\n", v6);
v7 = strlen(v11);
Send(a1, v11, v7, 0LL);
}
if ( head_response != 1 )
{
v8 = strlen(s);
Send(a1, s, v8, 0LL);
}
return strlen(s);
}

canonical_decompiled_code:
size_t __fastcall func2(unsigned int a1, unsigned int a2)
{
size_t v2; // rax
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
char v10[256]; // [rsp+0h] [rbp-6118h] BYREF
char v11[16]; // [rsp+100h] [rbp-6018h] BYREF
char v12[16]; // [rsp+110h] [rbp-6008h] BYREF
char s[16]; // [rsp+2100h] [rbp-4018h] BYREF
__m128i v14; // [rsp+2110h] [rbp-4008h]
__m128i v15; // [rsp+2120h] [rbp-3FF8h]
char v16[8]; // [rsp+2130h] [rbp-3FE8h] BYREF
char v17[8216]; // [rsp+4100h] [rbp-2018h] BYREF

func1(a2, (__int64)v17);
strcpy(v16, "\r\n");
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC30);
v14 = _mm_load_si128((const __m128i *)&LC31);
v15 = _mm_load_si128((const __m128i *)&LC32);
sprintf(s, "%s%d: %s\r\n", s, a2, v17);
sprintf(s, "%s May the force be with you.\r\n", s);
if ( simple_response != 1 )
{
sprintf(v11, "HTTP/1.0 %d %s\r\n", a2, v17);
v2 = strlen(v11);
Send(a1, v11, v2, 0LL);
func0(v10);
sprintf(v11, "Date: %s\r\n", v10);
v3 = strlen(v11);
Send(a1, v11, v3, 0LL);
strcpy(v12, "r\r\n");
*(__m128i *)v11 = _mm_load_si128((const __m128i *)&LC33);
v4 = strlen(v11);
Send(a1, v11, v4, 0LL);
strcpy(v12, "xt/html\r\n");
*(__m128i *)v11 = _mm_load_si128((const __m128i *)&LC34);
v5 = strlen(v11);
Send(a1, v11, v5, 0LL);
v6 = strlen(s);
sprintf(v11, "Content-length: %d\r\n\r\n", v6);
v7 = strlen(v11);
Send(a1, v11, v7, 0LL);
}
if ( head_response != 1 )
{
v8 = strlen(s);
Send(a1, s, v8, 0LL);
}
return strlen(s);
}

original_code:
int err_response(int fd, int status) {
    char buf[8192], body[8192], msg[8192];
    get_status_msg(status, msg);
    char date[256];
    sprintf(body, "<!DOCTYPE html><title>SWS Response</title>\r\n");
    sprintf(body, "%s%d: %s\r\n", body, status, msg);
    sprintf(body, "%s May the force be with you.\r\n", body);
    if (_simple_response !=1 ) {
        sprintf(buf, "HTTP/1.0 %d %s\r\n", status, msg);
        Send(fd, buf, strlen(buf),0);
        get_timestamp(date);
        sprintf(buf, "Date: %s\r\n",date);
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Server: Four0Four\r\n");
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Content-type: text/html\r\n");
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
        Send(fd, buf, strlen(buf), 0);
    }
    if (_head_response != 1)
        Send(fd, body, strlen(body), 0);
    return (int)strlen(body);
}

canonical_original_code:
int err_response(int fd, int status) {
    char buf[8192]; char body[8192]; char msg[8192];
    get_status_msg(status, msg);
    char date[256];
    sprintf(body, "<!DOCTYPE html><title>SWS Response</title>\r\n");
    sprintf(body, "%s%d: %s\r\n", body, status, msg);
    sprintf(body, "%s May the force be with you.\r\n", body);
    if (_simple_response !=1 ) {
        sprintf(buf, "HTTP/1.0 %d %s\r\n", status, msg);
        Send(fd, buf, strlen(buf),0);
        get_timestamp(date);
        sprintf(buf, "Date: %s\r\n",date);
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Server: Four0Four\r\n");
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Content-type: text/html\r\n");
        Send(fd, buf, strlen(buf), 0);
        sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body));
        Send(fd, buf, strlen(buf), 0);
    }
    if (_head_response != 1)
        Send(fd, body, strlen(body), 0);
    return (int)strlen(body);
}

variable_types: fd int

variable_types: status int

variable_types:
body
char [8192]

variable_types:
msg
char [8192]

variable_types:
date
char [256]


logging

decompiled_code:
void __fastcall logging(__int64 a1)
{
size_t v1; // rax
char s[8200]; // [rsp+0h] [rbp-2008h] BYREF

if ( logDir )
{
sprintf(
s,
"%s %s \"%s\" %d %d\n",
(const char *)(a1 + 528),
(const char *)(a1 + 544),
(const char *)(a1 + 272),
*(unsigned int *)(a1 + 4),
*(unsigned int *)(a1 + 12));
v1 = strlen(s);
fwrite(s, 1uLL, v1, log_fd);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
size_t v1; // rax
char s[8200]; // [rsp+0h] [rbp-2008h] BYREF

if ( logDir )
{
sprintf(
s,
"%s %s \"%s\" %d %d\n",
(const char *)(a1 + 528),
(const char *)(a1 + 544),
(const char *)(a1 + 272),
*(unsigned int *)(a1 + 4),
*(unsigned int *)(a1 + 12));
v1 = strlen(s);
fwrite(s, 1uLL, v1, log_fd);
}
}

original_code:
void logging(Req_info *req) {
    if (logDir != ((void *)0)) {
         char msg[8192];
        sprintf(msg, "%s %s \"%s\" %d %d\n",req->clientIp,req->recvTime,req->fstLine,req->status,req->contLen);
        (void)fwrite(msg,sizeof(char),strlen(msg),log_fd);
    }
}

canonical_original_code:
void logging(struct req_info *req) {
    if (logDir != ((void *)0)) {
         char msg[8192];
        sprintf(msg, "%s %s \"%s\" %d %d\n",req->clientIp,req->recvTime,req->fstLine,req->status,req->contLen);
        (void)fwrite(msg,sizeof(char),strlen(msg),log_fd);
    }
}


decompiled_code:
void __fastcall sws_response(unsigned int a1, __int64 a2)
{
*(_DWORD *)(a2 + 12) = err_response(a1, *(_DWORD *)(a2 + 4));
logging(a2);
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1, __int64 a2)
{
*(_DWORD *)(a2 + 12) = func2(a1, *(_DWORD *)(a2 + 4));
func3(a2);
}

original_code:
void sws_response(int fd, Req_info *req)
{
    req->contLen = err_response(fd, req->status);
    logging(req);
}

canonical_original_code:
void sws_response(int fd, struct req_info *req)
{
    req->contLen = err_response(fd, req->status);
    logging(req);
}


decompiled_code:
void __fastcall __noreturn sys_err(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void sys_err(const char *msg){
    perror(msg);
    exit( 1 );
}

canonical_original_code:
void sys_err(char *msg){
    perror(msg);
    exit( 1 );
}

variable_types:
msg
char *


Calloc

decompiled_code:
void *__fastcall Calloc(size_t a1, size_t a2)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
sys_err("calloc error");
return result;
}

canonical_decompiled_code:
void *__fastcall func1(size_t a1, size_t a2)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
func0("calloc error");
return result;
}

original_code:
void *Calloc(size_t n, size_t size){
    void *p;
    if( (p = calloc(n,size)) == ((void *)0))
        sys_err("calloc error");
    return (p);
}

canonical_original_code:
void *Calloc(unsigned long n, unsigned long size){
    void *p;
    if( (p = calloc(n,size)) == ((void *)0))
        sys_err("calloc error");
    return (p);
}

variable_types:
n
unsigned long

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *


Socket

decompiled_code:
int __fastcall Socket(int a1, int a2, int a3)
{
int result; // eax

result = socket(a1, a2, a3);
if ( result < 0 )
sys_err("socket error");
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, int a2, int a3)
{
int result; // eax

result = socket(a1, a2, a3);
if ( result < 0 )
func0("socket error");
return result;
}

original_code:
int Socket(int family, int type, int protocol){
    int n;
    if( (n = socket(family, type, protocol)) < 0)
        sys_err("socket error");
    return (n);
}

variable_types: family int

variable_types: type int

variable_types: protocol int

variable_types: n int


Accept

decompiled_code:
int __fastcall Accept(int a1, struct sockaddr *a2, socklen_t *a3)
{
int result; // eax

result = accept(a1, a2, a3);
if ( result < 0 )
sys_err("accept error");
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1, struct sockaddr *a2, socklen_t *a3)
{
int result; // eax

result = accept(a1, a2, a3);
if ( result < 0 )
func0("accept error");
return result;
}

original_code:
int Accept(int fd, struct sockaddr *sa, socklen_t *addrlen){
    int n;
    if( (n = accept(fd, sa, addrlen)) < 0){
        sys_err("accept error");
    }
    return (n);
}

canonical_original_code:
int Accept(int fd, struct sockaddr *sa, unsigned int *addrlen){
    int n;
    if( (n = accept(fd, sa, addrlen)) < 0){
        sys_err("accept error");
    }
    return (n);
}

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

variable_types:
addrlen
unsigned int *


Bind

decompiled_code:
int __fastcall Bind(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = bind(a1, a2, a3);
if ( result < 0 )
sys_err("bind error");
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = bind(a1, a2, a3);
if ( result < 0 )
func0("bind error");
return result;
}

original_code:
void Bind(int fd, const struct sockaddr *sa, socklen_t addrlen){
    if(bind(fd, sa, addrlen) < 0)
        sys_err("bind error");
}

canonical_original_code:
void Bind(int fd, struct sockaddr *sa, unsigned int addrlen){
    if(bind(fd, sa, addrlen) < 0)
        sys_err("bind error");
}

variable_types:
addrlen
unsigned int

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


Connect

decompiled_code:
int __fastcall Connect(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = connect(a1, a2, a3);
if ( result < 0 )
sys_err("connect error");
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1, const struct sockaddr *a2, socklen_t a3)
{
int result; // eax

result = connect(a1, a2, a3);
if ( result < 0 )
func0("connect error");
return result;
}

original_code:
void Connect(int fd, const struct sockaddr *sa, socklen_t addrlen){
    if(connect(fd, sa, addrlen) < 0)
        sys_err("connect error");
}

canonical_original_code:
void Connect(int fd, struct sockaddr *sa, unsigned int addrlen){
    if(connect(fd, sa, addrlen) < 0)
        sys_err("connect error");
}


Listen

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

result = listen(a1, a2);
if ( result < 0 )
sys_err("listen error");
return result;
}

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

result = listen(a1, a2);
if ( result < 0 )
func0("listen error");
return result;
}

original_code:
void Listen(int fd, int backlog){
    if(listen(fd, backlog) < 0)
        sys_err("listen error");
}

variable_types: backlog int


Setsockopt

decompiled_code:
int __fastcall Setsockopt(int a1, int a2, int a3, const void *a4, socklen_t a5)
{
int result; // eax

result = setsockopt(a1, a2, a3, a4, a5);
if ( result < 0 )
sys_err("setsockopt error");
return result;
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2, int a3, const void *a4, socklen_t a5)
{
int result; // eax

result = setsockopt(a1, a2, a3, a4, a5);
if ( result < 0 )
func0("setsockopt error");
return result;
}

original_code:
void Setsockopt(int fd, int level, int optname, const void *optval, int optlen){
    if(setsockopt(fd, level, optname, optval, optlen) < 0)
        sys_err("setsockopt error");
}

canonical_original_code:
void Setsockopt(int fd, int level, int optname, void *optval, int optlen){
    if(setsockopt(fd, level, optname, optval, optlen) < 0)
        sys_err("setsockopt error");
}

variable_types: fd int

variable_types: level int

variable_types: optname int

variable_types:
optval
void *

variable_types: optlen int


Close

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

result = close(a1);
if ( result == -1 )
sys_err("close error");
return result;
}

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

result = close(a1);
if ( result == -1 )
func0("close error");
return result;
}

original_code:
void Close(int fd){
    if(close(fd) == -1)
        sys_err("close error");
}


decompiled_code:
int __fastcall Inet_pton(int a1, const char *a2, void *a3)
{
int result; // eax

result = inet_pton(a1, a2, a3);
if ( result <= 0 )
{
fprintf(stderr, "inet error ->%s", a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1, const char *a2, void *a3)
{
int result; // eax

result = inet_pton(a1, a2, a3);
if ( result <= 0 )
{
fprintf(stderr, "inet error ->%s", a2);
exit(1);
}
return result;
}

original_code:
void Inet_pton(int family, const char *strptr, void *addrptr){
    int n;
    if( (n = inet_pton(family, strptr, addrptr)) <= 0){
        fprintf(stderr, "inet error ->%s", strptr);
        exit ( 1 );
    }
}

canonical_original_code:
void Inet_pton(int family, char *strptr, void *addrptr){
    int n;
    if( (n = inet_pton(family, strptr, addrptr)) <= 0){
        fprintf(stderr, "inet error ->%s", strptr);
        exit ( 1 );
    }
}

variable_types:
strptr
char *

variable_types:
addrptr
void *


decompiled_code:
const char *__fastcall Inet_ntop(int a1, const void *a2, char *a3, socklen_t a4)
{
const char *result; // rax

result = inet_ntop(a1, a2, a3, a4);
if ( !result )
sys_err("inet_ntop error");
return result;
}

canonical_decompiled_code:
const char *__fastcall func10(int a1, const void *a2, char *a3, socklen_t a4)
{
const char *result; // rax

result = inet_ntop(a1, a2, a3, a4);
if ( !result )
func0("inet_ntop error");
return result;
}

original_code:
const char *Inet_ntop(int family, const void *addrptr, char *strptr, size_t len){
    const char *ptr;
    if ( (ptr = inet_ntop(family, addrptr, strptr, len)) == ((void *)0))
        sys_err("inet_ntop error");
    return(ptr);
}

canonical_original_code:
char *Inet_ntop(int family, void *addrptr, char *strptr, unsigned long len){
    char *ptr;
    if ( (ptr = inet_ntop(family, addrptr, strptr, len)) == ((void *)0))
        sys_err("inet_ntop error");
    return(ptr);
}

variable_types:
len
unsigned long

variable_types:
ptr
char *

return_type: char *


Getaddrinfo

decompiled_code:
__int64 __fastcall Getaddrinfo(const char *a1, const char *a2, const struct addrinfo *a3, struct addrinfo **a4)
{
int v4; // eax
const char *v6; // rax

v4 = getaddrinfo(a1, a2, a3, a4);
if ( v4 )
{
v6 = gai_strerror(v4);
fprintf(stderr, "getaddrinfo error: %s\n", v6);
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(const char *a1, const char *a2, const struct addrinfo *a3, struct addrinfo **a4)
{
int v4; // eax
const char *v6; // rax

v4 = getaddrinfo(a1, a2, a3, a4);
if ( v4 )
{
v6 = gai_strerror(v4);
fprintf(stderr, "getaddrinfo error: %s\n", v6);
exit(1);
}
return 0LL;
}

original_code:
int Getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res){
    int n;
    if( (n = getaddrinfo(hostname, servname, hints, res)) != 0){
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(n));
        exit(1);
    }
    return (n);
}

canonical_original_code:
int Getaddrinfo(char *hostname, char *servname, struct addrinfo *hints, struct addrinfo **res){
    int n;
    if( (n = getaddrinfo(hostname, servname, hints, res)) != 0){
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(n));
        exit(1);
    }
    return (n);
}

variable_types:
hostname
char *

variable_types:
servname
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types:
res
struct addrinfo **

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


Read

decompiled_code:
__int64 __fastcall Read(int a1, void *a2, size_t a3)
{
__int64 result; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rax

LODWORD(result) = read(a1, a2, a3);
v4 = result;
if ( (int)result >= 0 )
return (int)result;
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Read warning: %s.\n", v6);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, void *a2, size_t a3)
{
__int64 result; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rax

LODWORD(result) = read(a1, a2, a3);
v4 = result;
if ( (int)result >= 0 )
return (int)result;
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Read warning: %s.\n", v6);
return v4;
}

original_code:
ssize_t Read(int fd, void *buf, size_t count){
    int n;
    if ((n = read(fd, buf, count)) < 0)
        fprintf(stderr, "Read warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}

canonical_original_code:
long Read(int fd, void *buf, unsigned long count){
    int n;
    if ((n = read(fd, buf, count)) < 0)
        fprintf(stderr, "Read warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: n int

return_type: long


Write

decompiled_code:
__int64 __fastcall Write(int a1, const void *a2, size_t a3)
{
__int64 result; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rax

LODWORD(result) = write(a1, a2, a3);
v4 = result;
if ( (int)result >= 0 )
return (int)result;
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Write warning: %s.\n", v6);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1, const void *a2, size_t a3)
{
__int64 result; // rax
int v4; // ebx
int *v5; // rax
char *v6; // rax

LODWORD(result) = write(a1, a2, a3);
v4 = result;
if ( (int)result >= 0 )
return (int)result;
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "Write warning: %s.\n", v6);
return v4;
}

original_code:
ssize_t Write(int fd, const void *buf, size_t count){
    int n;
    if ((n = write(fd, buf, count)) < 0)
        fprintf(stderr, "Write warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}

canonical_original_code:
long Write(int fd, void *buf, unsigned long count){
    int n;
    if ((n = write(fd, buf, count)) < 0)
        fprintf(stderr, "Write warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}


Send

decompiled_code:
__int64 __fastcall Send(int a1, const void *a2, size_t a3, int a4)
{
__int64 result; // rax
int v5; // ebx
int *v6; // rax
char *v7; // rax

LODWORD(result) = send(a1, a2, a3, a4);
v5 = result;
if ( (int)result >= 0 )
return (int)result;
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Send warning: %s.\n", v7);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, const void *a2, size_t a3, int a4)
{
__int64 result; // rax
int v5; // ebx
int *v6; // rax
char *v7; // rax

LODWORD(result) = send(a1, a2, a3, a4);
v5 = result;
if ( (int)result >= 0 )
return (int)result;
v6 = _errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Send warning: %s.\n", v7);
return v5;
}

original_code:
ssize_t Send(int sock, const void *buf, size_t count, int flag){
    int n;
    if ((n = send(sock, buf, count,flag)) < 0)
        fprintf(stderr, "Send warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}

canonical_original_code:
long Send(int sock, void *buf, unsigned long count, int flag){
    int n;
    if ((n = send(sock, buf, count,flag)) < 0)
        fprintf(stderr, "Send warning: %s.\n", strerror((*__errno_location ())));
    return (n);
}

variable_types: sock int

variable_types: flag int


Readline

decompiled_code:
__int64 __fastcall Readline(int fd, _BYTE *a2)
{
unsigned int v2; // er12
int v4; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = 1;
while ( 1 )
{
v4 = read(fd, buf, 1uLL);
if ( v4 != 1 )
break;
if ( buf[0] != 10 )
{
++v2;
*a2++ = buf[0];
if ( v2 != 0x2000 )
continue;
}
goto LABEL_7;
}
if ( v4 )
return (unsigned int)-1;
if ( v2 != 1 )
{
LABEL_7:
*a2 = 0;
return v2;
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func15(int fd, _BYTE *a2)
{
unsigned int v2; // er12
int v4; // eax
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v2 = 1;
while ( 1 )
{
v4 = read(fd, buf, 1uLL);
if ( v4 != 1 )
break;
if ( buf[0] != 10 )
{
++v2;
*a2++ = buf[0];
if ( v2 != 0x2000 )
continue;
}
goto LABEL_7;
}
if ( v4 )
return (unsigned int)-1;
if ( v2 != 1 )
{
LABEL_7:
*a2 = 0;
return v2;
}
return 0;
}

original_code:
int Readline(int fd, char* buf)
{
    int n, rc;
    char c, *bufp = buf;
    for (n = 1; n < 8192; n++) {
        if ((rc = read(fd, &c, 1)) == 1) {
            if (c == '\n')
                break;
            *bufp++ = c;
        } else if (rc == 0) {
            if (n == 1)
                return 0;
            else
                break;
        } else
            return -1;
    }
    *bufp = '\0';
    return n;
}

canonical_original_code:
int Readline(int fd, char *buf)
{
    int n; int rc;
    char c; char *bufp = buf;
    for (n = 1; n < 8192; n++) {
        if ((rc = read(fd, &c, 1)) == 1) {
            if (c == '\n')
                break;
            *bufp++ = c;
        } else if (rc == 0) {
            if (n == 1)
                return 0;
            else
                break;
        } else
            return -1;
    }
    *bufp = '\0';
    return n;
}

variable_types: fd int

variable_types:
buf
char *

variable_types: rc int

variable_types: c char

variable_types:
bufp
char *


decompiled_code:
void child_handler()
{
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( waitpid(-1, stat_loc, 1) != -1 )
;
}

canonical_decompiled_code:
void func0()
{
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( waitpid(-1, stat_loc, 1) != -1 )
;
}

original_code:
static void child_handler(int sig){
    pid_t p;
    int status;
    while( (p = waitpid(-1, &status, 1)) != -1)
        ;
}

canonical_original_code:
static void child_handler(int sig){
    int p;
    int status;
    while( (p = waitpid(-1, &status, 1)) != -1)
        ;
}

variable_types: sig int

variable_types: p int

variable_types: status int


decompiled_code:
void __fastcall __noreturn server_listen(__int64 a1)
{
__int64 v1; // rsi
__int64 v2; // rdi
__int64 v3; // r15
int v4; // ebx
__int64 v5; // rbx
__int64 v6; // r13
unsigned int v7; // er12
int v9; // [rsp+14h] [rbp-A3Ch] BYREF
int v10; // [rsp+18h] [rbp-A38h] BYREF
int v11; // [rsp+1Ch] [rbp-A34h] BYREF
__int64 v12; // [rsp+20h] [rbp-A30h] BYREF
char src[16]; // [rsp+28h] [rbp-A28h] BYREF
struct timeval timeout; // [rsp+38h] [rbp-A18h] BYREF
__int128 v15[3]; // [rsp+48h] [rbp-A08h] BYREF
char v16[4]; // [rsp+78h] [rbp-9D8h] BYREF
int v17; // [rsp+7Ch] [rbp-9D4h]
fd_set readfds; // [rsp+F8h] [rbp-958h] BYREF
char v19[528]; // [rsp+178h] [rbp-8D8h] BYREF
char dest[1736]; // [rsp+388h] [rbp-6C8h] BYREF

v1 = *(_QWORD *)(a1 + 24);
v2 = *(_QWORD *)(a1 + 8);
v15[0] = 0LL;
v15[1] = 0LL;
v15[2] = 0LL;
v10 = 1;
DWORD2(v15[0]) = 1;
LODWORD(v15[0]) = 1;
Getaddrinfo(v2, v1, v15, &v12);
v3 = (unsigned int)Socket(*(unsigned int *)(v12 + 4), *(unsigned int *)(v12 + 8), *(unsigned int *)(v12 + 12));
Setsockopt(v3, 1LL, 2LL, &v10, 4LL);
v4 = v3 + 63;
Bind((unsigned int)v3, *(_QWORD *)(v12 + 24), *(unsigned int *)(v12 + 16));
Listen((unsigned int)v3, 10LL);
signal(17, (__sighandler_t)child_handler);
if ( (int)v3 >= 0 )
v4 = v3;
v5 = v4 >> 6;
v6 = 1LL << ((int)v3 % 64);
while ( 1 )
{
do
{
do
{
v9 = 128;
timeout.tv_sec = 5LL;
v11 = v17;
timeout.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v5] |= v6;
}
while ( (unsigned int)(select(v3 + 1, &readfds, 0LL, 0LL, &timeout) + 1) <= 1 );
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
v7 = Accept((unsigned int)v3, v16, &v9);
if ( !fork() )
{
pidfile_close(pfh);
signal(15, 0LL);
Close((unsigned int)v3);
Inet_ntop(2LL, &v11, src, 16LL);
strncpy(dest, src, 0x10uLL);
read_sock(v7, v19, a1);
exit(0);
}
Close(v7);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(__int64 a1)
{
__int64 v1; // rsi
__int64 v2; // rdi
__int64 v3; // r15
int v4; // ebx
__int64 v5; // rbx
__int64 v6; // r13
unsigned int v7; // er12
int v9; // [rsp+14h] [rbp-A3Ch] BYREF
int v10; // [rsp+18h] [rbp-A38h] BYREF
int v11; // [rsp+1Ch] [rbp-A34h] BYREF
__int64 v12; // [rsp+20h] [rbp-A30h] BYREF
char src[16]; // [rsp+28h] [rbp-A28h] BYREF
struct timeval timeout; // [rsp+38h] [rbp-A18h] BYREF
__int128 v15[3]; // [rsp+48h] [rbp-A08h] BYREF
char v16[4]; // [rsp+78h] [rbp-9D8h] BYREF
int v17; // [rsp+7Ch] [rbp-9D4h]
fd_set readfds; // [rsp+F8h] [rbp-958h] BYREF
char v19[528]; // [rsp+178h] [rbp-8D8h] BYREF
char dest[1736]; // [rsp+388h] [rbp-6C8h] BYREF

v1 = *(_QWORD *)(a1 + 24);
v2 = *(_QWORD *)(a1 + 8);
v15[0] = 0LL;
v15[1] = 0LL;
v15[2] = 0LL;
v10 = 1;
DWORD2(v15[0]) = 1;
LODWORD(v15[0]) = 1;
Getaddrinfo(v2, v1, v15, &v12);
v3 = (unsigned int)Socket(*(unsigned int *)(v12 + 4), *(unsigned int *)(v12 + 8), *(unsigned int *)(v12 + 12));
Setsockopt(v3, 1LL, 2LL, &v10, 4LL);
v4 = v3 + 63;
Bind((unsigned int)v3, *(_QWORD *)(v12 + 24), *(unsigned int *)(v12 + 16));
Listen((unsigned int)v3, 10LL);
signal(17, (__sighandler_t)child_handler);
if ( (int)v3 >= 0 )
v4 = v3;
v5 = v4 >> 6;
v6 = 1LL << ((int)v3 % 64);
while ( 1 )
{
do
{
do
{
v9 = 128;
timeout.tv_sec = 5LL;
v11 = v17;
timeout.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v5] |= v6;
}
while ( (unsigned int)(select(v3 + 1, &readfds, 0LL, 0LL, &timeout) + 1) <= 1 );
}
while ( (readfds.fds_bits[v5] & v6) == 0 );
v7 = Accept((unsigned int)v3, v16, &v9);
if ( !fork() )
{
pidfile_close(pfh);
signal(15, 0LL);
Close((unsigned int)v3);
Inet_ntop(2LL, &v11, src, 16LL);
strncpy(dest, src, 0x10uLL);
read_sock(v7, v19, a1);
exit(0);
}
Close(v7);
}
}

original_code:
int server_listen( Arg_t *optInfo){
    struct addrinfo hints, *res;
    char ipstr[16];
    int listenfd, connfd;
    struct sockaddr_storage client_addr;
    socklen_t clientlen;
    int ignore = 1;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if(optInfo->ipAddr == ((void *)0)){
        hints.ai_flags = 0x0001;
        Getaddrinfo(((void *)0), optInfo->port, &hints, &res);
    }
    else{
        Getaddrinfo(optInfo->ipAddr, optInfo->port, &hints, &res);
    }
    listenfd = Socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    Setsockopt(listenfd, 1, 2, &ignore, sizeof(int));
    Bind(listenfd, res->ai_addr, res->ai_addrlen);
    Listen(listenfd, 10);
    signal(17, child_handler);
    while(1){
        pid_t childpid;
        struct sockaddr_in* cliAdr = (struct sockaddr_in*)&client_addr;
        int ipAddr = cliAdr->sin_addr.s_addr;
        clientlen = sizeof(client_addr);
        Req_info req;
        struct timeval to;
        fd_set rdy;
        to.tv_sec=5;
        to.tv_usec=0;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdy)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&rdy)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))));
        int ret=select(listenfd+1, &rdy, 0, 0, &to);
        if (ret==-1)
            continue;
        else if (ret==0)
            continue;
        else if (((((&rdy)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)==0)
            continue;
        connfd = Accept(listenfd, (struct sockaddr *) &client_addr, &clientlen);
        if( (childpid = fork()) == 0){
            pidfile_close(pfh);
            signal(15, ((__sighandler_t) 0));
            Close(listenfd);
            Inet_ntop(2, &ipAddr, ipstr, 16);
            memset(req.clientIp,0,sizeof(req.clientIp));
            strncpy(req.clientIp,ipstr,16);
            read_sock(connfd, &req, optInfo);
            exit(0);
        }
        Close(connfd);
    }
    freeaddrinfo(res);
    return connfd;
}

canonical_original_code:
int server_listen(struct arg_t *optInfo){
    struct addrinfo hints; struct addrinfo *res;
    char ipstr[16];
    int listenfd; int connfd;
    struct sockaddr_storage client_addr;
    unsigned int clientlen;
    int ignore = 1;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if(optInfo->ipAddr == ((void *)0)){
        hints.ai_flags = 0x0001;
        Getaddrinfo(((void *)0), optInfo->port, &hints, &res);
    }
    else{
        Getaddrinfo(optInfo->ipAddr, optInfo->port, &hints, &res);
    }
    listenfd = Socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    Setsockopt(listenfd, 1, 2, &ignore, sizeof(int));
    Bind(listenfd, res->ai_addr, res->ai_addrlen);
    Listen(listenfd, 10);
    signal(17, child_handler);
    while(1){
        int childpid;
        struct sockaddr_in *cliAdr = (struct sockaddr_in*)&client_addr;
        int ipAddr = cliAdr->sin_addr.s_addr;
        clientlen = sizeof(client_addr);
        struct req_info req;
        struct timeval to;
        struct fd_set rdy;
        to.tv_sec=5;
        to.tv_usec=0;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rdy)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&rdy)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))));
        int ret = select(listenfd+1, &rdy, 0, 0, &to);
        if (ret==-1)
            continue;
        else if (ret==0)
            continue;
        else if (((((&rdy)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)==0)
            continue;
        connfd = Accept(listenfd, (struct sockaddr *) &client_addr, &clientlen);
        if( (childpid = fork()) == 0){
            pidfile_close(pfh);
            signal(15, ((void (*)(int)) 0));
            Close(listenfd);
            Inet_ntop(2, &ipAddr, ipstr, 16);
            memset(req.clientIp,0,sizeof(req.clientIp));
            strncpy(req.clientIp,ipstr,16);
            read_sock(connfd, &req, optInfo);
            exit(0);
        }
        Close(connfd);
    }
    freeaddrinfo(res);
    return connfd;
}

variable_types: struct arg_t { char *cgiDir; char *ipAddr; char *logFile; char *port; char *dir; } *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

variable_types:
ipstr
char [16]

variable_types: listenfd int

variable_types: connfd int

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

variable_types:
clientlen
unsigned int

variable_types: ignore int

variable_types: childpid 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: ipAddr int

variable_types: struct req_info { int method; int status; int cgi; int contLen; char uri[256]; char fstLine[256]; char clientIp[16]; char recvTime[256]; char ifModified[128]; char query[256]; char msg_body[1024]; }

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

variable_types:
rdy
struct fd_set { long __fds_bits[-1]; }

variable_types: ret int

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


decompiled_code:
int __fastcall print_char(char a1)
{
int result; // eax

if ( (unsigned __int8)(a1 - 32) > 0x5Eu )
result = putchar(46);
else
result = putchar(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func7(char a1)
{
int result; // eax

if ( (unsigned __int8)(a1 - 32) > 0x5Eu )
result = putchar(46);
else
result = putchar(a1);
return result;
}

original_code:
void print_char(char c) {
    if (c >= 32 && c < 127) {
        printf("%c", c);
    } else {
        printf(".");
    }
}

variable_types: c char


repo: WojciechMula/toys

decompiled_code:
unsigned __int64 __fastcall movmask_unsafe(unsigned int a1)
{
if ( (a1 & 0x7F7F7F7F) != 0 )
__assert_fail("(input & 0x7f7f7f7f) == 0", "movmask.c", 6u, "movmask_unsafe");
return ((33818640 * (unsigned __int64)a1) >> 32) & 0xF;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func7(unsigned int a1)
{
if ( (a1 & 0x7F7F7F7F) != 0 )
__assert_fail("(input & 0x7f7f7f7f) == 0", "movmask.c", 6u, "movmask_unsafe");
return ((33818640 * (unsigned __int64)a1) >> 32) & 0xF;
}

original_code:
uint32_t movmask_unsafe(const uint32_t input) {
    (((input & 0x7f7f7f7f) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f) == 0", "movmask.c", 6, __extension__ __PRETTY_FUNCTION__));
 const uint32_t mult = 0x02040810;
 const uint64_t result = (uint64_t)input * mult;
 return (result >> 32) & 0x0f;
}

canonical_original_code:
unsigned int movmask_unsafe(unsigned int input) {
    (((input & 0x7f7f7f7f) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f) == 0", "movmask.c", 6, __extension__ __PRETTY_FUNCTION__));
 unsigned int mult = 0x02040810;
 unsigned long result = (uint64_t)input * mult;
 return (result >> 32) & 0x0f;
}

variable_types:
input
unsigned int

variable_types:
mult
unsigned int

variable_types:
result
unsigned long

return_type: unsigned int


movmask

decompiled_code:
unsigned __int64 __fastcall movmask(int a1)
{
return movmask_unsafe(a1 & 0x80808080);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(int a1)
{
return func7(a1 & 0x80808080);
}

original_code:
uint32_t movmask(const uint32_t input) {
    return movmask_unsafe(input & 0x80808080);
}

canonical_original_code:
unsigned int movmask(unsigned int input) {
    return movmask_unsafe(input & 0x80808080);
}


decompiled_code:
__int64 __fastcall movmask64(__int64 a1)
{
return movmask64_unsafe(a1 & 0x8080808080808080LL);
}

original_code:
uint64_t movmask64(const uint64_t input) {
    return movmask64_unsafe(input & 0x8080808080808080lu);
}

canonical_original_code:
unsigned long movmask64(unsigned long input) {
    return movmask64_unsafe(input & 0x8080808080808080lu);
}

variable_types:
input
unsigned long

return_type: unsigned long


generate

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

if ( a1 < 0 )
__assert_fail("x >= 0", "demo.c", 8u, "generate");
if ( a1 > 15 )
__assert_fail("x < 16", "demo.c", 9u, "generate");
v1 = 0;
v2 = 0;
do
{
if ( _bittest(&a1, v1) )
v2 |= 1 << (8 * v1 + 7);
++v1;
}
while ( v1 != 4 );
return v2;
}

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

if ( a1 < 0 )
__assert_fail("x >= 0", "demo.c", 8u, "generate");
if ( a1 > 15 )
__assert_fail("x < 16", "demo.c", 9u, "generate");
v1 = 0;
v2 = 0;
do
{
if ( _bittest(&a1, v1) )
v2 |= 1 << (8 * v1 + 7);
++v1;
}
while ( v1 != 4 );
return v2;
}

original_code:
uint32_t generate(int x) {
 ((x >= 0) ? (void) (0) : __assert_fail ("x >= 0", "demo.c", 8, __extension__ __PRETTY_FUNCTION__));
 ((x < 16) ? (void) (0) : __assert_fail ("x < 16", "demo.c", 9, __extension__ __PRETTY_FUNCTION__));
 uint32_t result = 0;
 for (int i=0; i < 4; i++) {
  if (x & (1 << i))
   result |= 1 << (8*i + 7);
 }
 return result;
}

canonical_original_code:
unsigned int generate(int x) {
 ((x >= 0) ? (void) (0) : __assert_fail ("x >= 0", "demo.c", 8, __extension__ __PRETTY_FUNCTION__));
 ((x < 16) ? (void) (0) : __assert_fail ("x < 16", "demo.c", 9, __extension__ __PRETTY_FUNCTION__));
 unsigned int result = 0;
 for (int i = 0; i < 4; i++) {
  if (x & (1 << i))
   result |= 1 << (8*i + 7);
 }
 return result;
}

variable_types: x int

variable_types:
result
unsigned int

variable_types: i int


decompiled_code:
__int64 test32()
{
unsigned int v0; // er12
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // eax

v0 = 0;
while ( 1 )
{
v1 = generate(v0);
v2 = v1;
if ( (v1 & 0x7F7F7F7F) != 0 )
__assert_fail("(input & 0x7f7f7f7f) == 0", "demo.c", 0x1Au, "test32");
v3 = movmask(v1);
if ( v3 != v0 )
break;
if ( ++v0 == 16 )
return 1LL;
}
printf("%08x => %x (expected %x)\n", v2, v3, v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func12()
{
unsigned int v0; // er12
int v1; // eax
unsigned int v2; // ebp
unsigned int v3; // eax

v0 = 0;
while ( 1 )
{
v1 = func11(v0);
v2 = v1;
if ( (v1 & 0x7F7F7F7F) != 0 )
__assert_fail("(input & 0x7f7f7f7f) == 0", "demo.c", 0x1Au, "test32");
v3 = func8(v1);
if ( v3 != v0 )
break;
if ( ++v0 == 16 )
return 1LL;
}
printf("%08x => %x (expected %x)\n", v2, v3, v0);
return 0LL;
}

original_code:
int test32() {
 for (unsigned mask=0; mask < 16; mask++) {
  const uint32_t input = generate(mask);
  (((input & 0x7f7f7f7f) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f) == 0", "demo.c", 26, __extension__ __PRETTY_FUNCTION__));
  const uint32_t result = movmask(input);
        if (result != mask) {
      printf("%08x => %x (expected %x)\n", input, result, mask);
            return 0;
        }
 }
    return 1;
}

canonical_original_code:
int test32() {
 for (unsigned int mask = 0; mask < 16; mask++) {
  unsigned int input = generate(mask);
  (((input & 0x7f7f7f7f) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f) == 0", "demo.c", 26, __extension__ __PRETTY_FUNCTION__));
  unsigned int result = movmask(input);
        if (result != mask) {
      printf("%08x => %x (expected %x)\n", input, result, mask);
            return 0;
        }
 }
    return 1;
}

variable_types:
mask
unsigned int


decompiled_code:
__int64 __fastcall generate64(int a1)
{
char v1; // cl
unsigned int v2; // eax
__int64 v3; // r8

if ( a1 < 0 )
__assert_fail("x >= 0", "demo.c", 0x29u, "generate64");
if ( a1 > 255 )
__assert_fail("x < 256", "demo.c", 0x2Au, "generate64");
v1 = 7;
v2 = 0;
v3 = 0LL;
do
{
if ( _bittest(&a1, v2) )
v3 |= 1LL << v1;
++v2;
v1 += 8;
}
while ( v2 != 8 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
char v1; // cl
unsigned int v2; // eax
__int64 v3; // r8

if ( a1 < 0 )
__assert_fail("x >= 0", "demo.c", 0x29u, "generate64");
if ( a1 > 255 )
__assert_fail("x < 256", "demo.c", 0x2Au, "generate64");
v1 = 7;
v2 = 0;
v3 = 0LL;
do
{
if ( _bittest(&a1, v2) )
v3 |= 1LL << v1;
++v2;
v1 += 8;
}
while ( v2 != 8 );
return v3;
}

original_code:
uint64_t generate64(int x) {
 ((x >= 0) ? (void) (0) : __assert_fail ("x >= 0", "demo.c", 41, __extension__ __PRETTY_FUNCTION__));
 ((x < 256) ? (void) (0) : __assert_fail ("x < 256", "demo.c", 42, __extension__ __PRETTY_FUNCTION__));
 uint64_t result = 0;
 for (int i=0; i < 8; i++) {
  if (x & (1 << i))
   result |= 1lu << (8*i + 7);
 }
 return result;
}

canonical_original_code:
unsigned long generate64(int x) {
 ((x >= 0) ? (void) (0) : __assert_fail ("x >= 0", "demo.c", 41, __extension__ __PRETTY_FUNCTION__));
 ((x < 256) ? (void) (0) : __assert_fail ("x < 256", "demo.c", 42, __extension__ __PRETTY_FUNCTION__));
 unsigned long result = 0;
 for (int i = 0; i < 8; i++) {
  if (x & (1 << i))
   result |= 1lu << (8*i + 7);
 }
 return result;
}


decompiled_code:
__int64 test64()
{
__int64 i; // rbx
__int64 v1; // rax
__int64 v2; // rbp
__int64 v3; // rax

for ( i = 0LL; i != 256; ++i )
{
v1 = generate64(i);
v2 = v1;
if ( (v1 & 0x7F7F7F7F7F7F7F7FLL) != 0 )
__assert_fail("(input & 0x7f7f7f7f7f7f7f7flu) == 0", "demo.c", 0x3Bu, "test64");
v3 = movmask64(v1);
if ( i != v3 )
{
printf("%016lx => %lx (expected %x)\n", v2, v3, (unsigned int)i);
return 0LL;
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 i; // rbx
__int64 v1; // rax
__int64 v2; // rbp
__int64 v3; // rax

for ( i = 0LL; i != 256; ++i )
{
v1 = func13(i);
v2 = v1;
if ( (v1 & 0x7F7F7F7F7F7F7F7FLL) != 0 )
__assert_fail("(input & 0x7f7f7f7f7f7f7f7flu) == 0", "demo.c", 0x3Bu, "test64");
v3 = func10(v1);
if ( i != v3 )
{
printf("%016lx => %lx (expected %x)\n", v2, v3, (unsigned int)i);
return 0LL;
}
}
return 1LL;
}

original_code:
int test64() {
 for (unsigned mask=0; mask < 256; mask++) {
  const uint64_t input = generate64(mask);
  (((input & 0x7f7f7f7f7f7f7f7flu) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f7f7f7f7flu) == 0", "demo.c", 59, __extension__ __PRETTY_FUNCTION__));
  const uint64_t result = movmask64(input);
        if (result != mask) {
            printf("%016lx => %lx (expected %x)\n", input, result, mask);
            return 0;
        }
 }
    return 1;
}

canonical_original_code:
int test64() {
 for (unsigned int mask = 0; mask < 256; mask++) {
  unsigned long input = generate64(mask);
  (((input & 0x7f7f7f7f7f7f7f7flu) == 0) ? (void) (0) : __assert_fail ("(input & 0x7f7f7f7f7f7f7f7flu) == 0", "demo.c", 59, __extension__ __PRETTY_FUNCTION__));
  unsigned long result = movmask64(input);
        if (result != mask) {
            printf("%016lx => %lx (expected %x)\n", input, result, mask);
            return 0;
        }
 }
    return 1;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi

puts("testing 32-bit code...");
if ( (unsigned int)test32("testing 32-bit code...", argv) )
{
puts("testing 64-bit code...");
test64();
v3 = "OK";
}
else
{
puts("testing 64-bit code...");
v3 = "OK";
if ( !(unsigned int)test64() )
v3 = "FAILED";
}
puts(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi

puts("testing 32-bit code...");
if ( (unsigned int)func12("testing 32-bit code...", argv) )
{
puts("testing 64-bit code...");
func14();
v3 = "OK";
}
else
{
puts("testing 64-bit code...");
v3 = "OK";
if ( !(unsigned int)func14() )
v3 = "FAILED";
}
puts(v3);
return 0;
}

original_code:
int main() {
    int result = 0;
    puts("testing 32-bit code...");
    result = test32() || result;
    puts("testing 64-bit code...");
    result = test64() || result;
    puts(result ? "OK" : "FAILED");
}

variable_types: result int


seq

decompiled_code:
void __fastcall seq(__int64 a1, int a2)
{
__int64 i; // rax

if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
*(_DWORD *)(a1 + 4 * i) = i;
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2)
{
__int64 i; // rax

if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
*(_DWORD *)(a1 + 4 * i) = i;
}
}

original_code:
void seq(uint32_t* tbl, uint32_t n) {
 uint32_t i;
 for (i=0; i < n; i++)
  tbl[i] = i;
}

canonical_original_code:
void seq(unsigned int *tbl, unsigned int n) {
 unsigned int i;
 for (i=0; i < n; i++)
  tbl[i] = i;
}

variable_types:
tbl
unsigned int *

variable_types:
n
unsigned int

variable_types:
i
unsigned int


shuffle

decompiled_code:
void __fastcall shuffle(__int64 a1, unsigned int a2)
{
__int64 v2; // r13
__int64 v4; // rbx
unsigned int v5; // eax
int v6; // esi
_DWORD *v7; // rax
__int64 v8; // rax

if ( a2 > 1 )
{
v2 = a2 - 2;
v4 = 0LL;
do
{
v5 = rand();
v6 = *(_DWORD *)(a1 + 4 * v4);
v7 = (_DWORD *)(a1 + 4LL * (v5 % (a2 - (unsigned int)v4) + (unsigned int)v4));
*(_DWORD *)(a1 + 4 * v4) = *v7;
*v7 = v6;
v8 = v4++;
}
while ( v2 != v8 );
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, unsigned int a2)
{
__int64 v2; // r13
__int64 v4; // rbx
unsigned int v5; // eax
int v6; // esi
_DWORD *v7; // rax
__int64 v8; // rax

if ( a2 > 1 )
{
v2 = a2 - 2;
v4 = 0LL;
do
{
v5 = rand();
v6 = *(_DWORD *)(a1 + 4 * v4);
v7 = (_DWORD *)(a1 + 4LL * (v5 % (a2 - (unsigned int)v4) + (unsigned int)v4));
*(_DWORD *)(a1 + 4 * v4) = *v7;
*v7 = v6;
v8 = v4++;
}
while ( v2 != v8 );
}
}

original_code:
void shuffle(uint32_t* tbl, uint32_t n) {
 uint32_t i, j, x;
 if (n == 0)
  return;
 for (i=0; i < n - 1; i++) {
  j = rand() % (n - i);
  x = tbl[i];
  tbl[i] = tbl[i+j];
  tbl[i+j] = x;
 }
}

canonical_original_code:
void shuffle(unsigned int *tbl, unsigned int n) {
 unsigned int i; unsigned int j; unsigned int x;
 if (n == 0)
  return;
 for (i=0; i < n - 1; i++) {
  j = rand() % (n - i);
  x = tbl[i];
  tbl[i] = tbl[i+j];
  tbl[i+j] = x;
 }
}

variable_types:
j
unsigned int

variable_types:
x
unsigned int


scanmem

decompiled_code:
__int64 __fastcall scanmem(__int64 a1, unsigned int *a2, int a3)
{
__int64 v3; // rcx
__int64 result; // rax
__int64 v5; // rdx

if ( !a3 )
return 0LL;
v3 = (__int64)&a2[a3 - 1 + 1];
LODWORD(result) = 0;
do
{
v5 = *a2++;
result = (unsigned int)(*(_DWORD *)(a1 + 4 * v5) + result);
}
while ( (unsigned int *)v3 != a2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned int *a2, int a3)
{
__int64 v3; // rcx
__int64 result; // rax
__int64 v5; // rdx

if ( !a3 )
return 0LL;
v3 = (__int64)&a2[a3 - 1 + 1];
LODWORD(result) = 0;
do
{
v5 = *a2++;
result = (unsigned int)(*(_DWORD *)(a1 + 4 * v5) + result);
}
while ( (unsigned int *)v3 != a2 );
return result;
}

original_code:
uint32_t scanmem(uint32_t* tbl, uint32_t* indexes, uint32_t n) {
 uint32_t i, acc;
 acc = 0;
 for (i=0; i < n; i++)
  acc += tbl[indexes[i]];
 return acc;
}

canonical_original_code:
unsigned int scanmem(unsigned int *tbl, unsigned int *indexes, unsigned int n) {
 unsigned int i; unsigned int acc;
 acc = 0;
 for (i=0; i < n; i++)
  acc += tbl[indexes[i]];
 return acc;
}

variable_types:
indexes
unsigned int *

variable_types:
acc
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebx
size_t v5; // rbp
void *v6; // r12
void *v7; // rax
void *v8; // rbp
int v9; // er13
unsigned int v10; // eax
clock_t v11; // r13
clock_t v12; // rbx
double v14; // [rsp+0h] [rbp-38h]
double v15; // [rsp+8h] [rbp-30h]

if ( argc <= 1 )
{
fwrite("program table_size\n", 1uLL, 0x13uLL, stderr);
return 1;
}
v3 = strtoul(argv[1], 0LL, 0);
v4 = v3;
if ( v3 <= 0 )
{
fprintf(stderr, "table_size=%d <= 0\n", (unsigned int)v3);
return 1;
}
v5 = 4LL * v3;
v6 = malloc(v5);
v7 = malloc(v5);
v8 = v7;
if ( !v6 )
goto LABEL_11;
if ( !v7 )
{
free(v6);
LABEL_11:
if ( v8 )
free(v6);
}
seq(v6, v4);
seq(v8, v4);
printf("sequential scan... ");
fflush(stdout);
v9 = clock();
v15 = (double)(int)(clock() - v9);
printf("took %0.2fs\n", v15 / 1000000.0);
printf("shuffling indexes... ");
fflush(stdout);
v10 = time(0LL);
srand(v10);
shuffle(v8, v4);
putchar(10);
printf("random scan... ");
fflush(stdout);
v11 = clock();
v12 = clock();
v14 = (double)((int)v12 - (int)v11);
printf("took %0.2fs\n", v14 / 1000000.0);
if ( v11 != v12 )
printf("slower %0.3f times\n", v14 / v15);
free(v6);
free(v8);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
unsigned int v4; // ebx
size_t v5; // rbp
void *v6; // r12
void *v7; // rax
void *v8; // rbp
int v9; // er13
unsigned int v10; // eax
clock_t v11; // r13
clock_t v12; // rbx
double v14; // [rsp+0h] [rbp-38h]
double v15; // [rsp+8h] [rbp-30h]

if ( argc <= 1 )
{
fwrite("program table_size\n", 1uLL, 0x13uLL, stderr);
return 1;
}
v3 = strtoul(argv[1], 0LL, 0);
v4 = v3;
if ( v3 <= 0 )
{
fprintf(stderr, "table_size=%d <= 0\n", (unsigned int)v3);
return 1;
}
v5 = 4LL * v3;
v6 = malloc(v5);
v7 = malloc(v5);
v8 = v7;
if ( !v6 )
goto LABEL_11;
if ( !v7 )
{
free(v6);
LABEL_11:
if ( v8 )
free(v6);
}
func7(v6, v4);
func7(v8, v4);
printf("sequential scan... ");
fflush(stdout);
v9 = clock();
v15 = (double)(int)(clock() - v9);
printf("took %0.2fs\n", v15 / 1000000.0);
printf("shuffling indexes... ");
fflush(stdout);
v10 = time(0LL);
srand(v10);
func8(v8, v4);
putchar(10);
printf("random scan... ");
fflush(stdout);
v11 = clock();
v12 = clock();
v14 = (double)((int)v12 - (int)v11);
printf("took %0.2fs\n", v14 / 1000000.0);
if ( v11 != v12 )
printf("slower %0.3f times\n", v14 / v15);
free(v6);
free(v8);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
 int n = 0;
 uint32_t* table = ((void *)0);
 uint32_t* indexes = ((void *)0);
 clock_t ta1, ta2;
 clock_t tb1, tb2;
 if (argc < 2) {
  fprintf(stderr, "program table_size\n");
  return 1;
 }
 n = strtoul(argv[1], ((void *)0), 0);
 if (n <= 0) {
  fprintf(stderr, "table_size=%d <= 0\n", n);
  return 1;
 }
 table = malloc(sizeof(uint32_t)*n);
 indexes = malloc(sizeof(uint32_t)*n);
 if (table == ((void *)0) || indexes == ((void *)0)) {
  if (table) free(table);
  if (indexes) free(table);
 }
 seq(table, n);
 seq(indexes, n);
 printf("sequential scan... "); fflush(stdout);
 ta1 = clock();
  scanmem(table, indexes, n);
 ta2 = clock();
 printf("took %0.2fs\n", (double)(ta2 - ta1)/((__clock_t) 1000000));
 printf("shuffling indexes... "); fflush(stdout);
 srand(time(((void *)0)));
 shuffle(indexes, n);
 putchar('\n');
 printf("random scan... "); fflush(stdout);
 tb1 = clock();
  scanmem(table, indexes, n);
 tb2 = clock();
 printf("took %0.2fs\n", (double)(tb2 - tb1)/((__clock_t) 1000000));
 if (tb2 - tb1 != 0) {
  printf("slower %0.3f times\n", (double)(tb2 - tb1)/(ta2 - ta1));
 }
 free(table);
 free(indexes);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int n = 0;
 unsigned int *table = ((void *)0);
 unsigned int *indexes = ((void *)0);
 long ta1; long ta2;
 long tb1; long tb2;
 if (argc < 2) {
  fprintf(stderr, "program table_size\n");
  return 1;
 }
 n = strtoul(argv[1], ((void *)0), 0);
 if (n <= 0) {
  fprintf(stderr, "table_size=%d <= 0\n", n);
  return 1;
 }
 table = malloc(sizeof(unsigned int)*n);
 indexes = malloc(sizeof(unsigned int)*n);
 if (table == ((void *)0) || indexes == ((void *)0)) {
  if (table) free(table);
  if (indexes) free(table);
 }
 seq(table, n);
 seq(indexes, n);
 printf("sequential scan... "); fflush(stdout);
 ta1 = clock();
  scanmem(table, indexes, n);
 ta2 = clock();
 printf("took %0.2fs\n", (double)(ta2 - ta1)/((long) 1000000));
 printf("shuffling indexes... "); fflush(stdout);
 srand(time(((void *)0)));
 shuffle(indexes, n);
 putchar('\n');
 printf("random scan... "); fflush(stdout);
 tb1 = clock();
  scanmem(table, indexes, n);
 tb2 = clock();
 printf("took %0.2fs\n", (double)(tb2 - tb1)/((long) 1000000));
 if (tb2 - tb1 != 0) {
  printf("slower %0.3f times\n", (double)(tb2 - tb1)/(ta2 - ta1));
 }
 free(table);
 free(indexes);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: n int

variable_types:
table
unsigned int *


decompiled_code:
size_t __fastcall save_8bit_width(__int64 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
char *v4; // r14
char *v5; // r13
int v6; // ebp
unsigned __int8 v7; // bl
size_t result; // rax

v4 = (char *)&palette + 3 * a4;
v5 = (char *)&palette + 3 * a3;
v6 = 8;
v7 = 0x80;
do
{
while ( (v7 & a2) != 0 )
{
v7 >>= 1;
result = fwrite(v5, 3uLL, 1uLL, stdout);
if ( !--v6 )
return result;
}
v7 >>= 1;
result = fwrite(v4, 3uLL, 1uLL, stdout);
--v6;
}
while ( v6 );
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(__int64 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
char *v4; // r14
char *v5; // r13
int v6; // ebp
unsigned __int8 v7; // bl
size_t result; // rax

v4 = (char *)&palette + 3 * a4;
v5 = (char *)&palette + 3 * a3;
v6 = 8;
v7 = 0x80;
do
{
while ( (v7 & a2) != 0 )
{
v7 >>= 1;
result = fwrite(v5, 3uLL, 1uLL, stdout);
if ( !--v6 )
return result;
}
v7 >>= 1;
result = fwrite(v4, 3uLL, 1uLL, stdout);
--v6;
}
while ( v6 );
return result;
}

original_code:
void save_8bit_width(uint8_t char_code, uint8_t font_row, uint8_t fore, uint8_t back) {
 uint8_t mask;
 for (mask = 0x80; mask != 0; mask >>= 1) {
  if (font_row & mask)
   fwrite(&palette[fore][0], 3, 1, stdout);
  else
   fwrite(&palette[back][0], 3, 1, stdout);
 }
}

canonical_original_code:
void save_8bit_width(unsigned char char_code, unsigned char font_row, unsigned char fore, unsigned char back) {
 unsigned char mask;
 for (mask = 0x80; mask != 0; mask >>= 1) {
  if (font_row & mask)
   fwrite(&palette[fore][0], 3, 1, stdout);
  else
   fwrite(&palette[back][0], 3, 1, stdout);
 }
}

variable_types:
fore
unsigned char

variable_types:
back
unsigned char

variable_types:
mask
unsigned char


decompiled_code:
size_t __fastcall save_9bit_width(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
char *v6; // rdi

save_8bit_width(a1, a2, a3, a4);
if ( (unsigned __int8)(a1 + 65) <= 0x20u && (a2 & 1) != 0 )
v6 = (char *)&palette + 3 * a3;
else
v6 = (char *)&palette + 3 * a4;
return fwrite(v6, 3uLL, 1uLL, stdout);
}

canonical_decompiled_code:
size_t __fastcall func8(unsigned __int8 a1, unsigned __int8 a2, unsigned __int8 a3, unsigned __int8 a4)
{
char *v6; // rdi

func7(a1, a2, a3, a4);
if ( (unsigned __int8)(a1 + 65) <= 0x20u && (a2 & 1) != 0 )
v6 = (char *)&palette + 3 * a3;
else
v6 = (char *)&palette + 3 * a4;
return fwrite(v6, 3uLL, 1uLL, stdout);
}

original_code:
void save_9bit_width(uint8_t char_code, uint8_t font_row, uint8_t fore, uint8_t back) {
 save_8bit_width(char_code, font_row, fore, back);
 if (char_code >= 0xbf && char_code <= 0xdf && (font_row & 0x01))
  fwrite(&palette[fore][0], 3, 1, stdout);
 else
  fwrite(&palette[back][0], 3, 1, stdout);
}

canonical_original_code:
void save_9bit_width(unsigned char char_code, unsigned char font_row, unsigned char fore, unsigned char back) {
 save_8bit_width(char_code, font_row, fore, back);
 if (char_code >= 0xbf && char_code <= 0xdf && (font_row & 0x01))
  fwrite(&palette[fore][0], 3, 1, stdout);
 else
  fwrite(&palette[back][0], 3, 1, stdout);
}


swapbits

decompiled_code:
__int64 __fastcall swapbits(int a1)
{
return a1 & 0xFFFFFFAA | (4 * (_BYTE)a1) & 0x44 | ((int)(unsigned __int8)a1 >> 2) & 0x11;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
return a1 & 0xFFFFFFAA | (4 * (_BYTE)a1) & 0x44 | ((int)(unsigned __int8)a1 >> 2) & 0x11;
}

canonical_original_code:
unsigned char swapbits(unsigned char b) {
 return (b & 0xaa) | ((b & 0x44) >> 2) | ((b & 0x11) << 2);
}

variable_types:
b
unsigned char

return_type: unsigned char


ordie

decompiled_code:
int *__fastcall ordie(const char *a1)
{
int *result; // rax
unsigned int *v2; // rbx
unsigned int v3; // eax

result = __errno_location();
if ( *result )
{
v2 = (unsigned int *)result;
v3 = (unsigned int)strerror(*result);
fprintf(stderr, "%s [errno=%d]: %s\n", a1, *v2, (const char *)v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
int *__fastcall func11(const char *a1)
{
int *result; // rax
unsigned int *v2; // rbx
unsigned int v3; // eax

result = __errno_location();
if ( *result )
{
v2 = (unsigned int *)result;
v3 = (unsigned int)strerror(*result);
fprintf(stderr, "%s [errno=%d]: %s\n", a1, *v2, (const char *)v3);
exit(1);
}
return result;
}

original_code:
void ordie(char* info) {
 if ((*__errno_location ()) != 0) {
  fprintf(stderr, "%s [errno=%d]: %s\n", info, (*__errno_location ()), strerror((*__errno_location ())));
  exit(1);
 }
}

canonical_original_code:
void ordie(char *info) {
 if ((*__errno_location ()) != 0) {
  fprintf(stderr, "%s [errno=%d]: %s\n", info, (*__errno_location ()), strerror((*__errno_location ())));
  exit(1);
 }
}

variable_types:
info
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // er12
int v6; // ebp
int *v7; // rax
unsigned int *v8; // r13
int v9; // er14
unsigned __int8 *v10; // r13
__int64 (__fastcall *v11)(); // rbp
unsigned __int64 v12; // rsi
int v13; // er12
__int64 v14; // rdx
__int64 v15; // r15
__int64 v16; // rdi
__int64 v17; // rcx
unsigned __int8 v18; // al
__int64 v19; // rcx
unsigned int v21; // eax
int v22; // [rsp+Ch] [rbp-105Ch]
unsigned __int8 buf; // [rsp+1Eh] [rbp-104Ah] BYREF
unsigned __int8 v24; // [rsp+1Fh] [rbp-1049h] BYREF
int v25; // [rsp+20h] [rbp-1048h] BYREF
char *v26; // [rsp+28h] [rbp-1040h]
char s[4152]; // [rsp+30h] [rbp-1038h] BYREF

if ( argc <= 2 )
usage(argc, argv, envp);
v3 = atoi(argv[1]);
if ( (unsigned int)(v3 - 8) > 1 )
die("First argument (character width) must be either 8 or 9.");
v4 = atoi(argv[2]);
v5 = v4;
if ( v4 > 0x3F )
die("Second argument (console number) must lie in range 0..63");
snprintf(s, 0x1000uLL, "/dev/tty%d", v4);
v6 = open(s, 2);
ordie("open(tty)");
v25 = 2097408;
v26 = font;
ioctl(v6, 0x4B6BuLL, &v25);
ordie("ioctl(GIO_FONTX)");
ioctl(v6, 0x4B70uLL, &palette);
v7 = __errno_location();
v8 = (unsigned int *)v7;
if ( *v7 )
{
v21 = (unsigned int)strerror(*v7);
fprintf(stderr, "Can't load palette, using defaults. Reason [errno=%d]: %s\n", *v8, (const char *)v21);
*v8 = 0;
}
close(v6);
ordie("close(tty)");
snprintf(s, 0x1000uLL, "/dev/vcsa%d", v5);
v9 = open(s, 2);
ordie("open(vcsa)");
read(v9, &buf, 1uLL);
ordie("read(lines)");
read(v9, &v24, 1uLL);
ordie("read(columns)");
v10 = (unsigned __int8 *)malloc(2 * v24);
if ( !v10 )
die("malloc failed");
lseek(v9, 4LL, 0);
v11 = save_8bit_width;
if ( v3 != 8 )
v11 = save_9bit_width;
printf("P6\n%d %d\n255\n", v3 * (unsigned int)v24, 16 * (unsigned int)buf);
if ( buf )
{
v22 = 0;
do
{
v12 = (unsigned __int64)v10;
v13 = 0;
read(v9, v10, 2 * (unsigned int)v24);
do
{
v15 = 0LL;
if ( v24 )
{
do
{
v16 = v10[v15 + 1];
v17 = v10[v15];
v15 += 2LL;
v18 = swapbits(v16, v12, v14, v17);
v12 = (unsigned __int8)font[32 * v19 + v13];
((void (__fastcall *)(_QWORD, unsigned __int64, _QWORD, _QWORD))v11)(
(unsigned __int8)v19,
v12,
v18 & 0xF,
v18 >> 4);
}
while ( 2 * v24 > (int)v15 );
}
++v13;
}
while ( v13 != 16 );
++v22;
}
while ( buf > v22 );
}
free(v10);
close(v9);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // eax
unsigned int v5; // er12
int v6; // ebp
int *v7; // rax
unsigned int *v8; // r13
int v9; // er14
unsigned __int8 *v10; // r13
__int64 (__fastcall *v11)(); // rbp
unsigned __int64 v12; // rsi
int v13; // er12
__int64 v14; // rdx
__int64 v15; // r15
__int64 v16; // rdi
__int64 v17; // rcx
unsigned __int8 v18; // al
__int64 v19; // rcx
unsigned int v21; // eax
int v22; // [rsp+Ch] [rbp-105Ch]
unsigned __int8 buf; // [rsp+1Eh] [rbp-104Ah] BYREF
unsigned __int8 v24; // [rsp+1Fh] [rbp-1049h] BYREF
int v25; // [rsp+20h] [rbp-1048h] BYREF
char *v26; // [rsp+28h] [rbp-1040h]
char s[4152]; // [rsp+30h] [rbp-1038h] BYREF

if ( argc <= 2 )
func9(argc, argv, envp);
v3 = atoi(argv[1]);
if ( (unsigned int)(v3 - 8) > 1 )
func12("First argument (character width) must be either 8 or 9.");
v4 = atoi(argv[2]);
v5 = v4;
if ( v4 > 0x3F )
func12("Second argument (console number) must lie in range 0..63");
snprintf(s, 0x1000uLL, "/dev/tty%d", v4);
v6 = open(s, 2);
func11("open(tty)");
v25 = 2097408;
v26 = font;
ioctl(v6, 0x4B6BuLL, &v25);
func11("ioctl(GIO_FONTX)");
ioctl(v6, 0x4B70uLL, &palette);
v7 = __errno_location();
v8 = (unsigned int *)v7;
if ( *v7 )
{
v21 = (unsigned int)strerror(*v7);
fprintf(stderr, "Can't load palette, using defaults. Reason [errno=%d]: %s\n", *v8, (const char *)v21);
*v8 = 0;
}
close(v6);
func11("close(tty)");
snprintf(s, 0x1000uLL, "/dev/vcsa%d", v5);
v9 = open(s, 2);
func11("open(vcsa)");
read(v9, &buf, 1uLL);
func11("read(lines)");
read(v9, &v24, 1uLL);
func11("read(columns)");
v10 = (unsigned __int8 *)malloc(2 * v24);
if ( !v10 )
func12("malloc failed");
lseek(v9, 4LL, 0);
v11 = save_8bit_width;
if ( v3 != 8 )
v11 = save_9bit_width;
printf("P6\n%d %d\n255\n", v3 * (unsigned int)v24, 16 * (unsigned int)buf);
if ( buf )
{
v22 = 0;
do
{
v12 = (unsigned __int64)v10;
v13 = 0;
read(v9, v10, 2 * (unsigned int)v24);
do
{
v15 = 0LL;
if ( v24 )
{
do
{
v16 = v10[v15 + 1];
v17 = v10[v15];
v15 += 2LL;
v18 = func10(v16, v12, v14, v17);
v12 = (unsigned __int8)font[32 * v19 + v13];
((void (__fastcall *)(_QWORD, unsigned __int64, _QWORD, _QWORD))v11)(
(unsigned __int8)v19,
v12,
v18 & 0xF,
v18 >> 4);
}
while ( 2 * v24 > (int)v15 );
}
++v13;
}
while ( v13 != 16 );
++v22;
}
while ( buf > v22 );
}
free(v10);
close(v9);
return 0;
}

original_code:
int main(int argv, char* argc[]) {
 int fd;
 struct consolefontdesc dsc;
 char path[4096];
 save_fun save;
 int char_width, console_num;
 uint8_t lines, columns;
 uint8_t c, attr, fore, back;
 int col, line, y;
 uint8_t* char_row;
 if (argv < 3)
  usage();
 char_width = atoi(argc[1]);
 if (char_width != 8 && char_width != 9)
  die("First argument (character width) must be either 8 or 9.");
 console_num = atoi(argc[2]);
 if (console_num < 0 || console_num > 63)
  die("Second argument (console number) must lie in range 0..63");
 snprintf(path, 4096, "/dev/tty%d", console_num);
 fd = open(path, 02); ordie("open(tty)");
 dsc.charcount = 256;
 dsc.charheight = 32;
 dsc.chardata = (void*)font;
 ioctl(fd, 0x4B6B, &dsc); ordie("ioctl(GIO_FONTX)");
 ioctl(fd, 0x4B70, &palette);
 if ((*__errno_location ())) {
  fprintf(stderr, "Can't load palette, using defaults.  Reason [errno=%d]: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  (*__errno_location ()) = 0;
 }
 close(fd); ordie("close(tty)");
 snprintf(path, 4096, "/dev/vcsa%d", console_num);
 fd = open(path, 02); ordie("open(vcsa)");
 read(fd, &lines, 1); ordie("read(lines)");
 read(fd, &columns, 1); ordie("read(columns)");
 char_row = (uint8_t*)malloc(columns*2);
 if (char_row == ((void *)0))
  die("malloc failed");
 lseek(fd, 4, 0);
 if (char_width == 8)
  save = save_8bit_width;
 else
  save = save_9bit_width;
 printf("P6\n%d %d\n255\n", columns*char_width, lines*16);
 for (line=0; line < lines; line++) {
  read(fd, char_row, columns*2);
  for (y=0; y < 16; y++) {
   for (col=0; col < columns*2; col+=2) {
    c = char_row[col];
    attr = swapbits(char_row[col+1]);
    fore = attr & 0x0f;
    back = attr >> 4;
    save(c, font[c][y], fore, back);
   }
  }
 }
 free(char_row);
 close(fd);
 return 0;
}

canonical_original_code:
int main(int argv, char *argc[]) {
 int fd;
 struct consolefontdesc dsc;
 char path[4096];
 void (*save)(unsigned char char_code, unsigned char font_row, unsigned char fore, unsigned char back);
 int char_width; int console_num;
 unsigned char lines; unsigned char columns;
 unsigned char c; unsigned char attr; unsigned char fore; unsigned char back;
 int col; int line; int y;
 unsigned char *char_row;
 if (argv < 3)
  usage();
 char_width = atoi(argc[1]);
 if (char_width != 8 && char_width != 9)
  die("First argument (character width) must be either 8 or 9.");
 console_num = atoi(argc[2]);
 if (console_num < 0 || console_num > 63)
  die("Second argument (console number) must lie in range 0..63");
 snprintf(path, 4096, "/dev/tty%d", console_num);
 fd = open(path, 02); ordie("open(tty)");
 dsc.charcount = 256;
 dsc.charheight = 32;
 dsc.chardata = (void *)font;
 ioctl(fd, 0x4B6B, &dsc); ordie("ioctl(GIO_FONTX)");
 ioctl(fd, 0x4B70, &palette);
 if ((*__errno_location ())) {
  fprintf(stderr, "Can't load palette, using defaults.  Reason [errno=%d]: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  (*__errno_location ()) = 0;
 }
 close(fd); ordie("close(tty)");
 snprintf(path, 4096, "/dev/vcsa%d", console_num);
 fd = open(path, 02); ordie("open(vcsa)");
 read(fd, &lines, 1); ordie("read(lines)");
 read(fd, &columns, 1); ordie("read(columns)");
 char_row = (unsigned char *)malloc(columns*2);
 if (char_row == ((void *)0))
  die("malloc failed");
 lseek(fd, 4, 0);
 if (char_width == 8)
  save = save_8bit_width;
 else
  save = save_9bit_width;
 printf("P6\n%d %d\n255\n", columns*char_width, lines*16);
 for (line=0; line < lines; line++) {
  read(fd, char_row, columns*2);
  for (y=0; y < 16; y++) {
   for (col=0; col < columns*2; col+=2) {
    c = char_row[col];
    attr = swapbits(char_row[col+1]);
    fore = attr & 0x0f;
    back = attr >> 4;
    save(c, font[c][y], fore, back);
   }
  }
 }
 free(char_row);
 close(fd);
 return 0;
}

variable_types: argv int

variable_types:
argc
char *[]

variable_types: fd int

variable_types: struct consolefontdesc { unsigned short charcount; unsigned short charheight; char *chardata; }

variable_types:
path
char [4096]

variable_types: void (*)(unsigned char char_code, unsigned char font_row, unsigned char fore, unsigned char back)

variable_types:
lines
unsigned char

variable_types:
columns
unsigned char

variable_types:
c
unsigned char

variable_types:
attr
unsigned char

variable_types: col int

variable_types: line int

variable_types: y int


usage

decompiled_code:
void __noreturn usage()
{
fwrite(
"Program save contents of console in PNM file\n"
"(PNM is printed on standard output\n"
"\n"
"usage:\n"
"\tttyscreenshot [8|9] console-num\n"
"\n"
"First argument is a character width in pixels;\n"
"9 pixels is a default width use by EGA/VGA cards,\n"
"8 pixels isn't.\n"
"\n"
"You need permissions to read /dev/vcsa[console-num] device.\n",
1uLL,
0x128uLL,
stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
fwrite(
"Program save contents of console in PNM file\n"
"(PNM is printed on standard output\n"
"\n"
"usage:\n"
"\tttyscreenshot [8|9] console-num\n"
"\n"
"First argument is a character width in pixels;\n"
"9 pixels is a default width use by EGA/VGA cards,\n"
"8 pixels isn't.\n"
"\n"
"You need permissions to read /dev/vcsa[console-num] device.\n",
1uLL,
0x128uLL,
stderr);
exit(1);
}

original_code:
void usage() {
 fputs(
  "Program save contents of console in PNM file\n"
  "(PNM is printed on standard output\n"
  "\n"
  "usage:\n"
  "\tttyscreenshot [8|9] console-num\n"
  "\n"
  "First argument is a character width in pixels;\n"
  "9 pixels is a default width use by EGA/VGA cards,\n"
  "8 pixels isn't.\n"
  "\n"
  "You need permissions to read /dev/vcsa[console-num] device.\n",
  stderr
 );
 exit(1);
}


die

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

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

original_code:
void die(char* info) {
 fprintf(stderr, "%s\n", info);
 exit(1);
}

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


decompiled_code:
_BOOL8 __fastcall is_pattern_old(unsigned __int16 a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = ((unsigned __int16)-a1 & a1) == -(__int16)a1;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(unsigned __int16 a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = ((unsigned __int16)-a1 & a1) == -(__int16)a1;
return result;
}

original_code:
int is_pattern_old(uint16_t x) {
    const uint16_t n = -x;
    return (x > 0) && n == (n & x);
}

canonical_original_code:
int is_pattern_old(unsigned short x) {
    unsigned short n = -x;
    return (x > 0) && n == (n & x);
}

variable_types:
x
unsigned short

variable_types:
n
unsigned short


decompiled_code:
_BOOL8 __fastcall is_pattern(unsigned __int16 a1)
{
return (unsigned __int16)(a1 ^ -a1) < a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(unsigned __int16 a1)
{
return (unsigned __int16)(a1 ^ -a1) < a1;
}

original_code:
int is_pattern(uint16_t x) {
    const uint16_t n = -x;
    return (n ^ x) < x;
}

canonical_original_code:
int is_pattern(unsigned short x) {
    unsigned short n = -x;
    return (n ^ x) < x;
}


decompiled_code:
__int64 __fastcall is_pattern_naive(__int16 a1)
{
__int16 v1; // ax
unsigned int v2; // er8

v1 = a1;
v2 = 0;
if ( !a1 )
return v2;
while ( v1 < 0 )
{
v1 *= 2;
if ( !v1 )
return 1;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1)
{
__int16 v1; // ax
unsigned int v2; // er8

v1 = a1;
v2 = 0;
if ( !a1 )
return v2;
while ( v1 < 0 )
{
v1 *= 2;
if ( !v1 )
return 1;
}
return 0LL;
}

original_code:
int is_pattern_naive(uint16_t x) {
    if (x == 0) {
        return 0;
    }
    while (x > 0) {
        if ((x & 0x8000) == 0) {
            return 0;
        }
        x <<= 1;
    }
    return 1;
}

canonical_original_code:
int is_pattern_naive(unsigned short x) {
    if (x == 0) {
        return 0;
    }
    while (x > 0) {
        if ((x & 0x8000) == 0) {
            return 0;
        }
        x <<= 1;
    }
    return 1;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
verify(argc, argv, envp);
puts("All OK");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func10(argc, argv, envp);
puts("All OK");
return 0;
}

original_code:
int main() {
    verify();
    puts("All OK");
}


verify

decompiled_code:
unsigned int verify()
{
unsigned int i; // ebp
unsigned int result; // eax
__int64 v2; // rcx
__int64 v3; // rsi

for ( i = 0; i != 0x10000; ++i )
{
while ( 1 )
{
is_pattern_naive(i);
result = is_pattern(i);
if ( (_DWORD)v2 != result )
{
printf("failed for %04x: result %d, expected %d\n", i, result, v2);
exit(1);
}
if ( (_DWORD)v2 )
break;
if ( ++i == 0x10000 )
return result;
}
v3 = i;
result = printf("pattern: %04x\n", v3);
}
return result;
}

canonical_decompiled_code:
unsigned int func10()
{
unsigned int i; // ebp
unsigned int result; // eax
__int64 v2; // rcx
__int64 v3; // rsi

for ( i = 0; i != 0x10000; ++i )
{
while ( 1 )
{
func9(i);
result = func8(i);
if ( (_DWORD)v2 != result )
{
printf("failed for %04x: result %d, expected %d\n", i, result, v2);
exit(1);
}
if ( (_DWORD)v2 )
break;
if ( ++i == 0x10000 )
return result;
}
v3 = i;
result = printf("pattern: %04x\n", v3);
}
return result;
}

original_code:
void verify() {
    for (int i=0; i < 65536; i++) {
        int expected = is_pattern_naive(i);
        int result = is_pattern(i);
        if (expected != result) {
            printf("failed for %04x: result %d, expected %d\n", i, result, expected);
            exit(1);
        }
        if (result) {
            printf("pattern: %04x\n", i);
        }
    }
}

canonical_original_code:
void verify() {
    for (int i = 0; i < 65536; i++) {
        int expected = is_pattern_naive(i);
        int result = is_pattern(i);
        if (expected != result) {
            printf("failed for %04x: result %d, expected %d\n", i, result, expected);
            exit(1);
        }
        if (result) {
            printf("pattern: %04x\n", i);
        }
    }
}

variable_types: i int

variable_types: expected int

variable_types: result int


decompiled_code:
__int64 __fastcall isolate_lowest_set(int a1)
{
return a1 & (unsigned int)-a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
return a1 & (unsigned int)-a1;
}

original_code:
uint32_t isolate_lowest_set(uint32_t x) {
    return x & -x;
}

canonical_original_code:
unsigned int isolate_lowest_set(unsigned int x) {
    return x & -x;
}

variable_types:
x
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
verify(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func11(argc, argv, envp);
return 0;
}

original_code:
int main() {
    verify();
}

canonical_original_code:
int main() {
    verify();
}


decompiled_code:
_BOOL8 __fastcall is_power_of_two_non_zero(int a1)
{
return (unsigned int)isolate_lowest_set(a1) == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(int a1)
{
return (unsigned int)func7(a1) == a1;
}

original_code:
int is_power_of_two_non_zero(uint32_t x) {
    return isolate_lowest_set(x) == x;
}

canonical_original_code:
int is_power_of_two_non_zero(unsigned int x) {
    return isolate_lowest_set(x) == x;
}


decompiled_code:
_BOOL8 __fastcall is_power_of_two(int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = is_power_of_two_non_zero(a1);
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(int a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 )
result = func8(a1);
return result;
}

original_code:
int is_power_of_two(uint32_t x) {
    return x != 0 && is_power_of_two_non_zero(x);
}

canonical_original_code:
int is_power_of_two(unsigned int x) {
    return x != 0 && is_power_of_two_non_zero(x);
}


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

v1 = 32;
v2 = 1;
do
{
if ( a1 == v2 )
return 1LL;
v2 *= 2;
--v1;
}
while ( v1 );
return 0LL;
}

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

v1 = 32;
v2 = 1;
do
{
if ( a1 == v2 )
return 1LL;
v2 *= 2;
--v1;
}
while ( v1 );
return 0LL;
}

original_code:
int is_power_of_two_naive(uint32_t x) {
    uint32_t bit = 1;
    for (int i=0; i < 32; i++, bit <<= 1) {
        if (x == bit) {
            return 1;
        }
    }
    return 0;
}

canonical_original_code:
int is_power_of_two_naive(unsigned int x) {
    unsigned int bit = 1;
    for (int i = 0; i < 32; i++, bit <<= 1) {
        if (x == bit) {
            return 1;
        }
    }
    return 0;
}

variable_types:
bit
unsigned int


verify

decompiled_code:
_BOOL8 verify()
{
int i; // ebx
_BOOL8 result; // rax
int v2; // edx
unsigned int v3; // ebp
int v4; // eax
int v5; // ecx

for ( i = 1; ; ++i )
{
v3 = i - 1;
if ( ((i - 1) & 0xFFFFF) == 0 )
{
printf("%x\n", v3);
is_power_of_two(v3);
v4 = is_power_of_two_naive(v3);
if ( v4 != v5 )
LABEL_7:
__assert_fail("is_power_of_two(x) == is_power_of_two_naive(x)", "is_power_of_two.c", 0x32u, "verify");
continue;
}
is_power_of_two_naive(v3);
result = is_power_of_two(v3);
if ( result != v2 )
goto LABEL_7;
if ( !i )
break;
}
return result;
}

canonical_decompiled_code:
_BOOL8 func11()
{
int i; // ebx
_BOOL8 result; // rax
int v2; // edx
unsigned int v3; // ebp
int v4; // eax
int v5; // ecx

for ( i = 1; ; ++i )
{
v3 = i - 1;
if ( ((i - 1) & 0xFFFFF) == 0 )
{
printf("%x\n", v3);
func9(v3);
v4 = func10(v3);
if ( v4 != v5 )
LABEL_7:
__assert_fail("is_power_of_two(x) == is_power_of_two_naive(x)", "is_power_of_two.c", 0x32u, "verify");
continue;
}
func10(v3);
result = func9(v3);
if ( result != v2 )
goto LABEL_7;
if ( !i )
break;
}
return result;
}

original_code:
void verify() {
    uint32_t x = 0;
    while (1) {
        if ((x & 0xfffff) == 0) {
            printf("%x\n", x);
        }
        ((is_power_of_two(x) == is_power_of_two_naive(x)) ? (void) (0) : __assert_fail ("is_power_of_two(x) == is_power_of_two_naive(x)", "is_power_of_two.c", 50, __extension__ __PRETTY_FUNCTION__));
        x += 1;
        if (x == 0) break;
    }
}

canonical_original_code:
void verify() {
    unsigned int x = 0;
    while (1) {
        if ((x & 0xfffff) == 0) {
            printf("%x\n", x);
        }
        ((is_power_of_two(x) == is_power_of_two_naive(x)) ? (void) (0) : __assert_fail ("is_power_of_two(x) == is_power_of_two_naive(x)", "is_power_of_two.c", 50, __extension__ __PRETTY_FUNCTION__));
        x += 1;
        if (x == 0) break;
    }
}


naive

original_code:
uint32_t naive(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    uint32_t mask_bit = 1;
    uint32_t src_bit = 1;
    int i;
    for (i=0; i < 32; i++, mask_bit <<= 1) {
        if (mask & mask_bit) {
            if (src & src_bit) {
                result |= mask_bit;
            }
            src_bit <<= 1;
        }
    }
    return result;
}

canonical_original_code:
unsigned int naive(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    unsigned int mask_bit = 1;
    unsigned int src_bit = 1;
    int i;
    for (i=0; i < 32; i++, mask_bit <<= 1) {
        if (mask & mask_bit) {
            if (src & src_bit) {
                result |= mask_bit;
            }
            src_bit <<= 1;
        }
    }
    return result;
}

variable_types:
src
unsigned int

variable_types:
mask
unsigned int

variable_types:
result
unsigned int


branchless

decompiled_code:
__int64 __fastcall branchless(unsigned int a1, int a2)
{
unsigned int v2; // er8
int i; // eax
unsigned int v4; // edx

v2 = 0;
for ( i = a2 & -a2; i; i = a2 & -a2 )
{
v4 = a1;
a1 >>= 1;
a2 &= ~i;
v2 |= i & ((int)(v4 << 31) >> 31);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, int a2)
{
unsigned int v2; // er8
int i; // eax
unsigned int v4; // edx

v2 = 0;
for ( i = a2 & -a2; i; i = a2 & -a2 )
{
v4 = a1;
a1 >>= 1;
a2 &= ~i;
v2 |= i & ((int)(v4 << 31) >> 31);
}
return v2;
}

original_code:
uint32_t branchless(uint32_t src, uint32_t mask) {
    uint32_t result = 0;
    while (1) {
        const uint32_t lowest = -mask & mask;
        if (lowest == 0) {
            break;
        }
        const uint32_t LSB = (uint32_t)((int32_t)(src << 31) >> 31);
        result |= LSB & lowest;
        mask &= ~lowest;
        src >>= 1;
    }
    return result;
}

canonical_original_code:
unsigned int branchless(unsigned int src, unsigned int mask) {
    unsigned int result = 0;
    while (1) {
        unsigned int lowest = -mask & mask;
        if (lowest == 0) {
            break;
        }
        unsigned int LSB = (uint32_t)((int32_t)(src << 31) >> 31);
        result |= LSB & lowest;
        mask &= ~lowest;
        src >>= 1;
    }
    return result;
}

variable_types:
lowest
unsigned int

variable_types:
LSB
unsigned int