decompiled_code:
__int64 __fastcall queue_size(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return *(unsigned int *)(a1 + 16);
}

original_code:
unsigned int queue_size(queue_t *q) {
 return q->size;
}

canonical_original_code:
unsigned int queue_size(struct queue_t *q) {
 return q->size;
}

return_type: unsigned int

user_defined_types: struct queue_node { void *item; struct queue_node *next; }


repo: JLLLinn/System-Programming

decompiled_code:
__int64 __fastcall queue_dequeue(__int64 a1)
{
__int64 v2; // rbp
__int64 *v3; // rdi

v2 = 0LL;
if ( (unsigned int)queue_size(a1) )
{
v3 = *(__int64 **)a1;
*(_QWORD *)a1 = *(_QWORD *)(*(_QWORD *)a1 + 8LL);
--*(_DWORD *)(a1 + 16);
v2 = *v3;
free(v3);
if ( !(unsigned int)queue_size(a1) )
{
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
}
return v2;
}

original_code:
void *queue_dequeue(queue_t *q) {
 struct queue_node *front;
 void *item;
 if(queue_size(q) == 0) {
  return ((void *)0);
 }
 front = q->head;
 q->head = q->head->next;
 q->size--;
 item = front->item;
 free(front);
 if(queue_size(q) == 0) {
  q->head = ((void *)0);
  q->tail = ((void *)0);
 }
 return item;
}

canonical_original_code:
void *queue_dequeue(struct queue_t *q) {
 struct queue_node *front;
 void *item;
 if(queue_size(q) == 0) {
  return ((void *)0);
 }
 front = q->head;
 q->head = q->head->next;
 q->size--;
 item = front->item;
 free(front);
 if(queue_size(q) == 0) {
  q->head = ((void *)0);
  q->tail = ((void *)0);
 }
 return item;
}

variable_types: struct queue_t { struct queue_node { void *item; struct queue_node *next; } *head; struct queue_node *tail; unsigned int size; } *

variable_types:
item
void *

return_type: void *

user_defined_types: struct queue_t { struct queue_node *head; struct queue_node *tail; unsigned int size; }


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

while ( 1 )
{
result = queue_size(a1);
if ( !(_DWORD)result )
break;
queue_dequeue(a1);
}
return result;
}

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

while ( 1 )
{
result = func2(a1);
if ( !(_DWORD)result )
break;
func3(a1);
}
return result;
}

original_code:
void queue_destroy(queue_t *q) {
 while(queue_size(q) > 0) {
  queue_dequeue(q);
 }
}

canonical_original_code:
void queue_destroy(struct queue_t *q) {
 while(queue_size(q) > 0) {
  queue_dequeue(q);
 }
}

return_type: void


decompiled_code:
__int64 __fastcall queue_enqueue(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 result; // rax

v2 = malloc(0x10uLL);
*v2 = a2;
v2[1] = 0LL;
result = queue_size(a1);
if ( (_DWORD)result )
{
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8) = v2;
}
else
{
*(_QWORD *)a1 = v2;
}
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 16);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
_QWORD *v2; // rbp
__int64 result; // rax

v2 = malloc(0x10uLL);
*v2 = a2;
v2[1] = 0LL;
result = func2(a1);
if ( (_DWORD)result )
{
result = *(_QWORD *)(a1 + 8);
*(_QWORD *)(result + 8) = v2;
}
else
{
*(_QWORD *)a1 = v2;
}
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 16);
return result;
}

original_code:
void queue_enqueue(queue_t *q, void *item) {
 struct queue_node *back = malloc(sizeof(struct queue_node));
 back->item = item;
 back->next = ((void *)0);
 if(queue_size(q) == 0) {
  q->head = back;
 } else {
  q->tail->next = back;
 }
 q->tail = back;
 q->size++;
}

canonical_original_code:
void queue_enqueue(struct queue_t *q, void *item) {
 struct queue_node *back = malloc(sizeof(struct queue_node));
 back->item = item;
 back->next = ((void *)0);
 if(queue_size(q) == 0) {
  q->head = back;
 } else {
  q->tail->next = back;
 }
 q->tail = back;
 q->size++;
}

variable_types: struct queue_node { void *item; struct queue_node *next; } *


decompiled_code:
__int64 __fastcall queue_iterate(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // rbx

result = queue_size((__int64)a1);
if ( (_DWORD)result )
{
v5 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
result = a2(*v5, a3);
v5 = (_QWORD *)v5[1];
}
while ( v5 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 (__fastcall *a2)(_QWORD, __int64), __int64 a3)
{
__int64 result; // rax
_QWORD *v5; // rbx

result = func2((__int64)a1);
if ( (_DWORD)result )
{
v5 = (_QWORD *)*a1;
if ( *a1 )
{
do
{
result = a2(*v5, a3);
v5 = (_QWORD *)v5[1];
}
while ( v5 );
}
}
return result;
}

original_code:
void queue_iterate(queue_t *q, void (*iter_func)(void *, void *), void *arg) {
 struct queue_node *node;
 if(queue_size(q) == 0) {
  return;
 }
 node = q->head;
 while(node != ((void *)0)) {
  iter_func(node->item, arg);
  node = node->next;
 }
}

canonical_original_code:
void queue_iterate(struct queue_t *q, void (*iter_func)(void *, void *), void *arg) {
 struct queue_node *node;
 if(queue_size(q) == 0) {
  return;
 }
 node = q->head;
 while(node != ((void *)0)) {
  iter_func(node->item, arg);
  node = node->next;
 }
}

variable_types:
arg
void *


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

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

original_code:
static void free_adapter(void *arg, void *trash){
 free(arg);
}

variable_types:
trash
void *


decompiled_code:
__int64 __fastcall rule_finished(char *s1)
{
unsigned int i; // ebx
const char *v2; // rax

pthread_mutex_lock(&finish_q_mutex);
for ( i = 0; (unsigned int)queue_size(finished_rule_q) > i; ++i )
{
v2 = (const char *)queue_at(finished_rule_q, i);
if ( !strcmp(s1, v2) )
{
pthread_mutex_unlock(&finish_q_mutex);
return 1LL;
}
}
pthread_mutex_unlock(&finish_q_mutex);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s1)
{
unsigned int i; // ebx
const char *v2; // rax

pthread_mutex_lock(&finish_q_mutex);
for ( i = 0; (unsigned int)queue_size(finished_rule_q) > i; ++i )
{
v2 = (const char *)queue_at(finished_rule_q, i);
if ( !strcmp(s1, v2) )
{
pthread_mutex_unlock(&finish_q_mutex);
return 1LL;
}
}
pthread_mutex_unlock(&finish_q_mutex);
return 0LL;
}

original_code:
int rule_finished(char* rule_char_p) {
 int i = 0;
 pthread_mutex_lock(&finish_q_mutex);
 for (i = 0; i < queue_size(finished_rule_q); i++) {
  if (strcmp(rule_char_p, (char*) queue_at(finished_rule_q, i)) == 0) {
   pthread_mutex_unlock(&finish_q_mutex);
   return 1;
  }
 }
 pthread_mutex_unlock(&finish_q_mutex);
 return 0;
}

canonical_original_code:
int rule_finished(char *rule_char_p) {
 int i = 0;
 pthread_mutex_lock(&finish_q_mutex);
 for (i = 0; i < queue_size(finished_rule_q); i++) {
  if (strcmp(rule_char_p, (char *) queue_at(finished_rule_q, i)) == 0) {
   pthread_mutex_unlock(&finish_q_mutex);
   return 1;
  }
 }
 pthread_mutex_unlock(&finish_q_mutex);
 return 0;
}

variable_types: i int

return_type: int


main

canonical_name: main

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

OpenErrorFile(argc, argv, envp);
while ( 1 )
{
v3 = yylex();
if ( !v3 )
break;
PrintToken(v3, yytext, yylval);
}
CloseErrorFile(0LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax

OpenErrorFile(argc, argv, envp);
while ( 1 )
{
v3 = yylex();
if ( !v3 )
break;
func0(v3, yytext, yylval);
}
CloseErrorFile(0LL);
return 0;
}

original_code:
int main()
{
    TokenType token;
    OpenErrorFile();
    while ((token = (TokenType)yylex()) != 0)
        PrintToken(token, yytext, yylval);
    CloseErrorFile();
    return 0;
}

canonical_original_code:
int main()
{
    enum TokenType token;
    OpenErrorFile();
    while ((token = (enum TokenType)yylex()) != 0)
        PrintToken(token, yytext, yylval);
    CloseErrorFile();
    return 0;
}

variable_types: enum TokenType { T_OpenSign=256, T_CloseSign=257, T_Elements=258, T_Atributtes=259, T_Values=260, T_Text=261, T_Minus=262, T_Equal=263, T_Colon=264, T_Slash=265, T_QuestionMark=266, T_ExclamationMark=267, T_XML=268, T_PublicIdentifier=269, T_DOCTYPE=270, T_NumTokenTypes=271 }


agregarListaAtributos

decompiled_code:
void __fastcall agregarListaAtributos(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2)
{
*(_QWORD *)(a1 + 8) = a2;
}

original_code:
void agregarListaAtributos(Nodo_t* nodo, ListaAtributos_t* listaAtributos){
 nodo->listaAtributos= listaAtributos;
}

canonical_original_code:
void agregarListaAtributos(struct Nodo *nodo, struct ListaAtributos *listaAtributos){
 nodo->listaAtributos= listaAtributos;
}

variable_types: struct Nodo { char *nombreNodo; struct ListaAtributos { struct Atributo { char *nombreAtributo; char *valorAtributo; struct Atributo *siguiente; int filaAtributo; int columnaAtributo; int filaValor; int columnaValor; } *primerAtributo; } *listaAtributos; struct ListaTextos { struct Texto { char *texto; struct Texto *siguiente; int fila; int columna; } *primerTexto; } *listaTextos; struct Nodo *padre; struct Nodo *siguiente; struct ListaHijos { struct Nodo *primerHijo; } *listaHijos; } *

variable_types:
listaAtributos
struct ListaAtributos *

user_defined_types: struct Nodo { char *nombreNodo; struct ListaAtributos *listaAtributos; struct ListaTextos *listaTextos; struct Nodo *padre; struct Nodo *siguiente; struct ListaHijos *listaHijos; }

user_defined_types: struct ListaAtributos { struct Atributo *primerAtributo; }

user_defined_types: struct ListaTextos { struct Texto *primerTexto; }


crearListaAtributos

decompiled_code:
void *crearListaAtributos()
{
return malloc(8uLL);
}

canonical_decompiled_code:
void *func3()
{
return malloc(8uLL);
}

original_code:
ListaAtributos_t* crearListaAtributos(){
 ListaAtributos_t* listaAtributos = malloc(sizeof(ListaAtributos_t));
 return listaAtributos;
}

canonical_original_code:
struct ListaAtributos *crearListaAtributos(){
 struct ListaAtributos *listaAtributos = malloc(sizeof(ListaAtributos_t));
 return listaAtributos;
}

variable_types: struct ListaAtributos { struct Atributo { char *nombreAtributo; char *valorAtributo; struct Atributo *siguiente; int filaAtributo; int columnaAtributo; int filaValor; int columnaValor; } *primerAtributo; } *


agregarListaTextos

decompiled_code:
__int64 __fastcall agregarListaTextos(__int64 a1, __int64 *a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = **(_QWORD **)(a1 + 16);
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *a2;
*(_QWORD *)(v3 + 8) = *a2;
}
else
{
*(_QWORD *)(a1 + 16) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 *a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = **(_QWORD **)(a1 + 16);
if ( result )
{
do
{
v3 = result;
result = *(_QWORD *)(result + 8);
}
while ( result );
result = *a2;
*(_QWORD *)(v3 + 8) = *a2;
}
else
{
*(_QWORD *)(a1 + 16) = a2;
}
return result;
}

original_code:
void agregarListaTextos(Nodo_t* nodo, ListaTextos_t* listaTextos){
 Texto_t* tmpTexto = nodo->listaTextos->primerTexto;
 if(tmpTexto!=((void *)0)){
  while(tmpTexto->siguiente!=((void *)0)){
   tmpTexto= tmpTexto->siguiente;
  }
  tmpTexto->siguiente= listaTextos->primerTexto;
 }
 else{
  nodo->listaTextos= listaTextos;
 }
}

canonical_original_code:
void agregarListaTextos(struct Nodo *nodo, struct ListaTextos *listaTextos){
 struct Texto *tmpTexto = nodo->listaTextos->primerTexto;
 if(tmpTexto!=((void *)0)){
  while(tmpTexto->siguiente!=((void *)0)){
   tmpTexto= tmpTexto->siguiente;
  }
  tmpTexto->siguiente= listaTextos->primerTexto;
 }
 else{
  nodo->listaTextos= listaTextos;
 }
}

variable_types:
listaTextos
struct ListaTextos *

variable_types: struct Texto { char *texto; struct Texto *siguiente; int fila; int columna; } *

user_defined_types: struct ListaHijos { struct Nodo *primerHijo; }

user_defined_types: struct Atributo { char *nombreAtributo; char *valorAtributo; struct Atributo *siguiente; int filaAtributo; int columnaAtributo; int filaValor; int columnaValor; }


crearListaTextos

decompiled_code:
void *crearListaTextos()
{
return malloc(8uLL);
}

canonical_decompiled_code:
void *func6()
{
return malloc(8uLL);
}

original_code:
ListaTextos_t* crearListaTextos(){
 ListaTextos_t* listaTextos = malloc(sizeof(ListaTextos_t));
 return listaTextos;
}

canonical_original_code:
struct ListaTextos *crearListaTextos(){
 struct ListaTextos *listaTextos = malloc(sizeof(ListaTextos_t));
 return listaTextos;
}

variable_types: struct ListaTextos { struct Texto { char *texto; struct Texto *siguiente; int fila; int columna; } *primerTexto; } *


printArregloEntero

decompiled_code:
int __fastcall printArregloEntero(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx
int result; // eax

if ( a2 )
{
v2 = a1;
do
result = printf("\x1B[34m%i.\x1B[m", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return result;
}

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

if ( a2 )
{
v2 = a1;
do
result = printf("\x1B[34m%i.\x1B[m", *v2++);
while ( v2 != &a1[a2 - 1 + 1] );
}
return result;
}

original_code:
void printArregloEntero(int numero[30], int profundidad ){
 int i=0;
 while(profundidad--){
  printf("\e[34m" "%i.""\e[m",numero[i]);
  i++;
 }
}

canonical_original_code:
void printArregloEntero(int numero[30], int profundidad){
 int i = 0;
 while(profundidad--){
  printf("\e[34m" "%i.""\e[m",numero[i]);
  i++;
 }
}

variable_types:
numero
int [30]

variable_types: profundidad int


printTabs

decompiled_code:
int __fastcall printTabs(int a1)
{
int i; // ebx
int result; // eax

for ( i = a1; i; --i )
result = printf(" ");
return result;
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
int i; // ebx
int result; // eax

for ( i = a1; i; --i )
result = printf(" ");
return result;
}

original_code:
void printTabs(int cantidad){
 while(cantidad--){
  printf("    ");
 }
}

variable_types: cantidad int


imprimirDoctype

decompiled_code:
int __fastcall imprimirDoctype(const char **a1)
{
return printf(
" \x1B[34m1.\x1B[31m Doctype\x1B[32m valor 1=\x1B[33m%s \x1B[32mvalor 2 =\x1B[33m %s\n ",
*a1,
a1[1]);
}

canonical_decompiled_code:
int __fastcall func9(const char **a1)
{
return printf(
" \x1B[34m1.\x1B[31m Doctype\x1B[32m valor 1=\x1B[33m%s \x1B[32mvalor 2 =\x1B[33m %s\n ",
*a1,
a1[1]);
}

original_code:
void imprimirDoctype(DocType_t* doctype){
 printf("     ""\e[34m""1." "\e[31m"" Doctype""\e[32m" " valor 1=""\e[33m" "%s ""\e[32m""valor 2 =""\e[33m" " %s\n ",doctype->valor1, doctype->valor2);
}

canonical_original_code:
void imprimirDoctype(struct DocType *doctype){
 printf("     ""\e[34m""1." "\e[31m"" Doctype""\e[32m" " valor 1=""\e[33m" "%s ""\e[32m""valor 2 =""\e[33m" " %s\n ",doctype->valor1, doctype->valor2);
}

variable_types: struct DocType { char *valor1; char *valor2; } *


main

repo: JacksonZhangkun/coding

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
const char **v4; // rbp
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

v3 = (FILE *)stdin;
if ( argc != 1 )
{
v4 = argv;
*(_QWORD *)&argc = argv[1];
argv = (const char **)"r";
v3 = fopen(*(const char **)&argc, "r");
if ( !v3 )
{
fprintf(stderr, "%s:%s cannot open.\n", *v4, v4[1]);
exit(1);
}
}
word_initialize(*(_QWORD *)&argc, argv, envp);
while ( (unsigned int)get_word(v6, 1024LL, v3) != -1 )
add_word(v6);
dump_word(_bss_start);
word_finalize();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rbx
const char **v4; // rbp
char v6[1048]; // [rsp+0h] [rbp-418h] BYREF

v3 = (FILE *)stdin;
if ( argc != 1 )
{
v4 = argv;
*(_QWORD *)&argc = argv[1];
argv = (const char **)"r";
v3 = fopen(*(const char **)&argc, "r");
if ( !v3 )
{
fprintf(stderr, "%s:%s cannot open.\n", *v4, v4[1]);
exit(1);
}
}
func10(*(_QWORD *)&argc, argv, envp);
while ( (unsigned int)func13(v6, 1024LL, v3) != -1 )
func9(v6);
func11(_bss_start);
func12();
return 0;
}

original_code:
int main()
{
    char **line;
    ReadLineStatus status = READ_LINE_SUCCESS;
    status = read_line(stdin, line);
    printf("%s", *line);
    return 0;
}

canonical_original_code:
int main()
{
    char **line;
    enum ReadLineStatus status = READ_LINE_SUCCESS;
    status = read_line(stdin, line);
    printf("%s", *line);
    return 0;
}

variable_types:
line
char **

variable_types: enum ReadLineStatus { READ_LINE_SUCCESS=0, READ_LINE_EOF=1, READ_LINE_OUT_OF_MEMORY=2 }


decompiled_code:
__int64 __fastcall shift_array(int a1)
{
int v1; // esi
__int64 result; // rax
__int64 v3; // rdx

v1 = num_of_word;
result = (unsigned int)(num_of_word - 1);
if ( (int)result > a1 )
{
result = 16LL * num_of_word;
do
{
*(__int128 *)((char *)&word_array + result) = (__int128)_mm_loadu_si128((const __m128i *)((char *)&xmmword_4040B0
+ result));
v3 = result;
result -= 16LL;
}
while ( v3 != 16 * (v1 - (unsigned __int64)(unsigned int)(v1 - 2 - a1)) );
}
num_of_word = v1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
int v1; // esi
__int64 result; // rax
__int64 v3; // rdx

v1 = num_of_word;
result = (unsigned int)(num_of_word - 1);
if ( (int)result > a1 )
{
result = 16LL * num_of_word;
do
{
*(__int128 *)((char *)&word_array + result) = (__int128)_mm_loadu_si128((const __m128i *)((char *)&xmmword_4040B0
+ result));
v3 = result;
result -= 16LL;
}
while ( v3 != 16 * (v1 - (unsigned __int64)(unsigned int)(v1 - 2 - a1)) );
}
num_of_word = v1 + 1;
return result;
}

original_code:
static void shift_array(int index){
    int src;
    for (src = num_of_word - 1; src > index; src--)
    {
 word_array[src + 1] = word_array[src];
    }
    num_of_word++;
}

variable_types: index int

variable_types: src int


decompiled_code:
char *__fastcall my_strdup(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)malloc(v1 + 1);
return strcpy(v2, src);
}

