decompiled_code:
__int64 __fastcall config_get_value(char *s1)
{
__int64 v1; // r12
__int64 v2; // rbx

v1 = config_list;
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
return 0LL;
}
while ( strcmp(s1, *(const char **)v2) );
return *(_QWORD *)(v2 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
__int64 v1; // r12
__int64 v2; // rbx

v1 = config_list;
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
return 0LL;
}
while ( strcmp(s1, *(const char **)v2) );
return *(_QWORD *)(v2 + 8);
}

original_code:
void *config_get_value(char * key) {
    config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (config_entry *) entry->next_entry;
    }
    return value;
}

canonical_original_code:
void *config_get_value(char *key) {
    struct config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (struct config_entry *) entry->next_entry;
    }
    return value;
}

variable_types:
key
char *

variable_types: struct config_entry { char *key; void *data; enum config_type { kConfigValueString=0, kConfigValueNumber=1 } type; struct config_entry *next_entry; } *

variable_types:
value
void *

user_defined_types: struct config_entry { char *key; void *data; enum config_type type; struct config_entry *next_entry; }


repo: tristanseifert/WSServer

decompiled_code:
__int64 __fastcall config_get_number(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
return 0LL;
}
while ( strcmp(s1, *(const char **)v2) );
return *(_QWORD *)(v2 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
do
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
return 0LL;
}
while ( strcmp(s1, *(const char **)v2) );
return *(_QWORD *)(v2 + 8);
}

original_code:
long long config_get_number(char * key) {
    config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (config_entry *) entry->next_entry;
    }
    return (long long) value;
}

canonical_original_code:
long long config_get_number(char *key) {
    struct config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (struct config_entry *) entry->next_entry;
    }
    return (long long) value;
}

return_type: long long

user_defined_types: enum config_type { kConfigValueString=0, kConfigValueNumber=1 }


decompiled_code:
__int64 config_find_last_entry()
{
__int64 v0; // rax
__int64 v1; // r8

v0 = config_list;
if ( !config_list )
return 0LL;
do
{
v1 = v0;
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // r8

v0 = config_list;
if ( !config_list )
return 0LL;
do
{
v1 = v0;
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
return v1;
}

original_code:
inline config_entry *config_find_last_entry() {
    if(config_list == ((void *)0)) {
        return config_list;
    }
    config_entry *list_read_ptr = config_list;
    while(list_read_ptr != ((void *)0)) {
        config_entry *entry = list_read_ptr;
        if(entry->next_entry != ((void *)0)) {
            list_read_ptr = (config_entry *) entry->next_entry;
        } else {
            break;
        }
    }
    return list_read_ptr;
}

canonical_original_code:
inline struct config_entry *config_find_last_entry() {
    if(config_list == ((void *)0)) {
        return config_list;
    }
    struct config_entry *list_read_ptr = config_list;
    while(list_read_ptr != ((void *)0)) {
        struct config_entry *entry = list_read_ptr;
        if(entry->next_entry != ((void *)0)) {
            list_read_ptr = (struct config_entry *) entry->next_entry;
        } else {
            break;
        }
    }
    return list_read_ptr;
}

return_type: struct config_entry { char *key; void *data; enum config_type { kConfigValueString=0, kConfigValueNumber=1 } type; struct config_entry *next_entry; } *


decompiled_code:
int config_print_pretty_rep()
{
__int64 v0; // rbx
unsigned int v1; // ebp
int result; // eax
const char *v3; // rdx
const char *v4; // rsi

v0 = config_list;
if ( config_list )
{
v1 = 0;
do
{
while ( 1 )
{
printf(
"Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n",
v1,
(unsigned int)v0,
*(unsigned int *)(v0 + 24),
*(unsigned int *)(v0 + 8));
v3 = *(const char **)(v0 + 8);
v4 = *(const char **)v0;
if ( *(_DWORD *)(v0 + 16) )
break;
++v1;
result = printf("\t %s: %s\n", v4, v3);
v0 = *(_QWORD *)(v0 + 24);
if ( !v0 )
return result;
}
++v1;
result = printf("\t %s: %lli\n", v4, v3);
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
}
return result;
}

canonical_decompiled_code:
int func5()
{
__int64 v0; // rbx
unsigned int v1; // ebp
int result; // eax
const char *v3; // rdx
const char *v4; // rsi

v0 = config_list;
if ( config_list )
{
v1 = 0;
do
{
while ( 1 )
{
printf(
"Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n",
v1,
(unsigned int)v0,
*(unsigned int *)(v0 + 24),
*(unsigned int *)(v0 + 8));
v3 = *(const char **)(v0 + 8);
v4 = *(const char **)v0;
if ( *(_DWORD *)(v0 + 16) )
break;
++v1;
result = printf("\t %s: %s\n", v4, v3);
v0 = *(_QWORD *)(v0 + 24);
if ( !v0 )
return result;
}
++v1;
result = printf("\t %s: %lli\n", v4, v3);
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
}
return result;
}

original_code:
void config_print_pretty_rep() {
    config_entry *entry = config_list;
    int i = 0;
    while(entry != ((void *)0)) {
        printf("Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n", i, (int) entry, (int) entry->next_entry, (int)entry->data);
        if(entry->type == kConfigValueString) {
            printf("\t   %s: %s\n", entry->key, entry->data);
        } else {
            printf("\t   %s: %lli\n", entry->key, (long long) entry->data);
        }
        i++;
        entry = (config_entry *) entry->next_entry;
    }
}

canonical_original_code:
void config_print_pretty_rep() {
    struct config_entry *entry = config_list;
    int i = 0;
    while(entry != ((void *)0)) {
        printf("Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n", i, (int) entry, (int) entry->next_entry, (int)entry->data);
        if(entry->type == kConfigValueString) {
            printf("\t   %s: %s\n", entry->key, entry->data);
        } else {
            printf("\t   %s: %lli\n", entry->key, (long long) entry->data);
        }
        i++;
        entry = (struct config_entry *) entry->next_entry;
    }
}

variable_types: i int

return_type: void


original_code:
void ws_calculate_accept(WS_Client *client) {
 int bufSize = strlen(client->ws_key) + strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + 2;
 char *accept_buf = malloc(bufSize);
 memset(accept_buf, 0x00, bufSize);
 strcpy(accept_buf, client->ws_key);
 strcpy(accept_buf+strlen(client->ws_key), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
 char *sha1_accept_buf = util_sha1(accept_buf, strlen(accept_buf));
 char *base64_accept_header = util_base64_encode(sha1_accept_buf, 20);
 free(accept_buf);
 free(sha1_accept_buf);
 client->ws_accept = base64_accept_header;
}

canonical_original_code:
void ws_calculate_accept(struct WS_Client *client) {
 int bufSize = strlen(client->ws_key) + strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + 2;
 char *accept_buf = malloc(bufSize);
 memset(accept_buf, 0x00, bufSize);
 strcpy(accept_buf, client->ws_key);
 strcpy(accept_buf+strlen(client->ws_key), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
 char *sha1_accept_buf = util_sha1(accept_buf, strlen(accept_buf));
 char *base64_accept_header = util_base64_encode(sha1_accept_buf, 20);
 free(accept_buf);
 free(sha1_accept_buf);
 client->ws_accept = base64_accept_header;
}

variable_types: struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *

variable_types: bufSize int


decompiled_code:
char __fastcall http_parse_headers(void *src, __int64 a2)
{
size_t v2; // rbx
void *v3; // rbp
char *v4; // r13
char *v5; // rbx
char *v6; // rbp
char *v7; // r14
char *v8; // rax
char v9; // bp
char *v10; // rax
__int64 i; // r13
const char *v12; // rbp
const char *v13; // rbp
bool v14; // zf

v2 = strlen((const char *)src);
v3 = malloc(v2 + 2);
memcpy(v3, src, v2 + 1);
v4 = (char *)calloc(0x18uLL, 1uLL);
v5 = strtok((char *)v3, "\r\n");
if ( v5 )
{
v6 = v4;
do
{
v7 = v6;
v8 = (char *)malloc(0x18uLL);
*(_QWORD *)v8 = v5;
v6 = v8;
*(_OWORD *)(v8 + 8) = 0LL;
*((_QWORD *)v7 + 2) = v8;
v5 = strtok(0LL, "\r\n");
}
while ( v5 );
v5 = (char *)*((_QWORD *)v4 + 2);
}
v9 = 9;
v10 = strtok(*(char **)v5, " ");
while ( v10 )
{
if ( *v10 == 47 )
{
*(_QWORD *)v5 = v10;
*((_QWORD *)v5 + 1) = "endpoint";
break;
}
v10 = strtok(0LL, " ");
if ( !--v9 )
return (char)v10;
}
for ( i = *((_QWORD *)v5 + 2); i; i = *(_QWORD *)(i + 16) )
{
v12 = *(const char **)i;
v13 = &v12[(int)strcspn(*(const char **)i, ":")];
*v13++ = 0;
*(_QWORD *)(i + 8) = *(_QWORD *)i;
util_string_trim(v13);
*(_QWORD *)i = v13;
}
*(_QWORD *)(a2 + 48) = v5;
do
{
if ( !strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Key") )
{
v10 = *(char **)v5;
*(_QWORD *)(a2 + 16) = *(_QWORD *)v5;
}
else if ( !strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Protocol") )
{
v10 = *(char **)v5;
*(_QWORD *)(a2 + 32) = *(_QWORD *)v5;
}
else
{
v14 = strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Version") == 0;
LOBYTE(v10) = !v14;
if ( v14 )
{
LODWORD(v10) = strtoll(*(const char **)v5, 0LL, 10);
*(_DWORD *)(a2 + 40) = (_DWORD)v10;
}
}
v5 = (char *)*((_QWORD *)v5 + 2);
}
while ( v5 );
return (char)v10;
}

canonical_decompiled_code:
char __fastcall func0(void *src, __int64 a2)
{
size_t v2; // rbx
void *v3; // rbp
char *v4; // r13
char *v5; // rbx
char *v6; // rbp
char *v7; // r14
char *v8; // rax
char v9; // bp
char *v10; // rax
__int64 i; // r13
const char *v12; // rbp
const char *v13; // rbp
bool v14; // zf

v2 = strlen((const char *)src);
v3 = malloc(v2 + 2);
memcpy(v3, src, v2 + 1);
v4 = (char *)calloc(0x18uLL, 1uLL);
v5 = strtok((char *)v3, "\r\n");
if ( v5 )
{
v6 = v4;
do
{
v7 = v6;
v8 = (char *)malloc(0x18uLL);
*(_QWORD *)v8 = v5;
v6 = v8;
*(_OWORD *)(v8 + 8) = 0LL;
*((_QWORD *)v7 + 2) = v8;
v5 = strtok(0LL, "\r\n");
}
while ( v5 );
v5 = (char *)*((_QWORD *)v4 + 2);
}
v9 = 9;
v10 = strtok(*(char **)v5, " ");
while ( v10 )
{
if ( *v10 == 47 )
{
*(_QWORD *)v5 = v10;
*((_QWORD *)v5 + 1) = "endpoint";
break;
}
v10 = strtok(0LL, " ");
if ( !--v9 )
return (char)v10;
}
for ( i = *((_QWORD *)v5 + 2); i; i = *(_QWORD *)(i + 16) )
{
v12 = *(const char **)i;
v13 = &v12[(int)strcspn(*(const char **)i, ":")];
*v13++ = 0;
*(_QWORD *)(i + 8) = *(_QWORD *)i;
util_string_trim(v13);
*(_QWORD *)i = v13;
}
*(_QWORD *)(a2 + 48) = v5;
do
{
if ( !strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Key") )
{
v10 = *(char **)v5;
*(_QWORD *)(a2 + 16) = *(_QWORD *)v5;
}
else if ( !strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Protocol") )
{
v10 = *(char **)v5;
*(_QWORD *)(a2 + 32) = *(_QWORD *)v5;
}
else
{
v14 = strcmp(*((const char **)v5 + 1), "Sec-WebSocket-Version") == 0;
LOBYTE(v10) = !v14;
if ( v14 )
{
LODWORD(v10) = strtoll(*(const char **)v5, 0LL, 10);
*(_DWORD *)(a2 + 40) = (_DWORD)v10;
}
}
v5 = (char *)*((_QWORD *)v5 + 2);
}
while ( v5 );
return (char)v10;
}

original_code:
void http_parse_headers(char* headers, WS_Client* client) {
 char* headers_copy = malloc(strlen(headers) + 2);
 strcpy(headers_copy, headers);
 http_header* temp_headers = malloc(sizeof(*temp_headers));
 memset(temp_headers, 0x00, sizeof(*temp_headers));
 http_header* current_header = temp_headers;
 char* header = strtok(headers_copy, "\r\n");
 while(header != ((void *)0)) {
  http_header* new_header = malloc(sizeof(*current_header));
  memset(new_header, 0x00, sizeof(*current_header));
  new_header->value = header;
  current_header->next = new_header;
  current_header = new_header;
  header = strtok(((void *)0), "\r\n");
 }
 current_header = temp_headers->next;
 char *endpoint = strtok(current_header->value, " ");
 uint8_t i = 0;
 while(endpoint != ((void *)0)) {
  if(*endpoint == '/') {
   current_header->value = endpoint;
   current_header->key = "endpoint";
   break;
  }
  endpoint = strtok(((void *)0), " ");
  i++;
  if(i > 8) {
   return;
  }
 }
 current_header = current_header->next;
 char *header_token;
 while(current_header != ((void *)0)) {
  int position_of_colon = strcspn(current_header->value, ":");
  char *value_colon = current_header->value + position_of_colon;
  *value_colon = 0x00;
  current_header->key = current_header->value;
  util_string_trim(value_colon + 1);
  current_header->value = value_colon + 1;
  current_header = current_header->next;
 }
 client->headers = temp_headers->next;
 current_header = temp_headers->next;
 while(current_header != ((void *)0)) {
  if(!strcmp(current_header->key, "Sec-WebSocket-Key")) {
   client->ws_key = current_header->value;
  } else if(!strcmp(current_header->key, "Sec-WebSocket-Protocol")) {
   client->ws_protocol = current_header->value;
  } else if(!strcmp(current_header->key, "Sec-WebSocket-Version")) {
   client->ws_version = strtoll(current_header->value, (char **) ((void *)0), 10);
  }
  current_header = current_header->next;
 }
}

canonical_original_code:
void http_parse_headers(char *headers, struct WS_Client *client) {
 char *headers_copy = malloc(strlen(headers) + 2);
 strcpy(headers_copy, headers);
 struct http_header *temp_headers = malloc(sizeof(*temp_headers));
 memset(temp_headers, 0x00, sizeof(*temp_headers));
 struct http_header *current_header = temp_headers;
 char *header = strtok(headers_copy, "\r\n");
 while(header != ((void *)0)) {
  struct http_header *new_header = malloc(sizeof(*current_header));
  memset(new_header, 0x00, sizeof(*current_header));
  new_header->value = header;
  current_header->next = new_header;
  current_header = new_header;
  header = strtok(((void *)0), "\r\n");
 }
 current_header = temp_headers->next;
 char *endpoint = strtok(current_header->value, " ");
 unsigned char i = 0;
 while(endpoint != ((void *)0)) {
  if(*endpoint == '/') {
   current_header->value = endpoint;
   current_header->key = "endpoint";
   break;
  }
  endpoint = strtok(((void *)0), " ");
  i++;
  if(i > 8) {
   return;
  }
 }
 current_header = current_header->next;
 char *header_token;
 while(current_header != ((void *)0)) {
  int position_of_colon = strcspn(current_header->value, ":");
  char *value_colon = current_header->value + position_of_colon;
  *value_colon = 0x00;
  current_header->key = current_header->value;
  util_string_trim(value_colon + 1);
  current_header->value = value_colon + 1;
  current_header = current_header->next;
 }
 client->headers = temp_headers->next;
 current_header = temp_headers->next;
 while(current_header != ((void *)0)) {
  if(!strcmp(current_header->key, "Sec-WebSocket-Key")) {
   client->ws_key = current_header->value;
  } else if(!strcmp(current_header->key, "Sec-WebSocket-Protocol")) {
   client->ws_protocol = current_header->value;
  } else if(!strcmp(current_header->key, "Sec-WebSocket-Version")) {
   client->ws_version = strtoll(current_header->value, (char **) ((void *)0), 10);
  }
  current_header = current_header->next;
 }
}

variable_types:
headers
char *

variable_types: struct http_header { char *value; char *key; void *next; } *

variable_types:
header
char *

variable_types:
endpoint
char *

variable_types:
i
unsigned char

user_defined_types: struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; }


main

canonical_name: main

repo: troessler/fanctrld

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // eax
int v6; // er12
unsigned int v8; // ebp
const char *v9; // r8
int v10; // er13
unsigned int v11; // eax
int v12; // er14
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17; // [rsp+4h] [rbp-34h]
unsigned int seconds; // [rsp+8h] [rbp-30h]
int v19; // [rsp+Ch] [rbp-2Ch]

v3 = *argv;
v19 = 3500;
v17 = 45;
seconds = 5;
Progname = (__int64)v3;
v4 = strrchr(v3, 47);
if ( v4 )
{
v3 = v4 + 1;
Progname = (__int64)(v4 + 1);
}
openlog(v3, 1, 24);
do
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "r:t:p:d");
if ( v5 == -1 )
{
if ( v19 <= 999 )
{
fprintf(stderr, "%s: RPM threshold must be at least 1000.\n", (const char *)Progname);
return 1;
}
if ( v17 > 60 )
{
fprintf(stderr, "%s: You like it hot. Please give a lower thermal threshold.\n", (const char *)Progname);
return 1;
}
if ( v17 < 0 )
{
fprintf(
stderr,
"%s: You want hell to freeze? Please give a positive thermal threshold.\n",
(const char *)Progname);
return 1;
}
if ( seconds > 0x258 )
{
fprintf(stderr, "%s: Please select a poll interval between 0 and 600.\n", (const char *)Progname);
return 1;
}
if ( chdir("/proc/acpi/ibm") == -1 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "%s: Can't chdir to %s -- %s.\n", (const char *)Progname, "/proc/acpi/ibm", v14);
return 1;
}
signal(1, handler);
signal(2, handler);
signal(3, handler);
signal(15, handler);
if ( daemon(0, 0) == -1 )
{
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s: %s. Exiting.", (const char *)Progname, v16);
return 1;
}
v6 = _setjmp(bail);
if ( v6 )
{
if ( v6 == 1 )
{
syslog(1, "Trying to turn on the fan before exiting.");
set_fan(1LL);
}
else
{
syslog(0, "Too many errors, exiting. Risk of overheating.");
}
closelog();
exit(1);
}
while ( 1 )
{
v8 = interrupted;
if ( interrupted )
{
set_fan(1LL);
return v6;
}
v10 = current_rpm();
v11 = current_thm();
v12 = v11;
if ( Debug )
syslog(7, "r = %d, t = %d\n", (unsigned int)v10, v11);
if ( v17 < v12 )
break;
if ( v10 > 0 )
{
v9 = "disabling";
if ( v19 > v10 )
goto LABEL_36;
}
LABEL_37:
sleep(seconds);
}
if ( v10 > 0 )
goto LABEL_37;
v8 = 1;
v9 = "enabling";
LABEL_36:
syslog(6, "r = %d, t = %d -- %s\n", (unsigned int)v10, (unsigned int)v12, v9);
set_fan(v8);
goto LABEL_37;
}
if ( v5 != 112 )
break;
seconds = atoi(_bss_start);
}
if ( v5 <= 112 )
break;
if ( v5 == 114 )
{
v19 = atoi(_bss_start);
}
else if ( v5 == 116 )
{
v17 = atoi(_bss_start);
}
}
if ( v5 == 63 )
{
fprintf(stderr, "%s: unknown parameter.\n", (const char *)Progname);
return 1;
}
if ( v5 != 100 )
break;
Debug = 1;
}
}
while ( v5 != 58 );
fprintf(stderr, "%s: missing argument.\n", (const char *)Progname);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
char *v4; // rax
int v5; // eax
int v6; // er12
unsigned int v8; // ebp
const char *v9; // r8
int v10; // er13
unsigned int v11; // eax
int v12; // er14
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int v17; // [rsp+4h] [rbp-34h]
unsigned int seconds; // [rsp+8h] [rbp-30h]
int v19; // [rsp+Ch] [rbp-2Ch]

v3 = *argv;
v19 = 3500;
v17 = 45;
seconds = 5;
Progname = (__int64)v3;
v4 = strrchr(v3, 47);
if ( v4 )
{
v3 = v4 + 1;
Progname = (__int64)(v4 + 1);
}
openlog(v3, 1, 24);
do
{
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
v5 = getopt(argc, (char *const *)argv, "r:t:p:d");
if ( v5 == -1 )
{
if ( v19 <= 999 )
{
fprintf(stderr, "%s: RPM threshold must be at least 1000.\n", (const char *)Progname);
return 1;
}
if ( v17 > 60 )
{
fprintf(stderr, "%s: You like it hot. Please give a lower thermal threshold.\n", (const char *)Progname);
return 1;
}
if ( v17 < 0 )
{
fprintf(
stderr,
"%s: You want hell to freeze? Please give a positive thermal threshold.\n",
(const char *)Progname);
return 1;
}
if ( seconds > 0x258 )
{
fprintf(stderr, "%s: Please select a poll interval between 0 and 600.\n", (const char *)Progname);
return 1;
}
if ( chdir("/proc/acpi/ibm") == -1 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "%s: Can't chdir to %s -- %s.\n", (const char *)Progname, "/proc/acpi/ibm", v14);
return 1;
}
signal(1, handler);
signal(2, handler);
signal(3, handler);
signal(15, handler);
if ( daemon(0, 0) == -1 )
{
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s: %s. Exiting.", (const char *)Progname, v16);
return 1;
}
v6 = _setjmp(bail);
if ( v6 )
{
if ( v6 == 1 )
{
syslog(1, "Trying to turn on the fan before exiting.");
func10(1LL);
}
else
{
syslog(0, "Too many errors, exiting. Risk of overheating.");
}
closelog();
exit(1);
}
while ( 1 )
{
v8 = interrupted;
if ( interrupted )
{
func10(1LL);
return v6;
}
v10 = func9();
v11 = func8();
v12 = v11;
if ( Debug )
syslog(7, "r = %d, t = %d\n", (unsigned int)v10, v11);
if ( v17 < v12 )
break;
if ( v10 > 0 )
{
v9 = "disabling";
if ( v19 > v10 )
goto LABEL_36;
}
LABEL_37:
sleep(seconds);
}
if ( v10 > 0 )
goto LABEL_37;
v8 = 1;
v9 = "enabling";
LABEL_36:
syslog(6, "r = %d, t = %d -- %s\n", (unsigned int)v10, (unsigned int)v12, v9);
func10(v8);
goto LABEL_37;
}
if ( v5 != 112 )
break;
seconds = atoi(_bss_start);
}
if ( v5 <= 112 )
break;
if ( v5 == 114 )
{
v19 = atoi(_bss_start);
}
else if ( v5 == 116 )
{
v17 = atoi(_bss_start);
}
}
if ( v5 == 63 )
{
fprintf(stderr, "%s: unknown parameter.\n", (const char *)Progname);
return 1;
}
if ( v5 != 100 )
break;
Debug = 1;
}
}
while ( v5 != 58 );
fprintf(stderr, "%s: missing argument.\n", (const char *)Progname);
return 1;
}

