decompiled_code:
__int64 __fastcall connection_on_out_event(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
int v3; // esi
int v4; // ebx
int v5; // eax
int v6; // edx
__int64 v7; // rbx
int v8; // eax

v1 = *(_QWORD *)(a1 + 16);
result = *(_QWORD *)(v1 + 32);
if ( result )
{
while ( 1 )
{
if ( *(_DWORD *)result )
return connection_really_close(a1);
v3 = *(_DWORD *)(result + 16);
v4 = *(_DWORD *)(result + 20) - v3;
v5 = write(*(_DWORD *)a1, (const void *)(*(_QWORD *)(result + 8) + v3), v4);
v6 = v5;
if ( v4 != v5 )
break;
v7 = *(_QWORD *)(v1 + 32);
*(_QWORD *)(v1 + 32) = *(_QWORD *)(v7 + 24);
epoll_add_to_free_list(*(_QWORD *)(v7 + 8));
epoll_add_to_free_list(v7);
result = *(_QWORD *)(v1 + 32);
if ( !result )
return result;
}
if ( v5 != -1 )
goto LABEL_9;
v8 = *_errno_location();
if ( v8 != 104 && v8 != 32 )
{
if ( v8 != 11 )
{
rsp_log_error("Error writing to client");
exit(-1);
}
v6 = 0;
LABEL_9:
result = *(_QWORD *)(v1 + 32);
*(_DWORD *)(result + 16) += v6;
return result;
}
rsp_log_error("On out event write error");
result = connection_on_close_event(a1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // rbp
__int64 result; // rax
int v3; // esi
int v4; // ebx
int v5; // eax
int v6; // edx
__int64 v7; // rbx
int v8; // eax

v1 = *(_QWORD *)(a1 + 16);
result = *(_QWORD *)(v1 + 32);
if ( result )
{
while ( 1 )
{
if ( *(_DWORD *)result )
return func0(a1);
v3 = *(_DWORD *)(result + 16);
v4 = *(_DWORD *)(result + 20) - v3;
v5 = write(*(_DWORD *)a1, (const void *)(*(_QWORD *)(result + 8) + v3), v4);
v6 = v5;
if ( v4 != v5 )
break;
v7 = *(_QWORD *)(v1 + 32);
*(_QWORD *)(v1 + 32) = *(_QWORD *)(v7 + 24);
epoll_add_to_free_list(*(_QWORD *)(v7 + 8));
epoll_add_to_free_list(v7);
result = *(_QWORD *)(v1 + 32);
if ( !result )
return result;
}
if ( v5 != -1 )
goto LABEL_9;
v8 = *_errno_location();
if ( v8 != 104 && v8 != 32 )
{
if ( v8 != 11 )
{
rsp_log_error("Error writing to client");
exit(-1);
}
v6 = 0;
LABEL_9:
result = *(_QWORD *)(v1 + 32);
*(_DWORD *)(result + 16) += v6;
return result;
}
rsp_log_error("On out event write error");
result = func3(a1);
}
return result;
}

original_code:
void connection_on_out_event(struct epoll_event_handler* self)
{
    struct connection_closure* closure = (struct connection_closure*) self->closure;
    int written;
    int to_write;
    struct data_buffer_entry* temp;
    while (closure->write_buffer != ((void *)0)) {
        if (closure->write_buffer->is_close_message) {
            connection_really_close(self);
            return;
        }
        to_write = closure->write_buffer->len - closure->write_buffer->current_offset;
        written = write(self->fd, closure->write_buffer->data + closure->write_buffer->current_offset, to_write);
        if (written != to_write) {
            if (written == -1) {
                if ((*__errno_location ()) == 104 || (*__errno_location ()) == 32) {
                    rsp_log_error("On out event write error");
                    connection_on_close_event(self);
                    return;
                }
                if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
                    rsp_log_error("Error writing to client");
                    exit(-1);
                }
                written = 0;
            }
            closure->write_buffer->current_offset += written;
            break;
        } else {
            temp = closure->write_buffer;
            closure->write_buffer = closure->write_buffer->next;
            epoll_add_to_free_list(temp->data);
            epoll_add_to_free_list(temp);
        }
    }
}

canonical_original_code:
void connection_on_out_event(struct epoll_event_handler *self)
{
    struct connection_closure *closure = (struct connection_closure*) self->closure;
    int written;
    int to_write;
    struct data_buffer_entry *temp;
    while (closure->write_buffer != ((void *)0)) {
        if (closure->write_buffer->is_close_message) {
            connection_really_close(self);
            return;
        }
        to_write = closure->write_buffer->len - closure->write_buffer->current_offset;
        written = write(self->fd, closure->write_buffer->data + closure->write_buffer->current_offset, to_write);
        if (written != to_write) {
            if (written == -1) {
                if ((*__errno_location ()) == 104 || (*__errno_location ()) == 32) {
                    rsp_log_error("On out event write error");
                    connection_on_close_event(self);
                    return;
                }
                if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
                    rsp_log_error("Error writing to client");
                    exit(-1);
                }
                written = 0;
            }
            closure->write_buffer->current_offset += written;
            break;
        } else {
            temp = closure->write_buffer;
            closure->write_buffer = closure->write_buffer->next;
            epoll_add_to_free_list(temp->data);
            epoll_add_to_free_list(temp);
        }
    }
}

variable_types: written int


repo: gpjt/rsp

decompiled_code:
ssize_t __fastcall connection_on_in_event(__int64 a1)
{
__int64 v1; // rbp
ssize_t result; // rax
_BYTE v3[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = *(_QWORD *)(a1 + 16);
while ( 1 )
{
result = read(*(_DWORD *)a1, v3, 0x1000uLL);
if ( (unsigned int)(result + 1) <= 1 )
break;
if ( *(_QWORD *)v1 )
(*(void (__fastcall **)(_QWORD, _BYTE *, _QWORD))v1)(*(_QWORD *)(v1 + 8), v3, (unsigned int)result);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(__int64 a1)
{
__int64 v1; // rbp
ssize_t result; // rax
_BYTE v3[4120]; // [rsp+0h] [rbp-1018h] BYREF

v1 = *(_QWORD *)(a1 + 16);
while ( 1 )
{
result = read(*(_DWORD *)a1, v3, 0x1000uLL);
if ( (unsigned int)(result + 1) <= 1 )
break;
if ( *(_QWORD *)v1 )
(*(void (__fastcall **)(_QWORD, _BYTE *, _QWORD))v1)(*(_QWORD *)(v1 + 8), v3, (unsigned int)result);
}
return result;
}

original_code:
void connection_on_in_event(struct epoll_event_handler* self)
{
    struct connection_closure* closure = (struct connection_closure*) self->closure;
    char read_buffer[4096];
    int bytes_read;
    while ((bytes_read = read(self->fd, read_buffer, 4096)) != -1 && bytes_read != 0) {
        if (bytes_read == -1 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
            return;
        }
        if (bytes_read == 0 || bytes_read == -1) {
            connection_on_close_event(self);
            return;
        }
        if (closure->on_read != ((void *)0)) {
            closure->on_read(closure->on_read_closure, read_buffer, bytes_read);
        }
    }
}

canonical_original_code:
void connection_on_in_event(struct epoll_event_handler *self)
{
    struct connection_closure *closure = (struct connection_closure*) self->closure;
    char read_buffer[4096];
    int bytes_read;
    while ((bytes_read = read(self->fd, read_buffer, 4096)) != -1 && bytes_read != 0) {
        if (bytes_read == -1 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
            return;
        }
        if (bytes_read == 0 || bytes_read == -1) {
            connection_on_close_event(self);
            return;
        }
        if (closure->on_read != ((void *)0)) {
            closure->on_read(closure->on_read_closure, read_buffer, bytes_read);
        }
    }
}

variable_types: struct epoll_event_handler { int fd; void (*handle)(struct epoll_event_handler *, unsigned int); void *closure; } *

variable_types: struct connection_closure { void (*on_read)(void *closure, char *buffer, int len); void *on_read_closure; void (*on_close)(void *closure); void *on_close_closure; struct data_buffer_entry { int is_close_message; char *data; int current_offset; int len; struct data_buffer_entry *next; } *write_buffer; } *

return_type: void

user_defined_types: struct connection_closure { void (*on_read)(void *closure, char *buffer, int len); void *on_read_closure; void (*on_close)(void *closure); void *on_close_closure; struct data_buffer_entry *write_buffer; }


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

if ( (a2 & 4) != 0 )
result = connection_on_out_event(a1);
if ( (a2 & 1) != 0 )
result = connection_on_in_event(a1);
if ( (a2 & 0x2018) != 0 )
result = connection_on_close_event(a1);
return result;
}

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

if ( (a2 & 4) != 0 )
result = func4(a1);
if ( (a2 & 1) != 0 )
result = func5(a1);
if ( (a2 & 0x2018) != 0 )
result = func3(a1);
return result;
}

original_code:
void connection_handle_event(struct epoll_event_handler* self, uint32_t events)
{
    if (events & EPOLLOUT) {
        connection_on_out_event(self);
    }
    if (events & EPOLLIN) {
        connection_on_in_event(self);
    }
    if ((events & EPOLLERR) | (events & EPOLLHUP) | (events & EPOLLRDHUP)) {
        connection_on_close_event(self);
    }
}

canonical_original_code:
void connection_handle_event(struct epoll_event_handler *self, unsigned int events)
{
    if (events & EPOLLOUT) {
        connection_on_out_event(self);
    }
    if (events & EPOLLIN) {
        connection_on_in_event(self);
    }
    if ((events & EPOLLERR) | (events & EPOLLHUP) | (events & EPOLLRDHUP)) {
        connection_on_close_event(self);
    }
}

variable_types:
events
unsigned int

user_defined_types: struct epoll_event_handler { int fd; void (*handle)(struct epoll_event_handler *, unsigned int); void *closure; }


decompiled_code:
ssize_t __fastcall connection_write(__int64 a1, char *a2, int a3)
{
__int64 v4; // r14
int v5; // er13
int v6; // ebp
_QWORD *v7; // rbx
void *v8; // rax
ssize_t result; // rax
int v10; // eax

v4 = *(_QWORD *)(a1 + 16);
v5 = 0;
if ( *(_QWORD *)(v4 + 32) )
{
LABEL_2:
v6 = a3 - v5;
v7 = malloc(0x20uLL);
*(_DWORD *)v7 = 0;
v8 = malloc(v6);
v7[1] = v8;
memcpy(v8, &a2[v5], v6);
*((_DWORD *)v7 + 4) = 0;
*((_DWORD *)v7 + 5) = v6;
v7[3] = 0LL;
return add_write_buffer_entry(v4, (__int64)v7);
}
result = write(*(_DWORD *)a1, a2, a3);
v5 = result;
if ( a3 != (_DWORD)result )
{
if ( (_DWORD)result == -1 )
{
v10 = *_errno_location();
if ( v10 == 104 || v10 == 32 )
{
rsp_log_error("Connection write error");
return connection_on_close_event(a1);
}
if ( v10 != 11 )
{
rsp_log_error("Error writing to client");
exit(-1);
}
v5 = 0;
}
goto LABEL_2;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func7(__int64 a1, char *a2, int a3)
{
__int64 v4; // r14
int v5; // er13
int v6; // ebp
_QWORD *v7; // rbx
void *v8; // rax
ssize_t result; // rax
int v10; // eax

v4 = *(_QWORD *)(a1 + 16);
v5 = 0;
if ( *(_QWORD *)(v4 + 32) )
{
LABEL_2:
v6 = a3 - v5;
v7 = malloc(0x20uLL);
*(_DWORD *)v7 = 0;
v8 = malloc(v6);
v7[1] = v8;
memcpy(v8, &a2[v5], v6);
*((_DWORD *)v7 + 4) = 0;
*((_DWORD *)v7 + 5) = v6;
v7[3] = 0LL;
return func1(v4, (__int64)v7);
}
result = write(*(_DWORD *)a1, a2, a3);
v5 = result;
if ( a3 != (_DWORD)result )
{
if ( (_DWORD)result == -1 )
{
v10 = *_errno_location();
if ( v10 == 104 || v10 == 32 )
{
rsp_log_error("Connection write error");
return func3(a1);
}
if ( v10 != 11 )
{
rsp_log_error("Error writing to client");
exit(-1);
}
v5 = 0;
}
goto LABEL_2;
}
return result;
}

original_code:
void connection_write(struct epoll_event_handler* self, char* data, int len)
{
    struct connection_closure* closure = (struct connection_closure* ) self->closure;
    int written = 0;
    if (closure->write_buffer == ((void *)0)) {
        written = write(self->fd, data, len);
        if (written == len) {
            return;
        }
    }
    if (written == -1) {
        if ((*__errno_location ()) == 104 || (*__errno_location ()) == 32) {
            rsp_log_error("Connection write error");
            connection_on_close_event(self);
            return;
        }
        if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
            rsp_log_error("Error writing to client");
            exit(-1);
        }
        written = 0;
    }
    int unwritten = len - written;
    struct data_buffer_entry* new_entry = malloc(sizeof(struct data_buffer_entry));
    new_entry->is_close_message = 0;
    new_entry->data = malloc(unwritten);
    memcpy(new_entry->data, data + written, unwritten);
    new_entry->current_offset = 0;
    new_entry->len = unwritten;
    new_entry->next = ((void *)0);
    add_write_buffer_entry(closure, new_entry);
}

canonical_original_code:
void connection_write(struct epoll_event_handler *self, char *data, int len)
{
    struct connection_closure *closure = (struct connection_closure* ) self->closure;
    int written = 0;
    if (closure->write_buffer == ((void *)0)) {
        written = write(self->fd, data, len);
        if (written == len) {
            return;
        }
    }
    if (written == -1) {
        if ((*__errno_location ()) == 104 || (*__errno_location ()) == 32) {
            rsp_log_error("Connection write error");
            connection_on_close_event(self);
            return;
        }
        if ((*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
            rsp_log_error("Error writing to client");
            exit(-1);
        }
        written = 0;
    }
    int unwritten = len - written;
    struct data_buffer_entry *new_entry = malloc(sizeof(struct data_buffer_entry));
    new_entry->is_close_message = 0;
    new_entry->data = malloc(unwritten);
    memcpy(new_entry->data, data + written, unwritten);
    new_entry->current_offset = 0;
    new_entry->len = unwritten;
    new_entry->next = ((void *)0);
    add_write_buffer_entry(closure, new_entry);
}

variable_types:
data
char *

variable_types: len int

variable_types: unwritten int

variable_types: struct data_buffer_entry { int is_close_message; char *data; int current_offset; int len; struct data_buffer_entry *next; } *


decompiled_code:
int rsp_log(char *format, ...)
{
struct tm *v1; // rax
gcc_va_list arg; // [rsp+8h] [rbp-160h] BYREF
struct timeval tv; // [rsp+20h] [rbp-148h] BYREF
char v5[64]; // [rsp+30h] [rbp-138h] BYREF
char s[64]; // [rsp+70h] [rbp-F8h] BYREF

gettimeofday(&tv, 0LL);
v1 = localtime(&tv.tv_sec);
if ( v1 )
{
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S.%%06u %z", v1);
snprintf(v5, 0x40uLL, s, tv.tv_usec);
fprintf(stdout, "[%s] ", v5);
}
va_start(arg, format);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
struct tm *v1; // rax
gcc_va_list arg; // [rsp+8h] [rbp-160h] BYREF
struct timeval tv; // [rsp+20h] [rbp-148h] BYREF
char v5[64]; // [rsp+30h] [rbp-138h] BYREF
char s[64]; // [rsp+70h] [rbp-F8h] BYREF

gettimeofday(&tv, 0LL);
v1 = localtime(&tv.tv_sec);
if ( v1 )
{
strftime(s, 0x40uLL, "%Y-%m-%d %H:%M:%S.%%06u %z", v1);
snprintf(v5, 0x40uLL, s, tv.tv_usec);
fprintf(stdout, "[%s] ", v5);
}
va_start(arg, format);
vfprintf(stdout, format, arg);
fputc(10, stdout);
return fflush(stdout);
}

original_code:
void rsp_log(char* format, ...)
{
    char without_ms[64];
    char with_ms[64];
    struct timeval tv;
    struct tm *tm;
    gettimeofday(&tv, ((void *)0));
    if ((tm = localtime(&tv.tv_sec)) != ((void *)0))
    {
        strftime(without_ms, sizeof(without_ms), "%Y-%m-%d %H:%M:%S.%%06u %z", tm);
        snprintf(with_ms, sizeof(with_ms), without_ms, tv.tv_usec);
        fprintf(stdout, "[%s] ", with_ms);
    }
    va_list argptr;
    __builtin_va_start(argptr,format);
    vfprintf(stdout, format, argptr);
    __builtin_va_end(argptr);
    fprintf(stdout, "\n");
    fflush(stdout);
}

canonical_original_code:
void rsp_log(char *format, ...)
{
    char without_ms[64];
    char with_ms[64];
    struct timeval tv;
    struct tm *tm;
    gettimeofday(&tv, ((void *)0));
    if ((tm = localtime(&tv.tv_sec)) != ((void *)0))
    {
        strftime(without_ms, sizeof(without_ms), "%Y-%m-%d %H:%M:%S.%%06u %z", tm);
        snprintf(with_ms, sizeof(with_ms), without_ms, tv.tv_usec);
        fprintf(stdout, "[%s] ", with_ms);
    }
    __builtin_va_list argptr;
    __builtin_va_start(argptr,format);
    vfprintf(stdout, format, argptr);
    __builtin_va_end(argptr);
    fprintf(stdout, "\n");
    fflush(stdout);
}

variable_types:
format
char *

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

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


decompiled_code:
int __fastcall rsp_log_error(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return rsp_log("%s: %s", a1, v2);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
return func0("%s: %s", a1, v2);
}

original_code:
void rsp_log_error(char* message)
{
    char* error = strerror((*__errno_location ()));
    rsp_log("%s: %s", message, error);
}

canonical_original_code:
void rsp_log_error(char *message)
{
    char *error = strerror((*__errno_location ()));
    rsp_log("%s: %s", message, error);
}

variable_types:
message
char *

variable_types:
error
char *


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

v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 )
{
rsp_log_error("Couldn't get socket flags");
exit(1);
}
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, v1);
if ( result == -1 )
{
rsp_log_error("Couldn't set socket flags");
exit(-1);
}
return result;
}

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

