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

if ( smtp_[0] && *smtp_[0] )
{
if ( off_DE8 && *off_DE8 )
{
if ( off_E08 && *off_E08 )
{
if ( qword_E20 && *(_BYTE *)qword_E20 )
{
if ( qword_E28 && *(_QWORD *)qword_E28 )
{
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_E30 |= 1u;
}
}
else
{
dword_E30 |= 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; // rbp
int result; // eax

if ( smtp_[0] && *smtp_[0] )
{
if ( off_DE8 && *off_DE8 )
{
if ( off_E08 && *off_E08 )
{
if ( qword_E20 && *(_BYTE *)qword_E20 )
{
if ( qword_E28 && *(_QWORD *)qword_E28 )
{
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_E30 |= 1u;
}
}
else
{
dword_E30 |= 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;
}


repo: nqv/niscy

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

return_type: void


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 func3(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 *

return_type: int


decompiled_code:
int get_smtp_code()
{
__int64 v0; // rbp
int i; // ebx
int v2; // eax
char *v3; // rax
__int64 v4; // rcx

v0 = 0LL;
for ( i = 0; ; v0 = i )
{
do
v2 = smtp_read(smtp_, &buf_ + v0, (unsigned int)(1024 - i));
while ( !v2 );
if ( v2 < 0 )
break;
i += v2;
if ( *(&buf_ + i - 1) == 10 )
{
v3 = find_lastline_constprop_0(i - 1);
if ( v3[3] == 32 )
{
if ( i > 1023 )
LABEL_11:
v3 = find_lastline_constprop_0(1023);
*(&buf_ + v4) = 0;
return atoi(v3);
}
}
if ( i > 1023 )
goto LABEL_11;
}
return -1;
}

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 func6()
{
unsigned int v0; // eax
__int64 result; // rax

if ( (int)smtp_write(smtp_, "STARTTLS\r\n", 10LL) <= 0 )
{
smtp_close(smtp_);
result = 0xFFFFFFFFLL;
}
else
{
v0 = func5();
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; // er12
unsigned int v1; // eax

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

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

if ( (unsigned int)smtp_open(smtp_) )
{
v0 = 1;
fwrite("Could not connect to server.\n", 1uLL, 0x1DuLL, stderr);
}
else
{
v0 = 0;
v1 = func5();
if ( v1 - 200 > 0x63 )
{
v0 = -1;
fprintf(stderr, "Unexpected server response: %d.\n", v1);
}
}
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_E08);
if ( v1 <= 1023 && (int)smtp_write(smtp_, &buf_, (unsigned int)v1) > 0 )
{
v2 = get_smtp_code();
if ( v2 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Unexpected EHLO response: %d.\n", v2);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func8()
{
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_E08);
if ( v1 <= 1023 && (int)smtp_write(smtp_, &buf_, (unsigned int)v1) > 0 )
{
v2 = func5();
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()
{
if ( (dword_E30 & 2) != 0 )
{
dword_E30 &= 0xFFFFFFFE;
if ( !(unsigned int)probe() && !(unsigned int)ehlo() && !(unsigned int)starttls() )
{
dword_E30 |= 1u;
if ( !(unsigned int)smtp_open(smtp_) )
return 0LL;
fwrite("Could not negotiate secure connection.\n", 1uLL, 0x27uLL, stderr);
}
}
else if ( !(unsigned int)probe() && !(unsigned int)ehlo() )
{
return 0LL;
}
smtp_close(smtp_);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func9()
{
if ( (dword_E30 & 2) != 0 )
{
dword_E30 &= 0xFFFFFFFE;
if ( !(unsigned int)func7() && !(unsigned int)func8() && !(unsigned int)func6() )
{
dword_E30 |= 1u;
if ( !(unsigned int)smtp_open(smtp_) )
return 0LL;
fwrite("Could not negotiate secure connection.\n", 1uLL, 0x27uLL, stderr);
}
}
else if ( !(unsigned int)func7() && !(unsigned int)func8() )
{
return 0LL;
}
smtp_close(smtp_);
return 0xFFFFFFFFLL;
}

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_E20);
if ( v0 <= 1023 && (int)smtp_write(smtp_, &buf_, (unsigned int)v0) > 0 )
{
v1 = get_smtp_code();
if ( v1 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Invalid MAIL FROM %d.\n", v1);
}
return 0xFFFFFFFFLL;
}

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

v0 = snprintf(&buf_, 0x400uLL, "MAIL FROM:<%s>\r\n", (const char *)qword_E20);
if ( v0 <= 1023 && (int)smtp_write(smtp_, &buf_, (unsigned int)v0) > 0 )
{
v1 = func5();
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()
{
__int64 v0; // rax
const char *v1; // r12
__int64 v2; // rbx
int v3; // ebp
int v4; // eax
unsigned int v5; // eax

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

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rax
const char *v1; // r12
__int64 v2; // rbx
int v3; // ebp
int v4; // eax
unsigned int v5; // eax

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

variable_types: count int

variable_types:
addr
char *


decompiled_code:
__int64 auth_login()
{
unsigned int v0; // eax
char *v1; // rbp
size_t v2; // rax
int v3; // eax
unsigned int v4; // eax
char *v5; // rbp
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_E00;
if ( qword_E00 )
{
v6 = strlen(qword_E00);
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 func12()
{
unsigned int v0; // eax
char *v1; // rbp
size_t v2; // rax
int v3; // eax
unsigned int v4; // eax
char *v5; // rbp
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 = func5();
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 = func5();
if ( v4 - 300 > 0x63 )
{
fprintf(stderr, "Unexpected AUTH/Username response: %d.\n", v4);
return 0xFFFFFFFFLL;
}
v5 = qword_E00;
if ( qword_E00 )
{
v6 = strlen(qword_E00);
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 = func5();
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
__int64 v2; // rsi
int v3; // eax
unsigned int v4; // eax
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);
return 0xFFFFFFFFLL;
}
v1 = qword_E00;
if ( !qword_E00 )
v1 = (const char *)&unk_151D;
v2 = (unsigned int)snprintf(s, 0x200uLL, "%s%c%s%c%s", ::s, 0LL, ::s, 0LL, v1);
if ( (int)v2 > 511 )
return 0xFFFFFFFFLL;
v3 = base64_encode(s, v2, &buf_, 1022LL);
if ( v3 < 0 )
{
fwrite("Could not encode authentication.\n", 1uLL, 0x21uLL, stderr);
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 - 200 <= 0x63 )
return 0LL;
fprintf(stderr, "Authentication failed %d.\n", v4);
return 0xFFFFFFFFLL;
}

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

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

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()
{
const char *v0; // r12

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

canonical_decompiled_code:
__int64 func14()
{
const char *v0; // r12

if ( !s )
return 0LL;
v0 = qword_E10;
if ( !qword_E10 || !strcasecmp(qword_E10, "login") )
return func12();
if ( !strcasecmp(v0, "plain") )
return func13();
fprintf(stderr, "Authentication method is not supported: %s.\n", v0);
return 0xFFFFFFFFLL;
}

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:
__int64 mail_data()
{
unsigned int v0; // eax
int v1; // eax
int v2; // ebx

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

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

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

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()
{
__int64 result; // rax

if ( (unsigned int)mail_from() || (unsigned int)rcpt_to() || (unsigned int)mail_data() )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)mail_stream(stdin) != 0);
return result;
}

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

if ( (unsigned int)func10() || (unsigned int)func11() || (unsigned int)func15() )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)func16(stdin) != 0);
return result;
}

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; // er12

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

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

func0(argc, (__int64)argv);
if ( !func1() && !(unsigned int)func9() )
{
if ( !(unsigned int)func14() )
{
v3 = func17();
if ( !v3 )
{
func2();
return v3;
}
}
func2();
}
return 1;
}

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

variable_types: argc int

variable_types:
argv
char **


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:
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:
// attributes: thunk
__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; // ebp
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; // ebp
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; // r12
__int64 v3; // rbp
__int64 result; // rax
unsigned int *v5; // r13
unsigned int *v6; // rbx
__int64 v7; // r15
unsigned int *v8; // rsi
__int64 v9; // [rsp+0h] [rbp-48h]
int v10; // [rsp+Ch] [rbp-3Ch]

if ( a2 > 0 )
{
v2 = a1;
v3 = 0LL;
v9 = (unsigned int)(a2 - 1);
result = (unsigned int)(a2 - 2);
v10 = a2 - 2;
if ( a2 != 1 )
{
do
{
v5 = v2++;
v6 = v2;
v7 = (__int64)&a1[v3 + 2 + (unsigned int)(v10 - v3)];
do
{
while ( 1 )
{
result = *v6;
if ( (int)*v5 > (int)result )
break;
if ( ++v6 == (unsigned int *)v7 )
goto LABEL_7;
}
v8 = v6++;
result = swap(v5, v8);
}
while ( v6 != (unsigned int *)v7 );
LABEL_7:
++v3;
}
while ( v3 != v9 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1, int a2)
{
unsigned int *v2; // r12
__int64 v3; // rbp
__int64 result; // rax
unsigned int *v5; // r13
unsigned int *v6; // rbx
__int64 v7; // r15
unsigned int *v8; // rsi
__int64 v9; // [rsp+0h] [rbp-48h]
int v10; // [rsp+Ch] [rbp-3Ch]

if ( a2 > 0 )
{
v2 = a1;
v3 = 0LL;
v9 = (unsigned int)(a2 - 1);
result = (unsigned int)(a2 - 2);
v10 = a2 - 2;
if ( a2 != 1 )
{
do
{
v5 = v2++;
v6 = v2;
v7 = (__int64)&a1[v3 + 2 + (unsigned int)(v10 - v3)];
do
{
while ( 1 )
{
result = *v6;
if ( (int)*v5 > (int)result )
break;
if ( ++v6 == (unsigned int *)v7 )
goto LABEL_7;
}
v8 = v6++;
result = func8(v5, v8);
}
while ( v6 != (unsigned int *)v7 );
LABEL_7:
++v3;
}
while ( v3 != v9 );
}
}
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: i 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)
{
__int64 v2; // rbp
unsigned int *v3; // rbx
__int64 v4; // rsi

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

canonical_decompiled_code:
int __fastcall func9(unsigned int *a1, int a2)
{
__int64 v2; // rbp
unsigned int *v3; // rbx
__int64 v4; // rsi

if ( a2 > 0 )
{
v2 = (__int64)&a1[a2 - 1 + 1];
v3 = a1;
do
{
v4 = *v3++;
printf("%d ", v4);
}
while ( v3 != (unsigned int *)v2 );
}
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; // r12
void *result; // rax

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

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *v1; // r12
void *result; // rax

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

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 *

return_type: 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 result; // rax
__int64 v3; // rbx

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

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

result = (unsigned int)log_callbacks_count;
if ( log_callbacks_count > 0 )
{
v3 = 0LL;
do
result = ((__int64 (__fastcall *)(_QWORD, __int64))log_callbacks[v3++])(a1, a2);
while ( log_callbacks_count > (int)v3 );
}
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 *


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

result = (unsigned int)log_callbacks_count;
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

result = (unsigned int)log_callbacks_count;
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 )
{
result = (unsigned int)(log_callbacks_count + 1);
log_callbacks[log_callbacks_count] = a1;
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 )
{
result = (unsigned int)(log_callbacks_count + 1);
log_callbacks[log_callbacks_count] = a1;
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()
{
void *result; // rax

if ( log_callbacks_count > 0 )
result = memset(log_callbacks, 0, 8LL * log_callbacks_count);
log_callbacks_count = 0;
return result;
}

canonical_decompiled_code:
void *func3()
{
void *result; // rax

if ( log_callbacks_count > 0 )
result = memset(log_callbacks, 0, 8LL * log_callbacks_count);
log_callbacks_count = 0;
return result;
}

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


decompiled_code:
int __fastcall log_console_color_callback(int a1, const char *a2)
{
__int64 v2; // rdx

v2 = (unsigned int)byte_4A4[8 * a1];
printf(
"\x1B[1m\x1B[30m[ \x1B[3%dm%c%c\x1B[0m \x1B[1m\x1B[30m]\x1B[0m ",
(unsigned int)log_console_color_params_1[2 * a1],
v2,
(unsigned int)v2);
return puts(a2);
}

canonical_decompiled_code:
int __fastcall func6(int a1, const char *a2)
{
__int64 v2; // rdx

v2 = (unsigned int)byte_4A4[8 * a1];
printf(
"\x1B[1m\x1B[30m[ \x1B[3%dm%c%c\x1B[0m \x1B[1m\x1B[30m]\x1B[0m ",
(unsigned int)log_console_color_params_1[2 * a1],
v2,
(unsigned int)v2);
return puts(a2);
}

original_code:
void log_console_color_callback(int level, const char *msg)
{
 static const struct {
  int color;
  char ch;
 } log_console_color_params[] = {
  {1, '!'},
  {3, '!'},
  {2, '+'},
  {6, '-'},
  {5, '*'}
 };
 printf("\033[1m\033[30m[ \033[3%dm%c%c\033[0m \033[1m\033[30m]\033[0m ",
   log_console_color_params[level].color,
   log_console_color_params[level].ch,
   log_console_color_params[level].ch);
 printf("%s\n", msg);
}

canonical_original_code:
void log_console_color_callback(int level, char *msg)
{
 static struct { int color; char ch; } log_console_color_params[] = {
  {1, '!'},
  {3, '!'},
  {2, '+'},
  {6, '-'},
  {5, '*'}
 };
 printf("\033[1m\033[30m[ \033[3%dm%c%c\033[0m \033[1m\033[30m]\033[0m ",
   log_console_color_params[level].color,
   log_console_color_params[level].ch,
   log_console_color_params[level].ch);
 printf("%s\n", msg);
}


createDeque

decompiled_code:
_DWORD *createDeque()
{
_DWORD *v0; // rax
_DWORD *v1; // r12
_QWORD *v2; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
__assert_fail("dp != NULL", "deque.c", 0x19u, "createDeque");
*v0 = 0;
v1 = v0;
v2 = malloc(0x18uLL);
*((_QWORD *)v1 + 1) = v2;
if ( !v2 )
__assert_fail("dp->head != NULL", "deque.c", 0x1Du, "createDeque");
v2[1] = v2;
v2[2] = v2;
return v1;
}

canonical_decompiled_code:
_DWORD *func7()
{
_DWORD *v0; // rax
_DWORD *v1; // r12
_QWORD *v2; // rax

v0 = malloc(0x10uLL);
if ( !v0 )
__assert_fail("dp != NULL", "deque.c", 0x19u, "createDeque");
*v0 = 0;
v1 = v0;
v2 = malloc(0x18uLL);
*((_QWORD *)v1 + 1) = v2;
if ( !v2 )
__assert_fail("dp->head != NULL", "deque.c", 0x1Du, "createDeque");
v2[1] = v2;
v2[2] = v2;
return v1;
}

original_code:
struct deque *createDeque(void)
{
    struct deque *dp;
    dp = malloc(sizeof(struct deque));
    ((void) sizeof ((dp != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp != ((void *)0)) ; else __assert_fail ("dp != NULL", "deque.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    dp->count = 0;
    dp->head = malloc(sizeof(struct node));
    ((void) sizeof ((dp->head != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp->head != ((void *)0)) ; else __assert_fail ("dp->head != NULL", "deque.c", 29, __extension__ __PRETTY_FUNCTION__); }));
    dp->head->next = dp->head;
    dp->head->prev = dp->head;
    return dp;
}

canonical_original_code:
struct deque *createDeque()
{
    struct deque *dp;
    dp = malloc(sizeof(struct deque));
    ((void) sizeof ((dp != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp != ((void *)0)) ; else __assert_fail ("dp != NULL", "deque.c", 25, __extension__ __PRETTY_FUNCTION__); }));
    dp->count = 0;
    dp->head = malloc(sizeof(struct node));
    ((void) sizeof ((dp->head != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp->head != ((void *)0)) ; else __assert_fail ("dp->head != NULL", "deque.c", 29, __extension__ __PRETTY_FUNCTION__); }));
    dp->head->next = dp->head;
    dp->head->prev = dp->head;
    return dp;
}

variable_types: struct deque { int count; struct node { int data; struct node *next; struct node *prev; } *head; } *

user_defined_types: struct deque { int count; struct node *head; }


destroyDeque

decompiled_code:
void __fastcall destroyDeque(void *ptr)
{
_QWORD *v1; // rbx
void *v3; // rdi

if ( !ptr )
__assert_fail("dp != NULL", "deque.c", 0x2Bu, "destroyDeque");
v1 = (_QWORD *)*((_QWORD *)ptr + 1);
do
{
v3 = v1;
v1 = (_QWORD *)v1[1];
free(v3);
}
while ( *((_QWORD **)ptr + 1) != v1 );
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
_QWORD *v1; // rbx
void *v3; // rdi

if ( !ptr )
__assert_fail("dp != NULL", "deque.c", 0x2Bu, "destroyDeque");
v1 = (_QWORD *)*((_QWORD *)ptr + 1);
do
{
v3 = v1;
v1 = (_QWORD *)v1[1];
free(v3);
}
while ( *((_QWORD **)ptr + 1) != v1 );
free(ptr);
}

original_code:
void destroyDeque(struct deque *dp)
{
    struct node *np, *next;
    ((void) sizeof ((dp != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp != ((void *)0)) ; else __assert_fail ("dp != NULL", "deque.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    np = dp->head;
    do {
 next = np->next;
 free(np);
 np = next;
    } while (np != dp->head);
    free(dp);
}

canonical_original_code:
void destroyDeque(struct deque *dp)
{
    struct node *np; struct node *next;
    ((void) sizeof ((dp != ((void *)0)) ? 1 : 0), __extension__ ({ if (dp != ((void *)0)) ; else __assert_fail ("dp != NULL", "deque.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    np = dp->head;
    do {
 next = np->next;
 free(np);
 np = next;
    } while (np != dp->head);
    free(dp);
}

variable_types: struct node { int data; struct node *next; struct node *prev; } *


removeFirst

decompiled_code:
__int64 __fastcall removeFirst(__int64 a1)
{
__int64 v1; // rdx
_QWORD *v2; // rdi
unsigned int v3; // er12

if ( !a1 || *(int *)a1 <= 0 )
__assert_fail("dp != NULL && dp->count > 0", "deque.c", 0x68u, "removeFirst");
v1 = *(_QWORD *)(a1 + 8);
--*(_DWORD *)a1;
v2 = *(_QWORD **)(v1 + 8);
v3 = *(_DWORD *)v2;
*(_QWORD *)(v1 + 8) = v2[1];
*(_QWORD *)(v2[1] + 16LL) = v1;
free(v2);
return v3;
}

original_code:
int removeFirst(struct deque *dp)
{
    int x;
    struct node *np, *sentinel;
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    sentinel = dp->head;
    dp->count --;
    np = sentinel->next;
    sentinel->next = np->next;
    np->next->prev = sentinel;
    x = np->data;
    free(np);
    return x;
}

canonical_original_code:
int removeFirst(struct deque *dp)
{
    int x;
    struct node *np; struct node *sentinel;
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    sentinel = dp->head;
    dp->count --;
    np = sentinel->next;
    sentinel->next = np->next;
    np->next->prev = sentinel;
    x = np->data;
    free(np);
    return x;
}

variable_types: x int

user_defined_types: struct node { int data; struct node *next; struct node *prev; }


removeLast

original_code:
int removeLast(struct deque *dp)
{
    int x;
    struct node *np, *sentinel;
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 124, __extension__ __PRETTY_FUNCTION__); }));
    sentinel = dp->head;
    dp->count --;
    np = sentinel->prev;
    sentinel->prev = np->prev;
    np->prev->next = sentinel;
    x = np->data;
    free(np);
    return x;
}

canonical_original_code:
int removeLast(struct deque *dp)
{
    int x;
    struct node *np; struct node *sentinel;
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 124, __extension__ __PRETTY_FUNCTION__); }));
    sentinel = dp->head;
    dp->count --;
    np = sentinel->prev;
    sentinel->prev = np->prev;
    np->prev->next = sentinel;
    x = np->data;
    free(np);
    return x;
}


getFirst

decompiled_code:
__int64 __fastcall getFirst(__int64 a1)
{
if ( !a1 || *(int *)a1 <= 0 )
__assert_fail("dp != NULL && dp->count > 0", "deque.c", 0x8Cu, "getFirst");
return **(unsigned int **)(*(_QWORD *)(a1 + 8) + 8LL);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
if ( !a1 || *(int *)a1 <= 0 )
__assert_fail("dp != NULL && dp->count > 0", "deque.c", 0x8Cu, "getFirst");
return **(unsigned int **)(*(_QWORD *)(a1 + 8) + 8LL);
}

original_code:
int getFirst(struct deque *dp)
{
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 140, __extension__ __PRETTY_FUNCTION__); }));
    return dp->head->next->data;
}


getLast

decompiled_code:
__int64 __fastcall getLast(__int64 a1)
{
if ( !a1 || *(int *)a1 <= 0 )
__assert_fail("dp != NULL && dp->count > 0", "deque.c", 0x93u, "getLast");
return **(unsigned int **)(*(_QWORD *)(a1 + 8) + 16LL);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
if ( !a1 || *(int *)a1 <= 0 )
__assert_fail("dp != NULL && dp->count > 0", "deque.c", 0x93u, "getLast");
return **(unsigned int **)(*(_QWORD *)(a1 + 8) + 16LL);
}

original_code:
int getLast(struct deque *dp)
{
    ((void) sizeof ((dp != ((void *)0) && dp->count > 0) ? 1 : 0), __extension__ ({ if (dp != ((void *)0) && dp->count > 0) ; else __assert_fail ("dp != NULL && dp->count > 0", "deque.c", 147, __extension__ __PRETTY_FUNCTION__); }));
    return dp->head->prev->data;
}


numItems

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

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

original_code:
int numItems(struct deque *dp)
{
    return dp->count;
}


initMaze

original_code:
static void initMaze(void)
{
    int x, y;
    for (y = 0; y < height; y ++)
 for (x = 0; x < width; x ++) {
     maze[y][x].visited = 0;
     maze[y][x].bottom = 1;
     maze[y][x].right = 1;
 }
}

canonical_original_code:
static void initMaze()
{
    int x; int y;
    for (y = 0; y < height; y ++)
 for (x = 0; x < width; x ++) {
     maze[y][x].visited = 0;
     maze[y][x].bottom = 1;
     maze[y][x].right = 1;
 }
}

variable_types: y int


createMaze

decompiled_code:
void createMaze()
{
int v0; // er12
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // r12
size_t v4; // rbp
void *v5; // rax
unsigned int v6; // eax

v0 = height;
v1 = malloc(8LL * height);
maze = (__int64)v1;
if ( !v1 )
_assert_fail("maze != NULL", "maze.c", 0x6Au, "createMaze");
if ( v0 > 0 )
{
v2 = v1;
v3 = &v1[v0];
v4 = 8LL * width;
do
{
v5 = malloc(v4);
*v2 = v5;
if ( !v5 )
_assert_fail("maze[y] != NULL", "maze.c", 0x6Eu, "createMaze");
++v2;
}
while ( v2 != v3 );
}
v6 = time(0LL);
srand(v6);
}

canonical_decompiled_code:
void func1()
{
int v0; // er12
_QWORD *v1; // rax
_QWORD *v2; // rbx
_QWORD *v3; // r12
size_t v4; // rbp
void *v5; // rax
unsigned int v6; // eax

v0 = height;
v1 = malloc(8LL * height);
maze = (__int64)v1;
if ( !v1 )
_assert_fail("maze != NULL", "maze.c", 0x6Au, "createMaze");
if ( v0 > 0 )
{
v2 = v1;
v3 = &v1[v0];
v4 = 8LL * width;
do
{
v5 = malloc(v4);
*v2 = v5;
if ( !v5 )
_assert_fail("maze[y] != NULL", "maze.c", 0x6Eu, "createMaze");
++v2;
}
while ( v2 != v3 );
}
v6 = time(0LL);
srand(v6);
}

original_code:
static void createMaze(void)
{
    int y;
    maze = malloc(sizeof(CELL *) * height);
    ((void) sizeof ((maze != ((void *)0)) ? 1 : 0), __extension__ ({ if (maze != ((void *)0)) ; else __assert_fail ("maze != NULL", "maze.c", 106, __extension__ __PRETTY_FUNCTION__); }));
    for (y = 0; y < height; y ++) {
 maze[y] = malloc(sizeof(CELL) * width);
 ((void) sizeof ((maze[y] != ((void *)0)) ? 1 : 0), __extension__ ({ if (maze[y] != ((void *)0)) ; else __assert_fail ("maze[y] != NULL", "maze.c", 110, __extension__ __PRETTY_FUNCTION__); }));
    }
    srand(time(((void *)0)));
}

canonical_original_code:
static void createMaze()
{
    int y;
    maze = malloc(sizeof(struct cell *) * height);
    ((void) sizeof ((maze != ((void *)0)) ? 1 : 0), __extension__ ({ if (maze != ((void *)0)) ; else __assert_fail ("maze != NULL", "maze.c", 106, __extension__ __PRETTY_FUNCTION__); }));
    for (y = 0; y < height; y ++) {
 maze[y] = malloc(sizeof(CELL) * width);
 ((void) sizeof ((maze[y] != ((void *)0)) ? 1 : 0), __extension__ ({ if (maze[y] != ((void *)0)) ; else __assert_fail ("maze[y] != NULL", "maze.c", 110, __extension__ __PRETTY_FUNCTION__); }));
    }
    srand(time(((void *)0)));
}


buildMaze

original_code:
static void buildMaze(int y, int x)
{
    int numOffsets, offset, offsets[4];
    while (1) {
 numOffsets = 0;
 maze[y][x].visited = 1;
 if (y > 0 && !maze[y - 1][x].visited)
     offsets[numOffsets ++] = -width;
 if (y < height - 1 && !maze[y + 1][x].visited)
     offsets[numOffsets ++] = width;
 if (x > 0 && !maze[y][x - 1].visited)
     offsets[numOffsets ++] = -1;
 if (x < width - 1 && !maze[y][x + 1].visited)
     offsets[numOffsets ++] = 1;
 if (numOffsets > 0) {
     offset = offsets[rand() % numOffsets];
     addFirst(dp, ((y) * width + (x)));
     if (offset == -width) {
  maze[y - 1][x].bottom = 0;
  buildMaze(y - 1, x);
     } else if (offset == width) {
  maze[y][x].bottom = 0;
  buildMaze(y + 1, x);
     } else if (offset == -1) {
  maze[y][x - 1].right = 0;
  buildMaze(y, x - 1);
     } else if (offset == 1) {
  maze[y][x].right = 0;
  buildMaze(y, x + 1);
     } else
  abort();
 } else if (numItems(dp) > 0) {
     offset = removeFirst(dp);
     x = ((offset) % width);
     y = ((offset) / width);
 } else
     break;
    }
    maze[height - 1][width - 1].right = 0;
}

canonical_original_code:
static void buildMaze(int y, int x)
{
    int numOffsets; int offset; int offsets[4];
    while (1) {
 numOffsets = 0;
 maze[y][x].visited = 1;
 if (y > 0 && !maze[y - 1][x].visited)
     offsets[numOffsets ++] = -width;
 if (y < height - 1 && !maze[y + 1][x].visited)
     offsets[numOffsets ++] = width;
 if (x > 0 && !maze[y][x - 1].visited)
     offsets[numOffsets ++] = -1;
 if (x < width - 1 && !maze[y][x + 1].visited)
     offsets[numOffsets ++] = 1;
 if (numOffsets > 0) {
     offset = offsets[rand() % numOffsets];
     addFirst(dp, ((y) * width + (x)));
     if (offset == -width) {
  maze[y - 1][x].bottom = 0;
  buildMaze(y - 1, x);
     } else if (offset == width) {
  maze[y][x].bottom = 0;
  buildMaze(y + 1, x);
     } else if (offset == -1) {
  maze[y][x - 1].right = 0;
  buildMaze(y, x - 1);
     } else if (offset == 1) {
  maze[y][x].right = 0;
  buildMaze(y, x + 1);
     } else
  abort();
 } else if (numItems(dp) > 0) {
     offset = removeFirst(dp);
     x = ((offset) % width);
     y = ((offset) / width);
 } else
     break;
    }
    maze[height - 1][width - 1].right = 0;
}

variable_types: numOffsets int

variable_types: offset int

variable_types:
offsets
int [4]


printMaze

decompiled_code:
int printMaze()
{
__int64 v0; // rbx
__int64 v1; // rbx
chtype v2; // rsi
__int64 v3; // rbp
chtype v4; // rsi
int v5; // edx
__int64 v6; // rax
__int64 v7; // rcx
int v8; // er13
__int64 v9; // rbp
__int64 v10; // r14
int v11; // er12
__int64 v12; // rax
_BOOL4 v13; // er15
chtype v14; // rsi
__int64 v15; // r14
int v17; // [rsp+4h] [rbp-44h]

v0 = 0LL;
waddch(stdscr, 0x20uLL);
if ( width > 0 )
{
while ( 1 )
{
waddch(stdscr, HIDWORD(acs_map[56]));
if ( width - 1 == (_DWORD)v0 )
break;
if ( *(_BYTE *)(*(_QWORD *)maze + 8 * v0 + 5) )
{
waddch(stdscr, HIDWORD(acs_map[59]));
LABEL_4:
if ( width <= (int)++v0 )
goto LABEL_8;
}
else
{
++v0;
waddch(stdscr, HIDWORD(acs_map[56]));
if ( width <= (int)v0 )
goto LABEL_8;
}
}
waddch(stdscr, HIDWORD(acs_map[53]));
goto LABEL_4;
}
LABEL_8:
v1 = 0LL;
waddch(stdscr, 0xAuLL);
v2 = 32LL;
v17 = 0;
if ( height > 0 )
{
while ( 1 )
{
v3 = 0LL;
waddch(stdscr, v2);
if ( width > 0 )
{
do
{
waddch(stdscr, 0x20uLL);
v4 = 32LL;
if ( *(_BYTE *)(*(_QWORD *)(maze + v1) + 8 * v3 + 5) )
v4 = LODWORD(acs_map[60]);
++v3;
waddch(stdscr, v4);
}
while ( width > (int)v3 );
}
waddch(stdscr, 0xAuLL);
if ( height - 1 == v17 )
{
waddch(stdscr, HIDWORD(acs_map[54]));
}
else if ( *(_BYTE *)(*(_QWORD *)(maze + v1) + 4LL) )
{
waddch(stdscr, LODWORD(acs_map[58]));
}
else
{
waddch(stdscr, LODWORD(acs_map[60]));
}
v5 = width;
v6 = 0LL;
if ( width > 0 )
break;
LABEL_37:
waddch(stdscr, 0xAuLL);
++v17;
v1 += 8LL;
if ( height <= v17 )
return wrefresh(stdscr);
v2 = LODWORD(acs_map[60]);
}
while ( 1 )
{
v7 = *(_QWORD *)(maze + v1);
if ( v5 - 1 == (_DWORD)v6 )
v8 = 0;
else
v8 = *(unsigned __int8 *)(v7 + 8 * v6 + 12);
v9 = v6 + 1;
v10 = 8 * v6;
if ( height - 1 == v17 )
{
v11 = 0;
v13 = 0;
}
else
{
v11 = 0;
v12 = *(_QWORD *)(maze + v1 + 8);
if ( *(_BYTE *)(v12 + v10 + 5) )
v11 = v8;
v13 = *(_BYTE *)(v12 + v10 + 5) != 0;
}
v14 = 32LL;
if ( *(_BYTE *)(v7 + v10 + 4) )
v14 = HIDWORD(acs_map[56]);
waddch(stdscr, v14);
v15 = *(_QWORD *)(maze + v1) + v10;
if ( *(_BYTE *)(v15 + 4) )
{
if ( *(_BYTE *)(v15 + 5) )
{
if ( v11 )
{
waddch(stdscr, LODWORD(acs_map[55]));
}
else if ( v8 )
{
waddch(stdscr, LODWORD(acs_map[59]));
}
else if ( v13 )
{
waddch(stdscr, HIDWORD(acs_map[58]));
}
else
{
waddch(stdscr, LODWORD(acs_map[53]));
}
}
else if ( v11 )
{
waddch(stdscr, HIDWORD(acs_map[59]));
}
else if ( v13 )
{
waddch(stdscr, HIDWORD(acs_map[53]));
}
else
{
waddch(stdscr, HIDWORD(acs_map[56]));
}
goto LABEL_23;
}
if ( !*(_BYTE *)(v15 + 5) )
{
if ( v11 )
waddch(stdscr, LODWORD(acs_map[54]));
else
waddch(stdscr, 0x20uLL);
goto LABEL_23;
}
if ( v11 )
break;
if ( v8 )
{
waddch(stdscr, HIDWORD(acs_map[54]));
v5 = width;
v6 = v9;
if ( width <= (int)v9 )
goto LABEL_37;
}
else
{
waddch(stdscr, LODWORD(acs_map[60]));
LABEL_23:
v5 = width;
v6 = v9;
if ( width <= (int)v9 )
goto LABEL_37;
}
}
waddch(stdscr, LODWORD(acs_map[58]));
goto LABEL_23;
}
return wrefresh(stdscr);
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rbx
__int64 v1; // rbx
chtype v2; // rsi
__int64 v3; // rbp
chtype v4; // rsi
int v5; // edx
__int64 v6; // rax
__int64 v7; // rcx
int v8; // er13
__int64 v9; // rbp
__int64 v10; // r14
int v11; // er12
__int64 v12; // rax
_BOOL4 v13; // er15
chtype v14; // rsi
__int64 v15; // r14
int v17; // [rsp+4h] [rbp-44h]

v0 = 0LL;
waddch(stdscr, 0x20uLL);
if ( width > 0 )
{
while ( 1 )
{
waddch(stdscr, HIDWORD(acs_map[56]));
if ( width - 1 == (_DWORD)v0 )
break;
if ( *(_BYTE *)(*(_QWORD *)maze + 8 * v0 + 5) )
{
waddch(stdscr, HIDWORD(acs_map[59]));
LABEL_4:
if ( width <= (int)++v0 )
goto LABEL_8;
}
else
{
++v0;
waddch(stdscr, HIDWORD(acs_map[56]));
if ( width <= (int)v0 )
goto LABEL_8;
}
}
waddch(stdscr, HIDWORD(acs_map[53]));
goto LABEL_4;
}
LABEL_8:
v1 = 0LL;
waddch(stdscr, 0xAuLL);
v2 = 32LL;
v17 = 0;
if ( height > 0 )
{
while ( 1 )
{
v3 = 0LL;
waddch(stdscr, v2);
if ( width > 0 )
{
do
{
waddch(stdscr, 0x20uLL);
v4 = 32LL;
if ( *(_BYTE *)(*(_QWORD *)(maze + v1) + 8 * v3 + 5) )
v4 = LODWORD(acs_map[60]);
++v3;
waddch(stdscr, v4);
}
while ( width > (int)v3 );
}
waddch(stdscr, 0xAuLL);
if ( height - 1 == v17 )
{
waddch(stdscr, HIDWORD(acs_map[54]));
}
else if ( *(_BYTE *)(*(_QWORD *)(maze + v1) + 4LL) )
{
waddch(stdscr, LODWORD(acs_map[58]));
}
else
{
waddch(stdscr, LODWORD(acs_map[60]));
}
v5 = width;
v6 = 0LL;
if ( width > 0 )
break;
LABEL_37:
waddch(stdscr, 0xAuLL);
++v17;
v1 += 8LL;
if ( height <= v17 )
return wrefresh(stdscr);
v2 = LODWORD(acs_map[60]);
}
while ( 1 )
{
v7 = *(_QWORD *)(maze + v1);
if ( v5 - 1 == (_DWORD)v6 )
v8 = 0;
else
v8 = *(unsigned __int8 *)(v7 + 8 * v6 + 12);
v9 = v6 + 1;
v10 = 8 * v6;
if ( height - 1 == v17 )
{
v11 = 0;
v13 = 0;
}
else
{
v11 = 0;
v12 = *(_QWORD *)(maze + v1 + 8);
if ( *(_BYTE *)(v12 + v10 + 5) )
v11 = v8;
v13 = *(_BYTE *)(v12 + v10 + 5) != 0;
}
v14 = 32LL;
if ( *(_BYTE *)(v7 + v10 + 4) )
v14 = HIDWORD(acs_map[56]);
waddch(stdscr, v14);
v15 = *(_QWORD *)(maze + v1) + v10;
if ( *(_BYTE *)(v15 + 4) )
{
if ( *(_BYTE *)(v15 + 5) )
{
if ( v11 )
{
waddch(stdscr, LODWORD(acs_map[55]));
}
else if ( v8 )
{
waddch(stdscr, LODWORD(acs_map[59]));
}
else if ( v13 )
{
waddch(stdscr, HIDWORD(acs_map[58]));
}
else
{
waddch(stdscr, LODWORD(acs_map[53]));
}
}
else if ( v11 )
{
waddch(stdscr, HIDWORD(acs_map[59]));
}
else if ( v13 )
{
waddch(stdscr, HIDWORD(acs_map[53]));
}
else
{
waddch(stdscr, HIDWORD(acs_map[56]));
}
goto LABEL_23;
}
if ( !*(_BYTE *)(v15 + 5) )
{
if ( v11 )
waddch(stdscr, LODWORD(acs_map[54]));
else
waddch(stdscr, 0x20uLL);
goto LABEL_23;
}
if ( v11 )
break;
if ( v8 )
{
waddch(stdscr, HIDWORD(acs_map[54]));
v5 = width;
v6 = v9;
if ( width <= (int)v9 )
goto LABEL_37;
}
else
{
waddch(stdscr, LODWORD(acs_map[60]));
LABEL_23:
v5 = width;
v6 = v9;
if ( width <= (int)v9 )
goto LABEL_37;
}
}
waddch(stdscr, LODWORD(acs_map[58]));
goto LABEL_23;
}
return wrefresh(stdscr);
}

original_code:
static void printMaze(void)
{
    int x, y;
    _Bool eastBottom, southRight;
    waddch(stdscr,(' '));
    for (x = 0; x < width; x ++) {
 waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
 if (x == width - 1)
     waddch(stdscr,((acs_map[(unsigned char)(('k'))])));
 else if (maze[0][x].right)
     waddch(stdscr,((acs_map[(unsigned char)(('w'))])));
 else
     waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
    }
    waddch(stdscr,('\n'));
    for (y = 0; y < height; y ++) {
 waddch(stdscr,(y != 0 ? (acs_map[(unsigned char)(('x'))]) : ' '));
 for (x = 0; x < width; x ++) {
     waddch(stdscr,(' '));
     waddch(stdscr,(maze[y][x].right ? (acs_map[(unsigned char)(('x'))]) : ' '));
 }
 waddch(stdscr,('\n'));
 if (y == height - 1)
     waddch(stdscr,((acs_map[(unsigned char)(('m'))])));
 else if (maze[y][0].bottom)
     waddch(stdscr,((acs_map[(unsigned char)(('t'))])));
 else
     waddch(stdscr,((acs_map[(unsigned char)(('x'))])));
 for (x = 0; x < width; x ++) {
     eastBottom = (x != width - 1 && maze[y][x + 1].bottom);
     southRight = (y != height - 1 && maze[y + 1][x].right);
     waddch(stdscr,(maze[y][x].bottom ? (acs_map[(unsigned char)(('q'))]) : ' '));
     if (maze[y][x].bottom) {
  if (maze[y][x].right) {
      if (eastBottom && southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('n'))])));
      else if (eastBottom)
   waddch(stdscr,((acs_map[(unsigned char)(('v'))])));
      else if (southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('u'))])));
      else
   waddch(stdscr,((acs_map[(unsigned char)(('j'))])));
  } else {
      if (eastBottom && southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('w'))])));
      else if (southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('k'))])));
      else
   waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
  }
     } else if (maze[y][x].right) {
  if (eastBottom && southRight)
      waddch(stdscr,((acs_map[(unsigned char)(('t'))])));
  else if (eastBottom)
      waddch(stdscr,((acs_map[(unsigned char)(('m'))])));
  else
      waddch(stdscr,((acs_map[(unsigned char)(('x'))])));
     } else {
  if (eastBottom && southRight)
      waddch(stdscr,((acs_map[(unsigned char)(('l'))])));
  else
      waddch(stdscr,(' '));
     }
 }
 waddch(stdscr,('\n'));
    }
    wrefresh(stdscr);
}