canonical_decompiled_code:
char *__fastcall func8(char *src)
{
size_t v1; // rax
char *v2; // rax

v1 = strlen(src);
v2 = (char *)malloc(v1 + 1);
return strcpy(v2, src);
}

original_code:
static char *my_strdup(char *src)
{
    char *dest;
    dest = malloc(sizeof(char) * (strlen(src) + 1));
    strcpy(dest, src);
    return dest;
}

variable_types:
src
char *

variable_types:
dest
char *

return_type: char *


decompiled_code:
int __fastcall add_word(char *src)
{
char *v1; // rax
int v2; // er14
__int64 v3; // rbx
int v4; // ebp
__int64 v5; // rbp

v2 = num_of_word;
if ( num_of_word <= 0 )
{
v4 = 0;
}
else
{
v3 = 0LL;
while ( 1 )
{
v4 = v3;
LODWORD(v1) = strcmp(*(const char **)&word_array[v3], src);
if ( (int)v1 >= 0 )
break;
v4 = ++v3;
if ( v3 == v2 )
goto LABEL_5;
}
}
if ( (_DWORD)v1 || !v2 )
{
LABEL_5:
shift_array(v4);
v1 = my_strdup(src);
v5 = v4;
*(_QWORD *)&word_array[v5] = v1;
*(_DWORD *)(v5 * 16 + 4210888) = 1;
}
else
{
++DWORD2(word_array[v4]);
}
return (int)v1;
}

canonical_decompiled_code:
int __fastcall func9(char *src)
{
char *v1; // rax
int v2; // er14
__int64 v3; // rbx
int v4; // ebp
__int64 v5; // rbp

v2 = num_of_word;
if ( num_of_word <= 0 )
{
v4 = 0;
}
else
{
v3 = 0LL;
while ( 1 )
{
v4 = v3;
LODWORD(v1) = strcmp(*(const char **)&word_array[v3], src);
if ( (int)v1 >= 0 )
break;
v4 = ++v3;
if ( v3 == v2 )
goto LABEL_5;
}
}
if ( (_DWORD)v1 || !v2 )
{
LABEL_5:
func7(v4);
v1 = func8(src);
v5 = v4;
*(_QWORD *)&word_array[v5] = v1;
*(_DWORD *)(v5 * 16 + 4210888) = 1;
}
else
{
++DWORD2(word_array[v4]);
}
return (int)v1;
}

original_code:
void add_word(char *word)
{
    int i;
    int result;
    for (i = 0; i < num_of_word; i++){
 result = strcmp(word_array[i].name, word);
 if (result >= 0)
     break;
    }
    if (num_of_word != 0 && result == 0)
 word_array[i].count++;
    else
    {
 shift_array(i);
 word_array[i].name = my_strdup(word);
 word_array[i].count = 1;
    }
}

variable_types:
word
char *

variable_types: i int

variable_types: result int


decompiled_code:
void word_initialize()
{
num_of_word = 0;
}

canonical_decompiled_code:
void func10()
{
num_of_word = 0;
}

original_code:
void word_initialize(void){
    num_of_word = 0;
}

canonical_original_code:
void word_initialize(){
    num_of_word = 0;
}


decompiled_code:
void word_finalize()
{
__int64 v0; // rbx

if ( num_of_word <= 0 )
{
num_of_word = 0;
}
else
{
v0 = 0LL;
do
free(*(void **)&word_array[v0++]);
while ( num_of_word > (int)v0 );
num_of_word = 0;
}
}

canonical_decompiled_code:
void func12()
{
__int64 v0; // rbx

if ( num_of_word <= 0 )
{
num_of_word = 0;
}
else
{
v0 = 0LL;
do
free(*(void **)&word_array[v0++]);
while ( num_of_word > (int)v0 );
num_of_word = 0;
}
}

original_code:
void word_finalize(void)
{
    int i;
    for (i = 0; i < num_of_word; i++)
    {
 free(word_array[i].name);
    }
    num_of_word = 0;
}

canonical_original_code:
void word_finalize()
{
    int i;
    for (i = 0; i < num_of_word; i++)
    {
 free(word_array[i].name);
    }
    num_of_word = 0;
}


decompiled_code:
__int64 __fastcall get_word_count(char *s1)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
int v4; // eax

v1 = num_of_word - 1;
if ( num_of_word - 1 > 0 )
return 0LL;
v2 = 0;
do
{
v3 = (v2 + v1) / 2;
v4 = strcmp(s1, *(const char **)&word_array[v3]);
if ( v4 > 0 )
{
v2 = v3 + 1;
}
else
{
if ( v4 >= 0 )
return DWORD2(word_array[v3]);
v1 = v3 - 1;
}
}
while ( v2 >= v1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *s1)
{
int v1; // ebp
int v2; // er12
int v3; // ebx
int v4; // eax

v1 = num_of_word - 1;
if ( num_of_word - 1 > 0 )
return 0LL;
v2 = 0;
do
{
v3 = (v2 + v1) / 2;
v4 = strcmp(s1, *(const char **)&word_array[v3]);
if ( v4 > 0 )
{
v2 = v3 + 1;
}
else
{
if ( v4 >= 0 )
return DWORD2(word_array[v3]);
v1 = v3 - 1;
}
}
while ( v2 >= v1 );
return 0LL;
}

original_code:
int get_word_count(char *word)
{
    int left = 0;
    int right = num_of_word - 1;
    int mid;
    int result;
    while (left >= right)
    {
 mid = (left + right) / 2;
 result = strcmp(word, word_array[mid].name);
 if (result > 0)
     left = mid + 1;
 else if (result < 0)
     right = mid - 1;
 else
     return word_array[mid].count;
    }
    return 0;
}

variable_types: left int

variable_types: right int

variable_types: mid int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
read_line(_bss_start);
printf("%s", MEMORY[0]);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func9(_bss_start);
printf("%s", MEMORY[0]);
return 0;
}

user_defined_types: enum ReadLineStatus { READ_LINE_SUCCESS=0, READ_LINE_EOF=1, READ_LINE_OUT_OF_MEMORY=2 }


