main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebx
unsigned int v5; // ebp
char *v6; // rax
__int16 v7; // ax
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v10; // [rsp+10h] [rbp-48h] BYREF
int optval; // [rsp+2Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

v3 = socket(2, 1, 0);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)addr.sa_data = _bswap_16(1337);
if ( argc > 1 )
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
if ( argc > 2 )
{
v7 = atoi(argv[2]);
*(_WORD *)addr.sa_data = _bswap_16(v7);
}
optval = 1;
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
result = 1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
perror("bind");
result = 1;
}
else if ( listen(v3, 128) == -1 )
{
perror("listen");
result = 1;
}
else
{
while ( 1 )
{
addr_len = 16;
v4 = accept(v3, &v10, &addr_len);
if ( v4 < 0 )
break;
v5 = fork();
if ( !v5 )
{
close(v3);
return handle_connection(v4);
}
v6 = inet_ntoa(*(struct in_addr *)&v10.sa_data[2]);
printf("connection from %s spawned process %d\n", v6, v5);
close(v4);
}
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er12
int v4; // ebx
unsigned int v5; // ebp
char *v6; // rax
__int16 v7; // ax
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v10; // [rsp+10h] [rbp-48h] BYREF
int optval; // [rsp+2Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

v3 = socket(2, 1, 0);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)addr.sa_data = func7(1337);
if ( argc > 1 )
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
if ( argc > 2 )
{
v7 = atoi(argv[2]);
*(_WORD *)addr.sa_data = func7(v7);
}
optval = 1;
if ( setsockopt(v3, 1, 2, &optval, 4u) == -1 )
{
perror("setsockopt");
result = 1;
}
else if ( bind(v3, &addr, 0x10u) == -1 )
{
perror("bind");
result = 1;
}
else if ( listen(v3, 128) == -1 )
{
perror("listen");
result = 1;
}
else
{
while ( 1 )
{
addr_len = 16;
v4 = accept(v3, &v10, &addr_len);
if ( v4 < 0 )
break;
v5 = fork();
if ( !v5 )
{
close(v3);
return func10(v4);
}
v6 = inet_ntoa(*(struct in_addr *)&v10.sa_data[2]);
printf("connection from %s spawned process %d\n", v6, v5);
close(v4);
}
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    int listener_sock = socket(2, SOCK_STREAM, 0);
    addr.sin_family = 2;
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr.sin_port = htons(1337);
    if (argc >= 2)
        addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (argc >= 3)
        addr.sin_port = htons(atoi(argv[2]));
    int yes=1;
    if (-1 == setsockopt(listener_sock, 1, 2, &yes, sizeof(int))) {
        perror("setsockopt");
        return 1;
    }
    if (-1 == bind(listener_sock, (struct sockaddr*)&addr, sizeof(addr))) {
        perror("bind");
        return 1;
    }
    if (-1 == listen(listener_sock, 128)) {
        perror("listen");
        return 1;
    }
    while (1) {
        int handler_pid;
        struct sockaddr_in client_addr;
        socklen_t len = sizeof(client_addr);
        int connection_sock;
        connection_sock = accept(
            listener_sock, (struct sockaddr*)&client_addr, &len);
        if (connection_sock < 0)
            return 1;
        handler_pid = fork();
        if (handler_pid == 0) {
            close(listener_sock);
            return handle_connection(connection_sock, &client_addr);
        } else {
            printf("connection from %s spawned process %d\n",
                inet_ntoa(client_addr.sin_addr), handler_pid);
            close(connection_sock);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    int listener_sock = socket(2, SOCK_STREAM, 0);
    addr.sin_family = 2;
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr.sin_port = htons(1337);
    if (argc >= 2)
        addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (argc >= 3)
        addr.sin_port = htons(atoi(argv[2]));
    int yes = 1;
    if (-1 == setsockopt(listener_sock, 1, 2, &yes, sizeof(int))) {
        perror("setsockopt");
        return 1;
    }
    if (-1 == bind(listener_sock, (struct sockaddr *)&addr, sizeof(addr))) {
        perror("bind");
        return 1;
    }
    if (-1 == listen(listener_sock, 128)) {
        perror("listen");
        return 1;
    }
    while (1) {
        int handler_pid;
        struct sockaddr_in client_addr;
        unsigned int len = sizeof(client_addr);
        int connection_sock;
        connection_sock = accept(
            listener_sock, (struct sockaddr *)&client_addr, &len);
        if (connection_sock < 0)
            return 1;
        handler_pid = fork();
        if (handler_pid == 0) {
            close(listener_sock);
            return handle_connection(connection_sock, &client_addr);
        } else {
            printf("connection from %s spawned process %d\n",
                inet_ntoa(client_addr.sin_addr), handler_pid);
            close(connection_sock);
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
len
unsigned int


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

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

original_code:
void die_with_error(char *message) {
    perror(message);
    exit(1);
}

variable_types:
message
char *

return_type: void


decompiled_code:
__int64 __fastcall setup_server_socket(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
die_with_error("could not create socket");
v2 = v1;
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_QWORD *)&v4.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)v4.sa_data = _bswap_16(a1);
if ( bind(v2, &v4, 0x10u) < 0 )
die_with_error("bind() failed");
if ( listen(v2, 5) < 0 )
die_with_error("listen() failed");
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
func3("could not create socket");
v2 = v1;
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_QWORD *)&v4.sa_data[2] = (unsigned int)func0(0);
*(_WORD *)v4.sa_data = func1(a1);
if ( bind(v2, &v4, 0x10u) < 0 )
func3("bind() failed");
if ( listen(v2, 5) < 0 )
func3("listen() failed");
return v2;
}

original_code:
int setup_server_socket(unsigned short port) {
    int servSock;
    struct sockaddr_in servAddr;
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        die_with_error("could not create socket");
    }
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    servAddr.sin_port = htons(port);
    if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        die_with_error("bind() failed");
    }
    if (listen(servSock, 5) < 0) {
        die_with_error("listen() failed");
    }
    return servSock;
}