canonical_original_code:
static void printMaze()
{
    int x; int y;
    _Bool eastBottom; _Bool southRight;
    waddch(stdscr,(' '));
    for (x = 0; x < width; x ++) {
 waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
 if (x == width - 1)
     waddch(stdscr,((acs_map[(unsigned char)(('k'))])));
 else if (maze[0][x].right)
     waddch(stdscr,((acs_map[(unsigned char)(('w'))])));
 else
     waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
    }
    waddch(stdscr,('\n'));
    for (y = 0; y < height; y ++) {
 waddch(stdscr,(y != 0 ? (acs_map[(unsigned char)(('x'))]) : ' '));
 for (x = 0; x < width; x ++) {
     waddch(stdscr,(' '));
     waddch(stdscr,(maze[y][x].right ? (acs_map[(unsigned char)(('x'))]) : ' '));
 }
 waddch(stdscr,('\n'));
 if (y == height - 1)
     waddch(stdscr,((acs_map[(unsigned char)(('m'))])));
 else if (maze[y][0].bottom)
     waddch(stdscr,((acs_map[(unsigned char)(('t'))])));
 else
     waddch(stdscr,((acs_map[(unsigned char)(('x'))])));
 for (x = 0; x < width; x ++) {
     eastBottom = (x != width - 1 && maze[y][x + 1].bottom);
     southRight = (y != height - 1 && maze[y + 1][x].right);
     waddch(stdscr,(maze[y][x].bottom ? (acs_map[(unsigned char)(('q'))]) : ' '));
     if (maze[y][x].bottom) {
  if (maze[y][x].right) {
      if (eastBottom && southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('n'))])));
      else if (eastBottom)
   waddch(stdscr,((acs_map[(unsigned char)(('v'))])));
      else if (southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('u'))])));
      else
   waddch(stdscr,((acs_map[(unsigned char)(('j'))])));
  } else {
      if (eastBottom && southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('w'))])));
      else if (southRight)
   waddch(stdscr,((acs_map[(unsigned char)(('k'))])));
      else
   waddch(stdscr,((acs_map[(unsigned char)(('q'))])));
  }
     } else if (maze[y][x].right) {
  if (eastBottom && southRight)
      waddch(stdscr,((acs_map[(unsigned char)(('t'))])));
  else if (eastBottom)
      waddch(stdscr,((acs_map[(unsigned char)(('m'))])));
  else
      waddch(stdscr,((acs_map[(unsigned char)(('x'))])));
     } else {
  if (eastBottom && southRight)
      waddch(stdscr,((acs_map[(unsigned char)(('l'))])));
  else
      waddch(stdscr,(' '));
     }
 }
 waddch(stdscr,('\n'));
    }
    wrefresh(stdscr);
}