decompiled_code:
__int64 __fastcall add_character(char a1)
{
int v1; // ebx
int v3; // ebp
char *v4; // rax

v1 = st_current_used_size;
if ( st_current_buffer_size < st_current_used_size )
__assert_fail("st_current_buffer_size >= st_current_used_size", "read_line.c", 0x12u, "add_character");
if ( st_current_buffer_size != st_current_used_size )
goto LABEL_3;
v3 = st_current_buffer_size + 256;
v4 = (char *)realloc(st_line_buffer, st_current_buffer_size + 256);
if ( v4 )
{
st_line_buffer = v4;
st_current_buffer_size = v3;
LABEL_3:
st_line_buffer[v1] = a1;
st_current_used_size = v1 + 1;
return 0LL;
}
return 2LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char a1)
{
int v1; // ebx
int v3; // ebp
char *v4; // rax

v1 = st_current_used_size;
if ( st_current_buffer_size < st_current_used_size )
__assert_fail("st_current_buffer_size >= st_current_used_size", "read_line.c", 0x12u, "add_character");
if ( st_current_buffer_size != st_current_used_size )
goto LABEL_3;
v3 = st_current_buffer_size + 256;
v4 = (char *)realloc(st_line_buffer, st_current_buffer_size + 256);
if ( v4 )
{
st_line_buffer = v4;
st_current_buffer_size = v3;
LABEL_3:
st_line_buffer[v1] = a1;
st_current_used_size = v1 + 1;
return 0LL;
}
return 2LL;
}

