decompiled_code:
__int64 __fastcall gdp_server_send(__int64 a1, __int64 a2, const void *a3, unsigned int a4)
{
size_t v6; // r15
void *v7; // rbx
__int64 v8; // rax

v6 = (int)a4;
v7 = (void *)xmalloc((int)a4);
memcpy(v7, a3, v6);
v8 = gdp_packet_new(a2, v7, a4);
return gdp_server_send_packet(a1, v8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, const void *a3, unsigned int a4)
{
size_t v6; // r15
void *v7; // rbx
__int64 v8; // rax

v6 = (int)a4;
v7 = (void *)xmalloc((int)a4);
memcpy(v7, a3, v6);
v8 = gdp_packet_new(a2, v7, a4);
return func4(a1, v8);
}

original_code:
void gdp_server_send(gdp_server_t* server, gdp_client_t* client, char* buffer, int len) {
 char* buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 gdp_packet_t* pack = gdp_packet_new(client, buf_clone, len);
 gdp_server_send_packet(server, pack);
}

canonical_original_code:
void gdp_server_send(struct gdp_server *server, struct gdp_client *client, char *buffer, int len) {
 char *buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 struct gdp_packet *pack = gdp_packet_new(client, buf_clone, len);
 gdp_server_send_packet(server, pack);
}

variable_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list *_onclose_callbacks; } *

variable_types: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *


decompiled_code:
__int64 __fastcall gdp_server_broadcast_except(__int64 a1, const void *a2, unsigned int a3, __int64 a4)
{
int i; // ebx
__int64 result; // rax
__int64 v9; // rsi

for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 16));
if ( (int)result <= i )
break;
v9 = gdp_list_get(*(_QWORD *)(a1 + 16), (unsigned int)i);
if ( v9 != a4 )
gdp_server_send(a1, v9, a2, a3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, const void *a2, unsigned int a3, __int64 a4)
{
int i; // ebx
__int64 result; // rax
__int64 v9; // rsi

for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 16));
if ( (int)result <= i )
break;
v9 = gdp_list_get(*(_QWORD *)(a1 + 16), (unsigned int)i);
if ( v9 != a4 )
func5(a1, v9, a2, a3);
}
return result;
}

original_code:
void gdp_server_broadcast_except(gdp_server_t* server, char* buffer, int len, gdp_client_t* except_client) {
 int i;
 for (i = 0; i < gdp_list_count(server->clients); i++) {
  gdp_client_t* client = (gdp_client_t*)gdp_list_get(server->clients, i);
  if (client == except_client)
   continue;
  gdp_server_send(server, client, buffer, len);
 }
}

canonical_original_code:
void gdp_server_broadcast_except(struct gdp_server *server, char *buffer, int len, struct gdp_client *except_client) {
 int i;
 for (i = 0; i < gdp_list_count(server->clients); i++) {
  struct gdp_client *client = (gdp_client_t*)gdp_list_get(server->clients, i);
  if (client == except_client)
   continue;
  gdp_server_send(server, client, buffer, len);
 }
}

variable_types: struct gdp_server { int sock; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onopenclose_callbacks; struct gdp_list *clients; } *

variable_types:
buffer
char *

variable_types: len int

variable_types: i int

variable_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *

return_type: void

user_defined_types: struct gdp_server { int sock; struct gdp_list *_onopenclose_callbacks; struct gdp_list *clients; }

user_defined_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr addr; int _closed; int state; struct gdp_stream_writer *_prebuffer; struct gdp_packet *_prepack; struct gdp_list *_onclose_callbacks; }

user_defined_types: struct gdp_list { int count; int available; void **items; union pthread_mutex_t lock_obj; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall gdp_server_broadcast(__int64 a1, const void *a2, unsigned int a3)
{
return gdp_server_broadcast_except(a1, a2, a3, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const void *a2, unsigned int a3)
{
return func6(a1, a2, a3, 0LL);
}

original_code:
void gdp_server_broadcast(gdp_server_t* server, char* buffer, int len) {
 gdp_server_broadcast_except(server, buffer, len, ((void *)0));
}

canonical_original_code:
void gdp_server_broadcast(struct gdp_server *server, char *buffer, int len) {
 gdp_server_broadcast_except(server, buffer, len, ((void *)0));
}

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
void __fastcall gdp_server_close(void *ptr)
{
int i; // ebp
__int64 v2; // rbx

for ( i = 0; (int)gdp_list_count(*((_QWORD *)ptr + 2)) > i; ++i )
{
v2 = gdp_list_get(*((_QWORD *)ptr + 2), (unsigned int)i);
if ( !(unsigned int)gdp_client_isclosed(v2) )
gdp_client_close(v2);
}
close(*(_DWORD *)ptr);
gdp_list_free(*((_QWORD *)ptr + 2));
gdp_list_free(*((_QWORD *)ptr + 1));
gdp_io_thread_remove_server(ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
int i; // ebp
__int64 v2; // rbx

for ( i = 0; (int)gdp_list_count(*((_QWORD *)ptr + 2)) > i; ++i )
{
v2 = gdp_list_get(*((_QWORD *)ptr + 2), (unsigned int)i);
if ( !(unsigned int)gdp_client_isclosed(v2) )
gdp_client_close(v2);
}
close(*(_DWORD *)ptr);
gdp_list_free(*((_QWORD *)ptr + 2));
gdp_list_free(*((_QWORD *)ptr + 1));
gdp_io_thread_remove_server(ptr);
free(ptr);
}

original_code:
void gdp_server_close(gdp_server_t* server) {
 int i;
 for (i = 0; i < gdp_list_count(server->clients); i++) {
  gdp_client_t* client = (gdp_client_t*)gdp_list_get(server->clients, i);
  if (!gdp_client_isclosed(client)) {
   gdp_client_close(client);
  }
 }
 close(server->sock);
 gdp_list_free(server->clients);
 gdp_list_free(server->_onopenclose_callbacks);
 gdp_io_thread_remove_server(server);
 free(server); server = ((void *)0);;
}

canonical_original_code:
void gdp_server_close(struct gdp_server *server) {
 int i;
 for (i = 0; i < gdp_list_count(server->clients); i++) {
  struct gdp_client *client = (gdp_client_t*)gdp_list_get(server->clients, i);
  if (!gdp_client_isclosed(client)) {
   gdp_client_close(client);
  }
 }
 close(server->sock);
 gdp_list_free(server->clients);
 gdp_list_free(server->_onopenclose_callbacks);
 gdp_io_thread_remove_server(server);
 free(server); server = ((void *)0);;
}

user_defined_types: struct gdp_addr { char ip[15]; int port; }

user_defined_types: struct gdp_stream_writer { char *buffer; int maxsize; int size; }

user_defined_types: struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; }


decompiled_code:
__int64 __fastcall gdp_server_accept(__int64 a1)
{
unsigned int v1; // ebx
__int64 v2; // r12
int i; // ebx
char *v4; // rsi
void (__fastcall *v5)(__int64, _QWORD); // rax
__int64 result; // rax
socklen_t addr_len; // [rsp+2Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
char dest[16]; // [rsp+40h] [rbp-38h] BYREF
int v10; // [rsp+50h] [rbp-28h]

*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr_len = 16;
v1 = accept(*(_DWORD *)a1, &addr, &addr_len);
strcpy(dest, "IPv6");
v10 = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
if ( addr.sa_family == 2 )
{
v4 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(dest, v4);
}
v2 = gdp_client_new(v1);
gdp_client_add_onclose(v2, gdp_server_onclose_client, a1);
gdp_list_add(*(_QWORD *)(a1 + 16));
for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 8));
if ( (int)result <= i )
break;
v5 = (void (__fastcall *)(__int64, _QWORD))gdp_list_get(*(_QWORD *)(a1 + 8), (unsigned int)i);
v5(v2, 0LL);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
unsigned int v1; // ebx
__int64 v2; // r12
int i; // ebx
char *v4; // rsi
void (__fastcall *v5)(__int64, _QWORD); // rax
__int64 result; // rax
socklen_t addr_len; // [rsp+2Ch] [rbp-4Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
char dest[16]; // [rsp+40h] [rbp-38h] BYREF
int v10; // [rsp+50h] [rbp-28h]

*(_QWORD *)&addr.sa_family = 0LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr_len = 16;
v1 = accept(*(_DWORD *)a1, &addr, &addr_len);
strcpy(dest, "IPv6");
v10 = (unsigned __int16)func1(*(__int16 *)addr.sa_data);
if ( addr.sa_family == 2 )
{
v4 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(dest, v4);
}
v2 = gdp_client_new(v1);
gdp_client_add_onclose(v2, gdp_server_onclose_client, a1);
gdp_list_add(*(_QWORD *)(a1 + 16));
for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 8));
if ( (int)result <= i )
break;
v5 = (void (__fastcall *)(__int64, _QWORD))gdp_list_get(*(_QWORD *)(a1 + 8), (unsigned int)i);
v5(v2, 0LL);
}
return result;
}

original_code:
void gdp_server_accept(gdp_server_t* server) {
 gdp_addr_t addr;
 struct sockaddr_in sin;
 memset(&sin, 0, sizeof(sin));
 socklen_t sinlen = sizeof(sin);
 int clientsocket = accept(server->sock, (struct sockaddr*)&sin, &sinlen);
 strcpy(addr.ip, "IPv6");
 addr.port = ntohs(sin.sin_port);
 if (sin.sin_family == 2) {
  strcpy(addr.ip, inet_ntoa(sin.sin_addr));
 }
 gdp_client_t* client = gdp_client_new(clientsocket, addr);
 gdp_client_add_onclose(client, gdp_server_onclose_client, server);
 gdp_list_add(server->clients, client);
 int i = 0;
 for (i = 0; i < gdp_list_count(server->_onopenclose_callbacks); i++) {
  onopenclose_callback callback = gdp_list_get(server->_onopenclose_callbacks, i);
  callback(client, 0);
 }
}

canonical_original_code:
void gdp_server_accept(struct gdp_server *server) {
 struct gdp_addr addr;
 struct sockaddr_in sin;
 memset(&sin, 0, sizeof(sin));
 unsigned int sinlen = sizeof(sin);
 int clientsocket = accept(server->sock, (struct sockaddr*)&sin, &sinlen);
 strcpy(addr.ip, "IPv6");
 addr.port = ntohs(sin.sin_port);
 if (sin.sin_family == 2) {
  strcpy(addr.ip, inet_ntoa(sin.sin_addr));
 }
 struct gdp_client *client = gdp_client_new(clientsocket, addr);
 gdp_client_add_onclose(client, gdp_server_onclose_client, server);
 gdp_list_add(server->clients, client);
 int i = 0;
 for (i = 0; i < gdp_list_count(server->_onopenclose_callbacks); i++) {
  void (*callback)(struct gdp_client *, int) = gdp_list_get(server->_onopenclose_callbacks, i);
  callback(client, 0);
 }
}

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:
sinlen
unsigned int

variable_types: clientsocket int

variable_types: void (*)(struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *, 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]; }


decompiled_code:
int __fastcall gdp_server_listen(unsigned int *a1, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
int result; // eax
int *v5; // rax
int *v6; // rax
int *v7; // rax
struct sockaddr v8; // [rsp+0h] [rbp-38h] BYREF
char optval; // [rsp+17h] [rbp-21h] BYREF
__int64 v10[4]; // [rsp+18h] [rbp-20h] BYREF

gdp_io_thread_assure_wsa();
v2 = socket(2, 1, 6);
if ( v2 == -1 )
{
v5 = _errno_location();
printf("Error at socket(): %d\n", (unsigned int)*v5);
exit(1);
}
v3 = v2;
v10[0] = 1LL;
optval = 1;
ioctl(v2, 0x5421uLL, &optval);
setsockopt(v3, 6, 1, &optval, 1u);
setsockopt(v3, 1, 2, v10, 8u);
v8.sa_family = 2;
*(_QWORD *)&v8.sa_data[6] = 0LL;
*(_DWORD *)&v8.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)v8.sa_data = _bswap_16(a2);
if ( bind(v3, &v8, 0x10u) == -1 )
{
v6 = _errno_location();
printf("bind failed with error: %d\n", (unsigned int)*v6);
result = close(v3);
}
else if ( listen(v3, 10) == -1 )
{
v7 = _errno_location();
printf("Listen failed with error: %d\n", (unsigned int)*v7);
result = close(v3);
}
else
{
*a1 = v3;
gdp_io_thread_inst(v3, 10LL);
result = gdp_io_thread_add_server(a1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(unsigned int *a1, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
int result; // eax
int *v5; // rax
int *v6; // rax
int *v7; // rax
struct sockaddr v8; // [rsp+0h] [rbp-38h] BYREF
char optval; // [rsp+17h] [rbp-21h] BYREF
__int64 v10[4]; // [rsp+18h] [rbp-20h] BYREF

gdp_io_thread_assure_wsa();
v2 = socket(2, 1, 6);
if ( v2 == -1 )
{
v5 = _errno_location();
printf("Error at socket(): %d\n", (unsigned int)*v5);
exit(1);
}
v3 = v2;
v10[0] = 1LL;
optval = 1;
ioctl(v2, 0x5421uLL, &optval);
setsockopt(v3, 6, 1, &optval, 1u);
setsockopt(v3, 1, 2, v10, 8u);
v8.sa_family = 2;
*(_QWORD *)&v8.sa_data[6] = 0LL;
*(_DWORD *)&v8.sa_data[2] = inet_addr("0.0.0.0");
*(_WORD *)v8.sa_data = func1(a2);
if ( bind(v3, &v8, 0x10u) == -1 )
{
v6 = _errno_location();
printf("bind failed with error: %d\n", (unsigned int)*v6);
result = close(v3);
}
else if ( listen(v3, 10) == -1 )
{
v7 = _errno_location();
printf("Listen failed with error: %d\n", (unsigned int)*v7);
result = close(v3);
}
else
{
*a1 = v3;
gdp_io_thread_inst(v3, 10LL);
result = gdp_io_thread_add_server(a1);
}
return result;
}

original_code:
void gdp_server_listen(gdp_server_t* server, int port) {
 gdp_io_thread_assure_wsa();
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (sock == -1) {
  printf("Error at socket(): %d\n", (*__errno_location ()));
  exit(1);
 }
 u_long mode = 1;
    char on = 1;
    ioctl(sock, 0x5421, (char *)&on);
    setsockopt(sock, IPPROTO_TCP, 1, &on, sizeof(on));
    setsockopt(sock, 1, 2, &mode, sizeof(mode));
 struct sockaddr_in sin;
 memset(&sin, 0, sizeof(sin));
 sin.sin_family = 2;
 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
 sin.sin_port = htons(port);
 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
  printf("bind failed with error: %d\n", (*__errno_location ()));
  close(sock);
  ;
  return;
 }
 if (listen(sock, 10) == -1) {
  printf("Listen failed with error: %d\n", (*__errno_location ()));
  close(sock);
  ;
  return;
 }
 server->sock = sock;
 gdp_io_thread_inst();
 gdp_io_thread_add_server(server);
}

canonical_original_code:
void gdp_server_listen(struct gdp_server *server, int port) {
 gdp_io_thread_assure_wsa();
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (sock == -1) {
  printf("Error at socket(): %d\n", (*__errno_location ()));
  exit(1);
 }
 unsigned long mode = 1;
    char on = 1;
    ioctl(sock, 0x5421, (char *)&on);
    setsockopt(sock, IPPROTO_TCP, 1, &on, sizeof(on));
    setsockopt(sock, 1, 2, &mode, sizeof(mode));
 struct sockaddr_in sin;
 memset(&sin, 0, sizeof(sin));
 sin.sin_family = 2;
 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
 sin.sin_port = htons(port);
 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
  printf("bind failed with error: %d\n", (*__errno_location ()));
  close(sock);
  ;
  return;
 }
 if (listen(sock, 10) == -1) {
  printf("Listen failed with error: %d\n", (*__errno_location ()));
  close(sock);
  ;
  return;
 }
 server->sock = sock;
 gdp_io_thread_inst();
 gdp_io_thread_add_server(server);
}