draw

decompiled_code:
int __fastcall draw(int a1, int a2, char a3)
{
int v5; // eax
int result; // eax
int v7; // edx
int v8; // esi

if ( a3 )
{
wattr_on(stdscr, 0x40000uLL, 0LL);
}
else
{
wmove(stdscr, 2 * a2 + 1, 2 * a1 + 1);
wechochar(stdscr, 0x20uLL);
}
v5 = *(_DWORD *)(*(_QWORD *)(maze + 8LL * a2) + 8LL * a1);
if ( v5 == 1 )
{
v7 = 2 * a1;
v8 = 2 * a2 + 1;
}
else if ( v5 == -1 )
{
v7 = 2 * a1 + 2;
v8 = 2 * a2 + 1;
}
else if ( v5 == width )
{
v7 = 2 * a1 + 1;
v8 = 2 * a2;
}
else
{
result = width + v5;
if ( result )
goto LABEL_7;
v7 = 2 * a1 + 1;
v8 = 2 * a2 + 2;
}
wmove(stdscr, v8, v7);
result = wechochar(stdscr, 0x20uLL);
LABEL_7:
if ( a3 )
{
usleep(0x4E20u);
wmove(stdscr, 2 * a2 + 1, 2 * a1 + 1);
wechochar(stdscr, 0x20uLL);
usleep(0x4E20u);
result = wattr_off(stdscr, 0x40000uLL, 0LL);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(int a1, int a2, char a3)
{
int v5; // eax
int result; // eax
int v7; // edx
int v8; // esi

if ( a3 )
{
wattr_on(stdscr, 0x40000uLL, 0LL);
}
else
{
wmove(stdscr, 2 * a2 + 1, 2 * a1 + 1);
wechochar(stdscr, 0x20uLL);
}
v5 = *(_DWORD *)(*(_QWORD *)(maze + 8LL * a2) + 8LL * a1);
if ( v5 == 1 )
{
v7 = 2 * a1;
v8 = 2 * a2 + 1;
}
else if ( v5 == -1 )
{
v7 = 2 * a1 + 2;
v8 = 2 * a2 + 1;
}
else if ( v5 == width )
{
v7 = 2 * a1 + 1;
v8 = 2 * a2;
}
else
{
result = width + v5;
if ( result )
goto LABEL_7;
v7 = 2 * a1 + 1;
v8 = 2 * a2 + 2;
}
wmove(stdscr, v8, v7);
result = wechochar(stdscr, 0x20uLL);
LABEL_7:
if ( a3 )
{
usleep(0x4E20u);
wmove(stdscr, 2 * a2 + 1, 2 * a1 + 1);
wechochar(stdscr, 0x20uLL);
usleep(0x4E20u);
result = wattr_off(stdscr, 0x40000uLL, 0LL);
}
return result;
}

original_code:
static void draw(int x, int y, _Bool forward)
{
    if (!forward) {
 wmove(stdscr,(y * 2 + 1),(x * 2 + 1));
 wechochar(stdscr,(' '));
    } else
 wattr_on(stdscr, (attr_t)((((chtype)((1U)) << ((10) + 8)))), ((void *)0));
    if (maze[y][x].from == 1) {
 wmove(stdscr,(y * 2 + 1),(x * 2 + 1 - 1));
 wechochar(stdscr,(' '));
    } else if (maze[y][x].from == -1) {
 wmove(stdscr,(y * 2 + 1),(x * 2 + 1 + 1));
 wechochar(stdscr,(' '));
    } else if (maze[y][x].from == width) {
 wmove(stdscr,(y * 2 + 1 - 1),(x * 2 + 1));
 wechochar(stdscr,(' '));
    } else if (maze[y][x].from == -width) {
 wmove(stdscr,(y * 2 + 1 + 1),(x * 2 + 1));
 wechochar(stdscr,(' '));
    }
    if (forward) {
 usleep(20000);
 wmove(stdscr,(y * 2 + 1),(x * 2 + 1));
 wechochar(stdscr,(' '));
 usleep(20000);
 wattr_off(stdscr, (attr_t)((((chtype)((1U)) << ((10) + 8)))), ((void *)0));
    }
}


solveMaze

original_code:
static void solveMaze(void)
{
    int x, y, offset;
    for (y = 0; y < height; y ++)
 for (x = 0; x < width; x ++)
     maze[y][x].visited = 0;
    y = 0;
    x = 0;
    while (y != height - 1 || x != width - 1) {
 draw(x, y, 1);
 maze[y][x].visited = 1;
 if (!maze[y][x].right && !maze[y][x + 1].visited) {
     addLast(dp, ((y) * width + (x + 1)));
     maze[y][x + 1].from = 1;
 }
 if (!maze[y][x].bottom && !maze[y + 1][x].visited) {
     addLast(dp, ((y + 1) * width + (x)));
     maze[y + 1][x].from = width;
 }
 if (x > 0 && !maze[y][x - 1].right && !maze[y][x - 1].visited) {
     addLast(dp, ((y) * width + (x - 1)));
     maze[y][x - 1].from = -1;
 }
 if (y > 0 && !maze[y - 1][x].bottom && !maze[y - 1][x].visited) {
     addLast(dp, ((y - 1) * width + (x)));
     maze[y - 1][x].from = -width;
 }
 if (getLast(dp) == ((y) * width + (x))) {
     draw(x, y, 0);
     removeLast(dp);
 }
 offset = getLast(dp);
 x = ((offset) % width);
 y = ((offset) / width);
    }
    draw(width - 1, height - 1, 1);
}

canonical_original_code:
static void solveMaze()
{
    int x; int y; int offset;
    for (y = 0; y < height; y ++)
 for (x = 0; x < width; x ++)
     maze[y][x].visited = 0;
    y = 0;
    x = 0;
    while (y != height - 1 || x != width - 1) {
 draw(x, y, 1);
 maze[y][x].visited = 1;
 if (!maze[y][x].right && !maze[y][x + 1].visited) {
     addLast(dp, ((y) * width + (x + 1)));
     maze[y][x + 1].from = 1;
 }
 if (!maze[y][x].bottom && !maze[y + 1][x].visited) {
     addLast(dp, ((y + 1) * width + (x)));
     maze[y + 1][x].from = width;
 }
 if (x > 0 && !maze[y][x - 1].right && !maze[y][x - 1].visited) {
     addLast(dp, ((y) * width + (x - 1)));
     maze[y][x - 1].from = -1;
 }
 if (y > 0 && !maze[y - 1][x].bottom && !maze[y - 1][x].visited) {
     addLast(dp, ((y - 1) * width + (x)));
     maze[y - 1][x].from = -width;
 }
 if (getLast(dp) == ((y) * width + (x))) {
     draw(x, y, 0);
     removeLast(dp);
 }
 offset = getLast(dp);
 x = ((offset) % width);
 y = ((offset) / width);
    }
    draw(width - 1, height - 1, 1);
}


decompiled_code:
double random_uniform()
{
return (double)rand() / 2147483647.0;
}

canonical_decompiled_code:
double func0()
{
return (double)rand() / 2147483647.0;
}

original_code:
double random_uniform() {
  double ret;
  ret = ((double)rand()/(double)2147483647);
  return ret;
}

variable_types: ret double

return_type: double


decompiled_code:
__int64 __fastcall random_uniform_range(unsigned __int8 a1, unsigned __int8 a2)
{
return (unsigned int)(int)((double)rand() * 4.656612873077393e-10 * (double)(a2 - a1 + 1) + (double)a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, unsigned __int8 a2)
{
return (unsigned int)(int)((double)rand() * 4.656612873077393e-10 * (double)(a2 - a1 + 1) + (double)a1);
}

original_code:
uint8_t random_uniform_range(uint8_t min, uint8_t max) {
  double ret;
  ret = ((double) rand() / (((double)2147483647) + 1.0)) * (max-min+1) + min;
  return (uint8_t)ret;
}

canonical_original_code:
unsigned char random_uniform_range(unsigned char min, unsigned char max) {
  double ret;
  ret = ((double) rand() / (((double)2147483647) + 1.0)) * (max-min+1) + min;
  return (unsigned char)ret;
}

variable_types:
min
unsigned char

variable_types:
max
unsigned char

return_type: unsigned char


decompiled_code:
bool __fastcall random_bernoulli(double a1)
{
return a1 > random_uniform();
}

canonical_decompiled_code:
bool __fastcall func2(double a1)
{
return a1 > func0();
}

original_code:
uint8_t random_bernoulli(double tau) {
  return (uint8_t)(random_uniform() < tau);
}

canonical_original_code:
unsigned char random_bernoulli(double tau) {
  return (unsigned char)(random_uniform() < tau);
}

variable_types: tau double


decompiled_code:
__int64 random_uniform_uint32()
{
char v0; // bp
unsigned int v1; // ebx

v0 = 32;
v1 = 0;
do
{
v1 = (unsigned __int8)random_uniform_range(0, 1u) ^ (2 * v1);
--v0;
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
__int64 func3()
{
char v0; // bp
unsigned int v1; // ebx

v0 = 32;
v1 = 0;
do
{
v1 = (unsigned __int8)func1(0, 1u) ^ (2 * v1);
--v0;
}
while ( v0 );
return v1;
}

original_code:
uint32_t random_uniform_uint32(void) {
  uint32_t n = 0;
  uint8_t i = 0;
  for(i = 0; i < 32; i++) {
    n <<= 1;
    n ^= (uint32_t)random_uniform_range(0, 1);
  }
  return n;
}

canonical_original_code:
unsigned int random_uniform_uint32() {
  unsigned int n = 0;
  unsigned char i = 0;
  for(i = 0; i < 32; i++) {
    n <<= 1;
    n ^= (unsigned int)random_uniform_range(0, 1);
  }
  return n;
}

variable_types:
n
unsigned int

variable_types:
i
unsigned char

return_type: unsigned int


decompiled_code:
__int64 __fastcall random_bernoulli_uint32(double a1)
{
char v1; // bp
unsigned int v2; // ebx

v1 = 32;
v2 = 0;
do
{
v2 = random_bernoulli(a1) ^ (2 * v2);
--v1;
}
while ( v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(double a1)
{
char v1; // bp
unsigned int v2; // ebx

v1 = 32;
v2 = 0;
do
{
v2 = func2(a1) ^ (2 * v2);
--v1;
}
while ( v1 );
return v2;
}

original_code:
uint32_t random_bernoulli_uint32(double tau) {
  uint8_t i = 0;
  uint32_t n = 0;
  for(; i < 32; i++) {
    n <<= 1;
    n ^= (uint32_t)random_bernoulli(tau);
  }
  return n;
}

canonical_original_code:
unsigned int random_bernoulli_uint32(double tau) {
  unsigned char i = 0;
  unsigned int n = 0;
  for(; i < 32; i++) {
    n <<= 1;
    n ^= (unsigned int)random_bernoulli(tau);
  }
  return n;
}


usage

decompiled_code:
size_t usage()
{
return fwrite("Usage: \n", 1uLL, 8uLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
return fwrite("Usage: \n", 1uLL, 8uLL, stderr);
}

original_code:
void usage() {
  fprintf(stderr, "Usage: \n");
}


decompiled_code:
__int64 test_lapin_reduc()
{
__int64 v0; // rbp
__int64 v1; // r12
__int64 v2; // rax
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = lapin_init(1LL);
v1 = challenge_generate(*(unsigned __int8 *)(v0 + 16));
v2 = lapin_pimapping_reduc(v0, v1);
poly_crt_free(v2);
lapin_tag(v0, v1, &v4, v5);
poly_crt_free(v4);
poly_crt_free(v5[0]);
challenge_free(v1);
return lapin_end(v0);
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbp
__int64 v1; // r12
__int64 v2; // rax
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = lapin_init(1LL);
v1 = challenge_generate(*(unsigned __int8 *)(v0 + 16));
v2 = lapin_pimapping_reduc(v0, v1);
poly_crt_free(v2);
lapin_tag(v0, v1, &v4, v5);
poly_crt_free(v4);
poly_crt_free(v5[0]);
challenge_free(v1);
return lapin_end(v0);
}

original_code:
void test_lapin_reduc() {
  Lapin *lapin = lapin_init(1);
  Challenge c = challenge_generate(lapin->sec_param);
  PolyCRT *pi = lapin_pimapping_reduc(lapin, c);
  poly_crt_free(pi);
  PolyCRT *r, *z;
  lapin_tag(lapin, c, &r, &z);
  poly_crt_free(r);
  poly_crt_free(z);
  challenge_free(c);
  lapin_end(lapin);
}

canonical_original_code:
void test_lapin_reduc() {
  struct s_lapin *lapin = lapin_init(1);
  unsigned int *c = challenge_generate(lapin->sec_param);
  struct s_poly_crt *pi = lapin_pimapping_reduc(lapin, c);
  poly_crt_free(pi);
  struct s_poly_crt *r; struct s_poly_crt *z;
  lapin_tag(lapin, c, &r, &z);
  poly_crt_free(r);
  poly_crt_free(z);
  challenge_free(c);
  lapin_end(lapin);
}

variable_types: struct s_lapin { double tau; unsigned short sec_param; unsigned int n; struct s_key { struct s_poly { unsigned int *vec; unsigned short n_words; } *s1; } *key; struct s_key_crt { struct s_poly_crt { unsigned char m; struct s_poly **crt; } *s1; } *key_crt; struct s_poly *f_normal; struct s_poly_crt *f_crt; unsigned char reduc; } *

variable_types:
c
unsigned int *

variable_types: struct s_poly_crt { unsigned char m; struct s_poly { unsigned int *vec; unsigned short n_words; } **crt; } *

user_defined_types: struct s_lapin { double tau; unsigned short sec_param; unsigned int n; struct s_key *key; struct s_key_crt *key_crt; struct s_poly *f_normal; struct s_poly_crt *f_crt; unsigned char reduc; }

user_defined_types: struct s_key_crt { struct s_poly_crt *s1; }


decompiled_code:
__int64 test_lapin_irreduc()
{
__int64 v0; // rbp
__int64 v1; // r12
char v2; // al
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = lapin_init(0LL);
v1 = challenge_generate(*(unsigned __int8 *)(v0 + 16));
lapin_tag(v0, v1, &v4, v5);
v2 = lapin_vrfy(v0, v1, v4, v5[0]);
printf("irreduc vrfy = %u\n", (unsigned int)v2);
poly_free(v4);
poly_free(v5[0]);
challenge_free(v1);
return lapin_end(v0);
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rbp
__int64 v1; // r12
char v2; // al
__int64 v4; // [rsp+0h] [rbp-28h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = lapin_init(0LL);
v1 = challenge_generate(*(unsigned __int8 *)(v0 + 16));
lapin_tag(v0, v1, &v4, v5);
v2 = lapin_vrfy(v0, v1, v4, v5[0]);
printf("irreduc vrfy = %u\n", (unsigned int)v2);
poly_free(v4);
poly_free(v5[0]);
challenge_free(v1);
return lapin_end(v0);
}

original_code:
void test_lapin_irreduc() {
  Lapin *lapin = lapin_init(0);
  Challenge c = challenge_generate(lapin->sec_param);
  Poly *r, *z;
  lapin_tag(lapin, c, &r, &z);
  printf("irreduc vrfy = %u\n", lapin_vrfy(lapin, c, r, z));
  poly_free(r);
  poly_free(z);
  challenge_free(c);
  lapin_end(lapin);
}

canonical_original_code:
void test_lapin_irreduc() {
  struct s_lapin *lapin = lapin_init(0);
  unsigned int *c = challenge_generate(lapin->sec_param);
  struct s_poly *r; struct s_poly *z;
  lapin_tag(lapin, c, &r, &z);
  printf("irreduc vrfy = %u\n", lapin_vrfy(lapin, c, r, z));
  poly_free(r);
  poly_free(z);
  challenge_free(c);
  lapin_end(lapin);
}

variable_types: struct s_poly { unsigned int *vec; unsigned short n_words; } *

user_defined_types: struct s_poly_crt { unsigned char m; struct s_poly **crt; }


main

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

v3 = time(0LL);
srand(v3);
test_lapin_irreduc(v3, argv);
return 0;
}

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

v3 = time(0LL);
srand(v3);
func2(v3, argv);
return 0;
}

original_code:
int main(int argc, char **argv) {
  srand((unsigned)time(((void *)0)));
  test_lapin_irreduc();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  srand((unsigned int)time(((void *)0)));
  test_lapin_irreduc();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **


canonical_original_code:
unsigned char binary_hamming_weight(unsigned int n) {
  unsigned int wt = 0;
  wt = n - ((n >> 1) & B[0]);
  wt = ((wt >> S[1]) & B[1]) + (wt & B[1]);
  wt = ((wt >> S[2]) + wt) & B[2];
  wt = ((wt >> S[3]) + wt) & B[3];
  wt = ((wt >> S[4]) + wt) & B[4];
  return (unsigned char)wt;
}

variable_types:
wt
unsigned int


decompiled_code:
__int64 __fastcall binary_degree(unsigned int a1)
{
__int64 result; // rax
unsigned int i; // edi

if ( !a1 )
return 0xFFFFFFFFLL;
result = 0LL;
for ( i = a1 >> 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
__int64 result; // rax
unsigned int i; // edi

if ( !a1 )
return 0xFFFFFFFFLL;
result = 0LL;
for ( i = a1 >> 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

original_code:
int16_t binary_degree(uint32_t p) {
  if(p == 0) {
    return -1;
  }
  int16_t deg = 0;
  while(p >>= 1)
    deg++;
  return deg;
}

canonical_original_code:
short binary_degree(unsigned int p) {
  if(p == 0) {
    return -1;
  }
  short deg = 0;
  while(p >>= 1)
    deg++;
  return deg;
}

variable_types:
p
unsigned int

variable_types: deg short

return_type: short


decompiled_code:
__int64 __fastcall binary_reverse_number(int a1)
{
__int64 result; // rax
int v2; // esi
unsigned int v3; // ecx
int v4; // edx
int v5; // edi
unsigned int v6; // eax
int v7; // edi

LODWORD(result) = a1;
v2 = 5;
v3 = 16;
v4 = -1;
do
{
v4 ^= v4 << v3;
v5 = result;
v6 = (unsigned int)result >> v3;
v7 = v5 << v3;
v3 >>= 1;
result = v7 ^ v4 & (v7 ^ v6);
--v2;
}
while ( v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
__int64 result; // rax
int v2; // esi
unsigned int v3; // ecx
int v4; // edx
int v5; // edi
unsigned int v6; // eax
int v7; // edi

LODWORD(result) = a1;
v2 = 5;
v3 = 16;
v4 = -1;
do
{
v4 ^= v4 << v3;
v5 = result;
v6 = (unsigned int)result >> v3;
v7 = v5 << v3;
v3 >>= 1;
result = v7 ^ v4 & (v7 ^ v6);
--v2;
}
while ( v2 );
return result;
}

original_code:
uint32_t binary_reverse_number(uint32_t n) {
  uint32_t s = sizeof(n) * sizeof(char) * 8;
  uint32_t mask = ~0;
  while ((s >>= 1) > 0) {
    mask ^= (mask << s);
    n = ((n >> s) & mask) | ((n << s) & ~mask);
  }
  return n;
}

canonical_original_code:
unsigned int binary_reverse_number(unsigned int n) {
  unsigned int s = sizeof(n) * sizeof(char) * 8;
  unsigned int mask = ~0;
  while ((s >>= 1) > 0) {
    mask ^= (mask << s);
    n = ((n >> s) & mask) | ((n << s) & ~mask);
  }
  return n;
}

variable_types:
s
unsigned int

variable_types:
mask
unsigned int


decompiled_code:
__int64 __fastcall binary_char_to_uint(char *a1)
{
char v1; // cl
char *v2; // rdx
__int64 result; // rax

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = a1;
LODWORD(result) = 0;
do
{
++v2;
result = (v1 != 48) | (unsigned int)(2 * result);
v1 = *v2;
}
while ( *v2 && v2 - a1 <= 31 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1)
{
char v1; // cl
char *v2; // rdx
__int64 result; // rax

v1 = *a1;
if ( !*a1 )
return 0LL;
v2 = a1;
LODWORD(result) = 0;
do
{
++v2;
result = (v1 != 48) | (unsigned int)(2 * result);
v1 = *v2;
}
while ( *v2 && v2 - a1 <= 31 );
return result;
}

original_code:
uint32_t binary_char_to_uint(const unsigned char *c) {
  uint32_t out = 0;
  const unsigned char *s = c;
  while(*s && s-c < (uint8_t)(sizeof(uint32_t) * 8)) {
    out = (out << 1) | (*s == '0' ? 0 : 1);
    s++;
  }
  return out;
}

canonical_original_code:
unsigned int binary_char_to_uint(unsigned char *c) {
  unsigned int out = 0;
  unsigned char *s = c;
  while(*s && s-c < (unsigned char)(sizeof(unsigned int) * 8)) {
    out = (out << 1) | (*s == '0' ? 0 : 1);
    s++;
  }
  return out;
}

variable_types:
c
unsigned char *

variable_types:
out
unsigned int

variable_types:
s
unsigned char *


decompiled_code:
bool __fastcall binary_get_bit(int a1, char a2)
{
return (a1 & (1 << a2)) != 0;
}

canonical_decompiled_code:
bool __fastcall func7(int a1, char a2)
{
return (a1 & (1 << a2)) != 0;
}

original_code:
uint8_t binary_get_bit(uint32_t w, uint8_t i) {
  uint8_t set = (w & (1 << i)) != 0;
  return set;
}

canonical_original_code:
unsigned char binary_get_bit(unsigned int w, unsigned char i) {
  unsigned char set = (w & (1 << i)) != 0;
  return set;
}

variable_types:
w
unsigned int

variable_types:
set
unsigned char


original_code:
int64_t time_in_ms(struct timespec *timeA_p, struct timespec *timeB_p) {
  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
         ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}

canonical_original_code:
long time_in_ms(struct timespec *timeA_p, struct timespec *timeB_p) {
  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
         ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *


randombytes

decompiled_code:
void __fastcall randombytes(void *buf, size_t nbytes)
{
size_t v3; // rbx
size_t v4; // rdx
int v5; // eax

v3 = nbytes;
if ( fd == -1 )
{
while ( 1 )
{
fd = open("/dev/urandom", 0);
if ( fd != -1 )
break;
sleep(1u);
}
}
if ( nbytes )
{
do
{
while ( 1 )
{
v4 = 0x100000LL;
if ( v3 <= 0xFFFFF )
v4 = v3;
v5 = read(fd, buf, v4);
if ( v5 > 0 )
break;
sleep(1u);
}
buf = (char *)buf + v5;
v3 -= v5;
}
while ( v3 );
}
}

canonical_decompiled_code:
void __fastcall func0(void *buf, size_t nbytes)
{
size_t v3; // rbx
size_t v4; // rdx
int v5; // eax

v3 = nbytes;
if ( fd == -1 )
{
while ( 1 )
{
fd = open("/dev/urandom", 0);
if ( fd != -1 )
break;
sleep(1u);
}
}
if ( nbytes )
{
do
{
while ( 1 )
{
v4 = 0x100000LL;
if ( v3 <= 0xFFFFF )
v4 = v3;
v5 = read(fd, buf, v4);
if ( v5 > 0 )
break;
sleep(1u);
}
buf = (char *)buf + v5;
v3 -= v5;
}
while ( v3 );
}
}

original_code:
void randombytes(void *x, unsigned long xlen)
{
  int i;
  if (fd == -1) {
    for (;;) {
      fd = open("/dev/urandom",00);
      if (fd != -1) break;
      sleep(1);
    }
  }
  while (xlen > 0) {
    if (xlen < 1048576) i = xlen; else i = 1048576;
    i = read(fd,x,i);
    if (i < 1) {
      sleep(1);
      continue;
    }
    x += i;
    xlen -= i;
  }
}

variable_types:
x
void *

variable_types:
xlen
unsigned long

variable_types: i int


decompiled_code:
__int64 parse_opts()
{
flag_pretty = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
flag_pretty = 0;
return 0LL;
}

original_code:
int parse_opts(int argc, char *argv[]){
  flag_pretty = 0;
  flag_aur = 0;
  flag_echo = 0;
  flag_composite = 1;
  flag_prime = 1;
  return 0;
}

variable_types:
argv
char *[]


decompiled_code:
void handle_each_line()
{
int v0; // eax
int v1; // ebp
int v2; // ebx
void *v3; // rax
__int64 v4; // rbx
__int64 v5; // rdi
int v6; // eax
void *v7; // rbp
__int64 v8; // rbx
__int64 v9; // rdi
__int64 v10; // r12
__int64 v11; // rbx
__int64 v12; // r13
int v13; // ebp
const char *v14; // rax
size_t v15; // rax
__int64 v16; // rdi
__int64 v17; // [rsp+0h] [rbp-2038h] BYREF

v0 = cn_set_str(cn, buf);
if ( v0 )
{
v1 = v0;
if ( (unsigned int)(v0 - 1) > 1 )
{
v2 = v0 >> 8;
if ( (v0 & 0xFC) != 0 )
{
fprintf(stderr, "%s\n", buf);
fprintf(
stderr,
"calc error retval = %d, reg1 = %d, reg2 = %d\n",
(unsigned __int8)v1,
BYTE1(v1),
(unsigned int)(v1 >> 16));
v3 = cn;
if ( *((int *)cn + 2) > 0 )
{
v4 = 0LL;
do
{
v5 = v4++;
_gmpz_clear(*((_QWORD *)v3 + 2) + 16 * v5);
v3 = cn;
}
while ( *((_DWORD *)cn + 2) > (int)v4 );
}
free(*((void **)v3 + 2));
}
else
{
fprintf(stderr, "parse error:\n%s\n", buf);
for ( ; v2; --v2 )
fputc(32, stderr);
fwrite("^\n\n", 1uLL, 3uLL, stderr);
}
}
}
else
{
v6 = cn_check_algebraic_factor(cn);
if ( v6 > 0 )
{
v10 = 16LL * v6;
v11 = 0LL;
puts(buf);
do
{
v12 = v11 + *((_QWORD *)cn + 4);
v13 = -((unsigned int)_gmpz_probab_prime_p(v12, 30LL) == 0);
v14 = (const char *)_gmpz_get_str(&v17, 10LL, v12);
v15 = strlen(v14);
printf("%c%d = ", (v13 & 0xFFFFFFF3) + 80, v15);
_gmpz_out_str(stdout, 10LL, v11 + *((_QWORD *)cn + 4));
puts(&s);
v16 = v11 + *((_QWORD *)cn + 4);
v11 += 16LL;
_gmpz_clear(v16);
}
while ( v10 != v11 );
puts(&s);
v7 = cn;
free(*((void **)cn + 4));
}
else
{
if ( flag_pretty )
printf("%c%d = ", (unsigned int)*((char *)cn + 24), *((unsigned int *)cn + 7));
_gmpz_out_str(stdout, 10LL, *((_QWORD *)cn + 2) + 16LL * *((int *)cn + 2) - 16);
puts(&s);
v7 = cn;
}
v8 = 0LL;
if ( *((int *)v7 + 2) > 0 )
{
do
{
v9 = v8++;
_gmpz_clear(*((_QWORD *)v7 + 2) + 16 * v9);
v7 = cn;
}
while ( *((_DWORD *)cn + 2) > (int)v8 );
}
free(*((void **)v7 + 2));
}
}

canonical_decompiled_code:
void func1()
{
int v0; // eax
int v1; // ebp
int v2; // ebx
void *v3; // rax
__int64 v4; // rbx
__int64 v5; // rdi
int v6; // eax
void *v7; // rbp
__int64 v8; // rbx
__int64 v9; // rdi
__int64 v10; // r12
__int64 v11; // rbx
__int64 v12; // r13
int v13; // ebp
const char *v14; // rax
size_t v15; // rax
__int64 v16; // rdi
__int64 v17; // [rsp+0h] [rbp-2038h] BYREF

v0 = cn_set_str(cn, buf);
if ( v0 )
{
v1 = v0;
if ( (unsigned int)(v0 - 1) > 1 )
{
v2 = v0 >> 8;
if ( (v0 & 0xFC) != 0 )
{
fprintf(stderr, "%s\n", buf);
fprintf(
stderr,
"calc error retval = %d, reg1 = %d, reg2 = %d\n",
(unsigned __int8)v1,
BYTE1(v1),
(unsigned int)(v1 >> 16));
v3 = cn;
if ( *((int *)cn + 2) > 0 )
{
v4 = 0LL;
do
{
v5 = v4++;
_gmpz_clear(*((_QWORD *)v3 + 2) + 16 * v5);
v3 = cn;
}
while ( *((_DWORD *)cn + 2) > (int)v4 );
}
free(*((void **)v3 + 2));
}
else
{
fprintf(stderr, "parse error:\n%s\n", buf);
for ( ; v2; --v2 )
fputc(32, stderr);
fwrite("^\n\n", 1uLL, 3uLL, stderr);
}
}
}
else
{
v6 = cn_check_algebraic_factor(cn);
if ( v6 > 0 )
{
v10 = 16LL * v6;
v11 = 0LL;
puts(buf);
do
{
v12 = v11 + *((_QWORD *)cn + 4);
v13 = -((unsigned int)_gmpz_probab_prime_p(v12, 30LL) == 0);
v14 = (const char *)_gmpz_get_str(&v17, 10LL, v12);
v15 = strlen(v14);
printf("%c%d = ", (v13 & 0xFFFFFFF3) + 80, v15);
_gmpz_out_str(stdout, 10LL, v11 + *((_QWORD *)cn + 4));
puts(&s);
v16 = v11 + *((_QWORD *)cn + 4);
v11 += 16LL;
_gmpz_clear(v16);
}
while ( v10 != v11 );
puts(&s);
v7 = cn;
free(*((void **)cn + 4));
}
else
{
if ( flag_pretty )
printf("%c%d = ", (unsigned int)*((char *)cn + 24), *((unsigned int *)cn + 7));
_gmpz_out_str(stdout, 10LL, *((_QWORD *)cn + 2) + 16LL * *((int *)cn + 2) - 16);
puts(&s);
v7 = cn;
}
v8 = 0LL;
if ( *((int *)v7 + 2) > 0 )
{
do
{
v9 = v8++;
_gmpz_clear(*((_QWORD *)v7 + 2) + 16 * v9);
v7 = cn;
}
while ( *((_DWORD *)cn + 2) > (int)v8 );
}
free(*((void **)v7 + 2));
}
}

original_code:
void handle_each_line(){
  int ret;
  ret = cn_set_str(cn, buf);
  if(ret != 0){
    if(ret == 1 || ret == 2) return;
    if(((ret) != 0 && ((ret) & 0xff) <= 3)){
      int cnt = ((ret) >> 8);
      fprintf(stderr, "parse error:\n%s\n", buf);
      while(cnt--){
        fputc(' ', stderr);
      }
      fputs("^\n\n", stderr);
    }else{
      fprintf(stderr, "%s\n", buf);
      fprintf(stderr, "calc error retval = %d, reg1 = %d, reg2 = %d\n", ret&0xff, (ret>>8)&0xff, ret>>16);
      do{ int _i; for(_i = 0; _i < (cn)->nfactors; _i++){ __gmpz_clear((cn)->factor[_i]); } free((cn)->factor); }while(0);
    }
    return;
  }
  ret = cn_check_algebraic_factor(cn);
  if(ret > 0){
    int i;
    char tmpbuf[8192 +10];
      puts(buf);
    for(i = 0; i < ret; i++){
      char flag = __gmpz_probab_prime_p(cn->newfactor[i], 30) ? 'P' : 'C';
      int digit = strlen(__gmpz_get_str(tmpbuf, 10, cn->newfactor[i]));
      printf("%c%d = ", flag, digit);
      __gmpz_out_str(stdout, 10, cn->newfactor[i]);
      puts("");
      __gmpz_clear(cn->newfactor[i]);
    }
    puts("");
    free(cn->newfactor);
  }else{
    if(flag_pretty){
      printf("%c%d = ", cn->flag, cn->lf_digit);
    }
    __gmpz_out_str(stdout, 10, cn->factor[cn->nfactors-1]);
    puts("");
  }
  do{ int _i; for(_i = 0; _i < (cn)->nfactors; _i++){ __gmpz_clear((cn)->factor[_i]); } free((cn)->factor); }while(0);
}

canonical_original_code:
void handle_each_line(){
  int ret;
  ret = cn_set_str(cn, buf);
  if(ret != 0){
    if(ret == 1 || ret == 2) return;
    if(((ret) != 0 && ((ret) & 0xff) <= 3)){
      int cnt = ((ret) >> 8);
      fprintf(stderr, "parse error:\n%s\n", buf);
      while(cnt--){
        fputc(' ', stderr);
      }
      fputs("^\n\n", stderr);
    }else{
      fprintf(stderr, "%s\n", buf);
      fprintf(stderr, "calc error retval = %d, reg1 = %d, reg2 = %d\n", ret&0xff, (ret>>8)&0xff, ret>>16);
      do{ int _i; for(_i = 0; _i < (cn)->nfactors; _i++){ __gmpz_clear((cn)->factor[_i]); } free((cn)->factor); }while(0);
    }
    return;
  }
  ret = cn_check_algebraic_factor(cn);
  if(ret > 0){
    int i;
    char tmpbuf[-1];
      puts(buf);
    for(i = 0; i < ret; i++){
      char flag = __gmpz_probab_prime_p(cn->newfactor[i], 30) ? 'P' : 'C';
      int digit = strlen(__gmpz_get_str(tmpbuf, 10, cn->newfactor[i]));
      printf("%c%d = ", flag, digit);
      __gmpz_out_str(stdout, 10, cn->newfactor[i]);
      puts("");
      __gmpz_clear(cn->newfactor[i]);
    }
    puts("");
    free(cn->newfactor);
  }else{
    if(flag_pretty){
      printf("%c%d = ", cn->flag, cn->lf_digit);
    }
    __gmpz_out_str(stdout, 10, cn->factor[cn->nfactors-1]);
    puts("");
  }
  do{ int _i; for(_i = 0; _i < (cn)->nfactors; _i++){ __gmpz_clear((cn)->factor[_i]); } free((cn)->factor); }while(0);
}

variable_types: ret int

variable_types: cnt int

variable_types:
tmpbuf
char [-1]

variable_types: flag char

variable_types: digit int


main

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

if ( (int)parse_opts(argc, argv, envp) < 0 )
{
puts("komata");
}
else
{
cn = malloc(0x28uLL);
cnpoly_init();
aurpoly_init();
powlist_init();
while ( fgets(buf, 0x2000, stdin) )
{
v3 = strpbrk(buf, "\r\n");
if ( !v3 )
{
fwrite("Input too long\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*v3 = 0;
handle_each_line();
}
free(cn);
cnpoly_free();
aurpoly_free();
powlist_free();
}
return 0;
}

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

if ( (int)func0(argc, argv, envp) < 0 )
{
puts("komata");
}
else
{
cn = malloc(0x28uLL);
cnpoly_init();
aurpoly_init();
powlist_init();
while ( fgets(buf, 0x2000, stdin) )
{
v3 = strpbrk(buf, "\r\n");
if ( !v3 )
{
fwrite("Input too long\n", 1uLL, 0xFuLL, stderr);
exit(1);
}
*v3 = 0;
func1();
}
free(cn);
cnpoly_free();
aurpoly_free();
powlist_free();
}
return 0;
}

original_code:
int main(int argc, char *argv[]){
  if(parse_opts(argc, argv) < 0){
    printf("komata\n");
    return 0;
  }
  do{ (cn) = malloc(sizeof(_cn_t)); }while(0);
  cnpoly_init();
  aurpoly_init();
  powlist_init();
  while(fgets(buf, 8192, stdin) != ((void *)0)){
    char *pos = strpbrk(buf, "\r\n");
    if(pos == ((void *)0)){
      fputs("Input too long\n", stderr);
      exit(1);
    }
    *pos = '\0';
    handle_each_line();
  }
  free(cn);
  cnpoly_free();
  aurpoly_free();
  powlist_free();
  return 0;
}

variable_types: argc int

variable_types:
pos
char *