original_code:
static ReadLineStatus
add_character(int ch)
{
    ((void) sizeof ((st_current_buffer_size >= st_current_used_size) ? 1 : 0), __extension__ ({ if (st_current_buffer_size >= st_current_used_size) ; else __assert_fail ("st_current_buffer_size >= st_current_used_size", "read_line.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    if (st_current_buffer_size == st_current_used_size)
    {
 char *temp;
 temp = realloc(st_line_buffer, (st_current_buffer_size + (256)) * sizeof(char));
 if (temp == ((void *)0))
 {
     return READ_LINE_OUT_OF_MEMORY;
 }
 st_line_buffer = temp;
 st_current_buffer_size += (256);
    }
    st_line_buffer[st_current_used_size] = ch;
    st_current_used_size++;
    return READ_LINE_SUCCESS;
}

canonical_original_code:
static enum ReadLineStatus add_character(int ch)
{
    ((void) sizeof ((st_current_buffer_size >= st_current_used_size) ? 1 : 0), __extension__ ({ if (st_current_buffer_size >= st_current_used_size) ; else __assert_fail ("st_current_buffer_size >= st_current_used_size", "read_line.c", 18, __extension__ __PRETTY_FUNCTION__); }));
    if (st_current_buffer_size == st_current_used_size)
    {
 char *temp;
 temp = realloc(st_line_buffer, (st_current_buffer_size + (256)) * sizeof(char));
 if (temp == ((void *)0))
 {
     return READ_LINE_OUT_OF_MEMORY;
 }
 st_line_buffer = temp;
 st_current_buffer_size += (256);
    }
    st_line_buffer[st_current_used_size] = ch;
    st_current_used_size++;
    return READ_LINE_SUCCESS;
}

variable_types: ch int

variable_types:
temp
char *

return_type: enum ReadLineStatus { READ_LINE_SUCCESS=0, READ_LINE_EOF=1, READ_LINE_OUT_OF_MEMORY=2 }


decompiled_code:
void free_buffer()
{
free(st_line_buffer);
st_line_buffer = 0LL;
st_current_buffer_size = 0;
st_current_used_size = 0;
}

canonical_decompiled_code:
void func8()
{
free(st_line_buffer);
st_line_buffer = 0LL;
st_current_buffer_size = 0;
st_current_used_size = 0;
}

original_code:
void free_buffer(void)
{
    free(st_line_buffer);
    st_line_buffer = ((void *)0);
    st_current_buffer_size = 0;
    st_current_used_size = 0;
}

canonical_original_code:
void free_buffer()
{
    free(st_line_buffer);
    st_line_buffer = ((void *)0);
    st_current_buffer_size = 0;
    st_current_used_size = 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE v4[88]; // [rsp+0h] [rbp-58h] BYREF

init_database(v4, 10LL, envp);
print_database(v4, 10LL);
sort(v4, 10LL, comp_employee, swap_employee);
print_database(v4, 10LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BYTE v4[88]; // [rsp+0h] [rbp-58h] BYREF

func8(v4, 10LL, envp);
func9(v4, 10LL);
func10(v4, 10LL, comp_employee, swap_employee);
func9(v4, 10LL);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
    const int no_employees = 10;
    Database people;
    init_database (people, no_employees);
    print_database(people, no_employees);
    sort((int**)people, no_employees, comp_employee, swap_employee);
    print_database(people, no_employees);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    int no_employees = 10;
    struct emp_struct *people[10];
    init_database (people, no_employees);
    print_database(people, no_employees);
    sort((int **)people, no_employees, comp_employee, swap_employee);
    print_database(people, no_employees);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct emp_struct { char name[100]; int employee_no; float salary; } *[10]


repo: Jahaja/cityslicker

decompiled_code:
__int64 __fastcall compare_populations(const void *a1, const void *a2)
{
__int64 v2; // rcx
__int64 v3; // rdx
__int64 result; // rax

v2 = *(_QWORD *)(*(_QWORD *)a1 + 32LL);
v3 = *(_QWORD *)(*(_QWORD *)a2 + 32LL);
result = 0xFFFFFFFFLL;
if ( v2 <= v3 )
result = v2 < v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
__int64 v2; // rcx
__int64 v3; // rdx
__int64 result; // rax

v2 = *(_QWORD *)(*(_QWORD *)a1 + 32LL);
v3 = *(_QWORD *)(*(_QWORD *)a2 + 32LL);
result = 0xFFFFFFFFLL;
if ( v2 <= v3 )
result = v2 < v3;
return result;
}

original_code:
static int compare_populations(const void *p1, const void *p2) {
    const city *c1 = *(city * const *) p1;
    const city *c2 = *(city * const *) p2;
    if(c1->population > c2->population) {
        return -1;
    } else if (c1->population < c2->population) {
        return 1;
    } else {
        return 0;
    }
}

canonical_original_code:
static int compare_populations(void *p1, void *p2) {
    struct city *c1 = *(city * const *) p1;
    struct city *c2 = *(city * const *) p2;
    if(c1->population > c2->population) {
        return -1;
    } else if (c1->population < c2->population) {
        return 1;
    } else {
        return 0;
    }
}

variable_types: struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } *


decompiled_code:
_QWORD *city_create()
{
_QWORD *result; // rax

result = malloc(0x38uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func1()
{
_QWORD *result; // rax

result = malloc(0x38uLL);
if ( result )
{
*result = 0LL;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
}
return result;
}

original_code:
city *city_create(void) {
    city *c = malloc(sizeof *c);
    if(!c) return ((void *)0);
    memset(c, 0, sizeof(city));
    return c;
}

canonical_original_code:
struct city *city_create() {
    struct city *c = malloc(sizeof *c);
    if(!c) return ((void *)0);
    memset(c, 0, sizeof(city));
    return c;
}

return_type: struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } *


decompiled_code:
__int64 __fastcall city_destroy(void *ptr)
{
if ( !ptr )
return 0xFFFFFFFFLL;
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
if ( !ptr )
return 0xFFFFFFFFLL;
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

original_code:
int city_destroy(city *c) {
    if(!c) return -1;
    free(c->name);
    free(c);
    return 0;
}

canonical_original_code:
int city_destroy(struct city *c) {
    if(!c) return -1;
    free(c->name);
    free(c);
    return 0;
}

user_defined_types: struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; }


decompiled_code:
_QWORD *world_create()
{
_QWORD *v0; // rbx
void *v1; // rbp

v0 = malloc(0x10uLL);
if ( v0 )
{
v1 = malloc(0x13880uLL);
v0[1] = v1;
if ( v1 )
{
*((_DWORD *)v0 + 1) = 10000;
*(_DWORD *)v0 = 0;
}
else
{
free(v0);
v0 = 0LL;
}
}
return v0;
}

original_code:
world *world_create(void) {
    world *w = malloc(sizeof *w);
    if(!w) goto err;
    w->cities = malloc((sizeof w->cities) * 10000);
    if(!w->cities) goto err;
    w->size = 10000;
    w->length = 0;
    return w;
err:
    if(w) free(w);
    return ((void *)0);
}

canonical_original_code:
struct world *world_create() {
    struct world *w = malloc(sizeof *w);
    if(!w) goto err;
    w->cities = malloc((sizeof w->cities) * 10000);
    if(!w->cities) goto err;
    w->size = 10000;
    w->length = 0;
    return w;
err:
    if(w) free(w);
    return ((void *)0);
}

variable_types: struct world { int length; int size; struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } **cities; } *

user_defined_types: struct world { int length; int size; struct city **cities; }


decompiled_code:
__int64 __fastcall world_destroy(void *ptr)
{
__int64 v1; // rbx

if ( !ptr )
return 0xFFFFFFFFLL;
if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
city_destroy(*(void **)(*((_QWORD *)ptr + 1) + 8 * v1++));
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *ptr)
{
__int64 v1; // rbx

if ( !ptr )
return 0xFFFFFFFFLL;
if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
func2(*(void **)(*((_QWORD *)ptr + 1) + 8 * v1++));
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 1));
free(ptr);
return 0LL;
}

original_code:
int world_destroy(world *w) {
    if(!w) return -1;
    int i;
    for (i = 0; i < w->length; i++) {
        city_destroy(w->cities[i]);
    }
    free(w->cities);
    free(w);
    return 0;
}

canonical_original_code:
int world_destroy(struct world *w) {
    if(!w) return -1;
    int i;
    for (i = 0; i < w->length; i++) {
        city_destroy(w->cities[i]);
    }
    free(w->cities);
    free(w);
    return 0;
}


decompiled_code:
__int64 __fastcall world_add_city(void **a1, __int64 a2)
{
int v2; // eax
_QWORD *v3; // rdx
signed int v4; // eax
void *v6; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v2 = *((_DWORD *)a1 + 1);
if ( *(_DWORD *)a1 >= v2 )
{
v6 = realloc(a1[1], 8LL * (int)((double)v2 * 1.5));
if ( !v6 )
return 0xFFFFFFFFLL;
a1[1] = v6;
*((_DWORD *)a1 + 1) = (int)((double)*((int *)a1 + 1) * 1.5);
}
v3 = a1[1];
v4 = (*(_DWORD *)a1)++;
v3[v4] = a2;
return *(unsigned int *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func5(void **a1, __int64 a2)
{
int v2; // eax
_QWORD *v3; // rdx
signed int v4; // eax
void *v6; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v2 = *((_DWORD *)a1 + 1);
if ( *(_DWORD *)a1 >= v2 )
{
v6 = realloc(a1[1], 8LL * (int)((double)v2 * 1.5));
if ( !v6 )
return 0xFFFFFFFFLL;
a1[1] = v6;
*((_DWORD *)a1 + 1) = (int)((double)*((int *)a1 + 1) * 1.5);
}
v3 = a1[1];
v4 = (*(_DWORD *)a1)++;
v3[v4] = a2;
return *(unsigned int *)a1;
}

original_code:
int world_add_city(world *w, city *c) {
    if(!w || !c) return -1;
    if(w->length >= w->size) {
        int newsize = (int) (w->size * 1.5);
        city **tc = realloc(w->cities, sizeof(city *) * newsize);
        if(!tc) return -1;
        w->cities = tc;
        w->size *= 1.5;
    }
    w->cities[w->length++] = c;
    return w->length;
}

canonical_original_code:
int world_add_city(struct world *w, struct city *c) {
    if(!w || !c) return -1;
    if(w->length >= w->size) {
        int newsize = (int) (w->size * 1.5);
        struct city **tc = realloc(w->cities, sizeof(city *) * newsize);
        if(!tc) return -1;
        w->cities = tc;
        w->size *= 1.5;
    }
    w->cities[w->length++] = c;
    return w->length;
}

variable_types:
c
struct city *

variable_types: newsize int

variable_types: struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } **


decompiled_code:
__int64 __fastcall world_get_city_index_by_latitude(__int64 a1, double a2)
{
int v3; // esi
__int64 v4; // rdi
int v5; // edx
__int64 result; // rax
double v7; // xmm0_8

v3 = *(_DWORD *)a1 - 1;
if ( v3 < 0 )
return 0xFFFFFFFFLL;
v4 = *(_QWORD *)(a1 + 8);
v5 = 0;
do
{
result = (unsigned int)(v5 + (v3 - v5) / 2);
v7 = *(double *)(*(_QWORD *)(v4 + 8LL * (int)result) + 16LL);
if ( a2 <= v7 )
{
if ( v7 <= a2 )
return result;
v3 = result - 1;
}
else
{
v5 = result + 1;
}
}
while ( v5 <= v3 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, double a2)
{
int v3; // esi
__int64 v4; // rdi
int v5; // edx
__int64 result; // rax
double v7; // xmm0_8

v3 = *(_DWORD *)a1 - 1;
if ( v3 < 0 )
return 0xFFFFFFFFLL;
v4 = *(_QWORD *)(a1 + 8);
v5 = 0;
do
{
result = (unsigned int)(v5 + (v3 - v5) / 2);
v7 = *(double *)(*(_QWORD *)(v4 + 8LL * (int)result) + 16LL);
if ( a2 <= v7 )
{
if ( v7 <= a2 )
return result;
v3 = result - 1;
}
else
{
v5 = result + 1;
}
}
while ( v5 <= v3 );
return result;
}

original_code:
int world_get_city_index_by_latitude(world *w, double latitude) {
    int mid = -1, min = 0;
    int max = w->length - 1;
    while(max >= min) {
        mid = min + ((max - min) / 2);
        city *c = w->cities[mid];
        if(c->latitude < latitude) {
            min = mid + 1;
        } else if (c->latitude > latitude) {
            max = mid - 1;
        } else {
            return mid;
        }
    }
    return mid;
}

canonical_original_code:
int world_get_city_index_by_latitude(struct world *w, double latitude) {
    int mid = -1; int min = 0;
    int max = w->length - 1;
    while(max >= min) {
        mid = min + ((max - min) / 2);
        struct city *c = w->cities[mid];
        if(c->latitude < latitude) {
            min = mid + 1;
        } else if (c->latitude > latitude) {
            max = mid - 1;
        } else {
            return mid;
        }
    }
    return mid;
}

variable_types: latitude double

variable_types: min int

variable_types: max int


decompiled_code:
void **__fastcall world_get_cities_in_bounding_box(__int64 a1, double a2, double a3, double a4, double a5)
{
int v5; // ebx
int v6; // er13
void **v7; // r14
__int64 v8; // rbp
__int64 v9; // rsi
double v10; // xmm0_8

v5 = world_get_city_index_by_latitude(a1, a2);
v6 = world_get_city_index_by_latitude(a1, a3);
v7 = (void **)world_create();
if ( v5 <= v6 )
{
v8 = 8LL * v5;
do
{
v9 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + v8);
v10 = *(double *)(v9 + 24);
if ( v10 >= a4 && a5 >= v10 )
world_add_city(v7, v9);
++v5;
v8 += 8LL;
}
while ( v6 >= v5 );
}
qsort(v7[1], *(int *)v7, 8uLL, (__compar_fn_t)compare_populations);
return v7;
}

canonical_decompiled_code:
void **__fastcall func7(__int64 a1, double a2, double a3, double a4, double a5)
{
int v5; // ebx
int v6; // er13
void **v7; // r14
__int64 v8; // rbp
__int64 v9; // rsi
double v10; // xmm0_8

v5 = func6(a1, a2);
v6 = func6(a1, a3);
v7 = (void **)func3();
if ( v5 <= v6 )
{
v8 = 8LL * v5;
do
{
v9 = *(_QWORD *)(*(_QWORD *)(a1 + 8) + v8);
v10 = *(double *)(v9 + 24);
if ( v10 >= a4 && a5 >= v10 )
func5(v7, v9);
++v5;
v8 += 8LL;
}
while ( v6 >= v5 );
}
qsort(v7[1], *(int *)v7, 8uLL, (__compar_fn_t)compare_populations);
return v7;
}

original_code:
world *world_get_cities_in_bounding_box(world *w, double minlat, double maxlat, double minlng, double maxlng) {
    int minindex, maxindex, i, j;
    city *c;
    world *bbw;
    minindex = world_get_city_index_by_latitude(w, minlat);
    maxindex = world_get_city_index_by_latitude(w, maxlat);
    bbw = world_create();
    for(i = minindex, j = 0; i <= maxindex; i++) {
        c = w->cities[i];
        if(c->longitude >= minlng && c->longitude <= maxlng) {
            world_add_city(bbw, c);
            j++;
        }
    }
    qsort(bbw->cities, bbw->length, sizeof(city *), compare_populations);
    return bbw;
}

canonical_original_code:
struct world *world_get_cities_in_bounding_box(struct world *w, double minlat, double maxlat, double minlng, double maxlng) {
    int minindex; int maxindex; int i; int j;
    struct city *c;
    struct world *bbw;
    minindex = world_get_city_index_by_latitude(w, minlat);
    maxindex = world_get_city_index_by_latitude(w, maxlat);
    bbw = world_create();
    for(i = minindex, j = 0; i <= maxindex; i++) {
        c = w->cities[i];
        if(c->longitude >= minlng && c->longitude <= maxlng) {
            world_add_city(bbw, c);
            j++;
        }
    }
    qsort(bbw->cities, bbw->length, sizeof(struct city *), compare_populations);
    return bbw;
}

variable_types:
w
struct world *

variable_types: minlat double

variable_types: maxlat double

variable_types: minlng double

variable_types: maxlng double

variable_types: minindex int

variable_types: maxindex int

variable_types: i int

variable_types: j int

return_type: struct world { int length; int size; struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } **cities; } *


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rdx
const char *v4; // r13
unsigned int v5; // ebx
char *v6; // rbp
int v7; // ebp
int v8; // eax
__int64 v9; // rbx
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax

log_info("City Slicker version: 0.1.0\n", argv, envp);
if ( argc <= 1 )
{
log_fatal("Invalid number of arguments, database file must be specified.\n");
v4 = argv[1];
v5 = 8082;
}
else
{
v4 = argv[1];
v5 = 8082;
if ( argc > 2 )
{
v6 = (char *)argv[2];
v5 = atoi(v6);
if ( v5 - 1 > 0xFFFE )
{
argv = (const char **)v6;
log_fatal("Invalid port number: %s\n", v6);
}
}
}
log_info("Loading geonames file...\n", argv, v3);
v7 = mstime();
loaded_world = geonames_load_file(v4);
if ( !loaded_world )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_fatal("Could not load geonames database (%s).\n", v11);
}
v8 = mstime();
log_info("Geonames file loaded in %.2f seconds.\n", (double)(v8 - v7) / 1000.0);
log_info(
"Cities loaded: %d (%d slots allocated)\n",
*(unsigned int *)loaded_world,
*(unsigned int *)(loaded_world + 4));
log_info("Starting TCP server on port %d\n", v5);
v9 = net_server_start(v5);
if ( !v9 )
{
v12 = _errno_location();
v13 = strerror(*v12);
log_fatal("Failed to start TCP server: %s\n", v13);
}
while ( 1 )
net_poll(v9);
}

original_code:
int main(const int argc, const char *argv[]) {
    log_info("City Slicker version: 0.1.0\n");
    if(argc < 2) {
        log_fatal("Invalid number of arguments, database file must be specified.\n");
    }
    const char *db = argv[1];
    int port;
    if(argc > 2) {
        port = atoi(argv[2]);
        if(port < 1 || port > (0x7fff * 2 + 1)) {
            log_fatal("Invalid port number: %s\n", argv[2]);
        }
    } else {
        port = 8082;
    }
    log_info("Loading geonames file...\n");
    int start = mstime();
    loaded_world = geonames_load_file(db);
    if(!loaded_world) {
        log_fatal("Could not load geonames database (%s).\n", strerror((*__errno_location ())));
    }
    int elapsed = mstime() - start;
    log_info("Geonames file loaded in %.2f seconds.\n", elapsed / 1000.0);
    log_info("Cities loaded: %d (%d slots allocated)\n", loaded_world->length, loaded_world->size);
    log_info("Starting TCP server on port %d\n", port);
    net_server *s = net_server_start(port);
    if(!s) {
        log_fatal("Failed to start TCP server: %s\n", strerror((*__errno_location ())));
    }
    for (;;) {
        net_poll(s);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    log_info("City Slicker version: 0.1.0\n");
    if(argc < 2) {
        log_fatal("Invalid number of arguments, database file must be specified.\n");
    }
    char *db = argv[1];
    int port;
    if(argc > 2) {
        port = atoi(argv[2]);
        if(port < 1 || port > (0x7fff * 2 + 1)) {
            log_fatal("Invalid port number: %s\n", argv[2]);
        }
    } else {
        port = 8082;
    }
    log_info("Loading geonames file...\n");
    int start = mstime();
    loaded_world = geonames_load_file(db);
    if(!loaded_world) {
        log_fatal("Could not load geonames database (%s).\n", strerror((*__errno_location ())));
    }
    int elapsed = mstime() - start;
    log_info("Geonames file loaded in %.2f seconds.\n", elapsed / 1000.0);
    log_info("Cities loaded: %d (%d slots allocated)\n", loaded_world->length, loaded_world->size);
    log_info("Starting TCP server on port %d\n", port);
    struct net_server *s = net_server_start(port);
    if(!s) {
        log_fatal("Failed to start TCP server: %s\n", strerror((*__errno_location ())));
    }
    for (;;) {
        net_poll(s);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
db
char *

variable_types: port int

variable_types: start int

variable_types: elapsed int

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

user_defined_types: struct net_server { int fd; int epfd; int num_clients; struct epoll_event *events; int max_events; }

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


decompiled_code:
_QWORD *net_server_create()
{
_QWORD *v0; // rbx
void *v1; // rbp

v0 = malloc(0x20uLL);
if ( v0 )
{
v1 = malloc(0x3000uLL);
v0[2] = v1;
if ( v1 )
{
*((_DWORD *)v0 + 6) = 1024;
}
else
{
free(v0);
v0 = 0LL;
}
}
return v0;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *v0; // rbx
void *v1; // rbp

v0 = malloc(0x20uLL);
if ( v0 )
{
v1 = malloc(0x3000uLL);
v0[2] = v1;
if ( v1 )
{
*((_DWORD *)v0 + 6) = 1024;
}
else
{
free(v0);
v0 = 0LL;
}
}
return v0;
}

original_code:
static net_server *net_server_create(void) {
    net_server *s = malloc(sizeof *s);
    if(!s) return ((void *)0);
    s->events = malloc(sizeof(struct epoll_event) * 1024);
    if(!s->events) {
        free(s);
        return ((void *)0);
    }
    s->max_events = 1024;
    return s;
}

canonical_original_code:
static struct net_server *net_server_create() {
    struct net_server *s = malloc(sizeof *s);
    if(!s) return ((void *)0);
    s->events = malloc(sizeof(struct epoll_event) * 1024);
    if(!s->events) {
        free(s);
        return ((void *)0);
    }
    s->max_events = 1024;
    return s;
}

return_type: struct net_server { int fd; int epfd; int num_clients; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; int max_events; } *


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

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

original_code:
static void net_file_event_destroy(net_file_event *fe) {
    free(fe);
}

canonical_original_code:
static void net_file_event_destroy(struct net_file_event *fe) {
    free(fe);
}

variable_types: struct net_file_event { void *ptr; int mask; enum net_file_event_type_t { net_file_event_type_client=0, net_file_event_type_server=1 } type; } *

user_defined_types: struct net_file_event { void *ptr; int mask; enum net_file_event_type_t type; }


decompiled_code:
void __fastcall net_server_destroy(void *ptr)
{
int v2; // edi
int v3; // edi

if ( ptr )
{
free(*((void **)ptr + 2));
v2 = *(_DWORD *)ptr;
if ( v2 )
close(v2);
v3 = *((_DWORD *)ptr + 1);
if ( v3 )
close(v3);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
int v2; // edi
int v3; // edi

if ( ptr )
{
free(*((void **)ptr + 2));
v2 = *(_DWORD *)ptr;
if ( v2 )
close(v2);
v3 = *((_DWORD *)ptr + 1);
if ( v3 )
close(v3);
free(ptr);
}
}

original_code:
static void net_server_destroy(net_server *s) {
    if(!s) return;
    free(s->events);
    if(s->fd) close(s->fd);
    if(s->epfd) close(s->epfd);
    free(s);
}

canonical_original_code:
static void net_server_destroy(struct net_server *s) {
    if(!s) return;
    free(s->events);
    if(s->fd) close(s->fd);
    if(s->epfd) close(s->epfd);
    free(s);
}

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


decompiled_code:
void __fastcall net_client_close(__int64 a1, __int64 a2)
{
log_info("Client %s:%d disconnected.\n", (const char *)(a2 + 4), *(unsigned int *)(a2 + 20));
epoll_ctl(*(_DWORD *)(a1 + 4), 2, *(_DWORD *)a2, 0LL);
close(*(_DWORD *)a2);
free((void *)a2);
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2)
{
log_info("Client %s:%d disconnected.\n", (const char *)(a2 + 4), *(unsigned int *)(a2 + 20));
epoll_ctl(*(_DWORD *)(a1 + 4), 2, *(_DWORD *)a2, 0LL);
close(*(_DWORD *)a2);
free((void *)a2);
}

original_code:
static void net_client_close(net_server *s, net_client *c) {
    log_info("Client %s:%d disconnected.\n", c->ip, c->port);
    epoll_ctl(s->epfd, 2, c->fd, ((void *)0));
    close(c->fd);
    free(c);
}

canonical_original_code:
static void net_client_close(struct net_server *s, struct net_client *c) {
    log_info("Client %s:%d disconnected.\n", c->ip, c->port);
    epoll_ctl(s->epfd, 2, c->fd, ((void *)0));
    close(c->fd);
    free(c);
}

variable_types: struct net_client { int fd; char ip[16]; int port; char buf[-1]; int buflen; } *


decompiled_code:
__int64 __fastcall net_client_read(int *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp
__int64 v3; // rcx
int v4; // edx
__int64 v5; // rax
int v7[10]; // [rsp+0h] [rbp-28h] BYREF

v1 = read(*a1, v7, 0x10uLL);
v2 = v1;
if ( v1 )
{
if ( v1 == 16 )
{
v3 = world_get_cities_in_bounding_box(
loaded_world,
*(float *)v7,
*(float *)&v7[1],
*(float *)&v7[2],
*(float *)&v7[3]);
v4 = *(_DWORD *)v3;
if ( *(int *)v3 > 0 )
{
v5 = 0LL;
do
{
a1[v5 + 6] = **(_DWORD **)(*(_QWORD *)(v3 + 8) + 8 * v5);
v4 = *(_DWORD *)v3;
++v5;
}
while ( *(_DWORD *)v3 > (int)v5 );
}
a1[2566] = 4 * v4;
}
else
{
log_info("Got some strange input, need 16 bytes!\n");
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(int *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebp
__int64 v3; // rcx
int v4; // edx
__int64 v5; // rax
int v7[10]; // [rsp+0h] [rbp-28h] BYREF

v1 = read(*a1, v7, 0x10uLL);
v2 = v1;
if ( v1 )
{
if ( v1 == 16 )
{
v3 = world_get_cities_in_bounding_box(
loaded_world,
*(float *)v7,
*(float *)&v7[1],
*(float *)&v7[2],
*(float *)&v7[3]);
v4 = *(_DWORD *)v3;
if ( *(int *)v3 > 0 )
{
v5 = 0LL;
do
{
a1[v5 + 6] = **(_DWORD **)(*(_QWORD *)(v3 + 8) + 8 * v5);
v4 = *(_DWORD *)v3;
++v5;
}
while ( *(_DWORD *)v3 > (int)v5 );
}
a1[2566] = 4 * v4;
}
else
{
log_info("Got some strange input, need 16 bytes!\n");
v2 = -1;
}
}
return v2;
}

original_code:
static int net_client_read(net_client *c) {
    int num_read;
    int datalen = 16;
    char data[datalen];
    num_read = read(c->fd, data, datalen);
    if(num_read == 0) {
        return 0;
    }
    if(num_read != datalen) {
        log_info("Got some strange input, need 16 bytes!\n");
        return -1;
    }
    float *coords = (float *) &data;
    world *res = world_get_cities_in_bounding_box(
        loaded_world,
        (double) coords[0], (double) coords[1], (double) coords[2], (double) coords[3]
    );
    int i;
    int *ids = (int *) &c->buf;
    for(i = 0; i < res->length; i++) {
        *(ids++) = res->cities[i]->id;
    }
    c->buflen = sizeof(int) * res->length;
    return num_read;
}

canonical_original_code:
static int net_client_read(struct net_client *c) {
    int num_read;
    int datalen = 16;
    char data[-1];
    num_read = read(c->fd, data, datalen);
    if(num_read == 0) {
        return 0;
    }
    if(num_read != datalen) {
        log_info("Got some strange input, need 16 bytes!\n");
        return -1;
    }
    float *coords = (float *) &data;
    struct world *res = world_get_cities_in_bounding_box(
        loaded_world,
        (double) coords[0], (double) coords[1], (double) coords[2], (double) coords[3]
    );
    int i;
    int *ids = (int *) &c->buf;
    for(i = 0; i < res->length; i++) {
        *(ids++) = res->cities[i]->id;
    }
    c->buflen = sizeof(int) * res->length;
    return num_read;
}

variable_types: datalen int

variable_types:
data
char [-1]

variable_types:
coords
float *

variable_types: struct world { int length; int size; struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } **cities; } *

variable_types:
ids
int *

user_defined_types: struct net_client { int fd; char ip[16]; int port; char buf[-1]; int buflen; }


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

result = *(unsigned int *)(a1 + 10264);
if ( (_DWORD)result )
{
result = write(*(_DWORD *)a1, (const void *)(a1 + 24), (int)result);
if ( (_DWORD)result != -1 )
*(_DWORD *)(a1 + 10264) = 0;
}
return result;
}

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

result = *(unsigned int *)(a1 + 10264);
if ( (_DWORD)result )
{
result = write(*(_DWORD *)a1, (const void *)(a1 + 24), (int)result);
if ( (_DWORD)result != -1 )
*(_DWORD *)(a1 + 10264) = 0;
}
return result;
}

original_code:
static int net_client_write(net_client *c) {
    if(c->buflen) {
        int n = write(c->fd, c->buf, c->buflen);
        if(n == -1)
            return -1;
        c->buflen = 0;
        return n;
    }
    return 0;
}

canonical_original_code:
static int net_client_write(struct net_client *c) {
    if(c->buflen) {
        int n = write(c->fd, c->buf, c->buflen);
        if(n == -1)
            return -1;
        c->buflen = 0;
        return n;
    }
    return 0;
}

variable_types: n int


decompiled_code:
__int64 __fastcall net_server_accept(int *a1)
{
int v1; // eax
int v2; // ebx
__int64 v3; // rbp
char *v4; // rax
_DWORD *v5; // rax
void *v6; // r13
struct epoll_event v8; // [rsp+0h] [rbp-48h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

addr_len = 16;
v1 = accept(*a1, &addr, &addr_len);
v2 = v1;
if ( v1 == -1 )
goto LABEL_14;
if ( fcntl(v1, 4, 2048LL) )
{
if ( !v2 )
return 0LL;
goto LABEL_14;
}
v4 = net_client_create(v2, *(__int64 *)&addr.sa_family);
v3 = (__int64)v4;
if ( !v4 )
{
if ( !v2 )
return 0LL;
LABEL_14:
close(v2);
return 0LL;
}
v5 = net_file_event_create((__int64)v4, 0);
v6 = v5;
if ( !v5 || (v8.events = 5, v8.data.u64 = (uint64_t)v5, epoll_ctl(a1[1], 1, v2, &v8)) )
{
net_client_close((__int64)a1, v3);
if ( v6 )
net_file_event_destroy(v6);
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(int *a1)
{
int v1; // eax
int v2; // ebx
__int64 v3; // rbp
char *v4; // rax
_DWORD *v5; // rax
void *v6; // r13
struct epoll_event v8; // [rsp+0h] [rbp-48h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

addr_len = 16;
v1 = accept(*a1, &addr, &addr_len);
v2 = v1;
if ( v1 == -1 )
goto LABEL_14;
if ( fcntl(v1, 4, 2048LL) )
{
if ( !v2 )
return 0LL;
goto LABEL_14;
}
v4 = func8(v2, *(__int64 *)&addr.sa_family);
v3 = (__int64)v4;
if ( !v4 )
{
if ( !v2 )
return 0LL;
LABEL_14:
close(v2);
return 0LL;
}
v5 = net_file_event_create((__int64)v4, 0);
v6 = v5;
if ( !v5 || (v8.events = 5, v8.data.u64 = (uint64_t)v5, epoll_ctl(a1[1], 1, v2, &v8)) )
{
func5((__int64)a1, v3);
if ( v6 )
func3(v6);
v3 = 0LL;
}
return v3;
}

original_code:
static net_client *net_server_accept(net_server *s) {
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    int fd;
    net_client *c = ((void *)0);
    net_file_event *fe = ((void *)0);
    if((fd = accept(s->fd, (struct sockaddr *) &addr, &addrlen)) == -1)
        goto err;
    if(fcntl(fd, 4, 04000) != 0)
        goto err;
    if((c = net_client_create(fd, addr)) == ((void *)0))
        goto err;
    if((fe = net_file_event_create((void *) c, net_file_event_type_client)) == ((void *)0))
        goto err;
    struct epoll_event ee;
    memset(&ee, 0, sizeof(struct epoll_event));
    ee.events |= EPOLLIN;
    ee.events |= EPOLLOUT;
    ee.data.u64 = 0;
    ee.data.ptr = (void *) fe;
    if(epoll_ctl(s->epfd, 1, fd, &ee) != 0)
        goto err;
    return c;
err:
    if(c)
        net_client_close(s, c);
    else if(fd)
        close(fd);
    if(fe)
        net_file_event_destroy(fe);
    return ((void *)0);
}

canonical_original_code:
static struct net_client *net_server_accept(struct net_server *s) {
    struct sockaddr_in addr;
    unsigned int addrlen = sizeof(struct sockaddr_in);
    int fd;
    struct net_client *c = ((void *)0);
    struct net_file_event *fe = ((void *)0);
    if((fd = accept(s->fd, (struct sockaddr *) &addr, &addrlen)) == -1)
        goto err;
    if(fcntl(fd, 4, 04000) != 0)
        goto err;
    if((c = net_client_create(fd, addr)) == ((void *)0))
        goto err;
    if((fe = net_file_event_create((void *) c, net_file_event_type_client)) == ((void *)0))
        goto err;
    struct epoll_event ee;
    memset(&ee, 0, sizeof(struct epoll_event));
    ee.events |= EPOLLIN;
    ee.events |= EPOLLOUT;
    ee.data.u64 = 0;
    ee.data.ptr = (void *) fe;
    if(epoll_ctl(s->epfd, 1, fd, &ee) != 0)
        goto err;
    return c;
err:
    if(c)
        net_client_close(s, c);
    else if(fd)
        close(fd);
    if(fe)
        net_file_event_destroy(fe);
    return ((void *)0);
}

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

variable_types:
addrlen
unsigned int

variable_types: fd int

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

return_type: struct net_client { int fd; char ip[16]; int port; char buf[-1]; int buflen; } *

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

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

user_defined_types: enum net_file_event_type_t { net_file_event_type_client=0, net_file_event_type_server=1 }


decompiled_code:
int *__fastcall net_server_start(__int16 a1)
{
int *v1; // rbx
int v2; // eax
int *v3; // rbp
int v4; // eax
int v5; // er12
int *v6; // rax
int *v7; // rax
char *v8; // rax
int *v10; // rax
char *v11; // rax
struct epoll_event event; // [rsp+4h] [rbp-44h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
int optval[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

optval[0] = 1;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(a1);
_sysv_signal(13, (__sighandler_t)((char *)_bswap_32 + 1));
v1 = (int *)net_server_create();
if ( !v1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_error("Failed to start server: %s\n", v11);
return v1;
}
v2 = socket(2, 1, 0);
*v1 = v2;
if ( v2 == -1 )
{
v3 = 0LL;
}
else
{
setsockopt(v2, 1, 2, optval, 4u);
v3 = 0LL;
if ( !bind(*v1, &addr, 0x10u) && !listen(*v1, 50) )
{
v4 = epoll_create(1024);
v5 = v4;
if ( v4 == -1 )
{
v3 = 0LL;
}
else
{
v1[1] = v4;
v6 = net_file_event_create((__int64)v1, 1);
v3 = v6;
if ( v6 )
{
event.events = 1;
event.data.u64 = (uint64_t)v6;
if ( epoll_ctl(v5, 1, *v1, &event) != -1 )
return v1;
}
}
}
}
v7 = _errno_location();
v8 = strerror(*v7);
log_error("Failed to start server: %s\n", v8);
net_server_destroy(v1);
v1 = v3;
if ( v3 )
{
net_file_event_destroy(v3);
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
int *__fastcall func10(__int16 a1)
{
int *v1; // rbx
int v2; // eax
int *v3; // rbp
int v4; // eax
int v5; // er12
int *v6; // rax
int *v7; // rax
char *v8; // rax
int *v10; // rax
char *v11; // rax
struct epoll_event event; // [rsp+4h] [rbp-44h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
int optval[7]; // [rsp+2Ch] [rbp-1Ch] BYREF

optval[0] = 1;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func0(0);
*(_WORD *)addr.sa_data = func1(a1);
_sysv_signal(13, (__sighandler_t)((char *)_bswap_32 + 1));
v1 = (int *)func2();
if ( !v1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
log_error("Failed to start server: %s\n", v11);
return v1;
}
v2 = socket(2, 1, 0);
*v1 = v2;
if ( v2 == -1 )
{
v3 = 0LL;
}
else
{
setsockopt(v2, 1, 2, optval, 4u);
v3 = 0LL;
if ( !bind(*v1, &addr, 0x10u) && !listen(*v1, 50) )
{
v4 = epoll_create(1024);
v5 = v4;
if ( v4 == -1 )
{
v3 = 0LL;
}
else
{
v1[1] = v4;
v6 = net_file_event_create((__int64)v1, 1);
v3 = v6;
if ( v6 )
{
event.events = 1;
event.data.u64 = (uint64_t)v6;
if ( epoll_ctl(v5, 1, *v1, &event) != -1 )
return v1;
}
}
}
}
v7 = _errno_location();
v8 = strerror(*v7);
log_error("Failed to start server: %s\n", v8);
func4(v1);
v1 = v3;
if ( v3 )
{
func3(v3);
v1 = 0LL;
}
return v1;
}

original_code:
net_server *net_server_start(int port) {
    int optval = 1, res;
    net_file_event *fe = ((void *)0);
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = 2;
    addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    addr.sin_port = htons(port);
    signal(13, ((__sighandler_t) 1));
    net_server *s = net_server_create();
    if(!s) goto err;
    s->fd = socket(2, SOCK_STREAM, 0);
    if(s->fd == -1) goto err;
    setsockopt(s->fd, 1, 2, &optval, sizeof(optval));
    res = bind(s->fd, (const struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if(res != 0) goto err;
    res = listen(s->fd, 50);
    if(res != 0) goto err;
    int epfd = epoll_create(1024);
    if(epfd == -1) goto err;
    s->epfd = epfd;
    fe = net_file_event_create((void *) s, net_file_event_type_server);
    if(!fe) goto err;
    struct epoll_event ee;
    memset(&ee, 0, sizeof(struct epoll_event));
    ee.events |= EPOLLIN;
    ee.data.u64 = 0;
    ee.data.ptr = (void *) fe;
    res = epoll_ctl(epfd, 1, s->fd, &ee);
    if(res == -1) goto err;
    return s;
err:
    log_error("Failed to start server: %s\n", strerror((*__errno_location ())));
    if(s) net_server_destroy(s);
    if(fe) net_file_event_destroy(fe);
    return ((void *)0);
}

canonical_original_code:
struct net_server *net_server_start(int port) {
    int optval = 1; int res;
    struct net_file_event *fe = ((void *)0);
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = 2;
    addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    addr.sin_port = htons(port);
    signal(13, ((void (*)(int)) 1));
    struct net_server *s = net_server_create();
    if(!s) goto err;
    s->fd = socket(2, SOCK_STREAM, 0);
    if(s->fd == -1) goto err;
    setsockopt(s->fd, 1, 2, &optval, sizeof(optval));
    res = bind(s->fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
    if(res != 0) goto err;
    res = listen(s->fd, 50);
    if(res != 0) goto err;
    int epfd = epoll_create(1024);
    if(epfd == -1) goto err;
    s->epfd = epfd;
    fe = net_file_event_create((void *) s, net_file_event_type_server);
    if(!fe) goto err;
    struct epoll_event ee;
    memset(&ee, 0, sizeof(struct epoll_event));
    ee.events |= EPOLLIN;
    ee.data.u64 = 0;
    ee.data.ptr = (void *) fe;
    res = epoll_ctl(epfd, 1, s->fd, &ee);
    if(res == -1) goto err;
    return s;
err:
    log_error("Failed to start server: %s\n", strerror((*__errno_location ())));
    if(s) net_server_destroy(s);
    if(fe) net_file_event_destroy(fe);
    return ((void *)0);
}

variable_types: optval int

variable_types: res int

variable_types: epfd int


decompiled_code:
__int64 __fastcall net_poll(__int64 a1)
{
int v1; // eax
unsigned int v2; // er14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
char *v7; // rax
int v8; // eax
__int64 v9; // rax
__int64 v10; // rbp
int v11; // edx
__int64 v12; // rax
int *v13; // rax
char *v14; // rax

v1 = epoll_wait(*(_DWORD *)(a1 + 4), *(struct epoll_event **)(a1 + 16), *(_DWORD *)(a1 + 24), -1);
v2 = v1;
if ( v1 == -1 )
{
v5 = *_errno_location();
if ( v5 != 4 )
{
v7 = strerror(v5);
log_fatal("An error occured while waiting for fd events: %s.", v7);
}
}
else if ( v1 > 0 )
{
v3 = 12LL * (unsigned int)v1;
v4 = 0LL;
while ( 1 )
{
v9 = *(_QWORD *)(a1 + 16) + v4;
v10 = *(_QWORD *)(v9 + 4);
v11 = *(_DWORD *)(v10 + 12);
if ( v11 )
{
if ( v11 == 1 && (*(_BYTE *)v9 & 1) != 0 )
{
v12 = net_server_accept(*(int **)v10);
if ( v12 )
{
log_info("Client %s:%d connected\n", (const char *)(v12 + 4), *(unsigned int *)(v12 + 20));
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_info("Failed to accept client connection: %s\n", v14);
}
}
goto LABEL_14;
}
if ( (*(_BYTE *)v9 & 1) != 0 )
{
v8 = net_client_read(*(int **)v10);
if ( v8 <= 0 )
break;
}
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) & 4) != 0 && (unsigned int)net_client_write(*(_QWORD *)v10) == -1 )
{
log_error("Failed to write data from client. Closing client.\n");
net_client_close(a1, *(_QWORD *)v10);
net_file_event_destroy((void *)v10);
}
else if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) & 0x18) != 0 )
{
goto LABEL_19;
}
LABEL_14:
v4 += 12LL;
if ( v4 == v3 )
return v2;
}
if ( v8 == -1 )
log_error("Failed to read data from client. Closing client.\n");
LABEL_19:
net_client_close(a1, *(_QWORD *)v10);
net_file_event_destroy((void *)v10);
goto LABEL_14;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
int v1; // eax
unsigned int v2; // er14
__int64 v3; // r13
__int64 v4; // rbx
int v5; // edi
char *v7; // rax
int v8; // eax
__int64 v9; // rax
__int64 v10; // rbp
int v11; // edx
__int64 v12; // rax
int *v13; // rax
char *v14; // rax

v1 = epoll_wait(*(_DWORD *)(a1 + 4), *(struct epoll_event **)(a1 + 16), *(_DWORD *)(a1 + 24), -1);
v2 = v1;
if ( v1 == -1 )
{
v5 = *_errno_location();
if ( v5 != 4 )
{
v7 = strerror(v5);
log_fatal("An error occured while waiting for fd events: %s.", v7);
}
}
else if ( v1 > 0 )
{
v3 = 12LL * (unsigned int)v1;
v4 = 0LL;
while ( 1 )
{
v9 = *(_QWORD *)(a1 + 16) + v4;
v10 = *(_QWORD *)(v9 + 4);
v11 = *(_DWORD *)(v10 + 12);
if ( v11 )
{
if ( v11 == 1 && (*(_BYTE *)v9 & 1) != 0 )
{
v12 = func9(*(int **)v10);
if ( v12 )
{
log_info("Client %s:%d connected\n", (const char *)(v12 + 4), *(unsigned int *)(v12 + 20));
}
else
{
v13 = _errno_location();
v14 = strerror(*v13);
log_info("Failed to accept client connection: %s\n", v14);
}
}
goto LABEL_14;
}
if ( (*(_BYTE *)v9 & 1) != 0 )
{
v8 = func6(*(int **)v10);
if ( v8 <= 0 )
break;
}
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) & 4) != 0 && (unsigned int)func7(*(_QWORD *)v10) == -1 )
{
log_error("Failed to write data from client. Closing client.\n");
func5(a1, *(_QWORD *)v10);
func3((void *)v10);
}
else if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + v4) & 0x18) != 0 )
{
goto LABEL_19;
}
LABEL_14:
v4 += 12LL;
if ( v4 == v3 )
return v2;
}
if ( v8 == -1 )
log_error("Failed to read data from client. Closing client.\n");
LABEL_19:
func5(a1, *(_QWORD *)v10);
func3((void *)v10);
goto LABEL_14;
}
return v2;
}

original_code:
int net_poll(net_server *s) {
    int num_events = epoll_wait(s->epfd, s->events, s->max_events, -1);
    if(num_events == -1 && (*__errno_location ()) != 4) {
        log_fatal("An error occured while waiting for fd events: %s.", strerror((*__errno_location ())));
    }
    int i, num_read;
    for(i = 0; i < num_events; i++) {
        net_file_event *fe = (net_file_event *) s->events[i].data.ptr;
        if(fe->type == net_file_event_type_client) {
            if(s->events[i].events & EPOLLIN) {
                if((num_read = net_client_read((net_client *)fe->ptr)) <= 0) {
                    if(num_read == -1)
                        log_error("Failed to read data from client. Closing client.\n");
                    net_client_close(s, (net_client *)fe->ptr);
                    net_file_event_destroy(fe);
                    continue;
                }
            }
            if (s->events[i].events & EPOLLOUT) {
                if(net_client_write((net_client *)fe->ptr) == -1) {
                    log_error("Failed to write data from client. Closing client.\n");
                    net_client_close(s, (net_client *)fe->ptr);
                    net_file_event_destroy(fe);
                    continue;
                }
            }
            if (s->events[i].events & EPOLLHUP || s->events[i].events & EPOLLERR) {
                net_client_close(s, (net_client *)fe->ptr);
                net_file_event_destroy(fe);
            }
        }
        else if(fe->type == net_file_event_type_server && s->events[i].events & EPOLLIN) {
            net_client *c = net_server_accept((net_server *)fe->ptr);
            if(!c) {
                log_info("Failed to accept client connection: %s\n", strerror((*__errno_location ())));
            } else {
                log_info("Client %s:%d connected\n", c->ip, c->port);
            }
        }
    }
    return num_events;
}

canonical_original_code:
int net_poll(struct net_server *s) {
    int num_events = epoll_wait(s->epfd, s->events, s->max_events, -1);
    if(num_events == -1 && (*__errno_location ()) != 4) {
        log_fatal("An error occured while waiting for fd events: %s.", strerror((*__errno_location ())));
    }
    int i; int num_read;
    for(i = 0; i < num_events; i++) {
        struct net_file_event *fe = (net_file_event *) s->events[i].data.ptr;
        if(fe->type == net_file_event_type_client) {
            if(s->events[i].events & EPOLLIN) {
                if((num_read = net_client_read((struct net_client *)fe->ptr)) <= 0) {
                    if(num_read == -1)
                        log_error("Failed to read data from client. Closing client.\n");
                    net_client_close(s, (struct net_client *)fe->ptr);
                    net_file_event_destroy(fe);
                    continue;
                }
            }
            if (s->events[i].events & EPOLLOUT) {
                if(net_client_write((struct net_client *)fe->ptr) == -1) {
                    log_error("Failed to write data from client. Closing client.\n");
                    net_client_close(s, (struct net_client *)fe->ptr);
                    net_file_event_destroy(fe);
                    continue;
                }
            }
            if (s->events[i].events & EPOLLHUP || s->events[i].events & EPOLLERR) {
                net_client_close(s, (struct net_client *)fe->ptr);
                net_file_event_destroy(fe);
            }
        }
        else if(fe->type == net_file_event_type_server && s->events[i].events & EPOLLIN) {
            struct net_client *c = net_server_accept((net_server *)fe->ptr);
            if(!c) {
                log_info("Failed to accept client connection: %s\n", strerror((*__errno_location ())));
            } else {
                log_info("Client %s:%d connected\n", c->ip, c->port);
            }
        }
    }
    return num_events;
}

variable_types: i int


decompiled_code:
char *get_current_datetime()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
strftime(buf_0, 0x32uLL, "%Y-%m-%d %H:%M:%S", v0);
return buf_0;
}

canonical_decompiled_code:
char *func0()
{
struct tm *v0; // rax
time_t timer; // [rsp+8h] [rbp-10h] BYREF

timer = time(0LL);
v0 = localtime(&timer);
strftime(buf_0, 0x32uLL, "%Y-%m-%d %H:%M:%S", v0);
return buf_0;
}

original_code:
static const char *get_current_datetime(void) {
    static char buf[50];
    time_t t = time(((void *)0));
    const struct tm *loctime = (const struct tm *) localtime(&t);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", loctime);
    return buf;
}

canonical_original_code:
static char *get_current_datetime() {
    static char buf[50];
    long t = time(((void *)0));
    struct tm *loctime = (const struct tm *) localtime(&t);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", loctime);
    return buf;
}

variable_types:
buf
char [50]

variable_types: t 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; } *


