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

result = ioctl(*(_DWORD *)(a1 + 4), 0x2401uLL, 0LL);
if ( result )
{
puts("\ndisable wrong");
exit(1);
}
*(_DWORD *)(a1 + 24) = 0;
return result;
}

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

result = ioctl(*(_DWORD *)(a1 + 4), 0x2401uLL, 0LL);
if ( result )
{
puts("\ndisable wrong");
exit(1);
}
*(_DWORD *)(a1 + 24) = 0;
return result;
}


repo: tmerrifi/TaskClock

decompiled_code:
int __fastcall perf_counter_close(__int64 a1)
{
if ( ioctl(*(_DWORD *)(a1 + 4), 0x2401uLL, 0LL) )
{
puts("\ndisable wrong");
exit(1);
}
return close(*(_DWORD *)(a1 + 4));
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
if ( ioctl(*(_DWORD *)(a1 + 4), 0x2401uLL, 0LL) )
{
puts("\ndisable wrong");
exit(1);
}
return close(*(_DWORD *)(a1 + 4));
}

variable_types: struct perf_counter_info { int pid; int fd; void *ring_buffer; void *ring_buffer_current; int started; } *

return_type: void


repo: tmichel/chatd

decompiled_code:
ssize_t __fastcall send_welcome(int fd)
{
size_t v1; // rax
ssize_t result; // rax
__int128 v3[3]; // [rsp+0h] [rbp-48h] BYREF
char v4[16]; // [rsp+30h] [rbp-18h] BYREF

strcpy(v4, "assword]'\n");
v3[0] = (__int128)_mm_load_si128((const __m128i *)&LC1);
v3[1] = (__int128)_mm_load_si128((const __m128i *)&LC2);
v3[2] = (__int128)_mm_load_si128((const __m128i *)&LC3);
v1 = strlen((const char *)v3);
result = write(fd, v3, v1);
if ( result < 0 )
result = log_sys(3LL, "Could not send message.");
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(int fd)
{
size_t v1; // rax
ssize_t result; // rax
__int128 v3[3]; // [rsp+0h] [rbp-48h] BYREF
char v4[16]; // [rsp+30h] [rbp-18h] BYREF

strcpy(v4, "assword]'\n");
v3[0] = (__int128)_mm_load_si128((const __m128i *)&LC1);
v3[1] = (__int128)_mm_load_si128((const __m128i *)&LC2);
v3[2] = (__int128)_mm_load_si128((const __m128i *)&LC3);
v1 = strlen((const char *)v3);
result = write(fd, v3, v1);
if ( result < 0 )
result = log_sys(3LL, "Could not send message.");
return result;
}

original_code:
static void
send_welcome(int sock) {
    char msg[60] = {0};
    sprintf(msg, "Welcome!\n"
                 "To register your nick send 'REG nick [password]'\n");
    if (write(sock, msg, strlen(msg)) < 0) {
        log_sys(3, "Could not send message.");
    }
}

variable_types: sock int

variable_types:
msg
char [60]


decompiled_code:
__int64 __fastcall handle_message(__int64 a1, __int64 a2, __int64 *a3, __int64 a4)
{
__int64 *v8; // rdi
__int64 v9; // rdx
unsigned int v10; // er12
__int64 v12; // [rsp+0h] [rbp-38h] BYREF

v8 = &v12;
parse(&v12, a1, a2);
v10 = v12;
if ( (_DWORD)v12 == 1 )
{
*(_DWORD *)a4 = 100;
}
else
{
v8 = a3;
v10 = 0;
*(_DWORD *)a4 = command_execute(a3);
*(_QWORD *)(a4 + 8) = v9;
}
command_destroy(v8, a1, v9);
return v10;
}

original_code:
static int
handle_message(string in, user_t *user, cr_t *res) {
    int quit = 0;
    command_t cmd = parse(in);;
    if (cmd.code == 1) {
        res->code = 100;
        quit = 1;
    } else {
        cr_t result = command_execute(cmd, user);
        res->code = result.code;
        res->user = result.user;
    }
    command_destroy(cmd);
    return quit;
}

canonical_original_code:
static int handle_message(struct string in, struct user_t *user, struct __command_result *res) {
    int quit = 0;
    struct __command cmd = parse(in);;
    if (cmd.code == 1) {
        res->code = 100;
        quit = 1;
    } else {
        struct __command_result result = command_execute(cmd, user);
        res->code = result.code;
        res->user = result.user;
    }
    command_destroy(cmd);
    return quit;
}

variable_types:
in
struct string { int len; char *val; }

variable_types: struct __command_result { int code; struct user_t *user; } *

variable_types: quit int

variable_types: struct __command { int code; struct string { int len; char *val; } args; }

variable_types: struct __command_result { int code; struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *user; }

return_type: int

user_defined_types: struct __command { int code; struct string args; }

user_defined_types: struct user_t { char *username; char *password; int sock; struct vec_t *rooms; }

user_defined_types: struct vec_t { int len; int cap; void **data; union pthread_rwlock_t *lock; }

user_defined_types: union pthread_rwlock_t { struct __pthread_rwlock_arch_t __data; char __size[56]; long __align; }


decompiled_code:
__int64 __fastcall user_quit(__int64 a1)
{
int i; // ebx
__int64 v3[4]; // [rsp+8h] [rbp-20h] BYREF

for ( i = 0; (int)vec_size(*(_QWORD *)(a1 + 24)) > i; ++i )
{
vec_get(*(_QWORD *)(a1 + 24), (unsigned int)i, v3);
room_remove_user(v3[0], a1);
if ( (unsigned int)vec_is_empty(*(_QWORD *)(v3[0] + 16)) )
mem_remove_room(v3[0]);
}
mem_remove_user(a1);
return user_free(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int i; // ebx
__int64 v3[4]; // [rsp+8h] [rbp-20h] BYREF

for ( i = 0; (int)vec_size(*(_QWORD *)(a1 + 24)) > i; ++i )
{
vec_get(*(_QWORD *)(a1 + 24), (unsigned int)i, v3);
room_remove_user(v3[0], a1);
if ( (unsigned int)vec_is_empty(*(_QWORD *)(v3[0] + 16)) )
mem_remove_room(v3[0]);
}
mem_remove_user(a1);
return user_free(a1);
}

original_code:
static void
user_quit(user_t *user) {
    room_t *r;
    for (int i = 0; i < vec_size(user->rooms); ++i) {
        vec_get(user->rooms, i, (any_t*)&r);
        room_remove_user(r, user);
        if (vec_is_empty(r->users)) {
            mem_remove_room(r);
        }
    }
    mem_remove_user(user);
    user_free(user);
}

canonical_original_code:
static void user_quit(struct user_t *user) {
    struct __room *r;
    for (int i = 0; i < vec_size(user->rooms); ++i) {
        vec_get(user->rooms, i, (void **)&r);
        room_remove_user(r, user);
        if (vec_is_empty(r->users)) {
            mem_remove_room(r);
        }
    }
    mem_remove_user(user);
    user_free(user);
}

variable_types: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *

variable_types: struct __room { char *name; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *admins; struct vec_t *users; struct vec_t *muted; } *

variable_types: i int

user_defined_types: struct __room { char *name; struct vec_t *admins; struct vec_t *users; struct vec_t *muted; }


decompiled_code:
__int64 __fastcall open_serv_sock(unsigned __int16 a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
__int16 v4; // ax
int v5; // edx
const void *v6; // rcx
socklen_t v7; // er8
int v9; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
v3 = -1;
log_sys(3LL, "Could not create socket");
}
else
{
v3 = v2;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32_constprop_0(a1, 1LL, 2LL, &v9, 4LL);
v4 = _bswap_16(a1);
v9 = 1;
*(_WORD *)addr.sa_data = v4;
setsockopt(v3, 1, v5, v6, v7);
bind(v3, &addr, 0x10u);
if ( listen(v3, a2) < 0 )
{
v3 = -1;
log_sys(3LL, "Could not listen on socket");
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
__int16 v4; // ax
int v5; // edx
const void *v6; // rcx
socklen_t v7; // er8
int v9; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
v3 = -1;
log_sys(3LL, "Could not create socket");
}
else
{
v3 = v2;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func4(a1, 1LL, 2LL, &v9, 4LL);
v4 = func3(a1);
v9 = 1;
*(_WORD *)addr.sa_data = v4;
setsockopt(v3, 1, v5, v6, v7);
bind(v3, &addr, 0x10u);
if ( listen(v3, a2) < 0 )
{
v3 = -1;
log_sys(3LL, "Could not listen on socket");
}
}
return v3;
}

original_code:
static int
open_serv_sock(const int port, const int max_conn) {
    int serv_sock;
    if ((serv_sock = socket(2, SOCK_STREAM, 0)) < 0) {
        log_sys(3, "Could not create socket");
        return -1;
    }
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    serv_addr.sin_port = htons(port);
    int reuse = 1;
    setsockopt(serv_sock, 1, 2, &reuse, sizeof(reuse));
    bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if ((listen(serv_sock, max_conn)) < 0) {
        log_sys(3, "Could not listen on socket");
        return -1;
    }
    return serv_sock;
}

canonical_original_code:
static int open_serv_sock(int port, int max_conn) {
    int serv_sock;
    if ((serv_sock = socket(2, SOCK_STREAM, 0)) < 0) {
        log_sys(3, "Could not create socket");
        return -1;
    }
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    serv_addr.sin_port = htons(port);
    int reuse = 1;
    setsockopt(serv_sock, 1, 2, &reuse, sizeof(reuse));
    bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if ((listen(serv_sock, max_conn)) < 0) {
        log_sys(3, "Could not listen on socket");
        return -1;
    }
    return serv_sock;
}

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

variable_types: reuse 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:
void *__fastcall handle_conn(void *a1)
{
__int64 *v1; // r13
int v2; // er12
int v3; // ebx
__int64 v4; // rdx
__int64 v5; // rax
__int64 v6; // rdx
int v7; // ebp
__int64 v8; // rax
unsigned int v10; // [rsp+0h] [rbp-438h] BYREF
__int64 v11; // [rsp+8h] [rbp-430h]
__int128 buf; // [rsp+10h] [rbp-428h] BYREF
char v13[1008]; // [rsp+20h] [rbp-418h] BYREF

v1 = 0LL;
v2 = *(_DWORD *)a1;
send_welcome(*(_DWORD *)a1);
buf = 0LL;
memset(v13, 0, sizeof(v13));
do
{
v3 = read(v2, &buf, 0x400uLL);
if ( v3 <= 0 )
break;
v10 = cr_empty();
v11 = v4;
v5 = str_newn(&buf, (unsigned int)v3);
v7 = handle_message(v5, v6, v1, (__int64)&v10);
v8 = v11;
if ( v11 )
{
*(_DWORD *)(v11 + 16) = v2;
v1 = (__int64 *)v8;
}
send_response_isra_0(v2, v10);
memset(&buf, 0, v3);
}
while ( !v7 );
if ( v1 )
user_quit((__int64)v1);
log_sys(6LL, "Client exited.");
close(v2);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
__int64 *v1; // r13
int v2; // er12
int v3; // ebx
__int64 v4; // rdx
__int64 v5; // rax
__int64 v6; // rdx
int v7; // ebp
__int64 v8; // rax
unsigned int v10; // [rsp+0h] [rbp-438h] BYREF
__int64 v11; // [rsp+8h] [rbp-430h]
__int128 buf; // [rsp+10h] [rbp-428h] BYREF
char v13[1008]; // [rsp+20h] [rbp-418h] BYREF

v1 = 0LL;
v2 = *(_DWORD *)a1;
func0(*(_DWORD *)a1);
buf = 0LL;
memset(v13, 0, sizeof(v13));
do
{
v3 = read(v2, &buf, 0x400uLL);
if ( v3 <= 0 )
break;
v10 = cr_empty();
v11 = v4;
v5 = str_newn(&buf, (unsigned int)v3);
v7 = func1(v5, v6, v1, (__int64)&v10);
v8 = v11;
if ( v11 )
{
*(_DWORD *)(v11 + 16) = v2;
v1 = (__int64 *)v8;
}
func6(v2, v10);
memset(&buf, 0, v3);
}
while ( !v7 );
if ( v1 )
func2((__int64)v1);
log_sys(6LL, "Client exited.");
close(v2);
return 0LL;
}

original_code:
static void*
handle_conn(void *arg) {
    int client_sock = *((int*)arg);
    send_welcome(client_sock);
    char buf[1024] = {0};
    int len = 0;
    int quit = 0;
    user_t *user = ((void *)0);
    while(!quit && (len = read(client_sock, buf, sizeof(buf))) > 0) {
        cr_t res = cr_empty();
        string in = str_newn(buf, len);
        quit = handle_message(in, user, &res);
        if (res.user != ((void *)0)) {
            user = res.user;
            user->sock = client_sock;
        }
        send_response(client_sock, res);
        memset(buf, 0, len);
    }
    if (user != ((void *)0)) {
        user_quit(user);
    }
    log_sys(6, "Client exited.");
    close(client_sock);
    return ((void *)0);
}

canonical_original_code:
static void *handle_conn(void *arg) {
    int client_sock = *((int*)arg);
    send_welcome(client_sock);
    char buf[1024] = {0};
    int len = 0;
    int quit = 0;
    struct user_t *user = ((void *)0);
    while(!quit && (len = read(client_sock, buf, sizeof(buf))) > 0) {
        struct __command_result res = cr_empty();
        struct string in = str_newn(buf, len);
        quit = handle_message(in, user, &res);
        if (res.user != ((void *)0)) {
            user = res.user;
            user->sock = client_sock;
        }
        send_response(client_sock, res);
        memset(buf, 0, len);
    }
    if (user != ((void *)0)) {
        user_quit(user);
    }
    log_sys(6, "Client exited.");
    close(client_sock);
    return ((void *)0);
}

variable_types:
arg
void *

variable_types:
buf
char [1024]

variable_types: len int

return_type: void *

user_defined_types: struct __command_result { int code; struct user_t *user; }

user_defined_types: struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; }


decompiled_code:
__int64 __fastcall start_server(unsigned __int16 a1, int a2)
{
int v2; // eax
int v3; // ebx
socklen_t salen; // [rsp+0h] [rbp-458h] BYREF
int arg; // [rsp+4h] [rbp-454h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-450h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-448h] BYREF
char serv[32]; // [rsp+20h] [rbp-438h] BYREF
char host[1048]; // [rsp+40h] [rbp-418h] BYREF

v2 = open_serv_sock(a1, a2);
if ( v2 >= 0 )
{
salen = 16;
v3 = v2;
while ( 1 )
{
arg = accept(v3, &addr, &salen);
getnameinfo(&addr, salen, host, 0x401u, serv, 0x20u, 3);
if ( arg >= 0 )
{
log_sys(6LL, "Connection from %s:%s", host, serv);
pthread_create(&newthread, 0LL, handle_conn, &arg);
}
else
{
log_sys(3LL, "Could not accept connection from %s %s", host, serv);
}
}
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned __int16 a1, int a2)
{
int v2; // eax
int v3; // ebx
socklen_t salen; // [rsp+0h] [rbp-458h] BYREF
int arg; // [rsp+4h] [rbp-454h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-450h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-448h] BYREF
char serv[32]; // [rsp+20h] [rbp-438h] BYREF
char host[1048]; // [rsp+40h] [rbp-418h] BYREF

v2 = func5(a1, a2);
if ( v2 >= 0 )
{
salen = 16;
v3 = v2;
while ( 1 )
{
arg = accept(v3, &addr, &salen);
getnameinfo(&addr, salen, host, 0x401u, serv, 0x20u, 3);
if ( arg >= 0 )
{
log_sys(6LL, "Connection from %s:%s", host, serv);
pthread_create(&newthread, 0LL, handle_conn, &arg);
}
else
{
log_sys(3LL, "Could not accept connection from %s %s", host, serv);
}
}
}
return 1LL;
}

original_code:
int start_server(const int port, const int max_conn)
{
    int serv_sock = open_serv_sock(port, max_conn);
    if (serv_sock < 0) {
        return 1;
    }
    struct sockaddr_in remote_addr;
    socklen_t remote_addr_len = sizeof(remote_addr);
    while(1) {
        char ip[1025];
        char srv[32];
        int client_sock = accept(serv_sock, (struct sockaddr*)&remote_addr, &remote_addr_len);
        getnameinfo((struct sockaddr*)&remote_addr, remote_addr_len, ip, sizeof(ip), srv, sizeof(srv), 1 | 2);
        if (client_sock < 0) {
            log_sys(3, "Could not accept connection from %s %s", ip, srv);
            continue;
        }
        log_sys(6, "Connection from %s:%s", ip, srv);
        pthread_t tid;
        pthread_create(&tid, ((void *)0), handle_conn, &client_sock);
    }
    close(serv_sock);
    return 0;
}

canonical_original_code:
int start_server(int port, int max_conn)
{
    int serv_sock = open_serv_sock(port, max_conn);
    if (serv_sock < 0) {
        return 1;
    }
    struct sockaddr_in remote_addr;
    unsigned int remote_addr_len = sizeof(remote_addr);
    while(1) {
        char ip[1025];
        char srv[32];
        int client_sock = accept(serv_sock, (struct sockaddr*)&remote_addr, &remote_addr_len);
        getnameinfo((struct sockaddr *)&remote_addr, remote_addr_len, ip, sizeof(ip), srv, sizeof(srv), 1 | 2);
        if (client_sock < 0) {
            log_sys(3, "Could not accept connection from %s %s", ip, srv);
            continue;
        }
        log_sys(6, "Connection from %s:%s", ip, srv);
        unsigned long tid;
        pthread_create(&tid, ((void *)0), handle_conn, &client_sock);
    }
    close(serv_sock);
    return 0;
}

variable_types:
ip
char [1025]

variable_types:
srv
char [32]

variable_types:
tid
unsigned long


decompiled_code:
_QWORD *user_new()
{
_QWORD *v0; // r12
__int64 v1; // rax
_QWORD *result; // rax

v0 = malloc(0x20uLL);
v1 = vec_new();
*v0 = 0LL;
v0[3] = v1;
result = v0;
v0[1] = 0LL;
*((_DWORD *)v0 + 4) = -1;
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12
__int64 v1; // rax
_QWORD *result; // rax

v0 = malloc(0x20uLL);
v1 = vec_new();
*v0 = 0LL;
v0[3] = v1;
result = v0;
v0[1] = 0LL;
*((_DWORD *)v0 + 4) = -1;
return result;
}

original_code:
user_t*
user_new() {
    user_t* user = (user_t*)malloc(sizeof(user_t));
    user->rooms = vec_new();
    user->username = ((void *)0);
    user->password = ((void *)0);
    user->sock = -1;
    return user;
}

canonical_original_code:
struct user_t *user_new() {
    struct user_t *user = (user_t*)malloc(sizeof(user_t));
    user->rooms = vec_new();
    user->username = ((void *)0);
    user->password = ((void *)0);
    user->sock = -1;
    return user;
}

return_type: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *


decompiled_code:
void __fastcall user_free(void *ptr)
{
vec_free(*((_QWORD *)ptr + 3));
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
vec_free(*((_QWORD *)ptr + 3));
free(*(void **)ptr);
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void
user_free(user_t *user) {
    vec_free(user->rooms);
    free(user->username);
    free(user->password);
    free(user);
}

canonical_original_code:
void user_free(struct user_t *user) {
    vec_free(user->rooms);
    free(user->username);
    free(user->password);
    free(user);
}


decompiled_code:
__int64 __fastcall user_message(const char **a1, __int64 a2, __int64 a3, const char *a4)
{
char *v5; // rbp
int v6; // eax
unsigned int v7; // ebx

cr_empty();
v5 = (char *)calloc(1uLL, 0x800uLL);
v6 = snprintf(v5, 0x800uLL, "<%s> %s\n", *a1, a4);
if ( write(*(_DWORD *)(a2 + 16), v5, v6) < 0 )
v7 = cr_create(105LL);
else
v7 = cr_ok();
free(v5);
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char **a1, __int64 a2, __int64 a3, const char *a4)
{
char *v5; // rbp
int v6; // eax
unsigned int v7; // ebx

cr_empty();
v5 = (char *)calloc(1uLL, 0x800uLL);
v6 = snprintf(v5, 0x800uLL, "<%s> %s\n", *a1, a4);
if ( write(*(_DWORD *)(a2 + 16), v5, v6) < 0 )
v7 = cr_create(105LL);
else
v7 = cr_ok();
free(v5);
return v7;
}

original_code:
cr_t
user_message(user_t* const sender, user_t* const receiver, string message) {
    cr_t res = cr_empty();
    char *msg = (char*)calloc(sizeof(char), sizeof(char) * 2048);
    int len = snprintf(msg, 2048, "<%s> %s\n", sender->username, message.val);
    if (write(receiver->sock, msg, len) < 0) {
        res = cr_create(105);
    } else {
        res = cr_ok();
    }
    free(msg);
    return res;
}

canonical_original_code:
struct __command_result user_message(struct user_t *sender, struct user_t *receiver, struct string message) {
    struct __command_result res = cr_empty();
    char *msg = (char*)calloc(sizeof(char), sizeof(char) * 2048);
    int len = snprintf(msg, 2048, "<%s> %s\n", sender->username, message.val);
    if (write(receiver->sock, msg, len) < 0) {
        res = cr_create(105);
    } else {
        res = cr_ok();
    }
    free(msg);
    return res;
}

variable_types:
sender
struct user_t *

variable_types:
receiver
struct user_t *

variable_types:
message
struct string { int len; char *val; }

variable_types:
msg
char *

return_type: struct __command_result { int code; struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *user; }


decompiled_code:
_BOOL8 __fastcall user_eq(const char *a1, const char **a2)
{
return strcmp(a1, *a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1, const char **a2)
{
return strcmp(a1, *a2) == 0;
}

original_code:
int
user_eq(any_t name, any_t user) {
    char *username = (char*)name;
    user_t *u = (user_t*)user;
    return strcmp(username, u->username) == 0;
}

canonical_original_code:
int user_eq(void *name, void *user) {
    char *username = (char*)name;
    struct user_t *u = (user_t*)user;
    return strcmp(username, u->username) == 0;
}

variable_types:
name
void *

variable_types:
user
void *

variable_types:
username
char *


broadcast

decompiled_code:
__int64 __fastcall broadcast(__int64 a1, const char *a2)
{
int v2; // er13
int i; // ebx
size_t v4; // rax
unsigned int v5; // eax
__int64 v6; // rdx
__int64 v7; // r14
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 v10; // rdx
__int64 v11; // r12
unsigned int v12; // er15
__int64 result; // rax
__int64 v14[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = 0;
for ( i = 0; (int)vec_size(*(_QWORD *)(a1 + 16)) > i; ++i )
{
if ( !(unsigned int)vec_get(*(_QWORD *)(a1 + 16), (unsigned int)i, v14) )
{
v4 = strlen(a2);
if ( write(*(_DWORD *)(v14[0] + 16), a2, v4) < 0 )
{
++v2;
log_sys(3LL, "Could not send message to user.");
}
}
}
v5 = str_new(a2);
v7 = v6;
v8 = v5;
v9 = str_trim(v5, v6);
v11 = v10;
v12 = v9;
db_log_msg(a1, v10);
str_destroy(v8, v7);
str_destroy(v12, v11);
if ( v2 )
result = cr_create(105LL);
else
result = cr_ok();
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2)
{
int v2; // er13
int i; // ebx
size_t v4; // rax
unsigned int v5; // eax
__int64 v6; // rdx
__int64 v7; // r14
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 v10; // rdx
__int64 v11; // r12
unsigned int v12; // er15
__int64 result; // rax
__int64 v14[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = 0;
for ( i = 0; (int)vec_size(*(_QWORD *)(a1 + 16)) > i; ++i )
{
if ( !(unsigned int)vec_get(*(_QWORD *)(a1 + 16), (unsigned int)i, v14) )
{
v4 = strlen(a2);
if ( write(*(_DWORD *)(v14[0] + 16), a2, v4) < 0 )
{
++v2;
log_sys(3LL, "Could not send message to user.");
}
}
}
v5 = str_new(a2);
v7 = v6;
v8 = v5;
v9 = str_trim(v5, v6);
v11 = v10;
v12 = v9;
db_log_msg(a1, v10);
str_destroy(v8, v7);
str_destroy(v12, v11);
if ( v2 )
result = cr_create(105LL);
else
result = cr_ok();
return result;
}

original_code:
static cr_t
broadcast(room_t * const room, char* message) {
    user_t *u;
    int err = 0;
    for (int i = 0; i < vec_size(room->users); ++i) {
        if (vec_get(room->users, i, (any_t*)&u) == 0) {
            if (write(u->sock, message, strlen(message)) < 0) {
                log_sys(3, "Could not send message to user.");
                ++err;
            }
        }
    }
    string msg = str_new(message);
    string trim = str_trim(msg);
    db_log_msg(room, trim.val);
    str_destroy(msg);
    str_destroy(trim);
    if (err == 0) {
        return cr_ok();
    }
    return cr_create(105);
}

canonical_original_code:
static struct __command_result broadcast(struct __room *room, char *message) {
    struct user_t *u;
    int err = 0;
    for (int i = 0; i < vec_size(room->users); ++i) {
        if (vec_get(room->users, i, (void **)&u) == 0) {
            if (write(u->sock, message, strlen(message)) < 0) {
                log_sys(3, "Could not send message to user.");
                ++err;
            }
        }
    }
    struct string msg = str_new(message);
    struct string trim = str_trim(msg);
    db_log_msg(room, trim.val);
    str_destroy(msg);
    str_destroy(trim);
    if (err == 0) {
        return cr_ok();
    }
    return cr_create(105);
}

variable_types: struct __room { char *name; struct vec_t *admins; struct vec_t *users; struct vec_t *muted; } *

variable_types:
message
char *

variable_types: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *

variable_types: err int

variable_types: i int

variable_types:
msg
struct string { int len; char *val; }

variable_types:
trim
struct string { int len; char *val; }


decompiled_code:
__int64 __fastcall remove_user(_QWORD *a1, __int64 a2, const char *a3)
{
unsigned int v3; // er13

v3 = broadcast((__int64)a1, a3);
vec_remove(a1[2], a2);
vec_remove(a1[1], a2);
vec_remove(a1[3], a2);
vec_remove(*(_QWORD *)(a2 + 24), a1);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, __int64 a2, const char *a3)
{
unsigned int v3; // er13

v3 = func0((__int64)a1, a3);
vec_remove(a1[2], a2);
vec_remove(a1[1], a2);
vec_remove(a1[3], a2);
vec_remove(*(_QWORD *)(a2 + 24), a1);
return v3;
}

original_code:
static cr_t
remove_user(room_t* const room, user_t* const user, char* msg) {
    cr_t res = broadcast(room, msg);
    vec_remove(room->users, user);
    vec_remove(room->admins, user);
    vec_remove(room->muted, user);
    vec_remove(user->rooms, room);
    return res;
}

canonical_original_code:
static struct __command_result remove_user(struct __room *room, struct user_t *user, char *msg) {
    struct __command_result res = broadcast(room, msg);
    vec_remove(room->users, user);
    vec_remove(room->admins, user);
    vec_remove(room->muted, user);
    vec_remove(user->rooms, room);
    return res;
}

variable_types:
user
struct user_t *

variable_types: struct __command_result { int code; struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *user; }


decompiled_code:
void __fastcall room_free(void *ptr)
{
free(*(void **)ptr);
vec_free(*((_QWORD *)ptr + 2));
vec_free(*((_QWORD *)ptr + 1));
vec_free(*((_QWORD *)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
free(*(void **)ptr);
vec_free(*((_QWORD *)ptr + 2));
vec_free(*((_QWORD *)ptr + 1));
vec_free(*((_QWORD *)ptr + 3));
free(ptr);
}

original_code:
void
room_free(room_t *room) {
    free(room->name);
    vec_free(room->users);
    vec_free(room->admins);
    vec_free(room->muted);
    free(room);
}

canonical_original_code:
void room_free(struct __room *room) {
    free(room->name);
    vec_free(room->users);
    vec_free(room->admins);
    vec_free(room->muted);
    free(room);
}

variable_types: struct __room { char *name; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *admins; struct vec_t *users; struct vec_t *muted; } *


decompiled_code:
__int64 __fastcall room_add_user(__int64 a1, __int64 a2, int a3)
{
const char *v5; // rcx
const char *v7; // rsi
const char *v8; // rdx
__int128 v9; // [rsp+0h] [rbp-118h] BYREF
char v10[232]; // [rsp+10h] [rbp-108h] BYREF
int v11; // [rsp+F8h] [rbp-20h]
__int16 v12; // [rsp+FCh] [rbp-1Ch]
char v13; // [rsp+FEh] [rbp-1Ah]

vec_add(*(_QWORD *)(a1 + 16));
if ( a3 )
{
vec_add(*(_QWORD *)(a1 + 8));
vec_add(*(_QWORD *)(a2 + 24));
v7 = *(const char **)a1;
memset(v10, 0, sizeof(v10));
v9 = 0LL;
v12 = 0;
v8 = *(const char **)a2;
v11 = 0;
v13 = 0;
sys_msg_isra_0((char *)&v9, v7, v8, "joined");
broadcast(a1, (const char *)&v9);
v5 = "is an admin";
}
else
{
vec_add(*(_QWORD *)(a2 + 24));
v9 = 0LL;
memset(v10, 0, sizeof(v10));
v5 = "joined";
v11 = 0;
v12 = 0;
v13 = 0;
}
sys_msg_isra_0((char *)&v9, *(const char **)a1, *(const char **)a2, v5);
return broadcast(a1, (const char *)&v9);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, int a3)
{
const char *v5; // rcx
const char *v7; // rsi
const char *v8; // rdx
__int128 v9; // [rsp+0h] [rbp-118h] BYREF
char v10[232]; // [rsp+10h] [rbp-108h] BYREF
int v11; // [rsp+F8h] [rbp-20h]
__int16 v12; // [rsp+FCh] [rbp-1Ch]
char v13; // [rsp+FEh] [rbp-1Ah]

vec_add(*(_QWORD *)(a1 + 16));
if ( a3 )
{
vec_add(*(_QWORD *)(a1 + 8));
vec_add(*(_QWORD *)(a2 + 24));
v7 = *(const char **)a1;
memset(v10, 0, sizeof(v10));
v9 = 0LL;
v12 = 0;
v8 = *(const char **)a2;
v11 = 0;
v13 = 0;
func2((char *)&v9, v7, v8, "joined");
func0(a1, (const char *)&v9);
v5 = "is an admin";
}
else
{
vec_add(*(_QWORD *)(a2 + 24));
v9 = 0LL;
memset(v10, 0, sizeof(v10));
v5 = "joined";
v11 = 0;
v12 = 0;
v13 = 0;
}
func2((char *)&v9, *(const char **)a1, *(const char **)a2, v5);
return func0(a1, (const char *)&v9);
}

original_code:
cr_t
room_add_user(room_t *room, user_t * const user, int admin) {
    vec_add(room->users, user);
    if(admin) {
        vec_add(room->admins, user);
    }
    vec_add(user->rooms, room);
    char buf[255] = {0};
    sys_msg(buf, room, user, "joined");
    cr_t res = broadcast(room, buf);
    if (admin) {
        sys_msg(buf, room, user, "is an admin");
        res = broadcast(room, buf);
    }
    return res;
}

canonical_original_code:
struct __command_result room_add_user(struct __room *room, struct user_t *user, int admin) {
    vec_add(room->users, user);
    if(admin) {
        vec_add(room->admins, user);
    }
    vec_add(user->rooms, room);
    char buf[255] = {0};
    sys_msg(buf, room, user, "joined");
    struct __command_result res = broadcast(room, buf);
    if (admin) {
        sys_msg(buf, room, user, "is an admin");
        res = broadcast(room, buf);
    }
    return res;
}

variable_types: admin int

variable_types:
buf
char [255]


decompiled_code:
__int64 __fastcall room_remove_user(const char **a1, const char **a2)
{
const char *v3; // rdx
const char *v4; // rsi
__int128 v6; // [rsp+0h] [rbp-118h] BYREF
char v7[232]; // [rsp+10h] [rbp-108h] BYREF
int v8; // [rsp+F8h] [rbp-20h]
__int16 v9; // [rsp+FCh] [rbp-1Ch]
char v10; // [rsp+FEh] [rbp-1Ah]

v3 = *a2;
v4 = *a1;
v6 = 0LL;
memset(v7, 0, sizeof(v7));
v9 = 0;
v8 = 0;
v10 = 0;
sys_msg_isra_0((char *)&v6, v4, v3, "is leaving..");
return remove_user(a1, (__int64)a2, (const char *)&v6);
}

canonical_decompiled_code:
__int64 __fastcall func7(const char **a1, const char **a2)
{
const char *v3; // rdx
const char *v4; // rsi
__int128 v6; // [rsp+0h] [rbp-118h] BYREF
char v7[232]; // [rsp+10h] [rbp-108h] BYREF
int v8; // [rsp+F8h] [rbp-20h]
__int16 v9; // [rsp+FCh] [rbp-1Ch]
char v10; // [rsp+FEh] [rbp-1Ah]

v3 = *a2;
v4 = *a1;
v6 = 0LL;
memset(v7, 0, sizeof(v7));
v9 = 0;
v8 = 0;
v10 = 0;
func2((char *)&v6, v4, v3, "is leaving..");
return func1(a1, (__int64)a2, (const char *)&v6);
}

original_code:
cr_t
room_remove_user(room_t *room, user_t * const user) {
    char buf[255] = {0};
    sys_msg(buf, room, user, "is leaving..");
    return remove_user(room, user, buf);
}

canonical_original_code:
struct __command_result room_remove_user(struct __room *room, struct user_t *user) {
    char buf[255] = {0};
    sys_msg(buf, room, user, "is leaving..");
    return remove_user(room, user, buf);
}


decompiled_code:
__int64 __fastcall room_send_msg(_QWORD *a1, _QWORD *a2, __int64 a3, __int64 a4)
{
int v6; // eax
__int64 v7; // rdi
const char *v8; // r14
char *v9; // r13
unsigned int v10; // ebx

v6 = vec_contains(a1[3]);
v7 = 103LL;
if ( v6 )
return cr_create(v7);
if ( !(unsigned int)vec_contains(a1[2]) )
{
v7 = 102LL;
return cr_create(v7);
}
v8 = "#%s <@%s> %s\n";
if ( !(unsigned int)is_admin_isra_0(a1[1]) )
v8 = "#%s <%s> %s\n";
v9 = (char *)calloc(1uLL, 0x800uLL);
snprintf(v9, 0x800uLL, v8, *a1, *a2, a4);
v10 = broadcast((__int64)a1, v9);
free(v9);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1, _QWORD *a2, __int64 a3, __int64 a4)
{
int v6; // eax
__int64 v7; // rdi
const char *v8; // r14
char *v9; // r13
unsigned int v10; // ebx

v6 = vec_contains(a1[3]);
v7 = 103LL;
if ( v6 )
return cr_create(v7);
if ( !(unsigned int)vec_contains(a1[2]) )
{
v7 = 102LL;
return cr_create(v7);
}
v8 = "#%s <@%s> %s\n";
if ( !(unsigned int)func3(a1[1]) )
v8 = "#%s <%s> %s\n";
v9 = (char *)calloc(1uLL, 0x800uLL);
snprintf(v9, 0x800uLL, v8, *a1, *a2, a4);
v10 = func0((__int64)a1, v9);
free(v9);
return v10;
}

original_code:
cr_t
room_send_msg(room_t * const room, user_t * const user, string msg) {
    if (vec_contains(room->muted, user)) {
        return cr_create(103);
    }
    if (!vec_contains(room->users, user)) {
        return cr_create(102);
    }
    char *fmt = is_admin(room, user) ? "#%s <@%s> %s\n" : "#%s <%s> %s\n";
    char *message = (char*)calloc(sizeof(char), sizeof(char) * 2048 );
    snprintf(message, 2048, fmt, room->name, user->username, msg.val);
    cr_t res = broadcast(room, message);
    free(message);
    return res;
}

canonical_original_code:
struct __command_result room_send_msg(struct __room *room, struct user_t *user, struct string msg) {
    if (vec_contains(room->muted, user)) {
        return cr_create(103);
    }
    if (!vec_contains(room->users, user)) {
        return cr_create(102);
    }
    char *fmt = is_admin(room, user) ? "#%s <@%s> %s\n" : "#%s <%s> %s\n";
    char *message = (char*)calloc(sizeof(char), sizeof(char) * 2048 );
    snprintf(message, 2048, fmt, room->name, user->username, msg.val);
    struct __command_result res = broadcast(room, message);
    free(message);
    return res;
}

variable_types:
fmt
char *


decompiled_code:
__int64 __fastcall room_admin(const char **a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char *v8; // rsi
const char **v9; // [rsp+8h] [rbp-130h] BYREF
__int64 v10[2]; // [rsp+10h] [rbp-128h] BYREF
__int128 v11; // [rsp+20h] [rbp-118h] BYREF
char v12[232]; // [rsp+30h] [rbp-108h] BYREF
int v13; // [rsp+118h] [rbp-20h]
__int16 v14; // [rsp+11Ch] [rbp-1Ch]
char v15; // [rsp+11Eh] [rbp-1Ah]

v9 = 0LL;
v5 = cr_empty();
v10[1] = v6;
LODWORD(v10[0]) = v5;
if ( !(unsigned int)admin_prep_isra_0((__int64)a1, a2, a4, v10, (__int64 *)&v9) )
return v10[0];
vec_add(a1[1]);
v11 = 0LL;
v8 = *a1;
memset(v12, 0, sizeof(v12));
v14 = 0;
v13 = 0;
v15 = 0;
sys_msg_isra_0((char *)&v11, v8, *v9, "was granted admin rights");
return broadcast((__int64)a1, (const char *)&v11);
}

original_code:
cr_t
room_admin(room_t* const room, user_t* const admin, string username) {
    user_t *user = ((void *)0);
    cr_t res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_add(room->admins, user);
    char buf[255] = {0};
    sys_msg(buf, room, user, "was granted admin rights");
    return broadcast(room, buf);
}

canonical_original_code:
struct __command_result room_admin(struct __room *room, struct user_t *admin, struct string username) {
    struct user_t *user = ((void *)0);
    struct __command_result res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_add(room->admins, user);
    char buf[255] = {0};
    sys_msg(buf, room, user, "was granted admin rights");
    return broadcast(room, buf);
}

variable_types:
admin
struct user_t *

variable_types:
username
struct string { int len; char *val; }


decompiled_code:
__int64 __fastcall room_mute_user(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char **v8; // [rsp+8h] [rbp-130h] BYREF
__int64 v9[2]; // [rsp+10h] [rbp-128h] BYREF
char v10[280]; // [rsp+20h] [rbp-118h] BYREF

v8 = 0LL;
v5 = cr_empty();
v9[1] = v6;
LODWORD(v9[0]) = v5;
if ( !(unsigned int)admin_prep_isra_0(a1, a2, a4, v9, (__int64 *)&v8) )
return v9[0];
vec_add(*(_QWORD *)(a1 + 24));
sys_msg_isra_0(v10, *(const char **)a1, *v8, "was muted");
return broadcast(a1, v10);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char **v8; // [rsp+8h] [rbp-130h] BYREF
__int64 v9[2]; // [rsp+10h] [rbp-128h] BYREF
char v10[280]; // [rsp+20h] [rbp-118h] BYREF

v8 = 0LL;
v5 = cr_empty();
v9[1] = v6;
LODWORD(v9[0]) = v5;
if ( !(unsigned int)func4(a1, a2, a4, v9, (__int64 *)&v8) )
return v9[0];
vec_add(*(_QWORD *)(a1 + 24));
func2(v10, *(const char **)a1, *v8, "was muted");
return func0(a1, v10);
}

original_code:
cr_t
room_mute_user(room_t* const room, user_t* const admin, string username) {
    user_t *user = ((void *)0);
    cr_t res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_add(room->muted, user);
    char buf[255];
    sys_msg(buf, room, user, "was muted");
    return broadcast(room, buf);
}

canonical_original_code:
struct __command_result room_mute_user(struct __room *room, struct user_t *admin, struct string username) {
    struct user_t *user = ((void *)0);
    struct __command_result res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_add(room->muted, user);
    char buf[255];
    sys_msg(buf, room, user, "was muted");
    return broadcast(room, buf);
}


decompiled_code:
__int64 __fastcall room_voice_user(const char **a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char *v8; // rsi
const char **v9; // [rsp+8h] [rbp-130h] BYREF
__int64 v10[2]; // [rsp+10h] [rbp-128h] BYREF
__int128 v11; // [rsp+20h] [rbp-118h] BYREF
char v12[232]; // [rsp+30h] [rbp-108h] BYREF
int v13; // [rsp+118h] [rbp-20h]
__int16 v14; // [rsp+11Ch] [rbp-1Ch]
char v15; // [rsp+11Eh] [rbp-1Ah]

v9 = 0LL;
v5 = cr_empty();
v10[1] = v6;
LODWORD(v10[0]) = v5;
if ( !(unsigned int)admin_prep_isra_0((__int64)a1, a2, a4, v10, (__int64 *)&v9) )
return v10[0];
vec_remove(a1[3], v9);
v11 = 0LL;
v8 = *a1;
memset(v12, 0, sizeof(v12));
v14 = 0;
v13 = 0;
v15 = 0;
sys_msg_isra_0((char *)&v11, v8, *v9, "can spean again");
return broadcast((__int64)a1, (const char *)&v11);
}

original_code:
cr_t
room_voice_user(room_t* const room, user_t* const admin, string username) {
    user_t *user = ((void *)0);
    cr_t res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_remove(room->muted, user);
    char buf[255] = {0};
    sys_msg(buf, room, user, "can spean again");
    return broadcast(room, buf);
}

canonical_original_code:
struct __command_result room_voice_user(struct __room *room, struct user_t *admin, struct string username) {
    struct user_t *user = ((void *)0);
    struct __command_result res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    vec_remove(room->muted, user);
    char buf[255] = {0};
    sys_msg(buf, room, user, "can spean again");
    return broadcast(room, buf);
}


decompiled_code:
__int64 __fastcall room_kick_user(const char **a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char **v8; // [rsp+8h] [rbp-130h] BYREF
__int64 v9[2]; // [rsp+10h] [rbp-128h] BYREF
char v10[280]; // [rsp+20h] [rbp-118h] BYREF

v8 = 0LL;
v5 = cr_empty();
v9[1] = v6;
LODWORD(v9[0]) = v5;
if ( !(unsigned int)admin_prep_isra_0((__int64)a1, a2, a4, v9, (__int64 *)&v8) )
return v9[0];
sys_msg_isra_0(v10, *a1, *v8, "was kicked out");
return remove_user(a1, (__int64)v8, v10);
}

canonical_decompiled_code:
__int64 __fastcall func12(const char **a1, __int64 a2, __int64 a3, __int64 a4)
{
int v5; // eax
__int64 v6; // rdx
const char **v8; // [rsp+8h] [rbp-130h] BYREF
__int64 v9[2]; // [rsp+10h] [rbp-128h] BYREF
char v10[280]; // [rsp+20h] [rbp-118h] BYREF

v8 = 0LL;
v5 = cr_empty();
v9[1] = v6;
LODWORD(v9[0]) = v5;
if ( !(unsigned int)func4((__int64)a1, a2, a4, v9, (__int64 *)&v8) )
return v9[0];
func2(v10, *a1, *v8, "was kicked out");
return func1(a1, (__int64)v8, v10);
}

original_code:
cr_t
room_kick_user(room_t* const room, user_t* const admin, string username) {
    user_t *user = ((void *)0);
    cr_t res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    char buf[255];
    sys_msg(buf, room, user, "was kicked out");
    return remove_user(room, user, buf);
}

canonical_original_code:
struct __command_result room_kick_user(struct __room *room, struct user_t *admin, struct string username) {
    struct user_t *user = ((void *)0);
    struct __command_result res = cr_empty();
    if (!admin_prep(room, admin, username, &res, &user)) {
        return res;
    }
    char buf[255];
    sys_msg(buf, room, user, "was kicked out");
    return remove_user(room, user, buf);
}


decompiled_code:
_DWORD *vec_new()
{
return vec_new_cap(10);
}

canonical_decompiled_code:
_DWORD *func0()
{
return vec_new_cap(10);
}

canonical_original_code:
struct vec_t *vec_new() {
    return vec_new_cap(10);
}

return_type: struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *


decompiled_code:
void __fastcall vec_free(void *ptr)
{
pthread_rwlock_destroy(*((pthread_rwlock_t **)ptr + 2));
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
pthread_rwlock_destroy(*((pthread_rwlock_t **)ptr + 2));
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void vec_free(vec_t *vec) {
    pthread_rwlock_destroy(vec->lock);
    free(vec->lock);
    free(vec->data);
    free(vec);
}

canonical_original_code:
void vec_free(struct vec_t *vec) {
    pthread_rwlock_destroy(vec->lock);
    free(vec->lock);
    free(vec->data);
    free(vec);
}

variable_types: struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *


decompiled_code:
int __fastcall vec_add(int *a1, __int64 a2)
{
int v4; // eax
int v5; // esi
_QWORD *v6; // rdx
pthread_rwlock_t *v7; // rdi
int v9; // esi
void *v10; // rdi
_QWORD *v11; // rax

pthread_rwlock_wrlock(*((pthread_rwlock_t **)a1 + 2));
v4 = *a1;
v5 = a1[1];
if ( *a1 >= v5 )
{
v9 = 2 * v5;
v10 = (void *)*((_QWORD *)a1 + 1);
a1[1] = v9;
v11 = realloc(v10, 8LL * v9);
*((_QWORD *)a1 + 1) = v11;
v6 = v11;
v4 = *a1;
}
else
{
v6 = (_QWORD *)*((_QWORD *)a1 + 1);
}
v6[v4] = a2;
v7 = (pthread_rwlock_t *)*((_QWORD *)a1 + 2);
*a1 = v4 + 1;
return pthread_rwlock_unlock(v7);
}

canonical_decompiled_code:
int __fastcall func2(int *a1, __int64 a2)
{
int v4; // eax
int v5; // esi
_QWORD *v6; // rdx
pthread_rwlock_t *v7; // rdi
int v9; // esi
void *v10; // rdi
_QWORD *v11; // rax

pthread_rwlock_wrlock(*((pthread_rwlock_t **)a1 + 2));
v4 = *a1;
v5 = a1[1];
if ( *a1 >= v5 )
{
v9 = 2 * v5;
v10 = (void *)*((_QWORD *)a1 + 1);
a1[1] = v9;
v11 = realloc(v10, 8LL * v9);
*((_QWORD *)a1 + 1) = v11;
v6 = v11;
v4 = *a1;
}
else
{
v6 = (_QWORD *)*((_QWORD *)a1 + 1);
}
v6[v4] = a2;
v7 = (pthread_rwlock_t *)*((_QWORD *)a1 + 2);
*a1 = v4 + 1;
return pthread_rwlock_unlock(v7);
}

original_code:
void vec_add(vec_t *vec, any_t value) {
    pthread_rwlock_wrlock(vec->lock);
    if (vec->len >= vec-> cap) {
        vec->cap *= 2;
        vec->data = realloc(vec->data, sizeof(any_t) * vec->cap);
    }
    vec->data[vec->len] = value;
    ++(vec->len);
    pthread_rwlock_unlock(vec->lock);
}

canonical_original_code:
void vec_add(struct vec_t *vec, void *value) {
    pthread_rwlock_wrlock(vec->lock);
    if (vec->len >= vec-> cap) {
        vec->cap *= 2;
        vec->data = realloc(vec->data, sizeof(any_t) * vec->cap);
    }
    vec->data[vec->len] = value;
    ++(vec->len);
    pthread_rwlock_unlock(vec->lock);
}

variable_types:
value
void *


decompiled_code:
__int64 __fastcall vec_remove(int *a1, __int64 a2)
{
_QWORD *v3; // rax
int v4; // ebx
__int64 result; // rax
int v6; // edi
__int64 v7; // rax

pthread_rwlock_rdlock(*((pthread_rwlock_t **)a1 + 2));
if ( *a1 <= 0 )
{
v4 = 0;
}
else
{
v3 = (_QWORD *)*((_QWORD *)a1 + 1);
v4 = 0;
do
{
if ( *v3 == a2 )
break;
++v4;
++v3;
}
while ( v4 != *a1 );
}
pthread_rwlock_unlock(*((pthread_rwlock_t **)a1 + 2));
result = 0LL;
if ( *a1 > v4 )
{
pthread_rwlock_wrlock(*((pthread_rwlock_t **)a1 + 2));
v6 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * v4) = 0LL;
if ( v4 + 1 < v6 )
{
v7 = 8LL * v4 + 8;
do
{
*(_QWORD *)(*((_QWORD *)a1 + 1) + v7 - 8) = *(_QWORD *)(*((_QWORD *)a1 + 1) + v7);
v7 += 8LL;
}
while ( v7 != 8 * (v4 + (unsigned __int64)(unsigned int)(v6 - 2 - v4) + 2) );
}
*a1 = v6 - 1;
pthread_rwlock_unlock(*((pthread_rwlock_t **)a1 + 2));
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int *a1, __int64 a2)
{
_QWORD *v3; // rax
int v4; // ebx
__int64 result; // rax
int v6; // edi
__int64 v7; // rax

pthread_rwlock_rdlock(*((pthread_rwlock_t **)a1 + 2));
if ( *a1 <= 0 )
{
v4 = 0;
}
else
{
v3 = (_QWORD *)*((_QWORD *)a1 + 1);
v4 = 0;
do
{
if ( *v3 == a2 )
break;
++v4;
++v3;
}
while ( v4 != *a1 );
}
pthread_rwlock_unlock(*((pthread_rwlock_t **)a1 + 2));
result = 0LL;
if ( *a1 > v4 )
{
pthread_rwlock_wrlock(*((pthread_rwlock_t **)a1 + 2));
v6 = *a1;
*(_QWORD *)(*((_QWORD *)a1 + 1) + 8LL * v4) = 0LL;
if ( v4 + 1 < v6 )
{
v7 = 8LL * v4 + 8;
do
{
*(_QWORD *)(*((_QWORD *)a1 + 1) + v7 - 8) = *(_QWORD *)(*((_QWORD *)a1 + 1) + v7);
v7 += 8LL;
}
while ( v7 != 8 * (v4 + (unsigned __int64)(unsigned int)(v6 - 2 - v4) + 2) );
}
*a1 = v6 - 1;
pthread_rwlock_unlock(*((pthread_rwlock_t **)a1 + 2));
result = 1LL;
}
return result;
}

original_code:
int vec_remove(vec_t *vec, any_t value) {
    int i = 0;
    pthread_rwlock_rdlock(vec->lock);
    while (i < vec->len && vec->data[i] != value) ++i;
    pthread_rwlock_unlock(vec->lock);
    if (i >= vec->len) {
        return 0;
    }
    pthread_rwlock_wrlock(vec->lock);
    vec->data[i] = ((void *)0);
    for (int j = i + 1; j < vec->len; ++j) {
        vec->data[j-1] = vec->data[j];
    }
    --(vec->len);
    pthread_rwlock_unlock(vec->lock);
    return 1;
}

canonical_original_code:
int vec_remove(struct vec_t *vec, void *value) {
    int i = 0;
    pthread_rwlock_rdlock(vec->lock);
    while (i < vec->len && vec->data[i] != value) ++i;
    pthread_rwlock_unlock(vec->lock);
    if (i >= vec->len) {
        return 0;
    }
    pthread_rwlock_wrlock(vec->lock);
    vec->data[i] = ((void *)0);
    for (int j = i + 1; j < vec->len; ++j) {
        vec->data[j-1] = vec->data[j];
    }
    --(vec->len);
    pthread_rwlock_unlock(vec->lock);
    return 1;
}

variable_types: j int


decompiled_code:
__int64 __fastcall vec_get(__int64 a1, int a2, _QWORD *a3)
{
if ( *(_DWORD *)a1 <= a2 )
return 0xFFFFFFFFLL;
pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
*a3 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, _QWORD *a3)
{
if ( *(_DWORD *)a1 <= a2 )
return 0xFFFFFFFFLL;
pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
*a3 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return 0LL;
}

original_code:
int vec_get(vec_t *vec, int idx, any_t* res) {
    if (idx >= vec->len) {
        return -1;
    }
    pthread_rwlock_rdlock(vec->lock);
    *res = vec->data[idx];
    pthread_rwlock_unlock(vec->lock);
    return 0;
}

canonical_original_code:
int vec_get(struct vec_t *vec, int idx, void **res) {
    if (idx >= vec->len) {
        return -1;
    }
    pthread_rwlock_rdlock(vec->lock);
    *res = vec->data[idx];
    pthread_rwlock_unlock(vec->lock);
    return 0;
}

variable_types: idx int

variable_types:
res
void **


decompiled_code:
__int64 __fastcall vec_set(__int64 a1, int a2, __int64 a3)
{
__int64 v3; // r13
__int64 *v5; // rax

v3 = 0LL;
if ( *(_DWORD *)a1 > a2 )
{
pthread_rwlock_wrlock(*(pthread_rwlock_t **)(a1 + 16));
v5 = (__int64 *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
v3 = *v5;
*v5 = a3;
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, __int64 a3)
{
__int64 v3; // r13
__int64 *v5; // rax

v3 = 0LL;
if ( *(_DWORD *)a1 > a2 )
{
pthread_rwlock_wrlock(*(pthread_rwlock_t **)(a1 + 16));
v5 = (__int64 *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
v3 = *v5;
*v5 = a3;
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
}
return v3;
}

original_code:
any_t vec_set(vec_t *vec, int idx, any_t val) {
    if (idx >= vec->len) {
        return ((void *)0);
    }
    pthread_rwlock_wrlock(vec->lock);
    any_t old = vec->data[idx];
    vec->data[idx] = val;
    pthread_rwlock_unlock(vec->lock);
    return old;
}

canonical_original_code:
void *vec_set(struct vec_t *vec, int idx, void *val) {
    if (idx >= vec->len) {
        return ((void *)0);
    }
    pthread_rwlock_wrlock(vec->lock);
    void *old = vec->data[idx];
    vec->data[idx] = val;
    pthread_rwlock_unlock(vec->lock);
    return old;
}

variable_types:
val
void *

variable_types:
old
void *


decompiled_code:
__int64 __fastcall vec_size(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1)
{
return *a1;
}

original_code:
int vec_size(vec_t *vec) {
    return vec->len;
}

canonical_original_code:
int vec_size(struct vec_t *vec) {
    return vec->len;
}


decompiled_code:
_BOOL8 __fastcall vec_is_empty(_DWORD *a1)
{
return *a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(_DWORD *a1)
{
return *a1 == 0;
}

original_code:
int vec_is_empty(vec_t *vec) {
    return vec->len == 0;
}

canonical_original_code:
int vec_is_empty(struct vec_t *vec) {
    return vec->len == 0;
}


decompiled_code:
__int64 __fastcall vec_contains(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
unsigned int v4; // er12

pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
LABEL_6:
v4 = 0;
}
else
{
v2 = *(_QWORD **)(a1 + 8);
v3 = (__int64)&v2[(unsigned int)(*(_DWORD *)a1 - 1) + 1];
while ( *v2 != a2 )
{
if ( ++v2 == (_QWORD *)v3 )
goto LABEL_6;
}
v4 = 1;
}
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
unsigned int v4; // er12

pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
LABEL_6:
v4 = 0;
}
else
{
v2 = *(_QWORD **)(a1 + 8);
v3 = (__int64)&v2[(unsigned int)(*(_DWORD *)a1 - 1) + 1];
while ( *v2 != a2 )
{
if ( ++v2 == (_QWORD *)v3 )
goto LABEL_6;
}
v4 = 1;
}
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return v4;
}

original_code:
int vec_contains(vec_t* vec, any_t val) {
    int res = 0;
    pthread_rwlock_rdlock(vec->lock);
    for (int i = 0; i < vec->len; ++i) {
        if (val == vec->data[i]) {
            res = 1;
            break;
        }
    }
    pthread_rwlock_unlock(vec->lock);
    return res;
}

canonical_original_code:
int vec_contains(struct vec_t *vec, void *val) {
    int res = 0;
    pthread_rwlock_rdlock(vec->lock);
    for (int i = 0; i < vec->len; ++i) {
        if (val == vec->data[i]) {
            res = 1;
            break;
        }
    }
    pthread_rwlock_unlock(vec->lock);
    return res;
}

variable_types: res int

variable_types: i int


decompiled_code:
__int64 __fastcall vec_find(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
__int64 v4; // rbx
__int64 v5; // r14

pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
v5 = 0LL;
}
else
{
v4 = 0LL;
v5 = 0LL;
do
{
if ( a3(a2, *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4)) )
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4);
++v4;
}
while ( *(_DWORD *)a1 > (int)v4 );
}
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
__int64 v4; // rbx
__int64 v5; // r14

pthread_rwlock_rdlock(*(pthread_rwlock_t **)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
v5 = 0LL;
}
else
{
v4 = 0LL;
v5 = 0LL;
do
{
if ( a3(a2, *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4)) )
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4);
++v4;
}
while ( *(_DWORD *)a1 > (int)v4 );
}
pthread_rwlock_unlock(*(pthread_rwlock_t **)(a1 + 16));
return v5;
}

original_code:
any_t vec_find(vec_t *vec, any_t val, int (*predicate)(any_t, any_t)) {
    any_t res = ((void *)0);
    pthread_rwlock_rdlock(vec->lock);
    for (int i = 0; i < vec->len; ++i) {
        if (predicate(val, vec->data[i])) {
            res = vec->data[i];
        }
    }
    pthread_rwlock_unlock(vec->lock);
    return res;
}

canonical_original_code:
void *vec_find(struct vec_t *vec, void *val, int (*predicate)(void *, void *)) {
    void *res = ((void *)0);
    pthread_rwlock_rdlock(vec->lock);
    for (int i = 0; i < vec->len; ++i) {
        if (predicate(val, vec->data[i])) {
            res = vec->data[i];
        }
    }
    pthread_rwlock_unlock(vec->lock);
    return res;
}

variable_types:
predicate
int (*)(void *, void *)

variable_types:
res
void *


term

decompiled_code:
void __fastcall term(int a1)
{
if ( a1 == 2 || a1 == 15 )
{
log_fini();
db_fini();
exit(0);
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 == 2 || a1 == 15 )
{
log_fini();
db_fini();
exit(0);
}
}

original_code:
static void
term(int signum) {
    if (signum == 2 || signum == 15) {
        log_fini();
        db_fini();
        exit(0);
    }
}

variable_types: signum int


daemonize

decompiled_code:
int daemonize()
{
__pid_t v0; // eax

v0 = fork();
if ( v0 < 0 )
goto LABEL_7;
if ( v0 )
exit(0);
umask(0);
if ( setsid() < 0 || chdir("/") < 0 )
LABEL_7:
exit(1);
close(0);
close(1);
return close(2);
}

canonical_decompiled_code:
int func2()
{
__pid_t v0; // eax

v0 = fork();
if ( v0 < 0 )
goto LABEL_7;
if ( v0 )
exit(0);
umask(0);
if ( setsid() < 0 || chdir("/") < 0 )
LABEL_7:
exit(1);
close(0);
close(1);
return close(2);
}

original_code:
static void
daemonize() {
    pid_t pid, sid;
    pid = fork();
    if (pid < 0) {
        exit(1);
    }
    if (pid > 0) {
        exit(0);
    }
    umask(0);
    sid = setsid();
    if (sid < 0) {
        exit(1);
    }
    if ((chdir("/")) < 0) {
        exit(1);
    }
    close(0);
    close(1);
    close(2);
}

canonical_original_code:
static void daemonize() {
    int pid; int sid;
    pid = fork();
    if (pid < 0) {
        exit(1);
    }
    if (pid > 0) {
        exit(0);
    }
    umask(0);
    sid = setsid();
    if (sid < 0) {
        exit(1);
    }
    if ((chdir("/")) < 0) {
        exit(1);
    }
    close(0);
    close(1);
    close(2);
}

variable_types: pid int

variable_types: sid int


decompiled_code:
int register_term_handler()
{
struct sigaction *v0; // rbp

v0 = (struct sigaction *)malloc(0x98uLL);
memset(&v0->sa_mask, 0, 0x90uLL);
v0->sa_handler = term;
sigaction(2, v0, 0LL);
return sigaction(15, v0, 0LL);
}

canonical_decompiled_code:
int func3()
{
struct sigaction *v0; // rbp

v0 = (struct sigaction *)malloc(0x98uLL);
memset(&v0->sa_mask, 0, 0x90uLL);
v0->sa_handler = term;
sigaction(2, v0, 0LL);
return sigaction(15, v0, 0LL);
}

original_code:
static void
register_term_handler() {
    struct sigaction *act = (struct sigaction*)malloc(sizeof(struct sigaction));
    memset(act, 0, sizeof(struct sigaction));
    act->__sigaction_handler.sa_handler = term;
    sigaction(2, act, ((void *)0));
    sigaction(15, act, ((void *)0));
}

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

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

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
__int64 v4; // r13
unsigned int v5; // er12
unsigned int v6; // edi
int v7; // er12
unsigned int v9; // edi
int v10[2]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v11; // [rsp+8h] [rbp-30h]
__int64 v12; // [rsp+10h] [rbp-28h]

parse_opts((__int64)v10, argc, (__int64)argv);
v3 = v11;
v4 = v12;
v5 = v10[0];
if ( v11 )
{
daemonize();
register_term_handler();
v9 = time(0LL);
srand(v9);
mem_init();
db_init(v4);
log_init(v3);
}
else
{
register_term_handler();
v6 = time(0LL);
srand(v6);
mem_init();
db_init(v4);
log_init(0LL);
printf("Server started on %d port, waiting for connections...\n", v5);
}
v7 = start_server(v5, (unsigned int)v10[1]);
if ( v7 )
log_sys(2LL, "Could not start server");
log_fini();
db_fini();
return v7;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
__int64 v4; // r13
unsigned int v5; // er12
unsigned int v6; // edi
int v7; // er12
unsigned int v9; // edi
int v10[2]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v11; // [rsp+8h] [rbp-30h]
__int64 v12; // [rsp+10h] [rbp-28h]

func0((__int64)v10, argc, (__int64)argv);
v3 = v11;
v4 = v12;
v5 = v10[0];
if ( v11 )
{
func2();
func3();
v9 = time(0LL);
srand(v9);
mem_init();
db_init(v4);
log_init(v3);
}
else
{
func3();
v6 = time(0LL);
srand(v6);
mem_init();
db_init(v4);
log_init(0LL);
printf("Server started on %d port, waiting for connections...\n", v5);
}
v7 = start_server(v5, (unsigned int)v10[1]);
if ( v7 )
log_sys(2LL, "Could not start server");
log_fini();
db_fini();
return v7;
}

original_code:
int main(int argc, char *argv[])
{
    struct options opts = parse_opts(argc, argv);
    if (opts.is_daemon)
        daemonize();
    register_term_handler();
    srand(time(((void *)0)));
    mem_init();
    db_init(opts.db_uri);
    log_init(opts.is_daemon);
    if (!opts.is_daemon) {
        printf("Server started on %d port, waiting for connections...\n", opts.port);
    }
    int res = start_server(opts.port, opts.max_conn);
    if (res != 0) {
        log_sys(2, "Could not start server");
    }
    log_fini();
    db_fini();
    return res;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct options { int port; int max_conn; int is_daemon; char *db_uri; }


original_code:
static void
cmd_kick(struct room_req req) {
    string username = str_tok_rest(req.tok);
    *req.res = room_kick_user(req.room, req.user, username);
    str_destroy(username);
}

canonical_original_code:
static void cmd_kick(struct room_req req) {
    struct string username = str_tok_rest(req.tok);
    *req.res = room_kick_user(req.room, req.user, username);
    str_destroy(username);
}

variable_types: struct room_req { struct __command { int code; struct string { int len; char *val; } args; } cmd; struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *user; struct __room { char *name; struct vec_t *admins; struct vec_t *users; struct vec_t *muted; } *room; struct tok_t { char sep; struct string str; int start; int has_more; } tok; struct __command_result { int code; struct user_t *user; } *res; }

variable_types:
username
struct string { int len; char *val; }

user_defined_types: struct room_req { struct __command cmd; struct user_t *user; struct __room *room; struct tok_t tok; struct __command_result *res; }


original_code:
static void
cmd_voice(struct room_req req) {
    string username = str_tok_rest(req.tok);
    *req.res = room_voice_user(req.room, req.user, username);
    str_destroy(username);
}

canonical_original_code:
static void cmd_voice(struct room_req req) {
    struct string username = str_tok_rest(req.tok);
    *req.res = room_voice_user(req.room, req.user, username);
    str_destroy(username);
}

user_defined_types: struct tok_t { char sep; struct string str; int start; int has_more; }


original_code:
static void
cmd_mute(struct room_req req) {
    string username = str_tok_rest(req.tok);
    *req.res = room_mute_user(req.room, req.user, username);
    str_destroy(username);
}

canonical_original_code:
static void cmd_mute(struct room_req req) {
    struct string username = str_tok_rest(req.tok);
    *req.res = room_mute_user(req.room, req.user, username);
    str_destroy(username);
}


original_code:
static void
cmd_grant(struct room_req req) {
    string username = str_tok_rest(req.tok);
    *req.res = room_admin(req.room, req.user, username);
    str_destroy(username);
}

canonical_original_code:
static void cmd_grant(struct room_req req) {
    struct string username = str_tok_rest(req.tok);
    *req.res = room_admin(req.room, req.user, username);
    str_destroy(username);
}


decompiled_code:
__int64 __fastcall cmd_leave(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, __int64 a10, __int64 a11, int a12, int a13, int a14, int a15, __int64 a16)
{
int v16; // eax
__int64 v17; // rdi
__int64 v18; // rdx
__int64 result; // rax

v16 = room_remove_user(a11, a10);
v17 = *(_QWORD *)(a11 + 16);
*(_DWORD *)a16 = v16;
*(_QWORD *)(a16 + 8) = v18;
result = vec_is_empty(v17);
if ( (_DWORD)result )
result = mem_remove_room(a11);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, __int64 a10, __int64 a11, int a12, int a13, int a14, int a15, __int64 a16)
{
int v16; // eax
__int64 v17; // rdi
__int64 v18; // rdx
__int64 result; // rax

v16 = room_remove_user(a11, a10);
v17 = *(_QWORD *)(a11 + 16);
*(_DWORD *)a16 = v16;
*(_QWORD *)(a16 + 8) = v18;
result = vec_is_empty(v17);
if ( (_DWORD)result )
result = mem_remove_room(a11);
return result;
}

original_code:
static void
cmd_leave(struct room_req req) {
    *req.res = room_remove_user(req.room, req.user);
    if (vec_is_empty(req.room->users)) {
        mem_remove_room(req.room);
    }
}


original_code:
static void
cmd_talk(struct room_req req) {
    string message = str_tok_rest(req.tok);
    *req.res = room_send_msg(req.room, req.user, message);
    str_destroy(message);
}

canonical_original_code:
static void cmd_talk(struct room_req req) {
    struct string message = str_tok_rest(req.tok);
    *req.res = room_send_msg(req.room, req.user, message);
    str_destroy(message);
}

variable_types:
message
struct string { int len; char *val; }


login

decompiled_code:
__int64 __fastcall login(__int64 a1, __int64 a2, unsigned int a3, const char *a4, _DWORD *a5)
{
__int64 v8; // r12
__int64 v10; // rdi
__int64 v11; // rdi
__int64 v12; // r14

v8 = db_get_user();
if ( (unsigned int)str_is_nil(a3, a4) || (unsigned int)str_is_empty(a3, a4) )
{
if ( !v8 )
return user_new_with_name(a2);
*a5 = 108;
v10 = v8;
v8 = 0LL;
user_free(v10);
}
else if ( v8 )
{
if ( strcmp(*(const char **)(v8 + 8), a4) )
{
v11 = v8;
v8 = 0LL;
user_free(v11);
*a5 = 110;
}
}
else
{
v12 = user_new_with_name(a2);
*(_QWORD *)(v12 + 8) = strdup(a4);
if ( (unsigned int)db_store_user(v12) )
{
user_free(v12);
*a5 = 108;
}
else
{
v8 = v12;
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, unsigned int a3, const char *a4, _DWORD *a5)
{
__int64 v8; // r12
__int64 v10; // rdi
__int64 v11; // rdi
__int64 v12; // r14

v8 = db_get_user();
if ( (unsigned int)str_is_nil(a3, a4) || (unsigned int)str_is_empty(a3, a4) )
{
if ( !v8 )
return user_new_with_name(a2);
*a5 = 108;
v10 = v8;
v8 = 0LL;
user_free(v10);
}
else if ( v8 )
{
if ( strcmp(*(const char **)(v8 + 8), a4) )
{
v11 = v8;
v8 = 0LL;
user_free(v11);
*a5 = 110;
}
}
else
{
v12 = user_new_with_name(a2);
*(_QWORD *)(v12 + 8) = strdup(a4);
if ( (unsigned int)db_store_user(v12) )
{
user_free(v12);
*a5 = 108;
}
else
{
v8 = v12;
}
}
return v8;
}

original_code:
static user_t*
login(string username, string password, cr_t *res) {
    user_t* user = db_get_user(username);
    if (!str_is_nil(password) && !str_is_empty(password)) {
        if (user == ((void *)0)) {
            user = user_new_with_name(username.val);
            user->password = strdup(password.val);
            if (db_store_user(user) == db_ok) {
               return user;
            } else {
                user_free(user);
                res->code = 108;
                return ((void *)0);
            }
        }
        if (strcmp(user->password, password.val) != 0) {
            user_free(user);
            res->code = 110;
            user = ((void *)0);
        }
    } else if (user != ((void *)0)) {
        res->code = 108;
        user_free(user);
        user = ((void *)0);
    } else {
        user = user_new_with_name(username.val);
    }
    return user;
}

canonical_original_code:
static struct user_t *login(struct string username, struct string password, struct __command_result *res) {
    struct user_t *user = db_get_user(username);
    if (!str_is_nil(password) && !str_is_empty(password)) {
        if (user == ((void *)0)) {
            user = user_new_with_name(username.val);
            user->password = strdup(password.val);
            if (db_store_user(user) == db_ok) {
               return user;
            } else {
                user_free(user);
                res->code = 108;
                return ((void *)0);
            }
        }
        if (strcmp(user->password, password.val) != 0) {
            user_free(user);
            res->code = 110;
            user = ((void *)0);
        }
    } else if (user != ((void *)0)) {
        res->code = 108;
        user_free(user);
        user = ((void *)0);
    } else {
        user = user_new_with_name(username.val);
    }
    return user;
}

variable_types:
password
struct string

variable_types: struct __command_result { int code; struct user_t *user; } *

variable_types: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *


decompiled_code:
__int64 __fastcall command_destroy(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, unsigned int a8, __int64 a9)
{
return str_destroy(a8, a9);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, unsigned int a8, __int64 a9)
{
return str_destroy(a8, a9);
}

original_code:
void
command_destroy(command_t cmd) {
    str_destroy(cmd.args);
}

canonical_original_code:
void command_destroy(struct __command cmd) {
    str_destroy(cmd.args);
}

variable_types: struct __command { int code; struct string { int len; char *val; } args; }


decompiled_code:
__int64 __fastcall cr_create(unsigned int a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
return a1;
}

original_code:
cr_t
cr_create(cc_t code) {
    cr_t res = {code, ((void *)0)};
    return res;
}

canonical_original_code:
struct __command_result cr_create(int code) {
    struct __command_result res = {code, ((void *)0)};
    return res;
}

variable_types: code int

variable_types: struct __command_result { int code; struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *user; }


decompiled_code:
__int64 cr_empty()
{
return cr_create(0);
}

canonical_decompiled_code:
__int64 func10()
{
return func9(0);
}

original_code:
cr_t
cr_empty() {
    return cr_create(0);
}

canonical_original_code:
struct __command_result cr_empty() {
    return cr_create(0);
}


decompiled_code:
__int64 cr_ok()
{
return cr_create(0x64u);
}

canonical_decompiled_code:
__int64 func11()
{
return func9(0x64u);
}

original_code:
cr_t
cr_ok() {
    return cr_create(100);
}

canonical_original_code:
struct __command_result cr_ok() {
    return cr_create(100);
}


decompiled_code:
__int64 cr_parse_err()
{
return cr_create(0x6Au);
}

canonical_decompiled_code:
__int64 func12()
{
return func9(0x6Au);
}

original_code:
cr_t
cr_parse_err() {
    return cr_create(106);
}

canonical_original_code:
struct __command_result cr_parse_err() {
    return cr_create(106);
}


original_code:
static cr_t
exec_room_cmd(command_t cmd, user_t* const user, void (*exec)(struct room_req)) {
    cr_t res = cr_empty();
    tok_t tok = str_tok_init(' ', cmd.args);
    room_t *room = ((void *)0);
    string rname = str_tok(&tok, 0);
    if (str_is_nil(rname)) {
        res = cr_parse_err();
    } else if (mem_lookup_room(rname.val, &room) == 0) {
        struct room_req req = {cmd, user, room, tok, &res};
        exec(req);
    } else {
        res = cr_create(104);
    }
    str_destroy(rname);
    return res;
}

canonical_original_code:
static struct __command_result exec_room_cmd(struct __command cmd, struct user_t *user, void (*exec)(struct room_req)) {
    struct __command_result res = cr_empty();
    struct tok_t tok = str_tok_init(' ', cmd.args);
    struct __room *room = ((void *)0);
    struct string rname = str_tok(&tok, 0);
    if (str_is_nil(rname)) {
        res = cr_parse_err();
    } else if (mem_lookup_room(rname.val, &room) == 0) {
        struct room_req req = {cmd, user, room, tok, &res};
        exec(req);
    } else {
        res = cr_create(104);
    }
    str_destroy(rname);
    return res;
}

variable_types:
user
struct user_t *

variable_types: void (*)(struct room_req { struct __command cmd; struct user_t *user; struct __room { char *name; struct vec_t *admins; struct vec_t *users; struct vec_t *muted; } *room; struct tok_t { char sep; struct string str; int start; int has_more; } tok; struct __command_result *res; })

variable_types: struct tok_t { char sep; struct string { int len; char *val; } str; int start; int has_more; }

variable_types: struct __room { char *name; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *admins; struct vec_t *users; struct vec_t *muted; } *

variable_types:
rname
struct string { int len; char *val; }


decompiled_code:
__int64 __fastcall command_execute(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
__int64 result; // rax

if ( (_DWORD)a7 == -1 )
return cr_parse_err();
if ( (_DWORD)a7 )
{
if ( a1 )
{
switch ( (int)a7 )
{
case 3:
result = cmd_join_isra_0(a8, a9, a1);
break;
case 4:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_talk,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 5:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_leave,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 6:
result = cmd_msg_isra_0(a8, a9, a1);
break;
case 7:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_grant,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 8:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_mute,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 9:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_voice,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 10:
result = exec_room_cmd(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_kick,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
default:
result = cr_create(0x6Bu);
break;
}
}
else
{
result = cr_create(0x66u);
}
}
else if ( a1 )
{
result = cr_create(0x65u);
}
else
{
result = cmd_reg_isra_0((unsigned int)a8, a9, a3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int64 a7, __int64 a8, __int64 a9)
{
__int64 result; // rax

if ( (_DWORD)a7 == -1 )
return func12();
if ( (_DWORD)a7 )
{
if ( a1 )
{
switch ( (int)a7 )
{
case 3:
result = func15(a8, a9, a1);
break;
case 4:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_talk,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 5:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_leave,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 6:
result = func14(a8, a9, a1);
break;
case 7:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_grant,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 8:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_mute,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 9:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_voice,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
case 10:
result = func13(
a1,
(void (__fastcall *)(__int64, __int64 *, __int64, __int64, __int64, __int64, __int64, __int64, __int64, __int64))cmd_kick,
a3,
a4,
a5,
a6,
a7,
a8,
a9);
break;
default:
result = func9(0x6Bu);
break;
}
}
else
{
result = func9(0x66u);
}
}
else if ( a1 )
{
result = func9(0x65u);
}
else
{
result = func16((unsigned int)a8, a9, a3);
}
return result;
}

original_code:
cr_t
command_execute(command_t cmd, user_t * const user) {
    if (cmd.code == -1) {
        return cr_parse_err();
    }
    if (cmd.code == 0 && user != ((void *)0)) {
        return cr_create(101);
    }
    if (cmd.code != 0 && user == ((void *)0)) {
        return cr_create(102);
    }
    switch (cmd.code) {
    case 0:
        return cmd_reg(cmd);
    case 3:
        return cmd_join(cmd, user);
    case 4:
        return exec_room_cmd(cmd, user, cmd_talk);
    case 5:
        return exec_room_cmd(cmd, user, cmd_leave);
    case 6:
        return cmd_msg(cmd, user);
    case 7:
        return exec_room_cmd(cmd, user, cmd_grant);
    case 8:
        return exec_room_cmd(cmd, user, cmd_mute);
    case 9:
        return exec_room_cmd(cmd, user, cmd_voice);
    case 10:
        return exec_room_cmd(cmd, user, cmd_kick);
    }
    return cr_create(107);
}

canonical_original_code:
struct __command_result command_execute(struct __command cmd, struct user_t *user) {
    if (cmd.code == -1) {
        return cr_parse_err();
    }
    if (cmd.code == 0 && user != ((void *)0)) {
        return cr_create(101);
    }
    if (cmd.code != 0 && user == ((void *)0)) {
        return cr_create(102);
    }
    switch (cmd.code) {
    case 0:
        return cmd_reg(cmd);
    case 3:
        return cmd_join(cmd, user);
    case 4:
        return exec_room_cmd(cmd, user, cmd_talk);
    case 5:
        return exec_room_cmd(cmd, user, cmd_leave);
    case 6:
        return cmd_msg(cmd, user);
    case 7:
        return exec_room_cmd(cmd, user, cmd_grant);
    case 8:
        return exec_room_cmd(cmd, user, cmd_mute);
    case 9:
        return exec_room_cmd(cmd, user, cmd_voice);
    case 10:
        return exec_room_cmd(cmd, user, cmd_kick);
    }
    return cr_create(107);
}


decompiled_code:
__int64 __fastcall db_init(__int64 a1)
{
__int64 result; // rax
const char *v2; // rax

if ( (unsigned int)sqlite3_open(a1, &db) )
{
v2 = (const char *)sqlite3_errmsg(db);
log_sys(2LL, "Could not open database: %s", v2);
sqlite3_close(db);
result = 1LL;
}
else
{
sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY,username VARCHAR(255) NOT NULL UNIQUE,password VARCHAR(25"
"5) NOT NULL);CREATE TABLE IF NOT EXISTS chat_log (id INTEGER PRIMARY KEY,timestamp DATETIME DEFAULT CURRENT_TIMEST"
"AMP,room VARCHAR(255) NOT NULL,message TEXT);",
0LL,
0LL,
0LL);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 result; // rax
const char *v2; // rax

if ( (unsigned int)sqlite3_open(a1, &db) )
{
v2 = (const char *)sqlite3_errmsg(db);
log_sys(2LL, "Could not open database: %s", v2);
sqlite3_close(db);
result = 1LL;
}
else
{
sqlite3_exec(
db,
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY,username VARCHAR(255) NOT NULL UNIQUE,password VARCHAR(25"
"5) NOT NULL);CREATE TABLE IF NOT EXISTS chat_log (id INTEGER PRIMARY KEY,timestamp DATETIME DEFAULT CURRENT_TIMEST"
"AMP,room VARCHAR(255) NOT NULL,message TEXT);",
0LL,
0LL,
0LL);
result = 0LL;
}
return result;
}

original_code:
int
db_init(char *uri) {
    int rc;
    rc = sqlite3_open(uri, &db);
    if (rc) {
        log_sys(2, "Could not open database: %s", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }
    const char *sql = "CREATE TABLE IF NOT EXISTS users ("
        "id INTEGER PRIMARY KEY,"
        "username VARCHAR(255) NOT NULL UNIQUE,"
        "password VARCHAR(255) NOT NULL"
    ");"
    "CREATE TABLE IF NOT EXISTS chat_log ("
        "id INTEGER PRIMARY KEY,"
        "timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,"
        "room VARCHAR(255) NOT NULL,"
        "message TEXT"
    ");";
    sqlite3_exec(db, sql, 0, 0, 0);
    return 0;
}

canonical_original_code:
int db_init(char *uri) {
    int rc;
    rc = sqlite3_open(uri, &db);
    if (rc) {
        log_sys(2, "Could not open database: %s", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }
    char *sql = "CREATE TABLE IF NOT EXISTS users ("
        "id INTEGER PRIMARY KEY,"
        "username VARCHAR(255) NOT NULL UNIQUE,"
        "password VARCHAR(255) NOT NULL"
    ");"
    "CREATE TABLE IF NOT EXISTS chat_log ("
        "id INTEGER PRIMARY KEY,"
        "timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,"
        "room VARCHAR(255) NOT NULL,"
        "message TEXT"
    ");";
    sqlite3_exec(db, sql, 0, 0, 0);
    return 0;
}

variable_types:
uri
char *

variable_types: rc int

variable_types:
sql
char *


decompiled_code:
__int64 db_fini()
{
return sqlite3_close(db);
}

canonical_decompiled_code:
__int64 func1()
{
return sqlite3_close(db);
}

original_code:
void
db_fini() {
    sqlite3_close(db);
}

canonical_original_code:
void db_fini() {
    sqlite3_close(db);
}


decompiled_code:
__int64 __fastcall str_newn(const char *a1, unsigned int a2)
{
strndup(a1, (int)a2);
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, unsigned int a2)
{
strndup(a1, (int)a2);
return a2;
}

original_code:
string
str_newn(const char *str, int n) {
    char *cpy = strndup(str, n);
    string s = { n, cpy };
    return s;
}

canonical_original_code:
struct string str_newn(char *str, int n) {
    char *cpy = strndup(str, n);
    struct string s = { n, cpy };
    return s;
}

variable_types:
str
char *

variable_types: n int

variable_types:
cpy
char *

variable_types:
s
struct string { int len; char *val; }

return_type: struct string { int len; char *val; }


decompiled_code:
__int64 __fastcall str_new(const char *a1)
{
unsigned int v1; // eax

v1 = strlen(a1);
return str_newn(a1, v1);
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
unsigned int v1; // eax

v1 = strlen(a1);
return func0(a1, v1);
}

original_code:
string
str_new(const char *str) {
    int n = strlen(str);
    return str_newn(str, n);
}

canonical_original_code:
struct string str_new(char *str) {
    int n = strlen(str);
    return str_newn(str, n);
}


decompiled_code:
void __fastcall str_destroy(__int64 a1, void *a2)
{
free(a2);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, void *a2)
{
free(a2);
}

original_code:
void
str_destroy(string str) {
    free((char*)str.val);
}

canonical_original_code:
void str_destroy(struct string str) {
    free((char *)str.val);
}

variable_types:
str
struct string { int len; char *val; }


decompiled_code:
__int64 str_empty()
{
return str_new((const char *)&unk_289);
}

canonical_decompiled_code:
__int64 func3()
{
return func1((const char *)&unk_289);
}

canonical_original_code:
struct string str_empty() {
    return str_new("");
}


decompiled_code:
_BOOL8 __fastcall str_is_empty(int a1, _BYTE *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( !a1 )
result = *a2 == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(int a1, _BYTE *a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( !a1 )
result = *a2 == 0;
return result;
}

original_code:
int
str_is_empty(string str) {
    return str.len == 0 && strcmp("", str.val) == 0;
}

canonical_original_code:
int str_is_empty(struct string str) {
    return str.len == 0 && strcmp("", str.val) == 0;
}


decompiled_code:
__int64 str_nil()
{
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func5()
{
return 0xFFFFFFFFLL;
}

original_code:
string
str_nil() {
    string s = {-1, ((void *)0)};
    return s;
}

canonical_original_code:
struct string str_nil() {
    struct string s = {-1, ((void *)0)};
    return s;
}


decompiled_code:
_BOOL8 __fastcall str_is_nil(int a1, __int64 a2)
{
_BOOL8 result; // rax

result = 0LL;
if ( a1 == -1 )
result = a2 == 0;
return result;
}

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

result = 0LL;
if ( a1 == -1 )
result = a2 == 0;
return result;
}

original_code:
int
str_is_nil(string str) {
    return str.len == -1 && str.val == ((void *)0);
}

canonical_original_code:
int str_is_nil(struct string str) {
    return str.len == -1 && str.val == ((void *)0);
}


decompiled_code:
__int64 __fastcall str_sub(__int64 a1, __int64 a2, int a3, int a4)
{
unsigned int v5; // esi
unsigned int v6; // edi

v5 = a4;
if ( a3 + a4 > (int)a1 || a3 < 0 )
return str_nil(a1, (unsigned int)a4);
if ( !a4 )
return str_empty();
v6 = a1 - a3;
if ( a4 < 0 )
v5 = v6;
return str_newn((const char *)(a3 + a2), v5);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, int a3, int a4)
{
unsigned int v5; // esi
unsigned int v6; // edi

v5 = a4;
if ( a3 + a4 > (int)a1 || a3 < 0 )
return func5(a1, (unsigned int)a4);
if ( !a4 )
return func3();
v6 = a1 - a3;
if ( a4 < 0 )
v5 = v6;
return func0((const char *)(a3 + a2), v5);
}

original_code:
string
str_sub(string str, int start, int count) {
    if (str.len < count + start || start < 0) {
        return str_nil();
    }
    if (count == 0) {
        return str_empty();
    }
    if (count < 0) {
        count = str.len - start;
    }
    return str_newn(str.val + start, count);
}

canonical_original_code:
struct string str_sub(struct string str, int start, int count) {
    if (str.len < count + start || start < 0) {
        return str_nil();
    }
    if (count == 0) {
        return str_empty();
    }
    if (count < 0) {
        count = str.len - start;
    }
    return str_newn(str.val + start, count);
}

variable_types:
str
struct string

variable_types: start int

variable_types: count int


decompiled_code:
_BOOL8 __fastcall str_eq(int a1, __int64 a2, int a3)
{
_BOOL8 result; // rax
const char *v4; // rcx
const char *v5; // r9
_BOOL4 v6; // er8

result = 0LL;
if ( a3 == a1 )
{
if ( !str_is_nil(a1, a2) || (v6 = str_is_nil(a1, (__int64)v4), result = 1LL, !v6) )
result = strcmp(v5, v4) == 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(int a1, __int64 a2, int a3)
{
_BOOL8 result; // rax
const char *v4; // rcx
const char *v5; // r9
_BOOL4 v6; // er8

result = 0LL;
if ( a3 == a1 )
{
if ( !func6(a1, a2) || (v6 = func6(a1, (__int64)v4), result = 1LL, !v6) )
result = strcmp(v5, v4) == 0;
}
return result;
}

original_code:
int
str_eq(string s1, string s2) {
    if (s1.len != s2.len) {
        return 0;
    }
    if (str_is_nil(s1) && str_is_nil(s2)) {
        return 1;
    }
    return strcmp(s1.val, s2.val) == 0;
}

canonical_original_code:
int str_eq(struct string s1, struct string s2) {
    if (s1.len != s2.len) {
        return 0;
    }
    if (str_is_nil(s1) && str_is_nil(s2)) {
        return 1;
    }
    return strcmp(s1.val, s2.val) == 0;
}


original_code:
string
str_trim(string str) {
    int lindex = 0;
    int rindex = str.len - 1;
    while(((*__ctype_b_loc ())[(int) ((str.val[lindex]))] & (unsigned short int) _ISspace)) {
        ++lindex;
    }
    while (rindex > lindex && ((*__ctype_b_loc ())[(int) ((str.val[rindex]))] & (unsigned short int) _ISspace)) {
        --rindex;
    }
    return str_sub(str, lindex, rindex - lindex + 1);
}

canonical_original_code:
struct string str_trim(struct string str) {
    int lindex = 0;
    int rindex = str.len - 1;
    while(((*__ctype_b_loc ())[(int) ((str.val[lindex]))] & (unsigned short) _ISspace)) {
        ++lindex;
    }
    while (rindex > lindex && ((*__ctype_b_loc ())[(int) ((str.val[rindex]))] & (unsigned short) _ISspace)) {
        --rindex;
    }
    return str_sub(str, lindex, rindex - lindex + 1);
}

variable_types: lindex int

variable_types: rindex int


original_code:
string
str_tok(tok_t *tok, int include_sep) {
    int i = tok->start;
    string str = tok->str;
    if (i > str.len || !tok->has_more) {
        return str_nil();
    }
    while (i <= str.len && tok->sep != str.val[i]) ++i;
    if (i > str.len) {
        tok->has_more = 0;
        return str_sub(str, tok->start, -1);
    }
    int count = include_sep ? i + 1 : i;
    string token = str_sub(str, tok->start, count);
    tok->start = i + 1;
    tok->has_more = 1;
    return token;
}

canonical_original_code:
struct string str_tok(struct tok_t *tok, int include_sep) {
    int i = tok->start;
    struct string str = tok->str;
    if (i > str.len || !tok->has_more) {
        return str_nil();
    }
    while (i <= str.len && tok->sep != str.val[i]) ++i;
    if (i > str.len) {
        tok->has_more = 0;
        return str_sub(str, tok->start, -1);
    }
    int count = include_sep ? i + 1 : i;
    struct string token = str_sub(str, tok->start, count);
    tok->start = i + 1;
    tok->has_more = 1;
    return token;
}

variable_types: struct tok_t { char sep; struct string str; int start; int has_more; } *

variable_types: i int

variable_types:
token
struct string { int len; char *val; }


decompiled_code:
__int64 __fastcall str_tok_rest(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, unsigned int a8, __int64 a9, int a10)
{
return str_sub(a8, a9, a10, -1);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, unsigned int a8, __int64 a9, int a10)
{
return func7(a8, a9, a10, -1);
}

original_code:
string
str_tok_rest(tok_t tok) {
    return str_sub(tok.str, tok.start, -1);
}

canonical_original_code:
struct string str_tok_rest(struct tok_t tok) {
    return str_sub(tok.str, tok.start, -1);
}

variable_types: struct tok_t { char sep; struct string str; int start; int has_more; }


decompiled_code:
void __fastcall log_init(int a1)
{
openlog(0LL, 3, 8);
is_daemon = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
openlog(0LL, 3, 8);
is_daemon = a1;
}

original_code:
void
log_init(int d) {
    openlog(((void *)0), 0x02 | 0x01, (1<<3));
    is_daemon = d;
}

variable_types: d int


decompiled_code:
// attributes: thunk
void log_fini(void)
{
closelog();
}

canonical_decompiled_code:
void func1(void)
{
closelog();
}

original_code:
void
log_fini() {
    closelog();
}

canonical_original_code:
void log_fini() {
    closelog();
}


decompiled_code:
void log_sys(int a1, const char *a2, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-2D0h] BYREF
char s[16]; // [rsp+20h] [rbp-2B8h] BYREF
char v4[480]; // [rsp+30h] [rbp-2A8h] BYREF
int v5; // [rsp+210h] [rbp-C8h]

va_start(ap, a2);
if ( is_daemon )
{
vsyslog(a1, a2, ap);
}
else
{
memset(v4, 0, sizeof(v4));
*(_OWORD *)s = 0LL;
v5 = 0;
vsnprintf(s, 0x1F4uLL, a2, ap);
write(1, s, 0x1F4uLL);
write(1, &unk_154, 1uLL);
}
}

canonical_decompiled_code:
void func2(int a1, const char *a2, ...)
{
gcc_va_list ap; // [rsp+8h] [rbp-2D0h] BYREF
char s[16]; // [rsp+20h] [rbp-2B8h] BYREF
char v4[480]; // [rsp+30h] [rbp-2A8h] BYREF
int v5; // [rsp+210h] [rbp-C8h]

va_start(ap, a2);
if ( is_daemon )
{
vsyslog(a1, a2, ap);
}
else
{
memset(v4, 0, sizeof(v4));
*(_OWORD *)s = 0LL;
v5 = 0;
vsnprintf(s, 0x1F4uLL, a2, ap);
write(1, s, 0x1F4uLL);
write(1, &unk_154, 1uLL);
}
}

original_code:
void
log_sys(int level, const char *format, ...) {
    va_list args;
    __builtin_va_start(args,format);
    if (is_daemon) {
        vsyslog(level, format, args);
    } else {
        char buf[500] = {0};
        vsnprintf(buf, 500, format, args);
        write(1, buf, 500);
        write(1, "\n", 1);
    }
    __builtin_va_end(args);
}

canonical_original_code:
void log_sys(int level, char *format, ...) {
    __builtin_va_list args;
    __builtin_va_start(args,format);
    if (is_daemon) {
        vsyslog(level, format, args);
    } else {
        char buf[500] = {0};
        vsnprintf(buf, 500, format, args);
        write(1, buf, 500);
        write(1, "\n", 1);
    }
    __builtin_va_end(args);
}

variable_types: level int

variable_types:
format
char *

variable_types:
buf
char [500]


decompiled_code:
_BOOL8 __fastcall room_eq(const char *a1, const char **a2)
{
return strcmp(a1, *a2) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(const char *a1, const char **a2)
{
return strcmp(a1, *a2) == 0;
}

original_code:
static int
room_eq(any_t name, any_t room) {
    char *rname = (char*)name;
    room_t *r = (room_t*)room;
    return strcmp(rname, r->name) == 0;
}

canonical_original_code:
static int room_eq(void *name, void *room) {
    char *rname = (char*)name;
    struct __room *r = (room_t*)room;
    return strcmp(rname, r->name) == 0;
}

variable_types:
name
void *

variable_types:
room
void *

variable_types:
rname
char *

variable_types: struct __room { char *name; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *admins; struct vec_t *users; struct vec_t *muted; } *


decompiled_code:
__int64 mem_init()
{
__int64 result; // rax

users = vec_new_cap(100LL);
result = vec_new_cap(100LL);
rooms = result;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

users = vec_new_cap(100LL);
result = vec_new_cap(100LL);
rooms = result;
return result;
}

original_code:
void
mem_init() {
    users = vec_new_cap(100);
    rooms = vec_new_cap(100);
}


decompiled_code:
__int64 __fastcall mem_store_user(__int64 a1)
{
vec_add(users, a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
vec_add(users, a1);
return 0LL;
}

original_code:
mem_res
mem_store_user(user_t *user) {
    vec_add(users, user);
    return 0;
}

canonical_original_code:
int mem_store_user(struct user_t *user) {
    vec_add(users, user);
    return 0;
}

variable_types: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } *


decompiled_code:
_BOOL8 __fastcall mem_remove_user(__int64 a1)
{
return (unsigned int)vec_remove(users, a1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
return (unsigned int)vec_remove(users, a1) == 0;
}

original_code:
mem_res
mem_remove_user(user_t* const user) {
    int res = vec_remove(users, user);
    return (res ? 0 : 1);
}

canonical_original_code:
int mem_remove_user(struct user_t *user) {
    int res = vec_remove(users, user);
    return (res ? 0 : 1);
}

variable_types: res int


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

v2 = vec_find(users, a1, &user_eq);
if ( !v2 )
return 1LL;
*a2 = v2;
return 0LL;
}

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

v2 = vec_find(users, a1, &user_eq);
if ( !v2 )
return 1LL;
*a2 = v2;
return 0LL;
}

original_code:
mem_res
mem_lookup_user(const char *username, user_t **user) {
    user_t *u = vec_find(users, (any_t)username, user_eq);
    if (u == ((void *)0)) {
        return 1;
    }
    *user = u;
    return 0;
}

canonical_original_code:
int mem_lookup_user(char *username, struct user_t **user) {
    struct user_t *u = vec_find(users, (any_t)username, user_eq);
    if (u == ((void *)0)) {
        return 1;
    }
    *user = u;
    return 0;
}

variable_types:
username
char *

variable_types: struct user_t { char *username; char *password; int sock; struct vec_t { int len; int cap; void **data; union pthread_rwlock_t { struct __pthread_rwlock_arch_t { unsigned int __readers; unsigned int __writers; unsigned int __wrphase_futex; unsigned int __writers_futex; unsigned int __pad3; unsigned int __pad4; int __cur_writer; int __shared; char __rwelision; unsigned char __pad1[7]; unsigned long __pad2; unsigned int __flags; } __data; char __size[56]; long __align; } *lock; } *rooms; } **