original_code:
int main (int argc, char *argv[])
{
  char *p;
  int c;
  int threshold_rpm = 3500;
  int threshold_thm = 45;
  int poll_time = 5;
  int fan_running = 1;
  Progname = argv[0];
  if ((p = strrchr (Progname, '/')))
    Progname = p + 1;
  openlog (Progname, 0x01, (3<<3));
  while ((c = getopt (argc, argv, "r:t:p:d")) != -1)
  {
    switch (c)
    {
      case 'r': threshold_rpm = atoi (optarg); break;
      case 't': threshold_thm = atoi (optarg); break;
      case 'p': poll_time = atoi (optarg); break;
      case 'd': Debug = 1; break;
      case '?': fprintf (stderr, "%s: unknown parameter.\n", Progname); return 1;
      case ':': fprintf (stderr, "%s: missing argument.\n", Progname); return 1;
    }
  }
  if (threshold_rpm < 1000)
  {
    fprintf (stderr, "%s: RPM threshold must be at least 1000.\n", Progname);
    return 1;
  }
  if (threshold_thm > 60)
  {
    fprintf (stderr, "%s: You like it hot.  Please give a lower thermal threshold.\n",
      Progname);
    return 1;
  }
  if (threshold_thm < 0)
  {
    fprintf (stderr, "%s: You want hell to freeze? Please give a positive thermal threshold.\n",
      Progname);
    return 1;
  }
  if (poll_time < 0 || poll_time > 600)
  {
    fprintf (stderr, "%s: Please select a poll interval between 0 and 600.\n",
      Progname);
    return 1;
  }
  if (chdir ("/proc/acpi/ibm") == -1)
  {
    fprintf (stderr, "%s: Can't chdir to %s -- %s.\n",
      Progname, "/proc/acpi/ibm", strerror ((*__errno_location ())));
    return 1;
  }
  signal (1, handler);
  signal (2, handler);
  signal (3, handler);
  signal (15, handler);
  if (daemon (0, 0) == -1)
  {
    fprintf (stderr, "%s: %s.  Exiting.", Progname, strerror ((*__errno_location ())));
    return 1;
  }
  switch (_setjmp (bail))
  {
    case 0: break;
    case 1:
      syslog (1, "Trying to turn on the fan before exiting.");
      set_fan (1);
      closelog ();
      exit (1);
    default:
      syslog (0, "Too many errors, exiting. Risk of overheating.");
      closelog ();
      exit (1);
  }
  while (!interrupted)
  {
    int r = current_rpm();
    int t = current_thm();
    int d;
    d = fan_running = r > 0 ? 1 : 0;
    if (Debug)
      syslog (7, "r = %d, t = %d\n", r, t);
    if (t > threshold_thm)
      d = 1;
    else if (r < threshold_rpm)
      d = 0;
    if (fan_running != d)
    {
      syslog (6, "r = %d, t = %d  --  %s\n", r, t, d ? "enabling" : "disabling");
      set_fan (d);
    }
    sleep (poll_time);
  }
  set_fan (1);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char *p;
  int c;
  int threshold_rpm = 3500;
  int threshold_thm = 45;
  int poll_time = 5;
  int fan_running = 1;
  Progname = argv[0];
  if ((p = strrchr (Progname, '/')))
    Progname = p + 1;
  openlog (Progname, 0x01, (3<<3));
  while ((c = getopt (argc, argv, "r:t:p:d")) != -1)
  {
    switch (c)
    {
      case 'r': threshold_rpm = atoi (optarg); break;
      case 't': threshold_thm = atoi (optarg); break;
      case 'p': poll_time = atoi (optarg); break;
      case 'd': Debug = 1; break;
      case '?': fprintf (stderr, "%s: unknown parameter.\n", Progname); return 1;
      case ':': fprintf (stderr, "%s: missing argument.\n", Progname); return 1;
    }
  }
  if (threshold_rpm < 1000)
  {
    fprintf (stderr, "%s: RPM threshold must be at least 1000.\n", Progname);
    return 1;
  }
  if (threshold_thm > 60)
  {
    fprintf (stderr, "%s: You like it hot.  Please give a lower thermal threshold.\n",
      Progname);
    return 1;
  }
  if (threshold_thm < 0)
  {
    fprintf (stderr, "%s: You want hell to freeze? Please give a positive thermal threshold.\n",
      Progname);
    return 1;
  }
  if (poll_time < 0 || poll_time > 600)
  {
    fprintf (stderr, "%s: Please select a poll interval between 0 and 600.\n",
      Progname);
    return 1;
  }
  if (chdir ("/proc/acpi/ibm") == -1)
  {
    fprintf (stderr, "%s: Can't chdir to %s -- %s.\n",
      Progname, "/proc/acpi/ibm", strerror ((*__errno_location ())));
    return 1;
  }
  signal (1, handler);
  signal (2, handler);
  signal (3, handler);
  signal (15, handler);
  if (daemon (0, 0) == -1)
  {
    fprintf (stderr, "%s: %s.  Exiting.", Progname, strerror ((*__errno_location ())));
    return 1;
  }
  switch (_setjmp (bail))
  {
    case 0: break;
    case 1:
      syslog (1, "Trying to turn on the fan before exiting.");
      set_fan (1);
      closelog ();
      exit (1);
    default:
      syslog (0, "Too many errors, exiting. Risk of overheating.");
      closelog ();
      exit (1);
  }
  while (!interrupted)
  {
    int r = current_rpm();
    int t = current_thm();
    int d;
    d = fan_running = r > 0 ? 1 : 0;
    if (Debug)
      syslog (7, "r = %d, t = %d\n", r, t);
    if (t > threshold_thm)
      d = 1;
    else if (r < threshold_rpm)
      d = 0;
    if (fan_running != d)
    {
      syslog (6, "r = %d, t = %d  --  %s\n", r, t, d ? "enabling" : "disabling");
      set_fan (d);
    }
    sleep (poll_time);
  }
  set_fan (1);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
p
char *

variable_types: c int

variable_types: r int

variable_types: t int

variable_types: d int

return_type: int


handler

decompiled_code:
void __fastcall handler(unsigned int a1)
{
syslog(5, "Caught signal %d, exiting.", a1);
interrupted = 1;
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
syslog(5, "Caught signal %d, exiting.", a1);
interrupted = 1;
}

original_code:
void handler (int s)
{
  syslog (5, "Caught signal %d, exiting.", s);
  interrupted = 1;
}

canonical_original_code:
void handler(int s)
{
  syslog (5, "Caught signal %d, exiting.", s);
  interrupted = 1;
}

variable_types: s int


Initialize

repo: truehyp/snake

decompiled_code:
__int64 __fastcall Initialize(_QWORD *a1)
{
__int64 result; // rax

result = 0x300000001LL;
a1[1] = 0LL;
*a1 = 0x300000001LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
__int64 result; // rax

result = 0x300000001LL;
a1[1] = 0LL;
*a1 = 0x300000001LL;
return result;
}

original_code:
void Initialize (struct Snake *psnake)
{
 psnake->point.x = 1;
 psnake->point.y = 3;
 psnake->next = ((void *)0);
}

canonical_original_code:
void Initialize(struct Snake *psnake)
{
 psnake->point.x = 1;
 psnake->point.y = 3;
 psnake->next = ((void *)0);
}

variable_types: struct Snake { struct Point { int x; int y; } point; struct Snake *next; } *

user_defined_types: struct Snake { struct Point point; struct Snake *next; }


AddSnakeNode

decompiled_code:
__int64 __fastcall AddSnakeNode(const void **a1)
{
_QWORD *v1; // rax
_DWORD *v2; // rsi
const void **v3; // rax
const void **v4; // rdx

v1 = malloc(0x10uLL);
v1[1] = 0LL;
v2 = v1;
v3 = a1;
do
{
v4 = v3;
v3 = (const void **)v3[1];
}
while ( v3 );
*v2 = *(_DWORD *)v4;
v2[1] = *((_DWORD *)v4 + 1);
v4[1] = v2;
g_print("pbody = %p\n", v2);
return g_print("psnake = %p psnake->next = %p\n", a1, a1[1]);
}

canonical_decompiled_code:
__int64 __fastcall func1(const void **a1)
{
_QWORD *v1; // rax
_DWORD *v2; // rsi
const void **v3; // rax
const void **v4; // rdx

v1 = malloc(0x10uLL);
v1[1] = 0LL;
v2 = v1;
v3 = a1;
do
{
v4 = v3;
v3 = (const void **)v3[1];
}
while ( v3 );
*v2 = *(_DWORD *)v4;
v2[1] = *((_DWORD *)v4 + 1);
v4[1] = v2;
g_print("pbody = %p\n", v2);
return g_print("psnake = %p psnake->next = %p\n", a1, a1[1]);
}

original_code:
void AddSnakeNode (struct Snake *psnake)
{
 struct Snake *pbody;
 pbody = (struct Snake *)malloc (sizeof (struct Snake));
 pbody->next = ((void *)0);
 struct Snake *scansnake = psnake;
 while (scansnake->next != ((void *)0))
  scansnake = scansnake->next;
 pbody->point.x = scansnake->point.x;
 pbody->point.y = scansnake->point.y;
 scansnake->next = pbody;
 g_print ("pbody = %p\n",pbody);
 g_print ("psnake = %p psnake->next = %p\n", psnake, psnake->next);
}

canonical_original_code:
void AddSnakeNode(struct Snake *psnake)
{
 struct Snake *pbody;
 pbody = (struct Snake *)malloc (sizeof (struct Snake));
 pbody->next = ((void *)0);
 struct Snake *scansnake = psnake;
 while (scansnake->next != ((void *)0))
  scansnake = scansnake->next;
 pbody->point.x = scansnake->point.x;
 pbody->point.y = scansnake->point.y;
 scansnake->next = pbody;
 g_print ("pbody = %p\n",pbody);
 g_print ("psnake = %p psnake->next = %p\n", psnake, psnake->next);
}


CheckEat

decompiled_code:
_BOOL8 __fastcall CheckEat(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

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

canonical_decompiled_code:
_BOOL8 __fastcall func2(_DWORD *a1, _DWORD *a2)
{
_BOOL8 result; // rax

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

original_code:
gboolean CheckEat (struct Snake *psnake, struct Bean *pbean)
{
 if (psnake->point.x == pbean->point.x)
 {
         if (psnake->point.y == pbean->point.y)
   return (!(0));
 }
 return (0);
}

canonical_original_code:
int CheckEat(struct Snake *psnake, struct Bean *pbean)
{
 if (psnake->point.x == pbean->point.x)
 {
         if (psnake->point.y == pbean->point.y)
   return (!(0));
 }
 return (0);
}

variable_types:
pbean
struct Bean { struct Point point; } *


CheckEnd

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

v2 = a1;
while ( 1 )
{
v2 = (_QWORD *)v2[1];
if ( !v2 )
break;
if ( *(_QWORD *)a1 == *v2 )
return 1LL;
}
v3 = a1[1];
if ( !v3 )
{
if ( *a2 == 1 )
return 2LL;
goto LABEL_6;
}
if ( v3 != 23 || (result = (unsigned int)*a2, (_DWORD)result != 2) )
{
LABEL_6:
result = *a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 23 )
result = 2 * (unsigned int)(*a2 == 4);
else
result = 0LL;
}
else if ( *a2 == 3 )
{
result = 2LL;
}
}
return result;
}

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

