decompiled_code:
__int64 __fastcall http_find_headerlength(const void *a1, int a2)
{
int v2; // ebp
int v3; // edx
char *v4; // rax
int v5; // edx

v2 = (int)a1;
v3 = 0;
while ( 1 )
{
v4 = (char *)memchr(a1, 10, a2 - v3);
if ( !v4 )
break;
v5 = (_DWORD)v4 - v2;
if ( (int)v4 - v2 > 0 && a2 - 2 > v5 && *(v4 - 1) == 13 && v4[1] == 13 )
{
if ( v4[2] == 10 )
return (unsigned int)(v5 + 3);
}
else if ( a2 > v5 && v4[1] == 10 )
{
return (unsigned int)((_DWORD)v4 - v2 + 2);
}
a1 = v4 + 1;
v3 = v5 + 1;
if ( a2 <= v3 )
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(const void *a1, int a2)
{
int v2; // ebp
int v3; // edx
char *v4; // rax
int v5; // edx

v2 = (int)a1;
v3 = 0;
while ( 1 )
{
v4 = (char *)memchr(a1, 10, a2 - v3);
if ( !v4 )
break;
v5 = (_DWORD)v4 - v2;
if ( (int)v4 - v2 > 0 && a2 - 2 > v5 && *(v4 - 1) == 13 && v4[1] == 13 )
{
if ( v4[2] == 10 )
return (unsigned int)(v5 + 3);
}
else if ( a2 > v5 && v4[1] == 10 )
{
return (unsigned int)((_DWORD)v4 - v2 + 2);
}
a1 = v4 + 1;
v3 = v5 + 1;
if ( a2 <= v3 )
return 0xFFFFFFFFLL;
}
return 0xFFFFFFFFLL;
}

original_code:
int http_find_headerlength(const char *data, int len) {
    int sz = 0;
    const char *crlf = data;
    for (;;) {
        crlf = memchr(crlf, '\n', len - sz);
        if (crlf == ((void *)0)) {
            break;
        }
        sz = crlf - data;
        if (sz > 0 && sz <= (len - 3) && *(crlf - 1) == '\r'
                && *(crlf + 1) == '\r' && *(crlf + 2) == '\n') {
            return sz + 3;
        }
        if (sz <= (len - 1) && *(crlf + 1) == '\n') {
            return sz + 2;
        }
        ++crlf;
        ++sz;
        if (sz >= len) {
            break;
        }
    }
    return -1;
}

canonical_original_code:
int http_find_headerlength(char *data, int len) {
    int sz = 0;
    char *crlf = data;
    for (;;) {
        crlf = memchr(crlf, '\n', len - sz);
        if (crlf == ((void *)0)) {
            break;
        }
        sz = crlf - data;
        if (sz > 0 && sz <= (len - 3) && *(crlf - 1) == '\r'
                && *(crlf + 1) == '\r' && *(crlf + 2) == '\n') {
            return sz + 3;
        }
        if (sz <= (len - 1) && *(crlf + 1) == '\n') {
            return sz + 2;
        }
        ++crlf;
        ++sz;
        if (sz >= len) {
            break;
        }
    }
    return -1;
}

variable_types:
crlf
char *


repo: nqv/aranea

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

if ( a1 > 299 )
{
if ( a1 <= 399 )
{
if ( a1 == 303 )
return "See Other";
if ( a1 > 303 )
{
result = "Not Modified";
if ( a1 == 304 )
return result;
}
else
{
result = "Moved Permanently";
if ( a1 == 301 )
return result;
if ( a1 == 302 )
return "Moved Temporarily ";
}
LABEL_4:
fprintf(stderr, "unknown code %d\n", (unsigned int)a1);
return "Unknown";
}
if ( a1 > 499 )
{
result = "Server Error";
if ( a1 != 500 )
{
result = "Not Implemented";
if ( a1 != 501 )
goto LABEL_4;
}
}
else
{
switch ( a1 )
{
case 400:
result = "Bad Request";
break;
case 401:
result = "Authorization Required";
break;
case 403:
result = "Forbidden";
break;
case 404:
result = "Not Found";
break;
case 413:
result = "Request Entity Too Large";
break;
case 416:
result = "Requested Range Not Satisfiable";
break;
default:
goto LABEL_4;
}
}
}
else
{
result = "OK";
if ( a1 != 200 )
{
result = "Partial Content";
if ( a1 != 206 )
goto LABEL_4;
}
}
return result;
}

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

if ( a1 > 299 )
{
if ( a1 <= 399 )
{
if ( a1 == 303 )
return "See Other";
if ( a1 > 303 )
{
result = "Not Modified";
if ( a1 == 304 )
return result;
}
else
{
result = "Moved Permanently";
if ( a1 == 301 )
return result;
if ( a1 == 302 )
return "Moved Temporarily ";
}
LABEL_4:
fprintf(stderr, "unknown code %d\n", (unsigned int)a1);
return "Unknown";
}
if ( a1 > 499 )
{
result = "Server Error";
if ( a1 != 500 )
{
result = "Not Implemented";
if ( a1 != 501 )
goto LABEL_4;
}
}
else
{
switch ( a1 )
{
case 400:
result = "Bad Request";
break;
case 401:
result = "Authorization Required";
break;
case 403:
result = "Forbidden";
break;
case 404:
result = "Not Found";
break;
case 413:
result = "Request Entity Too Large";
break;
case 416:
result = "Requested Range Not Satisfiable";
break;
default:
goto LABEL_4;
}
}
}
else
{
result = "OK";
if ( a1 != 200 )
{
result = "Partial Content";
if ( a1 != 206 )
goto LABEL_4;
}
}
return result;
}

original_code:
const char *http_string_status(int code) {
    if (code < 300) {
        switch (code) {
        case HTTP_STATUS_OK:
            return "OK";
        case HTTP_STATUS_PARTIALCONTENT:
            return "Partial Content";
        }
    } else if (code < 400) {
        switch (code) {
        case HTTP_STATUS_MOVEDPERMANENTLY:
            return "Moved Permanently";
        case 302:
            return "Moved Temporarily ";
        case 303:
            return "See Other";
        case HTTP_STATUS_NOTMODIFIED:
            return "Not Modified";
        }
    } else if (code < 500) {
        switch (code) {
        case HTTP_STATUS_BADREQUEST:
            return "Bad Request";
        case HTTP_STATUS_AUTHORIZATIONREQUIRED:
            return "Authorization Required";
        case HTTP_STATUS_FORBIDDEN:
            return "Forbidden";
        case HTTP_STATUS_NOTFOUND:
            return "Not Found";
        case HTTP_STATUS_ENTITYTOOLARGE:
            return "Request Entity Too Large";
        case HTTP_STATUS_RANGENOTSATISFIABLE:
            return "Requested Range Not Satisfiable";
        }
    } else {
        switch (code) {
        case HTTP_STATUS_SERVERERROR:
            return "Server Error";
        case HTTP_STATUS_NOTIMPLEMENTED:
            return "Not Implemented";
        }
    }
    fprintf(stderr, "unknown code %d" "\n", code);
    return "Unknown";
}

variable_types: code int

return_type: char *


decompiled_code:
int __fastcall http_put_headerstatus(int *a1, char *a2, int a3)
{
const char *v4; // rax

v4 = http_string_status(*a1);
return snprintf(a2, a3, "HTTP/1.1 %d %s\r\nServer: Aranea/0.1\r\n", (unsigned int)*a1, v4);
}

canonical_decompiled_code:
int __fastcall func14(int *a1, char *a2, int a3)
{
const char *v4; // rax

v4 = func13(*a1);
return snprintf(a2, a3, "HTTP/1.1 %d %s\r\nServer: Aranea/0.1\r\n", (unsigned int)*a1, v4);
}

original_code:
static
int http_put_headerstatus(struct response_t *self, char *data, int sz) {
    return snprintf(data, sz,
            "HTTP/1.1" " %d %s\r\n"
            "Server: " "Aranea" "/" "0.1" "\r\n",
            self->status_code, http_string_status(self->status_code));
}

variable_types: struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; } *

variable_types:
data
char *

variable_types: sz int

return_type: int


decompiled_code:
__int64 __fastcall http_gen_header(int *a1, char *a2, int a3, char a4)
{
int v6; // eax
int v7; // ebp
int v8; // ebx
unsigned int v10; // eax
int v11; // eax
int v12; // eax
int v13; // eax

v6 = http_put_headerstatus(a1, a2, a3);
if ( v6 < 0 || (v7 = v6, a3 <= v6) )
{
v7 = -1;
}
else
{
v8 = a3 - v6;
if ( (a4 & 0x10) != 0 )
{
v10 = http_put_headerdate((__int64)a1, &a2[v6], v8);
if ( v10 >= v8 )
return (unsigned int)-1;
v8 -= v10;
v7 += v10;
}
if ( (a4 & 2) != 0 )
{
v11 = http_put_headeraccept((__int64)a1, &a2[v7], v8);
if ( v11 < 0 || v8 <= v11 )
return (unsigned int)-1;
v8 -= v11;
v7 += v11;
}
if ( (a4 & 4) != 0 )
{
v12 = http_put_headercontent((__int64)a1, &a2[v7], v8);
if ( v12 < 0 || v8 <= v12 )
return (unsigned int)-1;
v8 -= v12;
v7 += v12;
}
if ( (a4 & 8) != 0 )
{
v13 = http_put_headerrange(a1, &a2[v7], v8);
if ( v13 < 0 || v8 <= v13 )
return (unsigned int)-1;
v8 -= v13;
v7 += v13;
}
if ( (a4 & 1) != 0 )
{
snprintf(&a2[v7], v8, "\r\n");
if ( v8 <= 2 )
v7 = -1;
else
v7 += 2;
}
}
return (unsigned int)v7;
}

