main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
struct addrinfo *v5; // r12
int v6; // eax
int v7; // ebx
_WORD *v8; // rax
const char *v10; // rax
int v11; // eax
unsigned int v12; // er12
_WORD *v13; // rax
struct timeval v14; // [rsp+0h] [rbp-2D8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-2C8h] BYREF
__m128i v16[8]; // [rsp+90h] [rbp-248h] BYREF
int optval; // [rsp+11Ch] [rbp-1BCh] BYREF
struct sigaction v18; // [rsp+120h] [rbp-1B8h] BYREF
char buf[60]; // [rsp+1C0h] [rbp-118h] BYREF
socklen_t addr_len; // [rsp+1FCh] [rbp-DCh] BYREF
struct sockaddr addr; // [rsp+200h] [rbp-D8h] BYREF
struct addrinfo *pai; // [rsp+288h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+290h] [rbp-48h] BYREF

optval = 1;
prepare_syslog();
syslog(6, "starting up...");
parse_cmdLine(argc, (__int64)argv);
if ( !option_ConfigFile )
{
fwrite("no config file given, exiting\n", 1uLL, 0x1EuLL, stderr);
syslog(3, "no config file given, exiting\n");
exit(8);
}
syslog(6, "using ConfigFile: %s", option_ConfigFile);
smtpConfigFile = parse_configfile(option_ConfigFile);
dump_configfile(smtpConfigFile);
if ( !option_DontFork )
daemonize();
syslog(6, "initialized.");
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v3 = getaddrinfo(*(const char **)smtpConfigFile, *(const char **)(smtpConfigFile + 8), &req, &pai);
v4 = v3;
if ( v3 )
{
v10 = gai_strerror(v3);
syslog(3, "getaddrinfo: %s\n", v10);
return 1;
}
v5 = pai;
if ( !pai )
{
LABEL_20:
syslog(3, "server: failed to bind\n");
return 2;
}
while ( 1 )
{
v6 = socket(v5->ai_family, v5->ai_socktype, v5->ai_protocol);
v7 = v6;
if ( v6 != -1 )
break;
perror("server: socket");
LABEL_19:
v5 = v5->ai_next;
if ( !v5 )
goto LABEL_20;
}
if ( setsockopt(v6, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v7, v5->ai_addr, v5->ai_addrlen) == -1 )
{
close(v7);
perror("server: bind");
goto LABEL_19;
}
v8 = get_in_addr(&v5->ai_addr->sa_family);
inet_ntop(v5->ai_family, v8, buf, 0x2Eu);
syslog(7, "listening on: %s", buf);
freeaddrinfo(pai);
if ( listen(v7, 10) == -1 )
{
perror("listen");
exit(1);
}
v18.sa_handler = sigchld_handler;
sigemptyset(&v18.sa_mask);
v18.sa_flags = 0x10000000;
if ( sigaction(17, &v18, 0LL) == -1 )
{
perror("sigaction SIGCHLD");
exit(1);
}
if ( sigaction(15, &v18, 0LL) == -1 )
{
perror("sigaction SIGTERM");
exit(1);
}
if ( sigaction(10, &v18, 0LL) == -1 )
{
perror("sigaction SIGUSR1");
exit(1);
}
syslog(6, "server: waiting for connections...");
memset(v16, 0, sizeof(v16));
memset(&readfds, 0, sizeof(readfds));
v16[0].m128i_i64[v7 / 64] |= 1LL << (v7 % 64);
while ( keep_going )
{
*(__m128i *)readfds.fds_bits = _mm_loadu_si128(v16);
*(__m128i *)&readfds.fds_bits[2] = _mm_loadu_si128(&v16[1]);
*(__m128i *)&readfds.fds_bits[4] = _mm_loadu_si128(&v16[2]);
*(__m128i *)&readfds.fds_bits[6] = _mm_loadu_si128(&v16[3]);
*(__m128i *)&readfds.fds_bits[8] = _mm_loadu_si128(&v16[4]);
*(__m128i *)&readfds.fds_bits[10] = _mm_loadu_si128(&v16[5]);
*(__m128i *)&readfds.fds_bits[12] = _mm_loadu_si128(&v16[6]);
*(__m128i *)&readfds.fds_bits[14] = _mm_loadu_si128(&v16[7]);
SIG_received = 0;
v14.tv_sec = 5LL;
v14.tv_usec = 0LL;
if ( select(v7 + 1, &readfds, 0LL, 0LL, &v14) == -1 )
{
if ( !SIG_received )
{
perror("select");
exit(4);
}
}
else
{
v11 = v4;
if ( v7 >= 0 )
{
while ( (readfds.fds_bits[v11 / 64] & (1LL << (v11 % 64))) == 0 || v7 != v11 )
{
if ( v7 < ++v11 )
goto LABEL_37;
}
addr_len = 128;
v12 = accept(v7, &addr, &addr_len);
if ( v12 == -1 )
{
perror("accept");
}
else
{
v13 = get_in_addr(&addr);
inet_ntop(addr.sa_family, v13, buf, 0x2Eu);
syslog(6, "server: got connection from %s\n", buf);
if ( !fork() )
{
close(v7);
syslog(6, "processing client %s\n", buf);
handle_NewClient(v12, &addr);
close(v12);
syslog(6, "client %s: connection closed\n", buf);
exit(0);
}
close(v12);
}
}
}
LABEL_37:
;
}
syslog(6, "cleaning up...");
memset(v16, 0, sizeof(v16));
memset(&readfds, 0, sizeof(readfds));
close(v7);
free_configfile(smtpConfigFile);
syslog(6, "shutdown");
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // ebp
struct addrinfo *v5; // r12
int v6; // eax
int v7; // ebx
_WORD *v8; // rax
const char *v10; // rax
int v11; // eax
unsigned int v12; // er12
_WORD *v13; // rax
struct timeval v14; // [rsp+0h] [rbp-2D8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-2C8h] BYREF
__m128i v16[8]; // [rsp+90h] [rbp-248h] BYREF
int optval; // [rsp+11Ch] [rbp-1BCh] BYREF
struct sigaction v18; // [rsp+120h] [rbp-1B8h] BYREF
char buf[60]; // [rsp+1C0h] [rbp-118h] BYREF
socklen_t addr_len; // [rsp+1FCh] [rbp-DCh] BYREF
struct sockaddr addr; // [rsp+200h] [rbp-D8h] BYREF
struct addrinfo *pai; // [rsp+288h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+290h] [rbp-48h] BYREF

optval = 1;
func5();
syslog(6, "starting up...");
func2(argc, (__int64)argv);
if ( !option_ConfigFile )
{
fwrite("no config file given, exiting\n", 1uLL, 0x1EuLL, stderr);
syslog(3, "no config file given, exiting\n");
exit(8);
}
syslog(6, "using ConfigFile: %s", option_ConfigFile);
smtpConfigFile = parse_configfile(option_ConfigFile);
dump_configfile(smtpConfigFile);
if ( !option_DontFork )
func3();
syslog(6, "initialized.");
*(_QWORD *)&req.ai_flags = 1LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v3 = getaddrinfo(*(const char **)smtpConfigFile, *(const char **)(smtpConfigFile + 8), &req, &pai);
v4 = v3;
if ( v3 )
{
v10 = gai_strerror(v3);
syslog(3, "getaddrinfo: %s\n", v10);
return 1;
}
v5 = pai;
if ( !pai )
{
LABEL_20:
syslog(3, "server: failed to bind\n");
return 2;
}
while ( 1 )
{
v6 = socket(v5->ai_family, v5->ai_socktype, v5->ai_protocol);
v7 = v6;
if ( v6 != -1 )
break;
perror("server: socket");
LABEL_19:
v5 = v5->ai_next;
if ( !v5 )
goto LABEL_20;
}
if ( setsockopt(v6, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
exit(1);
}
if ( bind(v7, v5->ai_addr, v5->ai_addrlen) == -1 )
{
close(v7);
perror("server: bind");
goto LABEL_19;
}
v8 = get_in_addr(&v5->ai_addr->sa_family);
inet_ntop(v5->ai_family, v8, buf, 0x2Eu);
syslog(7, "listening on: %s", buf);
freeaddrinfo(pai);
if ( listen(v7, 10) == -1 )
{
perror("listen");
exit(1);
}
v18.sa_handler = sigchld_handler;
sigemptyset(&v18.sa_mask);
v18.sa_flags = 0x10000000;
if ( sigaction(17, &v18, 0LL) == -1 )
{
perror("sigaction SIGCHLD");
exit(1);
}
if ( sigaction(15, &v18, 0LL) == -1 )
{
perror("sigaction SIGTERM");
exit(1);
}
if ( sigaction(10, &v18, 0LL) == -1 )
{
perror("sigaction SIGUSR1");
exit(1);
}
syslog(6, "server: waiting for connections...");
memset(v16, 0, sizeof(v16));
memset(&readfds, 0, sizeof(readfds));
v16[0].m128i_i64[v7 / 64] |= 1LL << (v7 % 64);
while ( keep_going )
{
*(__m128i *)readfds.fds_bits = _mm_loadu_si128(v16);
*(__m128i *)&readfds.fds_bits[2] = _mm_loadu_si128(&v16[1]);
*(__m128i *)&readfds.fds_bits[4] = _mm_loadu_si128(&v16[2]);
*(__m128i *)&readfds.fds_bits[6] = _mm_loadu_si128(&v16[3]);
*(__m128i *)&readfds.fds_bits[8] = _mm_loadu_si128(&v16[4]);
*(__m128i *)&readfds.fds_bits[10] = _mm_loadu_si128(&v16[5]);
*(__m128i *)&readfds.fds_bits[12] = _mm_loadu_si128(&v16[6]);
*(__m128i *)&readfds.fds_bits[14] = _mm_loadu_si128(&v16[7]);
SIG_received = 0;
v14.tv_sec = 5LL;
v14.tv_usec = 0LL;
if ( select(v7 + 1, &readfds, 0LL, 0LL, &v14) == -1 )
{
if ( !SIG_received )
{
perror("select");
exit(4);
}
}
else
{
v11 = v4;
if ( v7 >= 0 )
{
while ( (readfds.fds_bits[v11 / 64] & (1LL << (v11 % 64))) == 0 || v7 != v11 )
{
if ( v7 < ++v11 )
goto LABEL_37;
}
addr_len = 128;
v12 = accept(v7, &addr, &addr_len);
if ( v12 == -1 )
{
perror("accept");
}
else
{
v13 = get_in_addr(&addr);
inet_ntop(addr.sa_family, v13, buf, 0x2Eu);
syslog(6, "server: got connection from %s\n", buf);
if ( !fork() )
{
close(v7);
syslog(6, "processing client %s\n", buf);
func4(v12, &addr);
close(v12);
syslog(6, "client %s: connection closed\n", buf);
exit(0);
}
close(v12);
}
}
}
LABEL_37:
;
}
syslog(6, "cleaning up...");
memset(v16, 0, sizeof(v16));
memset(&readfds, 0, sizeof(readfds));
close(v7);
free_configfile(smtpConfigFile);
syslog(6, "shutdown");
return v4;
}