v1 = fcntl(fd, 3, 0LL);
if ( v1 == -1 )
{
rsp_log_error("Couldn't get socket flags");
exit(1);
}
BYTE1(v1) |= 8u;
result = fcntl(fd, 4, v1);
if ( result == -1 )
{
rsp_log_error("Couldn't set socket flags");
exit(-1);
}
return result;
}

original_code:
void make_socket_non_blocking(int socket_fd)
{
    int flags;
    flags = fcntl(socket_fd, 3, 0);
    if (flags == -1) {
        rsp_log_error("Couldn't get socket flags");
        exit(1);
    }
    flags |= 04000;
    if (fcntl(socket_fd, 4, flags) == -1) {
        rsp_log_error("Couldn't set socket flags");
        exit(-1);
    }
}

variable_types: flags int


decompiled_code:
__int64 __fastcall connect_to_backend(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *v3; // rbx
const char *v4; // rax
int v5; // eax
unsigned int v6; // ebp
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v2 = getaddrinfo(a1, a2, &req, &pai);
if ( v2 )
{
if ( v2 == -11 )
{
rsp_log_error("Couldn't find backend");
}
else
{
v4 = gai_strerror(v2);
rsp_log("Couldn't find backend: %s", v4);
}
exit(1);
}
v3 = pai;
if ( !pai )
{
LABEL_3:
rsp_log("Couldn't connect to backend");
exit(1);
}
while ( 1 )
{
v5 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v6 = v5;
if ( v5 != -1 )
break;
LABEL_8:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_3;
}
if ( connect(v5, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v6);
goto LABEL_8;
}
freeaddrinfo(pai);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
int v2; // eax
struct addrinfo *v3; // rbx
const char *v4; // rax
int v5; // eax
unsigned int v6; // ebp
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
v2 = getaddrinfo(a1, a2, &req, &pai);
if ( v2 )
{
if ( v2 == -11 )
{
rsp_log_error("Couldn't find backend");
}
else
{
v4 = gai_strerror(v2);
rsp_log("Couldn't find backend: %s", v4);
}
exit(1);
}
v3 = pai;
if ( !pai )
{
LABEL_3:
rsp_log("Couldn't connect to backend");
exit(1);
}
while ( 1 )
{
v5 = socket(v3->ai_family, v3->ai_socktype, v3->ai_protocol);
v6 = v5;
if ( v5 != -1 )
break;
LABEL_8:
v3 = v3->ai_next;
if ( !v3 )
goto LABEL_3;
}
if ( connect(v5, v3->ai_addr, v3->ai_addrlen) == -1 )
{
close(v6);
goto LABEL_8;
}
freeaddrinfo(pai);
return v6;
}

original_code:
int connect_to_backend(char* backend_host,
                       char* backend_port_str)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    int getaddrinfo_error;
    struct addrinfo* addrs;
    getaddrinfo_error = getaddrinfo(backend_host, backend_port_str, &hints, &addrs);
    if (getaddrinfo_error != 0) {
        if (getaddrinfo_error == -11) {
            rsp_log_error("Couldn't find backend");
        } else {
            rsp_log("Couldn't find backend: %s", gai_strerror(getaddrinfo_error));
        }
        exit(1);
    }
    int backend_socket_fd;
    struct addrinfo* addrs_iter;
    for (addrs_iter = addrs;
         addrs_iter != ((void *)0);
         addrs_iter = addrs_iter->ai_next)
    {
        backend_socket_fd = socket(addrs_iter->ai_family,
                                   addrs_iter->ai_socktype,
                                   addrs_iter->ai_protocol);
        if (backend_socket_fd == -1) {
            continue;
        }
        if (connect(backend_socket_fd,
                    addrs_iter->ai_addr,
                    addrs_iter->ai_addrlen) != -1) {
            break;
        }
        close(backend_socket_fd);
    }
    if (addrs_iter == ((void *)0)) {
        rsp_log("Couldn't connect to backend");
        exit(1);
    }
    freeaddrinfo(addrs);
    return backend_socket_fd;
}

canonical_original_code:
int connect_to_backend(char *backend_host, char *backend_port_str)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = 0;
    hints.ai_socktype = SOCK_STREAM;
    int getaddrinfo_error;
    struct addrinfo *addrs;
    getaddrinfo_error = getaddrinfo(backend_host, backend_port_str, &hints, &addrs);
    if (getaddrinfo_error != 0) {
        if (getaddrinfo_error == -11) {
            rsp_log_error("Couldn't find backend");
        } else {
            rsp_log("Couldn't find backend: %s", gai_strerror(getaddrinfo_error));
        }
        exit(1);
    }
    int backend_socket_fd;
    struct addrinfo *addrs_iter;
    for (addrs_iter = addrs;
         addrs_iter != ((void *)0);
         addrs_iter = addrs_iter->ai_next)
    {
        backend_socket_fd = socket(addrs_iter->ai_family,
                                   addrs_iter->ai_socktype,
                                   addrs_iter->ai_protocol);
        if (backend_socket_fd == -1) {
            continue;
        }
        if (connect(backend_socket_fd,
                    addrs_iter->ai_addr,
                    addrs_iter->ai_addrlen) != -1) {
            break;
        }
        close(backend_socket_fd);
    }
    if (addrs_iter == ((void *)0)) {
        rsp_log("Couldn't connect to backend");
        exit(1);
    }
    freeaddrinfo(addrs);
    return backend_socket_fd;
}

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

return_type: int

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


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

result = epoll_create1(0);
epoll_fd = result;
if ( result == -1 )
{
rsp_log_error("Couldn't create epoll FD");
exit(1);
}
return result;
}

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

result = epoll_create1(0);
epoll_fd = result;
if ( result == -1 )
{
rsp_log_error("Couldn't create epoll FD");
exit(1);
}
return result;
}

original_code:
void epoll_init()
{
    epoll_fd = epoll_create1(0);
    if (epoll_fd == -1) {
        rsp_log_error("Couldn't create epoll FD");
        exit(1);
    }
}


