decompiled_code:
__int64 __fastcall smime_process_mails(const char ***a1, const char **a2, int a3)
{
__int64 v4; // r12
const char **v5; // rbp
unsigned int v6; // er14
__int64 v7; // rax
__int64 v8; // rbx
const char *v9; // rsi
unsigned int v10; // er15
__int64 v11; // rax
__int64 v12; // rbx
const char *v13; // rsi
int v14; // ebx
unsigned int v16; // er14
__int64 v17; // rax
__int64 v18; // rbx
const char *v19; // rsi
int v20; // ebx
unsigned int v21; // ebx
__int64 v22; // rax
__int64 v23; // r14
const char *v24; // rsi
int v25; // ebx
char s[568]; // [rsp+0h] [rbp-238h] BYREF

if ( a3 > 0 )
{
v4 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
v5 = a2;
while ( 1 )
{
v6 = 0;
v7 = 0LL;
if ( *((_QWORD *)&conf + 8) )
{
while ( 1 )
{
v8 = 32 * v7;
v9 = *(const char **)(*((_QWORD *)&conf + 7) + 32 * v7);
if ( v9 )
{
if ( strcasestr(**a1, v9) )
break;
}
v7 = ++v6;
if ( (unsigned __int64)v6 >= *((_QWORD *)&conf + 8) )
{
if ( !*((_QWORD *)&conf + 6) )
goto LABEL_21;
v6 = 0;
goto LABEL_9;
}
}
snprintf(
s,
0x200uLL,
"smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 8),
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 16),
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 24),
*v5,
*v5);
v6 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v6 )
{
if ( rename(s, *v5) )
{
remove(s);
goto LABEL_38;
}
v6 = 1;
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
remove(s);
if ( !*((_QWORD *)&conf + 6) )
goto LABEL_17;
LABEL_9:
v10 = 0;
v11 = 0LL;
while ( 1 )
{
v12 = 16 * v11;
v13 = *(const char **)(*((_QWORD *)&conf + 5) + 16 * v11);
if ( v13 )
{
if ( (*a1)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*a1)[1], v13) )
break;
}
v11 = ++v10;
if ( (unsigned __int64)v10 >= *((_QWORD *)&conf + 6) )
goto LABEL_16;
}
snprintf(
s,
0x200uLL,
"smime-tool -encrypt -cert %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 5) + v12 + 8),
*v5,
*v5);
v14 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v14 && !rename(s, *v5) )
goto LABEL_41;
remove(s);
LABEL_16:
if ( !v6 )
goto LABEL_21;
goto LABEL_17;
}
v6 = 0;
remove(s);
if ( *((_QWORD *)&conf + 6) )
goto LABEL_9;
}
else
{
LABEL_38:
if ( *((_QWORD *)&conf + 6) )
goto LABEL_9;
}
LABEL_21:
v16 = 0;
v17 = 0LL;
if ( *((_QWORD *)&conf + 10) )
{
while ( 1 )
{
v18 = 32 * v17;
v19 = *(const char **)(*((_QWORD *)&conf + 9) + 32 * v17);
if ( v19 )
{
if ( (*a1)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*a1)[1], v19) )
break;
}
v17 = ++v16;
if ( (unsigned __int64)v16 >= *((_QWORD *)&conf + 10) )
goto LABEL_29;
}
snprintf(
s,
0x200uLL,
"smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 8),
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 16),
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 24),
*v5,
*v5);
v20 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v20 && !rename(s, *v5) )
{
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
}
remove(s);
}
LABEL_29:
if ( *((_QWORD *)&conf + 12) )
{
v21 = 0;
v22 = 0LL;
while ( 1 )
{
v23 = 24 * v22;
v24 = *(const char **)(*((_QWORD *)&conf + 11) + 24 * v22);
if ( v24 )
{
if ( strcasestr(**a1, v24) )
break;
}
v22 = ++v21;
if ( (unsigned __int64)v21 >= *((_QWORD *)&conf + 12) )
goto LABEL_17;
}
snprintf(
s,
0x200uLL,
"smime-tool -verify -cert %s -ca %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 11) + v23 + 8),
*(const char **)(*((_QWORD *)&conf + 11) + v23 + 16),
*v5,
*v5);
v25 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v25 && !rename(s, *v5) )
{
LABEL_41:
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
}
remove(s);
}
LABEL_17:
++v5;
++a1;
if ( v5 == (const char **)v4 )
return 0LL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char ***a1, const char **a2, int a3)
{
__int64 v4; // r12
const char **v5; // rbp
unsigned int v6; // er14
__int64 v7; // rax
__int64 v8; // rbx
const char *v9; // rsi
unsigned int v10; // er15
__int64 v11; // rax
__int64 v12; // rbx
const char *v13; // rsi
int v14; // ebx
unsigned int v16; // er14
__int64 v17; // rax
__int64 v18; // rbx
const char *v19; // rsi
int v20; // ebx
unsigned int v21; // ebx
__int64 v22; // rax
__int64 v23; // r14
const char *v24; // rsi
int v25; // ebx
char s[568]; // [rsp+0h] [rbp-238h] BYREF

if ( a3 > 0 )
{
v4 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
v5 = a2;
while ( 1 )
{
v6 = 0;
v7 = 0LL;
if ( *((_QWORD *)&conf + 8) )
{
while ( 1 )
{
v8 = 32 * v7;
v9 = *(const char **)(*((_QWORD *)&conf + 7) + 32 * v7);
if ( v9 )
{
if ( strcasestr(**a1, v9) )
break;
}
v7 = ++v6;
if ( (unsigned __int64)v6 >= *((_QWORD *)&conf + 8) )
{
if ( !*((_QWORD *)&conf + 6) )
goto LABEL_21;
v6 = 0;
goto LABEL_9;
}
}
snprintf(
s,
0x200uLL,
"smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 8),
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 16),
*(const char **)(v8 + *((_QWORD *)&conf + 7) + 24),
*v5,
*v5);
v6 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v6 )
{
if ( rename(s, *v5) )
{
remove(s);
goto LABEL_38;
}
v6 = 1;
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
remove(s);
if ( !*((_QWORD *)&conf + 6) )
goto LABEL_17;
LABEL_9:
v10 = 0;
v11 = 0LL;
while ( 1 )
{
v12 = 16 * v11;
v13 = *(const char **)(*((_QWORD *)&conf + 5) + 16 * v11);
if ( v13 )
{
if ( (*a1)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*a1)[1], v13) )
break;
}
v11 = ++v10;
if ( (unsigned __int64)v10 >= *((_QWORD *)&conf + 6) )
goto LABEL_16;
}
snprintf(
s,
0x200uLL,
"smime-tool -encrypt -cert %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 5) + v12 + 8),
*v5,
*v5);
v14 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v14 && !rename(s, *v5) )
goto LABEL_41;
remove(s);
LABEL_16:
if ( !v6 )
goto LABEL_21;
goto LABEL_17;
}
v6 = 0;
remove(s);
if ( *((_QWORD *)&conf + 6) )
goto LABEL_9;
}
else
{
LABEL_38:
if ( *((_QWORD *)&conf + 6) )
goto LABEL_9;
}
LABEL_21:
v16 = 0;
v17 = 0LL;
if ( *((_QWORD *)&conf + 10) )
{
while ( 1 )
{
v18 = 32 * v17;
v19 = *(const char **)(*((_QWORD *)&conf + 9) + 32 * v17);
if ( v19 )
{
if ( (*a1)[2] == (_BYTE *)generate_filename + 1 && strcasestr(*(const char **)(*a1)[1], v19) )
break;
}
v17 = ++v16;
if ( (unsigned __int64)v16 >= *((_QWORD *)&conf + 10) )
goto LABEL_29;
}
snprintf(
s,
0x200uLL,
"smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 8),
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 16),
*(const char **)(v18 + *((_QWORD *)&conf + 9) + 24),
*v5,
*v5);
v20 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v20 && !rename(s, *v5) )
{
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
}
remove(s);
}
LABEL_29:
if ( *((_QWORD *)&conf + 12) )
{
v21 = 0;
v22 = 0LL;
while ( 1 )
{
v23 = 24 * v22;
v24 = *(const char **)(*((_QWORD *)&conf + 11) + 24 * v22);
if ( v24 )
{
if ( strcasestr(**a1, v24) )
break;
}
v22 = ++v21;
if ( (unsigned __int64)v21 >= *((_QWORD *)&conf + 12) )
goto LABEL_17;
}
snprintf(
s,
0x200uLL,
"smime-tool -verify -cert %s -ca %s %s > %s.prcs",
*(const char **)(*((_QWORD *)&conf + 11) + v23 + 8),
*(const char **)(*((_QWORD *)&conf + 11) + v23 + 16),
*v5,
*v5);
v25 = system(s);
snprintf(s, 0x200uLL, "%s.prcs", *v5);
if ( !v25 && !rename(s, *v5) )
{
LABEL_41:
free_mail_object(*a1);
load_mail_from_file(*v5, *a1);
}
remove(s);
}
LABEL_17:
++v5;
++a1;
if ( v5 == (const char **)v4 )
return 0LL;
}
}
return 0LL;
}