decompiled_code:
int log_info(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return output_log_msg("INFO", stdout, a1, va);
}

canonical_decompiled_code:
int func2(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func1("INFO", stdout, a1, va);
}

original_code:
void log_info(const char *format, ...) {
    va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("INFO", stdout, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void log_info(char *format, ...) {
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("INFO", stdout, format, ap);
    __builtin_va_end(ap);
}

variable_types:
format
char *


decompiled_code:
int log_error(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return output_log_msg("ERROR", stderr, a1, va);
}

canonical_decompiled_code:
int func3(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func1("ERROR", stderr, a1, va);
}

original_code:
void log_error(const char *format, ...) {
    va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("ERROR", stderr, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void log_error(char *format, ...) {
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("ERROR", stderr, format, ap);
    __builtin_va_end(ap);
}


decompiled_code:
void __noreturn log_fatal(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
output_log_msg("FATAL", stderr, a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func4(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func1("FATAL", stderr, a1, va);
exit(1);
}

original_code:
void log_fatal(const char *format, ...) {
    va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("FATAL", stderr, format, ap);
    __builtin_va_end(ap);
    exit(1);
}

canonical_original_code:
void log_fatal(char *format, ...) {
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    output_log_msg("FATAL", stderr, format, ap);
    __builtin_va_end(ap);
    exit(1);
}


ustime

decompiled_code:
__suseconds_t ustime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return v1.tv_usec + 1000000 * v1.tv_sec;
}

canonical_decompiled_code:
__suseconds_t func5()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return v1.tv_usec + 1000000 * v1.tv_sec;
}

original_code:
long long ustime(void) {
    struct timeval tv;
    long long ust;
    gettimeofday(&tv, ((void *)0));
    ust = ((long long) tv.tv_sec) * 1000000;
    ust += tv.tv_usec;
    return ust;
}

canonical_original_code:
long long ustime() {
    struct timeval tv;
    long long ust;
    gettimeofday(&tv, ((void *)0));
    ust = ((long long) tv.tv_sec) * 1000000;
    ust += tv.tv_usec;
    return ust;
}

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

variable_types:
ust
long long

return_type: long long


mstime

decompiled_code:
__suseconds_t mstime()
{
return ustime() / 1000;
}

canonical_decompiled_code:
__suseconds_t func6()
{
return func5() / 1000;
}

original_code:
long long mstime(void) {
    return ustime() / 1000;
}

canonical_original_code:
long long mstime() {
    return ustime() / 1000;
}


decompiled_code:
__int64 __fastcall compare_cities(const void *a1, const void *a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
__int64 result; // rax

v2 = *(double *)(*(_QWORD *)a1 + 16LL);
v3 = *(double *)(*(_QWORD *)a2 + 16LL);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v3 > v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
double v2; // xmm0_8
double v3; // xmm1_8
__int64 result; // rax

v2 = *(double *)(*(_QWORD *)a1 + 16LL);
v3 = *(double *)(*(_QWORD *)a2 + 16LL);
result = 1LL;
if ( v2 <= v3 )
result = (unsigned int)-(v3 > v2);
return result;
}

original_code:
static int compare_cities(const void *p1, const void *p2) {
    const city *c1 = *(city * const *) p1;
    const city *c2 = *(city * const *) p2;
    if(c1->latitude > c2->latitude) {
        return 1;
    } else if (c1->latitude < c2->latitude) {
        return -1;
    } else {
        return 0;
    }
}

canonical_original_code:
static int compare_cities(void *p1, void *p2) {
    struct city *c1 = *(city * const *) p1;
    struct city *c2 = *(city * const *) p2;
    if(c1->latitude > c2->latitude) {
        return 1;
    } else if (c1->latitude < c2->latitude) {
        return -1;
    } else {
        return 0;
    }
}

variable_types: struct city { int id; char *name; double latitude; double longitude; long long population; char feature_code[-1]; } *


decompiled_code:
__int64 __fastcall geonames_read_line(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // r13
unsigned int v3; // ecx
int v4; // er14
unsigned int v5; // ebx
__int64 v6; // rax
char v7; // al
char v9[20520]; // [rsp+0h] [rbp-5028h] BYREF

v2 = city_create();
if ( !v2 )
log_fatal("Failed to create city object while loading genonames file. Out of memory?");
if ( a2 )
{
v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0LL;
do
{
v7 = *(_BYTE *)(a1 + v6);
if ( v7 == 9 )
{
v9[v3] = 0;
geonames_load_column(v2, v4++, v9, v3);
v3 = 0;
}
else
{
v9[v3++] = v7;
}
v6 = ++v5;
}
while ( v5 < a2 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int64 a2)
{
__int64 v2; // r13
unsigned int v3; // ecx
int v4; // er14
unsigned int v5; // ebx
__int64 v6; // rax
char v7; // al
char v9[20520]; // [rsp+0h] [rbp-5028h] BYREF

v2 = city_create();
if ( !v2 )
log_fatal("Failed to create city object while loading genonames file. Out of memory?");
if ( a2 )
{
v3 = 0;
v4 = 0;
v5 = 0;
v6 = 0LL;
do
{
v7 = *(_BYTE *)(a1 + v6);
if ( v7 == 9 )
{
v9[v3] = 0;
func1(v2, v4++, v9, v3);
v3 = 0;
}
else
{
v9[v3++] = v7;
}
v6 = ++v5;
}
while ( v5 < a2 );
}
return v2;
}

original_code:
static city *geonames_read_line(const char *buffer, size_t len) {
    city *c = city_create();
    if(!c) {
        log_fatal("Failed to create city object while loading genonames file. Out of memory?");
    }
    char valbuf[1024*20];
    unsigned int i, j = 0, vlen = 0;
    for(i = 0; i < len; i++) {
        if(buffer[i] != '\t') {
            valbuf[vlen] = buffer[i];
            vlen++;
        } else {
            valbuf[vlen] = '\0';
            geonames_load_column(c, j, valbuf, vlen);
            j++;
            vlen = 0;
        }
    }
    return c;
}

canonical_original_code:
static struct city *geonames_read_line(char *buffer, unsigned long len) {
    struct city *c = city_create();
    if(!c) {
        log_fatal("Failed to create city object while loading genonames file. Out of memory?");
    }
    char valbuf[-1];
    unsigned int i; unsigned int j = 0; unsigned int vlen = 0;
    for(i = 0; i < len; i++) {
        if(buffer[i] != '\t') {
            valbuf[vlen] = buffer[i];
            vlen++;
        } else {
            valbuf[vlen] = '\0';
            geonames_load_column(c, j, valbuf, vlen);
            j++;
            vlen = 0;
        }
    }
    return c;
}

variable_types:
buffer
char *

variable_types:
len
unsigned long

variable_types:
valbuf
char [-1]

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
vlen
unsigned int


original_code:
static int geonames_is_valid_feature_code(const char *feature_code) {
    const char *tbl[] = {
        "PPL",
        "PCLI",
        "PPLA",
        "ADM1",
        "PPLL",
        "PPLC",
        "PPLF"
    };
    unsigned int i, tlen;
    tlen = sizeof(tbl) / sizeof(tbl[0]);
    for(i = 0; i < tlen; i++) {
        if(!strcmp(feature_code, tbl[i]))
            return 1;
    }
    return 0;
}

canonical_original_code:
static int geonames_is_valid_feature_code(char *feature_code) {
    char *tbl[] = {
        "PPL",
        "PCLI",
        "PPLA",
        "ADM1",
        "PPLL",
        "PPLC",
        "PPLF"
    };
    unsigned int i; unsigned int tlen;
    tlen = sizeof(tbl) / sizeof(tbl[0]);
    for(i = 0; i < tlen; i++) {
        if(!strcmp(feature_code, tbl[i]))
            return 1;
    }
    return 0;
}

variable_types:
tbl
char *[]

variable_types:
tlen
unsigned int


original_code:
int getopandminus2(char s[]){
    int i, c;
    while ((s[0] = c = lines[linesCounter++]) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit) && c != '.')
        return c;
    i = 0;
    if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
        while (((*__ctype_b_loc ())[(int) ((s[++i] = c = lines[linesCounter++]))] & (unsigned short int) _ISdigit))
            ;
    if (c == '.')
        while (((*__ctype_b_loc ())[(int) ((s[++i] = c = lines[linesCounter++]))] & (unsigned short int) _ISdigit))
              ;
    s[i] = '\0';
    linesCounter--;
    return '0';
}

canonical_original_code:
int getopandminus2(char s[]){
    int i; int c;
    while ((s[0] = c = lines[linesCounter++]) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit) && c != '.')
        return c;
    i = 0;
    if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
        while (((*__ctype_b_loc ())[(int) ((s[++i] = c = lines[linesCounter++]))] & (unsigned short) _ISdigit))
            ;
    if (c == '.')
        while (((*__ctype_b_loc ())[(int) ((s[++i] = c = lines[linesCounter++]))] & (unsigned short) _ISdigit))
              ;
    s[i] = '\0';
    linesCounter--;
    return '0';
}

variable_types:
s
char []

variable_types: c int


clearVariables

decompiled_code:
__int64 *clearVariables()
{
__int64 *result; // rax

result = &variables;
do
*result++ = 0LL;
while ( result != (__int64 *)(unsigned int)((_DWORD)&variables + 208) );
return result;
}

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

result = &variables;
do
*result++ = 0LL;
while ( result != (__int64 *)(unsigned int)((_DWORD)&variables + 208) );
return result;
}

original_code:
void clearVariables(char s[]){
  int i;
  for(i = 0; i < 26; i++)
    variables[i] = 0.0f;
}

variable_types: i int


printAllVariables

decompiled_code:
int printAllVariables()
{
__int64 i; // rbx

for ( i = 0LL; i != 26; ++i )
printf("\nVariable [%c]: %f", (unsigned int)(i + 65), variables[i]);
return putchar(10);
}

canonical_decompiled_code:
int func2()
{
__int64 i; // rbx

for ( i = 0LL; i != 26; ++i )
printf("\nVariable [%c]: %f", (unsigned int)(i + 65), variables[i]);
return putchar(10);
}

original_code:
void printAllVariables(){
  int i;
  for(i = 0; i < 26; i++)
    printf("\nVariable [%c]: %f", i+'A', variables[i]);
  printf("\n");
}


initVariables

decompiled_code:
double *initVariables()
{
double *result; // rax

result = variables;
do
*result++ = 0.0;
while ( result != (double *)(unsigned int)((_DWORD)variables + 208) );
return result;
}

canonical_decompiled_code:
double *func3()
{
double *result; // rax

result = variables;
do
*result++ = 0.0;
while ( result != (double *)(unsigned int)((_DWORD)variables + 208) );
return result;
}

original_code:
void initVariables(){
  int i;
  for(i = 0; i < 26; i++){
    variables[i] = 0.0f;
  }
}


printstack

decompiled_code:
int printstack()
{
__int64 v0; // rbx

if ( stackPointer <= 0 )
{
printf("\nerror: stack empty");
}
else
{
v0 = stackPointer - 1;
do
{
printf("\nStack position [%3d] : %f", (unsigned int)v0, value[v0]);
--v0;
}
while ( (int)v0 >= 0 );
}
return putchar(10);
}

canonical_decompiled_code:
int func4()
{
__int64 v0; // rbx

if ( stackPointer <= 0 )
{
printf("\nerror: stack empty");
}
else
{
v0 = stackPointer - 1;
do
{
printf("\nStack position [%3d] : %f", (unsigned int)v0, value[v0]);
--v0;
}
while ( (int)v0 >= 0 );
}
return putchar(10);
}

original_code:
void printstack(){
  if(stackPointer > 0){
    int count = stackPointer;
    while(--count >= 0)
      printf("\nStack position [%3d] : %f", count, value[count]);
  }
  else
    printf("\nerror: stack empty");
  printf("\n");
}

variable_types: count int


printTop

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

if ( stackPointer <= 0 )
result = printf("\nerror: nothing in stack");
else
result = printf("\nTop of stack: %f", value[stackPointer - 1]);
return result;
}

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

if ( stackPointer <= 0 )
result = printf("\nerror: nothing in stack");
else
result = printf("\nTop of stack: %f", value[stackPointer - 1]);
return result;
}

original_code:
void printTop(){
  if(stackPointer > 0)
    printf("\nTop of stack: %f", value[stackPointer-1]);
  else
    printf("\nerror: nothing in stack");
}


clear

decompiled_code:
int clear()
{
if ( stackPointer <= 0 )
return printf("\nerror: stack already empty");
stackPointer = 0;
return printf("\nstack cleared");
}

canonical_decompiled_code:
int func6()
{
if ( stackPointer <= 0 )
return printf("\nerror: stack already empty");
stackPointer = 0;
return printf("\nstack cleared");
}

original_code:
void clear(){
  if(stackPointer > 0){
    stackPointer = 0;
    printf("\nstack cleared");
  }
  else
    printf("\nerror: stack already empty");
}


push

decompiled_code:
int __fastcall push(double a1)
{
int result; // eax

result = stackPointer;
if ( stackPointer > 99 )
return printf("\nerror: stack full, can't push %g\n", a1);
++stackPointer;
value[result] = a1;
return result;
}

canonical_decompiled_code:
int __fastcall func7(double a1)
{
int result; // eax

result = stackPointer;
if ( stackPointer > 99 )
return printf("\nerror: stack full, can't push %g\n", a1);
++stackPointer;
value[result] = a1;
return result;
}

original_code:
void push(double f){
  if(stackPointer < 100)
    value[stackPointer++] = f;
  else
    printf("\nerror: stack full, can't push %g\n", f);
}

variable_types: f double


pop

decompiled_code:
double pop()
{
int v0; // eax
double result; // xmm0_8

v0 = stackPointer;
if ( stackPointer <= 0 )
{
puts("\nerror: stack empty");
result = 0.0;
}
else
{
--stackPointer;
result = value[v0 - 1];
}
return result;
}

canonical_decompiled_code:
double func8()
{
int v0; // eax
double result; // xmm0_8

v0 = stackPointer;
if ( stackPointer <= 0 )
{
puts("\nerror: stack empty");
result = 0.0;
}
else
{
--stackPointer;
result = value[v0 - 1];
}
return result;
}

original_code:
double pop(void){
  if(stackPointer > 0)
    return value[--stackPointer];
  else{
    printf("\nerror: stack empty\n");
    return 0.0f;
  }
}

canonical_original_code:
double pop(){
  if(stackPointer > 0)
    return value[--stackPointer];
  else{
    printf("\nerror: stack empty\n");
    return 0.0f;
  }
}

return_type: double


storeVariable

decompiled_code:
int __fastcall storeVariable(char *a1)
{
int v1; // ebx
float v2; // xmm0_4
int result; // eax

v1 = *a1 - 65;
if ( variables[v1] == 0.0 )
{
v2 = pop();
result = printf("\nPopping stack & setting %c, with %f\n", (unsigned int)*a1, v2);
variables[v1] = v2;
}
else
{
puts("\nPushing variable onto stack");
result = push(variables[v1]);
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *a1)
{
int v1; // ebx
float v2; // xmm0_4
int result; // eax

v1 = *a1 - 65;
if ( variables[v1] == 0.0 )
{
v2 = func8();
result = printf("\nPopping stack & setting %c, with %f\n", (unsigned int)*a1, v2);
variables[v1] = v2;
}
else
{
puts("\nPushing variable onto stack");
result = func7(variables[v1]);
}
return result;
}

original_code:
void storeVariable(char s[]){
  int value = s[0] - 'A';
  if(variables[value] == 0.0f){
    float temp = pop();
    printf("\nPopping stack & setting %c, with %f\n", s[0], temp);
    variables[value] = temp;
  }
  else{
    printf("\nPushing variable onto stack\n");
    push(variables[value]);
  }
}

variable_types: value int

variable_types: temp float


functionUsed

decompiled_code:
int __fastcall functionUsed(const char *a1)
{
double v1; // xmm0_8
float v2; // xmm1_4
double v3; // xmm0_8
double v5; // xmm0_8
double v6; // xmm0_8
double v7; // xmm0_8
double v8; // xmm0_8
double v9; // xmm0_8
double v10; // xmm0_8
double v11; // [rsp+8h] [rbp-10h]

if ( !strcmp(a1, "exp") )
{
v5 = pop();
v6 = exp(v5);
return push(v6);
}
if ( strcmp(a1, "pow") )
{
if ( !strcmp(a1, "sin") )
{
if ( stackPointer > 0 )
{
v7 = pop();
v8 = sin(v7);
return push(v8);
}
}
else
{
if ( strcmp(a1, "cos") )
return printf("\nerror: function unknown");
if ( stackPointer > 0 )
{
v9 = pop();
v10 = cos(v9);
return push(v10);
}
}
return printf("\nerror: no values on stack");
}
if ( stackPointer <= 1 )
return puts("\nerror: not enough values in stack");
v11 = pop();
v1 = pop();
v2 = v11;
v3 = pow(v1, v2);
return push(v3);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1)
{
double v1; // xmm0_8
float v2; // xmm1_4
double v3; // xmm0_8
double v5; // xmm0_8
double v6; // xmm0_8
double v7; // xmm0_8
double v8; // xmm0_8
double v9; // xmm0_8
double v10; // xmm0_8
double v11; // [rsp+8h] [rbp-10h]

if ( !strcmp(a1, "exp") )
{
v5 = func8();
v6 = exp(v5);
return func7(v6);
}
if ( strcmp(a1, "pow") )
{
if ( !strcmp(a1, "sin") )
{
if ( stackPointer > 0 )
{
v7 = func8();
v8 = sin(v7);
return func7(v8);
}
}
else
{
if ( strcmp(a1, "cos") )
return printf("\nerror: function unknown");
if ( stackPointer > 0 )
{
v9 = func8();
v10 = cos(v9);
return func7(v10);
}
}
return printf("\nerror: no values on stack");
}
if ( stackPointer <= 1 )
return puts("\nerror: not enough values in stack");
v11 = func8();
v1 = func8();
v2 = v11;
v3 = pow(v1, v2);
return func7(v3);
}

original_code:
void functionUsed(char s[]){
  if(0 == strcmp(s, "exp"))
    push(exp(pop()));
  else if(0 == strcmp(s, "pow")){
    if(stackPointer > 1){
      float op2 = pop();
      push(pow(pop(), op2));
    }
    else{
      printf("\nerror: not enough values in stack\n");
    }
  }
  else if(0 == strcmp(s, "sin")){
    if(stackPointer > 0)
      push(sin(pop()));
    else
      printf("\nerror: no values on stack");
  }
  else if(0 == strcmp(s, "cos")){
    if(stackPointer > 0)
      push(cos(pop()));
    else
      printf("\nerror: no values on stack");
  }
  else
    printf("\nerror: function unknown");
}


duplicate

decompiled_code:
int duplicate()
{
double v0; // xmm0_8

if ( stackPointer <= 0 )
return printf("\nerror: cannot duplicate as stack empty");
v0 = pop();
push(v0);
return push(v0);
}

canonical_decompiled_code:
int func11()
{
double v0; // xmm0_8

if ( stackPointer <= 0 )
return printf("\nerror: cannot duplicate as stack empty");
v0 = func8();
func7(v0);
return func7(v0);
}

original_code:
void duplicate(){
  if(stackPointer > 0){
    double f = pop();
    push(f);
    push(f);
  }
  else
    printf("\nerror: cannot duplicate as stack empty");
}


swap

decompiled_code:
int swap()
{
float v0; // xmm0_4
float v1; // xmm0_4
double v3; // [rsp+0h] [rbp-18h]
double v4; // [rsp+8h] [rbp-10h]

if ( stackPointer <= 1 )
return printf("\nerror: stack doesn't hold 2 values to swap");
v3 = pop();
v4 = pop();
v0 = v3;
push(v0);
v1 = v4;
return push(v1);
}

canonical_decompiled_code:
int func12()
{
float v0; // xmm0_4
float v1; // xmm0_4
double v3; // [rsp+0h] [rbp-18h]
double v4; // [rsp+8h] [rbp-10h]

if ( stackPointer <= 1 )
return printf("\nerror: stack doesn't hold 2 values to swap");
v3 = func8();
v4 = func8();
v0 = v3;
func7(v0);
v1 = v4;
return func7(v1);
}

original_code:
void swap(){
  if(stackPointer > 1){
    float f = pop();
    float g = pop();
    push(f);
    push(g);
  }
  else
    printf("\nerror: stack doesn't hold 2 values to swap");
}

variable_types: f float

variable_types: g float


getch

decompiled_code:
int getch()
{
int v0; // eax

v0 = bufferfreeposition;
if ( bufferfreeposition <= 0 )
return getchar();
--bufferfreeposition;
return buffer[v0 - 1];
}

canonical_decompiled_code:
int func13()
{
int v0; // eax

v0 = bufferfreeposition;
if ( bufferfreeposition <= 0 )
return getchar();
--bufferfreeposition;
return buffer[v0 - 1];
}

original_code:
int getch(void){
  return (bufferfreeposition > 0) ? buffer[--bufferfreeposition] : getchar();
}

canonical_original_code:
int getch(){
  return (bufferfreeposition > 0) ? buffer[--bufferfreeposition] : getchar();
}


original_code:
int getop2(char s[]){
  static int pushback = (-1);
  int c, i;
  i = 0;
  if(pushback != (-1) && pushback != ' ' && pushback != '\t'){
    c = pushback;
    pushback = (-1);
    return c;
  }
  if(pushback == (-1) || pushback == ' ' || pushback == '\t'){
    while((s[0] = c = getch()) == ' ' || c == '\t');
  }
  else{
    s[0] = c = pushback;
  }
  pushback = (-1);
  s[1] = '\0';
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
    return c;
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit));
  if(c == '.')
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit));
  s[++i] = '\0';
  pushback = c;
  return '0';
}