decompiled_code:
int __fastcall epoll_add_handler(int *a1, uint32_t a2)
{
int result; // eax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.data.u64 = (uint64_t)a1;
event.events = a2;
result = epoll_ctl(epoll_fd, 1, *a1, &event);
if ( result == -1 )
{
rsp_log_error("Couldn't register server socket with epoll");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int *a1, uint32_t a2)
{
int result; // eax
struct epoll_event event; // [rsp+4h] [rbp-14h] BYREF

event.data.u64 = (uint64_t)a1;
event.events = a2;
result = epoll_ctl(epoll_fd, 1, *a1, &event);
if ( result == -1 )
{
rsp_log_error("Couldn't register server socket with epoll");
exit(-1);
}
return result;
}

original_code:
void epoll_add_handler(struct epoll_event_handler* handler, uint32_t event_mask)
{
    struct epoll_event event;
    memset(&event, 0, sizeof(struct epoll_event));
    event.data.ptr = handler;
    event.events = event_mask;
    if (epoll_ctl(epoll_fd, 1, handler->fd, &event) == -1) {
        rsp_log_error("Couldn't register server socket with epoll");
        exit(-1);
    }
}

canonical_original_code:
void epoll_add_handler(struct epoll_event_handler *handler, unsigned int event_mask)
{
    struct epoll_event event;
    memset(&event, 0, sizeof(struct epoll_event));
    event.data.ptr = handler;
    event.events = event_mask;
    if (epoll_ctl(epoll_fd, 1, handler->fd, &event) == -1) {
        rsp_log_error("Couldn't register server socket with epoll");
        exit(-1);
    }
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }


decompiled_code:
int __fastcall epoll_remove_handler(int *a1)
{
return epoll_ctl(epoll_fd, 2, *a1, 0LL);
}

canonical_decompiled_code:
int __fastcall func2(int *a1)
{
return epoll_ctl(epoll_fd, 2, *a1, 0LL);
}

original_code:
void epoll_remove_handler(struct epoll_event_handler* handler)
{
    epoll_ctl(epoll_fd, 2, handler->fd, ((void *)0));
}

canonical_original_code:
void epoll_remove_handler(struct epoll_event_handler *handler)
{
    epoll_ctl(epoll_fd, 2, handler->fd, ((void *)0));
}


decompiled_code:
void __noreturn epoll_do_reactor_loop()
{
void **v0; // rbx
struct epoll_event events; // [rsp+4h] [rbp-14h] BYREF

while ( 1 )
{
do
{
epoll_wait(epoll_fd, &events, 1, -1);
(*(void (__fastcall **)(epoll_data_t, _QWORD))(events.data.u64 + 8))(events.data, events.events);
v0 = (void **)free_list;
}
while ( !free_list );
do
{
free(*v0);
v0 = (void **)*((_QWORD *)free_list + 1);
free(free_list);
free_list = v0;
}
while ( v0 );
}
}

canonical_decompiled_code:
void __noreturn func3()
{
void **v0; // rbx
struct epoll_event events; // [rsp+4h] [rbp-14h] BYREF

while ( 1 )
{
do
{
epoll_wait(epoll_fd, &events, 1, -1);
(*(void (__fastcall **)(epoll_data_t, _QWORD))(events.data.u64 + 8))(events.data, events.events);
v0 = (void **)free_list;
}
while ( !free_list );
do
{
free(*v0);
v0 = (void **)*((_QWORD *)free_list + 1);
free(free_list);
free_list = v0;
}
while ( v0 );
}
}

original_code:
void epoll_do_reactor_loop()
{
    struct epoll_event current_epoll_event;
    while (1) {
        struct epoll_event_handler* handler;
        epoll_wait(epoll_fd, &current_epoll_event, 1, -1);
        handler = (struct epoll_event_handler*) current_epoll_event.data.ptr;
        handler->handle(handler, current_epoll_event.events);
        struct free_list_entry* temp;
        while (free_list != ((void *)0)) {
            free(free_list->block);
            temp = free_list->next;
            free(free_list);
            free_list = temp;
        }
    }
}

canonical_original_code:
void epoll_do_reactor_loop()
{
    struct epoll_event current_epoll_event;
    while (1) {
        struct epoll_event_handler *handler;
        epoll_wait(epoll_fd, &current_epoll_event, 1, -1);
        handler = (struct epoll_event_handler *) current_epoll_event.data.ptr;
        handler->handle(handler, current_epoll_event.events);
        struct free_list_entry *temp;
        while (free_list != ((void *)0)) {
            free(free_list->block);
            temp = free_list->next;
            free(free_list);
            free_list = temp;
        }
    }
}

variable_types: struct free_list_entry { void *block; struct free_list_entry *next; } *

user_defined_types: union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; }


repo: gsilvis/hyperban

original_code:
Graph *build_initial_node (void)
{
  Tile *t = malloc(sizeof(Tile));
  t->tile_type = TILE_TYPE_DEFAULT;
  t->agent = AGENT_DEFAULT;
  t->search_flag = 0;
  Graph *nodes[4];
  for (size_t i = 0; i < 4; i++)
    {
      nodes[i] = malloc(sizeof(Graph));
    }
  for (size_t i = 0; i < 4; i++)
    {
      nodes[i]->tile = t;
      nodes[i]->rotate_r = nodes[(i + 1) % 4];
      nodes[i]->adjacent = ((void *)0);
    }
  return nodes[0];
}

canonical_original_code:
struct graph_t *build_initial_node()
{
  struct Tile *t = malloc(sizeof(Tile));
  t->tile_type = TILE_TYPE_DEFAULT;
  t->agent = AGENT_DEFAULT;
  t->search_flag = 0;
  struct graph_t *nodes[4];
  for (unsigned long i = 0; i < 4; i++)
    {
      nodes[i] = malloc(sizeof(Graph));
    }
  for (unsigned long i = 0; i < 4; i++)
    {
      nodes[i]->tile = t;
      nodes[i]->rotate_r = nodes[(i + 1) % 4];
      nodes[i]->adjacent = ((void *)0);
    }
  return nodes[0];
}

variable_types: struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } *[4]

variable_types:
i
unsigned long

user_defined_types: struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile *tile; }


original_code:
static void build_enforce_convexity_left (Graph *g)
{
  if (!g)
    return;
  Graph *clockwise = g->rotate_r->adjacent;
  Graph *ccwise = g->adjacent;
  if (!clockwise || !ccwise)
    return;
  Graph *newclock = clockwise->rotate_r->adjacent;
  Graph *newcc = ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!newclock)
    {
      newclock = build_initial_node();
      clockwise->rotate_r->adjacent = newclock;
      newclock->adjacent = clockwise->rotate_r;
    }
  if (!newcc)
    {
      newcc = build_initial_node();
      ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent = newcc;
      newcc->adjacent = ((ccwise)->rotate_r->rotate_r->rotate_r);
    }
  newclock->rotate_r->adjacent = ((newcc)->rotate_r->rotate_r->rotate_r);
  ((newcc)->rotate_r->rotate_r->rotate_r)->adjacent = newclock->rotate_r;
  build_enforce_convexity_left(clockwise->rotate_r);
}

canonical_original_code:
static void build_enforce_convexity_left(struct graph_t *g)
{
  if (!g)
    return;
  struct graph_t *clockwise = g->rotate_r->adjacent;
  struct graph_t *ccwise = g->adjacent;
  if (!clockwise || !ccwise)
    return;
  struct graph_t *newclock = clockwise->rotate_r->adjacent;
  struct graph_t *newcc = ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!newclock)
    {
      newclock = build_initial_node();
      clockwise->rotate_r->adjacent = newclock;
      newclock->adjacent = clockwise->rotate_r;
    }
  if (!newcc)
    {
      newcc = build_initial_node();
      ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent = newcc;
      newcc->adjacent = ((ccwise)->rotate_r->rotate_r->rotate_r);
    }
  newclock->rotate_r->adjacent = ((newcc)->rotate_r->rotate_r->rotate_r);
  ((newcc)->rotate_r->rotate_r->rotate_r)->adjacent = newclock->rotate_r;
  build_enforce_convexity_left(clockwise->rotate_r);
}

variable_types: struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } *


original_code:
static void build_enforce_convexity_right (Graph *g)
{
  if (!g)
    return;
  Graph *clockwise = g->adjacent;
  Graph *ccwise = ((g)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!clockwise || !ccwise)
    return;
  Graph *newclock = clockwise->rotate_r->adjacent;
  Graph *newcc = ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!newclock)
    {
      newclock = build_initial_node();
      clockwise->rotate_r->adjacent = newclock;
      newclock->adjacent = clockwise->rotate_r;
    }
  if (!newcc)
    {
      newcc = build_initial_node();
      ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent = newcc;
      newcc->adjacent = ((ccwise)->rotate_r->rotate_r->rotate_r);
    }
  newclock->rotate_r->adjacent = ((newcc)->rotate_r->rotate_r->rotate_r);
  ((newcc)->rotate_r->rotate_r->rotate_r)->adjacent = newclock->rotate_r;
  build_enforce_convexity_right(((ccwise)->rotate_r->rotate_r->rotate_r));
}

canonical_original_code:
static void build_enforce_convexity_right(struct graph_t *g)
{
  if (!g)
    return;
  struct graph_t *clockwise = g->adjacent;
  struct graph_t *ccwise = ((g)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!clockwise || !ccwise)
    return;
  struct graph_t *newclock = clockwise->rotate_r->adjacent;
  struct graph_t *newcc = ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent;
  if (!newclock)
    {
      newclock = build_initial_node();
      clockwise->rotate_r->adjacent = newclock;
      newclock->adjacent = clockwise->rotate_r;
    }
  if (!newcc)
    {
      newcc = build_initial_node();
      ((ccwise)->rotate_r->rotate_r->rotate_r)->adjacent = newcc;
      newcc->adjacent = ((ccwise)->rotate_r->rotate_r->rotate_r);
    }
  newclock->rotate_r->adjacent = ((newcc)->rotate_r->rotate_r->rotate_r);
  ((newcc)->rotate_r->rotate_r->rotate_r)->adjacent = newclock->rotate_r;
  build_enforce_convexity_right(((ccwise)->rotate_r->rotate_r->rotate_r));
}

user_defined_types: struct Tile { char tile_type; char agent; char search_flag; }


decompiled_code:
void __fastcall build_wall_in(_QWORD *a1)
{
__int64 v2; // rbp
__int64 v3; // rax

v2 = 8LL;
do
{
if ( !a1[1] )
{
v3 = build_initial_node();
a1[1] = v3;
*(_QWORD *)(v3 + 8) = a1;
}
build_enforce_convexity_left(a1);
build_enforce_convexity_right(a1);
a1 = (_QWORD *)*a1;
--v2;
}
while ( v2 );
}

original_code:
void build_wall_in (Graph *graph)
{
  for (size_t i = 0; i < 8; i++)
    {
      if (!graph->adjacent)
        {
          graph->adjacent = build_initial_node();
          graph->adjacent->adjacent = graph;
        }
      build_enforce_convexity_left(graph);
      build_enforce_convexity_right(graph);
      graph = graph->rotate_r;
    }
}

canonical_original_code:
void build_wall_in(struct graph_t *graph)
{
  for (unsigned long i = 0; i < 8; i++)
    {
      if (!graph->adjacent)
        {
          graph->adjacent = build_initial_node();
          graph->adjacent->adjacent = graph;
        }
      build_enforce_convexity_left(graph);
      build_enforce_convexity_right(graph);
      graph = graph->rotate_r;
    }
}


decompiled_code:
__int64 __fastcall build_add_node(__int64 **a1, __int64 a2)
{
char *v3; // rbp
char i; // al
__int64 result; // rax
__int64 v6; // rax

if ( !a2 )
return 0LL;
v3 = *(char **)a2;
if ( !*(_QWORD *)a2 )
return 0LL;
for ( i = *v3; *v3; i = *v3 )
{
switch ( i )
{
case 'B':
case 'b':
goto LABEL_8;
case 'F':
case 'f':
if ( !a1[1] )
{
v6 = build_initial_node();
a1[1] = (__int64 *)v6;
*(_QWORD *)(v6 + 8) = a1;
build_enforce_convexity_left(a1);
build_enforce_convexity_right(a1);
}
a1 = *(__int64 ***)*a1[1];
break;
case 'L':
case 'l':
a1 = (__int64 **)*a1;
LABEL_8:
a1 = (__int64 **)*a1;
goto LABEL_9;
case 'R':
case 'r':
LABEL_9:
a1 = (__int64 **)*a1;
break;
default:
break;
}
++v3;
}
*(_BYTE *)a1[2] = *(_BYTE *)(a2 + 8);
*((_BYTE *)a1[2] + 1) = *(_BYTE *)(a2 + 9);
result = 1LL;
if ( *(_BYTE *)(a2 + 8) != 1 )
{
build_wall_in(a1);
result = 1LL;
}
return result;
}

original_code:
int build_add_node (Graph *graph, SavedTile *tile)
{
  if (!tile)
    return 0;
  char *direction_ptr = tile->path;
  if (!direction_ptr)
    return 0;
  for (; *direction_ptr; direction_ptr++)
    {
      switch (*direction_ptr)
        {
        case 'L': case 'l':
          graph = graph->rotate_r;
        case 'B': case 'b':
          graph = graph->rotate_r;
        case 'R': case 'r':
          graph = graph->rotate_r;
          break;
        case 'F': case 'f':
          if (!graph->adjacent)
            {
              graph->adjacent = build_initial_node();
              graph->adjacent->adjacent = graph;
              build_enforce_convexity_left(graph);
              build_enforce_convexity_right(graph);
            }
          graph = ((graph->adjacent)->rotate_r->rotate_r);
          break;
        }
    }
  graph->tile->tile_type = tile->tile_type;
  graph->tile->agent = tile->agent;
  if (tile->tile_type != TILE_TYPE_WALL)
    build_wall_in(graph);
  return 1;
}