variable_types: port int

variable_types: sock int

variable_types:
mode
unsigned long

variable_types: on char

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


decompiled_code:
__int64 gdp_server_new()
{
__int64 v0; // rbx

v0 = xmalloc0(24LL);
*(_QWORD *)(v0 + 16) = gdp_list_new();
*(_QWORD *)(v0 + 8) = gdp_list_new();
return v0;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rbx

v0 = xmalloc0(24LL);
*(_QWORD *)(v0 + 16) = gdp_list_new();
*(_QWORD *)(v0 + 8) = gdp_list_new();
return v0;
}

original_code:
gdp_server_t* gdp_server_new(void) {
 gdp_server_t* server = (gdp_server_t*)xmalloc0(sizeof(gdp_server_t));
 server->clients = gdp_list_new();
 server->_onopenclose_callbacks = gdp_list_new();
 return server;
}

canonical_original_code:
struct gdp_server *gdp_server_new() {
 struct gdp_server *server = (gdp_server_t*)xmalloc0(sizeof(gdp_server_t));
 server->clients = gdp_list_new();
 server->_onopenclose_callbacks = gdp_list_new();
 return server;
}

return_type: struct gdp_server { int sock; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onopenclose_callbacks; struct gdp_list *clients; } *


decompiled_code:
__int64 __fastcall gdp_client_new(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, int a8)
{
__int64 v8; // rbx

v8 = xmalloc0(64LL);
strcpy((char *)v8, "CLIENT");
*(_DWORD *)(v8 + 8) = a1;
*(__m128i *)(v8 + 12) = _mm_loadu_si128((const __m128i *)&a7);
*(_DWORD *)(v8 + 28) = a8;
*(_DWORD *)(v8 + 36) = 1;
*(_QWORD *)(v8 + 40) = gdp_stream_writer_new(8LL);
*(_QWORD *)(v8 + 56) = gdp_list_new();
gdp_io_thread_inst();
gdp_io_thread_add_client(v8);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, __int128 a7, int a8)
{
__int64 v8; // rbx

v8 = xmalloc0(64LL);
strcpy((char *)v8, "CLIENT");
*(_DWORD *)(v8 + 8) = a1;
*(__m128i *)(v8 + 12) = _mm_loadu_si128((const __m128i *)&a7);
*(_DWORD *)(v8 + 28) = a8;
*(_DWORD *)(v8 + 36) = 1;
*(_QWORD *)(v8 + 40) = gdp_stream_writer_new(8LL);
*(_QWORD *)(v8 + 56) = gdp_list_new();
gdp_io_thread_inst();
gdp_io_thread_add_client(v8);
return v8;
}

original_code:
gdp_client_t* gdp_client_new(int sock, gdp_addr_t addr) {
 gdp_client_t* client = (gdp_client_t*)xmalloc0(sizeof(gdp_client_t));
 strcpy(client->objsign, "CLIENT");
 client->sock = sock;
 client->addr = addr;
 client->state = IN_HEADER;
 client->_prebuffer = gdp_stream_writer_new(8);
 client->_onclose_callbacks = gdp_list_new();
 gdp_io_thread_inst();
 gdp_io_thread_add_client(client);
 return client;
}

canonical_original_code:
struct gdp_client *gdp_client_new(int sock, struct gdp_addr addr) {
 struct gdp_client *client = (gdp_client_t*)xmalloc0(sizeof(gdp_client_t));
 strcpy(client->objsign, "CLIENT");
 client->sock = sock;
 client->addr = addr;
 client->state = IN_HEADER;
 client->_prebuffer = gdp_stream_writer_new(8);
 client->_onclose_callbacks = gdp_list_new();
 gdp_io_thread_inst();
 gdp_io_thread_add_client(client);
 return client;
}

variable_types:
addr
struct gdp_addr

return_type: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *


decompiled_code:
__int64 __fastcall gdp_client_add_onclose(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = (_QWORD *)xmalloc0(16LL);
v4[1] = a2;
*v4 = a3;
return gdp_list_add(*(_QWORD *)(a1 + 56), v4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = (_QWORD *)xmalloc0(16LL);
v4[1] = a2;
*v4 = a3;
return gdp_list_add(*(_QWORD *)(a1 + 56), v4);
}

original_code:
void gdp_client_add_onclose(gdp_client_t* client, event_handler callback, void* state) {
 gdp_event_info_t* event_info = (gdp_event_info_t*)xmalloc0(sizeof(gdp_event_info_t));
 event_info->callback = callback;
 event_info->state = state;
 gdp_list_add(client->_onclose_callbacks, event_info);
}

canonical_original_code:
void gdp_client_add_onclose(struct gdp_client *client, void (*callback)(void *, void *), void *state) {
 struct gdp_event_info *event_info = (gdp_event_info_t*)xmalloc0(sizeof(gdp_event_info_t));
 event_info->callback = callback;
 event_info->state = state;
 gdp_list_add(client->_onclose_callbacks, event_info);
}

variable_types:
callback
void (*)(void *, void *)

variable_types:
state
void *

variable_types: struct gdp_event_info { void *state; void (*callback)(void *, void *); } *


decompiled_code:
__int64 __fastcall gdp_client_remove_onclose(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int i; // ebp
__int64 result; // rax
_QWORD *v6; // rax
void *v7; // rbx

for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 56));
if ( (int)result <= (int)i )
break;
v6 = (_QWORD *)gdp_list_get(*(_QWORD *)(a1 + 56), i);
v7 = v6;
if ( v6[1] == a2 && *v6 == a3 )
{
gdp_list_remove(*(_QWORD *)(a1 + 56), i);
free(v7);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, __int64 a3)
{
unsigned int i; // ebp
__int64 result; // rax
_QWORD *v6; // rax
void *v7; // rbx

for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(a1 + 56));
if ( (int)result <= (int)i )
break;
v6 = (_QWORD *)gdp_list_get(*(_QWORD *)(a1 + 56), i);
v7 = v6;
if ( v6[1] == a2 && *v6 == a3 )
{
gdp_list_remove(*(_QWORD *)(a1 + 56), i);
free(v7);
}
}
return result;
}

original_code:
void gdp_client_remove_onclose(gdp_client_t* client, event_handler callback, void* state) {
 int i;
 for (i = 0; i < gdp_list_count(client->_onclose_callbacks); ++i)
 {
  gdp_event_info_t* ei = (gdp_event_info_t*)gdp_list_get(client->_onclose_callbacks, i);
  if (ei->callback == callback && ei->state == state) {
   gdp_list_remove(client->_onclose_callbacks, i);
   free(ei); ei = ((void *)0);;
  }
 }
}

canonical_original_code:
void gdp_client_remove_onclose(struct gdp_client *client, void (*callback)(void *, void *), void *state) {
 int i;
 for (i = 0; i < gdp_list_count(client->_onclose_callbacks); ++i)
 {
  struct gdp_event_info *ei = (gdp_event_info_t*)gdp_list_get(client->_onclose_callbacks, i);
  if (ei->callback == callback && ei->state == state) {
   gdp_list_remove(client->_onclose_callbacks, i);
   free(ei); ei = ((void *)0);;
  }
 }
}

user_defined_types: struct gdp_event_info { void *state; void (*callback)(void *, void *); }


decompiled_code:
__int64 __fastcall gdp_client_send(__int64 a1, const void *a2, unsigned int a3)
{
size_t v4; // r14
void *v5; // rbx
__int64 v6; // rax

v4 = (int)a3;
v5 = (void *)xmalloc((int)a3);
memcpy(v5, a2, v4);
v6 = gdp_packet_new(a1, v5, a3);
return gdp_io_thread_enqueue_send(v6);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const void *a2, unsigned int a3)
{
size_t v4; // r14
void *v5; // rbx
__int64 v6; // rax

v4 = (int)a3;
v5 = (void *)xmalloc((int)a3);
memcpy(v5, a2, v4);
v6 = gdp_packet_new(a1, v5, a3);
return gdp_io_thread_enqueue_send(v6);
}

original_code:
void gdp_client_send(gdp_client_t* client, char* buffer, int len) {
 char* buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 gdp_packet_t* pack = gdp_packet_new(client, buf_clone, len);
 gdp_io_thread_enqueue_send(pack);
}

canonical_original_code:
void gdp_client_send(struct gdp_client *client, char *buffer, int len) {
 char *buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 struct gdp_packet *pack = gdp_packet_new(client, buf_clone, len);
 gdp_io_thread_enqueue_send(pack);
}

variable_types: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *


decompiled_code:
__int64 gdp_client_recv()
{
return gdp_io_thread_dequeue_recv();
}

canonical_decompiled_code:
__int64 func5()
{
return gdp_io_thread_dequeue_recv();
}

original_code:
gdp_packet_t* gdp_client_recv(gdp_client_t* client) {
 return gdp_io_thread_dequeue_recv();
}

canonical_original_code:
struct gdp_packet *gdp_client_recv(struct gdp_client *client) {
 return gdp_io_thread_dequeue_recv();
}

variable_types:
client
struct gdp_client *

return_type: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *


decompiled_code:
__int64 __fastcall gdp_client_isclosed(__int64 a1)
{
return *(unsigned int *)(a1 + 32);
}

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

original_code:
int gdp_client_isclosed(gdp_client_t* client) {
 return client->_closed;
}

canonical_original_code:
int gdp_client_isclosed(struct gdp_client *client) {
 return client->_closed;
}

return_type: int