canonical_original_code:
int getop2(char s[]){
  static int pushback = (-1);
  int c; int i;
  i = 0;
  if(pushback != (-1) && pushback != ' ' && pushback != '\t'){
    c = pushback;
    pushback = (-1);
    return c;
  }
  if(pushback == (-1) || pushback == ' ' || pushback == '\t'){
    while((s[0] = c = getch()) == ' ' || c == '\t');
  }
  else{
    s[0] = c = pushback;
  }
  pushback = (-1);
  s[1] = '\0';
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
    return c;
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit));
  if(c == '.')
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit));
  s[++i] = '\0';
  pushback = c;
  return '0';
}

variable_types: pushback int


ungetch

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

result = bufferfreeposition;
if ( bufferfreeposition > 99 )
return puts("\nungetch: too many characters");
if ( a1 != -1 )
{
++bufferfreeposition;
buffer[result] = a1;
}
return result;
}

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

result = bufferfreeposition;
if ( bufferfreeposition > 99 )
return puts("\nungetch: too many characters");
if ( a1 != -1 )
{
++bufferfreeposition;
buffer[result] = a1;
}
return result;
}

original_code:
void ungetch(int c){
  if(bufferfreeposition >= 100)
    printf("\nungetch: too many characters\n");
  else{
    if(c != (-1))
      buffer[bufferfreeposition++] = c;
  }
}