canonical_original_code:
int build_add_node(struct graph_t *graph, struct SavedTile *tile)
{
  if (!tile)
    return 0;
  char *direction_ptr = tile->path;
  if (!direction_ptr)
    return 0;
  for (; *direction_ptr; direction_ptr++)
    {
      switch (*direction_ptr)
        {
        case 'L': case 'l':
          graph = graph->rotate_r;
        case 'B': case 'b':
          graph = graph->rotate_r;
        case 'R': case 'r':
          graph = graph->rotate_r;
          break;
        case 'F': case 'f':
          if (!graph->adjacent)
            {
              graph->adjacent = build_initial_node();
              graph->adjacent->adjacent = graph;
              build_enforce_convexity_left(graph);
              build_enforce_convexity_right(graph);
            }
          graph = ((graph->adjacent)->rotate_r->rotate_r);
          break;
        }
    }
  graph->tile->tile_type = tile->tile_type;
  graph->tile->agent = tile->agent;
  if (tile->tile_type != TILE_TYPE_WALL)
    build_wall_in(graph);
  return 1;
}

variable_types: struct SavedTile { char *path; char tile_type; char agent; } *


decompiled_code:
__int64 __fastcall board_process_config(__int64 a1, __int64 a2)
{
const char *v2; // rax
bool v3; // zf
unsigned int v4; // ebx
size_t v5; // r13
const char *v6; // r14
__int64 v7; // rax
__int64 v9; // rax
__int64 v10; // rax
const char *v11; // rdx

if ( !a2 )
return 0;
v2 = *(const char **)a2;
if ( !*(_QWORD *)a2 )
return (unsigned int)-1;
v3 = strcmp(*(const char **)a2, "startpos") == 0;
v4 = !v3;
if ( v3 )
{
if ( *(_QWORD *)(a2 + 16) && *(_QWORD *)a1 )
{
v5 = 0LL;
while ( 2 )
{
v6 = *(const char **)(a2 + 16);
if ( strlen(v6) > v5 )
{
switch ( v6[v5] )
{
case 'B':
case 'b':
goto LABEL_8;
case 'F':
case 'f':
v7 = *(_QWORD *)(*(_QWORD *)a1 + 8LL);
if ( !v7 )
return v4;
*(_QWORD *)a1 = v7;
*(_QWORD *)a1 = ***(_QWORD ***)(v7 + 8);
goto LABEL_10;
case 'L':
case 'l':
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_8:
*(_QWORD *)a1 = **(_QWORD **)a1;
goto LABEL_9;
case 'R':
case 'r':
LABEL_9:
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_10:
++v5;
continue;
default:
fwrite("Invalid character in string for start_loc.\n", 1uLL, 0x2BuLL, stderr);
return v4;
}
}
break;
}
v4 = 1;
}
}
else
{
v3 = strcmp(v2, "title") == 0;
v4 = !v3;
if ( v3 )
{
v9 = *(_QWORD *)(a2 + 16);
if ( v9 )
{
*(_QWORD *)(a1 + 24) = v9;
v4 = 1;
}
}
else if ( !strcmp(v2, "difficulty") )
{
*(_DWORD *)(a1 + 12) = *(_DWORD *)(a2 + 24);
v4 = 1;
}
else
{
v3 = strcmp(v2, "collection") == 0;
v4 = !v3;
if ( v3 )
{
v10 = *(_QWORD *)(a2 + 16);
if ( v10 )
{
*(_QWORD *)(a1 + 32) = v10;
v4 = 1;
}
}
else if ( !strcmp(v2, "number") )
{
*(_DWORD *)(a1 + 16) = *(_DWORD *)(a2 + 24);
v4 = 1;
}
else
{
v3 = strcmp(v2, "debug_print") == 0;
v4 = !v3;
if ( v3 )
{
v11 = *(const char **)(a2 + 16);
if ( v11 )
{
fprintf(stderr, "%s\n", v11);
v4 = 1;
}
else
{
fwrite("No argument to debug_print\n", 1uLL, 0x1BuLL, stderr);
}
}
else
{
v4 = *(_BYTE *)(a2 + 8) == 0;
}
}
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
const char *v2; // rax
bool v3; // zf
unsigned int v4; // ebx
size_t v5; // r13
const char *v6; // r14
__int64 v7; // rax
__int64 v9; // rax
__int64 v10; // rax
const char *v11; // rdx

if ( !a2 )
return 0;
v2 = *(const char **)a2;
if ( !*(_QWORD *)a2 )
return (unsigned int)-1;
v3 = strcmp(*(const char **)a2, "startpos") == 0;
v4 = !v3;
if ( v3 )
{
if ( *(_QWORD *)(a2 + 16) && *(_QWORD *)a1 )
{
v5 = 0LL;
while ( 2 )
{
v6 = *(const char **)(a2 + 16);
if ( strlen(v6) > v5 )
{
switch ( v6[v5] )
{
case 'B':
case 'b':
goto LABEL_8;
case 'F':
case 'f':
v7 = *(_QWORD *)(*(_QWORD *)a1 + 8LL);
if ( !v7 )
return v4;
*(_QWORD *)a1 = v7;
*(_QWORD *)a1 = ***(_QWORD ***)(v7 + 8);
goto LABEL_10;
case 'L':
case 'l':
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_8:
*(_QWORD *)a1 = **(_QWORD **)a1;
goto LABEL_9;
case 'R':
case 'r':
LABEL_9:
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_10:
++v5;
continue;
default:
fwrite("Invalid character in string for start_loc.\n", 1uLL, 0x2BuLL, stderr);
return v4;
}
}
break;
}
v4 = 1;
}
}
else
{
v3 = strcmp(v2, "title") == 0;
v4 = !v3;
if ( v3 )
{
v9 = *(_QWORD *)(a2 + 16);
if ( v9 )
{
*(_QWORD *)(a1 + 24) = v9;
v4 = 1;
}
}
else if ( !strcmp(v2, "difficulty") )
{
*(_DWORD *)(a1 + 12) = *(_DWORD *)(a2 + 24);
v4 = 1;
}
else
{
v3 = strcmp(v2, "collection") == 0;
v4 = !v3;
if ( v3 )
{
v10 = *(_QWORD *)(a2 + 16);
if ( v10 )
{
*(_QWORD *)(a1 + 32) = v10;
v4 = 1;
}
}
else if ( !strcmp(v2, "number") )
{
*(_DWORD *)(a1 + 16) = *(_DWORD *)(a2 + 24);
v4 = 1;
}
else
{
v3 = strcmp(v2, "debug_print") == 0;
v4 = !v3;
if ( v3 )
{
v11 = *(const char **)(a2 + 16);
if ( v11 )
{
fprintf(stderr, "%s\n", v11);
v4 = 1;
}
else
{
fwrite("No argument to debug_print\n", 1uLL, 0x1BuLL, stderr);
}
}
else
{
v4 = *(_BYTE *)(a2 + 8) == 0;
}
}
}
}
return v4;
}

original_code:
static int board_process_config (Board *board, ConfigOption *option)
{
  if (!option)
    return 0;
  if (!(option->key))
    return -1;
  if (!strcmp(option->key, "startpos"))
    {
      if (!(option->value_s))
        return 0;
      if (!(board->graph))
        return 0;
      for (size_t i = 0; i < strlen(option->value_s); i++)
        {
          switch (option->value_s[i])
            {
            case 'L': case 'l':
              board->graph = board->graph->rotate_r;
            case 'B': case 'b':
              board->graph = board->graph->rotate_r;
            case 'R': case 'r':
              board->graph = board->graph->rotate_r;
              break;
            case 'F': case 'f':
              if (!(board->graph->adjacent))
                return 0;
              board->graph = board->graph->adjacent;
              board->graph = ((board->graph->adjacent)->rotate_r->rotate_r);
              break;
            default:
              fprintf(stderr, "Invalid character in string for start_loc.\n");
              return 0;
            }
        }
    }
  else if (!strcmp(option->key, "title"))
    {
      if (!(option->value_s))
        return 0;
      board->level_title = option->value_s;
    }
  else if (!strcmp(option->key, "difficulty"))
    board->difficulty = option->value_i;
  else if (!strcmp(option->key, "collection"))
    {
      if (!(option->value_s))
        return 0;
      board->collection_title = option->value_s;
    }
  else if (!strcmp(option->key, "number"))
    board->level_number = option->value_i;
  else if (!strcmp(option->key, "debug_print"))
    if (option->value_s)
      fprintf(stderr, "%s\n", option->value_s);
    else
      {
        fprintf(stderr, "No argument to debug_print\n");
        return 0;
      }
  else
    return !(option->mandatory);
  return 1;
}

canonical_original_code:
static int board_process_config(struct Board *board, struct ConfigOption *option)
{
  if (!option)
    return 0;
  if (!(option->key))
    return -1;
  if (!strcmp(option->key, "startpos"))
    {
      if (!(option->value_s))
        return 0;
      if (!(board->graph))
        return 0;
      for (unsigned long i = 0; i < strlen(option->value_s); i++)
        {
          switch (option->value_s[i])
            {
            case 'L': case 'l':
              board->graph = board->graph->rotate_r;
            case 'B': case 'b':
              board->graph = board->graph->rotate_r;
            case 'R': case 'r':
              board->graph = board->graph->rotate_r;
              break;
            case 'F': case 'f':
              if (!(board->graph->adjacent))
                return 0;
              board->graph = board->graph->adjacent;
              board->graph = ((board->graph->adjacent)->rotate_r->rotate_r);
              break;
            default:
              fprintf(stderr, "Invalid character in string for start_loc.\n");
              return 0;
            }
        }
    }
  else if (!strcmp(option->key, "title"))
    {
      if (!(option->value_s))
        return 0;
      board->level_title = option->value_s;
    }
  else if (!strcmp(option->key, "difficulty"))
    board->difficulty = option->value_i;
  else if (!strcmp(option->key, "collection"))
    {
      if (!(option->value_s))
        return 0;
      board->collection_title = option->value_s;
    }
  else if (!strcmp(option->key, "number"))
    board->level_number = option->value_i;
  else if (!strcmp(option->key, "debug_print"))
    if (option->value_s)
      fprintf(stderr, "%s\n", option->value_s);
    else
      {
        fprintf(stderr, "No argument to debug_print\n");
        return 0;
      }
  else
    return !(option->mandatory);
  return 1;
}

variable_types: struct Board { struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } *graph; int unsolved; int difficulty; int level_number; char *level_title; char *collection_title; char *moves; unsigned long moves_length; int number_moves; char *filename; } *

variable_types: struct ConfigOption { char *key; char mandatory; char *value_s; int value_i; } *

user_defined_types: struct Board { struct graph_t *graph; int unsolved; int difficulty; int level_number; char *level_title; char *collection_title; char *moves; unsigned long moves_length; int number_moves; char *filename; }


decompiled_code:
void __fastcall free_board(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

if ( *(_QWORD *)ptr )
free_graph();
v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

if ( *(_QWORD *)ptr )
free_graph();
v2 = (void *)*((_QWORD *)ptr + 5);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
free(ptr);
}

original_code:
void free_board (Board *b)
{
  if (b->graph)
    free_graph(b->graph);
  if (b->moves)
    free(b->moves);
  if (b->level_title)
    free(b->level_title);
  if (b->collection_title)
    free(b->collection_title);
  free(b);
}

canonical_original_code:
void free_board(struct Board *b)
{
  if (b->graph)
    free_graph(b->graph);
  if (b->moves)
    free(b->moves);
  if (b->level_title)
    free(b->level_title);
  if (b->collection_title)
    free(b->collection_title);
  free(b);
}


decompiled_code:
__int64 __fastcall sokoban_get_move_abbreviation(char a1, int a2)
{
int v2; // eax
unsigned int v3; // edx
unsigned int v4; // eax

v2 = 32 * (1 - a2);
if ( a1 == 2 )
return (unsigned int)(v2 + 68);
if ( a1 > 2 )
{
v4 = v2 + 76;
v3 = 63;
if ( a1 == 3 )
v3 = v4;
}
else
{
v3 = v2 + 85;
if ( a1 )
{
if ( a1 == 1 )
v3 = v2 + 82;
else
v3 = 63;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1, int a2)
{
int v2; // eax
unsigned int v3; // edx
unsigned int v4; // eax

v2 = 32 * (1 - a2);
if ( a1 == 2 )
return (unsigned int)(v2 + 68);
if ( a1 > 2 )
{
v4 = v2 + 76;
v3 = 63;
if ( a1 == 3 )
v3 = v4;
}
else
{
v3 = v2 + 85;
if ( a1 )
{
if ( a1 == 1 )
v3 = v2 + 82;
else
v3 = 63;
}
}
return v3;
}

original_code:
static char sokoban_get_move_abbreviation (Move move, int is_push)
{
  char returnee = 32 * (1 - is_push);
  switch (move)
    {
    case MOVE_UP:
      return returnee + 'U';
    case MOVE_RIGHT:
      return returnee + 'R';
    case MOVE_DOWN:
      return returnee + 'D';
    case MOVE_LEFT:
      return returnee + 'L';
    default:
      return '?';
    }
}

canonical_original_code:
static char sokoban_get_move_abbreviation(char move, int is_push)
{
  char returnee = 32 * (1 - is_push);
  switch (move)
    {
    case MOVE_UP:
      return returnee + 'U';
    case MOVE_RIGHT:
      return returnee + 'R';
    case MOVE_DOWN:
      return returnee + 'D';
    case MOVE_LEFT:
      return returnee + 'L';
    default:
      return '?';
    }
}

variable_types: move char

variable_types: returnee char

return_type: char


decompiled_code:
__int64 __fastcall sokoban_update_board_data(__int64 a1, char a2, unsigned int a3)
{
char *v4; // r12
size_t v5; // rax
__int64 v6; // rsi
size_t v8; // rsi
char src[25]; // [rsp+Fh] [rbp-19h] BYREF

++*(_DWORD *)(a1 + 56);
v4 = *(char **)(a1 + 40);
if ( v4 )
{
src[0] = sokoban_get_move_abbreviation(a2, a3);
v5 = strlen(v4);
v6 = *(_QWORD *)(a1 + 48);
if ( v5 >= v6 - 1 )
{
v8 = 2 * v6;
*(_QWORD *)(a1 + 48) = v8;
*(_QWORD *)(a1 + 40) = realloc(v4, v8);
}
strncat(*(char **)(a1 + 40), src, 1uLL);
}
return a3;
}

original_code:
static int sokoban_update_board_data (Board *b, Move move, int is_push)
{
  b->number_moves++;
  if (!(b->moves))
    return is_push;
  char c = sokoban_get_move_abbreviation(move, is_push);
  if (strlen(b->moves) >= b->moves_length - 1)
    {
      b->moves_length *= 2;
      b->moves = realloc(b->moves, b->moves_length);
    }
  strncat(b->moves, &c, 1);
  return is_push;
}

canonical_original_code:
static int sokoban_update_board_data(struct Board *b, char move, int is_push)
{
  b->number_moves++;
  if (!(b->moves))
    return is_push;
  char c = sokoban_get_move_abbreviation(move, is_push);
  if (strlen(b->moves) >= b->moves_length - 1)
    {
      b->moves_length *= 2;
      b->moves = realloc(b->moves, b->moves_length);
    }
  strncat(b->moves, &c, 1);
  return is_push;
}

variable_types: c char


original_code:
int perform_move (Board *b, Move move)
{
  Graph *old = b->graph;
  Graph *new = old;
  for (char i = 0; i < move; i++)
    new = new->rotate_r;
  new = new->adjacent;
  for (char i = 0; i < (2 - move + 4) % 4; i++)
    new = new->rotate_r;
  if (new->tile->tile_type == TILE_TYPE_WALL)
    return RESULT_NO_MOVE_POSSIBLE;
  if (new->tile->agent == AGENT_NONE)
    {
      b->graph = new;
      return sokoban_update_board_data(b, move, RESULT_MOVE);
    }
  Graph *behind = new;
  for (char i = 0; i < move; i++)
    behind = behind->rotate_r;
  behind = behind->adjacent;
  if (behind->tile->tile_type == TILE_TYPE_WALL)
    return RESULT_NO_MOVE_POSSIBLE;
  if (behind->tile->agent == AGENT_BOX)
    return RESULT_NO_MOVE_POSSIBLE;
  new->tile->agent = AGENT_NONE;
  behind->tile->agent = AGENT_BOX;
  if (new->tile->tile_type == TILE_TYPE_TARGET)
    (b->unsolved)++;
  if (behind->tile->tile_type == TILE_TYPE_TARGET)
    (b->unsolved)--;
  b->graph = new;
  return sokoban_update_board_data(b, move, RESULT_PUSH);
}

canonical_original_code:
int perform_move(struct Board *b, char move)
{
  struct graph_t *old = b->graph;
  struct graph_t *new = old;
  for (char i = 0; i < move; i++)
    new = new->rotate_r;
  new = new->adjacent;
  for (char i = 0; i < (2 - move + 4) % 4; i++)
    new = new->rotate_r;
  if (new->tile->tile_type == TILE_TYPE_WALL)
    return RESULT_NO_MOVE_POSSIBLE;
  if (new->tile->agent == AGENT_NONE)
    {
      b->graph = new;
      return sokoban_update_board_data(b, move, RESULT_MOVE);
    }
  struct graph_t *behind = new;
  for (char i = 0; i < move; i++)
    behind = behind->rotate_r;
  behind = behind->adjacent;
  if (behind->tile->tile_type == TILE_TYPE_WALL)
    return RESULT_NO_MOVE_POSSIBLE;
  if (behind->tile->agent == AGENT_BOX)
    return RESULT_NO_MOVE_POSSIBLE;
  new->tile->agent = AGENT_NONE;
  behind->tile->agent = AGENT_BOX;
  if (new->tile->tile_type == TILE_TYPE_TARGET)
    (b->unsolved)++;
  if (behind->tile->tile_type == TILE_TYPE_TARGET)
    (b->unsolved)--;
  b->graph = new;
  return sokoban_update_board_data(b, move, RESULT_PUSH);
}

variable_types: i char


decompiled_code:
__int64 __fastcall unperform_move(__int64 a1)
{
unsigned __int8 *v1; // rbp
__int64 result; // rax
unsigned __int8 *v3; // rdx
_QWORD *v4; // rdx
__int64 v5; // rcx
_QWORD *v6; // rcx

v1 = *(unsigned __int8 **)(a1 + 40);
if ( !v1 )
return 0LL;
result = *v1;
if ( (_BYTE)result )
{
v3 = &v1[strlen(*(const char **)(a1 + 40)) - 1];
result = *v3;
*v3 = 0;
v4 = *(_QWORD **)a1;
switch ( (char)result )
{
case 'D':
case 'd':
goto LABEL_5;
case 'L':
case 'l':
v4 = (_QWORD *)*v4;
LABEL_5:
v4 = (_QWORD *)*v4;
goto LABEL_6;
case 'R':
case 'r':
LABEL_6:
v4 = (_QWORD *)*v4;
goto LABEL_7;
case 'U':
case 'u':
LABEL_7:
if ( !*(_QWORD *)(*(_QWORD *)*v4 + 8LL) )
return 0LL;
if ( (char)result > 96 )
goto LABEL_14;
v5 = *(_QWORD *)(v4[1] + 16LL);
if ( *(_BYTE *)(v5 + 1) == 1 )
{
*(_BYTE *)(v5 + 1) = 0;
*(_BYTE *)(v4[2] + 1LL) = 1;
if ( **(_BYTE **)(v4[1] + 16LL) == 2 )
++*(_DWORD *)(a1 + 8);
if ( *(_BYTE *)v4[2] == 2 )
--*(_DWORD *)(a1 + 8);
LABEL_14:
v6 = *(_QWORD **)(*(_QWORD *)*v4 + 8LL);
*(_QWORD *)a1 = v6;
switch ( (char)result )
{
case 'D':
case 'd':
goto LABEL_16;
case 'L':
case 'l':
goto LABEL_17;
case 'R':
case 'r':
*(_QWORD *)a1 = *v6;
LABEL_16:
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_17:
*(_QWORD *)a1 = **(_QWORD **)a1;
break;
default:
break;
}
--*(_DWORD *)(a1 + 56);
}
else
{
result = 0LL;
}
break;
default:
return 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned __int8 *v1; // rbp
__int64 result; // rax
unsigned __int8 *v3; // rdx
_QWORD *v4; // rdx
__int64 v5; // rcx
_QWORD *v6; // rcx

v1 = *(unsigned __int8 **)(a1 + 40);
if ( !v1 )
return 0LL;
result = *v1;
if ( (_BYTE)result )
{
v3 = &v1[strlen(*(const char **)(a1 + 40)) - 1];
result = *v3;
*v3 = 0;
v4 = *(_QWORD **)a1;
switch ( (char)result )
{
case 'D':
case 'd':
goto LABEL_5;
case 'L':
case 'l':
v4 = (_QWORD *)*v4;
LABEL_5:
v4 = (_QWORD *)*v4;
goto LABEL_6;
case 'R':
case 'r':
LABEL_6:
v4 = (_QWORD *)*v4;
goto LABEL_7;
case 'U':
case 'u':
LABEL_7:
if ( !*(_QWORD *)(*(_QWORD *)*v4 + 8LL) )
return 0LL;
if ( (char)result > 96 )
goto LABEL_14;
v5 = *(_QWORD *)(v4[1] + 16LL);
if ( *(_BYTE *)(v5 + 1) == 1 )
{
*(_BYTE *)(v5 + 1) = 0;
*(_BYTE *)(v4[2] + 1LL) = 1;
if ( **(_BYTE **)(v4[1] + 16LL) == 2 )
++*(_DWORD *)(a1 + 8);
if ( *(_BYTE *)v4[2] == 2 )
--*(_DWORD *)(a1 + 8);
LABEL_14:
v6 = *(_QWORD **)(*(_QWORD *)*v4 + 8LL);
*(_QWORD *)a1 = v6;
switch ( (char)result )
{
case 'D':
case 'd':
goto LABEL_16;
case 'L':
case 'l':
goto LABEL_17;
case 'R':
case 'r':
*(_QWORD *)a1 = *v6;
LABEL_16:
*(_QWORD *)a1 = **(_QWORD **)a1;
LABEL_17:
*(_QWORD *)a1 = **(_QWORD **)a1;
break;
default:
break;
}
--*(_DWORD *)(a1 + 56);
}
else
{
result = 0LL;
}
break;
default:
return 0LL;
}
}
return result;
}

original_code:
char unperform_move (Board *b)
{
  if (!(b->moves))
    return '\0';
  if (strlen(b->moves) == 0)
    return '\0';
  char c = b->moves[strlen(b->moves) - 1];
  b->moves[strlen(b->moves) - 1] = '\0';
  Graph *bluh = b->graph;
  switch (c)
    {
    case 'L': case 'l':
      bluh = bluh->rotate_r;
    case 'D': case 'd':
      bluh = bluh->rotate_r;
    case 'R': case 'r':
      bluh = bluh->rotate_r;
    case 'U': case 'u':
      break;
    default:
      return '\0';
    }
  if (!(bluh->rotate_r->rotate_r->adjacent))
    return '\0';
  if (c < 'a')
    {
      if (bluh->adjacent->tile->agent != AGENT_BOX)
        return '\0';
      bluh->adjacent->tile->agent = AGENT_NONE;
      bluh->tile->agent = AGENT_BOX;
      if (bluh->adjacent->tile->tile_type == TILE_TYPE_TARGET)
        (b->unsolved)++;
      if (bluh->tile->tile_type == TILE_TYPE_TARGET)
        (b->unsolved)--;
    }
  b->graph = bluh->rotate_r->rotate_r->adjacent;
  switch (c)
    {
    case 'R': case 'r':
      b->graph = b->graph->rotate_r;
    case 'D': case 'd':
      b->graph = b->graph->rotate_r;
    case 'L': case 'l':
      b->graph = b->graph->rotate_r;
    case 'U': case 'u':
      break;
    }
  (b->number_moves)--;
  return c;
}

canonical_original_code:
char unperform_move(struct Board *b)
{
  if (!(b->moves))
    return '\0';
  if (strlen(b->moves) == 0)
    return '\0';
  char c = b->moves[strlen(b->moves) - 1];
  b->moves[strlen(b->moves) - 1] = '\0';
  struct graph_t *bluh = b->graph;
  switch (c)
    {
    case 'L': case 'l':
      bluh = bluh->rotate_r;
    case 'D': case 'd':
      bluh = bluh->rotate_r;
    case 'R': case 'r':
      bluh = bluh->rotate_r;
    case 'U': case 'u':
      break;
    default:
      return '\0';
    }
  if (!(bluh->rotate_r->rotate_r->adjacent))
    return '\0';
  if (c < 'a')
    {
      if (bluh->adjacent->tile->agent != AGENT_BOX)
        return '\0';
      bluh->adjacent->tile->agent = AGENT_NONE;
      bluh->tile->agent = AGENT_BOX;
      if (bluh->adjacent->tile->tile_type == TILE_TYPE_TARGET)
        (b->unsolved)++;
      if (bluh->tile->tile_type == TILE_TYPE_TARGET)
        (b->unsolved)--;
    }
  b->graph = bluh->rotate_r->rotate_r->adjacent;
  switch (c)
    {
    case 'R': case 'r':
      b->graph = b->graph->rotate_r;
    case 'D': case 'd':
      b->graph = b->graph->rotate_r;
    case 'L': case 'l':
      b->graph = b->graph->rotate_r;
    case 'U': case 'u':
      break;
    }
  (b->number_moves)--;
  return c;
}


decompiled_code:
__int64 audit_level()
{
return 1LL;
}

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

original_code:
int audit_level (SavedTile *tiles, ConfigOption *options)
{
  return 1;
}

canonical_original_code:
int audit_level(struct SavedTile *tiles, struct ConfigOption *options)
{
  return 1;
}

user_defined_types: struct SavedTile { char *path; char tile_type; char agent; }

user_defined_types: struct ConfigOption { char *key; char mandatory; char *value_s; int value_i; }


decompiled_code:
__int64 audit_board()
{
return 1LL;
}

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

original_code:
int audit_board (Board *board)
{
  return 1;
}

canonical_original_code:
int audit_board(struct Board *board)
{
  return 1;
}


decompiled_code:
void *new_queue()
{
return calloc(1uLL, 0x10uLL);
}

canonical_decompiled_code:
void *func0()
{
return calloc(1uLL, 0x10uLL);
}

original_code:
Queue *new_queue () {
  return calloc(1, sizeof(Queue));
}

canonical_original_code:
struct Queue *new_queue() {
  return calloc(1, sizeof(Queue));
}

return_type: struct Queue { struct node_t { struct node_t *next; struct queue_item_t { struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } *g; unsigned short refs; char whence; struct queue_item_t *parent; } *item; } *first; struct node_t *last; } *

user_defined_types: struct Queue { struct node_t *first; struct node_t *last; }

user_defined_types: struct node_t { struct node_t *next; struct queue_item_t *item; }

user_defined_types: struct queue_item_t { struct graph_t *g; unsigned short refs; char whence; struct queue_item_t *parent; }


dequeue

decompiled_code:
__int64 __fastcall dequeue(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rbx

v1 = *(_QWORD **)a1;
if ( !*(_QWORD *)a1 )
return 0LL;
if ( *v1 )
{
*(_QWORD *)a1 = *v1;
}
else
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
v2 = v1[1];
free(v1);
return v2;
}

original_code:
QueueItem *dequeue (Queue *q) {
  Node *tmp = q->first;
  if (!tmp) return ((void *)0);
  if (tmp->next) {
    q->first = tmp->next;
  } else {
    q->first = ((void *)0);
    q->last = ((void *)0);
  }
  QueueItem *returnee = tmp->item;
  free(tmp);
  return returnee;
}

canonical_original_code:
struct queue_item_t *dequeue(struct Queue *q) {
  struct node_t *tmp = q->first;
  if (!tmp) return ((void *)0);
  if (tmp->next) {
    q->first = tmp->next;
  } else {
    q->first = ((void *)0);
    q->last = ((void *)0);
  }
  struct queue_item_t *returnee = tmp->item;
  free(tmp);
  return returnee;
}

variable_types: struct Queue { struct node_t { struct node_t *next; struct queue_item_t *item; } *first; struct node_t *last; } *

variable_types: struct node_t { struct node_t *next; struct queue_item_t { struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } *g; unsigned short refs; char whence; struct queue_item_t *parent; } *item; } *


original_code:
static Graph *generate_room(const GeneratorParams *params) {
  Graph *start = build_initial_node();
  size_t num_walls = 1;
  Graph **walls = malloc(sizeof(Graph *));
  walls[0] = start;
  start->tile->search_flag = 1;
  Graph **floors = ((void *)0);
  size_t num_floors = 0;
  size_t room_size;
  size_t goal_size = params->min_size + (rand() % params->size_range);
  goal_size += params->num_goals;
  size_t num_targets = 0;
  for (room_size = 0; room_size < goal_size; room_size++) {
    size_t index = rand() % num_walls;
    Graph *t = walls[index];
    if (rand() % (goal_size - room_size) < params->num_goals-num_targets) {
      t->tile->tile_type = TILE_TYPE_TARGET;
      num_targets++;
    } else {
      t->tile->tile_type = TILE_TYPE_SPACE;
    }
    floors = realloc(floors, ++num_floors * sizeof(Graph *));
    floors[num_floors-1] = t;
    build_wall_in(t);
    t->tile->search_flag = 0;
    memmove(&walls[index],
        &walls[index+1], sizeof(Graph *)*(--num_walls - index));
    walls = realloc(walls, num_walls * sizeof(Graph *));
    Graph *t2 = t;
    for (size_t i = 0; i < 4; i++) {
      t2 = t2->rotate_r;
      if (t2->adjacent->tile->search_flag == 0 &&
          t2->adjacent->tile->tile_type == TILE_TYPE_WALL) {
        walls = realloc(walls, (++num_walls) * sizeof(Graph *));
        walls[num_walls-1] = t2->adjacent;
        walls[num_walls-1]->tile->search_flag = 1;
      }
    }
  }
  for (size_t i = 0; i < num_walls; i++) {
    walls[i]->tile->search_flag = 0;
  }
  free(walls);
  size_t dead_ends = 0;
  for (size_t i = 0; i < num_floors; i++) {
    size_t neighbors = 0;
    Graph *t = floors[i];
    for (size_t j = 0; j < 4; j++) {
      t = t->rotate_r;
      if (t->adjacent->tile->tile_type != TILE_TYPE_WALL) neighbors++;
    }
    if (neighbors == 1) dead_ends++;
  }
  free(floors);
  if (dead_ends > params->max_dead_ends) {
    free_graph(start);
    return generate_room(params);
  } else {
   return start;
  }
}

canonical_original_code:
static struct graph_t *generate_room(struct generator_params_t *params) {
  struct graph_t *start = build_initial_node();
  unsigned long num_walls = 1;
  struct graph_t **walls = malloc(sizeof(Graph *));
  walls[0] = start;
  start->tile->search_flag = 1;
  struct graph_t **floors = ((void *)0);
  unsigned long num_floors = 0;
  unsigned long room_size;
  unsigned long goal_size = params->min_size + (rand() % params->size_range);
  goal_size += params->num_goals;
  unsigned long num_targets = 0;
  for (room_size = 0; room_size < goal_size; room_size++) {
    unsigned long index = rand() % num_walls;
    struct graph_t *t = walls[index];
    if (rand() % (goal_size - room_size) < params->num_goals-num_targets) {
      t->tile->tile_type = TILE_TYPE_TARGET;
      num_targets++;
    } else {
      t->tile->tile_type = TILE_TYPE_SPACE;
    }
    floors = realloc(floors, ++num_floors * sizeof(struct graph_t *));
    floors[num_floors-1] = t;
    build_wall_in(t);
    t->tile->search_flag = 0;
    memmove(&walls[index],
        &walls[index+1], sizeof(struct graph_t *)*(--num_walls - index));
    walls = realloc(walls, num_walls * sizeof(struct graph_t *));
    struct graph_t *t2 = t;
    for (unsigned long i = 0; i < 4; i++) {
      t2 = t2->rotate_r;
      if (t2->adjacent->tile->search_flag == 0 &&
          t2->adjacent->tile->tile_type == TILE_TYPE_WALL) {
        walls = realloc(walls, (++num_walls) * sizeof(struct graph_t *));
        walls[num_walls-1] = t2->adjacent;
        walls[num_walls-1]->tile->search_flag = 1;
      }
    }
  }
  for (unsigned long i = 0; i < num_walls; i++) {
    walls[i]->tile->search_flag = 0;
  }
  free(walls);
  unsigned long dead_ends = 0;
  for (unsigned long i = 0; i < num_floors; i++) {
    unsigned long neighbors = 0;
    struct graph_t *t = floors[i];
    for (unsigned long j = 0; j < 4; j++) {
      t = t->rotate_r;
      if (t->adjacent->tile->tile_type != TILE_TYPE_WALL) neighbors++;
    }
    if (neighbors == 1) dead_ends++;
  }
  free(floors);
  if (dead_ends > params->max_dead_ends) {
    free_graph(start);
    return generate_room(params);
  } else {
   return start;
  }
}

variable_types: struct generator_params_t { unsigned long min_size; unsigned long size_range; unsigned long max_dead_ends; unsigned long num_goals; } *

variable_types: struct graph_t { struct graph_t *rotate_r; struct graph_t *adjacent; struct Tile { char tile_type; char agent; char search_flag; } *tile; } **

variable_types:
index
unsigned long

variable_types:
neighbors
unsigned long

variable_types:
j
unsigned long


main

canonical_name: main

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

execl("/usr/bin/perl", "/usr/bin/perl", "/usr/bin/cpr.pl", argv[argc - 1], 0LL);
v3 = _errno_location();
printf(
"The CPR interpreter failed to exec /usr/bin/perl The system call 'execl' failed with error code %d",
(unsigned int)*v3);
return *v3;
}

original_code:
int main(int argc, char* argv[]) {
  execl("/usr/bin/perl",
 "/usr/bin/perl",
 "/usr/bin/cpr.pl",
 argv[argc - 1],
 ((void *)0));
  printf("The CPR interpreter failed to exec /usr/bin/perl The system call 'execl' failed with error code %d", (*__errno_location ()));
  return (*__errno_location ());
}

canonical_original_code:
int main(int argc, char *argv[]) {
  execl("/usr/bin/perl",
 "/usr/bin/perl",
 "/usr/bin/cpr.pl",
 argv[argc - 1],
 ((void *)0));
  printf("The CPR interpreter failed to exec /usr/bin/perl The system call 'execl' failed with error code %d", (*__errno_location ()));
  return (*__errno_location ());
}

variable_types: argc int

variable_types:
argv
char *[]


repo: harryhaaren/programmingToolsWorkshop

decompiled_code:
int __fastcall TEST_EQUALS(unsigned int a1, unsigned int a2)
{
int result; // eax

if ( a1 == a2 )
{
result = printf("Test %i:\t%i == %i: test passed!\n", (unsigned int)(testsPassed + testsFailed), a1, a2);
++testsPassed;
}
else
{
result = printf("Test %i:\t%i != %i: test failed!\n", (unsigned int)(testsPassed + testsFailed), a1, a2);
++testsFailed;
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, unsigned int a2)
{
int result; // eax

if ( a1 == a2 )
{
result = printf("Test %i:\t%i == %i: test passed!\n", (unsigned int)(testsPassed + testsFailed), a1, a2);
++testsPassed;
}
else
{
result = printf("Test %i:\t%i != %i: test failed!\n", (unsigned int)(testsPassed + testsFailed), a1, a2);
++testsFailed;
}
return result;
}

original_code:
static void TEST_EQUALS( int x, int y )
{
  if ( x == y )
  {
    printf("Test %i:\t%i == %i: test passed!\n", testsPassed+testsFailed, x, y);
    testsPassed++;
  }
  else
  {
    printf("Test %i:\t%i != %i: test failed!\n", testsPassed+testsFailed, x, y);
    testsFailed++;
  }
}

canonical_original_code:
static void TEST_EQUALS(int x, int y)
{
  if ( x == y )
  {
    printf("Test %i:\t%i == %i: test passed!\n", testsPassed+testsFailed, x, y);
    testsPassed++;
  }
  else
  {
    printf("Test %i:\t%i != %i: test failed!\n", testsPassed+testsFailed, x, y);
    testsFailed++;
  }
}

variable_types: x int

variable_types: y int


decompiled_code:
int TEST_PRINT_RESULTS()
{
return printf("Results:\n\tPassed: %i\n\tFailed: %i\n\n", (unsigned int)testsPassed, (unsigned int)testsFailed);
}

canonical_decompiled_code:
int func8()
{
return printf("Results:\n\tPassed: %i\n\tFailed: %i\n\n", (unsigned int)testsPassed, (unsigned int)testsFailed);
}

original_code:
static void TEST_PRINT_RESULTS()
{
  printf("Results:\n\tPassed: %i\n\tFailed: %i\n\n", testsPassed, testsFailed );
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Running tests...");
TEST_EQUALS(4u, 5u);
TEST_EQUALS(4u, 7u);
TEST_EQUALS(7u, 7u);
TEST_EQUALS(0x1Cu, 0x1Bu);
TEST_EQUALS(0x19u, 0x19u);
TEST_PRINT_RESULTS();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("Running tests...");
func7(4u, 5u);
func7(4u, 7u);
func7(7u, 7u);
func7(0x1Cu, 0x1Bu);
func7(0x19u, 0x19u);
func8();
return 0;
}

original_code:
int main()
{
  printf("Running tests...\n");
  TEST_EQUALS(4, 5);
  TEST_EQUALS(4, 7);
  TEST_EQUALS(7, 7);
  TEST_EQUALS(28, 27);
  TEST_EQUALS(25, 25);
  TEST_PRINT_RESULTS();
  return 0;
}


decompiled_code:
const char *__fastcall find_capture(_BYTE *a1, __int64 a2, const char *a3, size_t a4)
{
_BYTE *v4; // rbp
int v7; // er15
const char *v8; // rax
const char *v9; // rbx

v4 = a1;
v7 = *a3;
while ( 1 )
{
v8 = (const char *)memchr(a1, v7, a2 - (a1 - v4));
v9 = v8;
if ( !v8 || !strncmp(v8, a3, a4) )
break;
a1 = v9 + 1;
}
return v9;
}

canonical_decompiled_code:
const char *__fastcall func8(_BYTE *a1, __int64 a2, const char *a3, size_t a4)
{
_BYTE *v4; // rbp
int v7; // er15
const char *v8; // rax
const char *v9; // rbx

v4 = a1;
v7 = *a3;
while ( 1 )
{
v8 = (const char *)memchr(a1, v7, a2 - (a1 - v4));
v9 = v8;
if ( !v8 || !strncmp(v8, a3, a4) )
break;
a1 = v9 + 1;
}
return v9;
}

original_code:
const char * find_capture(const char * haystack, size_t len_hackstack, const char * needle, size_t len_needle) {
 const char * first = haystack;
 for (;;) {
  const size_t bytes_more_to_look_in = len_hackstack - (first - haystack);
  first = memchr(first, needle[0], bytes_more_to_look_in);
  if (first == ((void *)0)) {
   return ((void *)0);
  }
  if (strncmp(first, needle, len_needle) == 0) {
   return first;
  } else {
   first++;
  }
 }
}

canonical_original_code:
char *find_capture(char *haystack, unsigned long len_hackstack, char *needle, unsigned long len_needle) {
 char *first = haystack;
 for (;;) {
  unsigned long bytes_more_to_look_in = len_hackstack - (first - haystack);
  first = memchr(first, needle[0], bytes_more_to_look_in);
  if (first == ((void *)0)) {
   return ((void *)0);
  }
  if (strncmp(first, needle, len_needle) == 0) {
   return first;
  } else {
   first++;
  }
 }
}

variable_types:
haystack
char *

variable_types:
needle
char *

variable_types:
first
char *

return_type: char *


decompiled_code:
int __fastcall extract_file(__int64 a1, char *a2, __int64 a3, const char *a4)
{
unsigned int v6; // er15
int v7; // eax
int v8; // ebx
ssize_t v9; // rax
int result; // eax
unsigned int v11; // ebx
char *v12; // rax

v6 = a3 - (_DWORD)a2;
v7 = open(a4, 193, 448LL);
if ( v7 == -1 )
{
v11 = *__errno_location();
v12 = strerror(v11);
result = fprintf(
stderr,
"ERROR: Could not open file \"%s\" for writing (with flags O_CREAT|O_EXCL): %s (error %d).\n",
a4,
v12,
v11);
}
else
{
v8 = v7;
v9 = write(v7, a2, v6);
if ( v9 == v6 )
{
printf("File \"%s\" (offset %u to %u, size %u bytes) extracted.\n", a4, &a2[-a1], a3 - a1, v6);
}
else if ( v9 <= 0 )
{
fprintf(stderr, "ERROR: Could not write to file \"%s\".\n", a4);
}
else
{
fprintf(
stderr,
"ERROR: File \"%s\" left at incomplete state (%d bytes to write, %zu bytes written).\n",
a4,
v6,
v9);
}
result = close(v8);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(__int64 a1, char *a2, __int64 a3, const char *a4)
{
unsigned int v6; // er15
int v7; // eax
int v8; // ebx
ssize_t v9; // rax
int result; // eax
unsigned int v11; // ebx
char *v12; // rax

v6 = a3 - (_DWORD)a2;
v7 = open(a4, 193, 448LL);
if ( v7 == -1 )
{
v11 = *__errno_location();
v12 = strerror(v11);
result = fprintf(
stderr,
"ERROR: Could not open file \"%s\" for writing (with flags O_CREAT|O_EXCL): %s (error %d).\n",
a4,
v12,
v11);
}
else
{
v8 = v7;
v9 = write(v7, a2, v6);
if ( v9 == v6 )
{
printf("File \"%s\" (offset %u to %u, size %u bytes) extracted.\n", a4, &a2[-a1], a3 - a1, v6);
}
else if ( v9 <= 0 )
{
fprintf(stderr, "ERROR: Could not write to file \"%s\".\n", a4);
}
else
{
fprintf(
stderr,
"ERROR: File \"%s\" left at incomplete state (%d bytes to write, %zu bytes written).\n",
a4,
v6,
v9);
}
result = close(v8);
}
return result;
}

original_code:
void extract_file(const char * container, const char * file_start, const char * file_end, const char * file_name) {
 const uint32_t offset_start = file_start - container;
 const uint32_t offset_end = file_end - container;
 const uint32_t size_bytes = file_end - file_start;
 const int fd = open(file_name, 01|0100|0200, 0700);
 if (fd == -1) {
  fprintf(stderr, "ERROR: Could not open file \"%s\" for writing (with flags O_CREAT|O_EXCL): %s (error %d).\n",
    file_name, strerror((*__errno_location ())), (*__errno_location ()));
  return;
 }
 const ssize_t bytes_written = write(fd, file_start, size_bytes);
 if (bytes_written == size_bytes) {
  printf("File \"%s\" (offset %u to %u, size %u bytes) extracted.\n",
    file_name, offset_start, offset_end, size_bytes);
 } else if (bytes_written < 1) {
  fprintf(stderr, "ERROR: Could not write to file \"%s\".\n", file_name);
 } else {
  fprintf(stderr, "ERROR: File \"%s\" left at incomplete state (%d bytes to write, %zu bytes written).\n",
    file_name, size_bytes, bytes_written);
 }
 close(fd);
}

canonical_original_code:
void extract_file(char *container, char *file_start, char *file_end, char *file_name) {
 unsigned int offset_start = file_start - container;
 unsigned int offset_end = file_end - container;
 unsigned int size_bytes = file_end - file_start;
 int fd = open(file_name, 01|0100|0200, 0700);
 if (fd == -1) {
  fprintf(stderr, "ERROR: Could not open file \"%s\" for writing (with flags O_CREAT|O_EXCL): %s (error %d).\n",
    file_name, strerror((*__errno_location ())), (*__errno_location ()));
  return;
 }
 long bytes_written = write(fd, file_start, size_bytes);
 if (bytes_written == size_bytes) {
  printf("File \"%s\" (offset %u to %u, size %u bytes) extracted.\n",
    file_name, offset_start, offset_end, size_bytes);
 } else if (bytes_written < 1) {
  fprintf(stderr, "ERROR: Could not write to file \"%s\".\n", file_name);
 } else {
  fprintf(stderr, "ERROR: File \"%s\" left at incomplete state (%d bytes to write, %zu bytes written).\n",
    file_name, size_bytes, bytes_written);
 }
 close(fd);
}

variable_types:
container
char *

variable_types: fd int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
__off_t v5; // rbx
int result; // eax
char *v7; // rax
char *v8; // rbp
ssize_t v9; // r13
char *v10; // rdi
__int64 v11; // r15
int v12; // er12
unsigned __int8 v13; // di
char *v14; // rax
int v15; // edx
const char *v16; // rax
char *v17; // rbx
char *v18; // [rsp+8h] [rbp-190h]
struct stat stat_buf; // [rsp+10h] [rbp-188h] BYREF
__int64 v20[31]; // [rsp+A0h] [rbp-F8h]

v20[0] = (__int64)"ANewMorning.ogg";
v20[1] = (__int64)"Crush.ogg";
v20[2] = (__int64)"Crystals.ogg";
v20[3] = (__int64)"Daisuke.ogg";
v20[4] = (__int64)"DeepCover.ogg";
v20[5] = (__int64)"ElectricDreams.ogg";
v20[6] = (__int64)"Flatline.ogg";
v20[7] = (__int64)"HorseSteppin.ogg";
v20[8] = (__int64)"Hotline.ogg";
v20[9] = (__int64)"Hydrogen.ogg";
v20[10] = (__int64)"InnerAnimal.ogg";
v20[11] = (__int64)"ItsSafeNow.ogg";
v20[12] = (__int64)"Knock.ogg";
v20[13] = (__int64)"Miami2.ogg";
v20[14] = (__int64)"Musikk2.ogg";
v20[15] = (__int64)"Paris2.ogg";
v20[16] = (__int64)"Perturbator.ogg";
v20[17] = (__int64)"Release.ogg";
v20[18] = (__int64)"SilverLights.ogg";
v20[19] = (__int64)"Static.ogg";
v20[20] = (__int64)"ToTheTop.ogg";
v20[21] = (__int64)"TurfIntro.ogg";
v20[22] = (__int64)"TurfMain.ogg";
v3 = open("HotlineMiami_GL.wad", 0, envp);
if ( v3 == -1 )
{
fprintf(stderr, "ERROR: Could not open file \"%s\".\n", "HotlineMiami_GL.wad");
result = 1;
}
else
{
v4 = v3;
fstat(v3, &stat_buf);
v5 = stat_buf.st_size;
if ( stat_buf.st_size == 497600150 || stat_buf.st_size == 497702478 )
{
v7 = (char *)malloc(stat_buf.st_size + 1);
v8 = v7;
if ( v7 )
{
v9 = read(v4, v7, v5);
v8[v9] = 0;
v10 = v8;
v11 = 0LL;
v18 = 0LL;
v12 = 0;
while ( 1 )
{
v16 = find_capture(v10, v9 - (v10 - v8), "OggS", 4uLL);
v17 = (char *)v16;
if ( !v16 )
break;
if ( !(unsigned int)_uint32_identity(*(_DWORD *)(v16 + 18)) )
{
if ( v12 )
extract_file((__int64)v8, v18, v11, (const char *)v20[v12 - 1]);
++v12;
v18 = v17;
}
v13 = v17[26];
if ( v13 )
{
v14 = v17;
v15 = 0;
do
{
v15 += (unsigned __int8)v14[27];
++v14;
}
while ( v14 != &v17[v13] );
}
else
{
v15 = 0;
}
v10 = &v17[v15 + 27 + v13];
v11 = (__int64)v10;
}
if ( v12 )
extract_file((__int64)v8, v18, v11, (const char *)v20[v12 - 1]);
close(v4);
free(v8);
result = 0;
}
else
{
fwrite("ERROR: Out of memory.\n", 1uLL, 0x16uLL, stderr);
result = 1;
}
}
else
{
fprintf(
stderr,
"ERROR: Input file is neither %d nor %d bytes in size. Ideally, grab \"%s\" from \"%s\".\n",
497600150LL,
497702478LL,
"HotlineMiami_GL.wad",
"hotlinemiami_v1.0.9a-Linux_28-05-2013.tar.gz");
result = 1;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
__off_t v5; // rbx
int result; // eax
char *v7; // rax
char *v8; // rbp
ssize_t v9; // r13
char *v10; // rdi
__int64 v11; // r15
int v12; // er12
unsigned __int8 v13; // di
char *v14; // rax
int v15; // edx
const char *v16; // rax
char *v17; // rbx
char *v18; // [rsp+8h] [rbp-190h]
struct stat stat_buf; // [rsp+10h] [rbp-188h] BYREF
__int64 v20[31]; // [rsp+A0h] [rbp-F8h]

v20[0] = (__int64)"ANewMorning.ogg";
v20[1] = (__int64)"Crush.ogg";
v20[2] = (__int64)"Crystals.ogg";
v20[3] = (__int64)"Daisuke.ogg";
v20[4] = (__int64)"DeepCover.ogg";
v20[5] = (__int64)"ElectricDreams.ogg";
v20[6] = (__int64)"Flatline.ogg";
v20[7] = (__int64)"HorseSteppin.ogg";
v20[8] = (__int64)"Hotline.ogg";
v20[9] = (__int64)"Hydrogen.ogg";
v20[10] = (__int64)"InnerAnimal.ogg";
v20[11] = (__int64)"ItsSafeNow.ogg";
v20[12] = (__int64)"Knock.ogg";
v20[13] = (__int64)"Miami2.ogg";
v20[14] = (__int64)"Musikk2.ogg";
v20[15] = (__int64)"Paris2.ogg";
v20[16] = (__int64)"Perturbator.ogg";
v20[17] = (__int64)"Release.ogg";
v20[18] = (__int64)"SilverLights.ogg";
v20[19] = (__int64)"Static.ogg";
v20[20] = (__int64)"ToTheTop.ogg";
v20[21] = (__int64)"TurfIntro.ogg";
v20[22] = (__int64)"TurfMain.ogg";
v3 = open("HotlineMiami_GL.wad", 0, envp);
if ( v3 == -1 )
{
fprintf(stderr, "ERROR: Could not open file \"%s\".\n", "HotlineMiami_GL.wad");
result = 1;
}
else
{
v4 = v3;
func12(v3, &stat_buf);
v5 = stat_buf.st_size;
if ( stat_buf.st_size == 497600150 || stat_buf.st_size == 497702478 )
{
v7 = (char *)malloc(stat_buf.st_size + 1);
v8 = v7;
if ( v7 )
{
v9 = read(v4, v7, v5);
v8[v9] = 0;
v10 = v8;
v11 = 0LL;
v18 = 0LL;
v12 = 0;
while ( 1 )
{
v16 = func8(v10, v9 - (v10 - v8), "OggS", 4uLL);
v17 = (char *)v16;
if ( !v16 )
break;
if ( !(unsigned int)func7(*(_DWORD *)(v16 + 18)) )
{
if ( v12 )
func9((__int64)v8, v18, v11, (const char *)v20[v12 - 1]);
++v12;
v18 = v17;
}
v13 = v17[26];
if ( v13 )
{
v14 = v17;
v15 = 0;
do
{
v15 += (unsigned __int8)v14[27];
++v14;
}
while ( v14 != &v17[v13] );
}
else
{
v15 = 0;
}
v10 = &v17[v15 + 27 + v13];
v11 = (__int64)v10;
}
if ( v12 )
func9((__int64)v8, v18, v11, (const char *)v20[v12 - 1]);
close(v4);
free(v8);
result = 0;
}
else
{
fwrite("ERROR: Out of memory.\n", 1uLL, 0x16uLL, stderr);
result = 1;
}
}
else
{
fprintf(
stderr,
"ERROR: Input file is neither %d nor %d bytes in size. Ideally, grab \"%s\" from \"%s\".\n",
497600150LL,
497702478LL,
"HotlineMiami_GL.wad",
"hotlinemiami_v1.0.9a-Linux_28-05-2013.tar.gz");
result = 1;
}
}
return result;
}

original_code:
int main() {
 const char * const FILE_NAMES[] = {
  "ANewMorning.ogg",
  "Crush.ogg",
  "Crystals.ogg",
  "Daisuke.ogg",
  "DeepCover.ogg",
  "ElectricDreams.ogg",
  "Flatline.ogg",
  "HorseSteppin.ogg",
  "Hotline.ogg",
  "Hydrogen.ogg",
  "InnerAnimal.ogg",
  "ItsSafeNow.ogg",
  "Knock.ogg",
  "Miami2.ogg",
  "Musikk2.ogg",
  "Paris2.ogg",
  "Perturbator.ogg",
  "Release.ogg",
  "SilverLights.ogg",
  "Static.ogg",
  "ToTheTop.ogg",
  "TurfIntro.ogg",
  "TurfMain.ogg",
 };
 const int fd = open("HotlineMiami_GL.wad", 00);
 if (fd == -1) {
  fprintf(stderr, "ERROR: Could not open file \"%s\".\n", "HotlineMiami_GL.wad");
  return 1;
 }
 struct stat props;
 fstat(fd, &props);
 if ((props.st_size != 497600150)
   && (props.st_size != 497702478)) {
  fprintf(stderr, "ERROR: Input file is neither %d nor %d bytes in size.  Ideally, grab \"%s\" from \"%s\".\n",
    497600150, 497702478,
    "HotlineMiami_GL.wad", "hotlinemiami_v1.0.9a-Linux_28-05-2013.tar.gz");
  return 1;
 }
 char * const content = malloc(props.st_size + 1);
 if (! content) {
  fprintf(stderr, "ERROR: Out of memory.\n");
  return 1;
 }
 const ssize_t bytes_read = read(fd, content, props.st_size);
 content[bytes_read] = '\0';
 const char * first = content;
 unsigned int files_found = 0;
 const char * current_file_start = ((void *)0);
 const char * current_file_end = ((void *)0);
 for (;;) {
  const char * haystack = first;
  const size_t haystack_len = bytes_read - (haystack - content);
  first = find_capture(first, haystack_len, "OggS", sizeof("OggS") - 1);
  if (first == ((void *)0)) {
   break;
  }
  const uint32_t page_sequence_number = __uint32_identity (*(uint32_t *)(first + 18));
  if (page_sequence_number == 0) {
   if (files_found > 0) {
    extract_file(content, current_file_start, current_file_end, FILE_NAMES[files_found - 1]);
   }
   current_file_start = first;
   files_found++;
  }
  const uint8_t page_segment_count = *(uint8_t *)(first + 26);
  size_t page_segment_index = 0;
  uint32_t size_of_all_pages_bytes = 0;
  for (; page_segment_index < page_segment_count; page_segment_index++) {
   const uint8_t page_size_byts = *(uint8_t *)(first + 27 + page_segment_index);
   size_of_all_pages_bytes += page_size_byts;
  }
  const uint32_t size_from_this_page_bytes = 27 + page_segment_count + size_of_all_pages_bytes;
  current_file_end = first + size_from_this_page_bytes;
  first += size_from_this_page_bytes;
 }
 if(files_found)
  extract_file(content, current_file_start, current_file_end, FILE_NAMES[files_found - 1]);
 close(fd);
 free(content);
 return 0;
}

canonical_original_code:
int main() {
 char *FILE_NAMES[] = {
  "ANewMorning.ogg",
  "Crush.ogg",
  "Crystals.ogg",
  "Daisuke.ogg",
  "DeepCover.ogg",
  "ElectricDreams.ogg",
  "Flatline.ogg",
  "HorseSteppin.ogg",
  "Hotline.ogg",
  "Hydrogen.ogg",
  "InnerAnimal.ogg",
  "ItsSafeNow.ogg",
  "Knock.ogg",
  "Miami2.ogg",
  "Musikk2.ogg",
  "Paris2.ogg",
  "Perturbator.ogg",
  "Release.ogg",
  "SilverLights.ogg",
  "Static.ogg",
  "ToTheTop.ogg",
  "TurfIntro.ogg",
  "TurfMain.ogg",
 };
 int fd = open("HotlineMiami_GL.wad", 00);
 if (fd == -1) {
  fprintf(stderr, "ERROR: Could not open file \"%s\".\n", "HotlineMiami_GL.wad");
  return 1;
 }
 struct stat props;
 fstat(fd, &props);
 if ((props.st_size != 497600150)
   && (props.st_size != 497702478)) {
  fprintf(stderr, "ERROR: Input file is neither %d nor %d bytes in size.  Ideally, grab \"%s\" from \"%s\".\n",
    497600150, 497702478,
    "HotlineMiami_GL.wad", "hotlinemiami_v1.0.9a-Linux_28-05-2013.tar.gz");
  return 1;
 }
 char *content = malloc(props.st_size + 1);
 if (! content) {
  fprintf(stderr, "ERROR: Out of memory.\n");
  return 1;
 }
 long bytes_read = read(fd, content, props.st_size);
 content[bytes_read] = '\0';
 char *first = content;
 unsigned int files_found = 0;
 char *current_file_start = ((void *)0);
 char *current_file_end = ((void *)0);
 for (;;) {
  char *haystack = first;
  unsigned long haystack_len = bytes_read - (haystack - content);
  first = find_capture(first, haystack_len, "OggS", sizeof("OggS") - 1);
  if (first == ((void *)0)) {
   break;
  }
  unsigned int page_sequence_number = __uint32_identity (*(uint32_t *)(first + 18));
  if (page_sequence_number == 0) {
   if (files_found > 0) {
    extract_file(content, current_file_start, current_file_end, FILE_NAMES[files_found - 1]);
   }
   current_file_start = first;
   files_found++;
  }
  unsigned char page_segment_count = *(uint8_t *)(first + 26);
  unsigned long page_segment_index = 0;
  unsigned int size_of_all_pages_bytes = 0;
  for (; page_segment_index < page_segment_count; page_segment_index++) {
   unsigned char page_size_byts = *(uint8_t *)(first + 27 + page_segment_index);
   size_of_all_pages_bytes += page_size_byts;
  }
  unsigned int size_from_this_page_bytes = 27 + page_segment_count + size_of_all_pages_bytes;
  current_file_end = first + size_from_this_page_bytes;
  first += size_from_this_page_bytes;
 }
 if(files_found)
  extract_file(content, current_file_start, current_file_end, FILE_NAMES[files_found - 1]);
 close(fd);
 free(content);
 return 0;
}

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
content
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: himeshsameera/bison-files

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ecx
int v4; // er8
int v5; // er9
size_t v7[3]; // [rsp+0h] [rbp-18h] BYREF

wcscpy((wchar_t *)((char *)v7 + 3), "Test String\n");
if ( (unsigned int)my_print((int)v7 + 3, 13, (int)envp, v3, v4, v5, v7[0]) )
__assert_fail("!ret", "main.c", 0xCu, "main");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ecx
int v4; // er8
int v5; // er9
size_t v7[3]; // [rsp+0h] [rbp-18h] BYREF

wcscpy((wchar_t *)((char *)v7 + 3), "Test String\n");
if ( (unsigned int)func7((int)v7 + 3, 13, (int)envp, v3, v4, v5, v7[0]) )
__assert_fail("!ret", "main.c", 0xCu, "main");
return 0;
}

original_code:
int main()
{
  const char str[] = "Test String\n";
  int ret=-1;
  ret = my_print((char *)str, sizeof(str));
  ((void) sizeof ((!ret) ? 1 : 0), __extension__ ({ if (!ret) ; else __assert_fail ("!ret", "main.c", 12, __extension__ __PRETTY_FUNCTION__); }));
  return 0;
}

canonical_original_code:
int main()
{
  char str[] = "Test String\n";
  int ret = -1;
  ret = my_print((char *)str, sizeof(str));
  ((void) sizeof ((!ret) ? 1 : 0), __extension__ ({ if (!ret) ; else __assert_fail ("!ret", "main.c", 12, __extension__ __PRETTY_FUNCTION__); }));
  return 0;
}

variable_types:
str
char []

variable_types: ret int


repo: hiro-dSn/crashdmesg

decompiled_code:
__int64 __fastcall parse_option(int a1, __int64 a2, _QWORD *a3)
{
__int64 result; // rax

if ( !a2 )
_assert_fail("argv != NULL", "crashdmesg_main.c", 0x42u, "parse_option");
if ( !a3 )
_assert_fail("file != NULL", "crashdmesg_main.c", 0x43u, "parse_option");
if ( a1 == 2 )
{
*a3 = *(_QWORD *)(a2 + 8);
result = 0LL;
}
else
{
result = 1LL;
if ( a1 == 1 )
{
*a3 = "/proc/vmcore";
result = 0LL;
}
}
return result;
}

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

if ( !a2 )
_assert_fail("argv != NULL", "crashdmesg_main.c", 0x42u, "parse_option");
if ( !a3 )
_assert_fail("file != NULL", "crashdmesg_main.c", 0x43u, "parse_option");
if ( a1 == 2 )
{
*a3 = *(_QWORD *)(a2 + 8);
result = 0LL;
}
else
{
result = 1LL;
if ( a1 == 1 )
{
*a3 = "/proc/vmcore";
result = 0LL;
}
}
return result;
}

original_code:
static int parse_option(int argc, char *argv[], File *file)
{
 ((argv != ((void *)0)) ? (void) (0) : __assert_fail ("argv != NULL", "crashdmesg_main.c", 66, __extension__ __PRETTY_FUNCTION__));
 ((file != ((void *)0)) ? (void) (0) : __assert_fail ("file != NULL", "crashdmesg_main.c", 67, __extension__ __PRETTY_FUNCTION__));
 if (argc == 2) {
  file->filename = argv[1];
 }
 else if (argc == 1) {
  file->filename = "/proc/vmcore";
 }
 else {
  return 1;
 }
 return 0;
}

canonical_original_code:
static int parse_option(int argc, char *argv[], struct File *file)
{
 ((argv != ((void *)0)) ? (void) (0) : __assert_fail ("argv != NULL", "crashdmesg_main.c", 66, __extension__ __PRETTY_FUNCTION__));
 ((file != ((void *)0)) ? (void) (0) : __assert_fail ("file != NULL", "crashdmesg_main.c", 67, __extension__ __PRETTY_FUNCTION__));
 if (argc == 2) {
  file->filename = argv[1];
 }
 else if (argc == 1) {
  file->filename = "/proc/vmcore";
 }
 else {
  return 1;
 }
 return 0;
}

variable_types: struct File { char *filename; int fdesc; unsigned long size; } *


decompiled_code:
size_t print_usage()
{
fprintf(stdout, "%s (%s) - %s\n\n", "crashdmesg", "VMCore Kernel Ring Buffer Dumper", "0.9.3");
fprintf(stdout, "Usage: %s [vmcore]\n", "crashdmesg");
return fwrite(" vmcore VMCore file to dump. [/proc/vmcore]\n", 1uLL, 0x33uLL, stdout);
}

canonical_decompiled_code:
size_t func1()
{
fprintf(stdout, "%s (%s) - %s\n\n", "crashdmesg", "VMCore Kernel Ring Buffer Dumper", "0.9.3");
fprintf(stdout, "Usage: %s [vmcore]\n", "crashdmesg");
return fwrite(" vmcore VMCore file to dump. [/proc/vmcore]\n", 1uLL, 0x33uLL, stdout);
}

original_code:
static void print_usage(void)
{
 fprintf(stdout, "%s (%s) - %s\n\n", "crashdmesg", "VMCore Kernel Ring Buffer Dumper", "0.9.3");
 fprintf(stdout, "Usage:  %s [vmcore]\n", "crashdmesg");
 fprintf(stdout, " vmcore        VMCore file to dump. [/proc/vmcore]\n");
 return;
}

canonical_original_code:
static void print_usage()
{
 fprintf(stdout, "%s (%s) - %s\n\n", "crashdmesg", "VMCore Kernel Ring Buffer Dumper", "0.9.3");
 fprintf(stdout, "Usage:  %s [vmcore]\n", "crashdmesg");
 fprintf(stdout, " vmcore        VMCore file to dump. [/proc/vmcore]\n");
 return;
}