decompiled_code:
void __fastcall gdp_client_close(void *ptr)
{
int v1; // ebp
__int64 v2; // rax
int v3; // ebp

v1 = *((_DWORD *)ptr + 8);
if ( v1 )
{
puts("Fechou 2 vezes.");
}
else
{
*(_DWORD *)ptr = 0;
*((_WORD *)ptr + 2) = 0;
*((_BYTE *)ptr + 6) = 0;
*((_DWORD *)ptr + 8) = 1;
if ( *((_QWORD *)ptr + 5) )
gdp_stream_writer_free();
if ( *((_QWORD *)ptr + 6) )
gdp_packet_free();
close(*((_DWORD *)ptr + 2));
while ( (int)gdp_list_count(*((_QWORD *)ptr + 7)) > v1 )
{
v2 = gdp_list_get(*((_QWORD *)ptr + 7), (unsigned int)v1);
(*(void (__fastcall **)(void *, _QWORD))(v2 + 8))(ptr, *(_QWORD *)v2);
++v1;
}
v3 = gdp_list_count(*((_QWORD *)ptr + 7)) - 1;
if ( v3 > 0 )
{
do
gdp_list_remove(*((_QWORD *)ptr + 7), (unsigned int)v3--);
while ( v3 );
}
gdp_list_free(*((_QWORD *)ptr + 7));
gdp_io_thread_remove_client(ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func7(void *ptr)
{
int v1; // ebp
__int64 v2; // rax
int v3; // ebp

v1 = *((_DWORD *)ptr + 8);
if ( v1 )
{
puts("Fechou 2 vezes.");
}
else
{
*(_DWORD *)ptr = 0;
*((_WORD *)ptr + 2) = 0;
*((_BYTE *)ptr + 6) = 0;
*((_DWORD *)ptr + 8) = 1;
if ( *((_QWORD *)ptr + 5) )
gdp_stream_writer_free();
if ( *((_QWORD *)ptr + 6) )
gdp_packet_free();
close(*((_DWORD *)ptr + 2));
while ( (int)gdp_list_count(*((_QWORD *)ptr + 7)) > v1 )
{
v2 = gdp_list_get(*((_QWORD *)ptr + 7), (unsigned int)v1);
(*(void (__fastcall **)(void *, _QWORD))(v2 + 8))(ptr, *(_QWORD *)v2);
++v1;
}
v3 = gdp_list_count(*((_QWORD *)ptr + 7)) - 1;
if ( v3 > 0 )
{
do
gdp_list_remove(*((_QWORD *)ptr + 7), (unsigned int)v3--);
while ( v3 );
}
gdp_list_free(*((_QWORD *)ptr + 7));
gdp_io_thread_remove_client(ptr);
free(ptr);
}
}

original_code:
void gdp_client_close(gdp_client_t* client) {
 if (!client->_closed) {
  memset(client->objsign, 0, sizeof(client->objsign));
  client->_closed = 1;
 } else {
  printf("Fechou 2 vezes.\n");
  return;
 }
 if (client->_prebuffer)
  gdp_stream_writer_free(client->_prebuffer);
 if (client->_prepack)
  gdp_packet_free(client->_prepack);
 close(client->sock);
 int i;
 for (i = 0; i < gdp_list_count(client->_onclose_callbacks); i++) {
  gdp_event_info_t* ei = (gdp_event_info_t*)gdp_list_get(client->_onclose_callbacks, i);
  ei->callback(client, ei->state);
 }
 for (i = gdp_list_count(client->_onclose_callbacks) - 1; i > 0; i--)
  gdp_list_remove(client->_onclose_callbacks, i);
 gdp_list_free(client->_onclose_callbacks);
 gdp_io_thread_remove_client(client);
 free(client); client = ((void *)0);;
}

canonical_original_code:
void gdp_client_close(struct gdp_client *client) {
 if (!client->_closed) {
  memset(client->objsign, 0, sizeof(client->objsign));
  client->_closed = 1;
 } else {
  printf("Fechou 2 vezes.\n");
  return;
 }
 if (client->_prebuffer)
  gdp_stream_writer_free(client->_prebuffer);
 if (client->_prepack)
  gdp_packet_free(client->_prepack);
 close(client->sock);
 int i;
 for (i = 0; i < gdp_list_count(client->_onclose_callbacks); i++) {
  struct gdp_event_info *ei = (gdp_event_info_t*)gdp_list_get(client->_onclose_callbacks, i);
  ei->callback(client, ei->state);
 }
 for (i = gdp_list_count(client->_onclose_callbacks) - 1; i > 0; i--)
  gdp_list_remove(client->_onclose_callbacks, i);
 gdp_list_free(client->_onclose_callbacks);
 gdp_io_thread_remove_client(client);
 free(client); client = ((void *)0);;
}


decompiled_code:
void __fastcall gdp_client_internal_recv(void *ptr)
{
void *v1; // r14
int v2; // eax
__int64 v3; // r12
unsigned int v4; // er13
void *v5; // rsp
void *v6; // r13
unsigned int v7; // eax
int v8; // er13
unsigned int v9; // er13
void *v10; // rsp
unsigned int v11; // eax
void *v12; // rsp
unsigned int v13; // eax
_BYTE v14[48]; // [rsp+0h] [rbp-30h] BYREF

v1 = (void *)xmalloc(512LL);
v2 = recv(*((_DWORD *)ptr + 2), v1, 0x200uLL, 0);
if ( v2 > 0 )
{
v3 = gdp_stream_reader_new(v1, (unsigned int)v2);
while ( (int)gdp_stream_reader_pending(v3) > 0 )
{
if ( *((_DWORD *)ptr + 9) == 1 )
{
v8 = gdp_stream_writer_size(*((_QWORD *)ptr + 5));
if ( (int)(gdp_stream_reader_pending(v3) + v8) > 7 )
{
v4 = 8 - gdp_stream_writer_size(*((_QWORD *)ptr + 5));
v5 = alloca((int)v4);
gdp_stream_reader_read(v3, v14, v4);
gdp_stream_writer_append(*((_QWORD *)ptr + 5), v14, v4);
v6 = (void *)gdp_stream_writer_clone_buffer(*((_QWORD *)ptr + 5));
v7 = gdp_stream_writer_size(*((_QWORD *)ptr + 5));
*((_QWORD *)ptr + 6) = gdp_packet_parse(ptr, v6, v7);
free(v6);
if ( !*((_QWORD *)ptr + 6) )
{
gdp_client_close(ptr);
free(v1);
return;
}
*((_DWORD *)ptr + 9) = 2;
gdp_stream_writer_clear(*((_QWORD *)ptr + 5));
}
else
{
v9 = gdp_stream_reader_pending(v3);
v10 = alloca((int)v9);
gdp_stream_reader_read(v3, v14, v9);
gdp_stream_writer_append(*((_QWORD *)ptr + 5), v14, v9);
}
}
if ( *((_DWORD *)ptr + 9) == 2 )
{
if ( (int)gdp_stream_reader_pending(v3) > 0 )
{
v11 = gdp_packet_pending(*((_QWORD *)ptr + 6));
v12 = alloca((int)v11);
v13 = gdp_stream_reader_read(v3, v14, v11);
gdp_packet_append(*((_QWORD *)ptr + 6), v14, v13);
}
if ( (unsigned int)gdp_packet_is_complete(*((_QWORD *)ptr + 6)) )
{
gdp_io_thread_enqueue_recv(*((_QWORD *)ptr + 6));
*((_QWORD *)ptr + 6) = 0LL;
*((_DWORD *)ptr + 9) = 1;
}
}
}
gdp_stream_reader_free(v3);
free(v1);
}
else
{
gdp_client_close(ptr);
free(v1);
}
}

canonical_decompiled_code:
void __fastcall func8(void *ptr)
{
void *v1; // r14
int v2; // eax
__int64 v3; // r12
unsigned int v4; // er13
void *v5; // rsp
void *v6; // r13
unsigned int v7; // eax
int v8; // er13
unsigned int v9; // er13
void *v10; // rsp
unsigned int v11; // eax
void *v12; // rsp
unsigned int v13; // eax
_BYTE v14[48]; // [rsp+0h] [rbp-30h] BYREF

v1 = (void *)xmalloc(512LL);
v2 = recv(*((_DWORD *)ptr + 2), v1, 0x200uLL, 0);
if ( v2 > 0 )
{
v3 = gdp_stream_reader_new(v1, (unsigned int)v2);
while ( (int)gdp_stream_reader_pending(v3) > 0 )
{
if ( *((_DWORD *)ptr + 9) == 1 )
{
v8 = gdp_stream_writer_size(*((_QWORD *)ptr + 5));
if ( (int)(gdp_stream_reader_pending(v3) + v8) > 7 )
{
v4 = 8 - gdp_stream_writer_size(*((_QWORD *)ptr + 5));
v5 = alloca((int)v4);
gdp_stream_reader_read(v3, v14, v4);
gdp_stream_writer_append(*((_QWORD *)ptr + 5), v14, v4);
v6 = (void *)gdp_stream_writer_clone_buffer(*((_QWORD *)ptr + 5));
v7 = gdp_stream_writer_size(*((_QWORD *)ptr + 5));
*((_QWORD *)ptr + 6) = gdp_packet_parse(ptr, v6, v7);
free(v6);
if ( !*((_QWORD *)ptr + 6) )
{
func7(ptr);
free(v1);
return;
}
*((_DWORD *)ptr + 9) = 2;
gdp_stream_writer_clear(*((_QWORD *)ptr + 5));
}
else
{
v9 = gdp_stream_reader_pending(v3);
v10 = alloca((int)v9);
gdp_stream_reader_read(v3, v14, v9);
gdp_stream_writer_append(*((_QWORD *)ptr + 5), v14, v9);
}
}
if ( *((_DWORD *)ptr + 9) == 2 )
{
if ( (int)gdp_stream_reader_pending(v3) > 0 )
{
v11 = gdp_packet_pending(*((_QWORD *)ptr + 6));
v12 = alloca((int)v11);
v13 = gdp_stream_reader_read(v3, v14, v11);
gdp_packet_append(*((_QWORD *)ptr + 6), v14, v13);
}
if ( (unsigned int)gdp_packet_is_complete(*((_QWORD *)ptr + 6)) )
{
gdp_io_thread_enqueue_recv(*((_QWORD *)ptr + 6));
*((_QWORD *)ptr + 6) = 0LL;
*((_DWORD *)ptr + 9) = 1;
}
}
}
gdp_stream_reader_free(v3);
free(v1);
}
else
{
func7(ptr);
free(v1);
}
}

original_code:
void gdp_client_internal_recv(gdp_client_t* client) {
 char *buffer = (char*)xmalloc(512 * sizeof(char));
 int bytes_read = recv(client->sock, buffer, 512, 0);
 if (bytes_read <= 0) {
  if (bytes_read != 11 && bytes_read != 11) {
   gdp_client_close(client);
   free(buffer); buffer = ((void *)0);;
   return;
  }
 } else {
  gdp_stream_reader_t* reader = gdp_stream_reader_new(buffer, bytes_read);
  while (gdp_stream_reader_pending(reader) > 0) {
   if (client->state == IN_HEADER) {
    int size_read_plus_pend =
     gdp_stream_writer_size(client->_prebuffer) + gdp_stream_reader_pending(reader);
    if (size_read_plus_pend < 8) {
     int pending = gdp_stream_reader_pending(reader);
     char tmp[pending];
     gdp_stream_reader_read(reader, tmp, pending);
     gdp_stream_writer_append(client->_prebuffer, tmp, pending);
    } else {
     int size = 8 - gdp_stream_writer_size(client->_prebuffer);
     char tmp[size];
     gdp_stream_reader_read(reader, tmp, size);
     gdp_stream_writer_append(client->_prebuffer, tmp, size);
     char *cloned_buf = gdp_stream_writer_clone_buffer(client->_prebuffer);
     client->_prepack = gdp_packet_parse(
            client,
            cloned_buf,
            gdp_stream_writer_size(client->_prebuffer));
     free(cloned_buf); cloned_buf = ((void *)0);
     if (client->_prepack != ((void *)0)) {
      client->state = IN_PACKET;
      gdp_stream_writer_clear(client->_prebuffer);
     } else {
      gdp_client_close(client);
      free(buffer); buffer = ((void *)0);;
      return;
     }
    }
   }
   if (client->state == IN_PACKET) {
    if (gdp_stream_reader_pending(reader) > 0) {
     int pending_in_packet = gdp_packet_pending(client->_prepack);
     char tmp[pending_in_packet];
     int bytes_to_write = gdp_stream_reader_read(reader, tmp, pending_in_packet);
     gdp_packet_append(client->_prepack, tmp, bytes_to_write);
    }
    if (gdp_packet_is_complete(client->_prepack)) {
     gdp_io_thread_enqueue_recv(client->_prepack);
     client->_prepack = ((void *)0);
     client->state = IN_HEADER;
    }
   }
  }
  gdp_stream_reader_free(reader);
 }
 free(buffer); buffer = ((void *)0);;
}

canonical_original_code:
void gdp_client_internal_recv(struct gdp_client *client) {
 char *buffer = (char*)xmalloc(512 * sizeof(char));
 int bytes_read = recv(client->sock, buffer, 512, 0);
 if (bytes_read <= 0) {
  if (bytes_read != 11 && bytes_read != 11) {
   gdp_client_close(client);
   free(buffer); buffer = ((void *)0);;
   return;
  }
 } else {
  struct gdp_stream_reader *reader = gdp_stream_reader_new(buffer, bytes_read);
  while (gdp_stream_reader_pending(reader) > 0) {
   if (client->state == IN_HEADER) {
    int size_read_plus_pend = gdp_stream_writer_size(client->_prebuffer) + gdp_stream_reader_pending(reader);
    if (size_read_plus_pend < 8) {
     int pending = gdp_stream_reader_pending(reader);
     char tmp[-1];
     gdp_stream_reader_read(reader, tmp, pending);
     gdp_stream_writer_append(client->_prebuffer, tmp, pending);
    } else {
     int size = 8 - gdp_stream_writer_size(client->_prebuffer);
     char tmp[-1];
     gdp_stream_reader_read(reader, tmp, size);
     gdp_stream_writer_append(client->_prebuffer, tmp, size);
     char *cloned_buf = gdp_stream_writer_clone_buffer(client->_prebuffer);
     client->_prepack = gdp_packet_parse(
            client,
            cloned_buf,
            gdp_stream_writer_size(client->_prebuffer));
     free(cloned_buf); cloned_buf = ((void *)0);
     if (client->_prepack != ((void *)0)) {
      client->state = IN_PACKET;
      gdp_stream_writer_clear(client->_prebuffer);
     } else {
      gdp_client_close(client);
      free(buffer); buffer = ((void *)0);;
      return;
     }
    }
   }
   if (client->state == IN_PACKET) {
    if (gdp_stream_reader_pending(reader) > 0) {
     int pending_in_packet = gdp_packet_pending(client->_prepack);
     char tmp[-1];
     int bytes_to_write = gdp_stream_reader_read(reader, tmp, pending_in_packet);
     gdp_packet_append(client->_prepack, tmp, bytes_to_write);
    }
    if (gdp_packet_is_complete(client->_prepack)) {
     gdp_io_thread_enqueue_recv(client->_prepack);
     client->_prepack = ((void *)0);
     client->state = IN_HEADER;
    }
   }
  }
  gdp_stream_reader_free(reader);
 }
 free(buffer); buffer = ((void *)0);;
}

variable_types:
buffer
char *

variable_types: struct gdp_stream_reader { char *buffer; int index; int buffer_length; } *

variable_types: pending int

variable_types:
tmp
char [-1]

variable_types: size int


decompiled_code:
__int64 __fastcall gdp_client_connect(char *cp, int a2)
{
struct hostent *v3; // rax
__int64 v4; // rdx
char **v5; // rax
char *v6; // rdx
_QWORD **v7; // rax
int v8; // eax
int v9; // ebx
char *v10; // rsi
__int64 v11; // rdx
__int64 v12; // rcx
__int64 v13; // r8
__int64 v14; // r9
int *v16; // rax
char optval; // [rsp+2Fh] [rbp-49h] BYREF
char dest[16]; // [rsp+30h] [rbp-48h] BYREF
int v19; // [rsp+40h] [rbp-38h]
struct sockaddr addr; // [rsp+50h] [rbp-28h] BYREF

gdp_io_thread_assure_wsa();
if ( ((*_ctype_b_loc())[*cp] & 0x400) != 0 )
{
v3 = gethostbyname(cp);
}
else
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
return 0LL;
v3 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
}
if ( v3 )
{
v4 = 0LL;
if ( v3->h_addrtype != 2 )
return v4;
v5 = v3->h_addr_list;
v6 = *v5;
if ( *v5 )
{
v7 = (_QWORD **)(v5 + 1);
do
{
*(_DWORD *)&addr.sa_data[2] = *(_QWORD *)v6;
v6 = (char *)*v7++;
}
while ( v6 );
}
}
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a2);
v8 = socket(2, 1, 6);
v9 = v8;
if ( v8 == -1 )
{
v16 = _errno_location();
printf("Error at socket(): %d\n", (unsigned int)*v16);
v4 = 0LL;
}
else if ( connect(v8, &addr, 0x10u) )
{
close(v9);
v4 = 0LL;
}
else
{
optval = 1;
ioctl(v9, 0x5421uLL, &optval);
setsockopt(v9, 6, 1, &optval, 1u);
v10 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(dest, v10);
v19 = a2;
v4 = gdp_client_new(v9, (__int64)v10, v11, v12, v13, v14, *(_OWORD *)&_mm_loadu_si128((const __m128i *)dest), a2);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *cp, int a2)
{
struct hostent *v3; // rax
__int64 v4; // rdx
char **v5; // rax
char *v6; // rdx
_QWORD **v7; // rax
int v8; // eax
int v9; // ebx
char *v10; // rsi
__int64 v11; // rdx
__int64 v12; // rcx
__int64 v13; // r8
__int64 v14; // r9
int *v16; // rax
char optval; // [rsp+2Fh] [rbp-49h] BYREF
char dest[16]; // [rsp+30h] [rbp-48h] BYREF
int v19; // [rsp+40h] [rbp-38h]
struct sockaddr addr; // [rsp+50h] [rbp-28h] BYREF

gdp_io_thread_assure_wsa();
if ( ((*_ctype_b_loc())[*cp] & 0x400) != 0 )
{
v3 = gethostbyname(cp);
}
else
{
*(_DWORD *)&addr.sa_data[2] = inet_addr(cp);
if ( *(_DWORD *)&addr.sa_data[2] == -1 )
return 0LL;
v3 = gethostbyaddr(&addr.sa_data[2], 4u, 2);
}
if ( v3 )
{
v4 = 0LL;
if ( v3->h_addrtype != 2 )
return v4;
v5 = v3->h_addr_list;
v6 = *v5;
if ( *v5 )
{
v7 = (_QWORD **)(v5 + 1);
do
{
*(_DWORD *)&addr.sa_data[2] = *(_QWORD *)v6;
v6 = (char *)*v7++;
}
while ( v6 );
}
}
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a2);
v8 = socket(2, 1, 6);
v9 = v8;
if ( v8 == -1 )
{
v16 = _errno_location();
printf("Error at socket(): %d\n", (unsigned int)*v16);
v4 = 0LL;
}
else if ( connect(v8, &addr, 0x10u) )
{
close(v9);
v4 = 0LL;
}
else
{
optval = 1;
ioctl(v9, 0x5421uLL, &optval);
setsockopt(v9, 6, 1, &optval, 1u);
v10 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
strcpy(dest, v10);
v19 = a2;
v4 = func1(v9, (__int64)v10, v11, v12, v13, v14, *(_OWORD *)&_mm_loadu_si128((const __m128i *)dest), a2);
}
return v4;
}

original_code:
gdp_client_t* gdp_client_connect(const char* host, int port) {
 gdp_io_thread_assure_wsa();
 struct hostent *remote_host;
 struct sockaddr_in sockaddr;
 if (((*__ctype_b_loc ())[(int) ((*host))] & (unsigned short int) _ISalpha)) {
  remote_host = gethostbyname(host);
 } else {
  sockaddr.sin_addr.s_addr = inet_addr(host);
  if (sockaddr.sin_addr.s_addr == ((in_addr_t) 0xffffffff)) {
   return ((void *)0);
  } else {
   remote_host = gethostbyaddr((char *) &(sockaddr.sin_addr), 4, 2);
  }
 }
 if (remote_host != ((void *)0)) {
  if (remote_host->h_addrtype == 2) {
   int i = 0;
   while (remote_host->h_addr_list[i] != 0) {
    sockaddr.sin_addr.s_addr = *(u_long *) remote_host->h_addr_list[i++];
   }
  } else {
   return ((void *)0);
  }
 }
 sockaddr.sin_family = 2;
 sockaddr.sin_port = htons(port);
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (sock == -1) {
  printf("Error at socket(): %d\n", (*__errno_location ()));
  return ((void *)0);
 }
 if (!connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
     char on = 1;
     ioctl(sock, 0x5421, (char *)&on);
     setsockopt(sock, IPPROTO_TCP, 1, &on, sizeof on);
  gdp_addr_t addr;
  strcpy(addr.ip, inet_ntoa(sockaddr.sin_addr));
  addr.port = port;
  return gdp_client_new(sock, addr);
 }
 close(sock);
 return ((void *)0);
}

canonical_original_code:
struct gdp_client *gdp_client_connect(char *host, int port) {
 gdp_io_thread_assure_wsa();
 struct hostent *remote_host;
 struct sockaddr_in sockaddr;
 if (((*__ctype_b_loc ())[(int) ((*host))] & (unsigned short) _ISalpha)) {
  remote_host = gethostbyname(host);
 } else {
  sockaddr.sin_addr.s_addr = inet_addr(host);
  if (sockaddr.sin_addr.s_addr == ((unsigned int) 0xffffffff)) {
   return ((void *)0);
  } else {
   remote_host = gethostbyaddr((char *) &(sockaddr.sin_addr), 4, 2);
  }
 }
 if (remote_host != ((void *)0)) {
  if (remote_host->h_addrtype == 2) {
   int i = 0;
   while (remote_host->h_addr_list[i] != 0) {
    sockaddr.sin_addr.s_addr = *(unsigned long *) remote_host->h_addr_list[i++];
   }
  } else {
   return ((void *)0);
  }
 }
 sockaddr.sin_family = 2;
 sockaddr.sin_port = htons(port);
 int sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (sock == -1) {
  printf("Error at socket(): %d\n", (*__errno_location ()));
  return ((void *)0);
 }
 if (!connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
     char on = 1;
     ioctl(sock, 0x5421, (char *)&on);
     setsockopt(sock, IPPROTO_TCP, 1, &on, sizeof on);
  struct gdp_addr addr;
  strcpy(addr.ip, inet_ntoa(sockaddr.sin_addr));
  addr.port = port;
  return gdp_client_new(sock, addr);
 }
 close(sock);
 return ((void *)0);
}

variable_types:
host
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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

variable_types: i int

variable_types: struct gdp_addr { char ip[15]; int port; }


decompiled_code:
__int64 gdp_list_new()
{
__int64 v0; // rbx

v0 = xmalloc0(56LL);
*(_DWORD *)(v0 + 4) = 8;
*(_QWORD *)(v0 + 8) = xmalloc(64LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx

v0 = xmalloc0(56LL);
*(_DWORD *)(v0 + 4) = 8;
*(_QWORD *)(v0 + 8) = xmalloc(64LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
return v0;
}

original_code:
gdp_list_t* gdp_list_new()
{
 gdp_list_t *ls = (gdp_list_t*)xmalloc0(sizeof(gdp_list_t));
 ls->available = 8;
 ls->items = (void**)xmalloc(ls->available * sizeof(void*));
 pthread_mutex_init(&(ls->lock_obj), ((void *)0));
 return ls;
}

canonical_original_code:
struct gdp_list *gdp_list_new()
{
 struct gdp_list *ls = (gdp_list_t*)xmalloc0(sizeof(gdp_list_t));
 ls->available = 8;
 ls->items = (void **)xmalloc(ls->available * sizeof(void *));
 pthread_mutex_init(&(ls->lock_obj), ((void *)0));
 return ls;
}

variable_types: struct gdp_list { int count; int available; void **items; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *


original_code:
void gdp_list_add(gdp_list_t* list, void* item)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (list->count + 1 > list->available)
 {
  void **newbuf;
  int i;
  list->available *= 2;
  newbuf = (void**)xmalloc(list->available * sizeof(void*));
  for (i = 0; i < list->count; i++)
   newbuf[i] = list->items[i];
  free(list->items); list->items = ((void *)0);;
  list->items = newbuf;
 }
 list->items[list->count] = item;
 list->count++;
 pthread_mutex_unlock(&(list->lock_obj));
}

canonical_original_code:
void gdp_list_add(struct gdp_list *list, void *item)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (list->count + 1 > list->available)
 {
  void **newbuf;
  int i;
  list->available *= 2;
  newbuf = (void **)xmalloc(list->available * sizeof(void *));
  for (i = 0; i < list->count; i++)
   newbuf[i] = list->items[i];
  free(list->items); list->items = ((void *)0);;
  list->items = newbuf;
 }
 list->items[list->count] = item;
 list->count++;
 pthread_mutex_unlock(&(list->lock_obj));
}

variable_types:
item
void *

variable_types:
newbuf
void **


decompiled_code:
__int64 __fastcall gdp_list_get(__int64 a1, int a2)
{
__int64 v2; // r13

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v2 = 0LL;
if ( *(_DWORD *)a1 > a2 )
v2 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // r13

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v2 = 0LL;
if ( *(_DWORD *)a1 > a2 )
v2 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v2;
}

original_code:
void* gdp_list_get(gdp_list_t* list, int index)
{
 void* item = ((void *)0);
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
  item = list->items[index];
 pthread_mutex_unlock(&(list->lock_obj));
 return item;
}

canonical_original_code:
void *gdp_list_get(struct gdp_list *list, int index)
{
 void *item = ((void *)0);
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
  item = list->items[index];
 pthread_mutex_unlock(&(list->lock_obj));
 return item;
}

variable_types: index int

return_type: void *


decompiled_code:
int __fastcall gdp_list_set(__int64 a1, int a2, __int64 a3)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_DWORD *)a1 > a2 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2) = a3;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, int a2, __int64 a3)
{
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_DWORD *)a1 > a2 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * a2) = a3;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void gdp_list_set(gdp_list_t* list, int index, void* item)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
  list->items[index] = item;
 pthread_mutex_unlock(&(list->lock_obj));
}