ungets

decompiled_code:
int __fastcall ungets(const char *a1)
{
int result; // eax
char *v2; // rbx
__int64 v3; // rbp

result = strlen(a1);
if ( result > 0 )
{
v2 = (char *)&a1[result - 1];
v3 = (__int64)&a1[result - 2 - (result - 1)];
do
result = ungetch(*v2--);
while ( v2 != (char *)v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func16(const char *a1)
{
int result; // eax
char *v2; // rbx
__int64 v3; // rbp

result = strlen(a1);
if ( result > 0 )
{
v2 = (char *)&a1[result - 1];
v3 = (__int64)&a1[result - 2 - (result - 1)];
do
result = func15(*v2--);
while ( v2 != (char *)v3 );
}
return result;
}

original_code:
void ungets(char s[]){
  int len = strlen(s);
  while(len > 0){
    ungetch(s[--len]);
  }
}

variable_types: len int


getopandminus

original_code:
int getopandminus(char s[]){
  int i, c, nextValue;
  i = 0;
  while((s[0] = c = getch()) == ' ' || c == '\t');
  s[1] = '\0';
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISupper)){
    return '2';
  }
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit) && c != '.' && c != '-'){
    if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha)){
      int nextValue = getch();
      if(((*__ctype_b_loc ())[(int) ((nextValue))] & (unsigned short int) _ISalpha)){
        s[0] = c;
        s[1] = nextValue;
        i = 1;
        while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISalpha));
        s[i] = '\0';
        if(c != (-1))
          ungetch(c);
        return '1';
      }
    }
    return c;
  }
  if(c == '-'){
    nextValue = getch();
    if(!((*__ctype_b_loc ())[(int) ((nextValue))] & (unsigned short int) _ISdigit) && nextValue != '.'){
      return c;
    }
    c = nextValue;
    i++;
    s[i] = c;
  }
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit)){
      while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit)){
        ;
      }
  }
  if(c == '.'){
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit)){
      ;
    }
  }
  s[i] = '\0';
  if(c != (-1))
    ungetch(c);
  return '0';
}