original_code:
int smime_process_mails (struct mail_object **mails, char **fns, int no_mails)
{
    int m, toprcs, sign_encr, ret;
    unsigned int r;
    char cmd[512];
    for (m = 0; m < no_mails; ++m) {
        toprcs = 0;
        sign_encr = 0;
        for (r = 0; r < conf.sign_rules_size; ++r) {
            if (((void *)0) != conf.sign_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.sign_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
                conf.sign_rules[r].cert_path, conf.sign_rules[r].key_path,
                conf.sign_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.encr_rules_size; ++r) {
            if (((void *)0) != conf.encr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.encr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                    "smime-tool -encrypt -cert %s %s > %s.prcs",
                    conf.encr_rules[r].cert_path, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        if (sign_encr)
            continue;
        toprcs = 0;
        for (r = 0; r < conf.decr_rules_size; ++r) {
            if (((void *)0) != conf.decr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.decr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
                conf.decr_rules[r].cert_path, conf.decr_rules[r].key_path,
                conf.decr_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.vrfy_rules_size; ++r) {
            if (((void *)0) != conf.vrfy_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.vrfy_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -verify -cert %s -ca %s %s > %s.prcs",
                conf.vrfy_rules[r].cert_path, conf.vrfy_rules[r].cacert_path,
                fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
    }
    return 0;
}

canonical_original_code:
int smime_process_mails(struct mail_object **mails, char **fns, int no_mails)
{
    int m; int toprcs; int sign_encr; int ret;
    unsigned int r;
    char cmd[512];
    for (m = 0; m < no_mails; ++m) {
        toprcs = 0;
        sign_encr = 0;
        for (r = 0; r < conf.sign_rules_size; ++r) {
            if (((void *)0) != conf.sign_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.sign_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -sign -cert %s -key %s -pass %s %s > %s.prcs",
                conf.sign_rules[r].cert_path, conf.sign_rules[r].key_path,
                conf.sign_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.encr_rules_size; ++r) {
            if (((void *)0) != conf.encr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.encr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                    "smime-tool -encrypt -cert %s %s > %s.prcs",
                    conf.encr_rules[r].cert_path, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                    sign_encr = 1;
                }
            }
            remove(cmd);
        }
        if (sign_encr)
            continue;
        toprcs = 0;
        for (r = 0; r < conf.decr_rules_size; ++r) {
            if (((void *)0) != conf.decr_rules[r].rcpt) {
                if (1 == mails[m]->no_rcpt) {
                    if (strcasestr(mails[m]->rcpt_to[0],
                                conf.decr_rules[r].rcpt))
                    {
                        toprcs = 1;
                        break;
                    }
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -decrypt -cert %s -key %s -pass %s %s > %s.prcs",
                conf.decr_rules[r].cert_path, conf.decr_rules[r].key_path,
                conf.decr_rules[r].key_pass, fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
        toprcs = 0;
        for (r = 0; r < conf.vrfy_rules_size; ++r) {
            if (((void *)0) != conf.vrfy_rules[r].sndr) {
                if (strcasestr(mails[m]->mail_from, conf.vrfy_rules[r].sndr)) {
                    toprcs = 1;
                    break;
                }
            }
        }
        if (toprcs) {
            snprintf(cmd, 512,
                "smime-tool -verify -cert %s -ca %s %s > %s.prcs",
                conf.vrfy_rules[r].cert_path, conf.vrfy_rules[r].cacert_path,
                fns[m], fns[m]);
            ret = system(cmd);
            snprintf(cmd, 512, "%s.prcs", fns[m]);
            if (0 == ret) {
                if (0 == rename(cmd, fns[m])) {
                    free_mail_object(mails[m]);
                    load_mail_from_file(fns[m], mails[m]);
                }
            }
            remove(cmd);
        }
    }
    return 0;
}

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; } **

variable_types:
fns
char **

variable_types: m int

variable_types: toprcs int

variable_types:
r
unsigned int

variable_types:
cmd
char [512]


repo: tphaster/smime-gate

decompiled_code:
__int64 __fastcall smime_gate_service(unsigned int a1)
{
const char **v1; // r12
char *v2; // r13
const char ***v3; // rbp
const char **v4; // rbx
char *v5; // r14
__int64 v6; // r15
__int64 v7; // rcx
int v9; // eax
unsigned int v10; // edx
__int64 v11; // r15
__int64 v12; // rdx
int v13; // eax
char *v14; // rdi
char *v15; // rax
char **v16; // r15
void **v17; // rbx
__int64 v18; // r14
char *v19; // rdi
char *v20; // rax
char *v21; // rdi
int v22; // [rsp+0h] [rbp-48h]
unsigned int v24; // [rsp+4h] [rbp-44h]
int v25; // [rsp+8h] [rbp-40h]
int v26; // [rsp+Ch] [rbp-3Ch]

v1 = (const char **)Calloc(10LL, 8LL);
v2 = (char *)Malloc(48LL);
v3 = (const char ***)Calloc(10LL, 8LL);
v4 = (const char **)Malloc(40LL);
v5 = generate_filename(0);
if ( !v5 )
err_sys("malloc error");
v6 = 1LL;
v7 = 0LL;
while ( 1 )
{
v25 = v6;
v22 = v6 - 1;
if ( (unsigned int)smtp_recv_mail(a1, v4, v5, v7) )
break;
v3[v6 - 1] = v4;
v1[v6 - 1] = v5;
v5 = generate_filename(v6);
if ( v5 )
{
v4 = (const char **)malloc(0x28uLL);
if ( v4 )
{
v7 = 2 * (unsigned int)((_DWORD)v6 != 10) - 1;
}
else
{
v21 = v5;
v5 = 0LL;
free(v21);
v7 = 0xFFFFFFFFLL;
}
}
else
{
v4 = 0LL;
v7 = 0xFFFFFFFFLL;
}
++v6;
}
free(v5);
free(v4);
if ( (_DWORD)v6 != 1 )
{
smime_process_mails(v3, v1, v22);
v24 = Socket(2LL, 1LL, 0LL);
v9 = connect(v24, (const struct sockaddr *)(unsigned int)((_DWORD)&conf + 104), 0x10u);
v10 = v6 - 2;
v26 = v6 - 2;
if ( v9 < 0 )
{
v16 = (char **)v1;
v17 = (void **)v3;
v18 = (__int64)&v1[v10 + 1];
do
{
v19 = *v16++;
v20 = _xpg_basename(v19);
snprintf(v2, 0x30uLL, "/var/run/smime-gate/unsent/%s", v20);
rename(*(v16 - 1), v2);
free_mail_object(*v17);
free(*v17);
free(*(v16 - 1));
++v17;
}
while ( (char **)v18 != v16 );
err_sys("connect error");
}
v11 = 0LL;
v12 = 2 * (unsigned int)(v22 == 1) + 1;
while ( 1 )
{
v13 = smtp_send_mail(v24, v3[v11], v12);
v14 = (char *)v1[v11];
if ( v13 )
{
v15 = _xpg_basename(v14);
snprintf(v2, 0x30uLL, "/var/run/smime-gate/unsent/%s", v15);
rename(v1[v11], v2);
}
else
{
remove(v14);
}
free_mail_object(v3[v11]);
free(v3[v11]);
free((void *)v1[v11]);
v12 = 2 * (unsigned int)(v25 - 3 == (_DWORD)v11);
if ( v11 == v26 )
break;
++v11;
}
}
free(v2);
free(v3);
free(v1);
return free_config();
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
const char **v1; // r12
char *v2; // r13
const char ***v3; // rbp
const char **v4; // rbx
char *v5; // r14
__int64 v6; // r15
__int64 v7; // rcx
int v9; // eax
unsigned int v10; // edx
__int64 v11; // r15
__int64 v12; // rdx
int v13; // eax
char *v14; // rdi
char *v15; // rax
char **v16; // r15
void **v17; // rbx
__int64 v18; // r14
char *v19; // rdi
char *v20; // rax
char *v21; // rdi
int v22; // [rsp+0h] [rbp-48h]
unsigned int v24; // [rsp+4h] [rbp-44h]
int v25; // [rsp+8h] [rbp-40h]
int v26; // [rsp+Ch] [rbp-3Ch]

v1 = (const char **)Calloc(10LL, 8LL);
v2 = (char *)Malloc(48LL);
v3 = (const char ***)Calloc(10LL, 8LL);
v4 = (const char **)Malloc(40LL);
v5 = func0(0);
if ( !v5 )
err_sys("malloc error");
v6 = 1LL;
v7 = 0LL;
while ( 1 )
{
v25 = v6;
v22 = v6 - 1;
if ( (unsigned int)smtp_recv_mail(a1, v4, v5, v7) )
break;
v3[v6 - 1] = v4;
v1[v6 - 1] = v5;
v5 = func0(v6);
if ( v5 )
{
v4 = (const char **)malloc(0x28uLL);
if ( v4 )
{
v7 = 2 * (unsigned int)((_DWORD)v6 != 10) - 1;
}
else
{
v21 = v5;
v5 = 0LL;
free(v21);
v7 = 0xFFFFFFFFLL;
}
}
else
{
v4 = 0LL;
v7 = 0xFFFFFFFFLL;
}
++v6;
}
free(v5);
free(v4);
if ( (_DWORD)v6 != 1 )
{
func1(v3, v1, v22);
v24 = Socket(2LL, 1LL, 0LL);
v9 = connect(v24, (const struct sockaddr *)(unsigned int)((_DWORD)&conf + 104), 0x10u);
v10 = v6 - 2;
v26 = v6 - 2;
if ( v9 < 0 )
{
v16 = (char **)v1;
v17 = (void **)v3;
v18 = (__int64)&v1[v10 + 1];
do
{
v19 = *v16++;
v20 = _xpg_basename(v19);
snprintf(v2, 0x30uLL, "/var/run/smime-gate/unsent/%s", v20);
rename(*(v16 - 1), v2);
free_mail_object(*v17);
free(*v17);
free(*(v16 - 1));
++v17;
}
while ( (char **)v18 != v16 );
err_sys("connect error");
}
v11 = 0LL;
v12 = 2 * (unsigned int)(v22 == 1) + 1;
while ( 1 )
{
v13 = smtp_send_mail(v24, v3[v11], v12);
v14 = (char *)v1[v11];
if ( v13 )
{
v15 = _xpg_basename(v14);
snprintf(v2, 0x30uLL, "/var/run/smime-gate/unsent/%s", v15);
rename(v1[v11], v2);
}
else
{
remove(v14);
}
free_mail_object(v3[v11]);
free(v3[v11]);
free((void *)v1[v11]);
v12 = 2 * (unsigned int)(v25 - 3 == (_DWORD)v11);
if ( v11 == v26 )
break;
++v11;
}
}
free(v2);
free(v3);
free(v1);
return free_config();
}

original_code:
void smime_gate_service (int sockfd)
{
    int i, srvfd;
    int srv = 0;
    int no_mails = 0;
    char **fns = Calloc(10, sizeof(char *));
    char *filename;
    char *unsent = Malloc(48);
    struct mail_object **mails = Calloc(10, sizeof(struct mail_object *));
    struct mail_object *mail = Malloc(sizeof(struct mail_object));
    if (((void *)0) == (filename = generate_filename(no_mails)))
        err_sys("malloc error");
    while (0 == smtp_recv_mail(sockfd, mail, filename, srv)) {
        mails[no_mails] = mail;
        fns[no_mails] = filename;
        ++no_mails;
        if (((void *)0) == (filename = generate_filename(no_mails))) {
            srv = -1;
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (((void *)0) == (mail = malloc(sizeof(struct mail_object)))) {
            srv = -1;
            free(filename);
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (10 == no_mails)
            srv = -1;
        else
            srv = 1;
    }
    free(filename);
    free(mail);
    filename = ((void *)0);
    mail = ((void *)0);
    if (0 == no_mails)
        goto end_service;
    smime_process_mails(mails, fns, no_mails);
    srvfd = Socket(2, SOCK_STREAM, 0);
    if (connect(srvfd, (struct sockaddr *) &(conf.mail_srv), sizeof(conf.mail_srv)) < 0) {
        for (i = 0; i < no_mails; ++i) {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
            free_mail_object(mails[i]);
            free(mails[i]);
            free(fns[i]);
        }
        err_sys("connect error");
    }
    if (1 == no_mails)
        srv = 0x1 | 0x2;
    else
        srv = 0x1 | 0x0;
    for (i = 0; i < no_mails; ++i) {
        if (0 == smtp_send_mail(srvfd, mails[i], srv)) {
            remove(fns[i]);
        }
        else {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
        }
        free_mail_object(mails[i]);
        free(mails[i]);
        free(fns[i]);
        if (no_mails-2 == i)
            srv = 0x0 | 0x2;
        else
            srv = 0x0 | 0x0;
    }
end_service:
    free(unsent);
    free(mails);
    free(fns);
    free_config();
}

canonical_original_code:
void smime_gate_service(int sockfd)
{
    int i; int srvfd;
    int srv = 0;
    int no_mails = 0;
    char **fns = Calloc(10, sizeof(char *));
    char *filename;
    char *unsent = Malloc(48);
    struct mail_object **mails = Calloc(10, sizeof(struct mail_object *));
    struct mail_object *mail = Malloc(sizeof(struct mail_object));
    if (((void *)0) == (filename = generate_filename(no_mails)))
        err_sys("malloc error");
    while (0 == smtp_recv_mail(sockfd, mail, filename, srv)) {
        mails[no_mails] = mail;
        fns[no_mails] = filename;
        ++no_mails;
        if (((void *)0) == (filename = generate_filename(no_mails))) {
            srv = -1;
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (((void *)0) == (mail = malloc(sizeof(struct mail_object)))) {
            srv = -1;
            free(filename);
            filename = ((void *)0);
            mail = ((void *)0);
            continue;
        }
        if (10 == no_mails)
            srv = -1;
        else
            srv = 1;
    }
    free(filename);
    free(mail);
    filename = ((void *)0);
    mail = ((void *)0);
    if (0 == no_mails)
        goto end_service;
    smime_process_mails(mails, fns, no_mails);
    srvfd = Socket(2, SOCK_STREAM, 0);
    if (connect(srvfd, (struct sockaddr *) &(conf.mail_srv), sizeof(conf.mail_srv)) < 0) {
        for (i = 0; i < no_mails; ++i) {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
            free_mail_object(mails[i]);
            free(mails[i]);
            free(fns[i]);
        }
        err_sys("connect error");
    }
    if (1 == no_mails)
        srv = 0x1 | 0x2;
    else
        srv = 0x1 | 0x0;
    for (i = 0; i < no_mails; ++i) {
        if (0 == smtp_send_mail(srvfd, mails[i], srv)) {
            remove(fns[i]);
        }
        else {
            snprintf(unsent, 48, "/var/run/smime-gate/unsent" "/%s", __xpg_basename(fns[i]));
            rename(fns[i], unsent);
        }
        free_mail_object(mails[i]);
        free(mails[i]);
        free(fns[i]);
        if (no_mails-2 == i)
            srv = 0x0 | 0x2;
        else
            srv = 0x0 | 0x0;
    }
end_service:
    free(unsent);
    free(mails);
    free(fns);
    free_config();
}

variable_types: sockfd int

variable_types: i int

variable_types: srvfd int

variable_types: srv int

variable_types:
filename
char *

variable_types:
unsent
char *

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; } *

return_type: void

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


decompiled_code:
void __noreturn unsent_service()
{
prctl(1, 15LL);
while ( 1 )
{
if ( (unsigned int)send_mails_from_dir("/var/run/smime-gate/unsent", (unsigned int)((_DWORD)&conf + 104)) == -1 )
err_sys("failed to open unsent directory");
sleep(0x384u);
}
}

canonical_decompiled_code:
void __noreturn func3()
{
prctl(1, 15LL);
while ( 1 )
{
if ( (unsigned int)send_mails_from_dir("/var/run/smime-gate/unsent", (unsigned int)((_DWORD)&conf + 104)) == -1 )
err_sys("failed to open unsent directory");
sleep(0x384u);
}
}

original_code:
void unsent_service (void)
{
    prctl(1, 15);
    for (;;) {
        if (-1 == send_mails_from_dir("/var/run/smime-gate/unsent", &(conf.mail_srv)) )
            err_sys("failed to open unsent directory");
        sleep(900);
    }
}

canonical_original_code:
void unsent_service()
{
    prctl(1, 15);
    for (;;) {
        if (-1 == send_mails_from_dir("/var/run/smime-gate/unsent", &(conf.mail_srv)) )
            err_sys("failed to open unsent directory");
        sleep(900);
    }
}


decompiled_code:
void __fastcall err_doit(int a1, int a2, const char *a3, void *a4)
{
int v6; // er14
size_t v7; // rbx
int v8; // edx
char *v9; // rax
char s[2]; // [rsp+0h] [rbp-1038h] BYREF

v6 = *_errno_location();
vsprintf(s, a3, a4);
v7 = strlen(s);
if ( a1 )
{
v9 = strerror(v6);
snprintf(&s[v7], 4096 - v7, ": %s", v9);
v7 = strlen(s);
}
v8 = daemon_proc;
*(_WORD *)&s[v7] = 10;
if ( v8 )
{
syslog(a2, s);
}
else
{
fflush(stdout);
fputs(s, stderr);
fflush(stderr);
}
}

canonical_decompiled_code:
void __fastcall func0(int a1, int a2, const char *a3, void *a4)
{
int v6; // er14
size_t v7; // rbx
int v8; // edx
char *v9; // rax
char s[2]; // [rsp+0h] [rbp-1038h] BYREF

v6 = *_errno_location();
vsprintf(s, a3, a4);
v7 = strlen(s);
if ( a1 )
{
v9 = strerror(v6);
snprintf(&s[v7], 4096 - v7, ": %s", v9);
v7 = strlen(s);
}
v8 = daemon_proc;
*(_WORD *)&s[v7] = 10;
if ( v8 )
{
syslog(a2, s);
}
else
{
fflush(stdout);
fputs(s, stderr);
fflush(stderr);
}
}

original_code:
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
{
    int errno_save, n;
    char buf[4096];
    errno_save = (*__errno_location ());
    vsprintf(buf, fmt, ap);
    n = strlen(buf);
    if (errnoflag)
        snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
    strcat(buf, "\n");
    if (daemon_proc) {
        syslog(level, buf);
    }
    else {
        fflush(stdout);
        fputs(buf, stderr);
        fflush(stderr);
    }
    return;
}

canonical_original_code:
static void err_doit(int errnoflag, int level, char *fmt, __builtin_va_list ap)
{
    int errno_save; int n;
    char buf[4096];
    errno_save = (*__errno_location ());
    vsprintf(buf, fmt, ap);
    n = strlen(buf);
    if (errnoflag)
        snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
    strcat(buf, "\n");
    if (daemon_proc) {
        syslog(level, buf);
    }
    else {
        fflush(stdout);
        fputs(buf, stderr);
        fflush(stderr);
    }
    return;
}

variable_types: errnoflag int

variable_types: level int

variable_types:
fmt
char *

variable_types: n int

variable_types:
buf
char [4096]


decompiled_code:
void err_ret(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
err_doit(1, 6, a1, va);
}

canonical_decompiled_code:
void func1(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func0(1, 6, a1, va);
}

original_code:
void err_ret (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}

canonical_original_code:
void err_ret(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}


decompiled_code:
void __noreturn err_sys(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
err_doit(1, 3, a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func2(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func0(1, 3, a1, va);
exit(1);
}

original_code:
void err_sys (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}

canonical_original_code:
void err_sys(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(1, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}


decompiled_code:
void err_msg(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
err_doit(0, 6, a1, va);
}

canonical_decompiled_code:
void func3(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func0(0, 6, a1, va);
}

original_code:
void err_msg (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}

canonical_original_code:
void err_msg(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 6, fmt, ap);
    __builtin_va_end(ap);
    return;
}


decompiled_code:
void __noreturn err_quit(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
err_doit(0, 3, a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func4(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func0(0, 3, a1, va);
exit(1);
}

original_code:
void err_quit (const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}

canonical_original_code:
void err_quit(char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    err_doit(0, 3, fmt, ap);
    __builtin_va_end(ap);
    exit(1);
}


readn

decompiled_code:
size_t __fastcall readn(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
v5 = nbytes;
if ( nbytes )
{
while ( 1 )
{
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( !v6 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
v3 -= v5;
}
return v3;
}

canonical_decompiled_code:
size_t __fastcall func0(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
v5 = nbytes;
if ( nbytes )
{
while ( 1 )
{
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( !v6 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
v3 -= v5;
}
return v3;
}

original_code:
ssize_t readn (int fd, void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nread = read(fd, ptr, nleft)) < 0) {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return(-1);
        }
        else if (nread == 0)
            break;
        nleft -= nread;
        ptr += nread;
    }
    return (n - nleft);
}

canonical_original_code:
long readn(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nread = read(fd, ptr, nleft)) < 0) {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return(-1);
        }
        else if (nread == 0)
            break;
        nleft -= nread;
        ptr += nread;
    }
    return (n - nleft);
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *

return_type: long


writen

decompiled_code:
size_t __fastcall writen(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
v5 = n;
if ( !n )
return v3;
do
{
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func1(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
v5 = n;
if ( !n )
return v3;
do
{
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
}
while ( *_errno_location() == 4 );
return -1LL;
}

original_code:
ssize_t writen (int fd, const void *vptr, size_t n)
{
    size_t nleft;
    ssize_t nwritten;
    const char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if ((*__errno_location ()) == 4)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return n;
}

canonical_original_code:
long writen(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nwritten;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if ((*__errno_location ()) == 4)
                nwritten = 0;
            else
                return -1;
        }
        nleft -= nwritten;
        ptr += nwritten;
    }
    return n;
}

variable_types: nwritten long


decompiled_code:
__int64 __fastcall buf_read(int fd, _BYTE *a2)
{
int v2; // edx
char *v3; // rax
__int64 v4; // rdx
char v5; // al
__int64 result; // rax

v2 = read_cnt_2;
v3 = (char *)read_ptr_0;
if ( read_cnt_2 <= 0 )
{
while ( 1 )
{
read_cnt_2 = read(fd, &read_buf_1, 0x200uLL);
v2 = read_cnt_2;
if ( read_cnt_2 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
result = 0LL;
if ( !read_cnt_2 )
return result;
v3 = (char *)&read_buf_1;
}
read_cnt_2 = v2 - 1;
v4 = (__int64)(v3 + 1);
v5 = *v3;
read_ptr_0 = v4;
*a2 = v5;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, _BYTE *a2)
{
int v2; // edx
char *v3; // rax
__int64 v4; // rdx
char v5; // al
__int64 result; // rax

v2 = read_cnt_2;
v3 = (char *)read_ptr_0;
if ( read_cnt_2 <= 0 )
{
while ( 1 )
{
read_cnt_2 = read(fd, &read_buf_1, 0x200uLL);
v2 = read_cnt_2;
if ( read_cnt_2 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
result = 0LL;
if ( !read_cnt_2 )
return result;
v3 = (char *)&read_buf_1;
}
read_cnt_2 = v2 - 1;
v4 = (__int64)(v3 + 1);
v5 = *v3;
read_ptr_0 = v4;
*a2 = v5;
return 1LL;
}

original_code:
static ssize_t buf_read (int fd, char *ptr)
{
    static int read_cnt = 0;
    static char *read_ptr;
    static char read_buf[512];
    if (read_cnt <= 0) {
again:
        if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
            if ((*__errno_location ()) == 4)
                goto again;
            return -1;
        }
        else if (read_cnt == 0)
            return 0;
        read_ptr = read_buf;
    }
    read_cnt--;
    *ptr = *read_ptr++;
    return 1;
}

canonical_original_code:
static long buf_read(int fd, char *ptr)
{
    static int read_cnt = 0;
    static char *read_ptr;
    static char read_buf[512];
    if (read_cnt <= 0) {
again:
        if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
            if ((*__errno_location ()) == 4)
                goto again;
            return -1;
        }
        else if (read_cnt == 0)
            return 0;
        read_ptr = read_buf;
    }
    read_cnt--;
    *ptr = *read_ptr++;
    return 1;
}


decompiled_code:
__int64 __fastcall smtp_send_command(__int64 a1, unsigned __int64 a2, __int64 a3)
{
size_t v3; // rbx
int v4; // eax
__int64 result; // rax
const char *v6; // r12
size_t v7; // rax
unsigned __int64 v8; // rsi
const char **v9; // rbx
const char *v10; // r12
size_t v11; // rax
char v12[256]; // [rsp+0h] [rbp-318h] BYREF
char dest[536]; // [rsp+100h] [rbp-218h] BYREF

v3 = a2 & 0xF;
switch ( a2 & 0xF )
{
case 1uLL:
case 2uLL:
if ( v3 == 1 )
*(_DWORD *)dest = 1330399301;
else
*(_DWORD *)dest = 1330398536;
strcpy(&dest[4], " ");
memset(v12, 0, sizeof(v12));
if ( gethostname(v12, 0x100uLL) != -1 )
{
strncpy(&dest[5], v12, 0x1F8uLL);
strcpy(&dest[strlen(v12) + 5], "\r\n");
goto LABEL_6;
}
return 4294967291LL;
case 3uLL:
if ( !a3 )
return 0xFFFFFFFFLL;
v6 = *(const char **)a3;
strcpy(dest, "MAIL FROM:<");
if ( strlen(v6) > 0x100 )
return 4294967290LL;
strncpy(&dest[11], v6, 0x100uLL);
v7 = strlen(v6);
if ( v7 > 0x100 )
v7 = 256LL;
*(_DWORD *)&dest[v7 + 11] = 658750;
goto LABEL_6;
case 4uLL:
if ( !a3 )
return 0xFFFFFFFFLL;
v8 = a2 >> 4;
if ( (v8 & 0x80000000) != 0LL || (unsigned __int64)(int)v8 > *(_QWORD *)(a3 + 16) )
return 4294967293LL;
strcpy(dest, "RCPT TO:<");
v9 = (const char **)(*(_QWORD *)(a3 + 8) + 8LL * (int)v8);
v10 = *v9;
if ( strlen(*v9) > 0x100 )
return 4294967290LL;
strncpy(&dest[9], v10, 0x1F8uLL);
v11 = strlen(*v9);
if ( v11 > 0x100 )
v11 = 256LL;
*(_DWORD *)&dest[v11 + 9] = 658750;
LABEL_6:
v3 = strlen(dest);
v4 = writen((unsigned int)a1, dest, v3);
LABEL_7:
if ( v4 < 0 )
result = 4294967292LL;
else
result = 4 * (unsigned int)(v4 == v3) - 4;
return result;
case 5uLL:
*(_DWORD *)dest = 1096040772;
goto LABEL_22;
case 6uLL:
strcpy(dest, "RSET\r\n");
v4 = writen(a1, dest, 6LL);
goto LABEL_7;
case 8uLL:
*(_DWORD *)dest = 1347374926;
goto LABEL_22;
case 9uLL:
*(_DWORD *)dest = 1414092113;
LABEL_22:
v3 = 6LL;
strcpy(&dest[4], "\r\n");
v4 = writen((unsigned int)a1, dest, 6LL);
goto LABEL_7;
default:
return 4294967293LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int64 a2, __int64 a3)
{
size_t v3; // rbx
int v4; // eax
__int64 result; // rax
const char *v6; // r12
size_t v7; // rax
unsigned __int64 v8; // rsi
const char **v9; // rbx
const char *v10; // r12
size_t v11; // rax
char v12[256]; // [rsp+0h] [rbp-318h] BYREF
char dest[536]; // [rsp+100h] [rbp-218h] BYREF

v3 = a2 & 0xF;
switch ( a2 & 0xF )
{
case 1uLL:
case 2uLL:
if ( v3 == 1 )
*(_DWORD *)dest = 1330399301;
else
*(_DWORD *)dest = 1330398536;
strcpy(&dest[4], " ");
memset(v12, 0, sizeof(v12));
if ( gethostname(v12, 0x100uLL) != -1 )
{
strncpy(&dest[5], v12, 0x1F8uLL);
strcpy(&dest[strlen(v12) + 5], "\r\n");
goto LABEL_6;
}
return 4294967291LL;
case 3uLL:
if ( !a3 )
return 0xFFFFFFFFLL;
v6 = *(const char **)a3;
strcpy(dest, "MAIL FROM:<");
if ( strlen(v6) > 0x100 )
return 4294967290LL;
strncpy(&dest[11], v6, 0x100uLL);
v7 = strlen(v6);
if ( v7 > 0x100 )
v7 = 256LL;
*(_DWORD *)&dest[v7 + 11] = 658750;
goto LABEL_6;
case 4uLL:
if ( !a3 )
return 0xFFFFFFFFLL;
v8 = a2 >> 4;
if ( (v8 & 0x80000000) != 0LL || (unsigned __int64)(int)v8 > *(_QWORD *)(a3 + 16) )
return 4294967293LL;
strcpy(dest, "RCPT TO:<");
v9 = (const char **)(*(_QWORD *)(a3 + 8) + 8LL * (int)v8);
v10 = *v9;
if ( strlen(*v9) > 0x100 )
return 4294967290LL;
strncpy(&dest[9], v10, 0x1F8uLL);
v11 = strlen(*v9);
if ( v11 > 0x100 )
v11 = 256LL;
*(_DWORD *)&dest[v11 + 9] = 658750;
LABEL_6:
v3 = strlen(dest);
v4 = writen((unsigned int)a1, dest, v3);
LABEL_7:
if ( v4 < 0 )
result = 4294967292LL;
else
result = 4 * (unsigned int)(v4 == v3) - 4;
return result;
case 5uLL:
*(_DWORD *)dest = 1096040772;
goto LABEL_22;
case 6uLL:
strcpy(dest, "RSET\r\n");
v4 = writen(a1, dest, 6LL);
goto LABEL_7;
case 8uLL:
*(_DWORD *)dest = 1347374926;
goto LABEL_22;
case 9uLL:
*(_DWORD *)dest = 1414092113;
LABEL_22:
v3 = 6LL;
strcpy(&dest[4], "\r\n");
v4 = writen((unsigned int)a1, dest, 6LL);
goto LABEL_7;
default:
return 4294967293LL;
}
}

original_code:
int smtp_send_command (int sockfd, size_t cmd, struct mail_object *mail)
{
    int ret;
    size_t slen;
    char domain[256], cmd_line[512];
    size_t pos = 0;
    int rcpt_no = (((cmd) & ~0xF)>>4);
    cmd = ((cmd) & 0xF);
    switch (cmd) {
        case 1:
        case 2:
            if (1 == cmd)
                strcpy(cmd_line, "EHLO ");
            else
                strcpy(cmd_line, "HELO ");
            pos = 5;
            memset(domain, 0, sizeof domain);
            if (-1 == gethostname(domain, sizeof domain))
                return -5;
            strncpy(cmd_line+pos, domain, (512 -8));
            pos += ((256) < (strlen(domain)) ? (256) : (strlen(domain)));
            strcpy(cmd_line+pos, "\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 3:
            if (((void *)0) == mail)
                return -1;
            strcpy(cmd_line, "MAIL FROM:<");
            pos = 11;
            if (strlen(mail->mail_from) > 256)
                return -6;
            strncpy(cmd_line+pos, mail->mail_from, 256);
            pos += ((256) < (strlen(mail->mail_from)) ? (256) : (strlen(mail->mail_from)));
            strcpy(cmd_line+pos, ">\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 4:
            if (((void *)0) == mail)
                return -1;
            if (rcpt_no < 0 || ((size_t) rcpt_no) > mail->no_rcpt)
                return -3;
            strcpy(cmd_line, "RCPT TO:<");
            pos = 9;
            if (strlen(mail->rcpt_to[rcpt_no]) > 256)
                return -6;
            strncpy(cmd_line+pos, mail->rcpt_to[rcpt_no], (512 -8));
            pos += ((256) < (strlen(mail->rcpt_to[rcpt_no])) ? (256) : (strlen(mail->rcpt_to[rcpt_no])));
            strcpy(cmd_line+pos, ">\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 5:
            strcpy(cmd_line, "DATA\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 6:
            strcpy(cmd_line, "RSET\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 7:
            return -3;
            break;
        case 8:
            strcpy(cmd_line, "NOOP\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 9:
            strcpy(cmd_line, "QUIT\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        default:
            return -3;
    }
    if (ret < 0 || (size_t) ret != slen)
        return -4;
    else
        return 0;
}

canonical_original_code:
int smtp_send_command(int sockfd, unsigned long cmd, struct mail_object *mail)
{
    int ret;
    unsigned long slen;
    char domain[256]; char cmd_line[512];
    unsigned long pos = 0;
    int rcpt_no = (((cmd) & ~0xF)>>4);
    cmd = ((cmd) & 0xF);
    switch (cmd) {
        case 1:
        case 2:
            if (1 == cmd)
                strcpy(cmd_line, "EHLO ");
            else
                strcpy(cmd_line, "HELO ");
            pos = 5;
            memset(domain, 0, sizeof domain);
            if (-1 == gethostname(domain, sizeof domain))
                return -5;
            strncpy(cmd_line+pos, domain, (512 -8));
            pos += ((256) < (strlen(domain)) ? (256) : (strlen(domain)));
            strcpy(cmd_line+pos, "\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 3:
            if (((void *)0) == mail)
                return -1;
            strcpy(cmd_line, "MAIL FROM:<");
            pos = 11;
            if (strlen(mail->mail_from) > 256)
                return -6;
            strncpy(cmd_line+pos, mail->mail_from, 256);
            pos += ((256) < (strlen(mail->mail_from)) ? (256) : (strlen(mail->mail_from)));
            strcpy(cmd_line+pos, ">\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 4:
            if (((void *)0) == mail)
                return -1;
            if (rcpt_no < 0 || ((unsigned long) rcpt_no) > mail->no_rcpt)
                return -3;
            strcpy(cmd_line, "RCPT TO:<");
            pos = 9;
            if (strlen(mail->rcpt_to[rcpt_no]) > 256)
                return -6;
            strncpy(cmd_line+pos, mail->rcpt_to[rcpt_no], (512 -8));
            pos += ((256) < (strlen(mail->rcpt_to[rcpt_no])) ? (256) : (strlen(mail->rcpt_to[rcpt_no])));
            strcpy(cmd_line+pos, ">\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 5:
            strcpy(cmd_line, "DATA\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 6:
            strcpy(cmd_line, "RSET\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 7:
            return -3;
            break;
        case 8:
            strcpy(cmd_line, "NOOP\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        case 9:
            strcpy(cmd_line, "QUIT\r\n");
            ret = writen(sockfd, cmd_line, slen = strlen(cmd_line));
            break;
        default:
            return -3;
    }
    if (ret < 0 || (unsigned long) ret != slen)
        return -4;
    else
        return 0;
}

variable_types: sockfd int

variable_types:
cmd
unsigned long

variable_types: ret int

variable_types:
slen
unsigned long

variable_types:
domain
char [256]

variable_types:
pos
unsigned long

return_type: int

user_defined_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; }


original_code:
int smtp_send_reply (int sockfd, size_t code, const char *msg, size_t msg_len)
{
    int ret;
    size_t slen;
    char domain[256], rply_line[512];
    size_t pos = 0;
    switch (code) {
        case 1:
            if (((void *)0) == msg) {
                strcpy(rply_line, "220 ");
                pos = 4;
                memset(domain, 0, sizeof domain);
                if (-1 == gethostname(domain, sizeof domain))
                    return -5;
                strncpy(rply_line+pos, domain, (512 -7));
                pos += ((256) < (strlen(domain)) ? (256) : (strlen(domain)));
                strcpy(rply_line+pos, " Service ready\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "220 ");
            break;
        case 2:
            if (((void *)0) == msg) {
                strcpy(rply_line, "221 closing connection, bye\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "221 ");
            break;
        case 3:
            if (((void *)0) == msg) {
                strcpy(rply_line, "250 OK\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "250 ");
            break;
        case 4:
            if (((void *)0) == msg)
                return -3;
            else
                strcpy(rply_line, "250-");
            break;
        case 5:
            if (((void *)0) == msg) {
                strcpy(rply_line, "251 User not local; "
                                  "will forward to next hop\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "251 ");
            break;
        case 6:
            if (((void *)0) == msg) {
                strcpy(rply_line, "252 cannot VRFY user, but will "
                                  "accept message and attempt delivery\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "252 ");
            break;
        case 7:
            if (((void *)0) == msg) {
                strcpy(rply_line, "354 Start mail input; "
                                  "end with <CRLF>.<CRLF>\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "354 ");
            break;
        case 8:
            if (((void *)0) == msg) {
                strcpy(rply_line, "450 Requested mail action not taken: "
                                  "mailbox unavailable\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "450 ");
            break;
        case 9:
            if (((void *)0) == msg) {
                strcpy(rply_line, "451 Requested action aborted: "
                                  "local error in processing\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "451 ");
            break;
        case 10:
            if (((void *)0) == msg) {
                strcpy(rply_line, "452 requested action not taken: "
                                  "insufficient system storage\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "452 ");
            break;
        case 11:
            if (((void *)0) == msg) {
                strcpy(rply_line, "455 Server unable to accommodate "
                                  "parameters\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "455  ");
            break;
        case 12:
            if (((void *)0) == msg) {
                strcpy(rply_line, "500 Syntax error, command unrecognized\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "500 ");
            break;
        case 13:
            if (((void *)0) == msg) {
                strcpy(rply_line, "502 Command not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "502 ");
            break;
        case 14:
            if (((void *)0) == msg) {
                strcpy(rply_line, "503 Bad sequence of commands\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "503 ");
            break;
        case 15:
            if (((void *)0) == msg) {
                strcpy(rply_line, "504 Command parameter not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "504 ");
            break;
        case 16:
            if (((void *)0) == msg) {
                strcpy(rply_line, "550 Requested action not taken: "
                                  "mailbox unavailable\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "550 ");
            break;
        case 17:
            if (((void *)0) == msg) {
                strcpy(rply_line, "551 User not local; "
                                  "please try next hop\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "551 ");
            break;
        case 18:
            if (((void *)0) == msg) {
                strcpy(rply_line, "552 Requested mail action aborted: "
                                  "exceeded storage allocation\r\n");
                ret = writen(sockfd, rply_line, slen =strlen(rply_line));
            }
            else
                strcpy(rply_line, "552 ");
            break;
        case 19:
            if (((void *)0) == msg) {
                strcpy(rply_line, "553 Requested action not taken: "
                                  "mailbox name not allowed\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
                return 0;
            }
            else
                strcpy(rply_line, "553 ");
            break;
        case 20:
            if (((void *)0) == msg) {
                strcpy(rply_line, "554 Transaction failed\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "554 ");
            break;
        case 21:
            if (((void *)0) == msg) {
                strcpy(rply_line, "555 MAIL FROM/RCPT TO parameters not "
                                  "recognized or not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "555 ");
            break;
        default:
            return -3;
    }
    if (((void *)0) != msg) {
        pos = 4;
        if (msg_len > (512 -7))
            return -3;
        strncpy(rply_line+pos, msg, ((msg_len) < ((512 -7)) ? (msg_len) : ((512 -7))));
        pos += (((512 -7)) < (msg_len) ? ((512 -7)) : (msg_len));
        strcpy(rply_line+pos, "\r\n");
        ret = writen(sockfd, rply_line, slen = strlen(rply_line));
    }
    if (ret < 0 || (size_t) ret != slen)
        return -4;
    else
        return 0;
}

canonical_original_code:
int smtp_send_reply(int sockfd, unsigned long code, char *msg, unsigned long msg_len)
{
    int ret;
    unsigned long slen;
    char domain[256]; char rply_line[512];
    unsigned long pos = 0;
    switch (code) {
        case 1:
            if (((void *)0) == msg) {
                strcpy(rply_line, "220 ");
                pos = 4;
                memset(domain, 0, sizeof domain);
                if (-1 == gethostname(domain, sizeof domain))
                    return -5;
                strncpy(rply_line+pos, domain, (512 -7));
                pos += ((256) < (strlen(domain)) ? (256) : (strlen(domain)));
                strcpy(rply_line+pos, " Service ready\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "220 ");
            break;
        case 2:
            if (((void *)0) == msg) {
                strcpy(rply_line, "221 closing connection, bye\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "221 ");
            break;
        case 3:
            if (((void *)0) == msg) {
                strcpy(rply_line, "250 OK\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "250 ");
            break;
        case 4:
            if (((void *)0) == msg)
                return -3;
            else
                strcpy(rply_line, "250-");
            break;
        case 5:
            if (((void *)0) == msg) {
                strcpy(rply_line, "251 User not local; "
                                  "will forward to next hop\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "251 ");
            break;
        case 6:
            if (((void *)0) == msg) {
                strcpy(rply_line, "252 cannot VRFY user, but will "
                                  "accept message and attempt delivery\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "252 ");
            break;
        case 7:
            if (((void *)0) == msg) {
                strcpy(rply_line, "354 Start mail input; "
                                  "end with <CRLF>.<CRLF>\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "354 ");
            break;
        case 8:
            if (((void *)0) == msg) {
                strcpy(rply_line, "450 Requested mail action not taken: "
                                  "mailbox unavailable\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "450 ");
            break;
        case 9:
            if (((void *)0) == msg) {
                strcpy(rply_line, "451 Requested action aborted: "
                                  "local error in processing\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "451 ");
            break;
        case 10:
            if (((void *)0) == msg) {
                strcpy(rply_line, "452 requested action not taken: "
                                  "insufficient system storage\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "452 ");
            break;
        case 11:
            if (((void *)0) == msg) {
                strcpy(rply_line, "455 Server unable to accommodate "
                                  "parameters\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "455  ");
            break;
        case 12:
            if (((void *)0) == msg) {
                strcpy(rply_line, "500 Syntax error, command unrecognized\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "500 ");
            break;
        case 13:
            if (((void *)0) == msg) {
                strcpy(rply_line, "502 Command not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "502 ");
            break;
        case 14:
            if (((void *)0) == msg) {
                strcpy(rply_line, "503 Bad sequence of commands\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "503 ");
            break;
        case 15:
            if (((void *)0) == msg) {
                strcpy(rply_line, "504 Command parameter not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "504 ");
            break;
        case 16:
            if (((void *)0) == msg) {
                strcpy(rply_line, "550 Requested action not taken: "
                                  "mailbox unavailable\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "550 ");
            break;
        case 17:
            if (((void *)0) == msg) {
                strcpy(rply_line, "551 User not local; "
                                  "please try next hop\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "551 ");
            break;
        case 18:
            if (((void *)0) == msg) {
                strcpy(rply_line, "552 Requested mail action aborted: "
                                  "exceeded storage allocation\r\n");
                ret = writen(sockfd, rply_line, slen =strlen(rply_line));
            }
            else
                strcpy(rply_line, "552 ");
            break;
        case 19:
            if (((void *)0) == msg) {
                strcpy(rply_line, "553 Requested action not taken: "
                                  "mailbox name not allowed\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
                return 0;
            }
            else
                strcpy(rply_line, "553 ");
            break;
        case 20:
            if (((void *)0) == msg) {
                strcpy(rply_line, "554 Transaction failed\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "554 ");
            break;
        case 21:
            if (((void *)0) == msg) {
                strcpy(rply_line, "555 MAIL FROM/RCPT TO parameters not "
                                  "recognized or not implemented\r\n");
                ret = writen(sockfd, rply_line, slen = strlen(rply_line));
            }
            else
                strcpy(rply_line, "555 ");
            break;
        default:
            return -3;
    }
    if (((void *)0) != msg) {
        pos = 4;
        if (msg_len > (512 -7))
            return -3;
        strncpy(rply_line+pos, msg, ((msg_len) < ((512 -7)) ? (msg_len) : ((512 -7))));
        pos += (((512 -7)) < (msg_len) ? ((512 -7)) : (msg_len));
        strcpy(rply_line+pos, "\r\n");
        ret = writen(sockfd, rply_line, slen = strlen(rply_line));
    }
    if (ret < 0 || (unsigned long) ret != slen)
        return -4;
    else
        return 0;
}

variable_types:
code
unsigned long

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall smtp_readline(int fd, _BYTE *a2, unsigned __int64 a3)
{
_BYTE *v3; // r14
unsigned int v5; // er13
_BOOL4 v6; // ebx
char v7; // al
_BYTE *v8; // rdx
__int64 result; // rax
int v10; // eax
char v11[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a2;
if ( a3 <= 1 )
{
v8 = a2;
result = 1LL;
}
else
{
v5 = 1;
v6 = 0;
while ( 1 )
{
v10 = buf_read(fd, v11);
if ( v10 != 1 )
break;
v7 = v11[0];
v8 = v3 + 1;
*v3 = v11[0];
if ( v7 == 10 && v6 )
{
v8 = v3 - 1;
result = v5;
goto LABEL_11;
}
v6 = v7 == 13;
result = ++v5;
if ( v5 >= a3 )
goto LABEL_11;
++v3;
}
if ( v10 )
return -1LL;
result = 0LL;
if ( v5 == 1 )
return result;
result = v5;
v8 = v3;
}
LABEL_11:
*v8 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, _BYTE *a2, unsigned __int64 a3)
{
_BYTE *v3; // r14
unsigned int v5; // er13
_BOOL4 v6; // ebx
char v7; // al
_BYTE *v8; // rdx
__int64 result; // rax
int v10; // eax
char v11[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a2;
if ( a3 <= 1 )
{
v8 = a2;
result = 1LL;
}
else
{
v5 = 1;
v6 = 0;
while ( 1 )
{
v10 = func0(fd, v11);
if ( v10 != 1 )
break;
v7 = v11[0];
v8 = v3 + 1;
*v3 = v11[0];
if ( v7 == 10 && v6 )
{
v8 = v3 - 1;
result = v5;
goto LABEL_11;
}
v6 = v7 == 13;
result = ++v5;
if ( v5 >= a3 )
goto LABEL_11;
++v3;
}
if ( v10 )
return -1LL;
result = 0LL;
if ( v5 == 1 )
return result;
result = v5;
v8 = v3;
}
LABEL_11:
*v8 = 0;
return result;
}

original_code:
ssize_t smtp_readline (int fd, void *vptr, size_t maxlen)
{
    int rc, state;
    unsigned int n;
    char c, *ptr;
    ptr = vptr;
    state = 0;
    for (n = 1; n < maxlen; n++) {
        if ( (rc = buf_read(fd, &c)) == 1) {
            *ptr++ = c;
            if (1 == state) {
                if (c == '\n') {
                    ptr -= 2;
                    break;
                }
                else
                    state = 0;
            }
            if (c == '\r')
                state = 1;
        }
        else if (rc == 0) {
            if (n == 1)
                return 0;
            else
                break;
        }
        else
            return -1;
    }
    *ptr = 0;
    return n;
}

canonical_original_code:
long smtp_readline(int fd, void *vptr, unsigned long maxlen)
{
    int rc; int state;
    unsigned int n;
    char c; char *ptr;
    ptr = vptr;
    state = 0;
    for (n = 1; n < maxlen; n++) {
        if ( (rc = buf_read(fd, &c)) == 1) {
            *ptr++ = c;
            if (1 == state) {
                if (c == '\n') {
                    ptr -= 2;
                    break;
                }
                else
                    state = 0;
            }
            if (c == '\r')
                state = 1;
        }
        else if (rc == 0) {
            if (n == 1)
                return 0;
            else
                break;
        }
        else
            return -1;
    }
    *ptr = 0;
    return n;
}

variable_types:
maxlen
unsigned long

variable_types: rc int

variable_types: state int

variable_types:
n
unsigned int

variable_types: c char


decompiled_code:
__int64 __fastcall smtp_recv_command(int a1, __int64 a2)
{
size_t v2; // r13
unsigned int v3; // er12
char v5[6]; // [rsp+1h] [rbp-229h]
char v6[5]; // [rsp+2h] [rbp-228h] BYREF
char v7[4]; // [rsp+7h] [rbp-223h] BYREF
char v8[2]; // [rsp+Bh] [rbp-21Fh] BYREF
char src[541]; // [rsp+Dh] [rbp-21Dh] BYREF

if ( !a2 )
return (unsigned int)-1;
if ( smtp_readline(a1, v6, 0x200uLL) <= 0 )
return (unsigned int)-2;
*(_QWORD *)(a2 + 2) = 0LL;
*(_QWORD *)(a2 + 498) = 0LL;
memset(
(void *)((a2 + 10) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)a2 + 2 - (((_DWORD)a2 + 10) & 0xFFFFFFF8) + 504) >> 3));
v2 = strlen(v6);
if ( v2 <= 3 )
{
v3 = 2;
*(_WORD *)a2 = 0;
return v3;
}
v3 = strncasecmp("HELO ", v6, 5uLL);
if ( !v3 )
{
*(_WORD *)a2 = 2;
if ( v2 <= 5 )
return 1;
LABEL_15:
strncpy((char *)(a2 + 2), v7, 0x100uLL);
return v3;
}
v3 = strncasecmp("EHLO ", v6, 5uLL);
if ( !v3 )
{
*(_WORD *)a2 = 1;
if ( v2 <= 5 )
return 1;
goto LABEL_15;
}
v3 = strncasecmp("MAIL FROM:", v6, 0xAuLL);
if ( v3 )
{
v3 = strncasecmp("RCPT TO:", v6, 8uLL);
if ( !v3 )
{
*(_WORD *)a2 = 4;
if ( v2 > 0xC && v7[3] == 60 && v5[v2] == 62 )
{
v5[v2] = 0;
strncpy((char *)(a2 + 2), v8, 0x100uLL);
return v3;
}
return 1;
}
if ( v2 == 4 )
{
v3 = strncasecmp("DATA", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 5;
return v3;
}
v3 = strncasecmp("RSET", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 6;
return v3;
}
if ( strncasecmp("VRFY ", v6, 5uLL) )
{
if ( !strncasecmp("NOOP", v6, 4uLL) || !strncasecmp("NOOP ", v6, 5uLL) )
goto LABEL_25;
v3 = strncasecmp("QUIT", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 9;
return v3;
}
goto LABEL_38;
}
}
else if ( strncasecmp("VRFY ", v6, 5uLL) )
{
if ( !strncasecmp("NOOP ", v6, 5uLL) )
{
LABEL_25:
v3 = 0;
*(_WORD *)a2 = 8;
return v3;
}
LABEL_38:
*(_WORD *)a2 = 0;
v3 = 2;
strncpy((char *)(a2 + 2), v6, 0x200uLL);
return v3;
}
v3 = 0;
*(_WORD *)a2 = 7;
return v3;
}
*(_WORD *)a2 = 3;
if ( v2 > 0xE && v8[1] == 60 && v5[v2] == 62 )
{
v5[v2] = 0;
strncpy((char *)(a2 + 2), src, 0x100uLL);
return v3;
}
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, __int64 a2)
{
size_t v2; // r13
unsigned int v3; // er12
char v5[6]; // [rsp+1h] [rbp-229h]
char v6[5]; // [rsp+2h] [rbp-228h] BYREF
char v7[4]; // [rsp+7h] [rbp-223h] BYREF
char v8[2]; // [rsp+Bh] [rbp-21Fh] BYREF
char src[541]; // [rsp+Dh] [rbp-21Dh] BYREF

if ( !a2 )
return (unsigned int)-1;
if ( func3(a1, v6, 0x200uLL) <= 0 )
return (unsigned int)-2;
*(_QWORD *)(a2 + 2) = 0LL;
*(_QWORD *)(a2 + 498) = 0LL;
memset(
(void *)((a2 + 10) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((_DWORD)a2 + 2 - (((_DWORD)a2 + 10) & 0xFFFFFFF8) + 504) >> 3));
v2 = strlen(v6);
if ( v2 <= 3 )
{
v3 = 2;
*(_WORD *)a2 = 0;
return v3;
}
v3 = strncasecmp("HELO ", v6, 5uLL);
if ( !v3 )
{
*(_WORD *)a2 = 2;
if ( v2 <= 5 )
return 1;
LABEL_15:
strncpy((char *)(a2 + 2), v7, 0x100uLL);
return v3;
}
v3 = strncasecmp("EHLO ", v6, 5uLL);
if ( !v3 )
{
*(_WORD *)a2 = 1;
if ( v2 <= 5 )
return 1;
goto LABEL_15;
}
v3 = strncasecmp("MAIL FROM:", v6, 0xAuLL);
if ( v3 )
{
v3 = strncasecmp("RCPT TO:", v6, 8uLL);
if ( !v3 )
{
*(_WORD *)a2 = 4;
if ( v2 > 0xC && v7[3] == 60 && v5[v2] == 62 )
{
v5[v2] = 0;
strncpy((char *)(a2 + 2), v8, 0x100uLL);
return v3;
}
return 1;
}
if ( v2 == 4 )
{
v3 = strncasecmp("DATA", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 5;
return v3;
}
v3 = strncasecmp("RSET", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 6;
return v3;
}
if ( strncasecmp("VRFY ", v6, 5uLL) )
{
if ( !strncasecmp("NOOP", v6, 4uLL) || !strncasecmp("NOOP ", v6, 5uLL) )
goto LABEL_25;
v3 = strncasecmp("QUIT", v6, 4uLL);
if ( !v3 )
{
*(_WORD *)a2 = 9;
return v3;
}
goto LABEL_38;
}
}
else if ( strncasecmp("VRFY ", v6, 5uLL) )
{
if ( !strncasecmp("NOOP ", v6, 5uLL) )
{
LABEL_25:
v3 = 0;
*(_WORD *)a2 = 8;
return v3;
}
LABEL_38:
*(_WORD *)a2 = 0;
v3 = 2;
strncpy((char *)(a2 + 2), v6, 0x200uLL);
return v3;
}
v3 = 0;
*(_WORD *)a2 = 7;
return v3;
}
*(_WORD *)a2 = 3;
if ( v2 > 0xE && v8[1] == 60 && v5[v2] == 62 )
{
v5[v2] = 0;
strncpy((char *)(a2 + 2), src, 0x100uLL);
return v3;
}
return 1;
}

original_code:
int smtp_recv_command (int sockfd, struct smtp_command *cmd)
{
    size_t len;
    char line[512];
    if (((void *)0) == cmd)
        return -1;
    if (smtp_readline(sockfd, line, 512) <= 0)
        return -2;
    memset(cmd->data, 0, sizeof(cmd->data));
    if ((len = strlen(line)) < 4) {
        cmd->code = 0;
        return 2;
    }
    if (0 == strncasecmp("HELO ", line, 5)) {
        cmd->code = 2;
        if (len < 6)
            return 1;
        strncpy(cmd->data, line+5, 256);
    }
    else if (0 == strncasecmp("EHLO ", line, 5)) {
        cmd->code = 1;
        if (len < 6)
            return 1;
        strncpy(cmd->data, line+5, 256);
    }
    else if (0 == strncasecmp("MAIL FROM:", line, 10)) {
        cmd->code = 3;
        if (len < 15 || '<' != line[10] || '>' != line[len-1])
            return 1;
        line[len-1] = '\0';
        strncpy(cmd->data, line+11, 256);
    }
    else if (0 == strncasecmp("RCPT TO:", line, 8)) {
        cmd->code = 4;
        if (len < 13 || '<' != line[8] || '>' != line[len-1])
            return 1;
        line[len-1] = '\0';
        strncpy(cmd->data, line+9, 256);
    }
    else if (4 == len && 0 == strncasecmp("DATA", line, 4))
        cmd->code = 5;
    else if (4 == len && 0 == strncasecmp("RSET", line, 4))
        cmd->code = 6;
    else if (0 == strncasecmp("VRFY ", line, 5))
        cmd->code = 7;
    else if ( (4 == len && 0 == strncasecmp("NOOP", line, 4))
            || 0 == strncasecmp("NOOP ", line, 5) )
        cmd->code = 8;
    else if (4 == len && 0 == strncasecmp("QUIT", line, 4))
        cmd->code = 9;
    else {
        cmd->code = 0;
        strncpy(cmd->data, line, 512);
        return 2;
    }
    return 0;
}

canonical_original_code:
int smtp_recv_command(int sockfd, struct smtp_command *cmd)
{
    unsigned long len;
    char line[512];
    if (((void *)0) == cmd)
        return -1;
    if (smtp_readline(sockfd, line, 512) <= 0)
        return -2;
    memset(cmd->data, 0, sizeof(cmd->data));
    if ((len = strlen(line)) < 4) {
        cmd->code = 0;
        return 2;
    }
    if (0 == strncasecmp("HELO ", line, 5)) {
        cmd->code = 2;
        if (len < 6)
            return 1;
        strncpy(cmd->data, line+5, 256);
    }
    else if (0 == strncasecmp("EHLO ", line, 5)) {
        cmd->code = 1;
        if (len < 6)
            return 1;
        strncpy(cmd->data, line+5, 256);
    }
    else if (0 == strncasecmp("MAIL FROM:", line, 10)) {
        cmd->code = 3;
        if (len < 15 || '<' != line[10] || '>' != line[len-1])
            return 1;
        line[len-1] = '\0';
        strncpy(cmd->data, line+11, 256);
    }
    else if (0 == strncasecmp("RCPT TO:", line, 8)) {
        cmd->code = 4;
        if (len < 13 || '<' != line[8] || '>' != line[len-1])
            return 1;
        line[len-1] = '\0';
        strncpy(cmd->data, line+9, 256);
    }
    else if (4 == len && 0 == strncasecmp("DATA", line, 4))
        cmd->code = 5;
    else if (4 == len && 0 == strncasecmp("RSET", line, 4))
        cmd->code = 6;
    else if (0 == strncasecmp("VRFY ", line, 5))
        cmd->code = 7;
    else if ( (4 == len && 0 == strncasecmp("NOOP", line, 4))
            || 0 == strncasecmp("NOOP ", line, 5) )
        cmd->code = 8;
    else if (4 == len && 0 == strncasecmp("QUIT", line, 4))
        cmd->code = 9;
    else {
        cmd->code = 0;
        strncpy(cmd->data, line, 512);
        return 2;
    }
    return 0;
}

variable_types: struct smtp_command { unsigned short code; char data[-1]; } *

variable_types:
len
unsigned long

variable_types:
line
char [512]


original_code:
int smtp_recv_reply (int sockfd, struct smtp_reply *rply)
{
    char line[512];
    if (((void *)0) == rply)
        return -1;
    if (smtp_readline(sockfd, line, 512) <= 0)
        return -2;
    memset(rply, 0, sizeof(*rply));
    if (0 == strncmp("220", line, 3))
        rply->code = 1;
    else if (0 == strncmp("221", line, 3))
        rply->code = 2;
    else if (0 == strncmp("250-", line, 4))
        rply->code = 4;
    else if (0 == strncmp("250", line, 3))
        rply->code = 3;
    else if (0 == strncmp("251", line, 3))
        rply->code = 5;
    else if (0 == strncmp("252", line, 3))
        rply->code = 6;
    else if (0 == strncmp("354", line, 3))
        rply->code = 7;
    else if (0 == strncmp("450", line, 3))
        rply->code = 8;
    else if (0 == strncmp("451", line, 3))
        rply->code = 9;
    else if (0 == strncmp("452", line, 3))
        rply->code = 10;
    else if (0 == strncmp("455", line, 3))
        rply->code = 11;
    else if (0 == strncmp("500", line, 3))
        rply->code = 12;
    else if (0 == strncmp("502", line, 3))
        rply->code = 13;
    else if (0 == strncmp("503", line, 3))
        rply->code = 14;
    else if (0 == strncmp("504", line, 3))
        rply->code = 15;
    else if (0 == strncmp("550", line, 3))
        rply->code = 16;
    else if (0 == strncmp("551", line, 3))
        rply->code = 17;
    else if (0 == strncmp("552", line, 3))
        rply->code = 18;
    else if (0 == strncmp("553", line, 3))
        rply->code = 19;
    else if (0 == strncmp("554", line, 3))
        rply->code = 20;
    else if (0 == strncmp("555", line, 3))
        rply->code = 21;
    else {
        rply->code = 0;
        strncpy(rply->msg, line, (512 -7));
        return 3;
    }
    if (strlen(line) > 4)
        strncpy(rply->msg, line+4, (512 -7));
    return 0;
}

canonical_original_code:
int smtp_recv_reply(int sockfd, struct smtp_reply *rply)
{
    char line[512];
    if (((void *)0) == rply)
        return -1;
    if (smtp_readline(sockfd, line, 512) <= 0)
        return -2;
    memset(rply, 0, sizeof(*rply));
    if (0 == strncmp("220", line, 3))
        rply->code = 1;
    else if (0 == strncmp("221", line, 3))
        rply->code = 2;
    else if (0 == strncmp("250-", line, 4))
        rply->code = 4;
    else if (0 == strncmp("250", line, 3))
        rply->code = 3;
    else if (0 == strncmp("251", line, 3))
        rply->code = 5;
    else if (0 == strncmp("252", line, 3))
        rply->code = 6;
    else if (0 == strncmp("354", line, 3))
        rply->code = 7;
    else if (0 == strncmp("450", line, 3))
        rply->code = 8;
    else if (0 == strncmp("451", line, 3))
        rply->code = 9;
    else if (0 == strncmp("452", line, 3))
        rply->code = 10;
    else if (0 == strncmp("455", line, 3))
        rply->code = 11;
    else if (0 == strncmp("500", line, 3))
        rply->code = 12;
    else if (0 == strncmp("502", line, 3))
        rply->code = 13;
    else if (0 == strncmp("503", line, 3))
        rply->code = 14;
    else if (0 == strncmp("504", line, 3))
        rply->code = 15;
    else if (0 == strncmp("550", line, 3))
        rply->code = 16;
    else if (0 == strncmp("551", line, 3))
        rply->code = 17;
    else if (0 == strncmp("552", line, 3))
        rply->code = 18;
    else if (0 == strncmp("553", line, 3))
        rply->code = 19;
    else if (0 == strncmp("554", line, 3))
        rply->code = 20;
    else if (0 == strncmp("555", line, 3))
        rply->code = 21;
    else {
        rply->code = 0;
        strncpy(rply->msg, line, (512 -7));
        return 3;
    }
    if (strlen(line) > 4)
        strncpy(rply->msg, line+4, (512 -7));
    return 0;
}

variable_types: struct smtp_reply { unsigned short code; char msg[-1]; } *


Calloc

decompiled_code:
void *__fastcall Calloc(size_t a1, size_t a2)
{
void *v2; // r12

v2 = calloc(a1, a2);
if ( !v2 )
err_sys("calloc error");
return v2;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1, size_t a2)
{
void *v2; // r12

v2 = calloc(a1, a2);
if ( !v2 )
err_sys("calloc error");
return v2;
}

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

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

variable_types:
n
unsigned long

variable_types:
size
unsigned long

variable_types:
ptr
void *

return_type: void *


Close

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

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

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

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

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

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

variable_types: fd int


Fork

decompiled_code:
__int64 Fork()
{
unsigned int v0; // er12

v0 = fork();
if ( v0 != -1 )
return v0;
err_sys("fork error");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // er12

v0 = fork();
if ( v0 != -1 )
return v0;
err_sys("fork error");
return 0xFFFFFFFFLL;
}

original_code:
pid_t Fork(void)
{
    pid_t pid;
    if ( (pid = fork()) == -1)
        err_sys("fork error");
    return pid;
}

canonical_original_code:
int Fork()
{
    int pid;
    if ( (pid = fork()) == -1)
        err_sys("fork error");
    return pid;
}

variable_types: pid int


Malloc

decompiled_code:
void *__fastcall Malloc(size_t a1)
{
void *v1; // r12

v1 = malloc(a1);
if ( !v1 )
err_sys("malloc error");
return v1;
}

canonical_decompiled_code:
void *__fastcall func3(size_t a1)
{
void *v1; // r12

v1 = malloc(a1);
if ( !v1 )
err_sys("malloc error");
return v1;
}

original_code:
void *Malloc (size_t size)
{
    void *ptr;
    if ( (ptr = malloc(size)) == ((void *)0))
        err_sys("malloc error");
    return ptr;
}

canonical_original_code:
void *Malloc(unsigned long size)
{
    void *ptr;
    if ( (ptr = malloc(size)) == ((void *)0))
        err_sys("malloc error");
    return ptr;
}


Bind

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

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

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

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

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

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

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

variable_types:
salen
unsigned int


Connect

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

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

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

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

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

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


Listen

decompiled_code:
int __fastcall Listen(int fd, int n)
{
char *v3; // rax
int result; // eax

v3 = getenv("LISTENQ");
if ( v3 )
n = atoi(v3);
result = listen(fd, n);
if ( result < 0 )
result = err_sys("listen error");
return result;
}

canonical_decompiled_code:
int __fastcall func2(int fd, int n)
{
char *v3; // rax
int result; // eax

v3 = getenv("LISTENQ");
if ( v3 )
n = atoi(v3);
result = listen(fd, n);
if ( result < 0 )
result = err_sys("listen error");
return result;
}

original_code:
void Listen (int fd, int backlog)
{
    char *ptr;
    if ( (ptr = getenv("LISTENQ")) != ((void *)0))
        backlog = atoi(ptr);
    if (listen(fd, backlog) < 0)
        err_sys("listen error");
}

canonical_original_code:
void Listen(int fd, int backlog)
{
    char *ptr;
    if ( (ptr = getenv("LISTENQ")) != ((void *)0))
        backlog = atoi(ptr);
    if (listen(fd, backlog) < 0)
        err_sys("listen error");
}

variable_types: backlog int

variable_types:
ptr
char *


Socket

decompiled_code:
__int64 __fastcall Socket(int a1, int a2, int a3)
{
int v3; // er12

v3 = socket(a1, a2, a3);
if ( v3 < 0 )
err_sys("socket error");
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2, int a3)
{
int v3; // er12

v3 = socket(a1, a2, a3);
if ( v3 < 0 )
err_sys("socket error");
return (unsigned int)v3;
}

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

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

variable_types: family int

variable_types: type int

variable_types: protocol int

variable_types: n int


signal

decompiled_code:
__int64 __fastcall signal(int sig, void (*a2)(int))
{
int v2; // eax
bool v3; // sf
__int64 result; // rax
struct sigaction v5; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-A8h] BYREF

v5.sa_handler = a2;
sigemptyset(&v5.sa_mask);
v2 = 0x10000000;
if ( sig == 14 )
v2 = 0x20000000;
v5.sa_flags = v2;
v3 = sigaction(sig, &v5, &v6) < 0;
result = -1LL;
if ( !v3 )
result = (__int64)v6.sa_handler;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int sig, void (*a2)(int))
{
int v2; // eax
bool v3; // sf
__int64 result; // rax
struct sigaction v5; // [rsp+0h] [rbp-148h] BYREF
struct sigaction v6; // [rsp+A0h] [rbp-A8h] BYREF

v5.sa_handler = a2;
sigemptyset(&v5.sa_mask);
v2 = 0x10000000;
if ( sig == 14 )
v2 = 0x20000000;
v5.sa_flags = v2;
v3 = sigaction(sig, &v5, &v6) < 0;
result = -1LL;
if ( !v3 )
result = (__int64)v6.sa_handler;
return result;
}

original_code:
Sigfunc *signal (int signo, Sigfunc *func)
{
    struct sigaction act, oact;
    act.__sigaction_handler.sa_handler = func;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    if (signo == 14) {
        act.sa_flags |= 0x20000000;
    }
    else {
        act.sa_flags |= 0x10000000;
    }
    if (sigaction(signo, &act, &oact) < 0)
        return ((__sighandler_t) -1);
    return oact.__sigaction_handler.sa_handler;
}

canonical_original_code:
void (*signal(int signo, void (*func)(int)))(int)
{
    struct sigaction act; struct sigaction oact;
    act.__sigaction_handler.sa_handler = func;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    if (signo == 14) {
        act.sa_flags |= 0x20000000;
    }
    else {
        act.sa_flags |= 0x10000000;
    }
    if (sigaction(signo, &act, &oact) < 0)
        return ((__sighandler_t) -1);
    return oact.__sigaction_handler.sa_handler;
}

variable_types: signo int

variable_types:
func
void (*)(int)

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

return_type: void (*)(int)

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


Signal

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

result = signal(a1, a2);
if ( result == -1 )
{
err_sys("signal error");
result = -1LL;
}
return result;
}

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

result = func0(a1, a2);
if ( result == -1 )
{
err_sys("signal error");
result = -1LL;
}
return result;
}

original_code:
Sigfunc *Signal (int signo, Sigfunc *func)
{
    Sigfunc *sigfunc;
    if ( (sigfunc = signal(signo, func)) == ((__sighandler_t) -1))
        err_sys("signal error");
    return sigfunc;
}

canonical_original_code:
void (*Signal(int signo, void (*func)(int)))(int)
{
    void (*sigfunc)(int);
    if ( (sigfunc = signal(signo, func)) == ((__sighandler_t) -1))
        err_sys("signal error");
    return sigfunc;
}

variable_types:
sigfunc
void (*)(int)


decompiled_code:
__pid_t sig_chld()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = waitpid(-1, stat_loc, 1);
if ( result <= 0 )
break;
--sproc_counter;
}
return result;
}

canonical_decompiled_code:
__pid_t func2()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = waitpid(-1, stat_loc, 1);
if ( result <= 0 )
break;
--sproc_counter;
}
return result;
}

original_code:
void sig_chld (int signo __attribute__((__unused__)))
{
    pid_t pid;
    int stat;
    while ( (pid = waitpid(-1, &stat, 1)) > 0) {
        --sproc_counter;
    }
    return;
}

canonical_original_code:
void sig_chld(int signo)
{
    int pid;
    int stat;
    while ( (pid = waitpid(-1, &stat, 1)) > 0) {
        --sproc_counter;
    }
    return;
}

variable_types: stat int


original_code:
void free_mail_object (struct mail_object *mail)
{
    unsigned int i;
    if (((void *)0) != mail->mail_from) {
        free(mail->mail_from);
        mail->mail_from = ((void *)0);
    }
    if (((void *)0) != mail->rcpt_to) {
        for (i = 0; i < mail->no_rcpt; ++i) {
            if (((void *)0) != mail->rcpt_to[i])
                free(mail->rcpt_to[i]);
        }
        free(mail->rcpt_to);
        mail->rcpt_to = ((void *)0);
    }
    if (((void *)0) != mail->data)
        free(mail->data);
    memset(mail, 0, sizeof(struct mail_object));
}

canonical_original_code:
void free_mail_object(struct mail_object *mail)
{
    unsigned int i;
    if (((void *)0) != mail->mail_from) {
        free(mail->mail_from);
        mail->mail_from = ((void *)0);
    }
    if (((void *)0) != mail->rcpt_to) {
        for (i = 0; i < mail->no_rcpt; ++i) {
            if (((void *)0) != mail->rcpt_to[i])
                free(mail->rcpt_to[i]);
        }
        free(mail->rcpt_to);
        mail->rcpt_to = ((void *)0);
    }
    if (((void *)0) != mail->data)
        free(mail->data);
    memset(mail, 0, sizeof(struct mail_object));
}

variable_types: struct mail_object { char *mail_from; char **rcpt_to; unsigned long no_rcpt; char *data; unsigned long data_size; } *

variable_types:
i
unsigned int


decompiled_code:
void __fastcall print_mail_object(__int64 a1)
{
unsigned int v1; // ebp
__int64 v2; // rax

if ( a1 )
{
fwrite("=== MAIL OBJECT ===\n", 1uLL, 0x14uLL, stderr);
fprintf(stderr, "FROM: %s\n", *(const char **)a1);
if ( *(_QWORD *)(a1 + 16) )
{
v1 = 0;
v2 = 0LL;
do
{
fprintf(stderr, "TO: %s\n", *(const char **)(*(_QWORD *)(a1 + 8) + 8 * v2));
v2 = ++v1;
}
while ( (unsigned __int64)v1 < *(_QWORD *)(a1 + 16) );
}
fprintf(
stderr,
"\nDATA (size: %u octets)\n%s=== END OF MAIL ===\n",
*(_QWORD *)(a1 + 32),
*(const char **)(a1 + 24));
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
unsigned int v1; // ebp
__int64 v2; // rax

if ( a1 )
{
fwrite("=== MAIL OBJECT ===\n", 1uLL, 0x14uLL, stderr);
fprintf(stderr, "FROM: %s\n", *(const char **)a1);
if ( *(_QWORD *)(a1 + 16) )
{
v1 = 0;
v2 = 0LL;
do
{
fprintf(stderr, "TO: %s\n", *(const char **)(*(_QWORD *)(a1 + 8) + 8 * v2));
v2 = ++v1;
}
while ( (unsigned __int64)v1 < *(_QWORD *)(a1 + 16) );
}
fprintf(
stderr,
"\nDATA (size: %u octets)\n%s=== END OF MAIL ===\n",
*(_QWORD *)(a1 + 32),
*(const char **)(a1 + 24));
}
}

original_code:
void print_mail_object (struct mail_object *mail)
{
    unsigned int i;
    if (((void *)0) == mail)
        return;
    fprintf(stderr, "=== MAIL OBJECT ===\n");
    fprintf(stderr, "FROM: %s\n", mail->mail_from);
    for (i = 0; i < mail->no_rcpt; ++i)
        fprintf(stderr, "TO:   %s\n", mail->rcpt_to[i]);
    fprintf(stderr, "\nDATA (size: %u octets)\n%s=== END OF MAIL ===\n",
            (unsigned int)mail->data_size, mail->data);
}

canonical_original_code:
void print_mail_object(struct mail_object *mail)
{
    unsigned int i;
    if (((void *)0) == mail)
        return;
    fprintf(stderr, "=== MAIL OBJECT ===\n");
    fprintf(stderr, "FROM: %s\n", mail->mail_from);
    for (i = 0; i < mail->no_rcpt; ++i)
        fprintf(stderr, "TO:   %s\n", mail->rcpt_to[i]);
    fprintf(stderr, "\nDATA (size: %u octets)\n%s=== END OF MAIL ===\n",
            (unsigned int)mail->data_size, mail->data);
}


daemonize

decompiled_code:
void __fastcall daemonize(char *ident, int facility)
{
int v3; // ebx
int v4; // edi

if ( (unsigned int)Fork() || (setsid(), Signal(1LL, 1LL), (v3 = Fork()) != 0) )
exit(0);
daemon_proc = 1;
chdir("/");
umask(0);
do
{
v4 = v3++;
close(v4);
}
while ( v3 != 64 );
openlog(ident, 1, facility);
}

canonical_decompiled_code:
void __fastcall func0(char *ident, int facility)
{
int v3; // ebx
int v4; // edi

if ( (unsigned int)Fork() || (setsid(), Signal(1LL, 1LL), (v3 = Fork()) != 0) )
exit(0);
daemon_proc = 1;
chdir("/");
umask(0);
do
{
v4 = v3++;
close(v4);
}
while ( v3 != 64 );
openlog(ident, 1, facility);
}

original_code:
void daemonize (const char *pname, int facility)
{
    int i;
    pid_t pid;
    if ( (pid = Fork()) != 0)
        exit(0);
    setsid();
    Signal(1, ((__sighandler_t) 1));
    if ( (pid = Fork()) != 0)
        exit(0);
    daemon_proc = 1;
    chdir("/");
    umask(0);
    for (i = 0; i < 64; ++i)
        close(i);
    openlog(pname, 0x01, facility);
}

canonical_original_code:
void daemonize(char *pname, int facility)
{
    int i;
    int pid;
    if ( (pid = Fork()) != 0)
        exit(0);
    setsid();
    Signal(1, ((void (*)(int)) 1));
    if ( (pid = Fork()) != 0)
        exit(0);
    daemon_proc = 1;
    chdir("/");
    umask(0);
    for (i = 0; i < 64; ++i)
        close(i);
    openlog(pname, 0x01, facility);
}

variable_types:
pname
char *

variable_types: facility int

variable_types: i int


repo: tracymacding/GtmyMusic

decompiled_code:
__int64 __fastcall send_response(int fd, __int64 a2, int a3)
{
__int64 result; // rax

if ( fd <= 0 )
_assert_fail("fd > 0", "main.c", 0x40u, "send_response");
*(_DWORD *)a2 = a3;
if ( (unsigned int)send_data(fd, a2, 8) == 1 )
result = send_data(fd, *(_QWORD *)(a2 + 8), *(_DWORD *)(a2 + 4));
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2, int a3)
{
__int64 result; // rax

if ( fd <= 0 )
_assert_fail("fd > 0", "main.c", 0x40u, "send_response");
*(_DWORD *)a2 = a3;
if ( (unsigned int)func0(fd, a2, 8) == 1 )
result = func0(fd, *(_QWORD *)(a2 + 8), *(_DWORD *)(a2 + 4));
else
result = 0LL;
return result;
}

original_code:
static int send_response(int fd, struct response *res, int error_code)
{
 ((void) sizeof ((fd > 0) ? 1 : 0), __extension__ ({ if (fd > 0) ; else __assert_fail ("fd > 0", "main.c", 64, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((res != ((void *)0)) ? 1 : 0), __extension__ ({ if (res != ((void *)0)) ; else __assert_fail ("res != NULL", "main.c", 65, __extension__ __PRETTY_FUNCTION__); }));
 res->header.status = error_code;
 if(send_data(fd, &(res->header), sizeof(res->header)) != 1)
  return 0;
 return send_data(fd, res->data, res->header.data_size);
}

variable_types: fd int

variable_types: struct response { struct response_header { int status; int data_size; } header; void *data; } *

user_defined_types: struct response { struct response_header header; void *data; }


decompiled_code:
void *__fastcall thread_loop(void *a1)
{
unsigned int v1; // ebp
int v2; // ebx
__int128 v4; // [rsp+0h] [rbp-38h] BYREF
_OWORD v5[2]; // [rsp+10h] [rbp-28h] BYREF

v1 = *(_DWORD *)a1;
printf("fd = %d\n", *(unsigned int *)a1);
if ( !g_shutdown )
{
v2 = 200;
do
{
v4 = 0LL;
v5[0] = 0LL;
if ( (unsigned int)read_command(v1, &v4) )
{
if ( (_DWORD)v4 == 3 )
break;
if ( !(unsigned int)process_command(&v4, v5) )
{
v2 = 400;
puts("Process command failed");
}
}
else
{
v2 = 400;
printf("Read command failed on %d\n", v1);
}
if ( !(unsigned int)send_response(v1, (__int64)v5, v2) )
printf("Send response failed on fd %d\n", v1);
if ( *((_QWORD *)&v4 + 1) )
free(*((void **)&v4 + 1));
if ( *((_QWORD *)&v5[0] + 1) )
free(*((void **)&v5[0] + 1));
}
while ( !g_shutdown );
}
close(v1);
if ( *((_QWORD *)&v4 + 1) )
free(*((void **)&v4 + 1));
if ( *((_QWORD *)&v5[0] + 1) )
free(*((void **)&v5[0] + 1));
*((_DWORD *)a1 + 1) = 0;
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
unsigned int v1; // ebp
int v2; // ebx
__int128 v4; // [rsp+0h] [rbp-38h] BYREF
_OWORD v5[2]; // [rsp+10h] [rbp-28h] BYREF

v1 = *(_DWORD *)a1;
printf("fd = %d\n", *(unsigned int *)a1);
if ( !g_shutdown )
{
v2 = 200;
do
{
v4 = 0LL;
v5[0] = 0LL;
if ( (unsigned int)read_command(v1, &v4) )
{
if ( (_DWORD)v4 == 3 )
break;
if ( !(unsigned int)process_command(&v4, v5) )
{
v2 = 400;
puts("Process command failed");
}
}
else
{
v2 = 400;
printf("Read command failed on %d\n", v1);
}
if ( !(unsigned int)func1(v1, (__int64)v5, v2) )
printf("Send response failed on fd %d\n", v1);
if ( *((_QWORD *)&v4 + 1) )
free(*((void **)&v4 + 1));
if ( *((_QWORD *)&v5[0] + 1) )
free(*((void **)&v5[0] + 1));
}
while ( !g_shutdown );
}
close(v1);
if ( *((_QWORD *)&v4 + 1) )
free(*((void **)&v4 + 1));
if ( *((_QWORD *)&v5[0] + 1) )
free(*((void **)&v5[0] + 1));
*((_DWORD *)a1 + 1) = 0;
return 0LL;
}

original_code:
static void *thread_loop(void *arg)
{
 struct thread *t = (thread_t *)arg;
 struct command cmd;
 struct response res;
 int error_code = 200;
 int fd = t->fd;
 printf("fd = %d\n", fd);
 while(!g_shutdown) {
  memset(&cmd, 0, sizeof(cmd));
  memset(&res, 0, sizeof(res));
  if(read_command(fd, &cmd) == 0) {
   printf("Read command failed on %d\n", fd);
   error_code = 400;
   goto send_response;
  }
  if(cmd.header.type == 3)
    break;
  if(process_command(&cmd, &res) == 0) {
   printf("Process command failed\n");
   error_code = 400;
   goto send_response;
  }
send_response:
  if(send_response(fd, &res, error_code) == 0) {
   printf("Send response failed on fd %d\n", fd);
  }
  if(cmd.data != ((void *)0))
   free(cmd.data);
  if(res.data != ((void *)0))
   free(res.data);
 }
 close(fd);
 if(cmd.data != ((void *)0))
  free(cmd.data);
 if(res.data != ((void *)0))
  free(res.data);
 t->running = 0;
 return ((void *)0);
}

variable_types:
arg
void *

variable_types: struct thread { int fd; int running; unsigned long pid; struct thread *next; } *

variable_types: struct command { struct command_header { int type; int data_size; } header; void *data; }

variable_types: struct response { struct response_header { int status; int data_size; } header; void *data; }

user_defined_types: struct command { struct command_header header; void *data; }