canonical_original_code:
void gdp_list_set(struct gdp_list *list, int index, void *item)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
  list->items[index] = item;
 pthread_mutex_unlock(&(list->lock_obj));
}


original_code:
void gdp_list_remove(gdp_list_t* list, int index)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
 {
  if (index + 1 < list->count)
  {
   int i;
   for (i = index; i < list->count; i++)
   {
    list->items[i] = list->items[i+1];
   }
  }
  list->count--;
 }
 if (list->available > 8)
 {
  if (list->available / 2 > list->count)
  {
   void** newbuf;
   int i;
   newbuf = (void**)xmalloc(list->available / 2 * sizeof(void*));
   for (i = 0; i < list->count; i++)
    newbuf[i] = list->items[i];
   free(list->items); list->items = ((void *)0);;
   list->items = newbuf;
  }
 }
 pthread_mutex_unlock(&(list->lock_obj));
}

canonical_original_code:
void gdp_list_remove(struct gdp_list *list, int index)
{
 pthread_mutex_lock(&(list->lock_obj));
 if (index < list->count)
 {
  if (index + 1 < list->count)
  {
   int i;
   for (i = index; i < list->count; i++)
   {
    list->items[i] = list->items[i+1];
   }
  }
  list->count--;
 }
 if (list->available > 8)
 {
  if (list->available / 2 > list->count)
  {
   void **newbuf;
   int i;
   newbuf = (void **)xmalloc(list->available / 2 * sizeof(void *));
   for (i = 0; i < list->count; i++)
    newbuf[i] = list->items[i];
   free(list->items); list->items = ((void *)0);;
   list->items = newbuf;
  }
 }
 pthread_mutex_unlock(&(list->lock_obj));
}


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

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

original_code:
int gdp_list_count(gdp_list_t* list)
{
 return list->count;
}

canonical_original_code:
int gdp_list_count(struct gdp_list *list)
{
 return list->count;
}


decompiled_code:
int __fastcall gdp_list_foreach(__int64 a1, void (__fastcall *a2)(_QWORD))
{
__int64 v2; // rbx

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 > 0 )
{
v2 = 0LL;
do
a2(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2++));
while ( *(_DWORD *)a1 > (int)v2 );
}
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, void (__fastcall *a2)(_QWORD))
{
__int64 v2; // rbx

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 > 0 )
{
v2 = 0LL;
do
a2(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v2++));
while ( *(_DWORD *)a1 > (int)v2 );
}
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void gdp_list_foreach(gdp_list_t* list, void (*eachfn)(void *))
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  eachfn(list->items[i]);
 }
 pthread_mutex_unlock(&(list->lock_obj));
}