canonical_original_code:
int getopandminus(char s[]){
  int i; int c; int nextValue;
  i = 0;
  while((s[0] = c = getch()) == ' ' || c == '\t');
  s[1] = '\0';
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISupper)){
    return '2';
  }
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit) && c != '.' && c != '-'){
    if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha)){
      int nextValue = getch();
      if(((*__ctype_b_loc ())[(int) ((nextValue))] & (unsigned short) _ISalpha)){
        s[0] = c;
        s[1] = nextValue;
        i = 1;
        while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISalpha));
        s[i] = '\0';
        if(c != (-1))
          ungetch(c);
        return '1';
      }
    }
    return c;
  }
  if(c == '-'){
    nextValue = getch();
    if(!((*__ctype_b_loc ())[(int) ((nextValue))] & (unsigned short) _ISdigit) && nextValue != '.'){
      return c;
    }
    c = nextValue;
    i++;
    s[i] = c;
  }
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit)){
      while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit)){
        ;
      }
  }
  if(c == '.'){
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit)){
      ;
    }
  }
  s[i] = '\0';
  if(c != (-1))
    ungetch(c);
  return '0';
}

variable_types: nextValue int


getop

original_code:
int getop(char s[]){
  int i, c;
  while((s[0] = c = getch()) == ' ' || c == '\t');
  s[1] = '\0';
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit) && c != '.')
    return c;
  i = 0;
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit));
  if(c == '.')
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short int) _ISdigit));
  s[i] = '\0';
  if(c != (-1))
    ungetch(c);
  return '0';
}

canonical_original_code:
int getop(char s[]){
  int i; int c;
  while((s[0] = c = getch()) == ' ' || c == '\t');
  s[1] = '\0';
  if(!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit) && c != '.')
    return c;
  i = 0;
  if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit));
  if(c == '.')
    while(((*__ctype_b_loc ())[(int) ((s[++i] = c = getch()))] & (unsigned short) _ISdigit));
  s[i] = '\0';
  if(c != (-1))
    ungetch(c);
  return '0';
}


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx

while ( 1 )
{
v3 = getcharch8(*(_QWORD *)&argc, argv, envp);
if ( !v3 || v3 == (*_ctype_tolower_loc())[113] )
break;
*(_QWORD *)&argc = v3;
putchar(v3);
}
return 0;
}

original_code:
int main(int argc, char *argv[]){
  int c;
  while((c = getcharch8()) && (c != tolower('q'))){
    printf("%c", c);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


lowerString

decompiled_code:
__int64 __fastcall lowerString(unsigned __int8 *a1)
{
__int64 result; // rax

for ( result = *a1; (_BYTE)result; result = *a1 )
{
if ( (unsigned __int8)(result - 65) < 0x1Au )
LOBYTE(result) = result + 32;
*a1++ = result;
}
return result;
}

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

for ( result = *a1; (_BYTE)result; result = *a1 )
{
if ( (unsigned __int8)(result - 65) < 0x1Au )
LOBYTE(result) = result + 32;
*a1++ = result;
}
return result;
}

original_code:
void lowerString(char word[]){
 int i = 0;
 while(word[i] != '\0'){
  word[i] = (word[i] >= 'A' && word[i] <= 'Z') ? word[i] + 'a'-'A' : word[i];
  i++;
 }
}

variable_types:
word
char []