react

decompiled_code:
__int64 __fastcall react(unsigned int a1, const char *a2, __int64 a3, __int64 a4)
{
size_t v5; // rax
unsigned int v6; // ebp
_QWORD v8[128]; // [rsp+0h] [rbp-828h] BYREF
char string[1064]; // [rsp+400h] [rbp-428h] BYREF

memset(string, 0, 0x400uLL);
memset(v8, 0, sizeof(v8));
*(_QWORD *)string = 0x2047534D56495250LL;
v5 = strnlen(string, 0x400uLL);
strncat(string, a2, 1023 - v5);
if ( (unsigned int)strfind(a4, "PING :") )
{
v6 = sendpong(a1, a4);
}
else
{
v6 = logFlag;
if ( logFlag )
{
msglogprint(a4);
v6 = 0;
}
}
if ( (unsigned int)strfind(a4, string) )
{
if ( (unsigned int)strfind(a4, ":!disconnect") )
{
if ( (unsigned int)checkPass(a4) )
{
disconnectirc(a1);
return 1;
}
}
else if ( (unsigned int)strfind(a4, ":!reconnect") )
{
if ( (unsigned int)checkPass(a4) )
{
disconnectirc(a1);
return (unsigned int)-1;
}
}
else if ( (unsigned int)strfind(a4, ":!ip") )
{
if ( (unsigned int)checkPass(a4) )
{
v6 = getIP(v8, a2, a1);
if ( v6 != -1 )
return (unsigned int)privatemsg((char *)v8, a4, a1);
errprint("getIP()");
return v6;
}
}
else if ( (unsigned int)strfind(a4, "!mute") )
{
if ( (unsigned int)checkPass(a4) )
{
mute = 1;
return (unsigned int)privatemsg("Muted.", a4, a1);
}
}
else if ( (unsigned int)strfind(a4, "!unmute") )
{
if ( (unsigned int)checkPass(a4) )
{
mute = 0;
return (unsigned int)privatemsg("Unmuted.", a4, a1);
}
}
else
{
if ( (unsigned int)strfind(a4, ":!os") )
{
memset(v8, 0, sizeof(v8));
osinfo(v8);
return (unsigned int)privatemsg((char *)v8, a4, a1);
}
if ( (unsigned int)strfind(a4, ":!version") )
{
memset(v8, 0, sizeof(v8));
snprintf((char *)v8, 0x400uLL, "Version: %s", "1.2.3");
return (unsigned int)privatemsg((char *)v8, a4, a1);
}
if ( (unsigned int)strfind(a4, "!log") )
{
if ( (unsigned int)checkPass(a4) )
{
if ( logFlag )
return (unsigned int)privatemsg("Already logging.", a4, a1);
logFlag = 1;
msglogprint("--- Log opened ---\n");
return (unsigned int)privatemsg("Logging all messages.", a4, a1);
}
}
else if ( (unsigned int)strfind(a4, "!dontlog") && (unsigned int)checkPass(a4) )
{
if ( !logFlag )
return (unsigned int)privatemsg("I'm not logging.", a4, a1);
v6 = privatemsg("Not logging anymore.", a4, a1);
logFlag = 0;
msglogprint("--- Log closed ---\n");
return v6;
}
}
v6 = privatemsg("No!", a4, a1);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, const char *a2, __int64 a3, __int64 a4)
{
size_t v5; // rax
unsigned int v6; // ebp
_QWORD v8[128]; // [rsp+0h] [rbp-828h] BYREF
char string[1064]; // [rsp+400h] [rbp-428h] BYREF

memset(string, 0, 0x400uLL);
memset(v8, 0, sizeof(v8));
*(_QWORD *)string = 0x2047534D56495250LL;
v5 = strnlen(string, 0x400uLL);
strncat(string, a2, 1023 - v5);
if ( (unsigned int)strfind(a4, "PING :") )
{
v6 = sendpong(a1, a4);
}
else
{
v6 = logFlag;
if ( logFlag )
{
msglogprint(a4);
v6 = 0;
}
}
if ( (unsigned int)strfind(a4, string) )
{
if ( (unsigned int)strfind(a4, ":!disconnect") )
{
if ( (unsigned int)checkPass(a4) )
{
disconnectirc(a1);
return 1;
}
}
else if ( (unsigned int)strfind(a4, ":!reconnect") )
{
if ( (unsigned int)checkPass(a4) )
{
disconnectirc(a1);
return (unsigned int)-1;
}
}
else if ( (unsigned int)strfind(a4, ":!ip") )
{
if ( (unsigned int)checkPass(a4) )
{
v6 = func2(v8, a2, a1);
if ( v6 != -1 )
return (unsigned int)func0((char *)v8, a4, a1);
errprint("getIP()");
return v6;
}
}
else if ( (unsigned int)strfind(a4, "!mute") )
{
if ( (unsigned int)checkPass(a4) )
{
mute = 1;
return (unsigned int)func0("Muted.", a4, a1);
}
}
else if ( (unsigned int)strfind(a4, "!unmute") )
{
if ( (unsigned int)checkPass(a4) )
{
mute = 0;
return (unsigned int)func0("Unmuted.", a4, a1);
}
}
else
{
if ( (unsigned int)strfind(a4, ":!os") )
{
memset(v8, 0, sizeof(v8));
osinfo(v8);
return (unsigned int)func0((char *)v8, a4, a1);
}
if ( (unsigned int)strfind(a4, ":!version") )
{
memset(v8, 0, sizeof(v8));
snprintf((char *)v8, 0x400uLL, "Version: %s", "1.2.3");
return (unsigned int)func0((char *)v8, a4, a1);
}
if ( (unsigned int)strfind(a4, "!log") )
{
if ( (unsigned int)checkPass(a4) )
{
if ( logFlag )
return (unsigned int)func0("Already logging.", a4, a1);
logFlag = 1;
msglogprint("--- Log opened ---\n");
return (unsigned int)func0("Logging all messages.", a4, a1);
}
}
else if ( (unsigned int)strfind(a4, "!dontlog") && (unsigned int)checkPass(a4) )
{
if ( !logFlag )
return (unsigned int)func0("I'm not logging.", a4, a1);
v6 = func0("Not logging anymore.", a4, a1);
logFlag = 0;
msglogprint("--- Log closed ---\n");
return v6;
}
}
v6 = func0("No!", a4, a1);
}
return v6;
}

original_code:
int react (int sckt, char *nick, char *channel, char *serverline)
{
    char privmsg[1024];
    char tmp[1024];
    int ret = 0;
    memset(privmsg, 0, 1024);
    memset(tmp, 0, 1024);
    snprintf(privmsg, 1024, "%s", "PRIVMSG ");
    strncat(privmsg, nick, 1024 -strnlen(privmsg, 1024)-1);
    if (strfind(serverline, "PING :")) {
        ret = sendpong(sckt, serverline);
    } else if (logFlag) {
        msglogprint(serverline);
    }
    if (strfind(serverline, privmsg)) {
        if (strfind(serverline, ":!disconnect")) {
            if (checkPass(serverline)) {
                disconnectirc(sckt);
                ret = 1;
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!reconnect")) {
            if (checkPass(serverline)) {
                disconnectirc(sckt);
                ret = -1;
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!ip")) {
            if (checkPass(serverline)) {
                if (getIP(tmp, nick, sckt) == -1) {
                    errprint("getIP()");
                    ret = -1;
                } else {
                    ret = privatemsg(tmp, serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!mute")) {
            if (checkPass(serverline)) {
                mute = 1;
                ret = privatemsg("Muted.", serverline, sckt);
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!unmute")) {
            if (checkPass(serverline)) {
                mute = 0;
                ret = privatemsg("Unmuted.", serverline, sckt);
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!os")) {
            memset(tmp, 0, 1024);
            osinfo(tmp);
            ret = privatemsg(tmp, serverline, sckt);
        } else if (strfind(serverline, ":!version")) {
            memset(tmp, 0, 1024);
            snprintf(tmp, 1024, "Version: %s", "1.2.3");
            ret = privatemsg(tmp, serverline, sckt);
        } else if (strfind(serverline, "!log")) {
            if (checkPass(serverline)) {
                if (logFlag) {
                    ret = privatemsg("Already logging.", serverline, sckt);
                } else {
                    logFlag = 1;
                    msglogprint("--- Log opened ---\n");
                    ret = privatemsg("Logging all messages.", serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!dontlog")) {
            if (checkPass(serverline)) {
                if (logFlag) {
                    ret = privatemsg("Not logging anymore.", serverline, sckt);
                    logFlag = 0;
                    msglogprint("--- Log closed ---\n");
                } else {
                    ret = privatemsg("I'm not logging.", serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else {
            ret = privatemsg("No!", serverline, sckt);
        }
    }
    return ret;
}

canonical_original_code:
int react(int sckt, char *nick, char *channel, char *serverline)
{
    char privmsg[1024];
    char tmp[1024];
    int ret = 0;
    memset(privmsg, 0, 1024);
    memset(tmp, 0, 1024);
    snprintf(privmsg, 1024, "%s", "PRIVMSG ");
    strncat(privmsg, nick, 1024 -strnlen(privmsg, 1024)-1);
    if (strfind(serverline, "PING :")) {
        ret = sendpong(sckt, serverline);
    } else if (logFlag) {
        msglogprint(serverline);
    }
    if (strfind(serverline, privmsg)) {
        if (strfind(serverline, ":!disconnect")) {
            if (checkPass(serverline)) {
                disconnectirc(sckt);
                ret = 1;
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!reconnect")) {
            if (checkPass(serverline)) {
                disconnectirc(sckt);
                ret = -1;
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!ip")) {
            if (checkPass(serverline)) {
                if (getIP(tmp, nick, sckt) == -1) {
                    errprint("getIP()");
                    ret = -1;
                } else {
                    ret = privatemsg(tmp, serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!mute")) {
            if (checkPass(serverline)) {
                mute = 1;
                ret = privatemsg("Muted.", serverline, sckt);
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!unmute")) {
            if (checkPass(serverline)) {
                mute = 0;
                ret = privatemsg("Unmuted.", serverline, sckt);
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, ":!os")) {
            memset(tmp, 0, 1024);
            osinfo(tmp);
            ret = privatemsg(tmp, serverline, sckt);
        } else if (strfind(serverline, ":!version")) {
            memset(tmp, 0, 1024);
            snprintf(tmp, 1024, "Version: %s", "1.2.3");
            ret = privatemsg(tmp, serverline, sckt);
        } else if (strfind(serverline, "!log")) {
            if (checkPass(serverline)) {
                if (logFlag) {
                    ret = privatemsg("Already logging.", serverline, sckt);
                } else {
                    logFlag = 1;
                    msglogprint("--- Log opened ---\n");
                    ret = privatemsg("Logging all messages.", serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else if (strfind(serverline, "!dontlog")) {
            if (checkPass(serverline)) {
                if (logFlag) {
                    ret = privatemsg("Not logging anymore.", serverline, sckt);
                    logFlag = 0;
                    msglogprint("--- Log closed ---\n");
                } else {
                    ret = privatemsg("I'm not logging.", serverline, sckt);
                }
            } else {
                ret = privatemsg("No!", serverline, sckt);
            }
        } else {
            ret = privatemsg("No!", serverline, sckt);
        }
    }
    return ret;
}

variable_types:
channel
char *

variable_types:
privmsg
char [1024]

variable_types:
tmp
char [1024]

variable_types: ret int


loop

decompiled_code:
__int64 __fastcall loop(unsigned int a1, const char *a2, __int64 a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx
_BYTE savedregs[1064]; // [rsp+0h] [rbp+0h] BYREF

do
{
memset(savedregs, 0, 0x400uLL);
if ( (unsigned int)readserver(savedregs, a1) != -1 )
{
v4 = react(a1, a2, a3, (__int64)savedregs);
v5 = v4;
if ( v4 != -1 )
continue;
}
v5 = -1;
if ( !logFlag )
return v5;
goto LABEL_6;
}
while ( v4 != 1 );
if ( logFlag )
LABEL_6:
msglogprint("--- Log closed ---\n");
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, const char *a2, __int64 a3)
{
unsigned int v4; // eax
unsigned int v5; // ebx
_BYTE savedregs[1064]; // [rsp+0h] [rbp+0h] BYREF

do
{
memset(savedregs, 0, 0x400uLL);
if ( (unsigned int)readserver(savedregs, a1) != -1 )
{
v4 = func3(a1, a2, a3, (__int64)savedregs);
v5 = v4;
if ( v4 != -1 )
continue;
}
v5 = -1;
if ( !logFlag )
return v5;
goto LABEL_6;
}
while ( v4 != 1 );
if ( logFlag )
LABEL_6:
msglogprint("--- Log closed ---\n");
return v5;
}

original_code:
int loop(int sckt, char *nick, char *channel)
{
    char serverline[1024];
    int ret = 0;
    while (1==1) {
        memset(serverline, 0, 1024);
        if ( (ret = readserver(serverline, sckt)) != -1) {
            ret = react(sckt, nick, channel, serverline);
        }
        if (ret == -1) {
            if (logFlag) {
                msglogprint("--- Log closed ---\n");
            }
            break;
        } else if (ret == 1) {
            if (logFlag) {
                msglogprint("--- Log closed ---\n");
            }
            break;
        }
    }
    return ret;
}

variable_types: sckt int

variable_types:
nick
char *

variable_types:
serverline
char [1024]

return_type: int


init

decompiled_code:
__int64 __fastcall init(int fd, const char *a2, __int64 a3, __int64 a4, __int64 a5)
{
unsigned int v8; // ebx

if ( (unsigned int)loginpass() != -1
&& (unsigned int)setnick(a2, (unsigned int)fd) != -1
&& (unsigned int)setuser(a3, a4, (unsigned int)fd) != -1
&& (unsigned int)waitForResponse((unsigned int)fd) != -1
&& (unsigned int)joinchannel(a5, (unsigned int)fd) != -1
&& (unsigned int)setmode(a2, "+B", (unsigned int)fd) != -1
&& (unsigned int)loop(fd, a2, a5) != -1 )
{
return 0;
}
v8 = close(fd);
if ( v8 != -1 )
return (unsigned int)-1;
errprint("close(sckt)");
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, const char *a2, __int64 a3, __int64 a4, __int64 a5)
{
unsigned int v8; // ebx

if ( (unsigned int)loginpass() != -1
&& (unsigned int)setnick(a2, (unsigned int)fd) != -1
&& (unsigned int)setuser(a3, a4, (unsigned int)fd) != -1
&& (unsigned int)waitForResponse((unsigned int)fd) != -1
&& (unsigned int)joinchannel(a5, (unsigned int)fd) != -1
&& (unsigned int)setmode(a2, "+B", (unsigned int)fd) != -1
&& (unsigned int)func4(fd, a2, a5) != -1 )
{
return 0;
}
v8 = close(fd);
if ( v8 != -1 )
return (unsigned int)-1;
errprint("close(sckt)");
return v8;
}

original_code:
int init(int sckt, char *nick, char *username, char *realname, char *channel)
{
    int ret = 0;
    if (loginpass(sckt) == -1) ret = -1;
    else if (setnick(nick, sckt) == -1) ret = -1;
    else if (setuser(username, realname, sckt) == -1) ret = -1;
    else if (waitForResponse(sckt) == -1) ret = -1;
    else if (joinchannel(channel, sckt) == -1) ret = -1;
    else if (setmode(nick, "+B", sckt) == -1) ret = -1;
    else if (loop(sckt, nick, channel) == -1) ret = -1;
    if (ret == -1) {
        if (close(sckt) == -1) {
            errprint("close(sckt)");
        }
        return -1;
    }
    return 0;
}

variable_types:
username
char *

variable_types:
realname
char *


main

canonical_name: main

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

if ( argc <= 7 )
{
putchar(10);
puts(" parameters: <server> <port> <nick> <realname> <channel> <mute> <log>");
putchar(10);
puts(" server: freenode server rotation is chat.freenode.net");
puts(" port: usually 6667");
puts(" nick: nickname");
puts(" realname: preferably your own nick (in case of problems with the bot)");
puts(" channel: channelname (put a \\ before the #)");
puts(" mute: set this to non-zero to mute the bot");
puts(" log: set this to non-zero to log all messages");
putchar(10);
}
else
{
mute = atoi(argv[6]);
logFlag = atoi(argv[7]);
while ( 1 )
{
v3 = connectirc(argv[1], argv[2]);
if ( v3 != -1 )
{
if ( logFlag )
msglogprint("--- Log opened ---\n");
if ( (unsigned int)init(v3, argv[3], argv[3], argv[4], argv[5]) != -1 )
break;
}
sleep(0x78u);
}
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    int sckt, ret;
    if (argc < 8) {
        printf("\n");
        printf("  parameters: <server> <port> <nick> <realname> <channel> <mute> <log>\n");
        printf("\n");
        printf("  server:   freenode server rotation is chat.freenode.net\n");
        printf("  port:     usually 6667\n");
        printf("  nick:     nickname\n");
        printf("  realname: preferably your own nick (in case of problems with the bot)\n");
        printf("  channel:  channelname (put a \\ before the #)\n");
        printf("  mute:     set this to non-zero to mute the bot\n");
        printf("  log:      set this to non-zero to log all messages\n");
        printf("\n");
        return 0;
    }
    mute = atoi(argv[6]);
    logFlag = atoi(argv[7]);
    do {
        sckt = connectirc(argv[1], argv[2]);
        if (sckt == -1) {
            ret = -1;
        } else {
            if (logFlag) {
                msglogprint("--- Log opened ---\n");
            }
            ret = init(sckt, argv[3], argv[3], argv[4], argv[5]);
        }
        if (ret == -1) {
            sleep(120);
        }
    } while (ret == -1);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int sckt; int ret;
    if (argc < 8) {
        printf("\n");
        printf("  parameters: <server> <port> <nick> <realname> <channel> <mute> <log>\n");
        printf("\n");
        printf("  server:   freenode server rotation is chat.freenode.net\n");
        printf("  port:     usually 6667\n");
        printf("  nick:     nickname\n");
        printf("  realname: preferably your own nick (in case of problems with the bot)\n");
        printf("  channel:  channelname (put a \\ before the #)\n");
        printf("  mute:     set this to non-zero to mute the bot\n");
        printf("  log:      set this to non-zero to log all messages\n");
        printf("\n");
        return 0;
    }
    mute = atoi(argv[6]);
    logFlag = atoi(argv[7]);
    do {
        sckt = connectirc(argv[1], argv[2]);
        if (sckt == -1) {
            ret = -1;
        } else {
            if (logFlag) {
                msglogprint("--- Log opened ---\n");
            }
            ret = init(sckt, argv[3], argv[3], argv[4], argv[5]);
        }
        if (ret == -1) {
            sleep(120);
        }
    } while (ret == -1);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


connectirc

decompiled_code:
__int64 __fastcall connectirc(const char *a1, const char *a2)
{
struct addrinfo *v2; // rbx
unsigned int v3; // ebp
int v4; // eax
__int64 v6[3]; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo(a1, a2, &req, &pai) )
{
errprint("getaddrinfo()");
return (unsigned int)-1;
}
v2 = pai;
if ( !pai )
{
LABEL_3:
errprint("failed to connect");
freeaddrinfo(pai);
return (unsigned int)-1;
}
while ( 1 )
{
v4 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v3 = v4;
if ( v4 != -1 )
break;
LABEL_5:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_3;
}
if ( connect(v4, v2->ai_addr, v2->ai_addrlen) == -1 )
{
close(v3);
goto LABEL_5;
}
freeaddrinfo(pai);
v6[0] = 180LL;
v6[1] = 0LL;
if ( setsockopt(v3, 1, 20, v6, 0x10u) == -1 )
{
errprint("setsockopt()");
close(v3);
v3 = -1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, const char *a2)
{
struct addrinfo *v2; // rbx
unsigned int v3; // ebp
int v4; // eax
__int64 v6[3]; // [rsp+0h] [rbp-68h] BYREF
struct addrinfo *pai; // [rsp+18h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+20h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
if ( getaddrinfo(a1, a2, &req, &pai) )
{
errprint("getaddrinfo()");
return (unsigned int)-1;
}
v2 = pai;
if ( !pai )
{
LABEL_3:
errprint("failed to connect");
freeaddrinfo(pai);
return (unsigned int)-1;
}
while ( 1 )
{
v4 = socket(v2->ai_family, v2->ai_socktype, v2->ai_protocol);
v3 = v4;
if ( v4 != -1 )
break;
LABEL_5:
v2 = v2->ai_next;
if ( !v2 )
goto LABEL_3;
}
if ( connect(v4, v2->ai_addr, v2->ai_addrlen) == -1 )
{
close(v3);
goto LABEL_5;
}
freeaddrinfo(pai);
v6[0] = 180LL;
v6[1] = 0LL;
if ( setsockopt(v3, 1, 20, v6, 0x10u) == -1 )
{
errprint("setsockopt()");
close(v3);
v3 = -1;
}
return v3;
}

original_code:
int connectirc(char *server, char *port)
{
    int sckt;
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    struct timeval tv;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;
    if (getaddrinfo(server, port, &hints, &result) != 0) {
        errprint("getaddrinfo()");
        return -1;
    }
    for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
        if ((sckt = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) {
            continue;
        }
        if ((connect(sckt, rp->ai_addr, rp->ai_addrlen)) != -1) {
            break;
        }
        close(sckt);
    }
    if (rp == ((void *)0)) {
        errprint("failed to connect");
        freeaddrinfo(result);
        return -1;
    }
    freeaddrinfo(result);
    tv.tv_sec = 180;
    tv.tv_usec = 0;
    if (setsockopt(sckt, 1, 20, (char *)&tv, (socklen_t)sizeof(struct timeval)) == -1) {
        errprint("setsockopt()");
        close(sckt);
        return -1;
    }
    return sckt;
}

canonical_original_code:
int connectirc(char *server, char *port)
{
    int sckt;
    struct addrinfo hints;
    struct addrinfo *result; struct addrinfo *rp;
    struct timeval tv;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 2;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;
    if (getaddrinfo(server, port, &hints, &result) != 0) {
        errprint("getaddrinfo()");
        return -1;
    }
    for (rp = result; rp != ((void *)0); rp = rp->ai_next) {
        if ((sckt = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) {
            continue;
        }
        if ((connect(sckt, rp->ai_addr, rp->ai_addrlen)) != -1) {
            break;
        }
        close(sckt);
    }
    if (rp == ((void *)0)) {
        errprint("failed to connect");
        freeaddrinfo(result);
        return -1;
    }
    freeaddrinfo(result);
    tv.tv_sec = 180;
    tv.tv_usec = 0;
    if (setsockopt(sckt, 1, 20, (char *)&tv, (unsigned int)sizeof(struct timeval)) == -1) {
        errprint("setsockopt()");
        close(sckt);
        return -1;
    }
    return sckt;
}

variable_types:
server
char *

variable_types:
port
char *

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

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

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


readserver

decompiled_code:
__int64 __fastcall readserver(char *s, unsigned int a2)
{
unsigned int v2; // ebx
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

if ( readline(a2, v4, 1024LL) == -1 )
{
errprint("readserver()");
v2 = -1;
}
else
{
v2 = strfind(v4, "Closing Link");
if ( v2 )
{
errprint("Closing Link");
v2 = -1;
}
else
{
snprintf(s, 0x400uLL, "%s", v4);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s, unsigned int a2)
{
unsigned int v2; // ebx
char v4[1048]; // [rsp+0h] [rbp-418h] BYREF

if ( readline(a2, v4, 1024LL) == -1 )
{
errprint("readserver()");
v2 = -1;
}
else
{
v2 = strfind(v4, "Closing Link");
if ( v2 )
{
errprint("Closing Link");
v2 = -1;
}
else
{
snprintf(s, 0x400uLL, "%s", v4);
}
}
return v2;
}

original_code:
int readserver(char *s, int sckt)
{
    char tmp[1024];
    if (readline(sckt, tmp, sizeof(tmp)) == -1) {
        errprint("readserver()");
        return -1;
    }
    if (strfind(tmp, "Closing Link")) {
        errprint("Closing Link");
        return -1;
    }
    snprintf(s, 1024, "%s", tmp);
    return 0;
}

variable_types:
s
char *


writeserver

decompiled_code:
__int64 __fastcall writeserver(void *buf, int fd)
{
size_t v2; // rax

v2 = strnlen((const char *)buf, 0x400uLL);
if ( write(fd, buf, v2) != -1 )
return 0LL;
errprint("writeserver()");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *buf, int fd)
{
size_t v2; // rax

v2 = strnlen((const char *)buf, 0x400uLL);
if ( write(fd, buf, v2) != -1 )
return 0LL;
errprint("writeserver()");
return 0xFFFFFFFFLL;
}

original_code:
int writeserver(char *s, int sckt)
{
    if (write(sckt, s, strnlen(s, 1024)) == -1) {
        errprint("writeserver()");
        return -1;
    }
    return 0;
}


loginpass

decompiled_code:
__int64 __fastcall loginpass(int fd)
{
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

strcpy(v2, "PASS drowssap\r\n");
return (unsigned int)-((unsigned int)writeserver(v2, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd)
{
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

strcpy(v2, "PASS drowssap\r\n");
return (unsigned int)-((unsigned int)func2(v2, fd) == -1);
}

original_code:
int loginpass(int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "PASS drowssap\r\n");
    if (writeserver(tmp, sckt) == -1) {
        return -1;
    }
    return 0;
}


setnick

decompiled_code:
__int64 __fastcall setnick(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "NICK ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)writeserver(v5, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "NICK ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)func2(v5, fd) == -1);
}

original_code:
int setnick(char *nick, int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "NICK ");
    strncat(tmp, nick, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, "\r\n", 1024 -strnlen(tmp, 1024)-1);
    if (writeserver(tmp, sckt) == -1) {
        return -1;
    }
    return 0;
}


setuser

decompiled_code:
__int64 __fastcall setuser(char *src, char *a2, int fd)
{
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v9[1064]; // [rsp+0h] [rbp-428h] BYREF

strcpy(v9, "USER ");
v4 = strnlen(v9, 0x400uLL);
strncat(v9, src, 1023 - v4);
v5 = strnlen(v9, 0x400uLL);
strncat(v9, " 0 * :", 1023 - v5);
v6 = strnlen(v9, 0x400uLL);
strncat(v9, a2, 1023 - v6);
v7 = strnlen(v9, 0x400uLL);
strncat(v9, "\r\n", 1023 - v7);
return (unsigned int)-((unsigned int)writeserver(v9, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *src, char *a2, int fd)
{
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
char v9[1064]; // [rsp+0h] [rbp-428h] BYREF

strcpy(v9, "USER ");
v4 = strnlen(v9, 0x400uLL);
strncat(v9, src, 1023 - v4);
v5 = strnlen(v9, 0x400uLL);
strncat(v9, " 0 * :", 1023 - v5);
v6 = strnlen(v9, 0x400uLL);
strncat(v9, a2, 1023 - v6);
v7 = strnlen(v9, 0x400uLL);
strncat(v9, "\r\n", 1023 - v7);
return (unsigned int)-((unsigned int)func2(v9, fd) == -1);
}

original_code:
int setuser(char *username, char *realname, int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "USER ");
    strncat(tmp, username, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, " 0 * :", 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, realname, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, "\r\n", 1024 -strnlen(tmp, 1024)-1);
    if (writeserver(tmp, sckt) == -1 ) {
        return -1;
    }
    return 0;
}


setmode

decompiled_code:
__int64 __fastcall setmode(char *src, char *a2, int fd)
{
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
char v10[1064]; // [rsp+0h] [rbp-428h] BYREF

strcpy(v10, "MODE ");
v4 = strnlen(v10, 0x400uLL);
strncat(v10, src, 1023 - v4);
v5 = strnlen(v10, 0x400uLL);
strncat(v10, " ", 1023 - v5);
v6 = strnlen(v10, 0x400uLL);
strncat(v10, a2, 1023 - v6);
v7 = strnlen(v10, 0x400uLL);
strncat(v10, " ", 1023 - v7);
v8 = strnlen(v10, 0x400uLL);
strncat(v10, "\r\n", 1023 - v8);
return (unsigned int)-((unsigned int)writeserver(v10, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src, char *a2, int fd)
{
size_t v4; // rax
size_t v5; // rax
size_t v6; // rax
size_t v7; // rax
size_t v8; // rax
char v10[1064]; // [rsp+0h] [rbp-428h] BYREF

strcpy(v10, "MODE ");
v4 = strnlen(v10, 0x400uLL);
strncat(v10, src, 1023 - v4);
v5 = strnlen(v10, 0x400uLL);
strncat(v10, " ", 1023 - v5);
v6 = strnlen(v10, 0x400uLL);
strncat(v10, a2, 1023 - v6);
v7 = strnlen(v10, 0x400uLL);
strncat(v10, " ", 1023 - v7);
v8 = strnlen(v10, 0x400uLL);
strncat(v10, "\r\n", 1023 - v8);
return (unsigned int)-((unsigned int)func2(v10, fd) == -1);
}

original_code:
int setmode(char *nick, char *mode, int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "MODE ");
    strncat(tmp, nick, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, " ", 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, mode, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, " ", 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, "\r\n", 1024 -strnlen(tmp, 1024)-1);
    if (writeserver(tmp, sckt) == -1 ) {
        return -1;
    }
    return 0;
}

variable_types:
mode
char *


joinchannel

decompiled_code:
__int64 __fastcall joinchannel(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "JOIN ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)writeserver(v5, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func7(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "JOIN ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)func2(v5, fd) == -1);
}

original_code:
int joinchannel(char *channel, int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "JOIN ");
    strncat(tmp, channel, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, "\r\n", 1024 -strnlen(tmp, 1024)-1);
    if (writeserver(tmp, sckt) == -1) {
        return -1;
    }
    return 0;
}

variable_types:
channel
char *


sendpong

decompiled_code:
__int64 __fastcall sendpong(int fd, const char *a2)
{
size_t v2; // rax
char v4[1024]; // [rsp+0h] [rbp-808h] BYREF
char s[1032]; // [rsp+400h] [rbp-408h] BYREF

snprintf(s, 0x400uLL, "%s", a2);
if ( (unsigned int)getmsg(s, "PING :") )
{
strcpy(v4, "PONG");
}
else
{
strcpy(v4, "PONG :");
v2 = strnlen(v4, 0x400uLL);
strncat(v4, s, 1023 - v2);
}
return (unsigned int)-((unsigned int)writeserver(v4, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, const char *a2)
{
size_t v2; // rax
char v4[1024]; // [rsp+0h] [rbp-808h] BYREF
char s[1032]; // [rsp+400h] [rbp-408h] BYREF

snprintf(s, 0x400uLL, "%s", a2);
if ( (unsigned int)getmsg(s, "PING :") )
{
strcpy(v4, "PONG");
}
else
{
strcpy(v4, "PONG :");
v2 = strnlen(v4, 0x400uLL);
strncat(v4, s, 1023 - v2);
}
return (unsigned int)-((unsigned int)func2(v4, fd) == -1);
}

original_code:
int sendpong(int sckt, char *s)
{
    char tmp[1024];
    char tmp2[1024];
    snprintf(tmp, 1024, "%s", s);
    if (getmsg(tmp, "PING :") == 0) {
        snprintf(tmp2, 1024, "%s", "PONG :");
        strncat(tmp2, tmp, 1024 -strnlen(tmp2, 1024)-1);
    } else {
        snprintf(tmp2, 1024, "%s", "PONG");
    }
    if (writeserver(tmp2, sckt) == -1) {
        return -1;
    }
    return 0;
}


waitForResponse

decompiled_code:
__int64 __fastcall waitForResponse(int fd)
{
__int64 result; // rax
char v2[1048]; // [rsp+0h] [rbp-418h] BYREF

while ( 1 )
{
memset(v2, 0, 0x400uLL);
result = readserver(v2, fd);
if ( (_DWORD)result == -1 )
break;
if ( (unsigned int)strfind(v2, ":End of /MOTD") )
return 0LL;
if ( (unsigned int)strfind(v2, "PING :") )
{
result = sendpong(fd, v2);
if ( (_DWORD)result == -1 )
return result;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd)
{
__int64 result; // rax
char v2[1048]; // [rsp+0h] [rbp-418h] BYREF

while ( 1 )
{
memset(v2, 0, 0x400uLL);
result = func1(v2, fd);
if ( (_DWORD)result == -1 )
break;
if ( (unsigned int)strfind(v2, ":End of /MOTD") )
return 0LL;
if ( (unsigned int)strfind(v2, "PING :") )
{
result = func8(fd, v2);
if ( (_DWORD)result == -1 )
return result;
}
}
return result;
}

original_code:
int waitForResponse(int sckt)
{
    char serverline[1024];
    while (1==1) {
        memset(serverline, 0, 1024);
        if (readserver(serverline, sckt) == -1) {
            return -1;
        }
        if (strfind(serverline, ":End of /MOTD")) {
            return 0;
        } else if (strfind(serverline, "PING :")) {
            if (sendpong(sckt, serverline) == -1) {
                return -1;
            }
        }
    }
    return 0;
}


quit

decompiled_code:
__int64 __fastcall quit(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "QUIT ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)writeserver(v5, fd) == -1);
}

canonical_decompiled_code:
__int64 __fastcall func10(char *src, int fd)
{
size_t v2; // rax
size_t v3; // rax
char v5[1048]; // [rsp+0h] [rbp-418h] BYREF

strcpy(v5, "QUIT ");
v2 = strnlen(v5, 0x400uLL);
strncat(v5, src, 1023 - v2);
v3 = strnlen(v5, 0x400uLL);
strncat(v5, "\r\n", 1023 - v3);
return (unsigned int)-((unsigned int)func2(v5, fd) == -1);
}

original_code:
int quit(char *msg, int sckt)
{
    char tmp[1024];
    snprintf(tmp, 1024, "%s", "QUIT ");
    strncat(tmp, msg, 1024 -strnlen(tmp, 1024)-1);
    strncat(tmp, "\r\n", 1024 -strnlen(tmp, 1024)-1);
    if (writeserver(tmp, sckt) == -1) {
        return -1;
    }
    return 0;
}

variable_types:
msg
char *


disconnectirc

decompiled_code:
__int64 __fastcall disconnectirc(int fd)
{
unsigned int v1; // ebp

v1 = quit("Terminated...", fd);
sleep(1u);
if ( close(fd) == -1 )
errprint("close(sckt)");
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd)
{
unsigned int v1; // ebp

v1 = func10("Terminated...", fd);
sleep(1u);
if ( close(fd) == -1 )
errprint("close(sckt)");
return v1;
}

original_code:
int disconnectirc(int sckt)
{
    int ret;
    ret = quit("Terminated...", sckt);
    sleep(1);
    if (close(sckt) == -1) {
        errprint("close(sckt)");
    }
    return ret;
}


strfind

decompiled_code:
__int64 __fastcall strfind(char *s1, char *s2)
{
char *v2; // rbx
char *v3; // r12
size_t v4; // rax

v2 = s1;
v3 = &s1[strnlen(s1, 0x400uLL)];
while ( v2 != v3 )
{
v4 = strnlen(s2, 0x400uLL);
if ( !strncmp(v2++, s2, v4) )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1, char *s2)
{
char *v2; // rbx
char *v3; // r12
size_t v4; // rax

v2 = s1;
v3 = &s1[strnlen(s1, 0x400uLL)];
while ( v2 != v3 )
{
v4 = strnlen(s2, 0x400uLL);
if ( !strncmp(v2++, s2, v4) )
return 1LL;
}
return 0LL;
}

original_code:
int strfind(char *s1, char *s2)
{
    int i;
    for (i=0; i<strnlen(s1, 1024); i++) {
        if (strncmp(s1+i, s2, strnlen(s2, 1024)) == 0) {
            return 1;
        }
    }
    return 0;
}

variable_types: i int


getmsg

decompiled_code:
__int64 __fastcall getmsg(char *s, const char *a2)
{
size_t v2; // r14
__int64 i; // r12
size_t v4; // rbx
unsigned int v5; // ebp
char v7[1064]; // [rsp+0h] [rbp-428h] BYREF

snprintf(v7, 0x400uLL, "%s", a2);
v2 = strnlen(s, 0x400uLL);
for ( i = 0LL; v2 != i; ++i )
{
v4 = strnlen(v7, 0x400uLL);
v5 = strncmp(&s[i], v7, v4);
if ( !v5 )
{
snprintf(s, 0x400uLL, "%s", &s[v4 + i]);
return v5;
}
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s, const char *a2)
{
size_t v2; // r14
__int64 i; // r12
size_t v4; // rbx
unsigned int v5; // ebp
char v7[1064]; // [rsp+0h] [rbp-428h] BYREF

snprintf(v7, 0x400uLL, "%s", a2);
v2 = strnlen(s, 0x400uLL);
for ( i = 0LL; v2 != i; ++i )
{
v4 = strnlen(v7, 0x400uLL);
v5 = strncmp(&s[i], v7, v4);
if ( !v5 )
{
snprintf(s, 0x400uLL, "%s", &s[v4 + i]);
return v5;
}
}
return (unsigned int)-1;
}

original_code:
int getmsg(char *s, char *key)
{
    int i;
    char tmp[1024];
    snprintf(tmp, 1024, "%s", key);
    for (i=0; i<strnlen(s, 1024); i++) {
        if (strncmp(s+i, tmp, strnlen(tmp, 1024)) == 0) {
            snprintf(s, 1024, "%s", s+i+strnlen(tmp, 1024));
            return 0;
        }
    }
    return -1;
}

variable_types:
key
char *

variable_types:
tmp
char [1024]


usernamecount

decompiled_code:
__int64 __fastcall usernamecount(const char *a1)
{
size_t v2; // r9
unsigned __int64 v3; // rax
int v4; // er10

v2 = strnlen(a1, 0x400uLL);
v3 = 2LL;
while ( 1 )
{
v4 = v3;
if ( v2 <= v3 )
break;
if ( memcmp(&a1[v3++], "!", 1uLL) == 0 )
return (unsigned int)(v4 - 1);
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
size_t v2; // r9
unsigned __int64 v3; // rax
int v4; // er10

v2 = strnlen(a1, 0x400uLL);
v3 = 2LL;
while ( 1 )
{
v4 = v3;
if ( v2 <= v3 )
break;
if ( memcmp(&a1[v3++], "!", 1uLL) == 0 )
return (unsigned int)(v4 - 1);
}
return 0xFFFFFFFFLL;
}

original_code:
int usernamecount(char *s)
{
    int i;
    for (i=2; i<strnlen(s, 1024); i++) {
        if (strncmp(s+i, "!", strnlen("!", 1024)) == 0) {
            return i-1;
        }
    }
    return -1;
}


osinfo

decompiled_code:
__int64 __fastcall osinfo(char *dest)
{
size_t v1; // rax
size_t v2; // rax
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
unsigned int v6; // ebp
struct utsname v8; // [rsp+0h] [rbp-1A8h] BYREF

if ( uname(&v8) == -1 )
{
v6 = -1;
errprint("uname()");
}
else
{
strcpy(dest, "System: ");
v1 = strnlen(dest, 0x400uLL);
strncat(dest, v8.sysname, 1023 - v1);
v2 = strnlen(dest, 0x400uLL);
strncat(dest, ", release: ", 1023 - v2);
v3 = strnlen(dest, 0x400uLL);
strncat(dest, v8.release, 1023 - v3);
v4 = strnlen(dest, 0x400uLL);
strncat(dest, ", architecture: ", 1023 - v4);
v5 = strnlen(dest, 0x400uLL);
strncat(dest, v8.machine, 1023 - v5);
v6 = 0;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *dest)
{
size_t v1; // rax
size_t v2; // rax
size_t v3; // rax
size_t v4; // rax
size_t v5; // rax
unsigned int v6; // ebp
struct utsname v8; // [rsp+0h] [rbp-1A8h] BYREF

if ( uname(&v8) == -1 )
{
v6 = -1;
func1("uname()");
}
else
{
strcpy(dest, "System: ");
v1 = strnlen(dest, 0x400uLL);
strncat(dest, v8.sysname, 1023 - v1);
v2 = strnlen(dest, 0x400uLL);
strncat(dest, ", release: ", 1023 - v2);
v3 = strnlen(dest, 0x400uLL);
strncat(dest, v8.release, 1023 - v3);
v4 = strnlen(dest, 0x400uLL);
strncat(dest, ", architecture: ", 1023 - v4);
v5 = strnlen(dest, 0x400uLL);
strncat(dest, v8.machine, 1023 - v5);
v6 = 0;
}
return v6;
}

original_code:
int osinfo(char *info)
{
    struct utsname name;
    if (uname(&name) == -1) {
        errprint("uname()");
        return -1;
    }
    snprintf(info, 1024, "%s", "System: ");
    strncat(info, name.sysname, 1024 -strnlen(info, 1024)-1);
    strncat(info, ", release: ", 1024 -strnlen(info, 1024)-1);
    strncat(info, name.release, 1024 -strnlen(info, 1024)-1);
    strncat(info, ", architecture: ", 1024 -strnlen(info, 1024)-1);
    strncat(info, name.machine, 1024 -strnlen(info, 1024)-1);
    return 0;
}

variable_types:
info
char *

variable_types: struct utsname { char sysname[65]; char nodename[65]; char release[65]; char version[65]; char machine[65]; char __domainname[65]; }


readline

decompiled_code:
__int64 __fastcall readline(int fd, _BYTE *a2, __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // rbx
char v6; // al
ssize_t v7; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v4 = 1LL;
goto LABEL_12;
}
v4 = a3;
v5 = 1LL;
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 != 1 )
break;
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_12;
}
++a2;
LABEL_5:
if ( ++v5 == v4 )
goto LABEL_12;
}
if ( v7 )
{
if ( *_errno_location() != 4 )
return -1LL;
goto LABEL_5;
}
v4 = 0LL;
if ( v5 == 1 )
return v4;
v4 = v5;
LABEL_12:
*a2 = 0;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, _BYTE *a2, __int64 a3)
{
__int64 v4; // rbp
__int64 v5; // rbx
char v6; // al
ssize_t v7; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

if ( a3 <= 1 )
{
v4 = 1LL;
goto LABEL_12;
}
v4 = a3;
v5 = 1LL;
while ( 1 )
{
v7 = read(fd, buf, 1uLL);
if ( v7 != 1 )
break;
v6 = buf[0];
*a2 = buf[0];
if ( v6 == 10 )
{
v4 = v5;
++a2;
goto LABEL_12;
}
++a2;
LABEL_5:
if ( ++v5 == v4 )
goto LABEL_12;
}
if ( v7 )
{
if ( *_errno_location() != 4 )
return -1LL;
goto LABEL_5;
}
v4 = 0LL;
if ( v5 == 1 )
return v4;
v4 = v5;
LABEL_12:
*a2 = 0;
return v4;
}

original_code:
ssize_t readline(int fd, void *vptr, size_t maxlen)
{
    ssize_t n, rc;
    char c, *ptr;
    ptr = vptr;
    for (n=1; n < (ssize_t)maxlen; n++) {
        if((rc=read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if (c == '\n') {
                break;
            }
        } else if (rc == 0) {
            if (n == 1) {
                return (0);
            } else {
                break;
            }
        } else if ((*__errno_location ()) != 4) {
            return -1;
        }
    }
    *ptr = '\0';
    return n;
}

canonical_original_code:
long readline(int fd, void *vptr, unsigned long maxlen)
{
    long n; long rc;
    char c; char *ptr;
    ptr = vptr;
    for (n=1; n < (long)maxlen; n++) {
        if((rc=read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if (c == '\n') {
                break;
            }
        } else if (rc == 0) {
            if (n == 1) {
                return (0);
            } else {
                break;
            }
        } else if ((*__errno_location ()) != 4) {
            return -1;
        }
    }
    *ptr = '\0';
    return n;
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
maxlen
unsigned long

variable_types: n long

variable_types: rc long

variable_types: c char

variable_types:
ptr
char *

return_type: long


repo: fsferrara/legacy-address-book

decompiled_code:
int apri_file_r()
{
int result; // eax

result = open("./rubrica", 0, 436LL);
if ( result == -1 )
{
perror("open di apri_file_r()");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func0()
{
int result; // eax

result = open("./rubrica", 0, 436LL);
if ( result == -1 )
{
perror("open di apri_file_r()");
exit(1);
}
return result;
}

original_code:
int apri_file_r(void) {
 int fd;
 if((fd = open("./rubrica", 00, 0664)) == -1)
  perror("open di apri_file_r()"), exit(1);
 return fd;
}

canonical_original_code:
int apri_file_r() {
 int fd;
 if((fd = open("./rubrica", 00, 0664)) == -1)
  perror("open di apri_file_r()"), exit(1);
 return fd;
}


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

result = close(a1);
if ( result == -1 )
{
perror("close di chiudi_file()");
exit(1);
}
return result;
}

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

result = close(a1);
if ( result == -1 )
{
perror("close di chiudi_file()");
exit(1);
}
return result;
}

original_code:
void chiudi_file(int fd) {
 if (close(fd) == -1)
  perror("close di chiudi_file()"), exit(1);
}

return_type: void


decompiled_code:
__int64 __fastcall leggi_record(int fd, _BYTE *a2, _DWORD *a3)
{
_BYTE *v4; // r13
ssize_t v5; // rax
char v6; // al
_BYTE *v7; // r13
ssize_t v8; // rax
char v9; // al
_BYTE *v10; // r13
ssize_t v11; // rax
char v12; // al
_BYTE *v13; // r13
ssize_t v14; // rax
char v15; // al
_BYTE *v16; // r13
ssize_t v17; // rax
char v18; // al
ssize_t v19; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

buf[0] = 9;
v4 = a2;
while ( 1 )
{
v5 = read(fd, buf, 1uLL);
if ( !v5 )
return 1LL;
if ( v5 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v6 = buf[0];
*v4 = buf[0];
++*a3;
++v4;
if ( !v6 )
goto LABEL_8;
}
*v4 = 0;
++*a3;
LABEL_8:
buf[0] = 9;
v7 = a2 + 40;
while ( 1 )
{
v8 = read(fd, buf, 1uLL);
if ( !v8 )
return 1LL;
if ( v8 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v9 = buf[0];
*v7 = buf[0];
++*a3;
++v7;
if ( !v9 )
goto LABEL_16;
}
*v7 = 0;
++*a3;
LABEL_16:
buf[0] = 9;
v10 = a2 + 80;
while ( 1 )
{
v11 = read(fd, buf, 1uLL);
if ( !v11 )
return 1LL;
if ( v11 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v12 = buf[0];
*v10 = buf[0];
++*a3;
++v10;
if ( !v12 )
goto LABEL_24;
}
*v10 = 0;
++*a3;
LABEL_24:
buf[0] = 9;
v13 = a2 + 170;
do
{
v14 = read(fd, buf, 1uLL);
if ( !v14 )
return 1LL;
if ( v14 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
buf[0] = 0;
v15 = buf[0];
*v13 = buf[0];
++*a3;
++v13;
}
while ( v15 );
buf[0] = 9;
v16 = a2 + 210;
do
{
v17 = read(fd, buf, 1uLL);
if ( !v17 )
return 1LL;
if ( v17 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
buf[0] = 0;
v18 = buf[0];
*v16 = buf[0];
++*a3;
++v16;
}
while ( v18 );
buf[0] = 9;
while ( 1 )
{
v19 = read(fd, buf, 1uLL);
if ( !v19 )
return 1LL;
if ( v19 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 10 )
break;
a2[250] = buf[0];
++*a3;
}
++*a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, _BYTE *a2, _DWORD *a3)
{
_BYTE *v4; // r13
ssize_t v5; // rax
char v6; // al
_BYTE *v7; // r13
ssize_t v8; // rax
char v9; // al
_BYTE *v10; // r13
ssize_t v11; // rax
char v12; // al
_BYTE *v13; // r13
ssize_t v14; // rax
char v15; // al
_BYTE *v16; // r13
ssize_t v17; // rax
char v18; // al
ssize_t v19; // rax
char buf[41]; // [rsp+Fh] [rbp-29h] BYREF

buf[0] = 9;
v4 = a2;
while ( 1 )
{
v5 = read(fd, buf, 1uLL);
if ( !v5 )
return 1LL;
if ( v5 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v6 = buf[0];
*v4 = buf[0];
++*a3;
++v4;
if ( !v6 )
goto LABEL_8;
}
*v4 = 0;
++*a3;
LABEL_8:
buf[0] = 9;
v7 = a2 + 40;
while ( 1 )
{
v8 = read(fd, buf, 1uLL);
if ( !v8 )
return 1LL;
if ( v8 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v9 = buf[0];
*v7 = buf[0];
++*a3;
++v7;
if ( !v9 )
goto LABEL_16;
}
*v7 = 0;
++*a3;
LABEL_16:
buf[0] = 9;
v10 = a2 + 80;
while ( 1 )
{
v11 = read(fd, buf, 1uLL);
if ( !v11 )
return 1LL;
if ( v11 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
break;
v12 = buf[0];
*v10 = buf[0];
++*a3;
++v10;
if ( !v12 )
goto LABEL_24;
}
*v10 = 0;
++*a3;
LABEL_24:
buf[0] = 9;
v13 = a2 + 170;
do
{
v14 = read(fd, buf, 1uLL);
if ( !v14 )
return 1LL;
if ( v14 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
buf[0] = 0;
v15 = buf[0];
*v13 = buf[0];
++*a3;
++v13;
}
while ( v15 );
buf[0] = 9;
v16 = a2 + 210;
do
{
v17 = read(fd, buf, 1uLL);
if ( !v17 )
return 1LL;
if ( v17 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 9 )
buf[0] = 0;
v18 = buf[0];
*v16 = buf[0];
++*a3;
++v16;
}
while ( v18 );
buf[0] = 9;
while ( 1 )
{
v19 = read(fd, buf, 1uLL);
if ( !v19 )
return 1LL;
if ( v19 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
if ( buf[0] == 10 )
break;
a2[250] = buf[0];
++*a3;
}
++*a3;
return 0LL;
}

original_code:
int leggi_record(int fd, struct contatto *record, int *posizione) {
 ssize_t letto;
 int i;
 char buffer;
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->nome[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->cognome[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->indirizzo[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->telefono[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->email[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\n') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer != '\n')
     record->marcatura=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 return 0;
}

canonical_original_code:
int leggi_record(int fd, struct contatto *record, int *posizione) {
 long letto;
 int i;
 char buffer;
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->nome[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->cognome[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->indirizzo[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->telefono[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\0') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer == '\t') {
     buffer = '\0';
    }
    record->email[i++]=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 i=0;
 buffer='\t';
 while(buffer != '\n') {
  switch((letto=read(fd, &buffer, sizeof(char)))) {
   case sizeof(char):
    if (buffer != '\n')
     record->marcatura=buffer;
    (*posizione)++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 return 0;
}

variable_types: struct contatto { char nome[40]; char cognome[40]; char indirizzo[90]; char telefono[40]; char email[40]; char marcatura; } *

variable_types:
posizione
int *

variable_types: letto long

variable_types: buffer char


decompiled_code:
__int64 __fastcall scrivi_record(int fd, void *buf, _DWORD *a3)
{
_BYTE *v4; // r12
_BYTE *v5; // r12
_BYTE *v6; // r12
_BYTE *v7; // r12
_BYTE *v8; // r12
__int64 result; // rax
char v10; // [rsp+Eh] [rbp-2Ah] BYREF
char bufa[41]; // [rsp+Fh] [rbp-29h] BYREF

bufa[0] = 9;
v10 = 10;
if ( *(_BYTE *)buf )
{
v4 = buf;
do
{
if ( write(fd, v4, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v4;
}
while ( *v4 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 40) )
{
v5 = (char *)buf + 40;
do
{
if ( write(fd, v5, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v5;
}
while ( *v5 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 80) )
{
v6 = (char *)buf + 80;
do
{
if ( write(fd, v6, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v6;
}
while ( *v6 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 170) )
{
v7 = (char *)buf + 170;
do
{
if ( write(fd, v7, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v7;
}
while ( *v7 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 210) )
{
v8 = (char *)buf + 210;
do
{
if ( write(fd, v8, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v8;
}
while ( *v8 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( write(fd, (char *)buf + 250, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( write(fd, &v10, 1uLL) != 1 )
{
perror("write");
exit(1);
}
result = (unsigned int)(*a3 + 1);
*a3 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, void *buf, _DWORD *a3)
{
_BYTE *v4; // r12
_BYTE *v5; // r12
_BYTE *v6; // r12
_BYTE *v7; // r12
_BYTE *v8; // r12
__int64 result; // rax
char v10; // [rsp+Eh] [rbp-2Ah] BYREF
char bufa[41]; // [rsp+Fh] [rbp-29h] BYREF

bufa[0] = 9;
v10 = 10;
if ( *(_BYTE *)buf )
{
v4 = buf;
do
{
if ( write(fd, v4, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v4;
}
while ( *v4 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 40) )
{
v5 = (char *)buf + 40;
do
{
if ( write(fd, v5, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v5;
}
while ( *v5 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 80) )
{
v6 = (char *)buf + 80;
do
{
if ( write(fd, v6, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v6;
}
while ( *v6 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 170) )
{
v7 = (char *)buf + 170;
do
{
if ( write(fd, v7, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v7;
}
while ( *v7 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( *((_BYTE *)buf + 210) )
{
v8 = (char *)buf + 210;
do
{
if ( write(fd, v8, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
++v8;
}
while ( *v8 );
}
if ( write(fd, bufa, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( write(fd, (char *)buf + 250, 1uLL) != 1 )
{
perror("write");
exit(1);
}
++*a3;
if ( write(fd, &v10, 1uLL) != 1 )
{
perror("write");
exit(1);
}
result = (unsigned int)(*a3 + 1);
*a3 = result;
return result;
}

original_code:
int scrivi_record(int fd, struct contatto *record, int *posizione)
{
 int i;
 char tab='\t';
 char nl='\n';
 i=0;
 while(record->nome[i] != '\0') {
  if(write(fd, (void *) &(record->nome[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->cognome[i] != '\0') {
  if(write(fd, (void *) &(record->cognome[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->indirizzo[i] != '\0') {
  if(write(fd, (void *) &(record->indirizzo[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->telefono[i] != '\0') {
  if(write(fd, (void *) &(record->telefono[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->email[i] != '\0') {
  if(write(fd, (void *) &(record->email[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 if(write(fd, (void *) &(record->marcatura), sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 if(write(fd, (void *) &nl, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 return (*posizione);
}

canonical_original_code:
int scrivi_record(int fd, struct contatto *record, int *posizione)
{
 int i;
 char tab = '\t';
 char nl = '\n';
 i=0;
 while(record->nome[i] != '\0') {
  if(write(fd, (void *) &(record->nome[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->cognome[i] != '\0') {
  if(write(fd, (void *) &(record->cognome[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->indirizzo[i] != '\0') {
  if(write(fd, (void *) &(record->indirizzo[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->telefono[i] != '\0') {
  if(write(fd, (void *) &(record->telefono[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 i=0;
 while(record->email[i] != '\0') {
  if(write(fd, (void *) &(record->email[i++]), sizeof(char)) != sizeof(char))
   perror("write"), exit(1);
  (*posizione)++;
 }
 if(write(fd, (void *) &tab, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 if(write(fd, (void *) &(record->marcatura), sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 if(write(fd, (void *) &nl, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 (*posizione)++;
 return (*posizione);
}

variable_types: tab char

variable_types: nl char

user_defined_types: struct contatto { char nome[40]; char cognome[40]; char indirizzo[90]; char telefono[40]; char email[40]; char marcatura; }


decompiled_code:
void __fastcall prendi_record_sentinella(__int64 a1)
{
*(_BYTE *)(a1 + 250) = 83;
}

original_code:
void prendi_record_sentinella(struct contatto *record) {
 record->marcatura = 'S';
}


decompiled_code:
__int64 __fastcall nuovo_record(void *buf)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // er12
char v5[40]; // [rsp+0h] [rbp-118h] BYREF
char s1[212]; // [rsp+28h] [rbp-F0h] BYREF
int v7[7]; // [rsp+FCh] [rbp-1Ch] BYREF

v7[0] = 0;
v1 = open("./rubrica", 2, 436LL);
if ( v1 == -1 )
{
perror("open di nuovo_record()");
exit(1);
}
v2 = v1;
v3 = 0;
while ( !(unsigned int)leggi_record(v2, v5, v7) )
{
if ( !(unsigned int)cancellato(v5) && !strcmp(v5, (const char *)buf) && !strcmp(s1, (const char *)buf + 40) )
v3 = 1;
}
if ( !v3 )
scrivi_record(v2, buf, v7);
if ( close(v2) == -1 )
{
perror("close di nuovo_record()");
exit(1);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *buf)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // er12
char v5[40]; // [rsp+0h] [rbp-118h] BYREF
char s1[212]; // [rsp+28h] [rbp-F0h] BYREF
int v7[7]; // [rsp+FCh] [rbp-1Ch] BYREF

v7[0] = 0;
v1 = open("./rubrica", 2, 436LL);
if ( v1 == -1 )
{
perror("open di nuovo_record()");
exit(1);
}
v2 = v1;
v3 = 0;
while ( !(unsigned int)func2(v2, v5, v7) )
{
if ( !(unsigned int)cancellato(v5) && !strcmp(v5, (const char *)buf) && !strcmp(s1, (const char *)buf + 40) )
v3 = 1;
}
if ( !v3 )
func3(v2, buf, v7);
if ( close(v2) == -1 )
{
perror("close di nuovo_record()");
exit(1);
}
return v3;
}

original_code:
int nuovo_record(struct contatto *new) {
 int fd, numero_fittizio=0;
 int errore = 0;
 struct contatto buff;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di nuovo_record()"), exit(1);
 while(leggi_record(fd, &buff, &numero_fittizio) == 0) {
  if (!cancellato(&buff))
   if ((strcmp(buff.nome, new->nome)==0) && (strcmp(buff.cognome, new->cognome)==0))
    errore=1;
 }
 if (!errore)
  scrivi_record(fd, new, &numero_fittizio);
 if (close(fd) == -1)
  perror("close di nuovo_record()"), exit(1);
 return errore;
}

canonical_original_code:
int nuovo_record(struct contatto *new) {
 int fd; int numero_fittizio = 0;
 int errore = 0;
 struct contatto buff;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di nuovo_record()"), exit(1);
 while(leggi_record(fd, &buff, &numero_fittizio) == 0) {
  if (!cancellato(&buff))
   if ((strcmp(buff.nome, new->nome)==0) && (strcmp(buff.cognome, new->cognome)==0))
    errore=1;
 }
 if (!errore)
  scrivi_record(fd, new, &numero_fittizio);
 if (close(fd) == -1)
  perror("close di nuovo_record()"), exit(1);
 return errore;
}

variable_types: errore int

variable_types: struct contatto { char nome[40]; char cognome[40]; char indirizzo[90]; char telefono[40]; char email[40]; char marcatura; }


decompiled_code:
int reset_database()
{
int v0; // eax
int result; // eax

v0 = open("./rubrica", 577, 436LL);
if ( v0 == -1 )
{
perror("open di reset_database()");
exit(1);
}
result = close(v0);
if ( result == -1 )
{
perror("close di reset_database()");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func6()
{
int v0; // eax
int result; // eax

v0 = open("./rubrica", 577, 436LL);
if ( v0 == -1 )
{
perror("open di reset_database()");
exit(1);
}
result = close(v0);
if ( result == -1 )
{
perror("close di reset_database()");
exit(1);
}
return result;
}

original_code:
void reset_database(void)
{
 int fd;
 fd = open("./rubrica", 0100 | 01000 | 01, 0664);
 if (fd == -1)
  perror("open di reset_database()"), exit(1);
 if (close(fd) == -1)
  perror("close di reset_database()"), exit(1);
}

canonical_original_code:
void reset_database()
{
 int fd;
 fd = open("./rubrica", 0100 | 01000 | 01, 0664);
 if (fd == -1)
  perror("open di reset_database()"), exit(1);
 if (close(fd) == -1)
  perror("close di reset_database()"), exit(1);
}


idoneo

original_code:
int idoneo(struct contatto *criterio, struct contatto *record)
{
 if(strcmp("", criterio->nome) != 0)
  if(strcmp(record->nome, criterio->nome) != 0)
   return 0;
 if(strcmp("", criterio->cognome) != 0)
  if(strcmp(record->cognome, criterio->cognome) != 0)
   return 0;
 if(strcmp("", criterio->indirizzo) != 0)
  if(strcmp(record->indirizzo, criterio->indirizzo) != 0)
   return 0;
 if(strcmp("", criterio->telefono) != 0)
  if(strcmp(record->telefono, criterio->telefono) != 0)
   return 0;
 if(strcmp("", criterio->email) != 0)
  if(strcmp(record->email, criterio->email) != 0)
   return 0;
 if(record->marcatura == 'X')
  return 0;
 return 1;
}

variable_types:
record
struct contatto *


decompiled_code:
__int64 __fastcall trova_record(char *s2)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
unsigned int v4; // er13
int v5; // eax
char v7[40]; // [rsp+0h] [rbp-128h] BYREF
char s1[212]; // [rsp+28h] [rbp-100h] BYREF
unsigned int v9[11]; // [rsp+FCh] [rbp-2Ch] BYREF

v9[0] = 0;
v1 = open("./rubrica", 0, 436LL);
if ( v1 == -1 )
{
perror("open di trova_record()");
exit(1);
}
v2 = v1;
v3 = 1;
v4 = 0;
while ( 1 )
{
v5 = leggi_record(v2, v7, v9);
if ( v3 != 1 || v5 )
break;
if ( !(unsigned int)cancellato(v7) && !strcmp(v7, s2) )
{
v3 = strcmp(s1, s2 + 40);
if ( !v3 )
continue;
}
v4 = v9[0];
v3 = 1;
}
if ( close(v2) == -1 )
{
perror("close di trova_record()");
exit(1);
}
if ( v3 )
v4 = -1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *s2)
{
int v1; // eax
int v2; // ebp
int v3; // ebx
unsigned int v4; // er13
int v5; // eax
char v7[40]; // [rsp+0h] [rbp-128h] BYREF
char s1[212]; // [rsp+28h] [rbp-100h] BYREF
unsigned int v9[11]; // [rsp+FCh] [rbp-2Ch] BYREF

v9[0] = 0;
v1 = open("./rubrica", 0, 436LL);
if ( v1 == -1 )
{
perror("open di trova_record()");
exit(1);
}
v2 = v1;
v3 = 1;
v4 = 0;
while ( 1 )
{
v5 = func2(v2, v7, v9);
if ( v3 != 1 || v5 )
break;
if ( !(unsigned int)cancellato(v7) && !strcmp(v7, s2) )
{
v3 = strcmp(s1, s2 + 40);
if ( !v3 )
continue;
}
v4 = v9[0];
v3 = 1;
}
if ( close(v2) == -1 )
{
perror("close di trova_record()");
exit(1);
}
if ( v3 )
v4 = -1;
return v4;
}

original_code:
int trova_record(struct ricerca_contatto *cerco)
{
 int posizione = 0;
 int vecchia_posizione = 0;
 int letto, fd;
 int flag = 1;
 struct contatto buffer;
 if((fd = open("./rubrica", 00, 0664)) == -1)
  perror("open di trova_record()"), exit(1);
 while ((leggi_record(fd, &buffer, &posizione) == 0) && (flag == 1)) {
  if (!cancellato(&buffer))
   if ((strcmp(buffer.nome, cerco->nome)==0) && (strcmp(buffer.cognome, cerco->cognome)==0))
    flag = 0;
  if (flag)
   vecchia_posizione = posizione;
 }
 if (close(fd) == -1)
  perror("close di trova_record()"), exit(1);
 if (flag)
  return -1;
 return vecchia_posizione;
}

canonical_original_code:
int trova_record(struct ricerca_contatto *cerco)
{
 int posizione = 0;
 int vecchia_posizione = 0;
 int letto; int fd;
 int flag = 1;
 struct contatto buffer;
 if((fd = open("./rubrica", 00, 0664)) == -1)
  perror("open di trova_record()"), exit(1);
 while ((leggi_record(fd, &buffer, &posizione) == 0) && (flag == 1)) {
  if (!cancellato(&buffer))
   if ((strcmp(buffer.nome, cerco->nome)==0) && (strcmp(buffer.cognome, cerco->cognome)==0))
    flag = 0;
  if (flag)
   vecchia_posizione = posizione;
 }
 if (close(fd) == -1)
  perror("close di trova_record()"), exit(1);
 if (flag)
  return -1;
 return vecchia_posizione;
}

variable_types: struct ricerca_contatto { char nome[40]; char cognome[40]; } *

variable_types: posizione int

variable_types: letto int

variable_types: flag int


decompiled_code:
__int64 __fastcall cancella_record(char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
int v4; // ebp
int i; // ebx
int v6; // eax
char v8; // [rsp+Eh] [rbp-1Ah] BYREF
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v8 = 88;
v1 = trova_record(a1);
if ( v1 == -1 )
return 1LL;
v2 = v1;
v3 = open("./rubrica", 2, 436LL);
v4 = v3;
if ( v3 == -1 )
{
perror("open di cancella_record()");
exit(1);
}
if ( lseek(v3, v2, 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
for ( i = 0; i <= 4; i += buf[0] == 9 )
{
v6 = read(v4, buf, 1uLL);
if ( !v6 )
return 1LL;
if ( v6 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
}
if ( write(v4, &v8, 1uLL) != 1 )
{
perror("write");
exit(1);
}
if ( close(v4) == -1 )
{
perror("close di cancella_record()");
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1)
{
int v1; // eax
int v2; // ebx
int v3; // eax
int v4; // ebp
int i; // ebx
int v6; // eax
char v8; // [rsp+Eh] [rbp-1Ah] BYREF
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v8 = 88;
v1 = func8(a1);
if ( v1 == -1 )
return 1LL;
v2 = v1;
v3 = open("./rubrica", 2, 436LL);
v4 = v3;
if ( v3 == -1 )
{
perror("open di cancella_record()");
exit(1);
}
if ( lseek(v3, v2, 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
for ( i = 0; i <= 4; i += buf[0] == 9 )
{
v6 = read(v4, buf, 1uLL);
if ( !v6 )
return 1LL;
if ( v6 != 1 )
{
perror("read di leggi_record()");
exit(1);
}
}
if ( write(v4, &v8, 1uLL) != 1 )
{
perror("write");
exit(1);
}
if ( close(v4) == -1 )
{
perror("close di cancella_record()");
exit(1);
}
return 0LL;
}

original_code:
int cancella_record(struct ricerca_contatto *cancello)
{
 int fd;
 int letto;
 int flag=1;
 int contatore;
 int i=0;
 char car;
 char canc = 'X';
 struct contatto buffer;
 if ((contatore = trova_record(cancello)) == -1)
  return 1;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di cancella_record()"), exit(1);
 if (lseek(fd, contatore, 0) == -1)
  perror("lseek di cancella_record()"), exit(1);
 while(i < 5) {
  switch((letto=read(fd, &car, sizeof(char)))) {
   case sizeof(char):
    if (car == '\t')
     i++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 if(write(fd, (void *) &canc, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 if (close(fd) == -1)
  perror("close di cancella_record()"), exit(1);
 return 0;
}

canonical_original_code:
int cancella_record(struct ricerca_contatto *cancello)
{
 int fd;
 int letto;
 int flag = 1;
 int contatore;
 int i = 0;
 char car;
 char canc = 'X';
 struct contatto buffer;
 if ((contatore = trova_record(cancello)) == -1)
  return 1;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di cancella_record()"), exit(1);
 if (lseek(fd, contatore, 0) == -1)
  perror("lseek di cancella_record()"), exit(1);
 while(i < 5) {
  switch((letto=read(fd, &car, sizeof(char)))) {
   case sizeof(char):
    if (car == '\t')
     i++;
    break;
   case 0:
    return 1;
    break;
   default:
    perror("read di leggi_record()");
    exit(1);
    break;
  }
 }
 if(write(fd, (void *) &canc, sizeof(char)) != sizeof(char))
  perror("write"), exit(1);
 if (close(fd) == -1)
  perror("close di cancella_record()"), exit(1);
 return 0;
}

variable_types: fd int

variable_types: contatore int

variable_types: i int

variable_types: car char

variable_types: canc char

user_defined_types: struct ricerca_contatto { char nome[40]; char cognome[40]; }


decompiled_code:
__int64 __fastcall modifica_record(void *buf, char *a2)
{
__int64 result; // rax

result = cancella_record(a2);
if ( (_DWORD)result != 1 )
result = (unsigned int)nuovo_record(buf) == 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(void *buf, char *a2)
{
__int64 result; // rax

result = func9(a2);
if ( (_DWORD)result != 1 )
result = (unsigned int)func5(buf) == 1;
return result;
}

original_code:
int modifica_record(struct contatto *new, struct ricerca_contatto *cerca) {
 if(cancella_record(cerca) == 1)
  return 1;
 if(nuovo_record(new) == 1)
  return 1;
 return 0;
}

variable_types: struct contatto { char nome[40]; char cognome[40]; char indirizzo[90]; char telefono[40]; char email[40]; char marcatura; } *


decompiled_code:
__int64 compatta_tabella()
{
int v0; // ebx
_BYTE v2[264]; // [rsp+0h] [rbp-118h] BYREF
int v3; // [rsp+108h] [rbp-10h] BYREF
int v4[3]; // [rsp+10Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 0;
v0 = open("./rubrica", 2, 436LL);
if ( v0 == -1 )
{
perror("open di compatta_tabella()");
exit(1);
}
while ( !(unsigned int)leggi_record(v0, v2, v4) )
{
if ( (unsigned int)cancellato(v2) )
{
while ( !(unsigned int)leggi_record(v0, v2, v4) )
{
if ( !(unsigned int)cancellato(v2) )
{
if ( lseek(v0, v3, 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
scrivi_record(v0, v2, &v3);
if ( lseek(v0, v4[0], 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
}
}
}
else
{
v3 = v4[0];
}
}
if ( close(v0) == -1 )
{
perror("close di compatta_tabella()");
exit(1);
}
if ( v3 == v4[0] )
return 1LL;
if ( truncate("./rubrica", v3) == -1 )
{
perror("truncate di compatta_tabella()");
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // ebx
_BYTE v2[264]; // [rsp+0h] [rbp-118h] BYREF
int v3; // [rsp+108h] [rbp-10h] BYREF
int v4[3]; // [rsp+10Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 0;
v0 = open("./rubrica", 2, 436LL);
if ( v0 == -1 )
{
perror("open di compatta_tabella()");
exit(1);
}
while ( !(unsigned int)func2(v0, v2, v4) )
{
if ( (unsigned int)cancellato(v2) )
{
while ( !(unsigned int)func2(v0, v2, v4) )
{
if ( !(unsigned int)cancellato(v2) )
{
if ( lseek(v0, v3, 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
func3(v0, v2, &v3);
if ( lseek(v0, v4[0], 0) == -1 )
{
perror("lseek di cancella_record()");
exit(1);
}
}
}
}
else
{
v3 = v4[0];
}
}
if ( close(v0) == -1 )
{
perror("close di compatta_tabella()");
exit(1);
}
if ( v3 == v4[0] )
return 1LL;
if ( truncate("./rubrica", v3) == -1 )
{
perror("truncate di compatta_tabella()");
exit(1);
}
return 0LL;
}

original_code:
int compatta_tabella(void) {
 int fd;
 int lettura=0, scrittura=0;
 struct contatto buffer;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di compatta_tabella()"), exit(1);
 while (leggi_record(fd, &buffer, &lettura) == 0) {
  if (!cancellato(&buffer))
   scrittura=lettura;
  else {
   while (leggi_record(fd, &buffer, &lettura) == 0) {
    if (!cancellato(&buffer)) {
     if (lseek(fd, scrittura, 0) == -1)
      perror("lseek di cancella_record()"), exit(1);
     scrivi_record(fd, &buffer, &scrittura);
     if (lseek(fd, lettura, 0) == -1)
      perror("lseek di cancella_record()"), exit(1);
    }
   }
  }
 }
 if (close(fd) == -1)
  perror("close di compatta_tabella()"), exit(1);
 if (scrittura == lettura)
  return 1;
 else
  if (truncate("./rubrica", scrittura) == -1)
   perror("truncate di compatta_tabella()"), exit(1);
 return 0;
}

canonical_original_code:
int compatta_tabella() {
 int fd;
 int lettura = 0; int scrittura = 0;
 struct contatto buffer;
 if((fd = open("./rubrica", 02, 0664)) == -1)
  perror("open di compatta_tabella()"), exit(1);
 while (leggi_record(fd, &buffer, &lettura) == 0) {
  if (!cancellato(&buffer))
   scrittura=lettura;
  else {
   while (leggi_record(fd, &buffer, &lettura) == 0) {
    if (!cancellato(&buffer)) {
     if (lseek(fd, scrittura, 0) == -1)
      perror("lseek di cancella_record()"), exit(1);
     scrivi_record(fd, &buffer, &scrittura);
     if (lseek(fd, lettura, 0) == -1)
      perror("lseek di cancella_record()"), exit(1);
    }
   }
  }
 }
 if (close(fd) == -1)
  perror("close di compatta_tabella()"), exit(1);
 if (scrittura == lettura)
  return 1;
 else
  if (truncate("./rubrica", scrittura) == -1)
   perror("truncate di compatta_tabella()"), exit(1);
 return 0;
}

variable_types: lettura int

variable_types: scrittura int


decompiled_code:
void *__fastcall r_stampa_contatti(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 97LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 97LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_stampa_contatti(void *id_c) {
 int id_connessione = *((int *) id_c);
 if(trasmetti_opzione(id_connessione, 'a'))
  fatal_error(id_connessione);
 return ((void *)0);
}

return_type: void *


decompiled_code:
void *__fastcall r_cerca_contatto(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 98LL) )
fatal_error(v1);
fwrite("Premere semplicemente <INVIO> per non dare il valore\n", 1uLL, 0x35uLL, stdout);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
fatal_error(v1);
fwrite("\nContatti trovati:\n", 1uLL, 0x13uLL, stdout);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 98LL) )
func0(v1);
fwrite("Premere semplicemente <INVIO> per non dare il valore\n", 1uLL, 0x35uLL, stdout);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
func0(v1);
fwrite("\nContatti trovati:\n", 1uLL, 0x13uLL, stdout);
return 0LL;
}

original_code:
void *r_cerca_contatto(void *id_c) {
 int id_connessione = *((int *) id_c);
 struct contatto cerca;
 if(trasmetti_opzione(id_connessione, 'b'))
  fatal_error(id_connessione);
 fprintf(stdout,"Premere semplicemente <INVIO> per non dare il valore\n");
 inserisci_record(&cerca);
 if(trasmetti(id_connessione, &cerca, sizeof(struct contatto)))
  fatal_error(id_connessione);
 fprintf(stdout,"\nContatti trovati:\n");
 return ((void *)0);
}


decompiled_code:
void *__fastcall r_modifica_contatto(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 99LL) )
fatal_error(v1);
fwrite("Dare tutti i valori richiesti:\n", 1uLL, 0x1FuLL, stdout);
inserisci_record_ricerca(v3);
if ( (unsigned int)trasmetti(v1, v3, 80LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func3(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 99LL) )
func0(v1);
fwrite("Dare tutti i valori richiesti:\n", 1uLL, 0x1FuLL, stdout);
inserisci_record_ricerca(v3);
if ( (unsigned int)trasmetti(v1, v3, 80LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_modifica_contatto(void *id_c) {
 int id_connessione = *((int *) id_c);
 struct ricerca_contatto modifico;
 if(trasmetti_opzione(id_connessione, 'c'))
  fatal_error(id_connessione);
 fprintf(stdout,"Dare tutti i valori richiesti:\n");
 inserisci_record_ricerca(&modifico);
 if(trasmetti(id_connessione, &modifico, sizeof(struct ricerca_contatto)))
  fatal_error(id_connessione);
 return ((void *)0);
}

variable_types: struct ricerca_contatto { char nome[40]; char cognome[40]; }


decompiled_code:
void *__fastcall r_modifica_contatto2(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
fwrite("Reinserisci i dati del contatto per modificarlo\n", 1uLL, 0x30uLL, stdout);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func4(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
fwrite("Reinserisci i dati del contatto per modificarlo\n", 1uLL, 0x30uLL, stdout);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_modifica_contatto2(void *id_c) {
 int id_connessione = *((int *) id_c);
 struct contatto new;
 fprintf(stdout,"Reinserisci i dati del contatto per modificarlo\n");
 inserisci_record(&new);
 if(trasmetti(id_connessione, &new, sizeof(struct contatto)))
  fatal_error(id_connessione);
 return ((void *)0);
}


decompiled_code:
void *__fastcall r_nuovo_contatto(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 100LL) )
fatal_error(v1);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[264]; // [rsp+0h] [rbp-108h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 100LL) )
func0(v1);
inserisci_record(v3);
if ( (unsigned int)trasmetti(v1, v3, 251LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_nuovo_contatto(void *id_c) {
 int id_connessione = *((int *) id_c);
 struct contatto new;
 if(trasmetti_opzione(id_connessione, 'd'))
  fatal_error(id_connessione);
 inserisci_record(&new);
 if(trasmetti(id_connessione, &new, sizeof(struct contatto)))
  fatal_error(id_connessione);
 return ((void *)0);
}


decompiled_code:
void *__fastcall r_cancella_contatto(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 101LL) )
fatal_error(v1);
fwrite("Dare tutti i valori richiesti:\n", 1uLL, 0x1FuLL, stdout);
inserisci_record_ricerca(v3);
if ( (unsigned int)trasmetti(v1, v3, 80LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func6(void *a1)
{
unsigned int v1; // ebx
_BYTE v3[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 101LL) )
func0(v1);
fwrite("Dare tutti i valori richiesti:\n", 1uLL, 0x1FuLL, stdout);
inserisci_record_ricerca(v3);
if ( (unsigned int)trasmetti(v1, v3, 80LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_cancella_contatto(void *id_c) {
 int id_connessione = *((int *) id_c);
 struct ricerca_contatto cancella;
 if(trasmetti_opzione(id_connessione, 'e'))
  fatal_error(id_connessione);
 fprintf(stdout,"Dare tutti i valori richiesti:\n");
 inserisci_record_ricerca(&cancella);
 if(trasmetti(id_connessione, &cancella, sizeof(struct ricerca_contatto)))
  fatal_error(id_connessione);
 return ((void *)0);
}


decompiled_code:
void *__fastcall r_compact_tabella(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 102LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 102LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_compact_tabella(void *id_c) {
 int id_connessione = *((int *) id_c);
 if(trasmetti_opzione(id_connessione, 'f'))
  fatal_error(id_connessione);
 return ((void *)0);
}


decompiled_code:
void *__fastcall r_program_exit(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 103LL) )
fatal_error(v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func8(void *a1)
{
unsigned int v1; // ebx

v1 = *(_DWORD *)a1;
if ( (unsigned int)trasmetti_opzione(*(unsigned int *)a1, 103LL) )
func0(v1);
return 0LL;
}

original_code:
void *r_program_exit(void *id_c) {
 int id_connessione = *((int *) id_c);
 if(trasmetti_opzione(id_connessione, 'g'))
  fatal_error(id_connessione);
 return ((void *)0);
}


decompiled_code:
__int64 __fastcall program_exit(__int64 a1)
{
__int64 result; // rax
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

result = ricevi_opzione(a1, v2);
if ( (_DWORD)result )
fatal_error(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
__int64 result; // rax
char v2[9]; // [rsp+Fh] [rbp-9h] BYREF

result = ricevi_opzione(a1, v2);
if ( (_DWORD)result )
func0(a1);
return result;
}

original_code:
void program_exit(int id_connessione)
{
 char risultato;
 if(ricevi_opzione(id_connessione, &risultato))
  fatal_error(id_connessione);
}

variable_types: risultato char


decompiled_code:
size_t stampa_menu()
{
fwrite("[]=-=-=-=-=-=-=-=-=-=-=-=[MENU]=-=-=-=-=-=-=-=-=-=-=-=[]\n", 1uLL, 0x39uLL, stdout);
fwrite("Scegli una delle seguenti opzioni:\n\n", 1uLL, 0x24uLL, stdout);
fwrite("\t1) Stampa tutti i contatti\n", 1uLL, 0x1CuLL, stdout);
fwrite("\t2) Cerca un contatto\n", 1uLL, 0x16uLL, stdout);
fwrite("\t3) Modifica un contatto\n", 1uLL, 0x19uLL, stdout);
fwrite("\t4) Nuovo contatto\n", 1uLL, 0x13uLL, stdout);
fwrite("\t5) Cancella un contatto\n", 1uLL, 0x19uLL, stdout);
fwrite("\t6) Compact tabella\n", 1uLL, 0x14uLL, stdout);
fwrite("\t7) ESCI\n\n", 1uLL, 0xAuLL, stdout);
return fwrite("Inserisci la tua scelta: ", 1uLL, 0x19uLL, stdout);
}

canonical_decompiled_code:
size_t func16()
{
fwrite("[]=-=-=-=-=-=-=-=-=-=-=-=[MENU]=-=-=-=-=-=-=-=-=-=-=-=[]\n", 1uLL, 0x39uLL, stdout);
fwrite("Scegli una delle seguenti opzioni:\n\n", 1uLL, 0x24uLL, stdout);
fwrite("\t1) Stampa tutti i contatti\n", 1uLL, 0x1CuLL, stdout);
fwrite("\t2) Cerca un contatto\n", 1uLL, 0x16uLL, stdout);
fwrite("\t3) Modifica un contatto\n", 1uLL, 0x19uLL, stdout);
fwrite("\t4) Nuovo contatto\n", 1uLL, 0x13uLL, stdout);
fwrite("\t5) Cancella un contatto\n", 1uLL, 0x19uLL, stdout);
fwrite("\t6) Compact tabella\n", 1uLL, 0x14uLL, stdout);
fwrite("\t7) ESCI\n\n", 1uLL, 0xAuLL, stdout);
return fwrite("Inserisci la tua scelta: ", 1uLL, 0x19uLL, stdout);
}

original_code:
void stampa_menu(void)
{
 fprintf(stdout,"[]=-=-=-=-=-=-=-=-=-=-=-=[MENU]=-=-=-=-=-=-=-=-=-=-=-=[]\n");
 fprintf(stdout,"Scegli una delle seguenti opzioni:\n\n");
 fprintf(stdout,"\t1) Stampa tutti i contatti\n");
 fprintf(stdout,"\t2) Cerca un contatto\n");
 fprintf(stdout,"\t3) Modifica un contatto\n");
 fprintf(stdout,"\t4) Nuovo contatto\n");
 fprintf(stdout,"\t5) Cancella un contatto\n");
 fprintf(stdout,"\t6) Compact tabella\n");
 fprintf(stdout,"\t7) ESCI\n\n");
 fprintf(stdout,"Inserisci la tua scelta: ");
}

canonical_original_code:
void stampa_menu()
{
 fprintf(stdout,"[]=-=-=-=-=-=-=-=-=-=-=-=[MENU]=-=-=-=-=-=-=-=-=-=-=-=[]\n");
 fprintf(stdout,"Scegli una delle seguenti opzioni:\n\n");
 fprintf(stdout,"\t1) Stampa tutti i contatti\n");
 fprintf(stdout,"\t2) Cerca un contatto\n");
 fprintf(stdout,"\t3) Modifica un contatto\n");
 fprintf(stdout,"\t4) Nuovo contatto\n");
 fprintf(stdout,"\t5) Cancella un contatto\n");
 fprintf(stdout,"\t6) Compact tabella\n");
 fprintf(stdout,"\t7) ESCI\n\n");
 fprintf(stdout,"Inserisci la tua scelta: ");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
const char *v4; // rbp
const char *v5; // rbx
int result; // eax
char v7; // [rsp+Fh] [rbp-29h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-28h] BYREF
int v9; // [rsp+18h] [rbp-20h] BYREF
unsigned int arg[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( argc == 2 )
{
v4 = argv[1];
if ( !strcmp(v4, "-h") || !strcmp(v4, "--help") )
{
stampa_help(*argv);
exit(0);
}
v3 = 3456;
}
else if ( argc == 3 )
{
v3 = atoi(argv[2]);
}
else
{
v3 = 3456;
if ( argc <= 1 )
{
arg[0] = connetti("127.0.0.1", 3456LL);
LABEL_11:
fwrite("Benvenuto, sei connesso con la rubrica\n", 1uLL, 0x27uLL, stdout);
while ( 1 )
{
fwrite("\n\nPremere INVIO per continuare", 1uLL, 0x1EuLL, stdout);
getchar();
fwrite("\n\n\n\n\n", 1uLL, 5uLL, stdout);
stampa_menu();
_isoc99_scanf("%d", &v9);
getchar();
fwrite("\n\n\n\n\n", 1uLL, 5uLL, stdout);
switch ( v9 )
{
case 1:
fwrite("[]=-=-=-=-=[STAMPA CONTATTI]=-=-=-=-=[]\n\n", 1uLL, 0x29uLL, stdout);
pthread_create(&newthread, 0LL, r_stampa_contatti, arg);
stampa_contatti(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 2:
fwrite("[]=-=-=-=-=-=[CERCA CONTATTO]=-=-=-=-=-=[]\n\n", 1uLL, 0x2CuLL, stdout);
pthread_create(&newthread, 0LL, r_cerca_contatto, arg);
cerca_contatto(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 3:
fwrite("[]=-=-=-=-=[MODIFICA CONTATTO]=-=-=-=-=[]\n\n", 1uLL, 0x2BuLL, stdout);
pthread_create(&newthread, 0LL, r_modifica_contatto, arg);
modifica_contatto(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 4:
fwrite("[]=-=-=-=-=-=[NUOVO CONTATTO]=-=-=-=-=-=[]\n\n", 1uLL, 0x2CuLL, stdout);
pthread_create(&newthread, 0LL, r_nuovo_contatto, arg);
nuovo_contatto(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 5:
fwrite("[]=-=-=-=-=[CANCELLA CONTATTO]=-=-=-=-=[]\n\n", 1uLL, 0x2BuLL, stdout);
pthread_create(&newthread, 0LL, r_cancella_contatto, arg);
cancella_contatto(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 6:
fwrite("[]=-=-=-=-=-=[COMPACT TABELLA]=-=-=-=-=-=[]\n\n", 1uLL, 0x2DuLL, stdout);
pthread_create(&newthread, 0LL, r_compact_tabella, arg);
compact_tabella(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 7:
pthread_create(&newthread, 0LL, r_program_exit, arg);
program_exit(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
default:
fwrite("[ERRORE] Scelta errata!\n", 1uLL, 0x18uLL, stdout);
break;
}
if ( (unsigned int)ricevi_opzione(arg[0], &v7) )
fatal_error(arg[0]);
if ( v7 == 108 )
break;
if ( v9 == 7 )
goto LABEL_35;
}
fwrite(&unk_11B0, 1uLL, 0x39uLL, stdout);
fwrite(&unk_11F0, 1uLL, 0x1EuLL, stdout);
v9 = 7;
LABEL_35:
chiudi_connessione(arg[0]);
fwrite("Arrivederci\n", 1uLL, 0xCuLL, stdout);
exit(0);
}
}
v5 = argv[1];
if ( !strcmp(v5, "localhost") )
arg[0] = connetti("127.0.0.1", v3);
else
arg[0] = connetti(v5, v3);
goto LABEL_11;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
const char *v4; // rbp
const char *v5; // rbx
int result; // eax
char v7; // [rsp+Fh] [rbp-29h] BYREF
pthread_t newthread; // [rsp+10h] [rbp-28h] BYREF
int v9; // [rsp+18h] [rbp-20h] BYREF
unsigned int arg[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( argc == 2 )
{
v4 = argv[1];
if ( !strcmp(v4, "-h") || !strcmp(v4, "--help") )
{
func17(*argv);
exit(0);
}
v3 = 3456;
}
else if ( argc == 3 )
{
v3 = atoi(argv[2]);
}
else
{
v3 = 3456;
if ( argc <= 1 )
{
arg[0] = connetti("127.0.0.1", 3456LL);
LABEL_11:
fwrite("Benvenuto, sei connesso con la rubrica\n", 1uLL, 0x27uLL, stdout);
while ( 1 )
{
fwrite("\n\nPremere INVIO per continuare", 1uLL, 0x1EuLL, stdout);
getchar();
fwrite("\n\n\n\n\n", 1uLL, 5uLL, stdout);
func16();
_isoc99_scanf("%d", &v9);
getchar();
fwrite("\n\n\n\n\n", 1uLL, 5uLL, stdout);
switch ( v9 )
{
case 1:
fwrite("[]=-=-=-=-=[STAMPA CONTATTI]=-=-=-=-=[]\n\n", 1uLL, 0x29uLL, stdout);
pthread_create(&newthread, 0LL, r_stampa_contatti, arg);
func9(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 2:
fwrite("[]=-=-=-=-=-=[CERCA CONTATTO]=-=-=-=-=-=[]\n\n", 1uLL, 0x2CuLL, stdout);
pthread_create(&newthread, 0LL, r_cerca_contatto, arg);
func10(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 3:
fwrite("[]=-=-=-=-=[MODIFICA CONTATTO]=-=-=-=-=[]\n\n", 1uLL, 0x2BuLL, stdout);
pthread_create(&newthread, 0LL, r_modifica_contatto, arg);
func11(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 4:
fwrite("[]=-=-=-=-=-=[NUOVO CONTATTO]=-=-=-=-=-=[]\n\n", 1uLL, 0x2CuLL, stdout);
pthread_create(&newthread, 0LL, r_nuovo_contatto, arg);
func12(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 5:
fwrite("[]=-=-=-=-=[CANCELLA CONTATTO]=-=-=-=-=[]\n\n", 1uLL, 0x2BuLL, stdout);
pthread_create(&newthread, 0LL, r_cancella_contatto, arg);
func13(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 6:
fwrite("[]=-=-=-=-=-=[COMPACT TABELLA]=-=-=-=-=-=[]\n\n", 1uLL, 0x2DuLL, stdout);
pthread_create(&newthread, 0LL, r_compact_tabella, arg);
func14(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
case 7:
pthread_create(&newthread, 0LL, r_program_exit, arg);
func15(arg[0]);
if ( pthread_join(newthread, 0LL) )
{
fwrite("Impossibile aspettare il tread\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
return result;
default:
fwrite("[ERRORE] Scelta errata!\n", 1uLL, 0x18uLL, stdout);
break;
}
if ( (unsigned int)ricevi_opzione(arg[0], &v7) )
func0(arg[0]);
if ( v7 == 108 )
break;
if ( v9 == 7 )
goto LABEL_35;
}
fwrite(&unk_11B0, 1uLL, 0x39uLL, stdout);
fwrite(&unk_11F0, 1uLL, 0x1EuLL, stdout);
v9 = 7;
LABEL_35:
chiudi_connessione(arg[0]);
fwrite("Arrivederci\n", 1uLL, 0xCuLL, stdout);
exit(0);
}
}
v5 = argv[1];
if ( !strcmp(v5, "localhost") )
arg[0] = connetti("127.0.0.1", v3);
else
arg[0] = connetti(v5, v3);
goto LABEL_11;
}

original_code:
int main(int argc, char *argv[]) {
 int temp;
 struct elemento *arg;
 pthread_t tid;
 int i, parametro_riconosciuto=0;
 int portnum = 3456;
 int debug_info = 0;
 for (i=1 ; i<argc ; i++) {
  if ((strcmp(argv[i],"-r") == 0) || (strcmp(argv[i],"--reset") == 0)) {
   reset_database();
   exit(0);
  }
  if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"--help") == 0)) {
   stampa_help(argv[0]);
   exit(0);
  }
  if ((strcmp(argv[i],"-p") == 0) || (strcmp(argv[i],"--port") == 0)) {
   parametro_riconosciuto = 1;
   portnum = atoi(argv[++i]);
  }
  if ((strcmp(argv[i],"-v") == 0) || (strcmp(argv[i],"--verbose") == 0)) {
   parametro_riconosciuto = 1;
   debug_info = 1;
  }
  if (!parametro_riconosciuto) {
   fprintf(stdout,"OPZIONE NON RICONOSCIUTA.\n");
   stampa_help(argv[0]);
   exit(1);
  }
 }
 lista = adtl_crealista();
 main_thread = pthread_self();
 pthread_mutex_init(&scrittura, ((void *)0));
 pthread_mutex_init(&lettura, ((void *)0));
 pthread_mutex_init(&m_lista, ((void *)0));
 if (signal(2,handler_term) == ((__sighandler_t) -1))
  perror("signal"), exit(1);
 id_socket = avvia_server(10, portnum);
 fprintf(stdout,"Server avviato... premere <Control+C> per uscire.\n");
 while(1) {
  temp = accetta_connessione(id_socket);
  pthread_mutex_lock(&m_lista);
   lista = adtl_inserisci(lista, temp, debug_info);
   arg = adtl_elem_addr(lista, temp);
   pthread_create(&tid, ((void *)0), (void *(*)()) servi_client, arg);
  pthread_mutex_unlock(&m_lista);
  pthread_detach(tid);
 }
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[]) {
 int temp;
 struct elemento *arg;
 unsigned long tid;
 int i; int parametro_riconosciuto = 0;
 int portnum = 3456;
 int debug_info = 0;
 for (i=1 ; i<argc ; i++) {
  if ((strcmp(argv[i],"-r") == 0) || (strcmp(argv[i],"--reset") == 0)) {
   reset_database();
   exit(0);
  }
  if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"--help") == 0)) {
   stampa_help(argv[0]);
   exit(0);
  }
  if ((strcmp(argv[i],"-p") == 0) || (strcmp(argv[i],"--port") == 0)) {
   parametro_riconosciuto = 1;
   portnum = atoi(argv[++i]);
  }
  if ((strcmp(argv[i],"-v") == 0) || (strcmp(argv[i],"--verbose") == 0)) {
   parametro_riconosciuto = 1;
   debug_info = 1;
  }
  if (!parametro_riconosciuto) {
   fprintf(stdout,"OPZIONE NON RICONOSCIUTA.\n");
   stampa_help(argv[0]);
   exit(1);
  }
 }
 lista = adtl_crealista();
 main_thread = pthread_self();
 pthread_mutex_init(&scrittura, ((void *)0));
 pthread_mutex_init(&lettura, ((void *)0));
 pthread_mutex_init(&m_lista, ((void *)0));
 if (signal(2,handler_term) == ((__sighandler_t) -1))
  perror("signal"), exit(1);
 id_socket = avvia_server(10, portnum);
 fprintf(stdout,"Server avviato... premere <Control+C> per uscire.\n");
 while(1) {
  temp = accetta_connessione(id_socket);
  pthread_mutex_lock(&m_lista);
   lista = adtl_inserisci(lista, temp, debug_info);
   arg = adtl_elem_addr(lista, temp);
   pthread_create(&tid, ((void *)0), (void *(*)()) servi_client, arg);
  pthread_mutex_unlock(&m_lista);
  pthread_detach(tid);
 }
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: temp int

variable_types: struct elemento { int connessione; int debug_info; } *

variable_types:
tid
unsigned long

variable_types: portnum int


decompiled_code:
void handler_term()
{
pthread_t v0; // rax

v0 = pthread_self();
if ( pthread_equal(main_thread, v0) )
{
fwrite("Chiusura del server in corso...\n", 1uLL, 0x20uLL, stdout);
devo_terminare = 1;
arresta_server((unsigned int)id_socket);
pthread_exit(0LL);
}
}

canonical_decompiled_code:
void func0()
{
pthread_t v0; // rax

v0 = pthread_self();
if ( pthread_equal(main_thread, v0) )
{
fwrite("Chiusura del server in corso...\n", 1uLL, 0x20uLL, stdout);
devo_terminare = 1;
arresta_server((unsigned int)id_socket);
pthread_exit(0LL);
}
}

original_code:
static void handler_term(int signo) {
 int elemento;
 if(pthread_equal(main_thread,pthread_self()) != 0) {
  fprintf(stdout,"Chiusura del server in corso...\n");
  devo_terminare=1;
  arresta_server(id_socket);
  pthread_exit(0);
 }
}

variable_types: signo int

variable_types: elemento int