v2 = a1;
while ( 1 )
{
v2 = (_QWORD *)v2[1];
if ( !v2 )
break;
if ( *(_QWORD *)a1 == *v2 )
return 1LL;
}
v3 = a1[1];
if ( !v3 )
{
if ( *a2 == 1 )
return 2LL;
goto LABEL_6;
}
if ( v3 != 23 || (result = (unsigned int)*a2, (_DWORD)result != 2) )
{
LABEL_6:
result = *a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 23 )
result = 2 * (unsigned int)(*a2 == 4);
else
result = 0LL;
}
else if ( *a2 == 3 )
{
result = 2LL;
}
}
return result;
}

original_code:
gint CheckEnd (struct Snake *psnake, int *pforward)
{
 struct Snake *scansnake = psnake;
 struct Snake *head = psnake;
 while (scansnake->next != ((void *)0))
 {
  scansnake = scansnake->next;
  if (head->point.x == scansnake->point.x &&head->point.y == scansnake->point.y)
   return 1;
 }
 if (head->point.y == 0 && (*pforward) == 1)
  return 2;
 if (head->point.y == 24 - 1 && (*pforward) == 2)
  return 2;
 if (head->point.x == 0 && (*pforward) == 3)
  return 2;
 if (head->point.x == 24 - 1 && (*pforward) == 4)
  return 2;
 return 0;
}

canonical_original_code:
int CheckEnd(struct Snake *psnake, int *pforward)
{
 struct Snake *scansnake = psnake;
 struct Snake *head = psnake;
 while (scansnake->next != ((void *)0))
 {
  scansnake = scansnake->next;
  if (head->point.x == scansnake->point.x &&head->point.y == scansnake->point.y)
   return 1;
 }
 if (head->point.y == 0 && (*pforward) == 1)
  return 2;
 if (head->point.y == 24 - 1 && (*pforward) == 2)
  return 2;
 if (head->point.x == 0 && (*pforward) == 3)
  return 2;
 if (head->point.x == 24 - 1 && (*pforward) == 4)
  return 2;
 return 0;
}

variable_types:
pforward
int *


Food

decompiled_code:
__int64 __fastcall Food(int *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
int v3; // edx
__int64 result; // rax

v1 = time(0LL);
srand(v1);
*a1 = rand() % 24;
v2 = time(0LL);
srand(v2);
v3 = rand();
result = (unsigned int)(24 * (v3 / 24));
a1[1] = v3 % 24;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1)
{
unsigned int v1; // eax
unsigned int v2; // eax
int v3; // edx
__int64 result; // rax

v1 = time(0LL);
srand(v1);
*a1 = rand() % 24;
v2 = time(0LL);
srand(v2);
v3 = rand();
result = (unsigned int)(24 * (v3 / 24));
a1[1] = v3 % 24;
return result;
}

original_code:
void Food (struct Bean *pbean)
{
 srand((unsigned int )time(0));
 pbean->point.x = rand()%24 ;
 srand((unsigned int )time(0));
 pbean->point.y = rand()%24 ;
}

canonical_original_code:
void Food(struct Bean *pbean)
{
 srand((unsigned int )time(0));
 pbean->point.x = rand()%24 ;
 srand((unsigned int )time(0));
 pbean->point.y = rand()%24 ;
}

variable_types: struct Bean { struct Point { int x; int y; } point; } *

user_defined_types:
struct Bean { struct Point point; }
struct Point { int x; int y; }