canonical_original_code:
void gdp_list_foreach(struct gdp_list *list, void (*eachfn)(void *))
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  eachfn(list->items[i]);
 }
 pthread_mutex_unlock(&(list->lock_obj));
}

variable_types:
eachfn
void (*)(void *)


decompiled_code:
__int64 __fastcall gdp_list_indexof(__int64 a1, __int64 a2)
{
pthread_mutex_t *v2; // r12
__int64 v3; // rax
unsigned int v4; // ebp

v2 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
LABEL_5:
pthread_mutex_unlock(v2);
v4 = -1;
}
else
{
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v3) == a2 )
break;
if ( ++v3 == *(_DWORD *)a1 )
goto LABEL_5;
}
pthread_mutex_unlock(v2);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
pthread_mutex_t *v2; // r12
__int64 v3; // rax
unsigned int v4; // ebp

v2 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 <= 0 )
{
LABEL_5:
pthread_mutex_unlock(v2);
v4 = -1;
}
else
{
v3 = 0LL;
while ( 1 )
{
v4 = v3;
if ( *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v3) == a2 )
break;
if ( ++v3 == *(_DWORD *)a1 )
goto LABEL_5;
}
pthread_mutex_unlock(v2);
}
return v4;
}

original_code:
int gdp_list_indexof(gdp_list_t* list, void* item)
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  if (list->items[i] == item) {
   pthread_mutex_unlock(&(list->lock_obj));
   return i;
  }
 }
 pthread_mutex_unlock(&(list->lock_obj));
 return -1;
}

canonical_original_code:
int gdp_list_indexof(struct gdp_list *list, void *item)
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  if (list->items[i] == item) {
   pthread_mutex_unlock(&(list->lock_obj));
   return i;
  }
 }
 pthread_mutex_unlock(&(list->lock_obj));
 return -1;
}


decompiled_code:
int __fastcall gdp_list_remove_item(__int64 a1, __int64 a2)
{
int result; // eax

result = gdp_list_indexof(a1, a2);
if ( result >= 0 )
result = gdp_list_remove(a1, result);
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, __int64 a2)
{
int result; // eax

result = func7(a1, a2);
if ( result >= 0 )
result = func4(a1, result);
return result;
}

original_code:
void gdp_list_remove_item(gdp_list_t* list, void* item) {
 int i = gdp_list_indexof(list, item);
 if (i > -1) {
  gdp_list_remove(list, i);
 }
}

canonical_original_code:
void gdp_list_remove_item(struct gdp_list *list, void *item) {
 int i = gdp_list_indexof(list, item);
 if (i > -1) {
  gdp_list_remove(list, i);
 }
}


decompiled_code:
int __fastcall gdp_list_foreach_arg(__int64 a1, void (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
__int64 v4; // rbx

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 > 0 )
{
v4 = 0LL;
do
a2(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4++), a3);
while ( *(_DWORD *)a1 > (int)v4 );
}
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

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

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(int *)a1 > 0 )
{
v4 = 0LL;
do
a2(*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8 * v4++), a3);
while ( *(_DWORD *)a1 > (int)v4 );
}
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void gdp_list_foreach_arg(gdp_list_t* list, void (*eachfn)(void *, void *), void* arg)
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  eachfn(list->items[i], arg);
 }
 pthread_mutex_unlock(&(list->lock_obj));
}

canonical_original_code:
void gdp_list_foreach_arg(struct gdp_list *list, void (*eachfn)(void *, void *), void *arg)
{
 int i;
 pthread_mutex_lock(&(list->lock_obj));
 for (i = 0; i < list->count; i++)
 {
  eachfn(list->items[i], arg);
 }
 pthread_mutex_unlock(&(list->lock_obj));
}

variable_types:
eachfn
void (*)(void *, void *)

variable_types:
arg
void *


decompiled_code:
void __fastcall gdp_list_free(void *ptr)
{
int v1; // eax

while ( 1 )
{
v1 = gdp_list_count((unsigned int *)ptr);
if ( v1 <= 0 )
break;
gdp_list_remove((__int64)ptr, v1 - 1);
}
free(*((void **)ptr + 1));
*((_QWORD *)ptr + 1) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
int v1; // eax

while ( 1 )
{
v1 = func5((unsigned int *)ptr);
if ( v1 <= 0 )
break;
func4((__int64)ptr, v1 - 1);
}
free(*((void **)ptr + 1));
*((_QWORD *)ptr + 1) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

original_code:
void gdp_list_free(gdp_list_t* list) {
 int i;
 pthread_mutex_t mutexzero = { { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } };
 while ((i = gdp_list_count(list)) > 0) {
  gdp_list_remove(list, i-1);
 }
 free(list->items); list->items = ((void *)0);;
 list->items = ((void *)0);
 pthread_mutex_destroy(&(list->lock_obj));
 list->lock_obj = mutexzero;
 free(list); list = ((void *)0);;
}

canonical_original_code:
void gdp_list_free(struct gdp_list *list) {
 int i;
 union pthread_mutex_t mutexzero = { { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } };
 while ((i = gdp_list_count(list)) > 0) {
  gdp_list_remove(list, i-1);
 }
 free(list->items); list->items = ((void *)0);;
 list->items = ((void *)0);
 pthread_mutex_destroy(&(list->lock_obj));
 list->lock_obj = mutexzero;
 free(list); list = ((void *)0);;
}

variable_types: union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; }


decompiled_code:
__int64 gdp_queue_new()
{
__int64 v0; // rbx

v0 = xmalloc0(64LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 24), 0LL);
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbx

v0 = xmalloc0(64LL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 24), 0LL);
return v0;
}

original_code:
void* gdp_queue_new() {
 gdp_queue_t* q = (gdp_queue_t*)xmalloc0(sizeof(gdp_queue_t));
    pthread_mutex_init(&(q->lock_obj), ((void *)0));
    return q;
}

canonical_original_code:
void *gdp_queue_new() {
 struct gdp_queue *q = (gdp_queue_t*)xmalloc0(sizeof(gdp_queue_t));
    pthread_mutex_init(&(q->lock_obj), ((void *)0));
    return q;
}

variable_types: struct gdp_queue { int len; struct gdp_node { struct gdp_node *next; void *value; } *head; struct gdp_node *tail; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *

user_defined_types: struct gdp_queue { int len; struct gdp_node *head; struct gdp_node *tail; union pthread_mutex_t lock_obj; }


decompiled_code:
int __fastcall gdp_queue_enq(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = (_QWORD *)xmalloc(16LL);
*v2 = 0LL;
v2[1] = a2;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( *(int *)a1 <= 0 )
*(_QWORD *)(a1 + 16) = v2;
else
**(_QWORD **)(a1 + 8) = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)a1;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = (_QWORD *)xmalloc(16LL);
*v2 = 0LL;
v2[1] = a2;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 24));
if ( *(int *)a1 <= 0 )
*(_QWORD *)(a1 + 16) = v2;
else
**(_QWORD **)(a1 + 8) = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)a1;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 24));
}

original_code:
void gdp_queue_enq(void* queue, void* value) {
    gdp_queue_t* q = (gdp_queue_t*)queue;
    gdp_node_t* newnode = (gdp_node_t*)xmalloc(sizeof(gdp_node_t));
    newnode->next = ((void *)0);
    newnode->value = value;
    pthread_mutex_lock(&(q->lock_obj));
    if (q->len > 0) {
        q->head->next = newnode;
    } else {
        q->tail = newnode;
    }
    q->head = newnode;
    q->len++;
    pthread_mutex_unlock(&(q->lock_obj));
}

canonical_original_code:
void gdp_queue_enq(void *queue, void *value) {
    struct gdp_queue *q = (gdp_queue_t*)queue;
    struct gdp_node *newnode = (gdp_node_t*)xmalloc(sizeof(gdp_node_t));
    newnode->next = ((void *)0);
    newnode->value = value;
    pthread_mutex_lock(&(q->lock_obj));
    if (q->len > 0) {
        q->head->next = newnode;
    } else {
        q->tail = newnode;
    }
    q->head = newnode;
    q->len++;
    pthread_mutex_unlock(&(q->lock_obj));
}

variable_types:
queue
void *

variable_types:
value
void *

variable_types: struct gdp_node { struct gdp_node *next; void *value; } *


decompiled_code:
__int64 __fastcall gdp_queue_deq(int *a1)
{
pthread_mutex_t *v1; // r12
int v2; // eax
_QWORD *v3; // rbp
__int64 v4; // r13

v1 = (pthread_mutex_t *)(a1 + 6);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 6));
v2 = *a1;
if ( *a1 )
{
v3 = (_QWORD *)*((_QWORD *)a1 + 2);
*((_QWORD *)a1 + 2) = *v3;
v4 = v3[1];
*a1 = v2 - 1;
pthread_mutex_unlock(v1);
free(v3);
}
else
{
pthread_mutex_unlock(v1);
v4 = 0LL;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1)
{
pthread_mutex_t *v1; // r12
int v2; // eax
_QWORD *v3; // rbp
__int64 v4; // r13

v1 = (pthread_mutex_t *)(a1 + 6);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 6));
v2 = *a1;
if ( *a1 )
{
v3 = (_QWORD *)*((_QWORD *)a1 + 2);
*((_QWORD *)a1 + 2) = *v3;
v4 = v3[1];
*a1 = v2 - 1;
pthread_mutex_unlock(v1);
free(v3);
}
else
{
pthread_mutex_unlock(v1);
v4 = 0LL;
}
return v4;
}

original_code:
void* gdp_queue_deq(void* queue) {
    gdp_queue_t* q = (gdp_queue_t*)queue;
    pthread_mutex_lock(&(q->lock_obj));
    if (q->len == 0) {
        pthread_mutex_unlock(&(q->lock_obj));
        return ((void *)0);
    }
    gdp_node_t* node = q->tail;
    q->tail = node->next;
    void* value = node->value;
    q->len--;
    pthread_mutex_unlock(&(q->lock_obj));
    free(node); node = ((void *)0);;
    return value;
}

canonical_original_code:
void *gdp_queue_deq(void *queue) {
    struct gdp_queue *q = (gdp_queue_t*)queue;
    pthread_mutex_lock(&(q->lock_obj));
    if (q->len == 0) {
        pthread_mutex_unlock(&(q->lock_obj));
        return ((void *)0);
    }
    struct gdp_node *node = q->tail;
    q->tail = node->next;
    void *value = node->value;
    q->len--;
    pthread_mutex_unlock(&(q->lock_obj));
    free(node); node = ((void *)0);;
    return value;
}

user_defined_types: struct gdp_node { struct gdp_node *next; void *value; }


decompiled_code:
void __fastcall gdp_queue_destroy(void *ptr)
{
while ( *(int *)ptr > 0 )
gdp_queue_deq((int *)ptr);
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 24));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
while ( *(int *)ptr > 0 )
func2((int *)ptr);
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 24));
free(ptr);
}

original_code:
void gdp_queue_destroy(void* queue) {
    gdp_queue_t* q = (gdp_queue_t*)queue;
 while (q->len > 0) {
  gdp_queue_deq(queue);
 }
 pthread_mutex_destroy(&(q->lock_obj));
 free(queue); queue = ((void *)0);;
}

canonical_original_code:
void gdp_queue_destroy(void *queue) {
    struct gdp_queue *q = (gdp_queue_t*)queue;
 while (q->len > 0) {
  gdp_queue_deq(queue);
 }
 pthread_mutex_destroy(&(q->lock_obj));
 free(queue); queue = ((void *)0);;
}


decompiled_code:
void __fastcall gdp_pop(_QWORD *a1)
{
_QWORD *v1; // rcx
_QWORD *v3; // rdi
_QWORD *v4; // rbx

v1 = (_QWORD *)*a1;
if ( *a1 )
{
v3 = (_QWORD *)*a1;
v4 = 0LL;
while ( v3[1] )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
}
if ( v1 == v3 )
*a1 = 0LL;
free(v3);
if ( v4 )
v4[1] = 0LL;
}
}

original_code:
void gdp_pop(GDPilha **paramPilha){
     GDPilha *Aux,*Ant = ((void *)0);
     if(*paramPilha != ((void *)0))
     {
       Aux = *paramPilha;
        while(Aux->oProximo != ((void *)0))
        {
            Ant = Aux;
            Aux = Aux->oProximo;
        }
        if (Aux == *paramPilha)
            *paramPilha = ((void *)0);
        free(Aux);
        if (Ant != ((void *)0))
            Ant->oProximo = ((void *)0);
    }
}

canonical_original_code:
void gdp_pop(struct _GDPilha **paramPilha){
     struct _GDPilha *Aux; struct _GDPilha *Ant = ((void *)0);
     if(*paramPilha != ((void *)0))
     {
       Aux = *paramPilha;
        while(Aux->oProximo != ((void *)0))
        {
            Ant = Aux;
            Aux = Aux->oProximo;
        }
        if (Aux == *paramPilha)
            *paramPilha = ((void *)0);
        free(Aux);
        if (Ant != ((void *)0))
            Ant->oProximo = ((void *)0);
    }
}

variable_types: struct _GDPilha { struct _Sprite { int w; int h; int ix; int iy; int last; int first; } *oObject; struct _GDPilha *oProximo; } **

user_defined_types: struct _GDPilha { struct _Sprite *oObject; struct _GDPilha *oProximo; }


