repo: beckysag/archiver

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // bp
char v4; // al
size_t v5; // rax
char *v6; // r14
int v7; // er13
int *v9; // rax
char *v10; // rax
unsigned int fd; // [rsp+Ch] [rbp-2Ch]

while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "qxtvdAw");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'A':
case 'd':
case 'q':
case 't':
case 'v':
case 'w':
case 'x':
v3 = v4;
break;
default:
usage();
exit(1);
}
}
if ( optind >= argc )
{
puts("no archive file given.");
usage();
v6 = 0LL;
}
else
{
v5 = strlen(argv[optind]);
v6 = (char *)malloc(v5 + 1);
if ( !v6 )
_assert_fail("ar_name != NULL", "main.c", 0x3Eu, "main");
v7 = optind;
strcpy(v6, argv[optind]);
optind = v7 + 1;
}
fd = open_ar_file(v6, (unsigned int)v3);
do
{
if ( v3 == 65 )
append_all(fd, v6);
++optind;
}
while ( optind < argc );
if ( close(fd) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
printf("ERROR: %s\n", v10);
}
return 0;
}

original_code:
int main(int argc, char **argv) {
 int fd;
    char c;
    char key;
    char *ar_name = ((void *)0);
    while ((c = getopt (argc, argv, "qxtvdAw")) != -1)
        switch (c) {
            case 'q':
                key = 'q';
                break;
            case 'x':
                key = 'x';
                break;
            case 't':
                key = 't';
                break;
            case 'v':
                key = 'v';
                break;
            case 'd':
                key = 'd';
                break;
            case 'A':
                key = 'A';
                break;
            case 'w':
                key = 'w';
                break;
            default:
                usage();
                exit(1);
                break;
        }
    if (!key){
     printf("No option given\n");
     usage();
     exit(-1);
    }
    if (optind < argc) {
        ar_name = (char*)malloc(sizeof(char) * (strlen(argv[optind]) + 1));
        ((void) sizeof ((ar_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (ar_name != ((void *)0)) ; else __assert_fail ("ar_name != NULL", "main.c", 62, __extension__ __PRETTY_FUNCTION__); }));
        strcpy(ar_name, argv[optind]);
        optind = optind + 1;
    }else {
        printf("no archive file given.\n");
        usage();
    }
    fd = open_ar_file(ar_name, key);
    do {
        switch(key) {
            case 'q':
                if (argv[optind] == ((void *)0)) {
                    printf("no file given to append.\n");
                 usage();
                 exit(1);
                }
             append(fd, &(argv[optind]), (argc-optind));
             optind = argc;
                break;
            case 't':
                print_concise(fd);
                break;
            case 'v':
                print_verbose(fd);
                break;
            case 'x':
                extract(fd, argv[optind] ? argv[optind] : ((void *)0));
                break;
            case 'd':
                if (argv[optind] == ((void *)0)) {
                    printf("no file given to delete.\n");
                 usage();
                 exit(1);
                }
             delete(&fd, ar_name, &(argv[optind]), (argc-optind));
             optind = argc;
                break;
            case 'A':
                append_all(fd, ar_name);
                break;
            case 'w':
                break;
        }
        optind++;
    } while (optind < argc);
    if (close(fd) == -1)
        printf("ERROR: %s\n", strerror((*__errno_location ())));
    return(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 int fd;
    char c;
    char key;
    char *ar_name = ((void *)0);
    while ((c = getopt (argc, argv, "qxtvdAw")) != -1)
        switch (c) {
            case 'q':
                key = 'q';
                break;
            case 'x':
                key = 'x';
                break;
            case 't':
                key = 't';
                break;
            case 'v':
                key = 'v';
                break;
            case 'd':
                key = 'd';
                break;
            case 'A':
                key = 'A';
                break;
            case 'w':
                key = 'w';
                break;
            default:
                usage();
                exit(1);
                break;
        }
    if (!key){
     printf("No option given\n");
     usage();
     exit(-1);
    }
    if (optind < argc) {
        ar_name = (char *)malloc(sizeof(char) * (strlen(argv[optind]) + 1));
        ((void) sizeof ((ar_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (ar_name != ((void *)0)) ; else __assert_fail ("ar_name != NULL", "main.c", 62, __extension__ __PRETTY_FUNCTION__); }));
        strcpy(ar_name, argv[optind]);
        optind = optind + 1;
    }else {
        printf("no archive file given.\n");
        usage();
    }
    fd = open_ar_file(ar_name, key);
    do {
        switch(key) {
            case 'q':
                if (argv[optind] == ((void *)0)) {
                    printf("no file given to append.\n");
                 usage();
                 exit(1);
                }
             append(fd, &(argv[optind]), (argc-optind));
             optind = argc;
                break;
            case 't':
                print_concise(fd);
                break;
            case 'v':
                print_verbose(fd);
                break;
            case 'x':
                extract(fd, argv[optind] ? argv[optind] : ((void *)0));
                break;
            case 'd':
                if (argv[optind] == ((void *)0)) {
                    printf("no file given to delete.\n");
                 usage();
                 exit(1);
                }
             delete(&fd, ar_name, &(argv[optind]), (argc-optind));
             optind = argc;
                break;
            case 'A':
                append_all(fd, ar_name);
                break;
            case 'w':
                break;
        }
        optind++;
    } while (optind < argc);
    if (close(fd) == -1)
        printf("ERROR: %s\n", strerror((*__errno_location ())));
    return(0);
}

variable_types: fd int

variable_types: c char

variable_types: key char


repo: beckysag/prime-finder

decompiled_code:
void __fastcall __noreturn perror_and_exit(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func12(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void perror_and_exit(char *msg) {
 perror(msg);
 exit(1);
}

variable_types:
msg
char *

return_type: void


usage

decompiled_code:
int usage()
{
printf("%s %s", "Usage:\n", "primePThread [-qv] [-m max_prime] [-c concurrency]\n\n");
puts("Options:");
printf("%*s%*s\n", 4, "-q", 29, "don't print prime numbers");
printf("%*s%*s\n", 4, "-v", 23, "verbose: print timing and count");
printf("%*s%*s\n", 4, "-m", 36, "maximum size of the prime number");
return printf("%*s%*s\n", 4, "-c", 23, "concurrency to use\n");
}

canonical_decompiled_code:
int func13()
{
printf("%s %s", "Usage:\n", "primePThread [-qv] [-m max_prime] [-c concurrency]\n\n");
puts("Options:");
printf("%*s%*s\n", 4, "-q", 29, "don't print prime numbers");
printf("%*s%*s\n", 4, "-v", 23, "verbose: print timing and count");
printf("%*s%*s\n", 4, "-m", 36, "maximum size of the prime number");
return printf("%*s%*s\n", 4, "-c", 23, "concurrency to use\n");
}

original_code:
void usage() {
 printf("%s  %s", "Usage:\n",
   "primePThread [-qv] [-m max_prime] [-c concurrency]\n\n");
 printf("%s", "Options:\n");
 printf("%*s%*s\n", 4, "-q", 29, "don't print prime numbers");
 printf("%*s%*s\n", 4, "-v", 23, "verbose: print timing and count");
 printf("%*s%*s\n", 4, "-m", 36, "maximum size of the prime number");
 printf("%*s%*s\n", 4, "-c", 23, "concurrency to use\n");
}


decompiled_code:
__int64 __fastcall socket_check_writable(int a1, int a2)
{
__int64 v2; // rbp
int v3; // eax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set writefds; // [rsp+10h] [rbp-98h] BYREF

memset(&writefds, 0, sizeof(writefds));
v2 = 1LL << (a1 % 64);
writefds.fds_bits[a1 / 64] |= v2;
v5.tv_sec = a2 / 1000;
v5.tv_usec = 1000 * (a2 % 1000);
v3 = select(a1 + 1, 0LL, &writefds, 0LL, &v5);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
if ( v3 )
return (unsigned int)-((writefds.fds_bits[a1 / 64] & v2) == 0);
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // rbp
int v3; // eax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set writefds; // [rsp+10h] [rbp-98h] BYREF

memset(&writefds, 0, sizeof(writefds));
v2 = 1LL << (a1 % 64);
writefds.fds_bits[a1 / 64] |= v2;
v5.tv_sec = a2 / 1000;
v5.tv_usec = 1000 * (a2 % 1000);
v3 = select(a1 + 1, 0LL, &writefds, 0LL, &v5);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
if ( v3 )
return (unsigned int)-((writefds.fds_bits[a1 / 64] & v2) == 0);
return 4294967294LL;
}

original_code:
static int socket_check_writable(int socket_fd, int ms)
{
        fd_set fdw;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdw)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fdw)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
        struct timeval tv;
        tv.tv_sec = ms / 1000;
        tv.tv_usec = (ms % 1000) * 1000;
        int ret = select(socket_fd + 1, ((void *)0), &fdw, ((void *)0), &tv);
        if (ret < 0) {
                return -1;
        } else if (ret == 0) {
                return -2;
        }
        if (((((&fdw)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                return 0;
        }
        return -1;
}

canonical_original_code:
static int socket_check_writable(int socket_fd, int ms)
{
        struct fd_set fdw;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdw)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fdw)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
        struct timeval tv;
        tv.tv_sec = ms / 1000;
        tv.tv_usec = (ms % 1000) * 1000;
        int ret = select(socket_fd + 1, ((void *)0), &fdw, ((void *)0), &tv);
        if (ret < 0) {
                return -1;
        } else if (ret == 0) {
                return -2;
        }
        if (((((&fdw)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                return 0;
        }
        return -1;
}

variable_types: ms int

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

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

variable_types: ret int

return_type: int


decompiled_code:
__int64 __fastcall socket_check_readable(int a1, int a2)
{
__int64 v2; // rbp
int v3; // eax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

memset(&readfds, 0, sizeof(readfds));
v2 = 1LL << (a1 % 64);
readfds.fds_bits[a1 / 64] |= v2;
v5.tv_sec = a2 / 1000;
v5.tv_usec = 1000 * (a2 % 1000);
v3 = select(a1 + 1, &readfds, 0LL, 0LL, &v5);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
if ( v3 )
return (unsigned int)-((readfds.fds_bits[a1 / 64] & v2) == 0);
return 4294967294LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
__int64 v2; // rbp
int v3; // eax
struct timeval v5; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

memset(&readfds, 0, sizeof(readfds));
v2 = 1LL << (a1 % 64);
readfds.fds_bits[a1 / 64] |= v2;
v5.tv_sec = a2 / 1000;
v5.tv_usec = 1000 * (a2 % 1000);
v3 = select(a1 + 1, &readfds, 0LL, 0LL, &v5);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
if ( v3 )
return (unsigned int)-((readfds.fds_bits[a1 / 64] & v2) == 0);
return 4294967294LL;
}

original_code:
static int socket_check_readable(int socket_fd, int ms)
{
        fd_set fdr;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdr)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fdr)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
        struct timeval tv;
        tv.tv_sec = ms / 1000;
        tv.tv_usec = (ms % 1000) * 1000;
        int ret = select(socket_fd + 1, &fdr, ((void *)0), ((void *)0), &tv);
        if (ret < 0) {
                return -1;
        } else if (ret == 0) {
                return -2;
        }
        if (((((&fdr)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                return 0;
        }
        return -1;
}

canonical_original_code:
static int socket_check_readable(int socket_fd, int ms)
{
        struct fd_set fdr;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fdr)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&fdr)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
        struct timeval tv;
        tv.tv_sec = ms / 1000;
        tv.tv_usec = (ms % 1000) * 1000;
        int ret = select(socket_fd + 1, &fdr, ((void *)0), ((void *)0), &tv);
        if (ret < 0) {
                return -1;
        } else if (ret == 0) {
                return -2;
        }
        if (((((&fdr)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                return 0;
        }
        return -1;
}

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

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


decompiled_code:
int socket_create_tcp()
{
return socket(2, 1, 6);
}

canonical_decompiled_code:
int func3()
{
return socket(2, 1, 6);
}

original_code:
int socket_create_tcp(void)
{
        int socket_fd = socket(2, SOCK_STREAM, IPPROTO_TCP);
        return socket_fd;
}

canonical_original_code:
int socket_create_tcp()
{
        int socket_fd = socket(2, SOCK_STREAM, IPPROTO_TCP);
        return socket_fd;
}


decompiled_code:
int __fastcall socket_set_nonblock(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func4(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) |= 8u;
return fcntl(fd, 4, v1);
}

original_code:
void socket_set_nonblock(int socket_fd)
{
        int flags = fcntl(socket_fd, 3);
        flags |= 04000;
        fcntl(socket_fd, 4, flags);
}

variable_types: flags int


decompiled_code:
int __fastcall socket_set_block(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

canonical_decompiled_code:
int __fastcall func5(int fd)
{
unsigned int v1; // eax

v1 = fcntl(fd, 3);
BYTE1(v1) &= 0xF7u;
return fcntl(fd, 4, v1);
}

original_code:
void socket_set_block(int socket_fd)
{
        int flags = fcntl(socket_fd, 3);
        flags &= ~04000;
        fcntl(socket_fd, 4, flags);
}


decompiled_code:
int __fastcall socket_connect(int fd, char *cp, __int16 a3)
{
int result; // eax
socklen_t optlen; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a3);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
result = connect(fd, &addr, 0x10u);
if ( result )
{
if ( (unsigned int)(*_errno_location() - 114) > 1 )
{
usleep(0x186A0u);
result = -1;
}
else
{
usleep(0x186A0u);
if ( (unsigned int)socket_check_writable(fd, 2000) )
{
result = -1;
}
else
{
optval = 0;
optlen = 4;
getsockopt(fd, 1, 4, &optval, &optlen);
result = -(optval != 0);
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(int fd, char *cp, __int16 a3)
{
int result; // eax
socklen_t optlen; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a3);
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
result = connect(fd, &addr, 0x10u);
if ( result )
{
if ( (unsigned int)(*_errno_location() - 114) > 1 )
{
usleep(0x186A0u);
result = -1;
}
else
{
usleep(0x186A0u);
if ( (unsigned int)func1(fd, 2000) )
{
result = -1;
}
else
{
optval = 0;
optlen = 4;
getsockopt(fd, 1, 4, &optval, &optlen);
result = -(optval != 0);
}
}
}
return result;
}

original_code:
int socket_connect(int socket_fd, char *dest_addr, int dest_port)
{
        struct sockaddr_in sin;
        bzero(&sin, sizeof(sin));
        sin.sin_family = 2;
        sin.sin_port = htons((u_short)dest_port);
        sin.sin_addr.s_addr = inet_addr(dest_addr);
        if (connect(socket_fd, (struct sockaddr *)&sin, sizeof(sin)) == 0) {
                return 0;
        }
        switch ((*__errno_location ())) {
        case 115:
        case 114:
                usleep(100000);
                break;
        default:
                usleep(100000);
                return -1;
        }
        int connect_timeout = 2000;
        if (socket_check_writable(socket_fd, connect_timeout)) {
                return -1;
        }
        int error = 0;
        socklen_t len = sizeof(error);
        getsockopt(socket_fd, 1, 4, &error, &len);
        if (error == 0) {
                return 0;
        }
        return -1;
}

canonical_original_code:
int socket_connect(int socket_fd, char *dest_addr, int dest_port)
{
        struct sockaddr_in sin;
        bzero(&sin, sizeof(sin));
        sin.sin_family = 2;
        sin.sin_port = htons((unsigned short)dest_port);
        sin.sin_addr.s_addr = inet_addr(dest_addr);
        if (connect(socket_fd, (struct sockaddr *)&sin, sizeof(sin)) == 0) {
                return 0;
        }
        switch ((*__errno_location ())) {
        case 115:
        case 114:
                usleep(100000);
                break;
        default:
                usleep(100000);
                return -1;
        }
        int connect_timeout = 2000;
        if (socket_check_writable(socket_fd, connect_timeout)) {
                return -1;
        }
        int error = 0;
        unsigned int len = sizeof(error);
        getsockopt(socket_fd, 1, 4, &error, &len);
        if (error == 0) {
                return 0;
        }
        return -1;
}

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

variable_types: error int

variable_types:
len
unsigned int

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

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


decompiled_code:
__int64 __fastcall socket_write(int fd, __int64 a2, int a3, int a4)
{
int i; // ebx
unsigned int v7; // ebp
int v8; // eax

if ( a3 <= 0 )
return 0;
for ( i = 0; i < a3; i += v8 )
{
v7 = socket_check_writable(fd, a4);
if ( v7 )
break;
v8 = send(fd, (const void *)(a2 + i), a3 - i, 0);
if ( (unsigned int)(v8 + 1) <= 1 )
return (unsigned int)-1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, __int64 a2, int a3, int a4)
{
int i; // ebx
unsigned int v7; // ebp
int v8; // eax

if ( a3 <= 0 )
return 0;
for ( i = 0; i < a3; i += v8 )
{
v7 = func1(fd, a4);
if ( v7 )
break;
v8 = send(fd, (const void *)(a2 + i), a3 - i, 0);
if ( (unsigned int)(v8 + 1) <= 1 )
return (unsigned int)-1;
}
return v7;
}

original_code:
int socket_write(int socket_fd, char *buf, int length, int ms)
{
        int sent_len = 0;
        int n = 0;
        int ret = 0;
        while (sent_len < length) {
                ret = socket_check_writable(socket_fd, ms);
                if (ret != 0) {
                        return ret;
                }
                n = send(socket_fd, buf + sent_len,
                                length - sent_len, 0);
                if (n == -1) {
                        return -1;
                } else if (n == 0) {
                        return -1;
                }
                sent_len += n;
        }
        return 0;
}

variable_types:
buf
char *

variable_types: length int

variable_types: n int


decompiled_code:
__int64 __fastcall socket_readn(int fd, void *buf, int a3, int a4)
{
__int64 result; // rax

result = socket_check_readable(fd, a4);
if ( !(_DWORD)result )
result = recv(fd, buf, a3, 0);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, void *buf, int a3, int a4)
{
__int64 result; // rax

result = func2(fd, a4);
if ( !(_DWORD)result )
result = recv(fd, buf, a3, 0);
return result;
}

original_code:
int socket_readn(int socket_fd, char *buf, int max_length, int ms)
{
        int length = 0;
        int ret = 0;
        ret = socket_check_readable(socket_fd, ms);
        if (ret == 0) {
                length = recv(socket_fd, buf, max_length, 0);
        } else {
                return ret;
        }
        return length;
}


decompiled_code:
__int64 __fastcall socket_read(int fd, __int64 a2, int a3, int a4)
{
int i; // ebx
unsigned int v7; // ebp
int v8; // eax

if ( a3 <= 0 )
return 0;
for ( i = 0; i < a3; i += v8 )
{
v7 = socket_check_readable(fd, a4);
if ( v7 )
break;
v8 = recv(fd, (void *)(a2 + i), a3 - i, 0);
if ( (unsigned int)(v8 + 1) <= 1 )
return (unsigned int)-1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, __int64 a2, int a3, int a4)
{
int i; // ebx
unsigned int v7; // ebp
int v8; // eax

if ( a3 <= 0 )
return 0;
for ( i = 0; i < a3; i += v8 )
{
v7 = func2(fd, a4);
if ( v7 )
break;
v8 = recv(fd, (void *)(a2 + i), a3 - i, 0);
if ( (unsigned int)(v8 + 1) <= 1 )
return (unsigned int)-1;
}
return v7;
}

original_code:
int socket_read(int socket_fd, char *buf, int req_length, int ms)
{
        char *ptr = (char *)buf;
        int rLen = 0;
        int n = 0;
        int ret = 0;
        while (rLen < req_length) {
                ret = socket_check_readable(socket_fd, ms);
                if (ret == 0) {
                        n = recv(socket_fd, ptr + rLen, req_length - rLen, 0);
                        if (n == -1) {
                                ret = -1;
                                break;
                        } else if (n == 0) {
                                ret = -1;
                                break;
                        }
                        rLen += n;
                } else {
                        break;
                }
        }
        return ret;
}

variable_types:
ptr
char *

variable_types: rLen int


decompiled_code:
__int64 __fastcall socket_clear_recv_buffer(int fd)
{
__int64 result; // rax
__int64 v2; // [rsp+0h] [rbp-108h] BYREF

do
result = socket_read(fd, (__int64)&v2, 256, 0);
while ( (unsigned int)result < 0xFFFFFFFE );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
__int64 result; // rax
__int64 v2; // [rsp+0h] [rbp-108h] BYREF

do
result = func9(fd, (__int64)&v2, 256, 0);
while ( (unsigned int)result < 0xFFFFFFFE );
return result;
}

original_code:
void socket_clear_recv_buffer(int socket_fd)
{
        int ret = 0;
        int buf_size = 256;
        char buf[buf_size];
        while (1) {
                ret = socket_read(socket_fd, buf, buf_size, 0);
                if ((ret == -2)
                        || (ret == -1)) {
                        break;
                }
        }
}

canonical_original_code:
void socket_clear_recv_buffer(int socket_fd)
{
        int ret = 0;
        int buf_size = 256;
        char buf[-1];
        while (1) {
                ret = socket_read(socket_fd, buf, buf_size, 0);
                if ((ret == -2)
                        || (ret == -1)) {
                        break;
                }
        }
}

variable_types:
buf
char [-1]


decompiled_code:
__int64 __fastcall socket_close(int fd)
{
if ( fd )
{
shutdown(fd, 2);
close(fd);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd)
{
if ( fd )
{
shutdown(fd, 2);
close(fd);
}
return 0LL;
}

original_code:
int socket_close(int socket_fd)
{
        if (socket_fd) {
                shutdown(socket_fd, SHUT_RDWR);
                close(socket_fd);
        }
        return 0;
}


decompiled_code:
__int64 ftp_put_file_port()
{
return 0LL;
}

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

original_code:
static int ftp_put_file_port()
{
        return 0;
}


decompiled_code:
int __fastcall ftp_get_response(__int64 a1)
{
char dest[4]; // [rsp+Ch] [rbp-Ch] BYREF

*(_DWORD *)dest = 0;
memset(response, 0, sizeof(response));
if ( (int)socket_readn(*(unsigned int *)(a1 + 132), response, 256LL, 2000LL) <= 0 )
return -1;
strncpy(dest, response, 3uLL);
socket_clear_recv_buffer(*(unsigned int *)(a1 + 132));
return atoi(dest);
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
char dest[4]; // [rsp+Ch] [rbp-Ch] BYREF

*(_DWORD *)dest = 0;
memset(response, 0, sizeof(response));
if ( (int)socket_readn(*(unsigned int *)(a1 + 132), response, 256LL, 2000LL) <= 0 )
return -1;
strncpy(dest, response, 3uLL);
socket_clear_recv_buffer(*(unsigned int *)(a1 + 132));
return atoi(dest);
}

original_code:
static int ftp_get_response(struct ftp_connect *ftp_client)
{
        char response_code[4] = {0};
        memset(response, 0, sizeof(response));
        int len = socket_readn(ftp_client->control_fd, response,
                                256, kSocketTimeout);
        if (len <= 0) {
                return -1;
        }
        strncpy(response_code, response, 3);
        socket_clear_recv_buffer(ftp_client->control_fd);
        return atoi(response_code);
}

variable_types: struct ftp_connect { char hostname[64]; char username[32]; char password[32]; unsigned short control_port; int control_fd; int data_fd; int mode; } *

variable_types: len int


decompiled_code:
__int64 __fastcall ftp_send_cmd(int a1, const char *a2, __int64 a3)
{
size_t v4; // rax
unsigned int v5; // ebp
int v6; // eax
char v8[280]; // [rsp+0h] [rbp-118h] BYREF

if ( a2 )
sprintf(v8, "%s %s\r\n", *((const char **)ftp_cmd + 2 * a1), a2);
else
sprintf(v8, "%s\r\n", *((const char **)ftp_cmd + 2 * a1));
v4 = strlen(v8);
v5 = socket_write(*(unsigned int *)(a3 + 132), v8, v4, 2000LL);
if ( v5 )
return (unsigned int)-1;
v6 = ftp_get_response(a3);
if ( *((_DWORD *)ftp_cmd + 4 * a1 + 2) != v6 )
v5 = (v6 != 421) - 2;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, const char *a2, __int64 a3)
{
size_t v4; // rax
unsigned int v5; // ebp
int v6; // eax
char v8[280]; // [rsp+0h] [rbp-118h] BYREF

if ( a2 )
sprintf(v8, "%s %s\r\n", *((const char **)ftp_cmd + 2 * a1), a2);
else
sprintf(v8, "%s\r\n", *((const char **)ftp_cmd + 2 * a1));
v4 = strlen(v8);
v5 = socket_write(*(unsigned int *)(a3 + 132), v8, v4, 2000LL);
if ( v5 )
return (unsigned int)-1;
v6 = func1(a3);
if ( *((_DWORD *)ftp_cmd + 4 * a1 + 2) != v6 )
v5 = (v6 != 421) - 2;
return v5;
}

original_code:
static int ftp_send_cmd(int index,
                        char *append_info,
                        struct ftp_connect *ftp_client)
{
        char cmd[256];
        if (append_info) {
                sprintf(cmd, "%s %s\r\n", ftp_cmd[index].cmd, append_info);
        } else {
                sprintf(cmd, "%s\r\n", ftp_cmd[index].cmd);
        }
        int ret = socket_write(ftp_client->control_fd, cmd,
                                        strlen(cmd), kSocketTimeout);
        if (ret != 0) {
                return -1;
        }
        int response_code = ftp_get_response(ftp_client);
        if (response_code == ftp_cmd[index].response_code) {
                return 0;
        }
        if (response_code == RES_SERVER_DOWN) {
                return -2;
        }
        return -1;
}

variable_types: index int

variable_types:
cmd
char [256]

user_defined_types: struct ftp_connect { char hostname[64]; char username[32]; char password[32]; unsigned short control_port; int control_fd; int data_fd; int mode; }


decompiled_code:
__int64 __fastcall ftp_mkdir(__int64 a1, const char *a2)
{
return ftp_send_cmd(4, a2, a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2)
{
return func2(4, a2, a1);
}

original_code:
static int ftp_mkdir(struct ftp_connect *client, char *path)
{
        return ftp_send_cmd(Mkdir, path, client);
}

variable_types:
path
char *


decompiled_code:
__int64 __fastcall ftp_cwd(__int64 a1, const char *a2)
{
return ftp_send_cmd(5, a2, a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const char *a2)
{
return func2(5, a2, a1);
}

original_code:
static int ftp_cwd(struct ftp_connect *client, char *path)
{
        return ftp_send_cmd(ChangeDir, path, client);
}


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

result = ftp_cwd(a1, a2);
if ( (((_DWORD)result + 2) & 0xFFFFFFFD) != 0 )
{
result = ftp_mkdir(a1, a2);
if ( (unsigned int)(result + 2) > 1 )
result = ftp_cwd(a1, a2);
}
return result;
}

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

result = func4(a1, a2);
if ( (((_DWORD)result + 2) & 0xFFFFFFFD) != 0 )
{
result = func3(a1, a2);
if ( (unsigned int)(result + 2) > 1 )
result = func4(a1, a2);
}
return result;
}

original_code:
static int ftp_enter_dir(struct ftp_connect *client, char *path)
{
        int ret = 0;
        do {
                ret = ftp_cwd(client, path);
                if (ret == 0 ||
                        ret == -2) {
                        break;
                }
                ret = ftp_mkdir(client, path);
                if (ret == -1 ||
                        ret == -2) {
                        break;
                }
                ret = ftp_cwd(client, path);
                if (ret == 0 ||
                        ret == -2) {
                        break;
                }
        } while(0);
        return ret;
}

variable_types: ret int


original_code:
static int ftp_change_dir(struct ftp_connect *client, char *absolute_path)
{
        int ret = 0;
        char path[32];
        char *p = absolute_path;
        if (*p == '/') {
                ++p;
        }
        ret = ftp_enter_dir(client, "/");
        if (ret != 0) {
                return ret;
        }
        int index = 0;
        while (1) {
                index = 0;
                memset(path, 0, sizeof(path));
                while ((*p != '/') && (*p != '\0')) {
                        path[index] = *p;
                        ++index;
                        ++p;
                }
                path[index] = '\0';
                if (path[0] != '\0') {
                        ret = ftp_enter_dir(client, path);
                        if (ret != 0) {
                                break;
                        }
                }
                if (*p == '\0') {
                        break;
                }
                ++p;
        }
        return ret;
}

variable_types:
path
char [32]

variable_types:
p
char *


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

result = ftp_send_cmd(0, (const char *)(a1 + 64), a1);
if ( !(_DWORD)result )
result = ftp_send_cmd(1, (const char *)(a1 + 96), a1);
return result;
}

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

result = func2(0, (const char *)(a1 + 64), a1);
if ( !(_DWORD)result )
result = func2(1, (const char *)(a1 + 96), a1);
return result;
}

original_code:
static int ftp_login(struct ftp_connect *client)
{
        int ret = ftp_send_cmd(Username, client->username, client);
        if (ret == 0) {
                ret = ftp_send_cmd(Password, client->password, client);
        }
        return ret;
}


decompiled_code:
__int64 __fastcall ftp_connect(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp

v1 = socket_create_tcp();
*(_DWORD *)(a1 + 132) = v1;
socket_set_nonblock(v1);
if ( (unsigned int)socket_connect(*(unsigned int *)(a1 + 132), a1, *(unsigned __int16 *)(a1 + 128)) )
return (unsigned int)-1;
v2 = 0;
socket_set_block(*(unsigned int *)(a1 + 132));
if ( ftp_get_response(a1) != 220 )
return (unsigned int)-1;
socket_clear_recv_buffer(*(unsigned int *)(a1 + 132));
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp

v1 = socket_create_tcp();
*(_DWORD *)(a1 + 132) = v1;
socket_set_nonblock(v1);
if ( (unsigned int)socket_connect(*(unsigned int *)(a1 + 132), a1, *(unsigned __int16 *)(a1 + 128)) )
return (unsigned int)-1;
v2 = 0;
socket_set_block(*(unsigned int *)(a1 + 132));
if ( func1(a1) != 220 )
return (unsigned int)-1;
socket_clear_recv_buffer(*(unsigned int *)(a1 + 132));
return v2;
}

original_code:
static int ftp_connect(struct ftp_connect *ftp_client)
{
        int ret = -1;
        ftp_client->control_fd = socket_create_tcp();
        socket_set_nonblock(ftp_client->control_fd);
        ret = socket_connect(ftp_client->control_fd, ftp_client->hostname,
                                ftp_client->control_port);
        if (ret) {
                return -1;
        }
        socket_set_block(ftp_client->control_fd);
        if (ftp_get_response(ftp_client) == RES_CONNECT_SUCCESS) {
                socket_clear_recv_buffer(ftp_client->control_fd);
                return 0;
        }
        return -1;
}


decompiled_code:
__int64 __fastcall ftp_put_file_pasv(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned int v3; // eax
int v4; // er12
_DWORD v5[19]; // [rsp+0h] [rbp-68h] BYREF
unsigned int v6[7]; // [rsp+4Ch] [rbp-1Ch] BYREF

v6[0] = 0;
result = ftp_get_file_size(a1, a2, (int *)v6);
if ( (_DWORD)result != -1 )
{
result = 0LL;
if ( *(_DWORD *)(a2 + 520) > v6[0] )
{
ftp_get_passive(a1, (__int64)v5);
v3 = socket_create_tcp();
*(_DWORD *)(a1 + 136) = v3;
socket_set_nonblock(v3);
v4 = socket_connect(*(unsigned int *)(a1 + 136), v5, v5[16]);
socket_set_block(*(unsigned int *)(a1 + 136));
if ( v4 )
{
socket_close(*(unsigned int *)(a1 + 136));
puts("connect failed");
result = 0xFFFFFFFFLL;
}
else if ( (unsigned int)ftp_send_cmd(6, (const char *)(a2 + 256), a1) )
{
puts("send command failed");
result = 0xFFFFFFFFLL;
}
else
{
socket_write(
*(unsigned int *)(a1 + 136),
*(_QWORD *)(a2 + 512) + (int)v6[0],
*(_DWORD *)(a2 + 520) - v6[0],
2000LL);
socket_close(*(unsigned int *)(a1 + 136));
result = (unsigned int)-(ftp_get_response(a1) != 226);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2)
{
__int64 result; // rax
unsigned int v3; // eax
int v4; // er12
_DWORD v5[19]; // [rsp+0h] [rbp-68h] BYREF
unsigned int v6[7]; // [rsp+4Ch] [rbp-1Ch] BYREF

v6[0] = 0;
result = func9(a1, a2, (int *)v6);
if ( (_DWORD)result != -1 )
{
result = 0LL;
if ( *(_DWORD *)(a2 + 520) > v6[0] )
{
func10(a1, (__int64)v5);
v3 = socket_create_tcp();
*(_DWORD *)(a1 + 136) = v3;
socket_set_nonblock(v3);
v4 = socket_connect(*(unsigned int *)(a1 + 136), v5, v5[16]);
socket_set_block(*(unsigned int *)(a1 + 136));
if ( v4 )
{
socket_close(*(unsigned int *)(a1 + 136));
puts("connect failed");
result = 0xFFFFFFFFLL;
}
else if ( (unsigned int)func2(6, (const char *)(a2 + 256), a1) )
{
puts("send command failed");
result = 0xFFFFFFFFLL;
}
else
{
socket_write(
*(unsigned int *)(a1 + 136),
*(_QWORD *)(a2 + 512) + (int)v6[0],
*(_DWORD *)(a2 + 520) - v6[0],
2000LL);
socket_close(*(unsigned int *)(a1 + 136));
result = (unsigned int)-(func1(a1) != 226);
}
}
}
return result;
}

original_code:
static int ftp_put_file_pasv(struct ftp_connect *client,
                                struct file_description *file)
{
        int exist_size = 0;
        int ret = ftp_get_file_size(client, file, &exist_size);
        if (ret == -1) {
                return -1;
        }
        if (exist_size >= file->file_size) {
                return 0;
        }
        struct _data_connect data_connect;
        ftp_get_passive(client, &data_connect);
        client->data_fd = socket_create_tcp();
        socket_set_nonblock(client->data_fd);
        ret = socket_connect(client->data_fd, data_connect.addr,
                                        data_connect.port);
        socket_set_block(client->data_fd);
        if (ret) {
                socket_close(client->data_fd);
                printf("connect failed\n");
                return -1;
        }
        ret = ftp_send_cmd(Upload, file->file_name, client);
        if (ret) {
                printf("send command failed\n");
                return -1;
        }
        ret = socket_write(client->data_fd, file->file_buffer + exist_size,
                                file->file_size - exist_size, kSocketTimeout);
        socket_close(client->data_fd);
        int response_code = ftp_get_response(client);
        if (response_code == RES_TRANSFER_OK) {
                return 0;
        } else {
                return -1;
        }
}

variable_types: struct file_description { char remote_dir[256]; char file_name[256]; char *file_buffer; unsigned int file_size; } *

variable_types: struct _data_connect { char addr[64]; unsigned int port; }


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

result = ftp_send_cmd(7, 0LL, a1);
if ( (unsigned int)(result + 2) > 1 )
{
if ( *(_DWORD *)(a1 + 140) )
result = ftp_put_file_port(a1, a2);
else
result = ftp_put_file_pasv(a1, a2);
}
return result;
}

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

result = func2(7, 0LL, a1);
if ( (unsigned int)(result + 2) > 1 )
{
if ( *(_DWORD *)(a1 + 140) )
result = func0(a1, a2);
else
result = func11(a1, a2);
}
return result;
}

original_code:
static int ftp_put_file(struct ftp_connect *client,
                                struct file_description *file)
{
        int ret = ftp_send_cmd(FileType, ((void *)0), client);
        if (ret == -1 ||
                ret == -2) {
                return ret;
        }
        if (client->mode) {
                ret = ftp_put_file_port(client, file);
        } else {
                ret = ftp_put_file_pasv(client, file);
        }
        return ret;
}

user_defined_types: struct file_description { char remote_dir[256]; char file_name[256]; char *file_buffer; unsigned int file_size; }


processSignal

decompiled_code:
void __fastcall processSignal(unsigned int a1)
{
printf("signal %d\n", a1);
signal(a1, processSignal);
}

canonical_decompiled_code:
void __fastcall func13(unsigned int a1)
{
printf("signal %d\n", a1);
signal(a1, processSignal);
}

original_code:
static void processSignal(int signo)
{
        printf("signal %d\n", signo);
        signal(signo, processSignal);
}

variable_types: signo int


original_code:
static void ftp_cmd_init(void)
{
        int size = sizeof(struct _ftp_cmd) * MaxIndex;
        ftp_cmd = (struct _ftp_cmd *)malloc(size);
        bzero(ftp_cmd, size);
        ftp_cmd[0].cmd = "USER";
        ftp_cmd[0].response_code = RES_REQUIRE_PASSWORD;
        ftp_cmd[1].cmd = "PASS";
        ftp_cmd[1].response_code = RES_LOGIN_PASS;
        ftp_cmd[2].cmd = "QUIT";
        ftp_cmd[2].response_code = RES_QUIT_SUCCESS;
        ftp_cmd[3].cmd = "PWD";
        ftp_cmd[3].response_code = RES_PWD;
        ftp_cmd[4].cmd = "MKD";
        ftp_cmd[4].response_code = RES_MKDIR;
        ftp_cmd[5].cmd = "CWD";
        ftp_cmd[5].response_code = RES_CHANGE_DIR;
        ftp_cmd[6].cmd = "STOR";
        ftp_cmd[6].response_code = RES_UPLOAD;
        ftp_cmd[7].cmd = "TYPE I";
        ftp_cmd[7].response_code = RES_EXCUTE_SUCCESS;
        ftp_cmd[8].cmd = "PASV";
        ftp_cmd[8].response_code = RES_PASSIVE;
        ftp_cmd[9].cmd = "PORT";
        ftp_cmd[9].response_code = RES_EXCUTE_SUCCESS;
        ftp_cmd[10].cmd = "SIZE";
        ftp_cmd[10].response_code = RES_UPLOADED_SIZE;
        ftp_cmd[11].cmd = "REST";
        ftp_cmd[11].response_code = RES_REST_POSITION;
        kInit = 1;
}

canonical_original_code:
static void ftp_cmd_init()
{
        int size = sizeof(struct _ftp_cmd) * MaxIndex;
        ftp_cmd = (struct _ftp_cmd *)malloc(size);
        bzero(ftp_cmd, size);
        ftp_cmd[0].cmd = "USER";
        ftp_cmd[0].response_code = RES_REQUIRE_PASSWORD;
        ftp_cmd[1].cmd = "PASS";
        ftp_cmd[1].response_code = RES_LOGIN_PASS;
        ftp_cmd[2].cmd = "QUIT";
        ftp_cmd[2].response_code = RES_QUIT_SUCCESS;
        ftp_cmd[3].cmd = "PWD";
        ftp_cmd[3].response_code = RES_PWD;
        ftp_cmd[4].cmd = "MKD";
        ftp_cmd[4].response_code = RES_MKDIR;
        ftp_cmd[5].cmd = "CWD";
        ftp_cmd[5].response_code = RES_CHANGE_DIR;
        ftp_cmd[6].cmd = "STOR";
        ftp_cmd[6].response_code = RES_UPLOAD;
        ftp_cmd[7].cmd = "TYPE I";
        ftp_cmd[7].response_code = RES_EXCUTE_SUCCESS;
        ftp_cmd[8].cmd = "PASV";
        ftp_cmd[8].response_code = RES_PASSIVE;
        ftp_cmd[9].cmd = "PORT";
        ftp_cmd[9].response_code = RES_EXCUTE_SUCCESS;
        ftp_cmd[10].cmd = "SIZE";
        ftp_cmd[10].response_code = RES_UPLOADED_SIZE;
        ftp_cmd[11].cmd = "REST";
        ftp_cmd[11].response_code = RES_REST_POSITION;
        kInit = 1;
}

variable_types: size int

user_defined_types: struct _ftp_cmd { char *cmd; unsigned int response_code; }


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

if ( (unsigned int)ftp_change_dir(a1, a2) )
result = 0xFFFFFFFFLL;
else
result = ftp_put_file(a1, (__int64)a2);
return result;
}

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

if ( (unsigned int)func6(a1, a2) )
result = 0xFFFFFFFFLL;
else
result = func12(a1, (__int64)a2);
return result;
}

original_code:
int ftp_upload(struct ftp_connect *client, struct file_description *file)
{
        int ret = ftp_change_dir(client, file->remote_dir);
        if (ret) {
                return -1;
        }
        return ftp_put_file(client, file);
}


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

if ( (unsigned int)ftp_connect(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)ftp_login(a1) != 0);
return result;
}

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

if ( (unsigned int)func8(a1) )
result = 0xFFFFFFFFLL;
else
result = (unsigned int)-((unsigned int)func7(a1) != 0);
return result;
}

original_code:
int ftp_open(struct ftp_connect *client)
{
        int ret = ftp_connect(client);
        if (ret) {
                return -1;
        }
        ret = ftp_login(client);
        if (ret) {
                return -1;
        }
        return 0;
}


decompiled_code:
__int64 __fastcall ftp_close(__int64 a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
socket_close(*(unsigned int *)(a1 + 132));
socket_close(*(unsigned int *)(a1 + 136));
*(_DWORD *)(a1 + 132) = -1;
*(_DWORD *)(a1 + 136) = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
if ( !a1 )
return 0xFFFFFFFFLL;
socket_close(*(unsigned int *)(a1 + 132));
socket_close(*(unsigned int *)(a1 + 136));
*(_DWORD *)(a1 + 132) = -1;
*(_DWORD *)(a1 + 136) = -1;
return 0LL;
}

original_code:
int ftp_close(struct ftp_connect *client)
{
        if (client == ((void *)0)) {
                return -1;
        }
        socket_close(client->control_fd);
        socket_close(client->data_fd);
        client->control_fd = -1;
        client->data_fd = -1;
        return 0;
}


decompiled_code:
__sighandler_t ftp_init()
{
ftp_cmd_init();
return signal(13, (__sighandler_t)processSignal);
}

canonical_decompiled_code:
__sighandler_t func18()
{
func14();
return signal(13, (__sighandler_t)processSignal);
}

original_code:
void ftp_init(void)
{
        ftp_cmd_init();
        signal(13, processSignal);
}

canonical_original_code:
void ftp_init()
{
        ftp_cmd_init();
        signal(13, processSignal);
}


decompiled_code:
void ftp_release()
{
free(ftp_cmd);
ftp_cmd = 0LL;
}

canonical_decompiled_code:
void func19()
{
free(ftp_cmd);
ftp_cmd = 0LL;
}

original_code:
void ftp_release(void)
{
        free(ftp_cmd);
        ftp_cmd = ((void *)0);
}

canonical_original_code:
void ftp_release()
{
        free(ftp_cmd);
        ftp_cmd = ((void *)0);
}


repo: bedops/bedops

decompiled_code:
int *get_crc_table()
{
return crc_table;
}

canonical_decompiled_code:
int *func4()
{
return crc_table;
}

original_code:
const z_crc_t * get_crc_table()
{
    return (const z_crc_t *)crc_table;
}

canonical_original_code:
unsigned int *get_crc_table()
{
    return (unsigned int *)crc_table;
}

return_type: unsigned int *


error

decompiled_code:
void __fastcall __noreturn error(const char *a1)
{
fprintf(stderr, "%s: %s\n", (const char *)prog, a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(const char *a1)
{
fprintf(stderr, "%s: %s\n", (const char *)prog, a1);
exit(1);
}

original_code:
void error(const char *msg)
{
  fprintf(stderr, "%s: %s\n", prog, msg);
  exit(1);
}

canonical_original_code:
void error(char *msg)
{
  fprintf(stderr, "%s: %s\n", prog, msg);
  exit(1);
}

variable_types:
msg
char *


original_code:
void BZ2_hbMakeCodeLengths ( UChar *len,
                             Int32 *freq,
                             Int32 alphaSize,
                             Int32 maxLen )
{
   Int32 nNodes, nHeap, n1, n2, i, j, k;
   Bool tooLong;
   Int32 heap [ 258 + 2 ];
   Int32 weight [ 258 * 2 ];
   Int32 parent [ 258 * 2 ];
   for (i = 0; i < alphaSize; i++)
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
   while (((Bool)1)) {
      nNodes = alphaSize;
      nHeap = 0;
      heap[0] = 0;
      weight[0] = 0;
      parent[0] = -2;
      for (i = 1; i <= alphaSize; i++) {
         parent[i] = -1;
         nHeap++;
         heap[nHeap] = i;
         { Int32 zz, tmp; zz = nHeap; tmp = heap[zz]; while (weight[tmp] < weight[heap[zz >> 1]]) { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; };
      }
      { if (!(nHeap < (258 +2))) BZ2_bz__AssertH__fail ( 2001 ); };
      while (nHeap > 1) {
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; { Int32 zz, yy, tmp; zz = 1; tmp = heap[zz]; while (((Bool)1)) { yy = zz << 1; if (yy > nHeap) break; if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) yy++; if (weight[tmp] < weight[heap[yy]]) break; heap[zz] = heap[yy]; zz = yy; } heap[zz] = tmp; };
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; { Int32 zz, yy, tmp; zz = 1; tmp = heap[zz]; while (((Bool)1)) { yy = zz << 1; if (yy > nHeap) break; if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) yy++; if (weight[tmp] < weight[heap[yy]]) break; heap[zz] = heap[yy]; zz = yy; } heap[zz] = tmp; };
         nNodes++;
         parent[n1] = parent[n2] = nNodes;
         weight[nNodes] = (((weight[n1]) & 0xffffff00)+((weight[n2]) & 0xffffff00)) | (1 + ((((weight[n1]) & 0x000000ff)) > (((weight[n2]) & 0x000000ff)) ? (((weight[n1]) & 0x000000ff)) : (((weight[n2]) & 0x000000ff))));
         parent[nNodes] = -1;
         nHeap++;
         heap[nHeap] = nNodes;
         { Int32 zz, tmp; zz = nHeap; tmp = heap[zz]; while (weight[tmp] < weight[heap[zz >> 1]]) { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; };
      }
      { if (!(nNodes < (258 * 2))) BZ2_bz__AssertH__fail ( 2002 ); };
      tooLong = ((Bool)0);
      for (i = 1; i <= alphaSize; i++) {
         j = 0;
         k = i;
         while (parent[k] >= 0) { k = parent[k]; j++; }
         len[i-1] = j;
         if (j > maxLen) tooLong = ((Bool)1);
      }
      if (! tooLong) break;
      for (i = 1; i <= alphaSize; i++) {
         j = weight[i] >> 8;
         j = 1 + (j / 2);
         weight[i] = j << 8;
      }
   }
}

canonical_original_code:
void BZ2_hbMakeCodeLengths(unsigned char *len, int *freq, int alphaSize, int maxLen)
{
   int nNodes; int nHeap; int n1; int n2; int i; int j; int k;
   unsigned char tooLong;
   int heap[-1];
   int weight[-1];
   int parent[-1];
   for (i = 0; i < alphaSize; i++)
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
   while (((unsigned char)1)) {
      nNodes = alphaSize;
      nHeap = 0;
      heap[0] = 0;
      weight[0] = 0;
      parent[0] = -2;
      for (i = 1; i <= alphaSize; i++) {
         parent[i] = -1;
         nHeap++;
         heap[nHeap] = i;
         { int zz; int tmp; zz = nHeap; tmp = heap[zz]; while (weight[tmp] < weight[heap[zz >> 1]]) { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; };
      }
      { if (!(nHeap < (258 +2))) BZ2_bz__AssertH__fail ( 2001 ); };
      while (nHeap > 1) {
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; { int zz; int yy; int tmp; zz = 1; tmp = heap[zz]; while (((unsigned char)1)) { yy = zz << 1; if (yy > nHeap) break; if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) yy++; if (weight[tmp] < weight[heap[yy]]) break; heap[zz] = heap[yy]; zz = yy; } heap[zz] = tmp; };
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; { int zz; int yy; int tmp; zz = 1; tmp = heap[zz]; while (((unsigned char)1)) { yy = zz << 1; if (yy > nHeap) break; if (yy < nHeap && weight[heap[yy+1]] < weight[heap[yy]]) yy++; if (weight[tmp] < weight[heap[yy]]) break; heap[zz] = heap[yy]; zz = yy; } heap[zz] = tmp; };
         nNodes++;
         parent[n1] = parent[n2] = nNodes;
         weight[nNodes] = (((weight[n1]) & 0xffffff00)+((weight[n2]) & 0xffffff00)) | (1 + ((((weight[n1]) & 0x000000ff)) > (((weight[n2]) & 0x000000ff)) ? (((weight[n1]) & 0x000000ff)) : (((weight[n2]) & 0x000000ff))));
         parent[nNodes] = -1;
         nHeap++;
         heap[nHeap] = nNodes;
         { int zz; int tmp; zz = nHeap; tmp = heap[zz]; while (weight[tmp] < weight[heap[zz >> 1]]) { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; };
      }
      { if (!(nNodes < (258 * 2))) BZ2_bz__AssertH__fail ( 2002 ); };
      tooLong = ((unsigned char)0);
      for (i = 1; i <= alphaSize; i++) {
         j = 0;
         k = i;
         while (parent[k] >= 0) { k = parent[k]; j++; }
         len[i-1] = j;
         if (j > maxLen) tooLong = ((unsigned char)1);
      }
      if (! tooLong) break;
      for (i = 1; i <= alphaSize; i++) {
         j = weight[i] >> 8;
         j = 1 + (j / 2);
         weight[i] = j << 8;
      }
   }
}

variable_types:
len
unsigned char *

variable_types:
freq
int *

variable_types: alphaSize int

variable_types: maxLen int

variable_types: nNodes int

variable_types: nHeap int

variable_types: i int

variable_types: j int

variable_types: k int

variable_types:
tooLong
unsigned char

variable_types:
heap
int [-1]

variable_types:
weight
int [-1]

variable_types:
parent
int [-1]

variable_types: zz int

variable_types: tmp int

variable_types: yy int


original_code:
void BZ2_hbAssignCodes ( Int32 *code,
                         UChar *length,
                         Int32 minLen,
                         Int32 maxLen,
                         Int32 alphaSize )
{
   Int32 n, vec, i;
   vec = 0;
   for (n = minLen; n <= maxLen; n++) {
      for (i = 0; i < alphaSize; i++)
         if (length[i] == n) { code[i] = vec; vec++; };
      vec <<= 1;
   }
}

canonical_original_code:
void BZ2_hbAssignCodes(int *code, unsigned char *length, int minLen, int maxLen, int alphaSize)
{
   int n; int vec; int i;
   vec = 0;
   for (n = minLen; n <= maxLen; n++) {
      for (i = 0; i < alphaSize; i++)
         if (length[i] == n) { code[i] = vec; vec++; };
      vec <<= 1;
   }
}

variable_types:
code
int *

variable_types:
length
unsigned char *

variable_types: minLen int

variable_types: n int

variable_types: vec int


original_code:
void BZ2_hbCreateDecodeTables ( Int32 *limit,
                                Int32 *base,
                                Int32 *perm,
                                UChar *length,
                                Int32 minLen,
                                Int32 maxLen,
                                Int32 alphaSize )
{
   Int32 pp, i, j, vec;
   pp = 0;
   for (i = minLen; i <= maxLen; i++)
      for (j = 0; j < alphaSize; j++)
         if (length[j] == i) { perm[pp] = j; pp++; };
   for (i = 0; i < 23; i++) base[i] = 0;
   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
   for (i = 1; i < 23; i++) base[i] += base[i-1];
   for (i = 0; i < 23; i++) limit[i] = 0;
   vec = 0;
   for (i = minLen; i <= maxLen; i++) {
      vec += (base[i+1] - base[i]);
      limit[i] = vec-1;
      vec <<= 1;
   }
   for (i = minLen + 1; i <= maxLen; i++)
      base[i] = ((limit[i-1] + 1) << 1) - base[i];
}

canonical_original_code:
void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize)
{
   int pp; int i; int j; int vec;
   pp = 0;
   for (i = minLen; i <= maxLen; i++)
      for (j = 0; j < alphaSize; j++)
         if (length[j] == i) { perm[pp] = j; pp++; };
   for (i = 0; i < 23; i++) base[i] = 0;
   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
   for (i = 1; i < 23; i++) base[i] += base[i-1];
   for (i = 0; i < 23; i++) limit[i] = 0;
   vec = 0;
   for (i = minLen; i <= maxLen; i++) {
      vec += (base[i+1] - base[i]);
      limit[i] = vec-1;
      vec <<= 1;
   }
   for (i = minLen + 1; i <= maxLen; i++)
      base[i] = ((limit[i-1] + 1) << 1) - base[i];
}

variable_types:
limit
int *

variable_types:
base
int *

variable_types:
perm
int *

variable_types: maxLen int

variable_types: alphaSize int

variable_types: pp int


gzprintf

original_code:
int gzprintf (gzFile file, const char *format, ...)
{
    int size, len;
    gz_statep state;
    z_streamp strm;
    va_list va;
    if (file == ((void *)0))
        return -1;
    state = (gz_statep)file;
    strm = &(state->strm);
    if (state->mode != 31153 || state->err != 0)
        return 0;
    if (state->size == 0 && gz_init(state) == -1)
        return 0;
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            return 0;
    }
    if (strm->avail_in && gz_comp(state, 0) == -1)
        return 0;
    size = (int)(state->size);
    state->in[size - 1] = 0;
    __builtin_va_start(va,format);
    len = vsnprintf((char *)(state->in), size, format, va);
    __builtin_va_end(va);
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
        return 0;
    strm->avail_in = (unsigned)len;
    strm->next_in = state->in;
    state->x.pos += len;
    return len;
}

canonical_original_code:
int gzprintf(struct gzFile_s *file, char *format, ...)
{
    int size; int len;
    struct gz_state *state;
    struct z_stream_s *strm;
    __builtin_va_list va;
    if (file == ((void *)0))
        return -1;
    state = (struct gz_state *)file;
    strm = &(state->strm);
    if (state->mode != 31153 || state->err != 0)
        return 0;
    if (state->size == 0 && gz_init(state) == -1)
        return 0;
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            return 0;
    }
    if (strm->avail_in && gz_comp(state, 0) == -1)
        return 0;
    size = (int)(state->size);
    state->in[size - 1] = 0;
    __builtin_va_start(va,format);
    len = vsnprintf((char *)(state->in), size, format, va);
    __builtin_va_end(va);
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
        return 0;
    strm->avail_in = (unsigned int)len;
    strm->next_in = state->in;
    state->x.pos += len;
    return len;
}

variable_types: struct gzFile_s { unsigned int have; unsigned char *next; long pos; } *

variable_types:
format
char *

variable_types: size int

variable_types: len int

variable_types: struct gz_state { struct gzFile_s { unsigned int have; unsigned char *next; long pos; } x; int mode; int fd; char *path; unsigned int size; unsigned int want; unsigned char *in; unsigned char *out; int direct; int how; long start; int eof; int past; int level; int strategy; long skip; int seek; int err; char *msg; struct z_stream_s { unsigned char *next_in; unsigned int avail_in; unsigned long total_in; unsigned char *next_out; unsigned int avail_out; unsigned long total_out; char *msg; struct internal_state { int dummy; } *state; void *(*zalloc)(void *opaque, unsigned int items, unsigned int size); void (*zfree)(void *opaque, void *address); void *opaque; int data_type; unsigned long adler; unsigned long reserved; } strm; } *

variable_types: struct z_stream_s { unsigned char *next_in; unsigned int avail_in; unsigned long total_in; unsigned char *next_out; unsigned int avail_out; unsigned long total_out; char *msg; struct internal_state { int dummy; } *state; void *(*zalloc)(void *opaque, unsigned int items, unsigned int size); void (*zfree)(void *opaque, void *address); void *opaque; int data_type; unsigned long adler; unsigned long reserved; } *

user_defined_types: struct gz_state { struct gzFile_s x; int mode; int fd; char *path; unsigned int size; unsigned int want; unsigned char *in; unsigned char *out; int direct; int how; long start; int eof; int past; int level; int strategy; long skip; int seek; int err; char *msg; struct z_stream_s strm; }

user_defined_types: struct z_stream_s { unsigned char *next_in; unsigned int avail_in; unsigned long total_in; unsigned char *next_out; unsigned int avail_out; unsigned long total_out; char *msg; struct internal_state *state; void *(*zalloc)(void *opaque, unsigned int items, unsigned int size); void (*zfree)(void *opaque, void *address); void *opaque; int data_type; unsigned long adler; unsigned long reserved; }


zlibVersion

decompiled_code:
const char *zlibVersion()
{
return "1.2.7";
}

canonical_decompiled_code:
const char *func0()
{
return "1.2.7";
}

original_code:
const char * zlibVersion()
{
    return "1.2.7";
}

canonical_original_code:
char *zlibVersion()
{
    return "1.2.7";
}

return_type: char *


zlibCompileFlags

decompiled_code:
__int64 zlibCompileFlags()
{
return 169LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 169LL;
}

original_code:
uLong zlibCompileFlags()
{
    uLong flags;
    flags = 0;
    switch ((int)(sizeof(uInt))) {
    case 2: break;
    case 4: flags += 1; break;
    case 8: flags += 2; break;
    default: flags += 3;
    }
    switch ((int)(sizeof(uLong))) {
    case 2: break;
    case 4: flags += 1 << 2; break;
    case 8: flags += 2 << 2; break;
    default: flags += 3 << 2;
    }
    switch ((int)(sizeof(voidpf))) {
    case 2: break;
    case 4: flags += 1 << 4; break;
    case 8: flags += 2 << 4; break;
    default: flags += 3 << 4;
    }
    switch ((int)(sizeof(off_t))) {
    case 2: break;
    case 4: flags += 1 << 6; break;
    case 8: flags += 2 << 6; break;
    default: flags += 3 << 6;
    }
    return flags;
}

canonical_original_code:
unsigned long zlibCompileFlags()
{
    unsigned long flags;
    flags = 0;
    switch ((int)(sizeof(uInt))) {
    case 2: break;
    case 4: flags += 1; break;
    case 8: flags += 2; break;
    default: flags += 3;
    }
    switch ((int)(sizeof(uLong))) {
    case 2: break;
    case 4: flags += 1 << 2; break;
    case 8: flags += 2 << 2; break;
    default: flags += 3 << 2;
    }
    switch ((int)(sizeof(voidpf))) {
    case 2: break;
    case 4: flags += 1 << 4; break;
    case 8: flags += 2 << 4; break;
    default: flags += 3 << 4;
    }
    switch ((int)(sizeof(off_t))) {
    case 2: break;
    case 4: flags += 1 << 6; break;
    case 8: flags += 2 << 6; break;
    default: flags += 3 << 6;
    }
    return flags;
}

variable_types:
flags
unsigned long

return_type: unsigned long


repo: kebrister/lscat-blcontrols

decompiled_code:
int __fastcall service_timers()
{
__time_t v0; // r14
__int64 v1; // rax
__time_t v2; // r15
struct timespec *v3; // rbx
int v4; // er13
int v5; // er12
__syscall_slong_t v6; // rax
int v7; // edx
int v8; // edx
__syscall_slong_t v9; // rax
unsigned __int64 v10; // rsi
__time_t v11; // rax
__syscall_slong_t v13; // [rsp+0h] [rbp-78h]
__int64 v14; // [rsp+8h] [rbp-70h]
struct itimerspec value; // [rsp+10h] [rbp-68h] BYREF
struct timespec tp; // [rsp+30h] [rbp-48h] BYREF

clock_gettime(0, &tp);
v1 = ((__int64)&unk_186A0 + tp.tv_nsec) >> 63;
v2 = tp.tv_sec + ((__int64)&unk_186A0 + tp.tv_nsec) / 1000000000;
v14 = ((__int64)&unk_186A0 + tp.tv_nsec) % 1000000000;
if ( lstimer_active_timers > 0 )
{
v3 = (struct timespec *)s2;
v4 = 0;
v5 = 0;
do
{
if ( LODWORD(v3[-1].tv_sec) )
{
++v4;
v11 = v3[16].tv_sec;
if ( v2 > v11 || v2 == v11 && v14 >= v3[16].tv_nsec )
{
lsevents_send_event(v3);
v3[18] = tp;
v6 = v3[-1].tv_nsec;
v3[-1].tv_nsec = v6 + 1;
v7 = v3[-1].tv_sec;
if ( v7 == -1 || (v8 = v7 - 1, (LODWORD(v3[-1].tv_sec) = v8) != 0) )
{
v9 = v6 + 2;
v10 = v3[19].tv_nsec + v3[17].tv_nsec * v9;
v3[16].tv_sec = v10 / 0x3B9ACA00 + v3[19].tv_sec + v3[17].tv_sec * v9;
v3[16].tv_nsec = v10 % 0x3B9ACA00;
}
else
{
--lstimer_active_timers;
}
}
if ( v4 == 1 )
{
v0 = v3[16].tv_sec;
v1 = v3[16].tv_nsec;
v13 = v1;
}
else
{
v1 = v3[16].tv_sec;
if ( v1 < v0 || v1 == v0 && v3[16].tv_nsec < v13 )
{
v13 = v3[16].tv_nsec;
v0 = v3[16].tv_sec;
}
}
}
++v5;
v3 += 21;
}
while ( lstimer_active_timers > v5 );
}
if ( v0 )
{
value.it_value.tv_sec = v0;
value.it_value.tv_nsec = v13;
value.it_interval.tv_sec = 0LL;
value.it_interval.tv_nsec = 0LL;
LODWORD(v1) = timer_settime(lstimer_timerid, 1, &value, 0LL);
}
return v1;
}

canonical_decompiled_code:
int __fastcall func0()
{
__time_t v0; // r14
__int64 v1; // rax
__time_t v2; // r15
struct timespec *v3; // rbx
int v4; // er13
int v5; // er12
__syscall_slong_t v6; // rax
int v7; // edx
int v8; // edx
__syscall_slong_t v9; // rax
unsigned __int64 v10; // rsi
__time_t v11; // rax
__syscall_slong_t v13; // [rsp+0h] [rbp-78h]
__int64 v14; // [rsp+8h] [rbp-70h]
struct itimerspec value; // [rsp+10h] [rbp-68h] BYREF
struct timespec tp; // [rsp+30h] [rbp-48h] BYREF

clock_gettime(0, &tp);
v1 = ((__int64)&unk_186A0 + tp.tv_nsec) >> 63;
v2 = tp.tv_sec + ((__int64)&unk_186A0 + tp.tv_nsec) / 1000000000;
v14 = ((__int64)&unk_186A0 + tp.tv_nsec) % 1000000000;
if ( lstimer_active_timers > 0 )
{
v3 = (struct timespec *)s2;
v4 = 0;
v5 = 0;
do
{
if ( LODWORD(v3[-1].tv_sec) )
{
++v4;
v11 = v3[16].tv_sec;
if ( v2 > v11 || v2 == v11 && v14 >= v3[16].tv_nsec )
{
lsevents_send_event(v3);
v3[18] = tp;
v6 = v3[-1].tv_nsec;
v3[-1].tv_nsec = v6 + 1;
v7 = v3[-1].tv_sec;
if ( v7 == -1 || (v8 = v7 - 1, (LODWORD(v3[-1].tv_sec) = v8) != 0) )
{
v9 = v6 + 2;
v10 = v3[19].tv_nsec + v3[17].tv_nsec * v9;
v3[16].tv_sec = v10 / 0x3B9ACA00 + v3[19].tv_sec + v3[17].tv_sec * v9;
v3[16].tv_nsec = v10 % 0x3B9ACA00;
}
else
{
--lstimer_active_timers;
}
}
if ( v4 == 1 )
{
v0 = v3[16].tv_sec;
v1 = v3[16].tv_nsec;
v13 = v1;
}
else
{
v1 = v3[16].tv_sec;
if ( v1 < v0 || v1 == v0 && v3[16].tv_nsec < v13 )
{
v13 = v3[16].tv_nsec;
v0 = v3[16].tv_sec;
}
}
}
++v5;
v3 += 21;
}
while ( lstimer_active_timers > v5 );
}
if ( v0 )
{
value.it_value.tv_sec = v0;
value.it_value.tv_nsec = v13;
value.it_interval.tv_sec = 0LL;
value.it_interval.tv_nsec = 0LL;
LODWORD(v1) = timer_settime(lstimer_timerid, 1, &value, 0LL);
}
return v1;
}

original_code:
static void service_timers() {
  int
    i,
    found_active;
  lstimer_list_t *p;
  struct timespec now, then, soonest;
  struct itimerspec its;
  clock_gettime( 0, &now);
  then.tv_sec = now.tv_sec + (now.tv_nsec + 100000) / 1000000000;
  then.tv_nsec = (now.tv_nsec + 100000) % 1000000000;
  found_active = 0;
  for( i=0; i<lstimer_active_timers; i++) {
    p = &(lstimer_list[i]);
    if( p->shots != 0) {
      found_active++;
      if( p->next_secs < then.tv_sec || (p->next_secs == then.tv_sec && p->next_nsecs <= then.tv_nsec)) {
 lsevents_send_event( p->event);
 p->last_secs = now.tv_sec;
 p->last_nsecs = now.tv_nsec;
 p->ncalls++;
 if( p->shots != -1)
   p->shots--;
 if( p->shots == 0) {
   lstimer_active_timers--;
 } else {
   p->next_secs = p->init_secs + (p->ncalls+1) * p->delay_secs + (p->init_nsecs + (p->ncalls+1)*p->delay_nsecs)/1000000000;
   p->next_nsecs = (p->init_nsecs + (p->ncalls+1)*p->delay_nsecs) % 1000000000;
 }
      }
      if( found_active == 1) {
 soonest.tv_sec = p->next_secs;
 soonest.tv_nsec = p->next_nsecs;
      } else {
 if( soonest.tv_sec > p->next_secs || (soonest.tv_sec == p->next_secs && soonest.tv_nsec > p->next_nsecs)) {
   soonest.tv_sec = p->next_secs;
   soonest.tv_nsec = p->next_nsecs;
 }
      }
    }
  }
  if( soonest.tv_sec != 0) {
    its.it_value.tv_sec = soonest.tv_sec;
    its.it_value.tv_nsec = soonest.tv_nsec;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    timer_settime( lstimer_timerid, 1, &its, ((void *)0));
  }
}

canonical_original_code:
static void service_timers() {
  int i; int found_active;
  struct lstimer_list_struct *p;
  struct timespec now; struct timespec then; struct timespec soonest;
  struct itimerspec its;
  clock_gettime( 0, &now);
  then.tv_sec = now.tv_sec + (now.tv_nsec + 100000) / 1000000000;
  then.tv_nsec = (now.tv_nsec + 100000) % 1000000000;
  found_active = 0;
  for( i=0; i<lstimer_active_timers; i++) {
    p = &(lstimer_list[i]);
    if( p->shots != 0) {
      found_active++;
      if( p->next_secs < then.tv_sec || (p->next_secs == then.tv_sec && p->next_nsecs <= then.tv_nsec)) {
 lsevents_send_event( p->event);
 p->last_secs = now.tv_sec;
 p->last_nsecs = now.tv_nsec;
 p->ncalls++;
 if( p->shots != -1)
   p->shots--;
 if( p->shots == 0) {
   lstimer_active_timers--;
 } else {
   p->next_secs = p->init_secs + (p->ncalls+1) * p->delay_secs + (p->init_nsecs + (p->ncalls+1)*p->delay_nsecs)/1000000000;
   p->next_nsecs = (p->init_nsecs + (p->ncalls+1)*p->delay_nsecs) % 1000000000;
 }
      }
      if( found_active == 1) {
 soonest.tv_sec = p->next_secs;
 soonest.tv_nsec = p->next_nsecs;
      } else {
 if( soonest.tv_sec > p->next_secs || (soonest.tv_sec == p->next_secs && soonest.tv_nsec > p->next_nsecs)) {
   soonest.tv_sec = p->next_secs;
   soonest.tv_nsec = p->next_nsecs;
 }
      }
    }
  }
  if( soonest.tv_sec != 0) {
    its.it_value.tv_sec = soonest.tv_sec;
    its.it_value.tv_nsec = soonest.tv_nsec;
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    timer_settime( lstimer_timerid, 1, &its, ((void *)0));
  }
}

variable_types: i int

variable_types: struct lstimer_list_struct { int shots; unsigned long ncalls; char event[256]; long next_secs; long next_nsecs; long delay_secs; long delay_nsecs; long last_secs; long last_nsecs; long init_secs; long init_nsecs; } *

variable_types: struct itimerspec { struct timespec { long tv_sec; long tv_nsec; } it_interval; struct timespec it_value; }

user_defined_types: struct itimerspec { struct timespec it_interval; struct timespec it_value; }


decompiled_code:
void __fastcall __noreturn lstimer_worker(void *a1)
{
int v1; // eax
int v2; // eax
sigset_t v3; // [rsp+0h] [rbp-168h] BYREF
struct sigaction v4; // [rsp+80h] [rbp-E8h] BYREF
struct sigevent evp; // [rsp+120h] [rbp-48h] BYREF

v4.sa_flags = 4;
v4.sa_handler = (__sighandler_t)handler;
sigemptyset(&v4.sa_mask);
v1 = _libc_current_sigrtmin();
if ( sigaction(v1, &v4, 0LL) != -1 )
{
evp.sigev_notify = 0;
evp.sigev_signo = _libc_current_sigrtmin();
evp.sigev_value.sival_ptr = &lstimer_timerid;
timer_create(0, &evp, &lstimer_timerid);
sigemptyset(&v3);
v2 = _libc_current_sigrtmin();
sigaddset(&v3, v2);
while ( 1 )
{
pthread_mutex_lock(&lstimer_mutex);
while ( !new_timer )
pthread_cond_wait(&lstimer_cond, &lstimer_mutex);
sigprocmask(2, &v3, 0LL);
service_timers();
new_timer = 0;
pthread_mutex_unlock(&lstimer_mutex);
sigprocmask(1, &v3, 0LL);
}
}
lslogging_log_message("lstimer_worker: sigaction failed");
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
int v1; // eax
int v2; // eax
sigset_t v3; // [rsp+0h] [rbp-168h] BYREF
struct sigaction v4; // [rsp+80h] [rbp-E8h] BYREF
struct sigevent evp; // [rsp+120h] [rbp-48h] BYREF

v4.sa_flags = 4;
v4.sa_handler = (__sighandler_t)handler;
sigemptyset(&v4.sa_mask);
v1 = _libc_current_sigrtmin();
if ( sigaction(v1, &v4, 0LL) != -1 )
{
evp.sigev_notify = 0;
evp.sigev_signo = _libc_current_sigrtmin();
evp.sigev_value.sival_ptr = &lstimer_timerid;
timer_create(0, &evp, &lstimer_timerid);
sigemptyset(&v3);
v2 = _libc_current_sigrtmin();
sigaddset(&v3, v2);
while ( 1 )
{
pthread_mutex_lock(&lstimer_mutex);
while ( !new_timer )
pthread_cond_wait(&lstimer_cond, &lstimer_mutex);
sigprocmask(2, &v3, 0LL);
func0();
new_timer = 0;
pthread_mutex_unlock(&lstimer_mutex);
sigprocmask(1, &v3, 0LL);
}
}
lslogging_log_message("lstimer_worker: sigaction failed");
exit(-1);
}

original_code:
static void *lstimer_worker(
       void *dummy
       ) {
  struct sigevent sev;
  struct sigaction sa;
  sigset_t mask;
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = handler;
  sigemptyset(&sa.sa_mask);
  if (sigaction((__libc_current_sigrtmin ()), &sa, ((void *)0)) == -1) {
    lslogging_log_message( "lstimer_worker: sigaction failed");
    exit( -1);
  }
  sev.sigev_notify = SIGEV_SIGNAL;
  sev.sigev_signo = (__libc_current_sigrtmin ());
  sev.sigev_value.sival_ptr = &lstimer_timerid;
  timer_create( 0, &sev, &lstimer_timerid);
  sigemptyset( &mask);
  sigaddset( &mask, (__libc_current_sigrtmin ()));
  while( 1) {
    pthread_mutex_lock( &lstimer_mutex);
    while( new_timer == 0)
      pthread_cond_wait( &lstimer_cond, &lstimer_mutex);
    sigprocmask( 2, &mask, ((void *)0));
    service_timers();
    new_timer = 0;
    pthread_mutex_unlock( &lstimer_mutex);
    sigprocmask( 1, &mask, ((void *)0));
  }
}

canonical_original_code:
static void *lstimer_worker(void *dummy) {
  struct sigevent sev;
  struct sigaction sa;
  struct __sigset_t mask;
  sa.sa_flags = 4;
  sa.__sigaction_handler.sa_sigaction = handler;
  sigemptyset(&sa.sa_mask);
  if (sigaction((__libc_current_sigrtmin ()), &sa, ((void *)0)) == -1) {
    lslogging_log_message( "lstimer_worker: sigaction failed");
    exit( -1);
  }
  sev.sigev_notify = SIGEV_SIGNAL;
  sev.sigev_signo = (__libc_current_sigrtmin ());
  sev.sigev_value.sival_ptr = &lstimer_timerid;
  timer_create( 0, &sev, &lstimer_timerid);
  sigemptyset( &mask);
  sigaddset( &mask, (__libc_current_sigrtmin ()));
  while( 1) {
    pthread_mutex_lock( &lstimer_mutex);
    while( new_timer == 0)
      pthread_cond_wait( &lstimer_cond, &lstimer_mutex);
    sigprocmask( 2, &mask, ((void *)0));
    service_timers();
    new_timer = 0;
    pthread_mutex_unlock( &lstimer_mutex);
    sigprocmask( 1, &mask, ((void *)0));
  }
}

variable_types:
dummy
void *

variable_types: struct sigevent { union sigval { int sival_int; void *sival_ptr; } sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t { char __size[56]; long __align; } *_attribute; } _sigev_thread; } _sigev_un; }

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

return_type: void *

user_defined_types: struct sigevent { union sigval sigev_value; int sigev_signo; int sigev_notify; union { int _pad[-1]; int _tid; struct { void (*_function)(union sigval); union pthread_attr_t *_attribute; } _sigev_thread; } _sigev_un; }

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


handler

decompiled_code:
int handler()
{
pthread_mutex_lock(&lstimer_mutex);
service_timers();
return pthread_mutex_unlock(&lstimer_mutex);
}

canonical_decompiled_code:
int func2()
{
pthread_mutex_lock(&lstimer_mutex);
func0();
return pthread_mutex_unlock(&lstimer_mutex);
}

original_code:
static void handler( int sig, siginfo_t *si, void *dummy) {
  pthread_mutex_lock( &lstimer_mutex);
  service_timers();
  pthread_mutex_unlock( &lstimer_mutex);
}

canonical_original_code:
static void handler(int sig, struct siginfo_t *si, void *dummy) {
  pthread_mutex_lock( &lstimer_mutex);
  service_timers();
  pthread_mutex_unlock( &lstimer_mutex);
}

variable_types: sig int

variable_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 { 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; } *


decompiled_code:
int __fastcall lstimer_unset_timer(char *s1)
{
const char *v1; // rbx
int result; // eax

v1 = s2;
do
{
result = strcmp(s1, v1);
if ( !result )
*((_DWORD *)v1 - 4) = 0;
v1 += 336;
}
while ( v1 != "worker: sigaction failed" );
return result;
}

canonical_decompiled_code:
int __fastcall func3(char *s1)
{
const char *v1; // rbx
int result; // eax

v1 = s2;
do
{
result = strcmp(s1, v1);
if ( !result )
*((_DWORD *)v1 - 4) = 0;
v1 += 336;
}
while ( v1 != "worker: sigaction failed" );
return result;
}

original_code:
void lstimer_unset_timer( char *event) {
  int i;
  for( i=0; i<1024; i++) {
    if( strcmp( event, lstimer_list[i].event) == 0) {
      lstimer_list[i].shots = 0;
    }
  }
}

canonical_original_code:
void lstimer_unset_timer(char *event) {
  int i;
  for( i=0; i<1024; i++) {
    if( strcmp( event, lstimer_list[i].event) == 0) {
      lstimer_list[i].shots = 0;
    }
  }
}

variable_types:
event
char *


decompiled_code:
int __fastcall lstimer_set_timer(char *src, unsigned int a2, __int64 a3, __int64 a4)
{
int *v7; // rdx
int v8; // eax
__int64 v10; // rbx
__int64 v11; // r15
__time_t v12; // rdi
__syscall_slong_t v13; // rsi
unsigned __int64 v14; // rbp
__int64 v15; // rcx
struct timespec v16; // [rsp+0h] [rbp-48h] BYREF

clock_gettime(0, &v16);
lsevents_preregister_event(src);
pthread_mutex_lock(&lstimer_mutex);
v7 = lstimer_list;
v8 = 0;
while ( *v7 )
{
++v8;
v7 += 84;
if ( v8 == 1024 )
{
pthread_mutex_unlock(&lstimer_mutex);
return lslogging_log_message(
"lstimer_set_timer: out of timers for event: %s, shots: %d, secs: %u, nsecs: %u",
src,
a2,
a3,
a4);
}
}
v10 = v8;
v11 = 20LL * v8;
strncpy(&s2[336 * v8], src, 0xFFuLL);
byte_74F[16 * v11 + 16 * v10] = 0;
lstimer_list[4 * v11 + 4 * v10] = a2;
qword_760[2 * v11 + 2 * v10] = a3;
qword_768[2 * v11 + 2 * v10] = a4;
v12 = v16.tv_sec;
v13 = v16.tv_nsec;
v14 = v16.tv_nsec + a4;
v15 = 2 * (v11 + v10);
qword_750[v15] = v14 / 0x3B9ACA00 + v16.tv_sec + a3;
qword_758[v15] = v14 % 0x3B9ACA00;
qword_770[v15] = 0LL;
qword_778[v15] = 0LL;
qword_648[v15] = 0LL;
qword_780[v15] = v12;
qword_788[v15] = v13;
if ( a2 )
{
++lstimer_active_timers;
++new_timer;
}
pthread_cond_signal(&lstimer_cond);
return pthread_mutex_unlock(&lstimer_mutex);
}

canonical_decompiled_code:
int __fastcall func4(char *src, unsigned int a2, __int64 a3, __int64 a4)
{
int *v7; // rdx
int v8; // eax
__int64 v10; // rbx
__int64 v11; // r15
__time_t v12; // rdi
__syscall_slong_t v13; // rsi
unsigned __int64 v14; // rbp
__int64 v15; // rcx
struct timespec v16; // [rsp+0h] [rbp-48h] BYREF

clock_gettime(0, &v16);
lsevents_preregister_event(src);
pthread_mutex_lock(&lstimer_mutex);
v7 = lstimer_list;
v8 = 0;
while ( *v7 )
{
++v8;
v7 += 84;
if ( v8 == 1024 )
{
pthread_mutex_unlock(&lstimer_mutex);
return lslogging_log_message(
"lstimer_set_timer: out of timers for event: %s, shots: %d, secs: %u, nsecs: %u",
src,
a2,
a3,
a4);
}
}
v10 = v8;
v11 = 20LL * v8;
strncpy(&s2[336 * v8], src, 0xFFuLL);
byte_74F[16 * v11 + 16 * v10] = 0;
lstimer_list[4 * v11 + 4 * v10] = a2;
qword_760[2 * v11 + 2 * v10] = a3;
qword_768[2 * v11 + 2 * v10] = a4;
v12 = v16.tv_sec;
v13 = v16.tv_nsec;
v14 = v16.tv_nsec + a4;
v15 = 2 * (v11 + v10);
qword_750[v15] = v14 / 0x3B9ACA00 + v16.tv_sec + a3;
qword_758[v15] = v14 % 0x3B9ACA00;
qword_770[v15] = 0LL;
qword_778[v15] = 0LL;
qword_648[v15] = 0LL;
qword_780[v15] = v12;
qword_788[v15] = v13;
if ( a2 )
{
++lstimer_active_timers;
++new_timer;
}
pthread_cond_signal(&lstimer_cond);
return pthread_mutex_unlock(&lstimer_mutex);
}

original_code:
void lstimer_set_timer( char *event, int shots, unsigned long int secs, unsigned long int nsecs) {
  int i;
  struct timespec now;
  clock_gettime( 0, &now);
  lsevents_preregister_event( event);
  pthread_mutex_lock( &lstimer_mutex);
  for( i=0; i<1024; i++) {
    if( lstimer_list[i].shots == 0)
      break;
  }
  if( i == 1024) {
    pthread_mutex_unlock( &lstimer_mutex);
    lslogging_log_message( "lstimer_set_timer: out of timers for event: %s, shots: %d,  secs: %u, nsecs: %u",
     event, shots, secs, nsecs);
    return;
  }
  strncpy( lstimer_list[i].event, event, 256 - 1);
  lstimer_list[i].event[256 - 1] = 0;
  lstimer_list[i].shots = shots;
  lstimer_list[i].delay_secs = secs;
  lstimer_list[i].delay_nsecs = nsecs;
  lstimer_list[i].next_secs = secs + now.tv_sec + (now.tv_nsec + nsecs) / 1000000000;
  lstimer_list[i].next_nsecs = (now.tv_nsec + nsecs) % 1000000000;
  lstimer_list[i].last_secs = 0;
  lstimer_list[i].last_nsecs = 0;
  lstimer_list[i].ncalls = 0;
  lstimer_list[i].init_secs = now.tv_sec;
  lstimer_list[i].init_nsecs = now.tv_nsec;
  if( shots != 0) {
    lstimer_active_timers++;
    new_timer++;
  }
  pthread_cond_signal( &lstimer_cond);
  pthread_mutex_unlock( &lstimer_mutex);
}

canonical_original_code:
void lstimer_set_timer(char *event, int shots, unsigned long secs, unsigned long nsecs) {
  int i;
  struct timespec now;
  clock_gettime( 0, &now);
  lsevents_preregister_event( event);
  pthread_mutex_lock( &lstimer_mutex);
  for( i=0; i<1024; i++) {
    if( lstimer_list[i].shots == 0)
      break;
  }
  if( i == 1024) {
    pthread_mutex_unlock( &lstimer_mutex);
    lslogging_log_message( "lstimer_set_timer: out of timers for event: %s, shots: %d,  secs: %u, nsecs: %u",
     event, shots, secs, nsecs);
    return;
  }
  strncpy( lstimer_list[i].event, event, 256 - 1);
  lstimer_list[i].event[256 - 1] = 0;
  lstimer_list[i].shots = shots;
  lstimer_list[i].delay_secs = secs;
  lstimer_list[i].delay_nsecs = nsecs;
  lstimer_list[i].next_secs = secs + now.tv_sec + (now.tv_nsec + nsecs) / 1000000000;
  lstimer_list[i].next_nsecs = (now.tv_nsec + nsecs) % 1000000000;
  lstimer_list[i].last_secs = 0;
  lstimer_list[i].last_nsecs = 0;
  lstimer_list[i].ncalls = 0;
  lstimer_list[i].init_secs = now.tv_sec;
  lstimer_list[i].init_nsecs = now.tv_nsec;
  if( shots != 0) {
    lstimer_active_timers++;
    new_timer++;
  }
  pthread_cond_signal( &lstimer_cond);
  pthread_mutex_unlock( &lstimer_mutex);
}

variable_types: shots int

variable_types:
secs
unsigned long

variable_types:
nsecs
unsigned long

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


decompiled_code:
int lstimer_init()
{
int *v0; // rax

v0 = lstimer_list;
do
{
*v0 = 0;
v0 += 84;
}
while ( v0 != &lstimer_active_timers );
pthread_mutex_init(&lstimer_mutex, 0LL);
return pthread_cond_init(&lstimer_cond, 0LL);
}

canonical_decompiled_code:
int func5()
{
int *v0; // rax

v0 = lstimer_list;
do
{
*v0 = 0;
v0 += 84;
}
while ( v0 != &lstimer_active_timers );
pthread_mutex_init(&lstimer_mutex, 0LL);
return pthread_cond_init(&lstimer_cond, 0LL);
}

original_code:
void lstimer_init() {
  int i;
  for( i=0; i<1024; i++) {
    lstimer_list[i].shots = 0;
  }
  pthread_mutex_init( &lstimer_mutex, ((void *)0));
  pthread_cond_init( &lstimer_cond, ((void *)0));
}


decompiled_code:
int lstimer_run()
{
return pthread_create(&lstimer_thread, 0LL, (void *(*)(void *))lstimer_worker, 0LL);
}

canonical_decompiled_code:
int func6()
{
return pthread_create(&lstimer_thread, 0LL, (void *(*)(void *))lstimer_worker, 0LL);
}

original_code:
void lstimer_run() {
  pthread_create( &lstimer_thread, ((void *)0), lstimer_worker, ((void *)0));
}


decompiled_code:
__int64 lstest_lspmac_est_move_time()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
__int64 result; // rax
unsigned int v9; // [rsp+4h] [rbp-14h] BYREF
__int64 v10[2]; // [rsp+8h] [rbp-10h] BYREF

v9 = 0;
v0 = lspmac_est_move_time(v10, &v9, omega, 0LL, 0LL, 0LL, 360.0);
lslogging_log_message("lstest_lspmac_est_move_time: omega 360 move_time=%f err=%d", v0, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v1 = lspmac_est_move_time(v10, &v9, aperz, 0LL, "Cover", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: aperz Cover move_time=%f err=%d", v1, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v2 = lspmac_est_move_time(v10, &v9, aperz, 0LL, "In", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: aperz In move_time=%f err=%d", v2, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v3 = lspmac_est_move_time(v10, &v9, capz, 0LL, "Cover", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: capz Cover move_time=%f err=%d", v3, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v4 = lspmac_est_move_time(v10, &v9, capz, 0LL, "In", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: capz In move_time=%f err=%d", v4, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v5 = lspmac_est_move_time(v10, &v9, apery, 0LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator move_time=%f err=%d",
v5,
*(double *)v10,
0.0,
0.0,
0.0);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v6 = lspmac_est_move_time(v10, &v9, apery, 0LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery Cover aperz Cover capy Cover capz Cover scint Cover move_time=%f err=%d",
v6,
*(double *)v10,
0.0,
0.0,
0.0);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v7 = lspmac_est_move_time(v10, &v9, apery, 1LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator omega manualMount kappa Manualmoun"
"t move_time=%f err=%d",
v7,
*(double *)v10,
0.0,
0.0,
0.0);
result = lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0);
if ( (_DWORD)result )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
__int64 result; // rax
unsigned int v9; // [rsp+4h] [rbp-14h] BYREF
__int64 v10[2]; // [rsp+8h] [rbp-10h] BYREF

v9 = 0;
v0 = lspmac_est_move_time(v10, &v9, omega, 0LL, 0LL, 0LL, 360.0);
lslogging_log_message("lstest_lspmac_est_move_time: omega 360 move_time=%f err=%d", v0, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v1 = lspmac_est_move_time(v10, &v9, aperz, 0LL, "Cover", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: aperz Cover move_time=%f err=%d", v1, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v2 = lspmac_est_move_time(v10, &v9, aperz, 0LL, "In", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: aperz In move_time=%f err=%d", v2, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v3 = lspmac_est_move_time(v10, &v9, capz, 0LL, "Cover", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: capz Cover move_time=%f err=%d", v3, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v4 = lspmac_est_move_time(v10, &v9, capz, 0LL, "In", 0LL, 0.0);
lslogging_log_message("lstest_lspmac_est_move_time: capz In move_time=%f err=%d", v4, *(double *)v10);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v5 = lspmac_est_move_time(v10, &v9, apery, 0LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator move_time=%f err=%d",
v5,
*(double *)v10,
0.0,
0.0,
0.0);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v6 = lspmac_est_move_time(v10, &v9, apery, 0LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery Cover aperz Cover capy Cover capz Cover scint Cover move_time=%f err=%d",
v6,
*(double *)v10,
0.0,
0.0,
0.0);
if ( (unsigned int)lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0) )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
v7 = lspmac_est_move_time(v10, &v9, apery, 1LL, "In", aperz, 0.0);
lslogging_log_message(
"lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator omega manualMount kappa Manualmoun"
"t move_time=%f err=%d",
v7,
*(double *)v10,
0.0,
0.0,
0.0);
result = lspmac_est_move_time_wait(v9, 0LL, *(double *)v10 + 2.0);
if ( (_DWORD)result )
return lslogging_log_message("lstest_lspmac_est_move_time: timed out");
return result;
}

original_code:
void lstest_lspmac_est_move_time() {
  int err;
  double move_time;
  double fudge;
  int mmask;
  fudge = 2.0;
  mmask = 0;
  err = lspmac_est_move_time( &move_time, &mmask, omega, 0, ((void *)0), 360., ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: omega 360  move_time=%f  err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, aperz, 0, "Cover", 0., ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: aperz Cover move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, aperz, 0, "In", 0., ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: aperz In    move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, capz, 0, "Cover", 0., ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: capz Cover  move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, capz, 0, "In", 0., ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: capz In     move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, apery, 0, "In", 0.0, aperz, 0, "In", 0.0, capy, 0, "In", 0.0, capz, 0, "In", 0.0, scint, 0, "Scintillator", 0.0, ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, apery, 0, "In", 0.0, aperz, 0, "Cover", 0.0, capy, 0, "In", 0.0, capz, 0, "Cover", 0.0, scint, 0, "Cover", 0.0, ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: apery Cover aperz Cover capy Cover capz Cover scint Cover move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
  err = lspmac_est_move_time( &move_time, &mmask, apery, 1, "In", 0.0, aperz, 1, "In", 0.0, capy, 1, "In", 0.0, capz, 1, "In", 0.0, scint, 1, "Scintillator", 0.0,
         omega, 0, "manualMount", 0.0, kappa, 0, "manualMount", 0.0, ((void *)0));
  lslogging_log_message( "lstest_lspmac_est_move_time: apery In aperz In capy In capz In scint Scintillator omega manualMount kappa Manualmount move_time=%f err=%d", move_time, err);
  if( lspmac_est_move_time_wait( move_time + fudge, mmask, ((void *)0))) {
    lslogging_log_message( "lstest_lspmac_est_move_time: timed out");
    return;
  }
}

variable_types: err int

variable_types: fudge double

variable_types: mmask int


decompiled_code:
__int64 lstest_main()
{
return lstest_lspmac_est_move_time();
}

canonical_decompiled_code:
__int64 func1()
{
return func0();
}

original_code:
void lstest_main() {
  lstest_lspmac_est_move_time();
}


decompiled_code:
__int64 __fastcall verif_key(__int64 a1)
{
__int64 v2; // r12
__int64 v3; // rdi
unsigned int v4; // ebp
__int64 v5; // rbx

v2 = recup_term();
v3 = **(_QWORD **)(v2 + 128);
v4 = 0;
v5 = 0LL;
if ( !v3 )
return (unsigned int)-1;
while ( (unsigned int)ft_strcmp(v3, a1) )
{
++v4;
v5 += 8LL;
v3 = *(_QWORD *)(*(_QWORD *)(v2 + 128) + v5);
if ( !v3 )
return (unsigned int)-1;
}
if ( !*(_QWORD *)(*(_QWORD *)(v2 + 128) + v5) )
v4 = -1;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v2; // r12
__int64 v3; // rdi
unsigned int v4; // ebp
__int64 v5; // rbx

v2 = recup_term();
v3 = **(_QWORD **)(v2 + 128);
v4 = 0;
v5 = 0LL;
if ( !v3 )
return (unsigned int)-1;
while ( (unsigned int)ft_strcmp(v3, a1) )
{
++v4;
v5 += 8LL;
v3 = *(_QWORD *)(*(_QWORD *)(v2 + 128) + v5);
if ( !v3 )
return (unsigned int)-1;
}
if ( !*(_QWORD *)(*(_QWORD *)(v2 + 128) + v5) )
v4 = -1;
return v4;
}

original_code:
int verif_key(char *buff)
{
 int i;
 t_term *term;
 i = 0;
 term = recup_term();
 while (term->key[i] && ft_strcmp(term->key[i], buff))
  i++;
 if (term->key[i])
  return (i);
 return (-1);
}

canonical_original_code:
int verif_key(char *buff)
{
 int i;
 struct s_term *term;
 i = 0;
 term = recup_term();
 while (term->key[i] && ft_strcmp(term->key[i], buff))
  i++;
 if (term->key[i])
  return (i);
 return (-1);
}

variable_types:
buff
char *

variable_types: struct s_term { struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; } term; struct termios term_ori; int fd; int ps1_len; char **key; int quote; int dquote; int wait_fermeture; struct s_hist { char value[4096]; int pose_cursor; int len; int modif; char buff[4096]; int exec; struct s_hist *next; struct s_hist *prev; } *hist; } *

user_defined_types: struct s_term { struct termios term; struct termios term_ori; int fd; int ps1_len; char **key; int quote; int dquote; int wait_fermeture; struct s_hist *hist; }


original_code:
void ft_add_char(char *buff, t_term *term)
{
 if (buff[0] >= 32 && buff[0] <= 126 && buff[1] == 0)
 {
  if (term->hist->len == term->hist->pose_cursor)
  {
   term->hist->buff[term->hist->pose_cursor] = buff[0];
   term->hist->buff[term->hist->pose_cursor + 1] = '\0';
   term->hist->len++;
   term->hist->pose_cursor++;
   if (term->hist->next == ((void *)0))
    ft_strcpy(term->hist->value, term->hist->buff);
   ft_putstr(buff);
  }
  else
   put_stradd(term, buff);
 }
}

canonical_original_code:
void ft_add_char(char *buff, struct s_term *term)
{
 if (buff[0] >= 32 && buff[0] <= 126 && buff[1] == 0)
 {
  if (term->hist->len == term->hist->pose_cursor)
  {
   term->hist->buff[term->hist->pose_cursor] = buff[0];
   term->hist->buff[term->hist->pose_cursor + 1] = '\0';
   term->hist->len++;
   term->hist->pose_cursor++;
   if (term->hist->next == ((void *)0))
    ft_strcpy(term->hist->value, term->hist->buff);
   ft_putstr(buff);
  }
  else
   put_stradd(term, buff);
 }
}

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

user_defined_types: struct s_hist { char value[4096]; int pose_cursor; int len; int modif; char buff[4096]; int exec; struct s_hist *next; struct s_hist *prev; }


decompiled_code:
__int64 __fastcall ft_break_read(_DWORD *a1)
{
ft_putchar(10LL);
if ( a1[34] == 1 )
{
if ( a1[35] != 1 )
{
ft_putstr("quote>");
goto LABEL_5;
}
}
else if ( a1[35] != 1 )
{
a1[36] = 0;
return 0LL;
}
ft_putstr("dquote>");
LABEL_5:
ft_add_char("\n", (__int64)a1);
a1[36] = 1;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1)
{
ft_putchar(10LL);
if ( a1[34] == 1 )
{
if ( a1[35] != 1 )
{
ft_putstr("quote>");
goto LABEL_5;
}
}
else if ( a1[35] != 1 )
{
a1[36] = 0;
return 0LL;
}
ft_putstr("dquote>");
LABEL_5:
func1("\n", (__int64)a1);
a1[36] = 1;
return 1LL;
}

original_code:
int ft_break_read(t_term *term)
{
 ft_putchar('\n');
 if (term->quote == 1 || term->dquote == 1)
 {
  if (term->dquote == 1)
   ft_putstr("dquote>");
  else if (term->quote == 1)
   ft_putstr("quote>");
  ft_add_char("\n", term);
  term->wait_fermeture = 1;
  return (1);
 }
 term->wait_fermeture = 0;
 return (0);
}

canonical_original_code:
int ft_break_read(struct s_term *term)
{
 ft_putchar('\n');
 if (term->quote == 1 || term->dquote == 1)
 {
  if (term->dquote == 1)
   ft_putstr("dquote>");
  else if (term->quote == 1)
   ft_putstr("quote>");
  ft_add_char("\n", term);
  term->wait_fermeture = 1;
  return (1);
 }
 term->wait_fermeture = 0;
 return (0);
}


decompiled_code:
__int64 __fastcall ft_stomp(unsigned __int8 *a1, _DWORD *a2)
{
int v2; // eax
__int64 result; // rax

if ( *a1 == 10 )
return ft_break_read(a2);
v2 = verif_key((__int64)a1);
if ( v2 == -1 )
{
if ( *a1 == 39 )
press_quote(a2);
if ( *a1 == 34 )
press_dquote(a2);
ft_add_char(a1, (__int64)a2);
result = 1LL;
}
else
{
(*(&act + v2))(a2);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, _DWORD *a2)
{
int v2; // eax
__int64 result; // rax

if ( *a1 == 10 )
return func2(a2);
v2 = func0((__int64)a1);
if ( v2 == -1 )
{
if ( *a1 == 39 )
press_quote(a2);
if ( *a1 == 34 )
press_dquote(a2);
func1(a1, (__int64)a2);
result = 1LL;
}
else
{
(*(&act + v2))(a2);
result = 1LL;
}
return result;
}

original_code:
int ft_stomp(char *buff, t_term *term)
{
 int i;
 i = 0;
 if (buff[0] == '\n')
 {
  return (ft_break_read(term));
 }
 else if ((i = verif_key(buff)) != -1)
 {
  act[i](term);
  return (1);
 }
 else
 {
  if (buff[0] == 39)
   press_quote(term);
  if (buff[0] == 34)
   press_dquote(term);
  ft_add_char(buff, term);
 }
 return (1);
}

canonical_original_code:
int ft_stomp(char *buff, struct s_term *term)
{
 int i;
 i = 0;
 if (buff[0] == '\n')
 {
  return (ft_break_read(term));
 }
 else if ((i = verif_key(buff)) != -1)
 {
  act[i](term);
  return (1);
 }
 else
 {
  if (buff[0] == 39)
   press_quote(term);
  if (buff[0] == 34)
   press_dquote(term);
  ft_add_char(buff, term);
 }
 return (1);
}

variable_types: i int


decompiled_code:
int __fastcall ft_close_pipe(_DWORD *a1)
{
int v2; // edi
int v3; // edi
int v4; // edi
int result; // eax

v2 = a1[2];
if ( v2 )
result = close(v2);
v3 = a1[4];
if ( v3 != 2 )
result = close(v3);
v4 = a1[3];
if ( v4 != 1 )
result = close(v4);
return result;
}

canonical_decompiled_code:
int __fastcall func0(_DWORD *a1)
{
int v2; // edi
int v3; // edi
int v4; // edi
int result; // eax

v2 = a1[2];
if ( v2 )
result = close(v2);
v3 = a1[4];
if ( v3 != 2 )
result = close(v3);
v4 = a1[3];
if ( v4 != 1 )
result = close(v4);
return result;
}

original_code:
void ft_close_pipe(t_process *p)
{
 if (0 != p->fd[0])
  close(p->fd[0]);
 if (2 != p->fd[2])
  close(p->fd[2]);
 if (1 != p->fd[1])
  close(p->fd[1]);
}

canonical_original_code:
void ft_close_pipe(struct s_process *p)
{
 if (0 != p->fd[0])
  close(p->fd[0]);
 if (2 != p->fd[2])
  close(p->fd[2]);
 if (1 != p->fd[1])
  close(p->fd[1]);
}

variable_types: struct s_process { char **av; int fd[3]; struct s_process *next; struct s_process *prev; } *