original_code:
int http_gen_header(struct response_t *self, char *data, int sz,
        const unsigned int flags) {
    int len, i;
    len = http_put_headerstatus(self, data, sz);
    if (len < 0 || len >= sz) {
        return -1;
    }
    sz -= len;
    do { if (flags & HTTP_FLAG_DATE) { i = http_put_headerdate(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_ACCEPT) { i = http_put_headeraccept(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_CONTENT) { i = http_put_headercontent(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_RANGE) { i = http_put_headerrange(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    if (flags & HTTP_FLAG_END) {
        i = snprintf(data + len, sz, "\r\n");
        if (i < 0 || i >= sz) {
            return -1;
        }
        len += i;
    }
    return len;
}

canonical_original_code:
int http_gen_header(struct response_t *self, char *data, int sz, unsigned int flags) {
    int len; int i;
    len = http_put_headerstatus(self, data, sz);
    if (len < 0 || len >= sz) {
        return -1;
    }
    sz -= len;
    do { if (flags & HTTP_FLAG_DATE) { i = http_put_headerdate(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_ACCEPT) { i = http_put_headeraccept(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_CONTENT) { i = http_put_headercontent(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    do { if (flags & HTTP_FLAG_RANGE) { i = http_put_headerrange(self, data + len, sz); if (i < 0 || i >= sz) { return -1; } sz -= i; len += i; } } while (0);
    if (flags & HTTP_FLAG_END) {
        i = snprintf(data + len, sz, "\r\n");
        if (i < 0 || i >= sz) {
            return -1;
        }
        len += i;
    }
    return len;
}

variable_types:
flags
unsigned int

variable_types: len int

variable_types: i int

user_defined_types: struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; }


decompiled_code:
__int64 __fastcall http_gen_errorpage(unsigned int *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
unsigned int v6; // ebx
int v7; // ebx
int v8; // ebp
const char *v9; // rax
int v10; // eax
__int64 result; // rax

v4 = http_put_headerstatus((int *)a1, a2, a3);
if ( v4 < 0 )
return 0xFFFFFFFFLL;
v5 = v4;
if ( a3 <= v4 )
return 0xFFFFFFFFLL;
v6 = a3 - v4;
snprintf(&a2[v4], (int)v6, "Content-Type: text/html\r\n\r\n");
if ( v6 <= 0x1B )
return 0xFFFFFFFFLL;
v7 = v6 - 27;
v8 = v5 + 27;
v9 = http_string_status(*a1);
v10 = snprintf(
&a2[v8],
v7,
"<title>%d</title>
%d %s

Aranea/0.1",
*a1,
*a1,
v9);
if ( v10 < 0 || v7 <= v10 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v8 + v10);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int *a1, char *a2, int a3)
{
int v4; // eax
int v5; // ebp
unsigned int v6; // ebx
int v7; // ebx
int v8; // ebp
const char *v9; // rax
int v10; // eax
__int64 result; // rax

v4 = func14((int *)a1, a2, a3);
if ( v4 < 0 )
return 0xFFFFFFFFLL;
v5 = v4;
if ( a3 <= v4 )
return 0xFFFFFFFFLL;
v6 = a3 - v4;
snprintf(&a2[v4], (int)v6, "Content-Type: text/html\r\n\r\n");
if ( v6 <= 0x1B )
return 0xFFFFFFFFLL;
v7 = v6 - 27;
v8 = v5 + 27;
v9 = func13(*a1);
v10 = snprintf(
&a2[v8],
v7,
"<title>%d</title>
%d %s

Aranea/0.1",
*a1,
*a1,
v9);
if ( v10 < 0 || v7 <= v10 )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)(v8 + v10);
return result;
}

original_code:
int http_gen_errorpage(struct response_t *self, char *data, int sz) {
    int len, i;
    len = http_put_headerstatus(self, data, sz);
    if (len < 0 || len >= sz) {
        return -1;
    }
    sz -= len;
    i = snprintf(data + len, sz,
            "Content-Type: text/html\r\n\r\n");
    if (i < 0 || i >= sz) {
        return -1;
    }
    sz -= i;
    len += i;
    i = snprintf(data + len, sz,
            "<title>%d</title>"
            "
%d %s

" "Aranea" "/" "0.1"
            "",
            self->status_code, self->status_code,
            http_string_status(self->status_code));
    if (i < 0 || i >= sz) {
        return -1;
    }
    return len + i;
}

canonical_original_code:
int http_gen_errorpage(struct response_t *self, char *data, int sz) {
    int len; int i;
    len = http_put_headerstatus(self, data, sz);
    if (len < 0 || len >= sz) {
        return -1;
    }
    sz -= len;
    i = snprintf(data + len, sz,
            "Content-Type: text/html\r\n\r\n");
    if (i < 0 || i >= sz) {
        return -1;
    }
    sz -= i;
    len += i;
    i = snprintf(data + len, sz,
            "<title>%d</title>"
            "
%d %s

" "Aranea" "/" "0.1"
            "",
            self->status_code, self->status_code,
            http_string_status(self->status_code));
    if (i < 0 || i >= sz) {
        return -1;
    }
    return len + i;
}


decompiled_code:
__int64 __fastcall forget_client(__int64 a1)
{
client_close();
client_detach(a1);
return clientpool_free(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
client_close();
client_detach(a1);
return clientpool_free(a1);
}

original_code:
static
void forget_client(struct client_t *c) {
    client_close(c);
    client_detach(c);
    clientpool_free(c);
}

variable_types: struct client_t { int remote_fd; int local_rfd; long timeout; int state; char ip[40]; struct request_t { char *header[-1]; char *method; char *url; char *query_string; char *version; char *range_from; char *range_to; long header_length; } request; char data[1024]; long data_length; long data_sent; struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; } response; long file_sent; unsigned int flags; struct client_t *next; struct client_t **prev; } *

return_type: void

user_defined_types: struct client_t { int remote_fd; int local_rfd; long timeout; int state; char ip[40]; struct request_t request; char data[1024]; long data_length; long data_sent; struct response_t response; long file_sent; unsigned int flags; struct client_t *next; struct client_t **prev; }


decompiled_code:
__int64 __fastcall server_init(__int64 a1)
{
int v1; // eax
unsigned int v2; // er12
struct addrinfo *v3; // r14
int v4; // eax
int v5; // ebx
const char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
int optval; // [rsp+4h] [rbp-64h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(0LL, *(const char **)(a1 + 8), &req, &pai);
v2 = v1;
if ( v1 )
{
v7 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n\n", v7);
return (unsigned int)-1;
}
v3 = pai;
if ( !pai )
{
LABEL_12:
v10 = _errno_location();
v11 = strerror(*v10);
fprintf(stderr, "bind: %s\n", v11);
return (unsigned int)-1;
}
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v5 = v4;
if ( v4 != -1 )
break;
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "socket: %s\n", v9);
LABEL_11:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_12;
}
if ( setsockopt(v4, 1, 2, &optval, 4u) == -1 )
{
close(v5);
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "setsockopt: %s\n", v13);
return (unsigned int)-1;
}
if ( bind(v5, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v5);
v14 = _errno_location();
v15 = strerror(*v14);
fprintf(stderr, "bind: %s\n", v15);
goto LABEL_11;
}
freeaddrinfo(pai);
if ( listen(v5, 10) == -1 )
{
close(v5);
v16 = _errno_location();
v17 = strerror(*v16);
fprintf(stderr, "listen: %s\n", v17);
v2 = -1;
}
else
{
*(_DWORD *)a1 = v5;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v1; // eax
unsigned int v2; // er12
struct addrinfo *v3; // r14
int v4; // eax
int v5; // ebx
const char *v7; // rax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int *v14; // rax
char *v15; // rax
int *v16; // rax
char *v17; // rax
int optval; // [rsp+4h] [rbp-64h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v1 = getaddrinfo(0LL, *(const char **)(a1 + 8), &req, &pai);
v2 = v1;
if ( v1 )
{
v7 = gai_strerror(v1);
fprintf(stderr, "getaddrinfo: %s\n\n", v7);
return (unsigned int)-1;
}
v3 = pai;
if ( !pai )
{
LABEL_12:
v10 = _errno_location();
v11 = strerror(*v10);
fprintf(stderr, "bind: %s\n", v11);
return (unsigned int)-1;
}
while ( 1 )
{
v4 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v5 = v4;
if ( v4 != -1 )
break;
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "socket: %s\n", v9);
LABEL_11:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_12;
}
if ( setsockopt(v4, 1, 2, &optval, 4u) == -1 )
{
close(v5);
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "setsockopt: %s\n", v13);
return (unsigned int)-1;
}
if ( bind(v5, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v5);
v14 = _errno_location();
v15 = strerror(*v14);
fprintf(stderr, "bind: %s\n", v15);
goto LABEL_11;
}
freeaddrinfo(pai);
if ( listen(v5, 10) == -1 )
{
close(v5);
v16 = _errno_location();
v17 = strerror(*v16);
fprintf(stderr, "listen: %s\n", v17);
v2 = -1;
}
else
{
*(_DWORD *)a1 = v5;
}
return v2;
}

original_code:
int server_init(struct server_t *self) {
    struct addrinfo hints, *info, *p;
    int enable = 1;
    int fd;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    fd = getaddrinfo(((void *)0), self->port, &hints, &info);
    if (fd != 0) {
        fprintf(stderr, "getaddrinfo: %s\n" "\n", gai_strerror(fd));
        return -1;
    }
    for (p = info; p != ((void *)0); p = p->ai_next) {
        fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
        if (fd == -1) {
            fprintf(stderr, "socket: %s" "\n", strerror((*__errno_location ())));
            continue;
        }
        if (setsockopt(fd, 1, 2, &enable, sizeof(int)) == -1) {
            close(fd);
            fprintf(stderr, "setsockopt: %s" "\n", strerror((*__errno_location ())));
            return -1;
        }
        if (bind(fd, p->ai_addr, p->ai_addrlen) == -1) {
            close(fd);
            fprintf(stderr, "bind: %s" "\n", strerror((*__errno_location ())));
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "bind: %s" "\n", strerror((*__errno_location ())));
        return -1;
    }
    freeaddrinfo(info);
    if (listen(fd, 10) == -1) {
        close(fd);
        fprintf(stderr, "listen: %s" "\n", strerror((*__errno_location ())));
        return -1;
    }
    ;
    self->fd = fd;
    return 0;
}

canonical_original_code:
int server_init(struct server_t *self) {
    struct addrinfo hints; struct addrinfo *info; struct addrinfo *p;
    int enable = 1;
    int fd;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    fd = getaddrinfo(((void *)0), self->port, &hints, &info);
    if (fd != 0) {
        fprintf(stderr, "getaddrinfo: %s\n" "\n", gai_strerror(fd));
        return -1;
    }
    for (p = info; p != ((void *)0); p = p->ai_next) {
        fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
        if (fd == -1) {
            fprintf(stderr, "socket: %s" "\n", strerror((*__errno_location ())));
            continue;
        }
        if (setsockopt(fd, 1, 2, &enable, sizeof(int)) == -1) {
            close(fd);
            fprintf(stderr, "setsockopt: %s" "\n", strerror((*__errno_location ())));
            return -1;
        }
        if (bind(fd, p->ai_addr, p->ai_addrlen) == -1) {
            close(fd);
            fprintf(stderr, "bind: %s" "\n", strerror((*__errno_location ())));
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        fprintf(stderr, "bind: %s" "\n", strerror((*__errno_location ())));
        return -1;
    }
    freeaddrinfo(info);
    if (listen(fd, 10) == -1) {
        close(fd);
        fprintf(stderr, "listen: %s" "\n", strerror((*__errno_location ())));
        return -1;
    }
    ;
    self->fd = fd;
    return 0;
}

variable_types: struct server_t { int fd; char *port; struct client_t { int remote_fd; int local_rfd; long timeout; int state; char ip[40]; struct request_t { char *header[-1]; char *method; char *url; char *query_string; char *version; char *range_from; char *range_to; long header_length; } request; char data[1024]; long data_length; long data_sent; struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; } response; long file_sent; unsigned int flags; struct client_t *next; struct client_t **prev; } *clients; } *

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

variable_types: fd int

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

user_defined_types: struct server_t { int fd; char *port; struct client_t *clients; }


decompiled_code:
__int64 __fastcall server_accept(int *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // eax
__int64 v4; // rax
__int64 v5; // rbx
char *v6; // rsi
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
struct sockaddr v14[8]; // [rsp+0h] [rbp-A8h] BYREF
socklen_t addr_len[7]; // [rsp+8Ch] [rbp-1Ch] BYREF

addr_len[0] = 128;
v1 = accept(*a1, v14, addr_len);
if ( v1 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "accept: %s\n", v9);
return 0LL;
}
v2 = v1;
v3 = fcntl(v1, 3, 0LL);
if ( v3 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
fprintf(stderr, "fcntl: F_GETFL %s\n", v11);
goto LABEL_11;
}
BYTE1(v3) |= 8u;
if ( fcntl(v2, 4, v3) == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "fcntl: F_SETFL O_NONBLOCK %s\n", v13);
goto LABEL_11;
}
v4 = clientpool_alloc();
v5 = v4;
if ( !v4 )
{
LABEL_11:
close(v2);
return 0LL;
}
client_init(v4);
*(_DWORD *)v5 = v2;
*(_DWORD *)(v5 + 16) = 1;
v6 = &v14[0].sa_data[2];
if ( v14[0].sa_family != 2 )
v6 = &v14[0].sa_data[6];
inet_ntop(v14[0].sa_family, v6, (char *)(v5 + 20), 0x28u);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // eax
__int64 v4; // rax
__int64 v5; // rbx
char *v6; // rsi
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
struct sockaddr v14[8]; // [rsp+0h] [rbp-A8h] BYREF
socklen_t addr_len[7]; // [rsp+8Ch] [rbp-1Ch] BYREF

addr_len[0] = 128;
v1 = accept(*a1, v14, addr_len);
if ( v1 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "accept: %s\n", v9);
return 0LL;
}
v2 = v1;
v3 = fcntl(v1, 3, 0LL);
if ( v3 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
fprintf(stderr, "fcntl: F_GETFL %s\n", v11);
goto LABEL_11;
}
BYTE1(v3) |= 8u;
if ( fcntl(v2, 4, v3) == -1 )
{
v12 = _errno_location();
v13 = strerror(*v12);
fprintf(stderr, "fcntl: F_SETFL O_NONBLOCK %s\n", v13);
goto LABEL_11;
}
v4 = clientpool_alloc();
v5 = v4;
if ( !v4 )
{
LABEL_11:
close(v2);
return 0LL;
}
client_init(v4);
*(_DWORD *)v5 = v2;
*(_DWORD *)(v5 + 16) = 1;
v6 = &v14[0].sa_data[2];
if ( v14[0].sa_family != 2 )
v6 = &v14[0].sa_data[6];
inet_ntop(v14[0].sa_family, v6, (char *)(v5 + 20), 0x28u);
return v5;
}

original_code:
struct client_t *server_accept(struct server_t *self) {
    socklen_t len;
    struct sockaddr_storage addr;
    int fd;
    int flags;
    struct client_t *c;
    len = sizeof(addr);
    fd = accept(self->fd, (struct sockaddr *)&addr, &len);
    if (fd == -1) {
        fprintf(stderr, "accept: %s" "\n", strerror((*__errno_location ())));
        return ((void *)0);
    }
    flags = fcntl(fd, 3, ((void *)0));
    if (flags == -1) {
        fprintf(stderr, "fcntl: F_GETFL %s" "\n", strerror((*__errno_location ())));
        goto err;
    }
    if (fcntl(fd, 4, flags | 04000) == -1) {
        fprintf(stderr, "fcntl: F_SETFL O_NONBLOCK %s" "\n", strerror((*__errno_location ())));
        goto err;
    }
    c = clientpool_alloc();
    if (c == ((void *)0)) {
        goto err;
    }
    client_init(c);
    c->remote_fd = fd;
    c->state = STATE_RECV_HEADER;
    inet_ntop(addr.ss_family, (((struct sockaddr *)(&addr))->sa_family == 2) ? (void *)&(((struct sockaddr_in *)(&addr))->sin_addr) : (void *)&(((struct sockaddr_in6 *)(&addr))->sin6_addr), c->ip, sizeof(c->ip));
    ;
    return c;
err:
    close(fd);
    return ((void *)0);
}

canonical_original_code:
struct client_t *server_accept(struct server_t *self) {
    unsigned int len;
    struct sockaddr_storage addr;
    int fd;
    int flags;
    struct client_t *c;
    len = sizeof(addr);
    fd = accept(self->fd, (struct sockaddr *)&addr, &len);
    if (fd == -1) {
        fprintf(stderr, "accept: %s" "\n", strerror((*__errno_location ())));
        return ((void *)0);
    }
    flags = fcntl(fd, 3, ((void *)0));
    if (flags == -1) {
        fprintf(stderr, "fcntl: F_GETFL %s" "\n", strerror((*__errno_location ())));
        goto err;
    }
    if (fcntl(fd, 4, flags | 04000) == -1) {
        fprintf(stderr, "fcntl: F_SETFL O_NONBLOCK %s" "\n", strerror((*__errno_location ())));
        goto err;
    }
    c = clientpool_alloc();
    if (c == ((void *)0)) {
        goto err;
    }
    client_init(c);
    c->remote_fd = fd;
    c->state = STATE_RECV_HEADER;
    inet_ntop(addr.ss_family, (((struct sockaddr *)(&addr))->sa_family == 2) ? (void *)&(((struct sockaddr_in *)(&addr))->sin_addr) : (void *)&(((struct sockaddr_in6 *)(&addr))->sin6_addr), c->ip, sizeof(c->ip));
    ;
    return c;
err:
    close(fd);
    return ((void *)0);
}

variable_types: struct server_t { int fd; char *port; struct client_t *clients; } *

variable_types:
len
unsigned int

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

variable_types: flags int

return_type: struct client_t { int remote_fd; int local_rfd; long timeout; int state; char ip[40]; struct request_t { char *header[-1]; char *method; char *url; char *query_string; char *version; char *range_from; char *range_to; long header_length; } request; char data[1024]; long data_length; long data_sent; struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; } response; long file_sent; unsigned int flags; struct client_t *next; struct client_t **prev; } *

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

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

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }

user_defined_types: struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; }


original_code:
void server_poll(struct server_t *self) {
    fd_set rfds, wfds;
    int max_rfd, max_wfd;
    int num_fd;
    time_t chk_time;
    struct timeval timeout;
    struct client_t *c, *tc;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
    max_rfd = max_wfd = -1;
    do { ((void) (((&rfds)->__fds_bits)[((self->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((self->fd) % (8 * (int) sizeof (__fd_mask))))))); if (self->fd > max_rfd) { max_rfd = self->fd; } } while (0);
    g_curtime = time(((void *)0));
    for (c = self->clients; c != ((void *)0); ) {
        if (g_curtime > c->timeout) {
            ;
            tc = c;
            c = c->next;
            forget_client(tc);
            continue;
        }
        switch (c->state) {
        case STATE_RECV_HEADER:
            do { ((void) (((&rfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask))))))); if (c->remote_fd > max_rfd) { max_rfd = c->remote_fd; } } while (0);
            break;
        case STATE_SEND_HEADER:
        case STATE_SEND_FILE:
            do { ((void) (((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask))))))); if (c->remote_fd > max_wfd) { max_wfd = c->remote_fd; } } while (0);
            break;
        }
        c = c->next;
    }
    timeout.tv_sec = 60;
    timeout.tv_usec = 0;
    for (;;) {
        num_fd = select((max_rfd > max_wfd) ? (max_rfd + 1) : (max_wfd + 1),
                &rfds, (max_wfd != -1) ? &wfds : ((void *)0),
                ((void *)0), &timeout);
        if (num_fd > 0) {
            break;
        }
        if (num_fd < 0) {
            if ((*__errno_location ()) == 4) {
                continue;
            } else {
                fprintf(stderr, "select: %s" "\n", strerror((*__errno_location ())));
                sleep(1);
            }
        }
        return;
    }
    g_curtime = time(((void *)0));
    chk_time = g_curtime + 60;
    if (((((&rfds)->__fds_bits)[((self->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((self->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        c = server_accept(self);
        if (c != ((void *)0)) {
            client_add(c, &self->clients);
            c->timeout = chk_time;
            state_recv_header(c);
            if (c->state == STATE_NONE) {
                forget_client(c);
            }
        }
        --num_fd;
    }
    for (c = self->clients; num_fd > 0 && c != ((void *)0); ) {
        switch (c->state) {
        case STATE_NONE:
            break;
        case STATE_RECV_HEADER:
            if (((((&rfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_recv_header(c);
                --num_fd;
            }
            break;
        case STATE_SEND_HEADER:
            if (((((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_send_header(c);
                --num_fd;
            }
            break;
        case STATE_SEND_FILE:
            if (((((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_send_file(c);
                --num_fd;
            }
            break;
        default:
            ;
            c->state = STATE_NONE;
            break;
        }
        if (c->state == STATE_NONE) {
            ;
            tc = c;
            c = c->next;
            forget_client(tc);
        } else {
            c = c->next;
        }
    }
}

canonical_original_code:
void server_poll(struct server_t *self) {
    struct fd_set rfds; struct fd_set wfds;
    int max_rfd; int max_wfd;
    int num_fd;
    long chk_time;
    struct timeval timeout;
    struct client_t *c; struct client_t *tc;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (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" (&((&wfds)->__fds_bits)[0]) : "memory"); } while (0);
    max_rfd = max_wfd = -1;
    do { ((void) (((&rfds)->__fds_bits)[((self->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((self->fd) % (8 * (int) sizeof (__fd_mask))))))); if (self->fd > max_rfd) { max_rfd = self->fd; } } while (0);
    g_curtime = time(((void *)0));
    for (c = self->clients; c != ((void *)0); ) {
        if (g_curtime > c->timeout) {
            ;
            tc = c;
            c = c->next;
            forget_client(tc);
            continue;
        }
        switch (c->state) {
        case STATE_RECV_HEADER:
            do { ((void) (((&rfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask))))))); if (c->remote_fd > max_rfd) { max_rfd = c->remote_fd; } } while (0);
            break;
        case STATE_SEND_HEADER:
        case STATE_SEND_FILE:
            do { ((void) (((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask))))))); if (c->remote_fd > max_wfd) { max_wfd = c->remote_fd; } } while (0);
            break;
        }
        c = c->next;
    }
    timeout.tv_sec = 60;
    timeout.tv_usec = 0;
    for (;;) {
        num_fd = select((max_rfd > max_wfd) ? (max_rfd + 1) : (max_wfd + 1),
                &rfds, (max_wfd != -1) ? &wfds : ((void *)0),
                ((void *)0), &timeout);
        if (num_fd > 0) {
            break;
        }
        if (num_fd < 0) {
            if ((*__errno_location ()) == 4) {
                continue;
            } else {
                fprintf(stderr, "select: %s" "\n", strerror((*__errno_location ())));
                sleep(1);
            }
        }
        return;
    }
    g_curtime = time(((void *)0));
    chk_time = g_curtime + 60;
    if (((((&rfds)->__fds_bits)[((self->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((self->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
        c = server_accept(self);
        if (c != ((void *)0)) {
            client_add(c, &self->clients);
            c->timeout = chk_time;
            state_recv_header(c);
            if (c->state == STATE_NONE) {
                forget_client(c);
            }
        }
        --num_fd;
    }
    for (c = self->clients; num_fd > 0 && c != ((void *)0); ) {
        switch (c->state) {
        case STATE_NONE:
            break;
        case STATE_RECV_HEADER:
            if (((((&rfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_recv_header(c);
                --num_fd;
            }
            break;
        case STATE_SEND_HEADER:
            if (((((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_send_header(c);
                --num_fd;
            }
            break;
        case STATE_SEND_FILE:
            if (((((&wfds)->__fds_bits)[((c->remote_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->remote_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                c->timeout = chk_time;
                state_send_file(c);
                --num_fd;
            }
            break;
        default:
            ;
            c->state = STATE_NONE;
            break;
        }
        if (c->state == STATE_NONE) {
            ;
            tc = c;
            c = c->next;
            forget_client(tc);
        } else {
            c = c->next;
        }
    }
}

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

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

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

user_defined_types: struct request_t { char *header[-1]; char *method; char *url; char *query_string; char *version; char *range_from; char *range_to; long header_length; }


decompiled_code:
int server_close_fds()
{
int result; // eax
__int64 i; // rbx
int v2; // edi

result = close(g_server);
for ( i = *((_QWORD *)&g_server + 2); i; i = *(_QWORD *)(i + 1272) )
{
result = close(*(_DWORD *)i);
v2 = *(_DWORD *)(i + 4);
if ( v2 != -1 )
result = close(v2);
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
__int64 i; // rbx
int v2; // edi

result = close(g_server);
for ( i = *((_QWORD *)&g_server + 2); i; i = *(_QWORD *)(i + 1272) )
{
result = close(*(_DWORD *)i);
v2 = *(_DWORD *)(i + 4);
if ( v2 != -1 )
result = close(v2);
}
return result;
}

original_code:
void server_close_fds() {
    struct client_t *c;
    close(g_server.fd);
    for (c = g_server.clients; c != ((void *)0); c = c->next) {
        close(c->remote_fd);
        if (c->local_rfd != -1) {
            close(c->local_rfd);
        }
    }
}


decompiled_code:
__int64 __fastcall mimetype_find_def(char *s2)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
unsigned int v4; // er14
int v5; // eax

v1 = 41;
v2 = 0;
do
{
v3 = (v2 + v1) / 2;
v4 = v3;
v5 = strcasecmp(DEFAULT_MIME_TYPES[2 * v3], s2);
if ( v5 < 0 )
{
v2 = v3 + 1;
}
else
{
if ( v5 <= 0 )
return v4;
v1 = v3 - 1;
}
}
while ( v1 >= v2 );
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
unsigned int v4; // er14
int v5; // eax

v1 = 41;
v2 = 0;
do
{
v3 = (v2 + v1) / 2;
v4 = v3;
v5 = strcasecmp(DEFAULT_MIME_TYPES[2 * v3], s2);
if ( v5 < 0 )
{
v2 = v3 + 1;
}
else
{
if ( v5 <= 0 )
return v4;
v1 = v3 - 1;
}
}
while ( v1 >= v2 );
return (unsigned int)-1;
}

original_code:
static
int mimetype_find_def(const char *ext) {
    int l, m, h, r;
    h = (sizeof(DEFAULT_MIME_TYPES) / sizeof((DEFAULT_MIME_TYPES)[0])) - 1;
    l = 0;
    while (h >= l) {
        m = (h + l) / 2;
        r = strcasecmp(DEFAULT_MIME_TYPES[m].ext, ext);
        if (r < 0) {
            l = m + 1;
        } else if (r > 0) {
            h = m - 1;
        } else {
            return m;
        }
    }
    return -1;
}

canonical_original_code:
static int mimetype_find_def(char *ext) {
    int l; int m; int h; int r;
    h = (sizeof(DEFAULT_MIME_TYPES) / sizeof((DEFAULT_MIME_TYPES)[0])) - 1;
    l = 0;
    while (h >= l) {
        m = (h + l) / 2;
        r = strcasecmp(DEFAULT_MIME_TYPES[m].ext, ext);
        if (r < 0) {
            l = m + 1;
        } else if (r > 0) {
            h = m - 1;
        } else {
            return m;
        }
    }
    return -1;
}

variable_types:
ext
char *

variable_types: l int

variable_types: m int

variable_types: h int

variable_types: r int


decompiled_code:
const char *__fastcall mimetype_get(const char *a1)
{
char *v1; // rax
const char *v2; // rdx
int v3; // eax

v1 = strrchr(a1, 46);
v2 = "text/plain";
if ( v1 )
{
v3 = mimetype_find_def(v1 + 1);
v2 = "text/plain";
if ( v3 >= 0 )
v2 = off_348[2 * v3];
}
return v2;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
char *v1; // rax
const char *v2; // rdx
int v3; // eax

v1 = strrchr(a1, 46);
v2 = "text/plain";
if ( v1 )
{
v3 = func0(v1 + 1);
v2 = "text/plain";
if ( v3 >= 0 )
v2 = off_348[2 * v3];
}
return v2;
}

original_code:
const char *mimetype_get(const char *name) {
    int i;
    name = strrchr(name, '.');
    if (name != ((void *)0)) {
        i = mimetype_find_def(name + 1);
        if (i >= 0) {
            return DEFAULT_MIME_TYPES[i].type;
        }
    }
    return "text/plain";
}

canonical_original_code:
char *mimetype_get(char *name) {
    int i;
    name = strrchr(name, '.');
    if (name != ((void *)0)) {
        i = mimetype_find_def(name + 1);
        if (i >= 0) {
            return DEFAULT_MIME_TYPES[i].type;
        }
    }
    return "text/plain";
}

variable_types:
name
char *

variable_types: i int


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

if ( (*(_BYTE *)(a1 + 1264) & 4) != 0 )
{
result = client_reset();
*(_DWORD *)(a1 + 16) = 1;
}
else
{
*(_DWORD *)(a1 + 16) = 0;
}
return result;
}

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

if ( (*(_BYTE *)(a1 + 1264) & 4) != 0 )
{
result = client_reset();
*(_DWORD *)(a1 + 16) = 1;
}
else
{
*(_DWORD *)(a1 + 16) = 0;
}
return result;
}

original_code:
static
void state_finish(struct client_t *client) {
    if (client->flags & CLIENT_FLAG_KEEPALIVE) {
        client_reset(client);
        client->state = STATE_RECV_HEADER;
    } else {
        client->state = STATE_NONE;
    }
}

variable_types: struct client_t { int remote_fd; int local_rfd; long timeout; int state; char ip[40]; struct request_t { char *header[-1]; char *method; char *url; char *query_string; char *version; char *range_from; char *range_to; long header_length; } request; char data[1024]; long data_length; long data_sent; struct response_t { int status_code; char *content_type; long total_length; long content_length; long content_from; long last_mod; } response; long file_sent; unsigned int flags; struct client_t *next; struct client_t **prev; } *


original_code:
void state_send_header(struct client_t *client) {
    ssize_t len;
    len = send(client->remote_fd, client->data + client->data_sent,
                client->data_length - client->data_sent, MSG_NOSIGNAL);
    do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "send", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
    client->data_sent += len;
    if (client->data_sent >= client->data_length) {
        client->data_length = client->data_sent = 0;
        if (client->local_rfd == -1) {
            state_finish(client);
        } else {
            client->state = STATE_SEND_FILE;
        }
    }
}

canonical_original_code:
void state_send_header(struct client_t *client) {
    long len;
    len = send(client->remote_fd, client->data + client->data_sent,
                client->data_length - client->data_sent, MSG_NOSIGNAL);
    do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "send", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
    client->data_sent += len;
    if (client->data_sent >= client->data_length) {
        client->data_length = client->data_sent = 0;
        if (client->local_rfd == -1) {
            state_finish(client);
        } else {
            client->state = STATE_SEND_FILE;
        }
    }
}

variable_types: len long


original_code:
void state_recv_header(struct client_t *client) {
    ssize_t len;
    if (client->request.header_length <= 0) {
        len = recv(client->remote_fd, client->data, sizeof(client->data),
                MSG_PEEK);
        do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "peek", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
        if (len > 4) {
            client->request.header_length = http_find_headerlength(client->data,
                    len);
            if (client->request.header_length < 0) {
                if (len >= 1024) {
                    client->response.status_code = HTTP_STATUS_ENTITYTOOLARGE;
                    client->data_length = http_gen_errorpage(&client->response,
                            client->data, sizeof(client->data));
                    client->state = STATE_SEND_HEADER;
                }
            }
        }
    }
    if (client->request.header_length > 0) {
        len = recv(client->remote_fd, client->data + client->data_length,
                client->request.header_length - client->data_length, 0);
        do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "recv", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
        client->data_length += len;
        if (client->data_length >= client->request.header_length) {
            client_process(client);
        }
    }
    if (client->state == STATE_SEND_HEADER) {
        state_send_header(client);
    }
}

canonical_original_code:
void state_recv_header(struct client_t *client) {
    long len;
    if (client->request.header_length <= 0) {
        len = recv(client->remote_fd, client->data, sizeof(client->data),
                MSG_PEEK);
        do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "peek", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
        if (len > 4) {
            client->request.header_length = http_find_headerlength(client->data,
                    len);
            if (client->request.header_length < 0) {
                if (len >= 1024) {
                    client->response.status_code = HTTP_STATUS_ENTITYTOOLARGE;
                    client->data_length = http_gen_errorpage(&client->response,
                            client->data, sizeof(client->data));
                    client->state = STATE_SEND_HEADER;
                }
            }
        }
    }
    if (client->request.header_length > 0) {
        len = recv(client->remote_fd, client->data + client->data_length,
                client->request.header_length - client->data_length, 0);
        do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "recv", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
        client->data_length += len;
        if (client->data_length >= client->request.header_length) {
            client_process(client);
        }
    }
    if (client->state == STATE_SEND_HEADER) {
        state_send_header(client);
    }
}


decompiled_code:
int __fastcall state_send_file(unsigned int *a1)
{
__int64 v1; // rax
int *v2; // rax
char *v3; // rax
off_t offset[2]; // [rsp+8h] [rbp-10h] BYREF

v1 = *((_QWORD *)a1 + 157);
offset[0] = *((_QWORD *)a1 + 155) + v1;
v2 = (int *)sendfile(*a1, a1[1], offset, *((_QWORD *)a1 + 154) - v1);
if ( v2 == (int *)-1LL )
{
v2 = _errno_location();
if ( *v2 != 11 )
{
v3 = strerror(*v2);
LODWORD(v2) = fprintf(stderr, "%s: client %d %s\n", "sendfile", *a1, v3);
a1[4] = 0;
}
}
else if ( v2 )
{
v2 = (int *)((char *)v2 + *((_QWORD *)a1 + 157));
*((_QWORD *)a1 + 157) = v2;
if ( (__int64)v2 >= *((_QWORD *)a1 + 154) )
{
close(a1[1]);
a1[1] = -1;
LODWORD(v2) = state_finish((__int64)a1);
}
}
else
{
a1[4] = 0;
}
return (int)v2;
}

original_code:
void state_send_file(struct client_t *client) {
    ssize_t len;
    off_t offset;
    offset = client->response.content_from + client->file_sent;
    len = sendfile(client->remote_fd, client->local_rfd, &offset,
            client->response.content_length - client->file_sent);
    do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "sendfile", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
    client->file_sent += len;
    if (client->file_sent >= client->response.content_length) {
        close(client->local_rfd);
        client->local_rfd = -1;
        state_finish(client);
    }
}

canonical_original_code:
void state_send_file(struct client_t *client) {
    long len;
    long offset;
    offset = client->response.content_from + client->file_sent;
    len = sendfile(client->remote_fd, client->local_rfd, &offset,
            client->response.content_length - client->file_sent);
    do { if (len == -1) { if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) { ; } else { fprintf(stderr, "%s: client %d %s" "\n", "sendfile", client->remote_fd, strerror((*__errno_location ()))); client->state = STATE_NONE; } return; } else if (len == 0) { client->state = STATE_NONE; return; } } while (0);
    client->file_sent += len;
    if (client->file_sent >= client->response.content_length) {
        close(client->local_rfd);
        client->local_rfd = -1;
        state_finish(client);
    }
}

variable_types: offset long


repo: nqv/niscy

original_code:
static void parse_args(int argc, char **argv) {
    int i;
    char *arg;
    for (i = 1; i < argc; ++i) {
        arg = argv[i];
        if (arg[0] != '-') {
            smtp_.mail_to = (const char **)&argv[i];
            break;
        }
        switch (arg[1]) {
        case 'U':
            smtp_.user = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'P':
            smtp_.pass = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'F':
            smtp_.mail_from = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'a':
            smtp_.auth = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'c':
            smtp_.cert = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'd':
            smtp_.domain = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'h':
            smtp_.host = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'p':
            smtp_.port = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 's':
            smtp_.security = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        }
    }
}

canonical_original_code:
static void parse_args(int argc, char **argv) {
    int i;
    char *arg;
    for (i = 1; i < argc; ++i) {
        arg = argv[i];
        if (arg[0] != '-') {
            smtp_.mail_to = (char **)&argv[i];
            break;
        }
        switch (arg[1]) {
        case 'U':
            smtp_.user = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'P':
            smtp_.pass = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'F':
            smtp_.mail_from = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'a':
            smtp_.auth = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'c':
            smtp_.cert = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'd':
            smtp_.domain = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'h':
            smtp_.host = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 'p':
            smtp_.port = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        case 's':
            smtp_.security = (arg[2] == '\0') ? argv[++i] : &arg[2];
            break;
        }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
arg
char *


decompiled_code:
int check_settings()
{
const char *v0; // rbx
int result; // eax

if ( smtp_[0] && *smtp_[0] )
{
if ( off_E68 && *off_E68 )
{
if ( off_E88 && *off_E88 )
{
if ( qword_EA0 && *(_BYTE *)qword_EA0 )
{
if ( qword_EA8 && *(_QWORD *)qword_EA8 )
{
v0 = s1;
result = 0;
if ( s1 )
{
result = strcasecmp(s1, "starttls");
if ( result )
{
result = strcasecmp(v0, "tls");
if ( result )
{
fwrite("Encrypted method is not supported.\n", 1uLL, 0x23uLL, stderr);
result = -1;
}
else
{
dword_EB0 |= 1u;
}
}
else
{
dword_EB0 |= 3u;
}
}
}
else
{
fwrite("Receipt address must not be empty.\n", 1uLL, 0x23uLL, stderr);
result = -1;
}
}
else
{
fwrite("FROM address must not be empty.\n", 1uLL, 0x20uLL, stderr);
result = -1;
}
}
else
{
fwrite("Domain must not be empty.\n", 1uLL, 0x1AuLL, stderr);
result = -1;
}
}
else
{
fwrite("Port must not be empty.\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
}
else
{
fwrite("Host must not be empty.\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
return result;
}

canonical_decompiled_code:
int func1()
{
const char *v0; // rbx
int result; // eax

if ( smtp_[0] && *smtp_[0] )
{
if ( off_E68 && *off_E68 )
{
if ( off_E88 && *off_E88 )
{
if ( qword_EA0 && *(_BYTE *)qword_EA0 )
{
if ( qword_EA8 && *(_QWORD *)qword_EA8 )
{
v0 = s1;
result = 0;
if ( s1 )
{
result = strcasecmp(s1, "starttls");
if ( result )
{
result = strcasecmp(v0, "tls");
if ( result )
{
fwrite("Encrypted method is not supported.\n", 1uLL, 0x23uLL, stderr);
result = -1;
}
else
{
dword_EB0 |= 1u;
}
}
else
{
dword_EB0 |= 3u;
}
}
}
else
{
fwrite("Receipt address must not be empty.\n", 1uLL, 0x23uLL, stderr);
result = -1;
}
}
else
{
fwrite("FROM address must not be empty.\n", 1uLL, 0x20uLL, stderr);
result = -1;
}
}
else
{
fwrite("Domain must not be empty.\n", 1uLL, 0x1AuLL, stderr);
result = -1;
}
}
else
{
fwrite("Port must not be empty.\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
}
else
{
fwrite("Host must not be empty.\n", 1uLL, 0x18uLL, stderr);
result = -1;
}
return result;
}

original_code:
static int check_settings() {
    if (((smtp_.host) == ((void *)0) || (smtp_.host)[0] == '\0')) {
        fprintf(stderr, "Host must not be empty.\n");
        return -1;
    }
    if (((smtp_.port) == ((void *)0) || (smtp_.port)[0] == '\0')) {
        fprintf(stderr, "Port must not be empty.\n");
        return -1;
    }
    if (((smtp_.domain) == ((void *)0) || (smtp_.domain)[0] == '\0')) {
        fprintf(stderr, "Domain must not be empty.\n");
        return -1;
    }
    if (((smtp_.mail_from) == ((void *)0) || (smtp_.mail_from)[0] == '\0')) {
        fprintf(stderr, "FROM address must not be empty.\n");
        return -1;
    }
    if ((smtp_.mail_to == ((void *)0)) || (*smtp_.mail_to == ((void *)0))) {
        fprintf(stderr, "Receipt address must not be empty.\n");
        return -1;
    }
    if (smtp_.security != ((void *)0)) {
        if (strcasecmp(smtp_.security, "starttls") == 0) {
            smtp_.options |= ((1 << 0) | (1 << 1));
        } else if (strcasecmp(smtp_.security, "tls") == 0) {
            smtp_.options |= (1 << 0);
        } else {
            fprintf(stderr, "Encrypted method is not supported.\n");
            return -1;
        }
    }
    return 0;
}


decompiled_code:
__int64 niscy_disconnect()
{
smtp_write(smtp_, "QUIT\r\n", 6LL);
return smtp_close(smtp_);
}

canonical_decompiled_code:
__int64 func2()
{
smtp_write(smtp_, "QUIT\r\n", 6LL);
return smtp_close(smtp_);
}

original_code:
static void niscy_disconnect() {
    smtp_write(&smtp_, "QUIT\r\n", 6);
    smtp_close(&smtp_);
    ;
}


decompiled_code:
int get_smtp_code()
{
int v0; // ebx
int v1; // eax
const char *v2; // rax

v0 = 0;
while ( 1 )
{
v1 = smtp_read(smtp_, &buf_ + v0, (unsigned int)(1024 - v0));
if ( !v1 )
goto LABEL_2;
if ( v1 < 0 )
return -1;
v0 += v1;
if ( *(&buf_ + v0 - 1) == 10 )
{
v2 = find_lastline(&buf_, v0 - 1);
if ( v2[3] == 32 )
{
if ( v0 > 1023 )
{
LABEL_8:
v2 = find_lastline(&buf_, 1023);
v0 = 1024;
}
*(&buf_ + v0 - 1) = 0;
return atoi(v2);
}
}
LABEL_2:
if ( v0 > 1023 )
goto LABEL_8;
}
}

canonical_decompiled_code:
int func3()
{
int v0; // ebx
int v1; // eax
const char *v2; // rax

v0 = 0;
while ( 1 )
{
v1 = smtp_read(smtp_, &buf_ + v0, (unsigned int)(1024 - v0));
if ( !v1 )
goto LABEL_2;
if ( v1 < 0 )
return -1;
v0 += v1;
if ( *(&buf_ + v0 - 1) == 10 )
{
v2 = find_lastline(&buf_, v0 - 1);
if ( v2[3] == 32 )
{
if ( v0 > 1023 )
{
LABEL_8:
v2 = find_lastline(&buf_, 1023);
v0 = 1024;
}
*(&buf_ + v0 - 1) = 0;
return atoi(v2);
}
}
LABEL_2:
if ( v0 > 1023 )
goto LABEL_8;
}
}

original_code:
static int get_smtp_code() {
    int len = 0;
    int rv;
    const char *lastline;
    while (len < (int)sizeof(buf_)) {
        rv = smtp_read(&smtp_, buf_ + len, sizeof(buf_) - len);
        if (rv == 0) {
            continue;
        }
        if (rv < 0) {
            return -1;
        }
        len += rv;
        if (buf_[len - 1] == '\n') {
            ;
            lastline = find_lastline(buf_, len - 1);
            if (lastline[3] == ' ') {
                break;
            }
        }
    }
    if (len >= (int)sizeof(buf_)) {
        len = sizeof(buf_);
        lastline = find_lastline(buf_, len - 1);
    }
    buf_[len - 1] = '\0';
    return atoi(lastline);
}

canonical_original_code:
static int get_smtp_code() {
    int len = 0;
    int rv;
    char *lastline;
    while (len < (int)sizeof(buf_)) {
        rv = smtp_read(&smtp_, buf_ + len, sizeof(buf_) - len);
        if (rv == 0) {
            continue;
        }
        if (rv < 0) {
            return -1;
        }
        len += rv;
        if (buf_[len - 1] == '\n') {
            ;
            lastline = find_lastline(buf_, len - 1);
            if (lastline[3] == ' ') {
                break;
            }
        }
    }
    if (len >= (int)sizeof(buf_)) {
        len = sizeof(buf_);
        lastline = find_lastline(buf_, len - 1);
    }
    buf_[len - 1] = '\0';
    return atoi(lastline);
}

variable_types: len int

variable_types: rv int

variable_types:
lastline
char *


starttls

decompiled_code:
__int64 starttls()
{
unsigned int v0; // eax
__int64 result; // rax

if ( (int)smtp_write(smtp_, "STARTTLS\r\n", 10LL) <= 0 )
{
smtp_close(smtp_);
result = 0xFFFFFFFFLL;
}
else
{
v0 = get_smtp_code();
if ( v0 - 200 > 0x63 )
{
fprintf(stderr, "Server does not accept STARTTLS: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // eax
__int64 result; // rax

if ( (int)smtp_write(smtp_, "STARTTLS\r\n", 10LL) <= 0 )
{
smtp_close(smtp_);
result = 0xFFFFFFFFLL;
}
else
{
v0 = func3();
if ( v0 - 200 > 0x63 )
{
fprintf(stderr, "Server does not accept STARTTLS: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
static int starttls() {
    int len;
    if (smtp_write(&smtp_, "STARTTLS\r\n", 10) <= 0) {
        smtp_close(&smtp_);
        return -1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Server does not accept STARTTLS: %d.\n", len);
        return -1;
    }
    return 0;
}


probe

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

if ( (unsigned int)smtp_open(smtp_) )
{
fwrite("Could not connect to server.\n", 1uLL, 0x1DuLL, stderr);
v0 = 1;
}
else
{
v0 = 0;
v1 = get_smtp_code();
if ( v1 - 200 > 0x63 )
{
fprintf(stderr, "Unexpected server response: %d.\n", v1);
v0 = -1;
}
}
return v0;
}

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

if ( (unsigned int)smtp_open(smtp_) )
{
fwrite("Could not connect to server.\n", 1uLL, 0x1DuLL, stderr);
v0 = 1;
}
else
{
v0 = 0;
v1 = func3();
if ( v1 - 200 > 0x63 )
{
fprintf(stderr, "Unexpected server response: %d.\n", v1);
v0 = -1;
}
}
return v0;
}

original_code:
static int probe() {
    int len;
    if (smtp_open(&smtp_) != 0) {
        fprintf(stderr, "Could not connect to server.\n");
        return 1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Unexpected server response: %d.\n", len);
        return -1;
    }
    return 0;
}


ehlo

decompiled_code:
__int64 ehlo()
{
const char *v0; // rdx
int v1; // eax
unsigned int v2; // eax

v0 = "EHLO %s\r\n";
if ( !s )
v0 = "HELO %s\r\n";
v1 = snprintf(&buf_, 0x400uLL, v0, off_E88);
if ( v1 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v1) <= 0 )
return 0xFFFFFFFFLL;
v2 = get_smtp_code();
if ( v2 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Unexpected EHLO response: %d.\n", v2);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func6()
{
const char *v0; // rdx
int v1; // eax
unsigned int v2; // eax

v0 = "EHLO %s\r\n";
if ( !s )
v0 = "HELO %s\r\n";
v1 = snprintf(&buf_, 0x400uLL, v0, off_E88);
if ( v1 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v1) <= 0 )
return 0xFFFFFFFFLL;
v2 = func3();
if ( v2 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Unexpected EHLO response: %d.\n", v2);
return 0xFFFFFFFFLL;
}

original_code:
static int ehlo() {
    int len;
    len = snprintf(buf_, sizeof(buf_),
            (smtp_.user != ((void *)0)) ? "EHLO %s\r\n" : "HELO %s\r\n", smtp_.domain);
    if (len >= (int)sizeof(buf_)) {
        return -1;
    }
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Unexpected EHLO response: %d.\n", len);
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 niscy_connect()
{
__int64 result; // rax

if ( (dword_EB0 & 2) != 0 )
{
dword_EB0 &= 0xFFFFFFFE;
if ( !(unsigned int)probe() && !(unsigned int)ehlo() && !(unsigned int)starttls() )
{
dword_EB0 |= 1u;
result = smtp_open(smtp_);
if ( (_DWORD)result )
{
fwrite("Could not negotiate secure connection.\n", 1uLL, 0x27uLL, stderr);
smtp_close(smtp_);
result = 0xFFFFFFFFLL;
}
return result;
}
LABEL_11:
smtp_close(smtp_);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)probe() )
goto LABEL_11;
result = ehlo();
if ( (_DWORD)result )
goto LABEL_11;
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax

if ( (dword_EB0 & 2) != 0 )
{
dword_EB0 &= 0xFFFFFFFE;
if ( !(unsigned int)func5() && !(unsigned int)func6() && !(unsigned int)func4() )
{
dword_EB0 |= 1u;
result = smtp_open(smtp_);
if ( (_DWORD)result )
{
fwrite("Could not negotiate secure connection.\n", 1uLL, 0x27uLL, stderr);
smtp_close(smtp_);
result = 0xFFFFFFFFLL;
}
return result;
}
LABEL_11:
smtp_close(smtp_);
return 0xFFFFFFFFLL;
}
if ( (unsigned int)func5() )
goto LABEL_11;
result = func6();
if ( (_DWORD)result )
goto LABEL_11;
return result;
}

original_code:
static int niscy_connect() {
    if (smtp_.options & (1 << 1)) {
        smtp_.options &= ~(1 << 0);
        if ((probe() != 0) || (ehlo() != 0) || (starttls() != 0)) {
            smtp_close(&smtp_);
            return -1;
        }
        smtp_.options |= (1 << 0);
        if (smtp_open(&smtp_) != 0) {
            fprintf(stderr, "Could not negotiate secure connection.\n");
            smtp_close(&smtp_);
            return -1;
        }
    } else {
        if ((probe() != 0) || (ehlo() != 0)) {
            smtp_close(&smtp_);
            return -1;
        }
    }
    return 0;
}


decompiled_code:
__int64 mail_from()
{
int v0; // eax
unsigned int v1; // eax

v0 = snprintf(&buf_, 0x400uLL, "MAIL FROM:<%s>\r\n", (const char *)qword_EA0);
if ( v0 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v0) <= 0 )
return 0xFFFFFFFFLL;
v1 = get_smtp_code();
if ( v1 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Invalid MAIL FROM %d.\n", v1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // eax
unsigned int v1; // eax

v0 = snprintf(&buf_, 0x400uLL, "MAIL FROM:<%s>\r\n", (const char *)qword_EA0);
if ( v0 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v0) <= 0 )
return 0xFFFFFFFFLL;
v1 = func3();
if ( v1 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Invalid MAIL FROM %d.\n", v1);
return 0xFFFFFFFFLL;
}

original_code:
static int mail_from() {
    int len;
    len = snprintf(buf_, sizeof(buf_), "MAIL FROM:<%s>\r\n", smtp_.mail_from);
    if (len >= (int)sizeof(buf_)) {
        return -1;
    }
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Invalid MAIL FROM %d.\n", len);
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 rcpt_to()
{
const char *v0; // rbx
__int64 v1; // rbp
int v2; // er12
int v3; // eax
unsigned int v4; // eax

v0 = *(const char **)qword_EA8;
if ( *(_QWORD *)qword_EA8 )
{
v1 = 8LL;
v2 = 0;
while ( 1 )
{
if ( *v0 != 45 )
{
v3 = snprintf(&buf_, 0x400uLL, "RCPT TO:<%s>\r\n", v0);
if ( v3 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v3) <= 0 )
return 0xFFFFFFFFLL;
v4 = get_smtp_code();
if ( v4 - 200 > 0x63 )
fprintf(stderr, "Invalid RCPT TO <%s>: %d.\n", v0, v4);
else
++v2;
}
v0 = *(const char **)(qword_EA8 + v1);
v1 += 8LL;
if ( !v0 )
return (unsigned int)-(v2 == 0);
}
}
v2 = 0;
return (unsigned int)-(v2 == 0);
}

canonical_decompiled_code:
__int64 func9()
{
const char *v0; // rbx
__int64 v1; // rbp
int v2; // er12
int v3; // eax
unsigned int v4; // eax

v0 = *(const char **)qword_EA8;
if ( *(_QWORD *)qword_EA8 )
{
v1 = 8LL;
v2 = 0;
while ( 1 )
{
if ( *v0 != 45 )
{
v3 = snprintf(&buf_, 0x400uLL, "RCPT TO:<%s>\r\n", v0);
if ( v3 > 1023 )
return 0xFFFFFFFFLL;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)v3) <= 0 )
return 0xFFFFFFFFLL;
v4 = func3();
if ( v4 - 200 > 0x63 )
fprintf(stderr, "Invalid RCPT TO <%s>: %d.\n", v0, v4);
else
++v2;
}
v0 = *(const char **)(qword_EA8 + v1);
v1 += 8LL;
if ( !v0 )
return (unsigned int)-(v2 == 0);
}
}
v2 = 0;
return (unsigned int)-(v2 == 0);
}

original_code:
static int rcpt_to() {
    int len;
    int i;
    int count = 0;
    const char *addr;
    for (i = 0; (addr = smtp_.mail_to[i]) != ((void *)0); ++i) {
        if (addr[0] == '-') {
            continue;
        }
        len = snprintf(buf_, sizeof(buf_), "RCPT TO:<%s>\r\n", addr);
        if (len >= (int)sizeof(buf_)) {
            return -1;
        }
        if (smtp_write(&smtp_, buf_, len) <= 0) {
            return -1;
        }
        len = get_smtp_code();
        if (len < 200 || len >= 300) {
            fprintf(stderr, "Invalid RCPT TO <%s>: %d.\n", addr, len);
        } else {
            ++count;
        }
    }
    return (count == 0) ? -1 : 0;
}

canonical_original_code:
static int rcpt_to() {
    int len;
    int i;
    int count = 0;
    char *addr;
    for (i = 0; (addr = smtp_.mail_to[i]) != ((void *)0); ++i) {
        if (addr[0] == '-') {
            continue;
        }
        len = snprintf(buf_, sizeof(buf_), "RCPT TO:<%s>\r\n", addr);
        if (len >= (int)sizeof(buf_)) {
            return -1;
        }
        if (smtp_write(&smtp_, buf_, len) <= 0) {
            return -1;
        }
        len = get_smtp_code();
        if (len < 200 || len >= 300) {
            fprintf(stderr, "Invalid RCPT TO <%s>: %d.\n", addr, len);
        } else {
            ++count;
        }
    }
    return (count == 0) ? -1 : 0;
}

variable_types: count int

variable_types:
addr
char *


decompiled_code:
__int64 auth_login()
{
unsigned int v0; // eax
char *v1; // rbx
size_t v2; // rax
int v3; // eax
unsigned int v4; // eax
char *v5; // rbx
size_t v6; // rax
int v7; // eax
unsigned int v8; // eax

if ( (int)smtp_write(smtp_, "AUTH LOGIN\r\n", 12LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = get_smtp_code();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH LOGIN response: %d.\n", v0);
return 0xFFFFFFFFLL;
}
v1 = s;
v2 = strlen(s);
v3 = base64_encode(v1, v2, &buf_, 1022LL);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
*(&buf_ + v3) = 13;
*(&buf_ + v3 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v3 + 2)) <= 0 )
return 0xFFFFFFFFLL;
v4 = get_smtp_code();
if ( v4 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH/Username response: %d.\n", v4);
return 0xFFFFFFFFLL;
}
v5 = qword_E80;
if ( qword_E80 )
{
v6 = strlen(qword_E80);
v7 = base64_encode(v5, v6, &buf_, 1022LL);
if ( v7 < 0 )
{
fwrite("Could not encode password.\n", 1uLL, 0x1BuLL, stderr);
return 0xFFFFFFFFLL;
}
}
else
{
v7 = 0;
}
*(&buf_ + v7) = 13;
*(&buf_ + v7 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v7 + 2)) <= 0 )
return 0xFFFFFFFFLL;
v8 = get_smtp_code();
if ( v8 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Authentication failed %d.\n", v8);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // eax
char *v1; // rbx
size_t v2; // rax
int v3; // eax
unsigned int v4; // eax
char *v5; // rbx
size_t v6; // rax
int v7; // eax
unsigned int v8; // eax

if ( (int)smtp_write(smtp_, "AUTH LOGIN\r\n", 12LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = func3();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH LOGIN response: %d.\n", v0);
return 0xFFFFFFFFLL;
}
v1 = s;
v2 = strlen(s);
v3 = base64_encode(v1, v2, &buf_, 1022LL);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
*(&buf_ + v3) = 13;
*(&buf_ + v3 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v3 + 2)) <= 0 )
return 0xFFFFFFFFLL;
v4 = func3();
if ( v4 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH/Username response: %d.\n", v4);
return 0xFFFFFFFFLL;
}
v5 = qword_E80;
if ( qword_E80 )
{
v6 = strlen(qword_E80);
v7 = base64_encode(v5, v6, &buf_, 1022LL);
if ( v7 < 0 )
{
fwrite("Could not encode password.\n", 1uLL, 0x1BuLL, stderr);
return 0xFFFFFFFFLL;
}
}
else
{
v7 = 0;
}
*(&buf_ + v7) = 13;
*(&buf_ + v7 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v7 + 2)) <= 0 )
return 0xFFFFFFFFLL;
v8 = func3();
if ( v8 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Authentication failed %d.\n", v8);
return 0xFFFFFFFFLL;
}

original_code:
static int auth_login() {
    int len;
    if (smtp_write(&smtp_, "AUTH LOGIN\r\n", 12) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 300 || len >= 400) {
        fprintf(stderr, "Unexpected AUTH LOGIN response: %d.\n", len);
        return -1;
    }
    len = base64_encode(smtp_.user, strlen(smtp_.user), buf_,
            sizeof(buf_) - 2);
    ;
    if (len < 0) {
        return -1;
    }
    buf_[len++] = '\r';
    buf_[len++] = '\n';
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 300 || len >= 400) {
        fprintf(stderr, "Unexpected AUTH/Username response: %d.\n", len);
        return -1;
    }
    if (smtp_.pass != ((void *)0)) {
        len = base64_encode(smtp_.pass, strlen(smtp_.pass), buf_,
                sizeof(buf_) - 2);
        if (len < 0) {
            fprintf(stderr, "Could not encode password.\n");
            return -1;
        }
    } else {
        len = 0;
    }
    buf_[len++] = '\r';
    buf_[len++] = '\n';
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Authentication failed %d.\n", len);
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 auth_plain()
{
unsigned int v0; // eax
const char *v1; // rax
int v2; // eax
int v3; // eax
unsigned int v4; // eax
__int64 result; // rax
char s[520]; // [rsp+0h] [rbp-208h] BYREF

if ( (int)smtp_write(smtp_, "AUTH PLAIN\r\n", 12LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = get_smtp_code();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH PLAIN response: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
v1 = qword_E80;
if ( !qword_E80 )
v1 = (const char *)&unk_1584;
v2 = snprintf(s, 0x200uLL, "%s%c%s%c%s", ::s, 0LL, ::s, 0LL, v1);
if ( v2 > 511 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = base64_encode(s, (unsigned int)v2, &buf_, 1022LL);
if ( v3 < 0 )
{
fwrite("Could not encode authentication.\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
*(&buf_ + v3) = 13;
*(&buf_ + v3 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v3 + 2)) <= 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v4 = get_smtp_code();
if ( v4 - 200 > 0x63 )
{
fprintf(stderr, "Authentication failed %d.\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
unsigned int v0; // eax
const char *v1; // rax
int v2; // eax
int v3; // eax
unsigned int v4; // eax
__int64 result; // rax
char s[520]; // [rsp+0h] [rbp-208h] BYREF

if ( (int)smtp_write(smtp_, "AUTH PLAIN\r\n", 12LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = func3();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH PLAIN response: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
v1 = qword_E80;
if ( !qword_E80 )
v1 = (const char *)&unk_1584;
v2 = snprintf(s, 0x200uLL, "%s%c%s%c%s", ::s, 0LL, ::s, 0LL, v1);
if ( v2 > 511 )
{
result = 0xFFFFFFFFLL;
}
else
{
v3 = base64_encode(s, (unsigned int)v2, &buf_, 1022LL);
if ( v3 < 0 )
{
fwrite("Could not encode authentication.\n", 1uLL, 0x21uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
*(&buf_ + v3) = 13;
*(&buf_ + v3 + 1) = 10;
if ( (int)smtp_write(smtp_, &buf_, (unsigned int)(v3 + 2)) <= 0 )
{
result = 0xFFFFFFFFLL;
}
else
{
v4 = func3();
if ( v4 - 200 > 0x63 )
{
fprintf(stderr, "Authentication failed %d.\n", v4);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
}
}
return result;
}

original_code:
static int auth_plain() {
    int len;
    char message[512];
    if (smtp_write(&smtp_, "AUTH PLAIN\r\n", 12) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 300 || len >= 400) {
        fprintf(stderr, "Unexpected AUTH PLAIN response: %d.\n", len);
        return -1;
    }
    len = snprintf(message, sizeof(message), "%s%c%s%c%s",
            smtp_.user, '\0', smtp_.user, '\0',
            (smtp_.pass != ((void *)0)) ? smtp_.pass : "");
    if (len >= (int)sizeof(message)) {
        return -1;
    }
    len = base64_encode(message, len, buf_, sizeof(buf_) - 2);
    if (len < 0) {
        fprintf(stderr, "Could not encode authentication.\n");
        return -1;
    }
    buf_[len++] = '\r';
    buf_[len++] = '\n';
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 200 || len >= 300) {
        fprintf(stderr, "Authentication failed %d.\n", len);
        return -1;
    }
    return 0;
}

variable_types:
message
char [512]


decompiled_code:
__int64 niscy_auth()
{
__int64 result; // rax
const char *v1; // rbx

result = 0LL;
if ( s )
{
v1 = qword_E90;
if ( !qword_E90 || !strcasecmp(qword_E90, "login") )
{
result = auth_login();
}
else if ( !strcasecmp(v1, "plain") )
{
result = auth_plain();
}
else
{
fprintf(stderr, "Authentication method is not supported: %s.\n", v1);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 result; // rax
const char *v1; // rbx

result = 0LL;
if ( s )
{
v1 = qword_E90;
if ( !qword_E90 || !strcasecmp(qword_E90, "login") )
{
result = func10();
}
else if ( !strcasecmp(v1, "plain") )
{
result = func11();
}
else
{
fprintf(stderr, "Authentication method is not supported: %s.\n", v1);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int niscy_auth() {
    if (smtp_.user != ((void *)0)) {
        if (smtp_.auth == ((void *)0) || strcasecmp(smtp_.auth, "login") == 0) {
            return auth_login();
        }
        if (strcasecmp(smtp_.auth, "plain") == 0) {
            return auth_plain();
        }
        fprintf(stderr, "Authentication method is not supported: %s.\n", smtp_.auth);
        return -1;
    } else {
        ;
    }
    return 0;
}


decompiled_code:
size_t __fastcall format_date(char *s, int a2)
{
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = time(0LL);
v2 = localtime(&timer);
return strftime(s, a2, "%a, %d %b %Y %H:%M:%S %z", v2);
}

canonical_decompiled_code:
size_t __fastcall func13(char *s, int a2)
{
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

timer = time(0LL);
v2 = localtime(&timer);
return strftime(s, a2, "%a, %d %b %Y %H:%M:%S %z", v2);
}

original_code:
static int format_date(char *data, int sz) {
    time_t now;
    struct tm *tm;
    const char * const FORMAT = "%a, %d %b %Y %H:%M:%S %z";
    now = time(((void *)0));
    tm = localtime(&now);
    return strftime(data, sz, FORMAT, tm);
}

canonical_original_code:
static int format_date(char *data, int sz) {
    long now;
    struct tm *tm;
    char *FORMAT = "%a, %d %b %Y %H:%M:%S %z";
    now = time(((void *)0));
    tm = localtime(&now);
    return strftime(data, sz, FORMAT, tm);
}

variable_types:
data
char *

variable_types: sz int

variable_types: now 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:
FORMAT
char *


decompiled_code:
__int64 mail_data()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ebx
__int64 result; // rax

if ( (int)smtp_write(smtp_, "DATA\r\n", 6LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = get_smtp_code();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected DATA response: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
v1 = snprintf(&buf_, 0x400uLL, "Received: by %s (NISCY v0.1); ", off_E88);
if ( v1 > 1023 )
{
result = 0xFFFFFFFFLL;
}
else
{
v2 = format_date(&buf_ + v1, 1024 - v1) + v1;
*(&buf_ + v2) = 13;
*(&buf_ + v2 + 1) = 10;
result = (unsigned int)-((int)smtp_write(smtp_, &buf_, (unsigned int)(v2 + 2)) <= 0);
}
}
return result;
}

canonical_decompiled_code:
__int64 func14()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ebx
__int64 result; // rax

if ( (int)smtp_write(smtp_, "DATA\r\n", 6LL) <= 0 )
return 0xFFFFFFFFLL;
v0 = func3();
if ( v0 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected DATA response: %d.\n", v0);
result = 0xFFFFFFFFLL;
}
else
{
v1 = snprintf(&buf_, 0x400uLL, "Received: by %s (NISCY v0.1); ", off_E88);
if ( v1 > 1023 )
{
result = 0xFFFFFFFFLL;
}
else
{
v2 = func13(&buf_ + v1, 1024 - v1) + v1;
*(&buf_ + v2) = 13;
*(&buf_ + v2 + 1) = 10;
result = (unsigned int)-((int)smtp_write(smtp_, &buf_, (unsigned int)(v2 + 2)) <= 0);
}
}
return result;
}

original_code:
static int mail_data() {
    int len;
    if (smtp_write(&smtp_, "DATA\r\n", 6) <= 0) {
        return -1;
    }
    len = get_smtp_code();
    if (len < 300 || len >= 400) {
        fprintf(stderr, "Unexpected DATA response: %d.\n", len);
        return -1;
    }
    len = snprintf(buf_, sizeof(buf_), "Received: by %s"
            " (NISCY v" "0.1" "); ", smtp_.domain);
    if (len >= (int)sizeof(buf_)) {
        return -1;
    }
    len += format_date(buf_ + len, sizeof(buf_) - len);
    buf_[len++] = '\r';
    buf_[len++] = '\n';
    if (smtp_write(&smtp_, buf_, len) <= 0) {
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 niscy_mail()
{
if ( (unsigned int)mail_from() )
return 0xFFFFFFFFLL;
if ( (unsigned int)rcpt_to() )
return 0xFFFFFFFFLL;
if ( (unsigned int)mail_data() )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)mail_stream(stdin) != 0);
}

canonical_decompiled_code:
__int64 func16()
{
if ( (unsigned int)func8() )
return 0xFFFFFFFFLL;
if ( (unsigned int)func9() )
return 0xFFFFFFFFLL;
if ( (unsigned int)func14() )
return 0xFFFFFFFFLL;
return (unsigned int)-((unsigned int)func15(stdin) != 0);
}

original_code:
static int niscy_mail() {
    if ((mail_from() != 0)
            || (rcpt_to() != 0)
            || (mail_data() != 0)
            || (mail_stream(stdin) != 0)) {
        return -1;
    }
    return 0;
}


main

canonical_name: main

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

parse_args(argc, (__int64)argv);
v3 = 1;
if ( !check_settings() && !(unsigned int)niscy_connect() )
{
if ( (unsigned int)niscy_auth() || (v3 = niscy_mail()) != 0 )
{
niscy_disconnect();
v3 = 1;
}
else
{
niscy_disconnect();
}
}
return v3;
}

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

func0(argc, (__int64)argv);
v3 = 1;
if ( !func1() && !(unsigned int)func7() )
{
if ( (unsigned int)func12() || (v3 = func16()) != 0 )
{
func2();
v3 = 1;
}
else
{
func2();
}
}
return v3;
}

original_code:
int main(int argc, char **argv) {
    parse_args(argc, argv);
    if (check_settings() != 0 || (niscy_connect() != 0)) {
        return 1;
    }
    if ((niscy_auth() != 0) || (niscy_mail() != 0)) {
        niscy_disconnect();
        return 1;
    }
    niscy_disconnect();
    return 0;
}


original_code:
int base64_encode(const char *in, int len, char *out, int sz) {
    const char BASE64_CHARS[] =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    int idx = 0;
    int i;
    uint32_t n = 0;
    int count = len % 3;
    uint8_t n0, n1, n2, n3;
    for (i = 0; i < len; i += 3) {
        n = in[i] << 16;
        if ((i + 1) < len)
            n += in[i + 1] << 8;
        if ((i + 2) < len)
            n += in[i + 2];
        n0 = (uint8_t) (n >> 18) & 63;
        n1 = (uint8_t) (n >> 12) & 63;
        n2 = (uint8_t) (n >> 6) & 63;
        n3 = (uint8_t) n & 63;
        if (idx >= sz)
            return -1;
        out[idx++] = BASE64_CHARS[n0];
        if (idx >= sz)
            return -1;
        out[idx++] = BASE64_CHARS[n1];
        if ((i + 1) < len) {
            if (idx >= sz)
                return -1;
            out[idx++] = BASE64_CHARS[n2];
        }
        if ((i + 2) < len) {
            if (idx >= sz)
                return -1;
            out[idx++] = BASE64_CHARS[n3];
        }
    }
    if (count > 0) {
        for (; count < 3; count++) {
            if (idx >= sz)
                return -1;
            out[idx++] = '=';
        }
    }
    if (idx >= sz)
        return -1;
    out[idx] = 0;
    return idx;
}

canonical_original_code:
int base64_encode(char *in, int len, char *out, int sz) {
    char BASE64_CHARS[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    int idx = 0;
    int i;
    unsigned int n = 0;
    int count = len % 3;
    unsigned char n0; unsigned char n1; unsigned char n2; unsigned char n3;
    for (i = 0; i < len; i += 3) {
        n = in[i] << 16;
        if ((i + 1) < len)
            n += in[i + 1] << 8;
        if ((i + 2) < len)
            n += in[i + 2];
        n0 = (unsigned char) (n >> 18) & 63;
        n1 = (unsigned char) (n >> 12) & 63;
        n2 = (unsigned char) (n >> 6) & 63;
        n3 = (unsigned char) n & 63;
        if (idx >= sz)
            return -1;
        out[idx++] = BASE64_CHARS[n0];
        if (idx >= sz)
            return -1;
        out[idx++] = BASE64_CHARS[n1];
        if ((i + 1) < len) {
            if (idx >= sz)
                return -1;
            out[idx++] = BASE64_CHARS[n2];
        }
        if ((i + 2) < len) {
            if (idx >= sz)
                return -1;
            out[idx++] = BASE64_CHARS[n3];
        }
    }
    if (count > 0) {
        for (; count < 3; count++) {
            if (idx >= sz)
                return -1;
            out[idx++] = '=';
        }
    }
    if (idx >= sz)
        return -1;
    out[idx] = 0;
    return idx;
}

variable_types:
in
char *

variable_types:
out
char *

variable_types: idx int

variable_types: i int

variable_types:
n
unsigned int


repo: nremond/framboisier

decompiled_code:
__int64 __fastcall http_write(const char *a1, __int64 a2, __int64 a3)
{
printf("body=%s\n", a1);
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, __int64 a2, __int64 a3)
{
printf("body=%s\n", a1);
return a3;
}

original_code:
size_t http_write(void* buf, size_t size, size_t nmemb, void* userp)
{
  printf("body=%s\n", (char*)buf);
  return nmemb;
}

canonical_original_code:
unsigned long http_write(void *buf, unsigned long size, unsigned long nmemb, void *userp)
{
  printf("body=%s\n", (char *)buf);
  return nmemb;
}

variable_types:
buf
void *

variable_types:
size
unsigned long

variable_types:
nmemb
unsigned long

variable_types:
userp
void *

return_type: unsigned long


decompiled_code:
__int64 rest_client_initialize()
{
return curl_global_init(3LL);
}

canonical_decompiled_code:
__int64 func1()
{
return curl_global_init(3LL);
}

original_code:
void rest_client_initialize()
{
  curl_global_init(((1<<0)|(1<<1)));
}


decompiled_code:
__int64 rest_client_cleanup()
{
return curl_global_cleanup();
}

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

original_code:
void rest_client_cleanup()
{
  curl_global_cleanup();
}


decompiled_code:
int __fastcall write_gpio(unsigned int a1, const void *a2)
{
int v2; // ebx
char v4[152]; // [rsp+0h] [rbp-98h] BYREF

sprintf(v4, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v4, 1);
write(v2, a2, 1uLL);
return close(v2);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, const void *a2)
{
int v2; // ebx
char v4[152]; // [rsp+0h] [rbp-98h] BYREF

sprintf(v4, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v4, 1);
write(v2, a2, 1uLL);
return close(v2);
}

original_code:
int write_gpio(const int pin, const char *value)
{
 char gpio_name[128];
 sprintf(gpio_name, "/sys/class/gpio/gpio%d/value", pin) ;
 const int fd = open(gpio_name, 01);
 write(fd, value, 1);
 return close(fd);
}

canonical_original_code:
int write_gpio(int pin, char *value)
{
 char gpio_name[128];
 sprintf(gpio_name, "/sys/class/gpio/gpio%d/value", pin) ;
 int fd = open(gpio_name, 01);
 write(fd, value, 1);
 return close(fd);
}

variable_types: pin int

variable_types:
value
char *

variable_types: fd int


decompiled_code:
int turn_led_on()
{
return write_gpio(0x1Bu, GPIO_ON);
}

canonical_decompiled_code:
int func1()
{
return func0(0x1Bu, GPIO_ON);
}

original_code:
int turn_led_on()
{
 return write_gpio(LED_PIN, GPIO_ON);
}


decompiled_code:
int turn_led_off()
{
return write_gpio(0x1Bu, GPIO_OFF);
}

canonical_decompiled_code:
int func2()
{
return func0(0x1Bu, GPIO_OFF);
}

original_code:
int turn_led_off()
{
 return write_gpio(LED_PIN, GPIO_OFF);
}


insertsort

repo: nsekhar/programs

decompiled_code:
__int64 __fastcall insertsort(unsigned int *a1, int a2)
{
unsigned int *v2; // r14
__int64 v3; // r13
__int64 result; // rax
unsigned int *v5; // rbx
unsigned int *v6; // rbp

if ( a2 > 0 )
{
v2 = a1;
v3 = 0LL;
result = (unsigned int)(a2 - 2);
while ( v3 != a2 - 1 )
{
v6 = v2++;
v5 = v2;
do
{
result = *v5;
if ( (int)*v6 > (int)result )
result = swap(v6, v5);
++v5;
}
while ( v5 != &a1[v3 + 2 + (unsigned int)(a2 - 2 - v3)] );
++v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1, int a2)
{
unsigned int *v2; // r14
__int64 v3; // r13
__int64 result; // rax
unsigned int *v5; // rbx
unsigned int *v6; // rbp

if ( a2 > 0 )
{
v2 = a1;
v3 = 0LL;
result = (unsigned int)(a2 - 2);
while ( v3 != a2 - 1 )
{
v6 = v2++;
v5 = v2;
do
{
result = *v5;
if ( (int)*v6 > (int)result )
result = func8(v6, v5);
++v5;
}
while ( v5 != &a1[v3 + 2 + (unsigned int)(a2 - 2 - v3)] );
++v3;
}
}
return result;
}

original_code:
void insertsort(int a[], int n)
{
 int i, j;
 for (i = 0; i < n; i++)
  for (j = i + 1; j < n; j++)
   if (a[i] > a[j])
    swap (a + i, a + j);
}

canonical_original_code:
void insertsort(int a[], int n)
{
 int i; int j;
 for (i = 0; i < n; i++)
  for (j = i + 1; j < n; j++)
   if (a[i] > a[j])
    swap (a + i, a + j);
}

variable_types:
a
int []

variable_types: n int

variable_types: j int


swap

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

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

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

result = (unsigned int)*a1;
*a1 = *a2;
*a2 = result;
return result;
}

original_code:
void swap(int *a, int *b)
{
 int tmp;
 tmp = *a;
 *a = *b;
 *b = tmp;
}

variable_types:
a
int *

variable_types:
b
int *

variable_types: tmp int


printarray

decompiled_code:
int __fastcall printarray(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

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

if ( a2 > 0 )
{
v2 = a1;
do
printf("%d ", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return putchar(10);
}

original_code:
void printarray(int a[], int n)
{
 int i;
 for (i = 0; i < n; i++)
  printf("%d ", a[i]);
 printf("\n");
}


decompiled_code:
int start_timestamp()
{
return gettimeofday((struct timeval *)(__readfsqword(0) - 16), 0LL);
}

canonical_decompiled_code:
int func10()
{
return gettimeofday((struct timeval *)(__readfsqword(0) - 16), 0LL);
}

original_code:
void start_timestamp()
{
 gettimeofday(&tv_start, ((void *)0));
}


decompiled_code:
unsigned __int64 stop_timestamp()
{
unsigned __int64 v0; // rax
signed __int64 v1; // rdx

gettimeofday((struct timeval *)(__readfsqword(0) - 32), 0LL);
v0 = __readfsqword(0xFFFFFFE0) - __readfsqword(0xFFFFFFF0);
v1 = __readfsqword(0xFFFFFFE8) - __readfsqword(0xFFFFFFF8);
if ( v1 < 0 )
{
--v0;
v1 += 1000000LL;
}
return v1 + 1000000 * v0;
}

canonical_decompiled_code:
unsigned __int64 func11()
{
unsigned __int64 v0; // rax
signed __int64 v1; // rdx

gettimeofday((struct timeval *)(__readfsqword(0) - 32), 0LL);
v0 = __readfsqword(0xFFFFFFE0) - __readfsqword(0xFFFFFFF0);
v1 = __readfsqword(0xFFFFFFE8) - __readfsqword(0xFFFFFFF8);
if ( v1 < 0 )
{
--v0;
v1 += 1000000LL;
}
return v1 + 1000000 * v0;
}

original_code:
time_t stop_timestamp()
{
 struct timeval tv_res;
 gettimeofday(&tv_stop, ((void *)0));
 do { (&tv_res)->tv_sec = (&tv_stop)->tv_sec - (&tv_start)->tv_sec; (&tv_res)->tv_usec = (&tv_stop)->tv_usec - (&tv_start)->tv_usec; if ((&tv_res)->tv_usec < 0) { --(&tv_res)->tv_sec; (&tv_res)->tv_usec += 1000000; } } while (0);
 return tv_res.tv_sec * 1000000UL + tv_res.tv_usec;
}

canonical_original_code:
long stop_timestamp()
{
 struct timeval tv_res;
 gettimeofday(&tv_stop, ((void *)0));
 do { (&tv_res)->tv_sec = (&tv_stop)->tv_sec - (&tv_start)->tv_sec; (&tv_res)->tv_usec = (&tv_stop)->tv_usec - (&tv_start)->tv_usec; if ((&tv_res)->tv_usec < 0) { --(&tv_res)->tv_sec; (&tv_res)->tv_usec += 1000000; } } while (0);
 return tv_res.tv_sec * 1000000UL + tv_res.tv_usec;
}

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

return_type: long


repo: nsf/bmpanel

decompiled_code:
void *__fastcall impl__xmalloc(size_t a1)
{
void *v1; // rbx

v1 = malloc(a1);
if ( !v1 )
log_msg(0LL, "common: out of memory, malloc failed >:-O");
++memleaks;
return v1;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *v1; // rbx

v1 = malloc(a1);
if ( !v1 )
log_msg(0LL, "common: out of memory, malloc failed >:-O");
++memleaks;
return v1;
}

original_code:
void *impl__xmalloc(size_t size)
{
 void *ret = malloc(size);
 if (!ret)
  log_msg(0, "common: out of memory, malloc failed >:-O");
 memleaks++;
 return ret;
}

canonical_original_code:
void *impl__xmalloc(unsigned long size)
{
 void *ret = malloc(size);
 if (!ret)
  log_msg(0, "common: out of memory, malloc failed >:-O");
 memleaks++;
 return ret;
}

variable_types:
ret
void *

return_type: void *


decompiled_code:
void *__fastcall impl__xmallocz(size_t n)
{
void *v1; // rax

v1 = impl__xmalloc(n);
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func1(size_t n)
{
void *v1; // rax

v1 = func0(n);
return memset(v1, 0, n);
}

original_code:
void *impl__xmallocz(size_t size)
{
 void *ret = impl__xmalloc(size);
 memset(ret, 0, size);
 return ret;
}

canonical_original_code:
void *impl__xmallocz(unsigned long size)
{
 void *ret = impl__xmalloc(size);
 memset(ret, 0, size);
 return ret;
}


decompiled_code:
void __fastcall impl__xfree(void *a1)
{
free(a1);
--memleaks;
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
free(a1);
--memleaks;
}

original_code:
void impl__xfree(void *ptr)
{
 free(ptr);
 memleaks--;
}

variable_types:
ptr
void *


decompiled_code:
char *__fastcall impl__xstrdup(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)impl__xmalloc(v1 + 1);
return strcpy(v2, src);
}

canonical_decompiled_code:
char *__fastcall func3(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)func0(v1 + 1);
return strcpy(v2, src);
}

original_code:
char *impl__xstrdup(const char *str)
{
 size_t strl = strlen(str);
 char *ret = impl__xmalloc(strl+1);
 return strcpy(ret, str);
}

canonical_original_code:
char *impl__xstrdup(char *str)
{
 unsigned long strl = strlen(str);
 char *ret = impl__xmalloc(strl+1);
 return strcpy(ret, str);
}

variable_types:
str
char *

variable_types:
strl
unsigned long

variable_types:
ret
char *


xmemleaks

decompiled_code:
__int64 xmemleaks()
{
return log_msg(4LL, "common: memory leaks = %d", (unsigned int)memleaks);
}

canonical_decompiled_code:
__int64 func4()
{
return log_msg(4LL, "common: memory leaks = %d", (unsigned int)memleaks);
}

original_code:
void xmemleaks()
{
 log_msg(4, "common: memory leaks = %d", memleaks);
}


decompiled_code:
__int64 __fastcall send_to_all(unsigned int a1, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax

if ( log_callbacks_count > 0 )
{
v2 = 0LL;
do
result = ((__int64 (__fastcall *)(_QWORD, __int64))log_callbacks[v2++])(a1, a2);
while ( log_callbacks_count > (int)v2 );
}
return result;
}

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

if ( log_callbacks_count > 0 )
{
v2 = 0LL;
do
result = ((__int64 (__fastcall *)(_QWORD, __int64))log_callbacks[v2++])(a1, a2);
while ( log_callbacks_count > (int)v2 );
}
return result;
}

original_code:
static void send_to_all(int level, const char *msg)
{
 int i;
 for (i = 0; i < log_callbacks_count; ++i)
  (*log_callbacks[i])(level, msg);
}

canonical_original_code:
static void send_to_all(int level, char *msg)
{
 int i;
 for (i = 0; i < log_callbacks_count; ++i)
  (*log_callbacks[i])(level, msg);
}

variable_types: level int

variable_types:
msg
char *

variable_types: i int


decompiled_code:
__int64 log_msg(unsigned int a1, const char *a2, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-10D0h] BYREF
char s[4096]; // [rsp+20h] [rbp-10B8h] BYREF

if ( log_callbacks_count )
{
result = log_verbosity & (unsigned int)verbosity_map[a1];
if ( (log_verbosity & verbosity_map[a1]) != 0 )
{
va_start(arg, a2);
vsnprintf(s, 0x1000uLL, a2, arg);
result = send_to_all(a1, (__int64)s);
}
}
if ( !a1 )
exit(1);
return result;
}

canonical_decompiled_code:
__int64 func1(unsigned int a1, const char *a2, ...)
{
__int64 result; // rax
gcc_va_list arg; // [rsp+8h] [rbp-10D0h] BYREF
char s[4096]; // [rsp+20h] [rbp-10B8h] BYREF

if ( log_callbacks_count )
{
result = log_verbosity & (unsigned int)verbosity_map[a1];
if ( (log_verbosity & verbosity_map[a1]) != 0 )
{
va_start(arg, a2);
vsnprintf(s, 0x1000uLL, a2, arg);
result = func0(a1, (__int64)s);
}
}
if ( !a1 )
exit(1);
return result;
}

original_code:
void log_msg(int level, const char *msg, ...)
{
 if (log_callbacks_count) {
  char buf[4096];
  va_list vl;
  if (log_verbosity & verbosity_map[level]) {
   __builtin_va_start(vl,msg);
   vsnprintf(buf, sizeof(char)*4096, msg, vl);
   __builtin_va_end(vl);
   send_to_all(level, buf);
  }
 }
 if (level == 0)
  exit(1);
}

canonical_original_code:
void log_msg(int level, char *msg, ...)
{
 if (log_callbacks_count) {
  char buf[4096];
  __builtin_va_list vl;
  if (log_verbosity & verbosity_map[level]) {
   __builtin_va_start(vl,msg);
   vsnprintf(buf, sizeof(char)*4096, msg, vl);
   __builtin_va_end(vl);
   send_to_all(level, buf);
  }
 }
 if (level == 0)
  exit(1);
}

variable_types:
buf
char [4096]


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

result = (unsigned int)log_callbacks_count;
if ( log_callbacks_count <= 31 )
{
log_callbacks[log_callbacks_count] = a1;
result = (unsigned int)(result + 1);
log_callbacks_count = result;
}
return result;
}

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

result = (unsigned int)log_callbacks_count;
if ( log_callbacks_count <= 31 )
{
log_callbacks[log_callbacks_count] = a1;
result = (unsigned int)(result + 1);
log_callbacks_count = result;
}
return result;
}

original_code:
void log_attach_callback(void (*callback)(int, const char*))
{
 if (log_callbacks_count < 32) {
  log_callbacks[log_callbacks_count] = callback;
  log_callbacks_count++;
 }
}

canonical_original_code:
void log_attach_callback(void (*callback)(int, char *))
{
 if (log_callbacks_count < 32) {
  log_callbacks[log_callbacks_count] = callback;
  log_callbacks_count++;
 }
}

variable_types:
callback
void (*)(int, char *)


decompiled_code:
void log_clear_callbacks()
{
int v0; // edx
__int64 v1; // rax

v0 = log_callbacks_count;
if ( log_callbacks_count > 0 )
{
v1 = 0LL;
do
log_callbacks[v1++] = 0LL;
while ( v1 != v0 );
}
log_callbacks_count = 0;
}

canonical_decompiled_code:
void func3()
{
int v0; // edx
__int64 v1; // rax

v0 = log_callbacks_count;
if ( log_callbacks_count > 0 )
{
v1 = 0LL;
do
log_callbacks[v1++] = 0LL;
while ( v1 != v0 );
}
log_callbacks_count = 0;
}

original_code:
void log_clear_callbacks()
{
 int i;
 for (i = 0; i < log_callbacks_count; ++i)
  log_callbacks[i] = 0;
 log_callbacks_count = 0;
}


decompiled_code:
void __fastcall log_set_verbosity(int a1)
{
log_verbosity = a1;
}

canonical_decompiled_code:
void __fastcall func4(int a1)
{
log_verbosity = a1;
}

original_code:
void log_set_verbosity(int level)
{
 log_verbosity = level;
}


decompiled_code:
int __fastcall log_console_callback(__int64 a1, const char *a2)
{
return puts(a2);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, const char *a2)
{
return puts(a2);
}

original_code:
void log_console_callback(int level, const char *msg)
{
 printf("%s\n", msg);
}

canonical_original_code:
void log_console_callback(int level, char *msg)
{
 printf("%s\n", msg);
}