decompiled_code:
__int64 __fastcall gdp_stacksize(__int64 *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
++v2;
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 *a1)
{
__int64 v1; // rax
unsigned int v2; // edx

v1 = *a1;
if ( !*a1 )
return 0;
v2 = 0;
do
{
++v2;
v1 = *(_QWORD *)(v1 + 8);
}
while ( v1 );
return v2;
}

original_code:
int gdp_stacksize(GDPilha **paramPilha)
{
  int nRetorno = 0;
  GDPilha *Aux;
  Aux = *paramPilha;
  while(Aux != ((void *)0))
  {
     nRetorno++;
     Aux = Aux->oProximo;
  }
  return nRetorno;
}

canonical_original_code:
int gdp_stacksize(struct _GDPilha **paramPilha)
{
  int nRetorno = 0;
  struct _GDPilha *Aux;
  Aux = *paramPilha;
  while(Aux != ((void *)0))
  {
     nRetorno++;
     Aux = Aux->oProximo;
  }
  return nRetorno;
}

variable_types: nRetorno int

variable_types: struct _GDPilha { struct _Sprite { int w; int h; int ix; int iy; int last; int first; } *oObject; struct _GDPilha *oProximo; } *


decompiled_code:
char *gdp_stack_new()
{
char *v0; // rbx

v0 = (char *)malloc(0x38uLL);
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 1) = 0LL;
*((_QWORD *)v0 + 2) = 0LL;
*((_QWORD *)v0 + 3) = 0LL;
*((_QWORD *)v0 + 4) = 0LL;
*((_QWORD *)v0 + 5) = 0LL;
*((_QWORD *)v0 + 6) = 0LL;
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
return v0;
}

original_code:
void* gdp_stack_new() {
    gdp_stack_t* s = (gdp_stack_t*)malloc(sizeof(gdp_stack_t));
    memset(s, 0, sizeof(gdp_stack_t));
    pthread_mutex_init(&(s->lock_obj), ((void *)0));
    return s;
}

canonical_original_code:
void *gdp_stack_new() {
    struct gdp_stack *s = (gdp_stack_t*)malloc(sizeof(gdp_stack_t));
    memset(s, 0, sizeof(gdp_stack_t));
    pthread_mutex_init(&(s->lock_obj), ((void *)0));
    return s;
}

variable_types: struct gdp_stack { int len; struct gdp_node { struct gdp_node *next; void *value; } *top; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock_obj; } *

user_defined_types: struct gdp_stack { int len; struct gdp_node *top; union pthread_mutex_t lock_obj; }


decompiled_code:
int __fastcall gdp_stack_push(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = malloc(0x10uLL);
v2[1] = a2;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
*v2 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)a1;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp

v2 = malloc(0x10uLL);
v2[1] = a2;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
*v2 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)a1;
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void gdp_stack_push(void* stack, void* value) {
    gdp_stack_t* s = (gdp_stack_t*)stack;
    gdp_node_t* node = (gdp_node_t*)malloc(sizeof(gdp_node_t));
    node->value = value;
    pthread_mutex_lock(&(s->lock_obj));
    node->next = s->top;
    s->top = node;
    s->len++;
    pthread_mutex_unlock(&(s->lock_obj));
}

canonical_original_code:
void gdp_stack_push(void *stack, void *value) {
    struct gdp_stack *s = (gdp_stack_t*)stack;
    struct gdp_node *node = (gdp_node_t*)malloc(sizeof(gdp_node_t));
    node->value = value;
    pthread_mutex_lock(&(s->lock_obj));
    node->next = s->top;
    s->top = node;
    s->len++;
    pthread_mutex_unlock(&(s->lock_obj));
}

variable_types:
stack
void *


decompiled_code:
__int64 __fastcall gdp_stack_pop(int *a1)
{
pthread_mutex_t *v1; // r12
int v2; // eax
_QWORD *v3; // rbp
__int64 v4; // r13

v1 = (pthread_mutex_t *)(a1 + 4);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 4));
v2 = *a1;
if ( *a1 )
{
v3 = (_QWORD *)*((_QWORD *)a1 + 1);
v4 = v3[1];
*((_QWORD *)a1 + 1) = *v3;
*a1 = v2 - 1;
pthread_mutex_unlock(v1);
free(v3);
}
else
{
pthread_mutex_unlock(v1);
v4 = 0LL;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1)
{
pthread_mutex_t *v1; // r12
int v2; // eax
_QWORD *v3; // rbp
__int64 v4; // r13

v1 = (pthread_mutex_t *)(a1 + 4);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 4));
v2 = *a1;
if ( *a1 )
{
v3 = (_QWORD *)*((_QWORD *)a1 + 1);
v4 = v3[1];
*((_QWORD *)a1 + 1) = *v3;
*a1 = v2 - 1;
pthread_mutex_unlock(v1);
free(v3);
}
else
{
pthread_mutex_unlock(v1);
v4 = 0LL;
}
return v4;
}

original_code:
void* gdp_stack_pop(void* stack) {
    gdp_stack_t* s = (gdp_stack_t*)stack;
    pthread_mutex_lock(&(s->lock_obj));
    if (s->len == 0) {
        pthread_mutex_unlock(&(s->lock_obj));
        return ((void *)0);
    }
    gdp_node_t* node = s->top;
    void* value = node->value;
    s->top = node->next;
    s->len--;
    pthread_mutex_unlock(&(s->lock_obj));
    free(node);
    return value;
}

canonical_original_code:
void *gdp_stack_pop(void *stack) {
    struct gdp_stack *s = (gdp_stack_t*)stack;
    pthread_mutex_lock(&(s->lock_obj));
    if (s->len == 0) {
        pthread_mutex_unlock(&(s->lock_obj));
        return ((void *)0);
    }
    struct gdp_node *node = s->top;
    void *value = node->value;
    s->top = node->next;
    s->len--;
    pthread_mutex_unlock(&(s->lock_obj));
    free(node);
    return value;
}


decompiled_code:
void __fastcall gdp_stream_writer_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void gdp_stream_writer_free(gdp_stream_writer_t* writer) {
 free(writer->buffer); writer->buffer = ((void *)0);;
 free(writer); writer = ((void *)0);;
}

canonical_original_code:
void gdp_stream_writer_free(struct gdp_stream_writer *writer) {
 free(writer->buffer); writer->buffer = ((void *)0);;
 free(writer); writer = ((void *)0);;
}

variable_types: struct gdp_stream_writer { char *buffer; int maxsize; int size; } *


decompiled_code:
__int64 __fastcall gdp_stream_writer_append(__int64 a1, const void *a2, unsigned int a3)
{
unsigned int v4; // ebx
int v5; // edi

v4 = a3;
v5 = *(_DWORD *)(a1 + 12);
if ( (signed int)(v5 + a3) > *(_DWORD *)(a1 + 8) )
v4 = *(_DWORD *)(a1 + 8) - v5;
memcpy((void *)(*(_QWORD *)a1 + v5), a2, (int)v4);
*(_DWORD *)(a1 + 12) += v4;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const void *a2, unsigned int a3)
{
unsigned int v4; // ebx
int v5; // edi

v4 = a3;
v5 = *(_DWORD *)(a1 + 12);
if ( (signed int)(v5 + a3) > *(_DWORD *)(a1 + 8) )
v4 = *(_DWORD *)(a1 + 8) - v5;
memcpy((void *)(*(_QWORD *)a1 + v5), a2, (int)v4);
*(_DWORD *)(a1 + 12) += v4;
return v4;
}

original_code:
int gdp_stream_writer_append(gdp_stream_writer_t* writer, const char* buffer, int len) {
 if (writer->size + len > writer->maxsize) {
  len = writer->maxsize - writer->size;
 }
 memcpy(writer->buffer + writer->size, buffer, len);
 writer->size += len;
 return len;
}

canonical_original_code:
int gdp_stream_writer_append(struct gdp_stream_writer *writer, char *buffer, int len) {
 if (writer->size + len > writer->maxsize) {
  len = writer->maxsize - writer->size;
 }
 memcpy(writer->buffer + writer->size, buffer, len);
 writer->size += len;
 return len;
}

variable_types:
buffer
char *

variable_types: len int