original_code:
int main(int argc, char *argv[])
{
    int listener, new_fd, fdmax;
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr;
    socklen_t sin_size;
    char s[46];
    struct sigaction sa;
    int yes=1, i;
    int rv;
    fd_set master, readfds;
    struct timeval tv_select;
    prepare_syslog();
    syslog(6, "starting up...");
    parse_cmdLine(argc, argv);
    if (!option_ConfigFile) {
          fprintf(stderr, "no config file given, exiting\n");
          syslog(3, "no config file given, exiting\n");
          exit(8);
    }
    syslog(6, "using ConfigFile: %s", option_ConfigFile);
    smtpConfigFile = parse_configfile(option_ConfigFile);
    dump_configfile(smtpConfigFile);
    if (option_DontFork == 0) {
      daemonize();
    }
    syslog(6, "initialized.");
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((rv = getaddrinfo(smtpConfigFile->listen_addr, smtpConfigFile->listen_port, &hints, &servinfo)) != 0) {
        syslog(3, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((listener = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(listener, 1, 2, &yes,
                sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(listener, p->ai_addr, p->ai_addrlen) == -1) {
            close(listener);
            perror("server: bind");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        syslog(3, "server: failed to bind\n");
        return 2;
    }
    inet_ntop(p->ai_family,
              get_in_addr((struct sockaddr *)p->ai_addr),
              s,
              sizeof s);
    syslog(7, "listening on: %s", s);
    freeaddrinfo(servinfo);
    if (listen(listener, 10) == -1) {
        perror("listen");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0x10000000;
    if (sigaction(17, &sa, ((void *)0)) == -1) {
        perror("sigaction SIGCHLD");
        exit(1);
    }
    if (sigaction(15, &sa, ((void *)0)) == -1) {
      perror("sigaction SIGTERM");
      exit(1);
    }
    if (sigaction(10, &sa, ((void *)0)) == -1) {
      perror("sigaction SIGUSR1");
      exit(1);
    }
    syslog(6, "server: waiting for connections...");
    fdmax = listener;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&master)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
    while(keep_going) {
        readfds = master;
        SIG_received = 0;
        tv_select.tv_sec = 5;
        tv_select.tv_usec = 0;
        if (select(fdmax+1, &readfds, ((void *)0), ((void *)0), &tv_select) == -1) {
           if (!SIG_received) {
               perror("select");
               exit(4);
           } else {
             continue;
           }
        }
        for (i = 0; i <= fdmax; i++) {
          if (((((&readfds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            if (i == listener) {
                sin_size = sizeof their_addr;
                new_fd = accept(listener, (struct sockaddr *)&their_addr, &sin_size);
                if (new_fd == -1) {
                    perror("accept");
                } else {
                    inet_ntop(their_addr.ss_family,
                              get_in_addr((struct sockaddr *)&their_addr),
                              s,
                              sizeof s
                             );
                    syslog(6, "server: got connection from %s\n", s);
                    if (!fork()) {
                        close(listener);
                        syslog(6, "processing client %s\n", s);
                        handle_NewClient(new_fd, &their_addr);
                        close(new_fd);
                        syslog(6, "client %s: connection closed\n", s);
                        exit(0);
                    }
                    close(new_fd);
                }
            }
          }
        }
    }
    syslog(6, "cleaning up...");
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
    close(listener);
    free_configfile(smtpConfigFile);
    syslog(6, "shutdown");
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int listener; int new_fd; int fdmax;
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
    struct sockaddr_storage their_addr;
    unsigned int sin_size;
    char s[46];
    struct sigaction sa;
    int yes = 1; int i;
    int rv;
    struct fd_set master; struct fd_set readfds;
    struct timeval tv_select;
    prepare_syslog();
    syslog(6, "starting up...");
    parse_cmdLine(argc, argv);
    if (!option_ConfigFile) {
          fprintf(stderr, "no config file given, exiting\n");
          syslog(3, "no config file given, exiting\n");
          exit(8);
    }
    syslog(6, "using ConfigFile: %s", option_ConfigFile);
    smtpConfigFile = parse_configfile(option_ConfigFile);
    dump_configfile(smtpConfigFile);
    if (option_DontFork == 0) {
      daemonize();
    }
    syslog(6, "initialized.");
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0001;
    if ((rv = getaddrinfo(smtpConfigFile->listen_addr, smtpConfigFile->listen_port, &hints, &servinfo)) != 0) {
        syslog(3, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((listener = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(listener, 1, 2, &yes,
                sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        if (bind(listener, p->ai_addr, p->ai_addrlen) == -1) {
            close(listener);
            perror("server: bind");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        syslog(3, "server: failed to bind\n");
        return 2;
    }
    inet_ntop(p->ai_family,
              get_in_addr((struct sockaddr *)p->ai_addr),
              s,
              sizeof s);
    syslog(7, "listening on: %s", s);
    freeaddrinfo(servinfo);
    if (listen(listener, 10) == -1) {
        perror("listen");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0x10000000;
    if (sigaction(17, &sa, ((void *)0)) == -1) {
        perror("sigaction SIGCHLD");
        exit(1);
    }
    if (sigaction(15, &sa, ((void *)0)) == -1) {
      perror("sigaction SIGTERM");
      exit(1);
    }
    if (sigaction(10, &sa, ((void *)0)) == -1) {
      perror("sigaction SIGUSR1");
      exit(1);
    }
    syslog(6, "server: waiting for connections...");
    fdmax = listener;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
    ((void) (((&master)->__fds_bits)[((listener) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listener) % (8 * (int) sizeof (__fd_mask)))))));
    while(keep_going) {
        readfds = master;
        SIG_received = 0;
        tv_select.tv_sec = 5;
        tv_select.tv_usec = 0;
        if (select(fdmax+1, &readfds, ((void *)0), ((void *)0), &tv_select) == -1) {
           if (!SIG_received) {
               perror("select");
               exit(4);
           } else {
             continue;
           }
        }
        for (i = 0; i <= fdmax; i++) {
          if (((((&readfds)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            if (i == listener) {
                sin_size = sizeof their_addr;
                new_fd = accept(listener, (struct sockaddr *)&their_addr, &sin_size);
                if (new_fd == -1) {
                    perror("accept");
                } else {
                    inet_ntop(their_addr.ss_family,
                              get_in_addr((struct sockaddr *)&their_addr),
                              s,
                              sizeof s
                             );
                    syslog(6, "server: got connection from %s\n", s);
                    if (!fork()) {
                        close(listener);
                        syslog(6, "processing client %s\n", s);
                        handle_NewClient(new_fd, &their_addr);
                        close(new_fd);
                        syslog(6, "client %s: connection closed\n", s);
                        exit(0);
                    }
                    close(new_fd);
                }
            }
          }
        }
    }
    syslog(6, "cleaning up...");
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&master)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readfds)->__fds_bits)[0]) : "memory"); } while (0);
    close(listener);
    free_configfile(smtpConfigFile);
    syslog(6, "shutdown");
    return 0;
}

variable_types: listener int

variable_types: fdmax int

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 sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
s
char [46]

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

variable_types: yes int

variable_types: rv int

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

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

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

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

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


repo: meissnol/smtp-duplicator

decompiled_code:
__int64 __fastcall textTo_smtp_configfileState(char *s1)
{
unsigned int v1; // ebx

v1 = 2 * (strcasecmp(s1, "LISTEN_PORT") == 0);
if ( !strcasecmp(s1, "LISTEN_ADDR") )
v1 = 1;
if ( !strcasecmp(s1, "FORWARD_PORT") )
v1 = 4;
if ( !strcasecmp(s1, "FORWARD_ADDR") )
v1 = 3;
if ( !strcasecmp(s1, "MAPFILE") )
v1 = 5;
if ( !strcasecmp(s1, " ") )
v1 = 0;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
unsigned int v1; // ebx

v1 = 2 * (strcasecmp(s1, "LISTEN_PORT") == 0);
if ( !strcasecmp(s1, "LISTEN_ADDR") )
v1 = 1;
if ( !strcasecmp(s1, "FORWARD_PORT") )
v1 = 4;
if ( !strcasecmp(s1, "FORWARD_ADDR") )
v1 = 3;
if ( !strcasecmp(s1, "MAPFILE") )
v1 = 5;
if ( !strcasecmp(s1, " ") )
v1 = 0;
return v1;
}

original_code:
smtp_configfileState textTo_smtp_configfileState(char *txt) {
  smtp_configfileState result = cfs_Unknown;
  if (strcasecmp(txt, "LISTEN_PORT") ==0) { result = cfs_listen_port; }
  if (strcasecmp(txt, "LISTEN_ADDR") ==0) { result = cfs_listen_addr; }
  if (strcasecmp(txt, "FORWARD_PORT") ==0) { result = cfs_fwd_port; }
  if (strcasecmp(txt, "FORWARD_ADDR") ==0) { result = cfs_fwd_addr; }
  if (strcasecmp(txt, "MAPFILE") ==0) { result = cfs_mapfile; }
  if (strcasecmp(txt, "  ") ==0) { result = cfs_Unknown; }
  return(result);
}

canonical_original_code:
enum t_configfileState textTo_smtp_configfileState(char *txt) {
  enum t_configfileState result = cfs_Unknown;
  if (strcasecmp(txt, "LISTEN_PORT") ==0) { result = cfs_listen_port; }
  if (strcasecmp(txt, "LISTEN_ADDR") ==0) { result = cfs_listen_addr; }
  if (strcasecmp(txt, "FORWARD_PORT") ==0) { result = cfs_fwd_port; }
  if (strcasecmp(txt, "FORWARD_ADDR") ==0) { result = cfs_fwd_addr; }
  if (strcasecmp(txt, "MAPFILE") ==0) { result = cfs_mapfile; }
  if (strcasecmp(txt, "  ") ==0) { result = cfs_Unknown; }
  return(result);
}

variable_types:
txt
char *

variable_types: enum t_configfileState { cfs_Unknown=0, cfs_listen_addr=1, cfs_listen_port=2, cfs_fwd_addr=3, cfs_fwd_port=4, cfs_mapfile=5 }


decompiled_code:
void __fastcall dump_configfile(__int64 a1)
{
_QWORD **v1; // rax
_QWORD *v2; // rbp
const char **v3; // rbx
unsigned int v4; // er12
__int64 v5; // rdx

syslog(6, "listen_addr: %s\n", *(const char **)a1);
syslog(6, "listen_port: %s\n", *(const char **)(a1 + 8));
syslog(6, "fwd_addr: %s\n", *(const char **)(a1 + 16));
syslog(6, "fwd_port: %s\n", *(const char **)(a1 + 24));
syslog(6, "mapfile: %s\n", *(const char **)(a1 + 32));
v1 = *(_QWORD ***)(a1 + 40);
if ( v1 )
{
v2 = *v1;
v3 = (const char **)malloc(0x20uLL);
if ( v2 )
{
v4 = 0;
do
{
llPop(*(_QWORD *)(a1 + 40), v2, v3);
v5 = v4++;
syslog(6, "mapping(%d): from='%s', to='%s', result='%s'\n", v5, *v3, v3[1], v3[2]);
v2 = (_QWORD *)*v2;
}
while ( v2 );
}
free(v3);
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
_QWORD **v1; // rax
_QWORD *v2; // rbp
const char **v3; // rbx
unsigned int v4; // er12
__int64 v5; // rdx

syslog(6, "listen_addr: %s\n", *(const char **)a1);
syslog(6, "listen_port: %s\n", *(const char **)(a1 + 8));
syslog(6, "fwd_addr: %s\n", *(const char **)(a1 + 16));
syslog(6, "fwd_port: %s\n", *(const char **)(a1 + 24));
syslog(6, "mapfile: %s\n", *(const char **)(a1 + 32));
v1 = *(_QWORD ***)(a1 + 40);
if ( v1 )
{
v2 = *v1;
v3 = (const char **)malloc(0x20uLL);
if ( v2 )
{
v4 = 0;
do
{
llPop(*(_QWORD *)(a1 + 40), v2, v3);
v5 = v4++;
syslog(6, "mapping(%d): from='%s', to='%s', result='%s'\n", v5, *v3, v3[1], v3[2]);
v2 = (_QWORD *)*v2;
}
while ( v2 );
}
free(v3);
}
}

original_code:
void dump_configfile(smtp_configfile* cfg) {
  syslog(6, "listen_addr: %s\n", cfg->listen_addr);
  syslog(6, "listen_port: %s\n", cfg->listen_port);
  syslog(6, "fwd_addr:    %s\n", cfg->fwd_addr);
  syslog(6, "fwd_port:    %s\n", cfg->fwd_port);
  syslog(6, "mapfile:     %s\n", cfg->mapfile);
  if (cfg->mappings) {
      llItem * itm = cfg->mappings->first;
      smtp_mapEntry* tmp = (smtp_mapEntry*)malloc(sizeof(smtp_mapEntry));
      int i = 0;
      while (itm) {
          llPop(cfg->mappings, itm, tmp);
          syslog(6, "mapping(%d): from='%s', to='%s', result='%s'\n",
                           i++, tmp->me_From, tmp->me_To, tmp->me_Result);
          itm = itm->next;
      }
      free(tmp);
  }
}

canonical_original_code:
void dump_configfile(struct t_configfile *cfg) {
  syslog(6, "listen_addr: %s\n", cfg->listen_addr);
  syslog(6, "listen_port: %s\n", cfg->listen_port);
  syslog(6, "fwd_addr:    %s\n", cfg->fwd_addr);
  syslog(6, "fwd_port:    %s\n", cfg->fwd_port);
  syslog(6, "mapfile:     %s\n", cfg->mapfile);
  if (cfg->mappings) {
      struct _llItem *itm = cfg->mappings->first;
      struct t_mappingEntry *tmp = (smtp_mapEntry*)malloc(sizeof(smtp_mapEntry));
      int i = 0;
      while (itm) {
          llPop(cfg->mappings, itm, tmp);
          syslog(6, "mapping(%d): from='%s', to='%s', result='%s'\n",
                           i++, tmp->me_From, tmp->me_To, tmp->me_Result);
          itm = itm->next;
      }
      free(tmp);
  }
}

variable_types: struct t_configfile { char *listen_addr; char *listen_port; char *fwd_addr; char *fwd_port; char *mapfile; struct _llList { struct _llItem { struct _llItem *next; void *data_ptr; } *first; unsigned long dataSize; } *mappings; } *

variable_types: struct t_mappingEntry { char *me_From; char *me_To; char *me_Result; enum t_mappingType { cfgType_NONE=0, cfgType_From=1, cfgType_To=2, cfgType_Result=3 } match; } *

variable_types: i int

return_type: void

user_defined_types: struct t_configfile { char *listen_addr; char *listen_port; char *fwd_addr; char *fwd_port; char *mapfile; struct _llList *mappings; }

user_defined_types: struct t_mappingEntry { char *me_From; char *me_To; char *me_Result; enum t_mappingType match; }

user_defined_types: struct _llList { struct _llItem *first; unsigned long dataSize; }


decompiled_code:
void __fastcall free_configfile(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
llFree((char *)ptr + 40);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
free(*((void **)ptr + 4));
llFree((char *)ptr + 40);
free(ptr);
}

original_code:
void free_configfile(smtp_configfile* cfg) {
  free(cfg->listen_addr);
  free(cfg->listen_port);
  free(cfg->fwd_addr);
  free(cfg->fwd_port);
  free(cfg->mapfile);
  llFree(&cfg->mappings);
  free(cfg);
  cfg=((void *)0);
}

canonical_original_code:
void free_configfile(struct t_configfile *cfg) {
  free(cfg->listen_addr);
  free(cfg->listen_port);
  free(cfg->fwd_addr);
  free(cfg->fwd_port);
  free(cfg->mapfile);
  llFree(&cfg->mappings);
  free(cfg);
  cfg=((void *)0);
}

user_defined_types: struct _llItem { struct _llItem *next; void *data_ptr; }


decompiled_code:
char *__fastcall init_configfile(char **a1)
{
char *v1; // rax
char *v2; // rax
char *result; // rax

a1[2] = 0LL;
a1[3] = 0LL;
a1[4] = 0LL;
a1[5] = 0LL;
v1 = (char *)malloc(5uLL);
a1[1] = v1;
strcpy(v1, "3490");
v2 = (char *)malloc(3uLL);
a1[3] = v2;
strcpy(v2, "25");
result = (char *)malloc(3uLL);
*a1 = result;
strcpy(result, "::");
return result;
}

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

a1[2] = 0LL;
a1[3] = 0LL;
a1[4] = 0LL;
a1[5] = 0LL;
v1 = (char *)malloc(5uLL);
a1[1] = v1;
strcpy(v1, "3490");
v2 = (char *)malloc(3uLL);
a1[3] = v2;
strcpy(v2, "25");
result = (char *)malloc(3uLL);
*a1 = result;
strcpy(result, "::");
return result;
}

original_code:
void init_configfile(smtp_configfile* cfg) {
  bzero(cfg, sizeof(smtp_configfile));
  cfg->listen_port = (char*)malloc(sizeof(char)* strlen("3490")+1);
  strcpy(cfg->listen_port, "3490");
  cfg->fwd_port = (char*)malloc(sizeof(char)* strlen("25")+1);
  strcpy(cfg->fwd_port, "25");
  cfg->listen_addr = (char*)malloc(sizeof(char)* strlen("::")+1);
  strcpy(cfg->listen_addr, "::");
}

canonical_original_code:
void init_configfile(struct t_configfile *cfg) {
  bzero(cfg, sizeof(smtp_configfile));
  cfg->listen_port = (char *)malloc(sizeof(char)* strlen("3490")+1);
  strcpy(cfg->listen_port, "3490");
  cfg->fwd_port = (char *)malloc(sizeof(char)* strlen("25")+1);
  strcpy(cfg->fwd_port, "25");
  cfg->listen_addr = (char *)malloc(sizeof(char)* strlen("::")+1);
  strcpy(cfg->listen_addr, "::");
}


chomp

decompiled_code:
size_t __fastcall chomp(const char *a1)
{
size_t result; // rax

result = strlen(a1);
a1[result - 1] = 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func4(const char *a1)
{
size_t result; // rax

result = strlen(a1);
a1[result - 1] = 0;
return result;
}

original_code:
void chomp(char *str) {
   size_t p=strlen(str);
   str[p-1]='\0';
}

canonical_original_code:
void chomp(char *str) {
   unsigned long p = strlen(str);
   str[p-1]='\0';
}

variable_types:
str
char *

variable_types:
p
unsigned long


splitQuotedStr

variable_types:
delims
char *

variable_types:
flgs
unsigned int

variable_types: maxWrdCnt int

variable_types:
words
char ***

variable_types: wrdcnt int

variable_types:
flags
unsigned int

variable_types:
Buffer
char *

variable_types:
bufPos
char *

variable_types:
ptr
char *

variable_types:
p
char *

return_type: int


decompiled_code:
__int64 __fastcall quote_test(int a1, __int64 a2)
{
char v2; // dl
int v3; // eax
int v4; // ebx
__int64 v5; // r12
__int64 v6; // rbx
__int64 v7; // rax
__int64 v8; // rbx
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
char v11[203]; // [rsp+Dh] [rbp-CBh] BYREF

strcpy(
&v11[3],
"'das, soll (mit \"qu'o\"t\"i'ng\")' \"ein'wirklich , \\\"wirklich\\\" \",\"langer und 'unsinniger'\",,'\"test-\"text"
"'und\\ \\,,, \\ so \"sein aber mit 'anfuehrungszeichen' und esc\\'s\",");
strcpy(v11, ", ");
ptr = 0LL;
v2 = 0;
if ( a1 > 0 )
v2 = 32 * (**(_BYTE **)(a2 + 8) == 49);
v3 = splitQuotedStr(&v11[3], v11, v2, -1, &ptr);
v4 = v3;
if ( v3 < 0 )
{
fprintf(stdout, "\nERROR processing line: %d\n", (unsigned int)v3);
}
else
{
fprintf(stdout, "\nstr=[%s]\n", &v11[3]);
fwrite("result follows\n", 1uLL, 0xFuLL, stdout);
if ( v4 )
{
v5 = (unsigned int)(v4 - 1);
v6 = 0LL;
do
{
fprintf(stdout, "words[%d]: [%s]\n", (unsigned int)v6, *((const char **)ptr + v6));
v7 = v6++;
}
while ( v7 != v5 );
v8 = 0LL;
do
{
free(*(void **)((char *)ptr + v8));
v8 += 8LL;
}
while ( 8 * (v5 + 1) != v8 );
}
}
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2)
{
char v2; // dl
int v3; // eax
int v4; // ebx
__int64 v5; // r12
__int64 v6; // rbx
__int64 v7; // rax
__int64 v8; // rbx
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
char v11[203]; // [rsp+Dh] [rbp-CBh] BYREF

strcpy(
&v11[3],
"'das, soll (mit \"qu'o\"t\"i'ng\")' \"ein'wirklich , \\\"wirklich\\\" \",\"langer und 'unsinniger'\",,'\"test-\"text"
"'und\\ \\,,, \\ so \"sein aber mit 'anfuehrungszeichen' und esc\\'s\",");
strcpy(v11, ", ");
ptr = 0LL;
v2 = 0;
if ( a1 > 0 )
v2 = 32 * (**(_BYTE **)(a2 + 8) == 49);
v3 = func0(&v11[3], v11, v2, -1, &ptr);
v4 = v3;
if ( v3 < 0 )
{
fprintf(stdout, "\nERROR processing line: %d\n", (unsigned int)v3);
}
else
{
fprintf(stdout, "\nstr=[%s]\n", &v11[3]);
fwrite("result follows\n", 1uLL, 0xFuLL, stdout);
if ( v4 )
{
v5 = (unsigned int)(v4 - 1);
v6 = 0LL;
do
{
fprintf(stdout, "words[%d]: [%s]\n", (unsigned int)v6, *((const char **)ptr + v6));
v7 = v6++;
}
while ( v7 != v5 );
v8 = 0LL;
do
{
free(*(void **)((char *)ptr + v8));
v8 += 8LL;
}
while ( 8 * (v5 + 1) != v8 );
}
}
free(ptr);
return 0LL;
}

original_code:
int quote_test (int argc, char *argv[]) {
  char str[] = "\'das, soll (mit \"qu\'o\"t\"i\'ng\")\' \"ein\'wirklich , "
               "\\\"wirklich\\\" \",\"langer und \'unsinniger\'\",,\'\"test-\"text"
               "\'und\\ \\,,, \\  so \"sein aber mit \'anfuehrungszeichen\' und esc\\\'s\",";
  char del[] = ", ";
  char **words = ((void *)0);
  uint i = 0;
  if (argc > 0) {
    if (*argv[1] == '1')
       (i |= (1 << 5));
  }
  int wrdcnt = splitQuotedStr(str, del, i, -1, &words);
  if (wrdcnt >= 0) {
     fprintf(stdout, "\nstr=[%s]\n", str);
     fprintf(stdout, "result follows\n");
     for (i = 0; i < wrdcnt; i++)
       fprintf(stdout, "words[%d]: [%s]\n", i, words[i]);
       for (i = 0; i < wrdcnt; i++)
         free(words[i]);
  } else {
       fprintf(stdout, "\nERROR processing line: %d\n", wrdcnt);
  }
  free(words);
  return(0);
}

canonical_original_code:
int quote_test(int argc, char *argv[]) {
  char str[] = "\'das, soll (mit \"qu\'o\"t\"i\'ng\")\' \"ein\'wirklich , "
               "\\\"wirklich\\\" \",\"langer und \'unsinniger\'\",,\'\"test-\"text"
               "\'und\\ \\,,, \\  so \"sein aber mit \'anfuehrungszeichen\' und esc\\\'s\",";
  char del[] = ", ";
  char **words = ((void *)0);
  unsigned int i = 0;
  if (argc > 0) {
    if (*argv[1] == '1')
       (i |= (1 << 5));
  }
  int wrdcnt = splitQuotedStr(str, del, i, -1, &words);
  if (wrdcnt >= 0) {
     fprintf(stdout, "\nstr=[%s]\n", str);
     fprintf(stdout, "result follows\n");
     for (i = 0; i < wrdcnt; i++)
       fprintf(stdout, "words[%d]: [%s]\n", i, words[i]);
       for (i = 0; i < wrdcnt; i++)
         free(words[i]);
  } else {
       fprintf(stdout, "\nERROR processing line: %d\n", wrdcnt);
  }
  free(words);
  return(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
str
char []

variable_types:
del
char []

variable_types:
words
char **

variable_types:
i
unsigned int


decompiled_code:
__int64 duplicator_OpenServerSocket()
{
int v0; // eax
unsigned int v1; // ebp
struct addrinfo *i; // rbx
int v3; // edi
const char *v5; // rax
int optval; // [rsp+4h] [rbp-54h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v0 = getaddrinfo(*(const char **)(smtpConfigFile + 16), *(const char **)(smtpConfigFile + 24), &req, &pai);
v1 = v0;
if ( v0 )
{
v5 = gai_strerror(v0);
syslog(3, "getaddrinfo: %s\n", v5);
v1 = -1;
}
else
{
for ( i = pai; i; i = i->ai_next )
{
v3 = socket(i->ai_family, i->ai_socktype, i->ai_protocol);
dp_ServerSock = v3;
if ( v3 == -1 )
{
perror("server: socket");
}
else
{
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("server: setsockopt");
return (unsigned int)-1;
}
if ( connect(dp_ServerSock, i->ai_addr, i->ai_addrlen) != -1 )
return v1;
close(dp_ServerSock);
perror("server: connect");
}
}
syslog(
3,
"duplicator: failed to connect to mailserver @host:%s; port:%s\n",
*(const char **)(smtpConfigFile + 16),
*(const char **)(smtpConfigFile + 24));
v1 = -2;
}
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // eax
unsigned int v1; // ebp
struct addrinfo *i; // rbx
int v3; // edi
const char *v5; // rax
int optval; // [rsp+4h] [rbp-54h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

optval = 1;
*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v0 = getaddrinfo(*(const char **)(smtpConfigFile + 16), *(const char **)(smtpConfigFile + 24), &req, &pai);
v1 = v0;
if ( v0 )
{
v5 = gai_strerror(v0);
syslog(3, "getaddrinfo: %s\n", v5);
v1 = -1;
}
else
{
for ( i = pai; i; i = i->ai_next )
{
v3 = socket(i->ai_family, i->ai_socktype, i->ai_protocol);
dp_ServerSock = v3;
if ( v3 == -1 )
{
perror("server: socket");
}
else
{
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("server: setsockopt");
return (unsigned int)-1;
}
if ( connect(dp_ServerSock, i->ai_addr, i->ai_addrlen) != -1 )
return v1;
close(dp_ServerSock);
perror("server: connect");
}
}
syslog(
3,
"duplicator: failed to connect to mailserver @host:%s; port:%s\n",
*(const char **)(smtpConfigFile + 16),
*(const char **)(smtpConfigFile + 24));
v1 = -2;
}
return v1;
}

original_code:
int duplicator_OpenServerSocket() {
    struct addrinfo hints, *servinfo, *p;
    int rv, yes=1;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(smtpConfigFile->fwd_addr,
                          smtpConfigFile->fwd_port,
                          &hints,
                          &servinfo)) != 0) {
        syslog(3, "getaddrinfo: %s\n", gai_strerror(rv));
        return -1;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((dp_ServerSock = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(dp_ServerSock, 1, 2, &yes,
                sizeof(int)) == -1) {
            perror("server: setsockopt");
            return(-1);
        }
        if (connect(dp_ServerSock, p->ai_addr, p->ai_addrlen) == -1) {
            close(dp_ServerSock);
            perror("server: connect");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        syslog(3, "duplicator: failed to connect to mailserver @host:%s; port:%s\n",
                        smtpConfigFile->fwd_addr, smtpConfigFile->fwd_port);
        return -2;
    }
  return(0);
}

canonical_original_code:
int duplicator_OpenServerSocket() {
    struct addrinfo hints; struct addrinfo *servinfo; struct addrinfo *p;
    int rv; int yes = 1;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(smtpConfigFile->fwd_addr,
                          smtpConfigFile->fwd_port,
                          &hints,
                          &servinfo)) != 0) {
        syslog(3, "getaddrinfo: %s\n", gai_strerror(rv));
        return -1;
    }
    for(p = servinfo; p != ((void *)0); p = p->ai_next) {
        if ((dp_ServerSock = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if (setsockopt(dp_ServerSock, 1, 2, &yes,
                sizeof(int)) == -1) {
            perror("server: setsockopt");
            return(-1);
        }
        if (connect(dp_ServerSock, p->ai_addr, p->ai_addrlen) == -1) {
            close(dp_ServerSock);
            perror("server: connect");
            continue;
        }
        break;
    }
    if (p == ((void *)0)) {
        syslog(3, "duplicator: failed to connect to mailserver @host:%s; port:%s\n",
                        smtpConfigFile->fwd_addr, smtpConfigFile->fwd_port);
        return -2;
    }
  return(0);
}

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

variable_types: yes int


decompiled_code:
__int64 duplicator_CloseServerSocket()
{
if ( dp_ServerSock )
close(dp_ServerSock);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
if ( dp_ServerSock )
close(dp_ServerSock);
return 0LL;
}

original_code:
int duplicator_CloseServerSocket() {
  if (dp_ServerSock) {
     close(dp_ServerSock);
  }
  return(0);
}


decompiled_code:
__int64 __fastcall duplicator_handleRelayMsg(__int64 a1)
{
int v2; // eax
int v3; // ebp
char v4[1064]; // [rsp+0h] [rbp-428h] BYREF

if ( *(_DWORD *)(a1 + 56) == 2 )
{
v2 = **(_DWORD **)(a1 + 16);
if ( v2 == 2 )
{
duplicator_OpenServerSocket();
}
else
{
v3 = 0;
if ( v2 == 11 )
{
duplicator_CloseServerSocket();
}
else
{
do
v3 += write(dp_ServerSock, *(const void **)(a1 + 32), *(int *)(a1 + 40));
while ( *(_DWORD *)(a1 + 40) > v3 );
do
{
memset(v4, 0, 0x400uLL);
smtprelay_readline((unsigned int)dp_ServerSock, v4, 1023LL);
}
while ( v4[3] == 45 );
}
}
}
if ( *(_DWORD *)(a1 + 56) == 1 && **(_DWORD **)(a1 + 16) == 7 )
write(dp_ServerSock, *(const void **)(a1 + 32), *(int *)(a1 + 40));
return 0LL;
}

original_code:
int duplicator_handleRelayMsg(struct SMTPSession *session) {
    int nWrite = 0, nRead = 0;
    if (session->event == hmsg_RelayFromClientToServer) {
       switch(*session->currState) {
         case ss_Connect:
            if (duplicator_OpenServerSocket() != 0) {
             }
             break;
    case ss_CleanUp:
             duplicator_CloseServerSocket();
             break;
         default:
             nWrite = 0;
             char tbuffer[1024 +1];
             while (
                (nWrite += write(dp_ServerSock, session->data, session->dataSize)) <
                  session->dataSize);
             do {
                bzero(tbuffer, 1024);
                nRead += smtprelay_readline(dp_ServerSock, tbuffer, 1024 -1);
             } while (tbuffer[3] == '-');
       }
    }
    if (session->event == hmsg_SendFromClientToServer ) {
       if (*session->currState == ss_Data) {
           write(dp_ServerSock, session->data, session->dataSize);
       }
 }
 return(0);
}

canonical_original_code:
int duplicator_handleRelayMsg(struct SMTPSession *session) {
    int nWrite = 0; int nRead = 0;
    if (session->event == hmsg_RelayFromClientToServer) {
       switch(*session->currState) {
         case ss_Connect:
            if (duplicator_OpenServerSocket() != 0) {
             }
             break;
    case ss_CleanUp:
             duplicator_CloseServerSocket();
             break;
         default:
             nWrite = 0;
             char tbuffer[-1];
             while (
                (nWrite += write(dp_ServerSock, session->data, session->dataSize)) <
                  session->dataSize);
             do {
                bzero(tbuffer, 1024);
                nRead += smtprelay_readline(dp_ServerSock, tbuffer, 1024 -1);
             } while (tbuffer[3] == '-');
       }
    }
    if (session->event == hmsg_SendFromClientToServer ) {
       if (*session->currState == ss_Data) {
           write(dp_ServerSock, session->data, session->dataSize);
       }
 }
 return(0);
}

variable_types: struct SMTPSession { int *clientSock; int *serverSock; enum SMTPState { ss_Unknown=0, ss_Inititalize=1, ss_Connect=2, ss_Helo=3, ss_MailFrom=4, ss_RcptTo=5, ss_DataBegin=6, ss_Data=7, ss_DataEnd=8, ss_Rset=9, ss_Quit=10, ss_CleanUp=11, ss_DisConn=12 } *currState; enum SMTPRetCode { sr_Unknown=0, sr_1=1, sr_2=2, sr_3=3, sr_4=4, sr_5=5 } currReply; char *data; int dataSize; struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *client_addr; enum stmpHandleMsg_Event { hmsg_Unknown=0, hmsg_SendFromClientToServer=1, hmsg_RelayFromClientToServer=2, hmsg_RelayFromServerToClient=3 } event; int _sendDataOut; unsigned int initMappingData; int sendDataToClientRequested; } *

variable_types: nWrite int

variable_types: nRead int

variable_types:
tbuffer
char [-1]

user_defined_types: struct SMTPSession { int *clientSock; int *serverSock; enum SMTPState *currState; enum SMTPRetCode currReply; char *data; int dataSize; struct sockaddr_storage *client_addr; enum stmpHandleMsg_Event event; int _sendDataOut; unsigned int initMappingData; int sendDataToClientRequested; }


llInit

decompiled_code:
__int64 __fastcall llInit(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax

v2 = malloc(0x10uLL);
*a1 = v2;
if ( !v2 )
return 0LL;
v2[1] = a2;
*v2 = 0LL;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax

v2 = malloc(0x10uLL);
*a1 = v2;
if ( !v2 )
return 0LL;
v2[1] = a2;
*v2 = 0LL;
return 1LL;
}

original_code:
int llInit(llList **list, size_t dataSize) {
    int result = 0;
    *list = (llList*)malloc(sizeof(llList));
    if (*list) {
        (*list)->dataSize = dataSize;
        (*list)->first = ((void *)0);
        result = 1;
    }
    return result;
}

canonical_original_code:
int llInit(struct _llList **list, unsigned long dataSize) {
    int result = 0;
    *list = (struct _llList *)malloc(sizeof(llList));
    if (*list) {
        (*list)->dataSize = dataSize;
        (*list)->first = ((void *)0);
        result = 1;
    }
    return result;
}

variable_types: struct _llList { struct _llItem { struct _llItem *next; void *data_ptr; } *first; unsigned long dataSize; } **

variable_types:
dataSize
unsigned long

variable_types: result int


llFree

original_code:
void llFree(llList **list) {
    if (*list) {
        llItem *item = (*list)->first;
        llItem *tmp;
        while (item) {
            free(item->data_ptr);
            tmp = item->next;
            free(item);
            if (tmp) {
                tmp->prev = ((void *)0);
            }
            item = tmp;
        }
    }
    free((*list));
    (*list) = ((void *)0);
}

canonical_original_code:
void llFree(struct _llList **list) {
    if (*list) {
        struct _llItem *item = (*list)->first;
        struct _llItem *tmp;
        while (item) {
            free(item->data_ptr);
            tmp = item->next;
            free(item);
            if (tmp) {
                tmp->prev = ((void *)0);
            }
            item = tmp;
        }
    }
    free((*list));
    (*list) = ((void *)0);
}

variable_types: struct _llItem { struct _llItem *next; void *data_ptr; } *


llPush

original_code:
void llPush(llList *list, void *data) {
    if (!list) { return; }
    if (list->dataSize <= 0) { return; }
    llItem *newItem = (llItem*)malloc(sizeof(llItem));
    newItem->data_ptr = (void*)malloc(list->dataSize);
    newItem->prev = ((void *)0);
    newItem->next = ((void *)0);
    memcpy((void*)newItem->data_ptr, (void*)data, list->dataSize);
    if (list->first) {
        list->first->prev = newItem;
        newItem->next = list->first;
    }
    list->first = newItem;
}

canonical_original_code:
void llPush(struct _llList *list, void *data) {
    if (!list) { return; }
    if (list->dataSize <= 0) { return; }
    struct _llItem *newItem = (llItem*)malloc(sizeof(llItem));
    newItem->data_ptr = (void *)malloc(list->dataSize);
    newItem->prev = ((void *)0);
    newItem->next = ((void *)0);
    memcpy((void *)newItem->data_ptr, (void *)data, list->dataSize);
    if (list->first) {
        list->first->prev = newItem;
        newItem->next = list->first;
    }
    list->first = newItem;
}

variable_types: struct _llList { struct _llItem { struct _llItem *next; void *data_ptr; } *first; unsigned long dataSize; } *

variable_types:
data
void *


llPop

decompiled_code:
__int64 __fastcall llPop(__int64 *a1, __int64 a2, void *a3)
{
if ( !a1 )
return 0LL;
if ( !*a1 )
return 0LL;
if ( !a2 )
a2 = *a1;
memcpy(a3, *(const void **)(a2 + 16), a1[1]);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1, __int64 a2, void *a3)
{
if ( !a1 )
return 0LL;
if ( !*a1 )
return 0LL;
if ( !a2 )
a2 = *a1;
memcpy(a3, *(const void **)(a2 + 16), a1[1]);
return 1LL;
}

original_code:
int llPop(llList *list, llItem *item, void *data) {
    int bOK = ((list) && (list->first) && (list->dataSize >= 0));
    if (!bOK) { return 0; }
    bOK = 0;
    if (!item) {
        item = list->first;
    }
    memcpy((void*)data, (void*)(item->data_ptr), list->dataSize);
    bOK = 1;
    return bOK;
}

canonical_original_code:
int llPop(struct _llList *list, struct _llItem *item, void *data) {
    int bOK = ((list) && (list->first) && (list->dataSize >= 0));
    if (!bOK) { return 0; }
    bOK = 0;
    if (!item) {
        item = list->first;
    }
    memcpy((void *)data, (void *)(item->data_ptr), list->dataSize);
    bOK = 1;
    return bOK;
}

variable_types:
item
struct _llItem *

variable_types: bOK int


repo: mengguang/mg-common-utils

decompiled_code:
int __fastcall g_func_print(const char *a1)
{
return puts(a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return puts(a1);
}

original_code:
void g_func_print(gpointer data,gpointer user_data) {
    printf("%s\n",data);
}

canonical_original_code:
void g_func_print(void *data, void *user_data) {
    printf("%s\n",data);
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
unsigned int v4; // eax
__int64 v5; // rbx
const char **v6; // rax
const char **v7; // rax
const char **v8; // rax

g_slist_append();
g_slist_append();
v3 = g_slist_prepend();
v4 = g_slist_length();
printf("length of list: %d\n", v4);
g_slist_foreach();
if ( v3 )
{
v5 = v3;
do
{
printf("for loop: %s\n", *(const char **)v5);
v5 = *(_QWORD *)(v5 + 8);
}
while ( v5 );
}
putchar(10);
g_slist_insert();
g_slist_foreach();
putchar(10);
g_slist_insert();
g_slist_foreach();
putchar(10);
g_slist_nth();
g_slist_delete_link();
g_slist_foreach();
putchar(10);
g_slist_nth();
g_slist_delete_link();
g_slist_foreach();
putchar(10);
g_slist_free();
g_list_append();
g_list_prepend();
g_list_insert();
v6 = (const char **)g_list_first();
printf("first element: %s\n", *v6);
v7 = (const char **)g_list_last();
printf("last element: %s\n", *v7);
v8 = (const char **)g_list_last();
if ( v8 )
v8 = *(const char ***)(g_list_last() + 16);
printf("previous element: %s\n", *v8);
return 0;
}

original_code:
int main(int argc,char **argv) {
    GSList *sl=((void *)0);
    sl=g_slist_append(sl,"laomeng188@163.com");
    sl=g_slist_append(sl,"laozhang@gmail.com");
    sl=g_slist_prepend(sl,"laoli@qq.com");
    printf("length of list: %d\n",g_slist_length(sl));
    g_slist_foreach(sl,g_func_print,((void *)0));
    for(GSList *s=sl;s!=((void *)0);s=((s) ? (((GSList *)(s))->next) : ((void *)0))) {
        printf("for loop: %s\n",s->data);
    }
    printf("\n");
    sl=g_slist_insert(sl,"xiaowang@163.com",1);
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_insert(sl,"xiaoxia@163.com",1);
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_delete_link(sl,g_slist_nth(sl,1));
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_delete_link(sl,g_slist_nth(sl,1));
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    g_slist_free(sl);
    GList *l=((void *)0);
    l=g_list_append(l,"a");
    l=g_list_prepend(l,"b");
    l=g_list_insert(l,"c",1);
    printf("first element: %s\n",g_list_first(l)->data);
    printf("last element: %s\n",g_list_last(l)->data);
    printf("previous element: %s\n",((g_list_last(l)) ? (((GList *)(g_list_last(l)))->prev) : ((void *)0))->data);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    struct _GSList *sl = ((void *)0);
    sl=g_slist_append(sl,"laomeng188@163.com");
    sl=g_slist_append(sl,"laozhang@gmail.com");
    sl=g_slist_prepend(sl,"laoli@qq.com");
    printf("length of list: %d\n",g_slist_length(sl));
    g_slist_foreach(sl,g_func_print,((void *)0));
    for(struct _GSList *s = sl;s!=((void *)0);s=((s) ? (((struct _GSList *)(s))->next) : ((void *)0))) {
        printf("for loop: %s\n",s->data);
    }
    printf("\n");
    sl=g_slist_insert(sl,"xiaowang@163.com",1);
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_insert(sl,"xiaoxia@163.com",1);
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_delete_link(sl,g_slist_nth(sl,1));
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    sl=g_slist_delete_link(sl,g_slist_nth(sl,1));
    g_slist_foreach(sl,g_func_print,((void *)0));
    printf("\n");
    g_slist_free(sl);
    struct _GList *l = ((void *)0);
    l=g_list_append(l,"a");
    l=g_list_prepend(l,"b");
    l=g_list_insert(l,"c",1);
    printf("first element: %s\n",g_list_first(l)->data);
    printf("last element: %s\n",g_list_last(l)->data);
    printf("previous element: %s\n",((g_list_last(l)) ? (((struct _GList *)(g_list_last(l)))->prev) : ((void *)0))->data);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct _GSList { void *data; struct _GSList *next; } *

variable_types: struct _GList { void *data; struct _GList *next; struct _GList *prev; } *


listCreate

decompiled_code:
_QWORD *listCreate()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
if ( result )
{
result[1] = 0LL;
*result = 0LL;
*((_DWORD *)result + 10) = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x30uLL);
if ( result )
{
result[1] = 0LL;
*result = 0LL;
*((_DWORD *)result + 10) = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
}
return result;
}

original_code:
list *listCreate(void)
{
    struct list *list;
    if ((list = malloc(sizeof(*list))) == ((void *)0))
        return ((void *)0);
    list->head = list->tail = ((void *)0);
    list->len = 0;
    list->dup = ((void *)0);
    list->free = ((void *)0);
    list->match = ((void *)0);
    return list;
}

canonical_original_code:
struct list *listCreate()
{
    struct list *list;
    if ((list = malloc(sizeof(*list))) == ((void *)0))
        return ((void *)0);
    list->head = list->tail = ((void *)0);
    list->len = 0;
    list->dup = ((void *)0);
    list->free = ((void *)0);
    list->match = ((void *)0);
    return list;
}

variable_types: struct list { struct listNode { struct listNode *prev; struct listNode *next; void *value; } *head; struct listNode *tail; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); unsigned int len; } *

user_defined_types: struct list { struct listNode *head; struct listNode *tail; void *(*dup)(void *ptr); void (*free)(void *ptr); int (*match)(void *ptr, void *key); unsigned int len; }


listRelease

decompiled_code:
void __fastcall listRelease(void *ptr)
{
_QWORD *v1; // r12
int v2; // eax
int v3; // ebx
_QWORD *v4; // r13
void (__fastcall *v5)(_QWORD); // rax

v1 = *(_QWORD **)ptr;
v2 = *((_DWORD *)ptr + 10);
v3 = v2 - 1;
if ( v2 )
{
do
{
v4 = v1;
v1 = (_QWORD *)v1[1];
v5 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 3);
if ( v5 )
v5(v4[2]);
free(v4);
--v3;
}
while ( v3 != -1 );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v1; // r12
int v2; // eax
int v3; // ebx
_QWORD *v4; // r13
void (__fastcall *v5)(_QWORD); // rax

v1 = *(_QWORD **)ptr;
v2 = *((_DWORD *)ptr + 10);
v3 = v2 - 1;
if ( v2 )
{
do
{
v4 = v1;
v1 = (_QWORD *)v1[1];
v5 = (void (__fastcall *)(_QWORD))*((_QWORD *)ptr + 3);
if ( v5 )
v5(v4[2]);
free(v4);
--v3;
}
while ( v3 != -1 );
}
free(ptr);
}

original_code:
void listRelease(list *list)
{
    unsigned int len;
    listNode *current, *next;
    current = list->head;
    len = list->len;
    while(len--) {
        next = current->next;
        if (list->free) list->free(current->value);
        free(current);
        current = next;
    }
    free(list);
}

canonical_original_code:
void listRelease(struct list *list)
{
    unsigned int len;
    struct listNode *current; struct listNode *next;
    current = list->head;
    len = list->len;
    while(len--) {
        next = current->next;
        if (list->free) list->free(current->value);
        free(current);
        current = next;
    }
    free(list);
}

variable_types:
len
unsigned int

variable_types: struct listNode { struct listNode *prev; struct listNode *next; void *value; } *


listDelNode

original_code:
void listDelNode(list *list, listNode *node)
{
    if (node->prev)
        node->prev->next = node->next;
    else
        list->head = node->next;
    if (node->next)
        node->next->prev = node->prev;
    else
        list->tail = node->prev;
    if (list->free) list->free(node->value);
    free(node);
    list->len--;
}

canonical_original_code:
void listDelNode(struct list *list, struct listNode *node)
{
    if (node->prev)
        node->prev->next = node->next;
    else
        list->head = node->next;
    if (node->next)
        node->next->prev = node->prev;
    else
        list->tail = node->prev;
    if (list->free) list->free(node->value);
    free(node);
    list->len--;
}

variable_types:
node
struct listNode *

user_defined_types: struct listNode { struct listNode *prev; struct listNode *next; void *value; }


listReleaseIterator

decompiled_code:
void __fastcall listReleaseIterator(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
free(a1);
}

original_code:
void listReleaseIterator(listIter *iter) {
    free(iter);
}

canonical_original_code:
void listReleaseIterator(struct listIter *iter) {
    free(iter);
}

variable_types: struct listIter { struct listNode { struct listNode *prev; struct listNode *next; void *value; } *next; int direction; } *

user_defined_types: struct listIter { struct listNode *next; int direction; }


listRewind

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

result = *a1;
*(_QWORD *)a2 = *a1;
*(_DWORD *)(a2 + 8) = 0;
return result;
}

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

result = *a1;
*(_QWORD *)a2 = *a1;
*(_DWORD *)(a2 + 8) = 0;
return result;
}

original_code:
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = 0;
}

canonical_original_code:
void listRewind(struct list *list, struct listIter *li) {
    li->next = list->head;
    li->direction = 0;
}

variable_types: struct listIter { struct listNode *next; int direction; } *


listRewindTail

decompiled_code:
__int64 __fastcall listRewindTail(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*(_QWORD *)a2 = result;
*(_DWORD *)(a2 + 8) = 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a1 + 8);
*(_QWORD *)a2 = result;
*(_DWORD *)(a2 + 8) = 1;
return result;
}

original_code:
void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = 1;
}

canonical_original_code:
void listRewindTail(struct list *list, struct listIter *li) {
    li->next = list->tail;
    li->direction = 1;
}


validSingleLineResponseBuffer

decompiled_code:
__int64 __fastcall validSingleLineResponseBuffer(const char *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *a1 == 43 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *a1 == 43 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

original_code:
static inline int validSingleLineResponseBuffer(char *rbuf,int rlen) {
    if( rbuf[0] != '+' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p=strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}

canonical_original_code:
static inline int validSingleLineResponseBuffer(char *rbuf, int rlen) {
    if( rbuf[0] != '+' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p = strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}

variable_types:
rbuf
char *

variable_types: rlen int

variable_types:
p
char *


validErrorMsgResponseBuffer

decompiled_code:
__int64 __fastcall validErrorMsgResponseBuffer(const char *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *a1 == 45 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *a1 == 45 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

original_code:
static inline int validErrorMsgResponseBuffer(char *rbuf,int rlen) {
    if( rbuf[0] != '-' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p=strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}

canonical_original_code:
static inline int validErrorMsgResponseBuffer(char *rbuf, int rlen) {
    if( rbuf[0] != '-' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p = strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}


validIntegerReplyResponseBuffer

decompiled_code:
__int64 __fastcall validIntegerReplyResponseBuffer(const char *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( *a1 == 58 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *a1 == 58 )
{
result = 0LL;
if ( a2 > 2 && a1[a2 - 2] == 13 && a1[a2 - 1] == 10 )
result = 2 * (unsigned int)(strstr(a1, "\r\n") - a1 + 2 == a2) - 1;
}
return result;
}

original_code:
static inline int validIntegerReplyResponseBuffer(char *rbuf,int rlen) {
    if( rbuf[0] != ':' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p=strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}

canonical_original_code:
static inline int validIntegerReplyResponseBuffer(char *rbuf, int rlen) {
    if( rbuf[0] != ':' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    char *p = strstr(rbuf,"\r\n");
    if(p - rbuf + 2 != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}


validBulkReplyResponseBuffer

decompiled_code:
__int64 __fastcall validBulkReplyResponseBuffer(__int64 a1, int a2)
{
__int64 result; // rax
char *v3; // r12
char *v4; // r12
int v5; // eax
char *v6; // r12
char dest[8]; // [rsp+1h] [rbp-33h] BYREF
__int16 v8; // [rsp+9h] [rbp-2Bh]
char v9; // [rsp+Bh] [rbp-29h]

result = 0xFFFFFFFFLL;
if ( *(_BYTE *)a1 == 36 )
{
result = 0LL;
if ( a2 > 2 && *(_BYTE *)(a1 + a2 - 2) == 13 && *(_BYTE *)(a1 + a2 - 1) == 10 )
{
*(_QWORD *)dest = 0LL;
v8 = 0;
v9 = 0;
v3 = strstr((const char *)(a1 + 1), "\r\n");
strncpy(dest, (const char *)(a1 + 1), (size_t)&v3[-a1 - 1]);
v4 = v3 + 2;
v5 = atoi(dest);
if ( v5 != -1 )
v4 += v5 + 2;
v6 = &v4[-a1];
result = 0LL;
if ( (__int64)v6 <= a2 )
{
result = 1LL;
if ( v6 != (char *)a2 )
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
__int64 result; // rax
char *v3; // r12
char *v4; // r12
int v5; // eax
char *v6; // r12
char dest[8]; // [rsp+1h] [rbp-33h] BYREF
__int16 v8; // [rsp+9h] [rbp-2Bh]
char v9; // [rsp+Bh] [rbp-29h]

result = 0xFFFFFFFFLL;
if ( *(_BYTE *)a1 == 36 )
{
result = 0LL;
if ( a2 > 2 && *(_BYTE *)(a1 + a2 - 2) == 13 && *(_BYTE *)(a1 + a2 - 1) == 10 )
{
*(_QWORD *)dest = 0LL;
v8 = 0;
v9 = 0;
v3 = strstr((const char *)(a1 + 1), "\r\n");
strncpy(dest, (const char *)(a1 + 1), (size_t)&v3[-a1 - 1]);
v4 = v3 + 2;
v5 = atoi(dest);
if ( v5 != -1 )
v4 += v5 + 2;
v6 = &v4[-a1];
result = 0LL;
if ( (__int64)v6 <= a2 )
{
result = 1LL;
if ( v6 != (char *)a2 )
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
static inline int validBulkReplyResponseBuffer(char *rbuf,int rlen) {
    char *op=rbuf;
    if(rbuf[0] != '$') {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    rbuf++;
    char slen[11];
    int ilen;
    memset(slen,0,11);
    char *p=strstr(rbuf,"\r\n");
    strncpy(slen,rbuf,p-rbuf);
    rbuf=p;
    rbuf+=2;
    ilen=atoi(slen);
    if(ilen == -1) {
        rbuf+=0;
    } else {
        rbuf+=ilen+2;
    }
    if(rbuf - op > rlen) {
        return 0;
    }
    if(rbuf - op == rlen) {
        return 1;
    }
    if(rbuf - op < rlen) {
        return -1;
    }
}

canonical_original_code:
static inline int validBulkReplyResponseBuffer(char *rbuf, int rlen) {
    char *op = rbuf;
    if(rbuf[0] != '$') {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    rbuf++;
    char slen[11];
    int ilen;
    memset(slen,0,11);
    char *p = strstr(rbuf,"\r\n");
    strncpy(slen,rbuf,p-rbuf);
    rbuf=p;
    rbuf+=2;
    ilen=atoi(slen);
    if(ilen == -1) {
        rbuf+=0;
    } else {
        rbuf+=ilen+2;
    }
    if(rbuf - op > rlen) {
        return 0;
    }
    if(rbuf - op == rlen) {
        return 1;
    }
    if(rbuf - op < rlen) {
        return -1;
    }
}

variable_types:
op
char *

variable_types:
slen
char [11]

variable_types: ilen int


validMultiBuckResponseBuffer

decompiled_code:
__int64 __fastcall validMultiBuckResponseBuffer(__int64 a1, int a2)
{
__int64 result; // rax
char *v3; // r12
unsigned int v4; // er14
char *v5; // r12
const char *v6; // r15
char *v7; // r12
int v8; // eax
char dest[8]; // [rsp+Dh] [rbp-43h] BYREF
__int16 v10; // [rsp+15h] [rbp-3Bh]
char v11; // [rsp+17h] [rbp-39h]

result = 0xFFFFFFFFLL;
if ( *(_BYTE *)a1 == 42 )
{
result = 0LL;
if ( a2 > 2 && *(_BYTE *)(a1 + a2 - 2) == 13 && *(_BYTE *)(a1 + a2 - 1) == 10 )
{
*(_QWORD *)dest = 0LL;
v10 = 0;
v11 = 0;
v3 = strstr((const char *)(a1 + 1), "\r\n");
strncpy(dest, (const char *)(a1 + 1), (size_t)&v3[-a1 - 1]);
v4 = atoi(dest);
result = 0xFFFFFFFFLL;
if ( v4 <= 0x400 )
{
v5 = v3 + 2;
if ( v4 )
{
result = 0LL;
if ( &v5[-a1] != (char *)a2 )
{
while ( 1 )
{
if ( *v5 != 36 )
return 0xFFFFFFFFLL;
v6 = v5 + 1;
*(_QWORD *)dest = 0LL;
v10 = 0;
v11 = 0;
v7 = strstr(v5 + 1, "\r\n");
strncpy(dest, v6, v7 - v6);
v5 = v7 + 2;
v8 = atoi(dest);
if ( v8 != -1 )
v5 += v8 + 2;
if ( a2 < (__int64)&v5[-a1] )
break;
if ( v4 != 1 && (char *)a2 == &v5[-a1] )
return 0LL;
if ( (int)--v4 <= 0 )
return 2 * (unsigned int)(&v5[-a1] == (char *)a2) - 1;
}
result = 0LL;
}
}
else
{
result = 2 * (unsigned int)(&v5[-a1] == (char *)a2) - 1;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2)
{
__int64 result; // rax
char *v3; // r12
unsigned int v4; // er14
char *v5; // r12
const char *v6; // r15
char *v7; // r12
int v8; // eax
char dest[8]; // [rsp+Dh] [rbp-43h] BYREF
__int16 v10; // [rsp+15h] [rbp-3Bh]
char v11; // [rsp+17h] [rbp-39h]

result = 0xFFFFFFFFLL;
if ( *(_BYTE *)a1 == 42 )
{
result = 0LL;
if ( a2 > 2 && *(_BYTE *)(a1 + a2 - 2) == 13 && *(_BYTE *)(a1 + a2 - 1) == 10 )
{
*(_QWORD *)dest = 0LL;
v10 = 0;
v11 = 0;
v3 = strstr((const char *)(a1 + 1), "\r\n");
strncpy(dest, (const char *)(a1 + 1), (size_t)&v3[-a1 - 1]);
v4 = atoi(dest);
result = 0xFFFFFFFFLL;
if ( v4 <= 0x400 )
{
v5 = v3 + 2;
if ( v4 )
{
result = 0LL;
if ( &v5[-a1] != (char *)a2 )
{
while ( 1 )
{
if ( *v5 != 36 )
return 0xFFFFFFFFLL;
v6 = v5 + 1;
*(_QWORD *)dest = 0LL;
v10 = 0;
v11 = 0;
v7 = strstr(v5 + 1, "\r\n");
strncpy(dest, v6, v7 - v6);
v5 = v7 + 2;
v8 = atoi(dest);
if ( v8 != -1 )
v5 += v8 + 2;
if ( a2 < (__int64)&v5[-a1] )
break;
if ( v4 != 1 && (char *)a2 == &v5[-a1] )
return 0LL;
if ( (int)--v4 <= 0 )
return 2 * (unsigned int)(&v5[-a1] == (char *)a2) - 1;
}
result = 0LL;
}
}
else
{
result = 2 * (unsigned int)(&v5[-a1] == (char *)a2) - 1;
}
}
}
}
return result;
}

original_code:
static inline int validMultiBuckResponseBuffer(char *rbuf,int rlen) {
    char *op=rbuf;
    if( rbuf[0] != '*' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    rbuf++;
    char slen[11];
    int argc,ilen;
    memset(slen,0,11);
    char *p=strstr(rbuf,"\r\n");
    strncpy(slen,rbuf,p-rbuf);
    rbuf=p;
    rbuf+=2;
    argc=atoi(slen);
    if(argc < 0 || argc > 1024) {
        return -1;
    }
    if( argc == 0 && rbuf - op == rlen) {
        return 1;
    }
    if( argc != 0 && rbuf - op == rlen ) {
        return 0;
    }
    for(; argc > 0; argc--) {
        if(rbuf[0] != '$') {
            return -1;
        }
        rbuf++;
        memset(slen,0,11);
        p=strstr(rbuf,"\r\n");
        strncpy(slen,rbuf,p-rbuf);
        rbuf=p;
        rbuf+=2;
        ilen=atoi(slen);
        if(ilen == -1) {
            rbuf+=0;
        } else {
            rbuf+=ilen+2;
        }
        if(rbuf - op > rlen) {
            return 0;
        }
        if(rbuf - op == rlen && argc != 1) {
            return 0;
        }
    }
    if(rbuf - op != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}

canonical_original_code:
static inline int validMultiBuckResponseBuffer(char *rbuf, int rlen) {
    char *op = rbuf;
    if( rbuf[0] != '*' ) {
        return -1;
    }
    if( rlen < 3 || rbuf[rlen-2] != '\r'|| rbuf[rlen-1] != '\n' ) {
        return 0;
    }
    rbuf++;
    char slen[11];
    int argc; int ilen;
    memset(slen,0,11);
    char *p = strstr(rbuf,"\r\n");
    strncpy(slen,rbuf,p-rbuf);
    rbuf=p;
    rbuf+=2;
    argc=atoi(slen);
    if(argc < 0 || argc > 1024) {
        return -1;
    }
    if( argc == 0 && rbuf - op == rlen) {
        return 1;
    }
    if( argc != 0 && rbuf - op == rlen ) {
        return 0;
    }
    for(; argc > 0; argc--) {
        if(rbuf[0] != '$') {
            return -1;
        }
        rbuf++;
        memset(slen,0,11);
        p=strstr(rbuf,"\r\n");
        strncpy(slen,rbuf,p-rbuf);
        rbuf=p;
        rbuf+=2;
        ilen=atoi(slen);
        if(ilen == -1) {
            rbuf+=0;
        } else {
            rbuf+=ilen+2;
        }
        if(rbuf - op > rlen) {
            return 0;
        }
        if(rbuf - op == rlen && argc != 1) {
            return 0;
        }
    }
    if(rbuf - op != rlen) {
        return -1;
    } else {
        return 1;
    }
    return -1;
}


connectRedis

decompiled_code:
__int64 connectRedis()
{
return anetTcpConnect();
}

canonical_decompiled_code:
__int64 func11()
{
return anetTcpConnect();
}

original_code:
static inline int connectRedis() {
    int fd=anetTcpConnect(((void *)0), "10.210.74.152", 6379);
    return fd;
}

canonical_original_code:
static inline int connectRedis() {
    int fd = anetTcpConnect(((void *)0), "10.210.74.152", 6379);
    return fd;
}

variable_types: fd int


timeout

decompiled_code:
__int64 __fastcall timeout(int a1, __int64 a2)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
return 1000 * (LODWORD(v3.tv_sec) - a1) + (unsigned int)((v3.tv_usec - a2) / 1000);
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, __int64 a2)
{
struct timeval v3; // [rsp+0h] [rbp-28h] BYREF

gettimeofday(&v3, 0LL);
return 1000 * (LODWORD(v3.tv_sec) - a1) + (unsigned int)((v3.tv_usec - a2) / 1000);
}

original_code:
static inline int timeout(struct timeval begin) {
    struct timeval now;
    gettimeofday(&now,((void *)0));
    int passed;
    passed=(now.tv_sec - begin.tv_sec) * 1000;
    passed+=(now.tv_usec - begin.tv_usec) / 1000;
    return passed;
}

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

variable_types: passed int


decompiled_code:
__int64 __fastcall object_cmp(const void *a1, const void *a2)
{
return (unsigned int)(*((_DWORD *)a1 + 1) - *((_DWORD *)a2 + 1));
}

canonical_decompiled_code:
__int64 __fastcall func6(const void *a1, const void *a2)
{
return (unsigned int)(*((_DWORD *)a1 + 1) - *((_DWORD *)a2 + 1));
}

original_code:
static int object_cmp(const void *p1, const void *p2)
{
    return ((object *)p1)->score - ((object *)p2)->score;
}

canonical_original_code:
static int object_cmp(void *p1, void *p2)
{
    return ((struct object *)p1)->score - ((struct object *)p2)->score;
}

user_defined_types: struct object { unsigned int member; unsigned int score; }


decompiled_code:
__int64 __fastcall zadd_init(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
__int64 v6; // rax
void *v7; // rax

if ( *(_DWORD *)a2 == 3 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 && v4[2] == 2 )
{
v6 = *(_QWORD *)(a2 + 16);
if ( **(_QWORD **)(v6 + 8) <= 0xFFFFFFFFuLL && **(_QWORD **)(v6 + 16) <= 0xFFFFFFFFuLL )
{
v7 = malloc(0x4000uLL);
if ( v7 )
{
*(_DWORD *)(a1 + 8) = 0x4000;
*(_QWORD *)(a1 + 16) = v7;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: member and score must be a number bewteen 0 and 4294967295.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zadd(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zadd(old_data,new_member,new_score).");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
__int64 v6; // rax
void *v7; // rax

if ( *(_DWORD *)a2 == 3 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 && v4[2] == 2 )
{
v6 = *(_QWORD *)(a2 + 16);
if ( **(_QWORD **)(v6 + 8) <= 0xFFFFFFFFuLL && **(_QWORD **)(v6 + 16) <= 0xFFFFFFFFuLL )
{
v7 = malloc(0x4000uLL);
if ( v7 )
{
*(_DWORD *)(a1 + 8) = 0x4000;
*(_QWORD *)(a1 + 16) = v7;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: member and score must be a number bewteen 0 and 4294967295.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zadd(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zadd(old_data,new_member,new_score).");
result = 1LL;
}
return result;
}

original_code:
my_bool zadd_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if(args->arg_count != 3) {
        strcpy(message,"Usage: select zadd(old_data,new_member,new_score).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT || args->arg_type[2] != INT_RESULT) {
        strcpy(message,"Usage: select zadd(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 4294967295
        || *(long long *)args->args[2] < 0 || *(long long *)args->args[2] > 4294967295) {
            strcpy(message,"Usage: member and score must be a number bewteen 0 and 4294967295.");
            return 1;
    }
    void *mptr=malloc(16384);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}

canonical_original_code:
char zadd_init(struct st_udf_init *initid, struct st_udf_args *args, char *message)
{
    if(args->arg_count != 3) {
        strcpy(message,"Usage: select zadd(old_data,new_member,new_score).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT || args->arg_type[2] != INT_RESULT) {
        strcpy(message,"Usage: select zadd(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 4294967295
        || *(long long *)args->args[2] < 0 || *(long long *)args->args[2] > 4294967295) {
            strcpy(message,"Usage: member and score must be a number bewteen 0 and 4294967295.");
            return 1;
    }
    void *mptr = malloc(16384);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}

variable_types: struct st_udf_init { char maybe_null; unsigned int decimals; unsigned int max_length; char *ptr; char const_item; } *

variable_types: struct st_udf_args { unsigned int arg_count; enum Item_result { STRING_RESULT=0, REAL_RESULT=1, INT_RESULT=2, ROW_RESULT=3, DECIMAL_RESULT=4 } *arg_type; char **args; unsigned long *lengths; char *maybe_null; } *

variable_types:
message
char *

variable_types:
mptr
void *

return_type: char

user_defined_types: struct st_udf_args { unsigned int arg_count; enum Item_result *arg_type; char **args; unsigned long *lengths; char *maybe_null; }


zadd

original_code:
char *zadd(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned *res_length, char *null_value, char *error)
{
    object *old_data=(object *)args->args[0];
    int len=args->lengths[0];
    memset(initid->ptr,0,initid->max_length);
    if(len > initid->max_length) {
        memcpy(initid->ptr,old_data,initid->max_length);
        *res_length=initid->max_length;
        return initid->ptr;
    }
    if( len % sizeof(object) != 0 ) {
        memcpy(initid->ptr,old_data,len);
        *res_length=len;
        return initid->ptr;
    }
    object obj;
    obj.member=*(long long *)args->args[1];
    obj.score=*(long long *)args->args[2];
    int found=0;
    memcpy(initid->ptr,old_data,len);
    object *new_data=(object *)initid->ptr;
    for(int i=0;i<len/sizeof(object);i++){
        if( new_data[i].member == obj.member ) {
            new_data[i].score = obj.score;
            found = 1;
        }
    }
    if(found == 0 && len + sizeof(object) <= initid->max_length) {
        memcpy(initid->ptr+len,&obj,sizeof(object));
        *res_length=len+sizeof(object);
    }
    else {
        *res_length=len;
    }
    qsort(initid->ptr,*res_length / sizeof(object),sizeof(object),object_cmp);
    return initid->ptr;
}

canonical_original_code:
char *zadd(struct st_udf_init *initid, struct st_udf_args *args, char *result, unsigned int *res_length, char *null_value, char *error)
{
    struct object *old_data = (object *)args->args[0];
    int len = args->lengths[0];
    memset(initid->ptr,0,initid->max_length);
    if(len > initid->max_length) {
        memcpy(initid->ptr,old_data,initid->max_length);
        *res_length=initid->max_length;
        return initid->ptr;
    }
    if( len % sizeof(object) != 0 ) {
        memcpy(initid->ptr,old_data,len);
        *res_length=len;
        return initid->ptr;
    }
    struct object obj;
    obj.member=*(long long *)args->args[1];
    obj.score=*(long long *)args->args[2];
    int found = 0;
    memcpy(initid->ptr,old_data,len);
    struct object *new_data = (object *)initid->ptr;
    for(int i = 0;i<len/sizeof(object);i++){
        if( new_data[i].member == obj.member ) {
            new_data[i].score = obj.score;
            found = 1;
        }
    }
    if(found == 0 && len + sizeof(object) <= initid->max_length) {
        memcpy(initid->ptr+len,&obj,sizeof(object));
        *res_length=len+sizeof(object);
    }
    else {
        *res_length=len;
    }
    qsort(initid->ptr,*res_length / sizeof(object),sizeof(object),object_cmp);
    return initid->ptr;
}

variable_types:
result
char *

variable_types:
error
char *

variable_types: struct object { unsigned int member; unsigned int score; } *

variable_types: len int

variable_types: found int

variable_types: i int

return_type: char *

user_defined_types: struct st_udf_init { char maybe_null; unsigned int decimals; unsigned int max_length; char *ptr; char const_item; }

user_defined_types: enum Item_result { STRING_RESULT=0, REAL_RESULT=1, INT_RESULT=2, ROW_RESULT=3, DECIMAL_RESULT=4 }


decompiled_code:
__int64 __fastcall zrange_init(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
__int64 v6; // rax
void *v7; // rax

if ( *(_DWORD *)a2 == 3 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 && v4[2] == 2 )
{
v6 = *(_QWORD *)(a2 + 16);
if ( **(_QWORD **)(v6 + 8) <= 0x7D0uLL && **(_QWORD **)(v6 + 16) <= 0x7D0uLL )
{
v7 = malloc(0xC000uLL);
if ( v7 )
{
*(_DWORD *)(a1 + 8) = 49152;
*(_QWORD *)(a1 + 16) = v7;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: member and score must a number between 0 and 2000");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrange(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrange(data,start,stop).");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
__int64 v6; // rax
void *v7; // rax

if ( *(_DWORD *)a2 == 3 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 && v4[2] == 2 )
{
v6 = *(_QWORD *)(a2 + 16);
if ( **(_QWORD **)(v6 + 8) <= 0x7D0uLL && **(_QWORD **)(v6 + 16) <= 0x7D0uLL )
{
v7 = malloc(0xC000uLL);
if ( v7 )
{
*(_DWORD *)(a1 + 8) = 49152;
*(_QWORD *)(a1 + 16) = v7;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: member and score must a number between 0 and 2000");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrange(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrange(data,start,stop).");
result = 1LL;
}
return result;
}

original_code:
my_bool zrange_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if(args->arg_count != 3) {
        strcpy(message,"Usage: select zrange(data,start,stop).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT || args->arg_type[2] != INT_RESULT) {
        strcpy(message,"Usage: select zrange(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 2000
        || *(long long *)args->args[2] < 0 || *(long long *)args->args[2] > 2000) {
            strcpy(message,"Usage: member and score must a number between 0 and 2000");
            return 1;
    }
    void *mptr=malloc(16384*3);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384*3;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}

canonical_original_code:
char zrange_init(struct st_udf_init *initid, struct st_udf_args *args, char *message)
{
    if(args->arg_count != 3) {
        strcpy(message,"Usage: select zrange(data,start,stop).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT || args->arg_type[2] != INT_RESULT) {
        strcpy(message,"Usage: select zrange(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 2000
        || *(long long *)args->args[2] < 0 || *(long long *)args->args[2] > 2000) {
            strcpy(message,"Usage: member and score must a number between 0 and 2000");
            return 1;
    }
    void *mptr = malloc(16384*3);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384*3;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}


decompiled_code:
void __fastcall zrange_deinit(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

canonical_decompiled_code:
void __fastcall func11(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

original_code:
void zrange_deinit(UDF_INIT *initid)
{
    if(initid->ptr != ((void *)0)) {
        free(initid->ptr);
    }
    return;
}

canonical_original_code:
void zrange_deinit(struct st_udf_init *initid)
{
    if(initid->ptr != ((void *)0)) {
        free(initid->ptr);
    }
    return;
}


zrange

original_code:
char *zrange(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned *res_length, char *null_value, char *error)
{
    object *objs=(object *)args->args[0];
    int len=args->lengths[0];
    char *pos=initid->ptr;
    if(len > 16384 ) {
        *null_value=1;
        return ((void *)0);
    }
    memset(initid->ptr,0,initid->max_length);
    if( len % sizeof(object) != 0) {
        *null_value=1;
        return ((void *)0);
    }
    unsigned int nobj=len / sizeof(object);
    unsigned int start=*(long long *)args->args[1];
    unsigned int stop=*(long long *)args->args[2];
    if(stop > nobj) {
        stop=nobj;
    }
    *pos='[';
    ++pos;
    for(int i=start;i < stop ;i++) {
        object obj=objs[i];
        pos+=sprintf(pos,"[%u,%u]",obj.member,obj.score);
        if(stop - i != 1) {
            *pos=',';
            ++pos;
        }
    }
    *pos=']';
    ++pos;
    *res_length=pos - (char *)initid->ptr;
    return initid->ptr;
}

canonical_original_code:
char *zrange(struct st_udf_init *initid, struct st_udf_args *args, char *result, unsigned int *res_length, char *null_value, char *error)
{
    struct object *objs = (object *)args->args[0];
    int len = args->lengths[0];
    char *pos = initid->ptr;
    if(len > 16384 ) {
        *null_value=1;
        return ((void *)0);
    }
    memset(initid->ptr,0,initid->max_length);
    if( len % sizeof(object) != 0) {
        *null_value=1;
        return ((void *)0);
    }
    unsigned int nobj = len / sizeof(object);
    unsigned int start = *(long long *)args->args[1];
    unsigned int stop = *(long long *)args->args[2];
    if(stop > nobj) {
        stop=nobj;
    }
    *pos='[';
    ++pos;
    for(int i = start;i < stop ;i++) {
        struct object obj = objs[i];
        pos+=sprintf(pos,"[%u,%u]",obj.member,obj.score);
        if(stop - i != 1) {
            *pos=',';
            ++pos;
        }
    }
    *pos=']';
    ++pos;
    *res_length=pos - (char *)initid->ptr;
    return initid->ptr;
}

variable_types:
pos
char *

variable_types:
nobj
unsigned int

variable_types:
start
unsigned int

variable_types:
stop
unsigned int

variable_types: struct object { unsigned int member; unsigned int score; }


decompiled_code:
__int64 __fastcall zcard_init(__int64 a1, __int64 a2, char *a3)
{
__int64 result; // rax

if ( *(_DWORD *)a2 == 1 )
{
result = 0LL;
if ( **(_DWORD **)(a2 + 8) )
{
strcpy(a3, "Usage: select zcard(string).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zcard(data).");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 a2, char *a3)
{
__int64 result; // rax

if ( *(_DWORD *)a2 == 1 )
{
result = 0LL;
if ( **(_DWORD **)(a2 + 8) )
{
strcpy(a3, "Usage: select zcard(string).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zcard(data).");
result = 1LL;
}
return result;
}

original_code:
my_bool zcard_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if(args->arg_count != 1) {
        strcpy(message,"Usage: select zcard(data).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT ) {
        strcpy(message,"Usage: select zcard(string).");
        return 1;
    }
    return 0;
}

canonical_original_code:
char zcard_init(struct st_udf_init *initid, struct st_udf_args *args, char *message)
{
    if(args->arg_count != 1) {
        strcpy(message,"Usage: select zcard(data).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT ) {
        strcpy(message,"Usage: select zcard(string).");
        return 1;
    }
    return 0;
}


decompiled_code:
void zcard_deinit()
{
;
}

canonical_decompiled_code:
void func14()
{
;
}

original_code:
void zcard_deinit(UDF_INIT *initid)
{
    return;
}

canonical_original_code:
void zcard_deinit(struct st_udf_init *initid)
{
    return;
}


zcard

decompiled_code:
unsigned __int64 __fastcall zcard(__int64 a1, __int64 a2)
{
__int64 v2; // rax

v2 = **(_QWORD **)(a2 + 24);
if ( (int)v2 > 0x4000 )
return -1LL;
if ( (v2 & 7) != 0 )
return -1LL;
return (unsigned __int64)(int)v2 >> 3;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func15(__int64 a1, __int64 a2)
{
__int64 v2; // rax

v2 = **(_QWORD **)(a2 + 24);
if ( (int)v2 > 0x4000 )
return -1LL;
if ( (v2 & 7) != 0 )
return -1LL;
return (unsigned __int64)(int)v2 >> 3;
}

original_code:
long long zcard(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
    int len=args->lengths[0];
    if(len > 16384 ) {
        return -1;
    }
    if( len % sizeof(object) != 0) {
        return -1;
    }
    long long nobj=len / sizeof(object);
    return nobj;
}

canonical_original_code:
long long zcard(struct st_udf_init *initid, struct st_udf_args *args, char *is_null, char *error)
{
    int len = args->lengths[0];
    if(len > 16384 ) {
        return -1;
    }
    if( len % sizeof(object) != 0) {
        return -1;
    }
    long long nobj = len / sizeof(object);
    return nobj;
}

variable_types:
nobj
long long

return_type: long long


decompiled_code:
__int64 __fastcall zrem_init(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
void *v6; // rax

if ( *(_DWORD *)a2 == 2 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 )
{
if ( **(_QWORD **)(*(_QWORD *)(a2 + 16) + 8LL) > 0xFFFFFFFFuLL )
{
strcpy(a3, "Usage: member must be a number between 0 and 4294967295.");
result = 1LL;
}
else
{
v6 = malloc(0x4000uLL);
if ( v6 )
{
*(_DWORD *)(a1 + 8) = 0x4000;
*(_QWORD *)(a1 + 16) = v6;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
}
else
{
strcpy(a3, "Usage: select zadd(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrem(data,member).");
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2, char *a3)
{
_DWORD *v4; // rax
__int64 result; // rax
void *v6; // rax

if ( *(_DWORD *)a2 == 2 )
{
v4 = *(_DWORD **)(a2 + 8);
if ( !*v4 && v4[1] == 2 )
{
if ( **(_QWORD **)(*(_QWORD *)(a2 + 16) + 8LL) > 0xFFFFFFFFuLL )
{
strcpy(a3, "Usage: member must be a number between 0 and 4294967295.");
result = 1LL;
}
else
{
v6 = malloc(0x4000uLL);
if ( v6 )
{
*(_DWORD *)(a1 + 8) = 0x4000;
*(_QWORD *)(a1 + 16) = v6;
*(_BYTE *)(a1 + 24) = 0;
result = 0LL;
}
else
{
strcpy(a3, "Memory allocation failed.");
result = 1LL;
}
}
}
else
{
strcpy(a3, "Usage: select zadd(string,int,int).");
result = 1LL;
}
}
else
{
strcpy(a3, "Usage: select zrem(data,member).");
result = 1LL;
}
return result;
}

original_code:
my_bool zrem_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if(args->arg_count != 2) {
        strcpy(message,"Usage: select zrem(data,member).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT ) {
        strcpy(message,"Usage: select zadd(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 4294967295) {
            strcpy(message,"Usage: member must be a number between 0 and 4294967295." );
            return 1;
    }
    void *mptr=malloc(16384);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}

canonical_original_code:
char zrem_init(struct st_udf_init *initid, struct st_udf_args *args, char *message)
{
    if(args->arg_count != 2) {
        strcpy(message,"Usage: select zrem(data,member).");
        return 1;
    }
    if(args->arg_type[0] != STRING_RESULT || args->arg_type[1] != INT_RESULT ) {
        strcpy(message,"Usage: select zadd(string,int,int).");
        return 1;
    }
    if( *(long long *)args->args[1] < 0 || *(long long *)args->args[1] > 4294967295) {
            strcpy(message,"Usage: member must be a number between 0 and 4294967295." );
            return 1;
    }
    void *mptr = malloc(16384);
    if(!mptr) {
        strcpy(message,"Memory allocation failed.");
        return 1;
    }
    initid->max_length=16384;
    initid->ptr=mptr;
    initid->const_item=0;
    return 0;
}


decompiled_code:
void __fastcall zrem_deinit(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

canonical_decompiled_code:
void __fastcall func17(__int64 a1)
{
void *v1; // rdi

v1 = *(void **)(a1 + 16);
if ( v1 )
free(v1);
}

original_code:
void zrem_deinit(UDF_INIT *initid)
{
    if(initid->ptr != ((void *)0)) {
        free(initid->ptr);
    }
    return;
}

canonical_original_code:
void zrem_deinit(struct st_udf_init *initid)
{
    if(initid->ptr != ((void *)0)) {
        free(initid->ptr);
    }
    return;
}


zrem

original_code:
char *zrem(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned *res_length, char *null_value, char *error)
{
    memset(initid->ptr,0,initid->max_length);
    object *old_data=(object *)args->args[0];
    int len=args->lengths[0];
    if(len + sizeof(object) > initid->max_length || len % sizeof(object) != 0 ) {
        memcpy(initid->ptr,old_data,len > initid->max_length ? initid->max_length : len);
        *res_length=len > initid->max_length ? initid->max_length : len;
        return initid->ptr;
    }
    unsigned int member=*(long long *)args->args[1];
    int k=0;
    for(int i=0;i<len/sizeof(object);i++) {
        if( old_data[i].member != member ) {
            memcpy(initid->ptr+k*sizeof(object),&old_data[i],sizeof(object));
            k++;
        }
    }
    *res_length=k*sizeof(object);
    return initid->ptr;
}

canonical_original_code:
char *zrem(struct st_udf_init *initid, struct st_udf_args *args, char *result, unsigned int *res_length, char *null_value, char *error)
{
    memset(initid->ptr,0,initid->max_length);
    struct object *old_data = (object *)args->args[0];
    int len = args->lengths[0];
    if(len + sizeof(object) > initid->max_length || len % sizeof(object) != 0 ) {
        memcpy(initid->ptr,old_data,len > initid->max_length ? initid->max_length : len);
        *res_length=len > initid->max_length ? initid->max_length : len;
        return initid->ptr;
    }
    unsigned int member = *(long long *)args->args[1];
    int k = 0;
    for(int i = 0;i<len/sizeof(object);i++) {
        if( old_data[i].member != member ) {
            memcpy(initid->ptr+k*sizeof(object),&old_data[i],sizeof(object));
            k++;
        }
    }
    *res_length=k*sizeof(object);
    return initid->ptr;
}

variable_types:
member
unsigned int

variable_types: k int


anetSetError

decompiled_code:
int anetSetError(char *a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(arg, a2);
result = vsnprintf(a1, 0x100uLL, a2, arg);
}
return result;
}

canonical_decompiled_code:
int func0(char *a1, const char *a2, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

if ( a1 )
{
va_start(arg, a2);
result = vsnprintf(a1, 0x100uLL, a2, arg);
}
return result;
}

original_code:
static void anetSetError(char *err, const char *fmt, ...)
{
    va_list ap;
    if (!err) return;
    __builtin_va_start(ap,fmt);
    vsnprintf(err, 256, fmt, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
static void anetSetError(char *err, char *fmt, ...)
{
    __builtin_va_list ap;
    if (!err) return;
    __builtin_va_start(ap,fmt);
    vsnprintf(err, 256, fmt, ap);
    __builtin_va_end(ap);
}

variable_types:
err
char *

variable_types:
fmt
char *


anetNonBlock

decompiled_code:
__int64 __fastcall anetNonBlock(char *a1, int a2)
{
unsigned int v2; // eax
unsigned int v3; // ebx
__int64 v4; // rdx
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax

v2 = fcntl(a2, 3);
v3 = v2;
if ( v2 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
anetSetError(a1, "fcntl(F_GETFL): %s\n", v7);
}
else
{
v4 = v2;
BYTE1(v4) = BYTE1(v2) | 8;
v3 = fcntl(a2, 4, v4);
if ( v3 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
anetSetError(a1, "fcntl(F_SETFL,O_NONBLOCK): %s\n", v9);
}
else
{
v3 = 0;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, int a2)
{
unsigned int v2; // eax
unsigned int v3; // ebx
__int64 v4; // rdx
int *v6; // rax
char *v7; // rax
int *v8; // rax
char *v9; // rax

v2 = fcntl(a2, 3);
v3 = v2;
if ( v2 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
func0(a1, "fcntl(F_GETFL): %s\n", v7);
}
else
{
v4 = v2;
BYTE1(v4) = BYTE1(v2) | 8;
v3 = fcntl(a2, 4, v4);
if ( v3 == -1 )
{
v8 = _errno_location();
v9 = strerror(*v8);
func0(a1, "fcntl(F_SETFL,O_NONBLOCK): %s\n", v9);
}
else
{
v3 = 0;
}
}
return v3;
}

original_code:
int anetNonBlock(char *err, int fd)
{
    int flags;
    if ((flags = fcntl(fd, 3)) == -1) {
        anetSetError(err, "fcntl(F_GETFL): %s\n", strerror((*__errno_location ())));
        return -1;
    }
    if (fcntl(fd, 4, flags | 04000) == -1) {
        anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s\n", strerror((*__errno_location ())));
        return -1;
    }
    return 0;
}

variable_types: fd int

variable_types: flags int