canonical_original_code:
int setup_server_socket(unsigned short port) {
    int servSock;
    struct sockaddr_in servAddr;
    if ((servSock = socket(2, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        die_with_error("could not create socket");
    }
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    servAddr.sin_port = htons(port);
    if (bind(servSock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
        die_with_error("bind() failed");
    }
    if (listen(servSock, 5) < 0) {
        die_with_error("listen() failed");
    }
    return servSock;
}

variable_types:
port
unsigned short

variable_types: servSock int

return_type: 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 send_data(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( a3 > 0 )
{
v4 = 0;
do
{
v5 = send(fd, (const void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
die_with_error("write() failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( !a2 )
return 1LL;
v3 = a3;
if ( a3 > 0 )
{
v4 = 0;
do
{
v5 = send(fd, (const void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
func3("write() failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
}
return 1LL;
}

original_code:
_Bool send_data(int sock, void *data, int size) {
    int sent = 0;
    int remaining = size;
    int result;
    if (data == ((void *)0) || size == 0) return 1;
    while (remaining > 0) {
        result = send(sock, (unsigned char *) data + sent, remaining, 0);
        if (result < 0) die_with_error("write() failed");
        sent += result;
        remaining -= result;
    }
    return 1;
}

variable_types: sock int

variable_types:
data
void *

variable_types: size int

variable_types: sent int

variable_types: remaining int

variable_types: result int


decompiled_code:
__int64 __fastcall get_request_header(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = 0;
do
{
v5 = recv(fd, (void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
die_with_error("read() in get_request failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = 0;
do
{
v5 = recv(fd, (void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
func3("read() in get_request failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
return 1LL;
}

original_code:
_Bool get_request_header(int sock, request_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_request failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

canonical_original_code:
_Bool get_request_header(int sock, struct lrequest_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_request failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

variable_types: struct lrequest_header { unsigned int type; unsigned int size; } *

variable_types: received int


decompiled_code:
__int64 __fastcall get_response_header(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = 0;
do
{
v5 = recv(fd, (void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
die_with_error("read() in get_response failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 1LL;
v3 = a3;
v4 = 0;
do
{
v5 = recv(fd, (void *)(a2 + v4), v3, 0);
if ( v5 < 0 )
func3("read() in get_response failed");
v4 += v5;
v3 -= v5;
}
while ( v3 > 0 );
return 1LL;
}

original_code:
_Bool get_response_header(int sock, response_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_response failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

canonical_original_code:
_Bool get_response_header(int sock, struct lresponse_header *header, int size) {
    int received = 0;
    int remaining = size;
    int result;
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) header + received, remaining, 0);
        if (result < 0) die_with_error("read() in get_response failed");
        received += result;
        remaining -= result;
    }
    return 1;
}

variable_types: struct lresponse_header { unsigned int status; unsigned int size; } *


decompiled_code:
char *__fastcall get_data(int fd, int a2)
{
int v2; // ebx
char *v3; // r12
int v4; // ebp
int v5; // eax

v2 = a2;
v3 = (char *)malloc(a2);
memset(v3, 0, a2);
if ( a2 > 0 )
{
v4 = 0;
do
{
v5 = recv(fd, &v3[v4], v2, 0);
if ( v5 < 0 )
die_with_error("read() failed");
v4 += v5;
v2 -= v5;
}
while ( v2 > 0 );
}
return v3;
}

canonical_decompiled_code:
char *__fastcall func8(int fd, int a2)
{
int v2; // ebx
char *v3; // r12
int v4; // ebp
int v5; // eax

v2 = a2;
v3 = (char *)malloc(a2);
memset(v3, 0, a2);
if ( a2 > 0 )
{
v4 = 0;
do
{
v5 = recv(fd, &v3[v4], v2, 0);
if ( v5 < 0 )
func3("read() failed");
v4 += v5;
v2 -= v5;
}
while ( v2 > 0 );
}
return v3;
}

original_code:
unsigned char *get_data(int sock, int size) {
    int received = 0;
    int remaining = size;
    int result;
    char *buffer = malloc(size);
    memset(buffer, 0, size);
    while (remaining > 0) {
        result = recv(sock, (unsigned char *) buffer + received, remaining, 0);
        if (result < 0) die_with_error("read() failed");
        received += result;
        remaining -= result;
    }
    return buffer;
}

variable_types:
buffer
char *

return_type: unsigned char *


decompiled_code:
int __fastcall accept_connection(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

addr_len = 128;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
die_with_error("accept() failed");
return result;
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int result; // eax
socklen_t addr_len; // [rsp+Ch] [rbp-8Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-88h] BYREF

addr_len = 128;
result = accept(a1, &addr, &addr_len);
if ( result < 0 )
func3("accept() failed");
return result;
}

original_code:
int accept_connection(int servSock) {
    struct sockaddr_storage clientAddr;
    socklen_t clientAddrLen = sizeof(clientAddr);
    int clientSock = accept(servSock, (struct sockaddr *) &clientAddr, &clientAddrLen);
    if (clientSock < 0) {
        die_with_error("accept() failed");
    }
    return clientSock;
}

canonical_original_code:
int accept_connection(int servSock) {
    struct sockaddr_storage clientAddr;
    unsigned int clientAddrLen = sizeof(clientAddr);
    int clientSock = accept(servSock, (struct sockaddr *) &clientAddr, &clientAddrLen);
    if (clientSock < 0) {
        die_with_error("accept() failed");
    }
    return clientSock;
}

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
clientAddrLen
unsigned int

variable_types: clientSock int


decompiled_code:
_BOOL8 __fastcall file_comparator(const char **a1, const char **a2)
{
int v2; // edx
_BOOL8 result; // rax

v2 = strcmp(*a1, *a2);
result = 0LL;
if ( !v2 )
result = strcmp(a1[1], a2[1]) == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func14(const char **a1, const char **a2)
{
int v2; // edx
_BOOL8 result; // rax

v2 = strcmp(*a1, *a2);
result = 0LL;
if ( !v2 )
result = strcmp(a1[1], a2[1]) == 0;
return result;
}

original_code:
int file_comparator(const void *data1, const void *data2) {
    int result;
    file_entry *file1 = (file_entry *) data1;
    file_entry *file2 = (file_entry *) data2;
    result = ((strcmp(file1->name, file2->name) == 0) &&
              (strcmp(file1->checksum, file2->checksum) == 0));
    return result;
}

canonical_original_code:
int file_comparator(void *data1, void *data2) {
    int result;
    struct lfile_entry *file1 = (file_entry *) data1;
    struct lfile_entry *file2 = (file_entry *) data2;
    result = ((strcmp(file1->name, file2->name) == 0) &&
              (strcmp(file1->checksum, file2->checksum) == 0));
    return result;
}

variable_types: struct lfile_entry { char *name; char *checksum; unsigned long size; } *


decompiled_code:
int __fastcall print_files(const char **a1)
{
puts(*a1);
return puts(a1[1]);
}

canonical_decompiled_code:
int __fastcall func15(const char **a1)
{
puts(*a1);
return puts(a1[1]);
}

original_code:
void print_files(void *data) {
    file_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
    printf("%s\n", file->checksum);
}

canonical_original_code:
void print_files(void *data) {
    struct lfile_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
    printf("%s\n", file->checksum);
}

user_defined_types: struct lfile_entry { char *name; char *checksum; unsigned long size; }


decompiled_code:
char *__fastcall file_to_string(__int64 a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s\n%s\n%lu\n", *(const char **)a1, *(const char **)(a1 + 8), *(_QWORD *)(a1 + 16));
return ptr;
}

canonical_decompiled_code:
char *__fastcall func16(__int64 a1)
{
char *ptr; // [rsp+8h] [rbp-10h] BYREF

asprintf(&ptr, "%s\n%s\n%lu\n", *(const char **)a1, *(const char **)(a1 + 8), *(_QWORD *)(a1 + 16));
return ptr;
}

original_code:
char *file_to_string(void *data) {
    char *buffer;
    file_entry *file = (file_entry *) data;
    asprintf(&buffer, "%s\n%s\n%lu\n", file->name, file->checksum, file->size);
    return buffer;
}

canonical_original_code:
char *file_to_string(void *data) {
    char *buffer;
    struct lfile_entry *file = (file_entry *) data;
    asprintf(&buffer, "%s\n%s\n%lu\n", file->name, file->checksum, file->size);
    return buffer;
}

return_type: char *


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

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

original_code:
void print_filenames(void *data) {
    file_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
}

canonical_original_code:
void print_filenames(void *data) {
    struct lfile_entry *file = (file_entry *) data;
    printf("%s\n", file->name);
}


decompiled_code:
void free_file()
{
;
}

canonical_decompiled_code:
void func18()
{
;
}

original_code: void free_file(void *data) { }

canonical_original_code: void free_file(void *data) { }


timestamp

decompiled_code:
char *timestamp()
{
char *v0; // rbx
struct tm *v1; // rax
char v2; // dl
char *v3; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x32uLL);
timer = time(0LL);
v1 = localtime(&timer);
asctime_r(v1, v0);
v2 = *v0;
if ( *v0 )
{
v3 = v0;
do
{
if ( v2 == 10 )
*v3 = 0;
v2 = *++v3;
}
while ( *v3 );
}
return v0;
}

canonical_decompiled_code:
char *func19()
{
char *v0; // rbx
struct tm *v1; // rax
char v2; // dl
char *v3; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

v0 = (char *)malloc(0x32uLL);
timer = time(0LL);
v1 = localtime(&timer);
asctime_r(v1, v0);
v2 = *v0;
if ( *v0 )
{
v3 = v0;
do
{
if ( v2 == 10 )
*v3 = 0;
v2 = *++v3;
}
while ( *v3 );
}
return v0;
}

original_code:
char *timestamp() {
 char *ts = (char *)malloc(sizeof(char)*50);
 time_t ltime;
 ltime = time(((void *)0));
 asctime_r(localtime(&ltime), ts);
 char *tmp = ts;
 while (*tmp) {
        if (*tmp == '\n') {
            *tmp='\0';
        }
        tmp++;
    }
 return ts;
}

canonical_original_code:
char *timestamp() {
 char *ts = (char *)malloc(sizeof(char)*50);
 long ltime;
 ltime = time(((void *)0));
 asctime_r(localtime(&ltime), ts);
 char *tmp = ts;
 while (*tmp) {
        if (*tmp == '\n') {
            *tmp='\0';
        }
        tmp++;
    }
 return ts;
}

variable_types:
ts
char *

variable_types: ltime long

variable_types:
tmp
char *


decompiled_code:
__int64 __fastcall parse_command(const char *a1)
{
const char *v1; // rax
bool v2; // zf
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = &a1[strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
v2 = strcmp(a1, "LIST") == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( strcmp(a1, "DIFF") )
{
result = 2LL;
if ( strcmp(a1, "PULL") )
{
result = 4LL;
if ( strcmp(a1, "LEAVE") )
result = strcmp(a1, "FETCH") == 0 ? 3 : -1;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
const char *v1; // rax
bool v2; // zf
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = &a1[strlen(a1) - 1];
if ( *v1 == 10 )
*v1 = 0;
v2 = strcmp(a1, "LIST") == 0;
result = !v2;
if ( !v2 )
{
result = 1LL;
if ( strcmp(a1, "DIFF") )
{
result = 2LL;
if ( strcmp(a1, "PULL") )
{
result = 4LL;
if ( strcmp(a1, "LEAVE") )
result = strcmp(a1, "FETCH") == 0 ? 3 : -1;
}
}
}
return result;
}

original_code:
int parse_command(char *command) {
 if (command == ((void *)0)) {
  return -1;
 }
 size_t length = strlen(command) - 1;
 if (command[length] == '\n') command[length] = '\0';
 if (strcmp(command, "LIST") == 0) {
  return 0;
 } else if (strcmp(command, "DIFF") == 0) {
  return 1;
 } else if (strcmp(command, "PULL") == 0) {
  return 2;
 } else if (strcmp(command, "LEAVE") == 0) {
  return 4;
 } else if (strcmp(command, "FETCH") == 0) {
  return 3;
 } else {
  return -1;
 }
}

canonical_original_code:
int parse_command(char *command) {
 if (command == ((void *)0)) {
  return -1;
 }
 unsigned long length = strlen(command) - 1;
 if (command[length] == '\n') command[length] = '\0';
 if (strcmp(command, "LIST") == 0) {
  return 0;
 } else if (strcmp(command, "DIFF") == 0) {
  return 1;
 } else if (strcmp(command, "PULL") == 0) {
  return 2;
 } else if (strcmp(command, "LEAVE") == 0) {
  return 4;
 } else if (strcmp(command, "FETCH") == 0) {
  return 3;
 } else {
  return -1;
 }
}

variable_types:
command
char *

variable_types:
length
unsigned long


decompiled_code:
int display_help()
{
puts("Invalid Command\nValid commands include:\n");
puts("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer");
puts(
"\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the"
" .mp3 files on this machine");
puts(
"\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same"
" filename that are a different version");
return puts("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n");
}

canonical_decompiled_code:
int func2()
{
puts("Invalid Command\nValid commands include:\n");
puts("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer");
puts(
"\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the"
" .mp3 files on this machine");
puts(
"\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same"
" filename that are a different version");
return puts("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n");
}

original_code:
void display_help(){
 printf("Invalid Command\nValid commands include:\n\n");
 printf("\tLIST\tDisplay a list of the .mp3 files currently on the GTmyMusicServer\n");
 printf("\tDIFF\tDisplay a list of the .mp3 files currently on the server that are not present or that are different than the .mp3 files on this machine\n");
 printf("\tPULL\tFetch the files displayed by DIFF. This will download any files not present or overwrite files with the same filename that are a different version\n");
 printf("\tLEAVE\tDisconnect from the GTmyMusic Server & terminate the program\n\n");
}


decompiled_code:
int __fastcall perform_list(__int64 a1)
{
int result; // eax
_DWORD v2[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
int v4[2]; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v4[0] = 0;
v4[1] = 0;
v5 = 0LL;
send_data(a1, v4, 8LL);
get_response_header((unsigned int)a1, v2, 8LL);
v3 = get_data((unsigned int)a1, v2[1]);
empty_list(server_list, &free_file);
deserialize_list(server_list, v3);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
int result; // eax
_DWORD v2[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v3; // [rsp+8h] [rbp-20h]
int v4[2]; // [rsp+10h] [rbp-18h] BYREF
__int64 v5; // [rsp+18h] [rbp-10h]

v4[0] = 0;
v4[1] = 0;
v5 = 0LL;
send_data(a1, v4, 8LL);
get_response_header((unsigned int)a1, v2, 8LL);
v3 = get_data((unsigned int)a1, v2[1]);
empty_list(server_list, &free_file);
deserialize_list(server_list, v3);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

original_code:
void perform_list(int sock) {
 request req;
 response res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_list(int sock) {
 struct lrequest req;
 struct lresponse res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types: sock int

variable_types: struct lrequest { struct lrequest_header { unsigned int type; unsigned int size; } header; void *data; }

variable_types: struct lresponse { struct lresponse_header { unsigned int status; unsigned int size; } header; void *data; }

user_defined_types: struct lrequest { struct lrequest_header header; void *data; }

user_defined_types: struct lresponse { struct lresponse_header header; void *data; }


decompiled_code:
int __fastcall perform_diff(unsigned int a1)
{
const char *v1; // rbp
int result; // eax
_DWORD v3[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v4; // [rsp+8h] [rbp-30h]
int v5; // [rsp+10h] [rbp-28h] BYREF
unsigned int v6; // [rsp+14h] [rbp-24h]
const char *v7; // [rsp+18h] [rbp-20h]

empty_list(local_list, &free_file);
read_directory(local_list);
v1 = (const char *)traverse_to_string(local_list, &file_to_string);
v5 = 1;
v6 = strlen(v1) + 1;
v7 = v1;
send_data(a1, &v5, 8LL);
send_data(a1, v7, v6);
get_response_header(a1, v3, 8LL);
v4 = get_data(a1, v3[1]);
empty_list(server_list, &free_file);
deserialize_list(server_list, v4);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1)
{
const char *v1; // rbp
int result; // eax
_DWORD v3[2]; // [rsp+0h] [rbp-38h] BYREF
__int64 v4; // [rsp+8h] [rbp-30h]
int v5; // [rsp+10h] [rbp-28h] BYREF
unsigned int v6; // [rsp+14h] [rbp-24h]
const char *v7; // [rsp+18h] [rbp-20h]

empty_list(local_list, &free_file);
read_directory(local_list);
v1 = (const char *)traverse_to_string(local_list, &file_to_string);
v5 = 1;
v6 = strlen(v1) + 1;
v7 = v1;
send_data(a1, &v5, 8LL);
send_data(a1, v7, v6);
get_response_header(a1, v3, 8LL);
v4 = get_data(a1, v3[1]);
empty_list(server_list, &free_file);
deserialize_list(server_list, v4);
if ( *(_DWORD *)(server_list + 8) )
result = traverse(server_list, &print_filenames);
else
result = puts("No files found.");
return result;
}

original_code:
void perform_diff(int sock) {
 char *list;
 request req;
 response res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_diff(int sock) {
 char *list;
 struct lrequest req;
 struct lresponse res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types:
list
char *

user_defined_types: struct lrequest_header { unsigned int type; unsigned int size; }

user_defined_types: struct lresponse_header { unsigned int status; unsigned int size; }


decompiled_code:
void perform_fetch()
{
;
}

canonical_decompiled_code:
void func6()
{
;
}

original_code: void perform_fetch(int sock) { }

canonical_original_code: void perform_fetch(int sock) { }


decompiled_code:
void __fastcall __noreturn perform_leave(__int64 a1)
{
int v1[2]; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1[0] = 4;
v1[1] = 0;
v2 = 0LL;
send_data(a1, v1, 8LL);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(__int64 a1)
{
int v1[2]; // [rsp+0h] [rbp-18h] BYREF
__int64 v2; // [rsp+8h] [rbp-10h]

v1[0] = 4;
v1[1] = 0;
v2 = 0LL;
send_data(a1, v1, 8LL);
exit(0);
}

original_code:
void perform_leave(int sock) {
 request req;
 req.header.type = 4;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 exit(0);
}

canonical_original_code:
void perform_leave(int sock) {
 struct lrequest req;
 req.header.type = 4;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 exit(0);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rdx
int v5; // er12
__int16 v6; // ax
int v7; // eax
_BYTE v8[512]; // [rsp+0h] [rbp-428h] BYREF
char v9[512]; // [rsp+200h] [rbp-228h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-28h] BYREF

v3 = (char *)malloc(0x20uLL);
memset(v9, 0, sizeof(v9));
memset(v8, 0, sizeof(v8));
*(_QWORD *)v3 = 0LL;
*((_QWORD *)v3 + 1) = 0LL;
*((_QWORD *)v3 + 2) = 0LL;
*((_QWORD *)v3 + 3) = 0LL;
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", *argv);
exit(1);
}
local_list = create_list(v9, argv, v8);
server_list = create_list(v9, argv, v4);
v5 = socket(2, 1, 6);
if ( v5 < 0 )
die_with_error("socket() failed\n");
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
v6 = atoi(argv[2]);
*(_WORD *)addr.sa_data = _bswap_16(v6);
if ( connect(v5, &addr, 0x10u) < 0 )
die_with_error("connect() failed");
puts("Welcome to GTmyMusic.");
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
fgets(v3, 32, stdin);
v7 = parse_command(v3);
if ( v7 != 2 )
break;
perform_pull(v5);
}
if ( v7 <= 2 )
break;
if ( v7 == 4 )
perform_leave((unsigned int)v5);
LABEL_17:
display_help();
}
if ( v7 )
{
if ( v7 != 1 )
goto LABEL_17;
perform_diff(v5);
}
else
{
perform_list((unsigned int)v5);
}
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
__int64 v4; // rdx
int v5; // er12
__int16 v6; // ax
int v7; // eax
_BYTE v8[512]; // [rsp+0h] [rbp-428h] BYREF
char v9[512]; // [rsp+200h] [rbp-228h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-28h] BYREF

v3 = (char *)malloc(0x20uLL);
memset(v9, 0, sizeof(v9));
memset(v8, 0, sizeof(v8));
*(_QWORD *)v3 = 0LL;
*((_QWORD *)v3 + 1) = 0LL;
*((_QWORD *)v3 + 2) = 0LL;
*((_QWORD *)v3 + 3) = 0LL;
if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", *argv);
exit(1);
}
local_list = create_list(v9, argv, v8);
server_list = create_list(v9, argv, v4);
v5 = socket(2, 1, 6);
if ( v5 < 0 )
die_with_error("socket() failed\n");
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_DWORD *)&addr.sa_data[2] = inet_addr(argv[1]);
v6 = atoi(argv[2]);
*(_WORD *)addr.sa_data = func0(v6);
if ( connect(v5, &addr, 0x10u) < 0 )
die_with_error("connect() failed");
puts("Welcome to GTmyMusic.");
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
fgets(v3, 32, stdin);
v7 = func1(v3);
if ( v7 != 2 )
break;
func5(v5);
}
if ( v7 <= 2 )
break;
if ( v7 == 4 )
func7((unsigned int)v5);
LABEL_17:
func2();
}
if ( v7 )
{
if ( v7 != 1 )
goto LABEL_17;
func4(v5);
}
else
{
func3((unsigned int)v5);
}
}
}

original_code:
int main(int argc, char *argv[]) {
 struct sockaddr_in serv_addr;
 int i;
 char *input = (char *) malloc(32);
 char sndBuf[512];
 char rcvBuf[512];
 memset(&sndBuf, 0, 512);
 memset(&rcvBuf, 0, 512);
 memset(input, 0, 32);
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <SERVER HOST> <SERVER PORT>\n", argv[0]);
        exit(1);
    }
    local_list = create_list();
    server_list = create_list();
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if(sock < 0)
  die_with_error("socket() failed\n");
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family = 2;
 serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
 serv_addr.sin_port = htons(atoi(argv[2]));
 if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  die_with_error("connect() failed");
 }
 printf("Welcome to GTmyMusic.\n");
 while (1) {
  fgets(input, 32, stdin);
  switch(parse_command(input)) {
   case 0:
    perform_list(sock);
    break;
   case 1:
    perform_diff(sock);
    break;
   case 2:
    perform_pull(sock);
    break;
   case 4:
    perform_leave(sock);
    break;
   default:
    display_help();
    break;
  }
 }
 close(sock);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types:
input
char *

variable_types:
sndBuf
char [512]

variable_types:
rcvBuf
char [512]


decompiled_code:
_DWORD *create_list()
{
_DWORD *result; // rax

result = malloc(0x10uLL);
if ( !result )
{
printf("Malloc failed.");
exit(0);
}
result[2] = 0;
*(_QWORD *)result = 0LL;
return result;
}

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

result = malloc(0x10uLL);
if ( !result )
{
printf("Malloc failed.");
exit(0);
}
result[2] = 0;
*(_QWORD *)result = 0LL;
return result;
}

original_code:
list* create_list(void) {
 list *newList = malloc(sizeof(list));
 if (newList == ((void *)0)) {
  printf("Malloc failed.");
  exit(0);
 } else {
  newList->size = 0;
  newList->head = ((void *)0);
  return newList;
 }
}

canonical_original_code:
struct llist *create_list() {
 struct llist *newList = malloc(sizeof(list));
 if (newList == ((void *)0)) {
  printf("Malloc failed.");
  exit(0);
 } else {
  newList->size = 0;
  newList->head = ((void *)0);
  return newList;
 }
}

variable_types: struct llist { struct lnode { struct lnode *prev; struct lnode *next; void *data; } *head; unsigned int size; } *

user_defined_types: struct llist { struct lnode *head; unsigned int size; }


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

result = 0LL;
if ( !*(_QWORD *)a1 )
result = *(_DWORD *)(a1 + 8) == 0;
return result;
}

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

result = 0LL;
if ( !*(_QWORD *)a1 )
result = *(_DWORD *)(a1 + 8) == 0;
return result;
}

original_code:
int is_empty(list* llist) {
 if (llist->head == ((void *)0) && llist->size == 0) {
  return 1;
 } else {
  return 0;
 }
}

canonical_original_code:
int is_empty(struct llist *llist) {
 if (llist->head == ((void *)0) && llist->size == 0) {
  return 1;
 } else {
  return 0;
 }
}

user_defined_types: struct lnode { struct lnode *prev; struct lnode *next; void *data; }


original_code:
int remove_front(list* llist, list_op free_func) {
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  free_func(llist->head->data);
  free(llist->head);
  llist->head = ((void *)0);
  llist->size--;
  return 0;
 } else {
  node *prev;
  node *next;
  prev = llist->head->prev;
  next = llist->head->next;
  prev->next = next;
  next->prev = prev;
  free_func(llist->head->data);
  free(llist->head);
  llist->head = next;
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_front(struct llist *llist, void (*free_func)(void *)) {
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  free_func(llist->head->data);
  free(llist->head);
  llist->head = ((void *)0);
  llist->size--;
  return 0;
 } else {
  struct lnode *prev;
  struct lnode *next;
  prev = llist->head->prev;
  next = llist->head->next;
  prev->next = next;
  next->prev = prev;
  free_func(llist->head->data);
  free(llist->head);
  llist->head = next;
  llist->size--;
  return 0;
 }
}

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


decompiled_code:
__int64 __fastcall remove_back(void **a1, void (__fastcall *a2)(__int64))
{
unsigned int v2; // ebp
__int64 *v3; // r13
__int64 v4; // rdx
_QWORD *v5; // rax

if ( is_empty((__int64)a1) )
return (unsigned int)-1;
v2 = 0;
if ( *((_DWORD *)a1 + 2) == 1 )
return (unsigned int)remove_front(a1, a2);
v3 = *(__int64 **)*a1;
v4 = *v3;
v5 = (_QWORD *)v3[1];
*(_QWORD *)(v4 + 8) = v5;
*v5 = v4;
a2(v3[2]);
free(v3);
--*((_DWORD *)a1 + 2);
return v2;
}

original_code:
int remove_back(list* llist, list_op free_func)
{
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  return remove_front(llist, free_func);
 } else {
  node *removed = llist->head->prev;
  node *prev;
  node *next;
  prev = removed->prev;
  next = removed->next;
  prev->next = next;
  next->prev = prev;
  free_func(removed->data);
  free(removed);
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_back(struct llist *llist, void (*free_func)(void *))
{
 if (is_empty(llist)) {
  return -1;
 } else if (llist->size == 1) {
  return remove_front(llist, free_func);
 } else {
  struct lnode *removed = llist->head->prev;
  struct lnode *prev;
  struct lnode *next;
  prev = removed->prev;
  next = removed->next;
  prev->next = next;
  next->prev = prev;
  free_func(removed->data);
  free(removed);
  llist->size--;
  return 0;
 }
}


decompiled_code:
__int64 __fastcall remove_index(__int64 a1, int a2, void (__fastcall *a3)(__int64))
{
unsigned int v4; // er13
unsigned int v5; // eax
__int64 *v6; // rbx
int i; // eax
__int64 v8; // rdx
_QWORD *v9; // rax

if ( is_empty(a1) )
return (unsigned int)-1;
v4 = 0;
v5 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v5 <= a2 )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)remove_front((void **)a1, a3);
if ( a2 == v5 - 1 )
return (unsigned int)remove_back((void **)a1, a3);
v6 = *(__int64 **)a1;
for ( i = 0; i < a2; ++i )
v6 = (__int64 *)v6[1];
v8 = *v6;
v9 = (_QWORD *)v6[1];
*(_QWORD *)(v8 + 8) = v9;
*v9 = v8;
a3(v6[2]);
free(v6);
--*(_DWORD *)(a1 + 8);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, void (__fastcall *a3)(__int64))
{
unsigned int v4; // er13
unsigned int v5; // eax
__int64 *v6; // rbx
int i; // eax
__int64 v8; // rdx
_QWORD *v9; // rax

if ( func1(a1) )
return (unsigned int)-1;
v4 = 0;
v5 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v5 <= a2 )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)func2((void **)a1, a3);
if ( a2 == v5 - 1 )
return (unsigned int)func3((void **)a1, a3);
v6 = *(__int64 **)a1;
for ( i = 0; i < a2; ++i )
v6 = (__int64 *)v6[1];
v8 = *v6;
v9 = (_QWORD *)v6[1];
*(_QWORD *)(v8 + 8) = v9;
*v9 = v8;
a3(v6[2]);
free(v6);
--*(_DWORD *)(a1 + 8);
return v4;
}

original_code:
int remove_index(list* llist, int index, list_op free_func)
{
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return -1;
 } else if (index == 0) {
  return remove_front(llist, free_func);
 } else if (index == llist->size - 1) {
  return remove_back(llist, free_func);
 } else {
  int i = 0;
  node *current = llist->head;
  while (i < index) {
   current = current->next;
   i++;
  }
  node *prev;
  node *next;
  prev = current->prev;
  next = current->next;
  prev->next = next;
  next->prev = prev;
  free_func(current->data);
  free(current);
  llist->size--;
  return 0;
 }
}

canonical_original_code:
int remove_index(struct llist *llist, int index, void (*free_func)(void *))
{
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return -1;
 } else if (index == 0) {
  return remove_front(llist, free_func);
 } else if (index == llist->size - 1) {
  return remove_back(llist, free_func);
 } else {
  int i = 0;
  struct lnode *current = llist->head;
  while (i < index) {
   current = current->next;
   i++;
  }
  struct lnode *prev;
  struct lnode *next;
  prev = current->prev;
  next = current->next;
  prev->next = next;
  next->prev = prev;
  free_func(current->data);
  free(current);
  llist->size--;
  return 0;
 }
}

variable_types: index int


original_code:
int remove_data(list* llist, const void* data, equal_op compare_func, list_op free_func)
{
 if (!is_empty(llist)) {
  node *upNext = llist->head;
  node *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (compare_func(current->data, data) && i > 0) {
    node *prev;
    node *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (compare_func(current->data, data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

canonical_original_code:
int remove_data(struct llist *llist, void *data, int (*compare_func)(void *, void *), void (*free_func)(void *))
{
 if (!is_empty(llist)) {
  struct lnode *upNext = llist->head;
  struct lnode *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (compare_func(current->data, data) && i > 0) {
    struct lnode *prev;
    struct lnode *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (compare_func(current->data, data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

variable_types:
data
void *

variable_types: count int


original_code:
int remove_if(list* llist, list_pred pred_func, list_op free_func) {
 if (!is_empty(llist)) {
  node *upNext = llist->head;
  node *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (pred_func(current->data) && i > 0) {
    node *prev;
    node *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (pred_func(current->data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}

canonical_original_code:
int remove_if(struct llist *llist, int (*pred_func)(void *), void (*free_func)(void *)) {
 if (!is_empty(llist)) {
  struct lnode *upNext = llist->head;
  struct lnode *current;
  int i = 0;
  int count = 0;
  while (i < llist->size) {
   current = upNext;
   upNext = current->next;
   if (pred_func(current->data) && i > 0) {
    struct lnode *prev;
    struct lnode *next;
    prev = current->prev;
    next = current->next;
    prev->next = next;
    next->prev = prev;
    free_func(current->data);
    free(current);
    llist->size--;
    i--;
    count++;
   } else if (pred_func(current->data) && i == 0) {
    remove_front(llist, free_func);
    i--;
    count++;
   }
   i++;
  }
  return count;
 } else {
  return -1;
 }
}


front

decompiled_code:
__int64 __fastcall front(__int64 a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = is_empty(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
return result;
}

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

v1 = func1(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(*(_QWORD *)a1 + 16LL);
return result;
}

original_code:
void* front(list* llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->data;
 }
}

canonical_original_code:
void *front(struct llist *llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->data;
 }
}

return_type: void *


back

decompiled_code:
__int64 __fastcall back(__int64 a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = is_empty(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(**(_QWORD **)a1 + 16LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
_BOOL4 v1; // edx
__int64 result; // rax

v1 = func1(a1);
result = 0LL;
if ( !v1 )
result = *(_QWORD *)(**(_QWORD **)a1 + 16LL);
return result;
}

original_code:
void* back(list* llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->prev->data;
 }
}

canonical_original_code:
void *back(struct llist *llist) {
 if (is_empty(llist)) {
  return ((void *)0);
 } else {
  return llist->head->prev->data;
 }
}


decompiled_code:
__int64 __fastcall get_index(__int64 a1, int a2)
{
int v3; // eax
unsigned int v4; // edx
__int64 v5; // rdx
__int64 v6; // rsi

v3 = is_empty(a1);
if ( v3 )
return 0LL;
v4 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v4 <= a2 )
return 0LL;
if ( !a2 )
return front(a1);
if ( a2 == v4 - 1 )
return back(a1);
v5 = *(_QWORD *)a1;
while ( 1 )
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
if ( v5 == *(_QWORD *)a1 )
break;
if ( a2 <= ++v3 )
{
v6 = v5;
return *(_QWORD *)(v6 + 16);
}
}
return *(_QWORD *)(v6 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
int v3; // eax
unsigned int v4; // edx
__int64 v5; // rdx
__int64 v6; // rsi

v3 = func1(a1);
if ( v3 )
return 0LL;
v4 = *(_DWORD *)(a1 + 8);
if ( a2 < 0 || v4 <= a2 )
return 0LL;
if ( !a2 )
return func7(a1);
if ( a2 == v4 - 1 )
return func8(a1);
v5 = *(_QWORD *)a1;
while ( 1 )
{
v6 = v5;
v5 = *(_QWORD *)(v5 + 8);
if ( v5 == *(_QWORD *)a1 )
break;
if ( a2 <= ++v3 )
{
v6 = v5;
return *(_QWORD *)(v6 + 16);
}
}
return *(_QWORD *)(v6 + 16);
}

original_code:
void* get_index(list* llist, int index) {
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return ((void *)0);
 } else if (index == 0) {
  return front(llist);
 } else if (index == llist->size - 1) {
  return back(llist);
 } else {
  int i = 0;
  node *current = llist->head;
  while (i < index && current->next != llist->head) {
   current = current->next;
   i++;
  }
  return current->data;
 }
}

canonical_original_code:
void *get_index(struct llist *llist, int index) {
 if (is_empty(llist) || index >= llist->size || index < 0) {
  return ((void *)0);
 } else if (index == 0) {
  return front(llist);
 } else if (index == llist->size - 1) {
  return back(llist);
 } else {
  int i = 0;
  struct lnode *current = llist->head;
  while (i < index && current->next != llist->head) {
   current = current->next;
   i++;
  }
  return current->data;
 }
}


size

decompiled_code:
__int64 __fastcall size(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int size(list* llist) {
    return llist->size;
}

canonical_original_code:
int size(struct llist *llist) {
    return llist->size;
}


decompiled_code:
__int64 __fastcall find_occurrence(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64))
{
int v4; // ebx
__int64 result; // rax
__int64 v6; // rbp
int v7; // er14

v4 = is_empty(a1);
result = 0LL;
if ( !v4 )
{
v6 = *(_QWORD *)a1;
v7 = *(_DWORD *)(a1 + 8);
if ( v7 <= 0 )
{
result = 0LL;
}
else
{
while ( 1 )
{
result = a3(*(_QWORD *)(v6 + 16), a2);
if ( (_DWORD)result )
break;
v6 = *(_QWORD *)(v6 + 8);
if ( v7 == ++v4 )
return result;
}
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD, __int64))
{
int v4; // ebx
__int64 result; // rax
__int64 v6; // rbp
int v7; // er14

v4 = func1(a1);
result = 0LL;
if ( !v4 )
{
v6 = *(_QWORD *)a1;
v7 = *(_DWORD *)(a1 + 8);
if ( v7 <= 0 )
{
result = 0LL;
}
else
{
while ( 1 )
{
result = a3(*(_QWORD *)(v6 + 16), a2);
if ( (_DWORD)result )
break;
v6 = *(_QWORD *)(v6 + 8);
if ( v7 == ++v4 )
return result;
}
result = 1LL;
}
}
return result;
}

original_code:
int find_occurrence(list* llist, const void* search, equal_op compare_func) {
 if (is_empty(llist)) {
  return 0;
 } else {
  node *current = llist->head;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   if (compare_func(current->data, search)) {
    return 1;
   }
   current = current->next;
   i++;
  }
  return 0;
 }
}

canonical_original_code:
int find_occurrence(struct llist *llist, void *search, int (*compare_func)(void *, void *)) {
 if (is_empty(llist)) {
  return 0;
 } else {
  struct lnode *current = llist->head;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   if (compare_func(current->data, search)) {
    return 1;
   }
   current = current->next;
   i++;
  }
  return 0;
 }
}

variable_types:
search
void *

variable_types: size int


decompiled_code:
_BOOL8 __fastcall empty_list(void **a1, void (__fastcall *a2)(_QWORD))
{
_BOOL8 result; // rax

while ( 1 )
{
result = is_empty((__int64)a1);
if ( result )
break;
remove_front(a1, a2);
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(void **a1, void (__fastcall *a2)(_QWORD))
{
_BOOL8 result; // rax

while ( 1 )
{
result = func1((__int64)a1);
if ( result )
break;
func2(a1, a2);
}
return result;
}

original_code:
void empty_list(list* llist, list_op free_func) {
 while (!is_empty(llist)) {
  remove_front(llist, free_func);
 }
}

canonical_original_code:
void empty_list(struct llist *llist, void (*free_func)(void *)) {
 while (!is_empty(llist)) {
  remove_front(llist, free_func);
 }
}


traverse

decompiled_code:
__int64 __fastcall traverse(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
int v3; // ebx
__int64 v4; // rbp
int v5; // er13
__int64 v6; // rax

result = is_empty(a1);
if ( !(_DWORD)result )
{
v3 = 0;
v4 = *(_QWORD *)a1;
v5 = *(_DWORD *)(a1 + 8);
if ( v5 > 0 )
{
do
{
v6 = v4;
v4 = *(_QWORD *)(v4 + 8);
result = a2(*(_QWORD *)(v6 + 16));
++v3;
}
while ( v5 != v3 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, __int64 (__fastcall *a2)(_QWORD))
{
__int64 result; // rax
int v3; // ebx
__int64 v4; // rbp
int v5; // er13
__int64 v6; // rax

result = func1(a1);
if ( !(_DWORD)result )
{
v3 = 0;
v4 = *(_QWORD *)a1;
v5 = *(_DWORD *)(a1 + 8);
if ( v5 > 0 )
{
do
{
v6 = v4;
v4 = *(_QWORD *)(v4 + 8);
result = a2(*(_QWORD *)(v6 + 16));
++v3;
}
while ( v5 != v3 );
}
}
return result;
}

original_code:
void traverse(list* llist, list_op do_func) {
 if (!is_empty(llist)) {
  node *next = llist->head;
  node *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   do_func(current->data);
   i++;
  }
 }
}

canonical_original_code:
void traverse(struct llist *llist, void (*do_func)(void *)) {
 if (!is_empty(llist)) {
  struct lnode *next = llist->head;
  struct lnode *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   do_func(current->data);
   i++;
  }
 }
}


decompiled_code:
__int64 __fastcall traverse_diff(__int64 a1, __int64 a2, _DWORD *a3, __int64 (__fastcall *a4)(_QWORD, __int64))
{
__int64 result; // rax
int v6; // ebx
__int64 v7; // rbp
int v8; // er15
__int64 v9; // r12

result = is_empty(a1);
if ( !(_DWORD)result )
{
v6 = 0;
v7 = *(_QWORD *)a1;
v8 = *(_DWORD *)(a1 + 8);
if ( v8 > 0 )
{
do
{
v9 = v7;
v7 = *(_QWORD *)(v7 + 8);
result = find_occurrence(a2, *(_QWORD *)(v9 + 16), a4);
if ( !(_DWORD)result )
result = (__int64)push_back(a3, *(_QWORD *)(v9 + 16));
++v6;
}
while ( v8 != v6 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, __int64 a2, _DWORD *a3, __int64 (__fastcall *a4)(_QWORD, __int64))
{
__int64 result; // rax
int v6; // ebx
__int64 v7; // rbp
int v8; // er15
__int64 v9; // r12

result = func1(a1);
if ( !(_DWORD)result )
{
v6 = 0;
v7 = *(_QWORD *)a1;
v8 = *(_DWORD *)(a1 + 8);
if ( v8 > 0 )
{
do
{
v9 = v7;
v7 = *(_QWORD *)(v7 + 8);
result = func11(a2, *(_QWORD *)(v9 + 16), a4);
if ( !(_DWORD)result )
result = (__int64)push_back(a3, *(_QWORD *)(v9 + 16));
++v6;
}
while ( v8 != v6 );
}
}
return result;
}

original_code:
void traverse_diff(list *llist, list *client_list, list *output, equal_op compare_func) {
 if (!is_empty(llist)) {
  node *next = llist->head;
  node *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   if (!find_occurrence(client_list, current->data, compare_func)) {
                push_back(output, current->data);
            }
   i++;
  }
 }
}

canonical_original_code:
void traverse_diff(struct llist *llist, struct llist *client_list, struct llist *output, int (*compare_func)(void *, void *)) {
 if (!is_empty(llist)) {
  struct lnode *next = llist->head;
  struct lnode *current;
  int i = 0;
  int size = llist->size;
  while (i < size) {
   current = next;
   next = current->next;
   if (!find_occurrence(client_list, current->data, compare_func)) {
                push_back(output, current->data);
            }
   i++;
  }
 }
}

variable_types:
output
struct llist *


decompiled_code:
__int64 __fastcall perform_list(unsigned int a1, __int64 a2, __int64 a3)
{
const char *v4; // rbx
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
const char *v7; // [rsp+8h] [rbp-20h]

empty_list(a3, &free_file);
read_directory(a3);
v4 = (const char *)traverse_to_string(a3, &file_to_string);
HIDWORD(v6) = strlen(v4) + 1;
v7 = v4;
send_data(a1, &v6, 8LL);
return send_data(a1, v7, HIDWORD(v6));
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2, __int64 a3)
{
const char *v4; // rbx
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
const char *v7; // [rsp+8h] [rbp-20h]

empty_list(a3, &free_file);
read_directory(a3);
v4 = (const char *)traverse_to_string(a3, &file_to_string);
HIDWORD(v6) = strlen(v4) + 1;
v7 = v4;
send_data(a1, &v6, 8LL);
return send_data(a1, v7, HIDWORD(v6));
}

original_code:
void perform_list(int sock) {
 request req;
 response res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_list(int sock) {
 struct lrequest req;
 struct lresponse res;
 req.header.type = 0;
 req.header.size = 0;
 req.data = ((void *)0);
 send_data(sock, &(req.header), sizeof(req.header));
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
 deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types: sock int

variable_types: struct lrequest { struct lrequest_header { unsigned int type; unsigned int size; } header; void *data; }

variable_types: struct lresponse { struct lresponse_header { unsigned int status; unsigned int size; } header; void *data; }


decompiled_code:
__int64 __fastcall perform_diff(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // r14
const char *v9; // rbp
__int64 v11; // [rsp+0h] [rbp-38h] BYREF
const char *v12; // [rsp+8h] [rbp-30h]

v8 = get_data(a1, *(unsigned int *)(a2 + 4));
empty_list(a4, &free_file);
deserialize_list(a4, v8);
empty_list(a3, &free_file);
read_directory(a3);
empty_list(a5, &free_file);
traverse_diff(a3, a4, a5, &file_comparator);
v9 = (const char *)traverse_to_string(a5, &file_to_string);
HIDWORD(v11) = strlen(v9) + 1;
v12 = v9;
send_data((unsigned int)a1, &v11, 8LL);
return send_data((unsigned int)a1, v12, HIDWORD(v11));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
__int64 v8; // r14
const char *v9; // rbp
__int64 v11; // [rsp+0h] [rbp-38h] BYREF
const char *v12; // [rsp+8h] [rbp-30h]

v8 = get_data(a1, *(unsigned int *)(a2 + 4));
empty_list(a4, &free_file);
deserialize_list(a4, v8);
empty_list(a3, &free_file);
read_directory(a3);
empty_list(a5, &free_file);
traverse_diff(a3, a4, a5, &file_comparator);
v9 = (const char *)traverse_to_string(a5, &file_to_string);
HIDWORD(v11) = strlen(v9) + 1;
v12 = v9;
send_data((unsigned int)a1, &v11, 8LL);
return send_data((unsigned int)a1, v12, HIDWORD(v11));
}

original_code:
void perform_diff(int sock) {
 char *list;
 request req;
 response res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

canonical_original_code:
void perform_diff(int sock) {
 char *list;
 struct lrequest req;
 struct lresponse res;
 empty_list(local_list, free_file);
 read_directory(local_list);
 list = traverse_to_string(local_list, file_to_string);
 req.header.type = 1;
 req.header.size = sizeof(char) * (strlen(list) + 1);
 req.data = list;
 send_data(sock, &(req.header), sizeof(req.header));
 send_data(sock, req.data, req.header.size);
 get_response_header(sock, &(res.header), sizeof(res.header));
 res.data = get_data(sock, res.header.size);
 empty_list(server_list, free_file);
    deserialize_list(server_list, res.data);
 if (server_list->size == 0) {
  printf("No files found.\n");
 } else {
  traverse(server_list, print_filenames);
 }
}

variable_types:
list
char *


decompiled_code:
int __fastcall get_client_ip(int a1, char *a2)
{
unsigned __int16 v2; // bp
char v4[16]; // [rsp+0h] [rbp-B8h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-A8h] BYREF
socklen_t len[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

len[0] = 128;
getpeername(a1, &addr, len);
v2 = _bswap_16(*(__int16 *)addr.sa_data);
inet_ntop(2, &addr.sa_data[2], v4, 0x10u);
return snprintf(a2, 0x15uLL, "%s:%i", v4, v2);
}

canonical_decompiled_code:
int __fastcall func5(int a1, char *a2)
{
unsigned __int16 v2; // bp
char v4[16]; // [rsp+0h] [rbp-B8h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-A8h] BYREF
socklen_t len[7]; // [rsp+9Ch] [rbp-1Ch] BYREF

len[0] = 128;
getpeername(a1, &addr, len);
v2 = func0(*(__int16 *)addr.sa_data);
inet_ntop(2, &addr.sa_data[2], v4, 0x10u);
return snprintf(a2, 0x15uLL, "%s:%i", v4, v2);
}

original_code:
void get_client_ip(int clientSock, char *buf){
 socklen_t len;
 struct sockaddr_storage addr;
 char ip[16];
 int port;
 len = sizeof addr;
 getpeername(clientSock, (struct sockaddr*)&addr, &len);
 struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    port = ntohs(s->sin_port);
    inet_ntop(2, &s->sin_addr, ip, sizeof(ip));
    snprintf(buf, sizeof(ip)+5, "%s:%i", ip, port);
}

canonical_original_code:
void get_client_ip(int clientSock, char *buf){
 unsigned int len;
 struct sockaddr_storage addr;
 char ip[16];
 int port;
 len = sizeof addr;
 getpeername(clientSock, (struct sockaddr *)&addr, &len);
 struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    port = ntohs(s->sin_port);
    inet_ntop(2, &s->sin_addr, ip, sizeof(ip));
    snprintf(buf, sizeof(ip)+5, "%s:%i", ip, port);
}

variable_types: clientSock int

variable_types:
buf
char *

variable_types:
len
unsigned int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
ip
char [16]

variable_types: port int

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


repo: pstuifzand/meta-ii-parser

original_code:
void generate_label(int lbl)
{
    int c = lbl % 10;
    lbl -= c;
    lbl /= 10;
    int b = lbl % 10;
    lbl -= b;
    lbl /= 10;
    int a = lbl % 26;
    printf("%c%c%c\n", a+'A', b+'0', c+'0');
}

variable_types: lbl int

variable_types: c int

variable_types: b int

variable_types: a int


label

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

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

original_code:
void label(char* s)
{
    printf("%s\n",s);
}

canonical_original_code:
void label(char *s)
{
    printf("%s\n",s);
}

variable_types:
s
char *


decompiled_code:
int __fastcall out1(const char *a1)
{
return printf(" %-3s\n", a1);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
return printf(" %-3s\n", a1);
}

original_code:
void out1(char* p)
{
    printf("        %-3s\n",p);
}

canonical_original_code:
void out1(char *p)
{
    printf("        %-3s\n",p);
}

variable_types:
p
char *


decompiled_code:
int __fastcall out2(const char *a1, const char *a2)
{
printf(" %-3s ", a1);
if ( a2 )
printf("%s", a2);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1, const char *a2)
{
printf(" %-3s ", a1);
if ( a2 )
printf("%s", a2);
return putchar(10);
}

original_code:
void out2(char* p, char* s)
{
    printf("        %-3s ",p);
    if (s) {
        printf("%s", s);
    }
    printf("\n");
}

canonical_original_code:
void out2(char *p, char *s)
{
    printf("        %-3s ",p);
    if (s) {
        printf("%s", s);
    }
    printf("\n");
}


decompiled_code:
int __fastcall out3(const char *a1, int a2)
{
printf(" %-3s ", a1);
return generate_label(a2);
}

canonical_decompiled_code:
int __fastcall func11(const char *a1, int a2)
{
printf(" %-3s ", a1);
return func7(a2);
}

original_code:
void out3(char* p, int lbl)
{
    printf("        %-3s ",p);
    generate_label(lbl);
}

canonical_original_code:
void out3(char *p, int lbl)
{
    printf("        %-3s ",p);
    generate_label(lbl);
}


repo: ptarcher/FIT-decode

decompiled_code:
__int64 __fastcall check_header(unsigned __int8 *a1)
{
if ( *a1 != 12 )
{
fprintf(stderr, "Not a .FIT header (%d != header_length = %ld)\n", *a1, 12LL);
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
if ( *a1 != 12 )
{
fprintf(stderr, "Not a .FIT header (%d != header_length = %ld)\n", *a1, 12LL);
exit(1);
}
return 0LL;
}

original_code:
int check_header(struct fit_header *hdr) {
    if (hdr->h_len != sizeof(*hdr)) {
        fprintf(stderr,
            "Not a .FIT header (%d != header_length = %ld)\n",
            hdr->h_len,
            sizeof(*hdr));
        exit(1);
    } else if (0) {
        fprintf(stderr,
            "Not a .FIT header (%d != signature '.FIT')\n",
            hdr->h_len);
        exit(1);
    }
    return 0;
}

variable_types: struct fit_header { unsigned char h_len; unsigned char protocol_version; unsigned char profile_version; unsigned char xxx2; unsigned int file_size; unsigned char sig[4]; } *


decompiled_code:
__int64 __fastcall fetch_record_definition(char a1)
{
char *v1; // rbx
unsigned __int8 v2; // bp
__int64 v3; // rbp

v1 = (char *)calloc(1uLL, 0x18uLL);
if ( (unsigned int)fread(v1 + 1, 5uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_record_desc\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
nbytes += 5;
v2 = v1[5];
*((_QWORD *)v1 + 1) = calloc(1uLL, 3LL * v2);
if ( v2 )
{
v3 = 0LL;
do
{
if ( (unsigned int)fread((void *)(*((_QWORD *)v1 + 1) + 3 * v3), 3uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_field_desc\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
nbytes += 3;
*((_QWORD *)v1 + 2) += *(unsigned __int8 *)(*((_QWORD *)v1 + 1) + 3 * v3++ + 1);
}
while ( (unsigned __int8)v1[5] > (int)v3 );
}
*v1 = a1;
return insert_definition(v1);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
char *v1; // rbx
unsigned __int8 v2; // bp
__int64 v3; // rbp

v1 = (char *)calloc(1uLL, 0x18uLL);
if ( (unsigned int)fread(v1 + 1, 5uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_record_desc\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
nbytes += 5;
v2 = v1[5];
*((_QWORD *)v1 + 1) = calloc(1uLL, 3LL * v2);
if ( v2 )
{
v3 = 0LL;
do
{
if ( (unsigned int)fread((void *)(*((_QWORD *)v1 + 1) + 3 * v3), 3uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_field_desc\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
nbytes += 3;
*((_QWORD *)v1 + 2) += *(unsigned __int8 *)(*((_QWORD *)v1 + 1) + 3 * v3++ + 1);
}
while ( (unsigned __int8)v1[5] > (int)v3 );
}
*v1 = a1;
return insert_definition(v1);
}

original_code:
void fetch_record_definition(uint8_t record_num) {
    int nelms;
    struct fit_definition *def;
    def = calloc(1, sizeof(*def));
    if ((nelms = fread(&(def->record), sizeof(def->record), 1, fp)) != 1) {
        fprintf(stderr, "Unable to read fit_record_desc\n");
        exit(1);
    }
    nbytes += sizeof(def->record);
    def->fields = calloc(1, sizeof(*def->fields)*def->record.nfields);
    int i;
    for (i = 0; i < def->record.nfields; i++) {
        if ((nelms = fread(&(def->fields[i]), sizeof(*(def->fields)), 1, fp)) != 1) {
            fprintf(stderr, "Unable to read fit_field_desc\n");
            exit(1);
        }
        nbytes += sizeof(*(def->fields));
        def->record_size += def->fields[i].size;
    }
    def->record_num = record_num;
    insert_definition(def);
}

canonical_original_code:
void fetch_record_definition(unsigned char record_num) {
    int nelms;
    struct fit_definition *def;
    def = calloc(1, sizeof(*def));
    if ((nelms = fread(&(def->record), sizeof(def->record), 1, fp)) != 1) {
        fprintf(stderr, "Unable to read fit_record_desc\n");
        exit(1);
    }
    nbytes += sizeof(def->record);
    def->fields = calloc(1, sizeof(*def->fields)*def->record.nfields);
    int i;
    for (i = 0; i < def->record.nfields; i++) {
        if ((nelms = fread(&(def->fields[i]), sizeof(*(def->fields)), 1, fp)) != 1) {
            fprintf(stderr, "Unable to read fit_field_desc\n");
            exit(1);
        }
        nbytes += sizeof(*(def->fields));
        def->record_size += def->fields[i].size;
    }
    def->record_num = record_num;
    insert_definition(def);
}

variable_types: nelms int

variable_types: struct fit_definition { unsigned char record_num; struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; } record; struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; } *fields; unsigned long record_size; } *

variable_types: i int

user_defined_types: struct fit_definition { unsigned char record_num; struct fit_record_definition record; struct fit_field_desc *fields; unsigned long record_size; }


original_code:
int read_data(void *data, size_t len, struct fit_definition *def, struct fit_record_data *record_data, int record_data_nelms) {
    int i, j;
    int nelms;
    size_t offset;
    uint8_t *buf = data;
    for (i = 0, offset = 0; i < def->record.nfields; i++) {
        if (record_data) {
            for (j = 0; j < record_data_nelms; j++) {
                if (record_data[j].index == def->fields[i].index) {
                    offset = record_data[j].offset;
                    break;
                }
            }
        }
        if (record_data == ((void *)0) || j == record_data_nelms) {
            uint8_t tmp[def->fields[i].size];
            if ((nelms = fread(tmp, sizeof(uint8_t), def->fields[i].size,fp)) != def->fields[i].size) {
                fprintf(stdout, "Unable to read unknown index\n");
            }
            nbytes += sizeof(uint8_t) * def->fields[i].size;
            continue;
        }
        if ((nelms = fread(&(buf[offset]), sizeof(uint8_t), def->fields[i].size,fp)) != def->fields[i].size) {
            fprintf(stdout, "Unable to read data at byte %d\n", nbytes);
            exit(1);
        }
        nbytes += sizeof(uint8_t) * def->fields[i].size;
    }
    return 0;
}

canonical_original_code:
int read_data(void *data, unsigned long len, struct fit_definition *def, struct fit_record_data *record_data, int record_data_nelms) {
    int i; int j;
    int nelms;
    unsigned long offset;
    unsigned char *buf = data;
    for (i = 0, offset = 0; i < def->record.nfields; i++) {
        if (record_data) {
            for (j = 0; j < record_data_nelms; j++) {
                if (record_data[j].index == def->fields[i].index) {
                    offset = record_data[j].offset;
                    break;
                }
            }
        }
        if (record_data == ((void *)0) || j == record_data_nelms) {
            unsigned char tmp[-1];
            if ((nelms = fread(tmp, sizeof(unsigned char), def->fields[i].size,fp)) != def->fields[i].size) {
                fprintf(stdout, "Unable to read unknown index\n");
            }
            nbytes += sizeof(unsigned char) * def->fields[i].size;
            continue;
        }
        if ((nelms = fread(&(buf[offset]), sizeof(unsigned char), def->fields[i].size,fp)) != def->fields[i].size) {
            fprintf(stdout, "Unable to read data at byte %d\n", nbytes);
            exit(1);
        }
        nbytes += sizeof(unsigned char) * def->fields[i].size;
    }
    return 0;
}

variable_types:
data
void *

variable_types:
len
unsigned long

variable_types: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; } *

variable_types: j int

variable_types:
offset
unsigned long

variable_types:
buf
unsigned char *

variable_types:
tmp
unsigned char [-1]

user_defined_types: struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; }

user_defined_types: struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; }


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

switch ( a1 )
{
case 0:
*a2 = 6;
result = &file_info_data;
break;
case 1:
*a2 = 2;
result = &file_creator_data;
break;
case 2:
*a2 = 5;
result = &event_data;
break;
case 3:
*a2 = 12;
result = &device_info_data;
break;
case 5:
*a2 = 13;
result = &record_data;
break;
case 6:
*a2 = 32;
result = &lap_data;
break;
case 7:
*a2 = 31;
result = &session_data;
break;
case 8:
*a2 = 6;
result = &activity_data;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 0:
*a2 = 6;
result = &file_info_data;
break;
case 1:
*a2 = 2;
result = &file_creator_data;
break;
case 2:
*a2 = 5;
result = &event_data;
break;
case 3:
*a2 = 12;
result = &device_info_data;
break;
case 5:
*a2 = 13;
result = &record_data;
break;
case 6:
*a2 = 32;
result = &lap_data;
break;
case 7:
*a2 = 31;
result = &session_data;
break;
case 8:
*a2 = 6;
result = &activity_data;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
struct fit_record_data *search_for_data(int record_num, int *data_nelms) {
    switch(record_num) {
        case FITFILE_MSGTYPE_FILE_ID:
            *data_nelms = (sizeof(file_info_data)/sizeof(file_info_data[0]));
            return file_info_data;
        case FITFILE_MSGTYPE_FILE_CREATOR:
            *data_nelms = (sizeof(file_creator_data)/sizeof(file_creator_data[0]));
            return file_creator_data;
        case FITFILE_MSGTYPE_EVENT:
            *data_nelms = (sizeof(event_data)/sizeof(event_data[0]));
            return event_data;
        case FITFILE_MSGTYPE_DEVICE_INFO:
            *data_nelms = (sizeof(device_info_data)/sizeof(device_info_data[0]));
            return device_info_data;
        case FITFILE_MSGTYPE_RECORD:
            *data_nelms = (sizeof(record_data)/sizeof(record_data[0]));
            return record_data;
        case FITFILE_MSGTYPE_LAP:
            *data_nelms = (sizeof(lap_data)/sizeof(lap_data[0]));
            return lap_data;
        case FITFILE_MSGTYPE_SESSION:
            *data_nelms = (sizeof(session_data)/sizeof(session_data[0]));
            return session_data;
        case FITFILE_MSGTYPE_ACTIVITY:
            *data_nelms = (sizeof(activity_data)/sizeof(activity_data[0]));
            return activity_data;
        default:
            return ((void *)0);
    }
}

return_type: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; } *


decompiled_code:
__int64 fit_process_file()
{
__int64 v1; // rbx
_DWORD *v2; // rcx
signed __int64 v3; // rsi
void *v4; // rsp
char v5; // al
_BYTE v6[15]; // [rsp+0h] [rbp-40h] BYREF
char v7; // [rsp+Fh] [rbp-31h] BYREF
int v8; // [rsp+10h] [rbp-30h] BYREF
unsigned __int8 ptr[4]; // [rsp+14h] [rbp-2Ch] BYREF
unsigned int v10; // [rsp+18h] [rbp-28h]

if ( (unsigned int)fread(ptr, 0xCuLL, 1uLL, fp) != 1 )
exit(1);
if ( (unsigned int)check_header(ptr) )
exit(1);
print_xml_header();
while ( v10 > nbytes )
{
fread(&v7, 1uLL, 1uLL, fp);
++nbytes;
if ( (v7 & 0x40) != 0 )
{
fetch_record_definition(v7 & 0xF);
}
else
{
v1 = search_for_def(v7 & 0xF);
if ( !v1 )
{
fwrite("Unable to find definition for message type\n", 1uLL, 0x2BuLL, stdout);
exit(1);
}
v2 = search_for_data(v7 & 0xF, &v8);
v3 = *(_QWORD *)(v1 + 16);
v4 = alloca(v3);
read_data((__int64)v6, v3, v1, v2, v8);
v5 = v7 & 0xF;
if ( (v7 & 0xF) == 6 )
{
if ( !display )
{
print_xml_open_tag("lap", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &lap_data, 32LL, stdout, 2LL);
print_xml_close_tag("lap", stdout, 1LL);
putchar(10);
}
}
else if ( v5 == 7 )
{
if ( display == 1 )
{
print_xml_open_tag("session", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &session_data, 31LL, stdout, 2LL);
print_xml_close_tag("session", stdout, 1LL);
putchar(10);
}
}
else if ( v5 == 5 && display == 2 )
{
print_xml_open_tag("record", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &record_data, 13LL, stdout, 2LL);
print_xml_close_tag("record", stdout, 1LL);
putchar(10);
}
}
}
fclose(fp);
print_xml_footer();
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v1; // rbx
_DWORD *v2; // rcx
signed __int64 v3; // rsi
void *v4; // rsp
char v5; // al
_BYTE v6[15]; // [rsp+0h] [rbp-40h] BYREF
char v7; // [rsp+Fh] [rbp-31h] BYREF
int v8; // [rsp+10h] [rbp-30h] BYREF
unsigned __int8 ptr[4]; // [rsp+14h] [rbp-2Ch] BYREF
unsigned int v10; // [rsp+18h] [rbp-28h]

if ( (unsigned int)fread(ptr, 0xCuLL, 1uLL, fp) != 1 )
exit(1);
if ( (unsigned int)func0(ptr) )
exit(1);
print_xml_header();
while ( v10 > nbytes )
{
fread(&v7, 1uLL, 1uLL, fp);
++nbytes;
if ( (v7 & 0x40) != 0 )
{
func1(v7 & 0xF);
}
else
{
v1 = search_for_def(v7 & 0xF);
if ( !v1 )
{
fwrite("Unable to find definition for message type\n", 1uLL, 0x2BuLL, stdout);
exit(1);
}
v2 = func3(v7 & 0xF, &v8);
v3 = *(_QWORD *)(v1 + 16);
v4 = alloca(v3);
func2((__int64)v6, v3, v1, v2, v8);
v5 = v7 & 0xF;
if ( (v7 & 0xF) == 6 )
{
if ( !display )
{
print_xml_open_tag("lap", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &lap_data, 32LL, stdout, 2LL);
print_xml_close_tag("lap", stdout, 1LL);
putchar(10);
}
}
else if ( v5 == 7 )
{
if ( display == 1 )
{
print_xml_open_tag("session", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &session_data, 31LL, stdout, 2LL);
print_xml_close_tag("session", stdout, 1LL);
putchar(10);
}
}
else if ( v5 == 5 && display == 2 )
{
print_xml_open_tag("record", stdout, 1LL);
putchar(10);
print_xml_record_data(v6, &record_data, 13LL, stdout, 2LL);
print_xml_close_tag("record", stdout, 1LL);
putchar(10);
}
}
}
fclose(fp);
print_xml_footer();
return 0LL;
}

original_code:
int fit_process_file(void) {
    int nelms;
    struct fit_header hdr;
    if ((nelms = fread(&hdr, sizeof(hdr), 1, fp)) != 1) {
        exit(1);
    }
    if (check_header(&hdr)) {
        exit(1);
    }
    print_xml_header();
    while (nbytes < hdr.file_size) {
        uint8_t record_hdr;
        if ((nelms = fread(&record_hdr, sizeof(record_hdr), 1, fp)) != 1) {
        }
        nbytes += sizeof(record_hdr);
        if (record_hdr & (1 << 6)) {
            fetch_record_definition(record_hdr & ((1 << 4) - 1));
            continue;
        }
        struct fit_definition *def;
        def = search_for_def(record_hdr & ((1 << 4) - 1));
        if (!def) {
            fprintf(stdout, "Unable to find definition for message type\n");
            exit(1);
        }
        struct fit_record_data *data;
        int data_len;
        data = search_for_data(record_hdr & ((1 << 4) - 1), &data_len);
        uint8_t array[def->record_size];
        read_data(array, sizeof(array), def, data, data_len);
        switch(record_hdr & ((1 << 4) - 1)) {
            case FITFILE_MSGTYPE_RECORD:
            {
                if (display == DISPLAY_RECORDS) {
                    print_xml_open_tag("record", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, record_data,
                            (sizeof(record_data)/sizeof(record_data[0])), stdout, 2);
                    print_xml_close_tag("record", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_LAP:
            {
                if (display == DISPLAY_LAPS) {
                    print_xml_open_tag("lap", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, lap_data,
                            (sizeof(lap_data)/sizeof(lap_data[0])), stdout, 2);
                    print_xml_close_tag("lap", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_SESSION:
            {
                if (display == DISPLAY_SESSIONS) {
                    print_xml_open_tag("session", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, session_data,
                            (sizeof(session_data)/sizeof(session_data[0])), stdout, 2);
                    print_xml_close_tag("session", stdout, 1);
                    printf("\n");
                }
            }
            break;
            default:
                break;
        }
    }
    fclose(fp);
    print_xml_footer();
    return 0;
}

canonical_original_code:
int fit_process_file() {
    int nelms;
    struct fit_header hdr;
    if ((nelms = fread(&hdr, sizeof(hdr), 1, fp)) != 1) {
        exit(1);
    }
    if (check_header(&hdr)) {
        exit(1);
    }
    print_xml_header();
    while (nbytes < hdr.file_size) {
        unsigned char record_hdr;
        if ((nelms = fread(&record_hdr, sizeof(record_hdr), 1, fp)) != 1) {
        }
        nbytes += sizeof(record_hdr);
        if (record_hdr & (1 << 6)) {
            fetch_record_definition(record_hdr & ((1 << 4) - 1));
            continue;
        }
        struct fit_definition *def;
        def = search_for_def(record_hdr & ((1 << 4) - 1));
        if (!def) {
            fprintf(stdout, "Unable to find definition for message type\n");
            exit(1);
        }
        struct fit_record_data *data;
        int data_len;
        data = search_for_data(record_hdr & ((1 << 4) - 1), &data_len);
        unsigned char array[-1];
        read_data(array, sizeof(array), def, data, data_len);
        switch(record_hdr & ((1 << 4) - 1)) {
            case FITFILE_MSGTYPE_RECORD:
            {
                if (display == DISPLAY_RECORDS) {
                    print_xml_open_tag("record", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, record_data,
                            (sizeof(record_data)/sizeof(record_data[0])), stdout, 2);
                    print_xml_close_tag("record", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_LAP:
            {
                if (display == DISPLAY_LAPS) {
                    print_xml_open_tag("lap", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, lap_data,
                            (sizeof(lap_data)/sizeof(lap_data[0])), stdout, 2);
                    print_xml_close_tag("lap", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_SESSION:
            {
                if (display == DISPLAY_SESSIONS) {
                    print_xml_open_tag("session", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, session_data,
                            (sizeof(session_data)/sizeof(session_data[0])), stdout, 2);
                    print_xml_close_tag("session", stdout, 1);
                    printf("\n");
                }
            }
            break;
            default:
                break;
        }
    }
    fclose(fp);
    print_xml_footer();
    return 0;
}

variable_types: struct fit_header { unsigned char h_len; unsigned char protocol_version; unsigned char profile_version; unsigned char xxx2; unsigned int file_size; unsigned char sig[4]; }

variable_types:
array
unsigned char [-1]

user_defined_types: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; }


printUsage

decompiled_code:
void __noreturn printUsage()
{
fprintf(
stdout,
"Usage: %s [options] filename\n"
" -l --laps\n"
" -s --session\n"
" -r --records\n"
" -v --verbose\n"
" -h --help\n"
"\n"
"\n"
"%s version %s. \n"
"Author: %s\n"
"Report bugs to %s",
"fitdecode",
"fitdecode",
"0.1",
"Paul Archer",
"ptarcher@gmail.com");
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fprintf(
stdout,
"Usage: %s [options] filename\n"
" -l --laps\n"
" -s --session\n"
" -r --records\n"
" -v --verbose\n"
" -h --help\n"
"\n"
"\n"
"%s version %s. \n"
"Author: %s\n"
"Report bugs to %s",
"fitdecode",
"fitdecode",
"0.1",
"Paul Archer",
"ptarcher@gmail.com");
exit(1);
}

original_code:
void printUsage(void) {
    fprintf(stdout, "Usage: %s [options] filename\n"
                    "       -l --laps\n"
                    "       -s --session\n"
                    "       -r --records\n"
                    "       -v --verbose\n"
                    "       -h --help\n"
                    "\n\n"
                    "%s version %s. \n"
                    "Author: %s\n"
                    "Report bugs to %s",
                    "fitdecode",
                    "fitdecode",
                    "0.1",
                    "Paul Archer",
                    "ptarcher@gmail.com");
    exit(1);
}

canonical_original_code:
void printUsage() {
    fprintf(stdout, "Usage: %s [options] filename\n"
                    "       -l --laps\n"
                    "       -s --session\n"
                    "       -r --records\n"
                    "       -v --verbose\n"
                    "       -h --help\n"
                    "\n\n"
                    "%s version %s. \n"
                    "Author: %s\n"
                    "Report bugs to %s",
                    "fitdecode",
                    "fitdecode",
                    "0.1",
                    "Paul Archer",
                    "ptarcher@gmail.com");
    exit(1);
}


parseArgs

decompiled_code:
__int64 __fastcall parseArgs(int argc, char **argv)
{
int v2; // eax
int v3; // er13
const char **i; // r12
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

fp = (__int64)stdin;
while ( 1 )
{
v2 = getopt_long(argc, argv, "lsrhv", &long_options_0, longind);
if ( v2 == -1 )
break;
if ( v2 )
printUsage();
printf("long option %s", *(&long_options_0.name + 4 * longind[0]));
if ( optarg )
printf(" with arg %s", optarg);
putchar(10);
}
v3 = optind;
if ( argc > optind )
{
for ( i = (const char **)&argv[optind]; ; ++i )
{
if ( (struct _IO_FILE *)fp != stdin )
{
fprintf(stderr, "more then one filename given %s\n", argv[v3]);
exit(1);
}
fp = (__int64)fopen(*i, "r");
if ( !fp )
break;
if ( argc == ++v3 )
return 0LL;
}
perror(*i);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
int v2; // eax
int v3; // er13
const char **i; // r12
int longind[11]; // [rsp+Ch] [rbp-2Ch] BYREF

fp = (__int64)stdin;
while ( 1 )
{
v2 = getopt_long(argc, argv, "lsrhv", &long_options_0, longind);
if ( v2 == -1 )
break;
if ( v2 )
func0();
printf("long option %s", *(&long_options_0.name + 4 * longind[0]));
if ( optarg )
printf(" with arg %s", optarg);
putchar(10);
}
v3 = optind;
if ( argc > optind )
{
for ( i = (const char **)&argv[optind]; ; ++i )
{
if ( (struct _IO_FILE *)fp != stdin )
{
fprintf(stderr, "more then one filename given %s\n", argv[v3]);
exit(1);
}
fp = (__int64)fopen(*i, "r");
if ( !fp )
break;
if ( argc == ++v3 )
return 0LL;
}
perror(*i);
}
return 0LL;
}

original_code:
int parseArgs(int argc, char *argv[]) {
    int c;
    int i;
    fp = stdin;
    while (1) {
        int this_option_optind;
        int option_index;
        static struct option long_options[] = {
            {"laps", 0, 0, 'l'},
            {"session", 0, 0, 's'},
            {"records", 0, 0, 'r'},
            {0, 0, 0, 0},
        };
        this_option_optind = optind ? optind : 1;
        c = getopt_long(argc, argv, "lsrhv", long_options, &option_index);
        if (c == -1) {
            break;
        }
        switch(c) {
            case 0:
                printf("long option %s", long_options[option_index].name);
                if (optarg) {
                    printf(" with arg %s", optarg);
                }
                printf("\n");
                break;
            case 'l':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_LAPS;
                break;
            case 's':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_SESSIONS;
                break;
            case 'r':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_RECORDS;
                break;
            case 'h':
                printUsage();
                break;
            case 'v':
                flag_verbose = 1;
                break;
            default:
                printUsage();
                break;
        }
    }
    for (i = optind; i < argc; i++) {
        if (fp != stdin) {
            fprintf(stderr, "more then one filename given %s\n", argv[i]);
            exit(1);
        }
        fp = fopen(argv[i], "r");
        if (fp == ((void *)0)) {
            perror(argv[i]);
            return 0;
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init_definitions(argc, argv, envp);
parseArgs(argc, (char **)argv);
fit_process_file();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
init_definitions(argc, argv, envp);
func1(argc, (char **)argv);
fit_process_file();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    init_definitions();
    parseArgs(argc, argv);
    fit_process_file();
    return 0;
}


decompiled_code:
int print_xml_header()
{
switch ( display )
{
case 1:
print_xml_open_tag("sessions", stdout, 0);
break;
case 2:
print_xml_open_tag("records", stdout, 0);
break;
case 0:
print_xml_open_tag("laps", stdout, 0);
break;
}
return putchar(10);
}

canonical_decompiled_code:
int func2()
{
switch ( display )
{
case 1:
func1("sessions", stdout, 0);
break;
case 2:
func1("records", stdout, 0);
break;
case 0:
func1("laps", stdout, 0);
break;
}
return putchar(10);
}

original_code:
void print_xml_header(void) {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_open_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_open_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_open_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}

canonical_original_code:
void print_xml_header() {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_open_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_open_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_open_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}


decompiled_code:
int print_xml_footer()
{
switch ( display )
{
case 1:
print_xml_close_tag("sessions", stdout, 0);
break;
case 2:
print_xml_close_tag("records", stdout, 0);
break;
case 0:
print_xml_close_tag("laps", stdout, 0);
break;
}
return putchar(10);
}

canonical_decompiled_code:
int func5()
{
switch ( display )
{
case 1:
func4("sessions", stdout, 0);
break;
case 2:
func4("records", stdout, 0);
break;
case 0:
func4("laps", stdout, 0);
break;
}
return putchar(10);
}

original_code:
void print_xml_footer(void) {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_close_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_close_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_close_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}

canonical_original_code:
void print_xml_footer() {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_close_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_close_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_close_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}


decompiled_code:
void *init_definitions()
{
void *result; // rax

*((_DWORD *)&defs + 2) = 0;
result = malloc(8uLL);
defs = result;
*((_DWORD *)&defs + 3) = 1;
return result;
}

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

*((_DWORD *)&defs + 2) = 0;
result = malloc(8uLL);
defs = result;
*((_DWORD *)&defs + 3) = 1;
return result;
}

original_code:
void init_definitions(void) {
    defs.nelms = 0;
    defs.defs = malloc(sizeof(*(defs.defs)));
    defs.size = 1;
}

canonical_original_code:
void init_definitions() {
    defs.nelms = 0;
    defs.defs = malloc(sizeof(*(defs.defs)));
    defs.size = 1;
}


decompiled_code:
void *__fastcall insert_definition(__int64 a1)
{
int v1; // edx
void *result; // rax
int v3; // ebx

v1 = *((_DWORD *)&defs + 3);
if ( v1 != *((_DWORD *)&defs + 2) + 1 )
goto LABEL_2;
v3 = 2 * v1;
result = realloc(defs, 16LL * v1);
if ( result )
{
defs = result;
*((_DWORD *)&defs + 3) = v3;
LABEL_2:
LODWORD(result) = *((_DWORD *)&defs + 2);
*((_DWORD *)&defs + 2) = (_DWORD)result + 1;
result = (void *)(int)result;
*((_QWORD *)defs + (int)result) = a1;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
int v1; // edx
void *result; // rax
int v3; // ebx

v1 = *((_DWORD *)&defs + 3);
if ( v1 != *((_DWORD *)&defs + 2) + 1 )
goto LABEL_2;
v3 = 2 * v1;
result = realloc(defs, 16LL * v1);
if ( result )
{
defs = result;
*((_DWORD *)&defs + 3) = v3;
LABEL_2:
LODWORD(result) = *((_DWORD *)&defs + 2);
*((_DWORD *)&defs + 2) = (_DWORD)result + 1;
result = (void *)(int)result;
*((_QWORD *)defs + (int)result) = a1;
}
return result;
}

original_code:
void insert_definition(struct fit_definition *def) {
    if (defs.size == (defs.nelms + 1)) {
        int new_size;
        struct fit_definition **new_defs;
        new_size = defs.size * 2;
        new_defs = realloc(defs.defs, sizeof(*new_defs)*new_size);
        if (new_defs == ((void *)0)) {
            return;
        }
        defs.defs = new_defs;
        defs.size = new_size;
    }
    defs.defs[defs.nelms++] = def;
}

variable_types: struct fit_definition { unsigned char record_num; struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; } record; struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; } *fields; unsigned long record_size; } **


decompiled_code:
void print_csv_header()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void print_csv_header(csv_display_t display) {
    int i;
}

canonical_original_code:
void print_csv_header(enum csv_display_t display) {
    int i;
}

variable_types: enum csv_display_t { DISPLAY_LAPS=0, DISPLAY_SESSIONS=1, DISPLAY_RECORDS=2 }


strrindex

repo: ptdecker/cbasics

decompiled_code:
__int64 __fastcall strrindex(const char *a1, const char *a2)
{
size_t v3; // r12
size_t v4; // rsi
const char *v5; // rcx
char v6; // dl
__int64 v7; // rax

v3 = strlen(a1);
v4 = v3 + 1 - strlen(a2);
v5 = &a1[v4];
while ( v4 )
{
v6 = *a2;
v7 = 0LL;
if ( *a2 )
{
do
{
if ( v5[v7 - 1] != v6 )
break;
v6 = a2[++v7];
}
while ( v6 );
if ( !v6 && v7 )
return (unsigned int)(v4 - 1);
}
--v5;
--v4;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
size_t v3; // r12
size_t v4; // rsi
const char *v5; // rcx
char v6; // dl
__int64 v7; // rax

v3 = strlen(a1);
v4 = v3 + 1 - strlen(a2);
v5 = &a1[v4];
while ( v4 )
{
v6 = *a2;
v7 = 0LL;
if ( *a2 )
{
do
{
if ( v5[v7 - 1] != v6 )
break;
v6 = a2[++v7];
}
while ( v6 );
if ( !v6 && v7 )
return (unsigned int)(v4 - 1);
}
--v5;
--v4;
}
return 0xFFFFFFFFLL;
}

original_code:
static int strrindex(char s[], char t[]) {
    size_t i;
    size_t j;
    size_t k;
    for (i = (strlen(s) - strlen(t) + 1); i > 0; i--) {
        for (j = (i - 1), k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)(i - 1);
    }
    return -1;
}

canonical_original_code:
static int strrindex(char s[], char t[]) {
    unsigned long i;
    unsigned long j;
    unsigned long k;
    for (i = (strlen(s) - strlen(t) + 1); i > 0; i--) {
        for (j = (i - 1), k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)(i - 1);
    }
    return -1;
}

variable_types:
s
char []

variable_types:
t
char []

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
k
unsigned long


strindex

decompiled_code:
__int64 __fastcall strindex(_BYTE *a1, char *a2)
{
unsigned int v2; // ecx
char v3; // dl
__int64 v4; // rax

if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
v3 = *a2;
v4 = 0LL;
if ( *a2 )
{
do
{
if ( a1[v4] != v3 )
break;
v3 = a2[++v4];
}
while ( v3 );
if ( !v3 && v4 )
return v2;
}
++v2;
++a1;
}
while ( *a1 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, char *a2)
{
unsigned int v2; // ecx
char v3; // dl
__int64 v4; // rax

if ( !*a1 )
return 0xFFFFFFFFLL;
v2 = 0;
do
{
v3 = *a2;
v4 = 0LL;
if ( *a2 )
{
do
{
if ( a1[v4] != v3 )
break;
v3 = a2[++v4];
}
while ( v3 );
if ( !v3 && v4 )
return v2;
}
++v2;
++a1;
}
while ( *a1 );
return 0xFFFFFFFFLL;
}

original_code:
static int strindex(char s[], char t[]) {
    size_t i;
    size_t j;
    size_t k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)i;
    }
    return -1;
}

canonical_original_code:
static int strindex(char s[], char t[]) {
    unsigned long i;
    unsigned long j;
    unsigned long k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)i;
    }
    return -1;
}