decompiled_code:
__int64 __fastcall gdp_stream_writer_buffer(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
char* gdp_stream_writer_buffer(gdp_stream_writer_t* writer) {
 return writer->buffer;
}

canonical_original_code:
char *gdp_stream_writer_buffer(struct gdp_stream_writer *writer) {
 return writer->buffer;
}

return_type: char *


decompiled_code:
__int64 __fastcall gdp_stream_writer_size(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
return *(unsigned int *)(a1 + 12);
}

original_code:
int gdp_stream_writer_size(gdp_stream_writer_t* writer) {
 return writer->size;
}

canonical_original_code:
int gdp_stream_writer_size(struct gdp_stream_writer *writer) {
 return writer->size;
}


decompiled_code:
void *__fastcall gdp_stream_writer_clone_buffer(__int64 a1)
{
int v1; // eax
void *v2; // r12
int v3; // ebx
const void *v4; // rax

v1 = gdp_stream_writer_size(a1);
v2 = (void *)xmalloc0(v1);
v3 = gdp_stream_writer_size(a1);
v4 = (const void *)gdp_stream_writer_buffer(a1);
return memcpy(v2, v4, v3);
}

canonical_decompiled_code:
void *__fastcall func4(__int64 a1)
{
int v1; // eax
void *v2; // r12
int v3; // ebx
const void *v4; // rax

v1 = func3(a1);
v2 = (void *)xmalloc0(v1);
v3 = func3(a1);
v4 = (const void *)func2(a1);
return memcpy(v2, v4, v3);
}

original_code:
char* gdp_stream_writer_clone_buffer(gdp_stream_writer_t* writer) {
 char* clone = (char*)xmalloc0(gdp_stream_writer_size(writer) * sizeof(char));
 memcpy(clone, gdp_stream_writer_buffer(writer), gdp_stream_writer_size(writer));
 return clone;
}

canonical_original_code:
char *gdp_stream_writer_clone_buffer(struct gdp_stream_writer *writer) {
 char *clone = (char*)xmalloc0(gdp_stream_writer_size(writer) * sizeof(char));
 memcpy(clone, gdp_stream_writer_buffer(writer), gdp_stream_writer_size(writer));
 return clone;
}

variable_types:
clone
char *


decompiled_code:
void *__fastcall gdp_stream_writer_clear(__int64 a1)
{
void *result; // rax

result = memset(*(void **)a1, 0, *(int *)(a1 + 12));
*(_DWORD *)(a1 + 12) = 0;
return result;
}

canonical_decompiled_code:
void *__fastcall func5(__int64 a1)
{
void *result; // rax

result = memset(*(void **)a1, 0, *(int *)(a1 + 12));
*(_DWORD *)(a1 + 12) = 0;
return result;
}

original_code:
void gdp_stream_writer_clear(gdp_stream_writer_t* writer) {
 memset(writer->buffer, 0, writer->size);
 writer->size = 0;
}

canonical_original_code:
void gdp_stream_writer_clear(struct gdp_stream_writer *writer) {
 memset(writer->buffer, 0, writer->size);
 writer->size = 0;
}


rsInit

repo: chefinspector-clouseau/openECC

decompiled_code:
__int64 rsInit()
{
int v0; // ebp
char *v1; // rbx
int *v2; // r12
__int64 v3; // rsi
int *v4; // rax
__int64 result; // rax
int v6; // edi
__int64 v7; // rcx
int v8[2]; // [rsp+8h] [rbp-40h] BYREF
char v9; // [rsp+10h] [rbp-38h] BYREF

gfInit();
dword_3BC = 1;
dword_3B8 = 0;
dword_3B4 = 0;
rsSup = 0;
rsGen[0] = 1;
v8[0] = GF_Z(1LL);
v8[1] = 1;
v0 = 4;
v1 = &v9;
v2 = rsGen;
v3 = 0LL;
while ( 1 )
{
result = gfPolMul(v2, v3, v8, 1LL, v1);
v3 = (unsigned int)result;
++v8[0];
if ( !--v0 )
break;
v4 = v2;
v2 = (int *)v1;
v1 = (char *)v4;
}
if ( (int)result >= 0 )
{
v6 = rsSup;
v7 = (int)result;
do
{
if ( !*(_DWORD *)&v1[4 * v7] )
rsGen[0] = 1 / v6;
result = *(unsigned int *)&v1[4 * v7];
rsGen[v7--] = result;
}
while ( (int)v7 >= 0 );
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // ebp
char *v1; // rbx
int *v2; // r12
__int64 v3; // rsi
int *v4; // rax
__int64 result; // rax
int v6; // edi
__int64 v7; // rcx
int v8[2]; // [rsp+8h] [rbp-40h] BYREF
char v9; // [rsp+10h] [rbp-38h] BYREF

gfInit();
dword_3BC = 1;
dword_3B8 = 0;
dword_3B4 = 0;
rsSup = 0;
rsGen[0] = 1;
v8[0] = GF_Z(1LL);
v8[1] = 1;
v0 = 4;
v1 = &v9;
v2 = rsGen;
v3 = 0LL;
while ( 1 )
{
result = gfPolMul(v2, v3, v8, 1LL, v1);
v3 = (unsigned int)result;
++v8[0];
if ( !--v0 )
break;
v4 = v2;
v2 = (int *)v1;
v1 = (char *)v4;
}
if ( (int)result >= 0 )
{
v6 = rsSup;
v7 = (int)result;
do
{
if ( !*(_DWORD *)&v1[4 * v7] )
rsGen[0] = 1 / v6;
result = *(unsigned int *)&v1[4 * v7];
rsGen[v7--] = result;
}
while ( (int)v7 >= 0 );
}
return result;
}

original_code:
void rsInit()
{
 ("---------- rsInit\n");
 gfInit();
 rsSup[4 - 1] = 1;
 for (int i=4 - 2; i>=0; i--)
  rsSup[i] = 0;
 gfExp T[4 + 1];
 int nD1 = 0; gfExp* D1 = rsGen;
 int nD2; gfExp* D2 = T;
 D1[0] = 1;
 gfExp Z[2] = {GF_Z(1), 1};
 for (int i=4; i>0; i--) {
  nD2 = gfPolMul(D1, nD1, Z, 1, D2);
  Z[0]++;
  int s = nD1;
  gfExp* t = D1;
  D1 = D2; nD1 = nD2;
  D2 = t; nD2 = s;
 }
 for (int i=nD1; i>=0; i--) {
  if (D1[i] == 0) {
   ("!! BAD CONFIG: generator polynomial has 0-coefficient !!\n");
   rsGen[0] = 1 / rsSup[0];
  }
  rsGen[i] = D1[i];
 }
 ("ini: rsSup", rsSup, 4 - 1);
 ("ini: rsGen", rsGen, 4);
}

canonical_original_code:
void rsInit()
{
 ("---------- rsInit\n");
 gfInit();
 rsSup[4 - 1] = 1;
 for (int i = 4 - 2; i>=0; i--)
  rsSup[i] = 0;
 int T[-1];
 int nD1 = 0; int *D1 = rsGen;
 int nD2; int *D2 = T;
 D1[0] = 1;
 int Z[2] = {GF_Z(1), 1};
 for (int i = 4; i>0; i--) {
  nD2 = gfPolMul(D1, nD1, Z, 1, D2);
  Z[0]++;
  int s = nD1;
  int *t = D1;
  D1 = D2; nD1 = nD2;
  D2 = t; nD2 = s;
 }
 for (int i = nD1; i>=0; i--) {
  if (D1[i] == 0) {
   ("!! BAD CONFIG: generator polynomial has 0-coefficient !!\n");
   rsGen[0] = 1 / rsSup[0];
  }
  rsGen[i] = D1[i];
 }
 ("ini: rsSup", rsSup, 4 - 1);
 ("ini: rsGen", rsGen, 4);
}

variable_types: i int

variable_types:
T
int [-1]

variable_types:
Z
int [2]

variable_types: s int

variable_types:
t
int *


rsEncode

variable_types:
A
int *

variable_types:
R
int *

variable_types:
XA
int *


rsDecode

decompiled_code:
__int64 __fastcall rsDecode(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
unsigned int v3; // ebx
unsigned int v4; // eax
_DWORD *v5; // rbp
__int64 i; // rbx
unsigned int v7; // er12
unsigned int v8; // er15
unsigned int v9; // er14
unsigned int v10; // er12
unsigned int v11; // eax
unsigned int v12; // eax
unsigned int v13; // [rsp+8h] [rbp-40h] BYREF
unsigned int v14[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v1 = GF_Z(1LL);
gfPolEvalSeq(a1, 14LL, &Sv_3, 3LL, v1);
result = gfPolDeg(&Sv_3, 3LL);
if ( (int)result >= 0 )
{
v3 = result;
gfPolV2E(&Sv_3, &Sv_3, (unsigned int)result);
v13 = 4 - v3;
gfPolEEA(&rsSup, 3LL, &Sv_3, v3, &P_2, v14, &Q_1, &v13, M_0);
v4 = GF_Z(4LL);
result = gfPolEvalSeq(&Q_1, v13, M_0, 10LL, v4);
v5 = (_DWORD *)(a1 + 56);
for ( i = 0LL; i != 11; ++i )
{
if ( !M_0[i] )
{
v7 = GF_Z((unsigned int)(14 - i));
v8 = gfInv1(v7);
v9 = gfPolEval(&P_2, v14[0], v7);
v10 = gfPolEvalDeriv(&Q_1, v13, v7);
v11 = gfMul11(v9, v8);
v12 = gfDiv1(v11, v10);
result = gfAdd((unsigned int)*v5, v12);
*v5 = result;
}
--v5;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
unsigned int v3; // ebx
unsigned int v4; // eax
_DWORD *v5; // rbp
__int64 i; // rbx
unsigned int v7; // er12
unsigned int v8; // er15
unsigned int v9; // er14
unsigned int v10; // er12
unsigned int v11; // eax
unsigned int v12; // eax
unsigned int v13; // [rsp+8h] [rbp-40h] BYREF
unsigned int v14[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v1 = GF_Z(1LL);
gfPolEvalSeq(a1, 14LL, &Sv_3, 3LL, v1);
result = gfPolDeg(&Sv_3, 3LL);
if ( (int)result >= 0 )
{
v3 = result;
gfPolV2E(&Sv_3, &Sv_3, (unsigned int)result);
v13 = 4 - v3;
gfPolEEA(&rsSup, 3LL, &Sv_3, v3, &P_2, v14, &Q_1, &v13, M_0);
v4 = GF_Z(4LL);
result = gfPolEvalSeq(&Q_1, v13, M_0, 10LL, v4);
v5 = (_DWORD *)(a1 + 56);
for ( i = 0LL; i != 11; ++i )
{
if ( !M_0[i] )
{
v7 = GF_Z((unsigned int)(14 - i));
v8 = gfInv1(v7);
v9 = gfPolEval(&P_2, v14[0], v7);
v10 = gfPolEvalDeriv(&Q_1, v13, v7);
v11 = gfMul11(v9, v8);
v12 = gfDiv1(v11, v10);
result = gfAdd((unsigned int)*v5, v12);
*v5 = result;
}
--v5;
}
}
return result;
}

canonical_original_code:
void rsDecode(int *C)
{
 ("---------- rsDecode\n");
 ("dec: C", C, ((1 << (4)) - 1) - 1);
 static int Sv[4];
 gfPolEvalSeq(C, ((1 << (4)) - 1) - 1, Sv, 4 - 1, GF_Z(1));
 ("dec: Sv", Sv, 4 - 1);
 static int M[-1];
 static int P[-1]; int nP;
 static int Q[-1]; int nQ;
 int nS = gfPolDeg(Sv, 4 - 1);
 if (nS >= 0) {
  int *S = Sv;
  gfPolV2E(Sv, S, nS);
  nQ = 4 - nS;
  gfPolEEA(rsSup, 4 - 1, S, nS, P, &nP, Q, &nQ, M);
  int *Vv = M;
  gfPolEvalSeq(Q, nQ, Vv, (((1 << (4)) - 1) - 4) - 1, GF_Z(4));
  for (int i = ((1 << (4)) - 1)-1; i>=4; i--) {
   if (*(Vv++) == 0) {
    int x = GF_Z(i);
    int nx = gfInv1(x);
    int px = gfPolEval(P, nP, x);
    int qx = gfPolEvalDeriv(Q, nQ, x);
    int e = gfDiv1(gfMul11(px, nx), qx);
    C[i] = gfAdd(C[i], e);
    ("Q(%d) = 0 => error E(%d)=%d; new C[%d]=%d\n", x, i, e, i, C[i]);
   }
  }
 }
 ("dec: A", C, (((1 << (4)) - 1) - 4) - 1);
}

variable_types:
C
int *

variable_types:
Sv
int [4]

variable_types:
M
int [-1]

variable_types:
P
int [-1]

variable_types: nP int

variable_types:
Q
int [-1]

variable_types: nQ int

variable_types: nS int

variable_types:
S
int *

variable_types:
Vv
int *

variable_types: x int

variable_types: nx int

variable_types: px int

variable_types: qx int

variable_types: e int


randSetSeed

decompiled_code:
void __fastcall randSetSeed(int a1)
{
SEED = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
SEED = a1;
}

original_code:
void randSetSeed(unsigned int seed)
{
 SEED = seed;
}

variable_types:
seed
unsigned int


randInt

decompiled_code:
__int64 __fastcall randInt(int a1, int a2)
{
SEED *= 1099087573;
return ((unsigned int)SEED >> 8) % (a2 - a1 + 1) + a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
SEED *= 1099087573;
return ((unsigned int)SEED >> 8) % (a2 - a1 + 1) + a1;
}

original_code:
unsigned int randInt(unsigned int min, unsigned int max)
{
 SEED = (1099087573 * SEED) & 0xffffffff;
 return min + ((SEED>>8) % (max - min + 1));
}

variable_types:
min
unsigned int

variable_types:
max
unsigned int

return_type: unsigned int


randE

decompiled_code:
__int64 randE()
{
return randInt(0, 15);
}

canonical_decompiled_code:
__int64 func2()
{
return func1(0, 15);
}

original_code:
gfExp randE()
{
 return randInt(0, (1 << (4)) - 1);
}

canonical_original_code:
int randE()
{
 return randInt(0, (1 << (4)) - 1);
}


decompiled_code:
__int64 randE1()
{
return randInt(1, 15);
}

canonical_decompiled_code:
__int64 func3()
{
return func1(1, 15);
}

original_code:
gfExp randE1()
{
 return randInt(1, (1 << (4)) - 1);
}

canonical_original_code:
int randE1()
{
 return randInt(1, (1 << (4)) - 1);
}


randPol

decompiled_code:
void __fastcall randPol(__int64 a1, int a2)
{
__int64 v2; // rbx

if ( a2 >= 0 )
{
v2 = 0LL;
do
*(_DWORD *)(a1 + 4 * v2++) = randE();
while ( a2 >= (int)v2 );
}
}

original_code:
void randPol(gfExp* P, int nP)
{
 for (int i = 0; i<=nP; i++)
  P[i] = randE();
}

canonical_original_code:
void randPol(int *P, int nP)
{
 for (int i = 0; i<=nP; i++)
  P[i] = randE();
}

variable_types:
P
int *

variable_types: i int


polCmp

decompiled_code:
__int64 __fastcall polCmp(__int64 a1, __int64 a2, int a3, int a4)
{
int v4; // eax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 result; // rax

if ( a3 <= a4 )
{
v4 = a4;
a4 = a3;
a3 = v4;
v5 = a2;
a2 = a1;
a1 = v5;
}
if ( a4 < 0 )
{
LABEL_7:
LODWORD(v7) = a4 + 1;
if ( a3 < a4 + 1 )
{
result = 1LL;
}
else
{
v7 = (int)v7;
while ( !*(_DWORD *)(a1 + 4 * v7) )
{
if ( a3 < (int)++v7 )
return 1LL;
}
result = 0LL;
}
}
else
{
v6 = 0LL;
while ( *(_DWORD *)(a2 + 4 * v6) == *(_DWORD *)(a1 + 4 * v6) )
{
if ( a4 < (int)++v6 )
goto LABEL_7;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, int a3, int a4)
{
int v4; // eax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 result; // rax

if ( a3 <= a4 )
{
v4 = a4;
a4 = a3;
a3 = v4;
v5 = a2;
a2 = a1;
a1 = v5;
}
if ( a4 < 0 )
{
LABEL_7:
LODWORD(v7) = a4 + 1;
if ( a3 < a4 + 1 )
{
result = 1LL;
}
else
{
v7 = (int)v7;
while ( !*(_DWORD *)(a1 + 4 * v7) )
{
if ( a3 < (int)++v7 )
return 1LL;
}
result = 0LL;
}
}
else
{
v6 = 0LL;
while ( *(_DWORD *)(a2 + 4 * v6) == *(_DWORD *)(a1 + 4 * v6) )
{
if ( a4 < (int)++v6 )
goto LABEL_7;
}
result = 0LL;
}
return result;
}

original_code:
int polCmp(gfExp* A, gfExp* B, int nA, int nB)
{
 gfExp* S; int nS;
 gfExp* L; int nL;
 if (nA > nB) {
  L = A; nL = nA; S = B; nS = nB;
 } else {
  L = B; nL = nB; S = A; nS = nA;
 }
 for (int i=0; i<=nS; i++)
  if (S[i] != L[i])
   return 0;
 for (int i=nS+1; i<=nL; i++)
  if (L[i] != 0)
   return 0;
 return 1;
}

canonical_original_code:
int polCmp(int *A, int *B, int nA, int nB)
{
 int *S; int nS;
 int *L; int nL;
 if (nA > nB) {
  L = A; nL = nA; S = B; nS = nB;
 } else {
  L = B; nL = nB; S = A; nS = nA;
 }
 for (int i = 0; i<=nS; i++)
  if (S[i] != L[i])
   return 0;
 for (int i = nS+1; i<=nL; i++)
  if (L[i] != 0)
   return 0;
 return 1;
}

variable_types:
A
int *

variable_types:
B
int *

variable_types: nB int

variable_types:
L
int *

variable_types: nL int


repo: chicyberwarrior/wos

decompiled_code:
__int64 __fastcall setup_gdt_segment_descriptor_bits(int a1, unsigned int a2, int a3, char a4, char a5)
{
__int64 result; // rax

gdt_entries[4 * a1] = a3;
gdt_entries[4 * a1 + 1] = a2;
result = HIWORD(a2);
LOBYTE(gdt_entries[4 * a1 + 2]) = BYTE2(a2);
HIBYTE(gdt_entries[4 * a1 + 3]) = HIBYTE(a2);
HIBYTE(gdt_entries[4 * a1 + 2]) = a4;
LOBYTE(gdt_entries[4 * a1 + 3]) = a5 & 0xF0 | BYTE2(a3) & 0xF;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2, int a3, char a4, char a5)
{
__int64 result; // rax

gdt_entries[4 * a1] = a3;
gdt_entries[4 * a1 + 1] = a2;
result = HIWORD(a2);
LOBYTE(gdt_entries[4 * a1 + 2]) = BYTE2(a2);
HIBYTE(gdt_entries[4 * a1 + 3]) = HIBYTE(a2);
HIBYTE(gdt_entries[4 * a1 + 2]) = a4;
LOBYTE(gdt_entries[4 * a1 + 3]) = a5 & 0xF0 | BYTE2(a3) & 0xF;
return result;
}

original_code:
void setup_gdt_segment_descriptor_bits(int i, unsigned int base_addr, unsigned int size, unsigned char sec, unsigned char granularity) {
    gdt_entries[i].seg_size = (size & 0xFFFF);
    gdt_entries[i].base_addr_1 = (base_addr & 0xFFFF);
    gdt_entries[i].base_addr_2 = (base_addr >> 16) & 0xFF;
    gdt_entries[i].base_addr_3 = (base_addr >> 24) & 0xFF;
    gdt_entries[i].seg_sec = sec;
    gdt_entries[i].seg_granularity = (size >> 16) & 0x0F;
    gdt_entries[i].seg_granularity |= granularity & 0xF0;
}

variable_types:
size
unsigned int

variable_types:
sec
unsigned char

variable_types:
granularity
unsigned char


decompiled_code:
__int64 __fastcall setup_gdt_segment_descriptor(int a1, int a2, char a3, char a4)
{
return setup_gdt_segment_descriptor_bits(a1, 0, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, char a3, char a4)
{
return func0(a1, 0, a2, a3, a4);
}

original_code:
void setup_gdt_segment_descriptor(int entryn, unsigned int base, unsigned char sec, unsigned char gran) {
    setup_gdt_segment_descriptor_bits(entryn, 0, base, sec, gran);
}

variable_types: entryn int

variable_types:
base
unsigned int

variable_types:
gran
unsigned char


decompiled_code:
__int64 flush_gdt()
{
return flush_gdt_and_refresh();
}

canonical_decompiled_code:
__int64 func2()
{
return flush_gdt_and_refresh();
}

original_code:
void flush_gdt() {
    _flush_gdt_and_refresh();
}


decompiled_code:
__int64 setup_gdt()
{
gdt_ptr = 40;
*(_DWORD *)(&gdt_ptr + 1) = (unsigned int)gdt_entries;
printk("GDT base address: 0x%x, size: %d\n", (unsigned int)gdt_entries, 40LL);
setup_gdt_segment_descriptor(0, 0, 0, 0);
setup_gdt_segment_descriptor(1, -1, 154, 207);
setup_gdt_segment_descriptor(2, -1, 146, 207);
setup_gdt_segment_descriptor(3, -1, 250, 207);
setup_gdt_segment_descriptor(4, -1, 242, 207);
return flush_gdt();
}

original_code:
void setup_gdt() {
    unsigned int gdt_base = (unsigned int) &gdt_entries;
    unsigned short gdt_size = (unsigned short) sizeof(struct gdt_segment_descriptor) * 5;
    gdt_ptr.size = gdt_size;
    gdt_ptr.base = gdt_base;
    printk("GDT base address: 0x%x, size: %d\n", gdt_ptr.base, gdt_ptr.size);
    setup_gdt_segment_descriptor(0, 0, 0, 0);
    setup_gdt_segment_descriptor(1, 0xFFFFFFFF, 0x9A, 0xCF);
    setup_gdt_segment_descriptor(2, 0xFFFFFFFF, 0x92, 0xCF);
    setup_gdt_segment_descriptor(3, 0xFFFFFFFF, 0xFA, 0xCF);
    setup_gdt_segment_descriptor(4, 0xFFFFFFFF, 0xF2, 0xCF);
    flush_gdt();
}


decompiled_code:
__int64 __fastcall pmm_set_block(int a1, unsigned int a2)
{
if ( a2 )
*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) |= 1 << (a1 % 32);
else
*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) &= __ROL4__(-2, a1 % 32);
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned int a2)
{
if ( a2 )
*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) |= 1 << (a1 % 32);
else
*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) &= __ROL4__(-2, a1 % 32);
return a2;
}

original_code:
int pmm_set_block(int bit, int val) {
    if(val) {
        pmm_memory_map[bit / 32] |= (1 << (bit % 32));
    } else {
        pmm_memory_map[bit / 32] &= ~ (1 << (bit % 32));
    }
    return val;
}

variable_types: bit int

variable_types: val int


decompiled_code:
_BOOL8 __fastcall pmm_get_block(int a1)
{
return (*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) & (1 << (a1 % 32))) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(int a1)
{
return (*(_DWORD *)(pmm_memory_map + 4LL * (a1 / 32)) & (1 << (a1 % 32))) != 0;
}

original_code:
int pmm_get_block(int bit) {
    return (pmm_memory_map[bit / 32] & (1 << (bit % 32))) != 0;
}


decompiled_code:
void *__fastcall pmm_initialize(void *s, unsigned int a2)
{
pmm_memory_map = (unsigned int)s;
pmm_total_memory_size = a2;
pmm_total_blocks = a2 >> 12;
pmm_used_blocks = a2 >> 12;
pmm_memory_map_length_in_blocks = a2 >> 15;
return memset((void *)(unsigned int)s, -1, a2 >> 15);
}

canonical_decompiled_code:
void *__fastcall func2(void *s, unsigned int a2)
{
pmm_memory_map = (unsigned int)s;
pmm_total_memory_size = a2;
pmm_total_blocks = a2 >> 12;
pmm_used_blocks = a2 >> 12;
pmm_memory_map_length_in_blocks = a2 >> 15;
return memset((void *)(unsigned int)s, -1, a2 >> 15);
}

original_code:
void pmm_initialize(unsigned int where, int size) {
    pmm_memory_map = (unsigned int *) where;
    pmm_total_memory_size = size;
    pmm_total_blocks = pmm_total_memory_size / 4096;
    pmm_used_blocks = pmm_total_blocks;
    pmm_memory_map_length_in_blocks = pmm_total_memory_size / 4096 / 8;
    memset((unsigned int *)where, 0xFFFFFFFF, pmm_total_memory_size / 4096 / 8);
}

variable_types:
where
unsigned int

variable_types: size int


decompiled_code:
void __fastcall pmm_set_memory_size(int a1)
{
pmm_total_memory_size = a1;
}

canonical_decompiled_code:
void __fastcall func3(int a1)
{
pmm_total_memory_size = a1;
}

original_code:
void pmm_set_memory_size(unsigned int size) {
    pmm_total_memory_size = size;
}


decompiled_code:
__int64 pmm_get_memory_start()
{
return (unsigned int)pmm_memory_map;
}

canonical_decompiled_code:
__int64 func4()
{
return (unsigned int)pmm_memory_map;
}

original_code:
unsigned int pmm_get_memory_start() {
    return (unsigned int) pmm_memory_map;
}


decompiled_code:
__int64 pmm_get_memory_size()
{
return (unsigned int)pmm_total_memory_size;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)pmm_total_memory_size;
}

original_code:
unsigned int pmm_get_memory_size() {
    return pmm_total_memory_size;
}


decompiled_code:
__int64 __fastcall pmm_location_to_block(unsigned int a1)
{
return a1 >> 12;
}

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

original_code:
int pmm_location_to_block(unsigned int loc) {
    return loc / 4096;
}

variable_types:
loc
unsigned int


decompiled_code:
__int64 __fastcall pmm_add_memory_region(unsigned int a1, int a2)
{
unsigned int v2; // esi
signed int v3; // ebp
int v4; // ebx
__int64 result; // rax

v2 = a2 - a1;
v3 = v2 >> 12;
if ( (v2 & 0xFFF) != 0 )
{
++v3;
}
else if ( !(v2 >> 12) )
{
return result;
}
v4 = 0;
do
{
result = pmm_set_block(v4 + (a1 >> 12), 0);
--pmm_used_blocks;
++v4;
}
while ( v4 < v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, int a2)
{
unsigned int v2; // esi
signed int v3; // ebp
int v4; // ebx
__int64 result; // rax

v2 = a2 - a1;
v3 = v2 >> 12;
if ( (v2 & 0xFFF) != 0 )
{
++v3;
}
else if ( !(v2 >> 12) )
{
return result;
}
v4 = 0;
do
{
result = func0(v4 + (a1 >> 12), 0);
--pmm_used_blocks;
++v4;
}
while ( v4 < v3 );
return result;
}

original_code:
void pmm_add_memory_region(unsigned int start, unsigned int end) {
    int bit = start / 4096;
    int num = (end - start) / 4096;
    if((end - start) % 4096 != 0) {
        num++;
    }
    for(int i = 0; i < num; i++) {
        pmm_set_block(bit++, 0);
        pmm_used_blocks--;
    }
}

variable_types:
start
unsigned int

variable_types:
end
unsigned int

variable_types: num int

variable_types: i int


decompiled_code:
__int64 pmm_next_free()
{
int v0; // ebp
unsigned int v1; // ebx

v0 = pmm_total_blocks;
if ( pmm_total_blocks <= 0 )
return (unsigned int)-1;
v1 = 0;
while ( pmm_get_block(v1) )
{
if ( ++v1 == v0 )
return (unsigned int)-1;
}
return v1;
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // ebp
unsigned int v1; // ebx

v0 = pmm_total_blocks;
if ( pmm_total_blocks <= 0 )
return (unsigned int)-1;
v1 = 0;
while ( func1(v1) )
{
if ( ++v1 == v0 )
return (unsigned int)-1;
}
return v1;
}

original_code:
int pmm_next_free() {
    for(int i = 0; i < pmm_total_blocks; i++) {
        if(pmm_get_block(i) == 0) {
            return i;
        }
    }
    return -1;
}


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

result = pmm_set_block(a1, 1u);
++pmm_used_blocks;
return result;
}

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

result = func0(a1, 1u);
++pmm_used_blocks;
return result;
}

original_code:
int pmm_reserve_block(int i) {
    int ret = pmm_set_block(i, 1);
    pmm_used_blocks++;
    return ret;
}

variable_types: ret int


decompiled_code:
__int64 pmm_reserve_next_free()
{
unsigned int v0; // ebx

v0 = pmm_next_free();
if ( v0 == -1 )
panic("Failed to reserve more memory blocks.");
pmm_reserve_block(v0);
return v0;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // ebx

v0 = func8();
if ( v0 == -1 )
panic("Failed to reserve more memory blocks.");
func9(v0);
return v0;
}

original_code:
int pmm_reserve_next_free() {
    int free = pmm_next_free();
    if(free == -1) {
        panic("Failed to reserve more memory blocks.");
    }
    pmm_reserve_block(free);
    return free;
}

variable_types: free int


decompiled_code:
__int64 __fastcall pmm_free_block(int a1)
{
--pmm_used_blocks;
return pmm_set_block(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func11(int a1)
{
--pmm_used_blocks;
return func0(a1, 0);
}

original_code:
int pmm_free_block(int i) {
    pmm_used_blocks--;
    return pmm_set_block(i, 0);
}


decompiled_code:
// attributes: thunk
_BOOL8 __fastcall pmm_block_status(int a1)
{
return pmm_get_block(a1);
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(int a1)
{
return func1(a1);
}

original_code:
int pmm_block_status(int block) {
    return pmm_get_block(block);
}

variable_types: block int


decompiled_code:
__int64 pmm_print_summary()
{
printk("PMM internal summary:\n");
return printk(
" pmm_mem_size: %d\n pmm_total_blocks: %d\n pmm_used_blocks: %d\n",
(unsigned int)pmm_total_memory_size,
(unsigned int)pmm_total_blocks,
(unsigned int)pmm_used_blocks);
}

canonical_decompiled_code:
__int64 func13()
{
printk("PMM internal summary:\n");
return printk(
" pmm_mem_size: %d\n pmm_total_blocks: %d\n pmm_used_blocks: %d\n",
(unsigned int)pmm_total_memory_size,
(unsigned int)pmm_total_blocks,
(unsigned int)pmm_used_blocks);
}

original_code:
void pmm_print_summary() {
    printk("PMM internal summary:\n");
    printk("    pmm_mem_size: %d\n    pmm_total_blocks: %d\n    pmm_used_blocks: %d\n",
        pmm_total_memory_size, pmm_total_blocks, pmm_used_blocks);
}


decompiled_code:
void __fastcall pmm_dump_mm(double a1)
{
int v1; // er14
int v2; // ebp
int v3; // er13
double v4; // xmm0_8
int v5; // ebx

v1 = pmm_total_blocks / 60;
if ( pmm_total_blocks > 59 )
{
v2 = 60;
v3 = 0;
do
{
v4 = log(a1);
v5 = v2 - 60;
do
{
pmm_get_block(v5);
v4 = log(v4);
++v5;
}
while ( v5 != v2 );
a1 = log(v4);
++v3;
v2 += 60;
}
while ( v1 > v3 );
}
}

canonical_decompiled_code:
void __fastcall func14(double a1)
{
int v1; // er14
int v2; // ebp
int v3; // er13
double v4; // xmm0_8
int v5; // ebx

v1 = pmm_total_blocks / 60;
if ( pmm_total_blocks > 59 )
{
v2 = 60;
v3 = 0;
do
{
v4 = log(a1);
v5 = v2 - 60;
do
{
func1(v5);
v4 = log(v4);
++v5;
}
while ( v5 != v2 );
a1 = log(v4);
++v3;
v2 += 60;
}
while ( v1 > v3 );
}
}

original_code:
void pmm_dump_mm() {
    int w = 60;
    int r = pmm_total_blocks / 60;
    for(int i = 0; i < r; i++) {
        log("%d | ", i);
        for(int j = 0; j < w; j++) {
            log(pmm_get_block(i * w + j)?"1":"0");
        }
        log("\n");
    }
}

variable_types: w int

variable_types: r int

variable_types: j int