decompiled_code:
int gklog_trace(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[33m");
printf("[TRACE]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func10(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[33m");
printf("[TRACE]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

original_code:
void gklog_trace(const char* format,...)
{
 if(!isDebug)return;
 printf("\033[33m");
 printf("[TRACE]:");
 printf("\033[0m");
 va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

canonical_original_code:
void gklog_trace(char *format, ...)
{
 if(!isDebug)return;
 printf("\033[33m");
 printf("[TRACE]:");
 printf("\033[0m");
 __builtin_va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

variable_types:
format
char *


decompiled_code:
int gklog_debug(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[32m\x1B[1m");
printf("[DEBUG]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func11(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[32m\x1B[1m");
printf("[DEBUG]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

original_code:
void gklog_debug(const char* format,...)
{
 if(!isDebug)return;
 printf("\033[32m\033[1m");
 printf("[DEBUG]:");
 printf("\033[0m");
 va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

canonical_original_code:
void gklog_debug(char *format, ...)
{
 if(!isDebug)return;
 printf("\033[32m\033[1m");
 printf("[DEBUG]:");
 printf("\033[0m");
 __builtin_va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}


decompiled_code:
int gklog_error(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[31m\x1B[1m");
printf("[ERROR]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func12(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[31m\x1B[1m");
printf("[ERROR]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

original_code:
void gklog_error(const char* format,...)
{
 if(!isDebug)return;
 printf("\033[31m\033[1m");
 printf("[ERROR]:");
 printf("\033[0m");
 va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

canonical_original_code:
void gklog_error(char *format, ...)
{
 if(!isDebug)return;
 printf("\033[31m\033[1m");
 printf("[ERROR]:");
 printf("\033[0m");
 __builtin_va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}


decompiled_code:
int gklog_warn(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("[WARN]:");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func13(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("[WARN]:");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

original_code:
void gklog_warn(const char* format,...)
{
 if(!isDebug)return;
 printf("[WARN]:");
 va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

canonical_original_code:
void gklog_warn(char *format, ...)
{
 if(!isDebug)return;
 printf("[WARN]:");
 __builtin_va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}


decompiled_code:
int gklog_infor(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[34m");
printf("[INFOR]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func14(char *format, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

result = isDebug;
if ( isDebug )
{
printf("\x1B[34m");
printf("[INFOR]:");
printf("\x1B[0m");
va_start(arg, format);
vprintf(format, arg);
result = putchar(10);
}
return result;
}

original_code:
void gklog_infor(const char* format,...)
{
 if(!isDebug)return;
 printf("\033[34m");
 printf("[INFOR]:");
 printf("\033[0m");
 va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}

canonical_original_code:
void gklog_infor(char *format, ...)
{
 if(!isDebug)return;
 printf("\033[34m");
 printf("[INFOR]:");
 printf("\033[0m");
 __builtin_va_list arg_ptr;
 __builtin_va_start(arg_ptr,format);
 vprintf(format,arg_ptr);
 printf("\n");
}


original_code:
void gklog_hex(unsigned char* data,int len)
{
 if(!isDebug)return;
 char* tmp = (char*)malloc(len*2+1);
 int count =0;
 int i = 0;
 for(i=0;i<len;i++)
 {
  tmp[count++] = hex[(data[i]>>4)];
  tmp[count++] = hex[(data[i]& 0x0f)];
 }
 tmp[count]='\0';
 printf("[HEX]: %s\n",tmp);
 free(tmp);
}

canonical_original_code:
void gklog_hex(unsigned char *data, int len)
{
 if(!isDebug)return;
 char *tmp = (char*)malloc(len*2+1);
 int count = 0;
 int i = 0;
 for(i=0;i<len;i++)
 {
  tmp[count++] = hex[(data[i]>>4)];
  tmp[count++] = hex[(data[i]& 0x0f)];
 }
 tmp[count]='\0';
 printf("[HEX]: %s\n",tmp);
 free(tmp);
}

variable_types:
data
unsigned char *

variable_types: len int

variable_types:
tmp
char *

variable_types: count int

variable_types: i int


main

repo: tsalmon/Dazibao

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rsi
__int64 v5; // rax
__int64 v6; // rsi

v3 = argv[1];
*(&dazibao + 1) = 0LL;
*((_DWORD *)&dazibao + 4) = 0;
dazibao = (__int64)v3;
*(&dazibao + 3) = 0LL;
if ( argc == 2 )
{
printf("[i] Dazibao file : %s\n", v3);
dazibao_get_file_size(&dazibao);
dazibao_open_file(&dazibao);
if ( (unsigned int)dazibao_check_header(&dazibao) )
{
puts("[+] Finding root tlv :\n");
v5 = find_next_tlv_array(&dazibao, 4LL, *((unsigned int *)&dazibao + 3), (unsigned int)((_DWORD)&dazibao + 16));
v6 = *((unsigned int *)&dazibao + 4);
*(&dazibao + 3) = v5;
load_tlv_init(v5, v6);
}
else
{
puts("[!] File header is not valid!");
}
vue_init();
puts("[i] :)");
dazibao_close_file(&dazibao);
}
else
{
puts("!!! ARGS !!!");
}
return 0;
}

original_code:
int main(int argc, char **argv) {
  dazibao.file_path = argv[1];
  dazibao.file_descriptor = 0;
  dazibao.file_size = 0;
  dazibao.tlv_count = 0;
  dazibao.elements = ((void *)0);
  if (argc != 2) {
    printf("!!! ARGS !!!\n");
    return 0;
  }
  printf("[i] Dazibao file : %s\n", dazibao.file_path);
  dazibao_get_file_size(&dazibao);
  dazibao_open_file(&dazibao);
  if (!dazibao_check_header(&dazibao)) {
    printf("[!] File header is not valid!\n");
  } else {
    printf("[+] Finding root tlv :\n\n");
    dazibao.elements = find_next_tlv_array(&dazibao, 4, dazibao.file_size, &dazibao.tlv_count);
    load_tlv_init(dazibao.elements, dazibao.tlv_count);
  }
  vue_init();
  printf("[i] :)\n");
  dazibao_close_file(&dazibao);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 __fastcall get_file_size(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) == -1 )
{
perror("get_file_size");
exit(58);
}
return LODWORD(v2.st_size);
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) == -1 )
{
perror("get_file_size");
exit(58);
}
return LODWORD(v2.st_size);
}

original_code:
int get_file_size(char *path) {
    struct stat stats;
    if(stat(path, &stats) == -1) {
        perror("get_file_size");
        exit(58);
    }
    return stats.st_size;
}

variable_types:
path
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


decompiled_code:
char *__fastcall timestamp_to_date(int a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x12uLL);
v2 = localtime(&timer);
strftime(v1, 0x12uLL, "%d/%m/%y %Hh%Mm%Ss", v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func1(int a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x12uLL);
v2 = localtime(&timer);
strftime(v1, 0x12uLL, "%d/%m/%y %Hh%Mm%Ss", v2);
return v1;
}

original_code:
char *timestamp_to_date(int timestamp) {
    time_t time = timestamp;
    struct tm *tmp;
    char *output = malloc(sizeof(char) * strlen("%d/%m/%y %Hh%Mm%Ss"));
    tmp = localtime(&time);
    strftime(output, strlen("%d/%m/%y %Hh%Mm%Ss"), "%d/%m/%y %Hh%Mm%Ss", tmp);
    return output;
}

canonical_original_code:
char *timestamp_to_date(int timestamp) {
    long time = timestamp;
    struct tm *tmp;
    char *output = malloc(sizeof(char) * strlen("%d/%m/%y %Hh%Mm%Ss"));
    tmp = localtime(&time);
    strftime(output, strlen("%d/%m/%y %Hh%Mm%Ss"), "%d/%m/%y %Hh%Mm%Ss", tmp);
    return output;
}

variable_types: timestamp int

variable_types: time long

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

variable_types:
output
char *

return_type: char *


decompiled_code:
char *__fastcall timestamp_to_date_short(int a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x12uLL);
v2 = localtime(&timer);
strftime(v1, 8uLL, "%d/%m/%y", v2);
return v1;
}

canonical_decompiled_code:
char *__fastcall func2(int a1)
{
char *v1; // r12
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = a1;
v1 = (char *)malloc(0x12uLL);
v2 = localtime(&timer);
strftime(v1, 8uLL, "%d/%m/%y", v2);
return v1;
}

original_code:
char *timestamp_to_date_short(int timestamp) {
    time_t time = timestamp;
    struct tm *tmp;
    char *output = malloc(sizeof(char) * strlen("%d/%m/%y %Hh%Mm%Ss"));
    tmp = localtime(&time);
    strftime(output, strlen("%d/%m/%y"), "%d/%m/%y", tmp);
    return output;
}

canonical_original_code:
char *timestamp_to_date_short(int timestamp) {
    long time = timestamp;
    struct tm *tmp;
    char *output = malloc(sizeof(char) * strlen("%d/%m/%y %Hh%Mm%Ss"));
    tmp = localtime(&time);
    strftime(output, strlen("%d/%m/%y"), "%d/%m/%y", tmp);
    return output;
}

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long __tm_gmtoff; char *__tm_zone; }


decompiled_code:
__int64 __fastcall get_tlv_dated_timestamp(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // rdx

result = 0LL;
if ( *(_DWORD *)a1 == 6 )
{
v2 = *(__int64 **)(a1 + 24);
if ( v2 )
result = *v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // rdx

result = 0LL;
if ( *(_DWORD *)a1 == 6 )
{
v2 = *(__int64 **)(a1 + 24);
if ( v2 )
result = *v2;
}
return result;
}

original_code:
long get_tlv_dated_timestamp(Dazibao_TLV *tlv) {
    if(tlv->type == DATED && tlv->value != ((void *)0)) {
        return ((Dazibao_TLV_Dated_Value *)tlv->value)->timestamp;
    } else {
        return 0;
    }
}

canonical_original_code:
long get_tlv_dated_timestamp(struct Dazibao_TLV *tlv) {
    if(tlv->type == DATED && tlv->value != ((void *)0)) {
        return ((struct Dazibao_TLV_Dated_Value *)tlv->value)->timestamp;
    } else {
        return 0;
    }
}

variable_types: struct Dazibao_TLV { enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 } type; long position; int length; void *value; } *

return_type: long

user_defined_types: struct Dazibao_TLV { enum Dazibao_TLV_Type type; long position; int length; void *value; }

user_defined_types: struct Dazibao_TLV_Dated_Value { long timestamp; struct Dazibao_TLV *element; }


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

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

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

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

original_code:
Dazibao_TLV *get_tlv_dated_element(Dazibao_TLV *tlv) {
    if(tlv->type == DATED) {
        return ((Dazibao_TLV_Dated_Value *)tlv->value)->element;
    } else {
        return ((void *)0);
    }
}

canonical_original_code:
struct Dazibao_TLV *get_tlv_dated_element(struct Dazibao_TLV *tlv) {
    if(tlv->type == DATED) {
        return ((struct Dazibao_TLV_Dated_Value *)tlv->value)->element;
    } else {
        return ((void *)0);
    }
}

variable_types:
tlv
struct Dazibao_TLV *

return_type: struct Dazibao_TLV { enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 } type; long position; int length; void *value; } *


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

if ( *(_DWORD *)a1 == 5 )
result = **(unsigned int **)(a1 + 24);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( *(_DWORD *)a1 == 5 )
result = **(unsigned int **)(a1 + 24);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int get_tlv_compound_count(Dazibao_TLV *tlv) {
    if(tlv->type == COMPOUND) {
        return ((Dazibao_TLV_Compound_Value *)tlv->value)->count;
    } else {
        return -1;
    }
}

canonical_original_code:
int get_tlv_compound_count(struct Dazibao_TLV *tlv) {
    if(tlv->type == COMPOUND) {
        return ((struct Dazibao_TLV_Compound_Value *)tlv->value)->count;
    } else {
        return -1;
    }
}

user_defined_types: struct Dazibao_TLV_Compound_Value { int count; struct Dazibao_TLV **elements; }

user_defined_types: enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 }


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

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

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

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

original_code:
Dazibao_TLV **get_tlv_compound_elements(Dazibao_TLV *tlv) {
    if(tlv->type == COMPOUND) {
        return ((Dazibao_TLV_Compound_Value *)tlv->value)->elements;
    } else {
        return ((void *)0);
    }
}

canonical_original_code:
struct Dazibao_TLV **get_tlv_compound_elements(struct Dazibao_TLV *tlv) {
    if(tlv->type == COMPOUND) {
        return ((struct Dazibao_TLV_Compound_Value *)tlv->value)->elements;
    } else {
        return ((void *)0);
    }
}

return_type: struct Dazibao_TLV { enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 } type; long position; int length; void *value; } **


decompiled_code:
const char *__fastcall tlv_type_to_text(unsigned int a1)
{
const char *result; // rax
__int64 v2[8]; // [rsp+0h] [rbp-40h]

v2[0] = (__int64)"PADONE";
result = "UNKNOW";
v2[1] = (__int64)"PADN";
v2[2] = (__int64)"TEXT";
v2[3] = (__int64)"PNG";
v2[4] = (__int64)"JPEG";
v2[5] = (__int64)"COMPOUND";
v2[6] = (__int64)"DATED";
if ( a1 <= 7 )
result = (const char *)v2[a1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func7(unsigned int a1)
{
const char *result; // rax
__int64 v2[8]; // [rsp+0h] [rbp-40h]

v2[0] = (__int64)"PADONE";
result = "UNKNOW";
v2[1] = (__int64)"PADN";
v2[2] = (__int64)"TEXT";
v2[3] = (__int64)"PNG";
v2[4] = (__int64)"JPEG";
v2[5] = (__int64)"COMPOUND";
v2[6] = (__int64)"DATED";
if ( a1 <= 7 )
result = (const char *)v2[a1];
return result;
}

original_code:
char *tlv_type_to_text(Dazibao_TLV_Type type) {
    char *strings[] = {"PADONE", "PADN", "TEXT", "PNG", "JPEG", "COMPOUND", "DATED"};
    return (type < 0 || type > 7) ? "UNKNOW" : strings[type];
}

canonical_original_code:
char *tlv_type_to_text(enum Dazibao_TLV_Type type) {
    char *strings[] = {"PADONE", "PADN", "TEXT", "PNG", "JPEG", "COMPOUND", "DATED"};
    return (type < 0 || type > 7) ? "UNKNOW" : strings[type];
}

variable_types: enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 }

variable_types:
strings
char *[]


decompiled_code:
char *__fastcall get_tlv_header(__int64 a1)
{
char *v1; // r12
const char *v2; // rax

v1 = (char *)malloc(0x1000uLL);
v2 = tlv_type_to_text(*(_DWORD *)a1);
sprintf(v1, ". %s | at %d | length %d", v2, *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
return v1;
}

canonical_decompiled_code:
char *__fastcall func8(__int64 a1)
{
char *v1; // r12
const char *v2; // rax

v1 = (char *)malloc(0x1000uLL);
v2 = func7(*(_DWORD *)a1);
sprintf(v1, ". %s | at %d | length %d", v2, *(_QWORD *)(a1 + 8), *(unsigned int *)(a1 + 16));
return v1;
}

original_code:
char *get_tlv_header(Dazibao_TLV *tlv) {
    char *output = malloc(sizeof(char) * 4096);
    sprintf(output, ". %s | at %d | length %d", tlv_type_to_text(tlv->type), (int)tlv->position, tlv->length);
    return output;
}

canonical_original_code:
char *get_tlv_header(struct Dazibao_TLV *tlv) {
    char *output = malloc(sizeof(char) * 4096);
    sprintf(output, ". %s | at %d | length %d", tlv_type_to_text(tlv->type), (int)tlv->position, tlv->length);
    return output;
}


insertSpace

original_code:
int insertSpace(char str[]){
  int cur = 0;
  int i = 0;
  int nb_lignes = 1;
  while(str[cur] != '\0'){
    if(str[cur] == '\n'){
      nb_lignes++;
      i = 0;
    }
    if(i == 80){
      while(str[cur--] != ' '){
      }
      str[cur] = '\n';
      nb_lignes++;
      cur++;
      i = 0;
    } else {
      i++;
      cur++;
    }
  }
  return nb_lignes;
}

variable_types:
str
char []

variable_types: cur int


decompiled_code:
void __fastcall dazibao_print_tree(__int64 a1, __int64 a2, int a3, int a4)
{
__int64 v6; // rbx
char *v7; // r12
const char *v8; // rax
int v9; // eax
__int64 v10; // rdi
__int64 v11; // rax
__int64 v12; // [rsp+0h] [rbp-50h]
__int64 v13[8]; // [rsp+10h] [rbp-40h] BYREF

if ( a3 > 0 )
{
v6 = 0LL;
v12 = a3;
do
{
v7 = get_tlv_header(*(_QWORD *)(a2 + 8 * v6));
v8 = string_repeat(a4, " ");
printf("%s%d)%s\n", v8, (unsigned int)v6, v7);
v9 = **(_DWORD **)(a2 + 8 * v6);
if ( v9 == 5 )
{
load_tlv_value(a1);
v10 = *(_QWORD *)(a2 + 8 * v6);
get_tlv_compound_count(v10);
v11 = get_tlv_compound_elements(v10);
dazibao_print_tree(a1, v11);
}
else if ( v9 == 6 )
{
load_tlv_value(a1);
v13[0] = get_tlv_dated_element(*(_QWORD *)(a2 + 8 * v6));
dazibao_print_tree(a1, v13);
}
++v6;
}
while ( v6 != v12 );
}
}

original_code:
void dazibao_print_tree(Dazibao *dazibao, Dazibao_TLV **elements, int count, int deepness) {
    int i;
    Dazibao_TLV *tmp;
    for (i = 0; i < count; i++) {
        printf("%s%d)%s\n", string_repeat(deepness, "    "), i ,get_tlv_header(elements[i]));
        switch(elements[i]->type) {
        case DATED:
            load_tlv_value(dazibao, elements[i]);
            tmp = get_tlv_dated_element(elements[i]);
            dazibao_print_tree(dazibao, &tmp, 1, deepness + 1);
            break;
        case COMPOUND:
            load_tlv_value(dazibao, elements[i]);
            dazibao_print_tree(dazibao, get_tlv_compound_elements(elements[i]), get_tlv_compound_count(elements[i]), deepness + 1);
            break;
        default:;
        }
    }
}

canonical_original_code:
void dazibao_print_tree(struct Dazibao *dazibao, struct Dazibao_TLV **elements, int count, int deepness) {
    int i;
    struct Dazibao_TLV *tmp;
    for (i = 0; i < count; i++) {
        printf("%s%d)%s\n", string_repeat(deepness, "    "), i ,get_tlv_header(elements[i]));
        switch(elements[i]->type) {
        case DATED:
            load_tlv_value(dazibao, elements[i]);
            tmp = get_tlv_dated_element(elements[i]);
            dazibao_print_tree(dazibao, &tmp, 1, deepness + 1);
            break;
        case COMPOUND:
            load_tlv_value(dazibao, elements[i]);
            dazibao_print_tree(dazibao, get_tlv_compound_elements(elements[i]), get_tlv_compound_count(elements[i]), deepness + 1);
            break;
        default:;
        }
    }
}

variable_types: struct Dazibao { char *file_path; int file_descriptor; int file_size; int tlv_count; struct Dazibao_TLV { enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 } type; long position; int length; void *value; } **elements; } *

variable_types:
elements
struct Dazibao_TLV **

variable_types: deepness int

user_defined_types: struct Dazibao { char *file_path; int file_descriptor; int file_size; int tlv_count; struct Dazibao_TLV **elements; }


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

result = get_file_size(*(_QWORD *)a1);
*(_DWORD *)(a1 + 12) = result;
return result;
}

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

result = get_file_size(*(_QWORD *)a1);
*(_DWORD *)(a1 + 12) = result;
return result;
}

original_code:
void dazibao_get_file_size(Dazibao *dazibao) {
    dazibao->file_size = get_file_size(dazibao->file_path);
}

canonical_original_code:
void dazibao_get_file_size(struct Dazibao *dazibao) {
    dazibao->file_size = get_file_size(dazibao->file_path);
}


original_code:
Dazibao_TLV **find_next_tlv_array(Dazibao *dazibao, int offset_start, int offset_max, int *tlv_count) {
   Dazibao_TLV *new = ((void *)0);
   Dazibao_TLV **new_array = ((void *)0);
   int next_position = offset_start;
   while (next_position < offset_max) {
       if(*tlv_count == 0) {
           new_array = malloc(sizeof(Dazibao_TLV *));
       } else {
  new_array = realloc(new_array, sizeof(Dazibao_TLV *) * (*tlv_count + 1 ));
       }
       new_array[(*tlv_count)++] = new = find_next_tlv(dazibao, next_position);
       next_position = new->position + 4 + new->length;
   }
   return new_array;
}

canonical_original_code:
struct Dazibao_TLV **find_next_tlv_array(struct Dazibao *dazibao, int offset_start, int offset_max, int *tlv_count) {
   struct Dazibao_TLV *new = ((void *)0);
   struct Dazibao_TLV **new_array = ((void *)0);
   int next_position = offset_start;
   while (next_position < offset_max) {
       if(*tlv_count == 0) {
           new_array = malloc(sizeof(struct Dazibao_TLV *));
       } else {
  new_array = realloc(new_array, sizeof(struct Dazibao_TLV *) * (*tlv_count + 1 ));
       }
       new_array[(*tlv_count)++] = new = find_next_tlv(dazibao, next_position);
       next_position = new->position + 4 + new->length;
   }
   return new_array;
}

variable_types: struct Dazibao { char *file_path; int file_descriptor; int file_size; int tlv_count; struct Dazibao_TLV **elements; } *

variable_types: struct Dazibao_TLV { enum Dazibao_TLV_Type { UNKNOW=-1, PADONE=0, PADN=1, TEXT=2, PNG=3, JPEG=4, COMPOUND=5, DATED=6 } type; long position; int length; void *value; } **


original_code:
char *load_tlv_value_raw(Dazibao *dazibao, Dazibao_TLV *tlv) {
   char *value = malloc(sizeof(char) * (tlv->length));
   lseek(dazibao->file_descriptor, tlv->position + 4, 0);
   read(dazibao->file_descriptor, value, tlv->length);
   return value;
}

canonical_original_code:
char *load_tlv_value_raw(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
   char *value = malloc(sizeof(char) * (tlv->length));
   lseek(dazibao->file_descriptor, tlv->position + 4, 0);
   read(dazibao->file_descriptor, value, tlv->length);
   return value;
}

variable_types:
value
char *


decompiled_code:
int __fastcall load_tlv_value(__int64 a1, __int64 a2)
{
int v2; // eax
void *v3; // rax

v2 = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 5 )
{
v3 = load_tlv_value_compound(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}
if ( v2 > 5 )
{
if ( v2 == 6 )
{
v3 = load_tlv_value_dated(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}
LABEL_8:
LODWORD(v3) = puts("CANT LOAD VALUE !!!");
return (int)v3;
}
if ( (unsigned int)(v2 - 2) > 2 )
goto LABEL_8;
v3 = load_tlv_value_raw(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1, __int64 a2)
{
int v2; // eax
void *v3; // rax

v2 = *(_DWORD *)a2;
if ( *(_DWORD *)a2 == 5 )
{
v3 = load_tlv_value_compound(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}
if ( v2 > 5 )
{
if ( v2 == 6 )
{
v3 = load_tlv_value_dated(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}
LABEL_8:
LODWORD(v3) = puts("CANT LOAD VALUE !!!");
return (int)v3;
}
if ( (unsigned int)(v2 - 2) > 2 )
goto LABEL_8;
v3 = func6(a1, a2);
*(_QWORD *)(a2 + 24) = v3;
return (int)v3;
}

original_code:
void load_tlv_value(Dazibao *dazibao, Dazibao_TLV *tlv) {
   switch(tlv->type) {
       case TEXT :
       case PNG :
       case JPEG :
           tlv->value = load_tlv_value_raw(dazibao, tlv);
           break;
       case DATED :
           tlv->value = load_tlv_value_dated(dazibao, tlv);
           break;
       case COMPOUND:
           tlv->value = load_tlv_value_compound(dazibao, tlv);
           break;
       default :
           printf("CANT LOAD VALUE !!!\n");
   }
}

canonical_original_code:
void load_tlv_value(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
   switch(tlv->type) {
       case TEXT :
       case PNG :
       case JPEG :
           tlv->value = load_tlv_value_raw(dazibao, tlv);
           break;
       case DATED :
           tlv->value = load_tlv_value_dated(dazibao, tlv);
           break;
       case COMPOUND:
           tlv->value = load_tlv_value_compound(dazibao, tlv);
           break;
       default :
           printf("CANT LOAD VALUE !!!\n");
   }
}


decompiled_code:
void __fastcall load_tlv_init(__int64 *a1, int a2)
{
__int64 *v2; // rbp
int v3; // eax
__int64 v4; // rbx
int v5; // eax
__int64 v6; // rax
__int64 v7; // rax

if ( a2 > 0 )
{
v2 = a1;
do
{
load_tlv_value((__int64)&dazibao, *v2);
v3 = *(_DWORD *)*v2;
if ( v3 == 5 )
{
v6 = ((__int64 (*)(void))get_tlv_compound_elements)();
load_tlv_init(v6, **(unsigned int **)(*v2 + 24));
}
else if ( v3 == 6 )
{
do
{
v4 = get_tlv_dated_element();
load_tlv_value((__int64)&dazibao, v4);
v5 = *(_DWORD *)v4;
if ( *(_DWORD *)v4 == 5 )
{
v7 = get_tlv_compound_elements(v4);
load_tlv_init(v7, **(unsigned int **)(v4 + 24));
v5 = *(_DWORD *)v4;
}
}
while ( v5 == 6 );
}
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 *a1, int a2)
{
__int64 *v2; // rbp
int v3; // eax
__int64 v4; // rbx
int v5; // eax
__int64 v6; // rax
__int64 v7; // rax

if ( a2 > 0 )
{
v2 = a1;
do
{
func7((__int64)&dazibao, *v2);
v3 = *(_DWORD *)*v2;
if ( v3 == 5 )
{
v6 = ((__int64 (*)(void))get_tlv_compound_elements)();
func8(v6, **(unsigned int **)(*v2 + 24));
}
else if ( v3 == 6 )
{
do
{
v4 = get_tlv_dated_element();
func7((__int64)&dazibao, v4);
v5 = *(_DWORD *)v4;
if ( *(_DWORD *)v4 == 5 )
{
v7 = get_tlv_compound_elements(v4);
func8(v7, **(unsigned int **)(v4 + 24));
v5 = *(_DWORD *)v4;
}
}
while ( v5 == 6 );
}
++v2;
}
while ( v2 != &a1[(unsigned int)(a2 - 1) + 1] );
}
}

original_code:
void load_tlv_init(Dazibao_TLV **element, int nb_elem){
  int i = 0;
  Dazibao_TLV *aux = ((void *)0);
  Dazibao_TLV **aux_tab = ((void *)0);
  for(; i < nb_elem; i++){
    load_tlv_value(&dazibao, element[i]);
    switch(element[i]->type){
    case DATED:
      do{
 aux = get_tlv_dated_element(element[i]);
 load_tlv_value(&dazibao, aux);
 if(aux->type == COMPOUND){
   aux_tab = get_tlv_compound_elements(aux);
   load_tlv_init(aux_tab, ((Dazibao_TLV_Compound_Value *)aux->value)->count);
 }
      }while(aux->type == DATED);
      break;
    case COMPOUND:
      aux_tab = get_tlv_compound_elements(element[i]);
      load_tlv_init(aux_tab, ((Dazibao_TLV_Compound_Value *)element[i]->value)->count);
      break;
    default:;
    }
  }
}

canonical_original_code:
void load_tlv_init(struct Dazibao_TLV **element, int nb_elem){
  int i = 0;
  struct Dazibao_TLV *aux = ((void *)0);
  struct Dazibao_TLV **aux_tab = ((void *)0);
  for(; i < nb_elem; i++){
    load_tlv_value(&dazibao, element[i]);
    switch(element[i]->type){
    case DATED:
      do{
 aux = get_tlv_dated_element(element[i]);
 load_tlv_value(&dazibao, aux);
 if(aux->type == COMPOUND){
   aux_tab = get_tlv_compound_elements(aux);
   load_tlv_init(aux_tab, ((struct Dazibao_TLV_Compound_Value *)aux->value)->count);
 }
      }while(aux->type == DATED);
      break;
    case COMPOUND:
      aux_tab = get_tlv_compound_elements(element[i]);
      load_tlv_init(aux_tab, ((struct Dazibao_TLV_Compound_Value *)element[i]->value)->count);
      break;
    default:;
    }
  }
}

variable_types: i int


decompiled_code:
int __fastcall print_tlv_header(unsigned int *a1)
{
unsigned int v1; // er12
__int64 v2; // rbx
const char *v3; // rax

v1 = a1[4];
v2 = *((_QWORD *)a1 + 1);
v3 = (const char *)tlv_type_to_text(*a1);
return printf("[+] TLV | Type %s | at %d | length %d\n", v3, (unsigned int)v2, v1);
}

canonical_decompiled_code:
int __fastcall func9(unsigned int *a1)
{
unsigned int v1; // er12
__int64 v2; // rbx
const char *v3; // rax

v1 = a1[4];
v2 = *((_QWORD *)a1 + 1);
v3 = (const char *)tlv_type_to_text(*a1);
return printf("[+] TLV | Type %s | at %d | length %d\n", v3, (unsigned int)v2, v1);
}

original_code:
void print_tlv_header(Dazibao_TLV *tlv) {
    printf("[+] TLV | Type %s | at %d | length %d\n", tlv_type_to_text(tlv->type), (int)tlv->position, tlv->length);
}

canonical_original_code:
void print_tlv_header(struct Dazibao_TLV *tlv) {
    printf("[+] TLV | Type %s | at %d | length %d\n", tlv_type_to_text(tlv->type), (int)tlv->position, tlv->length);
}


decompiled_code:
void *__fastcall safe_malloc(int a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
perror("Malloc Error:");
exit(1);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
perror("Malloc Error:");
exit(1);
}
return result;
}

original_code:
void *safe_malloc(int n) {
    void *p = malloc(n);
    if ( p == 0 ) {
        perror("Malloc Error:");
        exit(1);
    }
    return p;
}

variable_types: n int

variable_types:
p
void *

return_type: void *


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

if ( !a1 )
return safe_malloc(a2);
result = realloc(a1, a2);
if ( !result )
{
perror("Realloc Error:");
exit(1);
}
return result;
}

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

if ( !a1 )
return func0(a2);
result = realloc(a1, a2);
if ( !result )
{
perror("Realloc Error:");
exit(1);
}
return result;
}

original_code:
void *safe_realloc(void *p, int n) {
    void *p_new;
    if ( p == 0 ) {
        p_new = safe_malloc(n);
    } else {
        p_new = realloc(p, n);
        if ( p_new == 0 ) {
            perror("Realloc Error:");
            exit(1);
        }
    }
    return p_new;
}


decompiled_code:
void __fastcall safe_free(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func2(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void safe_free(void *p) {
    if (p) {
        free(p);
    }
}


original_code:
void dazibao_write_tlv_header(Dazibao *dazibao, Dazibao_TLV *tlv) {
    char header[4];
    header[0] = tlv->type;
    header[1] = (tlv->length >> 16) & 0xFF;
    header[2] = (tlv->length >> 8) & 0xFF;
    header[3] = tlv->length & 0xFF;
    safe_write(dazibao->file_descriptor, header, 4);
}

canonical_original_code:
void dazibao_write_tlv_header(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    char header[4];
    header[0] = tlv->type;
    header[1] = (tlv->length >> 16) & 0xFF;
    header[2] = (tlv->length >> 8) & 0xFF;
    header[3] = tlv->length & 0xFF;
    safe_write(dazibao->file_descriptor, header, 4);
}

variable_types:
header
char [4]


decompiled_code:
__int64 __fastcall dazibao_write_raw_tlv(__int64 a1, __int64 a2)
{
puts("write tlv header");
dazibao_write_tlv_header(a1, a2);
puts("write tlv");
return safe_write(*(unsigned int *)(a1 + 8), *(_QWORD *)(a2 + 24), *(int *)(a2 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
puts("write tlv header");
func0(a1, a2);
puts("write tlv");
return safe_write(*(unsigned int *)(a1 + 8), *(_QWORD *)(a2 + 24), *(int *)(a2 + 16));
}

original_code:
void dazibao_write_raw_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    printf("write tlv header\n");
    dazibao_write_tlv_header(dazibao, tlv);
    printf("write tlv\n");
    safe_write(dazibao->file_descriptor, tlv->value, tlv->length);
}

canonical_original_code:
void dazibao_write_raw_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    printf("write tlv header\n");
    dazibao_write_tlv_header(dazibao, tlv);
    printf("write tlv\n");
    safe_write(dazibao->file_descriptor, tlv->value, tlv->length);
}


decompiled_code:
int __fastcall dazibao_write_tlv(__int64 a1, int *a2)
{
int v2; // eax

v2 = *a2;
if ( *a2 == 5 )
return dazibao_write_compound_tlv();
if ( v2 > 5 )
{
if ( v2 == 6 )
return dazibao_write_dated_tlv();
}
else if ( (unsigned int)(v2 - 1) <= 3 )
{
return dazibao_write_raw_tlv(a1, (__int64)a2);
}
return puts("write_tlv : UNKNOW TLV TYPE!");
}

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

v2 = *a2;
if ( *a2 == 5 )
return func3();
if ( v2 > 5 )
{
if ( v2 == 6 )
return func4();
}
else if ( (unsigned int)(v2 - 1) <= 3 )
{
return func1(a1, (__int64)a2);
}
return puts("write_tlv : UNKNOW TLV TYPE!");
}

original_code:
void dazibao_write_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    switch (tlv->type) {
    case PADN:
    case TEXT:
    case PNG:
    case JPEG:
        dazibao_write_raw_tlv(dazibao, tlv);
        break;
    case DATED:
        dazibao_write_dated_tlv(dazibao, tlv);
        break;
    case COMPOUND:
        dazibao_write_compound_tlv(dazibao, tlv);
        break;
    default:
        printf("write_tlv : UNKNOW TLV TYPE!\n");
    }
}

canonical_original_code:
void dazibao_write_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    switch (tlv->type) {
    case PADN:
    case TEXT:
    case PNG:
    case JPEG:
        dazibao_write_raw_tlv(dazibao, tlv);
        break;
    case DATED:
        dazibao_write_dated_tlv(dazibao, tlv);
        break;
    case COMPOUND:
        dazibao_write_compound_tlv(dazibao, tlv);
        break;
    default:
        printf("write_tlv : UNKNOW TLV TYPE!\n");
    }
}


decompiled_code:
__int64 __fastcall dazibao_write_compound_tlv(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v4; // rbx
int *v5; // rsi

dazibao_write_tlv_header(a1, a2);
result = *(_QWORD *)(a2 + 24);
if ( *(int *)result > 0 )
{
v4 = 0LL;
do
{
v5 = *(int **)(*(_QWORD *)(result + 8) + 8 * v4++);
dazibao_write_tlv(a1, v5);
result = *(_QWORD *)(a2 + 24);
}
while ( *(_DWORD *)result > (int)v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v4; // rbx
int *v5; // rsi

func0(a1, a2);
result = *(_QWORD *)(a2 + 24);
if ( *(int *)result > 0 )
{
v4 = 0LL;
do
{
v5 = *(int **)(*(_QWORD *)(result + 8) + 8 * v4++);
func2(a1, v5);
result = *(_QWORD *)(a2 + 24);
}
while ( *(_DWORD *)result > (int)v4 );
}
return result;
}

original_code:
void dazibao_write_compound_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    int i = 0;
    dazibao_write_tlv_header(dazibao, tlv);
    for (i = 0; i < ((Dazibao_TLV_Compound_Value *)(tlv->value))->count; i++) {
        dazibao_write_tlv(dazibao, ((Dazibao_TLV_Compound_Value *)(tlv->value))->elements[i]);
    }
}

canonical_original_code:
void dazibao_write_compound_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    int i = 0;
    dazibao_write_tlv_header(dazibao, tlv);
    for (i = 0; i < ((struct Dazibao_TLV_Compound_Value *)(tlv->value))->count; i++) {
        dazibao_write_tlv(dazibao, ((struct Dazibao_TLV_Compound_Value *)(tlv->value))->elements[i]);
    }
}


decompiled_code:
int __fastcall dazibao_write_dated_tlv(__int64 a1, __int64 a2)
{
int *v2; // r13
unsigned __int32 v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (int *)get_tlv_dated_element(a2);
v4[0] = _byteswap_ulong(get_tlv_dated_timestamp(a2));
dazibao_write_tlv_header(a1, a2);
safe_write(*(unsigned int *)(a1 + 8), v4, 4LL);
return dazibao_write_tlv(a1, v2);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, __int64 a2)
{
int *v2; // r13
unsigned __int32 v4[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = (int *)get_tlv_dated_element(a2);
v4[0] = _byteswap_ulong(get_tlv_dated_timestamp(a2));
func0(a1, a2);
safe_write(*(unsigned int *)(a1 + 8), v4, 4LL);
return func2(a1, v2);
}

original_code:
void dazibao_write_dated_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    char date[4];
    Dazibao_TLV *element = get_tlv_dated_element(tlv);
    long timestamp = get_tlv_dated_timestamp(tlv);
    date[0] = (timestamp >> 24) & 0xFF;
    date[1] = (timestamp >> 16) & 0xFF;
    date[2] = (timestamp >> 8) & 0xFF;
    date[3] = timestamp & 0xFF;
    dazibao_write_tlv_header(dazibao, tlv);
    safe_write(dazibao->file_descriptor, date, 4);
    dazibao_write_tlv(dazibao, element);
}

canonical_original_code:
void dazibao_write_dated_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    char date[4];
    struct Dazibao_TLV *element = get_tlv_dated_element(tlv);
    long timestamp = get_tlv_dated_timestamp(tlv);
    date[0] = (timestamp >> 24) & 0xFF;
    date[1] = (timestamp >> 16) & 0xFF;
    date[2] = (timestamp >> 8) & 0xFF;
    date[3] = timestamp & 0xFF;
    dazibao_write_tlv_header(dazibao, tlv);
    safe_write(dazibao->file_descriptor, date, 4);
    dazibao_write_tlv(dazibao, element);
}

variable_types:
date
char [4]

variable_types: timestamp long


decompiled_code:
__int64 __fastcall dazibao_append_tlv(__int64 a1, int *a2)
{
int v3; // edi

printf("Appending TLV Type %d at %d\n", (unsigned int)*a2, *(unsigned int *)(a1 + 12));
v3 = *(_DWORD *)(a1 + 8);
*((_QWORD *)a2 + 1) = *(int *)(a1 + 12);
lseek(v3, 0LL, 2);
dazibao_write_tlv(a1, a2);
puts("[+] Appened");
return dazibao_get_file_size(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int *a2)
{
int v3; // edi

printf("Appending TLV Type %d at %d\n", (unsigned int)*a2, *(unsigned int *)(a1 + 12));
v3 = *(_DWORD *)(a1 + 8);
*((_QWORD *)a2 + 1) = *(int *)(a1 + 12);
lseek(v3, 0LL, 2);
func2(a1, a2);
puts("[+] Appened");
return dazibao_get_file_size(a1);
}

original_code:
void dazibao_append_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    printf("Appending TLV Type %d at %d\n", tlv->type, dazibao->file_size);
    tlv->position = dazibao->file_size;
    lseek(dazibao->file_descriptor, 0, 2);
    dazibao_write_tlv(dazibao, tlv);
    printf("[+] Appened\n");
    dazibao_get_file_size(dazibao);
}

canonical_original_code:
void dazibao_append_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    printf("Appending TLV Type %d at %d\n", tlv->type, dazibao->file_size);
    tlv->position = dazibao->file_size;
    lseek(dazibao->file_descriptor, 0, 2);
    dazibao_write_tlv(dazibao, tlv);
    printf("[+] Appened\n");
    dazibao_get_file_size(dazibao);
}

variable_types:
tlv
struct Dazibao_TLV *


decompiled_code:
ssize_t __fastcall dazibao_remove_tlv(__int64 a1, __int64 a2)
{
__int64 v3; // rdx
__int64 v4; // rsi
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v3 = *(unsigned int *)(a2 + 16);
v4 = *(unsigned int *)(a2 + 8);
buf[0] = 1;
printf("Removing p%d:l%d from file.\n", v4, v3);
lseek(*(_DWORD *)(a1 + 8), *(_QWORD *)(a2 + 8), 0);
return write(*(_DWORD *)(a1 + 8), buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v3; // rdx
__int64 v4; // rsi
char buf[25]; // [rsp+Fh] [rbp-19h] BYREF

v3 = *(unsigned int *)(a2 + 16);
v4 = *(unsigned int *)(a2 + 8);
buf[0] = 1;
printf("Removing p%d:l%d from file.\n", v4, v3);
lseek(*(_DWORD *)(a1 + 8), *(_QWORD *)(a2 + 8), 0);
return write(*(_DWORD *)(a1 + 8), buf, 1uLL);
}

original_code:
void dazibao_remove_tlv(Dazibao *dazibao, Dazibao_TLV *tlv) {
    char buffer = PADN;
    printf("Removing p%d:l%d from file.\n", (int)tlv->position, tlv->length);
    lseek(dazibao->file_descriptor, tlv->position, 0);
    write(dazibao->file_descriptor, &buffer, 1);
}

canonical_original_code:
void dazibao_remove_tlv(struct Dazibao *dazibao, struct Dazibao_TLV *tlv) {
    char buffer = PADN;
    printf("Removing p%d:l%d from file.\n", (int)tlv->position, tlv->length);
    lseek(dazibao->file_descriptor, tlv->position, 0);
    write(dazibao->file_descriptor, &buffer, 1);
}

variable_types: buffer char


deletePad

decompiled_code:
__int64 __fastcall deletePad(unsigned int a1, int a2, unsigned int a3)
{
int v4; // ebx
__off_t v5; // r14
unsigned int v6; // er15
__int64 v7; // r12
__off_t v8; // rax
__off_t v9; // rax
__int64 v10; // r14
__int64 v11; // r15
char buf; // [rsp+18h] [rbp-40h] BYREF
char v14[3]; // [rsp+19h] [rbp-3Fh] BYREF
char v15[60]; // [rsp+1Ch] [rbp-3Ch] BYREF

while ( (int)read(a1, &buf, 1uLL) > 0 && lseek(a1, 0LL, 1) <= a2 )
{
if ( buf )
{
if ( !(unsigned int)safe_read(a1, v14, 3LL) )
goto LABEL_36;
v4 = (unsigned __int8)v14[2] + (((unsigned __int8)v14[1] + ((unsigned __int8)v14[0] << 8)) << 8);
if ( buf == 1 )
{
v9 = lseek(a1, 0LL, 1);
if ( !(unsigned int)safe_lseek(a1, v9 + v4, 0LL) )
goto LABEL_22;
a3 += v4 + 4;
}
else if ( (unsigned __int8)(buf - 5) > 1u )
{
if ( a3 )
{
v10 = safe_malloc((unsigned int)v4);
if ( !v10 )
exit(1);
if ( !(unsigned int)safe_read(a1, v10, v4) )
goto LABEL_36;
v11 = lseek(a1, 0LL, 1);
if ( v11 < 0 || !lseek(a1, v11 - (int)(v4 + a3 + 4), 0) )
LABEL_22:
exit(4);
if ( !(unsigned int)safe_write(a1, &buf, 1LL)
|| !(unsigned int)safe_write(a1, v14, 3LL)
|| !(unsigned int)safe_write(a1, v10, v4) )
{
LABEL_38:
exit(6);
}
if ( !lseek(a1, v11, 0) )
goto LABEL_22;
safe_free(v10);
}
else
{
v8 = lseek(a1, 0LL, 1);
if ( !(unsigned int)safe_lseek(a1, v4 + v8, 0LL) )
goto LABEL_22;
}
}
else
{
v5 = lseek(a1, 0LL, 1);
if ( v5 == -1 )
goto LABEL_22;
if ( buf == 6 && !(unsigned int)safe_read(a1, v15, 4LL) )
LABEL_36:
exit(5);
v6 = deletePad(a1, (unsigned int)(v4 + v5), a3);
if ( v6 )
{
if ( !(unsigned int)safe_lseek(a1, v5 - (int)(a3 + 4), 0LL) )
goto LABEL_22;
v7 = convert_int_to_char(v4 - (v6 - a3));
if ( !(unsigned int)safe_write(a1, &buf, 1LL)
|| !(unsigned int)safe_write(a1, v7, 1LL)
|| !(unsigned int)safe_write(a1, v7 + 1, 1LL)
|| !(unsigned int)safe_write(a1, v7 + 2, 1LL)
|| buf == 6 && !(unsigned int)safe_write(a1, v15, 4LL) )
{
goto LABEL_38;
}
safe_free(v7);
}
if ( !(unsigned int)safe_lseek(a1, v5 + v4, 0LL) )
goto LABEL_22;
a3 = v6;
}
}
else
{
++a3;
}
}
return a3;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, int a2, unsigned int a3)
{
int v4; // ebx
__off_t v5; // r14
unsigned int v6; // er15
__int64 v7; // r12
__off_t v8; // rax
__off_t v9; // rax
__int64 v10; // r14
__int64 v11; // r15
char buf; // [rsp+18h] [rbp-40h] BYREF
char v14[3]; // [rsp+19h] [rbp-3Fh] BYREF
char v15[60]; // [rsp+1Ch] [rbp-3Ch] BYREF

while ( (int)read(a1, &buf, 1uLL) > 0 && lseek(a1, 0LL, 1) <= a2 )
{
if ( buf )
{
if ( !(unsigned int)safe_read(a1, v14, 3LL) )
goto LABEL_36;
v4 = (unsigned __int8)v14[2] + (((unsigned __int8)v14[1] + ((unsigned __int8)v14[0] << 8)) << 8);
if ( buf == 1 )
{
v9 = lseek(a1, 0LL, 1);
if ( !(unsigned int)safe_lseek(a1, v9 + v4, 0LL) )
goto LABEL_22;
a3 += v4 + 4;
}
else if ( (unsigned __int8)(buf - 5) > 1u )
{
if ( a3 )
{
v10 = safe_malloc((unsigned int)v4);
if ( !v10 )
exit(1);
if ( !(unsigned int)safe_read(a1, v10, v4) )
goto LABEL_36;
v11 = lseek(a1, 0LL, 1);
if ( v11 < 0 || !lseek(a1, v11 - (int)(v4 + a3 + 4), 0) )
LABEL_22:
exit(4);
if ( !(unsigned int)safe_write(a1, &buf, 1LL)
|| !(unsigned int)safe_write(a1, v14, 3LL)
|| !(unsigned int)safe_write(a1, v10, v4) )
{
LABEL_38:
exit(6);
}
if ( !lseek(a1, v11, 0) )
goto LABEL_22;
safe_free(v10);
}
else
{
v8 = lseek(a1, 0LL, 1);
if ( !(unsigned int)safe_lseek(a1, v4 + v8, 0LL) )
goto LABEL_22;
}
}
else
{
v5 = lseek(a1, 0LL, 1);
if ( v5 == -1 )
goto LABEL_22;
if ( buf == 6 && !(unsigned int)safe_read(a1, v15, 4LL) )
LABEL_36:
exit(5);
v6 = func0(a1, (unsigned int)(v4 + v5), a3);
if ( v6 )
{
if ( !(unsigned int)safe_lseek(a1, v5 - (int)(a3 + 4), 0LL) )
goto LABEL_22;
v7 = convert_int_to_char(v4 - (v6 - a3));
if ( !(unsigned int)safe_write(a1, &buf, 1LL)
|| !(unsigned int)safe_write(a1, v7, 1LL)
|| !(unsigned int)safe_write(a1, v7 + 1, 1LL)
|| !(unsigned int)safe_write(a1, v7 + 2, 1LL)
|| buf == 6 && !(unsigned int)safe_write(a1, v15, 4LL) )
{
goto LABEL_38;
}
safe_free(v7);
}
if ( !(unsigned int)safe_lseek(a1, v5 + v4, 0LL) )
goto LABEL_22;
a3 = v6;
}
}
else
{
++a3;
}
}
return a3;
}

original_code:
int deletePad(int f, int sizeLoop, int val) {
 off_t tmpPos;
 off_t curRead;
 int fr;
 int size;
 int tmpVal;
 int shift_start;
 int shift_end;
 int shift = val;
 char *buf_copy;
 unsigned char date[4];
 unsigned char buff;
 unsigned char length[3];
 unsigned char *newLength;
 while (((fr = read(f, &buff, 1)) > 0) && (lseek(f,0,1) <= sizeLoop)) {
  if (buff == 0) {
   shift = shift + 1;
  } else {
   if (!safe_read(f, &length, sizeof(length))) exit(5);
   size = length[0] * 256 * 256 + length[1] * 256 + length[2];
   switch(buff) {
    case 1:
     if (!safe_lseek(f, ( lseek(f, 0, 1) + size) , 0)) exit(4);
     shift = shift + size + 4;
     break;
    case 5:
    case 6:
     tmpPos = lseek(f, 0, 1);
     if (tmpPos == - 1) exit(4);
     if (buff == 6) {
      if (!safe_read(f, &date, 4)) exit(5);
     }
     shift_start = shift;
     tmpVal = deletePad(f, (tmpPos + size), shift);
     if(tmpVal != 0) {
      if (!safe_lseek(f, tmpPos - (shift + 4), 0)) exit(4);
      shift_end = tmpVal - shift_start;
      newLength = convert_int_to_char(size - shift_end);
      if (!safe_write(f, &buff, 1)) exit(6);
      if (!safe_write(f, &newLength[0], 1)) exit(6);
      if (!safe_write(f, &newLength[1], 1)) exit(6);
      if (!safe_write(f, &newLength[2], 1)) exit(6);
      if (buff == 6) {
       if (!safe_write(f, &date, 4)) exit(6);
      }
      safe_free(newLength);
     }
     if (!safe_lseek(f, (tmpPos + size), 0)) exit(4);
     shift = tmpVal;
     break;
    default:
     if( shift == 0 ) {
      if (!safe_lseek(f, ( lseek(f, 0, 1) + size) , 0)) exit(4);
     } else {
      buf_copy = safe_malloc (size * sizeof(char));
      if (buf_copy == ((void *)0)) exit(1);
      if (!safe_read(f, buf_copy, size)) exit(5);
      curRead = lseek(f, 0, 1);
      if (curRead < 0) exit(4);
      if (!lseek(f, (curRead) - (shift + size + 4), 0)) exit(4);
      if (!safe_write(f, &buff, 1)) exit(6);
      if (!safe_write(f, &length, 3)) exit(6);
      if (!safe_write(f, buf_copy, size)) exit(6);
      if (!lseek(f, curRead, 0)) exit(4);
      safe_free(buf_copy);
     }
     break;
    }
   }
  }
 return(shift);
}

canonical_original_code:
int deletePad(int f, int sizeLoop, int val) {
 long tmpPos;
 long curRead;
 int fr;
 int size;
 int tmpVal;
 int shift_start;
 int shift_end;
 int shift = val;
 char *buf_copy;
 unsigned char date[4];
 unsigned char buff;
 unsigned char length[3];
 unsigned char *newLength;
 while (((fr = read(f, &buff, 1)) > 0) && (lseek(f,0,1) <= sizeLoop)) {
  if (buff == 0) {
   shift = shift + 1;
  } else {
   if (!safe_read(f, &length, sizeof(length))) exit(5);
   size = length[0] * 256 * 256 + length[1] * 256 + length[2];
   switch(buff) {
    case 1:
     if (!safe_lseek(f, ( lseek(f, 0, 1) + size) , 0)) exit(4);
     shift = shift + size + 4;
     break;
    case 5:
    case 6:
     tmpPos = lseek(f, 0, 1);
     if (tmpPos == - 1) exit(4);
     if (buff == 6) {
      if (!safe_read(f, &date, 4)) exit(5);
     }
     shift_start = shift;
     tmpVal = deletePad(f, (tmpPos + size), shift);
     if(tmpVal != 0) {
      if (!safe_lseek(f, tmpPos - (shift + 4), 0)) exit(4);
      shift_end = tmpVal - shift_start;
      newLength = convert_int_to_char(size - shift_end);
      if (!safe_write(f, &buff, 1)) exit(6);
      if (!safe_write(f, &newLength[0], 1)) exit(6);
      if (!safe_write(f, &newLength[1], 1)) exit(6);
      if (!safe_write(f, &newLength[2], 1)) exit(6);
      if (buff == 6) {
       if (!safe_write(f, &date, 4)) exit(6);
      }
      safe_free(newLength);
     }
     if (!safe_lseek(f, (tmpPos + size), 0)) exit(4);
     shift = tmpVal;
     break;
    default:
     if( shift == 0 ) {
      if (!safe_lseek(f, ( lseek(f, 0, 1) + size) , 0)) exit(4);
     } else {
      buf_copy = safe_malloc (size * sizeof(char));
      if (buf_copy == ((void *)0)) exit(1);
      if (!safe_read(f, buf_copy, size)) exit(5);
      curRead = lseek(f, 0, 1);
      if (curRead < 0) exit(4);
      if (!lseek(f, (curRead) - (shift + size + 4), 0)) exit(4);
      if (!safe_write(f, &buff, 1)) exit(6);
      if (!safe_write(f, &length, 3)) exit(6);
      if (!safe_write(f, buf_copy, size)) exit(6);
      if (!lseek(f, curRead, 0)) exit(4);
      safe_free(buf_copy);
     }
     break;
    }
   }
  }
 return(shift);
}

variable_types: f int

variable_types: sizeLoop int

variable_types: val int

variable_types: tmpPos long

variable_types: curRead long

variable_types: fr int

variable_types: size int

variable_types: tmpVal int

variable_types: shift int

variable_types:
date
unsigned char [4]

variable_types:
buff
unsigned char

variable_types:
length
unsigned char [3]

variable_types:
newLength
unsigned char *


callDeletePad

decompiled_code:
__int64 __fastcall callDeletePad(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
__int64 result; // rax

if ( !(unsigned int)safe_flock(a1, 2LL) )
goto LABEL_5;
if ( !(unsigned int)safe_lseek((unsigned int)a1, 4LL, 0LL) )
exit(4);
v2 = get_file_size(a2);
v3 = deletePad(a1, v2, 0);
v4 = get_file_size(a2);
if ( truncate(a2, (unsigned int)(v4 - v3)) == -1 )
{
perror("Truncate Error:");
exit(1);
}
result = safe_flock((unsigned int)a1, 8LL);
if ( !(_DWORD)result )
LABEL_5:
exit(7);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
int v2; // eax
int v3; // ebx
int v4; // eax
__int64 result; // rax

if ( !(unsigned int)safe_flock(a1, 2LL) )
goto LABEL_5;
if ( !(unsigned int)safe_lseek((unsigned int)a1, 4LL, 0LL) )
exit(4);
v2 = get_file_size(a2);
v3 = func0(a1, v2, 0);
v4 = get_file_size(a2);
if ( truncate(a2, (unsigned int)(v4 - v3)) == -1 )
{
perror("Truncate Error:");
exit(1);
}
result = safe_flock((unsigned int)a1, 8LL);
if ( !(_DWORD)result )
LABEL_5:
exit(7);
return result;
}

original_code:
void callDeletePad(int f, char *file) {
 int shift;
 if (!safe_flock(f, 2)) exit(7);
 if (!safe_lseek(f, 4, 0)) exit(4);
 shift = deletePad(f, get_file_size(file), 0);
 if (truncate(file, get_file_size(file) - shift) == -1) {
  perror("Truncate Error:");
  exit(1);
 }
 if (!safe_flock(f, 8)) exit(7);
}

variable_types:
file
char *


main

repo: tsalmon/Sokoban

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // rdx
__int64 v5; // rdx

if ( (unsigned int)SDL_Init() == -1 )
{
v4 = SDL_GetError();
fprintf(stderr, "SDL:Init error: %s\n", v4);
goto LABEL_5;
}
charge_images(32LL, argv);
ecran = SDL_SetVideoMode();
if ( !ecran )
{
v5 = SDL_GetError();
fprintf(stderr, "SDL:VIdeo error : %s\n", v5);
LABEL_5:
exit(1);
}
SDL_WM_SetCaption();
events("Sokoban", 0LL);
SDL_Quit();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v4; // rdx
__int64 v5; // rdx

if ( (unsigned int)SDL_Init() == -1 )
{
v4 = SDL_GetError();
fprintf(stderr, "SDL:Init error: %s\n", v4);
goto LABEL_5;
}
func7(32LL, argv);
ecran = SDL_SetVideoMode();
if ( !ecran )
{
v5 = SDL_GetError();
fprintf(stderr, "SDL:VIdeo error : %s\n", v5);
LABEL_5:
exit(1);
}
SDL_WM_SetCaption();
func12("Sokoban", 0LL);
SDL_Quit();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  if (SDL_Init(0x00000020) == -1)
    {
      fprintf(stderr, "SDL:Init error: %s\n", SDL_GetError());
      exit(1);
    }
  charge_images();
  if((ecran = SDL_SetVideoMode(408, 408, 32, 0x00000001 | 0x80000000 )) == ((void *)0))
    {
      fprintf(stderr, "SDL:VIdeo error : %s\n", SDL_GetError());
      exit(1);
    }
  SDL_WM_SetCaption("Sokoban", ((void *)0));
  events();
  SDL_Quit();
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


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

position = 0;
menu = IMG_Load();
mur = IMG_Load();
caisse = IMG_Load();
caisse_ok = IMG_Load();
cible = IMG_Load();
mario = IMG_Load();
mario_right = IMG_Load();
mario_left = IMG_Load();
result = IMG_Load();
mario_down = result;
return result;
}

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

position = 0;
menu = IMG_Load();
mur = IMG_Load();
caisse = IMG_Load();
caisse_ok = IMG_Load();
cible = IMG_Load();
mario = IMG_Load();
mario_right = IMG_Load();
mario_left = IMG_Load();
result = IMG_Load();
mario_down = result;
return result;
}

original_code:
void charge_images()
{
  position.x = 0;
  position.y = 0;
  menu = IMG_Load("images/10121.jpg");
  mur = IMG_Load("images/10113.jpg");
  caisse = IMG_Load("images/10114.jpg");
  caisse_ok = IMG_Load("images/10117.jpg");
  cible = IMG_Load("images/10118.png");
  mario = IMG_Load("images/45.gif");
  mario_right = IMG_Load("images/46.gif");
  mario_left = IMG_Load("images/47.gif");
  mario_down = IMG_Load("images/48.gif");}


dessin

decompiled_code:
__int64 dessin()
{
char *v0; // rbp
char *v1; // rbx
__int64 result; // rax

v0 = (char *)&unk_404209;
SDL_MapRGB();
SDL_FillRect();
do
{
v1 = v0 - 169;
do
{
switch ( *v1 )
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
SDL_UpperBlit();
break;
default:
break;
}
v1 += 13;
LOWORD(position) = position + 34;
}
while ( v0 != v1 );
++v0;
HIWORD(position) += 34;
LOWORD(position) = 0;
}
while ( v0 != (_BYTE *)&marioX + 2 );
LOWORD(position) = 34 * marioX;
HIWORD(position) = 34 * marioY;
result = SDL_UpperBlit();
position = 0;
return result;
}

canonical_decompiled_code:
__int64 func8()
{
char *v0; // rbp
char *v1; // rbx
__int64 result; // rax

v0 = (char *)&unk_404209;
SDL_MapRGB();
SDL_FillRect();
do
{
v1 = v0 - 169;
do
{
switch ( *v1 )
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
SDL_UpperBlit();
break;
default:
break;
}
v1 += 13;
LOWORD(position) = position + 34;
}
while ( v0 != v1 );
++v0;
HIWORD(position) += 34;
LOWORD(position) = 0;
}
while ( v0 != (_BYTE *)&marioX + 2 );
LOWORD(position) = 34 * marioX;
HIWORD(position) = 34 * marioY;
result = SDL_UpperBlit();
position = 0;
return result;
}

original_code:
void dessin()
{
  int i = 0, j = 0;
  SDL_FillRect(ecran, ((void *)0), SDL_MapRGB(ecran->format, 255, 255, 255));
  for(j = 0; j < 13; j++)
    {
      for(i = 0; i < 13; i++)
 {
   switch(carte[i][j])
     {
     case '0':
       break;
     case '1':
       break;
     case '2':
       SDL_UpperBlit(mur, ((void *)0), ecran, &position);
       break;
     case '3':
       SDL_UpperBlit(caisse, ((void *)0), ecran, &position);
       break;
     case '4':
       SDL_UpperBlit(caisse_ok, ((void *)0), ecran, &position);
       break;
     case '5':
       SDL_UpperBlit(cible, ((void *)0), ecran, &position);
       break;
     case '6':
       SDL_UpperBlit(mario_left, ((void *)0), ecran, &position);
       break;
     case '7':
       SDL_UpperBlit(mario, ((void *)0), ecran, &position);
       break;
     case '8':
       SDL_UpperBlit(mario_right, ((void *)0), ecran, &position);
       break;
     case '9':
       SDL_UpperBlit(mario_down, ((void *)0), ecran, &position);
       break;
     }
   position.x += 34;
 }
      position.y = position.y + 34;
      position.x = 0;
    }
  position.x = marioX*34;
  position.y = marioY*34;
  SDL_UpperBlit(mario, ((void *)0), ecran, &position);
  position.x = 0;
  position.y = 0;
}

canonical_original_code:
void dessin()
{
  int i = 0; int j = 0;
  SDL_FillRect(ecran, ((void *)0), SDL_MapRGB(ecran->format, 255, 255, 255));
  for(j = 0; j < 13; j++)
    {
      for(i = 0; i < 13; i++)
 {
   switch(carte[i][j])
     {
     case '0':
       break;
     case '1':
       break;
     case '2':
       SDL_UpperBlit(mur, ((void *)0), ecran, &position);
       break;
     case '3':
       SDL_UpperBlit(caisse, ((void *)0), ecran, &position);
       break;
     case '4':
       SDL_UpperBlit(caisse_ok, ((void *)0), ecran, &position);
       break;
     case '5':
       SDL_UpperBlit(cible, ((void *)0), ecran, &position);
       break;
     case '6':
       SDL_UpperBlit(mario_left, ((void *)0), ecran, &position);
       break;
     case '7':
       SDL_UpperBlit(mario, ((void *)0), ecran, &position);
       break;
     case '8':
       SDL_UpperBlit(mario_right, ((void *)0), ecran, &position);
       break;
     case '9':
       SDL_UpperBlit(mario_down, ((void *)0), ecran, &position);
       break;
     }
   position.x += 34;
 }
      position.y = position.y + 34;
      position.x = 0;
    }
  position.x = marioX*34;
  position.y = marioY*34;
  SDL_UpperBlit(mario, ((void *)0), ecran, &position);
  position.x = 0;
  position.y = 0;
}

variable_types: i int

variable_types: j int


decompiled_code:
__int64 sokoban_free()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func11()
{
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
SDL_FreeSurface();
return SDL_Quit();
}

original_code:
void sokoban_free()
{
  SDL_FreeSurface(mur);
  SDL_FreeSurface(caisse);
  SDL_FreeSurface(caisse_ok);
  SDL_FreeSurface(cible);
  SDL_FreeSurface(mario);
  SDL_FreeSurface(mario_right);
  SDL_FreeSurface(mario_left);
  SDL_FreeSurface(mario_down);
  SDL_Quit();
}


main

repo: tseale/deadlocks

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // r13
int v4; // ebx
pthread_t *v5; // r12
_DWORD *v6; // rax
char v7; // dl
_DWORD *v8; // rbx
pthread_t *v9; // r14
int v10; // ebp
void *v11; // rcx
pthread_t *v12; // rdi
char v13; // bl
int v14; // eax
const char *v15; // rsi
__int64 v16; // rbx
pthread_t v17; // rdi

v3 = argv[1];
v4 = strlen(v3);
v5 = (pthread_t *)malloc(8LL * v4);
v6 = malloc(8LL * v4);
active_carts = v4;
v7 = *v3;
if ( *v3 )
{
v8 = v6;
v9 = v5;
v10 = 0;
do
{
*(_BYTE *)v8 = v7;
v11 = v8;
v12 = v9;
v8[1] = v10;
++v3;
++v10;
v8 += 2;
++v9;
pthread_create(v12, 0LL, manage_thread, v11);
v7 = *v3;
}
while ( *v3 );
if ( !active_carts )
goto LABEL_17;
}
else
{
v10 = 0;
if ( !v4 )
goto LABEL_19;
}
v13 = 0;
do
{
pthread_cond_signal(&northC);
pthread_cond_signal(&eastC);
pthread_cond_signal(&southC);
pthread_cond_signal(&westC);
sleep(3u);
if ( ncurr == 1 && ecurr == 1 && scurr == 1 && wcurr == 1 )
{
v14 = v13 & 3;
switch ( v14 )
{
case 2:
sdead = 1;
pthread_cond_signal(&southC);
v15 = "South";
break;
case 3:
wdead = 1;
pthread_cond_signal(&westC);
v15 = "West";
break;
case 1:
edead = 1;
pthread_cond_signal(&eastC);
v15 = "East";
break;
default:
ndead = 1;
pthread_cond_signal(&northC);
v15 = "North";
break;
}
++v13;
printf("DEADLOCK: CART jam detected, signalling %s to go\n", v15);
}
}
while ( active_carts );
if ( !v10 )
{
LABEL_19:
pthread_cond_destroy(&northQ);
pthread_cond_destroy(&eastQ);
pthread_cond_destroy(&southQ);
pthread_cond_destroy(&westQ);
pthread_cond_destroy(&northC);
pthread_cond_destroy(&eastC);
pthread_cond_destroy(&southC);
pthread_cond_destroy(&westC);
pthread_mutex_destroy(&intersection);
pthread_exit(0LL);
}
LABEL_17:
v16 = 0LL;
do
{
v17 = v5[v16++];
pthread_join(v17, 0LL);
}
while ( v10 > (int)v16 );
goto LABEL_19;
}

original_code:
int main (int argc, char* argv[]){
 pthread_t* trains = malloc((int)strlen(argv[1])*sizeof(pthread_t));
 train* cart = malloc((int)strlen(argv[1])*sizeof(train));
    char *str;
    int id;
    active_carts = (int)strlen(argv[1]);
 for(str=argv[1],id=0; *str; str++,id++){
  char c=*str;
  cart[id].direction=c;
  cart[id].trainID=id;
  pthread_create(&trains[id], ((void *)0), manage_thread, (void *)&cart[id]);
 }
    int deadlock_counter = 0;
    while(active_carts != 0){
        pthread_cond_signal(&northC);
        pthread_cond_signal(&eastC);
        pthread_cond_signal(&southC);
        pthread_cond_signal(&westC);
        sleep(3);
        if(ncurr==1 && ecurr==1 && scurr==1 && wcurr==1){
            char* direction;
            int next = deadlock_counter%4;
            switch(next){
                case 0:
                    direction="North";
                    ndead = 1;
                    pthread_cond_signal(&northC);
                    break;
                case 1:
                    direction="East";
                    edead = 1;
                    pthread_cond_signal(&eastC);
                    break;
                case 2:
                    direction="South";
                    sdead=1;
                    pthread_cond_signal(&southC);
                    break;
                case 3:
                    direction="West";
                    wdead=1;
                    pthread_cond_signal(&westC);
                    break;
                default:
                    break;
            }
            printf("DEADLOCK: CART jam detected, signalling %s to go\n",direction);
            deadlock_counter++;
        }
    }
    int i;
    for(i=0;i<id;i++){pthread_join(trains[i], ((void *)0));}
    pthread_cond_destroy(&northQ);pthread_cond_destroy(&eastQ);pthread_cond_destroy(&southQ);pthread_cond_destroy(&westQ);
    pthread_cond_destroy(&northC);pthread_cond_destroy(&eastC);pthread_cond_destroy(&southC);pthread_cond_destroy(&westC);
    pthread_mutex_destroy(&intersection);
    pthread_exit(((void *)0));
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
 unsigned long *trains = malloc((int)strlen(argv[1])*sizeof(pthread_t));
 struct train *cart = malloc((int)strlen(argv[1])*sizeof(train));
    char *str;
    int id;
    active_carts = (int)strlen(argv[1]);
 for(str=argv[1],id=0; *str; str++,id++){
  char c = *str;
  cart[id].direction=c;
  cart[id].trainID=id;
  pthread_create(&trains[id], ((void *)0), manage_thread, (void *)&cart[id]);
 }
    int deadlock_counter = 0;
    while(active_carts != 0){
        pthread_cond_signal(&northC);
        pthread_cond_signal(&eastC);
        pthread_cond_signal(&southC);
        pthread_cond_signal(&westC);
        sleep(3);
        if(ncurr==1 && ecurr==1 && scurr==1 && wcurr==1){
            char *direction;
            int next = deadlock_counter%4;
            switch(next){
                case 0:
                    direction="North";
                    ndead = 1;
                    pthread_cond_signal(&northC);
                    break;
                case 1:
                    direction="East";
                    edead = 1;
                    pthread_cond_signal(&eastC);
                    break;
                case 2:
                    direction="South";
                    sdead=1;
                    pthread_cond_signal(&southC);
                    break;
                case 3:
                    direction="West";
                    wdead=1;
                    pthread_cond_signal(&westC);
                    break;
                default:
                    break;
            }
            printf("DEADLOCK: CART jam detected, signalling %s to go\n",direction);
            deadlock_counter++;
        }
    }
    int i;
    for(i=0;i<id;i++){pthread_join(trains[i], ((void *)0));}
    pthread_cond_destroy(&northQ);pthread_cond_destroy(&eastQ);pthread_cond_destroy(&southQ);pthread_cond_destroy(&westQ);
    pthread_cond_destroy(&northC);pthread_cond_destroy(&eastC);pthread_cond_destroy(&southC);pthread_cond_destroy(&westC);
    pthread_mutex_destroy(&intersection);
    pthread_exit(((void *)0));
    return 0;
}

variable_types:
trains
unsigned long *

variable_types: struct train { char direction; int trainID; } *

variable_types:
str
char *

variable_types: id int

variable_types: c char

variable_types:
direction
char *

variable_types: next int